targhooks.c: New file.
[platform/upstream/gcc.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 /* Try to combine the insns I1 and I2 into I3.
1442    Here I1 and I2 appear earlier than I3.
1443    I1 can be zero; then we combine just I2 into I3.
1444
1445    If we are combining three insns and the resulting insn is not recognized,
1446    try splitting it into two insns.  If that happens, I2 and I3 are retained
1447    and I1 is pseudo-deleted by turning it into a NOTE.  Otherwise, I1 and I2
1448    are pseudo-deleted.
1449
1450    Return 0 if the combination does not work.  Then nothing is changed.
1451    If we did the combination, return the insn at which combine should
1452    resume scanning.
1453
1454    Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
1455    new direct jump instruction.  */
1456
1457 static rtx
1458 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
1459 {
1460   /* New patterns for I3 and I2, respectively.  */
1461   rtx newpat, newi2pat = 0;
1462   int substed_i2 = 0, substed_i1 = 0;
1463   /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead.  */
1464   int added_sets_1, added_sets_2;
1465   /* Total number of SETs to put into I3.  */
1466   int total_sets;
1467   /* Nonzero is I2's body now appears in I3.  */
1468   int i2_is_used;
1469   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
1470   int insn_code_number, i2_code_number = 0, other_code_number = 0;
1471   /* Contains I3 if the destination of I3 is used in its source, which means
1472      that the old life of I3 is being killed.  If that usage is placed into
1473      I2 and not in I3, a REG_DEAD note must be made.  */
1474   rtx i3dest_killed = 0;
1475   /* SET_DEST and SET_SRC of I2 and I1.  */
1476   rtx i2dest, i2src, i1dest = 0, i1src = 0;
1477   /* PATTERN (I2), or a copy of it in certain cases.  */
1478   rtx i2pat;
1479   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
1480   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1481   int i1_feeds_i3 = 0;
1482   /* Notes that must be added to REG_NOTES in I3 and I2.  */
1483   rtx new_i3_notes, new_i2_notes;
1484   /* Notes that we substituted I3 into I2 instead of the normal case.  */
1485   int i3_subst_into_i2 = 0;
1486   /* Notes that I1, I2 or I3 is a MULT operation.  */
1487   int have_mult = 0;
1488
1489   int maxreg;
1490   rtx temp;
1491   rtx link;
1492   int i;
1493
1494   /* Exit early if one of the insns involved can't be used for
1495      combinations.  */
1496   if (cant_combine_insn_p (i3)
1497       || cant_combine_insn_p (i2)
1498       || (i1 && cant_combine_insn_p (i1))
1499       /* We also can't do anything if I3 has a
1500          REG_LIBCALL note since we don't want to disrupt the contiguity of a
1501          libcall.  */
1502 #if 0
1503       /* ??? This gives worse code, and appears to be unnecessary, since no
1504          pass after flow uses REG_LIBCALL/REG_RETVAL notes.  */
1505       || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
1506 #endif
1507       )
1508     return 0;
1509
1510   combine_attempts++;
1511   undobuf.other_insn = 0;
1512
1513   /* Reset the hard register usage information.  */
1514   CLEAR_HARD_REG_SET (newpat_used_regs);
1515
1516   /* If I1 and I2 both feed I3, they can be in any order.  To simplify the
1517      code below, set I1 to be the earlier of the two insns.  */
1518   if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1519     temp = i1, i1 = i2, i2 = temp;
1520
1521   added_links_insn = 0;
1522
1523   /* First check for one important special-case that the code below will
1524      not handle.  Namely, the case where I1 is zero, I2 is a PARALLEL
1525      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
1526      we may be able to replace that destination with the destination of I3.
1527      This occurs in the common code where we compute both a quotient and
1528      remainder into a structure, in which case we want to do the computation
1529      directly into the structure to avoid register-register copies.
1530
1531      Note that this case handles both multiple sets in I2 and also
1532      cases where I2 has a number of CLOBBER or PARALLELs.
1533
1534      We make very conservative checks below and only try to handle the
1535      most common cases of this.  For example, we only handle the case
1536      where I2 and I3 are adjacent to avoid making difficult register
1537      usage tests.  */
1538
1539   if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
1540       && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1541       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1542       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1543       && GET_CODE (PATTERN (i2)) == PARALLEL
1544       && ! side_effects_p (SET_DEST (PATTERN (i3)))
1545       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1546          below would need to check what is inside (and reg_overlap_mentioned_p
1547          doesn't support those codes anyway).  Don't allow those destinations;
1548          the resulting insn isn't likely to be recognized anyway.  */
1549       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1550       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1551       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1552                                     SET_DEST (PATTERN (i3)))
1553       && next_real_insn (i2) == i3)
1554     {
1555       rtx p2 = PATTERN (i2);
1556
1557       /* Make sure that the destination of I3,
1558          which we are going to substitute into one output of I2,
1559          is not used within another output of I2.  We must avoid making this:
1560          (parallel [(set (mem (reg 69)) ...)
1561                     (set (reg 69) ...)])
1562          which is not well-defined as to order of actions.
1563          (Besides, reload can't handle output reloads for this.)
1564
1565          The problem can also happen if the dest of I3 is a memory ref,
1566          if another dest in I2 is an indirect memory ref.  */
1567       for (i = 0; i < XVECLEN (p2, 0); i++)
1568         if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1569              || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1570             && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1571                                         SET_DEST (XVECEXP (p2, 0, i))))
1572           break;
1573
1574       if (i == XVECLEN (p2, 0))
1575         for (i = 0; i < XVECLEN (p2, 0); i++)
1576           if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1577                || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1578               && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1579             {
1580               combine_merges++;
1581
1582               subst_insn = i3;
1583               subst_low_cuid = INSN_CUID (i2);
1584
1585               added_sets_2 = added_sets_1 = 0;
1586               i2dest = SET_SRC (PATTERN (i3));
1587
1588               /* Replace the dest in I2 with our dest and make the resulting
1589                  insn the new pattern for I3.  Then skip to where we
1590                  validate the pattern.  Everything was set up above.  */
1591               SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1592                      SET_DEST (PATTERN (i3)));
1593
1594               newpat = p2;
1595               i3_subst_into_i2 = 1;
1596               goto validate_replacement;
1597             }
1598     }
1599
1600   /* If I2 is setting a double-word pseudo to a constant and I3 is setting
1601      one of those words to another constant, merge them by making a new
1602      constant.  */
1603   if (i1 == 0
1604       && (temp = single_set (i2)) != 0
1605       && (GET_CODE (SET_SRC (temp)) == CONST_INT
1606           || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
1607       && GET_CODE (SET_DEST (temp)) == REG
1608       && GET_MODE_CLASS (GET_MODE (SET_DEST (temp))) == MODE_INT
1609       && GET_MODE_SIZE (GET_MODE (SET_DEST (temp))) == 2 * UNITS_PER_WORD
1610       && GET_CODE (PATTERN (i3)) == SET
1611       && GET_CODE (SET_DEST (PATTERN (i3))) == SUBREG
1612       && SUBREG_REG (SET_DEST (PATTERN (i3))) == SET_DEST (temp)
1613       && GET_MODE_CLASS (GET_MODE (SET_DEST (PATTERN (i3)))) == MODE_INT
1614       && GET_MODE_SIZE (GET_MODE (SET_DEST (PATTERN (i3)))) == UNITS_PER_WORD
1615       && GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT)
1616     {
1617       HOST_WIDE_INT lo, hi;
1618
1619       if (GET_CODE (SET_SRC (temp)) == CONST_INT)
1620         lo = INTVAL (SET_SRC (temp)), hi = lo < 0 ? -1 : 0;
1621       else
1622         {
1623           lo = CONST_DOUBLE_LOW (SET_SRC (temp));
1624           hi = CONST_DOUBLE_HIGH (SET_SRC (temp));
1625         }
1626
1627       if (subreg_lowpart_p (SET_DEST (PATTERN (i3))))
1628         {
1629           /* We don't handle the case of the target word being wider
1630              than a host wide int.  */
1631           if (HOST_BITS_PER_WIDE_INT < BITS_PER_WORD)
1632             abort ();
1633
1634           lo &= ~(UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1);
1635           lo |= (INTVAL (SET_SRC (PATTERN (i3)))
1636                  & (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1637         }
1638       else if (HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
1639         hi = INTVAL (SET_SRC (PATTERN (i3)));
1640       else if (HOST_BITS_PER_WIDE_INT >= 2 * BITS_PER_WORD)
1641         {
1642           int sign = -(int) ((unsigned HOST_WIDE_INT) lo
1643                              >> (HOST_BITS_PER_WIDE_INT - 1));
1644
1645           lo &= ~ (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1646                    (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1647           lo |= (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1648                  (INTVAL (SET_SRC (PATTERN (i3)))));
1649           if (hi == sign)
1650             hi = lo < 0 ? -1 : 0;
1651         }
1652       else
1653         /* We don't handle the case of the higher word not fitting
1654            entirely in either hi or lo.  */
1655         abort ();
1656
1657       combine_merges++;
1658       subst_insn = i3;
1659       subst_low_cuid = INSN_CUID (i2);
1660       added_sets_2 = added_sets_1 = 0;
1661       i2dest = SET_DEST (temp);
1662
1663       SUBST (SET_SRC (temp),
1664              immed_double_const (lo, hi, GET_MODE (SET_DEST (temp))));
1665
1666       newpat = PATTERN (i2);
1667       goto validate_replacement;
1668     }
1669
1670 #ifndef HAVE_cc0
1671   /* If we have no I1 and I2 looks like:
1672         (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1673                    (set Y OP)])
1674      make up a dummy I1 that is
1675         (set Y OP)
1676      and change I2 to be
1677         (set (reg:CC X) (compare:CC Y (const_int 0)))
1678
1679      (We can ignore any trailing CLOBBERs.)
1680
1681      This undoes a previous combination and allows us to match a branch-and-
1682      decrement insn.  */
1683
1684   if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1685       && XVECLEN (PATTERN (i2), 0) >= 2
1686       && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1687       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1688           == MODE_CC)
1689       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1690       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1691       && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1692       && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
1693       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1694                       SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1695     {
1696       for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1697         if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1698           break;
1699
1700       if (i == 1)
1701         {
1702           /* We make I1 with the same INSN_UID as I2.  This gives it
1703              the same INSN_CUID for value tracking.  Our fake I1 will
1704              never appear in the insn stream so giving it the same INSN_UID
1705              as I2 will not cause a problem.  */
1706
1707           i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1708                              BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
1709                              XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1710                              NULL_RTX);
1711
1712           SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1713           SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1714                  SET_DEST (PATTERN (i1)));
1715         }
1716     }
1717 #endif
1718
1719   /* Verify that I2 and I1 are valid for combining.  */
1720   if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1721       || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1722     {
1723       undo_all ();
1724       return 0;
1725     }
1726
1727   /* Record whether I2DEST is used in I2SRC and similarly for the other
1728      cases.  Knowing this will help in register status updating below.  */
1729   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1730   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1731   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1732
1733   /* See if I1 directly feeds into I3.  It does if I1DEST is not used
1734      in I2SRC.  */
1735   i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1736
1737   /* Ensure that I3's pattern can be the destination of combines.  */
1738   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1739                           i1 && i2dest_in_i1src && i1_feeds_i3,
1740                           &i3dest_killed))
1741     {
1742       undo_all ();
1743       return 0;
1744     }
1745
1746   /* See if any of the insns is a MULT operation.  Unless one is, we will
1747      reject a combination that is, since it must be slower.  Be conservative
1748      here.  */
1749   if (GET_CODE (i2src) == MULT
1750       || (i1 != 0 && GET_CODE (i1src) == MULT)
1751       || (GET_CODE (PATTERN (i3)) == SET
1752           && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1753     have_mult = 1;
1754
1755   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1756      We used to do this EXCEPT in one case: I3 has a post-inc in an
1757      output operand.  However, that exception can give rise to insns like
1758         mov r3,(r3)+
1759      which is a famous insn on the PDP-11 where the value of r3 used as the
1760      source was model-dependent.  Avoid this sort of thing.  */
1761
1762 #if 0
1763   if (!(GET_CODE (PATTERN (i3)) == SET
1764         && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1765         && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
1766         && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1767             || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1768     /* It's not the exception.  */
1769 #endif
1770 #ifdef AUTO_INC_DEC
1771     for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1772       if (REG_NOTE_KIND (link) == REG_INC
1773           && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1774               || (i1 != 0
1775                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1776         {
1777           undo_all ();
1778           return 0;
1779         }
1780 #endif
1781
1782   /* See if the SETs in I1 or I2 need to be kept around in the merged
1783      instruction: whenever the value set there is still needed past I3.
1784      For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1785
1786      For the SET in I1, we have two cases:  If I1 and I2 independently
1787      feed into I3, the set in I1 needs to be kept around if I1DEST dies
1788      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
1789      in I1 needs to be kept around unless I1DEST dies or is set in either
1790      I2 or I3.  We can distinguish these cases by seeing if I2SRC mentions
1791      I1DEST.  If so, we know I1 feeds into I2.  */
1792
1793   added_sets_2 = ! dead_or_set_p (i3, i2dest);
1794
1795   added_sets_1
1796     = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1797                : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1798
1799   /* If the set in I2 needs to be kept around, we must make a copy of
1800      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1801      PATTERN (I2), we are only substituting for the original I1DEST, not into
1802      an already-substituted copy.  This also prevents making self-referential
1803      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1804      I2DEST.  */
1805
1806   i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1807            ? gen_rtx_SET (VOIDmode, i2dest, i2src)
1808            : PATTERN (i2));
1809
1810   if (added_sets_2)
1811     i2pat = copy_rtx (i2pat);
1812
1813   combine_merges++;
1814
1815   /* Substitute in the latest insn for the regs set by the earlier ones.  */
1816
1817   maxreg = max_reg_num ();
1818
1819   subst_insn = i3;
1820
1821   /* It is possible that the source of I2 or I1 may be performing an
1822      unneeded operation, such as a ZERO_EXTEND of something that is known
1823      to have the high part zero.  Handle that case by letting subst look at
1824      the innermost one of them.
1825
1826      Another way to do this would be to have a function that tries to
1827      simplify a single insn instead of merging two or more insns.  We don't
1828      do this because of the potential of infinite loops and because
1829      of the potential extra memory required.  However, doing it the way
1830      we are is a bit of a kludge and doesn't catch all cases.
1831
1832      But only do this if -fexpensive-optimizations since it slows things down
1833      and doesn't usually win.  */
1834
1835   if (flag_expensive_optimizations)
1836     {
1837       /* Pass pc_rtx so no substitutions are done, just simplifications.
1838          The cases that we are interested in here do not involve the few
1839          cases were is_replaced is checked.  */
1840       if (i1)
1841         {
1842           subst_low_cuid = INSN_CUID (i1);
1843           i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1844         }
1845       else
1846         {
1847           subst_low_cuid = INSN_CUID (i2);
1848           i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1849         }
1850     }
1851
1852 #ifndef HAVE_cc0
1853   /* Many machines that don't use CC0 have insns that can both perform an
1854      arithmetic operation and set the condition code.  These operations will
1855      be represented as a PARALLEL with the first element of the vector
1856      being a COMPARE of an arithmetic operation with the constant zero.
1857      The second element of the vector will set some pseudo to the result
1858      of the same arithmetic operation.  If we simplify the COMPARE, we won't
1859      match such a pattern and so will generate an extra insn.   Here we test
1860      for this case, where both the comparison and the operation result are
1861      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1862      I2SRC.  Later we will make the PARALLEL that contains I2.  */
1863
1864   if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1865       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1866       && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1867       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
1868     {
1869 #ifdef EXTRA_CC_MODES
1870       rtx *cc_use;
1871       enum machine_mode compare_mode;
1872 #endif
1873
1874       newpat = PATTERN (i3);
1875       SUBST (XEXP (SET_SRC (newpat), 0), i2src);
1876
1877       i2_is_used = 1;
1878
1879 #ifdef EXTRA_CC_MODES
1880       /* See if a COMPARE with the operand we substituted in should be done
1881          with the mode that is currently being used.  If not, do the same
1882          processing we do in `subst' for a SET; namely, if the destination
1883          is used only once, try to replace it with a register of the proper
1884          mode and also replace the COMPARE.  */
1885       if (undobuf.other_insn == 0
1886           && (cc_use = find_single_use (SET_DEST (newpat), i3,
1887                                         &undobuf.other_insn))
1888           && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
1889                                               i2src, const0_rtx))
1890               != GET_MODE (SET_DEST (newpat))))
1891         {
1892           unsigned int regno = REGNO (SET_DEST (newpat));
1893           rtx new_dest = gen_rtx_REG (compare_mode, regno);
1894
1895           if (regno < FIRST_PSEUDO_REGISTER
1896               || (REG_N_SETS (regno) == 1 && ! added_sets_2
1897                   && ! REG_USERVAR_P (SET_DEST (newpat))))
1898             {
1899               if (regno >= FIRST_PSEUDO_REGISTER)
1900                 SUBST (regno_reg_rtx[regno], new_dest);
1901
1902               SUBST (SET_DEST (newpat), new_dest);
1903               SUBST (XEXP (*cc_use, 0), new_dest);
1904               SUBST (SET_SRC (newpat),
1905                      gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
1906             }
1907           else
1908             undobuf.other_insn = 0;
1909         }
1910 #endif
1911     }
1912   else
1913 #endif
1914     {
1915       n_occurrences = 0;                /* `subst' counts here */
1916
1917       /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
1918          need to make a unique copy of I2SRC each time we substitute it
1919          to avoid self-referential rtl.  */
1920
1921       subst_low_cuid = INSN_CUID (i2);
1922       newpat = subst (PATTERN (i3), i2dest, i2src, 0,
1923                       ! i1_feeds_i3 && i1dest_in_i1src);
1924       substed_i2 = 1;
1925
1926       /* Record whether i2's body now appears within i3's body.  */
1927       i2_is_used = n_occurrences;
1928     }
1929
1930   /* If we already got a failure, don't try to do more.  Otherwise,
1931      try to substitute in I1 if we have it.  */
1932
1933   if (i1 && GET_CODE (newpat) != CLOBBER)
1934     {
1935       /* Before we can do this substitution, we must redo the test done
1936          above (see detailed comments there) that ensures  that I1DEST
1937          isn't mentioned in any SETs in NEWPAT that are field assignments.  */
1938
1939       if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
1940                               0, (rtx*) 0))
1941         {
1942           undo_all ();
1943           return 0;
1944         }
1945
1946       n_occurrences = 0;
1947       subst_low_cuid = INSN_CUID (i1);
1948       newpat = subst (newpat, i1dest, i1src, 0, 0);
1949       substed_i1 = 1;
1950     }
1951
1952   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
1953      to count all the ways that I2SRC and I1SRC can be used.  */
1954   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
1955        && i2_is_used + added_sets_2 > 1)
1956       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
1957           && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
1958               > 1))
1959       /* Fail if we tried to make a new register (we used to abort, but there's
1960          really no reason to).  */
1961       || max_reg_num () != maxreg
1962       /* Fail if we couldn't do something and have a CLOBBER.  */
1963       || GET_CODE (newpat) == CLOBBER
1964       /* Fail if this new pattern is a MULT and we didn't have one before
1965          at the outer level.  */
1966       || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
1967           && ! have_mult))
1968     {
1969       undo_all ();
1970       return 0;
1971     }
1972
1973   /* If the actions of the earlier insns must be kept
1974      in addition to substituting them into the latest one,
1975      we must make a new PARALLEL for the latest insn
1976      to hold additional the SETs.  */
1977
1978   if (added_sets_1 || added_sets_2)
1979     {
1980       combine_extras++;
1981
1982       if (GET_CODE (newpat) == PARALLEL)
1983         {
1984           rtvec old = XVEC (newpat, 0);
1985           total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
1986           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
1987           memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
1988                   sizeof (old->elem[0]) * old->num_elem);
1989         }
1990       else
1991         {
1992           rtx old = newpat;
1993           total_sets = 1 + added_sets_1 + added_sets_2;
1994           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
1995           XVECEXP (newpat, 0, 0) = old;
1996         }
1997
1998       if (added_sets_1)
1999         XVECEXP (newpat, 0, --total_sets)
2000           = (GET_CODE (PATTERN (i1)) == PARALLEL
2001              ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
2002
2003       if (added_sets_2)
2004         {
2005           /* If there is no I1, use I2's body as is.  We used to also not do
2006              the subst call below if I2 was substituted into I3,
2007              but that could lose a simplification.  */
2008           if (i1 == 0)
2009             XVECEXP (newpat, 0, --total_sets) = i2pat;
2010           else
2011             /* See comment where i2pat is assigned.  */
2012             XVECEXP (newpat, 0, --total_sets)
2013               = subst (i2pat, i1dest, i1src, 0, 0);
2014         }
2015     }
2016
2017   /* We come here when we are replacing a destination in I2 with the
2018      destination of I3.  */
2019  validate_replacement:
2020
2021   /* Note which hard regs this insn has as inputs.  */
2022   mark_used_regs_combine (newpat);
2023
2024   /* Is the result of combination a valid instruction?  */
2025   insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2026
2027   /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2028      the second SET's destination is a register that is unused.  In that case,
2029      we just need the first SET.   This can occur when simplifying a divmod
2030      insn.  We *must* test for this case here because the code below that
2031      splits two independent SETs doesn't handle this case correctly when it
2032      updates the register status.  Also check the case where the first
2033      SET's destination is unused.  That would not cause incorrect code, but
2034      does cause an unneeded insn to remain.  */
2035
2036   if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
2037       && XVECLEN (newpat, 0) == 2
2038       && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2039       && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2040       && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == REG
2041       && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 1)))
2042       && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 1)))
2043       && asm_noperands (newpat) < 0)
2044     {
2045       newpat = XVECEXP (newpat, 0, 0);
2046       insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2047     }
2048
2049   else if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
2050            && XVECLEN (newpat, 0) == 2
2051            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2052            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2053            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) == REG
2054            && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 0)))
2055            && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 0)))
2056            && asm_noperands (newpat) < 0)
2057     {
2058       newpat = XVECEXP (newpat, 0, 1);
2059       insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2060     }
2061
2062   /* If we were combining three insns and the result is a simple SET
2063      with no ASM_OPERANDS that wasn't recognized, try to split it into two
2064      insns.  There are two ways to do this.  It can be split using a
2065      machine-specific method (like when you have an addition of a large
2066      constant) or by combine in the function find_split_point.  */
2067
2068   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2069       && asm_noperands (newpat) < 0)
2070     {
2071       rtx m_split, *split;
2072       rtx ni2dest = i2dest;
2073
2074       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
2075          use I2DEST as a scratch register will help.  In the latter case,
2076          convert I2DEST to the mode of the source of NEWPAT if we can.  */
2077
2078       m_split = split_insns (newpat, i3);
2079
2080       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2081          inputs of NEWPAT.  */
2082
2083       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2084          possible to try that as a scratch reg.  This would require adding
2085          more code to make it work though.  */
2086
2087       if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
2088         {
2089           /* If I2DEST is a hard register or the only use of a pseudo,
2090              we can change its mode.  */
2091           if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
2092               && GET_MODE (SET_DEST (newpat)) != VOIDmode
2093               && GET_CODE (i2dest) == REG
2094               && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2095                   || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2096                       && ! REG_USERVAR_P (i2dest))))
2097             ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
2098                                    REGNO (i2dest));
2099
2100           m_split = split_insns (gen_rtx_PARALLEL
2101                                  (VOIDmode,
2102                                   gen_rtvec (2, newpat,
2103                                              gen_rtx_CLOBBER (VOIDmode,
2104                                                               ni2dest))),
2105                                  i3);
2106           /* If the split with the mode-changed register didn't work, try
2107              the original register.  */
2108           if (! m_split && ni2dest != i2dest)
2109             {
2110               ni2dest = i2dest;
2111               m_split = split_insns (gen_rtx_PARALLEL
2112                                      (VOIDmode,
2113                                       gen_rtvec (2, newpat,
2114                                                  gen_rtx_CLOBBER (VOIDmode,
2115                                                                   i2dest))),
2116                                      i3);
2117             }
2118         }
2119
2120       if (m_split && NEXT_INSN (m_split) == NULL_RTX)
2121         {
2122           m_split = PATTERN (m_split);
2123           insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
2124           if (insn_code_number >= 0)
2125             newpat = m_split;
2126         }
2127       else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
2128                && (next_real_insn (i2) == i3
2129                    || ! use_crosses_set_p (PATTERN (m_split), INSN_CUID (i2))))
2130         {
2131           rtx i2set, i3set;
2132           rtx newi3pat = PATTERN (NEXT_INSN (m_split));
2133           newi2pat = PATTERN (m_split);
2134
2135           i3set = single_set (NEXT_INSN (m_split));
2136           i2set = single_set (m_split);
2137
2138           /* In case we changed the mode of I2DEST, replace it in the
2139              pseudo-register table here.  We can't do it above in case this
2140              code doesn't get executed and we do a split the other way.  */
2141
2142           if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2143             SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
2144
2145           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2146
2147           /* If I2 or I3 has multiple SETs, we won't know how to track
2148              register status, so don't use these insns.  If I2's destination
2149              is used between I2 and I3, we also can't use these insns.  */
2150
2151           if (i2_code_number >= 0 && i2set && i3set
2152               && (next_real_insn (i2) == i3
2153                   || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
2154             insn_code_number = recog_for_combine (&newi3pat, i3,
2155                                                   &new_i3_notes);
2156           if (insn_code_number >= 0)
2157             newpat = newi3pat;
2158
2159           /* It is possible that both insns now set the destination of I3.
2160              If so, we must show an extra use of it.  */
2161
2162           if (insn_code_number >= 0)
2163             {
2164               rtx new_i3_dest = SET_DEST (i3set);
2165               rtx new_i2_dest = SET_DEST (i2set);
2166
2167               while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
2168                      || GET_CODE (new_i3_dest) == STRICT_LOW_PART
2169                      || GET_CODE (new_i3_dest) == SUBREG)
2170                 new_i3_dest = XEXP (new_i3_dest, 0);
2171
2172               while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
2173                      || GET_CODE (new_i2_dest) == STRICT_LOW_PART
2174                      || GET_CODE (new_i2_dest) == SUBREG)
2175                 new_i2_dest = XEXP (new_i2_dest, 0);
2176
2177               if (GET_CODE (new_i3_dest) == REG
2178                   && GET_CODE (new_i2_dest) == REG
2179                   && REGNO (new_i3_dest) == REGNO (new_i2_dest))
2180                 REG_N_SETS (REGNO (new_i2_dest))++;
2181             }
2182         }
2183
2184       /* If we can split it and use I2DEST, go ahead and see if that
2185          helps things be recognized.  Verify that none of the registers
2186          are set between I2 and I3.  */
2187       if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
2188 #ifdef HAVE_cc0
2189           && GET_CODE (i2dest) == REG
2190 #endif
2191           /* We need I2DEST in the proper mode.  If it is a hard register
2192              or the only use of a pseudo, we can change its mode.  */
2193           && (GET_MODE (*split) == GET_MODE (i2dest)
2194               || GET_MODE (*split) == VOIDmode
2195               || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2196               || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2197                   && ! REG_USERVAR_P (i2dest)))
2198           && (next_real_insn (i2) == i3
2199               || ! use_crosses_set_p (*split, INSN_CUID (i2)))
2200           /* We can't overwrite I2DEST if its value is still used by
2201              NEWPAT.  */
2202           && ! reg_referenced_p (i2dest, newpat))
2203         {
2204           rtx newdest = i2dest;
2205           enum rtx_code split_code = GET_CODE (*split);
2206           enum machine_mode split_mode = GET_MODE (*split);
2207
2208           /* Get NEWDEST as a register in the proper mode.  We have already
2209              validated that we can do this.  */
2210           if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
2211             {
2212               newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
2213
2214               if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2215                 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2216             }
2217
2218           /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2219              an ASHIFT.  This can occur if it was inside a PLUS and hence
2220              appeared to be a memory address.  This is a kludge.  */
2221           if (split_code == MULT
2222               && GET_CODE (XEXP (*split, 1)) == CONST_INT
2223               && INTVAL (XEXP (*split, 1)) > 0
2224               && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2225             {
2226               SUBST (*split, gen_rtx_ASHIFT (split_mode,
2227                                              XEXP (*split, 0), GEN_INT (i)));
2228               /* Update split_code because we may not have a multiply
2229                  anymore.  */
2230               split_code = GET_CODE (*split);
2231             }
2232
2233 #ifdef INSN_SCHEDULING
2234           /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2235              be written as a ZERO_EXTEND.  */
2236           if (split_code == SUBREG && GET_CODE (SUBREG_REG (*split)) == MEM)
2237             {
2238 #ifdef LOAD_EXTEND_OP
2239               /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
2240                  what it really is.  */
2241               if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
2242                   == SIGN_EXTEND)
2243                 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
2244                                                     SUBREG_REG (*split)));
2245               else
2246 #endif
2247                 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
2248                                                     SUBREG_REG (*split)));
2249             }
2250 #endif
2251
2252           newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
2253           SUBST (*split, newdest);
2254           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2255
2256           /* If the split point was a MULT and we didn't have one before,
2257              don't use one now.  */
2258           if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2259             insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2260         }
2261     }
2262
2263   /* Check for a case where we loaded from memory in a narrow mode and
2264      then sign extended it, but we need both registers.  In that case,
2265      we have a PARALLEL with both loads from the same memory location.
2266      We can split this into a load from memory followed by a register-register
2267      copy.  This saves at least one insn, more if register allocation can
2268      eliminate the copy.
2269
2270      We cannot do this if the destination of the first assignment is a
2271      condition code register or cc0.  We eliminate this case by making sure
2272      the SET_DEST and SET_SRC have the same mode.
2273
2274      We cannot do this if the destination of the second assignment is
2275      a register that we have already assumed is zero-extended.  Similarly
2276      for a SUBREG of such a register.  */
2277
2278   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2279            && GET_CODE (newpat) == PARALLEL
2280            && XVECLEN (newpat, 0) == 2
2281            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2282            && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2283            && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
2284                == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
2285            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2286            && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2287                            XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2288            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2289                                    INSN_CUID (i2))
2290            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2291            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2292            && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2293                  (GET_CODE (temp) == REG
2294                   && reg_nonzero_bits[REGNO (temp)] != 0
2295                   && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2296                   && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2297                   && (reg_nonzero_bits[REGNO (temp)]
2298                       != GET_MODE_MASK (word_mode))))
2299            && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2300                  && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2301                      (GET_CODE (temp) == REG
2302                       && reg_nonzero_bits[REGNO (temp)] != 0
2303                       && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2304                       && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2305                       && (reg_nonzero_bits[REGNO (temp)]
2306                           != GET_MODE_MASK (word_mode)))))
2307            && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2308                                          SET_SRC (XVECEXP (newpat, 0, 1)))
2309            && ! find_reg_note (i3, REG_UNUSED,
2310                                SET_DEST (XVECEXP (newpat, 0, 0))))
2311     {
2312       rtx ni2dest;
2313
2314       newi2pat = XVECEXP (newpat, 0, 0);
2315       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2316       newpat = XVECEXP (newpat, 0, 1);
2317       SUBST (SET_SRC (newpat),
2318              gen_lowpart_for_combine (GET_MODE (SET_SRC (newpat)), ni2dest));
2319       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2320
2321       if (i2_code_number >= 0)
2322         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2323
2324       if (insn_code_number >= 0)
2325         {
2326           rtx insn;
2327           rtx link;
2328
2329           /* If we will be able to accept this, we have made a change to the
2330              destination of I3.  This can invalidate a LOG_LINKS pointing
2331              to I3.  No other part of combine.c makes such a transformation.
2332
2333              The new I3 will have a destination that was previously the
2334              destination of I1 or I2 and which was used in i2 or I3.  Call
2335              distribute_links to make a LOG_LINK from the next use of
2336              that destination.  */
2337
2338           PATTERN (i3) = newpat;
2339           distribute_links (gen_rtx_INSN_LIST (VOIDmode, i3, NULL_RTX));
2340
2341           /* I3 now uses what used to be its destination and which is
2342              now I2's destination.  That means we need a LOG_LINK from
2343              I3 to I2.  But we used to have one, so we still will.
2344
2345              However, some later insn might be using I2's dest and have
2346              a LOG_LINK pointing at I3.  We must remove this link.
2347              The simplest way to remove the link is to point it at I1,
2348              which we know will be a NOTE.  */
2349
2350           for (insn = NEXT_INSN (i3);
2351                insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2352                         || insn != this_basic_block->next_bb->head);
2353                insn = NEXT_INSN (insn))
2354             {
2355               if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
2356                 {
2357                   for (link = LOG_LINKS (insn); link;
2358                        link = XEXP (link, 1))
2359                     if (XEXP (link, 0) == i3)
2360                       XEXP (link, 0) = i1;
2361
2362                   break;
2363                 }
2364             }
2365         }
2366     }
2367
2368   /* Similarly, check for a case where we have a PARALLEL of two independent
2369      SETs but we started with three insns.  In this case, we can do the sets
2370      as two separate insns.  This case occurs when some SET allows two
2371      other insns to combine, but the destination of that SET is still live.  */
2372
2373   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2374            && GET_CODE (newpat) == PARALLEL
2375            && XVECLEN (newpat, 0) == 2
2376            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2377            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2378            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2379            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2380            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2381            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2382            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2383                                    INSN_CUID (i2))
2384            /* Don't pass sets with (USE (MEM ...)) dests to the following.  */
2385            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2386            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2387            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2388                                   XVECEXP (newpat, 0, 0))
2389            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2390                                   XVECEXP (newpat, 0, 1))
2391            && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
2392                  && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
2393     {
2394       /* Normally, it doesn't matter which of the two is done first,
2395          but it does if one references cc0.  In that case, it has to
2396          be first.  */
2397 #ifdef HAVE_cc0
2398       if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2399         {
2400           newi2pat = XVECEXP (newpat, 0, 0);
2401           newpat = XVECEXP (newpat, 0, 1);
2402         }
2403       else
2404 #endif
2405         {
2406           newi2pat = XVECEXP (newpat, 0, 1);
2407           newpat = XVECEXP (newpat, 0, 0);
2408         }
2409
2410       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2411
2412       if (i2_code_number >= 0)
2413         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2414     }
2415
2416   /* If it still isn't recognized, fail and change things back the way they
2417      were.  */
2418   if ((insn_code_number < 0
2419        /* Is the result a reasonable ASM_OPERANDS?  */
2420        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2421     {
2422       undo_all ();
2423       return 0;
2424     }
2425
2426   /* If we had to change another insn, make sure it is valid also.  */
2427   if (undobuf.other_insn)
2428     {
2429       rtx other_pat = PATTERN (undobuf.other_insn);
2430       rtx new_other_notes;
2431       rtx note, next;
2432
2433       CLEAR_HARD_REG_SET (newpat_used_regs);
2434
2435       other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
2436                                              &new_other_notes);
2437
2438       if (other_code_number < 0 && ! check_asm_operands (other_pat))
2439         {
2440           undo_all ();
2441           return 0;
2442         }
2443
2444       PATTERN (undobuf.other_insn) = other_pat;
2445
2446       /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2447          are still valid.  Then add any non-duplicate notes added by
2448          recog_for_combine.  */
2449       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2450         {
2451           next = XEXP (note, 1);
2452
2453           if (REG_NOTE_KIND (note) == REG_UNUSED
2454               && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2455             {
2456               if (GET_CODE (XEXP (note, 0)) == REG)
2457                 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2458
2459               remove_note (undobuf.other_insn, note);
2460             }
2461         }
2462
2463       for (note = new_other_notes; note; note = XEXP (note, 1))
2464         if (GET_CODE (XEXP (note, 0)) == REG)
2465           REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2466
2467       distribute_notes (new_other_notes, undobuf.other_insn,
2468                         undobuf.other_insn, NULL_RTX);
2469     }
2470 #ifdef HAVE_cc0
2471   /* If I2 is the setter CC0 and I3 is the user CC0 then check whether
2472      they are adjacent to each other or not.  */
2473   {
2474     rtx p = prev_nonnote_insn (i3);
2475     if (p && p != i2 && GET_CODE (p) == INSN && newi2pat
2476         && sets_cc0_p (newi2pat))
2477       {
2478         undo_all ();
2479         return 0;
2480       }
2481   }
2482 #endif
2483
2484   /* We now know that we can do this combination.  Merge the insns and
2485      update the status of registers and LOG_LINKS.  */
2486
2487   {
2488     rtx i3notes, i2notes, i1notes = 0;
2489     rtx i3links, i2links, i1links = 0;
2490     rtx midnotes = 0;
2491     unsigned int regno;
2492
2493     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2494        clear them.  */
2495     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2496     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2497     if (i1)
2498       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2499
2500     /* Ensure that we do not have something that should not be shared but
2501        occurs multiple times in the new insns.  Check this by first
2502        resetting all the `used' flags and then copying anything is shared.  */
2503
2504     reset_used_flags (i3notes);
2505     reset_used_flags (i2notes);
2506     reset_used_flags (i1notes);
2507     reset_used_flags (newpat);
2508     reset_used_flags (newi2pat);
2509     if (undobuf.other_insn)
2510       reset_used_flags (PATTERN (undobuf.other_insn));
2511
2512     i3notes = copy_rtx_if_shared (i3notes);
2513     i2notes = copy_rtx_if_shared (i2notes);
2514     i1notes = copy_rtx_if_shared (i1notes);
2515     newpat = copy_rtx_if_shared (newpat);
2516     newi2pat = copy_rtx_if_shared (newi2pat);
2517     if (undobuf.other_insn)
2518       reset_used_flags (PATTERN (undobuf.other_insn));
2519
2520     INSN_CODE (i3) = insn_code_number;
2521     PATTERN (i3) = newpat;
2522
2523     if (GET_CODE (i3) == CALL_INSN && CALL_INSN_FUNCTION_USAGE (i3))
2524       {
2525         rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
2526
2527         reset_used_flags (call_usage);
2528         call_usage = copy_rtx (call_usage);
2529
2530         if (substed_i2)
2531           replace_rtx (call_usage, i2dest, i2src);
2532
2533         if (substed_i1)
2534           replace_rtx (call_usage, i1dest, i1src);
2535
2536         CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
2537       }
2538
2539     if (undobuf.other_insn)
2540       INSN_CODE (undobuf.other_insn) = other_code_number;
2541
2542     /* We had one special case above where I2 had more than one set and
2543        we replaced a destination of one of those sets with the destination
2544        of I3.  In that case, we have to update LOG_LINKS of insns later
2545        in this basic block.  Note that this (expensive) case is rare.
2546
2547        Also, in this case, we must pretend that all REG_NOTEs for I2
2548        actually came from I3, so that REG_UNUSED notes from I2 will be
2549        properly handled.  */
2550
2551     if (i3_subst_into_i2)
2552       {
2553         for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2554           if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != USE
2555               && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
2556               && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2557               && ! find_reg_note (i2, REG_UNUSED,
2558                                   SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2559             for (temp = NEXT_INSN (i2);
2560                  temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2561                           || this_basic_block->head != temp);
2562                  temp = NEXT_INSN (temp))
2563               if (temp != i3 && INSN_P (temp))
2564                 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2565                   if (XEXP (link, 0) == i2)
2566                     XEXP (link, 0) = i3;
2567
2568         if (i3notes)
2569           {
2570             rtx link = i3notes;
2571             while (XEXP (link, 1))
2572               link = XEXP (link, 1);
2573             XEXP (link, 1) = i2notes;
2574           }
2575         else
2576           i3notes = i2notes;
2577         i2notes = 0;
2578       }
2579
2580     LOG_LINKS (i3) = 0;
2581     REG_NOTES (i3) = 0;
2582     LOG_LINKS (i2) = 0;
2583     REG_NOTES (i2) = 0;
2584
2585     if (newi2pat)
2586       {
2587         INSN_CODE (i2) = i2_code_number;
2588         PATTERN (i2) = newi2pat;
2589       }
2590     else
2591       {
2592         PUT_CODE (i2, NOTE);
2593         NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
2594         NOTE_SOURCE_FILE (i2) = 0;
2595       }
2596
2597     if (i1)
2598       {
2599         LOG_LINKS (i1) = 0;
2600         REG_NOTES (i1) = 0;
2601         PUT_CODE (i1, NOTE);
2602         NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
2603         NOTE_SOURCE_FILE (i1) = 0;
2604       }
2605
2606     /* Get death notes for everything that is now used in either I3 or
2607        I2 and used to die in a previous insn.  If we built two new
2608        patterns, move from I1 to I2 then I2 to I3 so that we get the
2609        proper movement on registers that I2 modifies.  */
2610
2611     if (newi2pat)
2612       {
2613         move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2614         move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2615       }
2616     else
2617       move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2618                    i3, &midnotes);
2619
2620     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
2621     if (i3notes)
2622       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX);
2623     if (i2notes)
2624       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX);
2625     if (i1notes)
2626       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX);
2627     if (midnotes)
2628       distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2629
2630     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
2631        know these are REG_UNUSED and want them to go to the desired insn,
2632        so we always pass it as i3.  We have not counted the notes in
2633        reg_n_deaths yet, so we need to do so now.  */
2634
2635     if (newi2pat && new_i2_notes)
2636       {
2637         for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2638           if (GET_CODE (XEXP (temp, 0)) == REG)
2639             REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2640
2641         distribute_notes (new_i2_notes, i2, i2, NULL_RTX);
2642       }
2643
2644     if (new_i3_notes)
2645       {
2646         for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2647           if (GET_CODE (XEXP (temp, 0)) == REG)
2648             REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2649
2650         distribute_notes (new_i3_notes, i3, i3, NULL_RTX);
2651       }
2652
2653     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
2654        put a REG_DEAD note for it somewhere.  If NEWI2PAT exists and sets
2655        I3DEST, the death must be somewhere before I2, not I3.  If we passed I3
2656        in that case, it might delete I2.  Similarly for I2 and I1.
2657        Show an additional death due to the REG_DEAD note we make here.  If
2658        we discard it in distribute_notes, we will decrement it again.  */
2659
2660     if (i3dest_killed)
2661       {
2662         if (GET_CODE (i3dest_killed) == REG)
2663           REG_N_DEATHS (REGNO (i3dest_killed))++;
2664
2665         if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
2666           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2667                                                NULL_RTX),
2668                             NULL_RTX, i2, NULL_RTX);
2669         else
2670           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2671                                                NULL_RTX),
2672                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2673       }
2674
2675     if (i2dest_in_i2src)
2676       {
2677         if (GET_CODE (i2dest) == REG)
2678           REG_N_DEATHS (REGNO (i2dest))++;
2679
2680         if (newi2pat && reg_set_p (i2dest, newi2pat))
2681           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2682                             NULL_RTX, i2, NULL_RTX);
2683         else
2684           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2685                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2686       }
2687
2688     if (i1dest_in_i1src)
2689       {
2690         if (GET_CODE (i1dest) == REG)
2691           REG_N_DEATHS (REGNO (i1dest))++;
2692
2693         if (newi2pat && reg_set_p (i1dest, newi2pat))
2694           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2695                             NULL_RTX, i2, NULL_RTX);
2696         else
2697           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2698                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2699       }
2700
2701     distribute_links (i3links);
2702     distribute_links (i2links);
2703     distribute_links (i1links);
2704
2705     if (GET_CODE (i2dest) == REG)
2706       {
2707         rtx link;
2708         rtx i2_insn = 0, i2_val = 0, set;
2709
2710         /* The insn that used to set this register doesn't exist, and
2711            this life of the register may not exist either.  See if one of
2712            I3's links points to an insn that sets I2DEST.  If it does,
2713            that is now the last known value for I2DEST. If we don't update
2714            this and I2 set the register to a value that depended on its old
2715            contents, we will get confused.  If this insn is used, thing
2716            will be set correctly in combine_instructions.  */
2717
2718         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2719           if ((set = single_set (XEXP (link, 0))) != 0
2720               && rtx_equal_p (i2dest, SET_DEST (set)))
2721             i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2722
2723         record_value_for_reg (i2dest, i2_insn, i2_val);
2724
2725         /* If the reg formerly set in I2 died only once and that was in I3,
2726            zero its use count so it won't make `reload' do any work.  */
2727         if (! added_sets_2
2728             && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2729             && ! i2dest_in_i2src)
2730           {
2731             regno = REGNO (i2dest);
2732             REG_N_SETS (regno)--;
2733           }
2734       }
2735
2736     if (i1 && GET_CODE (i1dest) == REG)
2737       {
2738         rtx link;
2739         rtx i1_insn = 0, i1_val = 0, set;
2740
2741         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2742           if ((set = single_set (XEXP (link, 0))) != 0
2743               && rtx_equal_p (i1dest, SET_DEST (set)))
2744             i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2745
2746         record_value_for_reg (i1dest, i1_insn, i1_val);
2747
2748         regno = REGNO (i1dest);
2749         if (! added_sets_1 && ! i1dest_in_i1src)
2750           REG_N_SETS (regno)--;
2751       }
2752
2753     /* Update reg_nonzero_bits et al for any changes that may have been made
2754        to this insn.  The order of set_nonzero_bits_and_sign_copies() is
2755        important.  Because newi2pat can affect nonzero_bits of newpat */
2756     if (newi2pat)
2757       note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
2758     note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
2759
2760     /* Set new_direct_jump_p if a new return or simple jump instruction
2761        has been created.
2762
2763        If I3 is now an unconditional jump, ensure that it has a
2764        BARRIER following it since it may have initially been a
2765        conditional jump.  It may also be the last nonnote insn.  */
2766
2767     if (returnjump_p (i3) || any_uncondjump_p (i3))
2768       {
2769         *new_direct_jump_p = 1;
2770         mark_jump_label (PATTERN (i3), i3, 0);
2771
2772         if ((temp = next_nonnote_insn (i3)) == NULL_RTX
2773             || GET_CODE (temp) != BARRIER)
2774           emit_barrier_after (i3);
2775       }
2776
2777     if (undobuf.other_insn != NULL_RTX
2778         && (returnjump_p (undobuf.other_insn)
2779             || any_uncondjump_p (undobuf.other_insn)))
2780       {
2781         *new_direct_jump_p = 1;
2782
2783         if ((temp = next_nonnote_insn (undobuf.other_insn)) == NULL_RTX
2784             || GET_CODE (temp) != BARRIER)
2785           emit_barrier_after (undobuf.other_insn);
2786       }
2787
2788     /* An NOOP jump does not need barrier, but it does need cleaning up
2789        of CFG.  */
2790     if (GET_CODE (newpat) == SET
2791         && SET_SRC (newpat) == pc_rtx
2792         && SET_DEST (newpat) == pc_rtx)
2793       *new_direct_jump_p = 1;
2794   }
2795
2796   combine_successes++;
2797   undo_commit ();
2798
2799   if (added_links_insn
2800       && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
2801       && INSN_CUID (added_links_insn) < INSN_CUID (i3))
2802     return added_links_insn;
2803   else
2804     return newi2pat ? i2 : i3;
2805 }
2806 \f
2807 /* Undo all the modifications recorded in undobuf.  */
2808
2809 static void
2810 undo_all (void)
2811 {
2812   struct undo *undo, *next;
2813
2814   for (undo = undobuf.undos; undo; undo = next)
2815     {
2816       next = undo->next;
2817       if (undo->is_int)
2818         *undo->where.i = undo->old_contents.i;
2819       else
2820         *undo->where.r = undo->old_contents.r;
2821
2822       undo->next = undobuf.frees;
2823       undobuf.frees = undo;
2824     }
2825
2826   undobuf.undos = 0;
2827 }
2828
2829 /* We've committed to accepting the changes we made.  Move all
2830    of the undos to the free list.  */
2831
2832 static void
2833 undo_commit (void)
2834 {
2835   struct undo *undo, *next;
2836
2837   for (undo = undobuf.undos; undo; undo = next)
2838     {
2839       next = undo->next;
2840       undo->next = undobuf.frees;
2841       undobuf.frees = undo;
2842     }
2843   undobuf.undos = 0;
2844 }
2845
2846 \f
2847 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
2848    where we have an arithmetic expression and return that point.  LOC will
2849    be inside INSN.
2850
2851    try_combine will call this function to see if an insn can be split into
2852    two insns.  */
2853
2854 static rtx *
2855 find_split_point (rtx *loc, rtx insn)
2856 {
2857   rtx x = *loc;
2858   enum rtx_code code = GET_CODE (x);
2859   rtx *split;
2860   unsigned HOST_WIDE_INT len = 0;
2861   HOST_WIDE_INT pos = 0;
2862   int unsignedp = 0;
2863   rtx inner = NULL_RTX;
2864
2865   /* First special-case some codes.  */
2866   switch (code)
2867     {
2868     case SUBREG:
2869 #ifdef INSN_SCHEDULING
2870       /* If we are making a paradoxical SUBREG invalid, it becomes a split
2871          point.  */
2872       if (GET_CODE (SUBREG_REG (x)) == MEM)
2873         return loc;
2874 #endif
2875       return find_split_point (&SUBREG_REG (x), insn);
2876
2877     case MEM:
2878 #ifdef HAVE_lo_sum
2879       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
2880          using LO_SUM and HIGH.  */
2881       if (GET_CODE (XEXP (x, 0)) == CONST
2882           || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2883         {
2884           SUBST (XEXP (x, 0),
2885                  gen_rtx_LO_SUM (Pmode,
2886                                  gen_rtx_HIGH (Pmode, XEXP (x, 0)),
2887                                  XEXP (x, 0)));
2888           return &XEXP (XEXP (x, 0), 0);
2889         }
2890 #endif
2891
2892       /* If we have a PLUS whose second operand is a constant and the
2893          address is not valid, perhaps will can split it up using
2894          the machine-specific way to split large constants.  We use
2895          the first pseudo-reg (one of the virtual regs) as a placeholder;
2896          it will not remain in the result.  */
2897       if (GET_CODE (XEXP (x, 0)) == PLUS
2898           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2899           && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
2900         {
2901           rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
2902           rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
2903                                  subst_insn);
2904
2905           /* This should have produced two insns, each of which sets our
2906              placeholder.  If the source of the second is a valid address,
2907              we can make put both sources together and make a split point
2908              in the middle.  */
2909
2910           if (seq
2911               && NEXT_INSN (seq) != NULL_RTX
2912               && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
2913               && GET_CODE (seq) == INSN
2914               && GET_CODE (PATTERN (seq)) == SET
2915               && SET_DEST (PATTERN (seq)) == reg
2916               && ! reg_mentioned_p (reg,
2917                                     SET_SRC (PATTERN (seq)))
2918               && GET_CODE (NEXT_INSN (seq)) == INSN
2919               && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
2920               && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
2921               && memory_address_p (GET_MODE (x),
2922                                    SET_SRC (PATTERN (NEXT_INSN (seq)))))
2923             {
2924               rtx src1 = SET_SRC (PATTERN (seq));
2925               rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
2926
2927               /* Replace the placeholder in SRC2 with SRC1.  If we can
2928                  find where in SRC2 it was placed, that can become our
2929                  split point and we can replace this address with SRC2.
2930                  Just try two obvious places.  */
2931
2932               src2 = replace_rtx (src2, reg, src1);
2933               split = 0;
2934               if (XEXP (src2, 0) == src1)
2935                 split = &XEXP (src2, 0);
2936               else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
2937                        && XEXP (XEXP (src2, 0), 0) == src1)
2938                 split = &XEXP (XEXP (src2, 0), 0);
2939
2940               if (split)
2941                 {
2942                   SUBST (XEXP (x, 0), src2);
2943                   return split;
2944                 }
2945             }
2946
2947           /* If that didn't work, perhaps the first operand is complex and
2948              needs to be computed separately, so make a split point there.
2949              This will occur on machines that just support REG + CONST
2950              and have a constant moved through some previous computation.  */
2951
2952           else if (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) != 'o'
2953                    && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
2954                          && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (XEXP (x, 0), 0))))
2955                              == 'o')))
2956             return &XEXP (XEXP (x, 0), 0);
2957         }
2958       break;
2959
2960     case SET:
2961 #ifdef HAVE_cc0
2962       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
2963          ZERO_EXTRACT, the most likely reason why this doesn't match is that
2964          we need to put the operand into a register.  So split at that
2965          point.  */
2966
2967       if (SET_DEST (x) == cc0_rtx
2968           && GET_CODE (SET_SRC (x)) != COMPARE
2969           && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
2970           && GET_RTX_CLASS (GET_CODE (SET_SRC (x))) != 'o'
2971           && ! (GET_CODE (SET_SRC (x)) == SUBREG
2972                 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (SET_SRC (x)))) == 'o'))
2973         return &SET_SRC (x);
2974 #endif
2975
2976       /* See if we can split SET_SRC as it stands.  */
2977       split = find_split_point (&SET_SRC (x), insn);
2978       if (split && split != &SET_SRC (x))
2979         return split;
2980
2981       /* See if we can split SET_DEST as it stands.  */
2982       split = find_split_point (&SET_DEST (x), insn);
2983       if (split && split != &SET_DEST (x))
2984         return split;
2985
2986       /* See if this is a bitfield assignment with everything constant.  If
2987          so, this is an IOR of an AND, so split it into that.  */
2988       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2989           && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2990               <= HOST_BITS_PER_WIDE_INT)
2991           && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
2992           && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
2993           && GET_CODE (SET_SRC (x)) == CONST_INT
2994           && ((INTVAL (XEXP (SET_DEST (x), 1))
2995                + INTVAL (XEXP (SET_DEST (x), 2)))
2996               <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
2997           && ! side_effects_p (XEXP (SET_DEST (x), 0)))
2998         {
2999           HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
3000           unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
3001           unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
3002           rtx dest = XEXP (SET_DEST (x), 0);
3003           enum machine_mode mode = GET_MODE (dest);
3004           unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
3005
3006           if (BITS_BIG_ENDIAN)
3007             pos = GET_MODE_BITSIZE (mode) - len - pos;
3008
3009           if (src == mask)
3010             SUBST (SET_SRC (x),
3011                    gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
3012           else
3013             SUBST (SET_SRC (x),
3014                    gen_binary (IOR, mode,
3015                                gen_binary (AND, mode, dest,
3016                                            gen_int_mode (~(mask << pos),
3017                                                          mode)),
3018                                GEN_INT (src << pos)));
3019
3020           SUBST (SET_DEST (x), dest);
3021
3022           split = find_split_point (&SET_SRC (x), insn);
3023           if (split && split != &SET_SRC (x))
3024             return split;
3025         }
3026
3027       /* Otherwise, see if this is an operation that we can split into two.
3028          If so, try to split that.  */
3029       code = GET_CODE (SET_SRC (x));
3030
3031       switch (code)
3032         {
3033         case AND:
3034           /* If we are AND'ing with a large constant that is only a single
3035              bit and the result is only being used in a context where we
3036              need to know if it is zero or nonzero, replace it with a bit
3037              extraction.  This will avoid the large constant, which might
3038              have taken more than one insn to make.  If the constant were
3039              not a valid argument to the AND but took only one insn to make,
3040              this is no worse, but if it took more than one insn, it will
3041              be better.  */
3042
3043           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3044               && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
3045               && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
3046               && GET_CODE (SET_DEST (x)) == REG
3047               && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
3048               && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
3049               && XEXP (*split, 0) == SET_DEST (x)
3050               && XEXP (*split, 1) == const0_rtx)
3051             {
3052               rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
3053                                                 XEXP (SET_SRC (x), 0),
3054                                                 pos, NULL_RTX, 1, 1, 0, 0);
3055               if (extraction != 0)
3056                 {
3057                   SUBST (SET_SRC (x), extraction);
3058                   return find_split_point (loc, insn);
3059                 }
3060             }
3061           break;
3062
3063         case NE:
3064           /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
3065              is known to be on, this can be converted into a NEG of a shift.  */
3066           if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
3067               && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
3068               && 1 <= (pos = exact_log2
3069                        (nonzero_bits (XEXP (SET_SRC (x), 0),
3070                                       GET_MODE (XEXP (SET_SRC (x), 0))))))
3071             {
3072               enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
3073
3074               SUBST (SET_SRC (x),
3075                      gen_rtx_NEG (mode,
3076                                   gen_rtx_LSHIFTRT (mode,
3077                                                     XEXP (SET_SRC (x), 0),
3078                                                     GEN_INT (pos))));
3079
3080               split = find_split_point (&SET_SRC (x), insn);
3081               if (split && split != &SET_SRC (x))
3082                 return split;
3083             }
3084           break;
3085
3086         case SIGN_EXTEND:
3087           inner = XEXP (SET_SRC (x), 0);
3088
3089           /* We can't optimize if either mode is a partial integer
3090              mode as we don't know how many bits are significant
3091              in those modes.  */
3092           if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
3093               || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
3094             break;
3095
3096           pos = 0;
3097           len = GET_MODE_BITSIZE (GET_MODE (inner));
3098           unsignedp = 0;
3099           break;
3100
3101         case SIGN_EXTRACT:
3102         case ZERO_EXTRACT:
3103           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3104               && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
3105             {
3106               inner = XEXP (SET_SRC (x), 0);
3107               len = INTVAL (XEXP (SET_SRC (x), 1));
3108               pos = INTVAL (XEXP (SET_SRC (x), 2));
3109
3110               if (BITS_BIG_ENDIAN)
3111                 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
3112               unsignedp = (code == ZERO_EXTRACT);
3113             }
3114           break;
3115
3116         default:
3117           break;
3118         }
3119
3120       if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
3121         {
3122           enum machine_mode mode = GET_MODE (SET_SRC (x));
3123
3124           /* For unsigned, we have a choice of a shift followed by an
3125              AND or two shifts.  Use two shifts for field sizes where the
3126              constant might be too large.  We assume here that we can
3127              always at least get 8-bit constants in an AND insn, which is
3128              true for every current RISC.  */
3129
3130           if (unsignedp && len <= 8)
3131             {
3132               SUBST (SET_SRC (x),
3133                      gen_rtx_AND (mode,
3134                                   gen_rtx_LSHIFTRT
3135                                   (mode, gen_lowpart_for_combine (mode, inner),
3136                                    GEN_INT (pos)),
3137                                   GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
3138
3139               split = find_split_point (&SET_SRC (x), insn);
3140               if (split && split != &SET_SRC (x))
3141                 return split;
3142             }
3143           else
3144             {
3145               SUBST (SET_SRC (x),
3146                      gen_rtx_fmt_ee
3147                      (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
3148                       gen_rtx_ASHIFT (mode,
3149                                       gen_lowpart_for_combine (mode, inner),
3150                                       GEN_INT (GET_MODE_BITSIZE (mode)
3151                                                - len - pos)),
3152                       GEN_INT (GET_MODE_BITSIZE (mode) - len)));
3153
3154               split = find_split_point (&SET_SRC (x), insn);
3155               if (split && split != &SET_SRC (x))
3156                 return split;
3157             }
3158         }
3159
3160       /* See if this is a simple operation with a constant as the second
3161          operand.  It might be that this constant is out of range and hence
3162          could be used as a split point.  */
3163       if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
3164            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
3165            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<')
3166           && CONSTANT_P (XEXP (SET_SRC (x), 1))
3167           && (GET_RTX_CLASS (GET_CODE (XEXP (SET_SRC (x), 0))) == 'o'
3168               || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
3169                   && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (SET_SRC (x), 0))))
3170                       == 'o'))))
3171         return &XEXP (SET_SRC (x), 1);
3172
3173       /* Finally, see if this is a simple operation with its first operand
3174          not in a register.  The operation might require this operand in a
3175          register, so return it as a split point.  We can always do this
3176          because if the first operand were another operation, we would have
3177          already found it as a split point.  */
3178       if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
3179            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
3180            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<'
3181            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '1')
3182           && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
3183         return &XEXP (SET_SRC (x), 0);
3184
3185       return 0;
3186
3187     case AND:
3188     case IOR:
3189       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
3190          it is better to write this as (not (ior A B)) so we can split it.
3191          Similarly for IOR.  */
3192       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
3193         {
3194           SUBST (*loc,
3195                  gen_rtx_NOT (GET_MODE (x),
3196                               gen_rtx_fmt_ee (code == IOR ? AND : IOR,
3197                                               GET_MODE (x),
3198                                               XEXP (XEXP (x, 0), 0),
3199                                               XEXP (XEXP (x, 1), 0))));
3200           return find_split_point (loc, insn);
3201         }
3202
3203       /* Many RISC machines have a large set of logical insns.  If the
3204          second operand is a NOT, put it first so we will try to split the
3205          other operand first.  */
3206       if (GET_CODE (XEXP (x, 1)) == NOT)
3207         {
3208           rtx tem = XEXP (x, 0);
3209           SUBST (XEXP (x, 0), XEXP (x, 1));
3210           SUBST (XEXP (x, 1), tem);
3211         }
3212       break;
3213
3214     default:
3215       break;
3216     }
3217
3218   /* Otherwise, select our actions depending on our rtx class.  */
3219   switch (GET_RTX_CLASS (code))
3220     {
3221     case 'b':                   /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
3222     case '3':
3223       split = find_split_point (&XEXP (x, 2), insn);
3224       if (split)
3225         return split;
3226       /* ... fall through ...  */
3227     case '2':
3228     case 'c':
3229     case '<':
3230       split = find_split_point (&XEXP (x, 1), insn);
3231       if (split)
3232         return split;
3233       /* ... fall through ...  */
3234     case '1':
3235       /* Some machines have (and (shift ...) ...) insns.  If X is not
3236          an AND, but XEXP (X, 0) is, use it as our split point.  */
3237       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
3238         return &XEXP (x, 0);
3239
3240       split = find_split_point (&XEXP (x, 0), insn);
3241       if (split)
3242         return split;
3243       return loc;
3244     }
3245
3246   /* Otherwise, we don't have a split point.  */
3247   return 0;
3248 }
3249 \f
3250 /* Throughout X, replace FROM with TO, and return the result.
3251    The result is TO if X is FROM;
3252    otherwise the result is X, but its contents may have been modified.
3253    If they were modified, a record was made in undobuf so that
3254    undo_all will (among other things) return X to its original state.
3255
3256    If the number of changes necessary is too much to record to undo,
3257    the excess changes are not made, so the result is invalid.
3258    The changes already made can still be undone.
3259    undobuf.num_undo is incremented for such changes, so by testing that
3260    the caller can tell whether the result is valid.
3261
3262    `n_occurrences' is incremented each time FROM is replaced.
3263
3264    IN_DEST is nonzero if we are processing the SET_DEST of a SET.
3265
3266    UNIQUE_COPY is nonzero if each substitution must be unique.  We do this
3267    by copying if `n_occurrences' is nonzero.  */
3268
3269 static rtx
3270 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
3271 {
3272   enum rtx_code code = GET_CODE (x);
3273   enum machine_mode op0_mode = VOIDmode;
3274   const char *fmt;
3275   int len, i;
3276   rtx new;
3277
3278 /* Two expressions are equal if they are identical copies of a shared
3279    RTX or if they are both registers with the same register number
3280    and mode.  */
3281
3282 #define COMBINE_RTX_EQUAL_P(X,Y)                        \
3283   ((X) == (Y)                                           \
3284    || (GET_CODE (X) == REG && GET_CODE (Y) == REG       \
3285        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3286
3287   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3288     {
3289       n_occurrences++;
3290       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3291     }
3292
3293   /* If X and FROM are the same register but different modes, they will
3294      not have been seen as equal above.  However, flow.c will make a
3295      LOG_LINKS entry for that case.  If we do nothing, we will try to
3296      rerecognize our original insn and, when it succeeds, we will
3297      delete the feeding insn, which is incorrect.
3298
3299      So force this insn not to match in this (rare) case.  */
3300   if (! in_dest && code == REG && GET_CODE (from) == REG
3301       && REGNO (x) == REGNO (from))
3302     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3303
3304   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3305      of which may contain things that can be combined.  */
3306   if (code != MEM && code != LO_SUM && GET_RTX_CLASS (code) == 'o')
3307     return x;
3308
3309   /* It is possible to have a subexpression appear twice in the insn.
3310      Suppose that FROM is a register that appears within TO.
3311      Then, after that subexpression has been scanned once by `subst',
3312      the second time it is scanned, TO may be found.  If we were
3313      to scan TO here, we would find FROM within it and create a
3314      self-referent rtl structure which is completely wrong.  */
3315   if (COMBINE_RTX_EQUAL_P (x, to))
3316     return to;
3317
3318   /* Parallel asm_operands need special attention because all of the
3319      inputs are shared across the arms.  Furthermore, unsharing the
3320      rtl results in recognition failures.  Failure to handle this case
3321      specially can result in circular rtl.
3322
3323      Solve this by doing a normal pass across the first entry of the
3324      parallel, and only processing the SET_DESTs of the subsequent
3325      entries.  Ug.  */
3326
3327   if (code == PARALLEL
3328       && GET_CODE (XVECEXP (x, 0, 0)) == SET
3329       && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
3330     {
3331       new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
3332
3333       /* If this substitution failed, this whole thing fails.  */
3334       if (GET_CODE (new) == CLOBBER
3335           && XEXP (new, 0) == const0_rtx)
3336         return new;
3337
3338       SUBST (XVECEXP (x, 0, 0), new);
3339
3340       for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
3341         {
3342           rtx dest = SET_DEST (XVECEXP (x, 0, i));
3343
3344           if (GET_CODE (dest) != REG
3345               && GET_CODE (dest) != CC0
3346               && GET_CODE (dest) != PC)
3347             {
3348               new = subst (dest, from, to, 0, unique_copy);
3349
3350               /* If this substitution failed, this whole thing fails.  */
3351               if (GET_CODE (new) == CLOBBER
3352                   && XEXP (new, 0) == const0_rtx)
3353                 return new;
3354
3355               SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
3356             }
3357         }
3358     }
3359   else
3360     {
3361       len = GET_RTX_LENGTH (code);
3362       fmt = GET_RTX_FORMAT (code);
3363
3364       /* We don't need to process a SET_DEST that is a register, CC0,
3365          or PC, so set up to skip this common case.  All other cases
3366          where we want to suppress replacing something inside a
3367          SET_SRC are handled via the IN_DEST operand.  */
3368       if (code == SET
3369           && (GET_CODE (SET_DEST (x)) == REG
3370               || GET_CODE (SET_DEST (x)) == CC0
3371               || GET_CODE (SET_DEST (x)) == PC))
3372         fmt = "ie";
3373
3374       /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3375          constant.  */
3376       if (fmt[0] == 'e')
3377         op0_mode = GET_MODE (XEXP (x, 0));
3378
3379       for (i = 0; i < len; i++)
3380         {
3381           if (fmt[i] == 'E')
3382             {
3383               int j;
3384               for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3385                 {
3386                   if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3387                     {
3388                       new = (unique_copy && n_occurrences
3389                              ? copy_rtx (to) : to);
3390                       n_occurrences++;
3391                     }
3392                   else
3393                     {
3394                       new = subst (XVECEXP (x, i, j), from, to, 0,
3395                                    unique_copy);
3396
3397                       /* If this substitution failed, this whole thing
3398                          fails.  */
3399                       if (GET_CODE (new) == CLOBBER
3400                           && XEXP (new, 0) == const0_rtx)
3401                         return new;
3402                     }
3403
3404                   SUBST (XVECEXP (x, i, j), new);
3405                 }
3406             }
3407           else if (fmt[i] == 'e')
3408             {
3409               /* If this is a register being set, ignore it.  */
3410               new = XEXP (x, i);
3411               if (in_dest
3412                   && (code == SUBREG || code == STRICT_LOW_PART
3413                       || code == ZERO_EXTRACT)
3414                   && i == 0
3415                   && GET_CODE (new) == REG)
3416                 ;
3417
3418               else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3419                 {
3420                   /* In general, don't install a subreg involving two
3421                      modes not tieable.  It can worsen register
3422                      allocation, and can even make invalid reload
3423                      insns, since the reg inside may need to be copied
3424                      from in the outside mode, and that may be invalid
3425                      if it is an fp reg copied in integer mode.
3426
3427                      We allow two exceptions to this: It is valid if
3428                      it is inside another SUBREG and the mode of that
3429                      SUBREG and the mode of the inside of TO is
3430                      tieable and it is valid if X is a SET that copies
3431                      FROM to CC0.  */
3432
3433                   if (GET_CODE (to) == SUBREG
3434                       && ! MODES_TIEABLE_P (GET_MODE (to),
3435                                             GET_MODE (SUBREG_REG (to)))
3436                       && ! (code == SUBREG
3437                             && MODES_TIEABLE_P (GET_MODE (x),
3438                                                 GET_MODE (SUBREG_REG (to))))
3439 #ifdef HAVE_cc0
3440                       && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3441 #endif
3442                       )
3443                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3444
3445 #ifdef CANNOT_CHANGE_MODE_CLASS
3446                   if (code == SUBREG
3447                       && GET_CODE (to) == REG
3448                       && REGNO (to) < FIRST_PSEUDO_REGISTER
3449                       && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
3450                                                    GET_MODE (to),
3451                                                    GET_MODE (x)))
3452                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3453 #endif
3454
3455                   new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3456                   n_occurrences++;
3457                 }
3458               else
3459                 /* If we are in a SET_DEST, suppress most cases unless we
3460                    have gone inside a MEM, in which case we want to
3461                    simplify the address.  We assume here that things that
3462                    are actually part of the destination have their inner
3463                    parts in the first expression.  This is true for SUBREG,
3464                    STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3465                    things aside from REG and MEM that should appear in a
3466                    SET_DEST.  */
3467                 new = subst (XEXP (x, i), from, to,
3468                              (((in_dest
3469                                 && (code == SUBREG || code == STRICT_LOW_PART
3470                                     || code == ZERO_EXTRACT))
3471                                || code == SET)
3472                               && i == 0), unique_copy);
3473
3474               /* If we found that we will have to reject this combination,
3475                  indicate that by returning the CLOBBER ourselves, rather than
3476                  an expression containing it.  This will speed things up as
3477                  well as prevent accidents where two CLOBBERs are considered
3478                  to be equal, thus producing an incorrect simplification.  */
3479
3480               if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3481                 return new;
3482
3483               if (GET_CODE (x) == SUBREG
3484                   && (GET_CODE (new) == CONST_INT
3485                       || GET_CODE (new) == CONST_DOUBLE))
3486                 {
3487                   enum machine_mode mode = GET_MODE (x);
3488
3489                   x = simplify_subreg (GET_MODE (x), new,
3490                                        GET_MODE (SUBREG_REG (x)),
3491                                        SUBREG_BYTE (x));
3492                   if (! x)
3493                     x = gen_rtx_CLOBBER (mode, const0_rtx);
3494                 }
3495               else if (GET_CODE (new) == CONST_INT
3496                        && GET_CODE (x) == ZERO_EXTEND)
3497                 {
3498                   x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
3499                                                 new, GET_MODE (XEXP (x, 0)));
3500                   if (! x)
3501                     abort ();
3502                 }
3503               else
3504                 SUBST (XEXP (x, i), new);
3505             }
3506         }
3507     }
3508
3509   /* Try to simplify X.  If the simplification changed the code, it is likely
3510      that further simplification will help, so loop, but limit the number
3511      of repetitions that will be performed.  */
3512
3513   for (i = 0; i < 4; i++)
3514     {
3515       /* If X is sufficiently simple, don't bother trying to do anything
3516          with it.  */
3517       if (code != CONST_INT && code != REG && code != CLOBBER)
3518         x = combine_simplify_rtx (x, op0_mode, i == 3, in_dest);
3519
3520       if (GET_CODE (x) == code)
3521         break;
3522
3523       code = GET_CODE (x);
3524
3525       /* We no longer know the original mode of operand 0 since we
3526          have changed the form of X)  */
3527       op0_mode = VOIDmode;
3528     }
3529
3530   return x;
3531 }
3532 \f
3533 /* Simplify X, a piece of RTL.  We just operate on the expression at the
3534    outer level; call `subst' to simplify recursively.  Return the new
3535    expression.
3536
3537    OP0_MODE is the original mode of XEXP (x, 0); LAST is nonzero if this
3538    will be the iteration even if an expression with a code different from
3539    X is returned; IN_DEST is nonzero if we are inside a SET_DEST.  */
3540
3541 static rtx
3542 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int last,
3543                       int in_dest)
3544 {
3545   enum rtx_code code = GET_CODE (x);
3546   enum machine_mode mode = GET_MODE (x);
3547   rtx temp;
3548   rtx reversed;
3549   int i;
3550
3551   /* If this is a commutative operation, put a constant last and a complex
3552      expression first.  We don't need to do this for comparisons here.  */
3553   if (GET_RTX_CLASS (code) == 'c'
3554       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
3555     {
3556       temp = XEXP (x, 0);
3557       SUBST (XEXP (x, 0), XEXP (x, 1));
3558       SUBST (XEXP (x, 1), temp);
3559     }
3560
3561   /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3562      sign extension of a PLUS with a constant, reverse the order of the sign
3563      extension and the addition. Note that this not the same as the original
3564      code, but overflow is undefined for signed values.  Also note that the
3565      PLUS will have been partially moved "inside" the sign-extension, so that
3566      the first operand of X will really look like:
3567          (ashiftrt (plus (ashift A C4) C5) C4).
3568      We convert this to
3569          (plus (ashiftrt (ashift A C4) C2) C4)
3570      and replace the first operand of X with that expression.  Later parts
3571      of this function may simplify the expression further.
3572
3573      For example, if we start with (mult (sign_extend (plus A C1)) C2),
3574      we swap the SIGN_EXTEND and PLUS.  Later code will apply the
3575      distributive law to produce (plus (mult (sign_extend X) C1) C3).
3576
3577      We do this to simplify address expressions.  */
3578
3579   if ((code == PLUS || code == MINUS || code == MULT)
3580       && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3581       && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3582       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3583       && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3584       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3585       && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3586       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3587       && (temp = simplify_binary_operation (ASHIFTRT, mode,
3588                                             XEXP (XEXP (XEXP (x, 0), 0), 1),
3589                                             XEXP (XEXP (x, 0), 1))) != 0)
3590     {
3591       rtx new
3592         = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3593                                 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3594                                 INTVAL (XEXP (XEXP (x, 0), 1)));
3595
3596       new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3597                                   INTVAL (XEXP (XEXP (x, 0), 1)));
3598
3599       SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
3600     }
3601
3602   /* If this is a simple operation applied to an IF_THEN_ELSE, try
3603      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
3604      things.  Check for cases where both arms are testing the same
3605      condition.
3606
3607      Don't do anything if all operands are very simple.  */
3608
3609   if (((GET_RTX_CLASS (code) == '2' || GET_RTX_CLASS (code) == 'c'
3610         || GET_RTX_CLASS (code) == '<')
3611        && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3612             && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3613                   && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3614                       == 'o')))
3615            || (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o'
3616                && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3617                      && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 1))))
3618                          == 'o')))))
3619       || (GET_RTX_CLASS (code) == '1'
3620           && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3621                && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3622                      && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3623                          == 'o'))))))
3624     {
3625       rtx cond, true_rtx, false_rtx;
3626
3627       cond = if_then_else_cond (x, &true_rtx, &false_rtx);
3628       if (cond != 0
3629           /* If everything is a comparison, what we have is highly unlikely
3630              to be simpler, so don't use it.  */
3631           && ! (GET_RTX_CLASS (code) == '<'
3632                 && (GET_RTX_CLASS (GET_CODE (true_rtx)) == '<'
3633                     || GET_RTX_CLASS (GET_CODE (false_rtx)) == '<')))
3634         {
3635           rtx cop1 = const0_rtx;
3636           enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3637
3638           if (cond_code == NE && GET_RTX_CLASS (GET_CODE (cond)) == '<')
3639             return x;
3640
3641           /* Simplify the alternative arms; this may collapse the true and
3642              false arms to store-flag values.  Be careful to use copy_rtx
3643              here since true_rtx or false_rtx might share RTL with x as a
3644              result of the if_then_else_cond call above.  */
3645           true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
3646           false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
3647
3648           /* If true_rtx and false_rtx are not general_operands, an if_then_else
3649              is unlikely to be simpler.  */
3650           if (general_operand (true_rtx, VOIDmode)
3651               && general_operand (false_rtx, VOIDmode))
3652             {
3653               enum rtx_code reversed;
3654
3655               /* Restarting if we generate a store-flag expression will cause
3656                  us to loop.  Just drop through in this case.  */
3657
3658               /* If the result values are STORE_FLAG_VALUE and zero, we can
3659                  just make the comparison operation.  */
3660               if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
3661                 x = gen_binary (cond_code, mode, cond, cop1);
3662               else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
3663                        && ((reversed = reversed_comparison_code_parts
3664                                         (cond_code, cond, cop1, NULL))
3665                            != UNKNOWN))
3666                 x = gen_binary (reversed, mode, cond, cop1);
3667
3668               /* Likewise, we can make the negate of a comparison operation
3669                  if the result values are - STORE_FLAG_VALUE and zero.  */
3670               else if (GET_CODE (true_rtx) == CONST_INT
3671                        && INTVAL (true_rtx) == - STORE_FLAG_VALUE
3672                        && false_rtx == const0_rtx)
3673                 x = simplify_gen_unary (NEG, mode,
3674                                         gen_binary (cond_code, mode, cond,
3675                                                     cop1),
3676                                         mode);
3677               else if (GET_CODE (false_rtx) == CONST_INT
3678                        && INTVAL (false_rtx) == - STORE_FLAG_VALUE
3679                        && true_rtx == const0_rtx
3680                        && ((reversed = reversed_comparison_code_parts
3681                                         (cond_code, cond, cop1, NULL))
3682                            != UNKNOWN))
3683                 x = simplify_gen_unary (NEG, mode,
3684                                         gen_binary (reversed, mode,
3685                                                     cond, cop1),
3686                                         mode);
3687               else
3688                 return gen_rtx_IF_THEN_ELSE (mode,
3689                                              gen_binary (cond_code, VOIDmode,
3690                                                          cond, cop1),
3691                                              true_rtx, false_rtx);
3692
3693               code = GET_CODE (x);
3694               op0_mode = VOIDmode;
3695             }
3696         }
3697     }
3698
3699   /* Try to fold this expression in case we have constants that weren't
3700      present before.  */
3701   temp = 0;
3702   switch (GET_RTX_CLASS (code))
3703     {
3704     case '1':
3705       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3706       break;
3707     case '<':
3708       {
3709         enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
3710         if (cmp_mode == VOIDmode)
3711           {
3712             cmp_mode = GET_MODE (XEXP (x, 1));
3713             if (cmp_mode == VOIDmode)
3714               cmp_mode = op0_mode;
3715           }
3716         temp = simplify_relational_operation (code, cmp_mode,
3717                                               XEXP (x, 0), XEXP (x, 1));
3718       }
3719 #ifdef FLOAT_STORE_FLAG_VALUE
3720       if (temp != 0 && GET_MODE_CLASS (mode) == MODE_FLOAT)
3721         {
3722           if (temp == const0_rtx)
3723             temp = CONST0_RTX (mode);
3724           else
3725             temp = CONST_DOUBLE_FROM_REAL_VALUE (FLOAT_STORE_FLAG_VALUE (mode),
3726                                                  mode);
3727         }
3728 #endif
3729       break;
3730     case 'c':
3731     case '2':
3732       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3733       break;
3734     case 'b':
3735     case '3':
3736       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3737                                          XEXP (x, 1), XEXP (x, 2));
3738       break;
3739     }
3740
3741   if (temp)
3742     {
3743       x = temp;
3744       code = GET_CODE (temp);
3745       op0_mode = VOIDmode;
3746       mode = GET_MODE (temp);
3747     }
3748
3749   /* First see if we can apply the inverse distributive law.  */
3750   if (code == PLUS || code == MINUS
3751       || code == AND || code == IOR || code == XOR)
3752     {
3753       x = apply_distributive_law (x);
3754       code = GET_CODE (x);
3755       op0_mode = VOIDmode;
3756     }
3757
3758   /* If CODE is an associative operation not otherwise handled, see if we
3759      can associate some operands.  This can win if they are constants or
3760      if they are logically related (i.e. (a & b) & a).  */
3761   if ((code == PLUS || code == MINUS || code == MULT || code == DIV
3762        || code == AND || code == IOR || code == XOR
3763        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3764       && ((INTEGRAL_MODE_P (mode) && code != DIV)
3765           || (flag_unsafe_math_optimizations && FLOAT_MODE_P (mode))))
3766     {
3767       if (GET_CODE (XEXP (x, 0)) == code)
3768         {
3769           rtx other = XEXP (XEXP (x, 0), 0);
3770           rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3771           rtx inner_op1 = XEXP (x, 1);
3772           rtx inner;
3773
3774           /* Make sure we pass the constant operand if any as the second
3775              one if this is a commutative operation.  */
3776           if (CONSTANT_P (inner_op0) && GET_RTX_CLASS (code) == 'c')
3777             {
3778               rtx tem = inner_op0;
3779               inner_op0 = inner_op1;
3780               inner_op1 = tem;
3781             }
3782           inner = simplify_binary_operation (code == MINUS ? PLUS
3783                                              : code == DIV ? MULT
3784                                              : code,
3785                                              mode, inner_op0, inner_op1);
3786
3787           /* For commutative operations, try the other pair if that one
3788              didn't simplify.  */
3789           if (inner == 0 && GET_RTX_CLASS (code) == 'c')
3790             {
3791               other = XEXP (XEXP (x, 0), 1);
3792               inner = simplify_binary_operation (code, mode,
3793                                                  XEXP (XEXP (x, 0), 0),
3794                                                  XEXP (x, 1));
3795             }
3796
3797           if (inner)
3798             return gen_binary (code, mode, other, inner);
3799         }
3800     }
3801
3802   /* A little bit of algebraic simplification here.  */
3803   switch (code)
3804     {
3805     case MEM:
3806       /* Ensure that our address has any ASHIFTs converted to MULT in case
3807          address-recognizing predicates are called later.  */
3808       temp = make_compound_operation (XEXP (x, 0), MEM);
3809       SUBST (XEXP (x, 0), temp);
3810       break;
3811
3812     case SUBREG:
3813       if (op0_mode == VOIDmode)
3814         op0_mode = GET_MODE (SUBREG_REG (x));
3815
3816       /* simplify_subreg can't use gen_lowpart_for_combine.  */
3817       if (CONSTANT_P (SUBREG_REG (x))
3818           && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
3819              /* Don't call gen_lowpart_for_combine if the inner mode
3820                 is VOIDmode and we cannot simplify it, as SUBREG without
3821                 inner mode is invalid.  */
3822           && (GET_MODE (SUBREG_REG (x)) != VOIDmode
3823               || gen_lowpart_common (mode, SUBREG_REG (x))))
3824         return gen_lowpart_for_combine (mode, SUBREG_REG (x));
3825
3826       if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
3827         break;
3828       {
3829         rtx temp;
3830         temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
3831                                 SUBREG_BYTE (x));
3832         if (temp)
3833           return temp;
3834       }
3835
3836       /* Don't change the mode of the MEM if that would change the meaning
3837          of the address.  */
3838       if (GET_CODE (SUBREG_REG (x)) == MEM
3839           && (MEM_VOLATILE_P (SUBREG_REG (x))
3840               || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
3841         return gen_rtx_CLOBBER (mode, const0_rtx);
3842
3843       /* Note that we cannot do any narrowing for non-constants since
3844          we might have been counting on using the fact that some bits were
3845          zero.  We now do this in the SET.  */
3846
3847       break;
3848
3849     case NOT:
3850       /* (not (plus X -1)) can become (neg X).  */
3851       if (GET_CODE (XEXP (x, 0)) == PLUS
3852           && XEXP (XEXP (x, 0), 1) == constm1_rtx)
3853         return gen_rtx_NEG (mode, XEXP (XEXP (x, 0), 0));
3854
3855       /* Similarly, (not (neg X)) is (plus X -1).  */
3856       if (GET_CODE (XEXP (x, 0)) == NEG)
3857         return gen_rtx_PLUS (mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
3858
3859       /* (not (xor X C)) for C constant is (xor X D) with D = ~C.  */
3860       if (GET_CODE (XEXP (x, 0)) == XOR
3861           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3862           && (temp = simplify_unary_operation (NOT, mode,
3863                                                XEXP (XEXP (x, 0), 1),
3864                                                mode)) != 0)
3865         return gen_binary (XOR, mode, XEXP (XEXP (x, 0), 0), temp);
3866
3867       /* (not (ashift 1 X)) is (rotate ~1 X).  We used to do this for operands
3868          other than 1, but that is not valid.  We could do a similar
3869          simplification for (not (lshiftrt C X)) where C is just the sign bit,
3870          but this doesn't seem common enough to bother with.  */
3871       if (GET_CODE (XEXP (x, 0)) == ASHIFT
3872           && XEXP (XEXP (x, 0), 0) == const1_rtx)
3873         return gen_rtx_ROTATE (mode, simplify_gen_unary (NOT, mode,
3874                                                          const1_rtx, mode),
3875                                XEXP (XEXP (x, 0), 1));
3876
3877       if (GET_CODE (XEXP (x, 0)) == SUBREG
3878           && subreg_lowpart_p (XEXP (x, 0))
3879           && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
3880               < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
3881           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
3882           && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
3883         {
3884           enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
3885
3886           x = gen_rtx_ROTATE (inner_mode,
3887                               simplify_gen_unary (NOT, inner_mode, const1_rtx,
3888                                                   inner_mode),
3889                               XEXP (SUBREG_REG (XEXP (x, 0)), 1));
3890           return gen_lowpart_for_combine (mode, x);
3891         }
3892
3893       /* If STORE_FLAG_VALUE is -1, (not (comparison foo bar)) can be done by
3894          reversing the comparison code if valid.  */
3895       if (STORE_FLAG_VALUE == -1
3896           && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3897           && (reversed = reversed_comparison (x, mode, XEXP (XEXP (x, 0), 0),
3898                                               XEXP (XEXP (x, 0), 1))))
3899         return reversed;
3900
3901       /* (not (ashiftrt foo C)) where C is the number of bits in FOO minus 1
3902          is (ge foo (const_int 0)) if STORE_FLAG_VALUE is -1, so we can
3903          perform the above simplification.  */
3904
3905       if (STORE_FLAG_VALUE == -1
3906           && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3907           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3908           && INTVAL (XEXP (XEXP (x, 0), 1)) == GET_MODE_BITSIZE (mode) - 1)
3909         return gen_rtx_GE (mode, XEXP (XEXP (x, 0), 0), const0_rtx);
3910
3911       /* Apply De Morgan's laws to reduce number of patterns for machines
3912          with negating logical insns (and-not, nand, etc.).  If result has
3913          only one NOT, put it first, since that is how the patterns are
3914          coded.  */
3915
3916       if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
3917         {
3918           rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
3919           enum machine_mode op_mode;
3920
3921           op_mode = GET_MODE (in1);
3922           in1 = simplify_gen_unary (NOT, op_mode, in1, op_mode);
3923
3924           op_mode = GET_MODE (in2);
3925           if (op_mode == VOIDmode)
3926             op_mode = mode;
3927           in2 = simplify_gen_unary (NOT, op_mode, in2, op_mode);
3928
3929           if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
3930             {
3931               rtx tem = in2;
3932               in2 = in1; in1 = tem;
3933             }
3934
3935           return gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
3936                                  mode, in1, in2);
3937         }
3938       break;
3939
3940     case NEG:
3941       /* (neg (plus X 1)) can become (not X).  */
3942       if (GET_CODE (XEXP (x, 0)) == PLUS
3943           && XEXP (XEXP (x, 0), 1) == const1_rtx)
3944         return gen_rtx_NOT (mode, XEXP (XEXP (x, 0), 0));
3945
3946       /* Similarly, (neg (not X)) is (plus X 1).  */
3947       if (GET_CODE (XEXP (x, 0)) == NOT)
3948         return plus_constant (XEXP (XEXP (x, 0), 0), 1);
3949
3950       /* (neg (minus X Y)) can become (minus Y X).  This transformation
3951          isn't safe for modes with signed zeros, since if X and Y are
3952          both +0, (minus Y X) is the same as (minus X Y).  If the rounding
3953          mode is towards +infinity (or -infinity) then the two expressions
3954          will be rounded differently.  */
3955       if (GET_CODE (XEXP (x, 0)) == MINUS
3956           && !HONOR_SIGNED_ZEROS (mode)
3957           && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
3958         return gen_binary (MINUS, mode, XEXP (XEXP (x, 0), 1),
3959                            XEXP (XEXP (x, 0), 0));
3960
3961       /* (neg (plus A B)) is canonicalized to (minus (neg A) B).  */
3962       if (GET_CODE (XEXP (x, 0)) == PLUS
3963           && !HONOR_SIGNED_ZEROS (mode)
3964           && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
3965         {
3966           temp = simplify_gen_unary (NEG, mode, XEXP (XEXP (x, 0), 0), mode);
3967           temp = combine_simplify_rtx (temp, mode, last, in_dest);
3968           return gen_binary (MINUS, mode, temp, XEXP (XEXP (x, 0), 1));
3969         }
3970
3971       /* (neg (mult A B)) becomes (mult (neg A) B).
3972          This works even for floating-point values.  */
3973       if (GET_CODE (XEXP (x, 0)) == MULT)
3974         {
3975           temp = simplify_gen_unary (NEG, mode, XEXP (XEXP (x, 0), 0), mode);
3976           return gen_binary (MULT, mode, temp, XEXP (XEXP (x, 0), 1));
3977         }
3978
3979       /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1.  */
3980       if (GET_CODE (XEXP (x, 0)) == XOR && XEXP (XEXP (x, 0), 1) == const1_rtx
3981           && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
3982         return gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
3983
3984       /* NEG commutes with ASHIFT since it is multiplication.  Only do this
3985          if we can then eliminate the NEG (e.g.,
3986          if the operand is a constant).  */
3987
3988       if (GET_CODE (XEXP (x, 0)) == ASHIFT)
3989         {
3990           temp = simplify_unary_operation (NEG, mode,
3991                                            XEXP (XEXP (x, 0), 0), mode);
3992           if (temp)
3993             return gen_binary (ASHIFT, mode, temp, XEXP (XEXP (x, 0), 1));
3994         }
3995
3996       temp = expand_compound_operation (XEXP (x, 0));
3997
3998       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
3999          replaced by (lshiftrt X C).  This will convert
4000          (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
4001
4002       if (GET_CODE (temp) == ASHIFTRT
4003           && GET_CODE (XEXP (temp, 1)) == CONST_INT
4004           && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
4005         return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
4006                                      INTVAL (XEXP (temp, 1)));
4007
4008       /* If X has only a single bit that might be nonzero, say, bit I, convert
4009          (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
4010          MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
4011          (sign_extract X 1 Y).  But only do this if TEMP isn't a register
4012          or a SUBREG of one since we'd be making the expression more
4013          complex if it was just a register.  */
4014
4015       if (GET_CODE (temp) != REG
4016           && ! (GET_CODE (temp) == SUBREG
4017                 && GET_CODE (SUBREG_REG (temp)) == REG)
4018           && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
4019         {
4020           rtx temp1 = simplify_shift_const
4021             (NULL_RTX, ASHIFTRT, mode,
4022              simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
4023                                    GET_MODE_BITSIZE (mode) - 1 - i),
4024              GET_MODE_BITSIZE (mode) - 1 - i);
4025
4026           /* If all we did was surround TEMP with the two shifts, we
4027              haven't improved anything, so don't use it.  Otherwise,
4028              we are better off with TEMP1.  */
4029           if (GET_CODE (temp1) != ASHIFTRT
4030               || GET_CODE (XEXP (temp1, 0)) != ASHIFT
4031               || XEXP (XEXP (temp1, 0), 0) != temp)
4032             return temp1;
4033         }
4034       break;
4035
4036     case TRUNCATE:
4037       /* We can't handle truncation to a partial integer mode here
4038          because we don't know the real bitsize of the partial
4039          integer mode.  */
4040       if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
4041         break;
4042
4043       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4044           && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4045                                     GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
4046         SUBST (XEXP (x, 0),
4047                force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
4048                               GET_MODE_MASK (mode), NULL_RTX, 0));
4049
4050       /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI.  */
4051       if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4052            || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4053           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4054         return XEXP (XEXP (x, 0), 0);
4055
4056       /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
4057          (OP:SI foo:SI) if OP is NEG or ABS.  */
4058       if ((GET_CODE (XEXP (x, 0)) == ABS
4059            || GET_CODE (XEXP (x, 0)) == NEG)
4060           && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
4061               || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
4062           && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4063         return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4064                                    XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
4065
4066       /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
4067          (truncate:SI x).  */
4068       if (GET_CODE (XEXP (x, 0)) == SUBREG
4069           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
4070           && subreg_lowpart_p (XEXP (x, 0)))
4071         return SUBREG_REG (XEXP (x, 0));
4072
4073       /* If we know that the value is already truncated, we can
4074          replace the TRUNCATE with a SUBREG if TRULY_NOOP_TRUNCATION
4075          is nonzero for the corresponding modes.  But don't do this
4076          for an (LSHIFTRT (MULT ...)) since this will cause problems
4077          with the umulXi3_highpart patterns.  */
4078       if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4079                                  GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
4080           && num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4081              >= (unsigned int) (GET_MODE_BITSIZE (mode) + 1)
4082           && ! (GET_CODE (XEXP (x, 0)) == LSHIFTRT
4083                 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT))
4084         return gen_lowpart_for_combine (mode, XEXP (x, 0));
4085
4086       /* A truncate of a comparison can be replaced with a subreg if
4087          STORE_FLAG_VALUE permits.  This is like the previous test,
4088          but it works even if the comparison is done in a mode larger
4089          than HOST_BITS_PER_WIDE_INT.  */
4090       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4091           && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4092           && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0)
4093         return gen_lowpart_for_combine (mode, XEXP (x, 0));
4094
4095       /* Similarly, a truncate of a register whose value is a
4096          comparison can be replaced with a subreg if STORE_FLAG_VALUE
4097          permits.  */
4098       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4099           && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4100           && (temp = get_last_value (XEXP (x, 0)))
4101           && GET_RTX_CLASS (GET_CODE (temp)) == '<')
4102         return gen_lowpart_for_combine (mode, XEXP (x, 0));
4103
4104       break;
4105
4106     case FLOAT_TRUNCATE:
4107       /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF.  */
4108       if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4109           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4110         return XEXP (XEXP (x, 0), 0);
4111
4112       /* (float_truncate:SF (float_truncate:DF foo:XF))
4113          = (float_truncate:SF foo:XF).
4114          This may eliminate double rounding, so it is unsafe.
4115
4116          (float_truncate:SF (float_extend:XF foo:DF))
4117          = (float_truncate:SF foo:DF).
4118
4119          (float_truncate:DF (float_extend:XF foo:SF))
4120          = (float_extend:SF foo:DF).  */
4121       if ((GET_CODE (XEXP (x, 0)) == FLOAT_TRUNCATE
4122            && flag_unsafe_math_optimizations)
4123           || GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND)
4124         return simplify_gen_unary (GET_MODE_SIZE (GET_MODE (XEXP (XEXP (x, 0),
4125                                                             0)))
4126                                    > GET_MODE_SIZE (mode)
4127                                    ? FLOAT_TRUNCATE : FLOAT_EXTEND,
4128                                    mode,
4129                                    XEXP (XEXP (x, 0), 0), mode);
4130
4131       /*  (float_truncate (float x)) is (float x)  */
4132       if (GET_CODE (XEXP (x, 0)) == FLOAT
4133           && (flag_unsafe_math_optimizations
4134               || ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4135                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4136                       - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4137                                              GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4138         return simplify_gen_unary (FLOAT, mode,
4139                                    XEXP (XEXP (x, 0), 0),
4140                                    GET_MODE (XEXP (XEXP (x, 0), 0)));
4141
4142       /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
4143          (OP:SF foo:SF) if OP is NEG or ABS.  */
4144       if ((GET_CODE (XEXP (x, 0)) == ABS
4145            || GET_CODE (XEXP (x, 0)) == NEG)
4146           && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
4147           && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4148         return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4149                                    XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
4150
4151       /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
4152          is (float_truncate:SF x).  */
4153       if (GET_CODE (XEXP (x, 0)) == SUBREG
4154           && subreg_lowpart_p (XEXP (x, 0))
4155           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
4156         return SUBREG_REG (XEXP (x, 0));
4157       break;
4158     case FLOAT_EXTEND:
4159       /*  (float_extend (float_extend x)) is (float_extend x)
4160
4161           (float_extend (float x)) is (float x) assuming that double
4162           rounding can't happen.
4163           */
4164       if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4165           || (GET_CODE (XEXP (x, 0)) == FLOAT
4166               && ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4167                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4168                       - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4169                                              GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4170         return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4171                                    XEXP (XEXP (x, 0), 0),
4172                                    GET_MODE (XEXP (XEXP (x, 0), 0)));
4173
4174       break;
4175 #ifdef HAVE_cc0
4176     case COMPARE:
4177       /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4178          using cc0, in which case we want to leave it as a COMPARE
4179          so we can distinguish it from a register-register-copy.  */
4180       if (XEXP (x, 1) == const0_rtx)
4181         return XEXP (x, 0);
4182
4183       /* x - 0 is the same as x unless x's mode has signed zeros and
4184          allows rounding towards -infinity.  Under those conditions,
4185          0 - 0 is -0.  */
4186       if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4187             && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4188           && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4189         return XEXP (x, 0);
4190       break;
4191 #endif
4192
4193     case CONST:
4194       /* (const (const X)) can become (const X).  Do it this way rather than
4195          returning the inner CONST since CONST can be shared with a
4196          REG_EQUAL note.  */
4197       if (GET_CODE (XEXP (x, 0)) == CONST)
4198         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4199       break;
4200
4201 #ifdef HAVE_lo_sum
4202     case LO_SUM:
4203       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
4204          can add in an offset.  find_split_point will split this address up
4205          again if it doesn't match.  */
4206       if (GET_CODE (XEXP (x, 0)) == HIGH
4207           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4208         return XEXP (x, 1);
4209       break;
4210 #endif
4211
4212     case PLUS:
4213       /* Canonicalize (plus (mult (neg B) C) A) to (minus A (mult B C)).
4214        */
4215       if (GET_CODE (XEXP (x, 0)) == MULT
4216           && GET_CODE (XEXP (XEXP (x, 0), 0)) == NEG)
4217         {
4218           rtx in1, in2;
4219
4220           in1 = XEXP (XEXP (XEXP (x, 0), 0), 0);
4221           in2 = XEXP (XEXP (x, 0), 1);
4222           return gen_binary (MINUS, mode, XEXP (x, 1),
4223                              gen_binary (MULT, mode, in1, in2));
4224         }
4225
4226       /* If we have (plus (plus (A const) B)), associate it so that CONST is
4227          outermost.  That's because that's the way indexed addresses are
4228          supposed to appear.  This code used to check many more cases, but
4229          they are now checked elsewhere.  */
4230       if (GET_CODE (XEXP (x, 0)) == PLUS
4231           && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
4232         return gen_binary (PLUS, mode,
4233                            gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
4234                                        XEXP (x, 1)),
4235                            XEXP (XEXP (x, 0), 1));
4236
4237       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4238          when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4239          bit-field and can be replaced by either a sign_extend or a
4240          sign_extract.  The `and' may be a zero_extend and the two
4241          <c>, -<c> constants may be reversed.  */
4242       if (GET_CODE (XEXP (x, 0)) == XOR
4243           && GET_CODE (XEXP (x, 1)) == CONST_INT
4244           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4245           && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4246           && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4247               || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4248           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4249           && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4250                && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4251                && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4252                    == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4253               || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4254                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4255                       == (unsigned int) i + 1))))
4256         return simplify_shift_const
4257           (NULL_RTX, ASHIFTRT, mode,
4258            simplify_shift_const (NULL_RTX, ASHIFT, mode,
4259                                  XEXP (XEXP (XEXP (x, 0), 0), 0),
4260                                  GET_MODE_BITSIZE (mode) - (i + 1)),
4261            GET_MODE_BITSIZE (mode) - (i + 1));
4262
4263       /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
4264          C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
4265          is 1.  This produces better code than the alternative immediately
4266          below.  */
4267       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4268           && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
4269               || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx))
4270           && (reversed = reversed_comparison (XEXP (x, 0), mode,
4271                                               XEXP (XEXP (x, 0), 0),
4272                                               XEXP (XEXP (x, 0), 1))))
4273         return
4274           simplify_gen_unary (NEG, mode, reversed, mode);
4275
4276       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4277          can become (ashiftrt (ashift (xor x 1) C) C) where C is
4278          the bitsize of the mode - 1.  This allows simplification of
4279          "a = (b & 8) == 0;"  */
4280       if (XEXP (x, 1) == constm1_rtx
4281           && GET_CODE (XEXP (x, 0)) != REG
4282           && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4283                 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
4284           && nonzero_bits (XEXP (x, 0), mode) == 1)
4285         return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4286            simplify_shift_const (NULL_RTX, ASHIFT, mode,
4287                                  gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4288                                  GET_MODE_BITSIZE (mode) - 1),
4289            GET_MODE_BITSIZE (mode) - 1);
4290
4291       /* If we are adding two things that have no bits in common, convert
4292          the addition into an IOR.  This will often be further simplified,
4293          for example in cases like ((a & 1) + (a & 2)), which can
4294          become a & 3.  */
4295
4296       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4297           && (nonzero_bits (XEXP (x, 0), mode)
4298               & nonzero_bits (XEXP (x, 1), mode)) == 0)
4299         {
4300           /* Try to simplify the expression further.  */
4301           rtx tor = gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4302           temp = combine_simplify_rtx (tor, mode, last, in_dest);
4303
4304           /* If we could, great.  If not, do not go ahead with the IOR
4305              replacement, since PLUS appears in many special purpose
4306              address arithmetic instructions.  */
4307           if (GET_CODE (temp) != CLOBBER && temp != tor)
4308             return temp;
4309         }
4310       break;
4311
4312     case MINUS:
4313       /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
4314          by reversing the comparison code if valid.  */
4315       if (STORE_FLAG_VALUE == 1
4316           && XEXP (x, 0) == const1_rtx
4317           && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<'
4318           && (reversed = reversed_comparison (XEXP (x, 1), mode,
4319                                               XEXP (XEXP (x, 1), 0),
4320                                               XEXP (XEXP (x, 1), 1))))
4321         return reversed;
4322
4323       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4324          (and <foo> (const_int pow2-1))  */
4325       if (GET_CODE (XEXP (x, 1)) == AND
4326           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4327           && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4328           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4329         return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4330                                        -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4331
4332       /* Canonicalize (minus A (mult (neg B) C)) to (plus (mult B C) A).
4333        */
4334       if (GET_CODE (XEXP (x, 1)) == MULT
4335           && GET_CODE (XEXP (XEXP (x, 1), 0)) == NEG)
4336         {
4337           rtx in1, in2;
4338
4339           in1 = XEXP (XEXP (XEXP (x, 1), 0), 0);
4340           in2 = XEXP (XEXP (x, 1), 1);
4341           return gen_binary (PLUS, mode, gen_binary (MULT, mode, in1, in2),
4342                              XEXP (x, 0));
4343         }
4344
4345       /* Canonicalize (minus (neg A) (mult B C)) to
4346          (minus (mult (neg B) C) A).  */
4347       if (GET_CODE (XEXP (x, 1)) == MULT
4348           && GET_CODE (XEXP (x, 0)) == NEG)
4349         {
4350           rtx in1, in2;
4351
4352           in1 = simplify_gen_unary (NEG, mode, XEXP (XEXP (x, 1), 0), mode);
4353           in2 = XEXP (XEXP (x, 1), 1);
4354           return gen_binary (MINUS, mode, gen_binary (MULT, mode, in1, in2),
4355                              XEXP (XEXP (x, 0), 0));
4356         }
4357
4358       /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
4359          integers.  */
4360       if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
4361         return gen_binary (MINUS, mode,
4362                            gen_binary (MINUS, mode, XEXP (x, 0),
4363                                        XEXP (XEXP (x, 1), 0)),
4364                            XEXP (XEXP (x, 1), 1));
4365       break;
4366
4367     case MULT:
4368       /* If we have (mult (plus A B) C), apply the distributive law and then
4369          the inverse distributive law to see if things simplify.  This
4370          occurs mostly in addresses, often when unrolling loops.  */
4371
4372       if (GET_CODE (XEXP (x, 0)) == PLUS)
4373         {
4374           x = apply_distributive_law
4375             (gen_binary (PLUS, mode,
4376                          gen_binary (MULT, mode,
4377                                      XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
4378                          gen_binary (MULT, mode,
4379                                      XEXP (XEXP (x, 0), 1),
4380                                      copy_rtx (XEXP (x, 1)))));
4381
4382           if (GET_CODE (x) != MULT)
4383             return x;
4384         }
4385       /* Try simplify a*(b/c) as (a*b)/c.  */
4386       if (FLOAT_MODE_P (mode) && flag_unsafe_math_optimizations
4387           && GET_CODE (XEXP (x, 0)) == DIV)
4388         {
4389           rtx tem = simplify_binary_operation (MULT, mode,
4390                                                XEXP (XEXP (x, 0), 0),
4391                                                XEXP (x, 1));
4392           if (tem)
4393             return gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
4394         }
4395       break;
4396
4397     case UDIV:
4398       /* If this is a divide by a power of two, treat it as a shift if
4399          its first operand is a shift.  */
4400       if (GET_CODE (XEXP (x, 1)) == CONST_INT
4401           && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4402           && (GET_CODE (XEXP (x, 0)) == ASHIFT
4403               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4404               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4405               || GET_CODE (XEXP (x, 0)) == ROTATE
4406               || GET_CODE (XEXP (x, 0)) == ROTATERT))
4407         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4408       break;
4409
4410     case EQ:  case NE:
4411     case GT:  case GTU:  case GE:  case GEU:
4412     case LT:  case LTU:  case LE:  case LEU:
4413     case UNEQ:  case LTGT:
4414     case UNGT:  case UNGE:
4415     case UNLT:  case UNLE:
4416     case UNORDERED: case ORDERED:
4417       /* If the first operand is a condition code, we can't do anything
4418          with it.  */
4419       if (GET_CODE (XEXP (x, 0)) == COMPARE
4420           || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4421               && ! CC0_P (XEXP (x, 0))))
4422         {
4423           rtx op0 = XEXP (x, 0);
4424           rtx op1 = XEXP (x, 1);
4425           enum rtx_code new_code;
4426
4427           if (GET_CODE (op0) == COMPARE)
4428             op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4429
4430           /* Simplify our comparison, if possible.  */
4431           new_code = simplify_comparison (code, &op0, &op1);
4432
4433           /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4434              if only the low-order bit is possibly nonzero in X (such as when
4435              X is a ZERO_EXTRACT of one bit).  Similarly, we can convert EQ to
4436              (xor X 1) or (minus 1 X); we use the former.  Finally, if X is
4437              known to be either 0 or -1, NE becomes a NEG and EQ becomes
4438              (plus X 1).
4439
4440              Remove any ZERO_EXTRACT we made when thinking this was a
4441              comparison.  It may now be simpler to use, e.g., an AND.  If a
4442              ZERO_EXTRACT is indeed appropriate, it will be placed back by
4443              the call to make_compound_operation in the SET case.  */
4444
4445           if (STORE_FLAG_VALUE == 1
4446               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4447               && op1 == const0_rtx
4448               && mode == GET_MODE (op0)
4449               && nonzero_bits (op0, mode) == 1)
4450             return gen_lowpart_for_combine (mode,
4451                                             expand_compound_operation (op0));
4452
4453           else if (STORE_FLAG_VALUE == 1
4454                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4455                    && op1 == const0_rtx
4456                    && mode == GET_MODE (op0)
4457                    && (num_sign_bit_copies (op0, mode)
4458                        == GET_MODE_BITSIZE (mode)))
4459             {
4460               op0 = expand_compound_operation (op0);
4461               return simplify_gen_unary (NEG, mode,
4462                                          gen_lowpart_for_combine (mode, op0),
4463                                          mode);
4464             }
4465
4466           else if (STORE_FLAG_VALUE == 1
4467                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4468                    && op1 == const0_rtx
4469                    && mode == GET_MODE (op0)
4470                    && nonzero_bits (op0, mode) == 1)
4471             {
4472               op0 = expand_compound_operation (op0);
4473               return gen_binary (XOR, mode,
4474                                  gen_lowpart_for_combine (mode, op0),
4475                                  const1_rtx);
4476             }
4477
4478           else if (STORE_FLAG_VALUE == 1
4479                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4480                    && op1 == const0_rtx
4481                    && mode == GET_MODE (op0)
4482                    && (num_sign_bit_copies (op0, mode)
4483                        == GET_MODE_BITSIZE (mode)))
4484             {
4485               op0 = expand_compound_operation (op0);
4486               return plus_constant (gen_lowpart_for_combine (mode, op0), 1);
4487             }
4488
4489           /* If STORE_FLAG_VALUE is -1, we have cases similar to
4490              those above.  */
4491           if (STORE_FLAG_VALUE == -1
4492               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4493               && op1 == const0_rtx
4494               && (num_sign_bit_copies (op0, mode)
4495                   == GET_MODE_BITSIZE (mode)))
4496             return gen_lowpart_for_combine (mode,
4497                                             expand_compound_operation (op0));
4498
4499           else if (STORE_FLAG_VALUE == -1
4500                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4501                    && op1 == const0_rtx
4502                    && mode == GET_MODE (op0)
4503                    && nonzero_bits (op0, mode) == 1)
4504             {
4505               op0 = expand_compound_operation (op0);
4506               return simplify_gen_unary (NEG, mode,
4507                                          gen_lowpart_for_combine (mode, op0),
4508                                          mode);
4509             }
4510
4511           else if (STORE_FLAG_VALUE == -1
4512                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4513                    && op1 == const0_rtx
4514                    && mode == GET_MODE (op0)
4515                    && (num_sign_bit_copies (op0, mode)
4516                        == GET_MODE_BITSIZE (mode)))
4517             {
4518               op0 = expand_compound_operation (op0);
4519               return simplify_gen_unary (NOT, mode,
4520                                          gen_lowpart_for_combine (mode, op0),
4521                                          mode);
4522             }
4523
4524           /* If X is 0/1, (eq X 0) is X-1.  */
4525           else if (STORE_FLAG_VALUE == -1
4526                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4527                    && op1 == const0_rtx
4528                    && mode == GET_MODE (op0)
4529                    && nonzero_bits (op0, mode) == 1)
4530             {
4531               op0 = expand_compound_operation (op0);
4532               return plus_constant (gen_lowpart_for_combine (mode, op0), -1);
4533             }
4534
4535           /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4536              one bit that might be nonzero, we can convert (ne x 0) to
4537              (ashift x c) where C puts the bit in the sign bit.  Remove any
4538              AND with STORE_FLAG_VALUE when we are done, since we are only
4539              going to test the sign bit.  */
4540           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4541               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4542               && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4543                   == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
4544               && op1 == const0_rtx
4545               && mode == GET_MODE (op0)
4546               && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4547             {
4548               x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4549                                         expand_compound_operation (op0),
4550                                         GET_MODE_BITSIZE (mode) - 1 - i);
4551               if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4552                 return XEXP (x, 0);
4553               else
4554                 return x;
4555             }
4556
4557           /* If the code changed, return a whole new comparison.  */
4558           if (new_code != code)
4559             return gen_rtx_fmt_ee (new_code, mode, op0, op1);
4560
4561           /* Otherwise, keep this operation, but maybe change its operands.
4562              This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
4563           SUBST (XEXP (x, 0), op0);
4564           SUBST (XEXP (x, 1), op1);
4565         }
4566       break;
4567
4568     case IF_THEN_ELSE:
4569       return simplify_if_then_else (x);
4570
4571     case ZERO_EXTRACT:
4572     case SIGN_EXTRACT:
4573     case ZERO_EXTEND:
4574     case SIGN_EXTEND:
4575       /* If we are processing SET_DEST, we are done.  */
4576       if (in_dest)
4577         return x;
4578
4579       return expand_compound_operation (x);
4580
4581     case SET:
4582       return simplify_set (x);
4583
4584     case AND:
4585     case IOR:
4586     case XOR:
4587       return simplify_logical (x, last);
4588
4589     case ABS:
4590       /* (abs (neg <foo>)) -> (abs <foo>) */
4591       if (GET_CODE (XEXP (x, 0)) == NEG)
4592         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4593
4594       /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
4595          do nothing.  */
4596       if (GET_MODE (XEXP (x, 0)) == VOIDmode)
4597         break;
4598
4599       /* If operand is something known to be positive, ignore the ABS.  */
4600       if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4601           || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4602                <= HOST_BITS_PER_WIDE_INT)
4603               && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4604                    & ((HOST_WIDE_INT) 1
4605                       << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4606                   == 0)))
4607         return XEXP (x, 0);
4608
4609       /* If operand is known to be only -1 or 0, convert ABS to NEG.  */
4610       if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4611         return gen_rtx_NEG (mode, XEXP (x, 0));
4612
4613       break;
4614
4615     case FFS:
4616       /* (ffs (*_extend <X>)) = (ffs <X>) */
4617       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4618           || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4619         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4620       break;
4621
4622     case POPCOUNT:
4623     case PARITY:
4624       /* (pop* (zero_extend <X>)) = (pop* <X>) */
4625       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4626         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4627       break;
4628
4629     case FLOAT:
4630       /* (float (sign_extend <X>)) = (float <X>).  */
4631       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4632         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4633       break;
4634
4635     case ASHIFT:
4636     case LSHIFTRT:
4637     case ASHIFTRT:
4638     case ROTATE:
4639     case ROTATERT:
4640       /* If this is a shift by a constant amount, simplify it.  */
4641       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4642         return simplify_shift_const (x, code, mode, XEXP (x, 0),
4643                                      INTVAL (XEXP (x, 1)));
4644
4645 #ifdef SHIFT_COUNT_TRUNCATED
4646       else if (SHIFT_COUNT_TRUNCATED && GET_CODE (XEXP (x, 1)) != REG)
4647         SUBST (XEXP (x, 1),
4648                force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
4649                               ((HOST_WIDE_INT) 1
4650                                << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4651                               - 1,
4652                               NULL_RTX, 0));
4653 #endif
4654
4655       break;
4656
4657     case VEC_SELECT:
4658       {
4659         rtx op0 = XEXP (x, 0);
4660         rtx op1 = XEXP (x, 1);
4661         int len;
4662
4663         if (GET_CODE (op1) != PARALLEL)
4664           abort ();
4665         len = XVECLEN (op1, 0);
4666         if (len == 1
4667             && GET_CODE (XVECEXP (op1, 0, 0)) == CONST_INT
4668             && GET_CODE (op0) == VEC_CONCAT)
4669           {
4670             int offset = INTVAL (XVECEXP (op1, 0, 0)) * GET_MODE_SIZE (GET_MODE (x));
4671
4672             /* Try to find the element in the VEC_CONCAT.  */
4673             for (;;)
4674               {
4675                 if (GET_MODE (op0) == GET_MODE (x))
4676                   return op0;
4677                 if (GET_CODE (op0) == VEC_CONCAT)
4678                   {
4679                     HOST_WIDE_INT op0_size = GET_MODE_SIZE (GET_MODE (XEXP (op0, 0)));
4680                     if (op0_size < offset)
4681                       op0 = XEXP (op0, 0);
4682                     else
4683                       {
4684                         offset -= op0_size;
4685                         op0 = XEXP (op0, 1);
4686                       }
4687                   }
4688                 else
4689                   break;
4690               }
4691           }
4692       }
4693
4694       break;
4695
4696     default:
4697       break;
4698     }
4699
4700   return x;
4701 }
4702 \f
4703 /* Simplify X, an IF_THEN_ELSE expression.  Return the new expression.  */
4704
4705 static rtx
4706 simplify_if_then_else (rtx x)
4707 {
4708   enum machine_mode mode = GET_MODE (x);
4709   rtx cond = XEXP (x, 0);
4710   rtx true_rtx = XEXP (x, 1);
4711   rtx false_rtx = XEXP (x, 2);
4712   enum rtx_code true_code = GET_CODE (cond);
4713   int comparison_p = GET_RTX_CLASS (true_code) == '<';
4714   rtx temp;
4715   int i;
4716   enum rtx_code false_code;
4717   rtx reversed;
4718
4719   /* Simplify storing of the truth value.  */
4720   if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
4721     return gen_binary (true_code, mode, XEXP (cond, 0), XEXP (cond, 1));
4722
4723   /* Also when the truth value has to be reversed.  */
4724   if (comparison_p
4725       && true_rtx == const0_rtx && false_rtx == const_true_rtx
4726       && (reversed = reversed_comparison (cond, mode, XEXP (cond, 0),
4727                                           XEXP (cond, 1))))
4728     return reversed;
4729
4730   /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4731      in it is being compared against certain values.  Get the true and false
4732      comparisons and see if that says anything about the value of each arm.  */
4733
4734   if (comparison_p
4735       && ((false_code = combine_reversed_comparison_code (cond))
4736           != UNKNOWN)
4737       && GET_CODE (XEXP (cond, 0)) == REG)
4738     {
4739       HOST_WIDE_INT nzb;
4740       rtx from = XEXP (cond, 0);
4741       rtx true_val = XEXP (cond, 1);
4742       rtx false_val = true_val;
4743       int swapped = 0;
4744
4745       /* If FALSE_CODE is EQ, swap the codes and arms.  */
4746
4747       if (false_code == EQ)
4748         {
4749           swapped = 1, true_code = EQ, false_code = NE;
4750           temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4751         }
4752
4753       /* If we are comparing against zero and the expression being tested has
4754          only a single bit that might be nonzero, that is its value when it is
4755          not equal to zero.  Similarly if it is known to be -1 or 0.  */
4756
4757       if (true_code == EQ && true_val == const0_rtx
4758           && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4759         false_code = EQ, false_val = GEN_INT (nzb);
4760       else if (true_code == EQ && true_val == const0_rtx
4761                && (num_sign_bit_copies (from, GET_MODE (from))
4762                    == GET_MODE_BITSIZE (GET_MODE (from))))
4763         false_code = EQ, false_val = constm1_rtx;
4764
4765       /* Now simplify an arm if we know the value of the register in the
4766          branch and it is used in the arm.  Be careful due to the potential
4767          of locally-shared RTL.  */
4768
4769       if (reg_mentioned_p (from, true_rtx))
4770         true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
4771                                       from, true_val),
4772                       pc_rtx, pc_rtx, 0, 0);
4773       if (reg_mentioned_p (from, false_rtx))
4774         false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
4775                                    from, false_val),
4776                        pc_rtx, pc_rtx, 0, 0);
4777
4778       SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
4779       SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
4780
4781       true_rtx = XEXP (x, 1);
4782       false_rtx = XEXP (x, 2);
4783       true_code = GET_CODE (cond);
4784     }
4785
4786   /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4787      reversed, do so to avoid needing two sets of patterns for
4788      subtract-and-branch insns.  Similarly if we have a constant in the true
4789      arm, the false arm is the same as the first operand of the comparison, or
4790      the false arm is more complicated than the true arm.  */
4791
4792   if (comparison_p
4793       && combine_reversed_comparison_code (cond) != UNKNOWN
4794       && (true_rtx == pc_rtx
4795           || (CONSTANT_P (true_rtx)
4796               && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
4797           || true_rtx == const0_rtx
4798           || (GET_RTX_CLASS (GET_CODE (true_rtx)) == 'o'
4799               && GET_RTX_CLASS (GET_CODE (false_rtx)) != 'o')
4800           || (GET_CODE (true_rtx) == SUBREG
4801               && GET_RTX_CLASS (GET_CODE (SUBREG_REG (true_rtx))) == 'o'
4802               && GET_RTX_CLASS (GET_CODE (false_rtx)) != 'o')
4803           || reg_mentioned_p (true_rtx, false_rtx)
4804           || rtx_equal_p (false_rtx, XEXP (cond, 0))))
4805     {
4806       true_code = reversed_comparison_code (cond, NULL);
4807       SUBST (XEXP (x, 0),
4808              reversed_comparison (cond, GET_MODE (cond), XEXP (cond, 0),
4809                                   XEXP (cond, 1)));
4810
4811       SUBST (XEXP (x, 1), false_rtx);
4812       SUBST (XEXP (x, 2), true_rtx);
4813
4814       temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4815       cond = XEXP (x, 0);
4816
4817       /* It is possible that the conditional has been simplified out.  */
4818       true_code = GET_CODE (cond);
4819       comparison_p = GET_RTX_CLASS (true_code) == '<';
4820     }
4821
4822   /* If the two arms are identical, we don't need the comparison.  */
4823
4824   if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
4825     return true_rtx;
4826
4827   /* Convert a == b ? b : a to "a".  */
4828   if (true_code == EQ && ! side_effects_p (cond)
4829       && !HONOR_NANS (mode)
4830       && rtx_equal_p (XEXP (cond, 0), false_rtx)
4831       && rtx_equal_p (XEXP (cond, 1), true_rtx))
4832     return false_rtx;
4833   else if (true_code == NE && ! side_effects_p (cond)
4834            && !HONOR_NANS (mode)
4835            && rtx_equal_p (XEXP (cond, 0), true_rtx)
4836            && rtx_equal_p (XEXP (cond, 1), false_rtx))
4837     return true_rtx;
4838
4839   /* Look for cases where we have (abs x) or (neg (abs X)).  */
4840
4841   if (GET_MODE_CLASS (mode) == MODE_INT
4842       && GET_CODE (false_rtx) == NEG
4843       && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
4844       && comparison_p
4845       && rtx_equal_p (true_rtx, XEXP (cond, 0))
4846       && ! side_effects_p (true_rtx))
4847     switch (true_code)
4848       {
4849       case GT:
4850       case GE:
4851         return simplify_gen_unary (ABS, mode, true_rtx, mode);
4852       case LT:
4853       case LE:
4854         return
4855           simplify_gen_unary (NEG, mode,
4856                               simplify_gen_unary (ABS, mode, true_rtx, mode),
4857                               mode);
4858       default:
4859         break;
4860       }
4861
4862   /* Look for MIN or MAX.  */
4863
4864   if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
4865       && comparison_p
4866       && rtx_equal_p (XEXP (cond, 0), true_rtx)
4867       && rtx_equal_p (XEXP (cond, 1), false_rtx)
4868       && ! side_effects_p (cond))
4869     switch (true_code)
4870       {
4871       case GE:
4872       case GT:
4873         return gen_binary (SMAX, mode, true_rtx, false_rtx);
4874       case LE:
4875       case LT:
4876         return gen_binary (SMIN, mode, true_rtx, false_rtx);
4877       case GEU:
4878       case GTU:
4879         return gen_binary (UMAX, mode, true_rtx, false_rtx);
4880       case LEU:
4881       case LTU:
4882         return gen_binary (UMIN, mode, true_rtx, false_rtx);
4883       default:
4884         break;
4885       }
4886
4887   /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4888      second operand is zero, this can be done as (OP Z (mult COND C2)) where
4889      C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4890      SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4891      We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4892      neither 1 or -1, but it isn't worth checking for.  */
4893
4894   if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4895       && comparison_p
4896       && GET_MODE_CLASS (mode) == MODE_INT
4897       && ! side_effects_p (x))
4898     {
4899       rtx t = make_compound_operation (true_rtx, SET);
4900       rtx f = make_compound_operation (false_rtx, SET);
4901       rtx cond_op0 = XEXP (cond, 0);
4902       rtx cond_op1 = XEXP (cond, 1);
4903       enum rtx_code op = NIL, extend_op = NIL;
4904       enum machine_mode m = mode;
4905       rtx z = 0, c1 = NULL_RTX;
4906
4907       if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4908            || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4909            || GET_CODE (t) == ASHIFT
4910            || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4911           && rtx_equal_p (XEXP (t, 0), f))
4912         c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4913
4914       /* If an identity-zero op is commutative, check whether there
4915          would be a match if we swapped the operands.  */
4916       else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4917                 || GET_CODE (t) == XOR)
4918                && rtx_equal_p (XEXP (t, 1), f))
4919         c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4920       else if (GET_CODE (t) == SIGN_EXTEND
4921                && (GET_CODE (XEXP (t, 0)) == PLUS
4922                    || GET_CODE (XEXP (t, 0)) == MINUS
4923                    || GET_CODE (XEXP (t, 0)) == IOR
4924                    || GET_CODE (XEXP (t, 0)) == XOR
4925                    || GET_CODE (XEXP (t, 0)) == ASHIFT
4926                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4927                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4928                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4929                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4930                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4931                && (num_sign_bit_copies (f, GET_MODE (f))
4932                    > (unsigned int)
4933                      (GET_MODE_BITSIZE (mode)
4934                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4935         {
4936           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4937           extend_op = SIGN_EXTEND;
4938           m = GET_MODE (XEXP (t, 0));
4939         }
4940       else if (GET_CODE (t) == SIGN_EXTEND
4941                && (GET_CODE (XEXP (t, 0)) == PLUS
4942                    || GET_CODE (XEXP (t, 0)) == IOR
4943                    || GET_CODE (XEXP (t, 0)) == XOR)
4944                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4945                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4946                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4947                && (num_sign_bit_copies (f, GET_MODE (f))
4948                    > (unsigned int)
4949                      (GET_MODE_BITSIZE (mode)
4950                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
4951         {
4952           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4953           extend_op = SIGN_EXTEND;
4954           m = GET_MODE (XEXP (t, 0));
4955         }
4956       else if (GET_CODE (t) == ZERO_EXTEND
4957                && (GET_CODE (XEXP (t, 0)) == PLUS
4958                    || GET_CODE (XEXP (t, 0)) == MINUS
4959                    || GET_CODE (XEXP (t, 0)) == IOR
4960                    || GET_CODE (XEXP (t, 0)) == XOR
4961                    || GET_CODE (XEXP (t, 0)) == ASHIFT
4962                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4963                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4964                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4965                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4966                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4967                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4968                && ((nonzero_bits (f, GET_MODE (f))
4969                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
4970                    == 0))
4971         {
4972           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4973           extend_op = ZERO_EXTEND;
4974           m = GET_MODE (XEXP (t, 0));
4975         }
4976       else if (GET_CODE (t) == ZERO_EXTEND
4977                && (GET_CODE (XEXP (t, 0)) == PLUS
4978                    || GET_CODE (XEXP (t, 0)) == IOR
4979                    || GET_CODE (XEXP (t, 0)) == XOR)
4980                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4981                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4982                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4983                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4984                && ((nonzero_bits (f, GET_MODE (f))
4985                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
4986                    == 0))
4987         {
4988           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4989           extend_op = ZERO_EXTEND;
4990           m = GET_MODE (XEXP (t, 0));
4991         }
4992
4993       if (z)
4994         {
4995           temp = subst (gen_binary (true_code, m, cond_op0, cond_op1),
4996                         pc_rtx, pc_rtx, 0, 0);
4997           temp = gen_binary (MULT, m, temp,
4998                              gen_binary (MULT, m, c1, const_true_rtx));
4999           temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
5000           temp = gen_binary (op, m, gen_lowpart_for_combine (m, z), temp);
5001
5002           if (extend_op != NIL)
5003             temp = simplify_gen_unary (extend_op, mode, temp, m);
5004
5005           return temp;
5006         }
5007     }
5008
5009   /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
5010      1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
5011      negation of a single bit, we can convert this operation to a shift.  We
5012      can actually do this more generally, but it doesn't seem worth it.  */
5013
5014   if (true_code == NE && XEXP (cond, 1) == const0_rtx
5015       && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5016       && ((1 == nonzero_bits (XEXP (cond, 0), mode)
5017            && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
5018           || ((num_sign_bit_copies (XEXP (cond, 0), mode)
5019                == GET_MODE_BITSIZE (mode))
5020               && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
5021     return
5022       simplify_shift_const (NULL_RTX, ASHIFT, mode,
5023                             gen_lowpart_for_combine (mode, XEXP (cond, 0)), i);
5024
5025   /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8.  */
5026   if (true_code == NE && XEXP (cond, 1) == const0_rtx
5027       && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5028       && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
5029           == nonzero_bits (XEXP (cond, 0), mode)
5030       && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
5031     return XEXP (cond, 0);
5032
5033   return x;
5034 }
5035 \f
5036 /* Simplify X, a SET expression.  Return the new expression.  */
5037
5038 static rtx
5039 simplify_set (rtx x)
5040 {
5041   rtx src = SET_SRC (x);
5042   rtx dest = SET_DEST (x);
5043   enum machine_mode mode
5044     = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
5045   rtx other_insn;
5046   rtx *cc_use;
5047
5048   /* (set (pc) (return)) gets written as (return).  */
5049   if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
5050     return src;
5051
5052   /* Now that we know for sure which bits of SRC we are using, see if we can
5053      simplify the expression for the object knowing that we only need the
5054      low-order bits.  */
5055
5056   if (GET_MODE_CLASS (mode) == MODE_INT
5057       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5058     {
5059       src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, NULL_RTX, 0);
5060       SUBST (SET_SRC (x), src);
5061     }
5062
5063   /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5064      the comparison result and try to simplify it unless we already have used
5065      undobuf.other_insn.  */
5066   if ((GET_MODE_CLASS (mode) == MODE_CC
5067        || GET_CODE (src) == COMPARE
5068        || CC0_P (dest))
5069       && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5070       && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5071       && GET_RTX_CLASS (GET_CODE (*cc_use)) == '<'
5072       && rtx_equal_p (XEXP (*cc_use, 0), dest))
5073     {
5074       enum rtx_code old_code = GET_CODE (*cc_use);
5075       enum rtx_code new_code;
5076       rtx op0, op1, tmp;
5077       int other_changed = 0;
5078       enum machine_mode compare_mode = GET_MODE (dest);
5079       enum machine_mode tmp_mode;
5080
5081       if (GET_CODE (src) == COMPARE)
5082         op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5083       else
5084         op0 = src, op1 = const0_rtx;
5085
5086       /* Check whether the comparison is known at compile time.  */
5087       if (GET_MODE (op0) != VOIDmode)
5088         tmp_mode = GET_MODE (op0);
5089       else if (GET_MODE (op1) != VOIDmode)
5090         tmp_mode = GET_MODE (op1);
5091       else
5092         tmp_mode = compare_mode;
5093       tmp = simplify_relational_operation (old_code, tmp_mode, op0, op1);
5094       if (tmp != NULL_RTX)
5095         {
5096           rtx pat = PATTERN (other_insn);
5097           undobuf.other_insn = other_insn;
5098           SUBST (*cc_use, tmp);
5099
5100           /* Attempt to simplify CC user.  */
5101           if (GET_CODE (pat) == SET)
5102             {
5103               rtx new = simplify_rtx (SET_SRC (pat));
5104               if (new != NULL_RTX)
5105                 SUBST (SET_SRC (pat), new);
5106             }
5107
5108           /* Convert X into a no-op move.  */
5109           SUBST (SET_DEST (x), pc_rtx);
5110           SUBST (SET_SRC (x), pc_rtx);
5111           return x;
5112         }
5113
5114       /* Simplify our comparison, if possible.  */
5115       new_code = simplify_comparison (old_code, &op0, &op1);
5116
5117 #ifdef EXTRA_CC_MODES
5118       /* If this machine has CC modes other than CCmode, check to see if we
5119          need to use a different CC mode here.  */
5120       compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5121 #endif /* EXTRA_CC_MODES */
5122
5123 #if !defined (HAVE_cc0) && defined (EXTRA_CC_MODES)
5124       /* If the mode changed, we have to change SET_DEST, the mode in the
5125          compare, and the mode in the place SET_DEST is used.  If SET_DEST is
5126          a hard register, just build new versions with the proper mode.  If it
5127          is a pseudo, we lose unless it is only time we set the pseudo, in
5128          which case we can safely change its mode.  */
5129       if (compare_mode != GET_MODE (dest))
5130         {
5131           unsigned int regno = REGNO (dest);
5132           rtx new_dest = gen_rtx_REG (compare_mode, regno);
5133
5134           if (regno < FIRST_PSEUDO_REGISTER
5135               || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
5136             {
5137               if (regno >= FIRST_PSEUDO_REGISTER)
5138                 SUBST (regno_reg_rtx[regno], new_dest);
5139
5140               SUBST (SET_DEST (x), new_dest);
5141               SUBST (XEXP (*cc_use, 0), new_dest);
5142               other_changed = 1;
5143
5144               dest = new_dest;
5145             }
5146         }
5147 #endif
5148
5149       /* If the code changed, we have to build a new comparison in
5150          undobuf.other_insn.  */
5151       if (new_code != old_code)
5152         {
5153           unsigned HOST_WIDE_INT mask;
5154
5155           SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5156                                           dest, const0_rtx));
5157
5158           /* If the only change we made was to change an EQ into an NE or
5159              vice versa, OP0 has only one bit that might be nonzero, and OP1
5160              is zero, check if changing the user of the condition code will
5161              produce a valid insn.  If it won't, we can keep the original code
5162              in that insn by surrounding our operation with an XOR.  */
5163
5164           if (((old_code == NE && new_code == EQ)
5165                || (old_code == EQ && new_code == NE))
5166               && ! other_changed && op1 == const0_rtx
5167               && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5168               && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5169             {
5170               rtx pat = PATTERN (other_insn), note = 0;
5171
5172               if ((recog_for_combine (&pat, other_insn, &note) < 0
5173                    && ! check_asm_operands (pat)))
5174                 {
5175                   PUT_CODE (*cc_use, old_code);
5176                   other_insn = 0;
5177
5178                   op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
5179                 }
5180             }
5181
5182           other_changed = 1;
5183         }
5184
5185       if (other_changed)
5186         undobuf.other_insn = other_insn;
5187
5188 #ifdef HAVE_cc0
5189       /* If we are now comparing against zero, change our source if
5190          needed.  If we do not use cc0, we always have a COMPARE.  */
5191       if (op1 == const0_rtx && dest == cc0_rtx)
5192         {
5193           SUBST (SET_SRC (x), op0);
5194           src = op0;
5195         }
5196       else
5197 #endif
5198
5199       /* Otherwise, if we didn't previously have a COMPARE in the
5200          correct mode, we need one.  */
5201       if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5202         {
5203           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5204           src = SET_SRC (x);
5205         }
5206       else
5207         {
5208           /* Otherwise, update the COMPARE if needed.  */
5209           SUBST (XEXP (src, 0), op0);
5210           SUBST (XEXP (src, 1), op1);
5211         }
5212     }
5213   else
5214     {
5215       /* Get SET_SRC in a form where we have placed back any
5216          compound expressions.  Then do the checks below.  */
5217       src = make_compound_operation (src, SET);
5218       SUBST (SET_SRC (x), src);
5219     }
5220
5221 #ifdef WORD_REGISTER_OPERATIONS
5222   /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5223      and X being a REG or (subreg (reg)), we may be able to convert this to
5224      (set (subreg:m2 x) (op)).
5225
5226      On a machine where WORD_REGISTER_OPERATIONS is defined, this
5227      transformation is safe as long as M1 and M2 have the same number
5228      of words.
5229
5230      However, on a machine without WORD_REGISTER_OPERATIONS defined,
5231      we cannot apply this transformation because it would create a
5232      paradoxical subreg in SET_DEST.  */
5233
5234   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5235       && GET_RTX_CLASS (GET_CODE (SUBREG_REG (src))) != 'o'
5236       && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5237            / UNITS_PER_WORD)
5238           == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5239                + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5240 #ifdef CANNOT_CHANGE_MODE_CLASS
5241       && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
5242             && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5243                                          GET_MODE (SUBREG_REG (src)),
5244                                          GET_MODE (src)))
5245 #endif
5246       && (GET_CODE (dest) == REG
5247           || (GET_CODE (dest) == SUBREG
5248               && GET_CODE (SUBREG_REG (dest)) == REG)))
5249     {
5250       SUBST (SET_DEST (x),
5251              gen_lowpart_for_combine (GET_MODE (SUBREG_REG (src)),
5252                                       dest));
5253       SUBST (SET_SRC (x), SUBREG_REG (src));
5254
5255       src = SET_SRC (x), dest = SET_DEST (x);
5256     }
5257 #endif
5258
5259 #ifdef HAVE_cc0
5260   /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5261      in SRC.  */
5262   if (dest == cc0_rtx
5263       && GET_CODE (src) == SUBREG
5264       && subreg_lowpart_p (src)
5265       && (GET_MODE_BITSIZE (GET_MODE (src))
5266           < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5267     {
5268       rtx inner = SUBREG_REG (src);
5269       enum machine_mode inner_mode = GET_MODE (inner);
5270
5271       /* Here we make sure that we don't have a sign bit on.  */
5272       if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5273           && (nonzero_bits (inner, inner_mode)
5274               < ((unsigned HOST_WIDE_INT) 1
5275                  << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5276         {
5277           SUBST (SET_SRC (x), inner);
5278           src = SET_SRC (x);
5279         }
5280     }
5281 #endif
5282
5283 #ifdef LOAD_EXTEND_OP
5284   /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5285      would require a paradoxical subreg.  Replace the subreg with a
5286      zero_extend to avoid the reload that would otherwise be required.  */
5287
5288   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5289       && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
5290       && SUBREG_BYTE (src) == 0
5291       && (GET_MODE_SIZE (GET_MODE (src))
5292           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5293       && GET_CODE (SUBREG_REG (src)) == MEM)
5294     {
5295       SUBST (SET_SRC (x),
5296              gen_rtx (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5297                       GET_MODE (src), SUBREG_REG (src)));
5298
5299       src = SET_SRC (x);
5300     }
5301 #endif
5302
5303   /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5304      are comparing an item known to be 0 or -1 against 0, use a logical
5305      operation instead. Check for one of the arms being an IOR of the other
5306      arm with some value.  We compute three terms to be IOR'ed together.  In
5307      practice, at most two will be nonzero.  Then we do the IOR's.  */
5308
5309   if (GET_CODE (dest) != PC
5310       && GET_CODE (src) == IF_THEN_ELSE
5311       && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5312       && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5313       && XEXP (XEXP (src, 0), 1) == const0_rtx
5314       && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5315 #ifdef HAVE_conditional_move
5316       && ! can_conditionally_move_p (GET_MODE (src))
5317 #endif
5318       && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5319                                GET_MODE (XEXP (XEXP (src, 0), 0)))
5320           == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5321       && ! side_effects_p (src))
5322     {
5323       rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5324                       ? XEXP (src, 1) : XEXP (src, 2));
5325       rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5326                    ? XEXP (src, 2) : XEXP (src, 1));
5327       rtx term1 = const0_rtx, term2, term3;
5328
5329       if (GET_CODE (true_rtx) == IOR
5330           && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5331         term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5332       else if (GET_CODE (true_rtx) == IOR
5333                && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5334         term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5335       else if (GET_CODE (false_rtx) == IOR
5336                && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5337         term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5338       else if (GET_CODE (false_rtx) == IOR
5339                && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5340         term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5341
5342       term2 = gen_binary (AND, GET_MODE (src),
5343                           XEXP (XEXP (src, 0), 0), true_rtx);
5344       term3 = gen_binary (AND, GET_MODE (src),
5345                           simplify_gen_unary (NOT, GET_MODE (src),
5346                                               XEXP (XEXP (src, 0), 0),
5347                                               GET_MODE (src)),
5348                           false_rtx);
5349
5350       SUBST (SET_SRC (x),
5351              gen_binary (IOR, GET_MODE (src),
5352                          gen_binary (IOR, GET_MODE (src), term1, term2),
5353                          term3));
5354
5355       src = SET_SRC (x);
5356     }
5357
5358   /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5359      whole thing fail.  */
5360   if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5361     return src;
5362   else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5363     return dest;
5364   else
5365     /* Convert this into a field assignment operation, if possible.  */
5366     return make_field_assignment (x);
5367 }
5368 \f
5369 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5370    result.  LAST is nonzero if this is the last retry.  */
5371
5372 static rtx
5373 simplify_logical (rtx x, int last)
5374 {
5375   enum machine_mode mode = GET_MODE (x);
5376   rtx op0 = XEXP (x, 0);
5377   rtx op1 = XEXP (x, 1);
5378   rtx reversed;
5379
5380   switch (GET_CODE (x))
5381     {
5382     case AND:
5383       /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
5384          insn (and may simplify more).  */
5385       if (GET_CODE (op0) == XOR
5386           && rtx_equal_p (XEXP (op0, 0), op1)
5387           && ! side_effects_p (op1))
5388         x = gen_binary (AND, mode,
5389                         simplify_gen_unary (NOT, mode, XEXP (op0, 1), mode),
5390                         op1);
5391
5392       if (GET_CODE (op0) == XOR
5393           && rtx_equal_p (XEXP (op0, 1), op1)
5394           && ! side_effects_p (op1))
5395         x = gen_binary (AND, mode,
5396                         simplify_gen_unary (NOT, mode, XEXP (op0, 0), mode),
5397                         op1);
5398
5399       /* Similarly for (~(A ^ B)) & A.  */
5400       if (GET_CODE (op0) == NOT
5401           && GET_CODE (XEXP (op0, 0)) == XOR
5402           && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
5403           && ! side_effects_p (op1))
5404         x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
5405
5406       if (GET_CODE (op0) == NOT
5407           && GET_CODE (XEXP (op0, 0)) == XOR
5408           && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
5409           && ! side_effects_p (op1))
5410         x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
5411
5412       /* We can call simplify_and_const_int only if we don't lose
5413          any (sign) bits when converting INTVAL (op1) to
5414          "unsigned HOST_WIDE_INT".  */
5415       if (GET_CODE (op1) == CONST_INT
5416           && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5417               || INTVAL (op1) > 0))
5418         {
5419           x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5420
5421           /* If we have (ior (and (X C1) C2)) and the next restart would be
5422              the last, simplify this by making C1 as small as possible
5423              and then exit.  */
5424           if (last
5425               && GET_CODE (x) == IOR && GET_CODE (op0) == AND
5426               && GET_CODE (XEXP (op0, 1)) == CONST_INT
5427               && GET_CODE (op1) == CONST_INT)
5428             return gen_binary (IOR, mode,
5429                                gen_binary (AND, mode, XEXP (op0, 0),
5430                                            GEN_INT (INTVAL (XEXP (op0, 1))
5431                                                     & ~INTVAL (op1))), op1);
5432
5433           if (GET_CODE (x) != AND)
5434             return x;
5435
5436           if (GET_RTX_CLASS (GET_CODE (x)) == 'c'
5437               || GET_RTX_CLASS (GET_CODE (x)) == '2')
5438             op0 = XEXP (x, 0), op1 = XEXP (x, 1);
5439         }
5440
5441       /* Convert (A | B) & A to A.  */
5442       if (GET_CODE (op0) == IOR
5443           && (rtx_equal_p (XEXP (op0, 0), op1)
5444               || rtx_equal_p (XEXP (op0, 1), op1))
5445           && ! side_effects_p (XEXP (op0, 0))
5446           && ! side_effects_p (XEXP (op0, 1)))
5447         return op1;
5448
5449       /* In the following group of tests (and those in case IOR below),
5450          we start with some combination of logical operations and apply
5451          the distributive law followed by the inverse distributive law.
5452          Most of the time, this results in no change.  However, if some of
5453          the operands are the same or inverses of each other, simplifications
5454          will result.
5455
5456          For example, (and (ior A B) (not B)) can occur as the result of
5457          expanding a bit field assignment.  When we apply the distributive
5458          law to this, we get (ior (and (A (not B))) (and (B (not B)))),
5459          which then simplifies to (and (A (not B))).
5460
5461          If we have (and (ior A B) C), apply the distributive law and then
5462          the inverse distributive law to see if things simplify.  */
5463
5464       if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5465         {
5466           x = apply_distributive_law
5467             (gen_binary (GET_CODE (op0), mode,
5468                          gen_binary (AND, mode, XEXP (op0, 0), op1),
5469                          gen_binary (AND, mode, XEXP (op0, 1),
5470                                      copy_rtx (op1))));
5471           if (GET_CODE (x) != AND)
5472             return x;
5473         }
5474
5475       if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5476         return apply_distributive_law
5477           (gen_binary (GET_CODE (op1), mode,
5478                        gen_binary (AND, mode, XEXP (op1, 0), op0),
5479                        gen_binary (AND, mode, XEXP (op1, 1),
5480                                    copy_rtx (op0))));
5481
5482       /* Similarly, taking advantage of the fact that
5483          (and (not A) (xor B C)) == (xor (ior A B) (ior A C))  */
5484
5485       if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
5486         return apply_distributive_law
5487           (gen_binary (XOR, mode,
5488                        gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
5489                        gen_binary (IOR, mode, copy_rtx (XEXP (op0, 0)),
5490                                    XEXP (op1, 1))));
5491
5492       else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
5493         return apply_distributive_law
5494           (gen_binary (XOR, mode,
5495                        gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
5496                        gen_binary (IOR, mode, copy_rtx (XEXP (op1, 0)), XEXP (op0, 1))));
5497       break;
5498
5499     case IOR:
5500       /* (ior A C) is C if all bits of A that might be nonzero are on in C.  */
5501       if (GET_CODE (op1) == CONST_INT
5502           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5503           && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
5504         return op1;
5505
5506       /* Convert (A & B) | A to A.  */
5507       if (GET_CODE (op0) == AND
5508           && (rtx_equal_p (XEXP (op0, 0), op1)
5509               || rtx_equal_p (XEXP (op0, 1), op1))
5510           && ! side_effects_p (XEXP (op0, 0))
5511           && ! side_effects_p (XEXP (op0, 1)))
5512         return op1;
5513
5514       /* If we have (ior (and A B) C), apply the distributive law and then
5515          the inverse distributive law to see if things simplify.  */
5516
5517       if (GET_CODE (op0) == AND)
5518         {
5519           x = apply_distributive_law
5520             (gen_binary (AND, mode,
5521                          gen_binary (IOR, mode, XEXP (op0, 0), op1),
5522                          gen_binary (IOR, mode, XEXP (op0, 1),
5523                                      copy_rtx (op1))));
5524
5525           if (GET_CODE (x) != IOR)
5526             return x;
5527         }
5528
5529       if (GET_CODE (op1) == AND)
5530         {
5531           x = apply_distributive_law
5532             (gen_binary (AND, mode,
5533                          gen_binary (IOR, mode, XEXP (op1, 0), op0),
5534                          gen_binary (IOR, mode, XEXP (op1, 1),
5535                                      copy_rtx (op0))));
5536
5537           if (GET_CODE (x) != IOR)
5538             return x;
5539         }
5540
5541       /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
5542          mode size to (rotate A CX).  */
5543
5544       if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
5545            || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
5546           && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
5547           && GET_CODE (XEXP (op0, 1)) == CONST_INT
5548           && GET_CODE (XEXP (op1, 1)) == CONST_INT
5549           && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
5550               == GET_MODE_BITSIZE (mode)))
5551         return gen_rtx_ROTATE (mode, XEXP (op0, 0),
5552                                (GET_CODE (op0) == ASHIFT
5553                                 ? XEXP (op0, 1) : XEXP (op1, 1)));
5554
5555       /* If OP0 is (ashiftrt (plus ...) C), it might actually be
5556          a (sign_extend (plus ...)).  If so, OP1 is a CONST_INT, and the PLUS
5557          does not affect any of the bits in OP1, it can really be done
5558          as a PLUS and we can associate.  We do this by seeing if OP1
5559          can be safely shifted left C bits.  */
5560       if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
5561           && GET_CODE (XEXP (op0, 0)) == PLUS
5562           && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
5563           && GET_CODE (XEXP (op0, 1)) == CONST_INT
5564           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
5565         {
5566           int count = INTVAL (XEXP (op0, 1));
5567           HOST_WIDE_INT mask = INTVAL (op1) << count;
5568
5569           if (mask >> count == INTVAL (op1)
5570               && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5571             {
5572               SUBST (XEXP (XEXP (op0, 0), 1),
5573                      GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5574               return op0;
5575             }
5576         }
5577       break;
5578
5579     case XOR:
5580       /* If we are XORing two things that have no bits in common,
5581          convert them into an IOR.  This helps to detect rotation encoded
5582          using those methods and possibly other simplifications.  */
5583
5584       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5585           && (nonzero_bits (op0, mode)
5586               & nonzero_bits (op1, mode)) == 0)
5587         return (gen_binary (IOR, mode, op0, op1));
5588
5589       /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5590          Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5591          (NOT y).  */
5592       {
5593         int num_negated = 0;
5594
5595         if (GET_CODE (op0) == NOT)
5596           num_negated++, op0 = XEXP (op0, 0);
5597         if (GET_CODE (op1) == NOT)
5598           num_negated++, op1 = XEXP (op1, 0);
5599
5600         if (num_negated == 2)
5601           {
5602             SUBST (XEXP (x, 0), op0);
5603             SUBST (XEXP (x, 1), op1);
5604           }
5605         else if (num_negated == 1)
5606           return
5607             simplify_gen_unary (NOT, mode, gen_binary (XOR, mode, op0, op1),
5608                                 mode);
5609       }
5610
5611       /* Convert (xor (and A B) B) to (and (not A) B).  The latter may
5612          correspond to a machine insn or result in further simplifications
5613          if B is a constant.  */
5614
5615       if (GET_CODE (op0) == AND
5616           && rtx_equal_p (XEXP (op0, 1), op1)
5617           && ! side_effects_p (op1))
5618         return gen_binary (AND, mode,
5619                            simplify_gen_unary (NOT, mode, XEXP (op0, 0), mode),
5620                            op1);
5621
5622       else if (GET_CODE (op0) == AND
5623                && rtx_equal_p (XEXP (op0, 0), op1)
5624                && ! side_effects_p (op1))
5625         return gen_binary (AND, mode,
5626                            simplify_gen_unary (NOT, mode, XEXP (op0, 1), mode),
5627                            op1);
5628
5629       /* (xor (comparison foo bar) (const_int 1)) can become the reversed
5630          comparison if STORE_FLAG_VALUE is 1.  */
5631       if (STORE_FLAG_VALUE == 1
5632           && op1 == const1_rtx
5633           && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5634           && (reversed = reversed_comparison (op0, mode, XEXP (op0, 0),
5635                                               XEXP (op0, 1))))
5636         return reversed;
5637
5638       /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5639          is (lt foo (const_int 0)), so we can perform the above
5640          simplification if STORE_FLAG_VALUE is 1.  */
5641
5642       if (STORE_FLAG_VALUE == 1
5643           && op1 == const1_rtx
5644           && GET_CODE (op0) == LSHIFTRT
5645           && GET_CODE (XEXP (op0, 1)) == CONST_INT
5646           && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5647         return gen_rtx_GE (mode, XEXP (op0, 0), const0_rtx);
5648
5649       /* (xor (comparison foo bar) (const_int sign-bit))
5650          when STORE_FLAG_VALUE is the sign bit.  */
5651       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5652           && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5653               == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5654           && op1 == const_true_rtx
5655           && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5656           && (reversed = reversed_comparison (op0, mode, XEXP (op0, 0),
5657                                               XEXP (op0, 1))))
5658         return reversed;
5659
5660       break;
5661
5662     default:
5663       abort ();
5664     }
5665
5666   return x;
5667 }
5668 \f
5669 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5670    operations" because they can be replaced with two more basic operations.
5671    ZERO_EXTEND is also considered "compound" because it can be replaced with
5672    an AND operation, which is simpler, though only one operation.
5673
5674    The function expand_compound_operation is called with an rtx expression
5675    and will convert it to the appropriate shifts and AND operations,
5676    simplifying at each stage.
5677
5678    The function make_compound_operation is called to convert an expression
5679    consisting of shifts and ANDs into the equivalent compound expression.
5680    It is the inverse of this function, loosely speaking.  */
5681
5682 static rtx
5683 expand_compound_operation (rtx x)
5684 {
5685   unsigned HOST_WIDE_INT pos = 0, len;
5686   int unsignedp = 0;
5687   unsigned int modewidth;
5688   rtx tem;
5689
5690   switch (GET_CODE (x))
5691     {
5692     case ZERO_EXTEND:
5693       unsignedp = 1;
5694     case SIGN_EXTEND:
5695       /* We can't necessarily use a const_int for a multiword mode;
5696          it depends on implicitly extending the value.
5697          Since we don't know the right way to extend it,
5698          we can't tell whether the implicit way is right.
5699
5700          Even for a mode that is no wider than a const_int,
5701          we can't win, because we need to sign extend one of its bits through
5702          the rest of it, and we don't know which bit.  */
5703       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5704         return x;
5705
5706       /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5707          (zero_extend:MODE FROM) or (sign_extend:MODE FROM).  It is for any MEM
5708          because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5709          reloaded. If not for that, MEM's would very rarely be safe.
5710
5711          Reject MODEs bigger than a word, because we might not be able
5712          to reference a two-register group starting with an arbitrary register
5713          (and currently gen_lowpart might crash for a SUBREG).  */
5714
5715       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5716         return x;
5717
5718       /* Reject MODEs that aren't scalar integers because turning vector
5719          or complex modes into shifts causes problems.  */
5720
5721       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5722         return x;
5723
5724       len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5725       /* If the inner object has VOIDmode (the only way this can happen
5726          is if it is an ASM_OPERANDS), we can't do anything since we don't
5727          know how much masking to do.  */
5728       if (len == 0)
5729         return x;
5730
5731       break;
5732
5733     case ZERO_EXTRACT:
5734       unsignedp = 1;
5735     case SIGN_EXTRACT:
5736       /* If the operand is a CLOBBER, just return it.  */
5737       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5738         return XEXP (x, 0);
5739
5740       if (GET_CODE (XEXP (x, 1)) != CONST_INT
5741           || GET_CODE (XEXP (x, 2)) != CONST_INT
5742           || GET_MODE (XEXP (x, 0)) == VOIDmode)
5743         return x;
5744
5745       /* Reject MODEs that aren't scalar integers because turning vector
5746          or complex modes into shifts causes problems.  */
5747
5748       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5749         return x;
5750
5751       len = INTVAL (XEXP (x, 1));
5752       pos = INTVAL (XEXP (x, 2));
5753
5754       /* If this goes outside the object being extracted, replace the object
5755          with a (use (mem ...)) construct that only combine understands
5756          and is used only for this purpose.  */
5757       if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5758         SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5759
5760       if (BITS_BIG_ENDIAN)
5761         pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5762
5763       break;
5764
5765     default:
5766       return x;
5767     }
5768   /* Convert sign extension to zero extension, if we know that the high
5769      bit is not set, as this is easier to optimize.  It will be converted
5770      back to cheaper alternative in make_extraction.  */
5771   if (GET_CODE (x) == SIGN_EXTEND
5772       && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5773           && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
5774                 & ~(((unsigned HOST_WIDE_INT)
5775                       GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5776                      >> 1))
5777                == 0)))
5778     {
5779       rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
5780       rtx temp2 = expand_compound_operation (temp);
5781
5782       /* Make sure this is a profitable operation.  */
5783       if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
5784        return temp2;
5785       else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
5786        return temp;
5787       else
5788        return x;
5789     }
5790
5791   /* We can optimize some special cases of ZERO_EXTEND.  */
5792   if (GET_CODE (x) == ZERO_EXTEND)
5793     {
5794       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5795          know that the last value didn't have any inappropriate bits
5796          set.  */
5797       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5798           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5799           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5800           && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5801               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5802         return XEXP (XEXP (x, 0), 0);
5803
5804       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
5805       if (GET_CODE (XEXP (x, 0)) == SUBREG
5806           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5807           && subreg_lowpart_p (XEXP (x, 0))
5808           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5809           && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5810               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5811         return SUBREG_REG (XEXP (x, 0));
5812
5813       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5814          is a comparison and STORE_FLAG_VALUE permits.  This is like
5815          the first case, but it works even when GET_MODE (x) is larger
5816          than HOST_WIDE_INT.  */
5817       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5818           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5819           && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) == '<'
5820           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5821               <= HOST_BITS_PER_WIDE_INT)
5822           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5823               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5824         return XEXP (XEXP (x, 0), 0);
5825
5826       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
5827       if (GET_CODE (XEXP (x, 0)) == SUBREG
5828           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5829           && subreg_lowpart_p (XEXP (x, 0))
5830           && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == '<'
5831           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5832               <= HOST_BITS_PER_WIDE_INT)
5833           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5834               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5835         return SUBREG_REG (XEXP (x, 0));
5836
5837     }
5838
5839   /* If we reach here, we want to return a pair of shifts.  The inner
5840      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
5841      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
5842      logical depending on the value of UNSIGNEDP.
5843
5844      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5845      converted into an AND of a shift.
5846
5847      We must check for the case where the left shift would have a negative
5848      count.  This can happen in a case like (x >> 31) & 255 on machines
5849      that can't shift by a constant.  On those machines, we would first
5850      combine the shift with the AND to produce a variable-position
5851      extraction.  Then the constant of 31 would be substituted in to produce
5852      a such a position.  */
5853
5854   modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5855   if (modewidth + len >= pos)
5856     tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5857                                 GET_MODE (x),
5858                                 simplify_shift_const (NULL_RTX, ASHIFT,
5859                                                       GET_MODE (x),
5860                                                       XEXP (x, 0),
5861                                                       modewidth - pos - len),
5862                                 modewidth - len);
5863
5864   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5865     tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5866                                   simplify_shift_const (NULL_RTX, LSHIFTRT,
5867                                                         GET_MODE (x),
5868                                                         XEXP (x, 0), pos),
5869                                   ((HOST_WIDE_INT) 1 << len) - 1);
5870   else
5871     /* Any other cases we can't handle.  */
5872     return x;
5873
5874   /* If we couldn't do this for some reason, return the original
5875      expression.  */
5876   if (GET_CODE (tem) == CLOBBER)
5877     return x;
5878
5879   return tem;
5880 }
5881 \f
5882 /* X is a SET which contains an assignment of one object into
5883    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5884    or certain SUBREGS). If possible, convert it into a series of
5885    logical operations.
5886
5887    We half-heartedly support variable positions, but do not at all
5888    support variable lengths.  */
5889
5890 static rtx
5891 expand_field_assignment (rtx x)
5892 {
5893   rtx inner;
5894   rtx pos;                      /* Always counts from low bit.  */
5895   int len;
5896   rtx mask;
5897   enum machine_mode compute_mode;
5898
5899   /* Loop until we find something we can't simplify.  */
5900   while (1)
5901     {
5902       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5903           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5904         {
5905           inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5906           len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5907           pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
5908         }
5909       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5910                && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5911         {
5912           inner = XEXP (SET_DEST (x), 0);
5913           len = INTVAL (XEXP (SET_DEST (x), 1));
5914           pos = XEXP (SET_DEST (x), 2);
5915
5916           /* If the position is constant and spans the width of INNER,
5917              surround INNER  with a USE to indicate this.  */
5918           if (GET_CODE (pos) == CONST_INT
5919               && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5920             inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
5921
5922           if (BITS_BIG_ENDIAN)
5923             {
5924               if (GET_CODE (pos) == CONST_INT)
5925                 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5926                                - INTVAL (pos));
5927               else if (GET_CODE (pos) == MINUS
5928                        && GET_CODE (XEXP (pos, 1)) == CONST_INT
5929                        && (INTVAL (XEXP (pos, 1))
5930                            == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5931                 /* If position is ADJUST - X, new position is X.  */
5932                 pos = XEXP (pos, 0);
5933               else
5934                 pos = gen_binary (MINUS, GET_MODE (pos),
5935                                   GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
5936                                            - len),
5937                                   pos);
5938             }
5939         }
5940
5941       /* A SUBREG between two modes that occupy the same numbers of words
5942          can be done by moving the SUBREG to the source.  */
5943       else if (GET_CODE (SET_DEST (x)) == SUBREG
5944                /* We need SUBREGs to compute nonzero_bits properly.  */
5945                && nonzero_sign_valid
5946                && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5947                      + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5948                    == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5949                         + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5950         {
5951           x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
5952                            gen_lowpart_for_combine
5953                            (GET_MODE (SUBREG_REG (SET_DEST (x))),
5954                             SET_SRC (x)));
5955           continue;
5956         }
5957       else
5958         break;
5959
5960       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5961         inner = SUBREG_REG (inner);
5962
5963       compute_mode = GET_MODE (inner);
5964
5965       /* Don't attempt bitwise arithmetic on non scalar integer modes.  */
5966       if (! SCALAR_INT_MODE_P (compute_mode))
5967         {
5968           enum machine_mode imode;
5969
5970           /* Don't do anything for vector or complex integral types.  */
5971           if (! FLOAT_MODE_P (compute_mode))
5972             break;
5973
5974           /* Try to find an integral mode to pun with.  */
5975           imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
5976           if (imode == BLKmode)
5977             break;
5978
5979           compute_mode = imode;
5980           inner = gen_lowpart_for_combine (imode, inner);
5981         }
5982
5983       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
5984       if (len < HOST_BITS_PER_WIDE_INT)
5985         mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
5986       else
5987         break;
5988
5989       /* Now compute the equivalent expression.  Make a copy of INNER
5990          for the SET_DEST in case it is a MEM into which we will substitute;
5991          we don't want shared RTL in that case.  */
5992       x = gen_rtx_SET
5993         (VOIDmode, copy_rtx (inner),
5994          gen_binary (IOR, compute_mode,
5995                      gen_binary (AND, compute_mode,
5996                                  simplify_gen_unary (NOT, compute_mode,
5997                                                      gen_binary (ASHIFT,
5998                                                                  compute_mode,
5999                                                                  mask, pos),
6000                                                      compute_mode),
6001                                  inner),
6002                      gen_binary (ASHIFT, compute_mode,
6003                                  gen_binary (AND, compute_mode,
6004                                              gen_lowpart_for_combine
6005                                              (compute_mode, SET_SRC (x)),
6006                                              mask),
6007                                  pos)));
6008     }
6009
6010   return x;
6011 }
6012 \f
6013 /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
6014    it is an RTX that represents a variable starting position; otherwise,
6015    POS is the (constant) starting bit position (counted from the LSB).
6016
6017    INNER may be a USE.  This will occur when we started with a bitfield
6018    that went outside the boundary of the object in memory, which is
6019    allowed on most machines.  To isolate this case, we produce a USE
6020    whose mode is wide enough and surround the MEM with it.  The only
6021    code that understands the USE is this routine.  If it is not removed,
6022    it will cause the resulting insn not to match.
6023
6024    UNSIGNEDP is nonzero for an unsigned reference and zero for a
6025    signed reference.
6026
6027    IN_DEST is nonzero if this is a reference in the destination of a
6028    SET.  This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If nonzero,
6029    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6030    be used.
6031
6032    IN_COMPARE is nonzero if we are in a COMPARE.  This means that a
6033    ZERO_EXTRACT should be built even for bits starting at bit 0.
6034
6035    MODE is the desired mode of the result (if IN_DEST == 0).
6036
6037    The result is an RTX for the extraction or NULL_RTX if the target
6038    can't handle it.  */
6039
6040 static rtx
6041 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6042                  rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6043                  int in_dest, int in_compare)
6044 {
6045   /* This mode describes the size of the storage area
6046      to fetch the overall value from.  Within that, we
6047      ignore the POS lowest bits, etc.  */
6048   enum machine_mode is_mode = GET_MODE (inner);
6049   enum machine_mode inner_mode;
6050   enum machine_mode wanted_inner_mode = byte_mode;
6051   enum machine_mode wanted_inner_reg_mode = word_mode;
6052   enum machine_mode pos_mode = word_mode;
6053   enum machine_mode extraction_mode = word_mode;
6054   enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6055   int spans_byte = 0;
6056   rtx new = 0;
6057   rtx orig_pos_rtx = pos_rtx;
6058   HOST_WIDE_INT orig_pos;
6059
6060   /* Get some information about INNER and get the innermost object.  */
6061   if (GET_CODE (inner) == USE)
6062     /* (use:SI (mem:QI foo)) stands for (mem:SI foo).  */
6063     /* We don't need to adjust the position because we set up the USE
6064        to pretend that it was a full-word object.  */
6065     spans_byte = 1, inner = XEXP (inner, 0);
6066   else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6067     {
6068       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6069          consider just the QI as the memory to extract from.
6070          The subreg adds or removes high bits; its mode is
6071          irrelevant to the meaning of this extraction,
6072          since POS and LEN count from the lsb.  */
6073       if (GET_CODE (SUBREG_REG (inner)) == MEM)
6074         is_mode = GET_MODE (SUBREG_REG (inner));
6075       inner = SUBREG_REG (inner);
6076     }
6077   else if (GET_CODE (inner) == ASHIFT
6078            && GET_CODE (XEXP (inner, 1)) == CONST_INT
6079            && pos_rtx == 0 && pos == 0
6080            && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6081     {
6082       /* We're extracting the least significant bits of an rtx
6083          (ashift X (const_int C)), where LEN > C.  Extract the
6084          least significant (LEN - C) bits of X, giving an rtx
6085          whose mode is MODE, then shift it left C times.  */
6086       new = make_extraction (mode, XEXP (inner, 0),
6087                              0, 0, len - INTVAL (XEXP (inner, 1)),
6088                              unsignedp, in_dest, in_compare);
6089       if (new != 0)
6090         return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
6091     }
6092
6093   inner_mode = GET_MODE (inner);
6094
6095   if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
6096     pos = INTVAL (pos_rtx), pos_rtx = 0;
6097
6098   /* See if this can be done without an extraction.  We never can if the
6099      width of the field is not the same as that of some integer mode. For
6100      registers, we can only avoid the extraction if the position is at the
6101      low-order bit and this is either not in the destination or we have the
6102      appropriate STRICT_LOW_PART operation available.
6103
6104      For MEM, we can avoid an extract if the field starts on an appropriate
6105      boundary and we can change the mode of the memory reference.  However,
6106      we cannot directly access the MEM if we have a USE and the underlying
6107      MEM is not TMODE.  This combination means that MEM was being used in a
6108      context where bits outside its mode were being referenced; that is only
6109      valid in bit-field insns.  */
6110
6111   if (tmode != BLKmode
6112       && ! (spans_byte && inner_mode != tmode)
6113       && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6114            && GET_CODE (inner) != MEM
6115            && (! in_dest
6116                || (GET_CODE (inner) == REG
6117                    && have_insn_for (STRICT_LOW_PART, tmode))))
6118           || (GET_CODE (inner) == MEM && pos_rtx == 0
6119               && (pos
6120                   % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6121                      : BITS_PER_UNIT)) == 0
6122               /* We can't do this if we are widening INNER_MODE (it
6123                  may not be aligned, for one thing).  */
6124               && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6125               && (inner_mode == tmode
6126                   || (! mode_dependent_address_p (XEXP (inner, 0))
6127                       && ! MEM_VOLATILE_P (inner))))))
6128     {
6129       /* If INNER is a MEM, make a new MEM that encompasses just the desired
6130          field.  If the original and current mode are the same, we need not
6131          adjust the offset.  Otherwise, we do if bytes big endian.
6132
6133          If INNER is not a MEM, get a piece consisting of just the field
6134          of interest (in this case POS % BITS_PER_WORD must be 0).  */
6135
6136       if (GET_CODE (inner) == MEM)
6137         {
6138           HOST_WIDE_INT offset;
6139
6140           /* POS counts from lsb, but make OFFSET count in memory order.  */
6141           if (BYTES_BIG_ENDIAN)
6142             offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6143           else
6144             offset = pos / BITS_PER_UNIT;
6145
6146           new = adjust_address_nv (inner, tmode, offset);
6147         }
6148       else if (GET_CODE (inner) == REG)
6149         {
6150           if (tmode != inner_mode)
6151             {
6152               if (in_dest)
6153                 {
6154                   /* We can't call gen_lowpart_for_combine here since we always want
6155                      a SUBREG and it would sometimes return a new hard register.  */
6156                   HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6157
6158                   if (WORDS_BIG_ENDIAN
6159                       && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6160                     final_word = ((GET_MODE_SIZE (inner_mode)
6161                                    - GET_MODE_SIZE (tmode))
6162                                   / UNITS_PER_WORD) - final_word;
6163
6164                   final_word *= UNITS_PER_WORD;
6165                   if (BYTES_BIG_ENDIAN &&
6166                       GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6167                     final_word += (GET_MODE_SIZE (inner_mode)
6168                                    - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6169
6170                   /* Avoid creating invalid subregs, for example when
6171                      simplifying (x>>32)&255.  */
6172                   if (final_word >= GET_MODE_SIZE (inner_mode))
6173                     return NULL_RTX;
6174
6175                   new = gen_rtx_SUBREG (tmode, inner, final_word);
6176                 }
6177               else
6178                 new = gen_lowpart_for_combine (tmode, inner);
6179             }
6180           else
6181             new = inner;
6182         }
6183       else
6184         new = force_to_mode (inner, tmode,
6185                              len >= HOST_BITS_PER_WIDE_INT
6186                              ? ~(unsigned HOST_WIDE_INT) 0
6187                              : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6188                              NULL_RTX, 0);
6189
6190       /* If this extraction is going into the destination of a SET,
6191          make a STRICT_LOW_PART unless we made a MEM.  */
6192
6193       if (in_dest)
6194         return (GET_CODE (new) == MEM ? new
6195                 : (GET_CODE (new) != SUBREG
6196                    ? gen_rtx_CLOBBER (tmode, const0_rtx)
6197                    : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
6198
6199       if (mode == tmode)
6200         return new;
6201
6202       if (GET_CODE (new) == CONST_INT)
6203         return gen_int_mode (INTVAL (new), mode);
6204
6205       /* If we know that no extraneous bits are set, and that the high
6206          bit is not set, convert the extraction to the cheaper of
6207          sign and zero extension, that are equivalent in these cases.  */
6208       if (flag_expensive_optimizations
6209           && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6210               && ((nonzero_bits (new, tmode)
6211                    & ~(((unsigned HOST_WIDE_INT)
6212                         GET_MODE_MASK (tmode))
6213                        >> 1))
6214                   == 0)))
6215         {
6216           rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6217           rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6218
6219           /* Prefer ZERO_EXTENSION, since it gives more information to
6220              backends.  */
6221           if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6222             return temp;
6223           return temp1;
6224         }
6225
6226       /* Otherwise, sign- or zero-extend unless we already are in the
6227          proper mode.  */
6228
6229       return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6230                              mode, new));
6231     }
6232
6233   /* Unless this is a COMPARE or we have a funny memory reference,
6234      don't do anything with zero-extending field extracts starting at
6235      the low-order bit since they are simple AND operations.  */
6236   if (pos_rtx == 0 && pos == 0 && ! in_dest
6237       && ! in_compare && ! spans_byte && unsignedp)
6238     return 0;
6239
6240   /* Unless we are allowed to span bytes or INNER is not MEM, reject this if
6241      we would be spanning bytes or if the position is not a constant and the
6242      length is not 1.  In all other cases, we would only be going outside
6243      our object in cases when an original shift would have been
6244      undefined.  */
6245   if (! spans_byte && GET_CODE (inner) == MEM
6246       && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6247           || (pos_rtx != 0 && len != 1)))
6248     return 0;
6249
6250   /* Get the mode to use should INNER not be a MEM, the mode for the position,
6251      and the mode for the result.  */
6252   if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6253     {
6254       wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6255       pos_mode = mode_for_extraction (EP_insv, 2);
6256       extraction_mode = mode_for_extraction (EP_insv, 3);
6257     }
6258
6259   if (! in_dest && unsignedp
6260       && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6261     {
6262       wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6263       pos_mode = mode_for_extraction (EP_extzv, 3);
6264       extraction_mode = mode_for_extraction (EP_extzv, 0);
6265     }
6266
6267   if (! in_dest && ! unsignedp
6268       && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6269     {
6270       wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6271       pos_mode = mode_for_extraction (EP_extv, 3);
6272       extraction_mode = mode_for_extraction (EP_extv, 0);
6273     }
6274
6275   /* Never narrow an object, since that might not be safe.  */
6276
6277   if (mode != VOIDmode
6278       && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6279     extraction_mode = mode;
6280
6281   if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6282       && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6283     pos_mode = GET_MODE (pos_rtx);
6284
6285   /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
6286      if we have to change the mode of memory and cannot, the desired mode is
6287      EXTRACTION_MODE.  */
6288   if (GET_CODE (inner) != MEM)
6289     wanted_inner_mode = wanted_inner_reg_mode;
6290   else if (inner_mode != wanted_inner_mode
6291            && (mode_dependent_address_p (XEXP (inner, 0))
6292                || MEM_VOLATILE_P (inner)))
6293     wanted_inner_mode = extraction_mode;
6294
6295   orig_pos = pos;
6296
6297   if (BITS_BIG_ENDIAN)
6298     {
6299       /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6300          BITS_BIG_ENDIAN style.  If position is constant, compute new
6301          position.  Otherwise, build subtraction.
6302          Note that POS is relative to the mode of the original argument.
6303          If it's a MEM we need to recompute POS relative to that.
6304          However, if we're extracting from (or inserting into) a register,
6305          we want to recompute POS relative to wanted_inner_mode.  */
6306       int width = (GET_CODE (inner) == MEM
6307                    ? GET_MODE_BITSIZE (is_mode)
6308                    : GET_MODE_BITSIZE (wanted_inner_mode));
6309
6310       if (pos_rtx == 0)
6311         pos = width - len - pos;
6312       else
6313         pos_rtx
6314           = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6315       /* POS may be less than 0 now, but we check for that below.
6316          Note that it can only be less than 0 if GET_CODE (inner) != MEM.  */
6317     }
6318
6319   /* If INNER has a wider mode, make it smaller.  If this is a constant
6320      extract, try to adjust the byte to point to the byte containing
6321      the value.  */
6322   if (wanted_inner_mode != VOIDmode
6323       && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6324       && ((GET_CODE (inner) == MEM
6325            && (inner_mode == wanted_inner_mode
6326                || (! mode_dependent_address_p (XEXP (inner, 0))
6327                    && ! MEM_VOLATILE_P (inner))))))
6328     {
6329       int offset = 0;
6330
6331       /* The computations below will be correct if the machine is big
6332          endian in both bits and bytes or little endian in bits and bytes.
6333          If it is mixed, we must adjust.  */
6334
6335       /* If bytes are big endian and we had a paradoxical SUBREG, we must
6336          adjust OFFSET to compensate.  */
6337       if (BYTES_BIG_ENDIAN
6338           && ! spans_byte
6339           && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6340         offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6341
6342       /* If this is a constant position, we can move to the desired byte.  */
6343       if (pos_rtx == 0)
6344         {
6345           offset += pos / BITS_PER_UNIT;
6346           pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6347         }
6348
6349       if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6350           && ! spans_byte
6351           && is_mode != wanted_inner_mode)
6352         offset = (GET_MODE_SIZE (is_mode)
6353                   - GET_MODE_SIZE (wanted_inner_mode) - offset);
6354
6355       if (offset != 0 || inner_mode != wanted_inner_mode)
6356         inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6357     }
6358
6359   /* If INNER is not memory, we can always get it into the proper mode.  If we
6360      are changing its mode, POS must be a constant and smaller than the size
6361      of the new mode.  */
6362   else if (GET_CODE (inner) != MEM)
6363     {
6364       if (GET_MODE (inner) != wanted_inner_mode
6365           && (pos_rtx != 0
6366               || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6367         return 0;
6368
6369       inner = force_to_mode (inner, wanted_inner_mode,
6370                              pos_rtx
6371                              || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6372                              ? ~(unsigned HOST_WIDE_INT) 0
6373                              : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6374                                 << orig_pos),
6375                              NULL_RTX, 0);
6376     }
6377
6378   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
6379      have to zero extend.  Otherwise, we can just use a SUBREG.  */
6380   if (pos_rtx != 0
6381       && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6382     {
6383       rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6384
6385       /* If we know that no extraneous bits are set, and that the high
6386          bit is not set, convert extraction to cheaper one - either
6387          SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6388          cases.  */
6389       if (flag_expensive_optimizations
6390           && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6391               && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6392                    & ~(((unsigned HOST_WIDE_INT)
6393                         GET_MODE_MASK (GET_MODE (pos_rtx)))
6394                        >> 1))
6395                   == 0)))
6396         {
6397           rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6398
6399           /* Prefer ZERO_EXTENSION, since it gives more information to
6400              backends.  */
6401           if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6402             temp = temp1;
6403         }
6404       pos_rtx = temp;
6405     }
6406   else if (pos_rtx != 0
6407            && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6408     pos_rtx = gen_lowpart_for_combine (pos_mode, pos_rtx);
6409
6410   /* Make POS_RTX unless we already have it and it is correct.  If we don't
6411      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6412      be a CONST_INT.  */
6413   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6414     pos_rtx = orig_pos_rtx;
6415
6416   else if (pos_rtx == 0)
6417     pos_rtx = GEN_INT (pos);
6418
6419   /* Make the required operation.  See if we can use existing rtx.  */
6420   new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6421                          extraction_mode, inner, GEN_INT (len), pos_rtx);
6422   if (! in_dest)
6423     new = gen_lowpart_for_combine (mode, new);
6424
6425   return new;
6426 }
6427 \f
6428 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6429    with any other operations in X.  Return X without that shift if so.  */
6430
6431 static rtx
6432 extract_left_shift (rtx x, int count)
6433 {
6434   enum rtx_code code = GET_CODE (x);
6435   enum machine_mode mode = GET_MODE (x);
6436   rtx tem;
6437
6438   switch (code)
6439     {
6440     case ASHIFT:
6441       /* This is the shift itself.  If it is wide enough, we will return
6442          either the value being shifted if the shift count is equal to
6443          COUNT or a shift for the difference.  */
6444       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6445           && INTVAL (XEXP (x, 1)) >= count)
6446         return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6447                                      INTVAL (XEXP (x, 1)) - count);
6448       break;
6449
6450     case NEG:  case NOT:
6451       if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6452         return simplify_gen_unary (code, mode, tem, mode);
6453
6454       break;
6455
6456     case PLUS:  case IOR:  case XOR:  case AND:
6457       /* If we can safely shift this constant and we find the inner shift,
6458          make a new operation.  */
6459       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6460           && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6461           && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6462         return gen_binary (code, mode, tem,
6463                            GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6464
6465       break;
6466
6467     default:
6468       break;
6469     }
6470
6471   return 0;
6472 }
6473 \f
6474 /* Look at the expression rooted at X.  Look for expressions
6475    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6476    Form these expressions.
6477
6478    Return the new rtx, usually just X.
6479
6480    Also, for machines like the VAX that don't have logical shift insns,
6481    try to convert logical to arithmetic shift operations in cases where
6482    they are equivalent.  This undoes the canonicalizations to logical
6483    shifts done elsewhere.
6484
6485    We try, as much as possible, to re-use rtl expressions to save memory.
6486
6487    IN_CODE says what kind of expression we are processing.  Normally, it is
6488    SET.  In a memory address (inside a MEM, PLUS or minus, the latter two
6489    being kludges), it is MEM.  When processing the arguments of a comparison
6490    or a COMPARE against zero, it is COMPARE.  */
6491
6492 static rtx
6493 make_compound_operation (rtx x, enum rtx_code in_code)
6494 {
6495   enum rtx_code code = GET_CODE (x);
6496   enum machine_mode mode = GET_MODE (x);
6497   int mode_width = GET_MODE_BITSIZE (mode);
6498   rtx rhs, lhs;
6499   enum rtx_code next_code;
6500   int i;
6501   rtx new = 0;
6502   rtx tem;
6503   const char *fmt;
6504
6505   /* Select the code to be used in recursive calls.  Once we are inside an
6506      address, we stay there.  If we have a comparison, set to COMPARE,
6507      but once inside, go back to our default of SET.  */
6508
6509   next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6510                : ((code == COMPARE || GET_RTX_CLASS (code) == '<')
6511                   && XEXP (x, 1) == const0_rtx) ? COMPARE
6512                : in_code == COMPARE ? SET : in_code);
6513
6514   /* Process depending on the code of this operation.  If NEW is set
6515      nonzero, it will be returned.  */
6516
6517   switch (code)
6518     {
6519     case ASHIFT:
6520       /* Convert shifts by constants into multiplications if inside
6521          an address.  */
6522       if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6523           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6524           && INTVAL (XEXP (x, 1)) >= 0)
6525         {
6526           new = make_compound_operation (XEXP (x, 0), next_code);
6527           new = gen_rtx_MULT (mode, new,
6528                               GEN_INT ((HOST_WIDE_INT) 1
6529                                        << INTVAL (XEXP (x, 1))));
6530         }
6531       break;
6532
6533     case AND:
6534       /* If the second operand is not a constant, we can't do anything
6535          with it.  */
6536       if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6537         break;
6538
6539       /* If the constant is a power of two minus one and the first operand
6540          is a logical right shift, make an extraction.  */
6541       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6542           && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6543         {
6544           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6545           new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6546                                  0, in_code == COMPARE);
6547         }
6548
6549       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
6550       else if (GET_CODE (XEXP (x, 0)) == SUBREG
6551                && subreg_lowpart_p (XEXP (x, 0))
6552                && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6553                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6554         {
6555           new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6556                                          next_code);
6557           new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6558                                  XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6559                                  0, in_code == COMPARE);
6560         }
6561       /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)).  */
6562       else if ((GET_CODE (XEXP (x, 0)) == XOR
6563                 || GET_CODE (XEXP (x, 0)) == IOR)
6564                && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6565                && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6566                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6567         {
6568           /* Apply the distributive law, and then try to make extractions.  */
6569           new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6570                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6571                                              XEXP (x, 1)),
6572                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6573                                              XEXP (x, 1)));
6574           new = make_compound_operation (new, in_code);
6575         }
6576
6577       /* If we are have (and (rotate X C) M) and C is larger than the number
6578          of bits in M, this is an extraction.  */
6579
6580       else if (GET_CODE (XEXP (x, 0)) == ROTATE
6581                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6582                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6583                && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6584         {
6585           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6586           new = make_extraction (mode, new,
6587                                  (GET_MODE_BITSIZE (mode)
6588                                   - INTVAL (XEXP (XEXP (x, 0), 1))),
6589                                  NULL_RTX, i, 1, 0, in_code == COMPARE);
6590         }
6591
6592       /* On machines without logical shifts, if the operand of the AND is
6593          a logical shift and our mask turns off all the propagated sign
6594          bits, we can replace the logical shift with an arithmetic shift.  */
6595       else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6596                && !have_insn_for (LSHIFTRT, mode)
6597                && have_insn_for (ASHIFTRT, mode)
6598                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6599                && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6600                && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6601                && mode_width <= HOST_BITS_PER_WIDE_INT)
6602         {
6603           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6604
6605           mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6606           if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6607             SUBST (XEXP (x, 0),
6608                    gen_rtx_ASHIFTRT (mode,
6609                                      make_compound_operation
6610                                      (XEXP (XEXP (x, 0), 0), next_code),
6611                                      XEXP (XEXP (x, 0), 1)));
6612         }
6613
6614       /* If the constant is one less than a power of two, this might be
6615          representable by an extraction even if no shift is present.
6616          If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6617          we are in a COMPARE.  */
6618       else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6619         new = make_extraction (mode,
6620                                make_compound_operation (XEXP (x, 0),
6621                                                         next_code),
6622                                0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6623
6624       /* If we are in a comparison and this is an AND with a power of two,
6625          convert this into the appropriate bit extract.  */
6626       else if (in_code == COMPARE
6627                && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6628         new = make_extraction (mode,
6629                                make_compound_operation (XEXP (x, 0),
6630                                                         next_code),
6631                                i, NULL_RTX, 1, 1, 0, 1);
6632
6633       break;
6634
6635     case LSHIFTRT:
6636       /* If the sign bit is known to be zero, replace this with an
6637          arithmetic shift.  */
6638       if (have_insn_for (ASHIFTRT, mode)
6639           && ! have_insn_for (LSHIFTRT, mode)
6640           && mode_width <= HOST_BITS_PER_WIDE_INT
6641           && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6642         {
6643           new = gen_rtx_ASHIFTRT (mode,
6644                                   make_compound_operation (XEXP (x, 0),
6645                                                            next_code),
6646                                   XEXP (x, 1));
6647           break;
6648         }
6649
6650       /* ... fall through ...  */
6651
6652     case ASHIFTRT:
6653       lhs = XEXP (x, 0);
6654       rhs = XEXP (x, 1);
6655
6656       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6657          this is a SIGN_EXTRACT.  */
6658       if (GET_CODE (rhs) == CONST_INT
6659           && GET_CODE (lhs) == ASHIFT
6660           && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6661           && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6662         {
6663           new = make_compound_operation (XEXP (lhs, 0), next_code);
6664           new = make_extraction (mode, new,
6665                                  INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6666                                  NULL_RTX, mode_width - INTVAL (rhs),
6667                                  code == LSHIFTRT, 0, in_code == COMPARE);
6668           break;
6669         }
6670
6671       /* See if we have operations between an ASHIFTRT and an ASHIFT.
6672          If so, try to merge the shifts into a SIGN_EXTEND.  We could
6673          also do this for some cases of SIGN_EXTRACT, but it doesn't
6674          seem worth the effort; the case checked for occurs on Alpha.  */
6675
6676       if (GET_RTX_CLASS (GET_CODE (lhs)) != 'o'
6677           && ! (GET_CODE (lhs) == SUBREG
6678                 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (lhs))) == 'o'))
6679           && GET_CODE (rhs) == CONST_INT
6680           && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6681           && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6682         new = make_extraction (mode, make_compound_operation (new, next_code),
6683                                0, NULL_RTX, mode_width - INTVAL (rhs),
6684                                code == LSHIFTRT, 0, in_code == COMPARE);
6685
6686       break;
6687
6688     case SUBREG:
6689       /* Call ourselves recursively on the inner expression.  If we are
6690          narrowing the object and it has a different RTL code from
6691          what it originally did, do this SUBREG as a force_to_mode.  */
6692
6693       tem = make_compound_operation (SUBREG_REG (x), in_code);
6694       if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6695           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6696           && subreg_lowpart_p (x))
6697         {
6698           rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
6699                                      NULL_RTX, 0);
6700
6701           /* If we have something other than a SUBREG, we might have
6702              done an expansion, so rerun ourselves.  */
6703           if (GET_CODE (newer) != SUBREG)
6704             newer = make_compound_operation (newer, in_code);
6705
6706           return newer;
6707         }
6708
6709       /* If this is a paradoxical subreg, and the new code is a sign or
6710          zero extension, omit the subreg and widen the extension.  If it
6711          is a regular subreg, we can still get rid of the subreg by not
6712          widening so much, or in fact removing the extension entirely.  */
6713       if ((GET_CODE (tem) == SIGN_EXTEND
6714            || GET_CODE (tem) == ZERO_EXTEND)
6715           && subreg_lowpart_p (x))
6716         {
6717           if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (tem))
6718               || (GET_MODE_SIZE (mode) >
6719                   GET_MODE_SIZE (GET_MODE (XEXP (tem, 0)))))
6720             {
6721               if (! SCALAR_INT_MODE_P (mode))
6722                 break;
6723               tem = gen_rtx_fmt_e (GET_CODE (tem), mode, XEXP (tem, 0));
6724             }
6725           else
6726             tem = gen_lowpart_for_combine (mode, XEXP (tem, 0));
6727           return tem;
6728         }
6729       break;
6730
6731     default:
6732       break;
6733     }
6734
6735   if (new)
6736     {
6737       x = gen_lowpart_for_combine (mode, new);
6738       code = GET_CODE (x);
6739     }
6740
6741   /* Now recursively process each operand of this operation.  */
6742   fmt = GET_RTX_FORMAT (code);
6743   for (i = 0; i < GET_RTX_LENGTH (code); i++)
6744     if (fmt[i] == 'e')
6745       {
6746         new = make_compound_operation (XEXP (x, i), next_code);
6747         SUBST (XEXP (x, i), new);
6748       }
6749
6750   return x;
6751 }
6752 \f
6753 /* Given M see if it is a value that would select a field of bits
6754    within an item, but not the entire word.  Return -1 if not.
6755    Otherwise, return the starting position of the field, where 0 is the
6756    low-order bit.
6757
6758    *PLEN is set to the length of the field.  */
6759
6760 static int
6761 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
6762 {
6763   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
6764   int pos = exact_log2 (m & -m);
6765   int len;
6766
6767   if (pos < 0)
6768     return -1;
6769
6770   /* Now shift off the low-order zero bits and see if we have a power of
6771      two minus 1.  */
6772   len = exact_log2 ((m >> pos) + 1);
6773
6774   if (len <= 0)
6775     return -1;
6776
6777   *plen = len;
6778   return pos;
6779 }
6780 \f
6781 /* See if X can be simplified knowing that we will only refer to it in
6782    MODE and will only refer to those bits that are nonzero in MASK.
6783    If other bits are being computed or if masking operations are done
6784    that select a superset of the bits in MASK, they can sometimes be
6785    ignored.
6786
6787    Return a possibly simplified expression, but always convert X to
6788    MODE.  If X is a CONST_INT, AND the CONST_INT with MASK.
6789
6790    Also, if REG is nonzero and X is a register equal in value to REG,
6791    replace X with REG.
6792
6793    If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6794    are all off in X.  This is used when X will be complemented, by either
6795    NOT, NEG, or XOR.  */
6796
6797 static rtx
6798 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
6799                rtx reg, int just_select)
6800 {
6801   enum rtx_code code = GET_CODE (x);
6802   int next_select = just_select || code == XOR || code == NOT || code == NEG;
6803   enum machine_mode op_mode;
6804   unsigned HOST_WIDE_INT fuller_mask, nonzero;
6805   rtx op0, op1, temp;
6806
6807   /* If this is a CALL or ASM_OPERANDS, don't do anything.  Some of the
6808      code below will do the wrong thing since the mode of such an
6809      expression is VOIDmode.
6810
6811      Also do nothing if X is a CLOBBER; this can happen if X was
6812      the return value from a call to gen_lowpart_for_combine.  */
6813   if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6814     return x;
6815
6816   /* We want to perform the operation is its present mode unless we know
6817      that the operation is valid in MODE, in which case we do the operation
6818      in MODE.  */
6819   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6820               && have_insn_for (code, mode))
6821              ? mode : GET_MODE (x));
6822
6823   /* It is not valid to do a right-shift in a narrower mode
6824      than the one it came in with.  */
6825   if ((code == LSHIFTRT || code == ASHIFTRT)
6826       && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6827     op_mode = GET_MODE (x);
6828
6829   /* Truncate MASK to fit OP_MODE.  */
6830   if (op_mode)
6831     mask &= GET_MODE_MASK (op_mode);
6832
6833   /* When we have an arithmetic operation, or a shift whose count we
6834      do not know, we need to assume that all bit the up to the highest-order
6835      bit in MASK will be needed.  This is how we form such a mask.  */
6836   if (op_mode)
6837     fuller_mask = (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT
6838                    ? GET_MODE_MASK (op_mode)
6839                    : (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
6840                       - 1));
6841   else
6842     fuller_mask = ~(HOST_WIDE_INT) 0;
6843
6844   /* Determine what bits of X are guaranteed to be (non)zero.  */
6845   nonzero = nonzero_bits (x, mode);
6846
6847   /* If none of the bits in X are needed, return a zero.  */
6848   if (! just_select && (nonzero & mask) == 0)
6849     x = const0_rtx;
6850
6851   /* If X is a CONST_INT, return a new one.  Do this here since the
6852      test below will fail.  */
6853   if (GET_CODE (x) == CONST_INT)
6854     {
6855       if (SCALAR_INT_MODE_P (mode))
6856         return gen_int_mode (INTVAL (x) & mask, mode);
6857       else
6858         {
6859           x = GEN_INT (INTVAL (x) & mask);
6860           return gen_lowpart_common (mode, x);
6861         }
6862     }
6863
6864   /* If X is narrower than MODE and we want all the bits in X's mode, just
6865      get X in the proper mode.  */
6866   if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6867       && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
6868     return gen_lowpart_for_combine (mode, x);
6869
6870   /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
6871      MASK are already known to be zero in X, we need not do anything.  */
6872   if (GET_MODE (x) == mode && code != SUBREG && (~mask & nonzero) == 0)
6873     return x;
6874
6875   switch (code)
6876     {
6877     case CLOBBER:
6878       /* If X is a (clobber (const_int)), return it since we know we are
6879          generating something that won't match.  */
6880       return x;
6881
6882     case USE:
6883       /* X is a (use (mem ..)) that was made from a bit-field extraction that
6884          spanned the boundary of the MEM.  If we are now masking so it is
6885          within that boundary, we don't need the USE any more.  */
6886       if (! BITS_BIG_ENDIAN
6887           && (mask & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6888         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6889       break;
6890
6891     case SIGN_EXTEND:
6892     case ZERO_EXTEND:
6893     case ZERO_EXTRACT:
6894     case SIGN_EXTRACT:
6895       x = expand_compound_operation (x);
6896       if (GET_CODE (x) != code)
6897         return force_to_mode (x, mode, mask, reg, next_select);
6898       break;
6899
6900     case REG:
6901       if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6902                        || rtx_equal_p (reg, get_last_value (x))))
6903         x = reg;
6904       break;
6905
6906     case SUBREG:
6907       if (subreg_lowpart_p (x)
6908           /* We can ignore the effect of this SUBREG if it narrows the mode or
6909              if the constant masks to zero all the bits the mode doesn't
6910              have.  */
6911           && ((GET_MODE_SIZE (GET_MODE (x))
6912                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6913               || (0 == (mask
6914                         & GET_MODE_MASK (GET_MODE (x))
6915                         & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6916         return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6917       break;
6918
6919     case AND:
6920       /* If this is an AND with a constant, convert it into an AND
6921          whose constant is the AND of that constant with MASK.  If it
6922          remains an AND of MASK, delete it since it is redundant.  */
6923
6924       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6925         {
6926           x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6927                                       mask & INTVAL (XEXP (x, 1)));
6928
6929           /* If X is still an AND, see if it is an AND with a mask that
6930              is just some low-order bits.  If so, and it is MASK, we don't
6931              need it.  */
6932
6933           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6934               && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
6935                   == mask))
6936             x = XEXP (x, 0);
6937
6938           /* If it remains an AND, try making another AND with the bits
6939              in the mode mask that aren't in MASK turned on.  If the
6940              constant in the AND is wide enough, this might make a
6941              cheaper constant.  */
6942
6943           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6944               && GET_MODE_MASK (GET_MODE (x)) != mask
6945               && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6946             {
6947               HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6948                                     | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
6949               int width = GET_MODE_BITSIZE (GET_MODE (x));
6950               rtx y;
6951
6952               /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6953                  number, sign extend it.  */
6954               if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6955                   && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6956                 cval |= (HOST_WIDE_INT) -1 << width;
6957
6958               y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
6959               if (rtx_cost (y, SET) < rtx_cost (x, SET))
6960                 x = y;
6961             }
6962
6963           break;
6964         }
6965
6966       goto binop;
6967
6968     case PLUS:
6969       /* In (and (plus FOO C1) M), if M is a mask that just turns off
6970          low-order bits (as in an alignment operation) and FOO is already
6971          aligned to that boundary, mask C1 to that boundary as well.
6972          This may eliminate that PLUS and, later, the AND.  */
6973
6974       {
6975         unsigned int width = GET_MODE_BITSIZE (mode);
6976         unsigned HOST_WIDE_INT smask = mask;
6977
6978         /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
6979            number, sign extend it.  */
6980
6981         if (width < HOST_BITS_PER_WIDE_INT
6982             && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6983           smask |= (HOST_WIDE_INT) -1 << width;
6984
6985         if (GET_CODE (XEXP (x, 1)) == CONST_INT
6986             && exact_log2 (- smask) >= 0
6987             && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
6988             && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
6989           return force_to_mode (plus_constant (XEXP (x, 0),
6990                                                (INTVAL (XEXP (x, 1)) & smask)),
6991                                 mode, smask, reg, next_select);
6992       }
6993
6994       /* ... fall through ...  */
6995
6996     case MULT:
6997       /* For PLUS, MINUS and MULT, we need any bits less significant than the
6998          most significant bit in MASK since carries from those bits will
6999          affect the bits we are interested in.  */
7000       mask = fuller_mask;
7001       goto binop;
7002
7003     case MINUS:
7004       /* If X is (minus C Y) where C's least set bit is larger than any bit
7005          in the mask, then we may replace with (neg Y).  */
7006       if (GET_CODE (XEXP (x, 0)) == CONST_INT
7007           && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
7008                                         & -INTVAL (XEXP (x, 0))))
7009               > mask))
7010         {
7011           x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
7012                                   GET_MODE (x));
7013           return force_to_mode (x, mode, mask, reg, next_select);
7014         }
7015
7016       /* Similarly, if C contains every bit in the fuller_mask, then we may
7017          replace with (not Y).  */
7018       if (GET_CODE (XEXP (x, 0)) == CONST_INT
7019           && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
7020               == INTVAL (XEXP (x, 0))))
7021         {
7022           x = simplify_gen_unary (NOT, GET_MODE (x),
7023                                   XEXP (x, 1), GET_MODE (x));
7024           return force_to_mode (x, mode, mask, reg, next_select);
7025         }
7026
7027       mask = fuller_mask;
7028       goto binop;
7029
7030     case IOR:
7031     case XOR:
7032       /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
7033          LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
7034          operation which may be a bitfield extraction.  Ensure that the
7035          constant we form is not wider than the mode of X.  */
7036
7037       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7038           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7039           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7040           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7041           && GET_CODE (XEXP (x, 1)) == CONST_INT
7042           && ((INTVAL (XEXP (XEXP (x, 0), 1))
7043                + floor_log2 (INTVAL (XEXP (x, 1))))
7044               < GET_MODE_BITSIZE (GET_MODE (x)))
7045           && (INTVAL (XEXP (x, 1))
7046               & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
7047         {
7048           temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
7049                           << INTVAL (XEXP (XEXP (x, 0), 1)));
7050           temp = gen_binary (GET_CODE (x), GET_MODE (x),
7051                              XEXP (XEXP (x, 0), 0), temp);
7052           x = gen_binary (LSHIFTRT, GET_MODE (x), temp,
7053                           XEXP (XEXP (x, 0), 1));
7054           return force_to_mode (x, mode, mask, reg, next_select);
7055         }
7056
7057     binop:
7058       /* For most binary operations, just propagate into the operation and
7059          change the mode if we have an operation of that mode.  */
7060
7061       op0 = gen_lowpart_for_combine (op_mode,
7062                                      force_to_mode (XEXP (x, 0), mode, mask,
7063                                                     reg, next_select));
7064       op1 = gen_lowpart_for_combine (op_mode,
7065                                      force_to_mode (XEXP (x, 1), mode, mask,
7066                                                     reg, next_select));
7067
7068       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7069         x = gen_binary (code, op_mode, op0, op1);
7070       break;
7071
7072     case ASHIFT:
7073       /* For left shifts, do the same, but just for the first operand.
7074          However, we cannot do anything with shifts where we cannot
7075          guarantee that the counts are smaller than the size of the mode
7076          because such a count will have a different meaning in a
7077          wider mode.  */
7078
7079       if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
7080              && INTVAL (XEXP (x, 1)) >= 0
7081              && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7082           && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7083                 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7084                     < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7085         break;
7086
7087       /* If the shift count is a constant and we can do arithmetic in
7088          the mode of the shift, refine which bits we need.  Otherwise, use the
7089          conservative form of the mask.  */
7090       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7091           && INTVAL (XEXP (x, 1)) >= 0
7092           && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7093           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7094         mask >>= INTVAL (XEXP (x, 1));
7095       else
7096         mask = fuller_mask;
7097
7098       op0 = gen_lowpart_for_combine (op_mode,
7099                                      force_to_mode (XEXP (x, 0), op_mode,
7100                                                     mask, reg, next_select));
7101
7102       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7103         x = gen_binary (code, op_mode, op0, XEXP (x, 1));
7104       break;
7105
7106     case LSHIFTRT:
7107       /* Here we can only do something if the shift count is a constant,
7108          this shift constant is valid for the host, and we can do arithmetic
7109          in OP_MODE.  */
7110
7111       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7112           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7113           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7114         {
7115           rtx inner = XEXP (x, 0);
7116           unsigned HOST_WIDE_INT inner_mask;
7117
7118           /* Select the mask of the bits we need for the shift operand.  */
7119           inner_mask = mask << INTVAL (XEXP (x, 1));
7120
7121           /* We can only change the mode of the shift if we can do arithmetic
7122              in the mode of the shift and INNER_MASK is no wider than the
7123              width of OP_MODE.  */
7124           if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
7125               || (inner_mask & ~GET_MODE_MASK (op_mode)) != 0)
7126             op_mode = GET_MODE (x);
7127
7128           inner = force_to_mode (inner, op_mode, inner_mask, reg, next_select);
7129
7130           if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7131             x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7132         }
7133
7134       /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7135          shift and AND produces only copies of the sign bit (C2 is one less
7136          than a power of two), we can do this with just a shift.  */
7137
7138       if (GET_CODE (x) == LSHIFTRT
7139           && GET_CODE (XEXP (x, 1)) == CONST_INT
7140           /* The shift puts one of the sign bit copies in the least significant
7141              bit.  */
7142           && ((INTVAL (XEXP (x, 1))
7143                + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7144               >= GET_MODE_BITSIZE (GET_MODE (x)))
7145           && exact_log2 (mask + 1) >= 0
7146           /* Number of bits left after the shift must be more than the mask
7147              needs.  */
7148           && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7149               <= GET_MODE_BITSIZE (GET_MODE (x)))
7150           /* Must be more sign bit copies than the mask needs.  */
7151           && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7152               >= exact_log2 (mask + 1)))
7153         x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7154                         GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7155                                  - exact_log2 (mask + 1)));
7156
7157       goto shiftrt;
7158
7159     case ASHIFTRT:
7160       /* If we are just looking for the sign bit, we don't need this shift at
7161          all, even if it has a variable count.  */
7162       if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7163           && (mask == ((unsigned HOST_WIDE_INT) 1
7164                        << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7165         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7166
7167       /* If this is a shift by a constant, get a mask that contains those bits
7168          that are not copies of the sign bit.  We then have two cases:  If
7169          MASK only includes those bits, this can be a logical shift, which may
7170          allow simplifications.  If MASK is a single-bit field not within
7171          those bits, we are requesting a copy of the sign bit and hence can
7172          shift the sign bit to the appropriate location.  */
7173
7174       if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7175           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7176         {
7177           int i = -1;
7178
7179           /* If the considered data is wider than HOST_WIDE_INT, we can't
7180              represent a mask for all its bits in a single scalar.
7181              But we only care about the lower bits, so calculate these.  */
7182
7183           if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7184             {
7185               nonzero = ~(HOST_WIDE_INT) 0;
7186
7187               /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7188                  is the number of bits a full-width mask would have set.
7189                  We need only shift if these are fewer than nonzero can
7190                  hold.  If not, we must keep all bits set in nonzero.  */
7191
7192               if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7193                   < HOST_BITS_PER_WIDE_INT)
7194                 nonzero >>= INTVAL (XEXP (x, 1))
7195                             + HOST_BITS_PER_WIDE_INT
7196                             - GET_MODE_BITSIZE (GET_MODE (x)) ;
7197             }
7198           else
7199             {
7200               nonzero = GET_MODE_MASK (GET_MODE (x));
7201               nonzero >>= INTVAL (XEXP (x, 1));
7202             }
7203
7204           if ((mask & ~nonzero) == 0
7205               || (i = exact_log2 (mask)) >= 0)
7206             {
7207               x = simplify_shift_const
7208                 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7209                  i < 0 ? INTVAL (XEXP (x, 1))
7210                  : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7211
7212               if (GET_CODE (x) != ASHIFTRT)
7213                 return force_to_mode (x, mode, mask, reg, next_select);
7214             }
7215         }
7216
7217       /* If MASK is 1, convert this to an LSHIFTRT.  This can be done
7218          even if the shift count isn't a constant.  */
7219       if (mask == 1)
7220         x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
7221
7222     shiftrt:
7223
7224       /* If this is a zero- or sign-extension operation that just affects bits
7225          we don't care about, remove it.  Be sure the call above returned
7226          something that is still a shift.  */
7227
7228       if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7229           && GET_CODE (XEXP (x, 1)) == CONST_INT
7230           && INTVAL (XEXP (x, 1)) >= 0
7231           && (INTVAL (XEXP (x, 1))
7232               <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7233           && GET_CODE (XEXP (x, 0)) == ASHIFT
7234           && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7235         return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7236                               reg, next_select);
7237
7238       break;
7239
7240     case ROTATE:
7241     case ROTATERT:
7242       /* If the shift count is constant and we can do computations
7243          in the mode of X, compute where the bits we care about are.
7244          Otherwise, we can't do anything.  Don't change the mode of
7245          the shift or propagate MODE into the shift, though.  */
7246       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7247           && INTVAL (XEXP (x, 1)) >= 0)
7248         {
7249           temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7250                                             GET_MODE (x), GEN_INT (mask),
7251                                             XEXP (x, 1));
7252           if (temp && GET_CODE (temp) == CONST_INT)
7253             SUBST (XEXP (x, 0),
7254                    force_to_mode (XEXP (x, 0), GET_MODE (x),
7255                                   INTVAL (temp), reg, next_select));
7256         }
7257       break;
7258
7259     case NEG:
7260       /* If we just want the low-order bit, the NEG isn't needed since it
7261          won't change the low-order bit.  */
7262       if (mask == 1)
7263         return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
7264
7265       /* We need any bits less significant than the most significant bit in
7266          MASK since carries from those bits will affect the bits we are
7267          interested in.  */
7268       mask = fuller_mask;
7269       goto unop;
7270
7271     case NOT:
7272       /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7273          same as the XOR case above.  Ensure that the constant we form is not
7274          wider than the mode of X.  */
7275
7276       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7277           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7278           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7279           && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7280               < GET_MODE_BITSIZE (GET_MODE (x)))
7281           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7282         {
7283           temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7284                                GET_MODE (x));
7285           temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
7286           x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
7287
7288           return force_to_mode (x, mode, mask, reg, next_select);
7289         }
7290
7291       /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7292          use the full mask inside the NOT.  */
7293       mask = fuller_mask;
7294
7295     unop:
7296       op0 = gen_lowpart_for_combine (op_mode,
7297                                      force_to_mode (XEXP (x, 0), mode, mask,
7298                                                     reg, next_select));
7299       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7300         x = simplify_gen_unary (code, op_mode, op0, op_mode);
7301       break;
7302
7303     case NE:
7304       /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7305          in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7306          which is equal to STORE_FLAG_VALUE.  */
7307       if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7308           && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7309           && (nonzero_bits (XEXP (x, 0), mode)
7310               == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7311         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7312
7313       break;
7314
7315     case IF_THEN_ELSE:
7316       /* We have no way of knowing if the IF_THEN_ELSE can itself be
7317          written in a narrower mode.  We play it safe and do not do so.  */
7318
7319       SUBST (XEXP (x, 1),
7320              gen_lowpart_for_combine (GET_MODE (x),
7321                                       force_to_mode (XEXP (x, 1), mode,
7322                                                      mask, reg, next_select)));
7323       SUBST (XEXP (x, 2),
7324              gen_lowpart_for_combine (GET_MODE (x),
7325                                       force_to_mode (XEXP (x, 2), mode,
7326                                                      mask, reg, next_select)));
7327       break;
7328
7329     default:
7330       break;
7331     }
7332
7333   /* Ensure we return a value of the proper mode.  */
7334   return gen_lowpart_for_combine (mode, x);
7335 }
7336 \f
7337 /* Return nonzero if X is an expression that has one of two values depending on
7338    whether some other value is zero or nonzero.  In that case, we return the
7339    value that is being tested, *PTRUE is set to the value if the rtx being
7340    returned has a nonzero value, and *PFALSE is set to the other alternative.
7341
7342    If we return zero, we set *PTRUE and *PFALSE to X.  */
7343
7344 static rtx
7345 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
7346 {
7347   enum machine_mode mode = GET_MODE (x);
7348   enum rtx_code code = GET_CODE (x);
7349   rtx cond0, cond1, true0, true1, false0, false1;
7350   unsigned HOST_WIDE_INT nz;
7351
7352   /* If we are comparing a value against zero, we are done.  */
7353   if ((code == NE || code == EQ)
7354       && XEXP (x, 1) == const0_rtx)
7355     {
7356       *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7357       *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7358       return XEXP (x, 0);
7359     }
7360
7361   /* If this is a unary operation whose operand has one of two values, apply
7362      our opcode to compute those values.  */
7363   else if (GET_RTX_CLASS (code) == '1'
7364            && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7365     {
7366       *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7367       *pfalse = simplify_gen_unary (code, mode, false0,
7368                                     GET_MODE (XEXP (x, 0)));
7369       return cond0;
7370     }
7371
7372   /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7373      make can't possibly match and would suppress other optimizations.  */
7374   else if (code == COMPARE)
7375     ;
7376
7377   /* If this is a binary operation, see if either side has only one of two
7378      values.  If either one does or if both do and they are conditional on
7379      the same value, compute the new true and false values.  */
7380   else if (GET_RTX_CLASS (code) == 'c' || GET_RTX_CLASS (code) == '2'
7381            || GET_RTX_CLASS (code) == '<')
7382     {
7383       cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7384       cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7385
7386       if ((cond0 != 0 || cond1 != 0)
7387           && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7388         {
7389           /* If if_then_else_cond returned zero, then true/false are the
7390              same rtl.  We must copy one of them to prevent invalid rtl
7391              sharing.  */
7392           if (cond0 == 0)
7393             true0 = copy_rtx (true0);
7394           else if (cond1 == 0)
7395             true1 = copy_rtx (true1);
7396
7397           *ptrue = gen_binary (code, mode, true0, true1);
7398           *pfalse = gen_binary (code, mode, false0, false1);
7399           return cond0 ? cond0 : cond1;
7400         }
7401
7402       /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7403          operands is zero when the other is nonzero, and vice-versa,
7404          and STORE_FLAG_VALUE is 1 or -1.  */
7405
7406       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7407           && (code == PLUS || code == IOR || code == XOR || code == MINUS
7408               || code == UMAX)
7409           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7410         {
7411           rtx op0 = XEXP (XEXP (x, 0), 1);
7412           rtx op1 = XEXP (XEXP (x, 1), 1);
7413
7414           cond0 = XEXP (XEXP (x, 0), 0);
7415           cond1 = XEXP (XEXP (x, 1), 0);
7416
7417           if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
7418               && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
7419               && ((GET_CODE (cond0) == combine_reversed_comparison_code (cond1)
7420                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7421                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7422                   || ((swap_condition (GET_CODE (cond0))
7423                        == combine_reversed_comparison_code (cond1))
7424                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7425                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7426               && ! side_effects_p (x))
7427             {
7428               *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
7429               *pfalse = gen_binary (MULT, mode,
7430                                     (code == MINUS
7431                                      ? simplify_gen_unary (NEG, mode, op1,
7432                                                            mode)
7433                                      : op1),
7434                                     const_true_rtx);
7435               return cond0;
7436             }
7437         }
7438
7439       /* Similarly for MULT, AND and UMIN, except that for these the result
7440          is always zero.  */
7441       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7442           && (code == MULT || code == AND || code == UMIN)
7443           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7444         {
7445           cond0 = XEXP (XEXP (x, 0), 0);
7446           cond1 = XEXP (XEXP (x, 1), 0);
7447
7448           if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
7449               && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
7450               && ((GET_CODE (cond0) == combine_reversed_comparison_code (cond1)
7451                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7452                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7453                   || ((swap_condition (GET_CODE (cond0))
7454                        == combine_reversed_comparison_code (cond1))
7455                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7456                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7457               && ! side_effects_p (x))
7458             {
7459               *ptrue = *pfalse = const0_rtx;
7460               return cond0;
7461             }
7462         }
7463     }
7464
7465   else if (code == IF_THEN_ELSE)
7466     {
7467       /* If we have IF_THEN_ELSE already, extract the condition and
7468          canonicalize it if it is NE or EQ.  */
7469       cond0 = XEXP (x, 0);
7470       *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7471       if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7472         return XEXP (cond0, 0);
7473       else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7474         {
7475           *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7476           return XEXP (cond0, 0);
7477         }
7478       else
7479         return cond0;
7480     }
7481
7482   /* If X is a SUBREG, we can narrow both the true and false values
7483      if the inner expression, if there is a condition.  */
7484   else if (code == SUBREG
7485            && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7486                                                &true0, &false0)))
7487     {
7488       *ptrue = simplify_gen_subreg (mode, true0,
7489                                     GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7490       *pfalse = simplify_gen_subreg (mode, false0,
7491                                      GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7492
7493       return cond0;
7494     }
7495
7496   /* If X is a constant, this isn't special and will cause confusions
7497      if we treat it as such.  Likewise if it is equivalent to a constant.  */
7498   else if (CONSTANT_P (x)
7499            || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7500     ;
7501
7502   /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7503      will be least confusing to the rest of the compiler.  */
7504   else if (mode == BImode)
7505     {
7506       *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7507       return x;
7508     }
7509
7510   /* If X is known to be either 0 or -1, those are the true and
7511      false values when testing X.  */
7512   else if (x == constm1_rtx || x == const0_rtx
7513            || (mode != VOIDmode
7514                && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7515     {
7516       *ptrue = constm1_rtx, *pfalse = const0_rtx;
7517       return x;
7518     }
7519
7520   /* Likewise for 0 or a single bit.  */
7521   else if (mode != VOIDmode
7522            && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7523            && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7524     {
7525       *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7526       return x;
7527     }
7528
7529   /* Otherwise fail; show no condition with true and false values the same.  */
7530   *ptrue = *pfalse = x;
7531   return 0;
7532 }
7533 \f
7534 /* Return the value of expression X given the fact that condition COND
7535    is known to be true when applied to REG as its first operand and VAL
7536    as its second.  X is known to not be shared and so can be modified in
7537    place.
7538
7539    We only handle the simplest cases, and specifically those cases that
7540    arise with IF_THEN_ELSE expressions.  */
7541
7542 static rtx
7543 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
7544 {
7545   enum rtx_code code = GET_CODE (x);
7546   rtx temp;
7547   const char *fmt;
7548   int i, j;
7549
7550   if (side_effects_p (x))
7551     return x;
7552
7553   /* If either operand of the condition is a floating point value,
7554      then we have to avoid collapsing an EQ comparison.  */
7555   if (cond == EQ
7556       && rtx_equal_p (x, reg)
7557       && ! FLOAT_MODE_P (GET_MODE (x))
7558       && ! FLOAT_MODE_P (GET_MODE (val)))
7559     return val;
7560
7561   if (cond == UNEQ && rtx_equal_p (x, reg))
7562     return val;
7563
7564   /* If X is (abs REG) and we know something about REG's relationship
7565      with zero, we may be able to simplify this.  */
7566
7567   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7568     switch (cond)
7569       {
7570       case GE:  case GT:  case EQ:
7571         return XEXP (x, 0);
7572       case LT:  case LE:
7573         return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
7574                                    XEXP (x, 0),
7575                                    GET_MODE (XEXP (x, 0)));
7576       default:
7577         break;
7578       }
7579
7580   /* The only other cases we handle are MIN, MAX, and comparisons if the
7581      operands are the same as REG and VAL.  */
7582
7583   else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == 'c')
7584     {
7585       if (rtx_equal_p (XEXP (x, 0), val))
7586         cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7587
7588       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7589         {
7590           if (GET_RTX_CLASS (code) == '<')
7591             {
7592               if (comparison_dominates_p (cond, code))
7593                 return const_true_rtx;
7594
7595               code = combine_reversed_comparison_code (x);
7596               if (code != UNKNOWN
7597                   && comparison_dominates_p (cond, code))
7598                 return const0_rtx;
7599               else
7600                 return x;
7601             }
7602           else if (code == SMAX || code == SMIN
7603                    || code == UMIN || code == UMAX)
7604             {
7605               int unsignedp = (code == UMIN || code == UMAX);
7606
7607               /* Do not reverse the condition when it is NE or EQ.
7608                  This is because we cannot conclude anything about
7609                  the value of 'SMAX (x, y)' when x is not equal to y,
7610                  but we can when x equals y.  */
7611               if ((code == SMAX || code == UMAX)
7612                   && ! (cond == EQ || cond == NE))
7613                 cond = reverse_condition (cond);
7614
7615               switch (cond)
7616                 {
7617                 case GE:   case GT:
7618                   return unsignedp ? x : XEXP (x, 1);
7619                 case LE:   case LT:
7620                   return unsignedp ? x : XEXP (x, 0);
7621                 case GEU:  case GTU:
7622                   return unsignedp ? XEXP (x, 1) : x;
7623                 case LEU:  case LTU:
7624                   return unsignedp ? XEXP (x, 0) : x;
7625                 default:
7626                   break;
7627                 }
7628             }
7629         }
7630     }
7631   else if (code == SUBREG)
7632     {
7633       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
7634       rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
7635
7636       if (SUBREG_REG (x) != r)
7637         {
7638           /* We must simplify subreg here, before we lose track of the
7639              original inner_mode.  */
7640           new = simplify_subreg (GET_MODE (x), r,
7641                                  inner_mode, SUBREG_BYTE (x));
7642           if (new)
7643             return new;
7644           else
7645             SUBST (SUBREG_REG (x), r);
7646         }
7647
7648       return x;
7649     }
7650   /* We don't have to handle SIGN_EXTEND here, because even in the
7651      case of replacing something with a modeless CONST_INT, a
7652      CONST_INT is already (supposed to be) a valid sign extension for
7653      its narrower mode, which implies it's already properly
7654      sign-extended for the wider mode.  Now, for ZERO_EXTEND, the
7655      story is different.  */
7656   else if (code == ZERO_EXTEND)
7657     {
7658       enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
7659       rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
7660
7661       if (XEXP (x, 0) != r)
7662         {
7663           /* We must simplify the zero_extend here, before we lose
7664              track of the original inner_mode.  */
7665           new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
7666                                           r, inner_mode);
7667           if (new)
7668             return new;
7669           else
7670             SUBST (XEXP (x, 0), r);
7671         }
7672
7673       return x;
7674     }
7675
7676   fmt = GET_RTX_FORMAT (code);
7677   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7678     {
7679       if (fmt[i] == 'e')
7680         SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7681       else if (fmt[i] == 'E')
7682         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7683           SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7684                                                 cond, reg, val));
7685     }
7686
7687   return x;
7688 }
7689 \f
7690 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
7691    assignment as a field assignment.  */
7692
7693 static int
7694 rtx_equal_for_field_assignment_p (rtx x, rtx y)
7695 {
7696   if (x == y || rtx_equal_p (x, y))
7697     return 1;
7698
7699   if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7700     return 0;
7701
7702   /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7703      Note that all SUBREGs of MEM are paradoxical; otherwise they
7704      would have been rewritten.  */
7705   if (GET_CODE (x) == MEM && GET_CODE (y) == SUBREG
7706       && GET_CODE (SUBREG_REG (y)) == MEM
7707       && rtx_equal_p (SUBREG_REG (y),
7708                       gen_lowpart_for_combine (GET_MODE (SUBREG_REG (y)), x)))
7709     return 1;
7710
7711   if (GET_CODE (y) == MEM && GET_CODE (x) == SUBREG
7712       && GET_CODE (SUBREG_REG (x)) == MEM
7713       && rtx_equal_p (SUBREG_REG (x),
7714                       gen_lowpart_for_combine (GET_MODE (SUBREG_REG (x)), y)))
7715     return 1;
7716
7717   /* We used to see if get_last_value of X and Y were the same but that's
7718      not correct.  In one direction, we'll cause the assignment to have
7719      the wrong destination and in the case, we'll import a register into this
7720      insn that might have already have been dead.   So fail if none of the
7721      above cases are true.  */
7722   return 0;
7723 }
7724 \f
7725 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
7726    Return that assignment if so.
7727
7728    We only handle the most common cases.  */
7729
7730 static rtx
7731 make_field_assignment (rtx x)
7732 {
7733   rtx dest = SET_DEST (x);
7734   rtx src = SET_SRC (x);
7735   rtx assign;
7736   rtx rhs, lhs;
7737   HOST_WIDE_INT c1;
7738   HOST_WIDE_INT pos;
7739   unsigned HOST_WIDE_INT len;
7740   rtx other;
7741   enum machine_mode mode;
7742
7743   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7744      a clear of a one-bit field.  We will have changed it to
7745      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
7746      for a SUBREG.  */
7747
7748   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7749       && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7750       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7751       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7752     {
7753       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7754                                 1, 1, 1, 0);
7755       if (assign != 0)
7756         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7757       return x;
7758     }
7759
7760   else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7761            && subreg_lowpart_p (XEXP (src, 0))
7762            && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7763                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7764            && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7765            && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7766            && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7767     {
7768       assign = make_extraction (VOIDmode, dest, 0,
7769                                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7770                                 1, 1, 1, 0);
7771       if (assign != 0)
7772         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7773       return x;
7774     }
7775
7776   /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7777      one-bit field.  */
7778   else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7779            && XEXP (XEXP (src, 0), 0) == const1_rtx
7780            && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7781     {
7782       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7783                                 1, 1, 1, 0);
7784       if (assign != 0)
7785         return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7786       return x;
7787     }
7788
7789   /* The other case we handle is assignments into a constant-position
7790      field.  They look like (ior/xor (and DEST C1) OTHER).  If C1 represents
7791      a mask that has all one bits except for a group of zero bits and
7792      OTHER is known to have zeros where C1 has ones, this is such an
7793      assignment.  Compute the position and length from C1.  Shift OTHER
7794      to the appropriate position, force it to the required mode, and
7795      make the extraction.  Check for the AND in both operands.  */
7796
7797   if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7798     return x;
7799
7800   rhs = expand_compound_operation (XEXP (src, 0));
7801   lhs = expand_compound_operation (XEXP (src, 1));
7802
7803   if (GET_CODE (rhs) == AND
7804       && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7805       && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7806     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7807   else if (GET_CODE (lhs) == AND
7808            && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7809            && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7810     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7811   else
7812     return x;
7813
7814   pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7815   if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7816       || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7817       || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7818     return x;
7819
7820   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7821   if (assign == 0)
7822     return x;
7823
7824   /* The mode to use for the source is the mode of the assignment, or of
7825      what is inside a possible STRICT_LOW_PART.  */
7826   mode = (GET_CODE (assign) == STRICT_LOW_PART
7827           ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7828
7829   /* Shift OTHER right POS places and make it the source, restricting it
7830      to the proper length and mode.  */
7831
7832   src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
7833                                              GET_MODE (src), other, pos),
7834                        mode,
7835                        GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7836                        ? ~(unsigned HOST_WIDE_INT) 0
7837                        : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7838                        dest, 0);
7839
7840   /* If SRC is masked by an AND that does not make a difference in
7841      the value being stored, strip it.  */
7842   if (GET_CODE (assign) == ZERO_EXTRACT
7843       && GET_CODE (XEXP (assign, 1)) == CONST_INT
7844       && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
7845       && GET_CODE (src) == AND
7846       && GET_CODE (XEXP (src, 1)) == CONST_INT
7847       && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
7848           == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
7849     src = XEXP (src, 0);
7850
7851   return gen_rtx_SET (VOIDmode, assign, src);
7852 }
7853 \f
7854 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7855    if so.  */
7856
7857 static rtx
7858 apply_distributive_law (rtx x)
7859 {
7860   enum rtx_code code = GET_CODE (x);
7861   rtx lhs, rhs, other;
7862   rtx tem;
7863   enum rtx_code inner_code;
7864
7865   /* Distributivity is not true for floating point.
7866      It can change the value.  So don't do it.
7867      -- rms and moshier@world.std.com.  */
7868   if (FLOAT_MODE_P (GET_MODE (x)))
7869     return x;
7870
7871   /* The outer operation can only be one of the following:  */
7872   if (code != IOR && code != AND && code != XOR
7873       && code != PLUS && code != MINUS)
7874     return x;
7875
7876   lhs = XEXP (x, 0), rhs = XEXP (x, 1);
7877
7878   /* If either operand is a primitive we can't do anything, so get out
7879      fast.  */
7880   if (GET_RTX_CLASS (GET_CODE (lhs)) == 'o'
7881       || GET_RTX_CLASS (GET_CODE (rhs)) == 'o')
7882     return x;
7883
7884   lhs = expand_compound_operation (lhs);
7885   rhs = expand_compound_operation (rhs);
7886   inner_code = GET_CODE (lhs);
7887   if (inner_code != GET_CODE (rhs))
7888     return x;
7889
7890   /* See if the inner and outer operations distribute.  */
7891   switch (inner_code)
7892     {
7893     case LSHIFTRT:
7894     case ASHIFTRT:
7895     case AND:
7896     case IOR:
7897       /* These all distribute except over PLUS.  */
7898       if (code == PLUS || code == MINUS)
7899         return x;
7900       break;
7901
7902     case MULT:
7903       if (code != PLUS && code != MINUS)
7904         return x;
7905       break;
7906
7907     case ASHIFT:
7908       /* This is also a multiply, so it distributes over everything.  */
7909       break;
7910
7911     case SUBREG:
7912       /* Non-paradoxical SUBREGs distributes over all operations, provided
7913          the inner modes and byte offsets are the same, this is an extraction
7914          of a low-order part, we don't convert an fp operation to int or
7915          vice versa, and we would not be converting a single-word
7916          operation into a multi-word operation.  The latter test is not
7917          required, but it prevents generating unneeded multi-word operations.
7918          Some of the previous tests are redundant given the latter test, but
7919          are retained because they are required for correctness.
7920
7921          We produce the result slightly differently in this case.  */
7922
7923       if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
7924           || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
7925           || ! subreg_lowpart_p (lhs)
7926           || (GET_MODE_CLASS (GET_MODE (lhs))
7927               != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
7928           || (GET_MODE_SIZE (GET_MODE (lhs))
7929               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
7930           || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
7931         return x;
7932
7933       tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
7934                         SUBREG_REG (lhs), SUBREG_REG (rhs));
7935       return gen_lowpart_for_combine (GET_MODE (x), tem);
7936
7937     default:
7938       return x;
7939     }
7940
7941   /* Set LHS and RHS to the inner operands (A and B in the example
7942      above) and set OTHER to the common operand (C in the example).
7943      These is only one way to do this unless the inner operation is
7944      commutative.  */
7945   if (GET_RTX_CLASS (inner_code) == 'c'
7946       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
7947     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
7948   else if (GET_RTX_CLASS (inner_code) == 'c'
7949            && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
7950     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
7951   else if (GET_RTX_CLASS (inner_code) == 'c'
7952            && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
7953     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
7954   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
7955     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
7956   else
7957     return x;
7958
7959   /* Form the new inner operation, seeing if it simplifies first.  */
7960   tem = gen_binary (code, GET_MODE (x), lhs, rhs);
7961
7962   /* There is one exception to the general way of distributing:
7963      (a | c) ^ (b | c) -> (a ^ b) & ~c  */
7964   if (code == XOR && inner_code == IOR)
7965     {
7966       inner_code = AND;
7967       other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
7968     }
7969
7970   /* We may be able to continuing distributing the result, so call
7971      ourselves recursively on the inner operation before forming the
7972      outer operation, which we return.  */
7973   return gen_binary (inner_code, GET_MODE (x),
7974                      apply_distributive_law (tem), other);
7975 }
7976 \f
7977 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
7978    in MODE.
7979
7980    Return an equivalent form, if different from X.  Otherwise, return X.  If
7981    X is zero, we are to always construct the equivalent form.  */
7982
7983 static rtx
7984 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
7985                         unsigned HOST_WIDE_INT constop)
7986 {
7987   unsigned HOST_WIDE_INT nonzero;
7988   int i;
7989
7990   /* Simplify VAROP knowing that we will be only looking at some of the
7991      bits in it.
7992
7993      Note by passing in CONSTOP, we guarantee that the bits not set in
7994      CONSTOP are not significant and will never be examined.  We must
7995      ensure that is the case by explicitly masking out those bits
7996      before returning.  */
7997   varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
7998
7999   /* If VAROP is a CLOBBER, we will fail so return it.  */
8000   if (GET_CODE (varop) == CLOBBER)
8001     return varop;
8002
8003   /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
8004      to VAROP and return the new constant.  */
8005   if (GET_CODE (varop) == CONST_INT)
8006     return GEN_INT (trunc_int_for_mode (INTVAL (varop) & constop, mode));
8007
8008   /* See what bits may be nonzero in VAROP.  Unlike the general case of
8009      a call to nonzero_bits, here we don't care about bits outside
8010      MODE.  */
8011
8012   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
8013
8014   /* Turn off all bits in the constant that are known to already be zero.
8015      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
8016      which is tested below.  */
8017
8018   constop &= nonzero;
8019
8020   /* If we don't have any bits left, return zero.  */
8021   if (constop == 0)
8022     return const0_rtx;
8023
8024   /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
8025      a power of two, we can replace this with an ASHIFT.  */
8026   if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
8027       && (i = exact_log2 (constop)) >= 0)
8028     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
8029
8030   /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8031      or XOR, then try to apply the distributive law.  This may eliminate
8032      operations if either branch can be simplified because of the AND.
8033      It may also make some cases more complex, but those cases probably
8034      won't match a pattern either with or without this.  */
8035
8036   if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
8037     return
8038       gen_lowpart_for_combine
8039         (mode,
8040          apply_distributive_law
8041          (gen_binary (GET_CODE (varop), GET_MODE (varop),
8042                       simplify_and_const_int (NULL_RTX, GET_MODE (varop),
8043                                               XEXP (varop, 0), constop),
8044                       simplify_and_const_int (NULL_RTX, GET_MODE (varop),
8045                                               XEXP (varop, 1), constop))));
8046
8047   /* If VAROP is PLUS, and the constant is a mask of low bite, distribute
8048      the AND and see if one of the operands simplifies to zero.  If so, we
8049      may eliminate it.  */
8050
8051   if (GET_CODE (varop) == PLUS
8052       && exact_log2 (constop + 1) >= 0)
8053     {
8054       rtx o0, o1;
8055
8056       o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
8057       o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
8058       if (o0 == const0_rtx)
8059         return o1;
8060       if (o1 == const0_rtx)
8061         return o0;
8062     }
8063
8064   /* Get VAROP in MODE.  Try to get a SUBREG if not.  Don't make a new SUBREG
8065      if we already had one (just check for the simplest cases).  */
8066   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
8067       && GET_MODE (XEXP (x, 0)) == mode
8068       && SUBREG_REG (XEXP (x, 0)) == varop)
8069     varop = XEXP (x, 0);
8070   else
8071     varop = gen_lowpart_for_combine (mode, varop);
8072
8073   /* If we can't make the SUBREG, try to return what we were given.  */
8074   if (GET_CODE (varop) == CLOBBER)
8075     return x ? x : varop;
8076
8077   /* If we are only masking insignificant bits, return VAROP.  */
8078   if (constop == nonzero)
8079     x = varop;
8080   else
8081     {
8082       /* Otherwise, return an AND.  */
8083       constop = trunc_int_for_mode (constop, mode);
8084       /* See how much, if any, of X we can use.  */
8085       if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
8086         x = gen_binary (AND, mode, varop, GEN_INT (constop));
8087
8088       else
8089         {
8090           if (GET_CODE (XEXP (x, 1)) != CONST_INT
8091               || (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) != constop)
8092             SUBST (XEXP (x, 1), GEN_INT (constop));
8093
8094           SUBST (XEXP (x, 0), varop);
8095         }
8096     }
8097
8098   return x;
8099 }
8100 \f
8101 #define nonzero_bits_with_known(X, MODE) \
8102   cached_nonzero_bits (X, MODE, known_x, known_mode, known_ret)
8103
8104 /* The function cached_nonzero_bits is a wrapper around nonzero_bits1.
8105    It avoids exponential behavior in nonzero_bits1 when X has
8106    identical subexpressions on the first or the second level.  */
8107
8108 static unsigned HOST_WIDE_INT
8109 cached_nonzero_bits (rtx x, enum machine_mode mode, rtx known_x,
8110                      enum machine_mode known_mode,
8111                      unsigned HOST_WIDE_INT known_ret)
8112 {
8113   if (x == known_x && mode == known_mode)
8114     return known_ret;
8115
8116   /* Try to find identical subexpressions.  If found call
8117      nonzero_bits1 on X with the subexpressions as KNOWN_X and the
8118      precomputed value for the subexpression as KNOWN_RET.  */
8119
8120   if (GET_RTX_CLASS (GET_CODE (x)) == '2'
8121       || GET_RTX_CLASS (GET_CODE (x)) == 'c')
8122     {
8123       rtx x0 = XEXP (x, 0);
8124       rtx x1 = XEXP (x, 1);
8125
8126       /* Check the first level.  */
8127       if (x0 == x1)
8128         return nonzero_bits1 (x, mode, x0, mode,
8129                               nonzero_bits_with_known (x0, mode));
8130
8131       /* Check the second level.  */
8132       if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
8133            || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
8134           && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
8135         return nonzero_bits1 (x, mode, x1, mode,
8136                               nonzero_bits_with_known (x1, mode));
8137
8138       if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
8139            || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
8140           && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
8141         return nonzero_bits1 (x, mode, x0, mode,
8142                          nonzero_bits_with_known (x0, mode));
8143     }
8144
8145   return nonzero_bits1 (x, mode, known_x, known_mode, known_ret);
8146 }
8147
8148 /* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
8149    We don't let nonzero_bits recur into num_sign_bit_copies, because that
8150    is less useful.  We can't allow both, because that results in exponential
8151    run time recursion.  There is a nullstone testcase that triggered
8152    this.  This macro avoids accidental uses of num_sign_bit_copies.  */
8153 #define cached_num_sign_bit_copies()
8154
8155 /* Given an expression, X, compute which bits in X can be nonzero.
8156    We don't care about bits outside of those defined in MODE.
8157
8158    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8159    a shift, AND, or zero_extract, we can do better.  */
8160
8161 static unsigned HOST_WIDE_INT
8162 nonzero_bits1 (rtx x, enum machine_mode mode, rtx known_x,
8163                enum machine_mode known_mode,
8164                unsigned HOST_WIDE_INT known_ret)
8165 {
8166   unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
8167   unsigned HOST_WIDE_INT inner_nz;
8168   enum rtx_code code;
8169   unsigned int mode_width = GET_MODE_BITSIZE (mode);
8170   rtx tem;
8171
8172   /* For floating-point values, assume all bits are needed.  */
8173   if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
8174     return nonzero;
8175
8176   /* If X is wider than MODE, use its mode instead.  */
8177   if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
8178     {
8179       mode = GET_MODE (x);
8180       nonzero = GET_MODE_MASK (mode);
8181       mode_width = GET_MODE_BITSIZE (mode);
8182     }
8183
8184   if (mode_width > HOST_BITS_PER_WIDE_INT)
8185     /* Our only callers in this case look for single bit values.  So
8186        just return the mode mask.  Those tests will then be false.  */
8187     return nonzero;
8188
8189 #ifndef WORD_REGISTER_OPERATIONS
8190   /* If MODE is wider than X, but both are a single word for both the host
8191      and target machines, we can compute this from which bits of the
8192      object might be nonzero in its own mode, taking into account the fact
8193      that on many CISC machines, accessing an object in a wider mode
8194      causes the high-order bits to become undefined.  So they are
8195      not known to be zero.  */
8196
8197   if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
8198       && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
8199       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
8200       && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
8201     {
8202       nonzero &= nonzero_bits_with_known (x, GET_MODE (x));
8203       nonzero |= GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x));
8204       return nonzero;
8205     }
8206 #endif
8207
8208   code = GET_CODE (x);
8209   switch (code)
8210     {
8211     case REG:
8212 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
8213       /* If pointers extend unsigned and this is a pointer in Pmode, say that
8214          all the bits above ptr_mode are known to be zero.  */
8215       if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
8216           && REG_POINTER (x))
8217         nonzero &= GET_MODE_MASK (ptr_mode);
8218 #endif
8219
8220       /* Include declared information about alignment of pointers.  */
8221       /* ??? We don't properly preserve REG_POINTER changes across
8222          pointer-to-integer casts, so we can't trust it except for
8223          things that we know must be pointers.  See execute/960116-1.c.  */
8224       if ((x == stack_pointer_rtx
8225            || x == frame_pointer_rtx
8226            || x == arg_pointer_rtx)
8227           && REGNO_POINTER_ALIGN (REGNO (x)))
8228         {
8229           unsigned HOST_WIDE_INT alignment
8230             = REGNO_POINTER_ALIGN (REGNO (x)) / BITS_PER_UNIT;
8231
8232 #ifdef PUSH_ROUNDING
8233           /* If PUSH_ROUNDING is defined, it is possible for the
8234              stack to be momentarily aligned only to that amount,
8235              so we pick the least alignment.  */
8236           if (x == stack_pointer_rtx && PUSH_ARGS)
8237             alignment = MIN ((unsigned HOST_WIDE_INT) PUSH_ROUNDING (1),
8238                              alignment);
8239 #endif
8240
8241           nonzero &= ~(alignment - 1);
8242         }
8243
8244       /* If X is a register whose nonzero bits value is current, use it.
8245          Otherwise, if X is a register whose value we can find, use that
8246          value.  Otherwise, use the previously-computed global nonzero bits
8247          for this register.  */
8248
8249       if (reg_last_set_value[REGNO (x)] != 0
8250           && (reg_last_set_mode[REGNO (x)] == mode
8251               || (GET_MODE_CLASS (reg_last_set_mode[REGNO (x)]) == MODE_INT
8252                   && GET_MODE_CLASS (mode) == MODE_INT))
8253           && (reg_last_set_label[REGNO (x)] == label_tick
8254               || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8255                   && REG_N_SETS (REGNO (x)) == 1
8256                   && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8257                                         REGNO (x))))
8258           && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
8259         return reg_last_set_nonzero_bits[REGNO (x)] & nonzero;
8260
8261       tem = get_last_value (x);
8262
8263       if (tem)
8264         {
8265 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8266           /* If X is narrower than MODE and TEM is a non-negative
8267              constant that would appear negative in the mode of X,
8268              sign-extend it for use in reg_nonzero_bits because some
8269              machines (maybe most) will actually do the sign-extension
8270              and this is the conservative approach.
8271
8272              ??? For 2.5, try to tighten up the MD files in this regard
8273              instead of this kludge.  */
8274
8275           if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
8276               && GET_CODE (tem) == CONST_INT
8277               && INTVAL (tem) > 0
8278               && 0 != (INTVAL (tem)
8279                        & ((HOST_WIDE_INT) 1
8280                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8281             tem = GEN_INT (INTVAL (tem)
8282                            | ((HOST_WIDE_INT) (-1)
8283                               << GET_MODE_BITSIZE (GET_MODE (x))));
8284 #endif
8285           return nonzero_bits_with_known (tem, mode) & nonzero;
8286         }
8287       else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
8288         {
8289           unsigned HOST_WIDE_INT mask = reg_nonzero_bits[REGNO (x)];
8290
8291           if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width)
8292             /* We don't know anything about the upper bits.  */
8293             mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8294           return nonzero & mask;
8295         }
8296       else
8297         return nonzero;
8298
8299     case CONST_INT:
8300 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8301       /* If X is negative in MODE, sign-extend the value.  */
8302       if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
8303           && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
8304         return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
8305 #endif
8306
8307       return INTVAL (x);
8308
8309     case MEM:
8310 #ifdef LOAD_EXTEND_OP
8311       /* In many, if not most, RISC machines, reading a byte from memory
8312          zeros the rest of the register.  Noticing that fact saves a lot
8313          of extra zero-extends.  */
8314       if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
8315         nonzero &= GET_MODE_MASK (GET_MODE (x));
8316 #endif
8317       break;
8318
8319     case EQ:  case NE:
8320     case UNEQ:  case LTGT:
8321     case GT:  case GTU:  case UNGT:
8322     case LT:  case LTU:  case UNLT:
8323     case GE:  case GEU:  case UNGE:
8324     case LE:  case LEU:  case UNLE:
8325     case UNORDERED: case ORDERED:
8326
8327       /* If this produces an integer result, we know which bits are set.
8328          Code here used to clear bits outside the mode of X, but that is
8329          now done above.  */
8330
8331       if (GET_MODE_CLASS (mode) == MODE_INT
8332           && mode_width <= HOST_BITS_PER_WIDE_INT)
8333         nonzero = STORE_FLAG_VALUE;
8334       break;
8335
8336     case NEG:
8337 #if 0
8338       /* Disabled to avoid exponential mutual recursion between nonzero_bits
8339          and num_sign_bit_copies.  */
8340       if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
8341           == GET_MODE_BITSIZE (GET_MODE (x)))
8342         nonzero = 1;
8343 #endif
8344
8345       if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
8346         nonzero |= (GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x)));
8347       break;
8348
8349     case ABS:
8350 #if 0
8351       /* Disabled to avoid exponential mutual recursion between nonzero_bits
8352          and num_sign_bit_copies.  */
8353       if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
8354           == GET_MODE_BITSIZE (GET_MODE (x)))
8355         nonzero = 1;
8356 #endif
8357       break;
8358
8359     case TRUNCATE:
8360       nonzero &= (nonzero_bits_with_known (XEXP (x, 0), mode)
8361                   & GET_MODE_MASK (mode));
8362       break;
8363
8364     case ZERO_EXTEND:
8365       nonzero &= nonzero_bits_with_known (XEXP (x, 0), mode);
8366       if (GET_MODE (XEXP (x, 0)) != VOIDmode)
8367         nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
8368       break;
8369
8370     case SIGN_EXTEND:
8371       /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
8372          Otherwise, show all the bits in the outer mode but not the inner
8373          may be nonzero.  */
8374       inner_nz = nonzero_bits_with_known (XEXP (x, 0), mode);
8375       if (GET_MODE (XEXP (x, 0)) != VOIDmode)
8376         {
8377           inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
8378           if (inner_nz
8379               & (((HOST_WIDE_INT) 1
8380                   << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
8381             inner_nz |= (GET_MODE_MASK (mode)
8382                          & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
8383         }
8384
8385       nonzero &= inner_nz;
8386       break;
8387
8388     case AND:
8389       nonzero &= (nonzero_bits_with_known (XEXP (x, 0), mode)
8390                   & nonzero_bits_with_known (XEXP (x, 1), mode));
8391       break;
8392
8393     case XOR:   case IOR:
8394     case UMIN:  case UMAX:  case SMIN:  case SMAX:
8395       {
8396         unsigned HOST_WIDE_INT nonzero0 =
8397           nonzero_bits_with_known (XEXP (x, 0), mode);
8398
8399         /* Don't call nonzero_bits for the second time if it cannot change
8400            anything.  */
8401         if ((nonzero & nonzero0) != nonzero)
8402           nonzero &= (nonzero0
8403                       | nonzero_bits_with_known (XEXP (x, 1), mode));
8404       }
8405       break;
8406
8407     case PLUS:  case MINUS:
8408     case MULT:
8409     case DIV:   case UDIV:
8410     case MOD:   case UMOD:
8411       /* We can apply the rules of arithmetic to compute the number of
8412          high- and low-order zero bits of these operations.  We start by
8413          computing the width (position of the highest-order nonzero bit)
8414          and the number of low-order zero bits for each value.  */
8415       {
8416         unsigned HOST_WIDE_INT nz0 =
8417           nonzero_bits_with_known (XEXP (x, 0), mode);
8418         unsigned HOST_WIDE_INT nz1 =
8419           nonzero_bits_with_known (XEXP (x, 1), mode);
8420         int sign_index = GET_MODE_BITSIZE (GET_MODE (x)) - 1;
8421         int width0 = floor_log2 (nz0) + 1;
8422         int width1 = floor_log2 (nz1) + 1;
8423         int low0 = floor_log2 (nz0 & -nz0);
8424         int low1 = floor_log2 (nz1 & -nz1);
8425         HOST_WIDE_INT op0_maybe_minusp
8426           = (nz0 & ((HOST_WIDE_INT) 1 << sign_index));
8427         HOST_WIDE_INT op1_maybe_minusp
8428           = (nz1 & ((HOST_WIDE_INT) 1 << sign_index));
8429         unsigned int result_width = mode_width;
8430         int result_low = 0;
8431
8432         switch (code)
8433           {
8434           case PLUS:
8435             result_width = MAX (width0, width1) + 1;
8436             result_low = MIN (low0, low1);
8437             break;
8438           case MINUS:
8439             result_low = MIN (low0, low1);
8440             break;
8441           case MULT:
8442             result_width = width0 + width1;
8443             result_low = low0 + low1;
8444             break;
8445           case DIV:
8446             if (width1 == 0)
8447               break;
8448             if (! op0_maybe_minusp && ! op1_maybe_minusp)
8449               result_width = width0;
8450             break;
8451           case UDIV:
8452             if (width1 == 0)
8453               break;
8454             result_width = width0;
8455             break;
8456           case MOD:
8457             if (width1 == 0)
8458               break;
8459             if (! op0_maybe_minusp && ! op1_maybe_minusp)
8460               result_width = MIN (width0, width1);
8461             result_low = MIN (low0, low1);
8462             break;
8463           case UMOD:
8464             if (width1 == 0)
8465               break;
8466             result_width = MIN (width0, width1);
8467             result_low = MIN (low0, low1);
8468             break;
8469           default:
8470             abort ();
8471           }
8472
8473         if (result_width < mode_width)
8474           nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
8475
8476         if (result_low > 0)
8477           nonzero &= ~(((HOST_WIDE_INT) 1 << result_low) - 1);
8478
8479 #ifdef POINTERS_EXTEND_UNSIGNED
8480         /* If pointers extend unsigned and this is an addition or subtraction
8481            to a pointer in Pmode, all the bits above ptr_mode are known to be
8482            zero.  */
8483         if (POINTERS_EXTEND_UNSIGNED > 0 && GET_MODE (x) == Pmode
8484             && (code == PLUS || code == MINUS)
8485             && GET_CODE (XEXP (x, 0)) == REG && REG_POINTER (XEXP (x, 0)))
8486           nonzero &= GET_MODE_MASK (ptr_mode);
8487 #endif
8488       }
8489       break;
8490
8491     case ZERO_EXTRACT:
8492       if (GET_CODE (XEXP (x, 1)) == CONST_INT
8493           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8494         nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
8495       break;
8496
8497     case SUBREG:
8498       /* If this is a SUBREG formed for a promoted variable that has
8499          been zero-extended, we know that at least the high-order bits
8500          are zero, though others might be too.  */
8501
8502       if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x) > 0)
8503         nonzero = (GET_MODE_MASK (GET_MODE (x))
8504                    & nonzero_bits_with_known (SUBREG_REG (x), GET_MODE (x)));
8505
8506       /* If the inner mode is a single word for both the host and target
8507          machines, we can compute this from which bits of the inner
8508          object might be nonzero.  */
8509       if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
8510           && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8511               <= HOST_BITS_PER_WIDE_INT))
8512         {
8513           nonzero &= nonzero_bits_with_known (SUBREG_REG (x), mode);
8514
8515 #if defined (WORD_REGISTER_OPERATIONS) && defined (LOAD_EXTEND_OP)
8516           /* If this is a typical RISC machine, we only have to worry
8517              about the way loads are extended.  */
8518           if ((LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
8519                ? (((nonzero
8520                     & (((unsigned HOST_WIDE_INT) 1
8521                         << (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) - 1))))
8522                    != 0))
8523                : LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) != ZERO_EXTEND)
8524               || GET_CODE (SUBREG_REG (x)) != MEM)
8525 #endif
8526             {
8527               /* On many CISC machines, accessing an object in a wider mode
8528                  causes the high-order bits to become undefined.  So they are
8529                  not known to be zero.  */
8530               if (GET_MODE_SIZE (GET_MODE (x))
8531                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8532                 nonzero |= (GET_MODE_MASK (GET_MODE (x))
8533                             & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
8534             }
8535         }
8536       break;
8537
8538     case ASHIFTRT:
8539     case LSHIFTRT:
8540     case ASHIFT:
8541     case ROTATE:
8542       /* The nonzero bits are in two classes: any bits within MODE
8543          that aren't in GET_MODE (x) are always significant.  The rest of the
8544          nonzero bits are those that are significant in the operand of
8545          the shift when shifted the appropriate number of bits.  This
8546          shows that high-order bits are cleared by the right shift and
8547          low-order bits by left shifts.  */
8548       if (GET_CODE (XEXP (x, 1)) == CONST_INT
8549           && INTVAL (XEXP (x, 1)) >= 0
8550           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8551         {
8552           enum machine_mode inner_mode = GET_MODE (x);
8553           unsigned int width = GET_MODE_BITSIZE (inner_mode);
8554           int count = INTVAL (XEXP (x, 1));
8555           unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
8556           unsigned HOST_WIDE_INT op_nonzero =
8557             nonzero_bits_with_known (XEXP (x, 0), mode);
8558           unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
8559           unsigned HOST_WIDE_INT outer = 0;
8560
8561           if (mode_width > width)
8562             outer = (op_nonzero & nonzero & ~mode_mask);
8563
8564           if (code == LSHIFTRT)
8565             inner >>= count;
8566           else if (code == ASHIFTRT)
8567             {
8568               inner >>= count;
8569
8570               /* If the sign bit may have been nonzero before the shift, we
8571                  need to mark all the places it could have been copied to
8572                  by the shift as possibly nonzero.  */
8573               if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
8574                 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
8575             }
8576           else if (code == ASHIFT)
8577             inner <<= count;
8578           else
8579             inner = ((inner << (count % width)
8580                       | (inner >> (width - (count % width)))) & mode_mask);
8581
8582           nonzero &= (outer | inner);
8583         }
8584       break;
8585
8586     case FFS:
8587     case POPCOUNT:
8588       /* This is at most the number of bits in the mode.  */
8589       nonzero = ((HOST_WIDE_INT) 2 << (floor_log2 (mode_width))) - 1;
8590       break;
8591
8592     case CLZ:
8593       /* If CLZ has a known value at zero, then the nonzero bits are
8594          that value, plus the number of bits in the mode minus one.  */
8595       if (CLZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
8596         nonzero |= ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width))) - 1;
8597       else
8598         nonzero = -1;
8599       break;
8600
8601     case CTZ:
8602       /* If CTZ has a known value at zero, then the nonzero bits are
8603          that value, plus the number of bits in the mode minus one.  */
8604       if (CTZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
8605         nonzero |= ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width))) - 1;
8606       else
8607         nonzero = -1;
8608       break;
8609
8610     case PARITY:
8611       nonzero = 1;
8612       break;
8613
8614     case IF_THEN_ELSE:
8615       nonzero &= (nonzero_bits_with_known (XEXP (x, 1), mode)
8616                   | nonzero_bits_with_known (XEXP (x, 2), mode));
8617       break;
8618
8619     default:
8620       break;
8621     }
8622
8623   return nonzero;
8624 }
8625
8626 /* See the macro definition above.  */
8627 #undef cached_num_sign_bit_copies
8628 \f
8629 #define num_sign_bit_copies_with_known(X, M) \
8630   cached_num_sign_bit_copies (X, M, known_x, known_mode, known_ret)
8631
8632 /* The function cached_num_sign_bit_copies is a wrapper around
8633    num_sign_bit_copies1.  It avoids exponential behavior in
8634    num_sign_bit_copies1 when X has identical subexpressions on the
8635    first or the second level.  */
8636
8637 static unsigned int
8638 cached_num_sign_bit_copies (rtx x, enum machine_mode mode, rtx known_x,
8639                             enum machine_mode known_mode,
8640                             unsigned int known_ret)
8641 {
8642   if (x == known_x && mode == known_mode)
8643     return known_ret;
8644
8645   /* Try to find identical subexpressions.  If found call
8646      num_sign_bit_copies1 on X with the subexpressions as KNOWN_X and
8647      the precomputed value for the subexpression as KNOWN_RET.  */
8648
8649   if (GET_RTX_CLASS (GET_CODE (x)) == '2'
8650       || GET_RTX_CLASS (GET_CODE (x)) == 'c')
8651     {
8652       rtx x0 = XEXP (x, 0);
8653       rtx x1 = XEXP (x, 1);
8654
8655       /* Check the first level.  */
8656       if (x0 == x1)
8657         return
8658           num_sign_bit_copies1 (x, mode, x0, mode,
8659                                 num_sign_bit_copies_with_known (x0, mode));
8660
8661       /* Check the second level.  */
8662       if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
8663            || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
8664           && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
8665         return
8666           num_sign_bit_copies1 (x, mode, x1, mode,
8667                                 num_sign_bit_copies_with_known (x1, mode));
8668
8669       if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
8670            || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
8671           && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
8672         return
8673           num_sign_bit_copies1 (x, mode, x0, mode,
8674                                 num_sign_bit_copies_with_known (x0, mode));
8675     }
8676
8677   return num_sign_bit_copies1 (x, mode, known_x, known_mode, known_ret);
8678 }
8679
8680 /* Return the number of bits at the high-order end of X that are known to
8681    be equal to the sign bit.  X will be used in mode MODE; if MODE is
8682    VOIDmode, X will be used in its own mode.  The returned value  will always
8683    be between 1 and the number of bits in MODE.  */
8684
8685 static unsigned int
8686 num_sign_bit_copies1 (rtx x, enum machine_mode mode, rtx known_x,
8687                       enum machine_mode known_mode,
8688                       unsigned int known_ret)
8689 {
8690   enum rtx_code code = GET_CODE (x);
8691   unsigned int bitwidth;
8692   int num0, num1, result;
8693   unsigned HOST_WIDE_INT nonzero;
8694   rtx tem;
8695
8696   /* If we weren't given a mode, use the mode of X.  If the mode is still
8697      VOIDmode, we don't know anything.  Likewise if one of the modes is
8698      floating-point.  */
8699
8700   if (mode == VOIDmode)
8701     mode = GET_MODE (x);
8702
8703   if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
8704     return 1;
8705
8706   bitwidth = GET_MODE_BITSIZE (mode);
8707
8708   /* For a smaller object, just ignore the high bits.  */
8709   if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
8710     {
8711       num0 = num_sign_bit_copies_with_known (x, GET_MODE (x));
8712       return MAX (1,
8713                   num0 - (int) (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth));
8714     }
8715
8716   if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
8717     {
8718 #ifndef WORD_REGISTER_OPERATIONS
8719   /* If this machine does not do all register operations on the entire
8720      register and MODE is wider than the mode of X, we can say nothing
8721      at all about the high-order bits.  */
8722       return 1;
8723 #else
8724       /* Likewise on machines that do, if the mode of the object is smaller
8725          than a word and loads of that size don't sign extend, we can say
8726          nothing about the high order bits.  */
8727       if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
8728 #ifdef LOAD_EXTEND_OP
8729           && LOAD_EXTEND_OP (GET_MODE (x)) != SIGN_EXTEND
8730 #endif
8731           )
8732         return 1;
8733 #endif
8734     }
8735
8736   switch (code)
8737     {
8738     case REG:
8739
8740 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
8741       /* If pointers extend signed and this is a pointer in Pmode, say that
8742          all the bits above ptr_mode are known to be sign bit copies.  */
8743       if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
8744           && REG_POINTER (x))
8745         return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
8746 #endif
8747
8748       if (reg_last_set_value[REGNO (x)] != 0
8749           && reg_last_set_mode[REGNO (x)] == mode
8750           && (reg_last_set_label[REGNO (x)] == label_tick
8751               || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8752                   && REG_N_SETS (REGNO (x)) == 1
8753                   && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8754                                         REGNO (x))))
8755           && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
8756         return reg_last_set_sign_bit_copies[REGNO (x)];
8757
8758       tem = get_last_value (x);
8759       if (tem != 0)
8760         return num_sign_bit_copies_with_known (tem, mode);
8761
8762       if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0
8763           && GET_MODE_BITSIZE (GET_MODE (x)) == bitwidth)
8764         return reg_sign_bit_copies[REGNO (x)];
8765       break;
8766
8767     case MEM:
8768 #ifdef LOAD_EXTEND_OP
8769       /* Some RISC machines sign-extend all loads of smaller than a word.  */
8770       if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
8771         return MAX (1, ((int) bitwidth
8772                         - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1));
8773 #endif
8774       break;
8775
8776     case CONST_INT:
8777       /* If the constant is negative, take its 1's complement and remask.
8778          Then see how many zero bits we have.  */
8779       nonzero = INTVAL (x) & GET_MODE_MASK (mode);
8780       if (bitwidth <= HOST_BITS_PER_WIDE_INT
8781           && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8782         nonzero = (~nonzero) & GET_MODE_MASK (mode);
8783
8784       return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
8785
8786     case SUBREG:
8787       /* If this is a SUBREG for a promoted object that is sign-extended
8788          and we are looking at it in a wider mode, we know that at least the
8789          high-order bits are known to be sign bit copies.  */
8790
8791       if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
8792         {
8793           num0 = num_sign_bit_copies_with_known (SUBREG_REG (x), mode);
8794           return MAX ((int) bitwidth
8795                       - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1,
8796                       num0);
8797         }
8798
8799       /* For a smaller object, just ignore the high bits.  */
8800       if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
8801         {
8802           num0 = num_sign_bit_copies_with_known (SUBREG_REG (x), VOIDmode);
8803           return MAX (1, (num0
8804                           - (int) (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8805                                    - bitwidth)));
8806         }
8807
8808 #ifdef WORD_REGISTER_OPERATIONS
8809 #ifdef LOAD_EXTEND_OP
8810       /* For paradoxical SUBREGs on machines where all register operations
8811          affect the entire register, just look inside.  Note that we are
8812          passing MODE to the recursive call, so the number of sign bit copies
8813          will remain relative to that mode, not the inner mode.  */
8814
8815       /* This works only if loads sign extend.  Otherwise, if we get a
8816          reload for the inner part, it may be loaded from the stack, and
8817          then we lose all sign bit copies that existed before the store
8818          to the stack.  */
8819
8820       if ((GET_MODE_SIZE (GET_MODE (x))
8821            > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8822           && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
8823           && GET_CODE (SUBREG_REG (x)) == MEM)
8824         return num_sign_bit_copies_with_known (SUBREG_REG (x), mode);
8825 #endif
8826 #endif
8827       break;
8828
8829     case SIGN_EXTRACT:
8830       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
8831         return MAX (1, (int) bitwidth - INTVAL (XEXP (x, 1)));
8832       break;
8833
8834     case SIGN_EXTEND:
8835       return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8836               + num_sign_bit_copies_with_known (XEXP (x, 0), VOIDmode));
8837
8838     case TRUNCATE:
8839       /* For a smaller object, just ignore the high bits.  */
8840       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), VOIDmode);
8841       return MAX (1, (num0 - (int) (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8842                                     - bitwidth)));
8843
8844     case NOT:
8845       return num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8846
8847     case ROTATE:       case ROTATERT:
8848       /* If we are rotating left by a number of bits less than the number
8849          of sign bit copies, we can just subtract that amount from the
8850          number.  */
8851       if (GET_CODE (XEXP (x, 1)) == CONST_INT
8852           && INTVAL (XEXP (x, 1)) >= 0
8853           && INTVAL (XEXP (x, 1)) < (int) bitwidth)
8854         {
8855           num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8856           return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
8857                                  : (int) bitwidth - INTVAL (XEXP (x, 1))));
8858         }
8859       break;
8860
8861     case NEG:
8862       /* In general, this subtracts one sign bit copy.  But if the value
8863          is known to be positive, the number of sign bit copies is the
8864          same as that of the input.  Finally, if the input has just one bit
8865          that might be nonzero, all the bits are copies of the sign bit.  */
8866       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8867       if (bitwidth > HOST_BITS_PER_WIDE_INT)
8868         return num0 > 1 ? num0 - 1 : 1;
8869
8870       nonzero = nonzero_bits (XEXP (x, 0), mode);
8871       if (nonzero == 1)
8872         return bitwidth;
8873
8874       if (num0 > 1
8875           && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
8876         num0--;
8877
8878       return num0;
8879
8880     case IOR:   case AND:   case XOR:
8881     case SMIN:  case SMAX:  case UMIN:  case UMAX:
8882       /* Logical operations will preserve the number of sign-bit copies.
8883          MIN and MAX operations always return one of the operands.  */
8884       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8885       num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8886       return MIN (num0, num1);
8887
8888     case PLUS:  case MINUS:
8889       /* For addition and subtraction, we can have a 1-bit carry.  However,
8890          if we are subtracting 1 from a positive number, there will not
8891          be such a carry.  Furthermore, if the positive number is known to
8892          be 0 or 1, we know the result is either -1 or 0.  */
8893
8894       if (code == PLUS && XEXP (x, 1) == constm1_rtx
8895           && bitwidth <= HOST_BITS_PER_WIDE_INT)
8896         {
8897           nonzero = nonzero_bits (XEXP (x, 0), mode);
8898           if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
8899             return (nonzero == 1 || nonzero == 0 ? bitwidth
8900                     : bitwidth - floor_log2 (nonzero) - 1);
8901         }
8902
8903       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8904       num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8905       result = MAX (1, MIN (num0, num1) - 1);
8906
8907 #ifdef POINTERS_EXTEND_UNSIGNED
8908       /* If pointers extend signed and this is an addition or subtraction
8909          to a pointer in Pmode, all the bits above ptr_mode are known to be
8910          sign bit copies.  */
8911       if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
8912           && (code == PLUS || code == MINUS)
8913           && GET_CODE (XEXP (x, 0)) == REG && REG_POINTER (XEXP (x, 0)))
8914         result = MAX ((int) (GET_MODE_BITSIZE (Pmode)
8915                              - GET_MODE_BITSIZE (ptr_mode) + 1),
8916                       result);
8917 #endif
8918       return result;
8919
8920     case MULT:
8921       /* The number of bits of the product is the sum of the number of
8922          bits of both terms.  However, unless one of the terms if known
8923          to be positive, we must allow for an additional bit since negating
8924          a negative number can remove one sign bit copy.  */
8925
8926       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8927       num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8928
8929       result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
8930       if (result > 0
8931           && (bitwidth > HOST_BITS_PER_WIDE_INT
8932               || (((nonzero_bits (XEXP (x, 0), mode)
8933                     & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8934                   && ((nonzero_bits (XEXP (x, 1), mode)
8935                        & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))))
8936         result--;
8937
8938       return MAX (1, result);
8939
8940     case UDIV:
8941       /* The result must be <= the first operand.  If the first operand
8942          has the high bit set, we know nothing about the number of sign
8943          bit copies.  */
8944       if (bitwidth > HOST_BITS_PER_WIDE_INT)
8945         return 1;
8946       else if ((nonzero_bits (XEXP (x, 0), mode)
8947                 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8948         return 1;
8949       else
8950         return num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8951
8952     case UMOD:
8953       /* The result must be <= the second operand.  */
8954       return num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8955
8956     case DIV:
8957       /* Similar to unsigned division, except that we have to worry about
8958          the case where the divisor is negative, in which case we have
8959          to add 1.  */
8960       result = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8961       if (result > 1
8962           && (bitwidth > HOST_BITS_PER_WIDE_INT
8963               || (nonzero_bits (XEXP (x, 1), mode)
8964                   & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8965         result--;
8966
8967       return result;
8968
8969     case MOD:
8970       result = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8971       if (result > 1
8972           && (bitwidth > HOST_BITS_PER_WIDE_INT
8973               || (nonzero_bits (XEXP (x, 1), mode)
8974                   & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8975         result--;
8976
8977       return result;
8978
8979     case ASHIFTRT:
8980       /* Shifts by a constant add to the number of bits equal to the
8981          sign bit.  */
8982       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8983       if (GET_CODE (XEXP (x, 1)) == CONST_INT
8984           && INTVAL (XEXP (x, 1)) > 0)
8985         num0 = MIN ((int) bitwidth, num0 + INTVAL (XEXP (x, 1)));
8986
8987       return num0;
8988
8989     case ASHIFT:
8990       /* Left shifts destroy copies.  */
8991       if (GET_CODE (XEXP (x, 1)) != CONST_INT
8992           || INTVAL (XEXP (x, 1)) < 0
8993           || INTVAL (XEXP (x, 1)) >= (int) bitwidth)
8994         return 1;
8995
8996       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8997       return MAX (1, num0 - INTVAL (XEXP (x, 1)));
8998
8999     case IF_THEN_ELSE:
9000       num0 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
9001       num1 = num_sign_bit_copies_with_known (XEXP (x, 2), mode);
9002       return MIN (num0, num1);
9003
9004     case EQ:  case NE:  case GE:  case GT:  case LE:  case LT:
9005     case UNEQ:  case LTGT:  case UNGE:  case UNGT:  case UNLE:  case UNLT:
9006     case GEU: case GTU: case LEU: case LTU:
9007     case UNORDERED: case ORDERED:
9008       /* If the constant is negative, take its 1's complement and remask.
9009          Then see how many zero bits we have.  */
9010       nonzero = STORE_FLAG_VALUE;
9011       if (bitwidth <= HOST_BITS_PER_WIDE_INT
9012           && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
9013         nonzero = (~nonzero) & GET_MODE_MASK (mode);
9014
9015       return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
9016       break;
9017
9018     default:
9019       break;
9020     }
9021
9022   /* If we haven't been able to figure it out by one of the above rules,
9023      see if some of the high-order bits are known to be zero.  If so,
9024      count those bits and return one less than that amount.  If we can't
9025      safely compute the mask for this mode, always return BITWIDTH.  */
9026
9027   if (bitwidth > HOST_BITS_PER_WIDE_INT)
9028     return 1;
9029
9030   nonzero = nonzero_bits (x, mode);
9031   return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
9032           ? 1 : bitwidth - floor_log2 (nonzero) - 1);
9033 }
9034 \f
9035 /* Return the number of "extended" bits there are in X, when interpreted
9036    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
9037    unsigned quantities, this is the number of high-order zero bits.
9038    For signed quantities, this is the number of copies of the sign bit
9039    minus 1.  In both case, this function returns the number of "spare"
9040    bits.  For example, if two quantities for which this function returns
9041    at least 1 are added, the addition is known not to overflow.
9042
9043    This function will always return 0 unless called during combine, which
9044    implies that it must be called from a define_split.  */
9045
9046 unsigned int
9047 extended_count (rtx x, enum machine_mode mode, int unsignedp)
9048 {
9049   if (nonzero_sign_valid == 0)
9050     return 0;
9051
9052   return (unsignedp
9053           ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9054              ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
9055                                - floor_log2 (nonzero_bits (x, mode)))
9056              : 0)
9057           : num_sign_bit_copies (x, mode) - 1);
9058 }
9059 \f
9060 /* This function is called from `simplify_shift_const' to merge two
9061    outer operations.  Specifically, we have already found that we need
9062    to perform operation *POP0 with constant *PCONST0 at the outermost
9063    position.  We would now like to also perform OP1 with constant CONST1
9064    (with *POP0 being done last).
9065
9066    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9067    the resulting operation.  *PCOMP_P is set to 1 if we would need to
9068    complement the innermost operand, otherwise it is unchanged.
9069
9070    MODE is the mode in which the operation will be done.  No bits outside
9071    the width of this mode matter.  It is assumed that the width of this mode
9072    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9073
9074    If *POP0 or OP1 are NIL, it means no operation is required.  Only NEG, PLUS,
9075    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
9076    result is simply *PCONST0.
9077
9078    If the resulting operation cannot be expressed as one operation, we
9079    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
9080
9081 static int
9082 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)
9083 {
9084   enum rtx_code op0 = *pop0;
9085   HOST_WIDE_INT const0 = *pconst0;
9086
9087   const0 &= GET_MODE_MASK (mode);
9088   const1 &= GET_MODE_MASK (mode);
9089
9090   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
9091   if (op0 == AND)
9092     const1 &= const0;
9093
9094   /* If OP0 or OP1 is NIL, this is easy.  Similarly if they are the same or
9095      if OP0 is SET.  */
9096
9097   if (op1 == NIL || op0 == SET)
9098     return 1;
9099
9100   else if (op0 == NIL)
9101     op0 = op1, const0 = const1;
9102
9103   else if (op0 == op1)
9104     {
9105       switch (op0)
9106         {
9107         case AND:
9108           const0 &= const1;
9109           break;
9110         case IOR:
9111           const0 |= const1;
9112           break;
9113         case XOR:
9114           const0 ^= const1;
9115           break;
9116         case PLUS:
9117           const0 += const1;
9118           break;
9119         case NEG:
9120           op0 = NIL;
9121           break;
9122         default:
9123           break;
9124         }
9125     }
9126
9127   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
9128   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9129     return 0;
9130
9131   /* If the two constants aren't the same, we can't do anything.  The
9132      remaining six cases can all be done.  */
9133   else if (const0 != const1)
9134     return 0;
9135
9136   else
9137     switch (op0)
9138       {
9139       case IOR:
9140         if (op1 == AND)
9141           /* (a & b) | b == b */
9142           op0 = SET;
9143         else /* op1 == XOR */
9144           /* (a ^ b) | b == a | b */
9145           {;}
9146         break;
9147
9148       case XOR:
9149         if (op1 == AND)
9150           /* (a & b) ^ b == (~a) & b */
9151           op0 = AND, *pcomp_p = 1;
9152         else /* op1 == IOR */
9153           /* (a | b) ^ b == a & ~b */
9154           op0 = AND, const0 = ~const0;
9155         break;
9156
9157       case AND:
9158         if (op1 == IOR)
9159           /* (a | b) & b == b */
9160         op0 = SET;
9161         else /* op1 == XOR */
9162           /* (a ^ b) & b) == (~a) & b */
9163           *pcomp_p = 1;
9164         break;
9165       default:
9166         break;
9167       }
9168
9169   /* Check for NO-OP cases.  */
9170   const0 &= GET_MODE_MASK (mode);
9171   if (const0 == 0
9172       && (op0 == IOR || op0 == XOR || op0 == PLUS))
9173     op0 = NIL;
9174   else if (const0 == 0 && op0 == AND)
9175     op0 = SET;
9176   else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9177            && op0 == AND)
9178     op0 = NIL;
9179
9180   /* ??? Slightly redundant with the above mask, but not entirely.
9181      Moving this above means we'd have to sign-extend the mode mask
9182      for the final test.  */
9183   const0 = trunc_int_for_mode (const0, mode);
9184
9185   *pop0 = op0;
9186   *pconst0 = const0;
9187
9188   return 1;
9189 }
9190 \f
9191 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
9192    The result of the shift is RESULT_MODE.  X, if nonzero, is an expression
9193    that we started with.
9194
9195    The shift is normally computed in the widest mode we find in VAROP, as
9196    long as it isn't a different number of words than RESULT_MODE.  Exceptions
9197    are ASHIFTRT and ROTATE, which are always done in their original mode,  */
9198
9199 static rtx
9200 simplify_shift_const (rtx x, enum rtx_code code,
9201                       enum machine_mode result_mode, rtx varop,
9202                       int orig_count)
9203 {
9204   enum rtx_code orig_code = code;
9205   unsigned int count;
9206   int signed_count;
9207   enum machine_mode mode = result_mode;
9208   enum machine_mode shift_mode, tmode;
9209   unsigned int mode_words
9210     = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9211   /* We form (outer_op (code varop count) (outer_const)).  */
9212   enum rtx_code outer_op = NIL;
9213   HOST_WIDE_INT outer_const = 0;
9214   rtx const_rtx;
9215   int complement_p = 0;
9216   rtx new;
9217
9218   /* Make sure and truncate the "natural" shift on the way in.  We don't
9219      want to do this inside the loop as it makes it more difficult to
9220      combine shifts.  */
9221 #ifdef SHIFT_COUNT_TRUNCATED
9222   if (SHIFT_COUNT_TRUNCATED)
9223     orig_count &= GET_MODE_BITSIZE (mode) - 1;
9224 #endif
9225
9226   /* If we were given an invalid count, don't do anything except exactly
9227      what was requested.  */
9228
9229   if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
9230     {
9231       if (x)
9232         return x;
9233
9234       return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (orig_count));
9235     }
9236
9237   count = orig_count;
9238
9239   /* Unless one of the branches of the `if' in this loop does a `continue',
9240      we will `break' the loop after the `if'.  */
9241
9242   while (count != 0)
9243     {
9244       /* If we have an operand of (clobber (const_int 0)), just return that
9245          value.  */
9246       if (GET_CODE (varop) == CLOBBER)
9247         return varop;
9248
9249       /* If we discovered we had to complement VAROP, leave.  Making a NOT
9250          here would cause an infinite loop.  */
9251       if (complement_p)
9252         break;
9253
9254       /* Convert ROTATERT to ROTATE.  */
9255       if (code == ROTATERT)
9256         {
9257           unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
9258           code = ROTATE;
9259           if (VECTOR_MODE_P (result_mode))
9260             count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9261           else
9262             count = bitsize - count;
9263         }
9264
9265       /* We need to determine what mode we will do the shift in.  If the
9266          shift is a right shift or a ROTATE, we must always do it in the mode
9267          it was originally done in.  Otherwise, we can do it in MODE, the
9268          widest mode encountered.  */
9269       shift_mode
9270         = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9271            ? result_mode : mode);
9272
9273       /* Handle cases where the count is greater than the size of the mode
9274          minus 1.  For ASHIFT, use the size minus one as the count (this can
9275          occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
9276          take the count modulo the size.  For other shifts, the result is
9277          zero.
9278
9279          Since these shifts are being produced by the compiler by combining
9280          multiple operations, each of which are defined, we know what the
9281          result is supposed to be.  */
9282
9283       if (count > (unsigned int) (GET_MODE_BITSIZE (shift_mode) - 1))
9284         {
9285           if (code == ASHIFTRT)
9286             count = GET_MODE_BITSIZE (shift_mode) - 1;
9287           else if (code == ROTATE || code == ROTATERT)
9288             count %= GET_MODE_BITSIZE (shift_mode);
9289           else
9290             {
9291               /* We can't simply return zero because there may be an
9292                  outer op.  */
9293               varop = const0_rtx;
9294               count = 0;
9295               break;
9296             }
9297         }
9298
9299       /* An arithmetic right shift of a quantity known to be -1 or 0
9300          is a no-op.  */
9301       if (code == ASHIFTRT
9302           && (num_sign_bit_copies (varop, shift_mode)
9303               == GET_MODE_BITSIZE (shift_mode)))
9304         {
9305           count = 0;
9306           break;
9307         }
9308
9309       /* If we are doing an arithmetic right shift and discarding all but
9310          the sign bit copies, this is equivalent to doing a shift by the
9311          bitsize minus one.  Convert it into that shift because it will often
9312          allow other simplifications.  */
9313
9314       if (code == ASHIFTRT
9315           && (count + num_sign_bit_copies (varop, shift_mode)
9316               >= GET_MODE_BITSIZE (shift_mode)))
9317         count = GET_MODE_BITSIZE (shift_mode) - 1;
9318
9319       /* We simplify the tests below and elsewhere by converting
9320          ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9321          `make_compound_operation' will convert it to an ASHIFTRT for
9322          those machines (such as VAX) that don't have an LSHIFTRT.  */
9323       if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9324           && code == ASHIFTRT
9325           && ((nonzero_bits (varop, shift_mode)
9326                & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
9327               == 0))
9328         code = LSHIFTRT;
9329
9330       if (code == LSHIFTRT
9331           && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9332           && !(nonzero_bits (varop, shift_mode) >> count))
9333         varop = const0_rtx;
9334       if (code == ASHIFT
9335           && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9336           && !((nonzero_bits (varop, shift_mode) << count)
9337                & GET_MODE_MASK (shift_mode)))
9338         varop = const0_rtx;
9339
9340       switch (GET_CODE (varop))
9341         {
9342         case SIGN_EXTEND:
9343         case ZERO_EXTEND:
9344         case SIGN_EXTRACT:
9345         case ZERO_EXTRACT:
9346           new = expand_compound_operation (varop);
9347           if (new != varop)
9348             {
9349               varop = new;
9350               continue;
9351             }
9352           break;
9353
9354         case MEM:
9355           /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9356              minus the width of a smaller mode, we can do this with a
9357              SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
9358           if ((code == ASHIFTRT || code == LSHIFTRT)
9359               && ! mode_dependent_address_p (XEXP (varop, 0))
9360               && ! MEM_VOLATILE_P (varop)
9361               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9362                                          MODE_INT, 1)) != BLKmode)
9363             {
9364               new = adjust_address_nv (varop, tmode,
9365                                        BYTES_BIG_ENDIAN ? 0
9366                                        : count / BITS_PER_UNIT);
9367
9368               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9369                                      : ZERO_EXTEND, mode, new);
9370               count = 0;
9371               continue;
9372             }
9373           break;
9374
9375         case USE:
9376           /* Similar to the case above, except that we can only do this if
9377              the resulting mode is the same as that of the underlying
9378              MEM and adjust the address depending on the *bits* endianness
9379              because of the way that bit-field extract insns are defined.  */
9380           if ((code == ASHIFTRT || code == LSHIFTRT)
9381               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9382                                          MODE_INT, 1)) != BLKmode
9383               && tmode == GET_MODE (XEXP (varop, 0)))
9384             {
9385               if (BITS_BIG_ENDIAN)
9386                 new = XEXP (varop, 0);
9387               else
9388                 {
9389                   new = copy_rtx (XEXP (varop, 0));
9390                   SUBST (XEXP (new, 0),
9391                          plus_constant (XEXP (new, 0),
9392                                         count / BITS_PER_UNIT));
9393                 }
9394
9395               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9396                                      : ZERO_EXTEND, mode, new);
9397               count = 0;
9398               continue;
9399             }
9400           break;
9401
9402         case SUBREG:
9403           /* If VAROP is a SUBREG, strip it as long as the inner operand has
9404              the same number of words as what we've seen so far.  Then store
9405              the widest mode in MODE.  */
9406           if (subreg_lowpart_p (varop)
9407               && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9408                   > GET_MODE_SIZE (GET_MODE (varop)))
9409               && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9410                                   + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9411                  == mode_words)
9412             {
9413               varop = SUBREG_REG (varop);
9414               if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9415                 mode = GET_MODE (varop);
9416               continue;
9417             }
9418           break;
9419
9420         case MULT:
9421           /* Some machines use MULT instead of ASHIFT because MULT
9422              is cheaper.  But it is still better on those machines to
9423              merge two shifts into one.  */
9424           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9425               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9426             {
9427               varop
9428                 = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
9429                               GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
9430               continue;
9431             }
9432           break;
9433
9434         case UDIV:
9435           /* Similar, for when divides are cheaper.  */
9436           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9437               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9438             {
9439               varop
9440                 = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
9441                               GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
9442               continue;
9443             }
9444           break;
9445
9446         case ASHIFTRT:
9447           /* If we are extracting just the sign bit of an arithmetic
9448              right shift, that shift is not needed.  However, the sign
9449              bit of a wider mode may be different from what would be
9450              interpreted as the sign bit in a narrower mode, so, if
9451              the result is narrower, don't discard the shift.  */
9452           if (code == LSHIFTRT
9453               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9454               && (GET_MODE_BITSIZE (result_mode)
9455                   >= GET_MODE_BITSIZE (GET_MODE (varop))))
9456             {
9457               varop = XEXP (varop, 0);
9458               continue;
9459             }
9460
9461           /* ... fall through ...  */
9462
9463         case LSHIFTRT:
9464         case ASHIFT:
9465         case ROTATE:
9466           /* Here we have two nested shifts.  The result is usually the
9467              AND of a new shift with a mask.  We compute the result below.  */
9468           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9469               && INTVAL (XEXP (varop, 1)) >= 0
9470               && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9471               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9472               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9473             {
9474               enum rtx_code first_code = GET_CODE (varop);
9475               unsigned int first_count = INTVAL (XEXP (varop, 1));
9476               unsigned HOST_WIDE_INT mask;
9477               rtx mask_rtx;
9478
9479               /* We have one common special case.  We can't do any merging if
9480                  the inner code is an ASHIFTRT of a smaller mode.  However, if
9481                  we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9482                  with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9483                  we can convert it to
9484                  (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9485                  This simplifies certain SIGN_EXTEND operations.  */
9486               if (code == ASHIFT && first_code == ASHIFTRT
9487                   && count == (unsigned int)
9488                               (GET_MODE_BITSIZE (result_mode)
9489                                - GET_MODE_BITSIZE (GET_MODE (varop))))
9490                 {
9491                   /* C3 has the low-order C1 bits zero.  */
9492
9493                   mask = (GET_MODE_MASK (mode)
9494                           & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
9495
9496                   varop = simplify_and_const_int (NULL_RTX, result_mode,
9497                                                   XEXP (varop, 0), mask);
9498                   varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
9499                                                 varop, count);
9500                   count = first_count;
9501                   code = ASHIFTRT;
9502                   continue;
9503                 }
9504
9505               /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
9506                  than C1 high-order bits equal to the sign bit, we can convert
9507                  this to either an ASHIFT or an ASHIFTRT depending on the
9508                  two counts.
9509
9510                  We cannot do this if VAROP's mode is not SHIFT_MODE.  */
9511
9512               if (code == ASHIFTRT && first_code == ASHIFT
9513                   && GET_MODE (varop) == shift_mode
9514                   && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
9515                       > first_count))
9516                 {
9517                   varop = XEXP (varop, 0);
9518
9519                   signed_count = count - first_count;
9520                   if (signed_count < 0)
9521                     count = -signed_count, code = ASHIFT;
9522                   else
9523                     count = signed_count;
9524
9525                   continue;
9526                 }
9527
9528               /* There are some cases we can't do.  If CODE is ASHIFTRT,
9529                  we can only do this if FIRST_CODE is also ASHIFTRT.
9530
9531                  We can't do the case when CODE is ROTATE and FIRST_CODE is
9532                  ASHIFTRT.
9533
9534                  If the mode of this shift is not the mode of the outer shift,
9535                  we can't do this if either shift is a right shift or ROTATE.
9536
9537                  Finally, we can't do any of these if the mode is too wide
9538                  unless the codes are the same.
9539
9540                  Handle the case where the shift codes are the same
9541                  first.  */
9542
9543               if (code == first_code)
9544                 {
9545                   if (GET_MODE (varop) != result_mode
9546                       && (code == ASHIFTRT || code == LSHIFTRT
9547                           || code == ROTATE))
9548                     break;
9549
9550                   count += first_count;
9551                   varop = XEXP (varop, 0);
9552                   continue;
9553                 }
9554
9555               if (code == ASHIFTRT
9556                   || (code == ROTATE && first_code == ASHIFTRT)
9557                   || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
9558                   || (GET_MODE (varop) != result_mode
9559                       && (first_code == ASHIFTRT || first_code == LSHIFTRT
9560                           || first_code == ROTATE
9561                           || code == ROTATE)))
9562                 break;
9563
9564               /* To compute the mask to apply after the shift, shift the
9565                  nonzero bits of the inner shift the same way the
9566                  outer shift will.  */
9567
9568               mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
9569
9570               mask_rtx
9571                 = simplify_binary_operation (code, result_mode, mask_rtx,
9572                                              GEN_INT (count));
9573
9574               /* Give up if we can't compute an outer operation to use.  */
9575               if (mask_rtx == 0
9576                   || GET_CODE (mask_rtx) != CONST_INT
9577                   || ! merge_outer_ops (&outer_op, &outer_const, AND,
9578                                         INTVAL (mask_rtx),
9579                                         result_mode, &complement_p))
9580                 break;
9581
9582               /* If the shifts are in the same direction, we add the
9583                  counts.  Otherwise, we subtract them.  */
9584               signed_count = count;
9585               if ((code == ASHIFTRT || code == LSHIFTRT)
9586                   == (first_code == ASHIFTRT || first_code == LSHIFTRT))
9587                 signed_count += first_count;
9588               else
9589                 signed_count -= first_count;
9590
9591               /* If COUNT is positive, the new shift is usually CODE,
9592                  except for the two exceptions below, in which case it is
9593                  FIRST_CODE.  If the count is negative, FIRST_CODE should
9594                  always be used  */
9595               if (signed_count > 0
9596                   && ((first_code == ROTATE && code == ASHIFT)
9597                       || (first_code == ASHIFTRT && code == LSHIFTRT)))
9598                 code = first_code, count = signed_count;
9599               else if (signed_count < 0)
9600                 code = first_code, count = -signed_count;
9601               else
9602                 count = signed_count;
9603
9604               varop = XEXP (varop, 0);
9605               continue;
9606             }
9607
9608           /* If we have (A << B << C) for any shift, we can convert this to
9609              (A << C << B).  This wins if A is a constant.  Only try this if
9610              B is not a constant.  */
9611
9612           else if (GET_CODE (varop) == code
9613                    && GET_CODE (XEXP (varop, 1)) != CONST_INT
9614                    && 0 != (new
9615                             = simplify_binary_operation (code, mode,
9616                                                          XEXP (varop, 0),
9617                                                          GEN_INT (count))))
9618             {
9619               varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
9620               count = 0;
9621               continue;
9622             }
9623           break;
9624
9625         case NOT:
9626           /* Make this fit the case below.  */
9627           varop = gen_rtx_XOR (mode, XEXP (varop, 0),
9628                                GEN_INT (GET_MODE_MASK (mode)));
9629           continue;
9630
9631         case IOR:
9632         case AND:
9633         case XOR:
9634           /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9635              with C the size of VAROP - 1 and the shift is logical if
9636              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9637              we have an (le X 0) operation.   If we have an arithmetic shift
9638              and STORE_FLAG_VALUE is 1 or we have a logical shift with
9639              STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
9640
9641           if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
9642               && XEXP (XEXP (varop, 0), 1) == constm1_rtx
9643               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9644               && (code == LSHIFTRT || code == ASHIFTRT)
9645               && count == (unsigned int)
9646                           (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9647               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9648             {
9649               count = 0;
9650               varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
9651                                   const0_rtx);
9652
9653               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9654                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9655
9656               continue;
9657             }
9658
9659           /* If we have (shift (logical)), move the logical to the outside
9660              to allow it to possibly combine with another logical and the
9661              shift to combine with another shift.  This also canonicalizes to
9662              what a ZERO_EXTRACT looks like.  Also, some machines have
9663              (and (shift)) insns.  */
9664
9665           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9666               && (new = simplify_binary_operation (code, result_mode,
9667                                                    XEXP (varop, 1),
9668                                                    GEN_INT (count))) != 0
9669               && GET_CODE (new) == CONST_INT
9670               && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9671                                   INTVAL (new), result_mode, &complement_p))
9672             {
9673               varop = XEXP (varop, 0);
9674               continue;
9675             }
9676
9677           /* If we can't do that, try to simplify the shift in each arm of the
9678              logical expression, make a new logical expression, and apply
9679              the inverse distributive law.  */
9680           {
9681             rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9682                                             XEXP (varop, 0), count);
9683             rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9684                                             XEXP (varop, 1), count);
9685
9686             varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
9687             varop = apply_distributive_law (varop);
9688
9689             count = 0;
9690           }
9691           break;
9692
9693         case EQ:
9694           /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9695              says that the sign bit can be tested, FOO has mode MODE, C is
9696              GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9697              that may be nonzero.  */
9698           if (code == LSHIFTRT
9699               && XEXP (varop, 1) == const0_rtx
9700               && GET_MODE (XEXP (varop, 0)) == result_mode
9701               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9702               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9703               && ((STORE_FLAG_VALUE
9704                    & ((HOST_WIDE_INT) 1
9705                       < (GET_MODE_BITSIZE (result_mode) - 1))))
9706               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9707               && merge_outer_ops (&outer_op, &outer_const, XOR,
9708                                   (HOST_WIDE_INT) 1, result_mode,
9709                                   &complement_p))
9710             {
9711               varop = XEXP (varop, 0);
9712               count = 0;
9713               continue;
9714             }
9715           break;
9716
9717         case NEG:
9718           /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9719              than the number of bits in the mode is equivalent to A.  */
9720           if (code == LSHIFTRT
9721               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9722               && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9723             {
9724               varop = XEXP (varop, 0);
9725               count = 0;
9726               continue;
9727             }
9728
9729           /* NEG commutes with ASHIFT since it is multiplication.  Move the
9730              NEG outside to allow shifts to combine.  */
9731           if (code == ASHIFT
9732               && merge_outer_ops (&outer_op, &outer_const, NEG,
9733                                   (HOST_WIDE_INT) 0, result_mode,
9734                                   &complement_p))
9735             {
9736               varop = XEXP (varop, 0);
9737               continue;
9738             }
9739           break;
9740
9741         case PLUS:
9742           /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9743              is one less than the number of bits in the mode is
9744              equivalent to (xor A 1).  */
9745           if (code == LSHIFTRT
9746               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9747               && XEXP (varop, 1) == constm1_rtx
9748               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9749               && merge_outer_ops (&outer_op, &outer_const, XOR,
9750                                   (HOST_WIDE_INT) 1, result_mode,
9751                                   &complement_p))
9752             {
9753               count = 0;
9754               varop = XEXP (varop, 0);
9755               continue;
9756             }
9757
9758           /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9759              that might be nonzero in BAR are those being shifted out and those
9760              bits are known zero in FOO, we can replace the PLUS with FOO.
9761              Similarly in the other operand order.  This code occurs when
9762              we are computing the size of a variable-size array.  */
9763
9764           if ((code == ASHIFTRT || code == LSHIFTRT)
9765               && count < HOST_BITS_PER_WIDE_INT
9766               && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9767               && (nonzero_bits (XEXP (varop, 1), result_mode)
9768                   & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9769             {
9770               varop = XEXP (varop, 0);
9771               continue;
9772             }
9773           else if ((code == ASHIFTRT || code == LSHIFTRT)
9774                    && count < HOST_BITS_PER_WIDE_INT
9775                    && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9776                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9777                             >> count)
9778                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9779                             & nonzero_bits (XEXP (varop, 1),
9780                                                  result_mode)))
9781             {
9782               varop = XEXP (varop, 1);
9783               continue;
9784             }
9785
9786           /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
9787           if (code == ASHIFT
9788               && GET_CODE (XEXP (varop, 1)) == CONST_INT
9789               && (new = simplify_binary_operation (ASHIFT, result_mode,
9790                                                    XEXP (varop, 1),
9791                                                    GEN_INT (count))) != 0
9792               && GET_CODE (new) == CONST_INT
9793               && merge_outer_ops (&outer_op, &outer_const, PLUS,
9794                                   INTVAL (new), result_mode, &complement_p))
9795             {
9796               varop = XEXP (varop, 0);
9797               continue;
9798             }
9799           break;
9800
9801         case MINUS:
9802           /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9803              with C the size of VAROP - 1 and the shift is logical if
9804              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9805              we have a (gt X 0) operation.  If the shift is arithmetic with
9806              STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9807              we have a (neg (gt X 0)) operation.  */
9808
9809           if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9810               && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9811               && count == (unsigned int)
9812                           (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9813               && (code == LSHIFTRT || code == ASHIFTRT)
9814               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9815               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (varop, 0), 1))
9816                  == count
9817               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9818             {
9819               count = 0;
9820               varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9821                                   const0_rtx);
9822
9823               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9824                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9825
9826               continue;
9827             }
9828           break;
9829
9830         case TRUNCATE:
9831           /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9832              if the truncate does not affect the value.  */
9833           if (code == LSHIFTRT
9834               && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9835               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9836               && (INTVAL (XEXP (XEXP (varop, 0), 1))
9837                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9838                       - GET_MODE_BITSIZE (GET_MODE (varop)))))
9839             {
9840               rtx varop_inner = XEXP (varop, 0);
9841
9842               varop_inner
9843                 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9844                                     XEXP (varop_inner, 0),
9845                                     GEN_INT
9846                                     (count + INTVAL (XEXP (varop_inner, 1))));
9847               varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9848               count = 0;
9849               continue;
9850             }
9851           break;
9852
9853         default:
9854           break;
9855         }
9856
9857       break;
9858     }
9859
9860   /* We need to determine what mode to do the shift in.  If the shift is
9861      a right shift or ROTATE, we must always do it in the mode it was
9862      originally done in.  Otherwise, we can do it in MODE, the widest mode
9863      encountered.  The code we care about is that of the shift that will
9864      actually be done, not the shift that was originally requested.  */
9865   shift_mode
9866     = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9867        ? result_mode : mode);
9868
9869   /* We have now finished analyzing the shift.  The result should be
9870      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
9871      OUTER_OP is non-NIL, it is an operation that needs to be applied
9872      to the result of the shift.  OUTER_CONST is the relevant constant,
9873      but we must turn off all bits turned off in the shift.
9874
9875      If we were passed a value for X, see if we can use any pieces of
9876      it.  If not, make new rtx.  */
9877
9878   if (x && GET_RTX_CLASS (GET_CODE (x)) == '2'
9879       && GET_CODE (XEXP (x, 1)) == CONST_INT
9880       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) == count)
9881     const_rtx = XEXP (x, 1);
9882   else
9883     const_rtx = GEN_INT (count);
9884
9885   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
9886       && GET_MODE (XEXP (x, 0)) == shift_mode
9887       && SUBREG_REG (XEXP (x, 0)) == varop)
9888     varop = XEXP (x, 0);
9889   else if (GET_MODE (varop) != shift_mode)
9890     varop = gen_lowpart_for_combine (shift_mode, varop);
9891
9892   /* If we can't make the SUBREG, try to return what we were given.  */
9893   if (GET_CODE (varop) == CLOBBER)
9894     return x ? x : varop;
9895
9896   new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
9897   if (new != 0)
9898     x = new;
9899   else
9900     x = gen_rtx_fmt_ee (code, shift_mode, varop, const_rtx);
9901
9902   /* If we have an outer operation and we just made a shift, it is
9903      possible that we could have simplified the shift were it not
9904      for the outer operation.  So try to do the simplification
9905      recursively.  */
9906
9907   if (outer_op != NIL && GET_CODE (x) == code
9908       && GET_CODE (XEXP (x, 1)) == CONST_INT)
9909     x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
9910                               INTVAL (XEXP (x, 1)));
9911
9912   /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9913      turn off all the bits that the shift would have turned off.  */
9914   if (orig_code == LSHIFTRT && result_mode != shift_mode)
9915     x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9916                                 GET_MODE_MASK (result_mode) >> orig_count);
9917
9918   /* Do the remainder of the processing in RESULT_MODE.  */
9919   x = gen_lowpart_for_combine (result_mode, x);
9920
9921   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9922      operation.  */
9923   if (complement_p)
9924     x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9925
9926   if (outer_op != NIL)
9927     {
9928       if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9929         outer_const = trunc_int_for_mode (outer_const, result_mode);
9930
9931       if (outer_op == AND)
9932         x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9933       else if (outer_op == SET)
9934         /* This means that we have determined that the result is
9935            equivalent to a constant.  This should be rare.  */
9936         x = GEN_INT (outer_const);
9937       else if (GET_RTX_CLASS (outer_op) == '1')
9938         x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
9939       else
9940         x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
9941     }
9942
9943   return x;
9944 }
9945 \f
9946 /* Like recog, but we receive the address of a pointer to a new pattern.
9947    We try to match the rtx that the pointer points to.
9948    If that fails, we may try to modify or replace the pattern,
9949    storing the replacement into the same pointer object.
9950
9951    Modifications include deletion or addition of CLOBBERs.
9952
9953    PNOTES is a pointer to a location where any REG_UNUSED notes added for
9954    the CLOBBERs are placed.
9955
9956    The value is the final insn code from the pattern ultimately matched,
9957    or -1.  */
9958
9959 static int
9960 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
9961 {
9962   rtx pat = *pnewpat;
9963   int insn_code_number;
9964   int num_clobbers_to_add = 0;
9965   int i;
9966   rtx notes = 0;
9967   rtx dummy_insn;
9968
9969   /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9970      we use to indicate that something didn't match.  If we find such a
9971      thing, force rejection.  */
9972   if (GET_CODE (pat) == PARALLEL)
9973     for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9974       if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9975           && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9976         return -1;
9977
9978   /* *pnewpat does not have to be actual PATTERN (insn), so make a dummy
9979      instruction for pattern recognition.  */
9980   dummy_insn = shallow_copy_rtx (insn);
9981   PATTERN (dummy_insn) = pat;
9982   REG_NOTES (dummy_insn) = 0;
9983
9984   insn_code_number = recog (pat, dummy_insn, &num_clobbers_to_add);
9985
9986   /* If it isn't, there is the possibility that we previously had an insn
9987      that clobbered some register as a side effect, but the combined
9988      insn doesn't need to do that.  So try once more without the clobbers
9989      unless this represents an ASM insn.  */
9990
9991   if (insn_code_number < 0 && ! check_asm_operands (pat)
9992       && GET_CODE (pat) == PARALLEL)
9993     {
9994       int pos;
9995
9996       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9997         if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9998           {
9999             if (i != pos)
10000               SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10001             pos++;
10002           }
10003
10004       SUBST_INT (XVECLEN (pat, 0), pos);
10005
10006       if (pos == 1)
10007         pat = XVECEXP (pat, 0, 0);
10008
10009       PATTERN (dummy_insn) = pat;
10010       insn_code_number = recog (pat, dummy_insn, &num_clobbers_to_add);
10011     }
10012
10013   /* Recognize all noop sets, these will be killed by followup pass.  */
10014   if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10015     insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10016
10017   /* If we had any clobbers to add, make a new pattern than contains
10018      them.  Then check to make sure that all of them are dead.  */
10019   if (num_clobbers_to_add)
10020     {
10021       rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10022                                      rtvec_alloc (GET_CODE (pat) == PARALLEL
10023                                                   ? (XVECLEN (pat, 0)
10024                                                      + num_clobbers_to_add)
10025                                                   : num_clobbers_to_add + 1));
10026
10027       if (GET_CODE (pat) == PARALLEL)
10028         for (i = 0; i < XVECLEN (pat, 0); i++)
10029           XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10030       else
10031         XVECEXP (newpat, 0, 0) = pat;
10032
10033       add_clobbers (newpat, insn_code_number);
10034
10035       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10036            i < XVECLEN (newpat, 0); i++)
10037         {
10038           if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
10039               && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10040             return -1;
10041           notes = gen_rtx_EXPR_LIST (REG_UNUSED,
10042                                      XEXP (XVECEXP (newpat, 0, i), 0), notes);
10043         }
10044       pat = newpat;
10045     }
10046
10047   *pnewpat = pat;
10048   *pnotes = notes;
10049
10050   return insn_code_number;
10051 }
10052 \f
10053 /* Like gen_lowpart but for use by combine.  In combine it is not possible
10054    to create any new pseudoregs.  However, it is safe to create
10055    invalid memory addresses, because combine will try to recognize
10056    them and all they will do is make the combine attempt fail.
10057
10058    If for some reason this cannot do its job, an rtx
10059    (clobber (const_int 0)) is returned.
10060    An insn containing that will not be recognized.  */
10061
10062 #undef gen_lowpart
10063
10064 static rtx
10065 gen_lowpart_for_combine (enum machine_mode mode, rtx x)
10066 {
10067   rtx result;
10068
10069   if (GET_MODE (x) == mode)
10070     return x;
10071
10072   /* Return identity if this is a CONST or symbolic
10073      reference.  */
10074   if (mode == Pmode
10075       && (GET_CODE (x) == CONST
10076           || GET_CODE (x) == SYMBOL_REF
10077           || GET_CODE (x) == LABEL_REF))
10078     return x;
10079
10080   /* We can only support MODE being wider than a word if X is a
10081      constant integer or has a mode the same size.  */
10082
10083   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
10084       && ! ((GET_MODE (x) == VOIDmode
10085              && (GET_CODE (x) == CONST_INT
10086                  || GET_CODE (x) == CONST_DOUBLE))
10087             || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
10088     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10089
10090   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
10091      won't know what to do.  So we will strip off the SUBREG here and
10092      process normally.  */
10093   if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
10094     {
10095       x = SUBREG_REG (x);
10096       if (GET_MODE (x) == mode)
10097         return x;
10098     }
10099
10100   result = gen_lowpart_common (mode, x);
10101 #ifdef CANNOT_CHANGE_MODE_CLASS
10102   if (result != 0
10103       && GET_CODE (result) == SUBREG
10104       && GET_CODE (SUBREG_REG (result)) == REG
10105       && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER)
10106     bitmap_set_bit (&subregs_of_mode, REGNO (SUBREG_REG (result))
10107                                       * MAX_MACHINE_MODE
10108                                       + GET_MODE (result));
10109 #endif
10110
10111   if (result)
10112     return result;
10113
10114   if (GET_CODE (x) == MEM)
10115     {
10116       int offset = 0;
10117
10118       /* Refuse to work on a volatile memory ref or one with a mode-dependent
10119          address.  */
10120       if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
10121         return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10122
10123       /* If we want to refer to something bigger than the original memref,
10124          generate a perverse subreg instead.  That will force a reload
10125          of the original memref X.  */
10126       if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
10127         return gen_rtx_SUBREG (mode, x, 0);
10128
10129       if (WORDS_BIG_ENDIAN)
10130         offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
10131                   - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
10132
10133       if (BYTES_BIG_ENDIAN)
10134         {
10135           /* Adjust the address so that the address-after-the-data is
10136              unchanged.  */
10137           offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
10138                      - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
10139         }
10140
10141       return adjust_address_nv (x, mode, offset);
10142     }
10143
10144   /* If X is a comparison operator, rewrite it in a new mode.  This
10145      probably won't match, but may allow further simplifications.  */
10146   else if (GET_RTX_CLASS (GET_CODE (x)) == '<')
10147     return gen_rtx_fmt_ee (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
10148
10149   /* If we couldn't simplify X any other way, just enclose it in a
10150      SUBREG.  Normally, this SUBREG won't match, but some patterns may
10151      include an explicit SUBREG or we may simplify it further in combine.  */
10152   else
10153     {
10154       int offset = 0;
10155       rtx res;
10156       enum machine_mode sub_mode = GET_MODE (x);
10157
10158       offset = subreg_lowpart_offset (mode, sub_mode);
10159       if (sub_mode == VOIDmode)
10160         {
10161           sub_mode = int_mode_for_mode (mode);
10162           x = gen_lowpart_common (sub_mode, x);
10163           if (x == 0)
10164             return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
10165         }
10166       res = simplify_gen_subreg (mode, x, sub_mode, offset);
10167       if (res)
10168         return res;
10169       return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10170     }
10171 }
10172 \f
10173 /* These routines make binary and unary operations by first seeing if they
10174    fold; if not, a new expression is allocated.  */
10175
10176 static rtx
10177 gen_binary (enum rtx_code code, enum machine_mode mode, rtx op0, rtx op1)
10178 {
10179   rtx result;
10180   rtx tem;
10181
10182   if (GET_CODE (op0) == CLOBBER)
10183     return op0;
10184   else if (GET_CODE (op1) == CLOBBER)
10185     return op1;
10186   
10187   if (GET_RTX_CLASS (code) == 'c'
10188       && swap_commutative_operands_p (op0, op1))
10189     tem = op0, op0 = op1, op1 = tem;
10190
10191   if (GET_RTX_CLASS (code) == '<')
10192     {
10193       enum machine_mode op_mode = GET_MODE (op0);
10194
10195       /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get
10196          just (REL_OP X Y).  */
10197       if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
10198         {
10199           op1 = XEXP (op0, 1);
10200           op0 = XEXP (op0, 0);
10201           op_mode = GET_MODE (op0);
10202         }
10203
10204       if (op_mode == VOIDmode)
10205         op_mode = GET_MODE (op1);
10206       result = simplify_relational_operation (code, op_mode, op0, op1);
10207     }
10208   else
10209     result = simplify_binary_operation (code, mode, op0, op1);
10210
10211   if (result)
10212     return result;
10213
10214   /* Put complex operands first and constants second.  */
10215   if (GET_RTX_CLASS (code) == 'c'
10216       && swap_commutative_operands_p (op0, op1))
10217     return gen_rtx_fmt_ee (code, mode, op1, op0);
10218
10219   /* If we are turning off bits already known off in OP0, we need not do
10220      an AND.  */
10221   else if (code == AND && GET_CODE (op1) == CONST_INT
10222            && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10223            && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
10224     return op0;
10225
10226   return gen_rtx_fmt_ee (code, mode, op0, op1);
10227 }
10228 \f
10229 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
10230    comparison code that will be tested.
10231
10232    The result is a possibly different comparison code to use.  *POP0 and
10233    *POP1 may be updated.
10234
10235    It is possible that we might detect that a comparison is either always
10236    true or always false.  However, we do not perform general constant
10237    folding in combine, so this knowledge isn't useful.  Such tautologies
10238    should have been detected earlier.  Hence we ignore all such cases.  */
10239
10240 static enum rtx_code
10241 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
10242 {
10243   rtx op0 = *pop0;
10244   rtx op1 = *pop1;
10245   rtx tem, tem1;
10246   int i;
10247   enum machine_mode mode, tmode;
10248
10249   /* Try a few ways of applying the same transformation to both operands.  */
10250   while (1)
10251     {
10252 #ifndef WORD_REGISTER_OPERATIONS
10253       /* The test below this one won't handle SIGN_EXTENDs on these machines,
10254          so check specially.  */
10255       if (code != GTU && code != GEU && code != LTU && code != LEU
10256           && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
10257           && GET_CODE (XEXP (op0, 0)) == ASHIFT
10258           && GET_CODE (XEXP (op1, 0)) == ASHIFT
10259           && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
10260           && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
10261           && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
10262               == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
10263           && GET_CODE (XEXP (op0, 1)) == CONST_INT
10264           && XEXP (op0, 1) == XEXP (op1, 1)
10265           && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10266           && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
10267           && (INTVAL (XEXP (op0, 1))
10268               == (GET_MODE_BITSIZE (GET_MODE (op0))
10269                   - (GET_MODE_BITSIZE
10270                      (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
10271         {
10272           op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
10273           op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
10274         }
10275 #endif
10276
10277       /* If both operands are the same constant shift, see if we can ignore the
10278          shift.  We can if the shift is a rotate or if the bits shifted out of
10279          this shift are known to be zero for both inputs and if the type of
10280          comparison is compatible with the shift.  */
10281       if (GET_CODE (op0) == GET_CODE (op1)
10282           && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10283           && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
10284               || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
10285                   && (code != GT && code != LT && code != GE && code != LE))
10286               || (GET_CODE (op0) == ASHIFTRT
10287                   && (code != GTU && code != LTU
10288                       && code != GEU && code != LEU)))
10289           && GET_CODE (XEXP (op0, 1)) == CONST_INT
10290           && INTVAL (XEXP (op0, 1)) >= 0
10291           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10292           && XEXP (op0, 1) == XEXP (op1, 1))
10293         {
10294           enum machine_mode mode = GET_MODE (op0);
10295           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10296           int shift_count = INTVAL (XEXP (op0, 1));
10297
10298           if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
10299             mask &= (mask >> shift_count) << shift_count;
10300           else if (GET_CODE (op0) == ASHIFT)
10301             mask = (mask & (mask << shift_count)) >> shift_count;
10302
10303           if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
10304               && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
10305             op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
10306           else
10307             break;
10308         }
10309
10310       /* If both operands are AND's of a paradoxical SUBREG by constant, the
10311          SUBREGs are of the same mode, and, in both cases, the AND would
10312          be redundant if the comparison was done in the narrower mode,
10313          do the comparison in the narrower mode (e.g., we are AND'ing with 1
10314          and the operand's possibly nonzero bits are 0xffffff01; in that case
10315          if we only care about QImode, we don't need the AND).  This case
10316          occurs if the output mode of an scc insn is not SImode and
10317          STORE_FLAG_VALUE == 1 (e.g., the 386).
10318
10319          Similarly, check for a case where the AND's are ZERO_EXTEND
10320          operations from some narrower mode even though a SUBREG is not
10321          present.  */
10322
10323       else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
10324                && GET_CODE (XEXP (op0, 1)) == CONST_INT
10325                && GET_CODE (XEXP (op1, 1)) == CONST_INT)
10326         {
10327           rtx inner_op0 = XEXP (op0, 0);
10328           rtx inner_op1 = XEXP (op1, 0);
10329           HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
10330           HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
10331           int changed = 0;
10332
10333           if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
10334               && (GET_MODE_SIZE (GET_MODE (inner_op0))
10335                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
10336               && (GET_MODE (SUBREG_REG (inner_op0))
10337                   == GET_MODE (SUBREG_REG (inner_op1)))
10338               && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
10339                   <= HOST_BITS_PER_WIDE_INT)
10340               && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
10341                                              GET_MODE (SUBREG_REG (inner_op0)))))
10342               && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
10343                                              GET_MODE (SUBREG_REG (inner_op1))))))
10344             {
10345               op0 = SUBREG_REG (inner_op0);
10346               op1 = SUBREG_REG (inner_op1);
10347
10348               /* The resulting comparison is always unsigned since we masked
10349                  off the original sign bit.  */
10350               code = unsigned_condition (code);
10351
10352               changed = 1;
10353             }
10354
10355           else if (c0 == c1)
10356             for (tmode = GET_CLASS_NARROWEST_MODE
10357                  (GET_MODE_CLASS (GET_MODE (op0)));
10358                  tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10359               if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10360                 {
10361                   op0 = gen_lowpart_for_combine (tmode, inner_op0);
10362                   op1 = gen_lowpart_for_combine (tmode, inner_op1);
10363                   code = unsigned_condition (code);
10364                   changed = 1;
10365                   break;
10366                 }
10367
10368           if (! changed)
10369             break;
10370         }
10371
10372       /* If both operands are NOT, we can strip off the outer operation
10373          and adjust the comparison code for swapped operands; similarly for
10374          NEG, except that this must be an equality comparison.  */
10375       else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10376                || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10377                    && (code == EQ || code == NE)))
10378         op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10379
10380       else
10381         break;
10382     }
10383
10384   /* If the first operand is a constant, swap the operands and adjust the
10385      comparison code appropriately, but don't do this if the second operand
10386      is already a constant integer.  */
10387   if (swap_commutative_operands_p (op0, op1))
10388     {
10389       tem = op0, op0 = op1, op1 = tem;
10390       code = swap_condition (code);
10391     }
10392
10393   /* We now enter a loop during which we will try to simplify the comparison.
10394      For the most part, we only are concerned with comparisons with zero,
10395      but some things may really be comparisons with zero but not start
10396      out looking that way.  */
10397
10398   while (GET_CODE (op1) == CONST_INT)
10399     {
10400       enum machine_mode mode = GET_MODE (op0);
10401       unsigned int mode_width = GET_MODE_BITSIZE (mode);
10402       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10403       int equality_comparison_p;
10404       int sign_bit_comparison_p;
10405       int unsigned_comparison_p;
10406       HOST_WIDE_INT const_op;
10407
10408       /* We only want to handle integral modes.  This catches VOIDmode,
10409          CCmode, and the floating-point modes.  An exception is that we
10410          can handle VOIDmode if OP0 is a COMPARE or a comparison
10411          operation.  */
10412
10413       if (GET_MODE_CLASS (mode) != MODE_INT
10414           && ! (mode == VOIDmode
10415                 && (GET_CODE (op0) == COMPARE
10416                     || GET_RTX_CLASS (GET_CODE (op0)) == '<')))
10417         break;
10418
10419       /* Get the constant we are comparing against and turn off all bits
10420          not on in our mode.  */
10421       const_op = INTVAL (op1);
10422       if (mode != VOIDmode)
10423         const_op = trunc_int_for_mode (const_op, mode);
10424       op1 = GEN_INT (const_op);
10425
10426       /* If we are comparing against a constant power of two and the value
10427          being compared can only have that single bit nonzero (e.g., it was
10428          `and'ed with that bit), we can replace this with a comparison
10429          with zero.  */
10430       if (const_op
10431           && (code == EQ || code == NE || code == GE || code == GEU
10432               || code == LT || code == LTU)
10433           && mode_width <= HOST_BITS_PER_WIDE_INT
10434           && exact_log2 (const_op) >= 0
10435           && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10436         {
10437           code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10438           op1 = const0_rtx, const_op = 0;
10439         }
10440
10441       /* Similarly, if we are comparing a value known to be either -1 or
10442          0 with -1, change it to the opposite comparison against zero.  */
10443
10444       if (const_op == -1
10445           && (code == EQ || code == NE || code == GT || code == LE
10446               || code == GEU || code == LTU)
10447           && num_sign_bit_copies (op0, mode) == mode_width)
10448         {
10449           code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10450           op1 = const0_rtx, const_op = 0;
10451         }
10452
10453       /* Do some canonicalizations based on the comparison code.  We prefer
10454          comparisons against zero and then prefer equality comparisons.
10455          If we can reduce the size of a constant, we will do that too.  */
10456
10457       switch (code)
10458         {
10459         case LT:
10460           /* < C is equivalent to <= (C - 1) */
10461           if (const_op > 0)
10462             {
10463               const_op -= 1;
10464               op1 = GEN_INT (const_op);
10465               code = LE;
10466               /* ... fall through to LE case below.  */
10467             }
10468           else
10469             break;
10470
10471         case LE:
10472           /* <= C is equivalent to < (C + 1); we do this for C < 0  */
10473           if (const_op < 0)
10474             {
10475               const_op += 1;
10476               op1 = GEN_INT (const_op);
10477               code = LT;
10478             }
10479
10480           /* If we are doing a <= 0 comparison on a value known to have
10481              a zero sign bit, we can replace this with == 0.  */
10482           else if (const_op == 0
10483                    && mode_width <= HOST_BITS_PER_WIDE_INT
10484                    && (nonzero_bits (op0, mode)
10485                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10486             code = EQ;
10487           break;
10488
10489         case GE:
10490           /* >= C is equivalent to > (C - 1).  */
10491           if (const_op > 0)
10492             {
10493               const_op -= 1;
10494               op1 = GEN_INT (const_op);
10495               code = GT;
10496               /* ... fall through to GT below.  */
10497             }
10498           else
10499             break;
10500
10501         case GT:
10502           /* > C is equivalent to >= (C + 1); we do this for C < 0.  */
10503           if (const_op < 0)
10504             {
10505               const_op += 1;
10506               op1 = GEN_INT (const_op);
10507               code = GE;
10508             }
10509
10510           /* If we are doing a > 0 comparison on a value known to have
10511              a zero sign bit, we can replace this with != 0.  */
10512           else if (const_op == 0
10513                    && mode_width <= HOST_BITS_PER_WIDE_INT
10514                    && (nonzero_bits (op0, mode)
10515                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10516             code = NE;
10517           break;
10518
10519         case LTU:
10520           /* < C is equivalent to <= (C - 1).  */
10521           if (const_op > 0)
10522             {
10523               const_op -= 1;
10524               op1 = GEN_INT (const_op);
10525               code = LEU;
10526               /* ... fall through ...  */
10527             }
10528
10529           /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
10530           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10531                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10532             {
10533               const_op = 0, op1 = const0_rtx;
10534               code = GE;
10535               break;
10536             }
10537           else
10538             break;
10539
10540         case LEU:
10541           /* unsigned <= 0 is equivalent to == 0 */
10542           if (const_op == 0)
10543             code = EQ;
10544
10545           /* (unsigned) <= 0x7fffffff is equivalent to >= 0.  */
10546           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10547                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10548             {
10549               const_op = 0, op1 = const0_rtx;
10550               code = GE;
10551             }
10552           break;
10553
10554         case GEU:
10555           /* >= C is equivalent to < (C - 1).  */
10556           if (const_op > 1)
10557             {
10558               const_op -= 1;
10559               op1 = GEN_INT (const_op);
10560               code = GTU;
10561               /* ... fall through ...  */
10562             }
10563
10564           /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
10565           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10566                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10567             {
10568               const_op = 0, op1 = const0_rtx;
10569               code = LT;
10570               break;
10571             }
10572           else
10573             break;
10574
10575         case GTU:
10576           /* unsigned > 0 is equivalent to != 0 */
10577           if (const_op == 0)
10578             code = NE;
10579
10580           /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
10581           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10582                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10583             {
10584               const_op = 0, op1 = const0_rtx;
10585               code = LT;
10586             }
10587           break;
10588
10589         default:
10590           break;
10591         }
10592
10593       /* Compute some predicates to simplify code below.  */
10594
10595       equality_comparison_p = (code == EQ || code == NE);
10596       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
10597       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
10598                                || code == GEU);
10599
10600       /* If this is a sign bit comparison and we can do arithmetic in
10601          MODE, say that we will only be needing the sign bit of OP0.  */
10602       if (sign_bit_comparison_p
10603           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10604         op0 = force_to_mode (op0, mode,
10605                              ((HOST_WIDE_INT) 1
10606                               << (GET_MODE_BITSIZE (mode) - 1)),
10607                              NULL_RTX, 0);
10608
10609       /* Now try cases based on the opcode of OP0.  If none of the cases
10610          does a "continue", we exit this loop immediately after the
10611          switch.  */
10612
10613       switch (GET_CODE (op0))
10614         {
10615         case ZERO_EXTRACT:
10616           /* If we are extracting a single bit from a variable position in
10617              a constant that has only a single bit set and are comparing it
10618              with zero, we can convert this into an equality comparison
10619              between the position and the location of the single bit.  */
10620
10621           if (GET_CODE (XEXP (op0, 0)) == CONST_INT
10622               && XEXP (op0, 1) == const1_rtx
10623               && equality_comparison_p && const_op == 0
10624               && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
10625             {
10626               if (BITS_BIG_ENDIAN)
10627                 {
10628                   enum machine_mode new_mode
10629                     = mode_for_extraction (EP_extzv, 1);
10630                   if (new_mode == MAX_MACHINE_MODE)
10631                     i = BITS_PER_WORD - 1 - i;
10632                   else
10633                     {
10634                       mode = new_mode;
10635                       i = (GET_MODE_BITSIZE (mode) - 1 - i);
10636                     }
10637                 }
10638
10639               op0 = XEXP (op0, 2);
10640               op1 = GEN_INT (i);
10641               const_op = i;
10642
10643               /* Result is nonzero iff shift count is equal to I.  */
10644               code = reverse_condition (code);
10645               continue;
10646             }
10647
10648           /* ... fall through ...  */
10649
10650         case SIGN_EXTRACT:
10651           tem = expand_compound_operation (op0);
10652           if (tem != op0)
10653             {
10654               op0 = tem;
10655               continue;
10656             }
10657           break;
10658
10659         case NOT:
10660           /* If testing for equality, we can take the NOT of the constant.  */
10661           if (equality_comparison_p
10662               && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10663             {
10664               op0 = XEXP (op0, 0);
10665               op1 = tem;
10666               continue;
10667             }
10668
10669           /* If just looking at the sign bit, reverse the sense of the
10670              comparison.  */
10671           if (sign_bit_comparison_p)
10672             {
10673               op0 = XEXP (op0, 0);
10674               code = (code == GE ? LT : GE);
10675               continue;
10676             }
10677           break;
10678
10679         case NEG:
10680           /* If testing for equality, we can take the NEG of the constant.  */
10681           if (equality_comparison_p
10682               && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10683             {
10684               op0 = XEXP (op0, 0);
10685               op1 = tem;
10686               continue;
10687             }
10688
10689           /* The remaining cases only apply to comparisons with zero.  */
10690           if (const_op != 0)
10691             break;
10692
10693           /* When X is ABS or is known positive,
10694              (neg X) is < 0 if and only if X != 0.  */
10695
10696           if (sign_bit_comparison_p
10697               && (GET_CODE (XEXP (op0, 0)) == ABS
10698                   || (mode_width <= HOST_BITS_PER_WIDE_INT
10699                       && (nonzero_bits (XEXP (op0, 0), mode)
10700                           & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10701             {
10702               op0 = XEXP (op0, 0);
10703               code = (code == LT ? NE : EQ);
10704               continue;
10705             }
10706
10707           /* If we have NEG of something whose two high-order bits are the
10708              same, we know that "(-a) < 0" is equivalent to "a > 0".  */
10709           if (num_sign_bit_copies (op0, mode) >= 2)
10710             {
10711               op0 = XEXP (op0, 0);
10712               code = swap_condition (code);
10713               continue;
10714             }
10715           break;
10716
10717         case ROTATE:
10718           /* If we are testing equality and our count is a constant, we
10719              can perform the inverse operation on our RHS.  */
10720           if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10721               && (tem = simplify_binary_operation (ROTATERT, mode,
10722                                                    op1, XEXP (op0, 1))) != 0)
10723             {
10724               op0 = XEXP (op0, 0);
10725               op1 = tem;
10726               continue;
10727             }
10728
10729           /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10730              a particular bit.  Convert it to an AND of a constant of that
10731              bit.  This will be converted into a ZERO_EXTRACT.  */
10732           if (const_op == 0 && sign_bit_comparison_p
10733               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10734               && mode_width <= HOST_BITS_PER_WIDE_INT)
10735             {
10736               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10737                                             ((HOST_WIDE_INT) 1
10738                                              << (mode_width - 1
10739                                                  - INTVAL (XEXP (op0, 1)))));
10740               code = (code == LT ? NE : EQ);
10741               continue;
10742             }
10743
10744           /* Fall through.  */
10745
10746         case ABS:
10747           /* ABS is ignorable inside an equality comparison with zero.  */
10748           if (const_op == 0 && equality_comparison_p)
10749             {
10750               op0 = XEXP (op0, 0);
10751               continue;
10752             }
10753           break;
10754
10755         case SIGN_EXTEND:
10756           /* Can simplify (compare (zero/sign_extend FOO) CONST)
10757              to (compare FOO CONST) if CONST fits in FOO's mode and we
10758              are either testing inequality or have an unsigned comparison
10759              with ZERO_EXTEND or a signed comparison with SIGN_EXTEND.  */
10760           if (! unsigned_comparison_p
10761               && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10762                   <= HOST_BITS_PER_WIDE_INT)
10763               && ((unsigned HOST_WIDE_INT) const_op
10764                   < (((unsigned HOST_WIDE_INT) 1
10765                       << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
10766             {
10767               op0 = XEXP (op0, 0);
10768               continue;
10769             }
10770           break;
10771
10772         case SUBREG:
10773           /* Check for the case where we are comparing A - C1 with C2,
10774              both constants are smaller than 1/2 the maximum positive
10775              value in MODE, and the comparison is equality or unsigned.
10776              In that case, if A is either zero-extended to MODE or has
10777              sufficient sign bits so that the high-order bit in MODE
10778              is a copy of the sign in the inner mode, we can prove that it is
10779              safe to do the operation in the wider mode.  This simplifies
10780              many range checks.  */
10781
10782           if (mode_width <= HOST_BITS_PER_WIDE_INT
10783               && subreg_lowpart_p (op0)
10784               && GET_CODE (SUBREG_REG (op0)) == PLUS
10785               && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
10786               && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
10787               && (-INTVAL (XEXP (SUBREG_REG (op0), 1))
10788                   < (HOST_WIDE_INT) (GET_MODE_MASK (mode) / 2))
10789               && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
10790               && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
10791                                       GET_MODE (SUBREG_REG (op0)))
10792                         & ~GET_MODE_MASK (mode))
10793                   || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
10794                                            GET_MODE (SUBREG_REG (op0)))
10795                       > (unsigned int)
10796                         (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10797                          - GET_MODE_BITSIZE (mode)))))
10798             {
10799               op0 = SUBREG_REG (op0);
10800               continue;
10801             }
10802
10803           /* If the inner mode is narrower and we are extracting the low part,
10804              we can treat the SUBREG as if it were a ZERO_EXTEND.  */
10805           if (subreg_lowpart_p (op0)
10806               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10807             /* Fall through */ ;
10808           else
10809             break;
10810
10811           /* ... fall through ...  */
10812
10813         case ZERO_EXTEND:
10814           if ((unsigned_comparison_p || equality_comparison_p)
10815               && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10816                   <= HOST_BITS_PER_WIDE_INT)
10817               && ((unsigned HOST_WIDE_INT) const_op
10818                   < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
10819             {
10820               op0 = XEXP (op0, 0);
10821               continue;
10822             }
10823           break;
10824
10825         case PLUS:
10826           /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
10827              this for equality comparisons due to pathological cases involving
10828              overflows.  */
10829           if (equality_comparison_p
10830               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10831                                                         op1, XEXP (op0, 1))))
10832             {
10833               op0 = XEXP (op0, 0);
10834               op1 = tem;
10835               continue;
10836             }
10837
10838           /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
10839           if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10840               && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10841             {
10842               op0 = XEXP (XEXP (op0, 0), 0);
10843               code = (code == LT ? EQ : NE);
10844               continue;
10845             }
10846           break;
10847
10848         case MINUS:
10849           /* We used to optimize signed comparisons against zero, but that
10850              was incorrect.  Unsigned comparisons against zero (GTU, LEU)
10851              arrive here as equality comparisons, or (GEU, LTU) are
10852              optimized away.  No need to special-case them.  */
10853
10854           /* (eq (minus A B) C) -> (eq A (plus B C)) or
10855              (eq B (minus A C)), whichever simplifies.  We can only do
10856              this for equality comparisons due to pathological cases involving
10857              overflows.  */
10858           if (equality_comparison_p
10859               && 0 != (tem = simplify_binary_operation (PLUS, mode,
10860                                                         XEXP (op0, 1), op1)))
10861             {
10862               op0 = XEXP (op0, 0);
10863               op1 = tem;
10864               continue;
10865             }
10866
10867           if (equality_comparison_p
10868               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10869                                                         XEXP (op0, 0), op1)))
10870             {
10871               op0 = XEXP (op0, 1);
10872               op1 = tem;
10873               continue;
10874             }
10875
10876           /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10877              of bits in X minus 1, is one iff X > 0.  */
10878           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10879               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10880               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10881                  == mode_width - 1
10882               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10883             {
10884               op0 = XEXP (op0, 1);
10885               code = (code == GE ? LE : GT);
10886               continue;
10887             }
10888           break;
10889
10890         case XOR:
10891           /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
10892              if C is zero or B is a constant.  */
10893           if (equality_comparison_p
10894               && 0 != (tem = simplify_binary_operation (XOR, mode,
10895                                                         XEXP (op0, 1), op1)))
10896             {
10897               op0 = XEXP (op0, 0);
10898               op1 = tem;
10899               continue;
10900             }
10901           break;
10902
10903         case EQ:  case NE:
10904         case UNEQ:  case LTGT:
10905         case LT:  case LTU:  case UNLT:  case LE:  case LEU:  case UNLE:
10906         case GT:  case GTU:  case UNGT:  case GE:  case GEU:  case UNGE:
10907         case UNORDERED: case ORDERED:
10908           /* We can't do anything if OP0 is a condition code value, rather
10909              than an actual data value.  */
10910           if (const_op != 0
10911               || CC0_P (XEXP (op0, 0))
10912               || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10913             break;
10914
10915           /* Get the two operands being compared.  */
10916           if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10917             tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10918           else
10919             tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10920
10921           /* Check for the cases where we simply want the result of the
10922              earlier test or the opposite of that result.  */
10923           if (code == NE || code == EQ
10924               || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10925                   && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10926                   && (STORE_FLAG_VALUE
10927                       & (((HOST_WIDE_INT) 1
10928                           << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10929                   && (code == LT || code == GE)))
10930             {
10931               enum rtx_code new_code;
10932               if (code == LT || code == NE)
10933                 new_code = GET_CODE (op0);
10934               else
10935                 new_code = combine_reversed_comparison_code (op0);
10936
10937               if (new_code != UNKNOWN)
10938                 {
10939                   code = new_code;
10940                   op0 = tem;
10941                   op1 = tem1;
10942                   continue;
10943                 }
10944             }
10945           break;
10946
10947         case IOR:
10948           /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10949              iff X <= 0.  */
10950           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10951               && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10952               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10953             {
10954               op0 = XEXP (op0, 1);
10955               code = (code == GE ? GT : LE);
10956               continue;
10957             }
10958           break;
10959
10960         case AND:
10961           /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
10962              will be converted to a ZERO_EXTRACT later.  */
10963           if (const_op == 0 && equality_comparison_p
10964               && GET_CODE (XEXP (op0, 0)) == ASHIFT
10965               && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10966             {
10967               op0 = simplify_and_const_int
10968                 (op0, mode, gen_rtx_LSHIFTRT (mode,
10969                                               XEXP (op0, 1),
10970                                               XEXP (XEXP (op0, 0), 1)),
10971                  (HOST_WIDE_INT) 1);
10972               continue;
10973             }
10974
10975           /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10976              zero and X is a comparison and C1 and C2 describe only bits set
10977              in STORE_FLAG_VALUE, we can compare with X.  */
10978           if (const_op == 0 && equality_comparison_p
10979               && mode_width <= HOST_BITS_PER_WIDE_INT
10980               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10981               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10982               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10983               && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10984               && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10985             {
10986               mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10987                       << INTVAL (XEXP (XEXP (op0, 0), 1)));
10988               if ((~STORE_FLAG_VALUE & mask) == 0
10989                   && (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (op0, 0), 0))) == '<'
10990                       || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10991                           && GET_RTX_CLASS (GET_CODE (tem)) == '<')))
10992                 {
10993                   op0 = XEXP (XEXP (op0, 0), 0);
10994                   continue;
10995                 }
10996             }
10997
10998           /* If we are doing an equality comparison of an AND of a bit equal
10999              to the sign bit, replace this with a LT or GE comparison of
11000              the underlying value.  */
11001           if (equality_comparison_p
11002               && const_op == 0
11003               && GET_CODE (XEXP (op0, 1)) == CONST_INT
11004               && mode_width <= HOST_BITS_PER_WIDE_INT
11005               && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11006                   == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11007             {
11008               op0 = XEXP (op0, 0);
11009               code = (code == EQ ? GE : LT);
11010               continue;
11011             }
11012
11013           /* If this AND operation is really a ZERO_EXTEND from a narrower
11014              mode, the constant fits within that mode, and this is either an
11015              equality or unsigned comparison, try to do this comparison in
11016              the narrower mode.  */
11017           if ((equality_comparison_p || unsigned_comparison_p)
11018               && GET_CODE (XEXP (op0, 1)) == CONST_INT
11019               && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
11020                                    & GET_MODE_MASK (mode))
11021                                   + 1)) >= 0
11022               && const_op >> i == 0
11023               && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
11024             {
11025               op0 = gen_lowpart_for_combine (tmode, XEXP (op0, 0));
11026               continue;
11027             }
11028
11029           /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11030              fits in both M1 and M2 and the SUBREG is either paradoxical
11031              or represents the low part, permute the SUBREG and the AND
11032              and try again.  */
11033           if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11034             {
11035               unsigned HOST_WIDE_INT c1;
11036               tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11037               /* Require an integral mode, to avoid creating something like
11038                  (AND:SF ...).  */
11039               if (SCALAR_INT_MODE_P (tmode)
11040                   /* It is unsafe to commute the AND into the SUBREG if the
11041                      SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11042                      not defined.  As originally written the upper bits
11043                      have a defined value due to the AND operation.
11044                      However, if we commute the AND inside the SUBREG then
11045                      they no longer have defined values and the meaning of
11046                      the code has been changed.  */
11047                   && (0
11048 #ifdef WORD_REGISTER_OPERATIONS
11049                       || (mode_width > GET_MODE_BITSIZE (tmode)
11050                           && mode_width <= BITS_PER_WORD)
11051 #endif
11052                       || (mode_width <= GET_MODE_BITSIZE (tmode)
11053                           && subreg_lowpart_p (XEXP (op0, 0))))
11054                   && GET_CODE (XEXP (op0, 1)) == CONST_INT
11055                   && mode_width <= HOST_BITS_PER_WIDE_INT
11056                   && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
11057                   && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11058                   && (c1 & ~GET_MODE_MASK (tmode)) == 0
11059                   && c1 != mask
11060                   && c1 != GET_MODE_MASK (tmode))
11061                 {
11062                   op0 = gen_binary (AND, tmode,
11063                                     SUBREG_REG (XEXP (op0, 0)),
11064                                     gen_int_mode (c1, tmode));
11065                   op0 = gen_lowpart_for_combine (mode, op0);
11066                   continue;
11067                 }
11068             }
11069
11070           /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0).  */
11071           if (const_op == 0 && equality_comparison_p
11072               && XEXP (op0, 1) == const1_rtx
11073               && GET_CODE (XEXP (op0, 0)) == NOT)
11074             {
11075               op0 = simplify_and_const_int
11076                 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
11077               code = (code == NE ? EQ : NE);
11078               continue;
11079             }
11080
11081           /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11082              (eq (and (lshiftrt X) 1) 0).  */
11083           if (const_op == 0 && equality_comparison_p
11084               && XEXP (op0, 1) == const1_rtx
11085               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11086               && GET_CODE (XEXP (XEXP (op0, 0), 0)) == NOT)
11087             {
11088               op0 = simplify_and_const_int
11089                 (op0, mode,
11090                  gen_rtx_LSHIFTRT (mode, XEXP (XEXP (XEXP (op0, 0), 0), 0),
11091                                    XEXP (XEXP (op0, 0), 1)),
11092                  (HOST_WIDE_INT) 1);
11093               code = (code == NE ? EQ : NE);
11094               continue;
11095             }
11096           break;
11097
11098         case ASHIFT:
11099           /* If we have (compare (ashift FOO N) (const_int C)) and
11100              the high order N bits of FOO (N+1 if an inequality comparison)
11101              are known to be zero, we can do this by comparing FOO with C
11102              shifted right N bits so long as the low-order N bits of C are
11103              zero.  */
11104           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
11105               && INTVAL (XEXP (op0, 1)) >= 0
11106               && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11107                   < HOST_BITS_PER_WIDE_INT)
11108               && ((const_op
11109                    & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
11110               && mode_width <= HOST_BITS_PER_WIDE_INT
11111               && (nonzero_bits (XEXP (op0, 0), mode)
11112                   & ~(mask >> (INTVAL (XEXP (op0, 1))
11113                                + ! equality_comparison_p))) == 0)
11114             {
11115               /* We must perform a logical shift, not an arithmetic one,
11116                  as we want the top N bits of C to be zero.  */
11117               unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11118
11119               temp >>= INTVAL (XEXP (op0, 1));
11120               op1 = gen_int_mode (temp, mode);
11121               op0 = XEXP (op0, 0);
11122               continue;
11123             }
11124
11125           /* If we are doing a sign bit comparison, it means we are testing
11126              a particular bit.  Convert it to the appropriate AND.  */
11127           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
11128               && mode_width <= HOST_BITS_PER_WIDE_INT)
11129             {
11130               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11131                                             ((HOST_WIDE_INT) 1
11132                                              << (mode_width - 1
11133                                                  - INTVAL (XEXP (op0, 1)))));
11134               code = (code == LT ? NE : EQ);
11135               continue;
11136             }
11137
11138           /* If this an equality comparison with zero and we are shifting
11139              the low bit to the sign bit, we can convert this to an AND of the
11140              low-order bit.  */
11141           if (const_op == 0 && equality_comparison_p
11142               && GET_CODE (XEXP (op0, 1)) == CONST_INT
11143               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11144                  == mode_width - 1)
11145             {
11146               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11147                                             (HOST_WIDE_INT) 1);
11148               continue;
11149             }
11150           break;
11151
11152         case ASHIFTRT:
11153           /* If this is an equality comparison with zero, we can do this
11154              as a logical shift, which might be much simpler.  */
11155           if (equality_comparison_p && const_op == 0
11156               && GET_CODE (XEXP (op0, 1)) == CONST_INT)
11157             {
11158               op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11159                                           XEXP (op0, 0),
11160                                           INTVAL (XEXP (op0, 1)));
11161               continue;
11162             }
11163
11164           /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11165              do the comparison in a narrower mode.  */
11166           if (! unsigned_comparison_p
11167               && GET_CODE (XEXP (op0, 1)) == CONST_INT
11168               && GET_CODE (XEXP (op0, 0)) == ASHIFT
11169               && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11170               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11171                                          MODE_INT, 1)) != BLKmode
11172               && (((unsigned HOST_WIDE_INT) const_op
11173                    + (GET_MODE_MASK (tmode) >> 1) + 1)
11174                   <= GET_MODE_MASK (tmode)))
11175             {
11176               op0 = gen_lowpart_for_combine (tmode, XEXP (XEXP (op0, 0), 0));
11177               continue;
11178             }
11179
11180           /* Likewise if OP0 is a PLUS of a sign extension with a
11181              constant, which is usually represented with the PLUS
11182              between the shifts.  */
11183           if (! unsigned_comparison_p
11184               && GET_CODE (XEXP (op0, 1)) == CONST_INT
11185               && GET_CODE (XEXP (op0, 0)) == PLUS
11186               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
11187               && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11188               && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11189               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11190                                          MODE_INT, 1)) != BLKmode
11191               && (((unsigned HOST_WIDE_INT) const_op
11192                    + (GET_MODE_MASK (tmode) >> 1) + 1)
11193                   <= GET_MODE_MASK (tmode)))
11194             {
11195               rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11196               rtx add_const = XEXP (XEXP (op0, 0), 1);
11197               rtx new_const = gen_binary (ASHIFTRT, GET_MODE (op0), add_const,
11198                                           XEXP (op0, 1));
11199
11200               op0 = gen_binary (PLUS, tmode,
11201                                 gen_lowpart_for_combine (tmode, inner),
11202                                 new_const);
11203               continue;
11204             }
11205
11206           /* ... fall through ...  */
11207         case LSHIFTRT:
11208           /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11209              the low order N bits of FOO are known to be zero, we can do this
11210              by comparing FOO with C shifted left N bits so long as no
11211              overflow occurs.  */
11212           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
11213               && INTVAL (XEXP (op0, 1)) >= 0
11214               && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11215               && mode_width <= HOST_BITS_PER_WIDE_INT
11216               && (nonzero_bits (XEXP (op0, 0), mode)
11217                   & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
11218               && (((unsigned HOST_WIDE_INT) const_op
11219                    + (GET_CODE (op0) != LSHIFTRT
11220                       ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11221                          + 1)
11222                       : 0))
11223                   <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11224             {
11225               /* If the shift was logical, then we must make the condition
11226                  unsigned.  */
11227               if (GET_CODE (op0) == LSHIFTRT)
11228                 code = unsigned_condition (code);
11229
11230               const_op <<= INTVAL (XEXP (op0, 1));
11231               op1 = GEN_INT (const_op);
11232               op0 = XEXP (op0, 0);
11233               continue;
11234             }
11235
11236           /* If we are using this shift to extract just the sign bit, we
11237              can replace this with an LT or GE comparison.  */
11238           if (const_op == 0
11239               && (equality_comparison_p || sign_bit_comparison_p)
11240               && GET_CODE (XEXP (op0, 1)) == CONST_INT
11241               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11242                  == mode_width - 1)
11243             {
11244               op0 = XEXP (op0, 0);
11245               code = (code == NE || code == GT ? LT : GE);
11246               continue;
11247             }
11248           break;
11249
11250         default:
11251           break;
11252         }
11253
11254       break;
11255     }
11256
11257   /* Now make any compound operations involved in this comparison.  Then,
11258      check for an outmost SUBREG on OP0 that is not doing anything or is
11259      paradoxical.  The latter transformation must only be performed when
11260      it is known that the "extra" bits will be the same in op0 and op1 or
11261      that they don't matter.  There are three cases to consider:
11262
11263      1. SUBREG_REG (op0) is a register.  In this case the bits are don't
11264      care bits and we can assume they have any convenient value.  So
11265      making the transformation is safe.
11266
11267      2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11268      In this case the upper bits of op0 are undefined.  We should not make
11269      the simplification in that case as we do not know the contents of
11270      those bits.
11271
11272      3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11273      NIL.  In that case we know those bits are zeros or ones.  We must
11274      also be sure that they are the same as the upper bits of op1.
11275
11276      We can never remove a SUBREG for a non-equality comparison because
11277      the sign bit is in a different place in the underlying object.  */
11278
11279   op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11280   op1 = make_compound_operation (op1, SET);
11281
11282   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11283       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11284       && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11285       && (code == NE || code == EQ))
11286     {
11287       if (GET_MODE_SIZE (GET_MODE (op0))
11288           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
11289         {
11290           /* For paradoxical subregs, allow case 1 as above.  Case 3 isn't
11291              implemented.  */
11292           if (GET_CODE (SUBREG_REG (op0)) == REG)
11293             {
11294               op0 = SUBREG_REG (op0);
11295               op1 = gen_lowpart_for_combine (GET_MODE (op0), op1);
11296             }
11297         }
11298       else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11299                 <= HOST_BITS_PER_WIDE_INT)
11300                && (nonzero_bits (SUBREG_REG (op0),
11301                                  GET_MODE (SUBREG_REG (op0)))
11302                    & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11303         {
11304           tem = gen_lowpart_for_combine (GET_MODE (SUBREG_REG (op0)), op1);
11305
11306           if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11307                & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11308             op0 = SUBREG_REG (op0), op1 = tem;
11309         }
11310     }
11311
11312   /* We now do the opposite procedure: Some machines don't have compare
11313      insns in all modes.  If OP0's mode is an integer mode smaller than a
11314      word and we can't do a compare in that mode, see if there is a larger
11315      mode for which we can do the compare.  There are a number of cases in
11316      which we can use the wider mode.  */
11317
11318   mode = GET_MODE (op0);
11319   if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11320       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11321       && ! have_insn_for (COMPARE, mode))
11322     for (tmode = GET_MODE_WIDER_MODE (mode);
11323          (tmode != VOIDmode
11324           && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11325          tmode = GET_MODE_WIDER_MODE (tmode))
11326       if (have_insn_for (COMPARE, tmode))
11327         {
11328           int zero_extended;
11329
11330           /* If the only nonzero bits in OP0 and OP1 are those in the
11331              narrower mode and this is an equality or unsigned comparison,
11332              we can use the wider mode.  Similarly for sign-extended
11333              values, in which case it is true for all comparisons.  */
11334           zero_extended = ((code == EQ || code == NE
11335                             || code == GEU || code == GTU
11336                             || code == LEU || code == LTU)
11337                            && (nonzero_bits (op0, tmode)
11338                                & ~GET_MODE_MASK (mode)) == 0
11339                            && ((GET_CODE (op1) == CONST_INT
11340                                 || (nonzero_bits (op1, tmode)
11341                                     & ~GET_MODE_MASK (mode)) == 0)));
11342
11343           if (zero_extended
11344               || ((num_sign_bit_copies (op0, tmode)
11345                    > (unsigned int) (GET_MODE_BITSIZE (tmode)
11346                                      - GET_MODE_BITSIZE (mode)))
11347                   && (num_sign_bit_copies (op1, tmode)
11348                       > (unsigned int) (GET_MODE_BITSIZE (tmode)
11349                                         - GET_MODE_BITSIZE (mode)))))
11350             {
11351               /* If OP0 is an AND and we don't have an AND in MODE either,
11352                  make a new AND in the proper mode.  */
11353               if (GET_CODE (op0) == AND
11354                   && !have_insn_for (AND, mode))
11355                 op0 = gen_binary (AND, tmode,
11356                                   gen_lowpart_for_combine (tmode,
11357                                                            XEXP (op0, 0)),
11358                                   gen_lowpart_for_combine (tmode,
11359                                                            XEXP (op0, 1)));
11360
11361               op0 = gen_lowpart_for_combine (tmode, op0);
11362               if (zero_extended && GET_CODE (op1) == CONST_INT)
11363                 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
11364               op1 = gen_lowpart_for_combine (tmode, op1);
11365               break;
11366             }
11367
11368           /* If this is a test for negative, we can make an explicit
11369              test of the sign bit.  */
11370
11371           if (op1 == const0_rtx && (code == LT || code == GE)
11372               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11373             {
11374               op0 = gen_binary (AND, tmode,
11375                                 gen_lowpart_for_combine (tmode, op0),
11376                                 GEN_INT ((HOST_WIDE_INT) 1
11377                                          << (GET_MODE_BITSIZE (mode) - 1)));
11378               code = (code == LT) ? NE : EQ;
11379               break;
11380             }
11381         }
11382
11383 #ifdef CANONICALIZE_COMPARISON
11384   /* If this machine only supports a subset of valid comparisons, see if we
11385      can convert an unsupported one into a supported one.  */
11386   CANONICALIZE_COMPARISON (code, op0, op1);
11387 #endif
11388
11389   *pop0 = op0;
11390   *pop1 = op1;
11391
11392   return code;
11393 }
11394 \f
11395 /* Like jump.c' reversed_comparison_code, but use combine infrastructure for
11396    searching backward.  */
11397 static enum rtx_code
11398 combine_reversed_comparison_code (rtx exp)
11399 {
11400   enum rtx_code code1 = reversed_comparison_code (exp, NULL);
11401   rtx x;
11402
11403   if (code1 != UNKNOWN
11404       || GET_MODE_CLASS (GET_MODE (XEXP (exp, 0))) != MODE_CC)
11405     return code1;
11406   /* Otherwise try and find where the condition codes were last set and
11407      use that.  */
11408   x = get_last_value (XEXP (exp, 0));
11409   if (!x || GET_CODE (x) != COMPARE)
11410     return UNKNOWN;
11411   return reversed_comparison_code_parts (GET_CODE (exp),
11412                                          XEXP (x, 0), XEXP (x, 1), NULL);
11413 }
11414
11415 /* Return comparison with reversed code of EXP and operands OP0 and OP1.
11416    Return NULL_RTX in case we fail to do the reversal.  */
11417 static rtx
11418 reversed_comparison (rtx exp, enum machine_mode mode, rtx op0, rtx op1)
11419 {
11420   enum rtx_code reversed_code = combine_reversed_comparison_code (exp);
11421   if (reversed_code == UNKNOWN)
11422     return NULL_RTX;
11423   else
11424     return gen_binary (reversed_code, mode, op0, op1);
11425 }
11426 \f
11427 /* Utility function for following routine.  Called when X is part of a value
11428    being stored into reg_last_set_value.  Sets reg_last_set_table_tick
11429    for each register mentioned.  Similar to mention_regs in cse.c  */
11430
11431 static void
11432 update_table_tick (rtx x)
11433 {
11434   enum rtx_code code = GET_CODE (x);
11435   const char *fmt = GET_RTX_FORMAT (code);
11436   int i;
11437
11438   if (code == REG)
11439     {
11440       unsigned int regno = REGNO (x);
11441       unsigned int endregno
11442         = regno + (regno < FIRST_PSEUDO_REGISTER
11443                    ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
11444       unsigned int r;
11445
11446       for (r = regno; r < endregno; r++)
11447         reg_last_set_table_tick[r] = label_tick;
11448
11449       return;
11450     }
11451
11452   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11453     /* Note that we can't have an "E" in values stored; see
11454        get_last_value_validate.  */
11455     if (fmt[i] == 'e')
11456       {
11457         /* Check for identical subexpressions.  If x contains
11458            identical subexpression we only have to traverse one of
11459            them.  */
11460         if (i == 0
11461             && (GET_RTX_CLASS (code) == '2'
11462                 || GET_RTX_CLASS (code) == 'c'))
11463           {
11464             /* Note that at this point x1 has already been
11465                processed.  */
11466             rtx x0 = XEXP (x, 0);
11467             rtx x1 = XEXP (x, 1);
11468
11469             /* If x0 and x1 are identical then there is no need to
11470                process x0.  */
11471             if (x0 == x1)
11472               break;
11473
11474             /* If x0 is identical to a subexpression of x1 then while
11475                processing x1, x0 has already been processed.  Thus we
11476                are done with x.  */
11477             if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11478                  || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11479                 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11480               break;
11481
11482             /* If x1 is identical to a subexpression of x0 then we
11483                still have to process the rest of x0.  */
11484             if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11485                  || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11486                 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11487               {
11488                 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
11489                 break;
11490               }
11491           }
11492
11493         update_table_tick (XEXP (x, i));
11494       }
11495 }
11496
11497 /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
11498    are saying that the register is clobbered and we no longer know its
11499    value.  If INSN is zero, don't update reg_last_set; this is only permitted
11500    with VALUE also zero and is used to invalidate the register.  */
11501
11502 static void
11503 record_value_for_reg (rtx reg, rtx insn, rtx value)
11504 {
11505   unsigned int regno = REGNO (reg);
11506   unsigned int endregno
11507     = regno + (regno < FIRST_PSEUDO_REGISTER
11508                ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
11509   unsigned int i;
11510
11511   /* If VALUE contains REG and we have a previous value for REG, substitute
11512      the previous value.  */
11513   if (value && insn && reg_overlap_mentioned_p (reg, value))
11514     {
11515       rtx tem;
11516
11517       /* Set things up so get_last_value is allowed to see anything set up to
11518          our insn.  */
11519       subst_low_cuid = INSN_CUID (insn);
11520       tem = get_last_value (reg);
11521
11522       /* If TEM is simply a binary operation with two CLOBBERs as operands,
11523          it isn't going to be useful and will take a lot of time to process,
11524          so just use the CLOBBER.  */
11525
11526       if (tem)
11527         {
11528           if ((GET_RTX_CLASS (GET_CODE (tem)) == '2'
11529                || GET_RTX_CLASS (GET_CODE (tem)) == 'c')
11530               && GET_CODE (XEXP (tem, 0)) == CLOBBER
11531               && GET_CODE (XEXP (tem, 1)) == CLOBBER)
11532             tem = XEXP (tem, 0);
11533
11534           value = replace_rtx (copy_rtx (value), reg, tem);
11535         }
11536     }
11537
11538   /* For each register modified, show we don't know its value, that
11539      we don't know about its bitwise content, that its value has been
11540      updated, and that we don't know the location of the death of the
11541      register.  */
11542   for (i = regno; i < endregno; i++)
11543     {
11544       if (insn)
11545         reg_last_set[i] = insn;
11546
11547       reg_last_set_value[i] = 0;
11548       reg_last_set_mode[i] = 0;
11549       reg_last_set_nonzero_bits[i] = 0;
11550       reg_last_set_sign_bit_copies[i] = 0;
11551       reg_last_death[i] = 0;
11552     }
11553
11554   /* Mark registers that are being referenced in this value.  */
11555   if (value)
11556     update_table_tick (value);
11557
11558   /* Now update the status of each register being set.
11559      If someone is using this register in this block, set this register
11560      to invalid since we will get confused between the two lives in this
11561      basic block.  This makes using this register always invalid.  In cse, we
11562      scan the table to invalidate all entries using this register, but this
11563      is too much work for us.  */
11564
11565   for (i = regno; i < endregno; i++)
11566     {
11567       reg_last_set_label[i] = label_tick;
11568       if (value && reg_last_set_table_tick[i] == label_tick)
11569         reg_last_set_invalid[i] = 1;
11570       else
11571         reg_last_set_invalid[i] = 0;
11572     }
11573
11574   /* The value being assigned might refer to X (like in "x++;").  In that
11575      case, we must replace it with (clobber (const_int 0)) to prevent
11576      infinite loops.  */
11577   if (value && ! get_last_value_validate (&value, insn,
11578                                           reg_last_set_label[regno], 0))
11579     {
11580       value = copy_rtx (value);
11581       if (! get_last_value_validate (&value, insn,
11582                                      reg_last_set_label[regno], 1))
11583         value = 0;
11584     }
11585
11586   /* For the main register being modified, update the value, the mode, the
11587      nonzero bits, and the number of sign bit copies.  */
11588
11589   reg_last_set_value[regno] = value;
11590
11591   if (value)
11592     {
11593       enum machine_mode mode = GET_MODE (reg);
11594       subst_low_cuid = INSN_CUID (insn);
11595       reg_last_set_mode[regno] = mode;
11596       if (GET_MODE_CLASS (mode) == MODE_INT
11597           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11598         mode = nonzero_bits_mode;
11599       reg_last_set_nonzero_bits[regno] = nonzero_bits (value, mode);
11600       reg_last_set_sign_bit_copies[regno]
11601         = num_sign_bit_copies (value, GET_MODE (reg));
11602     }
11603 }
11604
11605 /* Called via note_stores from record_dead_and_set_regs to handle one
11606    SET or CLOBBER in an insn.  DATA is the instruction in which the
11607    set is occurring.  */
11608
11609 static void
11610 record_dead_and_set_regs_1 (rtx dest, rtx setter, void *data)
11611 {
11612   rtx record_dead_insn = (rtx) data;
11613
11614   if (GET_CODE (dest) == SUBREG)
11615     dest = SUBREG_REG (dest);
11616
11617   if (GET_CODE (dest) == REG)
11618     {
11619       /* If we are setting the whole register, we know its value.  Otherwise
11620          show that we don't know the value.  We can handle SUBREG in
11621          some cases.  */
11622       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11623         record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11624       else if (GET_CODE (setter) == SET
11625                && GET_CODE (SET_DEST (setter)) == SUBREG
11626                && SUBREG_REG (SET_DEST (setter)) == dest
11627                && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11628                && subreg_lowpart_p (SET_DEST (setter)))
11629         record_value_for_reg (dest, record_dead_insn,
11630                               gen_lowpart_for_combine (GET_MODE (dest),
11631                                                        SET_SRC (setter)));
11632       else
11633         record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11634     }
11635   else if (GET_CODE (dest) == MEM
11636            /* Ignore pushes, they clobber nothing.  */
11637            && ! push_operand (dest, GET_MODE (dest)))
11638     mem_last_set = INSN_CUID (record_dead_insn);
11639 }
11640
11641 /* Update the records of when each REG was most recently set or killed
11642    for the things done by INSN.  This is the last thing done in processing
11643    INSN in the combiner loop.
11644
11645    We update reg_last_set, reg_last_set_value, reg_last_set_mode,
11646    reg_last_set_nonzero_bits, reg_last_set_sign_bit_copies, reg_last_death,
11647    and also the similar information mem_last_set (which insn most recently
11648    modified memory) and last_call_cuid (which insn was the most recent
11649    subroutine call).  */
11650
11651 static void
11652 record_dead_and_set_regs (rtx insn)
11653 {
11654   rtx link;
11655   unsigned int i;
11656
11657   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11658     {
11659       if (REG_NOTE_KIND (link) == REG_DEAD
11660           && GET_CODE (XEXP (link, 0)) == REG)
11661         {
11662           unsigned int regno = REGNO (XEXP (link, 0));
11663           unsigned int endregno
11664             = regno + (regno < FIRST_PSEUDO_REGISTER
11665                        ? HARD_REGNO_NREGS (regno, GET_MODE (XEXP (link, 0)))
11666                        : 1);
11667
11668           for (i = regno; i < endregno; i++)
11669             reg_last_death[i] = insn;
11670         }
11671       else if (REG_NOTE_KIND (link) == REG_INC)
11672         record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11673     }
11674
11675   if (GET_CODE (insn) == CALL_INSN)
11676     {
11677       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11678         if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11679           {
11680             reg_last_set_value[i] = 0;
11681             reg_last_set_mode[i] = 0;
11682             reg_last_set_nonzero_bits[i] = 0;
11683             reg_last_set_sign_bit_copies[i] = 0;
11684             reg_last_death[i] = 0;
11685           }
11686
11687       last_call_cuid = mem_last_set = INSN_CUID (insn);
11688
11689       /* Don't bother recording what this insn does.  It might set the
11690          return value register, but we can't combine into a call
11691          pattern anyway, so there's no point trying (and it may cause
11692          a crash, if e.g. we wind up asking for last_set_value of a
11693          SUBREG of the return value register).  */
11694       return;
11695     }
11696
11697   note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11698 }
11699
11700 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11701    register present in the SUBREG, so for each such SUBREG go back and
11702    adjust nonzero and sign bit information of the registers that are
11703    known to have some zero/sign bits set.
11704
11705    This is needed because when combine blows the SUBREGs away, the
11706    information on zero/sign bits is lost and further combines can be
11707    missed because of that.  */
11708
11709 static void
11710 record_promoted_value (rtx insn, rtx subreg)
11711 {
11712   rtx links, set;
11713   unsigned int regno = REGNO (SUBREG_REG (subreg));
11714   enum machine_mode mode = GET_MODE (subreg);
11715
11716   if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11717     return;
11718
11719   for (links = LOG_LINKS (insn); links;)
11720     {
11721       insn = XEXP (links, 0);
11722       set = single_set (insn);
11723
11724       if (! set || GET_CODE (SET_DEST (set)) != REG
11725           || REGNO (SET_DEST (set)) != regno
11726           || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11727         {
11728           links = XEXP (links, 1);
11729           continue;
11730         }
11731
11732       if (reg_last_set[regno] == insn)
11733         {
11734           if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11735             reg_last_set_nonzero_bits[regno] &= GET_MODE_MASK (mode);
11736         }
11737
11738       if (GET_CODE (SET_SRC (set)) == REG)
11739         {
11740           regno = REGNO (SET_SRC (set));
11741           links = LOG_LINKS (insn);
11742         }
11743       else
11744         break;
11745     }
11746 }
11747
11748 /* Scan X for promoted SUBREGs.  For each one found,
11749    note what it implies to the registers used in it.  */
11750
11751 static void
11752 check_promoted_subreg (rtx insn, rtx x)
11753 {
11754   if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
11755       && GET_CODE (SUBREG_REG (x)) == REG)
11756     record_promoted_value (insn, x);
11757   else
11758     {
11759       const char *format = GET_RTX_FORMAT (GET_CODE (x));
11760       int i, j;
11761
11762       for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11763         switch (format[i])
11764           {
11765           case 'e':
11766             check_promoted_subreg (insn, XEXP (x, i));
11767             break;
11768           case 'V':
11769           case 'E':
11770             if (XVEC (x, i) != 0)
11771               for (j = 0; j < XVECLEN (x, i); j++)
11772                 check_promoted_subreg (insn, XVECEXP (x, i, j));
11773             break;
11774           }
11775     }
11776 }
11777 \f
11778 /* Utility routine for the following function.  Verify that all the registers
11779    mentioned in *LOC are valid when *LOC was part of a value set when
11780    label_tick == TICK.  Return 0 if some are not.
11781
11782    If REPLACE is nonzero, replace the invalid reference with
11783    (clobber (const_int 0)) and return 1.  This replacement is useful because
11784    we often can get useful information about the form of a value (e.g., if
11785    it was produced by a shift that always produces -1 or 0) even though
11786    we don't know exactly what registers it was produced from.  */
11787
11788 static int
11789 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
11790 {
11791   rtx x = *loc;
11792   const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11793   int len = GET_RTX_LENGTH (GET_CODE (x));
11794   int i;
11795
11796   if (GET_CODE (x) == REG)
11797     {
11798       unsigned int regno = REGNO (x);
11799       unsigned int endregno
11800         = regno + (regno < FIRST_PSEUDO_REGISTER
11801                    ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
11802       unsigned int j;
11803
11804       for (j = regno; j < endregno; j++)
11805         if (reg_last_set_invalid[j]
11806             /* If this is a pseudo-register that was only set once and not
11807                live at the beginning of the function, it is always valid.  */
11808             || (! (regno >= FIRST_PSEUDO_REGISTER
11809                    && REG_N_SETS (regno) == 1
11810                    && (! REGNO_REG_SET_P
11811                        (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))
11812                 && reg_last_set_label[j] > tick))
11813           {
11814             if (replace)
11815               *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11816             return replace;
11817           }
11818
11819       return 1;
11820     }
11821   /* If this is a memory reference, make sure that there were
11822      no stores after it that might have clobbered the value.  We don't
11823      have alias info, so we assume any store invalidates it.  */
11824   else if (GET_CODE (x) == MEM && ! RTX_UNCHANGING_P (x)
11825            && INSN_CUID (insn) <= mem_last_set)
11826     {
11827       if (replace)
11828         *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11829       return replace;
11830     }
11831
11832   for (i = 0; i < len; i++)
11833     {
11834       if (fmt[i] == 'e')
11835         {
11836           /* Check for identical subexpressions.  If x contains
11837              identical subexpression we only have to traverse one of
11838              them.  */
11839           if (i == 1
11840               && (GET_RTX_CLASS (GET_CODE (x)) == '2'
11841                   || GET_RTX_CLASS (GET_CODE (x)) == 'c'))
11842             {
11843               /* Note that at this point x0 has already been checked
11844                  and found valid.  */
11845               rtx x0 = XEXP (x, 0);
11846               rtx x1 = XEXP (x, 1);
11847
11848               /* If x0 and x1 are identical then x is also valid.  */
11849               if (x0 == x1)
11850                 return 1;
11851
11852               /* If x1 is identical to a subexpression of x0 then
11853                  while checking x0, x1 has already been checked.  Thus
11854                  it is valid and so as x.  */
11855               if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11856                    || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11857                   && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11858                 return 1;
11859
11860               /* If x0 is identical to a subexpression of x1 then x is
11861                  valid iff the rest of x1 is valid.  */
11862               if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11863                    || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11864                   && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11865                 return
11866                   get_last_value_validate (&XEXP (x1,
11867                                                   x0 == XEXP (x1, 0) ? 1 : 0),
11868                                            insn, tick, replace);
11869             }
11870
11871           if (get_last_value_validate (&XEXP (x, i), insn, tick,
11872                                        replace) == 0)
11873             return 0;
11874         }
11875       /* Don't bother with these.  They shouldn't occur anyway.  */
11876       else if (fmt[i] == 'E')
11877         return 0;
11878     }
11879
11880   /* If we haven't found a reason for it to be invalid, it is valid.  */
11881   return 1;
11882 }
11883
11884 /* Get the last value assigned to X, if known.  Some registers
11885    in the value may be replaced with (clobber (const_int 0)) if their value
11886    is known longer known reliably.  */
11887
11888 static rtx
11889 get_last_value (rtx x)
11890 {
11891   unsigned int regno;
11892   rtx value;
11893
11894   /* If this is a non-paradoxical SUBREG, get the value of its operand and
11895      then convert it to the desired mode.  If this is a paradoxical SUBREG,
11896      we cannot predict what values the "extra" bits might have.  */
11897   if (GET_CODE (x) == SUBREG
11898       && subreg_lowpart_p (x)
11899       && (GET_MODE_SIZE (GET_MODE (x))
11900           <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11901       && (value = get_last_value (SUBREG_REG (x))) != 0)
11902     return gen_lowpart_for_combine (GET_MODE (x), value);
11903
11904   if (GET_CODE (x) != REG)
11905     return 0;
11906
11907   regno = REGNO (x);
11908   value = reg_last_set_value[regno];
11909
11910   /* If we don't have a value, or if it isn't for this basic block and
11911      it's either a hard register, set more than once, or it's a live
11912      at the beginning of the function, return 0.
11913
11914      Because if it's not live at the beginning of the function then the reg
11915      is always set before being used (is never used without being set).
11916      And, if it's set only once, and it's always set before use, then all
11917      uses must have the same last value, even if it's not from this basic
11918      block.  */
11919
11920   if (value == 0
11921       || (reg_last_set_label[regno] != label_tick
11922           && (regno < FIRST_PSEUDO_REGISTER
11923               || REG_N_SETS (regno) != 1
11924               || (REGNO_REG_SET_P
11925                   (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))))
11926     return 0;
11927
11928   /* If the value was set in a later insn than the ones we are processing,
11929      we can't use it even if the register was only set once.  */
11930   if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
11931     return 0;
11932
11933   /* If the value has all its registers valid, return it.  */
11934   if (get_last_value_validate (&value, reg_last_set[regno],
11935                                reg_last_set_label[regno], 0))
11936     return value;
11937
11938   /* Otherwise, make a copy and replace any invalid register with
11939      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
11940
11941   value = copy_rtx (value);
11942   if (get_last_value_validate (&value, reg_last_set[regno],
11943                                reg_last_set_label[regno], 1))
11944     return value;
11945
11946   return 0;
11947 }
11948 \f
11949 /* Return nonzero if expression X refers to a REG or to memory
11950    that is set in an instruction more recent than FROM_CUID.  */
11951
11952 static int
11953 use_crosses_set_p (rtx x, int from_cuid)
11954 {
11955   const char *fmt;
11956   int i;
11957   enum rtx_code code = GET_CODE (x);
11958
11959   if (code == REG)
11960     {
11961       unsigned int regno = REGNO (x);
11962       unsigned endreg = regno + (regno < FIRST_PSEUDO_REGISTER
11963                                  ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
11964
11965 #ifdef PUSH_ROUNDING
11966       /* Don't allow uses of the stack pointer to be moved,
11967          because we don't know whether the move crosses a push insn.  */
11968       if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11969         return 1;
11970 #endif
11971       for (; regno < endreg; regno++)
11972         if (reg_last_set[regno]
11973             && INSN_CUID (reg_last_set[regno]) > from_cuid)
11974           return 1;
11975       return 0;
11976     }
11977
11978   if (code == MEM && mem_last_set > from_cuid)
11979     return 1;
11980
11981   fmt = GET_RTX_FORMAT (code);
11982
11983   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11984     {
11985       if (fmt[i] == 'E')
11986         {
11987           int j;
11988           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11989             if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
11990               return 1;
11991         }
11992       else if (fmt[i] == 'e'
11993                && use_crosses_set_p (XEXP (x, i), from_cuid))
11994         return 1;
11995     }
11996   return 0;
11997 }
11998 \f
11999 /* Define three variables used for communication between the following
12000    routines.  */
12001
12002 static unsigned int reg_dead_regno, reg_dead_endregno;
12003 static int reg_dead_flag;
12004
12005 /* Function called via note_stores from reg_dead_at_p.
12006
12007    If DEST is within [reg_dead_regno, reg_dead_endregno), set
12008    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
12009
12010 static void
12011 reg_dead_at_p_1 (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
12012 {
12013   unsigned int regno, endregno;
12014
12015   if (GET_CODE (dest) != REG)
12016     return;
12017
12018   regno = REGNO (dest);
12019   endregno = regno + (regno < FIRST_PSEUDO_REGISTER
12020                       ? HARD_REGNO_NREGS (regno, GET_MODE (dest)) : 1);
12021
12022   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12023     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12024 }
12025
12026 /* Return nonzero if REG is known to be dead at INSN.
12027
12028    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
12029    referencing REG, it is dead.  If we hit a SET referencing REG, it is
12030    live.  Otherwise, see if it is live or dead at the start of the basic
12031    block we are in.  Hard regs marked as being live in NEWPAT_USED_REGS
12032    must be assumed to be always live.  */
12033
12034 static int
12035 reg_dead_at_p (rtx reg, rtx insn)
12036 {
12037   basic_block block;
12038   unsigned int i;
12039
12040   /* Set variables for reg_dead_at_p_1.  */
12041   reg_dead_regno = REGNO (reg);
12042   reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
12043                                         ? HARD_REGNO_NREGS (reg_dead_regno,
12044                                                             GET_MODE (reg))
12045                                         : 1);
12046
12047   reg_dead_flag = 0;
12048
12049   /* Check that reg isn't mentioned in NEWPAT_USED_REGS.  */
12050   if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12051     {
12052       for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12053         if (TEST_HARD_REG_BIT (newpat_used_regs, i))
12054           return 0;
12055     }
12056
12057   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
12058      beginning of function.  */
12059   for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
12060        insn = prev_nonnote_insn (insn))
12061     {
12062       note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12063       if (reg_dead_flag)
12064         return reg_dead_flag == 1 ? 1 : 0;
12065
12066       if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12067         return 1;
12068     }
12069
12070   /* Get the basic block that we were in.  */
12071   if (insn == 0)
12072     block = ENTRY_BLOCK_PTR->next_bb;
12073   else
12074     {
12075       FOR_EACH_BB (block)
12076         if (insn == block->head)
12077           break;
12078
12079       if (block == EXIT_BLOCK_PTR)
12080         return 0;
12081     }
12082
12083   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12084     if (REGNO_REG_SET_P (block->global_live_at_start, i))
12085       return 0;
12086
12087   return 1;
12088 }
12089 \f
12090 /* Note hard registers in X that are used.  This code is similar to
12091    that in flow.c, but much simpler since we don't care about pseudos.  */
12092
12093 static void
12094 mark_used_regs_combine (rtx x)
12095 {
12096   RTX_CODE code = GET_CODE (x);
12097   unsigned int regno;
12098   int i;
12099
12100   switch (code)
12101     {
12102     case LABEL_REF:
12103     case SYMBOL_REF:
12104     case CONST_INT:
12105     case CONST:
12106     case CONST_DOUBLE:
12107     case CONST_VECTOR:
12108     case PC:
12109     case ADDR_VEC:
12110     case ADDR_DIFF_VEC:
12111     case ASM_INPUT:
12112 #ifdef HAVE_cc0
12113     /* CC0 must die in the insn after it is set, so we don't need to take
12114        special note of it here.  */
12115     case CC0:
12116 #endif
12117       return;
12118
12119     case CLOBBER:
12120       /* If we are clobbering a MEM, mark any hard registers inside the
12121          address as used.  */
12122       if (GET_CODE (XEXP (x, 0)) == MEM)
12123         mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12124       return;
12125
12126     case REG:
12127       regno = REGNO (x);
12128       /* A hard reg in a wide mode may really be multiple registers.
12129          If so, mark all of them just like the first.  */
12130       if (regno < FIRST_PSEUDO_REGISTER)
12131         {
12132           unsigned int endregno, r;
12133
12134           /* None of this applies to the stack, frame or arg pointers.  */
12135           if (regno == STACK_POINTER_REGNUM
12136 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
12137               || regno == HARD_FRAME_POINTER_REGNUM
12138 #endif
12139 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12140               || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12141 #endif
12142               || regno == FRAME_POINTER_REGNUM)
12143             return;
12144
12145           endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12146           for (r = regno; r < endregno; r++)
12147             SET_HARD_REG_BIT (newpat_used_regs, r);
12148         }
12149       return;
12150
12151     case SET:
12152       {
12153         /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12154            the address.  */
12155         rtx testreg = SET_DEST (x);
12156
12157         while (GET_CODE (testreg) == SUBREG
12158                || GET_CODE (testreg) == ZERO_EXTRACT
12159                || GET_CODE (testreg) == SIGN_EXTRACT
12160                || GET_CODE (testreg) == STRICT_LOW_PART)
12161           testreg = XEXP (testreg, 0);
12162
12163         if (GET_CODE (testreg) == MEM)
12164           mark_used_regs_combine (XEXP (testreg, 0));
12165
12166         mark_used_regs_combine (SET_SRC (x));
12167       }
12168       return;
12169
12170     default:
12171       break;
12172     }
12173
12174   /* Recursively scan the operands of this expression.  */
12175
12176   {
12177     const char *fmt = GET_RTX_FORMAT (code);
12178
12179     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12180       {
12181         if (fmt[i] == 'e')
12182           mark_used_regs_combine (XEXP (x, i));
12183         else if (fmt[i] == 'E')
12184           {
12185             int j;
12186
12187             for (j = 0; j < XVECLEN (x, i); j++)
12188               mark_used_regs_combine (XVECEXP (x, i, j));
12189           }
12190       }
12191   }
12192 }
12193 \f
12194 /* Remove register number REGNO from the dead registers list of INSN.
12195
12196    Return the note used to record the death, if there was one.  */
12197
12198 rtx
12199 remove_death (unsigned int regno, rtx insn)
12200 {
12201   rtx note = find_regno_note (insn, REG_DEAD, regno);
12202
12203   if (note)
12204     {
12205       REG_N_DEATHS (regno)--;
12206       remove_note (insn, note);
12207     }
12208
12209   return note;
12210 }
12211
12212 /* For each register (hardware or pseudo) used within expression X, if its
12213    death is in an instruction with cuid between FROM_CUID (inclusive) and
12214    TO_INSN (exclusive), put a REG_DEAD note for that register in the
12215    list headed by PNOTES.
12216
12217    That said, don't move registers killed by maybe_kill_insn.
12218
12219    This is done when X is being merged by combination into TO_INSN.  These
12220    notes will then be distributed as needed.  */
12221
12222 static void
12223 move_deaths (rtx x, rtx maybe_kill_insn, int from_cuid, rtx to_insn,
12224              rtx *pnotes)
12225 {
12226   const char *fmt;
12227   int len, i;
12228   enum rtx_code code = GET_CODE (x);
12229
12230   if (code == REG)
12231     {
12232       unsigned int regno = REGNO (x);
12233       rtx where_dead = reg_last_death[regno];
12234       rtx before_dead, after_dead;
12235
12236       /* Don't move the register if it gets killed in between from and to.  */
12237       if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12238           && ! reg_referenced_p (x, maybe_kill_insn))
12239         return;
12240
12241       /* WHERE_DEAD could be a USE insn made by combine, so first we
12242          make sure that we have insns with valid INSN_CUID values.  */
12243       before_dead = where_dead;
12244       while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
12245         before_dead = PREV_INSN (before_dead);
12246
12247       after_dead = where_dead;
12248       while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
12249         after_dead = NEXT_INSN (after_dead);
12250
12251       if (before_dead && after_dead
12252           && INSN_CUID (before_dead) >= from_cuid
12253           && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
12254               || (where_dead != after_dead
12255                   && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
12256         {
12257           rtx note = remove_death (regno, where_dead);
12258
12259           /* It is possible for the call above to return 0.  This can occur
12260              when reg_last_death points to I2 or I1 that we combined with.
12261              In that case make a new note.
12262
12263              We must also check for the case where X is a hard register
12264              and NOTE is a death note for a range of hard registers
12265              including X.  In that case, we must put REG_DEAD notes for
12266              the remaining registers in place of NOTE.  */
12267
12268           if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12269               && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12270                   > GET_MODE_SIZE (GET_MODE (x))))
12271             {
12272               unsigned int deadregno = REGNO (XEXP (note, 0));
12273               unsigned int deadend
12274                 = (deadregno + HARD_REGNO_NREGS (deadregno,
12275                                                  GET_MODE (XEXP (note, 0))));
12276               unsigned int ourend
12277                 = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12278               unsigned int i;
12279
12280               for (i = deadregno; i < deadend; i++)
12281                 if (i < regno || i >= ourend)
12282                   REG_NOTES (where_dead)
12283                     = gen_rtx_EXPR_LIST (REG_DEAD,
12284                                          regno_reg_rtx[i],
12285                                          REG_NOTES (where_dead));
12286             }
12287
12288           /* If we didn't find any note, or if we found a REG_DEAD note that
12289              covers only part of the given reg, and we have a multi-reg hard
12290              register, then to be safe we must check for REG_DEAD notes
12291              for each register other than the first.  They could have
12292              their own REG_DEAD notes lying around.  */
12293           else if ((note == 0
12294                     || (note != 0
12295                         && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12296                             < GET_MODE_SIZE (GET_MODE (x)))))
12297                    && regno < FIRST_PSEUDO_REGISTER
12298                    && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
12299             {
12300               unsigned int ourend
12301                 = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12302               unsigned int i, offset;
12303               rtx oldnotes = 0;
12304
12305               if (note)
12306                 offset = HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0)));
12307               else
12308                 offset = 1;
12309
12310               for (i = regno + offset; i < ourend; i++)
12311                 move_deaths (regno_reg_rtx[i],
12312                              maybe_kill_insn, from_cuid, to_insn, &oldnotes);
12313             }
12314
12315           if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12316             {
12317               XEXP (note, 1) = *pnotes;
12318               *pnotes = note;
12319             }
12320           else
12321             *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
12322
12323           REG_N_DEATHS (regno)++;
12324         }
12325
12326       return;
12327     }
12328
12329   else if (GET_CODE (x) == SET)
12330     {
12331       rtx dest = SET_DEST (x);
12332
12333       move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
12334
12335       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
12336          that accesses one word of a multi-word item, some
12337          piece of everything register in the expression is used by
12338          this insn, so remove any old death.  */
12339       /* ??? So why do we test for equality of the sizes?  */
12340
12341       if (GET_CODE (dest) == ZERO_EXTRACT
12342           || GET_CODE (dest) == STRICT_LOW_PART
12343           || (GET_CODE (dest) == SUBREG
12344               && (((GET_MODE_SIZE (GET_MODE (dest))
12345                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
12346                   == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
12347                        + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
12348         {
12349           move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
12350           return;
12351         }
12352
12353       /* If this is some other SUBREG, we know it replaces the entire
12354          value, so use that as the destination.  */
12355       if (GET_CODE (dest) == SUBREG)
12356         dest = SUBREG_REG (dest);
12357
12358       /* If this is a MEM, adjust deaths of anything used in the address.
12359          For a REG (the only other possibility), the entire value is
12360          being replaced so the old value is not used in this insn.  */
12361
12362       if (GET_CODE (dest) == MEM)
12363         move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
12364                      to_insn, pnotes);
12365       return;
12366     }
12367
12368   else if (GET_CODE (x) == CLOBBER)
12369     return;
12370
12371   len = GET_RTX_LENGTH (code);
12372   fmt = GET_RTX_FORMAT (code);
12373
12374   for (i = 0; i < len; i++)
12375     {
12376       if (fmt[i] == 'E')
12377         {
12378           int j;
12379           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12380             move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
12381                          to_insn, pnotes);
12382         }
12383       else if (fmt[i] == 'e')
12384         move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
12385     }
12386 }
12387 \f
12388 /* Return 1 if X is the target of a bit-field assignment in BODY, the
12389    pattern of an insn.  X must be a REG.  */
12390
12391 static int
12392 reg_bitfield_target_p (rtx x, rtx body)
12393 {
12394   int i;
12395
12396   if (GET_CODE (body) == SET)
12397     {
12398       rtx dest = SET_DEST (body);
12399       rtx target;
12400       unsigned int regno, tregno, endregno, endtregno;
12401
12402       if (GET_CODE (dest) == ZERO_EXTRACT)
12403         target = XEXP (dest, 0);
12404       else if (GET_CODE (dest) == STRICT_LOW_PART)
12405         target = SUBREG_REG (XEXP (dest, 0));
12406       else
12407         return 0;
12408
12409       if (GET_CODE (target) == SUBREG)
12410         target = SUBREG_REG (target);
12411
12412       if (GET_CODE (target) != REG)
12413         return 0;
12414
12415       tregno = REGNO (target), regno = REGNO (x);
12416       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
12417         return target == x;
12418
12419       endtregno = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (target));
12420       endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12421
12422       return endregno > tregno && regno < endtregno;
12423     }
12424
12425   else if (GET_CODE (body) == PARALLEL)
12426     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
12427       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
12428         return 1;
12429
12430   return 0;
12431 }
12432 \f
12433 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
12434    as appropriate.  I3 and I2 are the insns resulting from the combination
12435    insns including FROM (I2 may be zero).
12436
12437    Each note in the list is either ignored or placed on some insns, depending
12438    on the type of note.  */
12439
12440 static void
12441 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2)
12442 {
12443   rtx note, next_note;
12444   rtx tem;
12445
12446   for (note = notes; note; note = next_note)
12447     {
12448       rtx place = 0, place2 = 0;
12449
12450       /* If this NOTE references a pseudo register, ensure it references
12451          the latest copy of that register.  */
12452       if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
12453           && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
12454         XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
12455
12456       next_note = XEXP (note, 1);
12457       switch (REG_NOTE_KIND (note))
12458         {
12459         case REG_BR_PROB:
12460         case REG_BR_PRED:
12461           /* Doesn't matter much where we put this, as long as it's somewhere.
12462              It is preferable to keep these notes on branches, which is most
12463              likely to be i3.  */
12464           place = i3;
12465           break;
12466
12467         case REG_VALUE_PROFILE:
12468           /* Just get rid of this note, as it is unused later anyway.  */
12469           break;
12470
12471         case REG_VTABLE_REF:
12472           /* ??? Should remain with *a particular* memory load.  Given the
12473              nature of vtable data, the last insn seems relatively safe.  */
12474           place = i3;
12475           break;
12476
12477         case REG_NON_LOCAL_GOTO:
12478           if (GET_CODE (i3) == JUMP_INSN)
12479             place = i3;
12480           else if (i2 && GET_CODE (i2) == JUMP_INSN)
12481             place = i2;
12482           else
12483             abort ();
12484           break;
12485
12486         case REG_EH_REGION:
12487           /* These notes must remain with the call or trapping instruction.  */
12488           if (GET_CODE (i3) == CALL_INSN)
12489             place = i3;
12490           else if (i2 && GET_CODE (i2) == CALL_INSN)
12491             place = i2;
12492           else if (flag_non_call_exceptions)
12493             {
12494               if (may_trap_p (i3))
12495                 place = i3;
12496               else if (i2 && may_trap_p (i2))
12497                 place = i2;
12498               /* ??? Otherwise assume we've combined things such that we
12499                  can now prove that the instructions can't trap.  Drop the
12500                  note in this case.  */
12501             }
12502           else
12503             abort ();
12504           break;
12505
12506         case REG_ALWAYS_RETURN:
12507         case REG_NORETURN:
12508         case REG_SETJMP:
12509           /* These notes must remain with the call.  It should not be
12510              possible for both I2 and I3 to be a call.  */
12511           if (GET_CODE (i3) == CALL_INSN)
12512             place = i3;
12513           else if (i2 && GET_CODE (i2) == CALL_INSN)
12514             place = i2;
12515           else
12516             abort ();
12517           break;
12518
12519         case REG_UNUSED:
12520           /* Any clobbers for i3 may still exist, and so we must process
12521              REG_UNUSED notes from that insn.
12522
12523              Any clobbers from i2 or i1 can only exist if they were added by
12524              recog_for_combine.  In that case, recog_for_combine created the
12525              necessary REG_UNUSED notes.  Trying to keep any original
12526              REG_UNUSED notes from these insns can cause incorrect output
12527              if it is for the same register as the original i3 dest.
12528              In that case, we will notice that the register is set in i3,
12529              and then add a REG_UNUSED note for the destination of i3, which
12530              is wrong.  However, it is possible to have REG_UNUSED notes from
12531              i2 or i1 for register which were both used and clobbered, so
12532              we keep notes from i2 or i1 if they will turn into REG_DEAD
12533              notes.  */
12534
12535           /* If this register is set or clobbered in I3, put the note there
12536              unless there is one already.  */
12537           if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
12538             {
12539               if (from_insn != i3)
12540                 break;
12541
12542               if (! (GET_CODE (XEXP (note, 0)) == REG
12543                      ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
12544                      : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
12545                 place = i3;
12546             }
12547           /* Otherwise, if this register is used by I3, then this register
12548              now dies here, so we must put a REG_DEAD note here unless there
12549              is one already.  */
12550           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
12551                    && ! (GET_CODE (XEXP (note, 0)) == REG
12552                          ? find_regno_note (i3, REG_DEAD,
12553                                             REGNO (XEXP (note, 0)))
12554                          : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
12555             {
12556               PUT_REG_NOTE_KIND (note, REG_DEAD);
12557               place = i3;
12558             }
12559           break;
12560
12561         case REG_EQUAL:
12562         case REG_EQUIV:
12563         case REG_NOALIAS:
12564           /* These notes say something about results of an insn.  We can
12565              only support them if they used to be on I3 in which case they
12566              remain on I3.  Otherwise they are ignored.
12567
12568              If the note refers to an expression that is not a constant, we
12569              must also ignore the note since we cannot tell whether the
12570              equivalence is still true.  It might be possible to do
12571              slightly better than this (we only have a problem if I2DEST
12572              or I1DEST is present in the expression), but it doesn't
12573              seem worth the trouble.  */
12574
12575           if (from_insn == i3
12576               && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
12577             place = i3;
12578           break;
12579
12580         case REG_INC:
12581         case REG_NO_CONFLICT:
12582           /* These notes say something about how a register is used.  They must
12583              be present on any use of the register in I2 or I3.  */
12584           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
12585             place = i3;
12586
12587           if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
12588             {
12589               if (place)
12590                 place2 = i2;
12591               else
12592                 place = i2;
12593             }
12594           break;
12595
12596         case REG_LABEL:
12597           /* This can show up in several ways -- either directly in the
12598              pattern, or hidden off in the constant pool with (or without?)
12599              a REG_EQUAL note.  */
12600           /* ??? Ignore the without-reg_equal-note problem for now.  */
12601           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12602               || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12603                   && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12604                   && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12605             place = i3;
12606
12607           if (i2
12608               && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12609                   || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12610                       && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12611                       && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12612             {
12613               if (place)
12614                 place2 = i2;
12615               else
12616                 place = i2;
12617             }
12618
12619           /* Don't attach REG_LABEL note to a JUMP_INSN which has
12620              JUMP_LABEL already.  Instead, decrement LABEL_NUSES.  */
12621           if (place && GET_CODE (place) == JUMP_INSN && JUMP_LABEL (place))
12622             {
12623               if (JUMP_LABEL (place) != XEXP (note, 0))
12624                 abort ();
12625               if (GET_CODE (JUMP_LABEL (place)) == CODE_LABEL)
12626                 LABEL_NUSES (JUMP_LABEL (place))--;
12627               place = 0;
12628             }
12629           if (place2 && GET_CODE (place2) == JUMP_INSN && JUMP_LABEL (place2))
12630             {
12631               if (JUMP_LABEL (place2) != XEXP (note, 0))
12632                 abort ();
12633               if (GET_CODE (JUMP_LABEL (place2)) == CODE_LABEL)
12634                 LABEL_NUSES (JUMP_LABEL (place2))--;
12635               place2 = 0;
12636             }
12637           break;
12638
12639         case REG_NONNEG:
12640           /* This note says something about the value of a register prior
12641              to the execution of an insn.  It is too much trouble to see
12642              if the note is still correct in all situations.  It is better
12643              to simply delete it.  */
12644           break;
12645
12646         case REG_RETVAL:
12647           /* If the insn previously containing this note still exists,
12648              put it back where it was.  Otherwise move it to the previous
12649              insn.  Adjust the corresponding REG_LIBCALL note.  */
12650           if (GET_CODE (from_insn) != NOTE)
12651             place = from_insn;
12652           else
12653             {
12654               tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
12655               place = prev_real_insn (from_insn);
12656               if (tem && place)
12657                 XEXP (tem, 0) = place;
12658               /* If we're deleting the last remaining instruction of a
12659                  libcall sequence, don't add the notes.  */
12660               else if (XEXP (note, 0) == from_insn)
12661                 tem = place = 0;
12662             }
12663           break;
12664
12665         case REG_LIBCALL:
12666           /* This is handled similarly to REG_RETVAL.  */
12667           if (GET_CODE (from_insn) != NOTE)
12668             place = from_insn;
12669           else
12670             {
12671               tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
12672               place = next_real_insn (from_insn);
12673               if (tem && place)
12674                 XEXP (tem, 0) = place;
12675               /* If we're deleting the last remaining instruction of a
12676                  libcall sequence, don't add the notes.  */
12677               else if (XEXP (note, 0) == from_insn)
12678                 tem = place = 0;
12679             }
12680           break;
12681
12682         case REG_DEAD:
12683           /* If the register is used as an input in I3, it dies there.
12684              Similarly for I2, if it is nonzero and adjacent to I3.
12685
12686              If the register is not used as an input in either I3 or I2
12687              and it is not one of the registers we were supposed to eliminate,
12688              there are two possibilities.  We might have a non-adjacent I2
12689              or we might have somehow eliminated an additional register
12690              from a computation.  For example, we might have had A & B where
12691              we discover that B will always be zero.  In this case we will
12692              eliminate the reference to A.
12693
12694              In both cases, we must search to see if we can find a previous
12695              use of A and put the death note there.  */
12696
12697           if (from_insn
12698               && GET_CODE (from_insn) == CALL_INSN
12699               && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12700             place = from_insn;
12701           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12702             place = i3;
12703           else if (i2 != 0 && next_nonnote_insn (i2) == i3
12704                    && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12705             place = i2;
12706
12707           if (place == 0)
12708             {
12709               basic_block bb = this_basic_block;
12710
12711               for (tem = PREV_INSN (i3); place == 0; tem = PREV_INSN (tem))
12712                 {
12713                   if (! INSN_P (tem))
12714                     {
12715                       if (tem == bb->head)
12716                         break;
12717                       continue;
12718                     }
12719
12720                   /* If the register is being set at TEM, see if that is all
12721                      TEM is doing.  If so, delete TEM.  Otherwise, make this
12722                      into a REG_UNUSED note instead.  */
12723                   if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
12724                     {
12725                       rtx set = single_set (tem);
12726                       rtx inner_dest = 0;
12727 #ifdef HAVE_cc0
12728                       rtx cc0_setter = NULL_RTX;
12729 #endif
12730
12731                       if (set != 0)
12732                         for (inner_dest = SET_DEST (set);
12733                              (GET_CODE (inner_dest) == STRICT_LOW_PART
12734                               || GET_CODE (inner_dest) == SUBREG
12735                               || GET_CODE (inner_dest) == ZERO_EXTRACT);
12736                              inner_dest = XEXP (inner_dest, 0))
12737                           ;
12738
12739                       /* Verify that it was the set, and not a clobber that
12740                          modified the register.
12741
12742                          CC0 targets must be careful to maintain setter/user
12743                          pairs.  If we cannot delete the setter due to side
12744                          effects, mark the user with an UNUSED note instead
12745                          of deleting it.  */
12746
12747                       if (set != 0 && ! side_effects_p (SET_SRC (set))
12748                           && rtx_equal_p (XEXP (note, 0), inner_dest)
12749 #ifdef HAVE_cc0
12750                           && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12751                               || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12752                                   && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12753 #endif
12754                           )
12755                         {
12756                           /* Move the notes and links of TEM elsewhere.
12757                              This might delete other dead insns recursively.
12758                              First set the pattern to something that won't use
12759                              any register.  */
12760
12761                           PATTERN (tem) = pc_rtx;
12762
12763                           distribute_notes (REG_NOTES (tem), tem, tem,
12764                                             NULL_RTX);
12765                           distribute_links (LOG_LINKS (tem));
12766
12767                           PUT_CODE (tem, NOTE);
12768                           NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
12769                           NOTE_SOURCE_FILE (tem) = 0;
12770
12771 #ifdef HAVE_cc0
12772                           /* Delete the setter too.  */
12773                           if (cc0_setter)
12774                             {
12775                               PATTERN (cc0_setter) = pc_rtx;
12776
12777                               distribute_notes (REG_NOTES (cc0_setter),
12778                                                 cc0_setter, cc0_setter,
12779                                                 NULL_RTX);
12780                               distribute_links (LOG_LINKS (cc0_setter));
12781
12782                               PUT_CODE (cc0_setter, NOTE);
12783                               NOTE_LINE_NUMBER (cc0_setter)
12784                                 = NOTE_INSN_DELETED;
12785                               NOTE_SOURCE_FILE (cc0_setter) = 0;
12786                             }
12787 #endif
12788                         }
12789                       /* If the register is both set and used here, put the
12790                          REG_DEAD note here, but place a REG_UNUSED note
12791                          here too unless there already is one.  */
12792                       else if (reg_referenced_p (XEXP (note, 0),
12793                                                  PATTERN (tem)))
12794                         {
12795                           place = tem;
12796
12797                           if (! find_regno_note (tem, REG_UNUSED,
12798                                                  REGNO (XEXP (note, 0))))
12799                             REG_NOTES (tem)
12800                               = gen_rtx_EXPR_LIST (REG_UNUSED, XEXP (note, 0),
12801                                                    REG_NOTES (tem));
12802                         }
12803                       else
12804                         {
12805                           PUT_REG_NOTE_KIND (note, REG_UNUSED);
12806
12807                           /*  If there isn't already a REG_UNUSED note, put one
12808                               here.  */
12809                           if (! find_regno_note (tem, REG_UNUSED,
12810                                                  REGNO (XEXP (note, 0))))
12811                             place = tem;
12812                           break;
12813                         }
12814                     }
12815                   else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12816                            || (GET_CODE (tem) == CALL_INSN
12817                                && find_reg_fusage (tem, USE, XEXP (note, 0))))
12818                     {
12819                       place = tem;
12820
12821                       /* If we are doing a 3->2 combination, and we have a
12822                          register which formerly died in i3 and was not used
12823                          by i2, which now no longer dies in i3 and is used in
12824                          i2 but does not die in i2, and place is between i2
12825                          and i3, then we may need to move a link from place to
12826                          i2.  */
12827                       if (i2 && INSN_UID (place) <= max_uid_cuid
12828                           && INSN_CUID (place) > INSN_CUID (i2)
12829                           && from_insn
12830                           && INSN_CUID (from_insn) > INSN_CUID (i2)
12831                           && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12832                         {
12833                           rtx links = LOG_LINKS (place);
12834                           LOG_LINKS (place) = 0;
12835                           distribute_links (links);
12836                         }
12837                       break;
12838                     }
12839
12840                   if (tem == bb->head)
12841                     break;
12842                 }
12843
12844               /* We haven't found an insn for the death note and it
12845                  is still a REG_DEAD note, but we have hit the beginning
12846                  of the block.  If the existing life info says the reg
12847                  was dead, there's nothing left to do.  Otherwise, we'll
12848                  need to do a global life update after combine.  */
12849               if (REG_NOTE_KIND (note) == REG_DEAD && place == 0
12850                   && REGNO_REG_SET_P (bb->global_live_at_start,
12851                                       REGNO (XEXP (note, 0))))
12852                 SET_BIT (refresh_blocks, this_basic_block->index);
12853             }
12854
12855           /* If the register is set or already dead at PLACE, we needn't do
12856              anything with this note if it is still a REG_DEAD note.
12857              We can here if it is set at all, not if is it totally replace,
12858              which is what `dead_or_set_p' checks, so also check for it being
12859              set partially.  */
12860
12861           if (place && REG_NOTE_KIND (note) == REG_DEAD)
12862             {
12863               unsigned int regno = REGNO (XEXP (note, 0));
12864
12865               /* Similarly, if the instruction on which we want to place
12866                  the note is a noop, we'll need do a global live update
12867                  after we remove them in delete_noop_moves.  */
12868               if (noop_move_p (place))
12869                 SET_BIT (refresh_blocks, this_basic_block->index);
12870
12871               if (dead_or_set_p (place, XEXP (note, 0))
12872                   || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12873                 {
12874                   /* Unless the register previously died in PLACE, clear
12875                      reg_last_death.  [I no longer understand why this is
12876                      being done.] */
12877                   if (reg_last_death[regno] != place)
12878                     reg_last_death[regno] = 0;
12879                   place = 0;
12880                 }
12881               else
12882                 reg_last_death[regno] = place;
12883
12884               /* If this is a death note for a hard reg that is occupying
12885                  multiple registers, ensure that we are still using all
12886                  parts of the object.  If we find a piece of the object
12887                  that is unused, we must arrange for an appropriate REG_DEAD
12888                  note to be added for it.  However, we can't just emit a USE
12889                  and tag the note to it, since the register might actually
12890                  be dead; so we recourse, and the recursive call then finds
12891                  the previous insn that used this register.  */
12892
12893               if (place && regno < FIRST_PSEUDO_REGISTER
12894                   && HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0))) > 1)
12895                 {
12896                   unsigned int endregno
12897                     = regno + HARD_REGNO_NREGS (regno,
12898                                                 GET_MODE (XEXP (note, 0)));
12899                   int all_used = 1;
12900                   unsigned int i;
12901
12902                   for (i = regno; i < endregno; i++)
12903                     if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12904                          && ! find_regno_fusage (place, USE, i))
12905                         || dead_or_set_regno_p (place, i))
12906                       all_used = 0;
12907
12908                   if (! all_used)
12909                     {
12910                       /* Put only REG_DEAD notes for pieces that are
12911                          not already dead or set.  */
12912
12913                       for (i = regno; i < endregno;
12914                            i += HARD_REGNO_NREGS (i, reg_raw_mode[i]))
12915                         {
12916                           rtx piece = regno_reg_rtx[i];
12917                           basic_block bb = this_basic_block;
12918
12919                           if (! dead_or_set_p (place, piece)
12920                               && ! reg_bitfield_target_p (piece,
12921                                                           PATTERN (place)))
12922                             {
12923                               rtx new_note
12924                                 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12925
12926                               distribute_notes (new_note, place, place,
12927                                                 NULL_RTX);
12928                             }
12929                           else if (! refers_to_regno_p (i, i + 1,
12930                                                         PATTERN (place), 0)
12931                                    && ! find_regno_fusage (place, USE, i))
12932                             for (tem = PREV_INSN (place); ;
12933                                  tem = PREV_INSN (tem))
12934                               {
12935                                 if (! INSN_P (tem))
12936                                   {
12937                                     if (tem == bb->head)
12938                                       {
12939                                         SET_BIT (refresh_blocks,
12940                                                  this_basic_block->index);
12941                                         break;
12942                                       }
12943                                     continue;
12944                                   }
12945                                 if (dead_or_set_p (tem, piece)
12946                                     || reg_bitfield_target_p (piece,
12947                                                               PATTERN (tem)))
12948                                   {
12949                                     REG_NOTES (tem)
12950                                       = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
12951                                                            REG_NOTES (tem));
12952                                     break;
12953                                   }
12954                               }
12955
12956                         }
12957
12958                       place = 0;
12959                     }
12960                 }
12961             }
12962           break;
12963
12964         default:
12965           /* Any other notes should not be present at this point in the
12966              compilation.  */
12967           abort ();
12968         }
12969
12970       if (place)
12971         {
12972           XEXP (note, 1) = REG_NOTES (place);
12973           REG_NOTES (place) = note;
12974         }
12975       else if ((REG_NOTE_KIND (note) == REG_DEAD
12976                 || REG_NOTE_KIND (note) == REG_UNUSED)
12977                && GET_CODE (XEXP (note, 0)) == REG)
12978         REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
12979
12980       if (place2)
12981         {
12982           if ((REG_NOTE_KIND (note) == REG_DEAD
12983                || REG_NOTE_KIND (note) == REG_UNUSED)
12984               && GET_CODE (XEXP (note, 0)) == REG)
12985             REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
12986
12987           REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
12988                                                REG_NOTE_KIND (note),
12989                                                XEXP (note, 0),
12990                                                REG_NOTES (place2));
12991         }
12992     }
12993 }
12994 \f
12995 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12996    I3, I2, and I1 to new locations.  This is also called in one case to
12997    add a link pointing at I3 when I3's destination is changed.  */
12998
12999 static void
13000 distribute_links (rtx links)
13001 {
13002   rtx link, next_link;
13003
13004   for (link = links; link; link = next_link)
13005     {
13006       rtx place = 0;
13007       rtx insn;
13008       rtx set, reg;
13009
13010       next_link = XEXP (link, 1);
13011
13012       /* If the insn that this link points to is a NOTE or isn't a single
13013          set, ignore it.  In the latter case, it isn't clear what we
13014          can do other than ignore the link, since we can't tell which
13015          register it was for.  Such links wouldn't be used by combine
13016          anyway.
13017
13018          It is not possible for the destination of the target of the link to
13019          have been changed by combine.  The only potential of this is if we
13020          replace I3, I2, and I1 by I3 and I2.  But in that case the
13021          destination of I2 also remains unchanged.  */
13022
13023       if (GET_CODE (XEXP (link, 0)) == NOTE
13024           || (set = single_set (XEXP (link, 0))) == 0)
13025         continue;
13026
13027       reg = SET_DEST (set);
13028       while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
13029              || GET_CODE (reg) == SIGN_EXTRACT
13030              || GET_CODE (reg) == STRICT_LOW_PART)
13031         reg = XEXP (reg, 0);
13032
13033       /* A LOG_LINK is defined as being placed on the first insn that uses
13034          a register and points to the insn that sets the register.  Start
13035          searching at the next insn after the target of the link and stop
13036          when we reach a set of the register or the end of the basic block.
13037
13038          Note that this correctly handles the link that used to point from
13039          I3 to I2.  Also note that not much searching is typically done here
13040          since most links don't point very far away.  */
13041
13042       for (insn = NEXT_INSN (XEXP (link, 0));
13043            (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
13044                      || this_basic_block->next_bb->head != insn));
13045            insn = NEXT_INSN (insn))
13046         if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13047           {
13048             if (reg_referenced_p (reg, PATTERN (insn)))
13049               place = insn;
13050             break;
13051           }
13052         else if (GET_CODE (insn) == CALL_INSN
13053                  && find_reg_fusage (insn, USE, reg))
13054           {
13055             place = insn;
13056             break;
13057           }
13058
13059       /* If we found a place to put the link, place it there unless there
13060          is already a link to the same insn as LINK at that point.  */
13061
13062       if (place)
13063         {
13064           rtx link2;
13065
13066           for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
13067             if (XEXP (link2, 0) == XEXP (link, 0))
13068               break;
13069
13070           if (link2 == 0)
13071             {
13072               XEXP (link, 1) = LOG_LINKS (place);
13073               LOG_LINKS (place) = link;
13074
13075               /* Set added_links_insn to the earliest insn we added a
13076                  link to.  */
13077               if (added_links_insn == 0
13078                   || INSN_CUID (added_links_insn) > INSN_CUID (place))
13079                 added_links_insn = place;
13080             }
13081         }
13082     }
13083 }
13084 \f
13085 /* Compute INSN_CUID for INSN, which is an insn made by combine.  */
13086
13087 static int
13088 insn_cuid (rtx insn)
13089 {
13090   while (insn != 0 && INSN_UID (insn) > max_uid_cuid
13091          && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE)
13092     insn = NEXT_INSN (insn);
13093
13094   if (INSN_UID (insn) > max_uid_cuid)
13095     abort ();
13096
13097   return INSN_CUID (insn);
13098 }
13099 \f
13100 void
13101 dump_combine_stats (FILE *file)
13102 {
13103   fnotice
13104     (file,
13105      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13106      combine_attempts, combine_merges, combine_extras, combine_successes);
13107 }
13108
13109 void
13110 dump_combine_total_stats (FILE *file)
13111 {
13112   fnotice
13113     (file,
13114      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13115      total_attempts, total_merges, total_extras, total_successes);
13116 }