OSDN Git Service

2005-03-21 Paolo Bonzini <bonzini@gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 /* This module is essentially the "combiner" phase of the U. of Arizona
23    Portable Optimizer, but redone to work on our list-structured
24    representation for RTL instead of their string representation.
25
26    The LOG_LINKS of each insn identify the most recent assignment
27    to each REG used in the insn.  It is a list of previous insns,
28    each of which contains a SET for a REG that is used in this insn
29    and not used or set in between.  LOG_LINKs never cross basic blocks.
30    They were set up by the preceding pass (lifetime analysis).
31
32    We try to combine each pair of insns joined by a logical link.
33    We also try to combine triples of insns A, B and C when
34    C has a link back to B and B has a link back to A.
35
36    LOG_LINKS does not have links for use of the CC0.  They don't
37    need to, because the insn that sets the CC0 is always immediately
38    before the insn that tests it.  So we always regard a branch
39    insn as having a logical link to the preceding insn.  The same is true
40    for an insn explicitly using CC0.
41
42    We check (with use_crosses_set_p) to avoid combining in such a way
43    as to move a computation to a place where its value would be different.
44
45    Combination is done by mathematically substituting the previous
46    insn(s) values for the regs they set into the expressions in
47    the later insns that refer to these regs.  If the result is a valid insn
48    for our target machine, according to the machine description,
49    we install it, delete the earlier insns, and update the data flow
50    information (LOG_LINKS and REG_NOTES) for what we did.
51
52    There are a few exceptions where the dataflow information created by
53    flow.c aren't completely updated:
54
55    - reg_live_length is not updated
56    - a LOG_LINKS entry that refers to an insn with multiple SETs may be
57      removed because there is no way to know which register it was
58      linking
59
60    To simplify substitution, we combine only when the earlier insn(s)
61    consist of only a single assignment.  To simplify updating afterward,
62    we never combine when a subroutine call appears in the middle.
63
64    Since we do not represent assignments to CC0 explicitly except when that
65    is all an insn does, there is no LOG_LINKS entry in an insn that uses
66    the condition code for the insn that set the condition code.
67    Fortunately, these two insns must be consecutive.
68    Therefore, every JUMP_INSN is taken to have an implicit logical link
69    to the preceding insn.  This is not quite right, since non-jumps can
70    also use the condition code; but in practice such insns would not
71    combine anyway.  */
72
73 #include "config.h"
74 #include "system.h"
75 #include "coretypes.h"
76 #include "tm.h"
77 #include "rtl.h"
78 #include "tree.h"
79 #include "tm_p.h"
80 #include "flags.h"
81 #include "regs.h"
82 #include "hard-reg-set.h"
83 #include "basic-block.h"
84 #include "insn-config.h"
85 #include "function.h"
86 /* Include expr.h after insn-config.h so we get HAVE_conditional_move.  */
87 #include "expr.h"
88 #include "insn-attr.h"
89 #include "recog.h"
90 #include "real.h"
91 #include "toplev.h"
92 #include "target.h"
93 #include "optabs.h"
94 #include "insn-codes.h"
95 #include "rtlhooks-def.h"
96 /* Include output.h for dump_file.  */
97 #include "output.h"
98 #include "params.h"
99
100 /* Number of attempts to combine instructions in this function.  */
101
102 static int combine_attempts;
103
104 /* Number of attempts that got as far as substitution in this function.  */
105
106 static int combine_merges;
107
108 /* Number of instructions combined with added SETs in this function.  */
109
110 static int combine_extras;
111
112 /* Number of instructions combined in this function.  */
113
114 static int combine_successes;
115
116 /* Totals over entire compilation.  */
117
118 static int total_attempts, total_merges, total_extras, total_successes;
119
120 \f
121 /* Vector mapping INSN_UIDs to cuids.
122    The cuids are like uids but increase monotonically always.
123    Combine always uses cuids so that it can compare them.
124    But actually renumbering the uids, which we used to do,
125    proves to be a bad idea because it makes it hard to compare
126    the dumps produced by earlier passes with those from later passes.  */
127
128 static int *uid_cuid;
129 static int max_uid_cuid;
130
131 /* Get the cuid of an insn.  */
132
133 #define INSN_CUID(INSN) \
134 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
135
136 /* In case BITS_PER_WORD == HOST_BITS_PER_WIDE_INT, shifting by
137    BITS_PER_WORD would invoke undefined behavior.  Work around it.  */
138
139 #define UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD(val) \
140   (((unsigned HOST_WIDE_INT) (val) << (BITS_PER_WORD - 1)) << 1)
141
142 /* Maximum register number, which is the size of the tables below.  */
143
144 static unsigned int combine_max_regno;
145
146 struct reg_stat {
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
242 static struct reg_stat *reg_stat;
243
244 /* Record the cuid of the last insn that invalidated memory
245    (anything that writes memory, and subroutine calls, but not pushes).  */
246
247 static int mem_last_set;
248
249 /* Record the cuid of the last CALL_INSN
250    so we can tell whether a potential combination crosses any calls.  */
251
252 static int last_call_cuid;
253
254 /* When `subst' is called, this is the insn that is being modified
255    (by combining in a previous insn).  The PATTERN of this insn
256    is still the old pattern partially modified and it should not be
257    looked at, but this may be used to examine the successors of the insn
258    to judge whether a simplification is valid.  */
259
260 static rtx subst_insn;
261
262 /* This is the lowest CUID that `subst' is currently dealing with.
263    get_last_value will not return a value if the register was set at or
264    after this CUID.  If not for this mechanism, we could get confused if
265    I2 or I1 in try_combine were an insn that used the old value of a register
266    to obtain a new value.  In that case, we might erroneously get the
267    new value of the register when we wanted the old one.  */
268
269 static int subst_low_cuid;
270
271 /* This contains any hard registers that are used in newpat; reg_dead_at_p
272    must consider all these registers to be always live.  */
273
274 static HARD_REG_SET newpat_used_regs;
275
276 /* This is an insn to which a LOG_LINKS entry has been added.  If this
277    insn is the earlier than I2 or I3, combine should rescan starting at
278    that location.  */
279
280 static rtx added_links_insn;
281
282 /* Basic block in which we are performing combines.  */
283 static basic_block this_basic_block;
284
285 /* A bitmap indicating which blocks had registers go dead at entry.
286    After combine, we'll need to re-do global life analysis with
287    those blocks as starting points.  */
288 static sbitmap refresh_blocks;
289 \f
290 /* The following array records the insn_rtx_cost for every insn
291    in the instruction stream.  */
292
293 static int *uid_insn_cost;
294
295 /* Length of the currently allocated uid_insn_cost array.  */
296
297 static int last_insn_cost;
298
299 /* Incremented for each label.  */
300
301 static int label_tick;
302
303 /* Mode used to compute significance in reg_stat[].nonzero_bits.  It is the
304    largest integer mode that can fit in HOST_BITS_PER_WIDE_INT.  */
305
306 static enum machine_mode nonzero_bits_mode;
307
308 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
309    be safely used.  It is zero while computing them and after combine has
310    completed.  This former test prevents propagating values based on
311    previously set values, which can be incorrect if a variable is modified
312    in a loop.  */
313
314 static int nonzero_sign_valid;
315
316 \f
317 /* Record one modification to rtl structure
318    to be undone by storing old_contents into *where.
319    is_int is 1 if the contents are an int.  */
320
321 struct undo
322 {
323   struct undo *next;
324   int is_int;
325   union {rtx r; int i;} old_contents;
326   union {rtx *r; int *i;} where;
327 };
328
329 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
330    num_undo says how many are currently recorded.
331
332    other_insn is nonzero if we have modified some other insn in the process
333    of working on subst_insn.  It must be verified too.  */
334
335 struct undobuf
336 {
337   struct undo *undos;
338   struct undo *frees;
339   rtx other_insn;
340 };
341
342 static struct undobuf undobuf;
343
344 /* Number of times the pseudo being substituted for
345    was found and replaced.  */
346
347 static int n_occurrences;
348
349 static rtx reg_nonzero_bits_for_combine (rtx, enum machine_mode, rtx,
350                                          enum machine_mode,
351                                          unsigned HOST_WIDE_INT,
352                                          unsigned HOST_WIDE_INT *);
353 static rtx reg_num_sign_bit_copies_for_combine (rtx, enum machine_mode, rtx,
354                                                 enum machine_mode,
355                                                 unsigned int, unsigned int *);
356 static void do_SUBST (rtx *, rtx);
357 static void do_SUBST_INT (int *, int);
358 static void init_reg_last (void);
359 static void setup_incoming_promotions (void);
360 static void set_nonzero_bits_and_sign_copies (rtx, rtx, void *);
361 static int cant_combine_insn_p (rtx);
362 static int can_combine_p (rtx, rtx, rtx, rtx, rtx *, rtx *);
363 static int combinable_i3pat (rtx, rtx *, rtx, rtx, int, rtx *);
364 static int contains_muldiv (rtx);
365 static rtx try_combine (rtx, rtx, rtx, int *);
366 static void undo_all (void);
367 static void undo_commit (void);
368 static rtx *find_split_point (rtx *, rtx);
369 static rtx subst (rtx, rtx, rtx, int, int);
370 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
371 static rtx simplify_if_then_else (rtx);
372 static rtx simplify_set (rtx);
373 static rtx simplify_logical (rtx);
374 static rtx expand_compound_operation (rtx);
375 static rtx expand_field_assignment (rtx);
376 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
377                             rtx, unsigned HOST_WIDE_INT, int, int, int);
378 static rtx extract_left_shift (rtx, int);
379 static rtx make_compound_operation (rtx, enum rtx_code);
380 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
381                               unsigned HOST_WIDE_INT *);
382 static rtx force_to_mode (rtx, enum machine_mode,
383                           unsigned HOST_WIDE_INT, rtx, int);
384 static rtx if_then_else_cond (rtx, rtx *, rtx *);
385 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
386 static int rtx_equal_for_field_assignment_p (rtx, rtx);
387 static rtx make_field_assignment (rtx);
388 static rtx apply_distributive_law (rtx);
389 static rtx distribute_and_simplify_rtx (rtx, int);
390 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
391                                    unsigned HOST_WIDE_INT);
392 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
393                             HOST_WIDE_INT, enum machine_mode, int *);
394 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
395                                  int);
396 static int recog_for_combine (rtx *, rtx, rtx *);
397 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
398 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
399 static void update_table_tick (rtx);
400 static void record_value_for_reg (rtx, rtx, rtx);
401 static void check_promoted_subreg (rtx, rtx);
402 static void record_dead_and_set_regs_1 (rtx, rtx, void *);
403 static void record_dead_and_set_regs (rtx);
404 static int get_last_value_validate (rtx *, rtx, int, int);
405 static rtx get_last_value (rtx);
406 static int use_crosses_set_p (rtx, int);
407 static void reg_dead_at_p_1 (rtx, rtx, void *);
408 static int reg_dead_at_p (rtx, rtx);
409 static void move_deaths (rtx, rtx, int, rtx, rtx *);
410 static int reg_bitfield_target_p (rtx, rtx);
411 static void distribute_notes (rtx, rtx, rtx, rtx);
412 static void distribute_links (rtx);
413 static void mark_used_regs_combine (rtx);
414 static int insn_cuid (rtx);
415 static void record_promoted_value (rtx, rtx);
416 static int unmentioned_reg_p_1 (rtx *, void *);
417 static bool unmentioned_reg_p (rtx, rtx);
418 \f
419
420 /* It is not safe to use ordinary gen_lowpart in combine.
421    See comments in gen_lowpart_for_combine.  */
422 #undef RTL_HOOKS_GEN_LOWPART
423 #define RTL_HOOKS_GEN_LOWPART              gen_lowpart_for_combine
424
425 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
426 #define RTL_HOOKS_REG_NONZERO_REG_BITS     reg_nonzero_bits_for_combine
427
428 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
429 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES  reg_num_sign_bit_copies_for_combine
430
431 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
432
433 \f
434 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
435    insn.  The substitution can be undone by undo_all.  If INTO is already
436    set to NEWVAL, do not record this change.  Because computing NEWVAL might
437    also call SUBST, we have to compute it before we put anything into
438    the undo table.  */
439
440 static void
441 do_SUBST (rtx *into, rtx newval)
442 {
443   struct undo *buf;
444   rtx oldval = *into;
445
446   if (oldval == newval)
447     return;
448
449   /* We'd like to catch as many invalid transformations here as
450      possible.  Unfortunately, there are way too many mode changes
451      that are perfectly valid, so we'd waste too much effort for
452      little gain doing the checks here.  Focus on catching invalid
453      transformations involving integer constants.  */
454   if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
455       && GET_CODE (newval) == CONST_INT)
456     {
457       /* Sanity check that we're replacing oldval with a CONST_INT
458          that is a valid sign-extension for the original mode.  */
459       gcc_assert (INTVAL (newval)
460                   == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
461
462       /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
463          CONST_INT is not valid, because after the replacement, the
464          original mode would be gone.  Unfortunately, we can't tell
465          when do_SUBST is called to replace the operand thereof, so we
466          perform this test on oldval instead, checking whether an
467          invalid replacement took place before we got here.  */
468       gcc_assert (!(GET_CODE (oldval) == SUBREG
469                     && GET_CODE (SUBREG_REG (oldval)) == CONST_INT));
470       gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
471                     && GET_CODE (XEXP (oldval, 0)) == CONST_INT));
472     }
473
474   if (undobuf.frees)
475     buf = undobuf.frees, undobuf.frees = buf->next;
476   else
477     buf = xmalloc (sizeof (struct undo));
478
479   buf->is_int = 0;
480   buf->where.r = into;
481   buf->old_contents.r = oldval;
482   *into = newval;
483
484   buf->next = undobuf.undos, undobuf.undos = buf;
485 }
486
487 #define SUBST(INTO, NEWVAL)     do_SUBST(&(INTO), (NEWVAL))
488
489 /* Similar to SUBST, but NEWVAL is an int expression.  Note that substitution
490    for the value of a HOST_WIDE_INT value (including CONST_INT) is
491    not safe.  */
492
493 static void
494 do_SUBST_INT (int *into, int newval)
495 {
496   struct undo *buf;
497   int oldval = *into;
498
499   if (oldval == newval)
500     return;
501
502   if (undobuf.frees)
503     buf = undobuf.frees, undobuf.frees = buf->next;
504   else
505     buf = xmalloc (sizeof (struct undo));
506
507   buf->is_int = 1;
508   buf->where.i = into;
509   buf->old_contents.i = oldval;
510   *into = newval;
511
512   buf->next = undobuf.undos, undobuf.undos = buf;
513 }
514
515 #define SUBST_INT(INTO, NEWVAL)  do_SUBST_INT(&(INTO), (NEWVAL))
516 \f
517 /* Subroutine of try_combine.  Determine whether the combine replacement
518    patterns NEWPAT and NEWI2PAT are cheaper according to insn_rtx_cost
519    that the original instruction sequence I1, I2 and I3.  Note that I1
520    and/or NEWI2PAT may be NULL_RTX.  This function returns false, if the
521    costs of all instructions can be estimated, and the replacements are
522    more expensive than the original sequence.  */
523
524 static bool
525 combine_validate_cost (rtx i1, rtx i2, rtx i3, rtx newpat, rtx newi2pat)
526 {
527   int i1_cost, i2_cost, i3_cost;
528   int new_i2_cost, new_i3_cost;
529   int old_cost, new_cost;
530
531   /* Lookup the original insn_rtx_costs.  */
532   i2_cost = INSN_UID (i2) <= last_insn_cost
533             ? uid_insn_cost[INSN_UID (i2)] : 0;
534   i3_cost = INSN_UID (i3) <= last_insn_cost
535             ? uid_insn_cost[INSN_UID (i3)] : 0;
536
537   if (i1)
538     {
539       i1_cost = INSN_UID (i1) <= last_insn_cost
540                 ? uid_insn_cost[INSN_UID (i1)] : 0;
541       old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0)
542                  ? i1_cost + i2_cost + i3_cost : 0;
543     }
544   else
545     {
546       old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
547       i1_cost = 0;
548     }
549
550   /* Calculate the replacement insn_rtx_costs.  */
551   new_i3_cost = insn_rtx_cost (newpat);
552   if (newi2pat)
553     {
554       new_i2_cost = insn_rtx_cost (newi2pat);
555       new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
556                  ? new_i2_cost + new_i3_cost : 0;
557     }
558   else
559     {
560       new_cost = new_i3_cost;
561       new_i2_cost = 0;
562     }
563
564   if (undobuf.other_insn)
565     {
566       int old_other_cost, new_other_cost;
567
568       old_other_cost = (INSN_UID (undobuf.other_insn) <= last_insn_cost
569                         ? uid_insn_cost[INSN_UID (undobuf.other_insn)] : 0);
570       new_other_cost = insn_rtx_cost (PATTERN (undobuf.other_insn));
571       if (old_other_cost > 0 && new_other_cost > 0)
572         {
573           old_cost += old_other_cost;
574           new_cost += new_other_cost;
575         }
576       else
577         old_cost = 0;
578     }
579
580   /* Disallow this recombination if both new_cost and old_cost are
581      greater than zero, and new_cost is greater than old cost.  */
582   if (old_cost > 0
583       && new_cost > old_cost)
584     {
585       if (dump_file)
586         {
587           if (i1)
588             {
589               fprintf (dump_file,
590                        "rejecting combination of insns %d, %d and %d\n",
591                        INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
592               fprintf (dump_file, "original costs %d + %d + %d = %d\n",
593                        i1_cost, i2_cost, i3_cost, old_cost);
594             }
595           else
596             {
597               fprintf (dump_file,
598                        "rejecting combination of insns %d and %d\n",
599                        INSN_UID (i2), INSN_UID (i3));
600               fprintf (dump_file, "original costs %d + %d = %d\n",
601                        i2_cost, i3_cost, old_cost);
602             }
603
604           if (newi2pat)
605             {
606               fprintf (dump_file, "replacement costs %d + %d = %d\n",
607                        new_i2_cost, new_i3_cost, new_cost);
608             }
609           else
610             fprintf (dump_file, "replacement cost %d\n", new_cost);
611         }
612
613       return false;
614     }
615
616   /* Update the uid_insn_cost array with the replacement costs.  */
617   uid_insn_cost[INSN_UID (i2)] = new_i2_cost;
618   uid_insn_cost[INSN_UID (i3)] = new_i3_cost;
619   if (i1)
620     uid_insn_cost[INSN_UID (i1)] = 0;
621
622   return true;
623 }
624 \f
625 /* Main entry point for combiner.  F is the first insn of the function.
626    NREGS is the first unused pseudo-reg number.
627
628    Return nonzero if the combiner has turned an indirect jump
629    instruction into a direct jump.  */
630 int
631 combine_instructions (rtx f, unsigned int nregs)
632 {
633   rtx insn, next;
634 #ifdef HAVE_cc0
635   rtx prev;
636 #endif
637   int i;
638   rtx links, nextlinks;
639
640   int new_direct_jump_p = 0;
641
642   combine_attempts = 0;
643   combine_merges = 0;
644   combine_extras = 0;
645   combine_successes = 0;
646
647   combine_max_regno = nregs;
648
649   rtl_hooks = combine_rtl_hooks;
650
651   reg_stat = xcalloc (nregs, sizeof (struct reg_stat));
652
653   init_recog_no_volatile ();
654
655   /* Compute maximum uid value so uid_cuid can be allocated.  */
656
657   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
658     if (INSN_UID (insn) > i)
659       i = INSN_UID (insn);
660
661   uid_cuid = xmalloc ((i + 1) * sizeof (int));
662   max_uid_cuid = i;
663
664   nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
665
666   /* Don't use reg_stat[].nonzero_bits when computing it.  This can cause
667      problems when, for example, we have j <<= 1 in a loop.  */
668
669   nonzero_sign_valid = 0;
670
671   /* Compute the mapping from uids to cuids.
672      Cuids are numbers assigned to insns, like uids,
673      except that cuids increase monotonically through the code.
674
675      Scan all SETs and see if we can deduce anything about what
676      bits are known to be zero for some registers and how many copies
677      of the sign bit are known to exist for those registers.
678
679      Also set any known values so that we can use it while searching
680      for what bits are known to be set.  */
681
682   label_tick = 1;
683
684   setup_incoming_promotions ();
685
686   refresh_blocks = sbitmap_alloc (last_basic_block);
687   sbitmap_zero (refresh_blocks);
688
689   /* Allocate array of current insn_rtx_costs.  */
690   uid_insn_cost = xcalloc (max_uid_cuid + 1, sizeof (int));
691   last_insn_cost = max_uid_cuid;
692
693   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
694     {
695       uid_cuid[INSN_UID (insn)] = ++i;
696       subst_low_cuid = i;
697       subst_insn = insn;
698
699       if (INSN_P (insn))
700         {
701           note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
702                        NULL);
703           record_dead_and_set_regs (insn);
704
705 #ifdef AUTO_INC_DEC
706           for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
707             if (REG_NOTE_KIND (links) == REG_INC)
708               set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
709                                                 NULL);
710 #endif
711
712           /* Record the current insn_rtx_cost of this instruction.  */
713           if (NONJUMP_INSN_P (insn))
714             uid_insn_cost[INSN_UID (insn)] = insn_rtx_cost (PATTERN (insn));
715           if (dump_file)
716             fprintf(dump_file, "insn_cost %d: %d\n",
717                     INSN_UID (insn), uid_insn_cost[INSN_UID (insn)]);
718         }
719
720       if (LABEL_P (insn))
721         label_tick++;
722     }
723
724   nonzero_sign_valid = 1;
725
726   /* Now scan all the insns in forward order.  */
727
728   label_tick = 1;
729   last_call_cuid = 0;
730   mem_last_set = 0;
731   init_reg_last ();
732   setup_incoming_promotions ();
733
734   FOR_EACH_BB (this_basic_block)
735     {
736       for (insn = BB_HEAD (this_basic_block);
737            insn != NEXT_INSN (BB_END (this_basic_block));
738            insn = next ? next : NEXT_INSN (insn))
739         {
740           next = 0;
741
742           if (LABEL_P (insn))
743             label_tick++;
744
745           else if (INSN_P (insn))
746             {
747               /* See if we know about function return values before this
748                  insn based upon SUBREG flags.  */
749               check_promoted_subreg (insn, PATTERN (insn));
750
751               /* Try this insn with each insn it links back to.  */
752
753               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
754                 if ((next = try_combine (insn, XEXP (links, 0),
755                                          NULL_RTX, &new_direct_jump_p)) != 0)
756                   goto retry;
757
758               /* Try each sequence of three linked insns ending with this one.  */
759
760               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
761                 {
762                   rtx link = XEXP (links, 0);
763
764                   /* If the linked insn has been replaced by a note, then there
765                      is no point in pursuing this chain any further.  */
766                   if (NOTE_P (link))
767                     continue;
768
769                   for (nextlinks = LOG_LINKS (link);
770                        nextlinks;
771                        nextlinks = XEXP (nextlinks, 1))
772                     if ((next = try_combine (insn, link,
773                                              XEXP (nextlinks, 0),
774                                              &new_direct_jump_p)) != 0)
775                       goto retry;
776                 }
777
778 #ifdef HAVE_cc0
779               /* Try to combine a jump insn that uses CC0
780                  with a preceding insn that sets CC0, and maybe with its
781                  logical predecessor as well.
782                  This is how we make decrement-and-branch insns.
783                  We need this special code because data flow connections
784                  via CC0 do not get entered in LOG_LINKS.  */
785
786               if (JUMP_P (insn)
787                   && (prev = prev_nonnote_insn (insn)) != 0
788                   && NONJUMP_INSN_P (prev)
789                   && sets_cc0_p (PATTERN (prev)))
790                 {
791                   if ((next = try_combine (insn, prev,
792                                            NULL_RTX, &new_direct_jump_p)) != 0)
793                     goto retry;
794
795                   for (nextlinks = LOG_LINKS (prev); nextlinks;
796                        nextlinks = XEXP (nextlinks, 1))
797                     if ((next = try_combine (insn, prev,
798                                              XEXP (nextlinks, 0),
799                                              &new_direct_jump_p)) != 0)
800                       goto retry;
801                 }
802
803               /* Do the same for an insn that explicitly references CC0.  */
804               if (NONJUMP_INSN_P (insn)
805                   && (prev = prev_nonnote_insn (insn)) != 0
806                   && NONJUMP_INSN_P (prev)
807                   && sets_cc0_p (PATTERN (prev))
808                   && GET_CODE (PATTERN (insn)) == SET
809                   && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
810                 {
811                   if ((next = try_combine (insn, prev,
812                                            NULL_RTX, &new_direct_jump_p)) != 0)
813                     goto retry;
814
815                   for (nextlinks = LOG_LINKS (prev); nextlinks;
816                        nextlinks = XEXP (nextlinks, 1))
817                     if ((next = try_combine (insn, prev,
818                                              XEXP (nextlinks, 0),
819                                              &new_direct_jump_p)) != 0)
820                       goto retry;
821                 }
822
823               /* Finally, see if any of the insns that this insn links to
824                  explicitly references CC0.  If so, try this insn, that insn,
825                  and its predecessor if it sets CC0.  */
826               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
827                 if (NONJUMP_INSN_P (XEXP (links, 0))
828                     && GET_CODE (PATTERN (XEXP (links, 0))) == SET
829                     && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
830                     && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
831                     && NONJUMP_INSN_P (prev)
832                     && sets_cc0_p (PATTERN (prev))
833                     && (next = try_combine (insn, XEXP (links, 0),
834                                             prev, &new_direct_jump_p)) != 0)
835                   goto retry;
836 #endif
837
838               /* Try combining an insn with two different insns whose results it
839                  uses.  */
840               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
841                 for (nextlinks = XEXP (links, 1); nextlinks;
842                      nextlinks = XEXP (nextlinks, 1))
843                   if ((next = try_combine (insn, XEXP (links, 0),
844                                            XEXP (nextlinks, 0),
845                                            &new_direct_jump_p)) != 0)
846                     goto retry;
847
848               /* Try this insn with each REG_EQUAL note it links back to.  */
849               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
850                 {
851                   rtx set, note;
852                   rtx temp = XEXP (links, 0);
853                   if ((set = single_set (temp)) != 0
854                       && (note = find_reg_equal_equiv_note (temp)) != 0
855                       && GET_CODE (XEXP (note, 0)) != EXPR_LIST
856                       /* Avoid using a register that may already been marked
857                          dead by an earlier instruction.  */
858                       && ! unmentioned_reg_p (XEXP (note, 0), SET_SRC (set)))
859                     {
860                       /* Temporarily replace the set's source with the
861                          contents of the REG_EQUAL note.  The insn will
862                          be deleted or recognized by try_combine.  */
863                       rtx orig = SET_SRC (set);
864                       SET_SRC (set) = XEXP (note, 0);
865                       next = try_combine (insn, temp, NULL_RTX,
866                                           &new_direct_jump_p);
867                       if (next)
868                         goto retry;
869                       SET_SRC (set) = orig;
870                     }
871                 }
872
873               if (!NOTE_P (insn))
874                 record_dead_and_set_regs (insn);
875
876             retry:
877               ;
878             }
879         }
880     }
881   clear_bb_flags ();
882
883   EXECUTE_IF_SET_IN_SBITMAP (refresh_blocks, 0, i,
884                              BASIC_BLOCK (i)->flags |= BB_DIRTY);
885   new_direct_jump_p |= purge_all_dead_edges (0);
886   delete_noop_moves ();
887
888   update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
889                                     PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
890                                     | PROP_KILL_DEAD_CODE);
891
892   /* Clean up.  */
893   sbitmap_free (refresh_blocks);
894   free (uid_insn_cost);
895   free (reg_stat);
896   free (uid_cuid);
897
898   {
899     struct undo *undo, *next;
900     for (undo = undobuf.frees; undo; undo = next)
901       {
902         next = undo->next;
903         free (undo);
904       }
905     undobuf.frees = 0;
906   }
907
908   total_attempts += combine_attempts;
909   total_merges += combine_merges;
910   total_extras += combine_extras;
911   total_successes += combine_successes;
912
913   nonzero_sign_valid = 0;
914   rtl_hooks = general_rtl_hooks;
915
916   /* Make recognizer allow volatile MEMs again.  */
917   init_recog ();
918
919   return new_direct_jump_p;
920 }
921
922 /* Wipe the last_xxx fields of reg_stat in preparation for another pass.  */
923
924 static void
925 init_reg_last (void)
926 {
927   unsigned int i;
928   for (i = 0; i < combine_max_regno; i++)
929     memset (reg_stat + i, 0, offsetof (struct reg_stat, sign_bit_copies));
930 }
931 \f
932 /* Set up any promoted values for incoming argument registers.  */
933
934 static void
935 setup_incoming_promotions (void)
936 {
937   unsigned int regno;
938   rtx reg;
939   enum machine_mode mode;
940   int unsignedp;
941   rtx first = get_insns ();
942
943   if (targetm.calls.promote_function_args (TREE_TYPE (cfun->decl)))
944     {
945       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
946         /* Check whether this register can hold an incoming pointer
947            argument.  FUNCTION_ARG_REGNO_P tests outgoing register
948            numbers, so translate if necessary due to register windows.  */
949         if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (regno))
950             && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
951           {
952             record_value_for_reg
953               (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
954                                            : SIGN_EXTEND),
955                                           GET_MODE (reg),
956                                           gen_rtx_CLOBBER (mode, const0_rtx)));
957           }
958     }
959 }
960 \f
961 /* Called via note_stores.  If X is a pseudo that is narrower than
962    HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
963
964    If we are setting only a portion of X and we can't figure out what
965    portion, assume all bits will be used since we don't know what will
966    be happening.
967
968    Similarly, set how many bits of X are known to be copies of the sign bit
969    at all locations in the function.  This is the smallest number implied
970    by any set of X.  */
971
972 static void
973 set_nonzero_bits_and_sign_copies (rtx x, rtx set,
974                                   void *data ATTRIBUTE_UNUSED)
975 {
976   unsigned int num;
977
978   if (REG_P (x)
979       && REGNO (x) >= FIRST_PSEUDO_REGISTER
980       /* If this register is undefined at the start of the file, we can't
981          say what its contents were.  */
982       && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, REGNO (x))
983       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
984     {
985       if (set == 0 || GET_CODE (set) == CLOBBER)
986         {
987           reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
988           reg_stat[REGNO (x)].sign_bit_copies = 1;
989           return;
990         }
991
992       /* If this is a complex assignment, see if we can convert it into a
993          simple assignment.  */
994       set = expand_field_assignment (set);
995
996       /* If this is a simple assignment, or we have a paradoxical SUBREG,
997          set what we know about X.  */
998
999       if (SET_DEST (set) == x
1000           || (GET_CODE (SET_DEST (set)) == SUBREG
1001               && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
1002                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
1003               && SUBREG_REG (SET_DEST (set)) == x))
1004         {
1005           rtx src = SET_SRC (set);
1006
1007 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1008           /* If X is narrower than a word and SRC is a non-negative
1009              constant that would appear negative in the mode of X,
1010              sign-extend it for use in reg_stat[].nonzero_bits because some
1011              machines (maybe most) will actually do the sign-extension
1012              and this is the conservative approach.
1013
1014              ??? For 2.5, try to tighten up the MD files in this regard
1015              instead of this kludge.  */
1016
1017           if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
1018               && GET_CODE (src) == CONST_INT
1019               && INTVAL (src) > 0
1020               && 0 != (INTVAL (src)
1021                        & ((HOST_WIDE_INT) 1
1022                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
1023             src = GEN_INT (INTVAL (src)
1024                            | ((HOST_WIDE_INT) (-1)
1025                               << GET_MODE_BITSIZE (GET_MODE (x))));
1026 #endif
1027
1028           /* Don't call nonzero_bits if it cannot change anything.  */
1029           if (reg_stat[REGNO (x)].nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1030             reg_stat[REGNO (x)].nonzero_bits
1031               |= nonzero_bits (src, nonzero_bits_mode);
1032           num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1033           if (reg_stat[REGNO (x)].sign_bit_copies == 0
1034               || reg_stat[REGNO (x)].sign_bit_copies > num)
1035             reg_stat[REGNO (x)].sign_bit_copies = num;
1036         }
1037       else
1038         {
1039           reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1040           reg_stat[REGNO (x)].sign_bit_copies = 1;
1041         }
1042     }
1043 }
1044 \f
1045 /* See if INSN can be combined into I3.  PRED and SUCC are optionally
1046    insns that were previously combined into I3 or that will be combined
1047    into the merger of INSN and I3.
1048
1049    Return 0 if the combination is not allowed for any reason.
1050
1051    If the combination is allowed, *PDEST will be set to the single
1052    destination of INSN and *PSRC to the single source, and this function
1053    will return 1.  */
1054
1055 static int
1056 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
1057                rtx *pdest, rtx *psrc)
1058 {
1059   int i;
1060   rtx set = 0, src, dest;
1061   rtx p;
1062 #ifdef AUTO_INC_DEC
1063   rtx link;
1064 #endif
1065   int all_adjacent = (succ ? (next_active_insn (insn) == succ
1066                               && next_active_insn (succ) == i3)
1067                       : next_active_insn (insn) == i3);
1068
1069   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1070      or a PARALLEL consisting of such a SET and CLOBBERs.
1071
1072      If INSN has CLOBBER parallel parts, ignore them for our processing.
1073      By definition, these happen during the execution of the insn.  When it
1074      is merged with another insn, all bets are off.  If they are, in fact,
1075      needed and aren't also supplied in I3, they may be added by
1076      recog_for_combine.  Otherwise, it won't match.
1077
1078      We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1079      note.
1080
1081      Get the source and destination of INSN.  If more than one, can't
1082      combine.  */
1083
1084   if (GET_CODE (PATTERN (insn)) == SET)
1085     set = PATTERN (insn);
1086   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1087            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1088     {
1089       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1090         {
1091           rtx elt = XVECEXP (PATTERN (insn), 0, i);
1092           rtx note;
1093
1094           switch (GET_CODE (elt))
1095             {
1096             /* This is important to combine floating point insns
1097                for the SH4 port.  */
1098             case USE:
1099               /* Combining an isolated USE doesn't make sense.
1100                  We depend here on combinable_i3pat to reject them.  */
1101               /* The code below this loop only verifies that the inputs of
1102                  the SET in INSN do not change.  We call reg_set_between_p
1103                  to verify that the REG in the USE does not change between
1104                  I3 and INSN.
1105                  If the USE in INSN was for a pseudo register, the matching
1106                  insn pattern will likely match any register; combining this
1107                  with any other USE would only be safe if we knew that the
1108                  used registers have identical values, or if there was
1109                  something to tell them apart, e.g. different modes.  For
1110                  now, we forgo such complicated tests and simply disallow
1111                  combining of USES of pseudo registers with any other USE.  */
1112               if (REG_P (XEXP (elt, 0))
1113                   && GET_CODE (PATTERN (i3)) == PARALLEL)
1114                 {
1115                   rtx i3pat = PATTERN (i3);
1116                   int i = XVECLEN (i3pat, 0) - 1;
1117                   unsigned int regno = REGNO (XEXP (elt, 0));
1118
1119                   do
1120                     {
1121                       rtx i3elt = XVECEXP (i3pat, 0, i);
1122
1123                       if (GET_CODE (i3elt) == USE
1124                           && REG_P (XEXP (i3elt, 0))
1125                           && (REGNO (XEXP (i3elt, 0)) == regno
1126                               ? reg_set_between_p (XEXP (elt, 0),
1127                                                    PREV_INSN (insn), i3)
1128                               : regno >= FIRST_PSEUDO_REGISTER))
1129                         return 0;
1130                     }
1131                   while (--i >= 0);
1132                 }
1133               break;
1134
1135               /* We can ignore CLOBBERs.  */
1136             case CLOBBER:
1137               break;
1138
1139             case SET:
1140               /* Ignore SETs whose result isn't used but not those that
1141                  have side-effects.  */
1142               if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1143                   && (!(note = find_reg_note (insn, REG_EH_REGION, NULL_RTX))
1144                       || INTVAL (XEXP (note, 0)) <= 0)
1145                   && ! side_effects_p (elt))
1146                 break;
1147
1148               /* If we have already found a SET, this is a second one and
1149                  so we cannot combine with this insn.  */
1150               if (set)
1151                 return 0;
1152
1153               set = elt;
1154               break;
1155
1156             default:
1157               /* Anything else means we can't combine.  */
1158               return 0;
1159             }
1160         }
1161
1162       if (set == 0
1163           /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1164              so don't do anything with it.  */
1165           || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1166         return 0;
1167     }
1168   else
1169     return 0;
1170
1171   if (set == 0)
1172     return 0;
1173
1174   set = expand_field_assignment (set);
1175   src = SET_SRC (set), dest = SET_DEST (set);
1176
1177   /* Don't eliminate a store in the stack pointer.  */
1178   if (dest == stack_pointer_rtx
1179       /* Don't combine with an insn that sets a register to itself if it has
1180          a REG_EQUAL note.  This may be part of a REG_NO_CONFLICT sequence.  */
1181       || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1182       /* Can't merge an ASM_OPERANDS.  */
1183       || GET_CODE (src) == ASM_OPERANDS
1184       /* Can't merge a function call.  */
1185       || GET_CODE (src) == CALL
1186       /* Don't eliminate a function call argument.  */
1187       || (CALL_P (i3)
1188           && (find_reg_fusage (i3, USE, dest)
1189               || (REG_P (dest)
1190                   && REGNO (dest) < FIRST_PSEUDO_REGISTER
1191                   && global_regs[REGNO (dest)])))
1192       /* Don't substitute into an incremented register.  */
1193       || FIND_REG_INC_NOTE (i3, dest)
1194       || (succ && FIND_REG_INC_NOTE (succ, dest))
1195       /* Don't substitute into a non-local goto, this confuses CFG.  */
1196       || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1197 #if 0
1198       /* Don't combine the end of a libcall into anything.  */
1199       /* ??? This gives worse code, and appears to be unnecessary, since no
1200          pass after flow uses REG_LIBCALL/REG_RETVAL notes.  Local-alloc does
1201          use REG_RETVAL notes for noconflict blocks, but other code here
1202          makes sure that those insns don't disappear.  */
1203       || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1204 #endif
1205       /* Make sure that DEST is not used after SUCC but before I3.  */
1206       || (succ && ! all_adjacent
1207           && reg_used_between_p (dest, succ, i3))
1208       /* Make sure that the value that is to be substituted for the register
1209          does not use any registers whose values alter in between.  However,
1210          If the insns are adjacent, a use can't cross a set even though we
1211          think it might (this can happen for a sequence of insns each setting
1212          the same destination; last_set of that register might point to
1213          a NOTE).  If INSN has a REG_EQUIV note, the register is always
1214          equivalent to the memory so the substitution is valid even if there
1215          are intervening stores.  Also, don't move a volatile asm or
1216          UNSPEC_VOLATILE across any other insns.  */
1217       || (! all_adjacent
1218           && (((!MEM_P (src)
1219                 || ! find_reg_note (insn, REG_EQUIV, src))
1220                && use_crosses_set_p (src, INSN_CUID (insn)))
1221               || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1222               || GET_CODE (src) == UNSPEC_VOLATILE))
1223       /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1224          better register allocation by not doing the combine.  */
1225       || find_reg_note (i3, REG_NO_CONFLICT, dest)
1226       || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1227       /* Don't combine across a CALL_INSN, because that would possibly
1228          change whether the life span of some REGs crosses calls or not,
1229          and it is a pain to update that information.
1230          Exception: if source is a constant, moving it later can't hurt.
1231          Accept that special case, because it helps -fforce-addr a lot.  */
1232       || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
1233     return 0;
1234
1235   /* DEST must either be a REG or CC0.  */
1236   if (REG_P (dest))
1237     {
1238       /* If register alignment is being enforced for multi-word items in all
1239          cases except for parameters, it is possible to have a register copy
1240          insn referencing a hard register that is not allowed to contain the
1241          mode being copied and which would not be valid as an operand of most
1242          insns.  Eliminate this problem by not combining with such an insn.
1243
1244          Also, on some machines we don't want to extend the life of a hard
1245          register.  */
1246
1247       if (REG_P (src)
1248           && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1249                && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1250               /* Don't extend the life of a hard register unless it is
1251                  user variable (if we have few registers) or it can't
1252                  fit into the desired register (meaning something special
1253                  is going on).
1254                  Also avoid substituting a return register into I3, because
1255                  reload can't handle a conflict with constraints of other
1256                  inputs.  */
1257               || (REGNO (src) < FIRST_PSEUDO_REGISTER
1258                   && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1259         return 0;
1260     }
1261   else if (GET_CODE (dest) != CC0)
1262     return 0;
1263
1264
1265   if (GET_CODE (PATTERN (i3)) == PARALLEL)
1266     for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1267       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1268         {
1269           /* Don't substitute for a register intended as a clobberable
1270              operand.  */
1271           rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1272           if (rtx_equal_p (reg, dest))
1273             return 0;
1274
1275           /* If the clobber represents an earlyclobber operand, we must not
1276              substitute an expression containing the clobbered register.
1277              As we do not analyze the constraint strings here, we have to
1278              make the conservative assumption.  However, if the register is
1279              a fixed hard reg, the clobber cannot represent any operand;
1280              we leave it up to the machine description to either accept or
1281              reject use-and-clobber patterns.  */
1282           if (!REG_P (reg)
1283               || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1284               || !fixed_regs[REGNO (reg)])
1285             if (reg_overlap_mentioned_p (reg, src))
1286               return 0;
1287         }
1288
1289   /* If INSN contains anything volatile, or is an `asm' (whether volatile
1290      or not), reject, unless nothing volatile comes between it and I3 */
1291
1292   if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1293     {
1294       /* Make sure succ doesn't contain a volatile reference.  */
1295       if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1296         return 0;
1297
1298       for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1299         if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1300           return 0;
1301     }
1302
1303   /* If INSN is an asm, and DEST is a hard register, reject, since it has
1304      to be an explicit register variable, and was chosen for a reason.  */
1305
1306   if (GET_CODE (src) == ASM_OPERANDS
1307       && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1308     return 0;
1309
1310   /* If there are any volatile insns between INSN and I3, reject, because
1311      they might affect machine state.  */
1312
1313   for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1314     if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1315       return 0;
1316
1317   /* If INSN or I2 contains an autoincrement or autodecrement,
1318      make sure that register is not used between there and I3,
1319      and not already used in I3 either.
1320      Also insist that I3 not be a jump; if it were one
1321      and the incremented register were spilled, we would lose.  */
1322
1323 #ifdef AUTO_INC_DEC
1324   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1325     if (REG_NOTE_KIND (link) == REG_INC
1326         && (JUMP_P (i3)
1327             || reg_used_between_p (XEXP (link, 0), insn, i3)
1328             || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1329       return 0;
1330 #endif
1331
1332 #ifdef HAVE_cc0
1333   /* Don't combine an insn that follows a CC0-setting insn.
1334      An insn that uses CC0 must not be separated from the one that sets it.
1335      We do, however, allow I2 to follow a CC0-setting insn if that insn
1336      is passed as I1; in that case it will be deleted also.
1337      We also allow combining in this case if all the insns are adjacent
1338      because that would leave the two CC0 insns adjacent as well.
1339      It would be more logical to test whether CC0 occurs inside I1 or I2,
1340      but that would be much slower, and this ought to be equivalent.  */
1341
1342   p = prev_nonnote_insn (insn);
1343   if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1344       && ! all_adjacent)
1345     return 0;
1346 #endif
1347
1348   /* If we get here, we have passed all the tests and the combination is
1349      to be allowed.  */
1350
1351   *pdest = dest;
1352   *psrc = src;
1353
1354   return 1;
1355 }
1356 \f
1357 /* LOC is the location within I3 that contains its pattern or the component
1358    of a PARALLEL of the pattern.  We validate that it is valid for combining.
1359
1360    One problem is if I3 modifies its output, as opposed to replacing it
1361    entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1362    so would produce an insn that is not equivalent to the original insns.
1363
1364    Consider:
1365
1366          (set (reg:DI 101) (reg:DI 100))
1367          (set (subreg:SI (reg:DI 101) 0) <foo>)
1368
1369    This is NOT equivalent to:
1370
1371          (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1372                     (set (reg:DI 101) (reg:DI 100))])
1373
1374    Not only does this modify 100 (in which case it might still be valid
1375    if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1376
1377    We can also run into a problem if I2 sets a register that I1
1378    uses and I1 gets directly substituted into I3 (not via I2).  In that
1379    case, we would be getting the wrong value of I2DEST into I3, so we
1380    must reject the combination.  This case occurs when I2 and I1 both
1381    feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1382    If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1383    of a SET must prevent combination from occurring.
1384
1385    Before doing the above check, we first try to expand a field assignment
1386    into a set of logical operations.
1387
1388    If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1389    we place a register that is both set and used within I3.  If more than one
1390    such register is detected, we fail.
1391
1392    Return 1 if the combination is valid, zero otherwise.  */
1393
1394 static int
1395 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
1396                   int i1_not_in_src, rtx *pi3dest_killed)
1397 {
1398   rtx x = *loc;
1399
1400   if (GET_CODE (x) == SET)
1401     {
1402       rtx set = x ;
1403       rtx dest = SET_DEST (set);
1404       rtx src = SET_SRC (set);
1405       rtx inner_dest = dest;
1406
1407       while (GET_CODE (inner_dest) == STRICT_LOW_PART
1408              || GET_CODE (inner_dest) == SUBREG
1409              || GET_CODE (inner_dest) == ZERO_EXTRACT)
1410         inner_dest = XEXP (inner_dest, 0);
1411
1412       /* Check for the case where I3 modifies its output, as discussed
1413          above.  We don't want to prevent pseudos from being combined
1414          into the address of a MEM, so only prevent the combination if
1415          i1 or i2 set the same MEM.  */
1416       if ((inner_dest != dest &&
1417            (!MEM_P (inner_dest)
1418             || rtx_equal_p (i2dest, inner_dest)
1419             || (i1dest && rtx_equal_p (i1dest, inner_dest)))
1420            && (reg_overlap_mentioned_p (i2dest, inner_dest)
1421                || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1422
1423           /* This is the same test done in can_combine_p except we can't test
1424              all_adjacent; we don't have to, since this instruction will stay
1425              in place, thus we are not considering increasing the lifetime of
1426              INNER_DEST.
1427
1428              Also, if this insn sets a function argument, combining it with
1429              something that might need a spill could clobber a previous
1430              function argument; the all_adjacent test in can_combine_p also
1431              checks this; here, we do a more specific test for this case.  */
1432
1433           || (REG_P (inner_dest)
1434               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1435               && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1436                                         GET_MODE (inner_dest))))
1437           || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1438         return 0;
1439
1440       /* If DEST is used in I3, it is being killed in this insn,
1441          so record that for later.
1442          Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1443          STACK_POINTER_REGNUM, since these are always considered to be
1444          live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
1445       if (pi3dest_killed && REG_P (dest)
1446           && reg_referenced_p (dest, PATTERN (i3))
1447           && REGNO (dest) != FRAME_POINTER_REGNUM
1448 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1449           && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1450 #endif
1451 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1452           && (REGNO (dest) != ARG_POINTER_REGNUM
1453               || ! fixed_regs [REGNO (dest)])
1454 #endif
1455           && REGNO (dest) != STACK_POINTER_REGNUM)
1456         {
1457           if (*pi3dest_killed)
1458             return 0;
1459
1460           *pi3dest_killed = dest;
1461         }
1462     }
1463
1464   else if (GET_CODE (x) == PARALLEL)
1465     {
1466       int i;
1467
1468       for (i = 0; i < XVECLEN (x, 0); i++)
1469         if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1470                                 i1_not_in_src, pi3dest_killed))
1471           return 0;
1472     }
1473
1474   return 1;
1475 }
1476 \f
1477 /* Return 1 if X is an arithmetic expression that contains a multiplication
1478    and division.  We don't count multiplications by powers of two here.  */
1479
1480 static int
1481 contains_muldiv (rtx x)
1482 {
1483   switch (GET_CODE (x))
1484     {
1485     case MOD:  case DIV:  case UMOD:  case UDIV:
1486       return 1;
1487
1488     case MULT:
1489       return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1490                 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1491     default:
1492       if (BINARY_P (x))
1493         return contains_muldiv (XEXP (x, 0))
1494             || contains_muldiv (XEXP (x, 1));
1495
1496       if (UNARY_P (x))
1497         return contains_muldiv (XEXP (x, 0));
1498
1499       return 0;
1500     }
1501 }
1502 \f
1503 /* Determine whether INSN can be used in a combination.  Return nonzero if
1504    not.  This is used in try_combine to detect early some cases where we
1505    can't perform combinations.  */
1506
1507 static int
1508 cant_combine_insn_p (rtx insn)
1509 {
1510   rtx set;
1511   rtx src, dest;
1512
1513   /* If this isn't really an insn, we can't do anything.
1514      This can occur when flow deletes an insn that it has merged into an
1515      auto-increment address.  */
1516   if (! INSN_P (insn))
1517     return 1;
1518
1519   /* Never combine loads and stores involving hard regs that are likely
1520      to be spilled.  The register allocator can usually handle such
1521      reg-reg moves by tying.  If we allow the combiner to make
1522      substitutions of likely-spilled regs, we may abort in reload.
1523      As an exception, we allow combinations involving fixed regs; these are
1524      not available to the register allocator so there's no risk involved.  */
1525
1526   set = single_set (insn);
1527   if (! set)
1528     return 0;
1529   src = SET_SRC (set);
1530   dest = SET_DEST (set);
1531   if (GET_CODE (src) == SUBREG)
1532     src = SUBREG_REG (src);
1533   if (GET_CODE (dest) == SUBREG)
1534     dest = SUBREG_REG (dest);
1535   if (REG_P (src) && REG_P (dest)
1536       && ((REGNO (src) < FIRST_PSEUDO_REGISTER
1537            && ! fixed_regs[REGNO (src)]
1538            && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
1539           || (REGNO (dest) < FIRST_PSEUDO_REGISTER
1540               && ! fixed_regs[REGNO (dest)]
1541               && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
1542     return 1;
1543
1544   return 0;
1545 }
1546
1547 /* Adjust INSN after we made a change to its destination.
1548
1549    Changing the destination can invalidate notes that say something about
1550    the results of the insn and a LOG_LINK pointing to the insn.  */
1551
1552 static void
1553 adjust_for_new_dest (rtx insn)
1554 {
1555   rtx *loc;
1556
1557   /* For notes, be conservative and simply remove them.  */
1558   loc = &REG_NOTES (insn);
1559   while (*loc)
1560     {
1561       enum reg_note kind = REG_NOTE_KIND (*loc);
1562       if (kind == REG_EQUAL || kind == REG_EQUIV)
1563         *loc = XEXP (*loc, 1);
1564       else
1565         loc = &XEXP (*loc, 1);
1566     }
1567
1568   /* The new insn will have a destination that was previously the destination
1569      of an insn just above it.  Call distribute_links to make a LOG_LINK from
1570      the next use of that destination.  */
1571   distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
1572 }
1573
1574 /* Try to combine the insns I1 and I2 into I3.
1575    Here I1 and I2 appear earlier than I3.
1576    I1 can be zero; then we combine just I2 into I3.
1577
1578    If we are combining three insns and the resulting insn is not recognized,
1579    try splitting it into two insns.  If that happens, I2 and I3 are retained
1580    and I1 is pseudo-deleted by turning it into a NOTE.  Otherwise, I1 and I2
1581    are pseudo-deleted.
1582
1583    Return 0 if the combination does not work.  Then nothing is changed.
1584    If we did the combination, return the insn at which combine should
1585    resume scanning.
1586
1587    Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
1588    new direct jump instruction.  */
1589
1590 static rtx
1591 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
1592 {
1593   /* New patterns for I3 and I2, respectively.  */
1594   rtx newpat, newi2pat = 0;
1595   rtvec newpat_vec_with_clobbers = 0;
1596   int substed_i2 = 0, substed_i1 = 0;
1597   /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead.  */
1598   int added_sets_1, added_sets_2;
1599   /* Total number of SETs to put into I3.  */
1600   int total_sets;
1601   /* Nonzero if I2's body now appears in I3.  */
1602   int i2_is_used;
1603   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
1604   int insn_code_number, i2_code_number = 0, other_code_number = 0;
1605   /* Contains I3 if the destination of I3 is used in its source, which means
1606      that the old life of I3 is being killed.  If that usage is placed into
1607      I2 and not in I3, a REG_DEAD note must be made.  */
1608   rtx i3dest_killed = 0;
1609   /* SET_DEST and SET_SRC of I2 and I1.  */
1610   rtx i2dest, i2src, i1dest = 0, i1src = 0;
1611   /* PATTERN (I2), or a copy of it in certain cases.  */
1612   rtx i2pat;
1613   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
1614   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1615   int i1_feeds_i3 = 0;
1616   /* Notes that must be added to REG_NOTES in I3 and I2.  */
1617   rtx new_i3_notes, new_i2_notes;
1618   /* Notes that we substituted I3 into I2 instead of the normal case.  */
1619   int i3_subst_into_i2 = 0;
1620   /* Notes that I1, I2 or I3 is a MULT operation.  */
1621   int have_mult = 0;
1622   int swap_i2i3 = 0;
1623
1624   int maxreg;
1625   rtx temp;
1626   rtx link;
1627   int i;
1628
1629   /* Exit early if one of the insns involved can't be used for
1630      combinations.  */
1631   if (cant_combine_insn_p (i3)
1632       || cant_combine_insn_p (i2)
1633       || (i1 && cant_combine_insn_p (i1))
1634       /* We also can't do anything if I3 has a
1635          REG_LIBCALL note since we don't want to disrupt the contiguity of a
1636          libcall.  */
1637 #if 0
1638       /* ??? This gives worse code, and appears to be unnecessary, since no
1639          pass after flow uses REG_LIBCALL/REG_RETVAL notes.  */
1640       || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
1641 #endif
1642       )
1643     return 0;
1644
1645   combine_attempts++;
1646   undobuf.other_insn = 0;
1647
1648   /* Reset the hard register usage information.  */
1649   CLEAR_HARD_REG_SET (newpat_used_regs);
1650
1651   /* If I1 and I2 both feed I3, they can be in any order.  To simplify the
1652      code below, set I1 to be the earlier of the two insns.  */
1653   if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1654     temp = i1, i1 = i2, i2 = temp;
1655
1656   added_links_insn = 0;
1657
1658   /* First check for one important special-case that the code below will
1659      not handle.  Namely, the case where I1 is zero, I2 is a PARALLEL
1660      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
1661      we may be able to replace that destination with the destination of I3.
1662      This occurs in the common code where we compute both a quotient and
1663      remainder into a structure, in which case we want to do the computation
1664      directly into the structure to avoid register-register copies.
1665
1666      Note that this case handles both multiple sets in I2 and also
1667      cases where I2 has a number of CLOBBER or PARALLELs.
1668
1669      We make very conservative checks below and only try to handle the
1670      most common cases of this.  For example, we only handle the case
1671      where I2 and I3 are adjacent to avoid making difficult register
1672      usage tests.  */
1673
1674   if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
1675       && REG_P (SET_SRC (PATTERN (i3)))
1676       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1677       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1678       && GET_CODE (PATTERN (i2)) == PARALLEL
1679       && ! side_effects_p (SET_DEST (PATTERN (i3)))
1680       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1681          below would need to check what is inside (and reg_overlap_mentioned_p
1682          doesn't support those codes anyway).  Don't allow those destinations;
1683          the resulting insn isn't likely to be recognized anyway.  */
1684       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1685       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1686       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1687                                     SET_DEST (PATTERN (i3)))
1688       && next_real_insn (i2) == i3)
1689     {
1690       rtx p2 = PATTERN (i2);
1691
1692       /* Make sure that the destination of I3,
1693          which we are going to substitute into one output of I2,
1694          is not used within another output of I2.  We must avoid making this:
1695          (parallel [(set (mem (reg 69)) ...)
1696                     (set (reg 69) ...)])
1697          which is not well-defined as to order of actions.
1698          (Besides, reload can't handle output reloads for this.)
1699
1700          The problem can also happen if the dest of I3 is a memory ref,
1701          if another dest in I2 is an indirect memory ref.  */
1702       for (i = 0; i < XVECLEN (p2, 0); i++)
1703         if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1704              || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1705             && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1706                                         SET_DEST (XVECEXP (p2, 0, i))))
1707           break;
1708
1709       if (i == XVECLEN (p2, 0))
1710         for (i = 0; i < XVECLEN (p2, 0); i++)
1711           if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1712                || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1713               && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1714             {
1715               combine_merges++;
1716
1717               subst_insn = i3;
1718               subst_low_cuid = INSN_CUID (i2);
1719
1720               added_sets_2 = added_sets_1 = 0;
1721               i2dest = SET_SRC (PATTERN (i3));
1722
1723               /* Replace the dest in I2 with our dest and make the resulting
1724                  insn the new pattern for I3.  Then skip to where we
1725                  validate the pattern.  Everything was set up above.  */
1726               SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1727                      SET_DEST (PATTERN (i3)));
1728
1729               newpat = p2;
1730               i3_subst_into_i2 = 1;
1731               goto validate_replacement;
1732             }
1733     }
1734
1735   /* If I2 is setting a double-word pseudo to a constant and I3 is setting
1736      one of those words to another constant, merge them by making a new
1737      constant.  */
1738   if (i1 == 0
1739       && (temp = single_set (i2)) != 0
1740       && (GET_CODE (SET_SRC (temp)) == CONST_INT
1741           || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
1742       && REG_P (SET_DEST (temp))
1743       && GET_MODE_CLASS (GET_MODE (SET_DEST (temp))) == MODE_INT
1744       && GET_MODE_SIZE (GET_MODE (SET_DEST (temp))) == 2 * UNITS_PER_WORD
1745       && GET_CODE (PATTERN (i3)) == SET
1746       && GET_CODE (SET_DEST (PATTERN (i3))) == SUBREG
1747       && SUBREG_REG (SET_DEST (PATTERN (i3))) == SET_DEST (temp)
1748       && GET_MODE_CLASS (GET_MODE (SET_DEST (PATTERN (i3)))) == MODE_INT
1749       && GET_MODE_SIZE (GET_MODE (SET_DEST (PATTERN (i3)))) == UNITS_PER_WORD
1750       && GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT)
1751     {
1752       HOST_WIDE_INT lo, hi;
1753
1754       if (GET_CODE (SET_SRC (temp)) == CONST_INT)
1755         lo = INTVAL (SET_SRC (temp)), hi = lo < 0 ? -1 : 0;
1756       else
1757         {
1758           lo = CONST_DOUBLE_LOW (SET_SRC (temp));
1759           hi = CONST_DOUBLE_HIGH (SET_SRC (temp));
1760         }
1761
1762       if (subreg_lowpart_p (SET_DEST (PATTERN (i3))))
1763         {
1764           /* We don't handle the case of the target word being wider
1765              than a host wide int.  */
1766           gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD);
1767
1768           lo &= ~(UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1);
1769           lo |= (INTVAL (SET_SRC (PATTERN (i3)))
1770                  & (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1771         }
1772       else if (HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
1773         hi = INTVAL (SET_SRC (PATTERN (i3)));
1774       else if (HOST_BITS_PER_WIDE_INT >= 2 * BITS_PER_WORD)
1775         {
1776           int sign = -(int) ((unsigned HOST_WIDE_INT) lo
1777                              >> (HOST_BITS_PER_WIDE_INT - 1));
1778
1779           lo &= ~ (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1780                    (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1781           lo |= (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1782                  (INTVAL (SET_SRC (PATTERN (i3)))));
1783           if (hi == sign)
1784             hi = lo < 0 ? -1 : 0;
1785         }
1786       else
1787         /* We don't handle the case of the higher word not fitting
1788            entirely in either hi or lo.  */
1789         gcc_unreachable ();
1790
1791       combine_merges++;
1792       subst_insn = i3;
1793       subst_low_cuid = INSN_CUID (i2);
1794       added_sets_2 = added_sets_1 = 0;
1795       i2dest = SET_DEST (temp);
1796
1797       SUBST (SET_SRC (temp),
1798              immed_double_const (lo, hi, GET_MODE (SET_DEST (temp))));
1799
1800       newpat = PATTERN (i2);
1801       goto validate_replacement;
1802     }
1803
1804 #ifndef HAVE_cc0
1805   /* If we have no I1 and I2 looks like:
1806         (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1807                    (set Y OP)])
1808      make up a dummy I1 that is
1809         (set Y OP)
1810      and change I2 to be
1811         (set (reg:CC X) (compare:CC Y (const_int 0)))
1812
1813      (We can ignore any trailing CLOBBERs.)
1814
1815      This undoes a previous combination and allows us to match a branch-and-
1816      decrement insn.  */
1817
1818   if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1819       && XVECLEN (PATTERN (i2), 0) >= 2
1820       && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1821       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1822           == MODE_CC)
1823       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1824       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1825       && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1826       && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
1827       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1828                       SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1829     {
1830       for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1831         if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1832           break;
1833
1834       if (i == 1)
1835         {
1836           /* We make I1 with the same INSN_UID as I2.  This gives it
1837              the same INSN_CUID for value tracking.  Our fake I1 will
1838              never appear in the insn stream so giving it the same INSN_UID
1839              as I2 will not cause a problem.  */
1840
1841           i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1842                              BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
1843                              XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1844                              NULL_RTX);
1845
1846           SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1847           SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1848                  SET_DEST (PATTERN (i1)));
1849         }
1850     }
1851 #endif
1852
1853   /* Verify that I2 and I1 are valid for combining.  */
1854   if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1855       || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1856     {
1857       undo_all ();
1858       return 0;
1859     }
1860
1861   /* Record whether I2DEST is used in I2SRC and similarly for the other
1862      cases.  Knowing this will help in register status updating below.  */
1863   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1864   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1865   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1866
1867   /* See if I1 directly feeds into I3.  It does if I1DEST is not used
1868      in I2SRC.  */
1869   i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1870
1871   /* Ensure that I3's pattern can be the destination of combines.  */
1872   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1873                           i1 && i2dest_in_i1src && i1_feeds_i3,
1874                           &i3dest_killed))
1875     {
1876       undo_all ();
1877       return 0;
1878     }
1879
1880   /* See if any of the insns is a MULT operation.  Unless one is, we will
1881      reject a combination that is, since it must be slower.  Be conservative
1882      here.  */
1883   if (GET_CODE (i2src) == MULT
1884       || (i1 != 0 && GET_CODE (i1src) == MULT)
1885       || (GET_CODE (PATTERN (i3)) == SET
1886           && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1887     have_mult = 1;
1888
1889   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1890      We used to do this EXCEPT in one case: I3 has a post-inc in an
1891      output operand.  However, that exception can give rise to insns like
1892         mov r3,(r3)+
1893      which is a famous insn on the PDP-11 where the value of r3 used as the
1894      source was model-dependent.  Avoid this sort of thing.  */
1895
1896 #if 0
1897   if (!(GET_CODE (PATTERN (i3)) == SET
1898         && REG_P (SET_SRC (PATTERN (i3)))
1899         && MEM_P (SET_DEST (PATTERN (i3)))
1900         && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1901             || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1902     /* It's not the exception.  */
1903 #endif
1904 #ifdef AUTO_INC_DEC
1905     for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1906       if (REG_NOTE_KIND (link) == REG_INC
1907           && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1908               || (i1 != 0
1909                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1910         {
1911           undo_all ();
1912           return 0;
1913         }
1914 #endif
1915
1916   /* See if the SETs in I1 or I2 need to be kept around in the merged
1917      instruction: whenever the value set there is still needed past I3.
1918      For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1919
1920      For the SET in I1, we have two cases:  If I1 and I2 independently
1921      feed into I3, the set in I1 needs to be kept around if I1DEST dies
1922      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
1923      in I1 needs to be kept around unless I1DEST dies or is set in either
1924      I2 or I3.  We can distinguish these cases by seeing if I2SRC mentions
1925      I1DEST.  If so, we know I1 feeds into I2.  */
1926
1927   added_sets_2 = ! dead_or_set_p (i3, i2dest);
1928
1929   added_sets_1
1930     = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1931                : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1932
1933   /* If the set in I2 needs to be kept around, we must make a copy of
1934      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1935      PATTERN (I2), we are only substituting for the original I1DEST, not into
1936      an already-substituted copy.  This also prevents making self-referential
1937      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1938      I2DEST.  */
1939
1940   i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1941            ? gen_rtx_SET (VOIDmode, i2dest, i2src)
1942            : PATTERN (i2));
1943
1944   if (added_sets_2)
1945     i2pat = copy_rtx (i2pat);
1946
1947   combine_merges++;
1948
1949   /* Substitute in the latest insn for the regs set by the earlier ones.  */
1950
1951   maxreg = max_reg_num ();
1952
1953   subst_insn = i3;
1954
1955   /* It is possible that the source of I2 or I1 may be performing an
1956      unneeded operation, such as a ZERO_EXTEND of something that is known
1957      to have the high part zero.  Handle that case by letting subst look at
1958      the innermost one of them.
1959
1960      Another way to do this would be to have a function that tries to
1961      simplify a single insn instead of merging two or more insns.  We don't
1962      do this because of the potential of infinite loops and because
1963      of the potential extra memory required.  However, doing it the way
1964      we are is a bit of a kludge and doesn't catch all cases.
1965
1966      But only do this if -fexpensive-optimizations since it slows things down
1967      and doesn't usually win.  */
1968
1969   if (flag_expensive_optimizations)
1970     {
1971       /* Pass pc_rtx so no substitutions are done, just simplifications.  */
1972       if (i1)
1973         {
1974           subst_low_cuid = INSN_CUID (i1);
1975           i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1976         }
1977       else
1978         {
1979           subst_low_cuid = INSN_CUID (i2);
1980           i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1981         }
1982     }
1983
1984 #ifndef HAVE_cc0
1985   /* Many machines that don't use CC0 have insns that can both perform an
1986      arithmetic operation and set the condition code.  These operations will
1987      be represented as a PARALLEL with the first element of the vector
1988      being a COMPARE of an arithmetic operation with the constant zero.
1989      The second element of the vector will set some pseudo to the result
1990      of the same arithmetic operation.  If we simplify the COMPARE, we won't
1991      match such a pattern and so will generate an extra insn.   Here we test
1992      for this case, where both the comparison and the operation result are
1993      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1994      I2SRC.  Later we will make the PARALLEL that contains I2.  */
1995
1996   if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1997       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1998       && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1999       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2000     {
2001 #ifdef SELECT_CC_MODE
2002       rtx *cc_use;
2003       enum machine_mode compare_mode;
2004 #endif
2005
2006       newpat = PATTERN (i3);
2007       SUBST (XEXP (SET_SRC (newpat), 0), i2src);
2008
2009       i2_is_used = 1;
2010
2011 #ifdef SELECT_CC_MODE
2012       /* See if a COMPARE with the operand we substituted in should be done
2013          with the mode that is currently being used.  If not, do the same
2014          processing we do in `subst' for a SET; namely, if the destination
2015          is used only once, try to replace it with a register of the proper
2016          mode and also replace the COMPARE.  */
2017       if (undobuf.other_insn == 0
2018           && (cc_use = find_single_use (SET_DEST (newpat), i3,
2019                                         &undobuf.other_insn))
2020           && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
2021                                               i2src, const0_rtx))
2022               != GET_MODE (SET_DEST (newpat))))
2023         {
2024           unsigned int regno = REGNO (SET_DEST (newpat));
2025           rtx new_dest = gen_rtx_REG (compare_mode, regno);
2026
2027           if (regno < FIRST_PSEUDO_REGISTER
2028               || (REG_N_SETS (regno) == 1 && ! added_sets_2
2029                   && ! REG_USERVAR_P (SET_DEST (newpat))))
2030             {
2031               if (regno >= FIRST_PSEUDO_REGISTER)
2032                 SUBST (regno_reg_rtx[regno], new_dest);
2033
2034               SUBST (SET_DEST (newpat), new_dest);
2035               SUBST (XEXP (*cc_use, 0), new_dest);
2036               SUBST (SET_SRC (newpat),
2037                      gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
2038             }
2039           else
2040             undobuf.other_insn = 0;
2041         }
2042 #endif
2043     }
2044   else
2045 #endif
2046     {
2047       n_occurrences = 0;                /* `subst' counts here */
2048
2049       /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
2050          need to make a unique copy of I2SRC each time we substitute it
2051          to avoid self-referential rtl.  */
2052
2053       subst_low_cuid = INSN_CUID (i2);
2054       newpat = subst (PATTERN (i3), i2dest, i2src, 0,
2055                       ! i1_feeds_i3 && i1dest_in_i1src);
2056       substed_i2 = 1;
2057
2058       /* Record whether i2's body now appears within i3's body.  */
2059       i2_is_used = n_occurrences;
2060     }
2061
2062   /* If we already got a failure, don't try to do more.  Otherwise,
2063      try to substitute in I1 if we have it.  */
2064
2065   if (i1 && GET_CODE (newpat) != CLOBBER)
2066     {
2067       /* Before we can do this substitution, we must redo the test done
2068          above (see detailed comments there) that ensures  that I1DEST
2069          isn't mentioned in any SETs in NEWPAT that are field assignments.  */
2070
2071       if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
2072                               0, (rtx*) 0))
2073         {
2074           undo_all ();
2075           return 0;
2076         }
2077
2078       n_occurrences = 0;
2079       subst_low_cuid = INSN_CUID (i1);
2080       newpat = subst (newpat, i1dest, i1src, 0, 0);
2081       substed_i1 = 1;
2082     }
2083
2084   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
2085      to count all the ways that I2SRC and I1SRC can be used.  */
2086   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
2087        && i2_is_used + added_sets_2 > 1)
2088       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2089           && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
2090               > 1))
2091       /* Fail if we tried to make a new register (we used to abort, but there's
2092          really no reason to).  */
2093       || max_reg_num () != maxreg
2094       /* Fail if we couldn't do something and have a CLOBBER.  */
2095       || GET_CODE (newpat) == CLOBBER
2096       /* Fail if this new pattern is a MULT and we didn't have one before
2097          at the outer level.  */
2098       || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
2099           && ! have_mult))
2100     {
2101       undo_all ();
2102       return 0;
2103     }
2104
2105   /* If the actions of the earlier insns must be kept
2106      in addition to substituting them into the latest one,
2107      we must make a new PARALLEL for the latest insn
2108      to hold additional the SETs.  */
2109
2110   if (added_sets_1 || added_sets_2)
2111     {
2112       combine_extras++;
2113
2114       if (GET_CODE (newpat) == PARALLEL)
2115         {
2116           rtvec old = XVEC (newpat, 0);
2117           total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
2118           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2119           memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
2120                   sizeof (old->elem[0]) * old->num_elem);
2121         }
2122       else
2123         {
2124           rtx old = newpat;
2125           total_sets = 1 + added_sets_1 + added_sets_2;
2126           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2127           XVECEXP (newpat, 0, 0) = old;
2128         }
2129
2130       if (added_sets_1)
2131         XVECEXP (newpat, 0, --total_sets)
2132           = (GET_CODE (PATTERN (i1)) == PARALLEL
2133              ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
2134
2135       if (added_sets_2)
2136         {
2137           /* If there is no I1, use I2's body as is.  We used to also not do
2138              the subst call below if I2 was substituted into I3,
2139              but that could lose a simplification.  */
2140           if (i1 == 0)
2141             XVECEXP (newpat, 0, --total_sets) = i2pat;
2142           else
2143             /* See comment where i2pat is assigned.  */
2144             XVECEXP (newpat, 0, --total_sets)
2145               = subst (i2pat, i1dest, i1src, 0, 0);
2146         }
2147     }
2148
2149   /* We come here when we are replacing a destination in I2 with the
2150      destination of I3.  */
2151  validate_replacement:
2152
2153   /* Note which hard regs this insn has as inputs.  */
2154   mark_used_regs_combine (newpat);
2155
2156   /* If recog_for_combine fails, it strips existing clobbers.  If we'll
2157      consider splitting this pattern, we might need these clobbers.  */
2158   if (i1 && GET_CODE (newpat) == PARALLEL
2159       && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
2160     {
2161       int len = XVECLEN (newpat, 0);
2162
2163       newpat_vec_with_clobbers = rtvec_alloc (len);
2164       for (i = 0; i < len; i++)
2165         RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
2166     }
2167
2168   /* Is the result of combination a valid instruction?  */
2169   insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2170
2171   /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2172      the second SET's destination is a register that is unused and isn't
2173      marked as an instruction that might trap in an EH region.  In that case,
2174      we just need the first SET.   This can occur when simplifying a divmod
2175      insn.  We *must* test for this case here because the code below that
2176      splits two independent SETs doesn't handle this case correctly when it
2177      updates the register status.
2178
2179      It's pointless doing this if we originally had two sets, one from
2180      i3, and one from i2.  Combining then splitting the parallel results
2181      in the original i2 again plus an invalid insn (which we delete).
2182      The net effect is only to move instructions around, which makes
2183      debug info less accurate.
2184
2185      Also check the case where the first SET's destination is unused.
2186      That would not cause incorrect code, but does cause an unneeded
2187      insn to remain.  */
2188
2189   if (insn_code_number < 0
2190       && !(added_sets_2 && i1 == 0)
2191       && GET_CODE (newpat) == PARALLEL
2192       && XVECLEN (newpat, 0) == 2
2193       && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2194       && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2195       && asm_noperands (newpat) < 0)
2196     {
2197       rtx set0 = XVECEXP (newpat, 0, 0);
2198       rtx set1 = XVECEXP (newpat, 0, 1);
2199       rtx note;
2200
2201       if (((REG_P (SET_DEST (set1))
2202             && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
2203            || (GET_CODE (SET_DEST (set1)) == SUBREG
2204                && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
2205           && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2206               || INTVAL (XEXP (note, 0)) <= 0)
2207           && ! side_effects_p (SET_SRC (set1)))
2208         {
2209           newpat = set0;
2210           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2211         }
2212
2213       else if (((REG_P (SET_DEST (set0))
2214                  && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
2215                 || (GET_CODE (SET_DEST (set0)) == SUBREG
2216                     && find_reg_note (i3, REG_UNUSED,
2217                                       SUBREG_REG (SET_DEST (set0)))))
2218                && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2219                    || INTVAL (XEXP (note, 0)) <= 0)
2220                && ! side_effects_p (SET_SRC (set0)))
2221         {
2222           newpat = set1;
2223           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2224
2225           if (insn_code_number >= 0)
2226             {
2227               /* If we will be able to accept this, we have made a
2228                  change to the destination of I3.  This requires us to
2229                  do a few adjustments.  */
2230
2231               PATTERN (i3) = newpat;
2232               adjust_for_new_dest (i3);
2233             }
2234         }
2235     }
2236
2237   /* If we were combining three insns and the result is a simple SET
2238      with no ASM_OPERANDS that wasn't recognized, try to split it into two
2239      insns.  There are two ways to do this.  It can be split using a
2240      machine-specific method (like when you have an addition of a large
2241      constant) or by combine in the function find_split_point.  */
2242
2243   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2244       && asm_noperands (newpat) < 0)
2245     {
2246       rtx m_split, *split;
2247       rtx ni2dest = i2dest;
2248
2249       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
2250          use I2DEST as a scratch register will help.  In the latter case,
2251          convert I2DEST to the mode of the source of NEWPAT if we can.  */
2252
2253       m_split = split_insns (newpat, i3);
2254
2255       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2256          inputs of NEWPAT.  */
2257
2258       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2259          possible to try that as a scratch reg.  This would require adding
2260          more code to make it work though.  */
2261
2262       if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
2263         {
2264           /* If I2DEST is a hard register or the only use of a pseudo,
2265              we can change its mode.  */
2266           if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
2267               && GET_MODE (SET_DEST (newpat)) != VOIDmode
2268               && REG_P (i2dest)
2269               && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2270                   || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2271                       && ! REG_USERVAR_P (i2dest))))
2272             ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
2273                                    REGNO (i2dest));
2274
2275           m_split = split_insns (gen_rtx_PARALLEL
2276                                  (VOIDmode,
2277                                   gen_rtvec (2, newpat,
2278                                              gen_rtx_CLOBBER (VOIDmode,
2279                                                               ni2dest))),
2280                                  i3);
2281           /* If the split with the mode-changed register didn't work, try
2282              the original register.  */
2283           if (! m_split && ni2dest != i2dest)
2284             {
2285               ni2dest = i2dest;
2286               m_split = split_insns (gen_rtx_PARALLEL
2287                                      (VOIDmode,
2288                                       gen_rtvec (2, newpat,
2289                                                  gen_rtx_CLOBBER (VOIDmode,
2290                                                                   i2dest))),
2291                                      i3);
2292             }
2293         }
2294
2295       /* If recog_for_combine has discarded clobbers, try to use them
2296          again for the split.  */
2297       if (m_split == 0 && newpat_vec_with_clobbers)
2298         m_split
2299           = split_insns (gen_rtx_PARALLEL (VOIDmode,
2300                                            newpat_vec_with_clobbers), i3);
2301
2302       if (m_split && NEXT_INSN (m_split) == NULL_RTX)
2303         {
2304           m_split = PATTERN (m_split);
2305           insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
2306           if (insn_code_number >= 0)
2307             newpat = m_split;
2308         }
2309       else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
2310                && (next_real_insn (i2) == i3
2311                    || ! use_crosses_set_p (PATTERN (m_split), INSN_CUID (i2))))
2312         {
2313           rtx i2set, i3set;
2314           rtx newi3pat = PATTERN (NEXT_INSN (m_split));
2315           newi2pat = PATTERN (m_split);
2316
2317           i3set = single_set (NEXT_INSN (m_split));
2318           i2set = single_set (m_split);
2319
2320           /* In case we changed the mode of I2DEST, replace it in the
2321              pseudo-register table here.  We can't do it above in case this
2322              code doesn't get executed and we do a split the other way.  */
2323
2324           if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2325             SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
2326
2327           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2328
2329           /* If I2 or I3 has multiple SETs, we won't know how to track
2330              register status, so don't use these insns.  If I2's destination
2331              is used between I2 and I3, we also can't use these insns.  */
2332
2333           if (i2_code_number >= 0 && i2set && i3set
2334               && (next_real_insn (i2) == i3
2335                   || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
2336             insn_code_number = recog_for_combine (&newi3pat, i3,
2337                                                   &new_i3_notes);
2338           if (insn_code_number >= 0)
2339             newpat = newi3pat;
2340
2341           /* It is possible that both insns now set the destination of I3.
2342              If so, we must show an extra use of it.  */
2343
2344           if (insn_code_number >= 0)
2345             {
2346               rtx new_i3_dest = SET_DEST (i3set);
2347               rtx new_i2_dest = SET_DEST (i2set);
2348
2349               while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
2350                      || GET_CODE (new_i3_dest) == STRICT_LOW_PART
2351                      || GET_CODE (new_i3_dest) == SUBREG)
2352                 new_i3_dest = XEXP (new_i3_dest, 0);
2353
2354               while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
2355                      || GET_CODE (new_i2_dest) == STRICT_LOW_PART
2356                      || GET_CODE (new_i2_dest) == SUBREG)
2357                 new_i2_dest = XEXP (new_i2_dest, 0);
2358
2359               if (REG_P (new_i3_dest)
2360                   && REG_P (new_i2_dest)
2361                   && REGNO (new_i3_dest) == REGNO (new_i2_dest))
2362                 REG_N_SETS (REGNO (new_i2_dest))++;
2363             }
2364         }
2365
2366       /* If we can split it and use I2DEST, go ahead and see if that
2367          helps things be recognized.  Verify that none of the registers
2368          are set between I2 and I3.  */
2369       if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
2370 #ifdef HAVE_cc0
2371           && REG_P (i2dest)
2372 #endif
2373           /* We need I2DEST in the proper mode.  If it is a hard register
2374              or the only use of a pseudo, we can change its mode.
2375              Make sure we don't change a hard register to have a mode that
2376              isn't valid for it, or change the number of registers.  */
2377           && (GET_MODE (*split) == GET_MODE (i2dest)
2378               || GET_MODE (*split) == VOIDmode
2379               || (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2380                   && HARD_REGNO_MODE_OK (REGNO (i2dest), GET_MODE (*split))
2381                   && (HARD_REGNO_NREGS (REGNO (i2dest), GET_MODE (i2dest))
2382                       == HARD_REGNO_NREGS (REGNO (i2dest), GET_MODE (*split))))
2383               || (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER
2384                   && REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2385                   && ! REG_USERVAR_P (i2dest)))
2386           && (next_real_insn (i2) == i3
2387               || ! use_crosses_set_p (*split, INSN_CUID (i2)))
2388           /* We can't overwrite I2DEST if its value is still used by
2389              NEWPAT.  */
2390           && ! reg_referenced_p (i2dest, newpat))
2391         {
2392           rtx newdest = i2dest;
2393           enum rtx_code split_code = GET_CODE (*split);
2394           enum machine_mode split_mode = GET_MODE (*split);
2395
2396           /* Get NEWDEST as a register in the proper mode.  We have already
2397              validated that we can do this.  */
2398           if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
2399             {
2400               newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
2401
2402               if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2403                 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2404             }
2405
2406           /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2407              an ASHIFT.  This can occur if it was inside a PLUS and hence
2408              appeared to be a memory address.  This is a kludge.  */
2409           if (split_code == MULT
2410               && GET_CODE (XEXP (*split, 1)) == CONST_INT
2411               && INTVAL (XEXP (*split, 1)) > 0
2412               && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2413             {
2414               SUBST (*split, gen_rtx_ASHIFT (split_mode,
2415                                              XEXP (*split, 0), GEN_INT (i)));
2416               /* Update split_code because we may not have a multiply
2417                  anymore.  */
2418               split_code = GET_CODE (*split);
2419             }
2420
2421 #ifdef INSN_SCHEDULING
2422           /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2423              be written as a ZERO_EXTEND.  */
2424           if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
2425             {
2426 #ifdef LOAD_EXTEND_OP
2427               /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
2428                  what it really is.  */
2429               if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
2430                   == SIGN_EXTEND)
2431                 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
2432                                                     SUBREG_REG (*split)));
2433               else
2434 #endif
2435                 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
2436                                                     SUBREG_REG (*split)));
2437             }
2438 #endif
2439
2440           newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
2441           SUBST (*split, newdest);
2442           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2443
2444           /* recog_for_combine might have added CLOBBERs to newi2pat.
2445              Make sure NEWPAT does not depend on the clobbered regs.  */
2446           if (GET_CODE (newi2pat) == PARALLEL)
2447             for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
2448               if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
2449                 {
2450                   rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
2451                   if (reg_overlap_mentioned_p (reg, newpat))
2452                     {
2453                       undo_all ();
2454                       return 0;
2455                     }
2456                 }
2457
2458           /* If the split point was a MULT and we didn't have one before,
2459              don't use one now.  */
2460           if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2461             insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2462         }
2463     }
2464
2465   /* Check for a case where we loaded from memory in a narrow mode and
2466      then sign extended it, but we need both registers.  In that case,
2467      we have a PARALLEL with both loads from the same memory location.
2468      We can split this into a load from memory followed by a register-register
2469      copy.  This saves at least one insn, more if register allocation can
2470      eliminate the copy.
2471
2472      We cannot do this if the destination of the first assignment is a
2473      condition code register or cc0.  We eliminate this case by making sure
2474      the SET_DEST and SET_SRC have the same mode.
2475
2476      We cannot do this if the destination of the second assignment is
2477      a register that we have already assumed is zero-extended.  Similarly
2478      for a SUBREG of such a register.  */
2479
2480   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2481            && GET_CODE (newpat) == PARALLEL
2482            && XVECLEN (newpat, 0) == 2
2483            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2484            && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2485            && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
2486                == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
2487            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2488            && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2489                            XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2490            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2491                                    INSN_CUID (i2))
2492            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2493            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2494            && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2495                  (REG_P (temp)
2496                   && reg_stat[REGNO (temp)].nonzero_bits != 0
2497                   && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2498                   && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2499                   && (reg_stat[REGNO (temp)].nonzero_bits
2500                       != GET_MODE_MASK (word_mode))))
2501            && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2502                  && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2503                      (REG_P (temp)
2504                       && reg_stat[REGNO (temp)].nonzero_bits != 0
2505                       && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2506                       && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2507                       && (reg_stat[REGNO (temp)].nonzero_bits
2508                           != GET_MODE_MASK (word_mode)))))
2509            && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2510                                          SET_SRC (XVECEXP (newpat, 0, 1)))
2511            && ! find_reg_note (i3, REG_UNUSED,
2512                                SET_DEST (XVECEXP (newpat, 0, 0))))
2513     {
2514       rtx ni2dest;
2515
2516       newi2pat = XVECEXP (newpat, 0, 0);
2517       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2518       newpat = XVECEXP (newpat, 0, 1);
2519       SUBST (SET_SRC (newpat),
2520              gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
2521       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2522
2523       if (i2_code_number >= 0)
2524         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2525
2526       if (insn_code_number >= 0)
2527         swap_i2i3 = 1;
2528     }
2529
2530   /* Similarly, check for a case where we have a PARALLEL of two independent
2531      SETs but we started with three insns.  In this case, we can do the sets
2532      as two separate insns.  This case occurs when some SET allows two
2533      other insns to combine, but the destination of that SET is still live.  */
2534
2535   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2536            && GET_CODE (newpat) == PARALLEL
2537            && XVECLEN (newpat, 0) == 2
2538            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2539            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2540            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2541            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2542            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2543            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2544            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2545                                    INSN_CUID (i2))
2546            /* Don't pass sets with (USE (MEM ...)) dests to the following.  */
2547            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2548            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2549            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2550                                   XVECEXP (newpat, 0, 0))
2551            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2552                                   XVECEXP (newpat, 0, 1))
2553            && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
2554                  && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
2555     {
2556       /* Normally, it doesn't matter which of the two is done first,
2557          but it does if one references cc0.  In that case, it has to
2558          be first.  */
2559 #ifdef HAVE_cc0
2560       if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2561         {
2562           newi2pat = XVECEXP (newpat, 0, 0);
2563           newpat = XVECEXP (newpat, 0, 1);
2564         }
2565       else
2566 #endif
2567         {
2568           newi2pat = XVECEXP (newpat, 0, 1);
2569           newpat = XVECEXP (newpat, 0, 0);
2570         }
2571
2572       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2573
2574       if (i2_code_number >= 0)
2575         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2576     }
2577
2578   /* If it still isn't recognized, fail and change things back the way they
2579      were.  */
2580   if ((insn_code_number < 0
2581        /* Is the result a reasonable ASM_OPERANDS?  */
2582        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2583     {
2584       undo_all ();
2585       return 0;
2586     }
2587
2588   /* If we had to change another insn, make sure it is valid also.  */
2589   if (undobuf.other_insn)
2590     {
2591       rtx other_pat = PATTERN (undobuf.other_insn);
2592       rtx new_other_notes;
2593       rtx note, next;
2594
2595       CLEAR_HARD_REG_SET (newpat_used_regs);
2596
2597       other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
2598                                              &new_other_notes);
2599
2600       if (other_code_number < 0 && ! check_asm_operands (other_pat))
2601         {
2602           undo_all ();
2603           return 0;
2604         }
2605
2606       PATTERN (undobuf.other_insn) = other_pat;
2607
2608       /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2609          are still valid.  Then add any non-duplicate notes added by
2610          recog_for_combine.  */
2611       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2612         {
2613           next = XEXP (note, 1);
2614
2615           if (REG_NOTE_KIND (note) == REG_UNUSED
2616               && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2617             {
2618               if (REG_P (XEXP (note, 0)))
2619                 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2620
2621               remove_note (undobuf.other_insn, note);
2622             }
2623         }
2624
2625       for (note = new_other_notes; note; note = XEXP (note, 1))
2626         if (REG_P (XEXP (note, 0)))
2627           REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2628
2629       distribute_notes (new_other_notes, undobuf.other_insn,
2630                         undobuf.other_insn, NULL_RTX);
2631     }
2632 #ifdef HAVE_cc0
2633   /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
2634      they are adjacent to each other or not.  */
2635   {
2636     rtx p = prev_nonnote_insn (i3);
2637     if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
2638         && sets_cc0_p (newi2pat))
2639       {
2640         undo_all ();
2641         return 0;
2642       }
2643   }
2644 #endif
2645
2646   /* Only allow this combination if insn_rtx_costs reports that the
2647      replacement instructions are cheaper than the originals.  */
2648   if (!combine_validate_cost (i1, i2, i3, newpat, newi2pat))
2649     {
2650       undo_all ();
2651       return 0;
2652     }
2653
2654   /* We now know that we can do this combination.  Merge the insns and
2655      update the status of registers and LOG_LINKS.  */
2656
2657   if (swap_i2i3)
2658     {
2659       rtx insn;
2660       rtx link;
2661       rtx ni2dest;
2662
2663       /* I3 now uses what used to be its destination and which is now
2664          I2's destination.  This requires us to do a few adjustments.  */
2665       PATTERN (i3) = newpat;
2666       adjust_for_new_dest (i3);
2667
2668       /* We need a LOG_LINK from I3 to I2.  But we used to have one,
2669          so we still will.
2670
2671          However, some later insn might be using I2's dest and have
2672          a LOG_LINK pointing at I3.  We must remove this link.
2673          The simplest way to remove the link is to point it at I1,
2674          which we know will be a NOTE.  */
2675
2676       /* newi2pat is usually a SET here; however, recog_for_combine might
2677          have added some clobbers.  */
2678       if (GET_CODE (newi2pat) == PARALLEL)
2679         ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
2680       else
2681         ni2dest = SET_DEST (newi2pat);
2682
2683       for (insn = NEXT_INSN (i3);
2684            insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2685                     || insn != BB_HEAD (this_basic_block->next_bb));
2686            insn = NEXT_INSN (insn))
2687         {
2688           if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
2689             {
2690               for (link = LOG_LINKS (insn); link;
2691                    link = XEXP (link, 1))
2692                 if (XEXP (link, 0) == i3)
2693                   XEXP (link, 0) = i1;
2694
2695               break;
2696             }
2697         }
2698     }
2699
2700   {
2701     rtx i3notes, i2notes, i1notes = 0;
2702     rtx i3links, i2links, i1links = 0;
2703     rtx midnotes = 0;
2704     unsigned int regno;
2705
2706     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2707        clear them.  */
2708     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2709     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2710     if (i1)
2711       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2712
2713     /* Ensure that we do not have something that should not be shared but
2714        occurs multiple times in the new insns.  Check this by first
2715        resetting all the `used' flags and then copying anything is shared.  */
2716
2717     reset_used_flags (i3notes);
2718     reset_used_flags (i2notes);
2719     reset_used_flags (i1notes);
2720     reset_used_flags (newpat);
2721     reset_used_flags (newi2pat);
2722     if (undobuf.other_insn)
2723       reset_used_flags (PATTERN (undobuf.other_insn));
2724
2725     i3notes = copy_rtx_if_shared (i3notes);
2726     i2notes = copy_rtx_if_shared (i2notes);
2727     i1notes = copy_rtx_if_shared (i1notes);
2728     newpat = copy_rtx_if_shared (newpat);
2729     newi2pat = copy_rtx_if_shared (newi2pat);
2730     if (undobuf.other_insn)
2731       reset_used_flags (PATTERN (undobuf.other_insn));
2732
2733     INSN_CODE (i3) = insn_code_number;
2734     PATTERN (i3) = newpat;
2735
2736     if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
2737       {
2738         rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
2739
2740         reset_used_flags (call_usage);
2741         call_usage = copy_rtx (call_usage);
2742
2743         if (substed_i2)
2744           replace_rtx (call_usage, i2dest, i2src);
2745
2746         if (substed_i1)
2747           replace_rtx (call_usage, i1dest, i1src);
2748
2749         CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
2750       }
2751
2752     if (undobuf.other_insn)
2753       INSN_CODE (undobuf.other_insn) = other_code_number;
2754
2755     /* We had one special case above where I2 had more than one set and
2756        we replaced a destination of one of those sets with the destination
2757        of I3.  In that case, we have to update LOG_LINKS of insns later
2758        in this basic block.  Note that this (expensive) case is rare.
2759
2760        Also, in this case, we must pretend that all REG_NOTEs for I2
2761        actually came from I3, so that REG_UNUSED notes from I2 will be
2762        properly handled.  */
2763
2764     if (i3_subst_into_i2)
2765       {
2766         for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2767           if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != USE
2768               && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
2769               && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2770               && ! find_reg_note (i2, REG_UNUSED,
2771                                   SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2772             for (temp = NEXT_INSN (i2);
2773                  temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2774                           || BB_HEAD (this_basic_block) != temp);
2775                  temp = NEXT_INSN (temp))
2776               if (temp != i3 && INSN_P (temp))
2777                 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2778                   if (XEXP (link, 0) == i2)
2779                     XEXP (link, 0) = i3;
2780
2781         if (i3notes)
2782           {
2783             rtx link = i3notes;
2784             while (XEXP (link, 1))
2785               link = XEXP (link, 1);
2786             XEXP (link, 1) = i2notes;
2787           }
2788         else
2789           i3notes = i2notes;
2790         i2notes = 0;
2791       }
2792
2793     LOG_LINKS (i3) = 0;
2794     REG_NOTES (i3) = 0;
2795     LOG_LINKS (i2) = 0;
2796     REG_NOTES (i2) = 0;
2797
2798     if (newi2pat)
2799       {
2800         INSN_CODE (i2) = i2_code_number;
2801         PATTERN (i2) = newi2pat;
2802       }
2803     else
2804       SET_INSN_DELETED (i2);
2805
2806     if (i1)
2807       {
2808         LOG_LINKS (i1) = 0;
2809         REG_NOTES (i1) = 0;
2810         SET_INSN_DELETED (i1);
2811       }
2812
2813     /* Get death notes for everything that is now used in either I3 or
2814        I2 and used to die in a previous insn.  If we built two new
2815        patterns, move from I1 to I2 then I2 to I3 so that we get the
2816        proper movement on registers that I2 modifies.  */
2817
2818     if (newi2pat)
2819       {
2820         move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2821         move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2822       }
2823     else
2824       move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2825                    i3, &midnotes);
2826
2827     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
2828     if (i3notes)
2829       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX);
2830     if (i2notes)
2831       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX);
2832     if (i1notes)
2833       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX);
2834     if (midnotes)
2835       distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2836
2837     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
2838        know these are REG_UNUSED and want them to go to the desired insn,
2839        so we always pass it as i3.  We have not counted the notes in
2840        reg_n_deaths yet, so we need to do so now.  */
2841
2842     if (newi2pat && new_i2_notes)
2843       {
2844         for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2845           if (REG_P (XEXP (temp, 0)))
2846             REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2847
2848         distribute_notes (new_i2_notes, i2, i2, NULL_RTX);
2849       }
2850
2851     if (new_i3_notes)
2852       {
2853         for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2854           if (REG_P (XEXP (temp, 0)))
2855             REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2856
2857         distribute_notes (new_i3_notes, i3, i3, NULL_RTX);
2858       }
2859
2860     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
2861        put a REG_DEAD note for it somewhere.  If NEWI2PAT exists and sets
2862        I3DEST, the death must be somewhere before I2, not I3.  If we passed I3
2863        in that case, it might delete I2.  Similarly for I2 and I1.
2864        Show an additional death due to the REG_DEAD note we make here.  If
2865        we discard it in distribute_notes, we will decrement it again.  */
2866
2867     if (i3dest_killed)
2868       {
2869         if (REG_P (i3dest_killed))
2870           REG_N_DEATHS (REGNO (i3dest_killed))++;
2871
2872         if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
2873           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2874                                                NULL_RTX),
2875                             NULL_RTX, i2, NULL_RTX);
2876         else
2877           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2878                                                NULL_RTX),
2879                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2880       }
2881
2882     if (i2dest_in_i2src)
2883       {
2884         if (REG_P (i2dest))
2885           REG_N_DEATHS (REGNO (i2dest))++;
2886
2887         if (newi2pat && reg_set_p (i2dest, newi2pat))
2888           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2889                             NULL_RTX, i2, NULL_RTX);
2890         else
2891           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2892                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2893       }
2894
2895     if (i1dest_in_i1src)
2896       {
2897         if (REG_P (i1dest))
2898           REG_N_DEATHS (REGNO (i1dest))++;
2899
2900         if (newi2pat && reg_set_p (i1dest, newi2pat))
2901           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2902                             NULL_RTX, i2, NULL_RTX);
2903         else
2904           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2905                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2906       }
2907
2908     distribute_links (i3links);
2909     distribute_links (i2links);
2910     distribute_links (i1links);
2911
2912     if (REG_P (i2dest))
2913       {
2914         rtx link;
2915         rtx i2_insn = 0, i2_val = 0, set;
2916
2917         /* The insn that used to set this register doesn't exist, and
2918            this life of the register may not exist either.  See if one of
2919            I3's links points to an insn that sets I2DEST.  If it does,
2920            that is now the last known value for I2DEST. If we don't update
2921            this and I2 set the register to a value that depended on its old
2922            contents, we will get confused.  If this insn is used, thing
2923            will be set correctly in combine_instructions.  */
2924
2925         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2926           if ((set = single_set (XEXP (link, 0))) != 0
2927               && rtx_equal_p (i2dest, SET_DEST (set)))
2928             i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2929
2930         record_value_for_reg (i2dest, i2_insn, i2_val);
2931
2932         /* If the reg formerly set in I2 died only once and that was in I3,
2933            zero its use count so it won't make `reload' do any work.  */
2934         if (! added_sets_2
2935             && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2936             && ! i2dest_in_i2src)
2937           {
2938             regno = REGNO (i2dest);
2939             REG_N_SETS (regno)--;
2940           }
2941       }
2942
2943     if (i1 && REG_P (i1dest))
2944       {
2945         rtx link;
2946         rtx i1_insn = 0, i1_val = 0, set;
2947
2948         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2949           if ((set = single_set (XEXP (link, 0))) != 0
2950               && rtx_equal_p (i1dest, SET_DEST (set)))
2951             i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2952
2953         record_value_for_reg (i1dest, i1_insn, i1_val);
2954
2955         regno = REGNO (i1dest);
2956         if (! added_sets_1 && ! i1dest_in_i1src)
2957           REG_N_SETS (regno)--;
2958       }
2959
2960     /* Update reg_stat[].nonzero_bits et al for any changes that may have
2961        been made to this insn.  The order of
2962        set_nonzero_bits_and_sign_copies() is important.  Because newi2pat
2963        can affect nonzero_bits of newpat */
2964     if (newi2pat)
2965       note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
2966     note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
2967
2968     /* Set new_direct_jump_p if a new return or simple jump instruction
2969        has been created.
2970
2971        If I3 is now an unconditional jump, ensure that it has a
2972        BARRIER following it since it may have initially been a
2973        conditional jump.  It may also be the last nonnote insn.  */
2974
2975     if (returnjump_p (i3) || any_uncondjump_p (i3))
2976       {
2977         *new_direct_jump_p = 1;
2978         mark_jump_label (PATTERN (i3), i3, 0);
2979
2980         if ((temp = next_nonnote_insn (i3)) == NULL_RTX
2981             || !BARRIER_P (temp))
2982           emit_barrier_after (i3);
2983       }
2984
2985     if (undobuf.other_insn != NULL_RTX
2986         && (returnjump_p (undobuf.other_insn)
2987             || any_uncondjump_p (undobuf.other_insn)))
2988       {
2989         *new_direct_jump_p = 1;
2990
2991         if ((temp = next_nonnote_insn (undobuf.other_insn)) == NULL_RTX
2992             || !BARRIER_P (temp))
2993           emit_barrier_after (undobuf.other_insn);
2994       }
2995
2996     /* An NOOP jump does not need barrier, but it does need cleaning up
2997        of CFG.  */
2998     if (GET_CODE (newpat) == SET
2999         && SET_SRC (newpat) == pc_rtx
3000         && SET_DEST (newpat) == pc_rtx)
3001       *new_direct_jump_p = 1;
3002   }
3003
3004   combine_successes++;
3005   undo_commit ();
3006
3007   if (added_links_insn
3008       && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
3009       && INSN_CUID (added_links_insn) < INSN_CUID (i3))
3010     return added_links_insn;
3011   else
3012     return newi2pat ? i2 : i3;
3013 }
3014 \f
3015 /* Undo all the modifications recorded in undobuf.  */
3016
3017 static void
3018 undo_all (void)
3019 {
3020   struct undo *undo, *next;
3021
3022   for (undo = undobuf.undos; undo; undo = next)
3023     {
3024       next = undo->next;
3025       if (undo->is_int)
3026         *undo->where.i = undo->old_contents.i;
3027       else
3028         *undo->where.r = undo->old_contents.r;
3029
3030       undo->next = undobuf.frees;
3031       undobuf.frees = undo;
3032     }
3033
3034   undobuf.undos = 0;
3035 }
3036
3037 /* We've committed to accepting the changes we made.  Move all
3038    of the undos to the free list.  */
3039
3040 static void
3041 undo_commit (void)
3042 {
3043   struct undo *undo, *next;
3044
3045   for (undo = undobuf.undos; undo; undo = next)
3046     {
3047       next = undo->next;
3048       undo->next = undobuf.frees;
3049       undobuf.frees = undo;
3050     }
3051   undobuf.undos = 0;
3052 }
3053
3054 \f
3055 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
3056    where we have an arithmetic expression and return that point.  LOC will
3057    be inside INSN.
3058
3059    try_combine will call this function to see if an insn can be split into
3060    two insns.  */
3061
3062 static rtx *
3063 find_split_point (rtx *loc, rtx insn)
3064 {
3065   rtx x = *loc;
3066   enum rtx_code code = GET_CODE (x);
3067   rtx *split;
3068   unsigned HOST_WIDE_INT len = 0;
3069   HOST_WIDE_INT pos = 0;
3070   int unsignedp = 0;
3071   rtx inner = NULL_RTX;
3072
3073   /* First special-case some codes.  */
3074   switch (code)
3075     {
3076     case SUBREG:
3077 #ifdef INSN_SCHEDULING
3078       /* If we are making a paradoxical SUBREG invalid, it becomes a split
3079          point.  */
3080       if (MEM_P (SUBREG_REG (x)))
3081         return loc;
3082 #endif
3083       return find_split_point (&SUBREG_REG (x), insn);
3084
3085     case MEM:
3086 #ifdef HAVE_lo_sum
3087       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
3088          using LO_SUM and HIGH.  */
3089       if (GET_CODE (XEXP (x, 0)) == CONST
3090           || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
3091         {
3092           SUBST (XEXP (x, 0),
3093                  gen_rtx_LO_SUM (Pmode,
3094                                  gen_rtx_HIGH (Pmode, XEXP (x, 0)),
3095                                  XEXP (x, 0)));
3096           return &XEXP (XEXP (x, 0), 0);
3097         }
3098 #endif
3099
3100       /* If we have a PLUS whose second operand is a constant and the
3101          address is not valid, perhaps will can split it up using
3102          the machine-specific way to split large constants.  We use
3103          the first pseudo-reg (one of the virtual regs) as a placeholder;
3104          it will not remain in the result.  */
3105       if (GET_CODE (XEXP (x, 0)) == PLUS
3106           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3107           && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
3108         {
3109           rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
3110           rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
3111                                  subst_insn);
3112
3113           /* This should have produced two insns, each of which sets our
3114              placeholder.  If the source of the second is a valid address,
3115              we can make put both sources together and make a split point
3116              in the middle.  */
3117
3118           if (seq
3119               && NEXT_INSN (seq) != NULL_RTX
3120               && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
3121               && NONJUMP_INSN_P (seq)
3122               && GET_CODE (PATTERN (seq)) == SET
3123               && SET_DEST (PATTERN (seq)) == reg
3124               && ! reg_mentioned_p (reg,
3125                                     SET_SRC (PATTERN (seq)))
3126               && NONJUMP_INSN_P (NEXT_INSN (seq))
3127               && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
3128               && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
3129               && memory_address_p (GET_MODE (x),
3130                                    SET_SRC (PATTERN (NEXT_INSN (seq)))))
3131             {
3132               rtx src1 = SET_SRC (PATTERN (seq));
3133               rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
3134
3135               /* Replace the placeholder in SRC2 with SRC1.  If we can
3136                  find where in SRC2 it was placed, that can become our
3137                  split point and we can replace this address with SRC2.
3138                  Just try two obvious places.  */
3139
3140               src2 = replace_rtx (src2, reg, src1);
3141               split = 0;
3142               if (XEXP (src2, 0) == src1)
3143                 split = &XEXP (src2, 0);
3144               else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
3145                        && XEXP (XEXP (src2, 0), 0) == src1)
3146                 split = &XEXP (XEXP (src2, 0), 0);
3147
3148               if (split)
3149                 {
3150                   SUBST (XEXP (x, 0), src2);
3151                   return split;
3152                 }
3153             }
3154
3155           /* If that didn't work, perhaps the first operand is complex and
3156              needs to be computed separately, so make a split point there.
3157              This will occur on machines that just support REG + CONST
3158              and have a constant moved through some previous computation.  */
3159
3160           else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
3161                    && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
3162                          && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
3163             return &XEXP (XEXP (x, 0), 0);
3164         }
3165       break;
3166
3167     case SET:
3168 #ifdef HAVE_cc0
3169       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
3170          ZERO_EXTRACT, the most likely reason why this doesn't match is that
3171          we need to put the operand into a register.  So split at that
3172          point.  */
3173
3174       if (SET_DEST (x) == cc0_rtx
3175           && GET_CODE (SET_SRC (x)) != COMPARE
3176           && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
3177           && !OBJECT_P (SET_SRC (x))
3178           && ! (GET_CODE (SET_SRC (x)) == SUBREG
3179                 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
3180         return &SET_SRC (x);
3181 #endif
3182
3183       /* See if we can split SET_SRC as it stands.  */
3184       split = find_split_point (&SET_SRC (x), insn);
3185       if (split && split != &SET_SRC (x))
3186         return split;
3187
3188       /* See if we can split SET_DEST as it stands.  */
3189       split = find_split_point (&SET_DEST (x), insn);
3190       if (split && split != &SET_DEST (x))
3191         return split;
3192
3193       /* See if this is a bitfield assignment with everything constant.  If
3194          so, this is an IOR of an AND, so split it into that.  */
3195       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
3196           && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
3197               <= HOST_BITS_PER_WIDE_INT)
3198           && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
3199           && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
3200           && GET_CODE (SET_SRC (x)) == CONST_INT
3201           && ((INTVAL (XEXP (SET_DEST (x), 1))
3202                + INTVAL (XEXP (SET_DEST (x), 2)))
3203               <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
3204           && ! side_effects_p (XEXP (SET_DEST (x), 0)))
3205         {
3206           HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
3207           unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
3208           unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
3209           rtx dest = XEXP (SET_DEST (x), 0);
3210           enum machine_mode mode = GET_MODE (dest);
3211           unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
3212
3213           if (BITS_BIG_ENDIAN)
3214             pos = GET_MODE_BITSIZE (mode) - len - pos;
3215
3216           if (src == mask)
3217             SUBST (SET_SRC (x),
3218                    simplify_gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
3219           else
3220             {
3221               rtx negmask = gen_int_mode (~(mask << pos), mode);
3222               SUBST (SET_SRC (x),
3223                      simplify_gen_binary (IOR, mode,
3224                                           simplify_gen_binary (AND, mode,
3225                                                                dest, negmask),
3226                                           GEN_INT (src << pos)));
3227             }
3228
3229           SUBST (SET_DEST (x), dest);
3230
3231           split = find_split_point (&SET_SRC (x), insn);
3232           if (split && split != &SET_SRC (x))
3233             return split;
3234         }
3235
3236       /* Otherwise, see if this is an operation that we can split into two.
3237          If so, try to split that.  */
3238       code = GET_CODE (SET_SRC (x));
3239
3240       switch (code)
3241         {
3242         case AND:
3243           /* If we are AND'ing with a large constant that is only a single
3244              bit and the result is only being used in a context where we
3245              need to know if it is zero or nonzero, replace it with a bit
3246              extraction.  This will avoid the large constant, which might
3247              have taken more than one insn to make.  If the constant were
3248              not a valid argument to the AND but took only one insn to make,
3249              this is no worse, but if it took more than one insn, it will
3250              be better.  */
3251
3252           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3253               && REG_P (XEXP (SET_SRC (x), 0))
3254               && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
3255               && REG_P (SET_DEST (x))
3256               && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
3257               && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
3258               && XEXP (*split, 0) == SET_DEST (x)
3259               && XEXP (*split, 1) == const0_rtx)
3260             {
3261               rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
3262                                                 XEXP (SET_SRC (x), 0),
3263                                                 pos, NULL_RTX, 1, 1, 0, 0);
3264               if (extraction != 0)
3265                 {
3266                   SUBST (SET_SRC (x), extraction);
3267                   return find_split_point (loc, insn);
3268                 }
3269             }
3270           break;
3271
3272         case NE:
3273           /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
3274              is known to be on, this can be converted into a NEG of a shift.  */
3275           if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
3276               && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
3277               && 1 <= (pos = exact_log2
3278                        (nonzero_bits (XEXP (SET_SRC (x), 0),
3279                                       GET_MODE (XEXP (SET_SRC (x), 0))))))
3280             {
3281               enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
3282
3283               SUBST (SET_SRC (x),
3284                      gen_rtx_NEG (mode,
3285                                   gen_rtx_LSHIFTRT (mode,
3286                                                     XEXP (SET_SRC (x), 0),
3287                                                     GEN_INT (pos))));
3288
3289               split = find_split_point (&SET_SRC (x), insn);
3290               if (split && split != &SET_SRC (x))
3291                 return split;
3292             }
3293           break;
3294
3295         case SIGN_EXTEND:
3296           inner = XEXP (SET_SRC (x), 0);
3297
3298           /* We can't optimize if either mode is a partial integer
3299              mode as we don't know how many bits are significant
3300              in those modes.  */
3301           if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
3302               || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
3303             break;
3304
3305           pos = 0;
3306           len = GET_MODE_BITSIZE (GET_MODE (inner));
3307           unsignedp = 0;
3308           break;
3309
3310         case SIGN_EXTRACT:
3311         case ZERO_EXTRACT:
3312           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3313               && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
3314             {
3315               inner = XEXP (SET_SRC (x), 0);
3316               len = INTVAL (XEXP (SET_SRC (x), 1));
3317               pos = INTVAL (XEXP (SET_SRC (x), 2));
3318
3319               if (BITS_BIG_ENDIAN)
3320                 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
3321               unsignedp = (code == ZERO_EXTRACT);
3322             }
3323           break;
3324
3325         default:
3326           break;
3327         }
3328
3329       if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
3330         {
3331           enum machine_mode mode = GET_MODE (SET_SRC (x));
3332
3333           /* For unsigned, we have a choice of a shift followed by an
3334              AND or two shifts.  Use two shifts for field sizes where the
3335              constant might be too large.  We assume here that we can
3336              always at least get 8-bit constants in an AND insn, which is
3337              true for every current RISC.  */
3338
3339           if (unsignedp && len <= 8)
3340             {
3341               SUBST (SET_SRC (x),
3342                      gen_rtx_AND (mode,
3343                                   gen_rtx_LSHIFTRT
3344                                   (mode, gen_lowpart (mode, inner),
3345                                    GEN_INT (pos)),
3346                                   GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
3347
3348               split = find_split_point (&SET_SRC (x), insn);
3349               if (split && split != &SET_SRC (x))
3350                 return split;
3351             }
3352           else
3353             {
3354               SUBST (SET_SRC (x),
3355                      gen_rtx_fmt_ee
3356                      (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
3357                       gen_rtx_ASHIFT (mode,
3358                                       gen_lowpart (mode, inner),
3359                                       GEN_INT (GET_MODE_BITSIZE (mode)
3360                                                - len - pos)),
3361                       GEN_INT (GET_MODE_BITSIZE (mode) - len)));
3362
3363               split = find_split_point (&SET_SRC (x), insn);
3364               if (split && split != &SET_SRC (x))
3365                 return split;
3366             }
3367         }
3368
3369       /* See if this is a simple operation with a constant as the second
3370          operand.  It might be that this constant is out of range and hence
3371          could be used as a split point.  */
3372       if (BINARY_P (SET_SRC (x))
3373           && CONSTANT_P (XEXP (SET_SRC (x), 1))
3374           && (OBJECT_P (XEXP (SET_SRC (x), 0))
3375               || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
3376                   && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
3377         return &XEXP (SET_SRC (x), 1);
3378
3379       /* Finally, see if this is a simple operation with its first operand
3380          not in a register.  The operation might require this operand in a
3381          register, so return it as a split point.  We can always do this
3382          because if the first operand were another operation, we would have
3383          already found it as a split point.  */
3384       if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
3385           && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
3386         return &XEXP (SET_SRC (x), 0);
3387
3388       return 0;
3389
3390     case AND:
3391     case IOR:
3392       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
3393          it is better to write this as (not (ior A B)) so we can split it.
3394          Similarly for IOR.  */
3395       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
3396         {
3397           SUBST (*loc,
3398                  gen_rtx_NOT (GET_MODE (x),
3399                               gen_rtx_fmt_ee (code == IOR ? AND : IOR,
3400                                               GET_MODE (x),
3401                                               XEXP (XEXP (x, 0), 0),
3402                                               XEXP (XEXP (x, 1), 0))));
3403           return find_split_point (loc, insn);
3404         }
3405
3406       /* Many RISC machines have a large set of logical insns.  If the
3407          second operand is a NOT, put it first so we will try to split the
3408          other operand first.  */
3409       if (GET_CODE (XEXP (x, 1)) == NOT)
3410         {
3411           rtx tem = XEXP (x, 0);
3412           SUBST (XEXP (x, 0), XEXP (x, 1));
3413           SUBST (XEXP (x, 1), tem);
3414         }
3415       break;
3416
3417     default:
3418       break;
3419     }
3420
3421   /* Otherwise, select our actions depending on our rtx class.  */
3422   switch (GET_RTX_CLASS (code))
3423     {
3424     case RTX_BITFIELD_OPS:              /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
3425     case RTX_TERNARY:
3426       split = find_split_point (&XEXP (x, 2), insn);
3427       if (split)
3428         return split;
3429       /* ... fall through ...  */
3430     case RTX_BIN_ARITH:
3431     case RTX_COMM_ARITH:
3432     case RTX_COMPARE:
3433     case RTX_COMM_COMPARE:
3434       split = find_split_point (&XEXP (x, 1), insn);
3435       if (split)
3436         return split;
3437       /* ... fall through ...  */
3438     case RTX_UNARY:
3439       /* Some machines have (and (shift ...) ...) insns.  If X is not
3440          an AND, but XEXP (X, 0) is, use it as our split point.  */
3441       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
3442         return &XEXP (x, 0);
3443
3444       split = find_split_point (&XEXP (x, 0), insn);
3445       if (split)
3446         return split;
3447       return loc;
3448
3449     default:
3450       /* Otherwise, we don't have a split point.  */
3451       return 0;
3452     }
3453 }
3454 \f
3455 /* Throughout X, replace FROM with TO, and return the result.
3456    The result is TO if X is FROM;
3457    otherwise the result is X, but its contents may have been modified.
3458    If they were modified, a record was made in undobuf so that
3459    undo_all will (among other things) return X to its original state.
3460
3461    If the number of changes necessary is too much to record to undo,
3462    the excess changes are not made, so the result is invalid.
3463    The changes already made can still be undone.
3464    undobuf.num_undo is incremented for such changes, so by testing that
3465    the caller can tell whether the result is valid.
3466
3467    `n_occurrences' is incremented each time FROM is replaced.
3468
3469    IN_DEST is nonzero if we are processing the SET_DEST of a SET.
3470
3471    UNIQUE_COPY is nonzero if each substitution must be unique.  We do this
3472    by copying if `n_occurrences' is nonzero.  */
3473
3474 static rtx
3475 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
3476 {
3477   enum rtx_code code = GET_CODE (x);
3478   enum machine_mode op0_mode = VOIDmode;
3479   const char *fmt;
3480   int len, i;
3481   rtx new;
3482
3483 /* Two expressions are equal if they are identical copies of a shared
3484    RTX or if they are both registers with the same register number
3485    and mode.  */
3486
3487 #define COMBINE_RTX_EQUAL_P(X,Y)                        \
3488   ((X) == (Y)                                           \
3489    || (REG_P (X) && REG_P (Y)   \
3490        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3491
3492   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3493     {
3494       n_occurrences++;
3495       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3496     }
3497
3498   /* If X and FROM are the same register but different modes, they will
3499      not have been seen as equal above.  However, flow.c will make a
3500      LOG_LINKS entry for that case.  If we do nothing, we will try to
3501      rerecognize our original insn and, when it succeeds, we will
3502      delete the feeding insn, which is incorrect.
3503
3504      So force this insn not to match in this (rare) case.  */
3505   if (! in_dest && code == REG && REG_P (from)
3506       && REGNO (x) == REGNO (from))
3507     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3508
3509   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3510      of which may contain things that can be combined.  */
3511   if (code != MEM && code != LO_SUM && OBJECT_P (x))
3512     return x;
3513
3514   /* It is possible to have a subexpression appear twice in the insn.
3515      Suppose that FROM is a register that appears within TO.
3516      Then, after that subexpression has been scanned once by `subst',
3517      the second time it is scanned, TO may be found.  If we were
3518      to scan TO here, we would find FROM within it and create a
3519      self-referent rtl structure which is completely wrong.  */
3520   if (COMBINE_RTX_EQUAL_P (x, to))
3521     return to;
3522
3523   /* Parallel asm_operands need special attention because all of the
3524      inputs are shared across the arms.  Furthermore, unsharing the
3525      rtl results in recognition failures.  Failure to handle this case
3526      specially can result in circular rtl.
3527
3528      Solve this by doing a normal pass across the first entry of the
3529      parallel, and only processing the SET_DESTs of the subsequent
3530      entries.  Ug.  */
3531
3532   if (code == PARALLEL
3533       && GET_CODE (XVECEXP (x, 0, 0)) == SET
3534       && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
3535     {
3536       new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
3537
3538       /* If this substitution failed, this whole thing fails.  */
3539       if (GET_CODE (new) == CLOBBER
3540           && XEXP (new, 0) == const0_rtx)
3541         return new;
3542
3543       SUBST (XVECEXP (x, 0, 0), new);
3544
3545       for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
3546         {
3547           rtx dest = SET_DEST (XVECEXP (x, 0, i));
3548
3549           if (!REG_P (dest)
3550               && GET_CODE (dest) != CC0
3551               && GET_CODE (dest) != PC)
3552             {
3553               new = subst (dest, from, to, 0, unique_copy);
3554
3555               /* If this substitution failed, this whole thing fails.  */
3556               if (GET_CODE (new) == CLOBBER
3557                   && XEXP (new, 0) == const0_rtx)
3558                 return new;
3559
3560               SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
3561             }
3562         }
3563     }
3564   else
3565     {
3566       len = GET_RTX_LENGTH (code);
3567       fmt = GET_RTX_FORMAT (code);
3568
3569       /* We don't need to process a SET_DEST that is a register, CC0,
3570          or PC, so set up to skip this common case.  All other cases
3571          where we want to suppress replacing something inside a
3572          SET_SRC are handled via the IN_DEST operand.  */
3573       if (code == SET
3574           && (REG_P (SET_DEST (x))
3575               || GET_CODE (SET_DEST (x)) == CC0
3576               || GET_CODE (SET_DEST (x)) == PC))
3577         fmt = "ie";
3578
3579       /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3580          constant.  */
3581       if (fmt[0] == 'e')
3582         op0_mode = GET_MODE (XEXP (x, 0));
3583
3584       for (i = 0; i < len; i++)
3585         {
3586           if (fmt[i] == 'E')
3587             {
3588               int j;
3589               for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3590                 {
3591                   if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3592                     {
3593                       new = (unique_copy && n_occurrences
3594                              ? copy_rtx (to) : to);
3595                       n_occurrences++;
3596                     }
3597                   else
3598                     {
3599                       new = subst (XVECEXP (x, i, j), from, to, 0,
3600                                    unique_copy);
3601
3602                       /* If this substitution failed, this whole thing
3603                          fails.  */
3604                       if (GET_CODE (new) == CLOBBER
3605                           && XEXP (new, 0) == const0_rtx)
3606                         return new;
3607                     }
3608
3609                   SUBST (XVECEXP (x, i, j), new);
3610                 }
3611             }
3612           else if (fmt[i] == 'e')
3613             {
3614               /* If this is a register being set, ignore it.  */
3615               new = XEXP (x, i);
3616               if (in_dest
3617                   && i == 0
3618                   && (((code == SUBREG || code == ZERO_EXTRACT)
3619                        && REG_P (new))
3620                       || code == STRICT_LOW_PART))
3621                 ;
3622
3623               else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3624                 {
3625                   /* In general, don't install a subreg involving two
3626                      modes not tieable.  It can worsen register
3627                      allocation, and can even make invalid reload
3628                      insns, since the reg inside may need to be copied
3629                      from in the outside mode, and that may be invalid
3630                      if it is an fp reg copied in integer mode.
3631
3632                      We allow two exceptions to this: It is valid if
3633                      it is inside another SUBREG and the mode of that
3634                      SUBREG and the mode of the inside of TO is
3635                      tieable and it is valid if X is a SET that copies
3636                      FROM to CC0.  */
3637
3638                   if (GET_CODE (to) == SUBREG
3639                       && ! MODES_TIEABLE_P (GET_MODE (to),
3640                                             GET_MODE (SUBREG_REG (to)))
3641                       && ! (code == SUBREG
3642                             && MODES_TIEABLE_P (GET_MODE (x),
3643                                                 GET_MODE (SUBREG_REG (to))))
3644 #ifdef HAVE_cc0
3645                       && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3646 #endif
3647                       )
3648                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3649
3650 #ifdef CANNOT_CHANGE_MODE_CLASS
3651                   if (code == SUBREG
3652                       && REG_P (to)
3653                       && REGNO (to) < FIRST_PSEUDO_REGISTER
3654                       && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
3655                                                    GET_MODE (to),
3656                                                    GET_MODE (x)))
3657                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3658 #endif
3659
3660                   new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3661                   n_occurrences++;
3662                 }
3663               else
3664                 /* If we are in a SET_DEST, suppress most cases unless we
3665                    have gone inside a MEM, in which case we want to
3666                    simplify the address.  We assume here that things that
3667                    are actually part of the destination have their inner
3668                    parts in the first expression.  This is true for SUBREG,
3669                    STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3670                    things aside from REG and MEM that should appear in a
3671                    SET_DEST.  */
3672                 new = subst (XEXP (x, i), from, to,
3673                              (((in_dest
3674                                 && (code == SUBREG || code == STRICT_LOW_PART
3675                                     || code == ZERO_EXTRACT))
3676                                || code == SET)
3677                               && i == 0), unique_copy);
3678
3679               /* If we found that we will have to reject this combination,
3680                  indicate that by returning the CLOBBER ourselves, rather than
3681                  an expression containing it.  This will speed things up as
3682                  well as prevent accidents where two CLOBBERs are considered
3683                  to be equal, thus producing an incorrect simplification.  */
3684
3685               if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3686                 return new;
3687
3688               if (GET_CODE (x) == SUBREG
3689                   && (GET_CODE (new) == CONST_INT
3690                       || GET_CODE (new) == CONST_DOUBLE))
3691                 {
3692                   enum machine_mode mode = GET_MODE (x);
3693
3694                   x = simplify_subreg (GET_MODE (x), new,
3695                                        GET_MODE (SUBREG_REG (x)),
3696                                        SUBREG_BYTE (x));
3697                   if (! x)
3698                     x = gen_rtx_CLOBBER (mode, const0_rtx);
3699                 }
3700               else if (GET_CODE (new) == CONST_INT
3701                        && GET_CODE (x) == ZERO_EXTEND)
3702                 {
3703                   x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
3704                                                 new, GET_MODE (XEXP (x, 0)));
3705                   gcc_assert (x);
3706                 }
3707               else
3708                 SUBST (XEXP (x, i), new);
3709             }
3710         }
3711     }
3712
3713   /* Try to simplify X.  If the simplification changed the code, it is likely
3714      that further simplification will help, so loop, but limit the number
3715      of repetitions that will be performed.  */
3716
3717   for (i = 0; i < 4; i++)
3718     {
3719       /* If X is sufficiently simple, don't bother trying to do anything
3720          with it.  */
3721       if (code != CONST_INT && code != REG && code != CLOBBER)
3722         x = combine_simplify_rtx (x, op0_mode, in_dest);
3723
3724       if (GET_CODE (x) == code)
3725         break;
3726
3727       code = GET_CODE (x);
3728
3729       /* We no longer know the original mode of operand 0 since we
3730          have changed the form of X)  */
3731       op0_mode = VOIDmode;
3732     }
3733
3734   return x;
3735 }
3736 \f
3737 /* Simplify X, a piece of RTL.  We just operate on the expression at the
3738    outer level; call `subst' to simplify recursively.  Return the new
3739    expression.
3740
3741    OP0_MODE is the original mode of XEXP (x, 0).  IN_DEST is nonzero
3742    if we are inside a SET_DEST.  */
3743
3744 static rtx
3745 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
3746 {
3747   enum rtx_code code = GET_CODE (x);
3748   enum machine_mode mode = GET_MODE (x);
3749   rtx temp;
3750   rtx reversed;
3751   int i;
3752
3753   /* If this is a commutative operation, put a constant last and a complex
3754      expression first.  We don't need to do this for comparisons here.  */
3755   if (COMMUTATIVE_ARITH_P (x)
3756       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
3757     {
3758       temp = XEXP (x, 0);
3759       SUBST (XEXP (x, 0), XEXP (x, 1));
3760       SUBST (XEXP (x, 1), temp);
3761     }
3762
3763   /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3764      sign extension of a PLUS with a constant, reverse the order of the sign
3765      extension and the addition. Note that this not the same as the original
3766      code, but overflow is undefined for signed values.  Also note that the
3767      PLUS will have been partially moved "inside" the sign-extension, so that
3768      the first operand of X will really look like:
3769          (ashiftrt (plus (ashift A C4) C5) C4).
3770      We convert this to
3771          (plus (ashiftrt (ashift A C4) C2) C4)
3772      and replace the first operand of X with that expression.  Later parts
3773      of this function may simplify the expression further.
3774
3775      For example, if we start with (mult (sign_extend (plus A C1)) C2),
3776      we swap the SIGN_EXTEND and PLUS.  Later code will apply the
3777      distributive law to produce (plus (mult (sign_extend X) C1) C3).
3778
3779      We do this to simplify address expressions.  */
3780
3781   if ((code == PLUS || code == MINUS || code == MULT)
3782       && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3783       && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3784       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3785       && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3786       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3787       && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3788       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3789       && (temp = simplify_binary_operation (ASHIFTRT, mode,
3790                                             XEXP (XEXP (XEXP (x, 0), 0), 1),
3791                                             XEXP (XEXP (x, 0), 1))) != 0)
3792     {
3793       rtx new
3794         = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3795                                 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3796                                 INTVAL (XEXP (XEXP (x, 0), 1)));
3797
3798       new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3799                                   INTVAL (XEXP (XEXP (x, 0), 1)));
3800
3801       SUBST (XEXP (x, 0), simplify_gen_binary (PLUS, mode, new, temp));
3802     }
3803
3804   /* If this is a simple operation applied to an IF_THEN_ELSE, try
3805      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
3806      things.  Check for cases where both arms are testing the same
3807      condition.
3808
3809      Don't do anything if all operands are very simple.  */
3810
3811   if ((BINARY_P (x)
3812        && ((!OBJECT_P (XEXP (x, 0))
3813             && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3814                   && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
3815            || (!OBJECT_P (XEXP (x, 1))
3816                && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3817                      && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
3818       || (UNARY_P (x)
3819           && (!OBJECT_P (XEXP (x, 0))
3820                && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3821                      && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
3822     {
3823       rtx cond, true_rtx, false_rtx;
3824
3825       cond = if_then_else_cond (x, &true_rtx, &false_rtx);
3826       if (cond != 0
3827           /* If everything is a comparison, what we have is highly unlikely
3828              to be simpler, so don't use it.  */
3829           && ! (COMPARISON_P (x)
3830                 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
3831         {
3832           rtx cop1 = const0_rtx;
3833           enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3834
3835           if (cond_code == NE && COMPARISON_P (cond))
3836             return x;
3837
3838           /* Simplify the alternative arms; this may collapse the true and
3839              false arms to store-flag values.  Be careful to use copy_rtx
3840              here since true_rtx or false_rtx might share RTL with x as a
3841              result of the if_then_else_cond call above.  */
3842           true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
3843           false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
3844
3845           /* If true_rtx and false_rtx are not general_operands, an if_then_else
3846              is unlikely to be simpler.  */
3847           if (general_operand (true_rtx, VOIDmode)
3848               && general_operand (false_rtx, VOIDmode))
3849             {
3850               enum rtx_code reversed;
3851
3852               /* Restarting if we generate a store-flag expression will cause
3853                  us to loop.  Just drop through in this case.  */
3854
3855               /* If the result values are STORE_FLAG_VALUE and zero, we can
3856                  just make the comparison operation.  */
3857               if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
3858                 x = simplify_gen_relational (cond_code, mode, VOIDmode,
3859                                              cond, cop1);
3860               else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
3861                        && ((reversed = reversed_comparison_code_parts
3862                                         (cond_code, cond, cop1, NULL))
3863                            != UNKNOWN))
3864                 x = simplify_gen_relational (reversed, mode, VOIDmode,
3865                                              cond, cop1);
3866
3867               /* Likewise, we can make the negate of a comparison operation
3868                  if the result values are - STORE_FLAG_VALUE and zero.  */
3869               else if (GET_CODE (true_rtx) == CONST_INT
3870                        && INTVAL (true_rtx) == - STORE_FLAG_VALUE
3871                        && false_rtx == const0_rtx)
3872                 x = simplify_gen_unary (NEG, mode,
3873                                         simplify_gen_relational (cond_code,
3874                                                                  mode, VOIDmode,
3875                                                                  cond, cop1),
3876                                         mode);
3877               else if (GET_CODE (false_rtx) == CONST_INT
3878                        && INTVAL (false_rtx) == - STORE_FLAG_VALUE
3879                        && true_rtx == const0_rtx
3880                        && ((reversed = reversed_comparison_code_parts
3881                                         (cond_code, cond, cop1, NULL))
3882                            != UNKNOWN))
3883                 x = simplify_gen_unary (NEG, mode,
3884                                         simplify_gen_relational (reversed,
3885                                                                  mode, VOIDmode,
3886                                                                  cond, cop1),
3887                                         mode);
3888               else
3889                 return gen_rtx_IF_THEN_ELSE (mode,
3890                                              simplify_gen_relational (cond_code,
3891                                                                       mode,
3892                                                                       VOIDmode,
3893                                                                       cond,
3894                                                                       cop1),
3895                                              true_rtx, false_rtx);
3896
3897               code = GET_CODE (x);
3898               op0_mode = VOIDmode;
3899             }
3900         }
3901     }
3902
3903   /* Try to fold this expression in case we have constants that weren't
3904      present before.  */
3905   temp = 0;
3906   switch (GET_RTX_CLASS (code))
3907     {
3908     case RTX_UNARY:
3909       if (op0_mode == VOIDmode)
3910         op0_mode = GET_MODE (XEXP (x, 0));
3911       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3912       break;
3913     case RTX_COMPARE:
3914     case RTX_COMM_COMPARE:
3915       {
3916         enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
3917         if (cmp_mode == VOIDmode)
3918           {
3919             cmp_mode = GET_MODE (XEXP (x, 1));
3920             if (cmp_mode == VOIDmode)
3921               cmp_mode = op0_mode;
3922           }
3923         temp = simplify_relational_operation (code, mode, cmp_mode,
3924                                               XEXP (x, 0), XEXP (x, 1));
3925       }
3926       break;
3927     case RTX_COMM_ARITH:
3928     case RTX_BIN_ARITH:
3929       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3930       break;
3931     case RTX_BITFIELD_OPS:
3932     case RTX_TERNARY:
3933       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3934                                          XEXP (x, 1), XEXP (x, 2));
3935       break;
3936     default:
3937       break;
3938     }
3939
3940   if (temp)
3941     {
3942       x = temp;
3943       code = GET_CODE (temp);
3944       op0_mode = VOIDmode;
3945       mode = GET_MODE (temp);
3946     }
3947
3948   /* First see if we can apply the inverse distributive law.  */
3949   if (code == PLUS || code == MINUS
3950       || code == AND || code == IOR || code == XOR)
3951     {
3952       x = apply_distributive_law (x);
3953       code = GET_CODE (x);
3954       op0_mode = VOIDmode;
3955     }
3956
3957   /* If CODE is an associative operation not otherwise handled, see if we
3958      can associate some operands.  This can win if they are constants or
3959      if they are logically related (i.e. (a & b) & a).  */
3960   if ((code == PLUS || code == MINUS || code == MULT || code == DIV
3961        || code == AND || code == IOR || code == XOR
3962        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3963       && ((INTEGRAL_MODE_P (mode) && code != DIV)
3964           || (flag_unsafe_math_optimizations && FLOAT_MODE_P (mode))))
3965     {
3966       if (GET_CODE (XEXP (x, 0)) == code)
3967         {
3968           rtx other = XEXP (XEXP (x, 0), 0);
3969           rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3970           rtx inner_op1 = XEXP (x, 1);
3971           rtx inner;
3972
3973           /* Make sure we pass the constant operand if any as the second
3974              one if this is a commutative operation.  */
3975           if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
3976             {
3977               rtx tem = inner_op0;
3978               inner_op0 = inner_op1;
3979               inner_op1 = tem;
3980             }
3981           inner = simplify_binary_operation (code == MINUS ? PLUS
3982                                              : code == DIV ? MULT
3983                                              : code,
3984                                              mode, inner_op0, inner_op1);
3985
3986           /* For commutative operations, try the other pair if that one
3987              didn't simplify.  */
3988           if (inner == 0 && COMMUTATIVE_ARITH_P (x))
3989             {
3990               other = XEXP (XEXP (x, 0), 1);
3991               inner = simplify_binary_operation (code, mode,
3992                                                  XEXP (XEXP (x, 0), 0),
3993                                                  XEXP (x, 1));
3994             }
3995
3996           if (inner)
3997             return simplify_gen_binary (code, mode, other, inner);
3998         }
3999     }
4000
4001   /* A little bit of algebraic simplification here.  */
4002   switch (code)
4003     {
4004     case MEM:
4005       /* Ensure that our address has any ASHIFTs converted to MULT in case
4006          address-recognizing predicates are called later.  */
4007       temp = make_compound_operation (XEXP (x, 0), MEM);
4008       SUBST (XEXP (x, 0), temp);
4009       break;
4010
4011     case SUBREG:
4012       if (op0_mode == VOIDmode)
4013         op0_mode = GET_MODE (SUBREG_REG (x));
4014
4015       /* See if this can be moved to simplify_subreg.  */
4016       if (CONSTANT_P (SUBREG_REG (x))
4017           && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
4018              /* Don't call gen_lowpart if the inner mode
4019                 is VOIDmode and we cannot simplify it, as SUBREG without
4020                 inner mode is invalid.  */
4021           && (GET_MODE (SUBREG_REG (x)) != VOIDmode
4022               || gen_lowpart_common (mode, SUBREG_REG (x))))
4023         return gen_lowpart (mode, SUBREG_REG (x));
4024
4025       if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
4026         break;
4027       {
4028         rtx temp;
4029         temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
4030                                 SUBREG_BYTE (x));
4031         if (temp)
4032           return temp;
4033       }
4034
4035       /* Don't change the mode of the MEM if that would change the meaning
4036          of the address.  */
4037       if (MEM_P (SUBREG_REG (x))
4038           && (MEM_VOLATILE_P (SUBREG_REG (x))
4039               || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
4040         return gen_rtx_CLOBBER (mode, const0_rtx);
4041
4042       /* Note that we cannot do any narrowing for non-constants since
4043          we might have been counting on using the fact that some bits were
4044          zero.  We now do this in the SET.  */
4045
4046       break;
4047
4048     case NOT:
4049       if (GET_CODE (XEXP (x, 0)) == SUBREG
4050           && subreg_lowpart_p (XEXP (x, 0))
4051           && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
4052               < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
4053           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
4054           && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
4055         {
4056           enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
4057
4058           x = gen_rtx_ROTATE (inner_mode,
4059                               simplify_gen_unary (NOT, inner_mode, const1_rtx,
4060                                                   inner_mode),
4061                               XEXP (SUBREG_REG (XEXP (x, 0)), 1));
4062           return gen_lowpart (mode, x);
4063         }
4064
4065       /* Apply De Morgan's laws to reduce number of patterns for machines
4066          with negating logical insns (and-not, nand, etc.).  If result has
4067          only one NOT, put it first, since that is how the patterns are
4068          coded.  */
4069
4070       if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
4071         {
4072           rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
4073           enum machine_mode op_mode;
4074
4075           op_mode = GET_MODE (in1);
4076           in1 = simplify_gen_unary (NOT, op_mode, in1, op_mode);
4077
4078           op_mode = GET_MODE (in2);
4079           if (op_mode == VOIDmode)
4080             op_mode = mode;
4081           in2 = simplify_gen_unary (NOT, op_mode, in2, op_mode);
4082
4083           if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
4084             {
4085               rtx tem = in2;
4086               in2 = in1; in1 = tem;
4087             }
4088
4089           return gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
4090                                  mode, in1, in2);
4091         }
4092       break;
4093
4094     case NEG:
4095       /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1.  */
4096       if (GET_CODE (XEXP (x, 0)) == XOR
4097           && XEXP (XEXP (x, 0), 1) == const1_rtx
4098           && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
4099         return simplify_gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
4100                                     constm1_rtx);
4101
4102       temp = expand_compound_operation (XEXP (x, 0));
4103
4104       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
4105          replaced by (lshiftrt X C).  This will convert
4106          (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
4107
4108       if (GET_CODE (temp) == ASHIFTRT
4109           && GET_CODE (XEXP (temp, 1)) == CONST_INT
4110           && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
4111         return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
4112                                      INTVAL (XEXP (temp, 1)));
4113
4114       /* If X has only a single bit that might be nonzero, say, bit I, convert
4115          (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
4116          MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
4117          (sign_extract X 1 Y).  But only do this if TEMP isn't a register
4118          or a SUBREG of one since we'd be making the expression more
4119          complex if it was just a register.  */
4120
4121       if (!REG_P (temp)
4122           && ! (GET_CODE (temp) == SUBREG
4123                 && REG_P (SUBREG_REG (temp)))
4124           && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
4125         {
4126           rtx temp1 = simplify_shift_const
4127             (NULL_RTX, ASHIFTRT, mode,
4128              simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
4129                                    GET_MODE_BITSIZE (mode) - 1 - i),
4130              GET_MODE_BITSIZE (mode) - 1 - i);
4131
4132           /* If all we did was surround TEMP with the two shifts, we
4133              haven't improved anything, so don't use it.  Otherwise,
4134              we are better off with TEMP1.  */
4135           if (GET_CODE (temp1) != ASHIFTRT
4136               || GET_CODE (XEXP (temp1, 0)) != ASHIFT
4137               || XEXP (XEXP (temp1, 0), 0) != temp)
4138             return temp1;
4139         }
4140       break;
4141
4142     case TRUNCATE:
4143       /* We can't handle truncation to a partial integer mode here
4144          because we don't know the real bitsize of the partial
4145          integer mode.  */
4146       if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
4147         break;
4148
4149       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4150           && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4151                                     GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
4152         SUBST (XEXP (x, 0),
4153                force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
4154                               GET_MODE_MASK (mode), NULL_RTX, 0));
4155
4156       /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI.  */
4157       if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4158            || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4159           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4160         return XEXP (XEXP (x, 0), 0);
4161
4162       /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
4163          (OP:SI foo:SI) if OP is NEG or ABS.  */
4164       if ((GET_CODE (XEXP (x, 0)) == ABS
4165            || GET_CODE (XEXP (x, 0)) == NEG)
4166           && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
4167               || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
4168           && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4169         return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4170                                    XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
4171
4172       /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
4173          (truncate:SI x).  */
4174       if (GET_CODE (XEXP (x, 0)) == SUBREG
4175           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
4176           && subreg_lowpart_p (XEXP (x, 0)))
4177         return SUBREG_REG (XEXP (x, 0));
4178
4179       /* If we know that the value is already truncated, we can
4180          replace the TRUNCATE with a SUBREG if TRULY_NOOP_TRUNCATION
4181          is nonzero for the corresponding modes.  But don't do this
4182          for an (LSHIFTRT (MULT ...)) since this will cause problems
4183          with the umulXi3_highpart patterns.  */
4184       if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4185                                  GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
4186           && num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4187              >= (unsigned int) (GET_MODE_BITSIZE (mode) + 1)
4188           && ! (GET_CODE (XEXP (x, 0)) == LSHIFTRT
4189                 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT))
4190         return gen_lowpart (mode, XEXP (x, 0));
4191
4192       /* A truncate of a comparison can be replaced with a subreg if
4193          STORE_FLAG_VALUE permits.  This is like the previous test,
4194          but it works even if the comparison is done in a mode larger
4195          than HOST_BITS_PER_WIDE_INT.  */
4196       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4197           && COMPARISON_P (XEXP (x, 0))
4198           && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0)
4199         return gen_lowpart (mode, XEXP (x, 0));
4200
4201       /* Similarly, a truncate of a register whose value is a
4202          comparison can be replaced with a subreg if STORE_FLAG_VALUE
4203          permits.  */
4204       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4205           && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4206           && (temp = get_last_value (XEXP (x, 0)))
4207           && COMPARISON_P (temp))
4208         return gen_lowpart (mode, XEXP (x, 0));
4209
4210       break;
4211
4212     case FLOAT_TRUNCATE:
4213       /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF.  */
4214       if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4215           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4216         return XEXP (XEXP (x, 0), 0);
4217
4218       /* (float_truncate:SF (float_truncate:DF foo:XF))
4219          = (float_truncate:SF foo:XF).
4220          This may eliminate double rounding, so it is unsafe.
4221
4222          (float_truncate:SF (float_extend:XF foo:DF))
4223          = (float_truncate:SF foo:DF).
4224
4225          (float_truncate:DF (float_extend:XF foo:SF))
4226          = (float_extend:SF foo:DF).  */
4227       if ((GET_CODE (XEXP (x, 0)) == FLOAT_TRUNCATE
4228            && flag_unsafe_math_optimizations)
4229           || GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND)
4230         return simplify_gen_unary (GET_MODE_SIZE (GET_MODE (XEXP (XEXP (x, 0),
4231                                                             0)))
4232                                    > GET_MODE_SIZE (mode)
4233                                    ? FLOAT_TRUNCATE : FLOAT_EXTEND,
4234                                    mode,
4235                                    XEXP (XEXP (x, 0), 0), mode);
4236
4237       /*  (float_truncate (float x)) is (float x)  */
4238       if (GET_CODE (XEXP (x, 0)) == FLOAT
4239           && (flag_unsafe_math_optimizations
4240               || ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4241                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4242                       - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4243                                              GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4244         return simplify_gen_unary (FLOAT, mode,
4245                                    XEXP (XEXP (x, 0), 0),
4246                                    GET_MODE (XEXP (XEXP (x, 0), 0)));
4247
4248       /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
4249          (OP:SF foo:SF) if OP is NEG or ABS.  */
4250       if ((GET_CODE (XEXP (x, 0)) == ABS
4251            || GET_CODE (XEXP (x, 0)) == NEG)
4252           && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
4253           && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4254         return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4255                                    XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
4256
4257       /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
4258          is (float_truncate:SF x).  */
4259       if (GET_CODE (XEXP (x, 0)) == SUBREG
4260           && subreg_lowpart_p (XEXP (x, 0))
4261           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
4262         return SUBREG_REG (XEXP (x, 0));
4263       break;
4264     case FLOAT_EXTEND:
4265       /*  (float_extend (float_extend x)) is (float_extend x)
4266
4267           (float_extend (float x)) is (float x) assuming that double
4268           rounding can't happen.
4269           */
4270       if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4271           || (GET_CODE (XEXP (x, 0)) == FLOAT
4272               && ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4273                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4274                       - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4275                                              GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4276         return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4277                                    XEXP (XEXP (x, 0), 0),
4278                                    GET_MODE (XEXP (XEXP (x, 0), 0)));
4279
4280       break;
4281 #ifdef HAVE_cc0
4282     case COMPARE:
4283       /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4284          using cc0, in which case we want to leave it as a COMPARE
4285          so we can distinguish it from a register-register-copy.  */
4286       if (XEXP (x, 1) == const0_rtx)
4287         return XEXP (x, 0);
4288
4289       /* x - 0 is the same as x unless x's mode has signed zeros and
4290          allows rounding towards -infinity.  Under those conditions,
4291          0 - 0 is -0.  */
4292       if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4293             && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4294           && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4295         return XEXP (x, 0);
4296       break;
4297 #endif
4298
4299     case CONST:
4300       /* (const (const X)) can become (const X).  Do it this way rather than
4301          returning the inner CONST since CONST can be shared with a
4302          REG_EQUAL note.  */
4303       if (GET_CODE (XEXP (x, 0)) == CONST)
4304         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4305       break;
4306
4307 #ifdef HAVE_lo_sum
4308     case LO_SUM:
4309       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
4310          can add in an offset.  find_split_point will split this address up
4311          again if it doesn't match.  */
4312       if (GET_CODE (XEXP (x, 0)) == HIGH
4313           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4314         return XEXP (x, 1);
4315       break;
4316 #endif
4317
4318     case PLUS:
4319       /* Canonicalize (plus (mult (neg B) C) A) to (minus A (mult B C)).
4320        */
4321       if (GET_CODE (XEXP (x, 0)) == MULT
4322           && GET_CODE (XEXP (XEXP (x, 0), 0)) == NEG)
4323         {
4324           rtx in1, in2;
4325
4326           in1 = XEXP (XEXP (XEXP (x, 0), 0), 0);
4327           in2 = XEXP (XEXP (x, 0), 1);
4328           return simplify_gen_binary (MINUS, mode, XEXP (x, 1),
4329                                       simplify_gen_binary (MULT, mode,
4330                                                            in1, in2));
4331         }
4332
4333       /* If we have (plus (plus (A const) B)), associate it so that CONST is
4334          outermost.  That's because that's the way indexed addresses are
4335          supposed to appear.  This code used to check many more cases, but
4336          they are now checked elsewhere.  */
4337       if (GET_CODE (XEXP (x, 0)) == PLUS
4338           && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
4339         return simplify_gen_binary (PLUS, mode,
4340                                     simplify_gen_binary (PLUS, mode,
4341                                                          XEXP (XEXP (x, 0), 0),
4342                                                          XEXP (x, 1)),
4343                                     XEXP (XEXP (x, 0), 1));
4344
4345       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4346          when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4347          bit-field and can be replaced by either a sign_extend or a
4348          sign_extract.  The `and' may be a zero_extend and the two
4349          <c>, -<c> constants may be reversed.  */
4350       if (GET_CODE (XEXP (x, 0)) == XOR
4351           && GET_CODE (XEXP (x, 1)) == CONST_INT
4352           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4353           && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4354           && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4355               || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4356           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4357           && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4358                && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4359                && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4360                    == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4361               || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4362                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4363                       == (unsigned int) i + 1))))
4364         return simplify_shift_const
4365           (NULL_RTX, ASHIFTRT, mode,
4366            simplify_shift_const (NULL_RTX, ASHIFT, mode,
4367                                  XEXP (XEXP (XEXP (x, 0), 0), 0),
4368                                  GET_MODE_BITSIZE (mode) - (i + 1)),
4369            GET_MODE_BITSIZE (mode) - (i + 1));
4370
4371       /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
4372          C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
4373          is 1.  This produces better code than the alternative immediately
4374          below.  */
4375       if (COMPARISON_P (XEXP (x, 0))
4376           && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
4377               || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx))
4378           && (reversed = reversed_comparison (XEXP (x, 0), mode)))
4379         return
4380           simplify_gen_unary (NEG, mode, reversed, mode);
4381
4382       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4383          can become (ashiftrt (ashift (xor x 1) C) C) where C is
4384          the bitsize of the mode - 1.  This allows simplification of
4385          "a = (b & 8) == 0;"  */
4386       if (XEXP (x, 1) == constm1_rtx
4387           && !REG_P (XEXP (x, 0))
4388           && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4389                 && REG_P (SUBREG_REG (XEXP (x, 0))))
4390           && nonzero_bits (XEXP (x, 0), mode) == 1)
4391         return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4392            simplify_shift_const (NULL_RTX, ASHIFT, mode,
4393                                  gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4394                                  GET_MODE_BITSIZE (mode) - 1),
4395            GET_MODE_BITSIZE (mode) - 1);
4396
4397       /* If we are adding two things that have no bits in common, convert
4398          the addition into an IOR.  This will often be further simplified,
4399          for example in cases like ((a & 1) + (a & 2)), which can
4400          become a & 3.  */
4401
4402       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4403           && (nonzero_bits (XEXP (x, 0), mode)
4404               & nonzero_bits (XEXP (x, 1), mode)) == 0)
4405         {
4406           /* Try to simplify the expression further.  */
4407           rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4408           temp = combine_simplify_rtx (tor, mode, in_dest);
4409
4410           /* If we could, great.  If not, do not go ahead with the IOR
4411              replacement, since PLUS appears in many special purpose
4412              address arithmetic instructions.  */
4413           if (GET_CODE (temp) != CLOBBER && temp != tor)
4414             return temp;
4415         }
4416       break;
4417
4418     case MINUS:
4419       /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
4420          by reversing the comparison code if valid.  */
4421       if (STORE_FLAG_VALUE == 1
4422           && XEXP (x, 0) == const1_rtx
4423           && COMPARISON_P (XEXP (x, 1))
4424           && (reversed = reversed_comparison (XEXP (x, 1), mode)))
4425         return reversed;
4426
4427       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4428          (and <foo> (const_int pow2-1))  */
4429       if (GET_CODE (XEXP (x, 1)) == AND
4430           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4431           && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4432           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4433         return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4434                                        -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4435
4436       /* Canonicalize (minus A (mult (neg B) C)) to (plus (mult B C) A).
4437        */
4438       if (GET_CODE (XEXP (x, 1)) == MULT
4439           && GET_CODE (XEXP (XEXP (x, 1), 0)) == NEG)
4440         {
4441           rtx in1, in2;
4442
4443           in1 = XEXP (XEXP (XEXP (x, 1), 0), 0);
4444           in2 = XEXP (XEXP (x, 1), 1);
4445           return simplify_gen_binary (PLUS, mode,
4446                                       simplify_gen_binary (MULT, mode,
4447                                                            in1, in2),
4448                                       XEXP (x, 0));
4449         }
4450
4451       /* Canonicalize (minus (neg A) (mult B C)) to
4452          (minus (mult (neg B) C) A).  */
4453       if (GET_CODE (XEXP (x, 1)) == MULT
4454           && GET_CODE (XEXP (x, 0)) == NEG)
4455         {
4456           rtx in1, in2;
4457
4458           in1 = simplify_gen_unary (NEG, mode, XEXP (XEXP (x, 1), 0), mode);
4459           in2 = XEXP (XEXP (x, 1), 1);
4460           return simplify_gen_binary (MINUS, mode,
4461                                       simplify_gen_binary (MULT, mode,
4462                                                            in1, in2),
4463                                       XEXP (XEXP (x, 0), 0));
4464         }
4465
4466       /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
4467          integers.  */
4468       if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
4469         return simplify_gen_binary (MINUS, mode,
4470                                     simplify_gen_binary (MINUS, mode,
4471                                                          XEXP (x, 0),
4472                                                          XEXP (XEXP (x, 1), 0)),
4473                                     XEXP (XEXP (x, 1), 1));
4474       break;
4475
4476     case MULT:
4477       /* If we have (mult (plus A B) C), apply the distributive law and then
4478          the inverse distributive law to see if things simplify.  This
4479          occurs mostly in addresses, often when unrolling loops.  */
4480
4481       if (GET_CODE (XEXP (x, 0)) == PLUS)
4482         {
4483           rtx result = distribute_and_simplify_rtx (x, 0);
4484           if (result)
4485             return result;
4486         }
4487
4488       /* Try simplify a*(b/c) as (a*b)/c.  */
4489       if (FLOAT_MODE_P (mode) && flag_unsafe_math_optimizations
4490           && GET_CODE (XEXP (x, 0)) == DIV)
4491         {
4492           rtx tem = simplify_binary_operation (MULT, mode,
4493                                                XEXP (XEXP (x, 0), 0),
4494                                                XEXP (x, 1));
4495           if (tem)
4496             return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
4497         }
4498       break;
4499
4500     case UDIV:
4501       /* If this is a divide by a power of two, treat it as a shift if
4502          its first operand is a shift.  */
4503       if (GET_CODE (XEXP (x, 1)) == CONST_INT
4504           && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4505           && (GET_CODE (XEXP (x, 0)) == ASHIFT
4506               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4507               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4508               || GET_CODE (XEXP (x, 0)) == ROTATE
4509               || GET_CODE (XEXP (x, 0)) == ROTATERT))
4510         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4511       break;
4512
4513     case EQ:  case NE:
4514     case GT:  case GTU:  case GE:  case GEU:
4515     case LT:  case LTU:  case LE:  case LEU:
4516     case UNEQ:  case LTGT:
4517     case UNGT:  case UNGE:
4518     case UNLT:  case UNLE:
4519     case UNORDERED: case ORDERED:
4520       /* If the first operand is a condition code, we can't do anything
4521          with it.  */
4522       if (GET_CODE (XEXP (x, 0)) == COMPARE
4523           || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4524               && ! CC0_P (XEXP (x, 0))))
4525         {
4526           rtx op0 = XEXP (x, 0);
4527           rtx op1 = XEXP (x, 1);
4528           enum rtx_code new_code;
4529
4530           if (GET_CODE (op0) == COMPARE)
4531             op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4532
4533           /* Simplify our comparison, if possible.  */
4534           new_code = simplify_comparison (code, &op0, &op1);
4535
4536           /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4537              if only the low-order bit is possibly nonzero in X (such as when
4538              X is a ZERO_EXTRACT of one bit).  Similarly, we can convert EQ to
4539              (xor X 1) or (minus 1 X); we use the former.  Finally, if X is
4540              known to be either 0 or -1, NE becomes a NEG and EQ becomes
4541              (plus X 1).
4542
4543              Remove any ZERO_EXTRACT we made when thinking this was a
4544              comparison.  It may now be simpler to use, e.g., an AND.  If a
4545              ZERO_EXTRACT is indeed appropriate, it will be placed back by
4546              the call to make_compound_operation in the SET case.  */
4547
4548           if (STORE_FLAG_VALUE == 1
4549               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4550               && op1 == const0_rtx
4551               && mode == GET_MODE (op0)
4552               && nonzero_bits (op0, mode) == 1)
4553             return gen_lowpart (mode,
4554                                 expand_compound_operation (op0));
4555
4556           else if (STORE_FLAG_VALUE == 1
4557                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4558                    && op1 == const0_rtx
4559                    && mode == GET_MODE (op0)
4560                    && (num_sign_bit_copies (op0, mode)
4561                        == GET_MODE_BITSIZE (mode)))
4562             {
4563               op0 = expand_compound_operation (op0);
4564               return simplify_gen_unary (NEG, mode,
4565                                          gen_lowpart (mode, op0),
4566                                          mode);
4567             }
4568
4569           else if (STORE_FLAG_VALUE == 1
4570                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4571                    && op1 == const0_rtx
4572                    && mode == GET_MODE (op0)
4573                    && nonzero_bits (op0, mode) == 1)
4574             {
4575               op0 = expand_compound_operation (op0);
4576               return simplify_gen_binary (XOR, mode,
4577                                           gen_lowpart (mode, op0),
4578                                           const1_rtx);
4579             }
4580
4581           else if (STORE_FLAG_VALUE == 1
4582                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4583                    && op1 == const0_rtx
4584                    && mode == GET_MODE (op0)
4585                    && (num_sign_bit_copies (op0, mode)
4586                        == GET_MODE_BITSIZE (mode)))
4587             {
4588               op0 = expand_compound_operation (op0);
4589               return plus_constant (gen_lowpart (mode, op0), 1);
4590             }
4591
4592           /* If STORE_FLAG_VALUE is -1, we have cases similar to
4593              those above.  */
4594           if (STORE_FLAG_VALUE == -1
4595               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4596               && op1 == const0_rtx
4597               && (num_sign_bit_copies (op0, mode)
4598                   == GET_MODE_BITSIZE (mode)))
4599             return gen_lowpart (mode,
4600                                 expand_compound_operation (op0));
4601
4602           else if (STORE_FLAG_VALUE == -1
4603                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4604                    && op1 == const0_rtx
4605                    && mode == GET_MODE (op0)
4606                    && nonzero_bits (op0, mode) == 1)
4607             {
4608               op0 = expand_compound_operation (op0);
4609               return simplify_gen_unary (NEG, mode,
4610                                          gen_lowpart (mode, op0),
4611                                          mode);
4612             }
4613
4614           else if (STORE_FLAG_VALUE == -1
4615                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4616                    && op1 == const0_rtx
4617                    && mode == GET_MODE (op0)
4618                    && (num_sign_bit_copies (op0, mode)
4619                        == GET_MODE_BITSIZE (mode)))
4620             {
4621               op0 = expand_compound_operation (op0);
4622               return simplify_gen_unary (NOT, mode,
4623                                          gen_lowpart (mode, op0),
4624                                          mode);
4625             }
4626
4627           /* If X is 0/1, (eq X 0) is X-1.  */
4628           else if (STORE_FLAG_VALUE == -1
4629                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4630                    && op1 == const0_rtx
4631                    && mode == GET_MODE (op0)
4632                    && nonzero_bits (op0, mode) == 1)
4633             {
4634               op0 = expand_compound_operation (op0);
4635               return plus_constant (gen_lowpart (mode, op0), -1);
4636             }
4637
4638           /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4639              one bit that might be nonzero, we can convert (ne x 0) to
4640              (ashift x c) where C puts the bit in the sign bit.  Remove any
4641              AND with STORE_FLAG_VALUE when we are done, since we are only
4642              going to test the sign bit.  */
4643           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4644               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4645               && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4646                   == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
4647               && op1 == const0_rtx
4648               && mode == GET_MODE (op0)
4649               && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4650             {
4651               x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4652                                         expand_compound_operation (op0),
4653                                         GET_MODE_BITSIZE (mode) - 1 - i);
4654               if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4655                 return XEXP (x, 0);
4656               else
4657                 return x;
4658             }
4659
4660           /* If the code changed, return a whole new comparison.  */
4661           if (new_code != code)
4662             return gen_rtx_fmt_ee (new_code, mode, op0, op1);
4663
4664           /* Otherwise, keep this operation, but maybe change its operands.
4665              This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
4666           SUBST (XEXP (x, 0), op0);
4667           SUBST (XEXP (x, 1), op1);
4668         }
4669       break;
4670
4671     case IF_THEN_ELSE:
4672       return simplify_if_then_else (x);
4673
4674     case ZERO_EXTRACT:
4675     case SIGN_EXTRACT:
4676     case ZERO_EXTEND:
4677     case SIGN_EXTEND:
4678       /* If we are processing SET_DEST, we are done.  */
4679       if (in_dest)
4680         return x;
4681
4682       return expand_compound_operation (x);
4683
4684     case SET:
4685       return simplify_set (x);
4686
4687     case AND:
4688     case IOR:
4689     case XOR:
4690       return simplify_logical (x);
4691
4692     case ABS:
4693       /* (abs (neg <foo>)) -> (abs <foo>) */
4694       if (GET_CODE (XEXP (x, 0)) == NEG)
4695         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4696
4697       /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
4698          do nothing.  */
4699       if (GET_MODE (XEXP (x, 0)) == VOIDmode)
4700         break;
4701
4702       /* If operand is something known to be positive, ignore the ABS.  */
4703       if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4704           || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4705                <= HOST_BITS_PER_WIDE_INT)
4706               && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4707                    & ((HOST_WIDE_INT) 1
4708                       << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4709                   == 0)))
4710         return XEXP (x, 0);
4711
4712       /* If operand is known to be only -1 or 0, convert ABS to NEG.  */
4713       if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4714         return gen_rtx_NEG (mode, XEXP (x, 0));
4715
4716       break;
4717
4718     case FFS:
4719       /* (ffs (*_extend <X>)) = (ffs <X>) */
4720       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4721           || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4722         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4723       break;
4724
4725     case POPCOUNT:
4726     case PARITY:
4727       /* (pop* (zero_extend <X>)) = (pop* <X>) */
4728       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4729         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4730       break;
4731
4732     case FLOAT:
4733       /* (float (sign_extend <X>)) = (float <X>).  */
4734       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4735         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4736       break;
4737
4738     case ASHIFT:
4739     case LSHIFTRT:
4740     case ASHIFTRT:
4741     case ROTATE:
4742     case ROTATERT:
4743       /* If this is a shift by a constant amount, simplify it.  */
4744       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4745         return simplify_shift_const (x, code, mode, XEXP (x, 0),
4746                                      INTVAL (XEXP (x, 1)));
4747
4748       else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
4749         SUBST (XEXP (x, 1),
4750                force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
4751                               ((HOST_WIDE_INT) 1
4752                                << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4753                               - 1,
4754                               NULL_RTX, 0));
4755       break;
4756
4757     case VEC_SELECT:
4758       {
4759         rtx op0 = XEXP (x, 0);
4760         rtx op1 = XEXP (x, 1);
4761         int len;
4762
4763         gcc_assert (GET_CODE (op1) == PARALLEL);
4764         len = XVECLEN (op1, 0);
4765         if (len == 1
4766             && GET_CODE (XVECEXP (op1, 0, 0)) == CONST_INT
4767             && GET_CODE (op0) == VEC_CONCAT)
4768           {
4769             int offset = INTVAL (XVECEXP (op1, 0, 0)) * GET_MODE_SIZE (GET_MODE (x));
4770
4771             /* Try to find the element in the VEC_CONCAT.  */
4772             for (;;)
4773               {
4774                 if (GET_MODE (op0) == GET_MODE (x))
4775                   return op0;
4776                 if (GET_CODE (op0) == VEC_CONCAT)
4777                   {
4778                     HOST_WIDE_INT op0_size = GET_MODE_SIZE (GET_MODE (XEXP (op0, 0)));
4779                     if (op0_size < offset)
4780                       op0 = XEXP (op0, 0);
4781                     else
4782                       {
4783                         offset -= op0_size;
4784                         op0 = XEXP (op0, 1);
4785                       }
4786                   }
4787                 else
4788                   break;
4789               }
4790           }
4791       }
4792
4793       break;
4794
4795     default:
4796       break;
4797     }
4798
4799   return x;
4800 }
4801 \f
4802 /* Simplify X, an IF_THEN_ELSE expression.  Return the new expression.  */
4803
4804 static rtx
4805 simplify_if_then_else (rtx x)
4806 {
4807   enum machine_mode mode = GET_MODE (x);
4808   rtx cond = XEXP (x, 0);
4809   rtx true_rtx = XEXP (x, 1);
4810   rtx false_rtx = XEXP (x, 2);
4811   enum rtx_code true_code = GET_CODE (cond);
4812   int comparison_p = COMPARISON_P (cond);
4813   rtx temp;
4814   int i;
4815   enum rtx_code false_code;
4816   rtx reversed;
4817
4818   /* Simplify storing of the truth value.  */
4819   if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
4820     return simplify_gen_relational (true_code, mode, VOIDmode,
4821                                     XEXP (cond, 0), XEXP (cond, 1));
4822
4823   /* Also when the truth value has to be reversed.  */
4824   if (comparison_p
4825       && true_rtx == const0_rtx && false_rtx == const_true_rtx
4826       && (reversed = reversed_comparison (cond, mode)))
4827     return reversed;
4828
4829   /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4830      in it is being compared against certain values.  Get the true and false
4831      comparisons and see if that says anything about the value of each arm.  */
4832
4833   if (comparison_p
4834       && ((false_code = reversed_comparison_code (cond, NULL))
4835           != UNKNOWN)
4836       && REG_P (XEXP (cond, 0)))
4837     {
4838       HOST_WIDE_INT nzb;
4839       rtx from = XEXP (cond, 0);
4840       rtx true_val = XEXP (cond, 1);
4841       rtx false_val = true_val;
4842       int swapped = 0;
4843
4844       /* If FALSE_CODE is EQ, swap the codes and arms.  */
4845
4846       if (false_code == EQ)
4847         {
4848           swapped = 1, true_code = EQ, false_code = NE;
4849           temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4850         }
4851
4852       /* If we are comparing against zero and the expression being tested has
4853          only a single bit that might be nonzero, that is its value when it is
4854          not equal to zero.  Similarly if it is known to be -1 or 0.  */
4855
4856       if (true_code == EQ && true_val == const0_rtx
4857           && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4858         false_code = EQ, false_val = GEN_INT (nzb);
4859       else if (true_code == EQ && true_val == const0_rtx
4860                && (num_sign_bit_copies (from, GET_MODE (from))
4861                    == GET_MODE_BITSIZE (GET_MODE (from))))
4862         false_code = EQ, false_val = constm1_rtx;
4863
4864       /* Now simplify an arm if we know the value of the register in the
4865          branch and it is used in the arm.  Be careful due to the potential
4866          of locally-shared RTL.  */
4867
4868       if (reg_mentioned_p (from, true_rtx))
4869         true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
4870                                       from, true_val),
4871                       pc_rtx, pc_rtx, 0, 0);
4872       if (reg_mentioned_p (from, false_rtx))
4873         false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
4874                                    from, false_val),
4875                        pc_rtx, pc_rtx, 0, 0);
4876
4877       SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
4878       SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
4879
4880       true_rtx = XEXP (x, 1);
4881       false_rtx = XEXP (x, 2);
4882       true_code = GET_CODE (cond);
4883     }
4884
4885   /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4886      reversed, do so to avoid needing two sets of patterns for
4887      subtract-and-branch insns.  Similarly if we have a constant in the true
4888      arm, the false arm is the same as the first operand of the comparison, or
4889      the false arm is more complicated than the true arm.  */
4890
4891   if (comparison_p
4892       && reversed_comparison_code (cond, NULL) != UNKNOWN
4893       && (true_rtx == pc_rtx
4894           || (CONSTANT_P (true_rtx)
4895               && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
4896           || true_rtx == const0_rtx
4897           || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
4898           || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
4899               && !OBJECT_P (false_rtx))
4900           || reg_mentioned_p (true_rtx, false_rtx)
4901           || rtx_equal_p (false_rtx, XEXP (cond, 0))))
4902     {
4903       true_code = reversed_comparison_code (cond, NULL);
4904       SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
4905       SUBST (XEXP (x, 1), false_rtx);
4906       SUBST (XEXP (x, 2), true_rtx);
4907
4908       temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4909       cond = XEXP (x, 0);
4910
4911       /* It is possible that the conditional has been simplified out.  */
4912       true_code = GET_CODE (cond);
4913       comparison_p = COMPARISON_P (cond);
4914     }
4915
4916   /* If the two arms are identical, we don't need the comparison.  */
4917
4918   if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
4919     return true_rtx;
4920
4921   /* Convert a == b ? b : a to "a".  */
4922   if (true_code == EQ && ! side_effects_p (cond)
4923       && !HONOR_NANS (mode)
4924       && rtx_equal_p (XEXP (cond, 0), false_rtx)
4925       && rtx_equal_p (XEXP (cond, 1), true_rtx))
4926     return false_rtx;
4927   else if (true_code == NE && ! side_effects_p (cond)
4928            && !HONOR_NANS (mode)
4929            && rtx_equal_p (XEXP (cond, 0), true_rtx)
4930            && rtx_equal_p (XEXP (cond, 1), false_rtx))
4931     return true_rtx;
4932
4933   /* Look for cases where we have (abs x) or (neg (abs X)).  */
4934
4935   if (GET_MODE_CLASS (mode) == MODE_INT
4936       && GET_CODE (false_rtx) == NEG
4937       && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
4938       && comparison_p
4939       && rtx_equal_p (true_rtx, XEXP (cond, 0))
4940       && ! side_effects_p (true_rtx))
4941     switch (true_code)
4942       {
4943       case GT:
4944       case GE:
4945         return simplify_gen_unary (ABS, mode, true_rtx, mode);
4946       case LT:
4947       case LE:
4948         return
4949           simplify_gen_unary (NEG, mode,
4950                               simplify_gen_unary (ABS, mode, true_rtx, mode),
4951                               mode);
4952       default:
4953         break;
4954       }
4955
4956   /* Look for MIN or MAX.  */
4957
4958   if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
4959       && comparison_p
4960       && rtx_equal_p (XEXP (cond, 0), true_rtx)
4961       && rtx_equal_p (XEXP (cond, 1), false_rtx)
4962       && ! side_effects_p (cond))
4963     switch (true_code)
4964       {
4965       case GE:
4966       case GT:
4967         return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
4968       case LE:
4969       case LT:
4970         return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
4971       case GEU:
4972       case GTU:
4973         return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
4974       case LEU:
4975       case LTU:
4976         return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
4977       default:
4978         break;
4979       }
4980
4981   /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4982      second operand is zero, this can be done as (OP Z (mult COND C2)) where
4983      C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4984      SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4985      We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4986      neither 1 or -1, but it isn't worth checking for.  */
4987
4988   if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4989       && comparison_p
4990       && GET_MODE_CLASS (mode) == MODE_INT
4991       && ! side_effects_p (x))
4992     {
4993       rtx t = make_compound_operation (true_rtx, SET);
4994       rtx f = make_compound_operation (false_rtx, SET);
4995       rtx cond_op0 = XEXP (cond, 0);
4996       rtx cond_op1 = XEXP (cond, 1);
4997       enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
4998       enum machine_mode m = mode;
4999       rtx z = 0, c1 = NULL_RTX;
5000
5001       if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
5002            || GET_CODE (t) == IOR || GET_CODE (t) == XOR
5003            || GET_CODE (t) == ASHIFT
5004            || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
5005           && rtx_equal_p (XEXP (t, 0), f))
5006         c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
5007
5008       /* If an identity-zero op is commutative, check whether there
5009          would be a match if we swapped the operands.  */
5010       else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
5011                 || GET_CODE (t) == XOR)
5012                && rtx_equal_p (XEXP (t, 1), f))
5013         c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
5014       else if (GET_CODE (t) == SIGN_EXTEND
5015                && (GET_CODE (XEXP (t, 0)) == PLUS
5016                    || GET_CODE (XEXP (t, 0)) == MINUS
5017                    || GET_CODE (XEXP (t, 0)) == IOR
5018                    || GET_CODE (XEXP (t, 0)) == XOR
5019                    || GET_CODE (XEXP (t, 0)) == ASHIFT
5020                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5021                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5022                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5023                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5024                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5025                && (num_sign_bit_copies (f, GET_MODE (f))
5026                    > (unsigned int)
5027                      (GET_MODE_BITSIZE (mode)
5028                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
5029         {
5030           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5031           extend_op = SIGN_EXTEND;
5032           m = GET_MODE (XEXP (t, 0));
5033         }
5034       else if (GET_CODE (t) == SIGN_EXTEND
5035                && (GET_CODE (XEXP (t, 0)) == PLUS
5036                    || GET_CODE (XEXP (t, 0)) == IOR
5037                    || GET_CODE (XEXP (t, 0)) == XOR)
5038                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5039                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5040                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5041                && (num_sign_bit_copies (f, GET_MODE (f))
5042                    > (unsigned int)
5043                      (GET_MODE_BITSIZE (mode)
5044                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
5045         {
5046           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5047           extend_op = SIGN_EXTEND;
5048           m = GET_MODE (XEXP (t, 0));
5049         }
5050       else if (GET_CODE (t) == ZERO_EXTEND
5051                && (GET_CODE (XEXP (t, 0)) == PLUS
5052                    || GET_CODE (XEXP (t, 0)) == MINUS
5053                    || GET_CODE (XEXP (t, 0)) == IOR
5054                    || GET_CODE (XEXP (t, 0)) == XOR
5055                    || GET_CODE (XEXP (t, 0)) == ASHIFT
5056                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5057                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5058                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5059                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5060                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5061                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5062                && ((nonzero_bits (f, GET_MODE (f))
5063                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
5064                    == 0))
5065         {
5066           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5067           extend_op = ZERO_EXTEND;
5068           m = GET_MODE (XEXP (t, 0));
5069         }
5070       else if (GET_CODE (t) == ZERO_EXTEND
5071                && (GET_CODE (XEXP (t, 0)) == PLUS
5072                    || GET_CODE (XEXP (t, 0)) == IOR
5073                    || GET_CODE (XEXP (t, 0)) == XOR)
5074                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5075                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5076                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5077                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5078                && ((nonzero_bits (f, GET_MODE (f))
5079                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
5080                    == 0))
5081         {
5082           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5083           extend_op = ZERO_EXTEND;
5084           m = GET_MODE (XEXP (t, 0));
5085         }
5086
5087       if (z)
5088         {
5089           temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
5090                                                  cond_op0, cond_op1),
5091                         pc_rtx, pc_rtx, 0, 0);
5092           temp = simplify_gen_binary (MULT, m, temp,
5093                                       simplify_gen_binary (MULT, m, c1,
5094                                                            const_true_rtx));
5095           temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
5096           temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
5097
5098           if (extend_op != UNKNOWN)
5099             temp = simplify_gen_unary (extend_op, mode, temp, m);
5100
5101           return temp;
5102         }
5103     }
5104
5105   /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
5106      1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
5107      negation of a single bit, we can convert this operation to a shift.  We
5108      can actually do this more generally, but it doesn't seem worth it.  */
5109
5110   if (true_code == NE && XEXP (cond, 1) == const0_rtx
5111       && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5112       && ((1 == nonzero_bits (XEXP (cond, 0), mode)
5113            && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
5114           || ((num_sign_bit_copies (XEXP (cond, 0), mode)
5115                == GET_MODE_BITSIZE (mode))
5116               && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
5117     return
5118       simplify_shift_const (NULL_RTX, ASHIFT, mode,
5119                             gen_lowpart (mode, XEXP (cond, 0)), i);
5120
5121   /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8.  */
5122   if (true_code == NE && XEXP (cond, 1) == const0_rtx
5123       && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5124       && GET_MODE (XEXP (cond, 0)) == mode
5125       && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
5126           == nonzero_bits (XEXP (cond, 0), mode)
5127       && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
5128     return XEXP (cond, 0);
5129
5130   return x;
5131 }
5132 \f
5133 /* Simplify X, a SET expression.  Return the new expression.  */
5134
5135 static rtx
5136 simplify_set (rtx x)
5137 {
5138   rtx src = SET_SRC (x);
5139   rtx dest = SET_DEST (x);
5140   enum machine_mode mode
5141     = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
5142   rtx other_insn;
5143   rtx *cc_use;
5144
5145   /* (set (pc) (return)) gets written as (return).  */
5146   if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
5147     return src;
5148
5149   /* Now that we know for sure which bits of SRC we are using, see if we can
5150      simplify the expression for the object knowing that we only need the
5151      low-order bits.  */
5152
5153   if (GET_MODE_CLASS (mode) == MODE_INT
5154       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5155     {
5156       src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, NULL_RTX, 0);
5157       SUBST (SET_SRC (x), src);
5158     }
5159
5160   /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5161      the comparison result and try to simplify it unless we already have used
5162      undobuf.other_insn.  */
5163   if ((GET_MODE_CLASS (mode) == MODE_CC
5164        || GET_CODE (src) == COMPARE
5165        || CC0_P (dest))
5166       && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5167       && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5168       && COMPARISON_P (*cc_use)
5169       && rtx_equal_p (XEXP (*cc_use, 0), dest))
5170     {
5171       enum rtx_code old_code = GET_CODE (*cc_use);
5172       enum rtx_code new_code;
5173       rtx op0, op1, tmp;
5174       int other_changed = 0;
5175       enum machine_mode compare_mode = GET_MODE (dest);
5176
5177       if (GET_CODE (src) == COMPARE)
5178         op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5179       else
5180         op0 = src, op1 = CONST0_RTX (GET_MODE (src));
5181
5182       tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
5183                                            op0, op1);
5184       if (!tmp)
5185         new_code = old_code;
5186       else if (!CONSTANT_P (tmp))
5187         {
5188           new_code = GET_CODE (tmp);
5189           op0 = XEXP (tmp, 0);
5190           op1 = XEXP (tmp, 1);
5191         }
5192       else
5193         {
5194           rtx pat = PATTERN (other_insn);
5195           undobuf.other_insn = other_insn;
5196           SUBST (*cc_use, tmp);
5197
5198           /* Attempt to simplify CC user.  */
5199           if (GET_CODE (pat) == SET)
5200             {
5201               rtx new = simplify_rtx (SET_SRC (pat));
5202               if (new != NULL_RTX)
5203                 SUBST (SET_SRC (pat), new);
5204             }
5205
5206           /* Convert X into a no-op move.  */
5207           SUBST (SET_DEST (x), pc_rtx);
5208           SUBST (SET_SRC (x), pc_rtx);
5209           return x;
5210         }
5211
5212       /* Simplify our comparison, if possible.  */
5213       new_code = simplify_comparison (new_code, &op0, &op1);
5214
5215 #ifdef SELECT_CC_MODE
5216       /* If this machine has CC modes other than CCmode, check to see if we
5217          need to use a different CC mode here.  */
5218       if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
5219         compare_mode = GET_MODE (op0);
5220       else
5221         compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5222
5223 #ifndef HAVE_cc0
5224       /* If the mode changed, we have to change SET_DEST, the mode in the
5225          compare, and the mode in the place SET_DEST is used.  If SET_DEST is
5226          a hard register, just build new versions with the proper mode.  If it
5227          is a pseudo, we lose unless it is only time we set the pseudo, in
5228          which case we can safely change its mode.  */
5229       if (compare_mode != GET_MODE (dest))
5230         {
5231           unsigned int regno = REGNO (dest);
5232           rtx new_dest = gen_rtx_REG (compare_mode, regno);
5233
5234           if (regno < FIRST_PSEUDO_REGISTER
5235               || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
5236             {
5237               if (regno >= FIRST_PSEUDO_REGISTER)
5238                 SUBST (regno_reg_rtx[regno], new_dest);
5239
5240               SUBST (SET_DEST (x), new_dest);
5241               SUBST (XEXP (*cc_use, 0), new_dest);
5242               other_changed = 1;
5243
5244               dest = new_dest;
5245             }
5246         }
5247 #endif  /* cc0 */
5248 #endif  /* SELECT_CC_MODE */
5249
5250       /* If the code changed, we have to build a new comparison in
5251          undobuf.other_insn.  */
5252       if (new_code != old_code)
5253         {
5254           int other_changed_previously = other_changed;
5255           unsigned HOST_WIDE_INT mask;
5256
5257           SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5258                                           dest, const0_rtx));
5259           other_changed = 1;
5260
5261           /* If the only change we made was to change an EQ into an NE or
5262              vice versa, OP0 has only one bit that might be nonzero, and OP1
5263              is zero, check if changing the user of the condition code will
5264              produce a valid insn.  If it won't, we can keep the original code
5265              in that insn by surrounding our operation with an XOR.  */
5266
5267           if (((old_code == NE && new_code == EQ)
5268                || (old_code == EQ && new_code == NE))
5269               && ! other_changed_previously && op1 == const0_rtx
5270               && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5271               && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5272             {
5273               rtx pat = PATTERN (other_insn), note = 0;
5274
5275               if ((recog_for_combine (&pat, other_insn, &note) < 0
5276                    && ! check_asm_operands (pat)))
5277                 {
5278                   PUT_CODE (*cc_use, old_code);
5279                   other_changed = 0;
5280
5281                   op0 = simplify_gen_binary (XOR, GET_MODE (op0),
5282                                              op0, GEN_INT (mask));
5283                 }
5284             }
5285         }
5286
5287       if (other_changed)
5288         undobuf.other_insn = other_insn;
5289
5290 #ifdef HAVE_cc0
5291       /* If we are now comparing against zero, change our source if
5292          needed.  If we do not use cc0, we always have a COMPARE.  */
5293       if (op1 == const0_rtx && dest == cc0_rtx)
5294         {
5295           SUBST (SET_SRC (x), op0);
5296           src = op0;
5297         }
5298       else
5299 #endif
5300
5301       /* Otherwise, if we didn't previously have a COMPARE in the
5302          correct mode, we need one.  */
5303       if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5304         {
5305           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5306           src = SET_SRC (x);
5307         }
5308       else
5309         {
5310           /* Otherwise, update the COMPARE if needed.  */
5311           SUBST (XEXP (src, 0), op0);
5312           SUBST (XEXP (src, 1), op1);
5313         }
5314     }
5315   else
5316     {
5317       /* Get SET_SRC in a form where we have placed back any
5318          compound expressions.  Then do the checks below.  */
5319       src = make_compound_operation (src, SET);
5320       SUBST (SET_SRC (x), src);
5321     }
5322
5323   /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5324      and X being a REG or (subreg (reg)), we may be able to convert this to
5325      (set (subreg:m2 x) (op)).
5326
5327      We can always do this if M1 is narrower than M2 because that means that
5328      we only care about the low bits of the result.
5329
5330      However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
5331      perform a narrower operation than requested since the high-order bits will
5332      be undefined.  On machine where it is defined, this transformation is safe
5333      as long as M1 and M2 have the same number of words.  */
5334
5335   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5336       && !OBJECT_P (SUBREG_REG (src))
5337       && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5338            / UNITS_PER_WORD)
5339           == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5340                + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5341 #ifndef WORD_REGISTER_OPERATIONS
5342       && (GET_MODE_SIZE (GET_MODE (src))
5343         < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5344 #endif
5345 #ifdef CANNOT_CHANGE_MODE_CLASS
5346       && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
5347             && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5348                                          GET_MODE (SUBREG_REG (src)),
5349                                          GET_MODE (src)))
5350 #endif
5351       && (REG_P (dest)
5352           || (GET_CODE (dest) == SUBREG
5353               && REG_P (SUBREG_REG (dest)))))
5354     {
5355       SUBST (SET_DEST (x),
5356              gen_lowpart (GET_MODE (SUBREG_REG (src)),
5357                                       dest));
5358       SUBST (SET_SRC (x), SUBREG_REG (src));
5359
5360       src = SET_SRC (x), dest = SET_DEST (x);
5361     }
5362
5363 #ifdef HAVE_cc0
5364   /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5365      in SRC.  */
5366   if (dest == cc0_rtx
5367       && GET_CODE (src) == SUBREG
5368       && subreg_lowpart_p (src)
5369       && (GET_MODE_BITSIZE (GET_MODE (src))
5370           < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5371     {
5372       rtx inner = SUBREG_REG (src);
5373       enum machine_mode inner_mode = GET_MODE (inner);
5374
5375       /* Here we make sure that we don't have a sign bit on.  */
5376       if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5377           && (nonzero_bits (inner, inner_mode)
5378               < ((unsigned HOST_WIDE_INT) 1
5379                  << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5380         {
5381           SUBST (SET_SRC (x), inner);
5382           src = SET_SRC (x);
5383         }
5384     }
5385 #endif
5386
5387 #ifdef LOAD_EXTEND_OP
5388   /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5389      would require a paradoxical subreg.  Replace the subreg with a
5390      zero_extend to avoid the reload that would otherwise be required.  */
5391
5392   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5393       && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
5394       && SUBREG_BYTE (src) == 0
5395       && (GET_MODE_SIZE (GET_MODE (src))
5396           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5397       && MEM_P (SUBREG_REG (src)))
5398     {
5399       SUBST (SET_SRC (x),
5400              gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5401                             GET_MODE (src), SUBREG_REG (src)));
5402
5403       src = SET_SRC (x);
5404     }
5405 #endif
5406
5407   /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5408      are comparing an item known to be 0 or -1 against 0, use a logical
5409      operation instead. Check for one of the arms being an IOR of the other
5410      arm with some value.  We compute three terms to be IOR'ed together.  In
5411      practice, at most two will be nonzero.  Then we do the IOR's.  */
5412
5413   if (GET_CODE (dest) != PC
5414       && GET_CODE (src) == IF_THEN_ELSE
5415       && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5416       && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5417       && XEXP (XEXP (src, 0), 1) == const0_rtx
5418       && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5419 #ifdef HAVE_conditional_move
5420       && ! can_conditionally_move_p (GET_MODE (src))
5421 #endif
5422       && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5423                                GET_MODE (XEXP (XEXP (src, 0), 0)))
5424           == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5425       && ! side_effects_p (src))
5426     {
5427       rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5428                       ? XEXP (src, 1) : XEXP (src, 2));
5429       rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5430                    ? XEXP (src, 2) : XEXP (src, 1));
5431       rtx term1 = const0_rtx, term2, term3;
5432
5433       if (GET_CODE (true_rtx) == IOR
5434           && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5435         term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5436       else if (GET_CODE (true_rtx) == IOR
5437                && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5438         term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5439       else if (GET_CODE (false_rtx) == IOR
5440                && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5441         term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5442       else if (GET_CODE (false_rtx) == IOR
5443                && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5444         term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5445
5446       term2 = simplify_gen_binary (AND, GET_MODE (src),
5447                                    XEXP (XEXP (src, 0), 0), true_rtx);
5448       term3 = simplify_gen_binary (AND, GET_MODE (src),
5449                                    simplify_gen_unary (NOT, GET_MODE (src),
5450                                                        XEXP (XEXP (src, 0), 0),
5451                                                        GET_MODE (src)),
5452                                    false_rtx);
5453
5454       SUBST (SET_SRC (x),
5455              simplify_gen_binary (IOR, GET_MODE (src),
5456                                   simplify_gen_binary (IOR, GET_MODE (src),
5457                                                        term1, term2),
5458                                   term3));
5459
5460       src = SET_SRC (x);
5461     }
5462
5463   /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5464      whole thing fail.  */
5465   if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5466     return src;
5467   else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5468     return dest;
5469   else
5470     /* Convert this into a field assignment operation, if possible.  */
5471     return make_field_assignment (x);
5472 }
5473 \f
5474 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5475    result.  */
5476
5477 static rtx
5478 simplify_logical (rtx x)
5479 {
5480   enum machine_mode mode = GET_MODE (x);
5481   rtx op0 = XEXP (x, 0);
5482   rtx op1 = XEXP (x, 1);
5483   rtx reversed;
5484
5485   switch (GET_CODE (x))
5486     {
5487     case AND:
5488       /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
5489          insn (and may simplify more).  */
5490       if (GET_CODE (op0) == XOR
5491           && rtx_equal_p (XEXP (op0, 0), op1)
5492           && ! side_effects_p (op1))
5493         x = simplify_gen_binary (AND, mode,
5494                                  simplify_gen_unary (NOT, mode,
5495                                                      XEXP (op0, 1), mode),
5496                                  op1);
5497
5498       if (GET_CODE (op0) == XOR
5499           && rtx_equal_p (XEXP (op0, 1), op1)
5500           && ! side_effects_p (op1))
5501         x = simplify_gen_binary (AND, mode,
5502                                  simplify_gen_unary (NOT, mode,
5503                                                      XEXP (op0, 0), mode),
5504                                  op1);
5505
5506       /* Similarly for (~(A ^ B)) & A.  */
5507       if (GET_CODE (op0) == NOT
5508           && GET_CODE (XEXP (op0, 0)) == XOR
5509           && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
5510           && ! side_effects_p (op1))
5511         x = simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
5512
5513       if (GET_CODE (op0) == NOT
5514           && GET_CODE (XEXP (op0, 0)) == XOR
5515           && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
5516           && ! side_effects_p (op1))
5517         x = simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
5518
5519       /* We can call simplify_and_const_int only if we don't lose
5520          any (sign) bits when converting INTVAL (op1) to
5521          "unsigned HOST_WIDE_INT".  */
5522       if (GET_CODE (op1) == CONST_INT
5523           && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5524               || INTVAL (op1) > 0))
5525         {
5526           x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5527
5528           /* If we have (ior (and (X C1) C2)) and the next restart would be
5529              the last, simplify this by making C1 as small as possible
5530              and then exit.  Only do this if C1 actually changes: for now
5531              this only saves memory but, should this transformation be
5532              moved to simplify-rtx.c, we'd risk unbounded recursion there.  */
5533           if (GET_CODE (x) == IOR && GET_CODE (op0) == AND
5534               && GET_CODE (XEXP (op0, 1)) == CONST_INT
5535               && GET_CODE (op1) == CONST_INT
5536               && (INTVAL (XEXP (op0, 1)) & INTVAL (op1)) != 0)
5537             return simplify_gen_binary (IOR, mode,
5538                                         simplify_gen_binary
5539                                           (AND, mode, XEXP (op0, 0),
5540                                            GEN_INT (INTVAL (XEXP (op0, 1))
5541                                                     & ~INTVAL (op1))), op1);
5542
5543           if (GET_CODE (x) != AND)
5544             return x;
5545
5546           op0 = XEXP (x, 0);
5547           op1 = XEXP (x, 1);
5548         }
5549
5550       /* Convert (A | B) & A to A.  */
5551       if (GET_CODE (op0) == IOR
5552           && (rtx_equal_p (XEXP (op0, 0), op1)
5553               || rtx_equal_p (XEXP (op0, 1), op1))
5554           && ! side_effects_p (XEXP (op0, 0))
5555           && ! side_effects_p (XEXP (op0, 1)))
5556         return op1;
5557
5558       /* If we have any of (and (ior A B) C) or (and (xor A B) C),
5559          apply the distributive law and then the inverse distributive
5560          law to see if things simplify.  */
5561       if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5562         {
5563           rtx result = distribute_and_simplify_rtx (x, 0);
5564           if (result)
5565             return result;
5566         }
5567       if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5568         {
5569           rtx result = distribute_and_simplify_rtx (x, 1);
5570           if (result)
5571             return result;
5572         }
5573       break;
5574
5575     case IOR:
5576       /* (ior A C) is C if all bits of A that might be nonzero are on in C.  */
5577       if (GET_CODE (op1) == CONST_INT
5578           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5579           && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
5580         return op1;
5581
5582       /* Convert (A & B) | A to A.  */
5583       if (GET_CODE (op0) == AND
5584           && (rtx_equal_p (XEXP (op0, 0), op1)
5585               || rtx_equal_p (XEXP (op0, 1), op1))
5586           && ! side_effects_p (XEXP (op0, 0))
5587           && ! side_effects_p (XEXP (op0, 1)))
5588         return op1;
5589
5590       /* If we have (ior (and A B) C), apply the distributive law and then
5591          the inverse distributive law to see if things simplify.  */
5592
5593       if (GET_CODE (op0) == AND)
5594         {
5595           rtx result = distribute_and_simplify_rtx (x, 0);
5596           if (result)
5597             return result;
5598         }
5599
5600       if (GET_CODE (op1) == AND)
5601         {
5602           rtx result = distribute_and_simplify_rtx (x, 1);
5603           if (result)
5604             return result;
5605         }
5606
5607       /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
5608          mode size to (rotate A CX).  */
5609
5610       if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
5611            || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
5612           && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
5613           && GET_CODE (XEXP (op0, 1)) == CONST_INT
5614           && GET_CODE (XEXP (op1, 1)) == CONST_INT
5615           && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
5616               == GET_MODE_BITSIZE (mode)))
5617         return gen_rtx_ROTATE (mode, XEXP (op0, 0),
5618                                (GET_CODE (op0) == ASHIFT
5619                                 ? XEXP (op0, 1) : XEXP (op1, 1)));
5620
5621       /* If OP0 is (ashiftrt (plus ...) C), it might actually be
5622          a (sign_extend (plus ...)).  If so, OP1 is a CONST_INT, and the PLUS
5623          does not affect any of the bits in OP1, it can really be done
5624          as a PLUS and we can associate.  We do this by seeing if OP1
5625          can be safely shifted left C bits.  */
5626       if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
5627           && GET_CODE (XEXP (op0, 0)) == PLUS
5628           && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
5629           && GET_CODE (XEXP (op0, 1)) == CONST_INT
5630           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
5631         {
5632           int count = INTVAL (XEXP (op0, 1));
5633           HOST_WIDE_INT mask = INTVAL (op1) << count;
5634
5635           if (mask >> count == INTVAL (op1)
5636               && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5637             {
5638               SUBST (XEXP (XEXP (op0, 0), 1),
5639                      GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5640               return op0;
5641             }
5642         }
5643       break;
5644
5645     case XOR:
5646       /* If we are XORing two things that have no bits in common,
5647          convert them into an IOR.  This helps to detect rotation encoded
5648          using those methods and possibly other simplifications.  */
5649
5650       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5651           && (nonzero_bits (op0, mode)
5652               & nonzero_bits (op1, mode)) == 0)
5653         return (simplify_gen_binary (IOR, mode, op0, op1));
5654
5655       /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5656          Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5657          (NOT y).  */
5658       {
5659         int num_negated = 0;
5660
5661         if (GET_CODE (op0) == NOT)
5662           num_negated++, op0 = XEXP (op0, 0);
5663         if (GET_CODE (op1) == NOT)
5664           num_negated++, op1 = XEXP (op1, 0);
5665
5666         if (num_negated == 2)
5667           {
5668             SUBST (XEXP (x, 0), op0);
5669             SUBST (XEXP (x, 1), op1);
5670           }
5671         else if (num_negated == 1)
5672           return
5673             simplify_gen_unary (NOT, mode,
5674                                 simplify_gen_binary (XOR, mode, op0, op1),
5675                                 mode);
5676       }
5677
5678       /* Convert (xor (and A B) B) to (and (not A) B).  The latter may
5679          correspond to a machine insn or result in further simplifications
5680          if B is a constant.  */
5681
5682       if (GET_CODE (op0) == AND
5683           && rtx_equal_p (XEXP (op0, 1), op1)
5684           && ! side_effects_p (op1))
5685         return simplify_gen_binary (AND, mode,
5686                                     simplify_gen_unary (NOT, mode,
5687                                                         XEXP (op0, 0), mode),
5688                                     op1);
5689
5690       else if (GET_CODE (op0) == AND
5691                && rtx_equal_p (XEXP (op0, 0), op1)
5692                && ! side_effects_p (op1))
5693         return simplify_gen_binary (AND, mode,
5694                                     simplify_gen_unary (NOT, mode,
5695                                                         XEXP (op0, 1), mode),
5696                                     op1);
5697
5698       /* (xor (comparison foo bar) (const_int 1)) can become the reversed
5699          comparison if STORE_FLAG_VALUE is 1.  */
5700       if (STORE_FLAG_VALUE == 1
5701           && op1 == const1_rtx
5702           && COMPARISON_P (op0)
5703           && (reversed = reversed_comparison (op0, mode)))
5704         return reversed;
5705
5706       /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5707          is (lt foo (const_int 0)), so we can perform the above
5708          simplification if STORE_FLAG_VALUE is 1.  */
5709
5710       if (STORE_FLAG_VALUE == 1
5711           && op1 == const1_rtx
5712           && GET_CODE (op0) == LSHIFTRT
5713           && GET_CODE (XEXP (op0, 1)) == CONST_INT
5714           && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5715         return gen_rtx_GE (mode, XEXP (op0, 0), const0_rtx);
5716
5717       /* (xor (comparison foo bar) (const_int sign-bit))
5718          when STORE_FLAG_VALUE is the sign bit.  */
5719       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5720           && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5721               == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5722           && op1 == const_true_rtx
5723           && COMPARISON_P (op0)
5724           && (reversed = reversed_comparison (op0, mode)))
5725         return reversed;
5726
5727       break;
5728
5729     default:
5730       gcc_unreachable ();
5731     }
5732
5733   return x;
5734 }
5735 \f
5736 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5737    operations" because they can be replaced with two more basic operations.
5738    ZERO_EXTEND is also considered "compound" because it can be replaced with
5739    an AND operation, which is simpler, though only one operation.
5740
5741    The function expand_compound_operation is called with an rtx expression
5742    and will convert it to the appropriate shifts and AND operations,
5743    simplifying at each stage.
5744
5745    The function make_compound_operation is called to convert an expression
5746    consisting of shifts and ANDs into the equivalent compound expression.
5747    It is the inverse of this function, loosely speaking.  */
5748
5749 static rtx
5750 expand_compound_operation (rtx x)
5751 {
5752   unsigned HOST_WIDE_INT pos = 0, len;
5753   int unsignedp = 0;
5754   unsigned int modewidth;
5755   rtx tem;
5756
5757   switch (GET_CODE (x))
5758     {
5759     case ZERO_EXTEND:
5760       unsignedp = 1;
5761     case SIGN_EXTEND:
5762       /* We can't necessarily use a const_int for a multiword mode;
5763          it depends on implicitly extending the value.
5764          Since we don't know the right way to extend it,
5765          we can't tell whether the implicit way is right.
5766
5767          Even for a mode that is no wider than a const_int,
5768          we can't win, because we need to sign extend one of its bits through
5769          the rest of it, and we don't know which bit.  */
5770       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5771         return x;
5772
5773       /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5774          (zero_extend:MODE FROM) or (sign_extend:MODE FROM).  It is for any MEM
5775          because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5776          reloaded. If not for that, MEM's would very rarely be safe.
5777
5778          Reject MODEs bigger than a word, because we might not be able
5779          to reference a two-register group starting with an arbitrary register
5780          (and currently gen_lowpart might crash for a SUBREG).  */
5781
5782       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5783         return x;
5784
5785       /* Reject MODEs that aren't scalar integers because turning vector
5786          or complex modes into shifts causes problems.  */
5787
5788       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5789         return x;
5790
5791       len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5792       /* If the inner object has VOIDmode (the only way this can happen
5793          is if it is an ASM_OPERANDS), we can't do anything since we don't
5794          know how much masking to do.  */
5795       if (len == 0)
5796         return x;
5797
5798       break;
5799
5800     case ZERO_EXTRACT:
5801       unsignedp = 1;
5802
5803       /* ... fall through ...  */
5804
5805     case SIGN_EXTRACT:
5806       /* If the operand is a CLOBBER, just return it.  */
5807       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5808         return XEXP (x, 0);
5809
5810       if (GET_CODE (XEXP (x, 1)) != CONST_INT
5811           || GET_CODE (XEXP (x, 2)) != CONST_INT
5812           || GET_MODE (XEXP (x, 0)) == VOIDmode)
5813         return x;
5814
5815       /* Reject MODEs that aren't scalar integers because turning vector
5816          or complex modes into shifts causes problems.  */
5817
5818       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5819         return x;
5820
5821       len = INTVAL (XEXP (x, 1));
5822       pos = INTVAL (XEXP (x, 2));
5823
5824       /* If this goes outside the object being extracted, replace the object
5825          with a (use (mem ...)) construct that only combine understands
5826          and is used only for this purpose.  */
5827       if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5828         SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5829
5830       if (BITS_BIG_ENDIAN)
5831         pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5832
5833       break;
5834
5835     default:
5836       return x;
5837     }
5838   /* Convert sign extension to zero extension, if we know that the high
5839      bit is not set, as this is easier to optimize.  It will be converted
5840      back to cheaper alternative in make_extraction.  */
5841   if (GET_CODE (x) == SIGN_EXTEND
5842       && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5843           && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
5844                 & ~(((unsigned HOST_WIDE_INT)
5845                       GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5846                      >> 1))
5847                == 0)))
5848     {
5849       rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
5850       rtx temp2 = expand_compound_operation (temp);
5851
5852       /* Make sure this is a profitable operation.  */
5853       if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
5854        return temp2;
5855       else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
5856        return temp;
5857       else
5858        return x;
5859     }
5860
5861   /* We can optimize some special cases of ZERO_EXTEND.  */
5862   if (GET_CODE (x) == ZERO_EXTEND)
5863     {
5864       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5865          know that the last value didn't have any inappropriate bits
5866          set.  */
5867       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5868           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5869           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5870           && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5871               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5872         return XEXP (XEXP (x, 0), 0);
5873
5874       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
5875       if (GET_CODE (XEXP (x, 0)) == SUBREG
5876           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5877           && subreg_lowpart_p (XEXP (x, 0))
5878           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5879           && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5880               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5881         return SUBREG_REG (XEXP (x, 0));
5882
5883       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5884          is a comparison and STORE_FLAG_VALUE permits.  This is like
5885          the first case, but it works even when GET_MODE (x) is larger
5886          than HOST_WIDE_INT.  */
5887       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5888           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5889           && COMPARISON_P (XEXP (XEXP (x, 0), 0))
5890           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5891               <= HOST_BITS_PER_WIDE_INT)
5892           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5893               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5894         return XEXP (XEXP (x, 0), 0);
5895
5896       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
5897       if (GET_CODE (XEXP (x, 0)) == SUBREG
5898           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5899           && subreg_lowpart_p (XEXP (x, 0))
5900           && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
5901           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5902               <= HOST_BITS_PER_WIDE_INT)
5903           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5904               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5905         return SUBREG_REG (XEXP (x, 0));
5906
5907     }
5908
5909   /* If we reach here, we want to return a pair of shifts.  The inner
5910      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
5911      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
5912      logical depending on the value of UNSIGNEDP.
5913
5914      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5915      converted into an AND of a shift.
5916
5917      We must check for the case where the left shift would have a negative
5918      count.  This can happen in a case like (x >> 31) & 255 on machines
5919      that can't shift by a constant.  On those machines, we would first
5920      combine the shift with the AND to produce a variable-position
5921      extraction.  Then the constant of 31 would be substituted in to produce
5922      a such a position.  */
5923
5924   modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5925   if (modewidth + len >= pos)
5926     tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5927                                 GET_MODE (x),
5928                                 simplify_shift_const (NULL_RTX, ASHIFT,
5929                                                       GET_MODE (x),
5930                                                       XEXP (x, 0),
5931                                                       modewidth - pos - len),
5932                                 modewidth - len);
5933
5934   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5935     tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5936                                   simplify_shift_const (NULL_RTX, LSHIFTRT,
5937                                                         GET_MODE (x),
5938                                                         XEXP (x, 0), pos),
5939                                   ((HOST_WIDE_INT) 1 << len) - 1);
5940   else
5941     /* Any other cases we can't handle.  */
5942     return x;
5943
5944   /* If we couldn't do this for some reason, return the original
5945      expression.  */
5946   if (GET_CODE (tem) == CLOBBER)
5947     return x;
5948
5949   return tem;
5950 }
5951 \f
5952 /* X is a SET which contains an assignment of one object into
5953    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5954    or certain SUBREGS). If possible, convert it into a series of
5955    logical operations.
5956
5957    We half-heartedly support variable positions, but do not at all
5958    support variable lengths.  */
5959
5960 static rtx
5961 expand_field_assignment (rtx x)
5962 {
5963   rtx inner;
5964   rtx pos;                      /* Always counts from low bit.  */
5965   int len;
5966   rtx mask, cleared, masked;
5967   enum machine_mode compute_mode;
5968
5969   /* Loop until we find something we can't simplify.  */
5970   while (1)
5971     {
5972       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5973           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5974         {
5975           inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5976           len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5977           pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
5978         }
5979       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5980                && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5981         {
5982           inner = XEXP (SET_DEST (x), 0);
5983           len = INTVAL (XEXP (SET_DEST (x), 1));
5984           pos = XEXP (SET_DEST (x), 2);
5985
5986           /* If the position is constant and spans the width of INNER,
5987              surround INNER  with a USE to indicate this.  */
5988           if (GET_CODE (pos) == CONST_INT
5989               && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5990             inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
5991
5992           if (BITS_BIG_ENDIAN)
5993             {
5994               if (GET_CODE (pos) == CONST_INT)
5995                 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5996                                - INTVAL (pos));
5997               else if (GET_CODE (pos) == MINUS
5998                        && GET_CODE (XEXP (pos, 1)) == CONST_INT
5999                        && (INTVAL (XEXP (pos, 1))
6000                            == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
6001                 /* If position is ADJUST - X, new position is X.  */
6002                 pos = XEXP (pos, 0);
6003               else
6004                 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6005                                            GEN_INT (GET_MODE_BITSIZE (
6006                                                     GET_MODE (inner))
6007                                                     - len),
6008                                            pos);
6009             }
6010         }
6011
6012       /* A SUBREG between two modes that occupy the same numbers of words
6013          can be done by moving the SUBREG to the source.  */
6014       else if (GET_CODE (SET_DEST (x)) == SUBREG
6015                /* We need SUBREGs to compute nonzero_bits properly.  */
6016                && nonzero_sign_valid
6017                && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6018                      + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6019                    == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6020                         + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6021         {
6022           x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6023                            gen_lowpart
6024                            (GET_MODE (SUBREG_REG (SET_DEST (x))),
6025                             SET_SRC (x)));
6026           continue;
6027         }
6028       else
6029         break;
6030
6031       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6032         inner = SUBREG_REG (inner);
6033
6034       compute_mode = GET_MODE (inner);
6035
6036       /* Don't attempt bitwise arithmetic on non scalar integer modes.  */
6037       if (! SCALAR_INT_MODE_P (compute_mode))
6038         {
6039           enum machine_mode imode;
6040
6041           /* Don't do anything for vector or complex integral types.  */
6042           if (! FLOAT_MODE_P (compute_mode))
6043             break;
6044
6045           /* Try to find an integral mode to pun with.  */
6046           imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6047           if (imode == BLKmode)
6048             break;
6049
6050           compute_mode = imode;
6051           inner = gen_lowpart (imode, inner);
6052         }
6053
6054       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
6055       if (len >= HOST_BITS_PER_WIDE_INT)
6056         break;
6057
6058       /* Now compute the equivalent expression.  Make a copy of INNER
6059          for the SET_DEST in case it is a MEM into which we will substitute;
6060          we don't want shared RTL in that case.  */
6061       mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
6062       cleared = simplify_gen_binary (AND, compute_mode,
6063                                      simplify_gen_unary (NOT, compute_mode,
6064                                        simplify_gen_binary (ASHIFT,
6065                                                             compute_mode,
6066                                                             mask, pos),
6067                                        compute_mode),
6068                                      inner);
6069       masked = simplify_gen_binary (ASHIFT, compute_mode,
6070                                     simplify_gen_binary (
6071                                       AND, compute_mode,
6072                                       gen_lowpart (compute_mode, SET_SRC (x)),
6073                                       mask),
6074                                     pos);
6075
6076       x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
6077                        simplify_gen_binary (IOR, compute_mode,
6078                                             cleared, masked));
6079     }
6080
6081   return x;
6082 }
6083 \f
6084 /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
6085    it is an RTX that represents a variable starting position; otherwise,
6086    POS is the (constant) starting bit position (counted from the LSB).
6087
6088    INNER may be a USE.  This will occur when we started with a bitfield
6089    that went outside the boundary of the object in memory, which is
6090    allowed on most machines.  To isolate this case, we produce a USE
6091    whose mode is wide enough and surround the MEM with it.  The only
6092    code that understands the USE is this routine.  If it is not removed,
6093    it will cause the resulting insn not to match.
6094
6095    UNSIGNEDP is nonzero for an unsigned reference and zero for a
6096    signed reference.
6097
6098    IN_DEST is nonzero if this is a reference in the destination of a
6099    SET.  This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If nonzero,
6100    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6101    be used.
6102
6103    IN_COMPARE is nonzero if we are in a COMPARE.  This means that a
6104    ZERO_EXTRACT should be built even for bits starting at bit 0.
6105
6106    MODE is the desired mode of the result (if IN_DEST == 0).
6107
6108    The result is an RTX for the extraction or NULL_RTX if the target
6109    can't handle it.  */
6110
6111 static rtx
6112 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6113                  rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6114                  int in_dest, int in_compare)
6115 {
6116   /* This mode describes the size of the storage area
6117      to fetch the overall value from.  Within that, we
6118      ignore the POS lowest bits, etc.  */
6119   enum machine_mode is_mode = GET_MODE (inner);
6120   enum machine_mode inner_mode;
6121   enum machine_mode wanted_inner_mode = byte_mode;
6122   enum machine_mode wanted_inner_reg_mode = word_mode;
6123   enum machine_mode pos_mode = word_mode;
6124   enum machine_mode extraction_mode = word_mode;
6125   enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6126   int spans_byte = 0;
6127   rtx new = 0;
6128   rtx orig_pos_rtx = pos_rtx;
6129   HOST_WIDE_INT orig_pos;
6130
6131   /* Get some information about INNER and get the innermost object.  */
6132   if (GET_CODE (inner) == USE)
6133     /* (use:SI (mem:QI foo)) stands for (mem:SI foo).  */
6134     /* We don't need to adjust the position because we set up the USE
6135        to pretend that it was a full-word object.  */
6136     spans_byte = 1, inner = XEXP (inner, 0);
6137   else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6138     {
6139       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6140          consider just the QI as the memory to extract from.
6141          The subreg adds or removes high bits; its mode is
6142          irrelevant to the meaning of this extraction,
6143          since POS and LEN count from the lsb.  */
6144       if (MEM_P (SUBREG_REG (inner)))
6145         is_mode = GET_MODE (SUBREG_REG (inner));
6146       inner = SUBREG_REG (inner);
6147     }
6148   else if (GET_CODE (inner) == ASHIFT
6149            && GET_CODE (XEXP (inner, 1)) == CONST_INT
6150            && pos_rtx == 0 && pos == 0
6151            && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6152     {
6153       /* We're extracting the least significant bits of an rtx
6154          (ashift X (const_int C)), where LEN > C.  Extract the
6155          least significant (LEN - C) bits of X, giving an rtx
6156          whose mode is MODE, then shift it left C times.  */
6157       new = make_extraction (mode, XEXP (inner, 0),
6158                              0, 0, len - INTVAL (XEXP (inner, 1)),
6159                              unsignedp, in_dest, in_compare);
6160       if (new != 0)
6161         return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
6162     }
6163
6164   inner_mode = GET_MODE (inner);
6165
6166   if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
6167     pos = INTVAL (pos_rtx), pos_rtx = 0;
6168
6169   /* See if this can be done without an extraction.  We never can if the
6170      width of the field is not the same as that of some integer mode. For
6171      registers, we can only avoid the extraction if the position is at the
6172      low-order bit and this is either not in the destination or we have the
6173      appropriate STRICT_LOW_PART operation available.
6174
6175      For MEM, we can avoid an extract if the field starts on an appropriate
6176      boundary and we can change the mode of the memory reference.  However,
6177      we cannot directly access the MEM if we have a USE and the underlying
6178      MEM is not TMODE.  This combination means that MEM was being used in a
6179      context where bits outside its mode were being referenced; that is only
6180      valid in bit-field insns.  */
6181
6182   if (tmode != BLKmode
6183       && ! (spans_byte && inner_mode != tmode)
6184       && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6185            && !MEM_P (inner)
6186            && (! in_dest
6187                || (REG_P (inner)
6188                    && have_insn_for (STRICT_LOW_PART, tmode))))
6189           || (MEM_P (inner) && pos_rtx == 0
6190               && (pos
6191                   % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6192                      : BITS_PER_UNIT)) == 0
6193               /* We can't do this if we are widening INNER_MODE (it
6194                  may not be aligned, for one thing).  */
6195               && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6196               && (inner_mode == tmode
6197                   || (! mode_dependent_address_p (XEXP (inner, 0))
6198                       && ! MEM_VOLATILE_P (inner))))))
6199     {
6200       /* If INNER is a MEM, make a new MEM that encompasses just the desired
6201          field.  If the original and current mode are the same, we need not
6202          adjust the offset.  Otherwise, we do if bytes big endian.
6203
6204          If INNER is not a MEM, get a piece consisting of just the field
6205          of interest (in this case POS % BITS_PER_WORD must be 0).  */
6206
6207       if (MEM_P (inner))
6208         {
6209           HOST_WIDE_INT offset;
6210
6211           /* POS counts from lsb, but make OFFSET count in memory order.  */
6212           if (BYTES_BIG_ENDIAN)
6213             offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6214           else
6215             offset = pos / BITS_PER_UNIT;
6216
6217           new = adjust_address_nv (inner, tmode, offset);
6218         }
6219       else if (REG_P (inner))
6220         {
6221           if (tmode != inner_mode)
6222             {
6223               /* We can't call gen_lowpart in a DEST since we
6224                  always want a SUBREG (see below) and it would sometimes
6225                  return a new hard register.  */
6226               if (pos || in_dest)
6227                 {
6228                   HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6229
6230                   if (WORDS_BIG_ENDIAN
6231                       && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6232                     final_word = ((GET_MODE_SIZE (inner_mode)
6233                                    - GET_MODE_SIZE (tmode))
6234                                   / UNITS_PER_WORD) - final_word;
6235
6236                   final_word *= UNITS_PER_WORD;
6237                   if (BYTES_BIG_ENDIAN &&
6238                       GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6239                     final_word += (GET_MODE_SIZE (inner_mode)
6240                                    - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6241
6242                   /* Avoid creating invalid subregs, for example when
6243                      simplifying (x>>32)&255.  */
6244                   if (final_word >= GET_MODE_SIZE (inner_mode))
6245                     return NULL_RTX;
6246
6247                   new = gen_rtx_SUBREG (tmode, inner, final_word);
6248                 }
6249               else
6250                 new = gen_lowpart (tmode, inner);
6251             }
6252           else
6253             new = inner;
6254         }
6255       else
6256         new = force_to_mode (inner, tmode,
6257                              len >= HOST_BITS_PER_WIDE_INT
6258                              ? ~(unsigned HOST_WIDE_INT) 0
6259                              : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6260                              NULL_RTX, 0);
6261
6262       /* If this extraction is going into the destination of a SET,
6263          make a STRICT_LOW_PART unless we made a MEM.  */
6264
6265       if (in_dest)
6266         return (MEM_P (new) ? new
6267                 : (GET_CODE (new) != SUBREG
6268                    ? gen_rtx_CLOBBER (tmode, const0_rtx)
6269                    : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
6270
6271       if (mode == tmode)
6272         return new;
6273
6274       if (GET_CODE (new) == CONST_INT)
6275         return gen_int_mode (INTVAL (new), mode);
6276
6277       /* If we know that no extraneous bits are set, and that the high
6278          bit is not set, convert the extraction to the cheaper of
6279          sign and zero extension, that are equivalent in these cases.  */
6280       if (flag_expensive_optimizations
6281           && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6282               && ((nonzero_bits (new, tmode)
6283                    & ~(((unsigned HOST_WIDE_INT)
6284                         GET_MODE_MASK (tmode))
6285                        >> 1))
6286                   == 0)))
6287         {
6288           rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6289           rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6290
6291           /* Prefer ZERO_EXTENSION, since it gives more information to
6292              backends.  */
6293           if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6294             return temp;
6295           return temp1;
6296         }
6297
6298       /* Otherwise, sign- or zero-extend unless we already are in the
6299          proper mode.  */
6300
6301       return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6302                              mode, new));
6303     }
6304
6305   /* Unless this is a COMPARE or we have a funny memory reference,
6306      don't do anything with zero-extending field extracts starting at
6307      the low-order bit since they are simple AND operations.  */
6308   if (pos_rtx == 0 && pos == 0 && ! in_dest
6309       && ! in_compare && ! spans_byte && unsignedp)
6310     return 0;
6311
6312   /* Unless we are allowed to span bytes or INNER is not MEM, reject this if
6313      we would be spanning bytes or if the position is not a constant and the
6314      length is not 1.  In all other cases, we would only be going outside
6315      our object in cases when an original shift would have been
6316      undefined.  */
6317   if (! spans_byte && MEM_P (inner)
6318       && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6319           || (pos_rtx != 0 && len != 1)))
6320     return 0;
6321
6322   /* Get the mode to use should INNER not be a MEM, the mode for the position,
6323      and the mode for the result.  */
6324   if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6325     {
6326       wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6327       pos_mode = mode_for_extraction (EP_insv, 2);
6328       extraction_mode = mode_for_extraction (EP_insv, 3);
6329     }
6330
6331   if (! in_dest && unsignedp
6332       && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6333     {
6334       wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6335       pos_mode = mode_for_extraction (EP_extzv, 3);
6336       extraction_mode = mode_for_extraction (EP_extzv, 0);
6337     }
6338
6339   if (! in_dest && ! unsignedp
6340       && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6341     {
6342       wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6343       pos_mode = mode_for_extraction (EP_extv, 3);
6344       extraction_mode = mode_for_extraction (EP_extv, 0);
6345     }
6346
6347   /* Never narrow an object, since that might not be safe.  */
6348
6349   if (mode != VOIDmode
6350       && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6351     extraction_mode = mode;
6352
6353   if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6354       && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6355     pos_mode = GET_MODE (pos_rtx);
6356
6357   /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
6358      if we have to change the mode of memory and cannot, the desired mode is
6359      EXTRACTION_MODE.  */
6360   if (!MEM_P (inner))
6361     wanted_inner_mode = wanted_inner_reg_mode;
6362   else if (inner_mode != wanted_inner_mode
6363            && (mode_dependent_address_p (XEXP (inner, 0))
6364                || MEM_VOLATILE_P (inner)))
6365     wanted_inner_mode = extraction_mode;
6366
6367   orig_pos = pos;
6368
6369   if (BITS_BIG_ENDIAN)
6370     {
6371       /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6372          BITS_BIG_ENDIAN style.  If position is constant, compute new
6373          position.  Otherwise, build subtraction.
6374          Note that POS is relative to the mode of the original argument.
6375          If it's a MEM we need to recompute POS relative to that.
6376          However, if we're extracting from (or inserting into) a register,
6377          we want to recompute POS relative to wanted_inner_mode.  */
6378       int width = (MEM_P (inner)
6379                    ? GET_MODE_BITSIZE (is_mode)
6380                    : GET_MODE_BITSIZE (wanted_inner_mode));
6381
6382       if (pos_rtx == 0)
6383         pos = width - len - pos;
6384       else
6385         pos_rtx
6386           = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6387       /* POS may be less than 0 now, but we check for that below.
6388          Note that it can only be less than 0 if !MEM_P (inner).  */
6389     }
6390
6391   /* If INNER has a wider mode, make it smaller.  If this is a constant
6392      extract, try to adjust the byte to point to the byte containing
6393      the value.  */
6394   if (wanted_inner_mode != VOIDmode
6395       && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6396       && ((MEM_P (inner)
6397            && (inner_mode == wanted_inner_mode
6398                || (! mode_dependent_address_p (XEXP (inner, 0))
6399                    && ! MEM_VOLATILE_P (inner))))))
6400     {
6401       int offset = 0;
6402
6403       /* The computations below will be correct if the machine is big
6404          endian in both bits and bytes or little endian in bits and bytes.
6405          If it is mixed, we must adjust.  */
6406
6407       /* If bytes are big endian and we had a paradoxical SUBREG, we must
6408          adjust OFFSET to compensate.  */
6409       if (BYTES_BIG_ENDIAN
6410           && ! spans_byte
6411           && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6412         offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6413
6414       /* If this is a constant position, we can move to the desired byte.  */
6415       if (pos_rtx == 0)
6416         {
6417           offset += pos / BITS_PER_UNIT;
6418           pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6419         }
6420
6421       if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6422           && ! spans_byte
6423           && is_mode != wanted_inner_mode)
6424         offset = (GET_MODE_SIZE (is_mode)
6425                   - GET_MODE_SIZE (wanted_inner_mode) - offset);
6426
6427       if (offset != 0 || inner_mode != wanted_inner_mode)
6428         inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6429     }
6430
6431   /* If INNER is not memory, we can always get it into the proper mode.  If we
6432      are changing its mode, POS must be a constant and smaller than the size
6433      of the new mode.  */
6434   else if (!MEM_P (inner))
6435     {
6436       if (GET_MODE (inner) != wanted_inner_mode
6437           && (pos_rtx != 0
6438               || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6439         return 0;
6440
6441       inner = force_to_mode (inner, wanted_inner_mode,
6442                              pos_rtx
6443                              || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6444                              ? ~(unsigned HOST_WIDE_INT) 0
6445                              : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6446                                 << orig_pos),
6447                              NULL_RTX, 0);
6448     }
6449
6450   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
6451      have to zero extend.  Otherwise, we can just use a SUBREG.  */
6452   if (pos_rtx != 0
6453       && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6454     {
6455       rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6456
6457       /* If we know that no extraneous bits are set, and that the high
6458          bit is not set, convert extraction to cheaper one - either
6459          SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6460          cases.  */
6461       if (flag_expensive_optimizations
6462           && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6463               && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6464                    & ~(((unsigned HOST_WIDE_INT)
6465                         GET_MODE_MASK (GET_MODE (pos_rtx)))
6466                        >> 1))
6467                   == 0)))
6468         {
6469           rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6470
6471           /* Prefer ZERO_EXTENSION, since it gives more information to
6472              backends.  */
6473           if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6474             temp = temp1;
6475         }
6476       pos_rtx = temp;
6477     }
6478   else if (pos_rtx != 0
6479            && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6480     pos_rtx = gen_lowpart (pos_mode, pos_rtx);
6481
6482   /* Make POS_RTX unless we already have it and it is correct.  If we don't
6483      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6484      be a CONST_INT.  */
6485   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6486     pos_rtx = orig_pos_rtx;
6487
6488   else if (pos_rtx == 0)
6489     pos_rtx = GEN_INT (pos);
6490
6491   /* Make the required operation.  See if we can use existing rtx.  */
6492   new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6493                          extraction_mode, inner, GEN_INT (len), pos_rtx);
6494   if (! in_dest)
6495     new = gen_lowpart (mode, new);
6496
6497   return new;
6498 }
6499 \f
6500 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6501    with any other operations in X.  Return X without that shift if so.  */
6502
6503 static rtx
6504 extract_left_shift (rtx x, int count)
6505 {
6506   enum rtx_code code = GET_CODE (x);
6507   enum machine_mode mode = GET_MODE (x);
6508   rtx tem;
6509
6510   switch (code)
6511     {
6512     case ASHIFT:
6513       /* This is the shift itself.  If it is wide enough, we will return
6514          either the value being shifted if the shift count is equal to
6515          COUNT or a shift for the difference.  */
6516       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6517           && INTVAL (XEXP (x, 1)) >= count)
6518         return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6519                                      INTVAL (XEXP (x, 1)) - count);
6520       break;
6521
6522     case NEG:  case NOT:
6523       if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6524         return simplify_gen_unary (code, mode, tem, mode);
6525
6526       break;
6527
6528     case PLUS:  case IOR:  case XOR:  case AND:
6529       /* If we can safely shift this constant and we find the inner shift,
6530          make a new operation.  */
6531       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6532           && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6533           && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6534         return simplify_gen_binary (code, mode, tem,
6535                                     GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6536
6537       break;
6538
6539     default:
6540       break;
6541     }
6542
6543   return 0;
6544 }
6545 \f
6546 /* Look at the expression rooted at X.  Look for expressions
6547    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6548    Form these expressions.
6549
6550    Return the new rtx, usually just X.
6551
6552    Also, for machines like the VAX that don't have logical shift insns,
6553    try to convert logical to arithmetic shift operations in cases where
6554    they are equivalent.  This undoes the canonicalizations to logical
6555    shifts done elsewhere.
6556
6557    We try, as much as possible, to re-use rtl expressions to save memory.
6558
6559    IN_CODE says what kind of expression we are processing.  Normally, it is
6560    SET.  In a memory address (inside a MEM, PLUS or minus, the latter two
6561    being kludges), it is MEM.  When processing the arguments of a comparison
6562    or a COMPARE against zero, it is COMPARE.  */
6563
6564 static rtx
6565 make_compound_operation (rtx x, enum rtx_code in_code)
6566 {
6567   enum rtx_code code = GET_CODE (x);
6568   enum machine_mode mode = GET_MODE (x);
6569   int mode_width = GET_MODE_BITSIZE (mode);
6570   rtx rhs, lhs;
6571   enum rtx_code next_code;
6572   int i;
6573   rtx new = 0;
6574   rtx tem;
6575   const char *fmt;
6576
6577   /* Select the code to be used in recursive calls.  Once we are inside an
6578      address, we stay there.  If we have a comparison, set to COMPARE,
6579      but once inside, go back to our default of SET.  */
6580
6581   next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6582                : ((code == COMPARE || COMPARISON_P (x))
6583                   && XEXP (x, 1) == const0_rtx) ? COMPARE
6584                : in_code == COMPARE ? SET : in_code);
6585
6586   /* Process depending on the code of this operation.  If NEW is set
6587      nonzero, it will be returned.  */
6588
6589   switch (code)
6590     {
6591     case ASHIFT:
6592       /* Convert shifts by constants into multiplications if inside
6593          an address.  */
6594       if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6595           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6596           && INTVAL (XEXP (x, 1)) >= 0)
6597         {
6598           new = make_compound_operation (XEXP (x, 0), next_code);
6599           new = gen_rtx_MULT (mode, new,
6600                               GEN_INT ((HOST_WIDE_INT) 1
6601                                        << INTVAL (XEXP (x, 1))));
6602         }
6603       break;
6604
6605     case AND:
6606       /* If the second operand is not a constant, we can't do anything
6607          with it.  */
6608       if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6609         break;
6610
6611       /* If the constant is a power of two minus one and the first operand
6612          is a logical right shift, make an extraction.  */
6613       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6614           && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6615         {
6616           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6617           new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6618                                  0, in_code == COMPARE);
6619         }
6620
6621       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
6622       else if (GET_CODE (XEXP (x, 0)) == SUBREG
6623                && subreg_lowpart_p (XEXP (x, 0))
6624                && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6625                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6626         {
6627           new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6628                                          next_code);
6629           new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6630                                  XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6631                                  0, in_code == COMPARE);
6632         }
6633       /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)).  */
6634       else if ((GET_CODE (XEXP (x, 0)) == XOR
6635                 || GET_CODE (XEXP (x, 0)) == IOR)
6636                && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6637                && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6638                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6639         {
6640           /* Apply the distributive law, and then try to make extractions.  */
6641           new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6642                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6643                                              XEXP (x, 1)),
6644                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6645                                              XEXP (x, 1)));
6646           new = make_compound_operation (new, in_code);
6647         }
6648
6649       /* If we are have (and (rotate X C) M) and C is larger than the number
6650          of bits in M, this is an extraction.  */
6651
6652       else if (GET_CODE (XEXP (x, 0)) == ROTATE
6653                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6654                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6655                && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6656         {
6657           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6658           new = make_extraction (mode, new,
6659                                  (GET_MODE_BITSIZE (mode)
6660                                   - INTVAL (XEXP (XEXP (x, 0), 1))),
6661                                  NULL_RTX, i, 1, 0, in_code == COMPARE);
6662         }
6663
6664       /* On machines without logical shifts, if the operand of the AND is
6665          a logical shift and our mask turns off all the propagated sign
6666          bits, we can replace the logical shift with an arithmetic shift.  */
6667       else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6668                && !have_insn_for (LSHIFTRT, mode)
6669                && have_insn_for (ASHIFTRT, mode)
6670                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6671                && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6672                && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6673                && mode_width <= HOST_BITS_PER_WIDE_INT)
6674         {
6675           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6676
6677           mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6678           if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6679             SUBST (XEXP (x, 0),
6680                    gen_rtx_ASHIFTRT (mode,
6681                                      make_compound_operation
6682                                      (XEXP (XEXP (x, 0), 0), next_code),
6683                                      XEXP (XEXP (x, 0), 1)));
6684         }
6685
6686       /* If the constant is one less than a power of two, this might be
6687          representable by an extraction even if no shift is present.
6688          If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6689          we are in a COMPARE.  */
6690       else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6691         new = make_extraction (mode,
6692                                make_compound_operation (XEXP (x, 0),
6693                                                         next_code),
6694                                0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6695
6696       /* If we are in a comparison and this is an AND with a power of two,
6697          convert this into the appropriate bit extract.  */
6698       else if (in_code == COMPARE
6699                && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6700         new = make_extraction (mode,
6701                                make_compound_operation (XEXP (x, 0),
6702                                                         next_code),
6703                                i, NULL_RTX, 1, 1, 0, 1);
6704
6705       break;
6706
6707     case LSHIFTRT:
6708       /* If the sign bit is known to be zero, replace this with an
6709          arithmetic shift.  */
6710       if (have_insn_for (ASHIFTRT, mode)
6711           && ! have_insn_for (LSHIFTRT, mode)
6712           && mode_width <= HOST_BITS_PER_WIDE_INT
6713           && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6714         {
6715           new = gen_rtx_ASHIFTRT (mode,
6716                                   make_compound_operation (XEXP (x, 0),
6717                                                            next_code),
6718                                   XEXP (x, 1));
6719           break;
6720         }
6721
6722       /* ... fall through ...  */
6723
6724     case ASHIFTRT:
6725       lhs = XEXP (x, 0);
6726       rhs = XEXP (x, 1);
6727
6728       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6729          this is a SIGN_EXTRACT.  */
6730       if (GET_CODE (rhs) == CONST_INT
6731           && GET_CODE (lhs) == ASHIFT
6732           && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6733           && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6734         {
6735           new = make_compound_operation (XEXP (lhs, 0), next_code);
6736           new = make_extraction (mode, new,
6737                                  INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6738                                  NULL_RTX, mode_width - INTVAL (rhs),
6739                                  code == LSHIFTRT, 0, in_code == COMPARE);
6740           break;
6741         }
6742
6743       /* See if we have operations between an ASHIFTRT and an ASHIFT.
6744          If so, try to merge the shifts into a SIGN_EXTEND.  We could
6745          also do this for some cases of SIGN_EXTRACT, but it doesn't
6746          seem worth the effort; the case checked for occurs on Alpha.  */
6747
6748       if (!OBJECT_P (lhs)
6749           && ! (GET_CODE (lhs) == SUBREG
6750                 && (OBJECT_P (SUBREG_REG (lhs))))
6751           && GET_CODE (rhs) == CONST_INT
6752           && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6753           && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6754         new = make_extraction (mode, make_compound_operation (new, next_code),
6755                                0, NULL_RTX, mode_width - INTVAL (rhs),
6756                                code == LSHIFTRT, 0, in_code == COMPARE);
6757
6758       break;
6759
6760     case SUBREG:
6761       /* Call ourselves recursively on the inner expression.  If we are
6762          narrowing the object and it has a different RTL code from
6763          what it originally did, do this SUBREG as a force_to_mode.  */
6764
6765       tem = make_compound_operation (SUBREG_REG (x), in_code);
6766       if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6767           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6768           && subreg_lowpart_p (x))
6769         {
6770           rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
6771                                      NULL_RTX, 0);
6772
6773           /* If we have something other than a SUBREG, we might have
6774              done an expansion, so rerun ourselves.  */
6775           if (GET_CODE (newer) != SUBREG)
6776             newer = make_compound_operation (newer, in_code);
6777
6778           return newer;
6779         }
6780
6781       /* If this is a paradoxical subreg, and the new code is a sign or
6782          zero extension, omit the subreg and widen the extension.  If it
6783          is a regular subreg, we can still get rid of the subreg by not
6784          widening so much, or in fact removing the extension entirely.  */
6785       if ((GET_CODE (tem) == SIGN_EXTEND
6786            || GET_CODE (tem) == ZERO_EXTEND)
6787           && subreg_lowpart_p (x))
6788         {
6789           if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (tem))
6790               || (GET_MODE_SIZE (mode) >
6791                   GET_MODE_SIZE (GET_MODE (XEXP (tem, 0)))))
6792             {
6793               if (! SCALAR_INT_MODE_P (mode))
6794                 break;
6795               tem = gen_rtx_fmt_e (GET_CODE (tem), mode, XEXP (tem, 0));
6796             }
6797           else
6798             tem = gen_lowpart (mode, XEXP (tem, 0));
6799           return tem;
6800         }
6801       break;
6802
6803     default:
6804       break;
6805     }
6806
6807   if (new)
6808     {
6809       x = gen_lowpart (mode, new);
6810       code = GET_CODE (x);
6811     }
6812
6813   /* Now recursively process each operand of this operation.  */
6814   fmt = GET_RTX_FORMAT (code);
6815   for (i = 0; i < GET_RTX_LENGTH (code); i++)
6816     if (fmt[i] == 'e')
6817       {
6818         new = make_compound_operation (XEXP (x, i), next_code);
6819         SUBST (XEXP (x, i), new);
6820       }
6821
6822   return x;
6823 }
6824 \f
6825 /* Given M see if it is a value that would select a field of bits
6826    within an item, but not the entire word.  Return -1 if not.
6827    Otherwise, return the starting position of the field, where 0 is the
6828    low-order bit.
6829
6830    *PLEN is set to the length of the field.  */
6831
6832 static int
6833 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
6834 {
6835   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
6836   int pos = exact_log2 (m & -m);
6837   int len = 0;
6838
6839   if (pos >= 0)
6840     /* Now shift off the low-order zero bits and see if we have a
6841        power of two minus 1.  */
6842     len = exact_log2 ((m >> pos) + 1);
6843
6844   if (len <= 0)
6845     pos = -1;
6846
6847   *plen = len;
6848   return pos;
6849 }
6850 \f
6851 /* See if X can be simplified knowing that we will only refer to it in
6852    MODE and will only refer to those bits that are nonzero in MASK.
6853    If other bits are being computed or if masking operations are done
6854    that select a superset of the bits in MASK, they can sometimes be
6855    ignored.
6856
6857    Return a possibly simplified expression, but always convert X to
6858    MODE.  If X is a CONST_INT, AND the CONST_INT with MASK.
6859
6860    Also, if REG is nonzero and X is a register equal in value to REG,
6861    replace X with REG.
6862
6863    If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6864    are all off in X.  This is used when X will be complemented, by either
6865    NOT, NEG, or XOR.  */
6866
6867 static rtx
6868 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
6869                rtx reg, int just_select)
6870 {
6871   enum rtx_code code = GET_CODE (x);
6872   int next_select = just_select || code == XOR || code == NOT || code == NEG;
6873   enum machine_mode op_mode;
6874   unsigned HOST_WIDE_INT fuller_mask, nonzero;
6875   rtx op0, op1, temp;
6876
6877   /* If this is a CALL or ASM_OPERANDS, don't do anything.  Some of the
6878      code below will do the wrong thing since the mode of such an
6879      expression is VOIDmode.
6880
6881      Also do nothing if X is a CLOBBER; this can happen if X was
6882      the return value from a call to gen_lowpart.  */
6883   if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6884     return x;
6885
6886   /* We want to perform the operation is its present mode unless we know
6887      that the operation is valid in MODE, in which case we do the operation
6888      in MODE.  */
6889   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6890               && have_insn_for (code, mode))
6891              ? mode : GET_MODE (x));
6892
6893   /* It is not valid to do a right-shift in a narrower mode
6894      than the one it came in with.  */
6895   if ((code == LSHIFTRT || code == ASHIFTRT)
6896       && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6897     op_mode = GET_MODE (x);
6898
6899   /* Truncate MASK to fit OP_MODE.  */
6900   if (op_mode)
6901     mask &= GET_MODE_MASK (op_mode);
6902
6903   /* When we have an arithmetic operation, or a shift whose count we
6904      do not know, we need to assume that all bits up to the highest-order
6905      bit in MASK will be needed.  This is how we form such a mask.  */
6906   if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
6907     fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
6908   else
6909     fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
6910                    - 1);
6911
6912   /* Determine what bits of X are guaranteed to be (non)zero.  */
6913   nonzero = nonzero_bits (x, mode);
6914
6915   /* If none of the bits in X are needed, return a zero.  */
6916   if (! just_select && (nonzero & mask) == 0)
6917     x = const0_rtx;
6918
6919   /* If X is a CONST_INT, return a new one.  Do this here since the
6920      test below will fail.  */
6921   if (GET_CODE (x) == CONST_INT)
6922     {
6923       if (SCALAR_INT_MODE_P (mode))
6924         return gen_int_mode (INTVAL (x) & mask, mode);
6925       else
6926         {
6927           x = GEN_INT (INTVAL (x) & mask);
6928           return gen_lowpart_common (mode, x);
6929         }
6930     }
6931
6932   /* If X is narrower than MODE and we want all the bits in X's mode, just
6933      get X in the proper mode.  */
6934   if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6935       && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
6936     return gen_lowpart (mode, x);
6937
6938   switch (code)
6939     {
6940     case CLOBBER:
6941       /* If X is a (clobber (const_int)), return it since we know we are
6942          generating something that won't match.  */
6943       return x;
6944
6945     case USE:
6946       /* X is a (use (mem ..)) that was made from a bit-field extraction that
6947          spanned the boundary of the MEM.  If we are now masking so it is
6948          within that boundary, we don't need the USE any more.  */
6949       if (! BITS_BIG_ENDIAN
6950           && (mask & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6951         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6952       break;
6953
6954     case SIGN_EXTEND:
6955     case ZERO_EXTEND:
6956     case ZERO_EXTRACT:
6957     case SIGN_EXTRACT:
6958       x = expand_compound_operation (x);
6959       if (GET_CODE (x) != code)
6960         return force_to_mode (x, mode, mask, reg, next_select);
6961       break;
6962
6963     case REG:
6964       if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6965                        || rtx_equal_p (reg, get_last_value (x))))
6966         x = reg;
6967       break;
6968
6969     case SUBREG:
6970       if (subreg_lowpart_p (x)
6971           /* We can ignore the effect of this SUBREG if it narrows the mode or
6972              if the constant masks to zero all the bits the mode doesn't
6973              have.  */
6974           && ((GET_MODE_SIZE (GET_MODE (x))
6975                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6976               || (0 == (mask
6977                         & GET_MODE_MASK (GET_MODE (x))
6978                         & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6979         return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6980       break;
6981
6982     case AND:
6983       /* If this is an AND with a constant, convert it into an AND
6984          whose constant is the AND of that constant with MASK.  If it
6985          remains an AND of MASK, delete it since it is redundant.  */
6986
6987       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6988         {
6989           x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6990                                       mask & INTVAL (XEXP (x, 1)));
6991
6992           /* If X is still an AND, see if it is an AND with a mask that
6993              is just some low-order bits.  If so, and it is MASK, we don't
6994              need it.  */
6995
6996           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6997               && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
6998                   == mask))
6999             x = XEXP (x, 0);
7000
7001           /* If it remains an AND, try making another AND with the bits
7002              in the mode mask that aren't in MASK turned on.  If the
7003              constant in the AND is wide enough, this might make a
7004              cheaper constant.  */
7005
7006           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
7007               && GET_MODE_MASK (GET_MODE (x)) != mask
7008               && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
7009             {
7010               HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
7011                                     | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
7012               int width = GET_MODE_BITSIZE (GET_MODE (x));
7013               rtx y;
7014
7015               /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
7016                  number, sign extend it.  */
7017               if (width > 0 && width < HOST_BITS_PER_WIDE_INT
7018                   && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7019                 cval |= (HOST_WIDE_INT) -1 << width;
7020
7021               y = simplify_gen_binary (AND, GET_MODE (x),
7022                                        XEXP (x, 0), GEN_INT (cval));
7023               if (rtx_cost (y, SET) < rtx_cost (x, SET))
7024                 x = y;
7025             }
7026
7027           break;
7028         }
7029
7030       goto binop;
7031
7032     case PLUS:
7033       /* In (and (plus FOO C1) M), if M is a mask that just turns off
7034          low-order bits (as in an alignment operation) and FOO is already
7035          aligned to that boundary, mask C1 to that boundary as well.
7036          This may eliminate that PLUS and, later, the AND.  */
7037
7038       {
7039         unsigned int width = GET_MODE_BITSIZE (mode);
7040         unsigned HOST_WIDE_INT smask = mask;
7041
7042         /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
7043            number, sign extend it.  */
7044
7045         if (width < HOST_BITS_PER_WIDE_INT
7046             && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7047           smask |= (HOST_WIDE_INT) -1 << width;
7048
7049         if (GET_CODE (XEXP (x, 1)) == CONST_INT
7050             && exact_log2 (- smask) >= 0
7051             && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
7052             && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
7053           return force_to_mode (plus_constant (XEXP (x, 0),
7054                                                (INTVAL (XEXP (x, 1)) & smask)),
7055                                 mode, smask, reg, next_select);
7056       }
7057
7058       /* ... fall through ...  */
7059
7060     case MULT:
7061       /* For PLUS, MINUS and MULT, we need any bits less significant than the
7062          most significant bit in MASK since carries from those bits will
7063          affect the bits we are interested in.  */
7064       mask = fuller_mask;
7065       goto binop;
7066
7067     case MINUS:
7068       /* If X is (minus C Y) where C's least set bit is larger than any bit
7069          in the mask, then we may replace with (neg Y).  */
7070       if (GET_CODE (XEXP (x, 0)) == CONST_INT
7071           && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
7072                                         & -INTVAL (XEXP (x, 0))))
7073               > mask))
7074         {
7075           x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
7076                                   GET_MODE (x));
7077           return force_to_mode (x, mode, mask, reg, next_select);
7078         }
7079
7080       /* Similarly, if C contains every bit in the fuller_mask, then we may
7081          replace with (not Y).  */
7082       if (GET_CODE (XEXP (x, 0)) == CONST_INT
7083           && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
7084               == INTVAL (XEXP (x, 0))))
7085         {
7086           x = simplify_gen_unary (NOT, GET_MODE (x),
7087                                   XEXP (x, 1), GET_MODE (x));
7088           return force_to_mode (x, mode, mask, reg, next_select);
7089         }
7090
7091       mask = fuller_mask;
7092       goto binop;
7093
7094     case IOR:
7095     case XOR:
7096       /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
7097          LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
7098          operation which may be a bitfield extraction.  Ensure that the
7099          constant we form is not wider than the mode of X.  */
7100
7101       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7102           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7103           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7104           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7105           && GET_CODE (XEXP (x, 1)) == CONST_INT
7106           && ((INTVAL (XEXP (XEXP (x, 0), 1))
7107                + floor_log2 (INTVAL (XEXP (x, 1))))
7108               < GET_MODE_BITSIZE (GET_MODE (x)))
7109           && (INTVAL (XEXP (x, 1))
7110               & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
7111         {
7112           temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
7113                           << INTVAL (XEXP (XEXP (x, 0), 1)));
7114           temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
7115                                       XEXP (XEXP (x, 0), 0), temp);
7116           x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
7117                                    XEXP (XEXP (x, 0), 1));
7118           return force_to_mode (x, mode, mask, reg, next_select);
7119         }
7120
7121     binop:
7122       /* For most binary operations, just propagate into the operation and
7123          change the mode if we have an operation of that mode.  */
7124
7125       op0 = gen_lowpart (op_mode,
7126                          force_to_mode (XEXP (x, 0), mode, mask,
7127                                         reg, next_select));
7128       op1 = gen_lowpart (op_mode,
7129                          force_to_mode (XEXP (x, 1), mode, mask,
7130                                         reg, next_select));
7131
7132       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7133         x = simplify_gen_binary (code, op_mode, op0, op1);
7134       break;
7135
7136     case ASHIFT:
7137       /* For left shifts, do the same, but just for the first operand.
7138          However, we cannot do anything with shifts where we cannot
7139          guarantee that the counts are smaller than the size of the mode
7140          because such a count will have a different meaning in a
7141          wider mode.  */
7142
7143       if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
7144              && INTVAL (XEXP (x, 1)) >= 0
7145              && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7146           && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7147                 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7148                     < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7149         break;
7150
7151       /* If the shift count is a constant and we can do arithmetic in
7152          the mode of the shift, refine which bits we need.  Otherwise, use the
7153          conservative form of the mask.  */
7154       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7155           && INTVAL (XEXP (x, 1)) >= 0
7156           && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7157           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7158         mask >>= INTVAL (XEXP (x, 1));
7159       else
7160         mask = fuller_mask;
7161
7162       op0 = gen_lowpart (op_mode,
7163                          force_to_mode (XEXP (x, 0), op_mode,
7164                                         mask, reg, next_select));
7165
7166       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7167         x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
7168       break;
7169
7170     case LSHIFTRT:
7171       /* Here we can only do something if the shift count is a constant,
7172          this shift constant is valid for the host, and we can do arithmetic
7173          in OP_MODE.  */
7174
7175       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7176           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7177           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7178         {
7179           rtx inner = XEXP (x, 0);
7180           unsigned HOST_WIDE_INT inner_mask;
7181
7182           /* Select the mask of the bits we need for the shift operand.  */
7183           inner_mask = mask << INTVAL (XEXP (x, 1));
7184
7185           /* We can only change the mode of the shift if we can do arithmetic
7186              in the mode of the shift and INNER_MASK is no wider than the
7187              width of X's mode.  */
7188           if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
7189             op_mode = GET_MODE (x);
7190
7191           inner = force_to_mode (inner, op_mode, inner_mask, reg, next_select);
7192
7193           if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7194             x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7195         }
7196
7197       /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7198          shift and AND produces only copies of the sign bit (C2 is one less
7199          than a power of two), we can do this with just a shift.  */
7200
7201       if (GET_CODE (x) == LSHIFTRT
7202           && GET_CODE (XEXP (x, 1)) == CONST_INT
7203           /* The shift puts one of the sign bit copies in the least significant
7204              bit.  */
7205           && ((INTVAL (XEXP (x, 1))
7206                + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7207               >= GET_MODE_BITSIZE (GET_MODE (x)))
7208           && exact_log2 (mask + 1) >= 0
7209           /* Number of bits left after the shift must be more than the mask
7210              needs.  */
7211           && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7212               <= GET_MODE_BITSIZE (GET_MODE (x)))
7213           /* Must be more sign bit copies than the mask needs.  */
7214           && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7215               >= exact_log2 (mask + 1)))
7216         x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7217                                  GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7218                                           - exact_log2 (mask + 1)));
7219
7220       goto shiftrt;
7221
7222     case ASHIFTRT:
7223       /* If we are just looking for the sign bit, we don't need this shift at
7224          all, even if it has a variable count.  */
7225       if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7226           && (mask == ((unsigned HOST_WIDE_INT) 1
7227                        << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7228         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7229
7230       /* If this is a shift by a constant, get a mask that contains those bits
7231          that are not copies of the sign bit.  We then have two cases:  If
7232          MASK only includes those bits, this can be a logical shift, which may
7233          allow simplifications.  If MASK is a single-bit field not within
7234          those bits, we are requesting a copy of the sign bit and hence can
7235          shift the sign bit to the appropriate location.  */
7236
7237       if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7238           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7239         {
7240           int i = -1;
7241
7242           /* If the considered data is wider than HOST_WIDE_INT, we can't
7243              represent a mask for all its bits in a single scalar.
7244              But we only care about the lower bits, so calculate these.  */
7245
7246           if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7247             {
7248               nonzero = ~(HOST_WIDE_INT) 0;
7249
7250               /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7251                  is the number of bits a full-width mask would have set.
7252                  We need only shift if these are fewer than nonzero can
7253                  hold.  If not, we must keep all bits set in nonzero.  */
7254
7255               if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7256                   < HOST_BITS_PER_WIDE_INT)
7257                 nonzero >>= INTVAL (XEXP (x, 1))
7258                             + HOST_BITS_PER_WIDE_INT
7259                             - GET_MODE_BITSIZE (GET_MODE (x)) ;
7260             }
7261           else
7262             {
7263               nonzero = GET_MODE_MASK (GET_MODE (x));
7264               nonzero >>= INTVAL (XEXP (x, 1));
7265             }
7266
7267           if ((mask & ~nonzero) == 0
7268               || (i = exact_log2 (mask)) >= 0)
7269             {
7270               x = simplify_shift_const
7271                 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7272                  i < 0 ? INTVAL (XEXP (x, 1))
7273                  : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7274
7275               if (GET_CODE (x) != ASHIFTRT)
7276                 return force_to_mode (x, mode, mask, reg, next_select);
7277             }
7278         }
7279
7280       /* If MASK is 1, convert this to an LSHIFTRT.  This can be done
7281          even if the shift count isn't a constant.  */
7282       if (mask == 1)
7283         x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7284                                  XEXP (x, 0), XEXP (x, 1));
7285
7286     shiftrt:
7287
7288       /* If this is a zero- or sign-extension operation that just affects bits
7289          we don't care about, remove it.  Be sure the call above returned
7290          something that is still a shift.  */
7291
7292       if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7293           && GET_CODE (XEXP (x, 1)) == CONST_INT
7294           && INTVAL (XEXP (x, 1)) >= 0
7295           && (INTVAL (XEXP (x, 1))
7296               <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7297           && GET_CODE (XEXP (x, 0)) == ASHIFT
7298           && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7299         return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7300                               reg, next_select);
7301
7302       break;
7303
7304     case ROTATE:
7305     case ROTATERT:
7306       /* If the shift count is constant and we can do computations
7307          in the mode of X, compute where the bits we care about are.
7308          Otherwise, we can't do anything.  Don't change the mode of
7309          the shift or propagate MODE into the shift, though.  */
7310       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7311           && INTVAL (XEXP (x, 1)) >= 0)
7312         {
7313           temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7314                                             GET_MODE (x), GEN_INT (mask),
7315                                             XEXP (x, 1));
7316           if (temp && GET_CODE (temp) == CONST_INT)
7317             SUBST (XEXP (x, 0),
7318                    force_to_mode (XEXP (x, 0), GET_MODE (x),
7319                                   INTVAL (temp), reg, next_select));
7320         }
7321       break;
7322
7323     case NEG:
7324       /* If we just want the low-order bit, the NEG isn't needed since it
7325          won't change the low-order bit.  */
7326       if (mask == 1)
7327         return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
7328
7329       /* We need any bits less significant than the most significant bit in
7330          MASK since carries from those bits will affect the bits we are
7331          interested in.  */
7332       mask = fuller_mask;
7333       goto unop;
7334
7335     case NOT:
7336       /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7337          same as the XOR case above.  Ensure that the constant we form is not
7338          wider than the mode of X.  */
7339
7340       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7341           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7342           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7343           && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7344               < GET_MODE_BITSIZE (GET_MODE (x)))
7345           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7346         {
7347           temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7348                                GET_MODE (x));
7349           temp = simplify_gen_binary (XOR, GET_MODE (x),
7350                                       XEXP (XEXP (x, 0), 0), temp);
7351           x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7352                                    temp, XEXP (XEXP (x, 0), 1));
7353
7354           return force_to_mode (x, mode, mask, reg, next_select);
7355         }
7356
7357       /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7358          use the full mask inside the NOT.  */
7359       mask = fuller_mask;
7360
7361     unop:
7362       op0 = gen_lowpart (op_mode,
7363                          force_to_mode (XEXP (x, 0), mode, mask,
7364                                         reg, next_select));
7365       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7366         x = simplify_gen_unary (code, op_mode, op0, op_mode);
7367       break;
7368
7369     case NE:
7370       /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7371          in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7372          which is equal to STORE_FLAG_VALUE.  */
7373       if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7374           && GET_MODE (XEXP (x, 0)) == mode
7375           && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7376           && (nonzero_bits (XEXP (x, 0), mode)
7377               == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7378         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7379
7380       break;
7381
7382     case IF_THEN_ELSE:
7383       /* We have no way of knowing if the IF_THEN_ELSE can itself be
7384          written in a narrower mode.  We play it safe and do not do so.  */
7385
7386       SUBST (XEXP (x, 1),
7387              gen_lowpart (GET_MODE (x),
7388                                       force_to_mode (XEXP (x, 1), mode,
7389                                                      mask, reg, next_select)));
7390       SUBST (XEXP (x, 2),
7391              gen_lowpart (GET_MODE (x),
7392                                       force_to_mode (XEXP (x, 2), mode,
7393                                                      mask, reg, next_select)));
7394       break;
7395
7396     default:
7397       break;
7398     }
7399
7400   /* Ensure we return a value of the proper mode.  */
7401   return gen_lowpart (mode, x);
7402 }
7403 \f
7404 /* Return nonzero if X is an expression that has one of two values depending on
7405    whether some other value is zero or nonzero.  In that case, we return the
7406    value that is being tested, *PTRUE is set to the value if the rtx being
7407    returned has a nonzero value, and *PFALSE is set to the other alternative.
7408
7409    If we return zero, we set *PTRUE and *PFALSE to X.  */
7410
7411 static rtx
7412 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
7413 {
7414   enum machine_mode mode = GET_MODE (x);
7415   enum rtx_code code = GET_CODE (x);
7416   rtx cond0, cond1, true0, true1, false0, false1;
7417   unsigned HOST_WIDE_INT nz;
7418
7419   /* If we are comparing a value against zero, we are done.  */
7420   if ((code == NE || code == EQ)
7421       && XEXP (x, 1) == const0_rtx)
7422     {
7423       *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7424       *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7425       return XEXP (x, 0);
7426     }
7427
7428   /* If this is a unary operation whose operand has one of two values, apply
7429      our opcode to compute those values.  */
7430   else if (UNARY_P (x)
7431            && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7432     {
7433       *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7434       *pfalse = simplify_gen_unary (code, mode, false0,
7435                                     GET_MODE (XEXP (x, 0)));
7436       return cond0;
7437     }
7438
7439   /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7440      make can't possibly match and would suppress other optimizations.  */
7441   else if (code == COMPARE)
7442     ;
7443
7444   /* If this is a binary operation, see if either side has only one of two
7445      values.  If either one does or if both do and they are conditional on
7446      the same value, compute the new true and false values.  */
7447   else if (BINARY_P (x))
7448     {
7449       cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7450       cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7451
7452       if ((cond0 != 0 || cond1 != 0)
7453           && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7454         {
7455           /* If if_then_else_cond returned zero, then true/false are the
7456              same rtl.  We must copy one of them to prevent invalid rtl
7457              sharing.  */
7458           if (cond0 == 0)
7459             true0 = copy_rtx (true0);
7460           else if (cond1 == 0)
7461             true1 = copy_rtx (true1);
7462
7463           if (COMPARISON_P (x))
7464             {
7465               *ptrue = simplify_gen_relational (code, mode, VOIDmode,
7466                                                 true0, true1);
7467               *pfalse = simplify_gen_relational (code, mode, VOIDmode,
7468                                                  false0, false1);
7469              }
7470           else
7471             {
7472               *ptrue = simplify_gen_binary (code, mode, true0, true1);
7473               *pfalse = simplify_gen_binary (code, mode, false0, false1);
7474             }
7475
7476           return cond0 ? cond0 : cond1;
7477         }
7478
7479       /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7480          operands is zero when the other is nonzero, and vice-versa,
7481          and STORE_FLAG_VALUE is 1 or -1.  */
7482
7483       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7484           && (code == PLUS || code == IOR || code == XOR || code == MINUS
7485               || code == UMAX)
7486           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7487         {
7488           rtx op0 = XEXP (XEXP (x, 0), 1);
7489           rtx op1 = XEXP (XEXP (x, 1), 1);
7490
7491           cond0 = XEXP (XEXP (x, 0), 0);
7492           cond1 = XEXP (XEXP (x, 1), 0);
7493
7494           if (COMPARISON_P (cond0)
7495               && COMPARISON_P (cond1)
7496               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7497                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7498                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7499                   || ((swap_condition (GET_CODE (cond0))
7500                        == reversed_comparison_code (cond1, NULL))
7501                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7502                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7503               && ! side_effects_p (x))
7504             {
7505               *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
7506               *pfalse = simplify_gen_binary (MULT, mode,
7507                                              (code == MINUS
7508                                               ? simplify_gen_unary (NEG, mode,
7509                                                                     op1, mode)
7510                                               : op1),
7511                                               const_true_rtx);
7512               return cond0;
7513             }
7514         }
7515
7516       /* Similarly for MULT, AND and UMIN, except that for these the result
7517          is always zero.  */
7518       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7519           && (code == MULT || code == AND || code == UMIN)
7520           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7521         {
7522           cond0 = XEXP (XEXP (x, 0), 0);
7523           cond1 = XEXP (XEXP (x, 1), 0);
7524
7525           if (COMPARISON_P (cond0)
7526               && COMPARISON_P (cond1)
7527               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7528                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7529                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7530                   || ((swap_condition (GET_CODE (cond0))
7531                        == reversed_comparison_code (cond1, NULL))
7532                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7533                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7534               && ! side_effects_p (x))
7535             {
7536               *ptrue = *pfalse = const0_rtx;
7537               return cond0;
7538             }
7539         }
7540     }
7541
7542   else if (code == IF_THEN_ELSE)
7543     {
7544       /* If we have IF_THEN_ELSE already, extract the condition and
7545          canonicalize it if it is NE or EQ.  */
7546       cond0 = XEXP (x, 0);
7547       *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7548       if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7549         return XEXP (cond0, 0);
7550       else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7551         {
7552           *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7553           return XEXP (cond0, 0);
7554         }
7555       else
7556         return cond0;
7557     }
7558
7559   /* If X is a SUBREG, we can narrow both the true and false values
7560      if the inner expression, if there is a condition.  */
7561   else if (code == SUBREG
7562            && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7563                                                &true0, &false0)))
7564     {
7565       true0 = simplify_gen_subreg (mode, true0,
7566                                    GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7567       false0 = simplify_gen_subreg (mode, false0,
7568                                     GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7569       if (true0 && false0)
7570         {
7571           *ptrue = true0;
7572           *pfalse = false0;
7573           return cond0;
7574         }
7575     }
7576
7577   /* If X is a constant, this isn't special and will cause confusions
7578      if we treat it as such.  Likewise if it is equivalent to a constant.  */
7579   else if (CONSTANT_P (x)
7580            || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7581     ;
7582
7583   /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7584      will be least confusing to the rest of the compiler.  */
7585   else if (mode == BImode)
7586     {
7587       *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7588       return x;
7589     }
7590
7591   /* If X is known to be either 0 or -1, those are the true and
7592      false values when testing X.  */
7593   else if (x == constm1_rtx || x == const0_rtx
7594            || (mode != VOIDmode
7595                && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7596     {
7597       *ptrue = constm1_rtx, *pfalse = const0_rtx;
7598       return x;
7599     }
7600
7601   /* Likewise for 0 or a single bit.  */
7602   else if (SCALAR_INT_MODE_P (mode)
7603            && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7604            && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7605     {
7606       *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7607       return x;
7608     }
7609
7610   /* Otherwise fail; show no condition with true and false values the same.  */
7611   *ptrue = *pfalse = x;
7612   return 0;
7613 }
7614 \f
7615 /* Return the value of expression X given the fact that condition COND
7616    is known to be true when applied to REG as its first operand and VAL
7617    as its second.  X is known to not be shared and so can be modified in
7618    place.
7619
7620    We only handle the simplest cases, and specifically those cases that
7621    arise with IF_THEN_ELSE expressions.  */
7622
7623 static rtx
7624 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
7625 {
7626   enum rtx_code code = GET_CODE (x);
7627   rtx temp;
7628   const char *fmt;
7629   int i, j;
7630
7631   if (side_effects_p (x))
7632     return x;
7633
7634   /* If either operand of the condition is a floating point value,
7635      then we have to avoid collapsing an EQ comparison.  */
7636   if (cond == EQ
7637       && rtx_equal_p (x, reg)
7638       && ! FLOAT_MODE_P (GET_MODE (x))
7639       && ! FLOAT_MODE_P (GET_MODE (val)))
7640     return val;
7641
7642   if (cond == UNEQ && rtx_equal_p (x, reg))
7643     return val;
7644
7645   /* If X is (abs REG) and we know something about REG's relationship
7646      with zero, we may be able to simplify this.  */
7647
7648   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7649     switch (cond)
7650       {
7651       case GE:  case GT:  case EQ:
7652         return XEXP (x, 0);
7653       case LT:  case LE:
7654         return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
7655                                    XEXP (x, 0),
7656                                    GET_MODE (XEXP (x, 0)));
7657       default:
7658         break;
7659       }
7660
7661   /* The only other cases we handle are MIN, MAX, and comparisons if the
7662      operands are the same as REG and VAL.  */
7663
7664   else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
7665     {
7666       if (rtx_equal_p (XEXP (x, 0), val))
7667         cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7668
7669       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7670         {
7671           if (COMPARISON_P (x))
7672             {
7673               if (comparison_dominates_p (cond, code))
7674                 return const_true_rtx;
7675
7676               code = reversed_comparison_code (x, NULL);
7677               if (code != UNKNOWN
7678                   && comparison_dominates_p (cond, code))
7679                 return const0_rtx;
7680               else
7681                 return x;
7682             }
7683           else if (code == SMAX || code == SMIN
7684                    || code == UMIN || code == UMAX)
7685             {
7686               int unsignedp = (code == UMIN || code == UMAX);
7687
7688               /* Do not reverse the condition when it is NE or EQ.
7689                  This is because we cannot conclude anything about
7690                  the value of 'SMAX (x, y)' when x is not equal to y,
7691                  but we can when x equals y.  */
7692               if ((code == SMAX || code == UMAX)
7693                   && ! (cond == EQ || cond == NE))
7694                 cond = reverse_condition (cond);
7695
7696               switch (cond)
7697                 {
7698                 case GE:   case GT:
7699                   return unsignedp ? x : XEXP (x, 1);
7700                 case LE:   case LT:
7701                   return unsignedp ? x : XEXP (x, 0);
7702                 case GEU:  case GTU:
7703                   return unsignedp ? XEXP (x, 1) : x;
7704                 case LEU:  case LTU:
7705                   return unsignedp ? XEXP (x, 0) : x;
7706                 default:
7707                   break;
7708                 }
7709             }
7710         }
7711     }
7712   else if (code == SUBREG)
7713     {
7714       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
7715       rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
7716
7717       if (SUBREG_REG (x) != r)
7718         {
7719           /* We must simplify subreg here, before we lose track of the
7720              original inner_mode.  */
7721           new = simplify_subreg (GET_MODE (x), r,
7722                                  inner_mode, SUBREG_BYTE (x));
7723           if (new)
7724             return new;
7725           else
7726             SUBST (SUBREG_REG (x), r);
7727         }
7728
7729       return x;
7730     }
7731   /* We don't have to handle SIGN_EXTEND here, because even in the
7732      case of replacing something with a modeless CONST_INT, a
7733      CONST_INT is already (supposed to be) a valid sign extension for
7734      its narrower mode, which implies it's already properly
7735      sign-extended for the wider mode.  Now, for ZERO_EXTEND, the
7736      story is different.  */
7737   else if (code == ZERO_EXTEND)
7738     {
7739       enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
7740       rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
7741
7742       if (XEXP (x, 0) != r)
7743         {
7744           /* We must simplify the zero_extend here, before we lose
7745              track of the original inner_mode.  */
7746           new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
7747                                           r, inner_mode);
7748           if (new)
7749             return new;
7750           else
7751             SUBST (XEXP (x, 0), r);
7752         }
7753
7754       return x;
7755     }
7756
7757   fmt = GET_RTX_FORMAT (code);
7758   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7759     {
7760       if (fmt[i] == 'e')
7761         SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7762       else if (fmt[i] == 'E')
7763         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7764           SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7765                                                 cond, reg, val));
7766     }
7767
7768   return x;
7769 }
7770 \f
7771 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
7772    assignment as a field assignment.  */
7773
7774 static int
7775 rtx_equal_for_field_assignment_p (rtx x, rtx y)
7776 {
7777   if (x == y || rtx_equal_p (x, y))
7778     return 1;
7779
7780   if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7781     return 0;
7782
7783   /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7784      Note that all SUBREGs of MEM are paradoxical; otherwise they
7785      would have been rewritten.  */
7786   if (MEM_P (x) && GET_CODE (y) == SUBREG
7787       && MEM_P (SUBREG_REG (y))
7788       && rtx_equal_p (SUBREG_REG (y),
7789                       gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
7790     return 1;
7791
7792   if (MEM_P (y) && GET_CODE (x) == SUBREG
7793       && MEM_P (SUBREG_REG (x))
7794       && rtx_equal_p (SUBREG_REG (x),
7795                       gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
7796     return 1;
7797
7798   /* We used to see if get_last_value of X and Y were the same but that's
7799      not correct.  In one direction, we'll cause the assignment to have
7800      the wrong destination and in the case, we'll import a register into this
7801      insn that might have already have been dead.   So fail if none of the
7802      above cases are true.  */
7803   return 0;
7804 }
7805 \f
7806 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
7807    Return that assignment if so.
7808
7809    We only handle the most common cases.  */
7810
7811 static rtx
7812 make_field_assignment (rtx x)
7813 {
7814   rtx dest = SET_DEST (x);
7815   rtx src = SET_SRC (x);
7816   rtx assign;
7817   rtx rhs, lhs;
7818   HOST_WIDE_INT c1;
7819   HOST_WIDE_INT pos;
7820   unsigned HOST_WIDE_INT len;
7821   rtx other;
7822   enum machine_mode mode;
7823
7824   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7825      a clear of a one-bit field.  We will have changed it to
7826      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
7827      for a SUBREG.  */
7828
7829   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7830       && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7831       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7832       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7833     {
7834       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7835                                 1, 1, 1, 0);
7836       if (assign != 0)
7837         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7838       return x;
7839     }
7840
7841   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7842       && subreg_lowpart_p (XEXP (src, 0))
7843       && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7844           < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7845       && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7846       && GET_CODE (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == CONST_INT
7847       && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7848       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7849     {
7850       assign = make_extraction (VOIDmode, dest, 0,
7851                                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7852                                 1, 1, 1, 0);
7853       if (assign != 0)
7854         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7855       return x;
7856     }
7857
7858   /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7859      one-bit field.  */
7860   if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7861       && XEXP (XEXP (src, 0), 0) == const1_rtx
7862       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7863     {
7864       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7865                                 1, 1, 1, 0);
7866       if (assign != 0)
7867         return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7868       return x;
7869     }
7870
7871   /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
7872      SRC is an AND with all bits of that field set, then we can discard
7873      the AND.  */
7874   if (GET_CODE (dest) == ZERO_EXTRACT
7875       && GET_CODE (XEXP (dest, 1)) == CONST_INT
7876       && GET_CODE (src) == AND
7877       && GET_CODE (XEXP (src, 1)) == CONST_INT)
7878     {
7879       HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
7880       unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
7881       unsigned HOST_WIDE_INT ze_mask;
7882
7883       if (width >= HOST_BITS_PER_WIDE_INT)
7884         ze_mask = -1;
7885       else
7886         ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
7887
7888       /* Complete overlap.  We can remove the source AND.  */
7889       if ((and_mask & ze_mask) == ze_mask)
7890         return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
7891
7892       /* Partial overlap.  We can reduce the source AND.  */
7893       if ((and_mask & ze_mask) != and_mask)
7894         {
7895           mode = GET_MODE (src);
7896           src = gen_rtx_AND (mode, XEXP (src, 0),
7897                              gen_int_mode (and_mask & ze_mask, mode));
7898           return gen_rtx_SET (VOIDmode, dest, src);
7899         }
7900     }
7901
7902   /* The other case we handle is assignments into a constant-position
7903      field.  They look like (ior/xor (and DEST C1) OTHER).  If C1 represents
7904      a mask that has all one bits except for a group of zero bits and
7905      OTHER is known to have zeros where C1 has ones, this is such an
7906      assignment.  Compute the position and length from C1.  Shift OTHER
7907      to the appropriate position, force it to the required mode, and
7908      make the extraction.  Check for the AND in both operands.  */
7909
7910   if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7911     return x;
7912
7913   rhs = expand_compound_operation (XEXP (src, 0));
7914   lhs = expand_compound_operation (XEXP (src, 1));
7915
7916   if (GET_CODE (rhs) == AND
7917       && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7918       && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7919     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7920   else if (GET_CODE (lhs) == AND
7921            && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7922            && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7923     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7924   else
7925     return x;
7926
7927   pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7928   if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7929       || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7930       || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7931     return x;
7932
7933   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7934   if (assign == 0)
7935     return x;
7936
7937   /* The mode to use for the source is the mode of the assignment, or of
7938      what is inside a possible STRICT_LOW_PART.  */
7939   mode = (GET_CODE (assign) == STRICT_LOW_PART
7940           ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7941
7942   /* Shift OTHER right POS places and make it the source, restricting it
7943      to the proper length and mode.  */
7944
7945   src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
7946                                              GET_MODE (src), other, pos),
7947                        mode,
7948                        GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7949                        ? ~(unsigned HOST_WIDE_INT) 0
7950                        : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7951                        dest, 0);
7952
7953   /* If SRC is masked by an AND that does not make a difference in
7954      the value being stored, strip it.  */
7955   if (GET_CODE (assign) == ZERO_EXTRACT
7956       && GET_CODE (XEXP (assign, 1)) == CONST_INT
7957       && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
7958       && GET_CODE (src) == AND
7959       && GET_CODE (XEXP (src, 1)) == CONST_INT
7960       && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
7961           == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
7962     src = XEXP (src, 0);
7963
7964   return gen_rtx_SET (VOIDmode, assign, src);
7965 }
7966 \f
7967 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7968    if so.  */
7969
7970 static rtx
7971 apply_distributive_law (rtx x)
7972 {
7973   enum rtx_code code = GET_CODE (x);
7974   enum rtx_code inner_code;
7975   rtx lhs, rhs, other;
7976   rtx tem;
7977
7978   /* Distributivity is not true for floating point as it can change the
7979      value.  So we don't do it unless -funsafe-math-optimizations.  */
7980   if (FLOAT_MODE_P (GET_MODE (x))
7981       && ! flag_unsafe_math_optimizations)
7982     return x;
7983
7984   /* The outer operation can only be one of the following:  */
7985   if (code != IOR && code != AND && code != XOR
7986       && code != PLUS && code != MINUS)
7987     return x;
7988
7989   lhs = XEXP (x, 0);
7990   rhs = XEXP (x, 1);
7991
7992   /* If either operand is a primitive we can't do anything, so get out
7993      fast.  */
7994   if (OBJECT_P (lhs) || OBJECT_P (rhs))
7995     return x;
7996
7997   lhs = expand_compound_operation (lhs);
7998   rhs = expand_compound_operation (rhs);
7999   inner_code = GET_CODE (lhs);
8000   if (inner_code != GET_CODE (rhs))
8001     return x;
8002
8003   /* See if the inner and outer operations distribute.  */
8004   switch (inner_code)
8005     {
8006     case LSHIFTRT:
8007     case ASHIFTRT:
8008     case AND:
8009     case IOR:
8010       /* These all distribute except over PLUS.  */
8011       if (code == PLUS || code == MINUS)
8012         return x;
8013       break;
8014
8015     case MULT:
8016       if (code != PLUS && code != MINUS)
8017         return x;
8018       break;
8019
8020     case ASHIFT:
8021       /* This is also a multiply, so it distributes over everything.  */
8022       break;
8023
8024     case SUBREG:
8025       /* Non-paradoxical SUBREGs distributes over all operations, provided
8026          the inner modes and byte offsets are the same, this is an extraction
8027          of a low-order part, we don't convert an fp operation to int or
8028          vice versa, and we would not be converting a single-word
8029          operation into a multi-word operation.  The latter test is not
8030          required, but it prevents generating unneeded multi-word operations.
8031          Some of the previous tests are redundant given the latter test, but
8032          are retained because they are required for correctness.
8033
8034          We produce the result slightly differently in this case.  */
8035
8036       if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
8037           || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
8038           || ! subreg_lowpart_p (lhs)
8039           || (GET_MODE_CLASS (GET_MODE (lhs))
8040               != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
8041           || (GET_MODE_SIZE (GET_MODE (lhs))
8042               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
8043           || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
8044         return x;
8045
8046       tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
8047                                  SUBREG_REG (lhs), SUBREG_REG (rhs));
8048       return gen_lowpart (GET_MODE (x), tem);
8049
8050     default:
8051       return x;
8052     }
8053
8054   /* Set LHS and RHS to the inner operands (A and B in the example
8055      above) and set OTHER to the common operand (C in the example).
8056      There is only one way to do this unless the inner operation is
8057      commutative.  */
8058   if (COMMUTATIVE_ARITH_P (lhs)
8059       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
8060     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
8061   else if (COMMUTATIVE_ARITH_P (lhs)
8062            && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
8063     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
8064   else if (COMMUTATIVE_ARITH_P (lhs)
8065            && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
8066     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
8067   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
8068     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
8069   else
8070     return x;
8071
8072   /* Form the new inner operation, seeing if it simplifies first.  */
8073   tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
8074
8075   /* There is one exception to the general way of distributing:
8076      (a | c) ^ (b | c) -> (a ^ b) & ~c  */
8077   if (code == XOR && inner_code == IOR)
8078     {
8079       inner_code = AND;
8080       other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
8081     }
8082
8083   /* We may be able to continuing distributing the result, so call
8084      ourselves recursively on the inner operation before forming the
8085      outer operation, which we return.  */
8086   return simplify_gen_binary (inner_code, GET_MODE (x),
8087                               apply_distributive_law (tem), other);
8088 }
8089
8090 /* See if X is of the form (* (+ A B) C), and if so convert to
8091    (+ (* A C) (* B C)) and try to simplify.
8092
8093    Most of the time, this results in no change.  However, if some of
8094    the operands are the same or inverses of each other, simplifications
8095    will result.
8096
8097    For example, (and (ior A B) (not B)) can occur as the result of
8098    expanding a bit field assignment.  When we apply the distributive
8099    law to this, we get (ior (and (A (not B))) (and (B (not B)))),
8100    which then simplifies to (and (A (not B))).
8101  
8102    Note that no checks happen on the validity of applying the inverse
8103    distributive law.  This is pointless since we can do it in the
8104    few places where this routine is called.
8105
8106    N is the index of the term that is decomposed (the arithmetic operation,
8107    i.e. (+ A B) in the first example above).  !N is the index of the term that
8108    is distributed, i.e. of C in the first example above.  */
8109 static rtx
8110 distribute_and_simplify_rtx (rtx x, int n)
8111 {
8112   enum machine_mode mode;
8113   enum rtx_code outer_code, inner_code;
8114   rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
8115
8116   decomposed = XEXP (x, n);
8117   if (!ARITHMETIC_P (decomposed))
8118     return NULL_RTX;
8119
8120   mode = GET_MODE (x);
8121   outer_code = GET_CODE (x);
8122   distributed = XEXP (x, !n);
8123
8124   inner_code = GET_CODE (decomposed);
8125   inner_op0 = XEXP (decomposed, 0);
8126   inner_op1 = XEXP (decomposed, 1);
8127
8128   /* Special case (and (xor B C) (not A)), which is equivalent to
8129      (xor (ior A B) (ior A C))  */
8130   if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
8131     {
8132       distributed = XEXP (distributed, 0);
8133       outer_code = IOR;
8134     }
8135
8136   if (n == 0)
8137     {
8138       /* Distribute the second term.  */
8139       new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
8140       new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
8141     }
8142   else
8143     {
8144       /* Distribute the first term.  */
8145       new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
8146       new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
8147     }
8148
8149   tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
8150                                                      new_op0, new_op1));
8151   if (GET_CODE (tmp) != outer_code
8152       && rtx_cost (tmp, SET) < rtx_cost (x, SET))
8153     return tmp;
8154
8155   return NULL_RTX;
8156 }
8157 \f
8158 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
8159    in MODE.
8160
8161    Return an equivalent form, if different from X.  Otherwise, return X.  If
8162    X is zero, we are to always construct the equivalent form.  */
8163
8164 static rtx
8165 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
8166                         unsigned HOST_WIDE_INT constop)
8167 {
8168   unsigned HOST_WIDE_INT nonzero;
8169   int i;
8170
8171   /* Simplify VAROP knowing that we will be only looking at some of the
8172      bits in it.
8173
8174      Note by passing in CONSTOP, we guarantee that the bits not set in
8175      CONSTOP are not significant and will never be examined.  We must
8176      ensure that is the case by explicitly masking out those bits
8177      before returning.  */
8178   varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
8179
8180   /* If VAROP is a CLOBBER, we will fail so return it.  */
8181   if (GET_CODE (varop) == CLOBBER)
8182     return varop;
8183
8184   /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
8185      to VAROP and return the new constant.  */
8186   if (GET_CODE (varop) == CONST_INT)
8187     return gen_int_mode (INTVAL (varop) & constop, mode);
8188
8189   /* See what bits may be nonzero in VAROP.  Unlike the general case of
8190      a call to nonzero_bits, here we don't care about bits outside
8191      MODE.  */
8192
8193   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
8194
8195   /* Turn off all bits in the constant that are known to already be zero.
8196      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
8197      which is tested below.  */
8198
8199   constop &= nonzero;
8200
8201   /* If we don't have any bits left, return zero.  */
8202   if (constop == 0)
8203     return const0_rtx;
8204
8205   /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
8206      a power of two, we can replace this with an ASHIFT.  */
8207   if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
8208       && (i = exact_log2 (constop)) >= 0)
8209     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
8210
8211   /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8212      or XOR, then try to apply the distributive law.  This may eliminate
8213      operations if either branch can be simplified because of the AND.
8214      It may also make some cases more complex, but those cases probably
8215      won't match a pattern either with or without this.  */
8216
8217   if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
8218     return
8219       gen_lowpart
8220         (mode,
8221          apply_distributive_law
8222          (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
8223                                simplify_and_const_int (NULL_RTX,
8224                                                        GET_MODE (varop),
8225                                                        XEXP (varop, 0),
8226                                                        constop),
8227                                simplify_and_const_int (NULL_RTX,
8228                                                        GET_MODE (varop),
8229                                                        XEXP (varop, 1),
8230                                                        constop))));
8231
8232   /* If VAROP is PLUS, and the constant is a mask of low bite, distribute
8233      the AND and see if one of the operands simplifies to zero.  If so, we
8234      may eliminate it.  */
8235
8236   if (GET_CODE (varop) == PLUS
8237       && exact_log2 (constop + 1) >= 0)
8238     {
8239       rtx o0, o1;
8240
8241       o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
8242       o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
8243       if (o0 == const0_rtx)
8244         return o1;
8245       if (o1 == const0_rtx)
8246         return o0;
8247     }
8248
8249   /* Get VAROP in MODE.  Try to get a SUBREG if not.  Don't make a new SUBREG
8250      if we already had one (just check for the simplest cases).  */
8251   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
8252       && GET_MODE (XEXP (x, 0)) == mode
8253       && SUBREG_REG (XEXP (x, 0)) == varop)
8254     varop = XEXP (x, 0);
8255   else
8256     varop = gen_lowpart (mode, varop);
8257
8258   /* If we can't make the SUBREG, try to return what we were given.  */
8259   if (GET_CODE (varop) == CLOBBER)
8260     return x ? x : varop;
8261
8262   /* If we are only masking insignificant bits, return VAROP.  */
8263   if (constop == nonzero)
8264     x = varop;
8265   else
8266     {
8267       /* Otherwise, return an AND.  */
8268       constop = trunc_int_for_mode (constop, mode);
8269       /* See how much, if any, of X we can use.  */
8270       if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
8271         x = simplify_gen_binary (AND, mode, varop, GEN_INT (constop));
8272
8273       else
8274         {
8275           if (GET_CODE (XEXP (x, 1)) != CONST_INT
8276               || (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) != constop)
8277             SUBST (XEXP (x, 1), GEN_INT (constop));
8278
8279           SUBST (XEXP (x, 0), varop);
8280         }
8281     }
8282
8283   return x;
8284 }
8285 \f
8286 /* Given a REG, X, compute which bits in X can be nonzero.
8287    We don't care about bits outside of those defined in MODE.
8288
8289    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8290    a shift, AND, or zero_extract, we can do better.  */
8291
8292 static rtx
8293 reg_nonzero_bits_for_combine (rtx x, enum machine_mode mode,
8294                               rtx known_x ATTRIBUTE_UNUSED,
8295                               enum machine_mode known_mode ATTRIBUTE_UNUSED,
8296                               unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
8297                               unsigned HOST_WIDE_INT *nonzero)
8298 {
8299   rtx tem;
8300
8301   /* If X is a register whose nonzero bits value is current, use it.
8302      Otherwise, if X is a register whose value we can find, use that
8303      value.  Otherwise, use the previously-computed global nonzero bits
8304      for this register.  */
8305
8306   if (reg_stat[REGNO (x)].last_set_value != 0
8307       && (reg_stat[REGNO (x)].last_set_mode == mode
8308           || (GET_MODE_CLASS (reg_stat[REGNO (x)].last_set_mode) == MODE_INT
8309               && GET_MODE_CLASS (mode) == MODE_INT))
8310       && (reg_stat[REGNO (x)].last_set_label == label_tick
8311           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8312               && REG_N_SETS (REGNO (x)) == 1
8313               && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8314                                     REGNO (x))))
8315       && INSN_CUID (reg_stat[REGNO (x)].last_set) < subst_low_cuid)
8316     {
8317       *nonzero &= reg_stat[REGNO (x)].last_set_nonzero_bits;
8318       return NULL;
8319     }
8320
8321   tem = get_last_value (x);
8322
8323   if (tem)
8324     {
8325 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8326       /* If X is narrower than MODE and TEM is a non-negative
8327          constant that would appear negative in the mode of X,
8328          sign-extend it for use in reg_nonzero_bits because some
8329          machines (maybe most) will actually do the sign-extension
8330          and this is the conservative approach.
8331
8332          ??? For 2.5, try to tighten up the MD files in this regard
8333          instead of this kludge.  */
8334
8335       if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
8336           && GET_CODE (tem) == CONST_INT
8337           && INTVAL (tem) > 0
8338           && 0 != (INTVAL (tem)
8339                    & ((HOST_WIDE_INT) 1
8340                       << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8341         tem = GEN_INT (INTVAL (tem)
8342                        | ((HOST_WIDE_INT) (-1)
8343                           << GET_MODE_BITSIZE (GET_MODE (x))));
8344 #endif
8345       return tem;
8346     }
8347   else if (nonzero_sign_valid && reg_stat[REGNO (x)].nonzero_bits)
8348     {
8349       unsigned HOST_WIDE_INT mask = reg_stat[REGNO (x)].nonzero_bits;
8350
8351       if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
8352         /* We don't know anything about the upper bits.  */
8353         mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8354       *nonzero &= mask;
8355     }
8356
8357   return NULL;
8358 }
8359
8360 /* Return the number of bits at the high-order end of X that are known to
8361    be equal to the sign bit.  X will be used in mode MODE; if MODE is
8362    VOIDmode, X will be used in its own mode.  The returned value  will always
8363    be between 1 and the number of bits in MODE.  */
8364
8365 static rtx
8366 reg_num_sign_bit_copies_for_combine (rtx x, enum machine_mode mode,
8367                                      rtx known_x ATTRIBUTE_UNUSED,
8368                                      enum machine_mode known_mode
8369                                      ATTRIBUTE_UNUSED,
8370                                      unsigned int known_ret ATTRIBUTE_UNUSED,
8371                                      unsigned int *result)
8372 {
8373   rtx tem;
8374
8375   if (reg_stat[REGNO (x)].last_set_value != 0
8376       && reg_stat[REGNO (x)].last_set_mode == mode
8377       && (reg_stat[REGNO (x)].last_set_label == label_tick
8378           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8379               && REG_N_SETS (REGNO (x)) == 1
8380               && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8381                                     REGNO (x))))
8382       && INSN_CUID (reg_stat[REGNO (x)].last_set) < subst_low_cuid)
8383     {
8384       *result = reg_stat[REGNO (x)].last_set_sign_bit_copies;
8385       return NULL;
8386     }
8387
8388   tem = get_last_value (x);
8389   if (tem != 0)
8390     return tem;
8391
8392   if (nonzero_sign_valid && reg_stat[REGNO (x)].sign_bit_copies != 0
8393       && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
8394     *result = reg_stat[REGNO (x)].sign_bit_copies;
8395       
8396   return NULL;
8397 }
8398 \f
8399 /* Return the number of "extended" bits there are in X, when interpreted
8400    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
8401    unsigned quantities, this is the number of high-order zero bits.
8402    For signed quantities, this is the number of copies of the sign bit
8403    minus 1.  In both case, this function returns the number of "spare"
8404    bits.  For example, if two quantities for which this function returns
8405    at least 1 are added, the addition is known not to overflow.
8406
8407    This function will always return 0 unless called during combine, which
8408    implies that it must be called from a define_split.  */
8409
8410 unsigned int
8411 extended_count (rtx x, enum machine_mode mode, int unsignedp)
8412 {
8413   if (nonzero_sign_valid == 0)
8414     return 0;
8415
8416   return (unsignedp
8417           ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8418              ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
8419                                - floor_log2 (nonzero_bits (x, mode)))
8420              : 0)
8421           : num_sign_bit_copies (x, mode) - 1);
8422 }
8423 \f
8424 /* This function is called from `simplify_shift_const' to merge two
8425    outer operations.  Specifically, we have already found that we need
8426    to perform operation *POP0 with constant *PCONST0 at the outermost
8427    position.  We would now like to also perform OP1 with constant CONST1
8428    (with *POP0 being done last).
8429
8430    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8431    the resulting operation.  *PCOMP_P is set to 1 if we would need to
8432    complement the innermost operand, otherwise it is unchanged.
8433
8434    MODE is the mode in which the operation will be done.  No bits outside
8435    the width of this mode matter.  It is assumed that the width of this mode
8436    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8437
8438    If *POP0 or OP1 are UNKNOWN, it means no operation is required.  Only NEG, PLUS,
8439    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
8440    result is simply *PCONST0.
8441
8442    If the resulting operation cannot be expressed as one operation, we
8443    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
8444
8445 static int
8446 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)
8447 {
8448   enum rtx_code op0 = *pop0;
8449   HOST_WIDE_INT const0 = *pconst0;
8450
8451   const0 &= GET_MODE_MASK (mode);
8452   const1 &= GET_MODE_MASK (mode);
8453
8454   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
8455   if (op0 == AND)
8456     const1 &= const0;
8457
8458   /* If OP0 or OP1 is UNKNOWN, this is easy.  Similarly if they are the same or
8459      if OP0 is SET.  */
8460
8461   if (op1 == UNKNOWN || op0 == SET)
8462     return 1;
8463
8464   else if (op0 == UNKNOWN)
8465     op0 = op1, const0 = const1;
8466
8467   else if (op0 == op1)
8468     {
8469       switch (op0)
8470         {
8471         case AND:
8472           const0 &= const1;
8473           break;
8474         case IOR:
8475           const0 |= const1;
8476           break;
8477         case XOR:
8478           const0 ^= const1;
8479           break;
8480         case PLUS:
8481           const0 += const1;
8482           break;
8483         case NEG:
8484           op0 = UNKNOWN;
8485           break;
8486         default:
8487           break;
8488         }
8489     }
8490
8491   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
8492   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8493     return 0;
8494
8495   /* If the two constants aren't the same, we can't do anything.  The
8496      remaining six cases can all be done.  */
8497   else if (const0 != const1)
8498     return 0;
8499
8500   else
8501     switch (op0)
8502       {
8503       case IOR:
8504         if (op1 == AND)
8505           /* (a & b) | b == b */
8506           op0 = SET;
8507         else /* op1 == XOR */
8508           /* (a ^ b) | b == a | b */
8509           {;}
8510         break;
8511
8512       case XOR:
8513         if (op1 == AND)
8514           /* (a & b) ^ b == (~a) & b */
8515           op0 = AND, *pcomp_p = 1;
8516         else /* op1 == IOR */
8517           /* (a | b) ^ b == a & ~b */
8518           op0 = AND, const0 = ~const0;
8519         break;
8520
8521       case AND:
8522         if (op1 == IOR)
8523           /* (a | b) & b == b */
8524         op0 = SET;
8525         else /* op1 == XOR */
8526           /* (a ^ b) & b) == (~a) & b */
8527           *pcomp_p = 1;
8528         break;
8529       default:
8530         break;
8531       }
8532
8533   /* Check for NO-OP cases.  */
8534   const0 &= GET_MODE_MASK (mode);
8535   if (const0 == 0
8536       && (op0 == IOR || op0 == XOR || op0 == PLUS))
8537     op0 = UNKNOWN;
8538   else if (const0 == 0 && op0 == AND)
8539     op0 = SET;
8540   else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8541            && op0 == AND)
8542     op0 = UNKNOWN;
8543
8544   /* ??? Slightly redundant with the above mask, but not entirely.
8545      Moving this above means we'd have to sign-extend the mode mask
8546      for the final test.  */
8547   const0 = trunc_int_for_mode (const0, mode);
8548
8549   *pop0 = op0;
8550   *pconst0 = const0;
8551
8552   return 1;
8553 }
8554 \f
8555 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
8556    The result of the shift is RESULT_MODE.  X, if nonzero, is an expression
8557    that we started with.
8558
8559    The shift is normally computed in the widest mode we find in VAROP, as
8560    long as it isn't a different number of words than RESULT_MODE.  Exceptions
8561    are ASHIFTRT and ROTATE, which are always done in their original mode,  */
8562
8563 static rtx
8564 simplify_shift_const (rtx x, enum rtx_code code,
8565                       enum machine_mode result_mode, rtx varop,
8566                       int orig_count)
8567 {
8568   enum rtx_code orig_code = code;
8569   unsigned int count;
8570   int signed_count;
8571   enum machine_mode mode = result_mode;
8572   enum machine_mode shift_mode, tmode;
8573   unsigned int mode_words
8574     = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8575   /* We form (outer_op (code varop count) (outer_const)).  */
8576   enum rtx_code outer_op = UNKNOWN;
8577   HOST_WIDE_INT outer_const = 0;
8578   rtx const_rtx;
8579   int complement_p = 0;
8580   rtx new;
8581
8582   /* Make sure and truncate the "natural" shift on the way in.  We don't
8583      want to do this inside the loop as it makes it more difficult to
8584      combine shifts.  */
8585   if (SHIFT_COUNT_TRUNCATED)
8586     orig_count &= GET_MODE_BITSIZE (mode) - 1;
8587
8588   /* If we were given an invalid count, don't do anything except exactly
8589      what was requested.  */
8590
8591   if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
8592     {
8593       if (x)
8594         return x;
8595
8596       return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (orig_count));
8597     }
8598
8599   count = orig_count;
8600
8601   /* Unless one of the branches of the `if' in this loop does a `continue',
8602      we will `break' the loop after the `if'.  */
8603
8604   while (count != 0)
8605     {
8606       /* If we have an operand of (clobber (const_int 0)), just return that
8607          value.  */
8608       if (GET_CODE (varop) == CLOBBER)
8609         return varop;
8610
8611       /* If we discovered we had to complement VAROP, leave.  Making a NOT
8612          here would cause an infinite loop.  */
8613       if (complement_p)
8614         break;
8615
8616       /* Convert ROTATERT to ROTATE.  */
8617       if (code == ROTATERT)
8618         {
8619           unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
8620           code = ROTATE;
8621           if (VECTOR_MODE_P (result_mode))
8622             count = bitsize / GET_MODE_NUNITS (result_mode) - count;
8623           else
8624             count = bitsize - count;
8625         }
8626
8627       /* We need to determine what mode we will do the shift in.  If the
8628          shift is a right shift or a ROTATE, we must always do it in the mode
8629          it was originally done in.  Otherwise, we can do it in MODE, the
8630          widest mode encountered.  */
8631       shift_mode
8632         = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8633            ? result_mode : mode);
8634
8635       /* Handle cases where the count is greater than the size of the mode
8636          minus 1.  For ASHIFT, use the size minus one as the count (this can
8637          occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
8638          take the count modulo the size.  For other shifts, the result is
8639          zero.
8640
8641          Since these shifts are being produced by the compiler by combining
8642          multiple operations, each of which are defined, we know what the
8643          result is supposed to be.  */
8644
8645       if (count > (unsigned int) (GET_MODE_BITSIZE (shift_mode) - 1))
8646         {
8647           if (code == ASHIFTRT)
8648             count = GET_MODE_BITSIZE (shift_mode) - 1;
8649           else if (code == ROTATE || code == ROTATERT)
8650             count %= GET_MODE_BITSIZE (shift_mode);
8651           else
8652             {
8653               /* We can't simply return zero because there may be an
8654                  outer op.  */
8655               varop = const0_rtx;
8656               count = 0;
8657               break;
8658             }
8659         }
8660
8661       /* An arithmetic right shift of a quantity known to be -1 or 0
8662          is a no-op.  */
8663       if (code == ASHIFTRT
8664           && (num_sign_bit_copies (varop, shift_mode)
8665               == GET_MODE_BITSIZE (shift_mode)))
8666         {
8667           count = 0;
8668           break;
8669         }
8670
8671       /* If we are doing an arithmetic right shift and discarding all but
8672          the sign bit copies, this is equivalent to doing a shift by the
8673          bitsize minus one.  Convert it into that shift because it will often
8674          allow other simplifications.  */
8675
8676       if (code == ASHIFTRT
8677           && (count + num_sign_bit_copies (varop, shift_mode)
8678               >= GET_MODE_BITSIZE (shift_mode)))
8679         count = GET_MODE_BITSIZE (shift_mode) - 1;
8680
8681       /* We simplify the tests below and elsewhere by converting
8682          ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8683          `make_compound_operation' will convert it to an ASHIFTRT for
8684          those machines (such as VAX) that don't have an LSHIFTRT.  */
8685       if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8686           && code == ASHIFTRT
8687           && ((nonzero_bits (varop, shift_mode)
8688                & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
8689               == 0))
8690         code = LSHIFTRT;
8691
8692       if (code == LSHIFTRT
8693           && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8694           && !(nonzero_bits (varop, shift_mode) >> count))
8695         varop = const0_rtx;
8696       if (code == ASHIFT
8697           && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8698           && !((nonzero_bits (varop, shift_mode) << count)
8699                & GET_MODE_MASK (shift_mode)))
8700         varop = const0_rtx;
8701
8702       switch (GET_CODE (varop))
8703         {
8704         case SIGN_EXTEND:
8705         case ZERO_EXTEND:
8706         case SIGN_EXTRACT:
8707         case ZERO_EXTRACT:
8708           new = expand_compound_operation (varop);
8709           if (new != varop)
8710             {
8711               varop = new;
8712               continue;
8713             }
8714           break;
8715
8716         case MEM:
8717           /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
8718              minus the width of a smaller mode, we can do this with a
8719              SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
8720           if ((code == ASHIFTRT || code == LSHIFTRT)
8721               && ! mode_dependent_address_p (XEXP (varop, 0))
8722               && ! MEM_VOLATILE_P (varop)
8723               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8724                                          MODE_INT, 1)) != BLKmode)
8725             {
8726               new = adjust_address_nv (varop, tmode,
8727                                        BYTES_BIG_ENDIAN ? 0
8728                                        : count / BITS_PER_UNIT);
8729
8730               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
8731                                      : ZERO_EXTEND, mode, new);
8732               count = 0;
8733               continue;
8734             }
8735           break;
8736
8737         case USE:
8738           /* Similar to the case above, except that we can only do this if
8739              the resulting mode is the same as that of the underlying
8740              MEM and adjust the address depending on the *bits* endianness
8741              because of the way that bit-field extract insns are defined.  */
8742           if ((code == ASHIFTRT || code == LSHIFTRT)
8743               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8744                                          MODE_INT, 1)) != BLKmode
8745               && tmode == GET_MODE (XEXP (varop, 0)))
8746             {
8747               if (BITS_BIG_ENDIAN)
8748                 new = XEXP (varop, 0);
8749               else
8750                 {
8751                   new = copy_rtx (XEXP (varop, 0));
8752                   SUBST (XEXP (new, 0),
8753                          plus_constant (XEXP (new, 0),
8754                                         count / BITS_PER_UNIT));
8755                 }
8756
8757               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
8758                                      : ZERO_EXTEND, mode, new);
8759               count = 0;
8760               continue;
8761             }
8762           break;
8763
8764         case SUBREG:
8765           /* If VAROP is a SUBREG, strip it as long as the inner operand has
8766              the same number of words as what we've seen so far.  Then store
8767              the widest mode in MODE.  */
8768           if (subreg_lowpart_p (varop)
8769               && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8770                   > GET_MODE_SIZE (GET_MODE (varop)))
8771               && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8772                                   + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
8773                  == mode_words)
8774             {
8775               varop = SUBREG_REG (varop);
8776               if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
8777                 mode = GET_MODE (varop);
8778               continue;
8779             }
8780           break;
8781
8782         case MULT:
8783           /* Some machines use MULT instead of ASHIFT because MULT
8784              is cheaper.  But it is still better on those machines to
8785              merge two shifts into one.  */
8786           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8787               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8788             {
8789               varop
8790                 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
8791                                        XEXP (varop, 0),
8792                                        GEN_INT (exact_log2 (
8793                                                 INTVAL (XEXP (varop, 1)))));
8794               continue;
8795             }
8796           break;
8797
8798         case UDIV:
8799           /* Similar, for when divides are cheaper.  */
8800           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8801               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8802             {
8803               varop
8804                 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
8805                                        XEXP (varop, 0),
8806                                        GEN_INT (exact_log2 (
8807                                                 INTVAL (XEXP (varop, 1)))));
8808               continue;
8809             }
8810           break;
8811
8812         case ASHIFTRT:
8813           /* If we are extracting just the sign bit of an arithmetic
8814              right shift, that shift is not needed.  However, the sign
8815              bit of a wider mode may be different from what would be
8816              interpreted as the sign bit in a narrower mode, so, if
8817              the result is narrower, don't discard the shift.  */
8818           if (code == LSHIFTRT
8819               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
8820               && (GET_MODE_BITSIZE (result_mode)
8821                   >= GET_MODE_BITSIZE (GET_MODE (varop))))
8822             {
8823               varop = XEXP (varop, 0);
8824               continue;
8825             }
8826
8827           /* ... fall through ...  */
8828
8829         case LSHIFTRT:
8830         case ASHIFT:
8831         case ROTATE:
8832           /* Here we have two nested shifts.  The result is usually the
8833              AND of a new shift with a mask.  We compute the result below.  */
8834           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8835               && INTVAL (XEXP (varop, 1)) >= 0
8836               && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
8837               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8838               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8839             {
8840               enum rtx_code first_code = GET_CODE (varop);
8841               unsigned int first_count = INTVAL (XEXP (varop, 1));
8842               unsigned HOST_WIDE_INT mask;
8843               rtx mask_rtx;
8844
8845               /* We have one common special case.  We can't do any merging if
8846                  the inner code is an ASHIFTRT of a smaller mode.  However, if
8847                  we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
8848                  with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
8849                  we can convert it to
8850                  (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
8851                  This simplifies certain SIGN_EXTEND operations.  */
8852               if (code == ASHIFT && first_code == ASHIFTRT
8853                   && count == (unsigned int)
8854                               (GET_MODE_BITSIZE (result_mode)
8855                                - GET_MODE_BITSIZE (GET_MODE (varop))))
8856                 {
8857                   /* C3 has the low-order C1 bits zero.  */
8858
8859                   mask = (GET_MODE_MASK (mode)
8860                           & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
8861
8862                   varop = simplify_and_const_int (NULL_RTX, result_mode,
8863                                                   XEXP (varop, 0), mask);
8864                   varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
8865                                                 varop, count);
8866                   count = first_count;
8867                   code = ASHIFTRT;
8868                   continue;
8869                 }
8870
8871               /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
8872                  than C1 high-order bits equal to the sign bit, we can convert
8873                  this to either an ASHIFT or an ASHIFTRT depending on the
8874                  two counts.
8875
8876                  We cannot do this if VAROP's mode is not SHIFT_MODE.  */
8877
8878               if (code == ASHIFTRT && first_code == ASHIFT
8879                   && GET_MODE (varop) == shift_mode
8880                   && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
8881                       > first_count))
8882                 {
8883                   varop = XEXP (varop, 0);
8884
8885                   signed_count = count - first_count;
8886                   if (signed_count < 0)
8887                     count = -signed_count, code = ASHIFT;
8888                   else
8889                     count = signed_count;
8890
8891                   continue;
8892                 }
8893
8894               /* There are some cases we can't do.  If CODE is ASHIFTRT,
8895                  we can only do this if FIRST_CODE is also ASHIFTRT.
8896
8897                  We can't do the case when CODE is ROTATE and FIRST_CODE is
8898                  ASHIFTRT.
8899
8900                  If the mode of this shift is not the mode of the outer shift,
8901                  we can't do this if either shift is a right shift or ROTATE.
8902
8903                  Finally, we can't do any of these if the mode is too wide
8904                  unless the codes are the same.
8905
8906                  Handle the case where the shift codes are the same
8907                  first.  */
8908
8909               if (code == first_code)
8910                 {
8911                   if (GET_MODE (varop) != result_mode
8912                       && (code == ASHIFTRT || code == LSHIFTRT
8913                           || code == ROTATE))
8914                     break;
8915
8916                   count += first_count;
8917                   varop = XEXP (varop, 0);
8918                   continue;
8919                 }
8920
8921               if (code == ASHIFTRT
8922                   || (code == ROTATE && first_code == ASHIFTRT)
8923                   || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
8924                   || (GET_MODE (varop) != result_mode
8925                       && (first_code == ASHIFTRT || first_code == LSHIFTRT
8926                           || first_code == ROTATE
8927                           || code == ROTATE)))
8928                 break;
8929
8930               /* To compute the mask to apply after the shift, shift the
8931                  nonzero bits of the inner shift the same way the
8932                  outer shift will.  */
8933
8934               mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
8935
8936               mask_rtx
8937                 = simplify_binary_operation (code, result_mode, mask_rtx,
8938                                              GEN_INT (count));
8939
8940               /* Give up if we can't compute an outer operation to use.  */
8941               if (mask_rtx == 0
8942                   || GET_CODE (mask_rtx) != CONST_INT
8943                   || ! merge_outer_ops (&outer_op, &outer_const, AND,
8944                                         INTVAL (mask_rtx),
8945                                         result_mode, &complement_p))
8946                 break;
8947
8948               /* If the shifts are in the same direction, we add the
8949                  counts.  Otherwise, we subtract them.  */
8950               signed_count = count;
8951               if ((code == ASHIFTRT || code == LSHIFTRT)
8952                   == (first_code == ASHIFTRT || first_code == LSHIFTRT))
8953                 signed_count += first_count;
8954               else
8955                 signed_count -= first_count;
8956
8957               /* If COUNT is positive, the new shift is usually CODE,
8958                  except for the two exceptions below, in which case it is
8959                  FIRST_CODE.  If the count is negative, FIRST_CODE should
8960                  always be used  */
8961               if (signed_count > 0
8962                   && ((first_code == ROTATE && code == ASHIFT)
8963                       || (first_code == ASHIFTRT && code == LSHIFTRT)))
8964                 code = first_code, count = signed_count;
8965               else if (signed_count < 0)
8966                 code = first_code, count = -signed_count;
8967               else
8968                 count = signed_count;
8969
8970               varop = XEXP (varop, 0);
8971               continue;
8972             }
8973
8974           /* If we have (A << B << C) for any shift, we can convert this to
8975              (A << C << B).  This wins if A is a constant.  Only try this if
8976              B is not a constant.  */
8977
8978           else if (GET_CODE (varop) == code
8979                    && GET_CODE (XEXP (varop, 1)) != CONST_INT
8980                    && 0 != (new
8981                             = simplify_binary_operation (code, mode,
8982                                                          XEXP (varop, 0),
8983                                                          GEN_INT (count))))
8984             {
8985               varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
8986               count = 0;
8987               continue;
8988             }
8989           break;
8990
8991         case NOT:
8992           /* Make this fit the case below.  */
8993           varop = gen_rtx_XOR (mode, XEXP (varop, 0),
8994                                GEN_INT (GET_MODE_MASK (mode)));
8995           continue;
8996
8997         case IOR:
8998         case AND:
8999         case XOR:
9000           /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9001              with C the size of VAROP - 1 and the shift is logical if
9002              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9003              we have an (le X 0) operation.   If we have an arithmetic shift
9004              and STORE_FLAG_VALUE is 1 or we have a logical shift with
9005              STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
9006
9007           if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
9008               && XEXP (XEXP (varop, 0), 1) == constm1_rtx
9009               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9010               && (code == LSHIFTRT || code == ASHIFTRT)
9011               && count == (unsigned int)
9012                           (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9013               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9014             {
9015               count = 0;
9016               varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
9017                                   const0_rtx);
9018
9019               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9020                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9021
9022               continue;
9023             }
9024
9025           /* If we have (shift (logical)), move the logical to the outside
9026              to allow it to possibly combine with another logical and the
9027              shift to combine with another shift.  This also canonicalizes to
9028              what a ZERO_EXTRACT looks like.  Also, some machines have
9029              (and (shift)) insns.  */
9030
9031           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9032               /* We can't do this if we have (ashiftrt (xor))  and the
9033                  constant has its sign bit set in shift_mode.  */
9034               && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9035                    && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9036                                               shift_mode))
9037               && (new = simplify_binary_operation (code, result_mode,
9038                                                    XEXP (varop, 1),
9039                                                    GEN_INT (count))) != 0
9040               && GET_CODE (new) == CONST_INT
9041               && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9042                                   INTVAL (new), result_mode, &complement_p))
9043             {
9044               varop = XEXP (varop, 0);
9045               continue;
9046             }
9047
9048           /* If we can't do that, try to simplify the shift in each arm of the
9049              logical expression, make a new logical expression, and apply
9050              the inverse distributive law.  This also can't be done
9051              for some (ashiftrt (xor)).  */
9052           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9053              && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9054                   && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9055                                              shift_mode)))
9056             {
9057               rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9058                                               XEXP (varop, 0), count);
9059               rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9060                                               XEXP (varop, 1), count);
9061
9062               varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
9063                                            lhs, rhs);
9064               varop = apply_distributive_law (varop);
9065
9066               count = 0;
9067               continue; 
9068             }
9069           break;
9070
9071         case EQ:
9072           /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9073              says that the sign bit can be tested, FOO has mode MODE, C is
9074              GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9075              that may be nonzero.  */
9076           if (code == LSHIFTRT
9077               && XEXP (varop, 1) == const0_rtx
9078               && GET_MODE (XEXP (varop, 0)) == result_mode
9079               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9080               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9081               && ((STORE_FLAG_VALUE
9082                    & ((HOST_WIDE_INT) 1
9083                       < (GET_MODE_BITSIZE (result_mode) - 1))))
9084               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9085               && merge_outer_ops (&outer_op, &outer_const, XOR,
9086                                   (HOST_WIDE_INT) 1, result_mode,
9087                                   &complement_p))
9088             {
9089               varop = XEXP (varop, 0);
9090               count = 0;
9091               continue;
9092             }
9093           break;
9094
9095         case NEG:
9096           /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9097              than the number of bits in the mode is equivalent to A.  */
9098           if (code == LSHIFTRT
9099               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9100               && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9101             {
9102               varop = XEXP (varop, 0);
9103               count = 0;
9104               continue;
9105             }
9106
9107           /* NEG commutes with ASHIFT since it is multiplication.  Move the
9108              NEG outside to allow shifts to combine.  */
9109           if (code == ASHIFT
9110               && merge_outer_ops (&outer_op, &outer_const, NEG,
9111                                   (HOST_WIDE_INT) 0, result_mode,
9112                                   &complement_p))
9113             {
9114               varop = XEXP (varop, 0);
9115               continue;
9116             }
9117           break;
9118
9119         case PLUS:
9120           /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9121              is one less than the number of bits in the mode is
9122              equivalent to (xor A 1).  */
9123           if (code == LSHIFTRT
9124               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9125               && XEXP (varop, 1) == constm1_rtx
9126               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9127               && merge_outer_ops (&outer_op, &outer_const, XOR,
9128                                   (HOST_WIDE_INT) 1, result_mode,
9129                                   &complement_p))
9130             {
9131               count = 0;
9132               varop = XEXP (varop, 0);
9133               continue;
9134             }
9135
9136           /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9137              that might be nonzero in BAR are those being shifted out and those
9138              bits are known zero in FOO, we can replace the PLUS with FOO.
9139              Similarly in the other operand order.  This code occurs when
9140              we are computing the size of a variable-size array.  */
9141
9142           if ((code == ASHIFTRT || code == LSHIFTRT)
9143               && count < HOST_BITS_PER_WIDE_INT
9144               && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9145               && (nonzero_bits (XEXP (varop, 1), result_mode)
9146                   & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9147             {
9148               varop = XEXP (varop, 0);
9149               continue;
9150             }
9151           else if ((code == ASHIFTRT || code == LSHIFTRT)
9152                    && count < HOST_BITS_PER_WIDE_INT
9153                    && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9154                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9155                             >> count)
9156                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9157                             & nonzero_bits (XEXP (varop, 1),
9158                                                  result_mode)))
9159             {
9160               varop = XEXP (varop, 1);
9161               continue;
9162             }
9163
9164           /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
9165           if (code == ASHIFT
9166               && GET_CODE (XEXP (varop, 1)) == CONST_INT
9167               && (new = simplify_binary_operation (ASHIFT, result_mode,
9168                                                    XEXP (varop, 1),
9169                                                    GEN_INT (count))) != 0
9170               && GET_CODE (new) == CONST_INT
9171               && merge_outer_ops (&outer_op, &outer_const, PLUS,
9172                                   INTVAL (new), result_mode, &complement_p))
9173             {
9174               varop = XEXP (varop, 0);
9175               continue;
9176             }
9177
9178           /* Check for 'PLUS signbit', which is the canonical form of 'XOR
9179              signbit', and attempt to change the PLUS to an XOR and move it to
9180              the outer operation as is done above in the AND/IOR/XOR case
9181              leg for shift(logical). See details in logical handling above
9182              for reasoning in doing so.  */
9183           if (code == LSHIFTRT
9184               && GET_CODE (XEXP (varop, 1)) == CONST_INT
9185               && mode_signbit_p (result_mode, XEXP (varop, 1))
9186               && (new = simplify_binary_operation (code, result_mode,
9187                                                    XEXP (varop, 1),
9188                                                    GEN_INT (count))) != 0
9189               && GET_CODE (new) == CONST_INT
9190               && merge_outer_ops (&outer_op, &outer_const, XOR,
9191                                   INTVAL (new), result_mode, &complement_p))
9192             {
9193               varop = XEXP (varop, 0);
9194               continue;
9195             }
9196
9197           break;
9198
9199         case MINUS:
9200           /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9201              with C the size of VAROP - 1 and the shift is logical if
9202              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9203              we have a (gt X 0) operation.  If the shift is arithmetic with
9204              STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9205              we have a (neg (gt X 0)) operation.  */
9206
9207           if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9208               && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9209               && count == (unsigned int)
9210                           (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9211               && (code == LSHIFTRT || code == ASHIFTRT)
9212               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9213               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (varop, 0), 1))
9214                  == count
9215               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9216             {
9217               count = 0;
9218               varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9219                                   const0_rtx);
9220
9221               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9222                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9223
9224               continue;
9225             }
9226           break;
9227
9228         case TRUNCATE:
9229           /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9230              if the truncate does not affect the value.  */
9231           if (code == LSHIFTRT
9232               && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9233               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9234               && (INTVAL (XEXP (XEXP (varop, 0), 1))
9235                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9236                       - GET_MODE_BITSIZE (GET_MODE (varop)))))
9237             {
9238               rtx varop_inner = XEXP (varop, 0);
9239
9240               varop_inner
9241                 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9242                                     XEXP (varop_inner, 0),
9243                                     GEN_INT
9244                                     (count + INTVAL (XEXP (varop_inner, 1))));
9245               varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9246               count = 0;
9247               continue;
9248             }
9249           break;
9250
9251         default:
9252           break;
9253         }
9254
9255       break;
9256     }
9257
9258   /* We need to determine what mode to do the shift in.  If the shift is
9259      a right shift or ROTATE, we must always do it in the mode it was
9260      originally done in.  Otherwise, we can do it in MODE, the widest mode
9261      encountered.  The code we care about is that of the shift that will
9262      actually be done, not the shift that was originally requested.  */
9263   shift_mode
9264     = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9265        ? result_mode : mode);
9266
9267   /* We have now finished analyzing the shift.  The result should be
9268      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
9269      OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
9270      to the result of the shift.  OUTER_CONST is the relevant constant,
9271      but we must turn off all bits turned off in the shift.
9272
9273      If we were passed a value for X, see if we can use any pieces of
9274      it.  If not, make new rtx.  */
9275
9276   if (x && GET_RTX_CLASS (GET_CODE (x)) == RTX_BIN_ARITH
9277       && GET_CODE (XEXP (x, 1)) == CONST_INT
9278       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) == count)
9279     const_rtx = XEXP (x, 1);
9280   else
9281     const_rtx = GEN_INT (count);
9282
9283   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
9284       && GET_MODE (XEXP (x, 0)) == shift_mode
9285       && SUBREG_REG (XEXP (x, 0)) == varop)
9286     varop = XEXP (x, 0);
9287   else if (GET_MODE (varop) != shift_mode)
9288     varop = gen_lowpart (shift_mode, varop);
9289
9290   /* If we can't make the SUBREG, try to return what we were given.  */
9291   if (GET_CODE (varop) == CLOBBER)
9292     return x ? x : varop;
9293
9294   new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
9295   if (new != 0)
9296     x = new;
9297   else
9298     x = gen_rtx_fmt_ee (code, shift_mode, varop, const_rtx);
9299
9300   /* If we have an outer operation and we just made a shift, it is
9301      possible that we could have simplified the shift were it not
9302      for the outer operation.  So try to do the simplification
9303      recursively.  */
9304
9305   if (outer_op != UNKNOWN && GET_CODE (x) == code
9306       && GET_CODE (XEXP (x, 1)) == CONST_INT)
9307     x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
9308                               INTVAL (XEXP (x, 1)));
9309
9310   /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9311      turn off all the bits that the shift would have turned off.  */
9312   if (orig_code == LSHIFTRT && result_mode != shift_mode)
9313     x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9314                                 GET_MODE_MASK (result_mode) >> orig_count);
9315
9316   /* Do the remainder of the processing in RESULT_MODE.  */
9317   x = gen_lowpart (result_mode, x);
9318
9319   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9320      operation.  */
9321   if (complement_p)
9322     x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9323
9324   if (outer_op != UNKNOWN)
9325     {
9326       if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9327         outer_const = trunc_int_for_mode (outer_const, result_mode);
9328
9329       if (outer_op == AND)
9330         x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9331       else if (outer_op == SET)
9332         /* This means that we have determined that the result is
9333            equivalent to a constant.  This should be rare.  */
9334         x = GEN_INT (outer_const);
9335       else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
9336         x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
9337       else
9338         x = simplify_gen_binary (outer_op, result_mode, x,
9339                                  GEN_INT (outer_const));
9340     }
9341
9342   return x;
9343 }
9344 \f
9345 /* Like recog, but we receive the address of a pointer to a new pattern.
9346    We try to match the rtx that the pointer points to.
9347    If that fails, we may try to modify or replace the pattern,
9348    storing the replacement into the same pointer object.
9349
9350    Modifications include deletion or addition of CLOBBERs.
9351
9352    PNOTES is a pointer to a location where any REG_UNUSED notes added for
9353    the CLOBBERs are placed.
9354
9355    The value is the final insn code from the pattern ultimately matched,
9356    or -1.  */
9357
9358 static int
9359 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
9360 {
9361   rtx pat = *pnewpat;
9362   int insn_code_number;
9363   int num_clobbers_to_add = 0;
9364   int i;
9365   rtx notes = 0;
9366   rtx old_notes, old_pat;
9367
9368   /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9369      we use to indicate that something didn't match.  If we find such a
9370      thing, force rejection.  */
9371   if (GET_CODE (pat) == PARALLEL)
9372     for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9373       if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9374           && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9375         return -1;
9376
9377   old_pat = PATTERN (insn);
9378   old_notes = REG_NOTES (insn);
9379   PATTERN (insn) = pat;
9380   REG_NOTES (insn) = 0;
9381
9382   insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9383
9384   /* If it isn't, there is the possibility that we previously had an insn
9385      that clobbered some register as a side effect, but the combined
9386      insn doesn't need to do that.  So try once more without the clobbers
9387      unless this represents an ASM insn.  */
9388
9389   if (insn_code_number < 0 && ! check_asm_operands (pat)
9390       && GET_CODE (pat) == PARALLEL)
9391     {
9392       int pos;
9393
9394       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9395         if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9396           {
9397             if (i != pos)
9398               SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9399             pos++;
9400           }
9401
9402       SUBST_INT (XVECLEN (pat, 0), pos);
9403
9404       if (pos == 1)
9405         pat = XVECEXP (pat, 0, 0);
9406
9407       PATTERN (insn) = pat;
9408       insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9409     }
9410   PATTERN (insn) = old_pat;
9411   REG_NOTES (insn) = old_notes;
9412
9413   /* Recognize all noop sets, these will be killed by followup pass.  */
9414   if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
9415     insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
9416
9417   /* If we had any clobbers to add, make a new pattern than contains
9418      them.  Then check to make sure that all of them are dead.  */
9419   if (num_clobbers_to_add)
9420     {
9421       rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9422                                      rtvec_alloc (GET_CODE (pat) == PARALLEL
9423                                                   ? (XVECLEN (pat, 0)
9424                                                      + num_clobbers_to_add)
9425                                                   : num_clobbers_to_add + 1));
9426
9427       if (GET_CODE (pat) == PARALLEL)
9428         for (i = 0; i < XVECLEN (pat, 0); i++)
9429           XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9430       else
9431         XVECEXP (newpat, 0, 0) = pat;
9432
9433       add_clobbers (newpat, insn_code_number);
9434
9435       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9436            i < XVECLEN (newpat, 0); i++)
9437         {
9438           if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
9439               && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9440             return -1;
9441           notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9442                                      XEXP (XVECEXP (newpat, 0, i), 0), notes);
9443         }
9444       pat = newpat;
9445     }
9446
9447   *pnewpat = pat;
9448   *pnotes = notes;
9449
9450   return insn_code_number;
9451 }
9452 \f
9453 /* Like gen_lowpart_general but for use by combine.  In combine it
9454    is not possible to create any new pseudoregs.  However, it is
9455    safe to create invalid memory addresses, because combine will
9456    try to recognize them and all they will do is make the combine
9457    attempt fail.
9458
9459    If for some reason this cannot do its job, an rtx
9460    (clobber (const_int 0)) is returned.
9461    An insn containing that will not be recognized.  */
9462
9463 static rtx
9464 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
9465 {
9466   enum machine_mode imode = GET_MODE (x);
9467   unsigned int osize = GET_MODE_SIZE (omode);
9468   unsigned int isize = GET_MODE_SIZE (imode);
9469   rtx result;
9470
9471   if (omode == imode)
9472     return x;
9473
9474   /* Return identity if this is a CONST or symbolic reference.  */
9475   if (omode == Pmode
9476       && (GET_CODE (x) == CONST
9477           || GET_CODE (x) == SYMBOL_REF
9478           || GET_CODE (x) == LABEL_REF))
9479     return x;
9480
9481   /* We can only support MODE being wider than a word if X is a
9482      constant integer or has a mode the same size.  */
9483   if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
9484       && ! ((imode == VOIDmode
9485              && (GET_CODE (x) == CONST_INT
9486                  || GET_CODE (x) == CONST_DOUBLE))
9487             || isize == osize))
9488     goto fail;
9489
9490   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
9491      won't know what to do.  So we will strip off the SUBREG here and
9492      process normally.  */
9493   if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
9494     {
9495       x = SUBREG_REG (x);
9496
9497       /* For use in case we fall down into the address adjustments
9498          further below, we need to adjust the known mode and size of
9499          x; imode and isize, since we just adjusted x.  */
9500       imode = GET_MODE (x);
9501
9502       if (imode == omode)
9503         return x;
9504
9505       isize = GET_MODE_SIZE (imode);
9506     }
9507
9508   result = gen_lowpart_common (omode, x);
9509
9510 #ifdef CANNOT_CHANGE_MODE_CLASS
9511   if (result != 0 && GET_CODE (result) == SUBREG)
9512     record_subregs_of_mode (result);
9513 #endif
9514
9515   if (result)
9516     return result;
9517
9518   if (MEM_P (x))
9519     {
9520       int offset = 0;
9521
9522       /* Refuse to work on a volatile memory ref or one with a mode-dependent
9523          address.  */
9524       if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9525         goto fail;
9526
9527       /* If we want to refer to something bigger than the original memref,
9528          generate a paradoxical subreg instead.  That will force a reload
9529          of the original memref X.  */
9530       if (isize < osize)
9531         return gen_rtx_SUBREG (omode, x, 0);
9532
9533       if (WORDS_BIG_ENDIAN)
9534         offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
9535
9536       /* Adjust the address so that the address-after-the-data is
9537          unchanged.  */
9538       if (BYTES_BIG_ENDIAN)
9539         offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
9540
9541       return adjust_address_nv (x, omode, offset);
9542     }
9543
9544   /* If X is a comparison operator, rewrite it in a new mode.  This
9545      probably won't match, but may allow further simplifications.  */
9546   else if (COMPARISON_P (x))
9547     return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
9548
9549   /* If we couldn't simplify X any other way, just enclose it in a
9550      SUBREG.  Normally, this SUBREG won't match, but some patterns may
9551      include an explicit SUBREG or we may simplify it further in combine.  */
9552   else
9553     {
9554       int offset = 0;
9555       rtx res;
9556
9557       offset = subreg_lowpart_offset (omode, imode);
9558       if (imode == VOIDmode)
9559         {
9560           imode = int_mode_for_mode (omode);
9561           x = gen_lowpart_common (imode, x);
9562           if (x == NULL)
9563             goto fail;
9564         }
9565       res = simplify_gen_subreg (omode, x, imode, offset);
9566       if (res)
9567         return res;
9568     }
9569
9570  fail:
9571   return gen_rtx_CLOBBER (imode, const0_rtx);
9572 }
9573 \f
9574 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9575    comparison code that will be tested.
9576
9577    The result is a possibly different comparison code to use.  *POP0 and
9578    *POP1 may be updated.
9579
9580    It is possible that we might detect that a comparison is either always
9581    true or always false.  However, we do not perform general constant
9582    folding in combine, so this knowledge isn't useful.  Such tautologies
9583    should have been detected earlier.  Hence we ignore all such cases.  */
9584
9585 static enum rtx_code
9586 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
9587 {
9588   rtx op0 = *pop0;
9589   rtx op1 = *pop1;
9590   rtx tem, tem1;
9591   int i;
9592   enum machine_mode mode, tmode;
9593
9594   /* Try a few ways of applying the same transformation to both operands.  */
9595   while (1)
9596     {
9597 #ifndef WORD_REGISTER_OPERATIONS
9598       /* The test below this one won't handle SIGN_EXTENDs on these machines,
9599          so check specially.  */
9600       if (code != GTU && code != GEU && code != LTU && code != LEU
9601           && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9602           && GET_CODE (XEXP (op0, 0)) == ASHIFT
9603           && GET_CODE (XEXP (op1, 0)) == ASHIFT
9604           && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9605           && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9606           && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9607               == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9608           && GET_CODE (XEXP (op0, 1)) == CONST_INT
9609           && XEXP (op0, 1) == XEXP (op1, 1)
9610           && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
9611           && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
9612           && (INTVAL (XEXP (op0, 1))
9613               == (GET_MODE_BITSIZE (GET_MODE (op0))
9614                   - (GET_MODE_BITSIZE
9615                      (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9616         {
9617           op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9618           op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9619         }
9620 #endif
9621
9622       /* If both operands are the same constant shift, see if we can ignore the
9623          shift.  We can if the shift is a rotate or if the bits shifted out of
9624          this shift are known to be zero for both inputs and if the type of
9625          comparison is compatible with the shift.  */
9626       if (GET_CODE (op0) == GET_CODE (op1)
9627           && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9628           && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
9629               || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
9630                   && (code != GT && code != LT && code != GE && code != LE))
9631               || (GET_CODE (op0) == ASHIFTRT
9632                   && (code != GTU && code != LTU
9633                       && code != GEU && code != LEU)))
9634           && GET_CODE (XEXP (op0, 1)) == CONST_INT
9635           && INTVAL (XEXP (op0, 1)) >= 0
9636           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9637           && XEXP (op0, 1) == XEXP (op1, 1))
9638         {
9639           enum machine_mode mode = GET_MODE (op0);
9640           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9641           int shift_count = INTVAL (XEXP (op0, 1));
9642
9643           if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9644             mask &= (mask >> shift_count) << shift_count;
9645           else if (GET_CODE (op0) == ASHIFT)
9646             mask = (mask & (mask << shift_count)) >> shift_count;
9647
9648           if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
9649               && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
9650             op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9651           else
9652             break;
9653         }
9654
9655       /* If both operands are AND's of a paradoxical SUBREG by constant, the
9656          SUBREGs are of the same mode, and, in both cases, the AND would
9657          be redundant if the comparison was done in the narrower mode,
9658          do the comparison in the narrower mode (e.g., we are AND'ing with 1
9659          and the operand's possibly nonzero bits are 0xffffff01; in that case
9660          if we only care about QImode, we don't need the AND).  This case
9661          occurs if the output mode of an scc insn is not SImode and
9662          STORE_FLAG_VALUE == 1 (e.g., the 386).
9663
9664          Similarly, check for a case where the AND's are ZERO_EXTEND
9665          operations from some narrower mode even though a SUBREG is not
9666          present.  */
9667
9668       else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9669                && GET_CODE (XEXP (op0, 1)) == CONST_INT
9670                && GET_CODE (XEXP (op1, 1)) == CONST_INT)
9671         {
9672           rtx inner_op0 = XEXP (op0, 0);
9673           rtx inner_op1 = XEXP (op1, 0);
9674           HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
9675           HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
9676           int changed = 0;
9677
9678           if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
9679               && (GET_MODE_SIZE (GET_MODE (inner_op0))
9680                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
9681               && (GET_MODE (SUBREG_REG (inner_op0))
9682                   == GET_MODE (SUBREG_REG (inner_op1)))
9683               && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
9684                   <= HOST_BITS_PER_WIDE_INT)
9685               && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
9686                                              GET_MODE (SUBREG_REG (inner_op0)))))
9687               && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
9688                                              GET_MODE (SUBREG_REG (inner_op1))))))
9689             {
9690               op0 = SUBREG_REG (inner_op0);
9691               op1 = SUBREG_REG (inner_op1);
9692
9693               /* The resulting comparison is always unsigned since we masked
9694                  off the original sign bit.  */
9695               code = unsigned_condition (code);
9696
9697               changed = 1;
9698             }
9699
9700           else if (c0 == c1)
9701             for (tmode = GET_CLASS_NARROWEST_MODE
9702                  (GET_MODE_CLASS (GET_MODE (op0)));
9703                  tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
9704               if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
9705                 {
9706                   op0 = gen_lowpart (tmode, inner_op0);
9707                   op1 = gen_lowpart (tmode, inner_op1);
9708                   code = unsigned_condition (code);
9709                   changed = 1;
9710                   break;
9711                 }
9712
9713           if (! changed)
9714             break;
9715         }
9716
9717       /* If both operands are NOT, we can strip off the outer operation
9718          and adjust the comparison code for swapped operands; similarly for
9719          NEG, except that this must be an equality comparison.  */
9720       else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
9721                || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
9722                    && (code == EQ || code == NE)))
9723         op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
9724
9725       else
9726         break;
9727     }
9728
9729   /* If the first operand is a constant, swap the operands and adjust the
9730      comparison code appropriately, but don't do this if the second operand
9731      is already a constant integer.  */
9732   if (swap_commutative_operands_p (op0, op1))
9733     {
9734       tem = op0, op0 = op1, op1 = tem;
9735       code = swap_condition (code);
9736     }
9737
9738   /* We now enter a loop during which we will try to simplify the comparison.
9739      For the most part, we only are concerned with comparisons with zero,
9740      but some things may really be comparisons with zero but not start
9741      out looking that way.  */
9742
9743   while (GET_CODE (op1) == CONST_INT)
9744     {
9745       enum machine_mode mode = GET_MODE (op0);
9746       unsigned int mode_width = GET_MODE_BITSIZE (mode);
9747       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9748       int equality_comparison_p;
9749       int sign_bit_comparison_p;
9750       int unsigned_comparison_p;
9751       HOST_WIDE_INT const_op;
9752
9753       /* We only want to handle integral modes.  This catches VOIDmode,
9754          CCmode, and the floating-point modes.  An exception is that we
9755          can handle VOIDmode if OP0 is a COMPARE or a comparison
9756          operation.  */
9757
9758       if (GET_MODE_CLASS (mode) != MODE_INT
9759           && ! (mode == VOIDmode
9760                 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
9761         break;
9762
9763       /* Get the constant we are comparing against and turn off all bits
9764          not on in our mode.  */
9765       const_op = INTVAL (op1);
9766       if (mode != VOIDmode)
9767         const_op = trunc_int_for_mode (const_op, mode);
9768       op1 = GEN_INT (const_op);
9769
9770       /* If we are comparing against a constant power of two and the value
9771          being compared can only have that single bit nonzero (e.g., it was
9772          `and'ed with that bit), we can replace this with a comparison
9773          with zero.  */
9774       if (const_op
9775           && (code == EQ || code == NE || code == GE || code == GEU
9776               || code == LT || code == LTU)
9777           && mode_width <= HOST_BITS_PER_WIDE_INT
9778           && exact_log2 (const_op) >= 0
9779           && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
9780         {
9781           code = (code == EQ || code == GE || code == GEU ? NE : EQ);
9782           op1 = const0_rtx, const_op = 0;
9783         }
9784
9785       /* Similarly, if we are comparing a value known to be either -1 or
9786          0 with -1, change it to the opposite comparison against zero.  */
9787
9788       if (const_op == -1
9789           && (code == EQ || code == NE || code == GT || code == LE
9790               || code == GEU || code == LTU)
9791           && num_sign_bit_copies (op0, mode) == mode_width)
9792         {
9793           code = (code == EQ || code == LE || code == GEU ? NE : EQ);
9794           op1 = const0_rtx, const_op = 0;
9795         }
9796
9797       /* Do some canonicalizations based on the comparison code.  We prefer
9798          comparisons against zero and then prefer equality comparisons.
9799          If we can reduce the size of a constant, we will do that too.  */
9800
9801       switch (code)
9802         {
9803         case LT:
9804           /* < C is equivalent to <= (C - 1) */
9805           if (const_op > 0)
9806             {
9807               const_op -= 1;
9808               op1 = GEN_INT (const_op);
9809               code = LE;
9810               /* ... fall through to LE case below.  */
9811             }
9812           else
9813             break;
9814
9815         case LE:
9816           /* <= C is equivalent to < (C + 1); we do this for C < 0  */
9817           if (const_op < 0)
9818             {
9819               const_op += 1;
9820               op1 = GEN_INT (const_op);
9821               code = LT;
9822             }
9823
9824           /* If we are doing a <= 0 comparison on a value known to have
9825              a zero sign bit, we can replace this with == 0.  */
9826           else if (const_op == 0
9827                    && mode_width <= HOST_BITS_PER_WIDE_INT
9828                    && (nonzero_bits (op0, mode)
9829                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9830             code = EQ;
9831           break;
9832
9833         case GE:
9834           /* >= C is equivalent to > (C - 1).  */
9835           if (const_op > 0)
9836             {
9837               const_op -= 1;
9838               op1 = GEN_INT (const_op);
9839               code = GT;
9840               /* ... fall through to GT below.  */
9841             }
9842           else
9843             break;
9844
9845         case GT:
9846           /* > C is equivalent to >= (C + 1); we do this for C < 0.  */
9847           if (const_op < 0)
9848             {
9849               const_op += 1;
9850               op1 = GEN_INT (const_op);
9851               code = GE;
9852             }
9853
9854           /* If we are doing a > 0 comparison on a value known to have
9855              a zero sign bit, we can replace this with != 0.  */
9856           else if (const_op == 0
9857                    && mode_width <= HOST_BITS_PER_WIDE_INT
9858                    && (nonzero_bits (op0, mode)
9859                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9860             code = NE;
9861           break;
9862
9863         case LTU:
9864           /* < C is equivalent to <= (C - 1).  */
9865           if (const_op > 0)
9866             {
9867               const_op -= 1;
9868               op1 = GEN_INT (const_op);
9869               code = LEU;
9870               /* ... fall through ...  */
9871             }
9872
9873           /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
9874           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9875                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9876             {
9877               const_op = 0, op1 = const0_rtx;
9878               code = GE;
9879               break;
9880             }
9881           else
9882             break;
9883
9884         case LEU:
9885           /* unsigned <= 0 is equivalent to == 0 */
9886           if (const_op == 0)
9887             code = EQ;
9888
9889           /* (unsigned) <= 0x7fffffff is equivalent to >= 0.  */
9890           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9891                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9892             {
9893               const_op = 0, op1 = const0_rtx;
9894               code = GE;
9895             }
9896           break;
9897
9898         case GEU:
9899           /* >= C is equivalent to > (C - 1).  */
9900           if (const_op > 1)
9901             {
9902               const_op -= 1;
9903               op1 = GEN_INT (const_op);
9904               code = GTU;
9905               /* ... fall through ...  */
9906             }
9907
9908           /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
9909           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9910                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9911             {
9912               const_op = 0, op1 = const0_rtx;
9913               code = LT;
9914               break;
9915             }
9916           else
9917             break;
9918
9919         case GTU:
9920           /* unsigned > 0 is equivalent to != 0 */
9921           if (const_op == 0)
9922             code = NE;
9923
9924           /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
9925           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9926                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9927             {
9928               const_op = 0, op1 = const0_rtx;
9929               code = LT;
9930             }
9931           break;
9932
9933         default:
9934           break;
9935         }
9936
9937       /* Compute some predicates to simplify code below.  */
9938
9939       equality_comparison_p = (code == EQ || code == NE);
9940       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
9941       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
9942                                || code == GEU);
9943
9944       /* If this is a sign bit comparison and we can do arithmetic in
9945          MODE, say that we will only be needing the sign bit of OP0.  */
9946       if (sign_bit_comparison_p
9947           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9948         op0 = force_to_mode (op0, mode,
9949                              ((HOST_WIDE_INT) 1
9950                               << (GET_MODE_BITSIZE (mode) - 1)),
9951                              NULL_RTX, 0);
9952
9953       /* Now try cases based on the opcode of OP0.  If none of the cases
9954          does a "continue", we exit this loop immediately after the
9955          switch.  */
9956
9957       switch (GET_CODE (op0))
9958         {
9959         case ZERO_EXTRACT:
9960           /* If we are extracting a single bit from a variable position in
9961              a constant that has only a single bit set and are comparing it
9962              with zero, we can convert this into an equality comparison
9963              between the position and the location of the single bit.  */
9964           /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
9965              have already reduced the shift count modulo the word size.  */
9966           if (!SHIFT_COUNT_TRUNCATED
9967               && GET_CODE (XEXP (op0, 0)) == CONST_INT
9968               && XEXP (op0, 1) == const1_rtx
9969               && equality_comparison_p && const_op == 0
9970               && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
9971             {
9972               if (BITS_BIG_ENDIAN)
9973                 {
9974                   enum machine_mode new_mode
9975                     = mode_for_extraction (EP_extzv, 1);
9976                   if (new_mode == MAX_MACHINE_MODE)
9977                     i = BITS_PER_WORD - 1 - i;
9978                   else
9979                     {
9980                       mode = new_mode;
9981                       i = (GET_MODE_BITSIZE (mode) - 1 - i);
9982                     }
9983                 }
9984
9985               op0 = XEXP (op0, 2);
9986               op1 = GEN_INT (i);
9987               const_op = i;
9988
9989               /* Result is nonzero iff shift count is equal to I.  */
9990               code = reverse_condition (code);
9991               continue;
9992             }
9993
9994           /* ... fall through ...  */
9995
9996         case SIGN_EXTRACT:
9997           tem = expand_compound_operation (op0);
9998           if (tem != op0)
9999             {
10000               op0 = tem;
10001               continue;
10002             }
10003           break;
10004
10005         case NOT:
10006           /* If testing for equality, we can take the NOT of the constant.  */
10007           if (equality_comparison_p
10008               && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10009             {
10010               op0 = XEXP (op0, 0);
10011               op1 = tem;
10012               continue;
10013             }
10014
10015           /* If just looking at the sign bit, reverse the sense of the
10016              comparison.  */
10017           if (sign_bit_comparison_p)
10018             {
10019               op0 = XEXP (op0, 0);
10020               code = (code == GE ? LT : GE);
10021               continue;
10022             }
10023           break;
10024
10025         case NEG:
10026           /* If testing for equality, we can take the NEG of the constant.  */
10027           if (equality_comparison_p
10028               && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10029             {
10030               op0 = XEXP (op0, 0);
10031               op1 = tem;
10032               continue;
10033             }
10034
10035           /* The remaining cases only apply to comparisons with zero.  */
10036           if (const_op != 0)
10037             break;
10038
10039           /* When X is ABS or is known positive,
10040              (neg X) is < 0 if and only if X != 0.  */
10041
10042           if (sign_bit_comparison_p
10043               && (GET_CODE (XEXP (op0, 0)) == ABS
10044                   || (mode_width <= HOST_BITS_PER_WIDE_INT
10045                       && (nonzero_bits (XEXP (op0, 0), mode)
10046                           & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10047             {
10048               op0 = XEXP (op0, 0);
10049               code = (code == LT ? NE : EQ);
10050               continue;
10051             }
10052
10053           /* If we have NEG of something whose two high-order bits are the
10054              same, we know that "(-a) < 0" is equivalent to "a > 0".  */
10055           if (num_sign_bit_copies (op0, mode) >= 2)
10056             {
10057               op0 = XEXP (op0, 0);
10058               code = swap_condition (code);
10059               continue;
10060             }
10061           break;
10062
10063         case ROTATE:
10064           /* If we are testing equality and our count is a constant, we
10065              can perform the inverse operation on our RHS.  */
10066           if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10067               && (tem = simplify_binary_operation (ROTATERT, mode,
10068                                                    op1, XEXP (op0, 1))) != 0)
10069             {
10070               op0 = XEXP (op0, 0);
10071               op1 = tem;
10072               continue;
10073             }
10074
10075           /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10076              a particular bit.  Convert it to an AND of a constant of that
10077              bit.  This will be converted into a ZERO_EXTRACT.  */
10078           if (const_op == 0 && sign_bit_comparison_p
10079               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10080               && mode_width <= HOST_BITS_PER_WIDE_INT)
10081             {
10082               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10083                                             ((HOST_WIDE_INT) 1
10084                                              << (mode_width - 1
10085                                                  - INTVAL (XEXP (op0, 1)))));
10086               code = (code == LT ? NE : EQ);
10087               continue;
10088             }
10089
10090           /* Fall through.  */
10091
10092         case ABS:
10093           /* ABS is ignorable inside an equality comparison with zero.  */
10094           if (const_op == 0 && equality_comparison_p)
10095             {
10096               op0 = XEXP (op0, 0);
10097               continue;
10098             }
10099           break;
10100
10101         case SIGN_EXTEND:
10102           /* Can simplify (compare (zero/sign_extend FOO) CONST) to
10103              (compare FOO CONST) if CONST fits in FOO's mode and we
10104              are either testing inequality or have an unsigned
10105              comparison with ZERO_EXTEND or a signed comparison with
10106              SIGN_EXTEND.  But don't do it if we don't have a compare
10107              insn of the given mode, since we'd have to revert it
10108              later on, and then we wouldn't know whether to sign- or
10109              zero-extend.  */
10110           mode = GET_MODE (XEXP (op0, 0));
10111           if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10112               && ! unsigned_comparison_p
10113               && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10114               && ((unsigned HOST_WIDE_INT) const_op
10115                   < (((unsigned HOST_WIDE_INT) 1 
10116                       << (GET_MODE_BITSIZE (mode) - 1))))
10117               && cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
10118             {
10119               op0 = XEXP (op0, 0);
10120               continue;
10121             }
10122           break;
10123
10124         case SUBREG:
10125           /* Check for the case where we are comparing A - C1 with C2, that is
10126
10127                (subreg:MODE (plus (A) (-C1))) op (C2)
10128
10129              with C1 a constant, and try to lift the SUBREG, i.e. to do the
10130              comparison in the wider mode.  One of the following two conditions
10131              must be true in order for this to be valid:
10132
10133                1. The mode extension results in the same bit pattern being added
10134                   on both sides and the comparison is equality or unsigned.  As
10135                   C2 has been truncated to fit in MODE, the pattern can only be
10136                   all 0s or all 1s.
10137
10138                2. The mode extension results in the sign bit being copied on
10139                   each side.
10140
10141              The difficulty here is that we have predicates for A but not for
10142              (A - C1) so we need to check that C1 is within proper bounds so
10143              as to perturbate A as little as possible.  */
10144
10145           if (mode_width <= HOST_BITS_PER_WIDE_INT
10146               && subreg_lowpart_p (op0)
10147               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
10148               && GET_CODE (SUBREG_REG (op0)) == PLUS
10149               && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT)
10150             {
10151               enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
10152               rtx a = XEXP (SUBREG_REG (op0), 0);
10153               HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
10154
10155               if ((c1 > 0
10156                    && (unsigned HOST_WIDE_INT) c1
10157                        < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
10158                    && (equality_comparison_p || unsigned_comparison_p)
10159                    /* (A - C1) zero-extends if it is positive and sign-extends
10160                       if it is negative, C2 both zero- and sign-extends.  */
10161                    && ((0 == (nonzero_bits (a, inner_mode)
10162                               & ~GET_MODE_MASK (mode))
10163                         && const_op >= 0)
10164                        /* (A - C1) sign-extends if it is positive and 1-extends
10165                           if it is negative, C2 both sign- and 1-extends.  */
10166                        || (num_sign_bit_copies (a, inner_mode)
10167                            > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10168                                              - mode_width)
10169                            && const_op < 0)))
10170                   || ((unsigned HOST_WIDE_INT) c1
10171                        < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
10172                       /* (A - C1) always sign-extends, like C2.  */
10173                       && num_sign_bit_copies (a, inner_mode)
10174                          > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10175                                            - mode_width - 1)))
10176                 {
10177                   op0 = SUBREG_REG (op0);
10178                   continue;
10179                 }
10180             }
10181
10182           /* If the inner mode is narrower and we are extracting the low part,
10183              we can treat the SUBREG as if it were a ZERO_EXTEND.  */
10184           if (subreg_lowpart_p (op0)
10185               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10186             /* Fall through */ ;
10187           else
10188             break;
10189
10190           /* ... fall through ...  */
10191
10192         case ZERO_EXTEND:
10193           mode = GET_MODE (XEXP (op0, 0));
10194           if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10195               && (unsigned_comparison_p || equality_comparison_p)
10196               && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10197               && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
10198               && cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
10199             {
10200               op0 = XEXP (op0, 0);
10201               continue;
10202             }
10203           break;
10204
10205         case PLUS:
10206           /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
10207              this for equality comparisons due to pathological cases involving
10208              overflows.  */
10209           if (equality_comparison_p
10210               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10211                                                         op1, XEXP (op0, 1))))
10212             {
10213               op0 = XEXP (op0, 0);
10214               op1 = tem;
10215               continue;
10216             }
10217
10218           /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
10219           if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10220               && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10221             {
10222               op0 = XEXP (XEXP (op0, 0), 0);
10223               code = (code == LT ? EQ : NE);
10224               continue;
10225             }
10226           break;
10227
10228         case MINUS:
10229           /* We used to optimize signed comparisons against zero, but that
10230              was incorrect.  Unsigned comparisons against zero (GTU, LEU)
10231              arrive here as equality comparisons, or (GEU, LTU) are
10232              optimized away.  No need to special-case them.  */
10233
10234           /* (eq (minus A B) C) -> (eq A (plus B C)) or
10235              (eq B (minus A C)), whichever simplifies.  We can only do
10236              this for equality comparisons due to pathological cases involving
10237              overflows.  */
10238           if (equality_comparison_p
10239               && 0 != (tem = simplify_binary_operation (PLUS, mode,
10240                                                         XEXP (op0, 1), op1)))
10241             {
10242               op0 = XEXP (op0, 0);
10243               op1 = tem;
10244               continue;
10245             }
10246
10247           if (equality_comparison_p
10248               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10249                                                         XEXP (op0, 0), op1)))
10250             {
10251               op0 = XEXP (op0, 1);
10252               op1 = tem;
10253               continue;
10254             }
10255
10256           /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10257              of bits in X minus 1, is one iff X > 0.  */
10258           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10259               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10260               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10261                  == mode_width - 1
10262               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10263             {
10264               op0 = XEXP (op0, 1);
10265               code = (code == GE ? LE : GT);
10266               continue;
10267             }
10268           break;
10269
10270         case XOR:
10271           /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
10272              if C is zero or B is a constant.  */
10273           if (equality_comparison_p
10274               && 0 != (tem = simplify_binary_operation (XOR, mode,
10275                                                         XEXP (op0, 1), op1)))
10276             {
10277               op0 = XEXP (op0, 0);
10278               op1 = tem;
10279               continue;
10280             }
10281           break;
10282
10283         case EQ:  case NE:
10284         case UNEQ:  case LTGT:
10285         case LT:  case LTU:  case UNLT:  case LE:  case LEU:  case UNLE:
10286         case GT:  case GTU:  case UNGT:  case GE:  case GEU:  case UNGE:
10287         case UNORDERED: case ORDERED:
10288           /* We can't do anything if OP0 is a condition code value, rather
10289              than an actual data value.  */
10290           if (const_op != 0
10291               || CC0_P (XEXP (op0, 0))
10292               || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10293             break;
10294
10295           /* Get the two operands being compared.  */
10296           if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10297             tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10298           else
10299             tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10300
10301           /* Check for the cases where we simply want the result of the
10302              earlier test or the opposite of that result.  */
10303           if (code == NE || code == EQ
10304               || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10305                   && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10306                   && (STORE_FLAG_VALUE
10307                       & (((HOST_WIDE_INT) 1
10308                           << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10309                   && (code == LT || code == GE)))
10310             {
10311               enum rtx_code new_code;
10312               if (code == LT || code == NE)
10313                 new_code = GET_CODE (op0);
10314               else
10315                 new_code = reversed_comparison_code (op0, NULL);
10316
10317               if (new_code != UNKNOWN)
10318                 {
10319                   code = new_code;
10320                   op0 = tem;
10321                   op1 = tem1;
10322                   continue;
10323                 }
10324             }
10325           break;
10326
10327         case IOR:
10328           /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10329              iff X <= 0.  */
10330           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10331               && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10332               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10333             {
10334               op0 = XEXP (op0, 1);
10335               code = (code == GE ? GT : LE);
10336               continue;
10337             }
10338           break;
10339
10340         case AND:
10341           /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
10342              will be converted to a ZERO_EXTRACT later.  */
10343           if (const_op == 0 && equality_comparison_p
10344               && GET_CODE (XEXP (op0, 0)) == ASHIFT
10345               && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10346             {
10347               op0 = simplify_and_const_int
10348                 (op0, mode, gen_rtx_LSHIFTRT (mode,
10349                                               XEXP (op0, 1),
10350                                               XEXP (XEXP (op0, 0), 1)),
10351                  (HOST_WIDE_INT) 1);
10352               continue;
10353             }
10354
10355           /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10356              zero and X is a comparison and C1 and C2 describe only bits set
10357              in STORE_FLAG_VALUE, we can compare with X.  */
10358           if (const_op == 0 && equality_comparison_p
10359               && mode_width <= HOST_BITS_PER_WIDE_INT
10360               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10361               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10362               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10363               && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10364               && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10365             {
10366               mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10367                       << INTVAL (XEXP (XEXP (op0, 0), 1)));
10368               if ((~STORE_FLAG_VALUE & mask) == 0
10369                   && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
10370                       || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10371                           && COMPARISON_P (tem))))
10372                 {
10373                   op0 = XEXP (XEXP (op0, 0), 0);
10374                   continue;
10375                 }
10376             }
10377
10378           /* If we are doing an equality comparison of an AND of a bit equal
10379              to the sign bit, replace this with a LT or GE comparison of
10380              the underlying value.  */
10381           if (equality_comparison_p
10382               && const_op == 0
10383               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10384               && mode_width <= HOST_BITS_PER_WIDE_INT
10385               && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10386                   == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10387             {
10388               op0 = XEXP (op0, 0);
10389               code = (code == EQ ? GE : LT);
10390               continue;
10391             }
10392
10393           /* If this AND operation is really a ZERO_EXTEND from a narrower
10394              mode, the constant fits within that mode, and this is either an
10395              equality or unsigned comparison, try to do this comparison in
10396              the narrower mode.  */
10397           if ((equality_comparison_p || unsigned_comparison_p)
10398               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10399               && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10400                                    & GET_MODE_MASK (mode))
10401                                   + 1)) >= 0
10402               && const_op >> i == 0
10403               && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
10404             {
10405               op0 = gen_lowpart (tmode, XEXP (op0, 0));
10406               continue;
10407             }
10408
10409           /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
10410              fits in both M1 and M2 and the SUBREG is either paradoxical
10411              or represents the low part, permute the SUBREG and the AND
10412              and try again.  */
10413           if (GET_CODE (XEXP (op0, 0)) == SUBREG)
10414             {
10415               unsigned HOST_WIDE_INT c1;
10416               tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
10417               /* Require an integral mode, to avoid creating something like
10418                  (AND:SF ...).  */
10419               if (SCALAR_INT_MODE_P (tmode)
10420                   /* It is unsafe to commute the AND into the SUBREG if the
10421                      SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
10422                      not defined.  As originally written the upper bits
10423                      have a defined value due to the AND operation.
10424                      However, if we commute the AND inside the SUBREG then
10425                      they no longer have defined values and the meaning of
10426                      the code has been changed.  */
10427                   && (0
10428 #ifdef WORD_REGISTER_OPERATIONS
10429                       || (mode_width > GET_MODE_BITSIZE (tmode)
10430                           && mode_width <= BITS_PER_WORD)
10431 #endif
10432                       || (mode_width <= GET_MODE_BITSIZE (tmode)
10433                           && subreg_lowpart_p (XEXP (op0, 0))))
10434                   && GET_CODE (XEXP (op0, 1)) == CONST_INT
10435                   && mode_width <= HOST_BITS_PER_WIDE_INT
10436                   && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
10437                   && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
10438                   && (c1 & ~GET_MODE_MASK (tmode)) == 0
10439                   && c1 != mask
10440                   && c1 != GET_MODE_MASK (tmode))
10441                 {
10442                   op0 = simplify_gen_binary (AND, tmode,
10443                                              SUBREG_REG (XEXP (op0, 0)),
10444                                              gen_int_mode (c1, tmode));
10445                   op0 = gen_lowpart (mode, op0);
10446                   continue;
10447                 }
10448             }
10449
10450           /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0).  */
10451           if (const_op == 0 && equality_comparison_p
10452               && XEXP (op0, 1) == const1_rtx
10453               && GET_CODE (XEXP (op0, 0)) == NOT)
10454             {
10455               op0 = simplify_and_const_int
10456                 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
10457               code = (code == NE ? EQ : NE);
10458               continue;
10459             }
10460
10461           /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
10462              (eq (and (lshiftrt X) 1) 0).
10463              Also handle the case where (not X) is expressed using xor.  */
10464           if (const_op == 0 && equality_comparison_p
10465               && XEXP (op0, 1) == const1_rtx
10466               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
10467             {
10468               rtx shift_op = XEXP (XEXP (op0, 0), 0);
10469               rtx shift_count = XEXP (XEXP (op0, 0), 1);
10470
10471               if (GET_CODE (shift_op) == NOT
10472                   || (GET_CODE (shift_op) == XOR
10473                       && GET_CODE (XEXP (shift_op, 1)) == CONST_INT
10474                       && GET_CODE (shift_count) == CONST_INT
10475                       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10476                       && (INTVAL (XEXP (shift_op, 1))
10477                           == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
10478                 {
10479                   op0 = simplify_and_const_int
10480                     (NULL_RTX, mode,
10481                      gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
10482                      (HOST_WIDE_INT) 1);
10483                   code = (code == NE ? EQ : NE);
10484                   continue;
10485                 }
10486             }
10487           break;
10488
10489         case ASHIFT:
10490           /* If we have (compare (ashift FOO N) (const_int C)) and
10491              the high order N bits of FOO (N+1 if an inequality comparison)
10492              are known to be zero, we can do this by comparing FOO with C
10493              shifted right N bits so long as the low-order N bits of C are
10494              zero.  */
10495           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10496               && INTVAL (XEXP (op0, 1)) >= 0
10497               && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10498                   < HOST_BITS_PER_WIDE_INT)
10499               && ((const_op
10500                    & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10501               && mode_width <= HOST_BITS_PER_WIDE_INT
10502               && (nonzero_bits (XEXP (op0, 0), mode)
10503                   & ~(mask >> (INTVAL (XEXP (op0, 1))
10504                                + ! equality_comparison_p))) == 0)
10505             {
10506               /* We must perform a logical shift, not an arithmetic one,
10507                  as we want the top N bits of C to be zero.  */
10508               unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
10509
10510               temp >>= INTVAL (XEXP (op0, 1));
10511               op1 = gen_int_mode (temp, mode);
10512               op0 = XEXP (op0, 0);
10513               continue;
10514             }
10515
10516           /* If we are doing a sign bit comparison, it means we are testing
10517              a particular bit.  Convert it to the appropriate AND.  */
10518           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10519               && mode_width <= HOST_BITS_PER_WIDE_INT)
10520             {
10521               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10522                                             ((HOST_WIDE_INT) 1
10523                                              << (mode_width - 1
10524                                                  - INTVAL (XEXP (op0, 1)))));
10525               code = (code == LT ? NE : EQ);
10526               continue;
10527             }
10528
10529           /* If this an equality comparison with zero and we are shifting
10530              the low bit to the sign bit, we can convert this to an AND of the
10531              low-order bit.  */
10532           if (const_op == 0 && equality_comparison_p
10533               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10534               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10535                  == mode_width - 1)
10536             {
10537               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10538                                             (HOST_WIDE_INT) 1);
10539               continue;
10540             }
10541           break;
10542
10543         case ASHIFTRT:
10544           /* If this is an equality comparison with zero, we can do this
10545              as a logical shift, which might be much simpler.  */
10546           if (equality_comparison_p && const_op == 0
10547               && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10548             {
10549               op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10550                                           XEXP (op0, 0),
10551                                           INTVAL (XEXP (op0, 1)));
10552               continue;
10553             }
10554
10555           /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10556              do the comparison in a narrower mode.  */
10557           if (! unsigned_comparison_p
10558               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10559               && GET_CODE (XEXP (op0, 0)) == ASHIFT
10560               && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10561               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10562                                          MODE_INT, 1)) != BLKmode
10563               && (((unsigned HOST_WIDE_INT) const_op
10564                    + (GET_MODE_MASK (tmode) >> 1) + 1)
10565                   <= GET_MODE_MASK (tmode)))
10566             {
10567               op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
10568               continue;
10569             }
10570
10571           /* Likewise if OP0 is a PLUS of a sign extension with a
10572              constant, which is usually represented with the PLUS
10573              between the shifts.  */
10574           if (! unsigned_comparison_p
10575               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10576               && GET_CODE (XEXP (op0, 0)) == PLUS
10577               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10578               && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
10579               && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
10580               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10581                                          MODE_INT, 1)) != BLKmode
10582               && (((unsigned HOST_WIDE_INT) const_op
10583                    + (GET_MODE_MASK (tmode) >> 1) + 1)
10584                   <= GET_MODE_MASK (tmode)))
10585             {
10586               rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
10587               rtx add_const = XEXP (XEXP (op0, 0), 1);
10588               rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
10589                                                    add_const, XEXP (op0, 1));
10590
10591               op0 = simplify_gen_binary (PLUS, tmode,
10592                                          gen_lowpart (tmode, inner),
10593                                          new_const);
10594               continue;
10595             }
10596
10597           /* ... fall through ...  */
10598         case LSHIFTRT:
10599           /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10600              the low order N bits of FOO are known to be zero, we can do this
10601              by comparing FOO with C shifted left N bits so long as no
10602              overflow occurs.  */
10603           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10604               && INTVAL (XEXP (op0, 1)) >= 0
10605               && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10606               && mode_width <= HOST_BITS_PER_WIDE_INT
10607               && (nonzero_bits (XEXP (op0, 0), mode)
10608                   & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
10609               && (((unsigned HOST_WIDE_INT) const_op
10610                    + (GET_CODE (op0) != LSHIFTRT
10611                       ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
10612                          + 1)
10613                       : 0))
10614                   <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
10615             {
10616               /* If the shift was logical, then we must make the condition
10617                  unsigned.  */
10618               if (GET_CODE (op0) == LSHIFTRT)
10619                 code = unsigned_condition (code);
10620
10621               const_op <<= INTVAL (XEXP (op0, 1));
10622               op1 = GEN_INT (const_op);
10623               op0 = XEXP (op0, 0);
10624               continue;
10625             }
10626
10627           /* If we are using this shift to extract just the sign bit, we
10628              can replace this with an LT or GE comparison.  */
10629           if (const_op == 0
10630               && (equality_comparison_p || sign_bit_comparison_p)
10631               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10632               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10633                  == mode_width - 1)
10634             {
10635               op0 = XEXP (op0, 0);
10636               code = (code == NE || code == GT ? LT : GE);
10637               continue;
10638             }
10639           break;
10640
10641         default:
10642           break;
10643         }
10644
10645       break;
10646     }
10647
10648   /* Now make any compound operations involved in this comparison.  Then,
10649      check for an outmost SUBREG on OP0 that is not doing anything or is
10650      paradoxical.  The latter transformation must only be performed when
10651      it is known that the "extra" bits will be the same in op0 and op1 or
10652      that they don't matter.  There are three cases to consider:
10653
10654      1. SUBREG_REG (op0) is a register.  In this case the bits are don't
10655      care bits and we can assume they have any convenient value.  So
10656      making the transformation is safe.
10657
10658      2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
10659      In this case the upper bits of op0 are undefined.  We should not make
10660      the simplification in that case as we do not know the contents of
10661      those bits.
10662
10663      3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
10664      UNKNOWN.  In that case we know those bits are zeros or ones.  We must
10665      also be sure that they are the same as the upper bits of op1.
10666
10667      We can never remove a SUBREG for a non-equality comparison because
10668      the sign bit is in a different place in the underlying object.  */
10669
10670   op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
10671   op1 = make_compound_operation (op1, SET);
10672
10673   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10674       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10675       && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
10676       && (code == NE || code == EQ))
10677     {
10678       if (GET_MODE_SIZE (GET_MODE (op0))
10679           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
10680         {
10681           /* For paradoxical subregs, allow case 1 as above.  Case 3 isn't
10682              implemented.  */
10683           if (REG_P (SUBREG_REG (op0)))
10684             {
10685               op0 = SUBREG_REG (op0);
10686               op1 = gen_lowpart (GET_MODE (op0), op1);
10687             }
10688         }
10689       else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10690                 <= HOST_BITS_PER_WIDE_INT)
10691                && (nonzero_bits (SUBREG_REG (op0),
10692                                  GET_MODE (SUBREG_REG (op0)))
10693                    & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
10694         {
10695           tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
10696
10697           if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
10698                & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
10699             op0 = SUBREG_REG (op0), op1 = tem;
10700         }
10701     }
10702
10703   /* We now do the opposite procedure: Some machines don't have compare
10704      insns in all modes.  If OP0's mode is an integer mode smaller than a
10705      word and we can't do a compare in that mode, see if there is a larger
10706      mode for which we can do the compare.  There are a number of cases in
10707      which we can use the wider mode.  */
10708
10709   mode = GET_MODE (op0);
10710   if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10711       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
10712       && ! have_insn_for (COMPARE, mode))
10713     for (tmode = GET_MODE_WIDER_MODE (mode);
10714          (tmode != VOIDmode
10715           && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
10716          tmode = GET_MODE_WIDER_MODE (tmode))
10717       if (have_insn_for (COMPARE, tmode))
10718         {
10719           int zero_extended;
10720
10721           /* If the only nonzero bits in OP0 and OP1 are those in the
10722              narrower mode and this is an equality or unsigned comparison,
10723              we can use the wider mode.  Similarly for sign-extended
10724              values, in which case it is true for all comparisons.  */
10725           zero_extended = ((code == EQ || code == NE
10726                             || code == GEU || code == GTU
10727                             || code == LEU || code == LTU)
10728                            && (nonzero_bits (op0, tmode)
10729                                & ~GET_MODE_MASK (mode)) == 0
10730                            && ((GET_CODE (op1) == CONST_INT
10731                                 || (nonzero_bits (op1, tmode)
10732                                     & ~GET_MODE_MASK (mode)) == 0)));
10733
10734           if (zero_extended
10735               || ((num_sign_bit_copies (op0, tmode)
10736                    > (unsigned int) (GET_MODE_BITSIZE (tmode)
10737                                      - GET_MODE_BITSIZE (mode)))
10738                   && (num_sign_bit_copies (op1, tmode)
10739                       > (unsigned int) (GET_MODE_BITSIZE (tmode)
10740                                         - GET_MODE_BITSIZE (mode)))))
10741             {
10742               /* If OP0 is an AND and we don't have an AND in MODE either,
10743                  make a new AND in the proper mode.  */
10744               if (GET_CODE (op0) == AND
10745                   && !have_insn_for (AND, mode))
10746                 op0 = simplify_gen_binary (AND, tmode,
10747                                            gen_lowpart (tmode,
10748                                                         XEXP (op0, 0)),
10749                                            gen_lowpart (tmode,
10750                                                         XEXP (op0, 1)));
10751
10752               op0 = gen_lowpart (tmode, op0);
10753               if (zero_extended && GET_CODE (op1) == CONST_INT)
10754                 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
10755               op1 = gen_lowpart (tmode, op1);
10756               break;
10757             }
10758
10759           /* If this is a test for negative, we can make an explicit
10760              test of the sign bit.  */
10761
10762           if (op1 == const0_rtx && (code == LT || code == GE)
10763               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10764             {
10765               op0 = simplify_gen_binary (AND, tmode,
10766                                          gen_lowpart (tmode, op0),
10767                                          GEN_INT ((HOST_WIDE_INT) 1
10768                                                   << (GET_MODE_BITSIZE (mode)
10769                                                       - 1)));
10770               code = (code == LT) ? NE : EQ;
10771               break;
10772             }
10773         }
10774
10775 #ifdef CANONICALIZE_COMPARISON
10776   /* If this machine only supports a subset of valid comparisons, see if we
10777      can convert an unsupported one into a supported one.  */
10778   CANONICALIZE_COMPARISON (code, op0, op1);
10779 #endif
10780
10781   *pop0 = op0;
10782   *pop1 = op1;
10783
10784   return code;
10785 }
10786 \f
10787 /* Utility function for record_value_for_reg.  Count number of
10788    rtxs in X.  */
10789 static int
10790 count_rtxs (rtx x)
10791 {
10792   enum rtx_code code = GET_CODE (x);
10793   const char *fmt;
10794   int i, ret = 1;
10795
10796   if (GET_RTX_CLASS (code) == '2'
10797       || GET_RTX_CLASS (code) == 'c')
10798     {
10799       rtx x0 = XEXP (x, 0);
10800       rtx x1 = XEXP (x, 1);
10801
10802       if (x0 == x1)
10803         return 1 + 2 * count_rtxs (x0);
10804
10805       if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
10806            || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
10807           && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
10808         return 2 + 2 * count_rtxs (x0)
10809                + count_rtxs (x == XEXP (x1, 0)
10810                              ? XEXP (x1, 1) : XEXP (x1, 0));
10811
10812       if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
10813            || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
10814           && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
10815         return 2 + 2 * count_rtxs (x1)
10816                + count_rtxs (x == XEXP (x0, 0)
10817                              ? XEXP (x0, 1) : XEXP (x0, 0));
10818     }
10819
10820   fmt = GET_RTX_FORMAT (code);
10821   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10822     if (fmt[i] == 'e')
10823       ret += count_rtxs (XEXP (x, i));
10824
10825   return ret;
10826 }
10827 \f
10828 /* Utility function for following routine.  Called when X is part of a value
10829    being stored into last_set_value.  Sets last_set_table_tick
10830    for each register mentioned.  Similar to mention_regs in cse.c  */
10831
10832 static void
10833 update_table_tick (rtx x)
10834 {
10835   enum rtx_code code = GET_CODE (x);
10836   const char *fmt = GET_RTX_FORMAT (code);
10837   int i;
10838
10839   if (code == REG)
10840     {
10841       unsigned int regno = REGNO (x);
10842       unsigned int endregno
10843         = regno + (regno < FIRST_PSEUDO_REGISTER
10844                    ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
10845       unsigned int r;
10846
10847       for (r = regno; r < endregno; r++)
10848         reg_stat[r].last_set_table_tick = label_tick;
10849
10850       return;
10851     }
10852
10853   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10854     /* Note that we can't have an "E" in values stored; see
10855        get_last_value_validate.  */
10856     if (fmt[i] == 'e')
10857       {
10858         /* Check for identical subexpressions.  If x contains
10859            identical subexpression we only have to traverse one of
10860            them.  */
10861         if (i == 0 && ARITHMETIC_P (x))
10862           {
10863             /* Note that at this point x1 has already been
10864                processed.  */
10865             rtx x0 = XEXP (x, 0);
10866             rtx x1 = XEXP (x, 1);
10867
10868             /* If x0 and x1 are identical then there is no need to
10869                process x0.  */
10870             if (x0 == x1)
10871               break;
10872
10873             /* If x0 is identical to a subexpression of x1 then while
10874                processing x1, x0 has already been processed.  Thus we
10875                are done with x.  */
10876             if (ARITHMETIC_P (x1)
10877                 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
10878               break;
10879
10880             /* If x1 is identical to a subexpression of x0 then we
10881                still have to process the rest of x0.  */
10882             if (ARITHMETIC_P (x0)
10883                 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
10884               {
10885                 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
10886                 break;
10887               }
10888           }
10889
10890         update_table_tick (XEXP (x, i));
10891       }
10892 }
10893
10894 /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
10895    are saying that the register is clobbered and we no longer know its
10896    value.  If INSN is zero, don't update reg_stat[].last_set; this is
10897    only permitted with VALUE also zero and is used to invalidate the
10898    register.  */
10899
10900 static void
10901 record_value_for_reg (rtx reg, rtx insn, rtx value)
10902 {
10903   unsigned int regno = REGNO (reg);
10904   unsigned int endregno
10905     = regno + (regno < FIRST_PSEUDO_REGISTER
10906                ? hard_regno_nregs[regno][GET_MODE (reg)] : 1);
10907   unsigned int i;
10908
10909   /* If VALUE contains REG and we have a previous value for REG, substitute
10910      the previous value.  */
10911   if (value && insn && reg_overlap_mentioned_p (reg, value))
10912     {
10913       rtx tem;
10914
10915       /* Set things up so get_last_value is allowed to see anything set up to
10916          our insn.  */
10917       subst_low_cuid = INSN_CUID (insn);
10918       tem = get_last_value (reg);
10919
10920       /* If TEM is simply a binary operation with two CLOBBERs as operands,
10921          it isn't going to be useful and will take a lot of time to process,
10922          so just use the CLOBBER.  */
10923
10924       if (tem)
10925         {
10926           if (ARITHMETIC_P (tem)
10927               && GET_CODE (XEXP (tem, 0)) == CLOBBER
10928               && GET_CODE (XEXP (tem, 1)) == CLOBBER)
10929             tem = XEXP (tem, 0);
10930           else if (count_occurrences (value, reg, 1) >= 2)
10931             {
10932               /* If there are two or more occurrences of REG in VALUE,
10933                  prevent the value from growing too much.  */
10934               if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
10935                 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
10936             }
10937
10938           value = replace_rtx (copy_rtx (value), reg, tem);
10939         }
10940     }
10941
10942   /* For each register modified, show we don't know its value, that
10943      we don't know about its bitwise content, that its value has been
10944      updated, and that we don't know the location of the death of the
10945      register.  */
10946   for (i = regno; i < endregno; i++)
10947     {
10948       if (insn)
10949         reg_stat[i].last_set = insn;
10950
10951       reg_stat[i].last_set_value = 0;
10952       reg_stat[i].last_set_mode = 0;
10953       reg_stat[i].last_set_nonzero_bits = 0;
10954       reg_stat[i].last_set_sign_bit_copies = 0;
10955       reg_stat[i].last_death = 0;
10956     }
10957
10958   /* Mark registers that are being referenced in this value.  */
10959   if (value)
10960     update_table_tick (value);
10961
10962   /* Now update the status of each register being set.
10963      If someone is using this register in this block, set this register
10964      to invalid since we will get confused between the two lives in this
10965      basic block.  This makes using this register always invalid.  In cse, we
10966      scan the table to invalidate all entries using this register, but this
10967      is too much work for us.  */
10968
10969   for (i = regno; i < endregno; i++)
10970     {
10971       reg_stat[i].last_set_label = label_tick;
10972       if (value && reg_stat[i].last_set_table_tick == label_tick)
10973         reg_stat[i].last_set_invalid = 1;
10974       else
10975         reg_stat[i].last_set_invalid = 0;
10976     }
10977
10978   /* The value being assigned might refer to X (like in "x++;").  In that
10979      case, we must replace it with (clobber (const_int 0)) to prevent
10980      infinite loops.  */
10981   if (value && ! get_last_value_validate (&value, insn,
10982                                           reg_stat[regno].last_set_label, 0))
10983     {
10984       value = copy_rtx (value);
10985       if (! get_last_value_validate (&value, insn,
10986                                      reg_stat[regno].last_set_label, 1))
10987         value = 0;
10988     }
10989
10990   /* For the main register being modified, update the value, the mode, the
10991      nonzero bits, and the number of sign bit copies.  */
10992
10993   reg_stat[regno].last_set_value = value;
10994
10995   if (value)
10996     {
10997       enum machine_mode mode = GET_MODE (reg);
10998       subst_low_cuid = INSN_CUID (insn);
10999       reg_stat[regno].last_set_mode = mode;
11000       if (GET_MODE_CLASS (mode) == MODE_INT
11001           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11002         mode = nonzero_bits_mode;
11003       reg_stat[regno].last_set_nonzero_bits = nonzero_bits (value, mode);
11004       reg_stat[regno].last_set_sign_bit_copies
11005         = num_sign_bit_copies (value, GET_MODE (reg));
11006     }
11007 }
11008
11009 /* Called via note_stores from record_dead_and_set_regs to handle one
11010    SET or CLOBBER in an insn.  DATA is the instruction in which the
11011    set is occurring.  */
11012
11013 static void
11014 record_dead_and_set_regs_1 (rtx dest, rtx setter, void *data)
11015 {
11016   rtx record_dead_insn = (rtx) data;
11017
11018   if (GET_CODE (dest) == SUBREG)
11019     dest = SUBREG_REG (dest);
11020
11021   if (REG_P (dest))
11022     {
11023       /* If we are setting the whole register, we know its value.  Otherwise
11024          show that we don't know the value.  We can handle SUBREG in
11025          some cases.  */
11026       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11027         record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11028       else if (GET_CODE (setter) == SET
11029                && GET_CODE (SET_DEST (setter)) == SUBREG
11030                && SUBREG_REG (SET_DEST (setter)) == dest
11031                && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11032                && subreg_lowpart_p (SET_DEST (setter)))
11033         record_value_for_reg (dest, record_dead_insn,
11034                               gen_lowpart (GET_MODE (dest),
11035                                                        SET_SRC (setter)));
11036       else
11037         record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11038     }
11039   else if (MEM_P (dest)
11040            /* Ignore pushes, they clobber nothing.  */
11041            && ! push_operand (dest, GET_MODE (dest)))
11042     mem_last_set = INSN_CUID (record_dead_insn);
11043 }
11044
11045 /* Update the records of when each REG was most recently set or killed
11046    for the things done by INSN.  This is the last thing done in processing
11047    INSN in the combiner loop.
11048
11049    We update reg_stat[], in particular fields last_set, last_set_value,
11050    last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
11051    last_death, and also the similar information mem_last_set (which insn
11052    most recently modified memory) and last_call_cuid (which insn was the
11053    most recent subroutine call).  */
11054
11055 static void
11056 record_dead_and_set_regs (rtx insn)
11057 {
11058   rtx link;
11059   unsigned int i;
11060
11061   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11062     {
11063       if (REG_NOTE_KIND (link) == REG_DEAD
11064           && REG_P (XEXP (link, 0)))
11065         {
11066           unsigned int regno = REGNO (XEXP (link, 0));
11067           unsigned int endregno
11068             = regno + (regno < FIRST_PSEUDO_REGISTER
11069                        ? hard_regno_nregs[regno][GET_MODE (XEXP (link, 0))]
11070                        : 1);
11071
11072           for (i = regno; i < endregno; i++)
11073             reg_stat[i].last_death = insn;
11074         }
11075       else if (REG_NOTE_KIND (link) == REG_INC)
11076         record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11077     }
11078
11079   if (CALL_P (insn))
11080     {
11081       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11082         if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11083           {
11084             reg_stat[i].last_set_value = 0;
11085             reg_stat[i].last_set_mode = 0;
11086             reg_stat[i].last_set_nonzero_bits = 0;
11087             reg_stat[i].last_set_sign_bit_copies = 0;
11088             reg_stat[i].last_death = 0;
11089           }
11090
11091       last_call_cuid = mem_last_set = INSN_CUID (insn);
11092
11093       /* Don't bother recording what this insn does.  It might set the
11094          return value register, but we can't combine into a call
11095          pattern anyway, so there's no point trying (and it may cause
11096          a crash, if e.g. we wind up asking for last_set_value of a
11097          SUBREG of the return value register).  */
11098       return;
11099     }
11100
11101   note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11102 }
11103
11104 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11105    register present in the SUBREG, so for each such SUBREG go back and
11106    adjust nonzero and sign bit information of the registers that are
11107    known to have some zero/sign bits set.
11108
11109    This is needed because when combine blows the SUBREGs away, the
11110    information on zero/sign bits is lost and further combines can be
11111    missed because of that.  */
11112
11113 static void
11114 record_promoted_value (rtx insn, rtx subreg)
11115 {
11116   rtx links, set;
11117   unsigned int regno = REGNO (SUBREG_REG (subreg));
11118   enum machine_mode mode = GET_MODE (subreg);
11119
11120   if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11121     return;
11122
11123   for (links = LOG_LINKS (insn); links;)
11124     {
11125       insn = XEXP (links, 0);
11126       set = single_set (insn);
11127
11128       if (! set || !REG_P (SET_DEST (set))
11129           || REGNO (SET_DEST (set)) != regno
11130           || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11131         {
11132           links = XEXP (links, 1);
11133           continue;
11134         }
11135
11136       if (reg_stat[regno].last_set == insn)
11137         {
11138           if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11139             reg_stat[regno].last_set_nonzero_bits &= GET_MODE_MASK (mode);
11140         }
11141
11142       if (REG_P (SET_SRC (set)))
11143         {
11144           regno = REGNO (SET_SRC (set));
11145           links = LOG_LINKS (insn);
11146         }
11147       else
11148         break;
11149     }
11150 }
11151
11152 /* Scan X for promoted SUBREGs.  For each one found,
11153    note what it implies to the registers used in it.  */
11154
11155 static void
11156 check_promoted_subreg (rtx insn, rtx x)
11157 {
11158   if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
11159       && REG_P (SUBREG_REG (x)))
11160     record_promoted_value (insn, x);
11161   else
11162     {
11163       const char *format = GET_RTX_FORMAT (GET_CODE (x));
11164       int i, j;
11165
11166       for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11167         switch (format[i])
11168           {
11169           case 'e':
11170             check_promoted_subreg (insn, XEXP (x, i));
11171             break;
11172           case 'V':
11173           case 'E':
11174             if (XVEC (x, i) != 0)
11175               for (j = 0; j < XVECLEN (x, i); j++)
11176                 check_promoted_subreg (insn, XVECEXP (x, i, j));
11177             break;
11178           }
11179     }
11180 }
11181 \f
11182 /* Utility routine for the following function.  Verify that all the registers
11183    mentioned in *LOC are valid when *LOC was part of a value set when
11184    label_tick == TICK.  Return 0 if some are not.
11185
11186    If REPLACE is nonzero, replace the invalid reference with
11187    (clobber (const_int 0)) and return 1.  This replacement is useful because
11188    we often can get useful information about the form of a value (e.g., if
11189    it was produced by a shift that always produces -1 or 0) even though
11190    we don't know exactly what registers it was produced from.  */
11191
11192 static int
11193 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
11194 {
11195   rtx x = *loc;
11196   const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11197   int len = GET_RTX_LENGTH (GET_CODE (x));
11198   int i;
11199
11200   if (REG_P (x))
11201     {
11202       unsigned int regno = REGNO (x);
11203       unsigned int endregno
11204         = regno + (regno < FIRST_PSEUDO_REGISTER
11205                    ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
11206       unsigned int j;
11207
11208       for (j = regno; j < endregno; j++)
11209         if (reg_stat[j].last_set_invalid
11210             /* If this is a pseudo-register that was only set once and not
11211                live at the beginning of the function, it is always valid.  */
11212             || (! (regno >= FIRST_PSEUDO_REGISTER
11213                    && REG_N_SETS (regno) == 1
11214                    && (! REGNO_REG_SET_P
11215                        (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))
11216                 && reg_stat[j].last_set_label > tick))
11217           {
11218             if (replace)
11219               *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11220             return replace;
11221           }
11222
11223       return 1;
11224     }
11225   /* If this is a memory reference, make sure that there were
11226      no stores after it that might have clobbered the value.  We don't
11227      have alias info, so we assume any store invalidates it.  */
11228   else if (MEM_P (x) && !MEM_READONLY_P (x)
11229            && INSN_CUID (insn) <= mem_last_set)
11230     {
11231       if (replace)
11232         *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11233       return replace;
11234     }
11235
11236   for (i = 0; i < len; i++)
11237     {
11238       if (fmt[i] == 'e')
11239         {
11240           /* Check for identical subexpressions.  If x contains
11241              identical subexpression we only have to traverse one of
11242              them.  */
11243           if (i == 1 && ARITHMETIC_P (x))
11244             {
11245               /* Note that at this point x0 has already been checked
11246                  and found valid.  */
11247               rtx x0 = XEXP (x, 0);
11248               rtx x1 = XEXP (x, 1);
11249
11250               /* If x0 and x1 are identical then x is also valid.  */
11251               if (x0 == x1)
11252                 return 1;
11253
11254               /* If x1 is identical to a subexpression of x0 then
11255                  while checking x0, x1 has already been checked.  Thus
11256                  it is valid and so as x.  */
11257               if (ARITHMETIC_P (x0)
11258                   && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11259                 return 1;
11260
11261               /* If x0 is identical to a subexpression of x1 then x is
11262                  valid iff the rest of x1 is valid.  */
11263               if (ARITHMETIC_P (x1)
11264                   && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11265                 return
11266                   get_last_value_validate (&XEXP (x1,
11267                                                   x0 == XEXP (x1, 0) ? 1 : 0),
11268                                            insn, tick, replace);
11269             }
11270
11271           if (get_last_value_validate (&XEXP (x, i), insn, tick,
11272                                        replace) == 0)
11273             return 0;
11274         }
11275       /* Don't bother with these.  They shouldn't occur anyway.  */
11276       else if (fmt[i] == 'E')
11277         return 0;
11278     }
11279
11280   /* If we haven't found a reason for it to be invalid, it is valid.  */
11281   return 1;
11282 }
11283
11284 /* Get the last value assigned to X, if known.  Some registers
11285    in the value may be replaced with (clobber (const_int 0)) if their value
11286    is known longer known reliably.  */
11287
11288 static rtx
11289 get_last_value (rtx x)
11290 {
11291   unsigned int regno;
11292   rtx value;
11293
11294   /* If this is a non-paradoxical SUBREG, get the value of its operand and
11295      then convert it to the desired mode.  If this is a paradoxical SUBREG,
11296      we cannot predict what values the "extra" bits might have.  */
11297   if (GET_CODE (x) == SUBREG
11298       && subreg_lowpart_p (x)
11299       && (GET_MODE_SIZE (GET_MODE (x))
11300           <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11301       && (value = get_last_value (SUBREG_REG (x))) != 0)
11302     return gen_lowpart (GET_MODE (x), value);
11303
11304   if (!REG_P (x))
11305     return 0;
11306
11307   regno = REGNO (x);
11308   value = reg_stat[regno].last_set_value;
11309
11310   /* If we don't have a value, or if it isn't for this basic block and
11311      it's either a hard register, set more than once, or it's a live
11312      at the beginning of the function, return 0.
11313
11314      Because if it's not live at the beginning of the function then the reg
11315      is always set before being used (is never used without being set).
11316      And, if it's set only once, and it's always set before use, then all
11317      uses must have the same last value, even if it's not from this basic
11318      block.  */
11319
11320   if (value == 0
11321       || (reg_stat[regno].last_set_label != label_tick
11322           && (regno < FIRST_PSEUDO_REGISTER
11323               || REG_N_SETS (regno) != 1
11324               || (REGNO_REG_SET_P
11325                   (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))))
11326     return 0;
11327
11328   /* If the value was set in a later insn than the ones we are processing,
11329      we can't use it even if the register was only set once.  */
11330   if (INSN_CUID (reg_stat[regno].last_set) >= subst_low_cuid)
11331     return 0;
11332
11333   /* If the value has all its registers valid, return it.  */
11334   if (get_last_value_validate (&value, reg_stat[regno].last_set,
11335                                reg_stat[regno].last_set_label, 0))
11336     return value;
11337
11338   /* Otherwise, make a copy and replace any invalid register with
11339      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
11340
11341   value = copy_rtx (value);
11342   if (get_last_value_validate (&value, reg_stat[regno].last_set,
11343                                reg_stat[regno].last_set_label, 1))
11344     return value;
11345
11346   return 0;
11347 }
11348 \f
11349 /* Return nonzero if expression X refers to a REG or to memory
11350    that is set in an instruction more recent than FROM_CUID.  */
11351
11352 static int
11353 use_crosses_set_p (rtx x, int from_cuid)
11354 {
11355   const char *fmt;
11356   int i;
11357   enum rtx_code code = GET_CODE (x);
11358
11359   if (code == REG)
11360     {
11361       unsigned int regno = REGNO (x);
11362       unsigned endreg = regno + (regno < FIRST_PSEUDO_REGISTER
11363                                  ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
11364
11365 #ifdef PUSH_ROUNDING
11366       /* Don't allow uses of the stack pointer to be moved,
11367          because we don't know whether the move crosses a push insn.  */
11368       if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11369         return 1;
11370 #endif
11371       for (; regno < endreg; regno++)
11372         if (reg_stat[regno].last_set
11373             && INSN_CUID (reg_stat[regno].last_set) > from_cuid)
11374           return 1;
11375       return 0;
11376     }
11377
11378   if (code == MEM && mem_last_set > from_cuid)
11379     return 1;
11380
11381   fmt = GET_RTX_FORMAT (code);
11382
11383   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11384     {
11385       if (fmt[i] == 'E')
11386         {
11387           int j;
11388           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11389             if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
11390               return 1;
11391         }
11392       else if (fmt[i] == 'e'
11393                && use_crosses_set_p (XEXP (x, i), from_cuid))
11394         return 1;
11395     }
11396   return 0;
11397 }
11398 \f
11399 /* Define three variables used for communication between the following
11400    routines.  */
11401
11402 static unsigned int reg_dead_regno, reg_dead_endregno;
11403 static int reg_dead_flag;
11404
11405 /* Function called via note_stores from reg_dead_at_p.
11406
11407    If DEST is within [reg_dead_regno, reg_dead_endregno), set
11408    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
11409
11410 static void
11411 reg_dead_at_p_1 (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
11412 {
11413   unsigned int regno, endregno;
11414
11415   if (!REG_P (dest))
11416     return;
11417
11418   regno = REGNO (dest);
11419   endregno = regno + (regno < FIRST_PSEUDO_REGISTER
11420                       ? hard_regno_nregs[regno][GET_MODE (dest)] : 1);
11421
11422   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11423     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11424 }
11425
11426 /* Return nonzero if REG is known to be dead at INSN.
11427
11428    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
11429    referencing REG, it is dead.  If we hit a SET referencing REG, it is
11430    live.  Otherwise, see if it is live or dead at the start of the basic
11431    block we are in.  Hard regs marked as being live in NEWPAT_USED_REGS
11432    must be assumed to be always live.  */
11433
11434 static int
11435 reg_dead_at_p (rtx reg, rtx insn)
11436 {
11437   basic_block block;
11438   unsigned int i;
11439
11440   /* Set variables for reg_dead_at_p_1.  */
11441   reg_dead_regno = REGNO (reg);
11442   reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
11443                                         ? hard_regno_nregs[reg_dead_regno]
11444                                                           [GET_MODE (reg)]
11445                                         : 1);
11446
11447   reg_dead_flag = 0;
11448
11449   /* Check that reg isn't mentioned in NEWPAT_USED_REGS.  For fixed registers
11450      we allow the machine description to decide whether use-and-clobber
11451      patterns are OK.  */
11452   if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11453     {
11454       for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11455         if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
11456           return 0;
11457     }
11458
11459   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11460      beginning of function.  */
11461   for (; insn && !LABEL_P (insn) && !BARRIER_P (insn);
11462        insn = prev_nonnote_insn (insn))
11463     {
11464       note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
11465       if (reg_dead_flag)
11466         return reg_dead_flag == 1 ? 1 : 0;
11467
11468       if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11469         return 1;
11470     }
11471
11472   /* Get the basic block that we were in.  */
11473   if (insn == 0)
11474     block = ENTRY_BLOCK_PTR->next_bb;
11475   else
11476     {
11477       FOR_EACH_BB (block)
11478         if (insn == BB_HEAD (block))
11479           break;
11480
11481       if (block == EXIT_BLOCK_PTR)
11482         return 0;
11483     }
11484
11485   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11486     if (REGNO_REG_SET_P (block->global_live_at_start, i))
11487       return 0;
11488
11489   return 1;
11490 }
11491 \f
11492 /* Note hard registers in X that are used.  This code is similar to
11493    that in flow.c, but much simpler since we don't care about pseudos.  */
11494
11495 static void
11496 mark_used_regs_combine (rtx x)
11497 {
11498   RTX_CODE code = GET_CODE (x);
11499   unsigned int regno;
11500   int i;
11501
11502   switch (code)
11503     {
11504     case LABEL_REF:
11505     case SYMBOL_REF:
11506     case CONST_INT:
11507     case CONST:
11508     case CONST_DOUBLE:
11509     case CONST_VECTOR:
11510     case PC:
11511     case ADDR_VEC:
11512     case ADDR_DIFF_VEC:
11513     case ASM_INPUT:
11514 #ifdef HAVE_cc0
11515     /* CC0 must die in the insn after it is set, so we don't need to take
11516        special note of it here.  */
11517     case CC0:
11518 #endif
11519       return;
11520
11521     case CLOBBER:
11522       /* If we are clobbering a MEM, mark any hard registers inside the
11523          address as used.  */
11524       if (MEM_P (XEXP (x, 0)))
11525         mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
11526       return;
11527
11528     case REG:
11529       regno = REGNO (x);
11530       /* A hard reg in a wide mode may really be multiple registers.
11531          If so, mark all of them just like the first.  */
11532       if (regno < FIRST_PSEUDO_REGISTER)
11533         {
11534           unsigned int endregno, r;
11535
11536           /* None of this applies to the stack, frame or arg pointers.  */
11537           if (regno == STACK_POINTER_REGNUM
11538 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11539               || regno == HARD_FRAME_POINTER_REGNUM
11540 #endif
11541 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
11542               || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
11543 #endif
11544               || regno == FRAME_POINTER_REGNUM)
11545             return;
11546
11547           endregno = regno + hard_regno_nregs[regno][GET_MODE (x)];
11548           for (r = regno; r < endregno; r++)
11549             SET_HARD_REG_BIT (newpat_used_regs, r);
11550         }
11551       return;
11552
11553     case SET:
11554       {
11555         /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11556            the address.  */
11557         rtx testreg = SET_DEST (x);
11558
11559         while (GET_CODE (testreg) == SUBREG
11560                || GET_CODE (testreg) == ZERO_EXTRACT
11561                || GET_CODE (testreg) == STRICT_LOW_PART)
11562           testreg = XEXP (testreg, 0);
11563
11564         if (MEM_P (testreg))
11565           mark_used_regs_combine (XEXP (testreg, 0));
11566
11567         mark_used_regs_combine (SET_SRC (x));
11568       }
11569       return;
11570
11571     default:
11572       break;
11573     }
11574
11575   /* Recursively scan the operands of this expression.  */
11576
11577   {
11578     const char *fmt = GET_RTX_FORMAT (code);
11579
11580     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11581       {
11582         if (fmt[i] == 'e')
11583           mark_used_regs_combine (XEXP (x, i));
11584         else if (fmt[i] == 'E')
11585           {
11586             int j;
11587
11588             for (j = 0; j < XVECLEN (x, i); j++)
11589               mark_used_regs_combine (XVECEXP (x, i, j));
11590           }
11591       }
11592   }
11593 }
11594 \f
11595 /* Remove register number REGNO from the dead registers list of INSN.
11596
11597    Return the note used to record the death, if there was one.  */
11598
11599 rtx
11600 remove_death (unsigned int regno, rtx insn)
11601 {
11602   rtx note = find_regno_note (insn, REG_DEAD, regno);
11603
11604   if (note)
11605     {
11606       REG_N_DEATHS (regno)--;
11607       remove_note (insn, note);
11608     }
11609
11610   return note;
11611 }
11612
11613 /* For each register (hardware or pseudo) used within expression X, if its
11614    death is in an instruction with cuid between FROM_CUID (inclusive) and
11615    TO_INSN (exclusive), put a REG_DEAD note for that register in the
11616    list headed by PNOTES.
11617
11618    That said, don't move registers killed by maybe_kill_insn.
11619
11620    This is done when X is being merged by combination into TO_INSN.  These
11621    notes will then be distributed as needed.  */
11622
11623 static void
11624 move_deaths (rtx x, rtx maybe_kill_insn, int from_cuid, rtx to_insn,
11625              rtx *pnotes)
11626 {
11627   const char *fmt;
11628   int len, i;
11629   enum rtx_code code = GET_CODE (x);
11630
11631   if (code == REG)
11632     {
11633       unsigned int regno = REGNO (x);
11634       rtx where_dead = reg_stat[regno].last_death;
11635       rtx before_dead, after_dead;
11636
11637       /* Don't move the register if it gets killed in between from and to.  */
11638       if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
11639           && ! reg_referenced_p (x, maybe_kill_insn))
11640         return;
11641
11642       /* WHERE_DEAD could be a USE insn made by combine, so first we
11643          make sure that we have insns with valid INSN_CUID values.  */
11644       before_dead = where_dead;
11645       while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
11646         before_dead = PREV_INSN (before_dead);
11647
11648       after_dead = where_dead;
11649       while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
11650         after_dead = NEXT_INSN (after_dead);
11651
11652       if (before_dead && after_dead
11653           && INSN_CUID (before_dead) >= from_cuid
11654           && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
11655               || (where_dead != after_dead
11656                   && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
11657         {
11658           rtx note = remove_death (regno, where_dead);
11659
11660           /* It is possible for the call above to return 0.  This can occur
11661              when last_death points to I2 or I1 that we combined with.
11662              In that case make a new note.
11663
11664              We must also check for the case where X is a hard register
11665              and NOTE is a death note for a range of hard registers
11666              including X.  In that case, we must put REG_DEAD notes for
11667              the remaining registers in place of NOTE.  */
11668
11669           if (note != 0 && regno < FIRST_PSEUDO_REGISTER
11670               && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11671                   > GET_MODE_SIZE (GET_MODE (x))))
11672             {
11673               unsigned int deadregno = REGNO (XEXP (note, 0));
11674               unsigned int deadend
11675                 = (deadregno + hard_regno_nregs[deadregno]
11676                                                [GET_MODE (XEXP (note, 0))]);
11677               unsigned int ourend
11678                 = regno + hard_regno_nregs[regno][GET_MODE (x)];
11679               unsigned int i;
11680
11681               for (i = deadregno; i < deadend; i++)
11682                 if (i < regno || i >= ourend)
11683                   REG_NOTES (where_dead)
11684                     = gen_rtx_EXPR_LIST (REG_DEAD,
11685                                          regno_reg_rtx[i],
11686                                          REG_NOTES (where_dead));
11687             }
11688
11689           /* If we didn't find any note, or if we found a REG_DEAD note that
11690              covers only part of the given reg, and we have a multi-reg hard
11691              register, then to be safe we must check for REG_DEAD notes
11692              for each register other than the first.  They could have
11693              their own REG_DEAD notes lying around.  */
11694           else if ((note == 0
11695                     || (note != 0
11696                         && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11697                             < GET_MODE_SIZE (GET_MODE (x)))))
11698                    && regno < FIRST_PSEUDO_REGISTER
11699                    && hard_regno_nregs[regno][GET_MODE (x)] > 1)
11700             {
11701               unsigned int ourend
11702                 = regno + hard_regno_nregs[regno][GET_MODE (x)];
11703               unsigned int i, offset;
11704               rtx oldnotes = 0;
11705
11706               if (note)
11707                 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
11708               else
11709                 offset = 1;
11710
11711               for (i = regno + offset; i < ourend; i++)
11712                 move_deaths (regno_reg_rtx[i],
11713                              maybe_kill_insn, from_cuid, to_insn, &oldnotes);
11714             }
11715
11716           if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
11717             {
11718               XEXP (note, 1) = *pnotes;
11719               *pnotes = note;
11720             }
11721           else
11722             *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
11723
11724           REG_N_DEATHS (regno)++;
11725         }
11726
11727       return;
11728     }
11729
11730   else if (GET_CODE (x) == SET)
11731     {
11732       rtx dest = SET_DEST (x);
11733
11734       move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
11735
11736       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
11737          that accesses one word of a multi-word item, some
11738          piece of everything register in the expression is used by
11739          this insn, so remove any old death.  */
11740       /* ??? So why do we test for equality of the sizes?  */
11741
11742       if (GET_CODE (dest) == ZERO_EXTRACT
11743           || GET_CODE (dest) == STRICT_LOW_PART
11744           || (GET_CODE (dest) == SUBREG
11745               && (((GET_MODE_SIZE (GET_MODE (dest))
11746                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
11747                   == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
11748                        + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
11749         {
11750           move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
11751           return;
11752         }
11753
11754       /* If this is some other SUBREG, we know it replaces the entire
11755          value, so use that as the destination.  */
11756       if (GET_CODE (dest) == SUBREG)
11757         dest = SUBREG_REG (dest);
11758
11759       /* If this is a MEM, adjust deaths of anything used in the address.
11760          For a REG (the only other possibility), the entire value is
11761          being replaced so the old value is not used in this insn.  */
11762
11763       if (MEM_P (dest))
11764         move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
11765                      to_insn, pnotes);
11766       return;
11767     }
11768
11769   else if (GET_CODE (x) == CLOBBER)
11770     return;
11771
11772   len = GET_RTX_LENGTH (code);
11773   fmt = GET_RTX_FORMAT (code);
11774
11775   for (i = 0; i < len; i++)
11776     {
11777       if (fmt[i] == 'E')
11778         {
11779           int j;
11780           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11781             move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
11782                          to_insn, pnotes);
11783         }
11784       else if (fmt[i] == 'e')
11785         move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
11786     }
11787 }
11788 \f
11789 /* Return 1 if X is the target of a bit-field assignment in BODY, the
11790    pattern of an insn.  X must be a REG.  */
11791
11792 static int
11793 reg_bitfield_target_p (rtx x, rtx body)
11794 {
11795   int i;
11796
11797   if (GET_CODE (body) == SET)
11798     {
11799       rtx dest = SET_DEST (body);
11800       rtx target;
11801       unsigned int regno, tregno, endregno, endtregno;
11802
11803       if (GET_CODE (dest) == ZERO_EXTRACT)
11804         target = XEXP (dest, 0);
11805       else if (GET_CODE (dest) == STRICT_LOW_PART)
11806         target = SUBREG_REG (XEXP (dest, 0));
11807       else
11808         return 0;
11809
11810       if (GET_CODE (target) == SUBREG)
11811         target = SUBREG_REG (target);
11812
11813       if (!REG_P (target))
11814         return 0;
11815
11816       tregno = REGNO (target), regno = REGNO (x);
11817       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
11818         return target == x;
11819
11820       endtregno = tregno + hard_regno_nregs[tregno][GET_MODE (target)];
11821       endregno = regno + hard_regno_nregs[regno][GET_MODE (x)];
11822
11823       return endregno > tregno && regno < endtregno;
11824     }
11825
11826   else if (GET_CODE (body) == PARALLEL)
11827     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
11828       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
11829         return 1;
11830
11831   return 0;
11832 }
11833 \f
11834 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
11835    as appropriate.  I3 and I2 are the insns resulting from the combination
11836    insns including FROM (I2 may be zero).
11837
11838    Each note in the list is either ignored or placed on some insns, depending
11839    on the type of note.  */
11840
11841 static void
11842 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2)
11843 {
11844   rtx note, next_note;
11845   rtx tem;
11846
11847   for (note = notes; note; note = next_note)
11848     {
11849       rtx place = 0, place2 = 0;
11850
11851       /* If this NOTE references a pseudo register, ensure it references
11852          the latest copy of that register.  */
11853       if (XEXP (note, 0) && REG_P (XEXP (note, 0))
11854           && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
11855         XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
11856
11857       next_note = XEXP (note, 1);
11858       switch (REG_NOTE_KIND (note))
11859         {
11860         case REG_BR_PROB:
11861         case REG_BR_PRED:
11862           /* Doesn't matter much where we put this, as long as it's somewhere.
11863              It is preferable to keep these notes on branches, which is most
11864              likely to be i3.  */
11865           place = i3;
11866           break;
11867
11868         case REG_VALUE_PROFILE:
11869           /* Just get rid of this note, as it is unused later anyway.  */
11870           break;
11871
11872         case REG_NON_LOCAL_GOTO:
11873           if (JUMP_P (i3))
11874             place = i3;
11875           else
11876             {
11877               gcc_assert (i2 && JUMP_P (i2));
11878               place = i2;
11879             }
11880           break;
11881
11882         case REG_EH_REGION:
11883           /* These notes must remain with the call or trapping instruction.  */
11884           if (CALL_P (i3))
11885             place = i3;
11886           else if (i2 && CALL_P (i2))
11887             place = i2;
11888           else
11889             {
11890               gcc_assert (flag_non_call_exceptions);
11891               if (may_trap_p (i3))
11892                 place = i3;
11893               else if (i2 && may_trap_p (i2))
11894                 place = i2;
11895               /* ??? Otherwise assume we've combined things such that we
11896                  can now prove that the instructions can't trap.  Drop the
11897                  note in this case.  */
11898             }
11899           break;
11900
11901         case REG_NORETURN:
11902         case REG_SETJMP:
11903           /* These notes must remain with the call.  It should not be
11904              possible for both I2 and I3 to be a call.  */
11905           if (CALL_P (i3))
11906             place = i3;
11907           else
11908             {
11909               gcc_assert (i2 && CALL_P (i2));
11910               place = i2;
11911             }
11912           break;
11913
11914         case REG_UNUSED:
11915           /* Any clobbers for i3 may still exist, and so we must process
11916              REG_UNUSED notes from that insn.
11917
11918              Any clobbers from i2 or i1 can only exist if they were added by
11919              recog_for_combine.  In that case, recog_for_combine created the
11920              necessary REG_UNUSED notes.  Trying to keep any original
11921              REG_UNUSED notes from these insns can cause incorrect output
11922              if it is for the same register as the original i3 dest.
11923              In that case, we will notice that the register is set in i3,
11924              and then add a REG_UNUSED note for the destination of i3, which
11925              is wrong.  However, it is possible to have REG_UNUSED notes from
11926              i2 or i1 for register which were both used and clobbered, so
11927              we keep notes from i2 or i1 if they will turn into REG_DEAD
11928              notes.  */
11929
11930           /* If this register is set or clobbered in I3, put the note there
11931              unless there is one already.  */
11932           if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
11933             {
11934               if (from_insn != i3)
11935                 break;
11936
11937               if (! (REG_P (XEXP (note, 0))
11938                      ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
11939                      : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
11940                 place = i3;
11941             }
11942           /* Otherwise, if this register is used by I3, then this register
11943              now dies here, so we must put a REG_DEAD note here unless there
11944              is one already.  */
11945           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
11946                    && ! (REG_P (XEXP (note, 0))
11947                          ? find_regno_note (i3, REG_DEAD,
11948                                             REGNO (XEXP (note, 0)))
11949                          : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
11950             {
11951               PUT_REG_NOTE_KIND (note, REG_DEAD);
11952               place = i3;
11953             }
11954           break;
11955
11956         case REG_EQUAL:
11957         case REG_EQUIV:
11958         case REG_NOALIAS:
11959           /* These notes say something about results of an insn.  We can
11960              only support them if they used to be on I3 in which case they
11961              remain on I3.  Otherwise they are ignored.
11962
11963              If the note refers to an expression that is not a constant, we
11964              must also ignore the note since we cannot tell whether the
11965              equivalence is still true.  It might be possible to do
11966              slightly better than this (we only have a problem if I2DEST
11967              or I1DEST is present in the expression), but it doesn't
11968              seem worth the trouble.  */
11969
11970           if (from_insn == i3
11971               && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
11972             place = i3;
11973           break;
11974
11975         case REG_INC:
11976         case REG_NO_CONFLICT:
11977           /* These notes say something about how a register is used.  They must
11978              be present on any use of the register in I2 or I3.  */
11979           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
11980             place = i3;
11981
11982           if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
11983             {
11984               if (place)
11985                 place2 = i2;
11986               else
11987                 place = i2;
11988             }
11989           break;
11990
11991         case REG_LABEL:
11992           /* This can show up in several ways -- either directly in the
11993              pattern, or hidden off in the constant pool with (or without?)
11994              a REG_EQUAL note.  */
11995           /* ??? Ignore the without-reg_equal-note problem for now.  */
11996           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
11997               || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
11998                   && GET_CODE (XEXP (tem, 0)) == LABEL_REF
11999                   && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12000             place = i3;
12001
12002           if (i2
12003               && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12004                   || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12005                       && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12006                       && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12007             {
12008               if (place)
12009                 place2 = i2;
12010               else
12011                 place = i2;
12012             }
12013
12014           /* Don't attach REG_LABEL note to a JUMP_INSN.  Add
12015              a JUMP_LABEL instead or decrement LABEL_NUSES.  */
12016           if (place && JUMP_P (place))
12017             {
12018               rtx label = JUMP_LABEL (place);
12019               
12020               if (!label)
12021                 JUMP_LABEL (place) = XEXP (note, 0);
12022               else
12023                 {
12024                   gcc_assert (label == XEXP (note, 0));
12025                   if (LABEL_P (label))
12026                     LABEL_NUSES (label)--;
12027                 }
12028               place = 0;
12029             }
12030           if (place2 && JUMP_P (place2))
12031             {
12032               rtx label = JUMP_LABEL (place2);
12033               
12034               if (!label)
12035                 JUMP_LABEL (place2) = XEXP (note, 0);
12036               else
12037                 {
12038                   gcc_assert (label == XEXP (note, 0));
12039                   if (LABEL_P (label))
12040                     LABEL_NUSES (label)--;
12041                 }
12042               place2 = 0;
12043             }
12044           break;
12045
12046         case REG_NONNEG:
12047           /* This note says something about the value of a register prior
12048              to the execution of an insn.  It is too much trouble to see
12049              if the note is still correct in all situations.  It is better
12050              to simply delete it.  */
12051           break;
12052
12053         case REG_RETVAL:
12054           /* If the insn previously containing this note still exists,
12055              put it back where it was.  Otherwise move it to the previous
12056              insn.  Adjust the corresponding REG_LIBCALL note.  */
12057           if (!NOTE_P (from_insn))
12058             place = from_insn;
12059           else
12060             {
12061               tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
12062               place = prev_real_insn (from_insn);
12063               if (tem && place)
12064                 XEXP (tem, 0) = place;
12065               /* If we're deleting the last remaining instruction of a
12066                  libcall sequence, don't add the notes.  */
12067               else if (XEXP (note, 0) == from_insn)
12068                 tem = place = 0;
12069               /* Don't add the dangling REG_RETVAL note.  */
12070               else if (! tem)
12071                 place = 0;
12072             }
12073           break;
12074
12075         case REG_LIBCALL:
12076           /* This is handled similarly to REG_RETVAL.  */
12077           if (!NOTE_P (from_insn))
12078             place = from_insn;
12079           else
12080             {
12081               tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
12082               place = next_real_insn (from_insn);
12083               if (tem && place)
12084                 XEXP (tem, 0) = place;
12085               /* If we're deleting the last remaining instruction of a
12086                  libcall sequence, don't add the notes.  */
12087               else if (XEXP (note, 0) == from_insn)
12088                 tem = place = 0;
12089               /* Don't add the dangling REG_LIBCALL note.  */
12090               else if (! tem)
12091                 place = 0;
12092             }
12093           break;
12094
12095         case REG_DEAD:
12096           /* If the register is used as an input in I3, it dies there.
12097              Similarly for I2, if it is nonzero and adjacent to I3.
12098
12099              If the register is not used as an input in either I3 or I2
12100              and it is not one of the registers we were supposed to eliminate,
12101              there are two possibilities.  We might have a non-adjacent I2
12102              or we might have somehow eliminated an additional register
12103              from a computation.  For example, we might have had A & B where
12104              we discover that B will always be zero.  In this case we will
12105              eliminate the reference to A.
12106
12107              In both cases, we must search to see if we can find a previous
12108              use of A and put the death note there.  */
12109
12110           if (from_insn
12111               && CALL_P (from_insn)
12112               && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12113             place = from_insn;
12114           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12115             place = i3;
12116           else if (i2 != 0 && next_nonnote_insn (i2) == i3
12117                    && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12118             place = i2;
12119
12120           if (place == 0)
12121             {
12122               basic_block bb = this_basic_block;
12123
12124               for (tem = PREV_INSN (i3); place == 0; tem = PREV_INSN (tem))
12125                 {
12126                   if (! INSN_P (tem))
12127                     {
12128                       if (tem == BB_HEAD (bb))
12129                         break;
12130                       continue;
12131                     }
12132
12133                   /* If the register is being set at TEM, see if that is all
12134                      TEM is doing.  If so, delete TEM.  Otherwise, make this
12135                      into a REG_UNUSED note instead. Don't delete sets to
12136                      global register vars.  */
12137                   if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
12138                        || !global_regs[REGNO (XEXP (note, 0))])
12139                       && reg_set_p (XEXP (note, 0), PATTERN (tem)))
12140                     {
12141                       rtx set = single_set (tem);
12142                       rtx inner_dest = 0;
12143 #ifdef HAVE_cc0
12144                       rtx cc0_setter = NULL_RTX;
12145 #endif
12146
12147                       if (set != 0)
12148                         for (inner_dest = SET_DEST (set);
12149                              (GET_CODE (inner_dest) == STRICT_LOW_PART
12150                               || GET_CODE (inner_dest) == SUBREG
12151                               || GET_CODE (inner_dest) == ZERO_EXTRACT);
12152                              inner_dest = XEXP (inner_dest, 0))
12153                           ;
12154
12155                       /* Verify that it was the set, and not a clobber that
12156                          modified the register.
12157
12158                          CC0 targets must be careful to maintain setter/user
12159                          pairs.  If we cannot delete the setter due to side
12160                          effects, mark the user with an UNUSED note instead
12161                          of deleting it.  */
12162
12163                       if (set != 0 && ! side_effects_p (SET_SRC (set))
12164                           && rtx_equal_p (XEXP (note, 0), inner_dest)
12165 #ifdef HAVE_cc0
12166                           && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12167                               || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12168                                   && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12169 #endif
12170                           )
12171                         {
12172                           /* Move the notes and links of TEM elsewhere.
12173                              This might delete other dead insns recursively.
12174                              First set the pattern to something that won't use
12175                              any register.  */
12176                           rtx old_notes = REG_NOTES (tem);
12177
12178                           PATTERN (tem) = pc_rtx;
12179                           REG_NOTES (tem) = NULL;
12180
12181                           distribute_notes (old_notes, tem, tem, NULL_RTX);
12182                           distribute_links (LOG_LINKS (tem));
12183
12184                           SET_INSN_DELETED (tem);
12185
12186 #ifdef HAVE_cc0
12187                           /* Delete the setter too.  */
12188                           if (cc0_setter)
12189                             {
12190                               PATTERN (cc0_setter) = pc_rtx;
12191                               old_notes = REG_NOTES (cc0_setter);
12192                               REG_NOTES (cc0_setter) = NULL;
12193
12194                               distribute_notes (old_notes, cc0_setter,
12195                                                 cc0_setter, NULL_RTX);
12196                               distribute_links (LOG_LINKS (cc0_setter));
12197
12198                               SET_INSN_DELETED (cc0_setter);
12199                             }
12200 #endif
12201                         }
12202                       else
12203                         {
12204                           PUT_REG_NOTE_KIND (note, REG_UNUSED);
12205
12206                           /*  If there isn't already a REG_UNUSED note, put one
12207                               here.  Do not place a REG_DEAD note, even if
12208                               the register is also used here; that would not
12209                               match the algorithm used in lifetime analysis
12210                               and can cause the consistency check in the
12211                               scheduler to fail.  */
12212                           if (! find_regno_note (tem, REG_UNUSED,
12213                                                  REGNO (XEXP (note, 0))))
12214                             place = tem;
12215                           break;
12216                         }
12217                     }
12218                   else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12219                            || (CALL_P (tem)
12220                                && find_reg_fusage (tem, USE, XEXP (note, 0))))
12221                     {
12222                       place = tem;
12223
12224                       /* If we are doing a 3->2 combination, and we have a
12225                          register which formerly died in i3 and was not used
12226                          by i2, which now no longer dies in i3 and is used in
12227                          i2 but does not die in i2, and place is between i2
12228                          and i3, then we may need to move a link from place to
12229                          i2.  */
12230                       if (i2 && INSN_UID (place) <= max_uid_cuid
12231                           && INSN_CUID (place) > INSN_CUID (i2)
12232                           && from_insn
12233                           && INSN_CUID (from_insn) > INSN_CUID (i2)
12234                           && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12235                         {
12236                           rtx links = LOG_LINKS (place);
12237                           LOG_LINKS (place) = 0;
12238                           distribute_links (links);
12239                         }
12240                       break;
12241                     }
12242
12243                   if (tem == BB_HEAD (bb))
12244                     break;
12245                 }
12246
12247               /* We haven't found an insn for the death note and it
12248                  is still a REG_DEAD note, but we have hit the beginning
12249                  of the block.  If the existing life info says the reg
12250                  was dead, there's nothing left to do.  Otherwise, we'll
12251                  need to do a global life update after combine.  */
12252               if (REG_NOTE_KIND (note) == REG_DEAD && place == 0
12253                   && REGNO_REG_SET_P (bb->global_live_at_start,
12254                                       REGNO (XEXP (note, 0))))
12255                 SET_BIT (refresh_blocks, this_basic_block->index);
12256             }
12257
12258           /* If the register is set or already dead at PLACE, we needn't do
12259              anything with this note if it is still a REG_DEAD note.
12260              We check here if it is set at all, not if is it totally replaced,
12261              which is what `dead_or_set_p' checks, so also check for it being
12262              set partially.  */
12263
12264           if (place && REG_NOTE_KIND (note) == REG_DEAD)
12265             {
12266               unsigned int regno = REGNO (XEXP (note, 0));
12267
12268               /* Similarly, if the instruction on which we want to place
12269                  the note is a noop, we'll need do a global live update
12270                  after we remove them in delete_noop_moves.  */
12271               if (noop_move_p (place))
12272                 SET_BIT (refresh_blocks, this_basic_block->index);
12273
12274               if (dead_or_set_p (place, XEXP (note, 0))
12275                   || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12276                 {
12277                   /* Unless the register previously died in PLACE, clear
12278                      last_death.  [I no longer understand why this is
12279                      being done.] */
12280                   if (reg_stat[regno].last_death != place)
12281                     reg_stat[regno].last_death = 0;
12282                   place = 0;
12283                 }
12284               else
12285                 reg_stat[regno].last_death = place;
12286
12287               /* If this is a death note for a hard reg that is occupying
12288                  multiple registers, ensure that we are still using all
12289                  parts of the object.  If we find a piece of the object
12290                  that is unused, we must arrange for an appropriate REG_DEAD
12291                  note to be added for it.  However, we can't just emit a USE
12292                  and tag the note to it, since the register might actually
12293                  be dead; so we recourse, and the recursive call then finds
12294                  the previous insn that used this register.  */
12295
12296               if (place && regno < FIRST_PSEUDO_REGISTER
12297                   && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
12298                 {
12299                   unsigned int endregno
12300                     = regno + hard_regno_nregs[regno]
12301                                               [GET_MODE (XEXP (note, 0))];
12302                   int all_used = 1;
12303                   unsigned int i;
12304
12305                   for (i = regno; i < endregno; i++)
12306                     if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12307                          && ! find_regno_fusage (place, USE, i))
12308                         || dead_or_set_regno_p (place, i))
12309                       all_used = 0;
12310
12311                   if (! all_used)
12312                     {
12313                       /* Put only REG_DEAD notes for pieces that are
12314                          not already dead or set.  */
12315
12316                       for (i = regno; i < endregno;
12317                            i += hard_regno_nregs[i][reg_raw_mode[i]])
12318                         {
12319                           rtx piece = regno_reg_rtx[i];
12320                           basic_block bb = this_basic_block;
12321
12322                           if (! dead_or_set_p (place, piece)
12323                               && ! reg_bitfield_target_p (piece,
12324                                                           PATTERN (place)))
12325                             {
12326                               rtx new_note
12327                                 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12328
12329                               distribute_notes (new_note, place, place,
12330                                                 NULL_RTX);
12331                             }
12332                           else if (! refers_to_regno_p (i, i + 1,
12333                                                         PATTERN (place), 0)
12334                                    && ! find_regno_fusage (place, USE, i))
12335                             for (tem = PREV_INSN (place); ;
12336                                  tem = PREV_INSN (tem))
12337                               {
12338                                 if (! INSN_P (tem))
12339                                   {
12340                                     if (tem == BB_HEAD (bb))
12341                                       {
12342                                         SET_BIT (refresh_blocks,
12343                                                  this_basic_block->index);
12344                                         break;
12345                                       }
12346                                     continue;
12347                                   }
12348                                 if (dead_or_set_p (tem, piece)
12349                                     || reg_bitfield_target_p (piece,
12350                                                               PATTERN (tem)))
12351                                   {
12352                                     REG_NOTES (tem)
12353                                       = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
12354                                                            REG_NOTES (tem));
12355                                     break;
12356                                   }
12357                               }
12358
12359                         }
12360
12361                       place = 0;
12362                     }
12363                 }
12364             }
12365           break;
12366
12367         default:
12368           /* Any other notes should not be present at this point in the
12369              compilation.  */
12370           gcc_unreachable ();
12371         }
12372
12373       if (place)
12374         {
12375           XEXP (note, 1) = REG_NOTES (place);
12376           REG_NOTES (place) = note;
12377         }
12378       else if ((REG_NOTE_KIND (note) == REG_DEAD
12379                 || REG_NOTE_KIND (note) == REG_UNUSED)
12380                && REG_P (XEXP (note, 0)))
12381         REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
12382
12383       if (place2)
12384         {
12385           if ((REG_NOTE_KIND (note) == REG_DEAD
12386                || REG_NOTE_KIND (note) == REG_UNUSED)
12387               && REG_P (XEXP (note, 0)))
12388             REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
12389
12390           REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
12391                                                REG_NOTE_KIND (note),
12392                                                XEXP (note, 0),
12393                                                REG_NOTES (place2));
12394         }
12395     }
12396 }
12397 \f
12398 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12399    I3, I2, and I1 to new locations.  This is also called to add a link
12400    pointing at I3 when I3's destination is changed.  */
12401
12402 static void
12403 distribute_links (rtx links)
12404 {
12405   rtx link, next_link;
12406
12407   for (link = links; link; link = next_link)
12408     {
12409       rtx place = 0;
12410       rtx insn;
12411       rtx set, reg;
12412
12413       next_link = XEXP (link, 1);
12414
12415       /* If the insn that this link points to is a NOTE or isn't a single
12416          set, ignore it.  In the latter case, it isn't clear what we
12417          can do other than ignore the link, since we can't tell which
12418          register it was for.  Such links wouldn't be used by combine
12419          anyway.
12420
12421          It is not possible for the destination of the target of the link to
12422          have been changed by combine.  The only potential of this is if we
12423          replace I3, I2, and I1 by I3 and I2.  But in that case the
12424          destination of I2 also remains unchanged.  */
12425
12426       if (NOTE_P (XEXP (link, 0))
12427           || (set = single_set (XEXP (link, 0))) == 0)
12428         continue;
12429
12430       reg = SET_DEST (set);
12431       while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12432              || GET_CODE (reg) == STRICT_LOW_PART)
12433         reg = XEXP (reg, 0);
12434
12435       /* A LOG_LINK is defined as being placed on the first insn that uses
12436          a register and points to the insn that sets the register.  Start
12437          searching at the next insn after the target of the link and stop
12438          when we reach a set of the register or the end of the basic block.
12439
12440          Note that this correctly handles the link that used to point from
12441          I3 to I2.  Also note that not much searching is typically done here
12442          since most links don't point very far away.  */
12443
12444       for (insn = NEXT_INSN (XEXP (link, 0));
12445            (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
12446                      || BB_HEAD (this_basic_block->next_bb) != insn));
12447            insn = NEXT_INSN (insn))
12448         if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12449           {
12450             if (reg_referenced_p (reg, PATTERN (insn)))
12451               place = insn;
12452             break;
12453           }
12454         else if (CALL_P (insn)
12455                  && find_reg_fusage (insn, USE, reg))
12456           {
12457             place = insn;
12458             break;
12459           }
12460         else if (INSN_P (insn) && reg_set_p (reg, insn))
12461           break;
12462
12463       /* If we found a place to put the link, place it there unless there
12464          is already a link to the same insn as LINK at that point.  */
12465
12466       if (place)
12467         {
12468           rtx link2;
12469
12470           for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12471             if (XEXP (link2, 0) == XEXP (link, 0))
12472               break;
12473
12474           if (link2 == 0)
12475             {
12476               XEXP (link, 1) = LOG_LINKS (place);
12477               LOG_LINKS (place) = link;
12478
12479               /* Set added_links_insn to the earliest insn we added a
12480                  link to.  */
12481               if (added_links_insn == 0
12482                   || INSN_CUID (added_links_insn) > INSN_CUID (place))
12483                 added_links_insn = place;
12484             }
12485         }
12486     }
12487 }
12488 \f
12489 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
12490    Check whether the expression pointer to by LOC is a register or
12491    memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
12492    Otherwise return zero.  */
12493
12494 static int
12495 unmentioned_reg_p_1 (rtx *loc, void *expr)
12496 {
12497   rtx x = *loc;
12498
12499   if (x != NULL_RTX
12500       && (REG_P (x) || MEM_P (x))
12501       && ! reg_mentioned_p (x, (rtx) expr))
12502     return 1;
12503   return 0;
12504 }
12505
12506 /* Check for any register or memory mentioned in EQUIV that is not
12507    mentioned in EXPR.  This is used to restrict EQUIV to "specializations"
12508    of EXPR where some registers may have been replaced by constants.  */
12509
12510 static bool
12511 unmentioned_reg_p (rtx equiv, rtx expr)
12512 {
12513   return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
12514 }
12515 \f
12516 /* Compute INSN_CUID for INSN, which is an insn made by combine.  */
12517
12518 static int
12519 insn_cuid (rtx insn)
12520 {
12521   while (insn != 0 && INSN_UID (insn) > max_uid_cuid
12522          && NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE)
12523     insn = NEXT_INSN (insn);
12524
12525   gcc_assert (INSN_UID (insn) <= max_uid_cuid);
12526
12527   return INSN_CUID (insn);
12528 }
12529 \f
12530 void
12531 dump_combine_stats (FILE *file)
12532 {
12533   fnotice
12534     (file,
12535      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12536      combine_attempts, combine_merges, combine_extras, combine_successes);
12537 }
12538
12539 void
12540 dump_combine_total_stats (FILE *file)
12541 {
12542   fnotice
12543     (file,
12544      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12545      total_attempts, total_merges, total_extras, total_successes);
12546 }