OSDN Git Service

* function.h (struct function): Add can_throw_non_call_exceptions bit.
[pf3gnuchains/gcc-fork.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /* This module is essentially the "combiner" phase of the U. of Arizona
23    Portable Optimizer, but redone to work on our list-structured
24    representation for RTL instead of their string representation.
25
26    The LOG_LINKS of each insn identify the most recent assignment
27    to each REG used in the insn.  It is a list of previous insns,
28    each of which contains a SET for a REG that is used in this insn
29    and not used or set in between.  LOG_LINKs never cross basic blocks.
30    They were set up by the preceding pass (lifetime analysis).
31
32    We try to combine each pair of insns joined by a logical link.
33    We also try to combine triples of insns A, B and C when
34    C has a link back to B and B has a link back to A.
35
36    LOG_LINKS does not have links for use of the CC0.  They don't
37    need to, because the insn that sets the CC0 is always immediately
38    before the insn that tests it.  So we always regard a branch
39    insn as having a logical link to the preceding insn.  The same is true
40    for an insn explicitly using CC0.
41
42    We check (with use_crosses_set_p) to avoid combining in such a way
43    as to move a computation to a place where its value would be different.
44
45    Combination is done by mathematically substituting the previous
46    insn(s) values for the regs they set into the expressions in
47    the later insns that refer to these regs.  If the result is a valid insn
48    for our target machine, according to the machine description,
49    we install it, delete the earlier insns, and update the data flow
50    information (LOG_LINKS and REG_NOTES) for what we did.
51
52    There are a few exceptions where the dataflow information isn't
53    completely updated (however this is only a local issue since it is
54    regenerated before the next pass that uses it):
55
56    - reg_live_length is not updated
57    - reg_n_refs is not adjusted in the rare case when a register is
58      no longer required in a computation
59    - there are extremely rare cases (see distribute_notes) when a
60      REG_DEAD note is lost
61    - a LOG_LINKS entry that refers to an insn with multiple SETs may be
62      removed because there is no way to know which register it was
63      linking
64
65    To simplify substitution, we combine only when the earlier insn(s)
66    consist of only a single assignment.  To simplify updating afterward,
67    we never combine when a subroutine call appears in the middle.
68
69    Since we do not represent assignments to CC0 explicitly except when that
70    is all an insn does, there is no LOG_LINKS entry in an insn that uses
71    the condition code for the insn that set the condition code.
72    Fortunately, these two insns must be consecutive.
73    Therefore, every JUMP_INSN is taken to have an implicit logical link
74    to the preceding insn.  This is not quite right, since non-jumps can
75    also use the condition code; but in practice such insns would not
76    combine anyway.  */
77
78 #include "config.h"
79 #include "system.h"
80 #include "coretypes.h"
81 #include "tm.h"
82 #include "rtl.h"
83 #include "tree.h"
84 #include "tm_p.h"
85 #include "flags.h"
86 #include "regs.h"
87 #include "hard-reg-set.h"
88 #include "basic-block.h"
89 #include "insn-config.h"
90 #include "function.h"
91 /* Include expr.h after insn-config.h so we get HAVE_conditional_move.  */
92 #include "expr.h"
93 #include "insn-attr.h"
94 #include "recog.h"
95 #include "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 #include "df.h"
106 #include "cgraph.h"
107
108 /* Number of attempts to combine instructions in this function.  */
109
110 static int combine_attempts;
111
112 /* Number of attempts that got as far as substitution in this function.  */
113
114 static int combine_merges;
115
116 /* Number of instructions combined with added SETs in this function.  */
117
118 static int combine_extras;
119
120 /* Number of instructions combined in this function.  */
121
122 static int combine_successes;
123
124 /* Totals over entire compilation.  */
125
126 static int total_attempts, total_merges, total_extras, total_successes;
127
128 /* combine_instructions may try to replace the right hand side of the
129    second instruction with the value of an associated REG_EQUAL note
130    before throwing it at try_combine.  That is problematic when there
131    is a REG_DEAD note for a register used in the old right hand side
132    and can cause distribute_notes to do wrong things.  This is the
133    second instruction if it has been so modified, null otherwise.  */
134
135 static rtx i2mod;
136
137 /* When I2MOD is nonnull, this is a copy of the old right hand side.  */
138
139 static rtx i2mod_old_rhs;
140
141 /* When I2MOD is nonnull, this is a copy of the new right hand side.  */
142
143 static rtx i2mod_new_rhs;
144 \f
145 typedef struct reg_stat_struct {
146   /* Record last point of death of (hard or pseudo) register n.  */
147   rtx                           last_death;
148
149   /* Record last point of modification of (hard or pseudo) register n.  */
150   rtx                           last_set;
151
152   /* The next group of fields allows the recording of the last value assigned
153      to (hard or pseudo) register n.  We use this information to see if an
154      operation being processed is redundant given a prior operation performed
155      on the register.  For example, an `and' with a constant is redundant if
156      all the zero bits are already known to be turned off.
157
158      We use an approach similar to that used by cse, but change it in the
159      following ways:
160
161      (1) We do not want to reinitialize at each label.
162      (2) It is useful, but not critical, to know the actual value assigned
163          to a register.  Often just its form is helpful.
164
165      Therefore, we maintain the following fields:
166
167      last_set_value             the last value assigned
168      last_set_label             records the value of label_tick when the
169                                 register was assigned
170      last_set_table_tick        records the value of label_tick when a
171                                 value using the register is assigned
172      last_set_invalid           set to nonzero when it is not valid
173                                 to use the value of this register in some
174                                 register's value
175
176      To understand the usage of these tables, it is important to understand
177      the distinction between the value in last_set_value being valid and
178      the register being validly contained in some other expression in the
179      table.
180
181      (The next two parameters are out of date).
182
183      reg_stat[i].last_set_value is valid if it is nonzero, and either
184      reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
185
186      Register I may validly appear in any expression returned for the value
187      of another register if reg_n_sets[i] is 1.  It may also appear in the
188      value for register J if reg_stat[j].last_set_invalid is zero, or
189      reg_stat[i].last_set_label < reg_stat[j].last_set_label.
190
191      If an expression is found in the table containing a register which may
192      not validly appear in an expression, the register is replaced by
193      something that won't match, (clobber (const_int 0)).  */
194
195   /* Record last value assigned to (hard or pseudo) register n.  */
196
197   rtx                           last_set_value;
198
199   /* Record the value of label_tick when an expression involving register n
200      is placed in last_set_value.  */
201
202   int                           last_set_table_tick;
203
204   /* Record the value of label_tick when the value for register n is placed in
205      last_set_value.  */
206
207   int                           last_set_label;
208
209   /* These fields are maintained in parallel with last_set_value and are
210      used to store the mode in which the register was last set, the bits
211      that were known to be zero when it was last set, and the number of
212      sign bits copies it was known to have when it was last set.  */
213
214   unsigned HOST_WIDE_INT        last_set_nonzero_bits;
215   char                          last_set_sign_bit_copies;
216   ENUM_BITFIELD(machine_mode)   last_set_mode : 8;
217
218   /* Set nonzero if references to register n in expressions should not be
219      used.  last_set_invalid is set nonzero when this register is being
220      assigned to and last_set_table_tick == label_tick.  */
221
222   char                          last_set_invalid;
223
224   /* Some registers that are set more than once and used in more than one
225      basic block are nevertheless always set in similar ways.  For example,
226      a QImode register may be loaded from memory in two places on a machine
227      where byte loads zero extend.
228
229      We record in the following fields if a register has some leading bits
230      that are always equal to the sign bit, and what we know about the
231      nonzero bits of a register, specifically which bits are known to be
232      zero.
233
234      If an entry is zero, it means that we don't know anything special.  */
235
236   unsigned char                 sign_bit_copies;
237
238   unsigned HOST_WIDE_INT        nonzero_bits;
239
240   /* Record the value of the label_tick when the last truncation
241      happened.  The field truncated_to_mode is only valid if
242      truncation_label == label_tick.  */
243
244   int                           truncation_label;
245
246   /* Record the last truncation seen for this register.  If truncation
247      is not a nop to this mode we might be able to save an explicit
248      truncation if we know that value already contains a truncated
249      value.  */
250
251   ENUM_BITFIELD(machine_mode)   truncated_to_mode : 8;
252 } reg_stat_type;
253
254 DEF_VEC_O(reg_stat_type);
255 DEF_VEC_ALLOC_O(reg_stat_type,heap);
256
257 static VEC(reg_stat_type,heap) *reg_stat;
258
259 /* Record the luid of the last insn that invalidated memory
260    (anything that writes memory, and subroutine calls, but not pushes).  */
261
262 static int mem_last_set;
263
264 /* Record the luid of the last CALL_INSN
265    so we can tell whether a potential combination crosses any calls.  */
266
267 static int last_call_luid;
268
269 /* When `subst' is called, this is the insn that is being modified
270    (by combining in a previous insn).  The PATTERN of this insn
271    is still the old pattern partially modified and it should not be
272    looked at, but this may be used to examine the successors of the insn
273    to judge whether a simplification is valid.  */
274
275 static rtx subst_insn;
276
277 /* This is the lowest LUID that `subst' is currently dealing with.
278    get_last_value will not return a value if the register was set at or
279    after this LUID.  If not for this mechanism, we could get confused if
280    I2 or I1 in try_combine were an insn that used the old value of a register
281    to obtain a new value.  In that case, we might erroneously get the
282    new value of the register when we wanted the old one.  */
283
284 static int subst_low_luid;
285
286 /* This contains any hard registers that are used in newpat; reg_dead_at_p
287    must consider all these registers to be always live.  */
288
289 static HARD_REG_SET newpat_used_regs;
290
291 /* This is an insn to which a LOG_LINKS entry has been added.  If this
292    insn is the earlier than I2 or I3, combine should rescan starting at
293    that location.  */
294
295 static rtx added_links_insn;
296
297 /* Basic block in which we are performing combines.  */
298 static basic_block this_basic_block;
299 static bool optimize_this_for_speed_p;
300
301 \f
302 /* Length of the currently allocated uid_insn_cost array.  */
303
304 static int max_uid_known;
305
306 /* The following array records the insn_rtx_cost for every insn
307    in the instruction stream.  */
308
309 static int *uid_insn_cost;
310
311 /* The following array records the LOG_LINKS for every insn in the
312    instruction stream as an INSN_LIST rtx.  */
313
314 static rtx *uid_log_links;
315
316 #define INSN_COST(INSN)         (uid_insn_cost[INSN_UID (INSN)])
317 #define LOG_LINKS(INSN)         (uid_log_links[INSN_UID (INSN)])
318
319 /* Incremented for each basic block.  */
320
321 static int label_tick;
322
323 /* Reset to label_tick for each extended basic block in scanning order.  */
324
325 static int label_tick_ebb_start;
326
327 /* Mode used to compute significance in reg_stat[].nonzero_bits.  It is the
328    largest integer mode that can fit in HOST_BITS_PER_WIDE_INT.  */
329
330 static enum machine_mode nonzero_bits_mode;
331
332 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
333    be safely used.  It is zero while computing them and after combine has
334    completed.  This former test prevents propagating values based on
335    previously set values, which can be incorrect if a variable is modified
336    in a loop.  */
337
338 static int nonzero_sign_valid;
339
340 \f
341 /* Record one modification to rtl structure
342    to be undone by storing old_contents into *where.  */
343
344 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE };
345
346 struct undo
347 {
348   struct undo *next;
349   enum undo_kind kind;
350   union { rtx r; int i; enum machine_mode m; } old_contents;
351   union { rtx *r; int *i; } where;
352 };
353
354 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
355    num_undo says how many are currently recorded.
356
357    other_insn is nonzero if we have modified some other insn in the process
358    of working on subst_insn.  It must be verified too.  */
359
360 struct undobuf
361 {
362   struct undo *undos;
363   struct undo *frees;
364   rtx other_insn;
365 };
366
367 static struct undobuf undobuf;
368
369 /* Number of times the pseudo being substituted for
370    was found and replaced.  */
371
372 static int n_occurrences;
373
374 static rtx reg_nonzero_bits_for_combine (const_rtx, enum machine_mode, const_rtx,
375                                          enum machine_mode,
376                                          unsigned HOST_WIDE_INT,
377                                          unsigned HOST_WIDE_INT *);
378 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, enum machine_mode, const_rtx,
379                                                 enum machine_mode,
380                                                 unsigned int, unsigned int *);
381 static void do_SUBST (rtx *, rtx);
382 static void do_SUBST_INT (int *, int);
383 static void init_reg_last (void);
384 static void setup_incoming_promotions (rtx);
385 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
386 static int cant_combine_insn_p (rtx);
387 static int can_combine_p (rtx, rtx, rtx, rtx, rtx *, rtx *);
388 static int combinable_i3pat (rtx, rtx *, rtx, rtx, int, rtx *);
389 static int contains_muldiv (rtx);
390 static rtx try_combine (rtx, rtx, rtx, int *);
391 static void undo_all (void);
392 static void undo_commit (void);
393 static rtx *find_split_point (rtx *, rtx, bool);
394 static rtx subst (rtx, rtx, rtx, int, int);
395 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
396 static rtx simplify_if_then_else (rtx);
397 static rtx simplify_set (rtx);
398 static rtx simplify_logical (rtx);
399 static rtx expand_compound_operation (rtx);
400 static const_rtx expand_field_assignment (const_rtx);
401 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
402                             rtx, unsigned HOST_WIDE_INT, int, int, int);
403 static rtx extract_left_shift (rtx, int);
404 static rtx make_compound_operation (rtx, enum rtx_code);
405 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
406                               unsigned HOST_WIDE_INT *);
407 static rtx canon_reg_for_combine (rtx, rtx);
408 static rtx force_to_mode (rtx, enum machine_mode,
409                           unsigned HOST_WIDE_INT, int);
410 static rtx if_then_else_cond (rtx, rtx *, rtx *);
411 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
412 static int rtx_equal_for_field_assignment_p (rtx, rtx);
413 static rtx make_field_assignment (rtx);
414 static rtx apply_distributive_law (rtx);
415 static rtx distribute_and_simplify_rtx (rtx, int);
416 static rtx simplify_and_const_int_1 (enum machine_mode, rtx,
417                                      unsigned HOST_WIDE_INT);
418 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
419                                    unsigned HOST_WIDE_INT);
420 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
421                             HOST_WIDE_INT, enum machine_mode, int *);
422 static rtx simplify_shift_const_1 (enum rtx_code, enum machine_mode, rtx, int);
423 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
424                                  int);
425 static int recog_for_combine (rtx *, rtx, rtx *);
426 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
427 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
428 static void update_table_tick (rtx);
429 static void record_value_for_reg (rtx, rtx, rtx);
430 static void check_promoted_subreg (rtx, rtx);
431 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
432 static void record_dead_and_set_regs (rtx);
433 static int get_last_value_validate (rtx *, rtx, int, int);
434 static rtx get_last_value (const_rtx);
435 static int use_crosses_set_p (const_rtx, int);
436 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
437 static int reg_dead_at_p (rtx, rtx);
438 static void move_deaths (rtx, rtx, int, rtx, rtx *);
439 static int reg_bitfield_target_p (rtx, rtx);
440 static void distribute_notes (rtx, rtx, rtx, rtx, rtx, rtx);
441 static void distribute_links (rtx);
442 static void mark_used_regs_combine (rtx);
443 static void record_promoted_value (rtx, rtx);
444 static int unmentioned_reg_p_1 (rtx *, void *);
445 static bool unmentioned_reg_p (rtx, rtx);
446 static int record_truncated_value (rtx *, void *);
447 static void record_truncated_values (rtx *, void *);
448 static bool reg_truncated_to_mode (enum machine_mode, const_rtx);
449 static rtx gen_lowpart_or_truncate (enum machine_mode, rtx);
450 \f
451
452 /* It is not safe to use ordinary gen_lowpart in combine.
453    See comments in gen_lowpart_for_combine.  */
454 #undef RTL_HOOKS_GEN_LOWPART
455 #define RTL_HOOKS_GEN_LOWPART              gen_lowpart_for_combine
456
457 /* Our implementation of gen_lowpart never emits a new pseudo.  */
458 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
459 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT      gen_lowpart_for_combine
460
461 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
462 #define RTL_HOOKS_REG_NONZERO_REG_BITS     reg_nonzero_bits_for_combine
463
464 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
465 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES  reg_num_sign_bit_copies_for_combine
466
467 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
468 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE    reg_truncated_to_mode
469
470 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
471
472 \f
473 /* Try to split PATTERN found in INSN.  This returns NULL_RTX if
474    PATTERN can not be split.  Otherwise, it returns an insn sequence.
475    This is a wrapper around split_insns which ensures that the
476    reg_stat vector is made larger if the splitter creates a new
477    register.  */
478
479 static rtx
480 combine_split_insns (rtx pattern, rtx insn)
481 {
482   rtx ret;
483   unsigned int nregs;
484
485   ret = split_insns (pattern, insn);
486   nregs = max_reg_num ();
487   if (nregs > VEC_length (reg_stat_type, reg_stat))
488     VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
489   return ret;
490 }
491
492 /* This is used by find_single_use to locate an rtx in LOC that
493    contains exactly one use of DEST, which is typically either a REG
494    or CC0.  It returns a pointer to the innermost rtx expression
495    containing DEST.  Appearances of DEST that are being used to
496    totally replace it are not counted.  */
497
498 static rtx *
499 find_single_use_1 (rtx dest, rtx *loc)
500 {
501   rtx x = *loc;
502   enum rtx_code code = GET_CODE (x);
503   rtx *result = NULL;
504   rtx *this_result;
505   int i;
506   const char *fmt;
507
508   switch (code)
509     {
510     case CONST_INT:
511     case CONST:
512     case LABEL_REF:
513     case SYMBOL_REF:
514     case CONST_DOUBLE:
515     case CONST_VECTOR:
516     case CLOBBER:
517       return 0;
518
519     case SET:
520       /* If the destination is anything other than CC0, PC, a REG or a SUBREG
521          of a REG that occupies all of the REG, the insn uses DEST if
522          it is mentioned in the destination or the source.  Otherwise, we
523          need just check the source.  */
524       if (GET_CODE (SET_DEST (x)) != CC0
525           && GET_CODE (SET_DEST (x)) != PC
526           && !REG_P (SET_DEST (x))
527           && ! (GET_CODE (SET_DEST (x)) == SUBREG
528                 && REG_P (SUBREG_REG (SET_DEST (x)))
529                 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
530                       + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
531                     == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
532                          + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
533         break;
534
535       return find_single_use_1 (dest, &SET_SRC (x));
536
537     case MEM:
538     case SUBREG:
539       return find_single_use_1 (dest, &XEXP (x, 0));
540
541     default:
542       break;
543     }
544
545   /* If it wasn't one of the common cases above, check each expression and
546      vector of this code.  Look for a unique usage of DEST.  */
547
548   fmt = GET_RTX_FORMAT (code);
549   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
550     {
551       if (fmt[i] == 'e')
552         {
553           if (dest == XEXP (x, i)
554               || (REG_P (dest) && REG_P (XEXP (x, i))
555                   && REGNO (dest) == REGNO (XEXP (x, i))))
556             this_result = loc;
557           else
558             this_result = find_single_use_1 (dest, &XEXP (x, i));
559
560           if (result == NULL)
561             result = this_result;
562           else if (this_result)
563             /* Duplicate usage.  */
564             return NULL;
565         }
566       else if (fmt[i] == 'E')
567         {
568           int j;
569
570           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
571             {
572               if (XVECEXP (x, i, j) == dest
573                   || (REG_P (dest)
574                       && REG_P (XVECEXP (x, i, j))
575                       && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
576                 this_result = loc;
577               else
578                 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
579
580               if (result == NULL)
581                 result = this_result;
582               else if (this_result)
583                 return NULL;
584             }
585         }
586     }
587
588   return result;
589 }
590
591
592 /* See if DEST, produced in INSN, is used only a single time in the
593    sequel.  If so, return a pointer to the innermost rtx expression in which
594    it is used.
595
596    If PLOC is nonzero, *PLOC is set to the insn containing the single use.
597
598    If DEST is cc0_rtx, we look only at the next insn.  In that case, we don't
599    care about REG_DEAD notes or LOG_LINKS.
600
601    Otherwise, we find the single use by finding an insn that has a
602    LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST.  If DEST is
603    only referenced once in that insn, we know that it must be the first
604    and last insn referencing DEST.  */
605
606 static rtx *
607 find_single_use (rtx dest, rtx insn, rtx *ploc)
608 {
609   basic_block bb;
610   rtx next;
611   rtx *result;
612   rtx link;
613
614 #ifdef HAVE_cc0
615   if (dest == cc0_rtx)
616     {
617       next = NEXT_INSN (insn);
618       if (next == 0
619           || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
620         return 0;
621
622       result = find_single_use_1 (dest, &PATTERN (next));
623       if (result && ploc)
624         *ploc = next;
625       return result;
626     }
627 #endif
628
629   if (!REG_P (dest))
630     return 0;
631
632   bb = BLOCK_FOR_INSN (insn);
633   for (next = NEXT_INSN (insn);
634        next && BLOCK_FOR_INSN (next) == bb;
635        next = NEXT_INSN (next))
636     if (INSN_P (next) && dead_or_set_p (next, dest))
637       {
638         for (link = LOG_LINKS (next); link; link = XEXP (link, 1))
639           if (XEXP (link, 0) == insn)
640             break;
641
642         if (link)
643           {
644             result = find_single_use_1 (dest, &PATTERN (next));
645             if (ploc)
646               *ploc = next;
647             return result;
648           }
649       }
650
651   return 0;
652 }
653 \f
654 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
655    insn.  The substitution can be undone by undo_all.  If INTO is already
656    set to NEWVAL, do not record this change.  Because computing NEWVAL might
657    also call SUBST, we have to compute it before we put anything into
658    the undo table.  */
659
660 static void
661 do_SUBST (rtx *into, rtx newval)
662 {
663   struct undo *buf;
664   rtx oldval = *into;
665
666   if (oldval == newval)
667     return;
668
669   /* We'd like to catch as many invalid transformations here as
670      possible.  Unfortunately, there are way too many mode changes
671      that are perfectly valid, so we'd waste too much effort for
672      little gain doing the checks here.  Focus on catching invalid
673      transformations involving integer constants.  */
674   if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
675       && CONST_INT_P (newval))
676     {
677       /* Sanity check that we're replacing oldval with a CONST_INT
678          that is a valid sign-extension for the original mode.  */
679       gcc_assert (INTVAL (newval)
680                   == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
681
682       /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
683          CONST_INT is not valid, because after the replacement, the
684          original mode would be gone.  Unfortunately, we can't tell
685          when do_SUBST is called to replace the operand thereof, so we
686          perform this test on oldval instead, checking whether an
687          invalid replacement took place before we got here.  */
688       gcc_assert (!(GET_CODE (oldval) == SUBREG
689                     && CONST_INT_P (SUBREG_REG (oldval))));
690       gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
691                     && CONST_INT_P (XEXP (oldval, 0))));
692     }
693
694   if (undobuf.frees)
695     buf = undobuf.frees, undobuf.frees = buf->next;
696   else
697     buf = XNEW (struct undo);
698
699   buf->kind = UNDO_RTX;
700   buf->where.r = into;
701   buf->old_contents.r = oldval;
702   *into = newval;
703
704   buf->next = undobuf.undos, undobuf.undos = buf;
705 }
706
707 #define SUBST(INTO, NEWVAL)     do_SUBST(&(INTO), (NEWVAL))
708
709 /* Similar to SUBST, but NEWVAL is an int expression.  Note that substitution
710    for the value of a HOST_WIDE_INT value (including CONST_INT) is
711    not safe.  */
712
713 static void
714 do_SUBST_INT (int *into, int newval)
715 {
716   struct undo *buf;
717   int oldval = *into;
718
719   if (oldval == newval)
720     return;
721
722   if (undobuf.frees)
723     buf = undobuf.frees, undobuf.frees = buf->next;
724   else
725     buf = XNEW (struct undo);
726
727   buf->kind = UNDO_INT;
728   buf->where.i = into;
729   buf->old_contents.i = oldval;
730   *into = newval;
731
732   buf->next = undobuf.undos, undobuf.undos = buf;
733 }
734
735 #define SUBST_INT(INTO, NEWVAL)  do_SUBST_INT(&(INTO), (NEWVAL))
736
737 /* Similar to SUBST, but just substitute the mode.  This is used when
738    changing the mode of a pseudo-register, so that any other
739    references to the entry in the regno_reg_rtx array will change as
740    well.  */
741
742 static void
743 do_SUBST_MODE (rtx *into, enum machine_mode newval)
744 {
745   struct undo *buf;
746   enum machine_mode oldval = GET_MODE (*into);
747
748   if (oldval == newval)
749     return;
750
751   if (undobuf.frees)
752     buf = undobuf.frees, undobuf.frees = buf->next;
753   else
754     buf = XNEW (struct undo);
755
756   buf->kind = UNDO_MODE;
757   buf->where.r = into;
758   buf->old_contents.m = oldval;
759   adjust_reg_mode (*into, newval);
760
761   buf->next = undobuf.undos, undobuf.undos = buf;
762 }
763
764 #define SUBST_MODE(INTO, NEWVAL)  do_SUBST_MODE(&(INTO), (NEWVAL))
765 \f
766 /* Subroutine of try_combine.  Determine whether the combine replacement
767    patterns NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to
768    insn_rtx_cost that the original instruction sequence I1, I2, I3 and
769    undobuf.other_insn.  Note that I1 and/or NEWI2PAT may be NULL_RTX.
770    NEWOTHERPAT and undobuf.other_insn may also both be NULL_RTX.  This
771    function returns false, if the costs of all instructions can be
772    estimated, and the replacements are more expensive than the original
773    sequence.  */
774
775 static bool
776 combine_validate_cost (rtx i1, rtx i2, rtx i3, rtx newpat, rtx newi2pat,
777                        rtx newotherpat)
778 {
779   int i1_cost, i2_cost, i3_cost;
780   int new_i2_cost, new_i3_cost;
781   int old_cost, new_cost;
782
783   /* Lookup the original insn_rtx_costs.  */
784   i2_cost = INSN_COST (i2);
785   i3_cost = INSN_COST (i3);
786
787   if (i1)
788     {
789       i1_cost = INSN_COST (i1);
790       old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0)
791                  ? i1_cost + i2_cost + i3_cost : 0;
792     }
793   else
794     {
795       old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
796       i1_cost = 0;
797     }
798
799   /* Calculate the replacement insn_rtx_costs.  */
800   new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
801   if (newi2pat)
802     {
803       new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
804       new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
805                  ? new_i2_cost + new_i3_cost : 0;
806     }
807   else
808     {
809       new_cost = new_i3_cost;
810       new_i2_cost = 0;
811     }
812
813   if (undobuf.other_insn)
814     {
815       int old_other_cost, new_other_cost;
816
817       old_other_cost = INSN_COST (undobuf.other_insn);
818       new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
819       if (old_other_cost > 0 && new_other_cost > 0)
820         {
821           old_cost += old_other_cost;
822           new_cost += new_other_cost;
823         }
824       else
825         old_cost = 0;
826     }
827
828   /* Disallow this recombination if both new_cost and old_cost are
829      greater than zero, and new_cost is greater than old cost.  */
830   if (old_cost > 0
831       && new_cost > old_cost)
832     {
833       if (dump_file)
834         {
835           if (i1)
836             {
837               fprintf (dump_file,
838                        "rejecting combination of insns %d, %d and %d\n",
839                        INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
840               fprintf (dump_file, "original costs %d + %d + %d = %d\n",
841                        i1_cost, i2_cost, i3_cost, old_cost);
842             }
843           else
844             {
845               fprintf (dump_file,
846                        "rejecting combination of insns %d and %d\n",
847                        INSN_UID (i2), INSN_UID (i3));
848               fprintf (dump_file, "original costs %d + %d = %d\n",
849                        i2_cost, i3_cost, old_cost);
850             }
851
852           if (newi2pat)
853             {
854               fprintf (dump_file, "replacement costs %d + %d = %d\n",
855                        new_i2_cost, new_i3_cost, new_cost);
856             }
857           else
858             fprintf (dump_file, "replacement cost %d\n", new_cost);
859         }
860
861       return false;
862     }
863
864   /* Update the uid_insn_cost array with the replacement costs.  */
865   INSN_COST (i2) = new_i2_cost;
866   INSN_COST (i3) = new_i3_cost;
867   if (i1)
868     INSN_COST (i1) = 0;
869
870   return true;
871 }
872
873
874 /* Delete any insns that copy a register to itself.  */
875
876 static void
877 delete_noop_moves (void)
878 {
879   rtx insn, next;
880   basic_block bb;
881
882   FOR_EACH_BB (bb)
883     {
884       for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
885         {
886           next = NEXT_INSN (insn);
887           if (INSN_P (insn) && noop_move_p (insn))
888             {
889               if (dump_file)
890                 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
891
892               delete_insn_and_edges (insn);
893             }
894         }
895     }
896 }
897
898 \f
899 /* Fill in log links field for all insns.  */
900
901 static void
902 create_log_links (void)
903 {
904   basic_block bb;
905   rtx *next_use, insn;
906   df_ref *def_vec, *use_vec;
907
908   next_use = XCNEWVEC (rtx, max_reg_num ());
909
910   /* Pass through each block from the end, recording the uses of each
911      register and establishing log links when def is encountered.
912      Note that we do not clear next_use array in order to save time,
913      so we have to test whether the use is in the same basic block as def.
914
915      There are a few cases below when we do not consider the definition or
916      usage -- these are taken from original flow.c did. Don't ask me why it is
917      done this way; I don't know and if it works, I don't want to know.  */
918
919   FOR_EACH_BB (bb)
920     {
921       FOR_BB_INSNS_REVERSE (bb, insn)
922         {
923           if (!NONDEBUG_INSN_P (insn))
924             continue;
925
926           /* Log links are created only once.  */
927           gcc_assert (!LOG_LINKS (insn));
928
929           for (def_vec = DF_INSN_DEFS (insn); *def_vec; def_vec++)
930             {
931               df_ref def = *def_vec;
932               int regno = DF_REF_REGNO (def);
933               rtx use_insn;
934
935               if (!next_use[regno])
936                 continue;
937
938               /* Do not consider if it is pre/post modification in MEM.  */
939               if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
940                 continue;
941
942               /* Do not make the log link for frame pointer.  */
943               if ((regno == FRAME_POINTER_REGNUM
944                    && (! reload_completed || frame_pointer_needed))
945 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
946                   || (regno == HARD_FRAME_POINTER_REGNUM
947                       && (! reload_completed || frame_pointer_needed))
948 #endif
949 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
950                   || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
951 #endif
952                   )
953                 continue;
954
955               use_insn = next_use[regno];
956               if (BLOCK_FOR_INSN (use_insn) == bb)
957                 {
958                   /* flow.c claimed:
959
960                      We don't build a LOG_LINK for hard registers contained
961                      in ASM_OPERANDs.  If these registers get replaced,
962                      we might wind up changing the semantics of the insn,
963                      even if reload can make what appear to be valid
964                      assignments later.  */
965                   if (regno >= FIRST_PSEUDO_REGISTER
966                       || asm_noperands (PATTERN (use_insn)) < 0)
967                     {
968                       /* Don't add duplicate links between instructions.  */
969                       rtx links;
970                       for (links = LOG_LINKS (use_insn); links;
971                            links = XEXP (links, 1))
972                         if (insn == XEXP (links, 0))
973                           break;
974
975                       if (!links)
976                         LOG_LINKS (use_insn) =
977                           alloc_INSN_LIST (insn, LOG_LINKS (use_insn));
978                     }
979                 }
980               next_use[regno] = NULL_RTX;
981             }
982
983           for (use_vec = DF_INSN_USES (insn); *use_vec; use_vec++)
984             {
985               df_ref use = *use_vec;
986               int regno = DF_REF_REGNO (use);
987
988               /* Do not consider the usage of the stack pointer
989                  by function call.  */
990               if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
991                 continue;
992
993               next_use[regno] = insn;
994             }
995         }
996     }
997
998   free (next_use);
999 }
1000
1001 /* Clear LOG_LINKS fields of insns.  */
1002
1003 static void
1004 clear_log_links (void)
1005 {
1006   rtx insn;
1007
1008   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1009     if (INSN_P (insn))
1010       free_INSN_LIST_list (&LOG_LINKS (insn));
1011 }
1012 \f
1013 /* Main entry point for combiner.  F is the first insn of the function.
1014    NREGS is the first unused pseudo-reg number.
1015
1016    Return nonzero if the combiner has turned an indirect jump
1017    instruction into a direct jump.  */
1018 static int
1019 combine_instructions (rtx f, unsigned int nregs)
1020 {
1021   rtx insn, next;
1022 #ifdef HAVE_cc0
1023   rtx prev;
1024 #endif
1025   rtx links, nextlinks;
1026   rtx first;
1027   basic_block last_bb;
1028
1029   int new_direct_jump_p = 0;
1030
1031   for (first = f; first && !INSN_P (first); )
1032     first = NEXT_INSN (first);
1033   if (!first)
1034     return 0;
1035
1036   combine_attempts = 0;
1037   combine_merges = 0;
1038   combine_extras = 0;
1039   combine_successes = 0;
1040
1041   rtl_hooks = combine_rtl_hooks;
1042
1043   VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
1044
1045   init_recog_no_volatile ();
1046
1047   /* Allocate array for insn info.  */
1048   max_uid_known = get_max_uid ();
1049   uid_log_links = XCNEWVEC (rtx, max_uid_known + 1);
1050   uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1051
1052   nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1053
1054   /* Don't use reg_stat[].nonzero_bits when computing it.  This can cause
1055      problems when, for example, we have j <<= 1 in a loop.  */
1056
1057   nonzero_sign_valid = 0;
1058   label_tick = label_tick_ebb_start = 1;
1059
1060   /* Scan all SETs and see if we can deduce anything about what
1061      bits are known to be zero for some registers and how many copies
1062      of the sign bit are known to exist for those registers.
1063
1064      Also set any known values so that we can use it while searching
1065      for what bits are known to be set.  */
1066
1067   setup_incoming_promotions (first);
1068   /* Allow the entry block and the first block to fall into the same EBB.
1069      Conceptually the incoming promotions are assigned to the entry block.  */
1070   last_bb = ENTRY_BLOCK_PTR;
1071
1072   create_log_links ();
1073   FOR_EACH_BB (this_basic_block)
1074     {
1075       optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1076       last_call_luid = 0;
1077       mem_last_set = -1;
1078
1079       label_tick++;
1080       if (!single_pred_p (this_basic_block)
1081           || single_pred (this_basic_block) != last_bb)
1082         label_tick_ebb_start = label_tick;
1083       last_bb = this_basic_block;
1084
1085       FOR_BB_INSNS (this_basic_block, insn)
1086         if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1087           {
1088             subst_low_luid = DF_INSN_LUID (insn);
1089             subst_insn = insn;
1090
1091             note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1092                          insn);
1093             record_dead_and_set_regs (insn);
1094
1095 #ifdef AUTO_INC_DEC
1096             for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1097               if (REG_NOTE_KIND (links) == REG_INC)
1098                 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1099                                                   insn);
1100 #endif
1101
1102             /* Record the current insn_rtx_cost of this instruction.  */
1103             if (NONJUMP_INSN_P (insn))
1104               INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1105                                                 optimize_this_for_speed_p);
1106             if (dump_file)
1107               fprintf(dump_file, "insn_cost %d: %d\n",
1108                     INSN_UID (insn), INSN_COST (insn));
1109           }
1110     }
1111
1112   nonzero_sign_valid = 1;
1113
1114   /* Now scan all the insns in forward order.  */
1115   label_tick = label_tick_ebb_start = 1;
1116   init_reg_last ();
1117   setup_incoming_promotions (first);
1118   last_bb = ENTRY_BLOCK_PTR;
1119
1120   FOR_EACH_BB (this_basic_block)
1121     {
1122       optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1123       last_call_luid = 0;
1124       mem_last_set = -1;
1125
1126       label_tick++;
1127       if (!single_pred_p (this_basic_block)
1128           || single_pred (this_basic_block) != last_bb)
1129         label_tick_ebb_start = label_tick;
1130       last_bb = this_basic_block;
1131
1132       rtl_profile_for_bb (this_basic_block);
1133       for (insn = BB_HEAD (this_basic_block);
1134            insn != NEXT_INSN (BB_END (this_basic_block));
1135            insn = next ? next : NEXT_INSN (insn))
1136         {
1137           next = 0;
1138           if (NONDEBUG_INSN_P (insn))
1139             {
1140               /* See if we know about function return values before this
1141                  insn based upon SUBREG flags.  */
1142               check_promoted_subreg (insn, PATTERN (insn));
1143
1144               /* See if we can find hardregs and subreg of pseudos in
1145                  narrower modes.  This could help turning TRUNCATEs
1146                  into SUBREGs.  */
1147               note_uses (&PATTERN (insn), record_truncated_values, NULL);
1148
1149               /* Try this insn with each insn it links back to.  */
1150
1151               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1152                 if ((next = try_combine (insn, XEXP (links, 0),
1153                                          NULL_RTX, &new_direct_jump_p)) != 0)
1154                   goto retry;
1155
1156               /* Try each sequence of three linked insns ending with this one.  */
1157
1158               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1159                 {
1160                   rtx link = XEXP (links, 0);
1161
1162                   /* If the linked insn has been replaced by a note, then there
1163                      is no point in pursuing this chain any further.  */
1164                   if (NOTE_P (link))
1165                     continue;
1166
1167                   for (nextlinks = LOG_LINKS (link);
1168                        nextlinks;
1169                        nextlinks = XEXP (nextlinks, 1))
1170                     if ((next = try_combine (insn, link,
1171                                              XEXP (nextlinks, 0),
1172                                              &new_direct_jump_p)) != 0)
1173                       goto retry;
1174                 }
1175
1176 #ifdef HAVE_cc0
1177               /* Try to combine a jump insn that uses CC0
1178                  with a preceding insn that sets CC0, and maybe with its
1179                  logical predecessor as well.
1180                  This is how we make decrement-and-branch insns.
1181                  We need this special code because data flow connections
1182                  via CC0 do not get entered in LOG_LINKS.  */
1183
1184               if (JUMP_P (insn)
1185                   && (prev = prev_nonnote_insn (insn)) != 0
1186                   && NONJUMP_INSN_P (prev)
1187                   && sets_cc0_p (PATTERN (prev)))
1188                 {
1189                   if ((next = try_combine (insn, prev,
1190                                            NULL_RTX, &new_direct_jump_p)) != 0)
1191                     goto retry;
1192
1193                   for (nextlinks = LOG_LINKS (prev); nextlinks;
1194                        nextlinks = XEXP (nextlinks, 1))
1195                     if ((next = try_combine (insn, prev,
1196                                              XEXP (nextlinks, 0),
1197                                              &new_direct_jump_p)) != 0)
1198                       goto retry;
1199                 }
1200
1201               /* Do the same for an insn that explicitly references CC0.  */
1202               if (NONJUMP_INSN_P (insn)
1203                   && (prev = prev_nonnote_insn (insn)) != 0
1204                   && NONJUMP_INSN_P (prev)
1205                   && sets_cc0_p (PATTERN (prev))
1206                   && GET_CODE (PATTERN (insn)) == SET
1207                   && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1208                 {
1209                   if ((next = try_combine (insn, prev,
1210                                            NULL_RTX, &new_direct_jump_p)) != 0)
1211                     goto retry;
1212
1213                   for (nextlinks = LOG_LINKS (prev); nextlinks;
1214                        nextlinks = XEXP (nextlinks, 1))
1215                     if ((next = try_combine (insn, prev,
1216                                              XEXP (nextlinks, 0),
1217                                              &new_direct_jump_p)) != 0)
1218                       goto retry;
1219                 }
1220
1221               /* Finally, see if any of the insns that this insn links to
1222                  explicitly references CC0.  If so, try this insn, that insn,
1223                  and its predecessor if it sets CC0.  */
1224               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1225                 if (NONJUMP_INSN_P (XEXP (links, 0))
1226                     && GET_CODE (PATTERN (XEXP (links, 0))) == SET
1227                     && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
1228                     && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
1229                     && NONJUMP_INSN_P (prev)
1230                     && sets_cc0_p (PATTERN (prev))
1231                     && (next = try_combine (insn, XEXP (links, 0),
1232                                             prev, &new_direct_jump_p)) != 0)
1233                   goto retry;
1234 #endif
1235
1236               /* Try combining an insn with two different insns whose results it
1237                  uses.  */
1238               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1239                 for (nextlinks = XEXP (links, 1); nextlinks;
1240                      nextlinks = XEXP (nextlinks, 1))
1241                   if ((next = try_combine (insn, XEXP (links, 0),
1242                                            XEXP (nextlinks, 0),
1243                                            &new_direct_jump_p)) != 0)
1244                     goto retry;
1245
1246               /* Try this insn with each REG_EQUAL note it links back to.  */
1247               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1248                 {
1249                   rtx set, note;
1250                   rtx temp = XEXP (links, 0);
1251                   if ((set = single_set (temp)) != 0
1252                       && (note = find_reg_equal_equiv_note (temp)) != 0
1253                       && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1254                       /* Avoid using a register that may already been marked
1255                          dead by an earlier instruction.  */
1256                       && ! unmentioned_reg_p (note, SET_SRC (set))
1257                       && (GET_MODE (note) == VOIDmode
1258                           ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1259                           : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1260                     {
1261                       /* Temporarily replace the set's source with the
1262                          contents of the REG_EQUAL note.  The insn will
1263                          be deleted or recognized by try_combine.  */
1264                       rtx orig = SET_SRC (set);
1265                       SET_SRC (set) = note;
1266                       i2mod = temp;
1267                       i2mod_old_rhs = copy_rtx (orig);
1268                       i2mod_new_rhs = copy_rtx (note);
1269                       next = try_combine (insn, i2mod, NULL_RTX,
1270                                           &new_direct_jump_p);
1271                       i2mod = NULL_RTX;
1272                       if (next)
1273                         goto retry;
1274                       SET_SRC (set) = orig;
1275                     }
1276                 }
1277
1278               if (!NOTE_P (insn))
1279                 record_dead_and_set_regs (insn);
1280
1281             retry:
1282               ;
1283             }
1284         }
1285     }
1286
1287   default_rtl_profile ();
1288   clear_log_links ();
1289   clear_bb_flags ();
1290   new_direct_jump_p |= purge_all_dead_edges ();
1291   delete_noop_moves ();
1292
1293   /* Clean up.  */
1294   free (uid_log_links);
1295   free (uid_insn_cost);
1296   VEC_free (reg_stat_type, heap, reg_stat);
1297
1298   {
1299     struct undo *undo, *next;
1300     for (undo = undobuf.frees; undo; undo = next)
1301       {
1302         next = undo->next;
1303         free (undo);
1304       }
1305     undobuf.frees = 0;
1306   }
1307
1308   total_attempts += combine_attempts;
1309   total_merges += combine_merges;
1310   total_extras += combine_extras;
1311   total_successes += combine_successes;
1312
1313   nonzero_sign_valid = 0;
1314   rtl_hooks = general_rtl_hooks;
1315
1316   /* Make recognizer allow volatile MEMs again.  */
1317   init_recog ();
1318
1319   return new_direct_jump_p;
1320 }
1321
1322 /* Wipe the last_xxx fields of reg_stat in preparation for another pass.  */
1323
1324 static void
1325 init_reg_last (void)
1326 {
1327   unsigned int i;
1328   reg_stat_type *p;
1329
1330   for (i = 0; VEC_iterate (reg_stat_type, reg_stat, i, p); ++i)
1331     memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1332 }
1333 \f
1334 /* Set up any promoted values for incoming argument registers.  */
1335
1336 static void
1337 setup_incoming_promotions (rtx first)
1338 {
1339   tree arg;
1340   bool strictly_local = false;
1341
1342   for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1343        arg = TREE_CHAIN (arg))
1344     {
1345       rtx x, reg = DECL_INCOMING_RTL (arg);
1346       int uns1, uns3;
1347       enum machine_mode mode1, mode2, mode3, mode4;
1348
1349       /* Only continue if the incoming argument is in a register.  */
1350       if (!REG_P (reg))
1351         continue;
1352
1353       /* Determine, if possible, whether all call sites of the current
1354          function lie within the current compilation unit.  (This does
1355          take into account the exporting of a function via taking its
1356          address, and so forth.)  */
1357       strictly_local = cgraph_local_info (current_function_decl)->local;
1358
1359       /* The mode and signedness of the argument before any promotions happen
1360          (equal to the mode of the pseudo holding it at that stage).  */
1361       mode1 = TYPE_MODE (TREE_TYPE (arg));
1362       uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1363
1364       /* The mode and signedness of the argument after any source language and
1365          TARGET_PROMOTE_PROTOTYPES-driven promotions.  */
1366       mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1367       uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1368
1369       /* The mode and signedness of the argument as it is actually passed,
1370          after any TARGET_PROMOTE_FUNCTION_ARGS-driven ABI promotions.  */
1371       mode3 = promote_function_mode (DECL_ARG_TYPE (arg), mode2, &uns3,
1372                                      TREE_TYPE (cfun->decl), 0);
1373
1374       /* The mode of the register in which the argument is being passed.  */
1375       mode4 = GET_MODE (reg);
1376
1377       /* Eliminate sign extensions in the callee when:
1378          (a) A mode promotion has occurred;  */
1379       if (mode1 == mode3)
1380         continue;
1381       /* (b) The mode of the register is the same as the mode of
1382              the argument as it is passed; */
1383       if (mode3 != mode4)
1384         continue;
1385       /* (c) There's no language level extension;  */
1386       if (mode1 == mode2)
1387         ;
1388       /* (c.1) All callers are from the current compilation unit.  If that's
1389          the case we don't have to rely on an ABI, we only have to know
1390          what we're generating right now, and we know that we will do the
1391          mode1 to mode2 promotion with the given sign.  */
1392       else if (!strictly_local)
1393         continue;
1394       /* (c.2) The combination of the two promotions is useful.  This is
1395          true when the signs match, or if the first promotion is unsigned.
1396          In the later case, (sign_extend (zero_extend x)) is the same as
1397          (zero_extend (zero_extend x)), so make sure to force UNS3 true.  */
1398       else if (uns1)
1399         uns3 = true;
1400       else if (uns3)
1401         continue;
1402
1403       /* Record that the value was promoted from mode1 to mode3,
1404          so that any sign extension at the head of the current
1405          function may be eliminated.  */
1406       x = gen_rtx_CLOBBER (mode1, const0_rtx);
1407       x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1408       record_value_for_reg (reg, first, x);
1409     }
1410 }
1411
1412 /* Called via note_stores.  If X is a pseudo that is narrower than
1413    HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1414
1415    If we are setting only a portion of X and we can't figure out what
1416    portion, assume all bits will be used since we don't know what will
1417    be happening.
1418
1419    Similarly, set how many bits of X are known to be copies of the sign bit
1420    at all locations in the function.  This is the smallest number implied
1421    by any set of X.  */
1422
1423 static void
1424 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1425 {
1426   rtx insn = (rtx) data;
1427   unsigned int num;
1428
1429   if (REG_P (x)
1430       && REGNO (x) >= FIRST_PSEUDO_REGISTER
1431       /* If this register is undefined at the start of the file, we can't
1432          say what its contents were.  */
1433       && ! REGNO_REG_SET_P
1434            (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x))
1435       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
1436     {
1437       reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
1438
1439       if (set == 0 || GET_CODE (set) == CLOBBER)
1440         {
1441           rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1442           rsp->sign_bit_copies = 1;
1443           return;
1444         }
1445
1446       /* If this register is being initialized using itself, and the
1447          register is uninitialized in this basic block, and there are
1448          no LOG_LINKS which set the register, then part of the
1449          register is uninitialized.  In that case we can't assume
1450          anything about the number of nonzero bits.
1451
1452          ??? We could do better if we checked this in
1453          reg_{nonzero_bits,num_sign_bit_copies}_for_combine.  Then we
1454          could avoid making assumptions about the insn which initially
1455          sets the register, while still using the information in other
1456          insns.  We would have to be careful to check every insn
1457          involved in the combination.  */
1458
1459       if (insn
1460           && reg_referenced_p (x, PATTERN (insn))
1461           && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1462                                REGNO (x)))
1463         {
1464           rtx link;
1465
1466           for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
1467             {
1468               if (dead_or_set_p (XEXP (link, 0), x))
1469                 break;
1470             }
1471           if (!link)
1472             {
1473               rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1474               rsp->sign_bit_copies = 1;
1475               return;
1476             }
1477         }
1478
1479       /* If this is a complex assignment, see if we can convert it into a
1480          simple assignment.  */
1481       set = expand_field_assignment (set);
1482
1483       /* If this is a simple assignment, or we have a paradoxical SUBREG,
1484          set what we know about X.  */
1485
1486       if (SET_DEST (set) == x
1487           || (GET_CODE (SET_DEST (set)) == SUBREG
1488               && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
1489                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
1490               && SUBREG_REG (SET_DEST (set)) == x))
1491         {
1492           rtx src = SET_SRC (set);
1493
1494 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1495           /* If X is narrower than a word and SRC is a non-negative
1496              constant that would appear negative in the mode of X,
1497              sign-extend it for use in reg_stat[].nonzero_bits because some
1498              machines (maybe most) will actually do the sign-extension
1499              and this is the conservative approach.
1500
1501              ??? For 2.5, try to tighten up the MD files in this regard
1502              instead of this kludge.  */
1503
1504           if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
1505               && CONST_INT_P (src)
1506               && INTVAL (src) > 0
1507               && 0 != (INTVAL (src)
1508                        & ((HOST_WIDE_INT) 1
1509                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
1510             src = GEN_INT (INTVAL (src)
1511                            | ((HOST_WIDE_INT) (-1)
1512                               << GET_MODE_BITSIZE (GET_MODE (x))));
1513 #endif
1514
1515           /* Don't call nonzero_bits if it cannot change anything.  */
1516           if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1517             rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1518           num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1519           if (rsp->sign_bit_copies == 0
1520               || rsp->sign_bit_copies > num)
1521             rsp->sign_bit_copies = num;
1522         }
1523       else
1524         {
1525           rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1526           rsp->sign_bit_copies = 1;
1527         }
1528     }
1529 }
1530 \f
1531 /* See if INSN can be combined into I3.  PRED and SUCC are optionally
1532    insns that were previously combined into I3 or that will be combined
1533    into the merger of INSN and I3.
1534
1535    Return 0 if the combination is not allowed for any reason.
1536
1537    If the combination is allowed, *PDEST will be set to the single
1538    destination of INSN and *PSRC to the single source, and this function
1539    will return 1.  */
1540
1541 static int
1542 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
1543                rtx *pdest, rtx *psrc)
1544 {
1545   int i;
1546   const_rtx set = 0;
1547   rtx src, dest;
1548   rtx p;
1549 #ifdef AUTO_INC_DEC
1550   rtx link;
1551 #endif
1552   int all_adjacent = (succ ? (next_active_insn (insn) == succ
1553                               && next_active_insn (succ) == i3)
1554                       : next_active_insn (insn) == i3);
1555
1556   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1557      or a PARALLEL consisting of such a SET and CLOBBERs.
1558
1559      If INSN has CLOBBER parallel parts, ignore them for our processing.
1560      By definition, these happen during the execution of the insn.  When it
1561      is merged with another insn, all bets are off.  If they are, in fact,
1562      needed and aren't also supplied in I3, they may be added by
1563      recog_for_combine.  Otherwise, it won't match.
1564
1565      We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1566      note.
1567
1568      Get the source and destination of INSN.  If more than one, can't
1569      combine.  */
1570
1571   if (GET_CODE (PATTERN (insn)) == SET)
1572     set = PATTERN (insn);
1573   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1574            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1575     {
1576       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1577         {
1578           rtx elt = XVECEXP (PATTERN (insn), 0, i);
1579
1580           switch (GET_CODE (elt))
1581             {
1582             /* This is important to combine floating point insns
1583                for the SH4 port.  */
1584             case USE:
1585               /* Combining an isolated USE doesn't make sense.
1586                  We depend here on combinable_i3pat to reject them.  */
1587               /* The code below this loop only verifies that the inputs of
1588                  the SET in INSN do not change.  We call reg_set_between_p
1589                  to verify that the REG in the USE does not change between
1590                  I3 and INSN.
1591                  If the USE in INSN was for a pseudo register, the matching
1592                  insn pattern will likely match any register; combining this
1593                  with any other USE would only be safe if we knew that the
1594                  used registers have identical values, or if there was
1595                  something to tell them apart, e.g. different modes.  For
1596                  now, we forgo such complicated tests and simply disallow
1597                  combining of USES of pseudo registers with any other USE.  */
1598               if (REG_P (XEXP (elt, 0))
1599                   && GET_CODE (PATTERN (i3)) == PARALLEL)
1600                 {
1601                   rtx i3pat = PATTERN (i3);
1602                   int i = XVECLEN (i3pat, 0) - 1;
1603                   unsigned int regno = REGNO (XEXP (elt, 0));
1604
1605                   do
1606                     {
1607                       rtx i3elt = XVECEXP (i3pat, 0, i);
1608
1609                       if (GET_CODE (i3elt) == USE
1610                           && REG_P (XEXP (i3elt, 0))
1611                           && (REGNO (XEXP (i3elt, 0)) == regno
1612                               ? reg_set_between_p (XEXP (elt, 0),
1613                                                    PREV_INSN (insn), i3)
1614                               : regno >= FIRST_PSEUDO_REGISTER))
1615                         return 0;
1616                     }
1617                   while (--i >= 0);
1618                 }
1619               break;
1620
1621               /* We can ignore CLOBBERs.  */
1622             case CLOBBER:
1623               break;
1624
1625             case SET:
1626               /* Ignore SETs whose result isn't used but not those that
1627                  have side-effects.  */
1628               if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1629                   && insn_nothrow_p (insn)
1630                   && !side_effects_p (elt))
1631                 break;
1632
1633               /* If we have already found a SET, this is a second one and
1634                  so we cannot combine with this insn.  */
1635               if (set)
1636                 return 0;
1637
1638               set = elt;
1639               break;
1640
1641             default:
1642               /* Anything else means we can't combine.  */
1643               return 0;
1644             }
1645         }
1646
1647       if (set == 0
1648           /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1649              so don't do anything with it.  */
1650           || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1651         return 0;
1652     }
1653   else
1654     return 0;
1655
1656   if (set == 0)
1657     return 0;
1658
1659   set = expand_field_assignment (set);
1660   src = SET_SRC (set), dest = SET_DEST (set);
1661
1662   /* Don't eliminate a store in the stack pointer.  */
1663   if (dest == stack_pointer_rtx
1664       /* Don't combine with an insn that sets a register to itself if it has
1665          a REG_EQUAL note.  This may be part of a LIBCALL sequence.  */
1666       || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1667       /* Can't merge an ASM_OPERANDS.  */
1668       || GET_CODE (src) == ASM_OPERANDS
1669       /* Can't merge a function call.  */
1670       || GET_CODE (src) == CALL
1671       /* Don't eliminate a function call argument.  */
1672       || (CALL_P (i3)
1673           && (find_reg_fusage (i3, USE, dest)
1674               || (REG_P (dest)
1675                   && REGNO (dest) < FIRST_PSEUDO_REGISTER
1676                   && global_regs[REGNO (dest)])))
1677       /* Don't substitute into an incremented register.  */
1678       || FIND_REG_INC_NOTE (i3, dest)
1679       || (succ && FIND_REG_INC_NOTE (succ, dest))
1680       /* Don't substitute into a non-local goto, this confuses CFG.  */
1681       || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1682       /* Make sure that DEST is not used after SUCC but before I3.  */
1683       || (succ && ! all_adjacent
1684           && reg_used_between_p (dest, succ, i3))
1685       /* Make sure that the value that is to be substituted for the register
1686          does not use any registers whose values alter in between.  However,
1687          If the insns are adjacent, a use can't cross a set even though we
1688          think it might (this can happen for a sequence of insns each setting
1689          the same destination; last_set of that register might point to
1690          a NOTE).  If INSN has a REG_EQUIV note, the register is always
1691          equivalent to the memory so the substitution is valid even if there
1692          are intervening stores.  Also, don't move a volatile asm or
1693          UNSPEC_VOLATILE across any other insns.  */
1694       || (! all_adjacent
1695           && (((!MEM_P (src)
1696                 || ! find_reg_note (insn, REG_EQUIV, src))
1697                && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1698               || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1699               || GET_CODE (src) == UNSPEC_VOLATILE))
1700       /* Don't combine across a CALL_INSN, because that would possibly
1701          change whether the life span of some REGs crosses calls or not,
1702          and it is a pain to update that information.
1703          Exception: if source is a constant, moving it later can't hurt.
1704          Accept that as a special case.  */
1705       || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1706     return 0;
1707
1708   /* DEST must either be a REG or CC0.  */
1709   if (REG_P (dest))
1710     {
1711       /* If register alignment is being enforced for multi-word items in all
1712          cases except for parameters, it is possible to have a register copy
1713          insn referencing a hard register that is not allowed to contain the
1714          mode being copied and which would not be valid as an operand of most
1715          insns.  Eliminate this problem by not combining with such an insn.
1716
1717          Also, on some machines we don't want to extend the life of a hard
1718          register.  */
1719
1720       if (REG_P (src)
1721           && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1722                && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1723               /* Don't extend the life of a hard register unless it is
1724                  user variable (if we have few registers) or it can't
1725                  fit into the desired register (meaning something special
1726                  is going on).
1727                  Also avoid substituting a return register into I3, because
1728                  reload can't handle a conflict with constraints of other
1729                  inputs.  */
1730               || (REGNO (src) < FIRST_PSEUDO_REGISTER
1731                   && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1732         return 0;
1733     }
1734   else if (GET_CODE (dest) != CC0)
1735     return 0;
1736
1737
1738   if (GET_CODE (PATTERN (i3)) == PARALLEL)
1739     for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1740       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1741         {
1742           /* Don't substitute for a register intended as a clobberable
1743              operand.  */
1744           rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1745           if (rtx_equal_p (reg, dest))
1746             return 0;
1747
1748           /* If the clobber represents an earlyclobber operand, we must not
1749              substitute an expression containing the clobbered register.
1750              As we do not analyze the constraint strings here, we have to
1751              make the conservative assumption.  However, if the register is
1752              a fixed hard reg, the clobber cannot represent any operand;
1753              we leave it up to the machine description to either accept or
1754              reject use-and-clobber patterns.  */
1755           if (!REG_P (reg)
1756               || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1757               || !fixed_regs[REGNO (reg)])
1758             if (reg_overlap_mentioned_p (reg, src))
1759               return 0;
1760         }
1761
1762   /* If INSN contains anything volatile, or is an `asm' (whether volatile
1763      or not), reject, unless nothing volatile comes between it and I3 */
1764
1765   if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1766     {
1767       /* Make sure succ doesn't contain a volatile reference.  */
1768       if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1769         return 0;
1770
1771       for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1772         if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1773           return 0;
1774     }
1775
1776   /* If INSN is an asm, and DEST is a hard register, reject, since it has
1777      to be an explicit register variable, and was chosen for a reason.  */
1778
1779   if (GET_CODE (src) == ASM_OPERANDS
1780       && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1781     return 0;
1782
1783   /* If there are any volatile insns between INSN and I3, reject, because
1784      they might affect machine state.  */
1785
1786   for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1787     if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1788       return 0;
1789
1790   /* If INSN contains an autoincrement or autodecrement, make sure that
1791      register is not used between there and I3, and not already used in
1792      I3 either.  Neither must it be used in PRED or SUCC, if they exist.
1793      Also insist that I3 not be a jump; if it were one
1794      and the incremented register were spilled, we would lose.  */
1795
1796 #ifdef AUTO_INC_DEC
1797   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1798     if (REG_NOTE_KIND (link) == REG_INC
1799         && (JUMP_P (i3)
1800             || reg_used_between_p (XEXP (link, 0), insn, i3)
1801             || (pred != NULL_RTX
1802                 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1803             || (succ != NULL_RTX
1804                 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
1805             || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1806       return 0;
1807 #endif
1808
1809 #ifdef HAVE_cc0
1810   /* Don't combine an insn that follows a CC0-setting insn.
1811      An insn that uses CC0 must not be separated from the one that sets it.
1812      We do, however, allow I2 to follow a CC0-setting insn if that insn
1813      is passed as I1; in that case it will be deleted also.
1814      We also allow combining in this case if all the insns are adjacent
1815      because that would leave the two CC0 insns adjacent as well.
1816      It would be more logical to test whether CC0 occurs inside I1 or I2,
1817      but that would be much slower, and this ought to be equivalent.  */
1818
1819   p = prev_nonnote_insn (insn);
1820   if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1821       && ! all_adjacent)
1822     return 0;
1823 #endif
1824
1825   /* If we get here, we have passed all the tests and the combination is
1826      to be allowed.  */
1827
1828   *pdest = dest;
1829   *psrc = src;
1830
1831   return 1;
1832 }
1833 \f
1834 /* LOC is the location within I3 that contains its pattern or the component
1835    of a PARALLEL of the pattern.  We validate that it is valid for combining.
1836
1837    One problem is if I3 modifies its output, as opposed to replacing it
1838    entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1839    so would produce an insn that is not equivalent to the original insns.
1840
1841    Consider:
1842
1843          (set (reg:DI 101) (reg:DI 100))
1844          (set (subreg:SI (reg:DI 101) 0) <foo>)
1845
1846    This is NOT equivalent to:
1847
1848          (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1849                     (set (reg:DI 101) (reg:DI 100))])
1850
1851    Not only does this modify 100 (in which case it might still be valid
1852    if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1853
1854    We can also run into a problem if I2 sets a register that I1
1855    uses and I1 gets directly substituted into I3 (not via I2).  In that
1856    case, we would be getting the wrong value of I2DEST into I3, so we
1857    must reject the combination.  This case occurs when I2 and I1 both
1858    feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1859    If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1860    of a SET must prevent combination from occurring.
1861
1862    Before doing the above check, we first try to expand a field assignment
1863    into a set of logical operations.
1864
1865    If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1866    we place a register that is both set and used within I3.  If more than one
1867    such register is detected, we fail.
1868
1869    Return 1 if the combination is valid, zero otherwise.  */
1870
1871 static int
1872 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
1873                   int i1_not_in_src, rtx *pi3dest_killed)
1874 {
1875   rtx x = *loc;
1876
1877   if (GET_CODE (x) == SET)
1878     {
1879       rtx set = x ;
1880       rtx dest = SET_DEST (set);
1881       rtx src = SET_SRC (set);
1882       rtx inner_dest = dest;
1883       rtx subdest;
1884
1885       while (GET_CODE (inner_dest) == STRICT_LOW_PART
1886              || GET_CODE (inner_dest) == SUBREG
1887              || GET_CODE (inner_dest) == ZERO_EXTRACT)
1888         inner_dest = XEXP (inner_dest, 0);
1889
1890       /* Check for the case where I3 modifies its output, as discussed
1891          above.  We don't want to prevent pseudos from being combined
1892          into the address of a MEM, so only prevent the combination if
1893          i1 or i2 set the same MEM.  */
1894       if ((inner_dest != dest &&
1895            (!MEM_P (inner_dest)
1896             || rtx_equal_p (i2dest, inner_dest)
1897             || (i1dest && rtx_equal_p (i1dest, inner_dest)))
1898            && (reg_overlap_mentioned_p (i2dest, inner_dest)
1899                || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1900
1901           /* This is the same test done in can_combine_p except we can't test
1902              all_adjacent; we don't have to, since this instruction will stay
1903              in place, thus we are not considering increasing the lifetime of
1904              INNER_DEST.
1905
1906              Also, if this insn sets a function argument, combining it with
1907              something that might need a spill could clobber a previous
1908              function argument; the all_adjacent test in can_combine_p also
1909              checks this; here, we do a more specific test for this case.  */
1910
1911           || (REG_P (inner_dest)
1912               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1913               && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1914                                         GET_MODE (inner_dest))))
1915           || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1916         return 0;
1917
1918       /* If DEST is used in I3, it is being killed in this insn, so
1919          record that for later.  We have to consider paradoxical
1920          subregs here, since they kill the whole register, but we
1921          ignore partial subregs, STRICT_LOW_PART, etc.
1922          Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1923          STACK_POINTER_REGNUM, since these are always considered to be
1924          live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
1925       subdest = dest;
1926       if (GET_CODE (subdest) == SUBREG
1927           && (GET_MODE_SIZE (GET_MODE (subdest))
1928               >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
1929         subdest = SUBREG_REG (subdest);
1930       if (pi3dest_killed
1931           && REG_P (subdest)
1932           && reg_referenced_p (subdest, PATTERN (i3))
1933           && REGNO (subdest) != FRAME_POINTER_REGNUM
1934 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1935           && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
1936 #endif
1937 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1938           && (REGNO (subdest) != ARG_POINTER_REGNUM
1939               || ! fixed_regs [REGNO (subdest)])
1940 #endif
1941           && REGNO (subdest) != STACK_POINTER_REGNUM)
1942         {
1943           if (*pi3dest_killed)
1944             return 0;
1945
1946           *pi3dest_killed = subdest;
1947         }
1948     }
1949
1950   else if (GET_CODE (x) == PARALLEL)
1951     {
1952       int i;
1953
1954       for (i = 0; i < XVECLEN (x, 0); i++)
1955         if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1956                                 i1_not_in_src, pi3dest_killed))
1957           return 0;
1958     }
1959
1960   return 1;
1961 }
1962 \f
1963 /* Return 1 if X is an arithmetic expression that contains a multiplication
1964    and division.  We don't count multiplications by powers of two here.  */
1965
1966 static int
1967 contains_muldiv (rtx x)
1968 {
1969   switch (GET_CODE (x))
1970     {
1971     case MOD:  case DIV:  case UMOD:  case UDIV:
1972       return 1;
1973
1974     case MULT:
1975       return ! (CONST_INT_P (XEXP (x, 1))
1976                 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1977     default:
1978       if (BINARY_P (x))
1979         return contains_muldiv (XEXP (x, 0))
1980             || contains_muldiv (XEXP (x, 1));
1981
1982       if (UNARY_P (x))
1983         return contains_muldiv (XEXP (x, 0));
1984
1985       return 0;
1986     }
1987 }
1988 \f
1989 /* Determine whether INSN can be used in a combination.  Return nonzero if
1990    not.  This is used in try_combine to detect early some cases where we
1991    can't perform combinations.  */
1992
1993 static int
1994 cant_combine_insn_p (rtx insn)
1995 {
1996   rtx set;
1997   rtx src, dest;
1998
1999   /* If this isn't really an insn, we can't do anything.
2000      This can occur when flow deletes an insn that it has merged into an
2001      auto-increment address.  */
2002   if (! INSN_P (insn))
2003     return 1;
2004
2005   /* Never combine loads and stores involving hard regs that are likely
2006      to be spilled.  The register allocator can usually handle such
2007      reg-reg moves by tying.  If we allow the combiner to make
2008      substitutions of likely-spilled regs, reload might die.
2009      As an exception, we allow combinations involving fixed regs; these are
2010      not available to the register allocator so there's no risk involved.  */
2011
2012   set = single_set (insn);
2013   if (! set)
2014     return 0;
2015   src = SET_SRC (set);
2016   dest = SET_DEST (set);
2017   if (GET_CODE (src) == SUBREG)
2018     src = SUBREG_REG (src);
2019   if (GET_CODE (dest) == SUBREG)
2020     dest = SUBREG_REG (dest);
2021   if (REG_P (src) && REG_P (dest)
2022       && ((REGNO (src) < FIRST_PSEUDO_REGISTER
2023            && ! fixed_regs[REGNO (src)]
2024            && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
2025           || (REGNO (dest) < FIRST_PSEUDO_REGISTER
2026               && ! fixed_regs[REGNO (dest)]
2027               && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
2028     return 1;
2029
2030   return 0;
2031 }
2032
2033 struct likely_spilled_retval_info
2034 {
2035   unsigned regno, nregs;
2036   unsigned mask;
2037 };
2038
2039 /* Called via note_stores by likely_spilled_retval_p.  Remove from info->mask
2040    hard registers that are known to be written to / clobbered in full.  */
2041 static void
2042 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2043 {
2044   struct likely_spilled_retval_info *const info =
2045     (struct likely_spilled_retval_info *) data;
2046   unsigned regno, nregs;
2047   unsigned new_mask;
2048
2049   if (!REG_P (XEXP (set, 0)))
2050     return;
2051   regno = REGNO (x);
2052   if (regno >= info->regno + info->nregs)
2053     return;
2054   nregs = hard_regno_nregs[regno][GET_MODE (x)];
2055   if (regno + nregs <= info->regno)
2056     return;
2057   new_mask = (2U << (nregs - 1)) - 1;
2058   if (regno < info->regno)
2059     new_mask >>= info->regno - regno;
2060   else
2061     new_mask <<= regno - info->regno;
2062   info->mask &= ~new_mask;
2063 }
2064
2065 /* Return nonzero iff part of the return value is live during INSN, and
2066    it is likely spilled.  This can happen when more than one insn is needed
2067    to copy the return value, e.g. when we consider to combine into the
2068    second copy insn for a complex value.  */
2069
2070 static int
2071 likely_spilled_retval_p (rtx insn)
2072 {
2073   rtx use = BB_END (this_basic_block);
2074   rtx reg, p;
2075   unsigned regno, nregs;
2076   /* We assume here that no machine mode needs more than
2077      32 hard registers when the value overlaps with a register
2078      for which TARGET_FUNCTION_VALUE_REGNO_P is true.  */
2079   unsigned mask;
2080   struct likely_spilled_retval_info info;
2081
2082   if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2083     return 0;
2084   reg = XEXP (PATTERN (use), 0);
2085   if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2086     return 0;
2087   regno = REGNO (reg);
2088   nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2089   if (nregs == 1)
2090     return 0;
2091   mask = (2U << (nregs - 1)) - 1;
2092
2093   /* Disregard parts of the return value that are set later.  */
2094   info.regno = regno;
2095   info.nregs = nregs;
2096   info.mask = mask;
2097   for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2098     if (INSN_P (p))
2099       note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2100   mask = info.mask;
2101
2102   /* Check if any of the (probably) live return value registers is
2103      likely spilled.  */
2104   nregs --;
2105   do
2106     {
2107       if ((mask & 1 << nregs)
2108           && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (regno + nregs)))
2109         return 1;
2110     } while (nregs--);
2111   return 0;
2112 }
2113
2114 /* Adjust INSN after we made a change to its destination.
2115
2116    Changing the destination can invalidate notes that say something about
2117    the results of the insn and a LOG_LINK pointing to the insn.  */
2118
2119 static void
2120 adjust_for_new_dest (rtx insn)
2121 {
2122   /* For notes, be conservative and simply remove them.  */
2123   remove_reg_equal_equiv_notes (insn);
2124
2125   /* The new insn will have a destination that was previously the destination
2126      of an insn just above it.  Call distribute_links to make a LOG_LINK from
2127      the next use of that destination.  */
2128   distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
2129
2130   df_insn_rescan (insn);
2131 }
2132
2133 /* Return TRUE if combine can reuse reg X in mode MODE.
2134    ADDED_SETS is nonzero if the original set is still required.  */
2135 static bool
2136 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
2137 {
2138   unsigned int regno;
2139
2140   if (!REG_P(x))
2141     return false;
2142
2143   regno = REGNO (x);
2144   /* Allow hard registers if the new mode is legal, and occupies no more
2145      registers than the old mode.  */
2146   if (regno < FIRST_PSEUDO_REGISTER)
2147     return (HARD_REGNO_MODE_OK (regno, mode)
2148             && (hard_regno_nregs[regno][GET_MODE (x)]
2149                 >= hard_regno_nregs[regno][mode]));
2150
2151   /* Or a pseudo that is only used once.  */
2152   return (REG_N_SETS (regno) == 1 && !added_sets
2153           && !REG_USERVAR_P (x));
2154 }
2155
2156
2157 /* Check whether X, the destination of a set, refers to part of
2158    the register specified by REG.  */
2159
2160 static bool
2161 reg_subword_p (rtx x, rtx reg)
2162 {
2163   /* Check that reg is an integer mode register.  */
2164   if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2165     return false;
2166
2167   if (GET_CODE (x) == STRICT_LOW_PART
2168       || GET_CODE (x) == ZERO_EXTRACT)
2169     x = XEXP (x, 0);
2170
2171   return GET_CODE (x) == SUBREG
2172          && SUBREG_REG (x) == reg
2173          && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2174 }
2175
2176 #ifdef AUTO_INC_DEC
2177 /* Replace auto-increment addressing modes with explicit operations to
2178    access the same addresses without modifying the corresponding
2179    registers.  If AFTER holds, SRC is meant to be reused after the
2180    side effect, otherwise it is to be reused before that.  */
2181
2182 static rtx
2183 cleanup_auto_inc_dec (rtx src, bool after, enum machine_mode mem_mode)
2184 {
2185   rtx x = src;
2186   const RTX_CODE code = GET_CODE (x);
2187   int i;
2188   const char *fmt;
2189
2190   switch (code)
2191     {
2192     case REG:
2193     case CONST_INT:
2194     case CONST_DOUBLE:
2195     case CONST_FIXED:
2196     case CONST_VECTOR:
2197     case SYMBOL_REF:
2198     case CODE_LABEL:
2199     case PC:
2200     case CC0:
2201     case SCRATCH:
2202       /* SCRATCH must be shared because they represent distinct values.  */
2203       return x;
2204     case CLOBBER:
2205       if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
2206         return x;
2207       break;
2208
2209     case CONST:
2210       if (shared_const_p (x))
2211         return x;
2212       break;
2213
2214     case MEM:
2215       mem_mode = GET_MODE (x);
2216       break;
2217
2218     case PRE_INC:
2219     case PRE_DEC:
2220     case POST_INC:
2221     case POST_DEC:
2222       gcc_assert (mem_mode != VOIDmode && mem_mode != BLKmode);
2223       if (after == (code == PRE_INC || code == PRE_DEC))
2224         x = cleanup_auto_inc_dec (XEXP (x, 0), after, mem_mode);
2225       else
2226         x = gen_rtx_PLUS (GET_MODE (x),
2227                           cleanup_auto_inc_dec (XEXP (x, 0), after, mem_mode),
2228                           GEN_INT ((code == PRE_INC || code == POST_INC)
2229                                    ? GET_MODE_SIZE (mem_mode)
2230                                    : -GET_MODE_SIZE (mem_mode)));
2231       return x;
2232
2233     case PRE_MODIFY:
2234     case POST_MODIFY:
2235       if (after == (code == PRE_MODIFY))
2236         x = XEXP (x, 0);
2237       else
2238         x = XEXP (x, 1);
2239       return cleanup_auto_inc_dec (x, after, mem_mode);
2240
2241     default:
2242       break;
2243     }
2244
2245   /* Copy the various flags, fields, and other information.  We assume
2246      that all fields need copying, and then clear the fields that should
2247      not be copied.  That is the sensible default behavior, and forces
2248      us to explicitly document why we are *not* copying a flag.  */
2249   x = shallow_copy_rtx (x);
2250
2251   /* We do not copy the USED flag, which is used as a mark bit during
2252      walks over the RTL.  */
2253   RTX_FLAG (x, used) = 0;
2254
2255   /* We do not copy FRAME_RELATED for INSNs.  */
2256   if (INSN_P (x))
2257     RTX_FLAG (x, frame_related) = 0;
2258
2259   fmt = GET_RTX_FORMAT (code);
2260   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2261     if (fmt[i] == 'e')
2262       XEXP (x, i) = cleanup_auto_inc_dec (XEXP (x, i), after, mem_mode);
2263     else if (fmt[i] == 'E' || fmt[i] == 'V')
2264       {
2265         int j;
2266         XVEC (x, i) = rtvec_alloc (XVECLEN (x, i));
2267         for (j = 0; j < XVECLEN (x, i); j++)
2268           XVECEXP (x, i, j)
2269             = cleanup_auto_inc_dec (XVECEXP (src, i, j), after, mem_mode);
2270       }
2271
2272   return x;
2273 }
2274
2275 /* Auxiliary data structure for propagate_for_debug_stmt.  */
2276
2277 struct rtx_subst_pair
2278 {
2279   rtx to;
2280   bool adjusted;
2281   bool after;
2282 };
2283
2284 /* DATA points to an rtx_subst_pair.  Return the value that should be
2285    substituted.  */
2286
2287 static rtx
2288 propagate_for_debug_subst (rtx from, const_rtx old_rtx, void *data)
2289 {
2290   struct rtx_subst_pair *pair = (struct rtx_subst_pair *)data;
2291
2292   if (!rtx_equal_p (from, old_rtx))
2293     return NULL_RTX;
2294   if (!pair->adjusted)
2295     {
2296       pair->adjusted = true;
2297       pair->to = cleanup_auto_inc_dec (pair->to, pair->after, VOIDmode);
2298       return pair->to;
2299     }
2300   return copy_rtx (pair->to);
2301 }
2302 #endif
2303
2304 /* Replace occurrences of DEST with SRC in DEBUG_INSNs between INSN
2305    and LAST.  If MOVE holds, debug insns must also be moved past
2306    LAST.  */
2307
2308 static void
2309 propagate_for_debug (rtx insn, rtx last, rtx dest, rtx src, bool move)
2310 {
2311   rtx next, move_pos = move ? last : NULL_RTX, loc;
2312   bool first_p;
2313
2314 #ifdef AUTO_INC_DEC
2315   struct rtx_subst_pair p;
2316   p.to = src;
2317   p.adjusted = false;
2318   p.after = move;
2319 #endif
2320
2321   first_p = true;
2322   next = NEXT_INSN (insn);
2323   while (next != last)
2324     {
2325       insn = next;
2326       next = NEXT_INSN (insn);
2327       if (DEBUG_INSN_P (insn))
2328         {
2329           if (first_p)
2330             {
2331               src = make_compound_operation (src, SET);
2332               first_p = false;
2333             }
2334 #ifdef AUTO_INC_DEC
2335           loc = simplify_replace_fn_rtx (INSN_VAR_LOCATION_LOC (insn),
2336                                          dest, propagate_for_debug_subst, &p);
2337 #else
2338           loc = simplify_replace_rtx (INSN_VAR_LOCATION_LOC (insn), dest, src);
2339 #endif
2340           if (loc == INSN_VAR_LOCATION_LOC (insn))
2341             continue;
2342           INSN_VAR_LOCATION_LOC (insn) = loc;
2343           if (move_pos)
2344             {
2345               remove_insn (insn);
2346               PREV_INSN (insn) = NEXT_INSN (insn) = NULL_RTX;
2347               move_pos = emit_debug_insn_after (insn, move_pos);
2348             }
2349           else
2350             df_insn_rescan (insn);
2351         }
2352     }
2353 }
2354
2355 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2356    Note that the INSN should be deleted *after* removing dead edges, so
2357    that the kept edge is the fallthrough edge for a (set (pc) (pc))
2358    but not for a (set (pc) (label_ref FOO)).  */
2359
2360 static void
2361 update_cfg_for_uncondjump (rtx insn)
2362 {
2363   basic_block bb = BLOCK_FOR_INSN (insn);
2364   bool at_end = (BB_END (bb) == insn);
2365
2366   if (at_end)
2367     purge_dead_edges (bb);
2368
2369   delete_insn (insn);
2370   if (at_end && EDGE_COUNT (bb->succs) == 1)
2371     single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2372 }
2373
2374
2375 /* Try to combine the insns I1 and I2 into I3.
2376    Here I1 and I2 appear earlier than I3.
2377    I1 can be zero; then we combine just I2 into I3.
2378
2379    If we are combining three insns and the resulting insn is not recognized,
2380    try splitting it into two insns.  If that happens, I2 and I3 are retained
2381    and I1 is pseudo-deleted by turning it into a NOTE.  Otherwise, I1 and I2
2382    are pseudo-deleted.
2383
2384    Return 0 if the combination does not work.  Then nothing is changed.
2385    If we did the combination, return the insn at which combine should
2386    resume scanning.
2387
2388    Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2389    new direct jump instruction.  */
2390
2391 static rtx
2392 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
2393 {
2394   /* New patterns for I3 and I2, respectively.  */
2395   rtx newpat, newi2pat = 0;
2396   rtvec newpat_vec_with_clobbers = 0;
2397   int substed_i2 = 0, substed_i1 = 0;
2398   /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead.  */
2399   int added_sets_1, added_sets_2;
2400   /* Total number of SETs to put into I3.  */
2401   int total_sets;
2402   /* Nonzero if I2's body now appears in I3.  */
2403   int i2_is_used;
2404   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
2405   int insn_code_number, i2_code_number = 0, other_code_number = 0;
2406   /* Contains I3 if the destination of I3 is used in its source, which means
2407      that the old life of I3 is being killed.  If that usage is placed into
2408      I2 and not in I3, a REG_DEAD note must be made.  */
2409   rtx i3dest_killed = 0;
2410   /* SET_DEST and SET_SRC of I2 and I1.  */
2411   rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0;
2412   /* Set if I2DEST was reused as a scratch register.  */
2413   bool i2scratch = false;
2414   /* PATTERN (I1) and PATTERN (I2), or a copy of it in certain cases.  */
2415   rtx i1pat = 0, i2pat = 0;
2416   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
2417   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2418   int i2dest_killed = 0, i1dest_killed = 0;
2419   int i1_feeds_i3 = 0;
2420   /* Notes that must be added to REG_NOTES in I3 and I2.  */
2421   rtx new_i3_notes, new_i2_notes;
2422   /* Notes that we substituted I3 into I2 instead of the normal case.  */
2423   int i3_subst_into_i2 = 0;
2424   /* Notes that I1, I2 or I3 is a MULT operation.  */
2425   int have_mult = 0;
2426   int swap_i2i3 = 0;
2427   int changed_i3_dest = 0;
2428
2429   int maxreg;
2430   rtx temp;
2431   rtx link;
2432   rtx other_pat = 0;
2433   rtx new_other_notes;
2434   int i;
2435
2436   /* Exit early if one of the insns involved can't be used for
2437      combinations.  */
2438   if (cant_combine_insn_p (i3)
2439       || cant_combine_insn_p (i2)
2440       || (i1 && cant_combine_insn_p (i1))
2441       || likely_spilled_retval_p (i3))
2442     return 0;
2443
2444   combine_attempts++;
2445   undobuf.other_insn = 0;
2446
2447   /* Reset the hard register usage information.  */
2448   CLEAR_HARD_REG_SET (newpat_used_regs);
2449
2450   if (dump_file && (dump_flags & TDF_DETAILS))
2451     {
2452       if (i1)
2453         fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2454                  INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2455       else
2456         fprintf (dump_file, "\nTrying %d -> %d:\n",
2457                  INSN_UID (i2), INSN_UID (i3));
2458     }
2459
2460   /* If I1 and I2 both feed I3, they can be in any order.  To simplify the
2461      code below, set I1 to be the earlier of the two insns.  */
2462   if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2463     temp = i1, i1 = i2, i2 = temp;
2464
2465   added_links_insn = 0;
2466
2467   /* First check for one important special-case that the code below will
2468      not handle.  Namely, the case where I1 is zero, I2 is a PARALLEL
2469      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
2470      we may be able to replace that destination with the destination of I3.
2471      This occurs in the common code where we compute both a quotient and
2472      remainder into a structure, in which case we want to do the computation
2473      directly into the structure to avoid register-register copies.
2474
2475      Note that this case handles both multiple sets in I2 and also
2476      cases where I2 has a number of CLOBBER or PARALLELs.
2477
2478      We make very conservative checks below and only try to handle the
2479      most common cases of this.  For example, we only handle the case
2480      where I2 and I3 are adjacent to avoid making difficult register
2481      usage tests.  */
2482
2483   if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2484       && REG_P (SET_SRC (PATTERN (i3)))
2485       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2486       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2487       && GET_CODE (PATTERN (i2)) == PARALLEL
2488       && ! side_effects_p (SET_DEST (PATTERN (i3)))
2489       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2490          below would need to check what is inside (and reg_overlap_mentioned_p
2491          doesn't support those codes anyway).  Don't allow those destinations;
2492          the resulting insn isn't likely to be recognized anyway.  */
2493       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2494       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2495       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2496                                     SET_DEST (PATTERN (i3)))
2497       && next_active_insn (i2) == i3)
2498     {
2499       rtx p2 = PATTERN (i2);
2500
2501       /* Make sure that the destination of I3,
2502          which we are going to substitute into one output of I2,
2503          is not used within another output of I2.  We must avoid making this:
2504          (parallel [(set (mem (reg 69)) ...)
2505                     (set (reg 69) ...)])
2506          which is not well-defined as to order of actions.
2507          (Besides, reload can't handle output reloads for this.)
2508
2509          The problem can also happen if the dest of I3 is a memory ref,
2510          if another dest in I2 is an indirect memory ref.  */
2511       for (i = 0; i < XVECLEN (p2, 0); i++)
2512         if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2513              || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2514             && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2515                                         SET_DEST (XVECEXP (p2, 0, i))))
2516           break;
2517
2518       if (i == XVECLEN (p2, 0))
2519         for (i = 0; i < XVECLEN (p2, 0); i++)
2520           if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2521                || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2522               && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2523             {
2524               combine_merges++;
2525
2526               subst_insn = i3;
2527               subst_low_luid = DF_INSN_LUID (i2);
2528
2529               added_sets_2 = added_sets_1 = 0;
2530               i2src = SET_DEST (PATTERN (i3));
2531               i2dest = SET_SRC (PATTERN (i3));
2532               i2dest_killed = dead_or_set_p (i2, i2dest);
2533
2534               /* Replace the dest in I2 with our dest and make the resulting
2535                  insn the new pattern for I3.  Then skip to where we
2536                  validate the pattern.  Everything was set up above.  */
2537               SUBST (SET_DEST (XVECEXP (p2, 0, i)),
2538                      SET_DEST (PATTERN (i3)));
2539
2540               newpat = p2;
2541               i3_subst_into_i2 = 1;
2542               goto validate_replacement;
2543             }
2544     }
2545
2546   /* If I2 is setting a pseudo to a constant and I3 is setting some
2547      sub-part of it to another constant, merge them by making a new
2548      constant.  */
2549   if (i1 == 0
2550       && (temp = single_set (i2)) != 0
2551       && (CONST_INT_P (SET_SRC (temp))
2552           || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
2553       && GET_CODE (PATTERN (i3)) == SET
2554       && (CONST_INT_P (SET_SRC (PATTERN (i3)))
2555           || GET_CODE (SET_SRC (PATTERN (i3))) == CONST_DOUBLE)
2556       && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp)))
2557     {
2558       rtx dest = SET_DEST (PATTERN (i3));
2559       int offset = -1;
2560       int width = 0;
2561
2562       if (GET_CODE (dest) == ZERO_EXTRACT)
2563         {
2564           if (CONST_INT_P (XEXP (dest, 1))
2565               && CONST_INT_P (XEXP (dest, 2)))
2566             {
2567               width = INTVAL (XEXP (dest, 1));
2568               offset = INTVAL (XEXP (dest, 2));
2569               dest = XEXP (dest, 0);
2570               if (BITS_BIG_ENDIAN)
2571                 offset = GET_MODE_BITSIZE (GET_MODE (dest)) - width - offset;
2572             }
2573         }
2574       else
2575         {
2576           if (GET_CODE (dest) == STRICT_LOW_PART)
2577             dest = XEXP (dest, 0);
2578           width = GET_MODE_BITSIZE (GET_MODE (dest));
2579           offset = 0;
2580         }
2581
2582       if (offset >= 0)
2583         {
2584           /* If this is the low part, we're done.  */
2585           if (subreg_lowpart_p (dest))
2586             ;
2587           /* Handle the case where inner is twice the size of outer.  */
2588           else if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2589                    == 2 * GET_MODE_BITSIZE (GET_MODE (dest)))
2590             offset += GET_MODE_BITSIZE (GET_MODE (dest));
2591           /* Otherwise give up for now.  */
2592           else
2593             offset = -1;
2594         }
2595
2596       if (offset >= 0
2597           && (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2598               <= HOST_BITS_PER_DOUBLE_INT))
2599         {
2600           double_int m, o, i;
2601           rtx inner = SET_SRC (PATTERN (i3));
2602           rtx outer = SET_SRC (temp);
2603
2604           o = rtx_to_double_int (outer);
2605           i = rtx_to_double_int (inner);
2606
2607           m = double_int_mask (width);
2608           i = double_int_and (i, m);
2609           m = double_int_lshift (m, offset, HOST_BITS_PER_DOUBLE_INT, false);
2610           i = double_int_lshift (i, offset, HOST_BITS_PER_DOUBLE_INT, false);
2611           o = double_int_ior (double_int_and (o, double_int_not (m)), i);
2612
2613           combine_merges++;
2614           subst_insn = i3;
2615           subst_low_luid = DF_INSN_LUID (i2);
2616           added_sets_2 = added_sets_1 = 0;
2617           i2dest = SET_DEST (temp);
2618           i2dest_killed = dead_or_set_p (i2, i2dest);
2619
2620           /* Replace the source in I2 with the new constant and make the
2621              resulting insn the new pattern for I3.  Then skip to where we
2622              validate the pattern.  Everything was set up above.  */
2623           SUBST (SET_SRC (temp),
2624                  immed_double_int_const (o, GET_MODE (SET_DEST (temp))));
2625
2626           newpat = PATTERN (i2);
2627
2628           /* The dest of I3 has been replaced with the dest of I2.  */
2629           changed_i3_dest = 1;
2630           goto validate_replacement;
2631         }
2632     }
2633
2634 #ifndef HAVE_cc0
2635   /* If we have no I1 and I2 looks like:
2636         (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2637                    (set Y OP)])
2638      make up a dummy I1 that is
2639         (set Y OP)
2640      and change I2 to be
2641         (set (reg:CC X) (compare:CC Y (const_int 0)))
2642
2643      (We can ignore any trailing CLOBBERs.)
2644
2645      This undoes a previous combination and allows us to match a branch-and-
2646      decrement insn.  */
2647
2648   if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2649       && XVECLEN (PATTERN (i2), 0) >= 2
2650       && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2651       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2652           == MODE_CC)
2653       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2654       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2655       && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2656       && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2657       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2658                       SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2659     {
2660       for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2661         if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2662           break;
2663
2664       if (i == 1)
2665         {
2666           /* We make I1 with the same INSN_UID as I2.  This gives it
2667              the same DF_INSN_LUID for value tracking.  Our fake I1 will
2668              never appear in the insn stream so giving it the same INSN_UID
2669              as I2 will not cause a problem.  */
2670
2671           i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
2672                              BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
2673                              XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX);
2674
2675           SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2676           SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2677                  SET_DEST (PATTERN (i1)));
2678         }
2679     }
2680 #endif
2681
2682   /* Verify that I2 and I1 are valid for combining.  */
2683   if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
2684       || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
2685     {
2686       undo_all ();
2687       return 0;
2688     }
2689
2690   /* Record whether I2DEST is used in I2SRC and similarly for the other
2691      cases.  Knowing this will help in register status updating below.  */
2692   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2693   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2694   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2695   i2dest_killed = dead_or_set_p (i2, i2dest);
2696   i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2697
2698   /* See if I1 directly feeds into I3.  It does if I1DEST is not used
2699      in I2SRC.  */
2700   i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
2701
2702   /* Ensure that I3's pattern can be the destination of combines.  */
2703   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
2704                           i1 && i2dest_in_i1src && i1_feeds_i3,
2705                           &i3dest_killed))
2706     {
2707       undo_all ();
2708       return 0;
2709     }
2710
2711   /* See if any of the insns is a MULT operation.  Unless one is, we will
2712      reject a combination that is, since it must be slower.  Be conservative
2713      here.  */
2714   if (GET_CODE (i2src) == MULT
2715       || (i1 != 0 && GET_CODE (i1src) == MULT)
2716       || (GET_CODE (PATTERN (i3)) == SET
2717           && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2718     have_mult = 1;
2719
2720   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2721      We used to do this EXCEPT in one case: I3 has a post-inc in an
2722      output operand.  However, that exception can give rise to insns like
2723         mov r3,(r3)+
2724      which is a famous insn on the PDP-11 where the value of r3 used as the
2725      source was model-dependent.  Avoid this sort of thing.  */
2726
2727 #if 0
2728   if (!(GET_CODE (PATTERN (i3)) == SET
2729         && REG_P (SET_SRC (PATTERN (i3)))
2730         && MEM_P (SET_DEST (PATTERN (i3)))
2731         && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2732             || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2733     /* It's not the exception.  */
2734 #endif
2735 #ifdef AUTO_INC_DEC
2736     for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2737       if (REG_NOTE_KIND (link) == REG_INC
2738           && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2739               || (i1 != 0
2740                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2741         {
2742           undo_all ();
2743           return 0;
2744         }
2745 #endif
2746
2747   /* See if the SETs in I1 or I2 need to be kept around in the merged
2748      instruction: whenever the value set there is still needed past I3.
2749      For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
2750
2751      For the SET in I1, we have two cases:  If I1 and I2 independently
2752      feed into I3, the set in I1 needs to be kept around if I1DEST dies
2753      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
2754      in I1 needs to be kept around unless I1DEST dies or is set in either
2755      I2 or I3.  We can distinguish these cases by seeing if I2SRC mentions
2756      I1DEST.  If so, we know I1 feeds into I2.  */
2757
2758   added_sets_2 = ! dead_or_set_p (i3, i2dest);
2759
2760   added_sets_1
2761     = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
2762                : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
2763
2764   /* If the set in I2 needs to be kept around, we must make a copy of
2765      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2766      PATTERN (I2), we are only substituting for the original I1DEST, not into
2767      an already-substituted copy.  This also prevents making self-referential
2768      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2769      I2DEST.  */
2770
2771   if (added_sets_2)
2772     {
2773       if (GET_CODE (PATTERN (i2)) == PARALLEL)
2774         i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
2775       else
2776         i2pat = copy_rtx (PATTERN (i2));
2777     }
2778
2779   if (added_sets_1)
2780     {
2781       if (GET_CODE (PATTERN (i1)) == PARALLEL)
2782         i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
2783       else
2784         i1pat = copy_rtx (PATTERN (i1));
2785     }
2786
2787   combine_merges++;
2788
2789   /* Substitute in the latest insn for the regs set by the earlier ones.  */
2790
2791   maxreg = max_reg_num ();
2792
2793   subst_insn = i3;
2794
2795 #ifndef HAVE_cc0
2796   /* Many machines that don't use CC0 have insns that can both perform an
2797      arithmetic operation and set the condition code.  These operations will
2798      be represented as a PARALLEL with the first element of the vector
2799      being a COMPARE of an arithmetic operation with the constant zero.
2800      The second element of the vector will set some pseudo to the result
2801      of the same arithmetic operation.  If we simplify the COMPARE, we won't
2802      match such a pattern and so will generate an extra insn.   Here we test
2803      for this case, where both the comparison and the operation result are
2804      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2805      I2SRC.  Later we will make the PARALLEL that contains I2.  */
2806
2807   if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2808       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2809       && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
2810       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2811     {
2812 #ifdef SELECT_CC_MODE
2813       rtx *cc_use;
2814       enum machine_mode compare_mode;
2815 #endif
2816
2817       newpat = PATTERN (i3);
2818       SUBST (XEXP (SET_SRC (newpat), 0), i2src);
2819
2820       i2_is_used = 1;
2821
2822 #ifdef SELECT_CC_MODE
2823       /* See if a COMPARE with the operand we substituted in should be done
2824          with the mode that is currently being used.  If not, do the same
2825          processing we do in `subst' for a SET; namely, if the destination
2826          is used only once, try to replace it with a register of the proper
2827          mode and also replace the COMPARE.  */
2828       if (undobuf.other_insn == 0
2829           && (cc_use = find_single_use (SET_DEST (newpat), i3,
2830                                         &undobuf.other_insn))
2831           && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
2832                                               i2src, const0_rtx))
2833               != GET_MODE (SET_DEST (newpat))))
2834         {
2835           if (can_change_dest_mode(SET_DEST (newpat), added_sets_2,
2836                                    compare_mode))
2837             {
2838               unsigned int regno = REGNO (SET_DEST (newpat));
2839               rtx new_dest;
2840
2841               if (regno < FIRST_PSEUDO_REGISTER)
2842                 new_dest = gen_rtx_REG (compare_mode, regno);
2843               else
2844                 {
2845                   SUBST_MODE (regno_reg_rtx[regno], compare_mode);
2846                   new_dest = regno_reg_rtx[regno];
2847                 }
2848
2849               SUBST (SET_DEST (newpat), new_dest);
2850               SUBST (XEXP (*cc_use, 0), new_dest);
2851               SUBST (SET_SRC (newpat),
2852                      gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
2853             }
2854           else
2855             undobuf.other_insn = 0;
2856         }
2857 #endif
2858     }
2859   else
2860 #endif
2861     {
2862       /* It is possible that the source of I2 or I1 may be performing
2863          an unneeded operation, such as a ZERO_EXTEND of something
2864          that is known to have the high part zero.  Handle that case
2865          by letting subst look at the innermost one of them.
2866
2867          Another way to do this would be to have a function that tries
2868          to simplify a single insn instead of merging two or more
2869          insns.  We don't do this because of the potential of infinite
2870          loops and because of the potential extra memory required.
2871          However, doing it the way we are is a bit of a kludge and
2872          doesn't catch all cases.
2873
2874          But only do this if -fexpensive-optimizations since it slows
2875          things down and doesn't usually win.
2876
2877          This is not done in the COMPARE case above because the
2878          unmodified I2PAT is used in the PARALLEL and so a pattern
2879          with a modified I2SRC would not match.  */
2880
2881       if (flag_expensive_optimizations)
2882         {
2883           /* Pass pc_rtx so no substitutions are done, just
2884              simplifications.  */
2885           if (i1)
2886             {
2887               subst_low_luid = DF_INSN_LUID (i1);
2888               i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
2889             }
2890           else
2891             {
2892               subst_low_luid = DF_INSN_LUID (i2);
2893               i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
2894             }
2895         }
2896
2897       n_occurrences = 0;                /* `subst' counts here */
2898
2899       /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
2900          need to make a unique copy of I2SRC each time we substitute it
2901          to avoid self-referential rtl.  */
2902
2903       subst_low_luid = DF_INSN_LUID (i2);
2904       newpat = subst (PATTERN (i3), i2dest, i2src, 0,
2905                       ! i1_feeds_i3 && i1dest_in_i1src);
2906       substed_i2 = 1;
2907
2908       /* Record whether i2's body now appears within i3's body.  */
2909       i2_is_used = n_occurrences;
2910     }
2911
2912   /* If we already got a failure, don't try to do more.  Otherwise,
2913      try to substitute in I1 if we have it.  */
2914
2915   if (i1 && GET_CODE (newpat) != CLOBBER)
2916     {
2917       /* Check that an autoincrement side-effect on I1 has not been lost.
2918          This happens if I1DEST is mentioned in I2 and dies there, and
2919          has disappeared from the new pattern.  */
2920       if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2921            && !i1_feeds_i3
2922            && dead_or_set_p (i2, i1dest)
2923            && !reg_overlap_mentioned_p (i1dest, newpat))
2924           /* Before we can do this substitution, we must redo the test done
2925              above (see detailed comments there) that ensures  that I1DEST
2926              isn't mentioned in any SETs in NEWPAT that are field assignments.  */
2927           || !combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX, 0, 0))
2928         {
2929           undo_all ();
2930           return 0;
2931         }
2932
2933       n_occurrences = 0;
2934       subst_low_luid = DF_INSN_LUID (i1);
2935       newpat = subst (newpat, i1dest, i1src, 0, 0);
2936       substed_i1 = 1;
2937     }
2938
2939   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
2940      to count all the ways that I2SRC and I1SRC can be used.  */
2941   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
2942        && i2_is_used + added_sets_2 > 1)
2943       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2944           && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
2945               > 1))
2946       /* Fail if we tried to make a new register.  */
2947       || max_reg_num () != maxreg
2948       /* Fail if we couldn't do something and have a CLOBBER.  */
2949       || GET_CODE (newpat) == CLOBBER
2950       /* Fail if this new pattern is a MULT and we didn't have one before
2951          at the outer level.  */
2952       || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
2953           && ! have_mult))
2954     {
2955       undo_all ();
2956       return 0;
2957     }
2958
2959   /* If the actions of the earlier insns must be kept
2960      in addition to substituting them into the latest one,
2961      we must make a new PARALLEL for the latest insn
2962      to hold additional the SETs.  */
2963
2964   if (added_sets_1 || added_sets_2)
2965     {
2966       combine_extras++;
2967
2968       if (GET_CODE (newpat) == PARALLEL)
2969         {
2970           rtvec old = XVEC (newpat, 0);
2971           total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
2972           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2973           memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
2974                   sizeof (old->elem[0]) * old->num_elem);
2975         }
2976       else
2977         {
2978           rtx old = newpat;
2979           total_sets = 1 + added_sets_1 + added_sets_2;
2980           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2981           XVECEXP (newpat, 0, 0) = old;
2982         }
2983
2984       if (added_sets_1)
2985         XVECEXP (newpat, 0, --total_sets) = i1pat;
2986
2987       if (added_sets_2)
2988         {
2989           /* If there is no I1, use I2's body as is.  We used to also not do
2990              the subst call below if I2 was substituted into I3,
2991              but that could lose a simplification.  */
2992           if (i1 == 0)
2993             XVECEXP (newpat, 0, --total_sets) = i2pat;
2994           else
2995             /* See comment where i2pat is assigned.  */
2996             XVECEXP (newpat, 0, --total_sets)
2997               = subst (i2pat, i1dest, i1src, 0, 0);
2998         }
2999     }
3000
3001  validate_replacement:
3002
3003   /* Note which hard regs this insn has as inputs.  */
3004   mark_used_regs_combine (newpat);
3005
3006   /* If recog_for_combine fails, it strips existing clobbers.  If we'll
3007      consider splitting this pattern, we might need these clobbers.  */
3008   if (i1 && GET_CODE (newpat) == PARALLEL
3009       && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3010     {
3011       int len = XVECLEN (newpat, 0);
3012
3013       newpat_vec_with_clobbers = rtvec_alloc (len);
3014       for (i = 0; i < len; i++)
3015         RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3016     }
3017
3018   /* Is the result of combination a valid instruction?  */
3019   insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3020
3021   /* If the result isn't valid, see if it is a PARALLEL of two SETs where
3022      the second SET's destination is a register that is unused and isn't
3023      marked as an instruction that might trap in an EH region.  In that case,
3024      we just need the first SET.   This can occur when simplifying a divmod
3025      insn.  We *must* test for this case here because the code below that
3026      splits two independent SETs doesn't handle this case correctly when it
3027      updates the register status.
3028
3029      It's pointless doing this if we originally had two sets, one from
3030      i3, and one from i2.  Combining then splitting the parallel results
3031      in the original i2 again plus an invalid insn (which we delete).
3032      The net effect is only to move instructions around, which makes
3033      debug info less accurate.
3034
3035      Also check the case where the first SET's destination is unused.
3036      That would not cause incorrect code, but does cause an unneeded
3037      insn to remain.  */
3038
3039   if (insn_code_number < 0
3040       && !(added_sets_2 && i1 == 0)
3041       && GET_CODE (newpat) == PARALLEL
3042       && XVECLEN (newpat, 0) == 2
3043       && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3044       && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3045       && asm_noperands (newpat) < 0)
3046     {
3047       rtx set0 = XVECEXP (newpat, 0, 0);
3048       rtx set1 = XVECEXP (newpat, 0, 1);
3049
3050       if (((REG_P (SET_DEST (set1))
3051             && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3052            || (GET_CODE (SET_DEST (set1)) == SUBREG
3053                && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3054           && insn_nothrow_p (i3)
3055           && !side_effects_p (SET_SRC (set1)))
3056         {
3057           newpat = set0;
3058           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3059         }
3060
3061       else if (((REG_P (SET_DEST (set0))
3062                  && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3063                 || (GET_CODE (SET_DEST (set0)) == SUBREG
3064                     && find_reg_note (i3, REG_UNUSED,
3065                                       SUBREG_REG (SET_DEST (set0)))))
3066                && insn_nothrow_p (i3)
3067                && !side_effects_p (SET_SRC (set0)))
3068         {
3069           newpat = set1;
3070           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3071
3072           if (insn_code_number >= 0)
3073             changed_i3_dest = 1;
3074         }
3075     }
3076
3077   /* If we were combining three insns and the result is a simple SET
3078      with no ASM_OPERANDS that wasn't recognized, try to split it into two
3079      insns.  There are two ways to do this.  It can be split using a
3080      machine-specific method (like when you have an addition of a large
3081      constant) or by combine in the function find_split_point.  */
3082
3083   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3084       && asm_noperands (newpat) < 0)
3085     {
3086       rtx parallel, m_split, *split;
3087
3088       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
3089          use I2DEST as a scratch register will help.  In the latter case,
3090          convert I2DEST to the mode of the source of NEWPAT if we can.  */
3091
3092       m_split = combine_split_insns (newpat, i3);
3093
3094       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3095          inputs of NEWPAT.  */
3096
3097       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3098          possible to try that as a scratch reg.  This would require adding
3099          more code to make it work though.  */
3100
3101       if (m_split == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3102         {
3103           enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3104
3105           /* First try to split using the original register as a
3106              scratch register.  */
3107           parallel = gen_rtx_PARALLEL (VOIDmode,
3108                                        gen_rtvec (2, newpat,
3109                                                   gen_rtx_CLOBBER (VOIDmode,
3110                                                                    i2dest)));
3111           m_split = combine_split_insns (parallel, i3);
3112
3113           /* If that didn't work, try changing the mode of I2DEST if
3114              we can.  */
3115           if (m_split == 0
3116               && new_mode != GET_MODE (i2dest)
3117               && new_mode != VOIDmode
3118               && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3119             {
3120               enum machine_mode old_mode = GET_MODE (i2dest);
3121               rtx ni2dest;
3122
3123               if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3124                 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3125               else
3126                 {
3127                   SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3128                   ni2dest = regno_reg_rtx[REGNO (i2dest)];
3129                 }
3130
3131               parallel = (gen_rtx_PARALLEL
3132                           (VOIDmode,
3133                            gen_rtvec (2, newpat,
3134                                       gen_rtx_CLOBBER (VOIDmode,
3135                                                        ni2dest))));
3136               m_split = combine_split_insns (parallel, i3);
3137
3138               if (m_split == 0
3139                   && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3140                 {
3141                   struct undo *buf;
3142
3143                   adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3144                   buf = undobuf.undos;
3145                   undobuf.undos = buf->next;
3146                   buf->next = undobuf.frees;
3147                   undobuf.frees = buf;
3148                 }
3149             }
3150
3151           i2scratch = m_split != 0;
3152         }
3153
3154       /* If recog_for_combine has discarded clobbers, try to use them
3155          again for the split.  */
3156       if (m_split == 0 && newpat_vec_with_clobbers)
3157         {
3158           parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3159           m_split = combine_split_insns (parallel, i3);
3160         }
3161
3162       if (m_split && NEXT_INSN (m_split) == NULL_RTX)
3163         {
3164           m_split = PATTERN (m_split);
3165           insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
3166           if (insn_code_number >= 0)
3167             newpat = m_split;
3168         }
3169       else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
3170                && (next_real_insn (i2) == i3
3171                    || ! use_crosses_set_p (PATTERN (m_split), DF_INSN_LUID (i2))))
3172         {
3173           rtx i2set, i3set;
3174           rtx newi3pat = PATTERN (NEXT_INSN (m_split));
3175           newi2pat = PATTERN (m_split);
3176
3177           i3set = single_set (NEXT_INSN (m_split));
3178           i2set = single_set (m_split);
3179
3180           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3181
3182           /* If I2 or I3 has multiple SETs, we won't know how to track
3183              register status, so don't use these insns.  If I2's destination
3184              is used between I2 and I3, we also can't use these insns.  */
3185
3186           if (i2_code_number >= 0 && i2set && i3set
3187               && (next_real_insn (i2) == i3
3188                   || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3189             insn_code_number = recog_for_combine (&newi3pat, i3,
3190                                                   &new_i3_notes);
3191           if (insn_code_number >= 0)
3192             newpat = newi3pat;
3193
3194           /* It is possible that both insns now set the destination of I3.
3195              If so, we must show an extra use of it.  */
3196
3197           if (insn_code_number >= 0)
3198             {
3199               rtx new_i3_dest = SET_DEST (i3set);
3200               rtx new_i2_dest = SET_DEST (i2set);
3201
3202               while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3203                      || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3204                      || GET_CODE (new_i3_dest) == SUBREG)
3205                 new_i3_dest = XEXP (new_i3_dest, 0);
3206
3207               while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3208                      || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3209                      || GET_CODE (new_i2_dest) == SUBREG)
3210                 new_i2_dest = XEXP (new_i2_dest, 0);
3211
3212               if (REG_P (new_i3_dest)
3213                   && REG_P (new_i2_dest)
3214                   && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3215                 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3216             }
3217         }
3218
3219       /* If we can split it and use I2DEST, go ahead and see if that
3220          helps things be recognized.  Verify that none of the registers
3221          are set between I2 and I3.  */
3222       if (insn_code_number < 0
3223           && (split = find_split_point (&newpat, i3, false)) != 0
3224 #ifdef HAVE_cc0
3225           && REG_P (i2dest)
3226 #endif
3227           /* We need I2DEST in the proper mode.  If it is a hard register
3228              or the only use of a pseudo, we can change its mode.
3229              Make sure we don't change a hard register to have a mode that
3230              isn't valid for it, or change the number of registers.  */
3231           && (GET_MODE (*split) == GET_MODE (i2dest)
3232               || GET_MODE (*split) == VOIDmode
3233               || can_change_dest_mode (i2dest, added_sets_2,
3234                                        GET_MODE (*split)))
3235           && (next_real_insn (i2) == i3
3236               || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3237           /* We can't overwrite I2DEST if its value is still used by
3238              NEWPAT.  */
3239           && ! reg_referenced_p (i2dest, newpat))
3240         {
3241           rtx newdest = i2dest;
3242           enum rtx_code split_code = GET_CODE (*split);
3243           enum machine_mode split_mode = GET_MODE (*split);
3244           bool subst_done = false;
3245           newi2pat = NULL_RTX;
3246
3247           i2scratch = true;
3248
3249           /* *SPLIT may be part of I2SRC, so make sure we have the
3250              original expression around for later debug processing.
3251              We should not need I2SRC any more in other cases.  */
3252           if (MAY_HAVE_DEBUG_INSNS)
3253             i2src = copy_rtx (i2src);
3254           else
3255             i2src = NULL;
3256
3257           /* Get NEWDEST as a register in the proper mode.  We have already
3258              validated that we can do this.  */
3259           if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3260             {
3261               if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3262                 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3263               else
3264                 {
3265                   SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3266                   newdest = regno_reg_rtx[REGNO (i2dest)];
3267                 }
3268             }
3269
3270           /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3271              an ASHIFT.  This can occur if it was inside a PLUS and hence
3272              appeared to be a memory address.  This is a kludge.  */
3273           if (split_code == MULT
3274               && CONST_INT_P (XEXP (*split, 1))
3275               && INTVAL (XEXP (*split, 1)) > 0
3276               && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
3277             {
3278               SUBST (*split, gen_rtx_ASHIFT (split_mode,
3279                                              XEXP (*split, 0), GEN_INT (i)));
3280               /* Update split_code because we may not have a multiply
3281                  anymore.  */
3282               split_code = GET_CODE (*split);
3283             }
3284
3285 #ifdef INSN_SCHEDULING
3286           /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3287              be written as a ZERO_EXTEND.  */
3288           if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3289             {
3290 #ifdef LOAD_EXTEND_OP
3291               /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3292                  what it really is.  */
3293               if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3294                   == SIGN_EXTEND)
3295                 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3296                                                     SUBREG_REG (*split)));
3297               else
3298 #endif
3299                 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3300                                                     SUBREG_REG (*split)));
3301             }
3302 #endif
3303
3304           /* Attempt to split binary operators using arithmetic identities.  */
3305           if (BINARY_P (SET_SRC (newpat))
3306               && split_mode == GET_MODE (SET_SRC (newpat))
3307               && ! side_effects_p (SET_SRC (newpat)))
3308             {
3309               rtx setsrc = SET_SRC (newpat);
3310               enum machine_mode mode = GET_MODE (setsrc);
3311               enum rtx_code code = GET_CODE (setsrc);
3312               rtx src_op0 = XEXP (setsrc, 0);
3313               rtx src_op1 = XEXP (setsrc, 1);
3314
3315               /* Split "X = Y op Y" as "Z = Y; X = Z op Z".  */
3316               if (rtx_equal_p (src_op0, src_op1))
3317                 {
3318                   newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3319                   SUBST (XEXP (setsrc, 0), newdest);
3320                   SUBST (XEXP (setsrc, 1), newdest);
3321                   subst_done = true;
3322                 }
3323               /* Split "((P op Q) op R) op S" where op is PLUS or MULT.  */
3324               else if ((code == PLUS || code == MULT)
3325                        && GET_CODE (src_op0) == code
3326                        && GET_CODE (XEXP (src_op0, 0)) == code
3327                        && (INTEGRAL_MODE_P (mode)
3328                            || (FLOAT_MODE_P (mode)
3329                                && flag_unsafe_math_optimizations)))
3330                 {
3331                   rtx p = XEXP (XEXP (src_op0, 0), 0);
3332                   rtx q = XEXP (XEXP (src_op0, 0), 1);
3333                   rtx r = XEXP (src_op0, 1);
3334                   rtx s = src_op1;
3335
3336                   /* Split both "((X op Y) op X) op Y" and
3337                      "((X op Y) op Y) op X" as "T op T" where T is
3338                      "X op Y".  */
3339                   if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3340                        || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3341                     {
3342                       newi2pat = gen_rtx_SET (VOIDmode, newdest,
3343                                               XEXP (src_op0, 0));
3344                       SUBST (XEXP (setsrc, 0), newdest);
3345                       SUBST (XEXP (setsrc, 1), newdest);
3346                       subst_done = true;
3347                     }
3348                   /* Split "((X op X) op Y) op Y)" as "T op T" where
3349                      T is "X op Y".  */
3350                   else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3351                     {
3352                       rtx tmp = simplify_gen_binary (code, mode, p, r);
3353                       newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3354                       SUBST (XEXP (setsrc, 0), newdest);
3355                       SUBST (XEXP (setsrc, 1), newdest);
3356                       subst_done = true;
3357                     }
3358                 }
3359             }
3360
3361           if (!subst_done)
3362             {
3363               newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3364               SUBST (*split, newdest);
3365             }
3366
3367           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3368
3369           /* recog_for_combine might have added CLOBBERs to newi2pat.
3370              Make sure NEWPAT does not depend on the clobbered regs.  */
3371           if (GET_CODE (newi2pat) == PARALLEL)
3372             for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3373               if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3374                 {
3375                   rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3376                   if (reg_overlap_mentioned_p (reg, newpat))
3377                     {
3378                       undo_all ();
3379                       return 0;
3380                     }
3381                 }
3382
3383           /* If the split point was a MULT and we didn't have one before,
3384              don't use one now.  */
3385           if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3386             insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3387         }
3388     }
3389
3390   /* Check for a case where we loaded from memory in a narrow mode and
3391      then sign extended it, but we need both registers.  In that case,
3392      we have a PARALLEL with both loads from the same memory location.
3393      We can split this into a load from memory followed by a register-register
3394      copy.  This saves at least one insn, more if register allocation can
3395      eliminate the copy.
3396
3397      We cannot do this if the destination of the first assignment is a
3398      condition code register or cc0.  We eliminate this case by making sure
3399      the SET_DEST and SET_SRC have the same mode.
3400
3401      We cannot do this if the destination of the second assignment is
3402      a register that we have already assumed is zero-extended.  Similarly
3403      for a SUBREG of such a register.  */
3404
3405   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3406            && GET_CODE (newpat) == PARALLEL
3407            && XVECLEN (newpat, 0) == 2
3408            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3409            && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3410            && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3411                == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3412            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3413            && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3414                            XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3415            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3416                                    DF_INSN_LUID (i2))
3417            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3418            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3419            && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
3420                  (REG_P (temp)
3421                   && VEC_index (reg_stat_type, reg_stat,
3422                                 REGNO (temp))->nonzero_bits != 0
3423                   && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3424                   && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3425                   && (VEC_index (reg_stat_type, reg_stat,
3426                                  REGNO (temp))->nonzero_bits
3427                       != GET_MODE_MASK (word_mode))))
3428            && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3429                  && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3430                      (REG_P (temp)
3431                       && VEC_index (reg_stat_type, reg_stat,
3432                                     REGNO (temp))->nonzero_bits != 0
3433                       && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3434                       && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3435                       && (VEC_index (reg_stat_type, reg_stat,
3436                                      REGNO (temp))->nonzero_bits
3437                           != GET_MODE_MASK (word_mode)))))
3438            && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3439                                          SET_SRC (XVECEXP (newpat, 0, 1)))
3440            && ! find_reg_note (i3, REG_UNUSED,
3441                                SET_DEST (XVECEXP (newpat, 0, 0))))
3442     {
3443       rtx ni2dest;
3444
3445       newi2pat = XVECEXP (newpat, 0, 0);
3446       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3447       newpat = XVECEXP (newpat, 0, 1);
3448       SUBST (SET_SRC (newpat),
3449              gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3450       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3451
3452       if (i2_code_number >= 0)
3453         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3454
3455       if (insn_code_number >= 0)
3456         swap_i2i3 = 1;
3457     }
3458
3459   /* Similarly, check for a case where we have a PARALLEL of two independent
3460      SETs but we started with three insns.  In this case, we can do the sets
3461      as two separate insns.  This case occurs when some SET allows two
3462      other insns to combine, but the destination of that SET is still live.  */
3463
3464   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3465            && GET_CODE (newpat) == PARALLEL
3466            && XVECLEN (newpat, 0) == 2
3467            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3468            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3469            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3470            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3471            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3472            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3473            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3474                                    DF_INSN_LUID (i2))
3475            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3476                                   XVECEXP (newpat, 0, 0))
3477            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3478                                   XVECEXP (newpat, 0, 1))
3479            && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3480                  && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1))))
3481 #ifdef HAVE_cc0
3482            /* We cannot split the parallel into two sets if both sets
3483               reference cc0.  */
3484            && ! (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0))
3485                  && reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 1)))
3486 #endif
3487            )
3488     {
3489       /* Normally, it doesn't matter which of the two is done first,
3490          but it does if one references cc0.  In that case, it has to
3491          be first.  */
3492 #ifdef HAVE_cc0
3493       if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
3494         {
3495           newi2pat = XVECEXP (newpat, 0, 0);
3496           newpat = XVECEXP (newpat, 0, 1);
3497         }
3498       else
3499 #endif
3500         {
3501           newi2pat = XVECEXP (newpat, 0, 1);
3502           newpat = XVECEXP (newpat, 0, 0);
3503         }
3504
3505       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3506
3507       if (i2_code_number >= 0)
3508         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3509     }
3510
3511   /* If it still isn't recognized, fail and change things back the way they
3512      were.  */
3513   if ((insn_code_number < 0
3514        /* Is the result a reasonable ASM_OPERANDS?  */
3515        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3516     {
3517       undo_all ();
3518       return 0;
3519     }
3520
3521   /* If we had to change another insn, make sure it is valid also.  */
3522   if (undobuf.other_insn)
3523     {
3524       CLEAR_HARD_REG_SET (newpat_used_regs);
3525
3526       other_pat = PATTERN (undobuf.other_insn);
3527       other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3528                                              &new_other_notes);
3529
3530       if (other_code_number < 0 && ! check_asm_operands (other_pat))
3531         {
3532           undo_all ();
3533           return 0;
3534         }
3535     }
3536
3537 #ifdef HAVE_cc0
3538   /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3539      they are adjacent to each other or not.  */
3540   {
3541     rtx p = prev_nonnote_insn (i3);
3542     if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3543         && sets_cc0_p (newi2pat))
3544       {
3545         undo_all ();
3546         return 0;
3547       }
3548   }
3549 #endif
3550
3551   /* Only allow this combination if insn_rtx_costs reports that the
3552      replacement instructions are cheaper than the originals.  */
3553   if (!combine_validate_cost (i1, i2, i3, newpat, newi2pat, other_pat))
3554     {
3555       undo_all ();
3556       return 0;
3557     }
3558
3559   if (MAY_HAVE_DEBUG_INSNS)
3560     {
3561       struct undo *undo;
3562
3563       for (undo = undobuf.undos; undo; undo = undo->next)
3564         if (undo->kind == UNDO_MODE)
3565           {
3566             rtx reg = *undo->where.r;
3567             enum machine_mode new_mode = GET_MODE (reg);
3568             enum machine_mode old_mode = undo->old_contents.m;
3569
3570             /* Temporarily revert mode back.  */
3571             adjust_reg_mode (reg, old_mode);
3572
3573             if (reg == i2dest && i2scratch)
3574               {
3575                 /* If we used i2dest as a scratch register with a
3576                    different mode, substitute it for the original
3577                    i2src while its original mode is temporarily
3578                    restored, and then clear i2scratch so that we don't
3579                    do it again later.  */
3580                 propagate_for_debug (i2, i3, reg, i2src, false);
3581                 i2scratch = false;
3582                 /* Put back the new mode.  */
3583                 adjust_reg_mode (reg, new_mode);
3584               }
3585             else
3586               {
3587                 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
3588                 rtx first, last;
3589
3590                 if (reg == i2dest)
3591                   {
3592                     first = i2;
3593                     last = i3;
3594                   }
3595                 else
3596                   {
3597                     first = i3;
3598                     last = undobuf.other_insn;
3599                     gcc_assert (last);
3600                   }
3601
3602                 /* We're dealing with a reg that changed mode but not
3603                    meaning, so we want to turn it into a subreg for
3604                    the new mode.  However, because of REG sharing and
3605                    because its mode had already changed, we have to do
3606                    it in two steps.  First, replace any debug uses of
3607                    reg, with its original mode temporarily restored,
3608                    with this copy we have created; then, replace the
3609                    copy with the SUBREG of the original shared reg,
3610                    once again changed to the new mode.  */
3611                 propagate_for_debug (first, last, reg, tempreg, false);
3612                 adjust_reg_mode (reg, new_mode);
3613                 propagate_for_debug (first, last, tempreg,
3614                                      lowpart_subreg (old_mode, reg, new_mode),
3615                                      false);
3616               }
3617           }
3618     }
3619
3620   /* If we will be able to accept this, we have made a
3621      change to the destination of I3.  This requires us to
3622      do a few adjustments.  */
3623
3624   if (changed_i3_dest)
3625     {
3626       PATTERN (i3) = newpat;
3627       adjust_for_new_dest (i3);
3628     }
3629
3630   /* We now know that we can do this combination.  Merge the insns and
3631      update the status of registers and LOG_LINKS.  */
3632
3633   if (undobuf.other_insn)
3634     {
3635       rtx note, next;
3636
3637       PATTERN (undobuf.other_insn) = other_pat;
3638
3639       /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
3640          are still valid.  Then add any non-duplicate notes added by
3641          recog_for_combine.  */
3642       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
3643         {
3644           next = XEXP (note, 1);
3645
3646           if (REG_NOTE_KIND (note) == REG_UNUSED
3647               && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
3648             remove_note (undobuf.other_insn, note);
3649         }
3650
3651       distribute_notes (new_other_notes, undobuf.other_insn,
3652                         undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
3653     }
3654
3655   if (swap_i2i3)
3656     {
3657       rtx insn;
3658       rtx link;
3659       rtx ni2dest;
3660
3661       /* I3 now uses what used to be its destination and which is now
3662          I2's destination.  This requires us to do a few adjustments.  */
3663       PATTERN (i3) = newpat;
3664       adjust_for_new_dest (i3);
3665
3666       /* We need a LOG_LINK from I3 to I2.  But we used to have one,
3667          so we still will.
3668
3669          However, some later insn might be using I2's dest and have
3670          a LOG_LINK pointing at I3.  We must remove this link.
3671          The simplest way to remove the link is to point it at I1,
3672          which we know will be a NOTE.  */
3673
3674       /* newi2pat is usually a SET here; however, recog_for_combine might
3675          have added some clobbers.  */
3676       if (GET_CODE (newi2pat) == PARALLEL)
3677         ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
3678       else
3679         ni2dest = SET_DEST (newi2pat);
3680
3681       for (insn = NEXT_INSN (i3);
3682            insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3683                     || insn != BB_HEAD (this_basic_block->next_bb));
3684            insn = NEXT_INSN (insn))
3685         {
3686           if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
3687             {
3688               for (link = LOG_LINKS (insn); link;
3689                    link = XEXP (link, 1))
3690                 if (XEXP (link, 0) == i3)
3691                   XEXP (link, 0) = i1;
3692
3693               break;
3694             }
3695         }
3696     }
3697
3698   {
3699     rtx i3notes, i2notes, i1notes = 0;
3700     rtx i3links, i2links, i1links = 0;
3701     rtx midnotes = 0;
3702     unsigned int regno;
3703     /* Compute which registers we expect to eliminate.  newi2pat may be setting
3704        either i3dest or i2dest, so we must check it.  Also, i1dest may be the
3705        same as i3dest, in which case newi2pat may be setting i1dest.  */
3706     rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
3707                    || i2dest_in_i2src || i2dest_in_i1src
3708                    || !i2dest_killed
3709                    ? 0 : i2dest);
3710     rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
3711                    || (newi2pat && reg_set_p (i1dest, newi2pat))
3712                    || !i1dest_killed
3713                    ? 0 : i1dest);
3714
3715     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
3716        clear them.  */
3717     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
3718     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
3719     if (i1)
3720       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
3721
3722     /* Ensure that we do not have something that should not be shared but
3723        occurs multiple times in the new insns.  Check this by first
3724        resetting all the `used' flags and then copying anything is shared.  */
3725
3726     reset_used_flags (i3notes);
3727     reset_used_flags (i2notes);
3728     reset_used_flags (i1notes);
3729     reset_used_flags (newpat);
3730     reset_used_flags (newi2pat);
3731     if (undobuf.other_insn)
3732       reset_used_flags (PATTERN (undobuf.other_insn));
3733
3734     i3notes = copy_rtx_if_shared (i3notes);
3735     i2notes = copy_rtx_if_shared (i2notes);
3736     i1notes = copy_rtx_if_shared (i1notes);
3737     newpat = copy_rtx_if_shared (newpat);
3738     newi2pat = copy_rtx_if_shared (newi2pat);
3739     if (undobuf.other_insn)
3740       reset_used_flags (PATTERN (undobuf.other_insn));
3741
3742     INSN_CODE (i3) = insn_code_number;
3743     PATTERN (i3) = newpat;
3744
3745     if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
3746       {
3747         rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
3748
3749         reset_used_flags (call_usage);
3750         call_usage = copy_rtx (call_usage);
3751
3752         if (substed_i2)
3753           {
3754             /* I2SRC must still be meaningful at this point.  Some splitting
3755                operations can invalidate I2SRC, but those operations do not
3756                apply to calls.  */
3757             gcc_assert (i2src);
3758             replace_rtx (call_usage, i2dest, i2src);
3759           }
3760
3761         if (substed_i1)
3762           replace_rtx (call_usage, i1dest, i1src);
3763
3764         CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
3765       }
3766
3767     if (undobuf.other_insn)
3768       INSN_CODE (undobuf.other_insn) = other_code_number;
3769
3770     /* We had one special case above where I2 had more than one set and
3771        we replaced a destination of one of those sets with the destination
3772        of I3.  In that case, we have to update LOG_LINKS of insns later
3773        in this basic block.  Note that this (expensive) case is rare.
3774
3775        Also, in this case, we must pretend that all REG_NOTEs for I2
3776        actually came from I3, so that REG_UNUSED notes from I2 will be
3777        properly handled.  */
3778
3779     if (i3_subst_into_i2)
3780       {
3781         for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
3782           if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
3783                || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
3784               && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
3785               && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
3786               && ! find_reg_note (i2, REG_UNUSED,
3787                                   SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
3788             for (temp = NEXT_INSN (i2);
3789                  temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3790                           || BB_HEAD (this_basic_block) != temp);
3791                  temp = NEXT_INSN (temp))
3792               if (temp != i3 && INSN_P (temp))
3793                 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
3794                   if (XEXP (link, 0) == i2)
3795                     XEXP (link, 0) = i3;
3796
3797         if (i3notes)
3798           {
3799             rtx link = i3notes;
3800             while (XEXP (link, 1))
3801               link = XEXP (link, 1);
3802             XEXP (link, 1) = i2notes;
3803           }
3804         else
3805           i3notes = i2notes;
3806         i2notes = 0;
3807       }
3808
3809     LOG_LINKS (i3) = 0;
3810     REG_NOTES (i3) = 0;
3811     LOG_LINKS (i2) = 0;
3812     REG_NOTES (i2) = 0;
3813
3814     if (newi2pat)
3815       {
3816         if (MAY_HAVE_DEBUG_INSNS && i2scratch)
3817           propagate_for_debug (i2, i3, i2dest, i2src, false);
3818         INSN_CODE (i2) = i2_code_number;
3819         PATTERN (i2) = newi2pat;
3820       }
3821     else
3822       {
3823         if (MAY_HAVE_DEBUG_INSNS && i2src)
3824           propagate_for_debug (i2, i3, i2dest, i2src, i3_subst_into_i2);
3825         SET_INSN_DELETED (i2);
3826       }
3827
3828     if (i1)
3829       {
3830         LOG_LINKS (i1) = 0;
3831         REG_NOTES (i1) = 0;
3832         if (MAY_HAVE_DEBUG_INSNS)
3833           propagate_for_debug (i1, i3, i1dest, i1src, false);
3834         SET_INSN_DELETED (i1);
3835       }
3836
3837     /* Get death notes for everything that is now used in either I3 or
3838        I2 and used to die in a previous insn.  If we built two new
3839        patterns, move from I1 to I2 then I2 to I3 so that we get the
3840        proper movement on registers that I2 modifies.  */
3841
3842     if (newi2pat)
3843       {
3844         move_deaths (newi2pat, NULL_RTX, DF_INSN_LUID (i1), i2, &midnotes);
3845         move_deaths (newpat, newi2pat, DF_INSN_LUID (i1), i3, &midnotes);
3846       }
3847     else
3848       move_deaths (newpat, NULL_RTX, i1 ? DF_INSN_LUID (i1) : DF_INSN_LUID (i2),
3849                    i3, &midnotes);
3850
3851     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
3852     if (i3notes)
3853       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
3854                         elim_i2, elim_i1);
3855     if (i2notes)
3856       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
3857                         elim_i2, elim_i1);
3858     if (i1notes)
3859       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
3860                         elim_i2, elim_i1);
3861     if (midnotes)
3862       distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3863                         elim_i2, elim_i1);
3864
3865     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
3866        know these are REG_UNUSED and want them to go to the desired insn,
3867        so we always pass it as i3.  */
3868
3869     if (newi2pat && new_i2_notes)
3870       distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3871
3872     if (new_i3_notes)
3873       distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
3874
3875     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
3876        put a REG_DEAD note for it somewhere.  If NEWI2PAT exists and sets
3877        I3DEST, the death must be somewhere before I2, not I3.  If we passed I3
3878        in that case, it might delete I2.  Similarly for I2 and I1.
3879        Show an additional death due to the REG_DEAD note we make here.  If
3880        we discard it in distribute_notes, we will decrement it again.  */
3881
3882     if (i3dest_killed)
3883       {
3884         if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
3885           distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
3886                                             NULL_RTX),
3887                             NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
3888         else
3889           distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
3890                                             NULL_RTX),
3891                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3892                             elim_i2, elim_i1);
3893       }
3894
3895     if (i2dest_in_i2src)
3896       {
3897         if (newi2pat && reg_set_p (i2dest, newi2pat))
3898           distribute_notes (alloc_reg_note (REG_DEAD, i2dest, NULL_RTX),
3899                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3900         else
3901           distribute_notes (alloc_reg_note (REG_DEAD, i2dest, NULL_RTX),
3902                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3903                             NULL_RTX, NULL_RTX);
3904       }
3905
3906     if (i1dest_in_i1src)
3907       {
3908         if (newi2pat && reg_set_p (i1dest, newi2pat))
3909           distribute_notes (alloc_reg_note (REG_DEAD, i1dest, NULL_RTX),
3910                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3911         else
3912           distribute_notes (alloc_reg_note (REG_DEAD, i1dest, NULL_RTX),
3913                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3914                             NULL_RTX, NULL_RTX);
3915       }
3916
3917     distribute_links (i3links);
3918     distribute_links (i2links);
3919     distribute_links (i1links);
3920
3921     if (REG_P (i2dest))
3922       {
3923         rtx link;
3924         rtx i2_insn = 0, i2_val = 0, set;
3925
3926         /* The insn that used to set this register doesn't exist, and
3927            this life of the register may not exist either.  See if one of
3928            I3's links points to an insn that sets I2DEST.  If it does,
3929            that is now the last known value for I2DEST. If we don't update
3930            this and I2 set the register to a value that depended on its old
3931            contents, we will get confused.  If this insn is used, thing
3932            will be set correctly in combine_instructions.  */
3933
3934         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3935           if ((set = single_set (XEXP (link, 0))) != 0
3936               && rtx_equal_p (i2dest, SET_DEST (set)))
3937             i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
3938
3939         record_value_for_reg (i2dest, i2_insn, i2_val);
3940
3941         /* If the reg formerly set in I2 died only once and that was in I3,
3942            zero its use count so it won't make `reload' do any work.  */
3943         if (! added_sets_2
3944             && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
3945             && ! i2dest_in_i2src)
3946           {
3947             regno = REGNO (i2dest);
3948             INC_REG_N_SETS (regno, -1);
3949           }
3950       }
3951
3952     if (i1 && REG_P (i1dest))
3953       {
3954         rtx link;
3955         rtx i1_insn = 0, i1_val = 0, set;
3956
3957         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3958           if ((set = single_set (XEXP (link, 0))) != 0
3959               && rtx_equal_p (i1dest, SET_DEST (set)))
3960             i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
3961
3962         record_value_for_reg (i1dest, i1_insn, i1_val);
3963
3964         regno = REGNO (i1dest);
3965         if (! added_sets_1 && ! i1dest_in_i1src)
3966           INC_REG_N_SETS (regno, -1);
3967       }
3968
3969     /* Update reg_stat[].nonzero_bits et al for any changes that may have
3970        been made to this insn.  The order of
3971        set_nonzero_bits_and_sign_copies() is important.  Because newi2pat
3972        can affect nonzero_bits of newpat */
3973     if (newi2pat)
3974       note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
3975     note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
3976   }
3977
3978   if (undobuf.other_insn != NULL_RTX)
3979     {
3980       if (dump_file)
3981         {
3982           fprintf (dump_file, "modifying other_insn ");
3983           dump_insn_slim (dump_file, undobuf.other_insn);
3984         }
3985       df_insn_rescan (undobuf.other_insn);
3986     }
3987
3988   if (i1 && !(NOTE_P(i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
3989     {
3990       if (dump_file)
3991         {
3992           fprintf (dump_file, "modifying insn i1 ");
3993           dump_insn_slim (dump_file, i1);
3994         }
3995       df_insn_rescan (i1);
3996     }
3997
3998   if (i2 && !(NOTE_P(i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
3999     {
4000       if (dump_file)
4001         {
4002           fprintf (dump_file, "modifying insn i2 ");
4003           dump_insn_slim (dump_file, i2);
4004         }
4005       df_insn_rescan (i2);
4006     }
4007
4008   if (i3 && !(NOTE_P(i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4009     {
4010       if (dump_file)
4011         {
4012           fprintf (dump_file, "modifying insn i3 ");
4013           dump_insn_slim (dump_file, i3);
4014         }
4015       df_insn_rescan (i3);
4016     }
4017
4018   /* Set new_direct_jump_p if a new return or simple jump instruction
4019      has been created.  Adjust the CFG accordingly.  */
4020
4021   if (returnjump_p (i3) || any_uncondjump_p (i3))
4022     {
4023       *new_direct_jump_p = 1;
4024       mark_jump_label (PATTERN (i3), i3, 0);
4025       update_cfg_for_uncondjump (i3);
4026     }
4027
4028   if (undobuf.other_insn != NULL_RTX
4029       && (returnjump_p (undobuf.other_insn)
4030           || any_uncondjump_p (undobuf.other_insn)))
4031     {
4032       *new_direct_jump_p = 1;
4033       update_cfg_for_uncondjump (undobuf.other_insn);
4034     }
4035
4036   /* A noop might also need cleaning up of CFG, if it comes from the
4037      simplification of a jump.  */
4038   if (GET_CODE (newpat) == SET
4039       && SET_SRC (newpat) == pc_rtx
4040       && SET_DEST (newpat) == pc_rtx)
4041     {
4042       *new_direct_jump_p = 1;
4043       update_cfg_for_uncondjump (i3);
4044     }
4045
4046   combine_successes++;
4047   undo_commit ();
4048
4049   if (added_links_insn
4050       && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4051       && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4052     return added_links_insn;
4053   else
4054     return newi2pat ? i2 : i3;
4055 }
4056 \f
4057 /* Undo all the modifications recorded in undobuf.  */
4058
4059 static void
4060 undo_all (void)
4061 {
4062   struct undo *undo, *next;
4063
4064   for (undo = undobuf.undos; undo; undo = next)
4065     {
4066       next = undo->next;
4067       switch (undo->kind)
4068         {
4069         case UNDO_RTX:
4070           *undo->where.r = undo->old_contents.r;
4071           break;
4072         case UNDO_INT:
4073           *undo->where.i = undo->old_contents.i;
4074           break;
4075         case UNDO_MODE:
4076           adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4077           break;
4078         default:
4079           gcc_unreachable ();
4080         }
4081
4082       undo->next = undobuf.frees;
4083       undobuf.frees = undo;
4084     }
4085
4086   undobuf.undos = 0;
4087 }
4088
4089 /* We've committed to accepting the changes we made.  Move all
4090    of the undos to the free list.  */
4091
4092 static void
4093 undo_commit (void)
4094 {
4095   struct undo *undo, *next;
4096
4097   for (undo = undobuf.undos; undo; undo = next)
4098     {
4099       next = undo->next;
4100       undo->next = undobuf.frees;
4101       undobuf.frees = undo;
4102     }
4103   undobuf.undos = 0;
4104 }
4105 \f
4106 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4107    where we have an arithmetic expression and return that point.  LOC will
4108    be inside INSN.
4109
4110    try_combine will call this function to see if an insn can be split into
4111    two insns.  */
4112
4113 static rtx *
4114 find_split_point (rtx *loc, rtx insn, bool set_src)
4115 {
4116   rtx x = *loc;
4117   enum rtx_code code = GET_CODE (x);
4118   rtx *split;
4119   unsigned HOST_WIDE_INT len = 0;
4120   HOST_WIDE_INT pos = 0;
4121   int unsignedp = 0;
4122   rtx inner = NULL_RTX;
4123
4124   /* First special-case some codes.  */
4125   switch (code)
4126     {
4127     case SUBREG:
4128 #ifdef INSN_SCHEDULING
4129       /* If we are making a paradoxical SUBREG invalid, it becomes a split
4130          point.  */
4131       if (MEM_P (SUBREG_REG (x)))
4132         return loc;
4133 #endif
4134       return find_split_point (&SUBREG_REG (x), insn, false);
4135
4136     case MEM:
4137 #ifdef HAVE_lo_sum
4138       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4139          using LO_SUM and HIGH.  */
4140       if (GET_CODE (XEXP (x, 0)) == CONST
4141           || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4142         {
4143           enum machine_mode address_mode
4144             = targetm.addr_space.address_mode (MEM_ADDR_SPACE (x));
4145
4146           SUBST (XEXP (x, 0),
4147                  gen_rtx_LO_SUM (address_mode,
4148                                  gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4149                                  XEXP (x, 0)));
4150           return &XEXP (XEXP (x, 0), 0);
4151         }
4152 #endif
4153
4154       /* If we have a PLUS whose second operand is a constant and the
4155          address is not valid, perhaps will can split it up using
4156          the machine-specific way to split large constants.  We use
4157          the first pseudo-reg (one of the virtual regs) as a placeholder;
4158          it will not remain in the result.  */
4159       if (GET_CODE (XEXP (x, 0)) == PLUS
4160           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4161           && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4162                                             MEM_ADDR_SPACE (x)))
4163         {
4164           rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4165           rtx seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4166                                                       XEXP (x, 0)),
4167                                          subst_insn);
4168
4169           /* This should have produced two insns, each of which sets our
4170              placeholder.  If the source of the second is a valid address,
4171              we can make put both sources together and make a split point
4172              in the middle.  */
4173
4174           if (seq
4175               && NEXT_INSN (seq) != NULL_RTX
4176               && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4177               && NONJUMP_INSN_P (seq)
4178               && GET_CODE (PATTERN (seq)) == SET
4179               && SET_DEST (PATTERN (seq)) == reg
4180               && ! reg_mentioned_p (reg,
4181                                     SET_SRC (PATTERN (seq)))
4182               && NONJUMP_INSN_P (NEXT_INSN (seq))
4183               && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4184               && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4185               && memory_address_addr_space_p
4186                    (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4187                     MEM_ADDR_SPACE (x)))
4188             {
4189               rtx src1 = SET_SRC (PATTERN (seq));
4190               rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4191
4192               /* Replace the placeholder in SRC2 with SRC1.  If we can
4193                  find where in SRC2 it was placed, that can become our
4194                  split point and we can replace this address with SRC2.
4195                  Just try two obvious places.  */
4196
4197               src2 = replace_rtx (src2, reg, src1);
4198               split = 0;
4199               if (XEXP (src2, 0) == src1)
4200                 split = &XEXP (src2, 0);
4201               else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4202                        && XEXP (XEXP (src2, 0), 0) == src1)
4203                 split = &XEXP (XEXP (src2, 0), 0);
4204
4205               if (split)
4206                 {
4207                   SUBST (XEXP (x, 0), src2);
4208                   return split;
4209                 }
4210             }
4211
4212           /* If that didn't work, perhaps the first operand is complex and
4213              needs to be computed separately, so make a split point there.
4214              This will occur on machines that just support REG + CONST
4215              and have a constant moved through some previous computation.  */
4216
4217           else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4218                    && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4219                          && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4220             return &XEXP (XEXP (x, 0), 0);
4221         }
4222
4223       /* If we have a PLUS whose first operand is complex, try computing it
4224          separately by making a split there.  */
4225       if (GET_CODE (XEXP (x, 0)) == PLUS
4226           && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4227                                             MEM_ADDR_SPACE (x))
4228           && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4229           && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4230                 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4231         return &XEXP (XEXP (x, 0), 0);
4232       break;
4233
4234     case SET:
4235 #ifdef HAVE_cc0
4236       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4237          ZERO_EXTRACT, the most likely reason why this doesn't match is that
4238          we need to put the operand into a register.  So split at that
4239          point.  */
4240
4241       if (SET_DEST (x) == cc0_rtx
4242           && GET_CODE (SET_SRC (x)) != COMPARE
4243           && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4244           && !OBJECT_P (SET_SRC (x))
4245           && ! (GET_CODE (SET_SRC (x)) == SUBREG
4246                 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4247         return &SET_SRC (x);
4248 #endif
4249
4250       /* See if we can split SET_SRC as it stands.  */
4251       split = find_split_point (&SET_SRC (x), insn, true);
4252       if (split && split != &SET_SRC (x))
4253         return split;
4254
4255       /* See if we can split SET_DEST as it stands.  */
4256       split = find_split_point (&SET_DEST (x), insn, false);
4257       if (split && split != &SET_DEST (x))
4258         return split;
4259
4260       /* See if this is a bitfield assignment with everything constant.  If
4261          so, this is an IOR of an AND, so split it into that.  */
4262       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4263           && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
4264               <= HOST_BITS_PER_WIDE_INT)
4265           && CONST_INT_P (XEXP (SET_DEST (x), 1))
4266           && CONST_INT_P (XEXP (SET_DEST (x), 2))
4267           && CONST_INT_P (SET_SRC (x))
4268           && ((INTVAL (XEXP (SET_DEST (x), 1))
4269                + INTVAL (XEXP (SET_DEST (x), 2)))
4270               <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
4271           && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4272         {
4273           HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4274           unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4275           unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4276           rtx dest = XEXP (SET_DEST (x), 0);
4277           enum machine_mode mode = GET_MODE (dest);
4278           unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
4279           rtx or_mask;
4280
4281           if (BITS_BIG_ENDIAN)
4282             pos = GET_MODE_BITSIZE (mode) - len - pos;
4283
4284           or_mask = gen_int_mode (src << pos, mode);
4285           if (src == mask)
4286             SUBST (SET_SRC (x),
4287                    simplify_gen_binary (IOR, mode, dest, or_mask));
4288           else
4289             {
4290               rtx negmask = gen_int_mode (~(mask << pos), mode);
4291               SUBST (SET_SRC (x),
4292                      simplify_gen_binary (IOR, mode,
4293                                           simplify_gen_binary (AND, mode,
4294                                                                dest, negmask),
4295                                           or_mask));
4296             }
4297
4298           SUBST (SET_DEST (x), dest);
4299
4300           split = find_split_point (&SET_SRC (x), insn, true);
4301           if (split && split != &SET_SRC (x))
4302             return split;
4303         }
4304
4305       /* Otherwise, see if this is an operation that we can split into two.
4306          If so, try to split that.  */
4307       code = GET_CODE (SET_SRC (x));
4308
4309       switch (code)
4310         {
4311         case AND:
4312           /* If we are AND'ing with a large constant that is only a single
4313              bit and the result is only being used in a context where we
4314              need to know if it is zero or nonzero, replace it with a bit
4315              extraction.  This will avoid the large constant, which might
4316              have taken more than one insn to make.  If the constant were
4317              not a valid argument to the AND but took only one insn to make,
4318              this is no worse, but if it took more than one insn, it will
4319              be better.  */
4320
4321           if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4322               && REG_P (XEXP (SET_SRC (x), 0))
4323               && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4324               && REG_P (SET_DEST (x))
4325               && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
4326               && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4327               && XEXP (*split, 0) == SET_DEST (x)
4328               && XEXP (*split, 1) == const0_rtx)
4329             {
4330               rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4331                                                 XEXP (SET_SRC (x), 0),
4332                                                 pos, NULL_RTX, 1, 1, 0, 0);
4333               if (extraction != 0)
4334                 {
4335                   SUBST (SET_SRC (x), extraction);
4336                   return find_split_point (loc, insn, false);
4337                 }
4338             }
4339           break;
4340
4341         case NE:
4342           /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4343              is known to be on, this can be converted into a NEG of a shift.  */
4344           if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4345               && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4346               && 1 <= (pos = exact_log2
4347                        (nonzero_bits (XEXP (SET_SRC (x), 0),
4348                                       GET_MODE (XEXP (SET_SRC (x), 0))))))
4349             {
4350               enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4351
4352               SUBST (SET_SRC (x),
4353                      gen_rtx_NEG (mode,
4354                                   gen_rtx_LSHIFTRT (mode,
4355                                                     XEXP (SET_SRC (x), 0),
4356                                                     GEN_INT (pos))));
4357
4358               split = find_split_point (&SET_SRC (x), insn, true);
4359               if (split && split != &SET_SRC (x))
4360                 return split;
4361             }
4362           break;
4363
4364         case SIGN_EXTEND:
4365           inner = XEXP (SET_SRC (x), 0);
4366
4367           /* We can't optimize if either mode is a partial integer
4368              mode as we don't know how many bits are significant
4369              in those modes.  */
4370           if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4371               || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4372             break;
4373
4374           pos = 0;
4375           len = GET_MODE_BITSIZE (GET_MODE (inner));
4376           unsignedp = 0;
4377           break;
4378
4379         case SIGN_EXTRACT:
4380         case ZERO_EXTRACT:
4381           if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4382               && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4383             {
4384               inner = XEXP (SET_SRC (x), 0);
4385               len = INTVAL (XEXP (SET_SRC (x), 1));
4386               pos = INTVAL (XEXP (SET_SRC (x), 2));
4387
4388               if (BITS_BIG_ENDIAN)
4389                 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
4390               unsignedp = (code == ZERO_EXTRACT);
4391             }
4392           break;
4393
4394         default:
4395           break;
4396         }
4397
4398       if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
4399         {
4400           enum machine_mode mode = GET_MODE (SET_SRC (x));
4401
4402           /* For unsigned, we have a choice of a shift followed by an
4403              AND or two shifts.  Use two shifts for field sizes where the
4404              constant might be too large.  We assume here that we can
4405              always at least get 8-bit constants in an AND insn, which is
4406              true for every current RISC.  */
4407
4408           if (unsignedp && len <= 8)
4409             {
4410               SUBST (SET_SRC (x),
4411                      gen_rtx_AND (mode,
4412                                   gen_rtx_LSHIFTRT
4413                                   (mode, gen_lowpart (mode, inner),
4414                                    GEN_INT (pos)),
4415                                   GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
4416
4417               split = find_split_point (&SET_SRC (x), insn, true);
4418               if (split && split != &SET_SRC (x))
4419                 return split;
4420             }
4421           else
4422             {
4423               SUBST (SET_SRC (x),
4424                      gen_rtx_fmt_ee
4425                      (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4426                       gen_rtx_ASHIFT (mode,
4427                                       gen_lowpart (mode, inner),
4428                                       GEN_INT (GET_MODE_BITSIZE (mode)
4429                                                - len - pos)),
4430                       GEN_INT (GET_MODE_BITSIZE (mode) - len)));
4431
4432               split = find_split_point (&SET_SRC (x), insn, true);
4433               if (split && split != &SET_SRC (x))
4434                 return split;
4435             }
4436         }
4437
4438       /* See if this is a simple operation with a constant as the second
4439          operand.  It might be that this constant is out of range and hence
4440          could be used as a split point.  */
4441       if (BINARY_P (SET_SRC (x))
4442           && CONSTANT_P (XEXP (SET_SRC (x), 1))
4443           && (OBJECT_P (XEXP (SET_SRC (x), 0))
4444               || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4445                   && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4446         return &XEXP (SET_SRC (x), 1);
4447
4448       /* Finally, see if this is a simple operation with its first operand
4449          not in a register.  The operation might require this operand in a
4450          register, so return it as a split point.  We can always do this
4451          because if the first operand were another operation, we would have
4452          already found it as a split point.  */
4453       if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4454           && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4455         return &XEXP (SET_SRC (x), 0);
4456
4457       return 0;
4458
4459     case AND:
4460     case IOR:
4461       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4462          it is better to write this as (not (ior A B)) so we can split it.
4463          Similarly for IOR.  */
4464       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4465         {
4466           SUBST (*loc,
4467                  gen_rtx_NOT (GET_MODE (x),
4468                               gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4469                                               GET_MODE (x),
4470                                               XEXP (XEXP (x, 0), 0),
4471                                               XEXP (XEXP (x, 1), 0))));
4472           return find_split_point (loc, insn, set_src);
4473         }
4474
4475       /* Many RISC machines have a large set of logical insns.  If the
4476          second operand is a NOT, put it first so we will try to split the
4477          other operand first.  */
4478       if (GET_CODE (XEXP (x, 1)) == NOT)
4479         {
4480           rtx tem = XEXP (x, 0);
4481           SUBST (XEXP (x, 0), XEXP (x, 1));
4482           SUBST (XEXP (x, 1), tem);
4483         }
4484       break;
4485
4486     case PLUS:
4487     case MINUS:
4488       /* Split at a multiply-accumulate instruction.  However if this is
4489          the SET_SRC, we likely do not have such an instruction and it's
4490          worthless to try this split.  */
4491       if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
4492         return loc;
4493
4494     default:
4495       break;
4496     }
4497
4498   /* Otherwise, select our actions depending on our rtx class.  */
4499   switch (GET_RTX_CLASS (code))
4500     {
4501     case RTX_BITFIELD_OPS:              /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
4502     case RTX_TERNARY:
4503       split = find_split_point (&XEXP (x, 2), insn, false);
4504       if (split)
4505         return split;
4506       /* ... fall through ...  */
4507     case RTX_BIN_ARITH:
4508     case RTX_COMM_ARITH:
4509     case RTX_COMPARE:
4510     case RTX_COMM_COMPARE:
4511       split = find_split_point (&XEXP (x, 1), insn, false);
4512       if (split)
4513         return split;
4514       /* ... fall through ...  */
4515     case RTX_UNARY:
4516       /* Some machines have (and (shift ...) ...) insns.  If X is not
4517          an AND, but XEXP (X, 0) is, use it as our split point.  */
4518       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4519         return &XEXP (x, 0);
4520
4521       split = find_split_point (&XEXP (x, 0), insn, false);
4522       if (split)
4523         return split;
4524       return loc;
4525
4526     default:
4527       /* Otherwise, we don't have a split point.  */
4528       return 0;
4529     }
4530 }
4531 \f
4532 /* Throughout X, replace FROM with TO, and return the result.
4533    The result is TO if X is FROM;
4534    otherwise the result is X, but its contents may have been modified.
4535    If they were modified, a record was made in undobuf so that
4536    undo_all will (among other things) return X to its original state.
4537
4538    If the number of changes necessary is too much to record to undo,
4539    the excess changes are not made, so the result is invalid.
4540    The changes already made can still be undone.
4541    undobuf.num_undo is incremented for such changes, so by testing that
4542    the caller can tell whether the result is valid.
4543
4544    `n_occurrences' is incremented each time FROM is replaced.
4545
4546    IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4547
4548    UNIQUE_COPY is nonzero if each substitution must be unique.  We do this
4549    by copying if `n_occurrences' is nonzero.  */
4550
4551 static rtx
4552 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
4553 {
4554   enum rtx_code code = GET_CODE (x);
4555   enum machine_mode op0_mode = VOIDmode;
4556   const char *fmt;
4557   int len, i;
4558   rtx new_rtx;
4559
4560 /* Two expressions are equal if they are identical copies of a shared
4561    RTX or if they are both registers with the same register number
4562    and mode.  */
4563
4564 #define COMBINE_RTX_EQUAL_P(X,Y)                        \
4565   ((X) == (Y)                                           \
4566    || (REG_P (X) && REG_P (Y)   \
4567        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4568
4569   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
4570     {
4571       n_occurrences++;
4572       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
4573     }
4574
4575   /* If X and FROM are the same register but different modes, they
4576      will not have been seen as equal above.  However, the log links code
4577      will make a LOG_LINKS entry for that case.  If we do nothing, we
4578      will try to rerecognize our original insn and, when it succeeds,
4579      we will delete the feeding insn, which is incorrect.
4580
4581      So force this insn not to match in this (rare) case.  */
4582   if (! in_dest && code == REG && REG_P (from)
4583       && reg_overlap_mentioned_p (x, from))
4584     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
4585
4586   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4587      of which may contain things that can be combined.  */
4588   if (code != MEM && code != LO_SUM && OBJECT_P (x))
4589     return x;
4590
4591   /* It is possible to have a subexpression appear twice in the insn.
4592      Suppose that FROM is a register that appears within TO.
4593      Then, after that subexpression has been scanned once by `subst',
4594      the second time it is scanned, TO may be found.  If we were
4595      to scan TO here, we would find FROM within it and create a
4596      self-referent rtl structure which is completely wrong.  */
4597   if (COMBINE_RTX_EQUAL_P (x, to))
4598     return to;
4599
4600   /* Parallel asm_operands need special attention because all of the
4601      inputs are shared across the arms.  Furthermore, unsharing the
4602      rtl results in recognition failures.  Failure to handle this case
4603      specially can result in circular rtl.
4604
4605      Solve this by doing a normal pass across the first entry of the
4606      parallel, and only processing the SET_DESTs of the subsequent
4607      entries.  Ug.  */
4608
4609   if (code == PARALLEL
4610       && GET_CODE (XVECEXP (x, 0, 0)) == SET
4611       && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
4612     {
4613       new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
4614
4615       /* If this substitution failed, this whole thing fails.  */
4616       if (GET_CODE (new_rtx) == CLOBBER
4617           && XEXP (new_rtx, 0) == const0_rtx)
4618         return new_rtx;
4619
4620       SUBST (XVECEXP (x, 0, 0), new_rtx);
4621
4622       for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
4623         {
4624           rtx dest = SET_DEST (XVECEXP (x, 0, i));
4625
4626           if (!REG_P (dest)
4627               && GET_CODE (dest) != CC0
4628               && GET_CODE (dest) != PC)
4629             {
4630               new_rtx = subst (dest, from, to, 0, unique_copy);
4631
4632               /* If this substitution failed, this whole thing fails.  */
4633               if (GET_CODE (new_rtx) == CLOBBER
4634                   && XEXP (new_rtx, 0) == const0_rtx)
4635                 return new_rtx;
4636
4637               SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
4638             }
4639         }
4640     }
4641   else
4642     {
4643       len = GET_RTX_LENGTH (code);
4644       fmt = GET_RTX_FORMAT (code);
4645
4646       /* We don't need to process a SET_DEST that is a register, CC0,
4647          or PC, so set up to skip this common case.  All other cases
4648          where we want to suppress replacing something inside a
4649          SET_SRC are handled via the IN_DEST operand.  */
4650       if (code == SET
4651           && (REG_P (SET_DEST (x))
4652               || GET_CODE (SET_DEST (x)) == CC0
4653               || GET_CODE (SET_DEST (x)) == PC))
4654         fmt = "ie";
4655
4656       /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
4657          constant.  */
4658       if (fmt[0] == 'e')
4659         op0_mode = GET_MODE (XEXP (x, 0));
4660
4661       for (i = 0; i < len; i++)
4662         {
4663           if (fmt[i] == 'E')
4664             {
4665               int j;
4666               for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4667                 {
4668                   if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
4669                     {
4670                       new_rtx = (unique_copy && n_occurrences
4671                              ? copy_rtx (to) : to);
4672                       n_occurrences++;
4673                     }
4674                   else
4675                     {
4676                       new_rtx = subst (XVECEXP (x, i, j), from, to, 0,
4677                                    unique_copy);
4678
4679                       /* If this substitution failed, this whole thing
4680                          fails.  */
4681                       if (GET_CODE (new_rtx) == CLOBBER
4682                           && XEXP (new_rtx, 0) == const0_rtx)
4683                         return new_rtx;
4684                     }
4685
4686                   SUBST (XVECEXP (x, i, j), new_rtx);
4687                 }
4688             }
4689           else if (fmt[i] == 'e')
4690             {
4691               /* If this is a register being set, ignore it.  */
4692               new_rtx = XEXP (x, i);
4693               if (in_dest
4694                   && i == 0
4695                   && (((code == SUBREG || code == ZERO_EXTRACT)
4696                        && REG_P (new_rtx))
4697                       || code == STRICT_LOW_PART))
4698                 ;
4699
4700               else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
4701                 {
4702                   /* In general, don't install a subreg involving two
4703                      modes not tieable.  It can worsen register
4704                      allocation, and can even make invalid reload
4705                      insns, since the reg inside may need to be copied
4706                      from in the outside mode, and that may be invalid
4707                      if it is an fp reg copied in integer mode.
4708
4709                      We allow two exceptions to this: It is valid if
4710                      it is inside another SUBREG and the mode of that
4711                      SUBREG and the mode of the inside of TO is
4712                      tieable and it is valid if X is a SET that copies
4713                      FROM to CC0.  */
4714
4715                   if (GET_CODE (to) == SUBREG
4716                       && ! MODES_TIEABLE_P (GET_MODE (to),
4717                                             GET_MODE (SUBREG_REG (to)))
4718                       && ! (code == SUBREG
4719                             && MODES_TIEABLE_P (GET_MODE (x),
4720                                                 GET_MODE (SUBREG_REG (to))))
4721 #ifdef HAVE_cc0
4722                       && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
4723 #endif
4724                       )
4725                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
4726
4727 #ifdef CANNOT_CHANGE_MODE_CLASS
4728                   if (code == SUBREG
4729                       && REG_P (to)
4730                       && REGNO (to) < FIRST_PSEUDO_REGISTER
4731                       && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
4732                                                    GET_MODE (to),
4733                                                    GET_MODE (x)))
4734                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
4735 #endif
4736
4737                   new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
4738                   n_occurrences++;
4739                 }
4740               else
4741                 /* If we are in a SET_DEST, suppress most cases unless we
4742                    have gone inside a MEM, in which case we want to
4743                    simplify the address.  We assume here that things that
4744                    are actually part of the destination have their inner
4745                    parts in the first expression.  This is true for SUBREG,
4746                    STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
4747                    things aside from REG and MEM that should appear in a
4748                    SET_DEST.  */
4749                 new_rtx = subst (XEXP (x, i), from, to,
4750                              (((in_dest
4751                                 && (code == SUBREG || code == STRICT_LOW_PART
4752                                     || code == ZERO_EXTRACT))
4753                                || code == SET)
4754                               && i == 0), unique_copy);
4755
4756               /* If we found that we will have to reject this combination,
4757                  indicate that by returning the CLOBBER ourselves, rather than
4758                  an expression containing it.  This will speed things up as
4759                  well as prevent accidents where two CLOBBERs are considered
4760                  to be equal, thus producing an incorrect simplification.  */
4761
4762               if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
4763                 return new_rtx;
4764
4765               if (GET_CODE (x) == SUBREG
4766                   && (CONST_INT_P (new_rtx)
4767                       || GET_CODE (new_rtx) == CONST_DOUBLE))
4768                 {
4769                   enum machine_mode mode = GET_MODE (x);
4770
4771                   x = simplify_subreg (GET_MODE (x), new_rtx,
4772                                        GET_MODE (SUBREG_REG (x)),
4773                                        SUBREG_BYTE (x));
4774                   if (! x)
4775                     x = gen_rtx_CLOBBER (mode, const0_rtx);
4776                 }
4777               else if (CONST_INT_P (new_rtx)
4778                        && GET_CODE (x) == ZERO_EXTEND)
4779                 {
4780                   x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
4781                                                 new_rtx, GET_MODE (XEXP (x, 0)));
4782                   gcc_assert (x);
4783                 }
4784               else
4785                 SUBST (XEXP (x, i), new_rtx);
4786             }
4787         }
4788     }
4789
4790   /* Check if we are loading something from the constant pool via float
4791      extension; in this case we would undo compress_float_constant
4792      optimization and degenerate constant load to an immediate value.  */
4793   if (GET_CODE (x) == FLOAT_EXTEND
4794       && MEM_P (XEXP (x, 0))
4795       && MEM_READONLY_P (XEXP (x, 0)))
4796     {
4797       rtx tmp = avoid_constant_pool_reference (x);
4798       if (x != tmp)
4799         return x;
4800     }
4801
4802   /* Try to simplify X.  If the simplification changed the code, it is likely
4803      that further simplification will help, so loop, but limit the number
4804      of repetitions that will be performed.  */
4805
4806   for (i = 0; i < 4; i++)
4807     {
4808       /* If X is sufficiently simple, don't bother trying to do anything
4809          with it.  */
4810       if (code != CONST_INT && code != REG && code != CLOBBER)
4811         x = combine_simplify_rtx (x, op0_mode, in_dest);
4812
4813       if (GET_CODE (x) == code)
4814         break;
4815
4816       code = GET_CODE (x);
4817
4818       /* We no longer know the original mode of operand 0 since we
4819          have changed the form of X)  */
4820       op0_mode = VOIDmode;
4821     }
4822
4823   return x;
4824 }
4825 \f
4826 /* Simplify X, a piece of RTL.  We just operate on the expression at the
4827    outer level; call `subst' to simplify recursively.  Return the new
4828    expression.
4829
4830    OP0_MODE is the original mode of XEXP (x, 0).  IN_DEST is nonzero
4831    if we are inside a SET_DEST.  */
4832
4833 static rtx
4834 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
4835 {
4836   enum rtx_code code = GET_CODE (x);
4837   enum machine_mode mode = GET_MODE (x);
4838   rtx temp;
4839   int i;
4840
4841   /* If this is a commutative operation, put a constant last and a complex
4842      expression first.  We don't need to do this for comparisons here.  */
4843   if (COMMUTATIVE_ARITH_P (x)
4844       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
4845     {
4846       temp = XEXP (x, 0);
4847       SUBST (XEXP (x, 0), XEXP (x, 1));
4848       SUBST (XEXP (x, 1), temp);
4849     }
4850
4851   /* If this is a simple operation applied to an IF_THEN_ELSE, try
4852      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
4853      things.  Check for cases where both arms are testing the same
4854      condition.
4855
4856      Don't do anything if all operands are very simple.  */
4857
4858   if ((BINARY_P (x)
4859        && ((!OBJECT_P (XEXP (x, 0))
4860             && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4861                   && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
4862            || (!OBJECT_P (XEXP (x, 1))
4863                && ! (GET_CODE (XEXP (x, 1)) == SUBREG
4864                      && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
4865       || (UNARY_P (x)
4866           && (!OBJECT_P (XEXP (x, 0))
4867                && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4868                      && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
4869     {
4870       rtx cond, true_rtx, false_rtx;
4871
4872       cond = if_then_else_cond (x, &true_rtx, &false_rtx);
4873       if (cond != 0
4874           /* If everything is a comparison, what we have is highly unlikely
4875              to be simpler, so don't use it.  */
4876           && ! (COMPARISON_P (x)
4877                 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
4878         {
4879           rtx cop1 = const0_rtx;
4880           enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
4881
4882           if (cond_code == NE && COMPARISON_P (cond))
4883             return x;
4884
4885           /* Simplify the alternative arms; this may collapse the true and
4886              false arms to store-flag values.  Be careful to use copy_rtx
4887              here since true_rtx or false_rtx might share RTL with x as a
4888              result of the if_then_else_cond call above.  */
4889           true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
4890           false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
4891
4892           /* If true_rtx and false_rtx are not general_operands, an if_then_else
4893              is unlikely to be simpler.  */
4894           if (general_operand (true_rtx, VOIDmode)
4895               && general_operand (false_rtx, VOIDmode))
4896             {
4897               enum rtx_code reversed;
4898
4899               /* Restarting if we generate a store-flag expression will cause
4900                  us to loop.  Just drop through in this case.  */
4901
4902               /* If the result values are STORE_FLAG_VALUE and zero, we can
4903                  just make the comparison operation.  */
4904               if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
4905                 x = simplify_gen_relational (cond_code, mode, VOIDmode,
4906                                              cond, cop1);
4907               else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
4908                        && ((reversed = reversed_comparison_code_parts
4909                                         (cond_code, cond, cop1, NULL))
4910                            != UNKNOWN))
4911                 x = simplify_gen_relational (reversed, mode, VOIDmode,
4912                                              cond, cop1);
4913
4914               /* Likewise, we can make the negate of a comparison operation
4915                  if the result values are - STORE_FLAG_VALUE and zero.  */
4916               else if (CONST_INT_P (true_rtx)
4917                        && INTVAL (true_rtx) == - STORE_FLAG_VALUE
4918                        && false_rtx == const0_rtx)
4919                 x = simplify_gen_unary (NEG, mode,
4920                                         simplify_gen_relational (cond_code,
4921                                                                  mode, VOIDmode,
4922                                                                  cond, cop1),
4923                                         mode);
4924               else if (CONST_INT_P (false_rtx)
4925                        && INTVAL (false_rtx) == - STORE_FLAG_VALUE
4926                        && true_rtx == const0_rtx
4927                        && ((reversed = reversed_comparison_code_parts
4928                                         (cond_code, cond, cop1, NULL))
4929                            != UNKNOWN))
4930                 x = simplify_gen_unary (NEG, mode,
4931                                         simplify_gen_relational (reversed,
4932                                                                  mode, VOIDmode,
4933                                                                  cond, cop1),
4934                                         mode);
4935               else
4936                 return gen_rtx_IF_THEN_ELSE (mode,
4937                                              simplify_gen_relational (cond_code,
4938                                                                       mode,
4939                                                                       VOIDmode,
4940                                                                       cond,
4941                                                                       cop1),
4942                                              true_rtx, false_rtx);
4943
4944               code = GET_CODE (x);
4945               op0_mode = VOIDmode;
4946             }
4947         }
4948     }
4949
4950   /* Try to fold this expression in case we have constants that weren't
4951      present before.  */
4952   temp = 0;
4953   switch (GET_RTX_CLASS (code))
4954     {
4955     case RTX_UNARY:
4956       if (op0_mode == VOIDmode)
4957         op0_mode = GET_MODE (XEXP (x, 0));
4958       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
4959       break;
4960     case RTX_COMPARE:
4961     case RTX_COMM_COMPARE:
4962       {
4963         enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
4964         if (cmp_mode == VOIDmode)
4965           {
4966             cmp_mode = GET_MODE (XEXP (x, 1));
4967             if (cmp_mode == VOIDmode)
4968               cmp_mode = op0_mode;
4969           }
4970         temp = simplify_relational_operation (code, mode, cmp_mode,
4971                                               XEXP (x, 0), XEXP (x, 1));
4972       }
4973       break;
4974     case RTX_COMM_ARITH:
4975     case RTX_BIN_ARITH:
4976       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
4977       break;
4978     case RTX_BITFIELD_OPS:
4979     case RTX_TERNARY:
4980       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
4981                                          XEXP (x, 1), XEXP (x, 2));
4982       break;
4983     default:
4984       break;
4985     }
4986
4987   if (temp)
4988     {
4989       x = temp;
4990       code = GET_CODE (temp);
4991       op0_mode = VOIDmode;
4992       mode = GET_MODE (temp);
4993     }
4994
4995   /* First see if we can apply the inverse distributive law.  */
4996   if (code == PLUS || code == MINUS
4997       || code == AND || code == IOR || code == XOR)
4998     {
4999       x = apply_distributive_law (x);
5000       code = GET_CODE (x);
5001       op0_mode = VOIDmode;
5002     }
5003
5004   /* If CODE is an associative operation not otherwise handled, see if we
5005      can associate some operands.  This can win if they are constants or
5006      if they are logically related (i.e. (a & b) & a).  */
5007   if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5008        || code == AND || code == IOR || code == XOR
5009        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5010       && ((INTEGRAL_MODE_P (mode) && code != DIV)
5011           || (flag_associative_math && FLOAT_MODE_P (mode))))
5012     {
5013       if (GET_CODE (XEXP (x, 0)) == code)
5014         {
5015           rtx other = XEXP (XEXP (x, 0), 0);
5016           rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5017           rtx inner_op1 = XEXP (x, 1);
5018           rtx inner;
5019
5020           /* Make sure we pass the constant operand if any as the second
5021              one if this is a commutative operation.  */
5022           if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5023             {
5024               rtx tem = inner_op0;
5025               inner_op0 = inner_op1;
5026               inner_op1 = tem;
5027             }
5028           inner = simplify_binary_operation (code == MINUS ? PLUS
5029                                              : code == DIV ? MULT
5030                                              : code,
5031                                              mode, inner_op0, inner_op1);
5032
5033           /* For commutative operations, try the other pair if that one
5034              didn't simplify.  */
5035           if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5036             {
5037               other = XEXP (XEXP (x, 0), 1);
5038               inner = simplify_binary_operation (code, mode,
5039                                                  XEXP (XEXP (x, 0), 0),
5040                                                  XEXP (x, 1));
5041             }
5042
5043           if (inner)
5044             return simplify_gen_binary (code, mode, other, inner);
5045         }
5046     }
5047
5048   /* A little bit of algebraic simplification here.  */
5049   switch (code)
5050     {
5051     case MEM:
5052       /* Ensure that our address has any ASHIFTs converted to MULT in case
5053          address-recognizing predicates are called later.  */
5054       temp = make_compound_operation (XEXP (x, 0), MEM);
5055       SUBST (XEXP (x, 0), temp);
5056       break;
5057
5058     case SUBREG:
5059       if (op0_mode == VOIDmode)
5060         op0_mode = GET_MODE (SUBREG_REG (x));
5061
5062       /* See if this can be moved to simplify_subreg.  */
5063       if (CONSTANT_P (SUBREG_REG (x))
5064           && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5065              /* Don't call gen_lowpart if the inner mode
5066                 is VOIDmode and we cannot simplify it, as SUBREG without
5067                 inner mode is invalid.  */
5068           && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5069               || gen_lowpart_common (mode, SUBREG_REG (x))))
5070         return gen_lowpart (mode, SUBREG_REG (x));
5071
5072       if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5073         break;
5074       {
5075         rtx temp;
5076         temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5077                                 SUBREG_BYTE (x));
5078         if (temp)
5079           return temp;
5080       }
5081
5082       /* Don't change the mode of the MEM if that would change the meaning
5083          of the address.  */
5084       if (MEM_P (SUBREG_REG (x))
5085           && (MEM_VOLATILE_P (SUBREG_REG (x))
5086               || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
5087         return gen_rtx_CLOBBER (mode, const0_rtx);
5088
5089       /* Note that we cannot do any narrowing for non-constants since
5090          we might have been counting on using the fact that some bits were
5091          zero.  We now do this in the SET.  */
5092
5093       break;
5094
5095     case NEG:
5096       temp = expand_compound_operation (XEXP (x, 0));
5097
5098       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5099          replaced by (lshiftrt X C).  This will convert
5100          (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
5101
5102       if (GET_CODE (temp) == ASHIFTRT
5103           && CONST_INT_P (XEXP (temp, 1))
5104           && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
5105         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5106                                      INTVAL (XEXP (temp, 1)));
5107
5108       /* If X has only a single bit that might be nonzero, say, bit I, convert
5109          (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5110          MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
5111          (sign_extract X 1 Y).  But only do this if TEMP isn't a register
5112          or a SUBREG of one since we'd be making the expression more
5113          complex if it was just a register.  */
5114
5115       if (!REG_P (temp)
5116           && ! (GET_CODE (temp) == SUBREG
5117                 && REG_P (SUBREG_REG (temp)))
5118           && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5119         {
5120           rtx temp1 = simplify_shift_const
5121             (NULL_RTX, ASHIFTRT, mode,
5122              simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5123                                    GET_MODE_BITSIZE (mode) - 1 - i),
5124              GET_MODE_BITSIZE (mode) - 1 - i);
5125
5126           /* If all we did was surround TEMP with the two shifts, we
5127              haven't improved anything, so don't use it.  Otherwise,
5128              we are better off with TEMP1.  */
5129           if (GET_CODE (temp1) != ASHIFTRT
5130               || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5131               || XEXP (XEXP (temp1, 0), 0) != temp)
5132             return temp1;
5133         }
5134       break;
5135
5136     case TRUNCATE:
5137       /* We can't handle truncation to a partial integer mode here
5138          because we don't know the real bitsize of the partial
5139          integer mode.  */
5140       if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5141         break;
5142
5143       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5144         SUBST (XEXP (x, 0),
5145                force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5146                               GET_MODE_MASK (mode), 0));
5147
5148       /* We can truncate a constant value and return it.  */
5149       if (CONST_INT_P (XEXP (x, 0)))
5150         return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5151
5152       /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5153          whose value is a comparison can be replaced with a subreg if
5154          STORE_FLAG_VALUE permits.  */
5155       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5156           && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5157           && (temp = get_last_value (XEXP (x, 0)))
5158           && COMPARISON_P (temp))
5159         return gen_lowpart (mode, XEXP (x, 0));
5160       break;
5161
5162     case CONST:
5163       /* (const (const X)) can become (const X).  Do it this way rather than
5164          returning the inner CONST since CONST can be shared with a
5165          REG_EQUAL note.  */
5166       if (GET_CODE (XEXP (x, 0)) == CONST)
5167         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5168       break;
5169
5170 #ifdef HAVE_lo_sum
5171     case LO_SUM:
5172       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
5173          can add in an offset.  find_split_point will split this address up
5174          again if it doesn't match.  */
5175       if (GET_CODE (XEXP (x, 0)) == HIGH
5176           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5177         return XEXP (x, 1);
5178       break;
5179 #endif
5180
5181     case PLUS:
5182       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5183          when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5184          bit-field and can be replaced by either a sign_extend or a
5185          sign_extract.  The `and' may be a zero_extend and the two
5186          <c>, -<c> constants may be reversed.  */
5187       if (GET_CODE (XEXP (x, 0)) == XOR
5188           && CONST_INT_P (XEXP (x, 1))
5189           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5190           && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5191           && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5192               || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
5193           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5194           && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5195                && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5196                && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5197                    == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
5198               || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5199                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5200                       == (unsigned int) i + 1))))
5201         return simplify_shift_const
5202           (NULL_RTX, ASHIFTRT, mode,
5203            simplify_shift_const (NULL_RTX, ASHIFT, mode,
5204                                  XEXP (XEXP (XEXP (x, 0), 0), 0),
5205                                  GET_MODE_BITSIZE (mode) - (i + 1)),
5206            GET_MODE_BITSIZE (mode) - (i + 1));
5207
5208       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5209          can become (ashiftrt (ashift (xor x 1) C) C) where C is
5210          the bitsize of the mode - 1.  This allows simplification of
5211          "a = (b & 8) == 0;"  */
5212       if (XEXP (x, 1) == constm1_rtx
5213           && !REG_P (XEXP (x, 0))
5214           && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5215                 && REG_P (SUBREG_REG (XEXP (x, 0))))
5216           && nonzero_bits (XEXP (x, 0), mode) == 1)
5217         return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5218            simplify_shift_const (NULL_RTX, ASHIFT, mode,
5219                                  gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5220                                  GET_MODE_BITSIZE (mode) - 1),
5221            GET_MODE_BITSIZE (mode) - 1);
5222
5223       /* If we are adding two things that have no bits in common, convert
5224          the addition into an IOR.  This will often be further simplified,
5225          for example in cases like ((a & 1) + (a & 2)), which can
5226          become a & 3.  */
5227
5228       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5229           && (nonzero_bits (XEXP (x, 0), mode)
5230               & nonzero_bits (XEXP (x, 1), mode)) == 0)
5231         {
5232           /* Try to simplify the expression further.  */
5233           rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5234           temp = combine_simplify_rtx (tor, mode, in_dest);
5235
5236           /* If we could, great.  If not, do not go ahead with the IOR
5237              replacement, since PLUS appears in many special purpose
5238              address arithmetic instructions.  */
5239           if (GET_CODE (temp) != CLOBBER && temp != tor)
5240             return temp;
5241         }
5242       break;
5243
5244     case MINUS:
5245       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5246          (and <foo> (const_int pow2-1))  */
5247       if (GET_CODE (XEXP (x, 1)) == AND
5248           && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5249           && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5250           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5251         return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5252                                        -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5253       break;
5254
5255     case MULT:
5256       /* If we have (mult (plus A B) C), apply the distributive law and then
5257          the inverse distributive law to see if things simplify.  This
5258          occurs mostly in addresses, often when unrolling loops.  */
5259
5260       if (GET_CODE (XEXP (x, 0)) == PLUS)
5261         {
5262           rtx result = distribute_and_simplify_rtx (x, 0);
5263           if (result)
5264             return result;
5265         }
5266
5267       /* Try simplify a*(b/c) as (a*b)/c.  */
5268       if (FLOAT_MODE_P (mode) && flag_associative_math
5269           && GET_CODE (XEXP (x, 0)) == DIV)
5270         {
5271           rtx tem = simplify_binary_operation (MULT, mode,
5272                                                XEXP (XEXP (x, 0), 0),
5273                                                XEXP (x, 1));
5274           if (tem)
5275             return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5276         }
5277       break;
5278
5279     case UDIV:
5280       /* If this is a divide by a power of two, treat it as a shift if
5281          its first operand is a shift.  */
5282       if (CONST_INT_P (XEXP (x, 1))
5283           && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
5284           && (GET_CODE (XEXP (x, 0)) == ASHIFT
5285               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5286               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5287               || GET_CODE (XEXP (x, 0)) == ROTATE
5288               || GET_CODE (XEXP (x, 0)) == ROTATERT))
5289         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5290       break;
5291
5292     case EQ:  case NE:
5293     case GT:  case GTU:  case GE:  case GEU:
5294     case LT:  case LTU:  case LE:  case LEU:
5295     case UNEQ:  case LTGT:
5296     case UNGT:  case UNGE:
5297     case UNLT:  case UNLE:
5298     case UNORDERED: case ORDERED:
5299       /* If the first operand is a condition code, we can't do anything
5300          with it.  */
5301       if (GET_CODE (XEXP (x, 0)) == COMPARE
5302           || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5303               && ! CC0_P (XEXP (x, 0))))
5304         {
5305           rtx op0 = XEXP (x, 0);
5306           rtx op1 = XEXP (x, 1);
5307           enum rtx_code new_code;
5308
5309           if (GET_CODE (op0) == COMPARE)
5310             op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5311
5312           /* Simplify our comparison, if possible.  */
5313           new_code = simplify_comparison (code, &op0, &op1);
5314
5315           /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5316              if only the low-order bit is possibly nonzero in X (such as when
5317              X is a ZERO_EXTRACT of one bit).  Similarly, we can convert EQ to
5318              (xor X 1) or (minus 1 X); we use the former.  Finally, if X is
5319              known to be either 0 or -1, NE becomes a NEG and EQ becomes
5320              (plus X 1).
5321
5322              Remove any ZERO_EXTRACT we made when thinking this was a
5323              comparison.  It may now be simpler to use, e.g., an AND.  If a
5324              ZERO_EXTRACT is indeed appropriate, it will be placed back by
5325              the call to make_compound_operation in the SET case.  */
5326
5327           if (STORE_FLAG_VALUE == 1
5328               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5329               && op1 == const0_rtx
5330               && mode == GET_MODE (op0)
5331               && nonzero_bits (op0, mode) == 1)
5332             return gen_lowpart (mode,
5333                                 expand_compound_operation (op0));
5334
5335           else if (STORE_FLAG_VALUE == 1
5336                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5337                    && op1 == const0_rtx
5338                    && mode == GET_MODE (op0)
5339                    && (num_sign_bit_copies (op0, mode)
5340                        == GET_MODE_BITSIZE (mode)))
5341             {
5342               op0 = expand_compound_operation (op0);
5343               return simplify_gen_unary (NEG, mode,
5344                                          gen_lowpart (mode, op0),
5345                                          mode);
5346             }
5347
5348           else if (STORE_FLAG_VALUE == 1
5349                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5350                    && op1 == const0_rtx
5351                    && mode == GET_MODE (op0)
5352                    && nonzero_bits (op0, mode) == 1)
5353             {
5354               op0 = expand_compound_operation (op0);
5355               return simplify_gen_binary (XOR, mode,
5356                                           gen_lowpart (mode, op0),
5357                                           const1_rtx);
5358             }
5359
5360           else if (STORE_FLAG_VALUE == 1
5361                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5362                    && op1 == const0_rtx
5363                    && mode == GET_MODE (op0)
5364                    && (num_sign_bit_copies (op0, mode)
5365                        == GET_MODE_BITSIZE (mode)))
5366             {
5367               op0 = expand_compound_operation (op0);
5368               return plus_constant (gen_lowpart (mode, op0), 1);
5369             }
5370
5371           /* If STORE_FLAG_VALUE is -1, we have cases similar to
5372              those above.  */
5373           if (STORE_FLAG_VALUE == -1
5374               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5375               && op1 == const0_rtx
5376               && (num_sign_bit_copies (op0, mode)
5377                   == GET_MODE_BITSIZE (mode)))
5378             return gen_lowpart (mode,
5379                                 expand_compound_operation (op0));
5380
5381           else if (STORE_FLAG_VALUE == -1
5382                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5383                    && op1 == const0_rtx
5384                    && mode == GET_MODE (op0)
5385                    && nonzero_bits (op0, mode) == 1)
5386             {
5387               op0 = expand_compound_operation (op0);
5388               return simplify_gen_unary (NEG, mode,
5389                                          gen_lowpart (mode, op0),
5390                                          mode);
5391             }
5392
5393           else if (STORE_FLAG_VALUE == -1
5394                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5395                    && op1 == const0_rtx
5396                    && mode == GET_MODE (op0)
5397                    && (num_sign_bit_copies (op0, mode)
5398                        == GET_MODE_BITSIZE (mode)))
5399             {
5400               op0 = expand_compound_operation (op0);
5401               return simplify_gen_unary (NOT, mode,
5402                                          gen_lowpart (mode, op0),
5403                                          mode);
5404             }
5405
5406           /* If X is 0/1, (eq X 0) is X-1.  */
5407           else if (STORE_FLAG_VALUE == -1
5408                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5409                    && op1 == const0_rtx
5410                    && mode == GET_MODE (op0)
5411                    && nonzero_bits (op0, mode) == 1)
5412             {
5413               op0 = expand_compound_operation (op0);
5414               return plus_constant (gen_lowpart (mode, op0), -1);
5415             }
5416
5417           /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5418              one bit that might be nonzero, we can convert (ne x 0) to
5419              (ashift x c) where C puts the bit in the sign bit.  Remove any
5420              AND with STORE_FLAG_VALUE when we are done, since we are only
5421              going to test the sign bit.  */
5422           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5423               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5424               && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5425                   == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5426               && op1 == const0_rtx
5427               && mode == GET_MODE (op0)
5428               && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5429             {
5430               x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5431                                         expand_compound_operation (op0),
5432                                         GET_MODE_BITSIZE (mode) - 1 - i);
5433               if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5434                 return XEXP (x, 0);
5435               else
5436                 return x;
5437             }
5438
5439           /* If the code changed, return a whole new comparison.  */
5440           if (new_code != code)
5441             return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5442
5443           /* Otherwise, keep this operation, but maybe change its operands.
5444              This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
5445           SUBST (XEXP (x, 0), op0);
5446           SUBST (XEXP (x, 1), op1);
5447         }
5448       break;
5449
5450     case IF_THEN_ELSE:
5451       return simplify_if_then_else (x);
5452
5453     case ZERO_EXTRACT:
5454     case SIGN_EXTRACT:
5455     case ZERO_EXTEND:
5456     case SIGN_EXTEND:
5457       /* If we are processing SET_DEST, we are done.  */
5458       if (in_dest)
5459         return x;
5460
5461       return expand_compound_operation (x);
5462
5463     case SET:
5464       return simplify_set (x);
5465
5466     case AND:
5467     case IOR:
5468       return simplify_logical (x);
5469
5470     case ASHIFT:
5471     case LSHIFTRT:
5472     case ASHIFTRT:
5473     case ROTATE:
5474     case ROTATERT:
5475       /* If this is a shift by a constant amount, simplify it.  */
5476       if (CONST_INT_P (XEXP (x, 1)))
5477         return simplify_shift_const (x, code, mode, XEXP (x, 0),
5478                                      INTVAL (XEXP (x, 1)));
5479
5480       else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5481         SUBST (XEXP (x, 1),
5482                force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5483                               ((HOST_WIDE_INT) 1
5484                                << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5485                               - 1,
5486                               0));
5487       break;
5488
5489     default:
5490       break;
5491     }
5492
5493   return x;
5494 }
5495 \f
5496 /* Simplify X, an IF_THEN_ELSE expression.  Return the new expression.  */
5497
5498 static rtx
5499 simplify_if_then_else (rtx x)
5500 {
5501   enum machine_mode mode = GET_MODE (x);
5502   rtx cond = XEXP (x, 0);
5503   rtx true_rtx = XEXP (x, 1);
5504   rtx false_rtx = XEXP (x, 2);
5505   enum rtx_code true_code = GET_CODE (cond);
5506   int comparison_p = COMPARISON_P (cond);
5507   rtx temp;
5508   int i;
5509   enum rtx_code false_code;
5510   rtx reversed;
5511
5512   /* Simplify storing of the truth value.  */
5513   if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5514     return simplify_gen_relational (true_code, mode, VOIDmode,
5515                                     XEXP (cond, 0), XEXP (cond, 1));
5516
5517   /* Also when the truth value has to be reversed.  */
5518   if (comparison_p
5519       && true_rtx == const0_rtx && false_rtx == const_true_rtx
5520       && (reversed = reversed_comparison (cond, mode)))
5521     return reversed;
5522
5523   /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5524      in it is being compared against certain values.  Get the true and false
5525      comparisons and see if that says anything about the value of each arm.  */
5526
5527   if (comparison_p
5528       && ((false_code = reversed_comparison_code (cond, NULL))
5529           != UNKNOWN)
5530       && REG_P (XEXP (cond, 0)))
5531     {
5532       HOST_WIDE_INT nzb;
5533       rtx from = XEXP (cond, 0);
5534       rtx true_val = XEXP (cond, 1);
5535       rtx false_val = true_val;
5536       int swapped = 0;
5537
5538       /* If FALSE_CODE is EQ, swap the codes and arms.  */
5539
5540       if (false_code == EQ)
5541         {
5542           swapped = 1, true_code = EQ, false_code = NE;
5543           temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5544         }
5545
5546       /* If we are comparing against zero and the expression being tested has
5547          only a single bit that might be nonzero, that is its value when it is
5548          not equal to zero.  Similarly if it is known to be -1 or 0.  */
5549
5550       if (true_code == EQ && true_val == const0_rtx
5551           && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5552         {
5553           false_code = EQ;
5554           false_val = GEN_INT (trunc_int_for_mode (nzb, GET_MODE (from)));
5555         }
5556       else if (true_code == EQ && true_val == const0_rtx
5557                && (num_sign_bit_copies (from, GET_MODE (from))
5558                    == GET_MODE_BITSIZE (GET_MODE (from))))
5559         {
5560           false_code = EQ;
5561           false_val = constm1_rtx;
5562         }
5563
5564       /* Now simplify an arm if we know the value of the register in the
5565          branch and it is used in the arm.  Be careful due to the potential
5566          of locally-shared RTL.  */
5567
5568       if (reg_mentioned_p (from, true_rtx))
5569         true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
5570                                       from, true_val),
5571                       pc_rtx, pc_rtx, 0, 0);
5572       if (reg_mentioned_p (from, false_rtx))
5573         false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
5574                                    from, false_val),
5575                        pc_rtx, pc_rtx, 0, 0);
5576
5577       SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
5578       SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
5579
5580       true_rtx = XEXP (x, 1);
5581       false_rtx = XEXP (x, 2);
5582       true_code = GET_CODE (cond);
5583     }
5584
5585   /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
5586      reversed, do so to avoid needing two sets of patterns for
5587      subtract-and-branch insns.  Similarly if we have a constant in the true
5588      arm, the false arm is the same as the first operand of the comparison, or
5589      the false arm is more complicated than the true arm.  */
5590
5591   if (comparison_p
5592       && reversed_comparison_code (cond, NULL) != UNKNOWN
5593       && (true_rtx == pc_rtx
5594           || (CONSTANT_P (true_rtx)
5595               && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
5596           || true_rtx == const0_rtx
5597           || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
5598           || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
5599               && !OBJECT_P (false_rtx))
5600           || reg_mentioned_p (true_rtx, false_rtx)
5601           || rtx_equal_p (false_rtx, XEXP (cond, 0))))
5602     {
5603       true_code = reversed_comparison_code (cond, NULL);
5604       SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
5605       SUBST (XEXP (x, 1), false_rtx);
5606       SUBST (XEXP (x, 2), true_rtx);
5607
5608       temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5609       cond = XEXP (x, 0);
5610
5611       /* It is possible that the conditional has been simplified out.  */
5612       true_code = GET_CODE (cond);
5613       comparison_p = COMPARISON_P (cond);
5614     }
5615
5616   /* If the two arms are identical, we don't need the comparison.  */
5617
5618   if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
5619     return true_rtx;
5620
5621   /* Convert a == b ? b : a to "a".  */
5622   if (true_code == EQ && ! side_effects_p (cond)
5623       && !HONOR_NANS (mode)
5624       && rtx_equal_p (XEXP (cond, 0), false_rtx)
5625       && rtx_equal_p (XEXP (cond, 1), true_rtx))
5626     return false_rtx;
5627   else if (true_code == NE && ! side_effects_p (cond)
5628            && !HONOR_NANS (mode)
5629            && rtx_equal_p (XEXP (cond, 0), true_rtx)
5630            && rtx_equal_p (XEXP (cond, 1), false_rtx))
5631     return true_rtx;
5632
5633   /* Look for cases where we have (abs x) or (neg (abs X)).  */
5634
5635   if (GET_MODE_CLASS (mode) == MODE_INT
5636       && comparison_p
5637       && XEXP (cond, 1) == const0_rtx
5638       && GET_CODE (false_rtx) == NEG
5639       && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
5640       && rtx_equal_p (true_rtx, XEXP (cond, 0))
5641       && ! side_effects_p (true_rtx))
5642     switch (true_code)
5643       {
5644       case GT:
5645       case GE:
5646         return simplify_gen_unary (ABS, mode, true_rtx, mode);
5647       case LT:
5648       case LE:
5649         return
5650           simplify_gen_unary (NEG, mode,
5651                               simplify_gen_unary (ABS, mode, true_rtx, mode),
5652                               mode);
5653       default:
5654         break;
5655       }
5656
5657   /* Look for MIN or MAX.  */
5658
5659   if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
5660       && comparison_p
5661       && rtx_equal_p (XEXP (cond, 0), true_rtx)
5662       && rtx_equal_p (XEXP (cond, 1), false_rtx)
5663       && ! side_effects_p (cond))
5664     switch (true_code)
5665       {
5666       case GE:
5667       case GT:
5668         return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
5669       case LE:
5670       case LT:
5671         return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
5672       case GEU:
5673       case GTU:
5674         return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
5675       case LEU:
5676       case LTU:
5677         return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
5678       default:
5679         break;
5680       }
5681
5682   /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
5683      second operand is zero, this can be done as (OP Z (mult COND C2)) where
5684      C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
5685      SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
5686      We can do this kind of thing in some cases when STORE_FLAG_VALUE is
5687      neither 1 or -1, but it isn't worth checking for.  */
5688
5689   if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5690       && comparison_p
5691       && GET_MODE_CLASS (mode) == MODE_INT
5692       && ! side_effects_p (x))
5693     {
5694       rtx t = make_compound_operation (true_rtx, SET);
5695       rtx f = make_compound_operation (false_rtx, SET);
5696       rtx cond_op0 = XEXP (cond, 0);
5697       rtx cond_op1 = XEXP (cond, 1);
5698       enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
5699       enum machine_mode m = mode;
5700       rtx z = 0, c1 = NULL_RTX;
5701
5702       if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
5703            || GET_CODE (t) == IOR || GET_CODE (t) == XOR
5704            || GET_CODE (t) == ASHIFT
5705            || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
5706           && rtx_equal_p (XEXP (t, 0), f))
5707         c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
5708
5709       /* If an identity-zero op is commutative, check whether there
5710          would be a match if we swapped the operands.  */
5711       else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
5712                 || GET_CODE (t) == XOR)
5713                && rtx_equal_p (XEXP (t, 1), f))
5714         c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
5715       else if (GET_CODE (t) == SIGN_EXTEND
5716                && (GET_CODE (XEXP (t, 0)) == PLUS
5717                    || GET_CODE (XEXP (t, 0)) == MINUS
5718                    || GET_CODE (XEXP (t, 0)) == IOR
5719                    || GET_CODE (XEXP (t, 0)) == XOR
5720                    || GET_CODE (XEXP (t, 0)) == ASHIFT
5721                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5722                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5723                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5724                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5725                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5726                && (num_sign_bit_copies (f, GET_MODE (f))
5727                    > (unsigned int)
5728                      (GET_MODE_BITSIZE (mode)
5729                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
5730         {
5731           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5732           extend_op = SIGN_EXTEND;
5733           m = GET_MODE (XEXP (t, 0));
5734         }
5735       else if (GET_CODE (t) == SIGN_EXTEND
5736                && (GET_CODE (XEXP (t, 0)) == PLUS
5737                    || GET_CODE (XEXP (t, 0)) == IOR
5738                    || GET_CODE (XEXP (t, 0)) == XOR)
5739                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5740                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5741                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5742                && (num_sign_bit_copies (f, GET_MODE (f))
5743                    > (unsigned int)
5744                      (GET_MODE_BITSIZE (mode)
5745                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
5746         {
5747           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5748           extend_op = SIGN_EXTEND;
5749           m = GET_MODE (XEXP (t, 0));
5750         }
5751       else if (GET_CODE (t) == ZERO_EXTEND
5752                && (GET_CODE (XEXP (t, 0)) == PLUS
5753                    || GET_CODE (XEXP (t, 0)) == MINUS
5754                    || GET_CODE (XEXP (t, 0)) == IOR
5755                    || GET_CODE (XEXP (t, 0)) == XOR
5756                    || GET_CODE (XEXP (t, 0)) == ASHIFT
5757                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5758                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5759                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5760                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5761                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5762                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5763                && ((nonzero_bits (f, GET_MODE (f))
5764                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
5765                    == 0))
5766         {
5767           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5768           extend_op = ZERO_EXTEND;
5769           m = GET_MODE (XEXP (t, 0));
5770         }
5771       else if (GET_CODE (t) == ZERO_EXTEND
5772                && (GET_CODE (XEXP (t, 0)) == PLUS
5773                    || GET_CODE (XEXP (t, 0)) == IOR
5774                    || GET_CODE (XEXP (t, 0)) == XOR)
5775                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5776                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5777                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5778                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5779                && ((nonzero_bits (f, GET_MODE (f))
5780                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
5781                    == 0))
5782         {
5783           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5784           extend_op = ZERO_EXTEND;
5785           m = GET_MODE (XEXP (t, 0));
5786         }
5787
5788       if (z)
5789         {
5790           temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
5791                                                  cond_op0, cond_op1),
5792                         pc_rtx, pc_rtx, 0, 0);
5793           temp = simplify_gen_binary (MULT, m, temp,
5794                                       simplify_gen_binary (MULT, m, c1,
5795                                                            const_true_rtx));
5796           temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
5797           temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
5798
5799           if (extend_op != UNKNOWN)
5800             temp = simplify_gen_unary (extend_op, mode, temp, m);
5801
5802           return temp;
5803         }
5804     }
5805
5806   /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
5807      1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
5808      negation of a single bit, we can convert this operation to a shift.  We
5809      can actually do this more generally, but it doesn't seem worth it.  */
5810
5811   if (true_code == NE && XEXP (cond, 1) == const0_rtx
5812       && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
5813       && ((1 == nonzero_bits (XEXP (cond, 0), mode)
5814            && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
5815           || ((num_sign_bit_copies (XEXP (cond, 0), mode)
5816                == GET_MODE_BITSIZE (mode))
5817               && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
5818     return
5819       simplify_shift_const (NULL_RTX, ASHIFT, mode,
5820                             gen_lowpart (mode, XEXP (cond, 0)), i);
5821
5822   /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8.  */
5823   if (true_code == NE && XEXP (cond, 1) == const0_rtx
5824       && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
5825       && GET_MODE (XEXP (cond, 0)) == mode
5826       && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
5827           == nonzero_bits (XEXP (cond, 0), mode)
5828       && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
5829     return XEXP (cond, 0);
5830
5831   return x;
5832 }
5833 \f
5834 /* Simplify X, a SET expression.  Return the new expression.  */
5835
5836 static rtx
5837 simplify_set (rtx x)
5838 {
5839   rtx src = SET_SRC (x);
5840   rtx dest = SET_DEST (x);
5841   enum machine_mode mode
5842     = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
5843   rtx other_insn;
5844   rtx *cc_use;
5845
5846   /* (set (pc) (return)) gets written as (return).  */
5847   if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
5848     return src;
5849
5850   /* Now that we know for sure which bits of SRC we are using, see if we can
5851      simplify the expression for the object knowing that we only need the
5852      low-order bits.  */
5853
5854   if (GET_MODE_CLASS (mode) == MODE_INT
5855       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5856     {
5857       src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, 0);
5858       SUBST (SET_SRC (x), src);
5859     }
5860
5861   /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5862      the comparison result and try to simplify it unless we already have used
5863      undobuf.other_insn.  */
5864   if ((GET_MODE_CLASS (mode) == MODE_CC
5865        || GET_CODE (src) == COMPARE
5866        || CC0_P (dest))
5867       && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5868       && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5869       && COMPARISON_P (*cc_use)
5870       && rtx_equal_p (XEXP (*cc_use, 0), dest))
5871     {
5872       enum rtx_code old_code = GET_CODE (*cc_use);
5873       enum rtx_code new_code;
5874       rtx op0, op1, tmp;
5875       int other_changed = 0;
5876       enum machine_mode compare_mode = GET_MODE (dest);
5877
5878       if (GET_CODE (src) == COMPARE)
5879         op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5880       else
5881         op0 = src, op1 = CONST0_RTX (GET_MODE (src));
5882
5883       tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
5884                                            op0, op1);
5885       if (!tmp)
5886         new_code = old_code;
5887       else if (!CONSTANT_P (tmp))
5888         {
5889           new_code = GET_CODE (tmp);
5890           op0 = XEXP (tmp, 0);
5891           op1 = XEXP (tmp, 1);
5892         }
5893       else
5894         {
5895           rtx pat = PATTERN (other_insn);
5896           undobuf.other_insn = other_insn;
5897           SUBST (*cc_use, tmp);
5898
5899           /* Attempt to simplify CC user.  */
5900           if (GET_CODE (pat) == SET)
5901             {
5902               rtx new_rtx = simplify_rtx (SET_SRC (pat));
5903               if (new_rtx != NULL_RTX)
5904                 SUBST (SET_SRC (pat), new_rtx);
5905             }
5906
5907           /* Convert X into a no-op move.  */
5908           SUBST (SET_DEST (x), pc_rtx);
5909           SUBST (SET_SRC (x), pc_rtx);
5910           return x;
5911         }
5912
5913       /* Simplify our comparison, if possible.  */
5914       new_code = simplify_comparison (new_code, &op0, &op1);
5915
5916 #ifdef SELECT_CC_MODE
5917       /* If this machine has CC modes other than CCmode, check to see if we
5918          need to use a different CC mode here.  */
5919       if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
5920         compare_mode = GET_MODE (op0);
5921       else
5922         compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5923
5924 #ifndef HAVE_cc0
5925       /* If the mode changed, we have to change SET_DEST, the mode in the
5926          compare, and the mode in the place SET_DEST is used.  If SET_DEST is
5927          a hard register, just build new versions with the proper mode.  If it
5928          is a pseudo, we lose unless it is only time we set the pseudo, in
5929          which case we can safely change its mode.  */
5930       if (compare_mode != GET_MODE (dest))
5931         {
5932           if (can_change_dest_mode (dest, 0, compare_mode))
5933             {
5934               unsigned int regno = REGNO (dest);
5935               rtx new_dest;
5936
5937               if (regno < FIRST_PSEUDO_REGISTER)
5938                 new_dest = gen_rtx_REG (compare_mode, regno);
5939               else
5940                 {
5941                   SUBST_MODE (regno_reg_rtx[regno], compare_mode);
5942                   new_dest = regno_reg_rtx[regno];
5943                 }
5944
5945               SUBST (SET_DEST (x), new_dest);
5946               SUBST (XEXP (*cc_use, 0), new_dest);
5947               other_changed = 1;
5948
5949               dest = new_dest;
5950             }
5951         }
5952 #endif  /* cc0 */
5953 #endif  /* SELECT_CC_MODE */
5954
5955       /* If the code changed, we have to build a new comparison in
5956          undobuf.other_insn.  */
5957       if (new_code != old_code)
5958         {
5959           int other_changed_previously = other_changed;
5960           unsigned HOST_WIDE_INT mask;
5961           rtx old_cc_use = *cc_use;
5962
5963           SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5964                                           dest, const0_rtx));
5965           other_changed = 1;
5966
5967           /* If the only change we made was to change an EQ into an NE or
5968              vice versa, OP0 has only one bit that might be nonzero, and OP1
5969              is zero, check if changing the user of the condition code will
5970              produce a valid insn.  If it won't, we can keep the original code
5971              in that insn by surrounding our operation with an XOR.  */
5972
5973           if (((old_code == NE && new_code == EQ)
5974                || (old_code == EQ && new_code == NE))
5975               && ! other_changed_previously && op1 == const0_rtx
5976               && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5977               && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5978             {
5979               rtx pat = PATTERN (other_insn), note = 0;
5980
5981               if ((recog_for_combine (&pat, other_insn, &note) < 0
5982                    && ! check_asm_operands (pat)))
5983                 {
5984                   *cc_use = old_cc_use;
5985                   other_changed = 0;
5986
5987                   op0 = simplify_gen_binary (XOR, GET_MODE (op0),
5988                                              op0, GEN_INT (mask));
5989                 }
5990             }
5991         }
5992
5993       if (other_changed)
5994         undobuf.other_insn = other_insn;
5995
5996       /* Otherwise, if we didn't previously have a COMPARE in the
5997          correct mode, we need one.  */
5998       if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5999         {
6000           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6001           src = SET_SRC (x);
6002         }
6003       else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6004         {
6005           SUBST (SET_SRC (x), op0);
6006           src = SET_SRC (x);
6007         }
6008       /* Otherwise, update the COMPARE if needed.  */
6009       else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6010         {
6011           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6012           src = SET_SRC (x);
6013         }
6014     }
6015   else
6016     {
6017       /* Get SET_SRC in a form where we have placed back any
6018          compound expressions.  Then do the checks below.  */
6019       src = make_compound_operation (src, SET);
6020       SUBST (SET_SRC (x), src);
6021     }
6022
6023   /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6024      and X being a REG or (subreg (reg)), we may be able to convert this to
6025      (set (subreg:m2 x) (op)).
6026
6027      We can always do this if M1 is narrower than M2 because that means that
6028      we only care about the low bits of the result.
6029
6030      However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6031      perform a narrower operation than requested since the high-order bits will
6032      be undefined.  On machine where it is defined, this transformation is safe
6033      as long as M1 and M2 have the same number of words.  */
6034
6035   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6036       && !OBJECT_P (SUBREG_REG (src))
6037       && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6038            / UNITS_PER_WORD)
6039           == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6040                + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6041 #ifndef WORD_REGISTER_OPERATIONS
6042       && (GET_MODE_SIZE (GET_MODE (src))
6043         < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6044 #endif
6045 #ifdef CANNOT_CHANGE_MODE_CLASS
6046       && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6047             && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6048                                          GET_MODE (SUBREG_REG (src)),
6049                                          GET_MODE (src)))
6050 #endif
6051       && (REG_P (dest)
6052           || (GET_CODE (dest) == SUBREG
6053               && REG_P (SUBREG_REG (dest)))))
6054     {
6055       SUBST (SET_DEST (x),
6056              gen_lowpart (GET_MODE (SUBREG_REG (src)),
6057                                       dest));
6058       SUBST (SET_SRC (x), SUBREG_REG (src));
6059
6060       src = SET_SRC (x), dest = SET_DEST (x);
6061     }
6062
6063 #ifdef HAVE_cc0
6064   /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6065      in SRC.  */
6066   if (dest == cc0_rtx
6067       && GET_CODE (src) == SUBREG
6068       && subreg_lowpart_p (src)
6069       && (GET_MODE_BITSIZE (GET_MODE (src))
6070           < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
6071     {
6072       rtx inner = SUBREG_REG (src);
6073       enum machine_mode inner_mode = GET_MODE (inner);
6074
6075       /* Here we make sure that we don't have a sign bit on.  */
6076       if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
6077           && (nonzero_bits (inner, inner_mode)
6078               < ((unsigned HOST_WIDE_INT) 1
6079                  << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
6080         {
6081           SUBST (SET_SRC (x), inner);
6082           src = SET_SRC (x);
6083         }
6084     }
6085 #endif
6086
6087 #ifdef LOAD_EXTEND_OP
6088   /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6089      would require a paradoxical subreg.  Replace the subreg with a
6090      zero_extend to avoid the reload that would otherwise be required.  */
6091
6092   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6093       && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6094       && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6095       && SUBREG_BYTE (src) == 0
6096       && (GET_MODE_SIZE (GET_MODE (src))
6097           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6098       && MEM_P (SUBREG_REG (src)))
6099     {
6100       SUBST (SET_SRC (x),
6101              gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6102                             GET_MODE (src), SUBREG_REG (src)));
6103
6104       src = SET_SRC (x);
6105     }
6106 #endif
6107
6108   /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6109      are comparing an item known to be 0 or -1 against 0, use a logical
6110      operation instead. Check for one of the arms being an IOR of the other
6111      arm with some value.  We compute three terms to be IOR'ed together.  In
6112      practice, at most two will be nonzero.  Then we do the IOR's.  */
6113
6114   if (GET_CODE (dest) != PC
6115       && GET_CODE (src) == IF_THEN_ELSE
6116       && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6117       && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6118       && XEXP (XEXP (src, 0), 1) == const0_rtx
6119       && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6120 #ifdef HAVE_conditional_move
6121       && ! can_conditionally_move_p (GET_MODE (src))
6122 #endif
6123       && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6124                                GET_MODE (XEXP (XEXP (src, 0), 0)))
6125           == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
6126       && ! side_effects_p (src))
6127     {
6128       rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6129                       ? XEXP (src, 1) : XEXP (src, 2));
6130       rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6131                    ? XEXP (src, 2) : XEXP (src, 1));
6132       rtx term1 = const0_rtx, term2, term3;
6133
6134       if (GET_CODE (true_rtx) == IOR
6135           && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6136         term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6137       else if (GET_CODE (true_rtx) == IOR
6138                && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6139         term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6140       else if (GET_CODE (false_rtx) == IOR
6141                && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6142         term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6143       else if (GET_CODE (false_rtx) == IOR
6144                && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6145         term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6146
6147       term2 = simplify_gen_binary (AND, GET_MODE (src),
6148                                    XEXP (XEXP (src, 0), 0), true_rtx);
6149       term3 = simplify_gen_binary (AND, GET_MODE (src),
6150                                    simplify_gen_unary (NOT, GET_MODE (src),
6151                                                        XEXP (XEXP (src, 0), 0),
6152                                                        GET_MODE (src)),
6153                                    false_rtx);
6154
6155       SUBST (SET_SRC (x),
6156              simplify_gen_binary (IOR, GET_MODE (src),
6157                                   simplify_gen_binary (IOR, GET_MODE (src),
6158                                                        term1, term2),
6159                                   term3));
6160
6161       src = SET_SRC (x);
6162     }
6163
6164   /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6165      whole thing fail.  */
6166   if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6167     return src;
6168   else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6169     return dest;
6170   else
6171     /* Convert this into a field assignment operation, if possible.  */
6172     return make_field_assignment (x);
6173 }
6174 \f
6175 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6176    result.  */
6177
6178 static rtx
6179 simplify_logical (rtx x)
6180 {
6181   enum machine_mode mode = GET_MODE (x);
6182   rtx op0 = XEXP (x, 0);
6183   rtx op1 = XEXP (x, 1);
6184
6185   switch (GET_CODE (x))
6186     {
6187     case AND:
6188       /* We can call simplify_and_const_int only if we don't lose
6189          any (sign) bits when converting INTVAL (op1) to
6190          "unsigned HOST_WIDE_INT".  */
6191       if (CONST_INT_P (op1)
6192           && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6193               || INTVAL (op1) > 0))
6194         {
6195           x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6196           if (GET_CODE (x) != AND)
6197             return x;
6198
6199           op0 = XEXP (x, 0);
6200           op1 = XEXP (x, 1);
6201         }
6202
6203       /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6204          apply the distributive law and then the inverse distributive
6205          law to see if things simplify.  */
6206       if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6207         {
6208           rtx result = distribute_and_simplify_rtx (x, 0);
6209           if (result)
6210             return result;
6211         }
6212       if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6213         {
6214           rtx result = distribute_and_simplify_rtx (x, 1);
6215           if (result)
6216             return result;
6217         }
6218       break;
6219
6220     case IOR:
6221       /* If we have (ior (and A B) C), apply the distributive law and then
6222          the inverse distributive law to see if things simplify.  */
6223
6224       if (GET_CODE (op0) == AND)
6225         {
6226           rtx result = distribute_and_simplify_rtx (x, 0);
6227           if (result)
6228             return result;
6229         }
6230
6231       if (GET_CODE (op1) == AND)
6232         {
6233           rtx result = distribute_and_simplify_rtx (x, 1);
6234           if (result)
6235             return result;
6236         }
6237       break;
6238
6239     default:
6240       gcc_unreachable ();
6241     }
6242
6243   return x;
6244 }
6245 \f
6246 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6247    operations" because they can be replaced with two more basic operations.
6248    ZERO_EXTEND is also considered "compound" because it can be replaced with
6249    an AND operation, which is simpler, though only one operation.
6250
6251    The function expand_compound_operation is called with an rtx expression
6252    and will convert it to the appropriate shifts and AND operations,
6253    simplifying at each stage.
6254
6255    The function make_compound_operation is called to convert an expression
6256    consisting of shifts and ANDs into the equivalent compound expression.
6257    It is the inverse of this function, loosely speaking.  */
6258
6259 static rtx
6260 expand_compound_operation (rtx x)
6261 {
6262   unsigned HOST_WIDE_INT pos = 0, len;
6263   int unsignedp = 0;
6264   unsigned int modewidth;
6265   rtx tem;
6266
6267   switch (GET_CODE (x))
6268     {
6269     case ZERO_EXTEND:
6270       unsignedp = 1;
6271     case SIGN_EXTEND:
6272       /* We can't necessarily use a const_int for a multiword mode;
6273          it depends on implicitly extending the value.
6274          Since we don't know the right way to extend it,
6275          we can't tell whether the implicit way is right.
6276
6277          Even for a mode that is no wider than a const_int,
6278          we can't win, because we need to sign extend one of its bits through
6279          the rest of it, and we don't know which bit.  */
6280       if (CONST_INT_P (XEXP (x, 0)))
6281         return x;
6282
6283       /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6284          (zero_extend:MODE FROM) or (sign_extend:MODE FROM).  It is for any MEM
6285          because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6286          reloaded. If not for that, MEM's would very rarely be safe.
6287
6288          Reject MODEs bigger than a word, because we might not be able
6289          to reference a two-register group starting with an arbitrary register
6290          (and currently gen_lowpart might crash for a SUBREG).  */
6291
6292       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6293         return x;
6294
6295       /* Reject MODEs that aren't scalar integers because turning vector
6296          or complex modes into shifts causes problems.  */
6297
6298       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6299         return x;
6300
6301       len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
6302       /* If the inner object has VOIDmode (the only way this can happen
6303          is if it is an ASM_OPERANDS), we can't do anything since we don't
6304          know how much masking to do.  */
6305       if (len == 0)
6306         return x;
6307
6308       break;
6309
6310     case ZERO_EXTRACT:
6311       unsignedp = 1;
6312
6313       /* ... fall through ...  */
6314
6315     case SIGN_EXTRACT:
6316       /* If the operand is a CLOBBER, just return it.  */
6317       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6318         return XEXP (x, 0);
6319
6320       if (!CONST_INT_P (XEXP (x, 1))
6321           || !CONST_INT_P (XEXP (x, 2))
6322           || GET_MODE (XEXP (x, 0)) == VOIDmode)
6323         return x;
6324
6325       /* Reject MODEs that aren't scalar integers because turning vector
6326          or complex modes into shifts causes problems.  */
6327
6328       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6329         return x;
6330
6331       len = INTVAL (XEXP (x, 1));
6332       pos = INTVAL (XEXP (x, 2));
6333
6334       /* This should stay within the object being extracted, fail otherwise.  */
6335       if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
6336         return x;
6337
6338       if (BITS_BIG_ENDIAN)
6339         pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
6340
6341       break;
6342
6343     default:
6344       return x;
6345     }
6346   /* Convert sign extension to zero extension, if we know that the high
6347      bit is not set, as this is easier to optimize.  It will be converted
6348      back to cheaper alternative in make_extraction.  */
6349   if (GET_CODE (x) == SIGN_EXTEND
6350       && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6351           && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6352                 & ~(((unsigned HOST_WIDE_INT)
6353                       GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6354                      >> 1))
6355                == 0)))
6356     {
6357       rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6358       rtx temp2 = expand_compound_operation (temp);
6359
6360       /* Make sure this is a profitable operation.  */
6361       if (rtx_cost (x, SET, optimize_this_for_speed_p)
6362           > rtx_cost (temp2, SET, optimize_this_for_speed_p))
6363        return temp2;
6364       else if (rtx_cost (x, SET, optimize_this_for_speed_p)
6365                > rtx_cost (temp, SET, optimize_this_for_speed_p))
6366        return temp;
6367       else
6368        return x;
6369     }
6370
6371   /* We can optimize some special cases of ZERO_EXTEND.  */
6372   if (GET_CODE (x) == ZERO_EXTEND)
6373     {
6374       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6375          know that the last value didn't have any inappropriate bits
6376          set.  */
6377       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6378           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6379           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6380           && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6381               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6382         return XEXP (XEXP (x, 0), 0);
6383
6384       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
6385       if (GET_CODE (XEXP (x, 0)) == SUBREG
6386           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6387           && subreg_lowpart_p (XEXP (x, 0))
6388           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6389           && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6390               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6391         return SUBREG_REG (XEXP (x, 0));
6392
6393       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6394          is a comparison and STORE_FLAG_VALUE permits.  This is like
6395          the first case, but it works even when GET_MODE (x) is larger
6396          than HOST_WIDE_INT.  */
6397       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6398           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6399           && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6400           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6401               <= HOST_BITS_PER_WIDE_INT)
6402           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
6403               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6404         return XEXP (XEXP (x, 0), 0);
6405
6406       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
6407       if (GET_CODE (XEXP (x, 0)) == SUBREG
6408           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6409           && subreg_lowpart_p (XEXP (x, 0))
6410           && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6411           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6412               <= HOST_BITS_PER_WIDE_INT)
6413           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
6414               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6415         return SUBREG_REG (XEXP (x, 0));
6416
6417     }
6418
6419   /* If we reach here, we want to return a pair of shifts.  The inner
6420      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
6421      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
6422      logical depending on the value of UNSIGNEDP.
6423
6424      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6425      converted into an AND of a shift.
6426
6427      We must check for the case where the left shift would have a negative
6428      count.  This can happen in a case like (x >> 31) & 255 on machines
6429      that can't shift by a constant.  On those machines, we would first
6430      combine the shift with the AND to produce a variable-position
6431      extraction.  Then the constant of 31 would be substituted in to produce
6432      a such a position.  */
6433
6434   modewidth = GET_MODE_BITSIZE (GET_MODE (x));
6435   if (modewidth + len >= pos)
6436     {
6437       enum machine_mode mode = GET_MODE (x);
6438       tem = gen_lowpart (mode, XEXP (x, 0));
6439       if (!tem || GET_CODE (tem) == CLOBBER)
6440         return x;
6441       tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6442                                   tem, modewidth - pos - len);
6443       tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6444                                   mode, tem, modewidth - len);
6445     }
6446   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6447     tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6448                                   simplify_shift_const (NULL_RTX, LSHIFTRT,
6449                                                         GET_MODE (x),
6450                                                         XEXP (x, 0), pos),
6451                                   ((HOST_WIDE_INT) 1 << len) - 1);
6452   else
6453     /* Any other cases we can't handle.  */
6454     return x;
6455
6456   /* If we couldn't do this for some reason, return the original
6457      expression.  */
6458   if (GET_CODE (tem) == CLOBBER)
6459     return x;
6460
6461   return tem;
6462 }
6463 \f
6464 /* X is a SET which contains an assignment of one object into
6465    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6466    or certain SUBREGS). If possible, convert it into a series of
6467    logical operations.
6468
6469    We half-heartedly support variable positions, but do not at all
6470    support variable lengths.  */
6471
6472 static const_rtx
6473 expand_field_assignment (const_rtx x)
6474 {
6475   rtx inner;
6476   rtx pos;                      /* Always counts from low bit.  */
6477   int len;
6478   rtx mask, cleared, masked;
6479   enum machine_mode compute_mode;
6480
6481   /* Loop until we find something we can't simplify.  */
6482   while (1)
6483     {
6484       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6485           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6486         {
6487           inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6488           len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
6489           pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6490         }
6491       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6492                && CONST_INT_P (XEXP (SET_DEST (x), 1)))
6493         {
6494           inner = XEXP (SET_DEST (x), 0);
6495           len = INTVAL (XEXP (SET_DEST (x), 1));
6496           pos = XEXP (SET_DEST (x), 2);
6497
6498           /* A constant position should stay within the width of INNER.  */
6499           if (CONST_INT_P (pos)
6500               && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
6501             break;
6502
6503           if (BITS_BIG_ENDIAN)
6504             {
6505               if (CONST_INT_P (pos))
6506                 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
6507                                - INTVAL (pos));
6508               else if (GET_CODE (pos) == MINUS
6509                        && CONST_INT_P (XEXP (pos, 1))
6510                        && (INTVAL (XEXP (pos, 1))
6511                            == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
6512                 /* If position is ADJUST - X, new position is X.  */
6513                 pos = XEXP (pos, 0);
6514               else
6515                 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6516                                            GEN_INT (GET_MODE_BITSIZE (
6517                                                     GET_MODE (inner))
6518                                                     - len),
6519                                            pos);
6520             }
6521         }
6522
6523       /* A SUBREG between two modes that occupy the same numbers of words
6524          can be done by moving the SUBREG to the source.  */
6525       else if (GET_CODE (SET_DEST (x)) == SUBREG
6526                /* We need SUBREGs to compute nonzero_bits properly.  */
6527                && nonzero_sign_valid
6528                && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6529                      + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6530                    == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6531                         + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6532         {
6533           x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6534                            gen_lowpart
6535                            (GET_MODE (SUBREG_REG (SET_DEST (x))),
6536                             SET_SRC (x)));
6537           continue;
6538         }
6539       else
6540         break;
6541
6542       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6543         inner = SUBREG_REG (inner);
6544
6545       compute_mode = GET_MODE (inner);
6546
6547       /* Don't attempt bitwise arithmetic on non scalar integer modes.  */
6548       if (! SCALAR_INT_MODE_P (compute_mode))
6549         {
6550           enum machine_mode imode;
6551
6552           /* Don't do anything for vector or complex integral types.  */
6553           if (! FLOAT_MODE_P (compute_mode))
6554             break;
6555
6556           /* Try to find an integral mode to pun with.  */
6557           imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6558           if (imode == BLKmode)
6559             break;
6560
6561           compute_mode = imode;
6562           inner = gen_lowpart (imode, inner);
6563         }
6564
6565       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
6566       if (len >= HOST_BITS_PER_WIDE_INT)
6567         break;
6568
6569       /* Now compute the equivalent expression.  Make a copy of INNER
6570          for the SET_DEST in case it is a MEM into which we will substitute;
6571          we don't want shared RTL in that case.  */
6572       mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
6573       cleared = simplify_gen_binary (AND, compute_mode,
6574                                      simplify_gen_unary (NOT, compute_mode,
6575                                        simplify_gen_binary (ASHIFT,
6576                                                             compute_mode,
6577                                                             mask, pos),
6578                                        compute_mode),
6579                                      inner);
6580       masked = simplify_gen_binary (ASHIFT, compute_mode,
6581                                     simplify_gen_binary (
6582                                       AND, compute_mode,
6583                                       gen_lowpart (compute_mode, SET_SRC (x)),
6584                                       mask),
6585                                     pos);
6586
6587       x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
6588                        simplify_gen_binary (IOR, compute_mode,
6589                                             cleared, masked));
6590     }
6591
6592   return x;
6593 }
6594 \f
6595 /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
6596    it is an RTX that represents a variable starting position; otherwise,
6597    POS is the (constant) starting bit position (counted from the LSB).
6598
6599    UNSIGNEDP is nonzero for an unsigned reference and zero for a
6600    signed reference.
6601
6602    IN_DEST is nonzero if this is a reference in the destination of a
6603    SET.  This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If nonzero,
6604    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6605    be used.
6606
6607    IN_COMPARE is nonzero if we are in a COMPARE.  This means that a
6608    ZERO_EXTRACT should be built even for bits starting at bit 0.
6609
6610    MODE is the desired mode of the result (if IN_DEST == 0).
6611
6612    The result is an RTX for the extraction or NULL_RTX if the target
6613    can't handle it.  */
6614
6615 static rtx
6616 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6617                  rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6618                  int in_dest, int in_compare)
6619 {
6620   /* This mode describes the size of the storage area
6621      to fetch the overall value from.  Within that, we
6622      ignore the POS lowest bits, etc.  */
6623   enum machine_mode is_mode = GET_MODE (inner);
6624   enum machine_mode inner_mode;
6625   enum machine_mode wanted_inner_mode;
6626   enum machine_mode wanted_inner_reg_mode = word_mode;
6627   enum machine_mode pos_mode = word_mode;
6628   enum machine_mode extraction_mode = word_mode;
6629   enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6630   rtx new_rtx = 0;
6631   rtx orig_pos_rtx = pos_rtx;
6632   HOST_WIDE_INT orig_pos;
6633
6634   if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6635     {
6636       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6637          consider just the QI as the memory to extract from.
6638          The subreg adds or removes high bits; its mode is
6639          irrelevant to the meaning of this extraction,
6640          since POS and LEN count from the lsb.  */
6641       if (MEM_P (SUBREG_REG (inner)))
6642         is_mode = GET_MODE (SUBREG_REG (inner));
6643       inner = SUBREG_REG (inner);
6644     }
6645   else if (GET_CODE (inner) == ASHIFT
6646            && CONST_INT_P (XEXP (inner, 1))
6647            && pos_rtx == 0 && pos == 0
6648            && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6649     {
6650       /* We're extracting the least significant bits of an rtx
6651          (ashift X (const_int C)), where LEN > C.  Extract the
6652          least significant (LEN - C) bits of X, giving an rtx
6653          whose mode is MODE, then shift it left C times.  */
6654       new_rtx = make_extraction (mode, XEXP (inner, 0),
6655                              0, 0, len - INTVAL (XEXP (inner, 1)),
6656                              unsignedp, in_dest, in_compare);
6657       if (new_rtx != 0)
6658         return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
6659     }
6660
6661   inner_mode = GET_MODE (inner);
6662
6663   if (pos_rtx && CONST_INT_P (pos_rtx))
6664     pos = INTVAL (pos_rtx), pos_rtx = 0;
6665
6666   /* See if this can be done without an extraction.  We never can if the
6667      width of the field is not the same as that of some integer mode. For
6668      registers, we can only avoid the extraction if the position is at the
6669      low-order bit and this is either not in the destination or we have the
6670      appropriate STRICT_LOW_PART operation available.
6671
6672      For MEM, we can avoid an extract if the field starts on an appropriate
6673      boundary and we can change the mode of the memory reference.  */
6674
6675   if (tmode != BLKmode
6676       && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6677            && !MEM_P (inner)
6678            && (inner_mode == tmode
6679                || !REG_P (inner)
6680                || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
6681                                          GET_MODE_BITSIZE (inner_mode))
6682                || reg_truncated_to_mode (tmode, inner))
6683            && (! in_dest
6684                || (REG_P (inner)
6685                    && have_insn_for (STRICT_LOW_PART, tmode))))
6686           || (MEM_P (inner) && pos_rtx == 0
6687               && (pos
6688                   % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6689                      : BITS_PER_UNIT)) == 0
6690               /* We can't do this if we are widening INNER_MODE (it
6691                  may not be aligned, for one thing).  */
6692               && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6693               && (inner_mode == tmode
6694                   || (! mode_dependent_address_p (XEXP (inner, 0))
6695                       && ! MEM_VOLATILE_P (inner))))))
6696     {
6697       /* If INNER is a MEM, make a new MEM that encompasses just the desired
6698          field.  If the original and current mode are the same, we need not
6699          adjust the offset.  Otherwise, we do if bytes big endian.
6700
6701          If INNER is not a MEM, get a piece consisting of just the field
6702          of interest (in this case POS % BITS_PER_WORD must be 0).  */
6703
6704       if (MEM_P (inner))
6705         {
6706           HOST_WIDE_INT offset;
6707
6708           /* POS counts from lsb, but make OFFSET count in memory order.  */
6709           if (BYTES_BIG_ENDIAN)
6710             offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6711           else
6712             offset = pos / BITS_PER_UNIT;
6713
6714           new_rtx = adjust_address_nv (inner, tmode, offset);
6715         }
6716       else if (REG_P (inner))
6717         {
6718           if (tmode != inner_mode)
6719             {
6720               /* We can't call gen_lowpart in a DEST since we
6721                  always want a SUBREG (see below) and it would sometimes
6722                  return a new hard register.  */
6723               if (pos || in_dest)
6724                 {
6725                   HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6726
6727                   if (WORDS_BIG_ENDIAN
6728                       && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6729                     final_word = ((GET_MODE_SIZE (inner_mode)
6730                                    - GET_MODE_SIZE (tmode))
6731                                   / UNITS_PER_WORD) - final_word;
6732
6733                   final_word *= UNITS_PER_WORD;
6734                   if (BYTES_BIG_ENDIAN &&
6735                       GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6736                     final_word += (GET_MODE_SIZE (inner_mode)
6737                                    - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6738
6739                   /* Avoid creating invalid subregs, for example when
6740                      simplifying (x>>32)&255.  */
6741                   if (!validate_subreg (tmode, inner_mode, inner, final_word))
6742                     return NULL_RTX;
6743
6744                   new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
6745                 }
6746               else
6747                 new_rtx = gen_lowpart (tmode, inner);
6748             }
6749           else
6750             new_rtx = inner;
6751         }
6752       else
6753         new_rtx = force_to_mode (inner, tmode,
6754                              len >= HOST_BITS_PER_WIDE_INT
6755                              ? ~(unsigned HOST_WIDE_INT) 0
6756                              : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6757                              0);
6758
6759       /* If this extraction is going into the destination of a SET,
6760          make a STRICT_LOW_PART unless we made a MEM.  */
6761
6762       if (in_dest)
6763         return (MEM_P (new_rtx) ? new_rtx
6764                 : (GET_CODE (new_rtx) != SUBREG
6765                    ? gen_rtx_CLOBBER (tmode, const0_rtx)
6766                    : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
6767
6768       if (mode == tmode)
6769         return new_rtx;
6770
6771       if (CONST_INT_P (new_rtx)
6772           || GET_CODE (new_rtx) == CONST_DOUBLE)
6773         return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6774                                          mode, new_rtx, tmode);
6775
6776       /* If we know that no extraneous bits are set, and that the high
6777          bit is not set, convert the extraction to the cheaper of
6778          sign and zero extension, that are equivalent in these cases.  */
6779       if (flag_expensive_optimizations
6780           && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6781               && ((nonzero_bits (new_rtx, tmode)
6782                    & ~(((unsigned HOST_WIDE_INT)
6783                         GET_MODE_MASK (tmode))
6784                        >> 1))
6785                   == 0)))
6786         {
6787           rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
6788           rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
6789
6790           /* Prefer ZERO_EXTENSION, since it gives more information to
6791              backends.  */
6792           if (rtx_cost (temp, SET, optimize_this_for_speed_p)
6793               <= rtx_cost (temp1, SET, optimize_this_for_speed_p))
6794             return temp;
6795           return temp1;
6796         }
6797
6798       /* Otherwise, sign- or zero-extend unless we already are in the
6799          proper mode.  */
6800
6801       return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6802                              mode, new_rtx));
6803     }
6804
6805   /* Unless this is a COMPARE or we have a funny memory reference,
6806      don't do anything with zero-extending field extracts starting at
6807      the low-order bit since they are simple AND operations.  */
6808   if (pos_rtx == 0 && pos == 0 && ! in_dest
6809       && ! in_compare && unsignedp)
6810     return 0;
6811
6812   /* Unless INNER is not MEM, reject this if we would be spanning bytes or
6813      if the position is not a constant and the length is not 1.  In all
6814      other cases, we would only be going outside our object in cases when
6815      an original shift would have been undefined.  */
6816   if (MEM_P (inner)
6817       && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6818           || (pos_rtx != 0 && len != 1)))
6819     return 0;
6820
6821   /* Get the mode to use should INNER not be a MEM, the mode for the position,
6822      and the mode for the result.  */
6823   if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6824     {
6825       wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6826       pos_mode = mode_for_extraction (EP_insv, 2);
6827       extraction_mode = mode_for_extraction (EP_insv, 3);
6828     }
6829
6830   if (! in_dest && unsignedp
6831       && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6832     {
6833       wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6834       pos_mode = mode_for_extraction (EP_extzv, 3);
6835       extraction_mode = mode_for_extraction (EP_extzv, 0);
6836     }
6837
6838   if (! in_dest && ! unsignedp
6839       && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6840     {
6841       wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6842       pos_mode = mode_for_extraction (EP_extv, 3);
6843       extraction_mode = mode_for_extraction (EP_extv, 0);
6844     }
6845
6846   /* Never narrow an object, since that might not be safe.  */
6847
6848   if (mode != VOIDmode
6849       && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6850     extraction_mode = mode;
6851
6852   if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6853       && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6854     pos_mode = GET_MODE (pos_rtx);
6855
6856   /* If this is not from memory, the desired mode is the preferred mode
6857      for an extraction pattern's first input operand, or word_mode if there
6858      is none.  */
6859   if (!MEM_P (inner))
6860     wanted_inner_mode = wanted_inner_reg_mode;
6861   else
6862     {
6863       /* Be careful not to go beyond the extracted object and maintain the
6864          natural alignment of the memory.  */
6865       wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
6866       while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
6867              > GET_MODE_BITSIZE (wanted_inner_mode))
6868         {
6869           wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
6870           gcc_assert (wanted_inner_mode != VOIDmode);
6871         }
6872
6873       /* If we have to change the mode of memory and cannot, the desired mode
6874          is EXTRACTION_MODE.  */
6875       if (inner_mode != wanted_inner_mode
6876           && (mode_dependent_address_p (XEXP (inner, 0))
6877               || MEM_VOLATILE_P (inner)
6878               || pos_rtx))
6879         wanted_inner_mode = extraction_mode;
6880     }
6881
6882   orig_pos = pos;
6883
6884   if (BITS_BIG_ENDIAN)
6885     {
6886       /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6887          BITS_BIG_ENDIAN style.  If position is constant, compute new
6888          position.  Otherwise, build subtraction.
6889          Note that POS is relative to the mode of the original argument.
6890          If it's a MEM we need to recompute POS relative to that.
6891          However, if we're extracting from (or inserting into) a register,
6892          we want to recompute POS relative to wanted_inner_mode.  */
6893       int width = (MEM_P (inner)
6894                    ? GET_MODE_BITSIZE (is_mode)
6895                    : GET_MODE_BITSIZE (wanted_inner_mode));
6896
6897       if (pos_rtx == 0)
6898         pos = width - len - pos;
6899       else
6900         pos_rtx
6901           = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6902       /* POS may be less than 0 now, but we check for that below.
6903          Note that it can only be less than 0 if !MEM_P (inner).  */
6904     }
6905
6906   /* If INNER has a wider mode, and this is a constant extraction, try to
6907      make it smaller and adjust the byte to point to the byte containing
6908      the value.  */
6909   if (wanted_inner_mode != VOIDmode
6910       && inner_mode != wanted_inner_mode
6911       && ! pos_rtx
6912       && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6913       && MEM_P (inner)
6914       && ! mode_dependent_address_p (XEXP (inner, 0))
6915       && ! MEM_VOLATILE_P (inner))
6916     {
6917       int offset = 0;
6918
6919       /* The computations below will be correct if the machine is big
6920          endian in both bits and bytes or little endian in bits and bytes.
6921          If it is mixed, we must adjust.  */
6922
6923       /* If bytes are big endian and we had a paradoxical SUBREG, we must
6924          adjust OFFSET to compensate.  */
6925       if (BYTES_BIG_ENDIAN
6926           && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6927         offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6928
6929       /* We can now move to the desired byte.  */
6930       offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
6931                 * GET_MODE_SIZE (wanted_inner_mode);
6932       pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6933
6934       if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6935           && is_mode != wanted_inner_mode)
6936         offset = (GET_MODE_SIZE (is_mode)
6937                   - GET_MODE_SIZE (wanted_inner_mode) - offset);
6938
6939       inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6940     }
6941
6942   /* If INNER is not memory, get it into the proper mode.  If we are changing
6943      its mode, POS must be a constant and smaller than the size of the new
6944      mode.  */
6945   else if (!MEM_P (inner))
6946     {
6947       /* On the LHS, don't create paradoxical subregs implicitely truncating
6948          the register unless TRULY_NOOP_TRUNCATION.  */
6949       if (in_dest
6950           && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (inner)),
6951                                      GET_MODE_BITSIZE (wanted_inner_mode)))
6952         return NULL_RTX;
6953
6954       if (GET_MODE (inner) != wanted_inner_mode
6955           && (pos_rtx != 0
6956               || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6957         return NULL_RTX;
6958
6959       if (orig_pos < 0)
6960         return NULL_RTX;
6961
6962       inner = force_to_mode (inner, wanted_inner_mode,
6963                              pos_rtx
6964                              || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6965                              ? ~(unsigned HOST_WIDE_INT) 0
6966                              : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6967                                 << orig_pos),
6968                              0);
6969     }
6970
6971   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
6972      have to zero extend.  Otherwise, we can just use a SUBREG.  */
6973   if (pos_rtx != 0
6974       && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6975     {
6976       rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6977
6978       /* If we know that no extraneous bits are set, and that the high
6979          bit is not set, convert extraction to cheaper one - either
6980          SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6981          cases.  */
6982       if (flag_expensive_optimizations
6983           && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6984               && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6985                    & ~(((unsigned HOST_WIDE_INT)
6986                         GET_MODE_MASK (GET_MODE (pos_rtx)))
6987                        >> 1))
6988                   == 0)))
6989         {
6990           rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6991
6992           /* Prefer ZERO_EXTENSION, since it gives more information to
6993              backends.  */
6994           if (rtx_cost (temp1, SET, optimize_this_for_speed_p)
6995               < rtx_cost (temp, SET, optimize_this_for_speed_p))
6996             temp = temp1;
6997         }
6998       pos_rtx = temp;
6999     }
7000   else if (pos_rtx != 0
7001            && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
7002     pos_rtx = gen_lowpart (pos_mode, pos_rtx);
7003
7004   /* Make POS_RTX unless we already have it and it is correct.  If we don't
7005      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7006      be a CONST_INT.  */
7007   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7008     pos_rtx = orig_pos_rtx;
7009
7010   else if (pos_rtx == 0)
7011     pos_rtx = GEN_INT (pos);
7012
7013   /* Make the required operation.  See if we can use existing rtx.  */
7014   new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7015                          extraction_mode, inner, GEN_INT (len), pos_rtx);
7016   if (! in_dest)
7017     new_rtx = gen_lowpart (mode, new_rtx);
7018
7019   return new_rtx;
7020 }
7021 \f
7022 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7023    with any other operations in X.  Return X without that shift if so.  */
7024
7025 static rtx
7026 extract_left_shift (rtx x, int count)
7027 {
7028   enum rtx_code code = GET_CODE (x);
7029   enum machine_mode mode = GET_MODE (x);
7030   rtx tem;
7031
7032   switch (code)
7033     {
7034     case ASHIFT:
7035       /* This is the shift itself.  If it is wide enough, we will return
7036          either the value being shifted if the shift count is equal to
7037          COUNT or a shift for the difference.  */
7038       if (CONST_INT_P (XEXP (x, 1))
7039           && INTVAL (XEXP (x, 1)) >= count)
7040         return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7041                                      INTVAL (XEXP (x, 1)) - count);
7042       break;
7043
7044     case NEG:  case NOT:
7045       if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7046         return simplify_gen_unary (code, mode, tem, mode);
7047
7048       break;
7049
7050     case PLUS:  case IOR:  case XOR:  case AND:
7051       /* If we can safely shift this constant and we find the inner shift,
7052          make a new operation.  */
7053       if (CONST_INT_P (XEXP (x, 1))
7054           && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
7055           && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7056         return simplify_gen_binary (code, mode, tem,
7057                                     GEN_INT (INTVAL (XEXP (x, 1)) >> count));
7058
7059       break;
7060
7061     default:
7062       break;
7063     }
7064
7065   return 0;
7066 }
7067 \f
7068 /* Look at the expression rooted at X.  Look for expressions
7069    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7070    Form these expressions.
7071
7072    Return the new rtx, usually just X.
7073
7074    Also, for machines like the VAX that don't have logical shift insns,
7075    try to convert logical to arithmetic shift operations in cases where
7076    they are equivalent.  This undoes the canonicalizations to logical
7077    shifts done elsewhere.
7078
7079    We try, as much as possible, to re-use rtl expressions to save memory.
7080
7081    IN_CODE says what kind of expression we are processing.  Normally, it is
7082    SET.  In a memory address (inside a MEM, PLUS or minus, the latter two
7083    being kludges), it is MEM.  When processing the arguments of a comparison
7084    or a COMPARE against zero, it is COMPARE.  */
7085
7086 static rtx
7087 make_compound_operation (rtx x, enum rtx_code in_code)
7088 {
7089   enum rtx_code code = GET_CODE (x);
7090   enum machine_mode mode = GET_MODE (x);
7091   int mode_width = GET_MODE_BITSIZE (mode);
7092   rtx rhs, lhs;
7093   enum rtx_code next_code;
7094   int i, j;
7095   rtx new_rtx = 0;
7096   rtx tem;
7097   const char *fmt;
7098
7099   /* Select the code to be used in recursive calls.  Once we are inside an
7100      address, we stay there.  If we have a comparison, set to COMPARE,
7101      but once inside, go back to our default of SET.  */
7102
7103   next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
7104                : ((code == COMPARE || COMPARISON_P (x))
7105                   && XEXP (x, 1) == const0_rtx) ? COMPARE
7106                : in_code == COMPARE ? SET : in_code);
7107
7108   /* Process depending on the code of this operation.  If NEW is set
7109      nonzero, it will be returned.  */
7110
7111   switch (code)
7112     {
7113     case ASHIFT:
7114       /* Convert shifts by constants into multiplications if inside
7115          an address.  */
7116       if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7117           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7118           && INTVAL (XEXP (x, 1)) >= 0)
7119         {
7120           new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7121           new_rtx = gen_rtx_MULT (mode, new_rtx,
7122                               GEN_INT ((HOST_WIDE_INT) 1
7123                                        << INTVAL (XEXP (x, 1))));
7124         }
7125       break;
7126
7127     case AND:
7128       /* If the second operand is not a constant, we can't do anything
7129          with it.  */
7130       if (!CONST_INT_P (XEXP (x, 1)))
7131         break;
7132
7133       /* If the constant is a power of two minus one and the first operand
7134          is a logical right shift, make an extraction.  */
7135       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7136           && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
7137         {
7138           new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7139           new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7140                                  0, in_code == COMPARE);
7141         }
7142
7143       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
7144       else if (GET_CODE (XEXP (x, 0)) == SUBREG
7145                && subreg_lowpart_p (XEXP (x, 0))
7146                && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7147                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
7148         {
7149           new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7150                                          next_code);
7151           new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7152                                  XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7153                                  0, in_code == COMPARE);
7154         }
7155       /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)).  */
7156       else if ((GET_CODE (XEXP (x, 0)) == XOR
7157                 || GET_CODE (XEXP (x, 0)) == IOR)
7158                && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7159                && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7160                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
7161         {
7162           /* Apply the distributive law, and then try to make extractions.  */
7163           new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7164                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7165                                              XEXP (x, 1)),
7166                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7167                                              XEXP (x, 1)));
7168           new_rtx = make_compound_operation (new_rtx, in_code);
7169         }
7170
7171       /* If we are have (and (rotate X C) M) and C is larger than the number
7172          of bits in M, this is an extraction.  */
7173
7174       else if (GET_CODE (XEXP (x, 0)) == ROTATE
7175                && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7176                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
7177                && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7178         {
7179           new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7180           new_rtx = make_extraction (mode, new_rtx,
7181                                  (GET_MODE_BITSIZE (mode)
7182                                   - INTVAL (XEXP (XEXP (x, 0), 1))),
7183                                  NULL_RTX, i, 1, 0, in_code == COMPARE);
7184         }
7185
7186       /* On machines without logical shifts, if the operand of the AND is
7187          a logical shift and our mask turns off all the propagated sign
7188          bits, we can replace the logical shift with an arithmetic shift.  */
7189       else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7190                && !have_insn_for (LSHIFTRT, mode)
7191                && have_insn_for (ASHIFTRT, mode)
7192                && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7193                && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7194                && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7195                && mode_width <= HOST_BITS_PER_WIDE_INT)
7196         {
7197           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7198
7199           mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7200           if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7201             SUBST (XEXP (x, 0),
7202                    gen_rtx_ASHIFTRT (mode,
7203                                      make_compound_operation
7204                                      (XEXP (XEXP (x, 0), 0), next_code),
7205                                      XEXP (XEXP (x, 0), 1)));
7206         }
7207
7208       /* If the constant is one less than a power of two, this might be
7209          representable by an extraction even if no shift is present.
7210          If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7211          we are in a COMPARE.  */
7212       else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
7213         new_rtx = make_extraction (mode,
7214                                make_compound_operation (XEXP (x, 0),
7215                                                         next_code),
7216                                0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7217
7218       /* If we are in a comparison and this is an AND with a power of two,
7219          convert this into the appropriate bit extract.  */
7220       else if (in_code == COMPARE
7221                && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
7222         new_rtx = make_extraction (mode,
7223                                make_compound_operation (XEXP (x, 0),
7224                                                         next_code),
7225                                i, NULL_RTX, 1, 1, 0, 1);
7226
7227       break;
7228
7229     case LSHIFTRT:
7230       /* If the sign bit is known to be zero, replace this with an
7231          arithmetic shift.  */
7232       if (have_insn_for (ASHIFTRT, mode)
7233           && ! have_insn_for (LSHIFTRT, mode)
7234           && mode_width <= HOST_BITS_PER_WIDE_INT
7235           && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7236         {
7237           new_rtx = gen_rtx_ASHIFTRT (mode,
7238                                   make_compound_operation (XEXP (x, 0),
7239                                                            next_code),
7240                                   XEXP (x, 1));
7241           break;
7242         }
7243
7244       /* ... fall through ...  */
7245
7246     case ASHIFTRT:
7247       lhs = XEXP (x, 0);
7248       rhs = XEXP (x, 1);
7249
7250       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7251          this is a SIGN_EXTRACT.  */
7252       if (CONST_INT_P (rhs)
7253           && GET_CODE (lhs) == ASHIFT
7254           && CONST_INT_P (XEXP (lhs, 1))
7255           && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7256           && INTVAL (rhs) < mode_width)
7257         {
7258           new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7259           new_rtx = make_extraction (mode, new_rtx,
7260                                  INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7261                                  NULL_RTX, mode_width - INTVAL (rhs),
7262                                  code == LSHIFTRT, 0, in_code == COMPARE);
7263           break;
7264         }
7265
7266       /* See if we have operations between an ASHIFTRT and an ASHIFT.
7267          If so, try to merge the shifts into a SIGN_EXTEND.  We could
7268          also do this for some cases of SIGN_EXTRACT, but it doesn't
7269          seem worth the effort; the case checked for occurs on Alpha.  */
7270
7271       if (!OBJECT_P (lhs)
7272           && ! (GET_CODE (lhs) == SUBREG
7273                 && (OBJECT_P (SUBREG_REG (lhs))))
7274           && CONST_INT_P (rhs)
7275           && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7276           && INTVAL (rhs) < mode_width
7277           && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7278         new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7279                                0, NULL_RTX, mode_width - INTVAL (rhs),
7280                                code == LSHIFTRT, 0, in_code == COMPARE);
7281
7282       break;
7283
7284     case SUBREG:
7285       /* Call ourselves recursively on the inner expression.  If we are
7286          narrowing the object and it has a different RTL code from
7287          what it originally did, do this SUBREG as a force_to_mode.  */
7288
7289       tem = make_compound_operation (SUBREG_REG (x), in_code);
7290
7291       {
7292         rtx simplified = simplify_subreg (mode, tem, GET_MODE (SUBREG_REG (x)),
7293                                           SUBREG_BYTE (x));
7294
7295         if (simplified)
7296           tem = simplified;
7297
7298         if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
7299             && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
7300             && subreg_lowpart_p (x))
7301           {
7302             rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
7303                                        0);
7304
7305             /* If we have something other than a SUBREG, we might have
7306                done an expansion, so rerun ourselves.  */
7307             if (GET_CODE (newer) != SUBREG)
7308               newer = make_compound_operation (newer, in_code);
7309
7310             /* force_to_mode can expand compounds.  If it just re-expanded the
7311                compound use gen_lowpart instead to convert to the desired
7312                mode.  */
7313             if (rtx_equal_p (newer, x))
7314               return gen_lowpart (GET_MODE (x), tem);
7315
7316             return newer;
7317           }
7318
7319         if (simplified)
7320           return tem;
7321       }
7322       break;
7323
7324     default:
7325       break;
7326     }
7327
7328   if (new_rtx)
7329     {
7330       x = gen_lowpart (mode, new_rtx);
7331       code = GET_CODE (x);
7332     }
7333
7334   /* Now recursively process each operand of this operation.  */
7335   fmt = GET_RTX_FORMAT (code);
7336   for (i = 0; i < GET_RTX_LENGTH (code); i++)
7337     if (fmt[i] == 'e')
7338       {
7339         new_rtx = make_compound_operation (XEXP (x, i), next_code);
7340         SUBST (XEXP (x, i), new_rtx);
7341       }
7342     else if (fmt[i] == 'E')
7343       for (j = 0; j < XVECLEN (x, i); j++)
7344         {
7345           new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
7346           SUBST (XVECEXP (x, i, j), new_rtx);
7347         }
7348
7349   /* If this is a commutative operation, the changes to the operands
7350      may have made it noncanonical.  */
7351   if (COMMUTATIVE_ARITH_P (x)
7352       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7353     {
7354       tem = XEXP (x, 0);
7355       SUBST (XEXP (x, 0), XEXP (x, 1));
7356       SUBST (XEXP (x, 1), tem);
7357     }
7358
7359   return x;
7360 }
7361 \f
7362 /* Given M see if it is a value that would select a field of bits
7363    within an item, but not the entire word.  Return -1 if not.
7364    Otherwise, return the starting position of the field, where 0 is the
7365    low-order bit.
7366
7367    *PLEN is set to the length of the field.  */
7368
7369 static int
7370 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7371 {
7372   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
7373   int pos = exact_log2 (m & -m);
7374   int len = 0;
7375
7376   if (pos >= 0)
7377     /* Now shift off the low-order zero bits and see if we have a
7378        power of two minus 1.  */
7379     len = exact_log2 ((m >> pos) + 1);
7380
7381   if (len <= 0)
7382     pos = -1;
7383
7384   *plen = len;
7385   return pos;
7386 }
7387 \f
7388 /* If X refers to a register that equals REG in value, replace these
7389    references with REG.  */
7390 static rtx
7391 canon_reg_for_combine (rtx x, rtx reg)
7392 {
7393   rtx op0, op1, op2;
7394   const char *fmt;
7395   int i;
7396   bool copied;
7397
7398   enum rtx_code code = GET_CODE (x);
7399   switch (GET_RTX_CLASS (code))
7400     {
7401     case RTX_UNARY:
7402       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7403       if (op0 != XEXP (x, 0))
7404         return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7405                                    GET_MODE (reg));
7406       break;
7407
7408     case RTX_BIN_ARITH:
7409     case RTX_COMM_ARITH:
7410       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7411       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7412       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7413         return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7414       break;
7415
7416     case RTX_COMPARE:
7417     case RTX_COMM_COMPARE:
7418       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7419       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7420       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7421         return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7422                                         GET_MODE (op0), op0, op1);
7423       break;
7424
7425     case RTX_TERNARY:
7426     case RTX_BITFIELD_OPS:
7427       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7428       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7429       op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7430       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7431         return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7432                                      GET_MODE (op0), op0, op1, op2);
7433
7434     case RTX_OBJ:
7435       if (REG_P (x))
7436         {
7437           if (rtx_equal_p (get_last_value (reg), x)
7438               || rtx_equal_p (reg, get_last_value (x)))
7439             return reg;
7440           else
7441             break;
7442         }
7443
7444       /* fall through */
7445
7446     default:
7447       fmt = GET_RTX_FORMAT (code);
7448       copied = false;
7449       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7450         if (fmt[i] == 'e')
7451           {
7452             rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7453             if (op != XEXP (x, i))
7454               {
7455                 if (!copied)
7456                   {
7457                     copied = true;
7458                     x = copy_rtx (x);
7459                   }
7460                 XEXP (x, i) = op;
7461               }
7462           }
7463         else if (fmt[i] == 'E')
7464           {
7465             int j;
7466             for (j = 0; j < XVECLEN (x, i); j++)
7467               {
7468                 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
7469                 if (op != XVECEXP (x, i, j))
7470                   {
7471                     if (!copied)
7472                       {
7473                         copied = true;
7474                         x = copy_rtx (x);
7475                       }
7476                     XVECEXP (x, i, j) = op;
7477                   }
7478               }
7479           }
7480
7481       break;
7482     }
7483
7484   return x;
7485 }
7486
7487 /* Return X converted to MODE.  If the value is already truncated to
7488    MODE we can just return a subreg even though in the general case we
7489    would need an explicit truncation.  */
7490
7491 static rtx
7492 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
7493 {
7494   if (!CONST_INT_P (x)
7495       && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
7496       && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
7497                                  GET_MODE_BITSIZE (GET_MODE (x)))
7498       && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
7499     {
7500       /* Bit-cast X into an integer mode.  */
7501       if (!SCALAR_INT_MODE_P (GET_MODE (x)))
7502         x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
7503       x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
7504                               x, GET_MODE (x));
7505     }
7506
7507   return gen_lowpart (mode, x);
7508 }
7509
7510 /* See if X can be simplified knowing that we will only refer to it in
7511    MODE and will only refer to those bits that are nonzero in MASK.
7512    If other bits are being computed or if masking operations are done
7513    that select a superset of the bits in MASK, they can sometimes be
7514    ignored.
7515
7516    Return a possibly simplified expression, but always convert X to
7517    MODE.  If X is a CONST_INT, AND the CONST_INT with MASK.
7518
7519    If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
7520    are all off in X.  This is used when X will be complemented, by either
7521    NOT, NEG, or XOR.  */
7522
7523 static rtx
7524 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
7525                int just_select)
7526 {
7527   enum rtx_code code = GET_CODE (x);
7528   int next_select = just_select || code == XOR || code == NOT || code == NEG;
7529   enum machine_mode op_mode;
7530   unsigned HOST_WIDE_INT fuller_mask, nonzero;
7531   rtx op0, op1, temp;
7532
7533   /* If this is a CALL or ASM_OPERANDS, don't do anything.  Some of the
7534      code below will do the wrong thing since the mode of such an
7535      expression is VOIDmode.
7536
7537      Also do nothing if X is a CLOBBER; this can happen if X was
7538      the return value from a call to gen_lowpart.  */
7539   if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
7540     return x;
7541
7542   /* We want to perform the operation is its present mode unless we know
7543      that the operation is valid in MODE, in which case we do the operation
7544      in MODE.  */
7545   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
7546               && have_insn_for (code, mode))
7547              ? mode : GET_MODE (x));
7548
7549   /* It is not valid to do a right-shift in a narrower mode
7550      than the one it came in with.  */
7551   if ((code == LSHIFTRT || code == ASHIFTRT)
7552       && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
7553     op_mode = GET_MODE (x);
7554
7555   /* Truncate MASK to fit OP_MODE.  */
7556   if (op_mode)
7557     mask &= GET_MODE_MASK (op_mode);
7558
7559   /* When we have an arithmetic operation, or a shift whose count we
7560      do not know, we need to assume that all bits up to the highest-order
7561      bit in MASK will be needed.  This is how we form such a mask.  */
7562   if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
7563     fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
7564   else
7565     fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
7566                    - 1);
7567
7568   /* Determine what bits of X are guaranteed to be (non)zero.  */
7569   nonzero = nonzero_bits (x, mode);
7570
7571   /* If none of the bits in X are needed, return a zero.  */
7572   if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
7573     x = const0_rtx;
7574
7575   /* If X is a CONST_INT, return a new one.  Do this here since the
7576      test below will fail.  */
7577   if (CONST_INT_P (x))
7578     {
7579       if (SCALAR_INT_MODE_P (mode))
7580         return gen_int_mode (INTVAL (x) & mask, mode);
7581       else
7582         {
7583           x = GEN_INT (INTVAL (x) & mask);
7584           return gen_lowpart_common (mode, x);
7585         }
7586     }
7587
7588   /* If X is narrower than MODE and we want all the bits in X's mode, just
7589      get X in the proper mode.  */
7590   if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
7591       && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
7592     return gen_lowpart (mode, x);
7593
7594   /* We can ignore the effect of a SUBREG if it narrows the mode or
7595      if the constant masks to zero all the bits the mode doesn't have.  */
7596   if (GET_CODE (x) == SUBREG
7597       && subreg_lowpart_p (x)
7598       && ((GET_MODE_SIZE (GET_MODE (x))
7599            < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7600           || (0 == (mask
7601                     & GET_MODE_MASK (GET_MODE (x))
7602                     & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
7603     return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
7604
7605   /* The arithmetic simplifications here only work for scalar integer modes.  */
7606   if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
7607     return gen_lowpart_or_truncate (mode, x);
7608
7609   switch (code)
7610     {
7611     case CLOBBER:
7612       /* If X is a (clobber (const_int)), return it since we know we are
7613          generating something that won't match.  */
7614       return x;
7615
7616     case SIGN_EXTEND:
7617     case ZERO_EXTEND:
7618     case ZERO_EXTRACT:
7619     case SIGN_EXTRACT:
7620       x = expand_compound_operation (x);
7621       if (GET_CODE (x) != code)
7622         return force_to_mode (x, mode, mask, next_select);
7623       break;
7624
7625     case TRUNCATE:
7626       /* Similarly for a truncate.  */
7627       return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7628
7629     case AND:
7630       /* If this is an AND with a constant, convert it into an AND
7631          whose constant is the AND of that constant with MASK.  If it
7632          remains an AND of MASK, delete it since it is redundant.  */
7633
7634       if (CONST_INT_P (XEXP (x, 1)))
7635         {
7636           x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
7637                                       mask & INTVAL (XEXP (x, 1)));
7638
7639           /* If X is still an AND, see if it is an AND with a mask that
7640              is just some low-order bits.  If so, and it is MASK, we don't
7641              need it.  */
7642
7643           if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
7644               && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
7645                   == mask))
7646             x = XEXP (x, 0);
7647
7648           /* If it remains an AND, try making another AND with the bits
7649              in the mode mask that aren't in MASK turned on.  If the
7650              constant in the AND is wide enough, this might make a
7651              cheaper constant.  */
7652
7653           if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
7654               && GET_MODE_MASK (GET_MODE (x)) != mask
7655               && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
7656             {
7657               HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
7658                                     | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
7659               int width = GET_MODE_BITSIZE (GET_MODE (x));
7660               rtx y;
7661
7662               /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
7663                  number, sign extend it.  */
7664               if (width > 0 && width < HOST_BITS_PER_WIDE_INT
7665                   && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7666                 cval |= (HOST_WIDE_INT) -1 << width;
7667
7668               y = simplify_gen_binary (AND, GET_MODE (x),
7669                                        XEXP (x, 0), GEN_INT (cval));
7670               if (rtx_cost (y, SET, optimize_this_for_speed_p)
7671                   < rtx_cost (x, SET, optimize_this_for_speed_p))
7672                 x = y;
7673             }
7674
7675           break;
7676         }
7677
7678       goto binop;
7679
7680     case PLUS:
7681       /* In (and (plus FOO C1) M), if M is a mask that just turns off
7682          low-order bits (as in an alignment operation) and FOO is already
7683          aligned to that boundary, mask C1 to that boundary as well.
7684          This may eliminate that PLUS and, later, the AND.  */
7685
7686       {
7687         unsigned int width = GET_MODE_BITSIZE (mode);
7688         unsigned HOST_WIDE_INT smask = mask;
7689
7690         /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
7691            number, sign extend it.  */
7692
7693         if (width < HOST_BITS_PER_WIDE_INT
7694             && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7695           smask |= (HOST_WIDE_INT) -1 << width;
7696
7697         if (CONST_INT_P (XEXP (x, 1))
7698             && exact_log2 (- smask) >= 0
7699             && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
7700             && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
7701           return force_to_mode (plus_constant (XEXP (x, 0),
7702                                                (INTVAL (XEXP (x, 1)) & smask)),
7703                                 mode, smask, next_select);
7704       }
7705
7706       /* ... fall through ...  */
7707
7708     case MULT:
7709       /* For PLUS, MINUS and MULT, we need any bits less significant than the
7710          most significant bit in MASK since carries from those bits will
7711          affect the bits we are interested in.  */
7712       mask = fuller_mask;
7713       goto binop;
7714
7715     case MINUS:
7716       /* If X is (minus C Y) where C's least set bit is larger than any bit
7717          in the mask, then we may replace with (neg Y).  */
7718       if (CONST_INT_P (XEXP (x, 0))
7719           && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
7720                                         & -INTVAL (XEXP (x, 0))))
7721               > mask))
7722         {
7723           x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
7724                                   GET_MODE (x));
7725           return force_to_mode (x, mode, mask, next_select);
7726         }
7727
7728       /* Similarly, if C contains every bit in the fuller_mask, then we may
7729          replace with (not Y).  */
7730       if (CONST_INT_P (XEXP (x, 0))
7731           && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
7732               == INTVAL (XEXP (x, 0))))
7733         {
7734           x = simplify_gen_unary (NOT, GET_MODE (x),
7735                                   XEXP (x, 1), GET_MODE (x));
7736           return force_to_mode (x, mode, mask, next_select);
7737         }
7738
7739       mask = fuller_mask;
7740       goto binop;
7741
7742     case IOR:
7743     case XOR:
7744       /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
7745          LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
7746          operation which may be a bitfield extraction.  Ensure that the
7747          constant we form is not wider than the mode of X.  */
7748
7749       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7750           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7751           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7752           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7753           && CONST_INT_P (XEXP (x, 1))
7754           && ((INTVAL (XEXP (XEXP (x, 0), 1))
7755                + floor_log2 (INTVAL (XEXP (x, 1))))
7756               < GET_MODE_BITSIZE (GET_MODE (x)))
7757           && (INTVAL (XEXP (x, 1))
7758               & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
7759         {
7760           temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
7761                           << INTVAL (XEXP (XEXP (x, 0), 1)));
7762           temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
7763                                       XEXP (XEXP (x, 0), 0), temp);
7764           x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
7765                                    XEXP (XEXP (x, 0), 1));
7766           return force_to_mode (x, mode, mask, next_select);
7767         }
7768
7769     binop:
7770       /* For most binary operations, just propagate into the operation and
7771          change the mode if we have an operation of that mode.  */
7772
7773       op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
7774       op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
7775
7776       /* If we ended up truncating both operands, truncate the result of the
7777          operation instead.  */
7778       if (GET_CODE (op0) == TRUNCATE
7779           && GET_CODE (op1) == TRUNCATE)
7780         {
7781           op0 = XEXP (op0, 0);
7782           op1 = XEXP (op1, 0);
7783         }
7784
7785       op0 = gen_lowpart_or_truncate (op_mode, op0);
7786       op1 = gen_lowpart_or_truncate (op_mode, op1);
7787
7788       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7789         x = simplify_gen_binary (code, op_mode, op0, op1);
7790       break;
7791
7792     case ASHIFT:
7793       /* For left shifts, do the same, but just for the first operand.
7794          However, we cannot do anything with shifts where we cannot
7795          guarantee that the counts are smaller than the size of the mode
7796          because such a count will have a different meaning in a
7797          wider mode.  */
7798
7799       if (! (CONST_INT_P (XEXP (x, 1))
7800              && INTVAL (XEXP (x, 1)) >= 0
7801              && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7802           && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7803                 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7804                     < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7805         break;
7806
7807       /* If the shift count is a constant and we can do arithmetic in
7808          the mode of the shift, refine which bits we need.  Otherwise, use the
7809          conservative form of the mask.  */
7810       if (CONST_INT_P (XEXP (x, 1))
7811           && INTVAL (XEXP (x, 1)) >= 0
7812           && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7813           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7814         mask >>= INTVAL (XEXP (x, 1));
7815       else
7816         mask = fuller_mask;
7817
7818       op0 = gen_lowpart_or_truncate (op_mode,
7819                                      force_to_mode (XEXP (x, 0), op_mode,
7820                                                     mask, next_select));
7821
7822       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7823         x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
7824       break;
7825
7826     case LSHIFTRT:
7827       /* Here we can only do something if the shift count is a constant,
7828          this shift constant is valid for the host, and we can do arithmetic
7829          in OP_MODE.  */
7830
7831       if (CONST_INT_P (XEXP (x, 1))
7832           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7833           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7834         {
7835           rtx inner = XEXP (x, 0);
7836           unsigned HOST_WIDE_INT inner_mask;
7837
7838           /* Select the mask of the bits we need for the shift operand.  */
7839           inner_mask = mask << INTVAL (XEXP (x, 1));
7840
7841           /* We can only change the mode of the shift if we can do arithmetic
7842              in the mode of the shift and INNER_MASK is no wider than the
7843              width of X's mode.  */
7844           if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
7845             op_mode = GET_MODE (x);
7846
7847           inner = force_to_mode (inner, op_mode, inner_mask, next_select);
7848
7849           if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7850             x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7851         }
7852
7853       /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7854          shift and AND produces only copies of the sign bit (C2 is one less
7855          than a power of two), we can do this with just a shift.  */
7856
7857       if (GET_CODE (x) == LSHIFTRT
7858           && CONST_INT_P (XEXP (x, 1))
7859           /* The shift puts one of the sign bit copies in the least significant
7860              bit.  */
7861           && ((INTVAL (XEXP (x, 1))
7862                + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7863               >= GET_MODE_BITSIZE (GET_MODE (x)))
7864           && exact_log2 (mask + 1) >= 0
7865           /* Number of bits left after the shift must be more than the mask
7866              needs.  */
7867           && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7868               <= GET_MODE_BITSIZE (GET_MODE (x)))
7869           /* Must be more sign bit copies than the mask needs.  */
7870           && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7871               >= exact_log2 (mask + 1)))
7872         x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7873                                  GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7874                                           - exact_log2 (mask + 1)));
7875
7876       goto shiftrt;
7877
7878     case ASHIFTRT:
7879       /* If we are just looking for the sign bit, we don't need this shift at
7880          all, even if it has a variable count.  */
7881       if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7882           && (mask == ((unsigned HOST_WIDE_INT) 1
7883                        << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7884         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7885
7886       /* If this is a shift by a constant, get a mask that contains those bits
7887          that are not copies of the sign bit.  We then have two cases:  If
7888          MASK only includes those bits, this can be a logical shift, which may
7889          allow simplifications.  If MASK is a single-bit field not within
7890          those bits, we are requesting a copy of the sign bit and hence can
7891          shift the sign bit to the appropriate location.  */
7892
7893       if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
7894           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7895         {
7896           int i;
7897
7898           /* If the considered data is wider than HOST_WIDE_INT, we can't
7899              represent a mask for all its bits in a single scalar.
7900              But we only care about the lower bits, so calculate these.  */
7901
7902           if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7903             {
7904               nonzero = ~(HOST_WIDE_INT) 0;
7905
7906               /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7907                  is the number of bits a full-width mask would have set.
7908                  We need only shift if these are fewer than nonzero can
7909                  hold.  If not, we must keep all bits set in nonzero.  */
7910
7911               if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7912                   < HOST_BITS_PER_WIDE_INT)
7913                 nonzero >>= INTVAL (XEXP (x, 1))
7914                             + HOST_BITS_PER_WIDE_INT
7915                             - GET_MODE_BITSIZE (GET_MODE (x)) ;
7916             }
7917           else
7918             {
7919               nonzero = GET_MODE_MASK (GET_MODE (x));
7920               nonzero >>= INTVAL (XEXP (x, 1));
7921             }
7922
7923           if ((mask & ~nonzero) == 0)
7924             {
7925               x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
7926                                         XEXP (x, 0), INTVAL (XEXP (x, 1)));
7927               if (GET_CODE (x) != ASHIFTRT)
7928                 return force_to_mode (x, mode, mask, next_select);
7929             }
7930
7931           else if ((i = exact_log2 (mask)) >= 0)
7932             {
7933               x = simplify_shift_const
7934                   (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7935                    GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7936
7937               if (GET_CODE (x) != ASHIFTRT)
7938                 return force_to_mode (x, mode, mask, next_select);
7939             }
7940         }
7941
7942       /* If MASK is 1, convert this to an LSHIFTRT.  This can be done
7943          even if the shift count isn't a constant.  */
7944       if (mask == 1)
7945         x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7946                                  XEXP (x, 0), XEXP (x, 1));
7947
7948     shiftrt:
7949
7950       /* If this is a zero- or sign-extension operation that just affects bits
7951          we don't care about, remove it.  Be sure the call above returned
7952          something that is still a shift.  */
7953
7954       if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7955           && CONST_INT_P (XEXP (x, 1))
7956           && INTVAL (XEXP (x, 1)) >= 0
7957           && (INTVAL (XEXP (x, 1))
7958               <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7959           && GET_CODE (XEXP (x, 0)) == ASHIFT
7960           && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7961         return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7962                               next_select);
7963
7964       break;
7965
7966     case ROTATE:
7967     case ROTATERT:
7968       /* If the shift count is constant and we can do computations
7969          in the mode of X, compute where the bits we care about are.
7970          Otherwise, we can't do anything.  Don't change the mode of
7971          the shift or propagate MODE into the shift, though.  */
7972       if (CONST_INT_P (XEXP (x, 1))
7973           && INTVAL (XEXP (x, 1)) >= 0)
7974         {
7975           temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7976                                             GET_MODE (x), GEN_INT (mask),
7977                                             XEXP (x, 1));
7978           if (temp && CONST_INT_P (temp))
7979             SUBST (XEXP (x, 0),
7980                    force_to_mode (XEXP (x, 0), GET_MODE (x),
7981                                   INTVAL (temp), next_select));
7982         }
7983       break;
7984
7985     case NEG:
7986       /* If we just want the low-order bit, the NEG isn't needed since it
7987          won't change the low-order bit.  */
7988       if (mask == 1)
7989         return force_to_mode (XEXP (x, 0), mode, mask, just_select);
7990
7991       /* We need any bits less significant than the most significant bit in
7992          MASK since carries from those bits will affect the bits we are
7993          interested in.  */
7994       mask = fuller_mask;
7995       goto unop;
7996
7997     case NOT:
7998       /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7999          same as the XOR case above.  Ensure that the constant we form is not
8000          wider than the mode of X.  */
8001
8002       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8003           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8004           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8005           && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8006               < GET_MODE_BITSIZE (GET_MODE (x)))
8007           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8008         {
8009           temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8010                                GET_MODE (x));
8011           temp = simplify_gen_binary (XOR, GET_MODE (x),
8012                                       XEXP (XEXP (x, 0), 0), temp);
8013           x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8014                                    temp, XEXP (XEXP (x, 0), 1));
8015
8016           return force_to_mode (x, mode, mask, next_select);
8017         }
8018
8019       /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8020          use the full mask inside the NOT.  */
8021       mask = fuller_mask;
8022
8023     unop:
8024       op0 = gen_lowpart_or_truncate (op_mode,
8025                                      force_to_mode (XEXP (x, 0), mode, mask,
8026                                                     next_select));
8027       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8028         x = simplify_gen_unary (code, op_mode, op0, op_mode);
8029       break;
8030
8031     case NE:
8032       /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8033          in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8034          which is equal to STORE_FLAG_VALUE.  */
8035       if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
8036           && GET_MODE (XEXP (x, 0)) == mode
8037           && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8038           && (nonzero_bits (XEXP (x, 0), mode)
8039               == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8040         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8041
8042       break;
8043
8044     case IF_THEN_ELSE:
8045       /* We have no way of knowing if the IF_THEN_ELSE can itself be
8046          written in a narrower mode.  We play it safe and do not do so.  */
8047
8048       SUBST (XEXP (x, 1),
8049              gen_lowpart_or_truncate (GET_MODE (x),
8050                                       force_to_mode (XEXP (x, 1), mode,
8051                                                      mask, next_select)));
8052       SUBST (XEXP (x, 2),
8053              gen_lowpart_or_truncate (GET_MODE (x),
8054                                       force_to_mode (XEXP (x, 2), mode,
8055                                                      mask, next_select)));
8056       break;
8057
8058     default:
8059       break;
8060     }
8061
8062   /* Ensure we return a value of the proper mode.  */
8063   return gen_lowpart_or_truncate (mode, x);
8064 }
8065 \f
8066 /* Return nonzero if X is an expression that has one of two values depending on
8067    whether some other value is zero or nonzero.  In that case, we return the
8068    value that is being tested, *PTRUE is set to the value if the rtx being
8069    returned has a nonzero value, and *PFALSE is set to the other alternative.
8070
8071    If we return zero, we set *PTRUE and *PFALSE to X.  */
8072
8073 static rtx
8074 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8075 {
8076   enum machine_mode mode = GET_MODE (x);
8077   enum rtx_code code = GET_CODE (x);
8078   rtx cond0, cond1, true0, true1, false0, false1;
8079   unsigned HOST_WIDE_INT nz;
8080
8081   /* If we are comparing a value against zero, we are done.  */
8082   if ((code == NE || code == EQ)
8083       && XEXP (x, 1) == const0_rtx)
8084     {
8085       *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8086       *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8087       return XEXP (x, 0);
8088     }
8089
8090   /* If this is a unary operation whose operand has one of two values, apply
8091      our opcode to compute those values.  */
8092   else if (UNARY_P (x)
8093            && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8094     {
8095       *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8096       *pfalse = simplify_gen_unary (code, mode, false0,
8097                                     GET_MODE (XEXP (x, 0)));
8098       return cond0;
8099     }
8100
8101   /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8102      make can't possibly match and would suppress other optimizations.  */
8103   else if (code == COMPARE)
8104     ;
8105
8106   /* If this is a binary operation, see if either side has only one of two
8107      values.  If either one does or if both do and they are conditional on
8108      the same value, compute the new true and false values.  */
8109   else if (BINARY_P (x))
8110     {
8111       cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8112       cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8113
8114       if ((cond0 != 0 || cond1 != 0)
8115           && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8116         {
8117           /* If if_then_else_cond returned zero, then true/false are the
8118              same rtl.  We must copy one of them to prevent invalid rtl
8119              sharing.  */
8120           if (cond0 == 0)
8121             true0 = copy_rtx (true0);
8122           else if (cond1 == 0)
8123             true1 = copy_rtx (true1);
8124
8125           if (COMPARISON_P (x))
8126             {
8127               *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8128                                                 true0, true1);
8129               *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8130                                                  false0, false1);
8131              }
8132           else
8133             {
8134               *ptrue = simplify_gen_binary (code, mode, true0, true1);
8135               *pfalse = simplify_gen_binary (code, mode, false0, false1);
8136             }
8137
8138           return cond0 ? cond0 : cond1;
8139         }
8140
8141       /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8142          operands is zero when the other is nonzero, and vice-versa,
8143          and STORE_FLAG_VALUE is 1 or -1.  */
8144
8145       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8146           && (code == PLUS || code == IOR || code == XOR || code == MINUS
8147               || code == UMAX)
8148           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8149         {
8150           rtx op0 = XEXP (XEXP (x, 0), 1);
8151           rtx op1 = XEXP (XEXP (x, 1), 1);
8152
8153           cond0 = XEXP (XEXP (x, 0), 0);
8154           cond1 = XEXP (XEXP (x, 1), 0);
8155
8156           if (COMPARISON_P (cond0)
8157               && COMPARISON_P (cond1)
8158               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8159                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8160                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8161                   || ((swap_condition (GET_CODE (cond0))
8162                        == reversed_comparison_code (cond1, NULL))
8163                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8164                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8165               && ! side_effects_p (x))
8166             {
8167               *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8168               *pfalse = simplify_gen_binary (MULT, mode,
8169                                              (code == MINUS
8170                                               ? simplify_gen_unary (NEG, mode,
8171                                                                     op1, mode)
8172                                               : op1),
8173                                               const_true_rtx);
8174               return cond0;
8175             }
8176         }
8177
8178       /* Similarly for MULT, AND and UMIN, except that for these the result
8179          is always zero.  */
8180       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8181           && (code == MULT || code == AND || code == UMIN)
8182           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8183         {
8184           cond0 = XEXP (XEXP (x, 0), 0);
8185           cond1 = XEXP (XEXP (x, 1), 0);
8186
8187           if (COMPARISON_P (cond0)
8188               && COMPARISON_P (cond1)
8189               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8190                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8191                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8192                   || ((swap_condition (GET_CODE (cond0))
8193                        == reversed_comparison_code (cond1, NULL))
8194                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8195                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8196               && ! side_effects_p (x))
8197             {
8198               *ptrue = *pfalse = const0_rtx;
8199               return cond0;
8200             }
8201         }
8202     }
8203
8204   else if (code == IF_THEN_ELSE)
8205     {
8206       /* If we have IF_THEN_ELSE already, extract the condition and
8207          canonicalize it if it is NE or EQ.  */
8208       cond0 = XEXP (x, 0);
8209       *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8210       if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8211         return XEXP (cond0, 0);
8212       else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8213         {
8214           *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8215           return XEXP (cond0, 0);
8216         }
8217       else
8218         return cond0;
8219     }
8220
8221   /* If X is a SUBREG, we can narrow both the true and false values
8222      if the inner expression, if there is a condition.  */
8223   else if (code == SUBREG
8224            && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8225                                                &true0, &false0)))
8226     {
8227       true0 = simplify_gen_subreg (mode, true0,
8228                                    GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8229       false0 = simplify_gen_subreg (mode, false0,
8230                                     GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8231       if (true0 && false0)
8232         {
8233           *ptrue = true0;
8234           *pfalse = false0;
8235           return cond0;
8236         }
8237     }
8238
8239   /* If X is a constant, this isn't special and will cause confusions
8240      if we treat it as such.  Likewise if it is equivalent to a constant.  */
8241   else if (CONSTANT_P (x)
8242            || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8243     ;
8244
8245   /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8246      will be least confusing to the rest of the compiler.  */
8247   else if (mode == BImode)
8248     {
8249       *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8250       return x;
8251     }
8252
8253   /* If X is known to be either 0 or -1, those are the true and
8254      false values when testing X.  */
8255   else if (x == constm1_rtx || x == const0_rtx
8256            || (mode != VOIDmode
8257                && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
8258     {
8259       *ptrue = constm1_rtx, *pfalse = const0_rtx;
8260       return x;
8261     }
8262
8263   /* Likewise for 0 or a single bit.  */
8264   else if (SCALAR_INT_MODE_P (mode)
8265            && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8266            && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
8267     {
8268       *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
8269       return x;
8270     }
8271
8272   /* Otherwise fail; show no condition with true and false values the same.  */
8273   *ptrue = *pfalse = x;
8274   return 0;
8275 }
8276 \f
8277 /* Return the value of expression X given the fact that condition COND
8278    is known to be true when applied to REG as its first operand and VAL
8279    as its second.  X is known to not be shared and so can be modified in
8280    place.
8281
8282    We only handle the simplest cases, and specifically those cases that
8283    arise with IF_THEN_ELSE expressions.  */
8284
8285 static rtx
8286 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8287 {
8288   enum rtx_code code = GET_CODE (x);
8289   rtx temp;
8290   const char *fmt;
8291   int i, j;
8292
8293   if (side_effects_p (x))
8294     return x;
8295
8296   /* If either operand of the condition is a floating point value,
8297      then we have to avoid collapsing an EQ comparison.  */
8298   if (cond == EQ
8299       && rtx_equal_p (x, reg)
8300       && ! FLOAT_MODE_P (GET_MODE (x))
8301       && ! FLOAT_MODE_P (GET_MODE (val)))
8302     return val;
8303
8304   if (cond == UNEQ && rtx_equal_p (x, reg))
8305     return val;
8306
8307   /* If X is (abs REG) and we know something about REG's relationship
8308      with zero, we may be able to simplify this.  */
8309
8310   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8311     switch (cond)
8312       {
8313       case GE:  case GT:  case EQ:
8314         return XEXP (x, 0);
8315       case LT:  case LE:
8316         return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8317                                    XEXP (x, 0),
8318                                    GET_MODE (XEXP (x, 0)));
8319       default:
8320         break;
8321       }
8322
8323   /* The only other cases we handle are MIN, MAX, and comparisons if the
8324      operands are the same as REG and VAL.  */
8325
8326   else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8327     {
8328       if (rtx_equal_p (XEXP (x, 0), val))
8329         cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8330
8331       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8332         {
8333           if (COMPARISON_P (x))
8334             {
8335               if (comparison_dominates_p (cond, code))
8336                 return const_true_rtx;
8337
8338               code = reversed_comparison_code (x, NULL);
8339               if (code != UNKNOWN
8340                   && comparison_dominates_p (cond, code))
8341                 return const0_rtx;
8342               else
8343                 return x;
8344             }
8345           else if (code == SMAX || code == SMIN
8346                    || code == UMIN || code == UMAX)
8347             {
8348               int unsignedp = (code == UMIN || code == UMAX);
8349
8350               /* Do not reverse the condition when it is NE or EQ.
8351                  This is because we cannot conclude anything about
8352                  the value of 'SMAX (x, y)' when x is not equal to y,
8353                  but we can when x equals y.  */
8354               if ((code == SMAX || code == UMAX)
8355                   && ! (cond == EQ || cond == NE))
8356                 cond = reverse_condition (cond);
8357
8358               switch (cond)
8359                 {
8360                 case GE:   case GT:
8361                   return unsignedp ? x : XEXP (x, 1);
8362                 case LE:   case LT:
8363                   return unsignedp ? x : XEXP (x, 0);
8364                 case GEU:  case GTU:
8365                   return unsignedp ? XEXP (x, 1) : x;
8366                 case LEU:  case LTU:
8367                   return unsignedp ? XEXP (x, 0) : x;
8368                 default:
8369                   break;
8370                 }
8371             }
8372         }
8373     }
8374   else if (code == SUBREG)
8375     {
8376       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8377       rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
8378
8379       if (SUBREG_REG (x) != r)
8380         {
8381           /* We must simplify subreg here, before we lose track of the
8382              original inner_mode.  */
8383           new_rtx = simplify_subreg (GET_MODE (x), r,
8384                                  inner_mode, SUBREG_BYTE (x));
8385           if (new_rtx)
8386             return new_rtx;
8387           else
8388             SUBST (SUBREG_REG (x), r);
8389         }
8390
8391       return x;
8392     }
8393   /* We don't have to handle SIGN_EXTEND here, because even in the
8394      case of replacing something with a modeless CONST_INT, a
8395      CONST_INT is already (supposed to be) a valid sign extension for
8396      its narrower mode, which implies it's already properly
8397      sign-extended for the wider mode.  Now, for ZERO_EXTEND, the
8398      story is different.  */
8399   else if (code == ZERO_EXTEND)
8400     {
8401       enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8402       rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
8403
8404       if (XEXP (x, 0) != r)
8405         {
8406           /* We must simplify the zero_extend here, before we lose
8407              track of the original inner_mode.  */
8408           new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8409                                           r, inner_mode);
8410           if (new_rtx)
8411             return new_rtx;
8412           else
8413             SUBST (XEXP (x, 0), r);
8414         }
8415
8416       return x;
8417     }
8418
8419   fmt = GET_RTX_FORMAT (code);
8420   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8421     {
8422       if (fmt[i] == 'e')
8423         SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8424       else if (fmt[i] == 'E')
8425         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8426           SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8427                                                 cond, reg, val));
8428     }
8429
8430   return x;
8431 }
8432 \f
8433 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8434    assignment as a field assignment.  */
8435
8436 static int
8437 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8438 {
8439   if (x == y || rtx_equal_p (x, y))
8440     return 1;
8441
8442   if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8443     return 0;
8444
8445   /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8446      Note that all SUBREGs of MEM are paradoxical; otherwise they
8447      would have been rewritten.  */
8448   if (MEM_P (x) && GET_CODE (y) == SUBREG
8449       && MEM_P (SUBREG_REG (y))
8450       && rtx_equal_p (SUBREG_REG (y),
8451                       gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8452     return 1;
8453
8454   if (MEM_P (y) && GET_CODE (x) == SUBREG
8455       && MEM_P (SUBREG_REG (x))
8456       && rtx_equal_p (SUBREG_REG (x),
8457                       gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8458     return 1;
8459
8460   /* We used to see if get_last_value of X and Y were the same but that's
8461      not correct.  In one direction, we'll cause the assignment to have
8462      the wrong destination and in the case, we'll import a register into this
8463      insn that might have already have been dead.   So fail if none of the
8464      above cases are true.  */
8465   return 0;
8466 }
8467 \f
8468 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8469    Return that assignment if so.
8470
8471    We only handle the most common cases.  */
8472
8473 static rtx
8474 make_field_assignment (rtx x)
8475 {
8476   rtx dest = SET_DEST (x);
8477   rtx src = SET_SRC (x);
8478   rtx assign;
8479   rtx rhs, lhs;
8480   HOST_WIDE_INT c1;
8481   HOST_WIDE_INT pos;
8482   unsigned HOST_WIDE_INT len;
8483   rtx other;
8484   enum machine_mode mode;
8485
8486   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
8487      a clear of a one-bit field.  We will have changed it to
8488      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
8489      for a SUBREG.  */
8490
8491   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
8492       && CONST_INT_P (XEXP (XEXP (src, 0), 0))
8493       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
8494       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8495     {
8496       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8497                                 1, 1, 1, 0);
8498       if (assign != 0)
8499         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8500       return x;
8501     }
8502
8503   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
8504       && subreg_lowpart_p (XEXP (src, 0))
8505       && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
8506           < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
8507       && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
8508       && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
8509       && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
8510       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8511     {
8512       assign = make_extraction (VOIDmode, dest, 0,
8513                                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
8514                                 1, 1, 1, 0);
8515       if (assign != 0)
8516         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8517       return x;
8518     }
8519
8520   /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
8521      one-bit field.  */
8522   if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
8523       && XEXP (XEXP (src, 0), 0) == const1_rtx
8524       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8525     {
8526       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8527                                 1, 1, 1, 0);
8528       if (assign != 0)
8529         return gen_rtx_SET (VOIDmode, assign, const1_rtx);
8530       return x;
8531     }
8532
8533   /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
8534      SRC is an AND with all bits of that field set, then we can discard
8535      the AND.  */
8536   if (GET_CODE (dest) == ZERO_EXTRACT
8537       && CONST_INT_P (XEXP (dest, 1))
8538       && GET_CODE (src) == AND
8539       && CONST_INT_P (XEXP (src, 1)))
8540     {
8541       HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
8542       unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
8543       unsigned HOST_WIDE_INT ze_mask;
8544
8545       if (width >= HOST_BITS_PER_WIDE_INT)
8546         ze_mask = -1;
8547       else
8548         ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
8549
8550       /* Complete overlap.  We can remove the source AND.  */
8551       if ((and_mask & ze_mask) == ze_mask)
8552         return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
8553
8554       /* Partial overlap.  We can reduce the source AND.  */
8555       if ((and_mask & ze_mask) != and_mask)
8556         {
8557           mode = GET_MODE (src);
8558           src = gen_rtx_AND (mode, XEXP (src, 0),
8559                              gen_int_mode (and_mask & ze_mask, mode));
8560           return gen_rtx_SET (VOIDmode, dest, src);
8561         }
8562     }
8563
8564   /* The other case we handle is assignments into a constant-position
8565      field.  They look like (ior/xor (and DEST C1) OTHER).  If C1 represents
8566      a mask that has all one bits except for a group of zero bits and
8567      OTHER is known to have zeros where C1 has ones, this is such an
8568      assignment.  Compute the position and length from C1.  Shift OTHER
8569      to the appropriate position, force it to the required mode, and
8570      make the extraction.  Check for the AND in both operands.  */
8571
8572   if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
8573     return x;
8574
8575   rhs = expand_compound_operation (XEXP (src, 0));
8576   lhs = expand_compound_operation (XEXP (src, 1));
8577
8578   if (GET_CODE (rhs) == AND
8579       && CONST_INT_P (XEXP (rhs, 1))
8580       && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
8581     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
8582   else if (GET_CODE (lhs) == AND
8583            && CONST_INT_P (XEXP (lhs, 1))
8584            && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
8585     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
8586   else
8587     return x;
8588
8589   pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
8590   if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
8591       || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
8592       || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
8593     return x;
8594
8595   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
8596   if (assign == 0)
8597     return x;
8598
8599   /* The mode to use for the source is the mode of the assignment, or of
8600      what is inside a possible STRICT_LOW_PART.  */
8601   mode = (GET_CODE (assign) == STRICT_LOW_PART
8602           ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
8603
8604   /* Shift OTHER right POS places and make it the source, restricting it
8605      to the proper length and mode.  */
8606
8607   src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
8608                                                      GET_MODE (src),
8609                                                      other, pos),
8610                                dest);
8611   src = force_to_mode (src, mode,
8612                        GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
8613                        ? ~(unsigned HOST_WIDE_INT) 0
8614                        : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
8615                        0);
8616
8617   /* If SRC is masked by an AND that does not make a difference in
8618      the value being stored, strip it.  */
8619   if (GET_CODE (assign) == ZERO_EXTRACT
8620       && CONST_INT_P (XEXP (assign, 1))
8621       && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
8622       && GET_CODE (src) == AND
8623       && CONST_INT_P (XEXP (src, 1))
8624       && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
8625           == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
8626     src = XEXP (src, 0);
8627
8628   return gen_rtx_SET (VOIDmode, assign, src);
8629 }
8630 \f
8631 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
8632    if so.  */
8633
8634 static rtx
8635 apply_distributive_law (rtx x)
8636 {
8637   enum rtx_code code = GET_CODE (x);
8638   enum rtx_code inner_code;
8639   rtx lhs, rhs, other;
8640   rtx tem;
8641
8642   /* Distributivity is not true for floating point as it can change the
8643      value.  So we don't do it unless -funsafe-math-optimizations.  */
8644   if (FLOAT_MODE_P (GET_MODE (x))
8645       && ! flag_unsafe_math_optimizations)
8646     return x;
8647
8648   /* The outer operation can only be one of the following:  */
8649   if (code != IOR && code != AND && code != XOR
8650       && code != PLUS && code != MINUS)
8651     return x;
8652
8653   lhs = XEXP (x, 0);
8654   rhs = XEXP (x, 1);
8655
8656   /* If either operand is a primitive we can't do anything, so get out
8657      fast.  */
8658   if (OBJECT_P (lhs) || OBJECT_P (rhs))
8659     return x;
8660
8661   lhs = expand_compound_operation (lhs);
8662   rhs = expand_compound_operation (rhs);
8663   inner_code = GET_CODE (lhs);
8664   if (inner_code != GET_CODE (rhs))
8665     return x;
8666
8667   /* See if the inner and outer operations distribute.  */
8668   switch (inner_code)
8669     {
8670     case LSHIFTRT:
8671     case ASHIFTRT:
8672     case AND:
8673     case IOR:
8674       /* These all distribute except over PLUS.  */
8675       if (code == PLUS || code == MINUS)
8676         return x;
8677       break;
8678
8679     case MULT:
8680       if (code != PLUS && code != MINUS)
8681         return x;
8682       break;
8683
8684     case ASHIFT:
8685       /* This is also a multiply, so it distributes over everything.  */
8686       break;
8687
8688     case SUBREG:
8689       /* Non-paradoxical SUBREGs distributes over all operations,
8690          provided the inner modes and byte offsets are the same, this
8691          is an extraction of a low-order part, we don't convert an fp
8692          operation to int or vice versa, this is not a vector mode,
8693          and we would not be converting a single-word operation into a
8694          multi-word operation.  The latter test is not required, but
8695          it prevents generating unneeded multi-word operations.  Some
8696          of the previous tests are redundant given the latter test,
8697          but are retained because they are required for correctness.
8698
8699          We produce the result slightly differently in this case.  */
8700
8701       if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
8702           || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
8703           || ! subreg_lowpart_p (lhs)
8704           || (GET_MODE_CLASS (GET_MODE (lhs))
8705               != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
8706           || (GET_MODE_SIZE (GET_MODE (lhs))
8707               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
8708           || VECTOR_MODE_P (GET_MODE (lhs))
8709           || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD
8710           /* Result might need to be truncated.  Don't change mode if
8711              explicit truncation is needed.  */
8712           || !TRULY_NOOP_TRUNCATION
8713                (GET_MODE_BITSIZE (GET_MODE (x)),
8714                 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (lhs)))))
8715         return x;
8716
8717       tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
8718                                  SUBREG_REG (lhs), SUBREG_REG (rhs));
8719       return gen_lowpart (GET_MODE (x), tem);
8720
8721     default:
8722       return x;
8723     }
8724
8725   /* Set LHS and RHS to the inner operands (A and B in the example
8726      above) and set OTHER to the common operand (C in the example).
8727      There is only one way to do this unless the inner operation is
8728      commutative.  */
8729   if (COMMUTATIVE_ARITH_P (lhs)
8730       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
8731     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
8732   else if (COMMUTATIVE_ARITH_P (lhs)
8733            && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
8734     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
8735   else if (COMMUTATIVE_ARITH_P (lhs)
8736            && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
8737     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
8738   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
8739     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
8740   else
8741     return x;
8742
8743   /* Form the new inner operation, seeing if it simplifies first.  */
8744   tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
8745
8746   /* There is one exception to the general way of distributing:
8747      (a | c) ^ (b | c) -> (a ^ b) & ~c  */
8748   if (code == XOR && inner_code == IOR)
8749     {
8750       inner_code = AND;
8751       other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
8752     }
8753
8754   /* We may be able to continuing distributing the result, so call
8755      ourselves recursively on the inner operation before forming the
8756      outer operation, which we return.  */
8757   return simplify_gen_binary (inner_code, GET_MODE (x),
8758                               apply_distributive_law (tem), other);
8759 }
8760
8761 /* See if X is of the form (* (+ A B) C), and if so convert to
8762    (+ (* A C) (* B C)) and try to simplify.
8763
8764    Most of the time, this results in no change.  However, if some of
8765    the operands are the same or inverses of each other, simplifications
8766    will result.
8767
8768    For example, (and (ior A B) (not B)) can occur as the result of
8769    expanding a bit field assignment.  When we apply the distributive
8770    law to this, we get (ior (and (A (not B))) (and (B (not B)))),
8771    which then simplifies to (and (A (not B))).
8772
8773    Note that no checks happen on the validity of applying the inverse
8774    distributive law.  This is pointless since we can do it in the
8775    few places where this routine is called.
8776
8777    N is the index of the term that is decomposed (the arithmetic operation,
8778    i.e. (+ A B) in the first example above).  !N is the index of the term that
8779    is distributed, i.e. of C in the first example above.  */
8780 static rtx
8781 distribute_and_simplify_rtx (rtx x, int n)
8782 {
8783   enum machine_mode mode;
8784   enum rtx_code outer_code, inner_code;
8785   rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
8786
8787   /* Distributivity is not true for floating point as it can change the
8788      value.  So we don't do it unless -funsafe-math-optimizations.  */
8789   if (FLOAT_MODE_P (GET_MODE (x))
8790       && ! flag_unsafe_math_optimizations)
8791     return NULL_RTX;
8792
8793   decomposed = XEXP (x, n);
8794   if (!ARITHMETIC_P (decomposed))
8795     return NULL_RTX;
8796
8797   mode = GET_MODE (x);
8798   outer_code = GET_CODE (x);
8799   distributed = XEXP (x, !n);
8800
8801   inner_code = GET_CODE (decomposed);
8802   inner_op0 = XEXP (decomposed, 0);
8803   inner_op1 = XEXP (decomposed, 1);
8804
8805   /* Special case (and (xor B C) (not A)), which is equivalent to
8806      (xor (ior A B) (ior A C))  */
8807   if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
8808     {
8809       distributed = XEXP (distributed, 0);
8810       outer_code = IOR;
8811     }
8812
8813   if (n == 0)
8814     {
8815       /* Distribute the second term.  */
8816       new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
8817       new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
8818     }
8819   else
8820     {
8821       /* Distribute the first term.  */
8822       new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
8823       new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
8824     }
8825
8826   tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
8827                                                      new_op0, new_op1));
8828   if (GET_CODE (tmp) != outer_code
8829       && rtx_cost (tmp, SET, optimize_this_for_speed_p)
8830          < rtx_cost (x, SET, optimize_this_for_speed_p))
8831     return tmp;
8832
8833   return NULL_RTX;
8834 }
8835 \f
8836 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
8837    in MODE.  Return an equivalent form, if different from (and VAROP
8838    (const_int CONSTOP)).  Otherwise, return NULL_RTX.  */
8839
8840 static rtx
8841 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
8842                           unsigned HOST_WIDE_INT constop)
8843 {
8844   unsigned HOST_WIDE_INT nonzero;
8845   unsigned HOST_WIDE_INT orig_constop;
8846   rtx orig_varop;
8847   int i;
8848
8849   orig_varop = varop;
8850   orig_constop = constop;
8851   if (GET_CODE (varop) == CLOBBER)
8852     return NULL_RTX;
8853
8854   /* Simplify VAROP knowing that we will be only looking at some of the
8855      bits in it.
8856
8857      Note by passing in CONSTOP, we guarantee that the bits not set in
8858      CONSTOP are not significant and will never be examined.  We must
8859      ensure that is the case by explicitly masking out those bits
8860      before returning.  */
8861   varop = force_to_mode (varop, mode, constop, 0);
8862
8863   /* If VAROP is a CLOBBER, we will fail so return it.  */
8864   if (GET_CODE (varop) == CLOBBER)
8865     return varop;
8866
8867   /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
8868      to VAROP and return the new constant.  */
8869   if (CONST_INT_P (varop))
8870     return gen_int_mode (INTVAL (varop) & constop, mode);
8871
8872   /* See what bits may be nonzero in VAROP.  Unlike the general case of
8873      a call to nonzero_bits, here we don't care about bits outside
8874      MODE.  */
8875
8876   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
8877
8878   /* Turn off all bits in the constant that are known to already be zero.
8879      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
8880      which is tested below.  */
8881
8882   constop &= nonzero;
8883
8884   /* If we don't have any bits left, return zero.  */
8885   if (constop == 0)
8886     return const0_rtx;
8887
8888   /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
8889      a power of two, we can replace this with an ASHIFT.  */
8890   if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
8891       && (i = exact_log2 (constop)) >= 0)
8892     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
8893
8894   /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8895      or XOR, then try to apply the distributive law.  This may eliminate
8896      operations if either branch can be simplified because of the AND.
8897      It may also make some cases more complex, but those cases probably
8898      won't match a pattern either with or without this.  */
8899
8900   if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
8901     return
8902       gen_lowpart
8903         (mode,
8904          apply_distributive_law
8905          (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
8906                                simplify_and_const_int (NULL_RTX,
8907                                                        GET_MODE (varop),
8908                                                        XEXP (varop, 0),
8909                                                        constop),
8910                                simplify_and_const_int (NULL_RTX,
8911                                                        GET_MODE (varop),
8912                                                        XEXP (varop, 1),
8913                                                        constop))));
8914
8915   /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
8916      the AND and see if one of the operands simplifies to zero.  If so, we
8917      may eliminate it.  */
8918
8919   if (GET_CODE (varop) == PLUS
8920       && exact_log2 (constop + 1) >= 0)
8921     {
8922       rtx o0, o1;
8923
8924       o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
8925       o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
8926       if (o0 == const0_rtx)
8927         return o1;
8928       if (o1 == const0_rtx)
8929         return o0;
8930     }
8931
8932   /* Make a SUBREG if necessary.  If we can't make it, fail.  */
8933   varop = gen_lowpart (mode, varop);
8934   if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
8935     return NULL_RTX;
8936
8937   /* If we are only masking insignificant bits, return VAROP.  */
8938   if (constop == nonzero)
8939     return varop;
8940
8941   if (varop == orig_varop && constop == orig_constop)
8942     return NULL_RTX;
8943
8944   /* Otherwise, return an AND.  */
8945   return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
8946 }
8947
8948
8949 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
8950    in MODE.
8951
8952    Return an equivalent form, if different from X.  Otherwise, return X.  If
8953    X is zero, we are to always construct the equivalent form.  */
8954
8955 static rtx
8956 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
8957                         unsigned HOST_WIDE_INT constop)
8958 {
8959   rtx tem = simplify_and_const_int_1 (mode, varop, constop);
8960   if (tem)
8961     return tem;
8962
8963   if (!x)
8964     x = simplify_gen_binary (AND, GET_MODE (varop), varop,
8965                              gen_int_mode (constop, mode));
8966   if (GET_MODE (x) != mode)
8967     x = gen_lowpart (mode, x);
8968   return x;
8969 }
8970 \f
8971 /* Given a REG, X, compute which bits in X can be nonzero.
8972    We don't care about bits outside of those defined in MODE.
8973
8974    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8975    a shift, AND, or zero_extract, we can do better.  */
8976
8977 static rtx
8978 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
8979                               const_rtx known_x ATTRIBUTE_UNUSED,
8980                               enum machine_mode known_mode ATTRIBUTE_UNUSED,
8981                               unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
8982                               unsigned HOST_WIDE_INT *nonzero)
8983 {
8984   rtx tem;
8985   reg_stat_type *rsp;
8986
8987   /* If X is a register whose nonzero bits value is current, use it.
8988      Otherwise, if X is a register whose value we can find, use that
8989      value.  Otherwise, use the previously-computed global nonzero bits
8990      for this register.  */
8991
8992   rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
8993   if (rsp->last_set_value != 0
8994       && (rsp->last_set_mode == mode
8995           || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
8996               && GET_MODE_CLASS (mode) == MODE_INT))
8997       && ((rsp->last_set_label >= label_tick_ebb_start
8998            && rsp->last_set_label < label_tick)
8999           || (rsp->last_set_label == label_tick
9000               && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9001           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9002               && REG_N_SETS (REGNO (x)) == 1
9003               && !REGNO_REG_SET_P
9004                   (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
9005     {
9006       *nonzero &= rsp->last_set_nonzero_bits;
9007       return NULL;
9008     }
9009
9010   tem = get_last_value (x);
9011
9012   if (tem)
9013     {
9014 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9015       /* If X is narrower than MODE and TEM is a non-negative
9016          constant that would appear negative in the mode of X,
9017          sign-extend it for use in reg_nonzero_bits because some
9018          machines (maybe most) will actually do the sign-extension
9019          and this is the conservative approach.
9020
9021          ??? For 2.5, try to tighten up the MD files in this regard
9022          instead of this kludge.  */
9023
9024       if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
9025           && CONST_INT_P (tem)
9026           && INTVAL (tem) > 0
9027           && 0 != (INTVAL (tem)
9028                    & ((HOST_WIDE_INT) 1
9029                       << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
9030         tem = GEN_INT (INTVAL (tem)
9031                        | ((HOST_WIDE_INT) (-1)
9032                           << GET_MODE_BITSIZE (GET_MODE (x))));
9033 #endif
9034       return tem;
9035     }
9036   else if (nonzero_sign_valid && rsp->nonzero_bits)
9037     {
9038       unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9039
9040       if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
9041         /* We don't know anything about the upper bits.  */
9042         mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9043       *nonzero &= mask;
9044     }
9045
9046   return NULL;
9047 }
9048
9049 /* Return the number of bits at the high-order end of X that are known to
9050    be equal to the sign bit.  X will be used in mode MODE; if MODE is
9051    VOIDmode, X will be used in its own mode.  The returned value  will always
9052    be between 1 and the number of bits in MODE.  */
9053
9054 static rtx
9055 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
9056                                      const_rtx known_x ATTRIBUTE_UNUSED,
9057                                      enum machine_mode known_mode
9058                                      ATTRIBUTE_UNUSED,
9059                                      unsigned int known_ret ATTRIBUTE_UNUSED,
9060                                      unsigned int *result)
9061 {
9062   rtx tem;
9063   reg_stat_type *rsp;
9064
9065   rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
9066   if (rsp->last_set_value != 0
9067       && rsp->last_set_mode == mode
9068       && ((rsp->last_set_label >= label_tick_ebb_start
9069            && rsp->last_set_label < label_tick)
9070           || (rsp->last_set_label == label_tick
9071               && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9072           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9073               && REG_N_SETS (REGNO (x)) == 1
9074               && !REGNO_REG_SET_P
9075                   (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
9076     {
9077       *result = rsp->last_set_sign_bit_copies;
9078       return NULL;
9079     }
9080
9081   tem = get_last_value (x);
9082   if (tem != 0)
9083     return tem;
9084
9085   if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9086       && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
9087     *result = rsp->sign_bit_copies;
9088
9089   return NULL;
9090 }
9091 \f
9092 /* Return the number of "extended" bits there are in X, when interpreted
9093    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
9094    unsigned quantities, this is the number of high-order zero bits.
9095    For signed quantities, this is the number of copies of the sign bit
9096    minus 1.  In both case, this function returns the number of "spare"
9097    bits.  For example, if two quantities for which this function returns
9098    at least 1 are added, the addition is known not to overflow.
9099
9100    This function will always return 0 unless called during combine, which
9101    implies that it must be called from a define_split.  */
9102
9103 unsigned int
9104 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
9105 {
9106   if (nonzero_sign_valid == 0)
9107     return 0;
9108
9109   return (unsignedp
9110           ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9111              ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
9112                                - floor_log2 (nonzero_bits (x, mode)))
9113              : 0)
9114           : num_sign_bit_copies (x, mode) - 1);
9115 }
9116 \f
9117 /* This function is called from `simplify_shift_const' to merge two
9118    outer operations.  Specifically, we have already found that we need
9119    to perform operation *POP0 with constant *PCONST0 at the outermost
9120    position.  We would now like to also perform OP1 with constant CONST1
9121    (with *POP0 being done last).
9122
9123    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9124    the resulting operation.  *PCOMP_P is set to 1 if we would need to
9125    complement the innermost operand, otherwise it is unchanged.
9126
9127    MODE is the mode in which the operation will be done.  No bits outside
9128    the width of this mode matter.  It is assumed that the width of this mode
9129    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9130
9131    If *POP0 or OP1 are UNKNOWN, it means no operation is required.  Only NEG, PLUS,
9132    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
9133    result is simply *PCONST0.
9134
9135    If the resulting operation cannot be expressed as one operation, we
9136    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
9137
9138 static int
9139 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)
9140 {
9141   enum rtx_code op0 = *pop0;
9142   HOST_WIDE_INT const0 = *pconst0;
9143
9144   const0 &= GET_MODE_MASK (mode);
9145   const1 &= GET_MODE_MASK (mode);
9146
9147   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
9148   if (op0 == AND)
9149     const1 &= const0;
9150
9151   /* If OP0 or OP1 is UNKNOWN, this is easy.  Similarly if they are the same or
9152      if OP0 is SET.  */
9153
9154   if (op1 == UNKNOWN || op0 == SET)
9155     return 1;
9156
9157   else if (op0 == UNKNOWN)
9158     op0 = op1, const0 = const1;
9159
9160   else if (op0 == op1)
9161     {
9162       switch (op0)
9163         {
9164         case AND:
9165           const0 &= const1;
9166           break;
9167         case IOR:
9168           const0 |= const1;
9169           break;
9170         case XOR:
9171           const0 ^= const1;
9172           break;
9173         case PLUS:
9174           const0 += const1;
9175           break;
9176         case NEG:
9177           op0 = UNKNOWN;
9178           break;
9179         default:
9180           break;
9181         }
9182     }
9183
9184   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
9185   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9186     return 0;
9187
9188   /* If the two constants aren't the same, we can't do anything.  The
9189      remaining six cases can all be done.  */
9190   else if (const0 != const1)
9191     return 0;
9192
9193   else
9194     switch (op0)
9195       {
9196       case IOR:
9197         if (op1 == AND)
9198           /* (a & b) | b == b */
9199           op0 = SET;
9200         else /* op1 == XOR */
9201           /* (a ^ b) | b == a | b */
9202           {;}
9203         break;
9204
9205       case XOR:
9206         if (op1 == AND)
9207           /* (a & b) ^ b == (~a) & b */
9208           op0 = AND, *pcomp_p = 1;
9209         else /* op1 == IOR */
9210           /* (a | b) ^ b == a & ~b */
9211           op0 = AND, const0 = ~const0;
9212         break;
9213
9214       case AND:
9215         if (op1 == IOR)
9216           /* (a | b) & b == b */
9217         op0 = SET;
9218         else /* op1 == XOR */
9219           /* (a ^ b) & b) == (~a) & b */
9220           *pcomp_p = 1;
9221         break;
9222       default:
9223         break;
9224       }
9225
9226   /* Check for NO-OP cases.  */
9227   const0 &= GET_MODE_MASK (mode);
9228   if (const0 == 0
9229       && (op0 == IOR || op0 == XOR || op0 == PLUS))
9230     op0 = UNKNOWN;
9231   else if (const0 == 0 && op0 == AND)
9232     op0 = SET;
9233   else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9234            && op0 == AND)
9235     op0 = UNKNOWN;
9236
9237   *pop0 = op0;
9238
9239   /* ??? Slightly redundant with the above mask, but not entirely.
9240      Moving this above means we'd have to sign-extend the mode mask
9241      for the final test.  */
9242   if (op0 != UNKNOWN && op0 != NEG)
9243     *pconst0 = trunc_int_for_mode (const0, mode);
9244
9245   return 1;
9246 }
9247 \f
9248 /* A helper to simplify_shift_const_1 to determine the mode we can perform
9249    the shift in.  The original shift operation CODE is performed on OP in
9250    ORIG_MODE.  Return the wider mode MODE if we can perform the operation
9251    in that mode.  Return ORIG_MODE otherwise.  We can also assume that the
9252    result of the shift is subject to operation OUTER_CODE with operand
9253    OUTER_CONST.  */
9254
9255 static enum machine_mode
9256 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
9257                       enum machine_mode orig_mode, enum machine_mode mode,
9258                       enum rtx_code outer_code, HOST_WIDE_INT outer_const)
9259 {
9260   if (orig_mode == mode)
9261     return mode;
9262   gcc_assert (GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (orig_mode));
9263
9264   /* In general we can't perform in wider mode for right shift and rotate.  */
9265   switch (code)
9266     {
9267     case ASHIFTRT:
9268       /* We can still widen if the bits brought in from the left are identical
9269          to the sign bit of ORIG_MODE.  */
9270       if (num_sign_bit_copies (op, mode)
9271           > (unsigned) (GET_MODE_BITSIZE (mode)
9272                         - GET_MODE_BITSIZE (orig_mode)))
9273         return mode;
9274       return orig_mode;
9275
9276     case LSHIFTRT:
9277       /* Similarly here but with zero bits.  */
9278       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9279           && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
9280         return mode;
9281
9282       /* We can also widen if the bits brought in will be masked off.  This
9283          operation is performed in ORIG_MODE.  */
9284       if (outer_code == AND)
9285         {
9286           int care_bits = low_bitmask_len (orig_mode, outer_const);
9287
9288           if (care_bits >= 0
9289               && GET_MODE_BITSIZE (orig_mode) - care_bits >= count)
9290             return mode;
9291         }
9292       /* fall through */
9293
9294     case ROTATE:
9295       return orig_mode;
9296
9297     case ROTATERT:
9298       gcc_unreachable ();
9299
9300     default:
9301       return mode;
9302     }
9303 }
9304
9305 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
9306    The result of the shift is RESULT_MODE.  Return NULL_RTX if we cannot
9307    simplify it.  Otherwise, return a simplified value.
9308
9309    The shift is normally computed in the widest mode we find in VAROP, as
9310    long as it isn't a different number of words than RESULT_MODE.  Exceptions
9311    are ASHIFTRT and ROTATE, which are always done in their original mode.  */
9312
9313 static rtx
9314 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
9315                         rtx varop, int orig_count)
9316 {
9317   enum rtx_code orig_code = code;
9318   rtx orig_varop = varop;
9319   int count;
9320   enum machine_mode mode = result_mode;
9321   enum machine_mode shift_mode, tmode;
9322   unsigned int mode_words
9323     = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9324   /* We form (outer_op (code varop count) (outer_const)).  */
9325   enum rtx_code outer_op = UNKNOWN;
9326   HOST_WIDE_INT outer_const = 0;
9327   int complement_p = 0;
9328   rtx new_rtx, x;
9329
9330   /* Make sure and truncate the "natural" shift on the way in.  We don't
9331      want to do this inside the loop as it makes it more difficult to
9332      combine shifts.  */
9333   if (SHIFT_COUNT_TRUNCATED)
9334     orig_count &= GET_MODE_BITSIZE (mode) - 1;
9335
9336   /* If we were given an invalid count, don't do anything except exactly
9337      what was requested.  */
9338
9339   if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
9340     return NULL_RTX;
9341
9342   count = orig_count;
9343
9344   /* Unless one of the branches of the `if' in this loop does a `continue',
9345      we will `break' the loop after the `if'.  */
9346
9347   while (count != 0)
9348     {
9349       /* If we have an operand of (clobber (const_int 0)), fail.  */
9350       if (GET_CODE (varop) == CLOBBER)
9351         return NULL_RTX;
9352
9353       /* Convert ROTATERT to ROTATE.  */
9354       if (code == ROTATERT)
9355         {
9356           unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
9357           code = ROTATE;
9358           if (VECTOR_MODE_P (result_mode))
9359             count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9360           else
9361             count = bitsize - count;
9362         }
9363
9364       shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
9365                                          mode, outer_op, outer_const);
9366
9367       /* Handle cases where the count is greater than the size of the mode
9368          minus 1.  For ASHIFT, use the size minus one as the count (this can
9369          occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
9370          take the count modulo the size.  For other shifts, the result is
9371          zero.
9372
9373          Since these shifts are being produced by the compiler by combining
9374          multiple operations, each of which are defined, we know what the
9375          result is supposed to be.  */
9376
9377       if (count > (GET_MODE_BITSIZE (shift_mode) - 1))
9378         {
9379           if (code == ASHIFTRT)
9380             count = GET_MODE_BITSIZE (shift_mode) - 1;
9381           else if (code == ROTATE || code == ROTATERT)
9382             count %= GET_MODE_BITSIZE (shift_mode);
9383           else
9384             {
9385               /* We can't simply return zero because there may be an
9386                  outer op.  */
9387               varop = const0_rtx;
9388               count = 0;
9389               break;
9390             }
9391         }
9392
9393       /* If we discovered we had to complement VAROP, leave.  Making a NOT
9394          here would cause an infinite loop.  */
9395       if (complement_p)
9396         break;
9397
9398       /* An arithmetic right shift of a quantity known to be -1 or 0
9399          is a no-op.  */
9400       if (code == ASHIFTRT
9401           && (num_sign_bit_copies (varop, shift_mode)
9402               == GET_MODE_BITSIZE (shift_mode)))
9403         {
9404           count = 0;
9405           break;
9406         }
9407
9408       /* If we are doing an arithmetic right shift and discarding all but
9409          the sign bit copies, this is equivalent to doing a shift by the
9410          bitsize minus one.  Convert it into that shift because it will often
9411          allow other simplifications.  */
9412
9413       if (code == ASHIFTRT
9414           && (count + num_sign_bit_copies (varop, shift_mode)
9415               >= GET_MODE_BITSIZE (shift_mode)))
9416         count = GET_MODE_BITSIZE (shift_mode) - 1;
9417
9418       /* We simplify the tests below and elsewhere by converting
9419          ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9420          `make_compound_operation' will convert it to an ASHIFTRT for
9421          those machines (such as VAX) that don't have an LSHIFTRT.  */
9422       if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9423           && code == ASHIFTRT
9424           && ((nonzero_bits (varop, shift_mode)
9425                & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
9426               == 0))
9427         code = LSHIFTRT;
9428
9429       if (((code == LSHIFTRT
9430             && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9431             && !(nonzero_bits (varop, shift_mode) >> count))
9432            || (code == ASHIFT
9433                && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9434                && !((nonzero_bits (varop, shift_mode) << count)
9435                     & GET_MODE_MASK (shift_mode))))
9436           && !side_effects_p (varop))
9437         varop = const0_rtx;
9438
9439       switch (GET_CODE (varop))
9440         {
9441         case SIGN_EXTEND:
9442         case ZERO_EXTEND:
9443         case SIGN_EXTRACT:
9444         case ZERO_EXTRACT:
9445           new_rtx = expand_compound_operation (varop);
9446           if (new_rtx != varop)
9447             {
9448               varop = new_rtx;
9449               continue;
9450             }
9451           break;
9452
9453         case MEM:
9454           /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9455              minus the width of a smaller mode, we can do this with a
9456              SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
9457           if ((code == ASHIFTRT || code == LSHIFTRT)
9458               && ! mode_dependent_address_p (XEXP (varop, 0))
9459               && ! MEM_VOLATILE_P (varop)
9460               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9461                                          MODE_INT, 1)) != BLKmode)
9462             {
9463               new_rtx = adjust_address_nv (varop, tmode,
9464                                        BYTES_BIG_ENDIAN ? 0
9465                                        : count / BITS_PER_UNIT);
9466
9467               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9468                                      : ZERO_EXTEND, mode, new_rtx);
9469               count = 0;
9470               continue;
9471             }
9472           break;
9473
9474         case SUBREG:
9475           /* If VAROP is a SUBREG, strip it as long as the inner operand has
9476              the same number of words as what we've seen so far.  Then store
9477              the widest mode in MODE.  */
9478           if (subreg_lowpart_p (varop)
9479               && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9480                   > GET_MODE_SIZE (GET_MODE (varop)))
9481               && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9482                                   + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9483                  == mode_words)
9484             {
9485               varop = SUBREG_REG (varop);
9486               if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9487                 mode = GET_MODE (varop);
9488               continue;
9489             }
9490           break;
9491
9492         case MULT:
9493           /* Some machines use MULT instead of ASHIFT because MULT
9494              is cheaper.  But it is still better on those machines to
9495              merge two shifts into one.  */
9496           if (CONST_INT_P (XEXP (varop, 1))
9497               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9498             {
9499               varop
9500                 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
9501                                        XEXP (varop, 0),
9502                                        GEN_INT (exact_log2 (
9503                                                 INTVAL (XEXP (varop, 1)))));
9504               continue;
9505             }
9506           break;
9507
9508         case UDIV:
9509           /* Similar, for when divides are cheaper.  */
9510           if (CONST_INT_P (XEXP (varop, 1))
9511               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9512             {
9513               varop
9514                 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
9515                                        XEXP (varop, 0),
9516                                        GEN_INT (exact_log2 (
9517                                                 INTVAL (XEXP (varop, 1)))));
9518               continue;
9519             }
9520           break;
9521
9522         case ASHIFTRT:
9523           /* If we are extracting just the sign bit of an arithmetic
9524              right shift, that shift is not needed.  However, the sign
9525              bit of a wider mode may be different from what would be
9526              interpreted as the sign bit in a narrower mode, so, if
9527              the result is narrower, don't discard the shift.  */
9528           if (code == LSHIFTRT
9529               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9530               && (GET_MODE_BITSIZE (result_mode)
9531                   >= GET_MODE_BITSIZE (GET_MODE (varop))))
9532             {
9533               varop = XEXP (varop, 0);
9534               continue;
9535             }
9536
9537           /* ... fall through ...  */
9538
9539         case LSHIFTRT:
9540         case ASHIFT:
9541         case ROTATE:
9542           /* Here we have two nested shifts.  The result is usually the
9543              AND of a new shift with a mask.  We compute the result below.  */
9544           if (CONST_INT_P (XEXP (varop, 1))
9545               && INTVAL (XEXP (varop, 1)) >= 0
9546               && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9547               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9548               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9549               && !VECTOR_MODE_P (result_mode))
9550             {
9551               enum rtx_code first_code = GET_CODE (varop);
9552               unsigned int first_count = INTVAL (XEXP (varop, 1));
9553               unsigned HOST_WIDE_INT mask;
9554               rtx mask_rtx;
9555
9556               /* We have one common special case.  We can't do any merging if
9557                  the inner code is an ASHIFTRT of a smaller mode.  However, if
9558                  we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9559                  with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9560                  we can convert it to
9561                  (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9562                  This simplifies certain SIGN_EXTEND operations.  */
9563               if (code == ASHIFT && first_code == ASHIFTRT
9564                   && count == (GET_MODE_BITSIZE (result_mode)
9565                                - GET_MODE_BITSIZE (GET_MODE (varop))))
9566                 {
9567                   /* C3 has the low-order C1 bits zero.  */
9568
9569                   mask = (GET_MODE_MASK (mode)
9570                           & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
9571
9572                   varop = simplify_and_const_int (NULL_RTX, result_mode,
9573                                                   XEXP (varop, 0), mask);
9574                   varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
9575                                                 varop, count);
9576                   count = first_count;
9577                   code = ASHIFTRT;
9578                   continue;
9579                 }
9580
9581               /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
9582                  than C1 high-order bits equal to the sign bit, we can convert
9583                  this to either an ASHIFT or an ASHIFTRT depending on the
9584                  two counts.
9585
9586                  We cannot do this if VAROP's mode is not SHIFT_MODE.  */
9587
9588               if (code == ASHIFTRT && first_code == ASHIFT
9589                   && GET_MODE (varop) == shift_mode
9590                   && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
9591                       > first_count))
9592                 {
9593                   varop = XEXP (varop, 0);
9594                   count -= first_count;
9595                   if (count < 0)
9596                     {
9597                       count = -count;
9598                       code = ASHIFT;
9599                     }
9600
9601                   continue;
9602                 }
9603
9604               /* There are some cases we can't do.  If CODE is ASHIFTRT,
9605                  we can only do this if FIRST_CODE is also ASHIFTRT.
9606
9607                  We can't do the case when CODE is ROTATE and FIRST_CODE is
9608                  ASHIFTRT.
9609
9610                  If the mode of this shift is not the mode of the outer shift,
9611                  we can't do this if either shift is a right shift or ROTATE.
9612
9613                  Finally, we can't do any of these if the mode is too wide
9614                  unless the codes are the same.
9615
9616                  Handle the case where the shift codes are the same
9617                  first.  */
9618
9619               if (code == first_code)
9620                 {
9621                   if (GET_MODE (varop) != result_mode
9622                       && (code == ASHIFTRT || code == LSHIFTRT
9623                           || code == ROTATE))
9624                     break;
9625
9626                   count += first_count;
9627                   varop = XEXP (varop, 0);
9628                   continue;
9629                 }
9630
9631               if (code == ASHIFTRT
9632                   || (code == ROTATE && first_code == ASHIFTRT)
9633                   || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
9634                   || (GET_MODE (varop) != result_mode
9635                       && (first_code == ASHIFTRT || first_code == LSHIFTRT
9636                           || first_code == ROTATE
9637                           || code == ROTATE)))
9638                 break;
9639
9640               /* To compute the mask to apply after the shift, shift the
9641                  nonzero bits of the inner shift the same way the
9642                  outer shift will.  */
9643
9644               mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
9645
9646               mask_rtx
9647                 = simplify_const_binary_operation (code, result_mode, mask_rtx,
9648                                                    GEN_INT (count));
9649
9650               /* Give up if we can't compute an outer operation to use.  */
9651               if (mask_rtx == 0
9652                   || !CONST_INT_P (mask_rtx)
9653                   || ! merge_outer_ops (&outer_op, &outer_const, AND,
9654                                         INTVAL (mask_rtx),
9655                                         result_mode, &complement_p))
9656                 break;
9657
9658               /* If the shifts are in the same direction, we add the
9659                  counts.  Otherwise, we subtract them.  */
9660               if ((code == ASHIFTRT || code == LSHIFTRT)
9661                   == (first_code == ASHIFTRT || first_code == LSHIFTRT))
9662                 count += first_count;
9663               else
9664                 count -= first_count;
9665
9666               /* If COUNT is positive, the new shift is usually CODE,
9667                  except for the two exceptions below, in which case it is
9668                  FIRST_CODE.  If the count is negative, FIRST_CODE should
9669                  always be used  */
9670               if (count > 0
9671                   && ((first_code == ROTATE && code == ASHIFT)
9672                       || (first_code == ASHIFTRT && code == LSHIFTRT)))
9673                 code = first_code;
9674               else if (count < 0)
9675                 code = first_code, count = -count;
9676
9677               varop = XEXP (varop, 0);
9678               continue;
9679             }
9680
9681           /* If we have (A << B << C) for any shift, we can convert this to
9682              (A << C << B).  This wins if A is a constant.  Only try this if
9683              B is not a constant.  */
9684
9685           else if (GET_CODE (varop) == code
9686                    && CONST_INT_P (XEXP (varop, 0))
9687                    && !CONST_INT_P (XEXP (varop, 1)))
9688             {
9689               rtx new_rtx = simplify_const_binary_operation (code, mode,
9690                                                          XEXP (varop, 0),
9691                                                          GEN_INT (count));
9692               varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
9693               count = 0;
9694               continue;
9695             }
9696           break;
9697
9698         case NOT:
9699           if (VECTOR_MODE_P (mode))
9700             break;
9701
9702           /* Make this fit the case below.  */
9703           varop = gen_rtx_XOR (mode, XEXP (varop, 0),
9704                                GEN_INT (GET_MODE_MASK (mode)));
9705           continue;
9706
9707         case IOR:
9708         case AND:
9709         case XOR:
9710           /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9711              with C the size of VAROP - 1 and the shift is logical if
9712              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9713              we have an (le X 0) operation.   If we have an arithmetic shift
9714              and STORE_FLAG_VALUE is 1 or we have a logical shift with
9715              STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
9716
9717           if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
9718               && XEXP (XEXP (varop, 0), 1) == constm1_rtx
9719               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9720               && (code == LSHIFTRT || code == ASHIFTRT)
9721               && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9722               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9723             {
9724               count = 0;
9725               varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
9726                                   const0_rtx);
9727
9728               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9729                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9730
9731               continue;
9732             }
9733
9734           /* If we have (shift (logical)), move the logical to the outside
9735              to allow it to possibly combine with another logical and the
9736              shift to combine with another shift.  This also canonicalizes to
9737              what a ZERO_EXTRACT looks like.  Also, some machines have
9738              (and (shift)) insns.  */
9739
9740           if (CONST_INT_P (XEXP (varop, 1))
9741               /* We can't do this if we have (ashiftrt (xor))  and the
9742                  constant has its sign bit set in shift_mode.  */
9743               && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9744                    && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9745                                               shift_mode))
9746               && (new_rtx = simplify_const_binary_operation (code, result_mode,
9747                                                          XEXP (varop, 1),
9748                                                          GEN_INT (count))) != 0
9749               && CONST_INT_P (new_rtx)
9750               && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9751                                   INTVAL (new_rtx), result_mode, &complement_p))
9752             {
9753               varop = XEXP (varop, 0);
9754               continue;
9755             }
9756
9757           /* If we can't do that, try to simplify the shift in each arm of the
9758              logical expression, make a new logical expression, and apply
9759              the inverse distributive law.  This also can't be done
9760              for some (ashiftrt (xor)).  */
9761           if (CONST_INT_P (XEXP (varop, 1))
9762              && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9763                   && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9764                                              shift_mode)))
9765             {
9766               rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9767                                               XEXP (varop, 0), count);
9768               rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9769                                               XEXP (varop, 1), count);
9770
9771               varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
9772                                            lhs, rhs);
9773               varop = apply_distributive_law (varop);
9774
9775               count = 0;
9776               continue;
9777             }
9778           break;
9779
9780         case EQ:
9781           /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9782              says that the sign bit can be tested, FOO has mode MODE, C is
9783              GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9784              that may be nonzero.  */
9785           if (code == LSHIFTRT
9786               && XEXP (varop, 1) == const0_rtx
9787               && GET_MODE (XEXP (varop, 0)) == result_mode
9788               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9789               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9790               && STORE_FLAG_VALUE == -1
9791               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9792               && merge_outer_ops (&outer_op, &outer_const, XOR,
9793                                   (HOST_WIDE_INT) 1, result_mode,
9794                                   &complement_p))
9795             {
9796               varop = XEXP (varop, 0);
9797               count = 0;
9798               continue;
9799             }
9800           break;
9801
9802         case NEG:
9803           /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9804              than the number of bits in the mode is equivalent to A.  */
9805           if (code == LSHIFTRT
9806               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9807               && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9808             {
9809               varop = XEXP (varop, 0);
9810               count = 0;
9811               continue;
9812             }
9813
9814           /* NEG commutes with ASHIFT since it is multiplication.  Move the
9815              NEG outside to allow shifts to combine.  */
9816           if (code == ASHIFT
9817               && merge_outer_ops (&outer_op, &outer_const, NEG,
9818                                   (HOST_WIDE_INT) 0, result_mode,
9819                                   &complement_p))
9820             {
9821               varop = XEXP (varop, 0);
9822               continue;
9823             }
9824           break;
9825
9826         case PLUS:
9827           /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9828              is one less than the number of bits in the mode is
9829              equivalent to (xor A 1).  */
9830           if (code == LSHIFTRT
9831               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9832               && XEXP (varop, 1) == constm1_rtx
9833               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9834               && merge_outer_ops (&outer_op, &outer_const, XOR,
9835                                   (HOST_WIDE_INT) 1, result_mode,
9836                                   &complement_p))
9837             {
9838               count = 0;
9839               varop = XEXP (varop, 0);
9840               continue;
9841             }
9842
9843           /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9844              that might be nonzero in BAR are those being shifted out and those
9845              bits are known zero in FOO, we can replace the PLUS with FOO.
9846              Similarly in the other operand order.  This code occurs when
9847              we are computing the size of a variable-size array.  */
9848
9849           if ((code == ASHIFTRT || code == LSHIFTRT)
9850               && count < HOST_BITS_PER_WIDE_INT
9851               && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9852               && (nonzero_bits (XEXP (varop, 1), result_mode)
9853                   & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9854             {
9855               varop = XEXP (varop, 0);
9856               continue;
9857             }
9858           else if ((code == ASHIFTRT || code == LSHIFTRT)
9859                    && count < HOST_BITS_PER_WIDE_INT
9860                    && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9861                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9862                             >> count)
9863                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9864                             & nonzero_bits (XEXP (varop, 1),
9865                                                  result_mode)))
9866             {
9867               varop = XEXP (varop, 1);
9868               continue;
9869             }
9870
9871           /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
9872           if (code == ASHIFT
9873               && CONST_INT_P (XEXP (varop, 1))
9874               && (new_rtx = simplify_const_binary_operation (ASHIFT, result_mode,
9875                                                          XEXP (varop, 1),
9876                                                          GEN_INT (count))) != 0
9877               && CONST_INT_P (new_rtx)
9878               && merge_outer_ops (&outer_op, &outer_const, PLUS,
9879                                   INTVAL (new_rtx), result_mode, &complement_p))
9880             {
9881               varop = XEXP (varop, 0);
9882               continue;
9883             }
9884
9885           /* Check for 'PLUS signbit', which is the canonical form of 'XOR
9886              signbit', and attempt to change the PLUS to an XOR and move it to
9887              the outer operation as is done above in the AND/IOR/XOR case
9888              leg for shift(logical). See details in logical handling above
9889              for reasoning in doing so.  */
9890           if (code == LSHIFTRT
9891               && CONST_INT_P (XEXP (varop, 1))
9892               && mode_signbit_p (result_mode, XEXP (varop, 1))
9893               && (new_rtx = simplify_const_binary_operation (code, result_mode,
9894                                                          XEXP (varop, 1),
9895                                                          GEN_INT (count))) != 0
9896               && CONST_INT_P (new_rtx)
9897               && merge_outer_ops (&outer_op, &outer_const, XOR,
9898                                   INTVAL (new_rtx), result_mode, &complement_p))
9899             {
9900               varop = XEXP (varop, 0);
9901               continue;
9902             }
9903
9904           break;
9905
9906         case MINUS:
9907           /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9908              with C the size of VAROP - 1 and the shift is logical if
9909              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9910              we have a (gt X 0) operation.  If the shift is arithmetic with
9911              STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9912              we have a (neg (gt X 0)) operation.  */
9913
9914           if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9915               && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9916               && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9917               && (code == LSHIFTRT || code == ASHIFTRT)
9918               && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
9919               && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
9920               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9921             {
9922               count = 0;
9923               varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9924                                   const0_rtx);
9925
9926               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9927                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9928
9929               continue;
9930             }
9931           break;
9932
9933         case TRUNCATE:
9934           /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9935              if the truncate does not affect the value.  */
9936           if (code == LSHIFTRT
9937               && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9938               && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
9939               && (INTVAL (XEXP (XEXP (varop, 0), 1))
9940                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9941                       - GET_MODE_BITSIZE (GET_MODE (varop)))))
9942             {
9943               rtx varop_inner = XEXP (varop, 0);
9944
9945               varop_inner
9946                 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9947                                     XEXP (varop_inner, 0),
9948                                     GEN_INT
9949                                     (count + INTVAL (XEXP (varop_inner, 1))));
9950               varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9951               count = 0;
9952               continue;
9953             }
9954           break;
9955
9956         default:
9957           break;
9958         }
9959
9960       break;
9961     }
9962
9963   shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
9964                                      outer_op, outer_const);
9965
9966   /* We have now finished analyzing the shift.  The result should be
9967      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
9968      OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
9969      to the result of the shift.  OUTER_CONST is the relevant constant,
9970      but we must turn off all bits turned off in the shift.  */
9971
9972   if (outer_op == UNKNOWN
9973       && orig_code == code && orig_count == count
9974       && varop == orig_varop
9975       && shift_mode == GET_MODE (varop))
9976     return NULL_RTX;
9977
9978   /* Make a SUBREG if necessary.  If we can't make it, fail.  */
9979   varop = gen_lowpart (shift_mode, varop);
9980   if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9981     return NULL_RTX;
9982
9983   /* If we have an outer operation and we just made a shift, it is
9984      possible that we could have simplified the shift were it not
9985      for the outer operation.  So try to do the simplification
9986      recursively.  */
9987
9988   if (outer_op != UNKNOWN)
9989     x = simplify_shift_const_1 (code, shift_mode, varop, count);
9990   else
9991     x = NULL_RTX;
9992
9993   if (x == NULL_RTX)
9994     x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
9995
9996   /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9997      turn off all the bits that the shift would have turned off.  */
9998   if (orig_code == LSHIFTRT && result_mode != shift_mode)
9999     x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10000                                 GET_MODE_MASK (result_mode) >> orig_count);
10001
10002   /* Do the remainder of the processing in RESULT_MODE.  */
10003   x = gen_lowpart_or_truncate (result_mode, x);
10004
10005   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10006      operation.  */
10007   if (complement_p)
10008     x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10009
10010   if (outer_op != UNKNOWN)
10011     {
10012       if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10013           && GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
10014         outer_const = trunc_int_for_mode (outer_const, result_mode);
10015
10016       if (outer_op == AND)
10017         x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10018       else if (outer_op == SET)
10019         {
10020           /* This means that we have determined that the result is
10021              equivalent to a constant.  This should be rare.  */
10022           if (!side_effects_p (x))
10023             x = GEN_INT (outer_const);
10024         }
10025       else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10026         x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10027       else
10028         x = simplify_gen_binary (outer_op, result_mode, x,
10029                                  GEN_INT (outer_const));
10030     }
10031
10032   return x;
10033 }
10034
10035 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
10036    The result of the shift is RESULT_MODE.  If we cannot simplify it,
10037    return X or, if it is NULL, synthesize the expression with
10038    simplify_gen_binary.  Otherwise, return a simplified value.
10039
10040    The shift is normally computed in the widest mode we find in VAROP, as
10041    long as it isn't a different number of words than RESULT_MODE.  Exceptions
10042    are ASHIFTRT and ROTATE, which are always done in their original mode.  */
10043
10044 static rtx
10045 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
10046                       rtx varop, int count)
10047 {
10048   rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10049   if (tem)
10050     return tem;
10051
10052   if (!x)
10053     x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10054   if (GET_MODE (x) != result_mode)
10055     x = gen_lowpart (result_mode, x);
10056   return x;
10057 }
10058
10059 \f
10060 /* Like recog, but we receive the address of a pointer to a new pattern.
10061    We try to match the rtx that the pointer points to.
10062    If that fails, we may try to modify or replace the pattern,
10063    storing the replacement into the same pointer object.
10064
10065    Modifications include deletion or addition of CLOBBERs.
10066
10067    PNOTES is a pointer to a location where any REG_UNUSED notes added for
10068    the CLOBBERs are placed.
10069
10070    The value is the final insn code from the pattern ultimately matched,
10071    or -1.  */
10072
10073 static int
10074 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
10075 {
10076   rtx pat = *pnewpat;
10077   int insn_code_number;
10078   int num_clobbers_to_add = 0;
10079   int i;
10080   rtx notes = 0;
10081   rtx old_notes, old_pat;
10082
10083   /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10084      we use to indicate that something didn't match.  If we find such a
10085      thing, force rejection.  */
10086   if (GET_CODE (pat) == PARALLEL)
10087     for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10088       if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10089           && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10090         return -1;
10091
10092   old_pat = PATTERN (insn);
10093   old_notes = REG_NOTES (insn);
10094   PATTERN (insn) = pat;
10095   REG_NOTES (insn) = 0;
10096
10097   insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10098   if (dump_file && (dump_flags & TDF_DETAILS))
10099     {
10100       if (insn_code_number < 0)
10101         fputs ("Failed to match this instruction:\n", dump_file);
10102       else
10103         fputs ("Successfully matched this instruction:\n", dump_file);
10104       print_rtl_single (dump_file, pat);
10105     }
10106
10107   /* If it isn't, there is the possibility that we previously had an insn
10108      that clobbered some register as a side effect, but the combined
10109      insn doesn't need to do that.  So try once more without the clobbers
10110      unless this represents an ASM insn.  */
10111
10112   if (insn_code_number < 0 && ! check_asm_operands (pat)
10113       && GET_CODE (pat) == PARALLEL)
10114     {
10115       int pos;
10116
10117       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10118         if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10119           {
10120             if (i != pos)
10121               SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10122             pos++;
10123           }
10124
10125       SUBST_INT (XVECLEN (pat, 0), pos);
10126
10127       if (pos == 1)
10128         pat = XVECEXP (pat, 0, 0);
10129
10130       PATTERN (insn) = pat;
10131       insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10132       if (dump_file && (dump_flags & TDF_DETAILS))
10133         {
10134           if (insn_code_number < 0)
10135             fputs ("Failed to match this instruction:\n", dump_file);
10136           else
10137             fputs ("Successfully matched this instruction:\n", dump_file);
10138           print_rtl_single (dump_file, pat);
10139         }
10140     }
10141   PATTERN (insn) = old_pat;
10142   REG_NOTES (insn) = old_notes;
10143
10144   /* Recognize all noop sets, these will be killed by followup pass.  */
10145   if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10146     insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10147
10148   /* If we had any clobbers to add, make a new pattern than contains
10149      them.  Then check to make sure that all of them are dead.  */
10150   if (num_clobbers_to_add)
10151     {
10152       rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10153                                      rtvec_alloc (GET_CODE (pat) == PARALLEL
10154                                                   ? (XVECLEN (pat, 0)
10155                                                      + num_clobbers_to_add)
10156                                                   : num_clobbers_to_add + 1));
10157
10158       if (GET_CODE (pat) == PARALLEL)
10159         for (i = 0; i < XVECLEN (pat, 0); i++)
10160           XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10161       else
10162         XVECEXP (newpat, 0, 0) = pat;
10163
10164       add_clobbers (newpat, insn_code_number);
10165
10166       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10167            i < XVECLEN (newpat, 0); i++)
10168         {
10169           if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10170               && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10171             return -1;
10172           if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10173             {
10174               gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10175               notes = alloc_reg_note (REG_UNUSED,
10176                                       XEXP (XVECEXP (newpat, 0, i), 0), notes);
10177             }
10178         }
10179       pat = newpat;
10180     }
10181
10182   *pnewpat = pat;
10183   *pnotes = notes;
10184
10185   return insn_code_number;
10186 }
10187 \f
10188 /* Like gen_lowpart_general but for use by combine.  In combine it
10189    is not possible to create any new pseudoregs.  However, it is
10190    safe to create invalid memory addresses, because combine will
10191    try to recognize them and all they will do is make the combine
10192    attempt fail.
10193
10194    If for some reason this cannot do its job, an rtx
10195    (clobber (const_int 0)) is returned.
10196    An insn containing that will not be recognized.  */
10197
10198 static rtx
10199 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
10200 {
10201   enum machine_mode imode = GET_MODE (x);
10202   unsigned int osize = GET_MODE_SIZE (omode);
10203   unsigned int isize = GET_MODE_SIZE (imode);
10204   rtx result;
10205
10206   if (omode == imode)
10207     return x;
10208
10209   /* Return identity if this is a CONST or symbolic reference.  */
10210   if (omode == Pmode
10211       && (GET_CODE (x) == CONST
10212           || GET_CODE (x) == SYMBOL_REF
10213           || GET_CODE (x) == LABEL_REF))
10214     return x;
10215
10216   /* We can only support MODE being wider than a word if X is a
10217      constant integer or has a mode the same size.  */
10218   if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
10219       && ! ((imode == VOIDmode
10220              && (CONST_INT_P (x)
10221                  || GET_CODE (x) == CONST_DOUBLE))
10222             || isize == osize))
10223     goto fail;
10224
10225   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
10226      won't know what to do.  So we will strip off the SUBREG here and
10227      process normally.  */
10228   if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
10229     {
10230       x = SUBREG_REG (x);
10231
10232       /* For use in case we fall down into the address adjustments
10233          further below, we need to adjust the known mode and size of
10234          x; imode and isize, since we just adjusted x.  */
10235       imode = GET_MODE (x);
10236
10237       if (imode == omode)
10238         return x;
10239
10240       isize = GET_MODE_SIZE (imode);
10241     }
10242
10243   result = gen_lowpart_common (omode, x);
10244
10245   if (result)
10246     return result;
10247
10248   if (MEM_P (x))
10249     {
10250       int offset = 0;
10251
10252       /* Refuse to work on a volatile memory ref or one with a mode-dependent
10253          address.  */
10254       if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
10255         goto fail;
10256
10257       /* If we want to refer to something bigger than the original memref,
10258          generate a paradoxical subreg instead.  That will force a reload
10259          of the original memref X.  */
10260       if (isize < osize)
10261         return gen_rtx_SUBREG (omode, x, 0);
10262
10263       if (WORDS_BIG_ENDIAN)
10264         offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
10265
10266       /* Adjust the address so that the address-after-the-data is
10267          unchanged.  */
10268       if (BYTES_BIG_ENDIAN)
10269         offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
10270
10271       return adjust_address_nv (x, omode, offset);
10272     }
10273
10274   /* If X is a comparison operator, rewrite it in a new mode.  This
10275      probably won't match, but may allow further simplifications.  */
10276   else if (COMPARISON_P (x))
10277     return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
10278
10279   /* If we couldn't simplify X any other way, just enclose it in a
10280      SUBREG.  Normally, this SUBREG won't match, but some patterns may
10281      include an explicit SUBREG or we may simplify it further in combine.  */
10282   else
10283     {
10284       int offset = 0;
10285       rtx res;
10286
10287       offset = subreg_lowpart_offset (omode, imode);
10288       if (imode == VOIDmode)
10289         {
10290           imode = int_mode_for_mode (omode);
10291           x = gen_lowpart_common (imode, x);
10292           if (x == NULL)
10293             goto fail;
10294         }
10295       res = simplify_gen_subreg (omode, x, imode, offset);
10296       if (res)
10297         return res;
10298     }
10299
10300  fail:
10301   return gen_rtx_CLOBBER (omode, const0_rtx);
10302 }
10303 \f
10304 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
10305    comparison code that will be tested.
10306
10307    The result is a possibly different comparison code to use.  *POP0 and
10308    *POP1 may be updated.
10309
10310    It is possible that we might detect that a comparison is either always
10311    true or always false.  However, we do not perform general constant
10312    folding in combine, so this knowledge isn't useful.  Such tautologies
10313    should have been detected earlier.  Hence we ignore all such cases.  */
10314
10315 static enum rtx_code
10316 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
10317 {
10318   rtx op0 = *pop0;
10319   rtx op1 = *pop1;
10320   rtx tem, tem1;
10321   int i;
10322   enum machine_mode mode, tmode;
10323
10324   /* Try a few ways of applying the same transformation to both operands.  */
10325   while (1)
10326     {
10327 #ifndef WORD_REGISTER_OPERATIONS
10328       /* The test below this one won't handle SIGN_EXTENDs on these machines,
10329          so check specially.  */
10330       if (code != GTU && code != GEU && code != LTU && code != LEU
10331           && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
10332           && GET_CODE (XEXP (op0, 0)) == ASHIFT
10333           && GET_CODE (XEXP (op1, 0)) == ASHIFT
10334           && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
10335           && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
10336           && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
10337               == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
10338           && CONST_INT_P (XEXP (op0, 1))
10339           && XEXP (op0, 1) == XEXP (op1, 1)
10340           && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10341           && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
10342           && (INTVAL (XEXP (op0, 1))
10343               == (GET_MODE_BITSIZE (GET_MODE (op0))
10344                   - (GET_MODE_BITSIZE
10345                      (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
10346         {
10347           op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
10348           op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
10349         }
10350 #endif
10351
10352       /* If both operands are the same constant shift, see if we can ignore the
10353          shift.  We can if the shift is a rotate or if the bits shifted out of
10354          this shift are known to be zero for both inputs and if the type of
10355          comparison is compatible with the shift.  */
10356       if (GET_CODE (op0) == GET_CODE (op1)
10357           && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10358           && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
10359               || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
10360                   && (code != GT && code != LT && code != GE && code != LE))
10361               || (GET_CODE (op0) == ASHIFTRT
10362                   && (code != GTU && code != LTU
10363                       && code != GEU && code != LEU)))
10364           && CONST_INT_P (XEXP (op0, 1))
10365           && INTVAL (XEXP (op0, 1)) >= 0
10366           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10367           && XEXP (op0, 1) == XEXP (op1, 1))
10368         {
10369           enum machine_mode mode = GET_MODE (op0);
10370           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10371           int shift_count = INTVAL (XEXP (op0, 1));
10372
10373           if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
10374             mask &= (mask >> shift_count) << shift_count;
10375           else if (GET_CODE (op0) == ASHIFT)
10376             mask = (mask & (mask << shift_count)) >> shift_count;
10377
10378           if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
10379               && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
10380             op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
10381           else
10382             break;
10383         }
10384
10385       /* If both operands are AND's of a paradoxical SUBREG by constant, the
10386          SUBREGs are of the same mode, and, in both cases, the AND would
10387          be redundant if the comparison was done in the narrower mode,
10388          do the comparison in the narrower mode (e.g., we are AND'ing with 1
10389          and the operand's possibly nonzero bits are 0xffffff01; in that case
10390          if we only care about QImode, we don't need the AND).  This case
10391          occurs if the output mode of an scc insn is not SImode and
10392          STORE_FLAG_VALUE == 1 (e.g., the 386).
10393
10394          Similarly, check for a case where the AND's are ZERO_EXTEND
10395          operations from some narrower mode even though a SUBREG is not
10396          present.  */
10397
10398       else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
10399                && CONST_INT_P (XEXP (op0, 1))
10400                && CONST_INT_P (XEXP (op1, 1)))
10401         {
10402           rtx inner_op0 = XEXP (op0, 0);
10403           rtx inner_op1 = XEXP (op1, 0);
10404           HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
10405           HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
10406           int changed = 0;
10407
10408           if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
10409               && (GET_MODE_SIZE (GET_MODE (inner_op0))
10410                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
10411               && (GET_MODE (SUBREG_REG (inner_op0))
10412                   == GET_MODE (SUBREG_REG (inner_op1)))
10413               && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
10414                   <= HOST_BITS_PER_WIDE_INT)
10415               && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
10416                                              GET_MODE (SUBREG_REG (inner_op0)))))
10417               && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
10418                                              GET_MODE (SUBREG_REG (inner_op1))))))
10419             {
10420               op0 = SUBREG_REG (inner_op0);
10421               op1 = SUBREG_REG (inner_op1);
10422
10423               /* The resulting comparison is always unsigned since we masked
10424                  off the original sign bit.  */
10425               code = unsigned_condition (code);
10426
10427               changed = 1;
10428             }
10429
10430           else if (c0 == c1)
10431             for (tmode = GET_CLASS_NARROWEST_MODE
10432                  (GET_MODE_CLASS (GET_MODE (op0)));
10433                  tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10434               if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10435                 {
10436                   op0 = gen_lowpart (tmode, inner_op0);
10437                   op1 = gen_lowpart (tmode, inner_op1);
10438                   code = unsigned_condition (code);
10439                   changed = 1;
10440                   break;
10441                 }
10442
10443           if (! changed)
10444             break;
10445         }
10446
10447       /* If both operands are NOT, we can strip off the outer operation
10448          and adjust the comparison code for swapped operands; similarly for
10449          NEG, except that this must be an equality comparison.  */
10450       else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10451                || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10452                    && (code == EQ || code == NE)))
10453         op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10454
10455       else
10456         break;
10457     }
10458
10459   /* If the first operand is a constant, swap the operands and adjust the
10460      comparison code appropriately, but don't do this if the second operand
10461      is already a constant integer.  */
10462   if (swap_commutative_operands_p (op0, op1))
10463     {
10464       tem = op0, op0 = op1, op1 = tem;
10465       code = swap_condition (code);
10466     }
10467
10468   /* We now enter a loop during which we will try to simplify the comparison.
10469      For the most part, we only are concerned with comparisons with zero,
10470      but some things may really be comparisons with zero but not start
10471      out looking that way.  */
10472
10473   while (CONST_INT_P (op1))
10474     {
10475       enum machine_mode mode = GET_MODE (op0);
10476       unsigned int mode_width = GET_MODE_BITSIZE (mode);
10477       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10478       int equality_comparison_p;
10479       int sign_bit_comparison_p;
10480       int unsigned_comparison_p;
10481       HOST_WIDE_INT const_op;
10482
10483       /* We only want to handle integral modes.  This catches VOIDmode,
10484          CCmode, and the floating-point modes.  An exception is that we
10485          can handle VOIDmode if OP0 is a COMPARE or a comparison
10486          operation.  */
10487
10488       if (GET_MODE_CLASS (mode) != MODE_INT
10489           && ! (mode == VOIDmode
10490                 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
10491         break;
10492
10493       /* Get the constant we are comparing against and turn off all bits
10494          not on in our mode.  */
10495       const_op = INTVAL (op1);
10496       if (mode != VOIDmode)
10497         const_op = trunc_int_for_mode (const_op, mode);
10498       op1 = GEN_INT (const_op);
10499
10500       /* If we are comparing against a constant power of two and the value
10501          being compared can only have that single bit nonzero (e.g., it was
10502          `and'ed with that bit), we can replace this with a comparison
10503          with zero.  */
10504       if (const_op
10505           && (code == EQ || code == NE || code == GE || code == GEU
10506               || code == LT || code == LTU)
10507           && mode_width <= HOST_BITS_PER_WIDE_INT
10508           && exact_log2 (const_op) >= 0
10509           && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10510         {
10511           code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10512           op1 = const0_rtx, const_op = 0;
10513         }
10514
10515       /* Similarly, if we are comparing a value known to be either -1 or
10516          0 with -1, change it to the opposite comparison against zero.  */
10517
10518       if (const_op == -1
10519           && (code == EQ || code == NE || code == GT || code == LE
10520               || code == GEU || code == LTU)
10521           && num_sign_bit_copies (op0, mode) == mode_width)
10522         {
10523           code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10524           op1 = const0_rtx, const_op = 0;
10525         }
10526
10527       /* Do some canonicalizations based on the comparison code.  We prefer
10528          comparisons against zero and then prefer equality comparisons.
10529          If we can reduce the size of a constant, we will do that too.  */
10530
10531       switch (code)
10532         {
10533         case LT:
10534           /* < C is equivalent to <= (C - 1) */
10535           if (const_op > 0)
10536             {
10537               const_op -= 1;
10538               op1 = GEN_INT (const_op);
10539               code = LE;
10540               /* ... fall through to LE case below.  */
10541             }
10542           else
10543             break;
10544
10545         case LE:
10546           /* <= C is equivalent to < (C + 1); we do this for C < 0  */
10547           if (const_op < 0)
10548             {
10549               const_op += 1;
10550               op1 = GEN_INT (const_op);
10551               code = LT;
10552             }
10553
10554           /* If we are doing a <= 0 comparison on a value known to have
10555              a zero sign bit, we can replace this with == 0.  */
10556           else if (const_op == 0
10557                    && mode_width <= HOST_BITS_PER_WIDE_INT
10558                    && (nonzero_bits (op0, mode)
10559                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10560             code = EQ;
10561           break;
10562
10563         case GE:
10564           /* >= C is equivalent to > (C - 1).  */
10565           if (const_op > 0)
10566             {
10567               const_op -= 1;
10568               op1 = GEN_INT (const_op);
10569               code = GT;
10570               /* ... fall through to GT below.  */
10571             }
10572           else
10573             break;
10574
10575         case GT:
10576           /* > C is equivalent to >= (C + 1); we do this for C < 0.  */
10577           if (const_op < 0)
10578             {
10579               const_op += 1;
10580               op1 = GEN_INT (const_op);
10581               code = GE;
10582             }
10583
10584           /* If we are doing a > 0 comparison on a value known to have
10585              a zero sign bit, we can replace this with != 0.  */
10586           else if (const_op == 0
10587                    && mode_width <= HOST_BITS_PER_WIDE_INT
10588                    && (nonzero_bits (op0, mode)
10589                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10590             code = NE;
10591           break;
10592
10593         case LTU:
10594           /* < C is equivalent to <= (C - 1).  */
10595           if (const_op > 0)
10596             {
10597               const_op -= 1;
10598               op1 = GEN_INT (const_op);
10599               code = LEU;
10600               /* ... fall through ...  */
10601             }
10602
10603           /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
10604           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10605                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10606             {
10607               const_op = 0, op1 = const0_rtx;
10608               code = GE;
10609               break;
10610             }
10611           else
10612             break;
10613
10614         case LEU:
10615           /* unsigned <= 0 is equivalent to == 0 */
10616           if (const_op == 0)
10617             code = EQ;
10618
10619           /* (unsigned) <= 0x7fffffff is equivalent to >= 0.  */
10620           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10621                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10622             {
10623               const_op = 0, op1 = const0_rtx;
10624               code = GE;
10625             }
10626           break;
10627
10628         case GEU:
10629           /* >= C is equivalent to > (C - 1).  */
10630           if (const_op > 1)
10631             {
10632               const_op -= 1;
10633               op1 = GEN_INT (const_op);
10634               code = GTU;
10635               /* ... fall through ...  */
10636             }
10637
10638           /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
10639           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10640                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10641             {
10642               const_op = 0, op1 = const0_rtx;
10643               code = LT;
10644               break;
10645             }
10646           else
10647             break;
10648
10649         case GTU:
10650           /* unsigned > 0 is equivalent to != 0 */
10651           if (const_op == 0)
10652             code = NE;
10653
10654           /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
10655           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10656                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10657             {
10658               const_op = 0, op1 = const0_rtx;
10659               code = LT;
10660             }
10661           break;
10662
10663         default:
10664           break;
10665         }
10666
10667       /* Compute some predicates to simplify code below.  */
10668
10669       equality_comparison_p = (code == EQ || code == NE);
10670       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
10671       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
10672                                || code == GEU);
10673
10674       /* If this is a sign bit comparison and we can do arithmetic in
10675          MODE, say that we will only be needing the sign bit of OP0.  */
10676       if (sign_bit_comparison_p
10677           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10678         op0 = force_to_mode (op0, mode,
10679                              ((HOST_WIDE_INT) 1
10680                               << (GET_MODE_BITSIZE (mode) - 1)),
10681                              0);
10682
10683       /* Now try cases based on the opcode of OP0.  If none of the cases
10684          does a "continue", we exit this loop immediately after the
10685          switch.  */
10686
10687       switch (GET_CODE (op0))
10688         {
10689         case ZERO_EXTRACT:
10690           /* If we are extracting a single bit from a variable position in
10691              a constant that has only a single bit set and are comparing it
10692              with zero, we can convert this into an equality comparison
10693              between the position and the location of the single bit.  */
10694           /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
10695              have already reduced the shift count modulo the word size.  */
10696           if (!SHIFT_COUNT_TRUNCATED
10697               && CONST_INT_P (XEXP (op0, 0))
10698               && XEXP (op0, 1) == const1_rtx
10699               && equality_comparison_p && const_op == 0
10700               && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
10701             {
10702               if (BITS_BIG_ENDIAN)
10703                 {
10704                   enum machine_mode new_mode
10705                     = mode_for_extraction (EP_extzv, 1);
10706                   if (new_mode == MAX_MACHINE_MODE)
10707                     i = BITS_PER_WORD - 1 - i;
10708                   else
10709                     {
10710                       mode = new_mode;
10711                       i = (GET_MODE_BITSIZE (mode) - 1 - i);
10712                     }
10713                 }
10714
10715               op0 = XEXP (op0, 2);
10716               op1 = GEN_INT (i);
10717               const_op = i;
10718
10719               /* Result is nonzero iff shift count is equal to I.  */
10720               code = reverse_condition (code);
10721               continue;
10722             }
10723
10724           /* ... fall through ...  */
10725
10726         case SIGN_EXTRACT:
10727           tem = expand_compound_operation (op0);
10728           if (tem != op0)
10729             {
10730               op0 = tem;
10731               continue;
10732             }
10733           break;
10734
10735         case NOT:
10736           /* If testing for equality, we can take the NOT of the constant.  */
10737           if (equality_comparison_p
10738               && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10739             {
10740               op0 = XEXP (op0, 0);
10741               op1 = tem;
10742               continue;
10743             }
10744
10745           /* If just looking at the sign bit, reverse the sense of the
10746              comparison.  */
10747           if (sign_bit_comparison_p)
10748             {
10749               op0 = XEXP (op0, 0);
10750               code = (code == GE ? LT : GE);
10751               continue;
10752             }
10753           break;
10754
10755         case NEG:
10756           /* If testing for equality, we can take the NEG of the constant.  */
10757           if (equality_comparison_p
10758               && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10759             {
10760               op0 = XEXP (op0, 0);
10761               op1 = tem;
10762               continue;
10763             }
10764
10765           /* The remaining cases only apply to comparisons with zero.  */
10766           if (const_op != 0)
10767             break;
10768
10769           /* When X is ABS or is known positive,
10770              (neg X) is < 0 if and only if X != 0.  */
10771
10772           if (sign_bit_comparison_p
10773               && (GET_CODE (XEXP (op0, 0)) == ABS
10774                   || (mode_width <= HOST_BITS_PER_WIDE_INT
10775                       && (nonzero_bits (XEXP (op0, 0), mode)
10776                           & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10777             {
10778               op0 = XEXP (op0, 0);
10779               code = (code == LT ? NE : EQ);
10780               continue;
10781             }
10782
10783           /* If we have NEG of something whose two high-order bits are the
10784              same, we know that "(-a) < 0" is equivalent to "a > 0".  */
10785           if (num_sign_bit_copies (op0, mode) >= 2)
10786             {
10787               op0 = XEXP (op0, 0);
10788               code = swap_condition (code);
10789               continue;
10790             }
10791           break;
10792
10793         case ROTATE:
10794           /* If we are testing equality and our count is a constant, we
10795              can perform the inverse operation on our RHS.  */
10796           if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
10797               && (tem = simplify_binary_operation (ROTATERT, mode,
10798                                                    op1, XEXP (op0, 1))) != 0)
10799             {
10800               op0 = XEXP (op0, 0);
10801               op1 = tem;
10802               continue;
10803             }
10804
10805           /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10806              a particular bit.  Convert it to an AND of a constant of that
10807              bit.  This will be converted into a ZERO_EXTRACT.  */
10808           if (const_op == 0 && sign_bit_comparison_p
10809               && CONST_INT_P (XEXP (op0, 1))
10810               && mode_width <= HOST_BITS_PER_WIDE_INT)
10811             {
10812               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10813                                             ((HOST_WIDE_INT) 1
10814                                              << (mode_width - 1
10815                                                  - INTVAL (XEXP (op0, 1)))));
10816               code = (code == LT ? NE : EQ);
10817               continue;
10818             }
10819
10820           /* Fall through.  */
10821
10822         case ABS:
10823           /* ABS is ignorable inside an equality comparison with zero.  */
10824           if (const_op == 0 && equality_comparison_p)
10825             {
10826               op0 = XEXP (op0, 0);
10827               continue;
10828             }
10829           break;
10830
10831         case SIGN_EXTEND:
10832           /* Can simplify (compare (zero/sign_extend FOO) CONST) to
10833              (compare FOO CONST) if CONST fits in FOO's mode and we
10834              are either testing inequality or have an unsigned
10835              comparison with ZERO_EXTEND or a signed comparison with
10836              SIGN_EXTEND.  But don't do it if we don't have a compare
10837              insn of the given mode, since we'd have to revert it
10838              later on, and then we wouldn't know whether to sign- or
10839              zero-extend.  */
10840           mode = GET_MODE (XEXP (op0, 0));
10841           if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10842               && ! unsigned_comparison_p
10843               && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10844               && ((unsigned HOST_WIDE_INT) const_op
10845                   < (((unsigned HOST_WIDE_INT) 1
10846                       << (GET_MODE_BITSIZE (mode) - 1))))
10847               && have_insn_for (COMPARE, mode))
10848             {
10849               op0 = XEXP (op0, 0);
10850               continue;
10851             }
10852           break;
10853
10854         case SUBREG:
10855           /* Check for the case where we are comparing A - C1 with C2, that is
10856
10857                (subreg:MODE (plus (A) (-C1))) op (C2)
10858
10859              with C1 a constant, and try to lift the SUBREG, i.e. to do the
10860              comparison in the wider mode.  One of the following two conditions
10861              must be true in order for this to be valid:
10862
10863                1. The mode extension results in the same bit pattern being added
10864                   on both sides and the comparison is equality or unsigned.  As
10865                   C2 has been truncated to fit in MODE, the pattern can only be
10866                   all 0s or all 1s.
10867
10868                2. The mode extension results in the sign bit being copied on
10869                   each side.
10870
10871              The difficulty here is that we have predicates for A but not for
10872              (A - C1) so we need to check that C1 is within proper bounds so
10873              as to perturbate A as little as possible.  */
10874
10875           if (mode_width <= HOST_BITS_PER_WIDE_INT
10876               && subreg_lowpart_p (op0)
10877               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
10878               && GET_CODE (SUBREG_REG (op0)) == PLUS
10879               && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
10880             {
10881               enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
10882               rtx a = XEXP (SUBREG_REG (op0), 0);
10883               HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
10884
10885               if ((c1 > 0
10886                    && (unsigned HOST_WIDE_INT) c1
10887                        < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
10888                    && (equality_comparison_p || unsigned_comparison_p)
10889                    /* (A - C1) zero-extends if it is positive and sign-extends
10890                       if it is negative, C2 both zero- and sign-extends.  */
10891                    && ((0 == (nonzero_bits (a, inner_mode)
10892                               & ~GET_MODE_MASK (mode))
10893                         && const_op >= 0)
10894                        /* (A - C1) sign-extends if it is positive and 1-extends
10895                           if it is negative, C2 both sign- and 1-extends.  */
10896                        || (num_sign_bit_copies (a, inner_mode)
10897                            > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10898                                              - mode_width)
10899                            && const_op < 0)))
10900                   || ((unsigned HOST_WIDE_INT) c1
10901                        < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
10902                       /* (A - C1) always sign-extends, like C2.  */
10903                       && num_sign_bit_copies (a, inner_mode)
10904                          > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10905                                            - (mode_width - 1))))
10906                 {
10907                   op0 = SUBREG_REG (op0);
10908                   continue;
10909                 }
10910             }
10911
10912           /* If the inner mode is narrower and we are extracting the low part,
10913              we can treat the SUBREG as if it were a ZERO_EXTEND.  */
10914           if (subreg_lowpart_p (op0)
10915               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10916             /* Fall through */ ;
10917           else
10918             break;
10919
10920           /* ... fall through ...  */
10921
10922         case ZERO_EXTEND:
10923           mode = GET_MODE (XEXP (op0, 0));
10924           if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10925               && (unsigned_comparison_p || equality_comparison_p)
10926               && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10927               && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
10928               && have_insn_for (COMPARE, mode))
10929             {
10930               op0 = XEXP (op0, 0);
10931               continue;
10932             }
10933           break;
10934
10935         case PLUS:
10936           /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
10937              this for equality comparisons due to pathological cases involving
10938              overflows.  */
10939           if (equality_comparison_p
10940               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10941                                                         op1, XEXP (op0, 1))))
10942             {
10943               op0 = XEXP (op0, 0);
10944               op1 = tem;
10945               continue;
10946             }
10947
10948           /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
10949           if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10950               && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10951             {
10952               op0 = XEXP (XEXP (op0, 0), 0);
10953               code = (code == LT ? EQ : NE);
10954               continue;
10955             }
10956           break;
10957
10958         case MINUS:
10959           /* We used to optimize signed comparisons against zero, but that
10960              was incorrect.  Unsigned comparisons against zero (GTU, LEU)
10961              arrive here as equality comparisons, or (GEU, LTU) are
10962              optimized away.  No need to special-case them.  */
10963
10964           /* (eq (minus A B) C) -> (eq A (plus B C)) or
10965              (eq B (minus A C)), whichever simplifies.  We can only do
10966              this for equality comparisons due to pathological cases involving
10967              overflows.  */
10968           if (equality_comparison_p
10969               && 0 != (tem = simplify_binary_operation (PLUS, mode,
10970                                                         XEXP (op0, 1), op1)))
10971             {
10972               op0 = XEXP (op0, 0);
10973               op1 = tem;
10974               continue;
10975             }
10976
10977           if (equality_comparison_p
10978               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10979                                                         XEXP (op0, 0), op1)))
10980             {
10981               op0 = XEXP (op0, 1);
10982               op1 = tem;
10983               continue;
10984             }
10985
10986           /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10987              of bits in X minus 1, is one iff X > 0.  */
10988           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10989               && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
10990               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10991                  == mode_width - 1
10992               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10993             {
10994               op0 = XEXP (op0, 1);
10995               code = (code == GE ? LE : GT);
10996               continue;
10997             }
10998           break;
10999
11000         case XOR:
11001           /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
11002              if C is zero or B is a constant.  */
11003           if (equality_comparison_p
11004               && 0 != (tem = simplify_binary_operation (XOR, mode,
11005                                                         XEXP (op0, 1), op1)))
11006             {
11007               op0 = XEXP (op0, 0);
11008               op1 = tem;
11009               continue;
11010             }
11011           break;
11012
11013         case EQ:  case NE:
11014         case UNEQ:  case LTGT:
11015         case LT:  case LTU:  case UNLT:  case LE:  case LEU:  case UNLE:
11016         case GT:  case GTU:  case UNGT:  case GE:  case GEU:  case UNGE:
11017         case UNORDERED: case ORDERED:
11018           /* We can't do anything if OP0 is a condition code value, rather
11019              than an actual data value.  */
11020           if (const_op != 0
11021               || CC0_P (XEXP (op0, 0))
11022               || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11023             break;
11024
11025           /* Get the two operands being compared.  */
11026           if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11027             tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11028           else
11029             tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11030
11031           /* Check for the cases where we simply want the result of the
11032              earlier test or the opposite of that result.  */
11033           if (code == NE || code == EQ
11034               || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
11035                   && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11036                   && (STORE_FLAG_VALUE
11037                       & (((HOST_WIDE_INT) 1
11038                           << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
11039                   && (code == LT || code == GE)))
11040             {
11041               enum rtx_code new_code;
11042               if (code == LT || code == NE)
11043                 new_code = GET_CODE (op0);
11044               else
11045                 new_code = reversed_comparison_code (op0, NULL);
11046
11047               if (new_code != UNKNOWN)
11048                 {
11049                   code = new_code;
11050                   op0 = tem;
11051                   op1 = tem1;
11052                   continue;
11053                 }
11054             }
11055           break;
11056
11057         case IOR:
11058           /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11059              iff X <= 0.  */
11060           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11061               && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11062               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11063             {
11064               op0 = XEXP (op0, 1);
11065               code = (code == GE ? GT : LE);
11066               continue;
11067             }
11068           break;
11069
11070         case AND:
11071           /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
11072              will be converted to a ZERO_EXTRACT later.  */
11073           if (const_op == 0 && equality_comparison_p
11074               && GET_CODE (XEXP (op0, 0)) == ASHIFT
11075               && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11076             {
11077               op0 = simplify_and_const_int
11078                 (NULL_RTX, mode, gen_rtx_LSHIFTRT (mode,
11079                                                    XEXP (op0, 1),
11080                                                    XEXP (XEXP (op0, 0), 1)),
11081                  (HOST_WIDE_INT) 1);
11082               continue;
11083             }
11084
11085           /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11086              zero and X is a comparison and C1 and C2 describe only bits set
11087              in STORE_FLAG_VALUE, we can compare with X.  */
11088           if (const_op == 0 && equality_comparison_p
11089               && mode_width <= HOST_BITS_PER_WIDE_INT
11090               && CONST_INT_P (XEXP (op0, 1))
11091               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11092               && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11093               && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11094               && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11095             {
11096               mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11097                       << INTVAL (XEXP (XEXP (op0, 0), 1)));
11098               if ((~STORE_FLAG_VALUE & mask) == 0
11099                   && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11100                       || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11101                           && COMPARISON_P (tem))))
11102                 {
11103                   op0 = XEXP (XEXP (op0, 0), 0);
11104                   continue;
11105                 }
11106             }
11107
11108           /* If we are doing an equality comparison of an AND of a bit equal
11109              to the sign bit, replace this with a LT or GE comparison of
11110              the underlying value.  */
11111           if (equality_comparison_p
11112               && const_op == 0
11113               && CONST_INT_P (XEXP (op0, 1))
11114               && mode_width <= HOST_BITS_PER_WIDE_INT
11115               && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11116                   == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11117             {
11118               op0 = XEXP (op0, 0);
11119               code = (code == EQ ? GE : LT);
11120               continue;
11121             }
11122
11123           /* If this AND operation is really a ZERO_EXTEND from a narrower
11124              mode, the constant fits within that mode, and this is either an
11125              equality or unsigned comparison, try to do this comparison in
11126              the narrower mode.
11127
11128              Note that in:
11129
11130              (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11131              -> (ne:DI (reg:SI 4) (const_int 0))
11132
11133              unless TRULY_NOOP_TRUNCATION allows it or the register is
11134              known to hold a value of the required mode the
11135              transformation is invalid.  */
11136           if ((equality_comparison_p || unsigned_comparison_p)
11137               && CONST_INT_P (XEXP (op0, 1))
11138               && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
11139                                    & GET_MODE_MASK (mode))
11140                                   + 1)) >= 0
11141               && const_op >> i == 0
11142               && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11143               && (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
11144                                          GET_MODE_BITSIZE (GET_MODE (op0)))
11145                   || (REG_P (XEXP (op0, 0))
11146                       && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11147             {
11148               op0 = gen_lowpart (tmode, XEXP (op0, 0));
11149               continue;
11150             }
11151
11152           /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11153              fits in both M1 and M2 and the SUBREG is either paradoxical
11154              or represents the low part, permute the SUBREG and the AND
11155              and try again.  */
11156           if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11157             {
11158               unsigned HOST_WIDE_INT c1;
11159               tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11160               /* Require an integral mode, to avoid creating something like
11161                  (AND:SF ...).  */
11162               if (SCALAR_INT_MODE_P (tmode)
11163                   /* It is unsafe to commute the AND into the SUBREG if the
11164                      SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11165                      not defined.  As originally written the upper bits
11166                      have a defined value due to the AND operation.
11167                      However, if we commute the AND inside the SUBREG then
11168                      they no longer have defined values and the meaning of
11169                      the code has been changed.  */
11170                   && (0
11171 #ifdef WORD_REGISTER_OPERATIONS
11172                       || (mode_width > GET_MODE_BITSIZE (tmode)
11173                           && mode_width <= BITS_PER_WORD)
11174 #endif
11175                       || (mode_width <= GET_MODE_BITSIZE (tmode)
11176                           && subreg_lowpart_p (XEXP (op0, 0))))
11177                   && CONST_INT_P (XEXP (op0, 1))
11178                   && mode_width <= HOST_BITS_PER_WIDE_INT
11179                   && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
11180                   && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11181                   && (c1 & ~GET_MODE_MASK (tmode)) == 0
11182                   && c1 != mask
11183                   && c1 != GET_MODE_MASK (tmode))
11184                 {
11185                   op0 = simplify_gen_binary (AND, tmode,
11186                                              SUBREG_REG (XEXP (op0, 0)),
11187                                              gen_int_mode (c1, tmode));
11188                   op0 = gen_lowpart (mode, op0);
11189                   continue;
11190                 }
11191             }
11192
11193           /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0).  */
11194           if (const_op == 0 && equality_comparison_p
11195               && XEXP (op0, 1) == const1_rtx
11196               && GET_CODE (XEXP (op0, 0)) == NOT)
11197             {
11198               op0 = simplify_and_const_int
11199                 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
11200               code = (code == NE ? EQ : NE);
11201               continue;
11202             }
11203
11204           /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11205              (eq (and (lshiftrt X) 1) 0).
11206              Also handle the case where (not X) is expressed using xor.  */
11207           if (const_op == 0 && equality_comparison_p
11208               && XEXP (op0, 1) == const1_rtx
11209               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11210             {
11211               rtx shift_op = XEXP (XEXP (op0, 0), 0);
11212               rtx shift_count = XEXP (XEXP (op0, 0), 1);
11213
11214               if (GET_CODE (shift_op) == NOT
11215                   || (GET_CODE (shift_op) == XOR
11216                       && CONST_INT_P (XEXP (shift_op, 1))
11217                       && CONST_INT_P (shift_count)
11218                       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
11219                       && (INTVAL (XEXP (shift_op, 1))
11220                           == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
11221                 {
11222                   op0 = simplify_and_const_int
11223                     (NULL_RTX, mode,
11224                      gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
11225                      (HOST_WIDE_INT) 1);
11226                   code = (code == NE ? EQ : NE);
11227                   continue;
11228                 }
11229             }
11230           break;
11231
11232         case ASHIFT:
11233           /* If we have (compare (ashift FOO N) (const_int C)) and
11234              the high order N bits of FOO (N+1 if an inequality comparison)
11235              are known to be zero, we can do this by comparing FOO with C
11236              shifted right N bits so long as the low-order N bits of C are
11237              zero.  */
11238           if (CONST_INT_P (XEXP (op0, 1))
11239               && INTVAL (XEXP (op0, 1)) >= 0
11240               && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11241                   < HOST_BITS_PER_WIDE_INT)
11242               && ((const_op
11243                    & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
11244               && mode_width <= HOST_BITS_PER_WIDE_INT
11245               && (nonzero_bits (XEXP (op0, 0), mode)
11246                   & ~(mask >> (INTVAL (XEXP (op0, 1))
11247                                + ! equality_comparison_p))) == 0)
11248             {
11249               /* We must perform a logical shift, not an arithmetic one,
11250                  as we want the top N bits of C to be zero.  */
11251               unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11252
11253               temp >>= INTVAL (XEXP (op0, 1));
11254               op1 = gen_int_mode (temp, mode);
11255               op0 = XEXP (op0, 0);
11256               continue;
11257             }
11258
11259           /* If we are doing a sign bit comparison, it means we are testing
11260              a particular bit.  Convert it to the appropriate AND.  */
11261           if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
11262               && mode_width <= HOST_BITS_PER_WIDE_INT)
11263             {
11264               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11265                                             ((HOST_WIDE_INT) 1
11266                                              << (mode_width - 1
11267                                                  - INTVAL (XEXP (op0, 1)))));
11268               code = (code == LT ? NE : EQ);
11269               continue;
11270             }
11271
11272           /* If this an equality comparison with zero and we are shifting
11273              the low bit to the sign bit, we can convert this to an AND of the
11274              low-order bit.  */
11275           if (const_op == 0 && equality_comparison_p
11276               && CONST_INT_P (XEXP (op0, 1))
11277               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11278                  == mode_width - 1)
11279             {
11280               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11281                                             (HOST_WIDE_INT) 1);
11282               continue;
11283             }
11284           break;
11285
11286         case ASHIFTRT:
11287           /* If this is an equality comparison with zero, we can do this
11288              as a logical shift, which might be much simpler.  */
11289           if (equality_comparison_p && const_op == 0
11290               && CONST_INT_P (XEXP (op0, 1)))
11291             {
11292               op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11293                                           XEXP (op0, 0),
11294                                           INTVAL (XEXP (op0, 1)));
11295               continue;
11296             }
11297
11298           /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11299              do the comparison in a narrower mode.  */
11300           if (! unsigned_comparison_p
11301               && CONST_INT_P (XEXP (op0, 1))
11302               && GET_CODE (XEXP (op0, 0)) == ASHIFT
11303               && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11304               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11305                                          MODE_INT, 1)) != BLKmode
11306               && (((unsigned HOST_WIDE_INT) const_op
11307                    + (GET_MODE_MASK (tmode) >> 1) + 1)
11308                   <= GET_MODE_MASK (tmode)))
11309             {
11310               op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
11311               continue;
11312             }
11313
11314           /* Likewise if OP0 is a PLUS of a sign extension with a
11315              constant, which is usually represented with the PLUS
11316              between the shifts.  */
11317           if (! unsigned_comparison_p
11318               && CONST_INT_P (XEXP (op0, 1))
11319               && GET_CODE (XEXP (op0, 0)) == PLUS
11320               && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11321               && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11322               && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11323               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11324                                          MODE_INT, 1)) != BLKmode
11325               && (((unsigned HOST_WIDE_INT) const_op
11326                    + (GET_MODE_MASK (tmode) >> 1) + 1)
11327                   <= GET_MODE_MASK (tmode)))
11328             {
11329               rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11330               rtx add_const = XEXP (XEXP (op0, 0), 1);
11331               rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
11332                                                    add_const, XEXP (op0, 1));
11333
11334               op0 = simplify_gen_binary (PLUS, tmode,
11335                                          gen_lowpart (tmode, inner),
11336                                          new_const);
11337               continue;
11338             }
11339
11340           /* ... fall through ...  */
11341         case LSHIFTRT:
11342           /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11343              the low order N bits of FOO are known to be zero, we can do this
11344              by comparing FOO with C shifted left N bits so long as no
11345              overflow occurs.  */
11346           if (CONST_INT_P (XEXP (op0, 1))
11347               && INTVAL (XEXP (op0, 1)) >= 0
11348               && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11349               && mode_width <= HOST_BITS_PER_WIDE_INT
11350               && (nonzero_bits (XEXP (op0, 0), mode)
11351                   & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
11352               && (((unsigned HOST_WIDE_INT) const_op
11353                    + (GET_CODE (op0) != LSHIFTRT
11354                       ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11355                          + 1)
11356                       : 0))
11357                   <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11358             {
11359               /* If the shift was logical, then we must make the condition
11360                  unsigned.  */
11361               if (GET_CODE (op0) == LSHIFTRT)
11362                 code = unsigned_condition (code);
11363
11364               const_op <<= INTVAL (XEXP (op0, 1));
11365               op1 = GEN_INT (const_op);
11366               op0 = XEXP (op0, 0);
11367               continue;
11368             }
11369
11370           /* If we are using this shift to extract just the sign bit, we
11371              can replace this with an LT or GE comparison.  */
11372           if (const_op == 0
11373               && (equality_comparison_p || sign_bit_comparison_p)
11374               && CONST_INT_P (XEXP (op0, 1))
11375               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11376                  == mode_width - 1)
11377             {
11378               op0 = XEXP (op0, 0);
11379               code = (code == NE || code == GT ? LT : GE);
11380               continue;
11381             }
11382           break;
11383
11384         default:
11385           break;
11386         }
11387
11388       break;
11389     }
11390
11391   /* Now make any compound operations involved in this comparison.  Then,
11392      check for an outmost SUBREG on OP0 that is not doing anything or is
11393      paradoxical.  The latter transformation must only be performed when
11394      it is known that the "extra" bits will be the same in op0 and op1 or
11395      that they don't matter.  There are three cases to consider:
11396
11397      1. SUBREG_REG (op0) is a register.  In this case the bits are don't
11398      care bits and we can assume they have any convenient value.  So
11399      making the transformation is safe.
11400
11401      2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11402      In this case the upper bits of op0 are undefined.  We should not make
11403      the simplification in that case as we do not know the contents of
11404      those bits.
11405
11406      3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11407      UNKNOWN.  In that case we know those bits are zeros or ones.  We must
11408      also be sure that they are the same as the upper bits of op1.
11409
11410      We can never remove a SUBREG for a non-equality comparison because
11411      the sign bit is in a different place in the underlying object.  */
11412
11413   op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11414   op1 = make_compound_operation (op1, SET);
11415
11416   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11417       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11418       && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11419       && (code == NE || code == EQ))
11420     {
11421       if (GET_MODE_SIZE (GET_MODE (op0))
11422           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
11423         {
11424           /* For paradoxical subregs, allow case 1 as above.  Case 3 isn't
11425              implemented.  */
11426           if (REG_P (SUBREG_REG (op0)))
11427             {
11428               op0 = SUBREG_REG (op0);
11429               op1 = gen_lowpart (GET_MODE (op0), op1);
11430             }
11431         }
11432       else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11433                 <= HOST_BITS_PER_WIDE_INT)
11434                && (nonzero_bits (SUBREG_REG (op0),
11435                                  GET_MODE (SUBREG_REG (op0)))
11436                    & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11437         {
11438           tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11439
11440           if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11441                & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11442             op0 = SUBREG_REG (op0), op1 = tem;
11443         }
11444     }
11445
11446   /* We now do the opposite procedure: Some machines don't have compare
11447      insns in all modes.  If OP0's mode is an integer mode smaller than a
11448      word and we can't do a compare in that mode, see if there is a larger
11449      mode for which we can do the compare.  There are a number of cases in
11450      which we can use the wider mode.  */
11451
11452   mode = GET_MODE (op0);
11453   if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11454       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11455       && ! have_insn_for (COMPARE, mode))
11456     for (tmode = GET_MODE_WIDER_MODE (mode);
11457          (tmode != VOIDmode
11458           && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11459          tmode = GET_MODE_WIDER_MODE (tmode))
11460       if (have_insn_for (COMPARE, tmode))
11461         {
11462           int zero_extended;
11463
11464           /* If this is a test for negative, we can make an explicit
11465              test of the sign bit.  Test this first so we can use
11466              a paradoxical subreg to extend OP0.  */
11467
11468           if (op1 == const0_rtx && (code == LT || code == GE)
11469               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11470             {
11471               op0 = simplify_gen_binary (AND, tmode,
11472                                          gen_lowpart (tmode, op0),
11473                                          GEN_INT ((HOST_WIDE_INT) 1
11474                                                   << (GET_MODE_BITSIZE (mode)
11475                                                       - 1)));
11476               code = (code == LT) ? NE : EQ;
11477               break;
11478             }
11479
11480           /* If the only nonzero bits in OP0 and OP1 are those in the
11481              narrower mode and this is an equality or unsigned comparison,
11482              we can use the wider mode.  Similarly for sign-extended
11483              values, in which case it is true for all comparisons.  */
11484           zero_extended = ((code == EQ || code == NE
11485                             || code == GEU || code == GTU
11486                             || code == LEU || code == LTU)
11487                            && (nonzero_bits (op0, tmode)
11488                                & ~GET_MODE_MASK (mode)) == 0
11489                            && ((CONST_INT_P (op1)
11490                                 || (nonzero_bits (op1, tmode)
11491                                     & ~GET_MODE_MASK (mode)) == 0)));
11492
11493           if (zero_extended
11494               || ((num_sign_bit_copies (op0, tmode)
11495                    > (unsigned int) (GET_MODE_BITSIZE (tmode)
11496                                      - GET_MODE_BITSIZE (mode)))
11497                   && (num_sign_bit_copies (op1, tmode)
11498                       > (unsigned int) (GET_MODE_BITSIZE (tmode)
11499                                         - GET_MODE_BITSIZE (mode)))))
11500             {
11501               /* If OP0 is an AND and we don't have an AND in MODE either,
11502                  make a new AND in the proper mode.  */
11503               if (GET_CODE (op0) == AND
11504                   && !have_insn_for (AND, mode))
11505                 op0 = simplify_gen_binary (AND, tmode,
11506                                            gen_lowpart (tmode,
11507                                                         XEXP (op0, 0)),
11508                                            gen_lowpart (tmode,
11509                                                         XEXP (op0, 1)));
11510               else
11511                 {
11512                   if (zero_extended)
11513                     {
11514                       op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
11515                       op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
11516                     }
11517                   else
11518                     {
11519                       op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
11520                       op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
11521                     }
11522                   break;
11523                 }
11524             }
11525         }
11526
11527 #ifdef CANONICALIZE_COMPARISON
11528   /* If this machine only supports a subset of valid comparisons, see if we
11529      can convert an unsupported one into a supported one.  */
11530   CANONICALIZE_COMPARISON (code, op0, op1);
11531 #endif
11532
11533   *pop0 = op0;
11534   *pop1 = op1;
11535
11536   return code;
11537 }
11538 \f
11539 /* Utility function for record_value_for_reg.  Count number of
11540    rtxs in X.  */
11541 static int
11542 count_rtxs (rtx x)
11543 {
11544   enum rtx_code code = GET_CODE (x);
11545   const char *fmt;
11546   int i, j, ret = 1;
11547
11548   if (GET_RTX_CLASS (code) == '2'
11549       || GET_RTX_CLASS (code) == 'c')
11550     {
11551       rtx x0 = XEXP (x, 0);
11552       rtx x1 = XEXP (x, 1);
11553
11554       if (x0 == x1)
11555         return 1 + 2 * count_rtxs (x0);
11556
11557       if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11558            || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11559           && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11560         return 2 + 2 * count_rtxs (x0)
11561                + count_rtxs (x == XEXP (x1, 0)
11562                              ? XEXP (x1, 1) : XEXP (x1, 0));
11563
11564       if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11565            || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11566           && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11567         return 2 + 2 * count_rtxs (x1)
11568                + count_rtxs (x == XEXP (x0, 0)
11569                              ? XEXP (x0, 1) : XEXP (x0, 0));
11570     }
11571
11572   fmt = GET_RTX_FORMAT (code);
11573   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11574     if (fmt[i] == 'e')
11575       ret += count_rtxs (XEXP (x, i));
11576     else if (fmt[i] == 'E')
11577       for (j = 0; j < XVECLEN (x, i); j++)
11578         ret += count_rtxs (XVECEXP (x, i, j));
11579
11580   return ret;
11581 }
11582 \f
11583 /* Utility function for following routine.  Called when X is part of a value
11584    being stored into last_set_value.  Sets last_set_table_tick
11585    for each register mentioned.  Similar to mention_regs in cse.c  */
11586
11587 static void
11588 update_table_tick (rtx x)
11589 {
11590   enum rtx_code code = GET_CODE (x);
11591   const char *fmt = GET_RTX_FORMAT (code);
11592   int i, j;
11593
11594   if (code == REG)
11595     {
11596       unsigned int regno = REGNO (x);
11597       unsigned int endregno = END_REGNO (x);
11598       unsigned int r;
11599
11600       for (r = regno; r < endregno; r++)
11601         {
11602           reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, r);
11603           rsp->last_set_table_tick = label_tick;
11604         }
11605
11606       return;
11607     }
11608
11609   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11610     if (fmt[i] == 'e')
11611       {
11612         /* Check for identical subexpressions.  If x contains
11613            identical subexpression we only have to traverse one of
11614            them.  */
11615         if (i == 0 && ARITHMETIC_P (x))
11616           {
11617             /* Note that at this point x1 has already been
11618                processed.  */
11619             rtx x0 = XEXP (x, 0);
11620             rtx x1 = XEXP (x, 1);
11621
11622             /* If x0 and x1 are identical then there is no need to
11623                process x0.  */
11624             if (x0 == x1)
11625               break;
11626
11627             /* If x0 is identical to a subexpression of x1 then while
11628                processing x1, x0 has already been processed.  Thus we
11629                are done with x.  */
11630             if (ARITHMETIC_P (x1)
11631                 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11632               break;
11633
11634             /* If x1 is identical to a subexpression of x0 then we
11635                still have to process the rest of x0.  */
11636             if (ARITHMETIC_P (x0)
11637                 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11638               {
11639                 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
11640                 break;
11641               }
11642           }
11643
11644         update_table_tick (XEXP (x, i));
11645       }
11646     else if (fmt[i] == 'E')
11647       for (j = 0; j < XVECLEN (x, i); j++)
11648         update_table_tick (XVECEXP (x, i, j));
11649 }
11650
11651 /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
11652    are saying that the register is clobbered and we no longer know its
11653    value.  If INSN is zero, don't update reg_stat[].last_set; this is
11654    only permitted with VALUE also zero and is used to invalidate the
11655    register.  */
11656
11657 static void
11658 record_value_for_reg (rtx reg, rtx insn, rtx value)
11659 {
11660   unsigned int regno = REGNO (reg);
11661   unsigned int endregno = END_REGNO (reg);
11662   unsigned int i;
11663   reg_stat_type *rsp;
11664
11665   /* If VALUE contains REG and we have a previous value for REG, substitute
11666      the previous value.  */
11667   if (value && insn && reg_overlap_mentioned_p (reg, value))
11668     {
11669       rtx tem;
11670
11671       /* Set things up so get_last_value is allowed to see anything set up to
11672          our insn.  */
11673       subst_low_luid = DF_INSN_LUID (insn);
11674       tem = get_last_value (reg);
11675
11676       /* If TEM is simply a binary operation with two CLOBBERs as operands,
11677          it isn't going to be useful and will take a lot of time to process,
11678          so just use the CLOBBER.  */
11679
11680       if (tem)
11681         {
11682           if (ARITHMETIC_P (tem)
11683               && GET_CODE (XEXP (tem, 0)) == CLOBBER
11684               && GET_CODE (XEXP (tem, 1)) == CLOBBER)
11685             tem = XEXP (tem, 0);
11686           else if (count_occurrences (value, reg, 1) >= 2)
11687             {
11688               /* If there are two or more occurrences of REG in VALUE,
11689                  prevent the value from growing too much.  */
11690               if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
11691                 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
11692             }
11693
11694           value = replace_rtx (copy_rtx (value), reg, tem);
11695         }
11696     }
11697
11698   /* For each register modified, show we don't know its value, that
11699      we don't know about its bitwise content, that its value has been
11700      updated, and that we don't know the location of the death of the
11701      register.  */
11702   for (i = regno; i < endregno; i++)
11703     {
11704       rsp = VEC_index (reg_stat_type, reg_stat, i);
11705
11706       if (insn)
11707         rsp->last_set = insn;
11708
11709       rsp->last_set_value = 0;
11710       rsp->last_set_mode = VOIDmode;
11711       rsp->last_set_nonzero_bits = 0;
11712       rsp->last_set_sign_bit_copies = 0;
11713       rsp->last_death = 0;
11714       rsp->truncated_to_mode = VOIDmode;
11715     }
11716
11717   /* Mark registers that are being referenced in this value.  */
11718   if (value)
11719     update_table_tick (value);
11720
11721   /* Now update the status of each register being set.
11722      If someone is using this register in this block, set this register
11723      to invalid since we will get confused between the two lives in this
11724      basic block.  This makes using this register always invalid.  In cse, we
11725      scan the table to invalidate all entries using this register, but this
11726      is too much work for us.  */
11727
11728   for (i = regno; i < endregno; i++)
11729     {
11730       rsp = VEC_index (reg_stat_type, reg_stat, i);
11731       rsp->last_set_label = label_tick;
11732       if (!insn
11733           || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
11734         rsp->last_set_invalid = 1;
11735       else
11736         rsp->last_set_invalid = 0;
11737     }
11738
11739   /* The value being assigned might refer to X (like in "x++;").  In that
11740      case, we must replace it with (clobber (const_int 0)) to prevent
11741      infinite loops.  */
11742   rsp = VEC_index (reg_stat_type, reg_stat, regno);
11743   if (value && !get_last_value_validate (&value, insn, label_tick, 0))
11744     {
11745       value = copy_rtx (value);
11746       if (!get_last_value_validate (&value, insn, label_tick, 1))
11747         value = 0;
11748     }
11749
11750   /* For the main register being modified, update the value, the mode, the
11751      nonzero bits, and the number of sign bit copies.  */
11752
11753   rsp->last_set_value = value;
11754
11755   if (value)
11756     {
11757       enum machine_mode mode = GET_MODE (reg);
11758       subst_low_luid = DF_INSN_LUID (insn);
11759       rsp->last_set_mode = mode;
11760       if (GET_MODE_CLASS (mode) == MODE_INT
11761           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11762         mode = nonzero_bits_mode;
11763       rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
11764       rsp->last_set_sign_bit_copies
11765         = num_sign_bit_copies (value, GET_MODE (reg));
11766     }
11767 }
11768
11769 /* Called via note_stores from record_dead_and_set_regs to handle one
11770    SET or CLOBBER in an insn.  DATA is the instruction in which the
11771    set is occurring.  */
11772
11773 static void
11774 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
11775 {
11776   rtx record_dead_insn = (rtx) data;
11777
11778   if (GET_CODE (dest) == SUBREG)
11779     dest = SUBREG_REG (dest);
11780
11781   if (!record_dead_insn)
11782     {
11783       if (REG_P (dest))
11784         record_value_for_reg (dest, NULL_RTX, NULL_RTX);
11785       return;
11786     }
11787
11788   if (REG_P (dest))
11789     {
11790       /* If we are setting the whole register, we know its value.  Otherwise
11791          show that we don't know the value.  We can handle SUBREG in
11792          some cases.  */
11793       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11794         record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11795       else if (GET_CODE (setter) == SET
11796                && GET_CODE (SET_DEST (setter)) == SUBREG
11797                && SUBREG_REG (SET_DEST (setter)) == dest
11798                && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11799                && subreg_lowpart_p (SET_DEST (setter)))
11800         record_value_for_reg (dest, record_dead_insn,
11801                               gen_lowpart (GET_MODE (dest),
11802                                                        SET_SRC (setter)));
11803       else
11804         record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11805     }
11806   else if (MEM_P (dest)
11807            /* Ignore pushes, they clobber nothing.  */
11808            && ! push_operand (dest, GET_MODE (dest)))
11809     mem_last_set = DF_INSN_LUID (record_dead_insn);
11810 }
11811
11812 /* Update the records of when each REG was most recently set or killed
11813    for the things done by INSN.  This is the last thing done in processing
11814    INSN in the combiner loop.
11815
11816    We update reg_stat[], in particular fields last_set, last_set_value,
11817    last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
11818    last_death, and also the similar information mem_last_set (which insn
11819    most recently modified memory) and last_call_luid (which insn was the
11820    most recent subroutine call).  */
11821
11822 static void
11823 record_dead_and_set_regs (rtx insn)
11824 {
11825   rtx link;
11826   unsigned int i;
11827
11828   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11829     {
11830       if (REG_NOTE_KIND (link) == REG_DEAD
11831           && REG_P (XEXP (link, 0)))
11832         {
11833           unsigned int regno = REGNO (XEXP (link, 0));
11834           unsigned int endregno = END_REGNO (XEXP (link, 0));
11835
11836           for (i = regno; i < endregno; i++)
11837             {
11838               reg_stat_type *rsp;
11839
11840               rsp = VEC_index (reg_stat_type, reg_stat, i);
11841               rsp->last_death = insn;
11842             }
11843         }
11844       else if (REG_NOTE_KIND (link) == REG_INC)
11845         record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11846     }
11847
11848   if (CALL_P (insn))
11849     {
11850       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11851         if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11852           {
11853             reg_stat_type *rsp;
11854
11855             rsp = VEC_index (reg_stat_type, reg_stat, i);
11856             rsp->last_set_invalid = 1;
11857             rsp->last_set = insn;
11858             rsp->last_set_value = 0;
11859             rsp->last_set_mode = VOIDmode;
11860             rsp->last_set_nonzero_bits = 0;
11861             rsp->last_set_sign_bit_copies = 0;
11862             rsp->last_death = 0;
11863             rsp->truncated_to_mode = VOIDmode;
11864           }
11865
11866       last_call_luid = mem_last_set = DF_INSN_LUID (insn);
11867
11868       /* We can't combine into a call pattern.  Remember, though, that
11869          the return value register is set at this LUID.  We could
11870          still replace a register with the return value from the
11871          wrong subroutine call!  */
11872       note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
11873     }
11874   else
11875     note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11876 }
11877
11878 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11879    register present in the SUBREG, so for each such SUBREG go back and
11880    adjust nonzero and sign bit information of the registers that are
11881    known to have some zero/sign bits set.
11882
11883    This is needed because when combine blows the SUBREGs away, the
11884    information on zero/sign bits is lost and further combines can be
11885    missed because of that.  */
11886
11887 static void
11888 record_promoted_value (rtx insn, rtx subreg)
11889 {
11890   rtx links, set;
11891   unsigned int regno = REGNO (SUBREG_REG (subreg));
11892   enum machine_mode mode = GET_MODE (subreg);
11893
11894   if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11895     return;
11896
11897   for (links = LOG_LINKS (insn); links;)
11898     {
11899       reg_stat_type *rsp;
11900
11901       insn = XEXP (links, 0);
11902       set = single_set (insn);
11903
11904       if (! set || !REG_P (SET_DEST (set))
11905           || REGNO (SET_DEST (set)) != regno
11906           || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11907         {
11908           links = XEXP (links, 1);
11909           continue;
11910         }
11911
11912       rsp = VEC_index (reg_stat_type, reg_stat, regno);
11913       if (rsp->last_set == insn)
11914         {
11915           if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11916             rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
11917         }
11918
11919       if (REG_P (SET_SRC (set)))
11920         {
11921           regno = REGNO (SET_SRC (set));
11922           links = LOG_LINKS (insn);
11923         }
11924       else
11925         break;
11926     }
11927 }
11928
11929 /* Check if X, a register, is known to contain a value already
11930    truncated to MODE.  In this case we can use a subreg to refer to
11931    the truncated value even though in the generic case we would need
11932    an explicit truncation.  */
11933
11934 static bool
11935 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
11936 {
11937   reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
11938   enum machine_mode truncated = rsp->truncated_to_mode;
11939
11940   if (truncated == 0
11941       || rsp->truncation_label < label_tick_ebb_start)
11942     return false;
11943   if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
11944     return true;
11945   if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
11946                              GET_MODE_BITSIZE (truncated)))
11947     return true;
11948   return false;
11949 }
11950
11951 /* Callback for for_each_rtx.  If *P is a hard reg or a subreg record the mode
11952    that the register is accessed in.  For non-TRULY_NOOP_TRUNCATION targets we
11953    might be able to turn a truncate into a subreg using this information.
11954    Return -1 if traversing *P is complete or 0 otherwise.  */
11955
11956 static int
11957 record_truncated_value (rtx *p, void *data ATTRIBUTE_UNUSED)
11958 {
11959   rtx x = *p;
11960   enum machine_mode truncated_mode;
11961   reg_stat_type *rsp;
11962
11963   if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
11964     {
11965       enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
11966       truncated_mode = GET_MODE (x);
11967
11968       if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
11969         return -1;
11970
11971       if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (truncated_mode),
11972                                  GET_MODE_BITSIZE (original_mode)))
11973         return -1;
11974
11975       x = SUBREG_REG (x);
11976     }
11977   /* ??? For hard-regs we now record everything.  We might be able to
11978      optimize this using last_set_mode.  */
11979   else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
11980     truncated_mode = GET_MODE (x);
11981   else
11982     return 0;
11983
11984   rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
11985   if (rsp->truncated_to_mode == 0
11986       || rsp->truncation_label < label_tick_ebb_start
11987       || (GET_MODE_SIZE (truncated_mode)
11988           < GET_MODE_SIZE (rsp->truncated_to_mode)))
11989     {
11990       rsp->truncated_to_mode = truncated_mode;
11991       rsp->truncation_label = label_tick;
11992     }
11993
11994   return -1;
11995 }
11996
11997 /* Callback for note_uses.  Find hardregs and subregs of pseudos and
11998    the modes they are used in.  This can help truning TRUNCATEs into
11999    SUBREGs.  */
12000
12001 static void
12002 record_truncated_values (rtx *x, void *data ATTRIBUTE_UNUSED)
12003 {
12004   for_each_rtx (x, record_truncated_value, NULL);
12005 }
12006
12007 /* Scan X for promoted SUBREGs.  For each one found,
12008    note what it implies to the registers used in it.  */
12009
12010 static void
12011 check_promoted_subreg (rtx insn, rtx x)
12012 {
12013   if (GET_CODE (x) == SUBREG
12014       && SUBREG_PROMOTED_VAR_P (x)
12015       && REG_P (SUBREG_REG (x)))
12016     record_promoted_value (insn, x);
12017   else
12018     {
12019       const char *format = GET_RTX_FORMAT (GET_CODE (x));
12020       int i, j;
12021
12022       for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12023         switch (format[i])
12024           {
12025           case 'e':
12026             check_promoted_subreg (insn, XEXP (x, i));
12027             break;
12028           case 'V':
12029           case 'E':
12030             if (XVEC (x, i) != 0)
12031               for (j = 0; j < XVECLEN (x, i); j++)
12032                 check_promoted_subreg (insn, XVECEXP (x, i, j));
12033             break;
12034           }
12035     }
12036 }
12037 \f
12038 /* Verify that all the registers and memory references mentioned in *LOC are
12039    still valid.  *LOC was part of a value set in INSN when label_tick was
12040    equal to TICK.  Return 0 if some are not.  If REPLACE is nonzero, replace
12041    the invalid references with (clobber (const_int 0)) and return 1.  This
12042    replacement is useful because we often can get useful information about
12043    the form of a value (e.g., if it was produced by a shift that always
12044    produces -1 or 0) even though we don't know exactly what registers it
12045    was produced from.  */
12046
12047 static int
12048 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
12049 {
12050   rtx x = *loc;
12051   const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12052   int len = GET_RTX_LENGTH (GET_CODE (x));
12053   int i, j;
12054
12055   if (REG_P (x))
12056     {
12057       unsigned int regno = REGNO (x);
12058       unsigned int endregno = END_REGNO (x);
12059       unsigned int j;
12060
12061       for (j = regno; j < endregno; j++)
12062         {
12063           reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, j);
12064           if (rsp->last_set_invalid
12065               /* If this is a pseudo-register that was only set once and not
12066                  live at the beginning of the function, it is always valid.  */
12067               || (! (regno >= FIRST_PSEUDO_REGISTER
12068                      && REG_N_SETS (regno) == 1
12069                      && (!REGNO_REG_SET_P
12070                          (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno)))
12071                   && rsp->last_set_label > tick))
12072           {
12073             if (replace)
12074               *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12075             return replace;
12076           }
12077         }
12078
12079       return 1;
12080     }
12081   /* If this is a memory reference, make sure that there were no stores after
12082      it that might have clobbered the value.  We don't have alias info, so we
12083      assume any store invalidates it.  Moreover, we only have local UIDs, so
12084      we also assume that there were stores in the intervening basic blocks.  */
12085   else if (MEM_P (x) && !MEM_READONLY_P (x)
12086            && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12087     {
12088       if (replace)
12089         *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12090       return replace;
12091     }
12092
12093   for (i = 0; i < len; i++)
12094     {
12095       if (fmt[i] == 'e')
12096         {
12097           /* Check for identical subexpressions.  If x contains
12098              identical subexpression we only have to traverse one of
12099              them.  */
12100           if (i == 1 && ARITHMETIC_P (x))
12101             {
12102               /* Note that at this point x0 has already been checked
12103                  and found valid.  */
12104               rtx x0 = XEXP (x, 0);
12105               rtx x1 = XEXP (x, 1);
12106
12107               /* If x0 and x1 are identical then x is also valid.  */
12108               if (x0 == x1)
12109                 return 1;
12110
12111               /* If x1 is identical to a subexpression of x0 then
12112                  while checking x0, x1 has already been checked.  Thus
12113                  it is valid and so as x.  */
12114               if (ARITHMETIC_P (x0)
12115                   && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12116                 return 1;
12117
12118               /* If x0 is identical to a subexpression of x1 then x is
12119                  valid iff the rest of x1 is valid.  */
12120               if (ARITHMETIC_P (x1)
12121                   && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12122                 return
12123                   get_last_value_validate (&XEXP (x1,
12124                                                   x0 == XEXP (x1, 0) ? 1 : 0),
12125                                            insn, tick, replace);
12126             }
12127
12128           if (get_last_value_validate (&XEXP (x, i), insn, tick,
12129                                        replace) == 0)
12130             return 0;
12131         }
12132       else if (fmt[i] == 'E')
12133         for (j = 0; j < XVECLEN (x, i); j++)
12134           if (get_last_value_validate (&XVECEXP (x, i, j),
12135                                        insn, tick, replace) == 0)
12136             return 0;
12137     }
12138
12139   /* If we haven't found a reason for it to be invalid, it is valid.  */
12140   return 1;
12141 }
12142
12143 /* Get the last value assigned to X, if known.  Some registers
12144    in the value may be replaced with (clobber (const_int 0)) if their value
12145    is known longer known reliably.  */
12146
12147 static rtx
12148 get_last_value (const_rtx x)
12149 {
12150   unsigned int regno;
12151   rtx value;
12152   reg_stat_type *rsp;
12153
12154   /* If this is a non-paradoxical SUBREG, get the value of its operand and
12155      then convert it to the desired mode.  If this is a paradoxical SUBREG,
12156      we cannot predict what values the "extra" bits might have.  */
12157   if (GET_CODE (x) == SUBREG
12158       && subreg_lowpart_p (x)
12159       && (GET_MODE_SIZE (GET_MODE (x))
12160           <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
12161       && (value = get_last_value (SUBREG_REG (x))) != 0)
12162     return gen_lowpart (GET_MODE (x), value);
12163
12164   if (!REG_P (x))
12165     return 0;
12166
12167   regno = REGNO (x);
12168   rsp = VEC_index (reg_stat_type, reg_stat, regno);
12169   value = rsp->last_set_value;
12170
12171   /* If we don't have a value, or if it isn't for this basic block and
12172      it's either a hard register, set more than once, or it's a live
12173      at the beginning of the function, return 0.
12174
12175      Because if it's not live at the beginning of the function then the reg
12176      is always set before being used (is never used without being set).
12177      And, if it's set only once, and it's always set before use, then all
12178      uses must have the same last value, even if it's not from this basic
12179      block.  */
12180
12181   if (value == 0
12182       || (rsp->last_set_label < label_tick_ebb_start
12183           && (regno < FIRST_PSEUDO_REGISTER
12184               || REG_N_SETS (regno) != 1
12185               || REGNO_REG_SET_P
12186                  (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno))))
12187     return 0;
12188
12189   /* If the value was set in a later insn than the ones we are processing,
12190      we can't use it even if the register was only set once.  */
12191   if (rsp->last_set_label == label_tick
12192       && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12193     return 0;
12194
12195   /* If the value has all its registers valid, return it.  */
12196   if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12197     return value;
12198
12199   /* Otherwise, make a copy and replace any invalid register with
12200      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
12201
12202   value = copy_rtx (value);
12203   if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
12204     return value;
12205
12206   return 0;
12207 }
12208 \f
12209 /* Return nonzero if expression X refers to a REG or to memory
12210    that is set in an instruction more recent than FROM_LUID.  */
12211
12212 static int
12213 use_crosses_set_p (const_rtx x, int from_luid)
12214 {
12215   const char *fmt;
12216   int i;
12217   enum rtx_code code = GET_CODE (x);
12218
12219   if (code == REG)
12220     {
12221       unsigned int regno = REGNO (x);
12222       unsigned endreg = END_REGNO (x);
12223
12224 #ifdef PUSH_ROUNDING
12225       /* Don't allow uses of the stack pointer to be moved,
12226          because we don't know whether the move crosses a push insn.  */
12227       if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12228         return 1;
12229 #endif
12230       for (; regno < endreg; regno++)
12231         {
12232           reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
12233           if (rsp->last_set
12234               && rsp->last_set_label == label_tick
12235               && DF_INSN_LUID (rsp->last_set) > from_luid)
12236             return 1;
12237         }
12238       return 0;
12239     }
12240
12241   if (code == MEM && mem_last_set > from_luid)
12242     return 1;
12243
12244   fmt = GET_RTX_FORMAT (code);
12245
12246   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12247     {
12248       if (fmt[i] == 'E')
12249         {
12250           int j;
12251           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12252             if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
12253               return 1;
12254         }
12255       else if (fmt[i] == 'e'
12256                && use_crosses_set_p (XEXP (x, i), from_luid))
12257         return 1;
12258     }
12259   return 0;
12260 }
12261 \f
12262 /* Define three variables used for communication between the following
12263    routines.  */
12264
12265 static unsigned int reg_dead_regno, reg_dead_endregno;
12266 static int reg_dead_flag;
12267
12268 /* Function called via note_stores from reg_dead_at_p.
12269
12270    If DEST is within [reg_dead_regno, reg_dead_endregno), set
12271    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
12272
12273 static void
12274 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
12275 {
12276   unsigned int regno, endregno;
12277
12278   if (!REG_P (dest))
12279     return;
12280
12281   regno = REGNO (dest);
12282   endregno = END_REGNO (dest);
12283   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12284     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12285 }
12286
12287 /* Return nonzero if REG is known to be dead at INSN.
12288
12289    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
12290    referencing REG, it is dead.  If we hit a SET referencing REG, it is
12291    live.  Otherwise, see if it is live or dead at the start of the basic
12292    block we are in.  Hard regs marked as being live in NEWPAT_USED_REGS
12293    must be assumed to be always live.  */
12294
12295 static int
12296 reg_dead_at_p (rtx reg, rtx insn)
12297 {
12298   basic_block block;
12299   unsigned int i;
12300
12301   /* Set variables for reg_dead_at_p_1.  */
12302   reg_dead_regno = REGNO (reg);
12303   reg_dead_endregno = END_REGNO (reg);
12304
12305   reg_dead_flag = 0;
12306
12307   /* Check that reg isn't mentioned in NEWPAT_USED_REGS.  For fixed registers
12308      we allow the machine description to decide whether use-and-clobber
12309      patterns are OK.  */
12310   if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12311     {
12312       for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12313         if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
12314           return 0;
12315     }
12316
12317   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
12318      beginning of basic block.  */
12319   block = BLOCK_FOR_INSN (insn);
12320   for (;;)
12321     {
12322       if (INSN_P (insn))
12323         {
12324           note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12325           if (reg_dead_flag)
12326             return reg_dead_flag == 1 ? 1 : 0;
12327
12328           if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12329             return 1;
12330         }
12331
12332       if (insn == BB_HEAD (block))
12333         break;
12334
12335       insn = PREV_INSN (insn);
12336     }
12337
12338   /* Look at live-in sets for the basic block that we were in.  */
12339   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12340     if (REGNO_REG_SET_P (df_get_live_in (block), i))
12341       return 0;
12342
12343   return 1;
12344 }
12345 \f
12346 /* Note hard registers in X that are used.  */
12347
12348 static void
12349 mark_used_regs_combine (rtx x)
12350 {
12351   RTX_CODE code = GET_CODE (x);
12352   unsigned int regno;
12353   int i;
12354
12355   switch (code)
12356     {
12357     case LABEL_REF:
12358     case SYMBOL_REF:
12359     case CONST_INT:
12360     case CONST:
12361     case CONST_DOUBLE:
12362     case CONST_VECTOR:
12363     case PC:
12364     case ADDR_VEC:
12365     case ADDR_DIFF_VEC:
12366     case ASM_INPUT:
12367 #ifdef HAVE_cc0
12368     /* CC0 must die in the insn after it is set, so we don't need to take
12369        special note of it here.  */
12370     case CC0:
12371 #endif
12372       return;
12373
12374     case CLOBBER:
12375       /* If we are clobbering a MEM, mark any hard registers inside the
12376          address as used.  */
12377       if (MEM_P (XEXP (x, 0)))
12378         mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12379       return;
12380
12381     case REG:
12382       regno = REGNO (x);
12383       /* A hard reg in a wide mode may really be multiple registers.
12384          If so, mark all of them just like the first.  */
12385       if (regno < FIRST_PSEUDO_REGISTER)
12386         {
12387           /* None of this applies to the stack, frame or arg pointers.  */
12388           if (regno == STACK_POINTER_REGNUM
12389 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
12390               || regno == HARD_FRAME_POINTER_REGNUM
12391 #endif
12392 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12393               || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12394 #endif
12395               || regno == FRAME_POINTER_REGNUM)
12396             return;
12397
12398           add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
12399         }
12400       return;
12401
12402     case SET:
12403       {
12404         /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12405            the address.  */
12406         rtx testreg = SET_DEST (x);
12407
12408         while (GET_CODE (testreg) == SUBREG
12409                || GET_CODE (testreg) == ZERO_EXTRACT
12410                || GET_CODE (testreg) == STRICT_LOW_PART)
12411           testreg = XEXP (testreg, 0);
12412
12413         if (MEM_P (testreg))
12414           mark_used_regs_combine (XEXP (testreg, 0));
12415
12416         mark_used_regs_combine (SET_SRC (x));
12417       }
12418       return;
12419
12420     default:
12421       break;
12422     }
12423
12424   /* Recursively scan the operands of this expression.  */
12425
12426   {
12427     const char *fmt = GET_RTX_FORMAT (code);
12428
12429     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12430       {
12431         if (fmt[i] == 'e')
12432           mark_used_regs_combine (XEXP (x, i));
12433         else if (fmt[i] == 'E')
12434           {
12435             int j;
12436
12437             for (j = 0; j < XVECLEN (x, i); j++)
12438               mark_used_regs_combine (XVECEXP (x, i, j));
12439           }
12440       }
12441   }
12442 }
12443 \f
12444 /* Remove register number REGNO from the dead registers list of INSN.
12445
12446    Return the note used to record the death, if there was one.  */
12447
12448 rtx
12449 remove_death (unsigned int regno, rtx insn)
12450 {
12451   rtx note = find_regno_note (insn, REG_DEAD, regno);
12452
12453   if (note)
12454     remove_note (insn, note);
12455
12456   return note;
12457 }
12458
12459 /* For each register (hardware or pseudo) used within expression X, if its
12460    death is in an instruction with luid between FROM_LUID (inclusive) and
12461    TO_INSN (exclusive), put a REG_DEAD note for that register in the
12462    list headed by PNOTES.
12463
12464    That said, don't move registers killed by maybe_kill_insn.
12465
12466    This is done when X is being merged by combination into TO_INSN.  These
12467    notes will then be distributed as needed.  */
12468
12469 static void
12470 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx to_insn,
12471              rtx *pnotes)
12472 {
12473   const char *fmt;
12474   int len, i;
12475   enum rtx_code code = GET_CODE (x);
12476
12477   if (code == REG)
12478     {
12479       unsigned int regno = REGNO (x);
12480       rtx where_dead = VEC_index (reg_stat_type, reg_stat, regno)->last_death;
12481
12482       /* Don't move the register if it gets killed in between from and to.  */
12483       if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12484           && ! reg_referenced_p (x, maybe_kill_insn))
12485         return;
12486
12487       if (where_dead
12488           && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
12489           && DF_INSN_LUID (where_dead) >= from_luid
12490           && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
12491         {
12492           rtx note = remove_death (regno, where_dead);
12493
12494           /* It is possible for the call above to return 0.  This can occur
12495              when last_death points to I2 or I1 that we combined with.
12496              In that case make a new note.
12497
12498              We must also check for the case where X is a hard register
12499              and NOTE is a death note for a range of hard registers
12500              including X.  In that case, we must put REG_DEAD notes for
12501              the remaining registers in place of NOTE.  */
12502
12503           if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12504               && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12505                   > GET_MODE_SIZE (GET_MODE (x))))
12506             {
12507               unsigned int deadregno = REGNO (XEXP (note, 0));
12508               unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
12509               unsigned int ourend = END_HARD_REGNO (x);
12510               unsigned int i;
12511
12512               for (i = deadregno; i < deadend; i++)
12513                 if (i < regno || i >= ourend)
12514                   add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
12515             }
12516
12517           /* If we didn't find any note, or if we found a REG_DEAD note that
12518              covers only part of the given reg, and we have a multi-reg hard
12519              register, then to be safe we must check for REG_DEAD notes
12520              for each register other than the first.  They could have
12521              their own REG_DEAD notes lying around.  */
12522           else if ((note == 0
12523                     || (note != 0
12524                         && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12525                             < GET_MODE_SIZE (GET_MODE (x)))))
12526                    && regno < FIRST_PSEUDO_REGISTER
12527                    && hard_regno_nregs[regno][GET_MODE (x)] > 1)
12528             {
12529               unsigned int ourend = END_HARD_REGNO (x);
12530               unsigned int i, offset;
12531               rtx oldnotes = 0;
12532
12533               if (note)
12534                 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
12535               else
12536                 offset = 1;
12537
12538               for (i = regno + offset; i < ourend; i++)
12539                 move_deaths (regno_reg_rtx[i],
12540                              maybe_kill_insn, from_luid, to_insn, &oldnotes);
12541             }
12542
12543           if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12544             {
12545               XEXP (note, 1) = *pnotes;
12546               *pnotes = note;
12547             }
12548           else
12549             *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
12550         }
12551
12552       return;
12553     }
12554
12555   else if (GET_CODE (x) == SET)
12556     {
12557       rtx dest = SET_DEST (x);
12558
12559       move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
12560
12561       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
12562          that accesses one word of a multi-word item, some
12563          piece of everything register in the expression is used by
12564          this insn, so remove any old death.  */
12565       /* ??? So why do we test for equality of the sizes?  */
12566
12567       if (GET_CODE (dest) == ZERO_EXTRACT
12568           || GET_CODE (dest) == STRICT_LOW_PART
12569           || (GET_CODE (dest) == SUBREG
12570               && (((GET_MODE_SIZE (GET_MODE (dest))
12571                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
12572                   == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
12573                        + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
12574         {
12575           move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
12576           return;
12577         }
12578
12579       /* If this is some other SUBREG, we know it replaces the entire
12580          value, so use that as the destination.  */
12581       if (GET_CODE (dest) == SUBREG)
12582         dest = SUBREG_REG (dest);
12583
12584       /* If this is a MEM, adjust deaths of anything used in the address.
12585          For a REG (the only other possibility), the entire value is
12586          being replaced so the old value is not used in this insn.  */
12587
12588       if (MEM_P (dest))
12589         move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
12590                      to_insn, pnotes);
12591       return;
12592     }
12593
12594   else if (GET_CODE (x) == CLOBBER)
12595     return;
12596
12597   len = GET_RTX_LENGTH (code);
12598   fmt = GET_RTX_FORMAT (code);
12599
12600   for (i = 0; i < len; i++)
12601     {
12602       if (fmt[i] == 'E')
12603         {
12604           int j;
12605           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12606             move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
12607                          to_insn, pnotes);
12608         }
12609       else if (fmt[i] == 'e')
12610         move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
12611     }
12612 }
12613 \f
12614 /* Return 1 if X is the target of a bit-field assignment in BODY, the
12615    pattern of an insn.  X must be a REG.  */
12616
12617 static int
12618 reg_bitfield_target_p (rtx x, rtx body)
12619 {
12620   int i;
12621
12622   if (GET_CODE (body) == SET)
12623     {
12624       rtx dest = SET_DEST (body);
12625       rtx target;
12626       unsigned int regno, tregno, endregno, endtregno;
12627
12628       if (GET_CODE (dest) == ZERO_EXTRACT)
12629         target = XEXP (dest, 0);
12630       else if (GET_CODE (dest) == STRICT_LOW_PART)
12631         target = SUBREG_REG (XEXP (dest, 0));
12632       else
12633         return 0;
12634
12635       if (GET_CODE (target) == SUBREG)
12636         target = SUBREG_REG (target);
12637
12638       if (!REG_P (target))
12639         return 0;
12640
12641       tregno = REGNO (target), regno = REGNO (x);
12642       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
12643         return target == x;
12644
12645       endtregno = end_hard_regno (GET_MODE (target), tregno);
12646       endregno = end_hard_regno (GET_MODE (x), regno);
12647
12648       return endregno > tregno && regno < endtregno;
12649     }
12650
12651   else if (GET_CODE (body) == PARALLEL)
12652     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
12653       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
12654         return 1;
12655
12656   return 0;
12657 }
12658
12659 /* Return the next insn after INSN that is neither a NOTE nor a
12660    DEBUG_INSN.  This routine does not look inside SEQUENCEs.  */
12661
12662 static rtx
12663 next_nonnote_nondebug_insn (rtx insn)
12664 {
12665   while (insn)
12666     {
12667       insn = NEXT_INSN (insn);
12668       if (insn == 0)
12669         break;
12670       if (NOTE_P (insn))
12671         continue;
12672       if (DEBUG_INSN_P (insn))
12673         continue;
12674       break;
12675     }
12676
12677   return insn;
12678 }
12679
12680
12681 \f
12682 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
12683    as appropriate.  I3 and I2 are the insns resulting from the combination
12684    insns including FROM (I2 may be zero).
12685
12686    ELIM_I2 and ELIM_I1 are either zero or registers that we know will
12687    not need REG_DEAD notes because they are being substituted for.  This
12688    saves searching in the most common cases.
12689
12690    Each note in the list is either ignored or placed on some insns, depending
12691    on the type of note.  */
12692
12693 static void
12694 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
12695                   rtx elim_i1)
12696 {
12697   rtx note, next_note;
12698   rtx tem;
12699
12700   for (note = notes; note; note = next_note)
12701     {
12702       rtx place = 0, place2 = 0;
12703
12704       next_note = XEXP (note, 1);
12705       switch (REG_NOTE_KIND (note))
12706         {
12707         case REG_BR_PROB:
12708         case REG_BR_PRED:
12709           /* Doesn't matter much where we put this, as long as it's somewhere.
12710              It is preferable to keep these notes on branches, which is most
12711              likely to be i3.  */
12712           place = i3;
12713           break;
12714
12715         case REG_VALUE_PROFILE:
12716           /* Just get rid of this note, as it is unused later anyway.  */
12717           break;
12718
12719         case REG_NON_LOCAL_GOTO:
12720           if (JUMP_P (i3))
12721             place = i3;
12722           else
12723             {
12724               gcc_assert (i2 && JUMP_P (i2));
12725               place = i2;
12726             }
12727           break;
12728
12729         case REG_EH_REGION:
12730           /* These notes must remain with the call or trapping instruction.  */
12731           if (CALL_P (i3))
12732             place = i3;
12733           else if (i2 && CALL_P (i2))
12734             place = i2;
12735           else
12736             {
12737               gcc_assert (cfun->can_throw_non_call_exceptions);
12738               if (may_trap_p (i3))
12739                 place = i3;
12740               else if (i2 && may_trap_p (i2))
12741                 place = i2;
12742               /* ??? Otherwise assume we've combined things such that we
12743                  can now prove that the instructions can't trap.  Drop the
12744                  note in this case.  */
12745             }
12746           break;
12747
12748         case REG_NORETURN:
12749         case REG_SETJMP:
12750           /* These notes must remain with the call.  It should not be
12751              possible for both I2 and I3 to be a call.  */
12752           if (CALL_P (i3))
12753             place = i3;
12754           else
12755             {
12756               gcc_assert (i2 && CALL_P (i2));
12757               place = i2;
12758             }
12759           break;
12760
12761         case REG_UNUSED:
12762           /* Any clobbers for i3 may still exist, and so we must process
12763              REG_UNUSED notes from that insn.
12764
12765              Any clobbers from i2 or i1 can only exist if they were added by
12766              recog_for_combine.  In that case, recog_for_combine created the
12767              necessary REG_UNUSED notes.  Trying to keep any original
12768              REG_UNUSED notes from these insns can cause incorrect output
12769              if it is for the same register as the original i3 dest.
12770              In that case, we will notice that the register is set in i3,
12771              and then add a REG_UNUSED note for the destination of i3, which
12772              is wrong.  However, it is possible to have REG_UNUSED notes from
12773              i2 or i1 for register which were both used and clobbered, so
12774              we keep notes from i2 or i1 if they will turn into REG_DEAD
12775              notes.  */
12776
12777           /* If this register is set or clobbered in I3, put the note there
12778              unless there is one already.  */
12779           if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
12780             {
12781               if (from_insn != i3)
12782                 break;
12783
12784               if (! (REG_P (XEXP (note, 0))
12785                      ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
12786                      : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
12787                 place = i3;
12788             }
12789           /* Otherwise, if this register is used by I3, then this register
12790              now dies here, so we must put a REG_DEAD note here unless there
12791              is one already.  */
12792           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
12793                    && ! (REG_P (XEXP (note, 0))
12794                          ? find_regno_note (i3, REG_DEAD,
12795                                             REGNO (XEXP (note, 0)))
12796                          : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
12797             {
12798               PUT_REG_NOTE_KIND (note, REG_DEAD);
12799               place = i3;
12800             }
12801           break;
12802
12803         case REG_EQUAL:
12804         case REG_EQUIV:
12805         case REG_NOALIAS:
12806           /* These notes say something about results of an insn.  We can
12807              only support them if they used to be on I3 in which case they
12808              remain on I3.  Otherwise they are ignored.
12809
12810              If the note refers to an expression that is not a constant, we
12811              must also ignore the note since we cannot tell whether the
12812              equivalence is still true.  It might be possible to do
12813              slightly better than this (we only have a problem if I2DEST
12814              or I1DEST is present in the expression), but it doesn't
12815              seem worth the trouble.  */
12816
12817           if (from_insn == i3
12818               && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
12819             place = i3;
12820           break;
12821
12822         case REG_INC:
12823           /* These notes say something about how a register is used.  They must
12824              be present on any use of the register in I2 or I3.  */
12825           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
12826             place = i3;
12827
12828           if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
12829             {
12830               if (place)
12831                 place2 = i2;
12832               else
12833                 place = i2;
12834             }
12835           break;
12836
12837         case REG_LABEL_TARGET:
12838         case REG_LABEL_OPERAND:
12839           /* This can show up in several ways -- either directly in the
12840              pattern, or hidden off in the constant pool with (or without?)
12841              a REG_EQUAL note.  */
12842           /* ??? Ignore the without-reg_equal-note problem for now.  */
12843           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12844               || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12845                   && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12846                   && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12847             place = i3;
12848
12849           if (i2
12850               && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12851                   || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12852                       && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12853                       && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12854             {
12855               if (place)
12856                 place2 = i2;
12857               else
12858                 place = i2;
12859             }
12860
12861           /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
12862              as a JUMP_LABEL or decrement LABEL_NUSES if it's already
12863              there.  */
12864           if (place && JUMP_P (place)
12865               && REG_NOTE_KIND (note) == REG_LABEL_TARGET
12866               && (JUMP_LABEL (place) == NULL
12867                   || JUMP_LABEL (place) == XEXP (note, 0)))
12868             {
12869               rtx label = JUMP_LABEL (place);
12870
12871               if (!label)
12872                 JUMP_LABEL (place) = XEXP (note, 0);
12873               else if (LABEL_P (label))
12874                 LABEL_NUSES (label)--;
12875             }
12876
12877           if (place2 && JUMP_P (place2)
12878               && REG_NOTE_KIND (note) == REG_LABEL_TARGET
12879               && (JUMP_LABEL (place2) == NULL
12880                   || JUMP_LABEL (place2) == XEXP (note, 0)))
12881             {
12882               rtx label = JUMP_LABEL (place2);
12883
12884               if (!label)
12885                 JUMP_LABEL (place2) = XEXP (note, 0);
12886               else if (LABEL_P (label))
12887                 LABEL_NUSES (label)--;
12888               place2 = 0;
12889             }
12890           break;
12891
12892         case REG_NONNEG:
12893           /* This note says something about the value of a register prior
12894              to the execution of an insn.  It is too much trouble to see
12895              if the note is still correct in all situations.  It is better
12896              to simply delete it.  */
12897           break;
12898
12899         case REG_DEAD:
12900           /* If we replaced the right hand side of FROM_INSN with a
12901              REG_EQUAL note, the original use of the dying register
12902              will not have been combined into I3 and I2.  In such cases,
12903              FROM_INSN is guaranteed to be the first of the combined
12904              instructions, so we simply need to search back before
12905              FROM_INSN for the previous use or set of this register,
12906              then alter the notes there appropriately.
12907
12908              If the register is used as an input in I3, it dies there.
12909              Similarly for I2, if it is nonzero and adjacent to I3.
12910
12911              If the register is not used as an input in either I3 or I2
12912              and it is not one of the registers we were supposed to eliminate,
12913              there are two possibilities.  We might have a non-adjacent I2
12914              or we might have somehow eliminated an additional register
12915              from a computation.  For example, we might have had A & B where
12916              we discover that B will always be zero.  In this case we will
12917              eliminate the reference to A.
12918
12919              In both cases, we must search to see if we can find a previous
12920              use of A and put the death note there.  */
12921
12922           if (from_insn
12923               && from_insn == i2mod
12924               && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
12925             tem = from_insn;
12926           else
12927             {
12928               if (from_insn
12929                   && CALL_P (from_insn)
12930                   && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12931                 place = from_insn;
12932               else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12933                 place = i3;
12934               else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
12935                        && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12936                 place = i2;
12937               else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
12938                         && !(i2mod
12939                              && reg_overlap_mentioned_p (XEXP (note, 0),
12940                                                          i2mod_old_rhs)))
12941                        || rtx_equal_p (XEXP (note, 0), elim_i1))
12942                 break;
12943               tem = i3;
12944             }
12945
12946           if (place == 0)
12947             {
12948               basic_block bb = this_basic_block;
12949
12950               for (tem = PREV_INSN (tem); place == 0; tem = PREV_INSN (tem))
12951                 {
12952                   if (!NONDEBUG_INSN_P (tem))
12953                     {
12954                       if (tem == BB_HEAD (bb))
12955                         break;
12956                       continue;
12957                     }
12958
12959                   /* If the register is being set at TEM, see if that is all
12960                      TEM is doing.  If so, delete TEM.  Otherwise, make this
12961                      into a REG_UNUSED note instead. Don't delete sets to
12962                      global register vars.  */
12963                   if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
12964                        || !global_regs[REGNO (XEXP (note, 0))])
12965                       && reg_set_p (XEXP (note, 0), PATTERN (tem)))
12966                     {
12967                       rtx set = single_set (tem);
12968                       rtx inner_dest = 0;
12969 #ifdef HAVE_cc0
12970                       rtx cc0_setter = NULL_RTX;
12971 #endif
12972
12973                       if (set != 0)
12974                         for (inner_dest = SET_DEST (set);
12975                              (GET_CODE (inner_dest) == STRICT_LOW_PART
12976                               || GET_CODE (inner_dest) == SUBREG
12977                               || GET_CODE (inner_dest) == ZERO_EXTRACT);
12978                              inner_dest = XEXP (inner_dest, 0))
12979                           ;
12980
12981                       /* Verify that it was the set, and not a clobber that
12982                          modified the register.
12983
12984                          CC0 targets must be careful to maintain setter/user
12985                          pairs.  If we cannot delete the setter due to side
12986                          effects, mark the user with an UNUSED note instead
12987                          of deleting it.  */
12988
12989                       if (set != 0 && ! side_effects_p (SET_SRC (set))
12990                           && rtx_equal_p (XEXP (note, 0), inner_dest)
12991 #ifdef HAVE_cc0
12992                           && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12993                               || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12994                                   && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12995 #endif
12996                           )
12997                         {
12998                           /* Move the notes and links of TEM elsewhere.
12999                              This might delete other dead insns recursively.
13000                              First set the pattern to something that won't use
13001                              any register.  */
13002                           rtx old_notes = REG_NOTES (tem);
13003
13004                           PATTERN (tem) = pc_rtx;
13005                           REG_NOTES (tem) = NULL;
13006
13007                           distribute_notes (old_notes, tem, tem, NULL_RTX,
13008                                             NULL_RTX, NULL_RTX);
13009                           distribute_links (LOG_LINKS (tem));
13010
13011                           SET_INSN_DELETED (tem);
13012                           if (tem == i2)
13013                             i2 = NULL_RTX;
13014
13015 #ifdef HAVE_cc0
13016                           /* Delete the setter too.  */
13017                           if (cc0_setter)
13018                             {
13019                               PATTERN (cc0_setter) = pc_rtx;
13020                               old_notes = REG_NOTES (cc0_setter);
13021                               REG_NOTES (cc0_setter) = NULL;
13022
13023                               distribute_notes (old_notes, cc0_setter,
13024                                                 cc0_setter, NULL_RTX,
13025                                                 NULL_RTX, NULL_RTX);
13026                               distribute_links (LOG_LINKS (cc0_setter));
13027
13028                               SET_INSN_DELETED (cc0_setter);
13029                               if (cc0_setter == i2)
13030                                 i2 = NULL_RTX;
13031                             }
13032 #endif
13033                         }
13034                       else
13035                         {
13036                           PUT_REG_NOTE_KIND (note, REG_UNUSED);
13037
13038                           /*  If there isn't already a REG_UNUSED note, put one
13039                               here.  Do not place a REG_DEAD note, even if
13040                               the register is also used here; that would not
13041                               match the algorithm used in lifetime analysis
13042                               and can cause the consistency check in the
13043                               scheduler to fail.  */
13044                           if (! find_regno_note (tem, REG_UNUSED,
13045                                                  REGNO (XEXP (note, 0))))
13046                             place = tem;
13047                           break;
13048                         }
13049                     }
13050                   else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
13051                            || (CALL_P (tem)
13052                                && find_reg_fusage (tem, USE, XEXP (note, 0))))
13053                     {
13054                       place = tem;
13055
13056                       /* If we are doing a 3->2 combination, and we have a
13057                          register which formerly died in i3 and was not used
13058                          by i2, which now no longer dies in i3 and is used in
13059                          i2 but does not die in i2, and place is between i2
13060                          and i3, then we may need to move a link from place to
13061                          i2.  */
13062                       if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13063                           && from_insn
13064                           && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13065                           && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13066                         {
13067                           rtx links = LOG_LINKS (place);
13068                           LOG_LINKS (place) = 0;
13069                           distribute_links (links);
13070                         }
13071                       break;
13072                     }
13073
13074                   if (tem == BB_HEAD (bb))
13075                     break;
13076                 }
13077
13078             }
13079
13080           /* If the register is set or already dead at PLACE, we needn't do
13081              anything with this note if it is still a REG_DEAD note.
13082              We check here if it is set at all, not if is it totally replaced,
13083              which is what `dead_or_set_p' checks, so also check for it being
13084              set partially.  */
13085
13086           if (place && REG_NOTE_KIND (note) == REG_DEAD)
13087             {
13088               unsigned int regno = REGNO (XEXP (note, 0));
13089               reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
13090
13091               if (dead_or_set_p (place, XEXP (note, 0))
13092                   || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13093                 {
13094                   /* Unless the register previously died in PLACE, clear
13095                      last_death.  [I no longer understand why this is
13096                      being done.] */
13097                   if (rsp->last_death != place)
13098                     rsp->last_death = 0;
13099                   place = 0;
13100                 }
13101               else
13102                 rsp->last_death = place;
13103
13104               /* If this is a death note for a hard reg that is occupying
13105                  multiple registers, ensure that we are still using all
13106                  parts of the object.  If we find a piece of the object
13107                  that is unused, we must arrange for an appropriate REG_DEAD
13108                  note to be added for it.  However, we can't just emit a USE
13109                  and tag the note to it, since the register might actually
13110                  be dead; so we recourse, and the recursive call then finds
13111                  the previous insn that used this register.  */
13112
13113               if (place && regno < FIRST_PSEUDO_REGISTER
13114                   && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13115                 {
13116                   unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13117                   int all_used = 1;
13118                   unsigned int i;
13119
13120                   for (i = regno; i < endregno; i++)
13121                     if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13122                          && ! find_regno_fusage (place, USE, i))
13123                         || dead_or_set_regno_p (place, i))
13124                       all_used = 0;
13125
13126                   if (! all_used)
13127                     {
13128                       /* Put only REG_DEAD notes for pieces that are
13129                          not already dead or set.  */
13130
13131                       for (i = regno; i < endregno;
13132                            i += hard_regno_nregs[i][reg_raw_mode[i]])
13133                         {
13134                           rtx piece = regno_reg_rtx[i];
13135                           basic_block bb = this_basic_block;
13136
13137                           if (! dead_or_set_p (place, piece)
13138                               && ! reg_bitfield_target_p (piece,
13139                                                           PATTERN (place)))
13140                             {
13141                               rtx new_note = alloc_reg_note (REG_DEAD, piece,
13142                                                              NULL_RTX);
13143
13144                               distribute_notes (new_note, place, place,
13145                                                 NULL_RTX, NULL_RTX, NULL_RTX);
13146                             }
13147                           else if (! refers_to_regno_p (i, i + 1,
13148                                                         PATTERN (place), 0)
13149                                    && ! find_regno_fusage (place, USE, i))
13150                             for (tem = PREV_INSN (place); ;
13151                                  tem = PREV_INSN (tem))
13152                               {
13153                                 if (!NONDEBUG_INSN_P (tem))
13154                                   {
13155                                     if (tem == BB_HEAD (bb))
13156                                       break;
13157                                     continue;
13158                                   }
13159                                 if (dead_or_set_p (tem, piece)
13160                                     || reg_bitfield_target_p (piece,
13161                                                               PATTERN (tem)))
13162                                   {
13163                                     add_reg_note (tem, REG_UNUSED, piece);
13164                                     break;
13165                                   }
13166                               }
13167
13168                         }
13169
13170                       place = 0;
13171                     }
13172                 }
13173             }
13174           break;
13175
13176         default:
13177           /* Any other notes should not be present at this point in the
13178              compilation.  */
13179           gcc_unreachable ();
13180         }
13181
13182       if (place)
13183         {
13184           XEXP (note, 1) = REG_NOTES (place);
13185           REG_NOTES (place) = note;
13186         }
13187
13188       if (place2)
13189         add_reg_note (place2, REG_NOTE_KIND (note), XEXP (note, 0));
13190     }
13191 }
13192 \f
13193 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13194    I3, I2, and I1 to new locations.  This is also called to add a link
13195    pointing at I3 when I3's destination is changed.  */
13196
13197 static void
13198 distribute_links (rtx links)
13199 {
13200   rtx link, next_link;
13201
13202   for (link = links; link; link = next_link)
13203     {
13204       rtx place = 0;
13205       rtx insn;
13206       rtx set, reg;
13207
13208       next_link = XEXP (link, 1);
13209
13210       /* If the insn that this link points to is a NOTE or isn't a single
13211          set, ignore it.  In the latter case, it isn't clear what we
13212          can do other than ignore the link, since we can't tell which
13213          register it was for.  Such links wouldn't be used by combine
13214          anyway.
13215
13216          It is not possible for the destination of the target of the link to
13217          have been changed by combine.  The only potential of this is if we
13218          replace I3, I2, and I1 by I3 and I2.  But in that case the
13219          destination of I2 also remains unchanged.  */
13220
13221       if (NOTE_P (XEXP (link, 0))
13222           || (set = single_set (XEXP (link, 0))) == 0)
13223         continue;
13224
13225       reg = SET_DEST (set);
13226       while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
13227              || GET_CODE (reg) == STRICT_LOW_PART)
13228         reg = XEXP (reg, 0);
13229
13230       /* A LOG_LINK is defined as being placed on the first insn that uses
13231          a register and points to the insn that sets the register.  Start
13232          searching at the next insn after the target of the link and stop
13233          when we reach a set of the register or the end of the basic block.
13234
13235          Note that this correctly handles the link that used to point from
13236          I3 to I2.  Also note that not much searching is typically done here
13237          since most links don't point very far away.  */
13238
13239       for (insn = NEXT_INSN (XEXP (link, 0));
13240            (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
13241                      || BB_HEAD (this_basic_block->next_bb) != insn));
13242            insn = NEXT_INSN (insn))
13243         if (DEBUG_INSN_P (insn))
13244           continue;
13245         else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13246           {
13247             if (reg_referenced_p (reg, PATTERN (insn)))
13248               place = insn;
13249             break;
13250           }
13251         else if (CALL_P (insn)
13252                  && find_reg_fusage (insn, USE, reg))
13253           {
13254             place = insn;
13255             break;
13256           }
13257         else if (INSN_P (insn) && reg_set_p (reg, insn))
13258           break;
13259
13260       /* If we found a place to put the link, place it there unless there
13261          is already a link to the same insn as LINK at that point.  */
13262
13263       if (place)
13264         {
13265           rtx link2;
13266
13267           for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
13268             if (XEXP (link2, 0) == XEXP (link, 0))
13269               break;
13270
13271           if (link2 == 0)
13272             {
13273               XEXP (link, 1) = LOG_LINKS (place);
13274               LOG_LINKS (place) = link;
13275
13276               /* Set added_links_insn to the earliest insn we added a
13277                  link to.  */
13278               if (added_links_insn == 0
13279                   || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
13280                 added_links_insn = place;
13281             }
13282         }
13283     }
13284 }
13285 \f
13286 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
13287    Check whether the expression pointer to by LOC is a register or
13288    memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
13289    Otherwise return zero.  */
13290
13291 static int
13292 unmentioned_reg_p_1 (rtx *loc, void *expr)
13293 {
13294   rtx x = *loc;
13295
13296   if (x != NULL_RTX
13297       && (REG_P (x) || MEM_P (x))
13298       && ! reg_mentioned_p (x, (rtx) expr))
13299     return 1;
13300   return 0;
13301 }
13302
13303 /* Check for any register or memory mentioned in EQUIV that is not
13304    mentioned in EXPR.  This is used to restrict EQUIV to "specializations"
13305    of EXPR where some registers may have been replaced by constants.  */
13306
13307 static bool
13308 unmentioned_reg_p (rtx equiv, rtx expr)
13309 {
13310   return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
13311 }
13312 \f
13313 void
13314 dump_combine_stats (FILE *file)
13315 {
13316   fprintf
13317     (file,
13318      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13319      combine_attempts, combine_merges, combine_extras, combine_successes);
13320 }
13321
13322 void
13323 dump_combine_total_stats (FILE *file)
13324 {
13325   fprintf
13326     (file,
13327      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13328      total_attempts, total_merges, total_extras, total_successes);
13329 }
13330 \f
13331 static bool
13332 gate_handle_combine (void)
13333 {
13334   return (optimize > 0);
13335 }
13336
13337 /* Try combining insns through substitution.  */
13338 static unsigned int
13339 rest_of_handle_combine (void)
13340 {
13341   int rebuild_jump_labels_after_combine;
13342
13343   df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
13344   df_note_add_problem ();
13345   df_analyze ();
13346
13347   regstat_init_n_sets_and_refs ();
13348
13349   rebuild_jump_labels_after_combine
13350     = combine_instructions (get_insns (), max_reg_num ());
13351
13352   /* Combining insns may have turned an indirect jump into a
13353      direct jump.  Rebuild the JUMP_LABEL fields of jumping
13354      instructions.  */
13355   if (rebuild_jump_labels_after_combine)
13356     {
13357       timevar_push (TV_JUMP);
13358       rebuild_jump_labels (get_insns ());
13359       cleanup_cfg (0);
13360       timevar_pop (TV_JUMP);
13361     }
13362
13363   regstat_free_n_sets_and_refs ();
13364   return 0;
13365 }
13366
13367 struct rtl_opt_pass pass_combine =
13368 {
13369  {
13370   RTL_PASS,
13371   "combine",                            /* name */
13372   gate_handle_combine,                  /* gate */
13373   rest_of_handle_combine,               /* execute */
13374   NULL,                                 /* sub */
13375   NULL,                                 /* next */
13376   0,                                    /* static_pass_number */
13377   TV_COMBINE,                           /* tv_id */
13378   PROP_cfglayout,                       /* properties_required */
13379   0,                                    /* properties_provided */
13380   0,                                    /* properties_destroyed */
13381   0,                                    /* todo_flags_start */
13382   TODO_dump_func |
13383   TODO_df_finish | TODO_verify_rtl_sharing |
13384   TODO_ggc_collect,                     /* todo_flags_finish */
13385  }
13386 };