OSDN Git Service

2007-07-01 Daniel Berlin <dberlin@dberlin.org>
[pf3gnuchains/gcc-fork.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.  */
22
23 /* This module is essentially the "combiner" phase of the U. of Arizona
24    Portable Optimizer, but redone to work on our list-structured
25    representation for RTL instead of their string representation.
26
27    The LOG_LINKS of each insn identify the most recent assignment
28    to each REG used in the insn.  It is a list of previous insns,
29    each of which contains a SET for a REG that is used in this insn
30    and not used or set in between.  LOG_LINKs never cross basic blocks.
31    They were set up by the preceding pass (lifetime analysis).
32
33    We try to combine each pair of insns joined by a logical link.
34    We also try to combine triples of insns A, B and C when
35    C has a link back to B and B has a link back to A.
36
37    LOG_LINKS does not have links for use of the CC0.  They don't
38    need to, because the insn that sets the CC0 is always immediately
39    before the insn that tests it.  So we always regard a branch
40    insn as having a logical link to the preceding insn.  The same is true
41    for an insn explicitly using CC0.
42
43    We check (with use_crosses_set_p) to avoid combining in such a way
44    as to move a computation to a place where its value would be different.
45
46    Combination is done by mathematically substituting the previous
47    insn(s) values for the regs they set into the expressions in
48    the later insns that refer to these regs.  If the result is a valid insn
49    for our target machine, according to the machine description,
50    we install it, delete the earlier insns, and update the data flow
51    information (LOG_LINKS and REG_NOTES) for what we did.
52
53    There are a few exceptions where the dataflow information isn't
54    completely updated (however this is only a local issue since it is
55    regenerated before the next pass that uses it):
56
57    - reg_live_length is not updated
58    - reg_n_refs is not adjusted in the rare case when a register is
59      no longer required in a computation
60    - there are extremely rare cases (see distribute_notes) when a
61      REG_DEAD note is lost
62    - a LOG_LINKS entry that refers to an insn with multiple SETs may be
63      removed because there is no way to know which register it was
64      linking
65
66    To simplify substitution, we combine only when the earlier insn(s)
67    consist of only a single assignment.  To simplify updating afterward,
68    we never combine when a subroutine call appears in the middle.
69
70    Since we do not represent assignments to CC0 explicitly except when that
71    is all an insn does, there is no LOG_LINKS entry in an insn that uses
72    the condition code for the insn that set the condition code.
73    Fortunately, these two insns must be consecutive.
74    Therefore, every JUMP_INSN is taken to have an implicit logical link
75    to the preceding insn.  This is not quite right, since non-jumps can
76    also use the condition code; but in practice such insns would not
77    combine anyway.  */
78
79 #include "config.h"
80 #include "system.h"
81 #include "coretypes.h"
82 #include "tm.h"
83 #include "rtl.h"
84 #include "tree.h"
85 #include "tm_p.h"
86 #include "flags.h"
87 #include "regs.h"
88 #include "hard-reg-set.h"
89 #include "basic-block.h"
90 #include "insn-config.h"
91 #include "function.h"
92 /* Include expr.h after insn-config.h so we get HAVE_conditional_move.  */
93 #include "expr.h"
94 #include "insn-attr.h"
95 #include "recog.h"
96 #include "real.h"
97 #include "toplev.h"
98 #include "target.h"
99 #include "optabs.h"
100 #include "insn-codes.h"
101 #include "rtlhooks-def.h"
102 /* Include output.h for dump_file.  */
103 #include "output.h"
104 #include "params.h"
105 #include "timevar.h"
106 #include "tree-pass.h"
107 #include "df.h"
108
109 /* Number of attempts to combine instructions in this function.  */
110
111 static int combine_attempts;
112
113 /* Number of attempts that got as far as substitution in this function.  */
114
115 static int combine_merges;
116
117 /* Number of instructions combined with added SETs in this function.  */
118
119 static int combine_extras;
120
121 /* Number of instructions combined in this function.  */
122
123 static int combine_successes;
124
125 /* Totals over entire compilation.  */
126
127 static int total_attempts, total_merges, total_extras, total_successes;
128
129 /* combine_instructions may try to replace the right hand side of the
130    second instruction with the value of an associated REG_EQUAL note
131    before throwing it at try_combine.  That is problematic when there
132    is a REG_DEAD note for a register used in the old right hand side
133    and can cause distribute_notes to do wrong things.  This is the
134    second instruction if it has been so modified, null otherwise.  */
135
136 static rtx i2mod;
137
138 /* When I2MOD is nonnull, this is a copy of the old right hand side.  */
139
140 static rtx i2mod_old_rhs;
141
142 /* When I2MOD is nonnull, this is a copy of the new right hand side.  */
143
144 static rtx i2mod_new_rhs;
145 \f
146 /* Maximum register number, which is the size of the tables below.  */
147
148 static unsigned int combine_max_regno;
149
150 struct reg_stat {
151   /* Record last point of death of (hard or pseudo) register n.  */
152   rtx                           last_death;
153
154   /* Record last point of modification of (hard or pseudo) register n.  */
155   rtx                           last_set;
156
157   /* The next group of fields allows the recording of the last value assigned
158      to (hard or pseudo) register n.  We use this information to see if an
159      operation being processed is redundant given a prior operation performed
160      on the register.  For example, an `and' with a constant is redundant if
161      all the zero bits are already known to be turned off.
162
163      We use an approach similar to that used by cse, but change it in the
164      following ways:
165
166      (1) We do not want to reinitialize at each label.
167      (2) It is useful, but not critical, to know the actual value assigned
168          to a register.  Often just its form is helpful.
169
170      Therefore, we maintain the following fields:
171
172      last_set_value             the last value assigned
173      last_set_label             records the value of label_tick when the
174                                 register was assigned
175      last_set_table_tick        records the value of label_tick when a
176                                 value using the register is assigned
177      last_set_invalid           set to nonzero when it is not valid
178                                 to use the value of this register in some
179                                 register's value
180
181      To understand the usage of these tables, it is important to understand
182      the distinction between the value in last_set_value being valid and
183      the register being validly contained in some other expression in the
184      table.
185
186      (The next two parameters are out of date).
187
188      reg_stat[i].last_set_value is valid if it is nonzero, and either
189      reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
190
191      Register I may validly appear in any expression returned for the value
192      of another register if reg_n_sets[i] is 1.  It may also appear in the
193      value for register J if reg_stat[j].last_set_invalid is zero, or
194      reg_stat[i].last_set_label < reg_stat[j].last_set_label.
195
196      If an expression is found in the table containing a register which may
197      not validly appear in an expression, the register is replaced by
198      something that won't match, (clobber (const_int 0)).  */
199
200   /* Record last value assigned to (hard or pseudo) register n.  */
201
202   rtx                           last_set_value;
203
204   /* Record the value of label_tick when an expression involving register n
205      is placed in last_set_value.  */
206
207   int                           last_set_table_tick;
208
209   /* Record the value of label_tick when the value for register n is placed in
210      last_set_value.  */
211
212   int                           last_set_label;
213
214   /* These fields are maintained in parallel with last_set_value and are
215      used to store the mode in which the register was last set, the bits
216      that were known to be zero when it was last set, and the number of
217      sign bits copies it was known to have when it was last set.  */
218
219   unsigned HOST_WIDE_INT        last_set_nonzero_bits;
220   char                          last_set_sign_bit_copies;
221   ENUM_BITFIELD(machine_mode)   last_set_mode : 8;
222
223   /* Set nonzero if references to register n in expressions should not be
224      used.  last_set_invalid is set nonzero when this register is being
225      assigned to and last_set_table_tick == label_tick.  */
226
227   char                          last_set_invalid;
228
229   /* Some registers that are set more than once and used in more than one
230      basic block are nevertheless always set in similar ways.  For example,
231      a QImode register may be loaded from memory in two places on a machine
232      where byte loads zero extend.
233
234      We record in the following fields if a register has some leading bits
235      that are always equal to the sign bit, and what we know about the
236      nonzero bits of a register, specifically which bits are known to be
237      zero.
238
239      If an entry is zero, it means that we don't know anything special.  */
240
241   unsigned char                 sign_bit_copies;
242
243   unsigned HOST_WIDE_INT        nonzero_bits;
244
245   /* Record the value of the label_tick when the last truncation
246      happened.  The field truncated_to_mode is only valid if
247      truncation_label == label_tick.  */
248
249   int                           truncation_label;
250
251   /* Record the last truncation seen for this register.  If truncation
252      is not a nop to this mode we might be able to save an explicit
253      truncation if we know that value already contains a truncated
254      value.  */
255
256   ENUM_BITFIELD(machine_mode)   truncated_to_mode : 8;
257 };
258
259 static struct reg_stat *reg_stat;
260
261 /* Record the luid of the last insn that invalidated memory
262    (anything that writes memory, and subroutine calls, but not pushes).  */
263
264 static int mem_last_set;
265
266 /* Record the luid of the last CALL_INSN
267    so we can tell whether a potential combination crosses any calls.  */
268
269 static int last_call_luid;
270
271 /* When `subst' is called, this is the insn that is being modified
272    (by combining in a previous insn).  The PATTERN of this insn
273    is still the old pattern partially modified and it should not be
274    looked at, but this may be used to examine the successors of the insn
275    to judge whether a simplification is valid.  */
276
277 static rtx subst_insn;
278
279 /* This is the lowest LUID that `subst' is currently dealing with.
280    get_last_value will not return a value if the register was set at or
281    after this LUID.  If not for this mechanism, we could get confused if
282    I2 or I1 in try_combine were an insn that used the old value of a register
283    to obtain a new value.  In that case, we might erroneously get the
284    new value of the register when we wanted the old one.  */
285
286 static int subst_low_luid;
287
288 /* This contains any hard registers that are used in newpat; reg_dead_at_p
289    must consider all these registers to be always live.  */
290
291 static HARD_REG_SET newpat_used_regs;
292
293 /* This is an insn to which a LOG_LINKS entry has been added.  If this
294    insn is the earlier than I2 or I3, combine should rescan starting at
295    that location.  */
296
297 static rtx added_links_insn;
298
299 /* Basic block in which we are performing combines.  */
300 static basic_block this_basic_block;
301
302 \f
303 /* Length of the currently allocated uid_insn_cost array.  */
304
305 static int max_uid_known;
306
307 /* The following array records the insn_rtx_cost for every insn
308    in the instruction stream.  */
309
310 static int *uid_insn_cost;
311
312 /* The following array records the LOG_LINKS for every insn in the
313    instruction stream as an INSN_LIST rtx.  */
314
315 static rtx *uid_log_links;
316
317 #define INSN_COST(INSN)         (uid_insn_cost[INSN_UID (INSN)])
318 #define LOG_LINKS(INSN)         (uid_log_links[INSN_UID (INSN)])
319
320 /* Incremented for each basic block.  */
321
322 static int label_tick;
323
324 /* Reset to label_tick for each label.  */
325
326 static int label_tick_ebb_start;
327
328 /* Mode used to compute significance in reg_stat[].nonzero_bits.  It is the
329    largest integer mode that can fit in HOST_BITS_PER_WIDE_INT.  */
330
331 static enum machine_mode nonzero_bits_mode;
332
333 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
334    be safely used.  It is zero while computing them and after combine has
335    completed.  This former test prevents propagating values based on
336    previously set values, which can be incorrect if a variable is modified
337    in a loop.  */
338
339 static int nonzero_sign_valid;
340
341 \f
342 /* Record one modification to rtl structure
343    to be undone by storing old_contents into *where.  */
344
345 struct undo
346 {
347   struct undo *next;
348   enum { UNDO_RTX, UNDO_INT, UNDO_MODE } kind;
349   union { rtx r; int i; enum machine_mode m; } old_contents;
350   union { rtx *r; int *i; } where;
351 };
352
353 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
354    num_undo says how many are currently recorded.
355
356    other_insn is nonzero if we have modified some other insn in the process
357    of working on subst_insn.  It must be verified too.  */
358
359 struct undobuf
360 {
361   struct undo *undos;
362   struct undo *frees;
363   rtx other_insn;
364 };
365
366 static struct undobuf undobuf;
367
368 /* Number of times the pseudo being substituted for
369    was found and replaced.  */
370
371 static int n_occurrences;
372
373 static rtx reg_nonzero_bits_for_combine (rtx, enum machine_mode, rtx,
374                                          enum machine_mode,
375                                          unsigned HOST_WIDE_INT,
376                                          unsigned HOST_WIDE_INT *);
377 static rtx reg_num_sign_bit_copies_for_combine (rtx, enum machine_mode, rtx,
378                                                 enum machine_mode,
379                                                 unsigned int, unsigned int *);
380 static void do_SUBST (rtx *, rtx);
381 static void do_SUBST_INT (int *, int);
382 static void init_reg_last (void);
383 static void setup_incoming_promotions (rtx);
384 static void set_nonzero_bits_and_sign_copies (rtx, rtx, void *);
385 static int cant_combine_insn_p (rtx);
386 static int can_combine_p (rtx, rtx, rtx, rtx, rtx *, rtx *);
387 static int combinable_i3pat (rtx, rtx *, rtx, rtx, int, rtx *);
388 static int contains_muldiv (rtx);
389 static rtx try_combine (rtx, rtx, rtx, int *);
390 static void undo_all (void);
391 static void undo_commit (void);
392 static rtx *find_split_point (rtx *, rtx);
393 static rtx subst (rtx, rtx, rtx, int, int);
394 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
395 static rtx simplify_if_then_else (rtx);
396 static rtx simplify_set (rtx);
397 static rtx simplify_logical (rtx);
398 static rtx expand_compound_operation (rtx);
399 static rtx expand_field_assignment (rtx);
400 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
401                             rtx, unsigned HOST_WIDE_INT, int, int, int);
402 static rtx extract_left_shift (rtx, int);
403 static rtx make_compound_operation (rtx, enum rtx_code);
404 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
405                               unsigned HOST_WIDE_INT *);
406 static rtx canon_reg_for_combine (rtx, rtx);
407 static rtx force_to_mode (rtx, enum machine_mode,
408                           unsigned HOST_WIDE_INT, int);
409 static rtx if_then_else_cond (rtx, rtx *, rtx *);
410 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
411 static int rtx_equal_for_field_assignment_p (rtx, rtx);
412 static rtx make_field_assignment (rtx);
413 static rtx apply_distributive_law (rtx);
414 static rtx distribute_and_simplify_rtx (rtx, int);
415 static rtx simplify_and_const_int_1 (enum machine_mode, rtx,
416                                      unsigned HOST_WIDE_INT);
417 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
418                                    unsigned HOST_WIDE_INT);
419 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
420                             HOST_WIDE_INT, enum machine_mode, int *);
421 static rtx simplify_shift_const_1 (enum rtx_code, enum machine_mode, rtx, int);
422 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
423                                  int);
424 static int recog_for_combine (rtx *, rtx, rtx *);
425 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
426 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
427 static void update_table_tick (rtx);
428 static void record_value_for_reg (rtx, rtx, rtx);
429 static void check_conversions (rtx, rtx);
430 static void record_dead_and_set_regs_1 (rtx, rtx, void *);
431 static void record_dead_and_set_regs (rtx);
432 static int get_last_value_validate (rtx *, rtx, int, int);
433 static rtx get_last_value (rtx);
434 static int use_crosses_set_p (rtx, int);
435 static void reg_dead_at_p_1 (rtx, rtx, void *);
436 static int reg_dead_at_p (rtx, rtx);
437 static void move_deaths (rtx, rtx, int, rtx, rtx *);
438 static int reg_bitfield_target_p (rtx, rtx);
439 static void distribute_notes (rtx, rtx, rtx, rtx, rtx, rtx);
440 static void distribute_links (rtx);
441 static void mark_used_regs_combine (rtx);
442 static void record_promoted_value (rtx, rtx);
443 static int unmentioned_reg_p_1 (rtx *, void *);
444 static bool unmentioned_reg_p (rtx, rtx);
445 static void record_truncated_value (rtx);
446 static bool reg_truncated_to_mode (enum machine_mode, rtx);
447 static rtx gen_lowpart_or_truncate (enum machine_mode, rtx);
448 \f
449
450 /* It is not safe to use ordinary gen_lowpart in combine.
451    See comments in gen_lowpart_for_combine.  */
452 #undef RTL_HOOKS_GEN_LOWPART
453 #define RTL_HOOKS_GEN_LOWPART              gen_lowpart_for_combine
454
455 /* Our implementation of gen_lowpart never emits a new pseudo.  */
456 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
457 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT      gen_lowpart_for_combine
458
459 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
460 #define RTL_HOOKS_REG_NONZERO_REG_BITS     reg_nonzero_bits_for_combine
461
462 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
463 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES  reg_num_sign_bit_copies_for_combine
464
465 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
466 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE    reg_truncated_to_mode
467
468 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
469
470 \f
471 /* This is used by find_single_use to locate an rtx in LOC that
472    contains exactly one use of DEST, which is typically either a REG
473    or CC0.  It returns a pointer to the innermost rtx expression
474    containing DEST.  Appearances of DEST that are being used to
475    totally replace it are not counted.  */
476
477 static rtx *
478 find_single_use_1 (rtx dest, rtx *loc)
479 {
480   rtx x = *loc;
481   enum rtx_code code = GET_CODE (x);
482   rtx *result = NULL;
483   rtx *this_result;
484   int i;
485   const char *fmt;
486
487   switch (code)
488     {
489     case CONST_INT:
490     case CONST:
491     case LABEL_REF:
492     case SYMBOL_REF:
493     case CONST_DOUBLE:
494     case CONST_VECTOR:
495     case CLOBBER:
496       return 0;
497
498     case SET:
499       /* If the destination is anything other than CC0, PC, a REG or a SUBREG
500          of a REG that occupies all of the REG, the insn uses DEST if
501          it is mentioned in the destination or the source.  Otherwise, we
502          need just check the source.  */
503       if (GET_CODE (SET_DEST (x)) != CC0
504           && GET_CODE (SET_DEST (x)) != PC
505           && !REG_P (SET_DEST (x))
506           && ! (GET_CODE (SET_DEST (x)) == SUBREG
507                 && REG_P (SUBREG_REG (SET_DEST (x)))
508                 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
509                       + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
510                     == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
511                          + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
512         break;
513
514       return find_single_use_1 (dest, &SET_SRC (x));
515
516     case MEM:
517     case SUBREG:
518       return find_single_use_1 (dest, &XEXP (x, 0));
519
520     default:
521       break;
522     }
523
524   /* If it wasn't one of the common cases above, check each expression and
525      vector of this code.  Look for a unique usage of DEST.  */
526
527   fmt = GET_RTX_FORMAT (code);
528   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
529     {
530       if (fmt[i] == 'e')
531         {
532           if (dest == XEXP (x, i)
533               || (REG_P (dest) && REG_P (XEXP (x, i))
534                   && REGNO (dest) == REGNO (XEXP (x, i))))
535             this_result = loc;
536           else
537             this_result = find_single_use_1 (dest, &XEXP (x, i));
538
539           if (result == NULL)
540             result = this_result;
541           else if (this_result)
542             /* Duplicate usage.  */
543             return NULL;
544         }
545       else if (fmt[i] == 'E')
546         {
547           int j;
548
549           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
550             {
551               if (XVECEXP (x, i, j) == dest
552                   || (REG_P (dest)
553                       && REG_P (XVECEXP (x, i, j))
554                       && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
555                 this_result = loc;
556               else
557                 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
558
559               if (result == NULL)
560                 result = this_result;
561               else if (this_result)
562                 return NULL;
563             }
564         }
565     }
566
567   return result;
568 }
569
570
571 /* See if DEST, produced in INSN, is used only a single time in the
572    sequel.  If so, return a pointer to the innermost rtx expression in which
573    it is used.
574
575    If PLOC is nonzero, *PLOC is set to the insn containing the single use.
576
577    If DEST is cc0_rtx, we look only at the next insn.  In that case, we don't
578    care about REG_DEAD notes or LOG_LINKS.
579
580    Otherwise, we find the single use by finding an insn that has a
581    LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST.  If DEST is
582    only referenced once in that insn, we know that it must be the first
583    and last insn referencing DEST.  */
584
585 static rtx *
586 find_single_use (rtx dest, rtx insn, rtx *ploc)
587 {
588   rtx next;
589   rtx *result;
590   rtx link;
591
592 #ifdef HAVE_cc0
593   if (dest == cc0_rtx)
594     {
595       next = NEXT_INSN (insn);
596       if (next == 0
597           || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
598         return 0;
599
600       result = find_single_use_1 (dest, &PATTERN (next));
601       if (result && ploc)
602         *ploc = next;
603       return result;
604     }
605 #endif
606
607   if (!REG_P (dest))
608     return 0;
609
610   for (next = next_nonnote_insn (insn);
611        next != 0 && !LABEL_P (next);
612        next = next_nonnote_insn (next))
613     if (INSN_P (next) && dead_or_set_p (next, dest))
614       {
615         for (link = LOG_LINKS (next); link; link = XEXP (link, 1))
616           if (XEXP (link, 0) == insn)
617             break;
618
619         if (link)
620           {
621             result = find_single_use_1 (dest, &PATTERN (next));
622             if (ploc)
623               *ploc = next;
624             return result;
625           }
626       }
627
628   return 0;
629 }
630 \f
631 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
632    insn.  The substitution can be undone by undo_all.  If INTO is already
633    set to NEWVAL, do not record this change.  Because computing NEWVAL might
634    also call SUBST, we have to compute it before we put anything into
635    the undo table.  */
636
637 static void
638 do_SUBST (rtx *into, rtx newval)
639 {
640   struct undo *buf;
641   rtx oldval = *into;
642
643   if (oldval == newval)
644     return;
645
646   /* We'd like to catch as many invalid transformations here as
647      possible.  Unfortunately, there are way too many mode changes
648      that are perfectly valid, so we'd waste too much effort for
649      little gain doing the checks here.  Focus on catching invalid
650      transformations involving integer constants.  */
651   if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
652       && GET_CODE (newval) == CONST_INT)
653     {
654       /* Sanity check that we're replacing oldval with a CONST_INT
655          that is a valid sign-extension for the original mode.  */
656       gcc_assert (INTVAL (newval)
657                   == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
658
659       /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
660          CONST_INT is not valid, because after the replacement, the
661          original mode would be gone.  Unfortunately, we can't tell
662          when do_SUBST is called to replace the operand thereof, so we
663          perform this test on oldval instead, checking whether an
664          invalid replacement took place before we got here.  */
665       gcc_assert (!(GET_CODE (oldval) == SUBREG
666                     && GET_CODE (SUBREG_REG (oldval)) == CONST_INT));
667       gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
668                     && GET_CODE (XEXP (oldval, 0)) == CONST_INT));
669     }
670
671   if (undobuf.frees)
672     buf = undobuf.frees, undobuf.frees = buf->next;
673   else
674     buf = XNEW (struct undo);
675
676   buf->kind = UNDO_RTX;
677   buf->where.r = into;
678   buf->old_contents.r = oldval;
679   *into = newval;
680
681   buf->next = undobuf.undos, undobuf.undos = buf;
682 }
683
684 #define SUBST(INTO, NEWVAL)     do_SUBST(&(INTO), (NEWVAL))
685
686 /* Similar to SUBST, but NEWVAL is an int expression.  Note that substitution
687    for the value of a HOST_WIDE_INT value (including CONST_INT) is
688    not safe.  */
689
690 static void
691 do_SUBST_INT (int *into, int newval)
692 {
693   struct undo *buf;
694   int oldval = *into;
695
696   if (oldval == newval)
697     return;
698
699   if (undobuf.frees)
700     buf = undobuf.frees, undobuf.frees = buf->next;
701   else
702     buf = XNEW (struct undo);
703
704   buf->kind = UNDO_INT;
705   buf->where.i = into;
706   buf->old_contents.i = oldval;
707   *into = newval;
708
709   buf->next = undobuf.undos, undobuf.undos = buf;
710 }
711
712 #define SUBST_INT(INTO, NEWVAL)  do_SUBST_INT(&(INTO), (NEWVAL))
713
714 /* Similar to SUBST, but just substitute the mode.  This is used when
715    changing the mode of a pseudo-register, so that any other
716    references to the entry in the regno_reg_rtx array will change as
717    well.  */
718
719 static void
720 do_SUBST_MODE (rtx *into, enum machine_mode newval)
721 {
722   struct undo *buf;
723   enum machine_mode oldval = GET_MODE (*into);
724
725   if (oldval == newval)
726     return;
727
728   if (undobuf.frees)
729     buf = undobuf.frees, undobuf.frees = buf->next;
730   else
731     buf = XNEW (struct undo);
732
733   buf->kind = UNDO_MODE;
734   buf->where.r = into;
735   buf->old_contents.m = oldval;
736   PUT_MODE (*into, newval);
737
738   buf->next = undobuf.undos, undobuf.undos = buf;
739 }
740
741 #define SUBST_MODE(INTO, NEWVAL)  do_SUBST_MODE(&(INTO), (NEWVAL))
742 \f
743 /* Subroutine of try_combine.  Determine whether the combine replacement
744    patterns NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to
745    insn_rtx_cost that the original instruction sequence I1, I2, I3 and
746    undobuf.other_insn.  Note that I1 and/or NEWI2PAT may be NULL_RTX. 
747    NEWOTHERPAT and undobuf.other_insn may also both be NULL_RTX.  This
748    function returns false, if the costs of all instructions can be
749    estimated, and the replacements are more expensive than the original
750    sequence.  */
751
752 static bool
753 combine_validate_cost (rtx i1, rtx i2, rtx i3, rtx newpat, rtx newi2pat,
754                        rtx newotherpat)
755 {
756   int i1_cost, i2_cost, i3_cost;
757   int new_i2_cost, new_i3_cost;
758   int old_cost, new_cost;
759
760   /* Lookup the original insn_rtx_costs.  */
761   i2_cost = INSN_COST (i2);
762   i3_cost = INSN_COST (i3);
763
764   if (i1)
765     {
766       i1_cost = INSN_COST (i1);
767       old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0)
768                  ? i1_cost + i2_cost + i3_cost : 0;
769     }
770   else
771     {
772       old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
773       i1_cost = 0;
774     }
775
776   /* Calculate the replacement insn_rtx_costs.  */
777   new_i3_cost = insn_rtx_cost (newpat);
778   if (newi2pat)
779     {
780       new_i2_cost = insn_rtx_cost (newi2pat);
781       new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
782                  ? new_i2_cost + new_i3_cost : 0;
783     }
784   else
785     {
786       new_cost = new_i3_cost;
787       new_i2_cost = 0;
788     }
789
790   if (undobuf.other_insn)
791     {
792       int old_other_cost, new_other_cost;
793
794       old_other_cost = INSN_COST (undobuf.other_insn);
795       new_other_cost = insn_rtx_cost (newotherpat);
796       if (old_other_cost > 0 && new_other_cost > 0)
797         {
798           old_cost += old_other_cost;
799           new_cost += new_other_cost;
800         }
801       else
802         old_cost = 0;
803     }
804
805   /* Disallow this recombination if both new_cost and old_cost are
806      greater than zero, and new_cost is greater than old cost.  */
807   if (old_cost > 0
808       && new_cost > old_cost)
809     {
810       if (dump_file)
811         {
812           if (i1)
813             {
814               fprintf (dump_file,
815                        "rejecting combination of insns %d, %d and %d\n",
816                        INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
817               fprintf (dump_file, "original costs %d + %d + %d = %d\n",
818                        i1_cost, i2_cost, i3_cost, old_cost);
819             }
820           else
821             {
822               fprintf (dump_file,
823                        "rejecting combination of insns %d and %d\n",
824                        INSN_UID (i2), INSN_UID (i3));
825               fprintf (dump_file, "original costs %d + %d = %d\n",
826                        i2_cost, i3_cost, old_cost);
827             }
828
829           if (newi2pat)
830             {
831               fprintf (dump_file, "replacement costs %d + %d = %d\n",
832                        new_i2_cost, new_i3_cost, new_cost);
833             }
834           else
835             fprintf (dump_file, "replacement cost %d\n", new_cost);
836         }
837
838       return false;
839     }
840
841   /* Update the uid_insn_cost array with the replacement costs.  */
842   INSN_COST (i2) = new_i2_cost;
843   INSN_COST (i3) = new_i3_cost;
844   if (i1)
845     INSN_COST (i1) = 0;
846
847   return true;
848 }
849
850
851 /* Delete any insns that copy a register to itself.  */
852
853 static void
854 delete_noop_moves (void)
855 {
856   rtx insn, next;
857   basic_block bb;
858
859   FOR_EACH_BB (bb)
860     {
861       for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
862         {
863           next = NEXT_INSN (insn);
864           if (INSN_P (insn) && noop_move_p (insn))
865             {
866               rtx note;
867
868               /* If we're about to remove the first insn of a libcall
869                  then move the libcall note to the next real insn and
870                  update the retval note.  */
871               if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX))
872                        && XEXP (note, 0) != insn)
873                 {
874                   rtx new_libcall_insn = next_real_insn (insn);
875                   rtx retval_note = find_reg_note (XEXP (note, 0),
876                                                    REG_RETVAL, NULL_RTX);
877                   REG_NOTES (new_libcall_insn)
878                     = gen_rtx_INSN_LIST (REG_LIBCALL, XEXP (note, 0),
879                                          REG_NOTES (new_libcall_insn));
880                   XEXP (retval_note, 0) = new_libcall_insn;
881                 }
882
883               if (dump_file)
884                 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
885
886               delete_insn_and_edges (insn);
887             }
888         }
889     }
890 }
891
892 \f
893 /* Fill in log links field for all insns.  */
894
895 static void
896 create_log_links (void)
897 {
898   basic_block bb;
899   rtx *next_use, insn;
900   struct df_ref **def_vec, **use_vec;
901
902   next_use = XCNEWVEC (rtx, max_reg_num ());
903
904   /* Pass through each block from the end, recording the uses of each
905      register and establishing log links when def is encountered.
906      Note that we do not clear next_use array in order to save time,
907      so we have to test whether the use is in the same basic block as def.
908               
909      There are a few cases below when we do not consider the definition or
910      usage -- these are taken from original flow.c did. Don't ask me why it is
911      done this way; I don't know and if it works, I don't want to know.  */
912
913   FOR_EACH_BB (bb)
914     {
915       FOR_BB_INSNS_REVERSE (bb, insn)
916         {
917           if (!INSN_P (insn))
918             continue;
919
920           /* Log links are created only once.  */
921           gcc_assert (!LOG_LINKS (insn));
922
923           for (def_vec = DF_INSN_DEFS (insn); *def_vec; def_vec++)
924             {
925               struct df_ref *def = *def_vec;
926               int regno = DF_REF_REGNO (def);
927               rtx use_insn;
928
929               if (!next_use[regno])
930                 continue;
931
932               /* Do not consider if it is pre/post modification in MEM.  */
933               if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
934                 continue;
935
936               /* Do not make the log link for frame pointer.  */
937               if ((regno == FRAME_POINTER_REGNUM
938                    && (! reload_completed || frame_pointer_needed))
939 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
940                   || (regno == HARD_FRAME_POINTER_REGNUM
941                       && (! reload_completed || frame_pointer_needed))
942 #endif
943 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
944                   || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
945 #endif
946                   )
947                 continue;
948
949               use_insn = next_use[regno];
950               if (BLOCK_FOR_INSN (use_insn) == bb)
951                 {
952                   /* flow.c claimed:
953
954                      We don't build a LOG_LINK for hard registers contained
955                      in ASM_OPERANDs.  If these registers get replaced,
956                      we might wind up changing the semantics of the insn,
957                      even if reload can make what appear to be valid
958                      assignments later.  */
959                   if (regno >= FIRST_PSEUDO_REGISTER
960                       || asm_noperands (PATTERN (use_insn)) < 0)
961                     LOG_LINKS (use_insn) =
962                       alloc_INSN_LIST (insn, LOG_LINKS (use_insn));
963                 }
964               next_use[regno] = NULL_RTX;
965             }
966
967           for (use_vec = DF_INSN_USES (insn); *use_vec; use_vec++)
968             {
969               struct df_ref *use = *use_vec;
970               int regno = DF_REF_REGNO (use);
971
972               /* Do not consider the usage of the stack pointer
973                  by function call.  */
974               if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
975                 continue;
976
977               next_use[regno] = insn;
978             }
979         }
980     }
981
982   free (next_use);
983 }
984
985 /* Clear LOG_LINKS fields of insns.  */
986
987 static void
988 clear_log_links (void)
989 {
990   rtx insn;
991
992   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
993     if (INSN_P (insn))
994       free_INSN_LIST_list (&LOG_LINKS (insn));
995 }
996
997
998
999 \f
1000 /* Main entry point for combiner.  F is the first insn of the function.
1001    NREGS is the first unused pseudo-reg number.
1002
1003    Return nonzero if the combiner has turned an indirect jump
1004    instruction into a direct jump.  */
1005 static int
1006 combine_instructions (rtx f, unsigned int nregs)
1007 {
1008   rtx insn, next;
1009 #ifdef HAVE_cc0
1010   rtx prev;
1011 #endif
1012   rtx links, nextlinks;
1013   rtx first;
1014
1015   int new_direct_jump_p = 0;
1016
1017   for (first = f; first && !INSN_P (first); )
1018     first = NEXT_INSN (first);
1019   if (!first)
1020     return 0;
1021
1022   combine_attempts = 0;
1023   combine_merges = 0;
1024   combine_extras = 0;
1025   combine_successes = 0;
1026
1027   combine_max_regno = nregs;
1028
1029   rtl_hooks = combine_rtl_hooks;
1030
1031   reg_stat = XCNEWVEC (struct reg_stat, nregs);
1032
1033   init_recog_no_volatile ();
1034
1035   /* Allocate array for insn info.  */
1036   max_uid_known = get_max_uid ();
1037   uid_log_links = XCNEWVEC (rtx, max_uid_known + 1);
1038   uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1039
1040   nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1041
1042   /* Don't use reg_stat[].nonzero_bits when computing it.  This can cause
1043      problems when, for example, we have j <<= 1 in a loop.  */
1044
1045   nonzero_sign_valid = 0;
1046
1047   /* Scan all SETs and see if we can deduce anything about what
1048      bits are known to be zero for some registers and how many copies
1049      of the sign bit are known to exist for those registers.
1050
1051      Also set any known values so that we can use it while searching
1052      for what bits are known to be set.  */
1053
1054   label_tick = label_tick_ebb_start = 1;
1055
1056   setup_incoming_promotions (first);
1057
1058   create_log_links ();
1059   FOR_EACH_BB (this_basic_block)
1060     {
1061       last_call_luid = 0;
1062       mem_last_set = -1;
1063       label_tick++;
1064       FOR_BB_INSNS (this_basic_block, insn)
1065         if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1066           {
1067             subst_low_luid = DF_INSN_LUID (insn);
1068             subst_insn = insn;
1069
1070             note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1071                          insn);
1072             record_dead_and_set_regs (insn);
1073
1074 #ifdef AUTO_INC_DEC
1075             for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1076               if (REG_NOTE_KIND (links) == REG_INC)
1077                 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1078                                                   insn);
1079 #endif
1080
1081             /* Record the current insn_rtx_cost of this instruction.  */
1082             if (NONJUMP_INSN_P (insn))
1083               INSN_COST (insn) = insn_rtx_cost (PATTERN (insn));
1084             if (dump_file)
1085               fprintf(dump_file, "insn_cost %d: %d\n",
1086                     INSN_UID (insn), INSN_COST (insn));
1087           }
1088         else if (LABEL_P (insn))
1089           label_tick_ebb_start = label_tick;
1090     }
1091
1092   nonzero_sign_valid = 1;
1093
1094   /* Now scan all the insns in forward order.  */
1095
1096   label_tick = label_tick_ebb_start = 1;
1097   init_reg_last ();
1098   setup_incoming_promotions (first);
1099
1100   FOR_EACH_BB (this_basic_block)
1101     {
1102       last_call_luid = 0;
1103       mem_last_set = -1;
1104       label_tick++;
1105       for (insn = BB_HEAD (this_basic_block);
1106            insn != NEXT_INSN (BB_END (this_basic_block));
1107            insn = next ? next : NEXT_INSN (insn))
1108         {
1109           next = 0;
1110           if (INSN_P (insn))
1111             {
1112               /* See if we know about function return values before this
1113                  insn based upon SUBREG flags.  */
1114               check_conversions (insn, PATTERN (insn));
1115
1116               /* Try this insn with each insn it links back to.  */
1117
1118               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1119                 if ((next = try_combine (insn, XEXP (links, 0),
1120                                          NULL_RTX, &new_direct_jump_p)) != 0)
1121                   goto retry;
1122
1123               /* Try each sequence of three linked insns ending with this one.  */
1124
1125               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1126                 {
1127                   rtx link = XEXP (links, 0);
1128
1129                   /* If the linked insn has been replaced by a note, then there
1130                      is no point in pursuing this chain any further.  */
1131                   if (NOTE_P (link))
1132                     continue;
1133
1134                   for (nextlinks = LOG_LINKS (link);
1135                        nextlinks;
1136                        nextlinks = XEXP (nextlinks, 1))
1137                     if ((next = try_combine (insn, link,
1138                                              XEXP (nextlinks, 0),
1139                                              &new_direct_jump_p)) != 0)
1140                       goto retry;
1141                 }
1142
1143 #ifdef HAVE_cc0
1144               /* Try to combine a jump insn that uses CC0
1145                  with a preceding insn that sets CC0, and maybe with its
1146                  logical predecessor as well.
1147                  This is how we make decrement-and-branch insns.
1148                  We need this special code because data flow connections
1149                  via CC0 do not get entered in LOG_LINKS.  */
1150
1151               if (JUMP_P (insn)
1152                   && (prev = prev_nonnote_insn (insn)) != 0
1153                   && NONJUMP_INSN_P (prev)
1154                   && sets_cc0_p (PATTERN (prev)))
1155                 {
1156                   if ((next = try_combine (insn, prev,
1157                                            NULL_RTX, &new_direct_jump_p)) != 0)
1158                     goto retry;
1159
1160                   for (nextlinks = LOG_LINKS (prev); nextlinks;
1161                        nextlinks = XEXP (nextlinks, 1))
1162                     if ((next = try_combine (insn, prev,
1163                                              XEXP (nextlinks, 0),
1164                                              &new_direct_jump_p)) != 0)
1165                       goto retry;
1166                 }
1167
1168               /* Do the same for an insn that explicitly references CC0.  */
1169               if (NONJUMP_INSN_P (insn)
1170                   && (prev = prev_nonnote_insn (insn)) != 0
1171                   && NONJUMP_INSN_P (prev)
1172                   && sets_cc0_p (PATTERN (prev))
1173                   && GET_CODE (PATTERN (insn)) == SET
1174                   && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1175                 {
1176                   if ((next = try_combine (insn, prev,
1177                                            NULL_RTX, &new_direct_jump_p)) != 0)
1178                     goto retry;
1179
1180                   for (nextlinks = LOG_LINKS (prev); nextlinks;
1181                        nextlinks = XEXP (nextlinks, 1))
1182                     if ((next = try_combine (insn, prev,
1183                                              XEXP (nextlinks, 0),
1184                                              &new_direct_jump_p)) != 0)
1185                       goto retry;
1186                 }
1187
1188               /* Finally, see if any of the insns that this insn links to
1189                  explicitly references CC0.  If so, try this insn, that insn,
1190                  and its predecessor if it sets CC0.  */
1191               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1192                 if (NONJUMP_INSN_P (XEXP (links, 0))
1193                     && GET_CODE (PATTERN (XEXP (links, 0))) == SET
1194                     && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
1195                     && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
1196                     && NONJUMP_INSN_P (prev)
1197                     && sets_cc0_p (PATTERN (prev))
1198                     && (next = try_combine (insn, XEXP (links, 0),
1199                                             prev, &new_direct_jump_p)) != 0)
1200                   goto retry;
1201 #endif
1202
1203               /* Try combining an insn with two different insns whose results it
1204                  uses.  */
1205               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1206                 for (nextlinks = XEXP (links, 1); nextlinks;
1207                      nextlinks = XEXP (nextlinks, 1))
1208                   if ((next = try_combine (insn, XEXP (links, 0),
1209                                            XEXP (nextlinks, 0),
1210                                            &new_direct_jump_p)) != 0)
1211                     goto retry;
1212
1213               /* Try this insn with each REG_EQUAL note it links back to.  */
1214               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1215                 {
1216                   rtx set, note;
1217                   rtx temp = XEXP (links, 0);
1218                   if ((set = single_set (temp)) != 0
1219                       && (note = find_reg_equal_equiv_note (temp)) != 0
1220                       && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1221                       /* Avoid using a register that may already been marked
1222                          dead by an earlier instruction.  */
1223                       && ! unmentioned_reg_p (note, SET_SRC (set))
1224                       && (GET_MODE (note) == VOIDmode
1225                           ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1226                           : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1227                     {
1228                       /* Temporarily replace the set's source with the
1229                          contents of the REG_EQUAL note.  The insn will
1230                          be deleted or recognized by try_combine.  */
1231                       rtx orig = SET_SRC (set);
1232                       SET_SRC (set) = note;
1233                       i2mod = temp;
1234                       i2mod_old_rhs = copy_rtx (orig);
1235                       i2mod_new_rhs = copy_rtx (note);
1236                       next = try_combine (insn, i2mod, NULL_RTX,
1237                                           &new_direct_jump_p);
1238                       i2mod = NULL_RTX;
1239                       if (next)
1240                         goto retry;
1241                       SET_SRC (set) = orig;
1242                     }
1243                 }
1244
1245               if (!NOTE_P (insn))
1246                 record_dead_and_set_regs (insn);
1247
1248             retry:
1249               ;
1250             }
1251           else if (LABEL_P (insn))
1252             label_tick_ebb_start = label_tick;
1253         }
1254     }
1255
1256   clear_log_links ();
1257   clear_bb_flags ();
1258   new_direct_jump_p |= purge_all_dead_edges ();
1259   delete_noop_moves ();
1260
1261   /* Clean up.  */
1262   free (uid_log_links);
1263   free (uid_insn_cost);
1264   free (reg_stat);
1265
1266   {
1267     struct undo *undo, *next;
1268     for (undo = undobuf.frees; undo; undo = next)
1269       {
1270         next = undo->next;
1271         free (undo);
1272       }
1273     undobuf.frees = 0;
1274   }
1275
1276   total_attempts += combine_attempts;
1277   total_merges += combine_merges;
1278   total_extras += combine_extras;
1279   total_successes += combine_successes;
1280
1281   nonzero_sign_valid = 0;
1282   rtl_hooks = general_rtl_hooks;
1283
1284   /* Make recognizer allow volatile MEMs again.  */
1285   init_recog ();
1286
1287   return new_direct_jump_p;
1288 }
1289
1290 /* Wipe the last_xxx fields of reg_stat in preparation for another pass.  */
1291
1292 static void
1293 init_reg_last (void)
1294 {
1295   unsigned int i;
1296   for (i = 0; i < combine_max_regno; i++)
1297     memset (reg_stat + i, 0, offsetof (struct reg_stat, sign_bit_copies));
1298 }
1299 \f
1300 /* Set up any promoted values for incoming argument registers.  */
1301
1302 static void
1303 setup_incoming_promotions (rtx first)
1304 {
1305   tree arg;
1306
1307   if (!targetm.calls.promote_function_args (TREE_TYPE (cfun->decl)))
1308     return;
1309
1310   for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1311        arg = TREE_CHAIN (arg))
1312     {
1313       rtx reg = DECL_INCOMING_RTL (arg);
1314
1315       if (!REG_P (reg))
1316         continue;
1317
1318       if (TYPE_MODE (DECL_ARG_TYPE (arg)) == TYPE_MODE (TREE_TYPE (arg)))
1319         {
1320           enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg));
1321           int uns = TYPE_UNSIGNED (TREE_TYPE (arg));
1322
1323           mode = promote_mode (TREE_TYPE (arg), mode, &uns, 1);
1324           if (mode == GET_MODE (reg) && mode != DECL_MODE (arg))
1325             {
1326               rtx x;
1327               x = gen_rtx_CLOBBER (DECL_MODE (arg), const0_rtx);
1328               x = gen_rtx_fmt_e ((uns ? ZERO_EXTEND : SIGN_EXTEND), mode, x);
1329               record_value_for_reg (reg, first, x);
1330             }
1331         }
1332     }
1333 }
1334 \f
1335 /* Called via note_stores.  If X is a pseudo that is narrower than
1336    HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1337
1338    If we are setting only a portion of X and we can't figure out what
1339    portion, assume all bits will be used since we don't know what will
1340    be happening.
1341
1342    Similarly, set how many bits of X are known to be copies of the sign bit
1343    at all locations in the function.  This is the smallest number implied
1344    by any set of X.  */
1345
1346 static void
1347 set_nonzero_bits_and_sign_copies (rtx x, rtx set, void *data)
1348 {
1349   rtx insn = (rtx) data;
1350   unsigned int num;
1351
1352   if (REG_P (x)
1353       && REGNO (x) >= FIRST_PSEUDO_REGISTER
1354       /* If this register is undefined at the start of the file, we can't
1355          say what its contents were.  */
1356       && ! REGNO_REG_SET_P
1357            (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x))
1358       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
1359     {
1360       if (set == 0 || GET_CODE (set) == CLOBBER)
1361         {
1362           reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1363           reg_stat[REGNO (x)].sign_bit_copies = 1;
1364           return;
1365         }
1366
1367       /* If this register is being initialized using itself, and the
1368          register is uninitialized in this basic block, and there are
1369          no LOG_LINKS which set the register, then part of the
1370          register is uninitialized.  In that case we can't assume
1371          anything about the number of nonzero bits.
1372
1373          ??? We could do better if we checked this in
1374          reg_{nonzero_bits,num_sign_bit_copies}_for_combine.  Then we
1375          could avoid making assumptions about the insn which initially
1376          sets the register, while still using the information in other
1377          insns.  We would have to be careful to check every insn
1378          involved in the combination.  */
1379
1380       if (insn
1381           && reg_referenced_p (x, PATTERN (insn))
1382           && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1383                                REGNO (x)))
1384         {
1385           rtx link;
1386
1387           for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
1388             {
1389               if (dead_or_set_p (XEXP (link, 0), x))
1390                 break;
1391             }
1392           if (!link)
1393             {
1394               reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1395               reg_stat[REGNO (x)].sign_bit_copies = 1;
1396               return;
1397             }
1398         }
1399
1400       /* If this is a complex assignment, see if we can convert it into a
1401          simple assignment.  */
1402       set = expand_field_assignment (set);
1403
1404       /* If this is a simple assignment, or we have a paradoxical SUBREG,
1405          set what we know about X.  */
1406
1407       if (SET_DEST (set) == x
1408           || (GET_CODE (SET_DEST (set)) == SUBREG
1409               && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
1410                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
1411               && SUBREG_REG (SET_DEST (set)) == x))
1412         {
1413           rtx src = SET_SRC (set);
1414
1415 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1416           /* If X is narrower than a word and SRC is a non-negative
1417              constant that would appear negative in the mode of X,
1418              sign-extend it for use in reg_stat[].nonzero_bits because some
1419              machines (maybe most) will actually do the sign-extension
1420              and this is the conservative approach.
1421
1422              ??? For 2.5, try to tighten up the MD files in this regard
1423              instead of this kludge.  */
1424
1425           if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
1426               && GET_CODE (src) == CONST_INT
1427               && INTVAL (src) > 0
1428               && 0 != (INTVAL (src)
1429                        & ((HOST_WIDE_INT) 1
1430                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
1431             src = GEN_INT (INTVAL (src)
1432                            | ((HOST_WIDE_INT) (-1)
1433                               << GET_MODE_BITSIZE (GET_MODE (x))));
1434 #endif
1435
1436           /* Don't call nonzero_bits if it cannot change anything.  */
1437           if (reg_stat[REGNO (x)].nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1438             reg_stat[REGNO (x)].nonzero_bits
1439               |= nonzero_bits (src, nonzero_bits_mode);
1440           num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1441           if (reg_stat[REGNO (x)].sign_bit_copies == 0
1442               || reg_stat[REGNO (x)].sign_bit_copies > num)
1443             reg_stat[REGNO (x)].sign_bit_copies = num;
1444         }
1445       else
1446         {
1447           reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1448           reg_stat[REGNO (x)].sign_bit_copies = 1;
1449         }
1450     }
1451 }
1452 \f
1453 /* See if INSN can be combined into I3.  PRED and SUCC are optionally
1454    insns that were previously combined into I3 or that will be combined
1455    into the merger of INSN and I3.
1456
1457    Return 0 if the combination is not allowed for any reason.
1458
1459    If the combination is allowed, *PDEST will be set to the single
1460    destination of INSN and *PSRC to the single source, and this function
1461    will return 1.  */
1462
1463 static int
1464 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
1465                rtx *pdest, rtx *psrc)
1466 {
1467   int i;
1468   rtx set = 0, src, dest;
1469   rtx p;
1470 #ifdef AUTO_INC_DEC
1471   rtx link;
1472 #endif
1473   int all_adjacent = (succ ? (next_active_insn (insn) == succ
1474                               && next_active_insn (succ) == i3)
1475                       : next_active_insn (insn) == i3);
1476
1477   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1478      or a PARALLEL consisting of such a SET and CLOBBERs.
1479
1480      If INSN has CLOBBER parallel parts, ignore them for our processing.
1481      By definition, these happen during the execution of the insn.  When it
1482      is merged with another insn, all bets are off.  If they are, in fact,
1483      needed and aren't also supplied in I3, they may be added by
1484      recog_for_combine.  Otherwise, it won't match.
1485
1486      We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1487      note.
1488
1489      Get the source and destination of INSN.  If more than one, can't
1490      combine.  */
1491
1492   if (GET_CODE (PATTERN (insn)) == SET)
1493     set = PATTERN (insn);
1494   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1495            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1496     {
1497       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1498         {
1499           rtx elt = XVECEXP (PATTERN (insn), 0, i);
1500           rtx note;
1501
1502           switch (GET_CODE (elt))
1503             {
1504             /* This is important to combine floating point insns
1505                for the SH4 port.  */
1506             case USE:
1507               /* Combining an isolated USE doesn't make sense.
1508                  We depend here on combinable_i3pat to reject them.  */
1509               /* The code below this loop only verifies that the inputs of
1510                  the SET in INSN do not change.  We call reg_set_between_p
1511                  to verify that the REG in the USE does not change between
1512                  I3 and INSN.
1513                  If the USE in INSN was for a pseudo register, the matching
1514                  insn pattern will likely match any register; combining this
1515                  with any other USE would only be safe if we knew that the
1516                  used registers have identical values, or if there was
1517                  something to tell them apart, e.g. different modes.  For
1518                  now, we forgo such complicated tests and simply disallow
1519                  combining of USES of pseudo registers with any other USE.  */
1520               if (REG_P (XEXP (elt, 0))
1521                   && GET_CODE (PATTERN (i3)) == PARALLEL)
1522                 {
1523                   rtx i3pat = PATTERN (i3);
1524                   int i = XVECLEN (i3pat, 0) - 1;
1525                   unsigned int regno = REGNO (XEXP (elt, 0));
1526
1527                   do
1528                     {
1529                       rtx i3elt = XVECEXP (i3pat, 0, i);
1530
1531                       if (GET_CODE (i3elt) == USE
1532                           && REG_P (XEXP (i3elt, 0))
1533                           && (REGNO (XEXP (i3elt, 0)) == regno
1534                               ? reg_set_between_p (XEXP (elt, 0),
1535                                                    PREV_INSN (insn), i3)
1536                               : regno >= FIRST_PSEUDO_REGISTER))
1537                         return 0;
1538                     }
1539                   while (--i >= 0);
1540                 }
1541               break;
1542
1543               /* We can ignore CLOBBERs.  */
1544             case CLOBBER:
1545               break;
1546
1547             case SET:
1548               /* Ignore SETs whose result isn't used but not those that
1549                  have side-effects.  */
1550               if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1551                   && (!(note = find_reg_note (insn, REG_EH_REGION, NULL_RTX))
1552                       || INTVAL (XEXP (note, 0)) <= 0)
1553                   && ! side_effects_p (elt))
1554                 break;
1555
1556               /* If we have already found a SET, this is a second one and
1557                  so we cannot combine with this insn.  */
1558               if (set)
1559                 return 0;
1560
1561               set = elt;
1562               break;
1563
1564             default:
1565               /* Anything else means we can't combine.  */
1566               return 0;
1567             }
1568         }
1569
1570       if (set == 0
1571           /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1572              so don't do anything with it.  */
1573           || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1574         return 0;
1575     }
1576   else
1577     return 0;
1578
1579   if (set == 0)
1580     return 0;
1581
1582   set = expand_field_assignment (set);
1583   src = SET_SRC (set), dest = SET_DEST (set);
1584
1585   /* Don't eliminate a store in the stack pointer.  */
1586   if (dest == stack_pointer_rtx
1587       /* Don't combine with an insn that sets a register to itself if it has
1588          a REG_EQUAL note.  This may be part of a REG_NO_CONFLICT sequence.  */
1589       || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1590       /* Can't merge an ASM_OPERANDS.  */
1591       || GET_CODE (src) == ASM_OPERANDS
1592       /* Can't merge a function call.  */
1593       || GET_CODE (src) == CALL
1594       /* Don't eliminate a function call argument.  */
1595       || (CALL_P (i3)
1596           && (find_reg_fusage (i3, USE, dest)
1597               || (REG_P (dest)
1598                   && REGNO (dest) < FIRST_PSEUDO_REGISTER
1599                   && global_regs[REGNO (dest)])))
1600       /* Don't substitute into an incremented register.  */
1601       || FIND_REG_INC_NOTE (i3, dest)
1602       || (succ && FIND_REG_INC_NOTE (succ, dest))
1603       /* Don't substitute into a non-local goto, this confuses CFG.  */
1604       || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1605 #if 0
1606       /* Don't combine the end of a libcall into anything.  */
1607       /* ??? This gives worse code, and appears to be unnecessary, since no
1608          pass after flow uses REG_LIBCALL/REG_RETVAL notes.  Local-alloc does
1609          use REG_RETVAL notes for noconflict blocks, but other code here
1610          makes sure that those insns don't disappear.  */
1611       || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1612 #endif
1613       /* Make sure that DEST is not used after SUCC but before I3.  */
1614       || (succ && ! all_adjacent
1615           && reg_used_between_p (dest, succ, i3))
1616       /* Make sure that the value that is to be substituted for the register
1617          does not use any registers whose values alter in between.  However,
1618          If the insns are adjacent, a use can't cross a set even though we
1619          think it might (this can happen for a sequence of insns each setting
1620          the same destination; last_set of that register might point to
1621          a NOTE).  If INSN has a REG_EQUIV note, the register is always
1622          equivalent to the memory so the substitution is valid even if there
1623          are intervening stores.  Also, don't move a volatile asm or
1624          UNSPEC_VOLATILE across any other insns.  */
1625       || (! all_adjacent
1626           && (((!MEM_P (src)
1627                 || ! find_reg_note (insn, REG_EQUIV, src))
1628                && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1629               || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1630               || GET_CODE (src) == UNSPEC_VOLATILE))
1631       /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1632          better register allocation by not doing the combine.  */
1633       || find_reg_note (i3, REG_NO_CONFLICT, dest)
1634       || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1635       /* Don't combine across a CALL_INSN, because that would possibly
1636          change whether the life span of some REGs crosses calls or not,
1637          and it is a pain to update that information.
1638          Exception: if source is a constant, moving it later can't hurt.
1639          Accept that special case, because it helps -fforce-addr a lot.  */
1640       || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1641     return 0;
1642
1643   /* DEST must either be a REG or CC0.  */
1644   if (REG_P (dest))
1645     {
1646       /* If register alignment is being enforced for multi-word items in all
1647          cases except for parameters, it is possible to have a register copy
1648          insn referencing a hard register that is not allowed to contain the
1649          mode being copied and which would not be valid as an operand of most
1650          insns.  Eliminate this problem by not combining with such an insn.
1651
1652          Also, on some machines we don't want to extend the life of a hard
1653          register.  */
1654
1655       if (REG_P (src)
1656           && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1657                && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1658               /* Don't extend the life of a hard register unless it is
1659                  user variable (if we have few registers) or it can't
1660                  fit into the desired register (meaning something special
1661                  is going on).
1662                  Also avoid substituting a return register into I3, because
1663                  reload can't handle a conflict with constraints of other
1664                  inputs.  */
1665               || (REGNO (src) < FIRST_PSEUDO_REGISTER
1666                   && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1667         return 0;
1668     }
1669   else if (GET_CODE (dest) != CC0)
1670     return 0;
1671
1672
1673   if (GET_CODE (PATTERN (i3)) == PARALLEL)
1674     for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1675       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1676         {
1677           /* Don't substitute for a register intended as a clobberable
1678              operand.  */
1679           rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1680           if (rtx_equal_p (reg, dest))
1681             return 0;
1682
1683           /* If the clobber represents an earlyclobber operand, we must not
1684              substitute an expression containing the clobbered register.
1685              As we do not analyze the constraint strings here, we have to
1686              make the conservative assumption.  However, if the register is
1687              a fixed hard reg, the clobber cannot represent any operand;
1688              we leave it up to the machine description to either accept or
1689              reject use-and-clobber patterns.  */
1690           if (!REG_P (reg)
1691               || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1692               || !fixed_regs[REGNO (reg)])
1693             if (reg_overlap_mentioned_p (reg, src))
1694               return 0;
1695         }
1696
1697   /* If INSN contains anything volatile, or is an `asm' (whether volatile
1698      or not), reject, unless nothing volatile comes between it and I3 */
1699
1700   if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1701     {
1702       /* Make sure succ doesn't contain a volatile reference.  */
1703       if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1704         return 0;
1705
1706       for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1707         if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1708           return 0;
1709     }
1710
1711   /* If INSN is an asm, and DEST is a hard register, reject, since it has
1712      to be an explicit register variable, and was chosen for a reason.  */
1713
1714   if (GET_CODE (src) == ASM_OPERANDS
1715       && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1716     return 0;
1717
1718   /* If there are any volatile insns between INSN and I3, reject, because
1719      they might affect machine state.  */
1720
1721   for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1722     if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1723       return 0;
1724
1725   /* If INSN contains an autoincrement or autodecrement, make sure that
1726      register is not used between there and I3, and not already used in
1727      I3 either.  Neither must it be used in PRED or SUCC, if they exist.
1728      Also insist that I3 not be a jump; if it were one
1729      and the incremented register were spilled, we would lose.  */
1730
1731 #ifdef AUTO_INC_DEC
1732   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1733     if (REG_NOTE_KIND (link) == REG_INC
1734         && (JUMP_P (i3)
1735             || reg_used_between_p (XEXP (link, 0), insn, i3)
1736             || (pred != NULL_RTX
1737                 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1738             || (succ != NULL_RTX
1739                 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
1740             || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1741       return 0;
1742 #endif
1743
1744 #ifdef HAVE_cc0
1745   /* Don't combine an insn that follows a CC0-setting insn.
1746      An insn that uses CC0 must not be separated from the one that sets it.
1747      We do, however, allow I2 to follow a CC0-setting insn if that insn
1748      is passed as I1; in that case it will be deleted also.
1749      We also allow combining in this case if all the insns are adjacent
1750      because that would leave the two CC0 insns adjacent as well.
1751      It would be more logical to test whether CC0 occurs inside I1 or I2,
1752      but that would be much slower, and this ought to be equivalent.  */
1753
1754   p = prev_nonnote_insn (insn);
1755   if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1756       && ! all_adjacent)
1757     return 0;
1758 #endif
1759
1760   /* If we get here, we have passed all the tests and the combination is
1761      to be allowed.  */
1762
1763   *pdest = dest;
1764   *psrc = src;
1765
1766   return 1;
1767 }
1768 \f
1769 /* LOC is the location within I3 that contains its pattern or the component
1770    of a PARALLEL of the pattern.  We validate that it is valid for combining.
1771
1772    One problem is if I3 modifies its output, as opposed to replacing it
1773    entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1774    so would produce an insn that is not equivalent to the original insns.
1775
1776    Consider:
1777
1778          (set (reg:DI 101) (reg:DI 100))
1779          (set (subreg:SI (reg:DI 101) 0) <foo>)
1780
1781    This is NOT equivalent to:
1782
1783          (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1784                     (set (reg:DI 101) (reg:DI 100))])
1785
1786    Not only does this modify 100 (in which case it might still be valid
1787    if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1788
1789    We can also run into a problem if I2 sets a register that I1
1790    uses and I1 gets directly substituted into I3 (not via I2).  In that
1791    case, we would be getting the wrong value of I2DEST into I3, so we
1792    must reject the combination.  This case occurs when I2 and I1 both
1793    feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1794    If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1795    of a SET must prevent combination from occurring.
1796
1797    Before doing the above check, we first try to expand a field assignment
1798    into a set of logical operations.
1799
1800    If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1801    we place a register that is both set and used within I3.  If more than one
1802    such register is detected, we fail.
1803
1804    Return 1 if the combination is valid, zero otherwise.  */
1805
1806 static int
1807 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
1808                   int i1_not_in_src, rtx *pi3dest_killed)
1809 {
1810   rtx x = *loc;
1811
1812   if (GET_CODE (x) == SET)
1813     {
1814       rtx set = x ;
1815       rtx dest = SET_DEST (set);
1816       rtx src = SET_SRC (set);
1817       rtx inner_dest = dest;
1818       rtx subdest;
1819
1820       while (GET_CODE (inner_dest) == STRICT_LOW_PART
1821              || GET_CODE (inner_dest) == SUBREG
1822              || GET_CODE (inner_dest) == ZERO_EXTRACT)
1823         inner_dest = XEXP (inner_dest, 0);
1824
1825       /* Check for the case where I3 modifies its output, as discussed
1826          above.  We don't want to prevent pseudos from being combined
1827          into the address of a MEM, so only prevent the combination if
1828          i1 or i2 set the same MEM.  */
1829       if ((inner_dest != dest &&
1830            (!MEM_P (inner_dest)
1831             || rtx_equal_p (i2dest, inner_dest)
1832             || (i1dest && rtx_equal_p (i1dest, inner_dest)))
1833            && (reg_overlap_mentioned_p (i2dest, inner_dest)
1834                || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1835
1836           /* This is the same test done in can_combine_p except we can't test
1837              all_adjacent; we don't have to, since this instruction will stay
1838              in place, thus we are not considering increasing the lifetime of
1839              INNER_DEST.
1840
1841              Also, if this insn sets a function argument, combining it with
1842              something that might need a spill could clobber a previous
1843              function argument; the all_adjacent test in can_combine_p also
1844              checks this; here, we do a more specific test for this case.  */
1845
1846           || (REG_P (inner_dest)
1847               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1848               && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1849                                         GET_MODE (inner_dest))))
1850           || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1851         return 0;
1852
1853       /* If DEST is used in I3, it is being killed in this insn, so
1854          record that for later.  We have to consider paradoxical
1855          subregs here, since they kill the whole register, but we
1856          ignore partial subregs, STRICT_LOW_PART, etc.
1857          Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1858          STACK_POINTER_REGNUM, since these are always considered to be
1859          live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
1860       subdest = dest;
1861       if (GET_CODE (subdest) == SUBREG
1862           && (GET_MODE_SIZE (GET_MODE (subdest))
1863               >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
1864         subdest = SUBREG_REG (subdest);
1865       if (pi3dest_killed
1866           && REG_P (subdest)
1867           && reg_referenced_p (subdest, PATTERN (i3))
1868           && REGNO (subdest) != FRAME_POINTER_REGNUM
1869 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1870           && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
1871 #endif
1872 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1873           && (REGNO (subdest) != ARG_POINTER_REGNUM
1874               || ! fixed_regs [REGNO (subdest)])
1875 #endif
1876           && REGNO (subdest) != STACK_POINTER_REGNUM)
1877         {
1878           if (*pi3dest_killed)
1879             return 0;
1880
1881           *pi3dest_killed = subdest;
1882         }
1883     }
1884
1885   else if (GET_CODE (x) == PARALLEL)
1886     {
1887       int i;
1888
1889       for (i = 0; i < XVECLEN (x, 0); i++)
1890         if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1891                                 i1_not_in_src, pi3dest_killed))
1892           return 0;
1893     }
1894
1895   return 1;
1896 }
1897 \f
1898 /* Return 1 if X is an arithmetic expression that contains a multiplication
1899    and division.  We don't count multiplications by powers of two here.  */
1900
1901 static int
1902 contains_muldiv (rtx x)
1903 {
1904   switch (GET_CODE (x))
1905     {
1906     case MOD:  case DIV:  case UMOD:  case UDIV:
1907       return 1;
1908
1909     case MULT:
1910       return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1911                 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1912     default:
1913       if (BINARY_P (x))
1914         return contains_muldiv (XEXP (x, 0))
1915             || contains_muldiv (XEXP (x, 1));
1916
1917       if (UNARY_P (x))
1918         return contains_muldiv (XEXP (x, 0));
1919
1920       return 0;
1921     }
1922 }
1923 \f
1924 /* Determine whether INSN can be used in a combination.  Return nonzero if
1925    not.  This is used in try_combine to detect early some cases where we
1926    can't perform combinations.  */
1927
1928 static int
1929 cant_combine_insn_p (rtx insn)
1930 {
1931   rtx set;
1932   rtx src, dest;
1933
1934   /* If this isn't really an insn, we can't do anything.
1935      This can occur when flow deletes an insn that it has merged into an
1936      auto-increment address.  */
1937   if (! INSN_P (insn))
1938     return 1;
1939
1940   /* Never combine loads and stores involving hard regs that are likely
1941      to be spilled.  The register allocator can usually handle such
1942      reg-reg moves by tying.  If we allow the combiner to make
1943      substitutions of likely-spilled regs, reload might die.
1944      As an exception, we allow combinations involving fixed regs; these are
1945      not available to the register allocator so there's no risk involved.  */
1946
1947   set = single_set (insn);
1948   if (! set)
1949     return 0;
1950   src = SET_SRC (set);
1951   dest = SET_DEST (set);
1952   if (GET_CODE (src) == SUBREG)
1953     src = SUBREG_REG (src);
1954   if (GET_CODE (dest) == SUBREG)
1955     dest = SUBREG_REG (dest);
1956   if (REG_P (src) && REG_P (dest)
1957       && ((REGNO (src) < FIRST_PSEUDO_REGISTER
1958            && ! fixed_regs[REGNO (src)]
1959            && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
1960           || (REGNO (dest) < FIRST_PSEUDO_REGISTER
1961               && ! fixed_regs[REGNO (dest)]
1962               && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
1963     return 1;
1964
1965   return 0;
1966 }
1967
1968 struct likely_spilled_retval_info
1969 {
1970   unsigned regno, nregs;
1971   unsigned mask;
1972 };
1973
1974 /* Called via note_stores by likely_spilled_retval_p.  Remove from info->mask
1975    hard registers that are known to be written to / clobbered in full.  */
1976 static void
1977 likely_spilled_retval_1 (rtx x, rtx set, void *data)
1978 {
1979   struct likely_spilled_retval_info *info = data;
1980   unsigned regno, nregs;
1981   unsigned new_mask;
1982
1983   if (!REG_P (XEXP (set, 0)))
1984     return;
1985   regno = REGNO (x);
1986   if (regno >= info->regno + info->nregs)
1987     return;
1988   nregs = hard_regno_nregs[regno][GET_MODE (x)];
1989   if (regno + nregs <= info->regno)
1990     return;
1991   new_mask = (2U << (nregs - 1)) - 1;
1992   if (regno < info->regno)
1993     new_mask >>= info->regno - regno;
1994   else
1995     new_mask <<= regno - info->regno;
1996   info->mask &= ~new_mask;
1997 }
1998
1999 /* Return nonzero iff part of the return value is live during INSN, and
2000    it is likely spilled.  This can happen when more than one insn is needed
2001    to copy the return value, e.g. when we consider to combine into the
2002    second copy insn for a complex value.  */
2003
2004 static int
2005 likely_spilled_retval_p (rtx insn)
2006 {
2007   rtx use = BB_END (this_basic_block);
2008   rtx reg, p;
2009   unsigned regno, nregs;
2010   /* We assume here that no machine mode needs more than
2011      32 hard registers when the value overlaps with a register
2012      for which FUNCTION_VALUE_REGNO_P is true.  */
2013   unsigned mask;
2014   struct likely_spilled_retval_info info;
2015
2016   if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2017     return 0;
2018   reg = XEXP (PATTERN (use), 0);
2019   if (!REG_P (reg) || !FUNCTION_VALUE_REGNO_P (REGNO (reg)))
2020     return 0;
2021   regno = REGNO (reg);
2022   nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2023   if (nregs == 1)
2024     return 0;
2025   mask = (2U << (nregs - 1)) - 1;
2026
2027   /* Disregard parts of the return value that are set later.  */
2028   info.regno = regno;
2029   info.nregs = nregs;
2030   info.mask = mask;
2031   for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2032     if (INSN_P (p))
2033       note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2034   mask = info.mask;
2035
2036   /* Check if any of the (probably) live return value registers is
2037      likely spilled.  */
2038   nregs --;
2039   do
2040     {
2041       if ((mask & 1 << nregs)
2042           && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (regno + nregs)))
2043         return 1;
2044     } while (nregs--);
2045   return 0;
2046 }
2047
2048 /* Adjust INSN after we made a change to its destination.
2049
2050    Changing the destination can invalidate notes that say something about
2051    the results of the insn and a LOG_LINK pointing to the insn.  */
2052
2053 static void
2054 adjust_for_new_dest (rtx insn)
2055 {
2056   /* For notes, be conservative and simply remove them.  */
2057   remove_reg_equal_equiv_notes (insn);
2058
2059   /* The new insn will have a destination that was previously the destination
2060      of an insn just above it.  Call distribute_links to make a LOG_LINK from
2061      the next use of that destination.  */
2062   distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
2063
2064   df_insn_rescan (insn);
2065 }
2066
2067 /* Return TRUE if combine can reuse reg X in mode MODE.
2068    ADDED_SETS is nonzero if the original set is still required.  */
2069 static bool
2070 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
2071 {
2072   unsigned int regno;
2073
2074   if (!REG_P(x))
2075     return false;
2076
2077   regno = REGNO (x);
2078   /* Allow hard registers if the new mode is legal, and occupies no more
2079      registers than the old mode.  */
2080   if (regno < FIRST_PSEUDO_REGISTER)
2081     return (HARD_REGNO_MODE_OK (regno, mode)
2082             && (hard_regno_nregs[regno][GET_MODE (x)]
2083                 >= hard_regno_nregs[regno][mode]));
2084
2085   /* Or a pseudo that is only used once.  */
2086   return (REG_N_SETS (regno) == 1 && !added_sets
2087           && !REG_USERVAR_P (x));
2088 }
2089
2090
2091 /* Check whether X, the destination of a set, refers to part of
2092    the register specified by REG.  */
2093
2094 static bool
2095 reg_subword_p (rtx x, rtx reg)
2096 {
2097   /* Check that reg is an integer mode register.  */
2098   if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2099     return false;
2100
2101   if (GET_CODE (x) == STRICT_LOW_PART
2102       || GET_CODE (x) == ZERO_EXTRACT)
2103     x = XEXP (x, 0);
2104
2105   return GET_CODE (x) == SUBREG
2106          && SUBREG_REG (x) == reg
2107          && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2108 }
2109
2110
2111 /* Try to combine the insns I1 and I2 into I3.
2112    Here I1 and I2 appear earlier than I3.
2113    I1 can be zero; then we combine just I2 into I3.
2114
2115    If we are combining three insns and the resulting insn is not recognized,
2116    try splitting it into two insns.  If that happens, I2 and I3 are retained
2117    and I1 is pseudo-deleted by turning it into a NOTE.  Otherwise, I1 and I2
2118    are pseudo-deleted.
2119
2120    Return 0 if the combination does not work.  Then nothing is changed.
2121    If we did the combination, return the insn at which combine should
2122    resume scanning.
2123
2124    Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2125    new direct jump instruction.  */
2126
2127 static rtx
2128 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
2129 {
2130   /* New patterns for I3 and I2, respectively.  */
2131   rtx newpat, newi2pat = 0;
2132   rtvec newpat_vec_with_clobbers = 0;
2133   int substed_i2 = 0, substed_i1 = 0;
2134   /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead.  */
2135   int added_sets_1, added_sets_2;
2136   /* Total number of SETs to put into I3.  */
2137   int total_sets;
2138   /* Nonzero if I2's body now appears in I3.  */
2139   int i2_is_used;
2140   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
2141   int insn_code_number, i2_code_number = 0, other_code_number = 0;
2142   /* Contains I3 if the destination of I3 is used in its source, which means
2143      that the old life of I3 is being killed.  If that usage is placed into
2144      I2 and not in I3, a REG_DEAD note must be made.  */
2145   rtx i3dest_killed = 0;
2146   /* SET_DEST and SET_SRC of I2 and I1.  */
2147   rtx i2dest, i2src, i1dest = 0, i1src = 0;
2148   /* PATTERN (I1) and PATTERN (I2), or a copy of it in certain cases.  */
2149   rtx i1pat = 0, i2pat = 0;
2150   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
2151   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2152   int i2dest_killed = 0, i1dest_killed = 0;
2153   int i1_feeds_i3 = 0;
2154   /* Notes that must be added to REG_NOTES in I3 and I2.  */
2155   rtx new_i3_notes, new_i2_notes;
2156   /* Notes that we substituted I3 into I2 instead of the normal case.  */
2157   int i3_subst_into_i2 = 0;
2158   /* Notes that I1, I2 or I3 is a MULT operation.  */
2159   int have_mult = 0;
2160   int swap_i2i3 = 0;
2161
2162   int maxreg;
2163   rtx temp;
2164   rtx link;
2165   rtx other_pat = 0;
2166   rtx new_other_notes;
2167   int i;
2168
2169   /* Exit early if one of the insns involved can't be used for
2170      combinations.  */
2171   if (cant_combine_insn_p (i3)
2172       || cant_combine_insn_p (i2)
2173       || (i1 && cant_combine_insn_p (i1))
2174       || likely_spilled_retval_p (i3)
2175       /* We also can't do anything if I3 has a
2176          REG_LIBCALL note since we don't want to disrupt the contiguity of a
2177          libcall.  */
2178 #if 0
2179       /* ??? This gives worse code, and appears to be unnecessary, since no
2180          pass after flow uses REG_LIBCALL/REG_RETVAL notes.  */
2181       || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
2182 #endif
2183       )
2184     return 0;
2185
2186   combine_attempts++;
2187   undobuf.other_insn = 0;
2188
2189   /* Reset the hard register usage information.  */
2190   CLEAR_HARD_REG_SET (newpat_used_regs);
2191
2192   /* If I1 and I2 both feed I3, they can be in any order.  To simplify the
2193      code below, set I1 to be the earlier of the two insns.  */
2194   if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2195     temp = i1, i1 = i2, i2 = temp;
2196
2197   added_links_insn = 0;
2198
2199   /* First check for one important special-case that the code below will
2200      not handle.  Namely, the case where I1 is zero, I2 is a PARALLEL
2201      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
2202      we may be able to replace that destination with the destination of I3.
2203      This occurs in the common code where we compute both a quotient and
2204      remainder into a structure, in which case we want to do the computation
2205      directly into the structure to avoid register-register copies.
2206
2207      Note that this case handles both multiple sets in I2 and also
2208      cases where I2 has a number of CLOBBER or PARALLELs.
2209
2210      We make very conservative checks below and only try to handle the
2211      most common cases of this.  For example, we only handle the case
2212      where I2 and I3 are adjacent to avoid making difficult register
2213      usage tests.  */
2214
2215   if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2216       && REG_P (SET_SRC (PATTERN (i3)))
2217       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2218       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2219       && GET_CODE (PATTERN (i2)) == PARALLEL
2220       && ! side_effects_p (SET_DEST (PATTERN (i3)))
2221       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2222          below would need to check what is inside (and reg_overlap_mentioned_p
2223          doesn't support those codes anyway).  Don't allow those destinations;
2224          the resulting insn isn't likely to be recognized anyway.  */
2225       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2226       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2227       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2228                                     SET_DEST (PATTERN (i3)))
2229       && next_real_insn (i2) == i3)
2230     {
2231       rtx p2 = PATTERN (i2);
2232
2233       /* Make sure that the destination of I3,
2234          which we are going to substitute into one output of I2,
2235          is not used within another output of I2.  We must avoid making this:
2236          (parallel [(set (mem (reg 69)) ...)
2237                     (set (reg 69) ...)])
2238          which is not well-defined as to order of actions.
2239          (Besides, reload can't handle output reloads for this.)
2240
2241          The problem can also happen if the dest of I3 is a memory ref,
2242          if another dest in I2 is an indirect memory ref.  */
2243       for (i = 0; i < XVECLEN (p2, 0); i++)
2244         if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2245              || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2246             && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2247                                         SET_DEST (XVECEXP (p2, 0, i))))
2248           break;
2249
2250       if (i == XVECLEN (p2, 0))
2251         for (i = 0; i < XVECLEN (p2, 0); i++)
2252           if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2253                || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2254               && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2255             {
2256               combine_merges++;
2257
2258               subst_insn = i3;
2259               subst_low_luid = DF_INSN_LUID (i2);
2260
2261               added_sets_2 = added_sets_1 = 0;
2262               i2dest = SET_SRC (PATTERN (i3));
2263               i2dest_killed = dead_or_set_p (i2, i2dest);
2264
2265               /* Replace the dest in I2 with our dest and make the resulting
2266                  insn the new pattern for I3.  Then skip to where we
2267                  validate the pattern.  Everything was set up above.  */
2268               SUBST (SET_DEST (XVECEXP (p2, 0, i)),
2269                      SET_DEST (PATTERN (i3)));
2270
2271               newpat = p2;
2272               i3_subst_into_i2 = 1;
2273               goto validate_replacement;
2274             }
2275     }
2276
2277   /* If I2 is setting a pseudo to a constant and I3 is setting some
2278      sub-part of it to another constant, merge them by making a new
2279      constant.  */
2280   if (i1 == 0
2281       && (temp = single_set (i2)) != 0
2282       && (GET_CODE (SET_SRC (temp)) == CONST_INT
2283           || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
2284       && GET_CODE (PATTERN (i3)) == SET
2285       && (GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT
2286           || GET_CODE (SET_SRC (PATTERN (i3))) == CONST_DOUBLE)
2287       && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp)))
2288     {
2289       rtx dest = SET_DEST (PATTERN (i3));
2290       int offset = -1;
2291       int width = 0;
2292
2293       if (GET_CODE (dest) == ZERO_EXTRACT)
2294         {
2295           if (GET_CODE (XEXP (dest, 1)) == CONST_INT
2296               && GET_CODE (XEXP (dest, 2)) == CONST_INT)
2297             {
2298               width = INTVAL (XEXP (dest, 1));
2299               offset = INTVAL (XEXP (dest, 2));
2300               dest = XEXP (dest, 0);
2301               if (BITS_BIG_ENDIAN)
2302                 offset = GET_MODE_BITSIZE (GET_MODE (dest)) - width - offset;
2303             }
2304         }
2305       else
2306         {
2307           if (GET_CODE (dest) == STRICT_LOW_PART)
2308             dest = XEXP (dest, 0);
2309           width = GET_MODE_BITSIZE (GET_MODE (dest));
2310           offset = 0;
2311         }
2312
2313       if (offset >= 0)
2314         {
2315           /* If this is the low part, we're done.  */
2316           if (subreg_lowpart_p (dest))
2317             ;
2318           /* Handle the case where inner is twice the size of outer.  */
2319           else if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2320                    == 2 * GET_MODE_BITSIZE (GET_MODE (dest)))
2321             offset += GET_MODE_BITSIZE (GET_MODE (dest));
2322           /* Otherwise give up for now.  */
2323           else
2324             offset = -1;
2325         }
2326
2327       if (offset >= 0
2328           && (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2329               <= HOST_BITS_PER_WIDE_INT * 2))
2330         {
2331           HOST_WIDE_INT mhi, ohi, ihi;
2332           HOST_WIDE_INT mlo, olo, ilo;
2333           rtx inner = SET_SRC (PATTERN (i3));
2334           rtx outer = SET_SRC (temp);
2335
2336           if (GET_CODE (outer) == CONST_INT)
2337             {
2338               olo = INTVAL (outer);
2339               ohi = olo < 0 ? -1 : 0;
2340             }
2341           else
2342             {
2343               olo = CONST_DOUBLE_LOW (outer);
2344               ohi = CONST_DOUBLE_HIGH (outer);
2345             }
2346
2347           if (GET_CODE (inner) == CONST_INT)
2348             {
2349               ilo = INTVAL (inner);
2350               ihi = ilo < 0 ? -1 : 0;
2351             }
2352           else
2353             {
2354               ilo = CONST_DOUBLE_LOW (inner);
2355               ihi = CONST_DOUBLE_HIGH (inner);
2356             }
2357
2358           if (width < HOST_BITS_PER_WIDE_INT)
2359             {
2360               mlo = ((unsigned HOST_WIDE_INT) 1 << width) - 1;
2361               mhi = 0;
2362             }
2363           else if (width < HOST_BITS_PER_WIDE_INT * 2)
2364             {
2365               mhi = ((unsigned HOST_WIDE_INT) 1
2366                      << (width - HOST_BITS_PER_WIDE_INT)) - 1;
2367               mlo = -1;
2368             }
2369           else
2370             {
2371               mlo = -1;
2372               mhi = -1;
2373             }
2374
2375           ilo &= mlo;
2376           ihi &= mhi;
2377
2378           if (offset >= HOST_BITS_PER_WIDE_INT)
2379             {
2380               mhi = mlo << (offset - HOST_BITS_PER_WIDE_INT);
2381               mlo = 0;
2382               ihi = ilo << (offset - HOST_BITS_PER_WIDE_INT);
2383               ilo = 0;
2384             }
2385           else if (offset > 0)
2386             {
2387               mhi = (mhi << offset) | ((unsigned HOST_WIDE_INT) mlo
2388                                        >> (HOST_BITS_PER_WIDE_INT - offset));
2389               mlo = mlo << offset;
2390               ihi = (ihi << offset) | ((unsigned HOST_WIDE_INT) ilo
2391                                        >> (HOST_BITS_PER_WIDE_INT - offset));
2392               ilo = ilo << offset;
2393             }
2394
2395           olo = (olo & ~mlo) | ilo;
2396           ohi = (ohi & ~mhi) | ihi;
2397
2398           combine_merges++;
2399           subst_insn = i3;
2400           subst_low_luid = DF_INSN_LUID (i2);
2401           added_sets_2 = added_sets_1 = 0;
2402           i2dest = SET_DEST (temp);
2403           i2dest_killed = dead_or_set_p (i2, i2dest);
2404
2405           SUBST (SET_SRC (temp),
2406                  immed_double_const (olo, ohi, GET_MODE (SET_DEST (temp))));
2407
2408           newpat = PATTERN (i2);
2409           goto validate_replacement;
2410         }
2411     }
2412
2413 #ifndef HAVE_cc0
2414   /* If we have no I1 and I2 looks like:
2415         (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2416                    (set Y OP)])
2417      make up a dummy I1 that is
2418         (set Y OP)
2419      and change I2 to be
2420         (set (reg:CC X) (compare:CC Y (const_int 0)))
2421
2422      (We can ignore any trailing CLOBBERs.)
2423
2424      This undoes a previous combination and allows us to match a branch-and-
2425      decrement insn.  */
2426
2427   if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2428       && XVECLEN (PATTERN (i2), 0) >= 2
2429       && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2430       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2431           == MODE_CC)
2432       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2433       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2434       && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2435       && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2436       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2437                       SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2438     {
2439       for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2440         if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2441           break;
2442
2443       if (i == 1)
2444         {
2445           /* We make I1 with the same INSN_UID as I2.  This gives it
2446              the same DF_INSN_LUID for value tracking.  Our fake I1 will
2447              never appear in the insn stream so giving it the same INSN_UID
2448              as I2 will not cause a problem.  */
2449
2450           i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
2451                              BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
2452                              XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX);
2453
2454           SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2455           SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2456                  SET_DEST (PATTERN (i1)));
2457         }
2458     }
2459 #endif
2460
2461   /* Verify that I2 and I1 are valid for combining.  */
2462   if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
2463       || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
2464     {
2465       undo_all ();
2466       return 0;
2467     }
2468
2469   /* Record whether I2DEST is used in I2SRC and similarly for the other
2470      cases.  Knowing this will help in register status updating below.  */
2471   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2472   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2473   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2474   i2dest_killed = dead_or_set_p (i2, i2dest);
2475   i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2476
2477   /* See if I1 directly feeds into I3.  It does if I1DEST is not used
2478      in I2SRC.  */
2479   i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
2480
2481   /* Ensure that I3's pattern can be the destination of combines.  */
2482   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
2483                           i1 && i2dest_in_i1src && i1_feeds_i3,
2484                           &i3dest_killed))
2485     {
2486       undo_all ();
2487       return 0;
2488     }
2489
2490   /* See if any of the insns is a MULT operation.  Unless one is, we will
2491      reject a combination that is, since it must be slower.  Be conservative
2492      here.  */
2493   if (GET_CODE (i2src) == MULT
2494       || (i1 != 0 && GET_CODE (i1src) == MULT)
2495       || (GET_CODE (PATTERN (i3)) == SET
2496           && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2497     have_mult = 1;
2498
2499   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2500      We used to do this EXCEPT in one case: I3 has a post-inc in an
2501      output operand.  However, that exception can give rise to insns like
2502         mov r3,(r3)+
2503      which is a famous insn on the PDP-11 where the value of r3 used as the
2504      source was model-dependent.  Avoid this sort of thing.  */
2505
2506 #if 0
2507   if (!(GET_CODE (PATTERN (i3)) == SET
2508         && REG_P (SET_SRC (PATTERN (i3)))
2509         && MEM_P (SET_DEST (PATTERN (i3)))
2510         && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2511             || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2512     /* It's not the exception.  */
2513 #endif
2514 #ifdef AUTO_INC_DEC
2515     for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2516       if (REG_NOTE_KIND (link) == REG_INC
2517           && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2518               || (i1 != 0
2519                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2520         {
2521           undo_all ();
2522           return 0;
2523         }
2524 #endif
2525
2526   /* See if the SETs in I1 or I2 need to be kept around in the merged
2527      instruction: whenever the value set there is still needed past I3.
2528      For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
2529
2530      For the SET in I1, we have two cases:  If I1 and I2 independently
2531      feed into I3, the set in I1 needs to be kept around if I1DEST dies
2532      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
2533      in I1 needs to be kept around unless I1DEST dies or is set in either
2534      I2 or I3.  We can distinguish these cases by seeing if I2SRC mentions
2535      I1DEST.  If so, we know I1 feeds into I2.  */
2536
2537   added_sets_2 = ! dead_or_set_p (i3, i2dest);
2538
2539   added_sets_1
2540     = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
2541                : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
2542
2543   /* If the set in I2 needs to be kept around, we must make a copy of
2544      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2545      PATTERN (I2), we are only substituting for the original I1DEST, not into
2546      an already-substituted copy.  This also prevents making self-referential
2547      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2548      I2DEST.  */
2549
2550   if (added_sets_2)
2551     {
2552       if (GET_CODE (PATTERN (i2)) == PARALLEL)
2553         i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
2554       else
2555         i2pat = copy_rtx (PATTERN (i2));
2556     }
2557
2558   if (added_sets_1)
2559     {
2560       if (GET_CODE (PATTERN (i1)) == PARALLEL)
2561         i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
2562       else
2563         i1pat = copy_rtx (PATTERN (i1));
2564     }
2565
2566   combine_merges++;
2567
2568   /* Substitute in the latest insn for the regs set by the earlier ones.  */
2569
2570   maxreg = max_reg_num ();
2571
2572   subst_insn = i3;
2573
2574 #ifndef HAVE_cc0
2575   /* Many machines that don't use CC0 have insns that can both perform an
2576      arithmetic operation and set the condition code.  These operations will
2577      be represented as a PARALLEL with the first element of the vector
2578      being a COMPARE of an arithmetic operation with the constant zero.
2579      The second element of the vector will set some pseudo to the result
2580      of the same arithmetic operation.  If we simplify the COMPARE, we won't
2581      match such a pattern and so will generate an extra insn.   Here we test
2582      for this case, where both the comparison and the operation result are
2583      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2584      I2SRC.  Later we will make the PARALLEL that contains I2.  */
2585
2586   if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2587       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2588       && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
2589       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2590     {
2591 #ifdef SELECT_CC_MODE
2592       rtx *cc_use;
2593       enum machine_mode compare_mode;
2594 #endif
2595
2596       newpat = PATTERN (i3);
2597       SUBST (XEXP (SET_SRC (newpat), 0), i2src);
2598
2599       i2_is_used = 1;
2600
2601 #ifdef SELECT_CC_MODE
2602       /* See if a COMPARE with the operand we substituted in should be done
2603          with the mode that is currently being used.  If not, do the same
2604          processing we do in `subst' for a SET; namely, if the destination
2605          is used only once, try to replace it with a register of the proper
2606          mode and also replace the COMPARE.  */
2607       if (undobuf.other_insn == 0
2608           && (cc_use = find_single_use (SET_DEST (newpat), i3,
2609                                         &undobuf.other_insn))
2610           && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
2611                                               i2src, const0_rtx))
2612               != GET_MODE (SET_DEST (newpat))))
2613         {
2614           if (can_change_dest_mode(SET_DEST (newpat), added_sets_2,
2615                                    compare_mode))
2616             {
2617               unsigned int regno = REGNO (SET_DEST (newpat));
2618               rtx new_dest;
2619
2620               if (regno < FIRST_PSEUDO_REGISTER)
2621                 new_dest = gen_rtx_REG (compare_mode, regno);
2622               else
2623                 {
2624                   SUBST_MODE (regno_reg_rtx[regno], compare_mode);
2625                   new_dest = regno_reg_rtx[regno];
2626                 }
2627
2628               SUBST (SET_DEST (newpat), new_dest);
2629               SUBST (XEXP (*cc_use, 0), new_dest);
2630               SUBST (SET_SRC (newpat),
2631                      gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
2632             }
2633           else
2634             undobuf.other_insn = 0;
2635         }
2636 #endif
2637     }
2638   else
2639 #endif
2640     {
2641       /* It is possible that the source of I2 or I1 may be performing
2642          an unneeded operation, such as a ZERO_EXTEND of something
2643          that is known to have the high part zero.  Handle that case
2644          by letting subst look at the innermost one of them.
2645
2646          Another way to do this would be to have a function that tries
2647          to simplify a single insn instead of merging two or more
2648          insns.  We don't do this because of the potential of infinite
2649          loops and because of the potential extra memory required.
2650          However, doing it the way we are is a bit of a kludge and
2651          doesn't catch all cases.
2652
2653          But only do this if -fexpensive-optimizations since it slows
2654          things down and doesn't usually win.
2655
2656          This is not done in the COMPARE case above because the
2657          unmodified I2PAT is used in the PARALLEL and so a pattern
2658          with a modified I2SRC would not match.  */
2659
2660       if (flag_expensive_optimizations)
2661         {
2662           /* Pass pc_rtx so no substitutions are done, just
2663              simplifications.  */
2664           if (i1)
2665             {
2666               subst_low_luid = DF_INSN_LUID (i1);
2667               i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
2668             }
2669           else
2670             {
2671               subst_low_luid = DF_INSN_LUID (i2);
2672               i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
2673             }
2674         }
2675
2676       n_occurrences = 0;                /* `subst' counts here */
2677
2678       /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
2679          need to make a unique copy of I2SRC each time we substitute it
2680          to avoid self-referential rtl.  */
2681
2682       subst_low_luid = DF_INSN_LUID (i2);
2683       newpat = subst (PATTERN (i3), i2dest, i2src, 0,
2684                       ! i1_feeds_i3 && i1dest_in_i1src);
2685       substed_i2 = 1;
2686
2687       /* Record whether i2's body now appears within i3's body.  */
2688       i2_is_used = n_occurrences;
2689     }
2690
2691   /* If we already got a failure, don't try to do more.  Otherwise,
2692      try to substitute in I1 if we have it.  */
2693
2694   if (i1 && GET_CODE (newpat) != CLOBBER)
2695     {
2696       /* Before we can do this substitution, we must redo the test done
2697          above (see detailed comments there) that ensures  that I1DEST
2698          isn't mentioned in any SETs in NEWPAT that are field assignments.  */
2699
2700       if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
2701                               0, (rtx*) 0))
2702         {
2703           undo_all ();
2704           return 0;
2705         }
2706
2707       n_occurrences = 0;
2708       subst_low_luid = DF_INSN_LUID (i1);
2709       newpat = subst (newpat, i1dest, i1src, 0, 0);
2710       substed_i1 = 1;
2711     }
2712
2713   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
2714      to count all the ways that I2SRC and I1SRC can be used.  */
2715   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
2716        && i2_is_used + added_sets_2 > 1)
2717       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2718           && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
2719               > 1))
2720       /* Fail if we tried to make a new register.  */
2721       || max_reg_num () != maxreg
2722       /* Fail if we couldn't do something and have a CLOBBER.  */
2723       || GET_CODE (newpat) == CLOBBER
2724       /* Fail if this new pattern is a MULT and we didn't have one before
2725          at the outer level.  */
2726       || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
2727           && ! have_mult))
2728     {
2729       undo_all ();
2730       return 0;
2731     }
2732
2733   /* If the actions of the earlier insns must be kept
2734      in addition to substituting them into the latest one,
2735      we must make a new PARALLEL for the latest insn
2736      to hold additional the SETs.  */
2737
2738   if (added_sets_1 || added_sets_2)
2739     {
2740       combine_extras++;
2741
2742       if (GET_CODE (newpat) == PARALLEL)
2743         {
2744           rtvec old = XVEC (newpat, 0);
2745           total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
2746           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2747           memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
2748                   sizeof (old->elem[0]) * old->num_elem);
2749         }
2750       else
2751         {
2752           rtx old = newpat;
2753           total_sets = 1 + added_sets_1 + added_sets_2;
2754           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2755           XVECEXP (newpat, 0, 0) = old;
2756         }
2757
2758       if (added_sets_1)
2759         XVECEXP (newpat, 0, --total_sets) = i1pat;
2760
2761       if (added_sets_2)
2762         {
2763           /* If there is no I1, use I2's body as is.  We used to also not do
2764              the subst call below if I2 was substituted into I3,
2765              but that could lose a simplification.  */
2766           if (i1 == 0)
2767             XVECEXP (newpat, 0, --total_sets) = i2pat;
2768           else
2769             /* See comment where i2pat is assigned.  */
2770             XVECEXP (newpat, 0, --total_sets)
2771               = subst (i2pat, i1dest, i1src, 0, 0);
2772         }
2773     }
2774
2775   /* We come here when we are replacing a destination in I2 with the
2776      destination of I3.  */
2777  validate_replacement:
2778
2779   /* Note which hard regs this insn has as inputs.  */
2780   mark_used_regs_combine (newpat);
2781
2782   /* If recog_for_combine fails, it strips existing clobbers.  If we'll
2783      consider splitting this pattern, we might need these clobbers.  */
2784   if (i1 && GET_CODE (newpat) == PARALLEL
2785       && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
2786     {
2787       int len = XVECLEN (newpat, 0);
2788
2789       newpat_vec_with_clobbers = rtvec_alloc (len);
2790       for (i = 0; i < len; i++)
2791         RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
2792     }
2793
2794   /* Is the result of combination a valid instruction?  */
2795   insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2796
2797   /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2798      the second SET's destination is a register that is unused and isn't
2799      marked as an instruction that might trap in an EH region.  In that case,
2800      we just need the first SET.   This can occur when simplifying a divmod
2801      insn.  We *must* test for this case here because the code below that
2802      splits two independent SETs doesn't handle this case correctly when it
2803      updates the register status.
2804
2805      It's pointless doing this if we originally had two sets, one from
2806      i3, and one from i2.  Combining then splitting the parallel results
2807      in the original i2 again plus an invalid insn (which we delete).
2808      The net effect is only to move instructions around, which makes
2809      debug info less accurate.
2810
2811      Also check the case where the first SET's destination is unused.
2812      That would not cause incorrect code, but does cause an unneeded
2813      insn to remain.  */
2814
2815   if (insn_code_number < 0
2816       && !(added_sets_2 && i1 == 0)
2817       && GET_CODE (newpat) == PARALLEL
2818       && XVECLEN (newpat, 0) == 2
2819       && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2820       && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2821       && asm_noperands (newpat) < 0)
2822     {
2823       rtx set0 = XVECEXP (newpat, 0, 0);
2824       rtx set1 = XVECEXP (newpat, 0, 1);
2825       rtx note;
2826
2827       if (((REG_P (SET_DEST (set1))
2828             && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
2829            || (GET_CODE (SET_DEST (set1)) == SUBREG
2830                && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
2831           && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2832               || INTVAL (XEXP (note, 0)) <= 0)
2833           && ! side_effects_p (SET_SRC (set1)))
2834         {
2835           newpat = set0;
2836           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2837         }
2838
2839       else if (((REG_P (SET_DEST (set0))
2840                  && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
2841                 || (GET_CODE (SET_DEST (set0)) == SUBREG
2842                     && find_reg_note (i3, REG_UNUSED,
2843                                       SUBREG_REG (SET_DEST (set0)))))
2844                && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2845                    || INTVAL (XEXP (note, 0)) <= 0)
2846                && ! side_effects_p (SET_SRC (set0)))
2847         {
2848           newpat = set1;
2849           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2850
2851           if (insn_code_number >= 0)
2852             {
2853               /* If we will be able to accept this, we have made a
2854                  change to the destination of I3.  This requires us to
2855                  do a few adjustments.  */
2856
2857               PATTERN (i3) = newpat;
2858               adjust_for_new_dest (i3);
2859             }
2860         }
2861     }
2862
2863   /* If we were combining three insns and the result is a simple SET
2864      with no ASM_OPERANDS that wasn't recognized, try to split it into two
2865      insns.  There are two ways to do this.  It can be split using a
2866      machine-specific method (like when you have an addition of a large
2867      constant) or by combine in the function find_split_point.  */
2868
2869   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2870       && asm_noperands (newpat) < 0)
2871     {
2872       rtx m_split, *split;
2873
2874       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
2875          use I2DEST as a scratch register will help.  In the latter case,
2876          convert I2DEST to the mode of the source of NEWPAT if we can.  */
2877
2878       m_split = split_insns (newpat, i3);
2879
2880       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2881          inputs of NEWPAT.  */
2882
2883       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2884          possible to try that as a scratch reg.  This would require adding
2885          more code to make it work though.  */
2886
2887       if (m_split == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
2888         {
2889           enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
2890
2891           /* First try to split using the original register as a
2892              scratch register.  */
2893           m_split = split_insns (gen_rtx_PARALLEL
2894                                  (VOIDmode,
2895                                   gen_rtvec (2, newpat,
2896                                              gen_rtx_CLOBBER (VOIDmode,
2897                                                               i2dest))),
2898                                  i3);
2899
2900           /* If that didn't work, try changing the mode of I2DEST if
2901              we can.  */
2902           if (m_split == 0
2903               && new_mode != GET_MODE (i2dest)
2904               && new_mode != VOIDmode
2905               && can_change_dest_mode (i2dest, added_sets_2, new_mode))
2906             {
2907               enum machine_mode old_mode = GET_MODE (i2dest);
2908               rtx ni2dest;
2909
2910               if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
2911                 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
2912               else
2913                 {
2914                   SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
2915                   ni2dest = regno_reg_rtx[REGNO (i2dest)];
2916                 }
2917
2918               m_split = split_insns (gen_rtx_PARALLEL
2919                                      (VOIDmode,
2920                                       gen_rtvec (2, newpat,
2921                                                  gen_rtx_CLOBBER (VOIDmode,
2922                                                                   ni2dest))),
2923                                      i3);
2924
2925               if (m_split == 0
2926                   && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2927                 {
2928                   struct undo *buf;
2929
2930                   PUT_MODE (regno_reg_rtx[REGNO (i2dest)], old_mode);
2931                   buf = undobuf.undos;
2932                   undobuf.undos = buf->next;
2933                   buf->next = undobuf.frees;
2934                   undobuf.frees = buf;
2935                 }
2936             }
2937         }
2938
2939       /* If recog_for_combine has discarded clobbers, try to use them
2940          again for the split.  */
2941       if (m_split == 0 && newpat_vec_with_clobbers)
2942         m_split
2943           = split_insns (gen_rtx_PARALLEL (VOIDmode,
2944                                            newpat_vec_with_clobbers), i3);
2945
2946       if (m_split && NEXT_INSN (m_split) == NULL_RTX)
2947         {
2948           m_split = PATTERN (m_split);
2949           insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
2950           if (insn_code_number >= 0)
2951             newpat = m_split;
2952         }
2953       else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
2954                && (next_real_insn (i2) == i3
2955                    || ! use_crosses_set_p (PATTERN (m_split), DF_INSN_LUID (i2))))
2956         {
2957           rtx i2set, i3set;
2958           rtx newi3pat = PATTERN (NEXT_INSN (m_split));
2959           newi2pat = PATTERN (m_split);
2960
2961           i3set = single_set (NEXT_INSN (m_split));
2962           i2set = single_set (m_split);
2963
2964           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2965
2966           /* If I2 or I3 has multiple SETs, we won't know how to track
2967              register status, so don't use these insns.  If I2's destination
2968              is used between I2 and I3, we also can't use these insns.  */
2969
2970           if (i2_code_number >= 0 && i2set && i3set
2971               && (next_real_insn (i2) == i3
2972                   || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
2973             insn_code_number = recog_for_combine (&newi3pat, i3,
2974                                                   &new_i3_notes);
2975           if (insn_code_number >= 0)
2976             newpat = newi3pat;
2977
2978           /* It is possible that both insns now set the destination of I3.
2979              If so, we must show an extra use of it.  */
2980
2981           if (insn_code_number >= 0)
2982             {
2983               rtx new_i3_dest = SET_DEST (i3set);
2984               rtx new_i2_dest = SET_DEST (i2set);
2985
2986               while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
2987                      || GET_CODE (new_i3_dest) == STRICT_LOW_PART
2988                      || GET_CODE (new_i3_dest) == SUBREG)
2989                 new_i3_dest = XEXP (new_i3_dest, 0);
2990
2991               while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
2992                      || GET_CODE (new_i2_dest) == STRICT_LOW_PART
2993                      || GET_CODE (new_i2_dest) == SUBREG)
2994                 new_i2_dest = XEXP (new_i2_dest, 0);
2995
2996               if (REG_P (new_i3_dest)
2997                   && REG_P (new_i2_dest)
2998                   && REGNO (new_i3_dest) == REGNO (new_i2_dest))
2999                 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3000             }
3001         }
3002
3003       /* If we can split it and use I2DEST, go ahead and see if that
3004          helps things be recognized.  Verify that none of the registers
3005          are set between I2 and I3.  */
3006       if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
3007 #ifdef HAVE_cc0
3008           && REG_P (i2dest)
3009 #endif
3010           /* We need I2DEST in the proper mode.  If it is a hard register
3011              or the only use of a pseudo, we can change its mode.
3012              Make sure we don't change a hard register to have a mode that
3013              isn't valid for it, or change the number of registers.  */
3014           && (GET_MODE (*split) == GET_MODE (i2dest)
3015               || GET_MODE (*split) == VOIDmode
3016               || can_change_dest_mode (i2dest, added_sets_2,
3017                                        GET_MODE (*split)))
3018           && (next_real_insn (i2) == i3
3019               || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3020           /* We can't overwrite I2DEST if its value is still used by
3021              NEWPAT.  */
3022           && ! reg_referenced_p (i2dest, newpat))
3023         {
3024           rtx newdest = i2dest;
3025           enum rtx_code split_code = GET_CODE (*split);
3026           enum machine_mode split_mode = GET_MODE (*split);
3027           bool subst_done = false;
3028           newi2pat = NULL_RTX;
3029
3030           /* Get NEWDEST as a register in the proper mode.  We have already
3031              validated that we can do this.  */
3032           if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3033             {
3034               if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3035                 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3036               else
3037                 {
3038                   SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3039                   newdest = regno_reg_rtx[REGNO (i2dest)];
3040                 }
3041             }
3042
3043           /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3044              an ASHIFT.  This can occur if it was inside a PLUS and hence
3045              appeared to be a memory address.  This is a kludge.  */
3046           if (split_code == MULT
3047               && GET_CODE (XEXP (*split, 1)) == CONST_INT
3048               && INTVAL (XEXP (*split, 1)) > 0
3049               && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
3050             {
3051               SUBST (*split, gen_rtx_ASHIFT (split_mode,
3052                                              XEXP (*split, 0), GEN_INT (i)));
3053               /* Update split_code because we may not have a multiply
3054                  anymore.  */
3055               split_code = GET_CODE (*split);
3056             }
3057
3058 #ifdef INSN_SCHEDULING
3059           /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3060              be written as a ZERO_EXTEND.  */
3061           if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3062             {
3063 #ifdef LOAD_EXTEND_OP
3064               /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3065                  what it really is.  */
3066               if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3067                   == SIGN_EXTEND)
3068                 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3069                                                     SUBREG_REG (*split)));
3070               else
3071 #endif
3072                 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3073                                                     SUBREG_REG (*split)));
3074             }
3075 #endif
3076
3077           /* Attempt to split binary operators using arithmetic identities.  */
3078           if (BINARY_P (SET_SRC (newpat))
3079               && split_mode == GET_MODE (SET_SRC (newpat))
3080               && ! side_effects_p (SET_SRC (newpat)))
3081             {
3082               rtx setsrc = SET_SRC (newpat);
3083               enum machine_mode mode = GET_MODE (setsrc);
3084               enum rtx_code code = GET_CODE (setsrc);
3085               rtx src_op0 = XEXP (setsrc, 0);
3086               rtx src_op1 = XEXP (setsrc, 1);
3087
3088               /* Split "X = Y op Y" as "Z = Y; X = Z op Z".  */
3089               if (rtx_equal_p (src_op0, src_op1))
3090                 {
3091                   newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3092                   SUBST (XEXP (setsrc, 0), newdest);
3093                   SUBST (XEXP (setsrc, 1), newdest);
3094                   subst_done = true;
3095                 }
3096               /* Split "((P op Q) op R) op S" where op is PLUS or MULT.  */
3097               else if ((code == PLUS || code == MULT)
3098                        && GET_CODE (src_op0) == code
3099                        && GET_CODE (XEXP (src_op0, 0)) == code
3100                        && (INTEGRAL_MODE_P (mode)
3101                            || (FLOAT_MODE_P (mode)
3102                                && flag_unsafe_math_optimizations)))
3103                 {
3104                   rtx p = XEXP (XEXP (src_op0, 0), 0);
3105                   rtx q = XEXP (XEXP (src_op0, 0), 1);
3106                   rtx r = XEXP (src_op0, 1);
3107                   rtx s = src_op1;
3108
3109                   /* Split both "((X op Y) op X) op Y" and
3110                      "((X op Y) op Y) op X" as "T op T" where T is
3111                      "X op Y".  */
3112                   if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3113                        || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3114                     {
3115                       newi2pat = gen_rtx_SET (VOIDmode, newdest,
3116                                               XEXP (src_op0, 0));
3117                       SUBST (XEXP (setsrc, 0), newdest);
3118                       SUBST (XEXP (setsrc, 1), newdest);
3119                       subst_done = true;
3120                     }
3121                   /* Split "((X op X) op Y) op Y)" as "T op T" where
3122                      T is "X op Y".  */
3123                   else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3124                     {
3125                       rtx tmp = simplify_gen_binary (code, mode, p, r);
3126                       newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3127                       SUBST (XEXP (setsrc, 0), newdest);
3128                       SUBST (XEXP (setsrc, 1), newdest);
3129                       subst_done = true;
3130                     }
3131                 }
3132             }
3133
3134           if (!subst_done)
3135             {
3136               newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3137               SUBST (*split, newdest);
3138             }
3139
3140           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3141
3142           /* recog_for_combine might have added CLOBBERs to newi2pat.
3143              Make sure NEWPAT does not depend on the clobbered regs.  */
3144           if (GET_CODE (newi2pat) == PARALLEL)
3145             for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3146               if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3147                 {
3148                   rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3149                   if (reg_overlap_mentioned_p (reg, newpat))
3150                     {
3151                       undo_all ();
3152                       return 0;
3153                     }
3154                 }
3155
3156           /* If the split point was a MULT and we didn't have one before,
3157              don't use one now.  */
3158           if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3159             insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3160         }
3161     }
3162
3163   /* Check for a case where we loaded from memory in a narrow mode and
3164      then sign extended it, but we need both registers.  In that case,
3165      we have a PARALLEL with both loads from the same memory location.
3166      We can split this into a load from memory followed by a register-register
3167      copy.  This saves at least one insn, more if register allocation can
3168      eliminate the copy.
3169
3170      We cannot do this if the destination of the first assignment is a
3171      condition code register or cc0.  We eliminate this case by making sure
3172      the SET_DEST and SET_SRC have the same mode.
3173
3174      We cannot do this if the destination of the second assignment is
3175      a register that we have already assumed is zero-extended.  Similarly
3176      for a SUBREG of such a register.  */
3177
3178   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3179            && GET_CODE (newpat) == PARALLEL
3180            && XVECLEN (newpat, 0) == 2
3181            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3182            && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3183            && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3184                == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3185            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3186            && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3187                            XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3188            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3189                                    DF_INSN_LUID (i2))
3190            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3191            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3192            && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
3193                  (REG_P (temp)
3194                   && reg_stat[REGNO (temp)].nonzero_bits != 0
3195                   && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3196                   && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3197                   && (reg_stat[REGNO (temp)].nonzero_bits
3198                       != GET_MODE_MASK (word_mode))))
3199            && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3200                  && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3201                      (REG_P (temp)
3202                       && reg_stat[REGNO (temp)].nonzero_bits != 0
3203                       && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3204                       && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3205                       && (reg_stat[REGNO (temp)].nonzero_bits
3206                           != GET_MODE_MASK (word_mode)))))
3207            && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3208                                          SET_SRC (XVECEXP (newpat, 0, 1)))
3209            && ! find_reg_note (i3, REG_UNUSED,
3210                                SET_DEST (XVECEXP (newpat, 0, 0))))
3211     {
3212       rtx ni2dest;
3213
3214       newi2pat = XVECEXP (newpat, 0, 0);
3215       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3216       newpat = XVECEXP (newpat, 0, 1);
3217       SUBST (SET_SRC (newpat),
3218              gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3219       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3220
3221       if (i2_code_number >= 0)
3222         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3223
3224       if (insn_code_number >= 0)
3225         swap_i2i3 = 1;
3226     }
3227
3228   /* Similarly, check for a case where we have a PARALLEL of two independent
3229      SETs but we started with three insns.  In this case, we can do the sets
3230      as two separate insns.  This case occurs when some SET allows two
3231      other insns to combine, but the destination of that SET is still live.  */
3232
3233   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3234            && GET_CODE (newpat) == PARALLEL
3235            && XVECLEN (newpat, 0) == 2
3236            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3237            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3238            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3239            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3240            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3241            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3242            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3243                                    DF_INSN_LUID (i2))
3244            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3245                                   XVECEXP (newpat, 0, 0))
3246            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3247                                   XVECEXP (newpat, 0, 1))
3248            && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3249                  && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1))))
3250 #ifdef HAVE_cc0
3251            /* We cannot split the parallel into two sets if both sets
3252               reference cc0.  */
3253            && ! (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0))
3254                  && reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 1)))
3255 #endif
3256            )
3257     {
3258       /* Normally, it doesn't matter which of the two is done first,
3259          but it does if one references cc0.  In that case, it has to
3260          be first.  */
3261 #ifdef HAVE_cc0
3262       if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
3263         {
3264           newi2pat = XVECEXP (newpat, 0, 0);
3265           newpat = XVECEXP (newpat, 0, 1);
3266         }
3267       else
3268 #endif
3269         {
3270           newi2pat = XVECEXP (newpat, 0, 1);
3271           newpat = XVECEXP (newpat, 0, 0);
3272         }
3273
3274       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3275
3276       if (i2_code_number >= 0)
3277         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3278     }
3279
3280   /* If it still isn't recognized, fail and change things back the way they
3281      were.  */
3282   if ((insn_code_number < 0
3283        /* Is the result a reasonable ASM_OPERANDS?  */
3284        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3285     {
3286       undo_all ();
3287       return 0;
3288     }
3289
3290   /* If we had to change another insn, make sure it is valid also.  */
3291   if (undobuf.other_insn)
3292     {
3293       CLEAR_HARD_REG_SET (newpat_used_regs);
3294
3295       other_pat = PATTERN (undobuf.other_insn);
3296       other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3297                                              &new_other_notes);
3298
3299       if (other_code_number < 0 && ! check_asm_operands (other_pat))
3300         {
3301           undo_all ();
3302           return 0;
3303         }
3304     }
3305
3306 #ifdef HAVE_cc0
3307   /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3308      they are adjacent to each other or not.  */
3309   {
3310     rtx p = prev_nonnote_insn (i3);
3311     if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3312         && sets_cc0_p (newi2pat))
3313       {
3314         undo_all ();
3315         return 0;
3316       }
3317   }
3318 #endif
3319
3320   /* Only allow this combination if insn_rtx_costs reports that the
3321      replacement instructions are cheaper than the originals.  */
3322   if (!combine_validate_cost (i1, i2, i3, newpat, newi2pat, other_pat))
3323     {
3324       undo_all ();
3325       return 0;
3326     }
3327
3328   /* We now know that we can do this combination.  Merge the insns and
3329      update the status of registers and LOG_LINKS.  */
3330
3331   if (undobuf.other_insn)
3332     {
3333       rtx note, next;
3334
3335       PATTERN (undobuf.other_insn) = other_pat;
3336
3337       /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
3338          are still valid.  Then add any non-duplicate notes added by
3339          recog_for_combine.  */
3340       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
3341         {
3342           next = XEXP (note, 1);
3343
3344           if (REG_NOTE_KIND (note) == REG_UNUSED
3345               && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
3346             remove_note (undobuf.other_insn, note);
3347         }
3348
3349       distribute_notes (new_other_notes, undobuf.other_insn,
3350                         undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
3351     }
3352
3353   if (swap_i2i3)
3354     {
3355       rtx insn;
3356       rtx link;
3357       rtx ni2dest;
3358
3359       /* I3 now uses what used to be its destination and which is now
3360          I2's destination.  This requires us to do a few adjustments.  */
3361       PATTERN (i3) = newpat;
3362       adjust_for_new_dest (i3);
3363
3364       /* We need a LOG_LINK from I3 to I2.  But we used to have one,
3365          so we still will.
3366
3367          However, some later insn might be using I2's dest and have
3368          a LOG_LINK pointing at I3.  We must remove this link.
3369          The simplest way to remove the link is to point it at I1,
3370          which we know will be a NOTE.  */
3371
3372       /* newi2pat is usually a SET here; however, recog_for_combine might
3373          have added some clobbers.  */
3374       if (GET_CODE (newi2pat) == PARALLEL)
3375         ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
3376       else
3377         ni2dest = SET_DEST (newi2pat);
3378
3379       for (insn = NEXT_INSN (i3);
3380            insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3381                     || insn != BB_HEAD (this_basic_block->next_bb));
3382            insn = NEXT_INSN (insn))
3383         {
3384           if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
3385             {
3386               for (link = LOG_LINKS (insn); link;
3387                    link = XEXP (link, 1))
3388                 if (XEXP (link, 0) == i3)
3389                   XEXP (link, 0) = i1;
3390
3391               break;
3392             }
3393         }
3394     }
3395
3396   {
3397     rtx i3notes, i2notes, i1notes = 0;
3398     rtx i3links, i2links, i1links = 0;
3399     rtx midnotes = 0;
3400     unsigned int regno;
3401     /* Compute which registers we expect to eliminate.  newi2pat may be setting
3402        either i3dest or i2dest, so we must check it.  Also, i1dest may be the
3403        same as i3dest, in which case newi2pat may be setting i1dest.  */
3404     rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
3405                    || i2dest_in_i2src || i2dest_in_i1src
3406                    || !i2dest_killed
3407                    ? 0 : i2dest);
3408     rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
3409                    || (newi2pat && reg_set_p (i1dest, newi2pat))
3410                    || !i1dest_killed
3411                    ? 0 : i1dest);
3412
3413     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
3414        clear them.  */
3415     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
3416     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
3417     if (i1)
3418       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
3419
3420     /* Ensure that we do not have something that should not be shared but
3421        occurs multiple times in the new insns.  Check this by first
3422        resetting all the `used' flags and then copying anything is shared.  */
3423
3424     reset_used_flags (i3notes);
3425     reset_used_flags (i2notes);
3426     reset_used_flags (i1notes);
3427     reset_used_flags (newpat);
3428     reset_used_flags (newi2pat);
3429     if (undobuf.other_insn)
3430       reset_used_flags (PATTERN (undobuf.other_insn));
3431
3432     i3notes = copy_rtx_if_shared (i3notes);
3433     i2notes = copy_rtx_if_shared (i2notes);
3434     i1notes = copy_rtx_if_shared (i1notes);
3435     newpat = copy_rtx_if_shared (newpat);
3436     newi2pat = copy_rtx_if_shared (newi2pat);
3437     if (undobuf.other_insn)
3438       reset_used_flags (PATTERN (undobuf.other_insn));
3439
3440     INSN_CODE (i3) = insn_code_number;
3441     PATTERN (i3) = newpat;
3442
3443     if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
3444       {
3445         rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
3446
3447         reset_used_flags (call_usage);
3448         call_usage = copy_rtx (call_usage);
3449
3450         if (substed_i2)
3451           replace_rtx (call_usage, i2dest, i2src);
3452
3453         if (substed_i1)
3454           replace_rtx (call_usage, i1dest, i1src);
3455
3456         CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
3457       }
3458
3459     if (undobuf.other_insn)
3460       INSN_CODE (undobuf.other_insn) = other_code_number;
3461
3462     /* We had one special case above where I2 had more than one set and
3463        we replaced a destination of one of those sets with the destination
3464        of I3.  In that case, we have to update LOG_LINKS of insns later
3465        in this basic block.  Note that this (expensive) case is rare.
3466
3467        Also, in this case, we must pretend that all REG_NOTEs for I2
3468        actually came from I3, so that REG_UNUSED notes from I2 will be
3469        properly handled.  */
3470
3471     if (i3_subst_into_i2)
3472       {
3473         for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
3474           if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
3475                || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
3476               && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
3477               && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
3478               && ! find_reg_note (i2, REG_UNUSED,
3479                                   SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
3480             for (temp = NEXT_INSN (i2);
3481                  temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3482                           || BB_HEAD (this_basic_block) != temp);
3483                  temp = NEXT_INSN (temp))
3484               if (temp != i3 && INSN_P (temp))
3485                 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
3486                   if (XEXP (link, 0) == i2)
3487                     XEXP (link, 0) = i3;
3488
3489         if (i3notes)
3490           {
3491             rtx link = i3notes;
3492             while (XEXP (link, 1))
3493               link = XEXP (link, 1);
3494             XEXP (link, 1) = i2notes;
3495           }
3496         else
3497           i3notes = i2notes;
3498         i2notes = 0;
3499       }
3500
3501     LOG_LINKS (i3) = 0;
3502     REG_NOTES (i3) = 0;
3503     LOG_LINKS (i2) = 0;
3504     REG_NOTES (i2) = 0;
3505
3506     if (newi2pat)
3507       {
3508         INSN_CODE (i2) = i2_code_number;
3509         PATTERN (i2) = newi2pat;
3510       }
3511     else
3512       SET_INSN_DELETED (i2);
3513
3514     if (i1)
3515       {
3516         LOG_LINKS (i1) = 0;
3517         REG_NOTES (i1) = 0;
3518         SET_INSN_DELETED (i1);
3519       }
3520
3521     /* Get death notes for everything that is now used in either I3 or
3522        I2 and used to die in a previous insn.  If we built two new
3523        patterns, move from I1 to I2 then I2 to I3 so that we get the
3524        proper movement on registers that I2 modifies.  */
3525
3526     if (newi2pat)
3527       {
3528         move_deaths (newi2pat, NULL_RTX, DF_INSN_LUID (i1), i2, &midnotes);
3529         move_deaths (newpat, newi2pat, DF_INSN_LUID (i1), i3, &midnotes);
3530       }
3531     else
3532       move_deaths (newpat, NULL_RTX, i1 ? DF_INSN_LUID (i1) : DF_INSN_LUID (i2),
3533                    i3, &midnotes);
3534
3535     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
3536     if (i3notes)
3537       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
3538                         elim_i2, elim_i1);
3539     if (i2notes)
3540       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
3541                         elim_i2, elim_i1);
3542     if (i1notes)
3543       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
3544                         elim_i2, elim_i1);
3545     if (midnotes)
3546       distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3547                         elim_i2, elim_i1);
3548
3549     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
3550        know these are REG_UNUSED and want them to go to the desired insn,
3551        so we always pass it as i3.  */
3552
3553     if (newi2pat && new_i2_notes)
3554       distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3555     
3556     if (new_i3_notes)
3557       distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
3558
3559     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
3560        put a REG_DEAD note for it somewhere.  If NEWI2PAT exists and sets
3561        I3DEST, the death must be somewhere before I2, not I3.  If we passed I3
3562        in that case, it might delete I2.  Similarly for I2 and I1.
3563        Show an additional death due to the REG_DEAD note we make here.  If
3564        we discard it in distribute_notes, we will decrement it again.  */
3565
3566     if (i3dest_killed)
3567       {
3568         if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
3569           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
3570                                                NULL_RTX),
3571                             NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
3572         else
3573           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
3574                                                NULL_RTX),
3575                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3576                             elim_i2, elim_i1);
3577       }
3578
3579     if (i2dest_in_i2src)
3580       {
3581         if (newi2pat && reg_set_p (i2dest, newi2pat))
3582           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
3583                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3584         else
3585           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
3586                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3587                             NULL_RTX, NULL_RTX);
3588       }
3589
3590     if (i1dest_in_i1src)
3591       {
3592         if (newi2pat && reg_set_p (i1dest, newi2pat))
3593           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
3594                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3595         else
3596           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
3597                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3598                             NULL_RTX, NULL_RTX);
3599       }
3600
3601     distribute_links (i3links);
3602     distribute_links (i2links);
3603     distribute_links (i1links);
3604
3605     if (REG_P (i2dest))
3606       {
3607         rtx link;
3608         rtx i2_insn = 0, i2_val = 0, set;
3609
3610         /* The insn that used to set this register doesn't exist, and
3611            this life of the register may not exist either.  See if one of
3612            I3's links points to an insn that sets I2DEST.  If it does,
3613            that is now the last known value for I2DEST. If we don't update
3614            this and I2 set the register to a value that depended on its old
3615            contents, we will get confused.  If this insn is used, thing
3616            will be set correctly in combine_instructions.  */
3617
3618         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3619           if ((set = single_set (XEXP (link, 0))) != 0
3620               && rtx_equal_p (i2dest, SET_DEST (set)))
3621             i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
3622
3623         record_value_for_reg (i2dest, i2_insn, i2_val);
3624
3625         /* If the reg formerly set in I2 died only once and that was in I3,
3626            zero its use count so it won't make `reload' do any work.  */
3627         if (! added_sets_2
3628             && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
3629             && ! i2dest_in_i2src)
3630           {
3631             regno = REGNO (i2dest);
3632             INC_REG_N_SETS (regno, -1);
3633           }
3634       }
3635
3636     if (i1 && REG_P (i1dest))
3637       {
3638         rtx link;
3639         rtx i1_insn = 0, i1_val = 0, set;
3640
3641         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3642           if ((set = single_set (XEXP (link, 0))) != 0
3643               && rtx_equal_p (i1dest, SET_DEST (set)))
3644             i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
3645
3646         record_value_for_reg (i1dest, i1_insn, i1_val);
3647
3648         regno = REGNO (i1dest);
3649         if (! added_sets_1 && ! i1dest_in_i1src)
3650           INC_REG_N_SETS (regno, -1);
3651       }
3652
3653     /* Update reg_stat[].nonzero_bits et al for any changes that may have
3654        been made to this insn.  The order of
3655        set_nonzero_bits_and_sign_copies() is important.  Because newi2pat
3656        can affect nonzero_bits of newpat */
3657     if (newi2pat)
3658       note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
3659     note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
3660
3661     /* Set new_direct_jump_p if a new return or simple jump instruction
3662        has been created.
3663
3664        If I3 is now an unconditional jump, ensure that it has a
3665        BARRIER following it since it may have initially been a
3666        conditional jump.  It may also be the last nonnote insn.  */
3667
3668     if (returnjump_p (i3) || any_uncondjump_p (i3))
3669       {
3670         *new_direct_jump_p = 1;
3671         mark_jump_label (PATTERN (i3), i3, 0);
3672
3673         if ((temp = next_nonnote_insn (i3)) == NULL_RTX
3674             || !BARRIER_P (temp))
3675           emit_barrier_after (i3);
3676       }
3677
3678     if (undobuf.other_insn != NULL_RTX
3679         && (returnjump_p (undobuf.other_insn)
3680             || any_uncondjump_p (undobuf.other_insn)))
3681       {
3682         *new_direct_jump_p = 1;
3683
3684         if ((temp = next_nonnote_insn (undobuf.other_insn)) == NULL_RTX
3685             || !BARRIER_P (temp))
3686           emit_barrier_after (undobuf.other_insn);
3687       }
3688
3689     /* An NOOP jump does not need barrier, but it does need cleaning up
3690        of CFG.  */
3691     if (GET_CODE (newpat) == SET
3692         && SET_SRC (newpat) == pc_rtx
3693         && SET_DEST (newpat) == pc_rtx)
3694       *new_direct_jump_p = 1;
3695   }
3696   
3697   if (undobuf.other_insn != NULL_RTX)
3698     {
3699       if (dump_file)
3700         {
3701           fprintf (dump_file, "modifying other_insn ");
3702           dump_insn_slim (dump_file, undobuf.other_insn);
3703         }
3704       df_insn_rescan (undobuf.other_insn);
3705     }
3706
3707   if (i1 && !(NOTE_P(i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
3708     {
3709       if (dump_file)
3710         {
3711           fprintf (dump_file, "modifying insn i1 ");
3712           dump_insn_slim (dump_file, i1);
3713         }
3714       df_insn_rescan (i1);
3715     }
3716
3717   if (i2 && !(NOTE_P(i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
3718     {
3719       if (dump_file)
3720         {
3721           fprintf (dump_file, "modifying insn i2 ");
3722           dump_insn_slim (dump_file, i2);
3723         }
3724       df_insn_rescan (i2);
3725     }
3726
3727   if (i3 && !(NOTE_P(i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
3728     {
3729       if (dump_file)
3730         {
3731           fprintf (dump_file, "modifying insn i3 ");
3732           dump_insn_slim (dump_file, i3);
3733         }
3734       df_insn_rescan (i3);
3735     }
3736   
3737   combine_successes++;
3738   undo_commit ();
3739
3740   if (added_links_insn
3741       && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
3742       && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
3743     return added_links_insn;
3744   else
3745     return newi2pat ? i2 : i3;
3746 }
3747 \f
3748 /* Undo all the modifications recorded in undobuf.  */
3749
3750 static void
3751 undo_all (void)
3752 {
3753   struct undo *undo, *next;
3754
3755   for (undo = undobuf.undos; undo; undo = next)
3756     {
3757       next = undo->next;
3758       switch (undo->kind)
3759         {
3760         case UNDO_RTX:
3761           *undo->where.r = undo->old_contents.r;
3762           break;
3763         case UNDO_INT:
3764           *undo->where.i = undo->old_contents.i;
3765           break;
3766         case UNDO_MODE:
3767           PUT_MODE (*undo->where.r, undo->old_contents.m);
3768           break;
3769         default:
3770           gcc_unreachable ();
3771         }
3772
3773       undo->next = undobuf.frees;
3774       undobuf.frees = undo;
3775     }
3776
3777   undobuf.undos = 0;
3778 }
3779
3780 /* We've committed to accepting the changes we made.  Move all
3781    of the undos to the free list.  */
3782
3783 static void
3784 undo_commit (void)
3785 {
3786   struct undo *undo, *next;
3787
3788   for (undo = undobuf.undos; undo; undo = next)
3789     {
3790       next = undo->next;
3791       undo->next = undobuf.frees;
3792       undobuf.frees = undo;
3793     }
3794   undobuf.undos = 0;
3795 }
3796 \f
3797 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
3798    where we have an arithmetic expression and return that point.  LOC will
3799    be inside INSN.
3800
3801    try_combine will call this function to see if an insn can be split into
3802    two insns.  */
3803
3804 static rtx *
3805 find_split_point (rtx *loc, rtx insn)
3806 {
3807   rtx x = *loc;
3808   enum rtx_code code = GET_CODE (x);
3809   rtx *split;
3810   unsigned HOST_WIDE_INT len = 0;
3811   HOST_WIDE_INT pos = 0;
3812   int unsignedp = 0;
3813   rtx inner = NULL_RTX;
3814
3815   /* First special-case some codes.  */
3816   switch (code)
3817     {
3818     case SUBREG:
3819 #ifdef INSN_SCHEDULING
3820       /* If we are making a paradoxical SUBREG invalid, it becomes a split
3821          point.  */
3822       if (MEM_P (SUBREG_REG (x)))
3823         return loc;
3824 #endif
3825       return find_split_point (&SUBREG_REG (x), insn);
3826
3827     case MEM:
3828 #ifdef HAVE_lo_sum
3829       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
3830          using LO_SUM and HIGH.  */
3831       if (GET_CODE (XEXP (x, 0)) == CONST
3832           || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
3833         {
3834           SUBST (XEXP (x, 0),
3835                  gen_rtx_LO_SUM (Pmode,
3836                                  gen_rtx_HIGH (Pmode, XEXP (x, 0)),
3837                                  XEXP (x, 0)));
3838           return &XEXP (XEXP (x, 0), 0);
3839         }
3840 #endif
3841
3842       /* If we have a PLUS whose second operand is a constant and the
3843          address is not valid, perhaps will can split it up using
3844          the machine-specific way to split large constants.  We use
3845          the first pseudo-reg (one of the virtual regs) as a placeholder;
3846          it will not remain in the result.  */
3847       if (GET_CODE (XEXP (x, 0)) == PLUS
3848           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3849           && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
3850         {
3851           rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
3852           rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
3853                                  subst_insn);
3854
3855           /* This should have produced two insns, each of which sets our
3856              placeholder.  If the source of the second is a valid address,
3857              we can make put both sources together and make a split point
3858              in the middle.  */
3859
3860           if (seq
3861               && NEXT_INSN (seq) != NULL_RTX
3862               && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
3863               && NONJUMP_INSN_P (seq)
3864               && GET_CODE (PATTERN (seq)) == SET
3865               && SET_DEST (PATTERN (seq)) == reg
3866               && ! reg_mentioned_p (reg,
3867                                     SET_SRC (PATTERN (seq)))
3868               && NONJUMP_INSN_P (NEXT_INSN (seq))
3869               && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
3870               && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
3871               && memory_address_p (GET_MODE (x),
3872                                    SET_SRC (PATTERN (NEXT_INSN (seq)))))
3873             {
3874               rtx src1 = SET_SRC (PATTERN (seq));
3875               rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
3876
3877               /* Replace the placeholder in SRC2 with SRC1.  If we can
3878                  find where in SRC2 it was placed, that can become our
3879                  split point and we can replace this address with SRC2.
3880                  Just try two obvious places.  */
3881
3882               src2 = replace_rtx (src2, reg, src1);
3883               split = 0;
3884               if (XEXP (src2, 0) == src1)
3885                 split = &XEXP (src2, 0);
3886               else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
3887                        && XEXP (XEXP (src2, 0), 0) == src1)
3888                 split = &XEXP (XEXP (src2, 0), 0);
3889
3890               if (split)
3891                 {
3892                   SUBST (XEXP (x, 0), src2);
3893                   return split;
3894                 }
3895             }
3896
3897           /* If that didn't work, perhaps the first operand is complex and
3898              needs to be computed separately, so make a split point there.
3899              This will occur on machines that just support REG + CONST
3900              and have a constant moved through some previous computation.  */
3901
3902           else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
3903                    && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
3904                          && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
3905             return &XEXP (XEXP (x, 0), 0);
3906         }
3907       break;
3908
3909     case SET:
3910 #ifdef HAVE_cc0
3911       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
3912          ZERO_EXTRACT, the most likely reason why this doesn't match is that
3913          we need to put the operand into a register.  So split at that
3914          point.  */
3915
3916       if (SET_DEST (x) == cc0_rtx
3917           && GET_CODE (SET_SRC (x)) != COMPARE
3918           && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
3919           && !OBJECT_P (SET_SRC (x))
3920           && ! (GET_CODE (SET_SRC (x)) == SUBREG
3921                 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
3922         return &SET_SRC (x);
3923 #endif
3924
3925       /* See if we can split SET_SRC as it stands.  */
3926       split = find_split_point (&SET_SRC (x), insn);
3927       if (split && split != &SET_SRC (x))
3928         return split;
3929
3930       /* See if we can split SET_DEST as it stands.  */
3931       split = find_split_point (&SET_DEST (x), insn);
3932       if (split && split != &SET_DEST (x))
3933         return split;
3934
3935       /* See if this is a bitfield assignment with everything constant.  If
3936          so, this is an IOR of an AND, so split it into that.  */
3937       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
3938           && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
3939               <= HOST_BITS_PER_WIDE_INT)
3940           && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
3941           && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
3942           && GET_CODE (SET_SRC (x)) == CONST_INT
3943           && ((INTVAL (XEXP (SET_DEST (x), 1))
3944                + INTVAL (XEXP (SET_DEST (x), 2)))
3945               <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
3946           && ! side_effects_p (XEXP (SET_DEST (x), 0)))
3947         {
3948           HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
3949           unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
3950           unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
3951           rtx dest = XEXP (SET_DEST (x), 0);
3952           enum machine_mode mode = GET_MODE (dest);
3953           unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
3954           rtx or_mask;
3955
3956           if (BITS_BIG_ENDIAN)
3957             pos = GET_MODE_BITSIZE (mode) - len - pos;
3958
3959           or_mask = gen_int_mode (src << pos, mode);
3960           if (src == mask)
3961             SUBST (SET_SRC (x),
3962                    simplify_gen_binary (IOR, mode, dest, or_mask));
3963           else
3964             {
3965               rtx negmask = gen_int_mode (~(mask << pos), mode);
3966               SUBST (SET_SRC (x),
3967                      simplify_gen_binary (IOR, mode,
3968                                           simplify_gen_binary (AND, mode,
3969                                                                dest, negmask),
3970                                           or_mask));
3971             }
3972
3973           SUBST (SET_DEST (x), dest);
3974
3975           split = find_split_point (&SET_SRC (x), insn);
3976           if (split && split != &SET_SRC (x))
3977             return split;
3978         }
3979
3980       /* Otherwise, see if this is an operation that we can split into two.
3981          If so, try to split that.  */
3982       code = GET_CODE (SET_SRC (x));
3983
3984       switch (code)
3985         {
3986         case AND:
3987           /* If we are AND'ing with a large constant that is only a single
3988              bit and the result is only being used in a context where we
3989              need to know if it is zero or nonzero, replace it with a bit
3990              extraction.  This will avoid the large constant, which might
3991              have taken more than one insn to make.  If the constant were
3992              not a valid argument to the AND but took only one insn to make,
3993              this is no worse, but if it took more than one insn, it will
3994              be better.  */
3995
3996           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3997               && REG_P (XEXP (SET_SRC (x), 0))
3998               && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
3999               && REG_P (SET_DEST (x))
4000               && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
4001               && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4002               && XEXP (*split, 0) == SET_DEST (x)
4003               && XEXP (*split, 1) == const0_rtx)
4004             {
4005               rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4006                                                 XEXP (SET_SRC (x), 0),
4007                                                 pos, NULL_RTX, 1, 1, 0, 0);
4008               if (extraction != 0)
4009                 {
4010                   SUBST (SET_SRC (x), extraction);
4011                   return find_split_point (loc, insn);
4012                 }
4013             }
4014           break;
4015
4016         case NE:
4017           /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4018              is known to be on, this can be converted into a NEG of a shift.  */
4019           if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4020               && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4021               && 1 <= (pos = exact_log2
4022                        (nonzero_bits (XEXP (SET_SRC (x), 0),
4023                                       GET_MODE (XEXP (SET_SRC (x), 0))))))
4024             {
4025               enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4026
4027               SUBST (SET_SRC (x),
4028                      gen_rtx_NEG (mode,
4029                                   gen_rtx_LSHIFTRT (mode,
4030                                                     XEXP (SET_SRC (x), 0),
4031                                                     GEN_INT (pos))));
4032
4033               split = find_split_point (&SET_SRC (x), insn);
4034               if (split && split != &SET_SRC (x))
4035                 return split;
4036             }
4037           break;
4038
4039         case SIGN_EXTEND:
4040           inner = XEXP (SET_SRC (x), 0);
4041
4042           /* We can't optimize if either mode is a partial integer
4043              mode as we don't know how many bits are significant
4044              in those modes.  */
4045           if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4046               || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4047             break;
4048
4049           pos = 0;
4050           len = GET_MODE_BITSIZE (GET_MODE (inner));
4051           unsignedp = 0;
4052           break;
4053
4054         case SIGN_EXTRACT:
4055         case ZERO_EXTRACT:
4056           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
4057               && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
4058             {
4059               inner = XEXP (SET_SRC (x), 0);
4060               len = INTVAL (XEXP (SET_SRC (x), 1));
4061               pos = INTVAL (XEXP (SET_SRC (x), 2));
4062
4063               if (BITS_BIG_ENDIAN)
4064                 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
4065               unsignedp = (code == ZERO_EXTRACT);
4066             }
4067           break;
4068
4069         default:
4070           break;
4071         }
4072
4073       if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
4074         {
4075           enum machine_mode mode = GET_MODE (SET_SRC (x));
4076
4077           /* For unsigned, we have a choice of a shift followed by an
4078              AND or two shifts.  Use two shifts for field sizes where the
4079              constant might be too large.  We assume here that we can
4080              always at least get 8-bit constants in an AND insn, which is
4081              true for every current RISC.  */
4082
4083           if (unsignedp && len <= 8)
4084             {
4085               SUBST (SET_SRC (x),
4086                      gen_rtx_AND (mode,
4087                                   gen_rtx_LSHIFTRT
4088                                   (mode, gen_lowpart (mode, inner),
4089                                    GEN_INT (pos)),
4090                                   GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
4091
4092               split = find_split_point (&SET_SRC (x), insn);
4093               if (split && split != &SET_SRC (x))
4094                 return split;
4095             }
4096           else
4097             {
4098               SUBST (SET_SRC (x),
4099                      gen_rtx_fmt_ee
4100                      (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4101                       gen_rtx_ASHIFT (mode,
4102                                       gen_lowpart (mode, inner),
4103                                       GEN_INT (GET_MODE_BITSIZE (mode)
4104                                                - len - pos)),
4105                       GEN_INT (GET_MODE_BITSIZE (mode) - len)));
4106
4107               split = find_split_point (&SET_SRC (x), insn);
4108               if (split && split != &SET_SRC (x))
4109                 return split;
4110             }
4111         }
4112
4113       /* See if this is a simple operation with a constant as the second
4114          operand.  It might be that this constant is out of range and hence
4115          could be used as a split point.  */
4116       if (BINARY_P (SET_SRC (x))
4117           && CONSTANT_P (XEXP (SET_SRC (x), 1))
4118           && (OBJECT_P (XEXP (SET_SRC (x), 0))
4119               || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4120                   && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4121         return &XEXP (SET_SRC (x), 1);
4122
4123       /* Finally, see if this is a simple operation with its first operand
4124          not in a register.  The operation might require this operand in a
4125          register, so return it as a split point.  We can always do this
4126          because if the first operand were another operation, we would have
4127          already found it as a split point.  */
4128       if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4129           && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4130         return &XEXP (SET_SRC (x), 0);
4131
4132       return 0;
4133
4134     case AND:
4135     case IOR:
4136       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4137          it is better to write this as (not (ior A B)) so we can split it.
4138          Similarly for IOR.  */
4139       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4140         {
4141           SUBST (*loc,
4142                  gen_rtx_NOT (GET_MODE (x),
4143                               gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4144                                               GET_MODE (x),
4145                                               XEXP (XEXP (x, 0), 0),
4146                                               XEXP (XEXP (x, 1), 0))));
4147           return find_split_point (loc, insn);
4148         }
4149
4150       /* Many RISC machines have a large set of logical insns.  If the
4151          second operand is a NOT, put it first so we will try to split the
4152          other operand first.  */
4153       if (GET_CODE (XEXP (x, 1)) == NOT)
4154         {
4155           rtx tem = XEXP (x, 0);
4156           SUBST (XEXP (x, 0), XEXP (x, 1));
4157           SUBST (XEXP (x, 1), tem);
4158         }
4159       break;
4160
4161     default:
4162       break;
4163     }
4164
4165   /* Otherwise, select our actions depending on our rtx class.  */
4166   switch (GET_RTX_CLASS (code))
4167     {
4168     case RTX_BITFIELD_OPS:              /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
4169     case RTX_TERNARY:
4170       split = find_split_point (&XEXP (x, 2), insn);
4171       if (split)
4172         return split;
4173       /* ... fall through ...  */
4174     case RTX_BIN_ARITH:
4175     case RTX_COMM_ARITH:
4176     case RTX_COMPARE:
4177     case RTX_COMM_COMPARE:
4178       split = find_split_point (&XEXP (x, 1), insn);
4179       if (split)
4180         return split;
4181       /* ... fall through ...  */
4182     case RTX_UNARY:
4183       /* Some machines have (and (shift ...) ...) insns.  If X is not
4184          an AND, but XEXP (X, 0) is, use it as our split point.  */
4185       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4186         return &XEXP (x, 0);
4187
4188       split = find_split_point (&XEXP (x, 0), insn);
4189       if (split)
4190         return split;
4191       return loc;
4192
4193     default:
4194       /* Otherwise, we don't have a split point.  */
4195       return 0;
4196     }
4197 }
4198 \f
4199 /* Throughout X, replace FROM with TO, and return the result.
4200    The result is TO if X is FROM;
4201    otherwise the result is X, but its contents may have been modified.
4202    If they were modified, a record was made in undobuf so that
4203    undo_all will (among other things) return X to its original state.
4204
4205    If the number of changes necessary is too much to record to undo,
4206    the excess changes are not made, so the result is invalid.
4207    The changes already made can still be undone.
4208    undobuf.num_undo is incremented for such changes, so by testing that
4209    the caller can tell whether the result is valid.
4210
4211    `n_occurrences' is incremented each time FROM is replaced.
4212
4213    IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4214
4215    UNIQUE_COPY is nonzero if each substitution must be unique.  We do this
4216    by copying if `n_occurrences' is nonzero.  */
4217
4218 static rtx
4219 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
4220 {
4221   enum rtx_code code = GET_CODE (x);
4222   enum machine_mode op0_mode = VOIDmode;
4223   const char *fmt;
4224   int len, i;
4225   rtx new;
4226
4227 /* Two expressions are equal if they are identical copies of a shared
4228    RTX or if they are both registers with the same register number
4229    and mode.  */
4230
4231 #define COMBINE_RTX_EQUAL_P(X,Y)                        \
4232   ((X) == (Y)                                           \
4233    || (REG_P (X) && REG_P (Y)   \
4234        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4235
4236   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
4237     {
4238       n_occurrences++;
4239       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
4240     }
4241
4242   /* If X and FROM are the same register but different modes, they
4243      will not have been seen as equal above.  However, the log links code
4244      will make a LOG_LINKS entry for that case.  If we do nothing, we
4245      will try to rerecognize our original insn and, when it succeeds,
4246      we will delete the feeding insn, which is incorrect.
4247
4248      So force this insn not to match in this (rare) case.  */
4249   if (! in_dest && code == REG && REG_P (from)
4250       && reg_overlap_mentioned_p (x, from))
4251     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
4252
4253   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4254      of which may contain things that can be combined.  */
4255   if (code != MEM && code != LO_SUM && OBJECT_P (x))
4256     return x;
4257
4258   /* It is possible to have a subexpression appear twice in the insn.
4259      Suppose that FROM is a register that appears within TO.
4260      Then, after that subexpression has been scanned once by `subst',
4261      the second time it is scanned, TO may be found.  If we were
4262      to scan TO here, we would find FROM within it and create a
4263      self-referent rtl structure which is completely wrong.  */
4264   if (COMBINE_RTX_EQUAL_P (x, to))
4265     return to;
4266
4267   /* Parallel asm_operands need special attention because all of the
4268      inputs are shared across the arms.  Furthermore, unsharing the
4269      rtl results in recognition failures.  Failure to handle this case
4270      specially can result in circular rtl.
4271
4272      Solve this by doing a normal pass across the first entry of the
4273      parallel, and only processing the SET_DESTs of the subsequent
4274      entries.  Ug.  */
4275
4276   if (code == PARALLEL
4277       && GET_CODE (XVECEXP (x, 0, 0)) == SET
4278       && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
4279     {
4280       new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
4281
4282       /* If this substitution failed, this whole thing fails.  */
4283       if (GET_CODE (new) == CLOBBER
4284           && XEXP (new, 0) == const0_rtx)
4285         return new;
4286
4287       SUBST (XVECEXP (x, 0, 0), new);
4288
4289       for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
4290         {
4291           rtx dest = SET_DEST (XVECEXP (x, 0, i));
4292
4293           if (!REG_P (dest)
4294               && GET_CODE (dest) != CC0
4295               && GET_CODE (dest) != PC)
4296             {
4297               new = subst (dest, from, to, 0, unique_copy);
4298
4299               /* If this substitution failed, this whole thing fails.  */
4300               if (GET_CODE (new) == CLOBBER
4301                   && XEXP (new, 0) == const0_rtx)
4302                 return new;
4303
4304               SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
4305             }
4306         }
4307     }
4308   else
4309     {
4310       len = GET_RTX_LENGTH (code);
4311       fmt = GET_RTX_FORMAT (code);
4312
4313       /* We don't need to process a SET_DEST that is a register, CC0,
4314          or PC, so set up to skip this common case.  All other cases
4315          where we want to suppress replacing something inside a
4316          SET_SRC are handled via the IN_DEST operand.  */
4317       if (code == SET
4318           && (REG_P (SET_DEST (x))
4319               || GET_CODE (SET_DEST (x)) == CC0
4320               || GET_CODE (SET_DEST (x)) == PC))
4321         fmt = "ie";
4322
4323       /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
4324          constant.  */
4325       if (fmt[0] == 'e')
4326         op0_mode = GET_MODE (XEXP (x, 0));
4327
4328       for (i = 0; i < len; i++)
4329         {
4330           if (fmt[i] == 'E')
4331             {
4332               int j;
4333               for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4334                 {
4335                   if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
4336                     {
4337                       new = (unique_copy && n_occurrences
4338                              ? copy_rtx (to) : to);
4339                       n_occurrences++;
4340                     }
4341                   else
4342                     {
4343                       new = subst (XVECEXP (x, i, j), from, to, 0,
4344                                    unique_copy);
4345
4346                       /* If this substitution failed, this whole thing
4347                          fails.  */
4348                       if (GET_CODE (new) == CLOBBER
4349                           && XEXP (new, 0) == const0_rtx)
4350                         return new;
4351                     }
4352
4353                   SUBST (XVECEXP (x, i, j), new);
4354                 }
4355             }
4356           else if (fmt[i] == 'e')
4357             {
4358               /* If this is a register being set, ignore it.  */
4359               new = XEXP (x, i);
4360               if (in_dest
4361                   && i == 0
4362                   && (((code == SUBREG || code == ZERO_EXTRACT)
4363                        && REG_P (new))
4364                       || code == STRICT_LOW_PART))
4365                 ;
4366
4367               else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
4368                 {
4369                   /* In general, don't install a subreg involving two
4370                      modes not tieable.  It can worsen register
4371                      allocation, and can even make invalid reload
4372                      insns, since the reg inside may need to be copied
4373                      from in the outside mode, and that may be invalid
4374                      if it is an fp reg copied in integer mode.
4375
4376                      We allow two exceptions to this: It is valid if
4377                      it is inside another SUBREG and the mode of that
4378                      SUBREG and the mode of the inside of TO is
4379                      tieable and it is valid if X is a SET that copies
4380                      FROM to CC0.  */
4381
4382                   if (GET_CODE (to) == SUBREG
4383                       && ! MODES_TIEABLE_P (GET_MODE (to),
4384                                             GET_MODE (SUBREG_REG (to)))
4385                       && ! (code == SUBREG
4386                             && MODES_TIEABLE_P (GET_MODE (x),
4387                                                 GET_MODE (SUBREG_REG (to))))
4388 #ifdef HAVE_cc0
4389                       && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
4390 #endif
4391                       )
4392                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
4393
4394 #ifdef CANNOT_CHANGE_MODE_CLASS
4395                   if (code == SUBREG
4396                       && REG_P (to)
4397                       && REGNO (to) < FIRST_PSEUDO_REGISTER
4398                       && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
4399                                                    GET_MODE (to),
4400                                                    GET_MODE (x)))
4401                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
4402 #endif
4403
4404                   new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
4405                   n_occurrences++;
4406                 }
4407               else
4408                 /* If we are in a SET_DEST, suppress most cases unless we
4409                    have gone inside a MEM, in which case we want to
4410                    simplify the address.  We assume here that things that
4411                    are actually part of the destination have their inner
4412                    parts in the first expression.  This is true for SUBREG,
4413                    STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
4414                    things aside from REG and MEM that should appear in a
4415                    SET_DEST.  */
4416                 new = subst (XEXP (x, i), from, to,
4417                              (((in_dest
4418                                 && (code == SUBREG || code == STRICT_LOW_PART
4419                                     || code == ZERO_EXTRACT))
4420                                || code == SET)
4421                               && i == 0), unique_copy);
4422
4423               /* If we found that we will have to reject this combination,
4424                  indicate that by returning the CLOBBER ourselves, rather than
4425                  an expression containing it.  This will speed things up as
4426                  well as prevent accidents where two CLOBBERs are considered
4427                  to be equal, thus producing an incorrect simplification.  */
4428
4429               if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
4430                 return new;
4431
4432               if (GET_CODE (x) == SUBREG
4433                   && (GET_CODE (new) == CONST_INT
4434                       || GET_CODE (new) == CONST_DOUBLE))
4435                 {
4436                   enum machine_mode mode = GET_MODE (x);
4437
4438                   x = simplify_subreg (GET_MODE (x), new,
4439                                        GET_MODE (SUBREG_REG (x)),
4440                                        SUBREG_BYTE (x));
4441                   if (! x)
4442                     x = gen_rtx_CLOBBER (mode, const0_rtx);
4443                 }
4444               else if (GET_CODE (new) == CONST_INT
4445                        && GET_CODE (x) == ZERO_EXTEND)
4446                 {
4447                   x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
4448                                                 new, GET_MODE (XEXP (x, 0)));
4449                   gcc_assert (x);
4450                 }
4451               else
4452                 SUBST (XEXP (x, i), new);
4453             }
4454         }
4455     }
4456
4457   /* Try to simplify X.  If the simplification changed the code, it is likely
4458      that further simplification will help, so loop, but limit the number
4459      of repetitions that will be performed.  */
4460
4461   for (i = 0; i < 4; i++)
4462     {
4463       /* If X is sufficiently simple, don't bother trying to do anything
4464          with it.  */
4465       if (code != CONST_INT && code != REG && code != CLOBBER)
4466         x = combine_simplify_rtx (x, op0_mode, in_dest);
4467
4468       if (GET_CODE (x) == code)
4469         break;
4470
4471       code = GET_CODE (x);
4472
4473       /* We no longer know the original mode of operand 0 since we
4474          have changed the form of X)  */
4475       op0_mode = VOIDmode;
4476     }
4477
4478   return x;
4479 }
4480 \f
4481 /* Simplify X, a piece of RTL.  We just operate on the expression at the
4482    outer level; call `subst' to simplify recursively.  Return the new
4483    expression.
4484
4485    OP0_MODE is the original mode of XEXP (x, 0).  IN_DEST is nonzero
4486    if we are inside a SET_DEST.  */
4487
4488 static rtx
4489 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
4490 {
4491   enum rtx_code code = GET_CODE (x);
4492   enum machine_mode mode = GET_MODE (x);
4493   rtx temp;
4494   int i;
4495
4496   /* If this is a commutative operation, put a constant last and a complex
4497      expression first.  We don't need to do this for comparisons here.  */
4498   if (COMMUTATIVE_ARITH_P (x)
4499       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
4500     {
4501       temp = XEXP (x, 0);
4502       SUBST (XEXP (x, 0), XEXP (x, 1));
4503       SUBST (XEXP (x, 1), temp);
4504     }
4505
4506   /* If this is a simple operation applied to an IF_THEN_ELSE, try
4507      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
4508      things.  Check for cases where both arms are testing the same
4509      condition.
4510
4511      Don't do anything if all operands are very simple.  */
4512
4513   if ((BINARY_P (x)
4514        && ((!OBJECT_P (XEXP (x, 0))
4515             && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4516                   && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
4517            || (!OBJECT_P (XEXP (x, 1))
4518                && ! (GET_CODE (XEXP (x, 1)) == SUBREG
4519                      && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
4520       || (UNARY_P (x)
4521           && (!OBJECT_P (XEXP (x, 0))
4522                && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4523                      && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
4524     {
4525       rtx cond, true_rtx, false_rtx;
4526
4527       cond = if_then_else_cond (x, &true_rtx, &false_rtx);
4528       if (cond != 0
4529           /* If everything is a comparison, what we have is highly unlikely
4530              to be simpler, so don't use it.  */
4531           && ! (COMPARISON_P (x)
4532                 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
4533         {
4534           rtx cop1 = const0_rtx;
4535           enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
4536
4537           if (cond_code == NE && COMPARISON_P (cond))
4538             return x;
4539
4540           /* Simplify the alternative arms; this may collapse the true and
4541              false arms to store-flag values.  Be careful to use copy_rtx
4542              here since true_rtx or false_rtx might share RTL with x as a
4543              result of the if_then_else_cond call above.  */
4544           true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
4545           false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
4546
4547           /* If true_rtx and false_rtx are not general_operands, an if_then_else
4548              is unlikely to be simpler.  */
4549           if (general_operand (true_rtx, VOIDmode)
4550               && general_operand (false_rtx, VOIDmode))
4551             {
4552               enum rtx_code reversed;
4553
4554               /* Restarting if we generate a store-flag expression will cause
4555                  us to loop.  Just drop through in this case.  */
4556
4557               /* If the result values are STORE_FLAG_VALUE and zero, we can
4558                  just make the comparison operation.  */
4559               if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
4560                 x = simplify_gen_relational (cond_code, mode, VOIDmode,
4561                                              cond, cop1);
4562               else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
4563                        && ((reversed = reversed_comparison_code_parts
4564                                         (cond_code, cond, cop1, NULL))
4565                            != UNKNOWN))
4566                 x = simplify_gen_relational (reversed, mode, VOIDmode,
4567                                              cond, cop1);
4568
4569               /* Likewise, we can make the negate of a comparison operation
4570                  if the result values are - STORE_FLAG_VALUE and zero.  */
4571               else if (GET_CODE (true_rtx) == CONST_INT
4572                        && INTVAL (true_rtx) == - STORE_FLAG_VALUE
4573                        && false_rtx == const0_rtx)
4574                 x = simplify_gen_unary (NEG, mode,
4575                                         simplify_gen_relational (cond_code,
4576                                                                  mode, VOIDmode,
4577                                                                  cond, cop1),
4578                                         mode);
4579               else if (GET_CODE (false_rtx) == CONST_INT
4580                        && INTVAL (false_rtx) == - STORE_FLAG_VALUE
4581                        && true_rtx == const0_rtx
4582                        && ((reversed = reversed_comparison_code_parts
4583                                         (cond_code, cond, cop1, NULL))
4584                            != UNKNOWN))
4585                 x = simplify_gen_unary (NEG, mode,
4586                                         simplify_gen_relational (reversed,
4587                                                                  mode, VOIDmode,
4588                                                                  cond, cop1),
4589                                         mode);
4590               else
4591                 return gen_rtx_IF_THEN_ELSE (mode,
4592                                              simplify_gen_relational (cond_code,
4593                                                                       mode,
4594                                                                       VOIDmode,
4595                                                                       cond,
4596                                                                       cop1),
4597                                              true_rtx, false_rtx);
4598
4599               code = GET_CODE (x);
4600               op0_mode = VOIDmode;
4601             }
4602         }
4603     }
4604
4605   /* Try to fold this expression in case we have constants that weren't
4606      present before.  */
4607   temp = 0;
4608   switch (GET_RTX_CLASS (code))
4609     {
4610     case RTX_UNARY:
4611       if (op0_mode == VOIDmode)
4612         op0_mode = GET_MODE (XEXP (x, 0));
4613       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
4614       break;
4615     case RTX_COMPARE:
4616     case RTX_COMM_COMPARE:
4617       {
4618         enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
4619         if (cmp_mode == VOIDmode)
4620           {
4621             cmp_mode = GET_MODE (XEXP (x, 1));
4622             if (cmp_mode == VOIDmode)
4623               cmp_mode = op0_mode;
4624           }
4625         temp = simplify_relational_operation (code, mode, cmp_mode,
4626                                               XEXP (x, 0), XEXP (x, 1));
4627       }
4628       break;
4629     case RTX_COMM_ARITH:
4630     case RTX_BIN_ARITH:
4631       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
4632       break;
4633     case RTX_BITFIELD_OPS:
4634     case RTX_TERNARY:
4635       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
4636                                          XEXP (x, 1), XEXP (x, 2));
4637       break;
4638     default:
4639       break;
4640     }
4641
4642   if (temp)
4643     {
4644       x = temp;
4645       code = GET_CODE (temp);
4646       op0_mode = VOIDmode;
4647       mode = GET_MODE (temp);
4648     }
4649
4650   /* First see if we can apply the inverse distributive law.  */
4651   if (code == PLUS || code == MINUS
4652       || code == AND || code == IOR || code == XOR)
4653     {
4654       x = apply_distributive_law (x);
4655       code = GET_CODE (x);
4656       op0_mode = VOIDmode;
4657     }
4658
4659   /* If CODE is an associative operation not otherwise handled, see if we
4660      can associate some operands.  This can win if they are constants or
4661      if they are logically related (i.e. (a & b) & a).  */
4662   if ((code == PLUS || code == MINUS || code == MULT || code == DIV
4663        || code == AND || code == IOR || code == XOR
4664        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
4665       && ((INTEGRAL_MODE_P (mode) && code != DIV)
4666           || (flag_unsafe_math_optimizations && FLOAT_MODE_P (mode))))
4667     {
4668       if (GET_CODE (XEXP (x, 0)) == code)
4669         {
4670           rtx other = XEXP (XEXP (x, 0), 0);
4671           rtx inner_op0 = XEXP (XEXP (x, 0), 1);
4672           rtx inner_op1 = XEXP (x, 1);
4673           rtx inner;
4674
4675           /* Make sure we pass the constant operand if any as the second
4676              one if this is a commutative operation.  */
4677           if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
4678             {
4679               rtx tem = inner_op0;
4680               inner_op0 = inner_op1;
4681               inner_op1 = tem;
4682             }
4683           inner = simplify_binary_operation (code == MINUS ? PLUS
4684                                              : code == DIV ? MULT
4685                                              : code,
4686                                              mode, inner_op0, inner_op1);
4687
4688           /* For commutative operations, try the other pair if that one
4689              didn't simplify.  */
4690           if (inner == 0 && COMMUTATIVE_ARITH_P (x))
4691             {
4692               other = XEXP (XEXP (x, 0), 1);
4693               inner = simplify_binary_operation (code, mode,
4694                                                  XEXP (XEXP (x, 0), 0),
4695                                                  XEXP (x, 1));
4696             }
4697
4698           if (inner)
4699             return simplify_gen_binary (code, mode, other, inner);
4700         }
4701     }
4702
4703   /* A little bit of algebraic simplification here.  */
4704   switch (code)
4705     {
4706     case MEM:
4707       /* Ensure that our address has any ASHIFTs converted to MULT in case
4708          address-recognizing predicates are called later.  */
4709       temp = make_compound_operation (XEXP (x, 0), MEM);
4710       SUBST (XEXP (x, 0), temp);
4711       break;
4712
4713     case SUBREG:
4714       if (op0_mode == VOIDmode)
4715         op0_mode = GET_MODE (SUBREG_REG (x));
4716
4717       /* See if this can be moved to simplify_subreg.  */
4718       if (CONSTANT_P (SUBREG_REG (x))
4719           && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
4720              /* Don't call gen_lowpart if the inner mode
4721                 is VOIDmode and we cannot simplify it, as SUBREG without
4722                 inner mode is invalid.  */
4723           && (GET_MODE (SUBREG_REG (x)) != VOIDmode
4724               || gen_lowpart_common (mode, SUBREG_REG (x))))
4725         return gen_lowpart (mode, SUBREG_REG (x));
4726
4727       if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
4728         break;
4729       {
4730         rtx temp;
4731         temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
4732                                 SUBREG_BYTE (x));
4733         if (temp)
4734           return temp;
4735       }
4736
4737       /* Don't change the mode of the MEM if that would change the meaning
4738          of the address.  */
4739       if (MEM_P (SUBREG_REG (x))
4740           && (MEM_VOLATILE_P (SUBREG_REG (x))
4741               || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
4742         return gen_rtx_CLOBBER (mode, const0_rtx);
4743
4744       /* Note that we cannot do any narrowing for non-constants since
4745          we might have been counting on using the fact that some bits were
4746          zero.  We now do this in the SET.  */
4747
4748       break;
4749
4750     case NEG:
4751       temp = expand_compound_operation (XEXP (x, 0));
4752
4753       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
4754          replaced by (lshiftrt X C).  This will convert
4755          (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
4756
4757       if (GET_CODE (temp) == ASHIFTRT
4758           && GET_CODE (XEXP (temp, 1)) == CONST_INT
4759           && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
4760         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
4761                                      INTVAL (XEXP (temp, 1)));
4762
4763       /* If X has only a single bit that might be nonzero, say, bit I, convert
4764          (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
4765          MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
4766          (sign_extract X 1 Y).  But only do this if TEMP isn't a register
4767          or a SUBREG of one since we'd be making the expression more
4768          complex if it was just a register.  */
4769
4770       if (!REG_P (temp)
4771           && ! (GET_CODE (temp) == SUBREG
4772                 && REG_P (SUBREG_REG (temp)))
4773           && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
4774         {
4775           rtx temp1 = simplify_shift_const
4776             (NULL_RTX, ASHIFTRT, mode,
4777              simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
4778                                    GET_MODE_BITSIZE (mode) - 1 - i),
4779              GET_MODE_BITSIZE (mode) - 1 - i);
4780
4781           /* If all we did was surround TEMP with the two shifts, we
4782              haven't improved anything, so don't use it.  Otherwise,
4783              we are better off with TEMP1.  */
4784           if (GET_CODE (temp1) != ASHIFTRT
4785               || GET_CODE (XEXP (temp1, 0)) != ASHIFT
4786               || XEXP (XEXP (temp1, 0), 0) != temp)
4787             return temp1;
4788         }
4789       break;
4790
4791     case TRUNCATE:
4792       /* We can't handle truncation to a partial integer mode here
4793          because we don't know the real bitsize of the partial
4794          integer mode.  */
4795       if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
4796         break;
4797
4798       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4799           && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4800                                     GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
4801         SUBST (XEXP (x, 0),
4802                force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
4803                               GET_MODE_MASK (mode), 0));
4804
4805       /* Similarly to what we do in simplify-rtx.c, a truncate of a register
4806          whose value is a comparison can be replaced with a subreg if
4807          STORE_FLAG_VALUE permits.  */
4808       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4809           && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4810           && (temp = get_last_value (XEXP (x, 0)))
4811           && COMPARISON_P (temp))
4812         return gen_lowpart (mode, XEXP (x, 0));
4813       break;
4814
4815 #ifdef HAVE_cc0
4816     case COMPARE:
4817       /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4818          using cc0, in which case we want to leave it as a COMPARE
4819          so we can distinguish it from a register-register-copy.  */
4820       if (XEXP (x, 1) == const0_rtx)
4821         return XEXP (x, 0);
4822
4823       /* x - 0 is the same as x unless x's mode has signed zeros and
4824          allows rounding towards -infinity.  Under those conditions,
4825          0 - 0 is -0.  */
4826       if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4827             && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4828           && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4829         return XEXP (x, 0);
4830       break;
4831 #endif
4832
4833     case CONST:
4834       /* (const (const X)) can become (const X).  Do it this way rather than
4835          returning the inner CONST since CONST can be shared with a
4836          REG_EQUAL note.  */
4837       if (GET_CODE (XEXP (x, 0)) == CONST)
4838         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4839       break;
4840
4841 #ifdef HAVE_lo_sum
4842     case LO_SUM:
4843       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
4844          can add in an offset.  find_split_point will split this address up
4845          again if it doesn't match.  */
4846       if (GET_CODE (XEXP (x, 0)) == HIGH
4847           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4848         return XEXP (x, 1);
4849       break;
4850 #endif
4851
4852     case PLUS:
4853       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4854          when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4855          bit-field and can be replaced by either a sign_extend or a
4856          sign_extract.  The `and' may be a zero_extend and the two
4857          <c>, -<c> constants may be reversed.  */
4858       if (GET_CODE (XEXP (x, 0)) == XOR
4859           && GET_CODE (XEXP (x, 1)) == CONST_INT
4860           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4861           && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4862           && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4863               || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4864           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4865           && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4866                && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4867                && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4868                    == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4869               || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4870                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4871                       == (unsigned int) i + 1))))
4872         return simplify_shift_const
4873           (NULL_RTX, ASHIFTRT, mode,
4874            simplify_shift_const (NULL_RTX, ASHIFT, mode,
4875                                  XEXP (XEXP (XEXP (x, 0), 0), 0),
4876                                  GET_MODE_BITSIZE (mode) - (i + 1)),
4877            GET_MODE_BITSIZE (mode) - (i + 1));
4878
4879       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4880          can become (ashiftrt (ashift (xor x 1) C) C) where C is
4881          the bitsize of the mode - 1.  This allows simplification of
4882          "a = (b & 8) == 0;"  */
4883       if (XEXP (x, 1) == constm1_rtx
4884           && !REG_P (XEXP (x, 0))
4885           && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4886                 && REG_P (SUBREG_REG (XEXP (x, 0))))
4887           && nonzero_bits (XEXP (x, 0), mode) == 1)
4888         return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4889            simplify_shift_const (NULL_RTX, ASHIFT, mode,
4890                                  gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4891                                  GET_MODE_BITSIZE (mode) - 1),
4892            GET_MODE_BITSIZE (mode) - 1);
4893
4894       /* If we are adding two things that have no bits in common, convert
4895          the addition into an IOR.  This will often be further simplified,
4896          for example in cases like ((a & 1) + (a & 2)), which can
4897          become a & 3.  */
4898
4899       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4900           && (nonzero_bits (XEXP (x, 0), mode)
4901               & nonzero_bits (XEXP (x, 1), mode)) == 0)
4902         {
4903           /* Try to simplify the expression further.  */
4904           rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4905           temp = combine_simplify_rtx (tor, mode, in_dest);
4906
4907           /* If we could, great.  If not, do not go ahead with the IOR
4908              replacement, since PLUS appears in many special purpose
4909              address arithmetic instructions.  */
4910           if (GET_CODE (temp) != CLOBBER && temp != tor)
4911             return temp;
4912         }
4913       break;
4914
4915     case MINUS:
4916       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4917          (and <foo> (const_int pow2-1))  */
4918       if (GET_CODE (XEXP (x, 1)) == AND
4919           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4920           && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4921           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4922         return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4923                                        -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4924       break;
4925
4926     case MULT:
4927       /* If we have (mult (plus A B) C), apply the distributive law and then
4928          the inverse distributive law to see if things simplify.  This
4929          occurs mostly in addresses, often when unrolling loops.  */
4930
4931       if (GET_CODE (XEXP (x, 0)) == PLUS)
4932         {
4933           rtx result = distribute_and_simplify_rtx (x, 0);
4934           if (result)
4935             return result;
4936         }
4937
4938       /* Try simplify a*(b/c) as (a*b)/c.  */
4939       if (FLOAT_MODE_P (mode) && flag_unsafe_math_optimizations
4940           && GET_CODE (XEXP (x, 0)) == DIV)
4941         {
4942           rtx tem = simplify_binary_operation (MULT, mode,
4943                                                XEXP (XEXP (x, 0), 0),
4944                                                XEXP (x, 1));
4945           if (tem)
4946             return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
4947         }
4948       break;
4949
4950     case UDIV:
4951       /* If this is a divide by a power of two, treat it as a shift if
4952          its first operand is a shift.  */
4953       if (GET_CODE (XEXP (x, 1)) == CONST_INT
4954           && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4955           && (GET_CODE (XEXP (x, 0)) == ASHIFT
4956               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4957               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4958               || GET_CODE (XEXP (x, 0)) == ROTATE
4959               || GET_CODE (XEXP (x, 0)) == ROTATERT))
4960         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4961       break;
4962
4963     case EQ:  case NE:
4964     case GT:  case GTU:  case GE:  case GEU:
4965     case LT:  case LTU:  case LE:  case LEU:
4966     case UNEQ:  case LTGT:
4967     case UNGT:  case UNGE:
4968     case UNLT:  case UNLE:
4969     case UNORDERED: case ORDERED:
4970       /* If the first operand is a condition code, we can't do anything
4971          with it.  */
4972       if (GET_CODE (XEXP (x, 0)) == COMPARE
4973           || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4974               && ! CC0_P (XEXP (x, 0))))
4975         {
4976           rtx op0 = XEXP (x, 0);
4977           rtx op1 = XEXP (x, 1);
4978           enum rtx_code new_code;
4979
4980           if (GET_CODE (op0) == COMPARE)
4981             op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4982
4983           /* Simplify our comparison, if possible.  */
4984           new_code = simplify_comparison (code, &op0, &op1);
4985
4986           /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4987              if only the low-order bit is possibly nonzero in X (such as when
4988              X is a ZERO_EXTRACT of one bit).  Similarly, we can convert EQ to
4989              (xor X 1) or (minus 1 X); we use the former.  Finally, if X is
4990              known to be either 0 or -1, NE becomes a NEG and EQ becomes
4991              (plus X 1).
4992
4993              Remove any ZERO_EXTRACT we made when thinking this was a
4994              comparison.  It may now be simpler to use, e.g., an AND.  If a
4995              ZERO_EXTRACT is indeed appropriate, it will be placed back by
4996              the call to make_compound_operation in the SET case.  */
4997
4998           if (STORE_FLAG_VALUE == 1
4999               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5000               && op1 == const0_rtx
5001               && mode == GET_MODE (op0)
5002               && nonzero_bits (op0, mode) == 1)
5003             return gen_lowpart (mode,
5004                                 expand_compound_operation (op0));
5005
5006           else if (STORE_FLAG_VALUE == 1
5007                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5008                    && op1 == const0_rtx
5009                    && mode == GET_MODE (op0)
5010                    && (num_sign_bit_copies (op0, mode)
5011                        == GET_MODE_BITSIZE (mode)))
5012             {
5013               op0 = expand_compound_operation (op0);
5014               return simplify_gen_unary (NEG, mode,
5015                                          gen_lowpart (mode, op0),
5016                                          mode);
5017             }
5018
5019           else if (STORE_FLAG_VALUE == 1
5020                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5021                    && op1 == const0_rtx
5022                    && mode == GET_MODE (op0)
5023                    && nonzero_bits (op0, mode) == 1)
5024             {
5025               op0 = expand_compound_operation (op0);
5026               return simplify_gen_binary (XOR, mode,
5027                                           gen_lowpart (mode, op0),
5028                                           const1_rtx);
5029             }
5030
5031           else if (STORE_FLAG_VALUE == 1
5032                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5033                    && op1 == const0_rtx
5034                    && mode == GET_MODE (op0)
5035                    && (num_sign_bit_copies (op0, mode)
5036                        == GET_MODE_BITSIZE (mode)))
5037             {
5038               op0 = expand_compound_operation (op0);
5039               return plus_constant (gen_lowpart (mode, op0), 1);
5040             }
5041
5042           /* If STORE_FLAG_VALUE is -1, we have cases similar to
5043              those above.  */
5044           if (STORE_FLAG_VALUE == -1
5045               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5046               && op1 == const0_rtx
5047               && (num_sign_bit_copies (op0, mode)
5048                   == GET_MODE_BITSIZE (mode)))
5049             return gen_lowpart (mode,
5050                                 expand_compound_operation (op0));
5051
5052           else if (STORE_FLAG_VALUE == -1
5053                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5054                    && op1 == const0_rtx
5055                    && mode == GET_MODE (op0)
5056                    && nonzero_bits (op0, mode) == 1)
5057             {
5058               op0 = expand_compound_operation (op0);
5059               return simplify_gen_unary (NEG, mode,
5060                                          gen_lowpart (mode, op0),
5061                                          mode);
5062             }
5063
5064           else if (STORE_FLAG_VALUE == -1
5065                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5066                    && op1 == const0_rtx
5067                    && mode == GET_MODE (op0)
5068                    && (num_sign_bit_copies (op0, mode)
5069                        == GET_MODE_BITSIZE (mode)))
5070             {
5071               op0 = expand_compound_operation (op0);
5072               return simplify_gen_unary (NOT, mode,
5073                                          gen_lowpart (mode, op0),
5074                                          mode);
5075             }
5076
5077           /* If X is 0/1, (eq X 0) is X-1.  */
5078           else if (STORE_FLAG_VALUE == -1
5079                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5080                    && op1 == const0_rtx
5081                    && mode == GET_MODE (op0)
5082                    && nonzero_bits (op0, mode) == 1)
5083             {
5084               op0 = expand_compound_operation (op0);
5085               return plus_constant (gen_lowpart (mode, op0), -1);
5086             }
5087
5088           /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5089              one bit that might be nonzero, we can convert (ne x 0) to
5090              (ashift x c) where C puts the bit in the sign bit.  Remove any
5091              AND with STORE_FLAG_VALUE when we are done, since we are only
5092              going to test the sign bit.  */
5093           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5094               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5095               && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5096                   == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5097               && op1 == const0_rtx
5098               && mode == GET_MODE (op0)
5099               && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5100             {
5101               x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5102                                         expand_compound_operation (op0),
5103                                         GET_MODE_BITSIZE (mode) - 1 - i);
5104               if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5105                 return XEXP (x, 0);
5106               else
5107                 return x;
5108             }
5109
5110           /* If the code changed, return a whole new comparison.  */
5111           if (new_code != code)
5112             return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5113
5114           /* Otherwise, keep this operation, but maybe change its operands.
5115              This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
5116           SUBST (XEXP (x, 0), op0);
5117           SUBST (XEXP (x, 1), op1);
5118         }
5119       break;
5120
5121     case IF_THEN_ELSE:
5122       return simplify_if_then_else (x);
5123
5124     case ZERO_EXTRACT:
5125     case SIGN_EXTRACT:
5126     case ZERO_EXTEND:
5127     case SIGN_EXTEND:
5128       /* If we are processing SET_DEST, we are done.  */
5129       if (in_dest)
5130         return x;
5131
5132       return expand_compound_operation (x);
5133
5134     case SET:
5135       return simplify_set (x);
5136
5137     case AND:
5138     case IOR:
5139       return simplify_logical (x);
5140
5141     case ASHIFT:
5142     case LSHIFTRT:
5143     case ASHIFTRT:
5144     case ROTATE:
5145     case ROTATERT:
5146       /* If this is a shift by a constant amount, simplify it.  */
5147       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5148         return simplify_shift_const (x, code, mode, XEXP (x, 0),
5149                                      INTVAL (XEXP (x, 1)));
5150
5151       else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5152         SUBST (XEXP (x, 1),
5153                force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5154                               ((HOST_WIDE_INT) 1
5155                                << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5156                               - 1,
5157                               0));
5158       break;
5159
5160     default:
5161       break;
5162     }
5163
5164   return x;
5165 }
5166 \f
5167 /* Simplify X, an IF_THEN_ELSE expression.  Return the new expression.  */
5168
5169 static rtx
5170 simplify_if_then_else (rtx x)
5171 {
5172   enum machine_mode mode = GET_MODE (x);
5173   rtx cond = XEXP (x, 0);
5174   rtx true_rtx = XEXP (x, 1);
5175   rtx false_rtx = XEXP (x, 2);
5176   enum rtx_code true_code = GET_CODE (cond);
5177   int comparison_p = COMPARISON_P (cond);
5178   rtx temp;
5179   int i;
5180   enum rtx_code false_code;
5181   rtx reversed;
5182
5183   /* Simplify storing of the truth value.  */
5184   if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5185     return simplify_gen_relational (true_code, mode, VOIDmode,
5186                                     XEXP (cond, 0), XEXP (cond, 1));
5187
5188   /* Also when the truth value has to be reversed.  */
5189   if (comparison_p
5190       && true_rtx == const0_rtx && false_rtx == const_true_rtx
5191       && (reversed = reversed_comparison (cond, mode)))
5192     return reversed;
5193
5194   /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5195      in it is being compared against certain values.  Get the true and false
5196      comparisons and see if that says anything about the value of each arm.  */
5197
5198   if (comparison_p
5199       && ((false_code = reversed_comparison_code (cond, NULL))
5200           != UNKNOWN)
5201       && REG_P (XEXP (cond, 0)))
5202     {
5203       HOST_WIDE_INT nzb;
5204       rtx from = XEXP (cond, 0);
5205       rtx true_val = XEXP (cond, 1);
5206       rtx false_val = true_val;
5207       int swapped = 0;
5208
5209       /* If FALSE_CODE is EQ, swap the codes and arms.  */
5210
5211       if (false_code == EQ)
5212         {
5213           swapped = 1, true_code = EQ, false_code = NE;
5214           temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5215         }
5216
5217       /* If we are comparing against zero and the expression being tested has
5218          only a single bit that might be nonzero, that is its value when it is
5219          not equal to zero.  Similarly if it is known to be -1 or 0.  */
5220
5221       if (true_code == EQ && true_val == const0_rtx
5222           && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5223         {
5224           false_code = EQ;
5225           false_val = GEN_INT (trunc_int_for_mode (nzb, GET_MODE (from)));
5226         }
5227       else if (true_code == EQ && true_val == const0_rtx
5228                && (num_sign_bit_copies (from, GET_MODE (from))
5229                    == GET_MODE_BITSIZE (GET_MODE (from))))
5230         {
5231           false_code = EQ;
5232           false_val = constm1_rtx;
5233         }
5234
5235       /* Now simplify an arm if we know the value of the register in the
5236          branch and it is used in the arm.  Be careful due to the potential
5237          of locally-shared RTL.  */
5238
5239       if (reg_mentioned_p (from, true_rtx))
5240         true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
5241                                       from, true_val),
5242                       pc_rtx, pc_rtx, 0, 0);
5243       if (reg_mentioned_p (from, false_rtx))
5244         false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
5245                                    from, false_val),
5246                        pc_rtx, pc_rtx, 0, 0);
5247
5248       SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
5249       SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
5250
5251       true_rtx = XEXP (x, 1);
5252       false_rtx = XEXP (x, 2);
5253       true_code = GET_CODE (cond);
5254     }
5255
5256   /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
5257      reversed, do so to avoid needing two sets of patterns for
5258      subtract-and-branch insns.  Similarly if we have a constant in the true
5259      arm, the false arm is the same as the first operand of the comparison, or
5260      the false arm is more complicated than the true arm.  */
5261
5262   if (comparison_p
5263       && reversed_comparison_code (cond, NULL) != UNKNOWN
5264       && (true_rtx == pc_rtx
5265           || (CONSTANT_P (true_rtx)
5266               && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
5267           || true_rtx == const0_rtx
5268           || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
5269           || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
5270               && !OBJECT_P (false_rtx))
5271           || reg_mentioned_p (true_rtx, false_rtx)
5272           || rtx_equal_p (false_rtx, XEXP (cond, 0))))
5273     {
5274       true_code = reversed_comparison_code (cond, NULL);
5275       SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
5276       SUBST (XEXP (x, 1), false_rtx);
5277       SUBST (XEXP (x, 2), true_rtx);
5278
5279       temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5280       cond = XEXP (x, 0);
5281
5282       /* It is possible that the conditional has been simplified out.  */
5283       true_code = GET_CODE (cond);
5284       comparison_p = COMPARISON_P (cond);
5285     }
5286
5287   /* If the two arms are identical, we don't need the comparison.  */
5288
5289   if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
5290     return true_rtx;
5291
5292   /* Convert a == b ? b : a to "a".  */
5293   if (true_code == EQ && ! side_effects_p (cond)
5294       && !HONOR_NANS (mode)
5295       && rtx_equal_p (XEXP (cond, 0), false_rtx)
5296       && rtx_equal_p (XEXP (cond, 1), true_rtx))
5297     return false_rtx;
5298   else if (true_code == NE && ! side_effects_p (cond)
5299            && !HONOR_NANS (mode)
5300            && rtx_equal_p (XEXP (cond, 0), true_rtx)
5301            && rtx_equal_p (XEXP (cond, 1), false_rtx))
5302     return true_rtx;
5303
5304   /* Look for cases where we have (abs x) or (neg (abs X)).  */
5305
5306   if (GET_MODE_CLASS (mode) == MODE_INT
5307       && GET_CODE (false_rtx) == NEG
5308       && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
5309       && comparison_p
5310       && rtx_equal_p (true_rtx, XEXP (cond, 0))
5311       && ! side_effects_p (true_rtx))
5312     switch (true_code)
5313       {
5314       case GT:
5315       case GE:
5316         return simplify_gen_unary (ABS, mode, true_rtx, mode);
5317       case LT:
5318       case LE:
5319         return
5320           simplify_gen_unary (NEG, mode,
5321                               simplify_gen_unary (ABS, mode, true_rtx, mode),
5322                               mode);
5323       default:
5324         break;
5325       }
5326
5327   /* Look for MIN or MAX.  */
5328
5329   if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
5330       && comparison_p
5331       && rtx_equal_p (XEXP (cond, 0), true_rtx)
5332       && rtx_equal_p (XEXP (cond, 1), false_rtx)
5333       && ! side_effects_p (cond))
5334     switch (true_code)
5335       {
5336       case GE:
5337       case GT:
5338         return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
5339       case LE:
5340       case LT:
5341         return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
5342       case GEU:
5343       case GTU:
5344         return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
5345       case LEU:
5346       case LTU:
5347         return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
5348       default:
5349         break;
5350       }
5351
5352   /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
5353      second operand is zero, this can be done as (OP Z (mult COND C2)) where
5354      C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
5355      SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
5356      We can do this kind of thing in some cases when STORE_FLAG_VALUE is
5357      neither 1 or -1, but it isn't worth checking for.  */
5358
5359   if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5360       && comparison_p
5361       && GET_MODE_CLASS (mode) == MODE_INT
5362       && ! side_effects_p (x))
5363     {
5364       rtx t = make_compound_operation (true_rtx, SET);
5365       rtx f = make_compound_operation (false_rtx, SET);
5366       rtx cond_op0 = XEXP (cond, 0);
5367       rtx cond_op1 = XEXP (cond, 1);
5368       enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
5369       enum machine_mode m = mode;
5370       rtx z = 0, c1 = NULL_RTX;
5371
5372       if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
5373            || GET_CODE (t) == IOR || GET_CODE (t) == XOR
5374            || GET_CODE (t) == ASHIFT
5375            || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
5376           && rtx_equal_p (XEXP (t, 0), f))
5377         c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
5378
5379       /* If an identity-zero op is commutative, check whether there
5380          would be a match if we swapped the operands.  */
5381       else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
5382                 || GET_CODE (t) == XOR)
5383                && rtx_equal_p (XEXP (t, 1), f))
5384         c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
5385       else if (GET_CODE (t) == SIGN_EXTEND
5386                && (GET_CODE (XEXP (t, 0)) == PLUS
5387                    || GET_CODE (XEXP (t, 0)) == MINUS
5388                    || GET_CODE (XEXP (t, 0)) == IOR
5389                    || GET_CODE (XEXP (t, 0)) == XOR
5390                    || GET_CODE (XEXP (t, 0)) == ASHIFT
5391                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5392                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5393                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5394                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5395                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5396                && (num_sign_bit_copies (f, GET_MODE (f))
5397                    > (unsigned int)
5398                      (GET_MODE_BITSIZE (mode)
5399                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
5400         {
5401           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5402           extend_op = SIGN_EXTEND;
5403           m = GET_MODE (XEXP (t, 0));
5404         }
5405       else if (GET_CODE (t) == SIGN_EXTEND
5406                && (GET_CODE (XEXP (t, 0)) == PLUS
5407                    || GET_CODE (XEXP (t, 0)) == IOR
5408                    || GET_CODE (XEXP (t, 0)) == XOR)
5409                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5410                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5411                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5412                && (num_sign_bit_copies (f, GET_MODE (f))
5413                    > (unsigned int)
5414                      (GET_MODE_BITSIZE (mode)
5415                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
5416         {
5417           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5418           extend_op = SIGN_EXTEND;
5419           m = GET_MODE (XEXP (t, 0));
5420         }
5421       else if (GET_CODE (t) == ZERO_EXTEND
5422                && (GET_CODE (XEXP (t, 0)) == PLUS
5423                    || GET_CODE (XEXP (t, 0)) == MINUS
5424                    || GET_CODE (XEXP (t, 0)) == IOR
5425                    || GET_CODE (XEXP (t, 0)) == XOR
5426                    || GET_CODE (XEXP (t, 0)) == ASHIFT
5427                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5428                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5429                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5430                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5431                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5432                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5433                && ((nonzero_bits (f, GET_MODE (f))
5434                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
5435                    == 0))
5436         {
5437           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5438           extend_op = ZERO_EXTEND;
5439           m = GET_MODE (XEXP (t, 0));
5440         }
5441       else if (GET_CODE (t) == ZERO_EXTEND
5442                && (GET_CODE (XEXP (t, 0)) == PLUS
5443                    || GET_CODE (XEXP (t, 0)) == IOR
5444                    || GET_CODE (XEXP (t, 0)) == XOR)
5445                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5446                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5447                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5448                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5449                && ((nonzero_bits (f, GET_MODE (f))
5450                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
5451                    == 0))
5452         {
5453           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5454           extend_op = ZERO_EXTEND;
5455           m = GET_MODE (XEXP (t, 0));
5456         }
5457
5458       if (z)
5459         {
5460           temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
5461                                                  cond_op0, cond_op1),
5462                         pc_rtx, pc_rtx, 0, 0);
5463           temp = simplify_gen_binary (MULT, m, temp,
5464                                       simplify_gen_binary (MULT, m, c1,
5465                                                            const_true_rtx));
5466           temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
5467           temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
5468
5469           if (extend_op != UNKNOWN)
5470             temp = simplify_gen_unary (extend_op, mode, temp, m);
5471
5472           return temp;
5473         }
5474     }
5475
5476   /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
5477      1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
5478      negation of a single bit, we can convert this operation to a shift.  We
5479      can actually do this more generally, but it doesn't seem worth it.  */
5480
5481   if (true_code == NE && XEXP (cond, 1) == const0_rtx
5482       && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5483       && ((1 == nonzero_bits (XEXP (cond, 0), mode)
5484            && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
5485           || ((num_sign_bit_copies (XEXP (cond, 0), mode)
5486                == GET_MODE_BITSIZE (mode))
5487               && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
5488     return
5489       simplify_shift_const (NULL_RTX, ASHIFT, mode,
5490                             gen_lowpart (mode, XEXP (cond, 0)), i);
5491
5492   /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8.  */
5493   if (true_code == NE && XEXP (cond, 1) == const0_rtx
5494       && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5495       && GET_MODE (XEXP (cond, 0)) == mode
5496       && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
5497           == nonzero_bits (XEXP (cond, 0), mode)
5498       && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
5499     return XEXP (cond, 0);
5500
5501   return x;
5502 }
5503 \f
5504 /* Simplify X, a SET expression.  Return the new expression.  */
5505
5506 static rtx
5507 simplify_set (rtx x)
5508 {
5509   rtx src = SET_SRC (x);
5510   rtx dest = SET_DEST (x);
5511   enum machine_mode mode
5512     = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
5513   rtx other_insn;
5514   rtx *cc_use;
5515
5516   /* (set (pc) (return)) gets written as (return).  */
5517   if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
5518     return src;
5519
5520   /* Now that we know for sure which bits of SRC we are using, see if we can
5521      simplify the expression for the object knowing that we only need the
5522      low-order bits.  */
5523
5524   if (GET_MODE_CLASS (mode) == MODE_INT
5525       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5526     {
5527       src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, 0);
5528       SUBST (SET_SRC (x), src);
5529     }
5530
5531   /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5532      the comparison result and try to simplify it unless we already have used
5533      undobuf.other_insn.  */
5534   if ((GET_MODE_CLASS (mode) == MODE_CC
5535        || GET_CODE (src) == COMPARE
5536        || CC0_P (dest))
5537       && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5538       && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5539       && COMPARISON_P (*cc_use)
5540       && rtx_equal_p (XEXP (*cc_use, 0), dest))
5541     {
5542       enum rtx_code old_code = GET_CODE (*cc_use);
5543       enum rtx_code new_code;
5544       rtx op0, op1, tmp;
5545       int other_changed = 0;
5546       enum machine_mode compare_mode = GET_MODE (dest);
5547
5548       if (GET_CODE (src) == COMPARE)
5549         op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5550       else
5551         op0 = src, op1 = CONST0_RTX (GET_MODE (src));
5552
5553       tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
5554                                            op0, op1);
5555       if (!tmp)
5556         new_code = old_code;
5557       else if (!CONSTANT_P (tmp))
5558         {
5559           new_code = GET_CODE (tmp);
5560           op0 = XEXP (tmp, 0);
5561           op1 = XEXP (tmp, 1);
5562         }
5563       else
5564         {
5565           rtx pat = PATTERN (other_insn);
5566           undobuf.other_insn = other_insn;
5567           SUBST (*cc_use, tmp);
5568
5569           /* Attempt to simplify CC user.  */
5570           if (GET_CODE (pat) == SET)
5571             {
5572               rtx new = simplify_rtx (SET_SRC (pat));
5573               if (new != NULL_RTX)
5574                 SUBST (SET_SRC (pat), new);
5575             }
5576
5577           /* Convert X into a no-op move.  */
5578           SUBST (SET_DEST (x), pc_rtx);
5579           SUBST (SET_SRC (x), pc_rtx);
5580           return x;
5581         }
5582
5583       /* Simplify our comparison, if possible.  */
5584       new_code = simplify_comparison (new_code, &op0, &op1);
5585
5586 #ifdef SELECT_CC_MODE
5587       /* If this machine has CC modes other than CCmode, check to see if we
5588          need to use a different CC mode here.  */
5589       if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
5590         compare_mode = GET_MODE (op0);
5591       else
5592         compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5593
5594 #ifndef HAVE_cc0
5595       /* If the mode changed, we have to change SET_DEST, the mode in the
5596          compare, and the mode in the place SET_DEST is used.  If SET_DEST is
5597          a hard register, just build new versions with the proper mode.  If it
5598          is a pseudo, we lose unless it is only time we set the pseudo, in
5599          which case we can safely change its mode.  */
5600       if (compare_mode != GET_MODE (dest))
5601         {
5602           if (can_change_dest_mode (dest, 0, compare_mode))
5603             {
5604               unsigned int regno = REGNO (dest);
5605               rtx new_dest;
5606
5607               if (regno < FIRST_PSEUDO_REGISTER)
5608                 new_dest = gen_rtx_REG (compare_mode, regno);
5609               else
5610                 {
5611                   SUBST_MODE (regno_reg_rtx[regno], compare_mode);
5612                   new_dest = regno_reg_rtx[regno];
5613                 }
5614
5615               SUBST (SET_DEST (x), new_dest);
5616               SUBST (XEXP (*cc_use, 0), new_dest);
5617               other_changed = 1;
5618
5619               dest = new_dest;
5620             }
5621         }
5622 #endif  /* cc0 */
5623 #endif  /* SELECT_CC_MODE */
5624
5625       /* If the code changed, we have to build a new comparison in
5626          undobuf.other_insn.  */
5627       if (new_code != old_code)
5628         {
5629           int other_changed_previously = other_changed;
5630           unsigned HOST_WIDE_INT mask;
5631
5632           SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5633                                           dest, const0_rtx));
5634           other_changed = 1;
5635
5636           /* If the only change we made was to change an EQ into an NE or
5637              vice versa, OP0 has only one bit that might be nonzero, and OP1
5638              is zero, check if changing the user of the condition code will
5639              produce a valid insn.  If it won't, we can keep the original code
5640              in that insn by surrounding our operation with an XOR.  */
5641
5642           if (((old_code == NE && new_code == EQ)
5643                || (old_code == EQ && new_code == NE))
5644               && ! other_changed_previously && op1 == const0_rtx
5645               && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5646               && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5647             {
5648               rtx pat = PATTERN (other_insn), note = 0;
5649
5650               if ((recog_for_combine (&pat, other_insn, &note) < 0
5651                    && ! check_asm_operands (pat)))
5652                 {
5653                   PUT_CODE (*cc_use, old_code);
5654                   other_changed = 0;
5655
5656                   op0 = simplify_gen_binary (XOR, GET_MODE (op0),
5657                                              op0, GEN_INT (mask));
5658                 }
5659             }
5660         }
5661
5662       if (other_changed)
5663         undobuf.other_insn = other_insn;
5664
5665 #ifdef HAVE_cc0
5666       /* If we are now comparing against zero, change our source if
5667          needed.  If we do not use cc0, we always have a COMPARE.  */
5668       if (op1 == const0_rtx && dest == cc0_rtx)
5669         {
5670           SUBST (SET_SRC (x), op0);
5671           src = op0;
5672         }
5673       else
5674 #endif
5675
5676       /* Otherwise, if we didn't previously have a COMPARE in the
5677          correct mode, we need one.  */
5678       if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5679         {
5680           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5681           src = SET_SRC (x);
5682         }
5683       else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
5684         {
5685           SUBST (SET_SRC (x), op0);
5686           src = SET_SRC (x);
5687         }
5688       /* Otherwise, update the COMPARE if needed.  */
5689       else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
5690         {
5691           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5692           src = SET_SRC (x);
5693         }
5694     }
5695   else
5696     {
5697       /* Get SET_SRC in a form where we have placed back any
5698          compound expressions.  Then do the checks below.  */
5699       src = make_compound_operation (src, SET);
5700       SUBST (SET_SRC (x), src);
5701     }
5702
5703   /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5704      and X being a REG or (subreg (reg)), we may be able to convert this to
5705      (set (subreg:m2 x) (op)).
5706
5707      We can always do this if M1 is narrower than M2 because that means that
5708      we only care about the low bits of the result.
5709
5710      However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
5711      perform a narrower operation than requested since the high-order bits will
5712      be undefined.  On machine where it is defined, this transformation is safe
5713      as long as M1 and M2 have the same number of words.  */
5714
5715   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5716       && !OBJECT_P (SUBREG_REG (src))
5717       && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5718            / UNITS_PER_WORD)
5719           == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5720                + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5721 #ifndef WORD_REGISTER_OPERATIONS
5722       && (GET_MODE_SIZE (GET_MODE (src))
5723         < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5724 #endif
5725 #ifdef CANNOT_CHANGE_MODE_CLASS
5726       && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
5727             && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5728                                          GET_MODE (SUBREG_REG (src)),
5729                                          GET_MODE (src)))
5730 #endif
5731       && (REG_P (dest)
5732           || (GET_CODE (dest) == SUBREG
5733               && REG_P (SUBREG_REG (dest)))))
5734     {
5735       SUBST (SET_DEST (x),
5736              gen_lowpart (GET_MODE (SUBREG_REG (src)),
5737                                       dest));
5738       SUBST (SET_SRC (x), SUBREG_REG (src));
5739
5740       src = SET_SRC (x), dest = SET_DEST (x);
5741     }
5742
5743 #ifdef HAVE_cc0
5744   /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5745      in SRC.  */
5746   if (dest == cc0_rtx
5747       && GET_CODE (src) == SUBREG
5748       && subreg_lowpart_p (src)
5749       && (GET_MODE_BITSIZE (GET_MODE (src))
5750           < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5751     {
5752       rtx inner = SUBREG_REG (src);
5753       enum machine_mode inner_mode = GET_MODE (inner);
5754
5755       /* Here we make sure that we don't have a sign bit on.  */
5756       if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5757           && (nonzero_bits (inner, inner_mode)
5758               < ((unsigned HOST_WIDE_INT) 1
5759                  << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5760         {
5761           SUBST (SET_SRC (x), inner);
5762           src = SET_SRC (x);
5763         }
5764     }
5765 #endif
5766
5767 #ifdef LOAD_EXTEND_OP
5768   /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5769      would require a paradoxical subreg.  Replace the subreg with a
5770      zero_extend to avoid the reload that would otherwise be required.  */
5771
5772   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5773       && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
5774       && SUBREG_BYTE (src) == 0
5775       && (GET_MODE_SIZE (GET_MODE (src))
5776           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5777       && MEM_P (SUBREG_REG (src)))
5778     {
5779       SUBST (SET_SRC (x),
5780              gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5781                             GET_MODE (src), SUBREG_REG (src)));
5782
5783       src = SET_SRC (x);
5784     }
5785 #endif
5786
5787   /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5788      are comparing an item known to be 0 or -1 against 0, use a logical
5789      operation instead. Check for one of the arms being an IOR of the other
5790      arm with some value.  We compute three terms to be IOR'ed together.  In
5791      practice, at most two will be nonzero.  Then we do the IOR's.  */
5792
5793   if (GET_CODE (dest) != PC
5794       && GET_CODE (src) == IF_THEN_ELSE
5795       && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5796       && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5797       && XEXP (XEXP (src, 0), 1) == const0_rtx
5798       && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5799 #ifdef HAVE_conditional_move
5800       && ! can_conditionally_move_p (GET_MODE (src))
5801 #endif
5802       && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5803                                GET_MODE (XEXP (XEXP (src, 0), 0)))
5804           == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5805       && ! side_effects_p (src))
5806     {
5807       rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5808                       ? XEXP (src, 1) : XEXP (src, 2));
5809       rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5810                    ? XEXP (src, 2) : XEXP (src, 1));
5811       rtx term1 = const0_rtx, term2, term3;
5812
5813       if (GET_CODE (true_rtx) == IOR
5814           && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5815         term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5816       else if (GET_CODE (true_rtx) == IOR
5817                && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5818         term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5819       else if (GET_CODE (false_rtx) == IOR
5820                && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5821         term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5822       else if (GET_CODE (false_rtx) == IOR
5823                && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5824         term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5825
5826       term2 = simplify_gen_binary (AND, GET_MODE (src),
5827                                    XEXP (XEXP (src, 0), 0), true_rtx);
5828       term3 = simplify_gen_binary (AND, GET_MODE (src),
5829                                    simplify_gen_unary (NOT, GET_MODE (src),
5830                                                        XEXP (XEXP (src, 0), 0),
5831                                                        GET_MODE (src)),
5832                                    false_rtx);
5833
5834       SUBST (SET_SRC (x),
5835              simplify_gen_binary (IOR, GET_MODE (src),
5836                                   simplify_gen_binary (IOR, GET_MODE (src),
5837                                                        term1, term2),
5838                                   term3));
5839
5840       src = SET_SRC (x);
5841     }
5842
5843   /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5844      whole thing fail.  */
5845   if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5846     return src;
5847   else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5848     return dest;
5849   else
5850     /* Convert this into a field assignment operation, if possible.  */
5851     return make_field_assignment (x);
5852 }
5853 \f
5854 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5855    result.  */
5856
5857 static rtx
5858 simplify_logical (rtx x)
5859 {
5860   enum machine_mode mode = GET_MODE (x);
5861   rtx op0 = XEXP (x, 0);
5862   rtx op1 = XEXP (x, 1);
5863
5864   switch (GET_CODE (x))
5865     {
5866     case AND:
5867       /* We can call simplify_and_const_int only if we don't lose
5868          any (sign) bits when converting INTVAL (op1) to
5869          "unsigned HOST_WIDE_INT".  */
5870       if (GET_CODE (op1) == CONST_INT
5871           && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5872               || INTVAL (op1) > 0))
5873         {
5874           x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5875           if (GET_CODE (x) != AND)
5876             return x;
5877
5878           op0 = XEXP (x, 0);
5879           op1 = XEXP (x, 1);
5880         }
5881
5882       /* If we have any of (and (ior A B) C) or (and (xor A B) C),
5883          apply the distributive law and then the inverse distributive
5884          law to see if things simplify.  */
5885       if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5886         {
5887           rtx result = distribute_and_simplify_rtx (x, 0);
5888           if (result)
5889             return result;
5890         }
5891       if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5892         {
5893           rtx result = distribute_and_simplify_rtx (x, 1);
5894           if (result)
5895             return result;
5896         }
5897       break;
5898
5899     case IOR:
5900       /* If we have (ior (and A B) C), apply the distributive law and then
5901          the inverse distributive law to see if things simplify.  */
5902
5903       if (GET_CODE (op0) == AND)
5904         {
5905           rtx result = distribute_and_simplify_rtx (x, 0);
5906           if (result)
5907             return result;
5908         }
5909
5910       if (GET_CODE (op1) == AND)
5911         {
5912           rtx result = distribute_and_simplify_rtx (x, 1);
5913           if (result)
5914             return result;
5915         }
5916       break;
5917
5918     default:
5919       gcc_unreachable ();
5920     }
5921
5922   return x;
5923 }
5924 \f
5925 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5926    operations" because they can be replaced with two more basic operations.
5927    ZERO_EXTEND is also considered "compound" because it can be replaced with
5928    an AND operation, which is simpler, though only one operation.
5929
5930    The function expand_compound_operation is called with an rtx expression
5931    and will convert it to the appropriate shifts and AND operations,
5932    simplifying at each stage.
5933
5934    The function make_compound_operation is called to convert an expression
5935    consisting of shifts and ANDs into the equivalent compound expression.
5936    It is the inverse of this function, loosely speaking.  */
5937
5938 static rtx
5939 expand_compound_operation (rtx x)
5940 {
5941   unsigned HOST_WIDE_INT pos = 0, len;
5942   int unsignedp = 0;
5943   unsigned int modewidth;
5944   rtx tem;
5945
5946   switch (GET_CODE (x))
5947     {
5948     case ZERO_EXTEND:
5949       unsignedp = 1;
5950     case SIGN_EXTEND:
5951       /* We can't necessarily use a const_int for a multiword mode;
5952          it depends on implicitly extending the value.
5953          Since we don't know the right way to extend it,
5954          we can't tell whether the implicit way is right.
5955
5956          Even for a mode that is no wider than a const_int,
5957          we can't win, because we need to sign extend one of its bits through
5958          the rest of it, and we don't know which bit.  */
5959       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5960         return x;
5961
5962       /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5963          (zero_extend:MODE FROM) or (sign_extend:MODE FROM).  It is for any MEM
5964          because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5965          reloaded. If not for that, MEM's would very rarely be safe.
5966
5967          Reject MODEs bigger than a word, because we might not be able
5968          to reference a two-register group starting with an arbitrary register
5969          (and currently gen_lowpart might crash for a SUBREG).  */
5970
5971       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5972         return x;
5973
5974       /* Reject MODEs that aren't scalar integers because turning vector
5975          or complex modes into shifts causes problems.  */
5976
5977       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5978         return x;
5979
5980       len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5981       /* If the inner object has VOIDmode (the only way this can happen
5982          is if it is an ASM_OPERANDS), we can't do anything since we don't
5983          know how much masking to do.  */
5984       if (len == 0)
5985         return x;
5986
5987       break;
5988
5989     case ZERO_EXTRACT:
5990       unsignedp = 1;
5991
5992       /* ... fall through ...  */
5993
5994     case SIGN_EXTRACT:
5995       /* If the operand is a CLOBBER, just return it.  */
5996       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5997         return XEXP (x, 0);
5998
5999       if (GET_CODE (XEXP (x, 1)) != CONST_INT
6000           || GET_CODE (XEXP (x, 2)) != CONST_INT
6001           || GET_MODE (XEXP (x, 0)) == VOIDmode)
6002         return x;
6003
6004       /* Reject MODEs that aren't scalar integers because turning vector
6005          or complex modes into shifts causes problems.  */
6006
6007       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6008         return x;
6009
6010       len = INTVAL (XEXP (x, 1));
6011       pos = INTVAL (XEXP (x, 2));
6012
6013       /* This should stay within the object being extracted, fail otherwise.  */
6014       if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
6015         return x;
6016
6017       if (BITS_BIG_ENDIAN)
6018         pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
6019
6020       break;
6021
6022     default:
6023       return x;
6024     }
6025   /* Convert sign extension to zero extension, if we know that the high
6026      bit is not set, as this is easier to optimize.  It will be converted
6027      back to cheaper alternative in make_extraction.  */
6028   if (GET_CODE (x) == SIGN_EXTEND
6029       && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6030           && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6031                 & ~(((unsigned HOST_WIDE_INT)
6032                       GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6033                      >> 1))
6034                == 0)))
6035     {
6036       rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6037       rtx temp2 = expand_compound_operation (temp);
6038
6039       /* Make sure this is a profitable operation.  */
6040       if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
6041        return temp2;
6042       else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
6043        return temp;
6044       else
6045        return x;
6046     }
6047
6048   /* We can optimize some special cases of ZERO_EXTEND.  */
6049   if (GET_CODE (x) == ZERO_EXTEND)
6050     {
6051       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6052          know that the last value didn't have any inappropriate bits
6053          set.  */
6054       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6055           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6056           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6057           && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6058               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6059         return XEXP (XEXP (x, 0), 0);
6060
6061       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
6062       if (GET_CODE (XEXP (x, 0)) == SUBREG
6063           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6064           && subreg_lowpart_p (XEXP (x, 0))
6065           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6066           && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6067               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6068         return SUBREG_REG (XEXP (x, 0));
6069
6070       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6071          is a comparison and STORE_FLAG_VALUE permits.  This is like
6072          the first case, but it works even when GET_MODE (x) is larger
6073          than HOST_WIDE_INT.  */
6074       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6075           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6076           && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6077           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6078               <= HOST_BITS_PER_WIDE_INT)
6079           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
6080               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6081         return XEXP (XEXP (x, 0), 0);
6082
6083       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
6084       if (GET_CODE (XEXP (x, 0)) == SUBREG
6085           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6086           && subreg_lowpart_p (XEXP (x, 0))
6087           && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6088           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6089               <= HOST_BITS_PER_WIDE_INT)
6090           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
6091               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6092         return SUBREG_REG (XEXP (x, 0));
6093
6094     }
6095
6096   /* If we reach here, we want to return a pair of shifts.  The inner
6097      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
6098      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
6099      logical depending on the value of UNSIGNEDP.
6100
6101      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6102      converted into an AND of a shift.
6103
6104      We must check for the case where the left shift would have a negative
6105      count.  This can happen in a case like (x >> 31) & 255 on machines
6106      that can't shift by a constant.  On those machines, we would first
6107      combine the shift with the AND to produce a variable-position
6108      extraction.  Then the constant of 31 would be substituted in to produce
6109      a such a position.  */
6110
6111   modewidth = GET_MODE_BITSIZE (GET_MODE (x));
6112   if (modewidth + len >= pos)
6113     {
6114       enum machine_mode mode = GET_MODE (x);
6115       tem = gen_lowpart (mode, XEXP (x, 0));
6116       if (!tem || GET_CODE (tem) == CLOBBER)
6117         return x;
6118       tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6119                                   tem, modewidth - pos - len);
6120       tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6121                                   mode, tem, modewidth - len);
6122     }
6123   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6124     tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6125                                   simplify_shift_const (NULL_RTX, LSHIFTRT,
6126                                                         GET_MODE (x),
6127                                                         XEXP (x, 0), pos),
6128                                   ((HOST_WIDE_INT) 1 << len) - 1);
6129   else
6130     /* Any other cases we can't handle.  */
6131     return x;
6132
6133   /* If we couldn't do this for some reason, return the original
6134      expression.  */
6135   if (GET_CODE (tem) == CLOBBER)
6136     return x;
6137
6138   return tem;
6139 }
6140 \f
6141 /* X is a SET which contains an assignment of one object into
6142    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6143    or certain SUBREGS). If possible, convert it into a series of
6144    logical operations.
6145
6146    We half-heartedly support variable positions, but do not at all
6147    support variable lengths.  */
6148
6149 static rtx
6150 expand_field_assignment (rtx x)
6151 {
6152   rtx inner;
6153   rtx pos;                      /* Always counts from low bit.  */
6154   int len;
6155   rtx mask, cleared, masked;
6156   enum machine_mode compute_mode;
6157
6158   /* Loop until we find something we can't simplify.  */
6159   while (1)
6160     {
6161       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6162           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6163         {
6164           inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6165           len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
6166           pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6167         }
6168       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6169                && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
6170         {
6171           inner = XEXP (SET_DEST (x), 0);
6172           len = INTVAL (XEXP (SET_DEST (x), 1));
6173           pos = XEXP (SET_DEST (x), 2);
6174
6175           /* A constant position should stay within the width of INNER.  */
6176           if (GET_CODE (pos) == CONST_INT
6177               && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
6178             break;
6179
6180           if (BITS_BIG_ENDIAN)
6181             {
6182               if (GET_CODE (pos) == CONST_INT)
6183                 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
6184                                - INTVAL (pos));
6185               else if (GET_CODE (pos) == MINUS
6186                        && GET_CODE (XEXP (pos, 1)) == CONST_INT
6187                        && (INTVAL (XEXP (pos, 1))
6188                            == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
6189                 /* If position is ADJUST - X, new position is X.  */
6190                 pos = XEXP (pos, 0);
6191               else
6192                 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6193                                            GEN_INT (GET_MODE_BITSIZE (
6194                                                     GET_MODE (inner))
6195                                                     - len),
6196                                            pos);
6197             }
6198         }
6199
6200       /* A SUBREG between two modes that occupy the same numbers of words
6201          can be done by moving the SUBREG to the source.  */
6202       else if (GET_CODE (SET_DEST (x)) == SUBREG
6203                /* We need SUBREGs to compute nonzero_bits properly.  */
6204                && nonzero_sign_valid
6205                && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6206                      + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6207                    == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6208                         + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6209         {
6210           x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6211                            gen_lowpart
6212                            (GET_MODE (SUBREG_REG (SET_DEST (x))),
6213                             SET_SRC (x)));
6214           continue;
6215         }
6216       else
6217         break;
6218
6219       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6220         inner = SUBREG_REG (inner);
6221
6222       compute_mode = GET_MODE (inner);
6223
6224       /* Don't attempt bitwise arithmetic on non scalar integer modes.  */
6225       if (! SCALAR_INT_MODE_P (compute_mode))
6226         {
6227           enum machine_mode imode;
6228
6229           /* Don't do anything for vector or complex integral types.  */
6230           if (! FLOAT_MODE_P (compute_mode))
6231             break;
6232
6233           /* Try to find an integral mode to pun with.  */
6234           imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6235           if (imode == BLKmode)
6236             break;
6237
6238           compute_mode = imode;
6239           inner = gen_lowpart (imode, inner);
6240         }
6241
6242       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
6243       if (len >= HOST_BITS_PER_WIDE_INT)
6244         break;
6245
6246       /* Now compute the equivalent expression.  Make a copy of INNER
6247          for the SET_DEST in case it is a MEM into which we will substitute;
6248          we don't want shared RTL in that case.  */
6249       mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
6250       cleared = simplify_gen_binary (AND, compute_mode,
6251                                      simplify_gen_unary (NOT, compute_mode,
6252                                        simplify_gen_binary (ASHIFT,
6253                                                             compute_mode,
6254                                                             mask, pos),
6255                                        compute_mode),
6256                                      inner);
6257       masked = simplify_gen_binary (ASHIFT, compute_mode,
6258                                     simplify_gen_binary (
6259                                       AND, compute_mode,
6260                                       gen_lowpart (compute_mode, SET_SRC (x)),
6261                                       mask),
6262                                     pos);
6263
6264       x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
6265                        simplify_gen_binary (IOR, compute_mode,
6266                                             cleared, masked));
6267     }
6268
6269   return x;
6270 }
6271 \f
6272 /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
6273    it is an RTX that represents a variable starting position; otherwise,
6274    POS is the (constant) starting bit position (counted from the LSB).
6275
6276    UNSIGNEDP is nonzero for an unsigned reference and zero for a
6277    signed reference.
6278
6279    IN_DEST is nonzero if this is a reference in the destination of a
6280    SET.  This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If nonzero,
6281    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6282    be used.
6283
6284    IN_COMPARE is nonzero if we are in a COMPARE.  This means that a
6285    ZERO_EXTRACT should be built even for bits starting at bit 0.
6286
6287    MODE is the desired mode of the result (if IN_DEST == 0).
6288
6289    The result is an RTX for the extraction or NULL_RTX if the target
6290    can't handle it.  */
6291
6292 static rtx
6293 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6294                  rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6295                  int in_dest, int in_compare)
6296 {
6297   /* This mode describes the size of the storage area
6298      to fetch the overall value from.  Within that, we
6299      ignore the POS lowest bits, etc.  */
6300   enum machine_mode is_mode = GET_MODE (inner);
6301   enum machine_mode inner_mode;
6302   enum machine_mode wanted_inner_mode;
6303   enum machine_mode wanted_inner_reg_mode = word_mode;
6304   enum machine_mode pos_mode = word_mode;
6305   enum machine_mode extraction_mode = word_mode;
6306   enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6307   rtx new = 0;
6308   rtx orig_pos_rtx = pos_rtx;
6309   HOST_WIDE_INT orig_pos;
6310
6311   if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6312     {
6313       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6314          consider just the QI as the memory to extract from.
6315          The subreg adds or removes high bits; its mode is
6316          irrelevant to the meaning of this extraction,
6317          since POS and LEN count from the lsb.  */
6318       if (MEM_P (SUBREG_REG (inner)))
6319         is_mode = GET_MODE (SUBREG_REG (inner));
6320       inner = SUBREG_REG (inner);
6321     }
6322   else if (GET_CODE (inner) == ASHIFT
6323            && GET_CODE (XEXP (inner, 1)) == CONST_INT
6324            && pos_rtx == 0 && pos == 0
6325            && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6326     {
6327       /* We're extracting the least significant bits of an rtx
6328          (ashift X (const_int C)), where LEN > C.  Extract the
6329          least significant (LEN - C) bits of X, giving an rtx
6330          whose mode is MODE, then shift it left C times.  */
6331       new = make_extraction (mode, XEXP (inner, 0),
6332                              0, 0, len - INTVAL (XEXP (inner, 1)),
6333                              unsignedp, in_dest, in_compare);
6334       if (new != 0)
6335         return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
6336     }
6337
6338   inner_mode = GET_MODE (inner);
6339
6340   if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
6341     pos = INTVAL (pos_rtx), pos_rtx = 0;
6342
6343   /* See if this can be done without an extraction.  We never can if the
6344      width of the field is not the same as that of some integer mode. For
6345      registers, we can only avoid the extraction if the position is at the
6346      low-order bit and this is either not in the destination or we have the
6347      appropriate STRICT_LOW_PART operation available.
6348
6349      For MEM, we can avoid an extract if the field starts on an appropriate
6350      boundary and we can change the mode of the memory reference.  */
6351
6352   if (tmode != BLKmode
6353       && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6354            && !MEM_P (inner)
6355            && (inner_mode == tmode
6356                || !REG_P (inner)
6357                || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
6358                                          GET_MODE_BITSIZE (inner_mode))
6359                || reg_truncated_to_mode (tmode, inner))
6360            && (! in_dest
6361                || (REG_P (inner)
6362                    && have_insn_for (STRICT_LOW_PART, tmode))))
6363           || (MEM_P (inner) && pos_rtx == 0
6364               && (pos
6365                   % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6366                      : BITS_PER_UNIT)) == 0
6367               /* We can't do this if we are widening INNER_MODE (it
6368                  may not be aligned, for one thing).  */
6369               && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6370               && (inner_mode == tmode
6371                   || (! mode_dependent_address_p (XEXP (inner, 0))
6372                       && ! MEM_VOLATILE_P (inner))))))
6373     {
6374       /* If INNER is a MEM, make a new MEM that encompasses just the desired
6375          field.  If the original and current mode are the same, we need not
6376          adjust the offset.  Otherwise, we do if bytes big endian.
6377
6378          If INNER is not a MEM, get a piece consisting of just the field
6379          of interest (in this case POS % BITS_PER_WORD must be 0).  */
6380
6381       if (MEM_P (inner))
6382         {
6383           HOST_WIDE_INT offset;
6384
6385           /* POS counts from lsb, but make OFFSET count in memory order.  */
6386           if (BYTES_BIG_ENDIAN)
6387             offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6388           else
6389             offset = pos / BITS_PER_UNIT;
6390
6391           new = adjust_address_nv (inner, tmode, offset);
6392         }
6393       else if (REG_P (inner))
6394         {
6395           if (tmode != inner_mode)
6396             {
6397               /* We can't call gen_lowpart in a DEST since we
6398                  always want a SUBREG (see below) and it would sometimes
6399                  return a new hard register.  */
6400               if (pos || in_dest)
6401                 {
6402                   HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6403
6404                   if (WORDS_BIG_ENDIAN
6405                       && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6406                     final_word = ((GET_MODE_SIZE (inner_mode)
6407                                    - GET_MODE_SIZE (tmode))
6408                                   / UNITS_PER_WORD) - final_word;
6409
6410                   final_word *= UNITS_PER_WORD;
6411                   if (BYTES_BIG_ENDIAN &&
6412                       GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6413                     final_word += (GET_MODE_SIZE (inner_mode)
6414                                    - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6415
6416                   /* Avoid creating invalid subregs, for example when
6417                      simplifying (x>>32)&255.  */
6418                   if (!validate_subreg (tmode, inner_mode, inner, final_word))
6419                     return NULL_RTX;
6420
6421                   new = gen_rtx_SUBREG (tmode, inner, final_word);
6422                 }
6423               else
6424                 new = gen_lowpart (tmode, inner);
6425             }
6426           else
6427             new = inner;
6428         }
6429       else
6430         new = force_to_mode (inner, tmode,
6431                              len >= HOST_BITS_PER_WIDE_INT
6432                              ? ~(unsigned HOST_WIDE_INT) 0
6433                              : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6434                              0);
6435
6436       /* If this extraction is going into the destination of a SET,
6437          make a STRICT_LOW_PART unless we made a MEM.  */
6438
6439       if (in_dest)
6440         return (MEM_P (new) ? new
6441                 : (GET_CODE (new) != SUBREG
6442                    ? gen_rtx_CLOBBER (tmode, const0_rtx)
6443                    : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
6444
6445       if (mode == tmode)
6446         return new;
6447
6448       if (GET_CODE (new) == CONST_INT)
6449         return gen_int_mode (INTVAL (new), mode);
6450
6451       /* If we know that no extraneous bits are set, and that the high
6452          bit is not set, convert the extraction to the cheaper of
6453          sign and zero extension, that are equivalent in these cases.  */
6454       if (flag_expensive_optimizations
6455           && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6456               && ((nonzero_bits (new, tmode)
6457                    & ~(((unsigned HOST_WIDE_INT)
6458                         GET_MODE_MASK (tmode))
6459                        >> 1))
6460                   == 0)))
6461         {
6462           rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6463           rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6464
6465           /* Prefer ZERO_EXTENSION, since it gives more information to
6466              backends.  */
6467           if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6468             return temp;
6469           return temp1;
6470         }
6471
6472       /* Otherwise, sign- or zero-extend unless we already are in the
6473          proper mode.  */
6474
6475       return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6476                              mode, new));
6477     }
6478
6479   /* Unless this is a COMPARE or we have a funny memory reference,
6480      don't do anything with zero-extending field extracts starting at
6481      the low-order bit since they are simple AND operations.  */
6482   if (pos_rtx == 0 && pos == 0 && ! in_dest
6483       && ! in_compare && unsignedp)
6484     return 0;
6485
6486   /* Unless INNER is not MEM, reject this if we would be spanning bytes or
6487      if the position is not a constant and the length is not 1.  In all
6488      other cases, we would only be going outside our object in cases when
6489      an original shift would have been undefined.  */
6490   if (MEM_P (inner)
6491       && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6492           || (pos_rtx != 0 && len != 1)))
6493     return 0;
6494
6495   /* Get the mode to use should INNER not be a MEM, the mode for the position,
6496      and the mode for the result.  */
6497   if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6498     {
6499       wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6500       pos_mode = mode_for_extraction (EP_insv, 2);
6501       extraction_mode = mode_for_extraction (EP_insv, 3);
6502     }
6503
6504   if (! in_dest && unsignedp
6505       && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6506     {
6507       wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6508       pos_mode = mode_for_extraction (EP_extzv, 3);
6509       extraction_mode = mode_for_extraction (EP_extzv, 0);
6510     }
6511
6512   if (! in_dest && ! unsignedp
6513       && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6514     {
6515       wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6516       pos_mode = mode_for_extraction (EP_extv, 3);
6517       extraction_mode = mode_for_extraction (EP_extv, 0);
6518     }
6519
6520   /* Never narrow an object, since that might not be safe.  */
6521
6522   if (mode != VOIDmode
6523       && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6524     extraction_mode = mode;
6525
6526   if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6527       && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6528     pos_mode = GET_MODE (pos_rtx);
6529
6530   /* If this is not from memory, the desired mode is the preferred mode
6531      for an extraction pattern's first input operand, or word_mode if there
6532      is none.  */
6533   if (!MEM_P (inner))
6534     wanted_inner_mode = wanted_inner_reg_mode;
6535   else
6536     {
6537       /* Be careful not to go beyond the extracted object and maintain the
6538          natural alignment of the memory.  */
6539       wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
6540       while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
6541              > GET_MODE_BITSIZE (wanted_inner_mode))
6542         {
6543           wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
6544           gcc_assert (wanted_inner_mode != VOIDmode);
6545         }
6546
6547       /* If we have to change the mode of memory and cannot, the desired mode
6548          is EXTRACTION_MODE.  */
6549       if (inner_mode != wanted_inner_mode
6550           && (mode_dependent_address_p (XEXP (inner, 0))
6551               || MEM_VOLATILE_P (inner)
6552               || pos_rtx))
6553         wanted_inner_mode = extraction_mode;
6554     }
6555
6556   orig_pos = pos;
6557
6558   if (BITS_BIG_ENDIAN)
6559     {
6560       /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6561          BITS_BIG_ENDIAN style.  If position is constant, compute new
6562          position.  Otherwise, build subtraction.
6563          Note that POS is relative to the mode of the original argument.
6564          If it's a MEM we need to recompute POS relative to that.
6565          However, if we're extracting from (or inserting into) a register,
6566          we want to recompute POS relative to wanted_inner_mode.  */
6567       int width = (MEM_P (inner)
6568                    ? GET_MODE_BITSIZE (is_mode)
6569                    : GET_MODE_BITSIZE (wanted_inner_mode));
6570
6571       if (pos_rtx == 0)
6572         pos = width - len - pos;
6573       else
6574         pos_rtx
6575           = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6576       /* POS may be less than 0 now, but we check for that below.
6577          Note that it can only be less than 0 if !MEM_P (inner).  */
6578     }
6579
6580   /* If INNER has a wider mode, and this is a constant extraction, try to
6581      make it smaller and adjust the byte to point to the byte containing
6582      the value.  */
6583   if (wanted_inner_mode != VOIDmode
6584       && inner_mode != wanted_inner_mode
6585       && ! pos_rtx
6586       && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6587       && MEM_P (inner)
6588       && ! mode_dependent_address_p (XEXP (inner, 0))
6589       && ! MEM_VOLATILE_P (inner))
6590     {
6591       int offset = 0;
6592
6593       /* The computations below will be correct if the machine is big
6594          endian in both bits and bytes or little endian in bits and bytes.
6595          If it is mixed, we must adjust.  */
6596
6597       /* If bytes are big endian and we had a paradoxical SUBREG, we must
6598          adjust OFFSET to compensate.  */
6599       if (BYTES_BIG_ENDIAN
6600           && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6601         offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6602
6603       /* We can now move to the desired byte.  */
6604       offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
6605                 * GET_MODE_SIZE (wanted_inner_mode);
6606       pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6607
6608       if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6609           && is_mode != wanted_inner_mode)
6610         offset = (GET_MODE_SIZE (is_mode)
6611                   - GET_MODE_SIZE (wanted_inner_mode) - offset);
6612
6613       inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6614     }
6615
6616   /* If INNER is not memory, we can always get it into the proper mode.  If we
6617      are changing its mode, POS must be a constant and smaller than the size
6618      of the new mode.  */
6619   else if (!MEM_P (inner))
6620     {
6621       if (GET_MODE (inner) != wanted_inner_mode
6622           && (pos_rtx != 0
6623               || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6624         return 0;
6625
6626       if (orig_pos < 0)
6627         return 0;
6628
6629       inner = force_to_mode (inner, wanted_inner_mode,
6630                              pos_rtx
6631                              || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6632                              ? ~(unsigned HOST_WIDE_INT) 0
6633                              : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6634                                 << orig_pos),
6635                              0);
6636     }
6637
6638   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
6639      have to zero extend.  Otherwise, we can just use a SUBREG.  */
6640   if (pos_rtx != 0
6641       && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6642     {
6643       rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6644
6645       /* If we know that no extraneous bits are set, and that the high
6646          bit is not set, convert extraction to cheaper one - either
6647          SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6648          cases.  */
6649       if (flag_expensive_optimizations
6650           && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6651               && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6652                    & ~(((unsigned HOST_WIDE_INT)
6653                         GET_MODE_MASK (GET_MODE (pos_rtx)))
6654                        >> 1))
6655                   == 0)))
6656         {
6657           rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6658
6659           /* Prefer ZERO_EXTENSION, since it gives more information to
6660              backends.  */
6661           if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6662             temp = temp1;
6663         }
6664       pos_rtx = temp;
6665     }
6666   else if (pos_rtx != 0
6667            && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6668     pos_rtx = gen_lowpart (pos_mode, pos_rtx);
6669
6670   /* Make POS_RTX unless we already have it and it is correct.  If we don't
6671      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6672      be a CONST_INT.  */
6673   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6674     pos_rtx = orig_pos_rtx;
6675
6676   else if (pos_rtx == 0)
6677     pos_rtx = GEN_INT (pos);
6678
6679   /* Make the required operation.  See if we can use existing rtx.  */
6680   new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6681                          extraction_mode, inner, GEN_INT (len), pos_rtx);
6682   if (! in_dest)
6683     new = gen_lowpart (mode, new);
6684
6685   return new;
6686 }
6687 \f
6688 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6689    with any other operations in X.  Return X without that shift if so.  */
6690
6691 static rtx
6692 extract_left_shift (rtx x, int count)
6693 {
6694   enum rtx_code code = GET_CODE (x);
6695   enum machine_mode mode = GET_MODE (x);
6696   rtx tem;
6697
6698   switch (code)
6699     {
6700     case ASHIFT:
6701       /* This is the shift itself.  If it is wide enough, we will return
6702          either the value being shifted if the shift count is equal to
6703          COUNT or a shift for the difference.  */
6704       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6705           && INTVAL (XEXP (x, 1)) >= count)
6706         return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6707                                      INTVAL (XEXP (x, 1)) - count);
6708       break;
6709
6710     case NEG:  case NOT:
6711       if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6712         return simplify_gen_unary (code, mode, tem, mode);
6713
6714       break;
6715
6716     case PLUS:  case IOR:  case XOR:  case AND:
6717       /* If we can safely shift this constant and we find the inner shift,
6718          make a new operation.  */
6719       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6720           && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6721           && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6722         return simplify_gen_binary (code, mode, tem,
6723                                     GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6724
6725       break;
6726
6727     default:
6728       break;
6729     }
6730
6731   return 0;
6732 }
6733 \f
6734 /* Look at the expression rooted at X.  Look for expressions
6735    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6736    Form these expressions.
6737
6738    Return the new rtx, usually just X.
6739
6740    Also, for machines like the VAX that don't have logical shift insns,
6741    try to convert logical to arithmetic shift operations in cases where
6742    they are equivalent.  This undoes the canonicalizations to logical
6743    shifts done elsewhere.
6744
6745    We try, as much as possible, to re-use rtl expressions to save memory.
6746
6747    IN_CODE says what kind of expression we are processing.  Normally, it is
6748    SET.  In a memory address (inside a MEM, PLUS or minus, the latter two
6749    being kludges), it is MEM.  When processing the arguments of a comparison
6750    or a COMPARE against zero, it is COMPARE.  */
6751
6752 static rtx
6753 make_compound_operation (rtx x, enum rtx_code in_code)
6754 {
6755   enum rtx_code code = GET_CODE (x);
6756   enum machine_mode mode = GET_MODE (x);
6757   int mode_width = GET_MODE_BITSIZE (mode);
6758   rtx rhs, lhs;
6759   enum rtx_code next_code;
6760   int i;
6761   rtx new = 0;
6762   rtx tem;
6763   const char *fmt;
6764
6765   /* Select the code to be used in recursive calls.  Once we are inside an
6766      address, we stay there.  If we have a comparison, set to COMPARE,
6767      but once inside, go back to our default of SET.  */
6768
6769   next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6770                : ((code == COMPARE || COMPARISON_P (x))
6771                   && XEXP (x, 1) == const0_rtx) ? COMPARE
6772                : in_code == COMPARE ? SET : in_code);
6773
6774   /* Process depending on the code of this operation.  If NEW is set
6775      nonzero, it will be returned.  */
6776
6777   switch (code)
6778     {
6779     case ASHIFT:
6780       /* Convert shifts by constants into multiplications if inside
6781          an address.  */
6782       if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6783           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6784           && INTVAL (XEXP (x, 1)) >= 0)
6785         {
6786           new = make_compound_operation (XEXP (x, 0), next_code);
6787           new = gen_rtx_MULT (mode, new,
6788                               GEN_INT ((HOST_WIDE_INT) 1
6789                                        << INTVAL (XEXP (x, 1))));
6790         }
6791       break;
6792
6793     case AND:
6794       /* If the second operand is not a constant, we can't do anything
6795          with it.  */
6796       if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6797         break;
6798
6799       /* If the constant is a power of two minus one and the first operand
6800          is a logical right shift, make an extraction.  */
6801       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6802           && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6803         {
6804           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6805           new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6806                                  0, in_code == COMPARE);
6807         }
6808
6809       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
6810       else if (GET_CODE (XEXP (x, 0)) == SUBREG
6811                && subreg_lowpart_p (XEXP (x, 0))
6812                && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6813                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6814         {
6815           new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6816                                          next_code);
6817           new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6818                                  XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6819                                  0, in_code == COMPARE);
6820         }
6821       /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)).  */
6822       else if ((GET_CODE (XEXP (x, 0)) == XOR
6823                 || GET_CODE (XEXP (x, 0)) == IOR)
6824                && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6825                && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6826                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6827         {
6828           /* Apply the distributive law, and then try to make extractions.  */
6829           new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6830                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6831                                              XEXP (x, 1)),
6832                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6833                                              XEXP (x, 1)));
6834           new = make_compound_operation (new, in_code);
6835         }
6836
6837       /* If we are have (and (rotate X C) M) and C is larger than the number
6838          of bits in M, this is an extraction.  */
6839
6840       else if (GET_CODE (XEXP (x, 0)) == ROTATE
6841                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6842                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6843                && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6844         {
6845           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6846           new = make_extraction (mode, new,
6847                                  (GET_MODE_BITSIZE (mode)
6848                                   - INTVAL (XEXP (XEXP (x, 0), 1))),
6849                                  NULL_RTX, i, 1, 0, in_code == COMPARE);
6850         }
6851
6852       /* On machines without logical shifts, if the operand of the AND is
6853          a logical shift and our mask turns off all the propagated sign
6854          bits, we can replace the logical shift with an arithmetic shift.  */
6855       else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6856                && !have_insn_for (LSHIFTRT, mode)
6857                && have_insn_for (ASHIFTRT, mode)
6858                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6859                && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6860                && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6861                && mode_width <= HOST_BITS_PER_WIDE_INT)
6862         {
6863           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6864
6865           mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6866           if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6867             SUBST (XEXP (x, 0),
6868                    gen_rtx_ASHIFTRT (mode,
6869                                      make_compound_operation
6870                                      (XEXP (XEXP (x, 0), 0), next_code),
6871                                      XEXP (XEXP (x, 0), 1)));
6872         }
6873
6874       /* If the constant is one less than a power of two, this might be
6875          representable by an extraction even if no shift is present.
6876          If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6877          we are in a COMPARE.  */
6878       else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6879         new = make_extraction (mode,
6880                                make_compound_operation (XEXP (x, 0),
6881                                                         next_code),
6882                                0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6883
6884       /* If we are in a comparison and this is an AND with a power of two,
6885          convert this into the appropriate bit extract.  */
6886       else if (in_code == COMPARE
6887                && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6888         new = make_extraction (mode,
6889                                make_compound_operation (XEXP (x, 0),
6890                                                         next_code),
6891                                i, NULL_RTX, 1, 1, 0, 1);
6892
6893       break;
6894
6895     case LSHIFTRT:
6896       /* If the sign bit is known to be zero, replace this with an
6897          arithmetic shift.  */
6898       if (have_insn_for (ASHIFTRT, mode)
6899           && ! have_insn_for (LSHIFTRT, mode)
6900           && mode_width <= HOST_BITS_PER_WIDE_INT
6901           && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6902         {
6903           new = gen_rtx_ASHIFTRT (mode,
6904                                   make_compound_operation (XEXP (x, 0),
6905                                                            next_code),
6906                                   XEXP (x, 1));
6907           break;
6908         }
6909
6910       /* ... fall through ...  */
6911
6912     case ASHIFTRT:
6913       lhs = XEXP (x, 0);
6914       rhs = XEXP (x, 1);
6915
6916       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6917          this is a SIGN_EXTRACT.  */
6918       if (GET_CODE (rhs) == CONST_INT
6919           && GET_CODE (lhs) == ASHIFT
6920           && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6921           && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6922         {
6923           new = make_compound_operation (XEXP (lhs, 0), next_code);
6924           new = make_extraction (mode, new,
6925                                  INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6926                                  NULL_RTX, mode_width - INTVAL (rhs),
6927                                  code == LSHIFTRT, 0, in_code == COMPARE);
6928           break;
6929         }
6930
6931       /* See if we have operations between an ASHIFTRT and an ASHIFT.
6932          If so, try to merge the shifts into a SIGN_EXTEND.  We could
6933          also do this for some cases of SIGN_EXTRACT, but it doesn't
6934          seem worth the effort; the case checked for occurs on Alpha.  */
6935
6936       if (!OBJECT_P (lhs)
6937           && ! (GET_CODE (lhs) == SUBREG
6938                 && (OBJECT_P (SUBREG_REG (lhs))))
6939           && GET_CODE (rhs) == CONST_INT
6940           && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6941           && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6942         new = make_extraction (mode, make_compound_operation (new, next_code),
6943                                0, NULL_RTX, mode_width - INTVAL (rhs),
6944                                code == LSHIFTRT, 0, in_code == COMPARE);
6945
6946       break;
6947
6948     case SUBREG:
6949       /* Call ourselves recursively on the inner expression.  If we are
6950          narrowing the object and it has a different RTL code from
6951          what it originally did, do this SUBREG as a force_to_mode.  */
6952
6953       tem = make_compound_operation (SUBREG_REG (x), in_code);
6954
6955       {
6956         rtx simplified;
6957         simplified = simplify_subreg (GET_MODE (x), tem, GET_MODE (tem),
6958                                       SUBREG_BYTE (x));
6959
6960         if (simplified)
6961           tem = simplified;
6962
6963         if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6964             && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6965             && subreg_lowpart_p (x))
6966           {
6967             rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
6968                                        0);
6969
6970             /* If we have something other than a SUBREG, we might have
6971                done an expansion, so rerun ourselves.  */
6972             if (GET_CODE (newer) != SUBREG)
6973               newer = make_compound_operation (newer, in_code);
6974
6975             return newer;
6976           }
6977
6978         if (simplified)
6979           return tem;
6980       }
6981       break;
6982
6983     default:
6984       break;
6985     }
6986
6987   if (new)
6988     {
6989       x = gen_lowpart (mode, new);
6990       code = GET_CODE (x);
6991     }
6992
6993   /* Now recursively process each operand of this operation.  */
6994   fmt = GET_RTX_FORMAT (code);
6995   for (i = 0; i < GET_RTX_LENGTH (code); i++)
6996     if (fmt[i] == 'e')
6997       {
6998         new = make_compound_operation (XEXP (x, i), next_code);
6999         SUBST (XEXP (x, i), new);
7000       }
7001
7002   /* If this is a commutative operation, the changes to the operands
7003      may have made it noncanonical.  */
7004   if (COMMUTATIVE_ARITH_P (x)
7005       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7006     {
7007       tem = XEXP (x, 0);
7008       SUBST (XEXP (x, 0), XEXP (x, 1));
7009       SUBST (XEXP (x, 1), tem);
7010     }
7011
7012   return x;
7013 }
7014 \f
7015 /* Given M see if it is a value that would select a field of bits
7016    within an item, but not the entire word.  Return -1 if not.
7017    Otherwise, return the starting position of the field, where 0 is the
7018    low-order bit.
7019
7020    *PLEN is set to the length of the field.  */
7021
7022 static int
7023 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7024 {
7025   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
7026   int pos = exact_log2 (m & -m);
7027   int len = 0;
7028
7029   if (pos >= 0)
7030     /* Now shift off the low-order zero bits and see if we have a
7031        power of two minus 1.  */
7032     len = exact_log2 ((m >> pos) + 1);
7033
7034   if (len <= 0)
7035     pos = -1;
7036
7037   *plen = len;
7038   return pos;
7039 }
7040 \f
7041 /* If X refers to a register that equals REG in value, replace these
7042    references with REG.  */
7043 static rtx
7044 canon_reg_for_combine (rtx x, rtx reg)
7045 {
7046   rtx op0, op1, op2;
7047   const char *fmt;
7048   int i;
7049   bool copied;
7050
7051   enum rtx_code code = GET_CODE (x);
7052   switch (GET_RTX_CLASS (code))
7053     {
7054     case RTX_UNARY:
7055       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7056       if (op0 != XEXP (x, 0))
7057         return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7058                                    GET_MODE (reg));
7059       break;
7060
7061     case RTX_BIN_ARITH:
7062     case RTX_COMM_ARITH:
7063       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7064       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7065       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7066         return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7067       break;
7068
7069     case RTX_COMPARE:
7070     case RTX_COMM_COMPARE:
7071       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7072       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7073       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7074         return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7075                                         GET_MODE (op0), op0, op1);
7076       break;
7077
7078     case RTX_TERNARY:
7079     case RTX_BITFIELD_OPS:
7080       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7081       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7082       op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7083       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7084         return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7085                                      GET_MODE (op0), op0, op1, op2);
7086
7087     case RTX_OBJ:
7088       if (REG_P (x))
7089         {
7090           if (rtx_equal_p (get_last_value (reg), x)
7091               || rtx_equal_p (reg, get_last_value (x)))
7092             return reg;
7093           else
7094             break;
7095         }
7096
7097       /* fall through */
7098
7099     default:
7100       fmt = GET_RTX_FORMAT (code);
7101       copied = false;
7102       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7103         if (fmt[i] == 'e')
7104           {
7105             rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7106             if (op != XEXP (x, i))
7107               {
7108                 if (!copied)
7109                   {
7110                     copied = true;
7111                     x = copy_rtx (x);
7112                   }
7113                 XEXP (x, i) = op;
7114               }
7115           }
7116         else if (fmt[i] == 'E')
7117           {
7118             int j;
7119             for (j = 0; j < XVECLEN (x, i); j++)
7120               {
7121                 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
7122                 if (op != XVECEXP (x, i, j))
7123                   {
7124                     if (!copied)
7125                       {
7126                         copied = true;
7127                         x = copy_rtx (x);
7128                       }
7129                     XVECEXP (x, i, j) = op;
7130                   }
7131               }
7132           }
7133
7134       break;
7135     }
7136
7137   return x;
7138 }
7139
7140 /* Return X converted to MODE.  If the value is already truncated to
7141    MODE we can just return a subreg even though in the general case we
7142    would need an explicit truncation.  */
7143
7144 static rtx
7145 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
7146 {
7147   if (GET_MODE_SIZE (GET_MODE (x)) <= GET_MODE_SIZE (mode)
7148       || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
7149                                 GET_MODE_BITSIZE (GET_MODE (x)))
7150       || (REG_P (x) && reg_truncated_to_mode (mode, x)))
7151     return gen_lowpart (mode, x);
7152   else
7153     return simplify_gen_unary (TRUNCATE, mode, x, GET_MODE (x));
7154 }
7155
7156 /* See if X can be simplified knowing that we will only refer to it in
7157    MODE and will only refer to those bits that are nonzero in MASK.
7158    If other bits are being computed or if masking operations are done
7159    that select a superset of the bits in MASK, they can sometimes be
7160    ignored.
7161
7162    Return a possibly simplified expression, but always convert X to
7163    MODE.  If X is a CONST_INT, AND the CONST_INT with MASK.
7164
7165    If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
7166    are all off in X.  This is used when X will be complemented, by either
7167    NOT, NEG, or XOR.  */
7168
7169 static rtx
7170 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
7171                int just_select)
7172 {
7173   enum rtx_code code = GET_CODE (x);
7174   int next_select = just_select || code == XOR || code == NOT || code == NEG;
7175   enum machine_mode op_mode;
7176   unsigned HOST_WIDE_INT fuller_mask, nonzero;
7177   rtx op0, op1, temp;
7178
7179   /* If this is a CALL or ASM_OPERANDS, don't do anything.  Some of the
7180      code below will do the wrong thing since the mode of such an
7181      expression is VOIDmode.
7182
7183      Also do nothing if X is a CLOBBER; this can happen if X was
7184      the return value from a call to gen_lowpart.  */
7185   if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
7186     return x;
7187
7188   /* We want to perform the operation is its present mode unless we know
7189      that the operation is valid in MODE, in which case we do the operation
7190      in MODE.  */
7191   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
7192               && have_insn_for (code, mode))
7193              ? mode : GET_MODE (x));
7194
7195   /* It is not valid to do a right-shift in a narrower mode
7196      than the one it came in with.  */
7197   if ((code == LSHIFTRT || code == ASHIFTRT)
7198       && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
7199     op_mode = GET_MODE (x);
7200
7201   /* Truncate MASK to fit OP_MODE.  */
7202   if (op_mode)
7203     mask &= GET_MODE_MASK (op_mode);
7204
7205   /* When we have an arithmetic operation, or a shift whose count we
7206      do not know, we need to assume that all bits up to the highest-order
7207      bit in MASK will be needed.  This is how we form such a mask.  */
7208   if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
7209     fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
7210   else
7211     fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
7212                    - 1);
7213
7214   /* Determine what bits of X are guaranteed to be (non)zero.  */
7215   nonzero = nonzero_bits (x, mode);
7216
7217   /* If none of the bits in X are needed, return a zero.  */
7218   if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
7219     x = const0_rtx;
7220
7221   /* If X is a CONST_INT, return a new one.  Do this here since the
7222      test below will fail.  */
7223   if (GET_CODE (x) == CONST_INT)
7224     {
7225       if (SCALAR_INT_MODE_P (mode))
7226         return gen_int_mode (INTVAL (x) & mask, mode);
7227       else
7228         {
7229           x = GEN_INT (INTVAL (x) & mask);
7230           return gen_lowpart_common (mode, x);
7231         }
7232     }
7233
7234   /* If X is narrower than MODE and we want all the bits in X's mode, just
7235      get X in the proper mode.  */
7236   if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
7237       && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
7238     return gen_lowpart (mode, x);
7239
7240   switch (code)
7241     {
7242     case CLOBBER:
7243       /* If X is a (clobber (const_int)), return it since we know we are
7244          generating something that won't match.  */
7245       return x;
7246
7247     case SIGN_EXTEND:
7248     case ZERO_EXTEND:
7249     case ZERO_EXTRACT:
7250     case SIGN_EXTRACT:
7251       x = expand_compound_operation (x);
7252       if (GET_CODE (x) != code)
7253         return force_to_mode (x, mode, mask, next_select);
7254       break;
7255
7256     case SUBREG:
7257       if (subreg_lowpart_p (x)
7258           /* We can ignore the effect of this SUBREG if it narrows the mode or
7259              if the constant masks to zero all the bits the mode doesn't
7260              have.  */
7261           && ((GET_MODE_SIZE (GET_MODE (x))
7262                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7263               || (0 == (mask
7264                         & GET_MODE_MASK (GET_MODE (x))
7265                         & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
7266         return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
7267       break;
7268
7269     case AND:
7270       /* If this is an AND with a constant, convert it into an AND
7271          whose constant is the AND of that constant with MASK.  If it
7272          remains an AND of MASK, delete it since it is redundant.  */
7273
7274       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7275         {
7276           x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
7277                                       mask & INTVAL (XEXP (x, 1)));
7278
7279           /* If X is still an AND, see if it is an AND with a mask that
7280              is just some low-order bits.  If so, and it is MASK, we don't
7281              need it.  */
7282
7283           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
7284               && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
7285                   == mask))
7286             x = XEXP (x, 0);
7287
7288           /* If it remains an AND, try making another AND with the bits
7289              in the mode mask that aren't in MASK turned on.  If the
7290              constant in the AND is wide enough, this might make a
7291              cheaper constant.  */
7292
7293           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
7294               && GET_MODE_MASK (GET_MODE (x)) != mask
7295               && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
7296             {
7297               HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
7298                                     | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
7299               int width = GET_MODE_BITSIZE (GET_MODE (x));
7300               rtx y;
7301
7302               /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
7303                  number, sign extend it.  */
7304               if (width > 0 && width < HOST_BITS_PER_WIDE_INT
7305                   && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7306                 cval |= (HOST_WIDE_INT) -1 << width;
7307
7308               y = simplify_gen_binary (AND, GET_MODE (x),
7309                                        XEXP (x, 0), GEN_INT (cval));
7310               if (rtx_cost (y, SET) < rtx_cost (x, SET))
7311                 x = y;
7312             }
7313
7314           break;
7315         }
7316
7317       goto binop;
7318
7319     case PLUS:
7320       /* In (and (plus FOO C1) M), if M is a mask that just turns off
7321          low-order bits (as in an alignment operation) and FOO is already
7322          aligned to that boundary, mask C1 to that boundary as well.
7323          This may eliminate that PLUS and, later, the AND.  */
7324
7325       {
7326         unsigned int width = GET_MODE_BITSIZE (mode);
7327         unsigned HOST_WIDE_INT smask = mask;
7328
7329         /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
7330            number, sign extend it.  */
7331
7332         if (width < HOST_BITS_PER_WIDE_INT
7333             && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7334           smask |= (HOST_WIDE_INT) -1 << width;
7335
7336         if (GET_CODE (XEXP (x, 1)) == CONST_INT
7337             && exact_log2 (- smask) >= 0
7338             && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
7339             && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
7340           return force_to_mode (plus_constant (XEXP (x, 0),
7341                                                (INTVAL (XEXP (x, 1)) & smask)),
7342                                 mode, smask, next_select);
7343       }
7344
7345       /* ... fall through ...  */
7346
7347     case MULT:
7348       /* For PLUS, MINUS and MULT, we need any bits less significant than the
7349          most significant bit in MASK since carries from those bits will
7350          affect the bits we are interested in.  */
7351       mask = fuller_mask;
7352       goto binop;
7353
7354     case MINUS:
7355       /* If X is (minus C Y) where C's least set bit is larger than any bit
7356          in the mask, then we may replace with (neg Y).  */
7357       if (GET_CODE (XEXP (x, 0)) == CONST_INT
7358           && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
7359                                         & -INTVAL (XEXP (x, 0))))
7360               > mask))
7361         {
7362           x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
7363                                   GET_MODE (x));
7364           return force_to_mode (x, mode, mask, next_select);
7365         }
7366
7367       /* Similarly, if C contains every bit in the fuller_mask, then we may
7368          replace with (not Y).  */
7369       if (GET_CODE (XEXP (x, 0)) == CONST_INT
7370           && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
7371               == INTVAL (XEXP (x, 0))))
7372         {
7373           x = simplify_gen_unary (NOT, GET_MODE (x),
7374                                   XEXP (x, 1), GET_MODE (x));
7375           return force_to_mode (x, mode, mask, next_select);
7376         }
7377
7378       mask = fuller_mask;
7379       goto binop;
7380
7381     case IOR:
7382     case XOR:
7383       /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
7384          LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
7385          operation which may be a bitfield extraction.  Ensure that the
7386          constant we form is not wider than the mode of X.  */
7387
7388       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7389           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7390           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7391           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7392           && GET_CODE (XEXP (x, 1)) == CONST_INT
7393           && ((INTVAL (XEXP (XEXP (x, 0), 1))
7394                + floor_log2 (INTVAL (XEXP (x, 1))))
7395               < GET_MODE_BITSIZE (GET_MODE (x)))
7396           && (INTVAL (XEXP (x, 1))
7397               & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
7398         {
7399           temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
7400                           << INTVAL (XEXP (XEXP (x, 0), 1)));
7401           temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
7402                                       XEXP (XEXP (x, 0), 0), temp);
7403           x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
7404                                    XEXP (XEXP (x, 0), 1));
7405           return force_to_mode (x, mode, mask, next_select);
7406         }
7407
7408     binop:
7409       /* For most binary operations, just propagate into the operation and
7410          change the mode if we have an operation of that mode.  */
7411
7412       op0 = gen_lowpart_or_truncate (op_mode,
7413                                      force_to_mode (XEXP (x, 0), mode, mask,
7414                                                     next_select));
7415       op1 = gen_lowpart_or_truncate (op_mode,
7416                                      force_to_mode (XEXP (x, 1), mode, mask,
7417                                         next_select));
7418
7419       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7420         x = simplify_gen_binary (code, op_mode, op0, op1);
7421       break;
7422
7423     case ASHIFT:
7424       /* For left shifts, do the same, but just for the first operand.
7425          However, we cannot do anything with shifts where we cannot
7426          guarantee that the counts are smaller than the size of the mode
7427          because such a count will have a different meaning in a
7428          wider mode.  */
7429
7430       if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
7431              && INTVAL (XEXP (x, 1)) >= 0
7432              && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7433           && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7434                 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7435                     < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7436         break;
7437
7438       /* If the shift count is a constant and we can do arithmetic in
7439          the mode of the shift, refine which bits we need.  Otherwise, use the
7440          conservative form of the mask.  */
7441       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7442           && INTVAL (XEXP (x, 1)) >= 0
7443           && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7444           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7445         mask >>= INTVAL (XEXP (x, 1));
7446       else
7447         mask = fuller_mask;
7448
7449       op0 = gen_lowpart_or_truncate (op_mode,
7450                                      force_to_mode (XEXP (x, 0), op_mode,
7451                                                     mask, next_select));
7452
7453       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7454         x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
7455       break;
7456
7457     case LSHIFTRT:
7458       /* Here we can only do something if the shift count is a constant,
7459          this shift constant is valid for the host, and we can do arithmetic
7460          in OP_MODE.  */
7461
7462       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7463           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7464           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7465         {
7466           rtx inner = XEXP (x, 0);
7467           unsigned HOST_WIDE_INT inner_mask;
7468
7469           /* Select the mask of the bits we need for the shift operand.  */
7470           inner_mask = mask << INTVAL (XEXP (x, 1));
7471
7472           /* We can only change the mode of the shift if we can do arithmetic
7473              in the mode of the shift and INNER_MASK is no wider than the
7474              width of X's mode.  */
7475           if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
7476             op_mode = GET_MODE (x);
7477
7478           inner = force_to_mode (inner, op_mode, inner_mask, next_select);
7479
7480           if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7481             x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7482         }
7483
7484       /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7485          shift and AND produces only copies of the sign bit (C2 is one less
7486          than a power of two), we can do this with just a shift.  */
7487
7488       if (GET_CODE (x) == LSHIFTRT
7489           && GET_CODE (XEXP (x, 1)) == CONST_INT
7490           /* The shift puts one of the sign bit copies in the least significant
7491              bit.  */
7492           && ((INTVAL (XEXP (x, 1))
7493                + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7494               >= GET_MODE_BITSIZE (GET_MODE (x)))
7495           && exact_log2 (mask + 1) >= 0
7496           /* Number of bits left after the shift must be more than the mask
7497              needs.  */
7498           && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7499               <= GET_MODE_BITSIZE (GET_MODE (x)))
7500           /* Must be more sign bit copies than the mask needs.  */
7501           && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7502               >= exact_log2 (mask + 1)))
7503         x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7504                                  GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7505                                           - exact_log2 (mask + 1)));
7506
7507       goto shiftrt;
7508
7509     case ASHIFTRT:
7510       /* If we are just looking for the sign bit, we don't need this shift at
7511          all, even if it has a variable count.  */
7512       if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7513           && (mask == ((unsigned HOST_WIDE_INT) 1
7514                        << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7515         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7516
7517       /* If this is a shift by a constant, get a mask that contains those bits
7518          that are not copies of the sign bit.  We then have two cases:  If
7519          MASK only includes those bits, this can be a logical shift, which may
7520          allow simplifications.  If MASK is a single-bit field not within
7521          those bits, we are requesting a copy of the sign bit and hence can
7522          shift the sign bit to the appropriate location.  */
7523
7524       if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7525           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7526         {
7527           int i;
7528
7529           /* If the considered data is wider than HOST_WIDE_INT, we can't
7530              represent a mask for all its bits in a single scalar.
7531              But we only care about the lower bits, so calculate these.  */
7532
7533           if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7534             {
7535               nonzero = ~(HOST_WIDE_INT) 0;
7536
7537               /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7538                  is the number of bits a full-width mask would have set.
7539                  We need only shift if these are fewer than nonzero can
7540                  hold.  If not, we must keep all bits set in nonzero.  */
7541
7542               if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7543                   < HOST_BITS_PER_WIDE_INT)
7544                 nonzero >>= INTVAL (XEXP (x, 1))
7545                             + HOST_BITS_PER_WIDE_INT
7546                             - GET_MODE_BITSIZE (GET_MODE (x)) ;
7547             }
7548           else
7549             {
7550               nonzero = GET_MODE_MASK (GET_MODE (x));
7551               nonzero >>= INTVAL (XEXP (x, 1));
7552             }
7553
7554           if ((mask & ~nonzero) == 0)
7555             {
7556               x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
7557                                         XEXP (x, 0), INTVAL (XEXP (x, 1)));
7558               if (GET_CODE (x) != ASHIFTRT)
7559                 return force_to_mode (x, mode, mask, next_select);
7560             }
7561
7562           else if ((i = exact_log2 (mask)) >= 0)
7563             {
7564               x = simplify_shift_const
7565                   (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7566                    GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7567
7568               if (GET_CODE (x) != ASHIFTRT)
7569                 return force_to_mode (x, mode, mask, next_select);
7570             }
7571         }
7572
7573       /* If MASK is 1, convert this to an LSHIFTRT.  This can be done
7574          even if the shift count isn't a constant.  */
7575       if (mask == 1)
7576         x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7577                                  XEXP (x, 0), XEXP (x, 1));
7578
7579     shiftrt:
7580
7581       /* If this is a zero- or sign-extension operation that just affects bits
7582          we don't care about, remove it.  Be sure the call above returned
7583          something that is still a shift.  */
7584
7585       if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7586           && GET_CODE (XEXP (x, 1)) == CONST_INT
7587           && INTVAL (XEXP (x, 1)) >= 0
7588           && (INTVAL (XEXP (x, 1))
7589               <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7590           && GET_CODE (XEXP (x, 0)) == ASHIFT
7591           && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7592         return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7593                               next_select);
7594
7595       break;
7596
7597     case ROTATE:
7598     case ROTATERT:
7599       /* If the shift count is constant and we can do computations
7600          in the mode of X, compute where the bits we care about are.
7601          Otherwise, we can't do anything.  Don't change the mode of
7602          the shift or propagate MODE into the shift, though.  */
7603       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7604           && INTVAL (XEXP (x, 1)) >= 0)
7605         {
7606           temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7607                                             GET_MODE (x), GEN_INT (mask),
7608                                             XEXP (x, 1));
7609           if (temp && GET_CODE (temp) == CONST_INT)
7610             SUBST (XEXP (x, 0),
7611                    force_to_mode (XEXP (x, 0), GET_MODE (x),
7612                                   INTVAL (temp), next_select));
7613         }
7614       break;
7615
7616     case NEG:
7617       /* If we just want the low-order bit, the NEG isn't needed since it
7618          won't change the low-order bit.  */
7619       if (mask == 1)
7620         return force_to_mode (XEXP (x, 0), mode, mask, just_select);
7621
7622       /* We need any bits less significant than the most significant bit in
7623          MASK since carries from those bits will affect the bits we are
7624          interested in.  */
7625       mask = fuller_mask;
7626       goto unop;
7627
7628     case NOT:
7629       /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7630          same as the XOR case above.  Ensure that the constant we form is not
7631          wider than the mode of X.  */
7632
7633       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7634           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7635           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7636           && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7637               < GET_MODE_BITSIZE (GET_MODE (x)))
7638           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7639         {
7640           temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7641                                GET_MODE (x));
7642           temp = simplify_gen_binary (XOR, GET_MODE (x),
7643                                       XEXP (XEXP (x, 0), 0), temp);
7644           x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7645                                    temp, XEXP (XEXP (x, 0), 1));
7646
7647           return force_to_mode (x, mode, mask, next_select);
7648         }
7649
7650       /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7651          use the full mask inside the NOT.  */
7652       mask = fuller_mask;
7653
7654     unop:
7655       op0 = gen_lowpart_or_truncate (op_mode,
7656                                      force_to_mode (XEXP (x, 0), mode, mask,
7657                                                     next_select));
7658       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7659         x = simplify_gen_unary (code, op_mode, op0, op_mode);
7660       break;
7661
7662     case NE:
7663       /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7664          in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7665          which is equal to STORE_FLAG_VALUE.  */
7666       if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7667           && GET_MODE (XEXP (x, 0)) == mode
7668           && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7669           && (nonzero_bits (XEXP (x, 0), mode)
7670               == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7671         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7672
7673       break;
7674
7675     case IF_THEN_ELSE:
7676       /* We have no way of knowing if the IF_THEN_ELSE can itself be
7677          written in a narrower mode.  We play it safe and do not do so.  */
7678
7679       SUBST (XEXP (x, 1),
7680              gen_lowpart_or_truncate (GET_MODE (x),
7681                                       force_to_mode (XEXP (x, 1), mode,
7682                                                      mask, next_select)));
7683       SUBST (XEXP (x, 2),
7684              gen_lowpart_or_truncate (GET_MODE (x),
7685                                       force_to_mode (XEXP (x, 2), mode,
7686                                                      mask, next_select)));
7687       break;
7688
7689     default:
7690       break;
7691     }
7692
7693   /* Ensure we return a value of the proper mode.  */
7694   return gen_lowpart_or_truncate (mode, x);
7695 }
7696 \f
7697 /* Return nonzero if X is an expression that has one of two values depending on
7698    whether some other value is zero or nonzero.  In that case, we return the
7699    value that is being tested, *PTRUE is set to the value if the rtx being
7700    returned has a nonzero value, and *PFALSE is set to the other alternative.
7701
7702    If we return zero, we set *PTRUE and *PFALSE to X.  */
7703
7704 static rtx
7705 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
7706 {
7707   enum machine_mode mode = GET_MODE (x);
7708   enum rtx_code code = GET_CODE (x);
7709   rtx cond0, cond1, true0, true1, false0, false1;
7710   unsigned HOST_WIDE_INT nz;
7711
7712   /* If we are comparing a value against zero, we are done.  */
7713   if ((code == NE || code == EQ)
7714       && XEXP (x, 1) == const0_rtx)
7715     {
7716       *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7717       *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7718       return XEXP (x, 0);
7719     }
7720
7721   /* If this is a unary operation whose operand has one of two values, apply
7722      our opcode to compute those values.  */
7723   else if (UNARY_P (x)
7724            && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7725     {
7726       *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7727       *pfalse = simplify_gen_unary (code, mode, false0,
7728                                     GET_MODE (XEXP (x, 0)));
7729       return cond0;
7730     }
7731
7732   /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7733      make can't possibly match and would suppress other optimizations.  */
7734   else if (code == COMPARE)
7735     ;
7736
7737   /* If this is a binary operation, see if either side has only one of two
7738      values.  If either one does or if both do and they are conditional on
7739      the same value, compute the new true and false values.  */
7740   else if (BINARY_P (x))
7741     {
7742       cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7743       cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7744
7745       if ((cond0 != 0 || cond1 != 0)
7746           && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7747         {
7748           /* If if_then_else_cond returned zero, then true/false are the
7749              same rtl.  We must copy one of them to prevent invalid rtl
7750              sharing.  */
7751           if (cond0 == 0)
7752             true0 = copy_rtx (true0);
7753           else if (cond1 == 0)
7754             true1 = copy_rtx (true1);
7755
7756           if (COMPARISON_P (x))
7757             {
7758               *ptrue = simplify_gen_relational (code, mode, VOIDmode,
7759                                                 true0, true1);
7760               *pfalse = simplify_gen_relational (code, mode, VOIDmode,
7761                                                  false0, false1);
7762              }
7763           else
7764             {
7765               *ptrue = simplify_gen_binary (code, mode, true0, true1);
7766               *pfalse = simplify_gen_binary (code, mode, false0, false1);
7767             }
7768
7769           return cond0 ? cond0 : cond1;
7770         }
7771
7772       /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7773          operands is zero when the other is nonzero, and vice-versa,
7774          and STORE_FLAG_VALUE is 1 or -1.  */
7775
7776       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7777           && (code == PLUS || code == IOR || code == XOR || code == MINUS
7778               || code == UMAX)
7779           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7780         {
7781           rtx op0 = XEXP (XEXP (x, 0), 1);
7782           rtx op1 = XEXP (XEXP (x, 1), 1);
7783
7784           cond0 = XEXP (XEXP (x, 0), 0);
7785           cond1 = XEXP (XEXP (x, 1), 0);
7786
7787           if (COMPARISON_P (cond0)
7788               && COMPARISON_P (cond1)
7789               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7790                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7791                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7792                   || ((swap_condition (GET_CODE (cond0))
7793                        == reversed_comparison_code (cond1, NULL))
7794                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7795                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7796               && ! side_effects_p (x))
7797             {
7798               *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
7799               *pfalse = simplify_gen_binary (MULT, mode,
7800                                              (code == MINUS
7801                                               ? simplify_gen_unary (NEG, mode,
7802                                                                     op1, mode)
7803                                               : op1),
7804                                               const_true_rtx);
7805               return cond0;
7806             }
7807         }
7808
7809       /* Similarly for MULT, AND and UMIN, except that for these the result
7810          is always zero.  */
7811       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7812           && (code == MULT || code == AND || code == UMIN)
7813           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7814         {
7815           cond0 = XEXP (XEXP (x, 0), 0);
7816           cond1 = XEXP (XEXP (x, 1), 0);
7817
7818           if (COMPARISON_P (cond0)
7819               && COMPARISON_P (cond1)
7820               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7821                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7822                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7823                   || ((swap_condition (GET_CODE (cond0))
7824                        == reversed_comparison_code (cond1, NULL))
7825                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7826                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7827               && ! side_effects_p (x))
7828             {
7829               *ptrue = *pfalse = const0_rtx;
7830               return cond0;
7831             }
7832         }
7833     }
7834
7835   else if (code == IF_THEN_ELSE)
7836     {
7837       /* If we have IF_THEN_ELSE already, extract the condition and
7838          canonicalize it if it is NE or EQ.  */
7839       cond0 = XEXP (x, 0);
7840       *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7841       if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7842         return XEXP (cond0, 0);
7843       else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7844         {
7845           *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7846           return XEXP (cond0, 0);
7847         }
7848       else
7849         return cond0;
7850     }
7851
7852   /* If X is a SUBREG, we can narrow both the true and false values
7853      if the inner expression, if there is a condition.  */
7854   else if (code == SUBREG
7855            && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7856                                                &true0, &false0)))
7857     {
7858       true0 = simplify_gen_subreg (mode, true0,
7859                                    GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7860       false0 = simplify_gen_subreg (mode, false0,
7861                                     GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7862       if (true0 && false0)
7863         {
7864           *ptrue = true0;
7865           *pfalse = false0;
7866           return cond0;
7867         }
7868     }
7869
7870   /* If X is a constant, this isn't special and will cause confusions
7871      if we treat it as such.  Likewise if it is equivalent to a constant.  */
7872   else if (CONSTANT_P (x)
7873            || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7874     ;
7875
7876   /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7877      will be least confusing to the rest of the compiler.  */
7878   else if (mode == BImode)
7879     {
7880       *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7881       return x;
7882     }
7883
7884   /* If X is known to be either 0 or -1, those are the true and
7885      false values when testing X.  */
7886   else if (x == constm1_rtx || x == const0_rtx
7887            || (mode != VOIDmode
7888                && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7889     {
7890       *ptrue = constm1_rtx, *pfalse = const0_rtx;
7891       return x;
7892     }
7893
7894   /* Likewise for 0 or a single bit.  */
7895   else if (SCALAR_INT_MODE_P (mode)
7896            && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7897            && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7898     {
7899       *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7900       return x;
7901     }
7902
7903   /* Otherwise fail; show no condition with true and false values the same.  */
7904   *ptrue = *pfalse = x;
7905   return 0;
7906 }
7907 \f
7908 /* Return the value of expression X given the fact that condition COND
7909    is known to be true when applied to REG as its first operand and VAL
7910    as its second.  X is known to not be shared and so can be modified in
7911    place.
7912
7913    We only handle the simplest cases, and specifically those cases that
7914    arise with IF_THEN_ELSE expressions.  */
7915
7916 static rtx
7917 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
7918 {
7919   enum rtx_code code = GET_CODE (x);
7920   rtx temp;
7921   const char *fmt;
7922   int i, j;
7923
7924   if (side_effects_p (x))
7925     return x;
7926
7927   /* If either operand of the condition is a floating point value,
7928      then we have to avoid collapsing an EQ comparison.  */
7929   if (cond == EQ
7930       && rtx_equal_p (x, reg)
7931       && ! FLOAT_MODE_P (GET_MODE (x))
7932       && ! FLOAT_MODE_P (GET_MODE (val)))
7933     return val;
7934
7935   if (cond == UNEQ && rtx_equal_p (x, reg))
7936     return val;
7937
7938   /* If X is (abs REG) and we know something about REG's relationship
7939      with zero, we may be able to simplify this.  */
7940
7941   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7942     switch (cond)
7943       {
7944       case GE:  case GT:  case EQ:
7945         return XEXP (x, 0);
7946       case LT:  case LE:
7947         return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
7948                                    XEXP (x, 0),
7949                                    GET_MODE (XEXP (x, 0)));
7950       default:
7951         break;
7952       }
7953
7954   /* The only other cases we handle are MIN, MAX, and comparisons if the
7955      operands are the same as REG and VAL.  */
7956
7957   else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
7958     {
7959       if (rtx_equal_p (XEXP (x, 0), val))
7960         cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7961
7962       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7963         {
7964           if (COMPARISON_P (x))
7965             {
7966               if (comparison_dominates_p (cond, code))
7967                 return const_true_rtx;
7968
7969               code = reversed_comparison_code (x, NULL);
7970               if (code != UNKNOWN
7971                   && comparison_dominates_p (cond, code))
7972                 return const0_rtx;
7973               else
7974                 return x;
7975             }
7976           else if (code == SMAX || code == SMIN
7977                    || code == UMIN || code == UMAX)
7978             {
7979               int unsignedp = (code == UMIN || code == UMAX);
7980
7981               /* Do not reverse the condition when it is NE or EQ.
7982                  This is because we cannot conclude anything about
7983                  the value of 'SMAX (x, y)' when x is not equal to y,
7984                  but we can when x equals y.  */
7985               if ((code == SMAX || code == UMAX)
7986                   && ! (cond == EQ || cond == NE))
7987                 cond = reverse_condition (cond);
7988
7989               switch (cond)
7990                 {
7991                 case GE:   case GT:
7992                   return unsignedp ? x : XEXP (x, 1);
7993                 case LE:   case LT:
7994                   return unsignedp ? x : XEXP (x, 0);
7995                 case GEU:  case GTU:
7996                   return unsignedp ? XEXP (x, 1) : x;
7997                 case LEU:  case LTU:
7998                   return unsignedp ? XEXP (x, 0) : x;
7999                 default:
8000                   break;
8001                 }
8002             }
8003         }
8004     }
8005   else if (code == SUBREG)
8006     {
8007       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8008       rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
8009
8010       if (SUBREG_REG (x) != r)
8011         {
8012           /* We must simplify subreg here, before we lose track of the
8013              original inner_mode.  */
8014           new = simplify_subreg (GET_MODE (x), r,
8015                                  inner_mode, SUBREG_BYTE (x));
8016           if (new)
8017             return new;
8018           else
8019             SUBST (SUBREG_REG (x), r);
8020         }
8021
8022       return x;
8023     }
8024   /* We don't have to handle SIGN_EXTEND here, because even in the
8025      case of replacing something with a modeless CONST_INT, a
8026      CONST_INT is already (supposed to be) a valid sign extension for
8027      its narrower mode, which implies it's already properly
8028      sign-extended for the wider mode.  Now, for ZERO_EXTEND, the
8029      story is different.  */
8030   else if (code == ZERO_EXTEND)
8031     {
8032       enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8033       rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
8034
8035       if (XEXP (x, 0) != r)
8036         {
8037           /* We must simplify the zero_extend here, before we lose
8038              track of the original inner_mode.  */
8039           new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8040                                           r, inner_mode);
8041           if (new)
8042             return new;
8043           else
8044             SUBST (XEXP (x, 0), r);
8045         }
8046
8047       return x;
8048     }
8049
8050   fmt = GET_RTX_FORMAT (code);
8051   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8052     {
8053       if (fmt[i] == 'e')
8054         SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8055       else if (fmt[i] == 'E')
8056         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8057           SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8058                                                 cond, reg, val));
8059     }
8060
8061   return x;
8062 }
8063 \f
8064 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8065    assignment as a field assignment.  */
8066
8067 static int
8068 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8069 {
8070   if (x == y || rtx_equal_p (x, y))
8071     return 1;
8072
8073   if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8074     return 0;
8075
8076   /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8077      Note that all SUBREGs of MEM are paradoxical; otherwise they
8078      would have been rewritten.  */
8079   if (MEM_P (x) && GET_CODE (y) == SUBREG
8080       && MEM_P (SUBREG_REG (y))
8081       && rtx_equal_p (SUBREG_REG (y),
8082                       gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8083     return 1;
8084
8085   if (MEM_P (y) && GET_CODE (x) == SUBREG
8086       && MEM_P (SUBREG_REG (x))
8087       && rtx_equal_p (SUBREG_REG (x),
8088                       gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8089     return 1;
8090
8091   /* We used to see if get_last_value of X and Y were the same but that's
8092      not correct.  In one direction, we'll cause the assignment to have
8093      the wrong destination and in the case, we'll import a register into this
8094      insn that might have already have been dead.   So fail if none of the
8095      above cases are true.  */
8096   return 0;
8097 }
8098 \f
8099 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8100    Return that assignment if so.
8101
8102    We only handle the most common cases.  */
8103
8104 static rtx
8105 make_field_assignment (rtx x)
8106 {
8107   rtx dest = SET_DEST (x);
8108   rtx src = SET_SRC (x);
8109   rtx assign;
8110   rtx rhs, lhs;
8111   HOST_WIDE_INT c1;
8112   HOST_WIDE_INT pos;
8113   unsigned HOST_WIDE_INT len;
8114   rtx other;
8115   enum machine_mode mode;
8116
8117   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
8118      a clear of a one-bit field.  We will have changed it to
8119      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
8120      for a SUBREG.  */
8121
8122   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
8123       && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
8124       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
8125       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8126     {
8127       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8128                                 1, 1, 1, 0);
8129       if (assign != 0)
8130         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8131       return x;
8132     }
8133
8134   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
8135       && subreg_lowpart_p (XEXP (src, 0))
8136       && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
8137           < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
8138       && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
8139       && GET_CODE (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == CONST_INT
8140       && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
8141       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8142     {
8143       assign = make_extraction (VOIDmode, dest, 0,
8144                                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
8145                                 1, 1, 1, 0);
8146       if (assign != 0)
8147         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8148       return x;
8149     }
8150
8151   /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
8152      one-bit field.  */
8153   if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
8154       && XEXP (XEXP (src, 0), 0) == const1_rtx
8155       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8156     {
8157       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8158                                 1, 1, 1, 0);
8159       if (assign != 0)
8160         return gen_rtx_SET (VOIDmode, assign, const1_rtx);
8161       return x;
8162     }
8163
8164   /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
8165      SRC is an AND with all bits of that field set, then we can discard
8166      the AND.  */
8167   if (GET_CODE (dest) == ZERO_EXTRACT
8168       && GET_CODE (XEXP (dest, 1)) == CONST_INT
8169       && GET_CODE (src) == AND
8170       && GET_CODE (XEXP (src, 1)) == CONST_INT)
8171     {
8172       HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
8173       unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
8174       unsigned HOST_WIDE_INT ze_mask;
8175
8176       if (width >= HOST_BITS_PER_WIDE_INT)
8177         ze_mask = -1;
8178       else
8179         ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
8180
8181       /* Complete overlap.  We can remove the source AND.  */
8182       if ((and_mask & ze_mask) == ze_mask)
8183         return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
8184
8185       /* Partial overlap.  We can reduce the source AND.  */
8186       if ((and_mask & ze_mask) != and_mask)
8187         {
8188           mode = GET_MODE (src);
8189           src = gen_rtx_AND (mode, XEXP (src, 0),
8190                              gen_int_mode (and_mask & ze_mask, mode));
8191           return gen_rtx_SET (VOIDmode, dest, src);
8192         }
8193     }
8194
8195   /* The other case we handle is assignments into a constant-position
8196      field.  They look like (ior/xor (and DEST C1) OTHER).  If C1 represents
8197      a mask that has all one bits except for a group of zero bits and
8198      OTHER is known to have zeros where C1 has ones, this is such an
8199      assignment.  Compute the position and length from C1.  Shift OTHER
8200      to the appropriate position, force it to the required mode, and
8201      make the extraction.  Check for the AND in both operands.  */
8202
8203   if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
8204     return x;
8205
8206   rhs = expand_compound_operation (XEXP (src, 0));
8207   lhs = expand_compound_operation (XEXP (src, 1));
8208
8209   if (GET_CODE (rhs) == AND
8210       && GET_CODE (XEXP (rhs, 1)) == CONST_INT
8211       && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
8212     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
8213   else if (GET_CODE (lhs) == AND
8214            && GET_CODE (XEXP (lhs, 1)) == CONST_INT
8215            && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
8216     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
8217   else
8218     return x;
8219
8220   pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
8221   if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
8222       || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
8223       || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
8224     return x;
8225
8226   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
8227   if (assign == 0)
8228     return x;
8229
8230   /* The mode to use for the source is the mode of the assignment, or of
8231      what is inside a possible STRICT_LOW_PART.  */
8232   mode = (GET_CODE (assign) == STRICT_LOW_PART
8233           ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
8234
8235   /* Shift OTHER right POS places and make it the source, restricting it
8236      to the proper length and mode.  */
8237
8238   src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
8239                                                      GET_MODE (src),
8240                                                      other, pos),
8241                                dest);
8242   src = force_to_mode (src, mode,
8243                        GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
8244                        ? ~(unsigned HOST_WIDE_INT) 0
8245                        : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
8246                        0);
8247
8248   /* If SRC is masked by an AND that does not make a difference in
8249      the value being stored, strip it.  */
8250   if (GET_CODE (assign) == ZERO_EXTRACT
8251       && GET_CODE (XEXP (assign, 1)) == CONST_INT
8252       && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
8253       && GET_CODE (src) == AND
8254       && GET_CODE (XEXP (src, 1)) == CONST_INT
8255       && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
8256           == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
8257     src = XEXP (src, 0);
8258
8259   return gen_rtx_SET (VOIDmode, assign, src);
8260 }
8261 \f
8262 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
8263    if so.  */
8264
8265 static rtx
8266 apply_distributive_law (rtx x)
8267 {
8268   enum rtx_code code = GET_CODE (x);
8269   enum rtx_code inner_code;
8270   rtx lhs, rhs, other;
8271   rtx tem;
8272
8273   /* Distributivity is not true for floating point as it can change the
8274      value.  So we don't do it unless -funsafe-math-optimizations.  */
8275   if (FLOAT_MODE_P (GET_MODE (x))
8276       && ! flag_unsafe_math_optimizations)
8277     return x;
8278
8279   /* The outer operation can only be one of the following:  */
8280   if (code != IOR && code != AND && code != XOR
8281       && code != PLUS && code != MINUS)
8282     return x;
8283
8284   lhs = XEXP (x, 0);
8285   rhs = XEXP (x, 1);
8286
8287   /* If either operand is a primitive we can't do anything, so get out
8288      fast.  */
8289   if (OBJECT_P (lhs) || OBJECT_P (rhs))
8290     return x;
8291
8292   lhs = expand_compound_operation (lhs);
8293   rhs = expand_compound_operation (rhs);
8294   inner_code = GET_CODE (lhs);
8295   if (inner_code != GET_CODE (rhs))
8296     return x;
8297
8298   /* See if the inner and outer operations distribute.  */
8299   switch (inner_code)
8300     {
8301     case LSHIFTRT:
8302     case ASHIFTRT:
8303     case AND:
8304     case IOR:
8305       /* These all distribute except over PLUS.  */
8306       if (code == PLUS || code == MINUS)
8307         return x;
8308       break;
8309
8310     case MULT:
8311       if (code != PLUS && code != MINUS)
8312         return x;
8313       break;
8314
8315     case ASHIFT:
8316       /* This is also a multiply, so it distributes over everything.  */
8317       break;
8318
8319     case SUBREG:
8320       /* Non-paradoxical SUBREGs distributes over all operations,
8321          provided the inner modes and byte offsets are the same, this
8322          is an extraction of a low-order part, we don't convert an fp
8323          operation to int or vice versa, this is not a vector mode,
8324          and we would not be converting a single-word operation into a
8325          multi-word operation.  The latter test is not required, but
8326          it prevents generating unneeded multi-word operations.  Some
8327          of the previous tests are redundant given the latter test,
8328          but are retained because they are required for correctness.
8329
8330          We produce the result slightly differently in this case.  */
8331
8332       if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
8333           || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
8334           || ! subreg_lowpart_p (lhs)
8335           || (GET_MODE_CLASS (GET_MODE (lhs))
8336               != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
8337           || (GET_MODE_SIZE (GET_MODE (lhs))
8338               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
8339           || VECTOR_MODE_P (GET_MODE (lhs))
8340           || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD
8341           /* Result might need to be truncated.  Don't change mode if
8342              explicit truncation is needed.  */
8343           || !TRULY_NOOP_TRUNCATION
8344                (GET_MODE_BITSIZE (GET_MODE (x)),
8345                 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (lhs)))))
8346         return x;
8347
8348       tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
8349                                  SUBREG_REG (lhs), SUBREG_REG (rhs));
8350       return gen_lowpart (GET_MODE (x), tem);
8351
8352     default:
8353       return x;
8354     }
8355
8356   /* Set LHS and RHS to the inner operands (A and B in the example
8357      above) and set OTHER to the common operand (C in the example).
8358      There is only one way to do this unless the inner operation is
8359      commutative.  */
8360   if (COMMUTATIVE_ARITH_P (lhs)
8361       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
8362     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
8363   else if (COMMUTATIVE_ARITH_P (lhs)
8364            && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
8365     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
8366   else if (COMMUTATIVE_ARITH_P (lhs)
8367            && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
8368     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
8369   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
8370     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
8371   else
8372     return x;
8373
8374   /* Form the new inner operation, seeing if it simplifies first.  */
8375   tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
8376
8377   /* There is one exception to the general way of distributing:
8378      (a | c) ^ (b | c) -> (a ^ b) & ~c  */
8379   if (code == XOR && inner_code == IOR)
8380     {
8381       inner_code = AND;
8382       other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
8383     }
8384
8385   /* We may be able to continuing distributing the result, so call
8386      ourselves recursively on the inner operation before forming the
8387      outer operation, which we return.  */
8388   return simplify_gen_binary (inner_code, GET_MODE (x),
8389                               apply_distributive_law (tem), other);
8390 }
8391
8392 /* See if X is of the form (* (+ A B) C), and if so convert to
8393    (+ (* A C) (* B C)) and try to simplify.
8394
8395    Most of the time, this results in no change.  However, if some of
8396    the operands are the same or inverses of each other, simplifications
8397    will result.
8398
8399    For example, (and (ior A B) (not B)) can occur as the result of
8400    expanding a bit field assignment.  When we apply the distributive
8401    law to this, we get (ior (and (A (not B))) (and (B (not B)))),
8402    which then simplifies to (and (A (not B))).
8403
8404    Note that no checks happen on the validity of applying the inverse
8405    distributive law.  This is pointless since we can do it in the
8406    few places where this routine is called.
8407
8408    N is the index of the term that is decomposed (the arithmetic operation,
8409    i.e. (+ A B) in the first example above).  !N is the index of the term that
8410    is distributed, i.e. of C in the first example above.  */
8411 static rtx
8412 distribute_and_simplify_rtx (rtx x, int n)
8413 {
8414   enum machine_mode mode;
8415   enum rtx_code outer_code, inner_code;
8416   rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
8417
8418   decomposed = XEXP (x, n);
8419   if (!ARITHMETIC_P (decomposed))
8420     return NULL_RTX;
8421
8422   mode = GET_MODE (x);
8423   outer_code = GET_CODE (x);
8424   distributed = XEXP (x, !n);
8425
8426   inner_code = GET_CODE (decomposed);
8427   inner_op0 = XEXP (decomposed, 0);
8428   inner_op1 = XEXP (decomposed, 1);
8429
8430   /* Special case (and (xor B C) (not A)), which is equivalent to
8431      (xor (ior A B) (ior A C))  */
8432   if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
8433     {
8434       distributed = XEXP (distributed, 0);
8435       outer_code = IOR;
8436     }
8437
8438   if (n == 0)
8439     {
8440       /* Distribute the second term.  */
8441       new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
8442       new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
8443     }
8444   else
8445     {
8446       /* Distribute the first term.  */
8447       new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
8448       new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
8449     }
8450
8451   tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
8452                                                      new_op0, new_op1));
8453   if (GET_CODE (tmp) != outer_code
8454       && rtx_cost (tmp, SET) < rtx_cost (x, SET))
8455     return tmp;
8456
8457   return NULL_RTX;
8458 }
8459 \f
8460 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
8461    in MODE.  Return an equivalent form, if different from (and VAROP
8462    (const_int CONSTOP)).  Otherwise, return NULL_RTX.  */
8463
8464 static rtx
8465 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
8466                           unsigned HOST_WIDE_INT constop)
8467 {
8468   unsigned HOST_WIDE_INT nonzero;
8469   unsigned HOST_WIDE_INT orig_constop;
8470   rtx orig_varop;
8471   int i;
8472
8473   orig_varop = varop;
8474   orig_constop = constop;
8475   if (GET_CODE (varop) == CLOBBER)
8476     return NULL_RTX;
8477
8478   /* Simplify VAROP knowing that we will be only looking at some of the
8479      bits in it.
8480
8481      Note by passing in CONSTOP, we guarantee that the bits not set in
8482      CONSTOP are not significant and will never be examined.  We must
8483      ensure that is the case by explicitly masking out those bits
8484      before returning.  */
8485   varop = force_to_mode (varop, mode, constop, 0);
8486
8487   /* If VAROP is a CLOBBER, we will fail so return it.  */
8488   if (GET_CODE (varop) == CLOBBER)
8489     return varop;
8490
8491   /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
8492      to VAROP and return the new constant.  */
8493   if (GET_CODE (varop) == CONST_INT)
8494     return gen_int_mode (INTVAL (varop) & constop, mode);
8495
8496   /* See what bits may be nonzero in VAROP.  Unlike the general case of
8497      a call to nonzero_bits, here we don't care about bits outside
8498      MODE.  */
8499
8500   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
8501
8502   /* Turn off all bits in the constant that are known to already be zero.
8503      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
8504      which is tested below.  */
8505
8506   constop &= nonzero;
8507
8508   /* If we don't have any bits left, return zero.  */
8509   if (constop == 0)
8510     return const0_rtx;
8511
8512   /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
8513      a power of two, we can replace this with an ASHIFT.  */
8514   if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
8515       && (i = exact_log2 (constop)) >= 0)
8516     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
8517
8518   /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8519      or XOR, then try to apply the distributive law.  This may eliminate
8520      operations if either branch can be simplified because of the AND.
8521      It may also make some cases more complex, but those cases probably
8522      won't match a pattern either with or without this.  */
8523
8524   if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
8525     return
8526       gen_lowpart
8527         (mode,
8528          apply_distributive_law
8529          (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
8530                                simplify_and_const_int (NULL_RTX,
8531                                                        GET_MODE (varop),
8532                                                        XEXP (varop, 0),
8533                                                        constop),
8534                                simplify_and_const_int (NULL_RTX,
8535                                                        GET_MODE (varop),
8536                                                        XEXP (varop, 1),
8537                                                        constop))));
8538
8539   /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
8540      the AND and see if one of the operands simplifies to zero.  If so, we
8541      may eliminate it.  */
8542
8543   if (GET_CODE (varop) == PLUS
8544       && exact_log2 (constop + 1) >= 0)
8545     {
8546       rtx o0, o1;
8547
8548       o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
8549       o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
8550       if (o0 == const0_rtx)
8551         return o1;
8552       if (o1 == const0_rtx)
8553         return o0;
8554     }
8555
8556   /* Make a SUBREG if necessary.  If we can't make it, fail.  */
8557   varop = gen_lowpart (mode, varop);
8558   if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
8559     return NULL_RTX;
8560
8561   /* If we are only masking insignificant bits, return VAROP.  */
8562   if (constop == nonzero)
8563     return varop;
8564
8565   if (varop == orig_varop && constop == orig_constop)
8566     return NULL_RTX;
8567
8568   /* Otherwise, return an AND.  */
8569   return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
8570 }
8571
8572
8573 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
8574    in MODE.
8575
8576    Return an equivalent form, if different from X.  Otherwise, return X.  If
8577    X is zero, we are to always construct the equivalent form.  */
8578
8579 static rtx
8580 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
8581                         unsigned HOST_WIDE_INT constop)
8582 {
8583   rtx tem = simplify_and_const_int_1 (mode, varop, constop);
8584   if (tem)
8585     return tem;
8586
8587   if (!x)
8588     x = simplify_gen_binary (AND, GET_MODE (varop), varop,
8589                              gen_int_mode (constop, mode));
8590   if (GET_MODE (x) != mode)
8591     x = gen_lowpart (mode, x);
8592   return x;
8593 }
8594 \f
8595 /* Given a REG, X, compute which bits in X can be nonzero.
8596    We don't care about bits outside of those defined in MODE.
8597
8598    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8599    a shift, AND, or zero_extract, we can do better.  */
8600
8601 static rtx
8602 reg_nonzero_bits_for_combine (rtx x, enum machine_mode mode,
8603                               rtx known_x ATTRIBUTE_UNUSED,
8604                               enum machine_mode known_mode ATTRIBUTE_UNUSED,
8605                               unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
8606                               unsigned HOST_WIDE_INT *nonzero)
8607 {
8608   rtx tem;
8609
8610   /* If X is a register whose nonzero bits value is current, use it.
8611      Otherwise, if X is a register whose value we can find, use that
8612      value.  Otherwise, use the previously-computed global nonzero bits
8613      for this register.  */
8614
8615   if (reg_stat[REGNO (x)].last_set_value != 0
8616       && (reg_stat[REGNO (x)].last_set_mode == mode
8617           || (GET_MODE_CLASS (reg_stat[REGNO (x)].last_set_mode) == MODE_INT
8618               && GET_MODE_CLASS (mode) == MODE_INT))
8619       && ((reg_stat[REGNO (x)].last_set_label >= label_tick_ebb_start
8620            && reg_stat[REGNO (x)].last_set_label < label_tick)
8621           || (reg_stat[REGNO (x)].last_set_label == label_tick
8622               && DF_INSN_LUID (reg_stat[REGNO (x)].last_set) < subst_low_luid)
8623           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8624               && REG_N_SETS (REGNO (x)) == 1
8625               && !REGNO_REG_SET_P
8626                   (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
8627     {
8628       *nonzero &= reg_stat[REGNO (x)].last_set_nonzero_bits;
8629       return NULL;
8630     }
8631
8632   tem = get_last_value (x);
8633
8634   if (tem)
8635     {
8636 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8637       /* If X is narrower than MODE and TEM is a non-negative
8638          constant that would appear negative in the mode of X,
8639          sign-extend it for use in reg_nonzero_bits because some
8640          machines (maybe most) will actually do the sign-extension
8641          and this is the conservative approach.
8642
8643          ??? For 2.5, try to tighten up the MD files in this regard
8644          instead of this kludge.  */
8645
8646       if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
8647           && GET_CODE (tem) == CONST_INT
8648           && INTVAL (tem) > 0
8649           && 0 != (INTVAL (tem)
8650                    & ((HOST_WIDE_INT) 1
8651                       << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8652         tem = GEN_INT (INTVAL (tem)
8653                        | ((HOST_WIDE_INT) (-1)
8654                           << GET_MODE_BITSIZE (GET_MODE (x))));
8655 #endif
8656       return tem;
8657     }
8658   else if (nonzero_sign_valid && reg_stat[REGNO (x)].nonzero_bits)
8659     {
8660       unsigned HOST_WIDE_INT mask = reg_stat[REGNO (x)].nonzero_bits;
8661
8662       if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
8663         /* We don't know anything about the upper bits.  */
8664         mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8665       *nonzero &= mask;
8666     }
8667
8668   return NULL;
8669 }
8670
8671 /* Return the number of bits at the high-order end of X that are known to
8672    be equal to the sign bit.  X will be used in mode MODE; if MODE is
8673    VOIDmode, X will be used in its own mode.  The returned value  will always
8674    be between 1 and the number of bits in MODE.  */
8675
8676 static rtx
8677 reg_num_sign_bit_copies_for_combine (rtx x, enum machine_mode mode,
8678                                      rtx known_x ATTRIBUTE_UNUSED,
8679                                      enum machine_mode known_mode
8680                                      ATTRIBUTE_UNUSED,
8681                                      unsigned int known_ret ATTRIBUTE_UNUSED,
8682                                      unsigned int *result)
8683 {
8684   rtx tem;
8685
8686   if (reg_stat[REGNO (x)].last_set_value != 0
8687       && reg_stat[REGNO (x)].last_set_mode == mode
8688       && ((reg_stat[REGNO (x)].last_set_label >= label_tick_ebb_start
8689            && reg_stat[REGNO (x)].last_set_label < label_tick)
8690           || (reg_stat[REGNO (x)].last_set_label == label_tick
8691               && DF_INSN_LUID (reg_stat[REGNO (x)].last_set) < subst_low_luid)
8692           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8693               && REG_N_SETS (REGNO (x)) == 1
8694               && !REGNO_REG_SET_P
8695                   (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
8696     {
8697       *result = reg_stat[REGNO (x)].last_set_sign_bit_copies;
8698       return NULL;
8699     }
8700
8701   tem = get_last_value (x);
8702   if (tem != 0)
8703     return tem;
8704
8705   if (nonzero_sign_valid && reg_stat[REGNO (x)].sign_bit_copies != 0
8706       && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
8707     *result = reg_stat[REGNO (x)].sign_bit_copies;
8708
8709   return NULL;
8710 }
8711 \f
8712 /* Return the number of "extended" bits there are in X, when interpreted
8713    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
8714    unsigned quantities, this is the number of high-order zero bits.
8715    For signed quantities, this is the number of copies of the sign bit
8716    minus 1.  In both case, this function returns the number of "spare"
8717    bits.  For example, if two quantities for which this function returns
8718    at least 1 are added, the addition is known not to overflow.
8719
8720    This function will always return 0 unless called during combine, which
8721    implies that it must be called from a define_split.  */
8722
8723 unsigned int
8724 extended_count (rtx x, enum machine_mode mode, int unsignedp)
8725 {
8726   if (nonzero_sign_valid == 0)
8727     return 0;
8728
8729   return (unsignedp
8730           ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8731              ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
8732                                - floor_log2 (nonzero_bits (x, mode)))
8733              : 0)
8734           : num_sign_bit_copies (x, mode) - 1);
8735 }
8736 \f
8737 /* This function is called from `simplify_shift_const' to merge two
8738    outer operations.  Specifically, we have already found that we need
8739    to perform operation *POP0 with constant *PCONST0 at the outermost
8740    position.  We would now like to also perform OP1 with constant CONST1
8741    (with *POP0 being done last).
8742
8743    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8744    the resulting operation.  *PCOMP_P is set to 1 if we would need to
8745    complement the innermost operand, otherwise it is unchanged.
8746
8747    MODE is the mode in which the operation will be done.  No bits outside
8748    the width of this mode matter.  It is assumed that the width of this mode
8749    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8750
8751    If *POP0 or OP1 are UNKNOWN, it means no operation is required.  Only NEG, PLUS,
8752    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
8753    result is simply *PCONST0.
8754
8755    If the resulting operation cannot be expressed as one operation, we
8756    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
8757
8758 static int
8759 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)
8760 {
8761   enum rtx_code op0 = *pop0;
8762   HOST_WIDE_INT const0 = *pconst0;
8763
8764   const0 &= GET_MODE_MASK (mode);
8765   const1 &= GET_MODE_MASK (mode);
8766
8767   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
8768   if (op0 == AND)
8769     const1 &= const0;
8770
8771   /* If OP0 or OP1 is UNKNOWN, this is easy.  Similarly if they are the same or
8772      if OP0 is SET.  */
8773
8774   if (op1 == UNKNOWN || op0 == SET)
8775     return 1;
8776
8777   else if (op0 == UNKNOWN)
8778     op0 = op1, const0 = const1;
8779
8780   else if (op0 == op1)
8781     {
8782       switch (op0)
8783         {
8784         case AND:
8785           const0 &= const1;
8786           break;
8787         case IOR:
8788           const0 |= const1;
8789           break;
8790         case XOR:
8791           const0 ^= const1;
8792           break;
8793         case PLUS:
8794           const0 += const1;
8795           break;
8796         case NEG:
8797           op0 = UNKNOWN;
8798           break;
8799         default:
8800           break;
8801         }
8802     }
8803
8804   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
8805   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8806     return 0;
8807
8808   /* If the two constants aren't the same, we can't do anything.  The
8809      remaining six cases can all be done.  */
8810   else if (const0 != const1)
8811     return 0;
8812
8813   else
8814     switch (op0)
8815       {
8816       case IOR:
8817         if (op1 == AND)
8818           /* (a & b) | b == b */
8819           op0 = SET;
8820         else /* op1 == XOR */
8821           /* (a ^ b) | b == a | b */
8822           {;}
8823         break;
8824
8825       case XOR:
8826         if (op1 == AND)
8827           /* (a & b) ^ b == (~a) & b */
8828           op0 = AND, *pcomp_p = 1;
8829         else /* op1 == IOR */
8830           /* (a | b) ^ b == a & ~b */
8831           op0 = AND, const0 = ~const0;
8832         break;
8833
8834       case AND:
8835         if (op1 == IOR)
8836           /* (a | b) & b == b */
8837         op0 = SET;
8838         else /* op1 == XOR */
8839           /* (a ^ b) & b) == (~a) & b */
8840           *pcomp_p = 1;
8841         break;
8842       default:
8843         break;
8844       }
8845
8846   /* Check for NO-OP cases.  */
8847   const0 &= GET_MODE_MASK (mode);
8848   if (const0 == 0
8849       && (op0 == IOR || op0 == XOR || op0 == PLUS))
8850     op0 = UNKNOWN;
8851   else if (const0 == 0 && op0 == AND)
8852     op0 = SET;
8853   else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8854            && op0 == AND)
8855     op0 = UNKNOWN;
8856
8857   /* ??? Slightly redundant with the above mask, but not entirely.
8858      Moving this above means we'd have to sign-extend the mode mask
8859      for the final test.  */
8860   const0 = trunc_int_for_mode (const0, mode);
8861
8862   *pop0 = op0;
8863   *pconst0 = const0;
8864
8865   return 1;
8866 }
8867 \f
8868 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
8869    The result of the shift is RESULT_MODE.  Return NULL_RTX if we cannot
8870    simplify it.  Otherwise, return a simplified value.
8871
8872    The shift is normally computed in the widest mode we find in VAROP, as
8873    long as it isn't a different number of words than RESULT_MODE.  Exceptions
8874    are ASHIFTRT and ROTATE, which are always done in their original mode.  */
8875
8876 static rtx
8877 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
8878                         rtx varop, int orig_count)
8879 {
8880   enum rtx_code orig_code = code;
8881   rtx orig_varop = varop;
8882   int count;
8883   enum machine_mode mode = result_mode;
8884   enum machine_mode shift_mode, tmode;
8885   unsigned int mode_words
8886     = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8887   /* We form (outer_op (code varop count) (outer_const)).  */
8888   enum rtx_code outer_op = UNKNOWN;
8889   HOST_WIDE_INT outer_const = 0;
8890   int complement_p = 0;
8891   rtx new, x;
8892
8893   /* Make sure and truncate the "natural" shift on the way in.  We don't
8894      want to do this inside the loop as it makes it more difficult to
8895      combine shifts.  */
8896   if (SHIFT_COUNT_TRUNCATED)
8897     orig_count &= GET_MODE_BITSIZE (mode) - 1;
8898
8899   /* If we were given an invalid count, don't do anything except exactly
8900      what was requested.  */
8901
8902   if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
8903     return NULL_RTX;
8904
8905   count = orig_count;
8906
8907   /* Unless one of the branches of the `if' in this loop does a `continue',
8908      we will `break' the loop after the `if'.  */
8909
8910   while (count != 0)
8911     {
8912       /* If we have an operand of (clobber (const_int 0)), fail.  */
8913       if (GET_CODE (varop) == CLOBBER)
8914         return NULL_RTX;
8915
8916       /* If we discovered we had to complement VAROP, leave.  Making a NOT
8917          here would cause an infinite loop.  */
8918       if (complement_p)
8919         break;
8920
8921       /* Convert ROTATERT to ROTATE.  */
8922       if (code == ROTATERT)
8923         {
8924           unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
8925           code = ROTATE;
8926           if (VECTOR_MODE_P (result_mode))
8927             count = bitsize / GET_MODE_NUNITS (result_mode) - count;
8928           else
8929             count = bitsize - count;
8930         }
8931
8932       /* We need to determine what mode we will do the shift in.  If the
8933          shift is a right shift or a ROTATE, we must always do it in the mode
8934          it was originally done in.  Otherwise, we can do it in MODE, the
8935          widest mode encountered.  */
8936       shift_mode
8937         = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8938            ? result_mode : mode);
8939
8940       /* Handle cases where the count is greater than the size of the mode
8941          minus 1.  For ASHIFT, use the size minus one as the count (this can
8942          occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
8943          take the count modulo the size.  For other shifts, the result is
8944          zero.
8945
8946          Since these shifts are being produced by the compiler by combining
8947          multiple operations, each of which are defined, we know what the
8948          result is supposed to be.  */
8949
8950       if (count > (GET_MODE_BITSIZE (shift_mode) - 1))
8951         {
8952           if (code == ASHIFTRT)
8953             count = GET_MODE_BITSIZE (shift_mode) - 1;
8954           else if (code == ROTATE || code == ROTATERT)
8955             count %= GET_MODE_BITSIZE (shift_mode);
8956           else
8957             {
8958               /* We can't simply return zero because there may be an
8959                  outer op.  */
8960               varop = const0_rtx;
8961               count = 0;
8962               break;
8963             }
8964         }
8965
8966       /* An arithmetic right shift of a quantity known to be -1 or 0
8967          is a no-op.  */
8968       if (code == ASHIFTRT
8969           && (num_sign_bit_copies (varop, shift_mode)
8970               == GET_MODE_BITSIZE (shift_mode)))
8971         {
8972           count = 0;
8973           break;
8974         }
8975
8976       /* If we are doing an arithmetic right shift and discarding all but
8977          the sign bit copies, this is equivalent to doing a shift by the
8978          bitsize minus one.  Convert it into that shift because it will often
8979          allow other simplifications.  */
8980
8981       if (code == ASHIFTRT
8982           && (count + num_sign_bit_copies (varop, shift_mode)
8983               >= GET_MODE_BITSIZE (shift_mode)))
8984         count = GET_MODE_BITSIZE (shift_mode) - 1;
8985
8986       /* We simplify the tests below and elsewhere by converting
8987          ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8988          `make_compound_operation' will convert it to an ASHIFTRT for
8989          those machines (such as VAX) that don't have an LSHIFTRT.  */
8990       if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8991           && code == ASHIFTRT
8992           && ((nonzero_bits (varop, shift_mode)
8993                & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
8994               == 0))
8995         code = LSHIFTRT;
8996
8997       if (((code == LSHIFTRT
8998             && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8999             && !(nonzero_bits (varop, shift_mode) >> count))
9000            || (code == ASHIFT
9001                && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9002                && !((nonzero_bits (varop, shift_mode) << count)
9003                     & GET_MODE_MASK (shift_mode))))
9004           && !side_effects_p (varop))
9005         varop = const0_rtx;
9006
9007       switch (GET_CODE (varop))
9008         {
9009         case SIGN_EXTEND:
9010         case ZERO_EXTEND:
9011         case SIGN_EXTRACT:
9012         case ZERO_EXTRACT:
9013           new = expand_compound_operation (varop);
9014           if (new != varop)
9015             {
9016               varop = new;
9017               continue;
9018             }
9019           break;
9020
9021         case MEM:
9022           /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9023              minus the width of a smaller mode, we can do this with a
9024              SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
9025           if ((code == ASHIFTRT || code == LSHIFTRT)
9026               && ! mode_dependent_address_p (XEXP (varop, 0))
9027               && ! MEM_VOLATILE_P (varop)
9028               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9029                                          MODE_INT, 1)) != BLKmode)
9030             {
9031               new = adjust_address_nv (varop, tmode,
9032                                        BYTES_BIG_ENDIAN ? 0
9033                                        : count / BITS_PER_UNIT);
9034
9035               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9036                                      : ZERO_EXTEND, mode, new);
9037               count = 0;
9038               continue;
9039             }
9040           break;
9041
9042         case SUBREG:
9043           /* If VAROP is a SUBREG, strip it as long as the inner operand has
9044              the same number of words as what we've seen so far.  Then store
9045              the widest mode in MODE.  */
9046           if (subreg_lowpart_p (varop)
9047               && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9048                   > GET_MODE_SIZE (GET_MODE (varop)))
9049               && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9050                                   + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9051                  == mode_words)
9052             {
9053               varop = SUBREG_REG (varop);
9054               if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9055                 mode = GET_MODE (varop);
9056               continue;
9057             }
9058           break;
9059
9060         case MULT:
9061           /* Some machines use MULT instead of ASHIFT because MULT
9062              is cheaper.  But it is still better on those machines to
9063              merge two shifts into one.  */
9064           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9065               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9066             {
9067               varop
9068                 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
9069                                        XEXP (varop, 0),
9070                                        GEN_INT (exact_log2 (
9071                                                 INTVAL (XEXP (varop, 1)))));
9072               continue;
9073             }
9074           break;
9075
9076         case UDIV:
9077           /* Similar, for when divides are cheaper.  */
9078           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9079               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9080             {
9081               varop
9082                 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
9083                                        XEXP (varop, 0),
9084                                        GEN_INT (exact_log2 (
9085                                                 INTVAL (XEXP (varop, 1)))));
9086               continue;
9087             }
9088           break;
9089
9090         case ASHIFTRT:
9091           /* If we are extracting just the sign bit of an arithmetic
9092              right shift, that shift is not needed.  However, the sign
9093              bit of a wider mode may be different from what would be
9094              interpreted as the sign bit in a narrower mode, so, if
9095              the result is narrower, don't discard the shift.  */
9096           if (code == LSHIFTRT
9097               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9098               && (GET_MODE_BITSIZE (result_mode)
9099                   >= GET_MODE_BITSIZE (GET_MODE (varop))))
9100             {
9101               varop = XEXP (varop, 0);
9102               continue;
9103             }
9104
9105           /* ... fall through ...  */
9106
9107         case LSHIFTRT:
9108         case ASHIFT:
9109         case ROTATE:
9110           /* Here we have two nested shifts.  The result is usually the
9111              AND of a new shift with a mask.  We compute the result below.  */
9112           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9113               && INTVAL (XEXP (varop, 1)) >= 0
9114               && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9115               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9116               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9117               && !VECTOR_MODE_P (result_mode))
9118             {
9119               enum rtx_code first_code = GET_CODE (varop);
9120               unsigned int first_count = INTVAL (XEXP (varop, 1));
9121               unsigned HOST_WIDE_INT mask;
9122               rtx mask_rtx;
9123
9124               /* We have one common special case.  We can't do any merging if
9125                  the inner code is an ASHIFTRT of a smaller mode.  However, if
9126                  we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9127                  with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9128                  we can convert it to
9129                  (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9130                  This simplifies certain SIGN_EXTEND operations.  */
9131               if (code == ASHIFT && first_code == ASHIFTRT
9132                   && count == (GET_MODE_BITSIZE (result_mode)
9133                                - GET_MODE_BITSIZE (GET_MODE (varop))))
9134                 {
9135                   /* C3 has the low-order C1 bits zero.  */
9136
9137                   mask = (GET_MODE_MASK (mode)
9138                           & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
9139
9140                   varop = simplify_and_const_int (NULL_RTX, result_mode,
9141                                                   XEXP (varop, 0), mask);
9142                   varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
9143                                                 varop, count);
9144                   count = first_count;
9145                   code = ASHIFTRT;
9146                   continue;
9147                 }
9148
9149               /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
9150                  than C1 high-order bits equal to the sign bit, we can convert
9151                  this to either an ASHIFT or an ASHIFTRT depending on the
9152                  two counts.
9153
9154                  We cannot do this if VAROP's mode is not SHIFT_MODE.  */
9155
9156               if (code == ASHIFTRT && first_code == ASHIFT
9157                   && GET_MODE (varop) == shift_mode
9158                   && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
9159                       > first_count))
9160                 {
9161                   varop = XEXP (varop, 0);
9162                   count -= first_count;
9163                   if (count < 0)
9164                     {
9165                       count = -count;
9166                       code = ASHIFT;
9167                     }
9168
9169                   continue;
9170                 }
9171
9172               /* There are some cases we can't do.  If CODE is ASHIFTRT,
9173                  we can only do this if FIRST_CODE is also ASHIFTRT.
9174
9175                  We can't do the case when CODE is ROTATE and FIRST_CODE is
9176                  ASHIFTRT.
9177
9178                  If the mode of this shift is not the mode of the outer shift,
9179                  we can't do this if either shift is a right shift or ROTATE.
9180
9181                  Finally, we can't do any of these if the mode is too wide
9182                  unless the codes are the same.
9183
9184                  Handle the case where the shift codes are the same
9185                  first.  */
9186
9187               if (code == first_code)
9188                 {
9189                   if (GET_MODE (varop) != result_mode
9190                       && (code == ASHIFTRT || code == LSHIFTRT
9191                           || code == ROTATE))
9192                     break;
9193
9194                   count += first_count;
9195                   varop = XEXP (varop, 0);
9196                   continue;
9197                 }
9198
9199               if (code == ASHIFTRT
9200                   || (code == ROTATE && first_code == ASHIFTRT)
9201                   || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
9202                   || (GET_MODE (varop) != result_mode
9203                       && (first_code == ASHIFTRT || first_code == LSHIFTRT
9204                           || first_code == ROTATE
9205                           || code == ROTATE)))
9206                 break;
9207
9208               /* To compute the mask to apply after the shift, shift the
9209                  nonzero bits of the inner shift the same way the
9210                  outer shift will.  */
9211
9212               mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
9213
9214               mask_rtx
9215                 = simplify_const_binary_operation (code, result_mode, mask_rtx,
9216                                                    GEN_INT (count));
9217
9218               /* Give up if we can't compute an outer operation to use.  */
9219               if (mask_rtx == 0
9220                   || GET_CODE (mask_rtx) != CONST_INT
9221                   || ! merge_outer_ops (&outer_op, &outer_const, AND,
9222                                         INTVAL (mask_rtx),
9223                                         result_mode, &complement_p))
9224                 break;
9225
9226               /* If the shifts are in the same direction, we add the
9227                  counts.  Otherwise, we subtract them.  */
9228               if ((code == ASHIFTRT || code == LSHIFTRT)
9229                   == (first_code == ASHIFTRT || first_code == LSHIFTRT))
9230                 count += first_count;
9231               else
9232                 count -= first_count;
9233
9234               /* If COUNT is positive, the new shift is usually CODE,
9235                  except for the two exceptions below, in which case it is
9236                  FIRST_CODE.  If the count is negative, FIRST_CODE should
9237                  always be used  */
9238               if (count > 0
9239                   && ((first_code == ROTATE && code == ASHIFT)
9240                       || (first_code == ASHIFTRT && code == LSHIFTRT)))
9241                 code = first_code;
9242               else if (count < 0)
9243                 code = first_code, count = -count;
9244
9245               varop = XEXP (varop, 0);
9246               continue;
9247             }
9248
9249           /* If we have (A << B << C) for any shift, we can convert this to
9250              (A << C << B).  This wins if A is a constant.  Only try this if
9251              B is not a constant.  */
9252
9253           else if (GET_CODE (varop) == code
9254                    && GET_CODE (XEXP (varop, 0)) == CONST_INT
9255                    && GET_CODE (XEXP (varop, 1)) != CONST_INT)
9256             {
9257               rtx new = simplify_const_binary_operation (code, mode,
9258                                                          XEXP (varop, 0),
9259                                                          GEN_INT (count));
9260               varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
9261               count = 0;
9262               continue;
9263             }
9264           break;
9265
9266         case NOT:
9267           /* Make this fit the case below.  */
9268           varop = gen_rtx_XOR (mode, XEXP (varop, 0),
9269                                GEN_INT (GET_MODE_MASK (mode)));
9270           continue;
9271
9272         case IOR:
9273         case AND:
9274         case XOR:
9275           /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9276              with C the size of VAROP - 1 and the shift is logical if
9277              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9278              we have an (le X 0) operation.   If we have an arithmetic shift
9279              and STORE_FLAG_VALUE is 1 or we have a logical shift with
9280              STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
9281
9282           if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
9283               && XEXP (XEXP (varop, 0), 1) == constm1_rtx
9284               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9285               && (code == LSHIFTRT || code == ASHIFTRT)
9286               && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9287               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9288             {
9289               count = 0;
9290               varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
9291                                   const0_rtx);
9292
9293               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9294                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9295
9296               continue;
9297             }
9298
9299           /* If we have (shift (logical)), move the logical to the outside
9300              to allow it to possibly combine with another logical and the
9301              shift to combine with another shift.  This also canonicalizes to
9302              what a ZERO_EXTRACT looks like.  Also, some machines have
9303              (and (shift)) insns.  */
9304
9305           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9306               /* We can't do this if we have (ashiftrt (xor))  and the
9307                  constant has its sign bit set in shift_mode.  */
9308               && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9309                    && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9310                                               shift_mode))
9311               && (new = simplify_const_binary_operation (code, result_mode,
9312                                                          XEXP (varop, 1),
9313                                                          GEN_INT (count))) != 0
9314               && GET_CODE (new) == CONST_INT
9315               && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9316                                   INTVAL (new), result_mode, &complement_p))
9317             {
9318               varop = XEXP (varop, 0);
9319               continue;
9320             }
9321
9322           /* If we can't do that, try to simplify the shift in each arm of the
9323              logical expression, make a new logical expression, and apply
9324              the inverse distributive law.  This also can't be done
9325              for some (ashiftrt (xor)).  */
9326           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9327              && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9328                   && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9329                                              shift_mode)))
9330             {
9331               rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9332                                               XEXP (varop, 0), count);
9333               rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9334                                               XEXP (varop, 1), count);
9335
9336               varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
9337                                            lhs, rhs);
9338               varop = apply_distributive_law (varop);
9339
9340               count = 0;
9341               continue;
9342             }
9343           break;
9344
9345         case EQ:
9346           /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9347              says that the sign bit can be tested, FOO has mode MODE, C is
9348              GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9349              that may be nonzero.  */
9350           if (code == LSHIFTRT
9351               && XEXP (varop, 1) == const0_rtx
9352               && GET_MODE (XEXP (varop, 0)) == result_mode
9353               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9354               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9355               && STORE_FLAG_VALUE == -1
9356               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9357               && merge_outer_ops (&outer_op, &outer_const, XOR,
9358                                   (HOST_WIDE_INT) 1, result_mode,
9359                                   &complement_p))
9360             {
9361               varop = XEXP (varop, 0);
9362               count = 0;
9363               continue;
9364             }
9365           break;
9366
9367         case NEG:
9368           /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9369              than the number of bits in the mode is equivalent to A.  */
9370           if (code == LSHIFTRT
9371               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9372               && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9373             {
9374               varop = XEXP (varop, 0);
9375               count = 0;
9376               continue;
9377             }
9378
9379           /* NEG commutes with ASHIFT since it is multiplication.  Move the
9380              NEG outside to allow shifts to combine.  */
9381           if (code == ASHIFT
9382               && merge_outer_ops (&outer_op, &outer_const, NEG,
9383                                   (HOST_WIDE_INT) 0, result_mode,
9384                                   &complement_p))
9385             {
9386               varop = XEXP (varop, 0);
9387               continue;
9388             }
9389           break;
9390
9391         case PLUS:
9392           /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9393              is one less than the number of bits in the mode is
9394              equivalent to (xor A 1).  */
9395           if (code == LSHIFTRT
9396               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9397               && XEXP (varop, 1) == constm1_rtx
9398               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9399               && merge_outer_ops (&outer_op, &outer_const, XOR,
9400                                   (HOST_WIDE_INT) 1, result_mode,
9401                                   &complement_p))
9402             {
9403               count = 0;
9404               varop = XEXP (varop, 0);
9405               continue;
9406             }
9407
9408           /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9409              that might be nonzero in BAR are those being shifted out and those
9410              bits are known zero in FOO, we can replace the PLUS with FOO.
9411              Similarly in the other operand order.  This code occurs when
9412              we are computing the size of a variable-size array.  */
9413
9414           if ((code == ASHIFTRT || code == LSHIFTRT)
9415               && count < HOST_BITS_PER_WIDE_INT
9416               && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9417               && (nonzero_bits (XEXP (varop, 1), result_mode)
9418                   & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9419             {
9420               varop = XEXP (varop, 0);
9421               continue;
9422             }
9423           else if ((code == ASHIFTRT || code == LSHIFTRT)
9424                    && count < HOST_BITS_PER_WIDE_INT
9425                    && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9426                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9427                             >> count)
9428                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9429                             & nonzero_bits (XEXP (varop, 1),
9430                                                  result_mode)))
9431             {
9432               varop = XEXP (varop, 1);
9433               continue;
9434             }
9435
9436           /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
9437           if (code == ASHIFT
9438               && GET_CODE (XEXP (varop, 1)) == CONST_INT
9439               && (new = simplify_const_binary_operation (ASHIFT, result_mode,
9440                                                          XEXP (varop, 1),
9441                                                          GEN_INT (count))) != 0
9442               && GET_CODE (new) == CONST_INT
9443               && merge_outer_ops (&outer_op, &outer_const, PLUS,
9444                                   INTVAL (new), result_mode, &complement_p))
9445             {
9446               varop = XEXP (varop, 0);
9447               continue;
9448             }
9449
9450           /* Check for 'PLUS signbit', which is the canonical form of 'XOR
9451              signbit', and attempt to change the PLUS to an XOR and move it to
9452              the outer operation as is done above in the AND/IOR/XOR case
9453              leg for shift(logical). See details in logical handling above
9454              for reasoning in doing so.  */
9455           if (code == LSHIFTRT
9456               && GET_CODE (XEXP (varop, 1)) == CONST_INT
9457               && mode_signbit_p (result_mode, XEXP (varop, 1))
9458               && (new = simplify_const_binary_operation (code, result_mode,
9459                                                          XEXP (varop, 1),
9460                                                          GEN_INT (count))) != 0
9461               && GET_CODE (new) == CONST_INT
9462               && merge_outer_ops (&outer_op, &outer_const, XOR,
9463                                   INTVAL (new), result_mode, &complement_p))
9464             {
9465               varop = XEXP (varop, 0);
9466               continue;
9467             }
9468
9469           break;
9470
9471         case MINUS:
9472           /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9473              with C the size of VAROP - 1 and the shift is logical if
9474              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9475              we have a (gt X 0) operation.  If the shift is arithmetic with
9476              STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9477              we have a (neg (gt X 0)) operation.  */
9478
9479           if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9480               && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9481               && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9482               && (code == LSHIFTRT || code == ASHIFTRT)
9483               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9484               && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
9485               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9486             {
9487               count = 0;
9488               varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9489                                   const0_rtx);
9490
9491               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9492                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9493
9494               continue;
9495             }
9496           break;
9497
9498         case TRUNCATE:
9499           /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9500              if the truncate does not affect the value.  */
9501           if (code == LSHIFTRT
9502               && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9503               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9504               && (INTVAL (XEXP (XEXP (varop, 0), 1))
9505                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9506                       - GET_MODE_BITSIZE (GET_MODE (varop)))))
9507             {
9508               rtx varop_inner = XEXP (varop, 0);
9509
9510               varop_inner
9511                 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9512                                     XEXP (varop_inner, 0),
9513                                     GEN_INT
9514                                     (count + INTVAL (XEXP (varop_inner, 1))));
9515               varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9516               count = 0;
9517               continue;
9518             }
9519           break;
9520
9521         default:
9522           break;
9523         }
9524
9525       break;
9526     }
9527
9528   /* We need to determine what mode to do the shift in.  If the shift is
9529      a right shift or ROTATE, we must always do it in the mode it was
9530      originally done in.  Otherwise, we can do it in MODE, the widest mode
9531      encountered.  The code we care about is that of the shift that will
9532      actually be done, not the shift that was originally requested.  */
9533   shift_mode
9534     = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9535        ? result_mode : mode);
9536
9537   /* We have now finished analyzing the shift.  The result should be
9538      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
9539      OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
9540      to the result of the shift.  OUTER_CONST is the relevant constant,
9541      but we must turn off all bits turned off in the shift.  */
9542
9543   if (outer_op == UNKNOWN
9544       && orig_code == code && orig_count == count
9545       && varop == orig_varop
9546       && shift_mode == GET_MODE (varop))
9547     return NULL_RTX;
9548
9549   /* Make a SUBREG if necessary.  If we can't make it, fail.  */
9550   varop = gen_lowpart (shift_mode, varop);
9551   if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9552     return NULL_RTX;
9553
9554   /* If we have an outer operation and we just made a shift, it is
9555      possible that we could have simplified the shift were it not
9556      for the outer operation.  So try to do the simplification
9557      recursively.  */
9558
9559   if (outer_op != UNKNOWN)
9560     x = simplify_shift_const_1 (code, shift_mode, varop, count);
9561   else
9562     x = NULL_RTX;
9563
9564   if (x == NULL_RTX)
9565     x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
9566
9567   /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9568      turn off all the bits that the shift would have turned off.  */
9569   if (orig_code == LSHIFTRT && result_mode != shift_mode)
9570     x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9571                                 GET_MODE_MASK (result_mode) >> orig_count);
9572
9573   /* Do the remainder of the processing in RESULT_MODE.  */
9574   x = gen_lowpart_or_truncate (result_mode, x);
9575
9576   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9577      operation.  */
9578   if (complement_p)
9579     x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9580
9581   if (outer_op != UNKNOWN)
9582     {
9583       if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9584         outer_const = trunc_int_for_mode (outer_const, result_mode);
9585
9586       if (outer_op == AND)
9587         x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9588       else if (outer_op == SET)
9589         {
9590           /* This means that we have determined that the result is
9591              equivalent to a constant.  This should be rare.  */
9592           if (!side_effects_p (x))
9593             x = GEN_INT (outer_const);
9594         }
9595       else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
9596         x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
9597       else
9598         x = simplify_gen_binary (outer_op, result_mode, x,
9599                                  GEN_INT (outer_const));
9600     }
9601
9602   return x;
9603 }
9604
9605 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
9606    The result of the shift is RESULT_MODE.  If we cannot simplify it,
9607    return X or, if it is NULL, synthesize the expression with
9608    simplify_gen_binary.  Otherwise, return a simplified value.
9609
9610    The shift is normally computed in the widest mode we find in VAROP, as
9611    long as it isn't a different number of words than RESULT_MODE.  Exceptions
9612    are ASHIFTRT and ROTATE, which are always done in their original mode.  */
9613
9614 static rtx
9615 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
9616                       rtx varop, int count)
9617 {
9618   rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
9619   if (tem)
9620     return tem;
9621
9622   if (!x)
9623     x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
9624   if (GET_MODE (x) != result_mode)
9625     x = gen_lowpart (result_mode, x);
9626   return x;
9627 }
9628
9629 \f
9630 /* Like recog, but we receive the address of a pointer to a new pattern.
9631    We try to match the rtx that the pointer points to.
9632    If that fails, we may try to modify or replace the pattern,
9633    storing the replacement into the same pointer object.
9634
9635    Modifications include deletion or addition of CLOBBERs.
9636
9637    PNOTES is a pointer to a location where any REG_UNUSED notes added for
9638    the CLOBBERs are placed.
9639
9640    The value is the final insn code from the pattern ultimately matched,
9641    or -1.  */
9642
9643 static int
9644 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
9645 {
9646   rtx pat = *pnewpat;
9647   int insn_code_number;
9648   int num_clobbers_to_add = 0;
9649   int i;
9650   rtx notes = 0;
9651   rtx old_notes, old_pat;
9652
9653   /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9654      we use to indicate that something didn't match.  If we find such a
9655      thing, force rejection.  */
9656   if (GET_CODE (pat) == PARALLEL)
9657     for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9658       if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9659           && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9660         return -1;
9661
9662   old_pat = PATTERN (insn);
9663   old_notes = REG_NOTES (insn);
9664   PATTERN (insn) = pat;
9665   REG_NOTES (insn) = 0;
9666
9667   insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9668
9669   /* If it isn't, there is the possibility that we previously had an insn
9670      that clobbered some register as a side effect, but the combined
9671      insn doesn't need to do that.  So try once more without the clobbers
9672      unless this represents an ASM insn.  */
9673
9674   if (insn_code_number < 0 && ! check_asm_operands (pat)
9675       && GET_CODE (pat) == PARALLEL)
9676     {
9677       int pos;
9678
9679       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9680         if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9681           {
9682             if (i != pos)
9683               SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9684             pos++;
9685           }
9686
9687       SUBST_INT (XVECLEN (pat, 0), pos);
9688
9689       if (pos == 1)
9690         pat = XVECEXP (pat, 0, 0);
9691
9692       PATTERN (insn) = pat;
9693       insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9694     }
9695   PATTERN (insn) = old_pat;
9696   REG_NOTES (insn) = old_notes;
9697
9698   /* Recognize all noop sets, these will be killed by followup pass.  */
9699   if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
9700     insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
9701
9702   /* If we had any clobbers to add, make a new pattern than contains
9703      them.  Then check to make sure that all of them are dead.  */
9704   if (num_clobbers_to_add)
9705     {
9706       rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9707                                      rtvec_alloc (GET_CODE (pat) == PARALLEL
9708                                                   ? (XVECLEN (pat, 0)
9709                                                      + num_clobbers_to_add)
9710                                                   : num_clobbers_to_add + 1));
9711
9712       if (GET_CODE (pat) == PARALLEL)
9713         for (i = 0; i < XVECLEN (pat, 0); i++)
9714           XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9715       else
9716         XVECEXP (newpat, 0, 0) = pat;
9717
9718       add_clobbers (newpat, insn_code_number);
9719
9720       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9721            i < XVECLEN (newpat, 0); i++)
9722         {
9723           if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
9724               && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9725             return -1;
9726           if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH) 
9727             {
9728               gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
9729               notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9730                                          XEXP (XVECEXP (newpat, 0, i), 0), notes);
9731             }
9732         }
9733       pat = newpat;
9734     }
9735
9736   *pnewpat = pat;
9737   *pnotes = notes;
9738
9739   return insn_code_number;
9740 }
9741 \f
9742 /* Like gen_lowpart_general but for use by combine.  In combine it
9743    is not possible to create any new pseudoregs.  However, it is
9744    safe to create invalid memory addresses, because combine will
9745    try to recognize them and all they will do is make the combine
9746    attempt fail.
9747
9748    If for some reason this cannot do its job, an rtx
9749    (clobber (const_int 0)) is returned.
9750    An insn containing that will not be recognized.  */
9751
9752 static rtx
9753 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
9754 {
9755   enum machine_mode imode = GET_MODE (x);
9756   unsigned int osize = GET_MODE_SIZE (omode);
9757   unsigned int isize = GET_MODE_SIZE (imode);
9758   rtx result;
9759
9760   if (omode == imode)
9761     return x;
9762
9763   /* Return identity if this is a CONST or symbolic reference.  */
9764   if (omode == Pmode
9765       && (GET_CODE (x) == CONST
9766           || GET_CODE (x) == SYMBOL_REF
9767           || GET_CODE (x) == LABEL_REF))
9768     return x;
9769
9770   /* We can only support MODE being wider than a word if X is a
9771      constant integer or has a mode the same size.  */
9772   if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
9773       && ! ((imode == VOIDmode
9774              && (GET_CODE (x) == CONST_INT
9775                  || GET_CODE (x) == CONST_DOUBLE))
9776             || isize == osize))
9777     goto fail;
9778
9779   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
9780      won't know what to do.  So we will strip off the SUBREG here and
9781      process normally.  */
9782   if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
9783     {
9784       x = SUBREG_REG (x);
9785
9786       /* For use in case we fall down into the address adjustments
9787          further below, we need to adjust the known mode and size of
9788          x; imode and isize, since we just adjusted x.  */
9789       imode = GET_MODE (x);
9790
9791       if (imode == omode)
9792         return x;
9793
9794       isize = GET_MODE_SIZE (imode);
9795     }
9796
9797   result = gen_lowpart_common (omode, x);
9798
9799   if (result)
9800     return result;
9801
9802   if (MEM_P (x))
9803     {
9804       int offset = 0;
9805
9806       /* Refuse to work on a volatile memory ref or one with a mode-dependent
9807          address.  */
9808       if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9809         goto fail;
9810
9811       /* If we want to refer to something bigger than the original memref,
9812          generate a paradoxical subreg instead.  That will force a reload
9813          of the original memref X.  */
9814       if (isize < osize)
9815         return gen_rtx_SUBREG (omode, x, 0);
9816
9817       if (WORDS_BIG_ENDIAN)
9818         offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
9819
9820       /* Adjust the address so that the address-after-the-data is
9821          unchanged.  */
9822       if (BYTES_BIG_ENDIAN)
9823         offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
9824
9825       return adjust_address_nv (x, omode, offset);
9826     }
9827
9828   /* If X is a comparison operator, rewrite it in a new mode.  This
9829      probably won't match, but may allow further simplifications.  */
9830   else if (COMPARISON_P (x))
9831     return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
9832
9833   /* If we couldn't simplify X any other way, just enclose it in a
9834      SUBREG.  Normally, this SUBREG won't match, but some patterns may
9835      include an explicit SUBREG or we may simplify it further in combine.  */
9836   else
9837     {
9838       int offset = 0;
9839       rtx res;
9840
9841       offset = subreg_lowpart_offset (omode, imode);
9842       if (imode == VOIDmode)
9843         {
9844           imode = int_mode_for_mode (omode);
9845           x = gen_lowpart_common (imode, x);
9846           if (x == NULL)
9847             goto fail;
9848         }
9849       res = simplify_gen_subreg (omode, x, imode, offset);
9850       if (res)
9851         return res;
9852     }
9853
9854  fail:
9855   return gen_rtx_CLOBBER (imode, const0_rtx);
9856 }
9857 \f
9858 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9859    comparison code that will be tested.
9860
9861    The result is a possibly different comparison code to use.  *POP0 and
9862    *POP1 may be updated.
9863
9864    It is possible that we might detect that a comparison is either always
9865    true or always false.  However, we do not perform general constant
9866    folding in combine, so this knowledge isn't useful.  Such tautologies
9867    should have been detected earlier.  Hence we ignore all such cases.  */
9868
9869 static enum rtx_code
9870 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
9871 {
9872   rtx op0 = *pop0;
9873   rtx op1 = *pop1;
9874   rtx tem, tem1;
9875   int i;
9876   enum machine_mode mode, tmode;
9877
9878   /* Try a few ways of applying the same transformation to both operands.  */
9879   while (1)
9880     {
9881 #ifndef WORD_REGISTER_OPERATIONS
9882       /* The test below this one won't handle SIGN_EXTENDs on these machines,
9883          so check specially.  */
9884       if (code != GTU && code != GEU && code != LTU && code != LEU
9885           && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9886           && GET_CODE (XEXP (op0, 0)) == ASHIFT
9887           && GET_CODE (XEXP (op1, 0)) == ASHIFT
9888           && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9889           && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9890           && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9891               == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9892           && GET_CODE (XEXP (op0, 1)) == CONST_INT
9893           && XEXP (op0, 1) == XEXP (op1, 1)
9894           && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
9895           && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
9896           && (INTVAL (XEXP (op0, 1))
9897               == (GET_MODE_BITSIZE (GET_MODE (op0))
9898                   - (GET_MODE_BITSIZE
9899                      (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9900         {
9901           op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9902           op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9903         }
9904 #endif
9905
9906       /* If both operands are the same constant shift, see if we can ignore the
9907          shift.  We can if the shift is a rotate or if the bits shifted out of
9908          this shift are known to be zero for both inputs and if the type of
9909          comparison is compatible with the shift.  */
9910       if (GET_CODE (op0) == GET_CODE (op1)
9911           && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9912           && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
9913               || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
9914                   && (code != GT && code != LT && code != GE && code != LE))
9915               || (GET_CODE (op0) == ASHIFTRT
9916                   && (code != GTU && code != LTU
9917                       && code != GEU && code != LEU)))
9918           && GET_CODE (XEXP (op0, 1)) == CONST_INT
9919           && INTVAL (XEXP (op0, 1)) >= 0
9920           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9921           && XEXP (op0, 1) == XEXP (op1, 1))
9922         {
9923           enum machine_mode mode = GET_MODE (op0);
9924           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9925           int shift_count = INTVAL (XEXP (op0, 1));
9926
9927           if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9928             mask &= (mask >> shift_count) << shift_count;
9929           else if (GET_CODE (op0) == ASHIFT)
9930             mask = (mask & (mask << shift_count)) >> shift_count;
9931
9932           if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
9933               && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
9934             op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9935           else
9936             break;
9937         }
9938
9939       /* If both operands are AND's of a paradoxical SUBREG by constant, the
9940          SUBREGs are of the same mode, and, in both cases, the AND would
9941          be redundant if the comparison was done in the narrower mode,
9942          do the comparison in the narrower mode (e.g., we are AND'ing with 1
9943          and the operand's possibly nonzero bits are 0xffffff01; in that case
9944          if we only care about QImode, we don't need the AND).  This case
9945          occurs if the output mode of an scc insn is not SImode and
9946          STORE_FLAG_VALUE == 1 (e.g., the 386).
9947
9948          Similarly, check for a case where the AND's are ZERO_EXTEND
9949          operations from some narrower mode even though a SUBREG is not
9950          present.  */
9951
9952       else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9953                && GET_CODE (XEXP (op0, 1)) == CONST_INT
9954                && GET_CODE (XEXP (op1, 1)) == CONST_INT)
9955         {
9956           rtx inner_op0 = XEXP (op0, 0);
9957           rtx inner_op1 = XEXP (op1, 0);
9958           HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
9959           HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
9960           int changed = 0;
9961
9962           if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
9963               && (GET_MODE_SIZE (GET_MODE (inner_op0))
9964                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
9965               && (GET_MODE (SUBREG_REG (inner_op0))
9966                   == GET_MODE (SUBREG_REG (inner_op1)))
9967               && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
9968                   <= HOST_BITS_PER_WIDE_INT)
9969               && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
9970                                              GET_MODE (SUBREG_REG (inner_op0)))))
9971               && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
9972                                              GET_MODE (SUBREG_REG (inner_op1))))))
9973             {
9974               op0 = SUBREG_REG (inner_op0);
9975               op1 = SUBREG_REG (inner_op1);
9976
9977               /* The resulting comparison is always unsigned since we masked
9978                  off the original sign bit.  */
9979               code = unsigned_condition (code);
9980
9981               changed = 1;
9982             }
9983
9984           else if (c0 == c1)
9985             for (tmode = GET_CLASS_NARROWEST_MODE
9986                  (GET_MODE_CLASS (GET_MODE (op0)));
9987                  tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
9988               if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
9989                 {
9990                   op0 = gen_lowpart (tmode, inner_op0);
9991                   op1 = gen_lowpart (tmode, inner_op1);
9992                   code = unsigned_condition (code);
9993                   changed = 1;
9994                   break;
9995                 }
9996
9997           if (! changed)
9998             break;
9999         }
10000
10001       /* If both operands are NOT, we can strip off the outer operation
10002          and adjust the comparison code for swapped operands; similarly for
10003          NEG, except that this must be an equality comparison.  */
10004       else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10005                || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10006                    && (code == EQ || code == NE)))
10007         op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10008
10009       else
10010         break;
10011     }
10012
10013   /* If the first operand is a constant, swap the operands and adjust the
10014      comparison code appropriately, but don't do this if the second operand
10015      is already a constant integer.  */
10016   if (swap_commutative_operands_p (op0, op1))
10017     {
10018       tem = op0, op0 = op1, op1 = tem;
10019       code = swap_condition (code);
10020     }
10021
10022   /* We now enter a loop during which we will try to simplify the comparison.
10023      For the most part, we only are concerned with comparisons with zero,
10024      but some things may really be comparisons with zero but not start
10025      out looking that way.  */
10026
10027   while (GET_CODE (op1) == CONST_INT)
10028     {
10029       enum machine_mode mode = GET_MODE (op0);
10030       unsigned int mode_width = GET_MODE_BITSIZE (mode);
10031       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10032       int equality_comparison_p;
10033       int sign_bit_comparison_p;
10034       int unsigned_comparison_p;
10035       HOST_WIDE_INT const_op;
10036
10037       /* We only want to handle integral modes.  This catches VOIDmode,
10038          CCmode, and the floating-point modes.  An exception is that we
10039          can handle VOIDmode if OP0 is a COMPARE or a comparison
10040          operation.  */
10041
10042       if (GET_MODE_CLASS (mode) != MODE_INT
10043           && ! (mode == VOIDmode
10044                 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
10045         break;
10046
10047       /* Get the constant we are comparing against and turn off all bits
10048          not on in our mode.  */
10049       const_op = INTVAL (op1);
10050       if (mode != VOIDmode)
10051         const_op = trunc_int_for_mode (const_op, mode);
10052       op1 = GEN_INT (const_op);
10053
10054       /* If we are comparing against a constant power of two and the value
10055          being compared can only have that single bit nonzero (e.g., it was
10056          `and'ed with that bit), we can replace this with a comparison
10057          with zero.  */
10058       if (const_op
10059           && (code == EQ || code == NE || code == GE || code == GEU
10060               || code == LT || code == LTU)
10061           && mode_width <= HOST_BITS_PER_WIDE_INT
10062           && exact_log2 (const_op) >= 0
10063           && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10064         {
10065           code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10066           op1 = const0_rtx, const_op = 0;
10067         }
10068
10069       /* Similarly, if we are comparing a value known to be either -1 or
10070          0 with -1, change it to the opposite comparison against zero.  */
10071
10072       if (const_op == -1
10073           && (code == EQ || code == NE || code == GT || code == LE
10074               || code == GEU || code == LTU)
10075           && num_sign_bit_copies (op0, mode) == mode_width)
10076         {
10077           code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10078           op1 = const0_rtx, const_op = 0;
10079         }
10080
10081       /* Do some canonicalizations based on the comparison code.  We prefer
10082          comparisons against zero and then prefer equality comparisons.
10083          If we can reduce the size of a constant, we will do that too.  */
10084
10085       switch (code)
10086         {
10087         case LT:
10088           /* < C is equivalent to <= (C - 1) */
10089           if (const_op > 0)
10090             {
10091               const_op -= 1;
10092               op1 = GEN_INT (const_op);
10093               code = LE;
10094               /* ... fall through to LE case below.  */
10095             }
10096           else
10097             break;
10098
10099         case LE:
10100           /* <= C is equivalent to < (C + 1); we do this for C < 0  */
10101           if (const_op < 0)
10102             {
10103               const_op += 1;
10104               op1 = GEN_INT (const_op);
10105               code = LT;
10106             }
10107
10108           /* If we are doing a <= 0 comparison on a value known to have
10109              a zero sign bit, we can replace this with == 0.  */
10110           else if (const_op == 0
10111                    && mode_width <= HOST_BITS_PER_WIDE_INT
10112                    && (nonzero_bits (op0, mode)
10113                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10114             code = EQ;
10115           break;
10116
10117         case GE:
10118           /* >= C is equivalent to > (C - 1).  */
10119           if (const_op > 0)
10120             {
10121               const_op -= 1;
10122               op1 = GEN_INT (const_op);
10123               code = GT;
10124               /* ... fall through to GT below.  */
10125             }
10126           else
10127             break;
10128
10129         case GT:
10130           /* > C is equivalent to >= (C + 1); we do this for C < 0.  */
10131           if (const_op < 0)
10132             {
10133               const_op += 1;
10134               op1 = GEN_INT (const_op);
10135               code = GE;
10136             }
10137
10138           /* If we are doing a > 0 comparison on a value known to have
10139              a zero sign bit, we can replace this with != 0.  */
10140           else if (const_op == 0
10141                    && mode_width <= HOST_BITS_PER_WIDE_INT
10142                    && (nonzero_bits (op0, mode)
10143                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10144             code = NE;
10145           break;
10146
10147         case LTU:
10148           /* < C is equivalent to <= (C - 1).  */
10149           if (const_op > 0)
10150             {
10151               const_op -= 1;
10152               op1 = GEN_INT (const_op);
10153               code = LEU;
10154               /* ... fall through ...  */
10155             }
10156
10157           /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
10158           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10159                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10160             {
10161               const_op = 0, op1 = const0_rtx;
10162               code = GE;
10163               break;
10164             }
10165           else
10166             break;
10167
10168         case LEU:
10169           /* unsigned <= 0 is equivalent to == 0 */
10170           if (const_op == 0)
10171             code = EQ;
10172
10173           /* (unsigned) <= 0x7fffffff is equivalent to >= 0.  */
10174           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10175                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10176             {
10177               const_op = 0, op1 = const0_rtx;
10178               code = GE;
10179             }
10180           break;
10181
10182         case GEU:
10183           /* >= C is equivalent to > (C - 1).  */
10184           if (const_op > 1)
10185             {
10186               const_op -= 1;
10187               op1 = GEN_INT (const_op);
10188               code = GTU;
10189               /* ... fall through ...  */
10190             }
10191
10192           /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
10193           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10194                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10195             {
10196               const_op = 0, op1 = const0_rtx;
10197               code = LT;
10198               break;
10199             }
10200           else
10201             break;
10202
10203         case GTU:
10204           /* unsigned > 0 is equivalent to != 0 */
10205           if (const_op == 0)
10206             code = NE;
10207
10208           /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
10209           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10210                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10211             {
10212               const_op = 0, op1 = const0_rtx;
10213               code = LT;
10214             }
10215           break;
10216
10217         default:
10218           break;
10219         }
10220
10221       /* Compute some predicates to simplify code below.  */
10222
10223       equality_comparison_p = (code == EQ || code == NE);
10224       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
10225       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
10226                                || code == GEU);
10227
10228       /* If this is a sign bit comparison and we can do arithmetic in
10229          MODE, say that we will only be needing the sign bit of OP0.  */
10230       if (sign_bit_comparison_p
10231           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10232         op0 = force_to_mode (op0, mode,
10233                              ((HOST_WIDE_INT) 1
10234                               << (GET_MODE_BITSIZE (mode) - 1)),
10235                              0);
10236
10237       /* Now try cases based on the opcode of OP0.  If none of the cases
10238          does a "continue", we exit this loop immediately after the
10239          switch.  */
10240
10241       switch (GET_CODE (op0))
10242         {
10243         case ZERO_EXTRACT:
10244           /* If we are extracting a single bit from a variable position in
10245              a constant that has only a single bit set and are comparing it
10246              with zero, we can convert this into an equality comparison
10247              between the position and the location of the single bit.  */
10248           /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
10249              have already reduced the shift count modulo the word size.  */
10250           if (!SHIFT_COUNT_TRUNCATED
10251               && GET_CODE (XEXP (op0, 0)) == CONST_INT
10252               && XEXP (op0, 1) == const1_rtx
10253               && equality_comparison_p && const_op == 0
10254               && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
10255             {
10256               if (BITS_BIG_ENDIAN)
10257                 {
10258                   enum machine_mode new_mode
10259                     = mode_for_extraction (EP_extzv, 1);
10260                   if (new_mode == MAX_MACHINE_MODE)
10261                     i = BITS_PER_WORD - 1 - i;
10262                   else
10263                     {
10264                       mode = new_mode;
10265                       i = (GET_MODE_BITSIZE (mode) - 1 - i);
10266                     }
10267                 }
10268
10269               op0 = XEXP (op0, 2);
10270               op1 = GEN_INT (i);
10271               const_op = i;
10272
10273               /* Result is nonzero iff shift count is equal to I.  */
10274               code = reverse_condition (code);
10275               continue;
10276             }
10277
10278           /* ... fall through ...  */
10279
10280         case SIGN_EXTRACT:
10281           tem = expand_compound_operation (op0);
10282           if (tem != op0)
10283             {
10284               op0 = tem;
10285               continue;
10286             }
10287           break;
10288
10289         case NOT:
10290           /* If testing for equality, we can take the NOT of the constant.  */
10291           if (equality_comparison_p
10292               && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10293             {
10294               op0 = XEXP (op0, 0);
10295               op1 = tem;
10296               continue;
10297             }
10298
10299           /* If just looking at the sign bit, reverse the sense of the
10300              comparison.  */
10301           if (sign_bit_comparison_p)
10302             {
10303               op0 = XEXP (op0, 0);
10304               code = (code == GE ? LT : GE);
10305               continue;
10306             }
10307           break;
10308
10309         case NEG:
10310           /* If testing for equality, we can take the NEG of the constant.  */
10311           if (equality_comparison_p
10312               && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10313             {
10314               op0 = XEXP (op0, 0);
10315               op1 = tem;
10316               continue;
10317             }
10318
10319           /* The remaining cases only apply to comparisons with zero.  */
10320           if (const_op != 0)
10321             break;
10322
10323           /* When X is ABS or is known positive,
10324              (neg X) is < 0 if and only if X != 0.  */
10325
10326           if (sign_bit_comparison_p
10327               && (GET_CODE (XEXP (op0, 0)) == ABS
10328                   || (mode_width <= HOST_BITS_PER_WIDE_INT
10329                       && (nonzero_bits (XEXP (op0, 0), mode)
10330                           & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10331             {
10332               op0 = XEXP (op0, 0);
10333               code = (code == LT ? NE : EQ);
10334               continue;
10335             }
10336
10337           /* If we have NEG of something whose two high-order bits are the
10338              same, we know that "(-a) < 0" is equivalent to "a > 0".  */
10339           if (num_sign_bit_copies (op0, mode) >= 2)
10340             {
10341               op0 = XEXP (op0, 0);
10342               code = swap_condition (code);
10343               continue;
10344             }
10345           break;
10346
10347         case ROTATE:
10348           /* If we are testing equality and our count is a constant, we
10349              can perform the inverse operation on our RHS.  */
10350           if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10351               && (tem = simplify_binary_operation (ROTATERT, mode,
10352                                                    op1, XEXP (op0, 1))) != 0)
10353             {
10354               op0 = XEXP (op0, 0);
10355               op1 = tem;
10356               continue;
10357             }
10358
10359           /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10360              a particular bit.  Convert it to an AND of a constant of that
10361              bit.  This will be converted into a ZERO_EXTRACT.  */
10362           if (const_op == 0 && sign_bit_comparison_p
10363               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10364               && mode_width <= HOST_BITS_PER_WIDE_INT)
10365             {
10366               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10367                                             ((HOST_WIDE_INT) 1
10368                                              << (mode_width - 1
10369                                                  - INTVAL (XEXP (op0, 1)))));
10370               code = (code == LT ? NE : EQ);
10371               continue;
10372             }
10373
10374           /* Fall through.  */
10375
10376         case ABS:
10377           /* ABS is ignorable inside an equality comparison with zero.  */
10378           if (const_op == 0 && equality_comparison_p)
10379             {
10380               op0 = XEXP (op0, 0);
10381               continue;
10382             }
10383           break;
10384
10385         case SIGN_EXTEND:
10386           /* Can simplify (compare (zero/sign_extend FOO) CONST) to
10387              (compare FOO CONST) if CONST fits in FOO's mode and we
10388              are either testing inequality or have an unsigned
10389              comparison with ZERO_EXTEND or a signed comparison with
10390              SIGN_EXTEND.  But don't do it if we don't have a compare
10391              insn of the given mode, since we'd have to revert it
10392              later on, and then we wouldn't know whether to sign- or
10393              zero-extend.  */
10394           mode = GET_MODE (XEXP (op0, 0));
10395           if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10396               && ! unsigned_comparison_p
10397               && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10398               && ((unsigned HOST_WIDE_INT) const_op
10399                   < (((unsigned HOST_WIDE_INT) 1
10400                       << (GET_MODE_BITSIZE (mode) - 1))))
10401               && cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
10402             {
10403               op0 = XEXP (op0, 0);
10404               continue;
10405             }
10406           break;
10407
10408         case SUBREG:
10409           /* Check for the case where we are comparing A - C1 with C2, that is
10410
10411                (subreg:MODE (plus (A) (-C1))) op (C2)
10412
10413              with C1 a constant, and try to lift the SUBREG, i.e. to do the
10414              comparison in the wider mode.  One of the following two conditions
10415              must be true in order for this to be valid:
10416
10417                1. The mode extension results in the same bit pattern being added
10418                   on both sides and the comparison is equality or unsigned.  As
10419                   C2 has been truncated to fit in MODE, the pattern can only be
10420                   all 0s or all 1s.
10421
10422                2. The mode extension results in the sign bit being copied on
10423                   each side.
10424
10425              The difficulty here is that we have predicates for A but not for
10426              (A - C1) so we need to check that C1 is within proper bounds so
10427              as to perturbate A as little as possible.  */
10428
10429           if (mode_width <= HOST_BITS_PER_WIDE_INT
10430               && subreg_lowpart_p (op0)
10431               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
10432               && GET_CODE (SUBREG_REG (op0)) == PLUS
10433               && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT)
10434             {
10435               enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
10436               rtx a = XEXP (SUBREG_REG (op0), 0);
10437               HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
10438
10439               if ((c1 > 0
10440                    && (unsigned HOST_WIDE_INT) c1
10441                        < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
10442                    && (equality_comparison_p || unsigned_comparison_p)
10443                    /* (A - C1) zero-extends if it is positive and sign-extends
10444                       if it is negative, C2 both zero- and sign-extends.  */
10445                    && ((0 == (nonzero_bits (a, inner_mode)
10446                               & ~GET_MODE_MASK (mode))
10447                         && const_op >= 0)
10448                        /* (A - C1) sign-extends if it is positive and 1-extends
10449                           if it is negative, C2 both sign- and 1-extends.  */
10450                        || (num_sign_bit_copies (a, inner_mode)
10451                            > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10452                                              - mode_width)
10453                            && const_op < 0)))
10454                   || ((unsigned HOST_WIDE_INT) c1
10455                        < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
10456                       /* (A - C1) always sign-extends, like C2.  */
10457                       && num_sign_bit_copies (a, inner_mode)
10458                          > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10459                                            - (mode_width - 1))))
10460                 {
10461                   op0 = SUBREG_REG (op0);
10462                   continue;
10463                 }
10464             }
10465
10466           /* If the inner mode is narrower and we are extracting the low part,
10467              we can treat the SUBREG as if it were a ZERO_EXTEND.  */
10468           if (subreg_lowpart_p (op0)
10469               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10470             /* Fall through */ ;
10471           else
10472             break;
10473
10474           /* ... fall through ...  */
10475
10476         case ZERO_EXTEND:
10477           mode = GET_MODE (XEXP (op0, 0));
10478           if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10479               && (unsigned_comparison_p || equality_comparison_p)
10480               && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10481               && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
10482               && cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
10483             {
10484               op0 = XEXP (op0, 0);
10485               continue;
10486             }
10487           break;
10488
10489         case PLUS:
10490           /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
10491              this for equality comparisons due to pathological cases involving
10492              overflows.  */
10493           if (equality_comparison_p
10494               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10495                                                         op1, XEXP (op0, 1))))
10496             {
10497               op0 = XEXP (op0, 0);
10498               op1 = tem;
10499               continue;
10500             }
10501
10502           /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
10503           if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10504               && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10505             {
10506               op0 = XEXP (XEXP (op0, 0), 0);
10507               code = (code == LT ? EQ : NE);
10508               continue;
10509             }
10510           break;
10511
10512         case MINUS:
10513           /* We used to optimize signed comparisons against zero, but that
10514              was incorrect.  Unsigned comparisons against zero (GTU, LEU)
10515              arrive here as equality comparisons, or (GEU, LTU) are
10516              optimized away.  No need to special-case them.  */
10517
10518           /* (eq (minus A B) C) -> (eq A (plus B C)) or
10519              (eq B (minus A C)), whichever simplifies.  We can only do
10520              this for equality comparisons due to pathological cases involving
10521              overflows.  */
10522           if (equality_comparison_p
10523               && 0 != (tem = simplify_binary_operation (PLUS, mode,
10524                                                         XEXP (op0, 1), op1)))
10525             {
10526               op0 = XEXP (op0, 0);
10527               op1 = tem;
10528               continue;
10529             }
10530
10531           if (equality_comparison_p
10532               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10533                                                         XEXP (op0, 0), op1)))
10534             {
10535               op0 = XEXP (op0, 1);
10536               op1 = tem;
10537               continue;
10538             }
10539
10540           /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10541              of bits in X minus 1, is one iff X > 0.  */
10542           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10543               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10544               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10545                  == mode_width - 1
10546               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10547             {
10548               op0 = XEXP (op0, 1);
10549               code = (code == GE ? LE : GT);
10550               continue;
10551             }
10552           break;
10553
10554         case XOR:
10555           /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
10556              if C is zero or B is a constant.  */
10557           if (equality_comparison_p
10558               && 0 != (tem = simplify_binary_operation (XOR, mode,
10559                                                         XEXP (op0, 1), op1)))
10560             {
10561               op0 = XEXP (op0, 0);
10562               op1 = tem;
10563               continue;
10564             }
10565           break;
10566
10567         case EQ:  case NE:
10568         case UNEQ:  case LTGT:
10569         case LT:  case LTU:  case UNLT:  case LE:  case LEU:  case UNLE:
10570         case GT:  case GTU:  case UNGT:  case GE:  case GEU:  case UNGE:
10571         case UNORDERED: case ORDERED:
10572           /* We can't do anything if OP0 is a condition code value, rather
10573              than an actual data value.  */
10574           if (const_op != 0
10575               || CC0_P (XEXP (op0, 0))
10576               || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10577             break;
10578
10579           /* Get the two operands being compared.  */
10580           if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10581             tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10582           else
10583             tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10584
10585           /* Check for the cases where we simply want the result of the
10586              earlier test or the opposite of that result.  */
10587           if (code == NE || code == EQ
10588               || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10589                   && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10590                   && (STORE_FLAG_VALUE
10591                       & (((HOST_WIDE_INT) 1
10592                           << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10593                   && (code == LT || code == GE)))
10594             {
10595               enum rtx_code new_code;
10596               if (code == LT || code == NE)
10597                 new_code = GET_CODE (op0);
10598               else
10599                 new_code = reversed_comparison_code (op0, NULL);
10600
10601               if (new_code != UNKNOWN)
10602                 {
10603                   code = new_code;
10604                   op0 = tem;
10605                   op1 = tem1;
10606                   continue;
10607                 }
10608             }
10609           break;
10610
10611         case IOR:
10612           /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10613              iff X <= 0.  */
10614           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10615               && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10616               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10617             {
10618               op0 = XEXP (op0, 1);
10619               code = (code == GE ? GT : LE);
10620               continue;
10621             }
10622           break;
10623
10624         case AND:
10625           /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
10626              will be converted to a ZERO_EXTRACT later.  */
10627           if (const_op == 0 && equality_comparison_p
10628               && GET_CODE (XEXP (op0, 0)) == ASHIFT
10629               && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10630             {
10631               op0 = simplify_and_const_int
10632                 (NULL_RTX, mode, gen_rtx_LSHIFTRT (mode,
10633                                                    XEXP (op0, 1),
10634                                                    XEXP (XEXP (op0, 0), 1)),
10635                  (HOST_WIDE_INT) 1);
10636               continue;
10637             }
10638
10639           /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10640              zero and X is a comparison and C1 and C2 describe only bits set
10641              in STORE_FLAG_VALUE, we can compare with X.  */
10642           if (const_op == 0 && equality_comparison_p
10643               && mode_width <= HOST_BITS_PER_WIDE_INT
10644               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10645               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10646               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10647               && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10648               && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10649             {
10650               mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10651                       << INTVAL (XEXP (XEXP (op0, 0), 1)));
10652               if ((~STORE_FLAG_VALUE & mask) == 0
10653                   && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
10654                       || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10655                           && COMPARISON_P (tem))))
10656                 {
10657                   op0 = XEXP (XEXP (op0, 0), 0);
10658                   continue;
10659                 }
10660             }
10661
10662           /* If we are doing an equality comparison of an AND of a bit equal
10663              to the sign bit, replace this with a LT or GE comparison of
10664              the underlying value.  */
10665           if (equality_comparison_p
10666               && const_op == 0
10667               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10668               && mode_width <= HOST_BITS_PER_WIDE_INT
10669               && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10670                   == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10671             {
10672               op0 = XEXP (op0, 0);
10673               code = (code == EQ ? GE : LT);
10674               continue;
10675             }
10676
10677           /* If this AND operation is really a ZERO_EXTEND from a narrower
10678              mode, the constant fits within that mode, and this is either an
10679              equality or unsigned comparison, try to do this comparison in
10680              the narrower mode.
10681
10682              Note that in:
10683
10684              (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
10685              -> (ne:DI (reg:SI 4) (const_int 0))
10686
10687              unless TRULY_NOOP_TRUNCATION allows it or the register is
10688              known to hold a value of the required mode the
10689              transformation is invalid.  */
10690           if ((equality_comparison_p || unsigned_comparison_p)
10691               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10692               && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10693                                    & GET_MODE_MASK (mode))
10694                                   + 1)) >= 0
10695               && const_op >> i == 0
10696               && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
10697               && (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
10698                                          GET_MODE_BITSIZE (GET_MODE (op0)))
10699                   || (REG_P (XEXP (op0, 0))
10700                       && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
10701             {
10702               op0 = gen_lowpart (tmode, XEXP (op0, 0));
10703               continue;
10704             }
10705
10706           /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
10707              fits in both M1 and M2 and the SUBREG is either paradoxical
10708              or represents the low part, permute the SUBREG and the AND
10709              and try again.  */
10710           if (GET_CODE (XEXP (op0, 0)) == SUBREG)
10711             {
10712               unsigned HOST_WIDE_INT c1;
10713               tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
10714               /* Require an integral mode, to avoid creating something like
10715                  (AND:SF ...).  */
10716               if (SCALAR_INT_MODE_P (tmode)
10717                   /* It is unsafe to commute the AND into the SUBREG if the
10718                      SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
10719                      not defined.  As originally written the upper bits
10720                      have a defined value due to the AND operation.
10721                      However, if we commute the AND inside the SUBREG then
10722                      they no longer have defined values and the meaning of
10723                      the code has been changed.  */
10724                   && (0
10725 #ifdef WORD_REGISTER_OPERATIONS
10726                       || (mode_width > GET_MODE_BITSIZE (tmode)
10727                           && mode_width <= BITS_PER_WORD)
10728 #endif
10729                       || (mode_width <= GET_MODE_BITSIZE (tmode)
10730                           && subreg_lowpart_p (XEXP (op0, 0))))
10731                   && GET_CODE (XEXP (op0, 1)) == CONST_INT
10732                   && mode_width <= HOST_BITS_PER_WIDE_INT
10733                   && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
10734                   && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
10735                   && (c1 & ~GET_MODE_MASK (tmode)) == 0
10736                   && c1 != mask
10737                   && c1 != GET_MODE_MASK (tmode))
10738                 {
10739                   op0 = simplify_gen_binary (AND, tmode,
10740                                              SUBREG_REG (XEXP (op0, 0)),
10741                                              gen_int_mode (c1, tmode));
10742                   op0 = gen_lowpart (mode, op0);
10743                   continue;
10744                 }
10745             }
10746
10747           /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0).  */
10748           if (const_op == 0 && equality_comparison_p
10749               && XEXP (op0, 1) == const1_rtx
10750               && GET_CODE (XEXP (op0, 0)) == NOT)
10751             {
10752               op0 = simplify_and_const_int
10753                 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
10754               code = (code == NE ? EQ : NE);
10755               continue;
10756             }
10757
10758           /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
10759              (eq (and (lshiftrt X) 1) 0).
10760              Also handle the case where (not X) is expressed using xor.  */
10761           if (const_op == 0 && equality_comparison_p
10762               && XEXP (op0, 1) == const1_rtx
10763               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
10764             {
10765               rtx shift_op = XEXP (XEXP (op0, 0), 0);
10766               rtx shift_count = XEXP (XEXP (op0, 0), 1);
10767
10768               if (GET_CODE (shift_op) == NOT
10769                   || (GET_CODE (shift_op) == XOR
10770                       && GET_CODE (XEXP (shift_op, 1)) == CONST_INT
10771                       && GET_CODE (shift_count) == CONST_INT
10772                       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10773                       && (INTVAL (XEXP (shift_op, 1))
10774                           == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
10775                 {
10776                   op0 = simplify_and_const_int
10777                     (NULL_RTX, mode,
10778                      gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
10779                      (HOST_WIDE_INT) 1);
10780                   code = (code == NE ? EQ : NE);
10781                   continue;
10782                 }
10783             }
10784           break;
10785
10786         case ASHIFT:
10787           /* If we have (compare (ashift FOO N) (const_int C)) and
10788              the high order N bits of FOO (N+1 if an inequality comparison)
10789              are known to be zero, we can do this by comparing FOO with C
10790              shifted right N bits so long as the low-order N bits of C are
10791              zero.  */
10792           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10793               && INTVAL (XEXP (op0, 1)) >= 0
10794               && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10795                   < HOST_BITS_PER_WIDE_INT)
10796               && ((const_op
10797                    & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10798               && mode_width <= HOST_BITS_PER_WIDE_INT
10799               && (nonzero_bits (XEXP (op0, 0), mode)
10800                   & ~(mask >> (INTVAL (XEXP (op0, 1))
10801                                + ! equality_comparison_p))) == 0)
10802             {
10803               /* We must perform a logical shift, not an arithmetic one,
10804                  as we want the top N bits of C to be zero.  */
10805               unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
10806
10807               temp >>= INTVAL (XEXP (op0, 1));
10808               op1 = gen_int_mode (temp, mode);
10809               op0 = XEXP (op0, 0);
10810               continue;
10811             }
10812
10813           /* If we are doing a sign bit comparison, it means we are testing
10814              a particular bit.  Convert it to the appropriate AND.  */
10815           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10816               && mode_width <= HOST_BITS_PER_WIDE_INT)
10817             {
10818               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10819                                             ((HOST_WIDE_INT) 1
10820                                              << (mode_width - 1
10821                                                  - INTVAL (XEXP (op0, 1)))));
10822               code = (code == LT ? NE : EQ);
10823               continue;
10824             }
10825
10826           /* If this an equality comparison with zero and we are shifting
10827              the low bit to the sign bit, we can convert this to an AND of the
10828              low-order bit.  */
10829           if (const_op == 0 && equality_comparison_p
10830               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10831               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10832                  == mode_width - 1)
10833             {
10834               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10835                                             (HOST_WIDE_INT) 1);
10836               continue;
10837             }
10838           break;
10839
10840         case ASHIFTRT:
10841           /* If this is an equality comparison with zero, we can do this
10842              as a logical shift, which might be much simpler.  */
10843           if (equality_comparison_p && const_op == 0
10844               && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10845             {
10846               op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10847                                           XEXP (op0, 0),
10848                                           INTVAL (XEXP (op0, 1)));
10849               continue;
10850             }
10851
10852           /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10853              do the comparison in a narrower mode.  */
10854           if (! unsigned_comparison_p
10855               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10856               && GET_CODE (XEXP (op0, 0)) == ASHIFT
10857               && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10858               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10859                                          MODE_INT, 1)) != BLKmode
10860               && (((unsigned HOST_WIDE_INT) const_op
10861                    + (GET_MODE_MASK (tmode) >> 1) + 1)
10862                   <= GET_MODE_MASK (tmode)))
10863             {
10864               op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
10865               continue;
10866             }
10867
10868           /* Likewise if OP0 is a PLUS of a sign extension with a
10869              constant, which is usually represented with the PLUS
10870              between the shifts.  */
10871           if (! unsigned_comparison_p
10872               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10873               && GET_CODE (XEXP (op0, 0)) == PLUS
10874               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10875               && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
10876               && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
10877               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10878                                          MODE_INT, 1)) != BLKmode
10879               && (((unsigned HOST_WIDE_INT) const_op
10880                    + (GET_MODE_MASK (tmode) >> 1) + 1)
10881                   <= GET_MODE_MASK (tmode)))
10882             {
10883               rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
10884               rtx add_const = XEXP (XEXP (op0, 0), 1);
10885               rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
10886                                                    add_const, XEXP (op0, 1));
10887
10888               op0 = simplify_gen_binary (PLUS, tmode,
10889                                          gen_lowpart (tmode, inner),
10890                                          new_const);
10891               continue;
10892             }
10893
10894           /* ... fall through ...  */
10895         case LSHIFTRT:
10896           /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10897              the low order N bits of FOO are known to be zero, we can do this
10898              by comparing FOO with C shifted left N bits so long as no
10899              overflow occurs.  */
10900           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10901               && INTVAL (XEXP (op0, 1)) >= 0
10902               && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10903               && mode_width <= HOST_BITS_PER_WIDE_INT
10904               && (nonzero_bits (XEXP (op0, 0), mode)
10905                   & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
10906               && (((unsigned HOST_WIDE_INT) const_op
10907                    + (GET_CODE (op0) != LSHIFTRT
10908                       ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
10909                          + 1)
10910                       : 0))
10911                   <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
10912             {
10913               /* If the shift was logical, then we must make the condition
10914                  unsigned.  */
10915               if (GET_CODE (op0) == LSHIFTRT)
10916                 code = unsigned_condition (code);
10917
10918               const_op <<= INTVAL (XEXP (op0, 1));
10919               op1 = GEN_INT (const_op);
10920               op0 = XEXP (op0, 0);
10921               continue;
10922             }
10923
10924           /* If we are using this shift to extract just the sign bit, we
10925              can replace this with an LT or GE comparison.  */
10926           if (const_op == 0
10927               && (equality_comparison_p || sign_bit_comparison_p)
10928               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10929               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10930                  == mode_width - 1)
10931             {
10932               op0 = XEXP (op0, 0);
10933               code = (code == NE || code == GT ? LT : GE);
10934               continue;
10935             }
10936           break;
10937
10938         default:
10939           break;
10940         }
10941
10942       break;
10943     }
10944
10945   /* Now make any compound operations involved in this comparison.  Then,
10946      check for an outmost SUBREG on OP0 that is not doing anything or is
10947      paradoxical.  The latter transformation must only be performed when
10948      it is known that the "extra" bits will be the same in op0 and op1 or
10949      that they don't matter.  There are three cases to consider:
10950
10951      1. SUBREG_REG (op0) is a register.  In this case the bits are don't
10952      care bits and we can assume they have any convenient value.  So
10953      making the transformation is safe.
10954
10955      2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
10956      In this case the upper bits of op0 are undefined.  We should not make
10957      the simplification in that case as we do not know the contents of
10958      those bits.
10959
10960      3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
10961      UNKNOWN.  In that case we know those bits are zeros or ones.  We must
10962      also be sure that they are the same as the upper bits of op1.
10963
10964      We can never remove a SUBREG for a non-equality comparison because
10965      the sign bit is in a different place in the underlying object.  */
10966
10967   op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
10968   op1 = make_compound_operation (op1, SET);
10969
10970   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10971       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10972       && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
10973       && (code == NE || code == EQ))
10974     {
10975       if (GET_MODE_SIZE (GET_MODE (op0))
10976           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
10977         {
10978           /* For paradoxical subregs, allow case 1 as above.  Case 3 isn't
10979              implemented.  */
10980           if (REG_P (SUBREG_REG (op0)))
10981             {
10982               op0 = SUBREG_REG (op0);
10983               op1 = gen_lowpart (GET_MODE (op0), op1);
10984             }
10985         }
10986       else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10987                 <= HOST_BITS_PER_WIDE_INT)
10988                && (nonzero_bits (SUBREG_REG (op0),
10989                                  GET_MODE (SUBREG_REG (op0)))
10990                    & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
10991         {
10992           tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
10993
10994           if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
10995                & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
10996             op0 = SUBREG_REG (op0), op1 = tem;
10997         }
10998     }
10999
11000   /* We now do the opposite procedure: Some machines don't have compare
11001      insns in all modes.  If OP0's mode is an integer mode smaller than a
11002      word and we can't do a compare in that mode, see if there is a larger
11003      mode for which we can do the compare.  There are a number of cases in
11004      which we can use the wider mode.  */
11005
11006   mode = GET_MODE (op0);
11007   if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11008       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11009       && ! have_insn_for (COMPARE, mode))
11010     for (tmode = GET_MODE_WIDER_MODE (mode);
11011          (tmode != VOIDmode
11012           && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11013          tmode = GET_MODE_WIDER_MODE (tmode))
11014       if (have_insn_for (COMPARE, tmode))
11015         {
11016           int zero_extended;
11017
11018           /* If the only nonzero bits in OP0 and OP1 are those in the
11019              narrower mode and this is an equality or unsigned comparison,
11020              we can use the wider mode.  Similarly for sign-extended
11021              values, in which case it is true for all comparisons.  */
11022           zero_extended = ((code == EQ || code == NE
11023                             || code == GEU || code == GTU
11024                             || code == LEU || code == LTU)
11025                            && (nonzero_bits (op0, tmode)
11026                                & ~GET_MODE_MASK (mode)) == 0
11027                            && ((GET_CODE (op1) == CONST_INT
11028                                 || (nonzero_bits (op1, tmode)
11029                                     & ~GET_MODE_MASK (mode)) == 0)));
11030
11031           if (zero_extended
11032               || ((num_sign_bit_copies (op0, tmode)
11033                    > (unsigned int) (GET_MODE_BITSIZE (tmode)
11034                                      - GET_MODE_BITSIZE (mode)))
11035                   && (num_sign_bit_copies (op1, tmode)
11036                       > (unsigned int) (GET_MODE_BITSIZE (tmode)
11037                                         - GET_MODE_BITSIZE (mode)))))
11038             {
11039               /* If OP0 is an AND and we don't have an AND in MODE either,
11040                  make a new AND in the proper mode.  */
11041               if (GET_CODE (op0) == AND
11042                   && !have_insn_for (AND, mode))
11043                 op0 = simplify_gen_binary (AND, tmode,
11044                                            gen_lowpart (tmode,
11045                                                         XEXP (op0, 0)),
11046                                            gen_lowpart (tmode,
11047                                                         XEXP (op0, 1)));
11048
11049               op0 = gen_lowpart (tmode, op0);
11050               if (zero_extended && GET_CODE (op1) == CONST_INT)
11051                 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
11052               op1 = gen_lowpart (tmode, op1);
11053               break;
11054             }
11055
11056           /* If this is a test for negative, we can make an explicit
11057              test of the sign bit.  */
11058
11059           if (op1 == const0_rtx && (code == LT || code == GE)
11060               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11061             {
11062               op0 = simplify_gen_binary (AND, tmode,
11063                                          gen_lowpart (tmode, op0),
11064                                          GEN_INT ((HOST_WIDE_INT) 1
11065                                                   << (GET_MODE_BITSIZE (mode)
11066                                                       - 1)));
11067               code = (code == LT) ? NE : EQ;
11068               break;
11069             }
11070         }
11071
11072 #ifdef CANONICALIZE_COMPARISON
11073   /* If this machine only supports a subset of valid comparisons, see if we
11074      can convert an unsupported one into a supported one.  */
11075   CANONICALIZE_COMPARISON (code, op0, op1);
11076 #endif
11077
11078   *pop0 = op0;
11079   *pop1 = op1;
11080
11081   return code;
11082 }
11083 \f
11084 /* Utility function for record_value_for_reg.  Count number of
11085    rtxs in X.  */
11086 static int
11087 count_rtxs (rtx x)
11088 {
11089   enum rtx_code code = GET_CODE (x);
11090   const char *fmt;
11091   int i, ret = 1;
11092
11093   if (GET_RTX_CLASS (code) == '2'
11094       || GET_RTX_CLASS (code) == 'c')
11095     {
11096       rtx x0 = XEXP (x, 0);
11097       rtx x1 = XEXP (x, 1);
11098
11099       if (x0 == x1)
11100         return 1 + 2 * count_rtxs (x0);
11101
11102       if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11103            || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11104           && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11105         return 2 + 2 * count_rtxs (x0)
11106                + count_rtxs (x == XEXP (x1, 0)
11107                              ? XEXP (x1, 1) : XEXP (x1, 0));
11108
11109       if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11110            || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11111           && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11112         return 2 + 2 * count_rtxs (x1)
11113                + count_rtxs (x == XEXP (x0, 0)
11114                              ? XEXP (x0, 1) : XEXP (x0, 0));
11115     }
11116
11117   fmt = GET_RTX_FORMAT (code);
11118   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11119     if (fmt[i] == 'e')
11120       ret += count_rtxs (XEXP (x, i));
11121
11122   return ret;
11123 }
11124 \f
11125 /* Utility function for following routine.  Called when X is part of a value
11126    being stored into last_set_value.  Sets last_set_table_tick
11127    for each register mentioned.  Similar to mention_regs in cse.c  */
11128
11129 static void
11130 update_table_tick (rtx x)
11131 {
11132   enum rtx_code code = GET_CODE (x);
11133   const char *fmt = GET_RTX_FORMAT (code);
11134   int i;
11135
11136   if (code == REG)
11137     {
11138       unsigned int regno = REGNO (x);
11139       unsigned int endregno = END_REGNO (x);
11140       unsigned int r;
11141
11142       for (r = regno; r < endregno; r++)
11143         reg_stat[r].last_set_table_tick = label_tick;
11144
11145       return;
11146     }
11147
11148   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11149     /* Note that we can't have an "E" in values stored; see
11150        get_last_value_validate.  */
11151     if (fmt[i] == 'e')
11152       {
11153         /* Check for identical subexpressions.  If x contains
11154            identical subexpression we only have to traverse one of
11155            them.  */
11156         if (i == 0 && ARITHMETIC_P (x))
11157           {
11158             /* Note that at this point x1 has already been
11159                processed.  */
11160             rtx x0 = XEXP (x, 0);
11161             rtx x1 = XEXP (x, 1);
11162
11163             /* If x0 and x1 are identical then there is no need to
11164                process x0.  */
11165             if (x0 == x1)
11166               break;
11167
11168             /* If x0 is identical to a subexpression of x1 then while
11169                processing x1, x0 has already been processed.  Thus we
11170                are done with x.  */
11171             if (ARITHMETIC_P (x1)
11172                 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11173               break;
11174
11175             /* If x1 is identical to a subexpression of x0 then we
11176                still have to process the rest of x0.  */
11177             if (ARITHMETIC_P (x0)
11178                 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11179               {
11180                 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
11181                 break;
11182               }
11183           }
11184
11185         update_table_tick (XEXP (x, i));
11186       }
11187 }
11188
11189 /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
11190    are saying that the register is clobbered and we no longer know its
11191    value.  If INSN is zero, don't update reg_stat[].last_set; this is
11192    only permitted with VALUE also zero and is used to invalidate the
11193    register.  */
11194
11195 static void
11196 record_value_for_reg (rtx reg, rtx insn, rtx value)
11197 {
11198   unsigned int regno = REGNO (reg);
11199   unsigned int endregno = END_REGNO (reg);
11200   unsigned int i;
11201
11202   /* If VALUE contains REG and we have a previous value for REG, substitute
11203      the previous value.  */
11204   if (value && insn && reg_overlap_mentioned_p (reg, value))
11205     {
11206       rtx tem;
11207
11208       /* Set things up so get_last_value is allowed to see anything set up to
11209          our insn.  */
11210       subst_low_luid = DF_INSN_LUID (insn);
11211       tem = get_last_value (reg);
11212
11213       /* If TEM is simply a binary operation with two CLOBBERs as operands,
11214          it isn't going to be useful and will take a lot of time to process,
11215          so just use the CLOBBER.  */
11216
11217       if (tem)
11218         {
11219           if (ARITHMETIC_P (tem)
11220               && GET_CODE (XEXP (tem, 0)) == CLOBBER
11221               && GET_CODE (XEXP (tem, 1)) == CLOBBER)
11222             tem = XEXP (tem, 0);
11223           else if (count_occurrences (value, reg, 1) >= 2)
11224             {
11225               /* If there are two or more occurrences of REG in VALUE,
11226                  prevent the value from growing too much.  */
11227               if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
11228                 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
11229             }
11230
11231           value = replace_rtx (copy_rtx (value), reg, tem);
11232         }
11233     }
11234
11235   /* For each register modified, show we don't know its value, that
11236      we don't know about its bitwise content, that its value has been
11237      updated, and that we don't know the location of the death of the
11238      register.  */
11239   for (i = regno; i < endregno; i++)
11240     {
11241       if (insn)
11242         reg_stat[i].last_set = insn;
11243
11244       reg_stat[i].last_set_value = 0;
11245       reg_stat[i].last_set_mode = 0;
11246       reg_stat[i].last_set_nonzero_bits = 0;
11247       reg_stat[i].last_set_sign_bit_copies = 0;
11248       reg_stat[i].last_death = 0;
11249       reg_stat[i].truncated_to_mode = 0;
11250     }
11251
11252   /* Mark registers that are being referenced in this value.  */
11253   if (value)
11254     update_table_tick (value);
11255
11256   /* Now update the status of each register being set.
11257      If someone is using this register in this block, set this register
11258      to invalid since we will get confused between the two lives in this
11259      basic block.  This makes using this register always invalid.  In cse, we
11260      scan the table to invalidate all entries using this register, but this
11261      is too much work for us.  */
11262
11263   for (i = regno; i < endregno; i++)
11264     {
11265       reg_stat[i].last_set_label = label_tick;
11266       if (!insn
11267           || (value && reg_stat[i].last_set_table_tick >= label_tick_ebb_start))
11268         reg_stat[i].last_set_invalid = 1;
11269       else
11270         reg_stat[i].last_set_invalid = 0;
11271     }
11272
11273   /* The value being assigned might refer to X (like in "x++;").  In that
11274      case, we must replace it with (clobber (const_int 0)) to prevent
11275      infinite loops.  */
11276   if (value && ! get_last_value_validate (&value, insn,
11277                                           reg_stat[regno].last_set_label, 0))
11278     {
11279       value = copy_rtx (value);
11280       if (! get_last_value_validate (&value, insn,
11281                                      reg_stat[regno].last_set_label, 1))
11282         value = 0;
11283     }
11284
11285   /* For the main register being modified, update the value, the mode, the
11286      nonzero bits, and the number of sign bit copies.  */
11287
11288   reg_stat[regno].last_set_value = value;
11289
11290   if (value)
11291     {
11292       enum machine_mode mode = GET_MODE (reg);
11293       subst_low_luid = DF_INSN_LUID (insn);
11294       reg_stat[regno].last_set_mode = mode;
11295       if (GET_MODE_CLASS (mode) == MODE_INT
11296           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11297         mode = nonzero_bits_mode;
11298       reg_stat[regno].last_set_nonzero_bits = nonzero_bits (value, mode);
11299       reg_stat[regno].last_set_sign_bit_copies
11300         = num_sign_bit_copies (value, GET_MODE (reg));
11301     }
11302 }
11303
11304 /* Called via note_stores from record_dead_and_set_regs to handle one
11305    SET or CLOBBER in an insn.  DATA is the instruction in which the
11306    set is occurring.  */
11307
11308 static void
11309 record_dead_and_set_regs_1 (rtx dest, rtx setter, void *data)
11310 {
11311   rtx record_dead_insn = (rtx) data;
11312
11313   if (GET_CODE (dest) == SUBREG)
11314     dest = SUBREG_REG (dest);
11315
11316   if (!record_dead_insn)
11317     {
11318       if (REG_P (dest))
11319         record_value_for_reg (dest, NULL_RTX, NULL_RTX);
11320       return;
11321     }
11322
11323   if (REG_P (dest))
11324     {
11325       /* If we are setting the whole register, we know its value.  Otherwise
11326          show that we don't know the value.  We can handle SUBREG in
11327          some cases.  */
11328       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11329         record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11330       else if (GET_CODE (setter) == SET
11331                && GET_CODE (SET_DEST (setter)) == SUBREG
11332                && SUBREG_REG (SET_DEST (setter)) == dest
11333                && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11334                && subreg_lowpart_p (SET_DEST (setter)))
11335         record_value_for_reg (dest, record_dead_insn,
11336                               gen_lowpart (GET_MODE (dest),
11337                                                        SET_SRC (setter)));
11338       else
11339         record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11340     }
11341   else if (MEM_P (dest)
11342            /* Ignore pushes, they clobber nothing.  */
11343            && ! push_operand (dest, GET_MODE (dest)))
11344     mem_last_set = DF_INSN_LUID (record_dead_insn);
11345 }
11346
11347 /* Update the records of when each REG was most recently set or killed
11348    for the things done by INSN.  This is the last thing done in processing
11349    INSN in the combiner loop.
11350
11351    We update reg_stat[], in particular fields last_set, last_set_value,
11352    last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
11353    last_death, and also the similar information mem_last_set (which insn
11354    most recently modified memory) and last_call_luid (which insn was the
11355    most recent subroutine call).  */
11356
11357 static void
11358 record_dead_and_set_regs (rtx insn)
11359 {
11360   rtx link;
11361   unsigned int i;
11362
11363   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11364     {
11365       if (REG_NOTE_KIND (link) == REG_DEAD
11366           && REG_P (XEXP (link, 0)))
11367         {
11368           unsigned int regno = REGNO (XEXP (link, 0));
11369           unsigned int endregno = END_REGNO (XEXP (link, 0));
11370
11371           for (i = regno; i < endregno; i++)
11372             reg_stat[i].last_death = insn;
11373         }
11374       else if (REG_NOTE_KIND (link) == REG_INC)
11375         record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11376     }
11377
11378   if (CALL_P (insn))
11379     {
11380       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11381         if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11382           {
11383             reg_stat[i].last_set_invalid = 1;
11384             reg_stat[i].last_set = insn;
11385             reg_stat[i].last_set_value = 0;
11386             reg_stat[i].last_set_mode = 0;
11387             reg_stat[i].last_set_nonzero_bits = 0;
11388             reg_stat[i].last_set_sign_bit_copies = 0;
11389             reg_stat[i].last_death = 0;
11390             reg_stat[i].truncated_to_mode = 0;
11391           }
11392
11393       last_call_luid = mem_last_set = DF_INSN_LUID (insn);
11394
11395       /* We can't combine into a call pattern.  Remember, though, that
11396          the return value register is set at this LUID.  We could
11397          still replace a register with the return value from the
11398          wrong subroutine call!  */
11399       note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
11400     }
11401   else
11402     note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11403 }
11404
11405 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11406    register present in the SUBREG, so for each such SUBREG go back and
11407    adjust nonzero and sign bit information of the registers that are
11408    known to have some zero/sign bits set.
11409
11410    This is needed because when combine blows the SUBREGs away, the
11411    information on zero/sign bits is lost and further combines can be
11412    missed because of that.  */
11413
11414 static void
11415 record_promoted_value (rtx insn, rtx subreg)
11416 {
11417   rtx links, set;
11418   unsigned int regno = REGNO (SUBREG_REG (subreg));
11419   enum machine_mode mode = GET_MODE (subreg);
11420
11421   if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11422     return;
11423
11424   for (links = LOG_LINKS (insn); links;)
11425     {
11426       insn = XEXP (links, 0);
11427       set = single_set (insn);
11428
11429       if (! set || !REG_P (SET_DEST (set))
11430           || REGNO (SET_DEST (set)) != regno
11431           || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11432         {
11433           links = XEXP (links, 1);
11434           continue;
11435         }
11436
11437       if (reg_stat[regno].last_set == insn)
11438         {
11439           if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11440             reg_stat[regno].last_set_nonzero_bits &= GET_MODE_MASK (mode);
11441         }
11442
11443       if (REG_P (SET_SRC (set)))
11444         {
11445           regno = REGNO (SET_SRC (set));
11446           links = LOG_LINKS (insn);
11447         }
11448       else
11449         break;
11450     }
11451 }
11452
11453 /* Check if X, a register, is known to contain a value already
11454    truncated to MODE.  In this case we can use a subreg to refer to
11455    the truncated value even though in the generic case we would need
11456    an explicit truncation.  */
11457
11458 static bool
11459 reg_truncated_to_mode (enum machine_mode mode, rtx x)
11460 {
11461   enum machine_mode truncated = reg_stat[REGNO (x)].truncated_to_mode;
11462
11463   if (truncated == 0
11464       || reg_stat[REGNO (x)].truncation_label < label_tick_ebb_start)
11465     return false;
11466   if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
11467     return true;
11468   if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
11469                              GET_MODE_BITSIZE (truncated)))
11470     return true;
11471   return false;
11472 }
11473
11474 /* X is a REG or a SUBREG.  If X is some sort of a truncation record
11475    it.  For non-TRULY_NOOP_TRUNCATION targets we might be able to turn
11476    a truncate into a subreg using this information.  */
11477
11478 static void
11479 record_truncated_value (rtx x)
11480 {
11481   enum machine_mode truncated_mode;
11482
11483   if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
11484     {
11485       enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
11486       truncated_mode = GET_MODE (x);
11487
11488       if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
11489         return;
11490
11491       if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (truncated_mode),
11492                                  GET_MODE_BITSIZE (original_mode)))
11493         return;
11494
11495       x = SUBREG_REG (x);
11496     }
11497   /* ??? For hard-regs we now record everything.  We might be able to
11498      optimize this using last_set_mode.  */
11499   else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
11500     truncated_mode = GET_MODE (x);
11501   else
11502     return;
11503
11504   if (reg_stat[REGNO (x)].truncated_to_mode == 0
11505       || reg_stat[REGNO (x)].truncation_label < label_tick_ebb_start
11506       || (GET_MODE_SIZE (truncated_mode)
11507           < GET_MODE_SIZE (reg_stat[REGNO (x)].truncated_to_mode)))
11508     {
11509       reg_stat[REGNO (x)].truncated_to_mode = truncated_mode;
11510       reg_stat[REGNO (x)].truncation_label = label_tick;
11511     }
11512 }
11513
11514 /* Scan X for promoted SUBREGs and truncated REGs.  For each one
11515    found, note what it implies to the registers used in it.  */
11516
11517 static void
11518 check_conversions (rtx insn, rtx x)
11519 {
11520   if (GET_CODE (x) == SUBREG || REG_P (x))
11521     {
11522       if (GET_CODE (x) == SUBREG
11523           && SUBREG_PROMOTED_VAR_P (x)
11524           && REG_P (SUBREG_REG (x)))
11525         record_promoted_value (insn, x);
11526
11527       record_truncated_value (x);
11528     }
11529   else
11530     {
11531       const char *format = GET_RTX_FORMAT (GET_CODE (x));
11532       int i, j;
11533
11534       for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11535         switch (format[i])
11536           {
11537           case 'e':
11538             check_conversions (insn, XEXP (x, i));
11539             break;
11540           case 'V':
11541           case 'E':
11542             if (XVEC (x, i) != 0)
11543               for (j = 0; j < XVECLEN (x, i); j++)
11544                 check_conversions (insn, XVECEXP (x, i, j));
11545             break;
11546           }
11547     }
11548 }
11549 \f
11550 /* Utility routine for the following function.  Verify that all the registers
11551    mentioned in *LOC are valid when *LOC was part of a value set when
11552    label_tick == TICK.  Return 0 if some are not.
11553
11554    If REPLACE is nonzero, replace the invalid reference with
11555    (clobber (const_int 0)) and return 1.  This replacement is useful because
11556    we often can get useful information about the form of a value (e.g., if
11557    it was produced by a shift that always produces -1 or 0) even though
11558    we don't know exactly what registers it was produced from.  */
11559
11560 static int
11561 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
11562 {
11563   rtx x = *loc;
11564   const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11565   int len = GET_RTX_LENGTH (GET_CODE (x));
11566   int i;
11567
11568   if (REG_P (x))
11569     {
11570       unsigned int regno = REGNO (x);
11571       unsigned int endregno = END_REGNO (x);
11572       unsigned int j;
11573
11574       for (j = regno; j < endregno; j++)
11575         if (reg_stat[j].last_set_invalid
11576             /* If this is a pseudo-register that was only set once and not
11577                live at the beginning of the function, it is always valid.  */
11578             || (! (regno >= FIRST_PSEUDO_REGISTER
11579                    && REG_N_SETS (regno) == 1
11580                    && !REGNO_REG_SET_P
11581                        (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno))
11582                 && reg_stat[j].last_set_label > tick))
11583           {
11584             if (replace)
11585               *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11586             return replace;
11587           }
11588
11589       return 1;
11590     }
11591   /* If this is a memory reference, make sure that there were
11592      no stores after it that might have clobbered the value.  We don't
11593      have alias info, so we assume any store invalidates it.  */
11594   else if (MEM_P (x) && !MEM_READONLY_P (x)
11595            && DF_INSN_LUID (insn) <= mem_last_set)
11596     {
11597       if (replace)
11598         *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11599       return replace;
11600     }
11601
11602   for (i = 0; i < len; i++)
11603     {
11604       if (fmt[i] == 'e')
11605         {
11606           /* Check for identical subexpressions.  If x contains
11607              identical subexpression we only have to traverse one of
11608              them.  */
11609           if (i == 1 && ARITHMETIC_P (x))
11610             {
11611               /* Note that at this point x0 has already been checked
11612                  and found valid.  */
11613               rtx x0 = XEXP (x, 0);
11614               rtx x1 = XEXP (x, 1);
11615
11616               /* If x0 and x1 are identical then x is also valid.  */
11617               if (x0 == x1)
11618                 return 1;
11619
11620               /* If x1 is identical to a subexpression of x0 then
11621                  while checking x0, x1 has already been checked.  Thus
11622                  it is valid and so as x.  */
11623               if (ARITHMETIC_P (x0)
11624                   && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11625                 return 1;
11626
11627               /* If x0 is identical to a subexpression of x1 then x is
11628                  valid iff the rest of x1 is valid.  */
11629               if (ARITHMETIC_P (x1)
11630                   && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11631                 return
11632                   get_last_value_validate (&XEXP (x1,
11633                                                   x0 == XEXP (x1, 0) ? 1 : 0),
11634                                            insn, tick, replace);
11635             }
11636
11637           if (get_last_value_validate (&XEXP (x, i), insn, tick,
11638                                        replace) == 0)
11639             return 0;
11640         }
11641       /* Don't bother with these.  They shouldn't occur anyway.  */
11642       else if (fmt[i] == 'E')
11643         return 0;
11644     }
11645
11646   /* If we haven't found a reason for it to be invalid, it is valid.  */
11647   return 1;
11648 }
11649
11650 /* Get the last value assigned to X, if known.  Some registers
11651    in the value may be replaced with (clobber (const_int 0)) if their value
11652    is known longer known reliably.  */
11653
11654 static rtx
11655 get_last_value (rtx x)
11656 {
11657   unsigned int regno;
11658   rtx value;
11659
11660   /* If this is a non-paradoxical SUBREG, get the value of its operand and
11661      then convert it to the desired mode.  If this is a paradoxical SUBREG,
11662      we cannot predict what values the "extra" bits might have.  */
11663   if (GET_CODE (x) == SUBREG
11664       && subreg_lowpart_p (x)
11665       && (GET_MODE_SIZE (GET_MODE (x))
11666           <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11667       && (value = get_last_value (SUBREG_REG (x))) != 0)
11668     return gen_lowpart (GET_MODE (x), value);
11669
11670   if (!REG_P (x))
11671     return 0;
11672
11673   regno = REGNO (x);
11674   value = reg_stat[regno].last_set_value;
11675
11676   /* If we don't have a value, or if it isn't for this basic block and
11677      it's either a hard register, set more than once, or it's a live
11678      at the beginning of the function, return 0.
11679
11680      Because if it's not live at the beginning of the function then the reg
11681      is always set before being used (is never used without being set).
11682      And, if it's set only once, and it's always set before use, then all
11683      uses must have the same last value, even if it's not from this basic
11684      block.  */
11685
11686   if (value == 0
11687       || (reg_stat[regno].last_set_label < label_tick_ebb_start
11688           && (regno < FIRST_PSEUDO_REGISTER
11689               || REG_N_SETS (regno) != 1
11690               || REGNO_REG_SET_P
11691                  (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno))))
11692     return 0;
11693
11694   /* If the value was set in a later insn than the ones we are processing,
11695      we can't use it even if the register was only set once.  */
11696   if (reg_stat[regno].last_set_label == label_tick
11697       && DF_INSN_LUID (reg_stat[regno].last_set) >= subst_low_luid)
11698     return 0;
11699
11700   /* If the value has all its registers valid, return it.  */
11701   if (get_last_value_validate (&value, reg_stat[regno].last_set,
11702                                reg_stat[regno].last_set_label, 0))
11703     return value;
11704
11705   /* Otherwise, make a copy and replace any invalid register with
11706      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
11707
11708   value = copy_rtx (value);
11709   if (get_last_value_validate (&value, reg_stat[regno].last_set,
11710                                reg_stat[regno].last_set_label, 1))
11711     return value;
11712
11713   return 0;
11714 }
11715 \f
11716 /* Return nonzero if expression X refers to a REG or to memory
11717    that is set in an instruction more recent than FROM_LUID.  */
11718
11719 static int
11720 use_crosses_set_p (rtx x, int from_luid)
11721 {
11722   const char *fmt;
11723   int i;
11724   enum rtx_code code = GET_CODE (x);
11725
11726   if (code == REG)
11727     {
11728       unsigned int regno = REGNO (x);
11729       unsigned endreg = END_REGNO (x);
11730
11731 #ifdef PUSH_ROUNDING
11732       /* Don't allow uses of the stack pointer to be moved,
11733          because we don't know whether the move crosses a push insn.  */
11734       if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11735         return 1;
11736 #endif
11737       for (; regno < endreg; regno++)
11738         if (reg_stat[regno].last_set
11739             && reg_stat[regno].last_set_label == label_tick
11740             && DF_INSN_LUID (reg_stat[regno].last_set) > from_luid)
11741           return 1;
11742       return 0;
11743     }
11744
11745   if (code == MEM && mem_last_set > from_luid)
11746     return 1;
11747
11748   fmt = GET_RTX_FORMAT (code);
11749
11750   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11751     {
11752       if (fmt[i] == 'E')
11753         {
11754           int j;
11755           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11756             if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
11757               return 1;
11758         }
11759       else if (fmt[i] == 'e'
11760                && use_crosses_set_p (XEXP (x, i), from_luid))
11761         return 1;
11762     }
11763   return 0;
11764 }
11765 \f
11766 /* Define three variables used for communication between the following
11767    routines.  */
11768
11769 static unsigned int reg_dead_regno, reg_dead_endregno;
11770 static int reg_dead_flag;
11771
11772 /* Function called via note_stores from reg_dead_at_p.
11773
11774    If DEST is within [reg_dead_regno, reg_dead_endregno), set
11775    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
11776
11777 static void
11778 reg_dead_at_p_1 (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
11779 {
11780   unsigned int regno, endregno;
11781
11782   if (!REG_P (dest))
11783     return;
11784
11785   regno = REGNO (dest);
11786   endregno = END_REGNO (dest);
11787   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11788     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11789 }
11790
11791 /* Return nonzero if REG is known to be dead at INSN.
11792
11793    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
11794    referencing REG, it is dead.  If we hit a SET referencing REG, it is
11795    live.  Otherwise, see if it is live or dead at the start of the basic
11796    block we are in.  Hard regs marked as being live in NEWPAT_USED_REGS
11797    must be assumed to be always live.  */
11798
11799 static int
11800 reg_dead_at_p (rtx reg, rtx insn)
11801 {
11802   basic_block block;
11803   unsigned int i;
11804
11805   /* Set variables for reg_dead_at_p_1.  */
11806   reg_dead_regno = REGNO (reg);
11807   reg_dead_endregno = END_REGNO (reg);
11808
11809   reg_dead_flag = 0;
11810
11811   /* Check that reg isn't mentioned in NEWPAT_USED_REGS.  For fixed registers
11812      we allow the machine description to decide whether use-and-clobber
11813      patterns are OK.  */
11814   if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11815     {
11816       for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11817         if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
11818           return 0;
11819     }
11820
11821   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11822      beginning of function.  */
11823   for (; insn && !LABEL_P (insn) && !BARRIER_P (insn);
11824        insn = prev_nonnote_insn (insn))
11825     {
11826       note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
11827       if (reg_dead_flag)
11828         return reg_dead_flag == 1 ? 1 : 0;
11829
11830       if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11831         return 1;
11832     }
11833
11834   /* Get the basic block that we were in.  */
11835   if (insn == 0)
11836     block = ENTRY_BLOCK_PTR->next_bb;
11837   else
11838     {
11839       FOR_EACH_BB (block)
11840         if (insn == BB_HEAD (block))
11841           break;
11842
11843       if (block == EXIT_BLOCK_PTR)
11844         return 0;
11845     }
11846
11847   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11848     if (REGNO_REG_SET_P (df_get_live_in (block), i))
11849       return 0;
11850
11851   return 1;
11852 }
11853 \f
11854 /* Note hard registers in X that are used.  */
11855
11856 static void
11857 mark_used_regs_combine (rtx x)
11858 {
11859   RTX_CODE code = GET_CODE (x);
11860   unsigned int regno;
11861   int i;
11862
11863   switch (code)
11864     {
11865     case LABEL_REF:
11866     case SYMBOL_REF:
11867     case CONST_INT:
11868     case CONST:
11869     case CONST_DOUBLE:
11870     case CONST_VECTOR:
11871     case PC:
11872     case ADDR_VEC:
11873     case ADDR_DIFF_VEC:
11874     case ASM_INPUT:
11875 #ifdef HAVE_cc0
11876     /* CC0 must die in the insn after it is set, so we don't need to take
11877        special note of it here.  */
11878     case CC0:
11879 #endif
11880       return;
11881
11882     case CLOBBER:
11883       /* If we are clobbering a MEM, mark any hard registers inside the
11884          address as used.  */
11885       if (MEM_P (XEXP (x, 0)))
11886         mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
11887       return;
11888
11889     case REG:
11890       regno = REGNO (x);
11891       /* A hard reg in a wide mode may really be multiple registers.
11892          If so, mark all of them just like the first.  */
11893       if (regno < FIRST_PSEUDO_REGISTER)
11894         {
11895           /* None of this applies to the stack, frame or arg pointers.  */
11896           if (regno == STACK_POINTER_REGNUM
11897 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11898               || regno == HARD_FRAME_POINTER_REGNUM
11899 #endif
11900 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
11901               || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
11902 #endif
11903               || regno == FRAME_POINTER_REGNUM)
11904             return;
11905
11906           add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
11907         }
11908       return;
11909
11910     case SET:
11911       {
11912         /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11913            the address.  */
11914         rtx testreg = SET_DEST (x);
11915
11916         while (GET_CODE (testreg) == SUBREG
11917                || GET_CODE (testreg) == ZERO_EXTRACT
11918                || GET_CODE (testreg) == STRICT_LOW_PART)
11919           testreg = XEXP (testreg, 0);
11920
11921         if (MEM_P (testreg))
11922           mark_used_regs_combine (XEXP (testreg, 0));
11923
11924         mark_used_regs_combine (SET_SRC (x));
11925       }
11926       return;
11927
11928     default:
11929       break;
11930     }
11931
11932   /* Recursively scan the operands of this expression.  */
11933
11934   {
11935     const char *fmt = GET_RTX_FORMAT (code);
11936
11937     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11938       {
11939         if (fmt[i] == 'e')
11940           mark_used_regs_combine (XEXP (x, i));
11941         else if (fmt[i] == 'E')
11942           {
11943             int j;
11944
11945             for (j = 0; j < XVECLEN (x, i); j++)
11946               mark_used_regs_combine (XVECEXP (x, i, j));
11947           }
11948       }
11949   }
11950 }
11951 \f
11952 /* Remove register number REGNO from the dead registers list of INSN.
11953
11954    Return the note used to record the death, if there was one.  */
11955
11956 rtx
11957 remove_death (unsigned int regno, rtx insn)
11958 {
11959   rtx note = find_regno_note (insn, REG_DEAD, regno);
11960
11961   if (note)
11962     remove_note (insn, note);
11963
11964   return note;
11965 }
11966
11967 /* For each register (hardware or pseudo) used within expression X, if its
11968    death is in an instruction with luid between FROM_LUID (inclusive) and
11969    TO_INSN (exclusive), put a REG_DEAD note for that register in the
11970    list headed by PNOTES.
11971
11972    That said, don't move registers killed by maybe_kill_insn.
11973
11974    This is done when X is being merged by combination into TO_INSN.  These
11975    notes will then be distributed as needed.  */
11976
11977 static void
11978 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx to_insn,
11979              rtx *pnotes)
11980 {
11981   const char *fmt;
11982   int len, i;
11983   enum rtx_code code = GET_CODE (x);
11984
11985   if (code == REG)
11986     {
11987       unsigned int regno = REGNO (x);
11988       rtx where_dead = reg_stat[regno].last_death;
11989
11990       /* Don't move the register if it gets killed in between from and to.  */
11991       if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
11992           && ! reg_referenced_p (x, maybe_kill_insn))
11993         return;
11994
11995       if (where_dead
11996           && DF_INSN_LUID (where_dead) >= from_luid
11997           && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
11998         {
11999           rtx note = remove_death (regno, where_dead);
12000
12001           /* It is possible for the call above to return 0.  This can occur
12002              when last_death points to I2 or I1 that we combined with.
12003              In that case make a new note.
12004
12005              We must also check for the case where X is a hard register
12006              and NOTE is a death note for a range of hard registers
12007              including X.  In that case, we must put REG_DEAD notes for
12008              the remaining registers in place of NOTE.  */
12009
12010           if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12011               && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12012                   > GET_MODE_SIZE (GET_MODE (x))))
12013             {
12014               unsigned int deadregno = REGNO (XEXP (note, 0));
12015               unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
12016               unsigned int ourend = END_HARD_REGNO (x);
12017               unsigned int i;
12018
12019               for (i = deadregno; i < deadend; i++)
12020                 if (i < regno || i >= ourend)
12021                   REG_NOTES (where_dead)
12022                     = gen_rtx_EXPR_LIST (REG_DEAD,
12023                                          regno_reg_rtx[i],
12024                                          REG_NOTES (where_dead));
12025             }
12026
12027           /* If we didn't find any note, or if we found a REG_DEAD note that
12028              covers only part of the given reg, and we have a multi-reg hard
12029              register, then to be safe we must check for REG_DEAD notes
12030              for each register other than the first.  They could have
12031              their own REG_DEAD notes lying around.  */
12032           else if ((note == 0
12033                     || (note != 0
12034                         && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12035                             < GET_MODE_SIZE (GET_MODE (x)))))
12036                    && regno < FIRST_PSEUDO_REGISTER
12037                    && hard_regno_nregs[regno][GET_MODE (x)] > 1)
12038             {
12039               unsigned int ourend = END_HARD_REGNO (x);
12040               unsigned int i, offset;
12041               rtx oldnotes = 0;
12042
12043               if (note)
12044                 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
12045               else
12046                 offset = 1;
12047
12048               for (i = regno + offset; i < ourend; i++)
12049                 move_deaths (regno_reg_rtx[i],
12050                              maybe_kill_insn, from_luid, to_insn, &oldnotes);
12051             }
12052
12053           if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12054             {
12055               XEXP (note, 1) = *pnotes;
12056               *pnotes = note;
12057             }
12058           else
12059             *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
12060         }
12061
12062       return;
12063     }
12064
12065   else if (GET_CODE (x) == SET)
12066     {
12067       rtx dest = SET_DEST (x);
12068
12069       move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
12070
12071       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
12072          that accesses one word of a multi-word item, some
12073          piece of everything register in the expression is used by
12074          this insn, so remove any old death.  */
12075       /* ??? So why do we test for equality of the sizes?  */
12076
12077       if (GET_CODE (dest) == ZERO_EXTRACT
12078           || GET_CODE (dest) == STRICT_LOW_PART
12079           || (GET_CODE (dest) == SUBREG
12080               && (((GET_MODE_SIZE (GET_MODE (dest))
12081                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
12082                   == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
12083                        + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
12084         {
12085           move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
12086           return;
12087         }
12088
12089       /* If this is some other SUBREG, we know it replaces the entire
12090          value, so use that as the destination.  */
12091       if (GET_CODE (dest) == SUBREG)
12092         dest = SUBREG_REG (dest);
12093
12094       /* If this is a MEM, adjust deaths of anything used in the address.
12095          For a REG (the only other possibility), the entire value is
12096          being replaced so the old value is not used in this insn.  */
12097
12098       if (MEM_P (dest))
12099         move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
12100                      to_insn, pnotes);
12101       return;
12102     }
12103
12104   else if (GET_CODE (x) == CLOBBER)
12105     return;
12106
12107   len = GET_RTX_LENGTH (code);
12108   fmt = GET_RTX_FORMAT (code);
12109
12110   for (i = 0; i < len; i++)
12111     {
12112       if (fmt[i] == 'E')
12113         {
12114           int j;
12115           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12116             move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
12117                          to_insn, pnotes);
12118         }
12119       else if (fmt[i] == 'e')
12120         move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
12121     }
12122 }
12123 \f
12124 /* Return 1 if X is the target of a bit-field assignment in BODY, the
12125    pattern of an insn.  X must be a REG.  */
12126
12127 static int
12128 reg_bitfield_target_p (rtx x, rtx body)
12129 {
12130   int i;
12131
12132   if (GET_CODE (body) == SET)
12133     {
12134       rtx dest = SET_DEST (body);
12135       rtx target;
12136       unsigned int regno, tregno, endregno, endtregno;
12137
12138       if (GET_CODE (dest) == ZERO_EXTRACT)
12139         target = XEXP (dest, 0);
12140       else if (GET_CODE (dest) == STRICT_LOW_PART)
12141         target = SUBREG_REG (XEXP (dest, 0));
12142       else
12143         return 0;
12144
12145       if (GET_CODE (target) == SUBREG)
12146         target = SUBREG_REG (target);
12147
12148       if (!REG_P (target))
12149         return 0;
12150
12151       tregno = REGNO (target), regno = REGNO (x);
12152       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
12153         return target == x;
12154
12155       endtregno = end_hard_regno (GET_MODE (target), tregno);
12156       endregno = end_hard_regno (GET_MODE (x), regno);
12157
12158       return endregno > tregno && regno < endtregno;
12159     }
12160
12161   else if (GET_CODE (body) == PARALLEL)
12162     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
12163       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
12164         return 1;
12165
12166   return 0;
12167 }
12168 \f
12169 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
12170    as appropriate.  I3 and I2 are the insns resulting from the combination
12171    insns including FROM (I2 may be zero).
12172
12173    ELIM_I2 and ELIM_I1 are either zero or registers that we know will
12174    not need REG_DEAD notes because they are being substituted for.  This
12175    saves searching in the most common cases.
12176
12177    Each note in the list is either ignored or placed on some insns, depending
12178    on the type of note.  */
12179
12180 static void
12181 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
12182                   rtx elim_i1)
12183 {
12184   rtx note, next_note;
12185   rtx tem;
12186
12187   for (note = notes; note; note = next_note)
12188     {
12189       rtx place = 0, place2 = 0;
12190
12191       next_note = XEXP (note, 1);
12192       switch (REG_NOTE_KIND (note))
12193         {
12194         case REG_BR_PROB:
12195         case REG_BR_PRED:
12196           /* Doesn't matter much where we put this, as long as it's somewhere.
12197              It is preferable to keep these notes on branches, which is most
12198              likely to be i3.  */
12199           place = i3;
12200           break;
12201
12202         case REG_VALUE_PROFILE:
12203           /* Just get rid of this note, as it is unused later anyway.  */
12204           break;
12205
12206         case REG_NON_LOCAL_GOTO:
12207           if (JUMP_P (i3))
12208             place = i3;
12209           else
12210             {
12211               gcc_assert (i2 && JUMP_P (i2));
12212               place = i2;
12213             }
12214           break;
12215
12216         case REG_EH_REGION:
12217           /* These notes must remain with the call or trapping instruction.  */
12218           if (CALL_P (i3))
12219             place = i3;
12220           else if (i2 && CALL_P (i2))
12221             place = i2;
12222           else
12223             {
12224               gcc_assert (flag_non_call_exceptions);
12225               if (may_trap_p (i3))
12226                 place = i3;
12227               else if (i2 && may_trap_p (i2))
12228                 place = i2;
12229               /* ??? Otherwise assume we've combined things such that we
12230                  can now prove that the instructions can't trap.  Drop the
12231                  note in this case.  */
12232             }
12233           break;
12234
12235         case REG_NORETURN:
12236         case REG_SETJMP:
12237           /* These notes must remain with the call.  It should not be
12238              possible for both I2 and I3 to be a call.  */
12239           if (CALL_P (i3))
12240             place = i3;
12241           else
12242             {
12243               gcc_assert (i2 && CALL_P (i2));
12244               place = i2;
12245             }
12246           break;
12247
12248         case REG_UNUSED:
12249           /* Any clobbers for i3 may still exist, and so we must process
12250              REG_UNUSED notes from that insn.
12251
12252              Any clobbers from i2 or i1 can only exist if they were added by
12253              recog_for_combine.  In that case, recog_for_combine created the
12254              necessary REG_UNUSED notes.  Trying to keep any original
12255              REG_UNUSED notes from these insns can cause incorrect output
12256              if it is for the same register as the original i3 dest.
12257              In that case, we will notice that the register is set in i3,
12258              and then add a REG_UNUSED note for the destination of i3, which
12259              is wrong.  However, it is possible to have REG_UNUSED notes from
12260              i2 or i1 for register which were both used and clobbered, so
12261              we keep notes from i2 or i1 if they will turn into REG_DEAD
12262              notes.  */
12263
12264           /* If this register is set or clobbered in I3, put the note there
12265              unless there is one already.  */
12266           if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
12267             {
12268               if (from_insn != i3)
12269                 break;
12270
12271               if (! (REG_P (XEXP (note, 0))
12272                      ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
12273                      : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
12274                 place = i3;
12275             }
12276           /* Otherwise, if this register is used by I3, then this register
12277              now dies here, so we must put a REG_DEAD note here unless there
12278              is one already.  */
12279           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
12280                    && ! (REG_P (XEXP (note, 0))
12281                          ? find_regno_note (i3, REG_DEAD,
12282                                             REGNO (XEXP (note, 0)))
12283                          : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
12284             {
12285               PUT_REG_NOTE_KIND (note, REG_DEAD);
12286               place = i3;
12287             }
12288           break;
12289
12290         case REG_EQUAL:
12291         case REG_EQUIV:
12292         case REG_NOALIAS:
12293           /* These notes say something about results of an insn.  We can
12294              only support them if they used to be on I3 in which case they
12295              remain on I3.  Otherwise they are ignored.
12296
12297              If the note refers to an expression that is not a constant, we
12298              must also ignore the note since we cannot tell whether the
12299              equivalence is still true.  It might be possible to do
12300              slightly better than this (we only have a problem if I2DEST
12301              or I1DEST is present in the expression), but it doesn't
12302              seem worth the trouble.  */
12303
12304           if (from_insn == i3
12305               && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
12306             place = i3;
12307           break;
12308
12309         case REG_INC:
12310         case REG_NO_CONFLICT:
12311           /* These notes say something about how a register is used.  They must
12312              be present on any use of the register in I2 or I3.  */
12313           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
12314             place = i3;
12315
12316           if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
12317             {
12318               if (place)
12319                 place2 = i2;
12320               else
12321                 place = i2;
12322             }
12323           break;
12324
12325         case REG_LABEL:
12326           /* This can show up in several ways -- either directly in the
12327              pattern, or hidden off in the constant pool with (or without?)
12328              a REG_EQUAL note.  */
12329           /* ??? Ignore the without-reg_equal-note problem for now.  */
12330           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12331               || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12332                   && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12333                   && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12334             place = i3;
12335
12336           if (i2
12337               && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12338                   || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12339                       && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12340                       && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12341             {
12342               if (place)
12343                 place2 = i2;
12344               else
12345                 place = i2;
12346             }
12347
12348           /* Don't attach REG_LABEL note to a JUMP_INSN.  Add
12349              a JUMP_LABEL instead or decrement LABEL_NUSES.  */
12350           if (place && JUMP_P (place))
12351             {
12352               rtx label = JUMP_LABEL (place);
12353
12354               if (!label)
12355                 JUMP_LABEL (place) = XEXP (note, 0);
12356               else
12357                 {
12358                   gcc_assert (label == XEXP (note, 0));
12359                   if (LABEL_P (label))
12360                     LABEL_NUSES (label)--;
12361                 }
12362               place = 0;
12363             }
12364           if (place2 && JUMP_P (place2))
12365             {
12366               rtx label = JUMP_LABEL (place2);
12367
12368               if (!label)
12369                 JUMP_LABEL (place2) = XEXP (note, 0);
12370               else
12371                 {
12372                   gcc_assert (label == XEXP (note, 0));
12373                   if (LABEL_P (label))
12374                     LABEL_NUSES (label)--;
12375                 }
12376               place2 = 0;
12377             }
12378           break;
12379
12380         case REG_NONNEG:
12381           /* This note says something about the value of a register prior
12382              to the execution of an insn.  It is too much trouble to see
12383              if the note is still correct in all situations.  It is better
12384              to simply delete it.  */
12385           break;
12386
12387         case REG_LIBCALL_ID:
12388           /* If the insn previously containing this note still exists,
12389              put it back where it was.  Otherwise move it to the previous
12390              insn.  */
12391           if (!NOTE_P (from_insn))
12392             place = from_insn;
12393           else
12394             place = prev_real_insn (from_insn);
12395           break;
12396         case REG_RETVAL:
12397           /* If the insn previously containing this note still exists,
12398              put it back where it was.  Otherwise move it to the previous
12399              insn.  Adjust the corresponding REG_LIBCALL note.  */
12400           if (!NOTE_P (from_insn))
12401             place = from_insn;
12402           else
12403             {
12404               tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
12405               place = prev_real_insn (from_insn);
12406               if (tem && place)
12407                 XEXP (tem, 0) = place;
12408               /* If we're deleting the last remaining instruction of a
12409                  libcall sequence, don't add the notes.  */
12410               else if (XEXP (note, 0) == from_insn)
12411                 tem = place = 0;
12412               /* Don't add the dangling REG_RETVAL note.  */
12413               else if (! tem)
12414                 place = 0;
12415             }
12416           break;
12417
12418         case REG_LIBCALL:
12419           /* This is handled similarly to REG_RETVAL.  */
12420           if (!NOTE_P (from_insn))
12421             place = from_insn;
12422           else
12423             {
12424               tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
12425               place = next_real_insn (from_insn);
12426               if (tem && place)
12427                 XEXP (tem, 0) = place;
12428               /* If we're deleting the last remaining instruction of a
12429                  libcall sequence, don't add the notes.  */
12430               else if (XEXP (note, 0) == from_insn)
12431                 tem = place = 0;
12432               /* Don't add the dangling REG_LIBCALL note.  */
12433               else if (! tem)
12434                 place = 0;
12435             }
12436           break;
12437
12438         case REG_DEAD:
12439           /* If we replaced the right hand side of FROM_INSN with a
12440              REG_EQUAL note, the original use of the dying register
12441              will not have been combined into I3 and I2.  In such cases,
12442              FROM_INSN is guaranteed to be the first of the combined
12443              instructions, so we simply need to search back before
12444              FROM_INSN for the previous use or set of this register,
12445              then alter the notes there appropriately.
12446
12447              If the register is used as an input in I3, it dies there.
12448              Similarly for I2, if it is nonzero and adjacent to I3.
12449
12450              If the register is not used as an input in either I3 or I2
12451              and it is not one of the registers we were supposed to eliminate,
12452              there are two possibilities.  We might have a non-adjacent I2
12453              or we might have somehow eliminated an additional register
12454              from a computation.  For example, we might have had A & B where
12455              we discover that B will always be zero.  In this case we will
12456              eliminate the reference to A.
12457
12458              In both cases, we must search to see if we can find a previous
12459              use of A and put the death note there.  */
12460
12461           if (from_insn
12462               && from_insn == i2mod
12463               && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
12464             tem = from_insn;
12465           else
12466             {
12467               if (from_insn
12468                   && CALL_P (from_insn)
12469                   && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12470                 place = from_insn;
12471               else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12472                 place = i3;
12473               else if (i2 != 0 && next_nonnote_insn (i2) == i3
12474                        && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12475                 place = i2;
12476               else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
12477                         && !(i2mod
12478                              && reg_overlap_mentioned_p (XEXP (note, 0),
12479                                                          i2mod_old_rhs)))
12480                        || rtx_equal_p (XEXP (note, 0), elim_i1))
12481                 break;
12482               tem = i3;
12483             }
12484
12485           if (place == 0)
12486             {
12487               basic_block bb = this_basic_block;
12488
12489               for (tem = PREV_INSN (tem); place == 0; tem = PREV_INSN (tem))
12490                 {
12491                   if (! INSN_P (tem))
12492                     {
12493                       if (tem == BB_HEAD (bb))
12494                         break;
12495                       continue;
12496                     }
12497
12498                   /* If the register is being set at TEM, see if that is all
12499                      TEM is doing.  If so, delete TEM.  Otherwise, make this
12500                      into a REG_UNUSED note instead. Don't delete sets to
12501                      global register vars.  */
12502                   if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
12503                        || !global_regs[REGNO (XEXP (note, 0))])
12504                       && reg_set_p (XEXP (note, 0), PATTERN (tem)))
12505                     {
12506                       rtx set = single_set (tem);
12507                       rtx inner_dest = 0;
12508 #ifdef HAVE_cc0
12509                       rtx cc0_setter = NULL_RTX;
12510 #endif
12511
12512                       if (set != 0)
12513                         for (inner_dest = SET_DEST (set);
12514                              (GET_CODE (inner_dest) == STRICT_LOW_PART
12515                               || GET_CODE (inner_dest) == SUBREG
12516                               || GET_CODE (inner_dest) == ZERO_EXTRACT);
12517                              inner_dest = XEXP (inner_dest, 0))
12518                           ;
12519
12520                       /* Verify that it was the set, and not a clobber that
12521                          modified the register.
12522
12523                          CC0 targets must be careful to maintain setter/user
12524                          pairs.  If we cannot delete the setter due to side
12525                          effects, mark the user with an UNUSED note instead
12526                          of deleting it.  */
12527
12528                       if (set != 0 && ! side_effects_p (SET_SRC (set))
12529                           && rtx_equal_p (XEXP (note, 0), inner_dest)
12530 #ifdef HAVE_cc0
12531                           && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12532                               || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12533                                   && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12534 #endif
12535                           )
12536                         {
12537                           /* Move the notes and links of TEM elsewhere.
12538                              This might delete other dead insns recursively.
12539                              First set the pattern to something that won't use
12540                              any register.  */
12541                           rtx old_notes = REG_NOTES (tem);
12542
12543                           PATTERN (tem) = pc_rtx;
12544                           REG_NOTES (tem) = NULL;
12545
12546                           distribute_notes (old_notes, tem, tem, NULL_RTX,
12547                                             NULL_RTX, NULL_RTX);
12548                           distribute_links (LOG_LINKS (tem));
12549
12550                           SET_INSN_DELETED (tem);
12551
12552 #ifdef HAVE_cc0
12553                           /* Delete the setter too.  */
12554                           if (cc0_setter)
12555                             {
12556                               PATTERN (cc0_setter) = pc_rtx;
12557                               old_notes = REG_NOTES (cc0_setter);
12558                               REG_NOTES (cc0_setter) = NULL;
12559
12560                               distribute_notes (old_notes, cc0_setter,
12561                                                 cc0_setter, NULL_RTX,
12562                                                 NULL_RTX, NULL_RTX);
12563                               distribute_links (LOG_LINKS (cc0_setter));
12564
12565                               SET_INSN_DELETED (cc0_setter);
12566                             }
12567 #endif
12568                         }
12569                       else
12570                         {
12571                           PUT_REG_NOTE_KIND (note, REG_UNUSED);
12572
12573                           /*  If there isn't already a REG_UNUSED note, put one
12574                               here.  Do not place a REG_DEAD note, even if
12575                               the register is also used here; that would not
12576                               match the algorithm used in lifetime analysis
12577                               and can cause the consistency check in the
12578                               scheduler to fail.  */
12579                           if (! find_regno_note (tem, REG_UNUSED,
12580                                                  REGNO (XEXP (note, 0))))
12581                             place = tem;
12582                           break;
12583                         }
12584                     }
12585                   else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12586                            || (CALL_P (tem)
12587                                && find_reg_fusage (tem, USE, XEXP (note, 0))))
12588                     {
12589                       place = tem;
12590
12591                       /* If we are doing a 3->2 combination, and we have a
12592                          register which formerly died in i3 and was not used
12593                          by i2, which now no longer dies in i3 and is used in
12594                          i2 but does not die in i2, and place is between i2
12595                          and i3, then we may need to move a link from place to
12596                          i2.  */
12597                       if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
12598                           && from_insn
12599                           && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
12600                           && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12601                         {
12602                           rtx links = LOG_LINKS (place);
12603                           LOG_LINKS (place) = 0;
12604                           distribute_links (links);
12605                         }
12606                       break;
12607                     }
12608
12609                   if (tem == BB_HEAD (bb))
12610                     break;
12611                 }
12612
12613             }
12614
12615           /* If the register is set or already dead at PLACE, we needn't do
12616              anything with this note if it is still a REG_DEAD note.
12617              We check here if it is set at all, not if is it totally replaced,
12618              which is what `dead_or_set_p' checks, so also check for it being
12619              set partially.  */
12620
12621           if (place && REG_NOTE_KIND (note) == REG_DEAD)
12622             {
12623               unsigned int regno = REGNO (XEXP (note, 0));
12624
12625
12626               if (dead_or_set_p (place, XEXP (note, 0))
12627                   || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12628                 {
12629                   /* Unless the register previously died in PLACE, clear
12630                      last_death.  [I no longer understand why this is
12631                      being done.] */
12632                   if (reg_stat[regno].last_death != place)
12633                     reg_stat[regno].last_death = 0;
12634                   place = 0;
12635                 }
12636               else
12637                 reg_stat[regno].last_death = place;
12638
12639               /* If this is a death note for a hard reg that is occupying
12640                  multiple registers, ensure that we are still using all
12641                  parts of the object.  If we find a piece of the object
12642                  that is unused, we must arrange for an appropriate REG_DEAD
12643                  note to be added for it.  However, we can't just emit a USE
12644                  and tag the note to it, since the register might actually
12645                  be dead; so we recourse, and the recursive call then finds
12646                  the previous insn that used this register.  */
12647
12648               if (place && regno < FIRST_PSEUDO_REGISTER
12649                   && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
12650                 {
12651                   unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
12652                   int all_used = 1;
12653                   unsigned int i;
12654
12655                   for (i = regno; i < endregno; i++)
12656                     if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12657                          && ! find_regno_fusage (place, USE, i))
12658                         || dead_or_set_regno_p (place, i))
12659                       all_used = 0;
12660
12661                   if (! all_used)
12662                     {
12663                       /* Put only REG_DEAD notes for pieces that are
12664                          not already dead or set.  */
12665
12666                       for (i = regno; i < endregno;
12667                            i += hard_regno_nregs[i][reg_raw_mode[i]])
12668                         {
12669                           rtx piece = regno_reg_rtx[i];
12670                           basic_block bb = this_basic_block;
12671
12672                           if (! dead_or_set_p (place, piece)
12673                               && ! reg_bitfield_target_p (piece,
12674                                                           PATTERN (place)))
12675                             {
12676                               rtx new_note
12677                                 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12678
12679                               distribute_notes (new_note, place, place,
12680                                                 NULL_RTX, NULL_RTX, NULL_RTX);
12681                             }
12682                           else if (! refers_to_regno_p (i, i + 1,
12683                                                         PATTERN (place), 0)
12684                                    && ! find_regno_fusage (place, USE, i))
12685                             for (tem = PREV_INSN (place); ;
12686                                  tem = PREV_INSN (tem))
12687                               {
12688                                 if (! INSN_P (tem))
12689                                   {
12690                                     if (tem == BB_HEAD (bb))
12691                                       break;
12692                                     continue;
12693                                   }
12694                                 if (dead_or_set_p (tem, piece)
12695                                     || reg_bitfield_target_p (piece,
12696                                                               PATTERN (tem)))
12697                                   {
12698                                     REG_NOTES (tem)
12699                                       = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
12700                                                            REG_NOTES (tem));
12701                                     break;
12702                                   }
12703                               }
12704
12705                         }
12706
12707                       place = 0;
12708                     }
12709                 }
12710             }
12711           break;
12712
12713         default:
12714           /* Any other notes should not be present at this point in the
12715              compilation.  */
12716           gcc_unreachable ();
12717         }
12718
12719       if (place)
12720         {
12721           XEXP (note, 1) = REG_NOTES (place);
12722           REG_NOTES (place) = note;
12723         }
12724
12725       if (place2)
12726         REG_NOTES (place2) 
12727           = gen_rtx_fmt_ee (GET_CODE (note), REG_NOTE_KIND (note),
12728                             XEXP (note, 0), REG_NOTES (place2));
12729     }
12730 }
12731 \f
12732 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12733    I3, I2, and I1 to new locations.  This is also called to add a link
12734    pointing at I3 when I3's destination is changed.  */
12735
12736 static void
12737 distribute_links (rtx links)
12738 {
12739   rtx link, next_link;
12740
12741   for (link = links; link; link = next_link)
12742     {
12743       rtx place = 0;
12744       rtx insn;
12745       rtx set, reg;
12746
12747       next_link = XEXP (link, 1);
12748
12749       /* If the insn that this link points to is a NOTE or isn't a single
12750          set, ignore it.  In the latter case, it isn't clear what we
12751          can do other than ignore the link, since we can't tell which
12752          register it was for.  Such links wouldn't be used by combine
12753          anyway.
12754
12755          It is not possible for the destination of the target of the link to
12756          have been changed by combine.  The only potential of this is if we
12757          replace I3, I2, and I1 by I3 and I2.  But in that case the
12758          destination of I2 also remains unchanged.  */
12759
12760       if (NOTE_P (XEXP (link, 0))
12761           || (set = single_set (XEXP (link, 0))) == 0)
12762         continue;
12763
12764       reg = SET_DEST (set);
12765       while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12766              || GET_CODE (reg) == STRICT_LOW_PART)
12767         reg = XEXP (reg, 0);
12768
12769       /* A LOG_LINK is defined as being placed on the first insn that uses
12770          a register and points to the insn that sets the register.  Start
12771          searching at the next insn after the target of the link and stop
12772          when we reach a set of the register or the end of the basic block.
12773
12774          Note that this correctly handles the link that used to point from
12775          I3 to I2.  Also note that not much searching is typically done here
12776          since most links don't point very far away.  */
12777
12778       for (insn = NEXT_INSN (XEXP (link, 0));
12779            (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
12780                      || BB_HEAD (this_basic_block->next_bb) != insn));
12781            insn = NEXT_INSN (insn))
12782         if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12783           {
12784             if (reg_referenced_p (reg, PATTERN (insn)))
12785               place = insn;
12786             break;
12787           }
12788         else if (CALL_P (insn)
12789                  && find_reg_fusage (insn, USE, reg))
12790           {
12791             place = insn;
12792             break;
12793           }
12794         else if (INSN_P (insn) && reg_set_p (reg, insn))
12795           break;
12796
12797       /* If we found a place to put the link, place it there unless there
12798          is already a link to the same insn as LINK at that point.  */
12799
12800       if (place)
12801         {
12802           rtx link2;
12803
12804           for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12805             if (XEXP (link2, 0) == XEXP (link, 0))
12806               break;
12807
12808           if (link2 == 0)
12809             {
12810               XEXP (link, 1) = LOG_LINKS (place);
12811               LOG_LINKS (place) = link;
12812
12813               /* Set added_links_insn to the earliest insn we added a
12814                  link to.  */
12815               if (added_links_insn == 0
12816                   || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
12817                 added_links_insn = place;
12818             }
12819         }
12820     }
12821 }
12822 \f
12823 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
12824    Check whether the expression pointer to by LOC is a register or
12825    memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
12826    Otherwise return zero.  */
12827
12828 static int
12829 unmentioned_reg_p_1 (rtx *loc, void *expr)
12830 {
12831   rtx x = *loc;
12832
12833   if (x != NULL_RTX
12834       && (REG_P (x) || MEM_P (x))
12835       && ! reg_mentioned_p (x, (rtx) expr))
12836     return 1;
12837   return 0;
12838 }
12839
12840 /* Check for any register or memory mentioned in EQUIV that is not
12841    mentioned in EXPR.  This is used to restrict EQUIV to "specializations"
12842    of EXPR where some registers may have been replaced by constants.  */
12843
12844 static bool
12845 unmentioned_reg_p (rtx equiv, rtx expr)
12846 {
12847   return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
12848 }
12849 \f
12850 void
12851 dump_combine_stats (FILE *file)
12852 {
12853   fprintf
12854     (file,
12855      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12856      combine_attempts, combine_merges, combine_extras, combine_successes);
12857 }
12858
12859 void
12860 dump_combine_total_stats (FILE *file)
12861 {
12862   fprintf
12863     (file,
12864      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12865      total_attempts, total_merges, total_extras, total_successes);
12866 }
12867 \f
12868 static bool
12869 gate_handle_combine (void)
12870 {
12871   return (optimize > 0);
12872 }
12873
12874 /* Try combining insns through substitution.  */
12875 static unsigned int
12876 rest_of_handle_combine (void)
12877 {
12878   int rebuild_jump_labels_after_combine;
12879
12880   df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
12881   df_note_add_problem ();
12882   df_analyze ();
12883
12884   regstat_init_n_sets_and_refs ();
12885
12886   rebuild_jump_labels_after_combine
12887     = combine_instructions (get_insns (), max_reg_num ());
12888
12889   /* Combining insns may have turned an indirect jump into a
12890      direct jump.  Rebuild the JUMP_LABEL fields of jumping
12891      instructions.  */
12892   if (rebuild_jump_labels_after_combine)
12893     {
12894       timevar_push (TV_JUMP);
12895       rebuild_jump_labels (get_insns ());
12896       cleanup_cfg (0);
12897       timevar_pop (TV_JUMP);
12898     }
12899
12900   regstat_free_n_sets_and_refs ();
12901   return 0;
12902 }
12903
12904 struct tree_opt_pass pass_combine =
12905 {
12906   "combine",                            /* name */
12907   gate_handle_combine,                  /* gate */
12908   rest_of_handle_combine,               /* execute */
12909   NULL,                                 /* sub */
12910   NULL,                                 /* next */
12911   0,                                    /* static_pass_number */
12912   TV_COMBINE,                           /* tv_id */
12913   0,                                    /* properties_required */
12914   0,                                    /* properties_provided */
12915   0,                                    /* properties_destroyed */
12916   0,                                    /* todo_flags_start */
12917   TODO_dump_func |
12918   TODO_df_finish |
12919   TODO_ggc_collect,                     /* todo_flags_finish */
12920   'c'                                   /* letter */
12921 };
12922