OSDN Git Service

2008-04-03 Jan Hubicka <jh@suse.cz>
[pf3gnuchains/gcc-fork.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 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 "real.h"
96 #include "toplev.h"
97 #include "target.h"
98 #include "optabs.h"
99 #include "insn-codes.h"
100 #include "rtlhooks-def.h"
101 /* Include output.h for dump_file.  */
102 #include "output.h"
103 #include "params.h"
104 #include "timevar.h"
105 #include "tree-pass.h"
106 #include "df.h"
107 #include "cgraph.h"
108
109 /* Number of attempts to combine instructions in this function.  */
110
111 static int combine_attempts;
112
113 /* Number of attempts that got as far as substitution in this function.  */
114
115 static int combine_merges;
116
117 /* Number of instructions combined with added SETs in this function.  */
118
119 static int combine_extras;
120
121 /* Number of instructions combined in this function.  */
122
123 static int combine_successes;
124
125 /* Totals over entire compilation.  */
126
127 static int total_attempts, total_merges, total_extras, total_successes;
128
129 /* combine_instructions may try to replace the right hand side of the
130    second instruction with the value of an associated REG_EQUAL note
131    before throwing it at try_combine.  That is problematic when there
132    is a REG_DEAD note for a register used in the old right hand side
133    and can cause distribute_notes to do wrong things.  This is the
134    second instruction if it has been so modified, null otherwise.  */
135
136 static rtx i2mod;
137
138 /* When I2MOD is nonnull, this is a copy of the old right hand side.  */
139
140 static rtx i2mod_old_rhs;
141
142 /* When I2MOD is nonnull, this is a copy of the new right hand side.  */
143
144 static rtx i2mod_new_rhs;
145 \f
146 typedef struct reg_stat_struct {
147   /* Record last point of death of (hard or pseudo) register n.  */
148   rtx                           last_death;
149
150   /* Record last point of modification of (hard or pseudo) register n.  */
151   rtx                           last_set;
152
153   /* The next group of fields allows the recording of the last value assigned
154      to (hard or pseudo) register n.  We use this information to see if an
155      operation being processed is redundant given a prior operation performed
156      on the register.  For example, an `and' with a constant is redundant if
157      all the zero bits are already known to be turned off.
158
159      We use an approach similar to that used by cse, but change it in the
160      following ways:
161
162      (1) We do not want to reinitialize at each label.
163      (2) It is useful, but not critical, to know the actual value assigned
164          to a register.  Often just its form is helpful.
165
166      Therefore, we maintain the following fields:
167
168      last_set_value             the last value assigned
169      last_set_label             records the value of label_tick when the
170                                 register was assigned
171      last_set_table_tick        records the value of label_tick when a
172                                 value using the register is assigned
173      last_set_invalid           set to nonzero when it is not valid
174                                 to use the value of this register in some
175                                 register's value
176
177      To understand the usage of these tables, it is important to understand
178      the distinction between the value in last_set_value being valid and
179      the register being validly contained in some other expression in the
180      table.
181
182      (The next two parameters are out of date).
183
184      reg_stat[i].last_set_value is valid if it is nonzero, and either
185      reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
186
187      Register I may validly appear in any expression returned for the value
188      of another register if reg_n_sets[i] is 1.  It may also appear in the
189      value for register J if reg_stat[j].last_set_invalid is zero, or
190      reg_stat[i].last_set_label < reg_stat[j].last_set_label.
191
192      If an expression is found in the table containing a register which may
193      not validly appear in an expression, the register is replaced by
194      something that won't match, (clobber (const_int 0)).  */
195
196   /* Record last value assigned to (hard or pseudo) register n.  */
197
198   rtx                           last_set_value;
199
200   /* Record the value of label_tick when an expression involving register n
201      is placed in last_set_value.  */
202
203   int                           last_set_table_tick;
204
205   /* Record the value of label_tick when the value for register n is placed in
206      last_set_value.  */
207
208   int                           last_set_label;
209
210   /* These fields are maintained in parallel with last_set_value and are
211      used to store the mode in which the register was last set, the bits
212      that were known to be zero when it was last set, and the number of
213      sign bits copies it was known to have when it was last set.  */
214
215   unsigned HOST_WIDE_INT        last_set_nonzero_bits;
216   char                          last_set_sign_bit_copies;
217   ENUM_BITFIELD(machine_mode)   last_set_mode : 8;
218
219   /* Set nonzero if references to register n in expressions should not be
220      used.  last_set_invalid is set nonzero when this register is being
221      assigned to and last_set_table_tick == label_tick.  */
222
223   char                          last_set_invalid;
224
225   /* Some registers that are set more than once and used in more than one
226      basic block are nevertheless always set in similar ways.  For example,
227      a QImode register may be loaded from memory in two places on a machine
228      where byte loads zero extend.
229
230      We record in the following fields if a register has some leading bits
231      that are always equal to the sign bit, and what we know about the
232      nonzero bits of a register, specifically which bits are known to be
233      zero.
234
235      If an entry is zero, it means that we don't know anything special.  */
236
237   unsigned char                 sign_bit_copies;
238
239   unsigned HOST_WIDE_INT        nonzero_bits;
240
241   /* Record the value of the label_tick when the last truncation
242      happened.  The field truncated_to_mode is only valid if
243      truncation_label == label_tick.  */
244
245   int                           truncation_label;
246
247   /* Record the last truncation seen for this register.  If truncation
248      is not a nop to this mode we might be able to save an explicit
249      truncation if we know that value already contains a truncated
250      value.  */
251
252   ENUM_BITFIELD(machine_mode)   truncated_to_mode : 8;
253 } reg_stat_type;
254
255 DEF_VEC_O(reg_stat_type);
256 DEF_VEC_ALLOC_O(reg_stat_type,heap);
257
258 static VEC(reg_stat_type,heap) *reg_stat;
259
260 /* Record the luid of the last insn that invalidated memory
261    (anything that writes memory, and subroutine calls, but not pushes).  */
262
263 static int mem_last_set;
264
265 /* Record the luid of the last CALL_INSN
266    so we can tell whether a potential combination crosses any calls.  */
267
268 static int last_call_luid;
269
270 /* When `subst' is called, this is the insn that is being modified
271    (by combining in a previous insn).  The PATTERN of this insn
272    is still the old pattern partially modified and it should not be
273    looked at, but this may be used to examine the successors of the insn
274    to judge whether a simplification is valid.  */
275
276 static rtx subst_insn;
277
278 /* This is the lowest LUID that `subst' is currently dealing with.
279    get_last_value will not return a value if the register was set at or
280    after this LUID.  If not for this mechanism, we could get confused if
281    I2 or I1 in try_combine were an insn that used the old value of a register
282    to obtain a new value.  In that case, we might erroneously get the
283    new value of the register when we wanted the old one.  */
284
285 static int subst_low_luid;
286
287 /* This contains any hard registers that are used in newpat; reg_dead_at_p
288    must consider all these registers to be always live.  */
289
290 static HARD_REG_SET newpat_used_regs;
291
292 /* This is an insn to which a LOG_LINKS entry has been added.  If this
293    insn is the earlier than I2 or I3, combine should rescan starting at
294    that location.  */
295
296 static rtx added_links_insn;
297
298 /* Basic block in which we are performing combines.  */
299 static basic_block this_basic_block;
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 label.  */
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 struct undo
345 {
346   struct undo *next;
347   enum { UNDO_RTX, UNDO_INT, UNDO_MODE } kind;
348   union { rtx r; int i; enum machine_mode m; } old_contents;
349   union { rtx *r; int *i; } where;
350 };
351
352 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
353    num_undo says how many are currently recorded.
354
355    other_insn is nonzero if we have modified some other insn in the process
356    of working on subst_insn.  It must be verified too.  */
357
358 struct undobuf
359 {
360   struct undo *undos;
361   struct undo *frees;
362   rtx other_insn;
363 };
364
365 static struct undobuf undobuf;
366
367 /* Number of times the pseudo being substituted for
368    was found and replaced.  */
369
370 static int n_occurrences;
371
372 static rtx reg_nonzero_bits_for_combine (const_rtx, enum machine_mode, const_rtx,
373                                          enum machine_mode,
374                                          unsigned HOST_WIDE_INT,
375                                          unsigned HOST_WIDE_INT *);
376 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, enum machine_mode, const_rtx,
377                                                 enum machine_mode,
378                                                 unsigned int, unsigned int *);
379 static void do_SUBST (rtx *, rtx);
380 static void do_SUBST_INT (int *, int);
381 static void init_reg_last (void);
382 static void setup_incoming_promotions (rtx);
383 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
384 static int cant_combine_insn_p (rtx);
385 static int can_combine_p (rtx, rtx, rtx, rtx, rtx *, rtx *);
386 static int combinable_i3pat (rtx, rtx *, rtx, rtx, int, rtx *);
387 static int contains_muldiv (rtx);
388 static rtx try_combine (rtx, rtx, rtx, int *);
389 static void undo_all (void);
390 static void undo_commit (void);
391 static rtx *find_split_point (rtx *, rtx);
392 static rtx subst (rtx, rtx, rtx, int, int);
393 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
394 static rtx simplify_if_then_else (rtx);
395 static rtx simplify_set (rtx);
396 static rtx simplify_logical (rtx);
397 static rtx expand_compound_operation (rtx);
398 static const_rtx expand_field_assignment (const_rtx);
399 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
400                             rtx, unsigned HOST_WIDE_INT, int, int, int);
401 static rtx extract_left_shift (rtx, int);
402 static rtx make_compound_operation (rtx, enum rtx_code);
403 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
404                               unsigned HOST_WIDE_INT *);
405 static rtx canon_reg_for_combine (rtx, rtx);
406 static rtx force_to_mode (rtx, enum machine_mode,
407                           unsigned HOST_WIDE_INT, int);
408 static rtx if_then_else_cond (rtx, rtx *, rtx *);
409 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
410 static int rtx_equal_for_field_assignment_p (rtx, rtx);
411 static rtx make_field_assignment (rtx);
412 static rtx apply_distributive_law (rtx);
413 static rtx distribute_and_simplify_rtx (rtx, int);
414 static rtx simplify_and_const_int_1 (enum machine_mode, rtx,
415                                      unsigned HOST_WIDE_INT);
416 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
417                                    unsigned HOST_WIDE_INT);
418 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
419                             HOST_WIDE_INT, enum machine_mode, int *);
420 static rtx simplify_shift_const_1 (enum rtx_code, enum machine_mode, rtx, int);
421 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
422                                  int);
423 static int recog_for_combine (rtx *, rtx, rtx *);
424 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
425 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
426 static void update_table_tick (rtx);
427 static void record_value_for_reg (rtx, rtx, rtx);
428 static void check_conversions (rtx, rtx);
429 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
430 static void record_dead_and_set_regs (rtx);
431 static int get_last_value_validate (rtx *, rtx, int, int);
432 static rtx get_last_value (const_rtx);
433 static int use_crosses_set_p (const_rtx, int);
434 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
435 static int reg_dead_at_p (rtx, rtx);
436 static void move_deaths (rtx, rtx, int, rtx, rtx *);
437 static int reg_bitfield_target_p (rtx, rtx);
438 static void distribute_notes (rtx, rtx, rtx, rtx, rtx, rtx);
439 static void distribute_links (rtx);
440 static void mark_used_regs_combine (rtx);
441 static void record_promoted_value (rtx, rtx);
442 static int unmentioned_reg_p_1 (rtx *, void *);
443 static bool unmentioned_reg_p (rtx, rtx);
444 static void record_truncated_value (rtx);
445 static bool reg_truncated_to_mode (enum machine_mode, const_rtx);
446 static rtx gen_lowpart_or_truncate (enum machine_mode, rtx);
447 \f
448
449 /* It is not safe to use ordinary gen_lowpart in combine.
450    See comments in gen_lowpart_for_combine.  */
451 #undef RTL_HOOKS_GEN_LOWPART
452 #define RTL_HOOKS_GEN_LOWPART              gen_lowpart_for_combine
453
454 /* Our implementation of gen_lowpart never emits a new pseudo.  */
455 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
456 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT      gen_lowpart_for_combine
457
458 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
459 #define RTL_HOOKS_REG_NONZERO_REG_BITS     reg_nonzero_bits_for_combine
460
461 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
462 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES  reg_num_sign_bit_copies_for_combine
463
464 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
465 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE    reg_truncated_to_mode
466
467 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
468
469 \f
470 /* Try to split PATTERN found in INSN.  This returns NULL_RTX if
471    PATTERN can not be split.  Otherwise, it returns an insn sequence.
472    This is a wrapper around split_insns which ensures that the
473    reg_stat vector is made larger if the splitter creates a new
474    register.  */
475
476 static rtx
477 combine_split_insns (rtx pattern, rtx insn)
478 {
479   rtx ret;
480   unsigned int nregs;
481
482   ret = split_insns (pattern, insn);
483   nregs = max_reg_num ();
484   if (nregs > VEC_length (reg_stat_type, reg_stat))
485     VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
486   return ret;
487 }
488
489 /* This is used by find_single_use to locate an rtx in LOC that
490    contains exactly one use of DEST, which is typically either a REG
491    or CC0.  It returns a pointer to the innermost rtx expression
492    containing DEST.  Appearances of DEST that are being used to
493    totally replace it are not counted.  */
494
495 static rtx *
496 find_single_use_1 (rtx dest, rtx *loc)
497 {
498   rtx x = *loc;
499   enum rtx_code code = GET_CODE (x);
500   rtx *result = NULL;
501   rtx *this_result;
502   int i;
503   const char *fmt;
504
505   switch (code)
506     {
507     case CONST_INT:
508     case CONST:
509     case LABEL_REF:
510     case SYMBOL_REF:
511     case CONST_DOUBLE:
512     case CONST_VECTOR:
513     case CLOBBER:
514       return 0;
515
516     case SET:
517       /* If the destination is anything other than CC0, PC, a REG or a SUBREG
518          of a REG that occupies all of the REG, the insn uses DEST if
519          it is mentioned in the destination or the source.  Otherwise, we
520          need just check the source.  */
521       if (GET_CODE (SET_DEST (x)) != CC0
522           && GET_CODE (SET_DEST (x)) != PC
523           && !REG_P (SET_DEST (x))
524           && ! (GET_CODE (SET_DEST (x)) == SUBREG
525                 && REG_P (SUBREG_REG (SET_DEST (x)))
526                 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
527                       + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
528                     == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
529                          + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
530         break;
531
532       return find_single_use_1 (dest, &SET_SRC (x));
533
534     case MEM:
535     case SUBREG:
536       return find_single_use_1 (dest, &XEXP (x, 0));
537
538     default:
539       break;
540     }
541
542   /* If it wasn't one of the common cases above, check each expression and
543      vector of this code.  Look for a unique usage of DEST.  */
544
545   fmt = GET_RTX_FORMAT (code);
546   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
547     {
548       if (fmt[i] == 'e')
549         {
550           if (dest == XEXP (x, i)
551               || (REG_P (dest) && REG_P (XEXP (x, i))
552                   && REGNO (dest) == REGNO (XEXP (x, i))))
553             this_result = loc;
554           else
555             this_result = find_single_use_1 (dest, &XEXP (x, i));
556
557           if (result == NULL)
558             result = this_result;
559           else if (this_result)
560             /* Duplicate usage.  */
561             return NULL;
562         }
563       else if (fmt[i] == 'E')
564         {
565           int j;
566
567           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
568             {
569               if (XVECEXP (x, i, j) == dest
570                   || (REG_P (dest)
571                       && REG_P (XVECEXP (x, i, j))
572                       && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
573                 this_result = loc;
574               else
575                 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
576
577               if (result == NULL)
578                 result = this_result;
579               else if (this_result)
580                 return NULL;
581             }
582         }
583     }
584
585   return result;
586 }
587
588
589 /* See if DEST, produced in INSN, is used only a single time in the
590    sequel.  If so, return a pointer to the innermost rtx expression in which
591    it is used.
592
593    If PLOC is nonzero, *PLOC is set to the insn containing the single use.
594
595    If DEST is cc0_rtx, we look only at the next insn.  In that case, we don't
596    care about REG_DEAD notes or LOG_LINKS.
597
598    Otherwise, we find the single use by finding an insn that has a
599    LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST.  If DEST is
600    only referenced once in that insn, we know that it must be the first
601    and last insn referencing DEST.  */
602
603 static rtx *
604 find_single_use (rtx dest, rtx insn, rtx *ploc)
605 {
606   rtx next;
607   rtx *result;
608   rtx link;
609
610 #ifdef HAVE_cc0
611   if (dest == cc0_rtx)
612     {
613       next = NEXT_INSN (insn);
614       if (next == 0
615           || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
616         return 0;
617
618       result = find_single_use_1 (dest, &PATTERN (next));
619       if (result && ploc)
620         *ploc = next;
621       return result;
622     }
623 #endif
624
625   if (!REG_P (dest))
626     return 0;
627
628   for (next = next_nonnote_insn (insn);
629        next != 0 && !LABEL_P (next);
630        next = next_nonnote_insn (next))
631     if (INSN_P (next) && dead_or_set_p (next, dest))
632       {
633         for (link = LOG_LINKS (next); link; link = XEXP (link, 1))
634           if (XEXP (link, 0) == insn)
635             break;
636
637         if (link)
638           {
639             result = find_single_use_1 (dest, &PATTERN (next));
640             if (ploc)
641               *ploc = next;
642             return result;
643           }
644       }
645
646   return 0;
647 }
648 \f
649 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
650    insn.  The substitution can be undone by undo_all.  If INTO is already
651    set to NEWVAL, do not record this change.  Because computing NEWVAL might
652    also call SUBST, we have to compute it before we put anything into
653    the undo table.  */
654
655 static void
656 do_SUBST (rtx *into, rtx newval)
657 {
658   struct undo *buf;
659   rtx oldval = *into;
660
661   if (oldval == newval)
662     return;
663
664   /* We'd like to catch as many invalid transformations here as
665      possible.  Unfortunately, there are way too many mode changes
666      that are perfectly valid, so we'd waste too much effort for
667      little gain doing the checks here.  Focus on catching invalid
668      transformations involving integer constants.  */
669   if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
670       && GET_CODE (newval) == CONST_INT)
671     {
672       /* Sanity check that we're replacing oldval with a CONST_INT
673          that is a valid sign-extension for the original mode.  */
674       gcc_assert (INTVAL (newval)
675                   == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
676
677       /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
678          CONST_INT is not valid, because after the replacement, the
679          original mode would be gone.  Unfortunately, we can't tell
680          when do_SUBST is called to replace the operand thereof, so we
681          perform this test on oldval instead, checking whether an
682          invalid replacement took place before we got here.  */
683       gcc_assert (!(GET_CODE (oldval) == SUBREG
684                     && GET_CODE (SUBREG_REG (oldval)) == CONST_INT));
685       gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
686                     && GET_CODE (XEXP (oldval, 0)) == CONST_INT));
687     }
688
689   if (undobuf.frees)
690     buf = undobuf.frees, undobuf.frees = buf->next;
691   else
692     buf = XNEW (struct undo);
693
694   buf->kind = UNDO_RTX;
695   buf->where.r = into;
696   buf->old_contents.r = oldval;
697   *into = newval;
698
699   buf->next = undobuf.undos, undobuf.undos = buf;
700 }
701
702 #define SUBST(INTO, NEWVAL)     do_SUBST(&(INTO), (NEWVAL))
703
704 /* Similar to SUBST, but NEWVAL is an int expression.  Note that substitution
705    for the value of a HOST_WIDE_INT value (including CONST_INT) is
706    not safe.  */
707
708 static void
709 do_SUBST_INT (int *into, int newval)
710 {
711   struct undo *buf;
712   int oldval = *into;
713
714   if (oldval == newval)
715     return;
716
717   if (undobuf.frees)
718     buf = undobuf.frees, undobuf.frees = buf->next;
719   else
720     buf = XNEW (struct undo);
721
722   buf->kind = UNDO_INT;
723   buf->where.i = into;
724   buf->old_contents.i = oldval;
725   *into = newval;
726
727   buf->next = undobuf.undos, undobuf.undos = buf;
728 }
729
730 #define SUBST_INT(INTO, NEWVAL)  do_SUBST_INT(&(INTO), (NEWVAL))
731
732 /* Similar to SUBST, but just substitute the mode.  This is used when
733    changing the mode of a pseudo-register, so that any other
734    references to the entry in the regno_reg_rtx array will change as
735    well.  */
736
737 static void
738 do_SUBST_MODE (rtx *into, enum machine_mode newval)
739 {
740   struct undo *buf;
741   enum machine_mode oldval = GET_MODE (*into);
742
743   if (oldval == newval)
744     return;
745
746   if (undobuf.frees)
747     buf = undobuf.frees, undobuf.frees = buf->next;
748   else
749     buf = XNEW (struct undo);
750
751   buf->kind = UNDO_MODE;
752   buf->where.r = into;
753   buf->old_contents.m = oldval;
754   adjust_reg_mode (*into, newval);
755
756   buf->next = undobuf.undos, undobuf.undos = buf;
757 }
758
759 #define SUBST_MODE(INTO, NEWVAL)  do_SUBST_MODE(&(INTO), (NEWVAL))
760 \f
761 /* Subroutine of try_combine.  Determine whether the combine replacement
762    patterns NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to
763    insn_rtx_cost that the original instruction sequence I1, I2, I3 and
764    undobuf.other_insn.  Note that I1 and/or NEWI2PAT may be NULL_RTX. 
765    NEWOTHERPAT and undobuf.other_insn may also both be NULL_RTX.  This
766    function returns false, if the costs of all instructions can be
767    estimated, and the replacements are more expensive than the original
768    sequence.  */
769
770 static bool
771 combine_validate_cost (rtx i1, rtx i2, rtx i3, rtx newpat, rtx newi2pat,
772                        rtx newotherpat)
773 {
774   int i1_cost, i2_cost, i3_cost;
775   int new_i2_cost, new_i3_cost;
776   int old_cost, new_cost;
777
778   /* Lookup the original insn_rtx_costs.  */
779   i2_cost = INSN_COST (i2);
780   i3_cost = INSN_COST (i3);
781
782   if (i1)
783     {
784       i1_cost = INSN_COST (i1);
785       old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0)
786                  ? i1_cost + i2_cost + i3_cost : 0;
787     }
788   else
789     {
790       old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
791       i1_cost = 0;
792     }
793
794   /* Calculate the replacement insn_rtx_costs.  */
795   new_i3_cost = insn_rtx_cost (newpat);
796   if (newi2pat)
797     {
798       new_i2_cost = insn_rtx_cost (newi2pat);
799       new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
800                  ? new_i2_cost + new_i3_cost : 0;
801     }
802   else
803     {
804       new_cost = new_i3_cost;
805       new_i2_cost = 0;
806     }
807
808   if (undobuf.other_insn)
809     {
810       int old_other_cost, new_other_cost;
811
812       old_other_cost = INSN_COST (undobuf.other_insn);
813       new_other_cost = insn_rtx_cost (newotherpat);
814       if (old_other_cost > 0 && new_other_cost > 0)
815         {
816           old_cost += old_other_cost;
817           new_cost += new_other_cost;
818         }
819       else
820         old_cost = 0;
821     }
822
823   /* Disallow this recombination if both new_cost and old_cost are
824      greater than zero, and new_cost is greater than old cost.  */
825   if (old_cost > 0
826       && new_cost > old_cost)
827     {
828       if (dump_file)
829         {
830           if (i1)
831             {
832               fprintf (dump_file,
833                        "rejecting combination of insns %d, %d and %d\n",
834                        INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
835               fprintf (dump_file, "original costs %d + %d + %d = %d\n",
836                        i1_cost, i2_cost, i3_cost, old_cost);
837             }
838           else
839             {
840               fprintf (dump_file,
841                        "rejecting combination of insns %d and %d\n",
842                        INSN_UID (i2), INSN_UID (i3));
843               fprintf (dump_file, "original costs %d + %d = %d\n",
844                        i2_cost, i3_cost, old_cost);
845             }
846
847           if (newi2pat)
848             {
849               fprintf (dump_file, "replacement costs %d + %d = %d\n",
850                        new_i2_cost, new_i3_cost, new_cost);
851             }
852           else
853             fprintf (dump_file, "replacement cost %d\n", new_cost);
854         }
855
856       return false;
857     }
858
859   /* Update the uid_insn_cost array with the replacement costs.  */
860   INSN_COST (i2) = new_i2_cost;
861   INSN_COST (i3) = new_i3_cost;
862   if (i1)
863     INSN_COST (i1) = 0;
864
865   return true;
866 }
867
868
869 /* Delete any insns that copy a register to itself.  */
870
871 static void
872 delete_noop_moves (void)
873 {
874   rtx insn, next;
875   basic_block bb;
876
877   FOR_EACH_BB (bb)
878     {
879       for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
880         {
881           next = NEXT_INSN (insn);
882           if (INSN_P (insn) && noop_move_p (insn))
883             {
884               rtx note;
885
886               /* If we're about to remove the first insn of a libcall
887                  then move the libcall note to the next real insn and
888                  update the retval note.  */
889               if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX))
890                        && XEXP (note, 0) != insn)
891                 {
892                   rtx new_libcall_insn = next_real_insn (insn);
893                   rtx retval_note = find_reg_note (XEXP (note, 0),
894                                                    REG_RETVAL, NULL_RTX);
895                   REG_NOTES (new_libcall_insn)
896                     = gen_rtx_INSN_LIST (REG_LIBCALL, XEXP (note, 0),
897                                          REG_NOTES (new_libcall_insn));
898                   XEXP (retval_note, 0) = new_libcall_insn;
899                 }
900
901               if (dump_file)
902                 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
903
904               delete_insn_and_edges (insn);
905             }
906         }
907     }
908 }
909
910 \f
911 /* Fill in log links field for all insns.  */
912
913 static void
914 create_log_links (void)
915 {
916   basic_block bb;
917   rtx *next_use, insn;
918   struct df_ref **def_vec, **use_vec;
919
920   next_use = XCNEWVEC (rtx, max_reg_num ());
921
922   /* Pass through each block from the end, recording the uses of each
923      register and establishing log links when def is encountered.
924      Note that we do not clear next_use array in order to save time,
925      so we have to test whether the use is in the same basic block as def.
926               
927      There are a few cases below when we do not consider the definition or
928      usage -- these are taken from original flow.c did. Don't ask me why it is
929      done this way; I don't know and if it works, I don't want to know.  */
930
931   FOR_EACH_BB (bb)
932     {
933       FOR_BB_INSNS_REVERSE (bb, insn)
934         {
935           if (!INSN_P (insn))
936             continue;
937
938           /* Log links are created only once.  */
939           gcc_assert (!LOG_LINKS (insn));
940
941           for (def_vec = DF_INSN_DEFS (insn); *def_vec; def_vec++)
942             {
943               struct df_ref *def = *def_vec;
944               int regno = DF_REF_REGNO (def);
945               rtx use_insn;
946
947               if (!next_use[regno])
948                 continue;
949
950               /* Do not consider if it is pre/post modification in MEM.  */
951               if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
952                 continue;
953
954               /* Do not make the log link for frame pointer.  */
955               if ((regno == FRAME_POINTER_REGNUM
956                    && (! reload_completed || frame_pointer_needed))
957 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
958                   || (regno == HARD_FRAME_POINTER_REGNUM
959                       && (! reload_completed || frame_pointer_needed))
960 #endif
961 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
962                   || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
963 #endif
964                   )
965                 continue;
966
967               use_insn = next_use[regno];
968               if (BLOCK_FOR_INSN (use_insn) == bb)
969                 {
970                   /* flow.c claimed:
971
972                      We don't build a LOG_LINK for hard registers contained
973                      in ASM_OPERANDs.  If these registers get replaced,
974                      we might wind up changing the semantics of the insn,
975                      even if reload can make what appear to be valid
976                      assignments later.  */
977                   if (regno >= FIRST_PSEUDO_REGISTER
978                       || asm_noperands (PATTERN (use_insn)) < 0)
979                     LOG_LINKS (use_insn) =
980                       alloc_INSN_LIST (insn, LOG_LINKS (use_insn));
981                 }
982               next_use[regno] = NULL_RTX;
983             }
984
985           for (use_vec = DF_INSN_USES (insn); *use_vec; use_vec++)
986             {
987               struct df_ref *use = *use_vec;
988               int regno = DF_REF_REGNO (use);
989
990               /* Do not consider the usage of the stack pointer
991                  by function call.  */
992               if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
993                 continue;
994
995               next_use[regno] = insn;
996             }
997         }
998     }
999
1000   free (next_use);
1001 }
1002
1003 /* Clear LOG_LINKS fields of insns.  */
1004
1005 static void
1006 clear_log_links (void)
1007 {
1008   rtx insn;
1009
1010   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1011     if (INSN_P (insn))
1012       free_INSN_LIST_list (&LOG_LINKS (insn));
1013 }
1014
1015
1016
1017 \f
1018 /* Main entry point for combiner.  F is the first insn of the function.
1019    NREGS is the first unused pseudo-reg number.
1020
1021    Return nonzero if the combiner has turned an indirect jump
1022    instruction into a direct jump.  */
1023 static int
1024 combine_instructions (rtx f, unsigned int nregs)
1025 {
1026   rtx insn, next;
1027 #ifdef HAVE_cc0
1028   rtx prev;
1029 #endif
1030   rtx links, nextlinks;
1031   rtx first;
1032
1033   int new_direct_jump_p = 0;
1034
1035   for (first = f; first && !INSN_P (first); )
1036     first = NEXT_INSN (first);
1037   if (!first)
1038     return 0;
1039
1040   combine_attempts = 0;
1041   combine_merges = 0;
1042   combine_extras = 0;
1043   combine_successes = 0;
1044
1045   rtl_hooks = combine_rtl_hooks;
1046
1047   VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
1048
1049   init_recog_no_volatile ();
1050
1051   /* Allocate array for insn info.  */
1052   max_uid_known = get_max_uid ();
1053   uid_log_links = XCNEWVEC (rtx, max_uid_known + 1);
1054   uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1055
1056   nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1057
1058   /* Don't use reg_stat[].nonzero_bits when computing it.  This can cause
1059      problems when, for example, we have j <<= 1 in a loop.  */
1060
1061   nonzero_sign_valid = 0;
1062
1063   /* Scan all SETs and see if we can deduce anything about what
1064      bits are known to be zero for some registers and how many copies
1065      of the sign bit are known to exist for those registers.
1066
1067      Also set any known values so that we can use it while searching
1068      for what bits are known to be set.  */
1069
1070   label_tick = label_tick_ebb_start = 1;
1071
1072   setup_incoming_promotions (first);
1073
1074   create_log_links ();
1075   FOR_EACH_BB (this_basic_block)
1076     {
1077       last_call_luid = 0;
1078       mem_last_set = -1;
1079       label_tick++;
1080       FOR_BB_INSNS (this_basic_block, insn)
1081         if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1082           {
1083             subst_low_luid = DF_INSN_LUID (insn);
1084             subst_insn = insn;
1085
1086             note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1087                          insn);
1088             record_dead_and_set_regs (insn);
1089
1090 #ifdef AUTO_INC_DEC
1091             for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1092               if (REG_NOTE_KIND (links) == REG_INC)
1093                 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1094                                                   insn);
1095 #endif
1096
1097             /* Record the current insn_rtx_cost of this instruction.  */
1098             if (NONJUMP_INSN_P (insn))
1099               INSN_COST (insn) = insn_rtx_cost (PATTERN (insn));
1100             if (dump_file)
1101               fprintf(dump_file, "insn_cost %d: %d\n",
1102                     INSN_UID (insn), INSN_COST (insn));
1103           }
1104         else if (LABEL_P (insn))
1105           label_tick_ebb_start = label_tick;
1106     }
1107
1108   nonzero_sign_valid = 1;
1109
1110   /* Now scan all the insns in forward order.  */
1111
1112   label_tick = label_tick_ebb_start = 1;
1113   init_reg_last ();
1114   setup_incoming_promotions (first);
1115
1116   FOR_EACH_BB (this_basic_block)
1117     {
1118       last_call_luid = 0;
1119       mem_last_set = -1;
1120       label_tick++;
1121       for (insn = BB_HEAD (this_basic_block);
1122            insn != NEXT_INSN (BB_END (this_basic_block));
1123            insn = next ? next : NEXT_INSN (insn))
1124         {
1125           next = 0;
1126           if (INSN_P (insn))
1127             {
1128               /* See if we know about function return values before this
1129                  insn based upon SUBREG flags.  */
1130               check_conversions (insn, PATTERN (insn));
1131
1132               /* Try this insn with each insn it links back to.  */
1133
1134               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1135                 if ((next = try_combine (insn, XEXP (links, 0),
1136                                          NULL_RTX, &new_direct_jump_p)) != 0)
1137                   goto retry;
1138
1139               /* Try each sequence of three linked insns ending with this one.  */
1140
1141               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1142                 {
1143                   rtx link = XEXP (links, 0);
1144
1145                   /* If the linked insn has been replaced by a note, then there
1146                      is no point in pursuing this chain any further.  */
1147                   if (NOTE_P (link))
1148                     continue;
1149
1150                   for (nextlinks = LOG_LINKS (link);
1151                        nextlinks;
1152                        nextlinks = XEXP (nextlinks, 1))
1153                     if ((next = try_combine (insn, link,
1154                                              XEXP (nextlinks, 0),
1155                                              &new_direct_jump_p)) != 0)
1156                       goto retry;
1157                 }
1158
1159 #ifdef HAVE_cc0
1160               /* Try to combine a jump insn that uses CC0
1161                  with a preceding insn that sets CC0, and maybe with its
1162                  logical predecessor as well.
1163                  This is how we make decrement-and-branch insns.
1164                  We need this special code because data flow connections
1165                  via CC0 do not get entered in LOG_LINKS.  */
1166
1167               if (JUMP_P (insn)
1168                   && (prev = prev_nonnote_insn (insn)) != 0
1169                   && NONJUMP_INSN_P (prev)
1170                   && sets_cc0_p (PATTERN (prev)))
1171                 {
1172                   if ((next = try_combine (insn, prev,
1173                                            NULL_RTX, &new_direct_jump_p)) != 0)
1174                     goto retry;
1175
1176                   for (nextlinks = LOG_LINKS (prev); nextlinks;
1177                        nextlinks = XEXP (nextlinks, 1))
1178                     if ((next = try_combine (insn, prev,
1179                                              XEXP (nextlinks, 0),
1180                                              &new_direct_jump_p)) != 0)
1181                       goto retry;
1182                 }
1183
1184               /* Do the same for an insn that explicitly references CC0.  */
1185               if (NONJUMP_INSN_P (insn)
1186                   && (prev = prev_nonnote_insn (insn)) != 0
1187                   && NONJUMP_INSN_P (prev)
1188                   && sets_cc0_p (PATTERN (prev))
1189                   && GET_CODE (PATTERN (insn)) == SET
1190                   && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1191                 {
1192                   if ((next = try_combine (insn, prev,
1193                                            NULL_RTX, &new_direct_jump_p)) != 0)
1194                     goto retry;
1195
1196                   for (nextlinks = LOG_LINKS (prev); nextlinks;
1197                        nextlinks = XEXP (nextlinks, 1))
1198                     if ((next = try_combine (insn, prev,
1199                                              XEXP (nextlinks, 0),
1200                                              &new_direct_jump_p)) != 0)
1201                       goto retry;
1202                 }
1203
1204               /* Finally, see if any of the insns that this insn links to
1205                  explicitly references CC0.  If so, try this insn, that insn,
1206                  and its predecessor if it sets CC0.  */
1207               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1208                 if (NONJUMP_INSN_P (XEXP (links, 0))
1209                     && GET_CODE (PATTERN (XEXP (links, 0))) == SET
1210                     && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
1211                     && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
1212                     && NONJUMP_INSN_P (prev)
1213                     && sets_cc0_p (PATTERN (prev))
1214                     && (next = try_combine (insn, XEXP (links, 0),
1215                                             prev, &new_direct_jump_p)) != 0)
1216                   goto retry;
1217 #endif
1218
1219               /* Try combining an insn with two different insns whose results it
1220                  uses.  */
1221               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1222                 for (nextlinks = XEXP (links, 1); nextlinks;
1223                      nextlinks = XEXP (nextlinks, 1))
1224                   if ((next = try_combine (insn, XEXP (links, 0),
1225                                            XEXP (nextlinks, 0),
1226                                            &new_direct_jump_p)) != 0)
1227                     goto retry;
1228
1229               /* Try this insn with each REG_EQUAL note it links back to.  */
1230               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1231                 {
1232                   rtx set, note;
1233                   rtx temp = XEXP (links, 0);
1234                   if ((set = single_set (temp)) != 0
1235                       && (note = find_reg_equal_equiv_note (temp)) != 0
1236                       && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1237                       /* Avoid using a register that may already been marked
1238                          dead by an earlier instruction.  */
1239                       && ! unmentioned_reg_p (note, SET_SRC (set))
1240                       && (GET_MODE (note) == VOIDmode
1241                           ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1242                           : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1243                     {
1244                       /* Temporarily replace the set's source with the
1245                          contents of the REG_EQUAL note.  The insn will
1246                          be deleted or recognized by try_combine.  */
1247                       rtx orig = SET_SRC (set);
1248                       SET_SRC (set) = note;
1249                       i2mod = temp;
1250                       i2mod_old_rhs = copy_rtx (orig);
1251                       i2mod_new_rhs = copy_rtx (note);
1252                       next = try_combine (insn, i2mod, NULL_RTX,
1253                                           &new_direct_jump_p);
1254                       i2mod = NULL_RTX;
1255                       if (next)
1256                         goto retry;
1257                       SET_SRC (set) = orig;
1258                     }
1259                 }
1260
1261               if (!NOTE_P (insn))
1262                 record_dead_and_set_regs (insn);
1263
1264             retry:
1265               ;
1266             }
1267           else if (LABEL_P (insn))
1268             label_tick_ebb_start = label_tick;
1269         }
1270     }
1271
1272   clear_log_links ();
1273   clear_bb_flags ();
1274   new_direct_jump_p |= purge_all_dead_edges ();
1275   delete_noop_moves ();
1276
1277   /* Clean up.  */
1278   free (uid_log_links);
1279   free (uid_insn_cost);
1280   VEC_free (reg_stat_type, heap, reg_stat);
1281
1282   {
1283     struct undo *undo, *next;
1284     for (undo = undobuf.frees; undo; undo = next)
1285       {
1286         next = undo->next;
1287         free (undo);
1288       }
1289     undobuf.frees = 0;
1290   }
1291
1292   total_attempts += combine_attempts;
1293   total_merges += combine_merges;
1294   total_extras += combine_extras;
1295   total_successes += combine_successes;
1296
1297   nonzero_sign_valid = 0;
1298   rtl_hooks = general_rtl_hooks;
1299
1300   /* Make recognizer allow volatile MEMs again.  */
1301   init_recog ();
1302
1303   return new_direct_jump_p;
1304 }
1305
1306 /* Wipe the last_xxx fields of reg_stat in preparation for another pass.  */
1307
1308 static void
1309 init_reg_last (void)
1310 {
1311   unsigned int i;
1312   reg_stat_type *p;
1313
1314   for (i = 0; VEC_iterate (reg_stat_type, reg_stat, i, p); ++i)
1315     memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1316 }
1317 \f
1318 /* Set up any promoted values for incoming argument registers.  */
1319
1320 static void
1321 setup_incoming_promotions (rtx first)
1322 {
1323   tree arg;
1324   bool strictly_local = false;
1325
1326   if (!targetm.calls.promote_function_args (TREE_TYPE (cfun->decl)))
1327     return;
1328
1329   for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1330        arg = TREE_CHAIN (arg))
1331     {
1332       rtx reg = DECL_INCOMING_RTL (arg);
1333       int uns1, uns3;
1334       enum machine_mode mode1, mode2, mode3, mode4;
1335
1336       /* Only continue if the incoming argument is in a register.  */
1337       if (!REG_P (reg))
1338         continue;
1339
1340       /* Determine, if possible, whether all call sites of the current
1341          function lie within the current compilation unit.  (This does
1342          take into account the exporting of a function via taking its
1343          address, and so forth.)  */
1344       if (flag_unit_at_a_time)
1345         strictly_local = cgraph_local_info (current_function_decl)->local;
1346
1347       /* The mode and signedness of the argument before any promotions happen
1348          (equal to the mode of the pseudo holding it at that stage).  */
1349       mode1 = TYPE_MODE (TREE_TYPE (arg));
1350       uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1351
1352       /* The mode and signedness of the argument after any source language and
1353          TARGET_PROMOTE_PROTOTYPES-driven promotions.  */
1354       mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1355       uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1356
1357       /* The mode and signedness of the argument as it is actually passed, 
1358          after any TARGET_PROMOTE_FUNCTION_ARGS-driven ABI promotions.  */
1359       mode3 = promote_mode (DECL_ARG_TYPE (arg), mode2, &uns3, 1);
1360
1361       /* The mode of the register in which the argument is being passed.  */
1362       mode4 = GET_MODE (reg);
1363
1364       /* Eliminate sign extensions in the callee when possible.  Only
1365          do this when:
1366          (a) a mode promotion has occurred;
1367          (b) the mode of the register is the same as the mode of
1368              the argument as it is passed; and
1369          (c) the signedness does not change across any of the promotions; and
1370          (d) when no language-level promotions (which we cannot guarantee
1371              will have been done by an external caller) are necessary,
1372              unless we know that this function is only ever called from
1373              the current compilation unit -- all of whose call sites will
1374              do the mode1 --> mode2 promotion.  */
1375       if (mode1 != mode3
1376           && mode3 == mode4
1377           && uns1 == uns3
1378           && (mode1 == mode2 || strictly_local))
1379         {
1380           /* Record that the value was promoted from mode1 to mode3,
1381              so that any sign extension at the head of the current
1382              function may be eliminated.  */
1383           rtx x;
1384           x = gen_rtx_CLOBBER (mode1, const0_rtx);
1385           x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1386           record_value_for_reg (reg, first, x);
1387         }
1388     }
1389 }
1390
1391 /* Called via note_stores.  If X is a pseudo that is narrower than
1392    HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1393
1394    If we are setting only a portion of X and we can't figure out what
1395    portion, assume all bits will be used since we don't know what will
1396    be happening.
1397
1398    Similarly, set how many bits of X are known to be copies of the sign bit
1399    at all locations in the function.  This is the smallest number implied
1400    by any set of X.  */
1401
1402 static void
1403 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1404 {
1405   rtx insn = (rtx) data;
1406   unsigned int num;
1407
1408   if (REG_P (x)
1409       && REGNO (x) >= FIRST_PSEUDO_REGISTER
1410       /* If this register is undefined at the start of the file, we can't
1411          say what its contents were.  */
1412       && ! REGNO_REG_SET_P
1413            (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x))
1414       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
1415     {
1416       reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
1417
1418       if (set == 0 || GET_CODE (set) == CLOBBER)
1419         {
1420           rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1421           rsp->sign_bit_copies = 1;
1422           return;
1423         }
1424
1425       /* If this register is being initialized using itself, and the
1426          register is uninitialized in this basic block, and there are
1427          no LOG_LINKS which set the register, then part of the
1428          register is uninitialized.  In that case we can't assume
1429          anything about the number of nonzero bits.
1430
1431          ??? We could do better if we checked this in
1432          reg_{nonzero_bits,num_sign_bit_copies}_for_combine.  Then we
1433          could avoid making assumptions about the insn which initially
1434          sets the register, while still using the information in other
1435          insns.  We would have to be careful to check every insn
1436          involved in the combination.  */
1437
1438       if (insn
1439           && reg_referenced_p (x, PATTERN (insn))
1440           && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1441                                REGNO (x)))
1442         {
1443           rtx link;
1444
1445           for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
1446             {
1447               if (dead_or_set_p (XEXP (link, 0), x))
1448                 break;
1449             }
1450           if (!link)
1451             {
1452               rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1453               rsp->sign_bit_copies = 1;
1454               return;
1455             }
1456         }
1457
1458       /* If this is a complex assignment, see if we can convert it into a
1459          simple assignment.  */
1460       set = expand_field_assignment (set);
1461
1462       /* If this is a simple assignment, or we have a paradoxical SUBREG,
1463          set what we know about X.  */
1464
1465       if (SET_DEST (set) == x
1466           || (GET_CODE (SET_DEST (set)) == SUBREG
1467               && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
1468                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
1469               && SUBREG_REG (SET_DEST (set)) == x))
1470         {
1471           rtx src = SET_SRC (set);
1472
1473 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1474           /* If X is narrower than a word and SRC is a non-negative
1475              constant that would appear negative in the mode of X,
1476              sign-extend it for use in reg_stat[].nonzero_bits because some
1477              machines (maybe most) will actually do the sign-extension
1478              and this is the conservative approach.
1479
1480              ??? For 2.5, try to tighten up the MD files in this regard
1481              instead of this kludge.  */
1482
1483           if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
1484               && GET_CODE (src) == CONST_INT
1485               && INTVAL (src) > 0
1486               && 0 != (INTVAL (src)
1487                        & ((HOST_WIDE_INT) 1
1488                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
1489             src = GEN_INT (INTVAL (src)
1490                            | ((HOST_WIDE_INT) (-1)
1491                               << GET_MODE_BITSIZE (GET_MODE (x))));
1492 #endif
1493
1494           /* Don't call nonzero_bits if it cannot change anything.  */
1495           if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1496             rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1497           num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1498           if (rsp->sign_bit_copies == 0
1499               || rsp->sign_bit_copies > num)
1500             rsp->sign_bit_copies = num;
1501         }
1502       else
1503         {
1504           rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1505           rsp->sign_bit_copies = 1;
1506         }
1507     }
1508 }
1509 \f
1510 /* See if INSN can be combined into I3.  PRED and SUCC are optionally
1511    insns that were previously combined into I3 or that will be combined
1512    into the merger of INSN and I3.
1513
1514    Return 0 if the combination is not allowed for any reason.
1515
1516    If the combination is allowed, *PDEST will be set to the single
1517    destination of INSN and *PSRC to the single source, and this function
1518    will return 1.  */
1519
1520 static int
1521 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
1522                rtx *pdest, rtx *psrc)
1523 {
1524   int i;
1525   const_rtx set = 0;
1526   rtx src, dest;
1527   rtx p;
1528 #ifdef AUTO_INC_DEC
1529   rtx link;
1530 #endif
1531   int all_adjacent = (succ ? (next_active_insn (insn) == succ
1532                               && next_active_insn (succ) == i3)
1533                       : next_active_insn (insn) == i3);
1534
1535   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1536      or a PARALLEL consisting of such a SET and CLOBBERs.
1537
1538      If INSN has CLOBBER parallel parts, ignore them for our processing.
1539      By definition, these happen during the execution of the insn.  When it
1540      is merged with another insn, all bets are off.  If they are, in fact,
1541      needed and aren't also supplied in I3, they may be added by
1542      recog_for_combine.  Otherwise, it won't match.
1543
1544      We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1545      note.
1546
1547      Get the source and destination of INSN.  If more than one, can't
1548      combine.  */
1549
1550   if (GET_CODE (PATTERN (insn)) == SET)
1551     set = PATTERN (insn);
1552   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1553            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1554     {
1555       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1556         {
1557           rtx elt = XVECEXP (PATTERN (insn), 0, i);
1558           rtx note;
1559
1560           switch (GET_CODE (elt))
1561             {
1562             /* This is important to combine floating point insns
1563                for the SH4 port.  */
1564             case USE:
1565               /* Combining an isolated USE doesn't make sense.
1566                  We depend here on combinable_i3pat to reject them.  */
1567               /* The code below this loop only verifies that the inputs of
1568                  the SET in INSN do not change.  We call reg_set_between_p
1569                  to verify that the REG in the USE does not change between
1570                  I3 and INSN.
1571                  If the USE in INSN was for a pseudo register, the matching
1572                  insn pattern will likely match any register; combining this
1573                  with any other USE would only be safe if we knew that the
1574                  used registers have identical values, or if there was
1575                  something to tell them apart, e.g. different modes.  For
1576                  now, we forgo such complicated tests and simply disallow
1577                  combining of USES of pseudo registers with any other USE.  */
1578               if (REG_P (XEXP (elt, 0))
1579                   && GET_CODE (PATTERN (i3)) == PARALLEL)
1580                 {
1581                   rtx i3pat = PATTERN (i3);
1582                   int i = XVECLEN (i3pat, 0) - 1;
1583                   unsigned int regno = REGNO (XEXP (elt, 0));
1584
1585                   do
1586                     {
1587                       rtx i3elt = XVECEXP (i3pat, 0, i);
1588
1589                       if (GET_CODE (i3elt) == USE
1590                           && REG_P (XEXP (i3elt, 0))
1591                           && (REGNO (XEXP (i3elt, 0)) == regno
1592                               ? reg_set_between_p (XEXP (elt, 0),
1593                                                    PREV_INSN (insn), i3)
1594                               : regno >= FIRST_PSEUDO_REGISTER))
1595                         return 0;
1596                     }
1597                   while (--i >= 0);
1598                 }
1599               break;
1600
1601               /* We can ignore CLOBBERs.  */
1602             case CLOBBER:
1603               break;
1604
1605             case SET:
1606               /* Ignore SETs whose result isn't used but not those that
1607                  have side-effects.  */
1608               if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1609                   && (!(note = find_reg_note (insn, REG_EH_REGION, NULL_RTX))
1610                       || INTVAL (XEXP (note, 0)) <= 0)
1611                   && ! side_effects_p (elt))
1612                 break;
1613
1614               /* If we have already found a SET, this is a second one and
1615                  so we cannot combine with this insn.  */
1616               if (set)
1617                 return 0;
1618
1619               set = elt;
1620               break;
1621
1622             default:
1623               /* Anything else means we can't combine.  */
1624               return 0;
1625             }
1626         }
1627
1628       if (set == 0
1629           /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1630              so don't do anything with it.  */
1631           || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1632         return 0;
1633     }
1634   else
1635     return 0;
1636
1637   if (set == 0)
1638     return 0;
1639
1640   set = expand_field_assignment (set);
1641   src = SET_SRC (set), dest = SET_DEST (set);
1642
1643   /* Don't eliminate a store in the stack pointer.  */
1644   if (dest == stack_pointer_rtx
1645       /* Don't combine with an insn that sets a register to itself if it has
1646          a REG_EQUAL note.  This may be part of a REG_NO_CONFLICT sequence.  */
1647       || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1648       /* Can't merge an ASM_OPERANDS.  */
1649       || GET_CODE (src) == ASM_OPERANDS
1650       /* Can't merge a function call.  */
1651       || GET_CODE (src) == CALL
1652       /* Don't eliminate a function call argument.  */
1653       || (CALL_P (i3)
1654           && (find_reg_fusage (i3, USE, dest)
1655               || (REG_P (dest)
1656                   && REGNO (dest) < FIRST_PSEUDO_REGISTER
1657                   && global_regs[REGNO (dest)])))
1658       /* Don't substitute into an incremented register.  */
1659       || FIND_REG_INC_NOTE (i3, dest)
1660       || (succ && FIND_REG_INC_NOTE (succ, dest))
1661       /* Don't substitute into a non-local goto, this confuses CFG.  */
1662       || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1663 #if 0
1664       /* Don't combine the end of a libcall into anything.  */
1665       /* ??? This gives worse code, and appears to be unnecessary, since no
1666          pass after flow uses REG_LIBCALL/REG_RETVAL notes.  Local-alloc does
1667          use REG_RETVAL notes for noconflict blocks, but other code here
1668          makes sure that those insns don't disappear.  */
1669       || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1670 #endif
1671       /* Make sure that DEST is not used after SUCC but before I3.  */
1672       || (succ && ! all_adjacent
1673           && reg_used_between_p (dest, succ, i3))
1674       /* Make sure that the value that is to be substituted for the register
1675          does not use any registers whose values alter in between.  However,
1676          If the insns are adjacent, a use can't cross a set even though we
1677          think it might (this can happen for a sequence of insns each setting
1678          the same destination; last_set of that register might point to
1679          a NOTE).  If INSN has a REG_EQUIV note, the register is always
1680          equivalent to the memory so the substitution is valid even if there
1681          are intervening stores.  Also, don't move a volatile asm or
1682          UNSPEC_VOLATILE across any other insns.  */
1683       || (! all_adjacent
1684           && (((!MEM_P (src)
1685                 || ! find_reg_note (insn, REG_EQUIV, src))
1686                && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1687               || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1688               || GET_CODE (src) == UNSPEC_VOLATILE))
1689       /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1690          better register allocation by not doing the combine.  */
1691       || find_reg_note (i3, REG_NO_CONFLICT, dest)
1692       || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1693       /* Don't combine across a CALL_INSN, because that would possibly
1694          change whether the life span of some REGs crosses calls or not,
1695          and it is a pain to update that information.
1696          Exception: if source is a constant, moving it later can't hurt.
1697          Accept that as a special case.  */
1698       || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1699     return 0;
1700
1701   /* DEST must either be a REG or CC0.  */
1702   if (REG_P (dest))
1703     {
1704       /* If register alignment is being enforced for multi-word items in all
1705          cases except for parameters, it is possible to have a register copy
1706          insn referencing a hard register that is not allowed to contain the
1707          mode being copied and which would not be valid as an operand of most
1708          insns.  Eliminate this problem by not combining with such an insn.
1709
1710          Also, on some machines we don't want to extend the life of a hard
1711          register.  */
1712
1713       if (REG_P (src)
1714           && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1715                && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1716               /* Don't extend the life of a hard register unless it is
1717                  user variable (if we have few registers) or it can't
1718                  fit into the desired register (meaning something special
1719                  is going on).
1720                  Also avoid substituting a return register into I3, because
1721                  reload can't handle a conflict with constraints of other
1722                  inputs.  */
1723               || (REGNO (src) < FIRST_PSEUDO_REGISTER
1724                   && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1725         return 0;
1726     }
1727   else if (GET_CODE (dest) != CC0)
1728     return 0;
1729
1730
1731   if (GET_CODE (PATTERN (i3)) == PARALLEL)
1732     for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1733       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1734         {
1735           /* Don't substitute for a register intended as a clobberable
1736              operand.  */
1737           rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1738           if (rtx_equal_p (reg, dest))
1739             return 0;
1740
1741           /* If the clobber represents an earlyclobber operand, we must not
1742              substitute an expression containing the clobbered register.
1743              As we do not analyze the constraint strings here, we have to
1744              make the conservative assumption.  However, if the register is
1745              a fixed hard reg, the clobber cannot represent any operand;
1746              we leave it up to the machine description to either accept or
1747              reject use-and-clobber patterns.  */
1748           if (!REG_P (reg)
1749               || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1750               || !fixed_regs[REGNO (reg)])
1751             if (reg_overlap_mentioned_p (reg, src))
1752               return 0;
1753         }
1754
1755   /* If INSN contains anything volatile, or is an `asm' (whether volatile
1756      or not), reject, unless nothing volatile comes between it and I3 */
1757
1758   if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1759     {
1760       /* Make sure succ doesn't contain a volatile reference.  */
1761       if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1762         return 0;
1763
1764       for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1765         if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1766           return 0;
1767     }
1768
1769   /* If INSN is an asm, and DEST is a hard register, reject, since it has
1770      to be an explicit register variable, and was chosen for a reason.  */
1771
1772   if (GET_CODE (src) == ASM_OPERANDS
1773       && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1774     return 0;
1775
1776   /* If there are any volatile insns between INSN and I3, reject, because
1777      they might affect machine state.  */
1778
1779   for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1780     if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1781       return 0;
1782
1783   /* If INSN contains an autoincrement or autodecrement, make sure that
1784      register is not used between there and I3, and not already used in
1785      I3 either.  Neither must it be used in PRED or SUCC, if they exist.
1786      Also insist that I3 not be a jump; if it were one
1787      and the incremented register were spilled, we would lose.  */
1788
1789 #ifdef AUTO_INC_DEC
1790   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1791     if (REG_NOTE_KIND (link) == REG_INC
1792         && (JUMP_P (i3)
1793             || reg_used_between_p (XEXP (link, 0), insn, i3)
1794             || (pred != NULL_RTX
1795                 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1796             || (succ != NULL_RTX
1797                 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
1798             || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1799       return 0;
1800 #endif
1801
1802 #ifdef HAVE_cc0
1803   /* Don't combine an insn that follows a CC0-setting insn.
1804      An insn that uses CC0 must not be separated from the one that sets it.
1805      We do, however, allow I2 to follow a CC0-setting insn if that insn
1806      is passed as I1; in that case it will be deleted also.
1807      We also allow combining in this case if all the insns are adjacent
1808      because that would leave the two CC0 insns adjacent as well.
1809      It would be more logical to test whether CC0 occurs inside I1 or I2,
1810      but that would be much slower, and this ought to be equivalent.  */
1811
1812   p = prev_nonnote_insn (insn);
1813   if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1814       && ! all_adjacent)
1815     return 0;
1816 #endif
1817
1818   /* If we get here, we have passed all the tests and the combination is
1819      to be allowed.  */
1820
1821   *pdest = dest;
1822   *psrc = src;
1823
1824   return 1;
1825 }
1826 \f
1827 /* LOC is the location within I3 that contains its pattern or the component
1828    of a PARALLEL of the pattern.  We validate that it is valid for combining.
1829
1830    One problem is if I3 modifies its output, as opposed to replacing it
1831    entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1832    so would produce an insn that is not equivalent to the original insns.
1833
1834    Consider:
1835
1836          (set (reg:DI 101) (reg:DI 100))
1837          (set (subreg:SI (reg:DI 101) 0) <foo>)
1838
1839    This is NOT equivalent to:
1840
1841          (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1842                     (set (reg:DI 101) (reg:DI 100))])
1843
1844    Not only does this modify 100 (in which case it might still be valid
1845    if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1846
1847    We can also run into a problem if I2 sets a register that I1
1848    uses and I1 gets directly substituted into I3 (not via I2).  In that
1849    case, we would be getting the wrong value of I2DEST into I3, so we
1850    must reject the combination.  This case occurs when I2 and I1 both
1851    feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1852    If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1853    of a SET must prevent combination from occurring.
1854
1855    Before doing the above check, we first try to expand a field assignment
1856    into a set of logical operations.
1857
1858    If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1859    we place a register that is both set and used within I3.  If more than one
1860    such register is detected, we fail.
1861
1862    Return 1 if the combination is valid, zero otherwise.  */
1863
1864 static int
1865 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
1866                   int i1_not_in_src, rtx *pi3dest_killed)
1867 {
1868   rtx x = *loc;
1869
1870   if (GET_CODE (x) == SET)
1871     {
1872       rtx set = x ;
1873       rtx dest = SET_DEST (set);
1874       rtx src = SET_SRC (set);
1875       rtx inner_dest = dest;
1876       rtx subdest;
1877
1878       while (GET_CODE (inner_dest) == STRICT_LOW_PART
1879              || GET_CODE (inner_dest) == SUBREG
1880              || GET_CODE (inner_dest) == ZERO_EXTRACT)
1881         inner_dest = XEXP (inner_dest, 0);
1882
1883       /* Check for the case where I3 modifies its output, as discussed
1884          above.  We don't want to prevent pseudos from being combined
1885          into the address of a MEM, so only prevent the combination if
1886          i1 or i2 set the same MEM.  */
1887       if ((inner_dest != dest &&
1888            (!MEM_P (inner_dest)
1889             || rtx_equal_p (i2dest, inner_dest)
1890             || (i1dest && rtx_equal_p (i1dest, inner_dest)))
1891            && (reg_overlap_mentioned_p (i2dest, inner_dest)
1892                || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1893
1894           /* This is the same test done in can_combine_p except we can't test
1895              all_adjacent; we don't have to, since this instruction will stay
1896              in place, thus we are not considering increasing the lifetime of
1897              INNER_DEST.
1898
1899              Also, if this insn sets a function argument, combining it with
1900              something that might need a spill could clobber a previous
1901              function argument; the all_adjacent test in can_combine_p also
1902              checks this; here, we do a more specific test for this case.  */
1903
1904           || (REG_P (inner_dest)
1905               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1906               && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1907                                         GET_MODE (inner_dest))))
1908           || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1909         return 0;
1910
1911       /* If DEST is used in I3, it is being killed in this insn, so
1912          record that for later.  We have to consider paradoxical
1913          subregs here, since they kill the whole register, but we
1914          ignore partial subregs, STRICT_LOW_PART, etc.
1915          Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1916          STACK_POINTER_REGNUM, since these are always considered to be
1917          live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
1918       subdest = dest;
1919       if (GET_CODE (subdest) == SUBREG
1920           && (GET_MODE_SIZE (GET_MODE (subdest))
1921               >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
1922         subdest = SUBREG_REG (subdest);
1923       if (pi3dest_killed
1924           && REG_P (subdest)
1925           && reg_referenced_p (subdest, PATTERN (i3))
1926           && REGNO (subdest) != FRAME_POINTER_REGNUM
1927 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1928           && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
1929 #endif
1930 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1931           && (REGNO (subdest) != ARG_POINTER_REGNUM
1932               || ! fixed_regs [REGNO (subdest)])
1933 #endif
1934           && REGNO (subdest) != STACK_POINTER_REGNUM)
1935         {
1936           if (*pi3dest_killed)
1937             return 0;
1938
1939           *pi3dest_killed = subdest;
1940         }
1941     }
1942
1943   else if (GET_CODE (x) == PARALLEL)
1944     {
1945       int i;
1946
1947       for (i = 0; i < XVECLEN (x, 0); i++)
1948         if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1949                                 i1_not_in_src, pi3dest_killed))
1950           return 0;
1951     }
1952
1953   return 1;
1954 }
1955 \f
1956 /* Return 1 if X is an arithmetic expression that contains a multiplication
1957    and division.  We don't count multiplications by powers of two here.  */
1958
1959 static int
1960 contains_muldiv (rtx x)
1961 {
1962   switch (GET_CODE (x))
1963     {
1964     case MOD:  case DIV:  case UMOD:  case UDIV:
1965       return 1;
1966
1967     case MULT:
1968       return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1969                 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1970     default:
1971       if (BINARY_P (x))
1972         return contains_muldiv (XEXP (x, 0))
1973             || contains_muldiv (XEXP (x, 1));
1974
1975       if (UNARY_P (x))
1976         return contains_muldiv (XEXP (x, 0));
1977
1978       return 0;
1979     }
1980 }
1981 \f
1982 /* Determine whether INSN can be used in a combination.  Return nonzero if
1983    not.  This is used in try_combine to detect early some cases where we
1984    can't perform combinations.  */
1985
1986 static int
1987 cant_combine_insn_p (rtx insn)
1988 {
1989   rtx set;
1990   rtx src, dest;
1991
1992   /* If this isn't really an insn, we can't do anything.
1993      This can occur when flow deletes an insn that it has merged into an
1994      auto-increment address.  */
1995   if (! INSN_P (insn))
1996     return 1;
1997
1998   /* Never combine loads and stores involving hard regs that are likely
1999      to be spilled.  The register allocator can usually handle such
2000      reg-reg moves by tying.  If we allow the combiner to make
2001      substitutions of likely-spilled regs, reload might die.
2002      As an exception, we allow combinations involving fixed regs; these are
2003      not available to the register allocator so there's no risk involved.  */
2004
2005   set = single_set (insn);
2006   if (! set)
2007     return 0;
2008   src = SET_SRC (set);
2009   dest = SET_DEST (set);
2010   if (GET_CODE (src) == SUBREG)
2011     src = SUBREG_REG (src);
2012   if (GET_CODE (dest) == SUBREG)
2013     dest = SUBREG_REG (dest);
2014   if (REG_P (src) && REG_P (dest)
2015       && ((REGNO (src) < FIRST_PSEUDO_REGISTER
2016            && ! fixed_regs[REGNO (src)]
2017            && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
2018           || (REGNO (dest) < FIRST_PSEUDO_REGISTER
2019               && ! fixed_regs[REGNO (dest)]
2020               && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
2021     return 1;
2022
2023   return 0;
2024 }
2025
2026 struct likely_spilled_retval_info
2027 {
2028   unsigned regno, nregs;
2029   unsigned mask;
2030 };
2031
2032 /* Called via note_stores by likely_spilled_retval_p.  Remove from info->mask
2033    hard registers that are known to be written to / clobbered in full.  */
2034 static void
2035 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2036 {
2037   struct likely_spilled_retval_info *info = data;
2038   unsigned regno, nregs;
2039   unsigned new_mask;
2040
2041   if (!REG_P (XEXP (set, 0)))
2042     return;
2043   regno = REGNO (x);
2044   if (regno >= info->regno + info->nregs)
2045     return;
2046   nregs = hard_regno_nregs[regno][GET_MODE (x)];
2047   if (regno + nregs <= info->regno)
2048     return;
2049   new_mask = (2U << (nregs - 1)) - 1;
2050   if (regno < info->regno)
2051     new_mask >>= info->regno - regno;
2052   else
2053     new_mask <<= regno - info->regno;
2054   info->mask &= ~new_mask;
2055 }
2056
2057 /* Return nonzero iff part of the return value is live during INSN, and
2058    it is likely spilled.  This can happen when more than one insn is needed
2059    to copy the return value, e.g. when we consider to combine into the
2060    second copy insn for a complex value.  */
2061
2062 static int
2063 likely_spilled_retval_p (rtx insn)
2064 {
2065   rtx use = BB_END (this_basic_block);
2066   rtx reg, p;
2067   unsigned regno, nregs;
2068   /* We assume here that no machine mode needs more than
2069      32 hard registers when the value overlaps with a register
2070      for which FUNCTION_VALUE_REGNO_P is true.  */
2071   unsigned mask;
2072   struct likely_spilled_retval_info info;
2073
2074   if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2075     return 0;
2076   reg = XEXP (PATTERN (use), 0);
2077   if (!REG_P (reg) || !FUNCTION_VALUE_REGNO_P (REGNO (reg)))
2078     return 0;
2079   regno = REGNO (reg);
2080   nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2081   if (nregs == 1)
2082     return 0;
2083   mask = (2U << (nregs - 1)) - 1;
2084
2085   /* Disregard parts of the return value that are set later.  */
2086   info.regno = regno;
2087   info.nregs = nregs;
2088   info.mask = mask;
2089   for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2090     if (INSN_P (p))
2091       note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2092   mask = info.mask;
2093
2094   /* Check if any of the (probably) live return value registers is
2095      likely spilled.  */
2096   nregs --;
2097   do
2098     {
2099       if ((mask & 1 << nregs)
2100           && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (regno + nregs)))
2101         return 1;
2102     } while (nregs--);
2103   return 0;
2104 }
2105
2106 /* Adjust INSN after we made a change to its destination.
2107
2108    Changing the destination can invalidate notes that say something about
2109    the results of the insn and a LOG_LINK pointing to the insn.  */
2110
2111 static void
2112 adjust_for_new_dest (rtx insn)
2113 {
2114   /* For notes, be conservative and simply remove them.  */
2115   remove_reg_equal_equiv_notes (insn);
2116
2117   /* The new insn will have a destination that was previously the destination
2118      of an insn just above it.  Call distribute_links to make a LOG_LINK from
2119      the next use of that destination.  */
2120   distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
2121
2122   df_insn_rescan (insn);
2123 }
2124
2125 /* Return TRUE if combine can reuse reg X in mode MODE.
2126    ADDED_SETS is nonzero if the original set is still required.  */
2127 static bool
2128 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
2129 {
2130   unsigned int regno;
2131
2132   if (!REG_P(x))
2133     return false;
2134
2135   regno = REGNO (x);
2136   /* Allow hard registers if the new mode is legal, and occupies no more
2137      registers than the old mode.  */
2138   if (regno < FIRST_PSEUDO_REGISTER)
2139     return (HARD_REGNO_MODE_OK (regno, mode)
2140             && (hard_regno_nregs[regno][GET_MODE (x)]
2141                 >= hard_regno_nregs[regno][mode]));
2142
2143   /* Or a pseudo that is only used once.  */
2144   return (REG_N_SETS (regno) == 1 && !added_sets
2145           && !REG_USERVAR_P (x));
2146 }
2147
2148
2149 /* Check whether X, the destination of a set, refers to part of
2150    the register specified by REG.  */
2151
2152 static bool
2153 reg_subword_p (rtx x, rtx reg)
2154 {
2155   /* Check that reg is an integer mode register.  */
2156   if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2157     return false;
2158
2159   if (GET_CODE (x) == STRICT_LOW_PART
2160       || GET_CODE (x) == ZERO_EXTRACT)
2161     x = XEXP (x, 0);
2162
2163   return GET_CODE (x) == SUBREG
2164          && SUBREG_REG (x) == reg
2165          && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2166 }
2167
2168
2169 /* Try to combine the insns I1 and I2 into I3.
2170    Here I1 and I2 appear earlier than I3.
2171    I1 can be zero; then we combine just I2 into I3.
2172
2173    If we are combining three insns and the resulting insn is not recognized,
2174    try splitting it into two insns.  If that happens, I2 and I3 are retained
2175    and I1 is pseudo-deleted by turning it into a NOTE.  Otherwise, I1 and I2
2176    are pseudo-deleted.
2177
2178    Return 0 if the combination does not work.  Then nothing is changed.
2179    If we did the combination, return the insn at which combine should
2180    resume scanning.
2181
2182    Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2183    new direct jump instruction.  */
2184
2185 static rtx
2186 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
2187 {
2188   /* New patterns for I3 and I2, respectively.  */
2189   rtx newpat, newi2pat = 0;
2190   rtvec newpat_vec_with_clobbers = 0;
2191   int substed_i2 = 0, substed_i1 = 0;
2192   /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead.  */
2193   int added_sets_1, added_sets_2;
2194   /* Total number of SETs to put into I3.  */
2195   int total_sets;
2196   /* Nonzero if I2's body now appears in I3.  */
2197   int i2_is_used;
2198   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
2199   int insn_code_number, i2_code_number = 0, other_code_number = 0;
2200   /* Contains I3 if the destination of I3 is used in its source, which means
2201      that the old life of I3 is being killed.  If that usage is placed into
2202      I2 and not in I3, a REG_DEAD note must be made.  */
2203   rtx i3dest_killed = 0;
2204   /* SET_DEST and SET_SRC of I2 and I1.  */
2205   rtx i2dest, i2src, i1dest = 0, i1src = 0;
2206   /* PATTERN (I1) and PATTERN (I2), or a copy of it in certain cases.  */
2207   rtx i1pat = 0, i2pat = 0;
2208   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
2209   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2210   int i2dest_killed = 0, i1dest_killed = 0;
2211   int i1_feeds_i3 = 0;
2212   /* Notes that must be added to REG_NOTES in I3 and I2.  */
2213   rtx new_i3_notes, new_i2_notes;
2214   /* Notes that we substituted I3 into I2 instead of the normal case.  */
2215   int i3_subst_into_i2 = 0;
2216   /* Notes that I1, I2 or I3 is a MULT operation.  */
2217   int have_mult = 0;
2218   int swap_i2i3 = 0;
2219
2220   int maxreg;
2221   rtx temp;
2222   rtx link;
2223   rtx other_pat = 0;
2224   rtx new_other_notes;
2225   int i;
2226
2227   /* Exit early if one of the insns involved can't be used for
2228      combinations.  */
2229   if (cant_combine_insn_p (i3)
2230       || cant_combine_insn_p (i2)
2231       || (i1 && cant_combine_insn_p (i1))
2232       || likely_spilled_retval_p (i3)
2233       /* We also can't do anything if I3 has a
2234          REG_LIBCALL note since we don't want to disrupt the contiguity of a
2235          libcall.  */
2236 #if 0
2237       /* ??? This gives worse code, and appears to be unnecessary, since no
2238          pass after flow uses REG_LIBCALL/REG_RETVAL notes.  */
2239       || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
2240 #endif
2241       )
2242     return 0;
2243
2244   combine_attempts++;
2245   undobuf.other_insn = 0;
2246
2247   /* Reset the hard register usage information.  */
2248   CLEAR_HARD_REG_SET (newpat_used_regs);
2249
2250   /* If I1 and I2 both feed I3, they can be in any order.  To simplify the
2251      code below, set I1 to be the earlier of the two insns.  */
2252   if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2253     temp = i1, i1 = i2, i2 = temp;
2254
2255   added_links_insn = 0;
2256
2257   /* First check for one important special-case that the code below will
2258      not handle.  Namely, the case where I1 is zero, I2 is a PARALLEL
2259      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
2260      we may be able to replace that destination with the destination of I3.
2261      This occurs in the common code where we compute both a quotient and
2262      remainder into a structure, in which case we want to do the computation
2263      directly into the structure to avoid register-register copies.
2264
2265      Note that this case handles both multiple sets in I2 and also
2266      cases where I2 has a number of CLOBBER or PARALLELs.
2267
2268      We make very conservative checks below and only try to handle the
2269      most common cases of this.  For example, we only handle the case
2270      where I2 and I3 are adjacent to avoid making difficult register
2271      usage tests.  */
2272
2273   if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2274       && REG_P (SET_SRC (PATTERN (i3)))
2275       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2276       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2277       && GET_CODE (PATTERN (i2)) == PARALLEL
2278       && ! side_effects_p (SET_DEST (PATTERN (i3)))
2279       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2280          below would need to check what is inside (and reg_overlap_mentioned_p
2281          doesn't support those codes anyway).  Don't allow those destinations;
2282          the resulting insn isn't likely to be recognized anyway.  */
2283       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2284       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2285       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2286                                     SET_DEST (PATTERN (i3)))
2287       && next_real_insn (i2) == i3)
2288     {
2289       rtx p2 = PATTERN (i2);
2290
2291       /* Make sure that the destination of I3,
2292          which we are going to substitute into one output of I2,
2293          is not used within another output of I2.  We must avoid making this:
2294          (parallel [(set (mem (reg 69)) ...)
2295                     (set (reg 69) ...)])
2296          which is not well-defined as to order of actions.
2297          (Besides, reload can't handle output reloads for this.)
2298
2299          The problem can also happen if the dest of I3 is a memory ref,
2300          if another dest in I2 is an indirect memory ref.  */
2301       for (i = 0; i < XVECLEN (p2, 0); i++)
2302         if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2303              || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2304             && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2305                                         SET_DEST (XVECEXP (p2, 0, i))))
2306           break;
2307
2308       if (i == XVECLEN (p2, 0))
2309         for (i = 0; i < XVECLEN (p2, 0); i++)
2310           if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2311                || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2312               && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2313             {
2314               combine_merges++;
2315
2316               subst_insn = i3;
2317               subst_low_luid = DF_INSN_LUID (i2);
2318
2319               added_sets_2 = added_sets_1 = 0;
2320               i2dest = SET_SRC (PATTERN (i3));
2321               i2dest_killed = dead_or_set_p (i2, i2dest);
2322
2323               /* Replace the dest in I2 with our dest and make the resulting
2324                  insn the new pattern for I3.  Then skip to where we
2325                  validate the pattern.  Everything was set up above.  */
2326               SUBST (SET_DEST (XVECEXP (p2, 0, i)),
2327                      SET_DEST (PATTERN (i3)));
2328
2329               newpat = p2;
2330               i3_subst_into_i2 = 1;
2331               goto validate_replacement;
2332             }
2333     }
2334
2335   /* If I2 is setting a pseudo to a constant and I3 is setting some
2336      sub-part of it to another constant, merge them by making a new
2337      constant.  */
2338   if (i1 == 0
2339       && (temp = single_set (i2)) != 0
2340       && (GET_CODE (SET_SRC (temp)) == CONST_INT
2341           || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
2342       && GET_CODE (PATTERN (i3)) == SET
2343       && (GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT
2344           || GET_CODE (SET_SRC (PATTERN (i3))) == CONST_DOUBLE)
2345       && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp)))
2346     {
2347       rtx dest = SET_DEST (PATTERN (i3));
2348       int offset = -1;
2349       int width = 0;
2350
2351       if (GET_CODE (dest) == ZERO_EXTRACT)
2352         {
2353           if (GET_CODE (XEXP (dest, 1)) == CONST_INT
2354               && GET_CODE (XEXP (dest, 2)) == CONST_INT)
2355             {
2356               width = INTVAL (XEXP (dest, 1));
2357               offset = INTVAL (XEXP (dest, 2));
2358               dest = XEXP (dest, 0);
2359               if (BITS_BIG_ENDIAN)
2360                 offset = GET_MODE_BITSIZE (GET_MODE (dest)) - width - offset;
2361             }
2362         }
2363       else
2364         {
2365           if (GET_CODE (dest) == STRICT_LOW_PART)
2366             dest = XEXP (dest, 0);
2367           width = GET_MODE_BITSIZE (GET_MODE (dest));
2368           offset = 0;
2369         }
2370
2371       if (offset >= 0)
2372         {
2373           /* If this is the low part, we're done.  */
2374           if (subreg_lowpart_p (dest))
2375             ;
2376           /* Handle the case where inner is twice the size of outer.  */
2377           else if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2378                    == 2 * GET_MODE_BITSIZE (GET_MODE (dest)))
2379             offset += GET_MODE_BITSIZE (GET_MODE (dest));
2380           /* Otherwise give up for now.  */
2381           else
2382             offset = -1;
2383         }
2384
2385       if (offset >= 0
2386           && (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2387               <= HOST_BITS_PER_WIDE_INT * 2))
2388         {
2389           HOST_WIDE_INT mhi, ohi, ihi;
2390           HOST_WIDE_INT mlo, olo, ilo;
2391           rtx inner = SET_SRC (PATTERN (i3));
2392           rtx outer = SET_SRC (temp);
2393
2394           if (GET_CODE (outer) == CONST_INT)
2395             {
2396               olo = INTVAL (outer);
2397               ohi = olo < 0 ? -1 : 0;
2398             }
2399           else
2400             {
2401               olo = CONST_DOUBLE_LOW (outer);
2402               ohi = CONST_DOUBLE_HIGH (outer);
2403             }
2404
2405           if (GET_CODE (inner) == CONST_INT)
2406             {
2407               ilo = INTVAL (inner);
2408               ihi = ilo < 0 ? -1 : 0;
2409             }
2410           else
2411             {
2412               ilo = CONST_DOUBLE_LOW (inner);
2413               ihi = CONST_DOUBLE_HIGH (inner);
2414             }
2415
2416           if (width < HOST_BITS_PER_WIDE_INT)
2417             {
2418               mlo = ((unsigned HOST_WIDE_INT) 1 << width) - 1;
2419               mhi = 0;
2420             }
2421           else if (width < HOST_BITS_PER_WIDE_INT * 2)
2422             {
2423               mhi = ((unsigned HOST_WIDE_INT) 1
2424                      << (width - HOST_BITS_PER_WIDE_INT)) - 1;
2425               mlo = -1;
2426             }
2427           else
2428             {
2429               mlo = -1;
2430               mhi = -1;
2431             }
2432
2433           ilo &= mlo;
2434           ihi &= mhi;
2435
2436           if (offset >= HOST_BITS_PER_WIDE_INT)
2437             {
2438               mhi = mlo << (offset - HOST_BITS_PER_WIDE_INT);
2439               mlo = 0;
2440               ihi = ilo << (offset - HOST_BITS_PER_WIDE_INT);
2441               ilo = 0;
2442             }
2443           else if (offset > 0)
2444             {
2445               mhi = (mhi << offset) | ((unsigned HOST_WIDE_INT) mlo
2446                                        >> (HOST_BITS_PER_WIDE_INT - offset));
2447               mlo = mlo << offset;
2448               ihi = (ihi << offset) | ((unsigned HOST_WIDE_INT) ilo
2449                                        >> (HOST_BITS_PER_WIDE_INT - offset));
2450               ilo = ilo << offset;
2451             }
2452
2453           olo = (olo & ~mlo) | ilo;
2454           ohi = (ohi & ~mhi) | ihi;
2455
2456           combine_merges++;
2457           subst_insn = i3;
2458           subst_low_luid = DF_INSN_LUID (i2);
2459           added_sets_2 = added_sets_1 = 0;
2460           i2dest = SET_DEST (temp);
2461           i2dest_killed = dead_or_set_p (i2, i2dest);
2462
2463           SUBST (SET_SRC (temp),
2464                  immed_double_const (olo, ohi, GET_MODE (SET_DEST (temp))));
2465
2466           newpat = PATTERN (i2);
2467           goto validate_replacement;
2468         }
2469     }
2470
2471 #ifndef HAVE_cc0
2472   /* If we have no I1 and I2 looks like:
2473         (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2474                    (set Y OP)])
2475      make up a dummy I1 that is
2476         (set Y OP)
2477      and change I2 to be
2478         (set (reg:CC X) (compare:CC Y (const_int 0)))
2479
2480      (We can ignore any trailing CLOBBERs.)
2481
2482      This undoes a previous combination and allows us to match a branch-and-
2483      decrement insn.  */
2484
2485   if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2486       && XVECLEN (PATTERN (i2), 0) >= 2
2487       && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2488       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2489           == MODE_CC)
2490       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2491       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2492       && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2493       && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2494       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2495                       SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2496     {
2497       for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2498         if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2499           break;
2500
2501       if (i == 1)
2502         {
2503           /* We make I1 with the same INSN_UID as I2.  This gives it
2504              the same DF_INSN_LUID for value tracking.  Our fake I1 will
2505              never appear in the insn stream so giving it the same INSN_UID
2506              as I2 will not cause a problem.  */
2507
2508           i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
2509                              BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
2510                              XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX);
2511
2512           SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2513           SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2514                  SET_DEST (PATTERN (i1)));
2515         }
2516     }
2517 #endif
2518
2519   /* Verify that I2 and I1 are valid for combining.  */
2520   if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
2521       || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
2522     {
2523       undo_all ();
2524       return 0;
2525     }
2526
2527   /* Record whether I2DEST is used in I2SRC and similarly for the other
2528      cases.  Knowing this will help in register status updating below.  */
2529   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2530   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2531   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2532   i2dest_killed = dead_or_set_p (i2, i2dest);
2533   i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2534
2535   /* See if I1 directly feeds into I3.  It does if I1DEST is not used
2536      in I2SRC.  */
2537   i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
2538
2539   /* Ensure that I3's pattern can be the destination of combines.  */
2540   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
2541                           i1 && i2dest_in_i1src && i1_feeds_i3,
2542                           &i3dest_killed))
2543     {
2544       undo_all ();
2545       return 0;
2546     }
2547
2548   /* See if any of the insns is a MULT operation.  Unless one is, we will
2549      reject a combination that is, since it must be slower.  Be conservative
2550      here.  */
2551   if (GET_CODE (i2src) == MULT
2552       || (i1 != 0 && GET_CODE (i1src) == MULT)
2553       || (GET_CODE (PATTERN (i3)) == SET
2554           && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2555     have_mult = 1;
2556
2557   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2558      We used to do this EXCEPT in one case: I3 has a post-inc in an
2559      output operand.  However, that exception can give rise to insns like
2560         mov r3,(r3)+
2561      which is a famous insn on the PDP-11 where the value of r3 used as the
2562      source was model-dependent.  Avoid this sort of thing.  */
2563
2564 #if 0
2565   if (!(GET_CODE (PATTERN (i3)) == SET
2566         && REG_P (SET_SRC (PATTERN (i3)))
2567         && MEM_P (SET_DEST (PATTERN (i3)))
2568         && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2569             || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2570     /* It's not the exception.  */
2571 #endif
2572 #ifdef AUTO_INC_DEC
2573     for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2574       if (REG_NOTE_KIND (link) == REG_INC
2575           && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2576               || (i1 != 0
2577                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2578         {
2579           undo_all ();
2580           return 0;
2581         }
2582 #endif
2583
2584   /* See if the SETs in I1 or I2 need to be kept around in the merged
2585      instruction: whenever the value set there is still needed past I3.
2586      For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
2587
2588      For the SET in I1, we have two cases:  If I1 and I2 independently
2589      feed into I3, the set in I1 needs to be kept around if I1DEST dies
2590      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
2591      in I1 needs to be kept around unless I1DEST dies or is set in either
2592      I2 or I3.  We can distinguish these cases by seeing if I2SRC mentions
2593      I1DEST.  If so, we know I1 feeds into I2.  */
2594
2595   added_sets_2 = ! dead_or_set_p (i3, i2dest);
2596
2597   added_sets_1
2598     = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
2599                : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
2600
2601   /* If the set in I2 needs to be kept around, we must make a copy of
2602      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2603      PATTERN (I2), we are only substituting for the original I1DEST, not into
2604      an already-substituted copy.  This also prevents making self-referential
2605      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2606      I2DEST.  */
2607
2608   if (added_sets_2)
2609     {
2610       if (GET_CODE (PATTERN (i2)) == PARALLEL)
2611         i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
2612       else
2613         i2pat = copy_rtx (PATTERN (i2));
2614     }
2615
2616   if (added_sets_1)
2617     {
2618       if (GET_CODE (PATTERN (i1)) == PARALLEL)
2619         i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
2620       else
2621         i1pat = copy_rtx (PATTERN (i1));
2622     }
2623
2624   combine_merges++;
2625
2626   /* Substitute in the latest insn for the regs set by the earlier ones.  */
2627
2628   maxreg = max_reg_num ();
2629
2630   subst_insn = i3;
2631
2632 #ifndef HAVE_cc0
2633   /* Many machines that don't use CC0 have insns that can both perform an
2634      arithmetic operation and set the condition code.  These operations will
2635      be represented as a PARALLEL with the first element of the vector
2636      being a COMPARE of an arithmetic operation with the constant zero.
2637      The second element of the vector will set some pseudo to the result
2638      of the same arithmetic operation.  If we simplify the COMPARE, we won't
2639      match such a pattern and so will generate an extra insn.   Here we test
2640      for this case, where both the comparison and the operation result are
2641      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2642      I2SRC.  Later we will make the PARALLEL that contains I2.  */
2643
2644   if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2645       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2646       && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
2647       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2648     {
2649 #ifdef SELECT_CC_MODE
2650       rtx *cc_use;
2651       enum machine_mode compare_mode;
2652 #endif
2653
2654       newpat = PATTERN (i3);
2655       SUBST (XEXP (SET_SRC (newpat), 0), i2src);
2656
2657       i2_is_used = 1;
2658
2659 #ifdef SELECT_CC_MODE
2660       /* See if a COMPARE with the operand we substituted in should be done
2661          with the mode that is currently being used.  If not, do the same
2662          processing we do in `subst' for a SET; namely, if the destination
2663          is used only once, try to replace it with a register of the proper
2664          mode and also replace the COMPARE.  */
2665       if (undobuf.other_insn == 0
2666           && (cc_use = find_single_use (SET_DEST (newpat), i3,
2667                                         &undobuf.other_insn))
2668           && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
2669                                               i2src, const0_rtx))
2670               != GET_MODE (SET_DEST (newpat))))
2671         {
2672           if (can_change_dest_mode(SET_DEST (newpat), added_sets_2,
2673                                    compare_mode))
2674             {
2675               unsigned int regno = REGNO (SET_DEST (newpat));
2676               rtx new_dest;
2677
2678               if (regno < FIRST_PSEUDO_REGISTER)
2679                 new_dest = gen_rtx_REG (compare_mode, regno);
2680               else
2681                 {
2682                   SUBST_MODE (regno_reg_rtx[regno], compare_mode);
2683                   new_dest = regno_reg_rtx[regno];
2684                 }
2685
2686               SUBST (SET_DEST (newpat), new_dest);
2687               SUBST (XEXP (*cc_use, 0), new_dest);
2688               SUBST (SET_SRC (newpat),
2689                      gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
2690             }
2691           else
2692             undobuf.other_insn = 0;
2693         }
2694 #endif
2695     }
2696   else
2697 #endif
2698     {
2699       /* It is possible that the source of I2 or I1 may be performing
2700          an unneeded operation, such as a ZERO_EXTEND of something
2701          that is known to have the high part zero.  Handle that case
2702          by letting subst look at the innermost one of them.
2703
2704          Another way to do this would be to have a function that tries
2705          to simplify a single insn instead of merging two or more
2706          insns.  We don't do this because of the potential of infinite
2707          loops and because of the potential extra memory required.
2708          However, doing it the way we are is a bit of a kludge and
2709          doesn't catch all cases.
2710
2711          But only do this if -fexpensive-optimizations since it slows
2712          things down and doesn't usually win.
2713
2714          This is not done in the COMPARE case above because the
2715          unmodified I2PAT is used in the PARALLEL and so a pattern
2716          with a modified I2SRC would not match.  */
2717
2718       if (flag_expensive_optimizations)
2719         {
2720           /* Pass pc_rtx so no substitutions are done, just
2721              simplifications.  */
2722           if (i1)
2723             {
2724               subst_low_luid = DF_INSN_LUID (i1);
2725               i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
2726             }
2727           else
2728             {
2729               subst_low_luid = DF_INSN_LUID (i2);
2730               i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
2731             }
2732         }
2733
2734       n_occurrences = 0;                /* `subst' counts here */
2735
2736       /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
2737          need to make a unique copy of I2SRC each time we substitute it
2738          to avoid self-referential rtl.  */
2739
2740       subst_low_luid = DF_INSN_LUID (i2);
2741       newpat = subst (PATTERN (i3), i2dest, i2src, 0,
2742                       ! i1_feeds_i3 && i1dest_in_i1src);
2743       substed_i2 = 1;
2744
2745       /* Record whether i2's body now appears within i3's body.  */
2746       i2_is_used = n_occurrences;
2747     }
2748
2749   /* If we already got a failure, don't try to do more.  Otherwise,
2750      try to substitute in I1 if we have it.  */
2751
2752   if (i1 && GET_CODE (newpat) != CLOBBER)
2753     {
2754       /* Check that an autoincrement side-effect on I1 has not been lost.
2755          This happens if I1DEST is mentioned in I2 and dies there, and
2756          has disappeared from the new pattern.  */
2757       if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2758            && !i1_feeds_i3
2759            && dead_or_set_p (i2, i1dest)
2760            && !reg_overlap_mentioned_p (i1dest, newpat))
2761           /* Before we can do this substitution, we must redo the test done
2762              above (see detailed comments there) that ensures  that I1DEST
2763              isn't mentioned in any SETs in NEWPAT that are field assignments.  */
2764           || !combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX, 0, 0))
2765         {
2766           undo_all ();
2767           return 0;
2768         }
2769
2770       n_occurrences = 0;
2771       subst_low_luid = DF_INSN_LUID (i1);
2772       newpat = subst (newpat, i1dest, i1src, 0, 0);
2773       substed_i1 = 1;
2774     }
2775
2776   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
2777      to count all the ways that I2SRC and I1SRC can be used.  */
2778   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
2779        && i2_is_used + added_sets_2 > 1)
2780       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2781           && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
2782               > 1))
2783       /* Fail if we tried to make a new register.  */
2784       || max_reg_num () != maxreg
2785       /* Fail if we couldn't do something and have a CLOBBER.  */
2786       || GET_CODE (newpat) == CLOBBER
2787       /* Fail if this new pattern is a MULT and we didn't have one before
2788          at the outer level.  */
2789       || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
2790           && ! have_mult))
2791     {
2792       undo_all ();
2793       return 0;
2794     }
2795
2796   /* If the actions of the earlier insns must be kept
2797      in addition to substituting them into the latest one,
2798      we must make a new PARALLEL for the latest insn
2799      to hold additional the SETs.  */
2800
2801   if (added_sets_1 || added_sets_2)
2802     {
2803       combine_extras++;
2804
2805       if (GET_CODE (newpat) == PARALLEL)
2806         {
2807           rtvec old = XVEC (newpat, 0);
2808           total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
2809           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2810           memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
2811                   sizeof (old->elem[0]) * old->num_elem);
2812         }
2813       else
2814         {
2815           rtx old = newpat;
2816           total_sets = 1 + added_sets_1 + added_sets_2;
2817           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2818           XVECEXP (newpat, 0, 0) = old;
2819         }
2820
2821       if (added_sets_1)
2822         XVECEXP (newpat, 0, --total_sets) = i1pat;
2823
2824       if (added_sets_2)
2825         {
2826           /* If there is no I1, use I2's body as is.  We used to also not do
2827              the subst call below if I2 was substituted into I3,
2828              but that could lose a simplification.  */
2829           if (i1 == 0)
2830             XVECEXP (newpat, 0, --total_sets) = i2pat;
2831           else
2832             /* See comment where i2pat is assigned.  */
2833             XVECEXP (newpat, 0, --total_sets)
2834               = subst (i2pat, i1dest, i1src, 0, 0);
2835         }
2836     }
2837
2838   /* We come here when we are replacing a destination in I2 with the
2839      destination of I3.  */
2840  validate_replacement:
2841
2842   /* Note which hard regs this insn has as inputs.  */
2843   mark_used_regs_combine (newpat);
2844
2845   /* If recog_for_combine fails, it strips existing clobbers.  If we'll
2846      consider splitting this pattern, we might need these clobbers.  */
2847   if (i1 && GET_CODE (newpat) == PARALLEL
2848       && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
2849     {
2850       int len = XVECLEN (newpat, 0);
2851
2852       newpat_vec_with_clobbers = rtvec_alloc (len);
2853       for (i = 0; i < len; i++)
2854         RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
2855     }
2856
2857   /* Is the result of combination a valid instruction?  */
2858   insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2859
2860   /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2861      the second SET's destination is a register that is unused and isn't
2862      marked as an instruction that might trap in an EH region.  In that case,
2863      we just need the first SET.   This can occur when simplifying a divmod
2864      insn.  We *must* test for this case here because the code below that
2865      splits two independent SETs doesn't handle this case correctly when it
2866      updates the register status.
2867
2868      It's pointless doing this if we originally had two sets, one from
2869      i3, and one from i2.  Combining then splitting the parallel results
2870      in the original i2 again plus an invalid insn (which we delete).
2871      The net effect is only to move instructions around, which makes
2872      debug info less accurate.
2873
2874      Also check the case where the first SET's destination is unused.
2875      That would not cause incorrect code, but does cause an unneeded
2876      insn to remain.  */
2877
2878   if (insn_code_number < 0
2879       && !(added_sets_2 && i1 == 0)
2880       && GET_CODE (newpat) == PARALLEL
2881       && XVECLEN (newpat, 0) == 2
2882       && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2883       && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2884       && asm_noperands (newpat) < 0)
2885     {
2886       rtx set0 = XVECEXP (newpat, 0, 0);
2887       rtx set1 = XVECEXP (newpat, 0, 1);
2888       rtx note;
2889
2890       if (((REG_P (SET_DEST (set1))
2891             && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
2892            || (GET_CODE (SET_DEST (set1)) == SUBREG
2893                && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
2894           && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2895               || INTVAL (XEXP (note, 0)) <= 0)
2896           && ! side_effects_p (SET_SRC (set1)))
2897         {
2898           newpat = set0;
2899           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2900         }
2901
2902       else if (((REG_P (SET_DEST (set0))
2903                  && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
2904                 || (GET_CODE (SET_DEST (set0)) == SUBREG
2905                     && find_reg_note (i3, REG_UNUSED,
2906                                       SUBREG_REG (SET_DEST (set0)))))
2907                && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2908                    || INTVAL (XEXP (note, 0)) <= 0)
2909                && ! side_effects_p (SET_SRC (set0)))
2910         {
2911           newpat = set1;
2912           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2913
2914           if (insn_code_number >= 0)
2915             {
2916               /* If we will be able to accept this, we have made a
2917                  change to the destination of I3.  This requires us to
2918                  do a few adjustments.  */
2919
2920               PATTERN (i3) = newpat;
2921               adjust_for_new_dest (i3);
2922             }
2923         }
2924     }
2925
2926   /* If we were combining three insns and the result is a simple SET
2927      with no ASM_OPERANDS that wasn't recognized, try to split it into two
2928      insns.  There are two ways to do this.  It can be split using a
2929      machine-specific method (like when you have an addition of a large
2930      constant) or by combine in the function find_split_point.  */
2931
2932   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2933       && asm_noperands (newpat) < 0)
2934     {
2935       rtx parallel, m_split, *split;
2936
2937       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
2938          use I2DEST as a scratch register will help.  In the latter case,
2939          convert I2DEST to the mode of the source of NEWPAT if we can.  */
2940
2941       m_split = combine_split_insns (newpat, i3);
2942
2943       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2944          inputs of NEWPAT.  */
2945
2946       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2947          possible to try that as a scratch reg.  This would require adding
2948          more code to make it work though.  */
2949
2950       if (m_split == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
2951         {
2952           enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
2953
2954           /* First try to split using the original register as a
2955              scratch register.  */
2956           parallel = gen_rtx_PARALLEL (VOIDmode,
2957                                        gen_rtvec (2, newpat,
2958                                                   gen_rtx_CLOBBER (VOIDmode,
2959                                                                    i2dest)));
2960           m_split = combine_split_insns (parallel, i3);
2961
2962           /* If that didn't work, try changing the mode of I2DEST if
2963              we can.  */
2964           if (m_split == 0
2965               && new_mode != GET_MODE (i2dest)
2966               && new_mode != VOIDmode
2967               && can_change_dest_mode (i2dest, added_sets_2, new_mode))
2968             {
2969               enum machine_mode old_mode = GET_MODE (i2dest);
2970               rtx ni2dest;
2971
2972               if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
2973                 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
2974               else
2975                 {
2976                   SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
2977                   ni2dest = regno_reg_rtx[REGNO (i2dest)];
2978                 }
2979
2980               parallel = (gen_rtx_PARALLEL
2981                           (VOIDmode,
2982                            gen_rtvec (2, newpat,
2983                                       gen_rtx_CLOBBER (VOIDmode,
2984                                                        ni2dest))));
2985               m_split = combine_split_insns (parallel, i3);
2986
2987               if (m_split == 0
2988                   && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2989                 {
2990                   struct undo *buf;
2991
2992                   adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
2993                   buf = undobuf.undos;
2994                   undobuf.undos = buf->next;
2995                   buf->next = undobuf.frees;
2996                   undobuf.frees = buf;
2997                 }
2998             }
2999         }
3000
3001       /* If recog_for_combine has discarded clobbers, try to use them
3002          again for the split.  */
3003       if (m_split == 0 && newpat_vec_with_clobbers)
3004         {
3005           parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3006           m_split = combine_split_insns (parallel, i3);
3007         }
3008
3009       if (m_split && NEXT_INSN (m_split) == NULL_RTX)
3010         {
3011           m_split = PATTERN (m_split);
3012           insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
3013           if (insn_code_number >= 0)
3014             newpat = m_split;
3015         }
3016       else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
3017                && (next_real_insn (i2) == i3
3018                    || ! use_crosses_set_p (PATTERN (m_split), DF_INSN_LUID (i2))))
3019         {
3020           rtx i2set, i3set;
3021           rtx newi3pat = PATTERN (NEXT_INSN (m_split));
3022           newi2pat = PATTERN (m_split);
3023
3024           i3set = single_set (NEXT_INSN (m_split));
3025           i2set = single_set (m_split);
3026
3027           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3028
3029           /* If I2 or I3 has multiple SETs, we won't know how to track
3030              register status, so don't use these insns.  If I2's destination
3031              is used between I2 and I3, we also can't use these insns.  */
3032
3033           if (i2_code_number >= 0 && i2set && i3set
3034               && (next_real_insn (i2) == i3
3035                   || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3036             insn_code_number = recog_for_combine (&newi3pat, i3,
3037                                                   &new_i3_notes);
3038           if (insn_code_number >= 0)
3039             newpat = newi3pat;
3040
3041           /* It is possible that both insns now set the destination of I3.
3042              If so, we must show an extra use of it.  */
3043
3044           if (insn_code_number >= 0)
3045             {
3046               rtx new_i3_dest = SET_DEST (i3set);
3047               rtx new_i2_dest = SET_DEST (i2set);
3048
3049               while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3050                      || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3051                      || GET_CODE (new_i3_dest) == SUBREG)
3052                 new_i3_dest = XEXP (new_i3_dest, 0);
3053
3054               while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3055                      || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3056                      || GET_CODE (new_i2_dest) == SUBREG)
3057                 new_i2_dest = XEXP (new_i2_dest, 0);
3058
3059               if (REG_P (new_i3_dest)
3060                   && REG_P (new_i2_dest)
3061                   && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3062                 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3063             }
3064         }
3065
3066       /* If we can split it and use I2DEST, go ahead and see if that
3067          helps things be recognized.  Verify that none of the registers
3068          are set between I2 and I3.  */
3069       if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
3070 #ifdef HAVE_cc0
3071           && REG_P (i2dest)
3072 #endif
3073           /* We need I2DEST in the proper mode.  If it is a hard register
3074              or the only use of a pseudo, we can change its mode.
3075              Make sure we don't change a hard register to have a mode that
3076              isn't valid for it, or change the number of registers.  */
3077           && (GET_MODE (*split) == GET_MODE (i2dest)
3078               || GET_MODE (*split) == VOIDmode
3079               || can_change_dest_mode (i2dest, added_sets_2,
3080                                        GET_MODE (*split)))
3081           && (next_real_insn (i2) == i3
3082               || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3083           /* We can't overwrite I2DEST if its value is still used by
3084              NEWPAT.  */
3085           && ! reg_referenced_p (i2dest, newpat))
3086         {
3087           rtx newdest = i2dest;
3088           enum rtx_code split_code = GET_CODE (*split);
3089           enum machine_mode split_mode = GET_MODE (*split);
3090           bool subst_done = false;
3091           newi2pat = NULL_RTX;
3092
3093           /* Get NEWDEST as a register in the proper mode.  We have already
3094              validated that we can do this.  */
3095           if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3096             {
3097               if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3098                 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3099               else
3100                 {
3101                   SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3102                   newdest = regno_reg_rtx[REGNO (i2dest)];
3103                 }
3104             }
3105
3106           /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3107              an ASHIFT.  This can occur if it was inside a PLUS and hence
3108              appeared to be a memory address.  This is a kludge.  */
3109           if (split_code == MULT
3110               && GET_CODE (XEXP (*split, 1)) == CONST_INT
3111               && INTVAL (XEXP (*split, 1)) > 0
3112               && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
3113             {
3114               SUBST (*split, gen_rtx_ASHIFT (split_mode,
3115                                              XEXP (*split, 0), GEN_INT (i)));
3116               /* Update split_code because we may not have a multiply
3117                  anymore.  */
3118               split_code = GET_CODE (*split);
3119             }
3120
3121 #ifdef INSN_SCHEDULING
3122           /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3123              be written as a ZERO_EXTEND.  */
3124           if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3125             {
3126 #ifdef LOAD_EXTEND_OP
3127               /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3128                  what it really is.  */
3129               if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3130                   == SIGN_EXTEND)
3131                 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3132                                                     SUBREG_REG (*split)));
3133               else
3134 #endif
3135                 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3136                                                     SUBREG_REG (*split)));
3137             }
3138 #endif
3139
3140           /* Attempt to split binary operators using arithmetic identities.  */
3141           if (BINARY_P (SET_SRC (newpat))
3142               && split_mode == GET_MODE (SET_SRC (newpat))
3143               && ! side_effects_p (SET_SRC (newpat)))
3144             {
3145               rtx setsrc = SET_SRC (newpat);
3146               enum machine_mode mode = GET_MODE (setsrc);
3147               enum rtx_code code = GET_CODE (setsrc);
3148               rtx src_op0 = XEXP (setsrc, 0);
3149               rtx src_op1 = XEXP (setsrc, 1);
3150
3151               /* Split "X = Y op Y" as "Z = Y; X = Z op Z".  */
3152               if (rtx_equal_p (src_op0, src_op1))
3153                 {
3154                   newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3155                   SUBST (XEXP (setsrc, 0), newdest);
3156                   SUBST (XEXP (setsrc, 1), newdest);
3157                   subst_done = true;
3158                 }
3159               /* Split "((P op Q) op R) op S" where op is PLUS or MULT.  */
3160               else if ((code == PLUS || code == MULT)
3161                        && GET_CODE (src_op0) == code
3162                        && GET_CODE (XEXP (src_op0, 0)) == code
3163                        && (INTEGRAL_MODE_P (mode)
3164                            || (FLOAT_MODE_P (mode)
3165                                && flag_unsafe_math_optimizations)))
3166                 {
3167                   rtx p = XEXP (XEXP (src_op0, 0), 0);
3168                   rtx q = XEXP (XEXP (src_op0, 0), 1);
3169                   rtx r = XEXP (src_op0, 1);
3170                   rtx s = src_op1;
3171
3172                   /* Split both "((X op Y) op X) op Y" and
3173                      "((X op Y) op Y) op X" as "T op T" where T is
3174                      "X op Y".  */
3175                   if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3176                        || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3177                     {
3178                       newi2pat = gen_rtx_SET (VOIDmode, newdest,
3179                                               XEXP (src_op0, 0));
3180                       SUBST (XEXP (setsrc, 0), newdest);
3181                       SUBST (XEXP (setsrc, 1), newdest);
3182                       subst_done = true;
3183                     }
3184                   /* Split "((X op X) op Y) op Y)" as "T op T" where
3185                      T is "X op Y".  */
3186                   else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3187                     {
3188                       rtx tmp = simplify_gen_binary (code, mode, p, r);
3189                       newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3190                       SUBST (XEXP (setsrc, 0), newdest);
3191                       SUBST (XEXP (setsrc, 1), newdest);
3192                       subst_done = true;
3193                     }
3194                 }
3195             }
3196
3197           if (!subst_done)
3198             {
3199               newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3200               SUBST (*split, newdest);
3201             }
3202
3203           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3204
3205           /* recog_for_combine might have added CLOBBERs to newi2pat.
3206              Make sure NEWPAT does not depend on the clobbered regs.  */
3207           if (GET_CODE (newi2pat) == PARALLEL)
3208             for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3209               if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3210                 {
3211                   rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3212                   if (reg_overlap_mentioned_p (reg, newpat))
3213                     {
3214                       undo_all ();
3215                       return 0;
3216                     }
3217                 }
3218
3219           /* If the split point was a MULT and we didn't have one before,
3220              don't use one now.  */
3221           if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3222             insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3223         }
3224     }
3225
3226   /* Check for a case where we loaded from memory in a narrow mode and
3227      then sign extended it, but we need both registers.  In that case,
3228      we have a PARALLEL with both loads from the same memory location.
3229      We can split this into a load from memory followed by a register-register
3230      copy.  This saves at least one insn, more if register allocation can
3231      eliminate the copy.
3232
3233      We cannot do this if the destination of the first assignment is a
3234      condition code register or cc0.  We eliminate this case by making sure
3235      the SET_DEST and SET_SRC have the same mode.
3236
3237      We cannot do this if the destination of the second assignment is
3238      a register that we have already assumed is zero-extended.  Similarly
3239      for a SUBREG of such a register.  */
3240
3241   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3242            && GET_CODE (newpat) == PARALLEL
3243            && XVECLEN (newpat, 0) == 2
3244            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3245            && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3246            && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3247                == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3248            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3249            && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3250                            XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3251            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3252                                    DF_INSN_LUID (i2))
3253            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3254            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3255            && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
3256                  (REG_P (temp)
3257                   && VEC_index (reg_stat_type, reg_stat,
3258                                 REGNO (temp))->nonzero_bits != 0
3259                   && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3260                   && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3261                   && (VEC_index (reg_stat_type, reg_stat,
3262                                  REGNO (temp))->nonzero_bits
3263                       != GET_MODE_MASK (word_mode))))
3264            && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3265                  && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3266                      (REG_P (temp)
3267                       && VEC_index (reg_stat_type, reg_stat,
3268                                     REGNO (temp))->nonzero_bits != 0
3269                       && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3270                       && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3271                       && (VEC_index (reg_stat_type, reg_stat,
3272                                      REGNO (temp))->nonzero_bits
3273                           != GET_MODE_MASK (word_mode)))))
3274            && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3275                                          SET_SRC (XVECEXP (newpat, 0, 1)))
3276            && ! find_reg_note (i3, REG_UNUSED,
3277                                SET_DEST (XVECEXP (newpat, 0, 0))))
3278     {
3279       rtx ni2dest;
3280
3281       newi2pat = XVECEXP (newpat, 0, 0);
3282       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3283       newpat = XVECEXP (newpat, 0, 1);
3284       SUBST (SET_SRC (newpat),
3285              gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3286       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3287
3288       if (i2_code_number >= 0)
3289         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3290
3291       if (insn_code_number >= 0)
3292         swap_i2i3 = 1;
3293     }
3294
3295   /* Similarly, check for a case where we have a PARALLEL of two independent
3296      SETs but we started with three insns.  In this case, we can do the sets
3297      as two separate insns.  This case occurs when some SET allows two
3298      other insns to combine, but the destination of that SET is still live.  */
3299
3300   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3301            && GET_CODE (newpat) == PARALLEL
3302            && XVECLEN (newpat, 0) == 2
3303            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3304            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3305            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3306            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3307            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3308            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3309            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3310                                    DF_INSN_LUID (i2))
3311            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3312                                   XVECEXP (newpat, 0, 0))
3313            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3314                                   XVECEXP (newpat, 0, 1))
3315            && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3316                  && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1))))
3317 #ifdef HAVE_cc0
3318            /* We cannot split the parallel into two sets if both sets
3319               reference cc0.  */
3320            && ! (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0))
3321                  && reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 1)))
3322 #endif
3323            )
3324     {
3325       /* Normally, it doesn't matter which of the two is done first,
3326          but it does if one references cc0.  In that case, it has to
3327          be first.  */
3328 #ifdef HAVE_cc0
3329       if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
3330         {
3331           newi2pat = XVECEXP (newpat, 0, 0);
3332           newpat = XVECEXP (newpat, 0, 1);
3333         }
3334       else
3335 #endif
3336         {
3337           newi2pat = XVECEXP (newpat, 0, 1);
3338           newpat = XVECEXP (newpat, 0, 0);
3339         }
3340
3341       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3342
3343       if (i2_code_number >= 0)
3344         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3345     }
3346
3347   /* If it still isn't recognized, fail and change things back the way they
3348      were.  */
3349   if ((insn_code_number < 0
3350        /* Is the result a reasonable ASM_OPERANDS?  */
3351        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3352     {
3353       undo_all ();
3354       return 0;
3355     }
3356
3357   /* If we had to change another insn, make sure it is valid also.  */
3358   if (undobuf.other_insn)
3359     {
3360       CLEAR_HARD_REG_SET (newpat_used_regs);
3361
3362       other_pat = PATTERN (undobuf.other_insn);
3363       other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3364                                              &new_other_notes);
3365
3366       if (other_code_number < 0 && ! check_asm_operands (other_pat))
3367         {
3368           undo_all ();
3369           return 0;
3370         }
3371     }
3372
3373 #ifdef HAVE_cc0
3374   /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3375      they are adjacent to each other or not.  */
3376   {
3377     rtx p = prev_nonnote_insn (i3);
3378     if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3379         && sets_cc0_p (newi2pat))
3380       {
3381         undo_all ();
3382         return 0;
3383       }
3384   }
3385 #endif
3386
3387   /* Only allow this combination if insn_rtx_costs reports that the
3388      replacement instructions are cheaper than the originals.  */
3389   if (!combine_validate_cost (i1, i2, i3, newpat, newi2pat, other_pat))
3390     {
3391       undo_all ();
3392       return 0;
3393     }
3394
3395   /* We now know that we can do this combination.  Merge the insns and
3396      update the status of registers and LOG_LINKS.  */
3397
3398   if (undobuf.other_insn)
3399     {
3400       rtx note, next;
3401
3402       PATTERN (undobuf.other_insn) = other_pat;
3403
3404       /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
3405          are still valid.  Then add any non-duplicate notes added by
3406          recog_for_combine.  */
3407       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
3408         {
3409           next = XEXP (note, 1);
3410
3411           if (REG_NOTE_KIND (note) == REG_UNUSED
3412               && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
3413             remove_note (undobuf.other_insn, note);
3414         }
3415
3416       distribute_notes (new_other_notes, undobuf.other_insn,
3417                         undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
3418     }
3419
3420   if (swap_i2i3)
3421     {
3422       rtx insn;
3423       rtx link;
3424       rtx ni2dest;
3425
3426       /* I3 now uses what used to be its destination and which is now
3427          I2's destination.  This requires us to do a few adjustments.  */
3428       PATTERN (i3) = newpat;
3429       adjust_for_new_dest (i3);
3430
3431       /* We need a LOG_LINK from I3 to I2.  But we used to have one,
3432          so we still will.
3433
3434          However, some later insn might be using I2's dest and have
3435          a LOG_LINK pointing at I3.  We must remove this link.
3436          The simplest way to remove the link is to point it at I1,
3437          which we know will be a NOTE.  */
3438
3439       /* newi2pat is usually a SET here; however, recog_for_combine might
3440          have added some clobbers.  */
3441       if (GET_CODE (newi2pat) == PARALLEL)
3442         ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
3443       else
3444         ni2dest = SET_DEST (newi2pat);
3445
3446       for (insn = NEXT_INSN (i3);
3447            insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3448                     || insn != BB_HEAD (this_basic_block->next_bb));
3449            insn = NEXT_INSN (insn))
3450         {
3451           if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
3452             {
3453               for (link = LOG_LINKS (insn); link;
3454                    link = XEXP (link, 1))
3455                 if (XEXP (link, 0) == i3)
3456                   XEXP (link, 0) = i1;
3457
3458               break;
3459             }
3460         }
3461     }
3462
3463   {
3464     rtx i3notes, i2notes, i1notes = 0;
3465     rtx i3links, i2links, i1links = 0;
3466     rtx midnotes = 0;
3467     unsigned int regno;
3468     /* Compute which registers we expect to eliminate.  newi2pat may be setting
3469        either i3dest or i2dest, so we must check it.  Also, i1dest may be the
3470        same as i3dest, in which case newi2pat may be setting i1dest.  */
3471     rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
3472                    || i2dest_in_i2src || i2dest_in_i1src
3473                    || !i2dest_killed
3474                    ? 0 : i2dest);
3475     rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
3476                    || (newi2pat && reg_set_p (i1dest, newi2pat))
3477                    || !i1dest_killed
3478                    ? 0 : i1dest);
3479
3480     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
3481        clear them.  */
3482     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
3483     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
3484     if (i1)
3485       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
3486
3487     /* Ensure that we do not have something that should not be shared but
3488        occurs multiple times in the new insns.  Check this by first
3489        resetting all the `used' flags and then copying anything is shared.  */
3490
3491     reset_used_flags (i3notes);
3492     reset_used_flags (i2notes);
3493     reset_used_flags (i1notes);
3494     reset_used_flags (newpat);
3495     reset_used_flags (newi2pat);
3496     if (undobuf.other_insn)
3497       reset_used_flags (PATTERN (undobuf.other_insn));
3498
3499     i3notes = copy_rtx_if_shared (i3notes);
3500     i2notes = copy_rtx_if_shared (i2notes);
3501     i1notes = copy_rtx_if_shared (i1notes);
3502     newpat = copy_rtx_if_shared (newpat);
3503     newi2pat = copy_rtx_if_shared (newi2pat);
3504     if (undobuf.other_insn)
3505       reset_used_flags (PATTERN (undobuf.other_insn));
3506
3507     INSN_CODE (i3) = insn_code_number;
3508     PATTERN (i3) = newpat;
3509
3510     if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
3511       {
3512         rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
3513
3514         reset_used_flags (call_usage);
3515         call_usage = copy_rtx (call_usage);
3516
3517         if (substed_i2)
3518           replace_rtx (call_usage, i2dest, i2src);
3519
3520         if (substed_i1)
3521           replace_rtx (call_usage, i1dest, i1src);
3522
3523         CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
3524       }
3525
3526     if (undobuf.other_insn)
3527       INSN_CODE (undobuf.other_insn) = other_code_number;
3528
3529     /* We had one special case above where I2 had more than one set and
3530        we replaced a destination of one of those sets with the destination
3531        of I3.  In that case, we have to update LOG_LINKS of insns later
3532        in this basic block.  Note that this (expensive) case is rare.
3533
3534        Also, in this case, we must pretend that all REG_NOTEs for I2
3535        actually came from I3, so that REG_UNUSED notes from I2 will be
3536        properly handled.  */
3537
3538     if (i3_subst_into_i2)
3539       {
3540         for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
3541           if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
3542                || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
3543               && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
3544               && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
3545               && ! find_reg_note (i2, REG_UNUSED,
3546                                   SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
3547             for (temp = NEXT_INSN (i2);
3548                  temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3549                           || BB_HEAD (this_basic_block) != temp);
3550                  temp = NEXT_INSN (temp))
3551               if (temp != i3 && INSN_P (temp))
3552                 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
3553                   if (XEXP (link, 0) == i2)
3554                     XEXP (link, 0) = i3;
3555
3556         if (i3notes)
3557           {
3558             rtx link = i3notes;
3559             while (XEXP (link, 1))
3560               link = XEXP (link, 1);
3561             XEXP (link, 1) = i2notes;
3562           }
3563         else
3564           i3notes = i2notes;
3565         i2notes = 0;
3566       }
3567
3568     LOG_LINKS (i3) = 0;
3569     REG_NOTES (i3) = 0;
3570     LOG_LINKS (i2) = 0;
3571     REG_NOTES (i2) = 0;
3572
3573     if (newi2pat)
3574       {
3575         INSN_CODE (i2) = i2_code_number;
3576         PATTERN (i2) = newi2pat;
3577       }
3578     else
3579       SET_INSN_DELETED (i2);
3580
3581     if (i1)
3582       {
3583         LOG_LINKS (i1) = 0;
3584         REG_NOTES (i1) = 0;
3585         SET_INSN_DELETED (i1);
3586       }
3587
3588     /* Get death notes for everything that is now used in either I3 or
3589        I2 and used to die in a previous insn.  If we built two new
3590        patterns, move from I1 to I2 then I2 to I3 so that we get the
3591        proper movement on registers that I2 modifies.  */
3592
3593     if (newi2pat)
3594       {
3595         move_deaths (newi2pat, NULL_RTX, DF_INSN_LUID (i1), i2, &midnotes);
3596         move_deaths (newpat, newi2pat, DF_INSN_LUID (i1), i3, &midnotes);
3597       }
3598     else
3599       move_deaths (newpat, NULL_RTX, i1 ? DF_INSN_LUID (i1) : DF_INSN_LUID (i2),
3600                    i3, &midnotes);
3601
3602     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
3603     if (i3notes)
3604       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
3605                         elim_i2, elim_i1);
3606     if (i2notes)
3607       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
3608                         elim_i2, elim_i1);
3609     if (i1notes)
3610       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
3611                         elim_i2, elim_i1);
3612     if (midnotes)
3613       distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3614                         elim_i2, elim_i1);
3615
3616     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
3617        know these are REG_UNUSED and want them to go to the desired insn,
3618        so we always pass it as i3.  */
3619
3620     if (newi2pat && new_i2_notes)
3621       distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3622     
3623     if (new_i3_notes)
3624       distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
3625
3626     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
3627        put a REG_DEAD note for it somewhere.  If NEWI2PAT exists and sets
3628        I3DEST, the death must be somewhere before I2, not I3.  If we passed I3
3629        in that case, it might delete I2.  Similarly for I2 and I1.
3630        Show an additional death due to the REG_DEAD note we make here.  If
3631        we discard it in distribute_notes, we will decrement it again.  */
3632
3633     if (i3dest_killed)
3634       {
3635         if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
3636           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
3637                                                NULL_RTX),
3638                             NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
3639         else
3640           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
3641                                                NULL_RTX),
3642                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3643                             elim_i2, elim_i1);
3644       }
3645
3646     if (i2dest_in_i2src)
3647       {
3648         if (newi2pat && reg_set_p (i2dest, newi2pat))
3649           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
3650                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3651         else
3652           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
3653                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3654                             NULL_RTX, NULL_RTX);
3655       }
3656
3657     if (i1dest_in_i1src)
3658       {
3659         if (newi2pat && reg_set_p (i1dest, newi2pat))
3660           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
3661                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3662         else
3663           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
3664                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3665                             NULL_RTX, NULL_RTX);
3666       }
3667
3668     distribute_links (i3links);
3669     distribute_links (i2links);
3670     distribute_links (i1links);
3671
3672     if (REG_P (i2dest))
3673       {
3674         rtx link;
3675         rtx i2_insn = 0, i2_val = 0, set;
3676
3677         /* The insn that used to set this register doesn't exist, and
3678            this life of the register may not exist either.  See if one of
3679            I3's links points to an insn that sets I2DEST.  If it does,
3680            that is now the last known value for I2DEST. If we don't update
3681            this and I2 set the register to a value that depended on its old
3682            contents, we will get confused.  If this insn is used, thing
3683            will be set correctly in combine_instructions.  */
3684
3685         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3686           if ((set = single_set (XEXP (link, 0))) != 0
3687               && rtx_equal_p (i2dest, SET_DEST (set)))
3688             i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
3689
3690         record_value_for_reg (i2dest, i2_insn, i2_val);
3691
3692         /* If the reg formerly set in I2 died only once and that was in I3,
3693            zero its use count so it won't make `reload' do any work.  */
3694         if (! added_sets_2
3695             && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
3696             && ! i2dest_in_i2src)
3697           {
3698             regno = REGNO (i2dest);
3699             INC_REG_N_SETS (regno, -1);
3700           }
3701       }
3702
3703     if (i1 && REG_P (i1dest))
3704       {
3705         rtx link;
3706         rtx i1_insn = 0, i1_val = 0, set;
3707
3708         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3709           if ((set = single_set (XEXP (link, 0))) != 0
3710               && rtx_equal_p (i1dest, SET_DEST (set)))
3711             i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
3712
3713         record_value_for_reg (i1dest, i1_insn, i1_val);
3714
3715         regno = REGNO (i1dest);
3716         if (! added_sets_1 && ! i1dest_in_i1src)
3717           INC_REG_N_SETS (regno, -1);
3718       }
3719
3720     /* Update reg_stat[].nonzero_bits et al for any changes that may have
3721        been made to this insn.  The order of
3722        set_nonzero_bits_and_sign_copies() is important.  Because newi2pat
3723        can affect nonzero_bits of newpat */
3724     if (newi2pat)
3725       note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
3726     note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
3727
3728     /* Set new_direct_jump_p if a new return or simple jump instruction
3729        has been created.
3730
3731        If I3 is now an unconditional jump, ensure that it has a
3732        BARRIER following it since it may have initially been a
3733        conditional jump.  It may also be the last nonnote insn.  */
3734
3735     if (returnjump_p (i3) || any_uncondjump_p (i3))
3736       {
3737         *new_direct_jump_p = 1;
3738         mark_jump_label (PATTERN (i3), i3, 0);
3739
3740         if ((temp = next_nonnote_insn (i3)) == NULL_RTX
3741             || !BARRIER_P (temp))
3742           emit_barrier_after (i3);
3743       }
3744
3745     if (undobuf.other_insn != NULL_RTX
3746         && (returnjump_p (undobuf.other_insn)
3747             || any_uncondjump_p (undobuf.other_insn)))
3748       {
3749         *new_direct_jump_p = 1;
3750
3751         if ((temp = next_nonnote_insn (undobuf.other_insn)) == NULL_RTX
3752             || !BARRIER_P (temp))
3753           emit_barrier_after (undobuf.other_insn);
3754       }
3755
3756     /* An NOOP jump does not need barrier, but it does need cleaning up
3757        of CFG.  */
3758     if (GET_CODE (newpat) == SET
3759         && SET_SRC (newpat) == pc_rtx
3760         && SET_DEST (newpat) == pc_rtx)
3761       *new_direct_jump_p = 1;
3762   }
3763   
3764   if (undobuf.other_insn != NULL_RTX)
3765     {
3766       if (dump_file)
3767         {
3768           fprintf (dump_file, "modifying other_insn ");
3769           dump_insn_slim (dump_file, undobuf.other_insn);
3770         }
3771       df_insn_rescan (undobuf.other_insn);
3772     }
3773
3774   if (i1 && !(NOTE_P(i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
3775     {
3776       if (dump_file)
3777         {
3778           fprintf (dump_file, "modifying insn i1 ");
3779           dump_insn_slim (dump_file, i1);
3780         }
3781       df_insn_rescan (i1);
3782     }
3783
3784   if (i2 && !(NOTE_P(i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
3785     {
3786       if (dump_file)
3787         {
3788           fprintf (dump_file, "modifying insn i2 ");
3789           dump_insn_slim (dump_file, i2);
3790         }
3791       df_insn_rescan (i2);
3792     }
3793
3794   if (i3 && !(NOTE_P(i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
3795     {
3796       if (dump_file)
3797         {
3798           fprintf (dump_file, "modifying insn i3 ");
3799           dump_insn_slim (dump_file, i3);
3800         }
3801       df_insn_rescan (i3);
3802     }
3803   
3804   combine_successes++;
3805   undo_commit ();
3806
3807   if (added_links_insn
3808       && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
3809       && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
3810     return added_links_insn;
3811   else
3812     return newi2pat ? i2 : i3;
3813 }
3814 \f
3815 /* Undo all the modifications recorded in undobuf.  */
3816
3817 static void
3818 undo_all (void)
3819 {
3820   struct undo *undo, *next;
3821
3822   for (undo = undobuf.undos; undo; undo = next)
3823     {
3824       next = undo->next;
3825       switch (undo->kind)
3826         {
3827         case UNDO_RTX:
3828           *undo->where.r = undo->old_contents.r;
3829           break;
3830         case UNDO_INT:
3831           *undo->where.i = undo->old_contents.i;
3832           break;
3833         case UNDO_MODE:
3834           adjust_reg_mode (*undo->where.r, undo->old_contents.m);
3835           break;
3836         default:
3837           gcc_unreachable ();
3838         }
3839
3840       undo->next = undobuf.frees;
3841       undobuf.frees = undo;
3842     }
3843
3844   undobuf.undos = 0;
3845 }
3846
3847 /* We've committed to accepting the changes we made.  Move all
3848    of the undos to the free list.  */
3849
3850 static void
3851 undo_commit (void)
3852 {
3853   struct undo *undo, *next;
3854
3855   for (undo = undobuf.undos; undo; undo = next)
3856     {
3857       next = undo->next;
3858       undo->next = undobuf.frees;
3859       undobuf.frees = undo;
3860     }
3861   undobuf.undos = 0;
3862 }
3863 \f
3864 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
3865    where we have an arithmetic expression and return that point.  LOC will
3866    be inside INSN.
3867
3868    try_combine will call this function to see if an insn can be split into
3869    two insns.  */
3870
3871 static rtx *
3872 find_split_point (rtx *loc, rtx insn)
3873 {
3874   rtx x = *loc;
3875   enum rtx_code code = GET_CODE (x);
3876   rtx *split;
3877   unsigned HOST_WIDE_INT len = 0;
3878   HOST_WIDE_INT pos = 0;
3879   int unsignedp = 0;
3880   rtx inner = NULL_RTX;
3881
3882   /* First special-case some codes.  */
3883   switch (code)
3884     {
3885     case SUBREG:
3886 #ifdef INSN_SCHEDULING
3887       /* If we are making a paradoxical SUBREG invalid, it becomes a split
3888          point.  */
3889       if (MEM_P (SUBREG_REG (x)))
3890         return loc;
3891 #endif
3892       return find_split_point (&SUBREG_REG (x), insn);
3893
3894     case MEM:
3895 #ifdef HAVE_lo_sum
3896       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
3897          using LO_SUM and HIGH.  */
3898       if (GET_CODE (XEXP (x, 0)) == CONST
3899           || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
3900         {
3901           SUBST (XEXP (x, 0),
3902                  gen_rtx_LO_SUM (Pmode,
3903                                  gen_rtx_HIGH (Pmode, XEXP (x, 0)),
3904                                  XEXP (x, 0)));
3905           return &XEXP (XEXP (x, 0), 0);
3906         }
3907 #endif
3908
3909       /* If we have a PLUS whose second operand is a constant and the
3910          address is not valid, perhaps will can split it up using
3911          the machine-specific way to split large constants.  We use
3912          the first pseudo-reg (one of the virtual regs) as a placeholder;
3913          it will not remain in the result.  */
3914       if (GET_CODE (XEXP (x, 0)) == PLUS
3915           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3916           && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
3917         {
3918           rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
3919           rtx seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
3920                                                       XEXP (x, 0)),
3921                                          subst_insn);
3922
3923           /* This should have produced two insns, each of which sets our
3924              placeholder.  If the source of the second is a valid address,
3925              we can make put both sources together and make a split point
3926              in the middle.  */
3927
3928           if (seq
3929               && NEXT_INSN (seq) != NULL_RTX
3930               && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
3931               && NONJUMP_INSN_P (seq)
3932               && GET_CODE (PATTERN (seq)) == SET
3933               && SET_DEST (PATTERN (seq)) == reg
3934               && ! reg_mentioned_p (reg,
3935                                     SET_SRC (PATTERN (seq)))
3936               && NONJUMP_INSN_P (NEXT_INSN (seq))
3937               && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
3938               && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
3939               && memory_address_p (GET_MODE (x),
3940                                    SET_SRC (PATTERN (NEXT_INSN (seq)))))
3941             {
3942               rtx src1 = SET_SRC (PATTERN (seq));
3943               rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
3944
3945               /* Replace the placeholder in SRC2 with SRC1.  If we can
3946                  find where in SRC2 it was placed, that can become our
3947                  split point and we can replace this address with SRC2.
3948                  Just try two obvious places.  */
3949
3950               src2 = replace_rtx (src2, reg, src1);
3951               split = 0;
3952               if (XEXP (src2, 0) == src1)
3953                 split = &XEXP (src2, 0);
3954               else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
3955                        && XEXP (XEXP (src2, 0), 0) == src1)
3956                 split = &XEXP (XEXP (src2, 0), 0);
3957
3958               if (split)
3959                 {
3960                   SUBST (XEXP (x, 0), src2);
3961                   return split;
3962                 }
3963             }
3964
3965           /* If that didn't work, perhaps the first operand is complex and
3966              needs to be computed separately, so make a split point there.
3967              This will occur on machines that just support REG + CONST
3968              and have a constant moved through some previous computation.  */
3969
3970           else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
3971                    && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
3972                          && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
3973             return &XEXP (XEXP (x, 0), 0);
3974         }
3975
3976       /* If we have a PLUS whose first operand is complex, try computing it
3977          separately by making a split there.  */
3978       if (GET_CODE (XEXP (x, 0)) == PLUS
3979           && ! memory_address_p (GET_MODE (x), XEXP (x, 0))
3980           && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
3981           && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
3982                 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
3983         return &XEXP (XEXP (x, 0), 0);
3984       break;
3985
3986     case SET:
3987 #ifdef HAVE_cc0
3988       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
3989          ZERO_EXTRACT, the most likely reason why this doesn't match is that
3990          we need to put the operand into a register.  So split at that
3991          point.  */
3992
3993       if (SET_DEST (x) == cc0_rtx
3994           && GET_CODE (SET_SRC (x)) != COMPARE
3995           && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
3996           && !OBJECT_P (SET_SRC (x))
3997           && ! (GET_CODE (SET_SRC (x)) == SUBREG
3998                 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
3999         return &SET_SRC (x);
4000 #endif
4001
4002       /* See if we can split SET_SRC as it stands.  */
4003       split = find_split_point (&SET_SRC (x), insn);
4004       if (split && split != &SET_SRC (x))
4005         return split;
4006
4007       /* See if we can split SET_DEST as it stands.  */
4008       split = find_split_point (&SET_DEST (x), insn);
4009       if (split && split != &SET_DEST (x))
4010         return split;
4011
4012       /* See if this is a bitfield assignment with everything constant.  If
4013          so, this is an IOR of an AND, so split it into that.  */
4014       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4015           && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
4016               <= HOST_BITS_PER_WIDE_INT)
4017           && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
4018           && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
4019           && GET_CODE (SET_SRC (x)) == CONST_INT
4020           && ((INTVAL (XEXP (SET_DEST (x), 1))
4021                + INTVAL (XEXP (SET_DEST (x), 2)))
4022               <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
4023           && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4024         {
4025           HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4026           unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4027           unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4028           rtx dest = XEXP (SET_DEST (x), 0);
4029           enum machine_mode mode = GET_MODE (dest);
4030           unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
4031           rtx or_mask;
4032
4033           if (BITS_BIG_ENDIAN)
4034             pos = GET_MODE_BITSIZE (mode) - len - pos;
4035
4036           or_mask = gen_int_mode (src << pos, mode);
4037           if (src == mask)
4038             SUBST (SET_SRC (x),
4039                    simplify_gen_binary (IOR, mode, dest, or_mask));
4040           else
4041             {
4042               rtx negmask = gen_int_mode (~(mask << pos), mode);
4043               SUBST (SET_SRC (x),
4044                      simplify_gen_binary (IOR, mode,
4045                                           simplify_gen_binary (AND, mode,
4046                                                                dest, negmask),
4047                                           or_mask));
4048             }
4049
4050           SUBST (SET_DEST (x), dest);
4051
4052           split = find_split_point (&SET_SRC (x), insn);
4053           if (split && split != &SET_SRC (x))
4054             return split;
4055         }
4056
4057       /* Otherwise, see if this is an operation that we can split into two.
4058          If so, try to split that.  */
4059       code = GET_CODE (SET_SRC (x));
4060
4061       switch (code)
4062         {
4063         case AND:
4064           /* If we are AND'ing with a large constant that is only a single
4065              bit and the result is only being used in a context where we
4066              need to know if it is zero or nonzero, replace it with a bit
4067              extraction.  This will avoid the large constant, which might
4068              have taken more than one insn to make.  If the constant were
4069              not a valid argument to the AND but took only one insn to make,
4070              this is no worse, but if it took more than one insn, it will
4071              be better.  */
4072
4073           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
4074               && REG_P (XEXP (SET_SRC (x), 0))
4075               && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4076               && REG_P (SET_DEST (x))
4077               && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
4078               && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4079               && XEXP (*split, 0) == SET_DEST (x)
4080               && XEXP (*split, 1) == const0_rtx)
4081             {
4082               rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4083                                                 XEXP (SET_SRC (x), 0),
4084                                                 pos, NULL_RTX, 1, 1, 0, 0);
4085               if (extraction != 0)
4086                 {
4087                   SUBST (SET_SRC (x), extraction);
4088                   return find_split_point (loc, insn);
4089                 }
4090             }
4091           break;
4092
4093         case NE:
4094           /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4095              is known to be on, this can be converted into a NEG of a shift.  */
4096           if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4097               && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4098               && 1 <= (pos = exact_log2
4099                        (nonzero_bits (XEXP (SET_SRC (x), 0),
4100                                       GET_MODE (XEXP (SET_SRC (x), 0))))))
4101             {
4102               enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4103
4104               SUBST (SET_SRC (x),
4105                      gen_rtx_NEG (mode,
4106                                   gen_rtx_LSHIFTRT (mode,
4107                                                     XEXP (SET_SRC (x), 0),
4108                                                     GEN_INT (pos))));
4109
4110               split = find_split_point (&SET_SRC (x), insn);
4111               if (split && split != &SET_SRC (x))
4112                 return split;
4113             }
4114           break;
4115
4116         case SIGN_EXTEND:
4117           inner = XEXP (SET_SRC (x), 0);
4118
4119           /* We can't optimize if either mode is a partial integer
4120              mode as we don't know how many bits are significant
4121              in those modes.  */
4122           if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4123               || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4124             break;
4125
4126           pos = 0;
4127           len = GET_MODE_BITSIZE (GET_MODE (inner));
4128           unsignedp = 0;
4129           break;
4130
4131         case SIGN_EXTRACT:
4132         case ZERO_EXTRACT:
4133           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
4134               && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
4135             {
4136               inner = XEXP (SET_SRC (x), 0);
4137               len = INTVAL (XEXP (SET_SRC (x), 1));
4138               pos = INTVAL (XEXP (SET_SRC (x), 2));
4139
4140               if (BITS_BIG_ENDIAN)
4141                 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
4142               unsignedp = (code == ZERO_EXTRACT);
4143             }
4144           break;
4145
4146         default:
4147           break;
4148         }
4149
4150       if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
4151         {
4152           enum machine_mode mode = GET_MODE (SET_SRC (x));
4153
4154           /* For unsigned, we have a choice of a shift followed by an
4155              AND or two shifts.  Use two shifts for field sizes where the
4156              constant might be too large.  We assume here that we can
4157              always at least get 8-bit constants in an AND insn, which is
4158              true for every current RISC.  */
4159
4160           if (unsignedp && len <= 8)
4161             {
4162               SUBST (SET_SRC (x),
4163                      gen_rtx_AND (mode,
4164                                   gen_rtx_LSHIFTRT
4165                                   (mode, gen_lowpart (mode, inner),
4166                                    GEN_INT (pos)),
4167                                   GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
4168
4169               split = find_split_point (&SET_SRC (x), insn);
4170               if (split && split != &SET_SRC (x))
4171                 return split;
4172             }
4173           else
4174             {
4175               SUBST (SET_SRC (x),
4176                      gen_rtx_fmt_ee
4177                      (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4178                       gen_rtx_ASHIFT (mode,
4179                                       gen_lowpart (mode, inner),
4180                                       GEN_INT (GET_MODE_BITSIZE (mode)
4181                                                - len - pos)),
4182                       GEN_INT (GET_MODE_BITSIZE (mode) - len)));
4183
4184               split = find_split_point (&SET_SRC (x), insn);
4185               if (split && split != &SET_SRC (x))
4186                 return split;
4187             }
4188         }
4189
4190       /* See if this is a simple operation with a constant as the second
4191          operand.  It might be that this constant is out of range and hence
4192          could be used as a split point.  */
4193       if (BINARY_P (SET_SRC (x))
4194           && CONSTANT_P (XEXP (SET_SRC (x), 1))
4195           && (OBJECT_P (XEXP (SET_SRC (x), 0))
4196               || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4197                   && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4198         return &XEXP (SET_SRC (x), 1);
4199
4200       /* Finally, see if this is a simple operation with its first operand
4201          not in a register.  The operation might require this operand in a
4202          register, so return it as a split point.  We can always do this
4203          because if the first operand were another operation, we would have
4204          already found it as a split point.  */
4205       if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4206           && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4207         return &XEXP (SET_SRC (x), 0);
4208
4209       return 0;
4210
4211     case AND:
4212     case IOR:
4213       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4214          it is better to write this as (not (ior A B)) so we can split it.
4215          Similarly for IOR.  */
4216       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4217         {
4218           SUBST (*loc,
4219                  gen_rtx_NOT (GET_MODE (x),
4220                               gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4221                                               GET_MODE (x),
4222                                               XEXP (XEXP (x, 0), 0),
4223                                               XEXP (XEXP (x, 1), 0))));
4224           return find_split_point (loc, insn);
4225         }
4226
4227       /* Many RISC machines have a large set of logical insns.  If the
4228          second operand is a NOT, put it first so we will try to split the
4229          other operand first.  */
4230       if (GET_CODE (XEXP (x, 1)) == NOT)
4231         {
4232           rtx tem = XEXP (x, 0);
4233           SUBST (XEXP (x, 0), XEXP (x, 1));
4234           SUBST (XEXP (x, 1), tem);
4235         }
4236       break;
4237
4238     default:
4239       break;
4240     }
4241
4242   /* Otherwise, select our actions depending on our rtx class.  */
4243   switch (GET_RTX_CLASS (code))
4244     {
4245     case RTX_BITFIELD_OPS:              /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
4246     case RTX_TERNARY:
4247       split = find_split_point (&XEXP (x, 2), insn);
4248       if (split)
4249         return split;
4250       /* ... fall through ...  */
4251     case RTX_BIN_ARITH:
4252     case RTX_COMM_ARITH:
4253     case RTX_COMPARE:
4254     case RTX_COMM_COMPARE:
4255       split = find_split_point (&XEXP (x, 1), insn);
4256       if (split)
4257         return split;
4258       /* ... fall through ...  */
4259     case RTX_UNARY:
4260       /* Some machines have (and (shift ...) ...) insns.  If X is not
4261          an AND, but XEXP (X, 0) is, use it as our split point.  */
4262       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4263         return &XEXP (x, 0);
4264
4265       split = find_split_point (&XEXP (x, 0), insn);
4266       if (split)
4267         return split;
4268       return loc;
4269
4270     default:
4271       /* Otherwise, we don't have a split point.  */
4272       return 0;
4273     }
4274 }
4275 \f
4276 /* Throughout X, replace FROM with TO, and return the result.
4277    The result is TO if X is FROM;
4278    otherwise the result is X, but its contents may have been modified.
4279    If they were modified, a record was made in undobuf so that
4280    undo_all will (among other things) return X to its original state.
4281
4282    If the number of changes necessary is too much to record to undo,
4283    the excess changes are not made, so the result is invalid.
4284    The changes already made can still be undone.
4285    undobuf.num_undo is incremented for such changes, so by testing that
4286    the caller can tell whether the result is valid.
4287
4288    `n_occurrences' is incremented each time FROM is replaced.
4289
4290    IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4291
4292    UNIQUE_COPY is nonzero if each substitution must be unique.  We do this
4293    by copying if `n_occurrences' is nonzero.  */
4294
4295 static rtx
4296 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
4297 {
4298   enum rtx_code code = GET_CODE (x);
4299   enum machine_mode op0_mode = VOIDmode;
4300   const char *fmt;
4301   int len, i;
4302   rtx new;
4303
4304 /* Two expressions are equal if they are identical copies of a shared
4305    RTX or if they are both registers with the same register number
4306    and mode.  */
4307
4308 #define COMBINE_RTX_EQUAL_P(X,Y)                        \
4309   ((X) == (Y)                                           \
4310    || (REG_P (X) && REG_P (Y)   \
4311        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4312
4313   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
4314     {
4315       n_occurrences++;
4316       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
4317     }
4318
4319   /* If X and FROM are the same register but different modes, they
4320      will not have been seen as equal above.  However, the log links code
4321      will make a LOG_LINKS entry for that case.  If we do nothing, we
4322      will try to rerecognize our original insn and, when it succeeds,
4323      we will delete the feeding insn, which is incorrect.
4324
4325      So force this insn not to match in this (rare) case.  */
4326   if (! in_dest && code == REG && REG_P (from)
4327       && reg_overlap_mentioned_p (x, from))
4328     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
4329
4330   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4331      of which may contain things that can be combined.  */
4332   if (code != MEM && code != LO_SUM && OBJECT_P (x))
4333     return x;
4334
4335   /* It is possible to have a subexpression appear twice in the insn.
4336      Suppose that FROM is a register that appears within TO.
4337      Then, after that subexpression has been scanned once by `subst',
4338      the second time it is scanned, TO may be found.  If we were
4339      to scan TO here, we would find FROM within it and create a
4340      self-referent rtl structure which is completely wrong.  */
4341   if (COMBINE_RTX_EQUAL_P (x, to))
4342     return to;
4343
4344   /* Parallel asm_operands need special attention because all of the
4345      inputs are shared across the arms.  Furthermore, unsharing the
4346      rtl results in recognition failures.  Failure to handle this case
4347      specially can result in circular rtl.
4348
4349      Solve this by doing a normal pass across the first entry of the
4350      parallel, and only processing the SET_DESTs of the subsequent
4351      entries.  Ug.  */
4352
4353   if (code == PARALLEL
4354       && GET_CODE (XVECEXP (x, 0, 0)) == SET
4355       && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
4356     {
4357       new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
4358
4359       /* If this substitution failed, this whole thing fails.  */
4360       if (GET_CODE (new) == CLOBBER
4361           && XEXP (new, 0) == const0_rtx)
4362         return new;
4363
4364       SUBST (XVECEXP (x, 0, 0), new);
4365
4366       for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
4367         {
4368           rtx dest = SET_DEST (XVECEXP (x, 0, i));
4369
4370           if (!REG_P (dest)
4371               && GET_CODE (dest) != CC0
4372               && GET_CODE (dest) != PC)
4373             {
4374               new = subst (dest, from, to, 0, unique_copy);
4375
4376               /* If this substitution failed, this whole thing fails.  */
4377               if (GET_CODE (new) == CLOBBER
4378                   && XEXP (new, 0) == const0_rtx)
4379                 return new;
4380
4381               SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
4382             }
4383         }
4384     }
4385   else
4386     {
4387       len = GET_RTX_LENGTH (code);
4388       fmt = GET_RTX_FORMAT (code);
4389
4390       /* We don't need to process a SET_DEST that is a register, CC0,
4391          or PC, so set up to skip this common case.  All other cases
4392          where we want to suppress replacing something inside a
4393          SET_SRC are handled via the IN_DEST operand.  */
4394       if (code == SET
4395           && (REG_P (SET_DEST (x))
4396               || GET_CODE (SET_DEST (x)) == CC0
4397               || GET_CODE (SET_DEST (x)) == PC))
4398         fmt = "ie";
4399
4400       /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
4401          constant.  */
4402       if (fmt[0] == 'e')
4403         op0_mode = GET_MODE (XEXP (x, 0));
4404
4405       for (i = 0; i < len; i++)
4406         {
4407           if (fmt[i] == 'E')
4408             {
4409               int j;
4410               for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4411                 {
4412                   if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
4413                     {
4414                       new = (unique_copy && n_occurrences
4415                              ? copy_rtx (to) : to);
4416                       n_occurrences++;
4417                     }
4418                   else
4419                     {
4420                       new = subst (XVECEXP (x, i, j), from, to, 0,
4421                                    unique_copy);
4422
4423                       /* If this substitution failed, this whole thing
4424                          fails.  */
4425                       if (GET_CODE (new) == CLOBBER
4426                           && XEXP (new, 0) == const0_rtx)
4427                         return new;
4428                     }
4429
4430                   SUBST (XVECEXP (x, i, j), new);
4431                 }
4432             }
4433           else if (fmt[i] == 'e')
4434             {
4435               /* If this is a register being set, ignore it.  */
4436               new = XEXP (x, i);
4437               if (in_dest
4438                   && i == 0
4439                   && (((code == SUBREG || code == ZERO_EXTRACT)
4440                        && REG_P (new))
4441                       || code == STRICT_LOW_PART))
4442                 ;
4443
4444               else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
4445                 {
4446                   /* In general, don't install a subreg involving two
4447                      modes not tieable.  It can worsen register
4448                      allocation, and can even make invalid reload
4449                      insns, since the reg inside may need to be copied
4450                      from in the outside mode, and that may be invalid
4451                      if it is an fp reg copied in integer mode.
4452
4453                      We allow two exceptions to this: It is valid if
4454                      it is inside another SUBREG and the mode of that
4455                      SUBREG and the mode of the inside of TO is
4456                      tieable and it is valid if X is a SET that copies
4457                      FROM to CC0.  */
4458
4459                   if (GET_CODE (to) == SUBREG
4460                       && ! MODES_TIEABLE_P (GET_MODE (to),
4461                                             GET_MODE (SUBREG_REG (to)))
4462                       && ! (code == SUBREG
4463                             && MODES_TIEABLE_P (GET_MODE (x),
4464                                                 GET_MODE (SUBREG_REG (to))))
4465 #ifdef HAVE_cc0
4466                       && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
4467 #endif
4468                       )
4469                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
4470
4471 #ifdef CANNOT_CHANGE_MODE_CLASS
4472                   if (code == SUBREG
4473                       && REG_P (to)
4474                       && REGNO (to) < FIRST_PSEUDO_REGISTER
4475                       && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
4476                                                    GET_MODE (to),
4477                                                    GET_MODE (x)))
4478                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
4479 #endif
4480
4481                   new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
4482                   n_occurrences++;
4483                 }
4484               else
4485                 /* If we are in a SET_DEST, suppress most cases unless we
4486                    have gone inside a MEM, in which case we want to
4487                    simplify the address.  We assume here that things that
4488                    are actually part of the destination have their inner
4489                    parts in the first expression.  This is true for SUBREG,
4490                    STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
4491                    things aside from REG and MEM that should appear in a
4492                    SET_DEST.  */
4493                 new = subst (XEXP (x, i), from, to,
4494                              (((in_dest
4495                                 && (code == SUBREG || code == STRICT_LOW_PART
4496                                     || code == ZERO_EXTRACT))
4497                                || code == SET)
4498                               && i == 0), unique_copy);
4499
4500               /* If we found that we will have to reject this combination,
4501                  indicate that by returning the CLOBBER ourselves, rather than
4502                  an expression containing it.  This will speed things up as
4503                  well as prevent accidents where two CLOBBERs are considered
4504                  to be equal, thus producing an incorrect simplification.  */
4505
4506               if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
4507                 return new;
4508
4509               if (GET_CODE (x) == SUBREG
4510                   && (GET_CODE (new) == CONST_INT
4511                       || GET_CODE (new) == CONST_DOUBLE))
4512                 {
4513                   enum machine_mode mode = GET_MODE (x);
4514
4515                   x = simplify_subreg (GET_MODE (x), new,
4516                                        GET_MODE (SUBREG_REG (x)),
4517                                        SUBREG_BYTE (x));
4518                   if (! x)
4519                     x = gen_rtx_CLOBBER (mode, const0_rtx);
4520                 }
4521               else if (GET_CODE (new) == CONST_INT
4522                        && GET_CODE (x) == ZERO_EXTEND)
4523                 {
4524                   x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
4525                                                 new, GET_MODE (XEXP (x, 0)));
4526                   gcc_assert (x);
4527                 }
4528               else
4529                 SUBST (XEXP (x, i), new);
4530             }
4531         }
4532     }
4533
4534   /* Check if we are loading something from the constant pool via float
4535      extension; in this case we would undo compress_float_constant
4536      optimization and degenerate constant load to an immediate value.  */
4537   if (GET_CODE (x) == FLOAT_EXTEND
4538       && MEM_P (XEXP (x, 0))
4539       && MEM_READONLY_P (XEXP (x, 0)))
4540     {
4541       rtx tmp = avoid_constant_pool_reference (x);
4542       if (x != tmp)
4543         return x;
4544     }
4545
4546   /* Try to simplify X.  If the simplification changed the code, it is likely
4547      that further simplification will help, so loop, but limit the number
4548      of repetitions that will be performed.  */
4549
4550   for (i = 0; i < 4; i++)
4551     {
4552       /* If X is sufficiently simple, don't bother trying to do anything
4553          with it.  */
4554       if (code != CONST_INT && code != REG && code != CLOBBER)
4555         x = combine_simplify_rtx (x, op0_mode, in_dest);
4556
4557       if (GET_CODE (x) == code)
4558         break;
4559
4560       code = GET_CODE (x);
4561
4562       /* We no longer know the original mode of operand 0 since we
4563          have changed the form of X)  */
4564       op0_mode = VOIDmode;
4565     }
4566
4567   return x;
4568 }
4569 \f
4570 /* Simplify X, a piece of RTL.  We just operate on the expression at the
4571    outer level; call `subst' to simplify recursively.  Return the new
4572    expression.
4573
4574    OP0_MODE is the original mode of XEXP (x, 0).  IN_DEST is nonzero
4575    if we are inside a SET_DEST.  */
4576
4577 static rtx
4578 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
4579 {
4580   enum rtx_code code = GET_CODE (x);
4581   enum machine_mode mode = GET_MODE (x);
4582   rtx temp;
4583   int i;
4584
4585   /* If this is a commutative operation, put a constant last and a complex
4586      expression first.  We don't need to do this for comparisons here.  */
4587   if (COMMUTATIVE_ARITH_P (x)
4588       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
4589     {
4590       temp = XEXP (x, 0);
4591       SUBST (XEXP (x, 0), XEXP (x, 1));
4592       SUBST (XEXP (x, 1), temp);
4593     }
4594
4595   /* If this is a simple operation applied to an IF_THEN_ELSE, try
4596      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
4597      things.  Check for cases where both arms are testing the same
4598      condition.
4599
4600      Don't do anything if all operands are very simple.  */
4601
4602   if ((BINARY_P (x)
4603        && ((!OBJECT_P (XEXP (x, 0))
4604             && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4605                   && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
4606            || (!OBJECT_P (XEXP (x, 1))
4607                && ! (GET_CODE (XEXP (x, 1)) == SUBREG
4608                      && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
4609       || (UNARY_P (x)
4610           && (!OBJECT_P (XEXP (x, 0))
4611                && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4612                      && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
4613     {
4614       rtx cond, true_rtx, false_rtx;
4615
4616       cond = if_then_else_cond (x, &true_rtx, &false_rtx);
4617       if (cond != 0
4618           /* If everything is a comparison, what we have is highly unlikely
4619              to be simpler, so don't use it.  */
4620           && ! (COMPARISON_P (x)
4621                 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
4622         {
4623           rtx cop1 = const0_rtx;
4624           enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
4625
4626           if (cond_code == NE && COMPARISON_P (cond))
4627             return x;
4628
4629           /* Simplify the alternative arms; this may collapse the true and
4630              false arms to store-flag values.  Be careful to use copy_rtx
4631              here since true_rtx or false_rtx might share RTL with x as a
4632              result of the if_then_else_cond call above.  */
4633           true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
4634           false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
4635
4636           /* If true_rtx and false_rtx are not general_operands, an if_then_else
4637              is unlikely to be simpler.  */
4638           if (general_operand (true_rtx, VOIDmode)
4639               && general_operand (false_rtx, VOIDmode))
4640             {
4641               enum rtx_code reversed;
4642
4643               /* Restarting if we generate a store-flag expression will cause
4644                  us to loop.  Just drop through in this case.  */
4645
4646               /* If the result values are STORE_FLAG_VALUE and zero, we can
4647                  just make the comparison operation.  */
4648               if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
4649                 x = simplify_gen_relational (cond_code, mode, VOIDmode,
4650                                              cond, cop1);
4651               else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
4652                        && ((reversed = reversed_comparison_code_parts
4653                                         (cond_code, cond, cop1, NULL))
4654                            != UNKNOWN))
4655                 x = simplify_gen_relational (reversed, mode, VOIDmode,
4656                                              cond, cop1);
4657
4658               /* Likewise, we can make the negate of a comparison operation
4659                  if the result values are - STORE_FLAG_VALUE and zero.  */
4660               else if (GET_CODE (true_rtx) == CONST_INT
4661                        && INTVAL (true_rtx) == - STORE_FLAG_VALUE
4662                        && false_rtx == const0_rtx)
4663                 x = simplify_gen_unary (NEG, mode,
4664                                         simplify_gen_relational (cond_code,
4665                                                                  mode, VOIDmode,
4666                                                                  cond, cop1),
4667                                         mode);
4668               else if (GET_CODE (false_rtx) == CONST_INT
4669                        && INTVAL (false_rtx) == - STORE_FLAG_VALUE
4670                        && true_rtx == const0_rtx
4671                        && ((reversed = reversed_comparison_code_parts
4672                                         (cond_code, cond, cop1, NULL))
4673                            != UNKNOWN))
4674                 x = simplify_gen_unary (NEG, mode,
4675                                         simplify_gen_relational (reversed,
4676                                                                  mode, VOIDmode,
4677                                                                  cond, cop1),
4678                                         mode);
4679               else
4680                 return gen_rtx_IF_THEN_ELSE (mode,
4681                                              simplify_gen_relational (cond_code,
4682                                                                       mode,
4683                                                                       VOIDmode,
4684                                                                       cond,
4685                                                                       cop1),
4686                                              true_rtx, false_rtx);
4687
4688               code = GET_CODE (x);
4689               op0_mode = VOIDmode;
4690             }
4691         }
4692     }
4693
4694   /* Try to fold this expression in case we have constants that weren't
4695      present before.  */
4696   temp = 0;
4697   switch (GET_RTX_CLASS (code))
4698     {
4699     case RTX_UNARY:
4700       if (op0_mode == VOIDmode)
4701         op0_mode = GET_MODE (XEXP (x, 0));
4702       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
4703       break;
4704     case RTX_COMPARE:
4705     case RTX_COMM_COMPARE:
4706       {
4707         enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
4708         if (cmp_mode == VOIDmode)
4709           {
4710             cmp_mode = GET_MODE (XEXP (x, 1));
4711             if (cmp_mode == VOIDmode)
4712               cmp_mode = op0_mode;
4713           }
4714         temp = simplify_relational_operation (code, mode, cmp_mode,
4715                                               XEXP (x, 0), XEXP (x, 1));
4716       }
4717       break;
4718     case RTX_COMM_ARITH:
4719     case RTX_BIN_ARITH:
4720       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
4721       break;
4722     case RTX_BITFIELD_OPS:
4723     case RTX_TERNARY:
4724       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
4725                                          XEXP (x, 1), XEXP (x, 2));
4726       break;
4727     default:
4728       break;
4729     }
4730
4731   if (temp)
4732     {
4733       x = temp;
4734       code = GET_CODE (temp);
4735       op0_mode = VOIDmode;
4736       mode = GET_MODE (temp);
4737     }
4738
4739   /* First see if we can apply the inverse distributive law.  */
4740   if (code == PLUS || code == MINUS
4741       || code == AND || code == IOR || code == XOR)
4742     {
4743       x = apply_distributive_law (x);
4744       code = GET_CODE (x);
4745       op0_mode = VOIDmode;
4746     }
4747
4748   /* If CODE is an associative operation not otherwise handled, see if we
4749      can associate some operands.  This can win if they are constants or
4750      if they are logically related (i.e. (a & b) & a).  */
4751   if ((code == PLUS || code == MINUS || code == MULT || code == DIV
4752        || code == AND || code == IOR || code == XOR
4753        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
4754       && ((INTEGRAL_MODE_P (mode) && code != DIV)
4755           || (flag_associative_math && FLOAT_MODE_P (mode))))
4756     {
4757       if (GET_CODE (XEXP (x, 0)) == code)
4758         {
4759           rtx other = XEXP (XEXP (x, 0), 0);
4760           rtx inner_op0 = XEXP (XEXP (x, 0), 1);
4761           rtx inner_op1 = XEXP (x, 1);
4762           rtx inner;
4763
4764           /* Make sure we pass the constant operand if any as the second
4765              one if this is a commutative operation.  */
4766           if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
4767             {
4768               rtx tem = inner_op0;
4769               inner_op0 = inner_op1;
4770               inner_op1 = tem;
4771             }
4772           inner = simplify_binary_operation (code == MINUS ? PLUS
4773                                              : code == DIV ? MULT
4774                                              : code,
4775                                              mode, inner_op0, inner_op1);
4776
4777           /* For commutative operations, try the other pair if that one
4778              didn't simplify.  */
4779           if (inner == 0 && COMMUTATIVE_ARITH_P (x))
4780             {
4781               other = XEXP (XEXP (x, 0), 1);
4782               inner = simplify_binary_operation (code, mode,
4783                                                  XEXP (XEXP (x, 0), 0),
4784                                                  XEXP (x, 1));
4785             }
4786
4787           if (inner)
4788             return simplify_gen_binary (code, mode, other, inner);
4789         }
4790     }
4791
4792   /* A little bit of algebraic simplification here.  */
4793   switch (code)
4794     {
4795     case MEM:
4796       /* Ensure that our address has any ASHIFTs converted to MULT in case
4797          address-recognizing predicates are called later.  */
4798       temp = make_compound_operation (XEXP (x, 0), MEM);
4799       SUBST (XEXP (x, 0), temp);
4800       break;
4801
4802     case SUBREG:
4803       if (op0_mode == VOIDmode)
4804         op0_mode = GET_MODE (SUBREG_REG (x));
4805
4806       /* See if this can be moved to simplify_subreg.  */
4807       if (CONSTANT_P (SUBREG_REG (x))
4808           && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
4809              /* Don't call gen_lowpart if the inner mode
4810                 is VOIDmode and we cannot simplify it, as SUBREG without
4811                 inner mode is invalid.  */
4812           && (GET_MODE (SUBREG_REG (x)) != VOIDmode
4813               || gen_lowpart_common (mode, SUBREG_REG (x))))
4814         return gen_lowpart (mode, SUBREG_REG (x));
4815
4816       if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
4817         break;
4818       {
4819         rtx temp;
4820         temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
4821                                 SUBREG_BYTE (x));
4822         if (temp)
4823           return temp;
4824       }
4825
4826       /* Don't change the mode of the MEM if that would change the meaning
4827          of the address.  */
4828       if (MEM_P (SUBREG_REG (x))
4829           && (MEM_VOLATILE_P (SUBREG_REG (x))
4830               || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
4831         return gen_rtx_CLOBBER (mode, const0_rtx);
4832
4833       /* Note that we cannot do any narrowing for non-constants since
4834          we might have been counting on using the fact that some bits were
4835          zero.  We now do this in the SET.  */
4836
4837       break;
4838
4839     case NEG:
4840       temp = expand_compound_operation (XEXP (x, 0));
4841
4842       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
4843          replaced by (lshiftrt X C).  This will convert
4844          (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
4845
4846       if (GET_CODE (temp) == ASHIFTRT
4847           && GET_CODE (XEXP (temp, 1)) == CONST_INT
4848           && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
4849         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
4850                                      INTVAL (XEXP (temp, 1)));
4851
4852       /* If X has only a single bit that might be nonzero, say, bit I, convert
4853          (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
4854          MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
4855          (sign_extract X 1 Y).  But only do this if TEMP isn't a register
4856          or a SUBREG of one since we'd be making the expression more
4857          complex if it was just a register.  */
4858
4859       if (!REG_P (temp)
4860           && ! (GET_CODE (temp) == SUBREG
4861                 && REG_P (SUBREG_REG (temp)))
4862           && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
4863         {
4864           rtx temp1 = simplify_shift_const
4865             (NULL_RTX, ASHIFTRT, mode,
4866              simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
4867                                    GET_MODE_BITSIZE (mode) - 1 - i),
4868              GET_MODE_BITSIZE (mode) - 1 - i);
4869
4870           /* If all we did was surround TEMP with the two shifts, we
4871              haven't improved anything, so don't use it.  Otherwise,
4872              we are better off with TEMP1.  */
4873           if (GET_CODE (temp1) != ASHIFTRT
4874               || GET_CODE (XEXP (temp1, 0)) != ASHIFT
4875               || XEXP (XEXP (temp1, 0), 0) != temp)
4876             return temp1;
4877         }
4878       break;
4879
4880     case TRUNCATE:
4881       /* We can't handle truncation to a partial integer mode here
4882          because we don't know the real bitsize of the partial
4883          integer mode.  */
4884       if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
4885         break;
4886
4887       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4888           && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4889                                     GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
4890         SUBST (XEXP (x, 0),
4891                force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
4892                               GET_MODE_MASK (mode), 0));
4893
4894       /* Similarly to what we do in simplify-rtx.c, a truncate of a register
4895          whose value is a comparison can be replaced with a subreg if
4896          STORE_FLAG_VALUE permits.  */
4897       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4898           && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4899           && (temp = get_last_value (XEXP (x, 0)))
4900           && COMPARISON_P (temp))
4901         return gen_lowpart (mode, XEXP (x, 0));
4902       break;
4903
4904 #ifdef HAVE_cc0
4905     case COMPARE:
4906       /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4907          using cc0, in which case we want to leave it as a COMPARE
4908          so we can distinguish it from a register-register-copy.  */
4909       if (XEXP (x, 1) == const0_rtx)
4910         return XEXP (x, 0);
4911
4912       /* x - 0 is the same as x unless x's mode has signed zeros and
4913          allows rounding towards -infinity.  Under those conditions,
4914          0 - 0 is -0.  */
4915       if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4916             && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4917           && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4918         return XEXP (x, 0);
4919       break;
4920 #endif
4921
4922     case CONST:
4923       /* (const (const X)) can become (const X).  Do it this way rather than
4924          returning the inner CONST since CONST can be shared with a
4925          REG_EQUAL note.  */
4926       if (GET_CODE (XEXP (x, 0)) == CONST)
4927         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4928       break;
4929
4930 #ifdef HAVE_lo_sum
4931     case LO_SUM:
4932       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
4933          can add in an offset.  find_split_point will split this address up
4934          again if it doesn't match.  */
4935       if (GET_CODE (XEXP (x, 0)) == HIGH
4936           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4937         return XEXP (x, 1);
4938       break;
4939 #endif
4940
4941     case PLUS:
4942       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4943          when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4944          bit-field and can be replaced by either a sign_extend or a
4945          sign_extract.  The `and' may be a zero_extend and the two
4946          <c>, -<c> constants may be reversed.  */
4947       if (GET_CODE (XEXP (x, 0)) == XOR
4948           && GET_CODE (XEXP (x, 1)) == CONST_INT
4949           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4950           && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4951           && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4952               || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4953           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4954           && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4955                && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4956                && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4957                    == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4958               || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4959                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4960                       == (unsigned int) i + 1))))
4961         return simplify_shift_const
4962           (NULL_RTX, ASHIFTRT, mode,
4963            simplify_shift_const (NULL_RTX, ASHIFT, mode,
4964                                  XEXP (XEXP (XEXP (x, 0), 0), 0),
4965                                  GET_MODE_BITSIZE (mode) - (i + 1)),
4966            GET_MODE_BITSIZE (mode) - (i + 1));
4967
4968       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4969          can become (ashiftrt (ashift (xor x 1) C) C) where C is
4970          the bitsize of the mode - 1.  This allows simplification of
4971          "a = (b & 8) == 0;"  */
4972       if (XEXP (x, 1) == constm1_rtx
4973           && !REG_P (XEXP (x, 0))
4974           && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4975                 && REG_P (SUBREG_REG (XEXP (x, 0))))
4976           && nonzero_bits (XEXP (x, 0), mode) == 1)
4977         return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4978            simplify_shift_const (NULL_RTX, ASHIFT, mode,
4979                                  gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4980                                  GET_MODE_BITSIZE (mode) - 1),
4981            GET_MODE_BITSIZE (mode) - 1);
4982
4983       /* If we are adding two things that have no bits in common, convert
4984          the addition into an IOR.  This will often be further simplified,
4985          for example in cases like ((a & 1) + (a & 2)), which can
4986          become a & 3.  */
4987
4988       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4989           && (nonzero_bits (XEXP (x, 0), mode)
4990               & nonzero_bits (XEXP (x, 1), mode)) == 0)
4991         {
4992           /* Try to simplify the expression further.  */
4993           rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4994           temp = combine_simplify_rtx (tor, mode, in_dest);
4995
4996           /* If we could, great.  If not, do not go ahead with the IOR
4997              replacement, since PLUS appears in many special purpose
4998              address arithmetic instructions.  */
4999           if (GET_CODE (temp) != CLOBBER && temp != tor)
5000             return temp;
5001         }
5002       break;
5003
5004     case MINUS:
5005       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5006          (and <foo> (const_int pow2-1))  */
5007       if (GET_CODE (XEXP (x, 1)) == AND
5008           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5009           && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5010           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5011         return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5012                                        -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5013       break;
5014
5015     case MULT:
5016       /* If we have (mult (plus A B) C), apply the distributive law and then
5017          the inverse distributive law to see if things simplify.  This
5018          occurs mostly in addresses, often when unrolling loops.  */
5019
5020       if (GET_CODE (XEXP (x, 0)) == PLUS)
5021         {
5022           rtx result = distribute_and_simplify_rtx (x, 0);
5023           if (result)
5024             return result;
5025         }
5026
5027       /* Try simplify a*(b/c) as (a*b)/c.  */
5028       if (FLOAT_MODE_P (mode) && flag_associative_math 
5029           && GET_CODE (XEXP (x, 0)) == DIV)
5030         {
5031           rtx tem = simplify_binary_operation (MULT, mode,
5032                                                XEXP (XEXP (x, 0), 0),
5033                                                XEXP (x, 1));
5034           if (tem)
5035             return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5036         }
5037       break;
5038
5039     case UDIV:
5040       /* If this is a divide by a power of two, treat it as a shift if
5041          its first operand is a shift.  */
5042       if (GET_CODE (XEXP (x, 1)) == CONST_INT
5043           && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
5044           && (GET_CODE (XEXP (x, 0)) == ASHIFT
5045               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5046               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5047               || GET_CODE (XEXP (x, 0)) == ROTATE
5048               || GET_CODE (XEXP (x, 0)) == ROTATERT))
5049         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5050       break;
5051
5052     case EQ:  case NE:
5053     case GT:  case GTU:  case GE:  case GEU:
5054     case LT:  case LTU:  case LE:  case LEU:
5055     case UNEQ:  case LTGT:
5056     case UNGT:  case UNGE:
5057     case UNLT:  case UNLE:
5058     case UNORDERED: case ORDERED:
5059       /* If the first operand is a condition code, we can't do anything
5060          with it.  */
5061       if (GET_CODE (XEXP (x, 0)) == COMPARE
5062           || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5063               && ! CC0_P (XEXP (x, 0))))
5064         {
5065           rtx op0 = XEXP (x, 0);
5066           rtx op1 = XEXP (x, 1);
5067           enum rtx_code new_code;
5068
5069           if (GET_CODE (op0) == COMPARE)
5070             op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5071
5072           /* Simplify our comparison, if possible.  */
5073           new_code = simplify_comparison (code, &op0, &op1);
5074
5075           /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5076              if only the low-order bit is possibly nonzero in X (such as when
5077              X is a ZERO_EXTRACT of one bit).  Similarly, we can convert EQ to
5078              (xor X 1) or (minus 1 X); we use the former.  Finally, if X is
5079              known to be either 0 or -1, NE becomes a NEG and EQ becomes
5080              (plus X 1).
5081
5082              Remove any ZERO_EXTRACT we made when thinking this was a
5083              comparison.  It may now be simpler to use, e.g., an AND.  If a
5084              ZERO_EXTRACT is indeed appropriate, it will be placed back by
5085              the call to make_compound_operation in the SET case.  */
5086
5087           if (STORE_FLAG_VALUE == 1
5088               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5089               && op1 == const0_rtx
5090               && mode == GET_MODE (op0)
5091               && nonzero_bits (op0, mode) == 1)
5092             return gen_lowpart (mode,
5093                                 expand_compound_operation (op0));
5094
5095           else if (STORE_FLAG_VALUE == 1
5096                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5097                    && op1 == const0_rtx
5098                    && mode == GET_MODE (op0)
5099                    && (num_sign_bit_copies (op0, mode)
5100                        == GET_MODE_BITSIZE (mode)))
5101             {
5102               op0 = expand_compound_operation (op0);
5103               return simplify_gen_unary (NEG, mode,
5104                                          gen_lowpart (mode, op0),
5105                                          mode);
5106             }
5107
5108           else if (STORE_FLAG_VALUE == 1
5109                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5110                    && op1 == const0_rtx
5111                    && mode == GET_MODE (op0)
5112                    && nonzero_bits (op0, mode) == 1)
5113             {
5114               op0 = expand_compound_operation (op0);
5115               return simplify_gen_binary (XOR, mode,
5116                                           gen_lowpart (mode, op0),
5117                                           const1_rtx);
5118             }
5119
5120           else if (STORE_FLAG_VALUE == 1
5121                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5122                    && op1 == const0_rtx
5123                    && mode == GET_MODE (op0)
5124                    && (num_sign_bit_copies (op0, mode)
5125                        == GET_MODE_BITSIZE (mode)))
5126             {
5127               op0 = expand_compound_operation (op0);
5128               return plus_constant (gen_lowpart (mode, op0), 1);
5129             }
5130
5131           /* If STORE_FLAG_VALUE is -1, we have cases similar to
5132              those above.  */
5133           if (STORE_FLAG_VALUE == -1
5134               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5135               && op1 == const0_rtx
5136               && (num_sign_bit_copies (op0, mode)
5137                   == GET_MODE_BITSIZE (mode)))
5138             return gen_lowpart (mode,
5139                                 expand_compound_operation (op0));
5140
5141           else if (STORE_FLAG_VALUE == -1
5142                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5143                    && op1 == const0_rtx
5144                    && mode == GET_MODE (op0)
5145                    && nonzero_bits (op0, mode) == 1)
5146             {
5147               op0 = expand_compound_operation (op0);
5148               return simplify_gen_unary (NEG, mode,
5149                                          gen_lowpart (mode, op0),
5150                                          mode);
5151             }
5152
5153           else if (STORE_FLAG_VALUE == -1
5154                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5155                    && op1 == const0_rtx
5156                    && mode == GET_MODE (op0)
5157                    && (num_sign_bit_copies (op0, mode)
5158                        == GET_MODE_BITSIZE (mode)))
5159             {
5160               op0 = expand_compound_operation (op0);
5161               return simplify_gen_unary (NOT, mode,
5162                                          gen_lowpart (mode, op0),
5163                                          mode);
5164             }
5165
5166           /* If X is 0/1, (eq X 0) is X-1.  */
5167           else if (STORE_FLAG_VALUE == -1
5168                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5169                    && op1 == const0_rtx
5170                    && mode == GET_MODE (op0)
5171                    && nonzero_bits (op0, mode) == 1)
5172             {
5173               op0 = expand_compound_operation (op0);
5174               return plus_constant (gen_lowpart (mode, op0), -1);
5175             }
5176
5177           /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5178              one bit that might be nonzero, we can convert (ne x 0) to
5179              (ashift x c) where C puts the bit in the sign bit.  Remove any
5180              AND with STORE_FLAG_VALUE when we are done, since we are only
5181              going to test the sign bit.  */
5182           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5183               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5184               && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5185                   == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5186               && op1 == const0_rtx
5187               && mode == GET_MODE (op0)
5188               && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5189             {
5190               x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5191                                         expand_compound_operation (op0),
5192                                         GET_MODE_BITSIZE (mode) - 1 - i);
5193               if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5194                 return XEXP (x, 0);
5195               else
5196                 return x;
5197             }
5198
5199           /* If the code changed, return a whole new comparison.  */
5200           if (new_code != code)
5201             return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5202
5203           /* Otherwise, keep this operation, but maybe change its operands.
5204              This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
5205           SUBST (XEXP (x, 0), op0);
5206           SUBST (XEXP (x, 1), op1);
5207         }
5208       break;
5209
5210     case IF_THEN_ELSE:
5211       return simplify_if_then_else (x);
5212
5213     case ZERO_EXTRACT:
5214     case SIGN_EXTRACT:
5215     case ZERO_EXTEND:
5216     case SIGN_EXTEND:
5217       /* If we are processing SET_DEST, we are done.  */
5218       if (in_dest)
5219         return x;
5220
5221       return expand_compound_operation (x);
5222
5223     case SET:
5224       return simplify_set (x);
5225
5226     case AND:
5227     case IOR:
5228       return simplify_logical (x);
5229
5230     case ASHIFT:
5231     case LSHIFTRT:
5232     case ASHIFTRT:
5233     case ROTATE:
5234     case ROTATERT:
5235       /* If this is a shift by a constant amount, simplify it.  */
5236       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5237         return simplify_shift_const (x, code, mode, XEXP (x, 0),
5238                                      INTVAL (XEXP (x, 1)));
5239
5240       else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5241         SUBST (XEXP (x, 1),
5242                force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5243                               ((HOST_WIDE_INT) 1
5244                                << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5245                               - 1,
5246                               0));
5247       break;
5248
5249     default:
5250       break;
5251     }
5252
5253   return x;
5254 }
5255 \f
5256 /* Simplify X, an IF_THEN_ELSE expression.  Return the new expression.  */
5257
5258 static rtx
5259 simplify_if_then_else (rtx x)
5260 {
5261   enum machine_mode mode = GET_MODE (x);
5262   rtx cond = XEXP (x, 0);
5263   rtx true_rtx = XEXP (x, 1);
5264   rtx false_rtx = XEXP (x, 2);
5265   enum rtx_code true_code = GET_CODE (cond);
5266   int comparison_p = COMPARISON_P (cond);
5267   rtx temp;
5268   int i;
5269   enum rtx_code false_code;
5270   rtx reversed;
5271
5272   /* Simplify storing of the truth value.  */
5273   if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5274     return simplify_gen_relational (true_code, mode, VOIDmode,
5275                                     XEXP (cond, 0), XEXP (cond, 1));
5276
5277   /* Also when the truth value has to be reversed.  */
5278   if (comparison_p
5279       && true_rtx == const0_rtx && false_rtx == const_true_rtx
5280       && (reversed = reversed_comparison (cond, mode)))
5281     return reversed;
5282
5283   /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5284      in it is being compared against certain values.  Get the true and false
5285      comparisons and see if that says anything about the value of each arm.  */
5286
5287   if (comparison_p
5288       && ((false_code = reversed_comparison_code (cond, NULL))
5289           != UNKNOWN)
5290       && REG_P (XEXP (cond, 0)))
5291     {
5292       HOST_WIDE_INT nzb;
5293       rtx from = XEXP (cond, 0);
5294       rtx true_val = XEXP (cond, 1);
5295       rtx false_val = true_val;
5296       int swapped = 0;
5297
5298       /* If FALSE_CODE is EQ, swap the codes and arms.  */
5299
5300       if (false_code == EQ)
5301         {
5302           swapped = 1, true_code = EQ, false_code = NE;
5303           temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5304         }
5305
5306       /* If we are comparing against zero and the expression being tested has
5307          only a single bit that might be nonzero, that is its value when it is
5308          not equal to zero.  Similarly if it is known to be -1 or 0.  */
5309
5310       if (true_code == EQ && true_val == const0_rtx
5311           && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5312         {
5313           false_code = EQ;
5314           false_val = GEN_INT (trunc_int_for_mode (nzb, GET_MODE (from)));
5315         }
5316       else if (true_code == EQ && true_val == const0_rtx
5317                && (num_sign_bit_copies (from, GET_MODE (from))
5318                    == GET_MODE_BITSIZE (GET_MODE (from))))
5319         {
5320           false_code = EQ;
5321           false_val = constm1_rtx;
5322         }
5323
5324       /* Now simplify an arm if we know the value of the register in the
5325          branch and it is used in the arm.  Be careful due to the potential
5326          of locally-shared RTL.  */
5327
5328       if (reg_mentioned_p (from, true_rtx))
5329         true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
5330                                       from, true_val),
5331                       pc_rtx, pc_rtx, 0, 0);
5332       if (reg_mentioned_p (from, false_rtx))
5333         false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
5334                                    from, false_val),
5335                        pc_rtx, pc_rtx, 0, 0);
5336
5337       SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
5338       SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
5339
5340       true_rtx = XEXP (x, 1);
5341       false_rtx = XEXP (x, 2);
5342       true_code = GET_CODE (cond);
5343     }
5344
5345   /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
5346      reversed, do so to avoid needing two sets of patterns for
5347      subtract-and-branch insns.  Similarly if we have a constant in the true
5348      arm, the false arm is the same as the first operand of the comparison, or
5349      the false arm is more complicated than the true arm.  */
5350
5351   if (comparison_p
5352       && reversed_comparison_code (cond, NULL) != UNKNOWN
5353       && (true_rtx == pc_rtx
5354           || (CONSTANT_P (true_rtx)
5355               && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
5356           || true_rtx == const0_rtx
5357           || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
5358           || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
5359               && !OBJECT_P (false_rtx))
5360           || reg_mentioned_p (true_rtx, false_rtx)
5361           || rtx_equal_p (false_rtx, XEXP (cond, 0))))
5362     {
5363       true_code = reversed_comparison_code (cond, NULL);
5364       SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
5365       SUBST (XEXP (x, 1), false_rtx);
5366       SUBST (XEXP (x, 2), true_rtx);
5367
5368       temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5369       cond = XEXP (x, 0);
5370
5371       /* It is possible that the conditional has been simplified out.  */
5372       true_code = GET_CODE (cond);
5373       comparison_p = COMPARISON_P (cond);
5374     }
5375
5376   /* If the two arms are identical, we don't need the comparison.  */
5377
5378   if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
5379     return true_rtx;
5380
5381   /* Convert a == b ? b : a to "a".  */
5382   if (true_code == EQ && ! side_effects_p (cond)
5383       && !HONOR_NANS (mode)
5384       && rtx_equal_p (XEXP (cond, 0), false_rtx)
5385       && rtx_equal_p (XEXP (cond, 1), true_rtx))
5386     return false_rtx;
5387   else if (true_code == NE && ! side_effects_p (cond)
5388            && !HONOR_NANS (mode)
5389            && rtx_equal_p (XEXP (cond, 0), true_rtx)
5390            && rtx_equal_p (XEXP (cond, 1), false_rtx))
5391     return true_rtx;
5392
5393   /* Look for cases where we have (abs x) or (neg (abs X)).  */
5394
5395   if (GET_MODE_CLASS (mode) == MODE_INT
5396       && comparison_p
5397       && XEXP (cond, 1) == const0_rtx
5398       && GET_CODE (false_rtx) == NEG
5399       && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
5400       && rtx_equal_p (true_rtx, XEXP (cond, 0))
5401       && ! side_effects_p (true_rtx))
5402     switch (true_code)
5403       {
5404       case GT:
5405       case GE:
5406         return simplify_gen_unary (ABS, mode, true_rtx, mode);
5407       case LT:
5408       case LE:
5409         return
5410           simplify_gen_unary (NEG, mode,
5411                               simplify_gen_unary (ABS, mode, true_rtx, mode),
5412                               mode);
5413       default:
5414         break;
5415       }
5416
5417   /* Look for MIN or MAX.  */
5418
5419   if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
5420       && comparison_p
5421       && rtx_equal_p (XEXP (cond, 0), true_rtx)
5422       && rtx_equal_p (XEXP (cond, 1), false_rtx)
5423       && ! side_effects_p (cond))
5424     switch (true_code)
5425       {
5426       case GE:
5427       case GT:
5428         return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
5429       case LE:
5430       case LT:
5431         return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
5432       case GEU:
5433       case GTU:
5434         return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
5435       case LEU:
5436       case LTU:
5437         return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
5438       default:
5439         break;
5440       }
5441
5442   /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
5443      second operand is zero, this can be done as (OP Z (mult COND C2)) where
5444      C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
5445      SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
5446      We can do this kind of thing in some cases when STORE_FLAG_VALUE is
5447      neither 1 or -1, but it isn't worth checking for.  */
5448
5449   if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5450       && comparison_p
5451       && GET_MODE_CLASS (mode) == MODE_INT
5452       && ! side_effects_p (x))
5453     {
5454       rtx t = make_compound_operation (true_rtx, SET);
5455       rtx f = make_compound_operation (false_rtx, SET);
5456       rtx cond_op0 = XEXP (cond, 0);
5457       rtx cond_op1 = XEXP (cond, 1);
5458       enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
5459       enum machine_mode m = mode;
5460       rtx z = 0, c1 = NULL_RTX;
5461
5462       if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
5463            || GET_CODE (t) == IOR || GET_CODE (t) == XOR
5464            || GET_CODE (t) == ASHIFT
5465            || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
5466           && rtx_equal_p (XEXP (t, 0), f))
5467         c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
5468
5469       /* If an identity-zero op is commutative, check whether there
5470          would be a match if we swapped the operands.  */
5471       else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
5472                 || GET_CODE (t) == XOR)
5473                && rtx_equal_p (XEXP (t, 1), f))
5474         c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
5475       else if (GET_CODE (t) == SIGN_EXTEND
5476                && (GET_CODE (XEXP (t, 0)) == PLUS
5477                    || GET_CODE (XEXP (t, 0)) == MINUS
5478                    || GET_CODE (XEXP (t, 0)) == IOR
5479                    || GET_CODE (XEXP (t, 0)) == XOR
5480                    || GET_CODE (XEXP (t, 0)) == ASHIFT
5481                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5482                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5483                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5484                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5485                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5486                && (num_sign_bit_copies (f, GET_MODE (f))
5487                    > (unsigned int)
5488                      (GET_MODE_BITSIZE (mode)
5489                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
5490         {
5491           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5492           extend_op = SIGN_EXTEND;
5493           m = GET_MODE (XEXP (t, 0));
5494         }
5495       else if (GET_CODE (t) == SIGN_EXTEND
5496                && (GET_CODE (XEXP (t, 0)) == PLUS
5497                    || GET_CODE (XEXP (t, 0)) == IOR
5498                    || GET_CODE (XEXP (t, 0)) == XOR)
5499                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5500                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5501                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5502                && (num_sign_bit_copies (f, GET_MODE (f))
5503                    > (unsigned int)
5504                      (GET_MODE_BITSIZE (mode)
5505                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
5506         {
5507           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5508           extend_op = SIGN_EXTEND;
5509           m = GET_MODE (XEXP (t, 0));
5510         }
5511       else if (GET_CODE (t) == ZERO_EXTEND
5512                && (GET_CODE (XEXP (t, 0)) == PLUS
5513                    || GET_CODE (XEXP (t, 0)) == MINUS
5514                    || GET_CODE (XEXP (t, 0)) == IOR
5515                    || GET_CODE (XEXP (t, 0)) == XOR
5516                    || GET_CODE (XEXP (t, 0)) == ASHIFT
5517                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5518                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5519                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5520                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5521                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5522                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5523                && ((nonzero_bits (f, GET_MODE (f))
5524                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
5525                    == 0))
5526         {
5527           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5528           extend_op = ZERO_EXTEND;
5529           m = GET_MODE (XEXP (t, 0));
5530         }
5531       else if (GET_CODE (t) == ZERO_EXTEND
5532                && (GET_CODE (XEXP (t, 0)) == PLUS
5533                    || GET_CODE (XEXP (t, 0)) == IOR
5534                    || GET_CODE (XEXP (t, 0)) == XOR)
5535                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5536                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5537                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5538                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5539                && ((nonzero_bits (f, GET_MODE (f))
5540                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
5541                    == 0))
5542         {
5543           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5544           extend_op = ZERO_EXTEND;
5545           m = GET_MODE (XEXP (t, 0));
5546         }
5547
5548       if (z)
5549         {
5550           temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
5551                                                  cond_op0, cond_op1),
5552                         pc_rtx, pc_rtx, 0, 0);
5553           temp = simplify_gen_binary (MULT, m, temp,
5554                                       simplify_gen_binary (MULT, m, c1,
5555                                                            const_true_rtx));
5556           temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
5557           temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
5558
5559           if (extend_op != UNKNOWN)
5560             temp = simplify_gen_unary (extend_op, mode, temp, m);
5561
5562           return temp;
5563         }
5564     }
5565
5566   /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
5567      1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
5568      negation of a single bit, we can convert this operation to a shift.  We
5569      can actually do this more generally, but it doesn't seem worth it.  */
5570
5571   if (true_code == NE && XEXP (cond, 1) == const0_rtx
5572       && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5573       && ((1 == nonzero_bits (XEXP (cond, 0), mode)
5574            && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
5575           || ((num_sign_bit_copies (XEXP (cond, 0), mode)
5576                == GET_MODE_BITSIZE (mode))
5577               && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
5578     return
5579       simplify_shift_const (NULL_RTX, ASHIFT, mode,
5580                             gen_lowpart (mode, XEXP (cond, 0)), i);
5581
5582   /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8.  */
5583   if (true_code == NE && XEXP (cond, 1) == const0_rtx
5584       && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5585       && GET_MODE (XEXP (cond, 0)) == mode
5586       && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
5587           == nonzero_bits (XEXP (cond, 0), mode)
5588       && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
5589     return XEXP (cond, 0);
5590
5591   return x;
5592 }
5593 \f
5594 /* Simplify X, a SET expression.  Return the new expression.  */
5595
5596 static rtx
5597 simplify_set (rtx x)
5598 {
5599   rtx src = SET_SRC (x);
5600   rtx dest = SET_DEST (x);
5601   enum machine_mode mode
5602     = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
5603   rtx other_insn;
5604   rtx *cc_use;
5605
5606   /* (set (pc) (return)) gets written as (return).  */
5607   if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
5608     return src;
5609
5610   /* Now that we know for sure which bits of SRC we are using, see if we can
5611      simplify the expression for the object knowing that we only need the
5612      low-order bits.  */
5613
5614   if (GET_MODE_CLASS (mode) == MODE_INT
5615       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5616     {
5617       src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, 0);
5618       SUBST (SET_SRC (x), src);
5619     }
5620
5621   /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5622      the comparison result and try to simplify it unless we already have used
5623      undobuf.other_insn.  */
5624   if ((GET_MODE_CLASS (mode) == MODE_CC
5625        || GET_CODE (src) == COMPARE
5626        || CC0_P (dest))
5627       && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5628       && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5629       && COMPARISON_P (*cc_use)
5630       && rtx_equal_p (XEXP (*cc_use, 0), dest))
5631     {
5632       enum rtx_code old_code = GET_CODE (*cc_use);
5633       enum rtx_code new_code;
5634       rtx op0, op1, tmp;
5635       int other_changed = 0;
5636       enum machine_mode compare_mode = GET_MODE (dest);
5637
5638       if (GET_CODE (src) == COMPARE)
5639         op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5640       else
5641         op0 = src, op1 = CONST0_RTX (GET_MODE (src));
5642
5643       tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
5644                                            op0, op1);
5645       if (!tmp)
5646         new_code = old_code;
5647       else if (!CONSTANT_P (tmp))
5648         {
5649           new_code = GET_CODE (tmp);
5650           op0 = XEXP (tmp, 0);
5651           op1 = XEXP (tmp, 1);
5652         }
5653       else
5654         {
5655           rtx pat = PATTERN (other_insn);
5656           undobuf.other_insn = other_insn;
5657           SUBST (*cc_use, tmp);
5658
5659           /* Attempt to simplify CC user.  */
5660           if (GET_CODE (pat) == SET)
5661             {
5662               rtx new = simplify_rtx (SET_SRC (pat));
5663               if (new != NULL_RTX)
5664                 SUBST (SET_SRC (pat), new);
5665             }
5666
5667           /* Convert X into a no-op move.  */
5668           SUBST (SET_DEST (x), pc_rtx);
5669           SUBST (SET_SRC (x), pc_rtx);
5670           return x;
5671         }
5672
5673       /* Simplify our comparison, if possible.  */
5674       new_code = simplify_comparison (new_code, &op0, &op1);
5675
5676 #ifdef SELECT_CC_MODE
5677       /* If this machine has CC modes other than CCmode, check to see if we
5678          need to use a different CC mode here.  */
5679       if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
5680         compare_mode = GET_MODE (op0);
5681       else
5682         compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5683
5684 #ifndef HAVE_cc0
5685       /* If the mode changed, we have to change SET_DEST, the mode in the
5686          compare, and the mode in the place SET_DEST is used.  If SET_DEST is
5687          a hard register, just build new versions with the proper mode.  If it
5688          is a pseudo, we lose unless it is only time we set the pseudo, in
5689          which case we can safely change its mode.  */
5690       if (compare_mode != GET_MODE (dest))
5691         {
5692           if (can_change_dest_mode (dest, 0, compare_mode))
5693             {
5694               unsigned int regno = REGNO (dest);
5695               rtx new_dest;
5696
5697               if (regno < FIRST_PSEUDO_REGISTER)
5698                 new_dest = gen_rtx_REG (compare_mode, regno);
5699               else
5700                 {
5701                   SUBST_MODE (regno_reg_rtx[regno], compare_mode);
5702                   new_dest = regno_reg_rtx[regno];
5703                 }
5704
5705               SUBST (SET_DEST (x), new_dest);
5706               SUBST (XEXP (*cc_use, 0), new_dest);
5707               other_changed = 1;
5708
5709               dest = new_dest;
5710             }
5711         }
5712 #endif  /* cc0 */
5713 #endif  /* SELECT_CC_MODE */
5714
5715       /* If the code changed, we have to build a new comparison in
5716          undobuf.other_insn.  */
5717       if (new_code != old_code)
5718         {
5719           int other_changed_previously = other_changed;
5720           unsigned HOST_WIDE_INT mask;
5721
5722           SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5723                                           dest, const0_rtx));
5724           other_changed = 1;
5725
5726           /* If the only change we made was to change an EQ into an NE or
5727              vice versa, OP0 has only one bit that might be nonzero, and OP1
5728              is zero, check if changing the user of the condition code will
5729              produce a valid insn.  If it won't, we can keep the original code
5730              in that insn by surrounding our operation with an XOR.  */
5731
5732           if (((old_code == NE && new_code == EQ)
5733                || (old_code == EQ && new_code == NE))
5734               && ! other_changed_previously && op1 == const0_rtx
5735               && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5736               && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5737             {
5738               rtx pat = PATTERN (other_insn), note = 0;
5739
5740               if ((recog_for_combine (&pat, other_insn, &note) < 0
5741                    && ! check_asm_operands (pat)))
5742                 {
5743                   PUT_CODE (*cc_use, old_code);
5744                   other_changed = 0;
5745
5746                   op0 = simplify_gen_binary (XOR, GET_MODE (op0),
5747                                              op0, GEN_INT (mask));
5748                 }
5749             }
5750         }
5751
5752       if (other_changed)
5753         undobuf.other_insn = other_insn;
5754
5755 #ifdef HAVE_cc0
5756       /* If we are now comparing against zero, change our source if
5757          needed.  If we do not use cc0, we always have a COMPARE.  */
5758       if (op1 == const0_rtx && dest == cc0_rtx)
5759         {
5760           SUBST (SET_SRC (x), op0);
5761           src = op0;
5762         }
5763       else
5764 #endif
5765
5766       /* Otherwise, if we didn't previously have a COMPARE in the
5767          correct mode, we need one.  */
5768       if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5769         {
5770           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5771           src = SET_SRC (x);
5772         }
5773       else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
5774         {
5775           SUBST (SET_SRC (x), op0);
5776           src = SET_SRC (x);
5777         }
5778       /* Otherwise, update the COMPARE if needed.  */
5779       else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
5780         {
5781           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5782           src = SET_SRC (x);
5783         }
5784     }
5785   else
5786     {
5787       /* Get SET_SRC in a form where we have placed back any
5788          compound expressions.  Then do the checks below.  */
5789       src = make_compound_operation (src, SET);
5790       SUBST (SET_SRC (x), src);
5791     }
5792
5793   /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5794      and X being a REG or (subreg (reg)), we may be able to convert this to
5795      (set (subreg:m2 x) (op)).
5796
5797      We can always do this if M1 is narrower than M2 because that means that
5798      we only care about the low bits of the result.
5799
5800      However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
5801      perform a narrower operation than requested since the high-order bits will
5802      be undefined.  On machine where it is defined, this transformation is safe
5803      as long as M1 and M2 have the same number of words.  */
5804
5805   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5806       && !OBJECT_P (SUBREG_REG (src))
5807       && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5808            / UNITS_PER_WORD)
5809           == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5810                + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5811 #ifndef WORD_REGISTER_OPERATIONS
5812       && (GET_MODE_SIZE (GET_MODE (src))
5813         < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5814 #endif
5815 #ifdef CANNOT_CHANGE_MODE_CLASS
5816       && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
5817             && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5818                                          GET_MODE (SUBREG_REG (src)),
5819                                          GET_MODE (src)))
5820 #endif
5821       && (REG_P (dest)
5822           || (GET_CODE (dest) == SUBREG
5823               && REG_P (SUBREG_REG (dest)))))
5824     {
5825       SUBST (SET_DEST (x),
5826              gen_lowpart (GET_MODE (SUBREG_REG (src)),
5827                                       dest));
5828       SUBST (SET_SRC (x), SUBREG_REG (src));
5829
5830       src = SET_SRC (x), dest = SET_DEST (x);
5831     }
5832
5833 #ifdef HAVE_cc0
5834   /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5835      in SRC.  */
5836   if (dest == cc0_rtx
5837       && GET_CODE (src) == SUBREG
5838       && subreg_lowpart_p (src)
5839       && (GET_MODE_BITSIZE (GET_MODE (src))
5840           < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5841     {
5842       rtx inner = SUBREG_REG (src);
5843       enum machine_mode inner_mode = GET_MODE (inner);
5844
5845       /* Here we make sure that we don't have a sign bit on.  */
5846       if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5847           && (nonzero_bits (inner, inner_mode)
5848               < ((unsigned HOST_WIDE_INT) 1
5849                  << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5850         {
5851           SUBST (SET_SRC (x), inner);
5852           src = SET_SRC (x);
5853         }
5854     }
5855 #endif
5856
5857 #ifdef LOAD_EXTEND_OP
5858   /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5859      would require a paradoxical subreg.  Replace the subreg with a
5860      zero_extend to avoid the reload that would otherwise be required.  */
5861
5862   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5863       && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
5864       && SUBREG_BYTE (src) == 0
5865       && (GET_MODE_SIZE (GET_MODE (src))
5866           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5867       && MEM_P (SUBREG_REG (src)))
5868     {
5869       SUBST (SET_SRC (x),
5870              gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5871                             GET_MODE (src), SUBREG_REG (src)));
5872
5873       src = SET_SRC (x);
5874     }
5875 #endif
5876
5877   /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5878      are comparing an item known to be 0 or -1 against 0, use a logical
5879      operation instead. Check for one of the arms being an IOR of the other
5880      arm with some value.  We compute three terms to be IOR'ed together.  In
5881      practice, at most two will be nonzero.  Then we do the IOR's.  */
5882
5883   if (GET_CODE (dest) != PC
5884       && GET_CODE (src) == IF_THEN_ELSE
5885       && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5886       && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5887       && XEXP (XEXP (src, 0), 1) == const0_rtx
5888       && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5889 #ifdef HAVE_conditional_move
5890       && ! can_conditionally_move_p (GET_MODE (src))
5891 #endif
5892       && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5893                                GET_MODE (XEXP (XEXP (src, 0), 0)))
5894           == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5895       && ! side_effects_p (src))
5896     {
5897       rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5898                       ? XEXP (src, 1) : XEXP (src, 2));
5899       rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5900                    ? XEXP (src, 2) : XEXP (src, 1));
5901       rtx term1 = const0_rtx, term2, term3;
5902
5903       if (GET_CODE (true_rtx) == IOR
5904           && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5905         term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5906       else if (GET_CODE (true_rtx) == IOR
5907                && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5908         term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5909       else if (GET_CODE (false_rtx) == IOR
5910                && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5911         term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5912       else if (GET_CODE (false_rtx) == IOR
5913                && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5914         term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5915
5916       term2 = simplify_gen_binary (AND, GET_MODE (src),
5917                                    XEXP (XEXP (src, 0), 0), true_rtx);
5918       term3 = simplify_gen_binary (AND, GET_MODE (src),
5919                                    simplify_gen_unary (NOT, GET_MODE (src),
5920                                                        XEXP (XEXP (src, 0), 0),
5921                                                        GET_MODE (src)),
5922                                    false_rtx);
5923
5924       SUBST (SET_SRC (x),
5925              simplify_gen_binary (IOR, GET_MODE (src),
5926                                   simplify_gen_binary (IOR, GET_MODE (src),
5927                                                        term1, term2),
5928                                   term3));
5929
5930       src = SET_SRC (x);
5931     }
5932
5933   /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5934      whole thing fail.  */
5935   if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5936     return src;
5937   else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5938     return dest;
5939   else
5940     /* Convert this into a field assignment operation, if possible.  */
5941     return make_field_assignment (x);
5942 }
5943 \f
5944 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5945    result.  */
5946
5947 static rtx
5948 simplify_logical (rtx x)
5949 {
5950   enum machine_mode mode = GET_MODE (x);
5951   rtx op0 = XEXP (x, 0);
5952   rtx op1 = XEXP (x, 1);
5953
5954   switch (GET_CODE (x))
5955     {
5956     case AND:
5957       /* We can call simplify_and_const_int only if we don't lose
5958          any (sign) bits when converting INTVAL (op1) to
5959          "unsigned HOST_WIDE_INT".  */
5960       if (GET_CODE (op1) == CONST_INT
5961           && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5962               || INTVAL (op1) > 0))
5963         {
5964           x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5965           if (GET_CODE (x) != AND)
5966             return x;
5967
5968           op0 = XEXP (x, 0);
5969           op1 = XEXP (x, 1);
5970         }
5971
5972       /* If we have any of (and (ior A B) C) or (and (xor A B) C),
5973          apply the distributive law and then the inverse distributive
5974          law to see if things simplify.  */
5975       if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5976         {
5977           rtx result = distribute_and_simplify_rtx (x, 0);
5978           if (result)
5979             return result;
5980         }
5981       if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5982         {
5983           rtx result = distribute_and_simplify_rtx (x, 1);
5984           if (result)
5985             return result;
5986         }
5987       break;
5988
5989     case IOR:
5990       /* If we have (ior (and A B) C), apply the distributive law and then
5991          the inverse distributive law to see if things simplify.  */
5992
5993       if (GET_CODE (op0) == AND)
5994         {
5995           rtx result = distribute_and_simplify_rtx (x, 0);
5996           if (result)
5997             return result;
5998         }
5999
6000       if (GET_CODE (op1) == AND)
6001         {
6002           rtx result = distribute_and_simplify_rtx (x, 1);
6003           if (result)
6004             return result;
6005         }
6006       break;
6007
6008     default:
6009       gcc_unreachable ();
6010     }
6011
6012   return x;
6013 }
6014 \f
6015 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6016    operations" because they can be replaced with two more basic operations.
6017    ZERO_EXTEND is also considered "compound" because it can be replaced with
6018    an AND operation, which is simpler, though only one operation.
6019
6020    The function expand_compound_operation is called with an rtx expression
6021    and will convert it to the appropriate shifts and AND operations,
6022    simplifying at each stage.
6023
6024    The function make_compound_operation is called to convert an expression
6025    consisting of shifts and ANDs into the equivalent compound expression.
6026    It is the inverse of this function, loosely speaking.  */
6027
6028 static rtx
6029 expand_compound_operation (rtx x)
6030 {
6031   unsigned HOST_WIDE_INT pos = 0, len;
6032   int unsignedp = 0;
6033   unsigned int modewidth;
6034   rtx tem;
6035
6036   switch (GET_CODE (x))
6037     {
6038     case ZERO_EXTEND:
6039       unsignedp = 1;
6040     case SIGN_EXTEND:
6041       /* We can't necessarily use a const_int for a multiword mode;
6042          it depends on implicitly extending the value.
6043          Since we don't know the right way to extend it,
6044          we can't tell whether the implicit way is right.
6045
6046          Even for a mode that is no wider than a const_int,
6047          we can't win, because we need to sign extend one of its bits through
6048          the rest of it, and we don't know which bit.  */
6049       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
6050         return x;
6051
6052       /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6053          (zero_extend:MODE FROM) or (sign_extend:MODE FROM).  It is for any MEM
6054          because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6055          reloaded. If not for that, MEM's would very rarely be safe.
6056
6057          Reject MODEs bigger than a word, because we might not be able
6058          to reference a two-register group starting with an arbitrary register
6059          (and currently gen_lowpart might crash for a SUBREG).  */
6060
6061       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6062         return x;
6063
6064       /* Reject MODEs that aren't scalar integers because turning vector
6065          or complex modes into shifts causes problems.  */
6066
6067       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6068         return x;
6069
6070       len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
6071       /* If the inner object has VOIDmode (the only way this can happen
6072          is if it is an ASM_OPERANDS), we can't do anything since we don't
6073          know how much masking to do.  */
6074       if (len == 0)
6075         return x;
6076
6077       break;
6078
6079     case ZERO_EXTRACT:
6080       unsignedp = 1;
6081
6082       /* ... fall through ...  */
6083
6084     case SIGN_EXTRACT:
6085       /* If the operand is a CLOBBER, just return it.  */
6086       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6087         return XEXP (x, 0);
6088
6089       if (GET_CODE (XEXP (x, 1)) != CONST_INT
6090           || GET_CODE (XEXP (x, 2)) != CONST_INT
6091           || GET_MODE (XEXP (x, 0)) == VOIDmode)
6092         return x;
6093
6094       /* Reject MODEs that aren't scalar integers because turning vector
6095          or complex modes into shifts causes problems.  */
6096
6097       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6098         return x;
6099
6100       len = INTVAL (XEXP (x, 1));
6101       pos = INTVAL (XEXP (x, 2));
6102
6103       /* This should stay within the object being extracted, fail otherwise.  */
6104       if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
6105         return x;
6106
6107       if (BITS_BIG_ENDIAN)
6108         pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
6109
6110       break;
6111
6112     default:
6113       return x;
6114     }
6115   /* Convert sign extension to zero extension, if we know that the high
6116      bit is not set, as this is easier to optimize.  It will be converted
6117      back to cheaper alternative in make_extraction.  */
6118   if (GET_CODE (x) == SIGN_EXTEND
6119       && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6120           && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6121                 & ~(((unsigned HOST_WIDE_INT)
6122                       GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6123                      >> 1))
6124                == 0)))
6125     {
6126       rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6127       rtx temp2 = expand_compound_operation (temp);
6128
6129       /* Make sure this is a profitable operation.  */
6130       if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
6131        return temp2;
6132       else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
6133        return temp;
6134       else
6135        return x;
6136     }
6137
6138   /* We can optimize some special cases of ZERO_EXTEND.  */
6139   if (GET_CODE (x) == ZERO_EXTEND)
6140     {
6141       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6142          know that the last value didn't have any inappropriate bits
6143          set.  */
6144       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6145           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6146           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6147           && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6148               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6149         return XEXP (XEXP (x, 0), 0);
6150
6151       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
6152       if (GET_CODE (XEXP (x, 0)) == SUBREG
6153           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6154           && subreg_lowpart_p (XEXP (x, 0))
6155           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6156           && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6157               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6158         return SUBREG_REG (XEXP (x, 0));
6159
6160       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6161          is a comparison and STORE_FLAG_VALUE permits.  This is like
6162          the first case, but it works even when GET_MODE (x) is larger
6163          than HOST_WIDE_INT.  */
6164       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6165           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6166           && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6167           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6168               <= HOST_BITS_PER_WIDE_INT)
6169           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
6170               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6171         return XEXP (XEXP (x, 0), 0);
6172
6173       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
6174       if (GET_CODE (XEXP (x, 0)) == SUBREG
6175           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6176           && subreg_lowpart_p (XEXP (x, 0))
6177           && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6178           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6179               <= HOST_BITS_PER_WIDE_INT)
6180           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
6181               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6182         return SUBREG_REG (XEXP (x, 0));
6183
6184     }
6185
6186   /* If we reach here, we want to return a pair of shifts.  The inner
6187      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
6188      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
6189      logical depending on the value of UNSIGNEDP.
6190
6191      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6192      converted into an AND of a shift.
6193
6194      We must check for the case where the left shift would have a negative
6195      count.  This can happen in a case like (x >> 31) & 255 on machines
6196      that can't shift by a constant.  On those machines, we would first
6197      combine the shift with the AND to produce a variable-position
6198      extraction.  Then the constant of 31 would be substituted in to produce
6199      a such a position.  */
6200
6201   modewidth = GET_MODE_BITSIZE (GET_MODE (x));
6202   if (modewidth + len >= pos)
6203     {
6204       enum machine_mode mode = GET_MODE (x);
6205       tem = gen_lowpart (mode, XEXP (x, 0));
6206       if (!tem || GET_CODE (tem) == CLOBBER)
6207         return x;
6208       tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6209                                   tem, modewidth - pos - len);
6210       tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6211                                   mode, tem, modewidth - len);
6212     }
6213   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6214     tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6215                                   simplify_shift_const (NULL_RTX, LSHIFTRT,
6216                                                         GET_MODE (x),
6217                                                         XEXP (x, 0), pos),
6218                                   ((HOST_WIDE_INT) 1 << len) - 1);
6219   else
6220     /* Any other cases we can't handle.  */
6221     return x;
6222
6223   /* If we couldn't do this for some reason, return the original
6224      expression.  */
6225   if (GET_CODE (tem) == CLOBBER)
6226     return x;
6227
6228   return tem;
6229 }
6230 \f
6231 /* X is a SET which contains an assignment of one object into
6232    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6233    or certain SUBREGS). If possible, convert it into a series of
6234    logical operations.
6235
6236    We half-heartedly support variable positions, but do not at all
6237    support variable lengths.  */
6238
6239 static const_rtx
6240 expand_field_assignment (const_rtx x)
6241 {
6242   rtx inner;
6243   rtx pos;                      /* Always counts from low bit.  */
6244   int len;
6245   rtx mask, cleared, masked;
6246   enum machine_mode compute_mode;
6247
6248   /* Loop until we find something we can't simplify.  */
6249   while (1)
6250     {
6251       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6252           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6253         {
6254           inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6255           len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
6256           pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6257         }
6258       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6259                && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
6260         {
6261           inner = XEXP (SET_DEST (x), 0);
6262           len = INTVAL (XEXP (SET_DEST (x), 1));
6263           pos = XEXP (SET_DEST (x), 2);
6264
6265           /* A constant position should stay within the width of INNER.  */
6266           if (GET_CODE (pos) == CONST_INT
6267               && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
6268             break;
6269
6270           if (BITS_BIG_ENDIAN)
6271             {
6272               if (GET_CODE (pos) == CONST_INT)
6273                 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
6274                                - INTVAL (pos));
6275               else if (GET_CODE (pos) == MINUS
6276                        && GET_CODE (XEXP (pos, 1)) == CONST_INT
6277                        && (INTVAL (XEXP (pos, 1))
6278                            == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
6279                 /* If position is ADJUST - X, new position is X.  */
6280                 pos = XEXP (pos, 0);
6281               else
6282                 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6283                                            GEN_INT (GET_MODE_BITSIZE (
6284                                                     GET_MODE (inner))
6285                                                     - len),
6286                                            pos);
6287             }
6288         }
6289
6290       /* A SUBREG between two modes that occupy the same numbers of words
6291          can be done by moving the SUBREG to the source.  */
6292       else if (GET_CODE (SET_DEST (x)) == SUBREG
6293                /* We need SUBREGs to compute nonzero_bits properly.  */
6294                && nonzero_sign_valid
6295                && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6296                      + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6297                    == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6298                         + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6299         {
6300           x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6301                            gen_lowpart
6302                            (GET_MODE (SUBREG_REG (SET_DEST (x))),
6303                             SET_SRC (x)));
6304           continue;
6305         }
6306       else
6307         break;
6308
6309       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6310         inner = SUBREG_REG (inner);
6311
6312       compute_mode = GET_MODE (inner);
6313
6314       /* Don't attempt bitwise arithmetic on non scalar integer modes.  */
6315       if (! SCALAR_INT_MODE_P (compute_mode))
6316         {
6317           enum machine_mode imode;
6318
6319           /* Don't do anything for vector or complex integral types.  */
6320           if (! FLOAT_MODE_P (compute_mode))
6321             break;
6322
6323           /* Try to find an integral mode to pun with.  */
6324           imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6325           if (imode == BLKmode)
6326             break;
6327
6328           compute_mode = imode;
6329           inner = gen_lowpart (imode, inner);
6330         }
6331
6332       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
6333       if (len >= HOST_BITS_PER_WIDE_INT)
6334         break;
6335
6336       /* Now compute the equivalent expression.  Make a copy of INNER
6337          for the SET_DEST in case it is a MEM into which we will substitute;
6338          we don't want shared RTL in that case.  */
6339       mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
6340       cleared = simplify_gen_binary (AND, compute_mode,
6341                                      simplify_gen_unary (NOT, compute_mode,
6342                                        simplify_gen_binary (ASHIFT,
6343                                                             compute_mode,
6344                                                             mask, pos),
6345                                        compute_mode),
6346                                      inner);
6347       masked = simplify_gen_binary (ASHIFT, compute_mode,
6348                                     simplify_gen_binary (
6349                                       AND, compute_mode,
6350                                       gen_lowpart (compute_mode, SET_SRC (x)),
6351                                       mask),
6352                                     pos);
6353
6354       x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
6355                        simplify_gen_binary (IOR, compute_mode,
6356                                             cleared, masked));
6357     }
6358
6359   return x;
6360 }
6361 \f
6362 /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
6363    it is an RTX that represents a variable starting position; otherwise,
6364    POS is the (constant) starting bit position (counted from the LSB).
6365
6366    UNSIGNEDP is nonzero for an unsigned reference and zero for a
6367    signed reference.
6368
6369    IN_DEST is nonzero if this is a reference in the destination of a
6370    SET.  This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If nonzero,
6371    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6372    be used.
6373
6374    IN_COMPARE is nonzero if we are in a COMPARE.  This means that a
6375    ZERO_EXTRACT should be built even for bits starting at bit 0.
6376
6377    MODE is the desired mode of the result (if IN_DEST == 0).
6378
6379    The result is an RTX for the extraction or NULL_RTX if the target
6380    can't handle it.  */
6381
6382 static rtx
6383 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6384                  rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6385                  int in_dest, int in_compare)
6386 {
6387   /* This mode describes the size of the storage area
6388      to fetch the overall value from.  Within that, we
6389      ignore the POS lowest bits, etc.  */
6390   enum machine_mode is_mode = GET_MODE (inner);
6391   enum machine_mode inner_mode;
6392   enum machine_mode wanted_inner_mode;
6393   enum machine_mode wanted_inner_reg_mode = word_mode;
6394   enum machine_mode pos_mode = word_mode;
6395   enum machine_mode extraction_mode = word_mode;
6396   enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6397   rtx new = 0;
6398   rtx orig_pos_rtx = pos_rtx;
6399   HOST_WIDE_INT orig_pos;
6400
6401   if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6402     {
6403       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6404          consider just the QI as the memory to extract from.
6405          The subreg adds or removes high bits; its mode is
6406          irrelevant to the meaning of this extraction,
6407          since POS and LEN count from the lsb.  */
6408       if (MEM_P (SUBREG_REG (inner)))
6409         is_mode = GET_MODE (SUBREG_REG (inner));
6410       inner = SUBREG_REG (inner);
6411     }
6412   else if (GET_CODE (inner) == ASHIFT
6413            && GET_CODE (XEXP (inner, 1)) == CONST_INT
6414            && pos_rtx == 0 && pos == 0
6415            && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6416     {
6417       /* We're extracting the least significant bits of an rtx
6418          (ashift X (const_int C)), where LEN > C.  Extract the
6419          least significant (LEN - C) bits of X, giving an rtx
6420          whose mode is MODE, then shift it left C times.  */
6421       new = make_extraction (mode, XEXP (inner, 0),
6422                              0, 0, len - INTVAL (XEXP (inner, 1)),
6423                              unsignedp, in_dest, in_compare);
6424       if (new != 0)
6425         return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
6426     }
6427
6428   inner_mode = GET_MODE (inner);
6429
6430   if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
6431     pos = INTVAL (pos_rtx), pos_rtx = 0;
6432
6433   /* See if this can be done without an extraction.  We never can if the
6434      width of the field is not the same as that of some integer mode. For
6435      registers, we can only avoid the extraction if the position is at the
6436      low-order bit and this is either not in the destination or we have the
6437      appropriate STRICT_LOW_PART operation available.
6438
6439      For MEM, we can avoid an extract if the field starts on an appropriate
6440      boundary and we can change the mode of the memory reference.  */
6441
6442   if (tmode != BLKmode
6443       && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6444            && !MEM_P (inner)
6445            && (inner_mode == tmode
6446                || !REG_P (inner)
6447                || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
6448                                          GET_MODE_BITSIZE (inner_mode))
6449                || reg_truncated_to_mode (tmode, inner))
6450            && (! in_dest
6451                || (REG_P (inner)
6452                    && have_insn_for (STRICT_LOW_PART, tmode))))
6453           || (MEM_P (inner) && pos_rtx == 0
6454               && (pos
6455                   % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6456                      : BITS_PER_UNIT)) == 0
6457               /* We can't do this if we are widening INNER_MODE (it
6458                  may not be aligned, for one thing).  */
6459               && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6460               && (inner_mode == tmode
6461                   || (! mode_dependent_address_p (XEXP (inner, 0))
6462                       && ! MEM_VOLATILE_P (inner))))))
6463     {
6464       /* If INNER is a MEM, make a new MEM that encompasses just the desired
6465          field.  If the original and current mode are the same, we need not
6466          adjust the offset.  Otherwise, we do if bytes big endian.
6467
6468          If INNER is not a MEM, get a piece consisting of just the field
6469          of interest (in this case POS % BITS_PER_WORD must be 0).  */
6470
6471       if (MEM_P (inner))
6472         {
6473           HOST_WIDE_INT offset;
6474
6475           /* POS counts from lsb, but make OFFSET count in memory order.  */
6476           if (BYTES_BIG_ENDIAN)
6477             offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6478           else
6479             offset = pos / BITS_PER_UNIT;
6480
6481           new = adjust_address_nv (inner, tmode, offset);
6482         }
6483       else if (REG_P (inner))
6484         {
6485           if (tmode != inner_mode)
6486             {
6487               /* We can't call gen_lowpart in a DEST since we
6488                  always want a SUBREG (see below) and it would sometimes
6489                  return a new hard register.  */
6490               if (pos || in_dest)
6491                 {
6492                   HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6493
6494                   if (WORDS_BIG_ENDIAN
6495                       && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6496                     final_word = ((GET_MODE_SIZE (inner_mode)
6497                                    - GET_MODE_SIZE (tmode))
6498                                   / UNITS_PER_WORD) - final_word;
6499
6500                   final_word *= UNITS_PER_WORD;
6501                   if (BYTES_BIG_ENDIAN &&
6502                       GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6503                     final_word += (GET_MODE_SIZE (inner_mode)
6504                                    - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6505
6506                   /* Avoid creating invalid subregs, for example when
6507                      simplifying (x>>32)&255.  */
6508                   if (!validate_subreg (tmode, inner_mode, inner, final_word))
6509                     return NULL_RTX;
6510
6511                   new = gen_rtx_SUBREG (tmode, inner, final_word);
6512                 }
6513               else
6514                 new = gen_lowpart (tmode, inner);
6515             }
6516           else
6517             new = inner;
6518         }
6519       else
6520         new = force_to_mode (inner, tmode,
6521                              len >= HOST_BITS_PER_WIDE_INT
6522                              ? ~(unsigned HOST_WIDE_INT) 0
6523                              : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6524                              0);
6525
6526       /* If this extraction is going into the destination of a SET,
6527          make a STRICT_LOW_PART unless we made a MEM.  */
6528
6529       if (in_dest)
6530         return (MEM_P (new) ? new
6531                 : (GET_CODE (new) != SUBREG
6532                    ? gen_rtx_CLOBBER (tmode, const0_rtx)
6533                    : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
6534
6535       if (mode == tmode)
6536         return new;
6537
6538       if (GET_CODE (new) == CONST_INT)
6539         return gen_int_mode (INTVAL (new), mode);
6540
6541       /* If we know that no extraneous bits are set, and that the high
6542          bit is not set, convert the extraction to the cheaper of
6543          sign and zero extension, that are equivalent in these cases.  */
6544       if (flag_expensive_optimizations
6545           && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6546               && ((nonzero_bits (new, tmode)
6547                    & ~(((unsigned HOST_WIDE_INT)
6548                         GET_MODE_MASK (tmode))
6549                        >> 1))
6550                   == 0)))
6551         {
6552           rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6553           rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6554
6555           /* Prefer ZERO_EXTENSION, since it gives more information to
6556              backends.  */
6557           if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6558             return temp;
6559           return temp1;
6560         }
6561
6562       /* Otherwise, sign- or zero-extend unless we already are in the
6563          proper mode.  */
6564
6565       return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6566                              mode, new));
6567     }
6568
6569   /* Unless this is a COMPARE or we have a funny memory reference,
6570      don't do anything with zero-extending field extracts starting at
6571      the low-order bit since they are simple AND operations.  */
6572   if (pos_rtx == 0 && pos == 0 && ! in_dest
6573       && ! in_compare && unsignedp)
6574     return 0;
6575
6576   /* Unless INNER is not MEM, reject this if we would be spanning bytes or
6577      if the position is not a constant and the length is not 1.  In all
6578      other cases, we would only be going outside our object in cases when
6579      an original shift would have been undefined.  */
6580   if (MEM_P (inner)
6581       && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6582           || (pos_rtx != 0 && len != 1)))
6583     return 0;
6584
6585   /* Get the mode to use should INNER not be a MEM, the mode for the position,
6586      and the mode for the result.  */
6587   if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6588     {
6589       wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6590       pos_mode = mode_for_extraction (EP_insv, 2);
6591       extraction_mode = mode_for_extraction (EP_insv, 3);
6592     }
6593
6594   if (! in_dest && unsignedp
6595       && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6596     {
6597       wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6598       pos_mode = mode_for_extraction (EP_extzv, 3);
6599       extraction_mode = mode_for_extraction (EP_extzv, 0);
6600     }
6601
6602   if (! in_dest && ! unsignedp
6603       && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6604     {
6605       wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6606       pos_mode = mode_for_extraction (EP_extv, 3);
6607       extraction_mode = mode_for_extraction (EP_extv, 0);
6608     }
6609
6610   /* Never narrow an object, since that might not be safe.  */
6611
6612   if (mode != VOIDmode
6613       && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6614     extraction_mode = mode;
6615
6616   if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6617       && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6618     pos_mode = GET_MODE (pos_rtx);
6619
6620   /* If this is not from memory, the desired mode is the preferred mode
6621      for an extraction pattern's first input operand, or word_mode if there
6622      is none.  */
6623   if (!MEM_P (inner))
6624     wanted_inner_mode = wanted_inner_reg_mode;
6625   else
6626     {
6627       /* Be careful not to go beyond the extracted object and maintain the
6628          natural alignment of the memory.  */
6629       wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
6630       while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
6631              > GET_MODE_BITSIZE (wanted_inner_mode))
6632         {
6633           wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
6634           gcc_assert (wanted_inner_mode != VOIDmode);
6635         }
6636
6637       /* If we have to change the mode of memory and cannot, the desired mode
6638          is EXTRACTION_MODE.  */
6639       if (inner_mode != wanted_inner_mode
6640           && (mode_dependent_address_p (XEXP (inner, 0))
6641               || MEM_VOLATILE_P (inner)
6642               || pos_rtx))
6643         wanted_inner_mode = extraction_mode;
6644     }
6645
6646   orig_pos = pos;
6647
6648   if (BITS_BIG_ENDIAN)
6649     {
6650       /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6651          BITS_BIG_ENDIAN style.  If position is constant, compute new
6652          position.  Otherwise, build subtraction.
6653          Note that POS is relative to the mode of the original argument.
6654          If it's a MEM we need to recompute POS relative to that.
6655          However, if we're extracting from (or inserting into) a register,
6656          we want to recompute POS relative to wanted_inner_mode.  */
6657       int width = (MEM_P (inner)
6658                    ? GET_MODE_BITSIZE (is_mode)
6659                    : GET_MODE_BITSIZE (wanted_inner_mode));
6660
6661       if (pos_rtx == 0)
6662         pos = width - len - pos;
6663       else
6664         pos_rtx
6665           = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6666       /* POS may be less than 0 now, but we check for that below.
6667          Note that it can only be less than 0 if !MEM_P (inner).  */
6668     }
6669
6670   /* If INNER has a wider mode, and this is a constant extraction, try to
6671      make it smaller and adjust the byte to point to the byte containing
6672      the value.  */
6673   if (wanted_inner_mode != VOIDmode
6674       && inner_mode != wanted_inner_mode
6675       && ! pos_rtx
6676       && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6677       && MEM_P (inner)
6678       && ! mode_dependent_address_p (XEXP (inner, 0))
6679       && ! MEM_VOLATILE_P (inner))
6680     {
6681       int offset = 0;
6682
6683       /* The computations below will be correct if the machine is big
6684          endian in both bits and bytes or little endian in bits and bytes.
6685          If it is mixed, we must adjust.  */
6686
6687       /* If bytes are big endian and we had a paradoxical SUBREG, we must
6688          adjust OFFSET to compensate.  */
6689       if (BYTES_BIG_ENDIAN
6690           && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6691         offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6692
6693       /* We can now move to the desired byte.  */
6694       offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
6695                 * GET_MODE_SIZE (wanted_inner_mode);
6696       pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6697
6698       if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6699           && is_mode != wanted_inner_mode)
6700         offset = (GET_MODE_SIZE (is_mode)
6701                   - GET_MODE_SIZE (wanted_inner_mode) - offset);
6702
6703       inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6704     }
6705
6706   /* If INNER is not memory, we can always get it into the proper mode.  If we
6707      are changing its mode, POS must be a constant and smaller than the size
6708      of the new mode.  */
6709   else if (!MEM_P (inner))
6710     {
6711       if (GET_MODE (inner) != wanted_inner_mode
6712           && (pos_rtx != 0
6713               || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6714         return 0;
6715
6716       if (orig_pos < 0)
6717         return 0;
6718
6719       inner = force_to_mode (inner, wanted_inner_mode,
6720                              pos_rtx
6721                              || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6722                              ? ~(unsigned HOST_WIDE_INT) 0
6723                              : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6724                                 << orig_pos),
6725                              0);
6726     }
6727
6728   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
6729      have to zero extend.  Otherwise, we can just use a SUBREG.  */
6730   if (pos_rtx != 0
6731       && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6732     {
6733       rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6734
6735       /* If we know that no extraneous bits are set, and that the high
6736          bit is not set, convert extraction to cheaper one - either
6737          SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6738          cases.  */
6739       if (flag_expensive_optimizations
6740           && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6741               && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6742                    & ~(((unsigned HOST_WIDE_INT)
6743                         GET_MODE_MASK (GET_MODE (pos_rtx)))
6744                        >> 1))
6745                   == 0)))
6746         {
6747           rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6748
6749           /* Prefer ZERO_EXTENSION, since it gives more information to
6750              backends.  */
6751           if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6752             temp = temp1;
6753         }
6754       pos_rtx = temp;
6755     }
6756   else if (pos_rtx != 0
6757            && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6758     pos_rtx = gen_lowpart (pos_mode, pos_rtx);
6759
6760   /* Make POS_RTX unless we already have it and it is correct.  If we don't
6761      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6762      be a CONST_INT.  */
6763   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6764     pos_rtx = orig_pos_rtx;
6765
6766   else if (pos_rtx == 0)
6767     pos_rtx = GEN_INT (pos);
6768
6769   /* Make the required operation.  See if we can use existing rtx.  */
6770   new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6771                          extraction_mode, inner, GEN_INT (len), pos_rtx);
6772   if (! in_dest)
6773     new = gen_lowpart (mode, new);
6774
6775   return new;
6776 }
6777 \f
6778 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6779    with any other operations in X.  Return X without that shift if so.  */
6780
6781 static rtx
6782 extract_left_shift (rtx x, int count)
6783 {
6784   enum rtx_code code = GET_CODE (x);
6785   enum machine_mode mode = GET_MODE (x);
6786   rtx tem;
6787
6788   switch (code)
6789     {
6790     case ASHIFT:
6791       /* This is the shift itself.  If it is wide enough, we will return
6792          either the value being shifted if the shift count is equal to
6793          COUNT or a shift for the difference.  */
6794       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6795           && INTVAL (XEXP (x, 1)) >= count)
6796         return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6797                                      INTVAL (XEXP (x, 1)) - count);
6798       break;
6799
6800     case NEG:  case NOT:
6801       if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6802         return simplify_gen_unary (code, mode, tem, mode);
6803
6804       break;
6805
6806     case PLUS:  case IOR:  case XOR:  case AND:
6807       /* If we can safely shift this constant and we find the inner shift,
6808          make a new operation.  */
6809       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6810           && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6811           && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6812         return simplify_gen_binary (code, mode, tem,
6813                                     GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6814
6815       break;
6816
6817     default:
6818       break;
6819     }
6820
6821   return 0;
6822 }
6823 \f
6824 /* Look at the expression rooted at X.  Look for expressions
6825    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6826    Form these expressions.
6827
6828    Return the new rtx, usually just X.
6829
6830    Also, for machines like the VAX that don't have logical shift insns,
6831    try to convert logical to arithmetic shift operations in cases where
6832    they are equivalent.  This undoes the canonicalizations to logical
6833    shifts done elsewhere.
6834
6835    We try, as much as possible, to re-use rtl expressions to save memory.
6836
6837    IN_CODE says what kind of expression we are processing.  Normally, it is
6838    SET.  In a memory address (inside a MEM, PLUS or minus, the latter two
6839    being kludges), it is MEM.  When processing the arguments of a comparison
6840    or a COMPARE against zero, it is COMPARE.  */
6841
6842 static rtx
6843 make_compound_operation (rtx x, enum rtx_code in_code)
6844 {
6845   enum rtx_code code = GET_CODE (x);
6846   enum machine_mode mode = GET_MODE (x);
6847   int mode_width = GET_MODE_BITSIZE (mode);
6848   rtx rhs, lhs;
6849   enum rtx_code next_code;
6850   int i;
6851   rtx new = 0;
6852   rtx tem;
6853   const char *fmt;
6854
6855   /* Select the code to be used in recursive calls.  Once we are inside an
6856      address, we stay there.  If we have a comparison, set to COMPARE,
6857      but once inside, go back to our default of SET.  */
6858
6859   next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6860                : ((code == COMPARE || COMPARISON_P (x))
6861                   && XEXP (x, 1) == const0_rtx) ? COMPARE
6862                : in_code == COMPARE ? SET : in_code);
6863
6864   /* Process depending on the code of this operation.  If NEW is set
6865      nonzero, it will be returned.  */
6866
6867   switch (code)
6868     {
6869     case ASHIFT:
6870       /* Convert shifts by constants into multiplications if inside
6871          an address.  */
6872       if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6873           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6874           && INTVAL (XEXP (x, 1)) >= 0)
6875         {
6876           new = make_compound_operation (XEXP (x, 0), next_code);
6877           new = gen_rtx_MULT (mode, new,
6878                               GEN_INT ((HOST_WIDE_INT) 1
6879                                        << INTVAL (XEXP (x, 1))));
6880         }
6881       break;
6882
6883     case AND:
6884       /* If the second operand is not a constant, we can't do anything
6885          with it.  */
6886       if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6887         break;
6888
6889       /* If the constant is a power of two minus one and the first operand
6890          is a logical right shift, make an extraction.  */
6891       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6892           && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6893         {
6894           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6895           new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6896                                  0, in_code == COMPARE);
6897         }
6898
6899       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
6900       else if (GET_CODE (XEXP (x, 0)) == SUBREG
6901                && subreg_lowpart_p (XEXP (x, 0))
6902                && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6903                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6904         {
6905           new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6906                                          next_code);
6907           new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6908                                  XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6909                                  0, in_code == COMPARE);
6910         }
6911       /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)).  */
6912       else if ((GET_CODE (XEXP (x, 0)) == XOR
6913                 || GET_CODE (XEXP (x, 0)) == IOR)
6914                && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6915                && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6916                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6917         {
6918           /* Apply the distributive law, and then try to make extractions.  */
6919           new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6920                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6921                                              XEXP (x, 1)),
6922                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6923                                              XEXP (x, 1)));
6924           new = make_compound_operation (new, in_code);
6925         }
6926
6927       /* If we are have (and (rotate X C) M) and C is larger than the number
6928          of bits in M, this is an extraction.  */
6929
6930       else if (GET_CODE (XEXP (x, 0)) == ROTATE
6931                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6932                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6933                && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6934         {
6935           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6936           new = make_extraction (mode, new,
6937                                  (GET_MODE_BITSIZE (mode)
6938                                   - INTVAL (XEXP (XEXP (x, 0), 1))),
6939                                  NULL_RTX, i, 1, 0, in_code == COMPARE);
6940         }
6941
6942       /* On machines without logical shifts, if the operand of the AND is
6943          a logical shift and our mask turns off all the propagated sign
6944          bits, we can replace the logical shift with an arithmetic shift.  */
6945       else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6946                && !have_insn_for (LSHIFTRT, mode)
6947                && have_insn_for (ASHIFTRT, mode)
6948                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6949                && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6950                && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6951                && mode_width <= HOST_BITS_PER_WIDE_INT)
6952         {
6953           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6954
6955           mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6956           if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6957             SUBST (XEXP (x, 0),
6958                    gen_rtx_ASHIFTRT (mode,
6959                                      make_compound_operation
6960                                      (XEXP (XEXP (x, 0), 0), next_code),
6961                                      XEXP (XEXP (x, 0), 1)));
6962         }
6963
6964       /* If the constant is one less than a power of two, this might be
6965          representable by an extraction even if no shift is present.
6966          If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6967          we are in a COMPARE.  */
6968       else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6969         new = make_extraction (mode,
6970                                make_compound_operation (XEXP (x, 0),
6971                                                         next_code),
6972                                0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6973
6974       /* If we are in a comparison and this is an AND with a power of two,
6975          convert this into the appropriate bit extract.  */
6976       else if (in_code == COMPARE
6977                && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6978         new = make_extraction (mode,
6979                                make_compound_operation (XEXP (x, 0),
6980                                                         next_code),
6981                                i, NULL_RTX, 1, 1, 0, 1);
6982
6983       break;
6984
6985     case LSHIFTRT:
6986       /* If the sign bit is known to be zero, replace this with an
6987          arithmetic shift.  */
6988       if (have_insn_for (ASHIFTRT, mode)
6989           && ! have_insn_for (LSHIFTRT, mode)
6990           && mode_width <= HOST_BITS_PER_WIDE_INT
6991           && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6992         {
6993           new = gen_rtx_ASHIFTRT (mode,
6994                                   make_compound_operation (XEXP (x, 0),
6995                                                            next_code),
6996                                   XEXP (x, 1));
6997           break;
6998         }
6999
7000       /* ... fall through ...  */
7001
7002     case ASHIFTRT:
7003       lhs = XEXP (x, 0);
7004       rhs = XEXP (x, 1);
7005
7006       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7007          this is a SIGN_EXTRACT.  */
7008       if (GET_CODE (rhs) == CONST_INT
7009           && GET_CODE (lhs) == ASHIFT
7010           && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7011           && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
7012         {
7013           new = make_compound_operation (XEXP (lhs, 0), next_code);
7014           new = make_extraction (mode, new,
7015                                  INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7016                                  NULL_RTX, mode_width - INTVAL (rhs),
7017                                  code == LSHIFTRT, 0, in_code == COMPARE);
7018           break;
7019         }
7020
7021       /* See if we have operations between an ASHIFTRT and an ASHIFT.
7022          If so, try to merge the shifts into a SIGN_EXTEND.  We could
7023          also do this for some cases of SIGN_EXTRACT, but it doesn't
7024          seem worth the effort; the case checked for occurs on Alpha.  */
7025
7026       if (!OBJECT_P (lhs)
7027           && ! (GET_CODE (lhs) == SUBREG
7028                 && (OBJECT_P (SUBREG_REG (lhs))))
7029           && GET_CODE (rhs) == CONST_INT
7030           && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7031           && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7032         new = make_extraction (mode, make_compound_operation (new, next_code),
7033                                0, NULL_RTX, mode_width - INTVAL (rhs),
7034                                code == LSHIFTRT, 0, in_code == COMPARE);
7035
7036       break;
7037
7038     case SUBREG:
7039       /* Call ourselves recursively on the inner expression.  If we are
7040          narrowing the object and it has a different RTL code from
7041          what it originally did, do this SUBREG as a force_to_mode.  */
7042
7043       tem = make_compound_operation (SUBREG_REG (x), in_code);
7044
7045       {
7046         rtx simplified;
7047         simplified = simplify_subreg (GET_MODE (x), tem, GET_MODE (tem),
7048                                       SUBREG_BYTE (x));
7049
7050         if (simplified)
7051           tem = simplified;
7052
7053         if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
7054             && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
7055             && subreg_lowpart_p (x))
7056           {
7057             rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
7058                                        0);
7059
7060             /* If we have something other than a SUBREG, we might have
7061                done an expansion, so rerun ourselves.  */
7062             if (GET_CODE (newer) != SUBREG)
7063               newer = make_compound_operation (newer, in_code);
7064
7065             return newer;
7066           }
7067
7068         if (simplified)
7069           return tem;
7070       }
7071       break;
7072
7073     default:
7074       break;
7075     }
7076
7077   if (new)
7078     {
7079       x = gen_lowpart (mode, new);
7080       code = GET_CODE (x);
7081     }
7082
7083   /* Now recursively process each operand of this operation.  */
7084   fmt = GET_RTX_FORMAT (code);
7085   for (i = 0; i < GET_RTX_LENGTH (code); i++)
7086     if (fmt[i] == 'e')
7087       {
7088         new = make_compound_operation (XEXP (x, i), next_code);
7089         SUBST (XEXP (x, i), new);
7090       }
7091
7092   /* If this is a commutative operation, the changes to the operands
7093      may have made it noncanonical.  */
7094   if (COMMUTATIVE_ARITH_P (x)
7095       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7096     {
7097       tem = XEXP (x, 0);
7098       SUBST (XEXP (x, 0), XEXP (x, 1));
7099       SUBST (XEXP (x, 1), tem);
7100     }
7101
7102   return x;
7103 }
7104 \f
7105 /* Given M see if it is a value that would select a field of bits
7106    within an item, but not the entire word.  Return -1 if not.
7107    Otherwise, return the starting position of the field, where 0 is the
7108    low-order bit.
7109
7110    *PLEN is set to the length of the field.  */
7111
7112 static int
7113 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7114 {
7115   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
7116   int pos = exact_log2 (m & -m);
7117   int len = 0;
7118
7119   if (pos >= 0)
7120     /* Now shift off the low-order zero bits and see if we have a
7121        power of two minus 1.  */
7122     len = exact_log2 ((m >> pos) + 1);
7123
7124   if (len <= 0)
7125     pos = -1;
7126
7127   *plen = len;
7128   return pos;
7129 }
7130 \f
7131 /* If X refers to a register that equals REG in value, replace these
7132    references with REG.  */
7133 static rtx
7134 canon_reg_for_combine (rtx x, rtx reg)
7135 {
7136   rtx op0, op1, op2;
7137   const char *fmt;
7138   int i;
7139   bool copied;
7140
7141   enum rtx_code code = GET_CODE (x);
7142   switch (GET_RTX_CLASS (code))
7143     {
7144     case RTX_UNARY:
7145       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7146       if (op0 != XEXP (x, 0))
7147         return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7148                                    GET_MODE (reg));
7149       break;
7150
7151     case RTX_BIN_ARITH:
7152     case RTX_COMM_ARITH:
7153       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7154       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7155       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7156         return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7157       break;
7158
7159     case RTX_COMPARE:
7160     case RTX_COMM_COMPARE:
7161       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7162       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7163       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7164         return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7165                                         GET_MODE (op0), op0, op1);
7166       break;
7167
7168     case RTX_TERNARY:
7169     case RTX_BITFIELD_OPS:
7170       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7171       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7172       op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7173       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7174         return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7175                                      GET_MODE (op0), op0, op1, op2);
7176
7177     case RTX_OBJ:
7178       if (REG_P (x))
7179         {
7180           if (rtx_equal_p (get_last_value (reg), x)
7181               || rtx_equal_p (reg, get_last_value (x)))
7182             return reg;
7183           else
7184             break;
7185         }
7186
7187       /* fall through */
7188
7189     default:
7190       fmt = GET_RTX_FORMAT (code);
7191       copied = false;
7192       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7193         if (fmt[i] == 'e')
7194           {
7195             rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7196             if (op != XEXP (x, i))
7197               {
7198                 if (!copied)
7199                   {
7200                     copied = true;
7201                     x = copy_rtx (x);
7202                   }
7203                 XEXP (x, i) = op;
7204               }
7205           }
7206         else if (fmt[i] == 'E')
7207           {
7208             int j;
7209             for (j = 0; j < XVECLEN (x, i); j++)
7210               {
7211                 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
7212                 if (op != XVECEXP (x, i, j))
7213                   {
7214                     if (!copied)
7215                       {
7216                         copied = true;
7217                         x = copy_rtx (x);
7218                       }
7219                     XVECEXP (x, i, j) = op;
7220                   }
7221               }
7222           }
7223
7224       break;
7225     }
7226
7227   return x;
7228 }
7229
7230 /* Return X converted to MODE.  If the value is already truncated to
7231    MODE we can just return a subreg even though in the general case we
7232    would need an explicit truncation.  */
7233
7234 static rtx
7235 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
7236 {
7237   if (GET_MODE_SIZE (GET_MODE (x)) <= GET_MODE_SIZE (mode)
7238       || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
7239                                 GET_MODE_BITSIZE (GET_MODE (x)))
7240       || (REG_P (x) && reg_truncated_to_mode (mode, x)))
7241     return gen_lowpart (mode, x);
7242   else
7243     return simplify_gen_unary (TRUNCATE, mode, x, GET_MODE (x));
7244 }
7245
7246 /* See if X can be simplified knowing that we will only refer to it in
7247    MODE and will only refer to those bits that are nonzero in MASK.
7248    If other bits are being computed or if masking operations are done
7249    that select a superset of the bits in MASK, they can sometimes be
7250    ignored.
7251
7252    Return a possibly simplified expression, but always convert X to
7253    MODE.  If X is a CONST_INT, AND the CONST_INT with MASK.
7254
7255    If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
7256    are all off in X.  This is used when X will be complemented, by either
7257    NOT, NEG, or XOR.  */
7258
7259 static rtx
7260 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
7261                int just_select)
7262 {
7263   enum rtx_code code = GET_CODE (x);
7264   int next_select = just_select || code == XOR || code == NOT || code == NEG;
7265   enum machine_mode op_mode;
7266   unsigned HOST_WIDE_INT fuller_mask, nonzero;
7267   rtx op0, op1, temp;
7268
7269   /* If this is a CALL or ASM_OPERANDS, don't do anything.  Some of the
7270      code below will do the wrong thing since the mode of such an
7271      expression is VOIDmode.
7272
7273      Also do nothing if X is a CLOBBER; this can happen if X was
7274      the return value from a call to gen_lowpart.  */
7275   if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
7276     return x;
7277
7278   /* We want to perform the operation is its present mode unless we know
7279      that the operation is valid in MODE, in which case we do the operation
7280      in MODE.  */
7281   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
7282               && have_insn_for (code, mode))
7283              ? mode : GET_MODE (x));
7284
7285   /* It is not valid to do a right-shift in a narrower mode
7286      than the one it came in with.  */
7287   if ((code == LSHIFTRT || code == ASHIFTRT)
7288       && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
7289     op_mode = GET_MODE (x);
7290
7291   /* Truncate MASK to fit OP_MODE.  */
7292   if (op_mode)
7293     mask &= GET_MODE_MASK (op_mode);
7294
7295   /* When we have an arithmetic operation, or a shift whose count we
7296      do not know, we need to assume that all bits up to the highest-order
7297      bit in MASK will be needed.  This is how we form such a mask.  */
7298   if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
7299     fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
7300   else
7301     fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
7302                    - 1);
7303
7304   /* Determine what bits of X are guaranteed to be (non)zero.  */
7305   nonzero = nonzero_bits (x, mode);
7306
7307   /* If none of the bits in X are needed, return a zero.  */
7308   if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
7309     x = const0_rtx;
7310
7311   /* If X is a CONST_INT, return a new one.  Do this here since the
7312      test below will fail.  */
7313   if (GET_CODE (x) == CONST_INT)
7314     {
7315       if (SCALAR_INT_MODE_P (mode))
7316         return gen_int_mode (INTVAL (x) & mask, mode);
7317       else
7318         {
7319           x = GEN_INT (INTVAL (x) & mask);
7320           return gen_lowpart_common (mode, x);
7321         }
7322     }
7323
7324   /* If X is narrower than MODE and we want all the bits in X's mode, just
7325      get X in the proper mode.  */
7326   if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
7327       && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
7328     return gen_lowpart (mode, x);
7329
7330   switch (code)
7331     {
7332     case CLOBBER:
7333       /* If X is a (clobber (const_int)), return it since we know we are
7334          generating something that won't match.  */
7335       return x;
7336
7337     case SIGN_EXTEND:
7338     case ZERO_EXTEND:
7339     case ZERO_EXTRACT:
7340     case SIGN_EXTRACT:
7341       x = expand_compound_operation (x);
7342       if (GET_CODE (x) != code)
7343         return force_to_mode (x, mode, mask, next_select);
7344       break;
7345
7346     case SUBREG:
7347       if (subreg_lowpart_p (x)
7348           /* We can ignore the effect of this SUBREG if it narrows the mode or
7349              if the constant masks to zero all the bits the mode doesn't
7350              have.  */
7351           && ((GET_MODE_SIZE (GET_MODE (x))
7352                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7353               || (0 == (mask
7354                         & GET_MODE_MASK (GET_MODE (x))
7355                         & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
7356         return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
7357       break;
7358
7359     case AND:
7360       /* If this is an AND with a constant, convert it into an AND
7361          whose constant is the AND of that constant with MASK.  If it
7362          remains an AND of MASK, delete it since it is redundant.  */
7363
7364       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7365         {
7366           x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
7367                                       mask & INTVAL (XEXP (x, 1)));
7368
7369           /* If X is still an AND, see if it is an AND with a mask that
7370              is just some low-order bits.  If so, and it is MASK, we don't
7371              need it.  */
7372
7373           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
7374               && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
7375                   == mask))
7376             x = XEXP (x, 0);
7377
7378           /* If it remains an AND, try making another AND with the bits
7379              in the mode mask that aren't in MASK turned on.  If the
7380              constant in the AND is wide enough, this might make a
7381              cheaper constant.  */
7382
7383           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
7384               && GET_MODE_MASK (GET_MODE (x)) != mask
7385               && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
7386             {
7387               HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
7388                                     | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
7389               int width = GET_MODE_BITSIZE (GET_MODE (x));
7390               rtx y;
7391
7392               /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
7393                  number, sign extend it.  */
7394               if (width > 0 && width < HOST_BITS_PER_WIDE_INT
7395                   && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7396                 cval |= (HOST_WIDE_INT) -1 << width;
7397
7398               y = simplify_gen_binary (AND, GET_MODE (x),
7399                                        XEXP (x, 0), GEN_INT (cval));
7400               if (rtx_cost (y, SET) < rtx_cost (x, SET))
7401                 x = y;
7402             }
7403
7404           break;
7405         }
7406
7407       goto binop;
7408
7409     case PLUS:
7410       /* In (and (plus FOO C1) M), if M is a mask that just turns off
7411          low-order bits (as in an alignment operation) and FOO is already
7412          aligned to that boundary, mask C1 to that boundary as well.
7413          This may eliminate that PLUS and, later, the AND.  */
7414
7415       {
7416         unsigned int width = GET_MODE_BITSIZE (mode);
7417         unsigned HOST_WIDE_INT smask = mask;
7418
7419         /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
7420            number, sign extend it.  */
7421
7422         if (width < HOST_BITS_PER_WIDE_INT
7423             && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7424           smask |= (HOST_WIDE_INT) -1 << width;
7425
7426         if (GET_CODE (XEXP (x, 1)) == CONST_INT
7427             && exact_log2 (- smask) >= 0
7428             && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
7429             && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
7430           return force_to_mode (plus_constant (XEXP (x, 0),
7431                                                (INTVAL (XEXP (x, 1)) & smask)),
7432                                 mode, smask, next_select);
7433       }
7434
7435       /* ... fall through ...  */
7436
7437     case MULT:
7438       /* For PLUS, MINUS and MULT, we need any bits less significant than the
7439          most significant bit in MASK since carries from those bits will
7440          affect the bits we are interested in.  */
7441       mask = fuller_mask;
7442       goto binop;
7443
7444     case MINUS:
7445       /* If X is (minus C Y) where C's least set bit is larger than any bit
7446          in the mask, then we may replace with (neg Y).  */
7447       if (GET_CODE (XEXP (x, 0)) == CONST_INT
7448           && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
7449                                         & -INTVAL (XEXP (x, 0))))
7450               > mask))
7451         {
7452           x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
7453                                   GET_MODE (x));
7454           return force_to_mode (x, mode, mask, next_select);
7455         }
7456
7457       /* Similarly, if C contains every bit in the fuller_mask, then we may
7458          replace with (not Y).  */
7459       if (GET_CODE (XEXP (x, 0)) == CONST_INT
7460           && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
7461               == INTVAL (XEXP (x, 0))))
7462         {
7463           x = simplify_gen_unary (NOT, GET_MODE (x),
7464                                   XEXP (x, 1), GET_MODE (x));
7465           return force_to_mode (x, mode, mask, next_select);
7466         }
7467
7468       mask = fuller_mask;
7469       goto binop;
7470
7471     case IOR:
7472     case XOR:
7473       /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
7474          LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
7475          operation which may be a bitfield extraction.  Ensure that the
7476          constant we form is not wider than the mode of X.  */
7477
7478       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7479           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7480           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7481           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7482           && GET_CODE (XEXP (x, 1)) == CONST_INT
7483           && ((INTVAL (XEXP (XEXP (x, 0), 1))
7484                + floor_log2 (INTVAL (XEXP (x, 1))))
7485               < GET_MODE_BITSIZE (GET_MODE (x)))
7486           && (INTVAL (XEXP (x, 1))
7487               & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
7488         {
7489           temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
7490                           << INTVAL (XEXP (XEXP (x, 0), 1)));
7491           temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
7492                                       XEXP (XEXP (x, 0), 0), temp);
7493           x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
7494                                    XEXP (XEXP (x, 0), 1));
7495           return force_to_mode (x, mode, mask, next_select);
7496         }
7497
7498     binop:
7499       /* For most binary operations, just propagate into the operation and
7500          change the mode if we have an operation of that mode.  */
7501
7502       op0 = gen_lowpart_or_truncate (op_mode,
7503                                      force_to_mode (XEXP (x, 0), mode, mask,
7504                                                     next_select));
7505       op1 = gen_lowpart_or_truncate (op_mode,
7506                                      force_to_mode (XEXP (x, 1), mode, mask,
7507                                         next_select));
7508
7509       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7510         x = simplify_gen_binary (code, op_mode, op0, op1);
7511       break;
7512
7513     case ASHIFT:
7514       /* For left shifts, do the same, but just for the first operand.
7515          However, we cannot do anything with shifts where we cannot
7516          guarantee that the counts are smaller than the size of the mode
7517          because such a count will have a different meaning in a
7518          wider mode.  */
7519
7520       if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
7521              && INTVAL (XEXP (x, 1)) >= 0
7522              && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7523           && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7524                 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7525                     < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7526         break;
7527
7528       /* If the shift count is a constant and we can do arithmetic in
7529          the mode of the shift, refine which bits we need.  Otherwise, use the
7530          conservative form of the mask.  */
7531       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7532           && INTVAL (XEXP (x, 1)) >= 0
7533           && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7534           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7535         mask >>= INTVAL (XEXP (x, 1));
7536       else
7537         mask = fuller_mask;
7538
7539       op0 = gen_lowpart_or_truncate (op_mode,
7540                                      force_to_mode (XEXP (x, 0), op_mode,
7541                                                     mask, next_select));
7542
7543       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7544         x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
7545       break;
7546
7547     case LSHIFTRT:
7548       /* Here we can only do something if the shift count is a constant,
7549          this shift constant is valid for the host, and we can do arithmetic
7550          in OP_MODE.  */
7551
7552       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7553           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7554           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7555         {
7556           rtx inner = XEXP (x, 0);
7557           unsigned HOST_WIDE_INT inner_mask;
7558
7559           /* Select the mask of the bits we need for the shift operand.  */
7560           inner_mask = mask << INTVAL (XEXP (x, 1));
7561
7562           /* We can only change the mode of the shift if we can do arithmetic
7563              in the mode of the shift and INNER_MASK is no wider than the
7564              width of X's mode.  */
7565           if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
7566             op_mode = GET_MODE (x);
7567
7568           inner = force_to_mode (inner, op_mode, inner_mask, next_select);
7569
7570           if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7571             x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7572         }
7573
7574       /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7575          shift and AND produces only copies of the sign bit (C2 is one less
7576          than a power of two), we can do this with just a shift.  */
7577
7578       if (GET_CODE (x) == LSHIFTRT
7579           && GET_CODE (XEXP (x, 1)) == CONST_INT
7580           /* The shift puts one of the sign bit copies in the least significant
7581              bit.  */
7582           && ((INTVAL (XEXP (x, 1))
7583                + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7584               >= GET_MODE_BITSIZE (GET_MODE (x)))
7585           && exact_log2 (mask + 1) >= 0
7586           /* Number of bits left after the shift must be more than the mask
7587              needs.  */
7588           && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7589               <= GET_MODE_BITSIZE (GET_MODE (x)))
7590           /* Must be more sign bit copies than the mask needs.  */
7591           && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7592               >= exact_log2 (mask + 1)))
7593         x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7594                                  GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7595                                           - exact_log2 (mask + 1)));
7596
7597       goto shiftrt;
7598
7599     case ASHIFTRT:
7600       /* If we are just looking for the sign bit, we don't need this shift at
7601          all, even if it has a variable count.  */
7602       if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7603           && (mask == ((unsigned HOST_WIDE_INT) 1
7604                        << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7605         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7606
7607       /* If this is a shift by a constant, get a mask that contains those bits
7608          that are not copies of the sign bit.  We then have two cases:  If
7609          MASK only includes those bits, this can be a logical shift, which may
7610          allow simplifications.  If MASK is a single-bit field not within
7611          those bits, we are requesting a copy of the sign bit and hence can
7612          shift the sign bit to the appropriate location.  */
7613
7614       if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7615           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7616         {
7617           int i;
7618
7619           /* If the considered data is wider than HOST_WIDE_INT, we can't
7620              represent a mask for all its bits in a single scalar.
7621              But we only care about the lower bits, so calculate these.  */
7622
7623           if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7624             {
7625               nonzero = ~(HOST_WIDE_INT) 0;
7626
7627               /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7628                  is the number of bits a full-width mask would have set.
7629                  We need only shift if these are fewer than nonzero can
7630                  hold.  If not, we must keep all bits set in nonzero.  */
7631
7632               if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7633                   < HOST_BITS_PER_WIDE_INT)
7634                 nonzero >>= INTVAL (XEXP (x, 1))
7635                             + HOST_BITS_PER_WIDE_INT
7636                             - GET_MODE_BITSIZE (GET_MODE (x)) ;
7637             }
7638           else
7639             {
7640               nonzero = GET_MODE_MASK (GET_MODE (x));
7641               nonzero >>= INTVAL (XEXP (x, 1));
7642             }
7643
7644           if ((mask & ~nonzero) == 0)
7645             {
7646               x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
7647                                         XEXP (x, 0), INTVAL (XEXP (x, 1)));
7648               if (GET_CODE (x) != ASHIFTRT)
7649                 return force_to_mode (x, mode, mask, next_select);
7650             }
7651
7652           else if ((i = exact_log2 (mask)) >= 0)
7653             {
7654               x = simplify_shift_const
7655                   (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7656                    GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7657
7658               if (GET_CODE (x) != ASHIFTRT)
7659                 return force_to_mode (x, mode, mask, next_select);
7660             }
7661         }
7662
7663       /* If MASK is 1, convert this to an LSHIFTRT.  This can be done
7664          even if the shift count isn't a constant.  */
7665       if (mask == 1)
7666         x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7667                                  XEXP (x, 0), XEXP (x, 1));
7668
7669     shiftrt:
7670
7671       /* If this is a zero- or sign-extension operation that just affects bits
7672          we don't care about, remove it.  Be sure the call above returned
7673          something that is still a shift.  */
7674
7675       if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7676           && GET_CODE (XEXP (x, 1)) == CONST_INT
7677           && INTVAL (XEXP (x, 1)) >= 0
7678           && (INTVAL (XEXP (x, 1))
7679               <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7680           && GET_CODE (XEXP (x, 0)) == ASHIFT
7681           && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7682         return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7683                               next_select);
7684
7685       break;
7686
7687     case ROTATE:
7688     case ROTATERT:
7689       /* If the shift count is constant and we can do computations
7690          in the mode of X, compute where the bits we care about are.
7691          Otherwise, we can't do anything.  Don't change the mode of
7692          the shift or propagate MODE into the shift, though.  */
7693       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7694           && INTVAL (XEXP (x, 1)) >= 0)
7695         {
7696           temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7697                                             GET_MODE (x), GEN_INT (mask),
7698                                             XEXP (x, 1));
7699           if (temp && GET_CODE (temp) == CONST_INT)
7700             SUBST (XEXP (x, 0),
7701                    force_to_mode (XEXP (x, 0), GET_MODE (x),
7702                                   INTVAL (temp), next_select));
7703         }
7704       break;
7705
7706     case NEG:
7707       /* If we just want the low-order bit, the NEG isn't needed since it
7708          won't change the low-order bit.  */
7709       if (mask == 1)
7710         return force_to_mode (XEXP (x, 0), mode, mask, just_select);
7711
7712       /* We need any bits less significant than the most significant bit in
7713          MASK since carries from those bits will affect the bits we are
7714          interested in.  */
7715       mask = fuller_mask;
7716       goto unop;
7717
7718     case NOT:
7719       /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7720          same as the XOR case above.  Ensure that the constant we form is not
7721          wider than the mode of X.  */
7722
7723       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7724           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7725           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7726           && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7727               < GET_MODE_BITSIZE (GET_MODE (x)))
7728           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7729         {
7730           temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7731                                GET_MODE (x));
7732           temp = simplify_gen_binary (XOR, GET_MODE (x),
7733                                       XEXP (XEXP (x, 0), 0), temp);
7734           x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7735                                    temp, XEXP (XEXP (x, 0), 1));
7736
7737           return force_to_mode (x, mode, mask, next_select);
7738         }
7739
7740       /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7741          use the full mask inside the NOT.  */
7742       mask = fuller_mask;
7743
7744     unop:
7745       op0 = gen_lowpart_or_truncate (op_mode,
7746                                      force_to_mode (XEXP (x, 0), mode, mask,
7747                                                     next_select));
7748       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7749         x = simplify_gen_unary (code, op_mode, op0, op_mode);
7750       break;
7751
7752     case NE:
7753       /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7754          in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7755          which is equal to STORE_FLAG_VALUE.  */
7756       if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7757           && GET_MODE (XEXP (x, 0)) == mode
7758           && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7759           && (nonzero_bits (XEXP (x, 0), mode)
7760               == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7761         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7762
7763       break;
7764
7765     case IF_THEN_ELSE:
7766       /* We have no way of knowing if the IF_THEN_ELSE can itself be
7767          written in a narrower mode.  We play it safe and do not do so.  */
7768
7769       SUBST (XEXP (x, 1),
7770              gen_lowpart_or_truncate (GET_MODE (x),
7771                                       force_to_mode (XEXP (x, 1), mode,
7772                                                      mask, next_select)));
7773       SUBST (XEXP (x, 2),
7774              gen_lowpart_or_truncate (GET_MODE (x),
7775                                       force_to_mode (XEXP (x, 2), mode,
7776                                                      mask, next_select)));
7777       break;
7778
7779     default:
7780       break;
7781     }
7782
7783   /* Ensure we return a value of the proper mode.  */
7784   return gen_lowpart_or_truncate (mode, x);
7785 }
7786 \f
7787 /* Return nonzero if X is an expression that has one of two values depending on
7788    whether some other value is zero or nonzero.  In that case, we return the
7789    value that is being tested, *PTRUE is set to the value if the rtx being
7790    returned has a nonzero value, and *PFALSE is set to the other alternative.
7791
7792    If we return zero, we set *PTRUE and *PFALSE to X.  */
7793
7794 static rtx
7795 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
7796 {
7797   enum machine_mode mode = GET_MODE (x);
7798   enum rtx_code code = GET_CODE (x);
7799   rtx cond0, cond1, true0, true1, false0, false1;
7800   unsigned HOST_WIDE_INT nz;
7801
7802   /* If we are comparing a value against zero, we are done.  */
7803   if ((code == NE || code == EQ)
7804       && XEXP (x, 1) == const0_rtx)
7805     {
7806       *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7807       *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7808       return XEXP (x, 0);
7809     }
7810
7811   /* If this is a unary operation whose operand has one of two values, apply
7812      our opcode to compute those values.  */
7813   else if (UNARY_P (x)
7814            && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7815     {
7816       *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7817       *pfalse = simplify_gen_unary (code, mode, false0,
7818                                     GET_MODE (XEXP (x, 0)));
7819       return cond0;
7820     }
7821
7822   /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7823      make can't possibly match and would suppress other optimizations.  */
7824   else if (code == COMPARE)
7825     ;
7826
7827   /* If this is a binary operation, see if either side has only one of two
7828      values.  If either one does or if both do and they are conditional on
7829      the same value, compute the new true and false values.  */
7830   else if (BINARY_P (x))
7831     {
7832       cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7833       cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7834
7835       if ((cond0 != 0 || cond1 != 0)
7836           && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7837         {
7838           /* If if_then_else_cond returned zero, then true/false are the
7839              same rtl.  We must copy one of them to prevent invalid rtl
7840              sharing.  */
7841           if (cond0 == 0)
7842             true0 = copy_rtx (true0);
7843           else if (cond1 == 0)
7844             true1 = copy_rtx (true1);
7845
7846           if (COMPARISON_P (x))
7847             {
7848               *ptrue = simplify_gen_relational (code, mode, VOIDmode,
7849                                                 true0, true1);
7850               *pfalse = simplify_gen_relational (code, mode, VOIDmode,
7851                                                  false0, false1);
7852              }
7853           else
7854             {
7855               *ptrue = simplify_gen_binary (code, mode, true0, true1);
7856               *pfalse = simplify_gen_binary (code, mode, false0, false1);
7857             }
7858
7859           return cond0 ? cond0 : cond1;
7860         }
7861
7862       /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7863          operands is zero when the other is nonzero, and vice-versa,
7864          and STORE_FLAG_VALUE is 1 or -1.  */
7865
7866       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7867           && (code == PLUS || code == IOR || code == XOR || code == MINUS
7868               || code == UMAX)
7869           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7870         {
7871           rtx op0 = XEXP (XEXP (x, 0), 1);
7872           rtx op1 = XEXP (XEXP (x, 1), 1);
7873
7874           cond0 = XEXP (XEXP (x, 0), 0);
7875           cond1 = XEXP (XEXP (x, 1), 0);
7876
7877           if (COMPARISON_P (cond0)
7878               && COMPARISON_P (cond1)
7879               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7880                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7881                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7882                   || ((swap_condition (GET_CODE (cond0))
7883                        == reversed_comparison_code (cond1, NULL))
7884                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7885                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7886               && ! side_effects_p (x))
7887             {
7888               *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
7889               *pfalse = simplify_gen_binary (MULT, mode,
7890                                              (code == MINUS
7891                                               ? simplify_gen_unary (NEG, mode,
7892                                                                     op1, mode)
7893                                               : op1),
7894                                               const_true_rtx);
7895               return cond0;
7896             }
7897         }
7898
7899       /* Similarly for MULT, AND and UMIN, except that for these the result
7900          is always zero.  */
7901       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7902           && (code == MULT || code == AND || code == UMIN)
7903           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7904         {
7905           cond0 = XEXP (XEXP (x, 0), 0);
7906           cond1 = XEXP (XEXP (x, 1), 0);
7907
7908           if (COMPARISON_P (cond0)
7909               && COMPARISON_P (cond1)
7910               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7911                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7912                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7913                   || ((swap_condition (GET_CODE (cond0))
7914                        == reversed_comparison_code (cond1, NULL))
7915                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7916                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7917               && ! side_effects_p (x))
7918             {
7919               *ptrue = *pfalse = const0_rtx;
7920               return cond0;
7921             }
7922         }
7923     }
7924
7925   else if (code == IF_THEN_ELSE)
7926     {
7927       /* If we have IF_THEN_ELSE already, extract the condition and
7928          canonicalize it if it is NE or EQ.  */
7929       cond0 = XEXP (x, 0);
7930       *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7931       if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7932         return XEXP (cond0, 0);
7933       else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7934         {
7935           *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7936           return XEXP (cond0, 0);
7937         }
7938       else
7939         return cond0;
7940     }
7941
7942   /* If X is a SUBREG, we can narrow both the true and false values
7943      if the inner expression, if there is a condition.  */
7944   else if (code == SUBREG
7945            && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7946                                                &true0, &false0)))
7947     {
7948       true0 = simplify_gen_subreg (mode, true0,
7949                                    GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7950       false0 = simplify_gen_subreg (mode, false0,
7951                                     GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7952       if (true0 && false0)
7953         {
7954           *ptrue = true0;
7955           *pfalse = false0;
7956           return cond0;
7957         }
7958     }
7959
7960   /* If X is a constant, this isn't special and will cause confusions
7961      if we treat it as such.  Likewise if it is equivalent to a constant.  */
7962   else if (CONSTANT_P (x)
7963            || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7964     ;
7965
7966   /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7967      will be least confusing to the rest of the compiler.  */
7968   else if (mode == BImode)
7969     {
7970       *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7971       return x;
7972     }
7973
7974   /* If X is known to be either 0 or -1, those are the true and
7975      false values when testing X.  */
7976   else if (x == constm1_rtx || x == const0_rtx
7977            || (mode != VOIDmode
7978                && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7979     {
7980       *ptrue = constm1_rtx, *pfalse = const0_rtx;
7981       return x;
7982     }
7983
7984   /* Likewise for 0 or a single bit.  */
7985   else if (SCALAR_INT_MODE_P (mode)
7986            && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7987            && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7988     {
7989       *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7990       return x;
7991     }
7992
7993   /* Otherwise fail; show no condition with true and false values the same.  */
7994   *ptrue = *pfalse = x;
7995   return 0;
7996 }
7997 \f
7998 /* Return the value of expression X given the fact that condition COND
7999    is known to be true when applied to REG as its first operand and VAL
8000    as its second.  X is known to not be shared and so can be modified in
8001    place.
8002
8003    We only handle the simplest cases, and specifically those cases that
8004    arise with IF_THEN_ELSE expressions.  */
8005
8006 static rtx
8007 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8008 {
8009   enum rtx_code code = GET_CODE (x);
8010   rtx temp;
8011   const char *fmt;
8012   int i, j;
8013
8014   if (side_effects_p (x))
8015     return x;
8016
8017   /* If either operand of the condition is a floating point value,
8018      then we have to avoid collapsing an EQ comparison.  */
8019   if (cond == EQ
8020       && rtx_equal_p (x, reg)
8021       && ! FLOAT_MODE_P (GET_MODE (x))
8022       && ! FLOAT_MODE_P (GET_MODE (val)))
8023     return val;
8024
8025   if (cond == UNEQ && rtx_equal_p (x, reg))
8026     return val;
8027
8028   /* If X is (abs REG) and we know something about REG's relationship
8029      with zero, we may be able to simplify this.  */
8030
8031   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8032     switch (cond)
8033       {
8034       case GE:  case GT:  case EQ:
8035         return XEXP (x, 0);
8036       case LT:  case LE:
8037         return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8038                                    XEXP (x, 0),
8039                                    GET_MODE (XEXP (x, 0)));
8040       default:
8041         break;
8042       }
8043
8044   /* The only other cases we handle are MIN, MAX, and comparisons if the
8045      operands are the same as REG and VAL.  */
8046
8047   else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8048     {
8049       if (rtx_equal_p (XEXP (x, 0), val))
8050         cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8051
8052       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8053         {
8054           if (COMPARISON_P (x))
8055             {
8056               if (comparison_dominates_p (cond, code))
8057                 return const_true_rtx;
8058
8059               code = reversed_comparison_code (x, NULL);
8060               if (code != UNKNOWN
8061                   && comparison_dominates_p (cond, code))
8062                 return const0_rtx;
8063               else
8064                 return x;
8065             }
8066           else if (code == SMAX || code == SMIN
8067                    || code == UMIN || code == UMAX)
8068             {
8069               int unsignedp = (code == UMIN || code == UMAX);
8070
8071               /* Do not reverse the condition when it is NE or EQ.
8072                  This is because we cannot conclude anything about
8073                  the value of 'SMAX (x, y)' when x is not equal to y,
8074                  but we can when x equals y.  */
8075               if ((code == SMAX || code == UMAX)
8076                   && ! (cond == EQ || cond == NE))
8077                 cond = reverse_condition (cond);
8078
8079               switch (cond)
8080                 {
8081                 case GE:   case GT:
8082                   return unsignedp ? x : XEXP (x, 1);
8083                 case LE:   case LT:
8084                   return unsignedp ? x : XEXP (x, 0);
8085                 case GEU:  case GTU:
8086                   return unsignedp ? XEXP (x, 1) : x;
8087                 case LEU:  case LTU:
8088                   return unsignedp ? XEXP (x, 0) : x;
8089                 default:
8090                   break;
8091                 }
8092             }
8093         }
8094     }
8095   else if (code == SUBREG)
8096     {
8097       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8098       rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
8099
8100       if (SUBREG_REG (x) != r)
8101         {
8102           /* We must simplify subreg here, before we lose track of the
8103              original inner_mode.  */
8104           new = simplify_subreg (GET_MODE (x), r,
8105                                  inner_mode, SUBREG_BYTE (x));
8106           if (new)
8107             return new;
8108           else
8109             SUBST (SUBREG_REG (x), r);
8110         }
8111
8112       return x;
8113     }
8114   /* We don't have to handle SIGN_EXTEND here, because even in the
8115      case of replacing something with a modeless CONST_INT, a
8116      CONST_INT is already (supposed to be) a valid sign extension for
8117      its narrower mode, which implies it's already properly
8118      sign-extended for the wider mode.  Now, for ZERO_EXTEND, the
8119      story is different.  */
8120   else if (code == ZERO_EXTEND)
8121     {
8122       enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8123       rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
8124
8125       if (XEXP (x, 0) != r)
8126         {
8127           /* We must simplify the zero_extend here, before we lose
8128              track of the original inner_mode.  */
8129           new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8130                                           r, inner_mode);
8131           if (new)
8132             return new;
8133           else
8134             SUBST (XEXP (x, 0), r);
8135         }
8136
8137       return x;
8138     }
8139
8140   fmt = GET_RTX_FORMAT (code);
8141   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8142     {
8143       if (fmt[i] == 'e')
8144         SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8145       else if (fmt[i] == 'E')
8146         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8147           SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8148                                                 cond, reg, val));
8149     }
8150
8151   return x;
8152 }
8153 \f
8154 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8155    assignment as a field assignment.  */
8156
8157 static int
8158 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8159 {
8160   if (x == y || rtx_equal_p (x, y))
8161     return 1;
8162
8163   if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8164     return 0;
8165
8166   /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8167      Note that all SUBREGs of MEM are paradoxical; otherwise they
8168      would have been rewritten.  */
8169   if (MEM_P (x) && GET_CODE (y) == SUBREG
8170       && MEM_P (SUBREG_REG (y))
8171       && rtx_equal_p (SUBREG_REG (y),
8172                       gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8173     return 1;
8174
8175   if (MEM_P (y) && GET_CODE (x) == SUBREG
8176       && MEM_P (SUBREG_REG (x))
8177       && rtx_equal_p (SUBREG_REG (x),
8178                       gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8179     return 1;
8180
8181   /* We used to see if get_last_value of X and Y were the same but that's
8182      not correct.  In one direction, we'll cause the assignment to have
8183      the wrong destination and in the case, we'll import a register into this
8184      insn that might have already have been dead.   So fail if none of the
8185      above cases are true.  */
8186   return 0;
8187 }
8188 \f
8189 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8190    Return that assignment if so.
8191
8192    We only handle the most common cases.  */
8193
8194 static rtx
8195 make_field_assignment (rtx x)
8196 {
8197   rtx dest = SET_DEST (x);
8198   rtx src = SET_SRC (x);
8199   rtx assign;
8200   rtx rhs, lhs;
8201   HOST_WIDE_INT c1;
8202   HOST_WIDE_INT pos;
8203   unsigned HOST_WIDE_INT len;
8204   rtx other;
8205   enum machine_mode mode;
8206
8207   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
8208      a clear of a one-bit field.  We will have changed it to
8209      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
8210      for a SUBREG.  */
8211
8212   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
8213       && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
8214       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
8215       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8216     {
8217       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8218                                 1, 1, 1, 0);
8219       if (assign != 0)
8220         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8221       return x;
8222     }
8223
8224   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
8225       && subreg_lowpart_p (XEXP (src, 0))
8226       && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
8227           < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
8228       && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
8229       && GET_CODE (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == CONST_INT
8230       && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
8231       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8232     {
8233       assign = make_extraction (VOIDmode, dest, 0,
8234                                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
8235                                 1, 1, 1, 0);
8236       if (assign != 0)
8237         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8238       return x;
8239     }
8240
8241   /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
8242      one-bit field.  */
8243   if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
8244       && XEXP (XEXP (src, 0), 0) == const1_rtx
8245       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8246     {
8247       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8248                                 1, 1, 1, 0);
8249       if (assign != 0)
8250         return gen_rtx_SET (VOIDmode, assign, const1_rtx);
8251       return x;
8252     }
8253
8254   /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
8255      SRC is an AND with all bits of that field set, then we can discard
8256      the AND.  */
8257   if (GET_CODE (dest) == ZERO_EXTRACT
8258       && GET_CODE (XEXP (dest, 1)) == CONST_INT
8259       && GET_CODE (src) == AND
8260       && GET_CODE (XEXP (src, 1)) == CONST_INT)
8261     {
8262       HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
8263       unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
8264       unsigned HOST_WIDE_INT ze_mask;
8265
8266       if (width >= HOST_BITS_PER_WIDE_INT)
8267         ze_mask = -1;
8268       else
8269         ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
8270
8271       /* Complete overlap.  We can remove the source AND.  */
8272       if ((and_mask & ze_mask) == ze_mask)
8273         return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
8274
8275       /* Partial overlap.  We can reduce the source AND.  */
8276       if ((and_mask & ze_mask) != and_mask)
8277         {
8278           mode = GET_MODE (src);
8279           src = gen_rtx_AND (mode, XEXP (src, 0),
8280                              gen_int_mode (and_mask & ze_mask, mode));
8281           return gen_rtx_SET (VOIDmode, dest, src);
8282         }
8283     }
8284
8285   /* The other case we handle is assignments into a constant-position
8286      field.  They look like (ior/xor (and DEST C1) OTHER).  If C1 represents
8287      a mask that has all one bits except for a group of zero bits and
8288      OTHER is known to have zeros where C1 has ones, this is such an
8289      assignment.  Compute the position and length from C1.  Shift OTHER
8290      to the appropriate position, force it to the required mode, and
8291      make the extraction.  Check for the AND in both operands.  */
8292
8293   if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
8294     return x;
8295
8296   rhs = expand_compound_operation (XEXP (src, 0));
8297   lhs = expand_compound_operation (XEXP (src, 1));
8298
8299   if (GET_CODE (rhs) == AND
8300       && GET_CODE (XEXP (rhs, 1)) == CONST_INT
8301       && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
8302     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
8303   else if (GET_CODE (lhs) == AND
8304            && GET_CODE (XEXP (lhs, 1)) == CONST_INT
8305            && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
8306     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
8307   else
8308     return x;
8309
8310   pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
8311   if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
8312       || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
8313       || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
8314     return x;
8315
8316   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
8317   if (assign == 0)
8318     return x;
8319
8320   /* The mode to use for the source is the mode of the assignment, or of
8321      what is inside a possible STRICT_LOW_PART.  */
8322   mode = (GET_CODE (assign) == STRICT_LOW_PART
8323           ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
8324
8325   /* Shift OTHER right POS places and make it the source, restricting it
8326      to the proper length and mode.  */
8327
8328   src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
8329                                                      GET_MODE (src),
8330                                                      other, pos),
8331                                dest);
8332   src = force_to_mode (src, mode,
8333                        GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
8334                        ? ~(unsigned HOST_WIDE_INT) 0
8335                        : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
8336                        0);
8337
8338   /* If SRC is masked by an AND that does not make a difference in
8339      the value being stored, strip it.  */
8340   if (GET_CODE (assign) == ZERO_EXTRACT
8341       && GET_CODE (XEXP (assign, 1)) == CONST_INT
8342       && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
8343       && GET_CODE (src) == AND
8344       && GET_CODE (XEXP (src, 1)) == CONST_INT
8345       && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
8346           == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
8347     src = XEXP (src, 0);
8348
8349   return gen_rtx_SET (VOIDmode, assign, src);
8350 }
8351 \f
8352 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
8353    if so.  */
8354
8355 static rtx
8356 apply_distributive_law (rtx x)
8357 {
8358   enum rtx_code code = GET_CODE (x);
8359   enum rtx_code inner_code;
8360   rtx lhs, rhs, other;
8361   rtx tem;
8362
8363   /* Distributivity is not true for floating point as it can change the
8364      value.  So we don't do it unless -funsafe-math-optimizations.  */
8365   if (FLOAT_MODE_P (GET_MODE (x))
8366       && ! flag_unsafe_math_optimizations)
8367     return x;
8368
8369   /* The outer operation can only be one of the following:  */
8370   if (code != IOR && code != AND && code != XOR
8371       && code != PLUS && code != MINUS)
8372     return x;
8373
8374   lhs = XEXP (x, 0);
8375   rhs = XEXP (x, 1);
8376
8377   /* If either operand is a primitive we can't do anything, so get out
8378      fast.  */
8379   if (OBJECT_P (lhs) || OBJECT_P (rhs))
8380     return x;
8381
8382   lhs = expand_compound_operation (lhs);
8383   rhs = expand_compound_operation (rhs);
8384   inner_code = GET_CODE (lhs);
8385   if (inner_code != GET_CODE (rhs))
8386     return x;
8387
8388   /* See if the inner and outer operations distribute.  */
8389   switch (inner_code)
8390     {
8391     case LSHIFTRT:
8392     case ASHIFTRT:
8393     case AND:
8394     case IOR:
8395       /* These all distribute except over PLUS.  */
8396       if (code == PLUS || code == MINUS)
8397         return x;
8398       break;
8399
8400     case MULT:
8401       if (code != PLUS && code != MINUS)
8402         return x;
8403       break;
8404
8405     case ASHIFT:
8406       /* This is also a multiply, so it distributes over everything.  */
8407       break;
8408
8409     case SUBREG:
8410       /* Non-paradoxical SUBREGs distributes over all operations,
8411          provided the inner modes and byte offsets are the same, this
8412          is an extraction of a low-order part, we don't convert an fp
8413          operation to int or vice versa, this is not a vector mode,
8414          and we would not be converting a single-word operation into a
8415          multi-word operation.  The latter test is not required, but
8416          it prevents generating unneeded multi-word operations.  Some
8417          of the previous tests are redundant given the latter test,
8418          but are retained because they are required for correctness.
8419
8420          We produce the result slightly differently in this case.  */
8421
8422       if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
8423           || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
8424           || ! subreg_lowpart_p (lhs)
8425           || (GET_MODE_CLASS (GET_MODE (lhs))
8426               != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
8427           || (GET_MODE_SIZE (GET_MODE (lhs))
8428               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
8429           || VECTOR_MODE_P (GET_MODE (lhs))
8430           || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD
8431           /* Result might need to be truncated.  Don't change mode if
8432              explicit truncation is needed.  */
8433           || !TRULY_NOOP_TRUNCATION
8434                (GET_MODE_BITSIZE (GET_MODE (x)),
8435                 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (lhs)))))
8436         return x;
8437
8438       tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
8439                                  SUBREG_REG (lhs), SUBREG_REG (rhs));
8440       return gen_lowpart (GET_MODE (x), tem);
8441
8442     default:
8443       return x;
8444     }
8445
8446   /* Set LHS and RHS to the inner operands (A and B in the example
8447      above) and set OTHER to the common operand (C in the example).
8448      There is only one way to do this unless the inner operation is
8449      commutative.  */
8450   if (COMMUTATIVE_ARITH_P (lhs)
8451       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
8452     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
8453   else if (COMMUTATIVE_ARITH_P (lhs)
8454            && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
8455     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
8456   else if (COMMUTATIVE_ARITH_P (lhs)
8457            && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
8458     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
8459   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
8460     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
8461   else
8462     return x;
8463
8464   /* Form the new inner operation, seeing if it simplifies first.  */
8465   tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
8466
8467   /* There is one exception to the general way of distributing:
8468      (a | c) ^ (b | c) -> (a ^ b) & ~c  */
8469   if (code == XOR && inner_code == IOR)
8470     {
8471       inner_code = AND;
8472       other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
8473     }
8474
8475   /* We may be able to continuing distributing the result, so call
8476      ourselves recursively on the inner operation before forming the
8477      outer operation, which we return.  */
8478   return simplify_gen_binary (inner_code, GET_MODE (x),
8479                               apply_distributive_law (tem), other);
8480 }
8481
8482 /* See if X is of the form (* (+ A B) C), and if so convert to
8483    (+ (* A C) (* B C)) and try to simplify.
8484
8485    Most of the time, this results in no change.  However, if some of
8486    the operands are the same or inverses of each other, simplifications
8487    will result.
8488
8489    For example, (and (ior A B) (not B)) can occur as the result of
8490    expanding a bit field assignment.  When we apply the distributive
8491    law to this, we get (ior (and (A (not B))) (and (B (not B)))),
8492    which then simplifies to (and (A (not B))).
8493
8494    Note that no checks happen on the validity of applying the inverse
8495    distributive law.  This is pointless since we can do it in the
8496    few places where this routine is called.
8497
8498    N is the index of the term that is decomposed (the arithmetic operation,
8499    i.e. (+ A B) in the first example above).  !N is the index of the term that
8500    is distributed, i.e. of C in the first example above.  */
8501 static rtx
8502 distribute_and_simplify_rtx (rtx x, int n)
8503 {
8504   enum machine_mode mode;
8505   enum rtx_code outer_code, inner_code;
8506   rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
8507
8508   decomposed = XEXP (x, n);
8509   if (!ARITHMETIC_P (decomposed))
8510     return NULL_RTX;
8511
8512   mode = GET_MODE (x);
8513   outer_code = GET_CODE (x);
8514   distributed = XEXP (x, !n);
8515
8516   inner_code = GET_CODE (decomposed);
8517   inner_op0 = XEXP (decomposed, 0);
8518   inner_op1 = XEXP (decomposed, 1);
8519
8520   /* Special case (and (xor B C) (not A)), which is equivalent to
8521      (xor (ior A B) (ior A C))  */
8522   if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
8523     {
8524       distributed = XEXP (distributed, 0);
8525       outer_code = IOR;
8526     }
8527
8528   if (n == 0)
8529     {
8530       /* Distribute the second term.  */
8531       new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
8532       new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
8533     }
8534   else
8535     {
8536       /* Distribute the first term.  */
8537       new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
8538       new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
8539     }
8540
8541   tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
8542                                                      new_op0, new_op1));
8543   if (GET_CODE (tmp) != outer_code
8544       && rtx_cost (tmp, SET) < rtx_cost (x, SET))
8545     return tmp;
8546
8547   return NULL_RTX;
8548 }
8549 \f
8550 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
8551    in MODE.  Return an equivalent form, if different from (and VAROP
8552    (const_int CONSTOP)).  Otherwise, return NULL_RTX.  */
8553
8554 static rtx
8555 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
8556                           unsigned HOST_WIDE_INT constop)
8557 {
8558   unsigned HOST_WIDE_INT nonzero;
8559   unsigned HOST_WIDE_INT orig_constop;
8560   rtx orig_varop;
8561   int i;
8562
8563   orig_varop = varop;
8564   orig_constop = constop;
8565   if (GET_CODE (varop) == CLOBBER)
8566     return NULL_RTX;
8567
8568   /* Simplify VAROP knowing that we will be only looking at some of the
8569      bits in it.
8570
8571      Note by passing in CONSTOP, we guarantee that the bits not set in
8572      CONSTOP are not significant and will never be examined.  We must
8573      ensure that is the case by explicitly masking out those bits
8574      before returning.  */
8575   varop = force_to_mode (varop, mode, constop, 0);
8576
8577   /* If VAROP is a CLOBBER, we will fail so return it.  */
8578   if (GET_CODE (varop) == CLOBBER)
8579     return varop;
8580
8581   /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
8582      to VAROP and return the new constant.  */
8583   if (GET_CODE (varop) == CONST_INT)
8584     return gen_int_mode (INTVAL (varop) & constop, mode);
8585
8586   /* See what bits may be nonzero in VAROP.  Unlike the general case of
8587      a call to nonzero_bits, here we don't care about bits outside
8588      MODE.  */
8589
8590   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
8591
8592   /* Turn off all bits in the constant that are known to already be zero.
8593      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
8594      which is tested below.  */
8595
8596   constop &= nonzero;
8597
8598   /* If we don't have any bits left, return zero.  */
8599   if (constop == 0)
8600     return const0_rtx;
8601
8602   /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
8603      a power of two, we can replace this with an ASHIFT.  */
8604   if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
8605       && (i = exact_log2 (constop)) >= 0)
8606     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
8607
8608   /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8609      or XOR, then try to apply the distributive law.  This may eliminate
8610      operations if either branch can be simplified because of the AND.
8611      It may also make some cases more complex, but those cases probably
8612      won't match a pattern either with or without this.  */
8613
8614   if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
8615     return
8616       gen_lowpart
8617         (mode,
8618          apply_distributive_law
8619          (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
8620                                simplify_and_const_int (NULL_RTX,
8621                                                        GET_MODE (varop),
8622                                                        XEXP (varop, 0),
8623                                                        constop),
8624                                simplify_and_const_int (NULL_RTX,
8625                                                        GET_MODE (varop),
8626                                                        XEXP (varop, 1),
8627                                                        constop))));
8628
8629   /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
8630      the AND and see if one of the operands simplifies to zero.  If so, we
8631      may eliminate it.  */
8632
8633   if (GET_CODE (varop) == PLUS
8634       && exact_log2 (constop + 1) >= 0)
8635     {
8636       rtx o0, o1;
8637
8638       o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
8639       o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
8640       if (o0 == const0_rtx)
8641         return o1;
8642       if (o1 == const0_rtx)
8643         return o0;
8644     }
8645
8646   /* Make a SUBREG if necessary.  If we can't make it, fail.  */
8647   varop = gen_lowpart (mode, varop);
8648   if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
8649     return NULL_RTX;
8650
8651   /* If we are only masking insignificant bits, return VAROP.  */
8652   if (constop == nonzero)
8653     return varop;
8654
8655   if (varop == orig_varop && constop == orig_constop)
8656     return NULL_RTX;
8657
8658   /* Otherwise, return an AND.  */
8659   return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
8660 }
8661
8662
8663 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
8664    in MODE.
8665
8666    Return an equivalent form, if different from X.  Otherwise, return X.  If
8667    X is zero, we are to always construct the equivalent form.  */
8668
8669 static rtx
8670 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
8671                         unsigned HOST_WIDE_INT constop)
8672 {
8673   rtx tem = simplify_and_const_int_1 (mode, varop, constop);
8674   if (tem)
8675     return tem;
8676
8677   if (!x)
8678     x = simplify_gen_binary (AND, GET_MODE (varop), varop,
8679                              gen_int_mode (constop, mode));
8680   if (GET_MODE (x) != mode)
8681     x = gen_lowpart (mode, x);
8682   return x;
8683 }
8684 \f
8685 /* Given a REG, X, compute which bits in X can be nonzero.
8686    We don't care about bits outside of those defined in MODE.
8687
8688    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8689    a shift, AND, or zero_extract, we can do better.  */
8690
8691 static rtx
8692 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
8693                               const_rtx known_x ATTRIBUTE_UNUSED,
8694                               enum machine_mode known_mode ATTRIBUTE_UNUSED,
8695                               unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
8696                               unsigned HOST_WIDE_INT *nonzero)
8697 {
8698   rtx tem;
8699   reg_stat_type *rsp;
8700
8701   /* If X is a register whose nonzero bits value is current, use it.
8702      Otherwise, if X is a register whose value we can find, use that
8703      value.  Otherwise, use the previously-computed global nonzero bits
8704      for this register.  */
8705
8706   rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
8707   if (rsp->last_set_value != 0
8708       && (rsp->last_set_mode == mode
8709           || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
8710               && GET_MODE_CLASS (mode) == MODE_INT))
8711       && ((rsp->last_set_label >= label_tick_ebb_start
8712            && rsp->last_set_label < label_tick)
8713           || (rsp->last_set_label == label_tick
8714               && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
8715           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8716               && REG_N_SETS (REGNO (x)) == 1
8717               && !REGNO_REG_SET_P
8718                   (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
8719     {
8720       *nonzero &= rsp->last_set_nonzero_bits;
8721       return NULL;
8722     }
8723
8724   tem = get_last_value (x);
8725
8726   if (tem)
8727     {
8728 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8729       /* If X is narrower than MODE and TEM is a non-negative
8730          constant that would appear negative in the mode of X,
8731          sign-extend it for use in reg_nonzero_bits because some
8732          machines (maybe most) will actually do the sign-extension
8733          and this is the conservative approach.
8734
8735          ??? For 2.5, try to tighten up the MD files in this regard
8736          instead of this kludge.  */
8737
8738       if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
8739           && GET_CODE (tem) == CONST_INT
8740           && INTVAL (tem) > 0
8741           && 0 != (INTVAL (tem)
8742                    & ((HOST_WIDE_INT) 1
8743                       << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8744         tem = GEN_INT (INTVAL (tem)
8745                        | ((HOST_WIDE_INT) (-1)
8746                           << GET_MODE_BITSIZE (GET_MODE (x))));
8747 #endif
8748       return tem;
8749     }
8750   else if (nonzero_sign_valid && rsp->nonzero_bits)
8751     {
8752       unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
8753
8754       if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
8755         /* We don't know anything about the upper bits.  */
8756         mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8757       *nonzero &= mask;
8758     }
8759
8760   return NULL;
8761 }
8762
8763 /* Return the number of bits at the high-order end of X that are known to
8764    be equal to the sign bit.  X will be used in mode MODE; if MODE is
8765    VOIDmode, X will be used in its own mode.  The returned value  will always
8766    be between 1 and the number of bits in MODE.  */
8767
8768 static rtx
8769 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
8770                                      const_rtx known_x ATTRIBUTE_UNUSED,
8771                                      enum machine_mode known_mode
8772                                      ATTRIBUTE_UNUSED,
8773                                      unsigned int known_ret ATTRIBUTE_UNUSED,
8774                                      unsigned int *result)
8775 {
8776   rtx tem;
8777   reg_stat_type *rsp;
8778
8779   rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
8780   if (rsp->last_set_value != 0
8781       && rsp->last_set_mode == mode
8782       && ((rsp->last_set_label >= label_tick_ebb_start
8783            && rsp->last_set_label < label_tick)
8784           || (rsp->last_set_label == label_tick
8785               && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
8786           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8787               && REG_N_SETS (REGNO (x)) == 1
8788               && !REGNO_REG_SET_P
8789                   (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
8790     {
8791       *result = rsp->last_set_sign_bit_copies;
8792       return NULL;
8793     }
8794
8795   tem = get_last_value (x);
8796   if (tem != 0)
8797     return tem;
8798
8799   if (nonzero_sign_valid && rsp->sign_bit_copies != 0
8800       && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
8801     *result = rsp->sign_bit_copies;
8802
8803   return NULL;
8804 }
8805 \f
8806 /* Return the number of "extended" bits there are in X, when interpreted
8807    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
8808    unsigned quantities, this is the number of high-order zero bits.
8809    For signed quantities, this is the number of copies of the sign bit
8810    minus 1.  In both case, this function returns the number of "spare"
8811    bits.  For example, if two quantities for which this function returns
8812    at least 1 are added, the addition is known not to overflow.
8813
8814    This function will always return 0 unless called during combine, which
8815    implies that it must be called from a define_split.  */
8816
8817 unsigned int
8818 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
8819 {
8820   if (nonzero_sign_valid == 0)
8821     return 0;
8822
8823   return (unsignedp
8824           ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8825              ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
8826                                - floor_log2 (nonzero_bits (x, mode)))
8827              : 0)
8828           : num_sign_bit_copies (x, mode) - 1);
8829 }
8830 \f
8831 /* This function is called from `simplify_shift_const' to merge two
8832    outer operations.  Specifically, we have already found that we need
8833    to perform operation *POP0 with constant *PCONST0 at the outermost
8834    position.  We would now like to also perform OP1 with constant CONST1
8835    (with *POP0 being done last).
8836
8837    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8838    the resulting operation.  *PCOMP_P is set to 1 if we would need to
8839    complement the innermost operand, otherwise it is unchanged.
8840
8841    MODE is the mode in which the operation will be done.  No bits outside
8842    the width of this mode matter.  It is assumed that the width of this mode
8843    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8844
8845    If *POP0 or OP1 are UNKNOWN, it means no operation is required.  Only NEG, PLUS,
8846    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
8847    result is simply *PCONST0.
8848
8849    If the resulting operation cannot be expressed as one operation, we
8850    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
8851
8852 static int
8853 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)
8854 {
8855   enum rtx_code op0 = *pop0;
8856   HOST_WIDE_INT const0 = *pconst0;
8857
8858   const0 &= GET_MODE_MASK (mode);
8859   const1 &= GET_MODE_MASK (mode);
8860
8861   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
8862   if (op0 == AND)
8863     const1 &= const0;
8864
8865   /* If OP0 or OP1 is UNKNOWN, this is easy.  Similarly if they are the same or
8866      if OP0 is SET.  */
8867
8868   if (op1 == UNKNOWN || op0 == SET)
8869     return 1;
8870
8871   else if (op0 == UNKNOWN)
8872     op0 = op1, const0 = const1;
8873
8874   else if (op0 == op1)
8875     {
8876       switch (op0)
8877         {
8878         case AND:
8879           const0 &= const1;
8880           break;
8881         case IOR:
8882           const0 |= const1;
8883           break;
8884         case XOR:
8885           const0 ^= const1;
8886           break;
8887         case PLUS:
8888           const0 += const1;
8889           break;
8890         case NEG:
8891           op0 = UNKNOWN;
8892           break;
8893         default:
8894           break;
8895         }
8896     }
8897
8898   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
8899   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8900     return 0;
8901
8902   /* If the two constants aren't the same, we can't do anything.  The
8903      remaining six cases can all be done.  */
8904   else if (const0 != const1)
8905     return 0;
8906
8907   else
8908     switch (op0)
8909       {
8910       case IOR:
8911         if (op1 == AND)
8912           /* (a & b) | b == b */
8913           op0 = SET;
8914         else /* op1 == XOR */
8915           /* (a ^ b) | b == a | b */
8916           {;}
8917         break;
8918
8919       case XOR:
8920         if (op1 == AND)
8921           /* (a & b) ^ b == (~a) & b */
8922           op0 = AND, *pcomp_p = 1;
8923         else /* op1 == IOR */
8924           /* (a | b) ^ b == a & ~b */
8925           op0 = AND, const0 = ~const0;
8926         break;
8927
8928       case AND:
8929         if (op1 == IOR)
8930           /* (a | b) & b == b */
8931         op0 = SET;
8932         else /* op1 == XOR */
8933           /* (a ^ b) & b) == (~a) & b */
8934           *pcomp_p = 1;
8935         break;
8936       default:
8937         break;
8938       }
8939
8940   /* Check for NO-OP cases.  */
8941   const0 &= GET_MODE_MASK (mode);
8942   if (const0 == 0
8943       && (op0 == IOR || op0 == XOR || op0 == PLUS))
8944     op0 = UNKNOWN;
8945   else if (const0 == 0 && op0 == AND)
8946     op0 = SET;
8947   else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8948            && op0 == AND)
8949     op0 = UNKNOWN;
8950
8951   /* ??? Slightly redundant with the above mask, but not entirely.
8952      Moving this above means we'd have to sign-extend the mode mask
8953      for the final test.  */
8954   const0 = trunc_int_for_mode (const0, mode);
8955
8956   *pop0 = op0;
8957   *pconst0 = const0;
8958
8959   return 1;
8960 }
8961 \f
8962 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
8963    The result of the shift is RESULT_MODE.  Return NULL_RTX if we cannot
8964    simplify it.  Otherwise, return a simplified value.
8965
8966    The shift is normally computed in the widest mode we find in VAROP, as
8967    long as it isn't a different number of words than RESULT_MODE.  Exceptions
8968    are ASHIFTRT and ROTATE, which are always done in their original mode.  */
8969
8970 static rtx
8971 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
8972                         rtx varop, int orig_count)
8973 {
8974   enum rtx_code orig_code = code;
8975   rtx orig_varop = varop;
8976   int count;
8977   enum machine_mode mode = result_mode;
8978   enum machine_mode shift_mode, tmode;
8979   unsigned int mode_words
8980     = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8981   /* We form (outer_op (code varop count) (outer_const)).  */
8982   enum rtx_code outer_op = UNKNOWN;
8983   HOST_WIDE_INT outer_const = 0;
8984   int complement_p = 0;
8985   rtx new, x;
8986
8987   /* Make sure and truncate the "natural" shift on the way in.  We don't
8988      want to do this inside the loop as it makes it more difficult to
8989      combine shifts.  */
8990   if (SHIFT_COUNT_TRUNCATED)
8991     orig_count &= GET_MODE_BITSIZE (mode) - 1;
8992
8993   /* If we were given an invalid count, don't do anything except exactly
8994      what was requested.  */
8995
8996   if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
8997     return NULL_RTX;
8998
8999   count = orig_count;
9000
9001   /* Unless one of the branches of the `if' in this loop does a `continue',
9002      we will `break' the loop after the `if'.  */
9003
9004   while (count != 0)
9005     {
9006       /* If we have an operand of (clobber (const_int 0)), fail.  */
9007       if (GET_CODE (varop) == CLOBBER)
9008         return NULL_RTX;
9009
9010       /* If we discovered we had to complement VAROP, leave.  Making a NOT
9011          here would cause an infinite loop.  */
9012       if (complement_p)
9013         break;
9014
9015       /* Convert ROTATERT to ROTATE.  */
9016       if (code == ROTATERT)
9017         {
9018           unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
9019           code = ROTATE;
9020           if (VECTOR_MODE_P (result_mode))
9021             count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9022           else
9023             count = bitsize - count;
9024         }
9025
9026       /* We need to determine what mode we will do the shift in.  If the
9027          shift is a right shift or a ROTATE, we must always do it in the mode
9028          it was originally done in.  Otherwise, we can do it in MODE, the
9029          widest mode encountered.  */
9030       shift_mode
9031         = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9032            ? result_mode : mode);
9033
9034       /* Handle cases where the count is greater than the size of the mode
9035          minus 1.  For ASHIFT, use the size minus one as the count (this can
9036          occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
9037          take the count modulo the size.  For other shifts, the result is
9038          zero.
9039
9040          Since these shifts are being produced by the compiler by combining
9041          multiple operations, each of which are defined, we know what the
9042          result is supposed to be.  */
9043
9044       if (count > (GET_MODE_BITSIZE (shift_mode) - 1))
9045         {
9046           if (code == ASHIFTRT)
9047             count = GET_MODE_BITSIZE (shift_mode) - 1;
9048           else if (code == ROTATE || code == ROTATERT)
9049             count %= GET_MODE_BITSIZE (shift_mode);
9050           else
9051             {
9052               /* We can't simply return zero because there may be an
9053                  outer op.  */
9054               varop = const0_rtx;
9055               count = 0;
9056               break;
9057             }
9058         }
9059
9060       /* An arithmetic right shift of a quantity known to be -1 or 0
9061          is a no-op.  */
9062       if (code == ASHIFTRT
9063           && (num_sign_bit_copies (varop, shift_mode)
9064               == GET_MODE_BITSIZE (shift_mode)))
9065         {
9066           count = 0;
9067           break;
9068         }
9069
9070       /* If we are doing an arithmetic right shift and discarding all but
9071          the sign bit copies, this is equivalent to doing a shift by the
9072          bitsize minus one.  Convert it into that shift because it will often
9073          allow other simplifications.  */
9074
9075       if (code == ASHIFTRT
9076           && (count + num_sign_bit_copies (varop, shift_mode)
9077               >= GET_MODE_BITSIZE (shift_mode)))
9078         count = GET_MODE_BITSIZE (shift_mode) - 1;
9079
9080       /* We simplify the tests below and elsewhere by converting
9081          ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9082          `make_compound_operation' will convert it to an ASHIFTRT for
9083          those machines (such as VAX) that don't have an LSHIFTRT.  */
9084       if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9085           && code == ASHIFTRT
9086           && ((nonzero_bits (varop, shift_mode)
9087                & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
9088               == 0))
9089         code = LSHIFTRT;
9090
9091       if (((code == LSHIFTRT
9092             && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9093             && !(nonzero_bits (varop, shift_mode) >> count))
9094            || (code == ASHIFT
9095                && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9096                && !((nonzero_bits (varop, shift_mode) << count)
9097                     & GET_MODE_MASK (shift_mode))))
9098           && !side_effects_p (varop))
9099         varop = const0_rtx;
9100
9101       switch (GET_CODE (varop))
9102         {
9103         case SIGN_EXTEND:
9104         case ZERO_EXTEND:
9105         case SIGN_EXTRACT:
9106         case ZERO_EXTRACT:
9107           new = expand_compound_operation (varop);
9108           if (new != varop)
9109             {
9110               varop = new;
9111               continue;
9112             }
9113           break;
9114
9115         case MEM:
9116           /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9117              minus the width of a smaller mode, we can do this with a
9118              SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
9119           if ((code == ASHIFTRT || code == LSHIFTRT)
9120               && ! mode_dependent_address_p (XEXP (varop, 0))
9121               && ! MEM_VOLATILE_P (varop)
9122               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9123                                          MODE_INT, 1)) != BLKmode)
9124             {
9125               new = adjust_address_nv (varop, tmode,
9126                                        BYTES_BIG_ENDIAN ? 0
9127                                        : count / BITS_PER_UNIT);
9128
9129               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9130                                      : ZERO_EXTEND, mode, new);
9131               count = 0;
9132               continue;
9133             }
9134           break;
9135
9136         case SUBREG:
9137           /* If VAROP is a SUBREG, strip it as long as the inner operand has
9138              the same number of words as what we've seen so far.  Then store
9139              the widest mode in MODE.  */
9140           if (subreg_lowpart_p (varop)
9141               && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9142                   > GET_MODE_SIZE (GET_MODE (varop)))
9143               && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9144                                   + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9145                  == mode_words)
9146             {
9147               varop = SUBREG_REG (varop);
9148               if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9149                 mode = GET_MODE (varop);
9150               continue;
9151             }
9152           break;
9153
9154         case MULT:
9155           /* Some machines use MULT instead of ASHIFT because MULT
9156              is cheaper.  But it is still better on those machines to
9157              merge two shifts into one.  */
9158           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9159               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9160             {
9161               varop
9162                 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
9163                                        XEXP (varop, 0),
9164                                        GEN_INT (exact_log2 (
9165                                                 INTVAL (XEXP (varop, 1)))));
9166               continue;
9167             }
9168           break;
9169
9170         case UDIV:
9171           /* Similar, for when divides are cheaper.  */
9172           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9173               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9174             {
9175               varop
9176                 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
9177                                        XEXP (varop, 0),
9178                                        GEN_INT (exact_log2 (
9179                                                 INTVAL (XEXP (varop, 1)))));
9180               continue;
9181             }
9182           break;
9183
9184         case ASHIFTRT:
9185           /* If we are extracting just the sign bit of an arithmetic
9186              right shift, that shift is not needed.  However, the sign
9187              bit of a wider mode may be different from what would be
9188              interpreted as the sign bit in a narrower mode, so, if
9189              the result is narrower, don't discard the shift.  */
9190           if (code == LSHIFTRT
9191               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9192               && (GET_MODE_BITSIZE (result_mode)
9193                   >= GET_MODE_BITSIZE (GET_MODE (varop))))
9194             {
9195               varop = XEXP (varop, 0);
9196               continue;
9197             }
9198
9199           /* ... fall through ...  */
9200
9201         case LSHIFTRT:
9202         case ASHIFT:
9203         case ROTATE:
9204           /* Here we have two nested shifts.  The result is usually the
9205              AND of a new shift with a mask.  We compute the result below.  */
9206           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9207               && INTVAL (XEXP (varop, 1)) >= 0
9208               && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9209               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9210               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9211               && !VECTOR_MODE_P (result_mode))
9212             {
9213               enum rtx_code first_code = GET_CODE (varop);
9214               unsigned int first_count = INTVAL (XEXP (varop, 1));
9215               unsigned HOST_WIDE_INT mask;
9216               rtx mask_rtx;
9217
9218               /* We have one common special case.  We can't do any merging if
9219                  the inner code is an ASHIFTRT of a smaller mode.  However, if
9220                  we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9221                  with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9222                  we can convert it to
9223                  (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9224                  This simplifies certain SIGN_EXTEND operations.  */
9225               if (code == ASHIFT && first_code == ASHIFTRT
9226                   && count == (GET_MODE_BITSIZE (result_mode)
9227                                - GET_MODE_BITSIZE (GET_MODE (varop))))
9228                 {
9229                   /* C3 has the low-order C1 bits zero.  */
9230
9231                   mask = (GET_MODE_MASK (mode)
9232                           & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
9233
9234                   varop = simplify_and_const_int (NULL_RTX, result_mode,
9235                                                   XEXP (varop, 0), mask);
9236                   varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
9237                                                 varop, count);
9238                   count = first_count;
9239                   code = ASHIFTRT;
9240                   continue;
9241                 }
9242
9243               /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
9244                  than C1 high-order bits equal to the sign bit, we can convert
9245                  this to either an ASHIFT or an ASHIFTRT depending on the
9246                  two counts.
9247
9248                  We cannot do this if VAROP's mode is not SHIFT_MODE.  */
9249
9250               if (code == ASHIFTRT && first_code == ASHIFT
9251                   && GET_MODE (varop) == shift_mode
9252                   && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
9253                       > first_count))
9254                 {
9255                   varop = XEXP (varop, 0);
9256                   count -= first_count;
9257                   if (count < 0)
9258                     {
9259                       count = -count;
9260                       code = ASHIFT;
9261                     }
9262
9263                   continue;
9264                 }
9265
9266               /* There are some cases we can't do.  If CODE is ASHIFTRT,
9267                  we can only do this if FIRST_CODE is also ASHIFTRT.
9268
9269                  We can't do the case when CODE is ROTATE and FIRST_CODE is
9270                  ASHIFTRT.
9271
9272                  If the mode of this shift is not the mode of the outer shift,
9273                  we can't do this if either shift is a right shift or ROTATE.
9274
9275                  Finally, we can't do any of these if the mode is too wide
9276                  unless the codes are the same.
9277
9278                  Handle the case where the shift codes are the same
9279                  first.  */
9280
9281               if (code == first_code)
9282                 {
9283                   if (GET_MODE (varop) != result_mode
9284                       && (code == ASHIFTRT || code == LSHIFTRT
9285                           || code == ROTATE))
9286                     break;
9287
9288                   count += first_count;
9289                   varop = XEXP (varop, 0);
9290                   continue;
9291                 }
9292
9293               if (code == ASHIFTRT
9294                   || (code == ROTATE && first_code == ASHIFTRT)
9295                   || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
9296                   || (GET_MODE (varop) != result_mode
9297                       && (first_code == ASHIFTRT || first_code == LSHIFTRT
9298                           || first_code == ROTATE
9299                           || code == ROTATE)))
9300                 break;
9301
9302               /* To compute the mask to apply after the shift, shift the
9303                  nonzero bits of the inner shift the same way the
9304                  outer shift will.  */
9305
9306               mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
9307
9308               mask_rtx
9309                 = simplify_const_binary_operation (code, result_mode, mask_rtx,
9310                                                    GEN_INT (count));
9311
9312               /* Give up if we can't compute an outer operation to use.  */
9313               if (mask_rtx == 0
9314                   || GET_CODE (mask_rtx) != CONST_INT
9315                   || ! merge_outer_ops (&outer_op, &outer_const, AND,
9316                                         INTVAL (mask_rtx),
9317                                         result_mode, &complement_p))
9318                 break;
9319
9320               /* If the shifts are in the same direction, we add the
9321                  counts.  Otherwise, we subtract them.  */
9322               if ((code == ASHIFTRT || code == LSHIFTRT)
9323                   == (first_code == ASHIFTRT || first_code == LSHIFTRT))
9324                 count += first_count;
9325               else
9326                 count -= first_count;
9327
9328               /* If COUNT is positive, the new shift is usually CODE,
9329                  except for the two exceptions below, in which case it is
9330                  FIRST_CODE.  If the count is negative, FIRST_CODE should
9331                  always be used  */
9332               if (count > 0
9333                   && ((first_code == ROTATE && code == ASHIFT)
9334                       || (first_code == ASHIFTRT && code == LSHIFTRT)))
9335                 code = first_code;
9336               else if (count < 0)
9337                 code = first_code, count = -count;
9338
9339               varop = XEXP (varop, 0);
9340               continue;
9341             }
9342
9343           /* If we have (A << B << C) for any shift, we can convert this to
9344              (A << C << B).  This wins if A is a constant.  Only try this if
9345              B is not a constant.  */
9346
9347           else if (GET_CODE (varop) == code
9348                    && GET_CODE (XEXP (varop, 0)) == CONST_INT
9349                    && GET_CODE (XEXP (varop, 1)) != CONST_INT)
9350             {
9351               rtx new = simplify_const_binary_operation (code, mode,
9352                                                          XEXP (varop, 0),
9353                                                          GEN_INT (count));
9354               varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
9355               count = 0;
9356               continue;
9357             }
9358           break;
9359
9360         case NOT:
9361           if (VECTOR_MODE_P (mode))
9362             break;
9363
9364           /* Make this fit the case below.  */
9365           varop = gen_rtx_XOR (mode, XEXP (varop, 0),
9366                                GEN_INT (GET_MODE_MASK (mode)));
9367           continue;
9368
9369         case IOR:
9370         case AND:
9371         case XOR:
9372           /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9373              with C the size of VAROP - 1 and the shift is logical if
9374              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9375              we have an (le X 0) operation.   If we have an arithmetic shift
9376              and STORE_FLAG_VALUE is 1 or we have a logical shift with
9377              STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
9378
9379           if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
9380               && XEXP (XEXP (varop, 0), 1) == constm1_rtx
9381               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9382               && (code == LSHIFTRT || code == ASHIFTRT)
9383               && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9384               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9385             {
9386               count = 0;
9387               varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
9388                                   const0_rtx);
9389
9390               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9391                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9392
9393               continue;
9394             }
9395
9396           /* If we have (shift (logical)), move the logical to the outside
9397              to allow it to possibly combine with another logical and the
9398              shift to combine with another shift.  This also canonicalizes to
9399              what a ZERO_EXTRACT looks like.  Also, some machines have
9400              (and (shift)) insns.  */
9401
9402           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9403               /* We can't do this if we have (ashiftrt (xor))  and the
9404                  constant has its sign bit set in shift_mode.  */
9405               && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9406                    && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9407                                               shift_mode))
9408               && (new = simplify_const_binary_operation (code, result_mode,
9409                                                          XEXP (varop, 1),
9410                                                          GEN_INT (count))) != 0
9411               && GET_CODE (new) == CONST_INT
9412               && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9413                                   INTVAL (new), result_mode, &complement_p))
9414             {
9415               varop = XEXP (varop, 0);
9416               continue;
9417             }
9418
9419           /* If we can't do that, try to simplify the shift in each arm of the
9420              logical expression, make a new logical expression, and apply
9421              the inverse distributive law.  This also can't be done
9422              for some (ashiftrt (xor)).  */
9423           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9424              && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9425                   && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9426                                              shift_mode)))
9427             {
9428               rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9429                                               XEXP (varop, 0), count);
9430               rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9431                                               XEXP (varop, 1), count);
9432
9433               varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
9434                                            lhs, rhs);
9435               varop = apply_distributive_law (varop);
9436
9437               count = 0;
9438               continue;
9439             }
9440           break;
9441
9442         case EQ:
9443           /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9444              says that the sign bit can be tested, FOO has mode MODE, C is
9445              GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9446              that may be nonzero.  */
9447           if (code == LSHIFTRT
9448               && XEXP (varop, 1) == const0_rtx
9449               && GET_MODE (XEXP (varop, 0)) == result_mode
9450               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9451               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9452               && STORE_FLAG_VALUE == -1
9453               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9454               && merge_outer_ops (&outer_op, &outer_const, XOR,
9455                                   (HOST_WIDE_INT) 1, result_mode,
9456                                   &complement_p))
9457             {
9458               varop = XEXP (varop, 0);
9459               count = 0;
9460               continue;
9461             }
9462           break;
9463
9464         case NEG:
9465           /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9466              than the number of bits in the mode is equivalent to A.  */
9467           if (code == LSHIFTRT
9468               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9469               && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9470             {
9471               varop = XEXP (varop, 0);
9472               count = 0;
9473               continue;
9474             }
9475
9476           /* NEG commutes with ASHIFT since it is multiplication.  Move the
9477              NEG outside to allow shifts to combine.  */
9478           if (code == ASHIFT
9479               && merge_outer_ops (&outer_op, &outer_const, NEG,
9480                                   (HOST_WIDE_INT) 0, result_mode,
9481                                   &complement_p))
9482             {
9483               varop = XEXP (varop, 0);
9484               continue;
9485             }
9486           break;
9487
9488         case PLUS:
9489           /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9490              is one less than the number of bits in the mode is
9491              equivalent to (xor A 1).  */
9492           if (code == LSHIFTRT
9493               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9494               && XEXP (varop, 1) == constm1_rtx
9495               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9496               && merge_outer_ops (&outer_op, &outer_const, XOR,
9497                                   (HOST_WIDE_INT) 1, result_mode,
9498                                   &complement_p))
9499             {
9500               count = 0;
9501               varop = XEXP (varop, 0);
9502               continue;
9503             }
9504
9505           /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9506              that might be nonzero in BAR are those being shifted out and those
9507              bits are known zero in FOO, we can replace the PLUS with FOO.
9508              Similarly in the other operand order.  This code occurs when
9509              we are computing the size of a variable-size array.  */
9510
9511           if ((code == ASHIFTRT || code == LSHIFTRT)
9512               && count < HOST_BITS_PER_WIDE_INT
9513               && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9514               && (nonzero_bits (XEXP (varop, 1), result_mode)
9515                   & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9516             {
9517               varop = XEXP (varop, 0);
9518               continue;
9519             }
9520           else if ((code == ASHIFTRT || code == LSHIFTRT)
9521                    && count < HOST_BITS_PER_WIDE_INT
9522                    && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9523                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9524                             >> count)
9525                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9526                             & nonzero_bits (XEXP (varop, 1),
9527                                                  result_mode)))
9528             {
9529               varop = XEXP (varop, 1);
9530               continue;
9531             }
9532
9533           /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
9534           if (code == ASHIFT
9535               && GET_CODE (XEXP (varop, 1)) == CONST_INT
9536               && (new = simplify_const_binary_operation (ASHIFT, result_mode,
9537                                                          XEXP (varop, 1),
9538                                                          GEN_INT (count))) != 0
9539               && GET_CODE (new) == CONST_INT
9540               && merge_outer_ops (&outer_op, &outer_const, PLUS,
9541                                   INTVAL (new), result_mode, &complement_p))
9542             {
9543               varop = XEXP (varop, 0);
9544               continue;
9545             }
9546
9547           /* Check for 'PLUS signbit', which is the canonical form of 'XOR
9548              signbit', and attempt to change the PLUS to an XOR and move it to
9549              the outer operation as is done above in the AND/IOR/XOR case
9550              leg for shift(logical). See details in logical handling above
9551              for reasoning in doing so.  */
9552           if (code == LSHIFTRT
9553               && GET_CODE (XEXP (varop, 1)) == CONST_INT
9554               && mode_signbit_p (result_mode, XEXP (varop, 1))
9555               && (new = simplify_const_binary_operation (code, result_mode,
9556                                                          XEXP (varop, 1),
9557                                                          GEN_INT (count))) != 0
9558               && GET_CODE (new) == CONST_INT
9559               && merge_outer_ops (&outer_op, &outer_const, XOR,
9560                                   INTVAL (new), result_mode, &complement_p))
9561             {
9562               varop = XEXP (varop, 0);
9563               continue;
9564             }
9565
9566           break;
9567
9568         case MINUS:
9569           /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9570              with C the size of VAROP - 1 and the shift is logical if
9571              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9572              we have a (gt X 0) operation.  If the shift is arithmetic with
9573              STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9574              we have a (neg (gt X 0)) operation.  */
9575
9576           if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9577               && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9578               && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9579               && (code == LSHIFTRT || code == ASHIFTRT)
9580               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9581               && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
9582               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9583             {
9584               count = 0;
9585               varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9586                                   const0_rtx);
9587
9588               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9589                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9590
9591               continue;
9592             }
9593           break;
9594
9595         case TRUNCATE:
9596           /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9597              if the truncate does not affect the value.  */
9598           if (code == LSHIFTRT
9599               && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9600               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9601               && (INTVAL (XEXP (XEXP (varop, 0), 1))
9602                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9603                       - GET_MODE_BITSIZE (GET_MODE (varop)))))
9604             {
9605               rtx varop_inner = XEXP (varop, 0);
9606
9607               varop_inner
9608                 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9609                                     XEXP (varop_inner, 0),
9610                                     GEN_INT
9611                                     (count + INTVAL (XEXP (varop_inner, 1))));
9612               varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9613               count = 0;
9614               continue;
9615             }
9616           break;
9617
9618         default:
9619           break;
9620         }
9621
9622       break;
9623     }
9624
9625   /* We need to determine what mode to do the shift in.  If the shift is
9626      a right shift or ROTATE, we must always do it in the mode it was
9627      originally done in.  Otherwise, we can do it in MODE, the widest mode
9628      encountered.  The code we care about is that of the shift that will
9629      actually be done, not the shift that was originally requested.  */
9630   shift_mode
9631     = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9632        ? result_mode : mode);
9633
9634   /* We have now finished analyzing the shift.  The result should be
9635      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
9636      OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
9637      to the result of the shift.  OUTER_CONST is the relevant constant,
9638      but we must turn off all bits turned off in the shift.  */
9639
9640   if (outer_op == UNKNOWN
9641       && orig_code == code && orig_count == count
9642       && varop == orig_varop
9643       && shift_mode == GET_MODE (varop))
9644     return NULL_RTX;
9645
9646   /* Make a SUBREG if necessary.  If we can't make it, fail.  */
9647   varop = gen_lowpart (shift_mode, varop);
9648   if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9649     return NULL_RTX;
9650
9651   /* If we have an outer operation and we just made a shift, it is
9652      possible that we could have simplified the shift were it not
9653      for the outer operation.  So try to do the simplification
9654      recursively.  */
9655
9656   if (outer_op != UNKNOWN)
9657     x = simplify_shift_const_1 (code, shift_mode, varop, count);
9658   else
9659     x = NULL_RTX;
9660
9661   if (x == NULL_RTX)
9662     x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
9663
9664   /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9665      turn off all the bits that the shift would have turned off.  */
9666   if (orig_code == LSHIFTRT && result_mode != shift_mode)
9667     x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9668                                 GET_MODE_MASK (result_mode) >> orig_count);
9669
9670   /* Do the remainder of the processing in RESULT_MODE.  */
9671   x = gen_lowpart_or_truncate (result_mode, x);
9672
9673   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9674      operation.  */
9675   if (complement_p)
9676     x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9677
9678   if (outer_op != UNKNOWN)
9679     {
9680       if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9681         outer_const = trunc_int_for_mode (outer_const, result_mode);
9682
9683       if (outer_op == AND)
9684         x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9685       else if (outer_op == SET)
9686         {
9687           /* This means that we have determined that the result is
9688              equivalent to a constant.  This should be rare.  */
9689           if (!side_effects_p (x))
9690             x = GEN_INT (outer_const);
9691         }
9692       else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
9693         x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
9694       else
9695         x = simplify_gen_binary (outer_op, result_mode, x,
9696                                  GEN_INT (outer_const));
9697     }
9698
9699   return x;
9700 }
9701
9702 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
9703    The result of the shift is RESULT_MODE.  If we cannot simplify it,
9704    return X or, if it is NULL, synthesize the expression with
9705    simplify_gen_binary.  Otherwise, return a simplified value.
9706
9707    The shift is normally computed in the widest mode we find in VAROP, as
9708    long as it isn't a different number of words than RESULT_MODE.  Exceptions
9709    are ASHIFTRT and ROTATE, which are always done in their original mode.  */
9710
9711 static rtx
9712 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
9713                       rtx varop, int count)
9714 {
9715   rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
9716   if (tem)
9717     return tem;
9718
9719   if (!x)
9720     x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
9721   if (GET_MODE (x) != result_mode)
9722     x = gen_lowpart (result_mode, x);
9723   return x;
9724 }
9725
9726 \f
9727 /* Like recog, but we receive the address of a pointer to a new pattern.
9728    We try to match the rtx that the pointer points to.
9729    If that fails, we may try to modify or replace the pattern,
9730    storing the replacement into the same pointer object.
9731
9732    Modifications include deletion or addition of CLOBBERs.
9733
9734    PNOTES is a pointer to a location where any REG_UNUSED notes added for
9735    the CLOBBERs are placed.
9736
9737    The value is the final insn code from the pattern ultimately matched,
9738    or -1.  */
9739
9740 static int
9741 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
9742 {
9743   rtx pat = *pnewpat;
9744   int insn_code_number;
9745   int num_clobbers_to_add = 0;
9746   int i;
9747   rtx notes = 0;
9748   rtx old_notes, old_pat;
9749
9750   /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9751      we use to indicate that something didn't match.  If we find such a
9752      thing, force rejection.  */
9753   if (GET_CODE (pat) == PARALLEL)
9754     for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9755       if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9756           && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9757         return -1;
9758
9759   old_pat = PATTERN (insn);
9760   old_notes = REG_NOTES (insn);
9761   PATTERN (insn) = pat;
9762   REG_NOTES (insn) = 0;
9763
9764   insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9765   if (dump_file && (dump_flags & TDF_DETAILS))
9766     {
9767       if (insn_code_number < 0)
9768         fputs ("Failed to match this instruction:\n", dump_file);
9769       else
9770         fputs ("Successfully matched this instruction:\n", dump_file);
9771       print_rtl_single (dump_file, pat);
9772     }
9773
9774   /* If it isn't, there is the possibility that we previously had an insn
9775      that clobbered some register as a side effect, but the combined
9776      insn doesn't need to do that.  So try once more without the clobbers
9777      unless this represents an ASM insn.  */
9778
9779   if (insn_code_number < 0 && ! check_asm_operands (pat)
9780       && GET_CODE (pat) == PARALLEL)
9781     {
9782       int pos;
9783
9784       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9785         if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9786           {
9787             if (i != pos)
9788               SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9789             pos++;
9790           }
9791
9792       SUBST_INT (XVECLEN (pat, 0), pos);
9793
9794       if (pos == 1)
9795         pat = XVECEXP (pat, 0, 0);
9796
9797       PATTERN (insn) = pat;
9798       insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9799       if (dump_file && (dump_flags & TDF_DETAILS))
9800         {
9801           if (insn_code_number < 0)
9802             fputs ("Failed to match this instruction:\n", dump_file);
9803           else
9804             fputs ("Successfully matched this instruction:\n", dump_file);
9805           print_rtl_single (dump_file, pat);
9806         }
9807     }
9808   PATTERN (insn) = old_pat;
9809   REG_NOTES (insn) = old_notes;
9810
9811   /* Recognize all noop sets, these will be killed by followup pass.  */
9812   if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
9813     insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
9814
9815   /* If we had any clobbers to add, make a new pattern than contains
9816      them.  Then check to make sure that all of them are dead.  */
9817   if (num_clobbers_to_add)
9818     {
9819       rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9820                                      rtvec_alloc (GET_CODE (pat) == PARALLEL
9821                                                   ? (XVECLEN (pat, 0)
9822                                                      + num_clobbers_to_add)
9823                                                   : num_clobbers_to_add + 1));
9824
9825       if (GET_CODE (pat) == PARALLEL)
9826         for (i = 0; i < XVECLEN (pat, 0); i++)
9827           XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9828       else
9829         XVECEXP (newpat, 0, 0) = pat;
9830
9831       add_clobbers (newpat, insn_code_number);
9832
9833       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9834            i < XVECLEN (newpat, 0); i++)
9835         {
9836           if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
9837               && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9838             return -1;
9839           if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH) 
9840             {
9841               gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
9842               notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9843                                          XEXP (XVECEXP (newpat, 0, i), 0), notes);
9844             }
9845         }
9846       pat = newpat;
9847     }
9848
9849   *pnewpat = pat;
9850   *pnotes = notes;
9851
9852   return insn_code_number;
9853 }
9854 \f
9855 /* Like gen_lowpart_general but for use by combine.  In combine it
9856    is not possible to create any new pseudoregs.  However, it is
9857    safe to create invalid memory addresses, because combine will
9858    try to recognize them and all they will do is make the combine
9859    attempt fail.
9860
9861    If for some reason this cannot do its job, an rtx
9862    (clobber (const_int 0)) is returned.
9863    An insn containing that will not be recognized.  */
9864
9865 static rtx
9866 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
9867 {
9868   enum machine_mode imode = GET_MODE (x);
9869   unsigned int osize = GET_MODE_SIZE (omode);
9870   unsigned int isize = GET_MODE_SIZE (imode);
9871   rtx result;
9872
9873   if (omode == imode)
9874     return x;
9875
9876   /* Return identity if this is a CONST or symbolic reference.  */
9877   if (omode == Pmode
9878       && (GET_CODE (x) == CONST
9879           || GET_CODE (x) == SYMBOL_REF
9880           || GET_CODE (x) == LABEL_REF))
9881     return x;
9882
9883   /* We can only support MODE being wider than a word if X is a
9884      constant integer or has a mode the same size.  */
9885   if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
9886       && ! ((imode == VOIDmode
9887              && (GET_CODE (x) == CONST_INT
9888                  || GET_CODE (x) == CONST_DOUBLE))
9889             || isize == osize))
9890     goto fail;
9891
9892   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
9893      won't know what to do.  So we will strip off the SUBREG here and
9894      process normally.  */
9895   if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
9896     {
9897       x = SUBREG_REG (x);
9898
9899       /* For use in case we fall down into the address adjustments
9900          further below, we need to adjust the known mode and size of
9901          x; imode and isize, since we just adjusted x.  */
9902       imode = GET_MODE (x);
9903
9904       if (imode == omode)
9905         return x;
9906
9907       isize = GET_MODE_SIZE (imode);
9908     }
9909
9910   result = gen_lowpart_common (omode, x);
9911
9912   if (result)
9913     return result;
9914
9915   if (MEM_P (x))
9916     {
9917       int offset = 0;
9918
9919       /* Refuse to work on a volatile memory ref or one with a mode-dependent
9920          address.  */
9921       if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9922         goto fail;
9923
9924       /* If we want to refer to something bigger than the original memref,
9925          generate a paradoxical subreg instead.  That will force a reload
9926          of the original memref X.  */
9927       if (isize < osize)
9928         return gen_rtx_SUBREG (omode, x, 0);
9929
9930       if (WORDS_BIG_ENDIAN)
9931         offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
9932
9933       /* Adjust the address so that the address-after-the-data is
9934          unchanged.  */
9935       if (BYTES_BIG_ENDIAN)
9936         offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
9937
9938       return adjust_address_nv (x, omode, offset);
9939     }
9940
9941   /* If X is a comparison operator, rewrite it in a new mode.  This
9942      probably won't match, but may allow further simplifications.  */
9943   else if (COMPARISON_P (x))
9944     return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
9945
9946   /* If we couldn't simplify X any other way, just enclose it in a
9947      SUBREG.  Normally, this SUBREG won't match, but some patterns may
9948      include an explicit SUBREG or we may simplify it further in combine.  */
9949   else
9950     {
9951       int offset = 0;
9952       rtx res;
9953
9954       offset = subreg_lowpart_offset (omode, imode);
9955       if (imode == VOIDmode)
9956         {
9957           imode = int_mode_for_mode (omode);
9958           x = gen_lowpart_common (imode, x);
9959           if (x == NULL)
9960             goto fail;
9961         }
9962       res = simplify_gen_subreg (omode, x, imode, offset);
9963       if (res)
9964         return res;
9965     }
9966
9967  fail:
9968   return gen_rtx_CLOBBER (imode, const0_rtx);
9969 }
9970 \f
9971 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9972    comparison code that will be tested.
9973
9974    The result is a possibly different comparison code to use.  *POP0 and
9975    *POP1 may be updated.
9976
9977    It is possible that we might detect that a comparison is either always
9978    true or always false.  However, we do not perform general constant
9979    folding in combine, so this knowledge isn't useful.  Such tautologies
9980    should have been detected earlier.  Hence we ignore all such cases.  */
9981
9982 static enum rtx_code
9983 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
9984 {
9985   rtx op0 = *pop0;
9986   rtx op1 = *pop1;
9987   rtx tem, tem1;
9988   int i;
9989   enum machine_mode mode, tmode;
9990
9991   /* Try a few ways of applying the same transformation to both operands.  */
9992   while (1)
9993     {
9994 #ifndef WORD_REGISTER_OPERATIONS
9995       /* The test below this one won't handle SIGN_EXTENDs on these machines,
9996          so check specially.  */
9997       if (code != GTU && code != GEU && code != LTU && code != LEU
9998           && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9999           && GET_CODE (XEXP (op0, 0)) == ASHIFT
10000           && GET_CODE (XEXP (op1, 0)) == ASHIFT
10001           && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
10002           && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
10003           && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
10004               == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
10005           && GET_CODE (XEXP (op0, 1)) == CONST_INT
10006           && XEXP (op0, 1) == XEXP (op1, 1)
10007           && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10008           && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
10009           && (INTVAL (XEXP (op0, 1))
10010               == (GET_MODE_BITSIZE (GET_MODE (op0))
10011                   - (GET_MODE_BITSIZE
10012                      (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
10013         {
10014           op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
10015           op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
10016         }
10017 #endif
10018
10019       /* If both operands are the same constant shift, see if we can ignore the
10020          shift.  We can if the shift is a rotate or if the bits shifted out of
10021          this shift are known to be zero for both inputs and if the type of
10022          comparison is compatible with the shift.  */
10023       if (GET_CODE (op0) == GET_CODE (op1)
10024           && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10025           && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
10026               || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
10027                   && (code != GT && code != LT && code != GE && code != LE))
10028               || (GET_CODE (op0) == ASHIFTRT
10029                   && (code != GTU && code != LTU
10030                       && code != GEU && code != LEU)))
10031           && GET_CODE (XEXP (op0, 1)) == CONST_INT
10032           && INTVAL (XEXP (op0, 1)) >= 0
10033           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10034           && XEXP (op0, 1) == XEXP (op1, 1))
10035         {
10036           enum machine_mode mode = GET_MODE (op0);
10037           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10038           int shift_count = INTVAL (XEXP (op0, 1));
10039
10040           if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
10041             mask &= (mask >> shift_count) << shift_count;
10042           else if (GET_CODE (op0) == ASHIFT)
10043             mask = (mask & (mask << shift_count)) >> shift_count;
10044
10045           if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
10046               && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
10047             op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
10048           else
10049             break;
10050         }
10051
10052       /* If both operands are AND's of a paradoxical SUBREG by constant, the
10053          SUBREGs are of the same mode, and, in both cases, the AND would
10054          be redundant if the comparison was done in the narrower mode,
10055          do the comparison in the narrower mode (e.g., we are AND'ing with 1
10056          and the operand's possibly nonzero bits are 0xffffff01; in that case
10057          if we only care about QImode, we don't need the AND).  This case
10058          occurs if the output mode of an scc insn is not SImode and
10059          STORE_FLAG_VALUE == 1 (e.g., the 386).
10060
10061          Similarly, check for a case where the AND's are ZERO_EXTEND
10062          operations from some narrower mode even though a SUBREG is not
10063          present.  */
10064
10065       else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
10066                && GET_CODE (XEXP (op0, 1)) == CONST_INT
10067                && GET_CODE (XEXP (op1, 1)) == CONST_INT)
10068         {
10069           rtx inner_op0 = XEXP (op0, 0);
10070           rtx inner_op1 = XEXP (op1, 0);
10071           HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
10072           HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
10073           int changed = 0;
10074
10075           if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
10076               && (GET_MODE_SIZE (GET_MODE (inner_op0))
10077                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
10078               && (GET_MODE (SUBREG_REG (inner_op0))
10079                   == GET_MODE (SUBREG_REG (inner_op1)))
10080               && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
10081                   <= HOST_BITS_PER_WIDE_INT)
10082               && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
10083                                              GET_MODE (SUBREG_REG (inner_op0)))))
10084               && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
10085                                              GET_MODE (SUBREG_REG (inner_op1))))))
10086             {
10087               op0 = SUBREG_REG (inner_op0);
10088               op1 = SUBREG_REG (inner_op1);
10089
10090               /* The resulting comparison is always unsigned since we masked
10091                  off the original sign bit.  */
10092               code = unsigned_condition (code);
10093
10094               changed = 1;
10095             }
10096
10097           else if (c0 == c1)
10098             for (tmode = GET_CLASS_NARROWEST_MODE
10099                  (GET_MODE_CLASS (GET_MODE (op0)));
10100                  tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10101               if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10102                 {
10103                   op0 = gen_lowpart (tmode, inner_op0);
10104                   op1 = gen_lowpart (tmode, inner_op1);
10105                   code = unsigned_condition (code);
10106                   changed = 1;
10107                   break;
10108                 }
10109
10110           if (! changed)
10111             break;
10112         }
10113
10114       /* If both operands are NOT, we can strip off the outer operation
10115          and adjust the comparison code for swapped operands; similarly for
10116          NEG, except that this must be an equality comparison.  */
10117       else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10118                || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10119                    && (code == EQ || code == NE)))
10120         op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10121
10122       else
10123         break;
10124     }
10125
10126   /* If the first operand is a constant, swap the operands and adjust the
10127      comparison code appropriately, but don't do this if the second operand
10128      is already a constant integer.  */
10129   if (swap_commutative_operands_p (op0, op1))
10130     {
10131       tem = op0, op0 = op1, op1 = tem;
10132       code = swap_condition (code);
10133     }
10134
10135   /* We now enter a loop during which we will try to simplify the comparison.
10136      For the most part, we only are concerned with comparisons with zero,
10137      but some things may really be comparisons with zero but not start
10138      out looking that way.  */
10139
10140   while (GET_CODE (op1) == CONST_INT)
10141     {
10142       enum machine_mode mode = GET_MODE (op0);
10143       unsigned int mode_width = GET_MODE_BITSIZE (mode);
10144       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10145       int equality_comparison_p;
10146       int sign_bit_comparison_p;
10147       int unsigned_comparison_p;
10148       HOST_WIDE_INT const_op;
10149
10150       /* We only want to handle integral modes.  This catches VOIDmode,
10151          CCmode, and the floating-point modes.  An exception is that we
10152          can handle VOIDmode if OP0 is a COMPARE or a comparison
10153          operation.  */
10154
10155       if (GET_MODE_CLASS (mode) != MODE_INT
10156           && ! (mode == VOIDmode
10157                 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
10158         break;
10159
10160       /* Get the constant we are comparing against and turn off all bits
10161          not on in our mode.  */
10162       const_op = INTVAL (op1);
10163       if (mode != VOIDmode)
10164         const_op = trunc_int_for_mode (const_op, mode);
10165       op1 = GEN_INT (const_op);
10166
10167       /* If we are comparing against a constant power of two and the value
10168          being compared can only have that single bit nonzero (e.g., it was
10169          `and'ed with that bit), we can replace this with a comparison
10170          with zero.  */
10171       if (const_op
10172           && (code == EQ || code == NE || code == GE || code == GEU
10173               || code == LT || code == LTU)
10174           && mode_width <= HOST_BITS_PER_WIDE_INT
10175           && exact_log2 (const_op) >= 0
10176           && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10177         {
10178           code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10179           op1 = const0_rtx, const_op = 0;
10180         }
10181
10182       /* Similarly, if we are comparing a value known to be either -1 or
10183          0 with -1, change it to the opposite comparison against zero.  */
10184
10185       if (const_op == -1
10186           && (code == EQ || code == NE || code == GT || code == LE
10187               || code == GEU || code == LTU)
10188           && num_sign_bit_copies (op0, mode) == mode_width)
10189         {
10190           code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10191           op1 = const0_rtx, const_op = 0;
10192         }
10193
10194       /* Do some canonicalizations based on the comparison code.  We prefer
10195          comparisons against zero and then prefer equality comparisons.
10196          If we can reduce the size of a constant, we will do that too.  */
10197
10198       switch (code)
10199         {
10200         case LT:
10201           /* < C is equivalent to <= (C - 1) */
10202           if (const_op > 0)
10203             {
10204               const_op -= 1;
10205               op1 = GEN_INT (const_op);
10206               code = LE;
10207               /* ... fall through to LE case below.  */
10208             }
10209           else
10210             break;
10211
10212         case LE:
10213           /* <= C is equivalent to < (C + 1); we do this for C < 0  */
10214           if (const_op < 0)
10215             {
10216               const_op += 1;
10217               op1 = GEN_INT (const_op);
10218               code = LT;
10219             }
10220
10221           /* If we are doing a <= 0 comparison on a value known to have
10222              a zero sign bit, we can replace this with == 0.  */
10223           else if (const_op == 0
10224                    && mode_width <= HOST_BITS_PER_WIDE_INT
10225                    && (nonzero_bits (op0, mode)
10226                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10227             code = EQ;
10228           break;
10229
10230         case GE:
10231           /* >= C is equivalent to > (C - 1).  */
10232           if (const_op > 0)
10233             {
10234               const_op -= 1;
10235               op1 = GEN_INT (const_op);
10236               code = GT;
10237               /* ... fall through to GT below.  */
10238             }
10239           else
10240             break;
10241
10242         case GT:
10243           /* > C is equivalent to >= (C + 1); we do this for C < 0.  */
10244           if (const_op < 0)
10245             {
10246               const_op += 1;
10247               op1 = GEN_INT (const_op);
10248               code = GE;
10249             }
10250
10251           /* If we are doing a > 0 comparison on a value known to have
10252              a zero sign bit, we can replace this with != 0.  */
10253           else if (const_op == 0
10254                    && mode_width <= HOST_BITS_PER_WIDE_INT
10255                    && (nonzero_bits (op0, mode)
10256                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10257             code = NE;
10258           break;
10259
10260         case LTU:
10261           /* < C is equivalent to <= (C - 1).  */
10262           if (const_op > 0)
10263             {
10264               const_op -= 1;
10265               op1 = GEN_INT (const_op);
10266               code = LEU;
10267               /* ... fall through ...  */
10268             }
10269
10270           /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
10271           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10272                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10273             {
10274               const_op = 0, op1 = const0_rtx;
10275               code = GE;
10276               break;
10277             }
10278           else
10279             break;
10280
10281         case LEU:
10282           /* unsigned <= 0 is equivalent to == 0 */
10283           if (const_op == 0)
10284             code = EQ;
10285
10286           /* (unsigned) <= 0x7fffffff is equivalent to >= 0.  */
10287           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10288                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10289             {
10290               const_op = 0, op1 = const0_rtx;
10291               code = GE;
10292             }
10293           break;
10294
10295         case GEU:
10296           /* >= C is equivalent to > (C - 1).  */
10297           if (const_op > 1)
10298             {
10299               const_op -= 1;
10300               op1 = GEN_INT (const_op);
10301               code = GTU;
10302               /* ... fall through ...  */
10303             }
10304
10305           /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
10306           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10307                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10308             {
10309               const_op = 0, op1 = const0_rtx;
10310               code = LT;
10311               break;
10312             }
10313           else
10314             break;
10315
10316         case GTU:
10317           /* unsigned > 0 is equivalent to != 0 */
10318           if (const_op == 0)
10319             code = NE;
10320
10321           /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
10322           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10323                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10324             {
10325               const_op = 0, op1 = const0_rtx;
10326               code = LT;
10327             }
10328           break;
10329
10330         default:
10331           break;
10332         }
10333
10334       /* Compute some predicates to simplify code below.  */
10335
10336       equality_comparison_p = (code == EQ || code == NE);
10337       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
10338       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
10339                                || code == GEU);
10340
10341       /* If this is a sign bit comparison and we can do arithmetic in
10342          MODE, say that we will only be needing the sign bit of OP0.  */
10343       if (sign_bit_comparison_p
10344           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10345         op0 = force_to_mode (op0, mode,
10346                              ((HOST_WIDE_INT) 1
10347                               << (GET_MODE_BITSIZE (mode) - 1)),
10348                              0);
10349
10350       /* Now try cases based on the opcode of OP0.  If none of the cases
10351          does a "continue", we exit this loop immediately after the
10352          switch.  */
10353
10354       switch (GET_CODE (op0))
10355         {
10356         case ZERO_EXTRACT:
10357           /* If we are extracting a single bit from a variable position in
10358              a constant that has only a single bit set and are comparing it
10359              with zero, we can convert this into an equality comparison
10360              between the position and the location of the single bit.  */
10361           /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
10362              have already reduced the shift count modulo the word size.  */
10363           if (!SHIFT_COUNT_TRUNCATED
10364               && GET_CODE (XEXP (op0, 0)) == CONST_INT
10365               && XEXP (op0, 1) == const1_rtx
10366               && equality_comparison_p && const_op == 0
10367               && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
10368             {
10369               if (BITS_BIG_ENDIAN)
10370                 {
10371                   enum machine_mode new_mode
10372                     = mode_for_extraction (EP_extzv, 1);
10373                   if (new_mode == MAX_MACHINE_MODE)
10374                     i = BITS_PER_WORD - 1 - i;
10375                   else
10376                     {
10377                       mode = new_mode;
10378                       i = (GET_MODE_BITSIZE (mode) - 1 - i);
10379                     }
10380                 }
10381
10382               op0 = XEXP (op0, 2);
10383               op1 = GEN_INT (i);
10384               const_op = i;
10385
10386               /* Result is nonzero iff shift count is equal to I.  */
10387               code = reverse_condition (code);
10388               continue;
10389             }
10390
10391           /* ... fall through ...  */
10392
10393         case SIGN_EXTRACT:
10394           tem = expand_compound_operation (op0);
10395           if (tem != op0)
10396             {
10397               op0 = tem;
10398               continue;
10399             }
10400           break;
10401
10402         case NOT:
10403           /* If testing for equality, we can take the NOT of the constant.  */
10404           if (equality_comparison_p
10405               && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10406             {
10407               op0 = XEXP (op0, 0);
10408               op1 = tem;
10409               continue;
10410             }
10411
10412           /* If just looking at the sign bit, reverse the sense of the
10413              comparison.  */
10414           if (sign_bit_comparison_p)
10415             {
10416               op0 = XEXP (op0, 0);
10417               code = (code == GE ? LT : GE);
10418               continue;
10419             }
10420           break;
10421
10422         case NEG:
10423           /* If testing for equality, we can take the NEG of the constant.  */
10424           if (equality_comparison_p
10425               && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10426             {
10427               op0 = XEXP (op0, 0);
10428               op1 = tem;
10429               continue;
10430             }
10431
10432           /* The remaining cases only apply to comparisons with zero.  */
10433           if (const_op != 0)
10434             break;
10435
10436           /* When X is ABS or is known positive,
10437              (neg X) is < 0 if and only if X != 0.  */
10438
10439           if (sign_bit_comparison_p
10440               && (GET_CODE (XEXP (op0, 0)) == ABS
10441                   || (mode_width <= HOST_BITS_PER_WIDE_INT
10442                       && (nonzero_bits (XEXP (op0, 0), mode)
10443                           & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10444             {
10445               op0 = XEXP (op0, 0);
10446               code = (code == LT ? NE : EQ);
10447               continue;
10448             }
10449
10450           /* If we have NEG of something whose two high-order bits are the
10451              same, we know that "(-a) < 0" is equivalent to "a > 0".  */
10452           if (num_sign_bit_copies (op0, mode) >= 2)
10453             {
10454               op0 = XEXP (op0, 0);
10455               code = swap_condition (code);
10456               continue;
10457             }
10458           break;
10459
10460         case ROTATE:
10461           /* If we are testing equality and our count is a constant, we
10462              can perform the inverse operation on our RHS.  */
10463           if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10464               && (tem = simplify_binary_operation (ROTATERT, mode,
10465                                                    op1, XEXP (op0, 1))) != 0)
10466             {
10467               op0 = XEXP (op0, 0);
10468               op1 = tem;
10469               continue;
10470             }
10471
10472           /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10473              a particular bit.  Convert it to an AND of a constant of that
10474              bit.  This will be converted into a ZERO_EXTRACT.  */
10475           if (const_op == 0 && sign_bit_comparison_p
10476               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10477               && mode_width <= HOST_BITS_PER_WIDE_INT)
10478             {
10479               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10480                                             ((HOST_WIDE_INT) 1
10481                                              << (mode_width - 1
10482                                                  - INTVAL (XEXP (op0, 1)))));
10483               code = (code == LT ? NE : EQ);
10484               continue;
10485             }
10486
10487           /* Fall through.  */
10488
10489         case ABS:
10490           /* ABS is ignorable inside an equality comparison with zero.  */
10491           if (const_op == 0 && equality_comparison_p)
10492             {
10493               op0 = XEXP (op0, 0);
10494               continue;
10495             }
10496           break;
10497
10498         case SIGN_EXTEND:
10499           /* Can simplify (compare (zero/sign_extend FOO) CONST) to
10500              (compare FOO CONST) if CONST fits in FOO's mode and we
10501              are either testing inequality or have an unsigned
10502              comparison with ZERO_EXTEND or a signed comparison with
10503              SIGN_EXTEND.  But don't do it if we don't have a compare
10504              insn of the given mode, since we'd have to revert it
10505              later on, and then we wouldn't know whether to sign- or
10506              zero-extend.  */
10507           mode = GET_MODE (XEXP (op0, 0));
10508           if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10509               && ! unsigned_comparison_p
10510               && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10511               && ((unsigned HOST_WIDE_INT) const_op
10512                   < (((unsigned HOST_WIDE_INT) 1
10513                       << (GET_MODE_BITSIZE (mode) - 1))))
10514               && optab_handler (cmp_optab, mode)->insn_code != CODE_FOR_nothing)
10515             {
10516               op0 = XEXP (op0, 0);
10517               continue;
10518             }
10519           break;
10520
10521         case SUBREG:
10522           /* Check for the case where we are comparing A - C1 with C2, that is
10523
10524                (subreg:MODE (plus (A) (-C1))) op (C2)
10525
10526              with C1 a constant, and try to lift the SUBREG, i.e. to do the
10527              comparison in the wider mode.  One of the following two conditions
10528              must be true in order for this to be valid:
10529
10530                1. The mode extension results in the same bit pattern being added
10531                   on both sides and the comparison is equality or unsigned.  As
10532                   C2 has been truncated to fit in MODE, the pattern can only be
10533                   all 0s or all 1s.
10534
10535                2. The mode extension results in the sign bit being copied on
10536                   each side.
10537
10538              The difficulty here is that we have predicates for A but not for
10539              (A - C1) so we need to check that C1 is within proper bounds so
10540              as to perturbate A as little as possible.  */
10541
10542           if (mode_width <= HOST_BITS_PER_WIDE_INT
10543               && subreg_lowpart_p (op0)
10544               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
10545               && GET_CODE (SUBREG_REG (op0)) == PLUS
10546               && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT)
10547             {
10548               enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
10549               rtx a = XEXP (SUBREG_REG (op0), 0);
10550               HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
10551
10552               if ((c1 > 0
10553                    && (unsigned HOST_WIDE_INT) c1
10554                        < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
10555                    && (equality_comparison_p || unsigned_comparison_p)
10556                    /* (A - C1) zero-extends if it is positive and sign-extends
10557                       if it is negative, C2 both zero- and sign-extends.  */
10558                    && ((0 == (nonzero_bits (a, inner_mode)
10559                               & ~GET_MODE_MASK (mode))
10560                         && const_op >= 0)
10561                        /* (A - C1) sign-extends if it is positive and 1-extends
10562                           if it is negative, C2 both sign- and 1-extends.  */
10563                        || (num_sign_bit_copies (a, inner_mode)
10564                            > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10565                                              - mode_width)
10566                            && const_op < 0)))
10567                   || ((unsigned HOST_WIDE_INT) c1
10568                        < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
10569                       /* (A - C1) always sign-extends, like C2.  */
10570                       && num_sign_bit_copies (a, inner_mode)
10571                          > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10572                                            - (mode_width - 1))))
10573                 {
10574                   op0 = SUBREG_REG (op0);
10575                   continue;
10576                 }
10577             }
10578
10579           /* If the inner mode is narrower and we are extracting the low part,
10580              we can treat the SUBREG as if it were a ZERO_EXTEND.  */
10581           if (subreg_lowpart_p (op0)
10582               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10583             /* Fall through */ ;
10584           else
10585             break;
10586
10587           /* ... fall through ...  */
10588
10589         case ZERO_EXTEND:
10590           mode = GET_MODE (XEXP (op0, 0));
10591           if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10592               && (unsigned_comparison_p || equality_comparison_p)
10593               && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10594               && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
10595               && optab_handler (cmp_optab, mode)->insn_code != CODE_FOR_nothing)
10596             {
10597               op0 = XEXP (op0, 0);
10598               continue;
10599             }
10600           break;
10601
10602         case PLUS:
10603           /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
10604              this for equality comparisons due to pathological cases involving
10605              overflows.  */
10606           if (equality_comparison_p
10607               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10608                                                         op1, XEXP (op0, 1))))
10609             {
10610               op0 = XEXP (op0, 0);
10611               op1 = tem;
10612               continue;
10613             }
10614
10615           /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
10616           if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10617               && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10618             {
10619               op0 = XEXP (XEXP (op0, 0), 0);
10620               code = (code == LT ? EQ : NE);
10621               continue;
10622             }
10623           break;
10624
10625         case MINUS:
10626           /* We used to optimize signed comparisons against zero, but that
10627              was incorrect.  Unsigned comparisons against zero (GTU, LEU)
10628              arrive here as equality comparisons, or (GEU, LTU) are
10629              optimized away.  No need to special-case them.  */
10630
10631           /* (eq (minus A B) C) -> (eq A (plus B C)) or
10632              (eq B (minus A C)), whichever simplifies.  We can only do
10633              this for equality comparisons due to pathological cases involving
10634              overflows.  */
10635           if (equality_comparison_p
10636               && 0 != (tem = simplify_binary_operation (PLUS, mode,
10637                                                         XEXP (op0, 1), op1)))
10638             {
10639               op0 = XEXP (op0, 0);
10640               op1 = tem;
10641               continue;
10642             }
10643
10644           if (equality_comparison_p
10645               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10646                                                         XEXP (op0, 0), op1)))
10647             {
10648               op0 = XEXP (op0, 1);
10649               op1 = tem;
10650               continue;
10651             }
10652
10653           /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10654              of bits in X minus 1, is one iff X > 0.  */
10655           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10656               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10657               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10658                  == mode_width - 1
10659               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10660             {
10661               op0 = XEXP (op0, 1);
10662               code = (code == GE ? LE : GT);
10663               continue;
10664             }
10665           break;
10666
10667         case XOR:
10668           /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
10669              if C is zero or B is a constant.  */
10670           if (equality_comparison_p
10671               && 0 != (tem = simplify_binary_operation (XOR, mode,
10672                                                         XEXP (op0, 1), op1)))
10673             {
10674               op0 = XEXP (op0, 0);
10675               op1 = tem;
10676               continue;
10677             }
10678           break;
10679
10680         case EQ:  case NE:
10681         case UNEQ:  case LTGT:
10682         case LT:  case LTU:  case UNLT:  case LE:  case LEU:  case UNLE:
10683         case GT:  case GTU:  case UNGT:  case GE:  case GEU:  case UNGE:
10684         case UNORDERED: case ORDERED:
10685           /* We can't do anything if OP0 is a condition code value, rather
10686              than an actual data value.  */
10687           if (const_op != 0
10688               || CC0_P (XEXP (op0, 0))
10689               || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10690             break;
10691
10692           /* Get the two operands being compared.  */
10693           if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10694             tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10695           else
10696             tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10697
10698           /* Check for the cases where we simply want the result of the
10699              earlier test or the opposite of that result.  */
10700           if (code == NE || code == EQ
10701               || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10702                   && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10703                   && (STORE_FLAG_VALUE
10704                       & (((HOST_WIDE_INT) 1
10705                           << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10706                   && (code == LT || code == GE)))
10707             {
10708               enum rtx_code new_code;
10709               if (code == LT || code == NE)
10710                 new_code = GET_CODE (op0);
10711               else
10712                 new_code = reversed_comparison_code (op0, NULL);
10713
10714               if (new_code != UNKNOWN)
10715                 {
10716                   code = new_code;
10717                   op0 = tem;
10718                   op1 = tem1;
10719                   continue;
10720                 }
10721             }
10722           break;
10723
10724         case IOR:
10725           /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10726              iff X <= 0.  */
10727           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10728               && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10729               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10730             {
10731               op0 = XEXP (op0, 1);
10732               code = (code == GE ? GT : LE);
10733               continue;
10734             }
10735           break;
10736
10737         case AND:
10738           /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
10739              will be converted to a ZERO_EXTRACT later.  */
10740           if (const_op == 0 && equality_comparison_p
10741               && GET_CODE (XEXP (op0, 0)) == ASHIFT
10742               && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10743             {
10744               op0 = simplify_and_const_int
10745                 (NULL_RTX, mode, gen_rtx_LSHIFTRT (mode,
10746                                                    XEXP (op0, 1),
10747                                                    XEXP (XEXP (op0, 0), 1)),
10748                  (HOST_WIDE_INT) 1);
10749               continue;
10750             }
10751
10752           /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10753              zero and X is a comparison and C1 and C2 describe only bits set
10754              in STORE_FLAG_VALUE, we can compare with X.  */
10755           if (const_op == 0 && equality_comparison_p
10756               && mode_width <= HOST_BITS_PER_WIDE_INT
10757               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10758               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10759               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10760               && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10761               && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10762             {
10763               mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10764                       << INTVAL (XEXP (XEXP (op0, 0), 1)));
10765               if ((~STORE_FLAG_VALUE & mask) == 0
10766                   && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
10767                       || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10768                           && COMPARISON_P (tem))))
10769                 {
10770                   op0 = XEXP (XEXP (op0, 0), 0);
10771                   continue;
10772                 }
10773             }
10774
10775           /* If we are doing an equality comparison of an AND of a bit equal
10776              to the sign bit, replace this with a LT or GE comparison of
10777              the underlying value.  */
10778           if (equality_comparison_p
10779               && const_op == 0
10780               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10781               && mode_width <= HOST_BITS_PER_WIDE_INT
10782               && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10783                   == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10784             {
10785               op0 = XEXP (op0, 0);
10786               code = (code == EQ ? GE : LT);
10787               continue;
10788             }
10789
10790           /* If this AND operation is really a ZERO_EXTEND from a narrower
10791              mode, the constant fits within that mode, and this is either an
10792              equality or unsigned comparison, try to do this comparison in
10793              the narrower mode.
10794
10795              Note that in:
10796
10797              (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
10798              -> (ne:DI (reg:SI 4) (const_int 0))
10799
10800              unless TRULY_NOOP_TRUNCATION allows it or the register is
10801              known to hold a value of the required mode the
10802              transformation is invalid.  */
10803           if ((equality_comparison_p || unsigned_comparison_p)
10804               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10805               && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10806                                    & GET_MODE_MASK (mode))
10807                                   + 1)) >= 0
10808               && const_op >> i == 0
10809               && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
10810               && (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
10811                                          GET_MODE_BITSIZE (GET_MODE (op0)))
10812                   || (REG_P (XEXP (op0, 0))
10813                       && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
10814             {
10815               op0 = gen_lowpart (tmode, XEXP (op0, 0));
10816               continue;
10817             }
10818
10819           /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
10820              fits in both M1 and M2 and the SUBREG is either paradoxical
10821              or represents the low part, permute the SUBREG and the AND
10822              and try again.  */
10823           if (GET_CODE (XEXP (op0, 0)) == SUBREG)
10824             {
10825               unsigned HOST_WIDE_INT c1;
10826               tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
10827               /* Require an integral mode, to avoid creating something like
10828                  (AND:SF ...).  */
10829               if (SCALAR_INT_MODE_P (tmode)
10830                   /* It is unsafe to commute the AND into the SUBREG if the
10831                      SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
10832                      not defined.  As originally written the upper bits
10833                      have a defined value due to the AND operation.
10834                      However, if we commute the AND inside the SUBREG then
10835                      they no longer have defined values and the meaning of
10836                      the code has been changed.  */
10837                   && (0
10838 #ifdef WORD_REGISTER_OPERATIONS
10839                       || (mode_width > GET_MODE_BITSIZE (tmode)
10840                           && mode_width <= BITS_PER_WORD)
10841 #endif
10842                       || (mode_width <= GET_MODE_BITSIZE (tmode)
10843                           && subreg_lowpart_p (XEXP (op0, 0))))
10844                   && GET_CODE (XEXP (op0, 1)) == CONST_INT
10845                   && mode_width <= HOST_BITS_PER_WIDE_INT
10846                   && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
10847                   && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
10848                   && (c1 & ~GET_MODE_MASK (tmode)) == 0
10849                   && c1 != mask
10850                   && c1 != GET_MODE_MASK (tmode))
10851                 {
10852                   op0 = simplify_gen_binary (AND, tmode,
10853                                              SUBREG_REG (XEXP (op0, 0)),
10854                                              gen_int_mode (c1, tmode));
10855                   op0 = gen_lowpart (mode, op0);
10856                   continue;
10857                 }
10858             }
10859
10860           /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0).  */
10861           if (const_op == 0 && equality_comparison_p
10862               && XEXP (op0, 1) == const1_rtx
10863               && GET_CODE (XEXP (op0, 0)) == NOT)
10864             {
10865               op0 = simplify_and_const_int
10866                 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
10867               code = (code == NE ? EQ : NE);
10868               continue;
10869             }
10870
10871           /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
10872              (eq (and (lshiftrt X) 1) 0).
10873              Also handle the case where (not X) is expressed using xor.  */
10874           if (const_op == 0 && equality_comparison_p
10875               && XEXP (op0, 1) == const1_rtx
10876               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
10877             {
10878               rtx shift_op = XEXP (XEXP (op0, 0), 0);
10879               rtx shift_count = XEXP (XEXP (op0, 0), 1);
10880
10881               if (GET_CODE (shift_op) == NOT
10882                   || (GET_CODE (shift_op) == XOR
10883                       && GET_CODE (XEXP (shift_op, 1)) == CONST_INT
10884                       && GET_CODE (shift_count) == CONST_INT
10885                       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10886                       && (INTVAL (XEXP (shift_op, 1))
10887                           == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
10888                 {
10889                   op0 = simplify_and_const_int
10890                     (NULL_RTX, mode,
10891                      gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
10892                      (HOST_WIDE_INT) 1);
10893                   code = (code == NE ? EQ : NE);
10894                   continue;
10895                 }
10896             }
10897           break;
10898
10899         case ASHIFT:
10900           /* If we have (compare (ashift FOO N) (const_int C)) and
10901              the high order N bits of FOO (N+1 if an inequality comparison)
10902              are known to be zero, we can do this by comparing FOO with C
10903              shifted right N bits so long as the low-order N bits of C are
10904              zero.  */
10905           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10906               && INTVAL (XEXP (op0, 1)) >= 0
10907               && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10908                   < HOST_BITS_PER_WIDE_INT)
10909               && ((const_op
10910                    & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10911               && mode_width <= HOST_BITS_PER_WIDE_INT
10912               && (nonzero_bits (XEXP (op0, 0), mode)
10913                   & ~(mask >> (INTVAL (XEXP (op0, 1))
10914                                + ! equality_comparison_p))) == 0)
10915             {
10916               /* We must perform a logical shift, not an arithmetic one,
10917                  as we want the top N bits of C to be zero.  */
10918               unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
10919
10920               temp >>= INTVAL (XEXP (op0, 1));
10921               op1 = gen_int_mode (temp, mode);
10922               op0 = XEXP (op0, 0);
10923               continue;
10924             }
10925
10926           /* If we are doing a sign bit comparison, it means we are testing
10927              a particular bit.  Convert it to the appropriate AND.  */
10928           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10929               && mode_width <= HOST_BITS_PER_WIDE_INT)
10930             {
10931               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10932                                             ((HOST_WIDE_INT) 1
10933                                              << (mode_width - 1
10934                                                  - INTVAL (XEXP (op0, 1)))));
10935               code = (code == LT ? NE : EQ);
10936               continue;
10937             }
10938
10939           /* If this an equality comparison with zero and we are shifting
10940              the low bit to the sign bit, we can convert this to an AND of the
10941              low-order bit.  */
10942           if (const_op == 0 && equality_comparison_p
10943               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10944               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10945                  == mode_width - 1)
10946             {
10947               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10948                                             (HOST_WIDE_INT) 1);
10949               continue;
10950             }
10951           break;
10952
10953         case ASHIFTRT:
10954           /* If this is an equality comparison with zero, we can do this
10955              as a logical shift, which might be much simpler.  */
10956           if (equality_comparison_p && const_op == 0
10957               && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10958             {
10959               op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10960                                           XEXP (op0, 0),
10961                                           INTVAL (XEXP (op0, 1)));
10962               continue;
10963             }
10964
10965           /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10966              do the comparison in a narrower mode.  */
10967           if (! unsigned_comparison_p
10968               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10969               && GET_CODE (XEXP (op0, 0)) == ASHIFT
10970               && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10971               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10972                                          MODE_INT, 1)) != BLKmode
10973               && (((unsigned HOST_WIDE_INT) const_op
10974                    + (GET_MODE_MASK (tmode) >> 1) + 1)
10975                   <= GET_MODE_MASK (tmode)))
10976             {
10977               op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
10978               continue;
10979             }
10980
10981           /* Likewise if OP0 is a PLUS of a sign extension with a
10982              constant, which is usually represented with the PLUS
10983              between the shifts.  */
10984           if (! unsigned_comparison_p
10985               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10986               && GET_CODE (XEXP (op0, 0)) == PLUS
10987               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10988               && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
10989               && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
10990               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10991                                          MODE_INT, 1)) != BLKmode
10992               && (((unsigned HOST_WIDE_INT) const_op
10993                    + (GET_MODE_MASK (tmode) >> 1) + 1)
10994                   <= GET_MODE_MASK (tmode)))
10995             {
10996               rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
10997               rtx add_const = XEXP (XEXP (op0, 0), 1);
10998               rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
10999                                                    add_const, XEXP (op0, 1));
11000
11001               op0 = simplify_gen_binary (PLUS, tmode,
11002                                          gen_lowpart (tmode, inner),
11003                                          new_const);
11004               continue;
11005             }
11006
11007           /* ... fall through ...  */
11008         case LSHIFTRT:
11009           /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11010              the low order N bits of FOO are known to be zero, we can do this
11011              by comparing FOO with C shifted left N bits so long as no
11012              overflow occurs.  */
11013           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
11014               && INTVAL (XEXP (op0, 1)) >= 0
11015               && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11016               && mode_width <= HOST_BITS_PER_WIDE_INT
11017               && (nonzero_bits (XEXP (op0, 0), mode)
11018                   & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
11019               && (((unsigned HOST_WIDE_INT) const_op
11020                    + (GET_CODE (op0) != LSHIFTRT
11021                       ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11022                          + 1)
11023                       : 0))
11024                   <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11025             {
11026               /* If the shift was logical, then we must make the condition
11027                  unsigned.  */
11028               if (GET_CODE (op0) == LSHIFTRT)
11029                 code = unsigned_condition (code);
11030
11031               const_op <<= INTVAL (XEXP (op0, 1));
11032               op1 = GEN_INT (const_op);
11033               op0 = XEXP (op0, 0);
11034               continue;
11035             }
11036
11037           /* If we are using this shift to extract just the sign bit, we
11038              can replace this with an LT or GE comparison.  */
11039           if (const_op == 0
11040               && (equality_comparison_p || sign_bit_comparison_p)
11041               && GET_CODE (XEXP (op0, 1)) == CONST_INT
11042               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11043                  == mode_width - 1)
11044             {
11045               op0 = XEXP (op0, 0);
11046               code = (code == NE || code == GT ? LT : GE);
11047               continue;
11048             }
11049           break;
11050
11051         default:
11052           break;
11053         }
11054
11055       break;
11056     }
11057
11058   /* Now make any compound operations involved in this comparison.  Then,
11059      check for an outmost SUBREG on OP0 that is not doing anything or is
11060      paradoxical.  The latter transformation must only be performed when
11061      it is known that the "extra" bits will be the same in op0 and op1 or
11062      that they don't matter.  There are three cases to consider:
11063
11064      1. SUBREG_REG (op0) is a register.  In this case the bits are don't
11065      care bits and we can assume they have any convenient value.  So
11066      making the transformation is safe.
11067
11068      2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11069      In this case the upper bits of op0 are undefined.  We should not make
11070      the simplification in that case as we do not know the contents of
11071      those bits.
11072
11073      3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11074      UNKNOWN.  In that case we know those bits are zeros or ones.  We must
11075      also be sure that they are the same as the upper bits of op1.
11076
11077      We can never remove a SUBREG for a non-equality comparison because
11078      the sign bit is in a different place in the underlying object.  */
11079
11080   op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11081   op1 = make_compound_operation (op1, SET);
11082
11083   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11084       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11085       && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11086       && (code == NE || code == EQ))
11087     {
11088       if (GET_MODE_SIZE (GET_MODE (op0))
11089           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
11090         {
11091           /* For paradoxical subregs, allow case 1 as above.  Case 3 isn't
11092              implemented.  */
11093           if (REG_P (SUBREG_REG (op0)))
11094             {
11095               op0 = SUBREG_REG (op0);
11096               op1 = gen_lowpart (GET_MODE (op0), op1);
11097             }
11098         }
11099       else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11100                 <= HOST_BITS_PER_WIDE_INT)
11101                && (nonzero_bits (SUBREG_REG (op0),
11102                                  GET_MODE (SUBREG_REG (op0)))
11103                    & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11104         {
11105           tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11106
11107           if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11108                & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11109             op0 = SUBREG_REG (op0), op1 = tem;
11110         }
11111     }
11112
11113   /* We now do the opposite procedure: Some machines don't have compare
11114      insns in all modes.  If OP0's mode is an integer mode smaller than a
11115      word and we can't do a compare in that mode, see if there is a larger
11116      mode for which we can do the compare.  There are a number of cases in
11117      which we can use the wider mode.  */
11118
11119   mode = GET_MODE (op0);
11120   if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11121       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11122       && ! have_insn_for (COMPARE, mode))
11123     for (tmode = GET_MODE_WIDER_MODE (mode);
11124          (tmode != VOIDmode
11125           && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11126          tmode = GET_MODE_WIDER_MODE (tmode))
11127       if (have_insn_for (COMPARE, tmode))
11128         {
11129           int zero_extended;
11130
11131           /* If the only nonzero bits in OP0 and OP1 are those in the
11132              narrower mode and this is an equality or unsigned comparison,
11133              we can use the wider mode.  Similarly for sign-extended
11134              values, in which case it is true for all comparisons.  */
11135           zero_extended = ((code == EQ || code == NE
11136                             || code == GEU || code == GTU
11137                             || code == LEU || code == LTU)
11138                            && (nonzero_bits (op0, tmode)
11139                                & ~GET_MODE_MASK (mode)) == 0
11140                            && ((GET_CODE (op1) == CONST_INT
11141                                 || (nonzero_bits (op1, tmode)
11142                                     & ~GET_MODE_MASK (mode)) == 0)));
11143
11144           if (zero_extended
11145               || ((num_sign_bit_copies (op0, tmode)
11146                    > (unsigned int) (GET_MODE_BITSIZE (tmode)
11147                                      - GET_MODE_BITSIZE (mode)))
11148                   && (num_sign_bit_copies (op1, tmode)
11149                       > (unsigned int) (GET_MODE_BITSIZE (tmode)
11150                                         - GET_MODE_BITSIZE (mode)))))
11151             {
11152               /* If OP0 is an AND and we don't have an AND in MODE either,
11153                  make a new AND in the proper mode.  */
11154               if (GET_CODE (op0) == AND
11155                   && !have_insn_for (AND, mode))
11156                 op0 = simplify_gen_binary (AND, tmode,
11157                                            gen_lowpart (tmode,
11158                                                         XEXP (op0, 0)),
11159                                            gen_lowpart (tmode,
11160                                                         XEXP (op0, 1)));
11161
11162               op0 = gen_lowpart (tmode, op0);
11163               if (zero_extended && GET_CODE (op1) == CONST_INT)
11164                 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
11165               op1 = gen_lowpart (tmode, op1);
11166               break;
11167             }
11168
11169           /* If this is a test for negative, we can make an explicit
11170              test of the sign bit.  */
11171
11172           if (op1 == const0_rtx && (code == LT || code == GE)
11173               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11174             {
11175               op0 = simplify_gen_binary (AND, tmode,
11176                                          gen_lowpart (tmode, op0),
11177                                          GEN_INT ((HOST_WIDE_INT) 1
11178                                                   << (GET_MODE_BITSIZE (mode)
11179                                                       - 1)));
11180               code = (code == LT) ? NE : EQ;
11181               break;
11182             }
11183         }
11184
11185 #ifdef CANONICALIZE_COMPARISON
11186   /* If this machine only supports a subset of valid comparisons, see if we
11187      can convert an unsupported one into a supported one.  */
11188   CANONICALIZE_COMPARISON (code, op0, op1);
11189 #endif
11190
11191   *pop0 = op0;
11192   *pop1 = op1;
11193
11194   return code;
11195 }
11196 \f
11197 /* Utility function for record_value_for_reg.  Count number of
11198    rtxs in X.  */
11199 static int
11200 count_rtxs (rtx x)
11201 {
11202   enum rtx_code code = GET_CODE (x);
11203   const char *fmt;
11204   int i, ret = 1;
11205
11206   if (GET_RTX_CLASS (code) == '2'
11207       || GET_RTX_CLASS (code) == 'c')
11208     {
11209       rtx x0 = XEXP (x, 0);
11210       rtx x1 = XEXP (x, 1);
11211
11212       if (x0 == x1)
11213         return 1 + 2 * count_rtxs (x0);
11214
11215       if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11216            || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11217           && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11218         return 2 + 2 * count_rtxs (x0)
11219                + count_rtxs (x == XEXP (x1, 0)
11220                              ? XEXP (x1, 1) : XEXP (x1, 0));
11221
11222       if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11223            || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11224           && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11225         return 2 + 2 * count_rtxs (x1)
11226                + count_rtxs (x == XEXP (x0, 0)
11227                              ? XEXP (x0, 1) : XEXP (x0, 0));
11228     }
11229
11230   fmt = GET_RTX_FORMAT (code);
11231   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11232     if (fmt[i] == 'e')
11233       ret += count_rtxs (XEXP (x, i));
11234
11235   return ret;
11236 }
11237 \f
11238 /* Utility function for following routine.  Called when X is part of a value
11239    being stored into last_set_value.  Sets last_set_table_tick
11240    for each register mentioned.  Similar to mention_regs in cse.c  */
11241
11242 static void
11243 update_table_tick (rtx x)
11244 {
11245   enum rtx_code code = GET_CODE (x);
11246   const char *fmt = GET_RTX_FORMAT (code);
11247   int i;
11248
11249   if (code == REG)
11250     {
11251       unsigned int regno = REGNO (x);
11252       unsigned int endregno = END_REGNO (x);
11253       unsigned int r;
11254
11255       for (r = regno; r < endregno; r++)
11256         {
11257           reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, r);
11258           rsp->last_set_table_tick = label_tick;
11259         }
11260
11261       return;
11262     }
11263
11264   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11265     /* Note that we can't have an "E" in values stored; see
11266        get_last_value_validate.  */
11267     if (fmt[i] == 'e')
11268       {
11269         /* Check for identical subexpressions.  If x contains
11270            identical subexpression we only have to traverse one of
11271            them.  */
11272         if (i == 0 && ARITHMETIC_P (x))
11273           {
11274             /* Note that at this point x1 has already been
11275                processed.  */
11276             rtx x0 = XEXP (x, 0);
11277             rtx x1 = XEXP (x, 1);
11278
11279             /* If x0 and x1 are identical then there is no need to
11280                process x0.  */
11281             if (x0 == x1)
11282               break;
11283
11284             /* If x0 is identical to a subexpression of x1 then while
11285                processing x1, x0 has already been processed.  Thus we
11286                are done with x.  */
11287             if (ARITHMETIC_P (x1)
11288                 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11289               break;
11290
11291             /* If x1 is identical to a subexpression of x0 then we
11292                still have to process the rest of x0.  */
11293             if (ARITHMETIC_P (x0)
11294                 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11295               {
11296                 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
11297                 break;
11298               }
11299           }
11300
11301         update_table_tick (XEXP (x, i));
11302       }
11303 }
11304
11305 /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
11306    are saying that the register is clobbered and we no longer know its
11307    value.  If INSN is zero, don't update reg_stat[].last_set; this is
11308    only permitted with VALUE also zero and is used to invalidate the
11309    register.  */
11310
11311 static void
11312 record_value_for_reg (rtx reg, rtx insn, rtx value)
11313 {
11314   unsigned int regno = REGNO (reg);
11315   unsigned int endregno = END_REGNO (reg);
11316   unsigned int i;
11317   reg_stat_type *rsp;
11318
11319   /* If VALUE contains REG and we have a previous value for REG, substitute
11320      the previous value.  */
11321   if (value && insn && reg_overlap_mentioned_p (reg, value))
11322     {
11323       rtx tem;
11324
11325       /* Set things up so get_last_value is allowed to see anything set up to
11326          our insn.  */
11327       subst_low_luid = DF_INSN_LUID (insn);
11328       tem = get_last_value (reg);
11329
11330       /* If TEM is simply a binary operation with two CLOBBERs as operands,
11331          it isn't going to be useful and will take a lot of time to process,
11332          so just use the CLOBBER.  */
11333
11334       if (tem)
11335         {
11336           if (ARITHMETIC_P (tem)
11337               && GET_CODE (XEXP (tem, 0)) == CLOBBER
11338               && GET_CODE (XEXP (tem, 1)) == CLOBBER)
11339             tem = XEXP (tem, 0);
11340           else if (count_occurrences (value, reg, 1) >= 2)
11341             {
11342               /* If there are two or more occurrences of REG in VALUE,
11343                  prevent the value from growing too much.  */
11344               if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
11345                 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
11346             }
11347
11348           value = replace_rtx (copy_rtx (value), reg, tem);
11349         }
11350     }
11351
11352   /* For each register modified, show we don't know its value, that
11353      we don't know about its bitwise content, that its value has been
11354      updated, and that we don't know the location of the death of the
11355      register.  */
11356   for (i = regno; i < endregno; i++)
11357     {
11358       rsp = VEC_index (reg_stat_type, reg_stat, i);
11359
11360       if (insn)
11361         rsp->last_set = insn;
11362
11363       rsp->last_set_value = 0;
11364       rsp->last_set_mode = 0;
11365       rsp->last_set_nonzero_bits = 0;
11366       rsp->last_set_sign_bit_copies = 0;
11367       rsp->last_death = 0;
11368       rsp->truncated_to_mode = 0;
11369     }
11370
11371   /* Mark registers that are being referenced in this value.  */
11372   if (value)
11373     update_table_tick (value);
11374
11375   /* Now update the status of each register being set.
11376      If someone is using this register in this block, set this register
11377      to invalid since we will get confused between the two lives in this
11378      basic block.  This makes using this register always invalid.  In cse, we
11379      scan the table to invalidate all entries using this register, but this
11380      is too much work for us.  */
11381
11382   for (i = regno; i < endregno; i++)
11383     {
11384       rsp = VEC_index (reg_stat_type, reg_stat, i);
11385       rsp->last_set_label = label_tick;
11386       if (!insn
11387           || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
11388         rsp->last_set_invalid = 1;
11389       else
11390         rsp->last_set_invalid = 0;
11391     }
11392
11393   /* The value being assigned might refer to X (like in "x++;").  In that
11394      case, we must replace it with (clobber (const_int 0)) to prevent
11395      infinite loops.  */
11396   rsp = VEC_index (reg_stat_type, reg_stat, regno);
11397   if (value && ! get_last_value_validate (&value, insn,
11398                                           rsp->last_set_label, 0))
11399     {
11400       value = copy_rtx (value);
11401       if (! get_last_value_validate (&value, insn,
11402                                      rsp->last_set_label, 1))
11403         value = 0;
11404     }
11405
11406   /* For the main register being modified, update the value, the mode, the
11407      nonzero bits, and the number of sign bit copies.  */
11408
11409   rsp->last_set_value = value;
11410
11411   if (value)
11412     {
11413       enum machine_mode mode = GET_MODE (reg);
11414       subst_low_luid = DF_INSN_LUID (insn);
11415       rsp->last_set_mode = mode;
11416       if (GET_MODE_CLASS (mode) == MODE_INT
11417           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11418         mode = nonzero_bits_mode;
11419       rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
11420       rsp->last_set_sign_bit_copies
11421         = num_sign_bit_copies (value, GET_MODE (reg));
11422     }
11423 }
11424
11425 /* Called via note_stores from record_dead_and_set_regs to handle one
11426    SET or CLOBBER in an insn.  DATA is the instruction in which the
11427    set is occurring.  */
11428
11429 static void
11430 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
11431 {
11432   rtx record_dead_insn = (rtx) data;
11433
11434   if (GET_CODE (dest) == SUBREG)
11435     dest = SUBREG_REG (dest);
11436
11437   if (!record_dead_insn)
11438     {
11439       if (REG_P (dest))
11440         record_value_for_reg (dest, NULL_RTX, NULL_RTX);
11441       return;
11442     }
11443
11444   if (REG_P (dest))
11445     {
11446       /* If we are setting the whole register, we know its value.  Otherwise
11447          show that we don't know the value.  We can handle SUBREG in
11448          some cases.  */
11449       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11450         record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11451       else if (GET_CODE (setter) == SET
11452                && GET_CODE (SET_DEST (setter)) == SUBREG
11453                && SUBREG_REG (SET_DEST (setter)) == dest
11454                && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11455                && subreg_lowpart_p (SET_DEST (setter)))
11456         record_value_for_reg (dest, record_dead_insn,
11457                               gen_lowpart (GET_MODE (dest),
11458                                                        SET_SRC (setter)));
11459       else
11460         record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11461     }
11462   else if (MEM_P (dest)
11463            /* Ignore pushes, they clobber nothing.  */
11464            && ! push_operand (dest, GET_MODE (dest)))
11465     mem_last_set = DF_INSN_LUID (record_dead_insn);
11466 }
11467
11468 /* Update the records of when each REG was most recently set or killed
11469    for the things done by INSN.  This is the last thing done in processing
11470    INSN in the combiner loop.
11471
11472    We update reg_stat[], in particular fields last_set, last_set_value,
11473    last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
11474    last_death, and also the similar information mem_last_set (which insn
11475    most recently modified memory) and last_call_luid (which insn was the
11476    most recent subroutine call).  */
11477
11478 static void
11479 record_dead_and_set_regs (rtx insn)
11480 {
11481   rtx link;
11482   unsigned int i;
11483
11484   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11485     {
11486       if (REG_NOTE_KIND (link) == REG_DEAD
11487           && REG_P (XEXP (link, 0)))
11488         {
11489           unsigned int regno = REGNO (XEXP (link, 0));
11490           unsigned int endregno = END_REGNO (XEXP (link, 0));
11491
11492           for (i = regno; i < endregno; i++)
11493             {
11494               reg_stat_type *rsp;
11495
11496               rsp = VEC_index (reg_stat_type, reg_stat, i);
11497               rsp->last_death = insn;
11498             }
11499         }
11500       else if (REG_NOTE_KIND (link) == REG_INC)
11501         record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11502     }
11503
11504   if (CALL_P (insn))
11505     {
11506       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11507         if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11508           {
11509             reg_stat_type *rsp;
11510
11511             rsp = VEC_index (reg_stat_type, reg_stat, i);
11512             rsp->last_set_invalid = 1;
11513             rsp->last_set = insn;
11514             rsp->last_set_value = 0;
11515             rsp->last_set_mode = 0;
11516             rsp->last_set_nonzero_bits = 0;
11517             rsp->last_set_sign_bit_copies = 0;
11518             rsp->last_death = 0;
11519             rsp->truncated_to_mode = 0;
11520           }
11521
11522       last_call_luid = mem_last_set = DF_INSN_LUID (insn);
11523
11524       /* We can't combine into a call pattern.  Remember, though, that
11525          the return value register is set at this LUID.  We could
11526          still replace a register with the return value from the
11527          wrong subroutine call!  */
11528       note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
11529     }
11530   else
11531     note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11532 }
11533
11534 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11535    register present in the SUBREG, so for each such SUBREG go back and
11536    adjust nonzero and sign bit information of the registers that are
11537    known to have some zero/sign bits set.
11538
11539    This is needed because when combine blows the SUBREGs away, the
11540    information on zero/sign bits is lost and further combines can be
11541    missed because of that.  */
11542
11543 static void
11544 record_promoted_value (rtx insn, rtx subreg)
11545 {
11546   rtx links, set;
11547   unsigned int regno = REGNO (SUBREG_REG (subreg));
11548   enum machine_mode mode = GET_MODE (subreg);
11549
11550   if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11551     return;
11552
11553   for (links = LOG_LINKS (insn); links;)
11554     {
11555       reg_stat_type *rsp;
11556
11557       insn = XEXP (links, 0);
11558       set = single_set (insn);
11559
11560       if (! set || !REG_P (SET_DEST (set))
11561           || REGNO (SET_DEST (set)) != regno
11562           || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11563         {
11564           links = XEXP (links, 1);
11565           continue;
11566         }
11567
11568       rsp = VEC_index (reg_stat_type, reg_stat, regno);
11569       if (rsp->last_set == insn)
11570         {
11571           if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11572             rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
11573         }
11574
11575       if (REG_P (SET_SRC (set)))
11576         {
11577           regno = REGNO (SET_SRC (set));
11578           links = LOG_LINKS (insn);
11579         }
11580       else
11581         break;
11582     }
11583 }
11584
11585 /* Check if X, a register, is known to contain a value already
11586    truncated to MODE.  In this case we can use a subreg to refer to
11587    the truncated value even though in the generic case we would need
11588    an explicit truncation.  */
11589
11590 static bool
11591 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
11592 {
11593   reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
11594   enum machine_mode truncated = rsp->truncated_to_mode;
11595
11596   if (truncated == 0
11597       || rsp->truncation_label < label_tick_ebb_start)
11598     return false;
11599   if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
11600     return true;
11601   if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
11602                              GET_MODE_BITSIZE (truncated)))
11603     return true;
11604   return false;
11605 }
11606
11607 /* X is a REG or a SUBREG.  If X is some sort of a truncation record
11608    it.  For non-TRULY_NOOP_TRUNCATION targets we might be able to turn
11609    a truncate into a subreg using this information.  */
11610
11611 static void
11612 record_truncated_value (rtx x)
11613 {
11614   enum machine_mode truncated_mode;
11615   reg_stat_type *rsp;
11616
11617   if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
11618     {
11619       enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
11620       truncated_mode = GET_MODE (x);
11621
11622       if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
11623         return;
11624
11625       if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (truncated_mode),
11626                                  GET_MODE_BITSIZE (original_mode)))
11627         return;
11628
11629       x = SUBREG_REG (x);
11630     }
11631   /* ??? For hard-regs we now record everything.  We might be able to
11632      optimize this using last_set_mode.  */
11633   else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
11634     truncated_mode = GET_MODE (x);
11635   else
11636     return;
11637
11638   rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
11639   if (rsp->truncated_to_mode == 0
11640       || rsp->truncation_label < label_tick_ebb_start
11641       || (GET_MODE_SIZE (truncated_mode)
11642           < GET_MODE_SIZE (rsp->truncated_to_mode)))
11643     {
11644       rsp->truncated_to_mode = truncated_mode;
11645       rsp->truncation_label = label_tick;
11646     }
11647 }
11648
11649 /* Scan X for promoted SUBREGs and truncated REGs.  For each one
11650    found, note what it implies to the registers used in it.  */
11651
11652 static void
11653 check_conversions (rtx insn, rtx x)
11654 {
11655   if (GET_CODE (x) == SUBREG || REG_P (x))
11656     {
11657       if (GET_CODE (x) == SUBREG
11658           && SUBREG_PROMOTED_VAR_P (x)
11659           && REG_P (SUBREG_REG (x)))
11660         record_promoted_value (insn, x);
11661
11662       record_truncated_value (x);
11663     }
11664   else
11665     {
11666       const char *format = GET_RTX_FORMAT (GET_CODE (x));
11667       int i, j;
11668
11669       for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11670         switch (format[i])
11671           {
11672           case 'e':
11673             check_conversions (insn, XEXP (x, i));
11674             break;
11675           case 'V':
11676           case 'E':
11677             if (XVEC (x, i) != 0)
11678               for (j = 0; j < XVECLEN (x, i); j++)
11679                 check_conversions (insn, XVECEXP (x, i, j));
11680             break;
11681           }
11682     }
11683 }
11684 \f
11685 /* Utility routine for the following function.  Verify that all the registers
11686    mentioned in *LOC are valid when *LOC was part of a value set when
11687    label_tick == TICK.  Return 0 if some are not.
11688
11689    If REPLACE is nonzero, replace the invalid reference with
11690    (clobber (const_int 0)) and return 1.  This replacement is useful because
11691    we often can get useful information about the form of a value (e.g., if
11692    it was produced by a shift that always produces -1 or 0) even though
11693    we don't know exactly what registers it was produced from.  */
11694
11695 static int
11696 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
11697 {
11698   rtx x = *loc;
11699   const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11700   int len = GET_RTX_LENGTH (GET_CODE (x));
11701   int i;
11702
11703   if (REG_P (x))
11704     {
11705       unsigned int regno = REGNO (x);
11706       unsigned int endregno = END_REGNO (x);
11707       unsigned int j;
11708
11709       for (j = regno; j < endregno; j++)
11710         {
11711           reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, j);
11712           if (rsp->last_set_invalid
11713               /* If this is a pseudo-register that was only set once and not
11714                  live at the beginning of the function, it is always valid.  */
11715               || (! (regno >= FIRST_PSEUDO_REGISTER
11716                      && REG_N_SETS (regno) == 1
11717                      && (!REGNO_REG_SET_P
11718                          (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno)))
11719                   && rsp->last_set_label > tick))
11720           {
11721             if (replace)
11722               *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11723             return replace;
11724           }
11725         }
11726
11727       return 1;
11728     }
11729   /* If this is a memory reference, make sure that there were
11730      no stores after it that might have clobbered the value.  We don't
11731      have alias info, so we assume any store invalidates it.  */
11732   else if (MEM_P (x) && !MEM_READONLY_P (x)
11733            && DF_INSN_LUID (insn) <= mem_last_set)
11734     {
11735       if (replace)
11736         *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11737       return replace;
11738     }
11739
11740   for (i = 0; i < len; i++)
11741     {
11742       if (fmt[i] == 'e')
11743         {
11744           /* Check for identical subexpressions.  If x contains
11745              identical subexpression we only have to traverse one of
11746              them.  */
11747           if (i == 1 && ARITHMETIC_P (x))
11748             {
11749               /* Note that at this point x0 has already been checked
11750                  and found valid.  */
11751               rtx x0 = XEXP (x, 0);
11752               rtx x1 = XEXP (x, 1);
11753
11754               /* If x0 and x1 are identical then x is also valid.  */
11755               if (x0 == x1)
11756                 return 1;
11757
11758               /* If x1 is identical to a subexpression of x0 then
11759                  while checking x0, x1 has already been checked.  Thus
11760                  it is valid and so as x.  */
11761               if (ARITHMETIC_P (x0)
11762                   && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11763                 return 1;
11764
11765               /* If x0 is identical to a subexpression of x1 then x is
11766                  valid iff the rest of x1 is valid.  */
11767               if (ARITHMETIC_P (x1)
11768                   && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11769                 return
11770                   get_last_value_validate (&XEXP (x1,
11771                                                   x0 == XEXP (x1, 0) ? 1 : 0),
11772                                            insn, tick, replace);
11773             }
11774
11775           if (get_last_value_validate (&XEXP (x, i), insn, tick,
11776                                        replace) == 0)
11777             return 0;
11778         }
11779       /* Don't bother with these.  They shouldn't occur anyway.  */
11780       else if (fmt[i] == 'E')
11781         return 0;
11782     }
11783
11784   /* If we haven't found a reason for it to be invalid, it is valid.  */
11785   return 1;
11786 }
11787
11788 /* Get the last value assigned to X, if known.  Some registers
11789    in the value may be replaced with (clobber (const_int 0)) if their value
11790    is known longer known reliably.  */
11791
11792 static rtx
11793 get_last_value (const_rtx x)
11794 {
11795   unsigned int regno;
11796   rtx value;
11797   reg_stat_type *rsp;
11798
11799   /* If this is a non-paradoxical SUBREG, get the value of its operand and
11800      then convert it to the desired mode.  If this is a paradoxical SUBREG,
11801      we cannot predict what values the "extra" bits might have.  */
11802   if (GET_CODE (x) == SUBREG
11803       && subreg_lowpart_p (x)
11804       && (GET_MODE_SIZE (GET_MODE (x))
11805           <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11806       && (value = get_last_value (SUBREG_REG (x))) != 0)
11807     return gen_lowpart (GET_MODE (x), value);
11808
11809   if (!REG_P (x))
11810     return 0;
11811
11812   regno = REGNO (x);
11813   rsp = VEC_index (reg_stat_type, reg_stat, regno);
11814   value = rsp->last_set_value;
11815
11816   /* If we don't have a value, or if it isn't for this basic block and
11817      it's either a hard register, set more than once, or it's a live
11818      at the beginning of the function, return 0.
11819
11820      Because if it's not live at the beginning of the function then the reg
11821      is always set before being used (is never used without being set).
11822      And, if it's set only once, and it's always set before use, then all
11823      uses must have the same last value, even if it's not from this basic
11824      block.  */
11825
11826   if (value == 0
11827       || (rsp->last_set_label < label_tick_ebb_start
11828           && (regno < FIRST_PSEUDO_REGISTER
11829               || REG_N_SETS (regno) != 1
11830               || REGNO_REG_SET_P
11831                  (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno))))
11832     return 0;
11833
11834   /* If the value was set in a later insn than the ones we are processing,
11835      we can't use it even if the register was only set once.  */
11836   if (rsp->last_set_label == label_tick
11837       && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
11838     return 0;
11839
11840   /* If the value has all its registers valid, return it.  */
11841   if (get_last_value_validate (&value, rsp->last_set,
11842                                rsp->last_set_label, 0))
11843     return value;
11844
11845   /* Otherwise, make a copy and replace any invalid register with
11846      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
11847
11848   value = copy_rtx (value);
11849   if (get_last_value_validate (&value, rsp->last_set,
11850                                rsp->last_set_label, 1))
11851     return value;
11852
11853   return 0;
11854 }
11855 \f
11856 /* Return nonzero if expression X refers to a REG or to memory
11857    that is set in an instruction more recent than FROM_LUID.  */
11858
11859 static int
11860 use_crosses_set_p (const_rtx x, int from_luid)
11861 {
11862   const char *fmt;
11863   int i;
11864   enum rtx_code code = GET_CODE (x);
11865
11866   if (code == REG)
11867     {
11868       unsigned int regno = REGNO (x);
11869       unsigned endreg = END_REGNO (x);
11870
11871 #ifdef PUSH_ROUNDING
11872       /* Don't allow uses of the stack pointer to be moved,
11873          because we don't know whether the move crosses a push insn.  */
11874       if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11875         return 1;
11876 #endif
11877       for (; regno < endreg; regno++)
11878         {
11879           reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
11880           if (rsp->last_set
11881               && rsp->last_set_label == label_tick
11882               && DF_INSN_LUID (rsp->last_set) > from_luid)
11883             return 1;
11884         }
11885       return 0;
11886     }
11887
11888   if (code == MEM && mem_last_set > from_luid)
11889     return 1;
11890
11891   fmt = GET_RTX_FORMAT (code);
11892
11893   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11894     {
11895       if (fmt[i] == 'E')
11896         {
11897           int j;
11898           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11899             if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
11900               return 1;
11901         }
11902       else if (fmt[i] == 'e'
11903                && use_crosses_set_p (XEXP (x, i), from_luid))
11904         return 1;
11905     }
11906   return 0;
11907 }
11908 \f
11909 /* Define three variables used for communication between the following
11910    routines.  */
11911
11912 static unsigned int reg_dead_regno, reg_dead_endregno;
11913 static int reg_dead_flag;
11914
11915 /* Function called via note_stores from reg_dead_at_p.
11916
11917    If DEST is within [reg_dead_regno, reg_dead_endregno), set
11918    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
11919
11920 static void
11921 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
11922 {
11923   unsigned int regno, endregno;
11924
11925   if (!REG_P (dest))
11926     return;
11927
11928   regno = REGNO (dest);
11929   endregno = END_REGNO (dest);
11930   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11931     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11932 }
11933
11934 /* Return nonzero if REG is known to be dead at INSN.
11935
11936    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
11937    referencing REG, it is dead.  If we hit a SET referencing REG, it is
11938    live.  Otherwise, see if it is live or dead at the start of the basic
11939    block we are in.  Hard regs marked as being live in NEWPAT_USED_REGS
11940    must be assumed to be always live.  */
11941
11942 static int
11943 reg_dead_at_p (rtx reg, rtx insn)
11944 {
11945   basic_block block;
11946   unsigned int i;
11947
11948   /* Set variables for reg_dead_at_p_1.  */
11949   reg_dead_regno = REGNO (reg);
11950   reg_dead_endregno = END_REGNO (reg);
11951
11952   reg_dead_flag = 0;
11953
11954   /* Check that reg isn't mentioned in NEWPAT_USED_REGS.  For fixed registers
11955      we allow the machine description to decide whether use-and-clobber
11956      patterns are OK.  */
11957   if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11958     {
11959       for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11960         if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
11961           return 0;
11962     }
11963
11964   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11965      beginning of function.  */
11966   for (; insn && !LABEL_P (insn) && !BARRIER_P (insn);
11967        insn = prev_nonnote_insn (insn))
11968     {
11969       note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
11970       if (reg_dead_flag)
11971         return reg_dead_flag == 1 ? 1 : 0;
11972
11973       if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11974         return 1;
11975     }
11976
11977   /* Get the basic block that we were in.  */
11978   if (insn == 0)
11979     block = ENTRY_BLOCK_PTR->next_bb;
11980   else
11981     {
11982       FOR_EACH_BB (block)
11983         if (insn == BB_HEAD (block))
11984           break;
11985
11986       if (block == EXIT_BLOCK_PTR)
11987         return 0;
11988     }
11989
11990   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11991     if (REGNO_REG_SET_P (df_get_live_in (block), i))
11992       return 0;
11993
11994   return 1;
11995 }
11996 \f
11997 /* Note hard registers in X that are used.  */
11998
11999 static void
12000 mark_used_regs_combine (rtx x)
12001 {
12002   RTX_CODE code = GET_CODE (x);
12003   unsigned int regno;
12004   int i;
12005
12006   switch (code)
12007     {
12008     case LABEL_REF:
12009     case SYMBOL_REF:
12010     case CONST_INT:
12011     case CONST:
12012     case CONST_DOUBLE:
12013     case CONST_VECTOR:
12014     case PC:
12015     case ADDR_VEC:
12016     case ADDR_DIFF_VEC:
12017     case ASM_INPUT:
12018 #ifdef HAVE_cc0
12019     /* CC0 must die in the insn after it is set, so we don't need to take
12020        special note of it here.  */
12021     case CC0:
12022 #endif
12023       return;
12024
12025     case CLOBBER:
12026       /* If we are clobbering a MEM, mark any hard registers inside the
12027          address as used.  */
12028       if (MEM_P (XEXP (x, 0)))
12029         mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12030       return;
12031
12032     case REG:
12033       regno = REGNO (x);
12034       /* A hard reg in a wide mode may really be multiple registers.
12035          If so, mark all of them just like the first.  */
12036       if (regno < FIRST_PSEUDO_REGISTER)
12037         {
12038           /* None of this applies to the stack, frame or arg pointers.  */
12039           if (regno == STACK_POINTER_REGNUM
12040 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
12041               || regno == HARD_FRAME_POINTER_REGNUM
12042 #endif
12043 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12044               || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12045 #endif
12046               || regno == FRAME_POINTER_REGNUM)
12047             return;
12048
12049           add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
12050         }
12051       return;
12052
12053     case SET:
12054       {
12055         /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12056            the address.  */
12057         rtx testreg = SET_DEST (x);
12058
12059         while (GET_CODE (testreg) == SUBREG
12060                || GET_CODE (testreg) == ZERO_EXTRACT
12061                || GET_CODE (testreg) == STRICT_LOW_PART)
12062           testreg = XEXP (testreg, 0);
12063
12064         if (MEM_P (testreg))
12065           mark_used_regs_combine (XEXP (testreg, 0));
12066
12067         mark_used_regs_combine (SET_SRC (x));
12068       }
12069       return;
12070
12071     default:
12072       break;
12073     }
12074
12075   /* Recursively scan the operands of this expression.  */
12076
12077   {
12078     const char *fmt = GET_RTX_FORMAT (code);
12079
12080     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12081       {
12082         if (fmt[i] == 'e')
12083           mark_used_regs_combine (XEXP (x, i));
12084         else if (fmt[i] == 'E')
12085           {
12086             int j;
12087
12088             for (j = 0; j < XVECLEN (x, i); j++)
12089               mark_used_regs_combine (XVECEXP (x, i, j));
12090           }
12091       }
12092   }
12093 }
12094 \f
12095 /* Remove register number REGNO from the dead registers list of INSN.
12096
12097    Return the note used to record the death, if there was one.  */
12098
12099 rtx
12100 remove_death (unsigned int regno, rtx insn)
12101 {
12102   rtx note = find_regno_note (insn, REG_DEAD, regno);
12103
12104   if (note)
12105     remove_note (insn, note);
12106
12107   return note;
12108 }
12109
12110 /* For each register (hardware or pseudo) used within expression X, if its
12111    death is in an instruction with luid between FROM_LUID (inclusive) and
12112    TO_INSN (exclusive), put a REG_DEAD note for that register in the
12113    list headed by PNOTES.
12114
12115    That said, don't move registers killed by maybe_kill_insn.
12116
12117    This is done when X is being merged by combination into TO_INSN.  These
12118    notes will then be distributed as needed.  */
12119
12120 static void
12121 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx to_insn,
12122              rtx *pnotes)
12123 {
12124   const char *fmt;
12125   int len, i;
12126   enum rtx_code code = GET_CODE (x);
12127
12128   if (code == REG)
12129     {
12130       unsigned int regno = REGNO (x);
12131       rtx where_dead = VEC_index (reg_stat_type, reg_stat, regno)->last_death;
12132
12133       /* Don't move the register if it gets killed in between from and to.  */
12134       if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12135           && ! reg_referenced_p (x, maybe_kill_insn))
12136         return;
12137
12138       if (where_dead
12139           && DF_INSN_LUID (where_dead) >= from_luid
12140           && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
12141         {
12142           rtx note = remove_death (regno, where_dead);
12143
12144           /* It is possible for the call above to return 0.  This can occur
12145              when last_death points to I2 or I1 that we combined with.
12146              In that case make a new note.
12147
12148              We must also check for the case where X is a hard register
12149              and NOTE is a death note for a range of hard registers
12150              including X.  In that case, we must put REG_DEAD notes for
12151              the remaining registers in place of NOTE.  */
12152
12153           if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12154               && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12155                   > GET_MODE_SIZE (GET_MODE (x))))
12156             {
12157               unsigned int deadregno = REGNO (XEXP (note, 0));
12158               unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
12159               unsigned int ourend = END_HARD_REGNO (x);
12160               unsigned int i;
12161
12162               for (i = deadregno; i < deadend; i++)
12163                 if (i < regno || i >= ourend)
12164                   REG_NOTES (where_dead)
12165                     = gen_rtx_EXPR_LIST (REG_DEAD,
12166                                          regno_reg_rtx[i],
12167                                          REG_NOTES (where_dead));
12168             }
12169
12170           /* If we didn't find any note, or if we found a REG_DEAD note that
12171              covers only part of the given reg, and we have a multi-reg hard
12172              register, then to be safe we must check for REG_DEAD notes
12173              for each register other than the first.  They could have
12174              their own REG_DEAD notes lying around.  */
12175           else if ((note == 0
12176                     || (note != 0
12177                         && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12178                             < GET_MODE_SIZE (GET_MODE (x)))))
12179                    && regno < FIRST_PSEUDO_REGISTER
12180                    && hard_regno_nregs[regno][GET_MODE (x)] > 1)
12181             {
12182               unsigned int ourend = END_HARD_REGNO (x);
12183               unsigned int i, offset;
12184               rtx oldnotes = 0;
12185
12186               if (note)
12187                 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
12188               else
12189                 offset = 1;
12190
12191               for (i = regno + offset; i < ourend; i++)
12192                 move_deaths (regno_reg_rtx[i],
12193                              maybe_kill_insn, from_luid, to_insn, &oldnotes);
12194             }
12195
12196           if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12197             {
12198               XEXP (note, 1) = *pnotes;
12199               *pnotes = note;
12200             }
12201           else
12202             *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
12203         }
12204
12205       return;
12206     }
12207
12208   else if (GET_CODE (x) == SET)
12209     {
12210       rtx dest = SET_DEST (x);
12211
12212       move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
12213
12214       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
12215          that accesses one word of a multi-word item, some
12216          piece of everything register in the expression is used by
12217          this insn, so remove any old death.  */
12218       /* ??? So why do we test for equality of the sizes?  */
12219
12220       if (GET_CODE (dest) == ZERO_EXTRACT
12221           || GET_CODE (dest) == STRICT_LOW_PART
12222           || (GET_CODE (dest) == SUBREG
12223               && (((GET_MODE_SIZE (GET_MODE (dest))
12224                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
12225                   == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
12226                        + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
12227         {
12228           move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
12229           return;
12230         }
12231
12232       /* If this is some other SUBREG, we know it replaces the entire
12233          value, so use that as the destination.  */
12234       if (GET_CODE (dest) == SUBREG)
12235         dest = SUBREG_REG (dest);
12236
12237       /* If this is a MEM, adjust deaths of anything used in the address.
12238          For a REG (the only other possibility), the entire value is
12239          being replaced so the old value is not used in this insn.  */
12240
12241       if (MEM_P (dest))
12242         move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
12243                      to_insn, pnotes);
12244       return;
12245     }
12246
12247   else if (GET_CODE (x) == CLOBBER)
12248     return;
12249
12250   len = GET_RTX_LENGTH (code);
12251   fmt = GET_RTX_FORMAT (code);
12252
12253   for (i = 0; i < len; i++)
12254     {
12255       if (fmt[i] == 'E')
12256         {
12257           int j;
12258           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12259             move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
12260                          to_insn, pnotes);
12261         }
12262       else if (fmt[i] == 'e')
12263         move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
12264     }
12265 }
12266 \f
12267 /* Return 1 if X is the target of a bit-field assignment in BODY, the
12268    pattern of an insn.  X must be a REG.  */
12269
12270 static int
12271 reg_bitfield_target_p (rtx x, rtx body)
12272 {
12273   int i;
12274
12275   if (GET_CODE (body) == SET)
12276     {
12277       rtx dest = SET_DEST (body);
12278       rtx target;
12279       unsigned int regno, tregno, endregno, endtregno;
12280
12281       if (GET_CODE (dest) == ZERO_EXTRACT)
12282         target = XEXP (dest, 0);
12283       else if (GET_CODE (dest) == STRICT_LOW_PART)
12284         target = SUBREG_REG (XEXP (dest, 0));
12285       else
12286         return 0;
12287
12288       if (GET_CODE (target) == SUBREG)
12289         target = SUBREG_REG (target);
12290
12291       if (!REG_P (target))
12292         return 0;
12293
12294       tregno = REGNO (target), regno = REGNO (x);
12295       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
12296         return target == x;
12297
12298       endtregno = end_hard_regno (GET_MODE (target), tregno);
12299       endregno = end_hard_regno (GET_MODE (x), regno);
12300
12301       return endregno > tregno && regno < endtregno;
12302     }
12303
12304   else if (GET_CODE (body) == PARALLEL)
12305     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
12306       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
12307         return 1;
12308
12309   return 0;
12310 }
12311 \f
12312 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
12313    as appropriate.  I3 and I2 are the insns resulting from the combination
12314    insns including FROM (I2 may be zero).
12315
12316    ELIM_I2 and ELIM_I1 are either zero or registers that we know will
12317    not need REG_DEAD notes because they are being substituted for.  This
12318    saves searching in the most common cases.
12319
12320    Each note in the list is either ignored or placed on some insns, depending
12321    on the type of note.  */
12322
12323 static void
12324 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
12325                   rtx elim_i1)
12326 {
12327   rtx note, next_note;
12328   rtx tem;
12329
12330   for (note = notes; note; note = next_note)
12331     {
12332       rtx place = 0, place2 = 0;
12333
12334       next_note = XEXP (note, 1);
12335       switch (REG_NOTE_KIND (note))
12336         {
12337         case REG_BR_PROB:
12338         case REG_BR_PRED:
12339           /* Doesn't matter much where we put this, as long as it's somewhere.
12340              It is preferable to keep these notes on branches, which is most
12341              likely to be i3.  */
12342           place = i3;
12343           break;
12344
12345         case REG_VALUE_PROFILE:
12346           /* Just get rid of this note, as it is unused later anyway.  */
12347           break;
12348
12349         case REG_NON_LOCAL_GOTO:
12350           if (JUMP_P (i3))
12351             place = i3;
12352           else
12353             {
12354               gcc_assert (i2 && JUMP_P (i2));
12355               place = i2;
12356             }
12357           break;
12358
12359         case REG_EH_REGION:
12360           /* These notes must remain with the call or trapping instruction.  */
12361           if (CALL_P (i3))
12362             place = i3;
12363           else if (i2 && CALL_P (i2))
12364             place = i2;
12365           else
12366             {
12367               gcc_assert (flag_non_call_exceptions);
12368               if (may_trap_p (i3))
12369                 place = i3;
12370               else if (i2 && may_trap_p (i2))
12371                 place = i2;
12372               /* ??? Otherwise assume we've combined things such that we
12373                  can now prove that the instructions can't trap.  Drop the
12374                  note in this case.  */
12375             }
12376           break;
12377
12378         case REG_NORETURN:
12379         case REG_SETJMP:
12380           /* These notes must remain with the call.  It should not be
12381              possible for both I2 and I3 to be a call.  */
12382           if (CALL_P (i3))
12383             place = i3;
12384           else
12385             {
12386               gcc_assert (i2 && CALL_P (i2));
12387               place = i2;
12388             }
12389           break;
12390
12391         case REG_UNUSED:
12392           /* Any clobbers for i3 may still exist, and so we must process
12393              REG_UNUSED notes from that insn.
12394
12395              Any clobbers from i2 or i1 can only exist if they were added by
12396              recog_for_combine.  In that case, recog_for_combine created the
12397              necessary REG_UNUSED notes.  Trying to keep any original
12398              REG_UNUSED notes from these insns can cause incorrect output
12399              if it is for the same register as the original i3 dest.
12400              In that case, we will notice that the register is set in i3,
12401              and then add a REG_UNUSED note for the destination of i3, which
12402              is wrong.  However, it is possible to have REG_UNUSED notes from
12403              i2 or i1 for register which were both used and clobbered, so
12404              we keep notes from i2 or i1 if they will turn into REG_DEAD
12405              notes.  */
12406
12407           /* If this register is set or clobbered in I3, put the note there
12408              unless there is one already.  */
12409           if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
12410             {
12411               if (from_insn != i3)
12412                 break;
12413
12414               if (! (REG_P (XEXP (note, 0))
12415                      ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
12416                      : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
12417                 place = i3;
12418             }
12419           /* Otherwise, if this register is used by I3, then this register
12420              now dies here, so we must put a REG_DEAD note here unless there
12421              is one already.  */
12422           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
12423                    && ! (REG_P (XEXP (note, 0))
12424                          ? find_regno_note (i3, REG_DEAD,
12425                                             REGNO (XEXP (note, 0)))
12426                          : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
12427             {
12428               PUT_REG_NOTE_KIND (note, REG_DEAD);
12429               place = i3;
12430             }
12431           break;
12432
12433         case REG_EQUAL:
12434         case REG_EQUIV:
12435         case REG_NOALIAS:
12436           /* These notes say something about results of an insn.  We can
12437              only support them if they used to be on I3 in which case they
12438              remain on I3.  Otherwise they are ignored.
12439
12440              If the note refers to an expression that is not a constant, we
12441              must also ignore the note since we cannot tell whether the
12442              equivalence is still true.  It might be possible to do
12443              slightly better than this (we only have a problem if I2DEST
12444              or I1DEST is present in the expression), but it doesn't
12445              seem worth the trouble.  */
12446
12447           if (from_insn == i3
12448               && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
12449             place = i3;
12450           break;
12451
12452         case REG_INC:
12453         case REG_NO_CONFLICT:
12454           /* These notes say something about how a register is used.  They must
12455              be present on any use of the register in I2 or I3.  */
12456           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
12457             place = i3;
12458
12459           if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
12460             {
12461               if (place)
12462                 place2 = i2;
12463               else
12464                 place = i2;
12465             }
12466           break;
12467
12468         case REG_LABEL_TARGET:
12469         case REG_LABEL_OPERAND:
12470           /* This can show up in several ways -- either directly in the
12471              pattern, or hidden off in the constant pool with (or without?)
12472              a REG_EQUAL note.  */
12473           /* ??? Ignore the without-reg_equal-note problem for now.  */
12474           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12475               || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12476                   && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12477                   && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12478             place = i3;
12479
12480           if (i2
12481               && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12482                   || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12483                       && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12484                       && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12485             {
12486               if (place)
12487                 place2 = i2;
12488               else
12489                 place = i2;
12490             }
12491
12492           /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
12493              as a JUMP_LABEL or decrement LABEL_NUSES if it's already
12494              there.  */
12495           if (place && JUMP_P (place)
12496               && REG_NOTE_KIND (note) == REG_LABEL_TARGET
12497               && (JUMP_LABEL (place) == NULL
12498                   || JUMP_LABEL (place) == XEXP (note, 0)))
12499             {
12500               rtx label = JUMP_LABEL (place);
12501
12502               if (!label)
12503                 JUMP_LABEL (place) = XEXP (note, 0);
12504               else if (LABEL_P (label))
12505                 LABEL_NUSES (label)--;
12506             }
12507
12508           if (place2 && JUMP_P (place2)
12509               && REG_NOTE_KIND (note) == REG_LABEL_TARGET
12510               && (JUMP_LABEL (place2) == NULL
12511                   || JUMP_LABEL (place2) == XEXP (note, 0)))
12512             {
12513               rtx label = JUMP_LABEL (place2);
12514
12515               if (!label)
12516                 JUMP_LABEL (place2) = XEXP (note, 0);
12517               else if (LABEL_P (label))
12518                 LABEL_NUSES (label)--;
12519               place2 = 0;
12520             }
12521           break;
12522
12523         case REG_NONNEG:
12524           /* This note says something about the value of a register prior
12525              to the execution of an insn.  It is too much trouble to see
12526              if the note is still correct in all situations.  It is better
12527              to simply delete it.  */
12528           break;
12529
12530         case REG_RETVAL:
12531           /* If the insn previously containing this note still exists,
12532              put it back where it was.  Otherwise move it to the previous
12533              insn.  Adjust the corresponding REG_LIBCALL note.  */
12534           if (!NOTE_P (from_insn))
12535             place = from_insn;
12536           else
12537             {
12538               tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
12539               place = prev_real_insn (from_insn);
12540               if (tem && place)
12541                 XEXP (tem, 0) = place;
12542               /* If we're deleting the last remaining instruction of a
12543                  libcall sequence, don't add the notes.  */
12544               else if (XEXP (note, 0) == from_insn)
12545                 tem = place = 0;
12546               /* Don't add the dangling REG_RETVAL note.  */
12547               else if (! tem)
12548                 place = 0;
12549             }
12550           break;
12551
12552         case REG_LIBCALL:
12553           /* This is handled similarly to REG_RETVAL.  */
12554           if (!NOTE_P (from_insn))
12555             place = from_insn;
12556           else
12557             {
12558               tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
12559               place = next_real_insn (from_insn);
12560               if (tem && place)
12561                 XEXP (tem, 0) = place;
12562               /* If we're deleting the last remaining instruction of a
12563                  libcall sequence, don't add the notes.  */
12564               else if (XEXP (note, 0) == from_insn)
12565                 tem = place = 0;
12566               /* Don't add the dangling REG_LIBCALL note.  */
12567               else if (! tem)
12568                 place = 0;
12569             }
12570           break;
12571
12572         case REG_DEAD:
12573           /* If we replaced the right hand side of FROM_INSN with a
12574              REG_EQUAL note, the original use of the dying register
12575              will not have been combined into I3 and I2.  In such cases,
12576              FROM_INSN is guaranteed to be the first of the combined
12577              instructions, so we simply need to search back before
12578              FROM_INSN for the previous use or set of this register,
12579              then alter the notes there appropriately.
12580
12581              If the register is used as an input in I3, it dies there.
12582              Similarly for I2, if it is nonzero and adjacent to I3.
12583
12584              If the register is not used as an input in either I3 or I2
12585              and it is not one of the registers we were supposed to eliminate,
12586              there are two possibilities.  We might have a non-adjacent I2
12587              or we might have somehow eliminated an additional register
12588              from a computation.  For example, we might have had A & B where
12589              we discover that B will always be zero.  In this case we will
12590              eliminate the reference to A.
12591
12592              In both cases, we must search to see if we can find a previous
12593              use of A and put the death note there.  */
12594
12595           if (from_insn
12596               && from_insn == i2mod
12597               && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
12598             tem = from_insn;
12599           else
12600             {
12601               if (from_insn
12602                   && CALL_P (from_insn)
12603                   && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12604                 place = from_insn;
12605               else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12606                 place = i3;
12607               else if (i2 != 0 && next_nonnote_insn (i2) == i3
12608                        && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12609                 place = i2;
12610               else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
12611                         && !(i2mod
12612                              && reg_overlap_mentioned_p (XEXP (note, 0),
12613                                                          i2mod_old_rhs)))
12614                        || rtx_equal_p (XEXP (note, 0), elim_i1))
12615                 break;
12616               tem = i3;
12617             }
12618
12619           if (place == 0)
12620             {
12621               basic_block bb = this_basic_block;
12622
12623               for (tem = PREV_INSN (tem); place == 0; tem = PREV_INSN (tem))
12624                 {
12625                   if (! INSN_P (tem))
12626                     {
12627                       if (tem == BB_HEAD (bb))
12628                         break;
12629                       continue;
12630                     }
12631
12632                   /* If the register is being set at TEM, see if that is all
12633                      TEM is doing.  If so, delete TEM.  Otherwise, make this
12634                      into a REG_UNUSED note instead. Don't delete sets to
12635                      global register vars.  */
12636                   if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
12637                        || !global_regs[REGNO (XEXP (note, 0))])
12638                       && reg_set_p (XEXP (note, 0), PATTERN (tem)))
12639                     {
12640                       rtx set = single_set (tem);
12641                       rtx inner_dest = 0;
12642 #ifdef HAVE_cc0
12643                       rtx cc0_setter = NULL_RTX;
12644 #endif
12645
12646                       if (set != 0)
12647                         for (inner_dest = SET_DEST (set);
12648                              (GET_CODE (inner_dest) == STRICT_LOW_PART
12649                               || GET_CODE (inner_dest) == SUBREG
12650                               || GET_CODE (inner_dest) == ZERO_EXTRACT);
12651                              inner_dest = XEXP (inner_dest, 0))
12652                           ;
12653
12654                       /* Verify that it was the set, and not a clobber that
12655                          modified the register.
12656
12657                          CC0 targets must be careful to maintain setter/user
12658                          pairs.  If we cannot delete the setter due to side
12659                          effects, mark the user with an UNUSED note instead
12660                          of deleting it.  */
12661
12662                       if (set != 0 && ! side_effects_p (SET_SRC (set))
12663                           && rtx_equal_p (XEXP (note, 0), inner_dest)
12664 #ifdef HAVE_cc0
12665                           && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12666                               || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12667                                   && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12668 #endif
12669                           )
12670                         {
12671                           /* Move the notes and links of TEM elsewhere.
12672                              This might delete other dead insns recursively.
12673                              First set the pattern to something that won't use
12674                              any register.  */
12675                           rtx old_notes = REG_NOTES (tem);
12676
12677                           PATTERN (tem) = pc_rtx;
12678                           REG_NOTES (tem) = NULL;
12679
12680                           distribute_notes (old_notes, tem, tem, NULL_RTX,
12681                                             NULL_RTX, NULL_RTX);
12682                           distribute_links (LOG_LINKS (tem));
12683
12684                           SET_INSN_DELETED (tem);
12685
12686 #ifdef HAVE_cc0
12687                           /* Delete the setter too.  */
12688                           if (cc0_setter)
12689                             {
12690                               PATTERN (cc0_setter) = pc_rtx;
12691                               old_notes = REG_NOTES (cc0_setter);
12692                               REG_NOTES (cc0_setter) = NULL;
12693
12694                               distribute_notes (old_notes, cc0_setter,
12695                                                 cc0_setter, NULL_RTX,
12696                                                 NULL_RTX, NULL_RTX);
12697                               distribute_links (LOG_LINKS (cc0_setter));
12698
12699                               SET_INSN_DELETED (cc0_setter);
12700                             }
12701 #endif
12702                         }
12703                       else
12704                         {
12705                           PUT_REG_NOTE_KIND (note, REG_UNUSED);
12706
12707                           /*  If there isn't already a REG_UNUSED note, put one
12708                               here.  Do not place a REG_DEAD note, even if
12709                               the register is also used here; that would not
12710                               match the algorithm used in lifetime analysis
12711                               and can cause the consistency check in the
12712                               scheduler to fail.  */
12713                           if (! find_regno_note (tem, REG_UNUSED,
12714                                                  REGNO (XEXP (note, 0))))
12715                             place = tem;
12716                           break;
12717                         }
12718                     }
12719                   else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12720                            || (CALL_P (tem)
12721                                && find_reg_fusage (tem, USE, XEXP (note, 0))))
12722                     {
12723                       place = tem;
12724
12725                       /* If we are doing a 3->2 combination, and we have a
12726                          register which formerly died in i3 and was not used
12727                          by i2, which now no longer dies in i3 and is used in
12728                          i2 but does not die in i2, and place is between i2
12729                          and i3, then we may need to move a link from place to
12730                          i2.  */
12731                       if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
12732                           && from_insn
12733                           && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
12734                           && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12735                         {
12736                           rtx links = LOG_LINKS (place);
12737                           LOG_LINKS (place) = 0;
12738                           distribute_links (links);
12739                         }
12740                       break;
12741                     }
12742
12743                   if (tem == BB_HEAD (bb))
12744                     break;
12745                 }
12746
12747             }
12748
12749           /* If the register is set or already dead at PLACE, we needn't do
12750              anything with this note if it is still a REG_DEAD note.
12751              We check here if it is set at all, not if is it totally replaced,
12752              which is what `dead_or_set_p' checks, so also check for it being
12753              set partially.  */
12754
12755           if (place && REG_NOTE_KIND (note) == REG_DEAD)
12756             {
12757               unsigned int regno = REGNO (XEXP (note, 0));
12758               reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
12759
12760               if (dead_or_set_p (place, XEXP (note, 0))
12761                   || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12762                 {
12763                   /* Unless the register previously died in PLACE, clear
12764                      last_death.  [I no longer understand why this is
12765                      being done.] */
12766                   if (rsp->last_death != place)
12767                     rsp->last_death = 0;
12768                   place = 0;
12769                 }
12770               else
12771                 rsp->last_death = place;
12772
12773               /* If this is a death note for a hard reg that is occupying
12774                  multiple registers, ensure that we are still using all
12775                  parts of the object.  If we find a piece of the object
12776                  that is unused, we must arrange for an appropriate REG_DEAD
12777                  note to be added for it.  However, we can't just emit a USE
12778                  and tag the note to it, since the register might actually
12779                  be dead; so we recourse, and the recursive call then finds
12780                  the previous insn that used this register.  */
12781
12782               if (place && regno < FIRST_PSEUDO_REGISTER
12783                   && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
12784                 {
12785                   unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
12786                   int all_used = 1;
12787                   unsigned int i;
12788
12789                   for (i = regno; i < endregno; i++)
12790                     if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12791                          && ! find_regno_fusage (place, USE, i))
12792                         || dead_or_set_regno_p (place, i))
12793                       all_used = 0;
12794
12795                   if (! all_used)
12796                     {
12797                       /* Put only REG_DEAD notes for pieces that are
12798                          not already dead or set.  */
12799
12800                       for (i = regno; i < endregno;
12801                            i += hard_regno_nregs[i][reg_raw_mode[i]])
12802                         {
12803                           rtx piece = regno_reg_rtx[i];
12804                           basic_block bb = this_basic_block;
12805
12806                           if (! dead_or_set_p (place, piece)
12807                               && ! reg_bitfield_target_p (piece,
12808                                                           PATTERN (place)))
12809                             {
12810                               rtx new_note
12811                                 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12812
12813                               distribute_notes (new_note, place, place,
12814                                                 NULL_RTX, NULL_RTX, NULL_RTX);
12815                             }
12816                           else if (! refers_to_regno_p (i, i + 1,
12817                                                         PATTERN (place), 0)
12818                                    && ! find_regno_fusage (place, USE, i))
12819                             for (tem = PREV_INSN (place); ;
12820                                  tem = PREV_INSN (tem))
12821                               {
12822                                 if (! INSN_P (tem))
12823                                   {
12824                                     if (tem == BB_HEAD (bb))
12825                                       break;
12826                                     continue;
12827                                   }
12828                                 if (dead_or_set_p (tem, piece)
12829                                     || reg_bitfield_target_p (piece,
12830                                                               PATTERN (tem)))
12831                                   {
12832                                     REG_NOTES (tem)
12833                                       = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
12834                                                            REG_NOTES (tem));
12835                                     break;
12836                                   }
12837                               }
12838
12839                         }
12840
12841                       place = 0;
12842                     }
12843                 }
12844             }
12845           break;
12846
12847         default:
12848           /* Any other notes should not be present at this point in the
12849              compilation.  */
12850           gcc_unreachable ();
12851         }
12852
12853       if (place)
12854         {
12855           XEXP (note, 1) = REG_NOTES (place);
12856           REG_NOTES (place) = note;
12857         }
12858
12859       if (place2)
12860         REG_NOTES (place2) 
12861           = gen_rtx_fmt_ee (GET_CODE (note), REG_NOTE_KIND (note),
12862                             XEXP (note, 0), REG_NOTES (place2));
12863     }
12864 }
12865 \f
12866 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12867    I3, I2, and I1 to new locations.  This is also called to add a link
12868    pointing at I3 when I3's destination is changed.  */
12869
12870 static void
12871 distribute_links (rtx links)
12872 {
12873   rtx link, next_link;
12874
12875   for (link = links; link; link = next_link)
12876     {
12877       rtx place = 0;
12878       rtx insn;
12879       rtx set, reg;
12880
12881       next_link = XEXP (link, 1);
12882
12883       /* If the insn that this link points to is a NOTE or isn't a single
12884          set, ignore it.  In the latter case, it isn't clear what we
12885          can do other than ignore the link, since we can't tell which
12886          register it was for.  Such links wouldn't be used by combine
12887          anyway.
12888
12889          It is not possible for the destination of the target of the link to
12890          have been changed by combine.  The only potential of this is if we
12891          replace I3, I2, and I1 by I3 and I2.  But in that case the
12892          destination of I2 also remains unchanged.  */
12893
12894       if (NOTE_P (XEXP (link, 0))
12895           || (set = single_set (XEXP (link, 0))) == 0)
12896         continue;
12897
12898       reg = SET_DEST (set);
12899       while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12900              || GET_CODE (reg) == STRICT_LOW_PART)
12901         reg = XEXP (reg, 0);
12902
12903       /* A LOG_LINK is defined as being placed on the first insn that uses
12904          a register and points to the insn that sets the register.  Start
12905          searching at the next insn after the target of the link and stop
12906          when we reach a set of the register or the end of the basic block.
12907
12908          Note that this correctly handles the link that used to point from
12909          I3 to I2.  Also note that not much searching is typically done here
12910          since most links don't point very far away.  */
12911
12912       for (insn = NEXT_INSN (XEXP (link, 0));
12913            (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
12914                      || BB_HEAD (this_basic_block->next_bb) != insn));
12915            insn = NEXT_INSN (insn))
12916         if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12917           {
12918             if (reg_referenced_p (reg, PATTERN (insn)))
12919               place = insn;
12920             break;
12921           }
12922         else if (CALL_P (insn)
12923                  && find_reg_fusage (insn, USE, reg))
12924           {
12925             place = insn;
12926             break;
12927           }
12928         else if (INSN_P (insn) && reg_set_p (reg, insn))
12929           break;
12930
12931       /* If we found a place to put the link, place it there unless there
12932          is already a link to the same insn as LINK at that point.  */
12933
12934       if (place)
12935         {
12936           rtx link2;
12937
12938           for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12939             if (XEXP (link2, 0) == XEXP (link, 0))
12940               break;
12941
12942           if (link2 == 0)
12943             {
12944               XEXP (link, 1) = LOG_LINKS (place);
12945               LOG_LINKS (place) = link;
12946
12947               /* Set added_links_insn to the earliest insn we added a
12948                  link to.  */
12949               if (added_links_insn == 0
12950                   || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
12951                 added_links_insn = place;
12952             }
12953         }
12954     }
12955 }
12956 \f
12957 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
12958    Check whether the expression pointer to by LOC is a register or
12959    memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
12960    Otherwise return zero.  */
12961
12962 static int
12963 unmentioned_reg_p_1 (rtx *loc, void *expr)
12964 {
12965   rtx x = *loc;
12966
12967   if (x != NULL_RTX
12968       && (REG_P (x) || MEM_P (x))
12969       && ! reg_mentioned_p (x, (rtx) expr))
12970     return 1;
12971   return 0;
12972 }
12973
12974 /* Check for any register or memory mentioned in EQUIV that is not
12975    mentioned in EXPR.  This is used to restrict EQUIV to "specializations"
12976    of EXPR where some registers may have been replaced by constants.  */
12977
12978 static bool
12979 unmentioned_reg_p (rtx equiv, rtx expr)
12980 {
12981   return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
12982 }
12983 \f
12984 void
12985 dump_combine_stats (FILE *file)
12986 {
12987   fprintf
12988     (file,
12989      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12990      combine_attempts, combine_merges, combine_extras, combine_successes);
12991 }
12992
12993 void
12994 dump_combine_total_stats (FILE *file)
12995 {
12996   fprintf
12997     (file,
12998      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12999      total_attempts, total_merges, total_extras, total_successes);
13000 }
13001 \f
13002 static bool
13003 gate_handle_combine (void)
13004 {
13005   return (optimize > 0);
13006 }
13007
13008 /* Try combining insns through substitution.  */
13009 static unsigned int
13010 rest_of_handle_combine (void)
13011 {
13012   int rebuild_jump_labels_after_combine;
13013
13014   df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
13015   df_note_add_problem ();
13016   df_analyze ();
13017
13018   regstat_init_n_sets_and_refs ();
13019
13020   rebuild_jump_labels_after_combine
13021     = combine_instructions (get_insns (), max_reg_num ());
13022
13023   /* Combining insns may have turned an indirect jump into a
13024      direct jump.  Rebuild the JUMP_LABEL fields of jumping
13025      instructions.  */
13026   if (rebuild_jump_labels_after_combine)
13027     {
13028       timevar_push (TV_JUMP);
13029       rebuild_jump_labels (get_insns ());
13030       cleanup_cfg (0);
13031       timevar_pop (TV_JUMP);
13032     }
13033
13034   regstat_free_n_sets_and_refs ();
13035   return 0;
13036 }
13037
13038 struct rtl_opt_pass pass_combine =
13039 {
13040  {
13041   RTL_PASS,
13042   "combine",                            /* name */
13043   gate_handle_combine,                  /* gate */
13044   rest_of_handle_combine,               /* execute */
13045   NULL,                                 /* sub */
13046   NULL,                                 /* next */
13047   0,                                    /* static_pass_number */
13048   TV_COMBINE,                           /* tv_id */
13049   0,                                    /* properties_required */
13050   0,                                    /* properties_provided */
13051   0,                                    /* properties_destroyed */
13052   0,                                    /* todo_flags_start */
13053   TODO_dump_func |
13054   TODO_df_finish | TODO_verify_rtl_sharing |
13055   TODO_ggc_collect,                     /* todo_flags_finish */
13056  }
13057 };
13058