OSDN Git Service

* combine.c (try_combine): Improve splitting of binary operators
[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, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, 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    - reg_n_refs is not adjusted in the rare case when a register is
57      no longer required in a computation
58    - there are extremely rare cases (see distribute_notes) when a
59      REG_DEAD note is lost
60    - a LOG_LINKS entry that refers to an insn with multiple SETs may be
61      removed because there is no way to know which register it was
62      linking
63
64    To simplify substitution, we combine only when the earlier insn(s)
65    consist of only a single assignment.  To simplify updating afterward,
66    we never combine when a subroutine call appears in the middle.
67
68    Since we do not represent assignments to CC0 explicitly except when that
69    is all an insn does, there is no LOG_LINKS entry in an insn that uses
70    the condition code for the insn that set the condition code.
71    Fortunately, these two insns must be consecutive.
72    Therefore, every JUMP_INSN is taken to have an implicit logical link
73    to the preceding insn.  This is not quite right, since non-jumps can
74    also use the condition code; but in practice such insns would not
75    combine anyway.  */
76
77 #include "config.h"
78 #include "system.h"
79 #include "coretypes.h"
80 #include "tm.h"
81 #include "rtl.h"
82 #include "tree.h"
83 #include "tm_p.h"
84 #include "flags.h"
85 #include "regs.h"
86 #include "hard-reg-set.h"
87 #include "basic-block.h"
88 #include "insn-config.h"
89 #include "function.h"
90 /* Include expr.h after insn-config.h so we get HAVE_conditional_move.  */
91 #include "expr.h"
92 #include "insn-attr.h"
93 #include "recog.h"
94 #include "real.h"
95 #include "toplev.h"
96 #include "target.h"
97 #include "optabs.h"
98 #include "insn-codes.h"
99 #include "rtlhooks-def.h"
100 /* Include output.h for dump_file.  */
101 #include "output.h"
102 #include "params.h"
103 #include "timevar.h"
104 #include "tree-pass.h"
105
106 /* Number of attempts to combine instructions in this function.  */
107
108 static int combine_attempts;
109
110 /* Number of attempts that got as far as substitution in this function.  */
111
112 static int combine_merges;
113
114 /* Number of instructions combined with added SETs in this function.  */
115
116 static int combine_extras;
117
118 /* Number of instructions combined in this function.  */
119
120 static int combine_successes;
121
122 /* Totals over entire compilation.  */
123
124 static int total_attempts, total_merges, total_extras, total_successes;
125
126 \f
127 /* Vector mapping INSN_UIDs to cuids.
128    The cuids are like uids but increase monotonically always.
129    Combine always uses cuids so that it can compare them.
130    But actually renumbering the uids, which we used to do,
131    proves to be a bad idea because it makes it hard to compare
132    the dumps produced by earlier passes with those from later passes.  */
133
134 static int *uid_cuid;
135 static int max_uid_cuid;
136
137 /* Get the cuid of an insn.  */
138
139 #define INSN_CUID(INSN) \
140 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
141
142 /* In case BITS_PER_WORD == HOST_BITS_PER_WIDE_INT, shifting by
143    BITS_PER_WORD would invoke undefined behavior.  Work around it.  */
144
145 #define UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD(val) \
146   (((unsigned HOST_WIDE_INT) (val) << (BITS_PER_WORD - 1)) << 1)
147
148 /* Maximum register number, which is the size of the tables below.  */
149
150 static unsigned int combine_max_regno;
151
152 struct reg_stat {
153   /* Record last point of death of (hard or pseudo) register n.  */
154   rtx                           last_death;
155
156   /* Record last point of modification of (hard or pseudo) register n.  */
157   rtx                           last_set;
158
159   /* The next group of fields allows the recording of the last value assigned
160      to (hard or pseudo) register n.  We use this information to see if an
161      operation being processed is redundant given a prior operation performed
162      on the register.  For example, an `and' with a constant is redundant if
163      all the zero bits are already known to be turned off.
164
165      We use an approach similar to that used by cse, but change it in the
166      following ways:
167
168      (1) We do not want to reinitialize at each label.
169      (2) It is useful, but not critical, to know the actual value assigned
170          to a register.  Often just its form is helpful.
171
172      Therefore, we maintain the following fields:
173
174      last_set_value             the last value assigned
175      last_set_label             records the value of label_tick when the
176                                 register was assigned
177      last_set_table_tick        records the value of label_tick when a
178                                 value using the register is assigned
179      last_set_invalid           set to nonzero when it is not valid
180                                 to use the value of this register in some
181                                 register's value
182
183      To understand the usage of these tables, it is important to understand
184      the distinction between the value in last_set_value being valid and
185      the register being validly contained in some other expression in the
186      table.
187
188      (The next two parameters are out of date).
189
190      reg_stat[i].last_set_value is valid if it is nonzero, and either
191      reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
192
193      Register I may validly appear in any expression returned for the value
194      of another register if reg_n_sets[i] is 1.  It may also appear in the
195      value for register J if reg_stat[j].last_set_invalid is zero, or
196      reg_stat[i].last_set_label < reg_stat[j].last_set_label.
197
198      If an expression is found in the table containing a register which may
199      not validly appear in an expression, the register is replaced by
200      something that won't match, (clobber (const_int 0)).  */
201
202   /* Record last value assigned to (hard or pseudo) register n.  */
203
204   rtx                           last_set_value;
205
206   /* Record the value of label_tick when an expression involving register n
207      is placed in last_set_value.  */
208
209   int                           last_set_table_tick;
210
211   /* Record the value of label_tick when the value for register n is placed in
212      last_set_value.  */
213
214   int                           last_set_label;
215
216   /* These fields are maintained in parallel with last_set_value and are
217      used to store the mode in which the register was last set, the bits
218      that were known to be zero when it was last set, and the number of
219      sign bits copies it was known to have when it was last set.  */
220
221   unsigned HOST_WIDE_INT        last_set_nonzero_bits;
222   char                          last_set_sign_bit_copies;
223   ENUM_BITFIELD(machine_mode)   last_set_mode : 8; 
224
225   /* Set nonzero if references to register n in expressions should not be
226      used.  last_set_invalid is set nonzero when this register is being
227      assigned to and last_set_table_tick == label_tick.  */
228
229   char                          last_set_invalid;
230
231   /* Some registers that are set more than once and used in more than one
232      basic block are nevertheless always set in similar ways.  For example,
233      a QImode register may be loaded from memory in two places on a machine
234      where byte loads zero extend.
235
236      We record in the following fields if a register has some leading bits
237      that are always equal to the sign bit, and what we know about the
238      nonzero bits of a register, specifically which bits are known to be
239      zero.
240
241      If an entry is zero, it means that we don't know anything special.  */
242
243   unsigned char                 sign_bit_copies;
244
245   unsigned HOST_WIDE_INT        nonzero_bits;
246 };
247
248 static struct reg_stat *reg_stat;
249
250 /* Record the cuid of the last insn that invalidated memory
251    (anything that writes memory, and subroutine calls, but not pushes).  */
252
253 static int mem_last_set;
254
255 /* Record the cuid of the last CALL_INSN
256    so we can tell whether a potential combination crosses any calls.  */
257
258 static int last_call_cuid;
259
260 /* When `subst' is called, this is the insn that is being modified
261    (by combining in a previous insn).  The PATTERN of this insn
262    is still the old pattern partially modified and it should not be
263    looked at, but this may be used to examine the successors of the insn
264    to judge whether a simplification is valid.  */
265
266 static rtx subst_insn;
267
268 /* This is the lowest CUID that `subst' is currently dealing with.
269    get_last_value will not return a value if the register was set at or
270    after this CUID.  If not for this mechanism, we could get confused if
271    I2 or I1 in try_combine were an insn that used the old value of a register
272    to obtain a new value.  In that case, we might erroneously get the
273    new value of the register when we wanted the old one.  */
274
275 static int subst_low_cuid;
276
277 /* This contains any hard registers that are used in newpat; reg_dead_at_p
278    must consider all these registers to be always live.  */
279
280 static HARD_REG_SET newpat_used_regs;
281
282 /* This is an insn to which a LOG_LINKS entry has been added.  If this
283    insn is the earlier than I2 or I3, combine should rescan starting at
284    that location.  */
285
286 static rtx added_links_insn;
287
288 /* Basic block in which we are performing combines.  */
289 static basic_block this_basic_block;
290
291 /* A bitmap indicating which blocks had registers go dead at entry.
292    After combine, we'll need to re-do global life analysis with
293    those blocks as starting points.  */
294 static sbitmap refresh_blocks;
295 \f
296 /* The following array records the insn_rtx_cost for every insn
297    in the instruction stream.  */
298
299 static int *uid_insn_cost;
300
301 /* Length of the currently allocated uid_insn_cost array.  */
302
303 static int last_insn_cost;
304
305 /* Incremented for each label.  */
306
307 static int label_tick;
308
309 /* Mode used to compute significance in reg_stat[].nonzero_bits.  It is the
310    largest integer mode that can fit in HOST_BITS_PER_WIDE_INT.  */
311
312 static enum machine_mode nonzero_bits_mode;
313
314 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
315    be safely used.  It is zero while computing them and after combine has
316    completed.  This former test prevents propagating values based on
317    previously set values, which can be incorrect if a variable is modified
318    in a loop.  */
319
320 static int nonzero_sign_valid;
321
322 \f
323 /* Record one modification to rtl structure
324    to be undone by storing old_contents into *where.
325    is_int is 1 if the contents are an int.  */
326
327 struct undo
328 {
329   struct undo *next;
330   int is_int;
331   union {rtx r; int i;} old_contents;
332   union {rtx *r; int *i;} where;
333 };
334
335 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
336    num_undo says how many are currently recorded.
337
338    other_insn is nonzero if we have modified some other insn in the process
339    of working on subst_insn.  It must be verified too.  */
340
341 struct undobuf
342 {
343   struct undo *undos;
344   struct undo *frees;
345   rtx other_insn;
346 };
347
348 static struct undobuf undobuf;
349
350 /* Number of times the pseudo being substituted for
351    was found and replaced.  */
352
353 static int n_occurrences;
354
355 static rtx reg_nonzero_bits_for_combine (rtx, enum machine_mode, rtx,
356                                          enum machine_mode,
357                                          unsigned HOST_WIDE_INT,
358                                          unsigned HOST_WIDE_INT *);
359 static rtx reg_num_sign_bit_copies_for_combine (rtx, enum machine_mode, rtx,
360                                                 enum machine_mode,
361                                                 unsigned int, unsigned int *);
362 static void do_SUBST (rtx *, rtx);
363 static void do_SUBST_INT (int *, int);
364 static void init_reg_last (void);
365 static void setup_incoming_promotions (void);
366 static void set_nonzero_bits_and_sign_copies (rtx, rtx, void *);
367 static int cant_combine_insn_p (rtx);
368 static int can_combine_p (rtx, rtx, rtx, rtx, rtx *, rtx *);
369 static int combinable_i3pat (rtx, rtx *, rtx, rtx, int, rtx *);
370 static int contains_muldiv (rtx);
371 static rtx try_combine (rtx, rtx, rtx, int *);
372 static void undo_all (void);
373 static void undo_commit (void);
374 static rtx *find_split_point (rtx *, rtx);
375 static rtx subst (rtx, rtx, rtx, int, int);
376 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
377 static rtx simplify_if_then_else (rtx);
378 static rtx simplify_set (rtx);
379 static rtx simplify_logical (rtx);
380 static rtx expand_compound_operation (rtx);
381 static rtx expand_field_assignment (rtx);
382 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
383                             rtx, unsigned HOST_WIDE_INT, int, int, int);
384 static rtx extract_left_shift (rtx, int);
385 static rtx make_compound_operation (rtx, enum rtx_code);
386 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
387                               unsigned HOST_WIDE_INT *);
388 static rtx canon_reg_for_combine (rtx, rtx);
389 static rtx force_to_mode (rtx, enum machine_mode,
390                           unsigned HOST_WIDE_INT, int);
391 static rtx if_then_else_cond (rtx, rtx *, rtx *);
392 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
393 static int rtx_equal_for_field_assignment_p (rtx, rtx);
394 static rtx make_field_assignment (rtx);
395 static rtx apply_distributive_law (rtx);
396 static rtx distribute_and_simplify_rtx (rtx, int);
397 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
398                                    unsigned HOST_WIDE_INT);
399 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
400                             HOST_WIDE_INT, enum machine_mode, int *);
401 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
402                                  int);
403 static int recog_for_combine (rtx *, rtx, rtx *);
404 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
405 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
406 static void update_table_tick (rtx);
407 static void record_value_for_reg (rtx, rtx, rtx);
408 static void check_promoted_subreg (rtx, rtx);
409 static void record_dead_and_set_regs_1 (rtx, rtx, void *);
410 static void record_dead_and_set_regs (rtx);
411 static int get_last_value_validate (rtx *, rtx, int, int);
412 static rtx get_last_value (rtx);
413 static int use_crosses_set_p (rtx, int);
414 static void reg_dead_at_p_1 (rtx, rtx, void *);
415 static int reg_dead_at_p (rtx, rtx);
416 static void move_deaths (rtx, rtx, int, rtx, rtx *);
417 static int reg_bitfield_target_p (rtx, rtx);
418 static void distribute_notes (rtx, rtx, rtx, rtx, rtx, rtx);
419 static void distribute_links (rtx);
420 static void mark_used_regs_combine (rtx);
421 static int insn_cuid (rtx);
422 static void record_promoted_value (rtx, rtx);
423 static int unmentioned_reg_p_1 (rtx *, void *);
424 static bool unmentioned_reg_p (rtx, rtx);
425 \f
426
427 /* It is not safe to use ordinary gen_lowpart in combine.
428    See comments in gen_lowpart_for_combine.  */
429 #undef RTL_HOOKS_GEN_LOWPART
430 #define RTL_HOOKS_GEN_LOWPART              gen_lowpart_for_combine
431
432 /* Our implementation of gen_lowpart never emits a new pseudo.  */
433 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
434 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT      gen_lowpart_for_combine
435
436 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
437 #define RTL_HOOKS_REG_NONZERO_REG_BITS     reg_nonzero_bits_for_combine
438
439 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
440 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES  reg_num_sign_bit_copies_for_combine
441
442 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
443
444 \f
445 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
446    insn.  The substitution can be undone by undo_all.  If INTO is already
447    set to NEWVAL, do not record this change.  Because computing NEWVAL might
448    also call SUBST, we have to compute it before we put anything into
449    the undo table.  */
450
451 static void
452 do_SUBST (rtx *into, rtx newval)
453 {
454   struct undo *buf;
455   rtx oldval = *into;
456
457   if (oldval == newval)
458     return;
459
460   /* We'd like to catch as many invalid transformations here as
461      possible.  Unfortunately, there are way too many mode changes
462      that are perfectly valid, so we'd waste too much effort for
463      little gain doing the checks here.  Focus on catching invalid
464      transformations involving integer constants.  */
465   if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
466       && GET_CODE (newval) == CONST_INT)
467     {
468       /* Sanity check that we're replacing oldval with a CONST_INT
469          that is a valid sign-extension for the original mode.  */
470       gcc_assert (INTVAL (newval)
471                   == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
472
473       /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
474          CONST_INT is not valid, because after the replacement, the
475          original mode would be gone.  Unfortunately, we can't tell
476          when do_SUBST is called to replace the operand thereof, so we
477          perform this test on oldval instead, checking whether an
478          invalid replacement took place before we got here.  */
479       gcc_assert (!(GET_CODE (oldval) == SUBREG
480                     && GET_CODE (SUBREG_REG (oldval)) == CONST_INT));
481       gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
482                     && GET_CODE (XEXP (oldval, 0)) == CONST_INT));
483     }
484
485   if (undobuf.frees)
486     buf = undobuf.frees, undobuf.frees = buf->next;
487   else
488     buf = xmalloc (sizeof (struct undo));
489
490   buf->is_int = 0;
491   buf->where.r = into;
492   buf->old_contents.r = oldval;
493   *into = newval;
494
495   buf->next = undobuf.undos, undobuf.undos = buf;
496 }
497
498 #define SUBST(INTO, NEWVAL)     do_SUBST(&(INTO), (NEWVAL))
499
500 /* Similar to SUBST, but NEWVAL is an int expression.  Note that substitution
501    for the value of a HOST_WIDE_INT value (including CONST_INT) is
502    not safe.  */
503
504 static void
505 do_SUBST_INT (int *into, int newval)
506 {
507   struct undo *buf;
508   int oldval = *into;
509
510   if (oldval == newval)
511     return;
512
513   if (undobuf.frees)
514     buf = undobuf.frees, undobuf.frees = buf->next;
515   else
516     buf = xmalloc (sizeof (struct undo));
517
518   buf->is_int = 1;
519   buf->where.i = into;
520   buf->old_contents.i = oldval;
521   *into = newval;
522
523   buf->next = undobuf.undos, undobuf.undos = buf;
524 }
525
526 #define SUBST_INT(INTO, NEWVAL)  do_SUBST_INT(&(INTO), (NEWVAL))
527 \f
528 /* Subroutine of try_combine.  Determine whether the combine replacement
529    patterns NEWPAT and NEWI2PAT are cheaper according to insn_rtx_cost
530    that the original instruction sequence I1, I2 and I3.  Note that I1
531    and/or NEWI2PAT may be NULL_RTX.  This function returns false, if the
532    costs of all instructions can be estimated, and the replacements are
533    more expensive than the original sequence.  */
534
535 static bool
536 combine_validate_cost (rtx i1, rtx i2, rtx i3, rtx newpat, rtx newi2pat)
537 {
538   int i1_cost, i2_cost, i3_cost;
539   int new_i2_cost, new_i3_cost;
540   int old_cost, new_cost;
541
542   /* Lookup the original insn_rtx_costs.  */
543   i2_cost = INSN_UID (i2) <= last_insn_cost
544             ? uid_insn_cost[INSN_UID (i2)] : 0;
545   i3_cost = INSN_UID (i3) <= last_insn_cost
546             ? uid_insn_cost[INSN_UID (i3)] : 0;
547
548   if (i1)
549     {
550       i1_cost = INSN_UID (i1) <= last_insn_cost
551                 ? uid_insn_cost[INSN_UID (i1)] : 0;
552       old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0)
553                  ? i1_cost + i2_cost + i3_cost : 0;
554     }
555   else
556     {
557       old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
558       i1_cost = 0;
559     }
560
561   /* Calculate the replacement insn_rtx_costs.  */
562   new_i3_cost = insn_rtx_cost (newpat);
563   if (newi2pat)
564     {
565       new_i2_cost = insn_rtx_cost (newi2pat);
566       new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
567                  ? new_i2_cost + new_i3_cost : 0;
568     }
569   else
570     {
571       new_cost = new_i3_cost;
572       new_i2_cost = 0;
573     }
574
575   if (undobuf.other_insn)
576     {
577       int old_other_cost, new_other_cost;
578
579       old_other_cost = (INSN_UID (undobuf.other_insn) <= last_insn_cost
580                         ? uid_insn_cost[INSN_UID (undobuf.other_insn)] : 0);
581       new_other_cost = insn_rtx_cost (PATTERN (undobuf.other_insn));
582       if (old_other_cost > 0 && new_other_cost > 0)
583         {
584           old_cost += old_other_cost;
585           new_cost += new_other_cost;
586         }
587       else
588         old_cost = 0;
589     }
590
591   /* Disallow this recombination if both new_cost and old_cost are
592      greater than zero, and new_cost is greater than old cost.  */
593   if (old_cost > 0
594       && new_cost > old_cost)
595     {
596       if (dump_file)
597         {
598           if (i1)
599             {
600               fprintf (dump_file,
601                        "rejecting combination of insns %d, %d and %d\n",
602                        INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
603               fprintf (dump_file, "original costs %d + %d + %d = %d\n",
604                        i1_cost, i2_cost, i3_cost, old_cost);
605             }
606           else
607             {
608               fprintf (dump_file,
609                        "rejecting combination of insns %d and %d\n",
610                        INSN_UID (i2), INSN_UID (i3));
611               fprintf (dump_file, "original costs %d + %d = %d\n",
612                        i2_cost, i3_cost, old_cost);
613             }
614
615           if (newi2pat)
616             {
617               fprintf (dump_file, "replacement costs %d + %d = %d\n",
618                        new_i2_cost, new_i3_cost, new_cost);
619             }
620           else
621             fprintf (dump_file, "replacement cost %d\n", new_cost);
622         }
623
624       return false;
625     }
626
627   /* Update the uid_insn_cost array with the replacement costs.  */
628   uid_insn_cost[INSN_UID (i2)] = new_i2_cost;
629   uid_insn_cost[INSN_UID (i3)] = new_i3_cost;
630   if (i1)
631     uid_insn_cost[INSN_UID (i1)] = 0;
632
633   return true;
634 }
635 \f
636 /* Main entry point for combiner.  F is the first insn of the function.
637    NREGS is the first unused pseudo-reg number.
638
639    Return nonzero if the combiner has turned an indirect jump
640    instruction into a direct jump.  */
641 int
642 combine_instructions (rtx f, unsigned int nregs)
643 {
644   rtx insn, next;
645 #ifdef HAVE_cc0
646   rtx prev;
647 #endif
648   int i;
649   unsigned int j = 0;
650   rtx links, nextlinks;
651   sbitmap_iterator sbi;
652
653   int new_direct_jump_p = 0;
654
655   combine_attempts = 0;
656   combine_merges = 0;
657   combine_extras = 0;
658   combine_successes = 0;
659
660   combine_max_regno = nregs;
661
662   rtl_hooks = combine_rtl_hooks;
663
664   reg_stat = xcalloc (nregs, sizeof (struct reg_stat));
665
666   init_recog_no_volatile ();
667
668   /* Compute maximum uid value so uid_cuid can be allocated.  */
669
670   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
671     if (INSN_UID (insn) > i)
672       i = INSN_UID (insn);
673
674   uid_cuid = xmalloc ((i + 1) * sizeof (int));
675   max_uid_cuid = i;
676
677   nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
678
679   /* Don't use reg_stat[].nonzero_bits when computing it.  This can cause
680      problems when, for example, we have j <<= 1 in a loop.  */
681
682   nonzero_sign_valid = 0;
683
684   /* Compute the mapping from uids to cuids.
685      Cuids are numbers assigned to insns, like uids,
686      except that cuids increase monotonically through the code.
687
688      Scan all SETs and see if we can deduce anything about what
689      bits are known to be zero for some registers and how many copies
690      of the sign bit are known to exist for those registers.
691
692      Also set any known values so that we can use it while searching
693      for what bits are known to be set.  */
694
695   label_tick = 1;
696
697   setup_incoming_promotions ();
698
699   refresh_blocks = sbitmap_alloc (last_basic_block);
700   sbitmap_zero (refresh_blocks);
701
702   /* Allocate array of current insn_rtx_costs.  */
703   uid_insn_cost = xcalloc (max_uid_cuid + 1, sizeof (int));
704   last_insn_cost = max_uid_cuid;
705
706   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
707     {
708       uid_cuid[INSN_UID (insn)] = ++i;
709       subst_low_cuid = i;
710       subst_insn = insn;
711
712       if (INSN_P (insn))
713         {
714           note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
715                        NULL);
716           record_dead_and_set_regs (insn);
717
718 #ifdef AUTO_INC_DEC
719           for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
720             if (REG_NOTE_KIND (links) == REG_INC)
721               set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
722                                                 NULL);
723 #endif
724
725           /* Record the current insn_rtx_cost of this instruction.  */
726           if (NONJUMP_INSN_P (insn))
727             uid_insn_cost[INSN_UID (insn)] = insn_rtx_cost (PATTERN (insn));
728           if (dump_file)
729             fprintf(dump_file, "insn_cost %d: %d\n",
730                     INSN_UID (insn), uid_insn_cost[INSN_UID (insn)]);
731         }
732
733       if (LABEL_P (insn))
734         label_tick++;
735     }
736
737   nonzero_sign_valid = 1;
738
739   /* Now scan all the insns in forward order.  */
740
741   label_tick = 1;
742   last_call_cuid = 0;
743   mem_last_set = 0;
744   init_reg_last ();
745   setup_incoming_promotions ();
746
747   FOR_EACH_BB (this_basic_block)
748     {
749       for (insn = BB_HEAD (this_basic_block);
750            insn != NEXT_INSN (BB_END (this_basic_block));
751            insn = next ? next : NEXT_INSN (insn))
752         {
753           next = 0;
754
755           if (LABEL_P (insn))
756             label_tick++;
757
758           else if (INSN_P (insn))
759             {
760               /* See if we know about function return values before this
761                  insn based upon SUBREG flags.  */
762               check_promoted_subreg (insn, PATTERN (insn));
763
764               /* Try this insn with each insn it links back to.  */
765
766               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
767                 if ((next = try_combine (insn, XEXP (links, 0),
768                                          NULL_RTX, &new_direct_jump_p)) != 0)
769                   goto retry;
770
771               /* Try each sequence of three linked insns ending with this one.  */
772
773               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
774                 {
775                   rtx link = XEXP (links, 0);
776
777                   /* If the linked insn has been replaced by a note, then there
778                      is no point in pursuing this chain any further.  */
779                   if (NOTE_P (link))
780                     continue;
781
782                   for (nextlinks = LOG_LINKS (link);
783                        nextlinks;
784                        nextlinks = XEXP (nextlinks, 1))
785                     if ((next = try_combine (insn, link,
786                                              XEXP (nextlinks, 0),
787                                              &new_direct_jump_p)) != 0)
788                       goto retry;
789                 }
790
791 #ifdef HAVE_cc0
792               /* Try to combine a jump insn that uses CC0
793                  with a preceding insn that sets CC0, and maybe with its
794                  logical predecessor as well.
795                  This is how we make decrement-and-branch insns.
796                  We need this special code because data flow connections
797                  via CC0 do not get entered in LOG_LINKS.  */
798
799               if (JUMP_P (insn)
800                   && (prev = prev_nonnote_insn (insn)) != 0
801                   && NONJUMP_INSN_P (prev)
802                   && sets_cc0_p (PATTERN (prev)))
803                 {
804                   if ((next = try_combine (insn, prev,
805                                            NULL_RTX, &new_direct_jump_p)) != 0)
806                     goto retry;
807
808                   for (nextlinks = LOG_LINKS (prev); nextlinks;
809                        nextlinks = XEXP (nextlinks, 1))
810                     if ((next = try_combine (insn, prev,
811                                              XEXP (nextlinks, 0),
812                                              &new_direct_jump_p)) != 0)
813                       goto retry;
814                 }
815
816               /* Do the same for an insn that explicitly references CC0.  */
817               if (NONJUMP_INSN_P (insn)
818                   && (prev = prev_nonnote_insn (insn)) != 0
819                   && NONJUMP_INSN_P (prev)
820                   && sets_cc0_p (PATTERN (prev))
821                   && GET_CODE (PATTERN (insn)) == SET
822                   && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
823                 {
824                   if ((next = try_combine (insn, prev,
825                                            NULL_RTX, &new_direct_jump_p)) != 0)
826                     goto retry;
827
828                   for (nextlinks = LOG_LINKS (prev); nextlinks;
829                        nextlinks = XEXP (nextlinks, 1))
830                     if ((next = try_combine (insn, prev,
831                                              XEXP (nextlinks, 0),
832                                              &new_direct_jump_p)) != 0)
833                       goto retry;
834                 }
835
836               /* Finally, see if any of the insns that this insn links to
837                  explicitly references CC0.  If so, try this insn, that insn,
838                  and its predecessor if it sets CC0.  */
839               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
840                 if (NONJUMP_INSN_P (XEXP (links, 0))
841                     && GET_CODE (PATTERN (XEXP (links, 0))) == SET
842                     && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
843                     && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
844                     && NONJUMP_INSN_P (prev)
845                     && sets_cc0_p (PATTERN (prev))
846                     && (next = try_combine (insn, XEXP (links, 0),
847                                             prev, &new_direct_jump_p)) != 0)
848                   goto retry;
849 #endif
850
851               /* Try combining an insn with two different insns whose results it
852                  uses.  */
853               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
854                 for (nextlinks = XEXP (links, 1); nextlinks;
855                      nextlinks = XEXP (nextlinks, 1))
856                   if ((next = try_combine (insn, XEXP (links, 0),
857                                            XEXP (nextlinks, 0),
858                                            &new_direct_jump_p)) != 0)
859                     goto retry;
860
861               /* Try this insn with each REG_EQUAL note it links back to.  */
862               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
863                 {
864                   rtx set, note;
865                   rtx temp = XEXP (links, 0);
866                   if ((set = single_set (temp)) != 0
867                       && (note = find_reg_equal_equiv_note (temp)) != 0
868                       && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
869                       /* Avoid using a register that may already been marked
870                          dead by an earlier instruction.  */
871                       && ! unmentioned_reg_p (note, SET_SRC (set))
872                       && (GET_MODE (note) == VOIDmode
873                           ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
874                           : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
875                     {
876                       /* Temporarily replace the set's source with the
877                          contents of the REG_EQUAL note.  The insn will
878                          be deleted or recognized by try_combine.  */
879                       rtx orig = SET_SRC (set);
880                       SET_SRC (set) = note;
881                       next = try_combine (insn, temp, NULL_RTX,
882                                           &new_direct_jump_p);
883                       if (next)
884                         goto retry;
885                       SET_SRC (set) = orig;
886                     }
887                 }
888
889               if (!NOTE_P (insn))
890                 record_dead_and_set_regs (insn);
891
892             retry:
893               ;
894             }
895         }
896     }
897   clear_bb_flags ();
898
899   EXECUTE_IF_SET_IN_SBITMAP (refresh_blocks, 0, j, sbi)
900     BASIC_BLOCK (j)->flags |= BB_DIRTY;
901   new_direct_jump_p |= purge_all_dead_edges ();
902   delete_noop_moves ();
903
904   update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
905                                     PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
906                                     | PROP_KILL_DEAD_CODE);
907
908   /* Clean up.  */
909   sbitmap_free (refresh_blocks);
910   free (uid_insn_cost);
911   free (reg_stat);
912   free (uid_cuid);
913
914   {
915     struct undo *undo, *next;
916     for (undo = undobuf.frees; undo; undo = next)
917       {
918         next = undo->next;
919         free (undo);
920       }
921     undobuf.frees = 0;
922   }
923
924   total_attempts += combine_attempts;
925   total_merges += combine_merges;
926   total_extras += combine_extras;
927   total_successes += combine_successes;
928
929   nonzero_sign_valid = 0;
930   rtl_hooks = general_rtl_hooks;
931
932   /* Make recognizer allow volatile MEMs again.  */
933   init_recog ();
934
935   return new_direct_jump_p;
936 }
937
938 /* Wipe the last_xxx fields of reg_stat in preparation for another pass.  */
939
940 static void
941 init_reg_last (void)
942 {
943   unsigned int i;
944   for (i = 0; i < combine_max_regno; i++)
945     memset (reg_stat + i, 0, offsetof (struct reg_stat, sign_bit_copies));
946 }
947 \f
948 /* Set up any promoted values for incoming argument registers.  */
949
950 static void
951 setup_incoming_promotions (void)
952 {
953   unsigned int regno;
954   rtx reg;
955   enum machine_mode mode;
956   int unsignedp;
957   rtx first = get_insns ();
958
959   if (targetm.calls.promote_function_args (TREE_TYPE (cfun->decl)))
960     {
961       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
962         /* Check whether this register can hold an incoming pointer
963            argument.  FUNCTION_ARG_REGNO_P tests outgoing register
964            numbers, so translate if necessary due to register windows.  */
965         if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (regno))
966             && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
967           {
968             record_value_for_reg
969               (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
970                                            : SIGN_EXTEND),
971                                           GET_MODE (reg),
972                                           gen_rtx_CLOBBER (mode, const0_rtx)));
973           }
974     }
975 }
976 \f
977 /* Called via note_stores.  If X is a pseudo that is narrower than
978    HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
979
980    If we are setting only a portion of X and we can't figure out what
981    portion, assume all bits will be used since we don't know what will
982    be happening.
983
984    Similarly, set how many bits of X are known to be copies of the sign bit
985    at all locations in the function.  This is the smallest number implied
986    by any set of X.  */
987
988 static void
989 set_nonzero_bits_and_sign_copies (rtx x, rtx set,
990                                   void *data ATTRIBUTE_UNUSED)
991 {
992   unsigned int num;
993
994   if (REG_P (x)
995       && REGNO (x) >= FIRST_PSEUDO_REGISTER
996       /* If this register is undefined at the start of the file, we can't
997          say what its contents were.  */
998       && ! REGNO_REG_SET_P
999          (ENTRY_BLOCK_PTR->next_bb->il.rtl->global_live_at_start, REGNO (x))
1000       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
1001     {
1002       if (set == 0 || GET_CODE (set) == CLOBBER)
1003         {
1004           reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1005           reg_stat[REGNO (x)].sign_bit_copies = 1;
1006           return;
1007         }
1008
1009       /* If this is a complex assignment, see if we can convert it into a
1010          simple assignment.  */
1011       set = expand_field_assignment (set);
1012
1013       /* If this is a simple assignment, or we have a paradoxical SUBREG,
1014          set what we know about X.  */
1015
1016       if (SET_DEST (set) == x
1017           || (GET_CODE (SET_DEST (set)) == SUBREG
1018               && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
1019                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
1020               && SUBREG_REG (SET_DEST (set)) == x))
1021         {
1022           rtx src = SET_SRC (set);
1023
1024 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1025           /* If X is narrower than a word and SRC is a non-negative
1026              constant that would appear negative in the mode of X,
1027              sign-extend it for use in reg_stat[].nonzero_bits because some
1028              machines (maybe most) will actually do the sign-extension
1029              and this is the conservative approach.
1030
1031              ??? For 2.5, try to tighten up the MD files in this regard
1032              instead of this kludge.  */
1033
1034           if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
1035               && GET_CODE (src) == CONST_INT
1036               && INTVAL (src) > 0
1037               && 0 != (INTVAL (src)
1038                        & ((HOST_WIDE_INT) 1
1039                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
1040             src = GEN_INT (INTVAL (src)
1041                            | ((HOST_WIDE_INT) (-1)
1042                               << GET_MODE_BITSIZE (GET_MODE (x))));
1043 #endif
1044
1045           /* Don't call nonzero_bits if it cannot change anything.  */
1046           if (reg_stat[REGNO (x)].nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1047             reg_stat[REGNO (x)].nonzero_bits
1048               |= nonzero_bits (src, nonzero_bits_mode);
1049           num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1050           if (reg_stat[REGNO (x)].sign_bit_copies == 0
1051               || reg_stat[REGNO (x)].sign_bit_copies > num)
1052             reg_stat[REGNO (x)].sign_bit_copies = num;
1053         }
1054       else
1055         {
1056           reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1057           reg_stat[REGNO (x)].sign_bit_copies = 1;
1058         }
1059     }
1060 }
1061 \f
1062 /* See if INSN can be combined into I3.  PRED and SUCC are optionally
1063    insns that were previously combined into I3 or that will be combined
1064    into the merger of INSN and I3.
1065
1066    Return 0 if the combination is not allowed for any reason.
1067
1068    If the combination is allowed, *PDEST will be set to the single
1069    destination of INSN and *PSRC to the single source, and this function
1070    will return 1.  */
1071
1072 static int
1073 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
1074                rtx *pdest, rtx *psrc)
1075 {
1076   int i;
1077   rtx set = 0, src, dest;
1078   rtx p;
1079 #ifdef AUTO_INC_DEC
1080   rtx link;
1081 #endif
1082   int all_adjacent = (succ ? (next_active_insn (insn) == succ
1083                               && next_active_insn (succ) == i3)
1084                       : next_active_insn (insn) == i3);
1085
1086   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1087      or a PARALLEL consisting of such a SET and CLOBBERs.
1088
1089      If INSN has CLOBBER parallel parts, ignore them for our processing.
1090      By definition, these happen during the execution of the insn.  When it
1091      is merged with another insn, all bets are off.  If they are, in fact,
1092      needed and aren't also supplied in I3, they may be added by
1093      recog_for_combine.  Otherwise, it won't match.
1094
1095      We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1096      note.
1097
1098      Get the source and destination of INSN.  If more than one, can't
1099      combine.  */
1100
1101   if (GET_CODE (PATTERN (insn)) == SET)
1102     set = PATTERN (insn);
1103   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1104            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1105     {
1106       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1107         {
1108           rtx elt = XVECEXP (PATTERN (insn), 0, i);
1109           rtx note;
1110
1111           switch (GET_CODE (elt))
1112             {
1113             /* This is important to combine floating point insns
1114                for the SH4 port.  */
1115             case USE:
1116               /* Combining an isolated USE doesn't make sense.
1117                  We depend here on combinable_i3pat to reject them.  */
1118               /* The code below this loop only verifies that the inputs of
1119                  the SET in INSN do not change.  We call reg_set_between_p
1120                  to verify that the REG in the USE does not change between
1121                  I3 and INSN.
1122                  If the USE in INSN was for a pseudo register, the matching
1123                  insn pattern will likely match any register; combining this
1124                  with any other USE would only be safe if we knew that the
1125                  used registers have identical values, or if there was
1126                  something to tell them apart, e.g. different modes.  For
1127                  now, we forgo such complicated tests and simply disallow
1128                  combining of USES of pseudo registers with any other USE.  */
1129               if (REG_P (XEXP (elt, 0))
1130                   && GET_CODE (PATTERN (i3)) == PARALLEL)
1131                 {
1132                   rtx i3pat = PATTERN (i3);
1133                   int i = XVECLEN (i3pat, 0) - 1;
1134                   unsigned int regno = REGNO (XEXP (elt, 0));
1135
1136                   do
1137                     {
1138                       rtx i3elt = XVECEXP (i3pat, 0, i);
1139
1140                       if (GET_CODE (i3elt) == USE
1141                           && REG_P (XEXP (i3elt, 0))
1142                           && (REGNO (XEXP (i3elt, 0)) == regno
1143                               ? reg_set_between_p (XEXP (elt, 0),
1144                                                    PREV_INSN (insn), i3)
1145                               : regno >= FIRST_PSEUDO_REGISTER))
1146                         return 0;
1147                     }
1148                   while (--i >= 0);
1149                 }
1150               break;
1151
1152               /* We can ignore CLOBBERs.  */
1153             case CLOBBER:
1154               break;
1155
1156             case SET:
1157               /* Ignore SETs whose result isn't used but not those that
1158                  have side-effects.  */
1159               if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1160                   && (!(note = find_reg_note (insn, REG_EH_REGION, NULL_RTX))
1161                       || INTVAL (XEXP (note, 0)) <= 0)
1162                   && ! side_effects_p (elt))
1163                 break;
1164
1165               /* If we have already found a SET, this is a second one and
1166                  so we cannot combine with this insn.  */
1167               if (set)
1168                 return 0;
1169
1170               set = elt;
1171               break;
1172
1173             default:
1174               /* Anything else means we can't combine.  */
1175               return 0;
1176             }
1177         }
1178
1179       if (set == 0
1180           /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1181              so don't do anything with it.  */
1182           || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1183         return 0;
1184     }
1185   else
1186     return 0;
1187
1188   if (set == 0)
1189     return 0;
1190
1191   set = expand_field_assignment (set);
1192   src = SET_SRC (set), dest = SET_DEST (set);
1193
1194   /* Don't eliminate a store in the stack pointer.  */
1195   if (dest == stack_pointer_rtx
1196       /* Don't combine with an insn that sets a register to itself if it has
1197          a REG_EQUAL note.  This may be part of a REG_NO_CONFLICT sequence.  */
1198       || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1199       /* Can't merge an ASM_OPERANDS.  */
1200       || GET_CODE (src) == ASM_OPERANDS
1201       /* Can't merge a function call.  */
1202       || GET_CODE (src) == CALL
1203       /* Don't eliminate a function call argument.  */
1204       || (CALL_P (i3)
1205           && (find_reg_fusage (i3, USE, dest)
1206               || (REG_P (dest)
1207                   && REGNO (dest) < FIRST_PSEUDO_REGISTER
1208                   && global_regs[REGNO (dest)])))
1209       /* Don't substitute into an incremented register.  */
1210       || FIND_REG_INC_NOTE (i3, dest)
1211       || (succ && FIND_REG_INC_NOTE (succ, dest))
1212       /* Don't substitute into a non-local goto, this confuses CFG.  */
1213       || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1214 #if 0
1215       /* Don't combine the end of a libcall into anything.  */
1216       /* ??? This gives worse code, and appears to be unnecessary, since no
1217          pass after flow uses REG_LIBCALL/REG_RETVAL notes.  Local-alloc does
1218          use REG_RETVAL notes for noconflict blocks, but other code here
1219          makes sure that those insns don't disappear.  */
1220       || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1221 #endif
1222       /* Make sure that DEST is not used after SUCC but before I3.  */
1223       || (succ && ! all_adjacent
1224           && reg_used_between_p (dest, succ, i3))
1225       /* Make sure that the value that is to be substituted for the register
1226          does not use any registers whose values alter in between.  However,
1227          If the insns are adjacent, a use can't cross a set even though we
1228          think it might (this can happen for a sequence of insns each setting
1229          the same destination; last_set of that register might point to
1230          a NOTE).  If INSN has a REG_EQUIV note, the register is always
1231          equivalent to the memory so the substitution is valid even if there
1232          are intervening stores.  Also, don't move a volatile asm or
1233          UNSPEC_VOLATILE across any other insns.  */
1234       || (! all_adjacent
1235           && (((!MEM_P (src)
1236                 || ! find_reg_note (insn, REG_EQUIV, src))
1237                && use_crosses_set_p (src, INSN_CUID (insn)))
1238               || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1239               || GET_CODE (src) == UNSPEC_VOLATILE))
1240       /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1241          better register allocation by not doing the combine.  */
1242       || find_reg_note (i3, REG_NO_CONFLICT, dest)
1243       || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1244       /* Don't combine across a CALL_INSN, because that would possibly
1245          change whether the life span of some REGs crosses calls or not,
1246          and it is a pain to update that information.
1247          Exception: if source is a constant, moving it later can't hurt.
1248          Accept that special case, because it helps -fforce-addr a lot.  */
1249       || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
1250     return 0;
1251
1252   /* DEST must either be a REG or CC0.  */
1253   if (REG_P (dest))
1254     {
1255       /* If register alignment is being enforced for multi-word items in all
1256          cases except for parameters, it is possible to have a register copy
1257          insn referencing a hard register that is not allowed to contain the
1258          mode being copied and which would not be valid as an operand of most
1259          insns.  Eliminate this problem by not combining with such an insn.
1260
1261          Also, on some machines we don't want to extend the life of a hard
1262          register.  */
1263
1264       if (REG_P (src)
1265           && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1266                && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1267               /* Don't extend the life of a hard register unless it is
1268                  user variable (if we have few registers) or it can't
1269                  fit into the desired register (meaning something special
1270                  is going on).
1271                  Also avoid substituting a return register into I3, because
1272                  reload can't handle a conflict with constraints of other
1273                  inputs.  */
1274               || (REGNO (src) < FIRST_PSEUDO_REGISTER
1275                   && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1276         return 0;
1277     }
1278   else if (GET_CODE (dest) != CC0)
1279     return 0;
1280
1281
1282   if (GET_CODE (PATTERN (i3)) == PARALLEL)
1283     for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1284       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1285         {
1286           /* Don't substitute for a register intended as a clobberable
1287              operand.  */
1288           rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1289           if (rtx_equal_p (reg, dest))
1290             return 0;
1291
1292           /* If the clobber represents an earlyclobber operand, we must not
1293              substitute an expression containing the clobbered register.
1294              As we do not analyze the constraint strings here, we have to
1295              make the conservative assumption.  However, if the register is
1296              a fixed hard reg, the clobber cannot represent any operand;
1297              we leave it up to the machine description to either accept or
1298              reject use-and-clobber patterns.  */
1299           if (!REG_P (reg)
1300               || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1301               || !fixed_regs[REGNO (reg)])
1302             if (reg_overlap_mentioned_p (reg, src))
1303               return 0;
1304         }
1305
1306   /* If INSN contains anything volatile, or is an `asm' (whether volatile
1307      or not), reject, unless nothing volatile comes between it and I3 */
1308
1309   if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1310     {
1311       /* Make sure succ doesn't contain a volatile reference.  */
1312       if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1313         return 0;
1314
1315       for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1316         if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1317           return 0;
1318     }
1319
1320   /* If INSN is an asm, and DEST is a hard register, reject, since it has
1321      to be an explicit register variable, and was chosen for a reason.  */
1322
1323   if (GET_CODE (src) == ASM_OPERANDS
1324       && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1325     return 0;
1326
1327   /* If there are any volatile insns between INSN and I3, reject, because
1328      they might affect machine state.  */
1329
1330   for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1331     if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1332       return 0;
1333
1334   /* If INSN contains an autoincrement or autodecrement, make sure that
1335      register is not used between there and I3, and not already used in
1336      I3 either.  Neither must it be used in PRED or SUCC, if they exist.
1337      Also insist that I3 not be a jump; if it were one
1338      and the incremented register were spilled, we would lose.  */
1339
1340 #ifdef AUTO_INC_DEC
1341   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1342     if (REG_NOTE_KIND (link) == REG_INC
1343         && (JUMP_P (i3)
1344             || reg_used_between_p (XEXP (link, 0), insn, i3)
1345             || (pred != NULL_RTX
1346                 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1347             || (succ != NULL_RTX
1348                 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
1349             || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1350       return 0;
1351 #endif
1352
1353 #ifdef HAVE_cc0
1354   /* Don't combine an insn that follows a CC0-setting insn.
1355      An insn that uses CC0 must not be separated from the one that sets it.
1356      We do, however, allow I2 to follow a CC0-setting insn if that insn
1357      is passed as I1; in that case it will be deleted also.
1358      We also allow combining in this case if all the insns are adjacent
1359      because that would leave the two CC0 insns adjacent as well.
1360      It would be more logical to test whether CC0 occurs inside I1 or I2,
1361      but that would be much slower, and this ought to be equivalent.  */
1362
1363   p = prev_nonnote_insn (insn);
1364   if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1365       && ! all_adjacent)
1366     return 0;
1367 #endif
1368
1369   /* If we get here, we have passed all the tests and the combination is
1370      to be allowed.  */
1371
1372   *pdest = dest;
1373   *psrc = src;
1374
1375   return 1;
1376 }
1377 \f
1378 /* LOC is the location within I3 that contains its pattern or the component
1379    of a PARALLEL of the pattern.  We validate that it is valid for combining.
1380
1381    One problem is if I3 modifies its output, as opposed to replacing it
1382    entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1383    so would produce an insn that is not equivalent to the original insns.
1384
1385    Consider:
1386
1387          (set (reg:DI 101) (reg:DI 100))
1388          (set (subreg:SI (reg:DI 101) 0) <foo>)
1389
1390    This is NOT equivalent to:
1391
1392          (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1393                     (set (reg:DI 101) (reg:DI 100))])
1394
1395    Not only does this modify 100 (in which case it might still be valid
1396    if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1397
1398    We can also run into a problem if I2 sets a register that I1
1399    uses and I1 gets directly substituted into I3 (not via I2).  In that
1400    case, we would be getting the wrong value of I2DEST into I3, so we
1401    must reject the combination.  This case occurs when I2 and I1 both
1402    feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1403    If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1404    of a SET must prevent combination from occurring.
1405
1406    Before doing the above check, we first try to expand a field assignment
1407    into a set of logical operations.
1408
1409    If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1410    we place a register that is both set and used within I3.  If more than one
1411    such register is detected, we fail.
1412
1413    Return 1 if the combination is valid, zero otherwise.  */
1414
1415 static int
1416 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
1417                   int i1_not_in_src, rtx *pi3dest_killed)
1418 {
1419   rtx x = *loc;
1420
1421   if (GET_CODE (x) == SET)
1422     {
1423       rtx set = x ;
1424       rtx dest = SET_DEST (set);
1425       rtx src = SET_SRC (set);
1426       rtx inner_dest = dest;
1427       rtx subdest;
1428
1429       while (GET_CODE (inner_dest) == STRICT_LOW_PART
1430              || GET_CODE (inner_dest) == SUBREG
1431              || GET_CODE (inner_dest) == ZERO_EXTRACT)
1432         inner_dest = XEXP (inner_dest, 0);
1433
1434       /* Check for the case where I3 modifies its output, as discussed
1435          above.  We don't want to prevent pseudos from being combined
1436          into the address of a MEM, so only prevent the combination if
1437          i1 or i2 set the same MEM.  */
1438       if ((inner_dest != dest &&
1439            (!MEM_P (inner_dest)
1440             || rtx_equal_p (i2dest, inner_dest)
1441             || (i1dest && rtx_equal_p (i1dest, inner_dest)))
1442            && (reg_overlap_mentioned_p (i2dest, inner_dest)
1443                || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1444
1445           /* This is the same test done in can_combine_p except we can't test
1446              all_adjacent; we don't have to, since this instruction will stay
1447              in place, thus we are not considering increasing the lifetime of
1448              INNER_DEST.
1449
1450              Also, if this insn sets a function argument, combining it with
1451              something that might need a spill could clobber a previous
1452              function argument; the all_adjacent test in can_combine_p also
1453              checks this; here, we do a more specific test for this case.  */
1454
1455           || (REG_P (inner_dest)
1456               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1457               && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1458                                         GET_MODE (inner_dest))))
1459           || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1460         return 0;
1461
1462       /* If DEST is used in I3, it is being killed in this insn, so
1463          record that for later.  We have to consider paradoxical
1464          subregs here, since they kill the whole register, but we
1465          ignore partial subregs, STRICT_LOW_PART, etc.
1466          Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1467          STACK_POINTER_REGNUM, since these are always considered to be
1468          live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
1469       subdest = dest;
1470       if (GET_CODE (subdest) == SUBREG
1471           && (GET_MODE_SIZE (GET_MODE (subdest))
1472               >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
1473         subdest = SUBREG_REG (subdest);
1474       if (pi3dest_killed
1475           && REG_P (subdest)
1476           && reg_referenced_p (subdest, PATTERN (i3))
1477           && REGNO (subdest) != FRAME_POINTER_REGNUM
1478 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1479           && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
1480 #endif
1481 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1482           && (REGNO (subdest) != ARG_POINTER_REGNUM
1483               || ! fixed_regs [REGNO (subdest)])
1484 #endif
1485           && REGNO (subdest) != STACK_POINTER_REGNUM)
1486         {
1487           if (*pi3dest_killed)
1488             return 0;
1489
1490           *pi3dest_killed = subdest;
1491         }
1492     }
1493
1494   else if (GET_CODE (x) == PARALLEL)
1495     {
1496       int i;
1497
1498       for (i = 0; i < XVECLEN (x, 0); i++)
1499         if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1500                                 i1_not_in_src, pi3dest_killed))
1501           return 0;
1502     }
1503
1504   return 1;
1505 }
1506 \f
1507 /* Return 1 if X is an arithmetic expression that contains a multiplication
1508    and division.  We don't count multiplications by powers of two here.  */
1509
1510 static int
1511 contains_muldiv (rtx x)
1512 {
1513   switch (GET_CODE (x))
1514     {
1515     case MOD:  case DIV:  case UMOD:  case UDIV:
1516       return 1;
1517
1518     case MULT:
1519       return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1520                 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1521     default:
1522       if (BINARY_P (x))
1523         return contains_muldiv (XEXP (x, 0))
1524             || contains_muldiv (XEXP (x, 1));
1525
1526       if (UNARY_P (x))
1527         return contains_muldiv (XEXP (x, 0));
1528
1529       return 0;
1530     }
1531 }
1532 \f
1533 /* Determine whether INSN can be used in a combination.  Return nonzero if
1534    not.  This is used in try_combine to detect early some cases where we
1535    can't perform combinations.  */
1536
1537 static int
1538 cant_combine_insn_p (rtx insn)
1539 {
1540   rtx set;
1541   rtx src, dest;
1542
1543   /* If this isn't really an insn, we can't do anything.
1544      This can occur when flow deletes an insn that it has merged into an
1545      auto-increment address.  */
1546   if (! INSN_P (insn))
1547     return 1;
1548
1549   /* Never combine loads and stores involving hard regs that are likely
1550      to be spilled.  The register allocator can usually handle such
1551      reg-reg moves by tying.  If we allow the combiner to make
1552      substitutions of likely-spilled regs, reload might die.
1553      As an exception, we allow combinations involving fixed regs; these are
1554      not available to the register allocator so there's no risk involved.  */
1555
1556   set = single_set (insn);
1557   if (! set)
1558     return 0;
1559   src = SET_SRC (set);
1560   dest = SET_DEST (set);
1561   if (GET_CODE (src) == SUBREG)
1562     src = SUBREG_REG (src);
1563   if (GET_CODE (dest) == SUBREG)
1564     dest = SUBREG_REG (dest);
1565   if (REG_P (src) && REG_P (dest)
1566       && ((REGNO (src) < FIRST_PSEUDO_REGISTER
1567            && ! fixed_regs[REGNO (src)]
1568            && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
1569           || (REGNO (dest) < FIRST_PSEUDO_REGISTER
1570               && ! fixed_regs[REGNO (dest)]
1571               && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
1572     return 1;
1573
1574   return 0;
1575 }
1576
1577 struct likely_spilled_retval_info
1578 {
1579   unsigned regno, nregs;
1580   unsigned mask;
1581 };
1582
1583 /* Called via note_stores by likely_spilled_retval_p.  Remove from info->mask
1584    hard registers that are known to be written to / clobbered in full.  */
1585 static void
1586 likely_spilled_retval_1 (rtx x, rtx set, void *data)
1587 {
1588   struct likely_spilled_retval_info *info = data;
1589   unsigned regno, nregs;
1590   unsigned new_mask;
1591
1592   if (!REG_P (XEXP (set, 0)))
1593     return;
1594   regno = REGNO (x);
1595   if (regno >= info->regno + info->nregs)
1596     return;
1597   nregs = hard_regno_nregs[regno][GET_MODE (x)];
1598   if (regno + nregs <= info->regno)
1599     return;
1600   new_mask = (2U << (nregs - 1)) - 1;
1601   if (regno < info->regno)
1602     new_mask >>= info->regno - regno;
1603   else
1604     new_mask <<= regno - info->regno;
1605   info->mask &= new_mask;
1606 }
1607
1608 /* Return nonzero iff part of the return value is live during INSN, and
1609    it is likely spilled.  This can happen when more than one insn is needed
1610    to copy the return value, e.g. when we consider to combine into the
1611    second copy insn for a complex value.  */
1612
1613 static int
1614 likely_spilled_retval_p (rtx insn)
1615 {
1616   rtx use = BB_END (this_basic_block);
1617   rtx reg, p;
1618   unsigned regno, nregs;
1619   /* We assume here that no machine mode needs more than
1620      32 hard registers when the value overlaps with a register
1621      for which FUNCTION_VALUE_REGNO_P is true.  */
1622   unsigned mask;
1623   struct likely_spilled_retval_info info;
1624
1625   if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
1626     return 0;
1627   reg = XEXP (PATTERN (use), 0);
1628   if (!REG_P (reg) || !FUNCTION_VALUE_REGNO_P (REGNO (reg)))
1629     return 0;
1630   regno = REGNO (reg);
1631   nregs = hard_regno_nregs[regno][GET_MODE (reg)];
1632   if (nregs == 1)
1633     return 0;
1634   mask = (2U << (nregs - 1)) - 1;
1635
1636   /* Disregard parts of the return value that are set later.  */
1637   info.regno = regno;
1638   info.nregs = nregs;
1639   info.mask = mask;
1640   for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
1641     note_stores (PATTERN (insn), likely_spilled_retval_1, &info);
1642   mask = info.mask;
1643
1644   /* Check if any of the (probably) live return value registers is
1645      likely spilled.  */
1646   nregs --;
1647   do
1648     {
1649       if ((mask & 1 << nregs)
1650           && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (regno + nregs)))
1651         return 1;
1652     } while (nregs--);
1653   return 0;
1654 }
1655
1656 /* Adjust INSN after we made a change to its destination.
1657
1658    Changing the destination can invalidate notes that say something about
1659    the results of the insn and a LOG_LINK pointing to the insn.  */
1660
1661 static void
1662 adjust_for_new_dest (rtx insn)
1663 {
1664   rtx *loc;
1665
1666   /* For notes, be conservative and simply remove them.  */
1667   loc = &REG_NOTES (insn);
1668   while (*loc)
1669     {
1670       enum reg_note kind = REG_NOTE_KIND (*loc);
1671       if (kind == REG_EQUAL || kind == REG_EQUIV)
1672         *loc = XEXP (*loc, 1);
1673       else
1674         loc = &XEXP (*loc, 1);
1675     }
1676
1677   /* The new insn will have a destination that was previously the destination
1678      of an insn just above it.  Call distribute_links to make a LOG_LINK from
1679      the next use of that destination.  */
1680   distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
1681 }
1682
1683 /* Return TRUE if combine can reuse reg X in mode MODE.
1684    ADDED_SETS is nonzero if the original set is still required.  */
1685 static bool
1686 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
1687 {
1688   unsigned int regno;
1689
1690   if (!REG_P(x))
1691     return false;
1692
1693   regno = REGNO (x);
1694   /* Allow hard registers if the new mode is legal, and occupies no more
1695      registers than the old mode.  */
1696   if (regno < FIRST_PSEUDO_REGISTER)
1697     return (HARD_REGNO_MODE_OK (regno, mode)
1698             && (hard_regno_nregs[regno][GET_MODE (x)]
1699                 >= hard_regno_nregs[regno][mode]));
1700
1701   /* Or a pseudo that is only used once.  */
1702   return (REG_N_SETS (regno) == 1 && !added_sets
1703           && !REG_USERVAR_P (x));
1704 }
1705
1706 /* Try to combine the insns I1 and I2 into I3.
1707    Here I1 and I2 appear earlier than I3.
1708    I1 can be zero; then we combine just I2 into I3.
1709
1710    If we are combining three insns and the resulting insn is not recognized,
1711    try splitting it into two insns.  If that happens, I2 and I3 are retained
1712    and I1 is pseudo-deleted by turning it into a NOTE.  Otherwise, I1 and I2
1713    are pseudo-deleted.
1714
1715    Return 0 if the combination does not work.  Then nothing is changed.
1716    If we did the combination, return the insn at which combine should
1717    resume scanning.
1718
1719    Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
1720    new direct jump instruction.  */
1721
1722 static rtx
1723 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
1724 {
1725   /* New patterns for I3 and I2, respectively.  */
1726   rtx newpat, newi2pat = 0;
1727   rtvec newpat_vec_with_clobbers = 0;
1728   int substed_i2 = 0, substed_i1 = 0;
1729   /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead.  */
1730   int added_sets_1, added_sets_2;
1731   /* Total number of SETs to put into I3.  */
1732   int total_sets;
1733   /* Nonzero if I2's body now appears in I3.  */
1734   int i2_is_used;
1735   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
1736   int insn_code_number, i2_code_number = 0, other_code_number = 0;
1737   /* Contains I3 if the destination of I3 is used in its source, which means
1738      that the old life of I3 is being killed.  If that usage is placed into
1739      I2 and not in I3, a REG_DEAD note must be made.  */
1740   rtx i3dest_killed = 0;
1741   /* SET_DEST and SET_SRC of I2 and I1.  */
1742   rtx i2dest, i2src, i1dest = 0, i1src = 0;
1743   /* PATTERN (I2), or a copy of it in certain cases.  */
1744   rtx i2pat;
1745   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
1746   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1747   int i2dest_killed = 0, i1dest_killed = 0;
1748   int i1_feeds_i3 = 0;
1749   /* Notes that must be added to REG_NOTES in I3 and I2.  */
1750   rtx new_i3_notes, new_i2_notes;
1751   /* Notes that we substituted I3 into I2 instead of the normal case.  */
1752   int i3_subst_into_i2 = 0;
1753   /* Notes that I1, I2 or I3 is a MULT operation.  */
1754   int have_mult = 0;
1755   int swap_i2i3 = 0;
1756
1757   int maxreg;
1758   rtx temp;
1759   rtx link;
1760   int i;
1761
1762   /* Exit early if one of the insns involved can't be used for
1763      combinations.  */
1764   if (cant_combine_insn_p (i3)
1765       || cant_combine_insn_p (i2)
1766       || (i1 && cant_combine_insn_p (i1))
1767       || likely_spilled_retval_p (i3)
1768       /* We also can't do anything if I3 has a
1769          REG_LIBCALL note since we don't want to disrupt the contiguity of a
1770          libcall.  */
1771 #if 0
1772       /* ??? This gives worse code, and appears to be unnecessary, since no
1773          pass after flow uses REG_LIBCALL/REG_RETVAL notes.  */
1774       || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
1775 #endif
1776       )
1777     return 0;
1778
1779   combine_attempts++;
1780   undobuf.other_insn = 0;
1781
1782   /* Reset the hard register usage information.  */
1783   CLEAR_HARD_REG_SET (newpat_used_regs);
1784
1785   /* If I1 and I2 both feed I3, they can be in any order.  To simplify the
1786      code below, set I1 to be the earlier of the two insns.  */
1787   if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1788     temp = i1, i1 = i2, i2 = temp;
1789
1790   added_links_insn = 0;
1791
1792   /* First check for one important special-case that the code below will
1793      not handle.  Namely, the case where I1 is zero, I2 is a PARALLEL
1794      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
1795      we may be able to replace that destination with the destination of I3.
1796      This occurs in the common code where we compute both a quotient and
1797      remainder into a structure, in which case we want to do the computation
1798      directly into the structure to avoid register-register copies.
1799
1800      Note that this case handles both multiple sets in I2 and also
1801      cases where I2 has a number of CLOBBER or PARALLELs.
1802
1803      We make very conservative checks below and only try to handle the
1804      most common cases of this.  For example, we only handle the case
1805      where I2 and I3 are adjacent to avoid making difficult register
1806      usage tests.  */
1807
1808   if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
1809       && REG_P (SET_SRC (PATTERN (i3)))
1810       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1811       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1812       && GET_CODE (PATTERN (i2)) == PARALLEL
1813       && ! side_effects_p (SET_DEST (PATTERN (i3)))
1814       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1815          below would need to check what is inside (and reg_overlap_mentioned_p
1816          doesn't support those codes anyway).  Don't allow those destinations;
1817          the resulting insn isn't likely to be recognized anyway.  */
1818       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1819       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1820       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1821                                     SET_DEST (PATTERN (i3)))
1822       && next_real_insn (i2) == i3)
1823     {
1824       rtx p2 = PATTERN (i2);
1825
1826       /* Make sure that the destination of I3,
1827          which we are going to substitute into one output of I2,
1828          is not used within another output of I2.  We must avoid making this:
1829          (parallel [(set (mem (reg 69)) ...)
1830                     (set (reg 69) ...)])
1831          which is not well-defined as to order of actions.
1832          (Besides, reload can't handle output reloads for this.)
1833
1834          The problem can also happen if the dest of I3 is a memory ref,
1835          if another dest in I2 is an indirect memory ref.  */
1836       for (i = 0; i < XVECLEN (p2, 0); i++)
1837         if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1838              || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1839             && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1840                                         SET_DEST (XVECEXP (p2, 0, i))))
1841           break;
1842
1843       if (i == XVECLEN (p2, 0))
1844         for (i = 0; i < XVECLEN (p2, 0); i++)
1845           if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1846                || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1847               && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1848             {
1849               combine_merges++;
1850
1851               subst_insn = i3;
1852               subst_low_cuid = INSN_CUID (i2);
1853
1854               added_sets_2 = added_sets_1 = 0;
1855               i2dest = SET_SRC (PATTERN (i3));
1856               i2dest_killed = dead_or_set_p (i2, i2dest);
1857
1858               /* Replace the dest in I2 with our dest and make the resulting
1859                  insn the new pattern for I3.  Then skip to where we
1860                  validate the pattern.  Everything was set up above.  */
1861               SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1862                      SET_DEST (PATTERN (i3)));
1863
1864               newpat = p2;
1865               i3_subst_into_i2 = 1;
1866               goto validate_replacement;
1867             }
1868     }
1869
1870   /* If I2 is setting a double-word pseudo to a constant and I3 is setting
1871      one of those words to another constant, merge them by making a new
1872      constant.  */
1873   if (i1 == 0
1874       && (temp = single_set (i2)) != 0
1875       && (GET_CODE (SET_SRC (temp)) == CONST_INT
1876           || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
1877       && REG_P (SET_DEST (temp))
1878       && GET_MODE_CLASS (GET_MODE (SET_DEST (temp))) == MODE_INT
1879       && GET_MODE_SIZE (GET_MODE (SET_DEST (temp))) == 2 * UNITS_PER_WORD
1880       && GET_CODE (PATTERN (i3)) == SET
1881       && GET_CODE (SET_DEST (PATTERN (i3))) == SUBREG
1882       && SUBREG_REG (SET_DEST (PATTERN (i3))) == SET_DEST (temp)
1883       && GET_MODE_CLASS (GET_MODE (SET_DEST (PATTERN (i3)))) == MODE_INT
1884       && GET_MODE_SIZE (GET_MODE (SET_DEST (PATTERN (i3)))) == UNITS_PER_WORD
1885       && GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT)
1886     {
1887       HOST_WIDE_INT lo, hi;
1888
1889       if (GET_CODE (SET_SRC (temp)) == CONST_INT)
1890         lo = INTVAL (SET_SRC (temp)), hi = lo < 0 ? -1 : 0;
1891       else
1892         {
1893           lo = CONST_DOUBLE_LOW (SET_SRC (temp));
1894           hi = CONST_DOUBLE_HIGH (SET_SRC (temp));
1895         }
1896
1897       if (subreg_lowpart_p (SET_DEST (PATTERN (i3))))
1898         {
1899           /* We don't handle the case of the target word being wider
1900              than a host wide int.  */
1901           gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD);
1902
1903           lo &= ~(UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1);
1904           lo |= (INTVAL (SET_SRC (PATTERN (i3)))
1905                  & (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1906         }
1907       else if (HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
1908         hi = INTVAL (SET_SRC (PATTERN (i3)));
1909       else if (HOST_BITS_PER_WIDE_INT >= 2 * BITS_PER_WORD)
1910         {
1911           int sign = -(int) ((unsigned HOST_WIDE_INT) lo
1912                              >> (HOST_BITS_PER_WIDE_INT - 1));
1913
1914           lo &= ~ (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1915                    (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1916           lo |= (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1917                  (INTVAL (SET_SRC (PATTERN (i3)))));
1918           if (hi == sign)
1919             hi = lo < 0 ? -1 : 0;
1920         }
1921       else
1922         /* We don't handle the case of the higher word not fitting
1923            entirely in either hi or lo.  */
1924         gcc_unreachable ();
1925
1926       combine_merges++;
1927       subst_insn = i3;
1928       subst_low_cuid = INSN_CUID (i2);
1929       added_sets_2 = added_sets_1 = 0;
1930       i2dest = SET_DEST (temp);
1931       i2dest_killed = dead_or_set_p (i2, i2dest);
1932
1933       SUBST (SET_SRC (temp),
1934              immed_double_const (lo, hi, GET_MODE (SET_DEST (temp))));
1935
1936       newpat = PATTERN (i2);
1937       goto validate_replacement;
1938     }
1939
1940 #ifndef HAVE_cc0
1941   /* If we have no I1 and I2 looks like:
1942         (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1943                    (set Y OP)])
1944      make up a dummy I1 that is
1945         (set Y OP)
1946      and change I2 to be
1947         (set (reg:CC X) (compare:CC Y (const_int 0)))
1948
1949      (We can ignore any trailing CLOBBERs.)
1950
1951      This undoes a previous combination and allows us to match a branch-and-
1952      decrement insn.  */
1953
1954   if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1955       && XVECLEN (PATTERN (i2), 0) >= 2
1956       && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1957       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1958           == MODE_CC)
1959       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1960       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1961       && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1962       && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
1963       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1964                       SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1965     {
1966       for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1967         if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1968           break;
1969
1970       if (i == 1)
1971         {
1972           /* We make I1 with the same INSN_UID as I2.  This gives it
1973              the same INSN_CUID for value tracking.  Our fake I1 will
1974              never appear in the insn stream so giving it the same INSN_UID
1975              as I2 will not cause a problem.  */
1976
1977           i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1978                              BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
1979                              XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1980                              NULL_RTX);
1981
1982           SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1983           SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1984                  SET_DEST (PATTERN (i1)));
1985         }
1986     }
1987 #endif
1988
1989   /* Verify that I2 and I1 are valid for combining.  */
1990   if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1991       || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1992     {
1993       undo_all ();
1994       return 0;
1995     }
1996
1997   /* Record whether I2DEST is used in I2SRC and similarly for the other
1998      cases.  Knowing this will help in register status updating below.  */
1999   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2000   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2001   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2002   i2dest_killed = dead_or_set_p (i2, i2dest);
2003   i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2004
2005   /* See if I1 directly feeds into I3.  It does if I1DEST is not used
2006      in I2SRC.  */
2007   i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
2008
2009   /* Ensure that I3's pattern can be the destination of combines.  */
2010   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
2011                           i1 && i2dest_in_i1src && i1_feeds_i3,
2012                           &i3dest_killed))
2013     {
2014       undo_all ();
2015       return 0;
2016     }
2017
2018   /* See if any of the insns is a MULT operation.  Unless one is, we will
2019      reject a combination that is, since it must be slower.  Be conservative
2020      here.  */
2021   if (GET_CODE (i2src) == MULT
2022       || (i1 != 0 && GET_CODE (i1src) == MULT)
2023       || (GET_CODE (PATTERN (i3)) == SET
2024           && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2025     have_mult = 1;
2026
2027   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2028      We used to do this EXCEPT in one case: I3 has a post-inc in an
2029      output operand.  However, that exception can give rise to insns like
2030         mov r3,(r3)+
2031      which is a famous insn on the PDP-11 where the value of r3 used as the
2032      source was model-dependent.  Avoid this sort of thing.  */
2033
2034 #if 0
2035   if (!(GET_CODE (PATTERN (i3)) == SET
2036         && REG_P (SET_SRC (PATTERN (i3)))
2037         && MEM_P (SET_DEST (PATTERN (i3)))
2038         && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2039             || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2040     /* It's not the exception.  */
2041 #endif
2042 #ifdef AUTO_INC_DEC
2043     for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2044       if (REG_NOTE_KIND (link) == REG_INC
2045           && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2046               || (i1 != 0
2047                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2048         {
2049           undo_all ();
2050           return 0;
2051         }
2052 #endif
2053
2054   /* See if the SETs in I1 or I2 need to be kept around in the merged
2055      instruction: whenever the value set there is still needed past I3.
2056      For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
2057
2058      For the SET in I1, we have two cases:  If I1 and I2 independently
2059      feed into I3, the set in I1 needs to be kept around if I1DEST dies
2060      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
2061      in I1 needs to be kept around unless I1DEST dies or is set in either
2062      I2 or I3.  We can distinguish these cases by seeing if I2SRC mentions
2063      I1DEST.  If so, we know I1 feeds into I2.  */
2064
2065   added_sets_2 = ! dead_or_set_p (i3, i2dest);
2066
2067   added_sets_1
2068     = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
2069                : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
2070
2071   /* If the set in I2 needs to be kept around, we must make a copy of
2072      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2073      PATTERN (I2), we are only substituting for the original I1DEST, not into
2074      an already-substituted copy.  This also prevents making self-referential
2075      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2076      I2DEST.  */
2077
2078   i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
2079            ? gen_rtx_SET (VOIDmode, i2dest, i2src)
2080            : PATTERN (i2));
2081
2082   if (added_sets_2)
2083     i2pat = copy_rtx (i2pat);
2084
2085   combine_merges++;
2086
2087   /* Substitute in the latest insn for the regs set by the earlier ones.  */
2088
2089   maxreg = max_reg_num ();
2090
2091   subst_insn = i3;
2092
2093 #ifndef HAVE_cc0
2094   /* Many machines that don't use CC0 have insns that can both perform an
2095      arithmetic operation and set the condition code.  These operations will
2096      be represented as a PARALLEL with the first element of the vector
2097      being a COMPARE of an arithmetic operation with the constant zero.
2098      The second element of the vector will set some pseudo to the result
2099      of the same arithmetic operation.  If we simplify the COMPARE, we won't
2100      match such a pattern and so will generate an extra insn.   Here we test
2101      for this case, where both the comparison and the operation result are
2102      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2103      I2SRC.  Later we will make the PARALLEL that contains I2.  */
2104
2105   if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2106       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2107       && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
2108       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2109     {
2110 #ifdef SELECT_CC_MODE
2111       rtx *cc_use;
2112       enum machine_mode compare_mode;
2113 #endif
2114
2115       newpat = PATTERN (i3);
2116       SUBST (XEXP (SET_SRC (newpat), 0), i2src);
2117
2118       i2_is_used = 1;
2119
2120 #ifdef SELECT_CC_MODE
2121       /* See if a COMPARE with the operand we substituted in should be done
2122          with the mode that is currently being used.  If not, do the same
2123          processing we do in `subst' for a SET; namely, if the destination
2124          is used only once, try to replace it with a register of the proper
2125          mode and also replace the COMPARE.  */
2126       if (undobuf.other_insn == 0
2127           && (cc_use = find_single_use (SET_DEST (newpat), i3,
2128                                         &undobuf.other_insn))
2129           && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
2130                                               i2src, const0_rtx))
2131               != GET_MODE (SET_DEST (newpat))))
2132         {
2133           if (can_change_dest_mode(SET_DEST (newpat), added_sets_2,
2134                                    compare_mode))
2135             {
2136               unsigned int regno = REGNO (SET_DEST (newpat));
2137               rtx new_dest = gen_rtx_REG (compare_mode, regno);
2138
2139               if (regno >= FIRST_PSEUDO_REGISTER)
2140                 SUBST (regno_reg_rtx[regno], new_dest);
2141
2142               SUBST (SET_DEST (newpat), new_dest);
2143               SUBST (XEXP (*cc_use, 0), new_dest);
2144               SUBST (SET_SRC (newpat),
2145                      gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
2146             }
2147           else
2148             undobuf.other_insn = 0;
2149         }
2150 #endif
2151     }
2152   else
2153 #endif
2154     {
2155       /* It is possible that the source of I2 or I1 may be performing
2156          an unneeded operation, such as a ZERO_EXTEND of something
2157          that is known to have the high part zero.  Handle that case
2158          by letting subst look at the innermost one of them.
2159
2160          Another way to do this would be to have a function that tries
2161          to simplify a single insn instead of merging two or more
2162          insns.  We don't do this because of the potential of infinite
2163          loops and because of the potential extra memory required.
2164          However, doing it the way we are is a bit of a kludge and
2165          doesn't catch all cases.
2166
2167          But only do this if -fexpensive-optimizations since it slows
2168          things down and doesn't usually win.
2169
2170          This is not done in the COMPARE case above because the
2171          unmodified I2PAT is used in the PARALLEL and so a pattern
2172          with a modified I2SRC would not match.  */
2173
2174       if (flag_expensive_optimizations)
2175         {
2176           /* Pass pc_rtx so no substitutions are done, just
2177              simplifications.  */
2178           if (i1)
2179             {
2180               subst_low_cuid = INSN_CUID (i1);
2181               i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
2182             }
2183           else
2184             {
2185               subst_low_cuid = INSN_CUID (i2);
2186               i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
2187             }
2188         }
2189
2190       n_occurrences = 0;                /* `subst' counts here */
2191
2192       /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
2193          need to make a unique copy of I2SRC each time we substitute it
2194          to avoid self-referential rtl.  */
2195
2196       subst_low_cuid = INSN_CUID (i2);
2197       newpat = subst (PATTERN (i3), i2dest, i2src, 0,
2198                       ! i1_feeds_i3 && i1dest_in_i1src);
2199       substed_i2 = 1;
2200
2201       /* Record whether i2's body now appears within i3's body.  */
2202       i2_is_used = n_occurrences;
2203     }
2204
2205   /* If we already got a failure, don't try to do more.  Otherwise,
2206      try to substitute in I1 if we have it.  */
2207
2208   if (i1 && GET_CODE (newpat) != CLOBBER)
2209     {
2210       /* Before we can do this substitution, we must redo the test done
2211          above (see detailed comments there) that ensures  that I1DEST
2212          isn't mentioned in any SETs in NEWPAT that are field assignments.  */
2213
2214       if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
2215                               0, (rtx*) 0))
2216         {
2217           undo_all ();
2218           return 0;
2219         }
2220
2221       n_occurrences = 0;
2222       subst_low_cuid = INSN_CUID (i1);
2223       newpat = subst (newpat, i1dest, i1src, 0, 0);
2224       substed_i1 = 1;
2225     }
2226
2227   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
2228      to count all the ways that I2SRC and I1SRC can be used.  */
2229   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
2230        && i2_is_used + added_sets_2 > 1)
2231       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2232           && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
2233               > 1))
2234       /* Fail if we tried to make a new register.  */
2235       || max_reg_num () != maxreg
2236       /* Fail if we couldn't do something and have a CLOBBER.  */
2237       || GET_CODE (newpat) == CLOBBER
2238       /* Fail if this new pattern is a MULT and we didn't have one before
2239          at the outer level.  */
2240       || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
2241           && ! have_mult))
2242     {
2243       undo_all ();
2244       return 0;
2245     }
2246
2247   /* If the actions of the earlier insns must be kept
2248      in addition to substituting them into the latest one,
2249      we must make a new PARALLEL for the latest insn
2250      to hold additional the SETs.  */
2251
2252   if (added_sets_1 || added_sets_2)
2253     {
2254       combine_extras++;
2255
2256       if (GET_CODE (newpat) == PARALLEL)
2257         {
2258           rtvec old = XVEC (newpat, 0);
2259           total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
2260           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2261           memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
2262                   sizeof (old->elem[0]) * old->num_elem);
2263         }
2264       else
2265         {
2266           rtx old = newpat;
2267           total_sets = 1 + added_sets_1 + added_sets_2;
2268           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2269           XVECEXP (newpat, 0, 0) = old;
2270         }
2271
2272       if (added_sets_1)
2273         XVECEXP (newpat, 0, --total_sets)
2274           = (GET_CODE (PATTERN (i1)) == PARALLEL
2275              ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
2276
2277       if (added_sets_2)
2278         {
2279           /* If there is no I1, use I2's body as is.  We used to also not do
2280              the subst call below if I2 was substituted into I3,
2281              but that could lose a simplification.  */
2282           if (i1 == 0)
2283             XVECEXP (newpat, 0, --total_sets) = i2pat;
2284           else
2285             /* See comment where i2pat is assigned.  */
2286             XVECEXP (newpat, 0, --total_sets)
2287               = subst (i2pat, i1dest, i1src, 0, 0);
2288         }
2289     }
2290
2291   /* We come here when we are replacing a destination in I2 with the
2292      destination of I3.  */
2293  validate_replacement:
2294
2295   /* Note which hard regs this insn has as inputs.  */
2296   mark_used_regs_combine (newpat);
2297
2298   /* If recog_for_combine fails, it strips existing clobbers.  If we'll
2299      consider splitting this pattern, we might need these clobbers.  */
2300   if (i1 && GET_CODE (newpat) == PARALLEL
2301       && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
2302     {
2303       int len = XVECLEN (newpat, 0);
2304
2305       newpat_vec_with_clobbers = rtvec_alloc (len);
2306       for (i = 0; i < len; i++)
2307         RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
2308     }
2309
2310   /* Is the result of combination a valid instruction?  */
2311   insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2312
2313   /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2314      the second SET's destination is a register that is unused and isn't
2315      marked as an instruction that might trap in an EH region.  In that case,
2316      we just need the first SET.   This can occur when simplifying a divmod
2317      insn.  We *must* test for this case here because the code below that
2318      splits two independent SETs doesn't handle this case correctly when it
2319      updates the register status.
2320
2321      It's pointless doing this if we originally had two sets, one from
2322      i3, and one from i2.  Combining then splitting the parallel results
2323      in the original i2 again plus an invalid insn (which we delete).
2324      The net effect is only to move instructions around, which makes
2325      debug info less accurate.
2326
2327      Also check the case where the first SET's destination is unused.
2328      That would not cause incorrect code, but does cause an unneeded
2329      insn to remain.  */
2330
2331   if (insn_code_number < 0
2332       && !(added_sets_2 && i1 == 0)
2333       && GET_CODE (newpat) == PARALLEL
2334       && XVECLEN (newpat, 0) == 2
2335       && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2336       && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2337       && asm_noperands (newpat) < 0)
2338     {
2339       rtx set0 = XVECEXP (newpat, 0, 0);
2340       rtx set1 = XVECEXP (newpat, 0, 1);
2341       rtx note;
2342
2343       if (((REG_P (SET_DEST (set1))
2344             && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
2345            || (GET_CODE (SET_DEST (set1)) == SUBREG
2346                && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
2347           && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2348               || INTVAL (XEXP (note, 0)) <= 0)
2349           && ! side_effects_p (SET_SRC (set1)))
2350         {
2351           newpat = set0;
2352           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2353         }
2354
2355       else if (((REG_P (SET_DEST (set0))
2356                  && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
2357                 || (GET_CODE (SET_DEST (set0)) == SUBREG
2358                     && find_reg_note (i3, REG_UNUSED,
2359                                       SUBREG_REG (SET_DEST (set0)))))
2360                && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2361                    || INTVAL (XEXP (note, 0)) <= 0)
2362                && ! side_effects_p (SET_SRC (set0)))
2363         {
2364           newpat = set1;
2365           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2366
2367           if (insn_code_number >= 0)
2368             {
2369               /* If we will be able to accept this, we have made a
2370                  change to the destination of I3.  This requires us to
2371                  do a few adjustments.  */
2372
2373               PATTERN (i3) = newpat;
2374               adjust_for_new_dest (i3);
2375             }
2376         }
2377     }
2378
2379   /* If we were combining three insns and the result is a simple SET
2380      with no ASM_OPERANDS that wasn't recognized, try to split it into two
2381      insns.  There are two ways to do this.  It can be split using a
2382      machine-specific method (like when you have an addition of a large
2383      constant) or by combine in the function find_split_point.  */
2384
2385   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2386       && asm_noperands (newpat) < 0)
2387     {
2388       rtx m_split, *split;
2389       rtx ni2dest = i2dest;
2390
2391       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
2392          use I2DEST as a scratch register will help.  In the latter case,
2393          convert I2DEST to the mode of the source of NEWPAT if we can.  */
2394
2395       m_split = split_insns (newpat, i3);
2396
2397       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2398          inputs of NEWPAT.  */
2399
2400       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2401          possible to try that as a scratch reg.  This would require adding
2402          more code to make it work though.  */
2403
2404       if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
2405         {
2406           enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
2407           /* If I2DEST is a hard register or the only use of a pseudo,
2408              we can change its mode.  */
2409           if (new_mode != GET_MODE (i2dest)
2410               && new_mode != VOIDmode
2411               && can_change_dest_mode (i2dest, added_sets_2, new_mode))
2412             ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
2413                                    REGNO (i2dest));
2414
2415           m_split = split_insns (gen_rtx_PARALLEL
2416                                  (VOIDmode,
2417                                   gen_rtvec (2, newpat,
2418                                              gen_rtx_CLOBBER (VOIDmode,
2419                                                               ni2dest))),
2420                                  i3);
2421           /* If the split with the mode-changed register didn't work, try
2422              the original register.  */
2423           if (! m_split && ni2dest != i2dest)
2424             {
2425               ni2dest = i2dest;
2426               m_split = split_insns (gen_rtx_PARALLEL
2427                                      (VOIDmode,
2428                                       gen_rtvec (2, newpat,
2429                                                  gen_rtx_CLOBBER (VOIDmode,
2430                                                                   i2dest))),
2431                                      i3);
2432             }
2433         }
2434
2435       /* If recog_for_combine has discarded clobbers, try to use them
2436          again for the split.  */
2437       if (m_split == 0 && newpat_vec_with_clobbers)
2438         m_split
2439           = split_insns (gen_rtx_PARALLEL (VOIDmode,
2440                                            newpat_vec_with_clobbers), i3);
2441
2442       if (m_split && NEXT_INSN (m_split) == NULL_RTX)
2443         {
2444           m_split = PATTERN (m_split);
2445           insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
2446           if (insn_code_number >= 0)
2447             newpat = m_split;
2448         }
2449       else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
2450                && (next_real_insn (i2) == i3
2451                    || ! use_crosses_set_p (PATTERN (m_split), INSN_CUID (i2))))
2452         {
2453           rtx i2set, i3set;
2454           rtx newi3pat = PATTERN (NEXT_INSN (m_split));
2455           newi2pat = PATTERN (m_split);
2456
2457           i3set = single_set (NEXT_INSN (m_split));
2458           i2set = single_set (m_split);
2459
2460           /* In case we changed the mode of I2DEST, replace it in the
2461              pseudo-register table here.  We can't do it above in case this
2462              code doesn't get executed and we do a split the other way.  */
2463
2464           if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2465             SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
2466
2467           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2468
2469           /* If I2 or I3 has multiple SETs, we won't know how to track
2470              register status, so don't use these insns.  If I2's destination
2471              is used between I2 and I3, we also can't use these insns.  */
2472
2473           if (i2_code_number >= 0 && i2set && i3set
2474               && (next_real_insn (i2) == i3
2475                   || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
2476             insn_code_number = recog_for_combine (&newi3pat, i3,
2477                                                   &new_i3_notes);
2478           if (insn_code_number >= 0)
2479             newpat = newi3pat;
2480
2481           /* It is possible that both insns now set the destination of I3.
2482              If so, we must show an extra use of it.  */
2483
2484           if (insn_code_number >= 0)
2485             {
2486               rtx new_i3_dest = SET_DEST (i3set);
2487               rtx new_i2_dest = SET_DEST (i2set);
2488
2489               while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
2490                      || GET_CODE (new_i3_dest) == STRICT_LOW_PART
2491                      || GET_CODE (new_i3_dest) == SUBREG)
2492                 new_i3_dest = XEXP (new_i3_dest, 0);
2493
2494               while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
2495                      || GET_CODE (new_i2_dest) == STRICT_LOW_PART
2496                      || GET_CODE (new_i2_dest) == SUBREG)
2497                 new_i2_dest = XEXP (new_i2_dest, 0);
2498
2499               if (REG_P (new_i3_dest)
2500                   && REG_P (new_i2_dest)
2501                   && REGNO (new_i3_dest) == REGNO (new_i2_dest))
2502                 REG_N_SETS (REGNO (new_i2_dest))++;
2503             }
2504         }
2505
2506       /* If we can split it and use I2DEST, go ahead and see if that
2507          helps things be recognized.  Verify that none of the registers
2508          are set between I2 and I3.  */
2509       if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
2510 #ifdef HAVE_cc0
2511           && REG_P (i2dest)
2512 #endif
2513           /* We need I2DEST in the proper mode.  If it is a hard register
2514              or the only use of a pseudo, we can change its mode.
2515              Make sure we don't change a hard register to have a mode that
2516              isn't valid for it, or change the number of registers.  */
2517           && (GET_MODE (*split) == GET_MODE (i2dest)
2518               || GET_MODE (*split) == VOIDmode
2519               || can_change_dest_mode (i2dest, added_sets_2,
2520                                        GET_MODE (*split)))
2521           && (next_real_insn (i2) == i3
2522               || ! use_crosses_set_p (*split, INSN_CUID (i2)))
2523           /* We can't overwrite I2DEST if its value is still used by
2524              NEWPAT.  */
2525           && ! reg_referenced_p (i2dest, newpat))
2526         {
2527           rtx newdest = i2dest;
2528           enum rtx_code split_code = GET_CODE (*split);
2529           enum machine_mode split_mode = GET_MODE (*split);
2530           bool subst_done = false;
2531           newi2pat = NULL_RTX;
2532
2533           /* Get NEWDEST as a register in the proper mode.  We have already
2534              validated that we can do this.  */
2535           if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
2536             {
2537               newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
2538
2539               if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2540                 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2541             }
2542
2543           /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2544              an ASHIFT.  This can occur if it was inside a PLUS and hence
2545              appeared to be a memory address.  This is a kludge.  */
2546           if (split_code == MULT
2547               && GET_CODE (XEXP (*split, 1)) == CONST_INT
2548               && INTVAL (XEXP (*split, 1)) > 0
2549               && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2550             {
2551               SUBST (*split, gen_rtx_ASHIFT (split_mode,
2552                                              XEXP (*split, 0), GEN_INT (i)));
2553               /* Update split_code because we may not have a multiply
2554                  anymore.  */
2555               split_code = GET_CODE (*split);
2556             }
2557
2558 #ifdef INSN_SCHEDULING
2559           /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2560              be written as a ZERO_EXTEND.  */
2561           if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
2562             {
2563 #ifdef LOAD_EXTEND_OP
2564               /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
2565                  what it really is.  */
2566               if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
2567                   == SIGN_EXTEND)
2568                 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
2569                                                     SUBREG_REG (*split)));
2570               else
2571 #endif
2572                 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
2573                                                     SUBREG_REG (*split)));
2574             }
2575 #endif
2576
2577           /* Attempt to split binary operators using arithmetic identities.  */
2578           if (BINARY_P (SET_SRC (newpat))
2579               && split_mode == GET_MODE (SET_SRC (newpat))
2580               && ! side_effects_p (SET_SRC (newpat)))
2581             {
2582               rtx setsrc = SET_SRC (newpat);
2583               enum machine_mode mode = GET_MODE (setsrc);
2584               enum rtx_code code = GET_CODE (setsrc);
2585               rtx src_op0 = XEXP (setsrc, 0);
2586               rtx src_op1 = XEXP (setsrc, 1);
2587
2588               /* Split "X = Y op Y" as "Z = Y; X = Z op Z".  */
2589               if (rtx_equal_p (src_op0, src_op1))
2590                 {
2591                   newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
2592                   SUBST (XEXP (setsrc, 0), newdest);
2593                   SUBST (XEXP (setsrc, 1), newdest);
2594                   subst_done = true;
2595                 }
2596               /* Split "((P op Q) op R) op S" where op is PLUS or MULT.  */
2597               else if ((code == PLUS || code == MULT)
2598                        && GET_CODE (src_op0) == code
2599                        && GET_CODE (XEXP (src_op0, 0)) == code
2600                        && (INTEGRAL_MODE_P (mode)
2601                            || (FLOAT_MODE_P (mode)
2602                                && flag_unsafe_math_optimizations)))
2603                 {
2604                   rtx p = XEXP (XEXP (src_op0, 0), 0);
2605                   rtx q = XEXP (XEXP (src_op0, 0), 1);
2606                   rtx r = XEXP (src_op0, 1);
2607                   rtx s = src_op1;
2608
2609                   /* Split both "((X op Y) op X) op Y" and
2610                      "((X op Y) op Y) op X" as "T op T" where T is
2611                      "X op Y".  */
2612                   if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
2613                        || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
2614                     {
2615                       newi2pat = gen_rtx_SET (VOIDmode, newdest,
2616                                               XEXP (src_op0, 0));
2617                       SUBST (XEXP (setsrc, 0), newdest);
2618                       SUBST (XEXP (setsrc, 1), newdest);
2619                       subst_done = true;
2620                     }
2621                   /* Split "((X op X) op Y) op Y)" as "T op T" where
2622                      T is "X op Y".  */
2623                   else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
2624                     {
2625                       rtx tmp = simplify_gen_binary (code, mode, p, r);
2626                       newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
2627                       SUBST (XEXP (setsrc, 0), newdest);
2628                       SUBST (XEXP (setsrc, 1), newdest);
2629                       subst_done = true;
2630                     }
2631                 }
2632             }
2633
2634           if (!subst_done)
2635             {
2636               newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
2637               SUBST (*split, newdest);
2638             }
2639
2640           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2641
2642           /* recog_for_combine might have added CLOBBERs to newi2pat.
2643              Make sure NEWPAT does not depend on the clobbered regs.  */
2644           if (GET_CODE (newi2pat) == PARALLEL)
2645             for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
2646               if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
2647                 {
2648                   rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
2649                   if (reg_overlap_mentioned_p (reg, newpat))
2650                     {
2651                       undo_all ();
2652                       return 0;
2653                     }
2654                 }
2655
2656           /* If the split point was a MULT and we didn't have one before,
2657              don't use one now.  */
2658           if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2659             insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2660         }
2661     }
2662
2663   /* Check for a case where we loaded from memory in a narrow mode and
2664      then sign extended it, but we need both registers.  In that case,
2665      we have a PARALLEL with both loads from the same memory location.
2666      We can split this into a load from memory followed by a register-register
2667      copy.  This saves at least one insn, more if register allocation can
2668      eliminate the copy.
2669
2670      We cannot do this if the destination of the first assignment is a
2671      condition code register or cc0.  We eliminate this case by making sure
2672      the SET_DEST and SET_SRC have the same mode.
2673
2674      We cannot do this if the destination of the second assignment is
2675      a register that we have already assumed is zero-extended.  Similarly
2676      for a SUBREG of such a register.  */
2677
2678   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2679            && GET_CODE (newpat) == PARALLEL
2680            && XVECLEN (newpat, 0) == 2
2681            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2682            && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2683            && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
2684                == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
2685            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2686            && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2687                            XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2688            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2689                                    INSN_CUID (i2))
2690            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2691            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2692            && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2693                  (REG_P (temp)
2694                   && reg_stat[REGNO (temp)].nonzero_bits != 0
2695                   && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2696                   && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2697                   && (reg_stat[REGNO (temp)].nonzero_bits
2698                       != GET_MODE_MASK (word_mode))))
2699            && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2700                  && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2701                      (REG_P (temp)
2702                       && reg_stat[REGNO (temp)].nonzero_bits != 0
2703                       && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2704                       && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2705                       && (reg_stat[REGNO (temp)].nonzero_bits
2706                           != GET_MODE_MASK (word_mode)))))
2707            && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2708                                          SET_SRC (XVECEXP (newpat, 0, 1)))
2709            && ! find_reg_note (i3, REG_UNUSED,
2710                                SET_DEST (XVECEXP (newpat, 0, 0))))
2711     {
2712       rtx ni2dest;
2713
2714       newi2pat = XVECEXP (newpat, 0, 0);
2715       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2716       newpat = XVECEXP (newpat, 0, 1);
2717       SUBST (SET_SRC (newpat),
2718              gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
2719       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2720
2721       if (i2_code_number >= 0)
2722         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2723
2724       if (insn_code_number >= 0)
2725         swap_i2i3 = 1;
2726     }
2727
2728   /* Similarly, check for a case where we have a PARALLEL of two independent
2729      SETs but we started with three insns.  In this case, we can do the sets
2730      as two separate insns.  This case occurs when some SET allows two
2731      other insns to combine, but the destination of that SET is still live.  */
2732
2733   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2734            && GET_CODE (newpat) == PARALLEL
2735            && XVECLEN (newpat, 0) == 2
2736            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2737            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2738            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2739            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2740            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2741            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2742            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2743                                    INSN_CUID (i2))
2744            /* Don't pass sets with (USE (MEM ...)) dests to the following.  */
2745            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2746            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2747            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2748                                   XVECEXP (newpat, 0, 0))
2749            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2750                                   XVECEXP (newpat, 0, 1))
2751            && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
2752                  && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
2753     {
2754       /* Normally, it doesn't matter which of the two is done first,
2755          but it does if one references cc0.  In that case, it has to
2756          be first.  */
2757 #ifdef HAVE_cc0
2758       if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2759         {
2760           newi2pat = XVECEXP (newpat, 0, 0);
2761           newpat = XVECEXP (newpat, 0, 1);
2762         }
2763       else
2764 #endif
2765         {
2766           newi2pat = XVECEXP (newpat, 0, 1);
2767           newpat = XVECEXP (newpat, 0, 0);
2768         }
2769
2770       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2771
2772       if (i2_code_number >= 0)
2773         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2774     }
2775
2776   /* If it still isn't recognized, fail and change things back the way they
2777      were.  */
2778   if ((insn_code_number < 0
2779        /* Is the result a reasonable ASM_OPERANDS?  */
2780        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2781     {
2782       undo_all ();
2783       return 0;
2784     }
2785
2786   /* If we had to change another insn, make sure it is valid also.  */
2787   if (undobuf.other_insn)
2788     {
2789       rtx other_pat = PATTERN (undobuf.other_insn);
2790       rtx new_other_notes;
2791       rtx note, next;
2792
2793       CLEAR_HARD_REG_SET (newpat_used_regs);
2794
2795       other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
2796                                              &new_other_notes);
2797
2798       if (other_code_number < 0 && ! check_asm_operands (other_pat))
2799         {
2800           undo_all ();
2801           return 0;
2802         }
2803
2804       PATTERN (undobuf.other_insn) = other_pat;
2805
2806       /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2807          are still valid.  Then add any non-duplicate notes added by
2808          recog_for_combine.  */
2809       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2810         {
2811           next = XEXP (note, 1);
2812
2813           if (REG_NOTE_KIND (note) == REG_UNUSED
2814               && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2815             {
2816               if (REG_P (XEXP (note, 0)))
2817                 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2818
2819               remove_note (undobuf.other_insn, note);
2820             }
2821         }
2822
2823       for (note = new_other_notes; note; note = XEXP (note, 1))
2824         if (REG_P (XEXP (note, 0)))
2825           REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2826
2827       distribute_notes (new_other_notes, undobuf.other_insn,
2828                         undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
2829     }
2830 #ifdef HAVE_cc0
2831   /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
2832      they are adjacent to each other or not.  */
2833   {
2834     rtx p = prev_nonnote_insn (i3);
2835     if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
2836         && sets_cc0_p (newi2pat))
2837       {
2838         undo_all ();
2839         return 0;
2840       }
2841   }
2842 #endif
2843
2844   /* Only allow this combination if insn_rtx_costs reports that the
2845      replacement instructions are cheaper than the originals.  */
2846   if (!combine_validate_cost (i1, i2, i3, newpat, newi2pat))
2847     {
2848       undo_all ();
2849       return 0;
2850     }
2851
2852   /* We now know that we can do this combination.  Merge the insns and
2853      update the status of registers and LOG_LINKS.  */
2854
2855   if (swap_i2i3)
2856     {
2857       rtx insn;
2858       rtx link;
2859       rtx ni2dest;
2860
2861       /* I3 now uses what used to be its destination and which is now
2862          I2's destination.  This requires us to do a few adjustments.  */
2863       PATTERN (i3) = newpat;
2864       adjust_for_new_dest (i3);
2865
2866       /* We need a LOG_LINK from I3 to I2.  But we used to have one,
2867          so we still will.
2868
2869          However, some later insn might be using I2's dest and have
2870          a LOG_LINK pointing at I3.  We must remove this link.
2871          The simplest way to remove the link is to point it at I1,
2872          which we know will be a NOTE.  */
2873
2874       /* newi2pat is usually a SET here; however, recog_for_combine might
2875          have added some clobbers.  */
2876       if (GET_CODE (newi2pat) == PARALLEL)
2877         ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
2878       else
2879         ni2dest = SET_DEST (newi2pat);
2880
2881       for (insn = NEXT_INSN (i3);
2882            insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2883                     || insn != BB_HEAD (this_basic_block->next_bb));
2884            insn = NEXT_INSN (insn))
2885         {
2886           if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
2887             {
2888               for (link = LOG_LINKS (insn); link;
2889                    link = XEXP (link, 1))
2890                 if (XEXP (link, 0) == i3)
2891                   XEXP (link, 0) = i1;
2892
2893               break;
2894             }
2895         }
2896     }
2897
2898   {
2899     rtx i3notes, i2notes, i1notes = 0;
2900     rtx i3links, i2links, i1links = 0;
2901     rtx midnotes = 0;
2902     unsigned int regno;
2903     /* Compute which registers we expect to eliminate.  newi2pat may be setting
2904        either i3dest or i2dest, so we must check it.  Also, i1dest may be the
2905        same as i3dest, in which case newi2pat may be setting i1dest.  */
2906     rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
2907                    || i2dest_in_i2src || i2dest_in_i1src
2908                    || !i2dest_killed
2909                    ? 0 : i2dest);
2910     rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
2911                    || (newi2pat && reg_set_p (i1dest, newi2pat))
2912                    || !i1dest_killed
2913                    ? 0 : i1dest);
2914
2915     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2916        clear them.  */
2917     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2918     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2919     if (i1)
2920       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2921
2922     /* Ensure that we do not have something that should not be shared but
2923        occurs multiple times in the new insns.  Check this by first
2924        resetting all the `used' flags and then copying anything is shared.  */
2925
2926     reset_used_flags (i3notes);
2927     reset_used_flags (i2notes);
2928     reset_used_flags (i1notes);
2929     reset_used_flags (newpat);
2930     reset_used_flags (newi2pat);
2931     if (undobuf.other_insn)
2932       reset_used_flags (PATTERN (undobuf.other_insn));
2933
2934     i3notes = copy_rtx_if_shared (i3notes);
2935     i2notes = copy_rtx_if_shared (i2notes);
2936     i1notes = copy_rtx_if_shared (i1notes);
2937     newpat = copy_rtx_if_shared (newpat);
2938     newi2pat = copy_rtx_if_shared (newi2pat);
2939     if (undobuf.other_insn)
2940       reset_used_flags (PATTERN (undobuf.other_insn));
2941
2942     INSN_CODE (i3) = insn_code_number;
2943     PATTERN (i3) = newpat;
2944
2945     if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
2946       {
2947         rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
2948
2949         reset_used_flags (call_usage);
2950         call_usage = copy_rtx (call_usage);
2951
2952         if (substed_i2)
2953           replace_rtx (call_usage, i2dest, i2src);
2954
2955         if (substed_i1)
2956           replace_rtx (call_usage, i1dest, i1src);
2957
2958         CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
2959       }
2960
2961     if (undobuf.other_insn)
2962       INSN_CODE (undobuf.other_insn) = other_code_number;
2963
2964     /* We had one special case above where I2 had more than one set and
2965        we replaced a destination of one of those sets with the destination
2966        of I3.  In that case, we have to update LOG_LINKS of insns later
2967        in this basic block.  Note that this (expensive) case is rare.
2968
2969        Also, in this case, we must pretend that all REG_NOTEs for I2
2970        actually came from I3, so that REG_UNUSED notes from I2 will be
2971        properly handled.  */
2972
2973     if (i3_subst_into_i2)
2974       {
2975         for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2976           if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != USE
2977               && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
2978               && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2979               && ! find_reg_note (i2, REG_UNUSED,
2980                                   SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2981             for (temp = NEXT_INSN (i2);
2982                  temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2983                           || BB_HEAD (this_basic_block) != temp);
2984                  temp = NEXT_INSN (temp))
2985               if (temp != i3 && INSN_P (temp))
2986                 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2987                   if (XEXP (link, 0) == i2)
2988                     XEXP (link, 0) = i3;
2989
2990         if (i3notes)
2991           {
2992             rtx link = i3notes;
2993             while (XEXP (link, 1))
2994               link = XEXP (link, 1);
2995             XEXP (link, 1) = i2notes;
2996           }
2997         else
2998           i3notes = i2notes;
2999         i2notes = 0;
3000       }
3001
3002     LOG_LINKS (i3) = 0;
3003     REG_NOTES (i3) = 0;
3004     LOG_LINKS (i2) = 0;
3005     REG_NOTES (i2) = 0;
3006
3007     if (newi2pat)
3008       {
3009         INSN_CODE (i2) = i2_code_number;
3010         PATTERN (i2) = newi2pat;
3011       }
3012     else
3013       SET_INSN_DELETED (i2);
3014
3015     if (i1)
3016       {
3017         LOG_LINKS (i1) = 0;
3018         REG_NOTES (i1) = 0;
3019         SET_INSN_DELETED (i1);
3020       }
3021
3022     /* Get death notes for everything that is now used in either I3 or
3023        I2 and used to die in a previous insn.  If we built two new
3024        patterns, move from I1 to I2 then I2 to I3 so that we get the
3025        proper movement on registers that I2 modifies.  */
3026
3027     if (newi2pat)
3028       {
3029         move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
3030         move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
3031       }
3032     else
3033       move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
3034                    i3, &midnotes);
3035
3036     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
3037     if (i3notes)
3038       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
3039                         elim_i2, elim_i1);
3040     if (i2notes)
3041       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
3042                         elim_i2, elim_i1);
3043     if (i1notes)
3044       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
3045                         elim_i2, elim_i1);
3046     if (midnotes)
3047       distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3048                         elim_i2, elim_i1);
3049
3050     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
3051        know these are REG_UNUSED and want them to go to the desired insn,
3052        so we always pass it as i3.  We have not counted the notes in
3053        reg_n_deaths yet, so we need to do so now.  */
3054
3055     if (newi2pat && new_i2_notes)
3056       {
3057         for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
3058           if (REG_P (XEXP (temp, 0)))
3059             REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
3060
3061         distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3062       }
3063
3064     if (new_i3_notes)
3065       {
3066         for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
3067           if (REG_P (XEXP (temp, 0)))
3068             REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
3069
3070         distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
3071       }
3072
3073     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
3074        put a REG_DEAD note for it somewhere.  If NEWI2PAT exists and sets
3075        I3DEST, the death must be somewhere before I2, not I3.  If we passed I3
3076        in that case, it might delete I2.  Similarly for I2 and I1.
3077        Show an additional death due to the REG_DEAD note we make here.  If
3078        we discard it in distribute_notes, we will decrement it again.  */
3079
3080     if (i3dest_killed)
3081       {
3082         if (REG_P (i3dest_killed))
3083           REG_N_DEATHS (REGNO (i3dest_killed))++;
3084
3085         if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
3086           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
3087                                                NULL_RTX),
3088                             NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
3089         else
3090           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
3091                                                NULL_RTX),
3092                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3093                             elim_i2, elim_i1);
3094       }
3095
3096     if (i2dest_in_i2src)
3097       {
3098         if (REG_P (i2dest))
3099           REG_N_DEATHS (REGNO (i2dest))++;
3100
3101         if (newi2pat && reg_set_p (i2dest, newi2pat))
3102           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
3103                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3104         else
3105           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
3106                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3107                             NULL_RTX, NULL_RTX);
3108       }
3109
3110     if (i1dest_in_i1src)
3111       {
3112         if (REG_P (i1dest))
3113           REG_N_DEATHS (REGNO (i1dest))++;
3114
3115         if (newi2pat && reg_set_p (i1dest, newi2pat))
3116           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
3117                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3118         else
3119           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
3120                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3121                             NULL_RTX, NULL_RTX);
3122       }
3123
3124     distribute_links (i3links);
3125     distribute_links (i2links);
3126     distribute_links (i1links);
3127
3128     if (REG_P (i2dest))
3129       {
3130         rtx link;
3131         rtx i2_insn = 0, i2_val = 0, set;
3132
3133         /* The insn that used to set this register doesn't exist, and
3134            this life of the register may not exist either.  See if one of
3135            I3's links points to an insn that sets I2DEST.  If it does,
3136            that is now the last known value for I2DEST. If we don't update
3137            this and I2 set the register to a value that depended on its old
3138            contents, we will get confused.  If this insn is used, thing
3139            will be set correctly in combine_instructions.  */
3140
3141         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3142           if ((set = single_set (XEXP (link, 0))) != 0
3143               && rtx_equal_p (i2dest, SET_DEST (set)))
3144             i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
3145
3146         record_value_for_reg (i2dest, i2_insn, i2_val);
3147
3148         /* If the reg formerly set in I2 died only once and that was in I3,
3149            zero its use count so it won't make `reload' do any work.  */
3150         if (! added_sets_2
3151             && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
3152             && ! i2dest_in_i2src)
3153           {
3154             regno = REGNO (i2dest);
3155             REG_N_SETS (regno)--;
3156           }
3157       }
3158
3159     if (i1 && REG_P (i1dest))
3160       {
3161         rtx link;
3162         rtx i1_insn = 0, i1_val = 0, set;
3163
3164         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3165           if ((set = single_set (XEXP (link, 0))) != 0
3166               && rtx_equal_p (i1dest, SET_DEST (set)))
3167             i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
3168
3169         record_value_for_reg (i1dest, i1_insn, i1_val);
3170
3171         regno = REGNO (i1dest);
3172         if (! added_sets_1 && ! i1dest_in_i1src)
3173           REG_N_SETS (regno)--;
3174       }
3175
3176     /* Update reg_stat[].nonzero_bits et al for any changes that may have
3177        been made to this insn.  The order of
3178        set_nonzero_bits_and_sign_copies() is important.  Because newi2pat
3179        can affect nonzero_bits of newpat */
3180     if (newi2pat)
3181       note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
3182     note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
3183
3184     /* Set new_direct_jump_p if a new return or simple jump instruction
3185        has been created.
3186
3187        If I3 is now an unconditional jump, ensure that it has a
3188        BARRIER following it since it may have initially been a
3189        conditional jump.  It may also be the last nonnote insn.  */
3190
3191     if (returnjump_p (i3) || any_uncondjump_p (i3))
3192       {
3193         *new_direct_jump_p = 1;
3194         mark_jump_label (PATTERN (i3), i3, 0);
3195
3196         if ((temp = next_nonnote_insn (i3)) == NULL_RTX
3197             || !BARRIER_P (temp))
3198           emit_barrier_after (i3);
3199       }
3200
3201     if (undobuf.other_insn != NULL_RTX
3202         && (returnjump_p (undobuf.other_insn)
3203             || any_uncondjump_p (undobuf.other_insn)))
3204       {
3205         *new_direct_jump_p = 1;
3206
3207         if ((temp = next_nonnote_insn (undobuf.other_insn)) == NULL_RTX
3208             || !BARRIER_P (temp))
3209           emit_barrier_after (undobuf.other_insn);
3210       }
3211
3212     /* An NOOP jump does not need barrier, but it does need cleaning up
3213        of CFG.  */
3214     if (GET_CODE (newpat) == SET
3215         && SET_SRC (newpat) == pc_rtx
3216         && SET_DEST (newpat) == pc_rtx)
3217       *new_direct_jump_p = 1;
3218   }
3219
3220   combine_successes++;
3221   undo_commit ();
3222
3223   if (added_links_insn
3224       && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
3225       && INSN_CUID (added_links_insn) < INSN_CUID (i3))
3226     return added_links_insn;
3227   else
3228     return newi2pat ? i2 : i3;
3229 }
3230 \f
3231 /* Undo all the modifications recorded in undobuf.  */
3232
3233 static void
3234 undo_all (void)
3235 {
3236   struct undo *undo, *next;
3237
3238   for (undo = undobuf.undos; undo; undo = next)
3239     {
3240       next = undo->next;
3241       if (undo->is_int)
3242         *undo->where.i = undo->old_contents.i;
3243       else
3244         *undo->where.r = undo->old_contents.r;
3245
3246       undo->next = undobuf.frees;
3247       undobuf.frees = undo;
3248     }
3249
3250   undobuf.undos = 0;
3251 }
3252
3253 /* We've committed to accepting the changes we made.  Move all
3254    of the undos to the free list.  */
3255
3256 static void
3257 undo_commit (void)
3258 {
3259   struct undo *undo, *next;
3260
3261   for (undo = undobuf.undos; undo; undo = next)
3262     {
3263       next = undo->next;
3264       undo->next = undobuf.frees;
3265       undobuf.frees = undo;
3266     }
3267   undobuf.undos = 0;
3268 }
3269
3270 \f
3271 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
3272    where we have an arithmetic expression and return that point.  LOC will
3273    be inside INSN.
3274
3275    try_combine will call this function to see if an insn can be split into
3276    two insns.  */
3277
3278 static rtx *
3279 find_split_point (rtx *loc, rtx insn)
3280 {
3281   rtx x = *loc;
3282   enum rtx_code code = GET_CODE (x);
3283   rtx *split;
3284   unsigned HOST_WIDE_INT len = 0;
3285   HOST_WIDE_INT pos = 0;
3286   int unsignedp = 0;
3287   rtx inner = NULL_RTX;
3288
3289   /* First special-case some codes.  */
3290   switch (code)
3291     {
3292     case SUBREG:
3293 #ifdef INSN_SCHEDULING
3294       /* If we are making a paradoxical SUBREG invalid, it becomes a split
3295          point.  */
3296       if (MEM_P (SUBREG_REG (x)))
3297         return loc;
3298 #endif
3299       return find_split_point (&SUBREG_REG (x), insn);
3300
3301     case MEM:
3302 #ifdef HAVE_lo_sum
3303       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
3304          using LO_SUM and HIGH.  */
3305       if (GET_CODE (XEXP (x, 0)) == CONST
3306           || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
3307         {
3308           SUBST (XEXP (x, 0),
3309                  gen_rtx_LO_SUM (Pmode,
3310                                  gen_rtx_HIGH (Pmode, XEXP (x, 0)),
3311                                  XEXP (x, 0)));
3312           return &XEXP (XEXP (x, 0), 0);
3313         }
3314 #endif
3315
3316       /* If we have a PLUS whose second operand is a constant and the
3317          address is not valid, perhaps will can split it up using
3318          the machine-specific way to split large constants.  We use
3319          the first pseudo-reg (one of the virtual regs) as a placeholder;
3320          it will not remain in the result.  */
3321       if (GET_CODE (XEXP (x, 0)) == PLUS
3322           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3323           && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
3324         {
3325           rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
3326           rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
3327                                  subst_insn);
3328
3329           /* This should have produced two insns, each of which sets our
3330              placeholder.  If the source of the second is a valid address,
3331              we can make put both sources together and make a split point
3332              in the middle.  */
3333
3334           if (seq
3335               && NEXT_INSN (seq) != NULL_RTX
3336               && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
3337               && NONJUMP_INSN_P (seq)
3338               && GET_CODE (PATTERN (seq)) == SET
3339               && SET_DEST (PATTERN (seq)) == reg
3340               && ! reg_mentioned_p (reg,
3341                                     SET_SRC (PATTERN (seq)))
3342               && NONJUMP_INSN_P (NEXT_INSN (seq))
3343               && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
3344               && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
3345               && memory_address_p (GET_MODE (x),
3346                                    SET_SRC (PATTERN (NEXT_INSN (seq)))))
3347             {
3348               rtx src1 = SET_SRC (PATTERN (seq));
3349               rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
3350
3351               /* Replace the placeholder in SRC2 with SRC1.  If we can
3352                  find where in SRC2 it was placed, that can become our
3353                  split point and we can replace this address with SRC2.
3354                  Just try two obvious places.  */
3355
3356               src2 = replace_rtx (src2, reg, src1);
3357               split = 0;
3358               if (XEXP (src2, 0) == src1)
3359                 split = &XEXP (src2, 0);
3360               else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
3361                        && XEXP (XEXP (src2, 0), 0) == src1)
3362                 split = &XEXP (XEXP (src2, 0), 0);
3363
3364               if (split)
3365                 {
3366                   SUBST (XEXP (x, 0), src2);
3367                   return split;
3368                 }
3369             }
3370
3371           /* If that didn't work, perhaps the first operand is complex and
3372              needs to be computed separately, so make a split point there.
3373              This will occur on machines that just support REG + CONST
3374              and have a constant moved through some previous computation.  */
3375
3376           else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
3377                    && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
3378                          && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
3379             return &XEXP (XEXP (x, 0), 0);
3380         }
3381       break;
3382
3383     case SET:
3384 #ifdef HAVE_cc0
3385       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
3386          ZERO_EXTRACT, the most likely reason why this doesn't match is that
3387          we need to put the operand into a register.  So split at that
3388          point.  */
3389
3390       if (SET_DEST (x) == cc0_rtx
3391           && GET_CODE (SET_SRC (x)) != COMPARE
3392           && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
3393           && !OBJECT_P (SET_SRC (x))
3394           && ! (GET_CODE (SET_SRC (x)) == SUBREG
3395                 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
3396         return &SET_SRC (x);
3397 #endif
3398
3399       /* See if we can split SET_SRC as it stands.  */
3400       split = find_split_point (&SET_SRC (x), insn);
3401       if (split && split != &SET_SRC (x))
3402         return split;
3403
3404       /* See if we can split SET_DEST as it stands.  */
3405       split = find_split_point (&SET_DEST (x), insn);
3406       if (split && split != &SET_DEST (x))
3407         return split;
3408
3409       /* See if this is a bitfield assignment with everything constant.  If
3410          so, this is an IOR of an AND, so split it into that.  */
3411       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
3412           && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
3413               <= HOST_BITS_PER_WIDE_INT)
3414           && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
3415           && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
3416           && GET_CODE (SET_SRC (x)) == CONST_INT
3417           && ((INTVAL (XEXP (SET_DEST (x), 1))
3418                + INTVAL (XEXP (SET_DEST (x), 2)))
3419               <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
3420           && ! side_effects_p (XEXP (SET_DEST (x), 0)))
3421         {
3422           HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
3423           unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
3424           unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
3425           rtx dest = XEXP (SET_DEST (x), 0);
3426           enum machine_mode mode = GET_MODE (dest);
3427           unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
3428           rtx or_mask;
3429
3430           if (BITS_BIG_ENDIAN)
3431             pos = GET_MODE_BITSIZE (mode) - len - pos;
3432
3433           or_mask = gen_int_mode (src << pos, mode);
3434           if (src == mask)
3435             SUBST (SET_SRC (x),
3436                    simplify_gen_binary (IOR, mode, dest, or_mask));
3437           else
3438             {
3439               rtx negmask = gen_int_mode (~(mask << pos), mode);
3440               SUBST (SET_SRC (x),
3441                      simplify_gen_binary (IOR, mode,
3442                                           simplify_gen_binary (AND, mode,
3443                                                                dest, negmask),
3444                                           or_mask));
3445             }
3446
3447           SUBST (SET_DEST (x), dest);
3448
3449           split = find_split_point (&SET_SRC (x), insn);
3450           if (split && split != &SET_SRC (x))
3451             return split;
3452         }
3453
3454       /* Otherwise, see if this is an operation that we can split into two.
3455          If so, try to split that.  */
3456       code = GET_CODE (SET_SRC (x));
3457
3458       switch (code)
3459         {
3460         case AND:
3461           /* If we are AND'ing with a large constant that is only a single
3462              bit and the result is only being used in a context where we
3463              need to know if it is zero or nonzero, replace it with a bit
3464              extraction.  This will avoid the large constant, which might
3465              have taken more than one insn to make.  If the constant were
3466              not a valid argument to the AND but took only one insn to make,
3467              this is no worse, but if it took more than one insn, it will
3468              be better.  */
3469
3470           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3471               && REG_P (XEXP (SET_SRC (x), 0))
3472               && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
3473               && REG_P (SET_DEST (x))
3474               && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
3475               && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
3476               && XEXP (*split, 0) == SET_DEST (x)
3477               && XEXP (*split, 1) == const0_rtx)
3478             {
3479               rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
3480                                                 XEXP (SET_SRC (x), 0),
3481                                                 pos, NULL_RTX, 1, 1, 0, 0);
3482               if (extraction != 0)
3483                 {
3484                   SUBST (SET_SRC (x), extraction);
3485                   return find_split_point (loc, insn);
3486                 }
3487             }
3488           break;
3489
3490         case NE:
3491           /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
3492              is known to be on, this can be converted into a NEG of a shift.  */
3493           if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
3494               && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
3495               && 1 <= (pos = exact_log2
3496                        (nonzero_bits (XEXP (SET_SRC (x), 0),
3497                                       GET_MODE (XEXP (SET_SRC (x), 0))))))
3498             {
3499               enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
3500
3501               SUBST (SET_SRC (x),
3502                      gen_rtx_NEG (mode,
3503                                   gen_rtx_LSHIFTRT (mode,
3504                                                     XEXP (SET_SRC (x), 0),
3505                                                     GEN_INT (pos))));
3506
3507               split = find_split_point (&SET_SRC (x), insn);
3508               if (split && split != &SET_SRC (x))
3509                 return split;
3510             }
3511           break;
3512
3513         case SIGN_EXTEND:
3514           inner = XEXP (SET_SRC (x), 0);
3515
3516           /* We can't optimize if either mode is a partial integer
3517              mode as we don't know how many bits are significant
3518              in those modes.  */
3519           if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
3520               || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
3521             break;
3522
3523           pos = 0;
3524           len = GET_MODE_BITSIZE (GET_MODE (inner));
3525           unsignedp = 0;
3526           break;
3527
3528         case SIGN_EXTRACT:
3529         case ZERO_EXTRACT:
3530           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3531               && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
3532             {
3533               inner = XEXP (SET_SRC (x), 0);
3534               len = INTVAL (XEXP (SET_SRC (x), 1));
3535               pos = INTVAL (XEXP (SET_SRC (x), 2));
3536
3537               if (BITS_BIG_ENDIAN)
3538                 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
3539               unsignedp = (code == ZERO_EXTRACT);
3540             }
3541           break;
3542
3543         default:
3544           break;
3545         }
3546
3547       if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
3548         {
3549           enum machine_mode mode = GET_MODE (SET_SRC (x));
3550
3551           /* For unsigned, we have a choice of a shift followed by an
3552              AND or two shifts.  Use two shifts for field sizes where the
3553              constant might be too large.  We assume here that we can
3554              always at least get 8-bit constants in an AND insn, which is
3555              true for every current RISC.  */
3556
3557           if (unsignedp && len <= 8)
3558             {
3559               SUBST (SET_SRC (x),
3560                      gen_rtx_AND (mode,
3561                                   gen_rtx_LSHIFTRT
3562                                   (mode, gen_lowpart (mode, inner),
3563                                    GEN_INT (pos)),
3564                                   GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
3565
3566               split = find_split_point (&SET_SRC (x), insn);
3567               if (split && split != &SET_SRC (x))
3568                 return split;
3569             }
3570           else
3571             {
3572               SUBST (SET_SRC (x),
3573                      gen_rtx_fmt_ee
3574                      (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
3575                       gen_rtx_ASHIFT (mode,
3576                                       gen_lowpart (mode, inner),
3577                                       GEN_INT (GET_MODE_BITSIZE (mode)
3578                                                - len - pos)),
3579                       GEN_INT (GET_MODE_BITSIZE (mode) - len)));
3580
3581               split = find_split_point (&SET_SRC (x), insn);
3582               if (split && split != &SET_SRC (x))
3583                 return split;
3584             }
3585         }
3586
3587       /* See if this is a simple operation with a constant as the second
3588          operand.  It might be that this constant is out of range and hence
3589          could be used as a split point.  */
3590       if (BINARY_P (SET_SRC (x))
3591           && CONSTANT_P (XEXP (SET_SRC (x), 1))
3592           && (OBJECT_P (XEXP (SET_SRC (x), 0))
3593               || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
3594                   && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
3595         return &XEXP (SET_SRC (x), 1);
3596
3597       /* Finally, see if this is a simple operation with its first operand
3598          not in a register.  The operation might require this operand in a
3599          register, so return it as a split point.  We can always do this
3600          because if the first operand were another operation, we would have
3601          already found it as a split point.  */
3602       if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
3603           && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
3604         return &XEXP (SET_SRC (x), 0);
3605
3606       return 0;
3607
3608     case AND:
3609     case IOR:
3610       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
3611          it is better to write this as (not (ior A B)) so we can split it.
3612          Similarly for IOR.  */
3613       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
3614         {
3615           SUBST (*loc,
3616                  gen_rtx_NOT (GET_MODE (x),
3617                               gen_rtx_fmt_ee (code == IOR ? AND : IOR,
3618                                               GET_MODE (x),
3619                                               XEXP (XEXP (x, 0), 0),
3620                                               XEXP (XEXP (x, 1), 0))));
3621           return find_split_point (loc, insn);
3622         }
3623
3624       /* Many RISC machines have a large set of logical insns.  If the
3625          second operand is a NOT, put it first so we will try to split the
3626          other operand first.  */
3627       if (GET_CODE (XEXP (x, 1)) == NOT)
3628         {
3629           rtx tem = XEXP (x, 0);
3630           SUBST (XEXP (x, 0), XEXP (x, 1));
3631           SUBST (XEXP (x, 1), tem);
3632         }
3633       break;
3634
3635     default:
3636       break;
3637     }
3638
3639   /* Otherwise, select our actions depending on our rtx class.  */
3640   switch (GET_RTX_CLASS (code))
3641     {
3642     case RTX_BITFIELD_OPS:              /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
3643     case RTX_TERNARY:
3644       split = find_split_point (&XEXP (x, 2), insn);
3645       if (split)
3646         return split;
3647       /* ... fall through ...  */
3648     case RTX_BIN_ARITH:
3649     case RTX_COMM_ARITH:
3650     case RTX_COMPARE:
3651     case RTX_COMM_COMPARE:
3652       split = find_split_point (&XEXP (x, 1), insn);
3653       if (split)
3654         return split;
3655       /* ... fall through ...  */
3656     case RTX_UNARY:
3657       /* Some machines have (and (shift ...) ...) insns.  If X is not
3658          an AND, but XEXP (X, 0) is, use it as our split point.  */
3659       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
3660         return &XEXP (x, 0);
3661
3662       split = find_split_point (&XEXP (x, 0), insn);
3663       if (split)
3664         return split;
3665       return loc;
3666
3667     default:
3668       /* Otherwise, we don't have a split point.  */
3669       return 0;
3670     }
3671 }
3672 \f
3673 /* Throughout X, replace FROM with TO, and return the result.
3674    The result is TO if X is FROM;
3675    otherwise the result is X, but its contents may have been modified.
3676    If they were modified, a record was made in undobuf so that
3677    undo_all will (among other things) return X to its original state.
3678
3679    If the number of changes necessary is too much to record to undo,
3680    the excess changes are not made, so the result is invalid.
3681    The changes already made can still be undone.
3682    undobuf.num_undo is incremented for such changes, so by testing that
3683    the caller can tell whether the result is valid.
3684
3685    `n_occurrences' is incremented each time FROM is replaced.
3686
3687    IN_DEST is nonzero if we are processing the SET_DEST of a SET.
3688
3689    UNIQUE_COPY is nonzero if each substitution must be unique.  We do this
3690    by copying if `n_occurrences' is nonzero.  */
3691
3692 static rtx
3693 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
3694 {
3695   enum rtx_code code = GET_CODE (x);
3696   enum machine_mode op0_mode = VOIDmode;
3697   const char *fmt;
3698   int len, i;
3699   rtx new;
3700
3701 /* Two expressions are equal if they are identical copies of a shared
3702    RTX or if they are both registers with the same register number
3703    and mode.  */
3704
3705 #define COMBINE_RTX_EQUAL_P(X,Y)                        \
3706   ((X) == (Y)                                           \
3707    || (REG_P (X) && REG_P (Y)   \
3708        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3709
3710   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3711     {
3712       n_occurrences++;
3713       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3714     }
3715
3716   /* If X and FROM are the same register but different modes, they will
3717      not have been seen as equal above.  However, flow.c will make a
3718      LOG_LINKS entry for that case.  If we do nothing, we will try to
3719      rerecognize our original insn and, when it succeeds, we will
3720      delete the feeding insn, which is incorrect.
3721
3722      So force this insn not to match in this (rare) case.  */
3723   if (! in_dest && code == REG && REG_P (from)
3724       && REGNO (x) == REGNO (from))
3725     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3726
3727   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3728      of which may contain things that can be combined.  */
3729   if (code != MEM && code != LO_SUM && OBJECT_P (x))
3730     return x;
3731
3732   /* It is possible to have a subexpression appear twice in the insn.
3733      Suppose that FROM is a register that appears within TO.
3734      Then, after that subexpression has been scanned once by `subst',
3735      the second time it is scanned, TO may be found.  If we were
3736      to scan TO here, we would find FROM within it and create a
3737      self-referent rtl structure which is completely wrong.  */
3738   if (COMBINE_RTX_EQUAL_P (x, to))
3739     return to;
3740
3741   /* Parallel asm_operands need special attention because all of the
3742      inputs are shared across the arms.  Furthermore, unsharing the
3743      rtl results in recognition failures.  Failure to handle this case
3744      specially can result in circular rtl.
3745
3746      Solve this by doing a normal pass across the first entry of the
3747      parallel, and only processing the SET_DESTs of the subsequent
3748      entries.  Ug.  */
3749
3750   if (code == PARALLEL
3751       && GET_CODE (XVECEXP (x, 0, 0)) == SET
3752       && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
3753     {
3754       new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
3755
3756       /* If this substitution failed, this whole thing fails.  */
3757       if (GET_CODE (new) == CLOBBER
3758           && XEXP (new, 0) == const0_rtx)
3759         return new;
3760
3761       SUBST (XVECEXP (x, 0, 0), new);
3762
3763       for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
3764         {
3765           rtx dest = SET_DEST (XVECEXP (x, 0, i));
3766
3767           if (!REG_P (dest)
3768               && GET_CODE (dest) != CC0
3769               && GET_CODE (dest) != PC)
3770             {
3771               new = subst (dest, from, to, 0, unique_copy);
3772
3773               /* If this substitution failed, this whole thing fails.  */
3774               if (GET_CODE (new) == CLOBBER
3775                   && XEXP (new, 0) == const0_rtx)
3776                 return new;
3777
3778               SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
3779             }
3780         }
3781     }
3782   else
3783     {
3784       len = GET_RTX_LENGTH (code);
3785       fmt = GET_RTX_FORMAT (code);
3786
3787       /* We don't need to process a SET_DEST that is a register, CC0,
3788          or PC, so set up to skip this common case.  All other cases
3789          where we want to suppress replacing something inside a
3790          SET_SRC are handled via the IN_DEST operand.  */
3791       if (code == SET
3792           && (REG_P (SET_DEST (x))
3793               || GET_CODE (SET_DEST (x)) == CC0
3794               || GET_CODE (SET_DEST (x)) == PC))
3795         fmt = "ie";
3796
3797       /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3798          constant.  */
3799       if (fmt[0] == 'e')
3800         op0_mode = GET_MODE (XEXP (x, 0));
3801
3802       for (i = 0; i < len; i++)
3803         {
3804           if (fmt[i] == 'E')
3805             {
3806               int j;
3807               for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3808                 {
3809                   if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3810                     {
3811                       new = (unique_copy && n_occurrences
3812                              ? copy_rtx (to) : to);
3813                       n_occurrences++;
3814                     }
3815                   else
3816                     {
3817                       new = subst (XVECEXP (x, i, j), from, to, 0,
3818                                    unique_copy);
3819
3820                       /* If this substitution failed, this whole thing
3821                          fails.  */
3822                       if (GET_CODE (new) == CLOBBER
3823                           && XEXP (new, 0) == const0_rtx)
3824                         return new;
3825                     }
3826
3827                   SUBST (XVECEXP (x, i, j), new);
3828                 }
3829             }
3830           else if (fmt[i] == 'e')
3831             {
3832               /* If this is a register being set, ignore it.  */
3833               new = XEXP (x, i);
3834               if (in_dest
3835                   && i == 0
3836                   && (((code == SUBREG || code == ZERO_EXTRACT)
3837                        && REG_P (new))
3838                       || code == STRICT_LOW_PART))
3839                 ;
3840
3841               else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3842                 {
3843                   /* In general, don't install a subreg involving two
3844                      modes not tieable.  It can worsen register
3845                      allocation, and can even make invalid reload
3846                      insns, since the reg inside may need to be copied
3847                      from in the outside mode, and that may be invalid
3848                      if it is an fp reg copied in integer mode.
3849
3850                      We allow two exceptions to this: It is valid if
3851                      it is inside another SUBREG and the mode of that
3852                      SUBREG and the mode of the inside of TO is
3853                      tieable and it is valid if X is a SET that copies
3854                      FROM to CC0.  */
3855
3856                   if (GET_CODE (to) == SUBREG
3857                       && ! MODES_TIEABLE_P (GET_MODE (to),
3858                                             GET_MODE (SUBREG_REG (to)))
3859                       && ! (code == SUBREG
3860                             && MODES_TIEABLE_P (GET_MODE (x),
3861                                                 GET_MODE (SUBREG_REG (to))))
3862 #ifdef HAVE_cc0
3863                       && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3864 #endif
3865                       )
3866                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3867
3868 #ifdef CANNOT_CHANGE_MODE_CLASS
3869                   if (code == SUBREG
3870                       && REG_P (to)
3871                       && REGNO (to) < FIRST_PSEUDO_REGISTER
3872                       && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
3873                                                    GET_MODE (to),
3874                                                    GET_MODE (x)))
3875                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3876 #endif
3877
3878                   new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3879                   n_occurrences++;
3880                 }
3881               else
3882                 /* If we are in a SET_DEST, suppress most cases unless we
3883                    have gone inside a MEM, in which case we want to
3884                    simplify the address.  We assume here that things that
3885                    are actually part of the destination have their inner
3886                    parts in the first expression.  This is true for SUBREG,
3887                    STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3888                    things aside from REG and MEM that should appear in a
3889                    SET_DEST.  */
3890                 new = subst (XEXP (x, i), from, to,
3891                              (((in_dest
3892                                 && (code == SUBREG || code == STRICT_LOW_PART
3893                                     || code == ZERO_EXTRACT))
3894                                || code == SET)
3895                               && i == 0), unique_copy);
3896
3897               /* If we found that we will have to reject this combination,
3898                  indicate that by returning the CLOBBER ourselves, rather than
3899                  an expression containing it.  This will speed things up as
3900                  well as prevent accidents where two CLOBBERs are considered
3901                  to be equal, thus producing an incorrect simplification.  */
3902
3903               if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3904                 return new;
3905
3906               if (GET_CODE (x) == SUBREG
3907                   && (GET_CODE (new) == CONST_INT
3908                       || GET_CODE (new) == CONST_DOUBLE))
3909                 {
3910                   enum machine_mode mode = GET_MODE (x);
3911
3912                   x = simplify_subreg (GET_MODE (x), new,
3913                                        GET_MODE (SUBREG_REG (x)),
3914                                        SUBREG_BYTE (x));
3915                   if (! x)
3916                     x = gen_rtx_CLOBBER (mode, const0_rtx);
3917                 }
3918               else if (GET_CODE (new) == CONST_INT
3919                        && GET_CODE (x) == ZERO_EXTEND)
3920                 {
3921                   x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
3922                                                 new, GET_MODE (XEXP (x, 0)));
3923                   gcc_assert (x);
3924                 }
3925               else
3926                 SUBST (XEXP (x, i), new);
3927             }
3928         }
3929     }
3930
3931   /* Try to simplify X.  If the simplification changed the code, it is likely
3932      that further simplification will help, so loop, but limit the number
3933      of repetitions that will be performed.  */
3934
3935   for (i = 0; i < 4; i++)
3936     {
3937       /* If X is sufficiently simple, don't bother trying to do anything
3938          with it.  */
3939       if (code != CONST_INT && code != REG && code != CLOBBER)
3940         x = combine_simplify_rtx (x, op0_mode, in_dest);
3941
3942       if (GET_CODE (x) == code)
3943         break;
3944
3945       code = GET_CODE (x);
3946
3947       /* We no longer know the original mode of operand 0 since we
3948          have changed the form of X)  */
3949       op0_mode = VOIDmode;
3950     }
3951
3952   return x;
3953 }
3954 \f
3955 /* Simplify X, a piece of RTL.  We just operate on the expression at the
3956    outer level; call `subst' to simplify recursively.  Return the new
3957    expression.
3958
3959    OP0_MODE is the original mode of XEXP (x, 0).  IN_DEST is nonzero
3960    if we are inside a SET_DEST.  */
3961
3962 static rtx
3963 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
3964 {
3965   enum rtx_code code = GET_CODE (x);
3966   enum machine_mode mode = GET_MODE (x);
3967   rtx temp;
3968   int i;
3969
3970   /* If this is a commutative operation, put a constant last and a complex
3971      expression first.  We don't need to do this for comparisons here.  */
3972   if (COMMUTATIVE_ARITH_P (x)
3973       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
3974     {
3975       temp = XEXP (x, 0);
3976       SUBST (XEXP (x, 0), XEXP (x, 1));
3977       SUBST (XEXP (x, 1), temp);
3978     }
3979
3980   /* If this is a simple operation applied to an IF_THEN_ELSE, try
3981      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
3982      things.  Check for cases where both arms are testing the same
3983      condition.
3984
3985      Don't do anything if all operands are very simple.  */
3986
3987   if ((BINARY_P (x)
3988        && ((!OBJECT_P (XEXP (x, 0))
3989             && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3990                   && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
3991            || (!OBJECT_P (XEXP (x, 1))
3992                && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3993                      && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
3994       || (UNARY_P (x)
3995           && (!OBJECT_P (XEXP (x, 0))
3996                && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3997                      && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
3998     {
3999       rtx cond, true_rtx, false_rtx;
4000
4001       cond = if_then_else_cond (x, &true_rtx, &false_rtx);
4002       if (cond != 0
4003           /* If everything is a comparison, what we have is highly unlikely
4004              to be simpler, so don't use it.  */
4005           && ! (COMPARISON_P (x)
4006                 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
4007         {
4008           rtx cop1 = const0_rtx;
4009           enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
4010
4011           if (cond_code == NE && COMPARISON_P (cond))
4012             return x;
4013
4014           /* Simplify the alternative arms; this may collapse the true and
4015              false arms to store-flag values.  Be careful to use copy_rtx
4016              here since true_rtx or false_rtx might share RTL with x as a
4017              result of the if_then_else_cond call above.  */
4018           true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
4019           false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
4020
4021           /* If true_rtx and false_rtx are not general_operands, an if_then_else
4022              is unlikely to be simpler.  */
4023           if (general_operand (true_rtx, VOIDmode)
4024               && general_operand (false_rtx, VOIDmode))
4025             {
4026               enum rtx_code reversed;
4027
4028               /* Restarting if we generate a store-flag expression will cause
4029                  us to loop.  Just drop through in this case.  */
4030
4031               /* If the result values are STORE_FLAG_VALUE and zero, we can
4032                  just make the comparison operation.  */
4033               if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
4034                 x = simplify_gen_relational (cond_code, mode, VOIDmode,
4035                                              cond, cop1);
4036               else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
4037                        && ((reversed = reversed_comparison_code_parts
4038                                         (cond_code, cond, cop1, NULL))
4039                            != UNKNOWN))
4040                 x = simplify_gen_relational (reversed, mode, VOIDmode,
4041                                              cond, cop1);
4042
4043               /* Likewise, we can make the negate of a comparison operation
4044                  if the result values are - STORE_FLAG_VALUE and zero.  */
4045               else if (GET_CODE (true_rtx) == CONST_INT
4046                        && INTVAL (true_rtx) == - STORE_FLAG_VALUE
4047                        && false_rtx == const0_rtx)
4048                 x = simplify_gen_unary (NEG, mode,
4049                                         simplify_gen_relational (cond_code,
4050                                                                  mode, VOIDmode,
4051                                                                  cond, cop1),
4052                                         mode);
4053               else if (GET_CODE (false_rtx) == CONST_INT
4054                        && INTVAL (false_rtx) == - STORE_FLAG_VALUE
4055                        && true_rtx == const0_rtx
4056                        && ((reversed = reversed_comparison_code_parts
4057                                         (cond_code, cond, cop1, NULL))
4058                            != UNKNOWN))
4059                 x = simplify_gen_unary (NEG, mode,
4060                                         simplify_gen_relational (reversed,
4061                                                                  mode, VOIDmode,
4062                                                                  cond, cop1),
4063                                         mode);
4064               else
4065                 return gen_rtx_IF_THEN_ELSE (mode,
4066                                              simplify_gen_relational (cond_code,
4067                                                                       mode,
4068                                                                       VOIDmode,
4069                                                                       cond,
4070                                                                       cop1),
4071                                              true_rtx, false_rtx);
4072
4073               code = GET_CODE (x);
4074               op0_mode = VOIDmode;
4075             }
4076         }
4077     }
4078
4079   /* Try to fold this expression in case we have constants that weren't
4080      present before.  */
4081   temp = 0;
4082   switch (GET_RTX_CLASS (code))
4083     {
4084     case RTX_UNARY:
4085       if (op0_mode == VOIDmode)
4086         op0_mode = GET_MODE (XEXP (x, 0));
4087       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
4088       break;
4089     case RTX_COMPARE:
4090     case RTX_COMM_COMPARE:
4091       {
4092         enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
4093         if (cmp_mode == VOIDmode)
4094           {
4095             cmp_mode = GET_MODE (XEXP (x, 1));
4096             if (cmp_mode == VOIDmode)
4097               cmp_mode = op0_mode;
4098           }
4099         temp = simplify_relational_operation (code, mode, cmp_mode,
4100                                               XEXP (x, 0), XEXP (x, 1));
4101       }
4102       break;
4103     case RTX_COMM_ARITH:
4104     case RTX_BIN_ARITH:
4105       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
4106       break;
4107     case RTX_BITFIELD_OPS:
4108     case RTX_TERNARY:
4109       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
4110                                          XEXP (x, 1), XEXP (x, 2));
4111       break;
4112     default:
4113       break;
4114     }
4115
4116   if (temp)
4117     {
4118       x = temp;
4119       code = GET_CODE (temp);
4120       op0_mode = VOIDmode;
4121       mode = GET_MODE (temp);
4122     }
4123
4124   /* First see if we can apply the inverse distributive law.  */
4125   if (code == PLUS || code == MINUS
4126       || code == AND || code == IOR || code == XOR)
4127     {
4128       x = apply_distributive_law (x);
4129       code = GET_CODE (x);
4130       op0_mode = VOIDmode;
4131     }
4132
4133   /* If CODE is an associative operation not otherwise handled, see if we
4134      can associate some operands.  This can win if they are constants or
4135      if they are logically related (i.e. (a & b) & a).  */
4136   if ((code == PLUS || code == MINUS || code == MULT || code == DIV
4137        || code == AND || code == IOR || code == XOR
4138        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
4139       && ((INTEGRAL_MODE_P (mode) && code != DIV)
4140           || (flag_unsafe_math_optimizations && FLOAT_MODE_P (mode))))
4141     {
4142       if (GET_CODE (XEXP (x, 0)) == code)
4143         {
4144           rtx other = XEXP (XEXP (x, 0), 0);
4145           rtx inner_op0 = XEXP (XEXP (x, 0), 1);
4146           rtx inner_op1 = XEXP (x, 1);
4147           rtx inner;
4148
4149           /* Make sure we pass the constant operand if any as the second
4150              one if this is a commutative operation.  */
4151           if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
4152             {
4153               rtx tem = inner_op0;
4154               inner_op0 = inner_op1;
4155               inner_op1 = tem;
4156             }
4157           inner = simplify_binary_operation (code == MINUS ? PLUS
4158                                              : code == DIV ? MULT
4159                                              : code,
4160                                              mode, inner_op0, inner_op1);
4161
4162           /* For commutative operations, try the other pair if that one
4163              didn't simplify.  */
4164           if (inner == 0 && COMMUTATIVE_ARITH_P (x))
4165             {
4166               other = XEXP (XEXP (x, 0), 1);
4167               inner = simplify_binary_operation (code, mode,
4168                                                  XEXP (XEXP (x, 0), 0),
4169                                                  XEXP (x, 1));
4170             }
4171
4172           if (inner)
4173             return simplify_gen_binary (code, mode, other, inner);
4174         }
4175     }
4176
4177   /* A little bit of algebraic simplification here.  */
4178   switch (code)
4179     {
4180     case MEM:
4181       /* Ensure that our address has any ASHIFTs converted to MULT in case
4182          address-recognizing predicates are called later.  */
4183       temp = make_compound_operation (XEXP (x, 0), MEM);
4184       SUBST (XEXP (x, 0), temp);
4185       break;
4186
4187     case SUBREG:
4188       if (op0_mode == VOIDmode)
4189         op0_mode = GET_MODE (SUBREG_REG (x));
4190
4191       /* See if this can be moved to simplify_subreg.  */
4192       if (CONSTANT_P (SUBREG_REG (x))
4193           && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
4194              /* Don't call gen_lowpart if the inner mode
4195                 is VOIDmode and we cannot simplify it, as SUBREG without
4196                 inner mode is invalid.  */
4197           && (GET_MODE (SUBREG_REG (x)) != VOIDmode
4198               || gen_lowpart_common (mode, SUBREG_REG (x))))
4199         return gen_lowpart (mode, SUBREG_REG (x));
4200
4201       if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
4202         break;
4203       {
4204         rtx temp;
4205         temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
4206                                 SUBREG_BYTE (x));
4207         if (temp)
4208           return temp;
4209       }
4210
4211       /* Don't change the mode of the MEM if that would change the meaning
4212          of the address.  */
4213       if (MEM_P (SUBREG_REG (x))
4214           && (MEM_VOLATILE_P (SUBREG_REG (x))
4215               || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
4216         return gen_rtx_CLOBBER (mode, const0_rtx);
4217
4218       /* Note that we cannot do any narrowing for non-constants since
4219          we might have been counting on using the fact that some bits were
4220          zero.  We now do this in the SET.  */
4221
4222       break;
4223
4224     case NEG:
4225       temp = expand_compound_operation (XEXP (x, 0));
4226
4227       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
4228          replaced by (lshiftrt X C).  This will convert
4229          (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
4230
4231       if (GET_CODE (temp) == ASHIFTRT
4232           && GET_CODE (XEXP (temp, 1)) == CONST_INT
4233           && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
4234         return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
4235                                      INTVAL (XEXP (temp, 1)));
4236
4237       /* If X has only a single bit that might be nonzero, say, bit I, convert
4238          (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
4239          MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
4240          (sign_extract X 1 Y).  But only do this if TEMP isn't a register
4241          or a SUBREG of one since we'd be making the expression more
4242          complex if it was just a register.  */
4243
4244       if (!REG_P (temp)
4245           && ! (GET_CODE (temp) == SUBREG
4246                 && REG_P (SUBREG_REG (temp)))
4247           && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
4248         {
4249           rtx temp1 = simplify_shift_const
4250             (NULL_RTX, ASHIFTRT, mode,
4251              simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
4252                                    GET_MODE_BITSIZE (mode) - 1 - i),
4253              GET_MODE_BITSIZE (mode) - 1 - i);
4254
4255           /* If all we did was surround TEMP with the two shifts, we
4256              haven't improved anything, so don't use it.  Otherwise,
4257              we are better off with TEMP1.  */
4258           if (GET_CODE (temp1) != ASHIFTRT
4259               || GET_CODE (XEXP (temp1, 0)) != ASHIFT
4260               || XEXP (XEXP (temp1, 0), 0) != temp)
4261             return temp1;
4262         }
4263       break;
4264
4265     case TRUNCATE:
4266       /* We can't handle truncation to a partial integer mode here
4267          because we don't know the real bitsize of the partial
4268          integer mode.  */
4269       if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
4270         break;
4271
4272       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4273           && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4274                                     GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
4275         SUBST (XEXP (x, 0),
4276                force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
4277                               GET_MODE_MASK (mode), 0));
4278
4279       /* Similarly to what we do in simplify-rtx.c, a truncate of a register
4280          whose value is a comparison can be replaced with a subreg if
4281          STORE_FLAG_VALUE permits.  */
4282       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4283           && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4284           && (temp = get_last_value (XEXP (x, 0)))
4285           && COMPARISON_P (temp))
4286         return gen_lowpart (mode, XEXP (x, 0));
4287       break;
4288
4289 #ifdef HAVE_cc0
4290     case COMPARE:
4291       /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4292          using cc0, in which case we want to leave it as a COMPARE
4293          so we can distinguish it from a register-register-copy.  */
4294       if (XEXP (x, 1) == const0_rtx)
4295         return XEXP (x, 0);
4296
4297       /* x - 0 is the same as x unless x's mode has signed zeros and
4298          allows rounding towards -infinity.  Under those conditions,
4299          0 - 0 is -0.  */
4300       if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4301             && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4302           && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4303         return XEXP (x, 0);
4304       break;
4305 #endif
4306
4307     case CONST:
4308       /* (const (const X)) can become (const X).  Do it this way rather than
4309          returning the inner CONST since CONST can be shared with a
4310          REG_EQUAL note.  */
4311       if (GET_CODE (XEXP (x, 0)) == CONST)
4312         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4313       break;
4314
4315 #ifdef HAVE_lo_sum
4316     case LO_SUM:
4317       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
4318          can add in an offset.  find_split_point will split this address up
4319          again if it doesn't match.  */
4320       if (GET_CODE (XEXP (x, 0)) == HIGH
4321           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4322         return XEXP (x, 1);
4323       break;
4324 #endif
4325
4326     case PLUS:
4327       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4328          when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4329          bit-field and can be replaced by either a sign_extend or a
4330          sign_extract.  The `and' may be a zero_extend and the two
4331          <c>, -<c> constants may be reversed.  */
4332       if (GET_CODE (XEXP (x, 0)) == XOR
4333           && GET_CODE (XEXP (x, 1)) == CONST_INT
4334           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4335           && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4336           && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4337               || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4338           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4339           && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4340                && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4341                && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4342                    == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4343               || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4344                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4345                       == (unsigned int) i + 1))))
4346         return simplify_shift_const
4347           (NULL_RTX, ASHIFTRT, mode,
4348            simplify_shift_const (NULL_RTX, ASHIFT, mode,
4349                                  XEXP (XEXP (XEXP (x, 0), 0), 0),
4350                                  GET_MODE_BITSIZE (mode) - (i + 1)),
4351            GET_MODE_BITSIZE (mode) - (i + 1));
4352
4353       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4354          can become (ashiftrt (ashift (xor x 1) C) C) where C is
4355          the bitsize of the mode - 1.  This allows simplification of
4356          "a = (b & 8) == 0;"  */
4357       if (XEXP (x, 1) == constm1_rtx
4358           && !REG_P (XEXP (x, 0))
4359           && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4360                 && REG_P (SUBREG_REG (XEXP (x, 0))))
4361           && nonzero_bits (XEXP (x, 0), mode) == 1)
4362         return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4363            simplify_shift_const (NULL_RTX, ASHIFT, mode,
4364                                  gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4365                                  GET_MODE_BITSIZE (mode) - 1),
4366            GET_MODE_BITSIZE (mode) - 1);
4367
4368       /* If we are adding two things that have no bits in common, convert
4369          the addition into an IOR.  This will often be further simplified,
4370          for example in cases like ((a & 1) + (a & 2)), which can
4371          become a & 3.  */
4372
4373       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4374           && (nonzero_bits (XEXP (x, 0), mode)
4375               & nonzero_bits (XEXP (x, 1), mode)) == 0)
4376         {
4377           /* Try to simplify the expression further.  */
4378           rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4379           temp = combine_simplify_rtx (tor, mode, in_dest);
4380
4381           /* If we could, great.  If not, do not go ahead with the IOR
4382              replacement, since PLUS appears in many special purpose
4383              address arithmetic instructions.  */
4384           if (GET_CODE (temp) != CLOBBER && temp != tor)
4385             return temp;
4386         }
4387       break;
4388
4389     case MINUS:
4390       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4391          (and <foo> (const_int pow2-1))  */
4392       if (GET_CODE (XEXP (x, 1)) == AND
4393           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4394           && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4395           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4396         return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4397                                        -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4398       break;
4399
4400     case MULT:
4401       /* If we have (mult (plus A B) C), apply the distributive law and then
4402          the inverse distributive law to see if things simplify.  This
4403          occurs mostly in addresses, often when unrolling loops.  */
4404
4405       if (GET_CODE (XEXP (x, 0)) == PLUS)
4406         {
4407           rtx result = distribute_and_simplify_rtx (x, 0);
4408           if (result)
4409             return result;
4410         }
4411
4412       /* Try simplify a*(b/c) as (a*b)/c.  */
4413       if (FLOAT_MODE_P (mode) && flag_unsafe_math_optimizations
4414           && GET_CODE (XEXP (x, 0)) == DIV)
4415         {
4416           rtx tem = simplify_binary_operation (MULT, mode,
4417                                                XEXP (XEXP (x, 0), 0),
4418                                                XEXP (x, 1));
4419           if (tem)
4420             return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
4421         }
4422       break;
4423
4424     case UDIV:
4425       /* If this is a divide by a power of two, treat it as a shift if
4426          its first operand is a shift.  */
4427       if (GET_CODE (XEXP (x, 1)) == CONST_INT
4428           && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4429           && (GET_CODE (XEXP (x, 0)) == ASHIFT
4430               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4431               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4432               || GET_CODE (XEXP (x, 0)) == ROTATE
4433               || GET_CODE (XEXP (x, 0)) == ROTATERT))
4434         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4435       break;
4436
4437     case EQ:  case NE:
4438     case GT:  case GTU:  case GE:  case GEU:
4439     case LT:  case LTU:  case LE:  case LEU:
4440     case UNEQ:  case LTGT:
4441     case UNGT:  case UNGE:
4442     case UNLT:  case UNLE:
4443     case UNORDERED: case ORDERED:
4444       /* If the first operand is a condition code, we can't do anything
4445          with it.  */
4446       if (GET_CODE (XEXP (x, 0)) == COMPARE
4447           || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4448               && ! CC0_P (XEXP (x, 0))))
4449         {
4450           rtx op0 = XEXP (x, 0);
4451           rtx op1 = XEXP (x, 1);
4452           enum rtx_code new_code;
4453
4454           if (GET_CODE (op0) == COMPARE)
4455             op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4456
4457           /* Simplify our comparison, if possible.  */
4458           new_code = simplify_comparison (code, &op0, &op1);
4459
4460           /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4461              if only the low-order bit is possibly nonzero in X (such as when
4462              X is a ZERO_EXTRACT of one bit).  Similarly, we can convert EQ to
4463              (xor X 1) or (minus 1 X); we use the former.  Finally, if X is
4464              known to be either 0 or -1, NE becomes a NEG and EQ becomes
4465              (plus X 1).
4466
4467              Remove any ZERO_EXTRACT we made when thinking this was a
4468              comparison.  It may now be simpler to use, e.g., an AND.  If a
4469              ZERO_EXTRACT is indeed appropriate, it will be placed back by
4470              the call to make_compound_operation in the SET case.  */
4471
4472           if (STORE_FLAG_VALUE == 1
4473               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4474               && op1 == const0_rtx
4475               && mode == GET_MODE (op0)
4476               && nonzero_bits (op0, mode) == 1)
4477             return gen_lowpart (mode,
4478                                 expand_compound_operation (op0));
4479
4480           else if (STORE_FLAG_VALUE == 1
4481                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4482                    && op1 == const0_rtx
4483                    && mode == GET_MODE (op0)
4484                    && (num_sign_bit_copies (op0, mode)
4485                        == GET_MODE_BITSIZE (mode)))
4486             {
4487               op0 = expand_compound_operation (op0);
4488               return simplify_gen_unary (NEG, mode,
4489                                          gen_lowpart (mode, op0),
4490                                          mode);
4491             }
4492
4493           else if (STORE_FLAG_VALUE == 1
4494                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4495                    && op1 == const0_rtx
4496                    && mode == GET_MODE (op0)
4497                    && nonzero_bits (op0, mode) == 1)
4498             {
4499               op0 = expand_compound_operation (op0);
4500               return simplify_gen_binary (XOR, mode,
4501                                           gen_lowpart (mode, op0),
4502                                           const1_rtx);
4503             }
4504
4505           else if (STORE_FLAG_VALUE == 1
4506                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4507                    && op1 == const0_rtx
4508                    && mode == GET_MODE (op0)
4509                    && (num_sign_bit_copies (op0, mode)
4510                        == GET_MODE_BITSIZE (mode)))
4511             {
4512               op0 = expand_compound_operation (op0);
4513               return plus_constant (gen_lowpart (mode, op0), 1);
4514             }
4515
4516           /* If STORE_FLAG_VALUE is -1, we have cases similar to
4517              those above.  */
4518           if (STORE_FLAG_VALUE == -1
4519               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4520               && op1 == const0_rtx
4521               && (num_sign_bit_copies (op0, mode)
4522                   == GET_MODE_BITSIZE (mode)))
4523             return gen_lowpart (mode,
4524                                 expand_compound_operation (op0));
4525
4526           else if (STORE_FLAG_VALUE == -1
4527                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4528                    && op1 == const0_rtx
4529                    && mode == GET_MODE (op0)
4530                    && nonzero_bits (op0, mode) == 1)
4531             {
4532               op0 = expand_compound_operation (op0);
4533               return simplify_gen_unary (NEG, mode,
4534                                          gen_lowpart (mode, op0),
4535                                          mode);
4536             }
4537
4538           else if (STORE_FLAG_VALUE == -1
4539                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4540                    && op1 == const0_rtx
4541                    && mode == GET_MODE (op0)
4542                    && (num_sign_bit_copies (op0, mode)
4543                        == GET_MODE_BITSIZE (mode)))
4544             {
4545               op0 = expand_compound_operation (op0);
4546               return simplify_gen_unary (NOT, mode,
4547                                          gen_lowpart (mode, op0),
4548                                          mode);
4549             }
4550
4551           /* If X is 0/1, (eq X 0) is X-1.  */
4552           else if (STORE_FLAG_VALUE == -1
4553                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4554                    && op1 == const0_rtx
4555                    && mode == GET_MODE (op0)
4556                    && nonzero_bits (op0, mode) == 1)
4557             {
4558               op0 = expand_compound_operation (op0);
4559               return plus_constant (gen_lowpart (mode, op0), -1);
4560             }
4561
4562           /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4563              one bit that might be nonzero, we can convert (ne x 0) to
4564              (ashift x c) where C puts the bit in the sign bit.  Remove any
4565              AND with STORE_FLAG_VALUE when we are done, since we are only
4566              going to test the sign bit.  */
4567           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4568               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4569               && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4570                   == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
4571               && op1 == const0_rtx
4572               && mode == GET_MODE (op0)
4573               && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4574             {
4575               x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4576                                         expand_compound_operation (op0),
4577                                         GET_MODE_BITSIZE (mode) - 1 - i);
4578               if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4579                 return XEXP (x, 0);
4580               else
4581                 return x;
4582             }
4583
4584           /* If the code changed, return a whole new comparison.  */
4585           if (new_code != code)
4586             return gen_rtx_fmt_ee (new_code, mode, op0, op1);
4587
4588           /* Otherwise, keep this operation, but maybe change its operands.
4589              This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
4590           SUBST (XEXP (x, 0), op0);
4591           SUBST (XEXP (x, 1), op1);
4592         }
4593       break;
4594
4595     case IF_THEN_ELSE:
4596       return simplify_if_then_else (x);
4597
4598     case ZERO_EXTRACT:
4599     case SIGN_EXTRACT:
4600     case ZERO_EXTEND:
4601     case SIGN_EXTEND:
4602       /* If we are processing SET_DEST, we are done.  */
4603       if (in_dest)
4604         return x;
4605
4606       return expand_compound_operation (x);
4607
4608     case SET:
4609       return simplify_set (x);
4610
4611     case AND:
4612     case IOR:
4613       return simplify_logical (x);
4614
4615     case ASHIFT:
4616     case LSHIFTRT:
4617     case ASHIFTRT:
4618     case ROTATE:
4619     case ROTATERT:
4620       /* If this is a shift by a constant amount, simplify it.  */
4621       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4622         return simplify_shift_const (x, code, mode, XEXP (x, 0),
4623                                      INTVAL (XEXP (x, 1)));
4624
4625       else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
4626         SUBST (XEXP (x, 1),
4627                force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
4628                               ((HOST_WIDE_INT) 1
4629                                << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4630                               - 1,
4631                               0));
4632       break;
4633
4634     default:
4635       break;
4636     }
4637
4638   return x;
4639 }
4640 \f
4641 /* Simplify X, an IF_THEN_ELSE expression.  Return the new expression.  */
4642
4643 static rtx
4644 simplify_if_then_else (rtx x)
4645 {
4646   enum machine_mode mode = GET_MODE (x);
4647   rtx cond = XEXP (x, 0);
4648   rtx true_rtx = XEXP (x, 1);
4649   rtx false_rtx = XEXP (x, 2);
4650   enum rtx_code true_code = GET_CODE (cond);
4651   int comparison_p = COMPARISON_P (cond);
4652   rtx temp;
4653   int i;
4654   enum rtx_code false_code;
4655   rtx reversed;
4656
4657   /* Simplify storing of the truth value.  */
4658   if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
4659     return simplify_gen_relational (true_code, mode, VOIDmode,
4660                                     XEXP (cond, 0), XEXP (cond, 1));
4661
4662   /* Also when the truth value has to be reversed.  */
4663   if (comparison_p
4664       && true_rtx == const0_rtx && false_rtx == const_true_rtx
4665       && (reversed = reversed_comparison (cond, mode)))
4666     return reversed;
4667
4668   /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4669      in it is being compared against certain values.  Get the true and false
4670      comparisons and see if that says anything about the value of each arm.  */
4671
4672   if (comparison_p
4673       && ((false_code = reversed_comparison_code (cond, NULL))
4674           != UNKNOWN)
4675       && REG_P (XEXP (cond, 0)))
4676     {
4677       HOST_WIDE_INT nzb;
4678       rtx from = XEXP (cond, 0);
4679       rtx true_val = XEXP (cond, 1);
4680       rtx false_val = true_val;
4681       int swapped = 0;
4682
4683       /* If FALSE_CODE is EQ, swap the codes and arms.  */
4684
4685       if (false_code == EQ)
4686         {
4687           swapped = 1, true_code = EQ, false_code = NE;
4688           temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4689         }
4690
4691       /* If we are comparing against zero and the expression being tested has
4692          only a single bit that might be nonzero, that is its value when it is
4693          not equal to zero.  Similarly if it is known to be -1 or 0.  */
4694
4695       if (true_code == EQ && true_val == const0_rtx
4696           && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4697         false_code = EQ, false_val = GEN_INT (nzb);
4698       else if (true_code == EQ && true_val == const0_rtx
4699                && (num_sign_bit_copies (from, GET_MODE (from))
4700                    == GET_MODE_BITSIZE (GET_MODE (from))))
4701         false_code = EQ, false_val = constm1_rtx;
4702
4703       /* Now simplify an arm if we know the value of the register in the
4704          branch and it is used in the arm.  Be careful due to the potential
4705          of locally-shared RTL.  */
4706
4707       if (reg_mentioned_p (from, true_rtx))
4708         true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
4709                                       from, true_val),
4710                       pc_rtx, pc_rtx, 0, 0);
4711       if (reg_mentioned_p (from, false_rtx))
4712         false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
4713                                    from, false_val),
4714                        pc_rtx, pc_rtx, 0, 0);
4715
4716       SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
4717       SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
4718
4719       true_rtx = XEXP (x, 1);
4720       false_rtx = XEXP (x, 2);
4721       true_code = GET_CODE (cond);
4722     }
4723
4724   /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4725      reversed, do so to avoid needing two sets of patterns for
4726      subtract-and-branch insns.  Similarly if we have a constant in the true
4727      arm, the false arm is the same as the first operand of the comparison, or
4728      the false arm is more complicated than the true arm.  */
4729
4730   if (comparison_p
4731       && reversed_comparison_code (cond, NULL) != UNKNOWN
4732       && (true_rtx == pc_rtx
4733           || (CONSTANT_P (true_rtx)
4734               && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
4735           || true_rtx == const0_rtx
4736           || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
4737           || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
4738               && !OBJECT_P (false_rtx))
4739           || reg_mentioned_p (true_rtx, false_rtx)
4740           || rtx_equal_p (false_rtx, XEXP (cond, 0))))
4741     {
4742       true_code = reversed_comparison_code (cond, NULL);
4743       SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
4744       SUBST (XEXP (x, 1), false_rtx);
4745       SUBST (XEXP (x, 2), true_rtx);
4746
4747       temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4748       cond = XEXP (x, 0);
4749
4750       /* It is possible that the conditional has been simplified out.  */
4751       true_code = GET_CODE (cond);
4752       comparison_p = COMPARISON_P (cond);
4753     }
4754
4755   /* If the two arms are identical, we don't need the comparison.  */
4756
4757   if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
4758     return true_rtx;
4759
4760   /* Convert a == b ? b : a to "a".  */
4761   if (true_code == EQ && ! side_effects_p (cond)
4762       && !HONOR_NANS (mode)
4763       && rtx_equal_p (XEXP (cond, 0), false_rtx)
4764       && rtx_equal_p (XEXP (cond, 1), true_rtx))
4765     return false_rtx;
4766   else if (true_code == NE && ! side_effects_p (cond)
4767            && !HONOR_NANS (mode)
4768            && rtx_equal_p (XEXP (cond, 0), true_rtx)
4769            && rtx_equal_p (XEXP (cond, 1), false_rtx))
4770     return true_rtx;
4771
4772   /* Look for cases where we have (abs x) or (neg (abs X)).  */
4773
4774   if (GET_MODE_CLASS (mode) == MODE_INT
4775       && GET_CODE (false_rtx) == NEG
4776       && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
4777       && comparison_p
4778       && rtx_equal_p (true_rtx, XEXP (cond, 0))
4779       && ! side_effects_p (true_rtx))
4780     switch (true_code)
4781       {
4782       case GT:
4783       case GE:
4784         return simplify_gen_unary (ABS, mode, true_rtx, mode);
4785       case LT:
4786       case LE:
4787         return
4788           simplify_gen_unary (NEG, mode,
4789                               simplify_gen_unary (ABS, mode, true_rtx, mode),
4790                               mode);
4791       default:
4792         break;
4793       }
4794
4795   /* Look for MIN or MAX.  */
4796
4797   if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
4798       && comparison_p
4799       && rtx_equal_p (XEXP (cond, 0), true_rtx)
4800       && rtx_equal_p (XEXP (cond, 1), false_rtx)
4801       && ! side_effects_p (cond))
4802     switch (true_code)
4803       {
4804       case GE:
4805       case GT:
4806         return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
4807       case LE:
4808       case LT:
4809         return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
4810       case GEU:
4811       case GTU:
4812         return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
4813       case LEU:
4814       case LTU:
4815         return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
4816       default:
4817         break;
4818       }
4819
4820   /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4821      second operand is zero, this can be done as (OP Z (mult COND C2)) where
4822      C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4823      SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4824      We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4825      neither 1 or -1, but it isn't worth checking for.  */
4826
4827   if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4828       && comparison_p
4829       && GET_MODE_CLASS (mode) == MODE_INT
4830       && ! side_effects_p (x))
4831     {
4832       rtx t = make_compound_operation (true_rtx, SET);
4833       rtx f = make_compound_operation (false_rtx, SET);
4834       rtx cond_op0 = XEXP (cond, 0);
4835       rtx cond_op1 = XEXP (cond, 1);
4836       enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
4837       enum machine_mode m = mode;
4838       rtx z = 0, c1 = NULL_RTX;
4839
4840       if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4841            || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4842            || GET_CODE (t) == ASHIFT
4843            || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4844           && rtx_equal_p (XEXP (t, 0), f))
4845         c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4846
4847       /* If an identity-zero op is commutative, check whether there
4848          would be a match if we swapped the operands.  */
4849       else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4850                 || GET_CODE (t) == XOR)
4851                && rtx_equal_p (XEXP (t, 1), f))
4852         c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4853       else if (GET_CODE (t) == SIGN_EXTEND
4854                && (GET_CODE (XEXP (t, 0)) == PLUS
4855                    || GET_CODE (XEXP (t, 0)) == MINUS
4856                    || GET_CODE (XEXP (t, 0)) == IOR
4857                    || GET_CODE (XEXP (t, 0)) == XOR
4858                    || GET_CODE (XEXP (t, 0)) == ASHIFT
4859                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4860                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4861                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4862                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4863                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4864                && (num_sign_bit_copies (f, GET_MODE (f))
4865                    > (unsigned int)
4866                      (GET_MODE_BITSIZE (mode)
4867                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4868         {
4869           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4870           extend_op = SIGN_EXTEND;
4871           m = GET_MODE (XEXP (t, 0));
4872         }
4873       else if (GET_CODE (t) == SIGN_EXTEND
4874                && (GET_CODE (XEXP (t, 0)) == PLUS
4875                    || GET_CODE (XEXP (t, 0)) == IOR
4876                    || GET_CODE (XEXP (t, 0)) == XOR)
4877                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4878                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4879                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4880                && (num_sign_bit_copies (f, GET_MODE (f))
4881                    > (unsigned int)
4882                      (GET_MODE_BITSIZE (mode)
4883                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
4884         {
4885           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4886           extend_op = SIGN_EXTEND;
4887           m = GET_MODE (XEXP (t, 0));
4888         }
4889       else if (GET_CODE (t) == ZERO_EXTEND
4890                && (GET_CODE (XEXP (t, 0)) == PLUS
4891                    || GET_CODE (XEXP (t, 0)) == MINUS
4892                    || GET_CODE (XEXP (t, 0)) == IOR
4893                    || GET_CODE (XEXP (t, 0)) == XOR
4894                    || GET_CODE (XEXP (t, 0)) == ASHIFT
4895                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4896                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4897                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4898                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4899                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4900                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4901                && ((nonzero_bits (f, GET_MODE (f))
4902                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
4903                    == 0))
4904         {
4905           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4906           extend_op = ZERO_EXTEND;
4907           m = GET_MODE (XEXP (t, 0));
4908         }
4909       else if (GET_CODE (t) == ZERO_EXTEND
4910                && (GET_CODE (XEXP (t, 0)) == PLUS
4911                    || GET_CODE (XEXP (t, 0)) == IOR
4912                    || GET_CODE (XEXP (t, 0)) == XOR)
4913                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4914                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4915                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4916                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4917                && ((nonzero_bits (f, GET_MODE (f))
4918                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
4919                    == 0))
4920         {
4921           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4922           extend_op = ZERO_EXTEND;
4923           m = GET_MODE (XEXP (t, 0));
4924         }
4925
4926       if (z)
4927         {
4928           temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
4929                                                  cond_op0, cond_op1),
4930                         pc_rtx, pc_rtx, 0, 0);
4931           temp = simplify_gen_binary (MULT, m, temp,
4932                                       simplify_gen_binary (MULT, m, c1,
4933                                                            const_true_rtx));
4934           temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
4935           temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
4936
4937           if (extend_op != UNKNOWN)
4938             temp = simplify_gen_unary (extend_op, mode, temp, m);
4939
4940           return temp;
4941         }
4942     }
4943
4944   /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
4945      1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
4946      negation of a single bit, we can convert this operation to a shift.  We
4947      can actually do this more generally, but it doesn't seem worth it.  */
4948
4949   if (true_code == NE && XEXP (cond, 1) == const0_rtx
4950       && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
4951       && ((1 == nonzero_bits (XEXP (cond, 0), mode)
4952            && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
4953           || ((num_sign_bit_copies (XEXP (cond, 0), mode)
4954                == GET_MODE_BITSIZE (mode))
4955               && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
4956     return
4957       simplify_shift_const (NULL_RTX, ASHIFT, mode,
4958                             gen_lowpart (mode, XEXP (cond, 0)), i);
4959
4960   /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8.  */
4961   if (true_code == NE && XEXP (cond, 1) == const0_rtx
4962       && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
4963       && GET_MODE (XEXP (cond, 0)) == mode
4964       && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
4965           == nonzero_bits (XEXP (cond, 0), mode)
4966       && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
4967     return XEXP (cond, 0);
4968
4969   return x;
4970 }
4971 \f
4972 /* Simplify X, a SET expression.  Return the new expression.  */
4973
4974 static rtx
4975 simplify_set (rtx x)
4976 {
4977   rtx src = SET_SRC (x);
4978   rtx dest = SET_DEST (x);
4979   enum machine_mode mode
4980     = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
4981   rtx other_insn;
4982   rtx *cc_use;
4983
4984   /* (set (pc) (return)) gets written as (return).  */
4985   if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
4986     return src;
4987
4988   /* Now that we know for sure which bits of SRC we are using, see if we can
4989      simplify the expression for the object knowing that we only need the
4990      low-order bits.  */
4991
4992   if (GET_MODE_CLASS (mode) == MODE_INT
4993       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
4994     {
4995       src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, 0);
4996       SUBST (SET_SRC (x), src);
4997     }
4998
4999   /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5000      the comparison result and try to simplify it unless we already have used
5001      undobuf.other_insn.  */
5002   if ((GET_MODE_CLASS (mode) == MODE_CC
5003        || GET_CODE (src) == COMPARE
5004        || CC0_P (dest))
5005       && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5006       && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5007       && COMPARISON_P (*cc_use)
5008       && rtx_equal_p (XEXP (*cc_use, 0), dest))
5009     {
5010       enum rtx_code old_code = GET_CODE (*cc_use);
5011       enum rtx_code new_code;
5012       rtx op0, op1, tmp;
5013       int other_changed = 0;
5014       enum machine_mode compare_mode = GET_MODE (dest);
5015
5016       if (GET_CODE (src) == COMPARE)
5017         op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5018       else
5019         op0 = src, op1 = CONST0_RTX (GET_MODE (src));
5020
5021       tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
5022                                            op0, op1);
5023       if (!tmp)
5024         new_code = old_code;
5025       else if (!CONSTANT_P (tmp))
5026         {
5027           new_code = GET_CODE (tmp);
5028           op0 = XEXP (tmp, 0);
5029           op1 = XEXP (tmp, 1);
5030         }
5031       else
5032         {
5033           rtx pat = PATTERN (other_insn);
5034           undobuf.other_insn = other_insn;
5035           SUBST (*cc_use, tmp);
5036
5037           /* Attempt to simplify CC user.  */
5038           if (GET_CODE (pat) == SET)
5039             {
5040               rtx new = simplify_rtx (SET_SRC (pat));
5041               if (new != NULL_RTX)
5042                 SUBST (SET_SRC (pat), new);
5043             }
5044
5045           /* Convert X into a no-op move.  */
5046           SUBST (SET_DEST (x), pc_rtx);
5047           SUBST (SET_SRC (x), pc_rtx);
5048           return x;
5049         }
5050
5051       /* Simplify our comparison, if possible.  */
5052       new_code = simplify_comparison (new_code, &op0, &op1);
5053
5054 #ifdef SELECT_CC_MODE
5055       /* If this machine has CC modes other than CCmode, check to see if we
5056          need to use a different CC mode here.  */
5057       if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
5058         compare_mode = GET_MODE (op0);
5059       else
5060         compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5061
5062 #ifndef HAVE_cc0
5063       /* If the mode changed, we have to change SET_DEST, the mode in the
5064          compare, and the mode in the place SET_DEST is used.  If SET_DEST is
5065          a hard register, just build new versions with the proper mode.  If it
5066          is a pseudo, we lose unless it is only time we set the pseudo, in
5067          which case we can safely change its mode.  */
5068       if (compare_mode != GET_MODE (dest))
5069         {
5070           if (can_change_dest_mode (dest, 0, compare_mode))
5071             {
5072               unsigned int regno = REGNO (dest);
5073               rtx new_dest = gen_rtx_REG (compare_mode, regno);
5074
5075               if (regno >= FIRST_PSEUDO_REGISTER)
5076                 SUBST (regno_reg_rtx[regno], new_dest);
5077
5078               SUBST (SET_DEST (x), new_dest);
5079               SUBST (XEXP (*cc_use, 0), new_dest);
5080               other_changed = 1;
5081
5082               dest = new_dest;
5083             }
5084         }
5085 #endif  /* cc0 */
5086 #endif  /* SELECT_CC_MODE */
5087
5088       /* If the code changed, we have to build a new comparison in
5089          undobuf.other_insn.  */
5090       if (new_code != old_code)
5091         {
5092           int other_changed_previously = other_changed;
5093           unsigned HOST_WIDE_INT mask;
5094
5095           SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5096                                           dest, const0_rtx));
5097           other_changed = 1;
5098
5099           /* If the only change we made was to change an EQ into an NE or
5100              vice versa, OP0 has only one bit that might be nonzero, and OP1
5101              is zero, check if changing the user of the condition code will
5102              produce a valid insn.  If it won't, we can keep the original code
5103              in that insn by surrounding our operation with an XOR.  */
5104
5105           if (((old_code == NE && new_code == EQ)
5106                || (old_code == EQ && new_code == NE))
5107               && ! other_changed_previously && op1 == const0_rtx
5108               && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5109               && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5110             {
5111               rtx pat = PATTERN (other_insn), note = 0;
5112
5113               if ((recog_for_combine (&pat, other_insn, &note) < 0
5114                    && ! check_asm_operands (pat)))
5115                 {
5116                   PUT_CODE (*cc_use, old_code);
5117                   other_changed = 0;
5118
5119                   op0 = simplify_gen_binary (XOR, GET_MODE (op0),
5120                                              op0, GEN_INT (mask));
5121                 }
5122             }
5123         }
5124
5125       if (other_changed)
5126         undobuf.other_insn = other_insn;
5127
5128 #ifdef HAVE_cc0
5129       /* If we are now comparing against zero, change our source if
5130          needed.  If we do not use cc0, we always have a COMPARE.  */
5131       if (op1 == const0_rtx && dest == cc0_rtx)
5132         {
5133           SUBST (SET_SRC (x), op0);
5134           src = op0;
5135         }
5136       else
5137 #endif
5138
5139       /* Otherwise, if we didn't previously have a COMPARE in the
5140          correct mode, we need one.  */
5141       if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5142         {
5143           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5144           src = SET_SRC (x);
5145         }
5146       else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
5147         {
5148           SUBST(SET_SRC (x), op0);
5149           src = SET_SRC (x);
5150         }
5151       else
5152         {
5153           /* Otherwise, update the COMPARE if needed.  */
5154           SUBST (XEXP (src, 0), op0);
5155           SUBST (XEXP (src, 1), op1);
5156         }
5157     }
5158   else
5159     {
5160       /* Get SET_SRC in a form where we have placed back any
5161          compound expressions.  Then do the checks below.  */
5162       src = make_compound_operation (src, SET);
5163       SUBST (SET_SRC (x), src);
5164     }
5165
5166   /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5167      and X being a REG or (subreg (reg)), we may be able to convert this to
5168      (set (subreg:m2 x) (op)).
5169
5170      We can always do this if M1 is narrower than M2 because that means that
5171      we only care about the low bits of the result.
5172
5173      However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
5174      perform a narrower operation than requested since the high-order bits will
5175      be undefined.  On machine where it is defined, this transformation is safe
5176      as long as M1 and M2 have the same number of words.  */
5177
5178   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5179       && !OBJECT_P (SUBREG_REG (src))
5180       && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5181            / UNITS_PER_WORD)
5182           == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5183                + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5184 #ifndef WORD_REGISTER_OPERATIONS
5185       && (GET_MODE_SIZE (GET_MODE (src))
5186         < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5187 #endif
5188 #ifdef CANNOT_CHANGE_MODE_CLASS
5189       && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
5190             && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5191                                          GET_MODE (SUBREG_REG (src)),
5192                                          GET_MODE (src)))
5193 #endif
5194       && (REG_P (dest)
5195           || (GET_CODE (dest) == SUBREG
5196               && REG_P (SUBREG_REG (dest)))))
5197     {
5198       SUBST (SET_DEST (x),
5199              gen_lowpart (GET_MODE (SUBREG_REG (src)),
5200                                       dest));
5201       SUBST (SET_SRC (x), SUBREG_REG (src));
5202
5203       src = SET_SRC (x), dest = SET_DEST (x);
5204     }
5205
5206 #ifdef HAVE_cc0
5207   /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5208      in SRC.  */
5209   if (dest == cc0_rtx
5210       && GET_CODE (src) == SUBREG
5211       && subreg_lowpart_p (src)
5212       && (GET_MODE_BITSIZE (GET_MODE (src))
5213           < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5214     {
5215       rtx inner = SUBREG_REG (src);
5216       enum machine_mode inner_mode = GET_MODE (inner);
5217
5218       /* Here we make sure that we don't have a sign bit on.  */
5219       if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5220           && (nonzero_bits (inner, inner_mode)
5221               < ((unsigned HOST_WIDE_INT) 1
5222                  << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5223         {
5224           SUBST (SET_SRC (x), inner);
5225           src = SET_SRC (x);
5226         }
5227     }
5228 #endif
5229
5230 #ifdef LOAD_EXTEND_OP
5231   /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5232      would require a paradoxical subreg.  Replace the subreg with a
5233      zero_extend to avoid the reload that would otherwise be required.  */
5234
5235   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5236       && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
5237       && SUBREG_BYTE (src) == 0
5238       && (GET_MODE_SIZE (GET_MODE (src))
5239           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5240       && MEM_P (SUBREG_REG (src)))
5241     {
5242       SUBST (SET_SRC (x),
5243              gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5244                             GET_MODE (src), SUBREG_REG (src)));
5245
5246       src = SET_SRC (x);
5247     }
5248 #endif
5249
5250   /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5251      are comparing an item known to be 0 or -1 against 0, use a logical
5252      operation instead. Check for one of the arms being an IOR of the other
5253      arm with some value.  We compute three terms to be IOR'ed together.  In
5254      practice, at most two will be nonzero.  Then we do the IOR's.  */
5255
5256   if (GET_CODE (dest) != PC
5257       && GET_CODE (src) == IF_THEN_ELSE
5258       && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5259       && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5260       && XEXP (XEXP (src, 0), 1) == const0_rtx
5261       && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5262 #ifdef HAVE_conditional_move
5263       && ! can_conditionally_move_p (GET_MODE (src))
5264 #endif
5265       && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5266                                GET_MODE (XEXP (XEXP (src, 0), 0)))
5267           == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5268       && ! side_effects_p (src))
5269     {
5270       rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5271                       ? XEXP (src, 1) : XEXP (src, 2));
5272       rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5273                    ? XEXP (src, 2) : XEXP (src, 1));
5274       rtx term1 = const0_rtx, term2, term3;
5275
5276       if (GET_CODE (true_rtx) == IOR
5277           && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5278         term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5279       else if (GET_CODE (true_rtx) == IOR
5280                && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5281         term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5282       else if (GET_CODE (false_rtx) == IOR
5283                && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5284         term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5285       else if (GET_CODE (false_rtx) == IOR
5286                && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5287         term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5288
5289       term2 = simplify_gen_binary (AND, GET_MODE (src),
5290                                    XEXP (XEXP (src, 0), 0), true_rtx);
5291       term3 = simplify_gen_binary (AND, GET_MODE (src),
5292                                    simplify_gen_unary (NOT, GET_MODE (src),
5293                                                        XEXP (XEXP (src, 0), 0),
5294                                                        GET_MODE (src)),
5295                                    false_rtx);
5296
5297       SUBST (SET_SRC (x),
5298              simplify_gen_binary (IOR, GET_MODE (src),
5299                                   simplify_gen_binary (IOR, GET_MODE (src),
5300                                                        term1, term2),
5301                                   term3));
5302
5303       src = SET_SRC (x);
5304     }
5305
5306   /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5307      whole thing fail.  */
5308   if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5309     return src;
5310   else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5311     return dest;
5312   else
5313     /* Convert this into a field assignment operation, if possible.  */
5314     return make_field_assignment (x);
5315 }
5316 \f
5317 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5318    result.  */
5319
5320 static rtx
5321 simplify_logical (rtx x)
5322 {
5323   enum machine_mode mode = GET_MODE (x);
5324   rtx op0 = XEXP (x, 0);
5325   rtx op1 = XEXP (x, 1);
5326
5327   switch (GET_CODE (x))
5328     {
5329     case AND:
5330       /* We can call simplify_and_const_int only if we don't lose
5331          any (sign) bits when converting INTVAL (op1) to
5332          "unsigned HOST_WIDE_INT".  */
5333       if (GET_CODE (op1) == CONST_INT
5334           && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5335               || INTVAL (op1) > 0))
5336         {
5337           x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5338           if (GET_CODE (x) != AND)
5339             return x;
5340
5341           op0 = XEXP (x, 0);
5342           op1 = XEXP (x, 1);
5343         }
5344
5345       /* If we have any of (and (ior A B) C) or (and (xor A B) C),
5346          apply the distributive law and then the inverse distributive
5347          law to see if things simplify.  */
5348       if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5349         {
5350           rtx result = distribute_and_simplify_rtx (x, 0);
5351           if (result)
5352             return result;
5353         }
5354       if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5355         {
5356           rtx result = distribute_and_simplify_rtx (x, 1);
5357           if (result)
5358             return result;
5359         }
5360       break;
5361
5362     case IOR:
5363       /* If we have (ior (and A B) C), apply the distributive law and then
5364          the inverse distributive law to see if things simplify.  */
5365
5366       if (GET_CODE (op0) == AND)
5367         {
5368           rtx result = distribute_and_simplify_rtx (x, 0);
5369           if (result)
5370             return result;
5371         }
5372
5373       if (GET_CODE (op1) == AND)
5374         {
5375           rtx result = distribute_and_simplify_rtx (x, 1);
5376           if (result)
5377             return result;
5378         }
5379       break;
5380
5381     default:
5382       gcc_unreachable ();
5383     }
5384
5385   return x;
5386 }
5387 \f
5388 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5389    operations" because they can be replaced with two more basic operations.
5390    ZERO_EXTEND is also considered "compound" because it can be replaced with
5391    an AND operation, which is simpler, though only one operation.
5392
5393    The function expand_compound_operation is called with an rtx expression
5394    and will convert it to the appropriate shifts and AND operations,
5395    simplifying at each stage.
5396
5397    The function make_compound_operation is called to convert an expression
5398    consisting of shifts and ANDs into the equivalent compound expression.
5399    It is the inverse of this function, loosely speaking.  */
5400
5401 static rtx
5402 expand_compound_operation (rtx x)
5403 {
5404   unsigned HOST_WIDE_INT pos = 0, len;
5405   int unsignedp = 0;
5406   unsigned int modewidth;
5407   rtx tem;
5408
5409   switch (GET_CODE (x))
5410     {
5411     case ZERO_EXTEND:
5412       unsignedp = 1;
5413     case SIGN_EXTEND:
5414       /* We can't necessarily use a const_int for a multiword mode;
5415          it depends on implicitly extending the value.
5416          Since we don't know the right way to extend it,
5417          we can't tell whether the implicit way is right.
5418
5419          Even for a mode that is no wider than a const_int,
5420          we can't win, because we need to sign extend one of its bits through
5421          the rest of it, and we don't know which bit.  */
5422       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5423         return x;
5424
5425       /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5426          (zero_extend:MODE FROM) or (sign_extend:MODE FROM).  It is for any MEM
5427          because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5428          reloaded. If not for that, MEM's would very rarely be safe.
5429
5430          Reject MODEs bigger than a word, because we might not be able
5431          to reference a two-register group starting with an arbitrary register
5432          (and currently gen_lowpart might crash for a SUBREG).  */
5433
5434       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5435         return x;
5436
5437       /* Reject MODEs that aren't scalar integers because turning vector
5438          or complex modes into shifts causes problems.  */
5439
5440       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5441         return x;
5442
5443       len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5444       /* If the inner object has VOIDmode (the only way this can happen
5445          is if it is an ASM_OPERANDS), we can't do anything since we don't
5446          know how much masking to do.  */
5447       if (len == 0)
5448         return x;
5449
5450       break;
5451
5452     case ZERO_EXTRACT:
5453       unsignedp = 1;
5454
5455       /* ... fall through ...  */
5456
5457     case SIGN_EXTRACT:
5458       /* If the operand is a CLOBBER, just return it.  */
5459       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5460         return XEXP (x, 0);
5461
5462       if (GET_CODE (XEXP (x, 1)) != CONST_INT
5463           || GET_CODE (XEXP (x, 2)) != CONST_INT
5464           || GET_MODE (XEXP (x, 0)) == VOIDmode)
5465         return x;
5466
5467       /* Reject MODEs that aren't scalar integers because turning vector
5468          or complex modes into shifts causes problems.  */
5469
5470       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5471         return x;
5472
5473       len = INTVAL (XEXP (x, 1));
5474       pos = INTVAL (XEXP (x, 2));
5475
5476       /* If this goes outside the object being extracted, replace the object
5477          with a (use (mem ...)) construct that only combine understands
5478          and is used only for this purpose.  */
5479       if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5480         SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5481
5482       if (BITS_BIG_ENDIAN)
5483         pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5484
5485       break;
5486
5487     default:
5488       return x;
5489     }
5490   /* Convert sign extension to zero extension, if we know that the high
5491      bit is not set, as this is easier to optimize.  It will be converted
5492      back to cheaper alternative in make_extraction.  */
5493   if (GET_CODE (x) == SIGN_EXTEND
5494       && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5495           && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
5496                 & ~(((unsigned HOST_WIDE_INT)
5497                       GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5498                      >> 1))
5499                == 0)))
5500     {
5501       rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
5502       rtx temp2 = expand_compound_operation (temp);
5503
5504       /* Make sure this is a profitable operation.  */
5505       if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
5506        return temp2;
5507       else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
5508        return temp;
5509       else
5510        return x;
5511     }
5512
5513   /* We can optimize some special cases of ZERO_EXTEND.  */
5514   if (GET_CODE (x) == ZERO_EXTEND)
5515     {
5516       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5517          know that the last value didn't have any inappropriate bits
5518          set.  */
5519       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5520           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5521           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5522           && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5523               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5524         return XEXP (XEXP (x, 0), 0);
5525
5526       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
5527       if (GET_CODE (XEXP (x, 0)) == SUBREG
5528           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5529           && subreg_lowpart_p (XEXP (x, 0))
5530           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5531           && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5532               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5533         return SUBREG_REG (XEXP (x, 0));
5534
5535       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5536          is a comparison and STORE_FLAG_VALUE permits.  This is like
5537          the first case, but it works even when GET_MODE (x) is larger
5538          than HOST_WIDE_INT.  */
5539       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5540           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5541           && COMPARISON_P (XEXP (XEXP (x, 0), 0))
5542           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5543               <= HOST_BITS_PER_WIDE_INT)
5544           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5545               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5546         return XEXP (XEXP (x, 0), 0);
5547
5548       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
5549       if (GET_CODE (XEXP (x, 0)) == SUBREG
5550           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5551           && subreg_lowpart_p (XEXP (x, 0))
5552           && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
5553           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5554               <= HOST_BITS_PER_WIDE_INT)
5555           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5556               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5557         return SUBREG_REG (XEXP (x, 0));
5558
5559     }
5560
5561   /* If we reach here, we want to return a pair of shifts.  The inner
5562      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
5563      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
5564      logical depending on the value of UNSIGNEDP.
5565
5566      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5567      converted into an AND of a shift.
5568
5569      We must check for the case where the left shift would have a negative
5570      count.  This can happen in a case like (x >> 31) & 255 on machines
5571      that can't shift by a constant.  On those machines, we would first
5572      combine the shift with the AND to produce a variable-position
5573      extraction.  Then the constant of 31 would be substituted in to produce
5574      a such a position.  */
5575
5576   modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5577   if (modewidth + len >= pos)
5578     tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5579                                 GET_MODE (x),
5580                                 simplify_shift_const (NULL_RTX, ASHIFT,
5581                                                       GET_MODE (x),
5582                                                       XEXP (x, 0),
5583                                                       modewidth - pos - len),
5584                                 modewidth - len);
5585
5586   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5587     tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5588                                   simplify_shift_const (NULL_RTX, LSHIFTRT,
5589                                                         GET_MODE (x),
5590                                                         XEXP (x, 0), pos),
5591                                   ((HOST_WIDE_INT) 1 << len) - 1);
5592   else
5593     /* Any other cases we can't handle.  */
5594     return x;
5595
5596   /* If we couldn't do this for some reason, return the original
5597      expression.  */
5598   if (GET_CODE (tem) == CLOBBER)
5599     return x;
5600
5601   return tem;
5602 }
5603 \f
5604 /* X is a SET which contains an assignment of one object into
5605    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5606    or certain SUBREGS). If possible, convert it into a series of
5607    logical operations.
5608
5609    We half-heartedly support variable positions, but do not at all
5610    support variable lengths.  */
5611
5612 static rtx
5613 expand_field_assignment (rtx x)
5614 {
5615   rtx inner;
5616   rtx pos;                      /* Always counts from low bit.  */
5617   int len;
5618   rtx mask, cleared, masked;
5619   enum machine_mode compute_mode;
5620
5621   /* Loop until we find something we can't simplify.  */
5622   while (1)
5623     {
5624       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5625           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5626         {
5627           inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5628           len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5629           pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
5630         }
5631       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5632                && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5633         {
5634           inner = XEXP (SET_DEST (x), 0);
5635           len = INTVAL (XEXP (SET_DEST (x), 1));
5636           pos = XEXP (SET_DEST (x), 2);
5637
5638           /* If the position is constant and spans the width of INNER,
5639              surround INNER  with a USE to indicate this.  */
5640           if (GET_CODE (pos) == CONST_INT
5641               && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5642             inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
5643
5644           if (BITS_BIG_ENDIAN)
5645             {
5646               if (GET_CODE (pos) == CONST_INT)
5647                 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5648                                - INTVAL (pos));
5649               else if (GET_CODE (pos) == MINUS
5650                        && GET_CODE (XEXP (pos, 1)) == CONST_INT
5651                        && (INTVAL (XEXP (pos, 1))
5652                            == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5653                 /* If position is ADJUST - X, new position is X.  */
5654                 pos = XEXP (pos, 0);
5655               else
5656                 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
5657                                            GEN_INT (GET_MODE_BITSIZE (
5658                                                     GET_MODE (inner))
5659                                                     - len),
5660                                            pos);
5661             }
5662         }
5663
5664       /* A SUBREG between two modes that occupy the same numbers of words
5665          can be done by moving the SUBREG to the source.  */
5666       else if (GET_CODE (SET_DEST (x)) == SUBREG
5667                /* We need SUBREGs to compute nonzero_bits properly.  */
5668                && nonzero_sign_valid
5669                && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5670                      + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5671                    == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5672                         + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5673         {
5674           x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
5675                            gen_lowpart
5676                            (GET_MODE (SUBREG_REG (SET_DEST (x))),
5677                             SET_SRC (x)));
5678           continue;
5679         }
5680       else
5681         break;
5682
5683       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5684         inner = SUBREG_REG (inner);
5685
5686       compute_mode = GET_MODE (inner);
5687
5688       /* Don't attempt bitwise arithmetic on non scalar integer modes.  */
5689       if (! SCALAR_INT_MODE_P (compute_mode))
5690         {
5691           enum machine_mode imode;
5692
5693           /* Don't do anything for vector or complex integral types.  */
5694           if (! FLOAT_MODE_P (compute_mode))
5695             break;
5696
5697           /* Try to find an integral mode to pun with.  */
5698           imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
5699           if (imode == BLKmode)
5700             break;
5701
5702           compute_mode = imode;
5703           inner = gen_lowpart (imode, inner);
5704         }
5705
5706       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
5707       if (len >= HOST_BITS_PER_WIDE_INT)
5708         break;
5709
5710       /* Now compute the equivalent expression.  Make a copy of INNER
5711          for the SET_DEST in case it is a MEM into which we will substitute;
5712          we don't want shared RTL in that case.  */
5713       mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
5714       cleared = simplify_gen_binary (AND, compute_mode,
5715                                      simplify_gen_unary (NOT, compute_mode,
5716                                        simplify_gen_binary (ASHIFT,
5717                                                             compute_mode,
5718                                                             mask, pos),
5719                                        compute_mode),
5720                                      inner);
5721       masked = simplify_gen_binary (ASHIFT, compute_mode,
5722                                     simplify_gen_binary (
5723                                       AND, compute_mode,
5724                                       gen_lowpart (compute_mode, SET_SRC (x)),
5725                                       mask),
5726                                     pos);
5727
5728       x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
5729                        simplify_gen_binary (IOR, compute_mode,
5730                                             cleared, masked));
5731     }
5732
5733   return x;
5734 }
5735 \f
5736 /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
5737    it is an RTX that represents a variable starting position; otherwise,
5738    POS is the (constant) starting bit position (counted from the LSB).
5739
5740    INNER may be a USE.  This will occur when we started with a bitfield
5741    that went outside the boundary of the object in memory, which is
5742    allowed on most machines.  To isolate this case, we produce a USE
5743    whose mode is wide enough and surround the MEM with it.  The only
5744    code that understands the USE is this routine.  If it is not removed,
5745    it will cause the resulting insn not to match.
5746
5747    UNSIGNEDP is nonzero for an unsigned reference and zero for a
5748    signed reference.
5749
5750    IN_DEST is nonzero if this is a reference in the destination of a
5751    SET.  This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If nonzero,
5752    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
5753    be used.
5754
5755    IN_COMPARE is nonzero if we are in a COMPARE.  This means that a
5756    ZERO_EXTRACT should be built even for bits starting at bit 0.
5757
5758    MODE is the desired mode of the result (if IN_DEST == 0).
5759
5760    The result is an RTX for the extraction or NULL_RTX if the target
5761    can't handle it.  */
5762
5763 static rtx
5764 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
5765                  rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
5766                  int in_dest, int in_compare)
5767 {
5768   /* This mode describes the size of the storage area
5769      to fetch the overall value from.  Within that, we
5770      ignore the POS lowest bits, etc.  */
5771   enum machine_mode is_mode = GET_MODE (inner);
5772   enum machine_mode inner_mode;
5773   enum machine_mode wanted_inner_mode = byte_mode;
5774   enum machine_mode wanted_inner_reg_mode = word_mode;
5775   enum machine_mode pos_mode = word_mode;
5776   enum machine_mode extraction_mode = word_mode;
5777   enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
5778   int spans_byte = 0;
5779   rtx new = 0;
5780   rtx orig_pos_rtx = pos_rtx;
5781   HOST_WIDE_INT orig_pos;
5782
5783   /* Get some information about INNER and get the innermost object.  */
5784   if (GET_CODE (inner) == USE)
5785     /* (use:SI (mem:QI foo)) stands for (mem:SI foo).  */
5786     /* We don't need to adjust the position because we set up the USE
5787        to pretend that it was a full-word object.  */
5788     spans_byte = 1, inner = XEXP (inner, 0);
5789   else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5790     {
5791       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
5792          consider just the QI as the memory to extract from.
5793          The subreg adds or removes high bits; its mode is
5794          irrelevant to the meaning of this extraction,
5795          since POS and LEN count from the lsb.  */
5796       if (MEM_P (SUBREG_REG (inner)))
5797         is_mode = GET_MODE (SUBREG_REG (inner));
5798       inner = SUBREG_REG (inner);
5799     }
5800   else if (GET_CODE (inner) == ASHIFT
5801            && GET_CODE (XEXP (inner, 1)) == CONST_INT
5802            && pos_rtx == 0 && pos == 0
5803            && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
5804     {
5805       /* We're extracting the least significant bits of an rtx
5806          (ashift X (const_int C)), where LEN > C.  Extract the
5807          least significant (LEN - C) bits of X, giving an rtx
5808          whose mode is MODE, then shift it left C times.  */
5809       new = make_extraction (mode, XEXP (inner, 0),
5810                              0, 0, len - INTVAL (XEXP (inner, 1)),
5811                              unsignedp, in_dest, in_compare);
5812       if (new != 0)
5813         return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
5814     }
5815
5816   inner_mode = GET_MODE (inner);
5817
5818   if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
5819     pos = INTVAL (pos_rtx), pos_rtx = 0;
5820
5821   /* See if this can be done without an extraction.  We never can if the
5822      width of the field is not the same as that of some integer mode. For
5823      registers, we can only avoid the extraction if the position is at the
5824      low-order bit and this is either not in the destination or we have the
5825      appropriate STRICT_LOW_PART operation available.
5826
5827      For MEM, we can avoid an extract if the field starts on an appropriate
5828      boundary and we can change the mode of the memory reference.  However,
5829      we cannot directly access the MEM if we have a USE and the underlying
5830      MEM is not TMODE.  This combination means that MEM was being used in a
5831      context where bits outside its mode were being referenced; that is only
5832      valid in bit-field insns.  */
5833
5834   if (tmode != BLKmode
5835       && ! (spans_byte && inner_mode != tmode)
5836       && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
5837            && !MEM_P (inner)
5838            && (! in_dest
5839                || (REG_P (inner)
5840                    && have_insn_for (STRICT_LOW_PART, tmode))))
5841           || (MEM_P (inner) && pos_rtx == 0
5842               && (pos
5843                   % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
5844                      : BITS_PER_UNIT)) == 0
5845               /* We can't do this if we are widening INNER_MODE (it
5846                  may not be aligned, for one thing).  */
5847               && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
5848               && (inner_mode == tmode
5849                   || (! mode_dependent_address_p (XEXP (inner, 0))
5850                       && ! MEM_VOLATILE_P (inner))))))
5851     {
5852       /* If INNER is a MEM, make a new MEM that encompasses just the desired
5853          field.  If the original and current mode are the same, we need not
5854          adjust the offset.  Otherwise, we do if bytes big endian.
5855
5856          If INNER is not a MEM, get a piece consisting of just the field
5857          of interest (in this case POS % BITS_PER_WORD must be 0).  */
5858
5859       if (MEM_P (inner))
5860         {
5861           HOST_WIDE_INT offset;
5862
5863           /* POS counts from lsb, but make OFFSET count in memory order.  */
5864           if (BYTES_BIG_ENDIAN)
5865             offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
5866           else
5867             offset = pos / BITS_PER_UNIT;
5868
5869           new = adjust_address_nv (inner, tmode, offset);
5870         }
5871       else if (REG_P (inner))
5872         {
5873           if (tmode != inner_mode)
5874             {
5875               /* We can't call gen_lowpart in a DEST since we
5876                  always want a SUBREG (see below) and it would sometimes
5877                  return a new hard register.  */
5878               if (pos || in_dest)
5879                 {
5880                   HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
5881
5882                   if (WORDS_BIG_ENDIAN
5883                       && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
5884                     final_word = ((GET_MODE_SIZE (inner_mode)
5885                                    - GET_MODE_SIZE (tmode))
5886                                   / UNITS_PER_WORD) - final_word;
5887
5888                   final_word *= UNITS_PER_WORD;
5889                   if (BYTES_BIG_ENDIAN &&
5890                       GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
5891                     final_word += (GET_MODE_SIZE (inner_mode)
5892                                    - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
5893
5894                   /* Avoid creating invalid subregs, for example when
5895                      simplifying (x>>32)&255.  */
5896                   if (!validate_subreg (tmode, inner_mode, inner, final_word))
5897                     return NULL_RTX;
5898
5899                   new = gen_rtx_SUBREG (tmode, inner, final_word);
5900                 }
5901               else
5902                 new = gen_lowpart (tmode, inner);
5903             }
5904           else
5905             new = inner;
5906         }
5907       else
5908         new = force_to_mode (inner, tmode,
5909                              len >= HOST_BITS_PER_WIDE_INT
5910                              ? ~(unsigned HOST_WIDE_INT) 0
5911                              : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
5912                              0);
5913
5914       /* If this extraction is going into the destination of a SET,
5915          make a STRICT_LOW_PART unless we made a MEM.  */
5916
5917       if (in_dest)
5918         return (MEM_P (new) ? new
5919                 : (GET_CODE (new) != SUBREG
5920                    ? gen_rtx_CLOBBER (tmode, const0_rtx)
5921                    : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
5922
5923       if (mode == tmode)
5924         return new;
5925
5926       if (GET_CODE (new) == CONST_INT)
5927         return gen_int_mode (INTVAL (new), mode);
5928
5929       /* If we know that no extraneous bits are set, and that the high
5930          bit is not set, convert the extraction to the cheaper of
5931          sign and zero extension, that are equivalent in these cases.  */
5932       if (flag_expensive_optimizations
5933           && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
5934               && ((nonzero_bits (new, tmode)
5935                    & ~(((unsigned HOST_WIDE_INT)
5936                         GET_MODE_MASK (tmode))
5937                        >> 1))
5938                   == 0)))
5939         {
5940           rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
5941           rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
5942
5943           /* Prefer ZERO_EXTENSION, since it gives more information to
5944              backends.  */
5945           if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
5946             return temp;
5947           return temp1;
5948         }
5949
5950       /* Otherwise, sign- or zero-extend unless we already are in the
5951          proper mode.  */
5952
5953       return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
5954                              mode, new));
5955     }
5956
5957   /* Unless this is a COMPARE or we have a funny memory reference,
5958      don't do anything with zero-extending field extracts starting at
5959      the low-order bit since they are simple AND operations.  */
5960   if (pos_rtx == 0 && pos == 0 && ! in_dest
5961       && ! in_compare && ! spans_byte && unsignedp)
5962     return 0;
5963
5964   /* Unless we are allowed to span bytes or INNER is not MEM, reject this if
5965      we would be spanning bytes or if the position is not a constant and the
5966      length is not 1.  In all other cases, we would only be going outside
5967      our object in cases when an original shift would have been
5968      undefined.  */
5969   if (! spans_byte && MEM_P (inner)
5970       && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
5971           || (pos_rtx != 0 && len != 1)))
5972     return 0;
5973
5974   /* Get the mode to use should INNER not be a MEM, the mode for the position,
5975      and the mode for the result.  */
5976   if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
5977     {
5978       wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
5979       pos_mode = mode_for_extraction (EP_insv, 2);
5980       extraction_mode = mode_for_extraction (EP_insv, 3);
5981     }
5982
5983   if (! in_dest && unsignedp
5984       && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
5985     {
5986       wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
5987       pos_mode = mode_for_extraction (EP_extzv, 3);
5988       extraction_mode = mode_for_extraction (EP_extzv, 0);
5989     }
5990
5991   if (! in_dest && ! unsignedp
5992       && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
5993     {
5994       wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
5995       pos_mode = mode_for_extraction (EP_extv, 3);
5996       extraction_mode = mode_for_extraction (EP_extv, 0);
5997     }
5998
5999   /* Never narrow an object, since that might not be safe.  */
6000
6001   if (mode != VOIDmode
6002       && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6003     extraction_mode = mode;
6004
6005   if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6006       && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6007     pos_mode = GET_MODE (pos_rtx);
6008
6009   /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
6010      if we have to change the mode of memory and cannot, the desired mode is
6011      EXTRACTION_MODE.  */
6012   if (!MEM_P (inner))
6013     wanted_inner_mode = wanted_inner_reg_mode;
6014   else if (inner_mode != wanted_inner_mode
6015            && (mode_dependent_address_p (XEXP (inner, 0))
6016                || MEM_VOLATILE_P (inner)))
6017     wanted_inner_mode = extraction_mode;
6018
6019   orig_pos = pos;
6020
6021   if (BITS_BIG_ENDIAN)
6022     {
6023       /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6024          BITS_BIG_ENDIAN style.  If position is constant, compute new
6025          position.  Otherwise, build subtraction.
6026          Note that POS is relative to the mode of the original argument.
6027          If it's a MEM we need to recompute POS relative to that.
6028          However, if we're extracting from (or inserting into) a register,
6029          we want to recompute POS relative to wanted_inner_mode.  */
6030       int width = (MEM_P (inner)
6031                    ? GET_MODE_BITSIZE (is_mode)
6032                    : GET_MODE_BITSIZE (wanted_inner_mode));
6033
6034       if (pos_rtx == 0)
6035         pos = width - len - pos;
6036       else
6037         pos_rtx
6038           = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6039       /* POS may be less than 0 now, but we check for that below.
6040          Note that it can only be less than 0 if !MEM_P (inner).  */
6041     }
6042
6043   /* If INNER has a wider mode, make it smaller.  If this is a constant
6044      extract, try to adjust the byte to point to the byte containing
6045      the value.  */
6046   if (wanted_inner_mode != VOIDmode
6047       && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6048       && ((MEM_P (inner)
6049            && (inner_mode == wanted_inner_mode
6050                || (! mode_dependent_address_p (XEXP (inner, 0))
6051                    && ! MEM_VOLATILE_P (inner))))))
6052     {
6053       int offset = 0;
6054
6055       /* The computations below will be correct if the machine is big
6056          endian in both bits and bytes or little endian in bits and bytes.
6057          If it is mixed, we must adjust.  */
6058
6059       /* If bytes are big endian and we had a paradoxical SUBREG, we must
6060          adjust OFFSET to compensate.  */
6061       if (BYTES_BIG_ENDIAN
6062           && ! spans_byte
6063           && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6064         offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6065
6066       /* If this is a constant position, we can move to the desired byte.
6067          Be careful not to go beyond the original object and maintain the
6068          natural alignment of the memory.  */ 
6069       if (pos_rtx == 0)
6070         {
6071           enum machine_mode bfmode = smallest_mode_for_size (len, MODE_INT);
6072           offset += (pos / GET_MODE_BITSIZE (bfmode)) * GET_MODE_SIZE (bfmode);
6073           pos %= GET_MODE_BITSIZE (bfmode);
6074         }
6075
6076       if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6077           && ! spans_byte
6078           && is_mode != wanted_inner_mode)
6079         offset = (GET_MODE_SIZE (is_mode)
6080                   - GET_MODE_SIZE (wanted_inner_mode) - offset);
6081
6082       if (offset != 0 || inner_mode != wanted_inner_mode)
6083         inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6084     }
6085
6086   /* If INNER is not memory, we can always get it into the proper mode.  If we
6087      are changing its mode, POS must be a constant and smaller than the size
6088      of the new mode.  */
6089   else if (!MEM_P (inner))
6090     {
6091       if (GET_MODE (inner) != wanted_inner_mode
6092           && (pos_rtx != 0
6093               || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6094         return 0;
6095
6096       inner = force_to_mode (inner, wanted_inner_mode,
6097                              pos_rtx
6098                              || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6099                              ? ~(unsigned HOST_WIDE_INT) 0
6100                              : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6101                                 << orig_pos),
6102                              0);
6103     }
6104
6105   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
6106      have to zero extend.  Otherwise, we can just use a SUBREG.  */
6107   if (pos_rtx != 0
6108       && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6109     {
6110       rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6111
6112       /* If we know that no extraneous bits are set, and that the high
6113          bit is not set, convert extraction to cheaper one - either
6114          SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6115          cases.  */
6116       if (flag_expensive_optimizations
6117           && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6118               && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6119                    & ~(((unsigned HOST_WIDE_INT)
6120                         GET_MODE_MASK (GET_MODE (pos_rtx)))
6121                        >> 1))
6122                   == 0)))
6123         {
6124           rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6125
6126           /* Prefer ZERO_EXTENSION, since it gives more information to
6127              backends.  */
6128           if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6129             temp = temp1;
6130         }
6131       pos_rtx = temp;
6132     }
6133   else if (pos_rtx != 0
6134            && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6135     pos_rtx = gen_lowpart (pos_mode, pos_rtx);
6136
6137   /* Make POS_RTX unless we already have it and it is correct.  If we don't
6138      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6139      be a CONST_INT.  */
6140   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6141     pos_rtx = orig_pos_rtx;
6142
6143   else if (pos_rtx == 0)
6144     pos_rtx = GEN_INT (pos);
6145
6146   /* Make the required operation.  See if we can use existing rtx.  */
6147   new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6148                          extraction_mode, inner, GEN_INT (len), pos_rtx);
6149   if (! in_dest)
6150     new = gen_lowpart (mode, new);
6151
6152   return new;
6153 }
6154 \f
6155 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6156    with any other operations in X.  Return X without that shift if so.  */
6157
6158 static rtx
6159 extract_left_shift (rtx x, int count)
6160 {
6161   enum rtx_code code = GET_CODE (x);
6162   enum machine_mode mode = GET_MODE (x);
6163   rtx tem;
6164
6165   switch (code)
6166     {
6167     case ASHIFT:
6168       /* This is the shift itself.  If it is wide enough, we will return
6169          either the value being shifted if the shift count is equal to
6170          COUNT or a shift for the difference.  */
6171       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6172           && INTVAL (XEXP (x, 1)) >= count)
6173         return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6174                                      INTVAL (XEXP (x, 1)) - count);
6175       break;
6176
6177     case NEG:  case NOT:
6178       if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6179         return simplify_gen_unary (code, mode, tem, mode);
6180
6181       break;
6182
6183     case PLUS:  case IOR:  case XOR:  case AND:
6184       /* If we can safely shift this constant and we find the inner shift,
6185          make a new operation.  */
6186       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6187           && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6188           && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6189         return simplify_gen_binary (code, mode, tem,
6190                                     GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6191
6192       break;
6193
6194     default:
6195       break;
6196     }
6197
6198   return 0;
6199 }
6200 \f
6201 /* Look at the expression rooted at X.  Look for expressions
6202    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6203    Form these expressions.
6204
6205    Return the new rtx, usually just X.
6206
6207    Also, for machines like the VAX that don't have logical shift insns,
6208    try to convert logical to arithmetic shift operations in cases where
6209    they are equivalent.  This undoes the canonicalizations to logical
6210    shifts done elsewhere.
6211
6212    We try, as much as possible, to re-use rtl expressions to save memory.
6213
6214    IN_CODE says what kind of expression we are processing.  Normally, it is
6215    SET.  In a memory address (inside a MEM, PLUS or minus, the latter two
6216    being kludges), it is MEM.  When processing the arguments of a comparison
6217    or a COMPARE against zero, it is COMPARE.  */
6218
6219 static rtx
6220 make_compound_operation (rtx x, enum rtx_code in_code)
6221 {
6222   enum rtx_code code = GET_CODE (x);
6223   enum machine_mode mode = GET_MODE (x);
6224   int mode_width = GET_MODE_BITSIZE (mode);
6225   rtx rhs, lhs;
6226   enum rtx_code next_code;
6227   int i;
6228   rtx new = 0;
6229   rtx tem;
6230   const char *fmt;
6231
6232   /* Select the code to be used in recursive calls.  Once we are inside an
6233      address, we stay there.  If we have a comparison, set to COMPARE,
6234      but once inside, go back to our default of SET.  */
6235
6236   next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6237                : ((code == COMPARE || COMPARISON_P (x))
6238                   && XEXP (x, 1) == const0_rtx) ? COMPARE
6239                : in_code == COMPARE ? SET : in_code);
6240
6241   /* Process depending on the code of this operation.  If NEW is set
6242      nonzero, it will be returned.  */
6243
6244   switch (code)
6245     {
6246     case ASHIFT:
6247       /* Convert shifts by constants into multiplications if inside
6248          an address.  */
6249       if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6250           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6251           && INTVAL (XEXP (x, 1)) >= 0)
6252         {
6253           new = make_compound_operation (XEXP (x, 0), next_code);
6254           new = gen_rtx_MULT (mode, new,
6255                               GEN_INT ((HOST_WIDE_INT) 1
6256                                        << INTVAL (XEXP (x, 1))));
6257         }
6258       break;
6259
6260     case AND:
6261       /* If the second operand is not a constant, we can't do anything
6262          with it.  */
6263       if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6264         break;
6265
6266       /* If the constant is a power of two minus one and the first operand
6267          is a logical right shift, make an extraction.  */
6268       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6269           && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6270         {
6271           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6272           new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6273                                  0, in_code == COMPARE);
6274         }
6275
6276       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
6277       else if (GET_CODE (XEXP (x, 0)) == SUBREG
6278                && subreg_lowpart_p (XEXP (x, 0))
6279                && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6280                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6281         {
6282           new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6283                                          next_code);
6284           new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6285                                  XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6286                                  0, in_code == COMPARE);
6287         }
6288       /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)).  */
6289       else if ((GET_CODE (XEXP (x, 0)) == XOR
6290                 || GET_CODE (XEXP (x, 0)) == IOR)
6291                && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6292                && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6293                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6294         {
6295           /* Apply the distributive law, and then try to make extractions.  */
6296           new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6297                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6298                                              XEXP (x, 1)),
6299                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6300                                              XEXP (x, 1)));
6301           new = make_compound_operation (new, in_code);
6302         }
6303
6304       /* If we are have (and (rotate X C) M) and C is larger than the number
6305          of bits in M, this is an extraction.  */
6306
6307       else if (GET_CODE (XEXP (x, 0)) == ROTATE
6308                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6309                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6310                && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6311         {
6312           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6313           new = make_extraction (mode, new,
6314                                  (GET_MODE_BITSIZE (mode)
6315                                   - INTVAL (XEXP (XEXP (x, 0), 1))),
6316                                  NULL_RTX, i, 1, 0, in_code == COMPARE);
6317         }
6318
6319       /* On machines without logical shifts, if the operand of the AND is
6320          a logical shift and our mask turns off all the propagated sign
6321          bits, we can replace the logical shift with an arithmetic shift.  */
6322       else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6323                && !have_insn_for (LSHIFTRT, mode)
6324                && have_insn_for (ASHIFTRT, mode)
6325                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6326                && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6327                && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6328                && mode_width <= HOST_BITS_PER_WIDE_INT)
6329         {
6330           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6331
6332           mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6333           if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6334             SUBST (XEXP (x, 0),
6335                    gen_rtx_ASHIFTRT (mode,
6336                                      make_compound_operation
6337                                      (XEXP (XEXP (x, 0), 0), next_code),
6338                                      XEXP (XEXP (x, 0), 1)));
6339         }
6340
6341       /* If the constant is one less than a power of two, this might be
6342          representable by an extraction even if no shift is present.
6343          If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6344          we are in a COMPARE.  */
6345       else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6346         new = make_extraction (mode,
6347                                make_compound_operation (XEXP (x, 0),
6348                                                         next_code),
6349                                0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6350
6351       /* If we are in a comparison and this is an AND with a power of two,
6352          convert this into the appropriate bit extract.  */
6353       else if (in_code == COMPARE
6354                && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6355         new = make_extraction (mode,
6356                                make_compound_operation (XEXP (x, 0),
6357                                                         next_code),
6358                                i, NULL_RTX, 1, 1, 0, 1);
6359
6360       break;
6361
6362     case LSHIFTRT:
6363       /* If the sign bit is known to be zero, replace this with an
6364          arithmetic shift.  */
6365       if (have_insn_for (ASHIFTRT, mode)
6366           && ! have_insn_for (LSHIFTRT, mode)
6367           && mode_width <= HOST_BITS_PER_WIDE_INT
6368           && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6369         {
6370           new = gen_rtx_ASHIFTRT (mode,
6371                                   make_compound_operation (XEXP (x, 0),
6372                                                            next_code),
6373                                   XEXP (x, 1));
6374           break;
6375         }
6376
6377       /* ... fall through ...  */
6378
6379     case ASHIFTRT:
6380       lhs = XEXP (x, 0);
6381       rhs = XEXP (x, 1);
6382
6383       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6384          this is a SIGN_EXTRACT.  */
6385       if (GET_CODE (rhs) == CONST_INT
6386           && GET_CODE (lhs) == ASHIFT
6387           && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6388           && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6389         {
6390           new = make_compound_operation (XEXP (lhs, 0), next_code);
6391           new = make_extraction (mode, new,
6392                                  INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6393                                  NULL_RTX, mode_width - INTVAL (rhs),
6394                                  code == LSHIFTRT, 0, in_code == COMPARE);
6395           break;
6396         }
6397
6398       /* See if we have operations between an ASHIFTRT and an ASHIFT.
6399          If so, try to merge the shifts into a SIGN_EXTEND.  We could
6400          also do this for some cases of SIGN_EXTRACT, but it doesn't
6401          seem worth the effort; the case checked for occurs on Alpha.  */
6402
6403       if (!OBJECT_P (lhs)
6404           && ! (GET_CODE (lhs) == SUBREG
6405                 && (OBJECT_P (SUBREG_REG (lhs))))
6406           && GET_CODE (rhs) == CONST_INT
6407           && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6408           && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6409         new = make_extraction (mode, make_compound_operation (new, next_code),
6410                                0, NULL_RTX, mode_width - INTVAL (rhs),
6411                                code == LSHIFTRT, 0, in_code == COMPARE);
6412
6413       break;
6414
6415     case SUBREG:
6416       /* Call ourselves recursively on the inner expression.  If we are
6417          narrowing the object and it has a different RTL code from
6418          what it originally did, do this SUBREG as a force_to_mode.  */
6419
6420       tem = make_compound_operation (SUBREG_REG (x), in_code);
6421
6422       {
6423         rtx simplified;
6424         simplified = simplify_subreg (GET_MODE (x), tem, GET_MODE (tem),
6425                                       SUBREG_BYTE (x));
6426
6427         if (simplified)
6428           tem = simplified;
6429
6430         if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6431             && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6432             && subreg_lowpart_p (x))
6433           {
6434             rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
6435                                        0);
6436             
6437             /* If we have something other than a SUBREG, we might have
6438                done an expansion, so rerun ourselves.  */
6439             if (GET_CODE (newer) != SUBREG)
6440               newer = make_compound_operation (newer, in_code);
6441             
6442             return newer;
6443           }
6444
6445         if (simplified)
6446           return tem;
6447       }
6448       break;
6449
6450     default:
6451       break;
6452     }
6453
6454   if (new)
6455     {
6456       x = gen_lowpart (mode, new);
6457       code = GET_CODE (x);
6458     }
6459
6460   /* Now recursively process each operand of this operation.  */
6461   fmt = GET_RTX_FORMAT (code);
6462   for (i = 0; i < GET_RTX_LENGTH (code); i++)
6463     if (fmt[i] == 'e')
6464       {
6465         new = make_compound_operation (XEXP (x, i), next_code);
6466         SUBST (XEXP (x, i), new);
6467       }
6468
6469   /* If this is a commutative operation, the changes to the operands
6470      may have made it noncanonical.  */
6471   if (COMMUTATIVE_ARITH_P (x)
6472       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
6473     {
6474       tem = XEXP (x, 0);
6475       SUBST (XEXP (x, 0), XEXP (x, 1));
6476       SUBST (XEXP (x, 1), tem);
6477     }
6478
6479   return x;
6480 }
6481 \f
6482 /* Given M see if it is a value that would select a field of bits
6483    within an item, but not the entire word.  Return -1 if not.
6484    Otherwise, return the starting position of the field, where 0 is the
6485    low-order bit.
6486
6487    *PLEN is set to the length of the field.  */
6488
6489 static int
6490 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
6491 {
6492   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
6493   int pos = exact_log2 (m & -m);
6494   int len = 0;
6495
6496   if (pos >= 0)
6497     /* Now shift off the low-order zero bits and see if we have a
6498        power of two minus 1.  */
6499     len = exact_log2 ((m >> pos) + 1);
6500
6501   if (len <= 0)
6502     pos = -1;
6503
6504   *plen = len;
6505   return pos;
6506 }
6507 \f
6508 /* If X refers to a register that equals REG in value, replace these
6509    references with REG.  */
6510 static rtx
6511 canon_reg_for_combine (rtx x, rtx reg)
6512 {
6513   rtx op0, op1, op2;
6514   const char *fmt;
6515   int i;
6516   bool copied;
6517
6518   enum rtx_code code = GET_CODE (x);
6519   switch (GET_RTX_CLASS (code))
6520     {
6521     case RTX_UNARY:
6522       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
6523       if (op0 != XEXP (x, 0))
6524         return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
6525                                    GET_MODE (reg));
6526       break;
6527
6528     case RTX_BIN_ARITH:
6529     case RTX_COMM_ARITH:
6530       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
6531       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
6532       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
6533         return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
6534       break;
6535
6536     case RTX_COMPARE:
6537     case RTX_COMM_COMPARE:
6538       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
6539       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
6540       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
6541         return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
6542                                         GET_MODE (op0), op0, op1);
6543       break;
6544
6545     case RTX_TERNARY:
6546     case RTX_BITFIELD_OPS:
6547       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
6548       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
6549       op2 = canon_reg_for_combine (XEXP (x, 2), reg);
6550       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
6551         return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
6552                                      GET_MODE (op0), op0, op1, op2);
6553
6554     case RTX_OBJ:
6555       if (REG_P (x))
6556         {
6557           if (rtx_equal_p (get_last_value (reg), x)
6558               || rtx_equal_p (reg, get_last_value (x)))
6559             return reg;
6560           else
6561             break;
6562         }
6563
6564       /* fall through */
6565
6566     default:
6567       fmt = GET_RTX_FORMAT (code);
6568       copied = false;
6569       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6570         if (fmt[i] == 'e')
6571           {
6572             rtx op = canon_reg_for_combine (XEXP (x, i), reg);
6573             if (op != XEXP (x, i))
6574               {
6575                 if (!copied)
6576                   {
6577                     copied = true;
6578                     x = copy_rtx (x);
6579                   }
6580                 XEXP (x, i) = op;
6581               }
6582           }
6583         else if (fmt[i] == 'E')
6584           {
6585             int j;
6586             for (j = 0; j < XVECLEN (x, i); j++)
6587               {
6588                 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
6589                 if (op != XVECEXP (x, i, j))
6590                   {
6591                     if (!copied)
6592                       {
6593                         copied = true;
6594                         x = copy_rtx (x);
6595                       }
6596                     XVECEXP (x, i, j) = op;
6597                   }
6598               }
6599           }
6600
6601       break;
6602     }
6603
6604   return x;
6605 }
6606
6607 /* See if X can be simplified knowing that we will only refer to it in
6608    MODE and will only refer to those bits that are nonzero in MASK.
6609    If other bits are being computed or if masking operations are done
6610    that select a superset of the bits in MASK, they can sometimes be
6611    ignored.
6612
6613    Return a possibly simplified expression, but always convert X to
6614    MODE.  If X is a CONST_INT, AND the CONST_INT with MASK.
6615
6616    If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6617    are all off in X.  This is used when X will be complemented, by either
6618    NOT, NEG, or XOR.  */
6619
6620 static rtx
6621 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
6622                int just_select)
6623 {
6624   enum rtx_code code = GET_CODE (x);
6625   int next_select = just_select || code == XOR || code == NOT || code == NEG;
6626   enum machine_mode op_mode;
6627   unsigned HOST_WIDE_INT fuller_mask, nonzero;
6628   rtx op0, op1, temp;
6629
6630   /* If this is a CALL or ASM_OPERANDS, don't do anything.  Some of the
6631      code below will do the wrong thing since the mode of such an
6632      expression is VOIDmode.
6633
6634      Also do nothing if X is a CLOBBER; this can happen if X was
6635      the return value from a call to gen_lowpart.  */
6636   if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6637     return x;
6638
6639   /* We want to perform the operation is its present mode unless we know
6640      that the operation is valid in MODE, in which case we do the operation
6641      in MODE.  */
6642   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6643               && have_insn_for (code, mode))
6644              ? mode : GET_MODE (x));
6645
6646   /* It is not valid to do a right-shift in a narrower mode
6647      than the one it came in with.  */
6648   if ((code == LSHIFTRT || code == ASHIFTRT)
6649       && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6650     op_mode = GET_MODE (x);
6651
6652   /* Truncate MASK to fit OP_MODE.  */
6653   if (op_mode)
6654     mask &= GET_MODE_MASK (op_mode);
6655
6656   /* When we have an arithmetic operation, or a shift whose count we
6657      do not know, we need to assume that all bits up to the highest-order
6658      bit in MASK will be needed.  This is how we form such a mask.  */
6659   if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
6660     fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
6661   else
6662     fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
6663                    - 1);
6664
6665   /* Determine what bits of X are guaranteed to be (non)zero.  */
6666   nonzero = nonzero_bits (x, mode);
6667
6668   /* If none of the bits in X are needed, return a zero.  */
6669   if (! just_select && (nonzero & mask) == 0)
6670     x = const0_rtx;
6671
6672   /* If X is a CONST_INT, return a new one.  Do this here since the
6673      test below will fail.  */
6674   if (GET_CODE (x) == CONST_INT)
6675     {
6676       if (SCALAR_INT_MODE_P (mode))
6677         return gen_int_mode (INTVAL (x) & mask, mode);
6678       else
6679         {
6680           x = GEN_INT (INTVAL (x) & mask);
6681           return gen_lowpart_common (mode, x);
6682         }
6683     }
6684
6685   /* If X is narrower than MODE and we want all the bits in X's mode, just
6686      get X in the proper mode.  */
6687   if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6688       && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
6689     return gen_lowpart (mode, x);
6690
6691   switch (code)
6692     {
6693     case CLOBBER:
6694       /* If X is a (clobber (const_int)), return it since we know we are
6695          generating something that won't match.  */
6696       return x;
6697
6698     case USE:
6699       /* X is a (use (mem ..)) that was made from a bit-field extraction that
6700          spanned the boundary of the MEM.  If we are now masking so it is
6701          within that boundary, we don't need the USE any more.  */
6702       if (! BITS_BIG_ENDIAN
6703           && (mask & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6704         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
6705       break;
6706
6707     case SIGN_EXTEND:
6708     case ZERO_EXTEND:
6709     case ZERO_EXTRACT:
6710     case SIGN_EXTRACT:
6711       x = expand_compound_operation (x);
6712       if (GET_CODE (x) != code)
6713         return force_to_mode (x, mode, mask, next_select);
6714       break;
6715
6716     case SUBREG:
6717       if (subreg_lowpart_p (x)
6718           /* We can ignore the effect of this SUBREG if it narrows the mode or
6719              if the constant masks to zero all the bits the mode doesn't
6720              have.  */
6721           && ((GET_MODE_SIZE (GET_MODE (x))
6722                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6723               || (0 == (mask
6724                         & GET_MODE_MASK (GET_MODE (x))
6725                         & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6726         return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
6727       break;
6728
6729     case AND:
6730       /* If this is an AND with a constant, convert it into an AND
6731          whose constant is the AND of that constant with MASK.  If it
6732          remains an AND of MASK, delete it since it is redundant.  */
6733
6734       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6735         {
6736           x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6737                                       mask & INTVAL (XEXP (x, 1)));
6738
6739           /* If X is still an AND, see if it is an AND with a mask that
6740              is just some low-order bits.  If so, and it is MASK, we don't
6741              need it.  */
6742
6743           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6744               && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
6745                   == mask))
6746             x = XEXP (x, 0);
6747
6748           /* If it remains an AND, try making another AND with the bits
6749              in the mode mask that aren't in MASK turned on.  If the
6750              constant in the AND is wide enough, this might make a
6751              cheaper constant.  */
6752
6753           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6754               && GET_MODE_MASK (GET_MODE (x)) != mask
6755               && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6756             {
6757               HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6758                                     | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
6759               int width = GET_MODE_BITSIZE (GET_MODE (x));
6760               rtx y;
6761
6762               /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
6763                  number, sign extend it.  */
6764               if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6765                   && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6766                 cval |= (HOST_WIDE_INT) -1 << width;
6767
6768               y = simplify_gen_binary (AND, GET_MODE (x),
6769                                        XEXP (x, 0), GEN_INT (cval));
6770               if (rtx_cost (y, SET) < rtx_cost (x, SET))
6771                 x = y;
6772             }
6773
6774           break;
6775         }
6776
6777       goto binop;
6778
6779     case PLUS:
6780       /* In (and (plus FOO C1) M), if M is a mask that just turns off
6781          low-order bits (as in an alignment operation) and FOO is already
6782          aligned to that boundary, mask C1 to that boundary as well.
6783          This may eliminate that PLUS and, later, the AND.  */
6784
6785       {
6786         unsigned int width = GET_MODE_BITSIZE (mode);
6787         unsigned HOST_WIDE_INT smask = mask;
6788
6789         /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
6790            number, sign extend it.  */
6791
6792         if (width < HOST_BITS_PER_WIDE_INT
6793             && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6794           smask |= (HOST_WIDE_INT) -1 << width;
6795
6796         if (GET_CODE (XEXP (x, 1)) == CONST_INT
6797             && exact_log2 (- smask) >= 0
6798             && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
6799             && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
6800           return force_to_mode (plus_constant (XEXP (x, 0),
6801                                                (INTVAL (XEXP (x, 1)) & smask)),
6802                                 mode, smask, next_select);
6803       }
6804
6805       /* ... fall through ...  */
6806
6807     case MULT:
6808       /* For PLUS, MINUS and MULT, we need any bits less significant than the
6809          most significant bit in MASK since carries from those bits will
6810          affect the bits we are interested in.  */
6811       mask = fuller_mask;
6812       goto binop;
6813
6814     case MINUS:
6815       /* If X is (minus C Y) where C's least set bit is larger than any bit
6816          in the mask, then we may replace with (neg Y).  */
6817       if (GET_CODE (XEXP (x, 0)) == CONST_INT
6818           && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
6819                                         & -INTVAL (XEXP (x, 0))))
6820               > mask))
6821         {
6822           x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
6823                                   GET_MODE (x));
6824           return force_to_mode (x, mode, mask, next_select);
6825         }
6826
6827       /* Similarly, if C contains every bit in the fuller_mask, then we may
6828          replace with (not Y).  */
6829       if (GET_CODE (XEXP (x, 0)) == CONST_INT
6830           && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
6831               == INTVAL (XEXP (x, 0))))
6832         {
6833           x = simplify_gen_unary (NOT, GET_MODE (x),
6834                                   XEXP (x, 1), GET_MODE (x));
6835           return force_to_mode (x, mode, mask, next_select);
6836         }
6837
6838       mask = fuller_mask;
6839       goto binop;
6840
6841     case IOR:
6842     case XOR:
6843       /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
6844          LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
6845          operation which may be a bitfield extraction.  Ensure that the
6846          constant we form is not wider than the mode of X.  */
6847
6848       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6849           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6850           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6851           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6852           && GET_CODE (XEXP (x, 1)) == CONST_INT
6853           && ((INTVAL (XEXP (XEXP (x, 0), 1))
6854                + floor_log2 (INTVAL (XEXP (x, 1))))
6855               < GET_MODE_BITSIZE (GET_MODE (x)))
6856           && (INTVAL (XEXP (x, 1))
6857               & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
6858         {
6859           temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
6860                           << INTVAL (XEXP (XEXP (x, 0), 1)));
6861           temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
6862                                       XEXP (XEXP (x, 0), 0), temp);
6863           x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
6864                                    XEXP (XEXP (x, 0), 1));
6865           return force_to_mode (x, mode, mask, next_select);
6866         }
6867
6868     binop:
6869       /* For most binary operations, just propagate into the operation and
6870          change the mode if we have an operation of that mode.  */
6871
6872       op0 = gen_lowpart (op_mode,
6873                          force_to_mode (XEXP (x, 0), mode, mask,
6874                                         next_select));
6875       op1 = gen_lowpart (op_mode,
6876                          force_to_mode (XEXP (x, 1), mode, mask,
6877                                         next_select));
6878
6879       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
6880         x = simplify_gen_binary (code, op_mode, op0, op1);
6881       break;
6882
6883     case ASHIFT:
6884       /* For left shifts, do the same, but just for the first operand.
6885          However, we cannot do anything with shifts where we cannot
6886          guarantee that the counts are smaller than the size of the mode
6887          because such a count will have a different meaning in a
6888          wider mode.  */
6889
6890       if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
6891              && INTVAL (XEXP (x, 1)) >= 0
6892              && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
6893           && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
6894                 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
6895                     < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
6896         break;
6897
6898       /* If the shift count is a constant and we can do arithmetic in
6899          the mode of the shift, refine which bits we need.  Otherwise, use the
6900          conservative form of the mask.  */
6901       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6902           && INTVAL (XEXP (x, 1)) >= 0
6903           && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
6904           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6905         mask >>= INTVAL (XEXP (x, 1));
6906       else
6907         mask = fuller_mask;
6908
6909       op0 = gen_lowpart (op_mode,
6910                          force_to_mode (XEXP (x, 0), op_mode,
6911                                         mask, next_select));
6912
6913       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6914         x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
6915       break;
6916
6917     case LSHIFTRT:
6918       /* Here we can only do something if the shift count is a constant,
6919          this shift constant is valid for the host, and we can do arithmetic
6920          in OP_MODE.  */
6921
6922       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6923           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6924           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6925         {
6926           rtx inner = XEXP (x, 0);
6927           unsigned HOST_WIDE_INT inner_mask;
6928
6929           /* Select the mask of the bits we need for the shift operand.  */
6930           inner_mask = mask << INTVAL (XEXP (x, 1));
6931
6932           /* We can only change the mode of the shift if we can do arithmetic
6933              in the mode of the shift and INNER_MASK is no wider than the
6934              width of X's mode.  */
6935           if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
6936             op_mode = GET_MODE (x);
6937
6938           inner = force_to_mode (inner, op_mode, inner_mask, next_select);
6939
6940           if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
6941             x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
6942         }
6943
6944       /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
6945          shift and AND produces only copies of the sign bit (C2 is one less
6946          than a power of two), we can do this with just a shift.  */
6947
6948       if (GET_CODE (x) == LSHIFTRT
6949           && GET_CODE (XEXP (x, 1)) == CONST_INT
6950           /* The shift puts one of the sign bit copies in the least significant
6951              bit.  */
6952           && ((INTVAL (XEXP (x, 1))
6953                + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
6954               >= GET_MODE_BITSIZE (GET_MODE (x)))
6955           && exact_log2 (mask + 1) >= 0
6956           /* Number of bits left after the shift must be more than the mask
6957              needs.  */
6958           && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
6959               <= GET_MODE_BITSIZE (GET_MODE (x)))
6960           /* Must be more sign bit copies than the mask needs.  */
6961           && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6962               >= exact_log2 (mask + 1)))
6963         x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6964                                  GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
6965                                           - exact_log2 (mask + 1)));
6966
6967       goto shiftrt;
6968
6969     case ASHIFTRT:
6970       /* If we are just looking for the sign bit, we don't need this shift at
6971          all, even if it has a variable count.  */
6972       if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6973           && (mask == ((unsigned HOST_WIDE_INT) 1
6974                        << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
6975         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
6976
6977       /* If this is a shift by a constant, get a mask that contains those bits
6978          that are not copies of the sign bit.  We then have two cases:  If
6979          MASK only includes those bits, this can be a logical shift, which may
6980          allow simplifications.  If MASK is a single-bit field not within
6981          those bits, we are requesting a copy of the sign bit and hence can
6982          shift the sign bit to the appropriate location.  */
6983
6984       if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
6985           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
6986         {
6987           int i = -1;
6988
6989           /* If the considered data is wider than HOST_WIDE_INT, we can't
6990              represent a mask for all its bits in a single scalar.
6991              But we only care about the lower bits, so calculate these.  */
6992
6993           if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
6994             {
6995               nonzero = ~(HOST_WIDE_INT) 0;
6996
6997               /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6998                  is the number of bits a full-width mask would have set.
6999                  We need only shift if these are fewer than nonzero can
7000                  hold.  If not, we must keep all bits set in nonzero.  */
7001
7002               if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7003                   < HOST_BITS_PER_WIDE_INT)
7004                 nonzero >>= INTVAL (XEXP (x, 1))
7005                             + HOST_BITS_PER_WIDE_INT
7006                             - GET_MODE_BITSIZE (GET_MODE (x)) ;
7007             }
7008           else
7009             {
7010               nonzero = GET_MODE_MASK (GET_MODE (x));
7011               nonzero >>= INTVAL (XEXP (x, 1));
7012             }
7013
7014           if ((mask & ~nonzero) == 0
7015               || (i = exact_log2 (mask)) >= 0)
7016             {
7017               x = simplify_shift_const
7018                 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7019                  i < 0 ? INTVAL (XEXP (x, 1))
7020                  : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7021
7022               if (GET_CODE (x) != ASHIFTRT)
7023                 return force_to_mode (x, mode, mask, next_select);
7024             }
7025         }
7026
7027       /* If MASK is 1, convert this to an LSHIFTRT.  This can be done
7028          even if the shift count isn't a constant.  */
7029       if (mask == 1)
7030         x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7031                                  XEXP (x, 0), XEXP (x, 1));
7032
7033     shiftrt:
7034
7035       /* If this is a zero- or sign-extension operation that just affects bits
7036          we don't care about, remove it.  Be sure the call above returned
7037          something that is still a shift.  */
7038
7039       if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7040           && GET_CODE (XEXP (x, 1)) == CONST_INT
7041           && INTVAL (XEXP (x, 1)) >= 0
7042           && (INTVAL (XEXP (x, 1))
7043               <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7044           && GET_CODE (XEXP (x, 0)) == ASHIFT
7045           && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7046         return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7047                               next_select);
7048
7049       break;
7050
7051     case ROTATE:
7052     case ROTATERT:
7053       /* If the shift count is constant and we can do computations
7054          in the mode of X, compute where the bits we care about are.
7055          Otherwise, we can't do anything.  Don't change the mode of
7056          the shift or propagate MODE into the shift, though.  */
7057       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7058           && INTVAL (XEXP (x, 1)) >= 0)
7059         {
7060           temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7061                                             GET_MODE (x), GEN_INT (mask),
7062                                             XEXP (x, 1));
7063           if (temp && GET_CODE (temp) == CONST_INT)
7064             SUBST (XEXP (x, 0),
7065                    force_to_mode (XEXP (x, 0), GET_MODE (x),
7066                                   INTVAL (temp), next_select));
7067         }
7068       break;
7069
7070     case NEG:
7071       /* If we just want the low-order bit, the NEG isn't needed since it
7072          won't change the low-order bit.  */
7073       if (mask == 1)
7074         return force_to_mode (XEXP (x, 0), mode, mask, just_select);
7075
7076       /* We need any bits less significant than the most significant bit in
7077          MASK since carries from those bits will affect the bits we are
7078          interested in.  */
7079       mask = fuller_mask;
7080       goto unop;
7081
7082     case NOT:
7083       /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7084          same as the XOR case above.  Ensure that the constant we form is not
7085          wider than the mode of X.  */
7086
7087       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7088           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7089           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7090           && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7091               < GET_MODE_BITSIZE (GET_MODE (x)))
7092           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7093         {
7094           temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7095                                GET_MODE (x));
7096           temp = simplify_gen_binary (XOR, GET_MODE (x),
7097                                       XEXP (XEXP (x, 0), 0), temp);
7098           x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7099                                    temp, XEXP (XEXP (x, 0), 1));
7100
7101           return force_to_mode (x, mode, mask, next_select);
7102         }
7103
7104       /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7105          use the full mask inside the NOT.  */
7106       mask = fuller_mask;
7107
7108     unop:
7109       op0 = gen_lowpart (op_mode,
7110                          force_to_mode (XEXP (x, 0), mode, mask,
7111                                         next_select));
7112       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7113         x = simplify_gen_unary (code, op_mode, op0, op_mode);
7114       break;
7115
7116     case NE:
7117       /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7118          in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7119          which is equal to STORE_FLAG_VALUE.  */
7120       if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7121           && GET_MODE (XEXP (x, 0)) == mode
7122           && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7123           && (nonzero_bits (XEXP (x, 0), mode)
7124               == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7125         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7126
7127       break;
7128
7129     case IF_THEN_ELSE:
7130       /* We have no way of knowing if the IF_THEN_ELSE can itself be
7131          written in a narrower mode.  We play it safe and do not do so.  */
7132
7133       SUBST (XEXP (x, 1),
7134              gen_lowpart (GET_MODE (x), force_to_mode (XEXP (x, 1), mode,
7135                                                        mask, next_select)));
7136       SUBST (XEXP (x, 2),
7137              gen_lowpart (GET_MODE (x), force_to_mode (XEXP (x, 2), mode,
7138                                                        mask, next_select)));
7139       break;
7140
7141     default:
7142       break;
7143     }
7144
7145   /* Ensure we return a value of the proper mode.  */
7146   return gen_lowpart (mode, x);
7147 }
7148 \f
7149 /* Return nonzero if X is an expression that has one of two values depending on
7150    whether some other value is zero or nonzero.  In that case, we return the
7151    value that is being tested, *PTRUE is set to the value if the rtx being
7152    returned has a nonzero value, and *PFALSE is set to the other alternative.
7153
7154    If we return zero, we set *PTRUE and *PFALSE to X.  */
7155
7156 static rtx
7157 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
7158 {
7159   enum machine_mode mode = GET_MODE (x);
7160   enum rtx_code code = GET_CODE (x);
7161   rtx cond0, cond1, true0, true1, false0, false1;
7162   unsigned HOST_WIDE_INT nz;
7163
7164   /* If we are comparing a value against zero, we are done.  */
7165   if ((code == NE || code == EQ)
7166       && XEXP (x, 1) == const0_rtx)
7167     {
7168       *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7169       *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7170       return XEXP (x, 0);
7171     }
7172
7173   /* If this is a unary operation whose operand has one of two values, apply
7174      our opcode to compute those values.  */
7175   else if (UNARY_P (x)
7176            && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7177     {
7178       *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7179       *pfalse = simplify_gen_unary (code, mode, false0,
7180                                     GET_MODE (XEXP (x, 0)));
7181       return cond0;
7182     }
7183
7184   /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7185      make can't possibly match and would suppress other optimizations.  */
7186   else if (code == COMPARE)
7187     ;
7188
7189   /* If this is a binary operation, see if either side has only one of two
7190      values.  If either one does or if both do and they are conditional on
7191      the same value, compute the new true and false values.  */
7192   else if (BINARY_P (x))
7193     {
7194       cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7195       cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7196
7197       if ((cond0 != 0 || cond1 != 0)
7198           && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7199         {
7200           /* If if_then_else_cond returned zero, then true/false are the
7201              same rtl.  We must copy one of them to prevent invalid rtl
7202              sharing.  */
7203           if (cond0 == 0)
7204             true0 = copy_rtx (true0);
7205           else if (cond1 == 0)
7206             true1 = copy_rtx (true1);
7207
7208           if (COMPARISON_P (x))
7209             {
7210               *ptrue = simplify_gen_relational (code, mode, VOIDmode,
7211                                                 true0, true1);
7212               *pfalse = simplify_gen_relational (code, mode, VOIDmode,
7213                                                  false0, false1);
7214              }
7215           else
7216             {
7217               *ptrue = simplify_gen_binary (code, mode, true0, true1);
7218               *pfalse = simplify_gen_binary (code, mode, false0, false1);
7219             }
7220
7221           return cond0 ? cond0 : cond1;
7222         }
7223
7224       /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7225          operands is zero when the other is nonzero, and vice-versa,
7226          and STORE_FLAG_VALUE is 1 or -1.  */
7227
7228       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7229           && (code == PLUS || code == IOR || code == XOR || code == MINUS
7230               || code == UMAX)
7231           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7232         {
7233           rtx op0 = XEXP (XEXP (x, 0), 1);
7234           rtx op1 = XEXP (XEXP (x, 1), 1);
7235
7236           cond0 = XEXP (XEXP (x, 0), 0);
7237           cond1 = XEXP (XEXP (x, 1), 0);
7238
7239           if (COMPARISON_P (cond0)
7240               && COMPARISON_P (cond1)
7241               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7242                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7243                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7244                   || ((swap_condition (GET_CODE (cond0))
7245                        == reversed_comparison_code (cond1, NULL))
7246                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7247                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7248               && ! side_effects_p (x))
7249             {
7250               *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
7251               *pfalse = simplify_gen_binary (MULT, mode,
7252                                              (code == MINUS
7253                                               ? simplify_gen_unary (NEG, mode,
7254                                                                     op1, mode)
7255                                               : op1),
7256                                               const_true_rtx);
7257               return cond0;
7258             }
7259         }
7260
7261       /* Similarly for MULT, AND and UMIN, except that for these the result
7262          is always zero.  */
7263       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7264           && (code == MULT || code == AND || code == UMIN)
7265           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7266         {
7267           cond0 = XEXP (XEXP (x, 0), 0);
7268           cond1 = XEXP (XEXP (x, 1), 0);
7269
7270           if (COMPARISON_P (cond0)
7271               && COMPARISON_P (cond1)
7272               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7273                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7274                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7275                   || ((swap_condition (GET_CODE (cond0))
7276                        == reversed_comparison_code (cond1, NULL))
7277                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7278                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7279               && ! side_effects_p (x))
7280             {
7281               *ptrue = *pfalse = const0_rtx;
7282               return cond0;
7283             }
7284         }
7285     }
7286
7287   else if (code == IF_THEN_ELSE)
7288     {
7289       /* If we have IF_THEN_ELSE already, extract the condition and
7290          canonicalize it if it is NE or EQ.  */
7291       cond0 = XEXP (x, 0);
7292       *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7293       if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7294         return XEXP (cond0, 0);
7295       else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7296         {
7297           *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7298           return XEXP (cond0, 0);
7299         }
7300       else
7301         return cond0;
7302     }
7303
7304   /* If X is a SUBREG, we can narrow both the true and false values
7305      if the inner expression, if there is a condition.  */
7306   else if (code == SUBREG
7307            && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7308                                                &true0, &false0)))
7309     {
7310       true0 = simplify_gen_subreg (mode, true0,
7311                                    GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7312       false0 = simplify_gen_subreg (mode, false0,
7313                                     GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7314       if (true0 && false0)
7315         {
7316           *ptrue = true0;
7317           *pfalse = false0;
7318           return cond0;
7319         }
7320     }
7321
7322   /* If X is a constant, this isn't special and will cause confusions
7323      if we treat it as such.  Likewise if it is equivalent to a constant.  */
7324   else if (CONSTANT_P (x)
7325            || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7326     ;
7327
7328   /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7329      will be least confusing to the rest of the compiler.  */
7330   else if (mode == BImode)
7331     {
7332       *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7333       return x;
7334     }
7335
7336   /* If X is known to be either 0 or -1, those are the true and
7337      false values when testing X.  */
7338   else if (x == constm1_rtx || x == const0_rtx
7339            || (mode != VOIDmode
7340                && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7341     {
7342       *ptrue = constm1_rtx, *pfalse = const0_rtx;
7343       return x;
7344     }
7345
7346   /* Likewise for 0 or a single bit.  */
7347   else if (SCALAR_INT_MODE_P (mode)
7348            && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7349            && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7350     {
7351       *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7352       return x;
7353     }
7354
7355   /* Otherwise fail; show no condition with true and false values the same.  */
7356   *ptrue = *pfalse = x;
7357   return 0;
7358 }
7359 \f
7360 /* Return the value of expression X given the fact that condition COND
7361    is known to be true when applied to REG as its first operand and VAL
7362    as its second.  X is known to not be shared and so can be modified in
7363    place.
7364
7365    We only handle the simplest cases, and specifically those cases that
7366    arise with IF_THEN_ELSE expressions.  */
7367
7368 static rtx
7369 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
7370 {
7371   enum rtx_code code = GET_CODE (x);
7372   rtx temp;
7373   const char *fmt;
7374   int i, j;
7375
7376   if (side_effects_p (x))
7377     return x;
7378
7379   /* If either operand of the condition is a floating point value,
7380      then we have to avoid collapsing an EQ comparison.  */
7381   if (cond == EQ
7382       && rtx_equal_p (x, reg)
7383       && ! FLOAT_MODE_P (GET_MODE (x))
7384       && ! FLOAT_MODE_P (GET_MODE (val)))
7385     return val;
7386
7387   if (cond == UNEQ && rtx_equal_p (x, reg))
7388     return val;
7389
7390   /* If X is (abs REG) and we know something about REG's relationship
7391      with zero, we may be able to simplify this.  */
7392
7393   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7394     switch (cond)
7395       {
7396       case GE:  case GT:  case EQ:
7397         return XEXP (x, 0);
7398       case LT:  case LE:
7399         return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
7400                                    XEXP (x, 0),
7401                                    GET_MODE (XEXP (x, 0)));
7402       default:
7403         break;
7404       }
7405
7406   /* The only other cases we handle are MIN, MAX, and comparisons if the
7407      operands are the same as REG and VAL.  */
7408
7409   else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
7410     {
7411       if (rtx_equal_p (XEXP (x, 0), val))
7412         cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7413
7414       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7415         {
7416           if (COMPARISON_P (x))
7417             {
7418               if (comparison_dominates_p (cond, code))
7419                 return const_true_rtx;
7420
7421               code = reversed_comparison_code (x, NULL);
7422               if (code != UNKNOWN
7423                   && comparison_dominates_p (cond, code))
7424                 return const0_rtx;
7425               else
7426                 return x;
7427             }
7428           else if (code == SMAX || code == SMIN
7429                    || code == UMIN || code == UMAX)
7430             {
7431               int unsignedp = (code == UMIN || code == UMAX);
7432
7433               /* Do not reverse the condition when it is NE or EQ.
7434                  This is because we cannot conclude anything about
7435                  the value of 'SMAX (x, y)' when x is not equal to y,
7436                  but we can when x equals y.  */
7437               if ((code == SMAX || code == UMAX)
7438                   && ! (cond == EQ || cond == NE))
7439                 cond = reverse_condition (cond);
7440
7441               switch (cond)
7442                 {
7443                 case GE:   case GT:
7444                   return unsignedp ? x : XEXP (x, 1);
7445                 case LE:   case LT:
7446                   return unsignedp ? x : XEXP (x, 0);
7447                 case GEU:  case GTU:
7448                   return unsignedp ? XEXP (x, 1) : x;
7449                 case LEU:  case LTU:
7450                   return unsignedp ? XEXP (x, 0) : x;
7451                 default:
7452                   break;
7453                 }
7454             }
7455         }
7456     }
7457   else if (code == SUBREG)
7458     {
7459       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
7460       rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
7461
7462       if (SUBREG_REG (x) != r)
7463         {
7464           /* We must simplify subreg here, before we lose track of the
7465              original inner_mode.  */
7466           new = simplify_subreg (GET_MODE (x), r,
7467                                  inner_mode, SUBREG_BYTE (x));
7468           if (new)
7469             return new;
7470           else
7471             SUBST (SUBREG_REG (x), r);
7472         }
7473
7474       return x;
7475     }
7476   /* We don't have to handle SIGN_EXTEND here, because even in the
7477      case of replacing something with a modeless CONST_INT, a
7478      CONST_INT is already (supposed to be) a valid sign extension for
7479      its narrower mode, which implies it's already properly
7480      sign-extended for the wider mode.  Now, for ZERO_EXTEND, the
7481      story is different.  */
7482   else if (code == ZERO_EXTEND)
7483     {
7484       enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
7485       rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
7486
7487       if (XEXP (x, 0) != r)
7488         {
7489           /* We must simplify the zero_extend here, before we lose
7490              track of the original inner_mode.  */
7491           new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
7492                                           r, inner_mode);
7493           if (new)
7494             return new;
7495           else
7496             SUBST (XEXP (x, 0), r);
7497         }
7498
7499       return x;
7500     }
7501
7502   fmt = GET_RTX_FORMAT (code);
7503   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7504     {
7505       if (fmt[i] == 'e')
7506         SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7507       else if (fmt[i] == 'E')
7508         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7509           SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7510                                                 cond, reg, val));
7511     }
7512
7513   return x;
7514 }
7515 \f
7516 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
7517    assignment as a field assignment.  */
7518
7519 static int
7520 rtx_equal_for_field_assignment_p (rtx x, rtx y)
7521 {
7522   if (x == y || rtx_equal_p (x, y))
7523     return 1;
7524
7525   if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7526     return 0;
7527
7528   /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7529      Note that all SUBREGs of MEM are paradoxical; otherwise they
7530      would have been rewritten.  */
7531   if (MEM_P (x) && GET_CODE (y) == SUBREG
7532       && MEM_P (SUBREG_REG (y))
7533       && rtx_equal_p (SUBREG_REG (y),
7534                       gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
7535     return 1;
7536
7537   if (MEM_P (y) && GET_CODE (x) == SUBREG
7538       && MEM_P (SUBREG_REG (x))
7539       && rtx_equal_p (SUBREG_REG (x),
7540                       gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
7541     return 1;
7542
7543   /* We used to see if get_last_value of X and Y were the same but that's
7544      not correct.  In one direction, we'll cause the assignment to have
7545      the wrong destination and in the case, we'll import a register into this
7546      insn that might have already have been dead.   So fail if none of the
7547      above cases are true.  */
7548   return 0;
7549 }
7550 \f
7551 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
7552    Return that assignment if so.
7553
7554    We only handle the most common cases.  */
7555
7556 static rtx
7557 make_field_assignment (rtx x)
7558 {
7559   rtx dest = SET_DEST (x);
7560   rtx src = SET_SRC (x);
7561   rtx assign;
7562   rtx rhs, lhs;
7563   HOST_WIDE_INT c1;
7564   HOST_WIDE_INT pos;
7565   unsigned HOST_WIDE_INT len;
7566   rtx other;
7567   enum machine_mode mode;
7568
7569   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7570      a clear of a one-bit field.  We will have changed it to
7571      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
7572      for a SUBREG.  */
7573
7574   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7575       && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7576       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7577       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7578     {
7579       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7580                                 1, 1, 1, 0);
7581       if (assign != 0)
7582         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7583       return x;
7584     }
7585
7586   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7587       && subreg_lowpart_p (XEXP (src, 0))
7588       && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7589           < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7590       && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7591       && GET_CODE (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == CONST_INT
7592       && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7593       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7594     {
7595       assign = make_extraction (VOIDmode, dest, 0,
7596                                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7597                                 1, 1, 1, 0);
7598       if (assign != 0)
7599         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7600       return x;
7601     }
7602
7603   /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7604      one-bit field.  */
7605   if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7606       && XEXP (XEXP (src, 0), 0) == const1_rtx
7607       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7608     {
7609       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7610                                 1, 1, 1, 0);
7611       if (assign != 0)
7612         return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7613       return x;
7614     }
7615
7616   /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
7617      SRC is an AND with all bits of that field set, then we can discard
7618      the AND.  */
7619   if (GET_CODE (dest) == ZERO_EXTRACT
7620       && GET_CODE (XEXP (dest, 1)) == CONST_INT
7621       && GET_CODE (src) == AND
7622       && GET_CODE (XEXP (src, 1)) == CONST_INT)
7623     {
7624       HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
7625       unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
7626       unsigned HOST_WIDE_INT ze_mask;
7627
7628       if (width >= HOST_BITS_PER_WIDE_INT)
7629         ze_mask = -1;
7630       else
7631         ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
7632
7633       /* Complete overlap.  We can remove the source AND.  */
7634       if ((and_mask & ze_mask) == ze_mask)
7635         return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
7636
7637       /* Partial overlap.  We can reduce the source AND.  */
7638       if ((and_mask & ze_mask) != and_mask)
7639         {
7640           mode = GET_MODE (src);
7641           src = gen_rtx_AND (mode, XEXP (src, 0),
7642                              gen_int_mode (and_mask & ze_mask, mode));
7643           return gen_rtx_SET (VOIDmode, dest, src);
7644         }
7645     }
7646
7647   /* The other case we handle is assignments into a constant-position
7648      field.  They look like (ior/xor (and DEST C1) OTHER).  If C1 represents
7649      a mask that has all one bits except for a group of zero bits and
7650      OTHER is known to have zeros where C1 has ones, this is such an
7651      assignment.  Compute the position and length from C1.  Shift OTHER
7652      to the appropriate position, force it to the required mode, and
7653      make the extraction.  Check for the AND in both operands.  */
7654
7655   if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7656     return x;
7657
7658   rhs = expand_compound_operation (XEXP (src, 0));
7659   lhs = expand_compound_operation (XEXP (src, 1));
7660
7661   if (GET_CODE (rhs) == AND
7662       && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7663       && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7664     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7665   else if (GET_CODE (lhs) == AND
7666            && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7667            && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7668     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7669   else
7670     return x;
7671
7672   pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7673   if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7674       || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7675       || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7676     return x;
7677
7678   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7679   if (assign == 0)
7680     return x;
7681
7682   /* The mode to use for the source is the mode of the assignment, or of
7683      what is inside a possible STRICT_LOW_PART.  */
7684   mode = (GET_CODE (assign) == STRICT_LOW_PART
7685           ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7686
7687   /* Shift OTHER right POS places and make it the source, restricting it
7688      to the proper length and mode.  */
7689
7690   src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
7691                                                      GET_MODE (src),
7692                                                      other, pos),
7693                                dest);
7694   src = force_to_mode (src, mode,
7695                        GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7696                        ? ~(unsigned HOST_WIDE_INT) 0
7697                        : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7698                        0);
7699
7700   /* If SRC is masked by an AND that does not make a difference in
7701      the value being stored, strip it.  */
7702   if (GET_CODE (assign) == ZERO_EXTRACT
7703       && GET_CODE (XEXP (assign, 1)) == CONST_INT
7704       && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
7705       && GET_CODE (src) == AND
7706       && GET_CODE (XEXP (src, 1)) == CONST_INT
7707       && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
7708           == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
7709     src = XEXP (src, 0);
7710
7711   return gen_rtx_SET (VOIDmode, assign, src);
7712 }
7713 \f
7714 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7715    if so.  */
7716
7717 static rtx
7718 apply_distributive_law (rtx x)
7719 {
7720   enum rtx_code code = GET_CODE (x);
7721   enum rtx_code inner_code;
7722   rtx lhs, rhs, other;
7723   rtx tem;
7724
7725   /* Distributivity is not true for floating point as it can change the
7726      value.  So we don't do it unless -funsafe-math-optimizations.  */
7727   if (FLOAT_MODE_P (GET_MODE (x))
7728       && ! flag_unsafe_math_optimizations)
7729     return x;
7730
7731   /* The outer operation can only be one of the following:  */
7732   if (code != IOR && code != AND && code != XOR
7733       && code != PLUS && code != MINUS)
7734     return x;
7735
7736   lhs = XEXP (x, 0);
7737   rhs = XEXP (x, 1);
7738
7739   /* If either operand is a primitive we can't do anything, so get out
7740      fast.  */
7741   if (OBJECT_P (lhs) || OBJECT_P (rhs))
7742     return x;
7743
7744   lhs = expand_compound_operation (lhs);
7745   rhs = expand_compound_operation (rhs);
7746   inner_code = GET_CODE (lhs);
7747   if (inner_code != GET_CODE (rhs))
7748     return x;
7749
7750   /* See if the inner and outer operations distribute.  */
7751   switch (inner_code)
7752     {
7753     case LSHIFTRT:
7754     case ASHIFTRT:
7755     case AND:
7756     case IOR:
7757       /* These all distribute except over PLUS.  */
7758       if (code == PLUS || code == MINUS)
7759         return x;
7760       break;
7761
7762     case MULT:
7763       if (code != PLUS && code != MINUS)
7764         return x;
7765       break;
7766
7767     case ASHIFT:
7768       /* This is also a multiply, so it distributes over everything.  */
7769       break;
7770
7771     case SUBREG:
7772       /* Non-paradoxical SUBREGs distributes over all operations,
7773          provided the inner modes and byte offsets are the same, this
7774          is an extraction of a low-order part, we don't convert an fp
7775          operation to int or vice versa, this is not a vector mode,
7776          and we would not be converting a single-word operation into a
7777          multi-word operation.  The latter test is not required, but
7778          it prevents generating unneeded multi-word operations.  Some
7779          of the previous tests are redundant given the latter test,
7780          but are retained because they are required for correctness.
7781
7782          We produce the result slightly differently in this case.  */
7783
7784       if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
7785           || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
7786           || ! subreg_lowpart_p (lhs)
7787           || (GET_MODE_CLASS (GET_MODE (lhs))
7788               != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
7789           || (GET_MODE_SIZE (GET_MODE (lhs))
7790               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
7791           || VECTOR_MODE_P (GET_MODE (lhs))
7792           || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
7793         return x;
7794
7795       tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
7796                                  SUBREG_REG (lhs), SUBREG_REG (rhs));
7797       return gen_lowpart (GET_MODE (x), tem);
7798
7799     default:
7800       return x;
7801     }
7802
7803   /* Set LHS and RHS to the inner operands (A and B in the example
7804      above) and set OTHER to the common operand (C in the example).
7805      There is only one way to do this unless the inner operation is
7806      commutative.  */
7807   if (COMMUTATIVE_ARITH_P (lhs)
7808       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
7809     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
7810   else if (COMMUTATIVE_ARITH_P (lhs)
7811            && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
7812     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
7813   else if (COMMUTATIVE_ARITH_P (lhs)
7814            && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
7815     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
7816   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
7817     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
7818   else
7819     return x;
7820
7821   /* Form the new inner operation, seeing if it simplifies first.  */
7822   tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
7823
7824   /* There is one exception to the general way of distributing:
7825      (a | c) ^ (b | c) -> (a ^ b) & ~c  */
7826   if (code == XOR && inner_code == IOR)
7827     {
7828       inner_code = AND;
7829       other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
7830     }
7831
7832   /* We may be able to continuing distributing the result, so call
7833      ourselves recursively on the inner operation before forming the
7834      outer operation, which we return.  */
7835   return simplify_gen_binary (inner_code, GET_MODE (x),
7836                               apply_distributive_law (tem), other);
7837 }
7838
7839 /* See if X is of the form (* (+ A B) C), and if so convert to
7840    (+ (* A C) (* B C)) and try to simplify.
7841
7842    Most of the time, this results in no change.  However, if some of
7843    the operands are the same or inverses of each other, simplifications
7844    will result.
7845
7846    For example, (and (ior A B) (not B)) can occur as the result of
7847    expanding a bit field assignment.  When we apply the distributive
7848    law to this, we get (ior (and (A (not B))) (and (B (not B)))),
7849    which then simplifies to (and (A (not B))).
7850  
7851    Note that no checks happen on the validity of applying the inverse
7852    distributive law.  This is pointless since we can do it in the
7853    few places where this routine is called.
7854
7855    N is the index of the term that is decomposed (the arithmetic operation,
7856    i.e. (+ A B) in the first example above).  !N is the index of the term that
7857    is distributed, i.e. of C in the first example above.  */
7858 static rtx
7859 distribute_and_simplify_rtx (rtx x, int n)
7860 {
7861   enum machine_mode mode;
7862   enum rtx_code outer_code, inner_code;
7863   rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
7864
7865   decomposed = XEXP (x, n);
7866   if (!ARITHMETIC_P (decomposed))
7867     return NULL_RTX;
7868
7869   mode = GET_MODE (x);
7870   outer_code = GET_CODE (x);
7871   distributed = XEXP (x, !n);
7872
7873   inner_code = GET_CODE (decomposed);
7874   inner_op0 = XEXP (decomposed, 0);
7875   inner_op1 = XEXP (decomposed, 1);
7876
7877   /* Special case (and (xor B C) (not A)), which is equivalent to
7878      (xor (ior A B) (ior A C))  */
7879   if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
7880     {
7881       distributed = XEXP (distributed, 0);
7882       outer_code = IOR;
7883     }
7884
7885   if (n == 0)
7886     {
7887       /* Distribute the second term.  */
7888       new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
7889       new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
7890     }
7891   else
7892     {
7893       /* Distribute the first term.  */
7894       new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
7895       new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
7896     }
7897
7898   tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
7899                                                      new_op0, new_op1));
7900   if (GET_CODE (tmp) != outer_code
7901       && rtx_cost (tmp, SET) < rtx_cost (x, SET))
7902     return tmp;
7903
7904   return NULL_RTX;
7905 }
7906 \f
7907 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
7908    in MODE.
7909
7910    Return an equivalent form, if different from X.  Otherwise, return X.  If
7911    X is zero, we are to always construct the equivalent form.  */
7912
7913 static rtx
7914 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
7915                         unsigned HOST_WIDE_INT constop)
7916 {
7917   unsigned HOST_WIDE_INT nonzero;
7918   int i;
7919
7920   /* Simplify VAROP knowing that we will be only looking at some of the
7921      bits in it.
7922
7923      Note by passing in CONSTOP, we guarantee that the bits not set in
7924      CONSTOP are not significant and will never be examined.  We must
7925      ensure that is the case by explicitly masking out those bits
7926      before returning.  */
7927   varop = force_to_mode (varop, mode, constop, 0);
7928
7929   /* If VAROP is a CLOBBER, we will fail so return it.  */
7930   if (GET_CODE (varop) == CLOBBER)
7931     return varop;
7932
7933   /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
7934      to VAROP and return the new constant.  */
7935   if (GET_CODE (varop) == CONST_INT)
7936     return gen_int_mode (INTVAL (varop) & constop, mode);
7937
7938   /* See what bits may be nonzero in VAROP.  Unlike the general case of
7939      a call to nonzero_bits, here we don't care about bits outside
7940      MODE.  */
7941
7942   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
7943
7944   /* Turn off all bits in the constant that are known to already be zero.
7945      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
7946      which is tested below.  */
7947
7948   constop &= nonzero;
7949
7950   /* If we don't have any bits left, return zero.  */
7951   if (constop == 0)
7952     return const0_rtx;
7953
7954   /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
7955      a power of two, we can replace this with an ASHIFT.  */
7956   if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
7957       && (i = exact_log2 (constop)) >= 0)
7958     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
7959
7960   /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
7961      or XOR, then try to apply the distributive law.  This may eliminate
7962      operations if either branch can be simplified because of the AND.
7963      It may also make some cases more complex, but those cases probably
7964      won't match a pattern either with or without this.  */
7965
7966   if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
7967     return
7968       gen_lowpart
7969         (mode,
7970          apply_distributive_law
7971          (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
7972                                simplify_and_const_int (NULL_RTX,
7973                                                        GET_MODE (varop),
7974                                                        XEXP (varop, 0),
7975                                                        constop),
7976                                simplify_and_const_int (NULL_RTX,
7977                                                        GET_MODE (varop),
7978                                                        XEXP (varop, 1),
7979                                                        constop))));
7980
7981   /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
7982      the AND and see if one of the operands simplifies to zero.  If so, we
7983      may eliminate it.  */
7984
7985   if (GET_CODE (varop) == PLUS
7986       && exact_log2 (constop + 1) >= 0)
7987     {
7988       rtx o0, o1;
7989
7990       o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
7991       o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
7992       if (o0 == const0_rtx)
7993         return o1;
7994       if (o1 == const0_rtx)
7995         return o0;
7996     }
7997
7998   /* Get VAROP in MODE.  Try to get a SUBREG if not.  Don't make a new SUBREG
7999      if we already had one (just check for the simplest cases).  */
8000   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
8001       && GET_MODE (XEXP (x, 0)) == mode
8002       && SUBREG_REG (XEXP (x, 0)) == varop)
8003     varop = XEXP (x, 0);
8004   else
8005     varop = gen_lowpart (mode, varop);
8006
8007   /* If we can't make the SUBREG, try to return what we were given.  */
8008   if (GET_CODE (varop) == CLOBBER)
8009     return x ? x : varop;
8010
8011   /* If we are only masking insignificant bits, return VAROP.  */
8012   if (constop == nonzero)
8013     x = varop;
8014   else
8015     {
8016       /* Otherwise, return an AND.  */
8017       constop = trunc_int_for_mode (constop, mode);
8018       /* See how much, if any, of X we can use.  */
8019       if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
8020         x = simplify_gen_binary (AND, mode, varop, GEN_INT (constop));
8021
8022       else
8023         {
8024           if (GET_CODE (XEXP (x, 1)) != CONST_INT
8025               || (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) != constop)
8026             SUBST (XEXP (x, 1), GEN_INT (constop));
8027
8028           SUBST (XEXP (x, 0), varop);
8029         }
8030     }
8031
8032   return x;
8033 }
8034 \f
8035 /* Given a REG, X, compute which bits in X can be nonzero.
8036    We don't care about bits outside of those defined in MODE.
8037
8038    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8039    a shift, AND, or zero_extract, we can do better.  */
8040
8041 static rtx
8042 reg_nonzero_bits_for_combine (rtx x, enum machine_mode mode,
8043                               rtx known_x ATTRIBUTE_UNUSED,
8044                               enum machine_mode known_mode ATTRIBUTE_UNUSED,
8045                               unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
8046                               unsigned HOST_WIDE_INT *nonzero)
8047 {
8048   rtx tem;
8049
8050   /* If X is a register whose nonzero bits value is current, use it.
8051      Otherwise, if X is a register whose value we can find, use that
8052      value.  Otherwise, use the previously-computed global nonzero bits
8053      for this register.  */
8054
8055   if (reg_stat[REGNO (x)].last_set_value != 0
8056       && (reg_stat[REGNO (x)].last_set_mode == mode
8057           || (GET_MODE_CLASS (reg_stat[REGNO (x)].last_set_mode) == MODE_INT
8058               && GET_MODE_CLASS (mode) == MODE_INT))
8059       && (reg_stat[REGNO (x)].last_set_label == label_tick
8060           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8061               && REG_N_SETS (REGNO (x)) == 1
8062               && ! REGNO_REG_SET_P
8063                  (ENTRY_BLOCK_PTR->next_bb->il.rtl->global_live_at_start,
8064                   REGNO (x))))
8065       && INSN_CUID (reg_stat[REGNO (x)].last_set) < subst_low_cuid)
8066     {
8067       *nonzero &= reg_stat[REGNO (x)].last_set_nonzero_bits;
8068       return NULL;
8069     }
8070
8071   tem = get_last_value (x);
8072
8073   if (tem)
8074     {
8075 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8076       /* If X is narrower than MODE and TEM is a non-negative
8077          constant that would appear negative in the mode of X,
8078          sign-extend it for use in reg_nonzero_bits because some
8079          machines (maybe most) will actually do the sign-extension
8080          and this is the conservative approach.
8081
8082          ??? For 2.5, try to tighten up the MD files in this regard
8083          instead of this kludge.  */
8084
8085       if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
8086           && GET_CODE (tem) == CONST_INT
8087           && INTVAL (tem) > 0
8088           && 0 != (INTVAL (tem)
8089                    & ((HOST_WIDE_INT) 1
8090                       << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8091         tem = GEN_INT (INTVAL (tem)
8092                        | ((HOST_WIDE_INT) (-1)
8093                           << GET_MODE_BITSIZE (GET_MODE (x))));
8094 #endif
8095       return tem;
8096     }
8097   else if (nonzero_sign_valid && reg_stat[REGNO (x)].nonzero_bits)
8098     {
8099       unsigned HOST_WIDE_INT mask = reg_stat[REGNO (x)].nonzero_bits;
8100
8101       if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
8102         /* We don't know anything about the upper bits.  */
8103         mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8104       *nonzero &= mask;
8105     }
8106
8107   return NULL;
8108 }
8109
8110 /* Return the number of bits at the high-order end of X that are known to
8111    be equal to the sign bit.  X will be used in mode MODE; if MODE is
8112    VOIDmode, X will be used in its own mode.  The returned value  will always
8113    be between 1 and the number of bits in MODE.  */
8114
8115 static rtx
8116 reg_num_sign_bit_copies_for_combine (rtx x, enum machine_mode mode,
8117                                      rtx known_x ATTRIBUTE_UNUSED,
8118                                      enum machine_mode known_mode
8119                                      ATTRIBUTE_UNUSED,
8120                                      unsigned int known_ret ATTRIBUTE_UNUSED,
8121                                      unsigned int *result)
8122 {
8123   rtx tem;
8124
8125   if (reg_stat[REGNO (x)].last_set_value != 0
8126       && reg_stat[REGNO (x)].last_set_mode == mode
8127       && (reg_stat[REGNO (x)].last_set_label == label_tick
8128           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8129               && REG_N_SETS (REGNO (x)) == 1
8130               && ! REGNO_REG_SET_P
8131                  (ENTRY_BLOCK_PTR->next_bb->il.rtl->global_live_at_start,
8132                   REGNO (x))))
8133       && INSN_CUID (reg_stat[REGNO (x)].last_set) < subst_low_cuid)
8134     {
8135       *result = reg_stat[REGNO (x)].last_set_sign_bit_copies;
8136       return NULL;
8137     }
8138
8139   tem = get_last_value (x);
8140   if (tem != 0)
8141     return tem;
8142
8143   if (nonzero_sign_valid && reg_stat[REGNO (x)].sign_bit_copies != 0
8144       && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
8145     *result = reg_stat[REGNO (x)].sign_bit_copies;
8146       
8147   return NULL;
8148 }
8149 \f
8150 /* Return the number of "extended" bits there are in X, when interpreted
8151    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
8152    unsigned quantities, this is the number of high-order zero bits.
8153    For signed quantities, this is the number of copies of the sign bit
8154    minus 1.  In both case, this function returns the number of "spare"
8155    bits.  For example, if two quantities for which this function returns
8156    at least 1 are added, the addition is known not to overflow.
8157
8158    This function will always return 0 unless called during combine, which
8159    implies that it must be called from a define_split.  */
8160
8161 unsigned int
8162 extended_count (rtx x, enum machine_mode mode, int unsignedp)
8163 {
8164   if (nonzero_sign_valid == 0)
8165     return 0;
8166
8167   return (unsignedp
8168           ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8169              ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
8170                                - floor_log2 (nonzero_bits (x, mode)))
8171              : 0)
8172           : num_sign_bit_copies (x, mode) - 1);
8173 }
8174 \f
8175 /* This function is called from `simplify_shift_const' to merge two
8176    outer operations.  Specifically, we have already found that we need
8177    to perform operation *POP0 with constant *PCONST0 at the outermost
8178    position.  We would now like to also perform OP1 with constant CONST1
8179    (with *POP0 being done last).
8180
8181    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8182    the resulting operation.  *PCOMP_P is set to 1 if we would need to
8183    complement the innermost operand, otherwise it is unchanged.
8184
8185    MODE is the mode in which the operation will be done.  No bits outside
8186    the width of this mode matter.  It is assumed that the width of this mode
8187    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8188
8189    If *POP0 or OP1 are UNKNOWN, it means no operation is required.  Only NEG, PLUS,
8190    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
8191    result is simply *PCONST0.
8192
8193    If the resulting operation cannot be expressed as one operation, we
8194    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
8195
8196 static int
8197 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)
8198 {
8199   enum rtx_code op0 = *pop0;
8200   HOST_WIDE_INT const0 = *pconst0;
8201
8202   const0 &= GET_MODE_MASK (mode);
8203   const1 &= GET_MODE_MASK (mode);
8204
8205   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
8206   if (op0 == AND)
8207     const1 &= const0;
8208
8209   /* If OP0 or OP1 is UNKNOWN, this is easy.  Similarly if they are the same or
8210      if OP0 is SET.  */
8211
8212   if (op1 == UNKNOWN || op0 == SET)
8213     return 1;
8214
8215   else if (op0 == UNKNOWN)
8216     op0 = op1, const0 = const1;
8217
8218   else if (op0 == op1)
8219     {
8220       switch (op0)
8221         {
8222         case AND:
8223           const0 &= const1;
8224           break;
8225         case IOR:
8226           const0 |= const1;
8227           break;
8228         case XOR:
8229           const0 ^= const1;
8230           break;
8231         case PLUS:
8232           const0 += const1;
8233           break;
8234         case NEG:
8235           op0 = UNKNOWN;
8236           break;
8237         default:
8238           break;
8239         }
8240     }
8241
8242   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
8243   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8244     return 0;
8245
8246   /* If the two constants aren't the same, we can't do anything.  The
8247      remaining six cases can all be done.  */
8248   else if (const0 != const1)
8249     return 0;
8250
8251   else
8252     switch (op0)
8253       {
8254       case IOR:
8255         if (op1 == AND)
8256           /* (a & b) | b == b */
8257           op0 = SET;
8258         else /* op1 == XOR */
8259           /* (a ^ b) | b == a | b */
8260           {;}
8261         break;
8262
8263       case XOR:
8264         if (op1 == AND)
8265           /* (a & b) ^ b == (~a) & b */
8266           op0 = AND, *pcomp_p = 1;
8267         else /* op1 == IOR */
8268           /* (a | b) ^ b == a & ~b */
8269           op0 = AND, const0 = ~const0;
8270         break;
8271
8272       case AND:
8273         if (op1 == IOR)
8274           /* (a | b) & b == b */
8275         op0 = SET;
8276         else /* op1 == XOR */
8277           /* (a ^ b) & b) == (~a) & b */
8278           *pcomp_p = 1;
8279         break;
8280       default:
8281         break;
8282       }
8283
8284   /* Check for NO-OP cases.  */
8285   const0 &= GET_MODE_MASK (mode);
8286   if (const0 == 0
8287       && (op0 == IOR || op0 == XOR || op0 == PLUS))
8288     op0 = UNKNOWN;
8289   else if (const0 == 0 && op0 == AND)
8290     op0 = SET;
8291   else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8292            && op0 == AND)
8293     op0 = UNKNOWN;
8294
8295   /* ??? Slightly redundant with the above mask, but not entirely.
8296      Moving this above means we'd have to sign-extend the mode mask
8297      for the final test.  */
8298   const0 = trunc_int_for_mode (const0, mode);
8299
8300   *pop0 = op0;
8301   *pconst0 = const0;
8302
8303   return 1;
8304 }
8305 \f
8306 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
8307    The result of the shift is RESULT_MODE.  X, if nonzero, is an expression
8308    that we started with.
8309
8310    The shift is normally computed in the widest mode we find in VAROP, as
8311    long as it isn't a different number of words than RESULT_MODE.  Exceptions
8312    are ASHIFTRT and ROTATE, which are always done in their original mode,  */
8313
8314 static rtx
8315 simplify_shift_const (rtx x, enum rtx_code code,
8316                       enum machine_mode result_mode, rtx varop,
8317                       int orig_count)
8318 {
8319   enum rtx_code orig_code = code;
8320   unsigned int count;
8321   int signed_count;
8322   enum machine_mode mode = result_mode;
8323   enum machine_mode shift_mode, tmode;
8324   unsigned int mode_words
8325     = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8326   /* We form (outer_op (code varop count) (outer_const)).  */
8327   enum rtx_code outer_op = UNKNOWN;
8328   HOST_WIDE_INT outer_const = 0;
8329   rtx const_rtx;
8330   int complement_p = 0;
8331   rtx new;
8332
8333   /* Make sure and truncate the "natural" shift on the way in.  We don't
8334      want to do this inside the loop as it makes it more difficult to
8335      combine shifts.  */
8336   if (SHIFT_COUNT_TRUNCATED)
8337     orig_count &= GET_MODE_BITSIZE (mode) - 1;
8338
8339   /* If we were given an invalid count, don't do anything except exactly
8340      what was requested.  */
8341
8342   if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
8343     {
8344       if (x)
8345         return x;
8346
8347       return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (orig_count));
8348     }
8349
8350   count = orig_count;
8351
8352   /* Unless one of the branches of the `if' in this loop does a `continue',
8353      we will `break' the loop after the `if'.  */
8354
8355   while (count != 0)
8356     {
8357       /* If we have an operand of (clobber (const_int 0)), just return that
8358          value.  */
8359       if (GET_CODE (varop) == CLOBBER)
8360         return varop;
8361
8362       /* If we discovered we had to complement VAROP, leave.  Making a NOT
8363          here would cause an infinite loop.  */
8364       if (complement_p)
8365         break;
8366
8367       /* Convert ROTATERT to ROTATE.  */
8368       if (code == ROTATERT)
8369         {
8370           unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
8371           code = ROTATE;
8372           if (VECTOR_MODE_P (result_mode))
8373             count = bitsize / GET_MODE_NUNITS (result_mode) - count;
8374           else
8375             count = bitsize - count;
8376         }
8377
8378       /* We need to determine what mode we will do the shift in.  If the
8379          shift is a right shift or a ROTATE, we must always do it in the mode
8380          it was originally done in.  Otherwise, we can do it in MODE, the
8381          widest mode encountered.  */
8382       shift_mode
8383         = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8384            ? result_mode : mode);
8385
8386       /* Handle cases where the count is greater than the size of the mode
8387          minus 1.  For ASHIFT, use the size minus one as the count (this can
8388          occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
8389          take the count modulo the size.  For other shifts, the result is
8390          zero.
8391
8392          Since these shifts are being produced by the compiler by combining
8393          multiple operations, each of which are defined, we know what the
8394          result is supposed to be.  */
8395
8396       if (count > (unsigned int) (GET_MODE_BITSIZE (shift_mode) - 1))
8397         {
8398           if (code == ASHIFTRT)
8399             count = GET_MODE_BITSIZE (shift_mode) - 1;
8400           else if (code == ROTATE || code == ROTATERT)
8401             count %= GET_MODE_BITSIZE (shift_mode);
8402           else
8403             {
8404               /* We can't simply return zero because there may be an
8405                  outer op.  */
8406               varop = const0_rtx;
8407               count = 0;
8408               break;
8409             }
8410         }
8411
8412       /* An arithmetic right shift of a quantity known to be -1 or 0
8413          is a no-op.  */
8414       if (code == ASHIFTRT
8415           && (num_sign_bit_copies (varop, shift_mode)
8416               == GET_MODE_BITSIZE (shift_mode)))
8417         {
8418           count = 0;
8419           break;
8420         }
8421
8422       /* If we are doing an arithmetic right shift and discarding all but
8423          the sign bit copies, this is equivalent to doing a shift by the
8424          bitsize minus one.  Convert it into that shift because it will often
8425          allow other simplifications.  */
8426
8427       if (code == ASHIFTRT
8428           && (count + num_sign_bit_copies (varop, shift_mode)
8429               >= GET_MODE_BITSIZE (shift_mode)))
8430         count = GET_MODE_BITSIZE (shift_mode) - 1;
8431
8432       /* We simplify the tests below and elsewhere by converting
8433          ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8434          `make_compound_operation' will convert it to an ASHIFTRT for
8435          those machines (such as VAX) that don't have an LSHIFTRT.  */
8436       if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8437           && code == ASHIFTRT
8438           && ((nonzero_bits (varop, shift_mode)
8439                & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
8440               == 0))
8441         code = LSHIFTRT;
8442
8443       if (code == LSHIFTRT
8444           && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8445           && !(nonzero_bits (varop, shift_mode) >> count))
8446         varop = const0_rtx;
8447       if (code == ASHIFT
8448           && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8449           && !((nonzero_bits (varop, shift_mode) << count)
8450                & GET_MODE_MASK (shift_mode)))
8451         varop = const0_rtx;
8452
8453       switch (GET_CODE (varop))
8454         {
8455         case SIGN_EXTEND:
8456         case ZERO_EXTEND:
8457         case SIGN_EXTRACT:
8458         case ZERO_EXTRACT:
8459           new = expand_compound_operation (varop);
8460           if (new != varop)
8461             {
8462               varop = new;
8463               continue;
8464             }
8465           break;
8466
8467         case MEM:
8468           /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
8469              minus the width of a smaller mode, we can do this with a
8470              SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
8471           if ((code == ASHIFTRT || code == LSHIFTRT)
8472               && ! mode_dependent_address_p (XEXP (varop, 0))
8473               && ! MEM_VOLATILE_P (varop)
8474               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8475                                          MODE_INT, 1)) != BLKmode)
8476             {
8477               new = adjust_address_nv (varop, tmode,
8478                                        BYTES_BIG_ENDIAN ? 0
8479                                        : count / BITS_PER_UNIT);
8480
8481               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
8482                                      : ZERO_EXTEND, mode, new);
8483               count = 0;
8484               continue;
8485             }
8486           break;
8487
8488         case USE:
8489           /* Similar to the case above, except that we can only do this if
8490              the resulting mode is the same as that of the underlying
8491              MEM and adjust the address depending on the *bits* endianness
8492              because of the way that bit-field extract insns are defined.  */
8493           if ((code == ASHIFTRT || code == LSHIFTRT)
8494               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8495                                          MODE_INT, 1)) != BLKmode
8496               && tmode == GET_MODE (XEXP (varop, 0)))
8497             {
8498               if (BITS_BIG_ENDIAN)
8499                 new = XEXP (varop, 0);
8500               else
8501                 {
8502                   new = copy_rtx (XEXP (varop, 0));
8503                   SUBST (XEXP (new, 0),
8504                          plus_constant (XEXP (new, 0),
8505                                         count / BITS_PER_UNIT));
8506                 }
8507
8508               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
8509                                      : ZERO_EXTEND, mode, new);
8510               count = 0;
8511               continue;
8512             }
8513           break;
8514
8515         case SUBREG:
8516           /* If VAROP is a SUBREG, strip it as long as the inner operand has
8517              the same number of words as what we've seen so far.  Then store
8518              the widest mode in MODE.  */
8519           if (subreg_lowpart_p (varop)
8520               && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8521                   > GET_MODE_SIZE (GET_MODE (varop)))
8522               && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8523                                   + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
8524                  == mode_words)
8525             {
8526               varop = SUBREG_REG (varop);
8527               if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
8528                 mode = GET_MODE (varop);
8529               continue;
8530             }
8531           break;
8532
8533         case MULT:
8534           /* Some machines use MULT instead of ASHIFT because MULT
8535              is cheaper.  But it is still better on those machines to
8536              merge two shifts into one.  */
8537           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8538               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8539             {
8540               varop
8541                 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
8542                                        XEXP (varop, 0),
8543                                        GEN_INT (exact_log2 (
8544                                                 INTVAL (XEXP (varop, 1)))));
8545               continue;
8546             }
8547           break;
8548
8549         case UDIV:
8550           /* Similar, for when divides are cheaper.  */
8551           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8552               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8553             {
8554               varop
8555                 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
8556                                        XEXP (varop, 0),
8557                                        GEN_INT (exact_log2 (
8558                                                 INTVAL (XEXP (varop, 1)))));
8559               continue;
8560             }
8561           break;
8562
8563         case ASHIFTRT:
8564           /* If we are extracting just the sign bit of an arithmetic
8565              right shift, that shift is not needed.  However, the sign
8566              bit of a wider mode may be different from what would be
8567              interpreted as the sign bit in a narrower mode, so, if
8568              the result is narrower, don't discard the shift.  */
8569           if (code == LSHIFTRT
8570               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
8571               && (GET_MODE_BITSIZE (result_mode)
8572                   >= GET_MODE_BITSIZE (GET_MODE (varop))))
8573             {
8574               varop = XEXP (varop, 0);
8575               continue;
8576             }
8577
8578           /* ... fall through ...  */
8579
8580         case LSHIFTRT:
8581         case ASHIFT:
8582         case ROTATE:
8583           /* Here we have two nested shifts.  The result is usually the
8584              AND of a new shift with a mask.  We compute the result below.  */
8585           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8586               && INTVAL (XEXP (varop, 1)) >= 0
8587               && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
8588               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8589               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8590             {
8591               enum rtx_code first_code = GET_CODE (varop);
8592               unsigned int first_count = INTVAL (XEXP (varop, 1));
8593               unsigned HOST_WIDE_INT mask;
8594               rtx mask_rtx;
8595
8596               /* We have one common special case.  We can't do any merging if
8597                  the inner code is an ASHIFTRT of a smaller mode.  However, if
8598                  we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
8599                  with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
8600                  we can convert it to
8601                  (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
8602                  This simplifies certain SIGN_EXTEND operations.  */
8603               if (code == ASHIFT && first_code == ASHIFTRT
8604                   && count == (unsigned int)
8605                               (GET_MODE_BITSIZE (result_mode)
8606                                - GET_MODE_BITSIZE (GET_MODE (varop))))
8607                 {
8608                   /* C3 has the low-order C1 bits zero.  */
8609
8610                   mask = (GET_MODE_MASK (mode)
8611                           & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
8612
8613                   varop = simplify_and_const_int (NULL_RTX, result_mode,
8614                                                   XEXP (varop, 0), mask);
8615                   varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
8616                                                 varop, count);
8617                   count = first_count;
8618                   code = ASHIFTRT;
8619                   continue;
8620                 }
8621
8622               /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
8623                  than C1 high-order bits equal to the sign bit, we can convert
8624                  this to either an ASHIFT or an ASHIFTRT depending on the
8625                  two counts.
8626
8627                  We cannot do this if VAROP's mode is not SHIFT_MODE.  */
8628
8629               if (code == ASHIFTRT && first_code == ASHIFT
8630                   && GET_MODE (varop) == shift_mode
8631                   && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
8632                       > first_count))
8633                 {
8634                   varop = XEXP (varop, 0);
8635
8636                   signed_count = count - first_count;
8637                   if (signed_count < 0)
8638                     count = -signed_count, code = ASHIFT;
8639                   else
8640                     count = signed_count;
8641
8642                   continue;
8643                 }
8644
8645               /* There are some cases we can't do.  If CODE is ASHIFTRT,
8646                  we can only do this if FIRST_CODE is also ASHIFTRT.
8647
8648                  We can't do the case when CODE is ROTATE and FIRST_CODE is
8649                  ASHIFTRT.
8650
8651                  If the mode of this shift is not the mode of the outer shift,
8652                  we can't do this if either shift is a right shift or ROTATE.
8653
8654                  Finally, we can't do any of these if the mode is too wide
8655                  unless the codes are the same.
8656
8657                  Handle the case where the shift codes are the same
8658                  first.  */
8659
8660               if (code == first_code)
8661                 {
8662                   if (GET_MODE (varop) != result_mode
8663                       && (code == ASHIFTRT || code == LSHIFTRT
8664                           || code == ROTATE))
8665                     break;
8666
8667                   count += first_count;
8668                   varop = XEXP (varop, 0);
8669                   continue;
8670                 }
8671
8672               if (code == ASHIFTRT
8673                   || (code == ROTATE && first_code == ASHIFTRT)
8674                   || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
8675                   || (GET_MODE (varop) != result_mode
8676                       && (first_code == ASHIFTRT || first_code == LSHIFTRT
8677                           || first_code == ROTATE
8678                           || code == ROTATE)))
8679                 break;
8680
8681               /* To compute the mask to apply after the shift, shift the
8682                  nonzero bits of the inner shift the same way the
8683                  outer shift will.  */
8684
8685               mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
8686
8687               mask_rtx
8688                 = simplify_binary_operation (code, result_mode, mask_rtx,
8689                                              GEN_INT (count));
8690
8691               /* Give up if we can't compute an outer operation to use.  */
8692               if (mask_rtx == 0
8693                   || GET_CODE (mask_rtx) != CONST_INT
8694                   || ! merge_outer_ops (&outer_op, &outer_const, AND,
8695                                         INTVAL (mask_rtx),
8696                                         result_mode, &complement_p))
8697                 break;
8698
8699               /* If the shifts are in the same direction, we add the
8700                  counts.  Otherwise, we subtract them.  */
8701               signed_count = count;
8702               if ((code == ASHIFTRT || code == LSHIFTRT)
8703                   == (first_code == ASHIFTRT || first_code == LSHIFTRT))
8704                 signed_count += first_count;
8705               else
8706                 signed_count -= first_count;
8707
8708               /* If COUNT is positive, the new shift is usually CODE,
8709                  except for the two exceptions below, in which case it is
8710                  FIRST_CODE.  If the count is negative, FIRST_CODE should
8711                  always be used  */
8712               if (signed_count > 0
8713                   && ((first_code == ROTATE && code == ASHIFT)
8714                       || (first_code == ASHIFTRT && code == LSHIFTRT)))
8715                 code = first_code, count = signed_count;
8716               else if (signed_count < 0)
8717                 code = first_code, count = -signed_count;
8718               else
8719                 count = signed_count;
8720
8721               varop = XEXP (varop, 0);
8722               continue;
8723             }
8724
8725           /* If we have (A << B << C) for any shift, we can convert this to
8726              (A << C << B).  This wins if A is a constant.  Only try this if
8727              B is not a constant.  */
8728
8729           else if (GET_CODE (varop) == code
8730                    && GET_CODE (XEXP (varop, 1)) != CONST_INT
8731                    && 0 != (new
8732                             = simplify_binary_operation (code, mode,
8733                                                          XEXP (varop, 0),
8734                                                          GEN_INT (count))))
8735             {
8736               varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
8737               count = 0;
8738               continue;
8739             }
8740           break;
8741
8742         case NOT:
8743           /* Make this fit the case below.  */
8744           varop = gen_rtx_XOR (mode, XEXP (varop, 0),
8745                                GEN_INT (GET_MODE_MASK (mode)));
8746           continue;
8747
8748         case IOR:
8749         case AND:
8750         case XOR:
8751           /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
8752              with C the size of VAROP - 1 and the shift is logical if
8753              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8754              we have an (le X 0) operation.   If we have an arithmetic shift
8755              and STORE_FLAG_VALUE is 1 or we have a logical shift with
8756              STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
8757
8758           if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
8759               && XEXP (XEXP (varop, 0), 1) == constm1_rtx
8760               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8761               && (code == LSHIFTRT || code == ASHIFTRT)
8762               && count == (unsigned int)
8763                           (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
8764               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8765             {
8766               count = 0;
8767               varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
8768                                   const0_rtx);
8769
8770               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8771                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
8772
8773               continue;
8774             }
8775
8776           /* If we have (shift (logical)), move the logical to the outside
8777              to allow it to possibly combine with another logical and the
8778              shift to combine with another shift.  This also canonicalizes to
8779              what a ZERO_EXTRACT looks like.  Also, some machines have
8780              (and (shift)) insns.  */
8781
8782           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8783               /* We can't do this if we have (ashiftrt (xor))  and the
8784                  constant has its sign bit set in shift_mode.  */
8785               && !(code == ASHIFTRT && GET_CODE (varop) == XOR
8786                    && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
8787                                               shift_mode))
8788               && (new = simplify_binary_operation (code, result_mode,
8789                                                    XEXP (varop, 1),
8790                                                    GEN_INT (count))) != 0
8791               && GET_CODE (new) == CONST_INT
8792               && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
8793                                   INTVAL (new), result_mode, &complement_p))
8794             {
8795               varop = XEXP (varop, 0);
8796               continue;
8797             }
8798
8799           /* If we can't do that, try to simplify the shift in each arm of the
8800              logical expression, make a new logical expression, and apply
8801              the inverse distributive law.  This also can't be done
8802              for some (ashiftrt (xor)).  */
8803           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8804              && !(code == ASHIFTRT && GET_CODE (varop) == XOR
8805                   && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
8806                                              shift_mode)))
8807             {
8808               rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8809                                               XEXP (varop, 0), count);
8810               rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8811                                               XEXP (varop, 1), count);
8812
8813               varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
8814                                            lhs, rhs);
8815               varop = apply_distributive_law (varop);
8816
8817               count = 0;
8818               continue; 
8819             }
8820           break;
8821
8822         case EQ:
8823           /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
8824              says that the sign bit can be tested, FOO has mode MODE, C is
8825              GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
8826              that may be nonzero.  */
8827           if (code == LSHIFTRT
8828               && XEXP (varop, 1) == const0_rtx
8829               && GET_MODE (XEXP (varop, 0)) == result_mode
8830               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
8831               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8832               && ((STORE_FLAG_VALUE
8833                    & ((HOST_WIDE_INT) 1
8834                       < (GET_MODE_BITSIZE (result_mode) - 1))))
8835               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
8836               && merge_outer_ops (&outer_op, &outer_const, XOR,
8837                                   (HOST_WIDE_INT) 1, result_mode,
8838                                   &complement_p))
8839             {
8840               varop = XEXP (varop, 0);
8841               count = 0;
8842               continue;
8843             }
8844           break;
8845
8846         case NEG:
8847           /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
8848              than the number of bits in the mode is equivalent to A.  */
8849           if (code == LSHIFTRT
8850               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
8851               && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
8852             {
8853               varop = XEXP (varop, 0);
8854               count = 0;
8855               continue;
8856             }
8857
8858           /* NEG commutes with ASHIFT since it is multiplication.  Move the
8859              NEG outside to allow shifts to combine.  */
8860           if (code == ASHIFT
8861               && merge_outer_ops (&outer_op, &outer_const, NEG,
8862                                   (HOST_WIDE_INT) 0, result_mode,
8863                                   &complement_p))
8864             {
8865               varop = XEXP (varop, 0);
8866               continue;
8867             }
8868           break;
8869
8870         case PLUS:
8871           /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
8872              is one less than the number of bits in the mode is
8873              equivalent to (xor A 1).  */
8874           if (code == LSHIFTRT
8875               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
8876               && XEXP (varop, 1) == constm1_rtx
8877               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
8878               && merge_outer_ops (&outer_op, &outer_const, XOR,
8879                                   (HOST_WIDE_INT) 1, result_mode,
8880                                   &complement_p))
8881             {
8882               count = 0;
8883               varop = XEXP (varop, 0);
8884               continue;
8885             }
8886
8887           /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
8888              that might be nonzero in BAR are those being shifted out and those
8889              bits are known zero in FOO, we can replace the PLUS with FOO.
8890              Similarly in the other operand order.  This code occurs when
8891              we are computing the size of a variable-size array.  */
8892
8893           if ((code == ASHIFTRT || code == LSHIFTRT)
8894               && count < HOST_BITS_PER_WIDE_INT
8895               && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
8896               && (nonzero_bits (XEXP (varop, 1), result_mode)
8897                   & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
8898             {
8899               varop = XEXP (varop, 0);
8900               continue;
8901             }
8902           else if ((code == ASHIFTRT || code == LSHIFTRT)
8903                    && count < HOST_BITS_PER_WIDE_INT
8904                    && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8905                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
8906                             >> count)
8907                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
8908                             & nonzero_bits (XEXP (varop, 1),
8909                                                  result_mode)))
8910             {
8911               varop = XEXP (varop, 1);
8912               continue;
8913             }
8914
8915           /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
8916           if (code == ASHIFT
8917               && GET_CODE (XEXP (varop, 1)) == CONST_INT
8918               && (new = simplify_binary_operation (ASHIFT, result_mode,
8919                                                    XEXP (varop, 1),
8920                                                    GEN_INT (count))) != 0
8921               && GET_CODE (new) == CONST_INT
8922               && merge_outer_ops (&outer_op, &outer_const, PLUS,
8923                                   INTVAL (new), result_mode, &complement_p))
8924             {
8925               varop = XEXP (varop, 0);
8926               continue;
8927             }
8928
8929           /* Check for 'PLUS signbit', which is the canonical form of 'XOR
8930              signbit', and attempt to change the PLUS to an XOR and move it to
8931              the outer operation as is done above in the AND/IOR/XOR case
8932              leg for shift(logical). See details in logical handling above
8933              for reasoning in doing so.  */
8934           if (code == LSHIFTRT
8935               && GET_CODE (XEXP (varop, 1)) == CONST_INT
8936               && mode_signbit_p (result_mode, XEXP (varop, 1))
8937               && (new = simplify_binary_operation (code, result_mode,
8938                                                    XEXP (varop, 1),
8939                                                    GEN_INT (count))) != 0
8940               && GET_CODE (new) == CONST_INT
8941               && merge_outer_ops (&outer_op, &outer_const, XOR,
8942                                   INTVAL (new), result_mode, &complement_p))
8943             {
8944               varop = XEXP (varop, 0);
8945               continue;
8946             }
8947
8948           break;
8949
8950         case MINUS:
8951           /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
8952              with C the size of VAROP - 1 and the shift is logical if
8953              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8954              we have a (gt X 0) operation.  If the shift is arithmetic with
8955              STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
8956              we have a (neg (gt X 0)) operation.  */
8957
8958           if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8959               && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
8960               && count == (unsigned int)
8961                           (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
8962               && (code == LSHIFTRT || code == ASHIFTRT)
8963               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
8964               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (varop, 0), 1))
8965                  == count
8966               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8967             {
8968               count = 0;
8969               varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
8970                                   const0_rtx);
8971
8972               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8973                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
8974
8975               continue;
8976             }
8977           break;
8978
8979         case TRUNCATE:
8980           /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
8981              if the truncate does not affect the value.  */
8982           if (code == LSHIFTRT
8983               && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
8984               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
8985               && (INTVAL (XEXP (XEXP (varop, 0), 1))
8986                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
8987                       - GET_MODE_BITSIZE (GET_MODE (varop)))))
8988             {
8989               rtx varop_inner = XEXP (varop, 0);
8990
8991               varop_inner
8992                 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
8993                                     XEXP (varop_inner, 0),
8994                                     GEN_INT
8995                                     (count + INTVAL (XEXP (varop_inner, 1))));
8996               varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
8997               count = 0;
8998               continue;
8999             }
9000           break;
9001
9002         default:
9003           break;
9004         }
9005
9006       break;
9007     }
9008
9009   /* We need to determine what mode to do the shift in.  If the shift is
9010      a right shift or ROTATE, we must always do it in the mode it was
9011      originally done in.  Otherwise, we can do it in MODE, the widest mode
9012      encountered.  The code we care about is that of the shift that will
9013      actually be done, not the shift that was originally requested.  */
9014   shift_mode
9015     = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9016        ? result_mode : mode);
9017
9018   /* We have now finished analyzing the shift.  The result should be
9019      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
9020      OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
9021      to the result of the shift.  OUTER_CONST is the relevant constant,
9022      but we must turn off all bits turned off in the shift.
9023
9024      If we were passed a value for X, see if we can use any pieces of
9025      it.  If not, make new rtx.  */
9026
9027   if (x && GET_RTX_CLASS (GET_CODE (x)) == RTX_BIN_ARITH
9028       && GET_CODE (XEXP (x, 1)) == CONST_INT
9029       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) == count)
9030     const_rtx = XEXP (x, 1);
9031   else
9032     const_rtx = GEN_INT (count);
9033
9034   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
9035       && GET_MODE (XEXP (x, 0)) == shift_mode
9036       && SUBREG_REG (XEXP (x, 0)) == varop)
9037     varop = XEXP (x, 0);
9038   else if (GET_MODE (varop) != shift_mode)
9039     varop = gen_lowpart (shift_mode, varop);
9040
9041   /* If we can't make the SUBREG, try to return what we were given.  */
9042   if (GET_CODE (varop) == CLOBBER)
9043     return x ? x : varop;
9044
9045   new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
9046   if (new != 0)
9047     x = new;
9048   else
9049     x = gen_rtx_fmt_ee (code, shift_mode, varop, const_rtx);
9050
9051   /* If we have an outer operation and we just made a shift, it is
9052      possible that we could have simplified the shift were it not
9053      for the outer operation.  So try to do the simplification
9054      recursively.  */
9055
9056   if (outer_op != UNKNOWN && GET_CODE (x) == code
9057       && GET_CODE (XEXP (x, 1)) == CONST_INT)
9058     x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
9059                               INTVAL (XEXP (x, 1)));
9060
9061   /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9062      turn off all the bits that the shift would have turned off.  */
9063   if (orig_code == LSHIFTRT && result_mode != shift_mode)
9064     x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9065                                 GET_MODE_MASK (result_mode) >> orig_count);
9066
9067   /* Do the remainder of the processing in RESULT_MODE.  */
9068   x = gen_lowpart (result_mode, x);
9069
9070   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9071      operation.  */
9072   if (complement_p)
9073     x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9074
9075   if (outer_op != UNKNOWN)
9076     {
9077       if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9078         outer_const = trunc_int_for_mode (outer_const, result_mode);
9079
9080       if (outer_op == AND)
9081         x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9082       else if (outer_op == SET)
9083         /* This means that we have determined that the result is
9084            equivalent to a constant.  This should be rare.  */
9085         x = GEN_INT (outer_const);
9086       else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
9087         x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
9088       else
9089         x = simplify_gen_binary (outer_op, result_mode, x,
9090                                  GEN_INT (outer_const));
9091     }
9092
9093   return x;
9094 }
9095 \f
9096 /* Like recog, but we receive the address of a pointer to a new pattern.
9097    We try to match the rtx that the pointer points to.
9098    If that fails, we may try to modify or replace the pattern,
9099    storing the replacement into the same pointer object.
9100
9101    Modifications include deletion or addition of CLOBBERs.
9102
9103    PNOTES is a pointer to a location where any REG_UNUSED notes added for
9104    the CLOBBERs are placed.
9105
9106    The value is the final insn code from the pattern ultimately matched,
9107    or -1.  */
9108
9109 static int
9110 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
9111 {
9112   rtx pat = *pnewpat;
9113   int insn_code_number;
9114   int num_clobbers_to_add = 0;
9115   int i;
9116   rtx notes = 0;
9117   rtx old_notes, old_pat;
9118
9119   /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9120      we use to indicate that something didn't match.  If we find such a
9121      thing, force rejection.  */
9122   if (GET_CODE (pat) == PARALLEL)
9123     for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9124       if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9125           && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9126         return -1;
9127
9128   old_pat = PATTERN (insn);
9129   old_notes = REG_NOTES (insn);
9130   PATTERN (insn) = pat;
9131   REG_NOTES (insn) = 0;
9132
9133   insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9134
9135   /* If it isn't, there is the possibility that we previously had an insn
9136      that clobbered some register as a side effect, but the combined
9137      insn doesn't need to do that.  So try once more without the clobbers
9138      unless this represents an ASM insn.  */
9139
9140   if (insn_code_number < 0 && ! check_asm_operands (pat)
9141       && GET_CODE (pat) == PARALLEL)
9142     {
9143       int pos;
9144
9145       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9146         if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9147           {
9148             if (i != pos)
9149               SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9150             pos++;
9151           }
9152
9153       SUBST_INT (XVECLEN (pat, 0), pos);
9154
9155       if (pos == 1)
9156         pat = XVECEXP (pat, 0, 0);
9157
9158       PATTERN (insn) = pat;
9159       insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9160     }
9161   PATTERN (insn) = old_pat;
9162   REG_NOTES (insn) = old_notes;
9163
9164   /* Recognize all noop sets, these will be killed by followup pass.  */
9165   if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
9166     insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
9167
9168   /* If we had any clobbers to add, make a new pattern than contains
9169      them.  Then check to make sure that all of them are dead.  */
9170   if (num_clobbers_to_add)
9171     {
9172       rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9173                                      rtvec_alloc (GET_CODE (pat) == PARALLEL
9174                                                   ? (XVECLEN (pat, 0)
9175                                                      + num_clobbers_to_add)
9176                                                   : num_clobbers_to_add + 1));
9177
9178       if (GET_CODE (pat) == PARALLEL)
9179         for (i = 0; i < XVECLEN (pat, 0); i++)
9180           XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9181       else
9182         XVECEXP (newpat, 0, 0) = pat;
9183
9184       add_clobbers (newpat, insn_code_number);
9185
9186       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9187            i < XVECLEN (newpat, 0); i++)
9188         {
9189           if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
9190               && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9191             return -1;
9192           notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9193                                      XEXP (XVECEXP (newpat, 0, i), 0), notes);
9194         }
9195       pat = newpat;
9196     }
9197
9198   *pnewpat = pat;
9199   *pnotes = notes;
9200
9201   return insn_code_number;
9202 }
9203 \f
9204 /* Like gen_lowpart_general but for use by combine.  In combine it
9205    is not possible to create any new pseudoregs.  However, it is
9206    safe to create invalid memory addresses, because combine will
9207    try to recognize them and all they will do is make the combine
9208    attempt fail.
9209
9210    If for some reason this cannot do its job, an rtx
9211    (clobber (const_int 0)) is returned.
9212    An insn containing that will not be recognized.  */
9213
9214 static rtx
9215 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
9216 {
9217   enum machine_mode imode = GET_MODE (x);
9218   unsigned int osize = GET_MODE_SIZE (omode);
9219   unsigned int isize = GET_MODE_SIZE (imode);
9220   rtx result;
9221
9222   if (omode == imode)
9223     return x;
9224
9225   /* Return identity if this is a CONST or symbolic reference.  */
9226   if (omode == Pmode
9227       && (GET_CODE (x) == CONST
9228           || GET_CODE (x) == SYMBOL_REF
9229           || GET_CODE (x) == LABEL_REF))
9230     return x;
9231
9232   /* We can only support MODE being wider than a word if X is a
9233      constant integer or has a mode the same size.  */
9234   if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
9235       && ! ((imode == VOIDmode
9236              && (GET_CODE (x) == CONST_INT
9237                  || GET_CODE (x) == CONST_DOUBLE))
9238             || isize == osize))
9239     goto fail;
9240
9241   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
9242      won't know what to do.  So we will strip off the SUBREG here and
9243      process normally.  */
9244   if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
9245     {
9246       x = SUBREG_REG (x);
9247
9248       /* For use in case we fall down into the address adjustments
9249          further below, we need to adjust the known mode and size of
9250          x; imode and isize, since we just adjusted x.  */
9251       imode = GET_MODE (x);
9252
9253       if (imode == omode)
9254         return x;
9255
9256       isize = GET_MODE_SIZE (imode);
9257     }
9258
9259   result = gen_lowpart_common (omode, x);
9260
9261 #ifdef CANNOT_CHANGE_MODE_CLASS
9262   if (result != 0 && GET_CODE (result) == SUBREG)
9263     record_subregs_of_mode (result);
9264 #endif
9265
9266   if (result)
9267     return result;
9268
9269   if (MEM_P (x))
9270     {
9271       int offset = 0;
9272
9273       /* Refuse to work on a volatile memory ref or one with a mode-dependent
9274          address.  */
9275       if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9276         goto fail;
9277
9278       /* If we want to refer to something bigger than the original memref,
9279          generate a paradoxical subreg instead.  That will force a reload
9280          of the original memref X.  */
9281       if (isize < osize)
9282         return gen_rtx_SUBREG (omode, x, 0);
9283
9284       if (WORDS_BIG_ENDIAN)
9285         offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
9286
9287       /* Adjust the address so that the address-after-the-data is
9288          unchanged.  */
9289       if (BYTES_BIG_ENDIAN)
9290         offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
9291
9292       return adjust_address_nv (x, omode, offset);
9293     }
9294
9295   /* If X is a comparison operator, rewrite it in a new mode.  This
9296      probably won't match, but may allow further simplifications.  */
9297   else if (COMPARISON_P (x))
9298     return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
9299
9300   /* If we couldn't simplify X any other way, just enclose it in a
9301      SUBREG.  Normally, this SUBREG won't match, but some patterns may
9302      include an explicit SUBREG or we may simplify it further in combine.  */
9303   else
9304     {
9305       int offset = 0;
9306       rtx res;
9307
9308       offset = subreg_lowpart_offset (omode, imode);
9309       if (imode == VOIDmode)
9310         {
9311           imode = int_mode_for_mode (omode);
9312           x = gen_lowpart_common (imode, x);
9313           if (x == NULL)
9314             goto fail;
9315         }
9316       res = simplify_gen_subreg (omode, x, imode, offset);
9317       if (res)
9318         return res;
9319     }
9320
9321  fail:
9322   return gen_rtx_CLOBBER (imode, const0_rtx);
9323 }
9324 \f
9325 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9326    comparison code that will be tested.
9327
9328    The result is a possibly different comparison code to use.  *POP0 and
9329    *POP1 may be updated.
9330
9331    It is possible that we might detect that a comparison is either always
9332    true or always false.  However, we do not perform general constant
9333    folding in combine, so this knowledge isn't useful.  Such tautologies
9334    should have been detected earlier.  Hence we ignore all such cases.  */
9335
9336 static enum rtx_code
9337 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
9338 {
9339   rtx op0 = *pop0;
9340   rtx op1 = *pop1;
9341   rtx tem, tem1;
9342   int i;
9343   enum machine_mode mode, tmode;
9344
9345   /* Try a few ways of applying the same transformation to both operands.  */
9346   while (1)
9347     {
9348 #ifndef WORD_REGISTER_OPERATIONS
9349       /* The test below this one won't handle SIGN_EXTENDs on these machines,
9350          so check specially.  */
9351       if (code != GTU && code != GEU && code != LTU && code != LEU
9352           && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9353           && GET_CODE (XEXP (op0, 0)) == ASHIFT
9354           && GET_CODE (XEXP (op1, 0)) == ASHIFT
9355           && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9356           && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9357           && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9358               == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9359           && GET_CODE (XEXP (op0, 1)) == CONST_INT
9360           && XEXP (op0, 1) == XEXP (op1, 1)
9361           && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
9362           && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
9363           && (INTVAL (XEXP (op0, 1))
9364               == (GET_MODE_BITSIZE (GET_MODE (op0))
9365                   - (GET_MODE_BITSIZE
9366                      (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9367         {
9368           op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9369           op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9370         }
9371 #endif
9372
9373       /* If both operands are the same constant shift, see if we can ignore the
9374          shift.  We can if the shift is a rotate or if the bits shifted out of
9375          this shift are known to be zero for both inputs and if the type of
9376          comparison is compatible with the shift.  */
9377       if (GET_CODE (op0) == GET_CODE (op1)
9378           && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9379           && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
9380               || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
9381                   && (code != GT && code != LT && code != GE && code != LE))
9382               || (GET_CODE (op0) == ASHIFTRT
9383                   && (code != GTU && code != LTU
9384                       && code != GEU && code != LEU)))
9385           && GET_CODE (XEXP (op0, 1)) == CONST_INT
9386           && INTVAL (XEXP (op0, 1)) >= 0
9387           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9388           && XEXP (op0, 1) == XEXP (op1, 1))
9389         {
9390           enum machine_mode mode = GET_MODE (op0);
9391           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9392           int shift_count = INTVAL (XEXP (op0, 1));
9393
9394           if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9395             mask &= (mask >> shift_count) << shift_count;
9396           else if (GET_CODE (op0) == ASHIFT)
9397             mask = (mask & (mask << shift_count)) >> shift_count;
9398
9399           if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
9400               && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
9401             op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9402           else
9403             break;
9404         }
9405
9406       /* If both operands are AND's of a paradoxical SUBREG by constant, the
9407          SUBREGs are of the same mode, and, in both cases, the AND would
9408          be redundant if the comparison was done in the narrower mode,
9409          do the comparison in the narrower mode (e.g., we are AND'ing with 1
9410          and the operand's possibly nonzero bits are 0xffffff01; in that case
9411          if we only care about QImode, we don't need the AND).  This case
9412          occurs if the output mode of an scc insn is not SImode and
9413          STORE_FLAG_VALUE == 1 (e.g., the 386).
9414
9415          Similarly, check for a case where the AND's are ZERO_EXTEND
9416          operations from some narrower mode even though a SUBREG is not
9417          present.  */
9418
9419       else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9420                && GET_CODE (XEXP (op0, 1)) == CONST_INT
9421                && GET_CODE (XEXP (op1, 1)) == CONST_INT)
9422         {
9423           rtx inner_op0 = XEXP (op0, 0);
9424           rtx inner_op1 = XEXP (op1, 0);
9425           HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
9426           HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
9427           int changed = 0;
9428
9429           if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
9430               && (GET_MODE_SIZE (GET_MODE (inner_op0))
9431                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
9432               && (GET_MODE (SUBREG_REG (inner_op0))
9433                   == GET_MODE (SUBREG_REG (inner_op1)))
9434               && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
9435                   <= HOST_BITS_PER_WIDE_INT)
9436               && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
9437                                              GET_MODE (SUBREG_REG (inner_op0)))))
9438               && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
9439                                              GET_MODE (SUBREG_REG (inner_op1))))))
9440             {
9441               op0 = SUBREG_REG (inner_op0);
9442               op1 = SUBREG_REG (inner_op1);
9443
9444               /* The resulting comparison is always unsigned since we masked
9445                  off the original sign bit.  */
9446               code = unsigned_condition (code);
9447
9448               changed = 1;
9449             }
9450
9451           else if (c0 == c1)
9452             for (tmode = GET_CLASS_NARROWEST_MODE
9453                  (GET_MODE_CLASS (GET_MODE (op0)));
9454                  tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
9455               if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
9456                 {
9457                   op0 = gen_lowpart (tmode, inner_op0);
9458                   op1 = gen_lowpart (tmode, inner_op1);
9459                   code = unsigned_condition (code);
9460                   changed = 1;
9461                   break;
9462                 }
9463
9464           if (! changed)
9465             break;
9466         }
9467
9468       /* If both operands are NOT, we can strip off the outer operation
9469          and adjust the comparison code for swapped operands; similarly for
9470          NEG, except that this must be an equality comparison.  */
9471       else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
9472                || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
9473                    && (code == EQ || code == NE)))
9474         op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
9475
9476       else
9477         break;
9478     }
9479
9480   /* If the first operand is a constant, swap the operands and adjust the
9481      comparison code appropriately, but don't do this if the second operand
9482      is already a constant integer.  */
9483   if (swap_commutative_operands_p (op0, op1))
9484     {
9485       tem = op0, op0 = op1, op1 = tem;
9486       code = swap_condition (code);
9487     }
9488
9489   /* We now enter a loop during which we will try to simplify the comparison.
9490      For the most part, we only are concerned with comparisons with zero,
9491      but some things may really be comparisons with zero but not start
9492      out looking that way.  */
9493
9494   while (GET_CODE (op1) == CONST_INT)
9495     {
9496       enum machine_mode mode = GET_MODE (op0);
9497       unsigned int mode_width = GET_MODE_BITSIZE (mode);
9498       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9499       int equality_comparison_p;
9500       int sign_bit_comparison_p;
9501       int unsigned_comparison_p;
9502       HOST_WIDE_INT const_op;
9503
9504       /* We only want to handle integral modes.  This catches VOIDmode,
9505          CCmode, and the floating-point modes.  An exception is that we
9506          can handle VOIDmode if OP0 is a COMPARE or a comparison
9507          operation.  */
9508
9509       if (GET_MODE_CLASS (mode) != MODE_INT
9510           && ! (mode == VOIDmode
9511                 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
9512         break;
9513
9514       /* Get the constant we are comparing against and turn off all bits
9515          not on in our mode.  */
9516       const_op = INTVAL (op1);
9517       if (mode != VOIDmode)
9518         const_op = trunc_int_for_mode (const_op, mode);
9519       op1 = GEN_INT (const_op);
9520
9521       /* If we are comparing against a constant power of two and the value
9522          being compared can only have that single bit nonzero (e.g., it was
9523          `and'ed with that bit), we can replace this with a comparison
9524          with zero.  */
9525       if (const_op
9526           && (code == EQ || code == NE || code == GE || code == GEU
9527               || code == LT || code == LTU)
9528           && mode_width <= HOST_BITS_PER_WIDE_INT
9529           && exact_log2 (const_op) >= 0
9530           && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
9531         {
9532           code = (code == EQ || code == GE || code == GEU ? NE : EQ);
9533           op1 = const0_rtx, const_op = 0;
9534         }
9535
9536       /* Similarly, if we are comparing a value known to be either -1 or
9537          0 with -1, change it to the opposite comparison against zero.  */
9538
9539       if (const_op == -1
9540           && (code == EQ || code == NE || code == GT || code == LE
9541               || code == GEU || code == LTU)
9542           && num_sign_bit_copies (op0, mode) == mode_width)
9543         {
9544           code = (code == EQ || code == LE || code == GEU ? NE : EQ);
9545           op1 = const0_rtx, const_op = 0;
9546         }
9547
9548       /* Do some canonicalizations based on the comparison code.  We prefer
9549          comparisons against zero and then prefer equality comparisons.
9550          If we can reduce the size of a constant, we will do that too.  */
9551
9552       switch (code)
9553         {
9554         case LT:
9555           /* < C is equivalent to <= (C - 1) */
9556           if (const_op > 0)
9557             {
9558               const_op -= 1;
9559               op1 = GEN_INT (const_op);
9560               code = LE;
9561               /* ... fall through to LE case below.  */
9562             }
9563           else
9564             break;
9565
9566         case LE:
9567           /* <= C is equivalent to < (C + 1); we do this for C < 0  */
9568           if (const_op < 0)
9569             {
9570               const_op += 1;
9571               op1 = GEN_INT (const_op);
9572               code = LT;
9573             }
9574
9575           /* If we are doing a <= 0 comparison on a value known to have
9576              a zero sign bit, we can replace this with == 0.  */
9577           else if (const_op == 0
9578                    && mode_width <= HOST_BITS_PER_WIDE_INT
9579                    && (nonzero_bits (op0, mode)
9580                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9581             code = EQ;
9582           break;
9583
9584         case GE:
9585           /* >= C is equivalent to > (C - 1).  */
9586           if (const_op > 0)
9587             {
9588               const_op -= 1;
9589               op1 = GEN_INT (const_op);
9590               code = GT;
9591               /* ... fall through to GT below.  */
9592             }
9593           else
9594             break;
9595
9596         case GT:
9597           /* > C is equivalent to >= (C + 1); we do this for C < 0.  */
9598           if (const_op < 0)
9599             {
9600               const_op += 1;
9601               op1 = GEN_INT (const_op);
9602               code = GE;
9603             }
9604
9605           /* If we are doing a > 0 comparison on a value known to have
9606              a zero sign bit, we can replace this with != 0.  */
9607           else if (const_op == 0
9608                    && mode_width <= HOST_BITS_PER_WIDE_INT
9609                    && (nonzero_bits (op0, mode)
9610                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9611             code = NE;
9612           break;
9613
9614         case LTU:
9615           /* < C is equivalent to <= (C - 1).  */
9616           if (const_op > 0)
9617             {
9618               const_op -= 1;
9619               op1 = GEN_INT (const_op);
9620               code = LEU;
9621               /* ... fall through ...  */
9622             }
9623
9624           /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
9625           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9626                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9627             {
9628               const_op = 0, op1 = const0_rtx;
9629               code = GE;
9630               break;
9631             }
9632           else
9633             break;
9634
9635         case LEU:
9636           /* unsigned <= 0 is equivalent to == 0 */
9637           if (const_op == 0)
9638             code = EQ;
9639
9640           /* (unsigned) <= 0x7fffffff is equivalent to >= 0.  */
9641           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9642                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9643             {
9644               const_op = 0, op1 = const0_rtx;
9645               code = GE;
9646             }
9647           break;
9648
9649         case GEU:
9650           /* >= C is equivalent to > (C - 1).  */
9651           if (const_op > 1)
9652             {
9653               const_op -= 1;
9654               op1 = GEN_INT (const_op);
9655               code = GTU;
9656               /* ... fall through ...  */
9657             }
9658
9659           /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
9660           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9661                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9662             {
9663               const_op = 0, op1 = const0_rtx;
9664               code = LT;
9665               break;
9666             }
9667           else
9668             break;
9669
9670         case GTU:
9671           /* unsigned > 0 is equivalent to != 0 */
9672           if (const_op == 0)
9673             code = NE;
9674
9675           /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
9676           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9677                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9678             {
9679               const_op = 0, op1 = const0_rtx;
9680               code = LT;
9681             }
9682           break;
9683
9684         default:
9685           break;
9686         }
9687
9688       /* Compute some predicates to simplify code below.  */
9689
9690       equality_comparison_p = (code == EQ || code == NE);
9691       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
9692       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
9693                                || code == GEU);
9694
9695       /* If this is a sign bit comparison and we can do arithmetic in
9696          MODE, say that we will only be needing the sign bit of OP0.  */
9697       if (sign_bit_comparison_p
9698           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9699         op0 = force_to_mode (op0, mode,
9700                              ((HOST_WIDE_INT) 1
9701                               << (GET_MODE_BITSIZE (mode) - 1)),
9702                              0);
9703
9704       /* Now try cases based on the opcode of OP0.  If none of the cases
9705          does a "continue", we exit this loop immediately after the
9706          switch.  */
9707
9708       switch (GET_CODE (op0))
9709         {
9710         case ZERO_EXTRACT:
9711           /* If we are extracting a single bit from a variable position in
9712              a constant that has only a single bit set and are comparing it
9713              with zero, we can convert this into an equality comparison
9714              between the position and the location of the single bit.  */
9715           /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
9716              have already reduced the shift count modulo the word size.  */
9717           if (!SHIFT_COUNT_TRUNCATED
9718               && GET_CODE (XEXP (op0, 0)) == CONST_INT
9719               && XEXP (op0, 1) == const1_rtx
9720               && equality_comparison_p && const_op == 0
9721               && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
9722             {
9723               if (BITS_BIG_ENDIAN)
9724                 {
9725                   enum machine_mode new_mode
9726                     = mode_for_extraction (EP_extzv, 1);
9727                   if (new_mode == MAX_MACHINE_MODE)
9728                     i = BITS_PER_WORD - 1 - i;
9729                   else
9730                     {
9731                       mode = new_mode;
9732                       i = (GET_MODE_BITSIZE (mode) - 1 - i);
9733                     }
9734                 }
9735
9736               op0 = XEXP (op0, 2);
9737               op1 = GEN_INT (i);
9738               const_op = i;
9739
9740               /* Result is nonzero iff shift count is equal to I.  */
9741               code = reverse_condition (code);
9742               continue;
9743             }
9744
9745           /* ... fall through ...  */
9746
9747         case SIGN_EXTRACT:
9748           tem = expand_compound_operation (op0);
9749           if (tem != op0)
9750             {
9751               op0 = tem;
9752               continue;
9753             }
9754           break;
9755
9756         case NOT:
9757           /* If testing for equality, we can take the NOT of the constant.  */
9758           if (equality_comparison_p
9759               && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
9760             {
9761               op0 = XEXP (op0, 0);
9762               op1 = tem;
9763               continue;
9764             }
9765
9766           /* If just looking at the sign bit, reverse the sense of the
9767              comparison.  */
9768           if (sign_bit_comparison_p)
9769             {
9770               op0 = XEXP (op0, 0);
9771               code = (code == GE ? LT : GE);
9772               continue;
9773             }
9774           break;
9775
9776         case NEG:
9777           /* If testing for equality, we can take the NEG of the constant.  */
9778           if (equality_comparison_p
9779               && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
9780             {
9781               op0 = XEXP (op0, 0);
9782               op1 = tem;
9783               continue;
9784             }
9785
9786           /* The remaining cases only apply to comparisons with zero.  */
9787           if (const_op != 0)
9788             break;
9789
9790           /* When X is ABS or is known positive,
9791              (neg X) is < 0 if and only if X != 0.  */
9792
9793           if (sign_bit_comparison_p
9794               && (GET_CODE (XEXP (op0, 0)) == ABS
9795                   || (mode_width <= HOST_BITS_PER_WIDE_INT
9796                       && (nonzero_bits (XEXP (op0, 0), mode)
9797                           & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
9798             {
9799               op0 = XEXP (op0, 0);
9800               code = (code == LT ? NE : EQ);
9801               continue;
9802             }
9803
9804           /* If we have NEG of something whose two high-order bits are the
9805              same, we know that "(-a) < 0" is equivalent to "a > 0".  */
9806           if (num_sign_bit_copies (op0, mode) >= 2)
9807             {
9808               op0 = XEXP (op0, 0);
9809               code = swap_condition (code);
9810               continue;
9811             }
9812           break;
9813
9814         case ROTATE:
9815           /* If we are testing equality and our count is a constant, we
9816              can perform the inverse operation on our RHS.  */
9817           if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
9818               && (tem = simplify_binary_operation (ROTATERT, mode,
9819                                                    op1, XEXP (op0, 1))) != 0)
9820             {
9821               op0 = XEXP (op0, 0);
9822               op1 = tem;
9823               continue;
9824             }
9825
9826           /* If we are doing a < 0 or >= 0 comparison, it means we are testing
9827              a particular bit.  Convert it to an AND of a constant of that
9828              bit.  This will be converted into a ZERO_EXTRACT.  */
9829           if (const_op == 0 && sign_bit_comparison_p
9830               && GET_CODE (XEXP (op0, 1)) == CONST_INT
9831               && mode_width <= HOST_BITS_PER_WIDE_INT)
9832             {
9833               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
9834                                             ((HOST_WIDE_INT) 1
9835                                              << (mode_width - 1
9836                                                  - INTVAL (XEXP (op0, 1)))));
9837               code = (code == LT ? NE : EQ);
9838               continue;
9839             }
9840
9841           /* Fall through.  */
9842
9843         case ABS:
9844           /* ABS is ignorable inside an equality comparison with zero.  */
9845           if (const_op == 0 && equality_comparison_p)
9846             {
9847               op0 = XEXP (op0, 0);
9848               continue;
9849             }
9850           break;
9851
9852         case SIGN_EXTEND:
9853           /* Can simplify (compare (zero/sign_extend FOO) CONST) to
9854              (compare FOO CONST) if CONST fits in FOO's mode and we
9855              are either testing inequality or have an unsigned
9856              comparison with ZERO_EXTEND or a signed comparison with
9857              SIGN_EXTEND.  But don't do it if we don't have a compare
9858              insn of the given mode, since we'd have to revert it
9859              later on, and then we wouldn't know whether to sign- or
9860              zero-extend.  */
9861           mode = GET_MODE (XEXP (op0, 0));
9862           if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
9863               && ! unsigned_comparison_p
9864               && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9865               && ((unsigned HOST_WIDE_INT) const_op
9866                   < (((unsigned HOST_WIDE_INT) 1 
9867                       << (GET_MODE_BITSIZE (mode) - 1))))
9868               && cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
9869             {
9870               op0 = XEXP (op0, 0);
9871               continue;
9872             }
9873           break;
9874
9875         case SUBREG:
9876           /* Check for the case where we are comparing A - C1 with C2, that is
9877
9878                (subreg:MODE (plus (A) (-C1))) op (C2)
9879
9880              with C1 a constant, and try to lift the SUBREG, i.e. to do the
9881              comparison in the wider mode.  One of the following two conditions
9882              must be true in order for this to be valid:
9883
9884                1. The mode extension results in the same bit pattern being added
9885                   on both sides and the comparison is equality or unsigned.  As
9886                   C2 has been truncated to fit in MODE, the pattern can only be
9887                   all 0s or all 1s.
9888
9889                2. The mode extension results in the sign bit being copied on
9890                   each side.
9891
9892              The difficulty here is that we have predicates for A but not for
9893              (A - C1) so we need to check that C1 is within proper bounds so
9894              as to perturbate A as little as possible.  */
9895
9896           if (mode_width <= HOST_BITS_PER_WIDE_INT
9897               && subreg_lowpart_p (op0)
9898               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
9899               && GET_CODE (SUBREG_REG (op0)) == PLUS
9900               && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT)
9901             {
9902               enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
9903               rtx a = XEXP (SUBREG_REG (op0), 0);
9904               HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
9905
9906               if ((c1 > 0
9907                    && (unsigned HOST_WIDE_INT) c1
9908                        < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
9909                    && (equality_comparison_p || unsigned_comparison_p)
9910                    /* (A - C1) zero-extends if it is positive and sign-extends
9911                       if it is negative, C2 both zero- and sign-extends.  */
9912                    && ((0 == (nonzero_bits (a, inner_mode)
9913                               & ~GET_MODE_MASK (mode))
9914                         && const_op >= 0)
9915                        /* (A - C1) sign-extends if it is positive and 1-extends
9916                           if it is negative, C2 both sign- and 1-extends.  */
9917                        || (num_sign_bit_copies (a, inner_mode)
9918                            > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
9919                                              - mode_width)
9920                            && const_op < 0)))
9921                   || ((unsigned HOST_WIDE_INT) c1
9922                        < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
9923                       /* (A - C1) always sign-extends, like C2.  */
9924                       && num_sign_bit_copies (a, inner_mode)
9925                          > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
9926                                            - (mode_width - 1))))
9927                 {
9928                   op0 = SUBREG_REG (op0);
9929                   continue;
9930                 }
9931             }
9932
9933           /* If the inner mode is narrower and we are extracting the low part,
9934              we can treat the SUBREG as if it were a ZERO_EXTEND.  */
9935           if (subreg_lowpart_p (op0)
9936               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
9937             /* Fall through */ ;
9938           else
9939             break;
9940
9941           /* ... fall through ...  */
9942
9943         case ZERO_EXTEND:
9944           mode = GET_MODE (XEXP (op0, 0));
9945           if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
9946               && (unsigned_comparison_p || equality_comparison_p)
9947               && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9948               && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
9949               && cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
9950             {
9951               op0 = XEXP (op0, 0);
9952               continue;
9953             }
9954           break;
9955
9956         case PLUS:
9957           /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
9958              this for equality comparisons due to pathological cases involving
9959              overflows.  */
9960           if (equality_comparison_p
9961               && 0 != (tem = simplify_binary_operation (MINUS, mode,
9962                                                         op1, XEXP (op0, 1))))
9963             {
9964               op0 = XEXP (op0, 0);
9965               op1 = tem;
9966               continue;
9967             }
9968
9969           /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
9970           if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
9971               && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
9972             {
9973               op0 = XEXP (XEXP (op0, 0), 0);
9974               code = (code == LT ? EQ : NE);
9975               continue;
9976             }
9977           break;
9978
9979         case MINUS:
9980           /* We used to optimize signed comparisons against zero, but that
9981              was incorrect.  Unsigned comparisons against zero (GTU, LEU)
9982              arrive here as equality comparisons, or (GEU, LTU) are
9983              optimized away.  No need to special-case them.  */
9984
9985           /* (eq (minus A B) C) -> (eq A (plus B C)) or
9986              (eq B (minus A C)), whichever simplifies.  We can only do
9987              this for equality comparisons due to pathological cases involving
9988              overflows.  */
9989           if (equality_comparison_p
9990               && 0 != (tem = simplify_binary_operation (PLUS, mode,
9991                                                         XEXP (op0, 1), op1)))
9992             {
9993               op0 = XEXP (op0, 0);
9994               op1 = tem;
9995               continue;
9996             }
9997
9998           if (equality_comparison_p
9999               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10000                                                         XEXP (op0, 0), op1)))
10001             {
10002               op0 = XEXP (op0, 1);
10003               op1 = tem;
10004               continue;
10005             }
10006
10007           /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10008              of bits in X minus 1, is one iff X > 0.  */
10009           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10010               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10011               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10012                  == mode_width - 1
10013               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10014             {
10015               op0 = XEXP (op0, 1);
10016               code = (code == GE ? LE : GT);
10017               continue;
10018             }
10019           break;
10020
10021         case XOR:
10022           /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
10023              if C is zero or B is a constant.  */
10024           if (equality_comparison_p
10025               && 0 != (tem = simplify_binary_operation (XOR, mode,
10026                                                         XEXP (op0, 1), op1)))
10027             {
10028               op0 = XEXP (op0, 0);
10029               op1 = tem;
10030               continue;
10031             }
10032           break;
10033
10034         case EQ:  case NE:
10035         case UNEQ:  case LTGT:
10036         case LT:  case LTU:  case UNLT:  case LE:  case LEU:  case UNLE:
10037         case GT:  case GTU:  case UNGT:  case GE:  case GEU:  case UNGE:
10038         case UNORDERED: case ORDERED:
10039           /* We can't do anything if OP0 is a condition code value, rather
10040              than an actual data value.  */
10041           if (const_op != 0
10042               || CC0_P (XEXP (op0, 0))
10043               || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10044             break;
10045
10046           /* Get the two operands being compared.  */
10047           if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10048             tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10049           else
10050             tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10051
10052           /* Check for the cases where we simply want the result of the
10053              earlier test or the opposite of that result.  */
10054           if (code == NE || code == EQ
10055               || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10056                   && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10057                   && (STORE_FLAG_VALUE
10058                       & (((HOST_WIDE_INT) 1
10059                           << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10060                   && (code == LT || code == GE)))
10061             {
10062               enum rtx_code new_code;
10063               if (code == LT || code == NE)
10064                 new_code = GET_CODE (op0);
10065               else
10066                 new_code = reversed_comparison_code (op0, NULL);
10067
10068               if (new_code != UNKNOWN)
10069                 {
10070                   code = new_code;
10071                   op0 = tem;
10072                   op1 = tem1;
10073                   continue;
10074                 }
10075             }
10076           break;
10077
10078         case IOR:
10079           /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10080              iff X <= 0.  */
10081           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10082               && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10083               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10084             {
10085               op0 = XEXP (op0, 1);
10086               code = (code == GE ? GT : LE);
10087               continue;
10088             }
10089           break;
10090
10091         case AND:
10092           /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
10093              will be converted to a ZERO_EXTRACT later.  */
10094           if (const_op == 0 && equality_comparison_p
10095               && GET_CODE (XEXP (op0, 0)) == ASHIFT
10096               && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10097             {
10098               op0 = simplify_and_const_int
10099                 (op0, mode, gen_rtx_LSHIFTRT (mode,
10100                                               XEXP (op0, 1),
10101                                               XEXP (XEXP (op0, 0), 1)),
10102                  (HOST_WIDE_INT) 1);
10103               continue;
10104             }
10105
10106           /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10107              zero and X is a comparison and C1 and C2 describe only bits set
10108              in STORE_FLAG_VALUE, we can compare with X.  */
10109           if (const_op == 0 && equality_comparison_p
10110               && mode_width <= HOST_BITS_PER_WIDE_INT
10111               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10112               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10113               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10114               && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10115               && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10116             {
10117               mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10118                       << INTVAL (XEXP (XEXP (op0, 0), 1)));
10119               if ((~STORE_FLAG_VALUE & mask) == 0
10120                   && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
10121                       || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10122                           && COMPARISON_P (tem))))
10123                 {
10124                   op0 = XEXP (XEXP (op0, 0), 0);
10125                   continue;
10126                 }
10127             }
10128
10129           /* If we are doing an equality comparison of an AND of a bit equal
10130              to the sign bit, replace this with a LT or GE comparison of
10131              the underlying value.  */
10132           if (equality_comparison_p
10133               && const_op == 0
10134               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10135               && mode_width <= HOST_BITS_PER_WIDE_INT
10136               && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10137                   == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10138             {
10139               op0 = XEXP (op0, 0);
10140               code = (code == EQ ? GE : LT);
10141               continue;
10142             }
10143
10144           /* If this AND operation is really a ZERO_EXTEND from a narrower
10145              mode, the constant fits within that mode, and this is either an
10146              equality or unsigned comparison, try to do this comparison in
10147              the narrower mode.  */
10148           if ((equality_comparison_p || unsigned_comparison_p)
10149               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10150               && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10151                                    & GET_MODE_MASK (mode))
10152                                   + 1)) >= 0
10153               && const_op >> i == 0
10154               && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
10155             {
10156               op0 = gen_lowpart (tmode, XEXP (op0, 0));
10157               continue;
10158             }
10159
10160           /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
10161              fits in both M1 and M2 and the SUBREG is either paradoxical
10162              or represents the low part, permute the SUBREG and the AND
10163              and try again.  */
10164           if (GET_CODE (XEXP (op0, 0)) == SUBREG)
10165             {
10166               unsigned HOST_WIDE_INT c1;
10167               tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
10168               /* Require an integral mode, to avoid creating something like
10169                  (AND:SF ...).  */
10170               if (SCALAR_INT_MODE_P (tmode)
10171                   /* It is unsafe to commute the AND into the SUBREG if the
10172                      SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
10173                      not defined.  As originally written the upper bits
10174                      have a defined value due to the AND operation.
10175                      However, if we commute the AND inside the SUBREG then
10176                      they no longer have defined values and the meaning of
10177                      the code has been changed.  */
10178                   && (0
10179 #ifdef WORD_REGISTER_OPERATIONS
10180                       || (mode_width > GET_MODE_BITSIZE (tmode)
10181                           && mode_width <= BITS_PER_WORD)
10182 #endif
10183                       || (mode_width <= GET_MODE_BITSIZE (tmode)
10184                           && subreg_lowpart_p (XEXP (op0, 0))))
10185                   && GET_CODE (XEXP (op0, 1)) == CONST_INT
10186                   && mode_width <= HOST_BITS_PER_WIDE_INT
10187                   && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
10188                   && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
10189                   && (c1 & ~GET_MODE_MASK (tmode)) == 0
10190                   && c1 != mask
10191                   && c1 != GET_MODE_MASK (tmode))
10192                 {
10193                   op0 = simplify_gen_binary (AND, tmode,
10194                                              SUBREG_REG (XEXP (op0, 0)),
10195                                              gen_int_mode (c1, tmode));
10196                   op0 = gen_lowpart (mode, op0);
10197                   continue;
10198                 }
10199             }
10200
10201           /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0).  */
10202           if (const_op == 0 && equality_comparison_p
10203               && XEXP (op0, 1) == const1_rtx
10204               && GET_CODE (XEXP (op0, 0)) == NOT)
10205             {
10206               op0 = simplify_and_const_int
10207                 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
10208               code = (code == NE ? EQ : NE);
10209               continue;
10210             }
10211
10212           /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
10213              (eq (and (lshiftrt X) 1) 0).
10214              Also handle the case where (not X) is expressed using xor.  */
10215           if (const_op == 0 && equality_comparison_p
10216               && XEXP (op0, 1) == const1_rtx
10217               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
10218             {
10219               rtx shift_op = XEXP (XEXP (op0, 0), 0);
10220               rtx shift_count = XEXP (XEXP (op0, 0), 1);
10221
10222               if (GET_CODE (shift_op) == NOT
10223                   || (GET_CODE (shift_op) == XOR
10224                       && GET_CODE (XEXP (shift_op, 1)) == CONST_INT
10225                       && GET_CODE (shift_count) == CONST_INT
10226                       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10227                       && (INTVAL (XEXP (shift_op, 1))
10228                           == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
10229                 {
10230                   op0 = simplify_and_const_int
10231                     (NULL_RTX, mode,
10232                      gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
10233                      (HOST_WIDE_INT) 1);
10234                   code = (code == NE ? EQ : NE);
10235                   continue;
10236                 }
10237             }
10238           break;
10239
10240         case ASHIFT:
10241           /* If we have (compare (ashift FOO N) (const_int C)) and
10242              the high order N bits of FOO (N+1 if an inequality comparison)
10243              are known to be zero, we can do this by comparing FOO with C
10244              shifted right N bits so long as the low-order N bits of C are
10245              zero.  */
10246           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10247               && INTVAL (XEXP (op0, 1)) >= 0
10248               && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10249                   < HOST_BITS_PER_WIDE_INT)
10250               && ((const_op
10251                    & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10252               && mode_width <= HOST_BITS_PER_WIDE_INT
10253               && (nonzero_bits (XEXP (op0, 0), mode)
10254                   & ~(mask >> (INTVAL (XEXP (op0, 1))
10255                                + ! equality_comparison_p))) == 0)
10256             {
10257               /* We must perform a logical shift, not an arithmetic one,
10258                  as we want the top N bits of C to be zero.  */
10259               unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
10260
10261               temp >>= INTVAL (XEXP (op0, 1));
10262               op1 = gen_int_mode (temp, mode);
10263               op0 = XEXP (op0, 0);
10264               continue;
10265             }
10266
10267           /* If we are doing a sign bit comparison, it means we are testing
10268              a particular bit.  Convert it to the appropriate AND.  */
10269           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10270               && mode_width <= HOST_BITS_PER_WIDE_INT)
10271             {
10272               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10273                                             ((HOST_WIDE_INT) 1
10274                                              << (mode_width - 1
10275                                                  - INTVAL (XEXP (op0, 1)))));
10276               code = (code == LT ? NE : EQ);
10277               continue;
10278             }
10279
10280           /* If this an equality comparison with zero and we are shifting
10281              the low bit to the sign bit, we can convert this to an AND of the
10282              low-order bit.  */
10283           if (const_op == 0 && equality_comparison_p
10284               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10285               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10286                  == mode_width - 1)
10287             {
10288               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10289                                             (HOST_WIDE_INT) 1);
10290               continue;
10291             }
10292           break;
10293
10294         case ASHIFTRT:
10295           /* If this is an equality comparison with zero, we can do this
10296              as a logical shift, which might be much simpler.  */
10297           if (equality_comparison_p && const_op == 0
10298               && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10299             {
10300               op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10301                                           XEXP (op0, 0),
10302                                           INTVAL (XEXP (op0, 1)));
10303               continue;
10304             }
10305
10306           /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10307              do the comparison in a narrower mode.  */
10308           if (! unsigned_comparison_p
10309               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10310               && GET_CODE (XEXP (op0, 0)) == ASHIFT
10311               && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10312               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10313                                          MODE_INT, 1)) != BLKmode
10314               && (((unsigned HOST_WIDE_INT) const_op
10315                    + (GET_MODE_MASK (tmode) >> 1) + 1)
10316                   <= GET_MODE_MASK (tmode)))
10317             {
10318               op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
10319               continue;
10320             }
10321
10322           /* Likewise if OP0 is a PLUS of a sign extension with a
10323              constant, which is usually represented with the PLUS
10324              between the shifts.  */
10325           if (! unsigned_comparison_p
10326               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10327               && GET_CODE (XEXP (op0, 0)) == PLUS
10328               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10329               && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
10330               && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
10331               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10332                                          MODE_INT, 1)) != BLKmode
10333               && (((unsigned HOST_WIDE_INT) const_op
10334                    + (GET_MODE_MASK (tmode) >> 1) + 1)
10335                   <= GET_MODE_MASK (tmode)))
10336             {
10337               rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
10338               rtx add_const = XEXP (XEXP (op0, 0), 1);
10339               rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
10340                                                    add_const, XEXP (op0, 1));
10341
10342               op0 = simplify_gen_binary (PLUS, tmode,
10343                                          gen_lowpart (tmode, inner),
10344                                          new_const);
10345               continue;
10346             }
10347
10348           /* ... fall through ...  */
10349         case LSHIFTRT:
10350           /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10351              the low order N bits of FOO are known to be zero, we can do this
10352              by comparing FOO with C shifted left N bits so long as no
10353              overflow occurs.  */
10354           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10355               && INTVAL (XEXP (op0, 1)) >= 0
10356               && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10357               && mode_width <= HOST_BITS_PER_WIDE_INT
10358               && (nonzero_bits (XEXP (op0, 0), mode)
10359                   & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
10360               && (((unsigned HOST_WIDE_INT) const_op
10361                    + (GET_CODE (op0) != LSHIFTRT
10362                       ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
10363                          + 1)
10364                       : 0))
10365                   <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
10366             {
10367               /* If the shift was logical, then we must make the condition
10368                  unsigned.  */
10369               if (GET_CODE (op0) == LSHIFTRT)
10370                 code = unsigned_condition (code);
10371
10372               const_op <<= INTVAL (XEXP (op0, 1));
10373               op1 = GEN_INT (const_op);
10374               op0 = XEXP (op0, 0);
10375               continue;
10376             }
10377
10378           /* If we are using this shift to extract just the sign bit, we
10379              can replace this with an LT or GE comparison.  */
10380           if (const_op == 0
10381               && (equality_comparison_p || sign_bit_comparison_p)
10382               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10383               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10384                  == mode_width - 1)
10385             {
10386               op0 = XEXP (op0, 0);
10387               code = (code == NE || code == GT ? LT : GE);
10388               continue;
10389             }
10390           break;
10391
10392         default:
10393           break;
10394         }
10395
10396       break;
10397     }
10398
10399   /* Now make any compound operations involved in this comparison.  Then,
10400      check for an outmost SUBREG on OP0 that is not doing anything or is
10401      paradoxical.  The latter transformation must only be performed when
10402      it is known that the "extra" bits will be the same in op0 and op1 or
10403      that they don't matter.  There are three cases to consider:
10404
10405      1. SUBREG_REG (op0) is a register.  In this case the bits are don't
10406      care bits and we can assume they have any convenient value.  So
10407      making the transformation is safe.
10408
10409      2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
10410      In this case the upper bits of op0 are undefined.  We should not make
10411      the simplification in that case as we do not know the contents of
10412      those bits.
10413
10414      3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
10415      UNKNOWN.  In that case we know those bits are zeros or ones.  We must
10416      also be sure that they are the same as the upper bits of op1.
10417
10418      We can never remove a SUBREG for a non-equality comparison because
10419      the sign bit is in a different place in the underlying object.  */
10420
10421   op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
10422   op1 = make_compound_operation (op1, SET);
10423
10424   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10425       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10426       && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
10427       && (code == NE || code == EQ))
10428     {
10429       if (GET_MODE_SIZE (GET_MODE (op0))
10430           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
10431         {
10432           /* For paradoxical subregs, allow case 1 as above.  Case 3 isn't
10433              implemented.  */
10434           if (REG_P (SUBREG_REG (op0)))
10435             {
10436               op0 = SUBREG_REG (op0);
10437               op1 = gen_lowpart (GET_MODE (op0), op1);
10438             }
10439         }
10440       else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10441                 <= HOST_BITS_PER_WIDE_INT)
10442                && (nonzero_bits (SUBREG_REG (op0),
10443                                  GET_MODE (SUBREG_REG (op0)))
10444                    & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
10445         {
10446           tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
10447
10448           if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
10449                & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
10450             op0 = SUBREG_REG (op0), op1 = tem;
10451         }
10452     }
10453
10454   /* We now do the opposite procedure: Some machines don't have compare
10455      insns in all modes.  If OP0's mode is an integer mode smaller than a
10456      word and we can't do a compare in that mode, see if there is a larger
10457      mode for which we can do the compare.  There are a number of cases in
10458      which we can use the wider mode.  */
10459
10460   mode = GET_MODE (op0);
10461   if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10462       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
10463       && ! have_insn_for (COMPARE, mode))
10464     for (tmode = GET_MODE_WIDER_MODE (mode);
10465          (tmode != VOIDmode
10466           && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
10467          tmode = GET_MODE_WIDER_MODE (tmode))
10468       if (have_insn_for (COMPARE, tmode))
10469         {
10470           int zero_extended;
10471
10472           /* If the only nonzero bits in OP0 and OP1 are those in the
10473              narrower mode and this is an equality or unsigned comparison,
10474              we can use the wider mode.  Similarly for sign-extended
10475              values, in which case it is true for all comparisons.  */
10476           zero_extended = ((code == EQ || code == NE
10477                             || code == GEU || code == GTU
10478                             || code == LEU || code == LTU)
10479                            && (nonzero_bits (op0, tmode)
10480                                & ~GET_MODE_MASK (mode)) == 0
10481                            && ((GET_CODE (op1) == CONST_INT
10482                                 || (nonzero_bits (op1, tmode)
10483                                     & ~GET_MODE_MASK (mode)) == 0)));
10484
10485           if (zero_extended
10486               || ((num_sign_bit_copies (op0, tmode)
10487                    > (unsigned int) (GET_MODE_BITSIZE (tmode)
10488                                      - GET_MODE_BITSIZE (mode)))
10489                   && (num_sign_bit_copies (op1, tmode)
10490                       > (unsigned int) (GET_MODE_BITSIZE (tmode)
10491                                         - GET_MODE_BITSIZE (mode)))))
10492             {
10493               /* If OP0 is an AND and we don't have an AND in MODE either,
10494                  make a new AND in the proper mode.  */
10495               if (GET_CODE (op0) == AND
10496                   && !have_insn_for (AND, mode))
10497                 op0 = simplify_gen_binary (AND, tmode,
10498                                            gen_lowpart (tmode,
10499                                                         XEXP (op0, 0)),
10500                                            gen_lowpart (tmode,
10501                                                         XEXP (op0, 1)));
10502
10503               op0 = gen_lowpart (tmode, op0);
10504               if (zero_extended && GET_CODE (op1) == CONST_INT)
10505                 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
10506               op1 = gen_lowpart (tmode, op1);
10507               break;
10508             }
10509
10510           /* If this is a test for negative, we can make an explicit
10511              test of the sign bit.  */
10512
10513           if (op1 == const0_rtx && (code == LT || code == GE)
10514               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10515             {
10516               op0 = simplify_gen_binary (AND, tmode,
10517                                          gen_lowpart (tmode, op0),
10518                                          GEN_INT ((HOST_WIDE_INT) 1
10519                                                   << (GET_MODE_BITSIZE (mode)
10520                                                       - 1)));
10521               code = (code == LT) ? NE : EQ;
10522               break;
10523             }
10524         }
10525
10526 #ifdef CANONICALIZE_COMPARISON
10527   /* If this machine only supports a subset of valid comparisons, see if we
10528      can convert an unsupported one into a supported one.  */
10529   CANONICALIZE_COMPARISON (code, op0, op1);
10530 #endif
10531
10532   *pop0 = op0;
10533   *pop1 = op1;
10534
10535   return code;
10536 }
10537 \f
10538 /* Utility function for record_value_for_reg.  Count number of
10539    rtxs in X.  */
10540 static int
10541 count_rtxs (rtx x)
10542 {
10543   enum rtx_code code = GET_CODE (x);
10544   const char *fmt;
10545   int i, ret = 1;
10546
10547   if (GET_RTX_CLASS (code) == '2'
10548       || GET_RTX_CLASS (code) == 'c')
10549     {
10550       rtx x0 = XEXP (x, 0);
10551       rtx x1 = XEXP (x, 1);
10552
10553       if (x0 == x1)
10554         return 1 + 2 * count_rtxs (x0);
10555
10556       if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
10557            || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
10558           && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
10559         return 2 + 2 * count_rtxs (x0)
10560                + count_rtxs (x == XEXP (x1, 0)
10561                              ? XEXP (x1, 1) : XEXP (x1, 0));
10562
10563       if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
10564            || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
10565           && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
10566         return 2 + 2 * count_rtxs (x1)
10567                + count_rtxs (x == XEXP (x0, 0)
10568                              ? XEXP (x0, 1) : XEXP (x0, 0));
10569     }
10570
10571   fmt = GET_RTX_FORMAT (code);
10572   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10573     if (fmt[i] == 'e')
10574       ret += count_rtxs (XEXP (x, i));
10575
10576   return ret;
10577 }
10578 \f
10579 /* Utility function for following routine.  Called when X is part of a value
10580    being stored into last_set_value.  Sets last_set_table_tick
10581    for each register mentioned.  Similar to mention_regs in cse.c  */
10582
10583 static void
10584 update_table_tick (rtx x)
10585 {
10586   enum rtx_code code = GET_CODE (x);
10587   const char *fmt = GET_RTX_FORMAT (code);
10588   int i;
10589
10590   if (code == REG)
10591     {
10592       unsigned int regno = REGNO (x);
10593       unsigned int endregno
10594         = regno + (regno < FIRST_PSEUDO_REGISTER
10595                    ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
10596       unsigned int r;
10597
10598       for (r = regno; r < endregno; r++)
10599         reg_stat[r].last_set_table_tick = label_tick;
10600
10601       return;
10602     }
10603
10604   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10605     /* Note that we can't have an "E" in values stored; see
10606        get_last_value_validate.  */
10607     if (fmt[i] == 'e')
10608       {
10609         /* Check for identical subexpressions.  If x contains
10610            identical subexpression we only have to traverse one of
10611            them.  */
10612         if (i == 0 && ARITHMETIC_P (x))
10613           {
10614             /* Note that at this point x1 has already been
10615                processed.  */
10616             rtx x0 = XEXP (x, 0);
10617             rtx x1 = XEXP (x, 1);
10618
10619             /* If x0 and x1 are identical then there is no need to
10620                process x0.  */
10621             if (x0 == x1)
10622               break;
10623
10624             /* If x0 is identical to a subexpression of x1 then while
10625                processing x1, x0 has already been processed.  Thus we
10626                are done with x.  */
10627             if (ARITHMETIC_P (x1)
10628                 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
10629               break;
10630
10631             /* If x1 is identical to a subexpression of x0 then we
10632                still have to process the rest of x0.  */
10633             if (ARITHMETIC_P (x0)
10634                 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
10635               {
10636                 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
10637                 break;
10638               }
10639           }
10640
10641         update_table_tick (XEXP (x, i));
10642       }
10643 }
10644
10645 /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
10646    are saying that the register is clobbered and we no longer know its
10647    value.  If INSN is zero, don't update reg_stat[].last_set; this is
10648    only permitted with VALUE also zero and is used to invalidate the
10649    register.  */
10650
10651 static void
10652 record_value_for_reg (rtx reg, rtx insn, rtx value)
10653 {
10654   unsigned int regno = REGNO (reg);
10655   unsigned int endregno
10656     = regno + (regno < FIRST_PSEUDO_REGISTER
10657                ? hard_regno_nregs[regno][GET_MODE (reg)] : 1);
10658   unsigned int i;
10659
10660   /* If VALUE contains REG and we have a previous value for REG, substitute
10661      the previous value.  */
10662   if (value && insn && reg_overlap_mentioned_p (reg, value))
10663     {
10664       rtx tem;
10665
10666       /* Set things up so get_last_value is allowed to see anything set up to
10667          our insn.  */
10668       subst_low_cuid = INSN_CUID (insn);
10669       tem = get_last_value (reg);
10670
10671       /* If TEM is simply a binary operation with two CLOBBERs as operands,
10672          it isn't going to be useful and will take a lot of time to process,
10673          so just use the CLOBBER.  */
10674
10675       if (tem)
10676         {
10677           if (ARITHMETIC_P (tem)
10678               && GET_CODE (XEXP (tem, 0)) == CLOBBER
10679               && GET_CODE (XEXP (tem, 1)) == CLOBBER)
10680             tem = XEXP (tem, 0);
10681           else if (count_occurrences (value, reg, 1) >= 2)
10682             {
10683               /* If there are two or more occurrences of REG in VALUE,
10684                  prevent the value from growing too much.  */
10685               if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
10686                 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
10687             }
10688
10689           value = replace_rtx (copy_rtx (value), reg, tem);
10690         }
10691     }
10692
10693   /* For each register modified, show we don't know its value, that
10694      we don't know about its bitwise content, that its value has been
10695      updated, and that we don't know the location of the death of the
10696      register.  */
10697   for (i = regno; i < endregno; i++)
10698     {
10699       if (insn)
10700         reg_stat[i].last_set = insn;
10701
10702       reg_stat[i].last_set_value = 0;
10703       reg_stat[i].last_set_mode = 0;
10704       reg_stat[i].last_set_nonzero_bits = 0;
10705       reg_stat[i].last_set_sign_bit_copies = 0;
10706       reg_stat[i].last_death = 0;
10707     }
10708
10709   /* Mark registers that are being referenced in this value.  */
10710   if (value)
10711     update_table_tick (value);
10712
10713   /* Now update the status of each register being set.
10714      If someone is using this register in this block, set this register
10715      to invalid since we will get confused between the two lives in this
10716      basic block.  This makes using this register always invalid.  In cse, we
10717      scan the table to invalidate all entries using this register, but this
10718      is too much work for us.  */
10719
10720   for (i = regno; i < endregno; i++)
10721     {
10722       reg_stat[i].last_set_label = label_tick;
10723       if (value && reg_stat[i].last_set_table_tick == label_tick)
10724         reg_stat[i].last_set_invalid = 1;
10725       else
10726         reg_stat[i].last_set_invalid = 0;
10727     }
10728
10729   /* The value being assigned might refer to X (like in "x++;").  In that
10730      case, we must replace it with (clobber (const_int 0)) to prevent
10731      infinite loops.  */
10732   if (value && ! get_last_value_validate (&value, insn,
10733                                           reg_stat[regno].last_set_label, 0))
10734     {
10735       value = copy_rtx (value);
10736       if (! get_last_value_validate (&value, insn,
10737                                      reg_stat[regno].last_set_label, 1))
10738         value = 0;
10739     }
10740
10741   /* For the main register being modified, update the value, the mode, the
10742      nonzero bits, and the number of sign bit copies.  */
10743
10744   reg_stat[regno].last_set_value = value;
10745
10746   if (value)
10747     {
10748       enum machine_mode mode = GET_MODE (reg);
10749       subst_low_cuid = INSN_CUID (insn);
10750       reg_stat[regno].last_set_mode = mode;
10751       if (GET_MODE_CLASS (mode) == MODE_INT
10752           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10753         mode = nonzero_bits_mode;
10754       reg_stat[regno].last_set_nonzero_bits = nonzero_bits (value, mode);
10755       reg_stat[regno].last_set_sign_bit_copies
10756         = num_sign_bit_copies (value, GET_MODE (reg));
10757     }
10758 }
10759
10760 /* Called via note_stores from record_dead_and_set_regs to handle one
10761    SET or CLOBBER in an insn.  DATA is the instruction in which the
10762    set is occurring.  */
10763
10764 static void
10765 record_dead_and_set_regs_1 (rtx dest, rtx setter, void *data)
10766 {
10767   rtx record_dead_insn = (rtx) data;
10768
10769   if (GET_CODE (dest) == SUBREG)
10770     dest = SUBREG_REG (dest);
10771
10772   if (REG_P (dest))
10773     {
10774       /* If we are setting the whole register, we know its value.  Otherwise
10775          show that we don't know the value.  We can handle SUBREG in
10776          some cases.  */
10777       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
10778         record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
10779       else if (GET_CODE (setter) == SET
10780                && GET_CODE (SET_DEST (setter)) == SUBREG
10781                && SUBREG_REG (SET_DEST (setter)) == dest
10782                && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
10783                && subreg_lowpart_p (SET_DEST (setter)))
10784         record_value_for_reg (dest, record_dead_insn,
10785                               gen_lowpart (GET_MODE (dest),
10786                                                        SET_SRC (setter)));
10787       else
10788         record_value_for_reg (dest, record_dead_insn, NULL_RTX);
10789     }
10790   else if (MEM_P (dest)
10791            /* Ignore pushes, they clobber nothing.  */
10792            && ! push_operand (dest, GET_MODE (dest)))
10793     mem_last_set = INSN_CUID (record_dead_insn);
10794 }
10795
10796 /* Update the records of when each REG was most recently set or killed
10797    for the things done by INSN.  This is the last thing done in processing
10798    INSN in the combiner loop.
10799
10800    We update reg_stat[], in particular fields last_set, last_set_value,
10801    last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
10802    last_death, and also the similar information mem_last_set (which insn
10803    most recently modified memory) and last_call_cuid (which insn was the
10804    most recent subroutine call).  */
10805
10806 static void
10807 record_dead_and_set_regs (rtx insn)
10808 {
10809   rtx link;
10810   unsigned int i;
10811
10812   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
10813     {
10814       if (REG_NOTE_KIND (link) == REG_DEAD
10815           && REG_P (XEXP (link, 0)))
10816         {
10817           unsigned int regno = REGNO (XEXP (link, 0));
10818           unsigned int endregno
10819             = regno + (regno < FIRST_PSEUDO_REGISTER
10820                        ? hard_regno_nregs[regno][GET_MODE (XEXP (link, 0))]
10821                        : 1);
10822
10823           for (i = regno; i < endregno; i++)
10824             reg_stat[i].last_death = insn;
10825         }
10826       else if (REG_NOTE_KIND (link) == REG_INC)
10827         record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
10828     }
10829
10830   if (CALL_P (insn))
10831     {
10832       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
10833         if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
10834           {
10835             reg_stat[i].last_set_value = 0;
10836             reg_stat[i].last_set_mode = 0;
10837             reg_stat[i].last_set_nonzero_bits = 0;
10838             reg_stat[i].last_set_sign_bit_copies = 0;
10839             reg_stat[i].last_death = 0;
10840           }
10841
10842       last_call_cuid = mem_last_set = INSN_CUID (insn);
10843
10844       /* Don't bother recording what this insn does.  It might set the
10845          return value register, but we can't combine into a call
10846          pattern anyway, so there's no point trying (and it may cause
10847          a crash, if e.g. we wind up asking for last_set_value of a
10848          SUBREG of the return value register).  */
10849       return;
10850     }
10851
10852   note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
10853 }
10854
10855 /* If a SUBREG has the promoted bit set, it is in fact a property of the
10856    register present in the SUBREG, so for each such SUBREG go back and
10857    adjust nonzero and sign bit information of the registers that are
10858    known to have some zero/sign bits set.
10859
10860    This is needed because when combine blows the SUBREGs away, the
10861    information on zero/sign bits is lost and further combines can be
10862    missed because of that.  */
10863
10864 static void
10865 record_promoted_value (rtx insn, rtx subreg)
10866 {
10867   rtx links, set;
10868   unsigned int regno = REGNO (SUBREG_REG (subreg));
10869   enum machine_mode mode = GET_MODE (subreg);
10870
10871   if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
10872     return;
10873
10874   for (links = LOG_LINKS (insn); links;)
10875     {
10876       insn = XEXP (links, 0);
10877       set = single_set (insn);
10878
10879       if (! set || !REG_P (SET_DEST (set))
10880           || REGNO (SET_DEST (set)) != regno
10881           || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
10882         {
10883           links = XEXP (links, 1);
10884           continue;
10885         }
10886
10887       if (reg_stat[regno].last_set == insn)
10888         {
10889           if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
10890             reg_stat[regno].last_set_nonzero_bits &= GET_MODE_MASK (mode);
10891         }
10892
10893       if (REG_P (SET_SRC (set)))
10894         {
10895           regno = REGNO (SET_SRC (set));
10896           links = LOG_LINKS (insn);
10897         }
10898       else
10899         break;
10900     }
10901 }
10902
10903 /* Scan X for promoted SUBREGs.  For each one found,
10904    note what it implies to the registers used in it.  */
10905
10906 static void
10907 check_promoted_subreg (rtx insn, rtx x)
10908 {
10909   if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
10910       && REG_P (SUBREG_REG (x)))
10911     record_promoted_value (insn, x);
10912   else
10913     {
10914       const char *format = GET_RTX_FORMAT (GET_CODE (x));
10915       int i, j;
10916
10917       for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
10918         switch (format[i])
10919           {
10920           case 'e':
10921             check_promoted_subreg (insn, XEXP (x, i));
10922             break;
10923           case 'V':
10924           case 'E':
10925             if (XVEC (x, i) != 0)
10926               for (j = 0; j < XVECLEN (x, i); j++)
10927                 check_promoted_subreg (insn, XVECEXP (x, i, j));
10928             break;
10929           }
10930     }
10931 }
10932 \f
10933 /* Utility routine for the following function.  Verify that all the registers
10934    mentioned in *LOC are valid when *LOC was part of a value set when
10935    label_tick == TICK.  Return 0 if some are not.
10936
10937    If REPLACE is nonzero, replace the invalid reference with
10938    (clobber (const_int 0)) and return 1.  This replacement is useful because
10939    we often can get useful information about the form of a value (e.g., if
10940    it was produced by a shift that always produces -1 or 0) even though
10941    we don't know exactly what registers it was produced from.  */
10942
10943 static int
10944 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
10945 {
10946   rtx x = *loc;
10947   const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
10948   int len = GET_RTX_LENGTH (GET_CODE (x));
10949   int i;
10950
10951   if (REG_P (x))
10952     {
10953       unsigned int regno = REGNO (x);
10954       unsigned int endregno
10955         = regno + (regno < FIRST_PSEUDO_REGISTER
10956                    ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
10957       unsigned int j;
10958
10959       for (j = regno; j < endregno; j++)
10960         if (reg_stat[j].last_set_invalid
10961             /* If this is a pseudo-register that was only set once and not
10962                live at the beginning of the function, it is always valid.  */
10963             || (! (regno >= FIRST_PSEUDO_REGISTER
10964                    && REG_N_SETS (regno) == 1
10965                    && (! REGNO_REG_SET_P
10966                        (ENTRY_BLOCK_PTR->next_bb->il.rtl->global_live_at_start,
10967                         regno)))
10968                 && reg_stat[j].last_set_label > tick))
10969           {
10970             if (replace)
10971               *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10972             return replace;
10973           }
10974
10975       return 1;
10976     }
10977   /* If this is a memory reference, make sure that there were
10978      no stores after it that might have clobbered the value.  We don't
10979      have alias info, so we assume any store invalidates it.  */
10980   else if (MEM_P (x) && !MEM_READONLY_P (x)
10981            && INSN_CUID (insn) <= mem_last_set)
10982     {
10983       if (replace)
10984         *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10985       return replace;
10986     }
10987
10988   for (i = 0; i < len; i++)
10989     {
10990       if (fmt[i] == 'e')
10991         {
10992           /* Check for identical subexpressions.  If x contains
10993              identical subexpression we only have to traverse one of
10994              them.  */
10995           if (i == 1 && ARITHMETIC_P (x))
10996             {
10997               /* Note that at this point x0 has already been checked
10998                  and found valid.  */
10999               rtx x0 = XEXP (x, 0);
11000               rtx x1 = XEXP (x, 1);
11001
11002               /* If x0 and x1 are identical then x is also valid.  */
11003               if (x0 == x1)
11004                 return 1;
11005
11006               /* If x1 is identical to a subexpression of x0 then
11007                  while checking x0, x1 has already been checked.  Thus
11008                  it is valid and so as x.  */
11009               if (ARITHMETIC_P (x0)
11010                   && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11011                 return 1;
11012
11013               /* If x0 is identical to a subexpression of x1 then x is
11014                  valid iff the rest of x1 is valid.  */
11015               if (ARITHMETIC_P (x1)
11016                   && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11017                 return
11018                   get_last_value_validate (&XEXP (x1,
11019                                                   x0 == XEXP (x1, 0) ? 1 : 0),
11020                                            insn, tick, replace);
11021             }
11022
11023           if (get_last_value_validate (&XEXP (x, i), insn, tick,
11024                                        replace) == 0)
11025             return 0;
11026         }
11027       /* Don't bother with these.  They shouldn't occur anyway.  */
11028       else if (fmt[i] == 'E')
11029         return 0;
11030     }
11031
11032   /* If we haven't found a reason for it to be invalid, it is valid.  */
11033   return 1;
11034 }
11035
11036 /* Get the last value assigned to X, if known.  Some registers
11037    in the value may be replaced with (clobber (const_int 0)) if their value
11038    is known longer known reliably.  */
11039
11040 static rtx
11041 get_last_value (rtx x)
11042 {
11043   unsigned int regno;
11044   rtx value;
11045
11046   /* If this is a non-paradoxical SUBREG, get the value of its operand and
11047      then convert it to the desired mode.  If this is a paradoxical SUBREG,
11048      we cannot predict what values the "extra" bits might have.  */
11049   if (GET_CODE (x) == SUBREG
11050       && subreg_lowpart_p (x)
11051       && (GET_MODE_SIZE (GET_MODE (x))
11052           <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11053       && (value = get_last_value (SUBREG_REG (x))) != 0)
11054     return gen_lowpart (GET_MODE (x), value);
11055
11056   if (!REG_P (x))
11057     return 0;
11058
11059   regno = REGNO (x);
11060   value = reg_stat[regno].last_set_value;
11061
11062   /* If we don't have a value, or if it isn't for this basic block and
11063      it's either a hard register, set more than once, or it's a live
11064      at the beginning of the function, return 0.
11065
11066      Because if it's not live at the beginning of the function then the reg
11067      is always set before being used (is never used without being set).
11068      And, if it's set only once, and it's always set before use, then all
11069      uses must have the same last value, even if it's not from this basic
11070      block.  */
11071
11072   if (value == 0
11073       || (reg_stat[regno].last_set_label != label_tick
11074           && (regno < FIRST_PSEUDO_REGISTER
11075               || REG_N_SETS (regno) != 1
11076               || (REGNO_REG_SET_P
11077                   (ENTRY_BLOCK_PTR->next_bb->il.rtl->global_live_at_start,
11078                    regno)))))
11079     return 0;
11080
11081   /* If the value was set in a later insn than the ones we are processing,
11082      we can't use it even if the register was only set once.  */
11083   if (INSN_CUID (reg_stat[regno].last_set) >= subst_low_cuid)
11084     return 0;
11085
11086   /* If the value has all its registers valid, return it.  */
11087   if (get_last_value_validate (&value, reg_stat[regno].last_set,
11088                                reg_stat[regno].last_set_label, 0))
11089     return value;
11090
11091   /* Otherwise, make a copy and replace any invalid register with
11092      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
11093
11094   value = copy_rtx (value);
11095   if (get_last_value_validate (&value, reg_stat[regno].last_set,
11096                                reg_stat[regno].last_set_label, 1))
11097     return value;
11098
11099   return 0;
11100 }
11101 \f
11102 /* Return nonzero if expression X refers to a REG or to memory
11103    that is set in an instruction more recent than FROM_CUID.  */
11104
11105 static int
11106 use_crosses_set_p (rtx x, int from_cuid)
11107 {
11108   const char *fmt;
11109   int i;
11110   enum rtx_code code = GET_CODE (x);
11111
11112   if (code == REG)
11113     {
11114       unsigned int regno = REGNO (x);
11115       unsigned endreg = regno + (regno < FIRST_PSEUDO_REGISTER
11116                                  ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
11117
11118 #ifdef PUSH_ROUNDING
11119       /* Don't allow uses of the stack pointer to be moved,
11120          because we don't know whether the move crosses a push insn.  */
11121       if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11122         return 1;
11123 #endif
11124       for (; regno < endreg; regno++)
11125         if (reg_stat[regno].last_set
11126             && INSN_CUID (reg_stat[regno].last_set) > from_cuid)
11127           return 1;
11128       return 0;
11129     }
11130
11131   if (code == MEM && mem_last_set > from_cuid)
11132     return 1;
11133
11134   fmt = GET_RTX_FORMAT (code);
11135
11136   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11137     {
11138       if (fmt[i] == 'E')
11139         {
11140           int j;
11141           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11142             if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
11143               return 1;
11144         }
11145       else if (fmt[i] == 'e'
11146                && use_crosses_set_p (XEXP (x, i), from_cuid))
11147         return 1;
11148     }
11149   return 0;
11150 }
11151 \f
11152 /* Define three variables used for communication between the following
11153    routines.  */
11154
11155 static unsigned int reg_dead_regno, reg_dead_endregno;
11156 static int reg_dead_flag;
11157
11158 /* Function called via note_stores from reg_dead_at_p.
11159
11160    If DEST is within [reg_dead_regno, reg_dead_endregno), set
11161    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
11162
11163 static void
11164 reg_dead_at_p_1 (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
11165 {
11166   unsigned int regno, endregno;
11167
11168   if (!REG_P (dest))
11169     return;
11170
11171   regno = REGNO (dest);
11172   endregno = regno + (regno < FIRST_PSEUDO_REGISTER
11173                       ? hard_regno_nregs[regno][GET_MODE (dest)] : 1);
11174
11175   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11176     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11177 }
11178
11179 /* Return nonzero if REG is known to be dead at INSN.
11180
11181    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
11182    referencing REG, it is dead.  If we hit a SET referencing REG, it is
11183    live.  Otherwise, see if it is live or dead at the start of the basic
11184    block we are in.  Hard regs marked as being live in NEWPAT_USED_REGS
11185    must be assumed to be always live.  */
11186
11187 static int
11188 reg_dead_at_p (rtx reg, rtx insn)
11189 {
11190   basic_block block;
11191   unsigned int i;
11192
11193   /* Set variables for reg_dead_at_p_1.  */
11194   reg_dead_regno = REGNO (reg);
11195   reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
11196                                         ? hard_regno_nregs[reg_dead_regno]
11197                                                           [GET_MODE (reg)]
11198                                         : 1);
11199
11200   reg_dead_flag = 0;
11201
11202   /* Check that reg isn't mentioned in NEWPAT_USED_REGS.  For fixed registers
11203      we allow the machine description to decide whether use-and-clobber
11204      patterns are OK.  */
11205   if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11206     {
11207       for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11208         if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
11209           return 0;
11210     }
11211
11212   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11213      beginning of function.  */
11214   for (; insn && !LABEL_P (insn) && !BARRIER_P (insn);
11215        insn = prev_nonnote_insn (insn))
11216     {
11217       note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
11218       if (reg_dead_flag)
11219         return reg_dead_flag == 1 ? 1 : 0;
11220
11221       if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11222         return 1;
11223     }
11224
11225   /* Get the basic block that we were in.  */
11226   if (insn == 0)
11227     block = ENTRY_BLOCK_PTR->next_bb;
11228   else
11229     {
11230       FOR_EACH_BB (block)
11231         if (insn == BB_HEAD (block))
11232           break;
11233
11234       if (block == EXIT_BLOCK_PTR)
11235         return 0;
11236     }
11237
11238   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11239     if (REGNO_REG_SET_P (block->il.rtl->global_live_at_start, i))
11240       return 0;
11241
11242   return 1;
11243 }
11244 \f
11245 /* Note hard registers in X that are used.  This code is similar to
11246    that in flow.c, but much simpler since we don't care about pseudos.  */
11247
11248 static void
11249 mark_used_regs_combine (rtx x)
11250 {
11251   RTX_CODE code = GET_CODE (x);
11252   unsigned int regno;
11253   int i;
11254
11255   switch (code)
11256     {
11257     case LABEL_REF:
11258     case SYMBOL_REF:
11259     case CONST_INT:
11260     case CONST:
11261     case CONST_DOUBLE:
11262     case CONST_VECTOR:
11263     case PC:
11264     case ADDR_VEC:
11265     case ADDR_DIFF_VEC:
11266     case ASM_INPUT:
11267 #ifdef HAVE_cc0
11268     /* CC0 must die in the insn after it is set, so we don't need to take
11269        special note of it here.  */
11270     case CC0:
11271 #endif
11272       return;
11273
11274     case CLOBBER:
11275       /* If we are clobbering a MEM, mark any hard registers inside the
11276          address as used.  */
11277       if (MEM_P (XEXP (x, 0)))
11278         mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
11279       return;
11280
11281     case REG:
11282       regno = REGNO (x);
11283       /* A hard reg in a wide mode may really be multiple registers.
11284          If so, mark all of them just like the first.  */
11285       if (regno < FIRST_PSEUDO_REGISTER)
11286         {
11287           unsigned int endregno, r;
11288
11289           /* None of this applies to the stack, frame or arg pointers.  */
11290           if (regno == STACK_POINTER_REGNUM
11291 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11292               || regno == HARD_FRAME_POINTER_REGNUM
11293 #endif
11294 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
11295               || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
11296 #endif
11297               || regno == FRAME_POINTER_REGNUM)
11298             return;
11299
11300           endregno = regno + hard_regno_nregs[regno][GET_MODE (x)];
11301           for (r = regno; r < endregno; r++)
11302             SET_HARD_REG_BIT (newpat_used_regs, r);
11303         }
11304       return;
11305
11306     case SET:
11307       {
11308         /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11309            the address.  */
11310         rtx testreg = SET_DEST (x);
11311
11312         while (GET_CODE (testreg) == SUBREG
11313                || GET_CODE (testreg) == ZERO_EXTRACT
11314                || GET_CODE (testreg) == STRICT_LOW_PART)
11315           testreg = XEXP (testreg, 0);
11316
11317         if (MEM_P (testreg))
11318           mark_used_regs_combine (XEXP (testreg, 0));
11319
11320         mark_used_regs_combine (SET_SRC (x));
11321       }
11322       return;
11323
11324     default:
11325       break;
11326     }
11327
11328   /* Recursively scan the operands of this expression.  */
11329
11330   {
11331     const char *fmt = GET_RTX_FORMAT (code);
11332
11333     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11334       {
11335         if (fmt[i] == 'e')
11336           mark_used_regs_combine (XEXP (x, i));
11337         else if (fmt[i] == 'E')
11338           {
11339             int j;
11340
11341             for (j = 0; j < XVECLEN (x, i); j++)
11342               mark_used_regs_combine (XVECEXP (x, i, j));
11343           }
11344       }
11345   }
11346 }
11347 \f
11348 /* Remove register number REGNO from the dead registers list of INSN.
11349
11350    Return the note used to record the death, if there was one.  */
11351
11352 rtx
11353 remove_death (unsigned int regno, rtx insn)
11354 {
11355   rtx note = find_regno_note (insn, REG_DEAD, regno);
11356
11357   if (note)
11358     {
11359       REG_N_DEATHS (regno)--;
11360       remove_note (insn, note);
11361     }
11362
11363   return note;
11364 }
11365
11366 /* For each register (hardware or pseudo) used within expression X, if its
11367    death is in an instruction with cuid between FROM_CUID (inclusive) and
11368    TO_INSN (exclusive), put a REG_DEAD note for that register in the
11369    list headed by PNOTES.
11370
11371    That said, don't move registers killed by maybe_kill_insn.
11372
11373    This is done when X is being merged by combination into TO_INSN.  These
11374    notes will then be distributed as needed.  */
11375
11376 static void
11377 move_deaths (rtx x, rtx maybe_kill_insn, int from_cuid, rtx to_insn,
11378              rtx *pnotes)
11379 {
11380   const char *fmt;
11381   int len, i;
11382   enum rtx_code code = GET_CODE (x);
11383
11384   if (code == REG)
11385     {
11386       unsigned int regno = REGNO (x);
11387       rtx where_dead = reg_stat[regno].last_death;
11388       rtx before_dead, after_dead;
11389
11390       /* Don't move the register if it gets killed in between from and to.  */
11391       if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
11392           && ! reg_referenced_p (x, maybe_kill_insn))
11393         return;
11394
11395       /* WHERE_DEAD could be a USE insn made by combine, so first we
11396          make sure that we have insns with valid INSN_CUID values.  */
11397       before_dead = where_dead;
11398       while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
11399         before_dead = PREV_INSN (before_dead);
11400
11401       after_dead = where_dead;
11402       while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
11403         after_dead = NEXT_INSN (after_dead);
11404
11405       if (before_dead && after_dead
11406           && INSN_CUID (before_dead) >= from_cuid
11407           && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
11408               || (where_dead != after_dead
11409                   && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
11410         {
11411           rtx note = remove_death (regno, where_dead);
11412
11413           /* It is possible for the call above to return 0.  This can occur
11414              when last_death points to I2 or I1 that we combined with.
11415              In that case make a new note.
11416
11417              We must also check for the case where X is a hard register
11418              and NOTE is a death note for a range of hard registers
11419              including X.  In that case, we must put REG_DEAD notes for
11420              the remaining registers in place of NOTE.  */
11421
11422           if (note != 0 && regno < FIRST_PSEUDO_REGISTER
11423               && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11424                   > GET_MODE_SIZE (GET_MODE (x))))
11425             {
11426               unsigned int deadregno = REGNO (XEXP (note, 0));
11427               unsigned int deadend
11428                 = (deadregno + hard_regno_nregs[deadregno]
11429                                                [GET_MODE (XEXP (note, 0))]);
11430               unsigned int ourend
11431                 = regno + hard_regno_nregs[regno][GET_MODE (x)];
11432               unsigned int i;
11433
11434               for (i = deadregno; i < deadend; i++)
11435                 if (i < regno || i >= ourend)
11436                   REG_NOTES (where_dead)
11437                     = gen_rtx_EXPR_LIST (REG_DEAD,
11438                                          regno_reg_rtx[i],
11439                                          REG_NOTES (where_dead));
11440             }
11441
11442           /* If we didn't find any note, or if we found a REG_DEAD note that
11443              covers only part of the given reg, and we have a multi-reg hard
11444              register, then to be safe we must check for REG_DEAD notes
11445              for each register other than the first.  They could have
11446              their own REG_DEAD notes lying around.  */
11447           else if ((note == 0
11448                     || (note != 0
11449                         && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11450                             < GET_MODE_SIZE (GET_MODE (x)))))
11451                    && regno < FIRST_PSEUDO_REGISTER
11452                    && hard_regno_nregs[regno][GET_MODE (x)] > 1)
11453             {
11454               unsigned int ourend
11455                 = regno + hard_regno_nregs[regno][GET_MODE (x)];
11456               unsigned int i, offset;
11457               rtx oldnotes = 0;
11458
11459               if (note)
11460                 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
11461               else
11462                 offset = 1;
11463
11464               for (i = regno + offset; i < ourend; i++)
11465                 move_deaths (regno_reg_rtx[i],
11466                              maybe_kill_insn, from_cuid, to_insn, &oldnotes);
11467             }
11468
11469           if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
11470             {
11471               XEXP (note, 1) = *pnotes;
11472               *pnotes = note;
11473             }
11474           else
11475             *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
11476
11477           REG_N_DEATHS (regno)++;
11478         }
11479
11480       return;
11481     }
11482
11483   else if (GET_CODE (x) == SET)
11484     {
11485       rtx dest = SET_DEST (x);
11486
11487       move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
11488
11489       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
11490          that accesses one word of a multi-word item, some
11491          piece of everything register in the expression is used by
11492          this insn, so remove any old death.  */
11493       /* ??? So why do we test for equality of the sizes?  */
11494
11495       if (GET_CODE (dest) == ZERO_EXTRACT
11496           || GET_CODE (dest) == STRICT_LOW_PART
11497           || (GET_CODE (dest) == SUBREG
11498               && (((GET_MODE_SIZE (GET_MODE (dest))
11499                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
11500                   == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
11501                        + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
11502         {
11503           move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
11504           return;
11505         }
11506
11507       /* If this is some other SUBREG, we know it replaces the entire
11508          value, so use that as the destination.  */
11509       if (GET_CODE (dest) == SUBREG)
11510         dest = SUBREG_REG (dest);
11511
11512       /* If this is a MEM, adjust deaths of anything used in the address.
11513          For a REG (the only other possibility), the entire value is
11514          being replaced so the old value is not used in this insn.  */
11515
11516       if (MEM_P (dest))
11517         move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
11518                      to_insn, pnotes);
11519       return;
11520     }
11521
11522   else if (GET_CODE (x) == CLOBBER)
11523     return;
11524
11525   len = GET_RTX_LENGTH (code);
11526   fmt = GET_RTX_FORMAT (code);
11527
11528   for (i = 0; i < len; i++)
11529     {
11530       if (fmt[i] == 'E')
11531         {
11532           int j;
11533           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11534             move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
11535                          to_insn, pnotes);
11536         }
11537       else if (fmt[i] == 'e')
11538         move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
11539     }
11540 }
11541 \f
11542 /* Return 1 if X is the target of a bit-field assignment in BODY, the
11543    pattern of an insn.  X must be a REG.  */
11544
11545 static int
11546 reg_bitfield_target_p (rtx x, rtx body)
11547 {
11548   int i;
11549
11550   if (GET_CODE (body) == SET)
11551     {
11552       rtx dest = SET_DEST (body);
11553       rtx target;
11554       unsigned int regno, tregno, endregno, endtregno;
11555
11556       if (GET_CODE (dest) == ZERO_EXTRACT)
11557         target = XEXP (dest, 0);
11558       else if (GET_CODE (dest) == STRICT_LOW_PART)
11559         target = SUBREG_REG (XEXP (dest, 0));
11560       else
11561         return 0;
11562
11563       if (GET_CODE (target) == SUBREG)
11564         target = SUBREG_REG (target);
11565
11566       if (!REG_P (target))
11567         return 0;
11568
11569       tregno = REGNO (target), regno = REGNO (x);
11570       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
11571         return target == x;
11572
11573       endtregno = tregno + hard_regno_nregs[tregno][GET_MODE (target)];
11574       endregno = regno + hard_regno_nregs[regno][GET_MODE (x)];
11575
11576       return endregno > tregno && regno < endtregno;
11577     }
11578
11579   else if (GET_CODE (body) == PARALLEL)
11580     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
11581       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
11582         return 1;
11583
11584   return 0;
11585 }
11586 \f
11587 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
11588    as appropriate.  I3 and I2 are the insns resulting from the combination
11589    insns including FROM (I2 may be zero).
11590
11591    ELIM_I2 and ELIM_I1 are either zero or registers that we know will
11592    not need REG_DEAD notes because they are being substituted for.  This
11593    saves searching in the most common cases.
11594
11595    Each note in the list is either ignored or placed on some insns, depending
11596    on the type of note.  */
11597
11598 static void
11599 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
11600                   rtx elim_i1)
11601 {
11602   rtx note, next_note;
11603   rtx tem;
11604
11605   for (note = notes; note; note = next_note)
11606     {
11607       rtx place = 0, place2 = 0;
11608
11609       /* If this NOTE references a pseudo register, ensure it references
11610          the latest copy of that register.  */
11611       if (XEXP (note, 0) && REG_P (XEXP (note, 0))
11612           && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
11613         XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
11614
11615       next_note = XEXP (note, 1);
11616       switch (REG_NOTE_KIND (note))
11617         {
11618         case REG_BR_PROB:
11619         case REG_BR_PRED:
11620           /* Doesn't matter much where we put this, as long as it's somewhere.
11621              It is preferable to keep these notes on branches, which is most
11622              likely to be i3.  */
11623           place = i3;
11624           break;
11625
11626         case REG_VALUE_PROFILE:
11627           /* Just get rid of this note, as it is unused later anyway.  */
11628           break;
11629
11630         case REG_NON_LOCAL_GOTO:
11631           if (JUMP_P (i3))
11632             place = i3;
11633           else
11634             {
11635               gcc_assert (i2 && JUMP_P (i2));
11636               place = i2;
11637             }
11638           break;
11639
11640         case REG_EH_REGION:
11641           /* These notes must remain with the call or trapping instruction.  */
11642           if (CALL_P (i3))
11643             place = i3;
11644           else if (i2 && CALL_P (i2))
11645             place = i2;
11646           else
11647             {
11648               gcc_assert (flag_non_call_exceptions);
11649               if (may_trap_p (i3))
11650                 place = i3;
11651               else if (i2 && may_trap_p (i2))
11652                 place = i2;
11653               /* ??? Otherwise assume we've combined things such that we
11654                  can now prove that the instructions can't trap.  Drop the
11655                  note in this case.  */
11656             }
11657           break;
11658
11659         case REG_NORETURN:
11660         case REG_SETJMP:
11661           /* These notes must remain with the call.  It should not be
11662              possible for both I2 and I3 to be a call.  */
11663           if (CALL_P (i3))
11664             place = i3;
11665           else
11666             {
11667               gcc_assert (i2 && CALL_P (i2));
11668               place = i2;
11669             }
11670           break;
11671
11672         case REG_UNUSED:
11673           /* Any clobbers for i3 may still exist, and so we must process
11674              REG_UNUSED notes from that insn.
11675
11676              Any clobbers from i2 or i1 can only exist if they were added by
11677              recog_for_combine.  In that case, recog_for_combine created the
11678              necessary REG_UNUSED notes.  Trying to keep any original
11679              REG_UNUSED notes from these insns can cause incorrect output
11680              if it is for the same register as the original i3 dest.
11681              In that case, we will notice that the register is set in i3,
11682              and then add a REG_UNUSED note for the destination of i3, which
11683              is wrong.  However, it is possible to have REG_UNUSED notes from
11684              i2 or i1 for register which were both used and clobbered, so
11685              we keep notes from i2 or i1 if they will turn into REG_DEAD
11686              notes.  */
11687
11688           /* If this register is set or clobbered in I3, put the note there
11689              unless there is one already.  */
11690           if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
11691             {
11692               if (from_insn != i3)
11693                 break;
11694
11695               if (! (REG_P (XEXP (note, 0))
11696                      ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
11697                      : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
11698                 place = i3;
11699             }
11700           /* Otherwise, if this register is used by I3, then this register
11701              now dies here, so we must put a REG_DEAD note here unless there
11702              is one already.  */
11703           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
11704                    && ! (REG_P (XEXP (note, 0))
11705                          ? find_regno_note (i3, REG_DEAD,
11706                                             REGNO (XEXP (note, 0)))
11707                          : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
11708             {
11709               PUT_REG_NOTE_KIND (note, REG_DEAD);
11710               place = i3;
11711             }
11712           break;
11713
11714         case REG_EQUAL:
11715         case REG_EQUIV:
11716         case REG_NOALIAS:
11717           /* These notes say something about results of an insn.  We can
11718              only support them if they used to be on I3 in which case they
11719              remain on I3.  Otherwise they are ignored.
11720
11721              If the note refers to an expression that is not a constant, we
11722              must also ignore the note since we cannot tell whether the
11723              equivalence is still true.  It might be possible to do
11724              slightly better than this (we only have a problem if I2DEST
11725              or I1DEST is present in the expression), but it doesn't
11726              seem worth the trouble.  */
11727
11728           if (from_insn == i3
11729               && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
11730             place = i3;
11731           break;
11732
11733         case REG_INC:
11734         case REG_NO_CONFLICT:
11735           /* These notes say something about how a register is used.  They must
11736              be present on any use of the register in I2 or I3.  */
11737           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
11738             place = i3;
11739
11740           if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
11741             {
11742               if (place)
11743                 place2 = i2;
11744               else
11745                 place = i2;
11746             }
11747           break;
11748
11749         case REG_LABEL:
11750           /* This can show up in several ways -- either directly in the
11751              pattern, or hidden off in the constant pool with (or without?)
11752              a REG_EQUAL note.  */
11753           /* ??? Ignore the without-reg_equal-note problem for now.  */
11754           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
11755               || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
11756                   && GET_CODE (XEXP (tem, 0)) == LABEL_REF
11757                   && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
11758             place = i3;
11759
11760           if (i2
11761               && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
11762                   || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
11763                       && GET_CODE (XEXP (tem, 0)) == LABEL_REF
11764                       && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
11765             {
11766               if (place)
11767                 place2 = i2;
11768               else
11769                 place = i2;
11770             }
11771
11772           /* Don't attach REG_LABEL note to a JUMP_INSN.  Add
11773              a JUMP_LABEL instead or decrement LABEL_NUSES.  */
11774           if (place && JUMP_P (place))
11775             {
11776               rtx label = JUMP_LABEL (place);
11777               
11778               if (!label)
11779                 JUMP_LABEL (place) = XEXP (note, 0);
11780               else
11781                 {
11782                   gcc_assert (label == XEXP (note, 0));
11783                   if (LABEL_P (label))
11784                     LABEL_NUSES (label)--;
11785                 }
11786               place = 0;
11787             }
11788           if (place2 && JUMP_P (place2))
11789             {
11790               rtx label = JUMP_LABEL (place2);
11791               
11792               if (!label)
11793                 JUMP_LABEL (place2) = XEXP (note, 0);
11794               else
11795                 {
11796                   gcc_assert (label == XEXP (note, 0));
11797                   if (LABEL_P (label))
11798                     LABEL_NUSES (label)--;
11799                 }
11800               place2 = 0;
11801             }
11802           break;
11803
11804         case REG_NONNEG:
11805           /* This note says something about the value of a register prior
11806              to the execution of an insn.  It is too much trouble to see
11807              if the note is still correct in all situations.  It is better
11808              to simply delete it.  */
11809           break;
11810
11811         case REG_RETVAL:
11812           /* If the insn previously containing this note still exists,
11813              put it back where it was.  Otherwise move it to the previous
11814              insn.  Adjust the corresponding REG_LIBCALL note.  */
11815           if (!NOTE_P (from_insn))
11816             place = from_insn;
11817           else
11818             {
11819               tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
11820               place = prev_real_insn (from_insn);
11821               if (tem && place)
11822                 XEXP (tem, 0) = place;
11823               /* If we're deleting the last remaining instruction of a
11824                  libcall sequence, don't add the notes.  */
11825               else if (XEXP (note, 0) == from_insn)
11826                 tem = place = 0;
11827               /* Don't add the dangling REG_RETVAL note.  */
11828               else if (! tem)
11829                 place = 0;
11830             }
11831           break;
11832
11833         case REG_LIBCALL:
11834           /* This is handled similarly to REG_RETVAL.  */
11835           if (!NOTE_P (from_insn))
11836             place = from_insn;
11837           else
11838             {
11839               tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
11840               place = next_real_insn (from_insn);
11841               if (tem && place)
11842                 XEXP (tem, 0) = place;
11843               /* If we're deleting the last remaining instruction of a
11844                  libcall sequence, don't add the notes.  */
11845               else if (XEXP (note, 0) == from_insn)
11846                 tem = place = 0;
11847               /* Don't add the dangling REG_LIBCALL note.  */
11848               else if (! tem)
11849                 place = 0;
11850             }
11851           break;
11852
11853         case REG_DEAD:
11854           /* If the register is used as an input in I3, it dies there.
11855              Similarly for I2, if it is nonzero and adjacent to I3.
11856
11857              If the register is not used as an input in either I3 or I2
11858              and it is not one of the registers we were supposed to eliminate,
11859              there are two possibilities.  We might have a non-adjacent I2
11860              or we might have somehow eliminated an additional register
11861              from a computation.  For example, we might have had A & B where
11862              we discover that B will always be zero.  In this case we will
11863              eliminate the reference to A.
11864
11865              In both cases, we must search to see if we can find a previous
11866              use of A and put the death note there.  */
11867
11868           if (from_insn
11869               && CALL_P (from_insn)
11870               && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
11871             place = from_insn;
11872           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
11873             place = i3;
11874           else if (i2 != 0 && next_nonnote_insn (i2) == i3
11875                    && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11876             place = i2;
11877
11878           if (place == 0
11879               && (rtx_equal_p (XEXP (note, 0), elim_i2)
11880                   || rtx_equal_p (XEXP (note, 0), elim_i1)))
11881             break;
11882
11883           if (place == 0)
11884             {
11885               basic_block bb = this_basic_block;
11886
11887               /* You might think you could search back from FROM_INSN
11888                  rather than from I3, but combine tries to split invalid
11889                  combined instructions.  This can result in the old I2
11890                  or I1 moving later in the insn sequence.  */
11891               for (tem = PREV_INSN (i3); place == 0; tem = PREV_INSN (tem))
11892                 {
11893                   if (! INSN_P (tem))
11894                     {
11895                       if (tem == BB_HEAD (bb))
11896                         break;
11897                       continue;
11898                     }
11899
11900                   /* If the register is being set at TEM, see if that is all
11901                      TEM is doing.  If so, delete TEM.  Otherwise, make this
11902                      into a REG_UNUSED note instead. Don't delete sets to
11903                      global register vars.  */
11904                   if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
11905                        || !global_regs[REGNO (XEXP (note, 0))])
11906                       && reg_set_p (XEXP (note, 0), PATTERN (tem)))
11907                     {
11908                       rtx set = single_set (tem);
11909                       rtx inner_dest = 0;
11910 #ifdef HAVE_cc0
11911                       rtx cc0_setter = NULL_RTX;
11912 #endif
11913
11914                       if (set != 0)
11915                         for (inner_dest = SET_DEST (set);
11916                              (GET_CODE (inner_dest) == STRICT_LOW_PART
11917                               || GET_CODE (inner_dest) == SUBREG
11918                               || GET_CODE (inner_dest) == ZERO_EXTRACT);
11919                              inner_dest = XEXP (inner_dest, 0))
11920                           ;
11921
11922                       /* Verify that it was the set, and not a clobber that
11923                          modified the register.
11924
11925                          CC0 targets must be careful to maintain setter/user
11926                          pairs.  If we cannot delete the setter due to side
11927                          effects, mark the user with an UNUSED note instead
11928                          of deleting it.  */
11929
11930                       if (set != 0 && ! side_effects_p (SET_SRC (set))
11931                           && rtx_equal_p (XEXP (note, 0), inner_dest)
11932 #ifdef HAVE_cc0
11933                           && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
11934                               || ((cc0_setter = prev_cc0_setter (tem)) != NULL
11935                                   && sets_cc0_p (PATTERN (cc0_setter)) > 0))
11936 #endif
11937                           )
11938                         {
11939                           /* Move the notes and links of TEM elsewhere.
11940                              This might delete other dead insns recursively.
11941                              First set the pattern to something that won't use
11942                              any register.  */
11943                           rtx old_notes = REG_NOTES (tem);
11944
11945                           PATTERN (tem) = pc_rtx;
11946                           REG_NOTES (tem) = NULL;
11947
11948                           distribute_notes (old_notes, tem, tem, NULL_RTX,
11949                                             NULL_RTX, NULL_RTX);
11950                           distribute_links (LOG_LINKS (tem));
11951
11952                           SET_INSN_DELETED (tem);
11953
11954 #ifdef HAVE_cc0
11955                           /* Delete the setter too.  */
11956                           if (cc0_setter)
11957                             {
11958                               PATTERN (cc0_setter) = pc_rtx;
11959                               old_notes = REG_NOTES (cc0_setter);
11960                               REG_NOTES (cc0_setter) = NULL;
11961
11962                               distribute_notes (old_notes, cc0_setter,
11963                                                 cc0_setter, NULL_RTX,
11964                                                 NULL_RTX, NULL_RTX);
11965                               distribute_links (LOG_LINKS (cc0_setter));
11966
11967                               SET_INSN_DELETED (cc0_setter);
11968                             }
11969 #endif
11970                         }
11971                       else
11972                         {
11973                           PUT_REG_NOTE_KIND (note, REG_UNUSED);
11974
11975                           /*  If there isn't already a REG_UNUSED note, put one
11976                               here.  Do not place a REG_DEAD note, even if
11977                               the register is also used here; that would not
11978                               match the algorithm used in lifetime analysis
11979                               and can cause the consistency check in the
11980                               scheduler to fail.  */
11981                           if (! find_regno_note (tem, REG_UNUSED,
11982                                                  REGNO (XEXP (note, 0))))
11983                             place = tem;
11984                           break;
11985                         }
11986                     }
11987                   else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
11988                            || (CALL_P (tem)
11989                                && find_reg_fusage (tem, USE, XEXP (note, 0))))
11990                     {
11991                       /* This may not be the correct place for the death
11992                          note if FROM_INSN is before TEM, and the reg is
11993                          set between FROM_INSN and TEM.  The reg might
11994                          die two or more times.  An existing death note
11995                          means we are looking at the wrong live range.  */
11996                       if (from_insn
11997                           && INSN_CUID (from_insn) < INSN_CUID (tem)
11998                           && find_regno_note (tem, REG_DEAD,
11999                                               REGNO (XEXP (note, 0))))
12000                         {
12001                           tem = from_insn;
12002                           if (tem == BB_HEAD (bb))
12003                             break;
12004                           continue;
12005                         }
12006
12007                       place = tem;
12008
12009                       /* If we are doing a 3->2 combination, and we have a
12010                          register which formerly died in i3 and was not used
12011                          by i2, which now no longer dies in i3 and is used in
12012                          i2 but does not die in i2, and place is between i2
12013                          and i3, then we may need to move a link from place to
12014                          i2.  */
12015                       if (i2 && INSN_UID (place) <= max_uid_cuid
12016                           && INSN_CUID (place) > INSN_CUID (i2)
12017                           && from_insn
12018                           && INSN_CUID (from_insn) > INSN_CUID (i2)
12019                           && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12020                         {
12021                           rtx links = LOG_LINKS (place);
12022                           LOG_LINKS (place) = 0;
12023                           distribute_links (links);
12024                         }
12025                       break;
12026                     }
12027
12028                   if (tem == BB_HEAD (bb))
12029                     break;
12030                 }
12031
12032               /* We haven't found an insn for the death note and it
12033                  is still a REG_DEAD note, but we have hit the beginning
12034                  of the block.  If the existing life info says the reg
12035                  was dead, there's nothing left to do.  Otherwise, we'll
12036                  need to do a global life update after combine.  */
12037               if (REG_NOTE_KIND (note) == REG_DEAD && place == 0
12038                   && REGNO_REG_SET_P (bb->il.rtl->global_live_at_start,
12039                                       REGNO (XEXP (note, 0))))
12040                 SET_BIT (refresh_blocks, this_basic_block->index);
12041             }
12042
12043           /* If the register is set or already dead at PLACE, we needn't do
12044              anything with this note if it is still a REG_DEAD note.
12045              We check here if it is set at all, not if is it totally replaced,
12046              which is what `dead_or_set_p' checks, so also check for it being
12047              set partially.  */
12048
12049           if (place && REG_NOTE_KIND (note) == REG_DEAD)
12050             {
12051               unsigned int regno = REGNO (XEXP (note, 0));
12052
12053               /* Similarly, if the instruction on which we want to place
12054                  the note is a noop, we'll need do a global live update
12055                  after we remove them in delete_noop_moves.  */
12056               if (noop_move_p (place))
12057                 SET_BIT (refresh_blocks, this_basic_block->index);
12058
12059               if (dead_or_set_p (place, XEXP (note, 0))
12060                   || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12061                 {
12062                   /* Unless the register previously died in PLACE, clear
12063                      last_death.  [I no longer understand why this is
12064                      being done.] */
12065                   if (reg_stat[regno].last_death != place)
12066                     reg_stat[regno].last_death = 0;
12067                   place = 0;
12068                 }
12069               else
12070                 reg_stat[regno].last_death = place;
12071
12072               /* If this is a death note for a hard reg that is occupying
12073                  multiple registers, ensure that we are still using all
12074                  parts of the object.  If we find a piece of the object
12075                  that is unused, we must arrange for an appropriate REG_DEAD
12076                  note to be added for it.  However, we can't just emit a USE
12077                  and tag the note to it, since the register might actually
12078                  be dead; so we recourse, and the recursive call then finds
12079                  the previous insn that used this register.  */
12080
12081               if (place && regno < FIRST_PSEUDO_REGISTER
12082                   && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
12083                 {
12084                   unsigned int endregno
12085                     = regno + hard_regno_nregs[regno]
12086                                               [GET_MODE (XEXP (note, 0))];
12087                   int all_used = 1;
12088                   unsigned int i;
12089
12090                   for (i = regno; i < endregno; i++)
12091                     if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12092                          && ! find_regno_fusage (place, USE, i))
12093                         || dead_or_set_regno_p (place, i))
12094                       all_used = 0;
12095
12096                   if (! all_used)
12097                     {
12098                       /* Put only REG_DEAD notes for pieces that are
12099                          not already dead or set.  */
12100
12101                       for (i = regno; i < endregno;
12102                            i += hard_regno_nregs[i][reg_raw_mode[i]])
12103                         {
12104                           rtx piece = regno_reg_rtx[i];
12105                           basic_block bb = this_basic_block;
12106
12107                           if (! dead_or_set_p (place, piece)
12108                               && ! reg_bitfield_target_p (piece,
12109                                                           PATTERN (place)))
12110                             {
12111                               rtx new_note
12112                                 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12113
12114                               distribute_notes (new_note, place, place,
12115                                                 NULL_RTX, NULL_RTX, NULL_RTX);
12116                             }
12117                           else if (! refers_to_regno_p (i, i + 1,
12118                                                         PATTERN (place), 0)
12119                                    && ! find_regno_fusage (place, USE, i))
12120                             for (tem = PREV_INSN (place); ;
12121                                  tem = PREV_INSN (tem))
12122                               {
12123                                 if (! INSN_P (tem))
12124                                   {
12125                                     if (tem == BB_HEAD (bb))
12126                                       {
12127                                         SET_BIT (refresh_blocks,
12128                                                  this_basic_block->index);
12129                                         break;
12130                                       }
12131                                     continue;
12132                                   }
12133                                 if (dead_or_set_p (tem, piece)
12134                                     || reg_bitfield_target_p (piece,
12135                                                               PATTERN (tem)))
12136                                   {
12137                                     REG_NOTES (tem)
12138                                       = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
12139                                                            REG_NOTES (tem));
12140                                     break;
12141                                   }
12142                               }
12143
12144                         }
12145
12146                       place = 0;
12147                     }
12148                 }
12149             }
12150           break;
12151
12152         default:
12153           /* Any other notes should not be present at this point in the
12154              compilation.  */
12155           gcc_unreachable ();
12156         }
12157
12158       if (place)
12159         {
12160           XEXP (note, 1) = REG_NOTES (place);
12161           REG_NOTES (place) = note;
12162         }
12163       else if ((REG_NOTE_KIND (note) == REG_DEAD
12164                 || REG_NOTE_KIND (note) == REG_UNUSED)
12165                && REG_P (XEXP (note, 0)))
12166         REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
12167
12168       if (place2)
12169         {
12170           if ((REG_NOTE_KIND (note) == REG_DEAD
12171                || REG_NOTE_KIND (note) == REG_UNUSED)
12172               && REG_P (XEXP (note, 0)))
12173             REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
12174
12175           REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
12176                                                REG_NOTE_KIND (note),
12177                                                XEXP (note, 0),
12178                                                REG_NOTES (place2));
12179         }
12180     }
12181 }
12182 \f
12183 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12184    I3, I2, and I1 to new locations.  This is also called to add a link
12185    pointing at I3 when I3's destination is changed.  */
12186
12187 static void
12188 distribute_links (rtx links)
12189 {
12190   rtx link, next_link;
12191
12192   for (link = links; link; link = next_link)
12193     {
12194       rtx place = 0;
12195       rtx insn;
12196       rtx set, reg;
12197
12198       next_link = XEXP (link, 1);
12199
12200       /* If the insn that this link points to is a NOTE or isn't a single
12201          set, ignore it.  In the latter case, it isn't clear what we
12202          can do other than ignore the link, since we can't tell which
12203          register it was for.  Such links wouldn't be used by combine
12204          anyway.
12205
12206          It is not possible for the destination of the target of the link to
12207          have been changed by combine.  The only potential of this is if we
12208          replace I3, I2, and I1 by I3 and I2.  But in that case the
12209          destination of I2 also remains unchanged.  */
12210
12211       if (NOTE_P (XEXP (link, 0))
12212           || (set = single_set (XEXP (link, 0))) == 0)
12213         continue;
12214
12215       reg = SET_DEST (set);
12216       while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12217              || GET_CODE (reg) == STRICT_LOW_PART)
12218         reg = XEXP (reg, 0);
12219
12220       /* A LOG_LINK is defined as being placed on the first insn that uses
12221          a register and points to the insn that sets the register.  Start
12222          searching at the next insn after the target of the link and stop
12223          when we reach a set of the register or the end of the basic block.
12224
12225          Note that this correctly handles the link that used to point from
12226          I3 to I2.  Also note that not much searching is typically done here
12227          since most links don't point very far away.  */
12228
12229       for (insn = NEXT_INSN (XEXP (link, 0));
12230            (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
12231                      || BB_HEAD (this_basic_block->next_bb) != insn));
12232            insn = NEXT_INSN (insn))
12233         if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12234           {
12235             if (reg_referenced_p (reg, PATTERN (insn)))
12236               place = insn;
12237             break;
12238           }
12239         else if (CALL_P (insn)
12240                  && find_reg_fusage (insn, USE, reg))
12241           {
12242             place = insn;
12243             break;
12244           }
12245         else if (INSN_P (insn) && reg_set_p (reg, insn))
12246           break;
12247
12248       /* If we found a place to put the link, place it there unless there
12249          is already a link to the same insn as LINK at that point.  */
12250
12251       if (place)
12252         {
12253           rtx link2;
12254
12255           for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12256             if (XEXP (link2, 0) == XEXP (link, 0))
12257               break;
12258
12259           if (link2 == 0)
12260             {
12261               XEXP (link, 1) = LOG_LINKS (place);
12262               LOG_LINKS (place) = link;
12263
12264               /* Set added_links_insn to the earliest insn we added a
12265                  link to.  */
12266               if (added_links_insn == 0
12267                   || INSN_CUID (added_links_insn) > INSN_CUID (place))
12268                 added_links_insn = place;
12269             }
12270         }
12271     }
12272 }
12273 \f
12274 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
12275    Check whether the expression pointer to by LOC is a register or
12276    memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
12277    Otherwise return zero.  */
12278
12279 static int
12280 unmentioned_reg_p_1 (rtx *loc, void *expr)
12281 {
12282   rtx x = *loc;
12283
12284   if (x != NULL_RTX
12285       && (REG_P (x) || MEM_P (x))
12286       && ! reg_mentioned_p (x, (rtx) expr))
12287     return 1;
12288   return 0;
12289 }
12290
12291 /* Check for any register or memory mentioned in EQUIV that is not
12292    mentioned in EXPR.  This is used to restrict EQUIV to "specializations"
12293    of EXPR where some registers may have been replaced by constants.  */
12294
12295 static bool
12296 unmentioned_reg_p (rtx equiv, rtx expr)
12297 {
12298   return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
12299 }
12300 \f
12301 /* Compute INSN_CUID for INSN, which is an insn made by combine.  */
12302
12303 static int
12304 insn_cuid (rtx insn)
12305 {
12306   while (insn != 0 && INSN_UID (insn) > max_uid_cuid
12307          && NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE)
12308     insn = NEXT_INSN (insn);
12309
12310   gcc_assert (INSN_UID (insn) <= max_uid_cuid);
12311
12312   return INSN_CUID (insn);
12313 }
12314 \f
12315 void
12316 dump_combine_stats (FILE *file)
12317 {
12318   fprintf
12319     (file,
12320      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12321      combine_attempts, combine_merges, combine_extras, combine_successes);
12322 }
12323
12324 void
12325 dump_combine_total_stats (FILE *file)
12326 {
12327   fprintf
12328     (file,
12329      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12330      total_attempts, total_merges, total_extras, total_successes);
12331 }
12332 \f
12333
12334 static bool
12335 gate_handle_combine (void)
12336 {
12337   return (optimize > 0);
12338 }
12339
12340 /* Try combining insns through substitution.  */
12341 static void
12342 rest_of_handle_combine (void)
12343 {
12344   int rebuild_jump_labels_after_combine
12345     = combine_instructions (get_insns (), max_reg_num ());
12346
12347   /* Combining insns may have turned an indirect jump into a
12348      direct jump.  Rebuild the JUMP_LABEL fields of jumping
12349      instructions.  */
12350   if (rebuild_jump_labels_after_combine)
12351     {
12352       timevar_push (TV_JUMP);
12353       rebuild_jump_labels (get_insns ());
12354       timevar_pop (TV_JUMP);
12355
12356       delete_dead_jumptables ();
12357       cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE);
12358     }
12359 }
12360
12361 struct tree_opt_pass pass_combine =
12362 {
12363   "combine",                            /* name */
12364   gate_handle_combine,                  /* gate */
12365   rest_of_handle_combine,               /* execute */
12366   NULL,                                 /* sub */
12367   NULL,                                 /* next */
12368   0,                                    /* static_pass_number */
12369   TV_COMBINE,                           /* tv_id */
12370   0,                                    /* properties_required */
12371   0,                                    /* properties_provided */
12372   0,                                    /* properties_destroyed */
12373   0,                                    /* todo_flags_start */
12374   TODO_dump_func |
12375   TODO_ggc_collect,                     /* todo_flags_finish */
12376   'c'                                   /* letter */
12377 };
12378