OSDN Git Service

* gcc.dg/funcorder.c: Remove XFAIL for hppa*64*-*-*.
[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, 2008, 2009, 2010
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 3, 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 COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /* This module is essentially the "combiner" phase of the U. of Arizona
23    Portable Optimizer, but redone to work on our list-structured
24    representation for RTL instead of their string representation.
25
26    The LOG_LINKS of each insn identify the most recent assignment
27    to each REG used in the insn.  It is a list of previous insns,
28    each of which contains a SET for a REG that is used in this insn
29    and not used or set in between.  LOG_LINKs never cross basic blocks.
30    They were set up by the preceding pass (lifetime analysis).
31
32    We try to combine each pair of insns joined by a logical link.
33    We also try to combine triples of insns A, B and C when
34    C has a link back to B and B has a link back to A.
35
36    LOG_LINKS does not have links for use of the CC0.  They don't
37    need to, because the insn that sets the CC0 is always immediately
38    before the insn that tests it.  So we always regard a branch
39    insn as having a logical link to the preceding insn.  The same is true
40    for an insn explicitly using CC0.
41
42    We check (with use_crosses_set_p) to avoid combining in such a way
43    as to move a computation to a place where its value would be different.
44
45    Combination is done by mathematically substituting the previous
46    insn(s) values for the regs they set into the expressions in
47    the later insns that refer to these regs.  If the result is a valid insn
48    for our target machine, according to the machine description,
49    we install it, delete the earlier insns, and update the data flow
50    information (LOG_LINKS and REG_NOTES) for what we did.
51
52    There are a few exceptions where the dataflow information isn't
53    completely updated (however this is only a local issue since it is
54    regenerated before the next pass that uses it):
55
56    - reg_live_length is not updated
57    - reg_n_refs is not adjusted in the rare case when a register is
58      no longer required in a computation
59    - there are extremely rare cases (see distribute_notes) when a
60      REG_DEAD note is lost
61    - a LOG_LINKS entry that refers to an insn with multiple SETs may be
62      removed because there is no way to know which register it was
63      linking
64
65    To simplify substitution, we combine only when the earlier insn(s)
66    consist of only a single assignment.  To simplify updating afterward,
67    we never combine when a subroutine call appears in the middle.
68
69    Since we do not represent assignments to CC0 explicitly except when that
70    is all an insn does, there is no LOG_LINKS entry in an insn that uses
71    the condition code for the insn that set the condition code.
72    Fortunately, these two insns must be consecutive.
73    Therefore, every JUMP_INSN is taken to have an implicit logical link
74    to the preceding insn.  This is not quite right, since non-jumps can
75    also use the condition code; but in practice such insns would not
76    combine anyway.  */
77
78 #include "config.h"
79 #include "system.h"
80 #include "coretypes.h"
81 #include "tm.h"
82 #include "rtl.h"
83 #include "tree.h"
84 #include "tm_p.h"
85 #include "flags.h"
86 #include "regs.h"
87 #include "hard-reg-set.h"
88 #include "basic-block.h"
89 #include "insn-config.h"
90 #include "function.h"
91 /* Include expr.h after insn-config.h so we get HAVE_conditional_move.  */
92 #include "expr.h"
93 #include "insn-attr.h"
94 #include "recog.h"
95 #include "diagnostic-core.h"
96 #include "target.h"
97 #include "optabs.h"
98 #include "insn-codes.h"
99 #include "rtlhooks-def.h"
100 /* Include output.h for dump_file.  */
101 #include "output.h"
102 #include "params.h"
103 #include "timevar.h"
104 #include "tree-pass.h"
105 #include "df.h"
106 #include "cgraph.h"
107
108 /* Number of attempts to combine instructions in this function.  */
109
110 static int combine_attempts;
111
112 /* Number of attempts that got as far as substitution in this function.  */
113
114 static int combine_merges;
115
116 /* Number of instructions combined with added SETs in this function.  */
117
118 static int combine_extras;
119
120 /* Number of instructions combined in this function.  */
121
122 static int combine_successes;
123
124 /* Totals over entire compilation.  */
125
126 static int total_attempts, total_merges, total_extras, total_successes;
127
128 /* combine_instructions may try to replace the right hand side of the
129    second instruction with the value of an associated REG_EQUAL note
130    before throwing it at try_combine.  That is problematic when there
131    is a REG_DEAD note for a register used in the old right hand side
132    and can cause distribute_notes to do wrong things.  This is the
133    second instruction if it has been so modified, null otherwise.  */
134
135 static rtx i2mod;
136
137 /* When I2MOD is nonnull, this is a copy of the old right hand side.  */
138
139 static rtx i2mod_old_rhs;
140
141 /* When I2MOD is nonnull, this is a copy of the new right hand side.  */
142
143 static rtx i2mod_new_rhs;
144 \f
145 typedef struct reg_stat_struct {
146   /* Record last point of death of (hard or pseudo) register n.  */
147   rtx                           last_death;
148
149   /* Record last point of modification of (hard or pseudo) register n.  */
150   rtx                           last_set;
151
152   /* The next group of fields allows the recording of the last value assigned
153      to (hard or pseudo) register n.  We use this information to see if an
154      operation being processed is redundant given a prior operation performed
155      on the register.  For example, an `and' with a constant is redundant if
156      all the zero bits are already known to be turned off.
157
158      We use an approach similar to that used by cse, but change it in the
159      following ways:
160
161      (1) We do not want to reinitialize at each label.
162      (2) It is useful, but not critical, to know the actual value assigned
163          to a register.  Often just its form is helpful.
164
165      Therefore, we maintain the following fields:
166
167      last_set_value             the last value assigned
168      last_set_label             records the value of label_tick when the
169                                 register was assigned
170      last_set_table_tick        records the value of label_tick when a
171                                 value using the register is assigned
172      last_set_invalid           set to nonzero when it is not valid
173                                 to use the value of this register in some
174                                 register's value
175
176      To understand the usage of these tables, it is important to understand
177      the distinction between the value in last_set_value being valid and
178      the register being validly contained in some other expression in the
179      table.
180
181      (The next two parameters are out of date).
182
183      reg_stat[i].last_set_value is valid if it is nonzero, and either
184      reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
185
186      Register I may validly appear in any expression returned for the value
187      of another register if reg_n_sets[i] is 1.  It may also appear in the
188      value for register J if reg_stat[j].last_set_invalid is zero, or
189      reg_stat[i].last_set_label < reg_stat[j].last_set_label.
190
191      If an expression is found in the table containing a register which may
192      not validly appear in an expression, the register is replaced by
193      something that won't match, (clobber (const_int 0)).  */
194
195   /* Record last value assigned to (hard or pseudo) register n.  */
196
197   rtx                           last_set_value;
198
199   /* Record the value of label_tick when an expression involving register n
200      is placed in last_set_value.  */
201
202   int                           last_set_table_tick;
203
204   /* Record the value of label_tick when the value for register n is placed in
205      last_set_value.  */
206
207   int                           last_set_label;
208
209   /* These fields are maintained in parallel with last_set_value and are
210      used to store the mode in which the register was last set, the bits
211      that were known to be zero when it was last set, and the number of
212      sign bits copies it was known to have when it was last set.  */
213
214   unsigned HOST_WIDE_INT        last_set_nonzero_bits;
215   char                          last_set_sign_bit_copies;
216   ENUM_BITFIELD(machine_mode)   last_set_mode : 8;
217
218   /* Set nonzero if references to register n in expressions should not be
219      used.  last_set_invalid is set nonzero when this register is being
220      assigned to and last_set_table_tick == label_tick.  */
221
222   char                          last_set_invalid;
223
224   /* Some registers that are set more than once and used in more than one
225      basic block are nevertheless always set in similar ways.  For example,
226      a QImode register may be loaded from memory in two places on a machine
227      where byte loads zero extend.
228
229      We record in the following fields if a register has some leading bits
230      that are always equal to the sign bit, and what we know about the
231      nonzero bits of a register, specifically which bits are known to be
232      zero.
233
234      If an entry is zero, it means that we don't know anything special.  */
235
236   unsigned char                 sign_bit_copies;
237
238   unsigned HOST_WIDE_INT        nonzero_bits;
239
240   /* Record the value of the label_tick when the last truncation
241      happened.  The field truncated_to_mode is only valid if
242      truncation_label == label_tick.  */
243
244   int                           truncation_label;
245
246   /* Record the last truncation seen for this register.  If truncation
247      is not a nop to this mode we might be able to save an explicit
248      truncation if we know that value already contains a truncated
249      value.  */
250
251   ENUM_BITFIELD(machine_mode)   truncated_to_mode : 8;
252 } reg_stat_type;
253
254 DEF_VEC_O(reg_stat_type);
255 DEF_VEC_ALLOC_O(reg_stat_type,heap);
256
257 static VEC(reg_stat_type,heap) *reg_stat;
258
259 /* Record the luid of the last insn that invalidated memory
260    (anything that writes memory, and subroutine calls, but not pushes).  */
261
262 static int mem_last_set;
263
264 /* Record the luid of the last CALL_INSN
265    so we can tell whether a potential combination crosses any calls.  */
266
267 static int last_call_luid;
268
269 /* When `subst' is called, this is the insn that is being modified
270    (by combining in a previous insn).  The PATTERN of this insn
271    is still the old pattern partially modified and it should not be
272    looked at, but this may be used to examine the successors of the insn
273    to judge whether a simplification is valid.  */
274
275 static rtx subst_insn;
276
277 /* This is the lowest LUID that `subst' is currently dealing with.
278    get_last_value will not return a value if the register was set at or
279    after this LUID.  If not for this mechanism, we could get confused if
280    I2 or I1 in try_combine were an insn that used the old value of a register
281    to obtain a new value.  In that case, we might erroneously get the
282    new value of the register when we wanted the old one.  */
283
284 static int subst_low_luid;
285
286 /* This contains any hard registers that are used in newpat; reg_dead_at_p
287    must consider all these registers to be always live.  */
288
289 static HARD_REG_SET newpat_used_regs;
290
291 /* This is an insn to which a LOG_LINKS entry has been added.  If this
292    insn is the earlier than I2 or I3, combine should rescan starting at
293    that location.  */
294
295 static rtx added_links_insn;
296
297 /* Basic block in which we are performing combines.  */
298 static basic_block this_basic_block;
299 static bool optimize_this_for_speed_p;
300
301 \f
302 /* Length of the currently allocated uid_insn_cost array.  */
303
304 static int max_uid_known;
305
306 /* The following array records the insn_rtx_cost for every insn
307    in the instruction stream.  */
308
309 static int *uid_insn_cost;
310
311 /* The following array records the LOG_LINKS for every insn in the
312    instruction stream as an INSN_LIST rtx.  */
313
314 static rtx *uid_log_links;
315
316 #define INSN_COST(INSN)         (uid_insn_cost[INSN_UID (INSN)])
317 #define LOG_LINKS(INSN)         (uid_log_links[INSN_UID (INSN)])
318
319 /* Incremented for each basic block.  */
320
321 static int label_tick;
322
323 /* Reset to label_tick for each extended basic block in scanning order.  */
324
325 static int label_tick_ebb_start;
326
327 /* Mode used to compute significance in reg_stat[].nonzero_bits.  It is the
328    largest integer mode that can fit in HOST_BITS_PER_WIDE_INT.  */
329
330 static enum machine_mode nonzero_bits_mode;
331
332 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
333    be safely used.  It is zero while computing them and after combine has
334    completed.  This former test prevents propagating values based on
335    previously set values, which can be incorrect if a variable is modified
336    in a loop.  */
337
338 static int nonzero_sign_valid;
339
340 \f
341 /* Record one modification to rtl structure
342    to be undone by storing old_contents into *where.  */
343
344 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE };
345
346 struct undo
347 {
348   struct undo *next;
349   enum undo_kind kind;
350   union { rtx r; int i; enum machine_mode m; } old_contents;
351   union { rtx *r; int *i; } where;
352 };
353
354 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
355    num_undo says how many are currently recorded.
356
357    other_insn is nonzero if we have modified some other insn in the process
358    of working on subst_insn.  It must be verified too.  */
359
360 struct undobuf
361 {
362   struct undo *undos;
363   struct undo *frees;
364   rtx other_insn;
365 };
366
367 static struct undobuf undobuf;
368
369 /* Number of times the pseudo being substituted for
370    was found and replaced.  */
371
372 static int n_occurrences;
373
374 static rtx reg_nonzero_bits_for_combine (const_rtx, enum machine_mode, const_rtx,
375                                          enum machine_mode,
376                                          unsigned HOST_WIDE_INT,
377                                          unsigned HOST_WIDE_INT *);
378 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, enum machine_mode, const_rtx,
379                                                 enum machine_mode,
380                                                 unsigned int, unsigned int *);
381 static void do_SUBST (rtx *, rtx);
382 static void do_SUBST_INT (int *, int);
383 static void init_reg_last (void);
384 static void setup_incoming_promotions (rtx);
385 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
386 static int cant_combine_insn_p (rtx);
387 static int can_combine_p (rtx, rtx, rtx, rtx, rtx, rtx, rtx *, rtx *);
388 static int combinable_i3pat (rtx, rtx *, rtx, rtx, rtx, int, int, rtx *);
389 static int contains_muldiv (rtx);
390 static rtx try_combine (rtx, rtx, rtx, rtx, int *);
391 static void undo_all (void);
392 static void undo_commit (void);
393 static rtx *find_split_point (rtx *, rtx, bool);
394 static rtx subst (rtx, rtx, rtx, int, int);
395 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
396 static rtx simplify_if_then_else (rtx);
397 static rtx simplify_set (rtx);
398 static rtx simplify_logical (rtx);
399 static rtx expand_compound_operation (rtx);
400 static const_rtx expand_field_assignment (const_rtx);
401 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
402                             rtx, unsigned HOST_WIDE_INT, int, int, int);
403 static rtx extract_left_shift (rtx, int);
404 static rtx make_compound_operation (rtx, enum rtx_code);
405 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
406                               unsigned HOST_WIDE_INT *);
407 static rtx canon_reg_for_combine (rtx, rtx);
408 static rtx force_to_mode (rtx, enum machine_mode,
409                           unsigned HOST_WIDE_INT, int);
410 static rtx if_then_else_cond (rtx, rtx *, rtx *);
411 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
412 static int rtx_equal_for_field_assignment_p (rtx, rtx);
413 static rtx make_field_assignment (rtx);
414 static rtx apply_distributive_law (rtx);
415 static rtx distribute_and_simplify_rtx (rtx, int);
416 static rtx simplify_and_const_int_1 (enum machine_mode, rtx,
417                                      unsigned HOST_WIDE_INT);
418 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
419                                    unsigned HOST_WIDE_INT);
420 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
421                             HOST_WIDE_INT, enum machine_mode, int *);
422 static rtx simplify_shift_const_1 (enum rtx_code, enum machine_mode, rtx, int);
423 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
424                                  int);
425 static int recog_for_combine (rtx *, rtx, rtx *);
426 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
427 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
428 static void update_table_tick (rtx);
429 static void record_value_for_reg (rtx, rtx, rtx);
430 static void check_promoted_subreg (rtx, rtx);
431 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
432 static void record_dead_and_set_regs (rtx);
433 static int get_last_value_validate (rtx *, rtx, int, int);
434 static rtx get_last_value (const_rtx);
435 static int use_crosses_set_p (const_rtx, int);
436 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
437 static int reg_dead_at_p (rtx, rtx);
438 static void move_deaths (rtx, rtx, int, rtx, rtx *);
439 static int reg_bitfield_target_p (rtx, rtx);
440 static void distribute_notes (rtx, rtx, rtx, rtx, rtx, rtx, rtx);
441 static void distribute_links (rtx);
442 static void mark_used_regs_combine (rtx);
443 static void record_promoted_value (rtx, rtx);
444 static int unmentioned_reg_p_1 (rtx *, void *);
445 static bool unmentioned_reg_p (rtx, rtx);
446 static int record_truncated_value (rtx *, void *);
447 static void record_truncated_values (rtx *, void *);
448 static bool reg_truncated_to_mode (enum machine_mode, const_rtx);
449 static rtx gen_lowpart_or_truncate (enum machine_mode, rtx);
450 \f
451
452 /* It is not safe to use ordinary gen_lowpart in combine.
453    See comments in gen_lowpart_for_combine.  */
454 #undef RTL_HOOKS_GEN_LOWPART
455 #define RTL_HOOKS_GEN_LOWPART              gen_lowpart_for_combine
456
457 /* Our implementation of gen_lowpart never emits a new pseudo.  */
458 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
459 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT      gen_lowpart_for_combine
460
461 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
462 #define RTL_HOOKS_REG_NONZERO_REG_BITS     reg_nonzero_bits_for_combine
463
464 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
465 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES  reg_num_sign_bit_copies_for_combine
466
467 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
468 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE    reg_truncated_to_mode
469
470 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
471
472 \f
473 /* Try to split PATTERN found in INSN.  This returns NULL_RTX if
474    PATTERN can not be split.  Otherwise, it returns an insn sequence.
475    This is a wrapper around split_insns which ensures that the
476    reg_stat vector is made larger if the splitter creates a new
477    register.  */
478
479 static rtx
480 combine_split_insns (rtx pattern, rtx insn)
481 {
482   rtx ret;
483   unsigned int nregs;
484
485   ret = split_insns (pattern, insn);
486   nregs = max_reg_num ();
487   if (nregs > VEC_length (reg_stat_type, reg_stat))
488     VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
489   return ret;
490 }
491
492 /* This is used by find_single_use to locate an rtx in LOC that
493    contains exactly one use of DEST, which is typically either a REG
494    or CC0.  It returns a pointer to the innermost rtx expression
495    containing DEST.  Appearances of DEST that are being used to
496    totally replace it are not counted.  */
497
498 static rtx *
499 find_single_use_1 (rtx dest, rtx *loc)
500 {
501   rtx x = *loc;
502   enum rtx_code code = GET_CODE (x);
503   rtx *result = NULL;
504   rtx *this_result;
505   int i;
506   const char *fmt;
507
508   switch (code)
509     {
510     case CONST_INT:
511     case CONST:
512     case LABEL_REF:
513     case SYMBOL_REF:
514     case CONST_DOUBLE:
515     case CONST_VECTOR:
516     case CLOBBER:
517       return 0;
518
519     case SET:
520       /* If the destination is anything other than CC0, PC, a REG or a SUBREG
521          of a REG that occupies all of the REG, the insn uses DEST if
522          it is mentioned in the destination or the source.  Otherwise, we
523          need just check the source.  */
524       if (GET_CODE (SET_DEST (x)) != CC0
525           && GET_CODE (SET_DEST (x)) != PC
526           && !REG_P (SET_DEST (x))
527           && ! (GET_CODE (SET_DEST (x)) == SUBREG
528                 && REG_P (SUBREG_REG (SET_DEST (x)))
529                 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
530                       + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
531                     == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
532                          + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
533         break;
534
535       return find_single_use_1 (dest, &SET_SRC (x));
536
537     case MEM:
538     case SUBREG:
539       return find_single_use_1 (dest, &XEXP (x, 0));
540
541     default:
542       break;
543     }
544
545   /* If it wasn't one of the common cases above, check each expression and
546      vector of this code.  Look for a unique usage of DEST.  */
547
548   fmt = GET_RTX_FORMAT (code);
549   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
550     {
551       if (fmt[i] == 'e')
552         {
553           if (dest == XEXP (x, i)
554               || (REG_P (dest) && REG_P (XEXP (x, i))
555                   && REGNO (dest) == REGNO (XEXP (x, i))))
556             this_result = loc;
557           else
558             this_result = find_single_use_1 (dest, &XEXP (x, i));
559
560           if (result == NULL)
561             result = this_result;
562           else if (this_result)
563             /* Duplicate usage.  */
564             return NULL;
565         }
566       else if (fmt[i] == 'E')
567         {
568           int j;
569
570           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
571             {
572               if (XVECEXP (x, i, j) == dest
573                   || (REG_P (dest)
574                       && REG_P (XVECEXP (x, i, j))
575                       && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
576                 this_result = loc;
577               else
578                 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
579
580               if (result == NULL)
581                 result = this_result;
582               else if (this_result)
583                 return NULL;
584             }
585         }
586     }
587
588   return result;
589 }
590
591
592 /* See if DEST, produced in INSN, is used only a single time in the
593    sequel.  If so, return a pointer to the innermost rtx expression in which
594    it is used.
595
596    If PLOC is nonzero, *PLOC is set to the insn containing the single use.
597
598    If DEST is cc0_rtx, we look only at the next insn.  In that case, we don't
599    care about REG_DEAD notes or LOG_LINKS.
600
601    Otherwise, we find the single use by finding an insn that has a
602    LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST.  If DEST is
603    only referenced once in that insn, we know that it must be the first
604    and last insn referencing DEST.  */
605
606 static rtx *
607 find_single_use (rtx dest, rtx insn, rtx *ploc)
608 {
609   basic_block bb;
610   rtx next;
611   rtx *result;
612   rtx link;
613
614 #ifdef HAVE_cc0
615   if (dest == cc0_rtx)
616     {
617       next = NEXT_INSN (insn);
618       if (next == 0
619           || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
620         return 0;
621
622       result = find_single_use_1 (dest, &PATTERN (next));
623       if (result && ploc)
624         *ploc = next;
625       return result;
626     }
627 #endif
628
629   if (!REG_P (dest))
630     return 0;
631
632   bb = BLOCK_FOR_INSN (insn);
633   for (next = NEXT_INSN (insn);
634        next && BLOCK_FOR_INSN (next) == bb;
635        next = NEXT_INSN (next))
636     if (INSN_P (next) && dead_or_set_p (next, dest))
637       {
638         for (link = LOG_LINKS (next); link; link = XEXP (link, 1))
639           if (XEXP (link, 0) == insn)
640             break;
641
642         if (link)
643           {
644             result = find_single_use_1 (dest, &PATTERN (next));
645             if (ploc)
646               *ploc = next;
647             return result;
648           }
649       }
650
651   return 0;
652 }
653 \f
654 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
655    insn.  The substitution can be undone by undo_all.  If INTO is already
656    set to NEWVAL, do not record this change.  Because computing NEWVAL might
657    also call SUBST, we have to compute it before we put anything into
658    the undo table.  */
659
660 static void
661 do_SUBST (rtx *into, rtx newval)
662 {
663   struct undo *buf;
664   rtx oldval = *into;
665
666   if (oldval == newval)
667     return;
668
669   /* We'd like to catch as many invalid transformations here as
670      possible.  Unfortunately, there are way too many mode changes
671      that are perfectly valid, so we'd waste too much effort for
672      little gain doing the checks here.  Focus on catching invalid
673      transformations involving integer constants.  */
674   if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
675       && CONST_INT_P (newval))
676     {
677       /* Sanity check that we're replacing oldval with a CONST_INT
678          that is a valid sign-extension for the original mode.  */
679       gcc_assert (INTVAL (newval)
680                   == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
681
682       /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
683          CONST_INT is not valid, because after the replacement, the
684          original mode would be gone.  Unfortunately, we can't tell
685          when do_SUBST is called to replace the operand thereof, so we
686          perform this test on oldval instead, checking whether an
687          invalid replacement took place before we got here.  */
688       gcc_assert (!(GET_CODE (oldval) == SUBREG
689                     && CONST_INT_P (SUBREG_REG (oldval))));
690       gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
691                     && CONST_INT_P (XEXP (oldval, 0))));
692     }
693
694   if (undobuf.frees)
695     buf = undobuf.frees, undobuf.frees = buf->next;
696   else
697     buf = XNEW (struct undo);
698
699   buf->kind = UNDO_RTX;
700   buf->where.r = into;
701   buf->old_contents.r = oldval;
702   *into = newval;
703
704   buf->next = undobuf.undos, undobuf.undos = buf;
705 }
706
707 #define SUBST(INTO, NEWVAL)     do_SUBST(&(INTO), (NEWVAL))
708
709 /* Similar to SUBST, but NEWVAL is an int expression.  Note that substitution
710    for the value of a HOST_WIDE_INT value (including CONST_INT) is
711    not safe.  */
712
713 static void
714 do_SUBST_INT (int *into, int newval)
715 {
716   struct undo *buf;
717   int oldval = *into;
718
719   if (oldval == newval)
720     return;
721
722   if (undobuf.frees)
723     buf = undobuf.frees, undobuf.frees = buf->next;
724   else
725     buf = XNEW (struct undo);
726
727   buf->kind = UNDO_INT;
728   buf->where.i = into;
729   buf->old_contents.i = oldval;
730   *into = newval;
731
732   buf->next = undobuf.undos, undobuf.undos = buf;
733 }
734
735 #define SUBST_INT(INTO, NEWVAL)  do_SUBST_INT(&(INTO), (NEWVAL))
736
737 /* Similar to SUBST, but just substitute the mode.  This is used when
738    changing the mode of a pseudo-register, so that any other
739    references to the entry in the regno_reg_rtx array will change as
740    well.  */
741
742 static void
743 do_SUBST_MODE (rtx *into, enum machine_mode newval)
744 {
745   struct undo *buf;
746   enum machine_mode oldval = GET_MODE (*into);
747
748   if (oldval == newval)
749     return;
750
751   if (undobuf.frees)
752     buf = undobuf.frees, undobuf.frees = buf->next;
753   else
754     buf = XNEW (struct undo);
755
756   buf->kind = UNDO_MODE;
757   buf->where.r = into;
758   buf->old_contents.m = oldval;
759   adjust_reg_mode (*into, newval);
760
761   buf->next = undobuf.undos, undobuf.undos = buf;
762 }
763
764 #define SUBST_MODE(INTO, NEWVAL)  do_SUBST_MODE(&(INTO), (NEWVAL))
765 \f
766 /* Subroutine of try_combine.  Determine whether the combine replacement
767    patterns NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to
768    insn_rtx_cost that the original instruction sequence I0, I1, I2, I3 and
769    undobuf.other_insn.  Note that I1 and/or NEWI2PAT may be NULL_RTX.
770    NEWOTHERPAT and undobuf.other_insn may also both be NULL_RTX.  This
771    function returns false, if the costs of all instructions can be
772    estimated, and the replacements are more expensive than the original
773    sequence.  */
774
775 static bool
776 combine_validate_cost (rtx i0, rtx i1, rtx i2, rtx i3, rtx newpat,
777                        rtx newi2pat, rtx newotherpat)
778 {
779   int i0_cost, i1_cost, i2_cost, i3_cost;
780   int new_i2_cost, new_i3_cost;
781   int old_cost, new_cost;
782
783   /* Lookup the original insn_rtx_costs.  */
784   i2_cost = INSN_COST (i2);
785   i3_cost = INSN_COST (i3);
786
787   if (i1)
788     {
789       i1_cost = INSN_COST (i1);
790       if (i0)
791         {
792           i0_cost = INSN_COST (i0);
793           old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
794                       ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
795         }
796       else
797         {
798           old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
799                       ? i1_cost + i2_cost + i3_cost : 0);
800           i0_cost = 0;
801         }
802     }
803   else
804     {
805       old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
806       i1_cost = i0_cost = 0;
807     }
808
809   /* Calculate the replacement insn_rtx_costs.  */
810   new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
811   if (newi2pat)
812     {
813       new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
814       new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
815                  ? new_i2_cost + new_i3_cost : 0;
816     }
817   else
818     {
819       new_cost = new_i3_cost;
820       new_i2_cost = 0;
821     }
822
823   if (undobuf.other_insn)
824     {
825       int old_other_cost, new_other_cost;
826
827       old_other_cost = INSN_COST (undobuf.other_insn);
828       new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
829       if (old_other_cost > 0 && new_other_cost > 0)
830         {
831           old_cost += old_other_cost;
832           new_cost += new_other_cost;
833         }
834       else
835         old_cost = 0;
836     }
837
838   /* Disallow this recombination if both new_cost and old_cost are
839      greater than zero, and new_cost is greater than old cost.  */
840   if (old_cost > 0
841       && new_cost > old_cost)
842     {
843       if (dump_file)
844         {
845           if (i0)
846             {
847               fprintf (dump_file,
848                        "rejecting combination of insns %d, %d, %d and %d\n",
849                        INSN_UID (i0), INSN_UID (i1), INSN_UID (i2),
850                        INSN_UID (i3));
851               fprintf (dump_file, "original costs %d + %d + %d + %d = %d\n",
852                        i0_cost, i1_cost, i2_cost, i3_cost, old_cost);
853             }
854           else if (i1)
855             {
856               fprintf (dump_file,
857                        "rejecting combination of insns %d, %d and %d\n",
858                        INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
859               fprintf (dump_file, "original costs %d + %d + %d = %d\n",
860                        i1_cost, i2_cost, i3_cost, old_cost);
861             }
862           else
863             {
864               fprintf (dump_file,
865                        "rejecting combination of insns %d and %d\n",
866                        INSN_UID (i2), INSN_UID (i3));
867               fprintf (dump_file, "original costs %d + %d = %d\n",
868                        i2_cost, i3_cost, old_cost);
869             }
870
871           if (newi2pat)
872             {
873               fprintf (dump_file, "replacement costs %d + %d = %d\n",
874                        new_i2_cost, new_i3_cost, new_cost);
875             }
876           else
877             fprintf (dump_file, "replacement cost %d\n", new_cost);
878         }
879
880       return false;
881     }
882
883   /* Update the uid_insn_cost array with the replacement costs.  */
884   INSN_COST (i2) = new_i2_cost;
885   INSN_COST (i3) = new_i3_cost;
886   if (i1)
887     INSN_COST (i1) = 0;
888
889   return true;
890 }
891
892
893 /* Delete any insns that copy a register to itself.  */
894
895 static void
896 delete_noop_moves (void)
897 {
898   rtx insn, next;
899   basic_block bb;
900
901   FOR_EACH_BB (bb)
902     {
903       for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
904         {
905           next = NEXT_INSN (insn);
906           if (INSN_P (insn) && noop_move_p (insn))
907             {
908               if (dump_file)
909                 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
910
911               delete_insn_and_edges (insn);
912             }
913         }
914     }
915 }
916
917 \f
918 /* Fill in log links field for all insns.  */
919
920 static void
921 create_log_links (void)
922 {
923   basic_block bb;
924   rtx *next_use, insn;
925   df_ref *def_vec, *use_vec;
926
927   next_use = XCNEWVEC (rtx, max_reg_num ());
928
929   /* Pass through each block from the end, recording the uses of each
930      register and establishing log links when def is encountered.
931      Note that we do not clear next_use array in order to save time,
932      so we have to test whether the use is in the same basic block as def.
933
934      There are a few cases below when we do not consider the definition or
935      usage -- these are taken from original flow.c did. Don't ask me why it is
936      done this way; I don't know and if it works, I don't want to know.  */
937
938   FOR_EACH_BB (bb)
939     {
940       FOR_BB_INSNS_REVERSE (bb, insn)
941         {
942           if (!NONDEBUG_INSN_P (insn))
943             continue;
944
945           /* Log links are created only once.  */
946           gcc_assert (!LOG_LINKS (insn));
947
948           for (def_vec = DF_INSN_DEFS (insn); *def_vec; def_vec++)
949             {
950               df_ref def = *def_vec;
951               int regno = DF_REF_REGNO (def);
952               rtx use_insn;
953
954               if (!next_use[regno])
955                 continue;
956
957               /* Do not consider if it is pre/post modification in MEM.  */
958               if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
959                 continue;
960
961               /* Do not make the log link for frame pointer.  */
962               if ((regno == FRAME_POINTER_REGNUM
963                    && (! reload_completed || frame_pointer_needed))
964 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
965                   || (regno == HARD_FRAME_POINTER_REGNUM
966                       && (! reload_completed || frame_pointer_needed))
967 #endif
968 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
969                   || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
970 #endif
971                   )
972                 continue;
973
974               use_insn = next_use[regno];
975               if (BLOCK_FOR_INSN (use_insn) == bb)
976                 {
977                   /* flow.c claimed:
978
979                      We don't build a LOG_LINK for hard registers contained
980                      in ASM_OPERANDs.  If these registers get replaced,
981                      we might wind up changing the semantics of the insn,
982                      even if reload can make what appear to be valid
983                      assignments later.  */
984                   if (regno >= FIRST_PSEUDO_REGISTER
985                       || asm_noperands (PATTERN (use_insn)) < 0)
986                     {
987                       /* Don't add duplicate links between instructions.  */
988                       rtx links;
989                       for (links = LOG_LINKS (use_insn); links;
990                            links = XEXP (links, 1))
991                         if (insn == XEXP (links, 0))
992                           break;
993
994                       if (!links)
995                         LOG_LINKS (use_insn) =
996                           alloc_INSN_LIST (insn, LOG_LINKS (use_insn));
997                     }
998                 }
999               next_use[regno] = NULL_RTX;
1000             }
1001
1002           for (use_vec = DF_INSN_USES (insn); *use_vec; use_vec++)
1003             {
1004               df_ref use = *use_vec;
1005               int regno = DF_REF_REGNO (use);
1006
1007               /* Do not consider the usage of the stack pointer
1008                  by function call.  */
1009               if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1010                 continue;
1011
1012               next_use[regno] = insn;
1013             }
1014         }
1015     }
1016
1017   free (next_use);
1018 }
1019
1020 /* Clear LOG_LINKS fields of insns.  */
1021
1022 static void
1023 clear_log_links (void)
1024 {
1025   rtx insn;
1026
1027   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1028     if (INSN_P (insn))
1029       free_INSN_LIST_list (&LOG_LINKS (insn));
1030 }
1031
1032 /* Walk the LOG_LINKS of insn B to see if we find a reference to A.  Return
1033    true if we found a LOG_LINK that proves that A feeds B.  This only works
1034    if there are no instructions between A and B which could have a link
1035    depending on A, since in that case we would not record a link for B.  */
1036
1037 static bool
1038 insn_a_feeds_b (rtx a, rtx b)
1039 {
1040   rtx links;
1041   for (links = LOG_LINKS (b); links; links = XEXP (links, 1))
1042     if (XEXP (links, 0) == a)
1043       return true;
1044   return false;
1045 }
1046 \f
1047 /* Main entry point for combiner.  F is the first insn of the function.
1048    NREGS is the first unused pseudo-reg number.
1049
1050    Return nonzero if the combiner has turned an indirect jump
1051    instruction into a direct jump.  */
1052 static int
1053 combine_instructions (rtx f, unsigned int nregs)
1054 {
1055   rtx insn, next;
1056 #ifdef HAVE_cc0
1057   rtx prev;
1058 #endif
1059   rtx links, nextlinks;
1060   rtx first;
1061   basic_block last_bb;
1062
1063   int new_direct_jump_p = 0;
1064
1065   for (first = f; first && !INSN_P (first); )
1066     first = NEXT_INSN (first);
1067   if (!first)
1068     return 0;
1069
1070   combine_attempts = 0;
1071   combine_merges = 0;
1072   combine_extras = 0;
1073   combine_successes = 0;
1074
1075   rtl_hooks = combine_rtl_hooks;
1076
1077   VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
1078
1079   init_recog_no_volatile ();
1080
1081   /* Allocate array for insn info.  */
1082   max_uid_known = get_max_uid ();
1083   uid_log_links = XCNEWVEC (rtx, max_uid_known + 1);
1084   uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1085
1086   nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1087
1088   /* Don't use reg_stat[].nonzero_bits when computing it.  This can cause
1089      problems when, for example, we have j <<= 1 in a loop.  */
1090
1091   nonzero_sign_valid = 0;
1092   label_tick = label_tick_ebb_start = 1;
1093
1094   /* Scan all SETs and see if we can deduce anything about what
1095      bits are known to be zero for some registers and how many copies
1096      of the sign bit are known to exist for those registers.
1097
1098      Also set any known values so that we can use it while searching
1099      for what bits are known to be set.  */
1100
1101   setup_incoming_promotions (first);
1102   /* Allow the entry block and the first block to fall into the same EBB.
1103      Conceptually the incoming promotions are assigned to the entry block.  */
1104   last_bb = ENTRY_BLOCK_PTR;
1105
1106   create_log_links ();
1107   FOR_EACH_BB (this_basic_block)
1108     {
1109       optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1110       last_call_luid = 0;
1111       mem_last_set = -1;
1112
1113       label_tick++;
1114       if (!single_pred_p (this_basic_block)
1115           || single_pred (this_basic_block) != last_bb)
1116         label_tick_ebb_start = label_tick;
1117       last_bb = this_basic_block;
1118
1119       FOR_BB_INSNS (this_basic_block, insn)
1120         if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1121           {
1122             subst_low_luid = DF_INSN_LUID (insn);
1123             subst_insn = insn;
1124
1125             note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1126                          insn);
1127             record_dead_and_set_regs (insn);
1128
1129 #ifdef AUTO_INC_DEC
1130             for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1131               if (REG_NOTE_KIND (links) == REG_INC)
1132                 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1133                                                   insn);
1134 #endif
1135
1136             /* Record the current insn_rtx_cost of this instruction.  */
1137             if (NONJUMP_INSN_P (insn))
1138               INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1139                                                 optimize_this_for_speed_p);
1140             if (dump_file)
1141               fprintf(dump_file, "insn_cost %d: %d\n",
1142                     INSN_UID (insn), INSN_COST (insn));
1143           }
1144     }
1145
1146   nonzero_sign_valid = 1;
1147
1148   /* Now scan all the insns in forward order.  */
1149   label_tick = label_tick_ebb_start = 1;
1150   init_reg_last ();
1151   setup_incoming_promotions (first);
1152   last_bb = ENTRY_BLOCK_PTR;
1153
1154   FOR_EACH_BB (this_basic_block)
1155     {
1156       optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1157       last_call_luid = 0;
1158       mem_last_set = -1;
1159
1160       label_tick++;
1161       if (!single_pred_p (this_basic_block)
1162           || single_pred (this_basic_block) != last_bb)
1163         label_tick_ebb_start = label_tick;
1164       last_bb = this_basic_block;
1165
1166       rtl_profile_for_bb (this_basic_block);
1167       for (insn = BB_HEAD (this_basic_block);
1168            insn != NEXT_INSN (BB_END (this_basic_block));
1169            insn = next ? next : NEXT_INSN (insn))
1170         {
1171           next = 0;
1172           if (NONDEBUG_INSN_P (insn))
1173             {
1174               /* See if we know about function return values before this
1175                  insn based upon SUBREG flags.  */
1176               check_promoted_subreg (insn, PATTERN (insn));
1177
1178               /* See if we can find hardregs and subreg of pseudos in
1179                  narrower modes.  This could help turning TRUNCATEs
1180                  into SUBREGs.  */
1181               note_uses (&PATTERN (insn), record_truncated_values, NULL);
1182
1183               /* Try this insn with each insn it links back to.  */
1184
1185               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1186                 if ((next = try_combine (insn, XEXP (links, 0), NULL_RTX,
1187                                          NULL_RTX, &new_direct_jump_p)) != 0)
1188                   goto retry;
1189
1190               /* Try each sequence of three linked insns ending with this one.  */
1191
1192               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1193                 {
1194                   rtx link = XEXP (links, 0);
1195
1196                   /* If the linked insn has been replaced by a note, then there
1197                      is no point in pursuing this chain any further.  */
1198                   if (NOTE_P (link))
1199                     continue;
1200
1201                   for (nextlinks = LOG_LINKS (link);
1202                        nextlinks;
1203                        nextlinks = XEXP (nextlinks, 1))
1204                     if ((next = try_combine (insn, link, XEXP (nextlinks, 0),
1205                                              NULL_RTX,
1206                                              &new_direct_jump_p)) != 0)
1207                       goto retry;
1208                 }
1209
1210 #ifdef HAVE_cc0
1211               /* Try to combine a jump insn that uses CC0
1212                  with a preceding insn that sets CC0, and maybe with its
1213                  logical predecessor as well.
1214                  This is how we make decrement-and-branch insns.
1215                  We need this special code because data flow connections
1216                  via CC0 do not get entered in LOG_LINKS.  */
1217
1218               if (JUMP_P (insn)
1219                   && (prev = prev_nonnote_insn (insn)) != 0
1220                   && NONJUMP_INSN_P (prev)
1221                   && sets_cc0_p (PATTERN (prev)))
1222                 {
1223                   if ((next = try_combine (insn, prev, NULL_RTX, NULL_RTX,
1224                                            &new_direct_jump_p)) != 0)
1225                     goto retry;
1226
1227                   for (nextlinks = LOG_LINKS (prev); nextlinks;
1228                        nextlinks = XEXP (nextlinks, 1))
1229                     if ((next = try_combine (insn, prev, XEXP (nextlinks, 0),
1230                                              NULL_RTX,
1231                                              &new_direct_jump_p)) != 0)
1232                       goto retry;
1233                 }
1234
1235               /* Do the same for an insn that explicitly references CC0.  */
1236               if (NONJUMP_INSN_P (insn)
1237                   && (prev = prev_nonnote_insn (insn)) != 0
1238                   && NONJUMP_INSN_P (prev)
1239                   && sets_cc0_p (PATTERN (prev))
1240                   && GET_CODE (PATTERN (insn)) == SET
1241                   && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1242                 {
1243                   if ((next = try_combine (insn, prev, NULL_RTX, NULL_RTX,
1244                                            &new_direct_jump_p)) != 0)
1245                     goto retry;
1246
1247                   for (nextlinks = LOG_LINKS (prev); nextlinks;
1248                        nextlinks = XEXP (nextlinks, 1))
1249                     if ((next = try_combine (insn, prev, XEXP (nextlinks, 0),
1250                                              NULL_RTX,
1251                                              &new_direct_jump_p)) != 0)
1252                       goto retry;
1253                 }
1254
1255               /* Finally, see if any of the insns that this insn links to
1256                  explicitly references CC0.  If so, try this insn, that insn,
1257                  and its predecessor if it sets CC0.  */
1258               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1259                 if (NONJUMP_INSN_P (XEXP (links, 0))
1260                     && GET_CODE (PATTERN (XEXP (links, 0))) == SET
1261                     && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
1262                     && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
1263                     && NONJUMP_INSN_P (prev)
1264                     && sets_cc0_p (PATTERN (prev))
1265                     && (next = try_combine (insn, XEXP (links, 0),
1266                                             prev, NULL_RTX,
1267                                             &new_direct_jump_p)) != 0)
1268                   goto retry;
1269 #endif
1270
1271               /* Try combining an insn with two different insns whose results it
1272                  uses.  */
1273               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1274                 for (nextlinks = XEXP (links, 1); nextlinks;
1275                      nextlinks = XEXP (nextlinks, 1))
1276                   if ((next = try_combine (insn, XEXP (links, 0),
1277                                            XEXP (nextlinks, 0), NULL_RTX,
1278                                            &new_direct_jump_p)) != 0)
1279                     goto retry;
1280
1281               /* Try four-instruction combinations.  */
1282               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1283                 {
1284                   rtx next1;
1285                   rtx link = XEXP (links, 0);
1286
1287                   /* If the linked insn has been replaced by a note, then there
1288                      is no point in pursuing this chain any further.  */
1289                   if (NOTE_P (link))
1290                     continue;
1291
1292                   for (next1 = LOG_LINKS (link); next1; next1 = XEXP (next1, 1))
1293                     {
1294                       rtx link1 = XEXP (next1, 0);
1295                       if (NOTE_P (link1))
1296                         continue;
1297                       /* I0 -> I1 -> I2 -> I3.  */
1298                       for (nextlinks = LOG_LINKS (link1); nextlinks;
1299                            nextlinks = XEXP (nextlinks, 1))
1300                         if ((next = try_combine (insn, link, link1,
1301                                                  XEXP (nextlinks, 0),
1302                                                  &new_direct_jump_p)) != 0)
1303                           goto retry;
1304                       /* I0, I1 -> I2, I2 -> I3.  */
1305                       for (nextlinks = XEXP (next1, 1); nextlinks;
1306                            nextlinks = XEXP (nextlinks, 1))
1307                         if ((next = try_combine (insn, link, link1,
1308                                                  XEXP (nextlinks, 0),
1309                                                  &new_direct_jump_p)) != 0)
1310                           goto retry;
1311                     }
1312
1313                   for (next1 = XEXP (links, 1); next1; next1 = XEXP (next1, 1))
1314                     {
1315                       rtx link1 = XEXP (next1, 0);
1316                       if (NOTE_P (link1))
1317                         continue;
1318                       /* I0 -> I2; I1, I2 -> I3.  */
1319                       for (nextlinks = LOG_LINKS (link); nextlinks;
1320                            nextlinks = XEXP (nextlinks, 1))
1321                         if ((next = try_combine (insn, link, link1,
1322                                                  XEXP (nextlinks, 0),
1323                                                  &new_direct_jump_p)) != 0)
1324                           goto retry;
1325                       /* I0 -> I1; I1, I2 -> I3.  */
1326                       for (nextlinks = LOG_LINKS (link1); nextlinks;
1327                            nextlinks = XEXP (nextlinks, 1))
1328                         if ((next = try_combine (insn, link, link1,
1329                                                  XEXP (nextlinks, 0),
1330                                                  &new_direct_jump_p)) != 0)
1331                           goto retry;
1332                     }
1333                 }
1334
1335               /* Try this insn with each REG_EQUAL note it links back to.  */
1336               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1337                 {
1338                   rtx set, note;
1339                   rtx temp = XEXP (links, 0);
1340                   if ((set = single_set (temp)) != 0
1341                       && (note = find_reg_equal_equiv_note (temp)) != 0
1342                       && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1343                       /* Avoid using a register that may already been marked
1344                          dead by an earlier instruction.  */
1345                       && ! unmentioned_reg_p (note, SET_SRC (set))
1346                       && (GET_MODE (note) == VOIDmode
1347                           ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1348                           : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1349                     {
1350                       /* Temporarily replace the set's source with the
1351                          contents of the REG_EQUAL note.  The insn will
1352                          be deleted or recognized by try_combine.  */
1353                       rtx orig = SET_SRC (set);
1354                       SET_SRC (set) = note;
1355                       i2mod = temp;
1356                       i2mod_old_rhs = copy_rtx (orig);
1357                       i2mod_new_rhs = copy_rtx (note);
1358                       next = try_combine (insn, i2mod, NULL_RTX, NULL_RTX,
1359                                           &new_direct_jump_p);
1360                       i2mod = NULL_RTX;
1361                       if (next)
1362                         goto retry;
1363                       SET_SRC (set) = orig;
1364                     }
1365                 }
1366
1367               if (!NOTE_P (insn))
1368                 record_dead_and_set_regs (insn);
1369
1370             retry:
1371               ;
1372             }
1373         }
1374     }
1375
1376   default_rtl_profile ();
1377   clear_log_links ();
1378   clear_bb_flags ();
1379   new_direct_jump_p |= purge_all_dead_edges ();
1380   delete_noop_moves ();
1381
1382   /* Clean up.  */
1383   free (uid_log_links);
1384   free (uid_insn_cost);
1385   VEC_free (reg_stat_type, heap, reg_stat);
1386
1387   {
1388     struct undo *undo, *next;
1389     for (undo = undobuf.frees; undo; undo = next)
1390       {
1391         next = undo->next;
1392         free (undo);
1393       }
1394     undobuf.frees = 0;
1395   }
1396
1397   total_attempts += combine_attempts;
1398   total_merges += combine_merges;
1399   total_extras += combine_extras;
1400   total_successes += combine_successes;
1401
1402   nonzero_sign_valid = 0;
1403   rtl_hooks = general_rtl_hooks;
1404
1405   /* Make recognizer allow volatile MEMs again.  */
1406   init_recog ();
1407
1408   return new_direct_jump_p;
1409 }
1410
1411 /* Wipe the last_xxx fields of reg_stat in preparation for another pass.  */
1412
1413 static void
1414 init_reg_last (void)
1415 {
1416   unsigned int i;
1417   reg_stat_type *p;
1418
1419   FOR_EACH_VEC_ELT (reg_stat_type, reg_stat, i, p)
1420     memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1421 }
1422 \f
1423 /* Set up any promoted values for incoming argument registers.  */
1424
1425 static void
1426 setup_incoming_promotions (rtx first)
1427 {
1428   tree arg;
1429   bool strictly_local = false;
1430
1431   for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1432        arg = DECL_CHAIN (arg))
1433     {
1434       rtx x, reg = DECL_INCOMING_RTL (arg);
1435       int uns1, uns3;
1436       enum machine_mode mode1, mode2, mode3, mode4;
1437
1438       /* Only continue if the incoming argument is in a register.  */
1439       if (!REG_P (reg))
1440         continue;
1441
1442       /* Determine, if possible, whether all call sites of the current
1443          function lie within the current compilation unit.  (This does
1444          take into account the exporting of a function via taking its
1445          address, and so forth.)  */
1446       strictly_local = cgraph_local_info (current_function_decl)->local;
1447
1448       /* The mode and signedness of the argument before any promotions happen
1449          (equal to the mode of the pseudo holding it at that stage).  */
1450       mode1 = TYPE_MODE (TREE_TYPE (arg));
1451       uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1452
1453       /* The mode and signedness of the argument after any source language and
1454          TARGET_PROMOTE_PROTOTYPES-driven promotions.  */
1455       mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1456       uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1457
1458       /* The mode and signedness of the argument as it is actually passed,
1459          after any TARGET_PROMOTE_FUNCTION_ARGS-driven ABI promotions.  */
1460       mode3 = promote_function_mode (DECL_ARG_TYPE (arg), mode2, &uns3,
1461                                      TREE_TYPE (cfun->decl), 0);
1462
1463       /* The mode of the register in which the argument is being passed.  */
1464       mode4 = GET_MODE (reg);
1465
1466       /* Eliminate sign extensions in the callee when:
1467          (a) A mode promotion has occurred;  */
1468       if (mode1 == mode3)
1469         continue;
1470       /* (b) The mode of the register is the same as the mode of
1471              the argument as it is passed; */
1472       if (mode3 != mode4)
1473         continue;
1474       /* (c) There's no language level extension;  */
1475       if (mode1 == mode2)
1476         ;
1477       /* (c.1) All callers are from the current compilation unit.  If that's
1478          the case we don't have to rely on an ABI, we only have to know
1479          what we're generating right now, and we know that we will do the
1480          mode1 to mode2 promotion with the given sign.  */
1481       else if (!strictly_local)
1482         continue;
1483       /* (c.2) The combination of the two promotions is useful.  This is
1484          true when the signs match, or if the first promotion is unsigned.
1485          In the later case, (sign_extend (zero_extend x)) is the same as
1486          (zero_extend (zero_extend x)), so make sure to force UNS3 true.  */
1487       else if (uns1)
1488         uns3 = true;
1489       else if (uns3)
1490         continue;
1491
1492       /* Record that the value was promoted from mode1 to mode3,
1493          so that any sign extension at the head of the current
1494          function may be eliminated.  */
1495       x = gen_rtx_CLOBBER (mode1, const0_rtx);
1496       x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1497       record_value_for_reg (reg, first, x);
1498     }
1499 }
1500
1501 /* Called via note_stores.  If X is a pseudo that is narrower than
1502    HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1503
1504    If we are setting only a portion of X and we can't figure out what
1505    portion, assume all bits will be used since we don't know what will
1506    be happening.
1507
1508    Similarly, set how many bits of X are known to be copies of the sign bit
1509    at all locations in the function.  This is the smallest number implied
1510    by any set of X.  */
1511
1512 static void
1513 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1514 {
1515   rtx insn = (rtx) data;
1516   unsigned int num;
1517
1518   if (REG_P (x)
1519       && REGNO (x) >= FIRST_PSEUDO_REGISTER
1520       /* If this register is undefined at the start of the file, we can't
1521          say what its contents were.  */
1522       && ! REGNO_REG_SET_P
1523            (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x))
1524       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
1525     {
1526       reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
1527
1528       if (set == 0 || GET_CODE (set) == CLOBBER)
1529         {
1530           rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1531           rsp->sign_bit_copies = 1;
1532           return;
1533         }
1534
1535       /* If this register is being initialized using itself, and the
1536          register is uninitialized in this basic block, and there are
1537          no LOG_LINKS which set the register, then part of the
1538          register is uninitialized.  In that case we can't assume
1539          anything about the number of nonzero bits.
1540
1541          ??? We could do better if we checked this in
1542          reg_{nonzero_bits,num_sign_bit_copies}_for_combine.  Then we
1543          could avoid making assumptions about the insn which initially
1544          sets the register, while still using the information in other
1545          insns.  We would have to be careful to check every insn
1546          involved in the combination.  */
1547
1548       if (insn
1549           && reg_referenced_p (x, PATTERN (insn))
1550           && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1551                                REGNO (x)))
1552         {
1553           rtx link;
1554
1555           for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
1556             {
1557               if (dead_or_set_p (XEXP (link, 0), x))
1558                 break;
1559             }
1560           if (!link)
1561             {
1562               rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1563               rsp->sign_bit_copies = 1;
1564               return;
1565             }
1566         }
1567
1568       /* If this is a complex assignment, see if we can convert it into a
1569          simple assignment.  */
1570       set = expand_field_assignment (set);
1571
1572       /* If this is a simple assignment, or we have a paradoxical SUBREG,
1573          set what we know about X.  */
1574
1575       if (SET_DEST (set) == x
1576           || (GET_CODE (SET_DEST (set)) == SUBREG
1577               && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
1578                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
1579               && SUBREG_REG (SET_DEST (set)) == x))
1580         {
1581           rtx src = SET_SRC (set);
1582
1583 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1584           /* If X is narrower than a word and SRC is a non-negative
1585              constant that would appear negative in the mode of X,
1586              sign-extend it for use in reg_stat[].nonzero_bits because some
1587              machines (maybe most) will actually do the sign-extension
1588              and this is the conservative approach.
1589
1590              ??? For 2.5, try to tighten up the MD files in this regard
1591              instead of this kludge.  */
1592
1593           if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
1594               && CONST_INT_P (src)
1595               && INTVAL (src) > 0
1596               && 0 != (UINTVAL (src)
1597                        & ((unsigned HOST_WIDE_INT) 1
1598                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
1599             src = GEN_INT (UINTVAL (src)
1600                            | ((unsigned HOST_WIDE_INT) (-1)
1601                               << GET_MODE_BITSIZE (GET_MODE (x))));
1602 #endif
1603
1604           /* Don't call nonzero_bits if it cannot change anything.  */
1605           if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1606             rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1607           num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1608           if (rsp->sign_bit_copies == 0
1609               || rsp->sign_bit_copies > num)
1610             rsp->sign_bit_copies = num;
1611         }
1612       else
1613         {
1614           rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1615           rsp->sign_bit_copies = 1;
1616         }
1617     }
1618 }
1619 \f
1620 /* See if INSN can be combined into I3.  PRED, PRED2, SUCC and SUCC2 are
1621    optionally insns that were previously combined into I3 or that will be
1622    combined into the merger of INSN and I3.  The order is PRED, PRED2,
1623    INSN, SUCC, SUCC2, I3.
1624
1625    Return 0 if the combination is not allowed for any reason.
1626
1627    If the combination is allowed, *PDEST will be set to the single
1628    destination of INSN and *PSRC to the single source, and this function
1629    will return 1.  */
1630
1631 static int
1632 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED,
1633                rtx pred2 ATTRIBUTE_UNUSED, rtx succ, rtx succ2,
1634                rtx *pdest, rtx *psrc)
1635 {
1636   int i;
1637   const_rtx set = 0;
1638   rtx src, dest;
1639   rtx p;
1640 #ifdef AUTO_INC_DEC
1641   rtx link;
1642 #endif
1643   bool all_adjacent = true;
1644
1645   if (succ)
1646     {
1647       if (succ2)
1648         {
1649           if (next_active_insn (succ2) != i3)
1650             all_adjacent = false;
1651           if (next_active_insn (succ) != succ2)
1652             all_adjacent = false;
1653         }
1654       else if (next_active_insn (succ) != i3)
1655         all_adjacent = false;
1656       if (next_active_insn (insn) != succ)
1657         all_adjacent = false;
1658     }
1659   else if (next_active_insn (insn) != i3)
1660     all_adjacent = false;
1661     
1662   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1663      or a PARALLEL consisting of such a SET and CLOBBERs.
1664
1665      If INSN has CLOBBER parallel parts, ignore them for our processing.
1666      By definition, these happen during the execution of the insn.  When it
1667      is merged with another insn, all bets are off.  If they are, in fact,
1668      needed and aren't also supplied in I3, they may be added by
1669      recog_for_combine.  Otherwise, it won't match.
1670
1671      We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1672      note.
1673
1674      Get the source and destination of INSN.  If more than one, can't
1675      combine.  */
1676
1677   if (GET_CODE (PATTERN (insn)) == SET)
1678     set = PATTERN (insn);
1679   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1680            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1681     {
1682       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1683         {
1684           rtx elt = XVECEXP (PATTERN (insn), 0, i);
1685
1686           switch (GET_CODE (elt))
1687             {
1688             /* This is important to combine floating point insns
1689                for the SH4 port.  */
1690             case USE:
1691               /* Combining an isolated USE doesn't make sense.
1692                  We depend here on combinable_i3pat to reject them.  */
1693               /* The code below this loop only verifies that the inputs of
1694                  the SET in INSN do not change.  We call reg_set_between_p
1695                  to verify that the REG in the USE does not change between
1696                  I3 and INSN.
1697                  If the USE in INSN was for a pseudo register, the matching
1698                  insn pattern will likely match any register; combining this
1699                  with any other USE would only be safe if we knew that the
1700                  used registers have identical values, or if there was
1701                  something to tell them apart, e.g. different modes.  For
1702                  now, we forgo such complicated tests and simply disallow
1703                  combining of USES of pseudo registers with any other USE.  */
1704               if (REG_P (XEXP (elt, 0))
1705                   && GET_CODE (PATTERN (i3)) == PARALLEL)
1706                 {
1707                   rtx i3pat = PATTERN (i3);
1708                   int i = XVECLEN (i3pat, 0) - 1;
1709                   unsigned int regno = REGNO (XEXP (elt, 0));
1710
1711                   do
1712                     {
1713                       rtx i3elt = XVECEXP (i3pat, 0, i);
1714
1715                       if (GET_CODE (i3elt) == USE
1716                           && REG_P (XEXP (i3elt, 0))
1717                           && (REGNO (XEXP (i3elt, 0)) == regno
1718                               ? reg_set_between_p (XEXP (elt, 0),
1719                                                    PREV_INSN (insn), i3)
1720                               : regno >= FIRST_PSEUDO_REGISTER))
1721                         return 0;
1722                     }
1723                   while (--i >= 0);
1724                 }
1725               break;
1726
1727               /* We can ignore CLOBBERs.  */
1728             case CLOBBER:
1729               break;
1730
1731             case SET:
1732               /* Ignore SETs whose result isn't used but not those that
1733                  have side-effects.  */
1734               if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1735                   && insn_nothrow_p (insn)
1736                   && !side_effects_p (elt))
1737                 break;
1738
1739               /* If we have already found a SET, this is a second one and
1740                  so we cannot combine with this insn.  */
1741               if (set)
1742                 return 0;
1743
1744               set = elt;
1745               break;
1746
1747             default:
1748               /* Anything else means we can't combine.  */
1749               return 0;
1750             }
1751         }
1752
1753       if (set == 0
1754           /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1755              so don't do anything with it.  */
1756           || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1757         return 0;
1758     }
1759   else
1760     return 0;
1761
1762   if (set == 0)
1763     return 0;
1764
1765   set = expand_field_assignment (set);
1766   src = SET_SRC (set), dest = SET_DEST (set);
1767
1768   /* Don't eliminate a store in the stack pointer.  */
1769   if (dest == stack_pointer_rtx
1770       /* Don't combine with an insn that sets a register to itself if it has
1771          a REG_EQUAL note.  This may be part of a LIBCALL sequence.  */
1772       || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1773       /* Can't merge an ASM_OPERANDS.  */
1774       || GET_CODE (src) == ASM_OPERANDS
1775       /* Can't merge a function call.  */
1776       || GET_CODE (src) == CALL
1777       /* Don't eliminate a function call argument.  */
1778       || (CALL_P (i3)
1779           && (find_reg_fusage (i3, USE, dest)
1780               || (REG_P (dest)
1781                   && REGNO (dest) < FIRST_PSEUDO_REGISTER
1782                   && global_regs[REGNO (dest)])))
1783       /* Don't substitute into an incremented register.  */
1784       || FIND_REG_INC_NOTE (i3, dest)
1785       || (succ && FIND_REG_INC_NOTE (succ, dest))
1786       || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1787       /* Don't substitute into a non-local goto, this confuses CFG.  */
1788       || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1789       /* Make sure that DEST is not used after SUCC but before I3.  */
1790       || (!all_adjacent
1791           && ((succ2
1792                && (reg_used_between_p (dest, succ2, i3)
1793                    || reg_used_between_p (dest, succ, succ2)))
1794               || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
1795       /* Make sure that the value that is to be substituted for the register
1796          does not use any registers whose values alter in between.  However,
1797          If the insns are adjacent, a use can't cross a set even though we
1798          think it might (this can happen for a sequence of insns each setting
1799          the same destination; last_set of that register might point to
1800          a NOTE).  If INSN has a REG_EQUIV note, the register is always
1801          equivalent to the memory so the substitution is valid even if there
1802          are intervening stores.  Also, don't move a volatile asm or
1803          UNSPEC_VOLATILE across any other insns.  */
1804       || (! all_adjacent
1805           && (((!MEM_P (src)
1806                 || ! find_reg_note (insn, REG_EQUIV, src))
1807                && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1808               || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1809               || GET_CODE (src) == UNSPEC_VOLATILE))
1810       /* Don't combine across a CALL_INSN, because that would possibly
1811          change whether the life span of some REGs crosses calls or not,
1812          and it is a pain to update that information.
1813          Exception: if source is a constant, moving it later can't hurt.
1814          Accept that as a special case.  */
1815       || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1816     return 0;
1817
1818   /* DEST must either be a REG or CC0.  */
1819   if (REG_P (dest))
1820     {
1821       /* If register alignment is being enforced for multi-word items in all
1822          cases except for parameters, it is possible to have a register copy
1823          insn referencing a hard register that is not allowed to contain the
1824          mode being copied and which would not be valid as an operand of most
1825          insns.  Eliminate this problem by not combining with such an insn.
1826
1827          Also, on some machines we don't want to extend the life of a hard
1828          register.  */
1829
1830       if (REG_P (src)
1831           && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1832                && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1833               /* Don't extend the life of a hard register unless it is
1834                  user variable (if we have few registers) or it can't
1835                  fit into the desired register (meaning something special
1836                  is going on).
1837                  Also avoid substituting a return register into I3, because
1838                  reload can't handle a conflict with constraints of other
1839                  inputs.  */
1840               || (REGNO (src) < FIRST_PSEUDO_REGISTER
1841                   && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1842         return 0;
1843     }
1844   else if (GET_CODE (dest) != CC0)
1845     return 0;
1846
1847
1848   if (GET_CODE (PATTERN (i3)) == PARALLEL)
1849     for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1850       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1851         {
1852           /* Don't substitute for a register intended as a clobberable
1853              operand.  */
1854           rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1855           if (rtx_equal_p (reg, dest))
1856             return 0;
1857
1858           /* If the clobber represents an earlyclobber operand, we must not
1859              substitute an expression containing the clobbered register.
1860              As we do not analyze the constraint strings here, we have to
1861              make the conservative assumption.  However, if the register is
1862              a fixed hard reg, the clobber cannot represent any operand;
1863              we leave it up to the machine description to either accept or
1864              reject use-and-clobber patterns.  */
1865           if (!REG_P (reg)
1866               || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1867               || !fixed_regs[REGNO (reg)])
1868             if (reg_overlap_mentioned_p (reg, src))
1869               return 0;
1870         }
1871
1872   /* If INSN contains anything volatile, or is an `asm' (whether volatile
1873      or not), reject, unless nothing volatile comes between it and I3 */
1874
1875   if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1876     {
1877       /* Make sure neither succ nor succ2 contains a volatile reference.  */
1878       if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
1879         return 0;
1880       if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1881         return 0;
1882       /* We'll check insns between INSN and I3 below.  */
1883     }
1884
1885   /* If INSN is an asm, and DEST is a hard register, reject, since it has
1886      to be an explicit register variable, and was chosen for a reason.  */
1887
1888   if (GET_CODE (src) == ASM_OPERANDS
1889       && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1890     return 0;
1891
1892   /* If there are any volatile insns between INSN and I3, reject, because
1893      they might affect machine state.  */
1894
1895   for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1896     if (INSN_P (p) && p != succ && p != succ2 && volatile_insn_p (PATTERN (p)))
1897       return 0;
1898
1899   /* If INSN contains an autoincrement or autodecrement, make sure that
1900      register is not used between there and I3, and not already used in
1901      I3 either.  Neither must it be used in PRED or SUCC, if they exist.
1902      Also insist that I3 not be a jump; if it were one
1903      and the incremented register were spilled, we would lose.  */
1904
1905 #ifdef AUTO_INC_DEC
1906   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1907     if (REG_NOTE_KIND (link) == REG_INC
1908         && (JUMP_P (i3)
1909             || reg_used_between_p (XEXP (link, 0), insn, i3)
1910             || (pred != NULL_RTX
1911                 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1912             || (pred2 != NULL_RTX
1913                 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
1914             || (succ != NULL_RTX
1915                 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
1916             || (succ2 != NULL_RTX
1917                 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
1918             || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1919       return 0;
1920 #endif
1921
1922 #ifdef HAVE_cc0
1923   /* Don't combine an insn that follows a CC0-setting insn.
1924      An insn that uses CC0 must not be separated from the one that sets it.
1925      We do, however, allow I2 to follow a CC0-setting insn if that insn
1926      is passed as I1; in that case it will be deleted also.
1927      We also allow combining in this case if all the insns are adjacent
1928      because that would leave the two CC0 insns adjacent as well.
1929      It would be more logical to test whether CC0 occurs inside I1 or I2,
1930      but that would be much slower, and this ought to be equivalent.  */
1931
1932   p = prev_nonnote_insn (insn);
1933   if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1934       && ! all_adjacent)
1935     return 0;
1936 #endif
1937
1938   /* If we get here, we have passed all the tests and the combination is
1939      to be allowed.  */
1940
1941   *pdest = dest;
1942   *psrc = src;
1943
1944   return 1;
1945 }
1946 \f
1947 /* LOC is the location within I3 that contains its pattern or the component
1948    of a PARALLEL of the pattern.  We validate that it is valid for combining.
1949
1950    One problem is if I3 modifies its output, as opposed to replacing it
1951    entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
1952    doing so would produce an insn that is not equivalent to the original insns.
1953
1954    Consider:
1955
1956          (set (reg:DI 101) (reg:DI 100))
1957          (set (subreg:SI (reg:DI 101) 0) <foo>)
1958
1959    This is NOT equivalent to:
1960
1961          (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1962                     (set (reg:DI 101) (reg:DI 100))])
1963
1964    Not only does this modify 100 (in which case it might still be valid
1965    if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1966
1967    We can also run into a problem if I2 sets a register that I1
1968    uses and I1 gets directly substituted into I3 (not via I2).  In that
1969    case, we would be getting the wrong value of I2DEST into I3, so we
1970    must reject the combination.  This case occurs when I2 and I1 both
1971    feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1972    If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1973    of a SET must prevent combination from occurring.  The same situation
1974    can occur for I0, in which case I0_NOT_IN_SRC is set.
1975
1976    Before doing the above check, we first try to expand a field assignment
1977    into a set of logical operations.
1978
1979    If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1980    we place a register that is both set and used within I3.  If more than one
1981    such register is detected, we fail.
1982
1983    Return 1 if the combination is valid, zero otherwise.  */
1984
1985 static int
1986 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
1987                   int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
1988 {
1989   rtx x = *loc;
1990
1991   if (GET_CODE (x) == SET)
1992     {
1993       rtx set = x ;
1994       rtx dest = SET_DEST (set);
1995       rtx src = SET_SRC (set);
1996       rtx inner_dest = dest;
1997       rtx subdest;
1998
1999       while (GET_CODE (inner_dest) == STRICT_LOW_PART
2000              || GET_CODE (inner_dest) == SUBREG
2001              || GET_CODE (inner_dest) == ZERO_EXTRACT)
2002         inner_dest = XEXP (inner_dest, 0);
2003
2004       /* Check for the case where I3 modifies its output, as discussed
2005          above.  We don't want to prevent pseudos from being combined
2006          into the address of a MEM, so only prevent the combination if
2007          i1 or i2 set the same MEM.  */
2008       if ((inner_dest != dest &&
2009            (!MEM_P (inner_dest)
2010             || rtx_equal_p (i2dest, inner_dest)
2011             || (i1dest && rtx_equal_p (i1dest, inner_dest))
2012             || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2013            && (reg_overlap_mentioned_p (i2dest, inner_dest)
2014                || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2015                || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2016
2017           /* This is the same test done in can_combine_p except we can't test
2018              all_adjacent; we don't have to, since this instruction will stay
2019              in place, thus we are not considering increasing the lifetime of
2020              INNER_DEST.
2021
2022              Also, if this insn sets a function argument, combining it with
2023              something that might need a spill could clobber a previous
2024              function argument; the all_adjacent test in can_combine_p also
2025              checks this; here, we do a more specific test for this case.  */
2026
2027           || (REG_P (inner_dest)
2028               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2029               && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2030                                         GET_MODE (inner_dest))))
2031           || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2032           || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2033         return 0;
2034
2035       /* If DEST is used in I3, it is being killed in this insn, so
2036          record that for later.  We have to consider paradoxical
2037          subregs here, since they kill the whole register, but we
2038          ignore partial subregs, STRICT_LOW_PART, etc.
2039          Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2040          STACK_POINTER_REGNUM, since these are always considered to be
2041          live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
2042       subdest = dest;
2043       if (GET_CODE (subdest) == SUBREG
2044           && (GET_MODE_SIZE (GET_MODE (subdest))
2045               >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
2046         subdest = SUBREG_REG (subdest);
2047       if (pi3dest_killed
2048           && REG_P (subdest)
2049           && reg_referenced_p (subdest, PATTERN (i3))
2050           && REGNO (subdest) != FRAME_POINTER_REGNUM
2051 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2052           && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
2053 #endif
2054 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2055           && (REGNO (subdest) != ARG_POINTER_REGNUM
2056               || ! fixed_regs [REGNO (subdest)])
2057 #endif
2058           && REGNO (subdest) != STACK_POINTER_REGNUM)
2059         {
2060           if (*pi3dest_killed)
2061             return 0;
2062
2063           *pi3dest_killed = subdest;
2064         }
2065     }
2066
2067   else if (GET_CODE (x) == PARALLEL)
2068     {
2069       int i;
2070
2071       for (i = 0; i < XVECLEN (x, 0); i++)
2072         if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2073                                 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2074           return 0;
2075     }
2076
2077   return 1;
2078 }
2079 \f
2080 /* Return 1 if X is an arithmetic expression that contains a multiplication
2081    and division.  We don't count multiplications by powers of two here.  */
2082
2083 static int
2084 contains_muldiv (rtx x)
2085 {
2086   switch (GET_CODE (x))
2087     {
2088     case MOD:  case DIV:  case UMOD:  case UDIV:
2089       return 1;
2090
2091     case MULT:
2092       return ! (CONST_INT_P (XEXP (x, 1))
2093                 && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
2094     default:
2095       if (BINARY_P (x))
2096         return contains_muldiv (XEXP (x, 0))
2097             || contains_muldiv (XEXP (x, 1));
2098
2099       if (UNARY_P (x))
2100         return contains_muldiv (XEXP (x, 0));
2101
2102       return 0;
2103     }
2104 }
2105 \f
2106 /* Determine whether INSN can be used in a combination.  Return nonzero if
2107    not.  This is used in try_combine to detect early some cases where we
2108    can't perform combinations.  */
2109
2110 static int
2111 cant_combine_insn_p (rtx insn)
2112 {
2113   rtx set;
2114   rtx src, dest;
2115
2116   /* If this isn't really an insn, we can't do anything.
2117      This can occur when flow deletes an insn that it has merged into an
2118      auto-increment address.  */
2119   if (! INSN_P (insn))
2120     return 1;
2121
2122   /* Never combine loads and stores involving hard regs that are likely
2123      to be spilled.  The register allocator can usually handle such
2124      reg-reg moves by tying.  If we allow the combiner to make
2125      substitutions of likely-spilled regs, reload might die.
2126      As an exception, we allow combinations involving fixed regs; these are
2127      not available to the register allocator so there's no risk involved.  */
2128
2129   set = single_set (insn);
2130   if (! set)
2131     return 0;
2132   src = SET_SRC (set);
2133   dest = SET_DEST (set);
2134   if (GET_CODE (src) == SUBREG)
2135     src = SUBREG_REG (src);
2136   if (GET_CODE (dest) == SUBREG)
2137     dest = SUBREG_REG (dest);
2138   if (REG_P (src) && REG_P (dest)
2139       && ((HARD_REGISTER_P (src)
2140            && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2141            && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2142           || (HARD_REGISTER_P (dest)
2143               && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2144               && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2145     return 1;
2146
2147   return 0;
2148 }
2149
2150 struct likely_spilled_retval_info
2151 {
2152   unsigned regno, nregs;
2153   unsigned mask;
2154 };
2155
2156 /* Called via note_stores by likely_spilled_retval_p.  Remove from info->mask
2157    hard registers that are known to be written to / clobbered in full.  */
2158 static void
2159 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2160 {
2161   struct likely_spilled_retval_info *const info =
2162     (struct likely_spilled_retval_info *) data;
2163   unsigned regno, nregs;
2164   unsigned new_mask;
2165
2166   if (!REG_P (XEXP (set, 0)))
2167     return;
2168   regno = REGNO (x);
2169   if (regno >= info->regno + info->nregs)
2170     return;
2171   nregs = hard_regno_nregs[regno][GET_MODE (x)];
2172   if (regno + nregs <= info->regno)
2173     return;
2174   new_mask = (2U << (nregs - 1)) - 1;
2175   if (regno < info->regno)
2176     new_mask >>= info->regno - regno;
2177   else
2178     new_mask <<= regno - info->regno;
2179   info->mask &= ~new_mask;
2180 }
2181
2182 /* Return nonzero iff part of the return value is live during INSN, and
2183    it is likely spilled.  This can happen when more than one insn is needed
2184    to copy the return value, e.g. when we consider to combine into the
2185    second copy insn for a complex value.  */
2186
2187 static int
2188 likely_spilled_retval_p (rtx insn)
2189 {
2190   rtx use = BB_END (this_basic_block);
2191   rtx reg, p;
2192   unsigned regno, nregs;
2193   /* We assume here that no machine mode needs more than
2194      32 hard registers when the value overlaps with a register
2195      for which TARGET_FUNCTION_VALUE_REGNO_P is true.  */
2196   unsigned mask;
2197   struct likely_spilled_retval_info info;
2198
2199   if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2200     return 0;
2201   reg = XEXP (PATTERN (use), 0);
2202   if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2203     return 0;
2204   regno = REGNO (reg);
2205   nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2206   if (nregs == 1)
2207     return 0;
2208   mask = (2U << (nregs - 1)) - 1;
2209
2210   /* Disregard parts of the return value that are set later.  */
2211   info.regno = regno;
2212   info.nregs = nregs;
2213   info.mask = mask;
2214   for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2215     if (INSN_P (p))
2216       note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2217   mask = info.mask;
2218
2219   /* Check if any of the (probably) live return value registers is
2220      likely spilled.  */
2221   nregs --;
2222   do
2223     {
2224       if ((mask & 1 << nregs)
2225           && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2226         return 1;
2227     } while (nregs--);
2228   return 0;
2229 }
2230
2231 /* Adjust INSN after we made a change to its destination.
2232
2233    Changing the destination can invalidate notes that say something about
2234    the results of the insn and a LOG_LINK pointing to the insn.  */
2235
2236 static void
2237 adjust_for_new_dest (rtx insn)
2238 {
2239   /* For notes, be conservative and simply remove them.  */
2240   remove_reg_equal_equiv_notes (insn);
2241
2242   /* The new insn will have a destination that was previously the destination
2243      of an insn just above it.  Call distribute_links to make a LOG_LINK from
2244      the next use of that destination.  */
2245   distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
2246
2247   df_insn_rescan (insn);
2248 }
2249
2250 /* Return TRUE if combine can reuse reg X in mode MODE.
2251    ADDED_SETS is nonzero if the original set is still required.  */
2252 static bool
2253 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
2254 {
2255   unsigned int regno;
2256
2257   if (!REG_P(x))
2258     return false;
2259
2260   regno = REGNO (x);
2261   /* Allow hard registers if the new mode is legal, and occupies no more
2262      registers than the old mode.  */
2263   if (regno < FIRST_PSEUDO_REGISTER)
2264     return (HARD_REGNO_MODE_OK (regno, mode)
2265             && (hard_regno_nregs[regno][GET_MODE (x)]
2266                 >= hard_regno_nregs[regno][mode]));
2267
2268   /* Or a pseudo that is only used once.  */
2269   return (REG_N_SETS (regno) == 1 && !added_sets
2270           && !REG_USERVAR_P (x));
2271 }
2272
2273
2274 /* Check whether X, the destination of a set, refers to part of
2275    the register specified by REG.  */
2276
2277 static bool
2278 reg_subword_p (rtx x, rtx reg)
2279 {
2280   /* Check that reg is an integer mode register.  */
2281   if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2282     return false;
2283
2284   if (GET_CODE (x) == STRICT_LOW_PART
2285       || GET_CODE (x) == ZERO_EXTRACT)
2286     x = XEXP (x, 0);
2287
2288   return GET_CODE (x) == SUBREG
2289          && SUBREG_REG (x) == reg
2290          && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2291 }
2292
2293 #ifdef AUTO_INC_DEC
2294 /* Replace auto-increment addressing modes with explicit operations to access
2295    the same addresses without modifying the corresponding registers.  */
2296
2297 static rtx
2298 cleanup_auto_inc_dec (rtx src, enum machine_mode mem_mode)
2299 {
2300   rtx x = src;
2301   const RTX_CODE code = GET_CODE (x);
2302   int i;
2303   const char *fmt;
2304
2305   switch (code)
2306     {
2307     case REG:
2308     case CONST_INT:
2309     case CONST_DOUBLE:
2310     case CONST_FIXED:
2311     case CONST_VECTOR:
2312     case SYMBOL_REF:
2313     case CODE_LABEL:
2314     case PC:
2315     case CC0:
2316     case SCRATCH:
2317       /* SCRATCH must be shared because they represent distinct values.  */
2318       return x;
2319     case CLOBBER:
2320       if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
2321         return x;
2322       break;
2323
2324     case CONST:
2325       if (shared_const_p (x))
2326         return x;
2327       break;
2328
2329     case MEM:
2330       mem_mode = GET_MODE (x);
2331       break;
2332
2333     case PRE_INC:
2334     case PRE_DEC:
2335       gcc_assert (mem_mode != VOIDmode && mem_mode != BLKmode);
2336       return gen_rtx_PLUS (GET_MODE (x),
2337                            cleanup_auto_inc_dec (XEXP (x, 0), mem_mode),
2338                            GEN_INT (code == PRE_INC
2339                                     ? GET_MODE_SIZE (mem_mode)
2340                                     : -GET_MODE_SIZE (mem_mode)));
2341
2342     case POST_INC:
2343     case POST_DEC:
2344     case PRE_MODIFY:
2345     case POST_MODIFY:
2346       return cleanup_auto_inc_dec (code == PRE_MODIFY
2347                                    ? XEXP (x, 1) : XEXP (x, 0),
2348                                    mem_mode);
2349
2350     default:
2351       break;
2352     }
2353
2354   /* Copy the various flags, fields, and other information.  We assume
2355      that all fields need copying, and then clear the fields that should
2356      not be copied.  That is the sensible default behavior, and forces
2357      us to explicitly document why we are *not* copying a flag.  */
2358   x = shallow_copy_rtx (x);
2359
2360   /* We do not copy the USED flag, which is used as a mark bit during
2361      walks over the RTL.  */
2362   RTX_FLAG (x, used) = 0;
2363
2364   /* We do not copy FRAME_RELATED for INSNs.  */
2365   if (INSN_P (x))
2366     RTX_FLAG (x, frame_related) = 0;
2367
2368   fmt = GET_RTX_FORMAT (code);
2369   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2370     if (fmt[i] == 'e')
2371       XEXP (x, i) = cleanup_auto_inc_dec (XEXP (x, i), mem_mode);
2372     else if (fmt[i] == 'E' || fmt[i] == 'V')
2373       {
2374         int j;
2375         XVEC (x, i) = rtvec_alloc (XVECLEN (x, i));
2376         for (j = 0; j < XVECLEN (x, i); j++)
2377           XVECEXP (x, i, j)
2378             = cleanup_auto_inc_dec (XVECEXP (src, i, j), mem_mode);
2379       }
2380
2381   return x;
2382 }
2383 #endif
2384
2385 /* Auxiliary data structure for propagate_for_debug_stmt.  */
2386
2387 struct rtx_subst_pair
2388 {
2389   rtx to;
2390   bool adjusted;
2391 };
2392
2393 /* DATA points to an rtx_subst_pair.  Return the value that should be
2394    substituted.  */
2395
2396 static rtx
2397 propagate_for_debug_subst (rtx from, const_rtx old_rtx, void *data)
2398 {
2399   struct rtx_subst_pair *pair = (struct rtx_subst_pair *)data;
2400
2401   if (!rtx_equal_p (from, old_rtx))
2402     return NULL_RTX;
2403   if (!pair->adjusted)
2404     {
2405       pair->adjusted = true;
2406 #ifdef AUTO_INC_DEC
2407       pair->to = cleanup_auto_inc_dec (pair->to, VOIDmode);
2408 #else
2409       pair->to = copy_rtx (pair->to);
2410 #endif
2411       pair->to = make_compound_operation (pair->to, SET);
2412       return pair->to;
2413     }
2414   return copy_rtx (pair->to);
2415 }
2416
2417 /* Replace all the occurrences of DEST with SRC in DEBUG_INSNs between INSN
2418    and LAST.  */
2419
2420 static void
2421 propagate_for_debug (rtx insn, rtx last, rtx dest, rtx src)
2422 {
2423   rtx next, loc;
2424
2425   struct rtx_subst_pair p;
2426   p.to = src;
2427   p.adjusted = false;
2428
2429   next = NEXT_INSN (insn);
2430   while (next != last)
2431     {
2432       insn = next;
2433       next = NEXT_INSN (insn);
2434       if (DEBUG_INSN_P (insn))
2435         {
2436           loc = simplify_replace_fn_rtx (INSN_VAR_LOCATION_LOC (insn),
2437                                          dest, propagate_for_debug_subst, &p);
2438           if (loc == INSN_VAR_LOCATION_LOC (insn))
2439             continue;
2440           INSN_VAR_LOCATION_LOC (insn) = loc;
2441           df_insn_rescan (insn);
2442         }
2443     }
2444 }
2445
2446 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2447    Note that the INSN should be deleted *after* removing dead edges, so
2448    that the kept edge is the fallthrough edge for a (set (pc) (pc))
2449    but not for a (set (pc) (label_ref FOO)).  */
2450
2451 static void
2452 update_cfg_for_uncondjump (rtx insn)
2453 {
2454   basic_block bb = BLOCK_FOR_INSN (insn);
2455   bool at_end = (BB_END (bb) == insn);
2456
2457   if (at_end)
2458     purge_dead_edges (bb);
2459
2460   delete_insn (insn);
2461   if (at_end && EDGE_COUNT (bb->succs) == 1)
2462     {
2463       rtx insn;
2464
2465       single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2466
2467       /* Remove barriers from the footer if there are any.  */
2468       for (insn = bb->il.rtl->footer; insn; insn = NEXT_INSN (insn))
2469         if (BARRIER_P (insn))
2470           {
2471             if (PREV_INSN (insn))
2472               NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2473             else
2474               bb->il.rtl->footer = NEXT_INSN (insn);
2475             if (NEXT_INSN (insn))
2476               PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2477           }
2478         else if (LABEL_P (insn))
2479           break;
2480     }
2481 }
2482
2483 /* Try to combine the insns I0, I1 and I2 into I3.
2484    Here I0, I1 and I2 appear earlier than I3.
2485    I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2486    I3.
2487
2488    If we are combining more than two insns and the resulting insn is not
2489    recognized, try splitting it into two insns.  If that happens, I2 and I3
2490    are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2491    Otherwise, I0, I1 and I2 are pseudo-deleted.
2492
2493    Return 0 if the combination does not work.  Then nothing is changed.
2494    If we did the combination, return the insn at which combine should
2495    resume scanning.
2496
2497    Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2498    new direct jump instruction.  */
2499
2500 static rtx
2501 try_combine (rtx i3, rtx i2, rtx i1, rtx i0, int *new_direct_jump_p)
2502 {
2503   /* New patterns for I3 and I2, respectively.  */
2504   rtx newpat, newi2pat = 0;
2505   rtvec newpat_vec_with_clobbers = 0;
2506   int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2507   /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2508      dead.  */
2509   int added_sets_0, added_sets_1, added_sets_2;
2510   /* Total number of SETs to put into I3.  */
2511   int total_sets;
2512   /* Nonzero if I2's or I1's body now appears in I3.  */
2513   int i2_is_used = 0, i1_is_used = 0;
2514   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
2515   int insn_code_number, i2_code_number = 0, other_code_number = 0;
2516   /* Contains I3 if the destination of I3 is used in its source, which means
2517      that the old life of I3 is being killed.  If that usage is placed into
2518      I2 and not in I3, a REG_DEAD note must be made.  */
2519   rtx i3dest_killed = 0;
2520   /* SET_DEST and SET_SRC of I2, I1 and I0.  */
2521   rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2522   /* Copy of SET_SRC of I1, if needed.  */
2523   rtx i1src_copy = 0;
2524   /* Set if I2DEST was reused as a scratch register.  */
2525   bool i2scratch = false;
2526   /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases.  */
2527   rtx i0pat = 0, i1pat = 0, i2pat = 0;
2528   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
2529   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2530   int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2531   int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2532   int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2533   /* Notes that must be added to REG_NOTES in I3 and I2.  */
2534   rtx new_i3_notes, new_i2_notes;
2535   /* Notes that we substituted I3 into I2 instead of the normal case.  */
2536   int i3_subst_into_i2 = 0;
2537   /* Notes that I1, I2 or I3 is a MULT operation.  */
2538   int have_mult = 0;
2539   int swap_i2i3 = 0;
2540   int changed_i3_dest = 0;
2541
2542   int maxreg;
2543   rtx temp;
2544   rtx link;
2545   rtx other_pat = 0;
2546   rtx new_other_notes;
2547   int i;
2548
2549   /* Only try four-insn combinations when there's high likelihood of
2550      success.  Look for simple insns, such as loads of constants or
2551      binary operations involving a constant.  */
2552   if (i0)
2553     {
2554       int i;
2555       int ngood = 0;
2556       int nshift = 0;
2557
2558       if (!flag_expensive_optimizations)
2559         return 0;
2560
2561       for (i = 0; i < 4; i++)
2562         {
2563           rtx insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2564           rtx set = single_set (insn);
2565           rtx src;
2566           if (!set)
2567             continue;
2568           src = SET_SRC (set);
2569           if (CONSTANT_P (src))
2570             {
2571               ngood += 2;
2572               break;
2573             }
2574           else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2575             ngood++;
2576           else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2577                    || GET_CODE (src) == LSHIFTRT)
2578             nshift++;
2579         }
2580       if (ngood < 2 && nshift < 2)
2581         return 0;
2582     }
2583
2584   /* Exit early if one of the insns involved can't be used for
2585      combinations.  */
2586   if (cant_combine_insn_p (i3)
2587       || cant_combine_insn_p (i2)
2588       || (i1 && cant_combine_insn_p (i1))
2589       || (i0 && cant_combine_insn_p (i0))
2590       || likely_spilled_retval_p (i3))
2591     return 0;
2592
2593   combine_attempts++;
2594   undobuf.other_insn = 0;
2595
2596   /* Reset the hard register usage information.  */
2597   CLEAR_HARD_REG_SET (newpat_used_regs);
2598
2599   if (dump_file && (dump_flags & TDF_DETAILS))
2600     {
2601       if (i0)
2602         fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2603                  INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2604       else if (i1)
2605         fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2606                  INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2607       else
2608         fprintf (dump_file, "\nTrying %d -> %d:\n",
2609                  INSN_UID (i2), INSN_UID (i3));
2610     }
2611
2612   /* If multiple insns feed into one of I2 or I3, they can be in any
2613      order.  To simplify the code below, reorder them in sequence.  */
2614   if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2615     temp = i2, i2 = i0, i0 = temp;
2616   if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2617     temp = i1, i1 = i0, i0 = temp;
2618   if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2619     temp = i1, i1 = i2, i2 = temp;
2620
2621   added_links_insn = 0;
2622
2623   /* First check for one important special case that the code below will
2624      not handle.  Namely, the case where I1 is zero, I2 is a PARALLEL
2625      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
2626      we may be able to replace that destination with the destination of I3.
2627      This occurs in the common code where we compute both a quotient and
2628      remainder into a structure, in which case we want to do the computation
2629      directly into the structure to avoid register-register copies.
2630
2631      Note that this case handles both multiple sets in I2 and also cases
2632      where I2 has a number of CLOBBERs inside the PARALLEL.
2633
2634      We make very conservative checks below and only try to handle the
2635      most common cases of this.  For example, we only handle the case
2636      where I2 and I3 are adjacent to avoid making difficult register
2637      usage tests.  */
2638
2639   if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2640       && REG_P (SET_SRC (PATTERN (i3)))
2641       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2642       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2643       && GET_CODE (PATTERN (i2)) == PARALLEL
2644       && ! side_effects_p (SET_DEST (PATTERN (i3)))
2645       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2646          below would need to check what is inside (and reg_overlap_mentioned_p
2647          doesn't support those codes anyway).  Don't allow those destinations;
2648          the resulting insn isn't likely to be recognized anyway.  */
2649       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2650       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2651       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2652                                     SET_DEST (PATTERN (i3)))
2653       && next_active_insn (i2) == i3)
2654     {
2655       rtx p2 = PATTERN (i2);
2656
2657       /* Make sure that the destination of I3,
2658          which we are going to substitute into one output of I2,
2659          is not used within another output of I2.  We must avoid making this:
2660          (parallel [(set (mem (reg 69)) ...)
2661                     (set (reg 69) ...)])
2662          which is not well-defined as to order of actions.
2663          (Besides, reload can't handle output reloads for this.)
2664
2665          The problem can also happen if the dest of I3 is a memory ref,
2666          if another dest in I2 is an indirect memory ref.  */
2667       for (i = 0; i < XVECLEN (p2, 0); i++)
2668         if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2669              || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2670             && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2671                                         SET_DEST (XVECEXP (p2, 0, i))))
2672           break;
2673
2674       if (i == XVECLEN (p2, 0))
2675         for (i = 0; i < XVECLEN (p2, 0); i++)
2676           if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2677               && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2678             {
2679               combine_merges++;
2680
2681               subst_insn = i3;
2682               subst_low_luid = DF_INSN_LUID (i2);
2683
2684               added_sets_2 = added_sets_1 = added_sets_0 = 0;
2685               i2src = SET_SRC (XVECEXP (p2, 0, i));
2686               i2dest = SET_DEST (XVECEXP (p2, 0, i));
2687               i2dest_killed = dead_or_set_p (i2, i2dest);
2688
2689               /* Replace the dest in I2 with our dest and make the resulting
2690                  insn the new pattern for I3.  Then skip to where we validate
2691                  the pattern.  Everything was set up above.  */
2692               SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2693               newpat = p2;
2694               i3_subst_into_i2 = 1;
2695               goto validate_replacement;
2696             }
2697     }
2698
2699   /* If I2 is setting a pseudo to a constant and I3 is setting some
2700      sub-part of it to another constant, merge them by making a new
2701      constant.  */
2702   if (i1 == 0
2703       && (temp = single_set (i2)) != 0
2704       && (CONST_INT_P (SET_SRC (temp))
2705           || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
2706       && GET_CODE (PATTERN (i3)) == SET
2707       && (CONST_INT_P (SET_SRC (PATTERN (i3)))
2708           || GET_CODE (SET_SRC (PATTERN (i3))) == CONST_DOUBLE)
2709       && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp)))
2710     {
2711       rtx dest = SET_DEST (PATTERN (i3));
2712       int offset = -1;
2713       int width = 0;
2714
2715       if (GET_CODE (dest) == ZERO_EXTRACT)
2716         {
2717           if (CONST_INT_P (XEXP (dest, 1))
2718               && CONST_INT_P (XEXP (dest, 2)))
2719             {
2720               width = INTVAL (XEXP (dest, 1));
2721               offset = INTVAL (XEXP (dest, 2));
2722               dest = XEXP (dest, 0);
2723               if (BITS_BIG_ENDIAN)
2724                 offset = GET_MODE_BITSIZE (GET_MODE (dest)) - width - offset;
2725             }
2726         }
2727       else
2728         {
2729           if (GET_CODE (dest) == STRICT_LOW_PART)
2730             dest = XEXP (dest, 0);
2731           width = GET_MODE_BITSIZE (GET_MODE (dest));
2732           offset = 0;
2733         }
2734
2735       if (offset >= 0)
2736         {
2737           /* If this is the low part, we're done.  */
2738           if (subreg_lowpart_p (dest))
2739             ;
2740           /* Handle the case where inner is twice the size of outer.  */
2741           else if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2742                    == 2 * GET_MODE_BITSIZE (GET_MODE (dest)))
2743             offset += GET_MODE_BITSIZE (GET_MODE (dest));
2744           /* Otherwise give up for now.  */
2745           else
2746             offset = -1;
2747         }
2748
2749       if (offset >= 0
2750           && (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2751               <= HOST_BITS_PER_DOUBLE_INT))
2752         {
2753           double_int m, o, i;
2754           rtx inner = SET_SRC (PATTERN (i3));
2755           rtx outer = SET_SRC (temp);
2756
2757           o = rtx_to_double_int (outer);
2758           i = rtx_to_double_int (inner);
2759
2760           m = double_int_mask (width);
2761           i = double_int_and (i, m);
2762           m = double_int_lshift (m, offset, HOST_BITS_PER_DOUBLE_INT, false);
2763           i = double_int_lshift (i, offset, HOST_BITS_PER_DOUBLE_INT, false);
2764           o = double_int_ior (double_int_and_not (o, m), i);
2765
2766           combine_merges++;
2767           subst_insn = i3;
2768           subst_low_luid = DF_INSN_LUID (i2);
2769           added_sets_2 = added_sets_1 = added_sets_0 = 0;
2770           i2dest = SET_DEST (temp);
2771           i2dest_killed = dead_or_set_p (i2, i2dest);
2772
2773           /* Replace the source in I2 with the new constant and make the
2774              resulting insn the new pattern for I3.  Then skip to where we
2775              validate the pattern.  Everything was set up above.  */
2776           SUBST (SET_SRC (temp),
2777                  immed_double_int_const (o, GET_MODE (SET_DEST (temp))));
2778
2779           newpat = PATTERN (i2);
2780
2781           /* The dest of I3 has been replaced with the dest of I2.  */
2782           changed_i3_dest = 1;
2783           goto validate_replacement;
2784         }
2785     }
2786
2787 #ifndef HAVE_cc0
2788   /* If we have no I1 and I2 looks like:
2789         (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2790                    (set Y OP)])
2791      make up a dummy I1 that is
2792         (set Y OP)
2793      and change I2 to be
2794         (set (reg:CC X) (compare:CC Y (const_int 0)))
2795
2796      (We can ignore any trailing CLOBBERs.)
2797
2798      This undoes a previous combination and allows us to match a branch-and-
2799      decrement insn.  */
2800
2801   if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2802       && XVECLEN (PATTERN (i2), 0) >= 2
2803       && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2804       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2805           == MODE_CC)
2806       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2807       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2808       && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2809       && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2810       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2811                       SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2812     {
2813       for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2814         if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2815           break;
2816
2817       if (i == 1)
2818         {
2819           /* We make I1 with the same INSN_UID as I2.  This gives it
2820              the same DF_INSN_LUID for value tracking.  Our fake I1 will
2821              never appear in the insn stream so giving it the same INSN_UID
2822              as I2 will not cause a problem.  */
2823
2824           i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
2825                              BLOCK_FOR_INSN (i2), XVECEXP (PATTERN (i2), 0, 1),
2826                              INSN_LOCATOR (i2), -1, NULL_RTX);
2827
2828           SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2829           SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2830                  SET_DEST (PATTERN (i1)));
2831         }
2832     }
2833 #endif
2834
2835   /* Verify that I2 and I1 are valid for combining.  */
2836   if (! can_combine_p (i2, i3, i0, i1, NULL_RTX, NULL_RTX, &i2dest, &i2src)
2837       || (i1 && ! can_combine_p (i1, i3, i0, NULL_RTX, i2, NULL_RTX,
2838                                  &i1dest, &i1src))
2839       || (i0 && ! can_combine_p (i0, i3, NULL_RTX, NULL_RTX, i1, i2,
2840                                  &i0dest, &i0src)))
2841     {
2842       undo_all ();
2843       return 0;
2844     }
2845
2846   /* Record whether I2DEST is used in I2SRC and similarly for the other
2847      cases.  Knowing this will help in register status updating below.  */
2848   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2849   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2850   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2851   i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2852   i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2853   i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2854   i2dest_killed = dead_or_set_p (i2, i2dest);
2855   i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2856   i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
2857
2858   /* For the earlier insns, determine which of the subsequent ones they
2859      feed.  */
2860   i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
2861   i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
2862   i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
2863                           : (!reg_overlap_mentioned_p (i1dest, i0dest)
2864                              && reg_overlap_mentioned_p (i0dest, i2src))));
2865
2866   /* Ensure that I3's pattern can be the destination of combines.  */
2867   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
2868                           i1 && i2dest_in_i1src && !i1_feeds_i2_n,
2869                           i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
2870                                  || (i1dest_in_i0src && !i0_feeds_i1_n)),
2871                           &i3dest_killed))
2872     {
2873       undo_all ();
2874       return 0;
2875     }
2876
2877   /* See if any of the insns is a MULT operation.  Unless one is, we will
2878      reject a combination that is, since it must be slower.  Be conservative
2879      here.  */
2880   if (GET_CODE (i2src) == MULT
2881       || (i1 != 0 && GET_CODE (i1src) == MULT)
2882       || (i0 != 0 && GET_CODE (i0src) == MULT)
2883       || (GET_CODE (PATTERN (i3)) == SET
2884           && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2885     have_mult = 1;
2886
2887   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2888      We used to do this EXCEPT in one case: I3 has a post-inc in an
2889      output operand.  However, that exception can give rise to insns like
2890         mov r3,(r3)+
2891      which is a famous insn on the PDP-11 where the value of r3 used as the
2892      source was model-dependent.  Avoid this sort of thing.  */
2893
2894 #if 0
2895   if (!(GET_CODE (PATTERN (i3)) == SET
2896         && REG_P (SET_SRC (PATTERN (i3)))
2897         && MEM_P (SET_DEST (PATTERN (i3)))
2898         && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2899             || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2900     /* It's not the exception.  */
2901 #endif
2902 #ifdef AUTO_INC_DEC
2903     for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2904       if (REG_NOTE_KIND (link) == REG_INC
2905           && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2906               || (i1 != 0
2907                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2908         {
2909           undo_all ();
2910           return 0;
2911         }
2912 #endif
2913
2914   /* See if the SETs in I1 or I2 need to be kept around in the merged
2915      instruction: whenever the value set there is still needed past I3.
2916      For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
2917
2918      For the SET in I1, we have two cases:  If I1 and I2 independently
2919      feed into I3, the set in I1 needs to be kept around if I1DEST dies
2920      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
2921      in I1 needs to be kept around unless I1DEST dies or is set in either
2922      I2 or I3.  The same consideration applies to I0.  */
2923
2924   added_sets_2 = !dead_or_set_p (i3, i2dest);
2925
2926   if (i1)
2927     added_sets_1 = !(dead_or_set_p (i3, i1dest)
2928                      || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
2929   else
2930     added_sets_1 = 0;
2931
2932   if (i0)
2933     added_sets_0 =  !(dead_or_set_p (i3, i0dest)
2934                       || (i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
2935                       || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)));
2936   else
2937     added_sets_0 = 0;
2938
2939   /* We are about to copy insns for the case where they need to be kept
2940      around.  Check that they can be copied in the merged instruction.  */
2941
2942   if (targetm.cannot_copy_insn_p
2943       && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
2944           || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
2945           || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
2946     {
2947       undo_all ();
2948       return 0;
2949     }
2950
2951   /* If the set in I2 needs to be kept around, we must make a copy of
2952      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2953      PATTERN (I2), we are only substituting for the original I1DEST, not into
2954      an already-substituted copy.  This also prevents making self-referential
2955      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2956      I2DEST.  */
2957
2958   if (added_sets_2)
2959     {
2960       if (GET_CODE (PATTERN (i2)) == PARALLEL)
2961         i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
2962       else
2963         i2pat = copy_rtx (PATTERN (i2));
2964     }
2965
2966   if (added_sets_1)
2967     {
2968       if (GET_CODE (PATTERN (i1)) == PARALLEL)
2969         i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
2970       else
2971         i1pat = copy_rtx (PATTERN (i1));
2972     }
2973
2974   if (added_sets_0)
2975     {
2976       if (GET_CODE (PATTERN (i0)) == PARALLEL)
2977         i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
2978       else
2979         i0pat = copy_rtx (PATTERN (i0));
2980     }
2981
2982   combine_merges++;
2983
2984   /* Substitute in the latest insn for the regs set by the earlier ones.  */
2985
2986   maxreg = max_reg_num ();
2987
2988   subst_insn = i3;
2989
2990 #ifndef HAVE_cc0
2991   /* Many machines that don't use CC0 have insns that can both perform an
2992      arithmetic operation and set the condition code.  These operations will
2993      be represented as a PARALLEL with the first element of the vector
2994      being a COMPARE of an arithmetic operation with the constant zero.
2995      The second element of the vector will set some pseudo to the result
2996      of the same arithmetic operation.  If we simplify the COMPARE, we won't
2997      match such a pattern and so will generate an extra insn.   Here we test
2998      for this case, where both the comparison and the operation result are
2999      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3000      I2SRC.  Later we will make the PARALLEL that contains I2.  */
3001
3002   if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3003       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3004       && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
3005       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3006     {
3007 #ifdef SELECT_CC_MODE
3008       rtx *cc_use;
3009       enum machine_mode compare_mode;
3010 #endif
3011
3012       newpat = PATTERN (i3);
3013       SUBST (XEXP (SET_SRC (newpat), 0), i2src);
3014
3015       i2_is_used = 1;
3016
3017 #ifdef SELECT_CC_MODE
3018       /* See if a COMPARE with the operand we substituted in should be done
3019          with the mode that is currently being used.  If not, do the same
3020          processing we do in `subst' for a SET; namely, if the destination
3021          is used only once, try to replace it with a register of the proper
3022          mode and also replace the COMPARE.  */
3023       if (undobuf.other_insn == 0
3024           && (cc_use = find_single_use (SET_DEST (newpat), i3,
3025                                         &undobuf.other_insn))
3026           && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
3027                                               i2src, const0_rtx))
3028               != GET_MODE (SET_DEST (newpat))))
3029         {
3030           if (can_change_dest_mode (SET_DEST (newpat), added_sets_2,
3031                                     compare_mode))
3032             {
3033               unsigned int regno = REGNO (SET_DEST (newpat));
3034               rtx new_dest;
3035
3036               if (regno < FIRST_PSEUDO_REGISTER)
3037                 new_dest = gen_rtx_REG (compare_mode, regno);
3038               else
3039                 {
3040                   SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3041                   new_dest = regno_reg_rtx[regno];
3042                 }
3043
3044               SUBST (SET_DEST (newpat), new_dest);
3045               SUBST (XEXP (*cc_use, 0), new_dest);
3046               SUBST (SET_SRC (newpat),
3047                      gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
3048             }
3049           else
3050             undobuf.other_insn = 0;
3051         }
3052 #endif
3053     }
3054   else
3055 #endif
3056     {
3057       /* It is possible that the source of I2 or I1 may be performing
3058          an unneeded operation, such as a ZERO_EXTEND of something
3059          that is known to have the high part zero.  Handle that case
3060          by letting subst look at the innermost one of them.
3061
3062          Another way to do this would be to have a function that tries
3063          to simplify a single insn instead of merging two or more
3064          insns.  We don't do this because of the potential of infinite
3065          loops and because of the potential extra memory required.
3066          However, doing it the way we are is a bit of a kludge and
3067          doesn't catch all cases.
3068
3069          But only do this if -fexpensive-optimizations since it slows
3070          things down and doesn't usually win.
3071
3072          This is not done in the COMPARE case above because the
3073          unmodified I2PAT is used in the PARALLEL and so a pattern
3074          with a modified I2SRC would not match.  */
3075
3076       if (flag_expensive_optimizations)
3077         {
3078           /* Pass pc_rtx so no substitutions are done, just
3079              simplifications.  */
3080           if (i1)
3081             {
3082               subst_low_luid = DF_INSN_LUID (i1);
3083               i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
3084             }
3085           else
3086             {
3087               subst_low_luid = DF_INSN_LUID (i2);
3088               i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
3089             }
3090         }
3091
3092       n_occurrences = 0;                /* `subst' counts here */
3093       subst_low_luid = DF_INSN_LUID (i2);
3094
3095       /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3096          copy of I2SRC each time we substitute it, in order to avoid creating
3097          self-referential RTL when we will be substituting I1SRC for I1DEST
3098          later.  Likewise if I0 feeds into I2, either directly or indirectly
3099          through I1, and I0DEST is in I0SRC.  */
3100       newpat = subst (PATTERN (i3), i2dest, i2src, 0,
3101                       (i1_feeds_i2_n && i1dest_in_i1src)
3102                       || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3103                           && i0dest_in_i0src));
3104       substed_i2 = 1;
3105
3106       /* Record whether I2's body now appears within I3's body.  */
3107       i2_is_used = n_occurrences;
3108     }
3109
3110   /* If we already got a failure, don't try to do more.  Otherwise, try to
3111      substitute I1 if we have it.  */
3112
3113   if (i1 && GET_CODE (newpat) != CLOBBER)
3114     {
3115       /* Check that an autoincrement side-effect on I1 has not been lost.
3116          This happens if I1DEST is mentioned in I2 and dies there, and
3117          has disappeared from the new pattern.  */
3118       if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3119            && i1_feeds_i2_n
3120            && dead_or_set_p (i2, i1dest)
3121            && !reg_overlap_mentioned_p (i1dest, newpat))
3122            /* Before we can do this substitution, we must redo the test done
3123               above (see detailed comments there) that ensures I1DEST isn't
3124               mentioned in any SETs in NEWPAT that are field assignments.  */
3125           || !combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX, NULL_RTX,
3126                                 0, 0, 0))
3127         {
3128           undo_all ();
3129           return 0;
3130         }
3131
3132       n_occurrences = 0;
3133       subst_low_luid = DF_INSN_LUID (i1);
3134
3135       /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3136          copy of I1SRC each time we substitute it, in order to avoid creating
3137          self-referential RTL when we will be substituting I0SRC for I0DEST
3138          later.  */
3139       newpat = subst (newpat, i1dest, i1src, 0,
3140                       i0_feeds_i1_n && i0dest_in_i0src);
3141       substed_i1 = 1;
3142
3143       /* Record whether I1's body now appears within I3's body.  */
3144       i1_is_used = n_occurrences;
3145     }
3146
3147   /* Likewise for I0 if we have it.  */
3148
3149   if (i0 && GET_CODE (newpat) != CLOBBER)
3150     {
3151       if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3152            && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3153                || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3154            && !reg_overlap_mentioned_p (i0dest, newpat))
3155           || !combinable_i3pat (NULL_RTX, &newpat, i0dest, NULL_RTX, NULL_RTX,
3156                                 0, 0, 0))
3157         {
3158           undo_all ();
3159           return 0;
3160         }
3161
3162       /* If the following substitution will modify I1SRC, make a copy of it
3163          for the case where it is substituted for I1DEST in I2PAT later.  */
3164       if (i0_feeds_i1_n && added_sets_2 && i1_feeds_i2_n)
3165         i1src_copy = copy_rtx (i1src);
3166
3167       n_occurrences = 0;
3168       subst_low_luid = DF_INSN_LUID (i0);
3169       newpat = subst (newpat, i0dest, i0src, 0, 0);
3170       substed_i0 = 1;
3171     }
3172
3173   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
3174      to count all the ways that I2SRC and I1SRC can be used.  */
3175   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3176        && i2_is_used + added_sets_2 > 1)
3177       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3178           && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3179               > 1))
3180       || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3181           && (n_occurrences + added_sets_0
3182               + (added_sets_1 && i0_feeds_i1_n)
3183               + (added_sets_2 && i0_feeds_i2_n)
3184               > 1))
3185       /* Fail if we tried to make a new register.  */
3186       || max_reg_num () != maxreg
3187       /* Fail if we couldn't do something and have a CLOBBER.  */
3188       || GET_CODE (newpat) == CLOBBER
3189       /* Fail if this new pattern is a MULT and we didn't have one before
3190          at the outer level.  */
3191       || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3192           && ! have_mult))
3193     {
3194       undo_all ();
3195       return 0;
3196     }
3197
3198   /* If the actions of the earlier insns must be kept
3199      in addition to substituting them into the latest one,
3200      we must make a new PARALLEL for the latest insn
3201      to hold additional the SETs.  */
3202
3203   if (added_sets_0 || added_sets_1 || added_sets_2)
3204     {
3205       int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3206       combine_extras++;
3207
3208       if (GET_CODE (newpat) == PARALLEL)
3209         {
3210           rtvec old = XVEC (newpat, 0);
3211           total_sets = XVECLEN (newpat, 0) + extra_sets;
3212           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3213           memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3214                   sizeof (old->elem[0]) * old->num_elem);
3215         }
3216       else
3217         {
3218           rtx old = newpat;
3219           total_sets = 1 + extra_sets;
3220           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3221           XVECEXP (newpat, 0, 0) = old;
3222         }
3223
3224       if (added_sets_0)
3225         XVECEXP (newpat, 0, --total_sets) = i0pat;
3226
3227       if (added_sets_1)
3228         {
3229           rtx t = i1pat;
3230           if (i0_feeds_i1_n)
3231             t = subst (t, i0dest, i0src, 0, 0);
3232
3233           XVECEXP (newpat, 0, --total_sets) = t;
3234         }
3235       if (added_sets_2)
3236         {
3237           rtx t = i2pat;
3238           if (i1_feeds_i2_n)
3239             t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0,
3240                        i0_feeds_i1_n && i0dest_in_i0src);
3241           if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3242             t = subst (t, i0dest, i0src, 0, 0);
3243
3244           XVECEXP (newpat, 0, --total_sets) = t;
3245         }
3246     }
3247
3248  validate_replacement:
3249
3250   /* Note which hard regs this insn has as inputs.  */
3251   mark_used_regs_combine (newpat);
3252
3253   /* If recog_for_combine fails, it strips existing clobbers.  If we'll
3254      consider splitting this pattern, we might need these clobbers.  */
3255   if (i1 && GET_CODE (newpat) == PARALLEL
3256       && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3257     {
3258       int len = XVECLEN (newpat, 0);
3259
3260       newpat_vec_with_clobbers = rtvec_alloc (len);
3261       for (i = 0; i < len; i++)
3262         RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3263     }
3264
3265   /* Is the result of combination a valid instruction?  */
3266   insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3267
3268   /* If the result isn't valid, see if it is a PARALLEL of two SETs where
3269      the second SET's destination is a register that is unused and isn't
3270      marked as an instruction that might trap in an EH region.  In that case,
3271      we just need the first SET.   This can occur when simplifying a divmod
3272      insn.  We *must* test for this case here because the code below that
3273      splits two independent SETs doesn't handle this case correctly when it
3274      updates the register status.
3275
3276      It's pointless doing this if we originally had two sets, one from
3277      i3, and one from i2.  Combining then splitting the parallel results
3278      in the original i2 again plus an invalid insn (which we delete).
3279      The net effect is only to move instructions around, which makes
3280      debug info less accurate.
3281
3282      Also check the case where the first SET's destination is unused.
3283      That would not cause incorrect code, but does cause an unneeded
3284      insn to remain.  */
3285
3286   if (insn_code_number < 0
3287       && !(added_sets_2 && i1 == 0)
3288       && GET_CODE (newpat) == PARALLEL
3289       && XVECLEN (newpat, 0) == 2
3290       && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3291       && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3292       && asm_noperands (newpat) < 0)
3293     {
3294       rtx set0 = XVECEXP (newpat, 0, 0);
3295       rtx set1 = XVECEXP (newpat, 0, 1);
3296
3297       if (((REG_P (SET_DEST (set1))
3298             && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3299            || (GET_CODE (SET_DEST (set1)) == SUBREG
3300                && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3301           && insn_nothrow_p (i3)
3302           && !side_effects_p (SET_SRC (set1)))
3303         {
3304           newpat = set0;
3305           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3306         }
3307
3308       else if (((REG_P (SET_DEST (set0))
3309                  && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3310                 || (GET_CODE (SET_DEST (set0)) == SUBREG
3311                     && find_reg_note (i3, REG_UNUSED,
3312                                       SUBREG_REG (SET_DEST (set0)))))
3313                && insn_nothrow_p (i3)
3314                && !side_effects_p (SET_SRC (set0)))
3315         {
3316           newpat = set1;
3317           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3318
3319           if (insn_code_number >= 0)
3320             changed_i3_dest = 1;
3321         }
3322     }
3323
3324   /* If we were combining three insns and the result is a simple SET
3325      with no ASM_OPERANDS that wasn't recognized, try to split it into two
3326      insns.  There are two ways to do this.  It can be split using a
3327      machine-specific method (like when you have an addition of a large
3328      constant) or by combine in the function find_split_point.  */
3329
3330   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3331       && asm_noperands (newpat) < 0)
3332     {
3333       rtx parallel, m_split, *split;
3334
3335       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
3336          use I2DEST as a scratch register will help.  In the latter case,
3337          convert I2DEST to the mode of the source of NEWPAT if we can.  */
3338
3339       m_split = combine_split_insns (newpat, i3);
3340
3341       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3342          inputs of NEWPAT.  */
3343
3344       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3345          possible to try that as a scratch reg.  This would require adding
3346          more code to make it work though.  */
3347
3348       if (m_split == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3349         {
3350           enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3351
3352           /* First try to split using the original register as a
3353              scratch register.  */
3354           parallel = gen_rtx_PARALLEL (VOIDmode,
3355                                        gen_rtvec (2, newpat,
3356                                                   gen_rtx_CLOBBER (VOIDmode,
3357                                                                    i2dest)));
3358           m_split = combine_split_insns (parallel, i3);
3359
3360           /* If that didn't work, try changing the mode of I2DEST if
3361              we can.  */
3362           if (m_split == 0
3363               && new_mode != GET_MODE (i2dest)
3364               && new_mode != VOIDmode
3365               && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3366             {
3367               enum machine_mode old_mode = GET_MODE (i2dest);
3368               rtx ni2dest;
3369
3370               if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3371                 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3372               else
3373                 {
3374                   SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3375                   ni2dest = regno_reg_rtx[REGNO (i2dest)];
3376                 }
3377
3378               parallel = (gen_rtx_PARALLEL
3379                           (VOIDmode,
3380                            gen_rtvec (2, newpat,
3381                                       gen_rtx_CLOBBER (VOIDmode,
3382                                                        ni2dest))));
3383               m_split = combine_split_insns (parallel, i3);
3384
3385               if (m_split == 0
3386                   && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3387                 {
3388                   struct undo *buf;
3389
3390                   adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3391                   buf = undobuf.undos;
3392                   undobuf.undos = buf->next;
3393                   buf->next = undobuf.frees;
3394                   undobuf.frees = buf;
3395                 }
3396             }
3397
3398           i2scratch = m_split != 0;
3399         }
3400
3401       /* If recog_for_combine has discarded clobbers, try to use them
3402          again for the split.  */
3403       if (m_split == 0 && newpat_vec_with_clobbers)
3404         {
3405           parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3406           m_split = combine_split_insns (parallel, i3);
3407         }
3408
3409       if (m_split && NEXT_INSN (m_split) == NULL_RTX)
3410         {
3411           m_split = PATTERN (m_split);
3412           insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
3413           if (insn_code_number >= 0)
3414             newpat = m_split;
3415         }
3416       else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
3417                && (next_real_insn (i2) == i3
3418                    || ! use_crosses_set_p (PATTERN (m_split), DF_INSN_LUID (i2))))
3419         {
3420           rtx i2set, i3set;
3421           rtx newi3pat = PATTERN (NEXT_INSN (m_split));
3422           newi2pat = PATTERN (m_split);
3423
3424           i3set = single_set (NEXT_INSN (m_split));
3425           i2set = single_set (m_split);
3426
3427           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3428
3429           /* If I2 or I3 has multiple SETs, we won't know how to track
3430              register status, so don't use these insns.  If I2's destination
3431              is used between I2 and I3, we also can't use these insns.  */
3432
3433           if (i2_code_number >= 0 && i2set && i3set
3434               && (next_real_insn (i2) == i3
3435                   || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3436             insn_code_number = recog_for_combine (&newi3pat, i3,
3437                                                   &new_i3_notes);
3438           if (insn_code_number >= 0)
3439             newpat = newi3pat;
3440
3441           /* It is possible that both insns now set the destination of I3.
3442              If so, we must show an extra use of it.  */
3443
3444           if (insn_code_number >= 0)
3445             {
3446               rtx new_i3_dest = SET_DEST (i3set);
3447               rtx new_i2_dest = SET_DEST (i2set);
3448
3449               while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3450                      || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3451                      || GET_CODE (new_i3_dest) == SUBREG)
3452                 new_i3_dest = XEXP (new_i3_dest, 0);
3453
3454               while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3455                      || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3456                      || GET_CODE (new_i2_dest) == SUBREG)
3457                 new_i2_dest = XEXP (new_i2_dest, 0);
3458
3459               if (REG_P (new_i3_dest)
3460                   && REG_P (new_i2_dest)
3461                   && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3462                 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3463             }
3464         }
3465
3466       /* If we can split it and use I2DEST, go ahead and see if that
3467          helps things be recognized.  Verify that none of the registers
3468          are set between I2 and I3.  */
3469       if (insn_code_number < 0
3470           && (split = find_split_point (&newpat, i3, false)) != 0
3471 #ifdef HAVE_cc0
3472           && REG_P (i2dest)
3473 #endif
3474           /* We need I2DEST in the proper mode.  If it is a hard register
3475              or the only use of a pseudo, we can change its mode.
3476              Make sure we don't change a hard register to have a mode that
3477              isn't valid for it, or change the number of registers.  */
3478           && (GET_MODE (*split) == GET_MODE (i2dest)
3479               || GET_MODE (*split) == VOIDmode
3480               || can_change_dest_mode (i2dest, added_sets_2,
3481                                        GET_MODE (*split)))
3482           && (next_real_insn (i2) == i3
3483               || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3484           /* We can't overwrite I2DEST if its value is still used by
3485              NEWPAT.  */
3486           && ! reg_referenced_p (i2dest, newpat))
3487         {
3488           rtx newdest = i2dest;
3489           enum rtx_code split_code = GET_CODE (*split);
3490           enum machine_mode split_mode = GET_MODE (*split);
3491           bool subst_done = false;
3492           newi2pat = NULL_RTX;
3493
3494           i2scratch = true;
3495
3496           /* *SPLIT may be part of I2SRC, so make sure we have the
3497              original expression around for later debug processing.
3498              We should not need I2SRC any more in other cases.  */
3499           if (MAY_HAVE_DEBUG_INSNS)
3500             i2src = copy_rtx (i2src);
3501           else
3502             i2src = NULL;
3503
3504           /* Get NEWDEST as a register in the proper mode.  We have already
3505              validated that we can do this.  */
3506           if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3507             {
3508               if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3509                 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3510               else
3511                 {
3512                   SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3513                   newdest = regno_reg_rtx[REGNO (i2dest)];
3514                 }
3515             }
3516
3517           /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3518              an ASHIFT.  This can occur if it was inside a PLUS and hence
3519              appeared to be a memory address.  This is a kludge.  */
3520           if (split_code == MULT
3521               && CONST_INT_P (XEXP (*split, 1))
3522               && INTVAL (XEXP (*split, 1)) > 0
3523               && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3524             {
3525               SUBST (*split, gen_rtx_ASHIFT (split_mode,
3526                                              XEXP (*split, 0), GEN_INT (i)));
3527               /* Update split_code because we may not have a multiply
3528                  anymore.  */
3529               split_code = GET_CODE (*split);
3530             }
3531
3532 #ifdef INSN_SCHEDULING
3533           /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3534              be written as a ZERO_EXTEND.  */
3535           if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3536             {
3537 #ifdef LOAD_EXTEND_OP
3538               /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3539                  what it really is.  */
3540               if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3541                   == SIGN_EXTEND)
3542                 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3543                                                     SUBREG_REG (*split)));
3544               else
3545 #endif
3546                 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3547                                                     SUBREG_REG (*split)));
3548             }
3549 #endif
3550
3551           /* Attempt to split binary operators using arithmetic identities.  */
3552           if (BINARY_P (SET_SRC (newpat))
3553               && split_mode == GET_MODE (SET_SRC (newpat))
3554               && ! side_effects_p (SET_SRC (newpat)))
3555             {
3556               rtx setsrc = SET_SRC (newpat);
3557               enum machine_mode mode = GET_MODE (setsrc);
3558               enum rtx_code code = GET_CODE (setsrc);
3559               rtx src_op0 = XEXP (setsrc, 0);
3560               rtx src_op1 = XEXP (setsrc, 1);
3561
3562               /* Split "X = Y op Y" as "Z = Y; X = Z op Z".  */
3563               if (rtx_equal_p (src_op0, src_op1))
3564                 {
3565                   newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3566                   SUBST (XEXP (setsrc, 0), newdest);
3567                   SUBST (XEXP (setsrc, 1), newdest);
3568                   subst_done = true;
3569                 }
3570               /* Split "((P op Q) op R) op S" where op is PLUS or MULT.  */
3571               else if ((code == PLUS || code == MULT)
3572                        && GET_CODE (src_op0) == code
3573                        && GET_CODE (XEXP (src_op0, 0)) == code
3574                        && (INTEGRAL_MODE_P (mode)
3575                            || (FLOAT_MODE_P (mode)
3576                                && flag_unsafe_math_optimizations)))
3577                 {
3578                   rtx p = XEXP (XEXP (src_op0, 0), 0);
3579                   rtx q = XEXP (XEXP (src_op0, 0), 1);
3580                   rtx r = XEXP (src_op0, 1);
3581                   rtx s = src_op1;
3582
3583                   /* Split both "((X op Y) op X) op Y" and
3584                      "((X op Y) op Y) op X" as "T op T" where T is
3585                      "X op Y".  */
3586                   if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3587                        || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3588                     {
3589                       newi2pat = gen_rtx_SET (VOIDmode, newdest,
3590                                               XEXP (src_op0, 0));
3591                       SUBST (XEXP (setsrc, 0), newdest);
3592                       SUBST (XEXP (setsrc, 1), newdest);
3593                       subst_done = true;
3594                     }
3595                   /* Split "((X op X) op Y) op Y)" as "T op T" where
3596                      T is "X op Y".  */
3597                   else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3598                     {
3599                       rtx tmp = simplify_gen_binary (code, mode, p, r);
3600                       newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3601                       SUBST (XEXP (setsrc, 0), newdest);
3602                       SUBST (XEXP (setsrc, 1), newdest);
3603                       subst_done = true;
3604                     }
3605                 }
3606             }
3607
3608           if (!subst_done)
3609             {
3610               newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3611               SUBST (*split, newdest);
3612             }
3613
3614           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3615
3616           /* recog_for_combine might have added CLOBBERs to newi2pat.
3617              Make sure NEWPAT does not depend on the clobbered regs.  */
3618           if (GET_CODE (newi2pat) == PARALLEL)
3619             for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3620               if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3621                 {
3622                   rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3623                   if (reg_overlap_mentioned_p (reg, newpat))
3624                     {
3625                       undo_all ();
3626                       return 0;
3627                     }
3628                 }
3629
3630           /* If the split point was a MULT and we didn't have one before,
3631              don't use one now.  */
3632           if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3633             insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3634         }
3635     }
3636
3637   /* Check for a case where we loaded from memory in a narrow mode and
3638      then sign extended it, but we need both registers.  In that case,
3639      we have a PARALLEL with both loads from the same memory location.
3640      We can split this into a load from memory followed by a register-register
3641      copy.  This saves at least one insn, more if register allocation can
3642      eliminate the copy.
3643
3644      We cannot do this if the destination of the first assignment is a
3645      condition code register or cc0.  We eliminate this case by making sure
3646      the SET_DEST and SET_SRC have the same mode.
3647
3648      We cannot do this if the destination of the second assignment is
3649      a register that we have already assumed is zero-extended.  Similarly
3650      for a SUBREG of such a register.  */
3651
3652   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3653            && GET_CODE (newpat) == PARALLEL
3654            && XVECLEN (newpat, 0) == 2
3655            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3656            && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3657            && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3658                == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3659            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3660            && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3661                            XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3662            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3663                                    DF_INSN_LUID (i2))
3664            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3665            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3666            && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
3667                  (REG_P (temp)
3668                   && VEC_index (reg_stat_type, reg_stat,
3669                                 REGNO (temp))->nonzero_bits != 0
3670                   && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3671                   && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3672                   && (VEC_index (reg_stat_type, reg_stat,
3673                                  REGNO (temp))->nonzero_bits
3674                       != GET_MODE_MASK (word_mode))))
3675            && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3676                  && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3677                      (REG_P (temp)
3678                       && VEC_index (reg_stat_type, reg_stat,
3679                                     REGNO (temp))->nonzero_bits != 0
3680                       && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3681                       && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3682                       && (VEC_index (reg_stat_type, reg_stat,
3683                                      REGNO (temp))->nonzero_bits
3684                           != GET_MODE_MASK (word_mode)))))
3685            && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3686                                          SET_SRC (XVECEXP (newpat, 0, 1)))
3687            && ! find_reg_note (i3, REG_UNUSED,
3688                                SET_DEST (XVECEXP (newpat, 0, 0))))
3689     {
3690       rtx ni2dest;
3691
3692       newi2pat = XVECEXP (newpat, 0, 0);
3693       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3694       newpat = XVECEXP (newpat, 0, 1);
3695       SUBST (SET_SRC (newpat),
3696              gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3697       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3698
3699       if (i2_code_number >= 0)
3700         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3701
3702       if (insn_code_number >= 0)
3703         swap_i2i3 = 1;
3704     }
3705
3706   /* Similarly, check for a case where we have a PARALLEL of two independent
3707      SETs but we started with three insns.  In this case, we can do the sets
3708      as two separate insns.  This case occurs when some SET allows two
3709      other insns to combine, but the destination of that SET is still live.  */
3710
3711   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3712            && GET_CODE (newpat) == PARALLEL
3713            && XVECLEN (newpat, 0) == 2
3714            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3715            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3716            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3717            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3718            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3719            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3720            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3721                                   XVECEXP (newpat, 0, 0))
3722            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3723                                   XVECEXP (newpat, 0, 1))
3724            && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3725                  && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3726     {
3727       /* Normally, it doesn't matter which of the two is done first,
3728          but the one that references cc0 can't be the second, and
3729          one which uses any regs/memory set in between i2 and i3 can't
3730          be first.  */
3731       if (!use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3732                               DF_INSN_LUID (i2))
3733 #ifdef HAVE_cc0
3734           && !reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0))
3735 #endif
3736          )
3737         {
3738           newi2pat = XVECEXP (newpat, 0, 1);
3739           newpat = XVECEXP (newpat, 0, 0);
3740         }
3741       else if (!use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 0)),
3742                                    DF_INSN_LUID (i2))
3743 #ifdef HAVE_cc0
3744                && !reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 1))
3745 #endif
3746               )
3747         {
3748           newi2pat = XVECEXP (newpat, 0, 0);
3749           newpat = XVECEXP (newpat, 0, 1);
3750         }
3751       else
3752         {
3753           undo_all ();
3754           return 0;
3755         }
3756
3757       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3758
3759       if (i2_code_number >= 0)
3760         {
3761           /* recog_for_combine might have added CLOBBERs to newi2pat.
3762              Make sure NEWPAT does not depend on the clobbered regs.  */
3763           if (GET_CODE (newi2pat) == PARALLEL)
3764             {
3765               for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3766                 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3767                   {
3768                     rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3769                     if (reg_overlap_mentioned_p (reg, newpat))
3770                       {
3771                         undo_all ();
3772                         return 0;
3773                       }
3774                   }
3775             }
3776
3777           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3778         }
3779     }
3780
3781   /* If it still isn't recognized, fail and change things back the way they
3782      were.  */
3783   if ((insn_code_number < 0
3784        /* Is the result a reasonable ASM_OPERANDS?  */
3785        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3786     {
3787       undo_all ();
3788       return 0;
3789     }
3790
3791   /* If we had to change another insn, make sure it is valid also.  */
3792   if (undobuf.other_insn)
3793     {
3794       CLEAR_HARD_REG_SET (newpat_used_regs);
3795
3796       other_pat = PATTERN (undobuf.other_insn);
3797       other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3798                                              &new_other_notes);
3799
3800       if (other_code_number < 0 && ! check_asm_operands (other_pat))
3801         {
3802           undo_all ();
3803           return 0;
3804         }
3805     }
3806
3807 #ifdef HAVE_cc0
3808   /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3809      they are adjacent to each other or not.  */
3810   {
3811     rtx p = prev_nonnote_insn (i3);
3812     if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3813         && sets_cc0_p (newi2pat))
3814       {
3815         undo_all ();
3816         return 0;
3817       }
3818   }
3819 #endif
3820
3821   /* Only allow this combination if insn_rtx_costs reports that the
3822      replacement instructions are cheaper than the originals.  */
3823   if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
3824     {
3825       undo_all ();
3826       return 0;
3827     }
3828
3829   if (MAY_HAVE_DEBUG_INSNS)
3830     {
3831       struct undo *undo;
3832
3833       for (undo = undobuf.undos; undo; undo = undo->next)
3834         if (undo->kind == UNDO_MODE)
3835           {
3836             rtx reg = *undo->where.r;
3837             enum machine_mode new_mode = GET_MODE (reg);
3838             enum machine_mode old_mode = undo->old_contents.m;
3839
3840             /* Temporarily revert mode back.  */
3841             adjust_reg_mode (reg, old_mode);
3842
3843             if (reg == i2dest && i2scratch)
3844               {
3845                 /* If we used i2dest as a scratch register with a
3846                    different mode, substitute it for the original
3847                    i2src while its original mode is temporarily
3848                    restored, and then clear i2scratch so that we don't
3849                    do it again later.  */
3850                 propagate_for_debug (i2, i3, reg, i2src);
3851                 i2scratch = false;
3852                 /* Put back the new mode.  */
3853                 adjust_reg_mode (reg, new_mode);
3854               }
3855             else
3856               {
3857                 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
3858                 rtx first, last;
3859
3860                 if (reg == i2dest)
3861                   {
3862                     first = i2;
3863                     last = i3;
3864                   }
3865                 else
3866                   {
3867                     first = i3;
3868                     last = undobuf.other_insn;
3869                     gcc_assert (last);
3870                   }
3871
3872                 /* We're dealing with a reg that changed mode but not
3873                    meaning, so we want to turn it into a subreg for
3874                    the new mode.  However, because of REG sharing and
3875                    because its mode had already changed, we have to do
3876                    it in two steps.  First, replace any debug uses of
3877                    reg, with its original mode temporarily restored,
3878                    with this copy we have created; then, replace the
3879                    copy with the SUBREG of the original shared reg,
3880                    once again changed to the new mode.  */
3881                 propagate_for_debug (first, last, reg, tempreg);
3882                 adjust_reg_mode (reg, new_mode);
3883                 propagate_for_debug (first, last, tempreg,
3884                                      lowpart_subreg (old_mode, reg, new_mode));
3885               }
3886           }
3887     }
3888
3889   /* If we will be able to accept this, we have made a
3890      change to the destination of I3.  This requires us to
3891      do a few adjustments.  */
3892
3893   if (changed_i3_dest)
3894     {
3895       PATTERN (i3) = newpat;
3896       adjust_for_new_dest (i3);
3897     }
3898
3899   /* We now know that we can do this combination.  Merge the insns and
3900      update the status of registers and LOG_LINKS.  */
3901
3902   if (undobuf.other_insn)
3903     {
3904       rtx note, next;
3905
3906       PATTERN (undobuf.other_insn) = other_pat;
3907
3908       /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
3909          are still valid.  Then add any non-duplicate notes added by
3910          recog_for_combine.  */
3911       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
3912         {
3913           next = XEXP (note, 1);
3914
3915           if (REG_NOTE_KIND (note) == REG_UNUSED
3916               && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
3917             remove_note (undobuf.other_insn, note);
3918         }
3919
3920       distribute_notes (new_other_notes, undobuf.other_insn,
3921                         undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX,
3922                         NULL_RTX);
3923     }
3924
3925   if (swap_i2i3)
3926     {
3927       rtx insn;
3928       rtx link;
3929       rtx ni2dest;
3930
3931       /* I3 now uses what used to be its destination and which is now
3932          I2's destination.  This requires us to do a few adjustments.  */
3933       PATTERN (i3) = newpat;
3934       adjust_for_new_dest (i3);
3935
3936       /* We need a LOG_LINK from I3 to I2.  But we used to have one,
3937          so we still will.
3938
3939          However, some later insn might be using I2's dest and have
3940          a LOG_LINK pointing at I3.  We must remove this link.
3941          The simplest way to remove the link is to point it at I1,
3942          which we know will be a NOTE.  */
3943
3944       /* newi2pat is usually a SET here; however, recog_for_combine might
3945          have added some clobbers.  */
3946       if (GET_CODE (newi2pat) == PARALLEL)
3947         ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
3948       else
3949         ni2dest = SET_DEST (newi2pat);
3950
3951       for (insn = NEXT_INSN (i3);
3952            insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3953                     || insn != BB_HEAD (this_basic_block->next_bb));
3954            insn = NEXT_INSN (insn))
3955         {
3956           if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
3957             {
3958               for (link = LOG_LINKS (insn); link;
3959                    link = XEXP (link, 1))
3960                 if (XEXP (link, 0) == i3)
3961                   XEXP (link, 0) = i1;
3962
3963               break;
3964             }
3965         }
3966     }
3967
3968   {
3969     rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
3970     rtx i3links, i2links, i1links = 0, i0links = 0;
3971     rtx midnotes = 0;
3972     int from_luid;
3973     unsigned int regno;
3974     /* Compute which registers we expect to eliminate.  newi2pat may be setting
3975        either i3dest or i2dest, so we must check it.  Also, i1dest may be the
3976        same as i3dest, in which case newi2pat may be setting i1dest.  */
3977     rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
3978                    || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
3979                    || !i2dest_killed
3980                    ? 0 : i2dest);
3981     rtx elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
3982                    || (newi2pat && reg_set_p (i1dest, newi2pat))
3983                    || !i1dest_killed
3984                    ? 0 : i1dest);
3985     rtx elim_i0 = (i0 == 0 || i0dest_in_i0src
3986                    || (newi2pat && reg_set_p (i0dest, newi2pat))
3987                    || !i0dest_killed
3988                    ? 0 : i0dest);
3989
3990     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
3991        clear them.  */
3992     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
3993     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
3994     if (i1)
3995       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
3996     if (i0)
3997       i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
3998
3999     /* Ensure that we do not have something that should not be shared but
4000        occurs multiple times in the new insns.  Check this by first
4001        resetting all the `used' flags and then copying anything is shared.  */
4002
4003     reset_used_flags (i3notes);
4004     reset_used_flags (i2notes);
4005     reset_used_flags (i1notes);
4006     reset_used_flags (i0notes);
4007     reset_used_flags (newpat);
4008     reset_used_flags (newi2pat);
4009     if (undobuf.other_insn)
4010       reset_used_flags (PATTERN (undobuf.other_insn));
4011
4012     i3notes = copy_rtx_if_shared (i3notes);
4013     i2notes = copy_rtx_if_shared (i2notes);
4014     i1notes = copy_rtx_if_shared (i1notes);
4015     i0notes = copy_rtx_if_shared (i0notes);
4016     newpat = copy_rtx_if_shared (newpat);
4017     newi2pat = copy_rtx_if_shared (newi2pat);
4018     if (undobuf.other_insn)
4019       reset_used_flags (PATTERN (undobuf.other_insn));
4020
4021     INSN_CODE (i3) = insn_code_number;
4022     PATTERN (i3) = newpat;
4023
4024     if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4025       {
4026         rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
4027
4028         reset_used_flags (call_usage);
4029         call_usage = copy_rtx (call_usage);
4030
4031         if (substed_i2)
4032           {
4033             /* I2SRC must still be meaningful at this point.  Some splitting
4034                operations can invalidate I2SRC, but those operations do not
4035                apply to calls.  */
4036             gcc_assert (i2src);
4037             replace_rtx (call_usage, i2dest, i2src);
4038           }
4039
4040         if (substed_i1)
4041           replace_rtx (call_usage, i1dest, i1src);
4042         if (substed_i0)
4043           replace_rtx (call_usage, i0dest, i0src);
4044
4045         CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4046       }
4047
4048     if (undobuf.other_insn)
4049       INSN_CODE (undobuf.other_insn) = other_code_number;
4050
4051     /* We had one special case above where I2 had more than one set and
4052        we replaced a destination of one of those sets with the destination
4053        of I3.  In that case, we have to update LOG_LINKS of insns later
4054        in this basic block.  Note that this (expensive) case is rare.
4055
4056        Also, in this case, we must pretend that all REG_NOTEs for I2
4057        actually came from I3, so that REG_UNUSED notes from I2 will be
4058        properly handled.  */
4059
4060     if (i3_subst_into_i2)
4061       {
4062         for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4063           if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4064                || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4065               && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4066               && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4067               && ! find_reg_note (i2, REG_UNUSED,
4068                                   SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4069             for (temp = NEXT_INSN (i2);
4070                  temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
4071                           || BB_HEAD (this_basic_block) != temp);
4072                  temp = NEXT_INSN (temp))
4073               if (temp != i3 && INSN_P (temp))
4074                 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
4075                   if (XEXP (link, 0) == i2)
4076                     XEXP (link, 0) = i3;
4077
4078         if (i3notes)
4079           {
4080             rtx link = i3notes;
4081             while (XEXP (link, 1))
4082               link = XEXP (link, 1);
4083             XEXP (link, 1) = i2notes;
4084           }
4085         else
4086           i3notes = i2notes;
4087         i2notes = 0;
4088       }
4089
4090     LOG_LINKS (i3) = 0;
4091     REG_NOTES (i3) = 0;
4092     LOG_LINKS (i2) = 0;
4093     REG_NOTES (i2) = 0;
4094
4095     if (newi2pat)
4096       {
4097         if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4098           propagate_for_debug (i2, i3, i2dest, i2src);
4099         INSN_CODE (i2) = i2_code_number;
4100         PATTERN (i2) = newi2pat;
4101       }
4102     else
4103       {
4104         if (MAY_HAVE_DEBUG_INSNS && i2src)
4105           propagate_for_debug (i2, i3, i2dest, i2src);
4106         SET_INSN_DELETED (i2);
4107       }
4108
4109     if (i1)
4110       {
4111         LOG_LINKS (i1) = 0;
4112         REG_NOTES (i1) = 0;
4113         if (MAY_HAVE_DEBUG_INSNS)
4114           propagate_for_debug (i1, i3, i1dest, i1src);
4115         SET_INSN_DELETED (i1);
4116       }
4117
4118     if (i0)
4119       {
4120         LOG_LINKS (i0) = 0;
4121         REG_NOTES (i0) = 0;
4122         if (MAY_HAVE_DEBUG_INSNS)
4123           propagate_for_debug (i0, i3, i0dest, i0src);
4124         SET_INSN_DELETED (i0);
4125       }
4126
4127     /* Get death notes for everything that is now used in either I3 or
4128        I2 and used to die in a previous insn.  If we built two new
4129        patterns, move from I1 to I2 then I2 to I3 so that we get the
4130        proper movement on registers that I2 modifies.  */
4131
4132     if (i0)
4133       from_luid = DF_INSN_LUID (i0);
4134     else if (i1)
4135       from_luid = DF_INSN_LUID (i1);
4136     else
4137       from_luid = DF_INSN_LUID (i2);
4138     if (newi2pat)
4139       move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4140     move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4141
4142     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
4143     if (i3notes)
4144       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
4145                         elim_i2, elim_i1, elim_i0);
4146     if (i2notes)
4147       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
4148                         elim_i2, elim_i1, elim_i0);
4149     if (i1notes)
4150       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
4151                         elim_i2, elim_i1, elim_i0);
4152     if (i0notes)
4153       distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL_RTX,
4154                         elim_i2, elim_i1, elim_i0);
4155     if (midnotes)
4156       distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4157                         elim_i2, elim_i1, elim_i0);
4158
4159     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
4160        know these are REG_UNUSED and want them to go to the desired insn,
4161        so we always pass it as i3.  */
4162
4163     if (newi2pat && new_i2_notes)
4164       distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX,
4165                         NULL_RTX);
4166
4167     if (new_i3_notes)
4168       distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX,
4169                         NULL_RTX);
4170
4171     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
4172        put a REG_DEAD note for it somewhere.  If NEWI2PAT exists and sets
4173        I3DEST, the death must be somewhere before I2, not I3.  If we passed I3
4174        in that case, it might delete I2.  Similarly for I2 and I1.
4175        Show an additional death due to the REG_DEAD note we make here.  If
4176        we discard it in distribute_notes, we will decrement it again.  */
4177
4178     if (i3dest_killed)
4179       {
4180         if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4181           distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
4182                                             NULL_RTX),
4183                             NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1, elim_i0);
4184         else
4185           distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
4186                                             NULL_RTX),
4187                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4188                             elim_i2, elim_i1, elim_i0);
4189       }
4190
4191     if (i2dest_in_i2src)
4192       {
4193         rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4194         if (newi2pat && reg_set_p (i2dest, newi2pat))
4195           distribute_notes (new_note,  NULL_RTX, i2, NULL_RTX, NULL_RTX,
4196                             NULL_RTX, NULL_RTX);
4197         else
4198           distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4199                             NULL_RTX, NULL_RTX, NULL_RTX);
4200       }
4201
4202     if (i1dest_in_i1src)
4203       {
4204         rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4205         if (newi2pat && reg_set_p (i1dest, newi2pat))
4206           distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4207                             NULL_RTX, NULL_RTX);
4208         else
4209           distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4210                             NULL_RTX, NULL_RTX, NULL_RTX);
4211       }
4212
4213     if (i0dest_in_i0src)
4214       {
4215         rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4216         if (newi2pat && reg_set_p (i0dest, newi2pat))
4217           distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4218                             NULL_RTX, NULL_RTX);
4219         else
4220           distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4221                             NULL_RTX, NULL_RTX, NULL_RTX);
4222       }
4223
4224     distribute_links (i3links);
4225     distribute_links (i2links);
4226     distribute_links (i1links);
4227     distribute_links (i0links);
4228
4229     if (REG_P (i2dest))
4230       {
4231         rtx link;
4232         rtx i2_insn = 0, i2_val = 0, set;
4233
4234         /* The insn that used to set this register doesn't exist, and
4235            this life of the register may not exist either.  See if one of
4236            I3's links points to an insn that sets I2DEST.  If it does,
4237            that is now the last known value for I2DEST. If we don't update
4238            this and I2 set the register to a value that depended on its old
4239            contents, we will get confused.  If this insn is used, thing
4240            will be set correctly in combine_instructions.  */
4241
4242         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
4243           if ((set = single_set (XEXP (link, 0))) != 0
4244               && rtx_equal_p (i2dest, SET_DEST (set)))
4245             i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
4246
4247         record_value_for_reg (i2dest, i2_insn, i2_val);
4248
4249         /* If the reg formerly set in I2 died only once and that was in I3,
4250            zero its use count so it won't make `reload' do any work.  */
4251         if (! added_sets_2
4252             && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4253             && ! i2dest_in_i2src)
4254           {
4255             regno = REGNO (i2dest);
4256             INC_REG_N_SETS (regno, -1);
4257           }
4258       }
4259
4260     if (i1 && REG_P (i1dest))
4261       {
4262         rtx link;
4263         rtx i1_insn = 0, i1_val = 0, set;
4264
4265         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
4266           if ((set = single_set (XEXP (link, 0))) != 0
4267               && rtx_equal_p (i1dest, SET_DEST (set)))
4268             i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
4269
4270         record_value_for_reg (i1dest, i1_insn, i1_val);
4271
4272         regno = REGNO (i1dest);
4273         if (! added_sets_1 && ! i1dest_in_i1src)
4274           INC_REG_N_SETS (regno, -1);
4275       }
4276
4277     if (i0 && REG_P (i0dest))
4278       {
4279         rtx link;
4280         rtx i0_insn = 0, i0_val = 0, set;
4281
4282         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
4283           if ((set = single_set (XEXP (link, 0))) != 0
4284               && rtx_equal_p (i0dest, SET_DEST (set)))
4285             i0_insn = XEXP (link, 0), i0_val = SET_SRC (set);
4286
4287         record_value_for_reg (i0dest, i0_insn, i0_val);
4288
4289         regno = REGNO (i0dest);
4290         if (! added_sets_0 && ! i0dest_in_i0src)
4291           INC_REG_N_SETS (regno, -1);
4292       }
4293
4294     /* Update reg_stat[].nonzero_bits et al for any changes that may have
4295        been made to this insn.  The order of
4296        set_nonzero_bits_and_sign_copies() is important.  Because newi2pat
4297        can affect nonzero_bits of newpat */
4298     if (newi2pat)
4299       note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4300     note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4301   }
4302
4303   if (undobuf.other_insn != NULL_RTX)
4304     {
4305       if (dump_file)
4306         {
4307           fprintf (dump_file, "modifying other_insn ");
4308           dump_insn_slim (dump_file, undobuf.other_insn);
4309         }
4310       df_insn_rescan (undobuf.other_insn);
4311     }
4312
4313   if (i0 && !(NOTE_P(i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4314     {
4315       if (dump_file)
4316         {
4317           fprintf (dump_file, "modifying insn i1 ");
4318           dump_insn_slim (dump_file, i0);
4319         }
4320       df_insn_rescan (i0);
4321     }
4322
4323   if (i1 && !(NOTE_P(i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4324     {
4325       if (dump_file)
4326         {
4327           fprintf (dump_file, "modifying insn i1 ");
4328           dump_insn_slim (dump_file, i1);
4329         }
4330       df_insn_rescan (i1);
4331     }
4332
4333   if (i2 && !(NOTE_P(i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4334     {
4335       if (dump_file)
4336         {
4337           fprintf (dump_file, "modifying insn i2 ");
4338           dump_insn_slim (dump_file, i2);
4339         }
4340       df_insn_rescan (i2);
4341     }
4342
4343   if (i3 && !(NOTE_P(i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4344     {
4345       if (dump_file)
4346         {
4347           fprintf (dump_file, "modifying insn i3 ");
4348           dump_insn_slim (dump_file, i3);
4349         }
4350       df_insn_rescan (i3);
4351     }
4352
4353   /* Set new_direct_jump_p if a new return or simple jump instruction
4354      has been created.  Adjust the CFG accordingly.  */
4355
4356   if (returnjump_p (i3) || any_uncondjump_p (i3))
4357     {
4358       *new_direct_jump_p = 1;
4359       mark_jump_label (PATTERN (i3), i3, 0);
4360       update_cfg_for_uncondjump (i3);
4361     }
4362
4363   if (undobuf.other_insn != NULL_RTX
4364       && (returnjump_p (undobuf.other_insn)
4365           || any_uncondjump_p (undobuf.other_insn)))
4366     {
4367       *new_direct_jump_p = 1;
4368       update_cfg_for_uncondjump (undobuf.other_insn);
4369     }
4370
4371   /* A noop might also need cleaning up of CFG, if it comes from the
4372      simplification of a jump.  */
4373   if (GET_CODE (newpat) == SET
4374       && SET_SRC (newpat) == pc_rtx
4375       && SET_DEST (newpat) == pc_rtx)
4376     {
4377       *new_direct_jump_p = 1;
4378       update_cfg_for_uncondjump (i3);
4379     }
4380
4381   combine_successes++;
4382   undo_commit ();
4383
4384   if (added_links_insn
4385       && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4386       && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4387     return added_links_insn;
4388   else
4389     return newi2pat ? i2 : i3;
4390 }
4391 \f
4392 /* Undo all the modifications recorded in undobuf.  */
4393
4394 static void
4395 undo_all (void)
4396 {
4397   struct undo *undo, *next;
4398
4399   for (undo = undobuf.undos; undo; undo = next)
4400     {
4401       next = undo->next;
4402       switch (undo->kind)
4403         {
4404         case UNDO_RTX:
4405           *undo->where.r = undo->old_contents.r;
4406           break;
4407         case UNDO_INT:
4408           *undo->where.i = undo->old_contents.i;
4409           break;
4410         case UNDO_MODE:
4411           adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4412           break;
4413         default:
4414           gcc_unreachable ();
4415         }
4416
4417       undo->next = undobuf.frees;
4418       undobuf.frees = undo;
4419     }
4420
4421   undobuf.undos = 0;
4422 }
4423
4424 /* We've committed to accepting the changes we made.  Move all
4425    of the undos to the free list.  */
4426
4427 static void
4428 undo_commit (void)
4429 {
4430   struct undo *undo, *next;
4431
4432   for (undo = undobuf.undos; undo; undo = next)
4433     {
4434       next = undo->next;
4435       undo->next = undobuf.frees;
4436       undobuf.frees = undo;
4437     }
4438   undobuf.undos = 0;
4439 }
4440 \f
4441 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4442    where we have an arithmetic expression and return that point.  LOC will
4443    be inside INSN.
4444
4445    try_combine will call this function to see if an insn can be split into
4446    two insns.  */
4447
4448 static rtx *
4449 find_split_point (rtx *loc, rtx insn, bool set_src)
4450 {
4451   rtx x = *loc;
4452   enum rtx_code code = GET_CODE (x);
4453   rtx *split;
4454   unsigned HOST_WIDE_INT len = 0;
4455   HOST_WIDE_INT pos = 0;
4456   int unsignedp = 0;
4457   rtx inner = NULL_RTX;
4458
4459   /* First special-case some codes.  */
4460   switch (code)
4461     {
4462     case SUBREG:
4463 #ifdef INSN_SCHEDULING
4464       /* If we are making a paradoxical SUBREG invalid, it becomes a split
4465          point.  */
4466       if (MEM_P (SUBREG_REG (x)))
4467         return loc;
4468 #endif
4469       return find_split_point (&SUBREG_REG (x), insn, false);
4470
4471     case MEM:
4472 #ifdef HAVE_lo_sum
4473       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4474          using LO_SUM and HIGH.  */
4475       if (GET_CODE (XEXP (x, 0)) == CONST
4476           || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4477         {
4478           enum machine_mode address_mode
4479             = targetm.addr_space.address_mode (MEM_ADDR_SPACE (x));
4480
4481           SUBST (XEXP (x, 0),
4482                  gen_rtx_LO_SUM (address_mode,
4483                                  gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4484                                  XEXP (x, 0)));
4485           return &XEXP (XEXP (x, 0), 0);
4486         }
4487 #endif
4488
4489       /* If we have a PLUS whose second operand is a constant and the
4490          address is not valid, perhaps will can split it up using
4491          the machine-specific way to split large constants.  We use
4492          the first pseudo-reg (one of the virtual regs) as a placeholder;
4493          it will not remain in the result.  */
4494       if (GET_CODE (XEXP (x, 0)) == PLUS
4495           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4496           && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4497                                             MEM_ADDR_SPACE (x)))
4498         {
4499           rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4500           rtx seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4501                                                       XEXP (x, 0)),
4502                                          subst_insn);
4503
4504           /* This should have produced two insns, each of which sets our
4505              placeholder.  If the source of the second is a valid address,
4506              we can make put both sources together and make a split point
4507              in the middle.  */
4508
4509           if (seq
4510               && NEXT_INSN (seq) != NULL_RTX
4511               && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4512               && NONJUMP_INSN_P (seq)
4513               && GET_CODE (PATTERN (seq)) == SET
4514               && SET_DEST (PATTERN (seq)) == reg
4515               && ! reg_mentioned_p (reg,
4516                                     SET_SRC (PATTERN (seq)))
4517               && NONJUMP_INSN_P (NEXT_INSN (seq))
4518               && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4519               && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4520               && memory_address_addr_space_p
4521                    (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4522                     MEM_ADDR_SPACE (x)))
4523             {
4524               rtx src1 = SET_SRC (PATTERN (seq));
4525               rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4526
4527               /* Replace the placeholder in SRC2 with SRC1.  If we can
4528                  find where in SRC2 it was placed, that can become our
4529                  split point and we can replace this address with SRC2.
4530                  Just try two obvious places.  */
4531
4532               src2 = replace_rtx (src2, reg, src1);
4533               split = 0;
4534               if (XEXP (src2, 0) == src1)
4535                 split = &XEXP (src2, 0);
4536               else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4537                        && XEXP (XEXP (src2, 0), 0) == src1)
4538                 split = &XEXP (XEXP (src2, 0), 0);
4539
4540               if (split)
4541                 {
4542                   SUBST (XEXP (x, 0), src2);
4543                   return split;
4544                 }
4545             }
4546
4547           /* If that didn't work, perhaps the first operand is complex and
4548              needs to be computed separately, so make a split point there.
4549              This will occur on machines that just support REG + CONST
4550              and have a constant moved through some previous computation.  */
4551
4552           else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4553                    && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4554                          && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4555             return &XEXP (XEXP (x, 0), 0);
4556         }
4557
4558       /* If we have a PLUS whose first operand is complex, try computing it
4559          separately by making a split there.  */
4560       if (GET_CODE (XEXP (x, 0)) == PLUS
4561           && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4562                                             MEM_ADDR_SPACE (x))
4563           && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4564           && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4565                 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4566         return &XEXP (XEXP (x, 0), 0);
4567       break;
4568
4569     case SET:
4570 #ifdef HAVE_cc0
4571       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4572          ZERO_EXTRACT, the most likely reason why this doesn't match is that
4573          we need to put the operand into a register.  So split at that
4574          point.  */
4575
4576       if (SET_DEST (x) == cc0_rtx
4577           && GET_CODE (SET_SRC (x)) != COMPARE
4578           && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4579           && !OBJECT_P (SET_SRC (x))
4580           && ! (GET_CODE (SET_SRC (x)) == SUBREG
4581                 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4582         return &SET_SRC (x);
4583 #endif
4584
4585       /* See if we can split SET_SRC as it stands.  */
4586       split = find_split_point (&SET_SRC (x), insn, true);
4587       if (split && split != &SET_SRC (x))
4588         return split;
4589
4590       /* See if we can split SET_DEST as it stands.  */
4591       split = find_split_point (&SET_DEST (x), insn, false);
4592       if (split && split != &SET_DEST (x))
4593         return split;
4594
4595       /* See if this is a bitfield assignment with everything constant.  If
4596          so, this is an IOR of an AND, so split it into that.  */
4597       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4598           && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
4599               <= HOST_BITS_PER_WIDE_INT)
4600           && CONST_INT_P (XEXP (SET_DEST (x), 1))
4601           && CONST_INT_P (XEXP (SET_DEST (x), 2))
4602           && CONST_INT_P (SET_SRC (x))
4603           && ((INTVAL (XEXP (SET_DEST (x), 1))
4604                + INTVAL (XEXP (SET_DEST (x), 2)))
4605               <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
4606           && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4607         {
4608           HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4609           unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4610           unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4611           rtx dest = XEXP (SET_DEST (x), 0);
4612           enum machine_mode mode = GET_MODE (dest);
4613           unsigned HOST_WIDE_INT mask
4614             = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4615           rtx or_mask;
4616
4617           if (BITS_BIG_ENDIAN)
4618             pos = GET_MODE_BITSIZE (mode) - len - pos;
4619
4620           or_mask = gen_int_mode (src << pos, mode);
4621           if (src == mask)
4622             SUBST (SET_SRC (x),
4623                    simplify_gen_binary (IOR, mode, dest, or_mask));
4624           else
4625             {
4626               rtx negmask = gen_int_mode (~(mask << pos), mode);
4627               SUBST (SET_SRC (x),
4628                      simplify_gen_binary (IOR, mode,
4629                                           simplify_gen_binary (AND, mode,
4630                                                                dest, negmask),
4631                                           or_mask));
4632             }
4633
4634           SUBST (SET_DEST (x), dest);
4635
4636           split = find_split_point (&SET_SRC (x), insn, true);
4637           if (split && split != &SET_SRC (x))
4638             return split;
4639         }
4640
4641       /* Otherwise, see if this is an operation that we can split into two.
4642          If so, try to split that.  */
4643       code = GET_CODE (SET_SRC (x));
4644
4645       switch (code)
4646         {
4647         case AND:
4648           /* If we are AND'ing with a large constant that is only a single
4649              bit and the result is only being used in a context where we
4650              need to know if it is zero or nonzero, replace it with a bit
4651              extraction.  This will avoid the large constant, which might
4652              have taken more than one insn to make.  If the constant were
4653              not a valid argument to the AND but took only one insn to make,
4654              this is no worse, but if it took more than one insn, it will
4655              be better.  */
4656
4657           if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4658               && REG_P (XEXP (SET_SRC (x), 0))
4659               && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4660               && REG_P (SET_DEST (x))
4661               && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
4662               && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4663               && XEXP (*split, 0) == SET_DEST (x)
4664               && XEXP (*split, 1) == const0_rtx)
4665             {
4666               rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4667                                                 XEXP (SET_SRC (x), 0),
4668                                                 pos, NULL_RTX, 1, 1, 0, 0);
4669               if (extraction != 0)
4670                 {
4671                   SUBST (SET_SRC (x), extraction);
4672                   return find_split_point (loc, insn, false);
4673                 }
4674             }
4675           break;
4676
4677         case NE:
4678           /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4679              is known to be on, this can be converted into a NEG of a shift.  */
4680           if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4681               && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4682               && 1 <= (pos = exact_log2
4683                        (nonzero_bits (XEXP (SET_SRC (x), 0),
4684                                       GET_MODE (XEXP (SET_SRC (x), 0))))))
4685             {
4686               enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4687
4688               SUBST (SET_SRC (x),
4689                      gen_rtx_NEG (mode,
4690                                   gen_rtx_LSHIFTRT (mode,
4691                                                     XEXP (SET_SRC (x), 0),
4692                                                     GEN_INT (pos))));
4693
4694               split = find_split_point (&SET_SRC (x), insn, true);
4695               if (split && split != &SET_SRC (x))
4696                 return split;
4697             }
4698           break;
4699
4700         case SIGN_EXTEND:
4701           inner = XEXP (SET_SRC (x), 0);
4702
4703           /* We can't optimize if either mode is a partial integer
4704              mode as we don't know how many bits are significant
4705              in those modes.  */
4706           if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4707               || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4708             break;
4709
4710           pos = 0;
4711           len = GET_MODE_BITSIZE (GET_MODE (inner));
4712           unsignedp = 0;
4713           break;
4714
4715         case SIGN_EXTRACT:
4716         case ZERO_EXTRACT:
4717           if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4718               && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4719             {
4720               inner = XEXP (SET_SRC (x), 0);
4721               len = INTVAL (XEXP (SET_SRC (x), 1));
4722               pos = INTVAL (XEXP (SET_SRC (x), 2));
4723
4724               if (BITS_BIG_ENDIAN)
4725                 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
4726               unsignedp = (code == ZERO_EXTRACT);
4727             }
4728           break;
4729
4730         default:
4731           break;
4732         }
4733
4734       if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
4735         {
4736           enum machine_mode mode = GET_MODE (SET_SRC (x));
4737
4738           /* For unsigned, we have a choice of a shift followed by an
4739              AND or two shifts.  Use two shifts for field sizes where the
4740              constant might be too large.  We assume here that we can
4741              always at least get 8-bit constants in an AND insn, which is
4742              true for every current RISC.  */
4743
4744           if (unsignedp && len <= 8)
4745             {
4746               SUBST (SET_SRC (x),
4747                      gen_rtx_AND (mode,
4748                                   gen_rtx_LSHIFTRT
4749                                   (mode, gen_lowpart (mode, inner),
4750                                    GEN_INT (pos)),
4751                                   GEN_INT (((unsigned HOST_WIDE_INT) 1 << len)
4752                                            - 1)));
4753
4754               split = find_split_point (&SET_SRC (x), insn, true);
4755               if (split && split != &SET_SRC (x))
4756                 return split;
4757             }
4758           else
4759             {
4760               SUBST (SET_SRC (x),
4761                      gen_rtx_fmt_ee
4762                      (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4763                       gen_rtx_ASHIFT (mode,
4764                                       gen_lowpart (mode, inner),
4765                                       GEN_INT (GET_MODE_BITSIZE (mode)
4766                                                - len - pos)),
4767                       GEN_INT (GET_MODE_BITSIZE (mode) - len)));
4768
4769               split = find_split_point (&SET_SRC (x), insn, true);
4770               if (split && split != &SET_SRC (x))
4771                 return split;
4772             }
4773         }
4774
4775       /* See if this is a simple operation with a constant as the second
4776          operand.  It might be that this constant is out of range and hence
4777          could be used as a split point.  */
4778       if (BINARY_P (SET_SRC (x))
4779           && CONSTANT_P (XEXP (SET_SRC (x), 1))
4780           && (OBJECT_P (XEXP (SET_SRC (x), 0))
4781               || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4782                   && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4783         return &XEXP (SET_SRC (x), 1);
4784
4785       /* Finally, see if this is a simple operation with its first operand
4786          not in a register.  The operation might require this operand in a
4787          register, so return it as a split point.  We can always do this
4788          because if the first operand were another operation, we would have
4789          already found it as a split point.  */
4790       if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4791           && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4792         return &XEXP (SET_SRC (x), 0);
4793
4794       return 0;
4795
4796     case AND:
4797     case IOR:
4798       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4799          it is better to write this as (not (ior A B)) so we can split it.
4800          Similarly for IOR.  */
4801       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4802         {
4803           SUBST (*loc,
4804                  gen_rtx_NOT (GET_MODE (x),
4805                               gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4806                                               GET_MODE (x),
4807                                               XEXP (XEXP (x, 0), 0),
4808                                               XEXP (XEXP (x, 1), 0))));
4809           return find_split_point (loc, insn, set_src);
4810         }
4811
4812       /* Many RISC machines have a large set of logical insns.  If the
4813          second operand is a NOT, put it first so we will try to split the
4814          other operand first.  */
4815       if (GET_CODE (XEXP (x, 1)) == NOT)
4816         {
4817           rtx tem = XEXP (x, 0);
4818           SUBST (XEXP (x, 0), XEXP (x, 1));
4819           SUBST (XEXP (x, 1), tem);
4820         }
4821       break;
4822
4823     case PLUS:
4824     case MINUS:
4825       /* Canonicalization can produce (minus A (mult B C)), where C is a
4826          constant.  It may be better to try splitting (plus (mult B -C) A)
4827          instead if this isn't a multiply by a power of two.  */
4828       if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
4829           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4830           && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
4831         {
4832           enum machine_mode mode = GET_MODE (x);
4833           unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
4834           HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
4835           SUBST (*loc, gen_rtx_PLUS (mode, gen_rtx_MULT (mode,
4836                                                          XEXP (XEXP (x, 1), 0),
4837                                                          GEN_INT (other_int)),
4838                                      XEXP (x, 0)));
4839           return find_split_point (loc, insn, set_src);
4840         }
4841
4842       /* Split at a multiply-accumulate instruction.  However if this is
4843          the SET_SRC, we likely do not have such an instruction and it's
4844          worthless to try this split.  */
4845       if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
4846         return loc;
4847
4848     default:
4849       break;
4850     }
4851
4852   /* Otherwise, select our actions depending on our rtx class.  */
4853   switch (GET_RTX_CLASS (code))
4854     {
4855     case RTX_BITFIELD_OPS:              /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
4856     case RTX_TERNARY:
4857       split = find_split_point (&XEXP (x, 2), insn, false);
4858       if (split)
4859         return split;
4860       /* ... fall through ...  */
4861     case RTX_BIN_ARITH:
4862     case RTX_COMM_ARITH:
4863     case RTX_COMPARE:
4864     case RTX_COMM_COMPARE:
4865       split = find_split_point (&XEXP (x, 1), insn, false);
4866       if (split)
4867         return split;
4868       /* ... fall through ...  */
4869     case RTX_UNARY:
4870       /* Some machines have (and (shift ...) ...) insns.  If X is not
4871          an AND, but XEXP (X, 0) is, use it as our split point.  */
4872       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4873         return &XEXP (x, 0);
4874
4875       split = find_split_point (&XEXP (x, 0), insn, false);
4876       if (split)
4877         return split;
4878       return loc;
4879
4880     default:
4881       /* Otherwise, we don't have a split point.  */
4882       return 0;
4883     }
4884 }
4885 \f
4886 /* Throughout X, replace FROM with TO, and return the result.
4887    The result is TO if X is FROM;
4888    otherwise the result is X, but its contents may have been modified.
4889    If they were modified, a record was made in undobuf so that
4890    undo_all will (among other things) return X to its original state.
4891
4892    If the number of changes necessary is too much to record to undo,
4893    the excess changes are not made, so the result is invalid.
4894    The changes already made can still be undone.
4895    undobuf.num_undo is incremented for such changes, so by testing that
4896    the caller can tell whether the result is valid.
4897
4898    `n_occurrences' is incremented each time FROM is replaced.
4899
4900    IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4901
4902    UNIQUE_COPY is nonzero if each substitution must be unique.  We do this
4903    by copying if `n_occurrences' is nonzero.  */
4904
4905 static rtx
4906 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
4907 {
4908   enum rtx_code code = GET_CODE (x);
4909   enum machine_mode op0_mode = VOIDmode;
4910   const char *fmt;
4911   int len, i;
4912   rtx new_rtx;
4913
4914 /* Two expressions are equal if they are identical copies of a shared
4915    RTX or if they are both registers with the same register number
4916    and mode.  */
4917
4918 #define COMBINE_RTX_EQUAL_P(X,Y)                        \
4919   ((X) == (Y)                                           \
4920    || (REG_P (X) && REG_P (Y)   \
4921        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4922
4923   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
4924     {
4925       n_occurrences++;
4926       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
4927     }
4928
4929   /* If X and FROM are the same register but different modes, they
4930      will not have been seen as equal above.  However, the log links code
4931      will make a LOG_LINKS entry for that case.  If we do nothing, we
4932      will try to rerecognize our original insn and, when it succeeds,
4933      we will delete the feeding insn, which is incorrect.
4934
4935      So force this insn not to match in this (rare) case.  */
4936   if (! in_dest && code == REG && REG_P (from)
4937       && reg_overlap_mentioned_p (x, from))
4938     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
4939
4940   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4941      of which may contain things that can be combined.  */
4942   if (code != MEM && code != LO_SUM && OBJECT_P (x))
4943     return x;
4944
4945   /* It is possible to have a subexpression appear twice in the insn.
4946      Suppose that FROM is a register that appears within TO.
4947      Then, after that subexpression has been scanned once by `subst',
4948      the second time it is scanned, TO may be found.  If we were
4949      to scan TO here, we would find FROM within it and create a
4950      self-referent rtl structure which is completely wrong.  */
4951   if (COMBINE_RTX_EQUAL_P (x, to))
4952     return to;
4953
4954   /* Parallel asm_operands need special attention because all of the
4955      inputs are shared across the arms.  Furthermore, unsharing the
4956      rtl results in recognition failures.  Failure to handle this case
4957      specially can result in circular rtl.
4958
4959      Solve this by doing a normal pass across the first entry of the
4960      parallel, and only processing the SET_DESTs of the subsequent
4961      entries.  Ug.  */
4962
4963   if (code == PARALLEL
4964       && GET_CODE (XVECEXP (x, 0, 0)) == SET
4965       && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
4966     {
4967       new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
4968
4969       /* If this substitution failed, this whole thing fails.  */
4970       if (GET_CODE (new_rtx) == CLOBBER
4971           && XEXP (new_rtx, 0) == const0_rtx)
4972         return new_rtx;
4973
4974       SUBST (XVECEXP (x, 0, 0), new_rtx);
4975
4976       for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
4977         {
4978           rtx dest = SET_DEST (XVECEXP (x, 0, i));
4979
4980           if (!REG_P (dest)
4981               && GET_CODE (dest) != CC0
4982               && GET_CODE (dest) != PC)
4983             {
4984               new_rtx = subst (dest, from, to, 0, unique_copy);
4985
4986               /* If this substitution failed, this whole thing fails.  */
4987               if (GET_CODE (new_rtx) == CLOBBER
4988                   && XEXP (new_rtx, 0) == const0_rtx)
4989                 return new_rtx;
4990
4991               SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
4992             }
4993         }
4994     }
4995   else
4996     {
4997       len = GET_RTX_LENGTH (code);
4998       fmt = GET_RTX_FORMAT (code);
4999
5000       /* We don't need to process a SET_DEST that is a register, CC0,
5001          or PC, so set up to skip this common case.  All other cases
5002          where we want to suppress replacing something inside a
5003          SET_SRC are handled via the IN_DEST operand.  */
5004       if (code == SET
5005           && (REG_P (SET_DEST (x))
5006               || GET_CODE (SET_DEST (x)) == CC0
5007               || GET_CODE (SET_DEST (x)) == PC))
5008         fmt = "ie";
5009
5010       /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5011          constant.  */
5012       if (fmt[0] == 'e')
5013         op0_mode = GET_MODE (XEXP (x, 0));
5014
5015       for (i = 0; i < len; i++)
5016         {
5017           if (fmt[i] == 'E')
5018             {
5019               int j;
5020               for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5021                 {
5022                   if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5023                     {
5024                       new_rtx = (unique_copy && n_occurrences
5025                              ? copy_rtx (to) : to);
5026                       n_occurrences++;
5027                     }
5028                   else
5029                     {
5030                       new_rtx = subst (XVECEXP (x, i, j), from, to, 0,
5031                                    unique_copy);
5032
5033                       /* If this substitution failed, this whole thing
5034                          fails.  */
5035                       if (GET_CODE (new_rtx) == CLOBBER
5036                           && XEXP (new_rtx, 0) == const0_rtx)
5037                         return new_rtx;
5038                     }
5039
5040                   SUBST (XVECEXP (x, i, j), new_rtx);
5041                 }
5042             }
5043           else if (fmt[i] == 'e')
5044             {
5045               /* If this is a register being set, ignore it.  */
5046               new_rtx = XEXP (x, i);
5047               if (in_dest
5048                   && i == 0
5049                   && (((code == SUBREG || code == ZERO_EXTRACT)
5050                        && REG_P (new_rtx))
5051                       || code == STRICT_LOW_PART))
5052                 ;
5053
5054               else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5055                 {
5056                   /* In general, don't install a subreg involving two
5057                      modes not tieable.  It can worsen register
5058                      allocation, and can even make invalid reload
5059                      insns, since the reg inside may need to be copied
5060                      from in the outside mode, and that may be invalid
5061                      if it is an fp reg copied in integer mode.
5062
5063                      We allow two exceptions to this: It is valid if
5064                      it is inside another SUBREG and the mode of that
5065                      SUBREG and the mode of the inside of TO is
5066                      tieable and it is valid if X is a SET that copies
5067                      FROM to CC0.  */
5068
5069                   if (GET_CODE (to) == SUBREG
5070                       && ! MODES_TIEABLE_P (GET_MODE (to),
5071                                             GET_MODE (SUBREG_REG (to)))
5072                       && ! (code == SUBREG
5073                             && MODES_TIEABLE_P (GET_MODE (x),
5074                                                 GET_MODE (SUBREG_REG (to))))
5075 #ifdef HAVE_cc0
5076                       && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5077 #endif
5078                       )
5079                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5080
5081 #ifdef CANNOT_CHANGE_MODE_CLASS
5082                   if (code == SUBREG
5083                       && REG_P (to)
5084                       && REGNO (to) < FIRST_PSEUDO_REGISTER
5085                       && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
5086                                                    GET_MODE (to),
5087                                                    GET_MODE (x)))
5088                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5089 #endif
5090
5091                   new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5092                   n_occurrences++;
5093                 }
5094               else
5095                 /* If we are in a SET_DEST, suppress most cases unless we
5096                    have gone inside a MEM, in which case we want to
5097                    simplify the address.  We assume here that things that
5098                    are actually part of the destination have their inner
5099                    parts in the first expression.  This is true for SUBREG,
5100                    STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5101                    things aside from REG and MEM that should appear in a
5102                    SET_DEST.  */
5103                 new_rtx = subst (XEXP (x, i), from, to,
5104                              (((in_dest
5105                                 && (code == SUBREG || code == STRICT_LOW_PART
5106                                     || code == ZERO_EXTRACT))
5107                                || code == SET)
5108                               && i == 0), unique_copy);
5109
5110               /* If we found that we will have to reject this combination,
5111                  indicate that by returning the CLOBBER ourselves, rather than
5112                  an expression containing it.  This will speed things up as
5113                  well as prevent accidents where two CLOBBERs are considered
5114                  to be equal, thus producing an incorrect simplification.  */
5115
5116               if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5117                 return new_rtx;
5118
5119               if (GET_CODE (x) == SUBREG
5120                   && (CONST_INT_P (new_rtx)
5121                       || GET_CODE (new_rtx) == CONST_DOUBLE))
5122                 {
5123                   enum machine_mode mode = GET_MODE (x);
5124
5125                   x = simplify_subreg (GET_MODE (x), new_rtx,
5126                                        GET_MODE (SUBREG_REG (x)),
5127                                        SUBREG_BYTE (x));
5128                   if (! x)
5129                     x = gen_rtx_CLOBBER (mode, const0_rtx);
5130                 }
5131               else if (CONST_INT_P (new_rtx)
5132                        && GET_CODE (x) == ZERO_EXTEND)
5133                 {
5134                   x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5135                                                 new_rtx, GET_MODE (XEXP (x, 0)));
5136                   gcc_assert (x);
5137                 }
5138               else
5139                 SUBST (XEXP (x, i), new_rtx);
5140             }
5141         }
5142     }
5143
5144   /* Check if we are loading something from the constant pool via float
5145      extension; in this case we would undo compress_float_constant
5146      optimization and degenerate constant load to an immediate value.  */
5147   if (GET_CODE (x) == FLOAT_EXTEND
5148       && MEM_P (XEXP (x, 0))
5149       && MEM_READONLY_P (XEXP (x, 0)))
5150     {
5151       rtx tmp = avoid_constant_pool_reference (x);
5152       if (x != tmp)
5153         return x;
5154     }
5155
5156   /* Try to simplify X.  If the simplification changed the code, it is likely
5157      that further simplification will help, so loop, but limit the number
5158      of repetitions that will be performed.  */
5159
5160   for (i = 0; i < 4; i++)
5161     {
5162       /* If X is sufficiently simple, don't bother trying to do anything
5163          with it.  */
5164       if (code != CONST_INT && code != REG && code != CLOBBER)
5165         x = combine_simplify_rtx (x, op0_mode, in_dest);
5166
5167       if (GET_CODE (x) == code)
5168         break;
5169
5170       code = GET_CODE (x);
5171
5172       /* We no longer know the original mode of operand 0 since we
5173          have changed the form of X)  */
5174       op0_mode = VOIDmode;
5175     }
5176
5177   return x;
5178 }
5179 \f
5180 /* Simplify X, a piece of RTL.  We just operate on the expression at the
5181    outer level; call `subst' to simplify recursively.  Return the new
5182    expression.
5183
5184    OP0_MODE is the original mode of XEXP (x, 0).  IN_DEST is nonzero
5185    if we are inside a SET_DEST.  */
5186
5187 static rtx
5188 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
5189 {
5190   enum rtx_code code = GET_CODE (x);
5191   enum machine_mode mode = GET_MODE (x);
5192   rtx temp;
5193   int i;
5194
5195   /* If this is a commutative operation, put a constant last and a complex
5196      expression first.  We don't need to do this for comparisons here.  */
5197   if (COMMUTATIVE_ARITH_P (x)
5198       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5199     {
5200       temp = XEXP (x, 0);
5201       SUBST (XEXP (x, 0), XEXP (x, 1));
5202       SUBST (XEXP (x, 1), temp);
5203     }
5204
5205   /* If this is a simple operation applied to an IF_THEN_ELSE, try
5206      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
5207      things.  Check for cases where both arms are testing the same
5208      condition.
5209
5210      Don't do anything if all operands are very simple.  */
5211
5212   if ((BINARY_P (x)
5213        && ((!OBJECT_P (XEXP (x, 0))
5214             && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5215                   && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5216            || (!OBJECT_P (XEXP (x, 1))
5217                && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5218                      && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5219       || (UNARY_P (x)
5220           && (!OBJECT_P (XEXP (x, 0))
5221                && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5222                      && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5223     {
5224       rtx cond, true_rtx, false_rtx;
5225
5226       cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5227       if (cond != 0
5228           /* If everything is a comparison, what we have is highly unlikely
5229              to be simpler, so don't use it.  */
5230           && ! (COMPARISON_P (x)
5231                 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5232         {
5233           rtx cop1 = const0_rtx;
5234           enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5235
5236           if (cond_code == NE && COMPARISON_P (cond))
5237             return x;
5238
5239           /* Simplify the alternative arms; this may collapse the true and
5240              false arms to store-flag values.  Be careful to use copy_rtx
5241              here since true_rtx or false_rtx might share RTL with x as a
5242              result of the if_then_else_cond call above.  */
5243           true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
5244           false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
5245
5246           /* If true_rtx and false_rtx are not general_operands, an if_then_else
5247              is unlikely to be simpler.  */
5248           if (general_operand (true_rtx, VOIDmode)
5249               && general_operand (false_rtx, VOIDmode))
5250             {
5251               enum rtx_code reversed;
5252
5253               /* Restarting if we generate a store-flag expression will cause
5254                  us to loop.  Just drop through in this case.  */
5255
5256               /* If the result values are STORE_FLAG_VALUE and zero, we can
5257                  just make the comparison operation.  */
5258               if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5259                 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5260                                              cond, cop1);
5261               else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5262                        && ((reversed = reversed_comparison_code_parts
5263                                         (cond_code, cond, cop1, NULL))
5264                            != UNKNOWN))
5265                 x = simplify_gen_relational (reversed, mode, VOIDmode,
5266                                              cond, cop1);
5267
5268               /* Likewise, we can make the negate of a comparison operation
5269                  if the result values are - STORE_FLAG_VALUE and zero.  */
5270               else if (CONST_INT_P (true_rtx)
5271                        && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5272                        && false_rtx == const0_rtx)
5273                 x = simplify_gen_unary (NEG, mode,
5274                                         simplify_gen_relational (cond_code,
5275                                                                  mode, VOIDmode,
5276                                                                  cond, cop1),
5277                                         mode);
5278               else if (CONST_INT_P (false_rtx)
5279                        && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5280                        && true_rtx == const0_rtx
5281                        && ((reversed = reversed_comparison_code_parts
5282                                         (cond_code, cond, cop1, NULL))
5283                            != UNKNOWN))
5284                 x = simplify_gen_unary (NEG, mode,
5285                                         simplify_gen_relational (reversed,
5286                                                                  mode, VOIDmode,
5287                                                                  cond, cop1),
5288                                         mode);
5289               else
5290                 return gen_rtx_IF_THEN_ELSE (mode,
5291                                              simplify_gen_relational (cond_code,
5292                                                                       mode,
5293                                                                       VOIDmode,
5294                                                                       cond,
5295                                                                       cop1),
5296                                              true_rtx, false_rtx);
5297
5298               code = GET_CODE (x);
5299               op0_mode = VOIDmode;
5300             }
5301         }
5302     }
5303
5304   /* Try to fold this expression in case we have constants that weren't
5305      present before.  */
5306   temp = 0;
5307   switch (GET_RTX_CLASS (code))
5308     {
5309     case RTX_UNARY:
5310       if (op0_mode == VOIDmode)
5311         op0_mode = GET_MODE (XEXP (x, 0));
5312       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5313       break;
5314     case RTX_COMPARE:
5315     case RTX_COMM_COMPARE:
5316       {
5317         enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5318         if (cmp_mode == VOIDmode)
5319           {
5320             cmp_mode = GET_MODE (XEXP (x, 1));
5321             if (cmp_mode == VOIDmode)
5322               cmp_mode = op0_mode;
5323           }
5324         temp = simplify_relational_operation (code, mode, cmp_mode,
5325                                               XEXP (x, 0), XEXP (x, 1));
5326       }
5327       break;
5328     case RTX_COMM_ARITH:
5329     case RTX_BIN_ARITH:
5330       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5331       break;
5332     case RTX_BITFIELD_OPS:
5333     case RTX_TERNARY:
5334       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5335                                          XEXP (x, 1), XEXP (x, 2));
5336       break;
5337     default:
5338       break;
5339     }
5340
5341   if (temp)
5342     {
5343       x = temp;
5344       code = GET_CODE (temp);
5345       op0_mode = VOIDmode;
5346       mode = GET_MODE (temp);
5347     }
5348
5349   /* First see if we can apply the inverse distributive law.  */
5350   if (code == PLUS || code == MINUS
5351       || code == AND || code == IOR || code == XOR)
5352     {
5353       x = apply_distributive_law (x);
5354       code = GET_CODE (x);
5355       op0_mode = VOIDmode;
5356     }
5357
5358   /* If CODE is an associative operation not otherwise handled, see if we
5359      can associate some operands.  This can win if they are constants or
5360      if they are logically related (i.e. (a & b) & a).  */
5361   if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5362        || code == AND || code == IOR || code == XOR
5363        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5364       && ((INTEGRAL_MODE_P (mode) && code != DIV)
5365           || (flag_associative_math && FLOAT_MODE_P (mode))))
5366     {
5367       if (GET_CODE (XEXP (x, 0)) == code)
5368         {
5369           rtx other = XEXP (XEXP (x, 0), 0);
5370           rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5371           rtx inner_op1 = XEXP (x, 1);
5372           rtx inner;
5373
5374           /* Make sure we pass the constant operand if any as the second
5375              one if this is a commutative operation.  */
5376           if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5377             {
5378               rtx tem = inner_op0;
5379               inner_op0 = inner_op1;
5380               inner_op1 = tem;
5381             }
5382           inner = simplify_binary_operation (code == MINUS ? PLUS
5383                                              : code == DIV ? MULT
5384                                              : code,
5385                                              mode, inner_op0, inner_op1);
5386
5387           /* For commutative operations, try the other pair if that one
5388              didn't simplify.  */
5389           if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5390             {
5391               other = XEXP (XEXP (x, 0), 1);
5392               inner = simplify_binary_operation (code, mode,
5393                                                  XEXP (XEXP (x, 0), 0),
5394                                                  XEXP (x, 1));
5395             }
5396
5397           if (inner)
5398             return simplify_gen_binary (code, mode, other, inner);
5399         }
5400     }
5401
5402   /* A little bit of algebraic simplification here.  */
5403   switch (code)
5404     {
5405     case MEM:
5406       /* Ensure that our address has any ASHIFTs converted to MULT in case
5407          address-recognizing predicates are called later.  */
5408       temp = make_compound_operation (XEXP (x, 0), MEM);
5409       SUBST (XEXP (x, 0), temp);
5410       break;
5411
5412     case SUBREG:
5413       if (op0_mode == VOIDmode)
5414         op0_mode = GET_MODE (SUBREG_REG (x));
5415
5416       /* See if this can be moved to simplify_subreg.  */
5417       if (CONSTANT_P (SUBREG_REG (x))
5418           && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5419              /* Don't call gen_lowpart if the inner mode
5420                 is VOIDmode and we cannot simplify it, as SUBREG without
5421                 inner mode is invalid.  */
5422           && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5423               || gen_lowpart_common (mode, SUBREG_REG (x))))
5424         return gen_lowpart (mode, SUBREG_REG (x));
5425
5426       if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5427         break;
5428       {
5429         rtx temp;
5430         temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5431                                 SUBREG_BYTE (x));
5432         if (temp)
5433           return temp;
5434       }
5435
5436       /* Don't change the mode of the MEM if that would change the meaning
5437          of the address.  */
5438       if (MEM_P (SUBREG_REG (x))
5439           && (MEM_VOLATILE_P (SUBREG_REG (x))
5440               || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
5441         return gen_rtx_CLOBBER (mode, const0_rtx);
5442
5443       /* Note that we cannot do any narrowing for non-constants since
5444          we might have been counting on using the fact that some bits were
5445          zero.  We now do this in the SET.  */
5446
5447       break;
5448
5449     case NEG:
5450       temp = expand_compound_operation (XEXP (x, 0));
5451
5452       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5453          replaced by (lshiftrt X C).  This will convert
5454          (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
5455
5456       if (GET_CODE (temp) == ASHIFTRT
5457           && CONST_INT_P (XEXP (temp, 1))
5458           && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
5459         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5460                                      INTVAL (XEXP (temp, 1)));
5461
5462       /* If X has only a single bit that might be nonzero, say, bit I, convert
5463          (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5464          MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
5465          (sign_extract X 1 Y).  But only do this if TEMP isn't a register
5466          or a SUBREG of one since we'd be making the expression more
5467          complex if it was just a register.  */
5468
5469       if (!REG_P (temp)
5470           && ! (GET_CODE (temp) == SUBREG
5471                 && REG_P (SUBREG_REG (temp)))
5472           && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5473         {
5474           rtx temp1 = simplify_shift_const
5475             (NULL_RTX, ASHIFTRT, mode,
5476              simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5477                                    GET_MODE_BITSIZE (mode) - 1 - i),
5478              GET_MODE_BITSIZE (mode) - 1 - i);
5479
5480           /* If all we did was surround TEMP with the two shifts, we
5481              haven't improved anything, so don't use it.  Otherwise,
5482              we are better off with TEMP1.  */
5483           if (GET_CODE (temp1) != ASHIFTRT
5484               || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5485               || XEXP (XEXP (temp1, 0), 0) != temp)
5486             return temp1;
5487         }
5488       break;
5489
5490     case TRUNCATE:
5491       /* We can't handle truncation to a partial integer mode here
5492          because we don't know the real bitsize of the partial
5493          integer mode.  */
5494       if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5495         break;
5496
5497       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5498         SUBST (XEXP (x, 0),
5499                force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5500                               GET_MODE_MASK (mode), 0));
5501
5502       /* We can truncate a constant value and return it.  */
5503       if (CONST_INT_P (XEXP (x, 0)))
5504         return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5505
5506       /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5507          whose value is a comparison can be replaced with a subreg if
5508          STORE_FLAG_VALUE permits.  */
5509       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5510           && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5511           && (temp = get_last_value (XEXP (x, 0)))
5512           && COMPARISON_P (temp))
5513         return gen_lowpart (mode, XEXP (x, 0));
5514       break;
5515
5516     case CONST:
5517       /* (const (const X)) can become (const X).  Do it this way rather than
5518          returning the inner CONST since CONST can be shared with a
5519          REG_EQUAL note.  */
5520       if (GET_CODE (XEXP (x, 0)) == CONST)
5521         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5522       break;
5523
5524 #ifdef HAVE_lo_sum
5525     case LO_SUM:
5526       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
5527          can add in an offset.  find_split_point will split this address up
5528          again if it doesn't match.  */
5529       if (GET_CODE (XEXP (x, 0)) == HIGH
5530           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5531         return XEXP (x, 1);
5532       break;
5533 #endif
5534
5535     case PLUS:
5536       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5537          when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5538          bit-field and can be replaced by either a sign_extend or a
5539          sign_extract.  The `and' may be a zero_extend and the two
5540          <c>, -<c> constants may be reversed.  */
5541       if (GET_CODE (XEXP (x, 0)) == XOR
5542           && CONST_INT_P (XEXP (x, 1))
5543           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5544           && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5545           && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5546               || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5547           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5548           && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5549                && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5550                && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5551                    == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5552               || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5553                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5554                       == (unsigned int) i + 1))))
5555         return simplify_shift_const
5556           (NULL_RTX, ASHIFTRT, mode,
5557            simplify_shift_const (NULL_RTX, ASHIFT, mode,
5558                                  XEXP (XEXP (XEXP (x, 0), 0), 0),
5559                                  GET_MODE_BITSIZE (mode) - (i + 1)),
5560            GET_MODE_BITSIZE (mode) - (i + 1));
5561
5562       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5563          can become (ashiftrt (ashift (xor x 1) C) C) where C is
5564          the bitsize of the mode - 1.  This allows simplification of
5565          "a = (b & 8) == 0;"  */
5566       if (XEXP (x, 1) == constm1_rtx
5567           && !REG_P (XEXP (x, 0))
5568           && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5569                 && REG_P (SUBREG_REG (XEXP (x, 0))))
5570           && nonzero_bits (XEXP (x, 0), mode) == 1)
5571         return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5572            simplify_shift_const (NULL_RTX, ASHIFT, mode,
5573                                  gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5574                                  GET_MODE_BITSIZE (mode) - 1),
5575            GET_MODE_BITSIZE (mode) - 1);
5576
5577       /* If we are adding two things that have no bits in common, convert
5578          the addition into an IOR.  This will often be further simplified,
5579          for example in cases like ((a & 1) + (a & 2)), which can
5580          become a & 3.  */
5581
5582       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5583           && (nonzero_bits (XEXP (x, 0), mode)
5584               & nonzero_bits (XEXP (x, 1), mode)) == 0)
5585         {
5586           /* Try to simplify the expression further.  */
5587           rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5588           temp = combine_simplify_rtx (tor, mode, in_dest);
5589
5590           /* If we could, great.  If not, do not go ahead with the IOR
5591              replacement, since PLUS appears in many special purpose
5592              address arithmetic instructions.  */
5593           if (GET_CODE (temp) != CLOBBER && temp != tor)
5594             return temp;
5595         }
5596       break;
5597
5598     case MINUS:
5599       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5600          (and <foo> (const_int pow2-1))  */
5601       if (GET_CODE (XEXP (x, 1)) == AND
5602           && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5603           && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5604           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5605         return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5606                                        -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5607       break;
5608
5609     case MULT:
5610       /* If we have (mult (plus A B) C), apply the distributive law and then
5611          the inverse distributive law to see if things simplify.  This
5612          occurs mostly in addresses, often when unrolling loops.  */
5613
5614       if (GET_CODE (XEXP (x, 0)) == PLUS)
5615         {
5616           rtx result = distribute_and_simplify_rtx (x, 0);
5617           if (result)
5618             return result;
5619         }
5620
5621       /* Try simplify a*(b/c) as (a*b)/c.  */
5622       if (FLOAT_MODE_P (mode) && flag_associative_math
5623           && GET_CODE (XEXP (x, 0)) == DIV)
5624         {
5625           rtx tem = simplify_binary_operation (MULT, mode,
5626                                                XEXP (XEXP (x, 0), 0),
5627                                                XEXP (x, 1));
5628           if (tem)
5629             return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5630         }
5631       break;
5632
5633     case UDIV:
5634       /* If this is a divide by a power of two, treat it as a shift if
5635          its first operand is a shift.  */
5636       if (CONST_INT_P (XEXP (x, 1))
5637           && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5638           && (GET_CODE (XEXP (x, 0)) == ASHIFT
5639               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5640               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5641               || GET_CODE (XEXP (x, 0)) == ROTATE
5642               || GET_CODE (XEXP (x, 0)) == ROTATERT))
5643         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5644       break;
5645
5646     case EQ:  case NE:
5647     case GT:  case GTU:  case GE:  case GEU:
5648     case LT:  case LTU:  case LE:  case LEU:
5649     case UNEQ:  case LTGT:
5650     case UNGT:  case UNGE:
5651     case UNLT:  case UNLE:
5652     case UNORDERED: case ORDERED:
5653       /* If the first operand is a condition code, we can't do anything
5654          with it.  */
5655       if (GET_CODE (XEXP (x, 0)) == COMPARE
5656           || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5657               && ! CC0_P (XEXP (x, 0))))
5658         {
5659           rtx op0 = XEXP (x, 0);
5660           rtx op1 = XEXP (x, 1);
5661           enum rtx_code new_code;
5662
5663           if (GET_CODE (op0) == COMPARE)
5664             op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5665
5666           /* Simplify our comparison, if possible.  */
5667           new_code = simplify_comparison (code, &op0, &op1);
5668
5669           /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5670              if only the low-order bit is possibly nonzero in X (such as when
5671              X is a ZERO_EXTRACT of one bit).  Similarly, we can convert EQ to
5672              (xor X 1) or (minus 1 X); we use the former.  Finally, if X is
5673              known to be either 0 or -1, NE becomes a NEG and EQ becomes
5674              (plus X 1).
5675
5676              Remove any ZERO_EXTRACT we made when thinking this was a
5677              comparison.  It may now be simpler to use, e.g., an AND.  If a
5678              ZERO_EXTRACT is indeed appropriate, it will be placed back by
5679              the call to make_compound_operation in the SET case.  */
5680
5681           if (STORE_FLAG_VALUE == 1
5682               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5683               && op1 == const0_rtx
5684               && mode == GET_MODE (op0)
5685               && nonzero_bits (op0, mode) == 1)
5686             return gen_lowpart (mode,
5687                                 expand_compound_operation (op0));
5688
5689           else if (STORE_FLAG_VALUE == 1
5690                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5691                    && op1 == const0_rtx
5692                    && mode == GET_MODE (op0)
5693                    && (num_sign_bit_copies (op0, mode)
5694                        == GET_MODE_BITSIZE (mode)))
5695             {
5696               op0 = expand_compound_operation (op0);
5697               return simplify_gen_unary (NEG, mode,
5698                                          gen_lowpart (mode, op0),
5699                                          mode);
5700             }
5701
5702           else if (STORE_FLAG_VALUE == 1
5703                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5704                    && op1 == const0_rtx
5705                    && mode == GET_MODE (op0)
5706                    && nonzero_bits (op0, mode) == 1)
5707             {
5708               op0 = expand_compound_operation (op0);
5709               return simplify_gen_binary (XOR, mode,
5710                                           gen_lowpart (mode, op0),
5711                                           const1_rtx);
5712             }
5713
5714           else if (STORE_FLAG_VALUE == 1
5715                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5716                    && op1 == const0_rtx
5717                    && mode == GET_MODE (op0)
5718                    && (num_sign_bit_copies (op0, mode)
5719                        == GET_MODE_BITSIZE (mode)))
5720             {
5721               op0 = expand_compound_operation (op0);
5722               return plus_constant (gen_lowpart (mode, op0), 1);
5723             }
5724
5725           /* If STORE_FLAG_VALUE is -1, we have cases similar to
5726              those above.  */
5727           if (STORE_FLAG_VALUE == -1
5728               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5729               && op1 == const0_rtx
5730               && (num_sign_bit_copies (op0, mode)
5731                   == GET_MODE_BITSIZE (mode)))
5732             return gen_lowpart (mode,
5733                                 expand_compound_operation (op0));
5734
5735           else if (STORE_FLAG_VALUE == -1
5736                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5737                    && op1 == const0_rtx
5738                    && mode == GET_MODE (op0)
5739                    && nonzero_bits (op0, mode) == 1)
5740             {
5741               op0 = expand_compound_operation (op0);
5742               return simplify_gen_unary (NEG, mode,
5743                                          gen_lowpart (mode, op0),
5744                                          mode);
5745             }
5746
5747           else if (STORE_FLAG_VALUE == -1
5748                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5749                    && op1 == const0_rtx
5750                    && mode == GET_MODE (op0)
5751                    && (num_sign_bit_copies (op0, mode)
5752                        == GET_MODE_BITSIZE (mode)))
5753             {
5754               op0 = expand_compound_operation (op0);
5755               return simplify_gen_unary (NOT, mode,
5756                                          gen_lowpart (mode, op0),
5757                                          mode);
5758             }
5759
5760           /* If X is 0/1, (eq X 0) is X-1.  */
5761           else if (STORE_FLAG_VALUE == -1
5762                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5763                    && op1 == const0_rtx
5764                    && mode == GET_MODE (op0)
5765                    && nonzero_bits (op0, mode) == 1)
5766             {
5767               op0 = expand_compound_operation (op0);
5768               return plus_constant (gen_lowpart (mode, op0), -1);
5769             }
5770
5771           /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5772              one bit that might be nonzero, we can convert (ne x 0) to
5773              (ashift x c) where C puts the bit in the sign bit.  Remove any
5774              AND with STORE_FLAG_VALUE when we are done, since we are only
5775              going to test the sign bit.  */
5776           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5777               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5778               && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5779                   == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5780               && op1 == const0_rtx
5781               && mode == GET_MODE (op0)
5782               && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5783             {
5784               x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5785                                         expand_compound_operation (op0),
5786                                         GET_MODE_BITSIZE (mode) - 1 - i);
5787               if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5788                 return XEXP (x, 0);
5789               else
5790                 return x;
5791             }
5792
5793           /* If the code changed, return a whole new comparison.  */
5794           if (new_code != code)
5795             return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5796
5797           /* Otherwise, keep this operation, but maybe change its operands.
5798              This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
5799           SUBST (XEXP (x, 0), op0);
5800           SUBST (XEXP (x, 1), op1);
5801         }
5802       break;
5803
5804     case IF_THEN_ELSE:
5805       return simplify_if_then_else (x);
5806
5807     case ZERO_EXTRACT:
5808     case SIGN_EXTRACT:
5809     case ZERO_EXTEND:
5810     case SIGN_EXTEND:
5811       /* If we are processing SET_DEST, we are done.  */
5812       if (in_dest)
5813         return x;
5814
5815       return expand_compound_operation (x);
5816
5817     case SET:
5818       return simplify_set (x);
5819
5820     case AND:
5821     case IOR:
5822       return simplify_logical (x);
5823
5824     case ASHIFT:
5825     case LSHIFTRT:
5826     case ASHIFTRT:
5827     case ROTATE:
5828     case ROTATERT:
5829       /* If this is a shift by a constant amount, simplify it.  */
5830       if (CONST_INT_P (XEXP (x, 1)))
5831         return simplify_shift_const (x, code, mode, XEXP (x, 0),
5832                                      INTVAL (XEXP (x, 1)));
5833
5834       else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5835         SUBST (XEXP (x, 1),
5836                force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5837                               ((unsigned HOST_WIDE_INT) 1
5838                                << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5839                               - 1,
5840                               0));
5841       break;
5842
5843     default:
5844       break;
5845     }
5846
5847   return x;
5848 }
5849 \f
5850 /* Simplify X, an IF_THEN_ELSE expression.  Return the new expression.  */
5851
5852 static rtx
5853 simplify_if_then_else (rtx x)
5854 {
5855   enum machine_mode mode = GET_MODE (x);
5856   rtx cond = XEXP (x, 0);
5857   rtx true_rtx = XEXP (x, 1);
5858   rtx false_rtx = XEXP (x, 2);
5859   enum rtx_code true_code = GET_CODE (cond);
5860   int comparison_p = COMPARISON_P (cond);
5861   rtx temp;
5862   int i;
5863   enum rtx_code false_code;
5864   rtx reversed;
5865
5866   /* Simplify storing of the truth value.  */
5867   if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5868     return simplify_gen_relational (true_code, mode, VOIDmode,
5869                                     XEXP (cond, 0), XEXP (cond, 1));
5870
5871   /* Also when the truth value has to be reversed.  */
5872   if (comparison_p
5873       && true_rtx == const0_rtx && false_rtx == const_true_rtx
5874       && (reversed = reversed_comparison (cond, mode)))
5875     return reversed;
5876
5877   /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5878      in it is being compared against certain values.  Get the true and false
5879      comparisons and see if that says anything about the value of each arm.  */
5880
5881   if (comparison_p
5882       && ((false_code = reversed_comparison_code (cond, NULL))
5883           != UNKNOWN)
5884       && REG_P (XEXP (cond, 0)))
5885     {
5886       HOST_WIDE_INT nzb;
5887       rtx from = XEXP (cond, 0);
5888       rtx true_val = XEXP (cond, 1);
5889       rtx false_val = true_val;
5890       int swapped = 0;
5891
5892       /* If FALSE_CODE is EQ, swap the codes and arms.  */
5893
5894       if (false_code == EQ)
5895         {
5896           swapped = 1, true_code = EQ, false_code = NE;
5897           temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5898         }
5899
5900       /* If we are comparing against zero and the expression being tested has
5901          only a single bit that might be nonzero, that is its value when it is
5902          not equal to zero.  Similarly if it is known to be -1 or 0.  */
5903
5904       if (true_code == EQ && true_val == const0_rtx
5905           && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5906         {
5907           false_code = EQ;
5908           false_val = GEN_INT (trunc_int_for_mode (nzb, GET_MODE (from)));
5909         }
5910       else if (true_code == EQ && true_val == const0_rtx
5911                && (num_sign_bit_copies (from, GET_MODE (from))
5912                    == GET_MODE_BITSIZE (GET_MODE (from))))
5913         {
5914           false_code = EQ;
5915           false_val = constm1_rtx;
5916         }
5917
5918       /* Now simplify an arm if we know the value of the register in the
5919          branch and it is used in the arm.  Be careful due to the potential
5920          of locally-shared RTL.  */
5921
5922       if (reg_mentioned_p (from, true_rtx))
5923         true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
5924                                       from, true_val),
5925                       pc_rtx, pc_rtx, 0, 0);
5926       if (reg_mentioned_p (from, false_rtx))
5927         false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
5928                                    from, false_val),
5929                        pc_rtx, pc_rtx, 0, 0);
5930
5931       SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
5932       SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
5933
5934       true_rtx = XEXP (x, 1);
5935       false_rtx = XEXP (x, 2);
5936       true_code = GET_CODE (cond);
5937     }
5938
5939   /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
5940      reversed, do so to avoid needing two sets of patterns for
5941      subtract-and-branch insns.  Similarly if we have a constant in the true
5942      arm, the false arm is the same as the first operand of the comparison, or
5943      the false arm is more complicated than the true arm.  */
5944
5945   if (comparison_p
5946       && reversed_comparison_code (cond, NULL) != UNKNOWN
5947       && (true_rtx == pc_rtx
5948           || (CONSTANT_P (true_rtx)
5949               && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
5950           || true_rtx == const0_rtx
5951           || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
5952           || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
5953               && !OBJECT_P (false_rtx))
5954           || reg_mentioned_p (true_rtx, false_rtx)
5955           || rtx_equal_p (false_rtx, XEXP (cond, 0))))
5956     {
5957       true_code = reversed_comparison_code (cond, NULL);
5958       SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
5959       SUBST (XEXP (x, 1), false_rtx);
5960       SUBST (XEXP (x, 2), true_rtx);
5961
5962       temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5963       cond = XEXP (x, 0);
5964
5965       /* It is possible that the conditional has been simplified out.  */
5966       true_code = GET_CODE (cond);
5967       comparison_p = COMPARISON_P (cond);
5968     }
5969
5970   /* If the two arms are identical, we don't need the comparison.  */
5971
5972   if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
5973     return true_rtx;
5974
5975   /* Convert a == b ? b : a to "a".  */
5976   if (true_code == EQ && ! side_effects_p (cond)
5977       && !HONOR_NANS (mode)
5978       && rtx_equal_p (XEXP (cond, 0), false_rtx)
5979       && rtx_equal_p (XEXP (cond, 1), true_rtx))
5980     return false_rtx;
5981   else if (true_code == NE && ! side_effects_p (cond)
5982            && !HONOR_NANS (mode)
5983            && rtx_equal_p (XEXP (cond, 0), true_rtx)
5984            && rtx_equal_p (XEXP (cond, 1), false_rtx))
5985     return true_rtx;
5986
5987   /* Look for cases where we have (abs x) or (neg (abs X)).  */
5988
5989   if (GET_MODE_CLASS (mode) == MODE_INT
5990       && comparison_p
5991       && XEXP (cond, 1) == const0_rtx
5992       && GET_CODE (false_rtx) == NEG
5993       && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
5994       && rtx_equal_p (true_rtx, XEXP (cond, 0))
5995       && ! side_effects_p (true_rtx))
5996     switch (true_code)
5997       {
5998       case GT:
5999       case GE:
6000         return simplify_gen_unary (ABS, mode, true_rtx, mode);
6001       case LT:
6002       case LE:
6003         return
6004           simplify_gen_unary (NEG, mode,
6005                               simplify_gen_unary (ABS, mode, true_rtx, mode),
6006                               mode);
6007       default:
6008         break;
6009       }
6010
6011   /* Look for MIN or MAX.  */
6012
6013   if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6014       && comparison_p
6015       && rtx_equal_p (XEXP (cond, 0), true_rtx)
6016       && rtx_equal_p (XEXP (cond, 1), false_rtx)
6017       && ! side_effects_p (cond))
6018     switch (true_code)
6019       {
6020       case GE:
6021       case GT:
6022         return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6023       case LE:
6024       case LT:
6025         return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6026       case GEU:
6027       case GTU:
6028         return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6029       case LEU:
6030       case LTU:
6031         return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6032       default:
6033         break;
6034       }
6035
6036   /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6037      second operand is zero, this can be done as (OP Z (mult COND C2)) where
6038      C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6039      SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6040      We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6041      neither 1 or -1, but it isn't worth checking for.  */
6042
6043   if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6044       && comparison_p
6045       && GET_MODE_CLASS (mode) == MODE_INT
6046       && ! side_effects_p (x))
6047     {
6048       rtx t = make_compound_operation (true_rtx, SET);
6049       rtx f = make_compound_operation (false_rtx, SET);
6050       rtx cond_op0 = XEXP (cond, 0);
6051       rtx cond_op1 = XEXP (cond, 1);
6052       enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6053       enum machine_mode m = mode;
6054       rtx z = 0, c1 = NULL_RTX;
6055
6056       if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6057            || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6058            || GET_CODE (t) == ASHIFT
6059            || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6060           && rtx_equal_p (XEXP (t, 0), f))
6061         c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6062
6063       /* If an identity-zero op is commutative, check whether there
6064          would be a match if we swapped the operands.  */
6065       else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6066                 || GET_CODE (t) == XOR)
6067                && rtx_equal_p (XEXP (t, 1), f))
6068         c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6069       else if (GET_CODE (t) == SIGN_EXTEND
6070                && (GET_CODE (XEXP (t, 0)) == PLUS
6071                    || GET_CODE (XEXP (t, 0)) == MINUS
6072                    || GET_CODE (XEXP (t, 0)) == IOR
6073                    || GET_CODE (XEXP (t, 0)) == XOR
6074                    || GET_CODE (XEXP (t, 0)) == ASHIFT
6075                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6076                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6077                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6078                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6079                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6080                && (num_sign_bit_copies (f, GET_MODE (f))
6081                    > (unsigned int)
6082                      (GET_MODE_BITSIZE (mode)
6083                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6084         {
6085           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6086           extend_op = SIGN_EXTEND;
6087           m = GET_MODE (XEXP (t, 0));
6088         }
6089       else if (GET_CODE (t) == SIGN_EXTEND
6090                && (GET_CODE (XEXP (t, 0)) == PLUS
6091                    || GET_CODE (XEXP (t, 0)) == IOR
6092                    || GET_CODE (XEXP (t, 0)) == XOR)
6093                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6094                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6095                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6096                && (num_sign_bit_copies (f, GET_MODE (f))
6097                    > (unsigned int)
6098                      (GET_MODE_BITSIZE (mode)
6099                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6100         {
6101           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6102           extend_op = SIGN_EXTEND;
6103           m = GET_MODE (XEXP (t, 0));
6104         }
6105       else if (GET_CODE (t) == ZERO_EXTEND
6106                && (GET_CODE (XEXP (t, 0)) == PLUS
6107                    || GET_CODE (XEXP (t, 0)) == MINUS
6108                    || GET_CODE (XEXP (t, 0)) == IOR
6109                    || GET_CODE (XEXP (t, 0)) == XOR
6110                    || GET_CODE (XEXP (t, 0)) == ASHIFT
6111                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6112                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6113                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6114                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6115                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6116                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6117                && ((nonzero_bits (f, GET_MODE (f))
6118                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6119                    == 0))
6120         {
6121           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6122           extend_op = ZERO_EXTEND;
6123           m = GET_MODE (XEXP (t, 0));
6124         }
6125       else if (GET_CODE (t) == ZERO_EXTEND
6126                && (GET_CODE (XEXP (t, 0)) == PLUS
6127                    || GET_CODE (XEXP (t, 0)) == IOR
6128                    || GET_CODE (XEXP (t, 0)) == XOR)
6129                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6130                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6131                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6132                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6133                && ((nonzero_bits (f, GET_MODE (f))
6134                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6135                    == 0))
6136         {
6137           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6138           extend_op = ZERO_EXTEND;
6139           m = GET_MODE (XEXP (t, 0));
6140         }
6141
6142       if (z)
6143         {
6144           temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6145                                                  cond_op0, cond_op1),
6146                         pc_rtx, pc_rtx, 0, 0);
6147           temp = simplify_gen_binary (MULT, m, temp,
6148                                       simplify_gen_binary (MULT, m, c1,
6149                                                            const_true_rtx));
6150           temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
6151           temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6152
6153           if (extend_op != UNKNOWN)
6154             temp = simplify_gen_unary (extend_op, mode, temp, m);
6155
6156           return temp;
6157         }
6158     }
6159
6160   /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6161      1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6162      negation of a single bit, we can convert this operation to a shift.  We
6163      can actually do this more generally, but it doesn't seem worth it.  */
6164
6165   if (true_code == NE && XEXP (cond, 1) == const0_rtx
6166       && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6167       && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6168            && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6169           || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6170                == GET_MODE_BITSIZE (mode))
6171               && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6172     return
6173       simplify_shift_const (NULL_RTX, ASHIFT, mode,
6174                             gen_lowpart (mode, XEXP (cond, 0)), i);
6175
6176   /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8.  */
6177   if (true_code == NE && XEXP (cond, 1) == const0_rtx
6178       && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6179       && GET_MODE (XEXP (cond, 0)) == mode
6180       && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6181           == nonzero_bits (XEXP (cond, 0), mode)
6182       && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6183     return XEXP (cond, 0);
6184
6185   return x;
6186 }
6187 \f
6188 /* Simplify X, a SET expression.  Return the new expression.  */
6189
6190 static rtx
6191 simplify_set (rtx x)
6192 {
6193   rtx src = SET_SRC (x);
6194   rtx dest = SET_DEST (x);
6195   enum machine_mode mode
6196     = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6197   rtx other_insn;
6198   rtx *cc_use;
6199
6200   /* (set (pc) (return)) gets written as (return).  */
6201   if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
6202     return src;
6203
6204   /* Now that we know for sure which bits of SRC we are using, see if we can
6205      simplify the expression for the object knowing that we only need the
6206      low-order bits.  */
6207
6208   if (GET_MODE_CLASS (mode) == MODE_INT
6209       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
6210     {
6211       src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6212       SUBST (SET_SRC (x), src);
6213     }
6214
6215   /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6216      the comparison result and try to simplify it unless we already have used
6217      undobuf.other_insn.  */
6218   if ((GET_MODE_CLASS (mode) == MODE_CC
6219        || GET_CODE (src) == COMPARE
6220        || CC0_P (dest))
6221       && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6222       && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6223       && COMPARISON_P (*cc_use)
6224       && rtx_equal_p (XEXP (*cc_use, 0), dest))
6225     {
6226       enum rtx_code old_code = GET_CODE (*cc_use);
6227       enum rtx_code new_code;
6228       rtx op0, op1, tmp;
6229       int other_changed = 0;
6230       enum machine_mode compare_mode = GET_MODE (dest);
6231
6232       if (GET_CODE (src) == COMPARE)
6233         op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6234       else
6235         op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6236
6237       tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6238                                            op0, op1);
6239       if (!tmp)
6240         new_code = old_code;
6241       else if (!CONSTANT_P (tmp))
6242         {
6243           new_code = GET_CODE (tmp);
6244           op0 = XEXP (tmp, 0);
6245           op1 = XEXP (tmp, 1);
6246         }
6247       else
6248         {
6249           rtx pat = PATTERN (other_insn);
6250           undobuf.other_insn = other_insn;
6251           SUBST (*cc_use, tmp);
6252
6253           /* Attempt to simplify CC user.  */
6254           if (GET_CODE (pat) == SET)
6255             {
6256               rtx new_rtx = simplify_rtx (SET_SRC (pat));
6257               if (new_rtx != NULL_RTX)
6258                 SUBST (SET_SRC (pat), new_rtx);
6259             }
6260
6261           /* Convert X into a no-op move.  */
6262           SUBST (SET_DEST (x), pc_rtx);
6263           SUBST (SET_SRC (x), pc_rtx);
6264           return x;
6265         }
6266
6267       /* Simplify our comparison, if possible.  */
6268       new_code = simplify_comparison (new_code, &op0, &op1);
6269
6270 #ifdef SELECT_CC_MODE
6271       /* If this machine has CC modes other than CCmode, check to see if we
6272          need to use a different CC mode here.  */
6273       if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6274         compare_mode = GET_MODE (op0);
6275       else
6276         compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6277
6278 #ifndef HAVE_cc0
6279       /* If the mode changed, we have to change SET_DEST, the mode in the
6280          compare, and the mode in the place SET_DEST is used.  If SET_DEST is
6281          a hard register, just build new versions with the proper mode.  If it
6282          is a pseudo, we lose unless it is only time we set the pseudo, in
6283          which case we can safely change its mode.  */
6284       if (compare_mode != GET_MODE (dest))
6285         {
6286           if (can_change_dest_mode (dest, 0, compare_mode))
6287             {
6288               unsigned int regno = REGNO (dest);
6289               rtx new_dest;
6290
6291               if (regno < FIRST_PSEUDO_REGISTER)
6292                 new_dest = gen_rtx_REG (compare_mode, regno);
6293               else
6294                 {
6295                   SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6296                   new_dest = regno_reg_rtx[regno];
6297                 }
6298
6299               SUBST (SET_DEST (x), new_dest);
6300               SUBST (XEXP (*cc_use, 0), new_dest);
6301               other_changed = 1;
6302
6303               dest = new_dest;
6304             }
6305         }
6306 #endif  /* cc0 */
6307 #endif  /* SELECT_CC_MODE */
6308
6309       /* If the code changed, we have to build a new comparison in
6310          undobuf.other_insn.  */
6311       if (new_code != old_code)
6312         {
6313           int other_changed_previously = other_changed;
6314           unsigned HOST_WIDE_INT mask;
6315           rtx old_cc_use = *cc_use;
6316
6317           SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6318                                           dest, const0_rtx));
6319           other_changed = 1;
6320
6321           /* If the only change we made was to change an EQ into an NE or
6322              vice versa, OP0 has only one bit that might be nonzero, and OP1
6323              is zero, check if changing the user of the condition code will
6324              produce a valid insn.  If it won't, we can keep the original code
6325              in that insn by surrounding our operation with an XOR.  */
6326
6327           if (((old_code == NE && new_code == EQ)
6328                || (old_code == EQ && new_code == NE))
6329               && ! other_changed_previously && op1 == const0_rtx
6330               && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
6331               && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6332             {
6333               rtx pat = PATTERN (other_insn), note = 0;
6334
6335               if ((recog_for_combine (&pat, other_insn, &note) < 0
6336                    && ! check_asm_operands (pat)))
6337                 {
6338                   *cc_use = old_cc_use;
6339                   other_changed = 0;
6340
6341                   op0 = simplify_gen_binary (XOR, GET_MODE (op0),
6342                                              op0, GEN_INT (mask));
6343                 }
6344             }
6345         }
6346
6347       if (other_changed)
6348         undobuf.other_insn = other_insn;
6349
6350       /* Otherwise, if we didn't previously have a COMPARE in the
6351          correct mode, we need one.  */
6352       if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6353         {
6354           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6355           src = SET_SRC (x);
6356         }
6357       else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6358         {
6359           SUBST (SET_SRC (x), op0);
6360           src = SET_SRC (x);
6361         }
6362       /* Otherwise, update the COMPARE if needed.  */
6363       else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6364         {
6365           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6366           src = SET_SRC (x);
6367         }
6368     }
6369   else
6370     {
6371       /* Get SET_SRC in a form where we have placed back any
6372          compound expressions.  Then do the checks below.  */
6373       src = make_compound_operation (src, SET);
6374       SUBST (SET_SRC (x), src);
6375     }
6376
6377   /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6378      and X being a REG or (subreg (reg)), we may be able to convert this to
6379      (set (subreg:m2 x) (op)).
6380
6381      We can always do this if M1 is narrower than M2 because that means that
6382      we only care about the low bits of the result.
6383
6384      However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6385      perform a narrower operation than requested since the high-order bits will
6386      be undefined.  On machine where it is defined, this transformation is safe
6387      as long as M1 and M2 have the same number of words.  */
6388
6389   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6390       && !OBJECT_P (SUBREG_REG (src))
6391       && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6392            / UNITS_PER_WORD)
6393           == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6394                + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6395 #ifndef WORD_REGISTER_OPERATIONS
6396       && (GET_MODE_SIZE (GET_MODE (src))
6397         < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6398 #endif
6399 #ifdef CANNOT_CHANGE_MODE_CLASS
6400       && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6401             && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6402                                          GET_MODE (SUBREG_REG (src)),
6403                                          GET_MODE (src)))
6404 #endif
6405       && (REG_P (dest)
6406           || (GET_CODE (dest) == SUBREG
6407               && REG_P (SUBREG_REG (dest)))))
6408     {
6409       SUBST (SET_DEST (x),
6410              gen_lowpart (GET_MODE (SUBREG_REG (src)),
6411                                       dest));
6412       SUBST (SET_SRC (x), SUBREG_REG (src));
6413
6414       src = SET_SRC (x), dest = SET_DEST (x);
6415     }
6416
6417 #ifdef HAVE_cc0
6418   /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6419      in SRC.  */
6420   if (dest == cc0_rtx
6421       && GET_CODE (src) == SUBREG
6422       && subreg_lowpart_p (src)
6423       && (GET_MODE_BITSIZE (GET_MODE (src))
6424           < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
6425     {
6426       rtx inner = SUBREG_REG (src);
6427       enum machine_mode inner_mode = GET_MODE (inner);
6428
6429       /* Here we make sure that we don't have a sign bit on.  */
6430       if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
6431           && (nonzero_bits (inner, inner_mode)
6432               < ((unsigned HOST_WIDE_INT) 1
6433                  << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
6434         {
6435           SUBST (SET_SRC (x), inner);
6436           src = SET_SRC (x);
6437         }
6438     }
6439 #endif
6440
6441 #ifdef LOAD_EXTEND_OP
6442   /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6443      would require a paradoxical subreg.  Replace the subreg with a
6444      zero_extend to avoid the reload that would otherwise be required.  */
6445
6446   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6447       && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6448       && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6449       && SUBREG_BYTE (src) == 0
6450       && (GET_MODE_SIZE (GET_MODE (src))
6451           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6452       && MEM_P (SUBREG_REG (src)))
6453     {
6454       SUBST (SET_SRC (x),
6455              gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6456                             GET_MODE (src), SUBREG_REG (src)));
6457
6458       src = SET_SRC (x);
6459     }
6460 #endif
6461
6462   /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6463      are comparing an item known to be 0 or -1 against 0, use a logical
6464      operation instead. Check for one of the arms being an IOR of the other
6465      arm with some value.  We compute three terms to be IOR'ed together.  In
6466      practice, at most two will be nonzero.  Then we do the IOR's.  */
6467
6468   if (GET_CODE (dest) != PC
6469       && GET_CODE (src) == IF_THEN_ELSE
6470       && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6471       && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6472       && XEXP (XEXP (src, 0), 1) == const0_rtx
6473       && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6474 #ifdef HAVE_conditional_move
6475       && ! can_conditionally_move_p (GET_MODE (src))
6476 #endif
6477       && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6478                                GET_MODE (XEXP (XEXP (src, 0), 0)))
6479           == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
6480       && ! side_effects_p (src))
6481     {
6482       rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6483                       ? XEXP (src, 1) : XEXP (src, 2));
6484       rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6485                    ? XEXP (src, 2) : XEXP (src, 1));
6486       rtx term1 = const0_rtx, term2, term3;
6487
6488       if (GET_CODE (true_rtx) == IOR
6489           && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6490         term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6491       else if (GET_CODE (true_rtx) == IOR
6492                && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6493         term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6494       else if (GET_CODE (false_rtx) == IOR
6495                && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6496         term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6497       else if (GET_CODE (false_rtx) == IOR
6498                && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6499         term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6500
6501       term2 = simplify_gen_binary (AND, GET_MODE (src),
6502                                    XEXP (XEXP (src, 0), 0), true_rtx);
6503       term3 = simplify_gen_binary (AND, GET_MODE (src),
6504                                    simplify_gen_unary (NOT, GET_MODE (src),
6505                                                        XEXP (XEXP (src, 0), 0),
6506                                                        GET_MODE (src)),
6507                                    false_rtx);
6508
6509       SUBST (SET_SRC (x),
6510              simplify_gen_binary (IOR, GET_MODE (src),
6511                                   simplify_gen_binary (IOR, GET_MODE (src),
6512                                                        term1, term2),
6513                                   term3));
6514
6515       src = SET_SRC (x);
6516     }
6517
6518   /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6519      whole thing fail.  */
6520   if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6521     return src;
6522   else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6523     return dest;
6524   else
6525     /* Convert this into a field assignment operation, if possible.  */
6526     return make_field_assignment (x);
6527 }
6528 \f
6529 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6530    result.  */
6531
6532 static rtx
6533 simplify_logical (rtx x)
6534 {
6535   enum machine_mode mode = GET_MODE (x);
6536   rtx op0 = XEXP (x, 0);
6537   rtx op1 = XEXP (x, 1);
6538
6539   switch (GET_CODE (x))
6540     {
6541     case AND:
6542       /* We can call simplify_and_const_int only if we don't lose
6543          any (sign) bits when converting INTVAL (op1) to
6544          "unsigned HOST_WIDE_INT".  */
6545       if (CONST_INT_P (op1)
6546           && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6547               || INTVAL (op1) > 0))
6548         {
6549           x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6550           if (GET_CODE (x) != AND)
6551             return x;
6552
6553           op0 = XEXP (x, 0);
6554           op1 = XEXP (x, 1);
6555         }
6556
6557       /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6558          apply the distributive law and then the inverse distributive
6559          law to see if things simplify.  */
6560       if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6561         {
6562           rtx result = distribute_and_simplify_rtx (x, 0);
6563           if (result)
6564             return result;
6565         }
6566       if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6567         {
6568           rtx result = distribute_and_simplify_rtx (x, 1);
6569           if (result)
6570             return result;
6571         }
6572       break;
6573
6574     case IOR:
6575       /* If we have (ior (and A B) C), apply the distributive law and then
6576          the inverse distributive law to see if things simplify.  */
6577
6578       if (GET_CODE (op0) == AND)
6579         {
6580           rtx result = distribute_and_simplify_rtx (x, 0);
6581           if (result)
6582             return result;
6583         }
6584
6585       if (GET_CODE (op1) == AND)
6586         {
6587           rtx result = distribute_and_simplify_rtx (x, 1);
6588           if (result)
6589             return result;
6590         }
6591       break;
6592
6593     default:
6594       gcc_unreachable ();
6595     }
6596
6597   return x;
6598 }
6599 \f
6600 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6601    operations" because they can be replaced with two more basic operations.
6602    ZERO_EXTEND is also considered "compound" because it can be replaced with
6603    an AND operation, which is simpler, though only one operation.
6604
6605    The function expand_compound_operation is called with an rtx expression
6606    and will convert it to the appropriate shifts and AND operations,
6607    simplifying at each stage.
6608
6609    The function make_compound_operation is called to convert an expression
6610    consisting of shifts and ANDs into the equivalent compound expression.
6611    It is the inverse of this function, loosely speaking.  */
6612
6613 static rtx
6614 expand_compound_operation (rtx x)
6615 {
6616   unsigned HOST_WIDE_INT pos = 0, len;
6617   int unsignedp = 0;
6618   unsigned int modewidth;
6619   rtx tem;
6620
6621   switch (GET_CODE (x))
6622     {
6623     case ZERO_EXTEND:
6624       unsignedp = 1;
6625     case SIGN_EXTEND:
6626       /* We can't necessarily use a const_int for a multiword mode;
6627          it depends on implicitly extending the value.
6628          Since we don't know the right way to extend it,
6629          we can't tell whether the implicit way is right.
6630
6631          Even for a mode that is no wider than a const_int,
6632          we can't win, because we need to sign extend one of its bits through
6633          the rest of it, and we don't know which bit.  */
6634       if (CONST_INT_P (XEXP (x, 0)))
6635         return x;
6636
6637       /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6638          (zero_extend:MODE FROM) or (sign_extend:MODE FROM).  It is for any MEM
6639          because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6640          reloaded. If not for that, MEM's would very rarely be safe.
6641
6642          Reject MODEs bigger than a word, because we might not be able
6643          to reference a two-register group starting with an arbitrary register
6644          (and currently gen_lowpart might crash for a SUBREG).  */
6645
6646       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6647         return x;
6648
6649       /* Reject MODEs that aren't scalar integers because turning vector
6650          or complex modes into shifts causes problems.  */
6651
6652       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6653         return x;
6654
6655       len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
6656       /* If the inner object has VOIDmode (the only way this can happen
6657          is if it is an ASM_OPERANDS), we can't do anything since we don't
6658          know how much masking to do.  */
6659       if (len == 0)
6660         return x;
6661
6662       break;
6663
6664     case ZERO_EXTRACT:
6665       unsignedp = 1;
6666
6667       /* ... fall through ...  */
6668
6669     case SIGN_EXTRACT:
6670       /* If the operand is a CLOBBER, just return it.  */
6671       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6672         return XEXP (x, 0);
6673
6674       if (!CONST_INT_P (XEXP (x, 1))
6675           || !CONST_INT_P (XEXP (x, 2))
6676           || GET_MODE (XEXP (x, 0)) == VOIDmode)
6677         return x;
6678
6679       /* Reject MODEs that aren't scalar integers because turning vector
6680          or complex modes into shifts causes problems.  */
6681
6682       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6683         return x;
6684
6685       len = INTVAL (XEXP (x, 1));
6686       pos = INTVAL (XEXP (x, 2));
6687
6688       /* This should stay within the object being extracted, fail otherwise.  */
6689       if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
6690         return x;
6691
6692       if (BITS_BIG_ENDIAN)
6693         pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
6694
6695       break;
6696
6697     default:
6698       return x;
6699     }
6700   /* Convert sign extension to zero extension, if we know that the high
6701      bit is not set, as this is easier to optimize.  It will be converted
6702      back to cheaper alternative in make_extraction.  */
6703   if (GET_CODE (x) == SIGN_EXTEND
6704       && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6705           && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6706                 & ~(((unsigned HOST_WIDE_INT)
6707                       GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6708                      >> 1))
6709                == 0)))
6710     {
6711       rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6712       rtx temp2 = expand_compound_operation (temp);
6713
6714       /* Make sure this is a profitable operation.  */
6715       if (rtx_cost (x, SET, optimize_this_for_speed_p)
6716           > rtx_cost (temp2, SET, optimize_this_for_speed_p))
6717        return temp2;
6718       else if (rtx_cost (x, SET, optimize_this_for_speed_p)
6719                > rtx_cost (temp, SET, optimize_this_for_speed_p))
6720        return temp;
6721       else
6722        return x;
6723     }
6724
6725   /* We can optimize some special cases of ZERO_EXTEND.  */
6726   if (GET_CODE (x) == ZERO_EXTEND)
6727     {
6728       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6729          know that the last value didn't have any inappropriate bits
6730          set.  */
6731       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6732           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6733           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6734           && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6735               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6736         return XEXP (XEXP (x, 0), 0);
6737
6738       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
6739       if (GET_CODE (XEXP (x, 0)) == SUBREG
6740           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6741           && subreg_lowpart_p (XEXP (x, 0))
6742           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6743           && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6744               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6745         return SUBREG_REG (XEXP (x, 0));
6746
6747       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6748          is a comparison and STORE_FLAG_VALUE permits.  This is like
6749          the first case, but it works even when GET_MODE (x) is larger
6750          than HOST_WIDE_INT.  */
6751       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6752           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6753           && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6754           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6755               <= HOST_BITS_PER_WIDE_INT)
6756           && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6757         return XEXP (XEXP (x, 0), 0);
6758
6759       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
6760       if (GET_CODE (XEXP (x, 0)) == SUBREG
6761           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6762           && subreg_lowpart_p (XEXP (x, 0))
6763           && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6764           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6765               <= HOST_BITS_PER_WIDE_INT)
6766           && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6767         return SUBREG_REG (XEXP (x, 0));
6768
6769     }
6770
6771   /* If we reach here, we want to return a pair of shifts.  The inner
6772      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
6773      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
6774      logical depending on the value of UNSIGNEDP.
6775
6776      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6777      converted into an AND of a shift.
6778
6779      We must check for the case where the left shift would have a negative
6780      count.  This can happen in a case like (x >> 31) & 255 on machines
6781      that can't shift by a constant.  On those machines, we would first
6782      combine the shift with the AND to produce a variable-position
6783      extraction.  Then the constant of 31 would be substituted in
6784      to produce such a position.  */
6785
6786   modewidth = GET_MODE_BITSIZE (GET_MODE (x));
6787   if (modewidth >= pos + len)
6788     {
6789       enum machine_mode mode = GET_MODE (x);
6790       tem = gen_lowpart (mode, XEXP (x, 0));
6791       if (!tem || GET_CODE (tem) == CLOBBER)
6792         return x;
6793       tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6794                                   tem, modewidth - pos - len);
6795       tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6796                                   mode, tem, modewidth - len);
6797     }
6798   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6799     tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6800                                   simplify_shift_const (NULL_RTX, LSHIFTRT,
6801                                                         GET_MODE (x),
6802                                                         XEXP (x, 0), pos),
6803                                   ((unsigned HOST_WIDE_INT) 1 << len) - 1);
6804   else
6805     /* Any other cases we can't handle.  */
6806     return x;
6807
6808   /* If we couldn't do this for some reason, return the original
6809      expression.  */
6810   if (GET_CODE (tem) == CLOBBER)
6811     return x;
6812
6813   return tem;
6814 }
6815 \f
6816 /* X is a SET which contains an assignment of one object into
6817    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6818    or certain SUBREGS). If possible, convert it into a series of
6819    logical operations.
6820
6821    We half-heartedly support variable positions, but do not at all
6822    support variable lengths.  */
6823
6824 static const_rtx
6825 expand_field_assignment (const_rtx x)
6826 {
6827   rtx inner;
6828   rtx pos;                      /* Always counts from low bit.  */
6829   int len;
6830   rtx mask, cleared, masked;
6831   enum machine_mode compute_mode;
6832
6833   /* Loop until we find something we can't simplify.  */
6834   while (1)
6835     {
6836       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6837           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6838         {
6839           inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6840           len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
6841           pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6842         }
6843       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6844                && CONST_INT_P (XEXP (SET_DEST (x), 1)))
6845         {
6846           inner = XEXP (SET_DEST (x), 0);
6847           len = INTVAL (XEXP (SET_DEST (x), 1));
6848           pos = XEXP (SET_DEST (x), 2);
6849
6850           /* A constant position should stay within the width of INNER.  */
6851           if (CONST_INT_P (pos)
6852               && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
6853             break;
6854
6855           if (BITS_BIG_ENDIAN)
6856             {
6857               if (CONST_INT_P (pos))
6858                 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
6859                                - INTVAL (pos));
6860               else if (GET_CODE (pos) == MINUS
6861                        && CONST_INT_P (XEXP (pos, 1))
6862                        && (INTVAL (XEXP (pos, 1))
6863                            == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
6864                 /* If position is ADJUST - X, new position is X.  */
6865                 pos = XEXP (pos, 0);
6866               else
6867                 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6868                                            GEN_INT (GET_MODE_BITSIZE (
6869                                                     GET_MODE (inner))
6870                                                     - len),
6871                                            pos);
6872             }
6873         }
6874
6875       /* A SUBREG between two modes that occupy the same numbers of words
6876          can be done by moving the SUBREG to the source.  */
6877       else if (GET_CODE (SET_DEST (x)) == SUBREG
6878                /* We need SUBREGs to compute nonzero_bits properly.  */
6879                && nonzero_sign_valid
6880                && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6881                      + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6882                    == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6883                         + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6884         {
6885           x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6886                            gen_lowpart
6887                            (GET_MODE (SUBREG_REG (SET_DEST (x))),
6888                             SET_SRC (x)));
6889           continue;
6890         }
6891       else
6892         break;
6893
6894       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6895         inner = SUBREG_REG (inner);
6896
6897       compute_mode = GET_MODE (inner);
6898
6899       /* Don't attempt bitwise arithmetic on non scalar integer modes.  */
6900       if (! SCALAR_INT_MODE_P (compute_mode))
6901         {
6902           enum machine_mode imode;
6903
6904           /* Don't do anything for vector or complex integral types.  */
6905           if (! FLOAT_MODE_P (compute_mode))
6906             break;
6907
6908           /* Try to find an integral mode to pun with.  */
6909           imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6910           if (imode == BLKmode)
6911             break;
6912
6913           compute_mode = imode;
6914           inner = gen_lowpart (imode, inner);
6915         }
6916
6917       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
6918       if (len >= HOST_BITS_PER_WIDE_INT)
6919         break;
6920
6921       /* Now compute the equivalent expression.  Make a copy of INNER
6922          for the SET_DEST in case it is a MEM into which we will substitute;
6923          we don't want shared RTL in that case.  */
6924       mask = GEN_INT (((unsigned HOST_WIDE_INT) 1 << len) - 1);
6925       cleared = simplify_gen_binary (AND, compute_mode,
6926                                      simplify_gen_unary (NOT, compute_mode,
6927                                        simplify_gen_binary (ASHIFT,
6928                                                             compute_mode,
6929                                                             mask, pos),
6930                                        compute_mode),
6931                                      inner);
6932       masked = simplify_gen_binary (ASHIFT, compute_mode,
6933                                     simplify_gen_binary (
6934                                       AND, compute_mode,
6935                                       gen_lowpart (compute_mode, SET_SRC (x)),
6936                                       mask),
6937                                     pos);
6938
6939       x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
6940                        simplify_gen_binary (IOR, compute_mode,
6941                                             cleared, masked));
6942     }
6943
6944   return x;
6945 }
6946 \f
6947 /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
6948    it is an RTX that represents a variable starting position; otherwise,
6949    POS is the (constant) starting bit position (counted from the LSB).
6950
6951    UNSIGNEDP is nonzero for an unsigned reference and zero for a
6952    signed reference.
6953
6954    IN_DEST is nonzero if this is a reference in the destination of a
6955    SET.  This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If nonzero,
6956    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6957    be used.
6958
6959    IN_COMPARE is nonzero if we are in a COMPARE.  This means that a
6960    ZERO_EXTRACT should be built even for bits starting at bit 0.
6961
6962    MODE is the desired mode of the result (if IN_DEST == 0).
6963
6964    The result is an RTX for the extraction or NULL_RTX if the target
6965    can't handle it.  */
6966
6967 static rtx
6968 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6969                  rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6970                  int in_dest, int in_compare)
6971 {
6972   /* This mode describes the size of the storage area
6973      to fetch the overall value from.  Within that, we
6974      ignore the POS lowest bits, etc.  */
6975   enum machine_mode is_mode = GET_MODE (inner);
6976   enum machine_mode inner_mode;
6977   enum machine_mode wanted_inner_mode;
6978   enum machine_mode wanted_inner_reg_mode = word_mode;
6979   enum machine_mode pos_mode = word_mode;
6980   enum machine_mode extraction_mode = word_mode;
6981   enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6982   rtx new_rtx = 0;
6983   rtx orig_pos_rtx = pos_rtx;
6984   HOST_WIDE_INT orig_pos;
6985
6986   if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6987     {
6988       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6989          consider just the QI as the memory to extract from.
6990          The subreg adds or removes high bits; its mode is
6991          irrelevant to the meaning of this extraction,
6992          since POS and LEN count from the lsb.  */
6993       if (MEM_P (SUBREG_REG (inner)))
6994         is_mode = GET_MODE (SUBREG_REG (inner));
6995       inner = SUBREG_REG (inner);
6996     }
6997   else if (GET_CODE (inner) == ASHIFT
6998            && CONST_INT_P (XEXP (inner, 1))
6999            && pos_rtx == 0 && pos == 0
7000            && len > UINTVAL (XEXP (inner, 1)))
7001     {
7002       /* We're extracting the least significant bits of an rtx
7003          (ashift X (const_int C)), where LEN > C.  Extract the
7004          least significant (LEN - C) bits of X, giving an rtx
7005          whose mode is MODE, then shift it left C times.  */
7006       new_rtx = make_extraction (mode, XEXP (inner, 0),
7007                              0, 0, len - INTVAL (XEXP (inner, 1)),
7008                              unsignedp, in_dest, in_compare);
7009       if (new_rtx != 0)
7010         return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7011     }
7012
7013   inner_mode = GET_MODE (inner);
7014
7015   if (pos_rtx && CONST_INT_P (pos_rtx))
7016     pos = INTVAL (pos_rtx), pos_rtx = 0;
7017
7018   /* See if this can be done without an extraction.  We never can if the
7019      width of the field is not the same as that of some integer mode. For
7020      registers, we can only avoid the extraction if the position is at the
7021      low-order bit and this is either not in the destination or we have the
7022      appropriate STRICT_LOW_PART operation available.
7023
7024      For MEM, we can avoid an extract if the field starts on an appropriate
7025      boundary and we can change the mode of the memory reference.  */
7026
7027   if (tmode != BLKmode
7028       && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7029            && !MEM_P (inner)
7030            && (inner_mode == tmode
7031                || !REG_P (inner)
7032                || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
7033                                          GET_MODE_BITSIZE (inner_mode))
7034                || reg_truncated_to_mode (tmode, inner))
7035            && (! in_dest
7036                || (REG_P (inner)
7037                    && have_insn_for (STRICT_LOW_PART, tmode))))
7038           || (MEM_P (inner) && pos_rtx == 0
7039               && (pos
7040                   % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7041                      : BITS_PER_UNIT)) == 0
7042               /* We can't do this if we are widening INNER_MODE (it
7043                  may not be aligned, for one thing).  */
7044               && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
7045               && (inner_mode == tmode
7046                   || (! mode_dependent_address_p (XEXP (inner, 0))
7047                       && ! MEM_VOLATILE_P (inner))))))
7048     {
7049       /* If INNER is a MEM, make a new MEM that encompasses just the desired
7050          field.  If the original and current mode are the same, we need not
7051          adjust the offset.  Otherwise, we do if bytes big endian.
7052
7053          If INNER is not a MEM, get a piece consisting of just the field
7054          of interest (in this case POS % BITS_PER_WORD must be 0).  */
7055
7056       if (MEM_P (inner))
7057         {
7058           HOST_WIDE_INT offset;
7059
7060           /* POS counts from lsb, but make OFFSET count in memory order.  */
7061           if (BYTES_BIG_ENDIAN)
7062             offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
7063           else
7064             offset = pos / BITS_PER_UNIT;
7065
7066           new_rtx = adjust_address_nv (inner, tmode, offset);
7067         }
7068       else if (REG_P (inner))
7069         {
7070           if (tmode != inner_mode)
7071             {
7072               /* We can't call gen_lowpart in a DEST since we
7073                  always want a SUBREG (see below) and it would sometimes
7074                  return a new hard register.  */
7075               if (pos || in_dest)
7076                 {
7077                   HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7078
7079                   if (WORDS_BIG_ENDIAN
7080                       && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7081                     final_word = ((GET_MODE_SIZE (inner_mode)
7082                                    - GET_MODE_SIZE (tmode))
7083                                   / UNITS_PER_WORD) - final_word;
7084
7085                   final_word *= UNITS_PER_WORD;
7086                   if (BYTES_BIG_ENDIAN &&
7087                       GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7088                     final_word += (GET_MODE_SIZE (inner_mode)
7089                                    - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7090
7091                   /* Avoid creating invalid subregs, for example when
7092                      simplifying (x>>32)&255.  */
7093                   if (!validate_subreg (tmode, inner_mode, inner, final_word))
7094                     return NULL_RTX;
7095
7096                   new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7097                 }
7098               else
7099                 new_rtx = gen_lowpart (tmode, inner);
7100             }
7101           else
7102             new_rtx = inner;
7103         }
7104       else
7105         new_rtx = force_to_mode (inner, tmode,
7106                              len >= HOST_BITS_PER_WIDE_INT
7107                              ? ~(unsigned HOST_WIDE_INT) 0
7108                              : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7109                              0);
7110
7111       /* If this extraction is going into the destination of a SET,
7112          make a STRICT_LOW_PART unless we made a MEM.  */
7113
7114       if (in_dest)
7115         return (MEM_P (new_rtx) ? new_rtx
7116                 : (GET_CODE (new_rtx) != SUBREG
7117                    ? gen_rtx_CLOBBER (tmode, const0_rtx)
7118                    : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7119
7120       if (mode == tmode)
7121         return new_rtx;
7122
7123       if (CONST_INT_P (new_rtx)
7124           || GET_CODE (new_rtx) == CONST_DOUBLE)
7125         return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7126                                          mode, new_rtx, tmode);
7127
7128       /* If we know that no extraneous bits are set, and that the high
7129          bit is not set, convert the extraction to the cheaper of
7130          sign and zero extension, that are equivalent in these cases.  */
7131       if (flag_expensive_optimizations
7132           && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
7133               && ((nonzero_bits (new_rtx, tmode)
7134                    & ~(((unsigned HOST_WIDE_INT)
7135                         GET_MODE_MASK (tmode))
7136                        >> 1))
7137                   == 0)))
7138         {
7139           rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7140           rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7141
7142           /* Prefer ZERO_EXTENSION, since it gives more information to
7143              backends.  */
7144           if (rtx_cost (temp, SET, optimize_this_for_speed_p)
7145               <= rtx_cost (temp1, SET, optimize_this_for_speed_p))
7146             return temp;
7147           return temp1;
7148         }
7149
7150       /* Otherwise, sign- or zero-extend unless we already are in the
7151          proper mode.  */
7152
7153       return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7154                              mode, new_rtx));
7155     }
7156
7157   /* Unless this is a COMPARE or we have a funny memory reference,
7158      don't do anything with zero-extending field extracts starting at
7159      the low-order bit since they are simple AND operations.  */
7160   if (pos_rtx == 0 && pos == 0 && ! in_dest
7161       && ! in_compare && unsignedp)
7162     return 0;
7163
7164   /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7165      if the position is not a constant and the length is not 1.  In all
7166      other cases, we would only be going outside our object in cases when
7167      an original shift would have been undefined.  */
7168   if (MEM_P (inner)
7169       && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
7170           || (pos_rtx != 0 && len != 1)))
7171     return 0;
7172
7173   /* Get the mode to use should INNER not be a MEM, the mode for the position,
7174      and the mode for the result.  */
7175   if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
7176     {
7177       wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
7178       pos_mode = mode_for_extraction (EP_insv, 2);
7179       extraction_mode = mode_for_extraction (EP_insv, 3);
7180     }
7181
7182   if (! in_dest && unsignedp
7183       && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
7184     {
7185       wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
7186       pos_mode = mode_for_extraction (EP_extzv, 3);
7187       extraction_mode = mode_for_extraction (EP_extzv, 0);
7188     }
7189
7190   if (! in_dest && ! unsignedp
7191       && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
7192     {
7193       wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
7194       pos_mode = mode_for_extraction (EP_extv, 3);
7195       extraction_mode = mode_for_extraction (EP_extv, 0);
7196     }
7197
7198   /* Never narrow an object, since that might not be safe.  */
7199
7200   if (mode != VOIDmode
7201       && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7202     extraction_mode = mode;
7203
7204   if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
7205       && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
7206     pos_mode = GET_MODE (pos_rtx);
7207
7208   /* If this is not from memory, the desired mode is the preferred mode
7209      for an extraction pattern's first input operand, or word_mode if there
7210      is none.  */
7211   if (!MEM_P (inner))
7212     wanted_inner_mode = wanted_inner_reg_mode;
7213   else
7214     {
7215       /* Be careful not to go beyond the extracted object and maintain the
7216          natural alignment of the memory.  */
7217       wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7218       while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7219              > GET_MODE_BITSIZE (wanted_inner_mode))
7220         {
7221           wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7222           gcc_assert (wanted_inner_mode != VOIDmode);
7223         }
7224
7225       /* If we have to change the mode of memory and cannot, the desired mode
7226          is EXTRACTION_MODE.  */
7227       if (inner_mode != wanted_inner_mode
7228           && (mode_dependent_address_p (XEXP (inner, 0))
7229               || MEM_VOLATILE_P (inner)
7230               || pos_rtx))
7231         wanted_inner_mode = extraction_mode;
7232     }
7233
7234   orig_pos = pos;
7235
7236   if (BITS_BIG_ENDIAN)
7237     {
7238       /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7239          BITS_BIG_ENDIAN style.  If position is constant, compute new
7240          position.  Otherwise, build subtraction.
7241          Note that POS is relative to the mode of the original argument.
7242          If it's a MEM we need to recompute POS relative to that.
7243          However, if we're extracting from (or inserting into) a register,
7244          we want to recompute POS relative to wanted_inner_mode.  */
7245       int width = (MEM_P (inner)
7246                    ? GET_MODE_BITSIZE (is_mode)
7247                    : GET_MODE_BITSIZE (wanted_inner_mode));
7248
7249       if (pos_rtx == 0)
7250         pos = width - len - pos;
7251       else
7252         pos_rtx
7253           = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
7254       /* POS may be less than 0 now, but we check for that below.
7255          Note that it can only be less than 0 if !MEM_P (inner).  */
7256     }
7257
7258   /* If INNER has a wider mode, and this is a constant extraction, try to
7259      make it smaller and adjust the byte to point to the byte containing
7260      the value.  */
7261   if (wanted_inner_mode != VOIDmode
7262       && inner_mode != wanted_inner_mode
7263       && ! pos_rtx
7264       && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7265       && MEM_P (inner)
7266       && ! mode_dependent_address_p (XEXP (inner, 0))
7267       && ! MEM_VOLATILE_P (inner))
7268     {
7269       int offset = 0;
7270
7271       /* The computations below will be correct if the machine is big
7272          endian in both bits and bytes or little endian in bits and bytes.
7273          If it is mixed, we must adjust.  */
7274
7275       /* If bytes are big endian and we had a paradoxical SUBREG, we must
7276          adjust OFFSET to compensate.  */
7277       if (BYTES_BIG_ENDIAN
7278           && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7279         offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7280
7281       /* We can now move to the desired byte.  */
7282       offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7283                 * GET_MODE_SIZE (wanted_inner_mode);
7284       pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7285
7286       if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7287           && is_mode != wanted_inner_mode)
7288         offset = (GET_MODE_SIZE (is_mode)
7289                   - GET_MODE_SIZE (wanted_inner_mode) - offset);
7290
7291       inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7292     }
7293
7294   /* If INNER is not memory, get it into the proper mode.  If we are changing
7295      its mode, POS must be a constant and smaller than the size of the new
7296      mode.  */
7297   else if (!MEM_P (inner))
7298     {
7299       /* On the LHS, don't create paradoxical subregs implicitely truncating
7300          the register unless TRULY_NOOP_TRUNCATION.  */
7301       if (in_dest
7302           && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (inner)),
7303                                      GET_MODE_BITSIZE (wanted_inner_mode)))
7304         return NULL_RTX;
7305
7306       if (GET_MODE (inner) != wanted_inner_mode
7307           && (pos_rtx != 0
7308               || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7309         return NULL_RTX;
7310
7311       if (orig_pos < 0)
7312         return NULL_RTX;
7313
7314       inner = force_to_mode (inner, wanted_inner_mode,
7315                              pos_rtx
7316                              || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7317                              ? ~(unsigned HOST_WIDE_INT) 0
7318                              : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7319                                 << orig_pos),
7320                              0);
7321     }
7322
7323   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
7324      have to zero extend.  Otherwise, we can just use a SUBREG.  */
7325   if (pos_rtx != 0
7326       && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7327     {
7328       rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
7329
7330       /* If we know that no extraneous bits are set, and that the high
7331          bit is not set, convert extraction to cheaper one - either
7332          SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7333          cases.  */
7334       if (flag_expensive_optimizations
7335           && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
7336               && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7337                    & ~(((unsigned HOST_WIDE_INT)
7338                         GET_MODE_MASK (GET_MODE (pos_rtx)))
7339                        >> 1))
7340                   == 0)))
7341         {
7342           rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
7343
7344           /* Prefer ZERO_EXTENSION, since it gives more information to
7345              backends.  */
7346           if (rtx_cost (temp1, SET, optimize_this_for_speed_p)
7347               < rtx_cost (temp, SET, optimize_this_for_speed_p))
7348             temp = temp1;
7349         }
7350       pos_rtx = temp;
7351     }
7352   else if (pos_rtx != 0
7353            && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
7354     pos_rtx = gen_lowpart (pos_mode, pos_rtx);
7355
7356   /* Make POS_RTX unless we already have it and it is correct.  If we don't
7357      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7358      be a CONST_INT.  */
7359   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7360     pos_rtx = orig_pos_rtx;
7361
7362   else if (pos_rtx == 0)
7363     pos_rtx = GEN_INT (pos);
7364
7365   /* Make the required operation.  See if we can use existing rtx.  */
7366   new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7367                          extraction_mode, inner, GEN_INT (len), pos_rtx);
7368   if (! in_dest)
7369     new_rtx = gen_lowpart (mode, new_rtx);
7370
7371   return new_rtx;
7372 }
7373 \f
7374 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7375    with any other operations in X.  Return X without that shift if so.  */
7376
7377 static rtx
7378 extract_left_shift (rtx x, int count)
7379 {
7380   enum rtx_code code = GET_CODE (x);
7381   enum machine_mode mode = GET_MODE (x);
7382   rtx tem;
7383
7384   switch (code)
7385     {
7386     case ASHIFT:
7387       /* This is the shift itself.  If it is wide enough, we will return
7388          either the value being shifted if the shift count is equal to
7389          COUNT or a shift for the difference.  */
7390       if (CONST_INT_P (XEXP (x, 1))
7391           && INTVAL (XEXP (x, 1)) >= count)
7392         return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7393                                      INTVAL (XEXP (x, 1)) - count);
7394       break;
7395
7396     case NEG:  case NOT:
7397       if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7398         return simplify_gen_unary (code, mode, tem, mode);
7399
7400       break;
7401
7402     case PLUS:  case IOR:  case XOR:  case AND:
7403       /* If we can safely shift this constant and we find the inner shift,
7404          make a new operation.  */
7405       if (CONST_INT_P (XEXP (x, 1))
7406           && (UINTVAL (XEXP (x, 1))
7407               & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7408           && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7409         return simplify_gen_binary (code, mode, tem,
7410                                     GEN_INT (INTVAL (XEXP (x, 1)) >> count));
7411
7412       break;
7413
7414     default:
7415       break;
7416     }
7417
7418   return 0;
7419 }
7420 \f
7421 /* Look at the expression rooted at X.  Look for expressions
7422    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7423    Form these expressions.
7424
7425    Return the new rtx, usually just X.
7426
7427    Also, for machines like the VAX that don't have logical shift insns,
7428    try to convert logical to arithmetic shift operations in cases where
7429    they are equivalent.  This undoes the canonicalizations to logical
7430    shifts done elsewhere.
7431
7432    We try, as much as possible, to re-use rtl expressions to save memory.
7433
7434    IN_CODE says what kind of expression we are processing.  Normally, it is
7435    SET.  In a memory address (inside a MEM, PLUS or minus, the latter two
7436    being kludges), it is MEM.  When processing the arguments of a comparison
7437    or a COMPARE against zero, it is COMPARE.  */
7438
7439 static rtx
7440 make_compound_operation (rtx x, enum rtx_code in_code)
7441 {
7442   enum rtx_code code = GET_CODE (x);
7443   enum machine_mode mode = GET_MODE (x);
7444   int mode_width = GET_MODE_BITSIZE (mode);
7445   rtx rhs, lhs;
7446   enum rtx_code next_code;
7447   int i, j;
7448   rtx new_rtx = 0;
7449   rtx tem;
7450   const char *fmt;
7451
7452   /* Select the code to be used in recursive calls.  Once we are inside an
7453      address, we stay there.  If we have a comparison, set to COMPARE,
7454      but once inside, go back to our default of SET.  */
7455
7456   next_code = (code == MEM ? MEM
7457                : ((code == PLUS || code == MINUS)
7458                   && SCALAR_INT_MODE_P (mode)) ? MEM
7459                : ((code == COMPARE || COMPARISON_P (x))
7460                   && XEXP (x, 1) == const0_rtx) ? COMPARE
7461                : in_code == COMPARE ? SET : in_code);
7462
7463   /* Process depending on the code of this operation.  If NEW is set
7464      nonzero, it will be returned.  */
7465
7466   switch (code)
7467     {
7468     case ASHIFT:
7469       /* Convert shifts by constants into multiplications if inside
7470          an address.  */
7471       if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7472           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7473           && INTVAL (XEXP (x, 1)) >= 0)
7474         {
7475           HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7476           HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7477
7478           new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7479           if (GET_CODE (new_rtx) == NEG)
7480             {
7481               new_rtx = XEXP (new_rtx, 0);
7482               multval = -multval;
7483             }
7484           multval = trunc_int_for_mode (multval, mode);
7485           new_rtx = gen_rtx_MULT (mode, new_rtx, GEN_INT (multval));
7486         }
7487       break;
7488
7489     case PLUS:
7490       lhs = XEXP (x, 0);
7491       rhs = XEXP (x, 1);
7492       lhs = make_compound_operation (lhs, next_code);
7493       rhs = make_compound_operation (rhs, next_code);
7494       if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7495           && SCALAR_INT_MODE_P (mode))
7496         {
7497           tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7498                                      XEXP (lhs, 1));
7499           new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7500         }
7501       else if (GET_CODE (lhs) == MULT
7502                && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7503         {
7504           tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7505                                      simplify_gen_unary (NEG, mode,
7506                                                          XEXP (lhs, 1),
7507                                                          mode));
7508           new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7509         }
7510       else
7511         {
7512           SUBST (XEXP (x, 0), lhs);
7513           SUBST (XEXP (x, 1), rhs);
7514           goto maybe_swap;
7515         }
7516       x = gen_lowpart (mode, new_rtx);
7517       goto maybe_swap;
7518
7519     case MINUS:
7520       lhs = XEXP (x, 0);
7521       rhs = XEXP (x, 1);
7522       lhs = make_compound_operation (lhs, next_code);
7523       rhs = make_compound_operation (rhs, next_code);
7524       if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7525           && SCALAR_INT_MODE_P (mode))
7526         {
7527           tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7528                                      XEXP (rhs, 1));
7529           new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7530         }
7531       else if (GET_CODE (rhs) == MULT
7532                && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7533         {
7534           tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7535                                      simplify_gen_unary (NEG, mode,
7536                                                          XEXP (rhs, 1),
7537                                                          mode));
7538           new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7539         }
7540       else
7541         {
7542           SUBST (XEXP (x, 0), lhs);
7543           SUBST (XEXP (x, 1), rhs);
7544           return x;
7545         }
7546       return gen_lowpart (mode, new_rtx);
7547
7548     case AND:
7549       /* If the second operand is not a constant, we can't do anything
7550          with it.  */
7551       if (!CONST_INT_P (XEXP (x, 1)))
7552         break;
7553
7554       /* If the constant is a power of two minus one and the first operand
7555          is a logical right shift, make an extraction.  */
7556       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7557           && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7558         {
7559           new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7560           new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7561                                  0, in_code == COMPARE);
7562         }
7563
7564       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
7565       else if (GET_CODE (XEXP (x, 0)) == SUBREG
7566                && subreg_lowpart_p (XEXP (x, 0))
7567                && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7568                && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7569         {
7570           new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7571                                          next_code);
7572           new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7573                                  XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7574                                  0, in_code == COMPARE);
7575         }
7576       /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)).  */
7577       else if ((GET_CODE (XEXP (x, 0)) == XOR
7578                 || GET_CODE (XEXP (x, 0)) == IOR)
7579                && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7580                && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7581                && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7582         {
7583           /* Apply the distributive law, and then try to make extractions.  */
7584           new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7585                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7586                                              XEXP (x, 1)),
7587                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7588                                              XEXP (x, 1)));
7589           new_rtx = make_compound_operation (new_rtx, in_code);
7590         }
7591
7592       /* If we are have (and (rotate X C) M) and C is larger than the number
7593          of bits in M, this is an extraction.  */
7594
7595       else if (GET_CODE (XEXP (x, 0)) == ROTATE
7596                && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7597                && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7598                && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7599         {
7600           new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7601           new_rtx = make_extraction (mode, new_rtx,
7602                                  (GET_MODE_BITSIZE (mode)
7603                                   - INTVAL (XEXP (XEXP (x, 0), 1))),
7604                                  NULL_RTX, i, 1, 0, in_code == COMPARE);
7605         }
7606
7607       /* On machines without logical shifts, if the operand of the AND is
7608          a logical shift and our mask turns off all the propagated sign
7609          bits, we can replace the logical shift with an arithmetic shift.  */
7610       else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7611                && !have_insn_for (LSHIFTRT, mode)
7612                && have_insn_for (ASHIFTRT, mode)
7613                && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7614                && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7615                && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7616                && mode_width <= HOST_BITS_PER_WIDE_INT)
7617         {
7618           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7619
7620           mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7621           if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7622             SUBST (XEXP (x, 0),
7623                    gen_rtx_ASHIFTRT (mode,
7624                                      make_compound_operation
7625                                      (XEXP (XEXP (x, 0), 0), next_code),
7626                                      XEXP (XEXP (x, 0), 1)));
7627         }
7628
7629       /* If the constant is one less than a power of two, this might be
7630          representable by an extraction even if no shift is present.
7631          If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7632          we are in a COMPARE.  */
7633       else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7634         new_rtx = make_extraction (mode,
7635                                make_compound_operation (XEXP (x, 0),
7636                                                         next_code),
7637                                0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7638
7639       /* If we are in a comparison and this is an AND with a power of two,
7640          convert this into the appropriate bit extract.  */
7641       else if (in_code == COMPARE
7642                && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7643         new_rtx = make_extraction (mode,
7644                                make_compound_operation (XEXP (x, 0),
7645                                                         next_code),
7646                                i, NULL_RTX, 1, 1, 0, 1);
7647
7648       break;
7649
7650     case LSHIFTRT:
7651       /* If the sign bit is known to be zero, replace this with an
7652          arithmetic shift.  */
7653       if (have_insn_for (ASHIFTRT, mode)
7654           && ! have_insn_for (LSHIFTRT, mode)
7655           && mode_width <= HOST_BITS_PER_WIDE_INT
7656           && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7657         {
7658           new_rtx = gen_rtx_ASHIFTRT (mode,
7659                                   make_compound_operation (XEXP (x, 0),
7660                                                            next_code),
7661                                   XEXP (x, 1));
7662           break;
7663         }
7664
7665       /* ... fall through ...  */
7666
7667     case ASHIFTRT:
7668       lhs = XEXP (x, 0);
7669       rhs = XEXP (x, 1);
7670
7671       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7672          this is a SIGN_EXTRACT.  */
7673       if (CONST_INT_P (rhs)
7674           && GET_CODE (lhs) == ASHIFT
7675           && CONST_INT_P (XEXP (lhs, 1))
7676           && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7677           && INTVAL (rhs) < mode_width)
7678         {
7679           new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7680           new_rtx = make_extraction (mode, new_rtx,
7681                                  INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7682                                  NULL_RTX, mode_width - INTVAL (rhs),
7683                                  code == LSHIFTRT, 0, in_code == COMPARE);
7684           break;
7685         }
7686
7687       /* See if we have operations between an ASHIFTRT and an ASHIFT.
7688          If so, try to merge the shifts into a SIGN_EXTEND.  We could
7689          also do this for some cases of SIGN_EXTRACT, but it doesn't
7690          seem worth the effort; the case checked for occurs on Alpha.  */
7691
7692       if (!OBJECT_P (lhs)
7693           && ! (GET_CODE (lhs) == SUBREG
7694                 && (OBJECT_P (SUBREG_REG (lhs))))
7695           && CONST_INT_P (rhs)
7696           && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7697           && INTVAL (rhs) < mode_width
7698           && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7699         new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7700                                0, NULL_RTX, mode_width - INTVAL (rhs),
7701                                code == LSHIFTRT, 0, in_code == COMPARE);
7702
7703       break;
7704
7705     case SUBREG:
7706       /* Call ourselves recursively on the inner expression.  If we are
7707          narrowing the object and it has a different RTL code from
7708          what it originally did, do this SUBREG as a force_to_mode.  */
7709       {
7710         rtx inner = SUBREG_REG (x), simplified;
7711         
7712         tem = make_compound_operation (inner, in_code);
7713
7714         simplified
7715           = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
7716         if (simplified)
7717           tem = simplified;
7718
7719         if (GET_CODE (tem) != GET_CODE (inner)
7720             && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7721             && subreg_lowpart_p (x))
7722           {
7723             rtx newer
7724               = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
7725
7726             /* If we have something other than a SUBREG, we might have
7727                done an expansion, so rerun ourselves.  */
7728             if (GET_CODE (newer) != SUBREG)
7729               newer = make_compound_operation (newer, in_code);
7730
7731             /* force_to_mode can expand compounds.  If it just re-expanded the
7732                compound, use gen_lowpart to convert to the desired mode.  */
7733             if (rtx_equal_p (newer, x)
7734                 /* Likewise if it re-expanded the compound only partially.
7735                    This happens for SUBREG of ZERO_EXTRACT if they extract
7736                    the same number of bits.  */
7737                 || (GET_CODE (newer) == SUBREG
7738                     && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
7739                         || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
7740                     && GET_CODE (inner) == AND
7741                     && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
7742               return gen_lowpart (GET_MODE (x), tem);
7743
7744             return newer;
7745           }
7746
7747         if (simplified)
7748           return tem;
7749       }
7750       break;
7751
7752     default:
7753       break;
7754     }
7755
7756   if (new_rtx)
7757     {
7758       x = gen_lowpart (mode, new_rtx);
7759       code = GET_CODE (x);
7760     }
7761
7762   /* Now recursively process each operand of this operation.  */
7763   fmt = GET_RTX_FORMAT (code);
7764   for (i = 0; i < GET_RTX_LENGTH (code); i++)
7765     if (fmt[i] == 'e')
7766       {
7767         new_rtx = make_compound_operation (XEXP (x, i), next_code);
7768         SUBST (XEXP (x, i), new_rtx);
7769       }
7770     else if (fmt[i] == 'E')
7771       for (j = 0; j < XVECLEN (x, i); j++)
7772         {
7773           new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
7774           SUBST (XVECEXP (x, i, j), new_rtx);
7775         }
7776
7777  maybe_swap:
7778   /* If this is a commutative operation, the changes to the operands
7779      may have made it noncanonical.  */
7780   if (COMMUTATIVE_ARITH_P (x)
7781       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7782     {
7783       tem = XEXP (x, 0);
7784       SUBST (XEXP (x, 0), XEXP (x, 1));
7785       SUBST (XEXP (x, 1), tem);
7786     }
7787
7788   return x;
7789 }
7790 \f
7791 /* Given M see if it is a value that would select a field of bits
7792    within an item, but not the entire word.  Return -1 if not.
7793    Otherwise, return the starting position of the field, where 0 is the
7794    low-order bit.
7795
7796    *PLEN is set to the length of the field.  */
7797
7798 static int
7799 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7800 {
7801   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
7802   int pos = m ? ctz_hwi (m) : -1;
7803   int len = 0;
7804
7805   if (pos >= 0)
7806     /* Now shift off the low-order zero bits and see if we have a
7807        power of two minus 1.  */
7808     len = exact_log2 ((m >> pos) + 1);
7809
7810   if (len <= 0)
7811     pos = -1;
7812
7813   *plen = len;
7814   return pos;
7815 }
7816 \f
7817 /* If X refers to a register that equals REG in value, replace these
7818    references with REG.  */
7819 static rtx
7820 canon_reg_for_combine (rtx x, rtx reg)
7821 {
7822   rtx op0, op1, op2;
7823   const char *fmt;
7824   int i;
7825   bool copied;
7826
7827   enum rtx_code code = GET_CODE (x);
7828   switch (GET_RTX_CLASS (code))
7829     {
7830     case RTX_UNARY:
7831       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7832       if (op0 != XEXP (x, 0))
7833         return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7834                                    GET_MODE (reg));
7835       break;
7836
7837     case RTX_BIN_ARITH:
7838     case RTX_COMM_ARITH:
7839       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7840       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7841       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7842         return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7843       break;
7844
7845     case RTX_COMPARE:
7846     case RTX_COMM_COMPARE:
7847       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7848       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7849       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7850         return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7851                                         GET_MODE (op0), op0, op1);
7852       break;
7853
7854     case RTX_TERNARY:
7855     case RTX_BITFIELD_OPS:
7856       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7857       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7858       op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7859       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7860         return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7861                                      GET_MODE (op0), op0, op1, op2);
7862
7863     case RTX_OBJ:
7864       if (REG_P (x))
7865         {
7866           if (rtx_equal_p (get_last_value (reg), x)
7867               || rtx_equal_p (reg, get_last_value (x)))
7868             return reg;
7869           else
7870             break;
7871         }
7872
7873       /* fall through */
7874
7875     default:
7876       fmt = GET_RTX_FORMAT (code);
7877       copied = false;
7878       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7879         if (fmt[i] == 'e')
7880           {
7881             rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7882             if (op != XEXP (x, i))
7883               {
7884                 if (!copied)
7885                   {
7886                     copied = true;
7887                     x = copy_rtx (x);
7888                   }
7889                 XEXP (x, i) = op;
7890               }
7891           }
7892         else if (fmt[i] == 'E')
7893           {
7894             int j;
7895             for (j = 0; j < XVECLEN (x, i); j++)
7896               {
7897                 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
7898                 if (op != XVECEXP (x, i, j))
7899                   {
7900                     if (!copied)
7901                       {
7902                         copied = true;
7903                         x = copy_rtx (x);
7904                       }
7905                     XVECEXP (x, i, j) = op;
7906                   }
7907               }
7908           }
7909
7910       break;
7911     }
7912
7913   return x;
7914 }
7915
7916 /* Return X converted to MODE.  If the value is already truncated to
7917    MODE we can just return a subreg even though in the general case we
7918    would need an explicit truncation.  */
7919
7920 static rtx
7921 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
7922 {
7923   if (!CONST_INT_P (x)
7924       && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
7925       && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
7926                                  GET_MODE_BITSIZE (GET_MODE (x)))
7927       && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
7928     {
7929       /* Bit-cast X into an integer mode.  */
7930       if (!SCALAR_INT_MODE_P (GET_MODE (x)))
7931         x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
7932       x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
7933                               x, GET_MODE (x));
7934     }
7935
7936   return gen_lowpart (mode, x);
7937 }
7938
7939 /* See if X can be simplified knowing that we will only refer to it in
7940    MODE and will only refer to those bits that are nonzero in MASK.
7941    If other bits are being computed or if masking operations are done
7942    that select a superset of the bits in MASK, they can sometimes be
7943    ignored.
7944
7945    Return a possibly simplified expression, but always convert X to
7946    MODE.  If X is a CONST_INT, AND the CONST_INT with MASK.
7947
7948    If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
7949    are all off in X.  This is used when X will be complemented, by either
7950    NOT, NEG, or XOR.  */
7951
7952 static rtx
7953 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
7954                int just_select)
7955 {
7956   enum rtx_code code = GET_CODE (x);
7957   int next_select = just_select || code == XOR || code == NOT || code == NEG;
7958   enum machine_mode op_mode;
7959   unsigned HOST_WIDE_INT fuller_mask, nonzero;
7960   rtx op0, op1, temp;
7961
7962   /* If this is a CALL or ASM_OPERANDS, don't do anything.  Some of the
7963      code below will do the wrong thing since the mode of such an
7964      expression is VOIDmode.
7965
7966      Also do nothing if X is a CLOBBER; this can happen if X was
7967      the return value from a call to gen_lowpart.  */
7968   if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
7969     return x;
7970
7971   /* We want to perform the operation is its present mode unless we know
7972      that the operation is valid in MODE, in which case we do the operation
7973      in MODE.  */
7974   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
7975               && have_insn_for (code, mode))
7976              ? mode : GET_MODE (x));
7977
7978   /* It is not valid to do a right-shift in a narrower mode
7979      than the one it came in with.  */
7980   if ((code == LSHIFTRT || code == ASHIFTRT)
7981       && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
7982     op_mode = GET_MODE (x);
7983
7984   /* Truncate MASK to fit OP_MODE.  */
7985   if (op_mode)
7986     mask &= GET_MODE_MASK (op_mode);
7987
7988   /* When we have an arithmetic operation, or a shift whose count we
7989      do not know, we need to assume that all bits up to the highest-order
7990      bit in MASK will be needed.  This is how we form such a mask.  */
7991   if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
7992     fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
7993   else
7994     fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
7995                    - 1);
7996
7997   /* Determine what bits of X are guaranteed to be (non)zero.  */
7998   nonzero = nonzero_bits (x, mode);
7999
8000   /* If none of the bits in X are needed, return a zero.  */
8001   if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8002     x = const0_rtx;
8003
8004   /* If X is a CONST_INT, return a new one.  Do this here since the
8005      test below will fail.  */
8006   if (CONST_INT_P (x))
8007     {
8008       if (SCALAR_INT_MODE_P (mode))
8009         return gen_int_mode (INTVAL (x) & mask, mode);
8010       else
8011         {
8012           x = GEN_INT (INTVAL (x) & mask);
8013           return gen_lowpart_common (mode, x);
8014         }
8015     }
8016
8017   /* If X is narrower than MODE and we want all the bits in X's mode, just
8018      get X in the proper mode.  */
8019   if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8020       && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8021     return gen_lowpart (mode, x);
8022
8023   /* We can ignore the effect of a SUBREG if it narrows the mode or
8024      if the constant masks to zero all the bits the mode doesn't have.  */
8025   if (GET_CODE (x) == SUBREG
8026       && subreg_lowpart_p (x)
8027       && ((GET_MODE_SIZE (GET_MODE (x))
8028            < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8029           || (0 == (mask
8030                     & GET_MODE_MASK (GET_MODE (x))
8031                     & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8032     return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8033
8034   /* The arithmetic simplifications here only work for scalar integer modes.  */
8035   if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8036     return gen_lowpart_or_truncate (mode, x);
8037
8038   switch (code)
8039     {
8040     case CLOBBER:
8041       /* If X is a (clobber (const_int)), return it since we know we are
8042          generating something that won't match.  */
8043       return x;
8044
8045     case SIGN_EXTEND:
8046     case ZERO_EXTEND:
8047     case ZERO_EXTRACT:
8048     case SIGN_EXTRACT:
8049       x = expand_compound_operation (x);
8050       if (GET_CODE (x) != code)
8051         return force_to_mode (x, mode, mask, next_select);
8052       break;
8053
8054     case TRUNCATE:
8055       /* Similarly for a truncate.  */
8056       return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8057
8058     case AND:
8059       /* If this is an AND with a constant, convert it into an AND
8060          whose constant is the AND of that constant with MASK.  If it
8061          remains an AND of MASK, delete it since it is redundant.  */
8062
8063       if (CONST_INT_P (XEXP (x, 1)))
8064         {
8065           x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8066                                       mask & INTVAL (XEXP (x, 1)));
8067
8068           /* If X is still an AND, see if it is an AND with a mask that
8069              is just some low-order bits.  If so, and it is MASK, we don't
8070              need it.  */
8071
8072           if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8073               && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8074                   == mask))
8075             x = XEXP (x, 0);
8076
8077           /* If it remains an AND, try making another AND with the bits
8078              in the mode mask that aren't in MASK turned on.  If the
8079              constant in the AND is wide enough, this might make a
8080              cheaper constant.  */
8081
8082           if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8083               && GET_MODE_MASK (GET_MODE (x)) != mask
8084               && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
8085             {
8086               unsigned HOST_WIDE_INT cval
8087                 = UINTVAL (XEXP (x, 1))
8088                   | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8089               int width = GET_MODE_BITSIZE (GET_MODE (x));
8090               rtx y;
8091
8092               /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
8093                  number, sign extend it.  */
8094               if (width > 0 && width < HOST_BITS_PER_WIDE_INT
8095                   && (cval & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8096                 cval |= (unsigned HOST_WIDE_INT) -1 << width;
8097
8098               y = simplify_gen_binary (AND, GET_MODE (x),
8099                                        XEXP (x, 0), GEN_INT (cval));
8100               if (rtx_cost (y, SET, optimize_this_for_speed_p)
8101                   < rtx_cost (x, SET, optimize_this_for_speed_p))
8102                 x = y;
8103             }
8104
8105           break;
8106         }
8107
8108       goto binop;
8109
8110     case PLUS:
8111       /* In (and (plus FOO C1) M), if M is a mask that just turns off
8112          low-order bits (as in an alignment operation) and FOO is already
8113          aligned to that boundary, mask C1 to that boundary as well.
8114          This may eliminate that PLUS and, later, the AND.  */
8115
8116       {
8117         unsigned int width = GET_MODE_BITSIZE (mode);
8118         unsigned HOST_WIDE_INT smask = mask;
8119
8120         /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8121            number, sign extend it.  */
8122
8123         if (width < HOST_BITS_PER_WIDE_INT
8124             && (smask & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8125           smask |= (unsigned HOST_WIDE_INT) (-1) << width;
8126
8127         if (CONST_INT_P (XEXP (x, 1))
8128             && exact_log2 (- smask) >= 0
8129             && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8130             && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8131           return force_to_mode (plus_constant (XEXP (x, 0),
8132                                                (INTVAL (XEXP (x, 1)) & smask)),
8133                                 mode, smask, next_select);
8134       }
8135
8136       /* ... fall through ...  */
8137
8138     case MULT:
8139       /* For PLUS, MINUS and MULT, we need any bits less significant than the
8140          most significant bit in MASK since carries from those bits will
8141          affect the bits we are interested in.  */
8142       mask = fuller_mask;
8143       goto binop;
8144
8145     case MINUS:
8146       /* If X is (minus C Y) where C's least set bit is larger than any bit
8147          in the mask, then we may replace with (neg Y).  */
8148       if (CONST_INT_P (XEXP (x, 0))
8149           && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
8150                                         & -INTVAL (XEXP (x, 0))))
8151               > mask))
8152         {
8153           x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8154                                   GET_MODE (x));
8155           return force_to_mode (x, mode, mask, next_select);
8156         }
8157
8158       /* Similarly, if C contains every bit in the fuller_mask, then we may
8159          replace with (not Y).  */
8160       if (CONST_INT_P (XEXP (x, 0))
8161           && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8162         {
8163           x = simplify_gen_unary (NOT, GET_MODE (x),
8164                                   XEXP (x, 1), GET_MODE (x));
8165           return force_to_mode (x, mode, mask, next_select);
8166         }
8167
8168       mask = fuller_mask;
8169       goto binop;
8170
8171     case IOR:
8172     case XOR:
8173       /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8174          LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8175          operation which may be a bitfield extraction.  Ensure that the
8176          constant we form is not wider than the mode of X.  */
8177
8178       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8179           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8180           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8181           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8182           && CONST_INT_P (XEXP (x, 1))
8183           && ((INTVAL (XEXP (XEXP (x, 0), 1))
8184                + floor_log2 (INTVAL (XEXP (x, 1))))
8185               < GET_MODE_BITSIZE (GET_MODE (x)))
8186           && (UINTVAL (XEXP (x, 1))
8187               & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8188         {
8189           temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
8190                           << INTVAL (XEXP (XEXP (x, 0), 1)));
8191           temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8192                                       XEXP (XEXP (x, 0), 0), temp);
8193           x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8194                                    XEXP (XEXP (x, 0), 1));
8195           return force_to_mode (x, mode, mask, next_select);
8196         }
8197
8198     binop:
8199       /* For most binary operations, just propagate into the operation and
8200          change the mode if we have an operation of that mode.  */
8201
8202       op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8203       op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8204
8205       /* If we ended up truncating both operands, truncate the result of the
8206          operation instead.  */
8207       if (GET_CODE (op0) == TRUNCATE
8208           && GET_CODE (op1) == TRUNCATE)
8209         {
8210           op0 = XEXP (op0, 0);
8211           op1 = XEXP (op1, 0);
8212         }
8213
8214       op0 = gen_lowpart_or_truncate (op_mode, op0);
8215       op1 = gen_lowpart_or_truncate (op_mode, op1);
8216
8217       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8218         x = simplify_gen_binary (code, op_mode, op0, op1);
8219       break;
8220
8221     case ASHIFT:
8222       /* For left shifts, do the same, but just for the first operand.
8223          However, we cannot do anything with shifts where we cannot
8224          guarantee that the counts are smaller than the size of the mode
8225          because such a count will have a different meaning in a
8226          wider mode.  */
8227
8228       if (! (CONST_INT_P (XEXP (x, 1))
8229              && INTVAL (XEXP (x, 1)) >= 0
8230              && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
8231           && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8232                 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8233                     < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
8234         break;
8235
8236       /* If the shift count is a constant and we can do arithmetic in
8237          the mode of the shift, refine which bits we need.  Otherwise, use the
8238          conservative form of the mask.  */
8239       if (CONST_INT_P (XEXP (x, 1))
8240           && INTVAL (XEXP (x, 1)) >= 0
8241           && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
8242           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
8243         mask >>= INTVAL (XEXP (x, 1));
8244       else
8245         mask = fuller_mask;
8246
8247       op0 = gen_lowpart_or_truncate (op_mode,
8248                                      force_to_mode (XEXP (x, 0), op_mode,
8249                                                     mask, next_select));
8250
8251       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8252         x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8253       break;
8254
8255     case LSHIFTRT:
8256       /* Here we can only do something if the shift count is a constant,
8257          this shift constant is valid for the host, and we can do arithmetic
8258          in OP_MODE.  */
8259
8260       if (CONST_INT_P (XEXP (x, 1))
8261           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8262           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
8263         {
8264           rtx inner = XEXP (x, 0);
8265           unsigned HOST_WIDE_INT inner_mask;
8266
8267           /* Select the mask of the bits we need for the shift operand.  */
8268           inner_mask = mask << INTVAL (XEXP (x, 1));
8269
8270           /* We can only change the mode of the shift if we can do arithmetic
8271              in the mode of the shift and INNER_MASK is no wider than the
8272              width of X's mode.  */
8273           if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8274             op_mode = GET_MODE (x);
8275
8276           inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8277
8278           if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8279             x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8280         }
8281
8282       /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8283          shift and AND produces only copies of the sign bit (C2 is one less
8284          than a power of two), we can do this with just a shift.  */
8285
8286       if (GET_CODE (x) == LSHIFTRT
8287           && CONST_INT_P (XEXP (x, 1))
8288           /* The shift puts one of the sign bit copies in the least significant
8289              bit.  */
8290           && ((INTVAL (XEXP (x, 1))
8291                + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8292               >= GET_MODE_BITSIZE (GET_MODE (x)))
8293           && exact_log2 (mask + 1) >= 0
8294           /* Number of bits left after the shift must be more than the mask
8295              needs.  */
8296           && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8297               <= GET_MODE_BITSIZE (GET_MODE (x)))
8298           /* Must be more sign bit copies than the mask needs.  */
8299           && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8300               >= exact_log2 (mask + 1)))
8301         x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8302                                  GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
8303                                           - exact_log2 (mask + 1)));
8304
8305       goto shiftrt;
8306
8307     case ASHIFTRT:
8308       /* If we are just looking for the sign bit, we don't need this shift at
8309          all, even if it has a variable count.  */
8310       if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
8311           && (mask == ((unsigned HOST_WIDE_INT) 1
8312                        << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8313         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8314
8315       /* If this is a shift by a constant, get a mask that contains those bits
8316          that are not copies of the sign bit.  We then have two cases:  If
8317          MASK only includes those bits, this can be a logical shift, which may
8318          allow simplifications.  If MASK is a single-bit field not within
8319          those bits, we are requesting a copy of the sign bit and hence can
8320          shift the sign bit to the appropriate location.  */
8321
8322       if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8323           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8324         {
8325           int i;
8326
8327           /* If the considered data is wider than HOST_WIDE_INT, we can't
8328              represent a mask for all its bits in a single scalar.
8329              But we only care about the lower bits, so calculate these.  */
8330
8331           if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8332             {
8333               nonzero = ~(unsigned HOST_WIDE_INT) 0;
8334
8335               /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8336                  is the number of bits a full-width mask would have set.
8337                  We need only shift if these are fewer than nonzero can
8338                  hold.  If not, we must keep all bits set in nonzero.  */
8339
8340               if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8341                   < HOST_BITS_PER_WIDE_INT)
8342                 nonzero >>= INTVAL (XEXP (x, 1))
8343                             + HOST_BITS_PER_WIDE_INT
8344                             - GET_MODE_BITSIZE (GET_MODE (x)) ;
8345             }
8346           else
8347             {
8348               nonzero = GET_MODE_MASK (GET_MODE (x));
8349               nonzero >>= INTVAL (XEXP (x, 1));
8350             }
8351
8352           if ((mask & ~nonzero) == 0)
8353             {
8354               x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8355                                         XEXP (x, 0), INTVAL (XEXP (x, 1)));
8356               if (GET_CODE (x) != ASHIFTRT)
8357                 return force_to_mode (x, mode, mask, next_select);
8358             }
8359
8360           else if ((i = exact_log2 (mask)) >= 0)
8361             {
8362               x = simplify_shift_const
8363                   (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8364                    GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
8365
8366               if (GET_CODE (x) != ASHIFTRT)
8367                 return force_to_mode (x, mode, mask, next_select);
8368             }
8369         }
8370
8371       /* If MASK is 1, convert this to an LSHIFTRT.  This can be done
8372          even if the shift count isn't a constant.  */
8373       if (mask == 1)
8374         x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8375                                  XEXP (x, 0), XEXP (x, 1));
8376
8377     shiftrt:
8378
8379       /* If this is a zero- or sign-extension operation that just affects bits
8380          we don't care about, remove it.  Be sure the call above returned
8381          something that is still a shift.  */
8382
8383       if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8384           && CONST_INT_P (XEXP (x, 1))
8385           && INTVAL (XEXP (x, 1)) >= 0
8386           && (INTVAL (XEXP (x, 1))
8387               <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
8388           && GET_CODE (XEXP (x, 0)) == ASHIFT
8389           && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8390         return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8391                               next_select);
8392
8393       break;
8394
8395     case ROTATE:
8396     case ROTATERT:
8397       /* If the shift count is constant and we can do computations
8398          in the mode of X, compute where the bits we care about are.
8399          Otherwise, we can't do anything.  Don't change the mode of
8400          the shift or propagate MODE into the shift, though.  */
8401       if (CONST_INT_P (XEXP (x, 1))
8402           && INTVAL (XEXP (x, 1)) >= 0)
8403         {
8404           temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8405                                             GET_MODE (x), GEN_INT (mask),
8406                                             XEXP (x, 1));
8407           if (temp && CONST_INT_P (temp))
8408             SUBST (XEXP (x, 0),
8409                    force_to_mode (XEXP (x, 0), GET_MODE (x),
8410                                   INTVAL (temp), next_select));
8411         }
8412       break;
8413
8414     case NEG:
8415       /* If we just want the low-order bit, the NEG isn't needed since it
8416          won't change the low-order bit.  */
8417       if (mask == 1)
8418         return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8419
8420       /* We need any bits less significant than the most significant bit in
8421          MASK since carries from those bits will affect the bits we are
8422          interested in.  */
8423       mask = fuller_mask;
8424       goto unop;
8425
8426     case NOT:
8427       /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8428          same as the XOR case above.  Ensure that the constant we form is not
8429          wider than the mode of X.  */
8430
8431       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8432           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8433           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8434           && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8435               < GET_MODE_BITSIZE (GET_MODE (x)))
8436           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8437         {
8438           temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8439                                GET_MODE (x));
8440           temp = simplify_gen_binary (XOR, GET_MODE (x),
8441                                       XEXP (XEXP (x, 0), 0), temp);
8442           x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8443                                    temp, XEXP (XEXP (x, 0), 1));
8444
8445           return force_to_mode (x, mode, mask, next_select);
8446         }
8447
8448       /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8449          use the full mask inside the NOT.  */
8450       mask = fuller_mask;
8451
8452     unop:
8453       op0 = gen_lowpart_or_truncate (op_mode,
8454                                      force_to_mode (XEXP (x, 0), mode, mask,
8455                                                     next_select));
8456       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8457         x = simplify_gen_unary (code, op_mode, op0, op_mode);
8458       break;
8459
8460     case NE:
8461       /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8462          in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8463          which is equal to STORE_FLAG_VALUE.  */
8464       if ((mask & ~STORE_FLAG_VALUE) == 0
8465           && XEXP (x, 1) == const0_rtx
8466           && GET_MODE (XEXP (x, 0)) == mode
8467           && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8468           && (nonzero_bits (XEXP (x, 0), mode)
8469               == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8470         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8471
8472       break;
8473
8474     case IF_THEN_ELSE:
8475       /* We have no way of knowing if the IF_THEN_ELSE can itself be
8476          written in a narrower mode.  We play it safe and do not do so.  */
8477
8478       SUBST (XEXP (x, 1),
8479              gen_lowpart_or_truncate (GET_MODE (x),
8480                                       force_to_mode (XEXP (x, 1), mode,
8481                                                      mask, next_select)));
8482       SUBST (XEXP (x, 2),
8483              gen_lowpart_or_truncate (GET_MODE (x),
8484                                       force_to_mode (XEXP (x, 2), mode,
8485                                                      mask, next_select)));
8486       break;
8487
8488     default:
8489       break;
8490     }
8491
8492   /* Ensure we return a value of the proper mode.  */
8493   return gen_lowpart_or_truncate (mode, x);
8494 }
8495 \f
8496 /* Return nonzero if X is an expression that has one of two values depending on
8497    whether some other value is zero or nonzero.  In that case, we return the
8498    value that is being tested, *PTRUE is set to the value if the rtx being
8499    returned has a nonzero value, and *PFALSE is set to the other alternative.
8500
8501    If we return zero, we set *PTRUE and *PFALSE to X.  */
8502
8503 static rtx
8504 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8505 {
8506   enum machine_mode mode = GET_MODE (x);
8507   enum rtx_code code = GET_CODE (x);
8508   rtx cond0, cond1, true0, true1, false0, false1;
8509   unsigned HOST_WIDE_INT nz;
8510
8511   /* If we are comparing a value against zero, we are done.  */
8512   if ((code == NE || code == EQ)
8513       && XEXP (x, 1) == const0_rtx)
8514     {
8515       *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8516       *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8517       return XEXP (x, 0);
8518     }
8519
8520   /* If this is a unary operation whose operand has one of two values, apply
8521      our opcode to compute those values.  */
8522   else if (UNARY_P (x)
8523            && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8524     {
8525       *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8526       *pfalse = simplify_gen_unary (code, mode, false0,
8527                                     GET_MODE (XEXP (x, 0)));
8528       return cond0;
8529     }
8530
8531   /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8532      make can't possibly match and would suppress other optimizations.  */
8533   else if (code == COMPARE)
8534     ;
8535
8536   /* If this is a binary operation, see if either side has only one of two
8537      values.  If either one does or if both do and they are conditional on
8538      the same value, compute the new true and false values.  */
8539   else if (BINARY_P (x))
8540     {
8541       cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8542       cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8543
8544       if ((cond0 != 0 || cond1 != 0)
8545           && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8546         {
8547           /* If if_then_else_cond returned zero, then true/false are the
8548              same rtl.  We must copy one of them to prevent invalid rtl
8549              sharing.  */
8550           if (cond0 == 0)
8551             true0 = copy_rtx (true0);
8552           else if (cond1 == 0)
8553             true1 = copy_rtx (true1);
8554
8555           if (COMPARISON_P (x))
8556             {
8557               *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8558                                                 true0, true1);
8559               *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8560                                                  false0, false1);
8561              }
8562           else
8563             {
8564               *ptrue = simplify_gen_binary (code, mode, true0, true1);
8565               *pfalse = simplify_gen_binary (code, mode, false0, false1);
8566             }
8567
8568           return cond0 ? cond0 : cond1;
8569         }
8570
8571       /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8572          operands is zero when the other is nonzero, and vice-versa,
8573          and STORE_FLAG_VALUE is 1 or -1.  */
8574
8575       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8576           && (code == PLUS || code == IOR || code == XOR || code == MINUS
8577               || code == UMAX)
8578           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8579         {
8580           rtx op0 = XEXP (XEXP (x, 0), 1);
8581           rtx op1 = XEXP (XEXP (x, 1), 1);
8582
8583           cond0 = XEXP (XEXP (x, 0), 0);
8584           cond1 = XEXP (XEXP (x, 1), 0);
8585
8586           if (COMPARISON_P (cond0)
8587               && COMPARISON_P (cond1)
8588               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8589                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8590                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8591                   || ((swap_condition (GET_CODE (cond0))
8592                        == reversed_comparison_code (cond1, NULL))
8593                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8594                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8595               && ! side_effects_p (x))
8596             {
8597               *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8598               *pfalse = simplify_gen_binary (MULT, mode,
8599                                              (code == MINUS
8600                                               ? simplify_gen_unary (NEG, mode,
8601                                                                     op1, mode)
8602                                               : op1),
8603                                               const_true_rtx);
8604               return cond0;
8605             }
8606         }
8607
8608       /* Similarly for MULT, AND and UMIN, except that for these the result
8609          is always zero.  */
8610       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8611           && (code == MULT || code == AND || code == UMIN)
8612           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8613         {
8614           cond0 = XEXP (XEXP (x, 0), 0);
8615           cond1 = XEXP (XEXP (x, 1), 0);
8616
8617           if (COMPARISON_P (cond0)
8618               && COMPARISON_P (cond1)
8619               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8620                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8621                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8622                   || ((swap_condition (GET_CODE (cond0))
8623                        == reversed_comparison_code (cond1, NULL))
8624                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8625                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8626               && ! side_effects_p (x))
8627             {
8628               *ptrue = *pfalse = const0_rtx;
8629               return cond0;
8630             }
8631         }
8632     }
8633
8634   else if (code == IF_THEN_ELSE)
8635     {
8636       /* If we have IF_THEN_ELSE already, extract the condition and
8637          canonicalize it if it is NE or EQ.  */
8638       cond0 = XEXP (x, 0);
8639       *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8640       if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8641         return XEXP (cond0, 0);
8642       else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8643         {
8644           *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8645           return XEXP (cond0, 0);
8646         }
8647       else
8648         return cond0;
8649     }
8650
8651   /* If X is a SUBREG, we can narrow both the true and false values
8652      if the inner expression, if there is a condition.  */
8653   else if (code == SUBREG
8654            && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8655                                                &true0, &false0)))
8656     {
8657       true0 = simplify_gen_subreg (mode, true0,
8658                                    GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8659       false0 = simplify_gen_subreg (mode, false0,
8660                                     GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8661       if (true0 && false0)
8662         {
8663           *ptrue = true0;
8664           *pfalse = false0;
8665           return cond0;
8666         }
8667     }
8668
8669   /* If X is a constant, this isn't special and will cause confusions
8670      if we treat it as such.  Likewise if it is equivalent to a constant.  */
8671   else if (CONSTANT_P (x)
8672            || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8673     ;
8674
8675   /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8676      will be least confusing to the rest of the compiler.  */
8677   else if (mode == BImode)
8678     {
8679       *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8680       return x;
8681     }
8682
8683   /* If X is known to be either 0 or -1, those are the true and
8684      false values when testing X.  */
8685   else if (x == constm1_rtx || x == const0_rtx
8686            || (mode != VOIDmode
8687                && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
8688     {
8689       *ptrue = constm1_rtx, *pfalse = const0_rtx;
8690       return x;
8691     }
8692
8693   /* Likewise for 0 or a single bit.  */
8694   else if (SCALAR_INT_MODE_P (mode)
8695            && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8696            && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
8697     {
8698       *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
8699       return x;
8700     }
8701
8702   /* Otherwise fail; show no condition with true and false values the same.  */
8703   *ptrue = *pfalse = x;
8704   return 0;
8705 }
8706 \f
8707 /* Return the value of expression X given the fact that condition COND
8708    is known to be true when applied to REG as its first operand and VAL
8709    as its second.  X is known to not be shared and so can be modified in
8710    place.
8711
8712    We only handle the simplest cases, and specifically those cases that
8713    arise with IF_THEN_ELSE expressions.  */
8714
8715 static rtx
8716 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8717 {
8718   enum rtx_code code = GET_CODE (x);
8719   rtx temp;
8720   const char *fmt;
8721   int i, j;
8722
8723   if (side_effects_p (x))
8724     return x;
8725
8726   /* If either operand of the condition is a floating point value,
8727      then we have to avoid collapsing an EQ comparison.  */
8728   if (cond == EQ
8729       && rtx_equal_p (x, reg)
8730       && ! FLOAT_MODE_P (GET_MODE (x))
8731       && ! FLOAT_MODE_P (GET_MODE (val)))
8732     return val;
8733
8734   if (cond == UNEQ && rtx_equal_p (x, reg))
8735     return val;
8736
8737   /* If X is (abs REG) and we know something about REG's relationship
8738      with zero, we may be able to simplify this.  */
8739
8740   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8741     switch (cond)
8742       {
8743       case GE:  case GT:  case EQ:
8744         return XEXP (x, 0);
8745       case LT:  case LE:
8746         return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8747                                    XEXP (x, 0),
8748                                    GET_MODE (XEXP (x, 0)));
8749       default:
8750         break;
8751       }
8752
8753   /* The only other cases we handle are MIN, MAX, and comparisons if the
8754      operands are the same as REG and VAL.  */
8755
8756   else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8757     {
8758       if (rtx_equal_p (XEXP (x, 0), val))
8759         cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8760
8761       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8762         {
8763           if (COMPARISON_P (x))
8764             {
8765               if (comparison_dominates_p (cond, code))
8766                 return const_true_rtx;
8767
8768               code = reversed_comparison_code (x, NULL);
8769               if (code != UNKNOWN
8770                   && comparison_dominates_p (cond, code))
8771                 return const0_rtx;
8772               else
8773                 return x;
8774             }
8775           else if (code == SMAX || code == SMIN
8776                    || code == UMIN || code == UMAX)
8777             {
8778               int unsignedp = (code == UMIN || code == UMAX);
8779
8780               /* Do not reverse the condition when it is NE or EQ.
8781                  This is because we cannot conclude anything about
8782                  the value of 'SMAX (x, y)' when x is not equal to y,
8783                  but we can when x equals y.  */
8784               if ((code == SMAX || code == UMAX)
8785                   && ! (cond == EQ || cond == NE))
8786                 cond = reverse_condition (cond);
8787
8788               switch (cond)
8789                 {
8790                 case GE:   case GT:
8791                   return unsignedp ? x : XEXP (x, 1);
8792                 case LE:   case LT:
8793                   return unsignedp ? x : XEXP (x, 0);
8794                 case GEU:  case GTU:
8795                   return unsignedp ? XEXP (x, 1) : x;
8796                 case LEU:  case LTU:
8797                   return unsignedp ? XEXP (x, 0) : x;
8798                 default:
8799                   break;
8800                 }
8801             }
8802         }
8803     }
8804   else if (code == SUBREG)
8805     {
8806       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8807       rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
8808
8809       if (SUBREG_REG (x) != r)
8810         {
8811           /* We must simplify subreg here, before we lose track of the
8812              original inner_mode.  */
8813           new_rtx = simplify_subreg (GET_MODE (x), r,
8814                                  inner_mode, SUBREG_BYTE (x));
8815           if (new_rtx)
8816             return new_rtx;
8817           else
8818             SUBST (SUBREG_REG (x), r);
8819         }
8820
8821       return x;
8822     }
8823   /* We don't have to handle SIGN_EXTEND here, because even in the
8824      case of replacing something with a modeless CONST_INT, a
8825      CONST_INT is already (supposed to be) a valid sign extension for
8826      its narrower mode, which implies it's already properly
8827      sign-extended for the wider mode.  Now, for ZERO_EXTEND, the
8828      story is different.  */
8829   else if (code == ZERO_EXTEND)
8830     {
8831       enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8832       rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
8833
8834       if (XEXP (x, 0) != r)
8835         {
8836           /* We must simplify the zero_extend here, before we lose
8837              track of the original inner_mode.  */
8838           new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8839                                           r, inner_mode);
8840           if (new_rtx)
8841             return new_rtx;
8842           else
8843             SUBST (XEXP (x, 0), r);
8844         }
8845
8846       return x;
8847     }
8848
8849   fmt = GET_RTX_FORMAT (code);
8850   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8851     {
8852       if (fmt[i] == 'e')
8853         SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8854       else if (fmt[i] == 'E')
8855         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8856           SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8857                                                 cond, reg, val));
8858     }
8859
8860   return x;
8861 }
8862 \f
8863 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8864    assignment as a field assignment.  */
8865
8866 static int
8867 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8868 {
8869   if (x == y || rtx_equal_p (x, y))
8870     return 1;
8871
8872   if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8873     return 0;
8874
8875   /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8876      Note that all SUBREGs of MEM are paradoxical; otherwise they
8877      would have been rewritten.  */
8878   if (MEM_P (x) && GET_CODE (y) == SUBREG
8879       && MEM_P (SUBREG_REG (y))
8880       && rtx_equal_p (SUBREG_REG (y),
8881                       gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8882     return 1;
8883
8884   if (MEM_P (y) && GET_CODE (x) == SUBREG
8885       && MEM_P (SUBREG_REG (x))
8886       && rtx_equal_p (SUBREG_REG (x),
8887                       gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8888     return 1;
8889
8890   /* We used to see if get_last_value of X and Y were the same but that's
8891      not correct.  In one direction, we'll cause the assignment to have
8892      the wrong destination and in the case, we'll import a register into this
8893      insn that might have already have been dead.   So fail if none of the
8894      above cases are true.  */
8895   return 0;
8896 }
8897 \f
8898 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8899    Return that assignment if so.
8900
8901    We only handle the most common cases.  */
8902
8903 static rtx
8904 make_field_assignment (rtx x)
8905 {
8906   rtx dest = SET_DEST (x);
8907   rtx src = SET_SRC (x);
8908   rtx assign;
8909   rtx rhs, lhs;
8910   HOST_WIDE_INT c1;
8911   HOST_WIDE_INT pos;
8912   unsigned HOST_WIDE_INT len;
8913   rtx other;
8914   enum machine_mode mode;
8915
8916   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
8917      a clear of a one-bit field.  We will have changed it to
8918      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
8919      for a SUBREG.  */
8920
8921   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
8922       && CONST_INT_P (XEXP (XEXP (src, 0), 0))
8923       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
8924       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8925     {
8926       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8927                                 1, 1, 1, 0);
8928       if (assign != 0)
8929         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8930       return x;
8931     }
8932
8933   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
8934       && subreg_lowpart_p (XEXP (src, 0))
8935       && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
8936           < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
8937       && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
8938       && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
8939       && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
8940       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8941     {
8942       assign = make_extraction (VOIDmode, dest, 0,
8943                                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
8944                                 1, 1, 1, 0);
8945       if (assign != 0)
8946         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8947       return x;
8948     }
8949
8950   /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
8951      one-bit field.  */
8952   if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
8953       && XEXP (XEXP (src, 0), 0) == const1_rtx
8954       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8955     {
8956       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8957                                 1, 1, 1, 0);
8958       if (assign != 0)
8959         return gen_rtx_SET (VOIDmode, assign, const1_rtx);
8960       return x;
8961     }
8962
8963   /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
8964      SRC is an AND with all bits of that field set, then we can discard
8965      the AND.  */
8966   if (GET_CODE (dest) == ZERO_EXTRACT
8967       && CONST_INT_P (XEXP (dest, 1))
8968       && GET_CODE (src) == AND
8969       && CONST_INT_P (XEXP (src, 1)))
8970     {
8971       HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
8972       unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
8973       unsigned HOST_WIDE_INT ze_mask;
8974
8975       if (width >= HOST_BITS_PER_WIDE_INT)
8976         ze_mask = -1;
8977       else
8978         ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
8979
8980       /* Complete overlap.  We can remove the source AND.  */
8981       if ((and_mask & ze_mask) == ze_mask)
8982         return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
8983
8984       /* Partial overlap.  We can reduce the source AND.  */
8985       if ((and_mask & ze_mask) != and_mask)
8986         {
8987           mode = GET_MODE (src);
8988           src = gen_rtx_AND (mode, XEXP (src, 0),
8989                              gen_int_mode (and_mask & ze_mask, mode));
8990           return gen_rtx_SET (VOIDmode, dest, src);
8991         }
8992     }
8993
8994   /* The other case we handle is assignments into a constant-position
8995      field.  They look like (ior/xor (and DEST C1) OTHER).  If C1 represents
8996      a mask that has all one bits except for a group of zero bits and
8997      OTHER is known to have zeros where C1 has ones, this is such an
8998      assignment.  Compute the position and length from C1.  Shift OTHER
8999      to the appropriate position, force it to the required mode, and
9000      make the extraction.  Check for the AND in both operands.  */
9001
9002   if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9003     return x;
9004
9005   rhs = expand_compound_operation (XEXP (src, 0));
9006   lhs = expand_compound_operation (XEXP (src, 1));
9007
9008   if (GET_CODE (rhs) == AND
9009       && CONST_INT_P (XEXP (rhs, 1))
9010       && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9011     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9012   else if (GET_CODE (lhs) == AND
9013            && CONST_INT_P (XEXP (lhs, 1))
9014            && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9015     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9016   else
9017     return x;
9018
9019   pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9020   if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
9021       || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9022       || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9023     return x;
9024
9025   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9026   if (assign == 0)
9027     return x;
9028
9029   /* The mode to use for the source is the mode of the assignment, or of
9030      what is inside a possible STRICT_LOW_PART.  */
9031   mode = (GET_CODE (assign) == STRICT_LOW_PART
9032           ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9033
9034   /* Shift OTHER right POS places and make it the source, restricting it
9035      to the proper length and mode.  */
9036
9037   src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9038                                                      GET_MODE (src),
9039                                                      other, pos),
9040                                dest);
9041   src = force_to_mode (src, mode,
9042                        GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
9043                        ? ~(unsigned HOST_WIDE_INT) 0
9044                        : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9045                        0);
9046
9047   /* If SRC is masked by an AND that does not make a difference in
9048      the value being stored, strip it.  */
9049   if (GET_CODE (assign) == ZERO_EXTRACT
9050       && CONST_INT_P (XEXP (assign, 1))
9051       && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9052       && GET_CODE (src) == AND
9053       && CONST_INT_P (XEXP (src, 1))
9054       && UINTVAL (XEXP (src, 1))
9055          == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9056     src = XEXP (src, 0);
9057
9058   return gen_rtx_SET (VOIDmode, assign, src);
9059 }
9060 \f
9061 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9062    if so.  */
9063
9064 static rtx
9065 apply_distributive_law (rtx x)
9066 {
9067   enum rtx_code code = GET_CODE (x);
9068   enum rtx_code inner_code;
9069   rtx lhs, rhs, other;
9070   rtx tem;
9071
9072   /* Distributivity is not true for floating point as it can change the
9073      value.  So we don't do it unless -funsafe-math-optimizations.  */
9074   if (FLOAT_MODE_P (GET_MODE (x))
9075       && ! flag_unsafe_math_optimizations)
9076     return x;
9077
9078   /* The outer operation can only be one of the following:  */
9079   if (code != IOR && code != AND && code != XOR
9080       && code != PLUS && code != MINUS)
9081     return x;
9082
9083   lhs = XEXP (x, 0);
9084   rhs = XEXP (x, 1);
9085
9086   /* If either operand is a primitive we can't do anything, so get out
9087      fast.  */
9088   if (OBJECT_P (lhs) || OBJECT_P (rhs))
9089     return x;
9090
9091   lhs = expand_compound_operation (lhs);
9092   rhs = expand_compound_operation (rhs);
9093   inner_code = GET_CODE (lhs);
9094   if (inner_code != GET_CODE (rhs))
9095     return x;
9096
9097   /* See if the inner and outer operations distribute.  */
9098   switch (inner_code)
9099     {
9100     case LSHIFTRT:
9101     case ASHIFTRT:
9102     case AND:
9103     case IOR:
9104       /* These all distribute except over PLUS.  */
9105       if (code == PLUS || code == MINUS)
9106         return x;
9107       break;
9108
9109     case MULT:
9110       if (code != PLUS && code != MINUS)
9111         return x;
9112       break;
9113
9114     case ASHIFT:
9115       /* This is also a multiply, so it distributes over everything.  */
9116       break;
9117
9118     case SUBREG:
9119       /* Non-paradoxical SUBREGs distributes over all operations,
9120          provided the inner modes and byte offsets are the same, this
9121          is an extraction of a low-order part, we don't convert an fp
9122          operation to int or vice versa, this is not a vector mode,
9123          and we would not be converting a single-word operation into a
9124          multi-word operation.  The latter test is not required, but
9125          it prevents generating unneeded multi-word operations.  Some
9126          of the previous tests are redundant given the latter test,
9127          but are retained because they are required for correctness.
9128
9129          We produce the result slightly differently in this case.  */
9130
9131       if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
9132           || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
9133           || ! subreg_lowpart_p (lhs)
9134           || (GET_MODE_CLASS (GET_MODE (lhs))
9135               != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
9136           || (GET_MODE_SIZE (GET_MODE (lhs))
9137               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
9138           || VECTOR_MODE_P (GET_MODE (lhs))
9139           || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD
9140           /* Result might need to be truncated.  Don't change mode if
9141              explicit truncation is needed.  */
9142           || !TRULY_NOOP_TRUNCATION
9143                (GET_MODE_BITSIZE (GET_MODE (x)),
9144                 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (lhs)))))
9145         return x;
9146
9147       tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
9148                                  SUBREG_REG (lhs), SUBREG_REG (rhs));
9149       return gen_lowpart (GET_MODE (x), tem);
9150
9151     default:
9152       return x;
9153     }
9154
9155   /* Set LHS and RHS to the inner operands (A and B in the example
9156      above) and set OTHER to the common operand (C in the example).
9157      There is only one way to do this unless the inner operation is
9158      commutative.  */
9159   if (COMMUTATIVE_ARITH_P (lhs)
9160       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9161     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9162   else if (COMMUTATIVE_ARITH_P (lhs)
9163            && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9164     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9165   else if (COMMUTATIVE_ARITH_P (lhs)
9166            && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9167     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9168   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9169     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9170   else
9171     return x;
9172
9173   /* Form the new inner operation, seeing if it simplifies first.  */
9174   tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9175
9176   /* There is one exception to the general way of distributing:
9177      (a | c) ^ (b | c) -> (a ^ b) & ~c  */
9178   if (code == XOR && inner_code == IOR)
9179     {
9180       inner_code = AND;
9181       other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9182     }
9183
9184   /* We may be able to continuing distributing the result, so call
9185      ourselves recursively on the inner operation before forming the
9186      outer operation, which we return.  */
9187   return simplify_gen_binary (inner_code, GET_MODE (x),
9188                               apply_distributive_law (tem), other);
9189 }
9190
9191 /* See if X is of the form (* (+ A B) C), and if so convert to
9192    (+ (* A C) (* B C)) and try to simplify.
9193
9194    Most of the time, this results in no change.  However, if some of
9195    the operands are the same or inverses of each other, simplifications
9196    will result.
9197
9198    For example, (and (ior A B) (not B)) can occur as the result of
9199    expanding a bit field assignment.  When we apply the distributive
9200    law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9201    which then simplifies to (and (A (not B))).
9202
9203    Note that no checks happen on the validity of applying the inverse
9204    distributive law.  This is pointless since we can do it in the
9205    few places where this routine is called.
9206
9207    N is the index of the term that is decomposed (the arithmetic operation,
9208    i.e. (+ A B) in the first example above).  !N is the index of the term that
9209    is distributed, i.e. of C in the first example above.  */
9210 static rtx
9211 distribute_and_simplify_rtx (rtx x, int n)
9212 {
9213   enum machine_mode mode;
9214   enum rtx_code outer_code, inner_code;
9215   rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9216
9217   /* Distributivity is not true for floating point as it can change the
9218      value.  So we don't do it unless -funsafe-math-optimizations.  */
9219   if (FLOAT_MODE_P (GET_MODE (x))
9220       && ! flag_unsafe_math_optimizations)
9221     return NULL_RTX;
9222
9223   decomposed = XEXP (x, n);
9224   if (!ARITHMETIC_P (decomposed))
9225     return NULL_RTX;
9226
9227   mode = GET_MODE (x);
9228   outer_code = GET_CODE (x);
9229   distributed = XEXP (x, !n);
9230
9231   inner_code = GET_CODE (decomposed);
9232   inner_op0 = XEXP (decomposed, 0);
9233   inner_op1 = XEXP (decomposed, 1);
9234
9235   /* Special case (and (xor B C) (not A)), which is equivalent to
9236      (xor (ior A B) (ior A C))  */
9237   if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9238     {
9239       distributed = XEXP (distributed, 0);
9240       outer_code = IOR;
9241     }
9242
9243   if (n == 0)
9244     {
9245       /* Distribute the second term.  */
9246       new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9247       new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9248     }
9249   else
9250     {
9251       /* Distribute the first term.  */
9252       new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9253       new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9254     }
9255
9256   tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9257                                                      new_op0, new_op1));
9258   if (GET_CODE (tmp) != outer_code
9259       && rtx_cost (tmp, SET, optimize_this_for_speed_p)
9260          < rtx_cost (x, SET, optimize_this_for_speed_p))
9261     return tmp;
9262
9263   return NULL_RTX;
9264 }
9265 \f
9266 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9267    in MODE.  Return an equivalent form, if different from (and VAROP
9268    (const_int CONSTOP)).  Otherwise, return NULL_RTX.  */
9269
9270 static rtx
9271 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
9272                           unsigned HOST_WIDE_INT constop)
9273 {
9274   unsigned HOST_WIDE_INT nonzero;
9275   unsigned HOST_WIDE_INT orig_constop;
9276   rtx orig_varop;
9277   int i;
9278
9279   orig_varop = varop;
9280   orig_constop = constop;
9281   if (GET_CODE (varop) == CLOBBER)
9282     return NULL_RTX;
9283
9284   /* Simplify VAROP knowing that we will be only looking at some of the
9285      bits in it.
9286
9287      Note by passing in CONSTOP, we guarantee that the bits not set in
9288      CONSTOP are not significant and will never be examined.  We must
9289      ensure that is the case by explicitly masking out those bits
9290      before returning.  */
9291   varop = force_to_mode (varop, mode, constop, 0);
9292
9293   /* If VAROP is a CLOBBER, we will fail so return it.  */
9294   if (GET_CODE (varop) == CLOBBER)
9295     return varop;
9296
9297   /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9298      to VAROP and return the new constant.  */
9299   if (CONST_INT_P (varop))
9300     return gen_int_mode (INTVAL (varop) & constop, mode);
9301
9302   /* See what bits may be nonzero in VAROP.  Unlike the general case of
9303      a call to nonzero_bits, here we don't care about bits outside
9304      MODE.  */
9305
9306   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9307
9308   /* Turn off all bits in the constant that are known to already be zero.
9309      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9310      which is tested below.  */
9311
9312   constop &= nonzero;
9313
9314   /* If we don't have any bits left, return zero.  */
9315   if (constop == 0)
9316     return const0_rtx;
9317
9318   /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9319      a power of two, we can replace this with an ASHIFT.  */
9320   if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9321       && (i = exact_log2 (constop)) >= 0)
9322     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9323
9324   /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9325      or XOR, then try to apply the distributive law.  This may eliminate
9326      operations if either branch can be simplified because of the AND.
9327      It may also make some cases more complex, but those cases probably
9328      won't match a pattern either with or without this.  */
9329
9330   if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9331     return
9332       gen_lowpart
9333         (mode,
9334          apply_distributive_law
9335          (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9336                                simplify_and_const_int (NULL_RTX,
9337                                                        GET_MODE (varop),
9338                                                        XEXP (varop, 0),
9339                                                        constop),
9340                                simplify_and_const_int (NULL_RTX,
9341                                                        GET_MODE (varop),
9342                                                        XEXP (varop, 1),
9343                                                        constop))));
9344
9345   /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9346      the AND and see if one of the operands simplifies to zero.  If so, we
9347      may eliminate it.  */
9348
9349   if (GET_CODE (varop) == PLUS
9350       && exact_log2 (constop + 1) >= 0)
9351     {
9352       rtx o0, o1;
9353
9354       o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9355       o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9356       if (o0 == const0_rtx)
9357         return o1;
9358       if (o1 == const0_rtx)
9359         return o0;
9360     }
9361
9362   /* Make a SUBREG if necessary.  If we can't make it, fail.  */
9363   varop = gen_lowpart (mode, varop);
9364   if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9365     return NULL_RTX;
9366
9367   /* If we are only masking insignificant bits, return VAROP.  */
9368   if (constop == nonzero)
9369     return varop;
9370
9371   if (varop == orig_varop && constop == orig_constop)
9372     return NULL_RTX;
9373
9374   /* Otherwise, return an AND.  */
9375   return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9376 }
9377
9378
9379 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9380    in MODE.
9381
9382    Return an equivalent form, if different from X.  Otherwise, return X.  If
9383    X is zero, we are to always construct the equivalent form.  */
9384
9385 static rtx
9386 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
9387                         unsigned HOST_WIDE_INT constop)
9388 {
9389   rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9390   if (tem)
9391     return tem;
9392
9393   if (!x)
9394     x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9395                              gen_int_mode (constop, mode));
9396   if (GET_MODE (x) != mode)
9397     x = gen_lowpart (mode, x);
9398   return x;
9399 }
9400 \f
9401 /* Given a REG, X, compute which bits in X can be nonzero.
9402    We don't care about bits outside of those defined in MODE.
9403
9404    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9405    a shift, AND, or zero_extract, we can do better.  */
9406
9407 static rtx
9408 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
9409                               const_rtx known_x ATTRIBUTE_UNUSED,
9410                               enum machine_mode known_mode ATTRIBUTE_UNUSED,
9411                               unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9412                               unsigned HOST_WIDE_INT *nonzero)
9413 {
9414   rtx tem;
9415   reg_stat_type *rsp;
9416
9417   /* If X is a register whose nonzero bits value is current, use it.
9418      Otherwise, if X is a register whose value we can find, use that
9419      value.  Otherwise, use the previously-computed global nonzero bits
9420      for this register.  */
9421
9422   rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
9423   if (rsp->last_set_value != 0
9424       && (rsp->last_set_mode == mode
9425           || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9426               && GET_MODE_CLASS (mode) == MODE_INT))
9427       && ((rsp->last_set_label >= label_tick_ebb_start
9428            && rsp->last_set_label < label_tick)
9429           || (rsp->last_set_label == label_tick
9430               && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9431           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9432               && REG_N_SETS (REGNO (x)) == 1
9433               && !REGNO_REG_SET_P
9434                   (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
9435     {
9436       *nonzero &= rsp->last_set_nonzero_bits;
9437       return NULL;
9438     }
9439
9440   tem = get_last_value (x);
9441
9442   if (tem)
9443     {
9444 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9445       /* If X is narrower than MODE and TEM is a non-negative
9446          constant that would appear negative in the mode of X,
9447          sign-extend it for use in reg_nonzero_bits because some
9448          machines (maybe most) will actually do the sign-extension
9449          and this is the conservative approach.
9450
9451          ??? For 2.5, try to tighten up the MD files in this regard
9452          instead of this kludge.  */
9453
9454       if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
9455           && CONST_INT_P (tem)
9456           && INTVAL (tem) > 0
9457           && 0 != (UINTVAL (tem)
9458                    & ((unsigned HOST_WIDE_INT) 1
9459                       << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
9460         tem = GEN_INT (UINTVAL (tem)
9461                        | ((unsigned HOST_WIDE_INT) (-1)
9462                           << GET_MODE_BITSIZE (GET_MODE (x))));
9463 #endif
9464       return tem;
9465     }
9466   else if (nonzero_sign_valid && rsp->nonzero_bits)
9467     {
9468       unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9469
9470       if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
9471         /* We don't know anything about the upper bits.  */
9472         mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9473       *nonzero &= mask;
9474     }
9475
9476   return NULL;
9477 }
9478
9479 /* Return the number of bits at the high-order end of X that are known to
9480    be equal to the sign bit.  X will be used in mode MODE; if MODE is
9481    VOIDmode, X will be used in its own mode.  The returned value  will always
9482    be between 1 and the number of bits in MODE.  */
9483
9484 static rtx
9485 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
9486                                      const_rtx known_x ATTRIBUTE_UNUSED,
9487                                      enum machine_mode known_mode
9488                                      ATTRIBUTE_UNUSED,
9489                                      unsigned int known_ret ATTRIBUTE_UNUSED,
9490                                      unsigned int *result)
9491 {
9492   rtx tem;
9493   reg_stat_type *rsp;
9494
9495   rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
9496   if (rsp->last_set_value != 0
9497       && rsp->last_set_mode == mode
9498       && ((rsp->last_set_label >= label_tick_ebb_start
9499            && rsp->last_set_label < label_tick)
9500           || (rsp->last_set_label == label_tick
9501               && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9502           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9503               && REG_N_SETS (REGNO (x)) == 1
9504               && !REGNO_REG_SET_P
9505                   (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
9506     {
9507       *result = rsp->last_set_sign_bit_copies;
9508       return NULL;
9509     }
9510
9511   tem = get_last_value (x);
9512   if (tem != 0)
9513     return tem;
9514
9515   if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9516       && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
9517     *result = rsp->sign_bit_copies;
9518
9519   return NULL;
9520 }
9521 \f
9522 /* Return the number of "extended" bits there are in X, when interpreted
9523    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
9524    unsigned quantities, this is the number of high-order zero bits.
9525    For signed quantities, this is the number of copies of the sign bit
9526    minus 1.  In both case, this function returns the number of "spare"
9527    bits.  For example, if two quantities for which this function returns
9528    at least 1 are added, the addition is known not to overflow.
9529
9530    This function will always return 0 unless called during combine, which
9531    implies that it must be called from a define_split.  */
9532
9533 unsigned int
9534 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
9535 {
9536   if (nonzero_sign_valid == 0)
9537     return 0;
9538
9539   return (unsignedp
9540           ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9541              ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
9542                                - floor_log2 (nonzero_bits (x, mode)))
9543              : 0)
9544           : num_sign_bit_copies (x, mode) - 1);
9545 }
9546 \f
9547 /* This function is called from `simplify_shift_const' to merge two
9548    outer operations.  Specifically, we have already found that we need
9549    to perform operation *POP0 with constant *PCONST0 at the outermost
9550    position.  We would now like to also perform OP1 with constant CONST1
9551    (with *POP0 being done last).
9552
9553    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9554    the resulting operation.  *PCOMP_P is set to 1 if we would need to
9555    complement the innermost operand, otherwise it is unchanged.
9556
9557    MODE is the mode in which the operation will be done.  No bits outside
9558    the width of this mode matter.  It is assumed that the width of this mode
9559    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9560
9561    If *POP0 or OP1 are UNKNOWN, it means no operation is required.  Only NEG, PLUS,
9562    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
9563    result is simply *PCONST0.
9564
9565    If the resulting operation cannot be expressed as one operation, we
9566    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
9567
9568 static int
9569 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)
9570 {
9571   enum rtx_code op0 = *pop0;
9572   HOST_WIDE_INT const0 = *pconst0;
9573
9574   const0 &= GET_MODE_MASK (mode);
9575   const1 &= GET_MODE_MASK (mode);
9576
9577   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
9578   if (op0 == AND)
9579     const1 &= const0;
9580
9581   /* If OP0 or OP1 is UNKNOWN, this is easy.  Similarly if they are the same or
9582      if OP0 is SET.  */
9583
9584   if (op1 == UNKNOWN || op0 == SET)
9585     return 1;
9586
9587   else if (op0 == UNKNOWN)
9588     op0 = op1, const0 = const1;
9589
9590   else if (op0 == op1)
9591     {
9592       switch (op0)
9593         {
9594         case AND:
9595           const0 &= const1;
9596           break;
9597         case IOR:
9598           const0 |= const1;
9599           break;
9600         case XOR:
9601           const0 ^= const1;
9602           break;
9603         case PLUS:
9604           const0 += const1;
9605           break;
9606         case NEG:
9607           op0 = UNKNOWN;
9608           break;
9609         default:
9610           break;
9611         }
9612     }
9613
9614   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
9615   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9616     return 0;
9617
9618   /* If the two constants aren't the same, we can't do anything.  The
9619      remaining six cases can all be done.  */
9620   else if (const0 != const1)
9621     return 0;
9622
9623   else
9624     switch (op0)
9625       {
9626       case IOR:
9627         if (op1 == AND)
9628           /* (a & b) | b == b */
9629           op0 = SET;
9630         else /* op1 == XOR */
9631           /* (a ^ b) | b == a | b */
9632           {;}
9633         break;
9634
9635       case XOR:
9636         if (op1 == AND)
9637           /* (a & b) ^ b == (~a) & b */
9638           op0 = AND, *pcomp_p = 1;
9639         else /* op1 == IOR */
9640           /* (a | b) ^ b == a & ~b */
9641           op0 = AND, const0 = ~const0;
9642         break;
9643
9644       case AND:
9645         if (op1 == IOR)
9646           /* (a | b) & b == b */
9647         op0 = SET;
9648         else /* op1 == XOR */
9649           /* (a ^ b) & b) == (~a) & b */
9650           *pcomp_p = 1;
9651         break;
9652       default:
9653         break;
9654       }
9655
9656   /* Check for NO-OP cases.  */
9657   const0 &= GET_MODE_MASK (mode);
9658   if (const0 == 0
9659       && (op0 == IOR || op0 == XOR || op0 == PLUS))
9660     op0 = UNKNOWN;
9661   else if (const0 == 0 && op0 == AND)
9662     op0 = SET;
9663   else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9664            && op0 == AND)
9665     op0 = UNKNOWN;
9666
9667   *pop0 = op0;
9668
9669   /* ??? Slightly redundant with the above mask, but not entirely.
9670      Moving this above means we'd have to sign-extend the mode mask
9671      for the final test.  */
9672   if (op0 != UNKNOWN && op0 != NEG)
9673     *pconst0 = trunc_int_for_mode (const0, mode);
9674
9675   return 1;
9676 }
9677 \f
9678 /* A helper to simplify_shift_const_1 to determine the mode we can perform
9679    the shift in.  The original shift operation CODE is performed on OP in
9680    ORIG_MODE.  Return the wider mode MODE if we can perform the operation
9681    in that mode.  Return ORIG_MODE otherwise.  We can also assume that the
9682    result of the shift is subject to operation OUTER_CODE with operand
9683    OUTER_CONST.  */
9684
9685 static enum machine_mode
9686 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
9687                       enum machine_mode orig_mode, enum machine_mode mode,
9688                       enum rtx_code outer_code, HOST_WIDE_INT outer_const)
9689 {
9690   if (orig_mode == mode)
9691     return mode;
9692   gcc_assert (GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (orig_mode));
9693
9694   /* In general we can't perform in wider mode for right shift and rotate.  */
9695   switch (code)
9696     {
9697     case ASHIFTRT:
9698       /* We can still widen if the bits brought in from the left are identical
9699          to the sign bit of ORIG_MODE.  */
9700       if (num_sign_bit_copies (op, mode)
9701           > (unsigned) (GET_MODE_BITSIZE (mode)
9702                         - GET_MODE_BITSIZE (orig_mode)))
9703         return mode;
9704       return orig_mode;
9705
9706     case LSHIFTRT:
9707       /* Similarly here but with zero bits.  */
9708       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9709           && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
9710         return mode;
9711
9712       /* We can also widen if the bits brought in will be masked off.  This
9713          operation is performed in ORIG_MODE.  */
9714       if (outer_code == AND)
9715         {
9716           int care_bits = low_bitmask_len (orig_mode, outer_const);
9717
9718           if (care_bits >= 0
9719               && GET_MODE_BITSIZE (orig_mode) - care_bits >= count)
9720             return mode;
9721         }
9722       /* fall through */
9723
9724     case ROTATE:
9725       return orig_mode;
9726
9727     case ROTATERT:
9728       gcc_unreachable ();
9729
9730     default:
9731       return mode;
9732     }
9733 }
9734
9735 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
9736    The result of the shift is RESULT_MODE.  Return NULL_RTX if we cannot
9737    simplify it.  Otherwise, return a simplified value.
9738
9739    The shift is normally computed in the widest mode we find in VAROP, as
9740    long as it isn't a different number of words than RESULT_MODE.  Exceptions
9741    are ASHIFTRT and ROTATE, which are always done in their original mode.  */
9742
9743 static rtx
9744 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
9745                         rtx varop, int orig_count)
9746 {
9747   enum rtx_code orig_code = code;
9748   rtx orig_varop = varop;
9749   int count;
9750   enum machine_mode mode = result_mode;
9751   enum machine_mode shift_mode, tmode;
9752   unsigned int mode_words
9753     = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9754   /* We form (outer_op (code varop count) (outer_const)).  */
9755   enum rtx_code outer_op = UNKNOWN;
9756   HOST_WIDE_INT outer_const = 0;
9757   int complement_p = 0;
9758   rtx new_rtx, x;
9759
9760   /* Make sure and truncate the "natural" shift on the way in.  We don't
9761      want to do this inside the loop as it makes it more difficult to
9762      combine shifts.  */
9763   if (SHIFT_COUNT_TRUNCATED)
9764     orig_count &= GET_MODE_BITSIZE (mode) - 1;
9765
9766   /* If we were given an invalid count, don't do anything except exactly
9767      what was requested.  */
9768
9769   if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
9770     return NULL_RTX;
9771
9772   count = orig_count;
9773
9774   /* Unless one of the branches of the `if' in this loop does a `continue',
9775      we will `break' the loop after the `if'.  */
9776
9777   while (count != 0)
9778     {
9779       /* If we have an operand of (clobber (const_int 0)), fail.  */
9780       if (GET_CODE (varop) == CLOBBER)
9781         return NULL_RTX;
9782
9783       /* Convert ROTATERT to ROTATE.  */
9784       if (code == ROTATERT)
9785         {
9786           unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
9787           code = ROTATE;
9788           if (VECTOR_MODE_P (result_mode))
9789             count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9790           else
9791             count = bitsize - count;
9792         }
9793
9794       shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
9795                                          mode, outer_op, outer_const);
9796
9797       /* Handle cases where the count is greater than the size of the mode
9798          minus 1.  For ASHIFT, use the size minus one as the count (this can
9799          occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
9800          take the count modulo the size.  For other shifts, the result is
9801          zero.
9802
9803          Since these shifts are being produced by the compiler by combining
9804          multiple operations, each of which are defined, we know what the
9805          result is supposed to be.  */
9806
9807       if (count > (GET_MODE_BITSIZE (shift_mode) - 1))
9808         {
9809           if (code == ASHIFTRT)
9810             count = GET_MODE_BITSIZE (shift_mode) - 1;
9811           else if (code == ROTATE || code == ROTATERT)
9812             count %= GET_MODE_BITSIZE (shift_mode);
9813           else
9814             {
9815               /* We can't simply return zero because there may be an
9816                  outer op.  */
9817               varop = const0_rtx;
9818               count = 0;
9819               break;
9820             }
9821         }
9822
9823       /* If we discovered we had to complement VAROP, leave.  Making a NOT
9824          here would cause an infinite loop.  */
9825       if (complement_p)
9826         break;
9827
9828       /* An arithmetic right shift of a quantity known to be -1 or 0
9829          is a no-op.  */
9830       if (code == ASHIFTRT
9831           && (num_sign_bit_copies (varop, shift_mode)
9832               == GET_MODE_BITSIZE (shift_mode)))
9833         {
9834           count = 0;
9835           break;
9836         }
9837
9838       /* If we are doing an arithmetic right shift and discarding all but
9839          the sign bit copies, this is equivalent to doing a shift by the
9840          bitsize minus one.  Convert it into that shift because it will often
9841          allow other simplifications.  */
9842
9843       if (code == ASHIFTRT
9844           && (count + num_sign_bit_copies (varop, shift_mode)
9845               >= GET_MODE_BITSIZE (shift_mode)))
9846         count = GET_MODE_BITSIZE (shift_mode) - 1;
9847
9848       /* We simplify the tests below and elsewhere by converting
9849          ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9850          `make_compound_operation' will convert it to an ASHIFTRT for
9851          those machines (such as VAX) that don't have an LSHIFTRT.  */
9852       if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9853           && code == ASHIFTRT
9854           && ((nonzero_bits (varop, shift_mode)
9855                & ((unsigned HOST_WIDE_INT) 1
9856                   << (GET_MODE_BITSIZE (shift_mode) - 1))) == 0))
9857         code = LSHIFTRT;
9858
9859       if (((code == LSHIFTRT
9860             && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9861             && !(nonzero_bits (varop, shift_mode) >> count))
9862            || (code == ASHIFT
9863                && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9864                && !((nonzero_bits (varop, shift_mode) << count)
9865                     & GET_MODE_MASK (shift_mode))))
9866           && !side_effects_p (varop))
9867         varop = const0_rtx;
9868
9869       switch (GET_CODE (varop))
9870         {
9871         case SIGN_EXTEND:
9872         case ZERO_EXTEND:
9873         case SIGN_EXTRACT:
9874         case ZERO_EXTRACT:
9875           new_rtx = expand_compound_operation (varop);
9876           if (new_rtx != varop)
9877             {
9878               varop = new_rtx;
9879               continue;
9880             }
9881           break;
9882
9883         case MEM:
9884           /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9885              minus the width of a smaller mode, we can do this with a
9886              SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
9887           if ((code == ASHIFTRT || code == LSHIFTRT)
9888               && ! mode_dependent_address_p (XEXP (varop, 0))
9889               && ! MEM_VOLATILE_P (varop)
9890               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9891                                          MODE_INT, 1)) != BLKmode)
9892             {
9893               new_rtx = adjust_address_nv (varop, tmode,
9894                                        BYTES_BIG_ENDIAN ? 0
9895                                        : count / BITS_PER_UNIT);
9896
9897               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9898                                      : ZERO_EXTEND, mode, new_rtx);
9899               count = 0;
9900               continue;
9901             }
9902           break;
9903
9904         case SUBREG:
9905           /* If VAROP is a SUBREG, strip it as long as the inner operand has
9906              the same number of words as what we've seen so far.  Then store
9907              the widest mode in MODE.  */
9908           if (subreg_lowpart_p (varop)
9909               && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9910                   > GET_MODE_SIZE (GET_MODE (varop)))
9911               && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9912                                   + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9913                  == mode_words
9914               && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
9915               && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
9916             {
9917               varop = SUBREG_REG (varop);
9918               if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9919                 mode = GET_MODE (varop);
9920               continue;
9921             }
9922           break;
9923
9924         case MULT:
9925           /* Some machines use MULT instead of ASHIFT because MULT
9926              is cheaper.  But it is still better on those machines to
9927              merge two shifts into one.  */
9928           if (CONST_INT_P (XEXP (varop, 1))
9929               && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
9930             {
9931               varop
9932                 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
9933                                        XEXP (varop, 0),
9934                                        GEN_INT (exact_log2 (
9935                                                 UINTVAL (XEXP (varop, 1)))));
9936               continue;
9937             }
9938           break;
9939
9940         case UDIV:
9941           /* Similar, for when divides are cheaper.  */
9942           if (CONST_INT_P (XEXP (varop, 1))
9943               && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
9944             {
9945               varop
9946                 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
9947                                        XEXP (varop, 0),
9948                                        GEN_INT (exact_log2 (
9949                                                 UINTVAL (XEXP (varop, 1)))));
9950               continue;
9951             }
9952           break;
9953
9954         case ASHIFTRT:
9955           /* If we are extracting just the sign bit of an arithmetic
9956              right shift, that shift is not needed.  However, the sign
9957              bit of a wider mode may be different from what would be
9958              interpreted as the sign bit in a narrower mode, so, if
9959              the result is narrower, don't discard the shift.  */
9960           if (code == LSHIFTRT
9961               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9962               && (GET_MODE_BITSIZE (result_mode)
9963                   >= GET_MODE_BITSIZE (GET_MODE (varop))))
9964             {
9965               varop = XEXP (varop, 0);
9966               continue;
9967             }
9968
9969           /* ... fall through ...  */
9970
9971         case LSHIFTRT:
9972         case ASHIFT:
9973         case ROTATE:
9974           /* Here we have two nested shifts.  The result is usually the
9975              AND of a new shift with a mask.  We compute the result below.  */
9976           if (CONST_INT_P (XEXP (varop, 1))
9977               && INTVAL (XEXP (varop, 1)) >= 0
9978               && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9979               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9980               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9981               && !VECTOR_MODE_P (result_mode))
9982             {
9983               enum rtx_code first_code = GET_CODE (varop);
9984               unsigned int first_count = INTVAL (XEXP (varop, 1));
9985               unsigned HOST_WIDE_INT mask;
9986               rtx mask_rtx;
9987
9988               /* We have one common special case.  We can't do any merging if
9989                  the inner code is an ASHIFTRT of a smaller mode.  However, if
9990                  we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9991                  with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9992                  we can convert it to
9993                  (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9994                  This simplifies certain SIGN_EXTEND operations.  */
9995               if (code == ASHIFT && first_code == ASHIFTRT
9996                   && count == (GET_MODE_BITSIZE (result_mode)
9997                                - GET_MODE_BITSIZE (GET_MODE (varop))))
9998                 {
9999                   /* C3 has the low-order C1 bits zero.  */
10000
10001                   mask = GET_MODE_MASK (mode)
10002                          & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10003
10004                   varop = simplify_and_const_int (NULL_RTX, result_mode,
10005                                                   XEXP (varop, 0), mask);
10006                   varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10007                                                 varop, count);
10008                   count = first_count;
10009                   code = ASHIFTRT;
10010                   continue;
10011                 }
10012
10013               /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10014                  than C1 high-order bits equal to the sign bit, we can convert
10015                  this to either an ASHIFT or an ASHIFTRT depending on the
10016                  two counts.
10017
10018                  We cannot do this if VAROP's mode is not SHIFT_MODE.  */
10019
10020               if (code == ASHIFTRT && first_code == ASHIFT
10021                   && GET_MODE (varop) == shift_mode
10022                   && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10023                       > first_count))
10024                 {
10025                   varop = XEXP (varop, 0);
10026                   count -= first_count;
10027                   if (count < 0)
10028                     {
10029                       count = -count;
10030                       code = ASHIFT;
10031                     }
10032
10033                   continue;
10034                 }
10035
10036               /* There are some cases we can't do.  If CODE is ASHIFTRT,
10037                  we can only do this if FIRST_CODE is also ASHIFTRT.
10038
10039                  We can't do the case when CODE is ROTATE and FIRST_CODE is
10040                  ASHIFTRT.
10041
10042                  If the mode of this shift is not the mode of the outer shift,
10043                  we can't do this if either shift is a right shift or ROTATE.
10044
10045                  Finally, we can't do any of these if the mode is too wide
10046                  unless the codes are the same.
10047
10048                  Handle the case where the shift codes are the same
10049                  first.  */
10050
10051               if (code == first_code)
10052                 {
10053                   if (GET_MODE (varop) != result_mode
10054                       && (code == ASHIFTRT || code == LSHIFTRT
10055                           || code == ROTATE))
10056                     break;
10057
10058                   count += first_count;
10059                   varop = XEXP (varop, 0);
10060                   continue;
10061                 }
10062
10063               if (code == ASHIFTRT
10064                   || (code == ROTATE && first_code == ASHIFTRT)
10065                   || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
10066                   || (GET_MODE (varop) != result_mode
10067                       && (first_code == ASHIFTRT || first_code == LSHIFTRT
10068                           || first_code == ROTATE
10069                           || code == ROTATE)))
10070                 break;
10071
10072               /* To compute the mask to apply after the shift, shift the
10073                  nonzero bits of the inner shift the same way the
10074                  outer shift will.  */
10075
10076               mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
10077
10078               mask_rtx
10079                 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10080                                                    GEN_INT (count));
10081
10082               /* Give up if we can't compute an outer operation to use.  */
10083               if (mask_rtx == 0
10084                   || !CONST_INT_P (mask_rtx)
10085                   || ! merge_outer_ops (&outer_op, &outer_const, AND,
10086                                         INTVAL (mask_rtx),
10087                                         result_mode, &complement_p))
10088                 break;
10089
10090               /* If the shifts are in the same direction, we add the
10091                  counts.  Otherwise, we subtract them.  */
10092               if ((code == ASHIFTRT || code == LSHIFTRT)
10093                   == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10094                 count += first_count;
10095               else
10096                 count -= first_count;
10097
10098               /* If COUNT is positive, the new shift is usually CODE,
10099                  except for the two exceptions below, in which case it is
10100                  FIRST_CODE.  If the count is negative, FIRST_CODE should
10101                  always be used  */
10102               if (count > 0
10103                   && ((first_code == ROTATE && code == ASHIFT)
10104                       || (first_code == ASHIFTRT && code == LSHIFTRT)))
10105                 code = first_code;
10106               else if (count < 0)
10107                 code = first_code, count = -count;
10108
10109               varop = XEXP (varop, 0);
10110               continue;
10111             }
10112
10113           /* If we have (A << B << C) for any shift, we can convert this to
10114              (A << C << B).  This wins if A is a constant.  Only try this if
10115              B is not a constant.  */
10116
10117           else if (GET_CODE (varop) == code
10118                    && CONST_INT_P (XEXP (varop, 0))
10119                    && !CONST_INT_P (XEXP (varop, 1)))
10120             {
10121               rtx new_rtx = simplify_const_binary_operation (code, mode,
10122                                                          XEXP (varop, 0),
10123                                                          GEN_INT (count));
10124               varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10125               count = 0;
10126               continue;
10127             }
10128           break;
10129
10130         case NOT:
10131           if (VECTOR_MODE_P (mode))
10132             break;
10133
10134           /* Make this fit the case below.  */
10135           varop = gen_rtx_XOR (mode, XEXP (varop, 0),
10136                                GEN_INT (GET_MODE_MASK (mode)));
10137           continue;
10138
10139         case IOR:
10140         case AND:
10141         case XOR:
10142           /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10143              with C the size of VAROP - 1 and the shift is logical if
10144              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10145              we have an (le X 0) operation.   If we have an arithmetic shift
10146              and STORE_FLAG_VALUE is 1 or we have a logical shift with
10147              STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
10148
10149           if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10150               && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10151               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10152               && (code == LSHIFTRT || code == ASHIFTRT)
10153               && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
10154               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10155             {
10156               count = 0;
10157               varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10158                                   const0_rtx);
10159
10160               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10161                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10162
10163               continue;
10164             }
10165
10166           /* If we have (shift (logical)), move the logical to the outside
10167              to allow it to possibly combine with another logical and the
10168              shift to combine with another shift.  This also canonicalizes to
10169              what a ZERO_EXTRACT looks like.  Also, some machines have
10170              (and (shift)) insns.  */
10171
10172           if (CONST_INT_P (XEXP (varop, 1))
10173               /* We can't do this if we have (ashiftrt (xor))  and the
10174                  constant has its sign bit set in shift_mode.  */
10175               && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10176                    && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10177                                               shift_mode))
10178               && (new_rtx = simplify_const_binary_operation (code, result_mode,
10179                                                          XEXP (varop, 1),
10180                                                          GEN_INT (count))) != 0
10181               && CONST_INT_P (new_rtx)
10182               && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10183                                   INTVAL (new_rtx), result_mode, &complement_p))
10184             {
10185               varop = XEXP (varop, 0);
10186               continue;
10187             }
10188
10189           /* If we can't do that, try to simplify the shift in each arm of the
10190              logical expression, make a new logical expression, and apply
10191              the inverse distributive law.  This also can't be done
10192              for some (ashiftrt (xor)).  */
10193           if (CONST_INT_P (XEXP (varop, 1))
10194              && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10195                   && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10196                                              shift_mode)))
10197             {
10198               rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10199                                               XEXP (varop, 0), count);
10200               rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10201                                               XEXP (varop, 1), count);
10202
10203               varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10204                                            lhs, rhs);
10205               varop = apply_distributive_law (varop);
10206
10207               count = 0;
10208               continue;
10209             }
10210           break;
10211
10212         case EQ:
10213           /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10214              says that the sign bit can be tested, FOO has mode MODE, C is
10215              GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
10216              that may be nonzero.  */
10217           if (code == LSHIFTRT
10218               && XEXP (varop, 1) == const0_rtx
10219               && GET_MODE (XEXP (varop, 0)) == result_mode
10220               && count == (GET_MODE_BITSIZE (result_mode) - 1)
10221               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
10222               && STORE_FLAG_VALUE == -1
10223               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10224               && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10225                                   &complement_p))
10226             {
10227               varop = XEXP (varop, 0);
10228               count = 0;
10229               continue;
10230             }
10231           break;
10232
10233         case NEG:
10234           /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10235              than the number of bits in the mode is equivalent to A.  */
10236           if (code == LSHIFTRT
10237               && count == (GET_MODE_BITSIZE (result_mode) - 1)
10238               && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10239             {
10240               varop = XEXP (varop, 0);
10241               count = 0;
10242               continue;
10243             }
10244
10245           /* NEG commutes with ASHIFT since it is multiplication.  Move the
10246              NEG outside to allow shifts to combine.  */
10247           if (code == ASHIFT
10248               && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10249                                   &complement_p))
10250             {
10251               varop = XEXP (varop, 0);
10252               continue;
10253             }
10254           break;
10255
10256         case PLUS:
10257           /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10258              is one less than the number of bits in the mode is
10259              equivalent to (xor A 1).  */
10260           if (code == LSHIFTRT
10261               && count == (GET_MODE_BITSIZE (result_mode) - 1)
10262               && XEXP (varop, 1) == constm1_rtx
10263               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10264               && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10265                                   &complement_p))
10266             {
10267               count = 0;
10268               varop = XEXP (varop, 0);
10269               continue;
10270             }
10271
10272           /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10273              that might be nonzero in BAR are those being shifted out and those
10274              bits are known zero in FOO, we can replace the PLUS with FOO.
10275              Similarly in the other operand order.  This code occurs when
10276              we are computing the size of a variable-size array.  */
10277
10278           if ((code == ASHIFTRT || code == LSHIFTRT)
10279               && count < HOST_BITS_PER_WIDE_INT
10280               && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10281               && (nonzero_bits (XEXP (varop, 1), result_mode)
10282                   & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10283             {
10284               varop = XEXP (varop, 0);
10285               continue;
10286             }
10287           else if ((code == ASHIFTRT || code == LSHIFTRT)
10288                    && count < HOST_BITS_PER_WIDE_INT
10289                    && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
10290                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10291                             >> count)
10292                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10293                             & nonzero_bits (XEXP (varop, 1),
10294                                                  result_mode)))
10295             {
10296               varop = XEXP (varop, 1);
10297               continue;
10298             }
10299
10300           /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
10301           if (code == ASHIFT
10302               && CONST_INT_P (XEXP (varop, 1))
10303               && (new_rtx = simplify_const_binary_operation (ASHIFT, result_mode,
10304                                                          XEXP (varop, 1),
10305                                                          GEN_INT (count))) != 0
10306               && CONST_INT_P (new_rtx)
10307               && merge_outer_ops (&outer_op, &outer_const, PLUS,
10308                                   INTVAL (new_rtx), result_mode, &complement_p))
10309             {
10310               varop = XEXP (varop, 0);
10311               continue;
10312             }
10313
10314           /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10315              signbit', and attempt to change the PLUS to an XOR and move it to
10316              the outer operation as is done above in the AND/IOR/XOR case
10317              leg for shift(logical). See details in logical handling above
10318              for reasoning in doing so.  */
10319           if (code == LSHIFTRT
10320               && CONST_INT_P (XEXP (varop, 1))
10321               && mode_signbit_p (result_mode, XEXP (varop, 1))
10322               && (new_rtx = simplify_const_binary_operation (code, result_mode,
10323                                                          XEXP (varop, 1),
10324                                                          GEN_INT (count))) != 0
10325               && CONST_INT_P (new_rtx)
10326               && merge_outer_ops (&outer_op, &outer_const, XOR,
10327                                   INTVAL (new_rtx), result_mode, &complement_p))
10328             {
10329               varop = XEXP (varop, 0);
10330               continue;
10331             }
10332
10333           break;
10334
10335         case MINUS:
10336           /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10337              with C the size of VAROP - 1 and the shift is logical if
10338              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10339              we have a (gt X 0) operation.  If the shift is arithmetic with
10340              STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10341              we have a (neg (gt X 0)) operation.  */
10342
10343           if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10344               && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10345               && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
10346               && (code == LSHIFTRT || code == ASHIFTRT)
10347               && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10348               && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10349               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10350             {
10351               count = 0;
10352               varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10353                                   const0_rtx);
10354
10355               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10356                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10357
10358               continue;
10359             }
10360           break;
10361
10362         case TRUNCATE:
10363           /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10364              if the truncate does not affect the value.  */
10365           if (code == LSHIFTRT
10366               && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10367               && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10368               && (INTVAL (XEXP (XEXP (varop, 0), 1))
10369                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
10370                       - GET_MODE_BITSIZE (GET_MODE (varop)))))
10371             {
10372               rtx varop_inner = XEXP (varop, 0);
10373
10374               varop_inner
10375                 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10376                                     XEXP (varop_inner, 0),
10377                                     GEN_INT
10378                                     (count + INTVAL (XEXP (varop_inner, 1))));
10379               varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10380               count = 0;
10381               continue;
10382             }
10383           break;
10384
10385         default:
10386           break;
10387         }
10388
10389       break;
10390     }
10391
10392   shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10393                                      outer_op, outer_const);
10394
10395   /* We have now finished analyzing the shift.  The result should be
10396      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
10397      OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10398      to the result of the shift.  OUTER_CONST is the relevant constant,
10399      but we must turn off all bits turned off in the shift.  */
10400
10401   if (outer_op == UNKNOWN
10402       && orig_code == code && orig_count == count
10403       && varop == orig_varop
10404       && shift_mode == GET_MODE (varop))
10405     return NULL_RTX;
10406
10407   /* Make a SUBREG if necessary.  If we can't make it, fail.  */
10408   varop = gen_lowpart (shift_mode, varop);
10409   if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10410     return NULL_RTX;
10411
10412   /* If we have an outer operation and we just made a shift, it is
10413      possible that we could have simplified the shift were it not
10414      for the outer operation.  So try to do the simplification
10415      recursively.  */
10416
10417   if (outer_op != UNKNOWN)
10418     x = simplify_shift_const_1 (code, shift_mode, varop, count);
10419   else
10420     x = NULL_RTX;
10421
10422   if (x == NULL_RTX)
10423     x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10424
10425   /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10426      turn off all the bits that the shift would have turned off.  */
10427   if (orig_code == LSHIFTRT && result_mode != shift_mode)
10428     x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10429                                 GET_MODE_MASK (result_mode) >> orig_count);
10430
10431   /* Do the remainder of the processing in RESULT_MODE.  */
10432   x = gen_lowpart_or_truncate (result_mode, x);
10433
10434   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10435      operation.  */
10436   if (complement_p)
10437     x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10438
10439   if (outer_op != UNKNOWN)
10440     {
10441       if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10442           && GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
10443         outer_const = trunc_int_for_mode (outer_const, result_mode);
10444
10445       if (outer_op == AND)
10446         x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10447       else if (outer_op == SET)
10448         {
10449           /* This means that we have determined that the result is
10450              equivalent to a constant.  This should be rare.  */
10451           if (!side_effects_p (x))
10452             x = GEN_INT (outer_const);
10453         }
10454       else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10455         x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10456       else
10457         x = simplify_gen_binary (outer_op, result_mode, x,
10458                                  GEN_INT (outer_const));
10459     }
10460
10461   return x;
10462 }
10463
10464 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
10465    The result of the shift is RESULT_MODE.  If we cannot simplify it,
10466    return X or, if it is NULL, synthesize the expression with
10467    simplify_gen_binary.  Otherwise, return a simplified value.
10468
10469    The shift is normally computed in the widest mode we find in VAROP, as
10470    long as it isn't a different number of words than RESULT_MODE.  Exceptions
10471    are ASHIFTRT and ROTATE, which are always done in their original mode.  */
10472
10473 static rtx
10474 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
10475                       rtx varop, int count)
10476 {
10477   rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10478   if (tem)
10479     return tem;
10480
10481   if (!x)
10482     x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10483   if (GET_MODE (x) != result_mode)
10484     x = gen_lowpart (result_mode, x);
10485   return x;
10486 }
10487
10488 \f
10489 /* Like recog, but we receive the address of a pointer to a new pattern.
10490    We try to match the rtx that the pointer points to.
10491    If that fails, we may try to modify or replace the pattern,
10492    storing the replacement into the same pointer object.
10493
10494    Modifications include deletion or addition of CLOBBERs.
10495
10496    PNOTES is a pointer to a location where any REG_UNUSED notes added for
10497    the CLOBBERs are placed.
10498
10499    The value is the final insn code from the pattern ultimately matched,
10500    or -1.  */
10501
10502 static int
10503 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
10504 {
10505   rtx pat = *pnewpat;
10506   int insn_code_number;
10507   int num_clobbers_to_add = 0;
10508   int i;
10509   rtx notes = 0;
10510   rtx old_notes, old_pat;
10511
10512   /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10513      we use to indicate that something didn't match.  If we find such a
10514      thing, force rejection.  */
10515   if (GET_CODE (pat) == PARALLEL)
10516     for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10517       if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10518           && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10519         return -1;
10520
10521   old_pat = PATTERN (insn);
10522   old_notes = REG_NOTES (insn);
10523   PATTERN (insn) = pat;
10524   REG_NOTES (insn) = 0;
10525
10526   insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10527   if (dump_file && (dump_flags & TDF_DETAILS))
10528     {
10529       if (insn_code_number < 0)
10530         fputs ("Failed to match this instruction:\n", dump_file);
10531       else
10532         fputs ("Successfully matched this instruction:\n", dump_file);
10533       print_rtl_single (dump_file, pat);
10534     }
10535
10536   /* If it isn't, there is the possibility that we previously had an insn
10537      that clobbered some register as a side effect, but the combined
10538      insn doesn't need to do that.  So try once more without the clobbers
10539      unless this represents an ASM insn.  */
10540
10541   if (insn_code_number < 0 && ! check_asm_operands (pat)
10542       && GET_CODE (pat) == PARALLEL)
10543     {
10544       int pos;
10545
10546       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10547         if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10548           {
10549             if (i != pos)
10550               SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10551             pos++;
10552           }
10553
10554       SUBST_INT (XVECLEN (pat, 0), pos);
10555
10556       if (pos == 1)
10557         pat = XVECEXP (pat, 0, 0);
10558
10559       PATTERN (insn) = pat;
10560       insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10561       if (dump_file && (dump_flags & TDF_DETAILS))
10562         {
10563           if (insn_code_number < 0)
10564             fputs ("Failed to match this instruction:\n", dump_file);
10565           else
10566             fputs ("Successfully matched this instruction:\n", dump_file);
10567           print_rtl_single (dump_file, pat);
10568         }
10569     }
10570   PATTERN (insn) = old_pat;
10571   REG_NOTES (insn) = old_notes;
10572
10573   /* Recognize all noop sets, these will be killed by followup pass.  */
10574   if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10575     insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10576
10577   /* If we had any clobbers to add, make a new pattern than contains
10578      them.  Then check to make sure that all of them are dead.  */
10579   if (num_clobbers_to_add)
10580     {
10581       rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10582                                      rtvec_alloc (GET_CODE (pat) == PARALLEL
10583                                                   ? (XVECLEN (pat, 0)
10584                                                      + num_clobbers_to_add)
10585                                                   : num_clobbers_to_add + 1));
10586
10587       if (GET_CODE (pat) == PARALLEL)
10588         for (i = 0; i < XVECLEN (pat, 0); i++)
10589           XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10590       else
10591         XVECEXP (newpat, 0, 0) = pat;
10592
10593       add_clobbers (newpat, insn_code_number);
10594
10595       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10596            i < XVECLEN (newpat, 0); i++)
10597         {
10598           if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10599               && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10600             return -1;
10601           if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10602             {
10603               gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10604               notes = alloc_reg_note (REG_UNUSED,
10605                                       XEXP (XVECEXP (newpat, 0, i), 0), notes);
10606             }
10607         }
10608       pat = newpat;
10609     }
10610
10611   *pnewpat = pat;
10612   *pnotes = notes;
10613
10614   return insn_code_number;
10615 }
10616 \f
10617 /* Like gen_lowpart_general but for use by combine.  In combine it
10618    is not possible to create any new pseudoregs.  However, it is
10619    safe to create invalid memory addresses, because combine will
10620    try to recognize them and all they will do is make the combine
10621    attempt fail.
10622
10623    If for some reason this cannot do its job, an rtx
10624    (clobber (const_int 0)) is returned.
10625    An insn containing that will not be recognized.  */
10626
10627 static rtx
10628 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
10629 {
10630   enum machine_mode imode = GET_MODE (x);
10631   unsigned int osize = GET_MODE_SIZE (omode);
10632   unsigned int isize = GET_MODE_SIZE (imode);
10633   rtx result;
10634
10635   if (omode == imode)
10636     return x;
10637
10638   /* Return identity if this is a CONST or symbolic reference.  */
10639   if (omode == Pmode
10640       && (GET_CODE (x) == CONST
10641           || GET_CODE (x) == SYMBOL_REF
10642           || GET_CODE (x) == LABEL_REF))
10643     return x;
10644
10645   /* We can only support MODE being wider than a word if X is a
10646      constant integer or has a mode the same size.  */
10647   if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
10648       && ! ((imode == VOIDmode
10649              && (CONST_INT_P (x)
10650                  || GET_CODE (x) == CONST_DOUBLE))
10651             || isize == osize))
10652     goto fail;
10653
10654   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
10655      won't know what to do.  So we will strip off the SUBREG here and
10656      process normally.  */
10657   if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
10658     {
10659       x = SUBREG_REG (x);
10660
10661       /* For use in case we fall down into the address adjustments
10662          further below, we need to adjust the known mode and size of
10663          x; imode and isize, since we just adjusted x.  */
10664       imode = GET_MODE (x);
10665
10666       if (imode == omode)
10667         return x;
10668
10669       isize = GET_MODE_SIZE (imode);
10670     }
10671
10672   result = gen_lowpart_common (omode, x);
10673
10674   if (result)
10675     return result;
10676
10677   if (MEM_P (x))
10678     {
10679       int offset = 0;
10680
10681       /* Refuse to work on a volatile memory ref or one with a mode-dependent
10682          address.  */
10683       if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
10684         goto fail;
10685
10686       /* If we want to refer to something bigger than the original memref,
10687          generate a paradoxical subreg instead.  That will force a reload
10688          of the original memref X.  */
10689       if (isize < osize)
10690         return gen_rtx_SUBREG (omode, x, 0);
10691
10692       if (WORDS_BIG_ENDIAN)
10693         offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
10694
10695       /* Adjust the address so that the address-after-the-data is
10696          unchanged.  */
10697       if (BYTES_BIG_ENDIAN)
10698         offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
10699
10700       return adjust_address_nv (x, omode, offset);
10701     }
10702
10703   /* If X is a comparison operator, rewrite it in a new mode.  This
10704      probably won't match, but may allow further simplifications.  */
10705   else if (COMPARISON_P (x))
10706     return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
10707
10708   /* If we couldn't simplify X any other way, just enclose it in a
10709      SUBREG.  Normally, this SUBREG won't match, but some patterns may
10710      include an explicit SUBREG or we may simplify it further in combine.  */
10711   else
10712     {
10713       int offset = 0;
10714       rtx res;
10715
10716       offset = subreg_lowpart_offset (omode, imode);
10717       if (imode == VOIDmode)
10718         {
10719           imode = int_mode_for_mode (omode);
10720           x = gen_lowpart_common (imode, x);
10721           if (x == NULL)
10722             goto fail;
10723         }
10724       res = simplify_gen_subreg (omode, x, imode, offset);
10725       if (res)
10726         return res;
10727     }
10728
10729  fail:
10730   return gen_rtx_CLOBBER (omode, const0_rtx);
10731 }
10732 \f
10733 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
10734    comparison code that will be tested.
10735
10736    The result is a possibly different comparison code to use.  *POP0 and
10737    *POP1 may be updated.
10738
10739    It is possible that we might detect that a comparison is either always
10740    true or always false.  However, we do not perform general constant
10741    folding in combine, so this knowledge isn't useful.  Such tautologies
10742    should have been detected earlier.  Hence we ignore all such cases.  */
10743
10744 static enum rtx_code
10745 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
10746 {
10747   rtx op0 = *pop0;
10748   rtx op1 = *pop1;
10749   rtx tem, tem1;
10750   int i;
10751   enum machine_mode mode, tmode;
10752
10753   /* Try a few ways of applying the same transformation to both operands.  */
10754   while (1)
10755     {
10756 #ifndef WORD_REGISTER_OPERATIONS
10757       /* The test below this one won't handle SIGN_EXTENDs on these machines,
10758          so check specially.  */
10759       if (code != GTU && code != GEU && code != LTU && code != LEU
10760           && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
10761           && GET_CODE (XEXP (op0, 0)) == ASHIFT
10762           && GET_CODE (XEXP (op1, 0)) == ASHIFT
10763           && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
10764           && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
10765           && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
10766               == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
10767           && CONST_INT_P (XEXP (op0, 1))
10768           && XEXP (op0, 1) == XEXP (op1, 1)
10769           && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10770           && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
10771           && (INTVAL (XEXP (op0, 1))
10772               == (GET_MODE_BITSIZE (GET_MODE (op0))
10773                   - (GET_MODE_BITSIZE
10774                      (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
10775         {
10776           op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
10777           op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
10778         }
10779 #endif
10780
10781       /* If both operands are the same constant shift, see if we can ignore the
10782          shift.  We can if the shift is a rotate or if the bits shifted out of
10783          this shift are known to be zero for both inputs and if the type of
10784          comparison is compatible with the shift.  */
10785       if (GET_CODE (op0) == GET_CODE (op1)
10786           && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10787           && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
10788               || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
10789                   && (code != GT && code != LT && code != GE && code != LE))
10790               || (GET_CODE (op0) == ASHIFTRT
10791                   && (code != GTU && code != LTU
10792                       && code != GEU && code != LEU)))
10793           && CONST_INT_P (XEXP (op0, 1))
10794           && INTVAL (XEXP (op0, 1)) >= 0
10795           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10796           && XEXP (op0, 1) == XEXP (op1, 1))
10797         {
10798           enum machine_mode mode = GET_MODE (op0);
10799           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10800           int shift_count = INTVAL (XEXP (op0, 1));
10801
10802           if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
10803             mask &= (mask >> shift_count) << shift_count;
10804           else if (GET_CODE (op0) == ASHIFT)
10805             mask = (mask & (mask << shift_count)) >> shift_count;
10806
10807           if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
10808               && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
10809             op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
10810           else
10811             break;
10812         }
10813
10814       /* If both operands are AND's of a paradoxical SUBREG by constant, the
10815          SUBREGs are of the same mode, and, in both cases, the AND would
10816          be redundant if the comparison was done in the narrower mode,
10817          do the comparison in the narrower mode (e.g., we are AND'ing with 1
10818          and the operand's possibly nonzero bits are 0xffffff01; in that case
10819          if we only care about QImode, we don't need the AND).  This case
10820          occurs if the output mode of an scc insn is not SImode and
10821          STORE_FLAG_VALUE == 1 (e.g., the 386).
10822
10823          Similarly, check for a case where the AND's are ZERO_EXTEND
10824          operations from some narrower mode even though a SUBREG is not
10825          present.  */
10826
10827       else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
10828                && CONST_INT_P (XEXP (op0, 1))
10829                && CONST_INT_P (XEXP (op1, 1)))
10830         {
10831           rtx inner_op0 = XEXP (op0, 0);
10832           rtx inner_op1 = XEXP (op1, 0);
10833           HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
10834           HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
10835           int changed = 0;
10836
10837           if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
10838               && (GET_MODE_SIZE (GET_MODE (inner_op0))
10839                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
10840               && (GET_MODE (SUBREG_REG (inner_op0))
10841                   == GET_MODE (SUBREG_REG (inner_op1)))
10842               && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
10843                   <= HOST_BITS_PER_WIDE_INT)
10844               && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
10845                                              GET_MODE (SUBREG_REG (inner_op0)))))
10846               && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
10847                                              GET_MODE (SUBREG_REG (inner_op1))))))
10848             {
10849               op0 = SUBREG_REG (inner_op0);
10850               op1 = SUBREG_REG (inner_op1);
10851
10852               /* The resulting comparison is always unsigned since we masked
10853                  off the original sign bit.  */
10854               code = unsigned_condition (code);
10855
10856               changed = 1;
10857             }
10858
10859           else if (c0 == c1)
10860             for (tmode = GET_CLASS_NARROWEST_MODE
10861                  (GET_MODE_CLASS (GET_MODE (op0)));
10862                  tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10863               if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10864                 {
10865                   op0 = gen_lowpart (tmode, inner_op0);
10866                   op1 = gen_lowpart (tmode, inner_op1);
10867                   code = unsigned_condition (code);
10868                   changed = 1;
10869                   break;
10870                 }
10871
10872           if (! changed)
10873             break;
10874         }
10875
10876       /* If both operands are NOT, we can strip off the outer operation
10877          and adjust the comparison code for swapped operands; similarly for
10878          NEG, except that this must be an equality comparison.  */
10879       else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10880                || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10881                    && (code == EQ || code == NE)))
10882         op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10883
10884       else
10885         break;
10886     }
10887
10888   /* If the first operand is a constant, swap the operands and adjust the
10889      comparison code appropriately, but don't do this if the second operand
10890      is already a constant integer.  */
10891   if (swap_commutative_operands_p (op0, op1))
10892     {
10893       tem = op0, op0 = op1, op1 = tem;
10894       code = swap_condition (code);
10895     }
10896
10897   /* We now enter a loop during which we will try to simplify the comparison.
10898      For the most part, we only are concerned with comparisons with zero,
10899      but some things may really be comparisons with zero but not start
10900      out looking that way.  */
10901
10902   while (CONST_INT_P (op1))
10903     {
10904       enum machine_mode mode = GET_MODE (op0);
10905       unsigned int mode_width = GET_MODE_BITSIZE (mode);
10906       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10907       int equality_comparison_p;
10908       int sign_bit_comparison_p;
10909       int unsigned_comparison_p;
10910       HOST_WIDE_INT const_op;
10911
10912       /* We only want to handle integral modes.  This catches VOIDmode,
10913          CCmode, and the floating-point modes.  An exception is that we
10914          can handle VOIDmode if OP0 is a COMPARE or a comparison
10915          operation.  */
10916
10917       if (GET_MODE_CLASS (mode) != MODE_INT
10918           && ! (mode == VOIDmode
10919                 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
10920         break;
10921
10922       /* Get the constant we are comparing against and turn off all bits
10923          not on in our mode.  */
10924       const_op = INTVAL (op1);
10925       if (mode != VOIDmode)
10926         const_op = trunc_int_for_mode (const_op, mode);
10927       op1 = GEN_INT (const_op);
10928
10929       /* If we are comparing against a constant power of two and the value
10930          being compared can only have that single bit nonzero (e.g., it was
10931          `and'ed with that bit), we can replace this with a comparison
10932          with zero.  */
10933       if (const_op
10934           && (code == EQ || code == NE || code == GE || code == GEU
10935               || code == LT || code == LTU)
10936           && mode_width <= HOST_BITS_PER_WIDE_INT
10937           && exact_log2 (const_op) >= 0
10938           && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10939         {
10940           code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10941           op1 = const0_rtx, const_op = 0;
10942         }
10943
10944       /* Similarly, if we are comparing a value known to be either -1 or
10945          0 with -1, change it to the opposite comparison against zero.  */
10946
10947       if (const_op == -1
10948           && (code == EQ || code == NE || code == GT || code == LE
10949               || code == GEU || code == LTU)
10950           && num_sign_bit_copies (op0, mode) == mode_width)
10951         {
10952           code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10953           op1 = const0_rtx, const_op = 0;
10954         }
10955
10956       /* Do some canonicalizations based on the comparison code.  We prefer
10957          comparisons against zero and then prefer equality comparisons.
10958          If we can reduce the size of a constant, we will do that too.  */
10959
10960       switch (code)
10961         {
10962         case LT:
10963           /* < C is equivalent to <= (C - 1) */
10964           if (const_op > 0)
10965             {
10966               const_op -= 1;
10967               op1 = GEN_INT (const_op);
10968               code = LE;
10969               /* ... fall through to LE case below.  */
10970             }
10971           else
10972             break;
10973
10974         case LE:
10975           /* <= C is equivalent to < (C + 1); we do this for C < 0  */
10976           if (const_op < 0)
10977             {
10978               const_op += 1;
10979               op1 = GEN_INT (const_op);
10980               code = LT;
10981             }
10982
10983           /* If we are doing a <= 0 comparison on a value known to have
10984              a zero sign bit, we can replace this with == 0.  */
10985           else if (const_op == 0
10986                    && mode_width <= HOST_BITS_PER_WIDE_INT
10987                    && (nonzero_bits (op0, mode)
10988                        & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10989                          == 0)
10990             code = EQ;
10991           break;
10992
10993         case GE:
10994           /* >= C is equivalent to > (C - 1).  */
10995           if (const_op > 0)
10996             {
10997               const_op -= 1;
10998               op1 = GEN_INT (const_op);
10999               code = GT;
11000               /* ... fall through to GT below.  */
11001             }
11002           else
11003             break;
11004
11005         case GT:
11006           /* > C is equivalent to >= (C + 1); we do this for C < 0.  */
11007           if (const_op < 0)
11008             {
11009               const_op += 1;
11010               op1 = GEN_INT (const_op);
11011               code = GE;
11012             }
11013
11014           /* If we are doing a > 0 comparison on a value known to have
11015              a zero sign bit, we can replace this with != 0.  */
11016           else if (const_op == 0
11017                    && mode_width <= HOST_BITS_PER_WIDE_INT
11018                    && (nonzero_bits (op0, mode)
11019                        & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11020                        == 0)
11021             code = NE;
11022           break;
11023
11024         case LTU:
11025           /* < C is equivalent to <= (C - 1).  */
11026           if (const_op > 0)
11027             {
11028               const_op -= 1;
11029               op1 = GEN_INT (const_op);
11030               code = LEU;
11031               /* ... fall through ...  */
11032             }
11033
11034           /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
11035           else if (mode_width <= HOST_BITS_PER_WIDE_INT
11036                    && (unsigned HOST_WIDE_INT) const_op
11037                       == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11038             {
11039               const_op = 0, op1 = const0_rtx;
11040               code = GE;
11041               break;
11042             }
11043           else
11044             break;
11045
11046         case LEU:
11047           /* unsigned <= 0 is equivalent to == 0 */
11048           if (const_op == 0)
11049             code = EQ;
11050
11051           /* (unsigned) <= 0x7fffffff is equivalent to >= 0.  */
11052           else if (mode_width <= HOST_BITS_PER_WIDE_INT
11053                    && (unsigned HOST_WIDE_INT) const_op
11054                       == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11055             {
11056               const_op = 0, op1 = const0_rtx;
11057               code = GE;
11058             }
11059           break;
11060
11061         case GEU:
11062           /* >= C is equivalent to > (C - 1).  */
11063           if (const_op > 1)
11064             {
11065               const_op -= 1;
11066               op1 = GEN_INT (const_op);
11067               code = GTU;
11068               /* ... fall through ...  */
11069             }
11070
11071           /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
11072           else if (mode_width <= HOST_BITS_PER_WIDE_INT
11073                    && (unsigned HOST_WIDE_INT) const_op
11074                       == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11075             {
11076               const_op = 0, op1 = const0_rtx;
11077               code = LT;
11078               break;
11079             }
11080           else
11081             break;
11082
11083         case GTU:
11084           /* unsigned > 0 is equivalent to != 0 */
11085           if (const_op == 0)
11086             code = NE;
11087
11088           /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
11089           else if (mode_width <= HOST_BITS_PER_WIDE_INT
11090                    && (unsigned HOST_WIDE_INT) const_op
11091                       == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11092             {
11093               const_op = 0, op1 = const0_rtx;
11094               code = LT;
11095             }
11096           break;
11097
11098         default:
11099           break;
11100         }
11101
11102       /* Compute some predicates to simplify code below.  */
11103
11104       equality_comparison_p = (code == EQ || code == NE);
11105       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11106       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11107                                || code == GEU);
11108
11109       /* If this is a sign bit comparison and we can do arithmetic in
11110          MODE, say that we will only be needing the sign bit of OP0.  */
11111       if (sign_bit_comparison_p
11112           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11113         op0 = force_to_mode (op0, mode,
11114                              (unsigned HOST_WIDE_INT) 1
11115                              << (GET_MODE_BITSIZE (mode) - 1),
11116                              0);
11117
11118       /* Now try cases based on the opcode of OP0.  If none of the cases
11119          does a "continue", we exit this loop immediately after the
11120          switch.  */
11121
11122       switch (GET_CODE (op0))
11123         {
11124         case ZERO_EXTRACT:
11125           /* If we are extracting a single bit from a variable position in
11126              a constant that has only a single bit set and are comparing it
11127              with zero, we can convert this into an equality comparison
11128              between the position and the location of the single bit.  */
11129           /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11130              have already reduced the shift count modulo the word size.  */
11131           if (!SHIFT_COUNT_TRUNCATED
11132               && CONST_INT_P (XEXP (op0, 0))
11133               && XEXP (op0, 1) == const1_rtx
11134               && equality_comparison_p && const_op == 0
11135               && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11136             {
11137               if (BITS_BIG_ENDIAN)
11138                 {
11139                   enum machine_mode new_mode
11140                     = mode_for_extraction (EP_extzv, 1);
11141                   if (new_mode == MAX_MACHINE_MODE)
11142                     i = BITS_PER_WORD - 1 - i;
11143                   else
11144                     {
11145                       mode = new_mode;
11146                       i = (GET_MODE_BITSIZE (mode) - 1 - i);
11147                     }
11148                 }
11149
11150               op0 = XEXP (op0, 2);
11151               op1 = GEN_INT (i);
11152               const_op = i;
11153
11154               /* Result is nonzero iff shift count is equal to I.  */
11155               code = reverse_condition (code);
11156               continue;
11157             }
11158
11159           /* ... fall through ...  */
11160
11161         case SIGN_EXTRACT:
11162           tem = expand_compound_operation (op0);
11163           if (tem != op0)
11164             {
11165               op0 = tem;
11166               continue;
11167             }
11168           break;
11169
11170         case NOT:
11171           /* If testing for equality, we can take the NOT of the constant.  */
11172           if (equality_comparison_p
11173               && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11174             {
11175               op0 = XEXP (op0, 0);
11176               op1 = tem;
11177               continue;
11178             }
11179
11180           /* If just looking at the sign bit, reverse the sense of the
11181              comparison.  */
11182           if (sign_bit_comparison_p)
11183             {
11184               op0 = XEXP (op0, 0);
11185               code = (code == GE ? LT : GE);
11186               continue;
11187             }
11188           break;
11189
11190         case NEG:
11191           /* If testing for equality, we can take the NEG of the constant.  */
11192           if (equality_comparison_p
11193               && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11194             {
11195               op0 = XEXP (op0, 0);
11196               op1 = tem;
11197               continue;
11198             }
11199
11200           /* The remaining cases only apply to comparisons with zero.  */
11201           if (const_op != 0)
11202             break;
11203
11204           /* When X is ABS or is known positive,
11205              (neg X) is < 0 if and only if X != 0.  */
11206
11207           if (sign_bit_comparison_p
11208               && (GET_CODE (XEXP (op0, 0)) == ABS
11209                   || (mode_width <= HOST_BITS_PER_WIDE_INT
11210                       && (nonzero_bits (XEXP (op0, 0), mode)
11211                           & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11212                          == 0)))
11213             {
11214               op0 = XEXP (op0, 0);
11215               code = (code == LT ? NE : EQ);
11216               continue;
11217             }
11218
11219           /* If we have NEG of something whose two high-order bits are the
11220              same, we know that "(-a) < 0" is equivalent to "a > 0".  */
11221           if (num_sign_bit_copies (op0, mode) >= 2)
11222             {
11223               op0 = XEXP (op0, 0);
11224               code = swap_condition (code);
11225               continue;
11226             }
11227           break;
11228
11229         case ROTATE:
11230           /* If we are testing equality and our count is a constant, we
11231              can perform the inverse operation on our RHS.  */
11232           if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11233               && (tem = simplify_binary_operation (ROTATERT, mode,
11234                                                    op1, XEXP (op0, 1))) != 0)
11235             {
11236               op0 = XEXP (op0, 0);
11237               op1 = tem;
11238               continue;
11239             }
11240
11241           /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11242              a particular bit.  Convert it to an AND of a constant of that
11243              bit.  This will be converted into a ZERO_EXTRACT.  */
11244           if (const_op == 0 && sign_bit_comparison_p
11245               && CONST_INT_P (XEXP (op0, 1))
11246               && mode_width <= HOST_BITS_PER_WIDE_INT)
11247             {
11248               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11249                                             ((unsigned HOST_WIDE_INT) 1
11250                                              << (mode_width - 1
11251                                                  - INTVAL (XEXP (op0, 1)))));
11252               code = (code == LT ? NE : EQ);
11253               continue;
11254             }
11255
11256           /* Fall through.  */
11257
11258         case ABS:
11259           /* ABS is ignorable inside an equality comparison with zero.  */
11260           if (const_op == 0 && equality_comparison_p)
11261             {
11262               op0 = XEXP (op0, 0);
11263               continue;
11264             }
11265           break;
11266
11267         case SIGN_EXTEND:
11268           /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11269              (compare FOO CONST) if CONST fits in FOO's mode and we
11270              are either testing inequality or have an unsigned
11271              comparison with ZERO_EXTEND or a signed comparison with
11272              SIGN_EXTEND.  But don't do it if we don't have a compare
11273              insn of the given mode, since we'd have to revert it
11274              later on, and then we wouldn't know whether to sign- or
11275              zero-extend.  */
11276           mode = GET_MODE (XEXP (op0, 0));
11277           if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11278               && ! unsigned_comparison_p
11279               && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11280               && ((unsigned HOST_WIDE_INT) const_op
11281                   < (((unsigned HOST_WIDE_INT) 1
11282                       << (GET_MODE_BITSIZE (mode) - 1))))
11283               && have_insn_for (COMPARE, mode))
11284             {
11285               op0 = XEXP (op0, 0);
11286               continue;
11287             }
11288           break;
11289
11290         case SUBREG:
11291           /* Check for the case where we are comparing A - C1 with C2, that is
11292
11293                (subreg:MODE (plus (A) (-C1))) op (C2)
11294
11295              with C1 a constant, and try to lift the SUBREG, i.e. to do the
11296              comparison in the wider mode.  One of the following two conditions
11297              must be true in order for this to be valid:
11298
11299                1. The mode extension results in the same bit pattern being added
11300                   on both sides and the comparison is equality or unsigned.  As
11301                   C2 has been truncated to fit in MODE, the pattern can only be
11302                   all 0s or all 1s.
11303
11304                2. The mode extension results in the sign bit being copied on
11305                   each side.
11306
11307              The difficulty here is that we have predicates for A but not for
11308              (A - C1) so we need to check that C1 is within proper bounds so
11309              as to perturbate A as little as possible.  */
11310
11311           if (mode_width <= HOST_BITS_PER_WIDE_INT
11312               && subreg_lowpart_p (op0)
11313               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
11314               && GET_CODE (SUBREG_REG (op0)) == PLUS
11315               && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11316             {
11317               enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11318               rtx a = XEXP (SUBREG_REG (op0), 0);
11319               HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11320
11321               if ((c1 > 0
11322                    && (unsigned HOST_WIDE_INT) c1
11323                        < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11324                    && (equality_comparison_p || unsigned_comparison_p)
11325                    /* (A - C1) zero-extends if it is positive and sign-extends
11326                       if it is negative, C2 both zero- and sign-extends.  */
11327                    && ((0 == (nonzero_bits (a, inner_mode)
11328                               & ~GET_MODE_MASK (mode))
11329                         && const_op >= 0)
11330                        /* (A - C1) sign-extends if it is positive and 1-extends
11331                           if it is negative, C2 both sign- and 1-extends.  */
11332                        || (num_sign_bit_copies (a, inner_mode)
11333                            > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
11334                                              - mode_width)
11335                            && const_op < 0)))
11336                   || ((unsigned HOST_WIDE_INT) c1
11337                        < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11338                       /* (A - C1) always sign-extends, like C2.  */
11339                       && num_sign_bit_copies (a, inner_mode)
11340                          > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
11341                                            - (mode_width - 1))))
11342                 {
11343                   op0 = SUBREG_REG (op0);
11344                   continue;
11345                 }
11346             }
11347
11348           /* If the inner mode is narrower and we are extracting the low part,
11349              we can treat the SUBREG as if it were a ZERO_EXTEND.  */
11350           if (subreg_lowpart_p (op0)
11351               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
11352             /* Fall through */ ;
11353           else
11354             break;
11355
11356           /* ... fall through ...  */
11357
11358         case ZERO_EXTEND:
11359           mode = GET_MODE (XEXP (op0, 0));
11360           if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11361               && (unsigned_comparison_p || equality_comparison_p)
11362               && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11363               && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
11364               && have_insn_for (COMPARE, mode))
11365             {
11366               op0 = XEXP (op0, 0);
11367               continue;
11368             }
11369           break;
11370
11371         case PLUS:
11372           /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
11373              this for equality comparisons due to pathological cases involving
11374              overflows.  */
11375           if (equality_comparison_p
11376               && 0 != (tem = simplify_binary_operation (MINUS, mode,
11377                                                         op1, XEXP (op0, 1))))
11378             {
11379               op0 = XEXP (op0, 0);
11380               op1 = tem;
11381               continue;
11382             }
11383
11384           /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
11385           if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11386               && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11387             {
11388               op0 = XEXP (XEXP (op0, 0), 0);
11389               code = (code == LT ? EQ : NE);
11390               continue;
11391             }
11392           break;
11393
11394         case MINUS:
11395           /* We used to optimize signed comparisons against zero, but that
11396              was incorrect.  Unsigned comparisons against zero (GTU, LEU)
11397              arrive here as equality comparisons, or (GEU, LTU) are
11398              optimized away.  No need to special-case them.  */
11399
11400           /* (eq (minus A B) C) -> (eq A (plus B C)) or
11401              (eq B (minus A C)), whichever simplifies.  We can only do
11402              this for equality comparisons due to pathological cases involving
11403              overflows.  */
11404           if (equality_comparison_p
11405               && 0 != (tem = simplify_binary_operation (PLUS, mode,
11406                                                         XEXP (op0, 1), op1)))
11407             {
11408               op0 = XEXP (op0, 0);
11409               op1 = tem;
11410               continue;
11411             }
11412
11413           if (equality_comparison_p
11414               && 0 != (tem = simplify_binary_operation (MINUS, mode,
11415                                                         XEXP (op0, 0), op1)))
11416             {
11417               op0 = XEXP (op0, 1);
11418               op1 = tem;
11419               continue;
11420             }
11421
11422           /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11423              of bits in X minus 1, is one iff X > 0.  */
11424           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11425               && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11426               && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11427               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11428             {
11429               op0 = XEXP (op0, 1);
11430               code = (code == GE ? LE : GT);
11431               continue;
11432             }
11433           break;
11434
11435         case XOR:
11436           /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
11437              if C is zero or B is a constant.  */
11438           if (equality_comparison_p
11439               && 0 != (tem = simplify_binary_operation (XOR, mode,
11440                                                         XEXP (op0, 1), op1)))
11441             {
11442               op0 = XEXP (op0, 0);
11443               op1 = tem;
11444               continue;
11445             }
11446           break;
11447
11448         case EQ:  case NE:
11449         case UNEQ:  case LTGT:
11450         case LT:  case LTU:  case UNLT:  case LE:  case LEU:  case UNLE:
11451         case GT:  case GTU:  case UNGT:  case GE:  case GEU:  case UNGE:
11452         case UNORDERED: case ORDERED:
11453           /* We can't do anything if OP0 is a condition code value, rather
11454              than an actual data value.  */
11455           if (const_op != 0
11456               || CC0_P (XEXP (op0, 0))
11457               || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11458             break;
11459
11460           /* Get the two operands being compared.  */
11461           if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11462             tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11463           else
11464             tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11465
11466           /* Check for the cases where we simply want the result of the
11467              earlier test or the opposite of that result.  */
11468           if (code == NE || code == EQ
11469               || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
11470                   && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11471                   && (STORE_FLAG_VALUE
11472                       & (((unsigned HOST_WIDE_INT) 1
11473                           << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
11474                   && (code == LT || code == GE)))
11475             {
11476               enum rtx_code new_code;
11477               if (code == LT || code == NE)
11478                 new_code = GET_CODE (op0);
11479               else
11480                 new_code = reversed_comparison_code (op0, NULL);
11481
11482               if (new_code != UNKNOWN)
11483                 {
11484                   code = new_code;
11485                   op0 = tem;
11486                   op1 = tem1;
11487                   continue;
11488                 }
11489             }
11490           break;
11491
11492         case IOR:
11493           /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11494              iff X <= 0.  */
11495           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11496               && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11497               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11498             {
11499               op0 = XEXP (op0, 1);
11500               code = (code == GE ? GT : LE);
11501               continue;
11502             }
11503           break;
11504
11505         case AND:
11506           /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
11507              will be converted to a ZERO_EXTRACT later.  */
11508           if (const_op == 0 && equality_comparison_p
11509               && GET_CODE (XEXP (op0, 0)) == ASHIFT
11510               && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11511             {
11512               op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11513                                       XEXP (XEXP (op0, 0), 1));
11514               op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11515               continue;
11516             }
11517
11518           /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11519              zero and X is a comparison and C1 and C2 describe only bits set
11520              in STORE_FLAG_VALUE, we can compare with X.  */
11521           if (const_op == 0 && equality_comparison_p
11522               && mode_width <= HOST_BITS_PER_WIDE_INT
11523               && CONST_INT_P (XEXP (op0, 1))
11524               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11525               && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11526               && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11527               && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11528             {
11529               mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11530                       << INTVAL (XEXP (XEXP (op0, 0), 1)));
11531               if ((~STORE_FLAG_VALUE & mask) == 0
11532                   && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11533                       || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11534                           && COMPARISON_P (tem))))
11535                 {
11536                   op0 = XEXP (XEXP (op0, 0), 0);
11537                   continue;
11538                 }
11539             }
11540
11541           /* If we are doing an equality comparison of an AND of a bit equal
11542              to the sign bit, replace this with a LT or GE comparison of
11543              the underlying value.  */
11544           if (equality_comparison_p
11545               && const_op == 0
11546               && CONST_INT_P (XEXP (op0, 1))
11547               && mode_width <= HOST_BITS_PER_WIDE_INT
11548               && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11549                   == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11550             {
11551               op0 = XEXP (op0, 0);
11552               code = (code == EQ ? GE : LT);
11553               continue;
11554             }
11555
11556           /* If this AND operation is really a ZERO_EXTEND from a narrower
11557              mode, the constant fits within that mode, and this is either an
11558              equality or unsigned comparison, try to do this comparison in
11559              the narrower mode.
11560
11561              Note that in:
11562
11563              (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11564              -> (ne:DI (reg:SI 4) (const_int 0))
11565
11566              unless TRULY_NOOP_TRUNCATION allows it or the register is
11567              known to hold a value of the required mode the
11568              transformation is invalid.  */
11569           if ((equality_comparison_p || unsigned_comparison_p)
11570               && CONST_INT_P (XEXP (op0, 1))
11571               && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11572                                    & GET_MODE_MASK (mode))
11573                                   + 1)) >= 0
11574               && const_op >> i == 0
11575               && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11576               && (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
11577                                          GET_MODE_BITSIZE (GET_MODE (op0)))
11578                   || (REG_P (XEXP (op0, 0))
11579                       && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11580             {
11581               op0 = gen_lowpart (tmode, XEXP (op0, 0));
11582               continue;
11583             }
11584
11585           /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11586              fits in both M1 and M2 and the SUBREG is either paradoxical
11587              or represents the low part, permute the SUBREG and the AND
11588              and try again.  */
11589           if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11590             {
11591               unsigned HOST_WIDE_INT c1;
11592               tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11593               /* Require an integral mode, to avoid creating something like
11594                  (AND:SF ...).  */
11595               if (SCALAR_INT_MODE_P (tmode)
11596                   /* It is unsafe to commute the AND into the SUBREG if the
11597                      SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11598                      not defined.  As originally written the upper bits
11599                      have a defined value due to the AND operation.
11600                      However, if we commute the AND inside the SUBREG then
11601                      they no longer have defined values and the meaning of
11602                      the code has been changed.  */
11603                   && (0
11604 #ifdef WORD_REGISTER_OPERATIONS
11605                       || (mode_width > GET_MODE_BITSIZE (tmode)
11606                           && mode_width <= BITS_PER_WORD)
11607 #endif
11608                       || (mode_width <= GET_MODE_BITSIZE (tmode)
11609                           && subreg_lowpart_p (XEXP (op0, 0))))
11610                   && CONST_INT_P (XEXP (op0, 1))
11611                   && mode_width <= HOST_BITS_PER_WIDE_INT
11612                   && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
11613                   && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11614                   && (c1 & ~GET_MODE_MASK (tmode)) == 0
11615                   && c1 != mask
11616                   && c1 != GET_MODE_MASK (tmode))
11617                 {
11618                   op0 = simplify_gen_binary (AND, tmode,
11619                                              SUBREG_REG (XEXP (op0, 0)),
11620                                              gen_int_mode (c1, tmode));
11621                   op0 = gen_lowpart (mode, op0);
11622                   continue;
11623                 }
11624             }
11625
11626           /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0).  */
11627           if (const_op == 0 && equality_comparison_p
11628               && XEXP (op0, 1) == const1_rtx
11629               && GET_CODE (XEXP (op0, 0)) == NOT)
11630             {
11631               op0 = simplify_and_const_int (NULL_RTX, mode,
11632                                             XEXP (XEXP (op0, 0), 0), 1);
11633               code = (code == NE ? EQ : NE);
11634               continue;
11635             }
11636
11637           /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11638              (eq (and (lshiftrt X) 1) 0).
11639              Also handle the case where (not X) is expressed using xor.  */
11640           if (const_op == 0 && equality_comparison_p
11641               && XEXP (op0, 1) == const1_rtx
11642               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11643             {
11644               rtx shift_op = XEXP (XEXP (op0, 0), 0);
11645               rtx shift_count = XEXP (XEXP (op0, 0), 1);
11646
11647               if (GET_CODE (shift_op) == NOT
11648                   || (GET_CODE (shift_op) == XOR
11649                       && CONST_INT_P (XEXP (shift_op, 1))
11650                       && CONST_INT_P (shift_count)
11651                       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
11652                       && (UINTVAL (XEXP (shift_op, 1))
11653                           == (unsigned HOST_WIDE_INT) 1
11654                                << INTVAL (shift_count))))
11655                 {
11656                   op0
11657                     = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
11658                   op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11659                   code = (code == NE ? EQ : NE);
11660                   continue;
11661                 }
11662             }
11663           break;
11664
11665         case ASHIFT:
11666           /* If we have (compare (ashift FOO N) (const_int C)) and
11667              the high order N bits of FOO (N+1 if an inequality comparison)
11668              are known to be zero, we can do this by comparing FOO with C
11669              shifted right N bits so long as the low-order N bits of C are
11670              zero.  */
11671           if (CONST_INT_P (XEXP (op0, 1))
11672               && INTVAL (XEXP (op0, 1)) >= 0
11673               && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11674                   < HOST_BITS_PER_WIDE_INT)
11675               && (((unsigned HOST_WIDE_INT) const_op
11676                    & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
11677                       - 1)) == 0)
11678               && mode_width <= HOST_BITS_PER_WIDE_INT
11679               && (nonzero_bits (XEXP (op0, 0), mode)
11680                   & ~(mask >> (INTVAL (XEXP (op0, 1))
11681                                + ! equality_comparison_p))) == 0)
11682             {
11683               /* We must perform a logical shift, not an arithmetic one,
11684                  as we want the top N bits of C to be zero.  */
11685               unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11686
11687               temp >>= INTVAL (XEXP (op0, 1));
11688               op1 = gen_int_mode (temp, mode);
11689               op0 = XEXP (op0, 0);
11690               continue;
11691             }
11692
11693           /* If we are doing a sign bit comparison, it means we are testing
11694              a particular bit.  Convert it to the appropriate AND.  */
11695           if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
11696               && mode_width <= HOST_BITS_PER_WIDE_INT)
11697             {
11698               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11699                                             ((unsigned HOST_WIDE_INT) 1
11700                                              << (mode_width - 1
11701                                                  - INTVAL (XEXP (op0, 1)))));
11702               code = (code == LT ? NE : EQ);
11703               continue;
11704             }
11705
11706           /* If this an equality comparison with zero and we are shifting
11707              the low bit to the sign bit, we can convert this to an AND of the
11708              low-order bit.  */
11709           if (const_op == 0 && equality_comparison_p
11710               && CONST_INT_P (XEXP (op0, 1))
11711               && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11712             {
11713               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
11714               continue;
11715             }
11716           break;
11717
11718         case ASHIFTRT:
11719           /* If this is an equality comparison with zero, we can do this
11720              as a logical shift, which might be much simpler.  */
11721           if (equality_comparison_p && const_op == 0
11722               && CONST_INT_P (XEXP (op0, 1)))
11723             {
11724               op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11725                                           XEXP (op0, 0),
11726                                           INTVAL (XEXP (op0, 1)));
11727               continue;
11728             }
11729
11730           /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11731              do the comparison in a narrower mode.  */
11732           if (! unsigned_comparison_p
11733               && CONST_INT_P (XEXP (op0, 1))
11734               && GET_CODE (XEXP (op0, 0)) == ASHIFT
11735               && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11736               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11737                                          MODE_INT, 1)) != BLKmode
11738               && (((unsigned HOST_WIDE_INT) const_op
11739                    + (GET_MODE_MASK (tmode) >> 1) + 1)
11740                   <= GET_MODE_MASK (tmode)))
11741             {
11742               op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
11743               continue;
11744             }
11745
11746           /* Likewise if OP0 is a PLUS of a sign extension with a
11747              constant, which is usually represented with the PLUS
11748              between the shifts.  */
11749           if (! unsigned_comparison_p
11750               && CONST_INT_P (XEXP (op0, 1))
11751               && GET_CODE (XEXP (op0, 0)) == PLUS
11752               && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11753               && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11754               && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11755               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11756                                          MODE_INT, 1)) != BLKmode
11757               && (((unsigned HOST_WIDE_INT) const_op
11758                    + (GET_MODE_MASK (tmode) >> 1) + 1)
11759                   <= GET_MODE_MASK (tmode)))
11760             {
11761               rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11762               rtx add_const = XEXP (XEXP (op0, 0), 1);
11763               rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
11764                                                    add_const, XEXP (op0, 1));
11765
11766               op0 = simplify_gen_binary (PLUS, tmode,
11767                                          gen_lowpart (tmode, inner),
11768                                          new_const);
11769               continue;
11770             }
11771
11772           /* ... fall through ...  */
11773         case LSHIFTRT:
11774           /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11775              the low order N bits of FOO are known to be zero, we can do this
11776              by comparing FOO with C shifted left N bits so long as no
11777              overflow occurs.  Even if the low order N bits of FOO aren't known
11778              to be zero, if the comparison is >= or < we can use the same
11779              optimization and for > or <= by setting all the low
11780              order N bits in the comparison constant.  */
11781           if (CONST_INT_P (XEXP (op0, 1))
11782               && INTVAL (XEXP (op0, 1)) > 0
11783               && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11784               && mode_width <= HOST_BITS_PER_WIDE_INT
11785               && (((unsigned HOST_WIDE_INT) const_op
11786                    + (GET_CODE (op0) != LSHIFTRT
11787                       ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11788                          + 1)
11789                       : 0))
11790                   <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11791             {
11792               unsigned HOST_WIDE_INT low_bits
11793                 = (nonzero_bits (XEXP (op0, 0), mode)
11794                    & (((unsigned HOST_WIDE_INT) 1
11795                        << INTVAL (XEXP (op0, 1))) - 1));
11796               if (low_bits == 0 || !equality_comparison_p)
11797                 {
11798                   /* If the shift was logical, then we must make the condition
11799                      unsigned.  */
11800                   if (GET_CODE (op0) == LSHIFTRT)
11801                     code = unsigned_condition (code);
11802
11803                   const_op <<= INTVAL (XEXP (op0, 1));
11804                   if (low_bits != 0
11805                       && (code == GT || code == GTU
11806                           || code == LE || code == LEU))
11807                     const_op
11808                       |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
11809                   op1 = GEN_INT (const_op);
11810                   op0 = XEXP (op0, 0);
11811                   continue;
11812                 }
11813             }
11814
11815           /* If we are using this shift to extract just the sign bit, we
11816              can replace this with an LT or GE comparison.  */
11817           if (const_op == 0
11818               && (equality_comparison_p || sign_bit_comparison_p)
11819               && CONST_INT_P (XEXP (op0, 1))
11820               && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11821             {
11822               op0 = XEXP (op0, 0);
11823               code = (code == NE || code == GT ? LT : GE);
11824               continue;
11825             }
11826           break;
11827
11828         default:
11829           break;
11830         }
11831
11832       break;
11833     }
11834
11835   /* Now make any compound operations involved in this comparison.  Then,
11836      check for an outmost SUBREG on OP0 that is not doing anything or is
11837      paradoxical.  The latter transformation must only be performed when
11838      it is known that the "extra" bits will be the same in op0 and op1 or
11839      that they don't matter.  There are three cases to consider:
11840
11841      1. SUBREG_REG (op0) is a register.  In this case the bits are don't
11842      care bits and we can assume they have any convenient value.  So
11843      making the transformation is safe.
11844
11845      2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11846      In this case the upper bits of op0 are undefined.  We should not make
11847      the simplification in that case as we do not know the contents of
11848      those bits.
11849
11850      3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11851      UNKNOWN.  In that case we know those bits are zeros or ones.  We must
11852      also be sure that they are the same as the upper bits of op1.
11853
11854      We can never remove a SUBREG for a non-equality comparison because
11855      the sign bit is in a different place in the underlying object.  */
11856
11857   op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11858   op1 = make_compound_operation (op1, SET);
11859
11860   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11861       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11862       && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11863       && (code == NE || code == EQ))
11864     {
11865       if (GET_MODE_SIZE (GET_MODE (op0))
11866           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
11867         {
11868           /* For paradoxical subregs, allow case 1 as above.  Case 3 isn't
11869              implemented.  */
11870           if (REG_P (SUBREG_REG (op0)))
11871             {
11872               op0 = SUBREG_REG (op0);
11873               op1 = gen_lowpart (GET_MODE (op0), op1);
11874             }
11875         }
11876       else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11877                 <= HOST_BITS_PER_WIDE_INT)
11878                && (nonzero_bits (SUBREG_REG (op0),
11879                                  GET_MODE (SUBREG_REG (op0)))
11880                    & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11881         {
11882           tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11883
11884           if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11885                & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11886             op0 = SUBREG_REG (op0), op1 = tem;
11887         }
11888     }
11889
11890   /* We now do the opposite procedure: Some machines don't have compare
11891      insns in all modes.  If OP0's mode is an integer mode smaller than a
11892      word and we can't do a compare in that mode, see if there is a larger
11893      mode for which we can do the compare.  There are a number of cases in
11894      which we can use the wider mode.  */
11895
11896   mode = GET_MODE (op0);
11897   if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11898       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11899       && ! have_insn_for (COMPARE, mode))
11900     for (tmode = GET_MODE_WIDER_MODE (mode);
11901          (tmode != VOIDmode
11902           && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11903          tmode = GET_MODE_WIDER_MODE (tmode))
11904       if (have_insn_for (COMPARE, tmode))
11905         {
11906           int zero_extended;
11907
11908           /* If this is a test for negative, we can make an explicit
11909              test of the sign bit.  Test this first so we can use
11910              a paradoxical subreg to extend OP0.  */
11911
11912           if (op1 == const0_rtx && (code == LT || code == GE)
11913               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11914             {
11915               op0 = simplify_gen_binary (AND, tmode,
11916                                          gen_lowpart (tmode, op0),
11917                                          GEN_INT ((unsigned HOST_WIDE_INT) 1
11918                                                   << (GET_MODE_BITSIZE (mode)
11919                                                       - 1)));
11920               code = (code == LT) ? NE : EQ;
11921               break;
11922             }
11923
11924           /* If the only nonzero bits in OP0 and OP1 are those in the
11925              narrower mode and this is an equality or unsigned comparison,
11926              we can use the wider mode.  Similarly for sign-extended
11927              values, in which case it is true for all comparisons.  */
11928           zero_extended = ((code == EQ || code == NE
11929                             || code == GEU || code == GTU
11930                             || code == LEU || code == LTU)
11931                            && (nonzero_bits (op0, tmode)
11932                                & ~GET_MODE_MASK (mode)) == 0
11933                            && ((CONST_INT_P (op1)
11934                                 || (nonzero_bits (op1, tmode)
11935                                     & ~GET_MODE_MASK (mode)) == 0)));
11936
11937           if (zero_extended
11938               || ((num_sign_bit_copies (op0, tmode)
11939                    > (unsigned int) (GET_MODE_BITSIZE (tmode)
11940                                      - GET_MODE_BITSIZE (mode)))
11941                   && (num_sign_bit_copies (op1, tmode)
11942                       > (unsigned int) (GET_MODE_BITSIZE (tmode)
11943                                         - GET_MODE_BITSIZE (mode)))))
11944             {
11945               /* If OP0 is an AND and we don't have an AND in MODE either,
11946                  make a new AND in the proper mode.  */
11947               if (GET_CODE (op0) == AND
11948                   && !have_insn_for (AND, mode))
11949                 op0 = simplify_gen_binary (AND, tmode,
11950                                            gen_lowpart (tmode,
11951                                                         XEXP (op0, 0)),
11952                                            gen_lowpart (tmode,
11953                                                         XEXP (op0, 1)));
11954               else
11955                 {
11956                   if (zero_extended)
11957                     {
11958                       op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
11959                       op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
11960                     }
11961                   else
11962                     {
11963                       op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
11964                       op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
11965                     }
11966                   break;
11967                 }
11968             }
11969         }
11970
11971 #ifdef CANONICALIZE_COMPARISON
11972   /* If this machine only supports a subset of valid comparisons, see if we
11973      can convert an unsupported one into a supported one.  */
11974   CANONICALIZE_COMPARISON (code, op0, op1);
11975 #endif
11976
11977   *pop0 = op0;
11978   *pop1 = op1;
11979
11980   return code;
11981 }
11982 \f
11983 /* Utility function for record_value_for_reg.  Count number of
11984    rtxs in X.  */
11985 static int
11986 count_rtxs (rtx x)
11987 {
11988   enum rtx_code code = GET_CODE (x);
11989   const char *fmt;
11990   int i, j, ret = 1;
11991
11992   if (GET_RTX_CLASS (code) == '2'
11993       || GET_RTX_CLASS (code) == 'c')
11994     {
11995       rtx x0 = XEXP (x, 0);
11996       rtx x1 = XEXP (x, 1);
11997
11998       if (x0 == x1)
11999         return 1 + 2 * count_rtxs (x0);
12000
12001       if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
12002            || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
12003           && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12004         return 2 + 2 * count_rtxs (x0)
12005                + count_rtxs (x == XEXP (x1, 0)
12006                              ? XEXP (x1, 1) : XEXP (x1, 0));
12007
12008       if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
12009            || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
12010           && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12011         return 2 + 2 * count_rtxs (x1)
12012                + count_rtxs (x == XEXP (x0, 0)
12013                              ? XEXP (x0, 1) : XEXP (x0, 0));
12014     }
12015
12016   fmt = GET_RTX_FORMAT (code);
12017   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12018     if (fmt[i] == 'e')
12019       ret += count_rtxs (XEXP (x, i));
12020     else if (fmt[i] == 'E')
12021       for (j = 0; j < XVECLEN (x, i); j++)
12022         ret += count_rtxs (XVECEXP (x, i, j));
12023
12024   return ret;
12025 }
12026 \f
12027 /* Utility function for following routine.  Called when X is part of a value
12028    being stored into last_set_value.  Sets last_set_table_tick
12029    for each register mentioned.  Similar to mention_regs in cse.c  */
12030
12031 static void
12032 update_table_tick (rtx x)
12033 {
12034   enum rtx_code code = GET_CODE (x);
12035   const char *fmt = GET_RTX_FORMAT (code);
12036   int i, j;
12037
12038   if (code == REG)
12039     {
12040       unsigned int regno = REGNO (x);
12041       unsigned int endregno = END_REGNO (x);
12042       unsigned int r;
12043
12044       for (r = regno; r < endregno; r++)
12045         {
12046           reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, r);
12047           rsp->last_set_table_tick = label_tick;
12048         }
12049
12050       return;
12051     }
12052
12053   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12054     if (fmt[i] == 'e')
12055       {
12056         /* Check for identical subexpressions.  If x contains
12057            identical subexpression we only have to traverse one of
12058            them.  */
12059         if (i == 0 && ARITHMETIC_P (x))
12060           {
12061             /* Note that at this point x1 has already been
12062                processed.  */
12063             rtx x0 = XEXP (x, 0);
12064             rtx x1 = XEXP (x, 1);
12065
12066             /* If x0 and x1 are identical then there is no need to
12067                process x0.  */
12068             if (x0 == x1)
12069               break;
12070
12071             /* If x0 is identical to a subexpression of x1 then while
12072                processing x1, x0 has already been processed.  Thus we
12073                are done with x.  */
12074             if (ARITHMETIC_P (x1)
12075                 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12076               break;
12077
12078             /* If x1 is identical to a subexpression of x0 then we
12079                still have to process the rest of x0.  */
12080             if (ARITHMETIC_P (x0)
12081                 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12082               {
12083                 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12084                 break;
12085               }
12086           }
12087
12088         update_table_tick (XEXP (x, i));
12089       }
12090     else if (fmt[i] == 'E')
12091       for (j = 0; j < XVECLEN (x, i); j++)
12092         update_table_tick (XVECEXP (x, i, j));
12093 }
12094
12095 /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
12096    are saying that the register is clobbered and we no longer know its
12097    value.  If INSN is zero, don't update reg_stat[].last_set; this is
12098    only permitted with VALUE also zero and is used to invalidate the
12099    register.  */
12100
12101 static void
12102 record_value_for_reg (rtx reg, rtx insn, rtx value)
12103 {
12104   unsigned int regno = REGNO (reg);
12105   unsigned int endregno = END_REGNO (reg);
12106   unsigned int i;
12107   reg_stat_type *rsp;
12108
12109   /* If VALUE contains REG and we have a previous value for REG, substitute
12110      the previous value.  */
12111   if (value && insn && reg_overlap_mentioned_p (reg, value))
12112     {
12113       rtx tem;
12114
12115       /* Set things up so get_last_value is allowed to see anything set up to
12116          our insn.  */
12117       subst_low_luid = DF_INSN_LUID (insn);
12118       tem = get_last_value (reg);
12119
12120       /* If TEM is simply a binary operation with two CLOBBERs as operands,
12121          it isn't going to be useful and will take a lot of time to process,
12122          so just use the CLOBBER.  */
12123
12124       if (tem)
12125         {
12126           if (ARITHMETIC_P (tem)
12127               && GET_CODE (XEXP (tem, 0)) == CLOBBER
12128               && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12129             tem = XEXP (tem, 0);
12130           else if (count_occurrences (value, reg, 1) >= 2)
12131             {
12132               /* If there are two or more occurrences of REG in VALUE,
12133                  prevent the value from growing too much.  */
12134               if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12135                 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12136             }
12137
12138           value = replace_rtx (copy_rtx (value), reg, tem);
12139         }
12140     }
12141
12142   /* For each register modified, show we don't know its value, that
12143      we don't know about its bitwise content, that its value has been
12144      updated, and that we don't know the location of the death of the
12145      register.  */
12146   for (i = regno; i < endregno; i++)
12147     {
12148       rsp = VEC_index (reg_stat_type, reg_stat, i);
12149
12150       if (insn)
12151         rsp->last_set = insn;
12152
12153       rsp->last_set_value = 0;
12154       rsp->last_set_mode = VOIDmode;
12155       rsp->last_set_nonzero_bits = 0;
12156       rsp->last_set_sign_bit_copies = 0;
12157       rsp->last_death = 0;
12158       rsp->truncated_to_mode = VOIDmode;
12159     }
12160
12161   /* Mark registers that are being referenced in this value.  */
12162   if (value)
12163     update_table_tick (value);
12164
12165   /* Now update the status of each register being set.
12166      If someone is using this register in this block, set this register
12167      to invalid since we will get confused between the two lives in this
12168      basic block.  This makes using this register always invalid.  In cse, we
12169      scan the table to invalidate all entries using this register, but this
12170      is too much work for us.  */
12171
12172   for (i = regno; i < endregno; i++)
12173     {
12174       rsp = VEC_index (reg_stat_type, reg_stat, i);
12175       rsp->last_set_label = label_tick;
12176       if (!insn
12177           || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12178         rsp->last_set_invalid = 1;
12179       else
12180         rsp->last_set_invalid = 0;
12181     }
12182
12183   /* The value being assigned might refer to X (like in "x++;").  In that
12184      case, we must replace it with (clobber (const_int 0)) to prevent
12185      infinite loops.  */
12186   rsp = VEC_index (reg_stat_type, reg_stat, regno);
12187   if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12188     {
12189       value = copy_rtx (value);
12190       if (!get_last_value_validate (&value, insn, label_tick, 1))
12191         value = 0;
12192     }
12193
12194   /* For the main register being modified, update the value, the mode, the
12195      nonzero bits, and the number of sign bit copies.  */
12196
12197   rsp->last_set_value = value;
12198
12199   if (value)
12200     {
12201       enum machine_mode mode = GET_MODE (reg);
12202       subst_low_luid = DF_INSN_LUID (insn);
12203       rsp->last_set_mode = mode;
12204       if (GET_MODE_CLASS (mode) == MODE_INT
12205           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12206         mode = nonzero_bits_mode;
12207       rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12208       rsp->last_set_sign_bit_copies
12209         = num_sign_bit_copies (value, GET_MODE (reg));
12210     }
12211 }
12212
12213 /* Called via note_stores from record_dead_and_set_regs to handle one
12214    SET or CLOBBER in an insn.  DATA is the instruction in which the
12215    set is occurring.  */
12216
12217 static void
12218 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12219 {
12220   rtx record_dead_insn = (rtx) data;
12221
12222   if (GET_CODE (dest) == SUBREG)
12223     dest = SUBREG_REG (dest);
12224
12225   if (!record_dead_insn)
12226     {
12227       if (REG_P (dest))
12228         record_value_for_reg (dest, NULL_RTX, NULL_RTX);
12229       return;
12230     }
12231
12232   if (REG_P (dest))
12233     {
12234       /* If we are setting the whole register, we know its value.  Otherwise
12235          show that we don't know the value.  We can handle SUBREG in
12236          some cases.  */
12237       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12238         record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12239       else if (GET_CODE (setter) == SET
12240                && GET_CODE (SET_DEST (setter)) == SUBREG
12241                && SUBREG_REG (SET_DEST (setter)) == dest
12242                && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
12243                && subreg_lowpart_p (SET_DEST (setter)))
12244         record_value_for_reg (dest, record_dead_insn,
12245                               gen_lowpart (GET_MODE (dest),
12246                                                        SET_SRC (setter)));
12247       else
12248         record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12249     }
12250   else if (MEM_P (dest)
12251            /* Ignore pushes, they clobber nothing.  */
12252            && ! push_operand (dest, GET_MODE (dest)))
12253     mem_last_set = DF_INSN_LUID (record_dead_insn);
12254 }
12255
12256 /* Update the records of when each REG was most recently set or killed
12257    for the things done by INSN.  This is the last thing done in processing
12258    INSN in the combiner loop.
12259
12260    We update reg_stat[], in particular fields last_set, last_set_value,
12261    last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12262    last_death, and also the similar information mem_last_set (which insn
12263    most recently modified memory) and last_call_luid (which insn was the
12264    most recent subroutine call).  */
12265
12266 static void
12267 record_dead_and_set_regs (rtx insn)
12268 {
12269   rtx link;
12270   unsigned int i;
12271
12272   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12273     {
12274       if (REG_NOTE_KIND (link) == REG_DEAD
12275           && REG_P (XEXP (link, 0)))
12276         {
12277           unsigned int regno = REGNO (XEXP (link, 0));
12278           unsigned int endregno = END_REGNO (XEXP (link, 0));
12279
12280           for (i = regno; i < endregno; i++)
12281             {
12282               reg_stat_type *rsp;
12283
12284               rsp = VEC_index (reg_stat_type, reg_stat, i);
12285               rsp->last_death = insn;
12286             }
12287         }
12288       else if (REG_NOTE_KIND (link) == REG_INC)
12289         record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12290     }
12291
12292   if (CALL_P (insn))
12293     {
12294       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
12295         if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
12296           {
12297             reg_stat_type *rsp;
12298
12299             rsp = VEC_index (reg_stat_type, reg_stat, i);
12300             rsp->last_set_invalid = 1;
12301             rsp->last_set = insn;
12302             rsp->last_set_value = 0;
12303             rsp->last_set_mode = VOIDmode;
12304             rsp->last_set_nonzero_bits = 0;
12305             rsp->last_set_sign_bit_copies = 0;
12306             rsp->last_death = 0;
12307             rsp->truncated_to_mode = VOIDmode;
12308           }
12309
12310       last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12311
12312       /* We can't combine into a call pattern.  Remember, though, that
12313          the return value register is set at this LUID.  We could
12314          still replace a register with the return value from the
12315          wrong subroutine call!  */
12316       note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12317     }
12318   else
12319     note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12320 }
12321
12322 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12323    register present in the SUBREG, so for each such SUBREG go back and
12324    adjust nonzero and sign bit information of the registers that are
12325    known to have some zero/sign bits set.
12326
12327    This is needed because when combine blows the SUBREGs away, the
12328    information on zero/sign bits is lost and further combines can be
12329    missed because of that.  */
12330
12331 static void
12332 record_promoted_value (rtx insn, rtx subreg)
12333 {
12334   rtx links, set;
12335   unsigned int regno = REGNO (SUBREG_REG (subreg));
12336   enum machine_mode mode = GET_MODE (subreg);
12337
12338   if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
12339     return;
12340
12341   for (links = LOG_LINKS (insn); links;)
12342     {
12343       reg_stat_type *rsp;
12344
12345       insn = XEXP (links, 0);
12346       set = single_set (insn);
12347
12348       if (! set || !REG_P (SET_DEST (set))
12349           || REGNO (SET_DEST (set)) != regno
12350           || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12351         {
12352           links = XEXP (links, 1);
12353           continue;
12354         }
12355
12356       rsp = VEC_index (reg_stat_type, reg_stat, regno);
12357       if (rsp->last_set == insn)
12358         {
12359           if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
12360             rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12361         }
12362
12363       if (REG_P (SET_SRC (set)))
12364         {
12365           regno = REGNO (SET_SRC (set));
12366           links = LOG_LINKS (insn);
12367         }
12368       else
12369         break;
12370     }
12371 }
12372
12373 /* Check if X, a register, is known to contain a value already
12374    truncated to MODE.  In this case we can use a subreg to refer to
12375    the truncated value even though in the generic case we would need
12376    an explicit truncation.  */
12377
12378 static bool
12379 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
12380 {
12381   reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
12382   enum machine_mode truncated = rsp->truncated_to_mode;
12383
12384   if (truncated == 0
12385       || rsp->truncation_label < label_tick_ebb_start)
12386     return false;
12387   if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12388     return true;
12389   if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
12390                              GET_MODE_BITSIZE (truncated)))
12391     return true;
12392   return false;
12393 }
12394
12395 /* Callback for for_each_rtx.  If *P is a hard reg or a subreg record the mode
12396    that the register is accessed in.  For non-TRULY_NOOP_TRUNCATION targets we
12397    might be able to turn a truncate into a subreg using this information.
12398    Return -1 if traversing *P is complete or 0 otherwise.  */
12399
12400 static int
12401 record_truncated_value (rtx *p, void *data ATTRIBUTE_UNUSED)
12402 {
12403   rtx x = *p;
12404   enum machine_mode truncated_mode;
12405   reg_stat_type *rsp;
12406
12407   if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12408     {
12409       enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12410       truncated_mode = GET_MODE (x);
12411
12412       if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12413         return -1;
12414
12415       if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (truncated_mode),
12416                                  GET_MODE_BITSIZE (original_mode)))
12417         return -1;
12418
12419       x = SUBREG_REG (x);
12420     }
12421   /* ??? For hard-regs we now record everything.  We might be able to
12422      optimize this using last_set_mode.  */
12423   else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12424     truncated_mode = GET_MODE (x);
12425   else
12426     return 0;
12427
12428   rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
12429   if (rsp->truncated_to_mode == 0
12430       || rsp->truncation_label < label_tick_ebb_start
12431       || (GET_MODE_SIZE (truncated_mode)
12432           < GET_MODE_SIZE (rsp->truncated_to_mode)))
12433     {
12434       rsp->truncated_to_mode = truncated_mode;
12435       rsp->truncation_label = label_tick;
12436     }
12437
12438   return -1;
12439 }
12440
12441 /* Callback for note_uses.  Find hardregs and subregs of pseudos and
12442    the modes they are used in.  This can help truning TRUNCATEs into
12443    SUBREGs.  */
12444
12445 static void
12446 record_truncated_values (rtx *x, void *data ATTRIBUTE_UNUSED)
12447 {
12448   for_each_rtx (x, record_truncated_value, NULL);
12449 }
12450
12451 /* Scan X for promoted SUBREGs.  For each one found,
12452    note what it implies to the registers used in it.  */
12453
12454 static void
12455 check_promoted_subreg (rtx insn, rtx x)
12456 {
12457   if (GET_CODE (x) == SUBREG
12458       && SUBREG_PROMOTED_VAR_P (x)
12459       && REG_P (SUBREG_REG (x)))
12460     record_promoted_value (insn, x);
12461   else
12462     {
12463       const char *format = GET_RTX_FORMAT (GET_CODE (x));
12464       int i, j;
12465
12466       for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12467         switch (format[i])
12468           {
12469           case 'e':
12470             check_promoted_subreg (insn, XEXP (x, i));
12471             break;
12472           case 'V':
12473           case 'E':
12474             if (XVEC (x, i) != 0)
12475               for (j = 0; j < XVECLEN (x, i); j++)
12476                 check_promoted_subreg (insn, XVECEXP (x, i, j));
12477             break;
12478           }
12479     }
12480 }
12481 \f
12482 /* Verify that all the registers and memory references mentioned in *LOC are
12483    still valid.  *LOC was part of a value set in INSN when label_tick was
12484    equal to TICK.  Return 0 if some are not.  If REPLACE is nonzero, replace
12485    the invalid references with (clobber (const_int 0)) and return 1.  This
12486    replacement is useful because we often can get useful information about
12487    the form of a value (e.g., if it was produced by a shift that always
12488    produces -1 or 0) even though we don't know exactly what registers it
12489    was produced from.  */
12490
12491 static int
12492 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
12493 {
12494   rtx x = *loc;
12495   const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12496   int len = GET_RTX_LENGTH (GET_CODE (x));
12497   int i, j;
12498
12499   if (REG_P (x))
12500     {
12501       unsigned int regno = REGNO (x);
12502       unsigned int endregno = END_REGNO (x);
12503       unsigned int j;
12504
12505       for (j = regno; j < endregno; j++)
12506         {
12507           reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, j);
12508           if (rsp->last_set_invalid
12509               /* If this is a pseudo-register that was only set once and not
12510                  live at the beginning of the function, it is always valid.  */
12511               || (! (regno >= FIRST_PSEUDO_REGISTER
12512                      && REG_N_SETS (regno) == 1
12513                      && (!REGNO_REG_SET_P
12514                          (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno)))
12515                   && rsp->last_set_label > tick))
12516           {
12517             if (replace)
12518               *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12519             return replace;
12520           }
12521         }
12522
12523       return 1;
12524     }
12525   /* If this is a memory reference, make sure that there were no stores after
12526      it that might have clobbered the value.  We don't have alias info, so we
12527      assume any store invalidates it.  Moreover, we only have local UIDs, so
12528      we also assume that there were stores in the intervening basic blocks.  */
12529   else if (MEM_P (x) && !MEM_READONLY_P (x)
12530            && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12531     {
12532       if (replace)
12533         *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12534       return replace;
12535     }
12536
12537   for (i = 0; i < len; i++)
12538     {
12539       if (fmt[i] == 'e')
12540         {
12541           /* Check for identical subexpressions.  If x contains
12542              identical subexpression we only have to traverse one of
12543              them.  */
12544           if (i == 1 && ARITHMETIC_P (x))
12545             {
12546               /* Note that at this point x0 has already been checked
12547                  and found valid.  */
12548               rtx x0 = XEXP (x, 0);
12549               rtx x1 = XEXP (x, 1);
12550
12551               /* If x0 and x1 are identical then x is also valid.  */
12552               if (x0 == x1)
12553                 return 1;
12554
12555               /* If x1 is identical to a subexpression of x0 then
12556                  while checking x0, x1 has already been checked.  Thus
12557                  it is valid and so as x.  */
12558               if (ARITHMETIC_P (x0)
12559                   && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12560                 return 1;
12561
12562               /* If x0 is identical to a subexpression of x1 then x is
12563                  valid iff the rest of x1 is valid.  */
12564               if (ARITHMETIC_P (x1)
12565                   && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12566                 return
12567                   get_last_value_validate (&XEXP (x1,
12568                                                   x0 == XEXP (x1, 0) ? 1 : 0),
12569                                            insn, tick, replace);
12570             }
12571
12572           if (get_last_value_validate (&XEXP (x, i), insn, tick,
12573                                        replace) == 0)
12574             return 0;
12575         }
12576       else if (fmt[i] == 'E')
12577         for (j = 0; j < XVECLEN (x, i); j++)
12578           if (get_last_value_validate (&XVECEXP (x, i, j),
12579                                        insn, tick, replace) == 0)
12580             return 0;
12581     }
12582
12583   /* If we haven't found a reason for it to be invalid, it is valid.  */
12584   return 1;
12585 }
12586
12587 /* Get the last value assigned to X, if known.  Some registers
12588    in the value may be replaced with (clobber (const_int 0)) if their value
12589    is known longer known reliably.  */
12590
12591 static rtx
12592 get_last_value (const_rtx x)
12593 {
12594   unsigned int regno;
12595   rtx value;
12596   reg_stat_type *rsp;
12597
12598   /* If this is a non-paradoxical SUBREG, get the value of its operand and
12599      then convert it to the desired mode.  If this is a paradoxical SUBREG,
12600      we cannot predict what values the "extra" bits might have.  */
12601   if (GET_CODE (x) == SUBREG
12602       && subreg_lowpart_p (x)
12603       && (GET_MODE_SIZE (GET_MODE (x))
12604           <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
12605       && (value = get_last_value (SUBREG_REG (x))) != 0)
12606     return gen_lowpart (GET_MODE (x), value);
12607
12608   if (!REG_P (x))
12609     return 0;
12610
12611   regno = REGNO (x);
12612   rsp = VEC_index (reg_stat_type, reg_stat, regno);
12613   value = rsp->last_set_value;
12614
12615   /* If we don't have a value, or if it isn't for this basic block and
12616      it's either a hard register, set more than once, or it's a live
12617      at the beginning of the function, return 0.
12618
12619      Because if it's not live at the beginning of the function then the reg
12620      is always set before being used (is never used without being set).
12621      And, if it's set only once, and it's always set before use, then all
12622      uses must have the same last value, even if it's not from this basic
12623      block.  */
12624
12625   if (value == 0
12626       || (rsp->last_set_label < label_tick_ebb_start
12627           && (regno < FIRST_PSEUDO_REGISTER
12628               || REG_N_SETS (regno) != 1
12629               || REGNO_REG_SET_P
12630                  (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno))))
12631     return 0;
12632
12633   /* If the value was set in a later insn than the ones we are processing,
12634      we can't use it even if the register was only set once.  */
12635   if (rsp->last_set_label == label_tick
12636       && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12637     return 0;
12638
12639   /* If the value has all its registers valid, return it.  */
12640   if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12641     return value;
12642
12643   /* Otherwise, make a copy and replace any invalid register with
12644      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
12645
12646   value = copy_rtx (value);
12647   if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
12648     return value;
12649
12650   return 0;
12651 }
12652 \f
12653 /* Return nonzero if expression X refers to a REG or to memory
12654    that is set in an instruction more recent than FROM_LUID.  */
12655
12656 static int
12657 use_crosses_set_p (const_rtx x, int from_luid)
12658 {
12659   const char *fmt;
12660   int i;
12661   enum rtx_code code = GET_CODE (x);
12662
12663   if (code == REG)
12664     {
12665       unsigned int regno = REGNO (x);
12666       unsigned endreg = END_REGNO (x);
12667
12668 #ifdef PUSH_ROUNDING
12669       /* Don't allow uses of the stack pointer to be moved,
12670          because we don't know whether the move crosses a push insn.  */
12671       if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12672         return 1;
12673 #endif
12674       for (; regno < endreg; regno++)
12675         {
12676           reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
12677           if (rsp->last_set
12678               && rsp->last_set_label == label_tick
12679               && DF_INSN_LUID (rsp->last_set) > from_luid)
12680             return 1;
12681         }
12682       return 0;
12683     }
12684
12685   if (code == MEM && mem_last_set > from_luid)
12686     return 1;
12687
12688   fmt = GET_RTX_FORMAT (code);
12689
12690   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12691     {
12692       if (fmt[i] == 'E')
12693         {
12694           int j;
12695           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12696             if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
12697               return 1;
12698         }
12699       else if (fmt[i] == 'e'
12700                && use_crosses_set_p (XEXP (x, i), from_luid))
12701         return 1;
12702     }
12703   return 0;
12704 }
12705 \f
12706 /* Define three variables used for communication between the following
12707    routines.  */
12708
12709 static unsigned int reg_dead_regno, reg_dead_endregno;
12710 static int reg_dead_flag;
12711
12712 /* Function called via note_stores from reg_dead_at_p.
12713
12714    If DEST is within [reg_dead_regno, reg_dead_endregno), set
12715    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
12716
12717 static void
12718 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
12719 {
12720   unsigned int regno, endregno;
12721
12722   if (!REG_P (dest))
12723     return;
12724
12725   regno = REGNO (dest);
12726   endregno = END_REGNO (dest);
12727   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12728     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12729 }
12730
12731 /* Return nonzero if REG is known to be dead at INSN.
12732
12733    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
12734    referencing REG, it is dead.  If we hit a SET referencing REG, it is
12735    live.  Otherwise, see if it is live or dead at the start of the basic
12736    block we are in.  Hard regs marked as being live in NEWPAT_USED_REGS
12737    must be assumed to be always live.  */
12738
12739 static int
12740 reg_dead_at_p (rtx reg, rtx insn)
12741 {
12742   basic_block block;
12743   unsigned int i;
12744
12745   /* Set variables for reg_dead_at_p_1.  */
12746   reg_dead_regno = REGNO (reg);
12747   reg_dead_endregno = END_REGNO (reg);
12748
12749   reg_dead_flag = 0;
12750
12751   /* Check that reg isn't mentioned in NEWPAT_USED_REGS.  For fixed registers
12752      we allow the machine description to decide whether use-and-clobber
12753      patterns are OK.  */
12754   if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12755     {
12756       for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12757         if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
12758           return 0;
12759     }
12760
12761   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
12762      beginning of basic block.  */
12763   block = BLOCK_FOR_INSN (insn);
12764   for (;;)
12765     {
12766       if (INSN_P (insn))
12767         {
12768           note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12769           if (reg_dead_flag)
12770             return reg_dead_flag == 1 ? 1 : 0;
12771
12772           if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12773             return 1;
12774         }
12775
12776       if (insn == BB_HEAD (block))
12777         break;
12778
12779       insn = PREV_INSN (insn);
12780     }
12781
12782   /* Look at live-in sets for the basic block that we were in.  */
12783   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12784     if (REGNO_REG_SET_P (df_get_live_in (block), i))
12785       return 0;
12786
12787   return 1;
12788 }
12789 \f
12790 /* Note hard registers in X that are used.  */
12791
12792 static void
12793 mark_used_regs_combine (rtx x)
12794 {
12795   RTX_CODE code = GET_CODE (x);
12796   unsigned int regno;
12797   int i;
12798
12799   switch (code)
12800     {
12801     case LABEL_REF:
12802     case SYMBOL_REF:
12803     case CONST_INT:
12804     case CONST:
12805     case CONST_DOUBLE:
12806     case CONST_VECTOR:
12807     case PC:
12808     case ADDR_VEC:
12809     case ADDR_DIFF_VEC:
12810     case ASM_INPUT:
12811 #ifdef HAVE_cc0
12812     /* CC0 must die in the insn after it is set, so we don't need to take
12813        special note of it here.  */
12814     case CC0:
12815 #endif
12816       return;
12817
12818     case CLOBBER:
12819       /* If we are clobbering a MEM, mark any hard registers inside the
12820          address as used.  */
12821       if (MEM_P (XEXP (x, 0)))
12822         mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12823       return;
12824
12825     case REG:
12826       regno = REGNO (x);
12827       /* A hard reg in a wide mode may really be multiple registers.
12828          If so, mark all of them just like the first.  */
12829       if (regno < FIRST_PSEUDO_REGISTER)
12830         {
12831           /* None of this applies to the stack, frame or arg pointers.  */
12832           if (regno == STACK_POINTER_REGNUM
12833 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
12834               || regno == HARD_FRAME_POINTER_REGNUM
12835 #endif
12836 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12837               || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12838 #endif
12839               || regno == FRAME_POINTER_REGNUM)
12840             return;
12841
12842           add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
12843         }
12844       return;
12845
12846     case SET:
12847       {
12848         /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12849            the address.  */
12850         rtx testreg = SET_DEST (x);
12851
12852         while (GET_CODE (testreg) == SUBREG
12853                || GET_CODE (testreg) == ZERO_EXTRACT
12854                || GET_CODE (testreg) == STRICT_LOW_PART)
12855           testreg = XEXP (testreg, 0);
12856
12857         if (MEM_P (testreg))
12858           mark_used_regs_combine (XEXP (testreg, 0));
12859
12860         mark_used_regs_combine (SET_SRC (x));
12861       }
12862       return;
12863
12864     default:
12865       break;
12866     }
12867
12868   /* Recursively scan the operands of this expression.  */
12869
12870   {
12871     const char *fmt = GET_RTX_FORMAT (code);
12872
12873     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12874       {
12875         if (fmt[i] == 'e')
12876           mark_used_regs_combine (XEXP (x, i));
12877         else if (fmt[i] == 'E')
12878           {
12879             int j;
12880
12881             for (j = 0; j < XVECLEN (x, i); j++)
12882               mark_used_regs_combine (XVECEXP (x, i, j));
12883           }
12884       }
12885   }
12886 }
12887 \f
12888 /* Remove register number REGNO from the dead registers list of INSN.
12889
12890    Return the note used to record the death, if there was one.  */
12891
12892 rtx
12893 remove_death (unsigned int regno, rtx insn)
12894 {
12895   rtx note = find_regno_note (insn, REG_DEAD, regno);
12896
12897   if (note)
12898     remove_note (insn, note);
12899
12900   return note;
12901 }
12902
12903 /* For each register (hardware or pseudo) used within expression X, if its
12904    death is in an instruction with luid between FROM_LUID (inclusive) and
12905    TO_INSN (exclusive), put a REG_DEAD note for that register in the
12906    list headed by PNOTES.
12907
12908    That said, don't move registers killed by maybe_kill_insn.
12909
12910    This is done when X is being merged by combination into TO_INSN.  These
12911    notes will then be distributed as needed.  */
12912
12913 static void
12914 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx to_insn,
12915              rtx *pnotes)
12916 {
12917   const char *fmt;
12918   int len, i;
12919   enum rtx_code code = GET_CODE (x);
12920
12921   if (code == REG)
12922     {
12923       unsigned int regno = REGNO (x);
12924       rtx where_dead = VEC_index (reg_stat_type, reg_stat, regno)->last_death;
12925
12926       /* Don't move the register if it gets killed in between from and to.  */
12927       if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12928           && ! reg_referenced_p (x, maybe_kill_insn))
12929         return;
12930
12931       if (where_dead
12932           && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
12933           && DF_INSN_LUID (where_dead) >= from_luid
12934           && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
12935         {
12936           rtx note = remove_death (regno, where_dead);
12937
12938           /* It is possible for the call above to return 0.  This can occur
12939              when last_death points to I2 or I1 that we combined with.
12940              In that case make a new note.
12941
12942              We must also check for the case where X is a hard register
12943              and NOTE is a death note for a range of hard registers
12944              including X.  In that case, we must put REG_DEAD notes for
12945              the remaining registers in place of NOTE.  */
12946
12947           if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12948               && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12949                   > GET_MODE_SIZE (GET_MODE (x))))
12950             {
12951               unsigned int deadregno = REGNO (XEXP (note, 0));
12952               unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
12953               unsigned int ourend = END_HARD_REGNO (x);
12954               unsigned int i;
12955
12956               for (i = deadregno; i < deadend; i++)
12957                 if (i < regno || i >= ourend)
12958                   add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
12959             }
12960
12961           /* If we didn't find any note, or if we found a REG_DEAD note that
12962              covers only part of the given reg, and we have a multi-reg hard
12963              register, then to be safe we must check for REG_DEAD notes
12964              for each register other than the first.  They could have
12965              their own REG_DEAD notes lying around.  */
12966           else if ((note == 0
12967                     || (note != 0
12968                         && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12969                             < GET_MODE_SIZE (GET_MODE (x)))))
12970                    && regno < FIRST_PSEUDO_REGISTER
12971                    && hard_regno_nregs[regno][GET_MODE (x)] > 1)
12972             {
12973               unsigned int ourend = END_HARD_REGNO (x);
12974               unsigned int i, offset;
12975               rtx oldnotes = 0;
12976
12977               if (note)
12978                 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
12979               else
12980                 offset = 1;
12981
12982               for (i = regno + offset; i < ourend; i++)
12983                 move_deaths (regno_reg_rtx[i],
12984                              maybe_kill_insn, from_luid, to_insn, &oldnotes);
12985             }
12986
12987           if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12988             {
12989               XEXP (note, 1) = *pnotes;
12990               *pnotes = note;
12991             }
12992           else
12993             *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
12994         }
12995
12996       return;
12997     }
12998
12999   else if (GET_CODE (x) == SET)
13000     {
13001       rtx dest = SET_DEST (x);
13002
13003       move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13004
13005       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13006          that accesses one word of a multi-word item, some
13007          piece of everything register in the expression is used by
13008          this insn, so remove any old death.  */
13009       /* ??? So why do we test for equality of the sizes?  */
13010
13011       if (GET_CODE (dest) == ZERO_EXTRACT
13012           || GET_CODE (dest) == STRICT_LOW_PART
13013           || (GET_CODE (dest) == SUBREG
13014               && (((GET_MODE_SIZE (GET_MODE (dest))
13015                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13016                   == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13017                        + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13018         {
13019           move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13020           return;
13021         }
13022
13023       /* If this is some other SUBREG, we know it replaces the entire
13024          value, so use that as the destination.  */
13025       if (GET_CODE (dest) == SUBREG)
13026         dest = SUBREG_REG (dest);
13027
13028       /* If this is a MEM, adjust deaths of anything used in the address.
13029          For a REG (the only other possibility), the entire value is
13030          being replaced so the old value is not used in this insn.  */
13031
13032       if (MEM_P (dest))
13033         move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13034                      to_insn, pnotes);
13035       return;
13036     }
13037
13038   else if (GET_CODE (x) == CLOBBER)
13039     return;
13040
13041   len = GET_RTX_LENGTH (code);
13042   fmt = GET_RTX_FORMAT (code);
13043
13044   for (i = 0; i < len; i++)
13045     {
13046       if (fmt[i] == 'E')
13047         {
13048           int j;
13049           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13050             move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13051                          to_insn, pnotes);
13052         }
13053       else if (fmt[i] == 'e')
13054         move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13055     }
13056 }
13057 \f
13058 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13059    pattern of an insn.  X must be a REG.  */
13060
13061 static int
13062 reg_bitfield_target_p (rtx x, rtx body)
13063 {
13064   int i;
13065
13066   if (GET_CODE (body) == SET)
13067     {
13068       rtx dest = SET_DEST (body);
13069       rtx target;
13070       unsigned int regno, tregno, endregno, endtregno;
13071
13072       if (GET_CODE (dest) == ZERO_EXTRACT)
13073         target = XEXP (dest, 0);
13074       else if (GET_CODE (dest) == STRICT_LOW_PART)
13075         target = SUBREG_REG (XEXP (dest, 0));
13076       else
13077         return 0;
13078
13079       if (GET_CODE (target) == SUBREG)
13080         target = SUBREG_REG (target);
13081
13082       if (!REG_P (target))
13083         return 0;
13084
13085       tregno = REGNO (target), regno = REGNO (x);
13086       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13087         return target == x;
13088
13089       endtregno = end_hard_regno (GET_MODE (target), tregno);
13090       endregno = end_hard_regno (GET_MODE (x), regno);
13091
13092       return endregno > tregno && regno < endtregno;
13093     }
13094
13095   else if (GET_CODE (body) == PARALLEL)
13096     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13097       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13098         return 1;
13099
13100   return 0;
13101 }
13102 \f
13103 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13104    as appropriate.  I3 and I2 are the insns resulting from the combination
13105    insns including FROM (I2 may be zero).
13106
13107    ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13108    not need REG_DEAD notes because they are being substituted for.  This
13109    saves searching in the most common cases.
13110
13111    Each note in the list is either ignored or placed on some insns, depending
13112    on the type of note.  */
13113
13114 static void
13115 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
13116                   rtx elim_i1, rtx elim_i0)
13117 {
13118   rtx note, next_note;
13119   rtx tem;
13120
13121   for (note = notes; note; note = next_note)
13122     {
13123       rtx place = 0, place2 = 0;
13124
13125       next_note = XEXP (note, 1);
13126       switch (REG_NOTE_KIND (note))
13127         {
13128         case REG_BR_PROB:
13129         case REG_BR_PRED:
13130           /* Doesn't matter much where we put this, as long as it's somewhere.
13131              It is preferable to keep these notes on branches, which is most
13132              likely to be i3.  */
13133           place = i3;
13134           break;
13135
13136         case REG_VALUE_PROFILE:
13137           /* Just get rid of this note, as it is unused later anyway.  */
13138           break;
13139
13140         case REG_NON_LOCAL_GOTO:
13141           if (JUMP_P (i3))
13142             place = i3;
13143           else
13144             {
13145               gcc_assert (i2 && JUMP_P (i2));
13146               place = i2;
13147             }
13148           break;
13149
13150         case REG_EH_REGION:
13151           /* These notes must remain with the call or trapping instruction.  */
13152           if (CALL_P (i3))
13153             place = i3;
13154           else if (i2 && CALL_P (i2))
13155             place = i2;
13156           else
13157             {
13158               gcc_assert (cfun->can_throw_non_call_exceptions);
13159               if (may_trap_p (i3))
13160                 place = i3;
13161               else if (i2 && may_trap_p (i2))
13162                 place = i2;
13163               /* ??? Otherwise assume we've combined things such that we
13164                  can now prove that the instructions can't trap.  Drop the
13165                  note in this case.  */
13166             }
13167           break;
13168
13169         case REG_NORETURN:
13170         case REG_SETJMP:
13171           /* These notes must remain with the call.  It should not be
13172              possible for both I2 and I3 to be a call.  */
13173           if (CALL_P (i3))
13174             place = i3;
13175           else
13176             {
13177               gcc_assert (i2 && CALL_P (i2));
13178               place = i2;
13179             }
13180           break;
13181
13182         case REG_UNUSED:
13183           /* Any clobbers for i3 may still exist, and so we must process
13184              REG_UNUSED notes from that insn.
13185
13186              Any clobbers from i2 or i1 can only exist if they were added by
13187              recog_for_combine.  In that case, recog_for_combine created the
13188              necessary REG_UNUSED notes.  Trying to keep any original
13189              REG_UNUSED notes from these insns can cause incorrect output
13190              if it is for the same register as the original i3 dest.
13191              In that case, we will notice that the register is set in i3,
13192              and then add a REG_UNUSED note for the destination of i3, which
13193              is wrong.  However, it is possible to have REG_UNUSED notes from
13194              i2 or i1 for register which were both used and clobbered, so
13195              we keep notes from i2 or i1 if they will turn into REG_DEAD
13196              notes.  */
13197
13198           /* If this register is set or clobbered in I3, put the note there
13199              unless there is one already.  */
13200           if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13201             {
13202               if (from_insn != i3)
13203                 break;
13204
13205               if (! (REG_P (XEXP (note, 0))
13206                      ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13207                      : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13208                 place = i3;
13209             }
13210           /* Otherwise, if this register is used by I3, then this register
13211              now dies here, so we must put a REG_DEAD note here unless there
13212              is one already.  */
13213           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13214                    && ! (REG_P (XEXP (note, 0))
13215                          ? find_regno_note (i3, REG_DEAD,
13216                                             REGNO (XEXP (note, 0)))
13217                          : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13218             {
13219               PUT_REG_NOTE_KIND (note, REG_DEAD);
13220               place = i3;
13221             }
13222           break;
13223
13224         case REG_EQUAL:
13225         case REG_EQUIV:
13226         case REG_NOALIAS:
13227           /* These notes say something about results of an insn.  We can
13228              only support them if they used to be on I3 in which case they
13229              remain on I3.  Otherwise they are ignored.
13230
13231              If the note refers to an expression that is not a constant, we
13232              must also ignore the note since we cannot tell whether the
13233              equivalence is still true.  It might be possible to do
13234              slightly better than this (we only have a problem if I2DEST
13235              or I1DEST is present in the expression), but it doesn't
13236              seem worth the trouble.  */
13237
13238           if (from_insn == i3
13239               && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13240             place = i3;
13241           break;
13242
13243         case REG_INC:
13244           /* These notes say something about how a register is used.  They must
13245              be present on any use of the register in I2 or I3.  */
13246           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13247             place = i3;
13248
13249           if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13250             {
13251               if (place)
13252                 place2 = i2;
13253               else
13254                 place = i2;
13255             }
13256           break;
13257
13258         case REG_LABEL_TARGET:
13259         case REG_LABEL_OPERAND:
13260           /* This can show up in several ways -- either directly in the
13261              pattern, or hidden off in the constant pool with (or without?)
13262              a REG_EQUAL note.  */
13263           /* ??? Ignore the without-reg_equal-note problem for now.  */
13264           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13265               || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13266                   && GET_CODE (XEXP (tem, 0)) == LABEL_REF
13267                   && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
13268             place = i3;
13269
13270           if (i2
13271               && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13272                   || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13273                       && GET_CODE (XEXP (tem, 0)) == LABEL_REF
13274                       && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
13275             {
13276               if (place)
13277                 place2 = i2;
13278               else
13279                 place = i2;
13280             }
13281
13282           /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13283              as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13284              there.  */
13285           if (place && JUMP_P (place)
13286               && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13287               && (JUMP_LABEL (place) == NULL
13288                   || JUMP_LABEL (place) == XEXP (note, 0)))
13289             {
13290               rtx label = JUMP_LABEL (place);
13291
13292               if (!label)
13293                 JUMP_LABEL (place) = XEXP (note, 0);
13294               else if (LABEL_P (label))
13295                 LABEL_NUSES (label)--;
13296             }
13297
13298           if (place2 && JUMP_P (place2)
13299               && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13300               && (JUMP_LABEL (place2) == NULL
13301                   || JUMP_LABEL (place2) == XEXP (note, 0)))
13302             {
13303               rtx label = JUMP_LABEL (place2);
13304
13305               if (!label)
13306                 JUMP_LABEL (place2) = XEXP (note, 0);
13307               else if (LABEL_P (label))
13308                 LABEL_NUSES (label)--;
13309               place2 = 0;
13310             }
13311           break;
13312
13313         case REG_NONNEG:
13314           /* This note says something about the value of a register prior
13315              to the execution of an insn.  It is too much trouble to see
13316              if the note is still correct in all situations.  It is better
13317              to simply delete it.  */
13318           break;
13319
13320         case REG_DEAD:
13321           /* If we replaced the right hand side of FROM_INSN with a
13322              REG_EQUAL note, the original use of the dying register
13323              will not have been combined into I3 and I2.  In such cases,
13324              FROM_INSN is guaranteed to be the first of the combined
13325              instructions, so we simply need to search back before
13326              FROM_INSN for the previous use or set of this register,
13327              then alter the notes there appropriately.
13328
13329              If the register is used as an input in I3, it dies there.
13330              Similarly for I2, if it is nonzero and adjacent to I3.
13331
13332              If the register is not used as an input in either I3 or I2
13333              and it is not one of the registers we were supposed to eliminate,
13334              there are two possibilities.  We might have a non-adjacent I2
13335              or we might have somehow eliminated an additional register
13336              from a computation.  For example, we might have had A & B where
13337              we discover that B will always be zero.  In this case we will
13338              eliminate the reference to A.
13339
13340              In both cases, we must search to see if we can find a previous
13341              use of A and put the death note there.  */
13342
13343           if (from_insn
13344               && from_insn == i2mod
13345               && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13346             tem = from_insn;
13347           else
13348             {
13349               if (from_insn
13350                   && CALL_P (from_insn)
13351                   && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13352                 place = from_insn;
13353               else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13354                 place = i3;
13355               else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13356                        && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13357                 place = i2;
13358               else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13359                         && !(i2mod
13360                              && reg_overlap_mentioned_p (XEXP (note, 0),
13361                                                          i2mod_old_rhs)))
13362                        || rtx_equal_p (XEXP (note, 0), elim_i1)
13363                        || rtx_equal_p (XEXP (note, 0), elim_i0))
13364                 break;
13365               tem = i3;
13366             }
13367
13368           if (place == 0)
13369             {
13370               basic_block bb = this_basic_block;
13371
13372               for (tem = PREV_INSN (tem); place == 0; tem = PREV_INSN (tem))
13373                 {
13374                   if (!NONDEBUG_INSN_P (tem))
13375                     {
13376                       if (tem == BB_HEAD (bb))
13377                         break;
13378                       continue;
13379                     }
13380
13381                   /* If the register is being set at TEM, see if that is all
13382                      TEM is doing.  If so, delete TEM.  Otherwise, make this
13383                      into a REG_UNUSED note instead. Don't delete sets to
13384                      global register vars.  */
13385                   if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13386                        || !global_regs[REGNO (XEXP (note, 0))])
13387                       && reg_set_p (XEXP (note, 0), PATTERN (tem)))
13388                     {
13389                       rtx set = single_set (tem);
13390                       rtx inner_dest = 0;
13391 #ifdef HAVE_cc0
13392                       rtx cc0_setter = NULL_RTX;
13393 #endif
13394
13395                       if (set != 0)
13396                         for (inner_dest = SET_DEST (set);
13397                              (GET_CODE (inner_dest) == STRICT_LOW_PART
13398                               || GET_CODE (inner_dest) == SUBREG
13399                               || GET_CODE (inner_dest) == ZERO_EXTRACT);
13400                              inner_dest = XEXP (inner_dest, 0))
13401                           ;
13402
13403                       /* Verify that it was the set, and not a clobber that
13404                          modified the register.
13405
13406                          CC0 targets must be careful to maintain setter/user
13407                          pairs.  If we cannot delete the setter due to side
13408                          effects, mark the user with an UNUSED note instead
13409                          of deleting it.  */
13410
13411                       if (set != 0 && ! side_effects_p (SET_SRC (set))
13412                           && rtx_equal_p (XEXP (note, 0), inner_dest)
13413 #ifdef HAVE_cc0
13414                           && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13415                               || ((cc0_setter = prev_cc0_setter (tem)) != NULL
13416                                   && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13417 #endif
13418                           )
13419                         {
13420                           /* Move the notes and links of TEM elsewhere.
13421                              This might delete other dead insns recursively.
13422                              First set the pattern to something that won't use
13423                              any register.  */
13424                           rtx old_notes = REG_NOTES (tem);
13425
13426                           PATTERN (tem) = pc_rtx;
13427                           REG_NOTES (tem) = NULL;
13428
13429                           distribute_notes (old_notes, tem, tem, NULL_RTX,
13430                                             NULL_RTX, NULL_RTX, NULL_RTX);
13431                           distribute_links (LOG_LINKS (tem));
13432
13433                           SET_INSN_DELETED (tem);
13434                           if (tem == i2)
13435                             i2 = NULL_RTX;
13436
13437 #ifdef HAVE_cc0
13438                           /* Delete the setter too.  */
13439                           if (cc0_setter)
13440                             {
13441                               PATTERN (cc0_setter) = pc_rtx;
13442                               old_notes = REG_NOTES (cc0_setter);
13443                               REG_NOTES (cc0_setter) = NULL;
13444
13445                               distribute_notes (old_notes, cc0_setter,
13446                                                 cc0_setter, NULL_RTX,
13447                                                 NULL_RTX, NULL_RTX, NULL_RTX);
13448                               distribute_links (LOG_LINKS (cc0_setter));
13449
13450                               SET_INSN_DELETED (cc0_setter);
13451                               if (cc0_setter == i2)
13452                                 i2 = NULL_RTX;
13453                             }
13454 #endif
13455                         }
13456                       else
13457                         {
13458                           PUT_REG_NOTE_KIND (note, REG_UNUSED);
13459
13460                           /*  If there isn't already a REG_UNUSED note, put one
13461                               here.  Do not place a REG_DEAD note, even if
13462                               the register is also used here; that would not
13463                               match the algorithm used in lifetime analysis
13464                               and can cause the consistency check in the
13465                               scheduler to fail.  */
13466                           if (! find_regno_note (tem, REG_UNUSED,
13467                                                  REGNO (XEXP (note, 0))))
13468                             place = tem;
13469                           break;
13470                         }
13471                     }
13472                   else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
13473                            || (CALL_P (tem)
13474                                && find_reg_fusage (tem, USE, XEXP (note, 0))))
13475                     {
13476                       place = tem;
13477
13478                       /* If we are doing a 3->2 combination, and we have a
13479                          register which formerly died in i3 and was not used
13480                          by i2, which now no longer dies in i3 and is used in
13481                          i2 but does not die in i2, and place is between i2
13482                          and i3, then we may need to move a link from place to
13483                          i2.  */
13484                       if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13485                           && from_insn
13486                           && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13487                           && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13488                         {
13489                           rtx links = LOG_LINKS (place);
13490                           LOG_LINKS (place) = 0;
13491                           distribute_links (links);
13492                         }
13493                       break;
13494                     }
13495
13496                   if (tem == BB_HEAD (bb))
13497                     break;
13498                 }
13499
13500             }
13501
13502           /* If the register is set or already dead at PLACE, we needn't do
13503              anything with this note if it is still a REG_DEAD note.
13504              We check here if it is set at all, not if is it totally replaced,
13505              which is what `dead_or_set_p' checks, so also check for it being
13506              set partially.  */
13507
13508           if (place && REG_NOTE_KIND (note) == REG_DEAD)
13509             {
13510               unsigned int regno = REGNO (XEXP (note, 0));
13511               reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
13512
13513               if (dead_or_set_p (place, XEXP (note, 0))
13514                   || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13515                 {
13516                   /* Unless the register previously died in PLACE, clear
13517                      last_death.  [I no longer understand why this is
13518                      being done.] */
13519                   if (rsp->last_death != place)
13520                     rsp->last_death = 0;
13521                   place = 0;
13522                 }
13523               else
13524                 rsp->last_death = place;
13525
13526               /* If this is a death note for a hard reg that is occupying
13527                  multiple registers, ensure that we are still using all
13528                  parts of the object.  If we find a piece of the object
13529                  that is unused, we must arrange for an appropriate REG_DEAD
13530                  note to be added for it.  However, we can't just emit a USE
13531                  and tag the note to it, since the register might actually
13532                  be dead; so we recourse, and the recursive call then finds
13533                  the previous insn that used this register.  */
13534
13535               if (place && regno < FIRST_PSEUDO_REGISTER
13536                   && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13537                 {
13538                   unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13539                   int all_used = 1;
13540                   unsigned int i;
13541
13542                   for (i = regno; i < endregno; i++)
13543                     if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13544                          && ! find_regno_fusage (place, USE, i))
13545                         || dead_or_set_regno_p (place, i))
13546                       all_used = 0;
13547
13548                   if (! all_used)
13549                     {
13550                       /* Put only REG_DEAD notes for pieces that are
13551                          not already dead or set.  */
13552
13553                       for (i = regno; i < endregno;
13554                            i += hard_regno_nregs[i][reg_raw_mode[i]])
13555                         {
13556                           rtx piece = regno_reg_rtx[i];
13557                           basic_block bb = this_basic_block;
13558
13559                           if (! dead_or_set_p (place, piece)
13560                               && ! reg_bitfield_target_p (piece,
13561                                                           PATTERN (place)))
13562                             {
13563                               rtx new_note = alloc_reg_note (REG_DEAD, piece,
13564                                                              NULL_RTX);
13565
13566                               distribute_notes (new_note, place, place,
13567                                                 NULL_RTX, NULL_RTX, NULL_RTX,
13568                                                 NULL_RTX);
13569                             }
13570                           else if (! refers_to_regno_p (i, i + 1,
13571                                                         PATTERN (place), 0)
13572                                    && ! find_regno_fusage (place, USE, i))
13573                             for (tem = PREV_INSN (place); ;
13574                                  tem = PREV_INSN (tem))
13575                               {
13576                                 if (!NONDEBUG_INSN_P (tem))
13577                                   {
13578                                     if (tem == BB_HEAD (bb))
13579                                       break;
13580                                     continue;
13581                                   }
13582                                 if (dead_or_set_p (tem, piece)
13583                                     || reg_bitfield_target_p (piece,
13584                                                               PATTERN (tem)))
13585                                   {
13586                                     add_reg_note (tem, REG_UNUSED, piece);
13587                                     break;
13588                                   }
13589                               }
13590
13591                         }
13592
13593                       place = 0;
13594                     }
13595                 }
13596             }
13597           break;
13598
13599         default:
13600           /* Any other notes should not be present at this point in the
13601              compilation.  */
13602           gcc_unreachable ();
13603         }
13604
13605       if (place)
13606         {
13607           XEXP (note, 1) = REG_NOTES (place);
13608           REG_NOTES (place) = note;
13609         }
13610
13611       if (place2)
13612         add_reg_note (place2, REG_NOTE_KIND (note), XEXP (note, 0));
13613     }
13614 }
13615 \f
13616 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13617    I3, I2, and I1 to new locations.  This is also called to add a link
13618    pointing at I3 when I3's destination is changed.  */
13619
13620 static void
13621 distribute_links (rtx links)
13622 {
13623   rtx link, next_link;
13624
13625   for (link = links; link; link = next_link)
13626     {
13627       rtx place = 0;
13628       rtx insn;
13629       rtx set, reg;
13630
13631       next_link = XEXP (link, 1);
13632
13633       /* If the insn that this link points to is a NOTE or isn't a single
13634          set, ignore it.  In the latter case, it isn't clear what we
13635          can do other than ignore the link, since we can't tell which
13636          register it was for.  Such links wouldn't be used by combine
13637          anyway.
13638
13639          It is not possible for the destination of the target of the link to
13640          have been changed by combine.  The only potential of this is if we
13641          replace I3, I2, and I1 by I3 and I2.  But in that case the
13642          destination of I2 also remains unchanged.  */
13643
13644       if (NOTE_P (XEXP (link, 0))
13645           || (set = single_set (XEXP (link, 0))) == 0)
13646         continue;
13647
13648       reg = SET_DEST (set);
13649       while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
13650              || GET_CODE (reg) == STRICT_LOW_PART)
13651         reg = XEXP (reg, 0);
13652
13653       /* A LOG_LINK is defined as being placed on the first insn that uses
13654          a register and points to the insn that sets the register.  Start
13655          searching at the next insn after the target of the link and stop
13656          when we reach a set of the register or the end of the basic block.
13657
13658          Note that this correctly handles the link that used to point from
13659          I3 to I2.  Also note that not much searching is typically done here
13660          since most links don't point very far away.  */
13661
13662       for (insn = NEXT_INSN (XEXP (link, 0));
13663            (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
13664                      || BB_HEAD (this_basic_block->next_bb) != insn));
13665            insn = NEXT_INSN (insn))
13666         if (DEBUG_INSN_P (insn))
13667           continue;
13668         else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13669           {
13670             if (reg_referenced_p (reg, PATTERN (insn)))
13671               place = insn;
13672             break;
13673           }
13674         else if (CALL_P (insn)
13675                  && find_reg_fusage (insn, USE, reg))
13676           {
13677             place = insn;
13678             break;
13679           }
13680         else if (INSN_P (insn) && reg_set_p (reg, insn))
13681           break;
13682
13683       /* If we found a place to put the link, place it there unless there
13684          is already a link to the same insn as LINK at that point.  */
13685
13686       if (place)
13687         {
13688           rtx link2;
13689
13690           for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
13691             if (XEXP (link2, 0) == XEXP (link, 0))
13692               break;
13693
13694           if (link2 == 0)
13695             {
13696               XEXP (link, 1) = LOG_LINKS (place);
13697               LOG_LINKS (place) = link;
13698
13699               /* Set added_links_insn to the earliest insn we added a
13700                  link to.  */
13701               if (added_links_insn == 0
13702                   || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
13703                 added_links_insn = place;
13704             }
13705         }
13706     }
13707 }
13708 \f
13709 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
13710    Check whether the expression pointer to by LOC is a register or
13711    memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
13712    Otherwise return zero.  */
13713
13714 static int
13715 unmentioned_reg_p_1 (rtx *loc, void *expr)
13716 {
13717   rtx x = *loc;
13718
13719   if (x != NULL_RTX
13720       && (REG_P (x) || MEM_P (x))
13721       && ! reg_mentioned_p (x, (rtx) expr))
13722     return 1;
13723   return 0;
13724 }
13725
13726 /* Check for any register or memory mentioned in EQUIV that is not
13727    mentioned in EXPR.  This is used to restrict EQUIV to "specializations"
13728    of EXPR where some registers may have been replaced by constants.  */
13729
13730 static bool
13731 unmentioned_reg_p (rtx equiv, rtx expr)
13732 {
13733   return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
13734 }
13735 \f
13736 void
13737 dump_combine_stats (FILE *file)
13738 {
13739   fprintf
13740     (file,
13741      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13742      combine_attempts, combine_merges, combine_extras, combine_successes);
13743 }
13744
13745 void
13746 dump_combine_total_stats (FILE *file)
13747 {
13748   fprintf
13749     (file,
13750      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13751      total_attempts, total_merges, total_extras, total_successes);
13752 }
13753 \f
13754 static bool
13755 gate_handle_combine (void)
13756 {
13757   return (optimize > 0);
13758 }
13759
13760 /* Try combining insns through substitution.  */
13761 static unsigned int
13762 rest_of_handle_combine (void)
13763 {
13764   int rebuild_jump_labels_after_combine;
13765
13766   df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
13767   df_note_add_problem ();
13768   df_analyze ();
13769
13770   regstat_init_n_sets_and_refs ();
13771
13772   rebuild_jump_labels_after_combine
13773     = combine_instructions (get_insns (), max_reg_num ());
13774
13775   /* Combining insns may have turned an indirect jump into a
13776      direct jump.  Rebuild the JUMP_LABEL fields of jumping
13777      instructions.  */
13778   if (rebuild_jump_labels_after_combine)
13779     {
13780       timevar_push (TV_JUMP);
13781       rebuild_jump_labels (get_insns ());
13782       cleanup_cfg (0);
13783       timevar_pop (TV_JUMP);
13784     }
13785
13786   regstat_free_n_sets_and_refs ();
13787   return 0;
13788 }
13789
13790 struct rtl_opt_pass pass_combine =
13791 {
13792  {
13793   RTL_PASS,
13794   "combine",                            /* name */
13795   gate_handle_combine,                  /* gate */
13796   rest_of_handle_combine,               /* execute */
13797   NULL,                                 /* sub */
13798   NULL,                                 /* next */
13799   0,                                    /* static_pass_number */
13800   TV_COMBINE,                           /* tv_id */
13801   PROP_cfglayout,                       /* properties_required */
13802   0,                                    /* properties_provided */
13803   0,                                    /* properties_destroyed */
13804   0,                                    /* todo_flags_start */
13805   TODO_dump_func |
13806   TODO_df_finish | TODO_verify_rtl_sharing |
13807   TODO_ggc_collect,                     /* todo_flags_finish */
13808  }
13809 };