OSDN Git Service

5b68bab12d796c32598fc948bc0d03fde917f165
[pf3gnuchains/gcc-fork.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4    2011 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /* This module is essentially the "combiner" phase of the U. of Arizona
23    Portable Optimizer, but redone to work on our list-structured
24    representation for RTL instead of their string representation.
25
26    The LOG_LINKS of each insn identify the most recent assignment
27    to each REG used in the insn.  It is a list of previous insns,
28    each of which contains a SET for a REG that is used in this insn
29    and not used or set in between.  LOG_LINKs never cross basic blocks.
30    They were set up by the preceding pass (lifetime analysis).
31
32    We try to combine each pair of insns joined by a logical link.
33    We also try to combine triples of insns A, B and C when
34    C has a link back to B and B has a link back to A.
35
36    LOG_LINKS does not have links for use of the CC0.  They don't
37    need to, because the insn that sets the CC0 is always immediately
38    before the insn that tests it.  So we always regard a branch
39    insn as having a logical link to the preceding insn.  The same is true
40    for an insn explicitly using CC0.
41
42    We check (with use_crosses_set_p) to avoid combining in such a way
43    as to move a computation to a place where its value would be different.
44
45    Combination is done by mathematically substituting the previous
46    insn(s) values for the regs they set into the expressions in
47    the later insns that refer to these regs.  If the result is a valid insn
48    for our target machine, according to the machine description,
49    we install it, delete the earlier insns, and update the data flow
50    information (LOG_LINKS and REG_NOTES) for what we did.
51
52    There are a few exceptions where the dataflow information isn't
53    completely updated (however this is only a local issue since it is
54    regenerated before the next pass that uses it):
55
56    - reg_live_length is not updated
57    - reg_n_refs is not adjusted in the rare case when a register is
58      no longer required in a computation
59    - there are extremely rare cases (see distribute_notes) when a
60      REG_DEAD note is lost
61    - a LOG_LINKS entry that refers to an insn with multiple SETs may be
62      removed because there is no way to know which register it was
63      linking
64
65    To simplify substitution, we combine only when the earlier insn(s)
66    consist of only a single assignment.  To simplify updating afterward,
67    we never combine when a subroutine call appears in the middle.
68
69    Since we do not represent assignments to CC0 explicitly except when that
70    is all an insn does, there is no LOG_LINKS entry in an insn that uses
71    the condition code for the insn that set the condition code.
72    Fortunately, these two insns must be consecutive.
73    Therefore, every JUMP_INSN is taken to have an implicit logical link
74    to the preceding insn.  This is not quite right, since non-jumps can
75    also use the condition code; but in practice such insns would not
76    combine anyway.  */
77
78 #include "config.h"
79 #include "system.h"
80 #include "coretypes.h"
81 #include "tm.h"
82 #include "rtl.h"
83 #include "tree.h"
84 #include "tm_p.h"
85 #include "flags.h"
86 #include "regs.h"
87 #include "hard-reg-set.h"
88 #include "basic-block.h"
89 #include "insn-config.h"
90 #include "function.h"
91 /* Include expr.h after insn-config.h so we get HAVE_conditional_move.  */
92 #include "expr.h"
93 #include "insn-attr.h"
94 #include "recog.h"
95 #include "diagnostic-core.h"
96 #include "target.h"
97 #include "optabs.h"
98 #include "insn-codes.h"
99 #include "rtlhooks-def.h"
100 /* Include output.h for dump_file.  */
101 #include "output.h"
102 #include "params.h"
103 #include "timevar.h"
104 #include "tree-pass.h"
105 #include "df.h"
106 #include "cgraph.h"
107 #include "obstack.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 static bool optimize_this_for_speed_p;
301
302 \f
303 /* Length of the currently allocated uid_insn_cost array.  */
304
305 static int max_uid_known;
306
307 /* The following array records the insn_rtx_cost for every insn
308    in the instruction stream.  */
309
310 static int *uid_insn_cost;
311
312 /* The following array records the LOG_LINKS for every insn in the
313    instruction stream as struct insn_link pointers.  */
314
315 struct insn_link {
316   rtx insn;
317   struct insn_link *next;
318 };
319
320 static struct insn_link **uid_log_links;
321
322 #define INSN_COST(INSN)         (uid_insn_cost[INSN_UID (INSN)])
323 #define LOG_LINKS(INSN)         (uid_log_links[INSN_UID (INSN)])
324
325 #define FOR_EACH_LOG_LINK(L, INSN)                              \
326   for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
327
328 /* Links for LOG_LINKS are allocated from this obstack.  */
329
330 static struct obstack insn_link_obstack;
331
332 /* Allocate a link.  */
333
334 static inline struct insn_link *
335 alloc_insn_link (rtx insn, struct insn_link *next)
336 {
337   struct insn_link *l
338     = (struct insn_link *) obstack_alloc (&insn_link_obstack,
339                                           sizeof (struct insn_link));
340   l->insn = insn;
341   l->next = next;
342   return l;
343 }
344
345 /* Incremented for each basic block.  */
346
347 static int label_tick;
348
349 /* Reset to label_tick for each extended basic block in scanning order.  */
350
351 static int label_tick_ebb_start;
352
353 /* Mode used to compute significance in reg_stat[].nonzero_bits.  It is the
354    largest integer mode that can fit in HOST_BITS_PER_WIDE_INT.  */
355
356 static enum machine_mode nonzero_bits_mode;
357
358 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
359    be safely used.  It is zero while computing them and after combine has
360    completed.  This former test prevents propagating values based on
361    previously set values, which can be incorrect if a variable is modified
362    in a loop.  */
363
364 static int nonzero_sign_valid;
365
366 \f
367 /* Record one modification to rtl structure
368    to be undone by storing old_contents into *where.  */
369
370 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE };
371
372 struct undo
373 {
374   struct undo *next;
375   enum undo_kind kind;
376   union { rtx r; int i; enum machine_mode m; } old_contents;
377   union { rtx *r; int *i; } where;
378 };
379
380 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
381    num_undo says how many are currently recorded.
382
383    other_insn is nonzero if we have modified some other insn in the process
384    of working on subst_insn.  It must be verified too.  */
385
386 struct undobuf
387 {
388   struct undo *undos;
389   struct undo *frees;
390   rtx other_insn;
391 };
392
393 static struct undobuf undobuf;
394
395 /* Number of times the pseudo being substituted for
396    was found and replaced.  */
397
398 static int n_occurrences;
399
400 static rtx reg_nonzero_bits_for_combine (const_rtx, enum machine_mode, const_rtx,
401                                          enum machine_mode,
402                                          unsigned HOST_WIDE_INT,
403                                          unsigned HOST_WIDE_INT *);
404 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, enum machine_mode, const_rtx,
405                                                 enum machine_mode,
406                                                 unsigned int, unsigned int *);
407 static void do_SUBST (rtx *, rtx);
408 static void do_SUBST_INT (int *, int);
409 static void init_reg_last (void);
410 static void setup_incoming_promotions (rtx);
411 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
412 static int cant_combine_insn_p (rtx);
413 static int can_combine_p (rtx, rtx, rtx, rtx, rtx, rtx, rtx *, rtx *);
414 static int combinable_i3pat (rtx, rtx *, rtx, rtx, rtx, int, int, rtx *);
415 static int contains_muldiv (rtx);
416 static rtx try_combine (rtx, rtx, rtx, rtx, int *, rtx);
417 static void undo_all (void);
418 static void undo_commit (void);
419 static rtx *find_split_point (rtx *, rtx, bool);
420 static rtx subst (rtx, rtx, rtx, int, int, int);
421 static rtx combine_simplify_rtx (rtx, enum machine_mode, int, int);
422 static rtx simplify_if_then_else (rtx);
423 static rtx simplify_set (rtx);
424 static rtx simplify_logical (rtx);
425 static rtx expand_compound_operation (rtx);
426 static const_rtx expand_field_assignment (const_rtx);
427 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
428                             rtx, unsigned HOST_WIDE_INT, int, int, int);
429 static rtx extract_left_shift (rtx, int);
430 static rtx make_compound_operation (rtx, enum rtx_code);
431 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
432                               unsigned HOST_WIDE_INT *);
433 static rtx canon_reg_for_combine (rtx, rtx);
434 static rtx force_to_mode (rtx, enum machine_mode,
435                           unsigned HOST_WIDE_INT, int);
436 static rtx if_then_else_cond (rtx, rtx *, rtx *);
437 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
438 static int rtx_equal_for_field_assignment_p (rtx, rtx);
439 static rtx make_field_assignment (rtx);
440 static rtx apply_distributive_law (rtx);
441 static rtx distribute_and_simplify_rtx (rtx, int);
442 static rtx simplify_and_const_int_1 (enum machine_mode, rtx,
443                                      unsigned HOST_WIDE_INT);
444 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
445                                    unsigned HOST_WIDE_INT);
446 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
447                             HOST_WIDE_INT, enum machine_mode, int *);
448 static rtx simplify_shift_const_1 (enum rtx_code, enum machine_mode, rtx, int);
449 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
450                                  int);
451 static int recog_for_combine (rtx *, rtx, rtx *);
452 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
453 static enum rtx_code simplify_compare_const (enum rtx_code, rtx, rtx *);
454 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
455 static void update_table_tick (rtx);
456 static void record_value_for_reg (rtx, rtx, rtx);
457 static void check_promoted_subreg (rtx, rtx);
458 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
459 static void record_dead_and_set_regs (rtx);
460 static int get_last_value_validate (rtx *, rtx, int, int);
461 static rtx get_last_value (const_rtx);
462 static int use_crosses_set_p (const_rtx, int);
463 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
464 static int reg_dead_at_p (rtx, rtx);
465 static void move_deaths (rtx, rtx, int, rtx, rtx *);
466 static int reg_bitfield_target_p (rtx, rtx);
467 static void distribute_notes (rtx, rtx, rtx, rtx, rtx, rtx, rtx);
468 static void distribute_links (struct insn_link *);
469 static void mark_used_regs_combine (rtx);
470 static void record_promoted_value (rtx, rtx);
471 static int unmentioned_reg_p_1 (rtx *, void *);
472 static bool unmentioned_reg_p (rtx, rtx);
473 static int record_truncated_value (rtx *, void *);
474 static void record_truncated_values (rtx *, void *);
475 static bool reg_truncated_to_mode (enum machine_mode, const_rtx);
476 static rtx gen_lowpart_or_truncate (enum machine_mode, rtx);
477 \f
478
479 /* It is not safe to use ordinary gen_lowpart in combine.
480    See comments in gen_lowpart_for_combine.  */
481 #undef RTL_HOOKS_GEN_LOWPART
482 #define RTL_HOOKS_GEN_LOWPART              gen_lowpart_for_combine
483
484 /* Our implementation of gen_lowpart never emits a new pseudo.  */
485 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
486 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT      gen_lowpart_for_combine
487
488 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
489 #define RTL_HOOKS_REG_NONZERO_REG_BITS     reg_nonzero_bits_for_combine
490
491 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
492 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES  reg_num_sign_bit_copies_for_combine
493
494 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
495 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE    reg_truncated_to_mode
496
497 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
498
499 \f
500 /* Try to split PATTERN found in INSN.  This returns NULL_RTX if
501    PATTERN can not be split.  Otherwise, it returns an insn sequence.
502    This is a wrapper around split_insns which ensures that the
503    reg_stat vector is made larger if the splitter creates a new
504    register.  */
505
506 static rtx
507 combine_split_insns (rtx pattern, rtx insn)
508 {
509   rtx ret;
510   unsigned int nregs;
511
512   ret = split_insns (pattern, insn);
513   nregs = max_reg_num ();
514   if (nregs > VEC_length (reg_stat_type, reg_stat))
515     VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
516   return ret;
517 }
518
519 /* This is used by find_single_use to locate an rtx in LOC that
520    contains exactly one use of DEST, which is typically either a REG
521    or CC0.  It returns a pointer to the innermost rtx expression
522    containing DEST.  Appearances of DEST that are being used to
523    totally replace it are not counted.  */
524
525 static rtx *
526 find_single_use_1 (rtx dest, rtx *loc)
527 {
528   rtx x = *loc;
529   enum rtx_code code = GET_CODE (x);
530   rtx *result = NULL;
531   rtx *this_result;
532   int i;
533   const char *fmt;
534
535   switch (code)
536     {
537     case CONST_INT:
538     case CONST:
539     case LABEL_REF:
540     case SYMBOL_REF:
541     case CONST_DOUBLE:
542     case CONST_VECTOR:
543     case CLOBBER:
544       return 0;
545
546     case SET:
547       /* If the destination is anything other than CC0, PC, a REG or a SUBREG
548          of a REG that occupies all of the REG, the insn uses DEST if
549          it is mentioned in the destination or the source.  Otherwise, we
550          need just check the source.  */
551       if (GET_CODE (SET_DEST (x)) != CC0
552           && GET_CODE (SET_DEST (x)) != PC
553           && !REG_P (SET_DEST (x))
554           && ! (GET_CODE (SET_DEST (x)) == SUBREG
555                 && REG_P (SUBREG_REG (SET_DEST (x)))
556                 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
557                       + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
558                     == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
559                          + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
560         break;
561
562       return find_single_use_1 (dest, &SET_SRC (x));
563
564     case MEM:
565     case SUBREG:
566       return find_single_use_1 (dest, &XEXP (x, 0));
567
568     default:
569       break;
570     }
571
572   /* If it wasn't one of the common cases above, check each expression and
573      vector of this code.  Look for a unique usage of DEST.  */
574
575   fmt = GET_RTX_FORMAT (code);
576   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
577     {
578       if (fmt[i] == 'e')
579         {
580           if (dest == XEXP (x, i)
581               || (REG_P (dest) && REG_P (XEXP (x, i))
582                   && REGNO (dest) == REGNO (XEXP (x, i))))
583             this_result = loc;
584           else
585             this_result = find_single_use_1 (dest, &XEXP (x, i));
586
587           if (result == NULL)
588             result = this_result;
589           else if (this_result)
590             /* Duplicate usage.  */
591             return NULL;
592         }
593       else if (fmt[i] == 'E')
594         {
595           int j;
596
597           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
598             {
599               if (XVECEXP (x, i, j) == dest
600                   || (REG_P (dest)
601                       && REG_P (XVECEXP (x, i, j))
602                       && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
603                 this_result = loc;
604               else
605                 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
606
607               if (result == NULL)
608                 result = this_result;
609               else if (this_result)
610                 return NULL;
611             }
612         }
613     }
614
615   return result;
616 }
617
618
619 /* See if DEST, produced in INSN, is used only a single time in the
620    sequel.  If so, return a pointer to the innermost rtx expression in which
621    it is used.
622
623    If PLOC is nonzero, *PLOC is set to the insn containing the single use.
624
625    If DEST is cc0_rtx, we look only at the next insn.  In that case, we don't
626    care about REG_DEAD notes or LOG_LINKS.
627
628    Otherwise, we find the single use by finding an insn that has a
629    LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST.  If DEST is
630    only referenced once in that insn, we know that it must be the first
631    and last insn referencing DEST.  */
632
633 static rtx *
634 find_single_use (rtx dest, rtx insn, rtx *ploc)
635 {
636   basic_block bb;
637   rtx next;
638   rtx *result;
639   struct insn_link *link;
640
641 #ifdef HAVE_cc0
642   if (dest == cc0_rtx)
643     {
644       next = NEXT_INSN (insn);
645       if (next == 0
646           || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
647         return 0;
648
649       result = find_single_use_1 (dest, &PATTERN (next));
650       if (result && ploc)
651         *ploc = next;
652       return result;
653     }
654 #endif
655
656   if (!REG_P (dest))
657     return 0;
658
659   bb = BLOCK_FOR_INSN (insn);
660   for (next = NEXT_INSN (insn);
661        next && BLOCK_FOR_INSN (next) == bb;
662        next = NEXT_INSN (next))
663     if (INSN_P (next) && dead_or_set_p (next, dest))
664       {
665         FOR_EACH_LOG_LINK (link, next)
666           if (link->insn == insn)
667             break;
668
669         if (link)
670           {
671             result = find_single_use_1 (dest, &PATTERN (next));
672             if (ploc)
673               *ploc = next;
674             return result;
675           }
676       }
677
678   return 0;
679 }
680 \f
681 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
682    insn.  The substitution can be undone by undo_all.  If INTO is already
683    set to NEWVAL, do not record this change.  Because computing NEWVAL might
684    also call SUBST, we have to compute it before we put anything into
685    the undo table.  */
686
687 static void
688 do_SUBST (rtx *into, rtx newval)
689 {
690   struct undo *buf;
691   rtx oldval = *into;
692
693   if (oldval == newval)
694     return;
695
696   /* We'd like to catch as many invalid transformations here as
697      possible.  Unfortunately, there are way too many mode changes
698      that are perfectly valid, so we'd waste too much effort for
699      little gain doing the checks here.  Focus on catching invalid
700      transformations involving integer constants.  */
701   if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
702       && CONST_INT_P (newval))
703     {
704       /* Sanity check that we're replacing oldval with a CONST_INT
705          that is a valid sign-extension for the original mode.  */
706       gcc_assert (INTVAL (newval)
707                   == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
708
709       /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
710          CONST_INT is not valid, because after the replacement, the
711          original mode would be gone.  Unfortunately, we can't tell
712          when do_SUBST is called to replace the operand thereof, so we
713          perform this test on oldval instead, checking whether an
714          invalid replacement took place before we got here.  */
715       gcc_assert (!(GET_CODE (oldval) == SUBREG
716                     && CONST_INT_P (SUBREG_REG (oldval))));
717       gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
718                     && CONST_INT_P (XEXP (oldval, 0))));
719     }
720
721   if (undobuf.frees)
722     buf = undobuf.frees, undobuf.frees = buf->next;
723   else
724     buf = XNEW (struct undo);
725
726   buf->kind = UNDO_RTX;
727   buf->where.r = into;
728   buf->old_contents.r = oldval;
729   *into = newval;
730
731   buf->next = undobuf.undos, undobuf.undos = buf;
732 }
733
734 #define SUBST(INTO, NEWVAL)     do_SUBST(&(INTO), (NEWVAL))
735
736 /* Similar to SUBST, but NEWVAL is an int expression.  Note that substitution
737    for the value of a HOST_WIDE_INT value (including CONST_INT) is
738    not safe.  */
739
740 static void
741 do_SUBST_INT (int *into, int newval)
742 {
743   struct undo *buf;
744   int oldval = *into;
745
746   if (oldval == newval)
747     return;
748
749   if (undobuf.frees)
750     buf = undobuf.frees, undobuf.frees = buf->next;
751   else
752     buf = XNEW (struct undo);
753
754   buf->kind = UNDO_INT;
755   buf->where.i = into;
756   buf->old_contents.i = oldval;
757   *into = newval;
758
759   buf->next = undobuf.undos, undobuf.undos = buf;
760 }
761
762 #define SUBST_INT(INTO, NEWVAL)  do_SUBST_INT(&(INTO), (NEWVAL))
763
764 /* Similar to SUBST, but just substitute the mode.  This is used when
765    changing the mode of a pseudo-register, so that any other
766    references to the entry in the regno_reg_rtx array will change as
767    well.  */
768
769 static void
770 do_SUBST_MODE (rtx *into, enum machine_mode newval)
771 {
772   struct undo *buf;
773   enum machine_mode oldval = GET_MODE (*into);
774
775   if (oldval == newval)
776     return;
777
778   if (undobuf.frees)
779     buf = undobuf.frees, undobuf.frees = buf->next;
780   else
781     buf = XNEW (struct undo);
782
783   buf->kind = UNDO_MODE;
784   buf->where.r = into;
785   buf->old_contents.m = oldval;
786   adjust_reg_mode (*into, newval);
787
788   buf->next = undobuf.undos, undobuf.undos = buf;
789 }
790
791 #define SUBST_MODE(INTO, NEWVAL)  do_SUBST_MODE(&(INTO), (NEWVAL))
792 \f
793 /* Subroutine of try_combine.  Determine whether the replacement patterns
794    NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_rtx_cost
795    than the original sequence I0, I1, I2, I3 and undobuf.other_insn.  Note
796    that I0, I1 and/or NEWI2PAT may be NULL_RTX.  Similarly, NEWOTHERPAT and
797    undobuf.other_insn may also both be NULL_RTX.  Return false if the cost
798    of all the instructions can be estimated and the replacements are more
799    expensive than the original sequence.  */
800
801 static bool
802 combine_validate_cost (rtx i0, rtx i1, rtx i2, rtx i3, rtx newpat,
803                        rtx newi2pat, rtx newotherpat)
804 {
805   int i0_cost, i1_cost, i2_cost, i3_cost;
806   int new_i2_cost, new_i3_cost;
807   int old_cost, new_cost;
808
809   /* Lookup the original insn_rtx_costs.  */
810   i2_cost = INSN_COST (i2);
811   i3_cost = INSN_COST (i3);
812
813   if (i1)
814     {
815       i1_cost = INSN_COST (i1);
816       if (i0)
817         {
818           i0_cost = INSN_COST (i0);
819           old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
820                       ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
821         }
822       else
823         {
824           old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
825                       ? i1_cost + i2_cost + i3_cost : 0);
826           i0_cost = 0;
827         }
828     }
829   else
830     {
831       old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
832       i1_cost = i0_cost = 0;
833     }
834
835   /* Calculate the replacement insn_rtx_costs.  */
836   new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
837   if (newi2pat)
838     {
839       new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
840       new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
841                  ? new_i2_cost + new_i3_cost : 0;
842     }
843   else
844     {
845       new_cost = new_i3_cost;
846       new_i2_cost = 0;
847     }
848
849   if (undobuf.other_insn)
850     {
851       int old_other_cost, new_other_cost;
852
853       old_other_cost = INSN_COST (undobuf.other_insn);
854       new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
855       if (old_other_cost > 0 && new_other_cost > 0)
856         {
857           old_cost += old_other_cost;
858           new_cost += new_other_cost;
859         }
860       else
861         old_cost = 0;
862     }
863
864   /* Disallow this combination if both new_cost and old_cost are greater than
865      zero, and new_cost is greater than old cost.  */
866   if (old_cost > 0 && new_cost > old_cost)
867     {
868       if (dump_file)
869         {
870           if (i0)
871             {
872               fprintf (dump_file,
873                        "rejecting combination of insns %d, %d, %d and %d\n",
874                        INSN_UID (i0), INSN_UID (i1), INSN_UID (i2),
875                        INSN_UID (i3));
876               fprintf (dump_file, "original costs %d + %d + %d + %d = %d\n",
877                        i0_cost, i1_cost, i2_cost, i3_cost, old_cost);
878             }
879           else if (i1)
880             {
881               fprintf (dump_file,
882                        "rejecting combination of insns %d, %d and %d\n",
883                        INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
884               fprintf (dump_file, "original costs %d + %d + %d = %d\n",
885                        i1_cost, i2_cost, i3_cost, old_cost);
886             }
887           else
888             {
889               fprintf (dump_file,
890                        "rejecting combination of insns %d and %d\n",
891                        INSN_UID (i2), INSN_UID (i3));
892               fprintf (dump_file, "original costs %d + %d = %d\n",
893                        i2_cost, i3_cost, old_cost);
894             }
895
896           if (newi2pat)
897             {
898               fprintf (dump_file, "replacement costs %d + %d = %d\n",
899                        new_i2_cost, new_i3_cost, new_cost);
900             }
901           else
902             fprintf (dump_file, "replacement cost %d\n", new_cost);
903         }
904
905       return false;
906     }
907
908   /* Update the uid_insn_cost array with the replacement costs.  */
909   INSN_COST (i2) = new_i2_cost;
910   INSN_COST (i3) = new_i3_cost;
911   if (i1)
912     {
913       INSN_COST (i1) = 0;
914       if (i0)
915         INSN_COST (i0) = 0;
916     }
917
918   return true;
919 }
920
921
922 /* Delete any insns that copy a register to itself.  */
923
924 static void
925 delete_noop_moves (void)
926 {
927   rtx insn, next;
928   basic_block bb;
929
930   FOR_EACH_BB (bb)
931     {
932       for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
933         {
934           next = NEXT_INSN (insn);
935           if (INSN_P (insn) && noop_move_p (insn))
936             {
937               if (dump_file)
938                 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
939
940               delete_insn_and_edges (insn);
941             }
942         }
943     }
944 }
945
946 \f
947 /* Fill in log links field for all insns.  */
948
949 static void
950 create_log_links (void)
951 {
952   basic_block bb;
953   rtx *next_use, insn;
954   df_ref *def_vec, *use_vec;
955
956   next_use = XCNEWVEC (rtx, max_reg_num ());
957
958   /* Pass through each block from the end, recording the uses of each
959      register and establishing log links when def is encountered.
960      Note that we do not clear next_use array in order to save time,
961      so we have to test whether the use is in the same basic block as def.
962
963      There are a few cases below when we do not consider the definition or
964      usage -- these are taken from original flow.c did. Don't ask me why it is
965      done this way; I don't know and if it works, I don't want to know.  */
966
967   FOR_EACH_BB (bb)
968     {
969       FOR_BB_INSNS_REVERSE (bb, insn)
970         {
971           if (!NONDEBUG_INSN_P (insn))
972             continue;
973
974           /* Log links are created only once.  */
975           gcc_assert (!LOG_LINKS (insn));
976
977           for (def_vec = DF_INSN_DEFS (insn); *def_vec; def_vec++)
978             {
979               df_ref def = *def_vec;
980               int regno = DF_REF_REGNO (def);
981               rtx use_insn;
982
983               if (!next_use[regno])
984                 continue;
985
986               /* Do not consider if it is pre/post modification in MEM.  */
987               if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
988                 continue;
989
990               /* Do not make the log link for frame pointer.  */
991               if ((regno == FRAME_POINTER_REGNUM
992                    && (! reload_completed || frame_pointer_needed))
993 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
994                   || (regno == HARD_FRAME_POINTER_REGNUM
995                       && (! reload_completed || frame_pointer_needed))
996 #endif
997 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
998                   || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
999 #endif
1000                   )
1001                 continue;
1002
1003               use_insn = next_use[regno];
1004               if (BLOCK_FOR_INSN (use_insn) == bb)
1005                 {
1006                   /* flow.c claimed:
1007
1008                      We don't build a LOG_LINK for hard registers contained
1009                      in ASM_OPERANDs.  If these registers get replaced,
1010                      we might wind up changing the semantics of the insn,
1011                      even if reload can make what appear to be valid
1012                      assignments later.  */
1013                   if (regno >= FIRST_PSEUDO_REGISTER
1014                       || asm_noperands (PATTERN (use_insn)) < 0)
1015                     {
1016                       /* Don't add duplicate links between instructions.  */
1017                       struct insn_link *links;
1018                       FOR_EACH_LOG_LINK (links, use_insn)
1019                         if (insn == links->insn)
1020                           break;
1021
1022                       if (!links)
1023                         LOG_LINKS (use_insn)
1024                           = alloc_insn_link (insn, LOG_LINKS (use_insn));
1025                     }
1026                 }
1027               next_use[regno] = NULL_RTX;
1028             }
1029
1030           for (use_vec = DF_INSN_USES (insn); *use_vec; use_vec++)
1031             {
1032               df_ref use = *use_vec;
1033               int regno = DF_REF_REGNO (use);
1034
1035               /* Do not consider the usage of the stack pointer
1036                  by function call.  */
1037               if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1038                 continue;
1039
1040               next_use[regno] = insn;
1041             }
1042         }
1043     }
1044
1045   free (next_use);
1046 }
1047
1048 /* Walk the LOG_LINKS of insn B to see if we find a reference to A.  Return
1049    true if we found a LOG_LINK that proves that A feeds B.  This only works
1050    if there are no instructions between A and B which could have a link
1051    depending on A, since in that case we would not record a link for B.
1052    We also check the implicit dependency created by a cc0 setter/user
1053    pair.  */
1054
1055 static bool
1056 insn_a_feeds_b (rtx a, rtx b)
1057 {
1058   struct insn_link *links;
1059   FOR_EACH_LOG_LINK (links, b)
1060     if (links->insn == a)
1061       return true;
1062 #ifdef HAVE_cc0
1063   if (sets_cc0_p (a))
1064     return true;
1065 #endif
1066   return false;
1067 }
1068 \f
1069 /* Main entry point for combiner.  F is the first insn of the function.
1070    NREGS is the first unused pseudo-reg number.
1071
1072    Return nonzero if the combiner has turned an indirect jump
1073    instruction into a direct jump.  */
1074 static int
1075 combine_instructions (rtx f, unsigned int nregs)
1076 {
1077   rtx insn, next;
1078 #ifdef HAVE_cc0
1079   rtx prev;
1080 #endif
1081   struct insn_link *links, *nextlinks;
1082   rtx first;
1083   basic_block last_bb;
1084
1085   int new_direct_jump_p = 0;
1086
1087   for (first = f; first && !INSN_P (first); )
1088     first = NEXT_INSN (first);
1089   if (!first)
1090     return 0;
1091
1092   combine_attempts = 0;
1093   combine_merges = 0;
1094   combine_extras = 0;
1095   combine_successes = 0;
1096
1097   rtl_hooks = combine_rtl_hooks;
1098
1099   VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
1100
1101   init_recog_no_volatile ();
1102
1103   /* Allocate array for insn info.  */
1104   max_uid_known = get_max_uid ();
1105   uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1106   uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1107   gcc_obstack_init (&insn_link_obstack);
1108
1109   nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1110
1111   /* Don't use reg_stat[].nonzero_bits when computing it.  This can cause
1112      problems when, for example, we have j <<= 1 in a loop.  */
1113
1114   nonzero_sign_valid = 0;
1115   label_tick = label_tick_ebb_start = 1;
1116
1117   /* Scan all SETs and see if we can deduce anything about what
1118      bits are known to be zero for some registers and how many copies
1119      of the sign bit are known to exist for those registers.
1120
1121      Also set any known values so that we can use it while searching
1122      for what bits are known to be set.  */
1123
1124   setup_incoming_promotions (first);
1125   /* Allow the entry block and the first block to fall into the same EBB.
1126      Conceptually the incoming promotions are assigned to the entry block.  */
1127   last_bb = ENTRY_BLOCK_PTR;
1128
1129   create_log_links ();
1130   FOR_EACH_BB (this_basic_block)
1131     {
1132       optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1133       last_call_luid = 0;
1134       mem_last_set = -1;
1135
1136       label_tick++;
1137       if (!single_pred_p (this_basic_block)
1138           || single_pred (this_basic_block) != last_bb)
1139         label_tick_ebb_start = label_tick;
1140       last_bb = this_basic_block;
1141
1142       FOR_BB_INSNS (this_basic_block, insn)
1143         if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1144           {
1145 #ifdef AUTO_INC_DEC
1146             rtx links;
1147 #endif
1148
1149             subst_low_luid = DF_INSN_LUID (insn);
1150             subst_insn = insn;
1151
1152             note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1153                          insn);
1154             record_dead_and_set_regs (insn);
1155
1156 #ifdef AUTO_INC_DEC
1157             for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1158               if (REG_NOTE_KIND (links) == REG_INC)
1159                 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1160                                                   insn);
1161 #endif
1162
1163             /* Record the current insn_rtx_cost of this instruction.  */
1164             if (NONJUMP_INSN_P (insn))
1165               INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1166                                                 optimize_this_for_speed_p);
1167             if (dump_file)
1168               fprintf(dump_file, "insn_cost %d: %d\n",
1169                     INSN_UID (insn), INSN_COST (insn));
1170           }
1171     }
1172
1173   nonzero_sign_valid = 1;
1174
1175   /* Now scan all the insns in forward order.  */
1176   label_tick = label_tick_ebb_start = 1;
1177   init_reg_last ();
1178   setup_incoming_promotions (first);
1179   last_bb = ENTRY_BLOCK_PTR;
1180
1181   FOR_EACH_BB (this_basic_block)
1182     {
1183       rtx last_combined_insn = NULL_RTX;
1184       optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1185       last_call_luid = 0;
1186       mem_last_set = -1;
1187
1188       label_tick++;
1189       if (!single_pred_p (this_basic_block)
1190           || single_pred (this_basic_block) != last_bb)
1191         label_tick_ebb_start = label_tick;
1192       last_bb = this_basic_block;
1193
1194       rtl_profile_for_bb (this_basic_block);
1195       for (insn = BB_HEAD (this_basic_block);
1196            insn != NEXT_INSN (BB_END (this_basic_block));
1197            insn = next ? next : NEXT_INSN (insn))
1198         {
1199           next = 0;
1200           if (NONDEBUG_INSN_P (insn))
1201             {
1202               while (last_combined_insn
1203                      && INSN_DELETED_P (last_combined_insn))
1204                 last_combined_insn = PREV_INSN (last_combined_insn);
1205               if (last_combined_insn == NULL_RTX
1206                   || BARRIER_P (last_combined_insn)
1207                   || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1208                   || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1209                 last_combined_insn = insn;
1210
1211               /* See if we know about function return values before this
1212                  insn based upon SUBREG flags.  */
1213               check_promoted_subreg (insn, PATTERN (insn));
1214
1215               /* See if we can find hardregs and subreg of pseudos in
1216                  narrower modes.  This could help turning TRUNCATEs
1217                  into SUBREGs.  */
1218               note_uses (&PATTERN (insn), record_truncated_values, NULL);
1219
1220               /* Try this insn with each insn it links back to.  */
1221
1222               FOR_EACH_LOG_LINK (links, insn)
1223                 if ((next = try_combine (insn, links->insn, NULL_RTX,
1224                                          NULL_RTX, &new_direct_jump_p,
1225                                          last_combined_insn)) != 0)
1226                   goto retry;
1227
1228               /* Try each sequence of three linked insns ending with this one.  */
1229
1230               FOR_EACH_LOG_LINK (links, insn)
1231                 {
1232                   rtx link = links->insn;
1233
1234                   /* If the linked insn has been replaced by a note, then there
1235                      is no point in pursuing this chain any further.  */
1236                   if (NOTE_P (link))
1237                     continue;
1238
1239                   FOR_EACH_LOG_LINK (nextlinks, link)
1240                     if ((next = try_combine (insn, link, nextlinks->insn,
1241                                              NULL_RTX, &new_direct_jump_p,
1242                                              last_combined_insn)) != 0)
1243                       goto retry;
1244                 }
1245
1246 #ifdef HAVE_cc0
1247               /* Try to combine a jump insn that uses CC0
1248                  with a preceding insn that sets CC0, and maybe with its
1249                  logical predecessor as well.
1250                  This is how we make decrement-and-branch insns.
1251                  We need this special code because data flow connections
1252                  via CC0 do not get entered in LOG_LINKS.  */
1253
1254               if (JUMP_P (insn)
1255                   && (prev = prev_nonnote_insn (insn)) != 0
1256                   && NONJUMP_INSN_P (prev)
1257                   && sets_cc0_p (PATTERN (prev)))
1258                 {
1259                   if ((next = try_combine (insn, prev, NULL_RTX, NULL_RTX,
1260                                            &new_direct_jump_p,
1261                                            last_combined_insn)) != 0)
1262                     goto retry;
1263
1264                   FOR_EACH_LOG_LINK (nextlinks, prev)
1265                     if ((next = try_combine (insn, prev, nextlinks->insn,
1266                                              NULL_RTX, &new_direct_jump_p,
1267                                              last_combined_insn)) != 0)
1268                       goto retry;
1269                 }
1270
1271               /* Do the same for an insn that explicitly references CC0.  */
1272               if (NONJUMP_INSN_P (insn)
1273                   && (prev = prev_nonnote_insn (insn)) != 0
1274                   && NONJUMP_INSN_P (prev)
1275                   && sets_cc0_p (PATTERN (prev))
1276                   && GET_CODE (PATTERN (insn)) == SET
1277                   && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1278                 {
1279                   if ((next = try_combine (insn, prev, NULL_RTX, NULL_RTX,
1280                                            &new_direct_jump_p,
1281                                            last_combined_insn)) != 0)
1282                     goto retry;
1283
1284                   FOR_EACH_LOG_LINK (nextlinks, prev)
1285                     if ((next = try_combine (insn, prev, nextlinks->insn,
1286                                              NULL_RTX, &new_direct_jump_p,
1287                                              last_combined_insn)) != 0)
1288                       goto retry;
1289                 }
1290
1291               /* Finally, see if any of the insns that this insn links to
1292                  explicitly references CC0.  If so, try this insn, that insn,
1293                  and its predecessor if it sets CC0.  */
1294               FOR_EACH_LOG_LINK (links, insn)
1295                 if (NONJUMP_INSN_P (links->insn)
1296                     && GET_CODE (PATTERN (links->insn)) == SET
1297                     && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1298                     && (prev = prev_nonnote_insn (links->insn)) != 0
1299                     && NONJUMP_INSN_P (prev)
1300                     && sets_cc0_p (PATTERN (prev))
1301                     && (next = try_combine (insn, links->insn,
1302                                             prev, NULL_RTX, &new_direct_jump_p,
1303                                             last_combined_insn)) != 0)
1304                   goto retry;
1305 #endif
1306
1307               /* Try combining an insn with two different insns whose results it
1308                  uses.  */
1309               FOR_EACH_LOG_LINK (links, insn)
1310                 for (nextlinks = links->next; nextlinks;
1311                      nextlinks = nextlinks->next)
1312                   if ((next = try_combine (insn, links->insn,
1313                                            nextlinks->insn, NULL_RTX,
1314                                            &new_direct_jump_p,
1315                                            last_combined_insn)) != 0)
1316                     goto retry;
1317
1318               /* Try four-instruction combinations.  */
1319               FOR_EACH_LOG_LINK (links, insn)
1320                 {
1321                   struct insn_link *next1;
1322                   rtx link = links->insn;
1323
1324                   /* If the linked insn has been replaced by a note, then there
1325                      is no point in pursuing this chain any further.  */
1326                   if (NOTE_P (link))
1327                     continue;
1328
1329                   FOR_EACH_LOG_LINK (next1, link)
1330                     {
1331                       rtx link1 = next1->insn;
1332                       if (NOTE_P (link1))
1333                         continue;
1334                       /* I0 -> I1 -> I2 -> I3.  */
1335                       FOR_EACH_LOG_LINK (nextlinks, link1)
1336                         if ((next = try_combine (insn, link, link1,
1337                                                  nextlinks->insn,
1338                                                  &new_direct_jump_p,
1339                                                  last_combined_insn)) != 0)
1340                           goto retry;
1341                       /* I0, I1 -> I2, I2 -> I3.  */
1342                       for (nextlinks = next1->next; nextlinks;
1343                            nextlinks = nextlinks->next)
1344                         if ((next = try_combine (insn, link, link1,
1345                                                  nextlinks->insn,
1346                                                  &new_direct_jump_p,
1347                                                  last_combined_insn)) != 0)
1348                           goto retry;
1349                     }
1350
1351                   for (next1 = links->next; next1; next1 = next1->next)
1352                     {
1353                       rtx link1 = next1->insn;
1354                       if (NOTE_P (link1))
1355                         continue;
1356                       /* I0 -> I2; I1, I2 -> I3.  */
1357                       FOR_EACH_LOG_LINK (nextlinks, link)
1358                         if ((next = try_combine (insn, link, link1,
1359                                                  nextlinks->insn,
1360                                                  &new_direct_jump_p,
1361                                                  last_combined_insn)) != 0)
1362                           goto retry;
1363                       /* I0 -> I1; I1, I2 -> I3.  */
1364                       FOR_EACH_LOG_LINK (nextlinks, link1)
1365                         if ((next = try_combine (insn, link, link1,
1366                                                  nextlinks->insn,
1367                                                  &new_direct_jump_p,
1368                                                  last_combined_insn)) != 0)
1369                           goto retry;
1370                     }
1371                 }
1372
1373               /* Try this insn with each REG_EQUAL note it links back to.  */
1374               FOR_EACH_LOG_LINK (links, insn)
1375                 {
1376                   rtx set, note;
1377                   rtx temp = links->insn;
1378                   if ((set = single_set (temp)) != 0
1379                       && (note = find_reg_equal_equiv_note (temp)) != 0
1380                       && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1381                       /* Avoid using a register that may already been marked
1382                          dead by an earlier instruction.  */
1383                       && ! unmentioned_reg_p (note, SET_SRC (set))
1384                       && (GET_MODE (note) == VOIDmode
1385                           ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1386                           : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1387                     {
1388                       /* Temporarily replace the set's source with the
1389                          contents of the REG_EQUAL note.  The insn will
1390                          be deleted or recognized by try_combine.  */
1391                       rtx orig = SET_SRC (set);
1392                       SET_SRC (set) = note;
1393                       i2mod = temp;
1394                       i2mod_old_rhs = copy_rtx (orig);
1395                       i2mod_new_rhs = copy_rtx (note);
1396                       next = try_combine (insn, i2mod, NULL_RTX, NULL_RTX,
1397                                           &new_direct_jump_p,
1398                                           last_combined_insn);
1399                       i2mod = NULL_RTX;
1400                       if (next)
1401                         goto retry;
1402                       SET_SRC (set) = orig;
1403                     }
1404                 }
1405
1406               if (!NOTE_P (insn))
1407                 record_dead_and_set_regs (insn);
1408
1409             retry:
1410               ;
1411             }
1412         }
1413     }
1414
1415   default_rtl_profile ();
1416   clear_bb_flags ();
1417   new_direct_jump_p |= purge_all_dead_edges ();
1418   delete_noop_moves ();
1419
1420   /* Clean up.  */
1421   obstack_free (&insn_link_obstack, NULL);
1422   free (uid_log_links);
1423   free (uid_insn_cost);
1424   VEC_free (reg_stat_type, heap, reg_stat);
1425
1426   {
1427     struct undo *undo, *next;
1428     for (undo = undobuf.frees; undo; undo = next)
1429       {
1430         next = undo->next;
1431         free (undo);
1432       }
1433     undobuf.frees = 0;
1434   }
1435
1436   total_attempts += combine_attempts;
1437   total_merges += combine_merges;
1438   total_extras += combine_extras;
1439   total_successes += combine_successes;
1440
1441   nonzero_sign_valid = 0;
1442   rtl_hooks = general_rtl_hooks;
1443
1444   /* Make recognizer allow volatile MEMs again.  */
1445   init_recog ();
1446
1447   return new_direct_jump_p;
1448 }
1449
1450 /* Wipe the last_xxx fields of reg_stat in preparation for another pass.  */
1451
1452 static void
1453 init_reg_last (void)
1454 {
1455   unsigned int i;
1456   reg_stat_type *p;
1457
1458   FOR_EACH_VEC_ELT (reg_stat_type, reg_stat, i, p)
1459     memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1460 }
1461 \f
1462 /* Set up any promoted values for incoming argument registers.  */
1463
1464 static void
1465 setup_incoming_promotions (rtx first)
1466 {
1467   tree arg;
1468   bool strictly_local = false;
1469
1470   for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1471        arg = DECL_CHAIN (arg))
1472     {
1473       rtx x, reg = DECL_INCOMING_RTL (arg);
1474       int uns1, uns3;
1475       enum machine_mode mode1, mode2, mode3, mode4;
1476
1477       /* Only continue if the incoming argument is in a register.  */
1478       if (!REG_P (reg))
1479         continue;
1480
1481       /* Determine, if possible, whether all call sites of the current
1482          function lie within the current compilation unit.  (This does
1483          take into account the exporting of a function via taking its
1484          address, and so forth.)  */
1485       strictly_local = cgraph_local_info (current_function_decl)->local;
1486
1487       /* The mode and signedness of the argument before any promotions happen
1488          (equal to the mode of the pseudo holding it at that stage).  */
1489       mode1 = TYPE_MODE (TREE_TYPE (arg));
1490       uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1491
1492       /* The mode and signedness of the argument after any source language and
1493          TARGET_PROMOTE_PROTOTYPES-driven promotions.  */
1494       mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1495       uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1496
1497       /* The mode and signedness of the argument as it is actually passed,
1498          after any TARGET_PROMOTE_FUNCTION_ARGS-driven ABI promotions.  */
1499       mode3 = promote_function_mode (DECL_ARG_TYPE (arg), mode2, &uns3,
1500                                      TREE_TYPE (cfun->decl), 0);
1501
1502       /* The mode of the register in which the argument is being passed.  */
1503       mode4 = GET_MODE (reg);
1504
1505       /* Eliminate sign extensions in the callee when:
1506          (a) A mode promotion has occurred;  */
1507       if (mode1 == mode3)
1508         continue;
1509       /* (b) The mode of the register is the same as the mode of
1510              the argument as it is passed; */
1511       if (mode3 != mode4)
1512         continue;
1513       /* (c) There's no language level extension;  */
1514       if (mode1 == mode2)
1515         ;
1516       /* (c.1) All callers are from the current compilation unit.  If that's
1517          the case we don't have to rely on an ABI, we only have to know
1518          what we're generating right now, and we know that we will do the
1519          mode1 to mode2 promotion with the given sign.  */
1520       else if (!strictly_local)
1521         continue;
1522       /* (c.2) The combination of the two promotions is useful.  This is
1523          true when the signs match, or if the first promotion is unsigned.
1524          In the later case, (sign_extend (zero_extend x)) is the same as
1525          (zero_extend (zero_extend x)), so make sure to force UNS3 true.  */
1526       else if (uns1)
1527         uns3 = true;
1528       else if (uns3)
1529         continue;
1530
1531       /* Record that the value was promoted from mode1 to mode3,
1532          so that any sign extension at the head of the current
1533          function may be eliminated.  */
1534       x = gen_rtx_CLOBBER (mode1, const0_rtx);
1535       x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1536       record_value_for_reg (reg, first, x);
1537     }
1538 }
1539
1540 /* Called via note_stores.  If X is a pseudo that is narrower than
1541    HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1542
1543    If we are setting only a portion of X and we can't figure out what
1544    portion, assume all bits will be used since we don't know what will
1545    be happening.
1546
1547    Similarly, set how many bits of X are known to be copies of the sign bit
1548    at all locations in the function.  This is the smallest number implied
1549    by any set of X.  */
1550
1551 static void
1552 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1553 {
1554   rtx insn = (rtx) data;
1555   unsigned int num;
1556
1557   if (REG_P (x)
1558       && REGNO (x) >= FIRST_PSEUDO_REGISTER
1559       /* If this register is undefined at the start of the file, we can't
1560          say what its contents were.  */
1561       && ! REGNO_REG_SET_P
1562            (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x))
1563       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
1564     {
1565       reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
1566
1567       if (set == 0 || GET_CODE (set) == CLOBBER)
1568         {
1569           rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1570           rsp->sign_bit_copies = 1;
1571           return;
1572         }
1573
1574       /* If this register is being initialized using itself, and the
1575          register is uninitialized in this basic block, and there are
1576          no LOG_LINKS which set the register, then part of the
1577          register is uninitialized.  In that case we can't assume
1578          anything about the number of nonzero bits.
1579
1580          ??? We could do better if we checked this in
1581          reg_{nonzero_bits,num_sign_bit_copies}_for_combine.  Then we
1582          could avoid making assumptions about the insn which initially
1583          sets the register, while still using the information in other
1584          insns.  We would have to be careful to check every insn
1585          involved in the combination.  */
1586
1587       if (insn
1588           && reg_referenced_p (x, PATTERN (insn))
1589           && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1590                                REGNO (x)))
1591         {
1592           struct insn_link *link;
1593
1594           FOR_EACH_LOG_LINK (link, insn)
1595             if (dead_or_set_p (link->insn, x))
1596               break;
1597           if (!link)
1598             {
1599               rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1600               rsp->sign_bit_copies = 1;
1601               return;
1602             }
1603         }
1604
1605       /* If this is a complex assignment, see if we can convert it into a
1606          simple assignment.  */
1607       set = expand_field_assignment (set);
1608
1609       /* If this is a simple assignment, or we have a paradoxical SUBREG,
1610          set what we know about X.  */
1611
1612       if (SET_DEST (set) == x
1613           || (GET_CODE (SET_DEST (set)) == SUBREG
1614               && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
1615                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
1616               && SUBREG_REG (SET_DEST (set)) == x))
1617         {
1618           rtx src = SET_SRC (set);
1619
1620 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1621           /* If X is narrower than a word and SRC is a non-negative
1622              constant that would appear negative in the mode of X,
1623              sign-extend it for use in reg_stat[].nonzero_bits because some
1624              machines (maybe most) will actually do the sign-extension
1625              and this is the conservative approach.
1626
1627              ??? For 2.5, try to tighten up the MD files in this regard
1628              instead of this kludge.  */
1629
1630           if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
1631               && CONST_INT_P (src)
1632               && INTVAL (src) > 0
1633               && 0 != (UINTVAL (src)
1634                        & ((unsigned HOST_WIDE_INT) 1
1635                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
1636             src = GEN_INT (UINTVAL (src)
1637                            | ((unsigned HOST_WIDE_INT) (-1)
1638                               << GET_MODE_BITSIZE (GET_MODE (x))));
1639 #endif
1640
1641           /* Don't call nonzero_bits if it cannot change anything.  */
1642           if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1643             rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1644           num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1645           if (rsp->sign_bit_copies == 0
1646               || rsp->sign_bit_copies > num)
1647             rsp->sign_bit_copies = num;
1648         }
1649       else
1650         {
1651           rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1652           rsp->sign_bit_copies = 1;
1653         }
1654     }
1655 }
1656 \f
1657 /* See if INSN can be combined into I3.  PRED, PRED2, SUCC and SUCC2 are
1658    optionally insns that were previously combined into I3 or that will be
1659    combined into the merger of INSN and I3.  The order is PRED, PRED2,
1660    INSN, SUCC, SUCC2, I3.
1661
1662    Return 0 if the combination is not allowed for any reason.
1663
1664    If the combination is allowed, *PDEST will be set to the single
1665    destination of INSN and *PSRC to the single source, and this function
1666    will return 1.  */
1667
1668 static int
1669 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED,
1670                rtx pred2 ATTRIBUTE_UNUSED, rtx succ, rtx succ2,
1671                rtx *pdest, rtx *psrc)
1672 {
1673   int i;
1674   const_rtx set = 0;
1675   rtx src, dest;
1676   rtx p;
1677 #ifdef AUTO_INC_DEC
1678   rtx link;
1679 #endif
1680   bool all_adjacent = true;
1681
1682   if (succ)
1683     {
1684       if (succ2)
1685         {
1686           if (next_active_insn (succ2) != i3)
1687             all_adjacent = false;
1688           if (next_active_insn (succ) != succ2)
1689             all_adjacent = false;
1690         }
1691       else if (next_active_insn (succ) != i3)
1692         all_adjacent = false;
1693       if (next_active_insn (insn) != succ)
1694         all_adjacent = false;
1695     }
1696   else if (next_active_insn (insn) != i3)
1697     all_adjacent = false;
1698     
1699   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1700      or a PARALLEL consisting of such a SET and CLOBBERs.
1701
1702      If INSN has CLOBBER parallel parts, ignore them for our processing.
1703      By definition, these happen during the execution of the insn.  When it
1704      is merged with another insn, all bets are off.  If they are, in fact,
1705      needed and aren't also supplied in I3, they may be added by
1706      recog_for_combine.  Otherwise, it won't match.
1707
1708      We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1709      note.
1710
1711      Get the source and destination of INSN.  If more than one, can't
1712      combine.  */
1713
1714   if (GET_CODE (PATTERN (insn)) == SET)
1715     set = PATTERN (insn);
1716   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1717            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1718     {
1719       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1720         {
1721           rtx elt = XVECEXP (PATTERN (insn), 0, i);
1722
1723           switch (GET_CODE (elt))
1724             {
1725             /* This is important to combine floating point insns
1726                for the SH4 port.  */
1727             case USE:
1728               /* Combining an isolated USE doesn't make sense.
1729                  We depend here on combinable_i3pat to reject them.  */
1730               /* The code below this loop only verifies that the inputs of
1731                  the SET in INSN do not change.  We call reg_set_between_p
1732                  to verify that the REG in the USE does not change between
1733                  I3 and INSN.
1734                  If the USE in INSN was for a pseudo register, the matching
1735                  insn pattern will likely match any register; combining this
1736                  with any other USE would only be safe if we knew that the
1737                  used registers have identical values, or if there was
1738                  something to tell them apart, e.g. different modes.  For
1739                  now, we forgo such complicated tests and simply disallow
1740                  combining of USES of pseudo registers with any other USE.  */
1741               if (REG_P (XEXP (elt, 0))
1742                   && GET_CODE (PATTERN (i3)) == PARALLEL)
1743                 {
1744                   rtx i3pat = PATTERN (i3);
1745                   int i = XVECLEN (i3pat, 0) - 1;
1746                   unsigned int regno = REGNO (XEXP (elt, 0));
1747
1748                   do
1749                     {
1750                       rtx i3elt = XVECEXP (i3pat, 0, i);
1751
1752                       if (GET_CODE (i3elt) == USE
1753                           && REG_P (XEXP (i3elt, 0))
1754                           && (REGNO (XEXP (i3elt, 0)) == regno
1755                               ? reg_set_between_p (XEXP (elt, 0),
1756                                                    PREV_INSN (insn), i3)
1757                               : regno >= FIRST_PSEUDO_REGISTER))
1758                         return 0;
1759                     }
1760                   while (--i >= 0);
1761                 }
1762               break;
1763
1764               /* We can ignore CLOBBERs.  */
1765             case CLOBBER:
1766               break;
1767
1768             case SET:
1769               /* Ignore SETs whose result isn't used but not those that
1770                  have side-effects.  */
1771               if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1772                   && insn_nothrow_p (insn)
1773                   && !side_effects_p (elt))
1774                 break;
1775
1776               /* If we have already found a SET, this is a second one and
1777                  so we cannot combine with this insn.  */
1778               if (set)
1779                 return 0;
1780
1781               set = elt;
1782               break;
1783
1784             default:
1785               /* Anything else means we can't combine.  */
1786               return 0;
1787             }
1788         }
1789
1790       if (set == 0
1791           /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1792              so don't do anything with it.  */
1793           || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1794         return 0;
1795     }
1796   else
1797     return 0;
1798
1799   if (set == 0)
1800     return 0;
1801
1802   set = expand_field_assignment (set);
1803   src = SET_SRC (set), dest = SET_DEST (set);
1804
1805   /* Don't eliminate a store in the stack pointer.  */
1806   if (dest == stack_pointer_rtx
1807       /* Don't combine with an insn that sets a register to itself if it has
1808          a REG_EQUAL note.  This may be part of a LIBCALL sequence.  */
1809       || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1810       /* Can't merge an ASM_OPERANDS.  */
1811       || GET_CODE (src) == ASM_OPERANDS
1812       /* Can't merge a function call.  */
1813       || GET_CODE (src) == CALL
1814       /* Don't eliminate a function call argument.  */
1815       || (CALL_P (i3)
1816           && (find_reg_fusage (i3, USE, dest)
1817               || (REG_P (dest)
1818                   && REGNO (dest) < FIRST_PSEUDO_REGISTER
1819                   && global_regs[REGNO (dest)])))
1820       /* Don't substitute into an incremented register.  */
1821       || FIND_REG_INC_NOTE (i3, dest)
1822       || (succ && FIND_REG_INC_NOTE (succ, dest))
1823       || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1824       /* Don't substitute into a non-local goto, this confuses CFG.  */
1825       || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1826       /* Make sure that DEST is not used after SUCC but before I3.  */
1827       || (!all_adjacent
1828           && ((succ2
1829                && (reg_used_between_p (dest, succ2, i3)
1830                    || reg_used_between_p (dest, succ, succ2)))
1831               || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
1832       /* Make sure that the value that is to be substituted for the register
1833          does not use any registers whose values alter in between.  However,
1834          If the insns are adjacent, a use can't cross a set even though we
1835          think it might (this can happen for a sequence of insns each setting
1836          the same destination; last_set of that register might point to
1837          a NOTE).  If INSN has a REG_EQUIV note, the register is always
1838          equivalent to the memory so the substitution is valid even if there
1839          are intervening stores.  Also, don't move a volatile asm or
1840          UNSPEC_VOLATILE across any other insns.  */
1841       || (! all_adjacent
1842           && (((!MEM_P (src)
1843                 || ! find_reg_note (insn, REG_EQUIV, src))
1844                && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1845               || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1846               || GET_CODE (src) == UNSPEC_VOLATILE))
1847       /* Don't combine across a CALL_INSN, because that would possibly
1848          change whether the life span of some REGs crosses calls or not,
1849          and it is a pain to update that information.
1850          Exception: if source is a constant, moving it later can't hurt.
1851          Accept that as a special case.  */
1852       || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1853     return 0;
1854
1855   /* DEST must either be a REG or CC0.  */
1856   if (REG_P (dest))
1857     {
1858       /* If register alignment is being enforced for multi-word items in all
1859          cases except for parameters, it is possible to have a register copy
1860          insn referencing a hard register that is not allowed to contain the
1861          mode being copied and which would not be valid as an operand of most
1862          insns.  Eliminate this problem by not combining with such an insn.
1863
1864          Also, on some machines we don't want to extend the life of a hard
1865          register.  */
1866
1867       if (REG_P (src)
1868           && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1869                && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1870               /* Don't extend the life of a hard register unless it is
1871                  user variable (if we have few registers) or it can't
1872                  fit into the desired register (meaning something special
1873                  is going on).
1874                  Also avoid substituting a return register into I3, because
1875                  reload can't handle a conflict with constraints of other
1876                  inputs.  */
1877               || (REGNO (src) < FIRST_PSEUDO_REGISTER
1878                   && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1879         return 0;
1880     }
1881   else if (GET_CODE (dest) != CC0)
1882     return 0;
1883
1884
1885   if (GET_CODE (PATTERN (i3)) == PARALLEL)
1886     for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1887       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1888         {
1889           /* Don't substitute for a register intended as a clobberable
1890              operand.  */
1891           rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1892           if (rtx_equal_p (reg, dest))
1893             return 0;
1894
1895           /* If the clobber represents an earlyclobber operand, we must not
1896              substitute an expression containing the clobbered register.
1897              As we do not analyze the constraint strings here, we have to
1898              make the conservative assumption.  However, if the register is
1899              a fixed hard reg, the clobber cannot represent any operand;
1900              we leave it up to the machine description to either accept or
1901              reject use-and-clobber patterns.  */
1902           if (!REG_P (reg)
1903               || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1904               || !fixed_regs[REGNO (reg)])
1905             if (reg_overlap_mentioned_p (reg, src))
1906               return 0;
1907         }
1908
1909   /* If INSN contains anything volatile, or is an `asm' (whether volatile
1910      or not), reject, unless nothing volatile comes between it and I3 */
1911
1912   if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1913     {
1914       /* Make sure neither succ nor succ2 contains a volatile reference.  */
1915       if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
1916         return 0;
1917       if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1918         return 0;
1919       /* We'll check insns between INSN and I3 below.  */
1920     }
1921
1922   /* If INSN is an asm, and DEST is a hard register, reject, since it has
1923      to be an explicit register variable, and was chosen for a reason.  */
1924
1925   if (GET_CODE (src) == ASM_OPERANDS
1926       && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1927     return 0;
1928
1929   /* If there are any volatile insns between INSN and I3, reject, because
1930      they might affect machine state.  */
1931
1932   for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1933     if (INSN_P (p) && p != succ && p != succ2 && volatile_insn_p (PATTERN (p)))
1934       return 0;
1935
1936   /* If INSN contains an autoincrement or autodecrement, make sure that
1937      register is not used between there and I3, and not already used in
1938      I3 either.  Neither must it be used in PRED or SUCC, if they exist.
1939      Also insist that I3 not be a jump; if it were one
1940      and the incremented register were spilled, we would lose.  */
1941
1942 #ifdef AUTO_INC_DEC
1943   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1944     if (REG_NOTE_KIND (link) == REG_INC
1945         && (JUMP_P (i3)
1946             || reg_used_between_p (XEXP (link, 0), insn, i3)
1947             || (pred != NULL_RTX
1948                 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1949             || (pred2 != NULL_RTX
1950                 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
1951             || (succ != NULL_RTX
1952                 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
1953             || (succ2 != NULL_RTX
1954                 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
1955             || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1956       return 0;
1957 #endif
1958
1959 #ifdef HAVE_cc0
1960   /* Don't combine an insn that follows a CC0-setting insn.
1961      An insn that uses CC0 must not be separated from the one that sets it.
1962      We do, however, allow I2 to follow a CC0-setting insn if that insn
1963      is passed as I1; in that case it will be deleted also.
1964      We also allow combining in this case if all the insns are adjacent
1965      because that would leave the two CC0 insns adjacent as well.
1966      It would be more logical to test whether CC0 occurs inside I1 or I2,
1967      but that would be much slower, and this ought to be equivalent.  */
1968
1969   p = prev_nonnote_insn (insn);
1970   if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1971       && ! all_adjacent)
1972     return 0;
1973 #endif
1974
1975   /* If we get here, we have passed all the tests and the combination is
1976      to be allowed.  */
1977
1978   *pdest = dest;
1979   *psrc = src;
1980
1981   return 1;
1982 }
1983 \f
1984 /* LOC is the location within I3 that contains its pattern or the component
1985    of a PARALLEL of the pattern.  We validate that it is valid for combining.
1986
1987    One problem is if I3 modifies its output, as opposed to replacing it
1988    entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
1989    doing so would produce an insn that is not equivalent to the original insns.
1990
1991    Consider:
1992
1993          (set (reg:DI 101) (reg:DI 100))
1994          (set (subreg:SI (reg:DI 101) 0) <foo>)
1995
1996    This is NOT equivalent to:
1997
1998          (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1999                     (set (reg:DI 101) (reg:DI 100))])
2000
2001    Not only does this modify 100 (in which case it might still be valid
2002    if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2003
2004    We can also run into a problem if I2 sets a register that I1
2005    uses and I1 gets directly substituted into I3 (not via I2).  In that
2006    case, we would be getting the wrong value of I2DEST into I3, so we
2007    must reject the combination.  This case occurs when I2 and I1 both
2008    feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2009    If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2010    of a SET must prevent combination from occurring.  The same situation
2011    can occur for I0, in which case I0_NOT_IN_SRC is set.
2012
2013    Before doing the above check, we first try to expand a field assignment
2014    into a set of logical operations.
2015
2016    If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2017    we place a register that is both set and used within I3.  If more than one
2018    such register is detected, we fail.
2019
2020    Return 1 if the combination is valid, zero otherwise.  */
2021
2022 static int
2023 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2024                   int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2025 {
2026   rtx x = *loc;
2027
2028   if (GET_CODE (x) == SET)
2029     {
2030       rtx set = x ;
2031       rtx dest = SET_DEST (set);
2032       rtx src = SET_SRC (set);
2033       rtx inner_dest = dest;
2034       rtx subdest;
2035
2036       while (GET_CODE (inner_dest) == STRICT_LOW_PART
2037              || GET_CODE (inner_dest) == SUBREG
2038              || GET_CODE (inner_dest) == ZERO_EXTRACT)
2039         inner_dest = XEXP (inner_dest, 0);
2040
2041       /* Check for the case where I3 modifies its output, as discussed
2042          above.  We don't want to prevent pseudos from being combined
2043          into the address of a MEM, so only prevent the combination if
2044          i1 or i2 set the same MEM.  */
2045       if ((inner_dest != dest &&
2046            (!MEM_P (inner_dest)
2047             || rtx_equal_p (i2dest, inner_dest)
2048             || (i1dest && rtx_equal_p (i1dest, inner_dest))
2049             || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2050            && (reg_overlap_mentioned_p (i2dest, inner_dest)
2051                || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2052                || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2053
2054           /* This is the same test done in can_combine_p except we can't test
2055              all_adjacent; we don't have to, since this instruction will stay
2056              in place, thus we are not considering increasing the lifetime of
2057              INNER_DEST.
2058
2059              Also, if this insn sets a function argument, combining it with
2060              something that might need a spill could clobber a previous
2061              function argument; the all_adjacent test in can_combine_p also
2062              checks this; here, we do a more specific test for this case.  */
2063
2064           || (REG_P (inner_dest)
2065               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2066               && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2067                                         GET_MODE (inner_dest))))
2068           || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2069           || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2070         return 0;
2071
2072       /* If DEST is used in I3, it is being killed in this insn, so
2073          record that for later.  We have to consider paradoxical
2074          subregs here, since they kill the whole register, but we
2075          ignore partial subregs, STRICT_LOW_PART, etc.
2076          Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2077          STACK_POINTER_REGNUM, since these are always considered to be
2078          live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
2079       subdest = dest;
2080       if (GET_CODE (subdest) == SUBREG
2081           && (GET_MODE_SIZE (GET_MODE (subdest))
2082               >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
2083         subdest = SUBREG_REG (subdest);
2084       if (pi3dest_killed
2085           && REG_P (subdest)
2086           && reg_referenced_p (subdest, PATTERN (i3))
2087           && REGNO (subdest) != FRAME_POINTER_REGNUM
2088 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2089           && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
2090 #endif
2091 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2092           && (REGNO (subdest) != ARG_POINTER_REGNUM
2093               || ! fixed_regs [REGNO (subdest)])
2094 #endif
2095           && REGNO (subdest) != STACK_POINTER_REGNUM)
2096         {
2097           if (*pi3dest_killed)
2098             return 0;
2099
2100           *pi3dest_killed = subdest;
2101         }
2102     }
2103
2104   else if (GET_CODE (x) == PARALLEL)
2105     {
2106       int i;
2107
2108       for (i = 0; i < XVECLEN (x, 0); i++)
2109         if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2110                                 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2111           return 0;
2112     }
2113
2114   return 1;
2115 }
2116 \f
2117 /* Return 1 if X is an arithmetic expression that contains a multiplication
2118    and division.  We don't count multiplications by powers of two here.  */
2119
2120 static int
2121 contains_muldiv (rtx x)
2122 {
2123   switch (GET_CODE (x))
2124     {
2125     case MOD:  case DIV:  case UMOD:  case UDIV:
2126       return 1;
2127
2128     case MULT:
2129       return ! (CONST_INT_P (XEXP (x, 1))
2130                 && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
2131     default:
2132       if (BINARY_P (x))
2133         return contains_muldiv (XEXP (x, 0))
2134             || contains_muldiv (XEXP (x, 1));
2135
2136       if (UNARY_P (x))
2137         return contains_muldiv (XEXP (x, 0));
2138
2139       return 0;
2140     }
2141 }
2142 \f
2143 /* Determine whether INSN can be used in a combination.  Return nonzero if
2144    not.  This is used in try_combine to detect early some cases where we
2145    can't perform combinations.  */
2146
2147 static int
2148 cant_combine_insn_p (rtx insn)
2149 {
2150   rtx set;
2151   rtx src, dest;
2152
2153   /* If this isn't really an insn, we can't do anything.
2154      This can occur when flow deletes an insn that it has merged into an
2155      auto-increment address.  */
2156   if (! INSN_P (insn))
2157     return 1;
2158
2159   /* Never combine loads and stores involving hard regs that are likely
2160      to be spilled.  The register allocator can usually handle such
2161      reg-reg moves by tying.  If we allow the combiner to make
2162      substitutions of likely-spilled regs, reload might die.
2163      As an exception, we allow combinations involving fixed regs; these are
2164      not available to the register allocator so there's no risk involved.  */
2165
2166   set = single_set (insn);
2167   if (! set)
2168     return 0;
2169   src = SET_SRC (set);
2170   dest = SET_DEST (set);
2171   if (GET_CODE (src) == SUBREG)
2172     src = SUBREG_REG (src);
2173   if (GET_CODE (dest) == SUBREG)
2174     dest = SUBREG_REG (dest);
2175   if (REG_P (src) && REG_P (dest)
2176       && ((HARD_REGISTER_P (src)
2177            && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2178            && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2179           || (HARD_REGISTER_P (dest)
2180               && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2181               && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2182     return 1;
2183
2184   return 0;
2185 }
2186
2187 struct likely_spilled_retval_info
2188 {
2189   unsigned regno, nregs;
2190   unsigned mask;
2191 };
2192
2193 /* Called via note_stores by likely_spilled_retval_p.  Remove from info->mask
2194    hard registers that are known to be written to / clobbered in full.  */
2195 static void
2196 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2197 {
2198   struct likely_spilled_retval_info *const info =
2199     (struct likely_spilled_retval_info *) data;
2200   unsigned regno, nregs;
2201   unsigned new_mask;
2202
2203   if (!REG_P (XEXP (set, 0)))
2204     return;
2205   regno = REGNO (x);
2206   if (regno >= info->regno + info->nregs)
2207     return;
2208   nregs = hard_regno_nregs[regno][GET_MODE (x)];
2209   if (regno + nregs <= info->regno)
2210     return;
2211   new_mask = (2U << (nregs - 1)) - 1;
2212   if (regno < info->regno)
2213     new_mask >>= info->regno - regno;
2214   else
2215     new_mask <<= regno - info->regno;
2216   info->mask &= ~new_mask;
2217 }
2218
2219 /* Return nonzero iff part of the return value is live during INSN, and
2220    it is likely spilled.  This can happen when more than one insn is needed
2221    to copy the return value, e.g. when we consider to combine into the
2222    second copy insn for a complex value.  */
2223
2224 static int
2225 likely_spilled_retval_p (rtx insn)
2226 {
2227   rtx use = BB_END (this_basic_block);
2228   rtx reg, p;
2229   unsigned regno, nregs;
2230   /* We assume here that no machine mode needs more than
2231      32 hard registers when the value overlaps with a register
2232      for which TARGET_FUNCTION_VALUE_REGNO_P is true.  */
2233   unsigned mask;
2234   struct likely_spilled_retval_info info;
2235
2236   if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2237     return 0;
2238   reg = XEXP (PATTERN (use), 0);
2239   if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2240     return 0;
2241   regno = REGNO (reg);
2242   nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2243   if (nregs == 1)
2244     return 0;
2245   mask = (2U << (nregs - 1)) - 1;
2246
2247   /* Disregard parts of the return value that are set later.  */
2248   info.regno = regno;
2249   info.nregs = nregs;
2250   info.mask = mask;
2251   for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2252     if (INSN_P (p))
2253       note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2254   mask = info.mask;
2255
2256   /* Check if any of the (probably) live return value registers is
2257      likely spilled.  */
2258   nregs --;
2259   do
2260     {
2261       if ((mask & 1 << nregs)
2262           && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2263         return 1;
2264     } while (nregs--);
2265   return 0;
2266 }
2267
2268 /* Adjust INSN after we made a change to its destination.
2269
2270    Changing the destination can invalidate notes that say something about
2271    the results of the insn and a LOG_LINK pointing to the insn.  */
2272
2273 static void
2274 adjust_for_new_dest (rtx insn)
2275 {
2276   /* For notes, be conservative and simply remove them.  */
2277   remove_reg_equal_equiv_notes (insn);
2278
2279   /* The new insn will have a destination that was previously the destination
2280      of an insn just above it.  Call distribute_links to make a LOG_LINK from
2281      the next use of that destination.  */
2282   distribute_links (alloc_insn_link (insn, NULL));
2283
2284   df_insn_rescan (insn);
2285 }
2286
2287 /* Return TRUE if combine can reuse reg X in mode MODE.
2288    ADDED_SETS is nonzero if the original set is still required.  */
2289 static bool
2290 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
2291 {
2292   unsigned int regno;
2293
2294   if (!REG_P(x))
2295     return false;
2296
2297   regno = REGNO (x);
2298   /* Allow hard registers if the new mode is legal, and occupies no more
2299      registers than the old mode.  */
2300   if (regno < FIRST_PSEUDO_REGISTER)
2301     return (HARD_REGNO_MODE_OK (regno, mode)
2302             && (hard_regno_nregs[regno][GET_MODE (x)]
2303                 >= hard_regno_nregs[regno][mode]));
2304
2305   /* Or a pseudo that is only used once.  */
2306   return (REG_N_SETS (regno) == 1 && !added_sets
2307           && !REG_USERVAR_P (x));
2308 }
2309
2310
2311 /* Check whether X, the destination of a set, refers to part of
2312    the register specified by REG.  */
2313
2314 static bool
2315 reg_subword_p (rtx x, rtx reg)
2316 {
2317   /* Check that reg is an integer mode register.  */
2318   if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2319     return false;
2320
2321   if (GET_CODE (x) == STRICT_LOW_PART
2322       || GET_CODE (x) == ZERO_EXTRACT)
2323     x = XEXP (x, 0);
2324
2325   return GET_CODE (x) == SUBREG
2326          && SUBREG_REG (x) == reg
2327          && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2328 }
2329
2330 #ifdef AUTO_INC_DEC
2331 /* Replace auto-increment addressing modes with explicit operations to access
2332    the same addresses without modifying the corresponding registers.  */
2333
2334 static rtx
2335 cleanup_auto_inc_dec (rtx src, enum machine_mode mem_mode)
2336 {
2337   rtx x = src;
2338   const RTX_CODE code = GET_CODE (x);
2339   int i;
2340   const char *fmt;
2341
2342   switch (code)
2343     {
2344     case REG:
2345     case CONST_INT:
2346     case CONST_DOUBLE:
2347     case CONST_FIXED:
2348     case CONST_VECTOR:
2349     case SYMBOL_REF:
2350     case CODE_LABEL:
2351     case PC:
2352     case CC0:
2353     case SCRATCH:
2354       /* SCRATCH must be shared because they represent distinct values.  */
2355       return x;
2356     case CLOBBER:
2357       if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
2358         return x;
2359       break;
2360
2361     case CONST:
2362       if (shared_const_p (x))
2363         return x;
2364       break;
2365
2366     case MEM:
2367       mem_mode = GET_MODE (x);
2368       break;
2369
2370     case PRE_INC:
2371     case PRE_DEC:
2372       gcc_assert (mem_mode != VOIDmode && mem_mode != BLKmode);
2373       return gen_rtx_PLUS (GET_MODE (x),
2374                            cleanup_auto_inc_dec (XEXP (x, 0), mem_mode),
2375                            GEN_INT (code == PRE_INC
2376                                     ? GET_MODE_SIZE (mem_mode)
2377                                     : -GET_MODE_SIZE (mem_mode)));
2378
2379     case POST_INC:
2380     case POST_DEC:
2381     case PRE_MODIFY:
2382     case POST_MODIFY:
2383       return cleanup_auto_inc_dec (code == PRE_MODIFY
2384                                    ? XEXP (x, 1) : XEXP (x, 0),
2385                                    mem_mode);
2386
2387     default:
2388       break;
2389     }
2390
2391   /* Copy the various flags, fields, and other information.  We assume
2392      that all fields need copying, and then clear the fields that should
2393      not be copied.  That is the sensible default behavior, and forces
2394      us to explicitly document why we are *not* copying a flag.  */
2395   x = shallow_copy_rtx (x);
2396
2397   /* We do not copy the USED flag, which is used as a mark bit during
2398      walks over the RTL.  */
2399   RTX_FLAG (x, used) = 0;
2400
2401   /* We do not copy FRAME_RELATED for INSNs.  */
2402   if (INSN_P (x))
2403     RTX_FLAG (x, frame_related) = 0;
2404
2405   fmt = GET_RTX_FORMAT (code);
2406   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2407     if (fmt[i] == 'e')
2408       XEXP (x, i) = cleanup_auto_inc_dec (XEXP (x, i), mem_mode);
2409     else if (fmt[i] == 'E' || fmt[i] == 'V')
2410       {
2411         int j;
2412         XVEC (x, i) = rtvec_alloc (XVECLEN (x, i));
2413         for (j = 0; j < XVECLEN (x, i); j++)
2414           XVECEXP (x, i, j)
2415             = cleanup_auto_inc_dec (XVECEXP (src, i, j), mem_mode);
2416       }
2417
2418   return x;
2419 }
2420 #endif
2421
2422 /* Auxiliary data structure for propagate_for_debug_stmt.  */
2423
2424 struct rtx_subst_pair
2425 {
2426   rtx to;
2427   bool adjusted;
2428 };
2429
2430 /* DATA points to an rtx_subst_pair.  Return the value that should be
2431    substituted.  */
2432
2433 static rtx
2434 propagate_for_debug_subst (rtx from, const_rtx old_rtx, void *data)
2435 {
2436   struct rtx_subst_pair *pair = (struct rtx_subst_pair *)data;
2437
2438   if (!rtx_equal_p (from, old_rtx))
2439     return NULL_RTX;
2440   if (!pair->adjusted)
2441     {
2442       pair->adjusted = true;
2443 #ifdef AUTO_INC_DEC
2444       pair->to = cleanup_auto_inc_dec (pair->to, VOIDmode);
2445 #else
2446       pair->to = copy_rtx (pair->to);
2447 #endif
2448       pair->to = make_compound_operation (pair->to, SET);
2449       return pair->to;
2450     }
2451   return copy_rtx (pair->to);
2452 }
2453
2454 /* Replace all the occurrences of DEST with SRC in DEBUG_INSNs between INSN
2455    and LAST, not including INSN, but including LAST.  Also stop at the end
2456    of THIS_BASIC_BLOCK.  */
2457
2458 static void
2459 propagate_for_debug (rtx insn, rtx last, rtx dest, rtx src)
2460 {
2461   rtx next, loc, end = NEXT_INSN (BB_END (this_basic_block));
2462
2463   struct rtx_subst_pair p;
2464   p.to = src;
2465   p.adjusted = false;
2466
2467   next = NEXT_INSN (insn);
2468   last = NEXT_INSN (last);
2469   while (next != last && next != end)
2470     {
2471       insn = next;
2472       next = NEXT_INSN (insn);
2473       if (DEBUG_INSN_P (insn))
2474         {
2475           loc = simplify_replace_fn_rtx (INSN_VAR_LOCATION_LOC (insn),
2476                                          dest, propagate_for_debug_subst, &p);
2477           if (loc == INSN_VAR_LOCATION_LOC (insn))
2478             continue;
2479           INSN_VAR_LOCATION_LOC (insn) = loc;
2480           df_insn_rescan (insn);
2481         }
2482     }
2483 }
2484
2485 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2486    Note that the INSN should be deleted *after* removing dead edges, so
2487    that the kept edge is the fallthrough edge for a (set (pc) (pc))
2488    but not for a (set (pc) (label_ref FOO)).  */
2489
2490 static void
2491 update_cfg_for_uncondjump (rtx insn)
2492 {
2493   basic_block bb = BLOCK_FOR_INSN (insn);
2494   gcc_assert (BB_END (bb) == insn);
2495
2496   purge_dead_edges (bb);
2497
2498   delete_insn (insn);
2499   if (EDGE_COUNT (bb->succs) == 1)
2500     {
2501       rtx insn;
2502
2503       single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2504
2505       /* Remove barriers from the footer if there are any.  */
2506       for (insn = bb->il.rtl->footer; insn; insn = NEXT_INSN (insn))
2507         if (BARRIER_P (insn))
2508           {
2509             if (PREV_INSN (insn))
2510               NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2511             else
2512               bb->il.rtl->footer = NEXT_INSN (insn);
2513             if (NEXT_INSN (insn))
2514               PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2515           }
2516         else if (LABEL_P (insn))
2517           break;
2518     }
2519 }
2520
2521 /* Try to combine the insns I0, I1 and I2 into I3.
2522    Here I0, I1 and I2 appear earlier than I3.
2523    I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2524    I3.
2525
2526    If we are combining more than two insns and the resulting insn is not
2527    recognized, try splitting it into two insns.  If that happens, I2 and I3
2528    are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2529    Otherwise, I0, I1 and I2 are pseudo-deleted.
2530
2531    Return 0 if the combination does not work.  Then nothing is changed.
2532    If we did the combination, return the insn at which combine should
2533    resume scanning.
2534
2535    Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2536    new direct jump instruction.
2537
2538    LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2539    been I3 passed to an earlier try_combine within the same basic
2540    block.  */
2541
2542 static rtx
2543 try_combine (rtx i3, rtx i2, rtx i1, rtx i0, int *new_direct_jump_p,
2544              rtx last_combined_insn)
2545 {
2546   /* New patterns for I3 and I2, respectively.  */
2547   rtx newpat, newi2pat = 0;
2548   rtvec newpat_vec_with_clobbers = 0;
2549   int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2550   /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2551      dead.  */
2552   int added_sets_0, added_sets_1, added_sets_2;
2553   /* Total number of SETs to put into I3.  */
2554   int total_sets;
2555   /* Nonzero if I2's or I1's body now appears in I3.  */
2556   int i2_is_used = 0, i1_is_used = 0;
2557   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
2558   int insn_code_number, i2_code_number = 0, other_code_number = 0;
2559   /* Contains I3 if the destination of I3 is used in its source, which means
2560      that the old life of I3 is being killed.  If that usage is placed into
2561      I2 and not in I3, a REG_DEAD note must be made.  */
2562   rtx i3dest_killed = 0;
2563   /* SET_DEST and SET_SRC of I2, I1 and I0.  */
2564   rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2565   /* Copy of SET_SRC of I1, if needed.  */
2566   rtx i1src_copy = 0;
2567   /* Set if I2DEST was reused as a scratch register.  */
2568   bool i2scratch = false;
2569   /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases.  */
2570   rtx i0pat = 0, i1pat = 0, i2pat = 0;
2571   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
2572   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2573   int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2574   int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2575   int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2576   /* Notes that must be added to REG_NOTES in I3 and I2.  */
2577   rtx new_i3_notes, new_i2_notes;
2578   /* Notes that we substituted I3 into I2 instead of the normal case.  */
2579   int i3_subst_into_i2 = 0;
2580   /* Notes that I1, I2 or I3 is a MULT operation.  */
2581   int have_mult = 0;
2582   int swap_i2i3 = 0;
2583   int changed_i3_dest = 0;
2584
2585   int maxreg;
2586   rtx temp;
2587   struct insn_link *link;
2588   rtx other_pat = 0;
2589   rtx new_other_notes;
2590   int i;
2591
2592   /* Only try four-insn combinations when there's high likelihood of
2593      success.  Look for simple insns, such as loads of constants or
2594      binary operations involving a constant.  */
2595   if (i0)
2596     {
2597       int i;
2598       int ngood = 0;
2599       int nshift = 0;
2600
2601       if (!flag_expensive_optimizations)
2602         return 0;
2603
2604       for (i = 0; i < 4; i++)
2605         {
2606           rtx insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2607           rtx set = single_set (insn);
2608           rtx src;
2609           if (!set)
2610             continue;
2611           src = SET_SRC (set);
2612           if (CONSTANT_P (src))
2613             {
2614               ngood += 2;
2615               break;
2616             }
2617           else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2618             ngood++;
2619           else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2620                    || GET_CODE (src) == LSHIFTRT)
2621             nshift++;
2622         }
2623       if (ngood < 2 && nshift < 2)
2624         return 0;
2625     }
2626
2627   /* Exit early if one of the insns involved can't be used for
2628      combinations.  */
2629   if (cant_combine_insn_p (i3)
2630       || cant_combine_insn_p (i2)
2631       || (i1 && cant_combine_insn_p (i1))
2632       || (i0 && cant_combine_insn_p (i0))
2633       || likely_spilled_retval_p (i3))
2634     return 0;
2635
2636   combine_attempts++;
2637   undobuf.other_insn = 0;
2638
2639   /* Reset the hard register usage information.  */
2640   CLEAR_HARD_REG_SET (newpat_used_regs);
2641
2642   if (dump_file && (dump_flags & TDF_DETAILS))
2643     {
2644       if (i0)
2645         fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2646                  INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2647       else if (i1)
2648         fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2649                  INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2650       else
2651         fprintf (dump_file, "\nTrying %d -> %d:\n",
2652                  INSN_UID (i2), INSN_UID (i3));
2653     }
2654
2655   /* If multiple insns feed into one of I2 or I3, they can be in any
2656      order.  To simplify the code below, reorder them in sequence.  */
2657   if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2658     temp = i2, i2 = i0, i0 = temp;
2659   if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2660     temp = i1, i1 = i0, i0 = temp;
2661   if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2662     temp = i1, i1 = i2, i2 = temp;
2663
2664   added_links_insn = 0;
2665
2666   /* First check for one important special case that the code below will
2667      not handle.  Namely, the case where I1 is zero, I2 is a PARALLEL
2668      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
2669      we may be able to replace that destination with the destination of I3.
2670      This occurs in the common code where we compute both a quotient and
2671      remainder into a structure, in which case we want to do the computation
2672      directly into the structure to avoid register-register copies.
2673
2674      Note that this case handles both multiple sets in I2 and also cases
2675      where I2 has a number of CLOBBERs inside the PARALLEL.
2676
2677      We make very conservative checks below and only try to handle the
2678      most common cases of this.  For example, we only handle the case
2679      where I2 and I3 are adjacent to avoid making difficult register
2680      usage tests.  */
2681
2682   if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2683       && REG_P (SET_SRC (PATTERN (i3)))
2684       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2685       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2686       && GET_CODE (PATTERN (i2)) == PARALLEL
2687       && ! side_effects_p (SET_DEST (PATTERN (i3)))
2688       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2689          below would need to check what is inside (and reg_overlap_mentioned_p
2690          doesn't support those codes anyway).  Don't allow those destinations;
2691          the resulting insn isn't likely to be recognized anyway.  */
2692       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2693       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2694       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2695                                     SET_DEST (PATTERN (i3)))
2696       && next_active_insn (i2) == i3)
2697     {
2698       rtx p2 = PATTERN (i2);
2699
2700       /* Make sure that the destination of I3,
2701          which we are going to substitute into one output of I2,
2702          is not used within another output of I2.  We must avoid making this:
2703          (parallel [(set (mem (reg 69)) ...)
2704                     (set (reg 69) ...)])
2705          which is not well-defined as to order of actions.
2706          (Besides, reload can't handle output reloads for this.)
2707
2708          The problem can also happen if the dest of I3 is a memory ref,
2709          if another dest in I2 is an indirect memory ref.  */
2710       for (i = 0; i < XVECLEN (p2, 0); i++)
2711         if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2712              || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2713             && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2714                                         SET_DEST (XVECEXP (p2, 0, i))))
2715           break;
2716
2717       if (i == XVECLEN (p2, 0))
2718         for (i = 0; i < XVECLEN (p2, 0); i++)
2719           if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2720               && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2721             {
2722               combine_merges++;
2723
2724               subst_insn = i3;
2725               subst_low_luid = DF_INSN_LUID (i2);
2726
2727               added_sets_2 = added_sets_1 = added_sets_0 = 0;
2728               i2src = SET_SRC (XVECEXP (p2, 0, i));
2729               i2dest = SET_DEST (XVECEXP (p2, 0, i));
2730               i2dest_killed = dead_or_set_p (i2, i2dest);
2731
2732               /* Replace the dest in I2 with our dest and make the resulting
2733                  insn the new pattern for I3.  Then skip to where we validate
2734                  the pattern.  Everything was set up above.  */
2735               SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2736               newpat = p2;
2737               i3_subst_into_i2 = 1;
2738               goto validate_replacement;
2739             }
2740     }
2741
2742   /* If I2 is setting a pseudo to a constant and I3 is setting some
2743      sub-part of it to another constant, merge them by making a new
2744      constant.  */
2745   if (i1 == 0
2746       && (temp = single_set (i2)) != 0
2747       && (CONST_INT_P (SET_SRC (temp))
2748           || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
2749       && GET_CODE (PATTERN (i3)) == SET
2750       && (CONST_INT_P (SET_SRC (PATTERN (i3)))
2751           || GET_CODE (SET_SRC (PATTERN (i3))) == CONST_DOUBLE)
2752       && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp)))
2753     {
2754       rtx dest = SET_DEST (PATTERN (i3));
2755       int offset = -1;
2756       int width = 0;
2757
2758       if (GET_CODE (dest) == ZERO_EXTRACT)
2759         {
2760           if (CONST_INT_P (XEXP (dest, 1))
2761               && CONST_INT_P (XEXP (dest, 2)))
2762             {
2763               width = INTVAL (XEXP (dest, 1));
2764               offset = INTVAL (XEXP (dest, 2));
2765               dest = XEXP (dest, 0);
2766               if (BITS_BIG_ENDIAN)
2767                 offset = GET_MODE_BITSIZE (GET_MODE (dest)) - width - offset;
2768             }
2769         }
2770       else
2771         {
2772           if (GET_CODE (dest) == STRICT_LOW_PART)
2773             dest = XEXP (dest, 0);
2774           width = GET_MODE_BITSIZE (GET_MODE (dest));
2775           offset = 0;
2776         }
2777
2778       if (offset >= 0)
2779         {
2780           /* If this is the low part, we're done.  */
2781           if (subreg_lowpart_p (dest))
2782             ;
2783           /* Handle the case where inner is twice the size of outer.  */
2784           else if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2785                    == 2 * GET_MODE_BITSIZE (GET_MODE (dest)))
2786             offset += GET_MODE_BITSIZE (GET_MODE (dest));
2787           /* Otherwise give up for now.  */
2788           else
2789             offset = -1;
2790         }
2791
2792       if (offset >= 0
2793           && (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2794               <= HOST_BITS_PER_DOUBLE_INT))
2795         {
2796           double_int m, o, i;
2797           rtx inner = SET_SRC (PATTERN (i3));
2798           rtx outer = SET_SRC (temp);
2799
2800           o = rtx_to_double_int (outer);
2801           i = rtx_to_double_int (inner);
2802
2803           m = double_int_mask (width);
2804           i = double_int_and (i, m);
2805           m = double_int_lshift (m, offset, HOST_BITS_PER_DOUBLE_INT, false);
2806           i = double_int_lshift (i, offset, HOST_BITS_PER_DOUBLE_INT, false);
2807           o = double_int_ior (double_int_and_not (o, m), i);
2808
2809           combine_merges++;
2810           subst_insn = i3;
2811           subst_low_luid = DF_INSN_LUID (i2);
2812           added_sets_2 = added_sets_1 = added_sets_0 = 0;
2813           i2dest = SET_DEST (temp);
2814           i2dest_killed = dead_or_set_p (i2, i2dest);
2815
2816           /* Replace the source in I2 with the new constant and make the
2817              resulting insn the new pattern for I3.  Then skip to where we
2818              validate the pattern.  Everything was set up above.  */
2819           SUBST (SET_SRC (temp),
2820                  immed_double_int_const (o, GET_MODE (SET_DEST (temp))));
2821
2822           newpat = PATTERN (i2);
2823
2824           /* The dest of I3 has been replaced with the dest of I2.  */
2825           changed_i3_dest = 1;
2826           goto validate_replacement;
2827         }
2828     }
2829
2830 #ifndef HAVE_cc0
2831   /* If we have no I1 and I2 looks like:
2832         (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2833                    (set Y OP)])
2834      make up a dummy I1 that is
2835         (set Y OP)
2836      and change I2 to be
2837         (set (reg:CC X) (compare:CC Y (const_int 0)))
2838
2839      (We can ignore any trailing CLOBBERs.)
2840
2841      This undoes a previous combination and allows us to match a branch-and-
2842      decrement insn.  */
2843
2844   if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2845       && XVECLEN (PATTERN (i2), 0) >= 2
2846       && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2847       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2848           == MODE_CC)
2849       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2850       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2851       && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2852       && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2853       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2854                       SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2855     {
2856       for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2857         if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2858           break;
2859
2860       if (i == 1)
2861         {
2862           /* We make I1 with the same INSN_UID as I2.  This gives it
2863              the same DF_INSN_LUID for value tracking.  Our fake I1 will
2864              never appear in the insn stream so giving it the same INSN_UID
2865              as I2 will not cause a problem.  */
2866
2867           i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
2868                              BLOCK_FOR_INSN (i2), XVECEXP (PATTERN (i2), 0, 1),
2869                              INSN_LOCATOR (i2), -1, NULL_RTX);
2870
2871           SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2872           SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2873                  SET_DEST (PATTERN (i1)));
2874         }
2875     }
2876 #endif
2877
2878   /* Verify that I2 and I1 are valid for combining.  */
2879   if (! can_combine_p (i2, i3, i0, i1, NULL_RTX, NULL_RTX, &i2dest, &i2src)
2880       || (i1 && ! can_combine_p (i1, i3, i0, NULL_RTX, i2, NULL_RTX,
2881                                  &i1dest, &i1src))
2882       || (i0 && ! can_combine_p (i0, i3, NULL_RTX, NULL_RTX, i1, i2,
2883                                  &i0dest, &i0src)))
2884     {
2885       undo_all ();
2886       return 0;
2887     }
2888
2889   /* Record whether I2DEST is used in I2SRC and similarly for the other
2890      cases.  Knowing this will help in register status updating below.  */
2891   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2892   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2893   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2894   i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2895   i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2896   i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2897   i2dest_killed = dead_or_set_p (i2, i2dest);
2898   i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2899   i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
2900
2901   /* For the earlier insns, determine which of the subsequent ones they
2902      feed.  */
2903   i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
2904   i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
2905   i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
2906                           : (!reg_overlap_mentioned_p (i1dest, i0dest)
2907                              && reg_overlap_mentioned_p (i0dest, i2src))));
2908
2909   /* Ensure that I3's pattern can be the destination of combines.  */
2910   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
2911                           i1 && i2dest_in_i1src && !i1_feeds_i2_n,
2912                           i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
2913                                  || (i1dest_in_i0src && !i0_feeds_i1_n)),
2914                           &i3dest_killed))
2915     {
2916       undo_all ();
2917       return 0;
2918     }
2919
2920   /* See if any of the insns is a MULT operation.  Unless one is, we will
2921      reject a combination that is, since it must be slower.  Be conservative
2922      here.  */
2923   if (GET_CODE (i2src) == MULT
2924       || (i1 != 0 && GET_CODE (i1src) == MULT)
2925       || (i0 != 0 && GET_CODE (i0src) == MULT)
2926       || (GET_CODE (PATTERN (i3)) == SET
2927           && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2928     have_mult = 1;
2929
2930   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2931      We used to do this EXCEPT in one case: I3 has a post-inc in an
2932      output operand.  However, that exception can give rise to insns like
2933         mov r3,(r3)+
2934      which is a famous insn on the PDP-11 where the value of r3 used as the
2935      source was model-dependent.  Avoid this sort of thing.  */
2936
2937 #if 0
2938   if (!(GET_CODE (PATTERN (i3)) == SET
2939         && REG_P (SET_SRC (PATTERN (i3)))
2940         && MEM_P (SET_DEST (PATTERN (i3)))
2941         && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2942             || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2943     /* It's not the exception.  */
2944 #endif
2945 #ifdef AUTO_INC_DEC
2946     {
2947       rtx link;
2948       for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2949         if (REG_NOTE_KIND (link) == REG_INC
2950             && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2951                 || (i1 != 0
2952                     && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2953           {
2954             undo_all ();
2955             return 0;
2956           }
2957     }
2958 #endif
2959
2960   /* See if the SETs in I1 or I2 need to be kept around in the merged
2961      instruction: whenever the value set there is still needed past I3.
2962      For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
2963
2964      For the SET in I1, we have two cases:  If I1 and I2 independently
2965      feed into I3, the set in I1 needs to be kept around if I1DEST dies
2966      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
2967      in I1 needs to be kept around unless I1DEST dies or is set in either
2968      I2 or I3.  The same consideration applies to I0.  */
2969
2970   added_sets_2 = !dead_or_set_p (i3, i2dest);
2971
2972   if (i1)
2973     added_sets_1 = !(dead_or_set_p (i3, i1dest)
2974                      || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
2975   else
2976     added_sets_1 = 0;
2977
2978   if (i0)
2979     added_sets_0 =  !(dead_or_set_p (i3, i0dest)
2980                       || (i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
2981                       || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)));
2982   else
2983     added_sets_0 = 0;
2984
2985   /* We are about to copy insns for the case where they need to be kept
2986      around.  Check that they can be copied in the merged instruction.  */
2987
2988   if (targetm.cannot_copy_insn_p
2989       && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
2990           || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
2991           || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
2992     {
2993       undo_all ();
2994       return 0;
2995     }
2996
2997   /* If the set in I2 needs to be kept around, we must make a copy of
2998      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2999      PATTERN (I2), we are only substituting for the original I1DEST, not into
3000      an already-substituted copy.  This also prevents making self-referential
3001      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3002      I2DEST.  */
3003
3004   if (added_sets_2)
3005     {
3006       if (GET_CODE (PATTERN (i2)) == PARALLEL)
3007         i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
3008       else
3009         i2pat = copy_rtx (PATTERN (i2));
3010     }
3011
3012   if (added_sets_1)
3013     {
3014       if (GET_CODE (PATTERN (i1)) == PARALLEL)
3015         i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
3016       else
3017         i1pat = copy_rtx (PATTERN (i1));
3018     }
3019
3020   if (added_sets_0)
3021     {
3022       if (GET_CODE (PATTERN (i0)) == PARALLEL)
3023         i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
3024       else
3025         i0pat = copy_rtx (PATTERN (i0));
3026     }
3027
3028   combine_merges++;
3029
3030   /* Substitute in the latest insn for the regs set by the earlier ones.  */
3031
3032   maxreg = max_reg_num ();
3033
3034   subst_insn = i3;
3035
3036 #ifndef HAVE_cc0
3037   /* Many machines that don't use CC0 have insns that can both perform an
3038      arithmetic operation and set the condition code.  These operations will
3039      be represented as a PARALLEL with the first element of the vector
3040      being a COMPARE of an arithmetic operation with the constant zero.
3041      The second element of the vector will set some pseudo to the result
3042      of the same arithmetic operation.  If we simplify the COMPARE, we won't
3043      match such a pattern and so will generate an extra insn.   Here we test
3044      for this case, where both the comparison and the operation result are
3045      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3046      I2SRC.  Later we will make the PARALLEL that contains I2.  */
3047
3048   if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3049       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3050       && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3051       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3052     {
3053       rtx newpat_dest;
3054       rtx *cc_use_loc = NULL, cc_use_insn = NULL_RTX;
3055       rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3056       enum machine_mode compare_mode, orig_compare_mode;
3057       enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3058
3059       newpat = PATTERN (i3);
3060       newpat_dest = SET_DEST (newpat);
3061       compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3062
3063       if (undobuf.other_insn == 0
3064           && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3065                                             &cc_use_insn)))
3066         {
3067           compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3068           compare_code = simplify_compare_const (compare_code,
3069                                                  op0, &op1);
3070 #ifdef CANONICALIZE_COMPARISON
3071           CANONICALIZE_COMPARISON (compare_code, op0, op1);
3072 #endif
3073         }
3074
3075       /* Do the rest only if op1 is const0_rtx, which may be the
3076          result of simplification.  */
3077       if (op1 == const0_rtx)
3078         {
3079           /* If a single use of the CC is found, prepare to modify it
3080              when SELECT_CC_MODE returns a new CC-class mode, or when
3081              the above simplify_compare_const() returned a new comparison
3082              operator.  undobuf.other_insn is assigned the CC use insn
3083              when modifying it.  */
3084           if (cc_use_loc)
3085             {
3086 #ifdef SELECT_CC_MODE
3087               enum machine_mode new_mode
3088                 = SELECT_CC_MODE (compare_code, op0, op1);
3089               if (new_mode != orig_compare_mode
3090                   && can_change_dest_mode (SET_DEST (newpat),
3091                                            added_sets_2, new_mode))
3092                 {
3093                   unsigned int regno = REGNO (newpat_dest);
3094                   compare_mode = new_mode;
3095                   if (regno < FIRST_PSEUDO_REGISTER)
3096                     newpat_dest = gen_rtx_REG (compare_mode, regno);
3097                   else
3098                     {
3099                       SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3100                       newpat_dest = regno_reg_rtx[regno];
3101                     }
3102                 }
3103 #endif
3104               /* Cases for modifying the CC-using comparison.  */
3105               if (compare_code != orig_compare_code
3106                   /* ??? Do we need to verify the zero rtx?  */
3107                   && XEXP (*cc_use_loc, 1) == const0_rtx)
3108                 {
3109                   /* Replace cc_use_loc with entire new RTX.  */
3110                   SUBST (*cc_use_loc,
3111                          gen_rtx_fmt_ee (compare_code, compare_mode,
3112                                          newpat_dest, const0_rtx));
3113                   undobuf.other_insn = cc_use_insn;
3114                 }
3115               else if (compare_mode != orig_compare_mode)
3116                 {
3117                   /* Just replace the CC reg with a new mode.  */
3118                   SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3119                   undobuf.other_insn = cc_use_insn;
3120                 }             
3121             }
3122
3123           /* Now we modify the current newpat:
3124              First, SET_DEST(newpat) is updated if the CC mode has been
3125              altered. For targets without SELECT_CC_MODE, this should be
3126              optimized away.  */
3127           if (compare_mode != orig_compare_mode)
3128             SUBST (SET_DEST (newpat), newpat_dest);
3129           /* This is always done to propagate i2src into newpat.  */
3130           SUBST (SET_SRC (newpat),
3131                  gen_rtx_COMPARE (compare_mode, op0, op1));
3132           /* Create new version of i2pat if needed; the below PARALLEL
3133              creation needs this to work correctly.  */
3134           if (! rtx_equal_p (i2src, op0))
3135             i2pat = gen_rtx_SET (VOIDmode, i2dest, op0);
3136           i2_is_used = 1;
3137         }
3138     }
3139 #endif
3140
3141   if (i2_is_used == 0)
3142     {
3143       /* It is possible that the source of I2 or I1 may be performing
3144          an unneeded operation, such as a ZERO_EXTEND of something
3145          that is known to have the high part zero.  Handle that case
3146          by letting subst look at the inner insns.
3147
3148          Another way to do this would be to have a function that tries
3149          to simplify a single insn instead of merging two or more
3150          insns.  We don't do this because of the potential of infinite
3151          loops and because of the potential extra memory required.
3152          However, doing it the way we are is a bit of a kludge and
3153          doesn't catch all cases.
3154
3155          But only do this if -fexpensive-optimizations since it slows
3156          things down and doesn't usually win.
3157
3158          This is not done in the COMPARE case above because the
3159          unmodified I2PAT is used in the PARALLEL and so a pattern
3160          with a modified I2SRC would not match.  */
3161
3162       if (flag_expensive_optimizations)
3163         {
3164           /* Pass pc_rtx so no substitutions are done, just
3165              simplifications.  */
3166           if (i1)
3167             {
3168               subst_low_luid = DF_INSN_LUID (i1);
3169               i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3170             }
3171
3172           subst_low_luid = DF_INSN_LUID (i2);
3173           i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3174         }
3175
3176       n_occurrences = 0;                /* `subst' counts here */
3177       subst_low_luid = DF_INSN_LUID (i2);
3178
3179       /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3180          copy of I2SRC each time we substitute it, in order to avoid creating
3181          self-referential RTL when we will be substituting I1SRC for I1DEST
3182          later.  Likewise if I0 feeds into I2, either directly or indirectly
3183          through I1, and I0DEST is in I0SRC.  */
3184       newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3185                       (i1_feeds_i2_n && i1dest_in_i1src)
3186                       || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3187                           && i0dest_in_i0src));
3188       substed_i2 = 1;
3189
3190       /* Record whether I2's body now appears within I3's body.  */
3191       i2_is_used = n_occurrences;
3192     }
3193
3194   /* If we already got a failure, don't try to do more.  Otherwise, try to
3195      substitute I1 if we have it.  */
3196
3197   if (i1 && GET_CODE (newpat) != CLOBBER)
3198     {
3199       /* Check that an autoincrement side-effect on I1 has not been lost.
3200          This happens if I1DEST is mentioned in I2 and dies there, and
3201          has disappeared from the new pattern.  */
3202       if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3203            && i1_feeds_i2_n
3204            && dead_or_set_p (i2, i1dest)
3205            && !reg_overlap_mentioned_p (i1dest, newpat))
3206            /* Before we can do this substitution, we must redo the test done
3207               above (see detailed comments there) that ensures I1DEST isn't
3208               mentioned in any SETs in NEWPAT that are field assignments.  */
3209           || !combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX, NULL_RTX,
3210                                 0, 0, 0))
3211         {
3212           undo_all ();
3213           return 0;
3214         }
3215
3216       n_occurrences = 0;
3217       subst_low_luid = DF_INSN_LUID (i1);
3218
3219       /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3220          copy of I1SRC each time we substitute it, in order to avoid creating
3221          self-referential RTL when we will be substituting I0SRC for I0DEST
3222          later.  */
3223       newpat = subst (newpat, i1dest, i1src, 0, 0,
3224                       i0_feeds_i1_n && i0dest_in_i0src);
3225       substed_i1 = 1;
3226
3227       /* Record whether I1's body now appears within I3's body.  */
3228       i1_is_used = n_occurrences;
3229     }
3230
3231   /* Likewise for I0 if we have it.  */
3232
3233   if (i0 && GET_CODE (newpat) != CLOBBER)
3234     {
3235       if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3236            && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3237                || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3238            && !reg_overlap_mentioned_p (i0dest, newpat))
3239           || !combinable_i3pat (NULL_RTX, &newpat, i0dest, NULL_RTX, NULL_RTX,
3240                                 0, 0, 0))
3241         {
3242           undo_all ();
3243           return 0;
3244         }
3245
3246       /* If the following substitution will modify I1SRC, make a copy of it
3247          for the case where it is substituted for I1DEST in I2PAT later.  */
3248       if (i0_feeds_i1_n && added_sets_2 && i1_feeds_i2_n)
3249         i1src_copy = copy_rtx (i1src);
3250
3251       n_occurrences = 0;
3252       subst_low_luid = DF_INSN_LUID (i0);
3253       newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3254       substed_i0 = 1;
3255     }
3256
3257   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
3258      to count all the ways that I2SRC and I1SRC can be used.  */
3259   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3260        && i2_is_used + added_sets_2 > 1)
3261       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3262           && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3263               > 1))
3264       || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3265           && (n_occurrences + added_sets_0
3266               + (added_sets_1 && i0_feeds_i1_n)
3267               + (added_sets_2 && i0_feeds_i2_n)
3268               > 1))
3269       /* Fail if we tried to make a new register.  */
3270       || max_reg_num () != maxreg
3271       /* Fail if we couldn't do something and have a CLOBBER.  */
3272       || GET_CODE (newpat) == CLOBBER
3273       /* Fail if this new pattern is a MULT and we didn't have one before
3274          at the outer level.  */
3275       || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3276           && ! have_mult))
3277     {
3278       undo_all ();
3279       return 0;
3280     }
3281
3282   /* If the actions of the earlier insns must be kept
3283      in addition to substituting them into the latest one,
3284      we must make a new PARALLEL for the latest insn
3285      to hold additional the SETs.  */
3286
3287   if (added_sets_0 || added_sets_1 || added_sets_2)
3288     {
3289       int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3290       combine_extras++;
3291
3292       if (GET_CODE (newpat) == PARALLEL)
3293         {
3294           rtvec old = XVEC (newpat, 0);
3295           total_sets = XVECLEN (newpat, 0) + extra_sets;
3296           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3297           memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3298                   sizeof (old->elem[0]) * old->num_elem);
3299         }
3300       else
3301         {
3302           rtx old = newpat;
3303           total_sets = 1 + extra_sets;
3304           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3305           XVECEXP (newpat, 0, 0) = old;
3306         }
3307
3308       if (added_sets_0)
3309         XVECEXP (newpat, 0, --total_sets) = i0pat;
3310
3311       if (added_sets_1)
3312         {
3313           rtx t = i1pat;
3314           if (i0_feeds_i1_n)
3315             t = subst (t, i0dest, i0src, 0, 0, 0);
3316
3317           XVECEXP (newpat, 0, --total_sets) = t;
3318         }
3319       if (added_sets_2)
3320         {
3321           rtx t = i2pat;
3322           if (i1_feeds_i2_n)
3323             t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3324                        i0_feeds_i1_n && i0dest_in_i0src);
3325           if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3326             t = subst (t, i0dest, i0src, 0, 0, 0);
3327
3328           XVECEXP (newpat, 0, --total_sets) = t;
3329         }
3330     }
3331
3332  validate_replacement:
3333
3334   /* Note which hard regs this insn has as inputs.  */
3335   mark_used_regs_combine (newpat);
3336
3337   /* If recog_for_combine fails, it strips existing clobbers.  If we'll
3338      consider splitting this pattern, we might need these clobbers.  */
3339   if (i1 && GET_CODE (newpat) == PARALLEL
3340       && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3341     {
3342       int len = XVECLEN (newpat, 0);
3343
3344       newpat_vec_with_clobbers = rtvec_alloc (len);
3345       for (i = 0; i < len; i++)
3346         RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3347     }
3348
3349   /* Is the result of combination a valid instruction?  */
3350   insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3351
3352   /* If the result isn't valid, see if it is a PARALLEL of two SETs where
3353      the second SET's destination is a register that is unused and isn't
3354      marked as an instruction that might trap in an EH region.  In that case,
3355      we just need the first SET.   This can occur when simplifying a divmod
3356      insn.  We *must* test for this case here because the code below that
3357      splits two independent SETs doesn't handle this case correctly when it
3358      updates the register status.
3359
3360      It's pointless doing this if we originally had two sets, one from
3361      i3, and one from i2.  Combining then splitting the parallel results
3362      in the original i2 again plus an invalid insn (which we delete).
3363      The net effect is only to move instructions around, which makes
3364      debug info less accurate.
3365
3366      Also check the case where the first SET's destination is unused.
3367      That would not cause incorrect code, but does cause an unneeded
3368      insn to remain.  */
3369
3370   if (insn_code_number < 0
3371       && !(added_sets_2 && i1 == 0)
3372       && GET_CODE (newpat) == PARALLEL
3373       && XVECLEN (newpat, 0) == 2
3374       && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3375       && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3376       && asm_noperands (newpat) < 0)
3377     {
3378       rtx set0 = XVECEXP (newpat, 0, 0);
3379       rtx set1 = XVECEXP (newpat, 0, 1);
3380
3381       if (((REG_P (SET_DEST (set1))
3382             && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3383            || (GET_CODE (SET_DEST (set1)) == SUBREG
3384                && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3385           && insn_nothrow_p (i3)
3386           && !side_effects_p (SET_SRC (set1)))
3387         {
3388           newpat = set0;
3389           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3390         }
3391
3392       else if (((REG_P (SET_DEST (set0))
3393                  && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3394                 || (GET_CODE (SET_DEST (set0)) == SUBREG
3395                     && find_reg_note (i3, REG_UNUSED,
3396                                       SUBREG_REG (SET_DEST (set0)))))
3397                && insn_nothrow_p (i3)
3398                && !side_effects_p (SET_SRC (set0)))
3399         {
3400           newpat = set1;
3401           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3402
3403           if (insn_code_number >= 0)
3404             changed_i3_dest = 1;
3405         }
3406     }
3407
3408   /* If we were combining three insns and the result is a simple SET
3409      with no ASM_OPERANDS that wasn't recognized, try to split it into two
3410      insns.  There are two ways to do this.  It can be split using a
3411      machine-specific method (like when you have an addition of a large
3412      constant) or by combine in the function find_split_point.  */
3413
3414   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3415       && asm_noperands (newpat) < 0)
3416     {
3417       rtx parallel, m_split, *split;
3418
3419       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
3420          use I2DEST as a scratch register will help.  In the latter case,
3421          convert I2DEST to the mode of the source of NEWPAT if we can.  */
3422
3423       m_split = combine_split_insns (newpat, i3);
3424
3425       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3426          inputs of NEWPAT.  */
3427
3428       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3429          possible to try that as a scratch reg.  This would require adding
3430          more code to make it work though.  */
3431
3432       if (m_split == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3433         {
3434           enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3435
3436           /* First try to split using the original register as a
3437              scratch register.  */
3438           parallel = gen_rtx_PARALLEL (VOIDmode,
3439                                        gen_rtvec (2, newpat,
3440                                                   gen_rtx_CLOBBER (VOIDmode,
3441                                                                    i2dest)));
3442           m_split = combine_split_insns (parallel, i3);
3443
3444           /* If that didn't work, try changing the mode of I2DEST if
3445              we can.  */
3446           if (m_split == 0
3447               && new_mode != GET_MODE (i2dest)
3448               && new_mode != VOIDmode
3449               && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3450             {
3451               enum machine_mode old_mode = GET_MODE (i2dest);
3452               rtx ni2dest;
3453
3454               if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3455                 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3456               else
3457                 {
3458                   SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3459                   ni2dest = regno_reg_rtx[REGNO (i2dest)];
3460                 }
3461
3462               parallel = (gen_rtx_PARALLEL
3463                           (VOIDmode,
3464                            gen_rtvec (2, newpat,
3465                                       gen_rtx_CLOBBER (VOIDmode,
3466                                                        ni2dest))));
3467               m_split = combine_split_insns (parallel, i3);
3468
3469               if (m_split == 0
3470                   && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3471                 {
3472                   struct undo *buf;
3473
3474                   adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3475                   buf = undobuf.undos;
3476                   undobuf.undos = buf->next;
3477                   buf->next = undobuf.frees;
3478                   undobuf.frees = buf;
3479                 }
3480             }
3481
3482           i2scratch = m_split != 0;
3483         }
3484
3485       /* If recog_for_combine has discarded clobbers, try to use them
3486          again for the split.  */
3487       if (m_split == 0 && newpat_vec_with_clobbers)
3488         {
3489           parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3490           m_split = combine_split_insns (parallel, i3);
3491         }
3492
3493       if (m_split && NEXT_INSN (m_split) == NULL_RTX)
3494         {
3495           m_split = PATTERN (m_split);
3496           insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
3497           if (insn_code_number >= 0)
3498             newpat = m_split;
3499         }
3500       else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
3501                && (next_nonnote_nondebug_insn (i2) == i3
3502                    || ! use_crosses_set_p (PATTERN (m_split), DF_INSN_LUID (i2))))
3503         {
3504           rtx i2set, i3set;
3505           rtx newi3pat = PATTERN (NEXT_INSN (m_split));
3506           newi2pat = PATTERN (m_split);
3507
3508           i3set = single_set (NEXT_INSN (m_split));
3509           i2set = single_set (m_split);
3510
3511           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3512
3513           /* If I2 or I3 has multiple SETs, we won't know how to track
3514              register status, so don't use these insns.  If I2's destination
3515              is used between I2 and I3, we also can't use these insns.  */
3516
3517           if (i2_code_number >= 0 && i2set && i3set
3518               && (next_nonnote_nondebug_insn (i2) == i3
3519                   || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3520             insn_code_number = recog_for_combine (&newi3pat, i3,
3521                                                   &new_i3_notes);
3522           if (insn_code_number >= 0)
3523             newpat = newi3pat;
3524
3525           /* It is possible that both insns now set the destination of I3.
3526              If so, we must show an extra use of it.  */
3527
3528           if (insn_code_number >= 0)
3529             {
3530               rtx new_i3_dest = SET_DEST (i3set);
3531               rtx new_i2_dest = SET_DEST (i2set);
3532
3533               while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3534                      || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3535                      || GET_CODE (new_i3_dest) == SUBREG)
3536                 new_i3_dest = XEXP (new_i3_dest, 0);
3537
3538               while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3539                      || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3540                      || GET_CODE (new_i2_dest) == SUBREG)
3541                 new_i2_dest = XEXP (new_i2_dest, 0);
3542
3543               if (REG_P (new_i3_dest)
3544                   && REG_P (new_i2_dest)
3545                   && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3546                 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3547             }
3548         }
3549
3550       /* If we can split it and use I2DEST, go ahead and see if that
3551          helps things be recognized.  Verify that none of the registers
3552          are set between I2 and I3.  */
3553       if (insn_code_number < 0
3554           && (split = find_split_point (&newpat, i3, false)) != 0
3555 #ifdef HAVE_cc0
3556           && REG_P (i2dest)
3557 #endif
3558           /* We need I2DEST in the proper mode.  If it is a hard register
3559              or the only use of a pseudo, we can change its mode.
3560              Make sure we don't change a hard register to have a mode that
3561              isn't valid for it, or change the number of registers.  */
3562           && (GET_MODE (*split) == GET_MODE (i2dest)
3563               || GET_MODE (*split) == VOIDmode
3564               || can_change_dest_mode (i2dest, added_sets_2,
3565                                        GET_MODE (*split)))
3566           && (next_nonnote_nondebug_insn (i2) == i3
3567               || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3568           /* We can't overwrite I2DEST if its value is still used by
3569              NEWPAT.  */
3570           && ! reg_referenced_p (i2dest, newpat))
3571         {
3572           rtx newdest = i2dest;
3573           enum rtx_code split_code = GET_CODE (*split);
3574           enum machine_mode split_mode = GET_MODE (*split);
3575           bool subst_done = false;
3576           newi2pat = NULL_RTX;
3577
3578           i2scratch = true;
3579
3580           /* *SPLIT may be part of I2SRC, so make sure we have the
3581              original expression around for later debug processing.
3582              We should not need I2SRC any more in other cases.  */
3583           if (MAY_HAVE_DEBUG_INSNS)
3584             i2src = copy_rtx (i2src);
3585           else
3586             i2src = NULL;
3587
3588           /* Get NEWDEST as a register in the proper mode.  We have already
3589              validated that we can do this.  */
3590           if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3591             {
3592               if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3593                 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3594               else
3595                 {
3596                   SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3597                   newdest = regno_reg_rtx[REGNO (i2dest)];
3598                 }
3599             }
3600
3601           /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3602              an ASHIFT.  This can occur if it was inside a PLUS and hence
3603              appeared to be a memory address.  This is a kludge.  */
3604           if (split_code == MULT
3605               && CONST_INT_P (XEXP (*split, 1))
3606               && INTVAL (XEXP (*split, 1)) > 0
3607               && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3608             {
3609               SUBST (*split, gen_rtx_ASHIFT (split_mode,
3610                                              XEXP (*split, 0), GEN_INT (i)));
3611               /* Update split_code because we may not have a multiply
3612                  anymore.  */
3613               split_code = GET_CODE (*split);
3614             }
3615
3616 #ifdef INSN_SCHEDULING
3617           /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3618              be written as a ZERO_EXTEND.  */
3619           if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3620             {
3621 #ifdef LOAD_EXTEND_OP
3622               /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3623                  what it really is.  */
3624               if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3625                   == SIGN_EXTEND)
3626                 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3627                                                     SUBREG_REG (*split)));
3628               else
3629 #endif
3630                 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3631                                                     SUBREG_REG (*split)));
3632             }
3633 #endif
3634
3635           /* Attempt to split binary operators using arithmetic identities.  */
3636           if (BINARY_P (SET_SRC (newpat))
3637               && split_mode == GET_MODE (SET_SRC (newpat))
3638               && ! side_effects_p (SET_SRC (newpat)))
3639             {
3640               rtx setsrc = SET_SRC (newpat);
3641               enum machine_mode mode = GET_MODE (setsrc);
3642               enum rtx_code code = GET_CODE (setsrc);
3643               rtx src_op0 = XEXP (setsrc, 0);
3644               rtx src_op1 = XEXP (setsrc, 1);
3645
3646               /* Split "X = Y op Y" as "Z = Y; X = Z op Z".  */
3647               if (rtx_equal_p (src_op0, src_op1))
3648                 {
3649                   newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3650                   SUBST (XEXP (setsrc, 0), newdest);
3651                   SUBST (XEXP (setsrc, 1), newdest);
3652                   subst_done = true;
3653                 }
3654               /* Split "((P op Q) op R) op S" where op is PLUS or MULT.  */
3655               else if ((code == PLUS || code == MULT)
3656                        && GET_CODE (src_op0) == code
3657                        && GET_CODE (XEXP (src_op0, 0)) == code
3658                        && (INTEGRAL_MODE_P (mode)
3659                            || (FLOAT_MODE_P (mode)
3660                                && flag_unsafe_math_optimizations)))
3661                 {
3662                   rtx p = XEXP (XEXP (src_op0, 0), 0);
3663                   rtx q = XEXP (XEXP (src_op0, 0), 1);
3664                   rtx r = XEXP (src_op0, 1);
3665                   rtx s = src_op1;
3666
3667                   /* Split both "((X op Y) op X) op Y" and
3668                      "((X op Y) op Y) op X" as "T op T" where T is
3669                      "X op Y".  */
3670                   if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3671                        || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3672                     {
3673                       newi2pat = gen_rtx_SET (VOIDmode, newdest,
3674                                               XEXP (src_op0, 0));
3675                       SUBST (XEXP (setsrc, 0), newdest);
3676                       SUBST (XEXP (setsrc, 1), newdest);
3677                       subst_done = true;
3678                     }
3679                   /* Split "((X op X) op Y) op Y)" as "T op T" where
3680                      T is "X op Y".  */
3681                   else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3682                     {
3683                       rtx tmp = simplify_gen_binary (code, mode, p, r);
3684                       newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3685                       SUBST (XEXP (setsrc, 0), newdest);
3686                       SUBST (XEXP (setsrc, 1), newdest);
3687                       subst_done = true;
3688                     }
3689                 }
3690             }
3691
3692           if (!subst_done)
3693             {
3694               newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3695               SUBST (*split, newdest);
3696             }
3697
3698           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3699
3700           /* recog_for_combine might have added CLOBBERs to newi2pat.
3701              Make sure NEWPAT does not depend on the clobbered regs.  */
3702           if (GET_CODE (newi2pat) == PARALLEL)
3703             for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3704               if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3705                 {
3706                   rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3707                   if (reg_overlap_mentioned_p (reg, newpat))
3708                     {
3709                       undo_all ();
3710                       return 0;
3711                     }
3712                 }
3713
3714           /* If the split point was a MULT and we didn't have one before,
3715              don't use one now.  */
3716           if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3717             insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3718         }
3719     }
3720
3721   /* Check for a case where we loaded from memory in a narrow mode and
3722      then sign extended it, but we need both registers.  In that case,
3723      we have a PARALLEL with both loads from the same memory location.
3724      We can split this into a load from memory followed by a register-register
3725      copy.  This saves at least one insn, more if register allocation can
3726      eliminate the copy.
3727
3728      We cannot do this if the destination of the first assignment is a
3729      condition code register or cc0.  We eliminate this case by making sure
3730      the SET_DEST and SET_SRC have the same mode.
3731
3732      We cannot do this if the destination of the second assignment is
3733      a register that we have already assumed is zero-extended.  Similarly
3734      for a SUBREG of such a register.  */
3735
3736   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3737            && GET_CODE (newpat) == PARALLEL
3738            && XVECLEN (newpat, 0) == 2
3739            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3740            && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3741            && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3742                == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3743            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3744            && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3745                            XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3746            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3747                                    DF_INSN_LUID (i2))
3748            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3749            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3750            && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
3751                  (REG_P (temp)
3752                   && VEC_index (reg_stat_type, reg_stat,
3753                                 REGNO (temp))->nonzero_bits != 0
3754                   && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3755                   && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3756                   && (VEC_index (reg_stat_type, reg_stat,
3757                                  REGNO (temp))->nonzero_bits
3758                       != GET_MODE_MASK (word_mode))))
3759            && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3760                  && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3761                      (REG_P (temp)
3762                       && VEC_index (reg_stat_type, reg_stat,
3763                                     REGNO (temp))->nonzero_bits != 0
3764                       && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3765                       && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3766                       && (VEC_index (reg_stat_type, reg_stat,
3767                                      REGNO (temp))->nonzero_bits
3768                           != GET_MODE_MASK (word_mode)))))
3769            && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3770                                          SET_SRC (XVECEXP (newpat, 0, 1)))
3771            && ! find_reg_note (i3, REG_UNUSED,
3772                                SET_DEST (XVECEXP (newpat, 0, 0))))
3773     {
3774       rtx ni2dest;
3775
3776       newi2pat = XVECEXP (newpat, 0, 0);
3777       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3778       newpat = XVECEXP (newpat, 0, 1);
3779       SUBST (SET_SRC (newpat),
3780              gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3781       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3782
3783       if (i2_code_number >= 0)
3784         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3785
3786       if (insn_code_number >= 0)
3787         swap_i2i3 = 1;
3788     }
3789
3790   /* Similarly, check for a case where we have a PARALLEL of two independent
3791      SETs but we started with three insns.  In this case, we can do the sets
3792      as two separate insns.  This case occurs when some SET allows two
3793      other insns to combine, but the destination of that SET is still live.  */
3794
3795   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3796            && GET_CODE (newpat) == PARALLEL
3797            && XVECLEN (newpat, 0) == 2
3798            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3799            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3800            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3801            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3802            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3803            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3804            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3805                                   XVECEXP (newpat, 0, 0))
3806            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3807                                   XVECEXP (newpat, 0, 1))
3808            && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3809                  && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3810     {
3811       /* Normally, it doesn't matter which of the two is done first,
3812          but the one that references cc0 can't be the second, and
3813          one which uses any regs/memory set in between i2 and i3 can't
3814          be first.  */
3815       if (!use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3816                               DF_INSN_LUID (i2))
3817 #ifdef HAVE_cc0
3818           && !reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0))
3819 #endif
3820          )
3821         {
3822           newi2pat = XVECEXP (newpat, 0, 1);
3823           newpat = XVECEXP (newpat, 0, 0);
3824         }
3825       else if (!use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 0)),
3826                                    DF_INSN_LUID (i2))
3827 #ifdef HAVE_cc0
3828                && !reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 1))
3829 #endif
3830               )
3831         {
3832           newi2pat = XVECEXP (newpat, 0, 0);
3833           newpat = XVECEXP (newpat, 0, 1);
3834         }
3835       else
3836         {
3837           undo_all ();
3838           return 0;
3839         }
3840
3841       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3842
3843       if (i2_code_number >= 0)
3844         {
3845           /* recog_for_combine might have added CLOBBERs to newi2pat.
3846              Make sure NEWPAT does not depend on the clobbered regs.  */
3847           if (GET_CODE (newi2pat) == PARALLEL)
3848             {
3849               for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3850                 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3851                   {
3852                     rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3853                     if (reg_overlap_mentioned_p (reg, newpat))
3854                       {
3855                         undo_all ();
3856                         return 0;
3857                       }
3858                   }
3859             }
3860
3861           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3862         }
3863     }
3864
3865   /* If it still isn't recognized, fail and change things back the way they
3866      were.  */
3867   if ((insn_code_number < 0
3868        /* Is the result a reasonable ASM_OPERANDS?  */
3869        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3870     {
3871       undo_all ();
3872       return 0;
3873     }
3874
3875   /* If we had to change another insn, make sure it is valid also.  */
3876   if (undobuf.other_insn)
3877     {
3878       CLEAR_HARD_REG_SET (newpat_used_regs);
3879
3880       other_pat = PATTERN (undobuf.other_insn);
3881       other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3882                                              &new_other_notes);
3883
3884       if (other_code_number < 0 && ! check_asm_operands (other_pat))
3885         {
3886           undo_all ();
3887           return 0;
3888         }
3889     }
3890
3891 #ifdef HAVE_cc0
3892   /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3893      they are adjacent to each other or not.  */
3894   {
3895     rtx p = prev_nonnote_insn (i3);
3896     if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3897         && sets_cc0_p (newi2pat))
3898       {
3899         undo_all ();
3900         return 0;
3901       }
3902   }
3903 #endif
3904
3905   /* Only allow this combination if insn_rtx_costs reports that the
3906      replacement instructions are cheaper than the originals.  */
3907   if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
3908     {
3909       undo_all ();
3910       return 0;
3911     }
3912
3913   if (MAY_HAVE_DEBUG_INSNS)
3914     {
3915       struct undo *undo;
3916
3917       for (undo = undobuf.undos; undo; undo = undo->next)
3918         if (undo->kind == UNDO_MODE)
3919           {
3920             rtx reg = *undo->where.r;
3921             enum machine_mode new_mode = GET_MODE (reg);
3922             enum machine_mode old_mode = undo->old_contents.m;
3923
3924             /* Temporarily revert mode back.  */
3925             adjust_reg_mode (reg, old_mode);
3926
3927             if (reg == i2dest && i2scratch)
3928               {
3929                 /* If we used i2dest as a scratch register with a
3930                    different mode, substitute it for the original
3931                    i2src while its original mode is temporarily
3932                    restored, and then clear i2scratch so that we don't
3933                    do it again later.  */
3934                 propagate_for_debug (i2, last_combined_insn, reg, i2src);
3935                 i2scratch = false;
3936                 /* Put back the new mode.  */
3937                 adjust_reg_mode (reg, new_mode);
3938               }
3939             else
3940               {
3941                 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
3942                 rtx first, last;
3943
3944                 if (reg == i2dest)
3945                   {
3946                     first = i2;
3947                     last = last_combined_insn;
3948                   }
3949                 else
3950                   {
3951                     first = i3;
3952                     last = undobuf.other_insn;
3953                     gcc_assert (last);
3954                     if (DF_INSN_LUID (last)
3955                         < DF_INSN_LUID (last_combined_insn))
3956                       last = last_combined_insn;
3957                   }
3958
3959                 /* We're dealing with a reg that changed mode but not
3960                    meaning, so we want to turn it into a subreg for
3961                    the new mode.  However, because of REG sharing and
3962                    because its mode had already changed, we have to do
3963                    it in two steps.  First, replace any debug uses of
3964                    reg, with its original mode temporarily restored,
3965                    with this copy we have created; then, replace the
3966                    copy with the SUBREG of the original shared reg,
3967                    once again changed to the new mode.  */
3968                 propagate_for_debug (first, last, reg, tempreg);
3969                 adjust_reg_mode (reg, new_mode);
3970                 propagate_for_debug (first, last, tempreg,
3971                                      lowpart_subreg (old_mode, reg, new_mode));
3972               }
3973           }
3974     }
3975
3976   /* If we will be able to accept this, we have made a
3977      change to the destination of I3.  This requires us to
3978      do a few adjustments.  */
3979
3980   if (changed_i3_dest)
3981     {
3982       PATTERN (i3) = newpat;
3983       adjust_for_new_dest (i3);
3984     }
3985
3986   /* We now know that we can do this combination.  Merge the insns and
3987      update the status of registers and LOG_LINKS.  */
3988
3989   if (undobuf.other_insn)
3990     {
3991       rtx note, next;
3992
3993       PATTERN (undobuf.other_insn) = other_pat;
3994
3995       /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
3996          are still valid.  Then add any non-duplicate notes added by
3997          recog_for_combine.  */
3998       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
3999         {
4000           next = XEXP (note, 1);
4001
4002           if (REG_NOTE_KIND (note) == REG_UNUSED
4003               && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
4004             remove_note (undobuf.other_insn, note);
4005         }
4006
4007       distribute_notes (new_other_notes, undobuf.other_insn,
4008                         undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX,
4009                         NULL_RTX);
4010     }
4011
4012   if (swap_i2i3)
4013     {
4014       rtx insn;
4015       struct insn_link *link;
4016       rtx ni2dest;
4017
4018       /* I3 now uses what used to be its destination and which is now
4019          I2's destination.  This requires us to do a few adjustments.  */
4020       PATTERN (i3) = newpat;
4021       adjust_for_new_dest (i3);
4022
4023       /* We need a LOG_LINK from I3 to I2.  But we used to have one,
4024          so we still will.
4025
4026          However, some later insn might be using I2's dest and have
4027          a LOG_LINK pointing at I3.  We must remove this link.
4028          The simplest way to remove the link is to point it at I1,
4029          which we know will be a NOTE.  */
4030
4031       /* newi2pat is usually a SET here; however, recog_for_combine might
4032          have added some clobbers.  */
4033       if (GET_CODE (newi2pat) == PARALLEL)
4034         ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
4035       else
4036         ni2dest = SET_DEST (newi2pat);
4037
4038       for (insn = NEXT_INSN (i3);
4039            insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
4040                     || insn != BB_HEAD (this_basic_block->next_bb));
4041            insn = NEXT_INSN (insn))
4042         {
4043           if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
4044             {
4045               FOR_EACH_LOG_LINK (link, insn)
4046                 if (link->insn == i3)
4047                   link->insn = i1;
4048
4049               break;
4050             }
4051         }
4052     }
4053
4054   {
4055     rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4056     struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4057     rtx midnotes = 0;
4058     int from_luid;
4059     /* Compute which registers we expect to eliminate.  newi2pat may be setting
4060        either i3dest or i2dest, so we must check it.  Also, i1dest may be the
4061        same as i3dest, in which case newi2pat may be setting i1dest.  */
4062     rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4063                    || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4064                    || !i2dest_killed
4065                    ? 0 : i2dest);
4066     rtx elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4067                    || (newi2pat && reg_set_p (i1dest, newi2pat))
4068                    || !i1dest_killed
4069                    ? 0 : i1dest);
4070     rtx elim_i0 = (i0 == 0 || i0dest_in_i0src
4071                    || (newi2pat && reg_set_p (i0dest, newi2pat))
4072                    || !i0dest_killed
4073                    ? 0 : i0dest);
4074
4075     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4076        clear them.  */
4077     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4078     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4079     if (i1)
4080       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4081     if (i0)
4082       i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4083
4084     /* Ensure that we do not have something that should not be shared but
4085        occurs multiple times in the new insns.  Check this by first
4086        resetting all the `used' flags and then copying anything is shared.  */
4087
4088     reset_used_flags (i3notes);
4089     reset_used_flags (i2notes);
4090     reset_used_flags (i1notes);
4091     reset_used_flags (i0notes);
4092     reset_used_flags (newpat);
4093     reset_used_flags (newi2pat);
4094     if (undobuf.other_insn)
4095       reset_used_flags (PATTERN (undobuf.other_insn));
4096
4097     i3notes = copy_rtx_if_shared (i3notes);
4098     i2notes = copy_rtx_if_shared (i2notes);
4099     i1notes = copy_rtx_if_shared (i1notes);
4100     i0notes = copy_rtx_if_shared (i0notes);
4101     newpat = copy_rtx_if_shared (newpat);
4102     newi2pat = copy_rtx_if_shared (newi2pat);
4103     if (undobuf.other_insn)
4104       reset_used_flags (PATTERN (undobuf.other_insn));
4105
4106     INSN_CODE (i3) = insn_code_number;
4107     PATTERN (i3) = newpat;
4108
4109     if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4110       {
4111         rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
4112
4113         reset_used_flags (call_usage);
4114         call_usage = copy_rtx (call_usage);
4115
4116         if (substed_i2)
4117           {
4118             /* I2SRC must still be meaningful at this point.  Some splitting
4119                operations can invalidate I2SRC, but those operations do not
4120                apply to calls.  */
4121             gcc_assert (i2src);
4122             replace_rtx (call_usage, i2dest, i2src);
4123           }
4124
4125         if (substed_i1)
4126           replace_rtx (call_usage, i1dest, i1src);
4127         if (substed_i0)
4128           replace_rtx (call_usage, i0dest, i0src);
4129
4130         CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4131       }
4132
4133     if (undobuf.other_insn)
4134       INSN_CODE (undobuf.other_insn) = other_code_number;
4135
4136     /* We had one special case above where I2 had more than one set and
4137        we replaced a destination of one of those sets with the destination
4138        of I3.  In that case, we have to update LOG_LINKS of insns later
4139        in this basic block.  Note that this (expensive) case is rare.
4140
4141        Also, in this case, we must pretend that all REG_NOTEs for I2
4142        actually came from I3, so that REG_UNUSED notes from I2 will be
4143        properly handled.  */
4144
4145     if (i3_subst_into_i2)
4146       {
4147         for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4148           if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4149                || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4150               && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4151               && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4152               && ! find_reg_note (i2, REG_UNUSED,
4153                                   SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4154             for (temp = NEXT_INSN (i2);
4155                  temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
4156                           || BB_HEAD (this_basic_block) != temp);
4157                  temp = NEXT_INSN (temp))
4158               if (temp != i3 && INSN_P (temp))
4159                 FOR_EACH_LOG_LINK (link, temp)
4160                   if (link->insn == i2)
4161                     link->insn = i3;
4162
4163         if (i3notes)
4164           {
4165             rtx link = i3notes;
4166             while (XEXP (link, 1))
4167               link = XEXP (link, 1);
4168             XEXP (link, 1) = i2notes;
4169           }
4170         else
4171           i3notes = i2notes;
4172         i2notes = 0;
4173       }
4174
4175     LOG_LINKS (i3) = NULL;
4176     REG_NOTES (i3) = 0;
4177     LOG_LINKS (i2) = NULL;
4178     REG_NOTES (i2) = 0;
4179
4180     if (newi2pat)
4181       {
4182         if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4183           propagate_for_debug (i2, last_combined_insn, i2dest, i2src);
4184         INSN_CODE (i2) = i2_code_number;
4185         PATTERN (i2) = newi2pat;
4186       }
4187     else
4188       {
4189         if (MAY_HAVE_DEBUG_INSNS && i2src)
4190           propagate_for_debug (i2, last_combined_insn, i2dest, i2src);
4191         SET_INSN_DELETED (i2);
4192       }
4193
4194     if (i1)
4195       {
4196         LOG_LINKS (i1) = NULL;
4197         REG_NOTES (i1) = 0;
4198         if (MAY_HAVE_DEBUG_INSNS)
4199           propagate_for_debug (i1, last_combined_insn, i1dest, i1src);
4200         SET_INSN_DELETED (i1);
4201       }
4202
4203     if (i0)
4204       {
4205         LOG_LINKS (i0) = NULL;
4206         REG_NOTES (i0) = 0;
4207         if (MAY_HAVE_DEBUG_INSNS)
4208           propagate_for_debug (i0, last_combined_insn, i0dest, i0src);
4209         SET_INSN_DELETED (i0);
4210       }
4211
4212     /* Get death notes for everything that is now used in either I3 or
4213        I2 and used to die in a previous insn.  If we built two new
4214        patterns, move from I1 to I2 then I2 to I3 so that we get the
4215        proper movement on registers that I2 modifies.  */
4216
4217     if (i0)
4218       from_luid = DF_INSN_LUID (i0);
4219     else if (i1)
4220       from_luid = DF_INSN_LUID (i1);
4221     else
4222       from_luid = DF_INSN_LUID (i2);
4223     if (newi2pat)
4224       move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4225     move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4226
4227     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
4228     if (i3notes)
4229       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
4230                         elim_i2, elim_i1, elim_i0);
4231     if (i2notes)
4232       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
4233                         elim_i2, elim_i1, elim_i0);
4234     if (i1notes)
4235       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
4236                         elim_i2, elim_i1, elim_i0);
4237     if (i0notes)
4238       distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL_RTX,
4239                         elim_i2, elim_i1, elim_i0);
4240     if (midnotes)
4241       distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4242                         elim_i2, elim_i1, elim_i0);
4243
4244     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
4245        know these are REG_UNUSED and want them to go to the desired insn,
4246        so we always pass it as i3.  */
4247
4248     if (newi2pat && new_i2_notes)
4249       distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX,
4250                         NULL_RTX);
4251
4252     if (new_i3_notes)
4253       distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX,
4254                         NULL_RTX);
4255
4256     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
4257        put a REG_DEAD note for it somewhere.  If NEWI2PAT exists and sets
4258        I3DEST, the death must be somewhere before I2, not I3.  If we passed I3
4259        in that case, it might delete I2.  Similarly for I2 and I1.
4260        Show an additional death due to the REG_DEAD note we make here.  If
4261        we discard it in distribute_notes, we will decrement it again.  */
4262
4263     if (i3dest_killed)
4264       {
4265         if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4266           distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
4267                                             NULL_RTX),
4268                             NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1, elim_i0);
4269         else
4270           distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
4271                                             NULL_RTX),
4272                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4273                             elim_i2, elim_i1, elim_i0);
4274       }
4275
4276     if (i2dest_in_i2src)
4277       {
4278         rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4279         if (newi2pat && reg_set_p (i2dest, newi2pat))
4280           distribute_notes (new_note,  NULL_RTX, i2, NULL_RTX, NULL_RTX,
4281                             NULL_RTX, NULL_RTX);
4282         else
4283           distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4284                             NULL_RTX, NULL_RTX, NULL_RTX);
4285       }
4286
4287     if (i1dest_in_i1src)
4288       {
4289         rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4290         if (newi2pat && reg_set_p (i1dest, newi2pat))
4291           distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4292                             NULL_RTX, NULL_RTX);
4293         else
4294           distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4295                             NULL_RTX, NULL_RTX, NULL_RTX);
4296       }
4297
4298     if (i0dest_in_i0src)
4299       {
4300         rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4301         if (newi2pat && reg_set_p (i0dest, newi2pat))
4302           distribute_notes (new_note, NULL_RTX, i2, NULL_RTX, NULL_RTX,
4303                             NULL_RTX, NULL_RTX);
4304         else
4305           distribute_notes (new_note, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
4306                             NULL_RTX, NULL_RTX, NULL_RTX);
4307       }
4308
4309     distribute_links (i3links);
4310     distribute_links (i2links);
4311     distribute_links (i1links);
4312     distribute_links (i0links);
4313
4314     if (REG_P (i2dest))
4315       {
4316         struct insn_link *link;
4317         rtx i2_insn = 0, i2_val = 0, set;
4318
4319         /* The insn that used to set this register doesn't exist, and
4320            this life of the register may not exist either.  See if one of
4321            I3's links points to an insn that sets I2DEST.  If it does,
4322            that is now the last known value for I2DEST. If we don't update
4323            this and I2 set the register to a value that depended on its old
4324            contents, we will get confused.  If this insn is used, thing
4325            will be set correctly in combine_instructions.  */
4326         FOR_EACH_LOG_LINK (link, i3)
4327           if ((set = single_set (link->insn)) != 0
4328               && rtx_equal_p (i2dest, SET_DEST (set)))
4329             i2_insn = link->insn, i2_val = SET_SRC (set);
4330
4331         record_value_for_reg (i2dest, i2_insn, i2_val);
4332
4333         /* If the reg formerly set in I2 died only once and that was in I3,
4334            zero its use count so it won't make `reload' do any work.  */
4335         if (! added_sets_2
4336             && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4337             && ! i2dest_in_i2src)
4338           INC_REG_N_SETS (REGNO (i2dest), -1);
4339       }
4340
4341     if (i1 && REG_P (i1dest))
4342       {
4343         struct insn_link *link;
4344         rtx i1_insn = 0, i1_val = 0, set;
4345
4346         FOR_EACH_LOG_LINK (link, i3)
4347           if ((set = single_set (link->insn)) != 0
4348               && rtx_equal_p (i1dest, SET_DEST (set)))
4349             i1_insn = link->insn, i1_val = SET_SRC (set);
4350
4351         record_value_for_reg (i1dest, i1_insn, i1_val);
4352
4353         if (! added_sets_1 && ! i1dest_in_i1src)
4354           INC_REG_N_SETS (REGNO (i1dest), -1);
4355       }
4356
4357     if (i0 && REG_P (i0dest))
4358       {
4359         struct insn_link *link;
4360         rtx i0_insn = 0, i0_val = 0, set;
4361
4362         FOR_EACH_LOG_LINK (link, i3)
4363           if ((set = single_set (link->insn)) != 0
4364               && rtx_equal_p (i0dest, SET_DEST (set)))
4365             i0_insn = link->insn, i0_val = SET_SRC (set);
4366
4367         record_value_for_reg (i0dest, i0_insn, i0_val);
4368
4369         if (! added_sets_0 && ! i0dest_in_i0src)
4370           INC_REG_N_SETS (REGNO (i0dest), -1);
4371       }
4372
4373     /* Update reg_stat[].nonzero_bits et al for any changes that may have
4374        been made to this insn.  The order of
4375        set_nonzero_bits_and_sign_copies() is important.  Because newi2pat
4376        can affect nonzero_bits of newpat */
4377     if (newi2pat)
4378       note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4379     note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4380   }
4381
4382   if (undobuf.other_insn != NULL_RTX)
4383     {
4384       if (dump_file)
4385         {
4386           fprintf (dump_file, "modifying other_insn ");
4387           dump_insn_slim (dump_file, undobuf.other_insn);
4388         }
4389       df_insn_rescan (undobuf.other_insn);
4390     }
4391
4392   if (i0 && !(NOTE_P(i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4393     {
4394       if (dump_file)
4395         {
4396           fprintf (dump_file, "modifying insn i1 ");
4397           dump_insn_slim (dump_file, i0);
4398         }
4399       df_insn_rescan (i0);
4400     }
4401
4402   if (i1 && !(NOTE_P(i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4403     {
4404       if (dump_file)
4405         {
4406           fprintf (dump_file, "modifying insn i1 ");
4407           dump_insn_slim (dump_file, i1);
4408         }
4409       df_insn_rescan (i1);
4410     }
4411
4412   if (i2 && !(NOTE_P(i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4413     {
4414       if (dump_file)
4415         {
4416           fprintf (dump_file, "modifying insn i2 ");
4417           dump_insn_slim (dump_file, i2);
4418         }
4419       df_insn_rescan (i2);
4420     }
4421
4422   if (i3 && !(NOTE_P(i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4423     {
4424       if (dump_file)
4425         {
4426           fprintf (dump_file, "modifying insn i3 ");
4427           dump_insn_slim (dump_file, i3);
4428         }
4429       df_insn_rescan (i3);
4430     }
4431
4432   /* Set new_direct_jump_p if a new return or simple jump instruction
4433      has been created.  Adjust the CFG accordingly.  */
4434
4435   if (returnjump_p (i3) || any_uncondjump_p (i3))
4436     {
4437       *new_direct_jump_p = 1;
4438       mark_jump_label (PATTERN (i3), i3, 0);
4439       update_cfg_for_uncondjump (i3);
4440     }
4441
4442   if (undobuf.other_insn != NULL_RTX
4443       && (returnjump_p (undobuf.other_insn)
4444           || any_uncondjump_p (undobuf.other_insn)))
4445     {
4446       *new_direct_jump_p = 1;
4447       update_cfg_for_uncondjump (undobuf.other_insn);
4448     }
4449
4450   /* A noop might also need cleaning up of CFG, if it comes from the
4451      simplification of a jump.  */
4452   if (JUMP_P (i3)
4453       && GET_CODE (newpat) == SET
4454       && SET_SRC (newpat) == pc_rtx
4455       && SET_DEST (newpat) == pc_rtx)
4456     {
4457       *new_direct_jump_p = 1;
4458       update_cfg_for_uncondjump (i3);
4459     }
4460
4461   if (undobuf.other_insn != NULL_RTX
4462       && JUMP_P (undobuf.other_insn)
4463       && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4464       && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4465       && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4466     {
4467       *new_direct_jump_p = 1;
4468       update_cfg_for_uncondjump (undobuf.other_insn);
4469     }
4470
4471   combine_successes++;
4472   undo_commit ();
4473
4474   if (added_links_insn
4475       && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4476       && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4477     return added_links_insn;
4478   else
4479     return newi2pat ? i2 : i3;
4480 }
4481 \f
4482 /* Undo all the modifications recorded in undobuf.  */
4483
4484 static void
4485 undo_all (void)
4486 {
4487   struct undo *undo, *next;
4488
4489   for (undo = undobuf.undos; undo; undo = next)
4490     {
4491       next = undo->next;
4492       switch (undo->kind)
4493         {
4494         case UNDO_RTX:
4495           *undo->where.r = undo->old_contents.r;
4496           break;
4497         case UNDO_INT:
4498           *undo->where.i = undo->old_contents.i;
4499           break;
4500         case UNDO_MODE:
4501           adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4502           break;
4503         default:
4504           gcc_unreachable ();
4505         }
4506
4507       undo->next = undobuf.frees;
4508       undobuf.frees = undo;
4509     }
4510
4511   undobuf.undos = 0;
4512 }
4513
4514 /* We've committed to accepting the changes we made.  Move all
4515    of the undos to the free list.  */
4516
4517 static void
4518 undo_commit (void)
4519 {
4520   struct undo *undo, *next;
4521
4522   for (undo = undobuf.undos; undo; undo = next)
4523     {
4524       next = undo->next;
4525       undo->next = undobuf.frees;
4526       undobuf.frees = undo;
4527     }
4528   undobuf.undos = 0;
4529 }
4530 \f
4531 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4532    where we have an arithmetic expression and return that point.  LOC will
4533    be inside INSN.
4534
4535    try_combine will call this function to see if an insn can be split into
4536    two insns.  */
4537
4538 static rtx *
4539 find_split_point (rtx *loc, rtx insn, bool set_src)
4540 {
4541   rtx x = *loc;
4542   enum rtx_code code = GET_CODE (x);
4543   rtx *split;
4544   unsigned HOST_WIDE_INT len = 0;
4545   HOST_WIDE_INT pos = 0;
4546   int unsignedp = 0;
4547   rtx inner = NULL_RTX;
4548
4549   /* First special-case some codes.  */
4550   switch (code)
4551     {
4552     case SUBREG:
4553 #ifdef INSN_SCHEDULING
4554       /* If we are making a paradoxical SUBREG invalid, it becomes a split
4555          point.  */
4556       if (MEM_P (SUBREG_REG (x)))
4557         return loc;
4558 #endif
4559       return find_split_point (&SUBREG_REG (x), insn, false);
4560
4561     case MEM:
4562 #ifdef HAVE_lo_sum
4563       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4564          using LO_SUM and HIGH.  */
4565       if (GET_CODE (XEXP (x, 0)) == CONST
4566           || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4567         {
4568           enum machine_mode address_mode
4569             = targetm.addr_space.address_mode (MEM_ADDR_SPACE (x));
4570
4571           SUBST (XEXP (x, 0),
4572                  gen_rtx_LO_SUM (address_mode,
4573                                  gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4574                                  XEXP (x, 0)));
4575           return &XEXP (XEXP (x, 0), 0);
4576         }
4577 #endif
4578
4579       /* If we have a PLUS whose second operand is a constant and the
4580          address is not valid, perhaps will can split it up using
4581          the machine-specific way to split large constants.  We use
4582          the first pseudo-reg (one of the virtual regs) as a placeholder;
4583          it will not remain in the result.  */
4584       if (GET_CODE (XEXP (x, 0)) == PLUS
4585           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4586           && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4587                                             MEM_ADDR_SPACE (x)))
4588         {
4589           rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4590           rtx seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4591                                                       XEXP (x, 0)),
4592                                          subst_insn);
4593
4594           /* This should have produced two insns, each of which sets our
4595              placeholder.  If the source of the second is a valid address,
4596              we can make put both sources together and make a split point
4597              in the middle.  */
4598
4599           if (seq
4600               && NEXT_INSN (seq) != NULL_RTX
4601               && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4602               && NONJUMP_INSN_P (seq)
4603               && GET_CODE (PATTERN (seq)) == SET
4604               && SET_DEST (PATTERN (seq)) == reg
4605               && ! reg_mentioned_p (reg,
4606                                     SET_SRC (PATTERN (seq)))
4607               && NONJUMP_INSN_P (NEXT_INSN (seq))
4608               && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4609               && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4610               && memory_address_addr_space_p
4611                    (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4612                     MEM_ADDR_SPACE (x)))
4613             {
4614               rtx src1 = SET_SRC (PATTERN (seq));
4615               rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4616
4617               /* Replace the placeholder in SRC2 with SRC1.  If we can
4618                  find where in SRC2 it was placed, that can become our
4619                  split point and we can replace this address with SRC2.
4620                  Just try two obvious places.  */
4621
4622               src2 = replace_rtx (src2, reg, src1);
4623               split = 0;
4624               if (XEXP (src2, 0) == src1)
4625                 split = &XEXP (src2, 0);
4626               else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4627                        && XEXP (XEXP (src2, 0), 0) == src1)
4628                 split = &XEXP (XEXP (src2, 0), 0);
4629
4630               if (split)
4631                 {
4632                   SUBST (XEXP (x, 0), src2);
4633                   return split;
4634                 }
4635             }
4636
4637           /* If that didn't work, perhaps the first operand is complex and
4638              needs to be computed separately, so make a split point there.
4639              This will occur on machines that just support REG + CONST
4640              and have a constant moved through some previous computation.  */
4641
4642           else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4643                    && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4644                          && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4645             return &XEXP (XEXP (x, 0), 0);
4646         }
4647
4648       /* If we have a PLUS whose first operand is complex, try computing it
4649          separately by making a split there.  */
4650       if (GET_CODE (XEXP (x, 0)) == PLUS
4651           && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4652                                             MEM_ADDR_SPACE (x))
4653           && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4654           && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4655                 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4656         return &XEXP (XEXP (x, 0), 0);
4657       break;
4658
4659     case SET:
4660 #ifdef HAVE_cc0
4661       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4662          ZERO_EXTRACT, the most likely reason why this doesn't match is that
4663          we need to put the operand into a register.  So split at that
4664          point.  */
4665
4666       if (SET_DEST (x) == cc0_rtx
4667           && GET_CODE (SET_SRC (x)) != COMPARE
4668           && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4669           && !OBJECT_P (SET_SRC (x))
4670           && ! (GET_CODE (SET_SRC (x)) == SUBREG
4671                 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4672         return &SET_SRC (x);
4673 #endif
4674
4675       /* See if we can split SET_SRC as it stands.  */
4676       split = find_split_point (&SET_SRC (x), insn, true);
4677       if (split && split != &SET_SRC (x))
4678         return split;
4679
4680       /* See if we can split SET_DEST as it stands.  */
4681       split = find_split_point (&SET_DEST (x), insn, false);
4682       if (split && split != &SET_DEST (x))
4683         return split;
4684
4685       /* See if this is a bitfield assignment with everything constant.  If
4686          so, this is an IOR of an AND, so split it into that.  */
4687       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4688           && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
4689               <= HOST_BITS_PER_WIDE_INT)
4690           && CONST_INT_P (XEXP (SET_DEST (x), 1))
4691           && CONST_INT_P (XEXP (SET_DEST (x), 2))
4692           && CONST_INT_P (SET_SRC (x))
4693           && ((INTVAL (XEXP (SET_DEST (x), 1))
4694                + INTVAL (XEXP (SET_DEST (x), 2)))
4695               <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
4696           && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4697         {
4698           HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4699           unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4700           unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4701           rtx dest = XEXP (SET_DEST (x), 0);
4702           enum machine_mode mode = GET_MODE (dest);
4703           unsigned HOST_WIDE_INT mask
4704             = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4705           rtx or_mask;
4706
4707           if (BITS_BIG_ENDIAN)
4708             pos = GET_MODE_BITSIZE (mode) - len - pos;
4709
4710           or_mask = gen_int_mode (src << pos, mode);
4711           if (src == mask)
4712             SUBST (SET_SRC (x),
4713                    simplify_gen_binary (IOR, mode, dest, or_mask));
4714           else
4715             {
4716               rtx negmask = gen_int_mode (~(mask << pos), mode);
4717               SUBST (SET_SRC (x),
4718                      simplify_gen_binary (IOR, mode,
4719                                           simplify_gen_binary (AND, mode,
4720                                                                dest, negmask),
4721                                           or_mask));
4722             }
4723
4724           SUBST (SET_DEST (x), dest);
4725
4726           split = find_split_point (&SET_SRC (x), insn, true);
4727           if (split && split != &SET_SRC (x))
4728             return split;
4729         }
4730
4731       /* Otherwise, see if this is an operation that we can split into two.
4732          If so, try to split that.  */
4733       code = GET_CODE (SET_SRC (x));
4734
4735       switch (code)
4736         {
4737         case AND:
4738           /* If we are AND'ing with a large constant that is only a single
4739              bit and the result is only being used in a context where we
4740              need to know if it is zero or nonzero, replace it with a bit
4741              extraction.  This will avoid the large constant, which might
4742              have taken more than one insn to make.  If the constant were
4743              not a valid argument to the AND but took only one insn to make,
4744              this is no worse, but if it took more than one insn, it will
4745              be better.  */
4746
4747           if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4748               && REG_P (XEXP (SET_SRC (x), 0))
4749               && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4750               && REG_P (SET_DEST (x))
4751               && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
4752               && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4753               && XEXP (*split, 0) == SET_DEST (x)
4754               && XEXP (*split, 1) == const0_rtx)
4755             {
4756               rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4757                                                 XEXP (SET_SRC (x), 0),
4758                                                 pos, NULL_RTX, 1, 1, 0, 0);
4759               if (extraction != 0)
4760                 {
4761                   SUBST (SET_SRC (x), extraction);
4762                   return find_split_point (loc, insn, false);
4763                 }
4764             }
4765           break;
4766
4767         case NE:
4768           /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4769              is known to be on, this can be converted into a NEG of a shift.  */
4770           if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4771               && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4772               && 1 <= (pos = exact_log2
4773                        (nonzero_bits (XEXP (SET_SRC (x), 0),
4774                                       GET_MODE (XEXP (SET_SRC (x), 0))))))
4775             {
4776               enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4777
4778               SUBST (SET_SRC (x),
4779                      gen_rtx_NEG (mode,
4780                                   gen_rtx_LSHIFTRT (mode,
4781                                                     XEXP (SET_SRC (x), 0),
4782                                                     GEN_INT (pos))));
4783
4784               split = find_split_point (&SET_SRC (x), insn, true);
4785               if (split && split != &SET_SRC (x))
4786                 return split;
4787             }
4788           break;
4789
4790         case SIGN_EXTEND:
4791           inner = XEXP (SET_SRC (x), 0);
4792
4793           /* We can't optimize if either mode is a partial integer
4794              mode as we don't know how many bits are significant
4795              in those modes.  */
4796           if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4797               || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4798             break;
4799
4800           pos = 0;
4801           len = GET_MODE_BITSIZE (GET_MODE (inner));
4802           unsignedp = 0;
4803           break;
4804
4805         case SIGN_EXTRACT:
4806         case ZERO_EXTRACT:
4807           if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4808               && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4809             {
4810               inner = XEXP (SET_SRC (x), 0);
4811               len = INTVAL (XEXP (SET_SRC (x), 1));
4812               pos = INTVAL (XEXP (SET_SRC (x), 2));
4813
4814               if (BITS_BIG_ENDIAN)
4815                 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
4816               unsignedp = (code == ZERO_EXTRACT);
4817             }
4818           break;
4819
4820         default:
4821           break;
4822         }
4823
4824       if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
4825         {
4826           enum machine_mode mode = GET_MODE (SET_SRC (x));
4827
4828           /* For unsigned, we have a choice of a shift followed by an
4829              AND or two shifts.  Use two shifts for field sizes where the
4830              constant might be too large.  We assume here that we can
4831              always at least get 8-bit constants in an AND insn, which is
4832              true for every current RISC.  */
4833
4834           if (unsignedp && len <= 8)
4835             {
4836               SUBST (SET_SRC (x),
4837                      gen_rtx_AND (mode,
4838                                   gen_rtx_LSHIFTRT
4839                                   (mode, gen_lowpart (mode, inner),
4840                                    GEN_INT (pos)),
4841                                   GEN_INT (((unsigned HOST_WIDE_INT) 1 << len)
4842                                            - 1)));
4843
4844               split = find_split_point (&SET_SRC (x), insn, true);
4845               if (split && split != &SET_SRC (x))
4846                 return split;
4847             }
4848           else
4849             {
4850               SUBST (SET_SRC (x),
4851                      gen_rtx_fmt_ee
4852                      (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4853                       gen_rtx_ASHIFT (mode,
4854                                       gen_lowpart (mode, inner),
4855                                       GEN_INT (GET_MODE_BITSIZE (mode)
4856                                                - len - pos)),
4857                       GEN_INT (GET_MODE_BITSIZE (mode) - len)));
4858
4859               split = find_split_point (&SET_SRC (x), insn, true);
4860               if (split && split != &SET_SRC (x))
4861                 return split;
4862             }
4863         }
4864
4865       /* See if this is a simple operation with a constant as the second
4866          operand.  It might be that this constant is out of range and hence
4867          could be used as a split point.  */
4868       if (BINARY_P (SET_SRC (x))
4869           && CONSTANT_P (XEXP (SET_SRC (x), 1))
4870           && (OBJECT_P (XEXP (SET_SRC (x), 0))
4871               || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4872                   && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4873         return &XEXP (SET_SRC (x), 1);
4874
4875       /* Finally, see if this is a simple operation with its first operand
4876          not in a register.  The operation might require this operand in a
4877          register, so return it as a split point.  We can always do this
4878          because if the first operand were another operation, we would have
4879          already found it as a split point.  */
4880       if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4881           && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4882         return &XEXP (SET_SRC (x), 0);
4883
4884       return 0;
4885
4886     case AND:
4887     case IOR:
4888       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4889          it is better to write this as (not (ior A B)) so we can split it.
4890          Similarly for IOR.  */
4891       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4892         {
4893           SUBST (*loc,
4894                  gen_rtx_NOT (GET_MODE (x),
4895                               gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4896                                               GET_MODE (x),
4897                                               XEXP (XEXP (x, 0), 0),
4898                                               XEXP (XEXP (x, 1), 0))));
4899           return find_split_point (loc, insn, set_src);
4900         }
4901
4902       /* Many RISC machines have a large set of logical insns.  If the
4903          second operand is a NOT, put it first so we will try to split the
4904          other operand first.  */
4905       if (GET_CODE (XEXP (x, 1)) == NOT)
4906         {
4907           rtx tem = XEXP (x, 0);
4908           SUBST (XEXP (x, 0), XEXP (x, 1));
4909           SUBST (XEXP (x, 1), tem);
4910         }
4911       break;
4912
4913     case PLUS:
4914     case MINUS:
4915       /* Canonicalization can produce (minus A (mult B C)), where C is a
4916          constant.  It may be better to try splitting (plus (mult B -C) A)
4917          instead if this isn't a multiply by a power of two.  */
4918       if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
4919           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4920           && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
4921         {
4922           enum machine_mode mode = GET_MODE (x);
4923           unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
4924           HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
4925           SUBST (*loc, gen_rtx_PLUS (mode, gen_rtx_MULT (mode,
4926                                                          XEXP (XEXP (x, 1), 0),
4927                                                          GEN_INT (other_int)),
4928                                      XEXP (x, 0)));
4929           return find_split_point (loc, insn, set_src);
4930         }
4931
4932       /* Split at a multiply-accumulate instruction.  However if this is
4933          the SET_SRC, we likely do not have such an instruction and it's
4934          worthless to try this split.  */
4935       if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
4936         return loc;
4937
4938     default:
4939       break;
4940     }
4941
4942   /* Otherwise, select our actions depending on our rtx class.  */
4943   switch (GET_RTX_CLASS (code))
4944     {
4945     case RTX_BITFIELD_OPS:              /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
4946     case RTX_TERNARY:
4947       split = find_split_point (&XEXP (x, 2), insn, false);
4948       if (split)
4949         return split;
4950       /* ... fall through ...  */
4951     case RTX_BIN_ARITH:
4952     case RTX_COMM_ARITH:
4953     case RTX_COMPARE:
4954     case RTX_COMM_COMPARE:
4955       split = find_split_point (&XEXP (x, 1), insn, false);
4956       if (split)
4957         return split;
4958       /* ... fall through ...  */
4959     case RTX_UNARY:
4960       /* Some machines have (and (shift ...) ...) insns.  If X is not
4961          an AND, but XEXP (X, 0) is, use it as our split point.  */
4962       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4963         return &XEXP (x, 0);
4964
4965       split = find_split_point (&XEXP (x, 0), insn, false);
4966       if (split)
4967         return split;
4968       return loc;
4969
4970     default:
4971       /* Otherwise, we don't have a split point.  */
4972       return 0;
4973     }
4974 }
4975 \f
4976 /* Throughout X, replace FROM with TO, and return the result.
4977    The result is TO if X is FROM;
4978    otherwise the result is X, but its contents may have been modified.
4979    If they were modified, a record was made in undobuf so that
4980    undo_all will (among other things) return X to its original state.
4981
4982    If the number of changes necessary is too much to record to undo,
4983    the excess changes are not made, so the result is invalid.
4984    The changes already made can still be undone.
4985    undobuf.num_undo is incremented for such changes, so by testing that
4986    the caller can tell whether the result is valid.
4987
4988    `n_occurrences' is incremented each time FROM is replaced.
4989
4990    IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4991
4992    IN_COND is nonzero if we are at the top level of a condition.
4993
4994    UNIQUE_COPY is nonzero if each substitution must be unique.  We do this
4995    by copying if `n_occurrences' is nonzero.  */
4996
4997 static rtx
4998 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
4999 {
5000   enum rtx_code code = GET_CODE (x);
5001   enum machine_mode op0_mode = VOIDmode;
5002   const char *fmt;
5003   int len, i;
5004   rtx new_rtx;
5005
5006 /* Two expressions are equal if they are identical copies of a shared
5007    RTX or if they are both registers with the same register number
5008    and mode.  */
5009
5010 #define COMBINE_RTX_EQUAL_P(X,Y)                        \
5011   ((X) == (Y)                                           \
5012    || (REG_P (X) && REG_P (Y)   \
5013        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5014
5015   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5016     {
5017       n_occurrences++;
5018       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5019     }
5020
5021   /* If X and FROM are the same register but different modes, they
5022      will not have been seen as equal above.  However, the log links code
5023      will make a LOG_LINKS entry for that case.  If we do nothing, we
5024      will try to rerecognize our original insn and, when it succeeds,
5025      we will delete the feeding insn, which is incorrect.
5026
5027      So force this insn not to match in this (rare) case.  */
5028   if (! in_dest && code == REG && REG_P (from)
5029       && reg_overlap_mentioned_p (x, from))
5030     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5031
5032   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5033      of which may contain things that can be combined.  */
5034   if (code != MEM && code != LO_SUM && OBJECT_P (x))
5035     return x;
5036
5037   /* It is possible to have a subexpression appear twice in the insn.
5038      Suppose that FROM is a register that appears within TO.
5039      Then, after that subexpression has been scanned once by `subst',
5040      the second time it is scanned, TO may be found.  If we were
5041      to scan TO here, we would find FROM within it and create a
5042      self-referent rtl structure which is completely wrong.  */
5043   if (COMBINE_RTX_EQUAL_P (x, to))
5044     return to;
5045
5046   /* Parallel asm_operands need special attention because all of the
5047      inputs are shared across the arms.  Furthermore, unsharing the
5048      rtl results in recognition failures.  Failure to handle this case
5049      specially can result in circular rtl.
5050
5051      Solve this by doing a normal pass across the first entry of the
5052      parallel, and only processing the SET_DESTs of the subsequent
5053      entries.  Ug.  */
5054
5055   if (code == PARALLEL
5056       && GET_CODE (XVECEXP (x, 0, 0)) == SET
5057       && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5058     {
5059       new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5060
5061       /* If this substitution failed, this whole thing fails.  */
5062       if (GET_CODE (new_rtx) == CLOBBER
5063           && XEXP (new_rtx, 0) == const0_rtx)
5064         return new_rtx;
5065
5066       SUBST (XVECEXP (x, 0, 0), new_rtx);
5067
5068       for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5069         {
5070           rtx dest = SET_DEST (XVECEXP (x, 0, i));
5071
5072           if (!REG_P (dest)
5073               && GET_CODE (dest) != CC0
5074               && GET_CODE (dest) != PC)
5075             {
5076               new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5077
5078               /* If this substitution failed, this whole thing fails.  */
5079               if (GET_CODE (new_rtx) == CLOBBER
5080                   && XEXP (new_rtx, 0) == const0_rtx)
5081                 return new_rtx;
5082
5083               SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5084             }
5085         }
5086     }
5087   else
5088     {
5089       len = GET_RTX_LENGTH (code);
5090       fmt = GET_RTX_FORMAT (code);
5091
5092       /* We don't need to process a SET_DEST that is a register, CC0,
5093          or PC, so set up to skip this common case.  All other cases
5094          where we want to suppress replacing something inside a
5095          SET_SRC are handled via the IN_DEST operand.  */
5096       if (code == SET
5097           && (REG_P (SET_DEST (x))
5098               || GET_CODE (SET_DEST (x)) == CC0
5099               || GET_CODE (SET_DEST (x)) == PC))
5100         fmt = "ie";
5101
5102       /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5103          constant.  */
5104       if (fmt[0] == 'e')
5105         op0_mode = GET_MODE (XEXP (x, 0));
5106
5107       for (i = 0; i < len; i++)
5108         {
5109           if (fmt[i] == 'E')
5110             {
5111               int j;
5112               for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5113                 {
5114                   if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5115                     {
5116                       new_rtx = (unique_copy && n_occurrences
5117                              ? copy_rtx (to) : to);
5118                       n_occurrences++;
5119                     }
5120                   else
5121                     {
5122                       new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5123                                        unique_copy);
5124
5125                       /* If this substitution failed, this whole thing
5126                          fails.  */
5127                       if (GET_CODE (new_rtx) == CLOBBER
5128                           && XEXP (new_rtx, 0) == const0_rtx)
5129                         return new_rtx;
5130                     }
5131
5132                   SUBST (XVECEXP (x, i, j), new_rtx);
5133                 }
5134             }
5135           else if (fmt[i] == 'e')
5136             {
5137               /* If this is a register being set, ignore it.  */
5138               new_rtx = XEXP (x, i);
5139               if (in_dest
5140                   && i == 0
5141                   && (((code == SUBREG || code == ZERO_EXTRACT)
5142                        && REG_P (new_rtx))
5143                       || code == STRICT_LOW_PART))
5144                 ;
5145
5146               else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5147                 {
5148                   /* In general, don't install a subreg involving two
5149                      modes not tieable.  It can worsen register
5150                      allocation, and can even make invalid reload
5151                      insns, since the reg inside may need to be copied
5152                      from in the outside mode, and that may be invalid
5153                      if it is an fp reg copied in integer mode.
5154
5155                      We allow two exceptions to this: It is valid if
5156                      it is inside another SUBREG and the mode of that
5157                      SUBREG and the mode of the inside of TO is
5158                      tieable and it is valid if X is a SET that copies
5159                      FROM to CC0.  */
5160
5161                   if (GET_CODE (to) == SUBREG
5162                       && ! MODES_TIEABLE_P (GET_MODE (to),
5163                                             GET_MODE (SUBREG_REG (to)))
5164                       && ! (code == SUBREG
5165                             && MODES_TIEABLE_P (GET_MODE (x),
5166                                                 GET_MODE (SUBREG_REG (to))))
5167 #ifdef HAVE_cc0
5168                       && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5169 #endif
5170                       )
5171                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5172
5173 #ifdef CANNOT_CHANGE_MODE_CLASS
5174                   if (code == SUBREG
5175                       && REG_P (to)
5176                       && REGNO (to) < FIRST_PSEUDO_REGISTER
5177                       && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
5178                                                    GET_MODE (to),
5179                                                    GET_MODE (x)))
5180                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5181 #endif
5182
5183                   new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5184                   n_occurrences++;
5185                 }
5186               else
5187                 /* If we are in a SET_DEST, suppress most cases unless we
5188                    have gone inside a MEM, in which case we want to
5189                    simplify the address.  We assume here that things that
5190                    are actually part of the destination have their inner
5191                    parts in the first expression.  This is true for SUBREG,
5192                    STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5193                    things aside from REG and MEM that should appear in a
5194                    SET_DEST.  */
5195                 new_rtx = subst (XEXP (x, i), from, to,
5196                              (((in_dest
5197                                 && (code == SUBREG || code == STRICT_LOW_PART
5198                                     || code == ZERO_EXTRACT))
5199                                || code == SET)
5200                               && i == 0),
5201                                  code == IF_THEN_ELSE && i == 0,
5202                                  unique_copy);
5203
5204               /* If we found that we will have to reject this combination,
5205                  indicate that by returning the CLOBBER ourselves, rather than
5206                  an expression containing it.  This will speed things up as
5207                  well as prevent accidents where two CLOBBERs are considered
5208                  to be equal, thus producing an incorrect simplification.  */
5209
5210               if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5211                 return new_rtx;
5212
5213               if (GET_CODE (x) == SUBREG
5214                   && (CONST_INT_P (new_rtx)
5215                       || GET_CODE (new_rtx) == CONST_DOUBLE))
5216                 {
5217                   enum machine_mode mode = GET_MODE (x);
5218
5219                   x = simplify_subreg (GET_MODE (x), new_rtx,
5220                                        GET_MODE (SUBREG_REG (x)),
5221                                        SUBREG_BYTE (x));
5222                   if (! x)
5223                     x = gen_rtx_CLOBBER (mode, const0_rtx);
5224                 }
5225               else if (CONST_INT_P (new_rtx)
5226                        && GET_CODE (x) == ZERO_EXTEND)
5227                 {
5228                   x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5229                                                 new_rtx, GET_MODE (XEXP (x, 0)));
5230                   gcc_assert (x);
5231                 }
5232               else
5233                 SUBST (XEXP (x, i), new_rtx);
5234             }
5235         }
5236     }
5237
5238   /* Check if we are loading something from the constant pool via float
5239      extension; in this case we would undo compress_float_constant
5240      optimization and degenerate constant load to an immediate value.  */
5241   if (GET_CODE (x) == FLOAT_EXTEND
5242       && MEM_P (XEXP (x, 0))
5243       && MEM_READONLY_P (XEXP (x, 0)))
5244     {
5245       rtx tmp = avoid_constant_pool_reference (x);
5246       if (x != tmp)
5247         return x;
5248     }
5249
5250   /* Try to simplify X.  If the simplification changed the code, it is likely
5251      that further simplification will help, so loop, but limit the number
5252      of repetitions that will be performed.  */
5253
5254   for (i = 0; i < 4; i++)
5255     {
5256       /* If X is sufficiently simple, don't bother trying to do anything
5257          with it.  */
5258       if (code != CONST_INT && code != REG && code != CLOBBER)
5259         x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5260
5261       if (GET_CODE (x) == code)
5262         break;
5263
5264       code = GET_CODE (x);
5265
5266       /* We no longer know the original mode of operand 0 since we
5267          have changed the form of X)  */
5268       op0_mode = VOIDmode;
5269     }
5270
5271   return x;
5272 }
5273 \f
5274 /* Simplify X, a piece of RTL.  We just operate on the expression at the
5275    outer level; call `subst' to simplify recursively.  Return the new
5276    expression.
5277
5278    OP0_MODE is the original mode of XEXP (x, 0).  IN_DEST is nonzero
5279    if we are inside a SET_DEST.  IN_COND is nonzero if we are at the top level
5280    of a condition.  */
5281
5282 static rtx
5283 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest,
5284                       int in_cond)
5285 {
5286   enum rtx_code code = GET_CODE (x);
5287   enum machine_mode mode = GET_MODE (x);
5288   rtx temp;
5289   int i;
5290
5291   /* If this is a commutative operation, put a constant last and a complex
5292      expression first.  We don't need to do this for comparisons here.  */
5293   if (COMMUTATIVE_ARITH_P (x)
5294       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5295     {
5296       temp = XEXP (x, 0);
5297       SUBST (XEXP (x, 0), XEXP (x, 1));
5298       SUBST (XEXP (x, 1), temp);
5299     }
5300
5301   /* If this is a simple operation applied to an IF_THEN_ELSE, try
5302      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
5303      things.  Check for cases where both arms are testing the same
5304      condition.
5305
5306      Don't do anything if all operands are very simple.  */
5307
5308   if ((BINARY_P (x)
5309        && ((!OBJECT_P (XEXP (x, 0))
5310             && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5311                   && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5312            || (!OBJECT_P (XEXP (x, 1))
5313                && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5314                      && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5315       || (UNARY_P (x)
5316           && (!OBJECT_P (XEXP (x, 0))
5317                && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5318                      && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5319     {
5320       rtx cond, true_rtx, false_rtx;
5321
5322       cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5323       if (cond != 0
5324           /* If everything is a comparison, what we have is highly unlikely
5325              to be simpler, so don't use it.  */
5326           && ! (COMPARISON_P (x)
5327                 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5328         {
5329           rtx cop1 = const0_rtx;
5330           enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5331
5332           if (cond_code == NE && COMPARISON_P (cond))
5333             return x;
5334
5335           /* Simplify the alternative arms; this may collapse the true and
5336              false arms to store-flag values.  Be careful to use copy_rtx
5337              here since true_rtx or false_rtx might share RTL with x as a
5338              result of the if_then_else_cond call above.  */
5339           true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5340           false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5341
5342           /* If true_rtx and false_rtx are not general_operands, an if_then_else
5343              is unlikely to be simpler.  */
5344           if (general_operand (true_rtx, VOIDmode)
5345               && general_operand (false_rtx, VOIDmode))
5346             {
5347               enum rtx_code reversed;
5348
5349               /* Restarting if we generate a store-flag expression will cause
5350                  us to loop.  Just drop through in this case.  */
5351
5352               /* If the result values are STORE_FLAG_VALUE and zero, we can
5353                  just make the comparison operation.  */
5354               if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5355                 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5356                                              cond, cop1);
5357               else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5358                        && ((reversed = reversed_comparison_code_parts
5359                                         (cond_code, cond, cop1, NULL))
5360                            != UNKNOWN))
5361                 x = simplify_gen_relational (reversed, mode, VOIDmode,
5362                                              cond, cop1);
5363
5364               /* Likewise, we can make the negate of a comparison operation
5365                  if the result values are - STORE_FLAG_VALUE and zero.  */
5366               else if (CONST_INT_P (true_rtx)
5367                        && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5368                        && false_rtx == const0_rtx)
5369                 x = simplify_gen_unary (NEG, mode,
5370                                         simplify_gen_relational (cond_code,
5371                                                                  mode, VOIDmode,
5372                                                                  cond, cop1),
5373                                         mode);
5374               else if (CONST_INT_P (false_rtx)
5375                        && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5376                        && true_rtx == const0_rtx
5377                        && ((reversed = reversed_comparison_code_parts
5378                                         (cond_code, cond, cop1, NULL))
5379                            != UNKNOWN))
5380                 x = simplify_gen_unary (NEG, mode,
5381                                         simplify_gen_relational (reversed,
5382                                                                  mode, VOIDmode,
5383                                                                  cond, cop1),
5384                                         mode);
5385               else
5386                 return gen_rtx_IF_THEN_ELSE (mode,
5387                                              simplify_gen_relational (cond_code,
5388                                                                       mode,
5389                                                                       VOIDmode,
5390                                                                       cond,
5391                                                                       cop1),
5392                                              true_rtx, false_rtx);
5393
5394               code = GET_CODE (x);
5395               op0_mode = VOIDmode;
5396             }
5397         }
5398     }
5399
5400   /* Try to fold this expression in case we have constants that weren't
5401      present before.  */
5402   temp = 0;
5403   switch (GET_RTX_CLASS (code))
5404     {
5405     case RTX_UNARY:
5406       if (op0_mode == VOIDmode)
5407         op0_mode = GET_MODE (XEXP (x, 0));
5408       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5409       break;
5410     case RTX_COMPARE:
5411     case RTX_COMM_COMPARE:
5412       {
5413         enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5414         if (cmp_mode == VOIDmode)
5415           {
5416             cmp_mode = GET_MODE (XEXP (x, 1));
5417             if (cmp_mode == VOIDmode)
5418               cmp_mode = op0_mode;
5419           }
5420         temp = simplify_relational_operation (code, mode, cmp_mode,
5421                                               XEXP (x, 0), XEXP (x, 1));
5422       }
5423       break;
5424     case RTX_COMM_ARITH:
5425     case RTX_BIN_ARITH:
5426       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5427       break;
5428     case RTX_BITFIELD_OPS:
5429     case RTX_TERNARY:
5430       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5431                                          XEXP (x, 1), XEXP (x, 2));
5432       break;
5433     default:
5434       break;
5435     }
5436
5437   if (temp)
5438     {
5439       x = temp;
5440       code = GET_CODE (temp);
5441       op0_mode = VOIDmode;
5442       mode = GET_MODE (temp);
5443     }
5444
5445   /* First see if we can apply the inverse distributive law.  */
5446   if (code == PLUS || code == MINUS
5447       || code == AND || code == IOR || code == XOR)
5448     {
5449       x = apply_distributive_law (x);
5450       code = GET_CODE (x);
5451       op0_mode = VOIDmode;
5452     }
5453
5454   /* If CODE is an associative operation not otherwise handled, see if we
5455      can associate some operands.  This can win if they are constants or
5456      if they are logically related (i.e. (a & b) & a).  */
5457   if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5458        || code == AND || code == IOR || code == XOR
5459        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5460       && ((INTEGRAL_MODE_P (mode) && code != DIV)
5461           || (flag_associative_math && FLOAT_MODE_P (mode))))
5462     {
5463       if (GET_CODE (XEXP (x, 0)) == code)
5464         {
5465           rtx other = XEXP (XEXP (x, 0), 0);
5466           rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5467           rtx inner_op1 = XEXP (x, 1);
5468           rtx inner;
5469
5470           /* Make sure we pass the constant operand if any as the second
5471              one if this is a commutative operation.  */
5472           if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5473             {
5474               rtx tem = inner_op0;
5475               inner_op0 = inner_op1;
5476               inner_op1 = tem;
5477             }
5478           inner = simplify_binary_operation (code == MINUS ? PLUS
5479                                              : code == DIV ? MULT
5480                                              : code,
5481                                              mode, inner_op0, inner_op1);
5482
5483           /* For commutative operations, try the other pair if that one
5484              didn't simplify.  */
5485           if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5486             {
5487               other = XEXP (XEXP (x, 0), 1);
5488               inner = simplify_binary_operation (code, mode,
5489                                                  XEXP (XEXP (x, 0), 0),
5490                                                  XEXP (x, 1));
5491             }
5492
5493           if (inner)
5494             return simplify_gen_binary (code, mode, other, inner);
5495         }
5496     }
5497
5498   /* A little bit of algebraic simplification here.  */
5499   switch (code)
5500     {
5501     case MEM:
5502       /* Ensure that our address has any ASHIFTs converted to MULT in case
5503          address-recognizing predicates are called later.  */
5504       temp = make_compound_operation (XEXP (x, 0), MEM);
5505       SUBST (XEXP (x, 0), temp);
5506       break;
5507
5508     case SUBREG:
5509       if (op0_mode == VOIDmode)
5510         op0_mode = GET_MODE (SUBREG_REG (x));
5511
5512       /* See if this can be moved to simplify_subreg.  */
5513       if (CONSTANT_P (SUBREG_REG (x))
5514           && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5515              /* Don't call gen_lowpart if the inner mode
5516                 is VOIDmode and we cannot simplify it, as SUBREG without
5517                 inner mode is invalid.  */
5518           && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5519               || gen_lowpart_common (mode, SUBREG_REG (x))))
5520         return gen_lowpart (mode, SUBREG_REG (x));
5521
5522       if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5523         break;
5524       {
5525         rtx temp;
5526         temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5527                                 SUBREG_BYTE (x));
5528         if (temp)
5529           return temp;
5530       }
5531
5532       /* Don't change the mode of the MEM if that would change the meaning
5533          of the address.  */
5534       if (MEM_P (SUBREG_REG (x))
5535           && (MEM_VOLATILE_P (SUBREG_REG (x))
5536               || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
5537         return gen_rtx_CLOBBER (mode, const0_rtx);
5538
5539       /* Note that we cannot do any narrowing for non-constants since
5540          we might have been counting on using the fact that some bits were
5541          zero.  We now do this in the SET.  */
5542
5543       break;
5544
5545     case NEG:
5546       temp = expand_compound_operation (XEXP (x, 0));
5547
5548       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5549          replaced by (lshiftrt X C).  This will convert
5550          (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
5551
5552       if (GET_CODE (temp) == ASHIFTRT
5553           && CONST_INT_P (XEXP (temp, 1))
5554           && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
5555         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5556                                      INTVAL (XEXP (temp, 1)));
5557
5558       /* If X has only a single bit that might be nonzero, say, bit I, convert
5559          (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5560          MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
5561          (sign_extract X 1 Y).  But only do this if TEMP isn't a register
5562          or a SUBREG of one since we'd be making the expression more
5563          complex if it was just a register.  */
5564
5565       if (!REG_P (temp)
5566           && ! (GET_CODE (temp) == SUBREG
5567                 && REG_P (SUBREG_REG (temp)))
5568           && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5569         {
5570           rtx temp1 = simplify_shift_const
5571             (NULL_RTX, ASHIFTRT, mode,
5572              simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5573                                    GET_MODE_BITSIZE (mode) - 1 - i),
5574              GET_MODE_BITSIZE (mode) - 1 - i);
5575
5576           /* If all we did was surround TEMP with the two shifts, we
5577              haven't improved anything, so don't use it.  Otherwise,
5578              we are better off with TEMP1.  */
5579           if (GET_CODE (temp1) != ASHIFTRT
5580               || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5581               || XEXP (XEXP (temp1, 0), 0) != temp)
5582             return temp1;
5583         }
5584       break;
5585
5586     case TRUNCATE:
5587       /* We can't handle truncation to a partial integer mode here
5588          because we don't know the real bitsize of the partial
5589          integer mode.  */
5590       if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5591         break;
5592
5593       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5594         SUBST (XEXP (x, 0),
5595                force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5596                               GET_MODE_MASK (mode), 0));
5597
5598       /* We can truncate a constant value and return it.  */
5599       if (CONST_INT_P (XEXP (x, 0)))
5600         return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5601
5602       /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5603          whose value is a comparison can be replaced with a subreg if
5604          STORE_FLAG_VALUE permits.  */
5605       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5606           && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5607           && (temp = get_last_value (XEXP (x, 0)))
5608           && COMPARISON_P (temp))
5609         return gen_lowpart (mode, XEXP (x, 0));
5610       break;
5611
5612     case CONST:
5613       /* (const (const X)) can become (const X).  Do it this way rather than
5614          returning the inner CONST since CONST can be shared with a
5615          REG_EQUAL note.  */
5616       if (GET_CODE (XEXP (x, 0)) == CONST)
5617         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5618       break;
5619
5620 #ifdef HAVE_lo_sum
5621     case LO_SUM:
5622       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
5623          can add in an offset.  find_split_point will split this address up
5624          again if it doesn't match.  */
5625       if (GET_CODE (XEXP (x, 0)) == HIGH
5626           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5627         return XEXP (x, 1);
5628       break;
5629 #endif
5630
5631     case PLUS:
5632       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5633          when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5634          bit-field and can be replaced by either a sign_extend or a
5635          sign_extract.  The `and' may be a zero_extend and the two
5636          <c>, -<c> constants may be reversed.  */
5637       if (GET_CODE (XEXP (x, 0)) == XOR
5638           && CONST_INT_P (XEXP (x, 1))
5639           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5640           && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5641           && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5642               || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5643           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5644           && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5645                && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5646                && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5647                    == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5648               || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5649                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5650                       == (unsigned int) i + 1))))
5651         return simplify_shift_const
5652           (NULL_RTX, ASHIFTRT, mode,
5653            simplify_shift_const (NULL_RTX, ASHIFT, mode,
5654                                  XEXP (XEXP (XEXP (x, 0), 0), 0),
5655                                  GET_MODE_BITSIZE (mode) - (i + 1)),
5656            GET_MODE_BITSIZE (mode) - (i + 1));
5657
5658       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5659          can become (ashiftrt (ashift (xor x 1) C) C) where C is
5660          the bitsize of the mode - 1.  This allows simplification of
5661          "a = (b & 8) == 0;"  */
5662       if (XEXP (x, 1) == constm1_rtx
5663           && !REG_P (XEXP (x, 0))
5664           && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5665                 && REG_P (SUBREG_REG (XEXP (x, 0))))
5666           && nonzero_bits (XEXP (x, 0), mode) == 1)
5667         return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5668            simplify_shift_const (NULL_RTX, ASHIFT, mode,
5669                                  gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5670                                  GET_MODE_BITSIZE (mode) - 1),
5671            GET_MODE_BITSIZE (mode) - 1);
5672
5673       /* If we are adding two things that have no bits in common, convert
5674          the addition into an IOR.  This will often be further simplified,
5675          for example in cases like ((a & 1) + (a & 2)), which can
5676          become a & 3.  */
5677
5678       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5679           && (nonzero_bits (XEXP (x, 0), mode)
5680               & nonzero_bits (XEXP (x, 1), mode)) == 0)
5681         {
5682           /* Try to simplify the expression further.  */
5683           rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5684           temp = combine_simplify_rtx (tor, mode, in_dest, 0);
5685
5686           /* If we could, great.  If not, do not go ahead with the IOR
5687              replacement, since PLUS appears in many special purpose
5688              address arithmetic instructions.  */
5689           if (GET_CODE (temp) != CLOBBER && temp != tor)
5690             return temp;
5691         }
5692       break;
5693
5694     case MINUS:
5695       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5696          (and <foo> (const_int pow2-1))  */
5697       if (GET_CODE (XEXP (x, 1)) == AND
5698           && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5699           && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5700           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5701         return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5702                                        -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5703       break;
5704
5705     case MULT:
5706       /* If we have (mult (plus A B) C), apply the distributive law and then
5707          the inverse distributive law to see if things simplify.  This
5708          occurs mostly in addresses, often when unrolling loops.  */
5709
5710       if (GET_CODE (XEXP (x, 0)) == PLUS)
5711         {
5712           rtx result = distribute_and_simplify_rtx (x, 0);
5713           if (result)
5714             return result;
5715         }
5716
5717       /* Try simplify a*(b/c) as (a*b)/c.  */
5718       if (FLOAT_MODE_P (mode) && flag_associative_math
5719           && GET_CODE (XEXP (x, 0)) == DIV)
5720         {
5721           rtx tem = simplify_binary_operation (MULT, mode,
5722                                                XEXP (XEXP (x, 0), 0),
5723                                                XEXP (x, 1));
5724           if (tem)
5725             return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5726         }
5727       break;
5728
5729     case UDIV:
5730       /* If this is a divide by a power of two, treat it as a shift if
5731          its first operand is a shift.  */
5732       if (CONST_INT_P (XEXP (x, 1))
5733           && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5734           && (GET_CODE (XEXP (x, 0)) == ASHIFT
5735               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5736               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5737               || GET_CODE (XEXP (x, 0)) == ROTATE
5738               || GET_CODE (XEXP (x, 0)) == ROTATERT))
5739         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5740       break;
5741
5742     case EQ:  case NE:
5743     case GT:  case GTU:  case GE:  case GEU:
5744     case LT:  case LTU:  case LE:  case LEU:
5745     case UNEQ:  case LTGT:
5746     case UNGT:  case UNGE:
5747     case UNLT:  case UNLE:
5748     case UNORDERED: case ORDERED:
5749       /* If the first operand is a condition code, we can't do anything
5750          with it.  */
5751       if (GET_CODE (XEXP (x, 0)) == COMPARE
5752           || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5753               && ! CC0_P (XEXP (x, 0))))
5754         {
5755           rtx op0 = XEXP (x, 0);
5756           rtx op1 = XEXP (x, 1);
5757           enum rtx_code new_code;
5758
5759           if (GET_CODE (op0) == COMPARE)
5760             op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5761
5762           /* Simplify our comparison, if possible.  */
5763           new_code = simplify_comparison (code, &op0, &op1);
5764
5765           /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5766              if only the low-order bit is possibly nonzero in X (such as when
5767              X is a ZERO_EXTRACT of one bit).  Similarly, we can convert EQ to
5768              (xor X 1) or (minus 1 X); we use the former.  Finally, if X is
5769              known to be either 0 or -1, NE becomes a NEG and EQ becomes
5770              (plus X 1).
5771
5772              Remove any ZERO_EXTRACT we made when thinking this was a
5773              comparison.  It may now be simpler to use, e.g., an AND.  If a
5774              ZERO_EXTRACT is indeed appropriate, it will be placed back by
5775              the call to make_compound_operation in the SET case.
5776
5777              Don't apply these optimizations if the caller would
5778              prefer a comparison rather than a value.
5779              E.g., for the condition in an IF_THEN_ELSE most targets need
5780              an explicit comparison.  */
5781
5782           if (in_cond)
5783             ;
5784
5785           else if (STORE_FLAG_VALUE == 1
5786               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5787               && op1 == const0_rtx
5788               && mode == GET_MODE (op0)
5789               && nonzero_bits (op0, mode) == 1)
5790             return gen_lowpart (mode,
5791                                 expand_compound_operation (op0));
5792
5793           else if (STORE_FLAG_VALUE == 1
5794                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5795                    && op1 == const0_rtx
5796                    && mode == GET_MODE (op0)
5797                    && (num_sign_bit_copies (op0, mode)
5798                        == GET_MODE_BITSIZE (mode)))
5799             {
5800               op0 = expand_compound_operation (op0);
5801               return simplify_gen_unary (NEG, mode,
5802                                          gen_lowpart (mode, op0),
5803                                          mode);
5804             }
5805
5806           else if (STORE_FLAG_VALUE == 1
5807                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5808                    && op1 == const0_rtx
5809                    && mode == GET_MODE (op0)
5810                    && nonzero_bits (op0, mode) == 1)
5811             {
5812               op0 = expand_compound_operation (op0);
5813               return simplify_gen_binary (XOR, mode,
5814                                           gen_lowpart (mode, op0),
5815                                           const1_rtx);
5816             }
5817
5818           else if (STORE_FLAG_VALUE == 1
5819                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5820                    && op1 == const0_rtx
5821                    && mode == GET_MODE (op0)
5822                    && (num_sign_bit_copies (op0, mode)
5823                        == GET_MODE_BITSIZE (mode)))
5824             {
5825               op0 = expand_compound_operation (op0);
5826               return plus_constant (gen_lowpart (mode, op0), 1);
5827             }
5828
5829           /* If STORE_FLAG_VALUE is -1, we have cases similar to
5830              those above.  */
5831           if (in_cond)
5832             ;
5833
5834           else if (STORE_FLAG_VALUE == -1
5835               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5836               && op1 == const0_rtx
5837               && (num_sign_bit_copies (op0, mode)
5838                   == GET_MODE_BITSIZE (mode)))
5839             return gen_lowpart (mode,
5840                                 expand_compound_operation (op0));
5841
5842           else if (STORE_FLAG_VALUE == -1
5843                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5844                    && op1 == const0_rtx
5845                    && mode == GET_MODE (op0)
5846                    && nonzero_bits (op0, mode) == 1)
5847             {
5848               op0 = expand_compound_operation (op0);
5849               return simplify_gen_unary (NEG, mode,
5850                                          gen_lowpart (mode, op0),
5851                                          mode);
5852             }
5853
5854           else if (STORE_FLAG_VALUE == -1
5855                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5856                    && op1 == const0_rtx
5857                    && mode == GET_MODE (op0)
5858                    && (num_sign_bit_copies (op0, mode)
5859                        == GET_MODE_BITSIZE (mode)))
5860             {
5861               op0 = expand_compound_operation (op0);
5862               return simplify_gen_unary (NOT, mode,
5863                                          gen_lowpart (mode, op0),
5864                                          mode);
5865             }
5866
5867           /* If X is 0/1, (eq X 0) is X-1.  */
5868           else if (STORE_FLAG_VALUE == -1
5869                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5870                    && op1 == const0_rtx
5871                    && mode == GET_MODE (op0)
5872                    && nonzero_bits (op0, mode) == 1)
5873             {
5874               op0 = expand_compound_operation (op0);
5875               return plus_constant (gen_lowpart (mode, op0), -1);
5876             }
5877
5878           /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5879              one bit that might be nonzero, we can convert (ne x 0) to
5880              (ashift x c) where C puts the bit in the sign bit.  Remove any
5881              AND with STORE_FLAG_VALUE when we are done, since we are only
5882              going to test the sign bit.  */
5883           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5884               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5885               && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5886                   == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5887               && op1 == const0_rtx
5888               && mode == GET_MODE (op0)
5889               && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5890             {
5891               x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5892                                         expand_compound_operation (op0),
5893                                         GET_MODE_BITSIZE (mode) - 1 - i);
5894               if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5895                 return XEXP (x, 0);
5896               else
5897                 return x;
5898             }
5899
5900           /* If the code changed, return a whole new comparison.  */
5901           if (new_code != code)
5902             return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5903
5904           /* Otherwise, keep this operation, but maybe change its operands.
5905              This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
5906           SUBST (XEXP (x, 0), op0);
5907           SUBST (XEXP (x, 1), op1);
5908         }
5909       break;
5910
5911     case IF_THEN_ELSE:
5912       return simplify_if_then_else (x);
5913
5914     case ZERO_EXTRACT:
5915     case SIGN_EXTRACT:
5916     case ZERO_EXTEND:
5917     case SIGN_EXTEND:
5918       /* If we are processing SET_DEST, we are done.  */
5919       if (in_dest)
5920         return x;
5921
5922       return expand_compound_operation (x);
5923
5924     case SET:
5925       return simplify_set (x);
5926
5927     case AND:
5928     case IOR:
5929       return simplify_logical (x);
5930
5931     case ASHIFT:
5932     case LSHIFTRT:
5933     case ASHIFTRT:
5934     case ROTATE:
5935     case ROTATERT:
5936       /* If this is a shift by a constant amount, simplify it.  */
5937       if (CONST_INT_P (XEXP (x, 1)))
5938         return simplify_shift_const (x, code, mode, XEXP (x, 0),
5939                                      INTVAL (XEXP (x, 1)));
5940
5941       else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5942         SUBST (XEXP (x, 1),
5943                force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5944                               ((unsigned HOST_WIDE_INT) 1
5945                                << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5946                               - 1,
5947                               0));
5948       break;
5949
5950     default:
5951       break;
5952     }
5953
5954   return x;
5955 }
5956 \f
5957 /* Simplify X, an IF_THEN_ELSE expression.  Return the new expression.  */
5958
5959 static rtx
5960 simplify_if_then_else (rtx x)
5961 {
5962   enum machine_mode mode = GET_MODE (x);
5963   rtx cond = XEXP (x, 0);
5964   rtx true_rtx = XEXP (x, 1);
5965   rtx false_rtx = XEXP (x, 2);
5966   enum rtx_code true_code = GET_CODE (cond);
5967   int comparison_p = COMPARISON_P (cond);
5968   rtx temp;
5969   int i;
5970   enum rtx_code false_code;
5971   rtx reversed;
5972
5973   /* Simplify storing of the truth value.  */
5974   if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5975     return simplify_gen_relational (true_code, mode, VOIDmode,
5976                                     XEXP (cond, 0), XEXP (cond, 1));
5977
5978   /* Also when the truth value has to be reversed.  */
5979   if (comparison_p
5980       && true_rtx == const0_rtx && false_rtx == const_true_rtx
5981       && (reversed = reversed_comparison (cond, mode)))
5982     return reversed;
5983
5984   /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5985      in it is being compared against certain values.  Get the true and false
5986      comparisons and see if that says anything about the value of each arm.  */
5987
5988   if (comparison_p
5989       && ((false_code = reversed_comparison_code (cond, NULL))
5990           != UNKNOWN)
5991       && REG_P (XEXP (cond, 0)))
5992     {
5993       HOST_WIDE_INT nzb;
5994       rtx from = XEXP (cond, 0);
5995       rtx true_val = XEXP (cond, 1);
5996       rtx false_val = true_val;
5997       int swapped = 0;
5998
5999       /* If FALSE_CODE is EQ, swap the codes and arms.  */
6000
6001       if (false_code == EQ)
6002         {
6003           swapped = 1, true_code = EQ, false_code = NE;
6004           temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6005         }
6006
6007       /* If we are comparing against zero and the expression being tested has
6008          only a single bit that might be nonzero, that is its value when it is
6009          not equal to zero.  Similarly if it is known to be -1 or 0.  */
6010
6011       if (true_code == EQ && true_val == const0_rtx
6012           && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
6013         {
6014           false_code = EQ;
6015           false_val = GEN_INT (trunc_int_for_mode (nzb, GET_MODE (from)));
6016         }
6017       else if (true_code == EQ && true_val == const0_rtx
6018                && (num_sign_bit_copies (from, GET_MODE (from))
6019                    == GET_MODE_BITSIZE (GET_MODE (from))))
6020         {
6021           false_code = EQ;
6022           false_val = constm1_rtx;
6023         }
6024
6025       /* Now simplify an arm if we know the value of the register in the
6026          branch and it is used in the arm.  Be careful due to the potential
6027          of locally-shared RTL.  */
6028
6029       if (reg_mentioned_p (from, true_rtx))
6030         true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6031                                       from, true_val),
6032                           pc_rtx, pc_rtx, 0, 0, 0);
6033       if (reg_mentioned_p (from, false_rtx))
6034         false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6035                                    from, false_val),
6036                            pc_rtx, pc_rtx, 0, 0, 0);
6037
6038       SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6039       SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6040
6041       true_rtx = XEXP (x, 1);
6042       false_rtx = XEXP (x, 2);
6043       true_code = GET_CODE (cond);
6044     }
6045
6046   /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6047      reversed, do so to avoid needing two sets of patterns for
6048      subtract-and-branch insns.  Similarly if we have a constant in the true
6049      arm, the false arm is the same as the first operand of the comparison, or
6050      the false arm is more complicated than the true arm.  */
6051
6052   if (comparison_p
6053       && reversed_comparison_code (cond, NULL) != UNKNOWN
6054       && (true_rtx == pc_rtx
6055           || (CONSTANT_P (true_rtx)
6056               && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6057           || true_rtx == const0_rtx
6058           || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6059           || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6060               && !OBJECT_P (false_rtx))
6061           || reg_mentioned_p (true_rtx, false_rtx)
6062           || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6063     {
6064       true_code = reversed_comparison_code (cond, NULL);
6065       SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6066       SUBST (XEXP (x, 1), false_rtx);
6067       SUBST (XEXP (x, 2), true_rtx);
6068
6069       temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6070       cond = XEXP (x, 0);
6071
6072       /* It is possible that the conditional has been simplified out.  */
6073       true_code = GET_CODE (cond);
6074       comparison_p = COMPARISON_P (cond);
6075     }
6076
6077   /* If the two arms are identical, we don't need the comparison.  */
6078
6079   if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6080     return true_rtx;
6081
6082   /* Convert a == b ? b : a to "a".  */
6083   if (true_code == EQ && ! side_effects_p (cond)
6084       && !HONOR_NANS (mode)
6085       && rtx_equal_p (XEXP (cond, 0), false_rtx)
6086       && rtx_equal_p (XEXP (cond, 1), true_rtx))
6087     return false_rtx;
6088   else if (true_code == NE && ! side_effects_p (cond)
6089            && !HONOR_NANS (mode)
6090            && rtx_equal_p (XEXP (cond, 0), true_rtx)
6091            && rtx_equal_p (XEXP (cond, 1), false_rtx))
6092     return true_rtx;
6093
6094   /* Look for cases where we have (abs x) or (neg (abs X)).  */
6095
6096   if (GET_MODE_CLASS (mode) == MODE_INT
6097       && comparison_p
6098       && XEXP (cond, 1) == const0_rtx
6099       && GET_CODE (false_rtx) == NEG
6100       && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6101       && rtx_equal_p (true_rtx, XEXP (cond, 0))
6102       && ! side_effects_p (true_rtx))
6103     switch (true_code)
6104       {
6105       case GT:
6106       case GE:
6107         return simplify_gen_unary (ABS, mode, true_rtx, mode);
6108       case LT:
6109       case LE:
6110         return
6111           simplify_gen_unary (NEG, mode,
6112                               simplify_gen_unary (ABS, mode, true_rtx, mode),
6113                               mode);
6114       default:
6115         break;
6116       }
6117
6118   /* Look for MIN or MAX.  */
6119
6120   if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6121       && comparison_p
6122       && rtx_equal_p (XEXP (cond, 0), true_rtx)
6123       && rtx_equal_p (XEXP (cond, 1), false_rtx)
6124       && ! side_effects_p (cond))
6125     switch (true_code)
6126       {
6127       case GE:
6128       case GT:
6129         return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6130       case LE:
6131       case LT:
6132         return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6133       case GEU:
6134       case GTU:
6135         return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6136       case LEU:
6137       case LTU:
6138         return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6139       default:
6140         break;
6141       }
6142
6143   /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6144      second operand is zero, this can be done as (OP Z (mult COND C2)) where
6145      C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6146      SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6147      We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6148      neither 1 or -1, but it isn't worth checking for.  */
6149
6150   if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6151       && comparison_p
6152       && GET_MODE_CLASS (mode) == MODE_INT
6153       && ! side_effects_p (x))
6154     {
6155       rtx t = make_compound_operation (true_rtx, SET);
6156       rtx f = make_compound_operation (false_rtx, SET);
6157       rtx cond_op0 = XEXP (cond, 0);
6158       rtx cond_op1 = XEXP (cond, 1);
6159       enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6160       enum machine_mode m = mode;
6161       rtx z = 0, c1 = NULL_RTX;
6162
6163       if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6164            || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6165            || GET_CODE (t) == ASHIFT
6166            || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6167           && rtx_equal_p (XEXP (t, 0), f))
6168         c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6169
6170       /* If an identity-zero op is commutative, check whether there
6171          would be a match if we swapped the operands.  */
6172       else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6173                 || GET_CODE (t) == XOR)
6174                && rtx_equal_p (XEXP (t, 1), f))
6175         c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6176       else if (GET_CODE (t) == SIGN_EXTEND
6177                && (GET_CODE (XEXP (t, 0)) == PLUS
6178                    || GET_CODE (XEXP (t, 0)) == MINUS
6179                    || GET_CODE (XEXP (t, 0)) == IOR
6180                    || GET_CODE (XEXP (t, 0)) == XOR
6181                    || GET_CODE (XEXP (t, 0)) == ASHIFT
6182                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6183                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6184                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6185                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6186                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6187                && (num_sign_bit_copies (f, GET_MODE (f))
6188                    > (unsigned int)
6189                      (GET_MODE_BITSIZE (mode)
6190                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6191         {
6192           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6193           extend_op = SIGN_EXTEND;
6194           m = GET_MODE (XEXP (t, 0));
6195         }
6196       else if (GET_CODE (t) == SIGN_EXTEND
6197                && (GET_CODE (XEXP (t, 0)) == PLUS
6198                    || GET_CODE (XEXP (t, 0)) == IOR
6199                    || GET_CODE (XEXP (t, 0)) == XOR)
6200                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6201                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6202                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6203                && (num_sign_bit_copies (f, GET_MODE (f))
6204                    > (unsigned int)
6205                      (GET_MODE_BITSIZE (mode)
6206                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6207         {
6208           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6209           extend_op = SIGN_EXTEND;
6210           m = GET_MODE (XEXP (t, 0));
6211         }
6212       else if (GET_CODE (t) == ZERO_EXTEND
6213                && (GET_CODE (XEXP (t, 0)) == PLUS
6214                    || GET_CODE (XEXP (t, 0)) == MINUS
6215                    || GET_CODE (XEXP (t, 0)) == IOR
6216                    || GET_CODE (XEXP (t, 0)) == XOR
6217                    || GET_CODE (XEXP (t, 0)) == ASHIFT
6218                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6219                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6220                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6221                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6222                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6223                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6224                && ((nonzero_bits (f, GET_MODE (f))
6225                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6226                    == 0))
6227         {
6228           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6229           extend_op = ZERO_EXTEND;
6230           m = GET_MODE (XEXP (t, 0));
6231         }
6232       else if (GET_CODE (t) == ZERO_EXTEND
6233                && (GET_CODE (XEXP (t, 0)) == PLUS
6234                    || GET_CODE (XEXP (t, 0)) == IOR
6235                    || GET_CODE (XEXP (t, 0)) == XOR)
6236                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6237                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6238                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6239                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6240                && ((nonzero_bits (f, GET_MODE (f))
6241                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6242                    == 0))
6243         {
6244           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6245           extend_op = ZERO_EXTEND;
6246           m = GET_MODE (XEXP (t, 0));
6247         }
6248
6249       if (z)
6250         {
6251           temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6252                                                  cond_op0, cond_op1),
6253                         pc_rtx, pc_rtx, 0, 0, 0);
6254           temp = simplify_gen_binary (MULT, m, temp,
6255                                       simplify_gen_binary (MULT, m, c1,
6256                                                            const_true_rtx));
6257           temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6258           temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6259
6260           if (extend_op != UNKNOWN)
6261             temp = simplify_gen_unary (extend_op, mode, temp, m);
6262
6263           return temp;
6264         }
6265     }
6266
6267   /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6268      1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6269      negation of a single bit, we can convert this operation to a shift.  We
6270      can actually do this more generally, but it doesn't seem worth it.  */
6271
6272   if (true_code == NE && XEXP (cond, 1) == const0_rtx
6273       && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6274       && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6275            && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6276           || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6277                == GET_MODE_BITSIZE (mode))
6278               && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6279     return
6280       simplify_shift_const (NULL_RTX, ASHIFT, mode,
6281                             gen_lowpart (mode, XEXP (cond, 0)), i);
6282
6283   /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8.  */
6284   if (true_code == NE && XEXP (cond, 1) == const0_rtx
6285       && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6286       && GET_MODE (XEXP (cond, 0)) == mode
6287       && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6288           == nonzero_bits (XEXP (cond, 0), mode)
6289       && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6290     return XEXP (cond, 0);
6291
6292   return x;
6293 }
6294 \f
6295 /* Simplify X, a SET expression.  Return the new expression.  */
6296
6297 static rtx
6298 simplify_set (rtx x)
6299 {
6300   rtx src = SET_SRC (x);
6301   rtx dest = SET_DEST (x);
6302   enum machine_mode mode
6303     = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6304   rtx other_insn;
6305   rtx *cc_use;
6306
6307   /* (set (pc) (return)) gets written as (return).  */
6308   if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
6309     return src;
6310
6311   /* Now that we know for sure which bits of SRC we are using, see if we can
6312      simplify the expression for the object knowing that we only need the
6313      low-order bits.  */
6314
6315   if (GET_MODE_CLASS (mode) == MODE_INT
6316       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
6317     {
6318       src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6319       SUBST (SET_SRC (x), src);
6320     }
6321
6322   /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6323      the comparison result and try to simplify it unless we already have used
6324      undobuf.other_insn.  */
6325   if ((GET_MODE_CLASS (mode) == MODE_CC
6326        || GET_CODE (src) == COMPARE
6327        || CC0_P (dest))
6328       && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6329       && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6330       && COMPARISON_P (*cc_use)
6331       && rtx_equal_p (XEXP (*cc_use, 0), dest))
6332     {
6333       enum rtx_code old_code = GET_CODE (*cc_use);
6334       enum rtx_code new_code;
6335       rtx op0, op1, tmp;
6336       int other_changed = 0;
6337       rtx inner_compare = NULL_RTX;
6338       enum machine_mode compare_mode = GET_MODE (dest);
6339
6340       if (GET_CODE (src) == COMPARE)
6341         {
6342           op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6343           if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6344             {
6345               inner_compare = op0;
6346               op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6347             }
6348         }
6349       else
6350         op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6351
6352       tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6353                                            op0, op1);
6354       if (!tmp)
6355         new_code = old_code;
6356       else if (!CONSTANT_P (tmp))
6357         {
6358           new_code = GET_CODE (tmp);
6359           op0 = XEXP (tmp, 0);
6360           op1 = XEXP (tmp, 1);
6361         }
6362       else
6363         {
6364           rtx pat = PATTERN (other_insn);
6365           undobuf.other_insn = other_insn;
6366           SUBST (*cc_use, tmp);
6367
6368           /* Attempt to simplify CC user.  */
6369           if (GET_CODE (pat) == SET)
6370             {
6371               rtx new_rtx = simplify_rtx (SET_SRC (pat));
6372               if (new_rtx != NULL_RTX)
6373                 SUBST (SET_SRC (pat), new_rtx);
6374             }
6375
6376           /* Convert X into a no-op move.  */
6377           SUBST (SET_DEST (x), pc_rtx);
6378           SUBST (SET_SRC (x), pc_rtx);
6379           return x;
6380         }
6381
6382       /* Simplify our comparison, if possible.  */
6383       new_code = simplify_comparison (new_code, &op0, &op1);
6384
6385 #ifdef SELECT_CC_MODE
6386       /* If this machine has CC modes other than CCmode, check to see if we
6387          need to use a different CC mode here.  */
6388       if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6389         compare_mode = GET_MODE (op0);
6390       else if (inner_compare
6391                && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6392                && new_code == old_code
6393                && op0 == XEXP (inner_compare, 0)
6394                && op1 == XEXP (inner_compare, 1))
6395         compare_mode = GET_MODE (inner_compare);
6396       else
6397         compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6398
6399 #ifndef HAVE_cc0
6400       /* If the mode changed, we have to change SET_DEST, the mode in the
6401          compare, and the mode in the place SET_DEST is used.  If SET_DEST is
6402          a hard register, just build new versions with the proper mode.  If it
6403          is a pseudo, we lose unless it is only time we set the pseudo, in
6404          which case we can safely change its mode.  */
6405       if (compare_mode != GET_MODE (dest))
6406         {
6407           if (can_change_dest_mode (dest, 0, compare_mode))
6408             {
6409               unsigned int regno = REGNO (dest);
6410               rtx new_dest;
6411
6412               if (regno < FIRST_PSEUDO_REGISTER)
6413                 new_dest = gen_rtx_REG (compare_mode, regno);
6414               else
6415                 {
6416                   SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6417                   new_dest = regno_reg_rtx[regno];
6418                 }
6419
6420               SUBST (SET_DEST (x), new_dest);
6421               SUBST (XEXP (*cc_use, 0), new_dest);
6422               other_changed = 1;
6423
6424               dest = new_dest;
6425             }
6426         }
6427 #endif  /* cc0 */
6428 #endif  /* SELECT_CC_MODE */
6429
6430       /* If the code changed, we have to build a new comparison in
6431          undobuf.other_insn.  */
6432       if (new_code != old_code)
6433         {
6434           int other_changed_previously = other_changed;
6435           unsigned HOST_WIDE_INT mask;
6436           rtx old_cc_use = *cc_use;
6437
6438           SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6439                                           dest, const0_rtx));
6440           other_changed = 1;
6441
6442           /* If the only change we made was to change an EQ into an NE or
6443              vice versa, OP0 has only one bit that might be nonzero, and OP1
6444              is zero, check if changing the user of the condition code will
6445              produce a valid insn.  If it won't, we can keep the original code
6446              in that insn by surrounding our operation with an XOR.  */
6447
6448           if (((old_code == NE && new_code == EQ)
6449                || (old_code == EQ && new_code == NE))
6450               && ! other_changed_previously && op1 == const0_rtx
6451               && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
6452               && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6453             {
6454               rtx pat = PATTERN (other_insn), note = 0;
6455
6456               if ((recog_for_combine (&pat, other_insn, &note) < 0
6457                    && ! check_asm_operands (pat)))
6458                 {
6459                   *cc_use = old_cc_use;
6460                   other_changed = 0;
6461
6462                   op0 = simplify_gen_binary (XOR, GET_MODE (op0),
6463                                              op0, GEN_INT (mask));
6464                 }
6465             }
6466         }
6467
6468       if (other_changed)
6469         undobuf.other_insn = other_insn;
6470
6471       /* Otherwise, if we didn't previously have a COMPARE in the
6472          correct mode, we need one.  */
6473       if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6474         {
6475           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6476           src = SET_SRC (x);
6477         }
6478       else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6479         {
6480           SUBST (SET_SRC (x), op0);
6481           src = SET_SRC (x);
6482         }
6483       /* Otherwise, update the COMPARE if needed.  */
6484       else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6485         {
6486           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6487           src = SET_SRC (x);
6488         }
6489     }
6490   else
6491     {
6492       /* Get SET_SRC in a form where we have placed back any
6493          compound expressions.  Then do the checks below.  */
6494       src = make_compound_operation (src, SET);
6495       SUBST (SET_SRC (x), src);
6496     }
6497
6498   /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6499      and X being a REG or (subreg (reg)), we may be able to convert this to
6500      (set (subreg:m2 x) (op)).
6501
6502      We can always do this if M1 is narrower than M2 because that means that
6503      we only care about the low bits of the result.
6504
6505      However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6506      perform a narrower operation than requested since the high-order bits will
6507      be undefined.  On machine where it is defined, this transformation is safe
6508      as long as M1 and M2 have the same number of words.  */
6509
6510   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6511       && !OBJECT_P (SUBREG_REG (src))
6512       && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6513            / UNITS_PER_WORD)
6514           == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6515                + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6516 #ifndef WORD_REGISTER_OPERATIONS
6517       && (GET_MODE_SIZE (GET_MODE (src))
6518         < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6519 #endif
6520 #ifdef CANNOT_CHANGE_MODE_CLASS
6521       && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6522             && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6523                                          GET_MODE (SUBREG_REG (src)),
6524                                          GET_MODE (src)))
6525 #endif
6526       && (REG_P (dest)
6527           || (GET_CODE (dest) == SUBREG
6528               && REG_P (SUBREG_REG (dest)))))
6529     {
6530       SUBST (SET_DEST (x),
6531              gen_lowpart (GET_MODE (SUBREG_REG (src)),
6532                                       dest));
6533       SUBST (SET_SRC (x), SUBREG_REG (src));
6534
6535       src = SET_SRC (x), dest = SET_DEST (x);
6536     }
6537
6538 #ifdef HAVE_cc0
6539   /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6540      in SRC.  */
6541   if (dest == cc0_rtx
6542       && GET_CODE (src) == SUBREG
6543       && subreg_lowpart_p (src)
6544       && (GET_MODE_BITSIZE (GET_MODE (src))
6545           < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
6546     {
6547       rtx inner = SUBREG_REG (src);
6548       enum machine_mode inner_mode = GET_MODE (inner);
6549
6550       /* Here we make sure that we don't have a sign bit on.  */
6551       if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
6552           && (nonzero_bits (inner, inner_mode)
6553               < ((unsigned HOST_WIDE_INT) 1
6554                  << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
6555         {
6556           SUBST (SET_SRC (x), inner);
6557           src = SET_SRC (x);
6558         }
6559     }
6560 #endif
6561
6562 #ifdef LOAD_EXTEND_OP
6563   /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6564      would require a paradoxical subreg.  Replace the subreg with a
6565      zero_extend to avoid the reload that would otherwise be required.  */
6566
6567   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6568       && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6569       && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6570       && SUBREG_BYTE (src) == 0
6571       && (GET_MODE_SIZE (GET_MODE (src))
6572           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6573       && MEM_P (SUBREG_REG (src)))
6574     {
6575       SUBST (SET_SRC (x),
6576              gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6577                             GET_MODE (src), SUBREG_REG (src)));
6578
6579       src = SET_SRC (x);
6580     }
6581 #endif
6582
6583   /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6584      are comparing an item known to be 0 or -1 against 0, use a logical
6585      operation instead. Check for one of the arms being an IOR of the other
6586      arm with some value.  We compute three terms to be IOR'ed together.  In
6587      practice, at most two will be nonzero.  Then we do the IOR's.  */
6588
6589   if (GET_CODE (dest) != PC
6590       && GET_CODE (src) == IF_THEN_ELSE
6591       && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6592       && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6593       && XEXP (XEXP (src, 0), 1) == const0_rtx
6594       && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6595 #ifdef HAVE_conditional_move
6596       && ! can_conditionally_move_p (GET_MODE (src))
6597 #endif
6598       && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6599                                GET_MODE (XEXP (XEXP (src, 0), 0)))
6600           == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
6601       && ! side_effects_p (src))
6602     {
6603       rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6604                       ? XEXP (src, 1) : XEXP (src, 2));
6605       rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6606                    ? XEXP (src, 2) : XEXP (src, 1));
6607       rtx term1 = const0_rtx, term2, term3;
6608
6609       if (GET_CODE (true_rtx) == IOR
6610           && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6611         term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6612       else if (GET_CODE (true_rtx) == IOR
6613                && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6614         term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6615       else if (GET_CODE (false_rtx) == IOR
6616                && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6617         term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6618       else if (GET_CODE (false_rtx) == IOR
6619                && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6620         term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6621
6622       term2 = simplify_gen_binary (AND, GET_MODE (src),
6623                                    XEXP (XEXP (src, 0), 0), true_rtx);
6624       term3 = simplify_gen_binary (AND, GET_MODE (src),
6625                                    simplify_gen_unary (NOT, GET_MODE (src),
6626                                                        XEXP (XEXP (src, 0), 0),
6627                                                        GET_MODE (src)),
6628                                    false_rtx);
6629
6630       SUBST (SET_SRC (x),
6631              simplify_gen_binary (IOR, GET_MODE (src),
6632                                   simplify_gen_binary (IOR, GET_MODE (src),
6633                                                        term1, term2),
6634                                   term3));
6635
6636       src = SET_SRC (x);
6637     }
6638
6639   /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6640      whole thing fail.  */
6641   if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6642     return src;
6643   else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6644     return dest;
6645   else
6646     /* Convert this into a field assignment operation, if possible.  */
6647     return make_field_assignment (x);
6648 }
6649 \f
6650 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6651    result.  */
6652
6653 static rtx
6654 simplify_logical (rtx x)
6655 {
6656   enum machine_mode mode = GET_MODE (x);
6657   rtx op0 = XEXP (x, 0);
6658   rtx op1 = XEXP (x, 1);
6659
6660   switch (GET_CODE (x))
6661     {
6662     case AND:
6663       /* We can call simplify_and_const_int only if we don't lose
6664          any (sign) bits when converting INTVAL (op1) to
6665          "unsigned HOST_WIDE_INT".  */
6666       if (CONST_INT_P (op1)
6667           && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6668               || INTVAL (op1) > 0))
6669         {
6670           x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6671           if (GET_CODE (x) != AND)
6672             return x;
6673
6674           op0 = XEXP (x, 0);
6675           op1 = XEXP (x, 1);
6676         }
6677
6678       /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6679          apply the distributive law and then the inverse distributive
6680          law to see if things simplify.  */
6681       if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6682         {
6683           rtx result = distribute_and_simplify_rtx (x, 0);
6684           if (result)
6685             return result;
6686         }
6687       if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6688         {
6689           rtx result = distribute_and_simplify_rtx (x, 1);
6690           if (result)
6691             return result;
6692         }
6693       break;
6694
6695     case IOR:
6696       /* If we have (ior (and A B) C), apply the distributive law and then
6697          the inverse distributive law to see if things simplify.  */
6698
6699       if (GET_CODE (op0) == AND)
6700         {
6701           rtx result = distribute_and_simplify_rtx (x, 0);
6702           if (result)
6703             return result;
6704         }
6705
6706       if (GET_CODE (op1) == AND)
6707         {
6708           rtx result = distribute_and_simplify_rtx (x, 1);
6709           if (result)
6710             return result;
6711         }
6712       break;
6713
6714     default:
6715       gcc_unreachable ();
6716     }
6717
6718   return x;
6719 }
6720 \f
6721 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6722    operations" because they can be replaced with two more basic operations.
6723    ZERO_EXTEND is also considered "compound" because it can be replaced with
6724    an AND operation, which is simpler, though only one operation.
6725
6726    The function expand_compound_operation is called with an rtx expression
6727    and will convert it to the appropriate shifts and AND operations,
6728    simplifying at each stage.
6729
6730    The function make_compound_operation is called to convert an expression
6731    consisting of shifts and ANDs into the equivalent compound expression.
6732    It is the inverse of this function, loosely speaking.  */
6733
6734 static rtx
6735 expand_compound_operation (rtx x)
6736 {
6737   unsigned HOST_WIDE_INT pos = 0, len;
6738   int unsignedp = 0;
6739   unsigned int modewidth;
6740   rtx tem;
6741
6742   switch (GET_CODE (x))
6743     {
6744     case ZERO_EXTEND:
6745       unsignedp = 1;
6746     case SIGN_EXTEND:
6747       /* We can't necessarily use a const_int for a multiword mode;
6748          it depends on implicitly extending the value.
6749          Since we don't know the right way to extend it,
6750          we can't tell whether the implicit way is right.
6751
6752          Even for a mode that is no wider than a const_int,
6753          we can't win, because we need to sign extend one of its bits through
6754          the rest of it, and we don't know which bit.  */
6755       if (CONST_INT_P (XEXP (x, 0)))
6756         return x;
6757
6758       /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6759          (zero_extend:MODE FROM) or (sign_extend:MODE FROM).  It is for any MEM
6760          because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6761          reloaded. If not for that, MEM's would very rarely be safe.
6762
6763          Reject MODEs bigger than a word, because we might not be able
6764          to reference a two-register group starting with an arbitrary register
6765          (and currently gen_lowpart might crash for a SUBREG).  */
6766
6767       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6768         return x;
6769
6770       /* Reject MODEs that aren't scalar integers because turning vector
6771          or complex modes into shifts causes problems.  */
6772
6773       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6774         return x;
6775
6776       len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
6777       /* If the inner object has VOIDmode (the only way this can happen
6778          is if it is an ASM_OPERANDS), we can't do anything since we don't
6779          know how much masking to do.  */
6780       if (len == 0)
6781         return x;
6782
6783       break;
6784
6785     case ZERO_EXTRACT:
6786       unsignedp = 1;
6787
6788       /* ... fall through ...  */
6789
6790     case SIGN_EXTRACT:
6791       /* If the operand is a CLOBBER, just return it.  */
6792       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6793         return XEXP (x, 0);
6794
6795       if (!CONST_INT_P (XEXP (x, 1))
6796           || !CONST_INT_P (XEXP (x, 2))
6797           || GET_MODE (XEXP (x, 0)) == VOIDmode)
6798         return x;
6799
6800       /* Reject MODEs that aren't scalar integers because turning vector
6801          or complex modes into shifts causes problems.  */
6802
6803       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6804         return x;
6805
6806       len = INTVAL (XEXP (x, 1));
6807       pos = INTVAL (XEXP (x, 2));
6808
6809       /* This should stay within the object being extracted, fail otherwise.  */
6810       if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
6811         return x;
6812
6813       if (BITS_BIG_ENDIAN)
6814         pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
6815
6816       break;
6817
6818     default:
6819       return x;
6820     }
6821   /* Convert sign extension to zero extension, if we know that the high
6822      bit is not set, as this is easier to optimize.  It will be converted
6823      back to cheaper alternative in make_extraction.  */
6824   if (GET_CODE (x) == SIGN_EXTEND
6825       && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6826           && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6827                 & ~(((unsigned HOST_WIDE_INT)
6828                       GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6829                      >> 1))
6830                == 0)))
6831     {
6832       rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6833       rtx temp2 = expand_compound_operation (temp);
6834
6835       /* Make sure this is a profitable operation.  */
6836       if (rtx_cost (x, SET, optimize_this_for_speed_p)
6837           > rtx_cost (temp2, SET, optimize_this_for_speed_p))
6838        return temp2;
6839       else if (rtx_cost (x, SET, optimize_this_for_speed_p)
6840                > rtx_cost (temp, SET, optimize_this_for_speed_p))
6841        return temp;
6842       else
6843        return x;
6844     }
6845
6846   /* We can optimize some special cases of ZERO_EXTEND.  */
6847   if (GET_CODE (x) == ZERO_EXTEND)
6848     {
6849       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6850          know that the last value didn't have any inappropriate bits
6851          set.  */
6852       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6853           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6854           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6855           && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6856               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6857         return XEXP (XEXP (x, 0), 0);
6858
6859       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
6860       if (GET_CODE (XEXP (x, 0)) == SUBREG
6861           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6862           && subreg_lowpart_p (XEXP (x, 0))
6863           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6864           && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6865               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6866         return SUBREG_REG (XEXP (x, 0));
6867
6868       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6869          is a comparison and STORE_FLAG_VALUE permits.  This is like
6870          the first case, but it works even when GET_MODE (x) is larger
6871          than HOST_WIDE_INT.  */
6872       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6873           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6874           && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6875           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6876               <= HOST_BITS_PER_WIDE_INT)
6877           && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6878         return XEXP (XEXP (x, 0), 0);
6879
6880       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
6881       if (GET_CODE (XEXP (x, 0)) == SUBREG
6882           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6883           && subreg_lowpart_p (XEXP (x, 0))
6884           && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6885           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6886               <= HOST_BITS_PER_WIDE_INT)
6887           && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6888         return SUBREG_REG (XEXP (x, 0));
6889
6890     }
6891
6892   /* If we reach here, we want to return a pair of shifts.  The inner
6893      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
6894      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
6895      logical depending on the value of UNSIGNEDP.
6896
6897      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6898      converted into an AND of a shift.
6899
6900      We must check for the case where the left shift would have a negative
6901      count.  This can happen in a case like (x >> 31) & 255 on machines
6902      that can't shift by a constant.  On those machines, we would first
6903      combine the shift with the AND to produce a variable-position
6904      extraction.  Then the constant of 31 would be substituted in
6905      to produce such a position.  */
6906
6907   modewidth = GET_MODE_BITSIZE (GET_MODE (x));
6908   if (modewidth >= pos + len)
6909     {
6910       enum machine_mode mode = GET_MODE (x);
6911       tem = gen_lowpart (mode, XEXP (x, 0));
6912       if (!tem || GET_CODE (tem) == CLOBBER)
6913         return x;
6914       tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6915                                   tem, modewidth - pos - len);
6916       tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6917                                   mode, tem, modewidth - len);
6918     }
6919   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6920     tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6921                                   simplify_shift_const (NULL_RTX, LSHIFTRT,
6922                                                         GET_MODE (x),
6923                                                         XEXP (x, 0), pos),
6924                                   ((unsigned HOST_WIDE_INT) 1 << len) - 1);
6925   else
6926     /* Any other cases we can't handle.  */
6927     return x;
6928
6929   /* If we couldn't do this for some reason, return the original
6930      expression.  */
6931   if (GET_CODE (tem) == CLOBBER)
6932     return x;
6933
6934   return tem;
6935 }
6936 \f
6937 /* X is a SET which contains an assignment of one object into
6938    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6939    or certain SUBREGS). If possible, convert it into a series of
6940    logical operations.
6941
6942    We half-heartedly support variable positions, but do not at all
6943    support variable lengths.  */
6944
6945 static const_rtx
6946 expand_field_assignment (const_rtx x)
6947 {
6948   rtx inner;
6949   rtx pos;                      /* Always counts from low bit.  */
6950   int len;
6951   rtx mask, cleared, masked;
6952   enum machine_mode compute_mode;
6953
6954   /* Loop until we find something we can't simplify.  */
6955   while (1)
6956     {
6957       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6958           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6959         {
6960           inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6961           len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
6962           pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6963         }
6964       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6965                && CONST_INT_P (XEXP (SET_DEST (x), 1)))
6966         {
6967           inner = XEXP (SET_DEST (x), 0);
6968           len = INTVAL (XEXP (SET_DEST (x), 1));
6969           pos = XEXP (SET_DEST (x), 2);
6970
6971           /* A constant position should stay within the width of INNER.  */
6972           if (CONST_INT_P (pos)
6973               && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
6974             break;
6975
6976           if (BITS_BIG_ENDIAN)
6977             {
6978               if (CONST_INT_P (pos))
6979                 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
6980                                - INTVAL (pos));
6981               else if (GET_CODE (pos) == MINUS
6982                        && CONST_INT_P (XEXP (pos, 1))
6983                        && (INTVAL (XEXP (pos, 1))
6984                            == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
6985                 /* If position is ADJUST - X, new position is X.  */
6986                 pos = XEXP (pos, 0);
6987               else
6988                 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6989                                            GEN_INT (GET_MODE_BITSIZE (
6990                                                     GET_MODE (inner))
6991                                                     - len),
6992                                            pos);
6993             }
6994         }
6995
6996       /* A SUBREG between two modes that occupy the same numbers of words
6997          can be done by moving the SUBREG to the source.  */
6998       else if (GET_CODE (SET_DEST (x)) == SUBREG
6999                /* We need SUBREGs to compute nonzero_bits properly.  */
7000                && nonzero_sign_valid
7001                && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
7002                      + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
7003                    == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
7004                         + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
7005         {
7006           x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
7007                            gen_lowpart
7008                            (GET_MODE (SUBREG_REG (SET_DEST (x))),
7009                             SET_SRC (x)));
7010           continue;
7011         }
7012       else
7013         break;
7014
7015       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7016         inner = SUBREG_REG (inner);
7017
7018       compute_mode = GET_MODE (inner);
7019
7020       /* Don't attempt bitwise arithmetic on non scalar integer modes.  */
7021       if (! SCALAR_INT_MODE_P (compute_mode))
7022         {
7023           enum machine_mode imode;
7024
7025           /* Don't do anything for vector or complex integral types.  */
7026           if (! FLOAT_MODE_P (compute_mode))
7027             break;
7028
7029           /* Try to find an integral mode to pun with.  */
7030           imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
7031           if (imode == BLKmode)
7032             break;
7033
7034           compute_mode = imode;
7035           inner = gen_lowpart (imode, inner);
7036         }
7037
7038       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
7039       if (len >= HOST_BITS_PER_WIDE_INT)
7040         break;
7041
7042       /* Now compute the equivalent expression.  Make a copy of INNER
7043          for the SET_DEST in case it is a MEM into which we will substitute;
7044          we don't want shared RTL in that case.  */
7045       mask = GEN_INT (((unsigned HOST_WIDE_INT) 1 << len) - 1);
7046       cleared = simplify_gen_binary (AND, compute_mode,
7047                                      simplify_gen_unary (NOT, compute_mode,
7048                                        simplify_gen_binary (ASHIFT,
7049                                                             compute_mode,
7050                                                             mask, pos),
7051                                        compute_mode),
7052                                      inner);
7053       masked = simplify_gen_binary (ASHIFT, compute_mode,
7054                                     simplify_gen_binary (
7055                                       AND, compute_mode,
7056                                       gen_lowpart (compute_mode, SET_SRC (x)),
7057                                       mask),
7058                                     pos);
7059
7060       x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
7061                        simplify_gen_binary (IOR, compute_mode,
7062                                             cleared, masked));
7063     }
7064
7065   return x;
7066 }
7067 \f
7068 /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
7069    it is an RTX that represents a variable starting position; otherwise,
7070    POS is the (constant) starting bit position (counted from the LSB).
7071
7072    UNSIGNEDP is nonzero for an unsigned reference and zero for a
7073    signed reference.
7074
7075    IN_DEST is nonzero if this is a reference in the destination of a
7076    SET.  This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If nonzero,
7077    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7078    be used.
7079
7080    IN_COMPARE is nonzero if we are in a COMPARE.  This means that a
7081    ZERO_EXTRACT should be built even for bits starting at bit 0.
7082
7083    MODE is the desired mode of the result (if IN_DEST == 0).
7084
7085    The result is an RTX for the extraction or NULL_RTX if the target
7086    can't handle it.  */
7087
7088 static rtx
7089 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7090                  rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7091                  int in_dest, int in_compare)
7092 {
7093   /* This mode describes the size of the storage area
7094      to fetch the overall value from.  Within that, we
7095      ignore the POS lowest bits, etc.  */
7096   enum machine_mode is_mode = GET_MODE (inner);
7097   enum machine_mode inner_mode;
7098   enum machine_mode wanted_inner_mode;
7099   enum machine_mode wanted_inner_reg_mode = word_mode;
7100   enum machine_mode pos_mode = word_mode;
7101   enum machine_mode extraction_mode = word_mode;
7102   enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
7103   rtx new_rtx = 0;
7104   rtx orig_pos_rtx = pos_rtx;
7105   HOST_WIDE_INT orig_pos;
7106
7107   if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7108     {
7109       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7110          consider just the QI as the memory to extract from.
7111          The subreg adds or removes high bits; its mode is
7112          irrelevant to the meaning of this extraction,
7113          since POS and LEN count from the lsb.  */
7114       if (MEM_P (SUBREG_REG (inner)))
7115         is_mode = GET_MODE (SUBREG_REG (inner));
7116       inner = SUBREG_REG (inner);
7117     }
7118   else if (GET_CODE (inner) == ASHIFT
7119            && CONST_INT_P (XEXP (inner, 1))
7120            && pos_rtx == 0 && pos == 0
7121            && len > UINTVAL (XEXP (inner, 1)))
7122     {
7123       /* We're extracting the least significant bits of an rtx
7124          (ashift X (const_int C)), where LEN > C.  Extract the
7125          least significant (LEN - C) bits of X, giving an rtx
7126          whose mode is MODE, then shift it left C times.  */
7127       new_rtx = make_extraction (mode, XEXP (inner, 0),
7128                              0, 0, len - INTVAL (XEXP (inner, 1)),
7129                              unsignedp, in_dest, in_compare);
7130       if (new_rtx != 0)
7131         return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7132     }
7133
7134   inner_mode = GET_MODE (inner);
7135
7136   if (pos_rtx && CONST_INT_P (pos_rtx))
7137     pos = INTVAL (pos_rtx), pos_rtx = 0;
7138
7139   /* See if this can be done without an extraction.  We never can if the
7140      width of the field is not the same as that of some integer mode. For
7141      registers, we can only avoid the extraction if the position is at the
7142      low-order bit and this is either not in the destination or we have the
7143      appropriate STRICT_LOW_PART operation available.
7144
7145      For MEM, we can avoid an extract if the field starts on an appropriate
7146      boundary and we can change the mode of the memory reference.  */
7147
7148   if (tmode != BLKmode
7149       && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7150            && !MEM_P (inner)
7151            && (inner_mode == tmode
7152                || !REG_P (inner)
7153                || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
7154                                          GET_MODE_BITSIZE (inner_mode))
7155                || reg_truncated_to_mode (tmode, inner))
7156            && (! in_dest
7157                || (REG_P (inner)
7158                    && have_insn_for (STRICT_LOW_PART, tmode))))
7159           || (MEM_P (inner) && pos_rtx == 0
7160               && (pos
7161                   % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7162                      : BITS_PER_UNIT)) == 0
7163               /* We can't do this if we are widening INNER_MODE (it
7164                  may not be aligned, for one thing).  */
7165               && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
7166               && (inner_mode == tmode
7167                   || (! mode_dependent_address_p (XEXP (inner, 0))
7168                       && ! MEM_VOLATILE_P (inner))))))
7169     {
7170       /* If INNER is a MEM, make a new MEM that encompasses just the desired
7171          field.  If the original and current mode are the same, we need not
7172          adjust the offset.  Otherwise, we do if bytes big endian.
7173
7174          If INNER is not a MEM, get a piece consisting of just the field
7175          of interest (in this case POS % BITS_PER_WORD must be 0).  */
7176
7177       if (MEM_P (inner))
7178         {
7179           HOST_WIDE_INT offset;
7180
7181           /* POS counts from lsb, but make OFFSET count in memory order.  */
7182           if (BYTES_BIG_ENDIAN)
7183             offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
7184           else
7185             offset = pos / BITS_PER_UNIT;
7186
7187           new_rtx = adjust_address_nv (inner, tmode, offset);
7188         }
7189       else if (REG_P (inner))
7190         {
7191           if (tmode != inner_mode)
7192             {
7193               /* We can't call gen_lowpart in a DEST since we
7194                  always want a SUBREG (see below) and it would sometimes
7195                  return a new hard register.  */
7196               if (pos || in_dest)
7197                 {
7198                   HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7199
7200                   if (WORDS_BIG_ENDIAN
7201                       && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7202                     final_word = ((GET_MODE_SIZE (inner_mode)
7203                                    - GET_MODE_SIZE (tmode))
7204                                   / UNITS_PER_WORD) - final_word;
7205
7206                   final_word *= UNITS_PER_WORD;
7207                   if (BYTES_BIG_ENDIAN &&
7208                       GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7209                     final_word += (GET_MODE_SIZE (inner_mode)
7210                                    - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7211
7212                   /* Avoid creating invalid subregs, for example when
7213                      simplifying (x>>32)&255.  */
7214                   if (!validate_subreg (tmode, inner_mode, inner, final_word))
7215                     return NULL_RTX;
7216
7217                   new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7218                 }
7219               else
7220                 new_rtx = gen_lowpart (tmode, inner);
7221             }
7222           else
7223             new_rtx = inner;
7224         }
7225       else
7226         new_rtx = force_to_mode (inner, tmode,
7227                              len >= HOST_BITS_PER_WIDE_INT
7228                              ? ~(unsigned HOST_WIDE_INT) 0
7229                              : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7230                              0);
7231
7232       /* If this extraction is going into the destination of a SET,
7233          make a STRICT_LOW_PART unless we made a MEM.  */
7234
7235       if (in_dest)
7236         return (MEM_P (new_rtx) ? new_rtx
7237                 : (GET_CODE (new_rtx) != SUBREG
7238                    ? gen_rtx_CLOBBER (tmode, const0_rtx)
7239                    : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7240
7241       if (mode == tmode)
7242         return new_rtx;
7243
7244       if (CONST_INT_P (new_rtx)
7245           || GET_CODE (new_rtx) == CONST_DOUBLE)
7246         return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7247                                          mode, new_rtx, tmode);
7248
7249       /* If we know that no extraneous bits are set, and that the high
7250          bit is not set, convert the extraction to the cheaper of
7251          sign and zero extension, that are equivalent in these cases.  */
7252       if (flag_expensive_optimizations
7253           && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
7254               && ((nonzero_bits (new_rtx, tmode)
7255                    & ~(((unsigned HOST_WIDE_INT)
7256                         GET_MODE_MASK (tmode))
7257                        >> 1))
7258                   == 0)))
7259         {
7260           rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7261           rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7262
7263           /* Prefer ZERO_EXTENSION, since it gives more information to
7264              backends.  */
7265           if (rtx_cost (temp, SET, optimize_this_for_speed_p)
7266               <= rtx_cost (temp1, SET, optimize_this_for_speed_p))
7267             return temp;
7268           return temp1;
7269         }
7270
7271       /* Otherwise, sign- or zero-extend unless we already are in the
7272          proper mode.  */
7273
7274       return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7275                              mode, new_rtx));
7276     }
7277
7278   /* Unless this is a COMPARE or we have a funny memory reference,
7279      don't do anything with zero-extending field extracts starting at
7280      the low-order bit since they are simple AND operations.  */
7281   if (pos_rtx == 0 && pos == 0 && ! in_dest
7282       && ! in_compare && unsignedp)
7283     return 0;
7284
7285   /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7286      if the position is not a constant and the length is not 1.  In all
7287      other cases, we would only be going outside our object in cases when
7288      an original shift would have been undefined.  */
7289   if (MEM_P (inner)
7290       && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
7291           || (pos_rtx != 0 && len != 1)))
7292     return 0;
7293
7294   /* Get the mode to use should INNER not be a MEM, the mode for the position,
7295      and the mode for the result.  */
7296   if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
7297     {
7298       wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
7299       pos_mode = mode_for_extraction (EP_insv, 2);
7300       extraction_mode = mode_for_extraction (EP_insv, 3);
7301     }
7302
7303   if (! in_dest && unsignedp
7304       && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
7305     {
7306       wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
7307       pos_mode = mode_for_extraction (EP_extzv, 3);
7308       extraction_mode = mode_for_extraction (EP_extzv, 0);
7309     }
7310
7311   if (! in_dest && ! unsignedp
7312       && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
7313     {
7314       wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
7315       pos_mode = mode_for_extraction (EP_extv, 3);
7316       extraction_mode = mode_for_extraction (EP_extv, 0);
7317     }
7318
7319   /* Never narrow an object, since that might not be safe.  */
7320
7321   if (mode != VOIDmode
7322       && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7323     extraction_mode = mode;
7324
7325   if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
7326       && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
7327     pos_mode = GET_MODE (pos_rtx);
7328
7329   /* If this is not from memory, the desired mode is the preferred mode
7330      for an extraction pattern's first input operand, or word_mode if there
7331      is none.  */
7332   if (!MEM_P (inner))
7333     wanted_inner_mode = wanted_inner_reg_mode;
7334   else
7335     {
7336       /* Be careful not to go beyond the extracted object and maintain the
7337          natural alignment of the memory.  */
7338       wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7339       while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7340              > GET_MODE_BITSIZE (wanted_inner_mode))
7341         {
7342           wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7343           gcc_assert (wanted_inner_mode != VOIDmode);
7344         }
7345
7346       /* If we have to change the mode of memory and cannot, the desired mode
7347          is EXTRACTION_MODE.  */
7348       if (inner_mode != wanted_inner_mode
7349           && (mode_dependent_address_p (XEXP (inner, 0))
7350               || MEM_VOLATILE_P (inner)
7351               || pos_rtx))
7352         wanted_inner_mode = extraction_mode;
7353     }
7354
7355   orig_pos = pos;
7356
7357   if (BITS_BIG_ENDIAN)
7358     {
7359       /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7360          BITS_BIG_ENDIAN style.  If position is constant, compute new
7361          position.  Otherwise, build subtraction.
7362          Note that POS is relative to the mode of the original argument.
7363          If it's a MEM we need to recompute POS relative to that.
7364          However, if we're extracting from (or inserting into) a register,
7365          we want to recompute POS relative to wanted_inner_mode.  */
7366       int width = (MEM_P (inner)
7367                    ? GET_MODE_BITSIZE (is_mode)
7368                    : GET_MODE_BITSIZE (wanted_inner_mode));
7369
7370       if (pos_rtx == 0)
7371         pos = width - len - pos;
7372       else
7373         pos_rtx
7374           = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
7375       /* POS may be less than 0 now, but we check for that below.
7376          Note that it can only be less than 0 if !MEM_P (inner).  */
7377     }
7378
7379   /* If INNER has a wider mode, and this is a constant extraction, try to
7380      make it smaller and adjust the byte to point to the byte containing
7381      the value.  */
7382   if (wanted_inner_mode != VOIDmode
7383       && inner_mode != wanted_inner_mode
7384       && ! pos_rtx
7385       && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7386       && MEM_P (inner)
7387       && ! mode_dependent_address_p (XEXP (inner, 0))
7388       && ! MEM_VOLATILE_P (inner))
7389     {
7390       int offset = 0;
7391
7392       /* The computations below will be correct if the machine is big
7393          endian in both bits and bytes or little endian in bits and bytes.
7394          If it is mixed, we must adjust.  */
7395
7396       /* If bytes are big endian and we had a paradoxical SUBREG, we must
7397          adjust OFFSET to compensate.  */
7398       if (BYTES_BIG_ENDIAN
7399           && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7400         offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7401
7402       /* We can now move to the desired byte.  */
7403       offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7404                 * GET_MODE_SIZE (wanted_inner_mode);
7405       pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7406
7407       if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7408           && is_mode != wanted_inner_mode)
7409         offset = (GET_MODE_SIZE (is_mode)
7410                   - GET_MODE_SIZE (wanted_inner_mode) - offset);
7411
7412       inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7413     }
7414
7415   /* If INNER is not memory, get it into the proper mode.  If we are changing
7416      its mode, POS must be a constant and smaller than the size of the new
7417      mode.  */
7418   else if (!MEM_P (inner))
7419     {
7420       /* On the LHS, don't create paradoxical subregs implicitely truncating
7421          the register unless TRULY_NOOP_TRUNCATION.  */
7422       if (in_dest
7423           && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (inner)),
7424                                      GET_MODE_BITSIZE (wanted_inner_mode)))
7425         return NULL_RTX;
7426
7427       if (GET_MODE (inner) != wanted_inner_mode
7428           && (pos_rtx != 0
7429               || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7430         return NULL_RTX;
7431
7432       if (orig_pos < 0)
7433         return NULL_RTX;
7434
7435       inner = force_to_mode (inner, wanted_inner_mode,
7436                              pos_rtx
7437                              || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7438                              ? ~(unsigned HOST_WIDE_INT) 0
7439                              : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7440                                 << orig_pos),
7441                              0);
7442     }
7443
7444   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
7445      have to zero extend.  Otherwise, we can just use a SUBREG.  */
7446   if (pos_rtx != 0
7447       && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7448     {
7449       rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
7450
7451       /* If we know that no extraneous bits are set, and that the high
7452          bit is not set, convert extraction to cheaper one - either
7453          SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7454          cases.  */
7455       if (flag_expensive_optimizations
7456           && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
7457               && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7458                    & ~(((unsigned HOST_WIDE_INT)
7459                         GET_MODE_MASK (GET_MODE (pos_rtx)))
7460                        >> 1))
7461                   == 0)))
7462         {
7463           rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
7464
7465           /* Prefer ZERO_EXTENSION, since it gives more information to
7466              backends.  */
7467           if (rtx_cost (temp1, SET, optimize_this_for_speed_p)
7468               < rtx_cost (temp, SET, optimize_this_for_speed_p))
7469             temp = temp1;
7470         }
7471       pos_rtx = temp;
7472     }
7473   else if (pos_rtx != 0
7474            && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
7475     pos_rtx = gen_lowpart (pos_mode, pos_rtx);
7476
7477   /* Make POS_RTX unless we already have it and it is correct.  If we don't
7478      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7479      be a CONST_INT.  */
7480   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7481     pos_rtx = orig_pos_rtx;
7482
7483   else if (pos_rtx == 0)
7484     pos_rtx = GEN_INT (pos);
7485
7486   /* Make the required operation.  See if we can use existing rtx.  */
7487   new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7488                          extraction_mode, inner, GEN_INT (len), pos_rtx);
7489   if (! in_dest)
7490     new_rtx = gen_lowpart (mode, new_rtx);
7491
7492   return new_rtx;
7493 }
7494 \f
7495 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7496    with any other operations in X.  Return X without that shift if so.  */
7497
7498 static rtx
7499 extract_left_shift (rtx x, int count)
7500 {
7501   enum rtx_code code = GET_CODE (x);
7502   enum machine_mode mode = GET_MODE (x);
7503   rtx tem;
7504
7505   switch (code)
7506     {
7507     case ASHIFT:
7508       /* This is the shift itself.  If it is wide enough, we will return
7509          either the value being shifted if the shift count is equal to
7510          COUNT or a shift for the difference.  */
7511       if (CONST_INT_P (XEXP (x, 1))
7512           && INTVAL (XEXP (x, 1)) >= count)
7513         return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7514                                      INTVAL (XEXP (x, 1)) - count);
7515       break;
7516
7517     case NEG:  case NOT:
7518       if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7519         return simplify_gen_unary (code, mode, tem, mode);
7520
7521       break;
7522
7523     case PLUS:  case IOR:  case XOR:  case AND:
7524       /* If we can safely shift this constant and we find the inner shift,
7525          make a new operation.  */
7526       if (CONST_INT_P (XEXP (x, 1))
7527           && (UINTVAL (XEXP (x, 1))
7528               & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7529           && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7530         return simplify_gen_binary (code, mode, tem,
7531                                     GEN_INT (INTVAL (XEXP (x, 1)) >> count));
7532
7533       break;
7534
7535     default:
7536       break;
7537     }
7538
7539   return 0;
7540 }
7541 \f
7542 /* Look at the expression rooted at X.  Look for expressions
7543    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7544    Form these expressions.
7545
7546    Return the new rtx, usually just X.
7547
7548    Also, for machines like the VAX that don't have logical shift insns,
7549    try to convert logical to arithmetic shift operations in cases where
7550    they are equivalent.  This undoes the canonicalizations to logical
7551    shifts done elsewhere.
7552
7553    We try, as much as possible, to re-use rtl expressions to save memory.
7554
7555    IN_CODE says what kind of expression we are processing.  Normally, it is
7556    SET.  In a memory address (inside a MEM, PLUS or minus, the latter two
7557    being kludges), it is MEM.  When processing the arguments of a comparison
7558    or a COMPARE against zero, it is COMPARE.  */
7559
7560 static rtx
7561 make_compound_operation (rtx x, enum rtx_code in_code)
7562 {
7563   enum rtx_code code = GET_CODE (x);
7564   enum machine_mode mode = GET_MODE (x);
7565   int mode_width = GET_MODE_BITSIZE (mode);
7566   rtx rhs, lhs;
7567   enum rtx_code next_code;
7568   int i, j;
7569   rtx new_rtx = 0;
7570   rtx tem;
7571   const char *fmt;
7572
7573   /* Select the code to be used in recursive calls.  Once we are inside an
7574      address, we stay there.  If we have a comparison, set to COMPARE,
7575      but once inside, go back to our default of SET.  */
7576
7577   next_code = (code == MEM ? MEM
7578                : ((code == PLUS || code == MINUS)
7579                   && SCALAR_INT_MODE_P (mode)) ? MEM
7580                : ((code == COMPARE || COMPARISON_P (x))
7581                   && XEXP (x, 1) == const0_rtx) ? COMPARE
7582                : in_code == COMPARE ? SET : in_code);
7583
7584   /* Process depending on the code of this operation.  If NEW is set
7585      nonzero, it will be returned.  */
7586
7587   switch (code)
7588     {
7589     case ASHIFT:
7590       /* Convert shifts by constants into multiplications if inside
7591          an address.  */
7592       if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7593           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7594           && INTVAL (XEXP (x, 1)) >= 0
7595           && SCALAR_INT_MODE_P (mode))
7596         {
7597           HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7598           HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7599
7600           new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7601           if (GET_CODE (new_rtx) == NEG)
7602             {
7603               new_rtx = XEXP (new_rtx, 0);
7604               multval = -multval;
7605             }
7606           multval = trunc_int_for_mode (multval, mode);
7607           new_rtx = gen_rtx_MULT (mode, new_rtx, GEN_INT (multval));
7608         }
7609       break;
7610
7611     case PLUS:
7612       lhs = XEXP (x, 0);
7613       rhs = XEXP (x, 1);
7614       lhs = make_compound_operation (lhs, next_code);
7615       rhs = make_compound_operation (rhs, next_code);
7616       if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7617           && SCALAR_INT_MODE_P (mode))
7618         {
7619           tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7620                                      XEXP (lhs, 1));
7621           new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7622         }
7623       else if (GET_CODE (lhs) == MULT
7624                && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7625         {
7626           tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7627                                      simplify_gen_unary (NEG, mode,
7628                                                          XEXP (lhs, 1),
7629                                                          mode));
7630           new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7631         }
7632       else
7633         {
7634           SUBST (XEXP (x, 0), lhs);
7635           SUBST (XEXP (x, 1), rhs);
7636           goto maybe_swap;
7637         }
7638       x = gen_lowpart (mode, new_rtx);
7639       goto maybe_swap;
7640
7641     case MINUS:
7642       lhs = XEXP (x, 0);
7643       rhs = XEXP (x, 1);
7644       lhs = make_compound_operation (lhs, next_code);
7645       rhs = make_compound_operation (rhs, next_code);
7646       if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7647           && SCALAR_INT_MODE_P (mode))
7648         {
7649           tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7650                                      XEXP (rhs, 1));
7651           new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7652         }
7653       else if (GET_CODE (rhs) == MULT
7654                && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7655         {
7656           tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7657                                      simplify_gen_unary (NEG, mode,
7658                                                          XEXP (rhs, 1),
7659                                                          mode));
7660           new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7661         }
7662       else
7663         {
7664           SUBST (XEXP (x, 0), lhs);
7665           SUBST (XEXP (x, 1), rhs);
7666           return x;
7667         }
7668       return gen_lowpart (mode, new_rtx);
7669
7670     case AND:
7671       /* If the second operand is not a constant, we can't do anything
7672          with it.  */
7673       if (!CONST_INT_P (XEXP (x, 1)))
7674         break;
7675
7676       /* If the constant is a power of two minus one and the first operand
7677          is a logical right shift, make an extraction.  */
7678       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7679           && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7680         {
7681           new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7682           new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7683                                  0, in_code == COMPARE);
7684         }
7685
7686       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
7687       else if (GET_CODE (XEXP (x, 0)) == SUBREG
7688                && subreg_lowpart_p (XEXP (x, 0))
7689                && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7690                && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7691         {
7692           new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7693                                          next_code);
7694           new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7695                                  XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7696                                  0, in_code == COMPARE);
7697         }
7698       /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)).  */
7699       else if ((GET_CODE (XEXP (x, 0)) == XOR
7700                 || GET_CODE (XEXP (x, 0)) == IOR)
7701                && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7702                && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7703                && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7704         {
7705           /* Apply the distributive law, and then try to make extractions.  */
7706           new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7707                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7708                                              XEXP (x, 1)),
7709                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7710                                              XEXP (x, 1)));
7711           new_rtx = make_compound_operation (new_rtx, in_code);
7712         }
7713
7714       /* If we are have (and (rotate X C) M) and C is larger than the number
7715          of bits in M, this is an extraction.  */
7716
7717       else if (GET_CODE (XEXP (x, 0)) == ROTATE
7718                && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7719                && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7720                && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7721         {
7722           new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7723           new_rtx = make_extraction (mode, new_rtx,
7724                                  (GET_MODE_BITSIZE (mode)
7725                                   - INTVAL (XEXP (XEXP (x, 0), 1))),
7726                                  NULL_RTX, i, 1, 0, in_code == COMPARE);
7727         }
7728
7729       /* On machines without logical shifts, if the operand of the AND is
7730          a logical shift and our mask turns off all the propagated sign
7731          bits, we can replace the logical shift with an arithmetic shift.  */
7732       else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7733                && !have_insn_for (LSHIFTRT, mode)
7734                && have_insn_for (ASHIFTRT, mode)
7735                && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7736                && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7737                && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7738                && mode_width <= HOST_BITS_PER_WIDE_INT)
7739         {
7740           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7741
7742           mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7743           if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7744             SUBST (XEXP (x, 0),
7745                    gen_rtx_ASHIFTRT (mode,
7746                                      make_compound_operation
7747                                      (XEXP (XEXP (x, 0), 0), next_code),
7748                                      XEXP (XEXP (x, 0), 1)));
7749         }
7750
7751       /* If the constant is one less than a power of two, this might be
7752          representable by an extraction even if no shift is present.
7753          If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7754          we are in a COMPARE.  */
7755       else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7756         new_rtx = make_extraction (mode,
7757                                make_compound_operation (XEXP (x, 0),
7758                                                         next_code),
7759                                0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7760
7761       /* If we are in a comparison and this is an AND with a power of two,
7762          convert this into the appropriate bit extract.  */
7763       else if (in_code == COMPARE
7764                && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7765         new_rtx = make_extraction (mode,
7766                                make_compound_operation (XEXP (x, 0),
7767                                                         next_code),
7768                                i, NULL_RTX, 1, 1, 0, 1);
7769
7770       break;
7771
7772     case LSHIFTRT:
7773       /* If the sign bit is known to be zero, replace this with an
7774          arithmetic shift.  */
7775       if (have_insn_for (ASHIFTRT, mode)
7776           && ! have_insn_for (LSHIFTRT, mode)
7777           && mode_width <= HOST_BITS_PER_WIDE_INT
7778           && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7779         {
7780           new_rtx = gen_rtx_ASHIFTRT (mode,
7781                                   make_compound_operation (XEXP (x, 0),
7782                                                            next_code),
7783                                   XEXP (x, 1));
7784           break;
7785         }
7786
7787       /* ... fall through ...  */
7788
7789     case ASHIFTRT:
7790       lhs = XEXP (x, 0);
7791       rhs = XEXP (x, 1);
7792
7793       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7794          this is a SIGN_EXTRACT.  */
7795       if (CONST_INT_P (rhs)
7796           && GET_CODE (lhs) == ASHIFT
7797           && CONST_INT_P (XEXP (lhs, 1))
7798           && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7799           && INTVAL (rhs) < mode_width)
7800         {
7801           new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7802           new_rtx = make_extraction (mode, new_rtx,
7803                                  INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7804                                  NULL_RTX, mode_width - INTVAL (rhs),
7805                                  code == LSHIFTRT, 0, in_code == COMPARE);
7806           break;
7807         }
7808
7809       /* See if we have operations between an ASHIFTRT and an ASHIFT.
7810          If so, try to merge the shifts into a SIGN_EXTEND.  We could
7811          also do this for some cases of SIGN_EXTRACT, but it doesn't
7812          seem worth the effort; the case checked for occurs on Alpha.  */
7813
7814       if (!OBJECT_P (lhs)
7815           && ! (GET_CODE (lhs) == SUBREG
7816                 && (OBJECT_P (SUBREG_REG (lhs))))
7817           && CONST_INT_P (rhs)
7818           && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7819           && INTVAL (rhs) < mode_width
7820           && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7821         new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7822                                0, NULL_RTX, mode_width - INTVAL (rhs),
7823                                code == LSHIFTRT, 0, in_code == COMPARE);
7824
7825       break;
7826
7827     case SUBREG:
7828       /* Call ourselves recursively on the inner expression.  If we are
7829          narrowing the object and it has a different RTL code from
7830          what it originally did, do this SUBREG as a force_to_mode.  */
7831       {
7832         rtx inner = SUBREG_REG (x), simplified;
7833         
7834         tem = make_compound_operation (inner, in_code);
7835
7836         simplified
7837           = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
7838         if (simplified)
7839           tem = simplified;
7840
7841         if (GET_CODE (tem) != GET_CODE (inner)
7842             && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7843             && subreg_lowpart_p (x))
7844           {
7845             rtx newer
7846               = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
7847
7848             /* If we have something other than a SUBREG, we might have
7849                done an expansion, so rerun ourselves.  */
7850             if (GET_CODE (newer) != SUBREG)
7851               newer = make_compound_operation (newer, in_code);
7852
7853             /* force_to_mode can expand compounds.  If it just re-expanded the
7854                compound, use gen_lowpart to convert to the desired mode.  */
7855             if (rtx_equal_p (newer, x)
7856                 /* Likewise if it re-expanded the compound only partially.
7857                    This happens for SUBREG of ZERO_EXTRACT if they extract
7858                    the same number of bits.  */
7859                 || (GET_CODE (newer) == SUBREG
7860                     && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
7861                         || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
7862                     && GET_CODE (inner) == AND
7863                     && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
7864               return gen_lowpart (GET_MODE (x), tem);
7865
7866             return newer;
7867           }
7868
7869         if (simplified)
7870           return tem;
7871       }
7872       break;
7873
7874     default:
7875       break;
7876     }
7877
7878   if (new_rtx)
7879     {
7880       x = gen_lowpart (mode, new_rtx);
7881       code = GET_CODE (x);
7882     }
7883
7884   /* Now recursively process each operand of this operation.  We need to
7885      handle ZERO_EXTEND specially so that we don't lose track of the
7886      inner mode.  */
7887   if (GET_CODE (x) == ZERO_EXTEND)
7888     {
7889       new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7890       tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
7891                                             new_rtx, GET_MODE (XEXP (x, 0)));
7892       if (tem)
7893         return tem;
7894       SUBST (XEXP (x, 0), new_rtx);
7895       return x;
7896     }
7897
7898   fmt = GET_RTX_FORMAT (code);
7899   for (i = 0; i < GET_RTX_LENGTH (code); i++)
7900     if (fmt[i] == 'e')
7901       {
7902         new_rtx = make_compound_operation (XEXP (x, i), next_code);
7903         SUBST (XEXP (x, i), new_rtx);
7904       }
7905     else if (fmt[i] == 'E')
7906       for (j = 0; j < XVECLEN (x, i); j++)
7907         {
7908           new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
7909           SUBST (XVECEXP (x, i, j), new_rtx);
7910         }
7911
7912  maybe_swap:
7913   /* If this is a commutative operation, the changes to the operands
7914      may have made it noncanonical.  */
7915   if (COMMUTATIVE_ARITH_P (x)
7916       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7917     {
7918       tem = XEXP (x, 0);
7919       SUBST (XEXP (x, 0), XEXP (x, 1));
7920       SUBST (XEXP (x, 1), tem);
7921     }
7922
7923   return x;
7924 }
7925 \f
7926 /* Given M see if it is a value that would select a field of bits
7927    within an item, but not the entire word.  Return -1 if not.
7928    Otherwise, return the starting position of the field, where 0 is the
7929    low-order bit.
7930
7931    *PLEN is set to the length of the field.  */
7932
7933 static int
7934 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7935 {
7936   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
7937   int pos = m ? ctz_hwi (m) : -1;
7938   int len = 0;
7939
7940   if (pos >= 0)
7941     /* Now shift off the low-order zero bits and see if we have a
7942        power of two minus 1.  */
7943     len = exact_log2 ((m >> pos) + 1);
7944
7945   if (len <= 0)
7946     pos = -1;
7947
7948   *plen = len;
7949   return pos;
7950 }
7951 \f
7952 /* If X refers to a register that equals REG in value, replace these
7953    references with REG.  */
7954 static rtx
7955 canon_reg_for_combine (rtx x, rtx reg)
7956 {
7957   rtx op0, op1, op2;
7958   const char *fmt;
7959   int i;
7960   bool copied;
7961
7962   enum rtx_code code = GET_CODE (x);
7963   switch (GET_RTX_CLASS (code))
7964     {
7965     case RTX_UNARY:
7966       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7967       if (op0 != XEXP (x, 0))
7968         return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7969                                    GET_MODE (reg));
7970       break;
7971
7972     case RTX_BIN_ARITH:
7973     case RTX_COMM_ARITH:
7974       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7975       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7976       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7977         return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7978       break;
7979
7980     case RTX_COMPARE:
7981     case RTX_COMM_COMPARE:
7982       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7983       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7984       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7985         return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7986                                         GET_MODE (op0), op0, op1);
7987       break;
7988
7989     case RTX_TERNARY:
7990     case RTX_BITFIELD_OPS:
7991       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7992       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7993       op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7994       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7995         return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7996                                      GET_MODE (op0), op0, op1, op2);
7997
7998     case RTX_OBJ:
7999       if (REG_P (x))
8000         {
8001           if (rtx_equal_p (get_last_value (reg), x)
8002               || rtx_equal_p (reg, get_last_value (x)))
8003             return reg;
8004           else
8005             break;
8006         }
8007
8008       /* fall through */
8009
8010     default:
8011       fmt = GET_RTX_FORMAT (code);
8012       copied = false;
8013       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8014         if (fmt[i] == 'e')
8015           {
8016             rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8017             if (op != XEXP (x, i))
8018               {
8019                 if (!copied)
8020                   {
8021                     copied = true;
8022                     x = copy_rtx (x);
8023                   }
8024                 XEXP (x, i) = op;
8025               }
8026           }
8027         else if (fmt[i] == 'E')
8028           {
8029             int j;
8030             for (j = 0; j < XVECLEN (x, i); j++)
8031               {
8032                 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8033                 if (op != XVECEXP (x, i, j))
8034                   {
8035                     if (!copied)
8036                       {
8037                         copied = true;
8038                         x = copy_rtx (x);
8039                       }
8040                     XVECEXP (x, i, j) = op;
8041                   }
8042               }
8043           }
8044
8045       break;
8046     }
8047
8048   return x;
8049 }
8050
8051 /* Return X converted to MODE.  If the value is already truncated to
8052    MODE we can just return a subreg even though in the general case we
8053    would need an explicit truncation.  */
8054
8055 static rtx
8056 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
8057 {
8058   if (!CONST_INT_P (x)
8059       && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8060       && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
8061                                  GET_MODE_BITSIZE (GET_MODE (x)))
8062       && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8063     {
8064       /* Bit-cast X into an integer mode.  */
8065       if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8066         x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
8067       x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
8068                               x, GET_MODE (x));
8069     }
8070
8071   return gen_lowpart (mode, x);
8072 }
8073
8074 /* See if X can be simplified knowing that we will only refer to it in
8075    MODE and will only refer to those bits that are nonzero in MASK.
8076    If other bits are being computed or if masking operations are done
8077    that select a superset of the bits in MASK, they can sometimes be
8078    ignored.
8079
8080    Return a possibly simplified expression, but always convert X to
8081    MODE.  If X is a CONST_INT, AND the CONST_INT with MASK.
8082
8083    If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8084    are all off in X.  This is used when X will be complemented, by either
8085    NOT, NEG, or XOR.  */
8086
8087 static rtx
8088 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
8089                int just_select)
8090 {
8091   enum rtx_code code = GET_CODE (x);
8092   int next_select = just_select || code == XOR || code == NOT || code == NEG;
8093   enum machine_mode op_mode;
8094   unsigned HOST_WIDE_INT fuller_mask, nonzero;
8095   rtx op0, op1, temp;
8096
8097   /* If this is a CALL or ASM_OPERANDS, don't do anything.  Some of the
8098      code below will do the wrong thing since the mode of such an
8099      expression is VOIDmode.
8100
8101      Also do nothing if X is a CLOBBER; this can happen if X was
8102      the return value from a call to gen_lowpart.  */
8103   if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8104     return x;
8105
8106   /* We want to perform the operation is its present mode unless we know
8107      that the operation is valid in MODE, in which case we do the operation
8108      in MODE.  */
8109   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8110               && have_insn_for (code, mode))
8111              ? mode : GET_MODE (x));
8112
8113   /* It is not valid to do a right-shift in a narrower mode
8114      than the one it came in with.  */
8115   if ((code == LSHIFTRT || code == ASHIFTRT)
8116       && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
8117     op_mode = GET_MODE (x);
8118
8119   /* Truncate MASK to fit OP_MODE.  */
8120   if (op_mode)
8121     mask &= GET_MODE_MASK (op_mode);
8122
8123   /* When we have an arithmetic operation, or a shift whose count we
8124      do not know, we need to assume that all bits up to the highest-order
8125      bit in MASK will be needed.  This is how we form such a mask.  */
8126   if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8127     fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8128   else
8129     fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8130                    - 1);
8131
8132   /* Determine what bits of X are guaranteed to be (non)zero.  */
8133   nonzero = nonzero_bits (x, mode);
8134
8135   /* If none of the bits in X are needed, return a zero.  */
8136   if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8137     x = const0_rtx;
8138
8139   /* If X is a CONST_INT, return a new one.  Do this here since the
8140      test below will fail.  */
8141   if (CONST_INT_P (x))
8142     {
8143       if (SCALAR_INT_MODE_P (mode))
8144         return gen_int_mode (INTVAL (x) & mask, mode);
8145       else
8146         {
8147           x = GEN_INT (INTVAL (x) & mask);
8148           return gen_lowpart_common (mode, x);
8149         }
8150     }
8151
8152   /* If X is narrower than MODE and we want all the bits in X's mode, just
8153      get X in the proper mode.  */
8154   if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8155       && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8156     return gen_lowpart (mode, x);
8157
8158   /* We can ignore the effect of a SUBREG if it narrows the mode or
8159      if the constant masks to zero all the bits the mode doesn't have.  */
8160   if (GET_CODE (x) == SUBREG
8161       && subreg_lowpart_p (x)
8162       && ((GET_MODE_SIZE (GET_MODE (x))
8163            < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8164           || (0 == (mask
8165                     & GET_MODE_MASK (GET_MODE (x))
8166                     & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8167     return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8168
8169   /* The arithmetic simplifications here only work for scalar integer modes.  */
8170   if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8171     return gen_lowpart_or_truncate (mode, x);
8172
8173   switch (code)
8174     {
8175     case CLOBBER:
8176       /* If X is a (clobber (const_int)), return it since we know we are
8177          generating something that won't match.  */
8178       return x;
8179
8180     case SIGN_EXTEND:
8181     case ZERO_EXTEND:
8182     case ZERO_EXTRACT:
8183     case SIGN_EXTRACT:
8184       x = expand_compound_operation (x);
8185       if (GET_CODE (x) != code)
8186         return force_to_mode (x, mode, mask, next_select);
8187       break;
8188
8189     case TRUNCATE:
8190       /* Similarly for a truncate.  */
8191       return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8192
8193     case AND:
8194       /* If this is an AND with a constant, convert it into an AND
8195          whose constant is the AND of that constant with MASK.  If it
8196          remains an AND of MASK, delete it since it is redundant.  */
8197
8198       if (CONST_INT_P (XEXP (x, 1)))
8199         {
8200           x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8201                                       mask & INTVAL (XEXP (x, 1)));
8202
8203           /* If X is still an AND, see if it is an AND with a mask that
8204              is just some low-order bits.  If so, and it is MASK, we don't
8205              need it.  */
8206
8207           if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8208               && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8209                   == mask))
8210             x = XEXP (x, 0);
8211
8212           /* If it remains an AND, try making another AND with the bits
8213              in the mode mask that aren't in MASK turned on.  If the
8214              constant in the AND is wide enough, this might make a
8215              cheaper constant.  */
8216
8217           if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8218               && GET_MODE_MASK (GET_MODE (x)) != mask
8219               && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
8220             {
8221               unsigned HOST_WIDE_INT cval
8222                 = UINTVAL (XEXP (x, 1))
8223                   | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8224               int width = GET_MODE_BITSIZE (GET_MODE (x));
8225               rtx y;
8226
8227               /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
8228                  number, sign extend it.  */
8229               if (width > 0 && width < HOST_BITS_PER_WIDE_INT
8230                   && (cval & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8231                 cval |= (unsigned HOST_WIDE_INT) -1 << width;
8232
8233               y = simplify_gen_binary (AND, GET_MODE (x),
8234                                        XEXP (x, 0), GEN_INT (cval));
8235               if (rtx_cost (y, SET, optimize_this_for_speed_p)
8236                   < rtx_cost (x, SET, optimize_this_for_speed_p))
8237                 x = y;
8238             }
8239
8240           break;
8241         }
8242
8243       goto binop;
8244
8245     case PLUS:
8246       /* In (and (plus FOO C1) M), if M is a mask that just turns off
8247          low-order bits (as in an alignment operation) and FOO is already
8248          aligned to that boundary, mask C1 to that boundary as well.
8249          This may eliminate that PLUS and, later, the AND.  */
8250
8251       {
8252         unsigned int width = GET_MODE_BITSIZE (mode);
8253         unsigned HOST_WIDE_INT smask = mask;
8254
8255         /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8256            number, sign extend it.  */
8257
8258         if (width < HOST_BITS_PER_WIDE_INT
8259             && (smask & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8260           smask |= (unsigned HOST_WIDE_INT) (-1) << width;
8261
8262         if (CONST_INT_P (XEXP (x, 1))
8263             && exact_log2 (- smask) >= 0
8264             && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8265             && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8266           return force_to_mode (plus_constant (XEXP (x, 0),
8267                                                (INTVAL (XEXP (x, 1)) & smask)),
8268                                 mode, smask, next_select);
8269       }
8270
8271       /* ... fall through ...  */
8272
8273     case MULT:
8274       /* For PLUS, MINUS and MULT, we need any bits less significant than the
8275          most significant bit in MASK since carries from those bits will
8276          affect the bits we are interested in.  */
8277       mask = fuller_mask;
8278       goto binop;
8279
8280     case MINUS:
8281       /* If X is (minus C Y) where C's least set bit is larger than any bit
8282          in the mask, then we may replace with (neg Y).  */
8283       if (CONST_INT_P (XEXP (x, 0))
8284           && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
8285                                         & -INTVAL (XEXP (x, 0))))
8286               > mask))
8287         {
8288           x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8289                                   GET_MODE (x));
8290           return force_to_mode (x, mode, mask, next_select);
8291         }
8292
8293       /* Similarly, if C contains every bit in the fuller_mask, then we may
8294          replace with (not Y).  */
8295       if (CONST_INT_P (XEXP (x, 0))
8296           && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8297         {
8298           x = simplify_gen_unary (NOT, GET_MODE (x),
8299                                   XEXP (x, 1), GET_MODE (x));
8300           return force_to_mode (x, mode, mask, next_select);
8301         }
8302
8303       mask = fuller_mask;
8304       goto binop;
8305
8306     case IOR:
8307     case XOR:
8308       /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8309          LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8310          operation which may be a bitfield extraction.  Ensure that the
8311          constant we form is not wider than the mode of X.  */
8312
8313       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8314           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8315           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8316           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8317           && CONST_INT_P (XEXP (x, 1))
8318           && ((INTVAL (XEXP (XEXP (x, 0), 1))
8319                + floor_log2 (INTVAL (XEXP (x, 1))))
8320               < GET_MODE_BITSIZE (GET_MODE (x)))
8321           && (UINTVAL (XEXP (x, 1))
8322               & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8323         {
8324           temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
8325                           << INTVAL (XEXP (XEXP (x, 0), 1)));
8326           temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8327                                       XEXP (XEXP (x, 0), 0), temp);
8328           x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8329                                    XEXP (XEXP (x, 0), 1));
8330           return force_to_mode (x, mode, mask, next_select);
8331         }
8332
8333     binop:
8334       /* For most binary operations, just propagate into the operation and
8335          change the mode if we have an operation of that mode.  */
8336
8337       op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8338       op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8339
8340       /* If we ended up truncating both operands, truncate the result of the
8341          operation instead.  */
8342       if (GET_CODE (op0) == TRUNCATE
8343           && GET_CODE (op1) == TRUNCATE)
8344         {
8345           op0 = XEXP (op0, 0);
8346           op1 = XEXP (op1, 0);
8347         }
8348
8349       op0 = gen_lowpart_or_truncate (op_mode, op0);
8350       op1 = gen_lowpart_or_truncate (op_mode, op1);
8351
8352       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8353         x = simplify_gen_binary (code, op_mode, op0, op1);
8354       break;
8355
8356     case ASHIFT:
8357       /* For left shifts, do the same, but just for the first operand.
8358          However, we cannot do anything with shifts where we cannot
8359          guarantee that the counts are smaller than the size of the mode
8360          because such a count will have a different meaning in a
8361          wider mode.  */
8362
8363       if (! (CONST_INT_P (XEXP (x, 1))
8364              && INTVAL (XEXP (x, 1)) >= 0
8365              && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
8366           && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8367                 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8368                     < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
8369         break;
8370
8371       /* If the shift count is a constant and we can do arithmetic in
8372          the mode of the shift, refine which bits we need.  Otherwise, use the
8373          conservative form of the mask.  */
8374       if (CONST_INT_P (XEXP (x, 1))
8375           && INTVAL (XEXP (x, 1)) >= 0
8376           && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
8377           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
8378         mask >>= INTVAL (XEXP (x, 1));
8379       else
8380         mask = fuller_mask;
8381
8382       op0 = gen_lowpart_or_truncate (op_mode,
8383                                      force_to_mode (XEXP (x, 0), op_mode,
8384                                                     mask, next_select));
8385
8386       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8387         x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8388       break;
8389
8390     case LSHIFTRT:
8391       /* Here we can only do something if the shift count is a constant,
8392          this shift constant is valid for the host, and we can do arithmetic
8393          in OP_MODE.  */
8394
8395       if (CONST_INT_P (XEXP (x, 1))
8396           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8397           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
8398         {
8399           rtx inner = XEXP (x, 0);
8400           unsigned HOST_WIDE_INT inner_mask;
8401
8402           /* Select the mask of the bits we need for the shift operand.  */
8403           inner_mask = mask << INTVAL (XEXP (x, 1));
8404
8405           /* We can only change the mode of the shift if we can do arithmetic
8406              in the mode of the shift and INNER_MASK is no wider than the
8407              width of X's mode.  */
8408           if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8409             op_mode = GET_MODE (x);
8410
8411           inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8412
8413           if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8414             x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8415         }
8416
8417       /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8418          shift and AND produces only copies of the sign bit (C2 is one less
8419          than a power of two), we can do this with just a shift.  */
8420
8421       if (GET_CODE (x) == LSHIFTRT
8422           && CONST_INT_P (XEXP (x, 1))
8423           /* The shift puts one of the sign bit copies in the least significant
8424              bit.  */
8425           && ((INTVAL (XEXP (x, 1))
8426                + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8427               >= GET_MODE_BITSIZE (GET_MODE (x)))
8428           && exact_log2 (mask + 1) >= 0
8429           /* Number of bits left after the shift must be more than the mask
8430              needs.  */
8431           && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8432               <= GET_MODE_BITSIZE (GET_MODE (x)))
8433           /* Must be more sign bit copies than the mask needs.  */
8434           && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8435               >= exact_log2 (mask + 1)))
8436         x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8437                                  GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
8438                                           - exact_log2 (mask + 1)));
8439
8440       goto shiftrt;
8441
8442     case ASHIFTRT:
8443       /* If we are just looking for the sign bit, we don't need this shift at
8444          all, even if it has a variable count.  */
8445       if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
8446           && (mask == ((unsigned HOST_WIDE_INT) 1
8447                        << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8448         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8449
8450       /* If this is a shift by a constant, get a mask that contains those bits
8451          that are not copies of the sign bit.  We then have two cases:  If
8452          MASK only includes those bits, this can be a logical shift, which may
8453          allow simplifications.  If MASK is a single-bit field not within
8454          those bits, we are requesting a copy of the sign bit and hence can
8455          shift the sign bit to the appropriate location.  */
8456
8457       if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8458           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8459         {
8460           int i;
8461
8462           /* If the considered data is wider than HOST_WIDE_INT, we can't
8463              represent a mask for all its bits in a single scalar.
8464              But we only care about the lower bits, so calculate these.  */
8465
8466           if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8467             {
8468               nonzero = ~(unsigned HOST_WIDE_INT) 0;
8469
8470               /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8471                  is the number of bits a full-width mask would have set.
8472                  We need only shift if these are fewer than nonzero can
8473                  hold.  If not, we must keep all bits set in nonzero.  */
8474
8475               if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8476                   < HOST_BITS_PER_WIDE_INT)
8477                 nonzero >>= INTVAL (XEXP (x, 1))
8478                             + HOST_BITS_PER_WIDE_INT
8479                             - GET_MODE_BITSIZE (GET_MODE (x)) ;
8480             }
8481           else
8482             {
8483               nonzero = GET_MODE_MASK (GET_MODE (x));
8484               nonzero >>= INTVAL (XEXP (x, 1));
8485             }
8486
8487           if ((mask & ~nonzero) == 0)
8488             {
8489               x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8490                                         XEXP (x, 0), INTVAL (XEXP (x, 1)));
8491               if (GET_CODE (x) != ASHIFTRT)
8492                 return force_to_mode (x, mode, mask, next_select);
8493             }
8494
8495           else if ((i = exact_log2 (mask)) >= 0)
8496             {
8497               x = simplify_shift_const
8498                   (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8499                    GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
8500
8501               if (GET_CODE (x) != ASHIFTRT)
8502                 return force_to_mode (x, mode, mask, next_select);
8503             }
8504         }
8505
8506       /* If MASK is 1, convert this to an LSHIFTRT.  This can be done
8507          even if the shift count isn't a constant.  */
8508       if (mask == 1)
8509         x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8510                                  XEXP (x, 0), XEXP (x, 1));
8511
8512     shiftrt:
8513
8514       /* If this is a zero- or sign-extension operation that just affects bits
8515          we don't care about, remove it.  Be sure the call above returned
8516          something that is still a shift.  */
8517
8518       if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8519           && CONST_INT_P (XEXP (x, 1))
8520           && INTVAL (XEXP (x, 1)) >= 0
8521           && (INTVAL (XEXP (x, 1))
8522               <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
8523           && GET_CODE (XEXP (x, 0)) == ASHIFT
8524           && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8525         return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8526                               next_select);
8527
8528       break;
8529
8530     case ROTATE:
8531     case ROTATERT:
8532       /* If the shift count is constant and we can do computations
8533          in the mode of X, compute where the bits we care about are.
8534          Otherwise, we can't do anything.  Don't change the mode of
8535          the shift or propagate MODE into the shift, though.  */
8536       if (CONST_INT_P (XEXP (x, 1))
8537           && INTVAL (XEXP (x, 1)) >= 0)
8538         {
8539           temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8540                                             GET_MODE (x), GEN_INT (mask),
8541                                             XEXP (x, 1));
8542           if (temp && CONST_INT_P (temp))
8543             SUBST (XEXP (x, 0),
8544                    force_to_mode (XEXP (x, 0), GET_MODE (x),
8545                                   INTVAL (temp), next_select));
8546         }
8547       break;
8548
8549     case NEG:
8550       /* If we just want the low-order bit, the NEG isn't needed since it
8551          won't change the low-order bit.  */
8552       if (mask == 1)
8553         return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8554
8555       /* We need any bits less significant than the most significant bit in
8556          MASK since carries from those bits will affect the bits we are
8557          interested in.  */
8558       mask = fuller_mask;
8559       goto unop;
8560
8561     case NOT:
8562       /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8563          same as the XOR case above.  Ensure that the constant we form is not
8564          wider than the mode of X.  */
8565
8566       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8567           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8568           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8569           && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8570               < GET_MODE_BITSIZE (GET_MODE (x)))
8571           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8572         {
8573           temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8574                                GET_MODE (x));
8575           temp = simplify_gen_binary (XOR, GET_MODE (x),
8576                                       XEXP (XEXP (x, 0), 0), temp);
8577           x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8578                                    temp, XEXP (XEXP (x, 0), 1));
8579
8580           return force_to_mode (x, mode, mask, next_select);
8581         }
8582
8583       /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8584          use the full mask inside the NOT.  */
8585       mask = fuller_mask;
8586
8587     unop:
8588       op0 = gen_lowpart_or_truncate (op_mode,
8589                                      force_to_mode (XEXP (x, 0), mode, mask,
8590                                                     next_select));
8591       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8592         x = simplify_gen_unary (code, op_mode, op0, op_mode);
8593       break;
8594
8595     case NE:
8596       /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8597          in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8598          which is equal to STORE_FLAG_VALUE.  */
8599       if ((mask & ~STORE_FLAG_VALUE) == 0
8600           && XEXP (x, 1) == const0_rtx
8601           && GET_MODE (XEXP (x, 0)) == mode
8602           && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8603           && (nonzero_bits (XEXP (x, 0), mode)
8604               == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8605         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8606
8607       break;
8608
8609     case IF_THEN_ELSE:
8610       /* We have no way of knowing if the IF_THEN_ELSE can itself be
8611          written in a narrower mode.  We play it safe and do not do so.  */
8612
8613       SUBST (XEXP (x, 1),
8614              gen_lowpart_or_truncate (GET_MODE (x),
8615                                       force_to_mode (XEXP (x, 1), mode,
8616                                                      mask, next_select)));
8617       SUBST (XEXP (x, 2),
8618              gen_lowpart_or_truncate (GET_MODE (x),
8619                                       force_to_mode (XEXP (x, 2), mode,
8620                                                      mask, next_select)));
8621       break;
8622
8623     default:
8624       break;
8625     }
8626
8627   /* Ensure we return a value of the proper mode.  */
8628   return gen_lowpart_or_truncate (mode, x);
8629 }
8630 \f
8631 /* Return nonzero if X is an expression that has one of two values depending on
8632    whether some other value is zero or nonzero.  In that case, we return the
8633    value that is being tested, *PTRUE is set to the value if the rtx being
8634    returned has a nonzero value, and *PFALSE is set to the other alternative.
8635
8636    If we return zero, we set *PTRUE and *PFALSE to X.  */
8637
8638 static rtx
8639 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8640 {
8641   enum machine_mode mode = GET_MODE (x);
8642   enum rtx_code code = GET_CODE (x);
8643   rtx cond0, cond1, true0, true1, false0, false1;
8644   unsigned HOST_WIDE_INT nz;
8645
8646   /* If we are comparing a value against zero, we are done.  */
8647   if ((code == NE || code == EQ)
8648       && XEXP (x, 1) == const0_rtx)
8649     {
8650       *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8651       *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8652       return XEXP (x, 0);
8653     }
8654
8655   /* If this is a unary operation whose operand has one of two values, apply
8656      our opcode to compute those values.  */
8657   else if (UNARY_P (x)
8658            && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8659     {
8660       *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8661       *pfalse = simplify_gen_unary (code, mode, false0,
8662                                     GET_MODE (XEXP (x, 0)));
8663       return cond0;
8664     }
8665
8666   /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8667      make can't possibly match and would suppress other optimizations.  */
8668   else if (code == COMPARE)
8669     ;
8670
8671   /* If this is a binary operation, see if either side has only one of two
8672      values.  If either one does or if both do and they are conditional on
8673      the same value, compute the new true and false values.  */
8674   else if (BINARY_P (x))
8675     {
8676       cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8677       cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8678
8679       if ((cond0 != 0 || cond1 != 0)
8680           && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8681         {
8682           /* If if_then_else_cond returned zero, then true/false are the
8683              same rtl.  We must copy one of them to prevent invalid rtl
8684              sharing.  */
8685           if (cond0 == 0)
8686             true0 = copy_rtx (true0);
8687           else if (cond1 == 0)
8688             true1 = copy_rtx (true1);
8689
8690           if (COMPARISON_P (x))
8691             {
8692               *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8693                                                 true0, true1);
8694               *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8695                                                  false0, false1);
8696              }
8697           else
8698             {
8699               *ptrue = simplify_gen_binary (code, mode, true0, true1);
8700               *pfalse = simplify_gen_binary (code, mode, false0, false1);
8701             }
8702
8703           return cond0 ? cond0 : cond1;
8704         }
8705
8706       /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8707          operands is zero when the other is nonzero, and vice-versa,
8708          and STORE_FLAG_VALUE is 1 or -1.  */
8709
8710       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8711           && (code == PLUS || code == IOR || code == XOR || code == MINUS
8712               || code == UMAX)
8713           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8714         {
8715           rtx op0 = XEXP (XEXP (x, 0), 1);
8716           rtx op1 = XEXP (XEXP (x, 1), 1);
8717
8718           cond0 = XEXP (XEXP (x, 0), 0);
8719           cond1 = XEXP (XEXP (x, 1), 0);
8720
8721           if (COMPARISON_P (cond0)
8722               && COMPARISON_P (cond1)
8723               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8724                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8725                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8726                   || ((swap_condition (GET_CODE (cond0))
8727                        == reversed_comparison_code (cond1, NULL))
8728                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8729                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8730               && ! side_effects_p (x))
8731             {
8732               *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8733               *pfalse = simplify_gen_binary (MULT, mode,
8734                                              (code == MINUS
8735                                               ? simplify_gen_unary (NEG, mode,
8736                                                                     op1, mode)
8737                                               : op1),
8738                                               const_true_rtx);
8739               return cond0;
8740             }
8741         }
8742
8743       /* Similarly for MULT, AND and UMIN, except that for these the result
8744          is always zero.  */
8745       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8746           && (code == MULT || code == AND || code == UMIN)
8747           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8748         {
8749           cond0 = XEXP (XEXP (x, 0), 0);
8750           cond1 = XEXP (XEXP (x, 1), 0);
8751
8752           if (COMPARISON_P (cond0)
8753               && COMPARISON_P (cond1)
8754               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8755                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8756                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8757                   || ((swap_condition (GET_CODE (cond0))
8758                        == reversed_comparison_code (cond1, NULL))
8759                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8760                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8761               && ! side_effects_p (x))
8762             {
8763               *ptrue = *pfalse = const0_rtx;
8764               return cond0;
8765             }
8766         }
8767     }
8768
8769   else if (code == IF_THEN_ELSE)
8770     {
8771       /* If we have IF_THEN_ELSE already, extract the condition and
8772          canonicalize it if it is NE or EQ.  */
8773       cond0 = XEXP (x, 0);
8774       *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8775       if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8776         return XEXP (cond0, 0);
8777       else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8778         {
8779           *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8780           return XEXP (cond0, 0);
8781         }
8782       else
8783         return cond0;
8784     }
8785
8786   /* If X is a SUBREG, we can narrow both the true and false values
8787      if the inner expression, if there is a condition.  */
8788   else if (code == SUBREG
8789            && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8790                                                &true0, &false0)))
8791     {
8792       true0 = simplify_gen_subreg (mode, true0,
8793                                    GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8794       false0 = simplify_gen_subreg (mode, false0,
8795                                     GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8796       if (true0 && false0)
8797         {
8798           *ptrue = true0;
8799           *pfalse = false0;
8800           return cond0;
8801         }
8802     }
8803
8804   /* If X is a constant, this isn't special and will cause confusions
8805      if we treat it as such.  Likewise if it is equivalent to a constant.  */
8806   else if (CONSTANT_P (x)
8807            || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8808     ;
8809
8810   /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8811      will be least confusing to the rest of the compiler.  */
8812   else if (mode == BImode)
8813     {
8814       *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8815       return x;
8816     }
8817
8818   /* If X is known to be either 0 or -1, those are the true and
8819      false values when testing X.  */
8820   else if (x == constm1_rtx || x == const0_rtx
8821            || (mode != VOIDmode
8822                && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
8823     {
8824       *ptrue = constm1_rtx, *pfalse = const0_rtx;
8825       return x;
8826     }
8827
8828   /* Likewise for 0 or a single bit.  */
8829   else if (SCALAR_INT_MODE_P (mode)
8830            && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8831            && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
8832     {
8833       *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
8834       return x;
8835     }
8836
8837   /* Otherwise fail; show no condition with true and false values the same.  */
8838   *ptrue = *pfalse = x;
8839   return 0;
8840 }
8841 \f
8842 /* Return the value of expression X given the fact that condition COND
8843    is known to be true when applied to REG as its first operand and VAL
8844    as its second.  X is known to not be shared and so can be modified in
8845    place.
8846
8847    We only handle the simplest cases, and specifically those cases that
8848    arise with IF_THEN_ELSE expressions.  */
8849
8850 static rtx
8851 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8852 {
8853   enum rtx_code code = GET_CODE (x);
8854   rtx temp;
8855   const char *fmt;
8856   int i, j;
8857
8858   if (side_effects_p (x))
8859     return x;
8860
8861   /* If either operand of the condition is a floating point value,
8862      then we have to avoid collapsing an EQ comparison.  */
8863   if (cond == EQ
8864       && rtx_equal_p (x, reg)
8865       && ! FLOAT_MODE_P (GET_MODE (x))
8866       && ! FLOAT_MODE_P (GET_MODE (val)))
8867     return val;
8868
8869   if (cond == UNEQ && rtx_equal_p (x, reg))
8870     return val;
8871
8872   /* If X is (abs REG) and we know something about REG's relationship
8873      with zero, we may be able to simplify this.  */
8874
8875   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8876     switch (cond)
8877       {
8878       case GE:  case GT:  case EQ:
8879         return XEXP (x, 0);
8880       case LT:  case LE:
8881         return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8882                                    XEXP (x, 0),
8883                                    GET_MODE (XEXP (x, 0)));
8884       default:
8885         break;
8886       }
8887
8888   /* The only other cases we handle are MIN, MAX, and comparisons if the
8889      operands are the same as REG and VAL.  */
8890
8891   else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8892     {
8893       if (rtx_equal_p (XEXP (x, 0), val))
8894         cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8895
8896       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8897         {
8898           if (COMPARISON_P (x))
8899             {
8900               if (comparison_dominates_p (cond, code))
8901                 return const_true_rtx;
8902
8903               code = reversed_comparison_code (x, NULL);
8904               if (code != UNKNOWN
8905                   && comparison_dominates_p (cond, code))
8906                 return const0_rtx;
8907               else
8908                 return x;
8909             }
8910           else if (code == SMAX || code == SMIN
8911                    || code == UMIN || code == UMAX)
8912             {
8913               int unsignedp = (code == UMIN || code == UMAX);
8914
8915               /* Do not reverse the condition when it is NE or EQ.
8916                  This is because we cannot conclude anything about
8917                  the value of 'SMAX (x, y)' when x is not equal to y,
8918                  but we can when x equals y.  */
8919               if ((code == SMAX || code == UMAX)
8920                   && ! (cond == EQ || cond == NE))
8921                 cond = reverse_condition (cond);
8922
8923               switch (cond)
8924                 {
8925                 case GE:   case GT:
8926                   return unsignedp ? x : XEXP (x, 1);
8927                 case LE:   case LT:
8928                   return unsignedp ? x : XEXP (x, 0);
8929                 case GEU:  case GTU:
8930                   return unsignedp ? XEXP (x, 1) : x;
8931                 case LEU:  case LTU:
8932                   return unsignedp ? XEXP (x, 0) : x;
8933                 default:
8934                   break;
8935                 }
8936             }
8937         }
8938     }
8939   else if (code == SUBREG)
8940     {
8941       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8942       rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
8943
8944       if (SUBREG_REG (x) != r)
8945         {
8946           /* We must simplify subreg here, before we lose track of the
8947              original inner_mode.  */
8948           new_rtx = simplify_subreg (GET_MODE (x), r,
8949                                  inner_mode, SUBREG_BYTE (x));
8950           if (new_rtx)
8951             return new_rtx;
8952           else
8953             SUBST (SUBREG_REG (x), r);
8954         }
8955
8956       return x;
8957     }
8958   /* We don't have to handle SIGN_EXTEND here, because even in the
8959      case of replacing something with a modeless CONST_INT, a
8960      CONST_INT is already (supposed to be) a valid sign extension for
8961      its narrower mode, which implies it's already properly
8962      sign-extended for the wider mode.  Now, for ZERO_EXTEND, the
8963      story is different.  */
8964   else if (code == ZERO_EXTEND)
8965     {
8966       enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8967       rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
8968
8969       if (XEXP (x, 0) != r)
8970         {
8971           /* We must simplify the zero_extend here, before we lose
8972              track of the original inner_mode.  */
8973           new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8974                                           r, inner_mode);
8975           if (new_rtx)
8976             return new_rtx;
8977           else
8978             SUBST (XEXP (x, 0), r);
8979         }
8980
8981       return x;
8982     }
8983
8984   fmt = GET_RTX_FORMAT (code);
8985   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8986     {
8987       if (fmt[i] == 'e')
8988         SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8989       else if (fmt[i] == 'E')
8990         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8991           SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8992                                                 cond, reg, val));
8993     }
8994
8995   return x;
8996 }
8997 \f
8998 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8999    assignment as a field assignment.  */
9000
9001 static int
9002 rtx_equal_for_field_assignment_p (rtx x, rtx y)
9003 {
9004   if (x == y || rtx_equal_p (x, y))
9005     return 1;
9006
9007   if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9008     return 0;
9009
9010   /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9011      Note that all SUBREGs of MEM are paradoxical; otherwise they
9012      would have been rewritten.  */
9013   if (MEM_P (x) && GET_CODE (y) == SUBREG
9014       && MEM_P (SUBREG_REG (y))
9015       && rtx_equal_p (SUBREG_REG (y),
9016                       gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9017     return 1;
9018
9019   if (MEM_P (y) && GET_CODE (x) == SUBREG
9020       && MEM_P (SUBREG_REG (x))
9021       && rtx_equal_p (SUBREG_REG (x),
9022                       gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9023     return 1;
9024
9025   /* We used to see if get_last_value of X and Y were the same but that's
9026      not correct.  In one direction, we'll cause the assignment to have
9027      the wrong destination and in the case, we'll import a register into this
9028      insn that might have already have been dead.   So fail if none of the
9029      above cases are true.  */
9030   return 0;
9031 }
9032 \f
9033 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9034    Return that assignment if so.
9035
9036    We only handle the most common cases.  */
9037
9038 static rtx
9039 make_field_assignment (rtx x)
9040 {
9041   rtx dest = SET_DEST (x);
9042   rtx src = SET_SRC (x);
9043   rtx assign;
9044   rtx rhs, lhs;
9045   HOST_WIDE_INT c1;
9046   HOST_WIDE_INT pos;
9047   unsigned HOST_WIDE_INT len;
9048   rtx other;
9049   enum machine_mode mode;
9050
9051   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9052      a clear of a one-bit field.  We will have changed it to
9053      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
9054      for a SUBREG.  */
9055
9056   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9057       && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9058       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9059       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9060     {
9061       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9062                                 1, 1, 1, 0);
9063       if (assign != 0)
9064         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9065       return x;
9066     }
9067
9068   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9069       && subreg_lowpart_p (XEXP (src, 0))
9070       && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
9071           < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
9072       && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9073       && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9074       && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9075       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9076     {
9077       assign = make_extraction (VOIDmode, dest, 0,
9078                                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9079                                 1, 1, 1, 0);
9080       if (assign != 0)
9081         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9082       return x;
9083     }
9084
9085   /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9086      one-bit field.  */
9087   if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9088       && XEXP (XEXP (src, 0), 0) == const1_rtx
9089       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9090     {
9091       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9092                                 1, 1, 1, 0);
9093       if (assign != 0)
9094         return gen_rtx_SET (VOIDmode, assign, const1_rtx);
9095       return x;
9096     }
9097
9098   /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9099      SRC is an AND with all bits of that field set, then we can discard
9100      the AND.  */
9101   if (GET_CODE (dest) == ZERO_EXTRACT
9102       && CONST_INT_P (XEXP (dest, 1))
9103       && GET_CODE (src) == AND
9104       && CONST_INT_P (XEXP (src, 1)))
9105     {
9106       HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9107       unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9108       unsigned HOST_WIDE_INT ze_mask;
9109
9110       if (width >= HOST_BITS_PER_WIDE_INT)
9111         ze_mask = -1;
9112       else
9113         ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9114
9115       /* Complete overlap.  We can remove the source AND.  */
9116       if ((and_mask & ze_mask) == ze_mask)
9117         return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
9118
9119       /* Partial overlap.  We can reduce the source AND.  */
9120       if ((and_mask & ze_mask) != and_mask)
9121         {
9122           mode = GET_MODE (src);
9123           src = gen_rtx_AND (mode, XEXP (src, 0),
9124                              gen_int_mode (and_mask & ze_mask, mode));
9125           return gen_rtx_SET (VOIDmode, dest, src);
9126         }
9127     }
9128
9129   /* The other case we handle is assignments into a constant-position
9130      field.  They look like (ior/xor (and DEST C1) OTHER).  If C1 represents
9131      a mask that has all one bits except for a group of zero bits and
9132      OTHER is known to have zeros where C1 has ones, this is such an
9133      assignment.  Compute the position and length from C1.  Shift OTHER
9134      to the appropriate position, force it to the required mode, and
9135      make the extraction.  Check for the AND in both operands.  */
9136
9137   if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9138     return x;
9139
9140   rhs = expand_compound_operation (XEXP (src, 0));
9141   lhs = expand_compound_operation (XEXP (src, 1));
9142
9143   if (GET_CODE (rhs) == AND
9144       && CONST_INT_P (XEXP (rhs, 1))
9145       && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9146     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9147   else if (GET_CODE (lhs) == AND
9148            && CONST_INT_P (XEXP (lhs, 1))
9149            && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9150     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9151   else
9152     return x;
9153
9154   pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9155   if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
9156       || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9157       || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9158     return x;
9159
9160   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9161   if (assign == 0)
9162     return x;
9163
9164   /* The mode to use for the source is the mode of the assignment, or of
9165      what is inside a possible STRICT_LOW_PART.  */
9166   mode = (GET_CODE (assign) == STRICT_LOW_PART
9167           ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9168
9169   /* Shift OTHER right POS places and make it the source, restricting it
9170      to the proper length and mode.  */
9171
9172   src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9173                                                      GET_MODE (src),
9174                                                      other, pos),
9175                                dest);
9176   src = force_to_mode (src, mode,
9177                        GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
9178                        ? ~(unsigned HOST_WIDE_INT) 0
9179                        : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9180                        0);
9181
9182   /* If SRC is masked by an AND that does not make a difference in
9183      the value being stored, strip it.  */
9184   if (GET_CODE (assign) == ZERO_EXTRACT
9185       && CONST_INT_P (XEXP (assign, 1))
9186       && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9187       && GET_CODE (src) == AND
9188       && CONST_INT_P (XEXP (src, 1))
9189       && UINTVAL (XEXP (src, 1))
9190          == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9191     src = XEXP (src, 0);
9192
9193   return gen_rtx_SET (VOIDmode, assign, src);
9194 }
9195 \f
9196 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9197    if so.  */
9198
9199 static rtx
9200 apply_distributive_law (rtx x)
9201 {
9202   enum rtx_code code = GET_CODE (x);
9203   enum rtx_code inner_code;
9204   rtx lhs, rhs, other;
9205   rtx tem;
9206
9207   /* Distributivity is not true for floating point as it can change the
9208      value.  So we don't do it unless -funsafe-math-optimizations.  */
9209   if (FLOAT_MODE_P (GET_MODE (x))
9210       && ! flag_unsafe_math_optimizations)
9211     return x;
9212
9213   /* The outer operation can only be one of the following:  */
9214   if (code != IOR && code != AND && code != XOR
9215       && code != PLUS && code != MINUS)
9216     return x;
9217
9218   lhs = XEXP (x, 0);
9219   rhs = XEXP (x, 1);
9220
9221   /* If either operand is a primitive we can't do anything, so get out
9222      fast.  */
9223   if (OBJECT_P (lhs) || OBJECT_P (rhs))
9224     return x;
9225
9226   lhs = expand_compound_operation (lhs);
9227   rhs = expand_compound_operation (rhs);
9228   inner_code = GET_CODE (lhs);
9229   if (inner_code != GET_CODE (rhs))
9230     return x;
9231
9232   /* See if the inner and outer operations distribute.  */
9233   switch (inner_code)
9234     {
9235     case LSHIFTRT:
9236     case ASHIFTRT:
9237     case AND:
9238     case IOR:
9239       /* These all distribute except over PLUS.  */
9240       if (code == PLUS || code == MINUS)
9241         return x;
9242       break;
9243
9244     case MULT:
9245       if (code != PLUS && code != MINUS)
9246         return x;
9247       break;
9248
9249     case ASHIFT:
9250       /* This is also a multiply, so it distributes over everything.  */
9251       break;
9252
9253     case SUBREG:
9254       /* Non-paradoxical SUBREGs distributes over all operations,
9255          provided the inner modes and byte offsets are the same, this
9256          is an extraction of a low-order part, we don't convert an fp
9257          operation to int or vice versa, this is not a vector mode,
9258          and we would not be converting a single-word operation into a
9259          multi-word operation.  The latter test is not required, but
9260          it prevents generating unneeded multi-word operations.  Some
9261          of the previous tests are redundant given the latter test,
9262          but are retained because they are required for correctness.
9263
9264          We produce the result slightly differently in this case.  */
9265
9266       if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
9267           || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
9268           || ! subreg_lowpart_p (lhs)
9269           || (GET_MODE_CLASS (GET_MODE (lhs))
9270               != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
9271           || (GET_MODE_SIZE (GET_MODE (lhs))
9272               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
9273           || VECTOR_MODE_P (GET_MODE (lhs))
9274           || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD
9275           /* Result might need to be truncated.  Don't change mode if
9276              explicit truncation is needed.  */
9277           || !TRULY_NOOP_TRUNCATION
9278                (GET_MODE_BITSIZE (GET_MODE (x)),
9279                 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (lhs)))))
9280         return x;
9281
9282       tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
9283                                  SUBREG_REG (lhs), SUBREG_REG (rhs));
9284       return gen_lowpart (GET_MODE (x), tem);
9285
9286     default:
9287       return x;
9288     }
9289
9290   /* Set LHS and RHS to the inner operands (A and B in the example
9291      above) and set OTHER to the common operand (C in the example).
9292      There is only one way to do this unless the inner operation is
9293      commutative.  */
9294   if (COMMUTATIVE_ARITH_P (lhs)
9295       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9296     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9297   else if (COMMUTATIVE_ARITH_P (lhs)
9298            && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9299     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9300   else if (COMMUTATIVE_ARITH_P (lhs)
9301            && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9302     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9303   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9304     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9305   else
9306     return x;
9307
9308   /* Form the new inner operation, seeing if it simplifies first.  */
9309   tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9310
9311   /* There is one exception to the general way of distributing:
9312      (a | c) ^ (b | c) -> (a ^ b) & ~c  */
9313   if (code == XOR && inner_code == IOR)
9314     {
9315       inner_code = AND;
9316       other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9317     }
9318
9319   /* We may be able to continuing distributing the result, so call
9320      ourselves recursively on the inner operation before forming the
9321      outer operation, which we return.  */
9322   return simplify_gen_binary (inner_code, GET_MODE (x),
9323                               apply_distributive_law (tem), other);
9324 }
9325
9326 /* See if X is of the form (* (+ A B) C), and if so convert to
9327    (+ (* A C) (* B C)) and try to simplify.
9328
9329    Most of the time, this results in no change.  However, if some of
9330    the operands are the same or inverses of each other, simplifications
9331    will result.
9332
9333    For example, (and (ior A B) (not B)) can occur as the result of
9334    expanding a bit field assignment.  When we apply the distributive
9335    law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9336    which then simplifies to (and (A (not B))).
9337
9338    Note that no checks happen on the validity of applying the inverse
9339    distributive law.  This is pointless since we can do it in the
9340    few places where this routine is called.
9341
9342    N is the index of the term that is decomposed (the arithmetic operation,
9343    i.e. (+ A B) in the first example above).  !N is the index of the term that
9344    is distributed, i.e. of C in the first example above.  */
9345 static rtx
9346 distribute_and_simplify_rtx (rtx x, int n)
9347 {
9348   enum machine_mode mode;
9349   enum rtx_code outer_code, inner_code;
9350   rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9351
9352   /* Distributivity is not true for floating point as it can change the
9353      value.  So we don't do it unless -funsafe-math-optimizations.  */
9354   if (FLOAT_MODE_P (GET_MODE (x))
9355       && ! flag_unsafe_math_optimizations)
9356     return NULL_RTX;
9357
9358   decomposed = XEXP (x, n);
9359   if (!ARITHMETIC_P (decomposed))
9360     return NULL_RTX;
9361
9362   mode = GET_MODE (x);
9363   outer_code = GET_CODE (x);
9364   distributed = XEXP (x, !n);
9365
9366   inner_code = GET_CODE (decomposed);
9367   inner_op0 = XEXP (decomposed, 0);
9368   inner_op1 = XEXP (decomposed, 1);
9369
9370   /* Special case (and (xor B C) (not A)), which is equivalent to
9371      (xor (ior A B) (ior A C))  */
9372   if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9373     {
9374       distributed = XEXP (distributed, 0);
9375       outer_code = IOR;
9376     }
9377
9378   if (n == 0)
9379     {
9380       /* Distribute the second term.  */
9381       new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9382       new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9383     }
9384   else
9385     {
9386       /* Distribute the first term.  */
9387       new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9388       new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9389     }
9390
9391   tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9392                                                      new_op0, new_op1));
9393   if (GET_CODE (tmp) != outer_code
9394       && rtx_cost (tmp, SET, optimize_this_for_speed_p)
9395          < rtx_cost (x, SET, optimize_this_for_speed_p))
9396     return tmp;
9397
9398   return NULL_RTX;
9399 }
9400 \f
9401 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9402    in MODE.  Return an equivalent form, if different from (and VAROP
9403    (const_int CONSTOP)).  Otherwise, return NULL_RTX.  */
9404
9405 static rtx
9406 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
9407                           unsigned HOST_WIDE_INT constop)
9408 {
9409   unsigned HOST_WIDE_INT nonzero;
9410   unsigned HOST_WIDE_INT orig_constop;
9411   rtx orig_varop;
9412   int i;
9413
9414   orig_varop = varop;
9415   orig_constop = constop;
9416   if (GET_CODE (varop) == CLOBBER)
9417     return NULL_RTX;
9418
9419   /* Simplify VAROP knowing that we will be only looking at some of the
9420      bits in it.
9421
9422      Note by passing in CONSTOP, we guarantee that the bits not set in
9423      CONSTOP are not significant and will never be examined.  We must
9424      ensure that is the case by explicitly masking out those bits
9425      before returning.  */
9426   varop = force_to_mode (varop, mode, constop, 0);
9427
9428   /* If VAROP is a CLOBBER, we will fail so return it.  */
9429   if (GET_CODE (varop) == CLOBBER)
9430     return varop;
9431
9432   /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9433      to VAROP and return the new constant.  */
9434   if (CONST_INT_P (varop))
9435     return gen_int_mode (INTVAL (varop) & constop, mode);
9436
9437   /* See what bits may be nonzero in VAROP.  Unlike the general case of
9438      a call to nonzero_bits, here we don't care about bits outside
9439      MODE.  */
9440
9441   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9442
9443   /* Turn off all bits in the constant that are known to already be zero.
9444      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9445      which is tested below.  */
9446
9447   constop &= nonzero;
9448
9449   /* If we don't have any bits left, return zero.  */
9450   if (constop == 0)
9451     return const0_rtx;
9452
9453   /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9454      a power of two, we can replace this with an ASHIFT.  */
9455   if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9456       && (i = exact_log2 (constop)) >= 0)
9457     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9458
9459   /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9460      or XOR, then try to apply the distributive law.  This may eliminate
9461      operations if either branch can be simplified because of the AND.
9462      It may also make some cases more complex, but those cases probably
9463      won't match a pattern either with or without this.  */
9464
9465   if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9466     return
9467       gen_lowpart
9468         (mode,
9469          apply_distributive_law
9470          (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9471                                simplify_and_const_int (NULL_RTX,
9472                                                        GET_MODE (varop),
9473                                                        XEXP (varop, 0),
9474                                                        constop),
9475                                simplify_and_const_int (NULL_RTX,
9476                                                        GET_MODE (varop),
9477                                                        XEXP (varop, 1),
9478                                                        constop))));
9479
9480   /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9481      the AND and see if one of the operands simplifies to zero.  If so, we
9482      may eliminate it.  */
9483
9484   if (GET_CODE (varop) == PLUS
9485       && exact_log2 (constop + 1) >= 0)
9486     {
9487       rtx o0, o1;
9488
9489       o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9490       o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9491       if (o0 == const0_rtx)
9492         return o1;
9493       if (o1 == const0_rtx)
9494         return o0;
9495     }
9496
9497   /* Make a SUBREG if necessary.  If we can't make it, fail.  */
9498   varop = gen_lowpart (mode, varop);
9499   if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9500     return NULL_RTX;
9501
9502   /* If we are only masking insignificant bits, return VAROP.  */
9503   if (constop == nonzero)
9504     return varop;
9505
9506   if (varop == orig_varop && constop == orig_constop)
9507     return NULL_RTX;
9508
9509   /* Otherwise, return an AND.  */
9510   return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9511 }
9512
9513
9514 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9515    in MODE.
9516
9517    Return an equivalent form, if different from X.  Otherwise, return X.  If
9518    X is zero, we are to always construct the equivalent form.  */
9519
9520 static rtx
9521 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
9522                         unsigned HOST_WIDE_INT constop)
9523 {
9524   rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9525   if (tem)
9526     return tem;
9527
9528   if (!x)
9529     x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9530                              gen_int_mode (constop, mode));
9531   if (GET_MODE (x) != mode)
9532     x = gen_lowpart (mode, x);
9533   return x;
9534 }
9535 \f
9536 /* Given a REG, X, compute which bits in X can be nonzero.
9537    We don't care about bits outside of those defined in MODE.
9538
9539    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9540    a shift, AND, or zero_extract, we can do better.  */
9541
9542 static rtx
9543 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
9544                               const_rtx known_x ATTRIBUTE_UNUSED,
9545                               enum machine_mode known_mode ATTRIBUTE_UNUSED,
9546                               unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9547                               unsigned HOST_WIDE_INT *nonzero)
9548 {
9549   rtx tem;
9550   reg_stat_type *rsp;
9551
9552   /* If X is a register whose nonzero bits value is current, use it.
9553      Otherwise, if X is a register whose value we can find, use that
9554      value.  Otherwise, use the previously-computed global nonzero bits
9555      for this register.  */
9556
9557   rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
9558   if (rsp->last_set_value != 0
9559       && (rsp->last_set_mode == mode
9560           || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9561               && GET_MODE_CLASS (mode) == MODE_INT))
9562       && ((rsp->last_set_label >= label_tick_ebb_start
9563            && rsp->last_set_label < label_tick)
9564           || (rsp->last_set_label == label_tick
9565               && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9566           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9567               && REG_N_SETS (REGNO (x)) == 1
9568               && !REGNO_REG_SET_P
9569                   (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
9570     {
9571       *nonzero &= rsp->last_set_nonzero_bits;
9572       return NULL;
9573     }
9574
9575   tem = get_last_value (x);
9576
9577   if (tem)
9578     {
9579 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9580       /* If X is narrower than MODE and TEM is a non-negative
9581          constant that would appear negative in the mode of X,
9582          sign-extend it for use in reg_nonzero_bits because some
9583          machines (maybe most) will actually do the sign-extension
9584          and this is the conservative approach.
9585
9586          ??? For 2.5, try to tighten up the MD files in this regard
9587          instead of this kludge.  */
9588
9589       if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
9590           && CONST_INT_P (tem)
9591           && INTVAL (tem) > 0
9592           && 0 != (UINTVAL (tem)
9593                    & ((unsigned HOST_WIDE_INT) 1
9594                       << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
9595         tem = GEN_INT (UINTVAL (tem)
9596                        | ((unsigned HOST_WIDE_INT) (-1)
9597                           << GET_MODE_BITSIZE (GET_MODE (x))));
9598 #endif
9599       return tem;
9600     }
9601   else if (nonzero_sign_valid && rsp->nonzero_bits)
9602     {
9603       unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9604
9605       if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
9606         /* We don't know anything about the upper bits.  */
9607         mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9608       *nonzero &= mask;
9609     }
9610
9611   return NULL;
9612 }
9613
9614 /* Return the number of bits at the high-order end of X that are known to
9615    be equal to the sign bit.  X will be used in mode MODE; if MODE is
9616    VOIDmode, X will be used in its own mode.  The returned value  will always
9617    be between 1 and the number of bits in MODE.  */
9618
9619 static rtx
9620 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
9621                                      const_rtx known_x ATTRIBUTE_UNUSED,
9622                                      enum machine_mode known_mode
9623                                      ATTRIBUTE_UNUSED,
9624                                      unsigned int known_ret ATTRIBUTE_UNUSED,
9625                                      unsigned int *result)
9626 {
9627   rtx tem;
9628   reg_stat_type *rsp;
9629
9630   rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
9631   if (rsp->last_set_value != 0
9632       && rsp->last_set_mode == mode
9633       && ((rsp->last_set_label >= label_tick_ebb_start
9634            && rsp->last_set_label < label_tick)
9635           || (rsp->last_set_label == label_tick
9636               && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9637           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9638               && REG_N_SETS (REGNO (x)) == 1
9639               && !REGNO_REG_SET_P
9640                   (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
9641     {
9642       *result = rsp->last_set_sign_bit_copies;
9643       return NULL;
9644     }
9645
9646   tem = get_last_value (x);
9647   if (tem != 0)
9648     return tem;
9649
9650   if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9651       && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
9652     *result = rsp->sign_bit_copies;
9653
9654   return NULL;
9655 }
9656 \f
9657 /* Return the number of "extended" bits there are in X, when interpreted
9658    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
9659    unsigned quantities, this is the number of high-order zero bits.
9660    For signed quantities, this is the number of copies of the sign bit
9661    minus 1.  In both case, this function returns the number of "spare"
9662    bits.  For example, if two quantities for which this function returns
9663    at least 1 are added, the addition is known not to overflow.
9664
9665    This function will always return 0 unless called during combine, which
9666    implies that it must be called from a define_split.  */
9667
9668 unsigned int
9669 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
9670 {
9671   if (nonzero_sign_valid == 0)
9672     return 0;
9673
9674   return (unsignedp
9675           ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9676              ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
9677                                - floor_log2 (nonzero_bits (x, mode)))
9678              : 0)
9679           : num_sign_bit_copies (x, mode) - 1);
9680 }
9681 \f
9682 /* This function is called from `simplify_shift_const' to merge two
9683    outer operations.  Specifically, we have already found that we need
9684    to perform operation *POP0 with constant *PCONST0 at the outermost
9685    position.  We would now like to also perform OP1 with constant CONST1
9686    (with *POP0 being done last).
9687
9688    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9689    the resulting operation.  *PCOMP_P is set to 1 if we would need to
9690    complement the innermost operand, otherwise it is unchanged.
9691
9692    MODE is the mode in which the operation will be done.  No bits outside
9693    the width of this mode matter.  It is assumed that the width of this mode
9694    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9695
9696    If *POP0 or OP1 are UNKNOWN, it means no operation is required.  Only NEG, PLUS,
9697    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
9698    result is simply *PCONST0.
9699
9700    If the resulting operation cannot be expressed as one operation, we
9701    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
9702
9703 static int
9704 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)
9705 {
9706   enum rtx_code op0 = *pop0;
9707   HOST_WIDE_INT const0 = *pconst0;
9708
9709   const0 &= GET_MODE_MASK (mode);
9710   const1 &= GET_MODE_MASK (mode);
9711
9712   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
9713   if (op0 == AND)
9714     const1 &= const0;
9715
9716   /* If OP0 or OP1 is UNKNOWN, this is easy.  Similarly if they are the same or
9717      if OP0 is SET.  */
9718
9719   if (op1 == UNKNOWN || op0 == SET)
9720     return 1;
9721
9722   else if (op0 == UNKNOWN)
9723     op0 = op1, const0 = const1;
9724
9725   else if (op0 == op1)
9726     {
9727       switch (op0)
9728         {
9729         case AND:
9730           const0 &= const1;
9731           break;
9732         case IOR:
9733           const0 |= const1;
9734           break;
9735         case XOR:
9736           const0 ^= const1;
9737           break;
9738         case PLUS:
9739           const0 += const1;
9740           break;
9741         case NEG:
9742           op0 = UNKNOWN;
9743           break;
9744         default:
9745           break;
9746         }
9747     }
9748
9749   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
9750   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9751     return 0;
9752
9753   /* If the two constants aren't the same, we can't do anything.  The
9754      remaining six cases can all be done.  */
9755   else if (const0 != const1)
9756     return 0;
9757
9758   else
9759     switch (op0)
9760       {
9761       case IOR:
9762         if (op1 == AND)
9763           /* (a & b) | b == b */
9764           op0 = SET;
9765         else /* op1 == XOR */
9766           /* (a ^ b) | b == a | b */
9767           {;}
9768         break;
9769
9770       case XOR:
9771         if (op1 == AND)
9772           /* (a & b) ^ b == (~a) & b */
9773           op0 = AND, *pcomp_p = 1;
9774         else /* op1 == IOR */
9775           /* (a | b) ^ b == a & ~b */
9776           op0 = AND, const0 = ~const0;
9777         break;
9778
9779       case AND:
9780         if (op1 == IOR)
9781           /* (a | b) & b == b */
9782         op0 = SET;
9783         else /* op1 == XOR */
9784           /* (a ^ b) & b) == (~a) & b */
9785           *pcomp_p = 1;
9786         break;
9787       default:
9788         break;
9789       }
9790
9791   /* Check for NO-OP cases.  */
9792   const0 &= GET_MODE_MASK (mode);
9793   if (const0 == 0
9794       && (op0 == IOR || op0 == XOR || op0 == PLUS))
9795     op0 = UNKNOWN;
9796   else if (const0 == 0 && op0 == AND)
9797     op0 = SET;
9798   else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9799            && op0 == AND)
9800     op0 = UNKNOWN;
9801
9802   *pop0 = op0;
9803
9804   /* ??? Slightly redundant with the above mask, but not entirely.
9805      Moving this above means we'd have to sign-extend the mode mask
9806      for the final test.  */
9807   if (op0 != UNKNOWN && op0 != NEG)
9808     *pconst0 = trunc_int_for_mode (const0, mode);
9809
9810   return 1;
9811 }
9812 \f
9813 /* A helper to simplify_shift_const_1 to determine the mode we can perform
9814    the shift in.  The original shift operation CODE is performed on OP in
9815    ORIG_MODE.  Return the wider mode MODE if we can perform the operation
9816    in that mode.  Return ORIG_MODE otherwise.  We can also assume that the
9817    result of the shift is subject to operation OUTER_CODE with operand
9818    OUTER_CONST.  */
9819
9820 static enum machine_mode
9821 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
9822                       enum machine_mode orig_mode, enum machine_mode mode,
9823                       enum rtx_code outer_code, HOST_WIDE_INT outer_const)
9824 {
9825   if (orig_mode == mode)
9826     return mode;
9827   gcc_assert (GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (orig_mode));
9828
9829   /* In general we can't perform in wider mode for right shift and rotate.  */
9830   switch (code)
9831     {
9832     case ASHIFTRT:
9833       /* We can still widen if the bits brought in from the left are identical
9834          to the sign bit of ORIG_MODE.  */
9835       if (num_sign_bit_copies (op, mode)
9836           > (unsigned) (GET_MODE_BITSIZE (mode)
9837                         - GET_MODE_BITSIZE (orig_mode)))
9838         return mode;
9839       return orig_mode;
9840
9841     case LSHIFTRT:
9842       /* Similarly here but with zero bits.  */
9843       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9844           && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
9845         return mode;
9846
9847       /* We can also widen if the bits brought in will be masked off.  This
9848          operation is performed in ORIG_MODE.  */
9849       if (outer_code == AND)
9850         {
9851           int care_bits = low_bitmask_len (orig_mode, outer_const);
9852
9853           if (care_bits >= 0
9854               && GET_MODE_BITSIZE (orig_mode) - care_bits >= count)
9855             return mode;
9856         }
9857       /* fall through */
9858
9859     case ROTATE:
9860       return orig_mode;
9861
9862     case ROTATERT:
9863       gcc_unreachable ();
9864
9865     default:
9866       return mode;
9867     }
9868 }
9869
9870 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
9871    The result of the shift is RESULT_MODE.  Return NULL_RTX if we cannot
9872    simplify it.  Otherwise, return a simplified value.
9873
9874    The shift is normally computed in the widest mode we find in VAROP, as
9875    long as it isn't a different number of words than RESULT_MODE.  Exceptions
9876    are ASHIFTRT and ROTATE, which are always done in their original mode.  */
9877
9878 static rtx
9879 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
9880                         rtx varop, int orig_count)
9881 {
9882   enum rtx_code orig_code = code;
9883   rtx orig_varop = varop;
9884   int count;
9885   enum machine_mode mode = result_mode;
9886   enum machine_mode shift_mode, tmode;
9887   unsigned int mode_words
9888     = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9889   /* We form (outer_op (code varop count) (outer_const)).  */
9890   enum rtx_code outer_op = UNKNOWN;
9891   HOST_WIDE_INT outer_const = 0;
9892   int complement_p = 0;
9893   rtx new_rtx, x;
9894
9895   /* Make sure and truncate the "natural" shift on the way in.  We don't
9896      want to do this inside the loop as it makes it more difficult to
9897      combine shifts.  */
9898   if (SHIFT_COUNT_TRUNCATED)
9899     orig_count &= GET_MODE_BITSIZE (mode) - 1;
9900
9901   /* If we were given an invalid count, don't do anything except exactly
9902      what was requested.  */
9903
9904   if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
9905     return NULL_RTX;
9906
9907   count = orig_count;
9908
9909   /* Unless one of the branches of the `if' in this loop does a `continue',
9910      we will `break' the loop after the `if'.  */
9911
9912   while (count != 0)
9913     {
9914       /* If we have an operand of (clobber (const_int 0)), fail.  */
9915       if (GET_CODE (varop) == CLOBBER)
9916         return NULL_RTX;
9917
9918       /* Convert ROTATERT to ROTATE.  */
9919       if (code == ROTATERT)
9920         {
9921           unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
9922           code = ROTATE;
9923           if (VECTOR_MODE_P (result_mode))
9924             count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9925           else
9926             count = bitsize - count;
9927         }
9928
9929       shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
9930                                          mode, outer_op, outer_const);
9931
9932       /* Handle cases where the count is greater than the size of the mode
9933          minus 1.  For ASHIFT, use the size minus one as the count (this can
9934          occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
9935          take the count modulo the size.  For other shifts, the result is
9936          zero.
9937
9938          Since these shifts are being produced by the compiler by combining
9939          multiple operations, each of which are defined, we know what the
9940          result is supposed to be.  */
9941
9942       if (count > (GET_MODE_BITSIZE (shift_mode) - 1))
9943         {
9944           if (code == ASHIFTRT)
9945             count = GET_MODE_BITSIZE (shift_mode) - 1;
9946           else if (code == ROTATE || code == ROTATERT)
9947             count %= GET_MODE_BITSIZE (shift_mode);
9948           else
9949             {
9950               /* We can't simply return zero because there may be an
9951                  outer op.  */
9952               varop = const0_rtx;
9953               count = 0;
9954               break;
9955             }
9956         }
9957
9958       /* If we discovered we had to complement VAROP, leave.  Making a NOT
9959          here would cause an infinite loop.  */
9960       if (complement_p)
9961         break;
9962
9963       /* An arithmetic right shift of a quantity known to be -1 or 0
9964          is a no-op.  */
9965       if (code == ASHIFTRT
9966           && (num_sign_bit_copies (varop, shift_mode)
9967               == GET_MODE_BITSIZE (shift_mode)))
9968         {
9969           count = 0;
9970           break;
9971         }
9972
9973       /* If we are doing an arithmetic right shift and discarding all but
9974          the sign bit copies, this is equivalent to doing a shift by the
9975          bitsize minus one.  Convert it into that shift because it will often
9976          allow other simplifications.  */
9977
9978       if (code == ASHIFTRT
9979           && (count + num_sign_bit_copies (varop, shift_mode)
9980               >= GET_MODE_BITSIZE (shift_mode)))
9981         count = GET_MODE_BITSIZE (shift_mode) - 1;
9982
9983       /* We simplify the tests below and elsewhere by converting
9984          ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9985          `make_compound_operation' will convert it to an ASHIFTRT for
9986          those machines (such as VAX) that don't have an LSHIFTRT.  */
9987       if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9988           && code == ASHIFTRT
9989           && ((nonzero_bits (varop, shift_mode)
9990                & ((unsigned HOST_WIDE_INT) 1
9991                   << (GET_MODE_BITSIZE (shift_mode) - 1))) == 0))
9992         code = LSHIFTRT;
9993
9994       if (((code == LSHIFTRT
9995             && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9996             && !(nonzero_bits (varop, shift_mode) >> count))
9997            || (code == ASHIFT
9998                && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9999                && !((nonzero_bits (varop, shift_mode) << count)
10000                     & GET_MODE_MASK (shift_mode))))
10001           && !side_effects_p (varop))
10002         varop = const0_rtx;
10003
10004       switch (GET_CODE (varop))
10005         {
10006         case SIGN_EXTEND:
10007         case ZERO_EXTEND:
10008         case SIGN_EXTRACT:
10009         case ZERO_EXTRACT:
10010           new_rtx = expand_compound_operation (varop);
10011           if (new_rtx != varop)
10012             {
10013               varop = new_rtx;
10014               continue;
10015             }
10016           break;
10017
10018         case MEM:
10019           /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
10020              minus the width of a smaller mode, we can do this with a
10021              SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
10022           if ((code == ASHIFTRT || code == LSHIFTRT)
10023               && ! mode_dependent_address_p (XEXP (varop, 0))
10024               && ! MEM_VOLATILE_P (varop)
10025               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
10026                                          MODE_INT, 1)) != BLKmode)
10027             {
10028               new_rtx = adjust_address_nv (varop, tmode,
10029                                        BYTES_BIG_ENDIAN ? 0
10030                                        : count / BITS_PER_UNIT);
10031
10032               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10033                                      : ZERO_EXTEND, mode, new_rtx);
10034               count = 0;
10035               continue;
10036             }
10037           break;
10038
10039         case SUBREG:
10040           /* If VAROP is a SUBREG, strip it as long as the inner operand has
10041              the same number of words as what we've seen so far.  Then store
10042              the widest mode in MODE.  */
10043           if (subreg_lowpart_p (varop)
10044               && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10045                   > GET_MODE_SIZE (GET_MODE (varop)))
10046               && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10047                                   + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
10048                  == mode_words
10049               && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
10050               && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
10051             {
10052               varop = SUBREG_REG (varop);
10053               if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
10054                 mode = GET_MODE (varop);
10055               continue;
10056             }
10057           break;
10058
10059         case MULT:
10060           /* Some machines use MULT instead of ASHIFT because MULT
10061              is cheaper.  But it is still better on those machines to
10062              merge two shifts into one.  */
10063           if (CONST_INT_P (XEXP (varop, 1))
10064               && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10065             {
10066               varop
10067                 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10068                                        XEXP (varop, 0),
10069                                        GEN_INT (exact_log2 (
10070                                                 UINTVAL (XEXP (varop, 1)))));
10071               continue;
10072             }
10073           break;
10074
10075         case UDIV:
10076           /* Similar, for when divides are cheaper.  */
10077           if (CONST_INT_P (XEXP (varop, 1))
10078               && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10079             {
10080               varop
10081                 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10082                                        XEXP (varop, 0),
10083                                        GEN_INT (exact_log2 (
10084                                                 UINTVAL (XEXP (varop, 1)))));
10085               continue;
10086             }
10087           break;
10088
10089         case ASHIFTRT:
10090           /* If we are extracting just the sign bit of an arithmetic
10091              right shift, that shift is not needed.  However, the sign
10092              bit of a wider mode may be different from what would be
10093              interpreted as the sign bit in a narrower mode, so, if
10094              the result is narrower, don't discard the shift.  */
10095           if (code == LSHIFTRT
10096               && count == (GET_MODE_BITSIZE (result_mode) - 1)
10097               && (GET_MODE_BITSIZE (result_mode)
10098                   >= GET_MODE_BITSIZE (GET_MODE (varop))))
10099             {
10100               varop = XEXP (varop, 0);
10101               continue;
10102             }
10103
10104           /* ... fall through ...  */
10105
10106         case LSHIFTRT:
10107         case ASHIFT:
10108         case ROTATE:
10109           /* Here we have two nested shifts.  The result is usually the
10110              AND of a new shift with a mask.  We compute the result below.  */
10111           if (CONST_INT_P (XEXP (varop, 1))
10112               && INTVAL (XEXP (varop, 1)) >= 0
10113               && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
10114               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
10115               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10116               && !VECTOR_MODE_P (result_mode))
10117             {
10118               enum rtx_code first_code = GET_CODE (varop);
10119               unsigned int first_count = INTVAL (XEXP (varop, 1));
10120               unsigned HOST_WIDE_INT mask;
10121               rtx mask_rtx;
10122
10123               /* We have one common special case.  We can't do any merging if
10124                  the inner code is an ASHIFTRT of a smaller mode.  However, if
10125                  we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10126                  with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10127                  we can convert it to
10128                  (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
10129                  This simplifies certain SIGN_EXTEND operations.  */
10130               if (code == ASHIFT && first_code == ASHIFTRT
10131                   && count == (GET_MODE_BITSIZE (result_mode)
10132                                - GET_MODE_BITSIZE (GET_MODE (varop))))
10133                 {
10134                   /* C3 has the low-order C1 bits zero.  */
10135
10136                   mask = GET_MODE_MASK (mode)
10137                          & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10138
10139                   varop = simplify_and_const_int (NULL_RTX, result_mode,
10140                                                   XEXP (varop, 0), mask);
10141                   varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10142                                                 varop, count);
10143                   count = first_count;
10144                   code = ASHIFTRT;
10145                   continue;
10146                 }
10147
10148               /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10149                  than C1 high-order bits equal to the sign bit, we can convert
10150                  this to either an ASHIFT or an ASHIFTRT depending on the
10151                  two counts.
10152
10153                  We cannot do this if VAROP's mode is not SHIFT_MODE.  */
10154
10155               if (code == ASHIFTRT && first_code == ASHIFT
10156                   && GET_MODE (varop) == shift_mode
10157                   && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10158                       > first_count))
10159                 {
10160                   varop = XEXP (varop, 0);
10161                   count -= first_count;
10162                   if (count < 0)
10163                     {
10164                       count = -count;
10165                       code = ASHIFT;
10166                     }
10167
10168                   continue;
10169                 }
10170
10171               /* There are some cases we can't do.  If CODE is ASHIFTRT,
10172                  we can only do this if FIRST_CODE is also ASHIFTRT.
10173
10174                  We can't do the case when CODE is ROTATE and FIRST_CODE is
10175                  ASHIFTRT.
10176
10177                  If the mode of this shift is not the mode of the outer shift,
10178                  we can't do this if either shift is a right shift or ROTATE.
10179
10180                  Finally, we can't do any of these if the mode is too wide
10181                  unless the codes are the same.
10182
10183                  Handle the case where the shift codes are the same
10184                  first.  */
10185
10186               if (code == first_code)
10187                 {
10188                   if (GET_MODE (varop) != result_mode
10189                       && (code == ASHIFTRT || code == LSHIFTRT
10190                           || code == ROTATE))
10191                     break;
10192
10193                   count += first_count;
10194                   varop = XEXP (varop, 0);
10195                   continue;
10196                 }
10197
10198               if (code == ASHIFTRT
10199                   || (code == ROTATE && first_code == ASHIFTRT)
10200                   || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
10201                   || (GET_MODE (varop) != result_mode
10202                       && (first_code == ASHIFTRT || first_code == LSHIFTRT
10203                           || first_code == ROTATE
10204                           || code == ROTATE)))
10205                 break;
10206
10207               /* To compute the mask to apply after the shift, shift the
10208                  nonzero bits of the inner shift the same way the
10209                  outer shift will.  */
10210
10211               mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
10212
10213               mask_rtx
10214                 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10215                                                    GEN_INT (count));
10216
10217               /* Give up if we can't compute an outer operation to use.  */
10218               if (mask_rtx == 0
10219                   || !CONST_INT_P (mask_rtx)
10220                   || ! merge_outer_ops (&outer_op, &outer_const, AND,
10221                                         INTVAL (mask_rtx),
10222                                         result_mode, &complement_p))
10223                 break;
10224
10225               /* If the shifts are in the same direction, we add the
10226                  counts.  Otherwise, we subtract them.  */
10227               if ((code == ASHIFTRT || code == LSHIFTRT)
10228                   == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10229                 count += first_count;
10230               else
10231                 count -= first_count;
10232
10233               /* If COUNT is positive, the new shift is usually CODE,
10234                  except for the two exceptions below, in which case it is
10235                  FIRST_CODE.  If the count is negative, FIRST_CODE should
10236                  always be used  */
10237               if (count > 0
10238                   && ((first_code == ROTATE && code == ASHIFT)
10239                       || (first_code == ASHIFTRT && code == LSHIFTRT)))
10240                 code = first_code;
10241               else if (count < 0)
10242                 code = first_code, count = -count;
10243
10244               varop = XEXP (varop, 0);
10245               continue;
10246             }
10247
10248           /* If we have (A << B << C) for any shift, we can convert this to
10249              (A << C << B).  This wins if A is a constant.  Only try this if
10250              B is not a constant.  */
10251
10252           else if (GET_CODE (varop) == code
10253                    && CONST_INT_P (XEXP (varop, 0))
10254                    && !CONST_INT_P (XEXP (varop, 1)))
10255             {
10256               rtx new_rtx = simplify_const_binary_operation (code, mode,
10257                                                          XEXP (varop, 0),
10258                                                          GEN_INT (count));
10259               varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10260               count = 0;
10261               continue;
10262             }
10263           break;
10264
10265         case NOT:
10266           if (VECTOR_MODE_P (mode))
10267             break;
10268
10269           /* Make this fit the case below.  */
10270           varop = gen_rtx_XOR (mode, XEXP (varop, 0),
10271                                GEN_INT (GET_MODE_MASK (mode)));
10272           continue;
10273
10274         case IOR:
10275         case AND:
10276         case XOR:
10277           /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10278              with C the size of VAROP - 1 and the shift is logical if
10279              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10280              we have an (le X 0) operation.   If we have an arithmetic shift
10281              and STORE_FLAG_VALUE is 1 or we have a logical shift with
10282              STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
10283
10284           if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10285               && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10286               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10287               && (code == LSHIFTRT || code == ASHIFTRT)
10288               && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
10289               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10290             {
10291               count = 0;
10292               varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10293                                   const0_rtx);
10294
10295               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10296                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10297
10298               continue;
10299             }
10300
10301           /* If we have (shift (logical)), move the logical to the outside
10302              to allow it to possibly combine with another logical and the
10303              shift to combine with another shift.  This also canonicalizes to
10304              what a ZERO_EXTRACT looks like.  Also, some machines have
10305              (and (shift)) insns.  */
10306
10307           if (CONST_INT_P (XEXP (varop, 1))
10308               /* We can't do this if we have (ashiftrt (xor))  and the
10309                  constant has its sign bit set in shift_mode.  */
10310               && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10311                    && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10312                                               shift_mode))
10313               && (new_rtx = simplify_const_binary_operation (code, result_mode,
10314                                                          XEXP (varop, 1),
10315                                                          GEN_INT (count))) != 0
10316               && CONST_INT_P (new_rtx)
10317               && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10318                                   INTVAL (new_rtx), result_mode, &complement_p))
10319             {
10320               varop = XEXP (varop, 0);
10321               continue;
10322             }
10323
10324           /* If we can't do that, try to simplify the shift in each arm of the
10325              logical expression, make a new logical expression, and apply
10326              the inverse distributive law.  This also can't be done
10327              for some (ashiftrt (xor)).  */
10328           if (CONST_INT_P (XEXP (varop, 1))
10329              && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10330                   && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10331                                              shift_mode)))
10332             {
10333               rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10334                                               XEXP (varop, 0), count);
10335               rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10336                                               XEXP (varop, 1), count);
10337
10338               varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10339                                            lhs, rhs);
10340               varop = apply_distributive_law (varop);
10341
10342               count = 0;
10343               continue;
10344             }
10345           break;
10346
10347         case EQ:
10348           /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10349              says that the sign bit can be tested, FOO has mode MODE, C is
10350              GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
10351              that may be nonzero.  */
10352           if (code == LSHIFTRT
10353               && XEXP (varop, 1) == const0_rtx
10354               && GET_MODE (XEXP (varop, 0)) == result_mode
10355               && count == (GET_MODE_BITSIZE (result_mode) - 1)
10356               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
10357               && STORE_FLAG_VALUE == -1
10358               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10359               && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10360                                   &complement_p))
10361             {
10362               varop = XEXP (varop, 0);
10363               count = 0;
10364               continue;
10365             }
10366           break;
10367
10368         case NEG:
10369           /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10370              than the number of bits in the mode is equivalent to A.  */
10371           if (code == LSHIFTRT
10372               && count == (GET_MODE_BITSIZE (result_mode) - 1)
10373               && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10374             {
10375               varop = XEXP (varop, 0);
10376               count = 0;
10377               continue;
10378             }
10379
10380           /* NEG commutes with ASHIFT since it is multiplication.  Move the
10381              NEG outside to allow shifts to combine.  */
10382           if (code == ASHIFT
10383               && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10384                                   &complement_p))
10385             {
10386               varop = XEXP (varop, 0);
10387               continue;
10388             }
10389           break;
10390
10391         case PLUS:
10392           /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10393              is one less than the number of bits in the mode is
10394              equivalent to (xor A 1).  */
10395           if (code == LSHIFTRT
10396               && count == (GET_MODE_BITSIZE (result_mode) - 1)
10397               && XEXP (varop, 1) == constm1_rtx
10398               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10399               && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10400                                   &complement_p))
10401             {
10402               count = 0;
10403               varop = XEXP (varop, 0);
10404               continue;
10405             }
10406
10407           /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10408              that might be nonzero in BAR are those being shifted out and those
10409              bits are known zero in FOO, we can replace the PLUS with FOO.
10410              Similarly in the other operand order.  This code occurs when
10411              we are computing the size of a variable-size array.  */
10412
10413           if ((code == ASHIFTRT || code == LSHIFTRT)
10414               && count < HOST_BITS_PER_WIDE_INT
10415               && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10416               && (nonzero_bits (XEXP (varop, 1), result_mode)
10417                   & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10418             {
10419               varop = XEXP (varop, 0);
10420               continue;
10421             }
10422           else if ((code == ASHIFTRT || code == LSHIFTRT)
10423                    && count < HOST_BITS_PER_WIDE_INT
10424                    && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
10425                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10426                             >> count)
10427                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10428                             & nonzero_bits (XEXP (varop, 1),
10429                                                  result_mode)))
10430             {
10431               varop = XEXP (varop, 1);
10432               continue;
10433             }
10434
10435           /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
10436           if (code == ASHIFT
10437               && CONST_INT_P (XEXP (varop, 1))
10438               && (new_rtx = simplify_const_binary_operation (ASHIFT, result_mode,
10439                                                          XEXP (varop, 1),
10440                                                          GEN_INT (count))) != 0
10441               && CONST_INT_P (new_rtx)
10442               && merge_outer_ops (&outer_op, &outer_const, PLUS,
10443                                   INTVAL (new_rtx), result_mode, &complement_p))
10444             {
10445               varop = XEXP (varop, 0);
10446               continue;
10447             }
10448
10449           /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10450              signbit', and attempt to change the PLUS to an XOR and move it to
10451              the outer operation as is done above in the AND/IOR/XOR case
10452              leg for shift(logical). See details in logical handling above
10453              for reasoning in doing so.  */
10454           if (code == LSHIFTRT
10455               && CONST_INT_P (XEXP (varop, 1))
10456               && mode_signbit_p (result_mode, XEXP (varop, 1))
10457               && (new_rtx = simplify_const_binary_operation (code, result_mode,
10458                                                          XEXP (varop, 1),
10459                                                          GEN_INT (count))) != 0
10460               && CONST_INT_P (new_rtx)
10461               && merge_outer_ops (&outer_op, &outer_const, XOR,
10462                                   INTVAL (new_rtx), result_mode, &complement_p))
10463             {
10464               varop = XEXP (varop, 0);
10465               continue;
10466             }
10467
10468           break;
10469
10470         case MINUS:
10471           /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10472              with C the size of VAROP - 1 and the shift is logical if
10473              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10474              we have a (gt X 0) operation.  If the shift is arithmetic with
10475              STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10476              we have a (neg (gt X 0)) operation.  */
10477
10478           if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10479               && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10480               && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
10481               && (code == LSHIFTRT || code == ASHIFTRT)
10482               && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10483               && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10484               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10485             {
10486               count = 0;
10487               varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10488                                   const0_rtx);
10489
10490               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10491                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10492
10493               continue;
10494             }
10495           break;
10496
10497         case TRUNCATE:
10498           /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10499              if the truncate does not affect the value.  */
10500           if (code == LSHIFTRT
10501               && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10502               && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10503               && (INTVAL (XEXP (XEXP (varop, 0), 1))
10504                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
10505                       - GET_MODE_BITSIZE (GET_MODE (varop)))))
10506             {
10507               rtx varop_inner = XEXP (varop, 0);
10508
10509               varop_inner
10510                 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10511                                     XEXP (varop_inner, 0),
10512                                     GEN_INT
10513                                     (count + INTVAL (XEXP (varop_inner, 1))));
10514               varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10515               count = 0;
10516               continue;
10517             }
10518           break;
10519
10520         default:
10521           break;
10522         }
10523
10524       break;
10525     }
10526
10527   shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10528                                      outer_op, outer_const);
10529
10530   /* We have now finished analyzing the shift.  The result should be
10531      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
10532      OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10533      to the result of the shift.  OUTER_CONST is the relevant constant,
10534      but we must turn off all bits turned off in the shift.  */
10535
10536   if (outer_op == UNKNOWN
10537       && orig_code == code && orig_count == count
10538       && varop == orig_varop
10539       && shift_mode == GET_MODE (varop))
10540     return NULL_RTX;
10541
10542   /* Make a SUBREG if necessary.  If we can't make it, fail.  */
10543   varop = gen_lowpart (shift_mode, varop);
10544   if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10545     return NULL_RTX;
10546
10547   /* If we have an outer operation and we just made a shift, it is
10548      possible that we could have simplified the shift were it not
10549      for the outer operation.  So try to do the simplification
10550      recursively.  */
10551
10552   if (outer_op != UNKNOWN)
10553     x = simplify_shift_const_1 (code, shift_mode, varop, count);
10554   else
10555     x = NULL_RTX;
10556
10557   if (x == NULL_RTX)
10558     x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10559
10560   /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10561      turn off all the bits that the shift would have turned off.  */
10562   if (orig_code == LSHIFTRT && result_mode != shift_mode)
10563     x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10564                                 GET_MODE_MASK (result_mode) >> orig_count);
10565
10566   /* Do the remainder of the processing in RESULT_MODE.  */
10567   x = gen_lowpart_or_truncate (result_mode, x);
10568
10569   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10570      operation.  */
10571   if (complement_p)
10572     x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10573
10574   if (outer_op != UNKNOWN)
10575     {
10576       if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10577           && GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
10578         outer_const = trunc_int_for_mode (outer_const, result_mode);
10579
10580       if (outer_op == AND)
10581         x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10582       else if (outer_op == SET)
10583         {
10584           /* This means that we have determined that the result is
10585              equivalent to a constant.  This should be rare.  */
10586           if (!side_effects_p (x))
10587             x = GEN_INT (outer_const);
10588         }
10589       else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10590         x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10591       else
10592         x = simplify_gen_binary (outer_op, result_mode, x,
10593                                  GEN_INT (outer_const));
10594     }
10595
10596   return x;
10597 }
10598
10599 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
10600    The result of the shift is RESULT_MODE.  If we cannot simplify it,
10601    return X or, if it is NULL, synthesize the expression with
10602    simplify_gen_binary.  Otherwise, return a simplified value.
10603
10604    The shift is normally computed in the widest mode we find in VAROP, as
10605    long as it isn't a different number of words than RESULT_MODE.  Exceptions
10606    are ASHIFTRT and ROTATE, which are always done in their original mode.  */
10607
10608 static rtx
10609 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
10610                       rtx varop, int count)
10611 {
10612   rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10613   if (tem)
10614     return tem;
10615
10616   if (!x)
10617     x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10618   if (GET_MODE (x) != result_mode)
10619     x = gen_lowpart (result_mode, x);
10620   return x;
10621 }
10622
10623 \f
10624 /* Like recog, but we receive the address of a pointer to a new pattern.
10625    We try to match the rtx that the pointer points to.
10626    If that fails, we may try to modify or replace the pattern,
10627    storing the replacement into the same pointer object.
10628
10629    Modifications include deletion or addition of CLOBBERs.
10630
10631    PNOTES is a pointer to a location where any REG_UNUSED notes added for
10632    the CLOBBERs are placed.
10633
10634    The value is the final insn code from the pattern ultimately matched,
10635    or -1.  */
10636
10637 static int
10638 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
10639 {
10640   rtx pat = *pnewpat;
10641   int insn_code_number;
10642   int num_clobbers_to_add = 0;
10643   int i;
10644   rtx notes = 0;
10645   rtx old_notes, old_pat;
10646
10647   /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10648      we use to indicate that something didn't match.  If we find such a
10649      thing, force rejection.  */
10650   if (GET_CODE (pat) == PARALLEL)
10651     for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10652       if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10653           && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10654         return -1;
10655
10656   old_pat = PATTERN (insn);
10657   old_notes = REG_NOTES (insn);
10658   PATTERN (insn) = pat;
10659   REG_NOTES (insn) = 0;
10660
10661   insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10662   if (dump_file && (dump_flags & TDF_DETAILS))
10663     {
10664       if (insn_code_number < 0)
10665         fputs ("Failed to match this instruction:\n", dump_file);
10666       else
10667         fputs ("Successfully matched this instruction:\n", dump_file);
10668       print_rtl_single (dump_file, pat);
10669     }
10670
10671   /* If it isn't, there is the possibility that we previously had an insn
10672      that clobbered some register as a side effect, but the combined
10673      insn doesn't need to do that.  So try once more without the clobbers
10674      unless this represents an ASM insn.  */
10675
10676   if (insn_code_number < 0 && ! check_asm_operands (pat)
10677       && GET_CODE (pat) == PARALLEL)
10678     {
10679       int pos;
10680
10681       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10682         if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10683           {
10684             if (i != pos)
10685               SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10686             pos++;
10687           }
10688
10689       SUBST_INT (XVECLEN (pat, 0), pos);
10690
10691       if (pos == 1)
10692         pat = XVECEXP (pat, 0, 0);
10693
10694       PATTERN (insn) = pat;
10695       insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10696       if (dump_file && (dump_flags & TDF_DETAILS))
10697         {
10698           if (insn_code_number < 0)
10699             fputs ("Failed to match this instruction:\n", dump_file);
10700           else
10701             fputs ("Successfully matched this instruction:\n", dump_file);
10702           print_rtl_single (dump_file, pat);
10703         }
10704     }
10705   PATTERN (insn) = old_pat;
10706   REG_NOTES (insn) = old_notes;
10707
10708   /* Recognize all noop sets, these will be killed by followup pass.  */
10709   if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10710     insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10711
10712   /* If we had any clobbers to add, make a new pattern than contains
10713      them.  Then check to make sure that all of them are dead.  */
10714   if (num_clobbers_to_add)
10715     {
10716       rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10717                                      rtvec_alloc (GET_CODE (pat) == PARALLEL
10718                                                   ? (XVECLEN (pat, 0)
10719                                                      + num_clobbers_to_add)
10720                                                   : num_clobbers_to_add + 1));
10721
10722       if (GET_CODE (pat) == PARALLEL)
10723         for (i = 0; i < XVECLEN (pat, 0); i++)
10724           XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10725       else
10726         XVECEXP (newpat, 0, 0) = pat;
10727
10728       add_clobbers (newpat, insn_code_number);
10729
10730       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10731            i < XVECLEN (newpat, 0); i++)
10732         {
10733           if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10734               && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10735             return -1;
10736           if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10737             {
10738               gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10739               notes = alloc_reg_note (REG_UNUSED,
10740                                       XEXP (XVECEXP (newpat, 0, i), 0), notes);
10741             }
10742         }
10743       pat = newpat;
10744     }
10745
10746   *pnewpat = pat;
10747   *pnotes = notes;
10748
10749   return insn_code_number;
10750 }
10751 \f
10752 /* Like gen_lowpart_general but for use by combine.  In combine it
10753    is not possible to create any new pseudoregs.  However, it is
10754    safe to create invalid memory addresses, because combine will
10755    try to recognize them and all they will do is make the combine
10756    attempt fail.
10757
10758    If for some reason this cannot do its job, an rtx
10759    (clobber (const_int 0)) is returned.
10760    An insn containing that will not be recognized.  */
10761
10762 static rtx
10763 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
10764 {
10765   enum machine_mode imode = GET_MODE (x);
10766   unsigned int osize = GET_MODE_SIZE (omode);
10767   unsigned int isize = GET_MODE_SIZE (imode);
10768   rtx result;
10769
10770   if (omode == imode)
10771     return x;
10772
10773   /* Return identity if this is a CONST or symbolic reference.  */
10774   if (omode == Pmode
10775       && (GET_CODE (x) == CONST
10776           || GET_CODE (x) == SYMBOL_REF
10777           || GET_CODE (x) == LABEL_REF))
10778     return x;
10779
10780   /* We can only support MODE being wider than a word if X is a
10781      constant integer or has a mode the same size.  */
10782   if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
10783       && ! ((imode == VOIDmode
10784              && (CONST_INT_P (x)
10785                  || GET_CODE (x) == CONST_DOUBLE))
10786             || isize == osize))
10787     goto fail;
10788
10789   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
10790      won't know what to do.  So we will strip off the SUBREG here and
10791      process normally.  */
10792   if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
10793     {
10794       x = SUBREG_REG (x);
10795
10796       /* For use in case we fall down into the address adjustments
10797          further below, we need to adjust the known mode and size of
10798          x; imode and isize, since we just adjusted x.  */
10799       imode = GET_MODE (x);
10800
10801       if (imode == omode)
10802         return x;
10803
10804       isize = GET_MODE_SIZE (imode);
10805     }
10806
10807   result = gen_lowpart_common (omode, x);
10808
10809   if (result)
10810     return result;
10811
10812   if (MEM_P (x))
10813     {
10814       int offset = 0;
10815
10816       /* Refuse to work on a volatile memory ref or one with a mode-dependent
10817          address.  */
10818       if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
10819         goto fail;
10820
10821       /* If we want to refer to something bigger than the original memref,
10822          generate a paradoxical subreg instead.  That will force a reload
10823          of the original memref X.  */
10824       if (isize < osize)
10825         return gen_rtx_SUBREG (omode, x, 0);
10826
10827       if (WORDS_BIG_ENDIAN)
10828         offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
10829
10830       /* Adjust the address so that the address-after-the-data is
10831          unchanged.  */
10832       if (BYTES_BIG_ENDIAN)
10833         offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
10834
10835       return adjust_address_nv (x, omode, offset);
10836     }
10837
10838   /* If X is a comparison operator, rewrite it in a new mode.  This
10839      probably won't match, but may allow further simplifications.  */
10840   else if (COMPARISON_P (x))
10841     return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
10842
10843   /* If we couldn't simplify X any other way, just enclose it in a
10844      SUBREG.  Normally, this SUBREG won't match, but some patterns may
10845      include an explicit SUBREG or we may simplify it further in combine.  */
10846   else
10847     {
10848       int offset = 0;
10849       rtx res;
10850
10851       offset = subreg_lowpart_offset (omode, imode);
10852       if (imode == VOIDmode)
10853         {
10854           imode = int_mode_for_mode (omode);
10855           x = gen_lowpart_common (imode, x);
10856           if (x == NULL)
10857             goto fail;
10858         }
10859       res = simplify_gen_subreg (omode, x, imode, offset);
10860       if (res)
10861         return res;
10862     }
10863
10864  fail:
10865   return gen_rtx_CLOBBER (omode, const0_rtx);
10866 }
10867 \f
10868 /* Try to simplify a comparison between OP0 and a constant OP1,
10869    where CODE is the comparison code that will be tested, into a
10870    (CODE OP0 const0_rtx) form.
10871
10872    The result is a possibly different comparison code to use.
10873    *POP1 may be updated.  */
10874
10875 static enum rtx_code
10876 simplify_compare_const (enum rtx_code code, rtx op0, rtx *pop1)
10877 {
10878   enum machine_mode mode = GET_MODE (op0);
10879   unsigned int mode_width = GET_MODE_BITSIZE (mode);
10880   HOST_WIDE_INT const_op = INTVAL (*pop1);
10881
10882   /* Get the constant we are comparing against and turn off all bits
10883      not on in our mode.  */
10884   if (mode != VOIDmode)
10885     const_op = trunc_int_for_mode (const_op, mode);
10886
10887   /* If we are comparing against a constant power of two and the value
10888      being compared can only have that single bit nonzero (e.g., it was
10889      `and'ed with that bit), we can replace this with a comparison
10890      with zero.  */
10891   if (const_op
10892       && (code == EQ || code == NE || code == GE || code == GEU
10893           || code == LT || code == LTU)
10894       && mode_width <= HOST_BITS_PER_WIDE_INT
10895       && exact_log2 (const_op) >= 0
10896       && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10897     {
10898       code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10899       const_op = 0;
10900     }
10901
10902   /* Similarly, if we are comparing a value known to be either -1 or
10903      0 with -1, change it to the opposite comparison against zero.  */
10904   if (const_op == -1
10905       && (code == EQ || code == NE || code == GT || code == LE
10906           || code == GEU || code == LTU)
10907       && num_sign_bit_copies (op0, mode) == mode_width)
10908     {
10909       code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10910       const_op = 0;
10911     }
10912
10913   /* Do some canonicalizations based on the comparison code.  We prefer
10914      comparisons against zero and then prefer equality comparisons.
10915      If we can reduce the size of a constant, we will do that too.  */
10916   switch (code)
10917     {
10918     case LT:
10919       /* < C is equivalent to <= (C - 1) */
10920       if (const_op > 0)
10921         {
10922           const_op -= 1;
10923           code = LE;
10924           /* ... fall through to LE case below.  */
10925         }
10926       else
10927         break;
10928
10929     case LE:
10930       /* <= C is equivalent to < (C + 1); we do this for C < 0  */
10931       if (const_op < 0)
10932         {
10933           const_op += 1;
10934           code = LT;
10935         }
10936
10937       /* If we are doing a <= 0 comparison on a value known to have
10938          a zero sign bit, we can replace this with == 0.  */
10939       else if (const_op == 0
10940                && mode_width <= HOST_BITS_PER_WIDE_INT
10941                && (nonzero_bits (op0, mode)
10942                    & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10943                == 0)
10944         code = EQ;
10945       break;
10946
10947     case GE:
10948       /* >= C is equivalent to > (C - 1).  */
10949       if (const_op > 0)
10950         {
10951           const_op -= 1;
10952           code = GT;
10953           /* ... fall through to GT below.  */
10954         }
10955       else
10956         break;
10957
10958     case GT:
10959       /* > C is equivalent to >= (C + 1); we do this for C < 0.  */
10960       if (const_op < 0)
10961         {
10962           const_op += 1;
10963           code = GE;
10964         }
10965
10966       /* If we are doing a > 0 comparison on a value known to have
10967          a zero sign bit, we can replace this with != 0.  */
10968       else if (const_op == 0
10969                && mode_width <= HOST_BITS_PER_WIDE_INT
10970                && (nonzero_bits (op0, mode)
10971                    & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10972                == 0)
10973         code = NE;
10974       break;
10975
10976     case LTU:
10977       /* < C is equivalent to <= (C - 1).  */
10978       if (const_op > 0)
10979         {
10980           const_op -= 1;
10981           code = LEU;
10982           /* ... fall through ...  */
10983         }
10984       /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
10985       else if (mode_width <= HOST_BITS_PER_WIDE_INT
10986                && (unsigned HOST_WIDE_INT) const_op
10987                == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
10988         {
10989           const_op = 0;
10990           code = GE;
10991           break;
10992         }
10993       else
10994         break;
10995
10996     case LEU:
10997       /* unsigned <= 0 is equivalent to == 0 */
10998       if (const_op == 0)
10999         code = EQ;
11000       /* (unsigned) <= 0x7fffffff is equivalent to >= 0.  */
11001       else if (mode_width <= HOST_BITS_PER_WIDE_INT
11002                && (unsigned HOST_WIDE_INT) const_op
11003                == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11004         {
11005           const_op = 0;
11006           code = GE;
11007         }
11008       break;
11009
11010     case GEU:
11011       /* >= C is equivalent to > (C - 1).  */
11012       if (const_op > 1)
11013         {
11014           const_op -= 1;
11015           code = GTU;
11016           /* ... fall through ...  */
11017         }
11018
11019       /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
11020       else if (mode_width <= HOST_BITS_PER_WIDE_INT
11021                && (unsigned HOST_WIDE_INT) const_op
11022                == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11023         {
11024           const_op = 0;
11025           code = LT;
11026           break;
11027         }
11028       else
11029         break;
11030
11031     case GTU:
11032       /* unsigned > 0 is equivalent to != 0 */
11033       if (const_op == 0)
11034         code = NE;
11035       /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
11036       else if (mode_width <= HOST_BITS_PER_WIDE_INT
11037                && (unsigned HOST_WIDE_INT) const_op
11038                == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11039         {
11040           const_op = 0;
11041           code = LT;
11042         }
11043       break;
11044
11045     default:
11046       break;
11047     }
11048
11049   *pop1 = GEN_INT (const_op);
11050   return code;
11051 }
11052 \f
11053 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11054    comparison code that will be tested.
11055
11056    The result is a possibly different comparison code to use.  *POP0 and
11057    *POP1 may be updated.
11058
11059    It is possible that we might detect that a comparison is either always
11060    true or always false.  However, we do not perform general constant
11061    folding in combine, so this knowledge isn't useful.  Such tautologies
11062    should have been detected earlier.  Hence we ignore all such cases.  */
11063
11064 static enum rtx_code
11065 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11066 {
11067   rtx op0 = *pop0;
11068   rtx op1 = *pop1;
11069   rtx tem, tem1;
11070   int i;
11071   enum machine_mode mode, tmode;
11072
11073   /* Try a few ways of applying the same transformation to both operands.  */
11074   while (1)
11075     {
11076 #ifndef WORD_REGISTER_OPERATIONS
11077       /* The test below this one won't handle SIGN_EXTENDs on these machines,
11078          so check specially.  */
11079       if (code != GTU && code != GEU && code != LTU && code != LEU
11080           && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11081           && GET_CODE (XEXP (op0, 0)) == ASHIFT
11082           && GET_CODE (XEXP (op1, 0)) == ASHIFT
11083           && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11084           && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11085           && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
11086               == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
11087           && CONST_INT_P (XEXP (op0, 1))
11088           && XEXP (op0, 1) == XEXP (op1, 1)
11089           && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11090           && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11091           && (INTVAL (XEXP (op0, 1))
11092               == (GET_MODE_BITSIZE (GET_MODE (op0))
11093                   - (GET_MODE_BITSIZE
11094                      (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
11095         {
11096           op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11097           op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11098         }
11099 #endif
11100
11101       /* If both operands are the same constant shift, see if we can ignore the
11102          shift.  We can if the shift is a rotate or if the bits shifted out of
11103          this shift are known to be zero for both inputs and if the type of
11104          comparison is compatible with the shift.  */
11105       if (GET_CODE (op0) == GET_CODE (op1)
11106           && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
11107           && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11108               || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11109                   && (code != GT && code != LT && code != GE && code != LE))
11110               || (GET_CODE (op0) == ASHIFTRT
11111                   && (code != GTU && code != LTU
11112                       && code != GEU && code != LEU)))
11113           && CONST_INT_P (XEXP (op0, 1))
11114           && INTVAL (XEXP (op0, 1)) >= 0
11115           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11116           && XEXP (op0, 1) == XEXP (op1, 1))
11117         {
11118           enum machine_mode mode = GET_MODE (op0);
11119           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11120           int shift_count = INTVAL (XEXP (op0, 1));
11121
11122           if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11123             mask &= (mask >> shift_count) << shift_count;
11124           else if (GET_CODE (op0) == ASHIFT)
11125             mask = (mask & (mask << shift_count)) >> shift_count;
11126
11127           if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11128               && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11129             op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11130           else
11131             break;
11132         }
11133
11134       /* If both operands are AND's of a paradoxical SUBREG by constant, the
11135          SUBREGs are of the same mode, and, in both cases, the AND would
11136          be redundant if the comparison was done in the narrower mode,
11137          do the comparison in the narrower mode (e.g., we are AND'ing with 1
11138          and the operand's possibly nonzero bits are 0xffffff01; in that case
11139          if we only care about QImode, we don't need the AND).  This case
11140          occurs if the output mode of an scc insn is not SImode and
11141          STORE_FLAG_VALUE == 1 (e.g., the 386).
11142
11143          Similarly, check for a case where the AND's are ZERO_EXTEND
11144          operations from some narrower mode even though a SUBREG is not
11145          present.  */
11146
11147       else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11148                && CONST_INT_P (XEXP (op0, 1))
11149                && CONST_INT_P (XEXP (op1, 1)))
11150         {
11151           rtx inner_op0 = XEXP (op0, 0);
11152           rtx inner_op1 = XEXP (op1, 0);
11153           HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11154           HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11155           int changed = 0;
11156
11157           if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
11158               && (GET_MODE_SIZE (GET_MODE (inner_op0))
11159                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
11160               && (GET_MODE (SUBREG_REG (inner_op0))
11161                   == GET_MODE (SUBREG_REG (inner_op1)))
11162               && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
11163                   <= HOST_BITS_PER_WIDE_INT)
11164               && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11165                                              GET_MODE (SUBREG_REG (inner_op0)))))
11166               && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11167                                              GET_MODE (SUBREG_REG (inner_op1))))))
11168             {
11169               op0 = SUBREG_REG (inner_op0);
11170               op1 = SUBREG_REG (inner_op1);
11171
11172               /* The resulting comparison is always unsigned since we masked
11173                  off the original sign bit.  */
11174               code = unsigned_condition (code);
11175
11176               changed = 1;
11177             }
11178
11179           else if (c0 == c1)
11180             for (tmode = GET_CLASS_NARROWEST_MODE
11181                  (GET_MODE_CLASS (GET_MODE (op0)));
11182                  tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
11183               if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11184                 {
11185                   op0 = gen_lowpart (tmode, inner_op0);
11186                   op1 = gen_lowpart (tmode, inner_op1);
11187                   code = unsigned_condition (code);
11188                   changed = 1;
11189                   break;
11190                 }
11191
11192           if (! changed)
11193             break;
11194         }
11195
11196       /* If both operands are NOT, we can strip off the outer operation
11197          and adjust the comparison code for swapped operands; similarly for
11198          NEG, except that this must be an equality comparison.  */
11199       else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
11200                || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
11201                    && (code == EQ || code == NE)))
11202         op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
11203
11204       else
11205         break;
11206     }
11207
11208   /* If the first operand is a constant, swap the operands and adjust the
11209      comparison code appropriately, but don't do this if the second operand
11210      is already a constant integer.  */
11211   if (swap_commutative_operands_p (op0, op1))
11212     {
11213       tem = op0, op0 = op1, op1 = tem;
11214       code = swap_condition (code);
11215     }
11216
11217   /* We now enter a loop during which we will try to simplify the comparison.
11218      For the most part, we only are concerned with comparisons with zero,
11219      but some things may really be comparisons with zero but not start
11220      out looking that way.  */
11221
11222   while (CONST_INT_P (op1))
11223     {
11224       enum machine_mode mode = GET_MODE (op0);
11225       unsigned int mode_width = GET_MODE_BITSIZE (mode);
11226       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11227       int equality_comparison_p;
11228       int sign_bit_comparison_p;
11229       int unsigned_comparison_p;
11230       HOST_WIDE_INT const_op;
11231
11232       /* We only want to handle integral modes.  This catches VOIDmode,
11233          CCmode, and the floating-point modes.  An exception is that we
11234          can handle VOIDmode if OP0 is a COMPARE or a comparison
11235          operation.  */
11236
11237       if (GET_MODE_CLASS (mode) != MODE_INT
11238           && ! (mode == VOIDmode
11239                 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
11240         break;
11241
11242       /* Try to simplify the compare to constant, possibly changing the
11243          comparison op, and/or changing op1 to zero.  */
11244       code = simplify_compare_const (code, op0, &op1);
11245       const_op = INTVAL (op1);
11246
11247       /* Compute some predicates to simplify code below.  */
11248
11249       equality_comparison_p = (code == EQ || code == NE);
11250       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11251       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11252                                || code == GEU);
11253
11254       /* If this is a sign bit comparison and we can do arithmetic in
11255          MODE, say that we will only be needing the sign bit of OP0.  */
11256       if (sign_bit_comparison_p
11257           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11258         op0 = force_to_mode (op0, mode,
11259                              (unsigned HOST_WIDE_INT) 1
11260                              << (GET_MODE_BITSIZE (mode) - 1),
11261                              0);
11262
11263       /* Now try cases based on the opcode of OP0.  If none of the cases
11264          does a "continue", we exit this loop immediately after the
11265          switch.  */
11266
11267       switch (GET_CODE (op0))
11268         {
11269         case ZERO_EXTRACT:
11270           /* If we are extracting a single bit from a variable position in
11271              a constant that has only a single bit set and are comparing it
11272              with zero, we can convert this into an equality comparison
11273              between the position and the location of the single bit.  */
11274           /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11275              have already reduced the shift count modulo the word size.  */
11276           if (!SHIFT_COUNT_TRUNCATED
11277               && CONST_INT_P (XEXP (op0, 0))
11278               && XEXP (op0, 1) == const1_rtx
11279               && equality_comparison_p && const_op == 0
11280               && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11281             {
11282               if (BITS_BIG_ENDIAN)
11283                 {
11284                   enum machine_mode new_mode
11285                     = mode_for_extraction (EP_extzv, 1);
11286                   if (new_mode == MAX_MACHINE_MODE)
11287                     i = BITS_PER_WORD - 1 - i;
11288                   else
11289                     {
11290                       mode = new_mode;
11291                       i = (GET_MODE_BITSIZE (mode) - 1 - i);
11292                     }
11293                 }
11294
11295               op0 = XEXP (op0, 2);
11296               op1 = GEN_INT (i);
11297               const_op = i;
11298
11299               /* Result is nonzero iff shift count is equal to I.  */
11300               code = reverse_condition (code);
11301               continue;
11302             }
11303
11304           /* ... fall through ...  */
11305
11306         case SIGN_EXTRACT:
11307           tem = expand_compound_operation (op0);
11308           if (tem != op0)
11309             {
11310               op0 = tem;
11311               continue;
11312             }
11313           break;
11314
11315         case NOT:
11316           /* If testing for equality, we can take the NOT of the constant.  */
11317           if (equality_comparison_p
11318               && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11319             {
11320               op0 = XEXP (op0, 0);
11321               op1 = tem;
11322               continue;
11323             }
11324
11325           /* If just looking at the sign bit, reverse the sense of the
11326              comparison.  */
11327           if (sign_bit_comparison_p)
11328             {
11329               op0 = XEXP (op0, 0);
11330               code = (code == GE ? LT : GE);
11331               continue;
11332             }
11333           break;
11334
11335         case NEG:
11336           /* If testing for equality, we can take the NEG of the constant.  */
11337           if (equality_comparison_p
11338               && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11339             {
11340               op0 = XEXP (op0, 0);
11341               op1 = tem;
11342               continue;
11343             }
11344
11345           /* The remaining cases only apply to comparisons with zero.  */
11346           if (const_op != 0)
11347             break;
11348
11349           /* When X is ABS or is known positive,
11350              (neg X) is < 0 if and only if X != 0.  */
11351
11352           if (sign_bit_comparison_p
11353               && (GET_CODE (XEXP (op0, 0)) == ABS
11354                   || (mode_width <= HOST_BITS_PER_WIDE_INT
11355                       && (nonzero_bits (XEXP (op0, 0), mode)
11356                           & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11357                          == 0)))
11358             {
11359               op0 = XEXP (op0, 0);
11360               code = (code == LT ? NE : EQ);
11361               continue;
11362             }
11363
11364           /* If we have NEG of something whose two high-order bits are the
11365              same, we know that "(-a) < 0" is equivalent to "a > 0".  */
11366           if (num_sign_bit_copies (op0, mode) >= 2)
11367             {
11368               op0 = XEXP (op0, 0);
11369               code = swap_condition (code);
11370               continue;
11371             }
11372           break;
11373
11374         case ROTATE:
11375           /* If we are testing equality and our count is a constant, we
11376              can perform the inverse operation on our RHS.  */
11377           if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11378               && (tem = simplify_binary_operation (ROTATERT, mode,
11379                                                    op1, XEXP (op0, 1))) != 0)
11380             {
11381               op0 = XEXP (op0, 0);
11382               op1 = tem;
11383               continue;
11384             }
11385
11386           /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11387              a particular bit.  Convert it to an AND of a constant of that
11388              bit.  This will be converted into a ZERO_EXTRACT.  */
11389           if (const_op == 0 && sign_bit_comparison_p
11390               && CONST_INT_P (XEXP (op0, 1))
11391               && mode_width <= HOST_BITS_PER_WIDE_INT)
11392             {
11393               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11394                                             ((unsigned HOST_WIDE_INT) 1
11395                                              << (mode_width - 1
11396                                                  - INTVAL (XEXP (op0, 1)))));
11397               code = (code == LT ? NE : EQ);
11398               continue;
11399             }
11400
11401           /* Fall through.  */
11402
11403         case ABS:
11404           /* ABS is ignorable inside an equality comparison with zero.  */
11405           if (const_op == 0 && equality_comparison_p)
11406             {
11407               op0 = XEXP (op0, 0);
11408               continue;
11409             }
11410           break;
11411
11412         case SIGN_EXTEND:
11413           /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11414              (compare FOO CONST) if CONST fits in FOO's mode and we
11415              are either testing inequality or have an unsigned
11416              comparison with ZERO_EXTEND or a signed comparison with
11417              SIGN_EXTEND.  But don't do it if we don't have a compare
11418              insn of the given mode, since we'd have to revert it
11419              later on, and then we wouldn't know whether to sign- or
11420              zero-extend.  */
11421           mode = GET_MODE (XEXP (op0, 0));
11422           if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11423               && ! unsigned_comparison_p
11424               && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11425               && ((unsigned HOST_WIDE_INT) const_op
11426                   < (((unsigned HOST_WIDE_INT) 1
11427                       << (GET_MODE_BITSIZE (mode) - 1))))
11428               && have_insn_for (COMPARE, mode))
11429             {
11430               op0 = XEXP (op0, 0);
11431               continue;
11432             }
11433           break;
11434
11435         case SUBREG:
11436           /* Check for the case where we are comparing A - C1 with C2, that is
11437
11438                (subreg:MODE (plus (A) (-C1))) op (C2)
11439
11440              with C1 a constant, and try to lift the SUBREG, i.e. to do the
11441              comparison in the wider mode.  One of the following two conditions
11442              must be true in order for this to be valid:
11443
11444                1. The mode extension results in the same bit pattern being added
11445                   on both sides and the comparison is equality or unsigned.  As
11446                   C2 has been truncated to fit in MODE, the pattern can only be
11447                   all 0s or all 1s.
11448
11449                2. The mode extension results in the sign bit being copied on
11450                   each side.
11451
11452              The difficulty here is that we have predicates for A but not for
11453              (A - C1) so we need to check that C1 is within proper bounds so
11454              as to perturbate A as little as possible.  */
11455
11456           if (mode_width <= HOST_BITS_PER_WIDE_INT
11457               && subreg_lowpart_p (op0)
11458               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
11459               && GET_CODE (SUBREG_REG (op0)) == PLUS
11460               && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11461             {
11462               enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11463               rtx a = XEXP (SUBREG_REG (op0), 0);
11464               HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11465
11466               if ((c1 > 0
11467                    && (unsigned HOST_WIDE_INT) c1
11468                        < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11469                    && (equality_comparison_p || unsigned_comparison_p)
11470                    /* (A - C1) zero-extends if it is positive and sign-extends
11471                       if it is negative, C2 both zero- and sign-extends.  */
11472                    && ((0 == (nonzero_bits (a, inner_mode)
11473                               & ~GET_MODE_MASK (mode))
11474                         && const_op >= 0)
11475                        /* (A - C1) sign-extends if it is positive and 1-extends
11476                           if it is negative, C2 both sign- and 1-extends.  */
11477                        || (num_sign_bit_copies (a, inner_mode)
11478                            > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
11479                                              - mode_width)
11480                            && const_op < 0)))
11481                   || ((unsigned HOST_WIDE_INT) c1
11482                        < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11483                       /* (A - C1) always sign-extends, like C2.  */
11484                       && num_sign_bit_copies (a, inner_mode)
11485                          > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
11486                                            - (mode_width - 1))))
11487                 {
11488                   op0 = SUBREG_REG (op0);
11489                   continue;
11490                 }
11491             }
11492
11493           /* If the inner mode is narrower and we are extracting the low part,
11494              we can treat the SUBREG as if it were a ZERO_EXTEND.  */
11495           if (subreg_lowpart_p (op0)
11496               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
11497             /* Fall through */ ;
11498           else
11499             break;
11500
11501           /* ... fall through ...  */
11502
11503         case ZERO_EXTEND:
11504           mode = GET_MODE (XEXP (op0, 0));
11505           if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11506               && (unsigned_comparison_p || equality_comparison_p)
11507               && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11508               && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
11509               && have_insn_for (COMPARE, mode))
11510             {
11511               op0 = XEXP (op0, 0);
11512               continue;
11513             }
11514           break;
11515
11516         case PLUS:
11517           /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
11518              this for equality comparisons due to pathological cases involving
11519              overflows.  */
11520           if (equality_comparison_p
11521               && 0 != (tem = simplify_binary_operation (MINUS, mode,
11522                                                         op1, XEXP (op0, 1))))
11523             {
11524               op0 = XEXP (op0, 0);
11525               op1 = tem;
11526               continue;
11527             }
11528
11529           /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
11530           if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11531               && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11532             {
11533               op0 = XEXP (XEXP (op0, 0), 0);
11534               code = (code == LT ? EQ : NE);
11535               continue;
11536             }
11537           break;
11538
11539         case MINUS:
11540           /* We used to optimize signed comparisons against zero, but that
11541              was incorrect.  Unsigned comparisons against zero (GTU, LEU)
11542              arrive here as equality comparisons, or (GEU, LTU) are
11543              optimized away.  No need to special-case them.  */
11544
11545           /* (eq (minus A B) C) -> (eq A (plus B C)) or
11546              (eq B (minus A C)), whichever simplifies.  We can only do
11547              this for equality comparisons due to pathological cases involving
11548              overflows.  */
11549           if (equality_comparison_p
11550               && 0 != (tem = simplify_binary_operation (PLUS, mode,
11551                                                         XEXP (op0, 1), op1)))
11552             {
11553               op0 = XEXP (op0, 0);
11554               op1 = tem;
11555               continue;
11556             }
11557
11558           if (equality_comparison_p
11559               && 0 != (tem = simplify_binary_operation (MINUS, mode,
11560                                                         XEXP (op0, 0), op1)))
11561             {
11562               op0 = XEXP (op0, 1);
11563               op1 = tem;
11564               continue;
11565             }
11566
11567           /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11568              of bits in X minus 1, is one iff X > 0.  */
11569           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11570               && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11571               && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11572               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11573             {
11574               op0 = XEXP (op0, 1);
11575               code = (code == GE ? LE : GT);
11576               continue;
11577             }
11578           break;
11579
11580         case XOR:
11581           /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
11582              if C is zero or B is a constant.  */
11583           if (equality_comparison_p
11584               && 0 != (tem = simplify_binary_operation (XOR, mode,
11585                                                         XEXP (op0, 1), op1)))
11586             {
11587               op0 = XEXP (op0, 0);
11588               op1 = tem;
11589               continue;
11590             }
11591           break;
11592
11593         case EQ:  case NE:
11594         case UNEQ:  case LTGT:
11595         case LT:  case LTU:  case UNLT:  case LE:  case LEU:  case UNLE:
11596         case GT:  case GTU:  case UNGT:  case GE:  case GEU:  case UNGE:
11597         case UNORDERED: case ORDERED:
11598           /* We can't do anything if OP0 is a condition code value, rather
11599              than an actual data value.  */
11600           if (const_op != 0
11601               || CC0_P (XEXP (op0, 0))
11602               || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11603             break;
11604
11605           /* Get the two operands being compared.  */
11606           if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11607             tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11608           else
11609             tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11610
11611           /* Check for the cases where we simply want the result of the
11612              earlier test or the opposite of that result.  */
11613           if (code == NE || code == EQ
11614               || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
11615                   && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11616                   && (STORE_FLAG_VALUE
11617                       & (((unsigned HOST_WIDE_INT) 1
11618                           << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
11619                   && (code == LT || code == GE)))
11620             {
11621               enum rtx_code new_code;
11622               if (code == LT || code == NE)
11623                 new_code = GET_CODE (op0);
11624               else
11625                 new_code = reversed_comparison_code (op0, NULL);
11626
11627               if (new_code != UNKNOWN)
11628                 {
11629                   code = new_code;
11630                   op0 = tem;
11631                   op1 = tem1;
11632                   continue;
11633                 }
11634             }
11635           break;
11636
11637         case IOR:
11638           /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11639              iff X <= 0.  */
11640           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11641               && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11642               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11643             {
11644               op0 = XEXP (op0, 1);
11645               code = (code == GE ? GT : LE);
11646               continue;
11647             }
11648           break;
11649
11650         case AND:
11651           /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
11652              will be converted to a ZERO_EXTRACT later.  */
11653           if (const_op == 0 && equality_comparison_p
11654               && GET_CODE (XEXP (op0, 0)) == ASHIFT
11655               && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11656             {
11657               op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11658                                       XEXP (XEXP (op0, 0), 1));
11659               op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11660               continue;
11661             }
11662
11663           /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11664              zero and X is a comparison and C1 and C2 describe only bits set
11665              in STORE_FLAG_VALUE, we can compare with X.  */
11666           if (const_op == 0 && equality_comparison_p
11667               && mode_width <= HOST_BITS_PER_WIDE_INT
11668               && CONST_INT_P (XEXP (op0, 1))
11669               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11670               && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11671               && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11672               && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11673             {
11674               mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11675                       << INTVAL (XEXP (XEXP (op0, 0), 1)));
11676               if ((~STORE_FLAG_VALUE & mask) == 0
11677                   && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11678                       || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11679                           && COMPARISON_P (tem))))
11680                 {
11681                   op0 = XEXP (XEXP (op0, 0), 0);
11682                   continue;
11683                 }
11684             }
11685
11686           /* If we are doing an equality comparison of an AND of a bit equal
11687              to the sign bit, replace this with a LT or GE comparison of
11688              the underlying value.  */
11689           if (equality_comparison_p
11690               && const_op == 0
11691               && CONST_INT_P (XEXP (op0, 1))
11692               && mode_width <= HOST_BITS_PER_WIDE_INT
11693               && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11694                   == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11695             {
11696               op0 = XEXP (op0, 0);
11697               code = (code == EQ ? GE : LT);
11698               continue;
11699             }
11700
11701           /* If this AND operation is really a ZERO_EXTEND from a narrower
11702              mode, the constant fits within that mode, and this is either an
11703              equality or unsigned comparison, try to do this comparison in
11704              the narrower mode.
11705
11706              Note that in:
11707
11708              (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11709              -> (ne:DI (reg:SI 4) (const_int 0))
11710
11711              unless TRULY_NOOP_TRUNCATION allows it or the register is
11712              known to hold a value of the required mode the
11713              transformation is invalid.  */
11714           if ((equality_comparison_p || unsigned_comparison_p)
11715               && CONST_INT_P (XEXP (op0, 1))
11716               && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11717                                    & GET_MODE_MASK (mode))
11718                                   + 1)) >= 0
11719               && const_op >> i == 0
11720               && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11721               && (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
11722                                          GET_MODE_BITSIZE (GET_MODE (op0)))
11723                   || (REG_P (XEXP (op0, 0))
11724                       && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11725             {
11726               op0 = gen_lowpart (tmode, XEXP (op0, 0));
11727               continue;
11728             }
11729
11730           /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11731              fits in both M1 and M2 and the SUBREG is either paradoxical
11732              or represents the low part, permute the SUBREG and the AND
11733              and try again.  */
11734           if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11735             {
11736               unsigned HOST_WIDE_INT c1;
11737               tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11738               /* Require an integral mode, to avoid creating something like
11739                  (AND:SF ...).  */
11740               if (SCALAR_INT_MODE_P (tmode)
11741                   /* It is unsafe to commute the AND into the SUBREG if the
11742                      SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11743                      not defined.  As originally written the upper bits
11744                      have a defined value due to the AND operation.
11745                      However, if we commute the AND inside the SUBREG then
11746                      they no longer have defined values and the meaning of
11747                      the code has been changed.  */
11748                   && (0
11749 #ifdef WORD_REGISTER_OPERATIONS
11750                       || (mode_width > GET_MODE_BITSIZE (tmode)
11751                           && mode_width <= BITS_PER_WORD)
11752 #endif
11753                       || (mode_width <= GET_MODE_BITSIZE (tmode)
11754                           && subreg_lowpart_p (XEXP (op0, 0))))
11755                   && CONST_INT_P (XEXP (op0, 1))
11756                   && mode_width <= HOST_BITS_PER_WIDE_INT
11757                   && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
11758                   && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11759                   && (c1 & ~GET_MODE_MASK (tmode)) == 0
11760                   && c1 != mask
11761                   && c1 != GET_MODE_MASK (tmode))
11762                 {
11763                   op0 = simplify_gen_binary (AND, tmode,
11764                                              SUBREG_REG (XEXP (op0, 0)),
11765                                              gen_int_mode (c1, tmode));
11766                   op0 = gen_lowpart (mode, op0);
11767                   continue;
11768                 }
11769             }
11770
11771           /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0).  */
11772           if (const_op == 0 && equality_comparison_p
11773               && XEXP (op0, 1) == const1_rtx
11774               && GET_CODE (XEXP (op0, 0)) == NOT)
11775             {
11776               op0 = simplify_and_const_int (NULL_RTX, mode,
11777                                             XEXP (XEXP (op0, 0), 0), 1);
11778               code = (code == NE ? EQ : NE);
11779               continue;
11780             }
11781
11782           /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11783              (eq (and (lshiftrt X) 1) 0).
11784              Also handle the case where (not X) is expressed using xor.  */
11785           if (const_op == 0 && equality_comparison_p
11786               && XEXP (op0, 1) == const1_rtx
11787               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11788             {
11789               rtx shift_op = XEXP (XEXP (op0, 0), 0);
11790               rtx shift_count = XEXP (XEXP (op0, 0), 1);
11791
11792               if (GET_CODE (shift_op) == NOT
11793                   || (GET_CODE (shift_op) == XOR
11794                       && CONST_INT_P (XEXP (shift_op, 1))
11795                       && CONST_INT_P (shift_count)
11796                       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
11797                       && (UINTVAL (XEXP (shift_op, 1))
11798                           == (unsigned HOST_WIDE_INT) 1
11799                                << INTVAL (shift_count))))
11800                 {
11801                   op0
11802                     = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
11803                   op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11804                   code = (code == NE ? EQ : NE);
11805                   continue;
11806                 }
11807             }
11808           break;
11809
11810         case ASHIFT:
11811           /* If we have (compare (ashift FOO N) (const_int C)) and
11812              the high order N bits of FOO (N+1 if an inequality comparison)
11813              are known to be zero, we can do this by comparing FOO with C
11814              shifted right N bits so long as the low-order N bits of C are
11815              zero.  */
11816           if (CONST_INT_P (XEXP (op0, 1))
11817               && INTVAL (XEXP (op0, 1)) >= 0
11818               && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11819                   < HOST_BITS_PER_WIDE_INT)
11820               && (((unsigned HOST_WIDE_INT) const_op
11821                    & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
11822                       - 1)) == 0)
11823               && mode_width <= HOST_BITS_PER_WIDE_INT
11824               && (nonzero_bits (XEXP (op0, 0), mode)
11825                   & ~(mask >> (INTVAL (XEXP (op0, 1))
11826                                + ! equality_comparison_p))) == 0)
11827             {
11828               /* We must perform a logical shift, not an arithmetic one,
11829                  as we want the top N bits of C to be zero.  */
11830               unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11831
11832               temp >>= INTVAL (XEXP (op0, 1));
11833               op1 = gen_int_mode (temp, mode);
11834               op0 = XEXP (op0, 0);
11835               continue;
11836             }
11837
11838           /* If we are doing a sign bit comparison, it means we are testing
11839              a particular bit.  Convert it to the appropriate AND.  */
11840           if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
11841               && mode_width <= HOST_BITS_PER_WIDE_INT)
11842             {
11843               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11844                                             ((unsigned HOST_WIDE_INT) 1
11845                                              << (mode_width - 1
11846                                                  - INTVAL (XEXP (op0, 1)))));
11847               code = (code == LT ? NE : EQ);
11848               continue;
11849             }
11850
11851           /* If this an equality comparison with zero and we are shifting
11852              the low bit to the sign bit, we can convert this to an AND of the
11853              low-order bit.  */
11854           if (const_op == 0 && equality_comparison_p
11855               && CONST_INT_P (XEXP (op0, 1))
11856               && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11857             {
11858               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
11859               continue;
11860             }
11861           break;
11862
11863         case ASHIFTRT:
11864           /* If this is an equality comparison with zero, we can do this
11865              as a logical shift, which might be much simpler.  */
11866           if (equality_comparison_p && const_op == 0
11867               && CONST_INT_P (XEXP (op0, 1)))
11868             {
11869               op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11870                                           XEXP (op0, 0),
11871                                           INTVAL (XEXP (op0, 1)));
11872               continue;
11873             }
11874
11875           /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11876              do the comparison in a narrower mode.  */
11877           if (! unsigned_comparison_p
11878               && CONST_INT_P (XEXP (op0, 1))
11879               && GET_CODE (XEXP (op0, 0)) == ASHIFT
11880               && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11881               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11882                                          MODE_INT, 1)) != BLKmode
11883               && (((unsigned HOST_WIDE_INT) const_op
11884                    + (GET_MODE_MASK (tmode) >> 1) + 1)
11885                   <= GET_MODE_MASK (tmode)))
11886             {
11887               op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
11888               continue;
11889             }
11890
11891           /* Likewise if OP0 is a PLUS of a sign extension with a
11892              constant, which is usually represented with the PLUS
11893              between the shifts.  */
11894           if (! unsigned_comparison_p
11895               && CONST_INT_P (XEXP (op0, 1))
11896               && GET_CODE (XEXP (op0, 0)) == PLUS
11897               && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11898               && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11899               && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11900               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11901                                          MODE_INT, 1)) != BLKmode
11902               && (((unsigned HOST_WIDE_INT) const_op
11903                    + (GET_MODE_MASK (tmode) >> 1) + 1)
11904                   <= GET_MODE_MASK (tmode)))
11905             {
11906               rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11907               rtx add_const = XEXP (XEXP (op0, 0), 1);
11908               rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
11909                                                    add_const, XEXP (op0, 1));
11910
11911               op0 = simplify_gen_binary (PLUS, tmode,
11912                                          gen_lowpart (tmode, inner),
11913                                          new_const);
11914               continue;
11915             }
11916
11917           /* ... fall through ...  */
11918         case LSHIFTRT:
11919           /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11920              the low order N bits of FOO are known to be zero, we can do this
11921              by comparing FOO with C shifted left N bits so long as no
11922              overflow occurs.  Even if the low order N bits of FOO aren't known
11923              to be zero, if the comparison is >= or < we can use the same
11924              optimization and for > or <= by setting all the low
11925              order N bits in the comparison constant.  */
11926           if (CONST_INT_P (XEXP (op0, 1))
11927               && INTVAL (XEXP (op0, 1)) > 0
11928               && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11929               && mode_width <= HOST_BITS_PER_WIDE_INT
11930               && (((unsigned HOST_WIDE_INT) const_op
11931                    + (GET_CODE (op0) != LSHIFTRT
11932                       ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11933                          + 1)
11934                       : 0))
11935                   <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11936             {
11937               unsigned HOST_WIDE_INT low_bits
11938                 = (nonzero_bits (XEXP (op0, 0), mode)
11939                    & (((unsigned HOST_WIDE_INT) 1
11940                        << INTVAL (XEXP (op0, 1))) - 1));
11941               if (low_bits == 0 || !equality_comparison_p)
11942                 {
11943                   /* If the shift was logical, then we must make the condition
11944                      unsigned.  */
11945                   if (GET_CODE (op0) == LSHIFTRT)
11946                     code = unsigned_condition (code);
11947
11948                   const_op <<= INTVAL (XEXP (op0, 1));
11949                   if (low_bits != 0
11950                       && (code == GT || code == GTU
11951                           || code == LE || code == LEU))
11952                     const_op
11953                       |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
11954                   op1 = GEN_INT (const_op);
11955                   op0 = XEXP (op0, 0);
11956                   continue;
11957                 }
11958             }
11959
11960           /* If we are using this shift to extract just the sign bit, we
11961              can replace this with an LT or GE comparison.  */
11962           if (const_op == 0
11963               && (equality_comparison_p || sign_bit_comparison_p)
11964               && CONST_INT_P (XEXP (op0, 1))
11965               && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11966             {
11967               op0 = XEXP (op0, 0);
11968               code = (code == NE || code == GT ? LT : GE);
11969               continue;
11970             }
11971           break;
11972
11973         default:
11974           break;
11975         }
11976
11977       break;
11978     }
11979
11980   /* Now make any compound operations involved in this comparison.  Then,
11981      check for an outmost SUBREG on OP0 that is not doing anything or is
11982      paradoxical.  The latter transformation must only be performed when
11983      it is known that the "extra" bits will be the same in op0 and op1 or
11984      that they don't matter.  There are three cases to consider:
11985
11986      1. SUBREG_REG (op0) is a register.  In this case the bits are don't
11987      care bits and we can assume they have any convenient value.  So
11988      making the transformation is safe.
11989
11990      2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11991      In this case the upper bits of op0 are undefined.  We should not make
11992      the simplification in that case as we do not know the contents of
11993      those bits.
11994
11995      3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11996      UNKNOWN.  In that case we know those bits are zeros or ones.  We must
11997      also be sure that they are the same as the upper bits of op1.
11998
11999      We can never remove a SUBREG for a non-equality comparison because
12000      the sign bit is in a different place in the underlying object.  */
12001
12002   op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
12003   op1 = make_compound_operation (op1, SET);
12004
12005   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
12006       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
12007       && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
12008       && (code == NE || code == EQ))
12009     {
12010       if (GET_MODE_SIZE (GET_MODE (op0))
12011           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
12012         {
12013           /* For paradoxical subregs, allow case 1 as above.  Case 3 isn't
12014              implemented.  */
12015           if (REG_P (SUBREG_REG (op0)))
12016             {
12017               op0 = SUBREG_REG (op0);
12018               op1 = gen_lowpart (GET_MODE (op0), op1);
12019             }
12020         }
12021       else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
12022                 <= HOST_BITS_PER_WIDE_INT)
12023                && (nonzero_bits (SUBREG_REG (op0),
12024                                  GET_MODE (SUBREG_REG (op0)))
12025                    & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12026         {
12027           tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
12028
12029           if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
12030                & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12031             op0 = SUBREG_REG (op0), op1 = tem;
12032         }
12033     }
12034
12035   /* We now do the opposite procedure: Some machines don't have compare
12036      insns in all modes.  If OP0's mode is an integer mode smaller than a
12037      word and we can't do a compare in that mode, see if there is a larger
12038      mode for which we can do the compare.  There are a number of cases in
12039      which we can use the wider mode.  */
12040
12041   mode = GET_MODE (op0);
12042   if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
12043       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12044       && ! have_insn_for (COMPARE, mode))
12045     for (tmode = GET_MODE_WIDER_MODE (mode);
12046          (tmode != VOIDmode
12047           && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
12048          tmode = GET_MODE_WIDER_MODE (tmode))
12049       if (have_insn_for (COMPARE, tmode))
12050         {
12051           int zero_extended;
12052
12053           /* If this is a test for negative, we can make an explicit
12054              test of the sign bit.  Test this first so we can use
12055              a paradoxical subreg to extend OP0.  */
12056
12057           if (op1 == const0_rtx && (code == LT || code == GE)
12058               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12059             {
12060               op0 = simplify_gen_binary (AND, tmode,
12061                                          gen_lowpart (tmode, op0),
12062                                          GEN_INT ((unsigned HOST_WIDE_INT) 1
12063                                                   << (GET_MODE_BITSIZE (mode)
12064                                                       - 1)));
12065               code = (code == LT) ? NE : EQ;
12066               break;
12067             }
12068
12069           /* If the only nonzero bits in OP0 and OP1 are those in the
12070              narrower mode and this is an equality or unsigned comparison,
12071              we can use the wider mode.  Similarly for sign-extended
12072              values, in which case it is true for all comparisons.  */
12073           zero_extended = ((code == EQ || code == NE
12074                             || code == GEU || code == GTU
12075                             || code == LEU || code == LTU)
12076                            && (nonzero_bits (op0, tmode)
12077                                & ~GET_MODE_MASK (mode)) == 0
12078                            && ((CONST_INT_P (op1)
12079                                 || (nonzero_bits (op1, tmode)
12080                                     & ~GET_MODE_MASK (mode)) == 0)));
12081
12082           if (zero_extended
12083               || ((num_sign_bit_copies (op0, tmode)
12084                    > (unsigned int) (GET_MODE_BITSIZE (tmode)
12085                                      - GET_MODE_BITSIZE (mode)))
12086                   && (num_sign_bit_copies (op1, tmode)
12087                       > (unsigned int) (GET_MODE_BITSIZE (tmode)
12088                                         - GET_MODE_BITSIZE (mode)))))
12089             {
12090               /* If OP0 is an AND and we don't have an AND in MODE either,
12091                  make a new AND in the proper mode.  */
12092               if (GET_CODE (op0) == AND
12093                   && !have_insn_for (AND, mode))
12094                 op0 = simplify_gen_binary (AND, tmode,
12095                                            gen_lowpart (tmode,
12096                                                         XEXP (op0, 0)),
12097                                            gen_lowpart (tmode,
12098                                                         XEXP (op0, 1)));
12099               else
12100                 {
12101                   if (zero_extended)
12102                     {
12103                       op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
12104                       op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
12105                     }
12106                   else
12107                     {
12108                       op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
12109                       op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
12110                     }
12111                   break;
12112                 }
12113             }
12114         }
12115
12116 #ifdef CANONICALIZE_COMPARISON
12117   /* If this machine only supports a subset of valid comparisons, see if we
12118      can convert an unsupported one into a supported one.  */
12119   CANONICALIZE_COMPARISON (code, op0, op1);
12120 #endif
12121
12122   *pop0 = op0;
12123   *pop1 = op1;
12124
12125   return code;
12126 }
12127 \f
12128 /* Utility function for record_value_for_reg.  Count number of
12129    rtxs in X.  */
12130 static int
12131 count_rtxs (rtx x)
12132 {
12133   enum rtx_code code = GET_CODE (x);
12134   const char *fmt;
12135   int i, j, ret = 1;
12136
12137   if (GET_RTX_CLASS (code) == '2'
12138       || GET_RTX_CLASS (code) == 'c')
12139     {
12140       rtx x0 = XEXP (x, 0);
12141       rtx x1 = XEXP (x, 1);
12142
12143       if (x0 == x1)
12144         return 1 + 2 * count_rtxs (x0);
12145
12146       if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
12147            || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
12148           && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12149         return 2 + 2 * count_rtxs (x0)
12150                + count_rtxs (x == XEXP (x1, 0)
12151                              ? XEXP (x1, 1) : XEXP (x1, 0));
12152
12153       if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
12154            || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
12155           && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12156         return 2 + 2 * count_rtxs (x1)
12157                + count_rtxs (x == XEXP (x0, 0)
12158                              ? XEXP (x0, 1) : XEXP (x0, 0));
12159     }
12160
12161   fmt = GET_RTX_FORMAT (code);
12162   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12163     if (fmt[i] == 'e')
12164       ret += count_rtxs (XEXP (x, i));
12165     else if (fmt[i] == 'E')
12166       for (j = 0; j < XVECLEN (x, i); j++)
12167         ret += count_rtxs (XVECEXP (x, i, j));
12168
12169   return ret;
12170 }
12171 \f
12172 /* Utility function for following routine.  Called when X is part of a value
12173    being stored into last_set_value.  Sets last_set_table_tick
12174    for each register mentioned.  Similar to mention_regs in cse.c  */
12175
12176 static void
12177 update_table_tick (rtx x)
12178 {
12179   enum rtx_code code = GET_CODE (x);
12180   const char *fmt = GET_RTX_FORMAT (code);
12181   int i, j;
12182
12183   if (code == REG)
12184     {
12185       unsigned int regno = REGNO (x);
12186       unsigned int endregno = END_REGNO (x);
12187       unsigned int r;
12188
12189       for (r = regno; r < endregno; r++)
12190         {
12191           reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, r);
12192           rsp->last_set_table_tick = label_tick;
12193         }
12194
12195       return;
12196     }
12197
12198   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12199     if (fmt[i] == 'e')
12200       {
12201         /* Check for identical subexpressions.  If x contains
12202            identical subexpression we only have to traverse one of
12203            them.  */
12204         if (i == 0 && ARITHMETIC_P (x))
12205           {
12206             /* Note that at this point x1 has already been
12207                processed.  */
12208             rtx x0 = XEXP (x, 0);
12209             rtx x1 = XEXP (x, 1);
12210
12211             /* If x0 and x1 are identical then there is no need to
12212                process x0.  */
12213             if (x0 == x1)
12214               break;
12215
12216             /* If x0 is identical to a subexpression of x1 then while
12217                processing x1, x0 has already been processed.  Thus we
12218                are done with x.  */
12219             if (ARITHMETIC_P (x1)
12220                 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12221               break;
12222
12223             /* If x1 is identical to a subexpression of x0 then we
12224                still have to process the rest of x0.  */
12225             if (ARITHMETIC_P (x0)
12226                 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12227               {
12228                 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12229                 break;
12230               }
12231           }
12232
12233         update_table_tick (XEXP (x, i));
12234       }
12235     else if (fmt[i] == 'E')
12236       for (j = 0; j < XVECLEN (x, i); j++)
12237         update_table_tick (XVECEXP (x, i, j));
12238 }
12239
12240 /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
12241    are saying that the register is clobbered and we no longer know its
12242    value.  If INSN is zero, don't update reg_stat[].last_set; this is
12243    only permitted with VALUE also zero and is used to invalidate the
12244    register.  */
12245
12246 static void
12247 record_value_for_reg (rtx reg, rtx insn, rtx value)
12248 {
12249   unsigned int regno = REGNO (reg);
12250   unsigned int endregno = END_REGNO (reg);
12251   unsigned int i;
12252   reg_stat_type *rsp;
12253
12254   /* If VALUE contains REG and we have a previous value for REG, substitute
12255      the previous value.  */
12256   if (value && insn && reg_overlap_mentioned_p (reg, value))
12257     {
12258       rtx tem;
12259
12260       /* Set things up so get_last_value is allowed to see anything set up to
12261          our insn.  */
12262       subst_low_luid = DF_INSN_LUID (insn);
12263       tem = get_last_value (reg);
12264
12265       /* If TEM is simply a binary operation with two CLOBBERs as operands,
12266          it isn't going to be useful and will take a lot of time to process,
12267          so just use the CLOBBER.  */
12268
12269       if (tem)
12270         {
12271           if (ARITHMETIC_P (tem)
12272               && GET_CODE (XEXP (tem, 0)) == CLOBBER
12273               && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12274             tem = XEXP (tem, 0);
12275           else if (count_occurrences (value, reg, 1) >= 2)
12276             {
12277               /* If there are two or more occurrences of REG in VALUE,
12278                  prevent the value from growing too much.  */
12279               if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12280                 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12281             }
12282
12283           value = replace_rtx (copy_rtx (value), reg, tem);
12284         }
12285     }
12286
12287   /* For each register modified, show we don't know its value, that
12288      we don't know about its bitwise content, that its value has been
12289      updated, and that we don't know the location of the death of the
12290      register.  */
12291   for (i = regno; i < endregno; i++)
12292     {
12293       rsp = VEC_index (reg_stat_type, reg_stat, i);
12294
12295       if (insn)
12296         rsp->last_set = insn;
12297
12298       rsp->last_set_value = 0;
12299       rsp->last_set_mode = VOIDmode;
12300       rsp->last_set_nonzero_bits = 0;
12301       rsp->last_set_sign_bit_copies = 0;
12302       rsp->last_death = 0;
12303       rsp->truncated_to_mode = VOIDmode;
12304     }
12305
12306   /* Mark registers that are being referenced in this value.  */
12307   if (value)
12308     update_table_tick (value);
12309
12310   /* Now update the status of each register being set.
12311      If someone is using this register in this block, set this register
12312      to invalid since we will get confused between the two lives in this
12313      basic block.  This makes using this register always invalid.  In cse, we
12314      scan the table to invalidate all entries using this register, but this
12315      is too much work for us.  */
12316
12317   for (i = regno; i < endregno; i++)
12318     {
12319       rsp = VEC_index (reg_stat_type, reg_stat, i);
12320       rsp->last_set_label = label_tick;
12321       if (!insn
12322           || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12323         rsp->last_set_invalid = 1;
12324       else
12325         rsp->last_set_invalid = 0;
12326     }
12327
12328   /* The value being assigned might refer to X (like in "x++;").  In that
12329      case, we must replace it with (clobber (const_int 0)) to prevent
12330      infinite loops.  */
12331   rsp = VEC_index (reg_stat_type, reg_stat, regno);
12332   if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12333     {
12334       value = copy_rtx (value);
12335       if (!get_last_value_validate (&value, insn, label_tick, 1))
12336         value = 0;
12337     }
12338
12339   /* For the main register being modified, update the value, the mode, the
12340      nonzero bits, and the number of sign bit copies.  */
12341
12342   rsp->last_set_value = value;
12343
12344   if (value)
12345     {
12346       enum machine_mode mode = GET_MODE (reg);
12347       subst_low_luid = DF_INSN_LUID (insn);
12348       rsp->last_set_mode = mode;
12349       if (GET_MODE_CLASS (mode) == MODE_INT
12350           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12351         mode = nonzero_bits_mode;
12352       rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12353       rsp->last_set_sign_bit_copies
12354         = num_sign_bit_copies (value, GET_MODE (reg));
12355     }
12356 }
12357
12358 /* Called via note_stores from record_dead_and_set_regs to handle one
12359    SET or CLOBBER in an insn.  DATA is the instruction in which the
12360    set is occurring.  */
12361
12362 static void
12363 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12364 {
12365   rtx record_dead_insn = (rtx) data;
12366
12367   if (GET_CODE (dest) == SUBREG)
12368     dest = SUBREG_REG (dest);
12369
12370   if (!record_dead_insn)
12371     {
12372       if (REG_P (dest))
12373         record_value_for_reg (dest, NULL_RTX, NULL_RTX);
12374       return;
12375     }
12376
12377   if (REG_P (dest))
12378     {
12379       /* If we are setting the whole register, we know its value.  Otherwise
12380          show that we don't know the value.  We can handle SUBREG in
12381          some cases.  */
12382       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12383         record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12384       else if (GET_CODE (setter) == SET
12385                && GET_CODE (SET_DEST (setter)) == SUBREG
12386                && SUBREG_REG (SET_DEST (setter)) == dest
12387                && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
12388                && subreg_lowpart_p (SET_DEST (setter)))
12389         record_value_for_reg (dest, record_dead_insn,
12390                               gen_lowpart (GET_MODE (dest),
12391                                                        SET_SRC (setter)));
12392       else
12393         record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12394     }
12395   else if (MEM_P (dest)
12396            /* Ignore pushes, they clobber nothing.  */
12397            && ! push_operand (dest, GET_MODE (dest)))
12398     mem_last_set = DF_INSN_LUID (record_dead_insn);
12399 }
12400
12401 /* Update the records of when each REG was most recently set or killed
12402    for the things done by INSN.  This is the last thing done in processing
12403    INSN in the combiner loop.
12404
12405    We update reg_stat[], in particular fields last_set, last_set_value,
12406    last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12407    last_death, and also the similar information mem_last_set (which insn
12408    most recently modified memory) and last_call_luid (which insn was the
12409    most recent subroutine call).  */
12410
12411 static void
12412 record_dead_and_set_regs (rtx insn)
12413 {
12414   rtx link;
12415   unsigned int i;
12416
12417   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12418     {
12419       if (REG_NOTE_KIND (link) == REG_DEAD
12420           && REG_P (XEXP (link, 0)))
12421         {
12422           unsigned int regno = REGNO (XEXP (link, 0));
12423           unsigned int endregno = END_REGNO (XEXP (link, 0));
12424
12425           for (i = regno; i < endregno; i++)
12426             {
12427               reg_stat_type *rsp;
12428
12429               rsp = VEC_index (reg_stat_type, reg_stat, i);
12430               rsp->last_death = insn;
12431             }
12432         }
12433       else if (REG_NOTE_KIND (link) == REG_INC)
12434         record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12435     }
12436
12437   if (CALL_P (insn))
12438     {
12439       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
12440         if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
12441           {
12442             reg_stat_type *rsp;
12443
12444             rsp = VEC_index (reg_stat_type, reg_stat, i);
12445             rsp->last_set_invalid = 1;
12446             rsp->last_set = insn;
12447             rsp->last_set_value = 0;
12448             rsp->last_set_mode = VOIDmode;
12449             rsp->last_set_nonzero_bits = 0;
12450             rsp->last_set_sign_bit_copies = 0;
12451             rsp->last_death = 0;
12452             rsp->truncated_to_mode = VOIDmode;
12453           }
12454
12455       last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12456
12457       /* We can't combine into a call pattern.  Remember, though, that
12458          the return value register is set at this LUID.  We could
12459          still replace a register with the return value from the
12460          wrong subroutine call!  */
12461       note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12462     }
12463   else
12464     note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12465 }
12466
12467 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12468    register present in the SUBREG, so for each such SUBREG go back and
12469    adjust nonzero and sign bit information of the registers that are
12470    known to have some zero/sign bits set.
12471
12472    This is needed because when combine blows the SUBREGs away, the
12473    information on zero/sign bits is lost and further combines can be
12474    missed because of that.  */
12475
12476 static void
12477 record_promoted_value (rtx insn, rtx subreg)
12478 {
12479   struct insn_link *links;
12480   rtx set;
12481   unsigned int regno = REGNO (SUBREG_REG (subreg));
12482   enum machine_mode mode = GET_MODE (subreg);
12483
12484   if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
12485     return;
12486
12487   for (links = LOG_LINKS (insn); links;)
12488     {
12489       reg_stat_type *rsp;
12490
12491       insn = links->insn;
12492       set = single_set (insn);
12493
12494       if (! set || !REG_P (SET_DEST (set))
12495           || REGNO (SET_DEST (set)) != regno
12496           || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12497         {
12498           links = links->next;
12499           continue;
12500         }
12501
12502       rsp = VEC_index (reg_stat_type, reg_stat, regno);
12503       if (rsp->last_set == insn)
12504         {
12505           if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
12506             rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12507         }
12508
12509       if (REG_P (SET_SRC (set)))
12510         {
12511           regno = REGNO (SET_SRC (set));
12512           links = LOG_LINKS (insn);
12513         }
12514       else
12515         break;
12516     }
12517 }
12518
12519 /* Check if X, a register, is known to contain a value already
12520    truncated to MODE.  In this case we can use a subreg to refer to
12521    the truncated value even though in the generic case we would need
12522    an explicit truncation.  */
12523
12524 static bool
12525 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
12526 {
12527   reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
12528   enum machine_mode truncated = rsp->truncated_to_mode;
12529
12530   if (truncated == 0
12531       || rsp->truncation_label < label_tick_ebb_start)
12532     return false;
12533   if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12534     return true;
12535   if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
12536                              GET_MODE_BITSIZE (truncated)))
12537     return true;
12538   return false;
12539 }
12540
12541 /* Callback for for_each_rtx.  If *P is a hard reg or a subreg record the mode
12542    that the register is accessed in.  For non-TRULY_NOOP_TRUNCATION targets we
12543    might be able to turn a truncate into a subreg using this information.
12544    Return -1 if traversing *P is complete or 0 otherwise.  */
12545
12546 static int
12547 record_truncated_value (rtx *p, void *data ATTRIBUTE_UNUSED)
12548 {
12549   rtx x = *p;
12550   enum machine_mode truncated_mode;
12551   reg_stat_type *rsp;
12552
12553   if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12554     {
12555       enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12556       truncated_mode = GET_MODE (x);
12557
12558       if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12559         return -1;
12560
12561       if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (truncated_mode),
12562                                  GET_MODE_BITSIZE (original_mode)))
12563         return -1;
12564
12565       x = SUBREG_REG (x);
12566     }
12567   /* ??? For hard-regs we now record everything.  We might be able to
12568      optimize this using last_set_mode.  */
12569   else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12570     truncated_mode = GET_MODE (x);
12571   else
12572     return 0;
12573
12574   rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
12575   if (rsp->truncated_to_mode == 0
12576       || rsp->truncation_label < label_tick_ebb_start
12577       || (GET_MODE_SIZE (truncated_mode)
12578           < GET_MODE_SIZE (rsp->truncated_to_mode)))
12579     {
12580       rsp->truncated_to_mode = truncated_mode;
12581       rsp->truncation_label = label_tick;
12582     }
12583
12584   return -1;
12585 }
12586
12587 /* Callback for note_uses.  Find hardregs and subregs of pseudos and
12588    the modes they are used in.  This can help truning TRUNCATEs into
12589    SUBREGs.  */
12590
12591 static void
12592 record_truncated_values (rtx *x, void *data ATTRIBUTE_UNUSED)
12593 {
12594   for_each_rtx (x, record_truncated_value, NULL);
12595 }
12596
12597 /* Scan X for promoted SUBREGs.  For each one found,
12598    note what it implies to the registers used in it.  */
12599
12600 static void
12601 check_promoted_subreg (rtx insn, rtx x)
12602 {
12603   if (GET_CODE (x) == SUBREG
12604       && SUBREG_PROMOTED_VAR_P (x)
12605       && REG_P (SUBREG_REG (x)))
12606     record_promoted_value (insn, x);
12607   else
12608     {
12609       const char *format = GET_RTX_FORMAT (GET_CODE (x));
12610       int i, j;
12611
12612       for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12613         switch (format[i])
12614           {
12615           case 'e':
12616             check_promoted_subreg (insn, XEXP (x, i));
12617             break;
12618           case 'V':
12619           case 'E':
12620             if (XVEC (x, i) != 0)
12621               for (j = 0; j < XVECLEN (x, i); j++)
12622                 check_promoted_subreg (insn, XVECEXP (x, i, j));
12623             break;
12624           }
12625     }
12626 }
12627 \f
12628 /* Verify that all the registers and memory references mentioned in *LOC are
12629    still valid.  *LOC was part of a value set in INSN when label_tick was
12630    equal to TICK.  Return 0 if some are not.  If REPLACE is nonzero, replace
12631    the invalid references with (clobber (const_int 0)) and return 1.  This
12632    replacement is useful because we often can get useful information about
12633    the form of a value (e.g., if it was produced by a shift that always
12634    produces -1 or 0) even though we don't know exactly what registers it
12635    was produced from.  */
12636
12637 static int
12638 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
12639 {
12640   rtx x = *loc;
12641   const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12642   int len = GET_RTX_LENGTH (GET_CODE (x));
12643   int i, j;
12644
12645   if (REG_P (x))
12646     {
12647       unsigned int regno = REGNO (x);
12648       unsigned int endregno = END_REGNO (x);
12649       unsigned int j;
12650
12651       for (j = regno; j < endregno; j++)
12652         {
12653           reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, j);
12654           if (rsp->last_set_invalid
12655               /* If this is a pseudo-register that was only set once and not
12656                  live at the beginning of the function, it is always valid.  */
12657               || (! (regno >= FIRST_PSEUDO_REGISTER
12658                      && REG_N_SETS (regno) == 1
12659                      && (!REGNO_REG_SET_P
12660                          (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno)))
12661                   && rsp->last_set_label > tick))
12662           {
12663             if (replace)
12664               *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12665             return replace;
12666           }
12667         }
12668
12669       return 1;
12670     }
12671   /* If this is a memory reference, make sure that there were no stores after
12672      it that might have clobbered the value.  We don't have alias info, so we
12673      assume any store invalidates it.  Moreover, we only have local UIDs, so
12674      we also assume that there were stores in the intervening basic blocks.  */
12675   else if (MEM_P (x) && !MEM_READONLY_P (x)
12676            && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12677     {
12678       if (replace)
12679         *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12680       return replace;
12681     }
12682
12683   for (i = 0; i < len; i++)
12684     {
12685       if (fmt[i] == 'e')
12686         {
12687           /* Check for identical subexpressions.  If x contains
12688              identical subexpression we only have to traverse one of
12689              them.  */
12690           if (i == 1 && ARITHMETIC_P (x))
12691             {
12692               /* Note that at this point x0 has already been checked
12693                  and found valid.  */
12694               rtx x0 = XEXP (x, 0);
12695               rtx x1 = XEXP (x, 1);
12696
12697               /* If x0 and x1 are identical then x is also valid.  */
12698               if (x0 == x1)
12699                 return 1;
12700
12701               /* If x1 is identical to a subexpression of x0 then
12702                  while checking x0, x1 has already been checked.  Thus
12703                  it is valid and so as x.  */
12704               if (ARITHMETIC_P (x0)
12705                   && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12706                 return 1;
12707
12708               /* If x0 is identical to a subexpression of x1 then x is
12709                  valid iff the rest of x1 is valid.  */
12710               if (ARITHMETIC_P (x1)
12711                   && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12712                 return
12713                   get_last_value_validate (&XEXP (x1,
12714                                                   x0 == XEXP (x1, 0) ? 1 : 0),
12715                                            insn, tick, replace);
12716             }
12717
12718           if (get_last_value_validate (&XEXP (x, i), insn, tick,
12719                                        replace) == 0)
12720             return 0;
12721         }
12722       else if (fmt[i] == 'E')
12723         for (j = 0; j < XVECLEN (x, i); j++)
12724           if (get_last_value_validate (&XVECEXP (x, i, j),
12725                                        insn, tick, replace) == 0)
12726             return 0;
12727     }
12728
12729   /* If we haven't found a reason for it to be invalid, it is valid.  */
12730   return 1;
12731 }
12732
12733 /* Get the last value assigned to X, if known.  Some registers
12734    in the value may be replaced with (clobber (const_int 0)) if their value
12735    is known longer known reliably.  */
12736
12737 static rtx
12738 get_last_value (const_rtx x)
12739 {
12740   unsigned int regno;
12741   rtx value;
12742   reg_stat_type *rsp;
12743
12744   /* If this is a non-paradoxical SUBREG, get the value of its operand and
12745      then convert it to the desired mode.  If this is a paradoxical SUBREG,
12746      we cannot predict what values the "extra" bits might have.  */
12747   if (GET_CODE (x) == SUBREG
12748       && subreg_lowpart_p (x)
12749       && (GET_MODE_SIZE (GET_MODE (x))
12750           <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
12751       && (value = get_last_value (SUBREG_REG (x))) != 0)
12752     return gen_lowpart (GET_MODE (x), value);
12753
12754   if (!REG_P (x))
12755     return 0;
12756
12757   regno = REGNO (x);
12758   rsp = VEC_index (reg_stat_type, reg_stat, regno);
12759   value = rsp->last_set_value;
12760
12761   /* If we don't have a value, or if it isn't for this basic block and
12762      it's either a hard register, set more than once, or it's a live
12763      at the beginning of the function, return 0.
12764
12765      Because if it's not live at the beginning of the function then the reg
12766      is always set before being used (is never used without being set).
12767      And, if it's set only once, and it's always set before use, then all
12768      uses must have the same last value, even if it's not from this basic
12769      block.  */
12770
12771   if (value == 0
12772       || (rsp->last_set_label < label_tick_ebb_start
12773           && (regno < FIRST_PSEUDO_REGISTER
12774               || REG_N_SETS (regno) != 1
12775               || REGNO_REG_SET_P
12776                  (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno))))
12777     return 0;
12778
12779   /* If the value was set in a later insn than the ones we are processing,
12780      we can't use it even if the register was only set once.  */
12781   if (rsp->last_set_label == label_tick
12782       && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12783     return 0;
12784
12785   /* If the value has all its registers valid, return it.  */
12786   if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12787     return value;
12788
12789   /* Otherwise, make a copy and replace any invalid register with
12790      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
12791
12792   value = copy_rtx (value);
12793   if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
12794     return value;
12795
12796   return 0;
12797 }
12798 \f
12799 /* Return nonzero if expression X refers to a REG or to memory
12800    that is set in an instruction more recent than FROM_LUID.  */
12801
12802 static int
12803 use_crosses_set_p (const_rtx x, int from_luid)
12804 {
12805   const char *fmt;
12806   int i;
12807   enum rtx_code code = GET_CODE (x);
12808
12809   if (code == REG)
12810     {
12811       unsigned int regno = REGNO (x);
12812       unsigned endreg = END_REGNO (x);
12813
12814 #ifdef PUSH_ROUNDING
12815       /* Don't allow uses of the stack pointer to be moved,
12816          because we don't know whether the move crosses a push insn.  */
12817       if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12818         return 1;
12819 #endif
12820       for (; regno < endreg; regno++)
12821         {
12822           reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
12823           if (rsp->last_set
12824               && rsp->last_set_label == label_tick
12825               && DF_INSN_LUID (rsp->last_set) > from_luid)
12826             return 1;
12827         }
12828       return 0;
12829     }
12830
12831   if (code == MEM && mem_last_set > from_luid)
12832     return 1;
12833
12834   fmt = GET_RTX_FORMAT (code);
12835
12836   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12837     {
12838       if (fmt[i] == 'E')
12839         {
12840           int j;
12841           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12842             if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
12843               return 1;
12844         }
12845       else if (fmt[i] == 'e'
12846                && use_crosses_set_p (XEXP (x, i), from_luid))
12847         return 1;
12848     }
12849   return 0;
12850 }
12851 \f
12852 /* Define three variables used for communication between the following
12853    routines.  */
12854
12855 static unsigned int reg_dead_regno, reg_dead_endregno;
12856 static int reg_dead_flag;
12857
12858 /* Function called via note_stores from reg_dead_at_p.
12859
12860    If DEST is within [reg_dead_regno, reg_dead_endregno), set
12861    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
12862
12863 static void
12864 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
12865 {
12866   unsigned int regno, endregno;
12867
12868   if (!REG_P (dest))
12869     return;
12870
12871   regno = REGNO (dest);
12872   endregno = END_REGNO (dest);
12873   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12874     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12875 }
12876
12877 /* Return nonzero if REG is known to be dead at INSN.
12878
12879    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
12880    referencing REG, it is dead.  If we hit a SET referencing REG, it is
12881    live.  Otherwise, see if it is live or dead at the start of the basic
12882    block we are in.  Hard regs marked as being live in NEWPAT_USED_REGS
12883    must be assumed to be always live.  */
12884
12885 static int
12886 reg_dead_at_p (rtx reg, rtx insn)
12887 {
12888   basic_block block;
12889   unsigned int i;
12890
12891   /* Set variables for reg_dead_at_p_1.  */
12892   reg_dead_regno = REGNO (reg);
12893   reg_dead_endregno = END_REGNO (reg);
12894
12895   reg_dead_flag = 0;
12896
12897   /* Check that reg isn't mentioned in NEWPAT_USED_REGS.  For fixed registers
12898      we allow the machine description to decide whether use-and-clobber
12899      patterns are OK.  */
12900   if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12901     {
12902       for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12903         if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
12904           return 0;
12905     }
12906
12907   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
12908      beginning of basic block.  */
12909   block = BLOCK_FOR_INSN (insn);
12910   for (;;)
12911     {
12912       if (INSN_P (insn))
12913         {
12914           note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12915           if (reg_dead_flag)
12916             return reg_dead_flag == 1 ? 1 : 0;
12917
12918           if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12919             return 1;
12920         }
12921
12922       if (insn == BB_HEAD (block))
12923         break;
12924
12925       insn = PREV_INSN (insn);
12926     }
12927
12928   /* Look at live-in sets for the basic block that we were in.  */
12929   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12930     if (REGNO_REG_SET_P (df_get_live_in (block), i))
12931       return 0;
12932
12933   return 1;
12934 }
12935 \f
12936 /* Note hard registers in X that are used.  */
12937
12938 static void
12939 mark_used_regs_combine (rtx x)
12940 {
12941   RTX_CODE code = GET_CODE (x);
12942   unsigned int regno;
12943   int i;
12944
12945   switch (code)
12946     {
12947     case LABEL_REF:
12948     case SYMBOL_REF:
12949     case CONST_INT:
12950     case CONST:
12951     case CONST_DOUBLE:
12952     case CONST_VECTOR:
12953     case PC:
12954     case ADDR_VEC:
12955     case ADDR_DIFF_VEC:
12956     case ASM_INPUT:
12957 #ifdef HAVE_cc0
12958     /* CC0 must die in the insn after it is set, so we don't need to take
12959        special note of it here.  */
12960     case CC0:
12961 #endif
12962       return;
12963
12964     case CLOBBER:
12965       /* If we are clobbering a MEM, mark any hard registers inside the
12966          address as used.  */
12967       if (MEM_P (XEXP (x, 0)))
12968         mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12969       return;
12970
12971     case REG:
12972       regno = REGNO (x);
12973       /* A hard reg in a wide mode may really be multiple registers.
12974          If so, mark all of them just like the first.  */
12975       if (regno < FIRST_PSEUDO_REGISTER)
12976         {
12977           /* None of this applies to the stack, frame or arg pointers.  */
12978           if (regno == STACK_POINTER_REGNUM
12979 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
12980               || regno == HARD_FRAME_POINTER_REGNUM
12981 #endif
12982 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12983               || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12984 #endif
12985               || regno == FRAME_POINTER_REGNUM)
12986             return;
12987
12988           add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
12989         }
12990       return;
12991
12992     case SET:
12993       {
12994         /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12995            the address.  */
12996         rtx testreg = SET_DEST (x);
12997
12998         while (GET_CODE (testreg) == SUBREG
12999                || GET_CODE (testreg) == ZERO_EXTRACT
13000                || GET_CODE (testreg) == STRICT_LOW_PART)
13001           testreg = XEXP (testreg, 0);
13002
13003         if (MEM_P (testreg))
13004           mark_used_regs_combine (XEXP (testreg, 0));
13005
13006         mark_used_regs_combine (SET_SRC (x));
13007       }
13008       return;
13009
13010     default:
13011       break;
13012     }
13013
13014   /* Recursively scan the operands of this expression.  */
13015
13016   {
13017     const char *fmt = GET_RTX_FORMAT (code);
13018
13019     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13020       {
13021         if (fmt[i] == 'e')
13022           mark_used_regs_combine (XEXP (x, i));
13023         else if (fmt[i] == 'E')
13024           {
13025             int j;
13026
13027             for (j = 0; j < XVECLEN (x, i); j++)
13028               mark_used_regs_combine (XVECEXP (x, i, j));
13029           }
13030       }
13031   }
13032 }
13033 \f
13034 /* Remove register number REGNO from the dead registers list of INSN.
13035
13036    Return the note used to record the death, if there was one.  */
13037
13038 rtx
13039 remove_death (unsigned int regno, rtx insn)
13040 {
13041   rtx note = find_regno_note (insn, REG_DEAD, regno);
13042
13043   if (note)
13044     remove_note (insn, note);
13045
13046   return note;
13047 }
13048
13049 /* For each register (hardware or pseudo) used within expression X, if its
13050    death is in an instruction with luid between FROM_LUID (inclusive) and
13051    TO_INSN (exclusive), put a REG_DEAD note for that register in the
13052    list headed by PNOTES.
13053
13054    That said, don't move registers killed by maybe_kill_insn.
13055
13056    This is done when X is being merged by combination into TO_INSN.  These
13057    notes will then be distributed as needed.  */
13058
13059 static void
13060 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx to_insn,
13061              rtx *pnotes)
13062 {
13063   const char *fmt;
13064   int len, i;
13065   enum rtx_code code = GET_CODE (x);
13066
13067   if (code == REG)
13068     {
13069       unsigned int regno = REGNO (x);
13070       rtx where_dead = VEC_index (reg_stat_type, reg_stat, regno)->last_death;
13071
13072       /* Don't move the register if it gets killed in between from and to.  */
13073       if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13074           && ! reg_referenced_p (x, maybe_kill_insn))
13075         return;
13076
13077       if (where_dead
13078           && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13079           && DF_INSN_LUID (where_dead) >= from_luid
13080           && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13081         {
13082           rtx note = remove_death (regno, where_dead);
13083
13084           /* It is possible for the call above to return 0.  This can occur
13085              when last_death points to I2 or I1 that we combined with.
13086              In that case make a new note.
13087
13088              We must also check for the case where X is a hard register
13089              and NOTE is a death note for a range of hard registers
13090              including X.  In that case, we must put REG_DEAD notes for
13091              the remaining registers in place of NOTE.  */
13092
13093           if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13094               && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13095                   > GET_MODE_SIZE (GET_MODE (x))))
13096             {
13097               unsigned int deadregno = REGNO (XEXP (note, 0));
13098               unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
13099               unsigned int ourend = END_HARD_REGNO (x);
13100               unsigned int i;
13101
13102               for (i = deadregno; i < deadend; i++)
13103                 if (i < regno || i >= ourend)
13104                   add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13105             }
13106
13107           /* If we didn't find any note, or if we found a REG_DEAD note that
13108              covers only part of the given reg, and we have a multi-reg hard
13109              register, then to be safe we must check for REG_DEAD notes
13110              for each register other than the first.  They could have
13111              their own REG_DEAD notes lying around.  */
13112           else if ((note == 0
13113                     || (note != 0
13114                         && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13115                             < GET_MODE_SIZE (GET_MODE (x)))))
13116                    && regno < FIRST_PSEUDO_REGISTER
13117                    && hard_regno_nregs[regno][GET_MODE (x)] > 1)
13118             {
13119               unsigned int ourend = END_HARD_REGNO (x);
13120               unsigned int i, offset;
13121               rtx oldnotes = 0;
13122
13123               if (note)
13124                 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
13125               else
13126                 offset = 1;
13127
13128               for (i = regno + offset; i < ourend; i++)
13129                 move_deaths (regno_reg_rtx[i],
13130                              maybe_kill_insn, from_luid, to_insn, &oldnotes);
13131             }
13132
13133           if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13134             {
13135               XEXP (note, 1) = *pnotes;
13136               *pnotes = note;
13137             }
13138           else
13139             *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13140         }
13141
13142       return;
13143     }
13144
13145   else if (GET_CODE (x) == SET)
13146     {
13147       rtx dest = SET_DEST (x);
13148
13149       move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13150
13151       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13152          that accesses one word of a multi-word item, some
13153          piece of everything register in the expression is used by
13154          this insn, so remove any old death.  */
13155       /* ??? So why do we test for equality of the sizes?  */
13156
13157       if (GET_CODE (dest) == ZERO_EXTRACT
13158           || GET_CODE (dest) == STRICT_LOW_PART
13159           || (GET_CODE (dest) == SUBREG
13160               && (((GET_MODE_SIZE (GET_MODE (dest))
13161                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13162                   == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13163                        + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13164         {
13165           move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13166           return;
13167         }
13168
13169       /* If this is some other SUBREG, we know it replaces the entire
13170          value, so use that as the destination.  */
13171       if (GET_CODE (dest) == SUBREG)
13172         dest = SUBREG_REG (dest);
13173
13174       /* If this is a MEM, adjust deaths of anything used in the address.
13175          For a REG (the only other possibility), the entire value is
13176          being replaced so the old value is not used in this insn.  */
13177
13178       if (MEM_P (dest))
13179         move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13180                      to_insn, pnotes);
13181       return;
13182     }
13183
13184   else if (GET_CODE (x) == CLOBBER)
13185     return;
13186
13187   len = GET_RTX_LENGTH (code);
13188   fmt = GET_RTX_FORMAT (code);
13189
13190   for (i = 0; i < len; i++)
13191     {
13192       if (fmt[i] == 'E')
13193         {
13194           int j;
13195           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13196             move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13197                          to_insn, pnotes);
13198         }
13199       else if (fmt[i] == 'e')
13200         move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13201     }
13202 }
13203 \f
13204 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13205    pattern of an insn.  X must be a REG.  */
13206
13207 static int
13208 reg_bitfield_target_p (rtx x, rtx body)
13209 {
13210   int i;
13211
13212   if (GET_CODE (body) == SET)
13213     {
13214       rtx dest = SET_DEST (body);
13215       rtx target;
13216       unsigned int regno, tregno, endregno, endtregno;
13217
13218       if (GET_CODE (dest) == ZERO_EXTRACT)
13219         target = XEXP (dest, 0);
13220       else if (GET_CODE (dest) == STRICT_LOW_PART)
13221         target = SUBREG_REG (XEXP (dest, 0));
13222       else
13223         return 0;
13224
13225       if (GET_CODE (target) == SUBREG)
13226         target = SUBREG_REG (target);
13227
13228       if (!REG_P (target))
13229         return 0;
13230
13231       tregno = REGNO (target), regno = REGNO (x);
13232       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13233         return target == x;
13234
13235       endtregno = end_hard_regno (GET_MODE (target), tregno);
13236       endregno = end_hard_regno (GET_MODE (x), regno);
13237
13238       return endregno > tregno && regno < endtregno;
13239     }
13240
13241   else if (GET_CODE (body) == PARALLEL)
13242     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13243       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13244         return 1;
13245
13246   return 0;
13247 }
13248 \f
13249 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13250    as appropriate.  I3 and I2 are the insns resulting from the combination
13251    insns including FROM (I2 may be zero).
13252
13253    ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13254    not need REG_DEAD notes because they are being substituted for.  This
13255    saves searching in the most common cases.
13256
13257    Each note in the list is either ignored or placed on some insns, depending
13258    on the type of note.  */
13259
13260 static void
13261 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
13262                   rtx elim_i1, rtx elim_i0)
13263 {
13264   rtx note, next_note;
13265   rtx tem;
13266
13267   for (note = notes; note; note = next_note)
13268     {
13269       rtx place = 0, place2 = 0;
13270
13271       next_note = XEXP (note, 1);
13272       switch (REG_NOTE_KIND (note))
13273         {
13274         case REG_BR_PROB:
13275         case REG_BR_PRED:
13276           /* Doesn't matter much where we put this, as long as it's somewhere.
13277              It is preferable to keep these notes on branches, which is most
13278              likely to be i3.  */
13279           place = i3;
13280           break;
13281
13282         case REG_NON_LOCAL_GOTO:
13283           if (JUMP_P (i3))
13284             place = i3;
13285           else
13286             {
13287               gcc_assert (i2 && JUMP_P (i2));
13288               place = i2;
13289             }
13290           break;
13291
13292         case REG_EH_REGION:
13293           /* These notes must remain with the call or trapping instruction.  */
13294           if (CALL_P (i3))
13295             place = i3;
13296           else if (i2 && CALL_P (i2))
13297             place = i2;
13298           else
13299             {
13300               gcc_assert (cfun->can_throw_non_call_exceptions);
13301               if (may_trap_p (i3))
13302                 place = i3;
13303               else if (i2 && may_trap_p (i2))
13304                 place = i2;
13305               /* ??? Otherwise assume we've combined things such that we
13306                  can now prove that the instructions can't trap.  Drop the
13307                  note in this case.  */
13308             }
13309           break;
13310
13311         case REG_NORETURN:
13312         case REG_SETJMP:
13313           /* These notes must remain with the call.  It should not be
13314              possible for both I2 and I3 to be a call.  */
13315           if (CALL_P (i3))
13316             place = i3;
13317           else
13318             {
13319               gcc_assert (i2 && CALL_P (i2));
13320               place = i2;
13321             }
13322           break;
13323
13324         case REG_UNUSED:
13325           /* Any clobbers for i3 may still exist, and so we must process
13326              REG_UNUSED notes from that insn.
13327
13328              Any clobbers from i2 or i1 can only exist if they were added by
13329              recog_for_combine.  In that case, recog_for_combine created the
13330              necessary REG_UNUSED notes.  Trying to keep any original
13331              REG_UNUSED notes from these insns can cause incorrect output
13332              if it is for the same register as the original i3 dest.
13333              In that case, we will notice that the register is set in i3,
13334              and then add a REG_UNUSED note for the destination of i3, which
13335              is wrong.  However, it is possible to have REG_UNUSED notes from
13336              i2 or i1 for register which were both used and clobbered, so
13337              we keep notes from i2 or i1 if they will turn into REG_DEAD
13338              notes.  */
13339
13340           /* If this register is set or clobbered in I3, put the note there
13341              unless there is one already.  */
13342           if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13343             {
13344               if (from_insn != i3)
13345                 break;
13346
13347               if (! (REG_P (XEXP (note, 0))
13348                      ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13349                      : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13350                 place = i3;
13351             }
13352           /* Otherwise, if this register is used by I3, then this register
13353              now dies here, so we must put a REG_DEAD note here unless there
13354              is one already.  */
13355           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13356                    && ! (REG_P (XEXP (note, 0))
13357                          ? find_regno_note (i3, REG_DEAD,
13358                                             REGNO (XEXP (note, 0)))
13359                          : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13360             {
13361               PUT_REG_NOTE_KIND (note, REG_DEAD);
13362               place = i3;
13363             }
13364           break;
13365
13366         case REG_EQUAL:
13367         case REG_EQUIV:
13368         case REG_NOALIAS:
13369           /* These notes say something about results of an insn.  We can
13370              only support them if they used to be on I3 in which case they
13371              remain on I3.  Otherwise they are ignored.
13372
13373              If the note refers to an expression that is not a constant, we
13374              must also ignore the note since we cannot tell whether the
13375              equivalence is still true.  It might be possible to do
13376              slightly better than this (we only have a problem if I2DEST
13377              or I1DEST is present in the expression), but it doesn't
13378              seem worth the trouble.  */
13379
13380           if (from_insn == i3
13381               && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13382             place = i3;
13383           break;
13384
13385         case REG_INC:
13386           /* These notes say something about how a register is used.  They must
13387              be present on any use of the register in I2 or I3.  */
13388           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13389             place = i3;
13390
13391           if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13392             {
13393               if (place)
13394                 place2 = i2;
13395               else
13396                 place = i2;
13397             }
13398           break;
13399
13400         case REG_LABEL_TARGET:
13401         case REG_LABEL_OPERAND:
13402           /* This can show up in several ways -- either directly in the
13403              pattern, or hidden off in the constant pool with (or without?)
13404              a REG_EQUAL note.  */
13405           /* ??? Ignore the without-reg_equal-note problem for now.  */
13406           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13407               || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13408                   && GET_CODE (XEXP (tem, 0)) == LABEL_REF
13409                   && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
13410             place = i3;
13411
13412           if (i2
13413               && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13414                   || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13415                       && GET_CODE (XEXP (tem, 0)) == LABEL_REF
13416                       && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
13417             {
13418               if (place)
13419                 place2 = i2;
13420               else
13421                 place = i2;
13422             }
13423
13424           /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13425              as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13426              there.  */
13427           if (place && JUMP_P (place)
13428               && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13429               && (JUMP_LABEL (place) == NULL
13430                   || JUMP_LABEL (place) == XEXP (note, 0)))
13431             {
13432               rtx label = JUMP_LABEL (place);
13433
13434               if (!label)
13435                 JUMP_LABEL (place) = XEXP (note, 0);
13436               else if (LABEL_P (label))
13437                 LABEL_NUSES (label)--;
13438             }
13439
13440           if (place2 && JUMP_P (place2)
13441               && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13442               && (JUMP_LABEL (place2) == NULL
13443                   || JUMP_LABEL (place2) == XEXP (note, 0)))
13444             {
13445               rtx label = JUMP_LABEL (place2);
13446
13447               if (!label)
13448                 JUMP_LABEL (place2) = XEXP (note, 0);
13449               else if (LABEL_P (label))
13450                 LABEL_NUSES (label)--;
13451               place2 = 0;
13452             }
13453           break;
13454
13455         case REG_NONNEG:
13456           /* This note says something about the value of a register prior
13457              to the execution of an insn.  It is too much trouble to see
13458              if the note is still correct in all situations.  It is better
13459              to simply delete it.  */
13460           break;
13461
13462         case REG_DEAD:
13463           /* If we replaced the right hand side of FROM_INSN with a
13464              REG_EQUAL note, the original use of the dying register
13465              will not have been combined into I3 and I2.  In such cases,
13466              FROM_INSN is guaranteed to be the first of the combined
13467              instructions, so we simply need to search back before
13468              FROM_INSN for the previous use or set of this register,
13469              then alter the notes there appropriately.
13470
13471              If the register is used as an input in I3, it dies there.
13472              Similarly for I2, if it is nonzero and adjacent to I3.
13473
13474              If the register is not used as an input in either I3 or I2
13475              and it is not one of the registers we were supposed to eliminate,
13476              there are two possibilities.  We might have a non-adjacent I2
13477              or we might have somehow eliminated an additional register
13478              from a computation.  For example, we might have had A & B where
13479              we discover that B will always be zero.  In this case we will
13480              eliminate the reference to A.
13481
13482              In both cases, we must search to see if we can find a previous
13483              use of A and put the death note there.  */
13484
13485           if (from_insn
13486               && from_insn == i2mod
13487               && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13488             tem = from_insn;
13489           else
13490             {
13491               if (from_insn
13492                   && CALL_P (from_insn)
13493                   && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13494                 place = from_insn;
13495               else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13496                 place = i3;
13497               else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13498                        && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13499                 place = i2;
13500               else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13501                         && !(i2mod
13502                              && reg_overlap_mentioned_p (XEXP (note, 0),
13503                                                          i2mod_old_rhs)))
13504                        || rtx_equal_p (XEXP (note, 0), elim_i1)
13505                        || rtx_equal_p (XEXP (note, 0), elim_i0))
13506                 break;
13507               tem = i3;
13508             }
13509
13510           if (place == 0)
13511             {
13512               basic_block bb = this_basic_block;
13513
13514               for (tem = PREV_INSN (tem); place == 0; tem = PREV_INSN (tem))
13515                 {
13516                   if (!NONDEBUG_INSN_P (tem))
13517                     {
13518                       if (tem == BB_HEAD (bb))
13519                         break;
13520                       continue;
13521                     }
13522
13523                   /* If the register is being set at TEM, see if that is all
13524                      TEM is doing.  If so, delete TEM.  Otherwise, make this
13525                      into a REG_UNUSED note instead. Don't delete sets to
13526                      global register vars.  */
13527                   if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13528                        || !global_regs[REGNO (XEXP (note, 0))])
13529                       && reg_set_p (XEXP (note, 0), PATTERN (tem)))
13530                     {
13531                       rtx set = single_set (tem);
13532                       rtx inner_dest = 0;
13533 #ifdef HAVE_cc0
13534                       rtx cc0_setter = NULL_RTX;
13535 #endif
13536
13537                       if (set != 0)
13538                         for (inner_dest = SET_DEST (set);
13539                              (GET_CODE (inner_dest) == STRICT_LOW_PART
13540                               || GET_CODE (inner_dest) == SUBREG
13541                               || GET_CODE (inner_dest) == ZERO_EXTRACT);
13542                              inner_dest = XEXP (inner_dest, 0))
13543                           ;
13544
13545                       /* Verify that it was the set, and not a clobber that
13546                          modified the register.
13547
13548                          CC0 targets must be careful to maintain setter/user
13549                          pairs.  If we cannot delete the setter due to side
13550                          effects, mark the user with an UNUSED note instead
13551                          of deleting it.  */
13552
13553                       if (set != 0 && ! side_effects_p (SET_SRC (set))
13554                           && rtx_equal_p (XEXP (note, 0), inner_dest)
13555 #ifdef HAVE_cc0
13556                           && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13557                               || ((cc0_setter = prev_cc0_setter (tem)) != NULL
13558                                   && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13559 #endif
13560                           )
13561                         {
13562                           /* Move the notes and links of TEM elsewhere.
13563                              This might delete other dead insns recursively.
13564                              First set the pattern to something that won't use
13565                              any register.  */
13566                           rtx old_notes = REG_NOTES (tem);
13567
13568                           PATTERN (tem) = pc_rtx;
13569                           REG_NOTES (tem) = NULL;
13570
13571                           distribute_notes (old_notes, tem, tem, NULL_RTX,
13572                                             NULL_RTX, NULL_RTX, NULL_RTX);
13573                           distribute_links (LOG_LINKS (tem));
13574
13575                           SET_INSN_DELETED (tem);
13576                           if (tem == i2)
13577                             i2 = NULL_RTX;
13578
13579 #ifdef HAVE_cc0
13580                           /* Delete the setter too.  */
13581                           if (cc0_setter)
13582                             {
13583                               PATTERN (cc0_setter) = pc_rtx;
13584                               old_notes = REG_NOTES (cc0_setter);
13585                               REG_NOTES (cc0_setter) = NULL;
13586
13587                               distribute_notes (old_notes, cc0_setter,
13588                                                 cc0_setter, NULL_RTX,
13589                                                 NULL_RTX, NULL_RTX, NULL_RTX);
13590                               distribute_links (LOG_LINKS (cc0_setter));
13591
13592                               SET_INSN_DELETED (cc0_setter);
13593                               if (cc0_setter == i2)
13594                                 i2 = NULL_RTX;
13595                             }
13596 #endif
13597                         }
13598                       else
13599                         {
13600                           PUT_REG_NOTE_KIND (note, REG_UNUSED);
13601
13602                           /*  If there isn't already a REG_UNUSED note, put one
13603                               here.  Do not place a REG_DEAD note, even if
13604                               the register is also used here; that would not
13605                               match the algorithm used in lifetime analysis
13606                               and can cause the consistency check in the
13607                               scheduler to fail.  */
13608                           if (! find_regno_note (tem, REG_UNUSED,
13609                                                  REGNO (XEXP (note, 0))))
13610                             place = tem;
13611                           break;
13612                         }
13613                     }
13614                   else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
13615                            || (CALL_P (tem)
13616                                && find_reg_fusage (tem, USE, XEXP (note, 0))))
13617                     {
13618                       place = tem;
13619
13620                       /* If we are doing a 3->2 combination, and we have a
13621                          register which formerly died in i3 and was not used
13622                          by i2, which now no longer dies in i3 and is used in
13623                          i2 but does not die in i2, and place is between i2
13624                          and i3, then we may need to move a link from place to
13625                          i2.  */
13626                       if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13627                           && from_insn
13628                           && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13629                           && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13630                         {
13631                           struct insn_link *links = LOG_LINKS (place);
13632                           LOG_LINKS (place) = NULL;
13633                           distribute_links (links);
13634                         }
13635                       break;
13636                     }
13637
13638                   if (tem == BB_HEAD (bb))
13639                     break;
13640                 }
13641
13642             }
13643
13644           /* If the register is set or already dead at PLACE, we needn't do
13645              anything with this note if it is still a REG_DEAD note.
13646              We check here if it is set at all, not if is it totally replaced,
13647              which is what `dead_or_set_p' checks, so also check for it being
13648              set partially.  */
13649
13650           if (place && REG_NOTE_KIND (note) == REG_DEAD)
13651             {
13652               unsigned int regno = REGNO (XEXP (note, 0));
13653               reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
13654
13655               if (dead_or_set_p (place, XEXP (note, 0))
13656                   || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13657                 {
13658                   /* Unless the register previously died in PLACE, clear
13659                      last_death.  [I no longer understand why this is
13660                      being done.] */
13661                   if (rsp->last_death != place)
13662                     rsp->last_death = 0;
13663                   place = 0;
13664                 }
13665               else
13666                 rsp->last_death = place;
13667
13668               /* If this is a death note for a hard reg that is occupying
13669                  multiple registers, ensure that we are still using all
13670                  parts of the object.  If we find a piece of the object
13671                  that is unused, we must arrange for an appropriate REG_DEAD
13672                  note to be added for it.  However, we can't just emit a USE
13673                  and tag the note to it, since the register might actually
13674                  be dead; so we recourse, and the recursive call then finds
13675                  the previous insn that used this register.  */
13676
13677               if (place && regno < FIRST_PSEUDO_REGISTER
13678                   && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13679                 {
13680                   unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13681                   int all_used = 1;
13682                   unsigned int i;
13683
13684                   for (i = regno; i < endregno; i++)
13685                     if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13686                          && ! find_regno_fusage (place, USE, i))
13687                         || dead_or_set_regno_p (place, i))
13688                       all_used = 0;
13689
13690                   if (! all_used)
13691                     {
13692                       /* Put only REG_DEAD notes for pieces that are
13693                          not already dead or set.  */
13694
13695                       for (i = regno; i < endregno;
13696                            i += hard_regno_nregs[i][reg_raw_mode[i]])
13697                         {
13698                           rtx piece = regno_reg_rtx[i];
13699                           basic_block bb = this_basic_block;
13700
13701                           if (! dead_or_set_p (place, piece)
13702                               && ! reg_bitfield_target_p (piece,
13703                                                           PATTERN (place)))
13704                             {
13705                               rtx new_note = alloc_reg_note (REG_DEAD, piece,
13706                                                              NULL_RTX);
13707
13708                               distribute_notes (new_note, place, place,
13709                                                 NULL_RTX, NULL_RTX, NULL_RTX,
13710                                                 NULL_RTX);
13711                             }
13712                           else if (! refers_to_regno_p (i, i + 1,
13713                                                         PATTERN (place), 0)
13714                                    && ! find_regno_fusage (place, USE, i))
13715                             for (tem = PREV_INSN (place); ;
13716                                  tem = PREV_INSN (tem))
13717                               {
13718                                 if (!NONDEBUG_INSN_P (tem))
13719                                   {
13720                                     if (tem == BB_HEAD (bb))
13721                                       break;
13722                                     continue;
13723                                   }
13724                                 if (dead_or_set_p (tem, piece)
13725                                     || reg_bitfield_target_p (piece,
13726                                                               PATTERN (tem)))
13727                                   {
13728                                     add_reg_note (tem, REG_UNUSED, piece);
13729                                     break;
13730                                   }
13731                               }
13732
13733                         }
13734
13735                       place = 0;
13736                     }
13737                 }
13738             }
13739           break;
13740
13741         default:
13742           /* Any other notes should not be present at this point in the
13743              compilation.  */
13744           gcc_unreachable ();
13745         }
13746
13747       if (place)
13748         {
13749           XEXP (note, 1) = REG_NOTES (place);
13750           REG_NOTES (place) = note;
13751         }
13752
13753       if (place2)
13754         add_reg_note (place2, REG_NOTE_KIND (note), XEXP (note, 0));
13755     }
13756 }
13757 \f
13758 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13759    I3, I2, and I1 to new locations.  This is also called to add a link
13760    pointing at I3 when I3's destination is changed.  */
13761
13762 static void
13763 distribute_links (struct insn_link *links)
13764 {
13765   struct insn_link *link, *next_link;
13766
13767   for (link = links; link; link = next_link)
13768     {
13769       rtx place = 0;
13770       rtx insn;
13771       rtx set, reg;
13772
13773       next_link = link->next;
13774
13775       /* If the insn that this link points to is a NOTE or isn't a single
13776          set, ignore it.  In the latter case, it isn't clear what we
13777          can do other than ignore the link, since we can't tell which
13778          register it was for.  Such links wouldn't be used by combine
13779          anyway.
13780
13781          It is not possible for the destination of the target of the link to
13782          have been changed by combine.  The only potential of this is if we
13783          replace I3, I2, and I1 by I3 and I2.  But in that case the
13784          destination of I2 also remains unchanged.  */
13785
13786       if (NOTE_P (link->insn)
13787           || (set = single_set (link->insn)) == 0)
13788         continue;
13789
13790       reg = SET_DEST (set);
13791       while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
13792              || GET_CODE (reg) == STRICT_LOW_PART)
13793         reg = XEXP (reg, 0);
13794
13795       /* A LOG_LINK is defined as being placed on the first insn that uses
13796          a register and points to the insn that sets the register.  Start
13797          searching at the next insn after the target of the link and stop
13798          when we reach a set of the register or the end of the basic block.
13799
13800          Note that this correctly handles the link that used to point from
13801          I3 to I2.  Also note that not much searching is typically done here
13802          since most links don't point very far away.  */
13803
13804       for (insn = NEXT_INSN (link->insn);
13805            (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
13806                      || BB_HEAD (this_basic_block->next_bb) != insn));
13807            insn = NEXT_INSN (insn))
13808         if (DEBUG_INSN_P (insn))
13809           continue;
13810         else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13811           {
13812             if (reg_referenced_p (reg, PATTERN (insn)))
13813               place = insn;
13814             break;
13815           }
13816         else if (CALL_P (insn)
13817                  && find_reg_fusage (insn, USE, reg))
13818           {
13819             place = insn;
13820             break;
13821           }
13822         else if (INSN_P (insn) && reg_set_p (reg, insn))
13823           break;
13824
13825       /* If we found a place to put the link, place it there unless there
13826          is already a link to the same insn as LINK at that point.  */
13827
13828       if (place)
13829         {
13830           struct insn_link *link2;
13831
13832           FOR_EACH_LOG_LINK (link2, place)
13833             if (link2->insn == link->insn)
13834               break;
13835
13836           if (link2 == NULL)
13837             {
13838               link->next = LOG_LINKS (place);
13839               LOG_LINKS (place) = link;
13840
13841               /* Set added_links_insn to the earliest insn we added a
13842                  link to.  */
13843               if (added_links_insn == 0
13844                   || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
13845                 added_links_insn = place;
13846             }
13847         }
13848     }
13849 }
13850 \f
13851 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
13852    Check whether the expression pointer to by LOC is a register or
13853    memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
13854    Otherwise return zero.  */
13855
13856 static int
13857 unmentioned_reg_p_1 (rtx *loc, void *expr)
13858 {
13859   rtx x = *loc;
13860
13861   if (x != NULL_RTX
13862       && (REG_P (x) || MEM_P (x))
13863       && ! reg_mentioned_p (x, (rtx) expr))
13864     return 1;
13865   return 0;
13866 }
13867
13868 /* Check for any register or memory mentioned in EQUIV that is not
13869    mentioned in EXPR.  This is used to restrict EQUIV to "specializations"
13870    of EXPR where some registers may have been replaced by constants.  */
13871
13872 static bool
13873 unmentioned_reg_p (rtx equiv, rtx expr)
13874 {
13875   return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
13876 }
13877 \f
13878 void
13879 dump_combine_stats (FILE *file)
13880 {
13881   fprintf
13882     (file,
13883      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13884      combine_attempts, combine_merges, combine_extras, combine_successes);
13885 }
13886
13887 void
13888 dump_combine_total_stats (FILE *file)
13889 {
13890   fprintf
13891     (file,
13892      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13893      total_attempts, total_merges, total_extras, total_successes);
13894 }
13895 \f
13896 static bool
13897 gate_handle_combine (void)
13898 {
13899   return (optimize > 0);
13900 }
13901
13902 /* Try combining insns through substitution.  */
13903 static unsigned int
13904 rest_of_handle_combine (void)
13905 {
13906   int rebuild_jump_labels_after_combine;
13907
13908   df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
13909   df_note_add_problem ();
13910   df_analyze ();
13911
13912   regstat_init_n_sets_and_refs ();
13913
13914   rebuild_jump_labels_after_combine
13915     = combine_instructions (get_insns (), max_reg_num ());
13916
13917   /* Combining insns may have turned an indirect jump into a
13918      direct jump.  Rebuild the JUMP_LABEL fields of jumping
13919      instructions.  */
13920   if (rebuild_jump_labels_after_combine)
13921     {
13922       timevar_push (TV_JUMP);
13923       rebuild_jump_labels (get_insns ());
13924       cleanup_cfg (0);
13925       timevar_pop (TV_JUMP);
13926     }
13927
13928   regstat_free_n_sets_and_refs ();
13929   return 0;
13930 }
13931
13932 struct rtl_opt_pass pass_combine =
13933 {
13934  {
13935   RTL_PASS,
13936   "combine",                            /* name */
13937   gate_handle_combine,                  /* gate */
13938   rest_of_handle_combine,               /* execute */
13939   NULL,                                 /* sub */
13940   NULL,                                 /* next */
13941   0,                                    /* static_pass_number */
13942   TV_COMBINE,                           /* tv_id */
13943   PROP_cfglayout,                       /* properties_required */
13944   0,                                    /* properties_provided */
13945   0,                                    /* properties_destroyed */
13946   0,                                    /* todo_flags_start */
13947   TODO_dump_func |
13948   TODO_df_finish | TODO_verify_rtl_sharing |
13949   TODO_ggc_collect,                     /* todo_flags_finish */
13950  }
13951 };