OSDN Git Service

2010-06-18 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005, 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 #endif
2275
2276 /* Auxiliary data structure for propagate_for_debug_stmt.  */
2277
2278 struct rtx_subst_pair
2279 {
2280   rtx to;
2281   bool adjusted;
2282   bool after;
2283 };
2284
2285 /* DATA points to an rtx_subst_pair.  Return the value that should be
2286    substituted.  */
2287
2288 static rtx
2289 propagate_for_debug_subst (rtx from, const_rtx old_rtx, void *data)
2290 {
2291   struct rtx_subst_pair *pair = (struct rtx_subst_pair *)data;
2292
2293   if (!rtx_equal_p (from, old_rtx))
2294     return NULL_RTX;
2295   if (!pair->adjusted)
2296     {
2297       pair->adjusted = true;
2298 #ifdef AUTO_INC_DEC
2299       pair->to = cleanup_auto_inc_dec (pair->to, pair->after, VOIDmode);
2300 #else
2301       pair->to = copy_rtx (pair->to);
2302 #endif
2303       pair->to = make_compound_operation (pair->to, SET);
2304       return pair->to;
2305     }
2306   return copy_rtx (pair->to);
2307 }
2308
2309 /* Replace occurrences of DEST with SRC in DEBUG_INSNs between INSN
2310    and LAST.  If MOVE holds, debug insns must also be moved past
2311    LAST.  */
2312
2313 static void
2314 propagate_for_debug (rtx insn, rtx last, rtx dest, rtx src, bool move)
2315 {
2316   rtx next, move_pos = move ? last : NULL_RTX, loc;
2317
2318   struct rtx_subst_pair p;
2319   p.to = src;
2320   p.adjusted = false;
2321   p.after = move;
2322
2323   next = NEXT_INSN (insn);
2324   while (next != last)
2325     {
2326       insn = next;
2327       next = NEXT_INSN (insn);
2328       if (DEBUG_INSN_P (insn))
2329         {
2330           loc = simplify_replace_fn_rtx (INSN_VAR_LOCATION_LOC (insn),
2331                                          dest, propagate_for_debug_subst, &p);
2332           if (loc == INSN_VAR_LOCATION_LOC (insn))
2333             continue;
2334           INSN_VAR_LOCATION_LOC (insn) = loc;
2335           if (move_pos)
2336             {
2337               remove_insn (insn);
2338               PREV_INSN (insn) = NEXT_INSN (insn) = NULL_RTX;
2339               move_pos = emit_debug_insn_after (insn, move_pos);
2340             }
2341           else
2342             df_insn_rescan (insn);
2343         }
2344     }
2345 }
2346
2347 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2348    Note that the INSN should be deleted *after* removing dead edges, so
2349    that the kept edge is the fallthrough edge for a (set (pc) (pc))
2350    but not for a (set (pc) (label_ref FOO)).  */
2351
2352 static void
2353 update_cfg_for_uncondjump (rtx insn)
2354 {
2355   basic_block bb = BLOCK_FOR_INSN (insn);
2356   bool at_end = (BB_END (bb) == insn);
2357
2358   if (at_end)
2359     purge_dead_edges (bb);
2360
2361   delete_insn (insn);
2362   if (at_end && EDGE_COUNT (bb->succs) == 1)
2363     single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2364 }
2365
2366
2367 /* Try to combine the insns I1 and I2 into I3.
2368    Here I1 and I2 appear earlier than I3.
2369    I1 can be zero; then we combine just I2 into I3.
2370
2371    If we are combining three insns and the resulting insn is not recognized,
2372    try splitting it into two insns.  If that happens, I2 and I3 are retained
2373    and I1 is pseudo-deleted by turning it into a NOTE.  Otherwise, I1 and I2
2374    are pseudo-deleted.
2375
2376    Return 0 if the combination does not work.  Then nothing is changed.
2377    If we did the combination, return the insn at which combine should
2378    resume scanning.
2379
2380    Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2381    new direct jump instruction.  */
2382
2383 static rtx
2384 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
2385 {
2386   /* New patterns for I3 and I2, respectively.  */
2387   rtx newpat, newi2pat = 0;
2388   rtvec newpat_vec_with_clobbers = 0;
2389   int substed_i2 = 0, substed_i1 = 0;
2390   /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead.  */
2391   int added_sets_1, added_sets_2;
2392   /* Total number of SETs to put into I3.  */
2393   int total_sets;
2394   /* Nonzero if I2's body now appears in I3.  */
2395   int i2_is_used;
2396   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
2397   int insn_code_number, i2_code_number = 0, other_code_number = 0;
2398   /* Contains I3 if the destination of I3 is used in its source, which means
2399      that the old life of I3 is being killed.  If that usage is placed into
2400      I2 and not in I3, a REG_DEAD note must be made.  */
2401   rtx i3dest_killed = 0;
2402   /* SET_DEST and SET_SRC of I2 and I1.  */
2403   rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0;
2404   /* Set if I2DEST was reused as a scratch register.  */
2405   bool i2scratch = false;
2406   /* PATTERN (I1) and PATTERN (I2), or a copy of it in certain cases.  */
2407   rtx i1pat = 0, i2pat = 0;
2408   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
2409   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2410   int i2dest_killed = 0, i1dest_killed = 0;
2411   int i1_feeds_i3 = 0;
2412   /* Notes that must be added to REG_NOTES in I3 and I2.  */
2413   rtx new_i3_notes, new_i2_notes;
2414   /* Notes that we substituted I3 into I2 instead of the normal case.  */
2415   int i3_subst_into_i2 = 0;
2416   /* Notes that I1, I2 or I3 is a MULT operation.  */
2417   int have_mult = 0;
2418   int swap_i2i3 = 0;
2419   int changed_i3_dest = 0;
2420
2421   int maxreg;
2422   rtx temp;
2423   rtx link;
2424   rtx other_pat = 0;
2425   rtx new_other_notes;
2426   int i;
2427
2428   /* Exit early if one of the insns involved can't be used for
2429      combinations.  */
2430   if (cant_combine_insn_p (i3)
2431       || cant_combine_insn_p (i2)
2432       || (i1 && cant_combine_insn_p (i1))
2433       || likely_spilled_retval_p (i3))
2434     return 0;
2435
2436   combine_attempts++;
2437   undobuf.other_insn = 0;
2438
2439   /* Reset the hard register usage information.  */
2440   CLEAR_HARD_REG_SET (newpat_used_regs);
2441
2442   if (dump_file && (dump_flags & TDF_DETAILS))
2443     {
2444       if (i1)
2445         fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2446                  INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2447       else
2448         fprintf (dump_file, "\nTrying %d -> %d:\n",
2449                  INSN_UID (i2), INSN_UID (i3));
2450     }
2451
2452   /* If I1 and I2 both feed I3, they can be in any order.  To simplify the
2453      code below, set I1 to be the earlier of the two insns.  */
2454   if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2455     temp = i1, i1 = i2, i2 = temp;
2456
2457   added_links_insn = 0;
2458
2459   /* First check for one important special-case that the code below will
2460      not handle.  Namely, the case where I1 is zero, I2 is a PARALLEL
2461      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
2462      we may be able to replace that destination with the destination of I3.
2463      This occurs in the common code where we compute both a quotient and
2464      remainder into a structure, in which case we want to do the computation
2465      directly into the structure to avoid register-register copies.
2466
2467      Note that this case handles both multiple sets in I2 and also
2468      cases where I2 has a number of CLOBBER or PARALLELs.
2469
2470      We make very conservative checks below and only try to handle the
2471      most common cases of this.  For example, we only handle the case
2472      where I2 and I3 are adjacent to avoid making difficult register
2473      usage tests.  */
2474
2475   if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2476       && REG_P (SET_SRC (PATTERN (i3)))
2477       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2478       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2479       && GET_CODE (PATTERN (i2)) == PARALLEL
2480       && ! side_effects_p (SET_DEST (PATTERN (i3)))
2481       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2482          below would need to check what is inside (and reg_overlap_mentioned_p
2483          doesn't support those codes anyway).  Don't allow those destinations;
2484          the resulting insn isn't likely to be recognized anyway.  */
2485       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2486       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2487       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2488                                     SET_DEST (PATTERN (i3)))
2489       && next_active_insn (i2) == i3)
2490     {
2491       rtx p2 = PATTERN (i2);
2492
2493       /* Make sure that the destination of I3,
2494          which we are going to substitute into one output of I2,
2495          is not used within another output of I2.  We must avoid making this:
2496          (parallel [(set (mem (reg 69)) ...)
2497                     (set (reg 69) ...)])
2498          which is not well-defined as to order of actions.
2499          (Besides, reload can't handle output reloads for this.)
2500
2501          The problem can also happen if the dest of I3 is a memory ref,
2502          if another dest in I2 is an indirect memory ref.  */
2503       for (i = 0; i < XVECLEN (p2, 0); i++)
2504         if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2505              || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2506             && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2507                                         SET_DEST (XVECEXP (p2, 0, i))))
2508           break;
2509
2510       if (i == XVECLEN (p2, 0))
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               && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2515             {
2516               combine_merges++;
2517
2518               subst_insn = i3;
2519               subst_low_luid = DF_INSN_LUID (i2);
2520
2521               added_sets_2 = added_sets_1 = 0;
2522               i2src = SET_DEST (PATTERN (i3));
2523               i2dest = SET_SRC (PATTERN (i3));
2524               i2dest_killed = dead_or_set_p (i2, i2dest);
2525
2526               /* Replace the dest in I2 with our dest and make the resulting
2527                  insn the new pattern for I3.  Then skip to where we
2528                  validate the pattern.  Everything was set up above.  */
2529               SUBST (SET_DEST (XVECEXP (p2, 0, i)),
2530                      SET_DEST (PATTERN (i3)));
2531
2532               newpat = p2;
2533               i3_subst_into_i2 = 1;
2534               goto validate_replacement;
2535             }
2536     }
2537
2538   /* If I2 is setting a pseudo to a constant and I3 is setting some
2539      sub-part of it to another constant, merge them by making a new
2540      constant.  */
2541   if (i1 == 0
2542       && (temp = single_set (i2)) != 0
2543       && (CONST_INT_P (SET_SRC (temp))
2544           || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
2545       && GET_CODE (PATTERN (i3)) == SET
2546       && (CONST_INT_P (SET_SRC (PATTERN (i3)))
2547           || GET_CODE (SET_SRC (PATTERN (i3))) == CONST_DOUBLE)
2548       && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp)))
2549     {
2550       rtx dest = SET_DEST (PATTERN (i3));
2551       int offset = -1;
2552       int width = 0;
2553
2554       if (GET_CODE (dest) == ZERO_EXTRACT)
2555         {
2556           if (CONST_INT_P (XEXP (dest, 1))
2557               && CONST_INT_P (XEXP (dest, 2)))
2558             {
2559               width = INTVAL (XEXP (dest, 1));
2560               offset = INTVAL (XEXP (dest, 2));
2561               dest = XEXP (dest, 0);
2562               if (BITS_BIG_ENDIAN)
2563                 offset = GET_MODE_BITSIZE (GET_MODE (dest)) - width - offset;
2564             }
2565         }
2566       else
2567         {
2568           if (GET_CODE (dest) == STRICT_LOW_PART)
2569             dest = XEXP (dest, 0);
2570           width = GET_MODE_BITSIZE (GET_MODE (dest));
2571           offset = 0;
2572         }
2573
2574       if (offset >= 0)
2575         {
2576           /* If this is the low part, we're done.  */
2577           if (subreg_lowpart_p (dest))
2578             ;
2579           /* Handle the case where inner is twice the size of outer.  */
2580           else if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2581                    == 2 * GET_MODE_BITSIZE (GET_MODE (dest)))
2582             offset += GET_MODE_BITSIZE (GET_MODE (dest));
2583           /* Otherwise give up for now.  */
2584           else
2585             offset = -1;
2586         }
2587
2588       if (offset >= 0
2589           && (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2590               <= HOST_BITS_PER_DOUBLE_INT))
2591         {
2592           double_int m, o, i;
2593           rtx inner = SET_SRC (PATTERN (i3));
2594           rtx outer = SET_SRC (temp);
2595
2596           o = rtx_to_double_int (outer);
2597           i = rtx_to_double_int (inner);
2598
2599           m = double_int_mask (width);
2600           i = double_int_and (i, m);
2601           m = double_int_lshift (m, offset, HOST_BITS_PER_DOUBLE_INT, false);
2602           i = double_int_lshift (i, offset, HOST_BITS_PER_DOUBLE_INT, false);
2603           o = double_int_ior (double_int_and (o, double_int_not (m)), i);
2604
2605           combine_merges++;
2606           subst_insn = i3;
2607           subst_low_luid = DF_INSN_LUID (i2);
2608           added_sets_2 = added_sets_1 = 0;
2609           i2dest = SET_DEST (temp);
2610           i2dest_killed = dead_or_set_p (i2, i2dest);
2611
2612           /* Replace the source in I2 with the new constant and make the
2613              resulting insn the new pattern for I3.  Then skip to where we
2614              validate the pattern.  Everything was set up above.  */
2615           SUBST (SET_SRC (temp),
2616                  immed_double_int_const (o, GET_MODE (SET_DEST (temp))));
2617
2618           newpat = PATTERN (i2);
2619
2620           /* The dest of I3 has been replaced with the dest of I2.  */
2621           changed_i3_dest = 1;
2622           goto validate_replacement;
2623         }
2624     }
2625
2626 #ifndef HAVE_cc0
2627   /* If we have no I1 and I2 looks like:
2628         (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2629                    (set Y OP)])
2630      make up a dummy I1 that is
2631         (set Y OP)
2632      and change I2 to be
2633         (set (reg:CC X) (compare:CC Y (const_int 0)))
2634
2635      (We can ignore any trailing CLOBBERs.)
2636
2637      This undoes a previous combination and allows us to match a branch-and-
2638      decrement insn.  */
2639
2640   if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2641       && XVECLEN (PATTERN (i2), 0) >= 2
2642       && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2643       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2644           == MODE_CC)
2645       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2646       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2647       && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2648       && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2649       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2650                       SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2651     {
2652       for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2653         if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2654           break;
2655
2656       if (i == 1)
2657         {
2658           /* We make I1 with the same INSN_UID as I2.  This gives it
2659              the same DF_INSN_LUID for value tracking.  Our fake I1 will
2660              never appear in the insn stream so giving it the same INSN_UID
2661              as I2 will not cause a problem.  */
2662
2663           i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
2664                              BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
2665                              XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX);
2666
2667           SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2668           SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2669                  SET_DEST (PATTERN (i1)));
2670         }
2671     }
2672 #endif
2673
2674   /* Verify that I2 and I1 are valid for combining.  */
2675   if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
2676       || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
2677     {
2678       undo_all ();
2679       return 0;
2680     }
2681
2682   /* Record whether I2DEST is used in I2SRC and similarly for the other
2683      cases.  Knowing this will help in register status updating below.  */
2684   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2685   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2686   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2687   i2dest_killed = dead_or_set_p (i2, i2dest);
2688   i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2689
2690   /* See if I1 directly feeds into I3.  It does if I1DEST is not used
2691      in I2SRC.  */
2692   i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
2693
2694   /* Ensure that I3's pattern can be the destination of combines.  */
2695   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
2696                           i1 && i2dest_in_i1src && i1_feeds_i3,
2697                           &i3dest_killed))
2698     {
2699       undo_all ();
2700       return 0;
2701     }
2702
2703   /* See if any of the insns is a MULT operation.  Unless one is, we will
2704      reject a combination that is, since it must be slower.  Be conservative
2705      here.  */
2706   if (GET_CODE (i2src) == MULT
2707       || (i1 != 0 && GET_CODE (i1src) == MULT)
2708       || (GET_CODE (PATTERN (i3)) == SET
2709           && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2710     have_mult = 1;
2711
2712   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2713      We used to do this EXCEPT in one case: I3 has a post-inc in an
2714      output operand.  However, that exception can give rise to insns like
2715         mov r3,(r3)+
2716      which is a famous insn on the PDP-11 where the value of r3 used as the
2717      source was model-dependent.  Avoid this sort of thing.  */
2718
2719 #if 0
2720   if (!(GET_CODE (PATTERN (i3)) == SET
2721         && REG_P (SET_SRC (PATTERN (i3)))
2722         && MEM_P (SET_DEST (PATTERN (i3)))
2723         && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2724             || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2725     /* It's not the exception.  */
2726 #endif
2727 #ifdef AUTO_INC_DEC
2728     for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2729       if (REG_NOTE_KIND (link) == REG_INC
2730           && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2731               || (i1 != 0
2732                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2733         {
2734           undo_all ();
2735           return 0;
2736         }
2737 #endif
2738
2739   /* See if the SETs in I1 or I2 need to be kept around in the merged
2740      instruction: whenever the value set there is still needed past I3.
2741      For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
2742
2743      For the SET in I1, we have two cases:  If I1 and I2 independently
2744      feed into I3, the set in I1 needs to be kept around if I1DEST dies
2745      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
2746      in I1 needs to be kept around unless I1DEST dies or is set in either
2747      I2 or I3.  We can distinguish these cases by seeing if I2SRC mentions
2748      I1DEST.  If so, we know I1 feeds into I2.  */
2749
2750   added_sets_2 = ! dead_or_set_p (i3, i2dest);
2751
2752   added_sets_1
2753     = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
2754                : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
2755
2756   /* If the set in I2 needs to be kept around, we must make a copy of
2757      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2758      PATTERN (I2), we are only substituting for the original I1DEST, not into
2759      an already-substituted copy.  This also prevents making self-referential
2760      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2761      I2DEST.  */
2762
2763   if (added_sets_2)
2764     {
2765       if (GET_CODE (PATTERN (i2)) == PARALLEL)
2766         i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
2767       else
2768         i2pat = copy_rtx (PATTERN (i2));
2769     }
2770
2771   if (added_sets_1)
2772     {
2773       if (GET_CODE (PATTERN (i1)) == PARALLEL)
2774         i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
2775       else
2776         i1pat = copy_rtx (PATTERN (i1));
2777     }
2778
2779   combine_merges++;
2780
2781   /* Substitute in the latest insn for the regs set by the earlier ones.  */
2782
2783   maxreg = max_reg_num ();
2784
2785   subst_insn = i3;
2786
2787 #ifndef HAVE_cc0
2788   /* Many machines that don't use CC0 have insns that can both perform an
2789      arithmetic operation and set the condition code.  These operations will
2790      be represented as a PARALLEL with the first element of the vector
2791      being a COMPARE of an arithmetic operation with the constant zero.
2792      The second element of the vector will set some pseudo to the result
2793      of the same arithmetic operation.  If we simplify the COMPARE, we won't
2794      match such a pattern and so will generate an extra insn.   Here we test
2795      for this case, where both the comparison and the operation result are
2796      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2797      I2SRC.  Later we will make the PARALLEL that contains I2.  */
2798
2799   if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2800       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2801       && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
2802       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2803     {
2804 #ifdef SELECT_CC_MODE
2805       rtx *cc_use;
2806       enum machine_mode compare_mode;
2807 #endif
2808
2809       newpat = PATTERN (i3);
2810       SUBST (XEXP (SET_SRC (newpat), 0), i2src);
2811
2812       i2_is_used = 1;
2813
2814 #ifdef SELECT_CC_MODE
2815       /* See if a COMPARE with the operand we substituted in should be done
2816          with the mode that is currently being used.  If not, do the same
2817          processing we do in `subst' for a SET; namely, if the destination
2818          is used only once, try to replace it with a register of the proper
2819          mode and also replace the COMPARE.  */
2820       if (undobuf.other_insn == 0
2821           && (cc_use = find_single_use (SET_DEST (newpat), i3,
2822                                         &undobuf.other_insn))
2823           && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
2824                                               i2src, const0_rtx))
2825               != GET_MODE (SET_DEST (newpat))))
2826         {
2827           if (can_change_dest_mode(SET_DEST (newpat), added_sets_2,
2828                                    compare_mode))
2829             {
2830               unsigned int regno = REGNO (SET_DEST (newpat));
2831               rtx new_dest;
2832
2833               if (regno < FIRST_PSEUDO_REGISTER)
2834                 new_dest = gen_rtx_REG (compare_mode, regno);
2835               else
2836                 {
2837                   SUBST_MODE (regno_reg_rtx[regno], compare_mode);
2838                   new_dest = regno_reg_rtx[regno];
2839                 }
2840
2841               SUBST (SET_DEST (newpat), new_dest);
2842               SUBST (XEXP (*cc_use, 0), new_dest);
2843               SUBST (SET_SRC (newpat),
2844                      gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
2845             }
2846           else
2847             undobuf.other_insn = 0;
2848         }
2849 #endif
2850     }
2851   else
2852 #endif
2853     {
2854       /* It is possible that the source of I2 or I1 may be performing
2855          an unneeded operation, such as a ZERO_EXTEND of something
2856          that is known to have the high part zero.  Handle that case
2857          by letting subst look at the innermost one of them.
2858
2859          Another way to do this would be to have a function that tries
2860          to simplify a single insn instead of merging two or more
2861          insns.  We don't do this because of the potential of infinite
2862          loops and because of the potential extra memory required.
2863          However, doing it the way we are is a bit of a kludge and
2864          doesn't catch all cases.
2865
2866          But only do this if -fexpensive-optimizations since it slows
2867          things down and doesn't usually win.
2868
2869          This is not done in the COMPARE case above because the
2870          unmodified I2PAT is used in the PARALLEL and so a pattern
2871          with a modified I2SRC would not match.  */
2872
2873       if (flag_expensive_optimizations)
2874         {
2875           /* Pass pc_rtx so no substitutions are done, just
2876              simplifications.  */
2877           if (i1)
2878             {
2879               subst_low_luid = DF_INSN_LUID (i1);
2880               i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
2881             }
2882           else
2883             {
2884               subst_low_luid = DF_INSN_LUID (i2);
2885               i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
2886             }
2887         }
2888
2889       n_occurrences = 0;                /* `subst' counts here */
2890
2891       /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
2892          need to make a unique copy of I2SRC each time we substitute it
2893          to avoid self-referential rtl.  */
2894
2895       subst_low_luid = DF_INSN_LUID (i2);
2896       newpat = subst (PATTERN (i3), i2dest, i2src, 0,
2897                       ! i1_feeds_i3 && i1dest_in_i1src);
2898       substed_i2 = 1;
2899
2900       /* Record whether i2's body now appears within i3's body.  */
2901       i2_is_used = n_occurrences;
2902     }
2903
2904   /* If we already got a failure, don't try to do more.  Otherwise,
2905      try to substitute in I1 if we have it.  */
2906
2907   if (i1 && GET_CODE (newpat) != CLOBBER)
2908     {
2909       /* Check that an autoincrement side-effect on I1 has not been lost.
2910          This happens if I1DEST is mentioned in I2 and dies there, and
2911          has disappeared from the new pattern.  */
2912       if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2913            && !i1_feeds_i3
2914            && dead_or_set_p (i2, i1dest)
2915            && !reg_overlap_mentioned_p (i1dest, newpat))
2916           /* Before we can do this substitution, we must redo the test done
2917              above (see detailed comments there) that ensures  that I1DEST
2918              isn't mentioned in any SETs in NEWPAT that are field assignments.  */
2919           || !combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX, 0, 0))
2920         {
2921           undo_all ();
2922           return 0;
2923         }
2924
2925       n_occurrences = 0;
2926       subst_low_luid = DF_INSN_LUID (i1);
2927       newpat = subst (newpat, i1dest, i1src, 0, 0);
2928       substed_i1 = 1;
2929     }
2930
2931   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
2932      to count all the ways that I2SRC and I1SRC can be used.  */
2933   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
2934        && i2_is_used + added_sets_2 > 1)
2935       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2936           && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
2937               > 1))
2938       /* Fail if we tried to make a new register.  */
2939       || max_reg_num () != maxreg
2940       /* Fail if we couldn't do something and have a CLOBBER.  */
2941       || GET_CODE (newpat) == CLOBBER
2942       /* Fail if this new pattern is a MULT and we didn't have one before
2943          at the outer level.  */
2944       || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
2945           && ! have_mult))
2946     {
2947       undo_all ();
2948       return 0;
2949     }
2950
2951   /* If the actions of the earlier insns must be kept
2952      in addition to substituting them into the latest one,
2953      we must make a new PARALLEL for the latest insn
2954      to hold additional the SETs.  */
2955
2956   if (added_sets_1 || added_sets_2)
2957     {
2958       combine_extras++;
2959
2960       if (GET_CODE (newpat) == PARALLEL)
2961         {
2962           rtvec old = XVEC (newpat, 0);
2963           total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
2964           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2965           memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
2966                   sizeof (old->elem[0]) * old->num_elem);
2967         }
2968       else
2969         {
2970           rtx old = newpat;
2971           total_sets = 1 + added_sets_1 + added_sets_2;
2972           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2973           XVECEXP (newpat, 0, 0) = old;
2974         }
2975
2976       if (added_sets_1)
2977         XVECEXP (newpat, 0, --total_sets) = i1pat;
2978
2979       if (added_sets_2)
2980         {
2981           /* If there is no I1, use I2's body as is.  We used to also not do
2982              the subst call below if I2 was substituted into I3,
2983              but that could lose a simplification.  */
2984           if (i1 == 0)
2985             XVECEXP (newpat, 0, --total_sets) = i2pat;
2986           else
2987             /* See comment where i2pat is assigned.  */
2988             XVECEXP (newpat, 0, --total_sets)
2989               = subst (i2pat, i1dest, i1src, 0, 0);
2990         }
2991     }
2992
2993  validate_replacement:
2994
2995   /* Note which hard regs this insn has as inputs.  */
2996   mark_used_regs_combine (newpat);
2997
2998   /* If recog_for_combine fails, it strips existing clobbers.  If we'll
2999      consider splitting this pattern, we might need these clobbers.  */
3000   if (i1 && GET_CODE (newpat) == PARALLEL
3001       && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3002     {
3003       int len = XVECLEN (newpat, 0);
3004
3005       newpat_vec_with_clobbers = rtvec_alloc (len);
3006       for (i = 0; i < len; i++)
3007         RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3008     }
3009
3010   /* Is the result of combination a valid instruction?  */
3011   insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3012
3013   /* If the result isn't valid, see if it is a PARALLEL of two SETs where
3014      the second SET's destination is a register that is unused and isn't
3015      marked as an instruction that might trap in an EH region.  In that case,
3016      we just need the first SET.   This can occur when simplifying a divmod
3017      insn.  We *must* test for this case here because the code below that
3018      splits two independent SETs doesn't handle this case correctly when it
3019      updates the register status.
3020
3021      It's pointless doing this if we originally had two sets, one from
3022      i3, and one from i2.  Combining then splitting the parallel results
3023      in the original i2 again plus an invalid insn (which we delete).
3024      The net effect is only to move instructions around, which makes
3025      debug info less accurate.
3026
3027      Also check the case where the first SET's destination is unused.
3028      That would not cause incorrect code, but does cause an unneeded
3029      insn to remain.  */
3030
3031   if (insn_code_number < 0
3032       && !(added_sets_2 && i1 == 0)
3033       && GET_CODE (newpat) == PARALLEL
3034       && XVECLEN (newpat, 0) == 2
3035       && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3036       && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3037       && asm_noperands (newpat) < 0)
3038     {
3039       rtx set0 = XVECEXP (newpat, 0, 0);
3040       rtx set1 = XVECEXP (newpat, 0, 1);
3041
3042       if (((REG_P (SET_DEST (set1))
3043             && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3044            || (GET_CODE (SET_DEST (set1)) == SUBREG
3045                && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3046           && insn_nothrow_p (i3)
3047           && !side_effects_p (SET_SRC (set1)))
3048         {
3049           newpat = set0;
3050           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3051         }
3052
3053       else if (((REG_P (SET_DEST (set0))
3054                  && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3055                 || (GET_CODE (SET_DEST (set0)) == SUBREG
3056                     && find_reg_note (i3, REG_UNUSED,
3057                                       SUBREG_REG (SET_DEST (set0)))))
3058                && insn_nothrow_p (i3)
3059                && !side_effects_p (SET_SRC (set0)))
3060         {
3061           newpat = set1;
3062           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3063
3064           if (insn_code_number >= 0)
3065             changed_i3_dest = 1;
3066         }
3067     }
3068
3069   /* If we were combining three insns and the result is a simple SET
3070      with no ASM_OPERANDS that wasn't recognized, try to split it into two
3071      insns.  There are two ways to do this.  It can be split using a
3072      machine-specific method (like when you have an addition of a large
3073      constant) or by combine in the function find_split_point.  */
3074
3075   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3076       && asm_noperands (newpat) < 0)
3077     {
3078       rtx parallel, m_split, *split;
3079
3080       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
3081          use I2DEST as a scratch register will help.  In the latter case,
3082          convert I2DEST to the mode of the source of NEWPAT if we can.  */
3083
3084       m_split = combine_split_insns (newpat, i3);
3085
3086       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3087          inputs of NEWPAT.  */
3088
3089       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3090          possible to try that as a scratch reg.  This would require adding
3091          more code to make it work though.  */
3092
3093       if (m_split == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3094         {
3095           enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3096
3097           /* First try to split using the original register as a
3098              scratch register.  */
3099           parallel = gen_rtx_PARALLEL (VOIDmode,
3100                                        gen_rtvec (2, newpat,
3101                                                   gen_rtx_CLOBBER (VOIDmode,
3102                                                                    i2dest)));
3103           m_split = combine_split_insns (parallel, i3);
3104
3105           /* If that didn't work, try changing the mode of I2DEST if
3106              we can.  */
3107           if (m_split == 0
3108               && new_mode != GET_MODE (i2dest)
3109               && new_mode != VOIDmode
3110               && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3111             {
3112               enum machine_mode old_mode = GET_MODE (i2dest);
3113               rtx ni2dest;
3114
3115               if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3116                 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3117               else
3118                 {
3119                   SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3120                   ni2dest = regno_reg_rtx[REGNO (i2dest)];
3121                 }
3122
3123               parallel = (gen_rtx_PARALLEL
3124                           (VOIDmode,
3125                            gen_rtvec (2, newpat,
3126                                       gen_rtx_CLOBBER (VOIDmode,
3127                                                        ni2dest))));
3128               m_split = combine_split_insns (parallel, i3);
3129
3130               if (m_split == 0
3131                   && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3132                 {
3133                   struct undo *buf;
3134
3135                   adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3136                   buf = undobuf.undos;
3137                   undobuf.undos = buf->next;
3138                   buf->next = undobuf.frees;
3139                   undobuf.frees = buf;
3140                 }
3141             }
3142
3143           i2scratch = m_split != 0;
3144         }
3145
3146       /* If recog_for_combine has discarded clobbers, try to use them
3147          again for the split.  */
3148       if (m_split == 0 && newpat_vec_with_clobbers)
3149         {
3150           parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3151           m_split = combine_split_insns (parallel, i3);
3152         }
3153
3154       if (m_split && NEXT_INSN (m_split) == NULL_RTX)
3155         {
3156           m_split = PATTERN (m_split);
3157           insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
3158           if (insn_code_number >= 0)
3159             newpat = m_split;
3160         }
3161       else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
3162                && (next_real_insn (i2) == i3
3163                    || ! use_crosses_set_p (PATTERN (m_split), DF_INSN_LUID (i2))))
3164         {
3165           rtx i2set, i3set;
3166           rtx newi3pat = PATTERN (NEXT_INSN (m_split));
3167           newi2pat = PATTERN (m_split);
3168
3169           i3set = single_set (NEXT_INSN (m_split));
3170           i2set = single_set (m_split);
3171
3172           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3173
3174           /* If I2 or I3 has multiple SETs, we won't know how to track
3175              register status, so don't use these insns.  If I2's destination
3176              is used between I2 and I3, we also can't use these insns.  */
3177
3178           if (i2_code_number >= 0 && i2set && i3set
3179               && (next_real_insn (i2) == i3
3180                   || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3181             insn_code_number = recog_for_combine (&newi3pat, i3,
3182                                                   &new_i3_notes);
3183           if (insn_code_number >= 0)
3184             newpat = newi3pat;
3185
3186           /* It is possible that both insns now set the destination of I3.
3187              If so, we must show an extra use of it.  */
3188
3189           if (insn_code_number >= 0)
3190             {
3191               rtx new_i3_dest = SET_DEST (i3set);
3192               rtx new_i2_dest = SET_DEST (i2set);
3193
3194               while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3195                      || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3196                      || GET_CODE (new_i3_dest) == SUBREG)
3197                 new_i3_dest = XEXP (new_i3_dest, 0);
3198
3199               while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3200                      || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3201                      || GET_CODE (new_i2_dest) == SUBREG)
3202                 new_i2_dest = XEXP (new_i2_dest, 0);
3203
3204               if (REG_P (new_i3_dest)
3205                   && REG_P (new_i2_dest)
3206                   && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3207                 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3208             }
3209         }
3210
3211       /* If we can split it and use I2DEST, go ahead and see if that
3212          helps things be recognized.  Verify that none of the registers
3213          are set between I2 and I3.  */
3214       if (insn_code_number < 0
3215           && (split = find_split_point (&newpat, i3, false)) != 0
3216 #ifdef HAVE_cc0
3217           && REG_P (i2dest)
3218 #endif
3219           /* We need I2DEST in the proper mode.  If it is a hard register
3220              or the only use of a pseudo, we can change its mode.
3221              Make sure we don't change a hard register to have a mode that
3222              isn't valid for it, or change the number of registers.  */
3223           && (GET_MODE (*split) == GET_MODE (i2dest)
3224               || GET_MODE (*split) == VOIDmode
3225               || can_change_dest_mode (i2dest, added_sets_2,
3226                                        GET_MODE (*split)))
3227           && (next_real_insn (i2) == i3
3228               || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3229           /* We can't overwrite I2DEST if its value is still used by
3230              NEWPAT.  */
3231           && ! reg_referenced_p (i2dest, newpat))
3232         {
3233           rtx newdest = i2dest;
3234           enum rtx_code split_code = GET_CODE (*split);
3235           enum machine_mode split_mode = GET_MODE (*split);
3236           bool subst_done = false;
3237           newi2pat = NULL_RTX;
3238
3239           i2scratch = true;
3240
3241           /* *SPLIT may be part of I2SRC, so make sure we have the
3242              original expression around for later debug processing.
3243              We should not need I2SRC any more in other cases.  */
3244           if (MAY_HAVE_DEBUG_INSNS)
3245             i2src = copy_rtx (i2src);
3246           else
3247             i2src = NULL;
3248
3249           /* Get NEWDEST as a register in the proper mode.  We have already
3250              validated that we can do this.  */
3251           if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3252             {
3253               if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3254                 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3255               else
3256                 {
3257                   SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3258                   newdest = regno_reg_rtx[REGNO (i2dest)];
3259                 }
3260             }
3261
3262           /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3263              an ASHIFT.  This can occur if it was inside a PLUS and hence
3264              appeared to be a memory address.  This is a kludge.  */
3265           if (split_code == MULT
3266               && CONST_INT_P (XEXP (*split, 1))
3267               && INTVAL (XEXP (*split, 1)) > 0
3268               && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
3269             {
3270               SUBST (*split, gen_rtx_ASHIFT (split_mode,
3271                                              XEXP (*split, 0), GEN_INT (i)));
3272               /* Update split_code because we may not have a multiply
3273                  anymore.  */
3274               split_code = GET_CODE (*split);
3275             }
3276
3277 #ifdef INSN_SCHEDULING
3278           /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3279              be written as a ZERO_EXTEND.  */
3280           if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3281             {
3282 #ifdef LOAD_EXTEND_OP
3283               /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3284                  what it really is.  */
3285               if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3286                   == SIGN_EXTEND)
3287                 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3288                                                     SUBREG_REG (*split)));
3289               else
3290 #endif
3291                 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3292                                                     SUBREG_REG (*split)));
3293             }
3294 #endif
3295
3296           /* Attempt to split binary operators using arithmetic identities.  */
3297           if (BINARY_P (SET_SRC (newpat))
3298               && split_mode == GET_MODE (SET_SRC (newpat))
3299               && ! side_effects_p (SET_SRC (newpat)))
3300             {
3301               rtx setsrc = SET_SRC (newpat);
3302               enum machine_mode mode = GET_MODE (setsrc);
3303               enum rtx_code code = GET_CODE (setsrc);
3304               rtx src_op0 = XEXP (setsrc, 0);
3305               rtx src_op1 = XEXP (setsrc, 1);
3306
3307               /* Split "X = Y op Y" as "Z = Y; X = Z op Z".  */
3308               if (rtx_equal_p (src_op0, src_op1))
3309                 {
3310                   newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3311                   SUBST (XEXP (setsrc, 0), newdest);
3312                   SUBST (XEXP (setsrc, 1), newdest);
3313                   subst_done = true;
3314                 }
3315               /* Split "((P op Q) op R) op S" where op is PLUS or MULT.  */
3316               else if ((code == PLUS || code == MULT)
3317                        && GET_CODE (src_op0) == code
3318                        && GET_CODE (XEXP (src_op0, 0)) == code
3319                        && (INTEGRAL_MODE_P (mode)
3320                            || (FLOAT_MODE_P (mode)
3321                                && flag_unsafe_math_optimizations)))
3322                 {
3323                   rtx p = XEXP (XEXP (src_op0, 0), 0);
3324                   rtx q = XEXP (XEXP (src_op0, 0), 1);
3325                   rtx r = XEXP (src_op0, 1);
3326                   rtx s = src_op1;
3327
3328                   /* Split both "((X op Y) op X) op Y" and
3329                      "((X op Y) op Y) op X" as "T op T" where T is
3330                      "X op Y".  */
3331                   if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3332                        || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3333                     {
3334                       newi2pat = gen_rtx_SET (VOIDmode, newdest,
3335                                               XEXP (src_op0, 0));
3336                       SUBST (XEXP (setsrc, 0), newdest);
3337                       SUBST (XEXP (setsrc, 1), newdest);
3338                       subst_done = true;
3339                     }
3340                   /* Split "((X op X) op Y) op Y)" as "T op T" where
3341                      T is "X op Y".  */
3342                   else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3343                     {
3344                       rtx tmp = simplify_gen_binary (code, mode, p, r);
3345                       newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3346                       SUBST (XEXP (setsrc, 0), newdest);
3347                       SUBST (XEXP (setsrc, 1), newdest);
3348                       subst_done = true;
3349                     }
3350                 }
3351             }
3352
3353           if (!subst_done)
3354             {
3355               newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3356               SUBST (*split, newdest);
3357             }
3358
3359           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3360
3361           /* recog_for_combine might have added CLOBBERs to newi2pat.
3362              Make sure NEWPAT does not depend on the clobbered regs.  */
3363           if (GET_CODE (newi2pat) == PARALLEL)
3364             for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3365               if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3366                 {
3367                   rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3368                   if (reg_overlap_mentioned_p (reg, newpat))
3369                     {
3370                       undo_all ();
3371                       return 0;
3372                     }
3373                 }
3374
3375           /* If the split point was a MULT and we didn't have one before,
3376              don't use one now.  */
3377           if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3378             insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3379         }
3380     }
3381
3382   /* Check for a case where we loaded from memory in a narrow mode and
3383      then sign extended it, but we need both registers.  In that case,
3384      we have a PARALLEL with both loads from the same memory location.
3385      We can split this into a load from memory followed by a register-register
3386      copy.  This saves at least one insn, more if register allocation can
3387      eliminate the copy.
3388
3389      We cannot do this if the destination of the first assignment is a
3390      condition code register or cc0.  We eliminate this case by making sure
3391      the SET_DEST and SET_SRC have the same mode.
3392
3393      We cannot do this if the destination of the second assignment is
3394      a register that we have already assumed is zero-extended.  Similarly
3395      for a SUBREG of such a register.  */
3396
3397   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3398            && GET_CODE (newpat) == PARALLEL
3399            && XVECLEN (newpat, 0) == 2
3400            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3401            && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3402            && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3403                == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3404            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3405            && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3406                            XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3407            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3408                                    DF_INSN_LUID (i2))
3409            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3410            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3411            && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
3412                  (REG_P (temp)
3413                   && VEC_index (reg_stat_type, reg_stat,
3414                                 REGNO (temp))->nonzero_bits != 0
3415                   && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3416                   && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3417                   && (VEC_index (reg_stat_type, reg_stat,
3418                                  REGNO (temp))->nonzero_bits
3419                       != GET_MODE_MASK (word_mode))))
3420            && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3421                  && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3422                      (REG_P (temp)
3423                       && VEC_index (reg_stat_type, reg_stat,
3424                                     REGNO (temp))->nonzero_bits != 0
3425                       && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3426                       && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3427                       && (VEC_index (reg_stat_type, reg_stat,
3428                                      REGNO (temp))->nonzero_bits
3429                           != GET_MODE_MASK (word_mode)))))
3430            && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3431                                          SET_SRC (XVECEXP (newpat, 0, 1)))
3432            && ! find_reg_note (i3, REG_UNUSED,
3433                                SET_DEST (XVECEXP (newpat, 0, 0))))
3434     {
3435       rtx ni2dest;
3436
3437       newi2pat = XVECEXP (newpat, 0, 0);
3438       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3439       newpat = XVECEXP (newpat, 0, 1);
3440       SUBST (SET_SRC (newpat),
3441              gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3442       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3443
3444       if (i2_code_number >= 0)
3445         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3446
3447       if (insn_code_number >= 0)
3448         swap_i2i3 = 1;
3449     }
3450
3451   /* Similarly, check for a case where we have a PARALLEL of two independent
3452      SETs but we started with three insns.  In this case, we can do the sets
3453      as two separate insns.  This case occurs when some SET allows two
3454      other insns to combine, but the destination of that SET is still live.  */
3455
3456   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3457            && GET_CODE (newpat) == PARALLEL
3458            && XVECLEN (newpat, 0) == 2
3459            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3460            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3461            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3462            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3463            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3464            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3465            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3466                                    DF_INSN_LUID (i2))
3467            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3468                                   XVECEXP (newpat, 0, 0))
3469            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3470                                   XVECEXP (newpat, 0, 1))
3471            && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3472                  && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1))))
3473 #ifdef HAVE_cc0
3474            /* We cannot split the parallel into two sets if both sets
3475               reference cc0.  */
3476            && ! (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0))
3477                  && reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 1)))
3478 #endif
3479            )
3480     {
3481       /* Normally, it doesn't matter which of the two is done first,
3482          but it does if one references cc0.  In that case, it has to
3483          be first.  */
3484 #ifdef HAVE_cc0
3485       if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
3486         {
3487           newi2pat = XVECEXP (newpat, 0, 0);
3488           newpat = XVECEXP (newpat, 0, 1);
3489         }
3490       else
3491 #endif
3492         {
3493           newi2pat = XVECEXP (newpat, 0, 1);
3494           newpat = XVECEXP (newpat, 0, 0);
3495         }
3496
3497       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3498
3499       if (i2_code_number >= 0)
3500         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3501     }
3502
3503   /* If it still isn't recognized, fail and change things back the way they
3504      were.  */
3505   if ((insn_code_number < 0
3506        /* Is the result a reasonable ASM_OPERANDS?  */
3507        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3508     {
3509       undo_all ();
3510       return 0;
3511     }
3512
3513   /* If we had to change another insn, make sure it is valid also.  */
3514   if (undobuf.other_insn)
3515     {
3516       CLEAR_HARD_REG_SET (newpat_used_regs);
3517
3518       other_pat = PATTERN (undobuf.other_insn);
3519       other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3520                                              &new_other_notes);
3521
3522       if (other_code_number < 0 && ! check_asm_operands (other_pat))
3523         {
3524           undo_all ();
3525           return 0;
3526         }
3527     }
3528
3529 #ifdef HAVE_cc0
3530   /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3531      they are adjacent to each other or not.  */
3532   {
3533     rtx p = prev_nonnote_insn (i3);
3534     if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3535         && sets_cc0_p (newi2pat))
3536       {
3537         undo_all ();
3538         return 0;
3539       }
3540   }
3541 #endif
3542
3543   /* Only allow this combination if insn_rtx_costs reports that the
3544      replacement instructions are cheaper than the originals.  */
3545   if (!combine_validate_cost (i1, i2, i3, newpat, newi2pat, other_pat))
3546     {
3547       undo_all ();
3548       return 0;
3549     }
3550
3551   if (MAY_HAVE_DEBUG_INSNS)
3552     {
3553       struct undo *undo;
3554
3555       for (undo = undobuf.undos; undo; undo = undo->next)
3556         if (undo->kind == UNDO_MODE)
3557           {
3558             rtx reg = *undo->where.r;
3559             enum machine_mode new_mode = GET_MODE (reg);
3560             enum machine_mode old_mode = undo->old_contents.m;
3561
3562             /* Temporarily revert mode back.  */
3563             adjust_reg_mode (reg, old_mode);
3564
3565             if (reg == i2dest && i2scratch)
3566               {
3567                 /* If we used i2dest as a scratch register with a
3568                    different mode, substitute it for the original
3569                    i2src while its original mode is temporarily
3570                    restored, and then clear i2scratch so that we don't
3571                    do it again later.  */
3572                 propagate_for_debug (i2, i3, reg, i2src, false);
3573                 i2scratch = false;
3574                 /* Put back the new mode.  */
3575                 adjust_reg_mode (reg, new_mode);
3576               }
3577             else
3578               {
3579                 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
3580                 rtx first, last;
3581
3582                 if (reg == i2dest)
3583                   {
3584                     first = i2;
3585                     last = i3;
3586                   }
3587                 else
3588                   {
3589                     first = i3;
3590                     last = undobuf.other_insn;
3591                     gcc_assert (last);
3592                   }
3593
3594                 /* We're dealing with a reg that changed mode but not
3595                    meaning, so we want to turn it into a subreg for
3596                    the new mode.  However, because of REG sharing and
3597                    because its mode had already changed, we have to do
3598                    it in two steps.  First, replace any debug uses of
3599                    reg, with its original mode temporarily restored,
3600                    with this copy we have created; then, replace the
3601                    copy with the SUBREG of the original shared reg,
3602                    once again changed to the new mode.  */
3603                 propagate_for_debug (first, last, reg, tempreg, false);
3604                 adjust_reg_mode (reg, new_mode);
3605                 propagate_for_debug (first, last, tempreg,
3606                                      lowpart_subreg (old_mode, reg, new_mode),
3607                                      false);
3608               }
3609           }
3610     }
3611
3612   /* If we will be able to accept this, we have made a
3613      change to the destination of I3.  This requires us to
3614      do a few adjustments.  */
3615
3616   if (changed_i3_dest)
3617     {
3618       PATTERN (i3) = newpat;
3619       adjust_for_new_dest (i3);
3620     }
3621
3622   /* We now know that we can do this combination.  Merge the insns and
3623      update the status of registers and LOG_LINKS.  */
3624
3625   if (undobuf.other_insn)
3626     {
3627       rtx note, next;
3628
3629       PATTERN (undobuf.other_insn) = other_pat;
3630
3631       /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
3632          are still valid.  Then add any non-duplicate notes added by
3633          recog_for_combine.  */
3634       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
3635         {
3636           next = XEXP (note, 1);
3637
3638           if (REG_NOTE_KIND (note) == REG_UNUSED
3639               && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
3640             remove_note (undobuf.other_insn, note);
3641         }
3642
3643       distribute_notes (new_other_notes, undobuf.other_insn,
3644                         undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
3645     }
3646
3647   if (swap_i2i3)
3648     {
3649       rtx insn;
3650       rtx link;
3651       rtx ni2dest;
3652
3653       /* I3 now uses what used to be its destination and which is now
3654          I2's destination.  This requires us to do a few adjustments.  */
3655       PATTERN (i3) = newpat;
3656       adjust_for_new_dest (i3);
3657
3658       /* We need a LOG_LINK from I3 to I2.  But we used to have one,
3659          so we still will.
3660
3661          However, some later insn might be using I2's dest and have
3662          a LOG_LINK pointing at I3.  We must remove this link.
3663          The simplest way to remove the link is to point it at I1,
3664          which we know will be a NOTE.  */
3665
3666       /* newi2pat is usually a SET here; however, recog_for_combine might
3667          have added some clobbers.  */
3668       if (GET_CODE (newi2pat) == PARALLEL)
3669         ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
3670       else
3671         ni2dest = SET_DEST (newi2pat);
3672
3673       for (insn = NEXT_INSN (i3);
3674            insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3675                     || insn != BB_HEAD (this_basic_block->next_bb));
3676            insn = NEXT_INSN (insn))
3677         {
3678           if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
3679             {
3680               for (link = LOG_LINKS (insn); link;
3681                    link = XEXP (link, 1))
3682                 if (XEXP (link, 0) == i3)
3683                   XEXP (link, 0) = i1;
3684
3685               break;
3686             }
3687         }
3688     }
3689
3690   {
3691     rtx i3notes, i2notes, i1notes = 0;
3692     rtx i3links, i2links, i1links = 0;
3693     rtx midnotes = 0;
3694     unsigned int regno;
3695     /* Compute which registers we expect to eliminate.  newi2pat may be setting
3696        either i3dest or i2dest, so we must check it.  Also, i1dest may be the
3697        same as i3dest, in which case newi2pat may be setting i1dest.  */
3698     rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
3699                    || i2dest_in_i2src || i2dest_in_i1src
3700                    || !i2dest_killed
3701                    ? 0 : i2dest);
3702     rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
3703                    || (newi2pat && reg_set_p (i1dest, newi2pat))
3704                    || !i1dest_killed
3705                    ? 0 : i1dest);
3706
3707     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
3708        clear them.  */
3709     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
3710     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
3711     if (i1)
3712       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
3713
3714     /* Ensure that we do not have something that should not be shared but
3715        occurs multiple times in the new insns.  Check this by first
3716        resetting all the `used' flags and then copying anything is shared.  */
3717
3718     reset_used_flags (i3notes);
3719     reset_used_flags (i2notes);
3720     reset_used_flags (i1notes);
3721     reset_used_flags (newpat);
3722     reset_used_flags (newi2pat);
3723     if (undobuf.other_insn)
3724       reset_used_flags (PATTERN (undobuf.other_insn));
3725
3726     i3notes = copy_rtx_if_shared (i3notes);
3727     i2notes = copy_rtx_if_shared (i2notes);
3728     i1notes = copy_rtx_if_shared (i1notes);
3729     newpat = copy_rtx_if_shared (newpat);
3730     newi2pat = copy_rtx_if_shared (newi2pat);
3731     if (undobuf.other_insn)
3732       reset_used_flags (PATTERN (undobuf.other_insn));
3733
3734     INSN_CODE (i3) = insn_code_number;
3735     PATTERN (i3) = newpat;
3736
3737     if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
3738       {
3739         rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
3740
3741         reset_used_flags (call_usage);
3742         call_usage = copy_rtx (call_usage);
3743
3744         if (substed_i2)
3745           {
3746             /* I2SRC must still be meaningful at this point.  Some splitting
3747                operations can invalidate I2SRC, but those operations do not
3748                apply to calls.  */
3749             gcc_assert (i2src);
3750             replace_rtx (call_usage, i2dest, i2src);
3751           }
3752
3753         if (substed_i1)
3754           replace_rtx (call_usage, i1dest, i1src);
3755
3756         CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
3757       }
3758
3759     if (undobuf.other_insn)
3760       INSN_CODE (undobuf.other_insn) = other_code_number;
3761
3762     /* We had one special case above where I2 had more than one set and
3763        we replaced a destination of one of those sets with the destination
3764        of I3.  In that case, we have to update LOG_LINKS of insns later
3765        in this basic block.  Note that this (expensive) case is rare.
3766
3767        Also, in this case, we must pretend that all REG_NOTEs for I2
3768        actually came from I3, so that REG_UNUSED notes from I2 will be
3769        properly handled.  */
3770
3771     if (i3_subst_into_i2)
3772       {
3773         for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
3774           if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
3775                || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
3776               && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
3777               && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
3778               && ! find_reg_note (i2, REG_UNUSED,
3779                                   SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
3780             for (temp = NEXT_INSN (i2);
3781                  temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3782                           || BB_HEAD (this_basic_block) != temp);
3783                  temp = NEXT_INSN (temp))
3784               if (temp != i3 && INSN_P (temp))
3785                 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
3786                   if (XEXP (link, 0) == i2)
3787                     XEXP (link, 0) = i3;
3788
3789         if (i3notes)
3790           {
3791             rtx link = i3notes;
3792             while (XEXP (link, 1))
3793               link = XEXP (link, 1);
3794             XEXP (link, 1) = i2notes;
3795           }
3796         else
3797           i3notes = i2notes;
3798         i2notes = 0;
3799       }
3800
3801     LOG_LINKS (i3) = 0;
3802     REG_NOTES (i3) = 0;
3803     LOG_LINKS (i2) = 0;
3804     REG_NOTES (i2) = 0;
3805
3806     if (newi2pat)
3807       {
3808         if (MAY_HAVE_DEBUG_INSNS && i2scratch)
3809           propagate_for_debug (i2, i3, i2dest, i2src, false);
3810         INSN_CODE (i2) = i2_code_number;
3811         PATTERN (i2) = newi2pat;
3812       }
3813     else
3814       {
3815         if (MAY_HAVE_DEBUG_INSNS && i2src)
3816           propagate_for_debug (i2, i3, i2dest, i2src, i3_subst_into_i2);
3817         SET_INSN_DELETED (i2);
3818       }
3819
3820     if (i1)
3821       {
3822         LOG_LINKS (i1) = 0;
3823         REG_NOTES (i1) = 0;
3824         if (MAY_HAVE_DEBUG_INSNS)
3825           propagate_for_debug (i1, i3, i1dest, i1src, false);
3826         SET_INSN_DELETED (i1);
3827       }
3828
3829     /* Get death notes for everything that is now used in either I3 or
3830        I2 and used to die in a previous insn.  If we built two new
3831        patterns, move from I1 to I2 then I2 to I3 so that we get the
3832        proper movement on registers that I2 modifies.  */
3833
3834     if (newi2pat)
3835       {
3836         move_deaths (newi2pat, NULL_RTX, DF_INSN_LUID (i1), i2, &midnotes);
3837         move_deaths (newpat, newi2pat, DF_INSN_LUID (i1), i3, &midnotes);
3838       }
3839     else
3840       move_deaths (newpat, NULL_RTX, i1 ? DF_INSN_LUID (i1) : DF_INSN_LUID (i2),
3841                    i3, &midnotes);
3842
3843     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
3844     if (i3notes)
3845       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
3846                         elim_i2, elim_i1);
3847     if (i2notes)
3848       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
3849                         elim_i2, elim_i1);
3850     if (i1notes)
3851       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
3852                         elim_i2, elim_i1);
3853     if (midnotes)
3854       distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3855                         elim_i2, elim_i1);
3856
3857     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
3858        know these are REG_UNUSED and want them to go to the desired insn,
3859        so we always pass it as i3.  */
3860
3861     if (newi2pat && new_i2_notes)
3862       distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3863
3864     if (new_i3_notes)
3865       distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
3866
3867     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
3868        put a REG_DEAD note for it somewhere.  If NEWI2PAT exists and sets
3869        I3DEST, the death must be somewhere before I2, not I3.  If we passed I3
3870        in that case, it might delete I2.  Similarly for I2 and I1.
3871        Show an additional death due to the REG_DEAD note we make here.  If
3872        we discard it in distribute_notes, we will decrement it again.  */
3873
3874     if (i3dest_killed)
3875       {
3876         if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
3877           distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
3878                                             NULL_RTX),
3879                             NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
3880         else
3881           distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
3882                                             NULL_RTX),
3883                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3884                             elim_i2, elim_i1);
3885       }
3886
3887     if (i2dest_in_i2src)
3888       {
3889         if (newi2pat && reg_set_p (i2dest, newi2pat))
3890           distribute_notes (alloc_reg_note (REG_DEAD, i2dest, NULL_RTX),
3891                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3892         else
3893           distribute_notes (alloc_reg_note (REG_DEAD, i2dest, NULL_RTX),
3894                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3895                             NULL_RTX, NULL_RTX);
3896       }
3897
3898     if (i1dest_in_i1src)
3899       {
3900         if (newi2pat && reg_set_p (i1dest, newi2pat))
3901           distribute_notes (alloc_reg_note (REG_DEAD, i1dest, NULL_RTX),
3902                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3903         else
3904           distribute_notes (alloc_reg_note (REG_DEAD, i1dest, NULL_RTX),
3905                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3906                             NULL_RTX, NULL_RTX);
3907       }
3908
3909     distribute_links (i3links);
3910     distribute_links (i2links);
3911     distribute_links (i1links);
3912
3913     if (REG_P (i2dest))
3914       {
3915         rtx link;
3916         rtx i2_insn = 0, i2_val = 0, set;
3917
3918         /* The insn that used to set this register doesn't exist, and
3919            this life of the register may not exist either.  See if one of
3920            I3's links points to an insn that sets I2DEST.  If it does,
3921            that is now the last known value for I2DEST. If we don't update
3922            this and I2 set the register to a value that depended on its old
3923            contents, we will get confused.  If this insn is used, thing
3924            will be set correctly in combine_instructions.  */
3925
3926         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3927           if ((set = single_set (XEXP (link, 0))) != 0
3928               && rtx_equal_p (i2dest, SET_DEST (set)))
3929             i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
3930
3931         record_value_for_reg (i2dest, i2_insn, i2_val);
3932
3933         /* If the reg formerly set in I2 died only once and that was in I3,
3934            zero its use count so it won't make `reload' do any work.  */
3935         if (! added_sets_2
3936             && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
3937             && ! i2dest_in_i2src)
3938           {
3939             regno = REGNO (i2dest);
3940             INC_REG_N_SETS (regno, -1);
3941           }
3942       }
3943
3944     if (i1 && REG_P (i1dest))
3945       {
3946         rtx link;
3947         rtx i1_insn = 0, i1_val = 0, set;
3948
3949         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3950           if ((set = single_set (XEXP (link, 0))) != 0
3951               && rtx_equal_p (i1dest, SET_DEST (set)))
3952             i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
3953
3954         record_value_for_reg (i1dest, i1_insn, i1_val);
3955
3956         regno = REGNO (i1dest);
3957         if (! added_sets_1 && ! i1dest_in_i1src)
3958           INC_REG_N_SETS (regno, -1);
3959       }
3960
3961     /* Update reg_stat[].nonzero_bits et al for any changes that may have
3962        been made to this insn.  The order of
3963        set_nonzero_bits_and_sign_copies() is important.  Because newi2pat
3964        can affect nonzero_bits of newpat */
3965     if (newi2pat)
3966       note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
3967     note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
3968   }
3969
3970   if (undobuf.other_insn != NULL_RTX)
3971     {
3972       if (dump_file)
3973         {
3974           fprintf (dump_file, "modifying other_insn ");
3975           dump_insn_slim (dump_file, undobuf.other_insn);
3976         }
3977       df_insn_rescan (undobuf.other_insn);
3978     }
3979
3980   if (i1 && !(NOTE_P(i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
3981     {
3982       if (dump_file)
3983         {
3984           fprintf (dump_file, "modifying insn i1 ");
3985           dump_insn_slim (dump_file, i1);
3986         }
3987       df_insn_rescan (i1);
3988     }
3989
3990   if (i2 && !(NOTE_P(i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
3991     {
3992       if (dump_file)
3993         {
3994           fprintf (dump_file, "modifying insn i2 ");
3995           dump_insn_slim (dump_file, i2);
3996         }
3997       df_insn_rescan (i2);
3998     }
3999
4000   if (i3 && !(NOTE_P(i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4001     {
4002       if (dump_file)
4003         {
4004           fprintf (dump_file, "modifying insn i3 ");
4005           dump_insn_slim (dump_file, i3);
4006         }
4007       df_insn_rescan (i3);
4008     }
4009
4010   /* Set new_direct_jump_p if a new return or simple jump instruction
4011      has been created.  Adjust the CFG accordingly.  */
4012
4013   if (returnjump_p (i3) || any_uncondjump_p (i3))
4014     {
4015       *new_direct_jump_p = 1;
4016       mark_jump_label (PATTERN (i3), i3, 0);
4017       update_cfg_for_uncondjump (i3);
4018     }
4019
4020   if (undobuf.other_insn != NULL_RTX
4021       && (returnjump_p (undobuf.other_insn)
4022           || any_uncondjump_p (undobuf.other_insn)))
4023     {
4024       *new_direct_jump_p = 1;
4025       update_cfg_for_uncondjump (undobuf.other_insn);
4026     }
4027
4028   /* A noop might also need cleaning up of CFG, if it comes from the
4029      simplification of a jump.  */
4030   if (GET_CODE (newpat) == SET
4031       && SET_SRC (newpat) == pc_rtx
4032       && SET_DEST (newpat) == pc_rtx)
4033     {
4034       *new_direct_jump_p = 1;
4035       update_cfg_for_uncondjump (i3);
4036     }
4037
4038   combine_successes++;
4039   undo_commit ();
4040
4041   if (added_links_insn
4042       && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4043       && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4044     return added_links_insn;
4045   else
4046     return newi2pat ? i2 : i3;
4047 }
4048 \f
4049 /* Undo all the modifications recorded in undobuf.  */
4050
4051 static void
4052 undo_all (void)
4053 {
4054   struct undo *undo, *next;
4055
4056   for (undo = undobuf.undos; undo; undo = next)
4057     {
4058       next = undo->next;
4059       switch (undo->kind)
4060         {
4061         case UNDO_RTX:
4062           *undo->where.r = undo->old_contents.r;
4063           break;
4064         case UNDO_INT:
4065           *undo->where.i = undo->old_contents.i;
4066           break;
4067         case UNDO_MODE:
4068           adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4069           break;
4070         default:
4071           gcc_unreachable ();
4072         }
4073
4074       undo->next = undobuf.frees;
4075       undobuf.frees = undo;
4076     }
4077
4078   undobuf.undos = 0;
4079 }
4080
4081 /* We've committed to accepting the changes we made.  Move all
4082    of the undos to the free list.  */
4083
4084 static void
4085 undo_commit (void)
4086 {
4087   struct undo *undo, *next;
4088
4089   for (undo = undobuf.undos; undo; undo = next)
4090     {
4091       next = undo->next;
4092       undo->next = undobuf.frees;
4093       undobuf.frees = undo;
4094     }
4095   undobuf.undos = 0;
4096 }
4097 \f
4098 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4099    where we have an arithmetic expression and return that point.  LOC will
4100    be inside INSN.
4101
4102    try_combine will call this function to see if an insn can be split into
4103    two insns.  */
4104
4105 static rtx *
4106 find_split_point (rtx *loc, rtx insn, bool set_src)
4107 {
4108   rtx x = *loc;
4109   enum rtx_code code = GET_CODE (x);
4110   rtx *split;
4111   unsigned HOST_WIDE_INT len = 0;
4112   HOST_WIDE_INT pos = 0;
4113   int unsignedp = 0;
4114   rtx inner = NULL_RTX;
4115
4116   /* First special-case some codes.  */
4117   switch (code)
4118     {
4119     case SUBREG:
4120 #ifdef INSN_SCHEDULING
4121       /* If we are making a paradoxical SUBREG invalid, it becomes a split
4122          point.  */
4123       if (MEM_P (SUBREG_REG (x)))
4124         return loc;
4125 #endif
4126       return find_split_point (&SUBREG_REG (x), insn, false);
4127
4128     case MEM:
4129 #ifdef HAVE_lo_sum
4130       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4131          using LO_SUM and HIGH.  */
4132       if (GET_CODE (XEXP (x, 0)) == CONST
4133           || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4134         {
4135           enum machine_mode address_mode
4136             = targetm.addr_space.address_mode (MEM_ADDR_SPACE (x));
4137
4138           SUBST (XEXP (x, 0),
4139                  gen_rtx_LO_SUM (address_mode,
4140                                  gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4141                                  XEXP (x, 0)));
4142           return &XEXP (XEXP (x, 0), 0);
4143         }
4144 #endif
4145
4146       /* If we have a PLUS whose second operand is a constant and the
4147          address is not valid, perhaps will can split it up using
4148          the machine-specific way to split large constants.  We use
4149          the first pseudo-reg (one of the virtual regs) as a placeholder;
4150          it will not remain in the result.  */
4151       if (GET_CODE (XEXP (x, 0)) == PLUS
4152           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4153           && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4154                                             MEM_ADDR_SPACE (x)))
4155         {
4156           rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4157           rtx seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4158                                                       XEXP (x, 0)),
4159                                          subst_insn);
4160
4161           /* This should have produced two insns, each of which sets our
4162              placeholder.  If the source of the second is a valid address,
4163              we can make put both sources together and make a split point
4164              in the middle.  */
4165
4166           if (seq
4167               && NEXT_INSN (seq) != NULL_RTX
4168               && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4169               && NONJUMP_INSN_P (seq)
4170               && GET_CODE (PATTERN (seq)) == SET
4171               && SET_DEST (PATTERN (seq)) == reg
4172               && ! reg_mentioned_p (reg,
4173                                     SET_SRC (PATTERN (seq)))
4174               && NONJUMP_INSN_P (NEXT_INSN (seq))
4175               && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4176               && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4177               && memory_address_addr_space_p
4178                    (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4179                     MEM_ADDR_SPACE (x)))
4180             {
4181               rtx src1 = SET_SRC (PATTERN (seq));
4182               rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4183
4184               /* Replace the placeholder in SRC2 with SRC1.  If we can
4185                  find where in SRC2 it was placed, that can become our
4186                  split point and we can replace this address with SRC2.
4187                  Just try two obvious places.  */
4188
4189               src2 = replace_rtx (src2, reg, src1);
4190               split = 0;
4191               if (XEXP (src2, 0) == src1)
4192                 split = &XEXP (src2, 0);
4193               else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4194                        && XEXP (XEXP (src2, 0), 0) == src1)
4195                 split = &XEXP (XEXP (src2, 0), 0);
4196
4197               if (split)
4198                 {
4199                   SUBST (XEXP (x, 0), src2);
4200                   return split;
4201                 }
4202             }
4203
4204           /* If that didn't work, perhaps the first operand is complex and
4205              needs to be computed separately, so make a split point there.
4206              This will occur on machines that just support REG + CONST
4207              and have a constant moved through some previous computation.  */
4208
4209           else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4210                    && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4211                          && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4212             return &XEXP (XEXP (x, 0), 0);
4213         }
4214
4215       /* If we have a PLUS whose first operand is complex, try computing it
4216          separately by making a split there.  */
4217       if (GET_CODE (XEXP (x, 0)) == PLUS
4218           && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4219                                             MEM_ADDR_SPACE (x))
4220           && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4221           && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4222                 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4223         return &XEXP (XEXP (x, 0), 0);
4224       break;
4225
4226     case SET:
4227 #ifdef HAVE_cc0
4228       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4229          ZERO_EXTRACT, the most likely reason why this doesn't match is that
4230          we need to put the operand into a register.  So split at that
4231          point.  */
4232
4233       if (SET_DEST (x) == cc0_rtx
4234           && GET_CODE (SET_SRC (x)) != COMPARE
4235           && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4236           && !OBJECT_P (SET_SRC (x))
4237           && ! (GET_CODE (SET_SRC (x)) == SUBREG
4238                 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4239         return &SET_SRC (x);
4240 #endif
4241
4242       /* See if we can split SET_SRC as it stands.  */
4243       split = find_split_point (&SET_SRC (x), insn, true);
4244       if (split && split != &SET_SRC (x))
4245         return split;
4246
4247       /* See if we can split SET_DEST as it stands.  */
4248       split = find_split_point (&SET_DEST (x), insn, false);
4249       if (split && split != &SET_DEST (x))
4250         return split;
4251
4252       /* See if this is a bitfield assignment with everything constant.  If
4253          so, this is an IOR of an AND, so split it into that.  */
4254       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4255           && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
4256               <= HOST_BITS_PER_WIDE_INT)
4257           && CONST_INT_P (XEXP (SET_DEST (x), 1))
4258           && CONST_INT_P (XEXP (SET_DEST (x), 2))
4259           && CONST_INT_P (SET_SRC (x))
4260           && ((INTVAL (XEXP (SET_DEST (x), 1))
4261                + INTVAL (XEXP (SET_DEST (x), 2)))
4262               <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
4263           && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4264         {
4265           HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4266           unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4267           unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4268           rtx dest = XEXP (SET_DEST (x), 0);
4269           enum machine_mode mode = GET_MODE (dest);
4270           unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
4271           rtx or_mask;
4272
4273           if (BITS_BIG_ENDIAN)
4274             pos = GET_MODE_BITSIZE (mode) - len - pos;
4275
4276           or_mask = gen_int_mode (src << pos, mode);
4277           if (src == mask)
4278             SUBST (SET_SRC (x),
4279                    simplify_gen_binary (IOR, mode, dest, or_mask));
4280           else
4281             {
4282               rtx negmask = gen_int_mode (~(mask << pos), mode);
4283               SUBST (SET_SRC (x),
4284                      simplify_gen_binary (IOR, mode,
4285                                           simplify_gen_binary (AND, mode,
4286                                                                dest, negmask),
4287                                           or_mask));
4288             }
4289
4290           SUBST (SET_DEST (x), dest);
4291
4292           split = find_split_point (&SET_SRC (x), insn, true);
4293           if (split && split != &SET_SRC (x))
4294             return split;
4295         }
4296
4297       /* Otherwise, see if this is an operation that we can split into two.
4298          If so, try to split that.  */
4299       code = GET_CODE (SET_SRC (x));
4300
4301       switch (code)
4302         {
4303         case AND:
4304           /* If we are AND'ing with a large constant that is only a single
4305              bit and the result is only being used in a context where we
4306              need to know if it is zero or nonzero, replace it with a bit
4307              extraction.  This will avoid the large constant, which might
4308              have taken more than one insn to make.  If the constant were
4309              not a valid argument to the AND but took only one insn to make,
4310              this is no worse, but if it took more than one insn, it will
4311              be better.  */
4312
4313           if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4314               && REG_P (XEXP (SET_SRC (x), 0))
4315               && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4316               && REG_P (SET_DEST (x))
4317               && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
4318               && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4319               && XEXP (*split, 0) == SET_DEST (x)
4320               && XEXP (*split, 1) == const0_rtx)
4321             {
4322               rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4323                                                 XEXP (SET_SRC (x), 0),
4324                                                 pos, NULL_RTX, 1, 1, 0, 0);
4325               if (extraction != 0)
4326                 {
4327                   SUBST (SET_SRC (x), extraction);
4328                   return find_split_point (loc, insn, false);
4329                 }
4330             }
4331           break;
4332
4333         case NE:
4334           /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4335              is known to be on, this can be converted into a NEG of a shift.  */
4336           if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4337               && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4338               && 1 <= (pos = exact_log2
4339                        (nonzero_bits (XEXP (SET_SRC (x), 0),
4340                                       GET_MODE (XEXP (SET_SRC (x), 0))))))
4341             {
4342               enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4343
4344               SUBST (SET_SRC (x),
4345                      gen_rtx_NEG (mode,
4346                                   gen_rtx_LSHIFTRT (mode,
4347                                                     XEXP (SET_SRC (x), 0),
4348                                                     GEN_INT (pos))));
4349
4350               split = find_split_point (&SET_SRC (x), insn, true);
4351               if (split && split != &SET_SRC (x))
4352                 return split;
4353             }
4354           break;
4355
4356         case SIGN_EXTEND:
4357           inner = XEXP (SET_SRC (x), 0);
4358
4359           /* We can't optimize if either mode is a partial integer
4360              mode as we don't know how many bits are significant
4361              in those modes.  */
4362           if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4363               || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4364             break;
4365
4366           pos = 0;
4367           len = GET_MODE_BITSIZE (GET_MODE (inner));
4368           unsignedp = 0;
4369           break;
4370
4371         case SIGN_EXTRACT:
4372         case ZERO_EXTRACT:
4373           if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4374               && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4375             {
4376               inner = XEXP (SET_SRC (x), 0);
4377               len = INTVAL (XEXP (SET_SRC (x), 1));
4378               pos = INTVAL (XEXP (SET_SRC (x), 2));
4379
4380               if (BITS_BIG_ENDIAN)
4381                 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
4382               unsignedp = (code == ZERO_EXTRACT);
4383             }
4384           break;
4385
4386         default:
4387           break;
4388         }
4389
4390       if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
4391         {
4392           enum machine_mode mode = GET_MODE (SET_SRC (x));
4393
4394           /* For unsigned, we have a choice of a shift followed by an
4395              AND or two shifts.  Use two shifts for field sizes where the
4396              constant might be too large.  We assume here that we can
4397              always at least get 8-bit constants in an AND insn, which is
4398              true for every current RISC.  */
4399
4400           if (unsignedp && len <= 8)
4401             {
4402               SUBST (SET_SRC (x),
4403                      gen_rtx_AND (mode,
4404                                   gen_rtx_LSHIFTRT
4405                                   (mode, gen_lowpart (mode, inner),
4406                                    GEN_INT (pos)),
4407                                   GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
4408
4409               split = find_split_point (&SET_SRC (x), insn, true);
4410               if (split && split != &SET_SRC (x))
4411                 return split;
4412             }
4413           else
4414             {
4415               SUBST (SET_SRC (x),
4416                      gen_rtx_fmt_ee
4417                      (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4418                       gen_rtx_ASHIFT (mode,
4419                                       gen_lowpart (mode, inner),
4420                                       GEN_INT (GET_MODE_BITSIZE (mode)
4421                                                - len - pos)),
4422                       GEN_INT (GET_MODE_BITSIZE (mode) - len)));
4423
4424               split = find_split_point (&SET_SRC (x), insn, true);
4425               if (split && split != &SET_SRC (x))
4426                 return split;
4427             }
4428         }
4429
4430       /* See if this is a simple operation with a constant as the second
4431          operand.  It might be that this constant is out of range and hence
4432          could be used as a split point.  */
4433       if (BINARY_P (SET_SRC (x))
4434           && CONSTANT_P (XEXP (SET_SRC (x), 1))
4435           && (OBJECT_P (XEXP (SET_SRC (x), 0))
4436               || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4437                   && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4438         return &XEXP (SET_SRC (x), 1);
4439
4440       /* Finally, see if this is a simple operation with its first operand
4441          not in a register.  The operation might require this operand in a
4442          register, so return it as a split point.  We can always do this
4443          because if the first operand were another operation, we would have
4444          already found it as a split point.  */
4445       if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4446           && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4447         return &XEXP (SET_SRC (x), 0);
4448
4449       return 0;
4450
4451     case AND:
4452     case IOR:
4453       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4454          it is better to write this as (not (ior A B)) so we can split it.
4455          Similarly for IOR.  */
4456       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4457         {
4458           SUBST (*loc,
4459                  gen_rtx_NOT (GET_MODE (x),
4460                               gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4461                                               GET_MODE (x),
4462                                               XEXP (XEXP (x, 0), 0),
4463                                               XEXP (XEXP (x, 1), 0))));
4464           return find_split_point (loc, insn, set_src);
4465         }
4466
4467       /* Many RISC machines have a large set of logical insns.  If the
4468          second operand is a NOT, put it first so we will try to split the
4469          other operand first.  */
4470       if (GET_CODE (XEXP (x, 1)) == NOT)
4471         {
4472           rtx tem = XEXP (x, 0);
4473           SUBST (XEXP (x, 0), XEXP (x, 1));
4474           SUBST (XEXP (x, 1), tem);
4475         }
4476       break;
4477
4478     case PLUS:
4479     case MINUS:
4480       /* Split at a multiply-accumulate instruction.  However if this is
4481          the SET_SRC, we likely do not have such an instruction and it's
4482          worthless to try this split.  */
4483       if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
4484         return loc;
4485
4486     default:
4487       break;
4488     }
4489
4490   /* Otherwise, select our actions depending on our rtx class.  */
4491   switch (GET_RTX_CLASS (code))
4492     {
4493     case RTX_BITFIELD_OPS:              /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
4494     case RTX_TERNARY:
4495       split = find_split_point (&XEXP (x, 2), insn, false);
4496       if (split)
4497         return split;
4498       /* ... fall through ...  */
4499     case RTX_BIN_ARITH:
4500     case RTX_COMM_ARITH:
4501     case RTX_COMPARE:
4502     case RTX_COMM_COMPARE:
4503       split = find_split_point (&XEXP (x, 1), insn, false);
4504       if (split)
4505         return split;
4506       /* ... fall through ...  */
4507     case RTX_UNARY:
4508       /* Some machines have (and (shift ...) ...) insns.  If X is not
4509          an AND, but XEXP (X, 0) is, use it as our split point.  */
4510       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4511         return &XEXP (x, 0);
4512
4513       split = find_split_point (&XEXP (x, 0), insn, false);
4514       if (split)
4515         return split;
4516       return loc;
4517
4518     default:
4519       /* Otherwise, we don't have a split point.  */
4520       return 0;
4521     }
4522 }
4523 \f
4524 /* Throughout X, replace FROM with TO, and return the result.
4525    The result is TO if X is FROM;
4526    otherwise the result is X, but its contents may have been modified.
4527    If they were modified, a record was made in undobuf so that
4528    undo_all will (among other things) return X to its original state.
4529
4530    If the number of changes necessary is too much to record to undo,
4531    the excess changes are not made, so the result is invalid.
4532    The changes already made can still be undone.
4533    undobuf.num_undo is incremented for such changes, so by testing that
4534    the caller can tell whether the result is valid.
4535
4536    `n_occurrences' is incremented each time FROM is replaced.
4537
4538    IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4539
4540    UNIQUE_COPY is nonzero if each substitution must be unique.  We do this
4541    by copying if `n_occurrences' is nonzero.  */
4542
4543 static rtx
4544 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
4545 {
4546   enum rtx_code code = GET_CODE (x);
4547   enum machine_mode op0_mode = VOIDmode;
4548   const char *fmt;
4549   int len, i;
4550   rtx new_rtx;
4551
4552 /* Two expressions are equal if they are identical copies of a shared
4553    RTX or if they are both registers with the same register number
4554    and mode.  */
4555
4556 #define COMBINE_RTX_EQUAL_P(X,Y)                        \
4557   ((X) == (Y)                                           \
4558    || (REG_P (X) && REG_P (Y)   \
4559        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4560
4561   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
4562     {
4563       n_occurrences++;
4564       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
4565     }
4566
4567   /* If X and FROM are the same register but different modes, they
4568      will not have been seen as equal above.  However, the log links code
4569      will make a LOG_LINKS entry for that case.  If we do nothing, we
4570      will try to rerecognize our original insn and, when it succeeds,
4571      we will delete the feeding insn, which is incorrect.
4572
4573      So force this insn not to match in this (rare) case.  */
4574   if (! in_dest && code == REG && REG_P (from)
4575       && reg_overlap_mentioned_p (x, from))
4576     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
4577
4578   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4579      of which may contain things that can be combined.  */
4580   if (code != MEM && code != LO_SUM && OBJECT_P (x))
4581     return x;
4582
4583   /* It is possible to have a subexpression appear twice in the insn.
4584      Suppose that FROM is a register that appears within TO.
4585      Then, after that subexpression has been scanned once by `subst',
4586      the second time it is scanned, TO may be found.  If we were
4587      to scan TO here, we would find FROM within it and create a
4588      self-referent rtl structure which is completely wrong.  */
4589   if (COMBINE_RTX_EQUAL_P (x, to))
4590     return to;
4591
4592   /* Parallel asm_operands need special attention because all of the
4593      inputs are shared across the arms.  Furthermore, unsharing the
4594      rtl results in recognition failures.  Failure to handle this case
4595      specially can result in circular rtl.
4596
4597      Solve this by doing a normal pass across the first entry of the
4598      parallel, and only processing the SET_DESTs of the subsequent
4599      entries.  Ug.  */
4600
4601   if (code == PARALLEL
4602       && GET_CODE (XVECEXP (x, 0, 0)) == SET
4603       && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
4604     {
4605       new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
4606
4607       /* If this substitution failed, this whole thing fails.  */
4608       if (GET_CODE (new_rtx) == CLOBBER
4609           && XEXP (new_rtx, 0) == const0_rtx)
4610         return new_rtx;
4611
4612       SUBST (XVECEXP (x, 0, 0), new_rtx);
4613
4614       for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
4615         {
4616           rtx dest = SET_DEST (XVECEXP (x, 0, i));
4617
4618           if (!REG_P (dest)
4619               && GET_CODE (dest) != CC0
4620               && GET_CODE (dest) != PC)
4621             {
4622               new_rtx = subst (dest, from, to, 0, unique_copy);
4623
4624               /* If this substitution failed, this whole thing fails.  */
4625               if (GET_CODE (new_rtx) == CLOBBER
4626                   && XEXP (new_rtx, 0) == const0_rtx)
4627                 return new_rtx;
4628
4629               SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
4630             }
4631         }
4632     }
4633   else
4634     {
4635       len = GET_RTX_LENGTH (code);
4636       fmt = GET_RTX_FORMAT (code);
4637
4638       /* We don't need to process a SET_DEST that is a register, CC0,
4639          or PC, so set up to skip this common case.  All other cases
4640          where we want to suppress replacing something inside a
4641          SET_SRC are handled via the IN_DEST operand.  */
4642       if (code == SET
4643           && (REG_P (SET_DEST (x))
4644               || GET_CODE (SET_DEST (x)) == CC0
4645               || GET_CODE (SET_DEST (x)) == PC))
4646         fmt = "ie";
4647
4648       /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
4649          constant.  */
4650       if (fmt[0] == 'e')
4651         op0_mode = GET_MODE (XEXP (x, 0));
4652
4653       for (i = 0; i < len; i++)
4654         {
4655           if (fmt[i] == 'E')
4656             {
4657               int j;
4658               for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4659                 {
4660                   if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
4661                     {
4662                       new_rtx = (unique_copy && n_occurrences
4663                              ? copy_rtx (to) : to);
4664                       n_occurrences++;
4665                     }
4666                   else
4667                     {
4668                       new_rtx = subst (XVECEXP (x, i, j), from, to, 0,
4669                                    unique_copy);
4670
4671                       /* If this substitution failed, this whole thing
4672                          fails.  */
4673                       if (GET_CODE (new_rtx) == CLOBBER
4674                           && XEXP (new_rtx, 0) == const0_rtx)
4675                         return new_rtx;
4676                     }
4677
4678                   SUBST (XVECEXP (x, i, j), new_rtx);
4679                 }
4680             }
4681           else if (fmt[i] == 'e')
4682             {
4683               /* If this is a register being set, ignore it.  */
4684               new_rtx = XEXP (x, i);
4685               if (in_dest
4686                   && i == 0
4687                   && (((code == SUBREG || code == ZERO_EXTRACT)
4688                        && REG_P (new_rtx))
4689                       || code == STRICT_LOW_PART))
4690                 ;
4691
4692               else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
4693                 {
4694                   /* In general, don't install a subreg involving two
4695                      modes not tieable.  It can worsen register
4696                      allocation, and can even make invalid reload
4697                      insns, since the reg inside may need to be copied
4698                      from in the outside mode, and that may be invalid
4699                      if it is an fp reg copied in integer mode.
4700
4701                      We allow two exceptions to this: It is valid if
4702                      it is inside another SUBREG and the mode of that
4703                      SUBREG and the mode of the inside of TO is
4704                      tieable and it is valid if X is a SET that copies
4705                      FROM to CC0.  */
4706
4707                   if (GET_CODE (to) == SUBREG
4708                       && ! MODES_TIEABLE_P (GET_MODE (to),
4709                                             GET_MODE (SUBREG_REG (to)))
4710                       && ! (code == SUBREG
4711                             && MODES_TIEABLE_P (GET_MODE (x),
4712                                                 GET_MODE (SUBREG_REG (to))))
4713 #ifdef HAVE_cc0
4714                       && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
4715 #endif
4716                       )
4717                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
4718
4719 #ifdef CANNOT_CHANGE_MODE_CLASS
4720                   if (code == SUBREG
4721                       && REG_P (to)
4722                       && REGNO (to) < FIRST_PSEUDO_REGISTER
4723                       && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
4724                                                    GET_MODE (to),
4725                                                    GET_MODE (x)))
4726                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
4727 #endif
4728
4729                   new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
4730                   n_occurrences++;
4731                 }
4732               else
4733                 /* If we are in a SET_DEST, suppress most cases unless we
4734                    have gone inside a MEM, in which case we want to
4735                    simplify the address.  We assume here that things that
4736                    are actually part of the destination have their inner
4737                    parts in the first expression.  This is true for SUBREG,
4738                    STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
4739                    things aside from REG and MEM that should appear in a
4740                    SET_DEST.  */
4741                 new_rtx = subst (XEXP (x, i), from, to,
4742                              (((in_dest
4743                                 && (code == SUBREG || code == STRICT_LOW_PART
4744                                     || code == ZERO_EXTRACT))
4745                                || code == SET)
4746                               && i == 0), unique_copy);
4747
4748               /* If we found that we will have to reject this combination,
4749                  indicate that by returning the CLOBBER ourselves, rather than
4750                  an expression containing it.  This will speed things up as
4751                  well as prevent accidents where two CLOBBERs are considered
4752                  to be equal, thus producing an incorrect simplification.  */
4753
4754               if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
4755                 return new_rtx;
4756
4757               if (GET_CODE (x) == SUBREG
4758                   && (CONST_INT_P (new_rtx)
4759                       || GET_CODE (new_rtx) == CONST_DOUBLE))
4760                 {
4761                   enum machine_mode mode = GET_MODE (x);
4762
4763                   x = simplify_subreg (GET_MODE (x), new_rtx,
4764                                        GET_MODE (SUBREG_REG (x)),
4765                                        SUBREG_BYTE (x));
4766                   if (! x)
4767                     x = gen_rtx_CLOBBER (mode, const0_rtx);
4768                 }
4769               else if (CONST_INT_P (new_rtx)
4770                        && GET_CODE (x) == ZERO_EXTEND)
4771                 {
4772                   x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
4773                                                 new_rtx, GET_MODE (XEXP (x, 0)));
4774                   gcc_assert (x);
4775                 }
4776               else
4777                 SUBST (XEXP (x, i), new_rtx);
4778             }
4779         }
4780     }
4781
4782   /* Check if we are loading something from the constant pool via float
4783      extension; in this case we would undo compress_float_constant
4784      optimization and degenerate constant load to an immediate value.  */
4785   if (GET_CODE (x) == FLOAT_EXTEND
4786       && MEM_P (XEXP (x, 0))
4787       && MEM_READONLY_P (XEXP (x, 0)))
4788     {
4789       rtx tmp = avoid_constant_pool_reference (x);
4790       if (x != tmp)
4791         return x;
4792     }
4793
4794   /* Try to simplify X.  If the simplification changed the code, it is likely
4795      that further simplification will help, so loop, but limit the number
4796      of repetitions that will be performed.  */
4797
4798   for (i = 0; i < 4; i++)
4799     {
4800       /* If X is sufficiently simple, don't bother trying to do anything
4801          with it.  */
4802       if (code != CONST_INT && code != REG && code != CLOBBER)
4803         x = combine_simplify_rtx (x, op0_mode, in_dest);
4804
4805       if (GET_CODE (x) == code)
4806         break;
4807
4808       code = GET_CODE (x);
4809
4810       /* We no longer know the original mode of operand 0 since we
4811          have changed the form of X)  */
4812       op0_mode = VOIDmode;
4813     }
4814
4815   return x;
4816 }
4817 \f
4818 /* Simplify X, a piece of RTL.  We just operate on the expression at the
4819    outer level; call `subst' to simplify recursively.  Return the new
4820    expression.
4821
4822    OP0_MODE is the original mode of XEXP (x, 0).  IN_DEST is nonzero
4823    if we are inside a SET_DEST.  */
4824
4825 static rtx
4826 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
4827 {
4828   enum rtx_code code = GET_CODE (x);
4829   enum machine_mode mode = GET_MODE (x);
4830   rtx temp;
4831   int i;
4832
4833   /* If this is a commutative operation, put a constant last and a complex
4834      expression first.  We don't need to do this for comparisons here.  */
4835   if (COMMUTATIVE_ARITH_P (x)
4836       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
4837     {
4838       temp = XEXP (x, 0);
4839       SUBST (XEXP (x, 0), XEXP (x, 1));
4840       SUBST (XEXP (x, 1), temp);
4841     }
4842
4843   /* If this is a simple operation applied to an IF_THEN_ELSE, try
4844      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
4845      things.  Check for cases where both arms are testing the same
4846      condition.
4847
4848      Don't do anything if all operands are very simple.  */
4849
4850   if ((BINARY_P (x)
4851        && ((!OBJECT_P (XEXP (x, 0))
4852             && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4853                   && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
4854            || (!OBJECT_P (XEXP (x, 1))
4855                && ! (GET_CODE (XEXP (x, 1)) == SUBREG
4856                      && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
4857       || (UNARY_P (x)
4858           && (!OBJECT_P (XEXP (x, 0))
4859                && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4860                      && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
4861     {
4862       rtx cond, true_rtx, false_rtx;
4863
4864       cond = if_then_else_cond (x, &true_rtx, &false_rtx);
4865       if (cond != 0
4866           /* If everything is a comparison, what we have is highly unlikely
4867              to be simpler, so don't use it.  */
4868           && ! (COMPARISON_P (x)
4869                 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
4870         {
4871           rtx cop1 = const0_rtx;
4872           enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
4873
4874           if (cond_code == NE && COMPARISON_P (cond))
4875             return x;
4876
4877           /* Simplify the alternative arms; this may collapse the true and
4878              false arms to store-flag values.  Be careful to use copy_rtx
4879              here since true_rtx or false_rtx might share RTL with x as a
4880              result of the if_then_else_cond call above.  */
4881           true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
4882           false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
4883
4884           /* If true_rtx and false_rtx are not general_operands, an if_then_else
4885              is unlikely to be simpler.  */
4886           if (general_operand (true_rtx, VOIDmode)
4887               && general_operand (false_rtx, VOIDmode))
4888             {
4889               enum rtx_code reversed;
4890
4891               /* Restarting if we generate a store-flag expression will cause
4892                  us to loop.  Just drop through in this case.  */
4893
4894               /* If the result values are STORE_FLAG_VALUE and zero, we can
4895                  just make the comparison operation.  */
4896               if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
4897                 x = simplify_gen_relational (cond_code, mode, VOIDmode,
4898                                              cond, cop1);
4899               else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
4900                        && ((reversed = reversed_comparison_code_parts
4901                                         (cond_code, cond, cop1, NULL))
4902                            != UNKNOWN))
4903                 x = simplify_gen_relational (reversed, mode, VOIDmode,
4904                                              cond, cop1);
4905
4906               /* Likewise, we can make the negate of a comparison operation
4907                  if the result values are - STORE_FLAG_VALUE and zero.  */
4908               else if (CONST_INT_P (true_rtx)
4909                        && INTVAL (true_rtx) == - STORE_FLAG_VALUE
4910                        && false_rtx == const0_rtx)
4911                 x = simplify_gen_unary (NEG, mode,
4912                                         simplify_gen_relational (cond_code,
4913                                                                  mode, VOIDmode,
4914                                                                  cond, cop1),
4915                                         mode);
4916               else if (CONST_INT_P (false_rtx)
4917                        && INTVAL (false_rtx) == - STORE_FLAG_VALUE
4918                        && true_rtx == const0_rtx
4919                        && ((reversed = reversed_comparison_code_parts
4920                                         (cond_code, cond, cop1, NULL))
4921                            != UNKNOWN))
4922                 x = simplify_gen_unary (NEG, mode,
4923                                         simplify_gen_relational (reversed,
4924                                                                  mode, VOIDmode,
4925                                                                  cond, cop1),
4926                                         mode);
4927               else
4928                 return gen_rtx_IF_THEN_ELSE (mode,
4929                                              simplify_gen_relational (cond_code,
4930                                                                       mode,
4931                                                                       VOIDmode,
4932                                                                       cond,
4933                                                                       cop1),
4934                                              true_rtx, false_rtx);
4935
4936               code = GET_CODE (x);
4937               op0_mode = VOIDmode;
4938             }
4939         }
4940     }
4941
4942   /* Try to fold this expression in case we have constants that weren't
4943      present before.  */
4944   temp = 0;
4945   switch (GET_RTX_CLASS (code))
4946     {
4947     case RTX_UNARY:
4948       if (op0_mode == VOIDmode)
4949         op0_mode = GET_MODE (XEXP (x, 0));
4950       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
4951       break;
4952     case RTX_COMPARE:
4953     case RTX_COMM_COMPARE:
4954       {
4955         enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
4956         if (cmp_mode == VOIDmode)
4957           {
4958             cmp_mode = GET_MODE (XEXP (x, 1));
4959             if (cmp_mode == VOIDmode)
4960               cmp_mode = op0_mode;
4961           }
4962         temp = simplify_relational_operation (code, mode, cmp_mode,
4963                                               XEXP (x, 0), XEXP (x, 1));
4964       }
4965       break;
4966     case RTX_COMM_ARITH:
4967     case RTX_BIN_ARITH:
4968       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
4969       break;
4970     case RTX_BITFIELD_OPS:
4971     case RTX_TERNARY:
4972       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
4973                                          XEXP (x, 1), XEXP (x, 2));
4974       break;
4975     default:
4976       break;
4977     }
4978
4979   if (temp)
4980     {
4981       x = temp;
4982       code = GET_CODE (temp);
4983       op0_mode = VOIDmode;
4984       mode = GET_MODE (temp);
4985     }
4986
4987   /* First see if we can apply the inverse distributive law.  */
4988   if (code == PLUS || code == MINUS
4989       || code == AND || code == IOR || code == XOR)
4990     {
4991       x = apply_distributive_law (x);
4992       code = GET_CODE (x);
4993       op0_mode = VOIDmode;
4994     }
4995
4996   /* If CODE is an associative operation not otherwise handled, see if we
4997      can associate some operands.  This can win if they are constants or
4998      if they are logically related (i.e. (a & b) & a).  */
4999   if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5000        || code == AND || code == IOR || code == XOR
5001        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5002       && ((INTEGRAL_MODE_P (mode) && code != DIV)
5003           || (flag_associative_math && FLOAT_MODE_P (mode))))
5004     {
5005       if (GET_CODE (XEXP (x, 0)) == code)
5006         {
5007           rtx other = XEXP (XEXP (x, 0), 0);
5008           rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5009           rtx inner_op1 = XEXP (x, 1);
5010           rtx inner;
5011
5012           /* Make sure we pass the constant operand if any as the second
5013              one if this is a commutative operation.  */
5014           if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5015             {
5016               rtx tem = inner_op0;
5017               inner_op0 = inner_op1;
5018               inner_op1 = tem;
5019             }
5020           inner = simplify_binary_operation (code == MINUS ? PLUS
5021                                              : code == DIV ? MULT
5022                                              : code,
5023                                              mode, inner_op0, inner_op1);
5024
5025           /* For commutative operations, try the other pair if that one
5026              didn't simplify.  */
5027           if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5028             {
5029               other = XEXP (XEXP (x, 0), 1);
5030               inner = simplify_binary_operation (code, mode,
5031                                                  XEXP (XEXP (x, 0), 0),
5032                                                  XEXP (x, 1));
5033             }
5034
5035           if (inner)
5036             return simplify_gen_binary (code, mode, other, inner);
5037         }
5038     }
5039
5040   /* A little bit of algebraic simplification here.  */
5041   switch (code)
5042     {
5043     case MEM:
5044       /* Ensure that our address has any ASHIFTs converted to MULT in case
5045          address-recognizing predicates are called later.  */
5046       temp = make_compound_operation (XEXP (x, 0), MEM);
5047       SUBST (XEXP (x, 0), temp);
5048       break;
5049
5050     case SUBREG:
5051       if (op0_mode == VOIDmode)
5052         op0_mode = GET_MODE (SUBREG_REG (x));
5053
5054       /* See if this can be moved to simplify_subreg.  */
5055       if (CONSTANT_P (SUBREG_REG (x))
5056           && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5057              /* Don't call gen_lowpart if the inner mode
5058                 is VOIDmode and we cannot simplify it, as SUBREG without
5059                 inner mode is invalid.  */
5060           && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5061               || gen_lowpart_common (mode, SUBREG_REG (x))))
5062         return gen_lowpart (mode, SUBREG_REG (x));
5063
5064       if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5065         break;
5066       {
5067         rtx temp;
5068         temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5069                                 SUBREG_BYTE (x));
5070         if (temp)
5071           return temp;
5072       }
5073
5074       /* Don't change the mode of the MEM if that would change the meaning
5075          of the address.  */
5076       if (MEM_P (SUBREG_REG (x))
5077           && (MEM_VOLATILE_P (SUBREG_REG (x))
5078               || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
5079         return gen_rtx_CLOBBER (mode, const0_rtx);
5080
5081       /* Note that we cannot do any narrowing for non-constants since
5082          we might have been counting on using the fact that some bits were
5083          zero.  We now do this in the SET.  */
5084
5085       break;
5086
5087     case NEG:
5088       temp = expand_compound_operation (XEXP (x, 0));
5089
5090       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5091          replaced by (lshiftrt X C).  This will convert
5092          (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
5093
5094       if (GET_CODE (temp) == ASHIFTRT
5095           && CONST_INT_P (XEXP (temp, 1))
5096           && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
5097         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5098                                      INTVAL (XEXP (temp, 1)));
5099
5100       /* If X has only a single bit that might be nonzero, say, bit I, convert
5101          (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5102          MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
5103          (sign_extract X 1 Y).  But only do this if TEMP isn't a register
5104          or a SUBREG of one since we'd be making the expression more
5105          complex if it was just a register.  */
5106
5107       if (!REG_P (temp)
5108           && ! (GET_CODE (temp) == SUBREG
5109                 && REG_P (SUBREG_REG (temp)))
5110           && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5111         {
5112           rtx temp1 = simplify_shift_const
5113             (NULL_RTX, ASHIFTRT, mode,
5114              simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5115                                    GET_MODE_BITSIZE (mode) - 1 - i),
5116              GET_MODE_BITSIZE (mode) - 1 - i);
5117
5118           /* If all we did was surround TEMP with the two shifts, we
5119              haven't improved anything, so don't use it.  Otherwise,
5120              we are better off with TEMP1.  */
5121           if (GET_CODE (temp1) != ASHIFTRT
5122               || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5123               || XEXP (XEXP (temp1, 0), 0) != temp)
5124             return temp1;
5125         }
5126       break;
5127
5128     case TRUNCATE:
5129       /* We can't handle truncation to a partial integer mode here
5130          because we don't know the real bitsize of the partial
5131          integer mode.  */
5132       if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5133         break;
5134
5135       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5136         SUBST (XEXP (x, 0),
5137                force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5138                               GET_MODE_MASK (mode), 0));
5139
5140       /* We can truncate a constant value and return it.  */
5141       if (CONST_INT_P (XEXP (x, 0)))
5142         return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5143
5144       /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5145          whose value is a comparison can be replaced with a subreg if
5146          STORE_FLAG_VALUE permits.  */
5147       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5148           && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5149           && (temp = get_last_value (XEXP (x, 0)))
5150           && COMPARISON_P (temp))
5151         return gen_lowpart (mode, XEXP (x, 0));
5152       break;
5153
5154     case CONST:
5155       /* (const (const X)) can become (const X).  Do it this way rather than
5156          returning the inner CONST since CONST can be shared with a
5157          REG_EQUAL note.  */
5158       if (GET_CODE (XEXP (x, 0)) == CONST)
5159         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5160       break;
5161
5162 #ifdef HAVE_lo_sum
5163     case LO_SUM:
5164       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
5165          can add in an offset.  find_split_point will split this address up
5166          again if it doesn't match.  */
5167       if (GET_CODE (XEXP (x, 0)) == HIGH
5168           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5169         return XEXP (x, 1);
5170       break;
5171 #endif
5172
5173     case PLUS:
5174       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5175          when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5176          bit-field and can be replaced by either a sign_extend or a
5177          sign_extract.  The `and' may be a zero_extend and the two
5178          <c>, -<c> constants may be reversed.  */
5179       if (GET_CODE (XEXP (x, 0)) == XOR
5180           && CONST_INT_P (XEXP (x, 1))
5181           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5182           && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5183           && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5184               || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
5185           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5186           && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5187                && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5188                && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5189                    == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
5190               || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5191                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5192                       == (unsigned int) i + 1))))
5193         return simplify_shift_const
5194           (NULL_RTX, ASHIFTRT, mode,
5195            simplify_shift_const (NULL_RTX, ASHIFT, mode,
5196                                  XEXP (XEXP (XEXP (x, 0), 0), 0),
5197                                  GET_MODE_BITSIZE (mode) - (i + 1)),
5198            GET_MODE_BITSIZE (mode) - (i + 1));
5199
5200       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5201          can become (ashiftrt (ashift (xor x 1) C) C) where C is
5202          the bitsize of the mode - 1.  This allows simplification of
5203          "a = (b & 8) == 0;"  */
5204       if (XEXP (x, 1) == constm1_rtx
5205           && !REG_P (XEXP (x, 0))
5206           && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5207                 && REG_P (SUBREG_REG (XEXP (x, 0))))
5208           && nonzero_bits (XEXP (x, 0), mode) == 1)
5209         return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5210            simplify_shift_const (NULL_RTX, ASHIFT, mode,
5211                                  gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5212                                  GET_MODE_BITSIZE (mode) - 1),
5213            GET_MODE_BITSIZE (mode) - 1);
5214
5215       /* If we are adding two things that have no bits in common, convert
5216          the addition into an IOR.  This will often be further simplified,
5217          for example in cases like ((a & 1) + (a & 2)), which can
5218          become a & 3.  */
5219
5220       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5221           && (nonzero_bits (XEXP (x, 0), mode)
5222               & nonzero_bits (XEXP (x, 1), mode)) == 0)
5223         {
5224           /* Try to simplify the expression further.  */
5225           rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5226           temp = combine_simplify_rtx (tor, mode, in_dest);
5227
5228           /* If we could, great.  If not, do not go ahead with the IOR
5229              replacement, since PLUS appears in many special purpose
5230              address arithmetic instructions.  */
5231           if (GET_CODE (temp) != CLOBBER && temp != tor)
5232             return temp;
5233         }
5234       break;
5235
5236     case MINUS:
5237       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5238          (and <foo> (const_int pow2-1))  */
5239       if (GET_CODE (XEXP (x, 1)) == AND
5240           && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5241           && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5242           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5243         return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5244                                        -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5245       break;
5246
5247     case MULT:
5248       /* If we have (mult (plus A B) C), apply the distributive law and then
5249          the inverse distributive law to see if things simplify.  This
5250          occurs mostly in addresses, often when unrolling loops.  */
5251
5252       if (GET_CODE (XEXP (x, 0)) == PLUS)
5253         {
5254           rtx result = distribute_and_simplify_rtx (x, 0);
5255           if (result)
5256             return result;
5257         }
5258
5259       /* Try simplify a*(b/c) as (a*b)/c.  */
5260       if (FLOAT_MODE_P (mode) && flag_associative_math
5261           && GET_CODE (XEXP (x, 0)) == DIV)
5262         {
5263           rtx tem = simplify_binary_operation (MULT, mode,
5264                                                XEXP (XEXP (x, 0), 0),
5265                                                XEXP (x, 1));
5266           if (tem)
5267             return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5268         }
5269       break;
5270
5271     case UDIV:
5272       /* If this is a divide by a power of two, treat it as a shift if
5273          its first operand is a shift.  */
5274       if (CONST_INT_P (XEXP (x, 1))
5275           && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
5276           && (GET_CODE (XEXP (x, 0)) == ASHIFT
5277               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5278               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5279               || GET_CODE (XEXP (x, 0)) == ROTATE
5280               || GET_CODE (XEXP (x, 0)) == ROTATERT))
5281         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5282       break;
5283
5284     case EQ:  case NE:
5285     case GT:  case GTU:  case GE:  case GEU:
5286     case LT:  case LTU:  case LE:  case LEU:
5287     case UNEQ:  case LTGT:
5288     case UNGT:  case UNGE:
5289     case UNLT:  case UNLE:
5290     case UNORDERED: case ORDERED:
5291       /* If the first operand is a condition code, we can't do anything
5292          with it.  */
5293       if (GET_CODE (XEXP (x, 0)) == COMPARE
5294           || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5295               && ! CC0_P (XEXP (x, 0))))
5296         {
5297           rtx op0 = XEXP (x, 0);
5298           rtx op1 = XEXP (x, 1);
5299           enum rtx_code new_code;
5300
5301           if (GET_CODE (op0) == COMPARE)
5302             op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5303
5304           /* Simplify our comparison, if possible.  */
5305           new_code = simplify_comparison (code, &op0, &op1);
5306
5307           /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5308              if only the low-order bit is possibly nonzero in X (such as when
5309              X is a ZERO_EXTRACT of one bit).  Similarly, we can convert EQ to
5310              (xor X 1) or (minus 1 X); we use the former.  Finally, if X is
5311              known to be either 0 or -1, NE becomes a NEG and EQ becomes
5312              (plus X 1).
5313
5314              Remove any ZERO_EXTRACT we made when thinking this was a
5315              comparison.  It may now be simpler to use, e.g., an AND.  If a
5316              ZERO_EXTRACT is indeed appropriate, it will be placed back by
5317              the call to make_compound_operation in the SET case.  */
5318
5319           if (STORE_FLAG_VALUE == 1
5320               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5321               && op1 == const0_rtx
5322               && mode == GET_MODE (op0)
5323               && nonzero_bits (op0, mode) == 1)
5324             return gen_lowpart (mode,
5325                                 expand_compound_operation (op0));
5326
5327           else 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                    && (num_sign_bit_copies (op0, mode)
5332                        == GET_MODE_BITSIZE (mode)))
5333             {
5334               op0 = expand_compound_operation (op0);
5335               return simplify_gen_unary (NEG, mode,
5336                                          gen_lowpart (mode, op0),
5337                                          mode);
5338             }
5339
5340           else if (STORE_FLAG_VALUE == 1
5341                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5342                    && op1 == const0_rtx
5343                    && mode == GET_MODE (op0)
5344                    && nonzero_bits (op0, mode) == 1)
5345             {
5346               op0 = expand_compound_operation (op0);
5347               return simplify_gen_binary (XOR, mode,
5348                                           gen_lowpart (mode, op0),
5349                                           const1_rtx);
5350             }
5351
5352           else if (STORE_FLAG_VALUE == 1
5353                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5354                    && op1 == const0_rtx
5355                    && mode == GET_MODE (op0)
5356                    && (num_sign_bit_copies (op0, mode)
5357                        == GET_MODE_BITSIZE (mode)))
5358             {
5359               op0 = expand_compound_operation (op0);
5360               return plus_constant (gen_lowpart (mode, op0), 1);
5361             }
5362
5363           /* If STORE_FLAG_VALUE is -1, we have cases similar to
5364              those above.  */
5365           if (STORE_FLAG_VALUE == -1
5366               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5367               && op1 == const0_rtx
5368               && (num_sign_bit_copies (op0, mode)
5369                   == GET_MODE_BITSIZE (mode)))
5370             return gen_lowpart (mode,
5371                                 expand_compound_operation (op0));
5372
5373           else if (STORE_FLAG_VALUE == -1
5374                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5375                    && op1 == const0_rtx
5376                    && mode == GET_MODE (op0)
5377                    && nonzero_bits (op0, mode) == 1)
5378             {
5379               op0 = expand_compound_operation (op0);
5380               return simplify_gen_unary (NEG, mode,
5381                                          gen_lowpart (mode, op0),
5382                                          mode);
5383             }
5384
5385           else if (STORE_FLAG_VALUE == -1
5386                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5387                    && op1 == const0_rtx
5388                    && mode == GET_MODE (op0)
5389                    && (num_sign_bit_copies (op0, mode)
5390                        == GET_MODE_BITSIZE (mode)))
5391             {
5392               op0 = expand_compound_operation (op0);
5393               return simplify_gen_unary (NOT, mode,
5394                                          gen_lowpart (mode, op0),
5395                                          mode);
5396             }
5397
5398           /* If X is 0/1, (eq X 0) is X-1.  */
5399           else if (STORE_FLAG_VALUE == -1
5400                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5401                    && op1 == const0_rtx
5402                    && mode == GET_MODE (op0)
5403                    && nonzero_bits (op0, mode) == 1)
5404             {
5405               op0 = expand_compound_operation (op0);
5406               return plus_constant (gen_lowpart (mode, op0), -1);
5407             }
5408
5409           /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5410              one bit that might be nonzero, we can convert (ne x 0) to
5411              (ashift x c) where C puts the bit in the sign bit.  Remove any
5412              AND with STORE_FLAG_VALUE when we are done, since we are only
5413              going to test the sign bit.  */
5414           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5415               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5416               && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5417                   == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5418               && op1 == const0_rtx
5419               && mode == GET_MODE (op0)
5420               && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5421             {
5422               x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5423                                         expand_compound_operation (op0),
5424                                         GET_MODE_BITSIZE (mode) - 1 - i);
5425               if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5426                 return XEXP (x, 0);
5427               else
5428                 return x;
5429             }
5430
5431           /* If the code changed, return a whole new comparison.  */
5432           if (new_code != code)
5433             return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5434
5435           /* Otherwise, keep this operation, but maybe change its operands.
5436              This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
5437           SUBST (XEXP (x, 0), op0);
5438           SUBST (XEXP (x, 1), op1);
5439         }
5440       break;
5441
5442     case IF_THEN_ELSE:
5443       return simplify_if_then_else (x);
5444
5445     case ZERO_EXTRACT:
5446     case SIGN_EXTRACT:
5447     case ZERO_EXTEND:
5448     case SIGN_EXTEND:
5449       /* If we are processing SET_DEST, we are done.  */
5450       if (in_dest)
5451         return x;
5452
5453       return expand_compound_operation (x);
5454
5455     case SET:
5456       return simplify_set (x);
5457
5458     case AND:
5459     case IOR:
5460       return simplify_logical (x);
5461
5462     case ASHIFT:
5463     case LSHIFTRT:
5464     case ASHIFTRT:
5465     case ROTATE:
5466     case ROTATERT:
5467       /* If this is a shift by a constant amount, simplify it.  */
5468       if (CONST_INT_P (XEXP (x, 1)))
5469         return simplify_shift_const (x, code, mode, XEXP (x, 0),
5470                                      INTVAL (XEXP (x, 1)));
5471
5472       else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5473         SUBST (XEXP (x, 1),
5474                force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5475                               ((HOST_WIDE_INT) 1
5476                                << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5477                               - 1,
5478                               0));
5479       break;
5480
5481     default:
5482       break;
5483     }
5484
5485   return x;
5486 }
5487 \f
5488 /* Simplify X, an IF_THEN_ELSE expression.  Return the new expression.  */
5489
5490 static rtx
5491 simplify_if_then_else (rtx x)
5492 {
5493   enum machine_mode mode = GET_MODE (x);
5494   rtx cond = XEXP (x, 0);
5495   rtx true_rtx = XEXP (x, 1);
5496   rtx false_rtx = XEXP (x, 2);
5497   enum rtx_code true_code = GET_CODE (cond);
5498   int comparison_p = COMPARISON_P (cond);
5499   rtx temp;
5500   int i;
5501   enum rtx_code false_code;
5502   rtx reversed;
5503
5504   /* Simplify storing of the truth value.  */
5505   if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5506     return simplify_gen_relational (true_code, mode, VOIDmode,
5507                                     XEXP (cond, 0), XEXP (cond, 1));
5508
5509   /* Also when the truth value has to be reversed.  */
5510   if (comparison_p
5511       && true_rtx == const0_rtx && false_rtx == const_true_rtx
5512       && (reversed = reversed_comparison (cond, mode)))
5513     return reversed;
5514
5515   /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5516      in it is being compared against certain values.  Get the true and false
5517      comparisons and see if that says anything about the value of each arm.  */
5518
5519   if (comparison_p
5520       && ((false_code = reversed_comparison_code (cond, NULL))
5521           != UNKNOWN)
5522       && REG_P (XEXP (cond, 0)))
5523     {
5524       HOST_WIDE_INT nzb;
5525       rtx from = XEXP (cond, 0);
5526       rtx true_val = XEXP (cond, 1);
5527       rtx false_val = true_val;
5528       int swapped = 0;
5529
5530       /* If FALSE_CODE is EQ, swap the codes and arms.  */
5531
5532       if (false_code == EQ)
5533         {
5534           swapped = 1, true_code = EQ, false_code = NE;
5535           temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5536         }
5537
5538       /* If we are comparing against zero and the expression being tested has
5539          only a single bit that might be nonzero, that is its value when it is
5540          not equal to zero.  Similarly if it is known to be -1 or 0.  */
5541
5542       if (true_code == EQ && true_val == const0_rtx
5543           && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5544         {
5545           false_code = EQ;
5546           false_val = GEN_INT (trunc_int_for_mode (nzb, GET_MODE (from)));
5547         }
5548       else if (true_code == EQ && true_val == const0_rtx
5549                && (num_sign_bit_copies (from, GET_MODE (from))
5550                    == GET_MODE_BITSIZE (GET_MODE (from))))
5551         {
5552           false_code = EQ;
5553           false_val = constm1_rtx;
5554         }
5555
5556       /* Now simplify an arm if we know the value of the register in the
5557          branch and it is used in the arm.  Be careful due to the potential
5558          of locally-shared RTL.  */
5559
5560       if (reg_mentioned_p (from, true_rtx))
5561         true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
5562                                       from, true_val),
5563                       pc_rtx, pc_rtx, 0, 0);
5564       if (reg_mentioned_p (from, false_rtx))
5565         false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
5566                                    from, false_val),
5567                        pc_rtx, pc_rtx, 0, 0);
5568
5569       SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
5570       SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
5571
5572       true_rtx = XEXP (x, 1);
5573       false_rtx = XEXP (x, 2);
5574       true_code = GET_CODE (cond);
5575     }
5576
5577   /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
5578      reversed, do so to avoid needing two sets of patterns for
5579      subtract-and-branch insns.  Similarly if we have a constant in the true
5580      arm, the false arm is the same as the first operand of the comparison, or
5581      the false arm is more complicated than the true arm.  */
5582
5583   if (comparison_p
5584       && reversed_comparison_code (cond, NULL) != UNKNOWN
5585       && (true_rtx == pc_rtx
5586           || (CONSTANT_P (true_rtx)
5587               && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
5588           || true_rtx == const0_rtx
5589           || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
5590           || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
5591               && !OBJECT_P (false_rtx))
5592           || reg_mentioned_p (true_rtx, false_rtx)
5593           || rtx_equal_p (false_rtx, XEXP (cond, 0))))
5594     {
5595       true_code = reversed_comparison_code (cond, NULL);
5596       SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
5597       SUBST (XEXP (x, 1), false_rtx);
5598       SUBST (XEXP (x, 2), true_rtx);
5599
5600       temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5601       cond = XEXP (x, 0);
5602
5603       /* It is possible that the conditional has been simplified out.  */
5604       true_code = GET_CODE (cond);
5605       comparison_p = COMPARISON_P (cond);
5606     }
5607
5608   /* If the two arms are identical, we don't need the comparison.  */
5609
5610   if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
5611     return true_rtx;
5612
5613   /* Convert a == b ? b : a to "a".  */
5614   if (true_code == EQ && ! side_effects_p (cond)
5615       && !HONOR_NANS (mode)
5616       && rtx_equal_p (XEXP (cond, 0), false_rtx)
5617       && rtx_equal_p (XEXP (cond, 1), true_rtx))
5618     return false_rtx;
5619   else if (true_code == NE && ! side_effects_p (cond)
5620            && !HONOR_NANS (mode)
5621            && rtx_equal_p (XEXP (cond, 0), true_rtx)
5622            && rtx_equal_p (XEXP (cond, 1), false_rtx))
5623     return true_rtx;
5624
5625   /* Look for cases where we have (abs x) or (neg (abs X)).  */
5626
5627   if (GET_MODE_CLASS (mode) == MODE_INT
5628       && comparison_p
5629       && XEXP (cond, 1) == const0_rtx
5630       && GET_CODE (false_rtx) == NEG
5631       && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
5632       && rtx_equal_p (true_rtx, XEXP (cond, 0))
5633       && ! side_effects_p (true_rtx))
5634     switch (true_code)
5635       {
5636       case GT:
5637       case GE:
5638         return simplify_gen_unary (ABS, mode, true_rtx, mode);
5639       case LT:
5640       case LE:
5641         return
5642           simplify_gen_unary (NEG, mode,
5643                               simplify_gen_unary (ABS, mode, true_rtx, mode),
5644                               mode);
5645       default:
5646         break;
5647       }
5648
5649   /* Look for MIN or MAX.  */
5650
5651   if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
5652       && comparison_p
5653       && rtx_equal_p (XEXP (cond, 0), true_rtx)
5654       && rtx_equal_p (XEXP (cond, 1), false_rtx)
5655       && ! side_effects_p (cond))
5656     switch (true_code)
5657       {
5658       case GE:
5659       case GT:
5660         return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
5661       case LE:
5662       case LT:
5663         return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
5664       case GEU:
5665       case GTU:
5666         return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
5667       case LEU:
5668       case LTU:
5669         return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
5670       default:
5671         break;
5672       }
5673
5674   /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
5675      second operand is zero, this can be done as (OP Z (mult COND C2)) where
5676      C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
5677      SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
5678      We can do this kind of thing in some cases when STORE_FLAG_VALUE is
5679      neither 1 or -1, but it isn't worth checking for.  */
5680
5681   if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5682       && comparison_p
5683       && GET_MODE_CLASS (mode) == MODE_INT
5684       && ! side_effects_p (x))
5685     {
5686       rtx t = make_compound_operation (true_rtx, SET);
5687       rtx f = make_compound_operation (false_rtx, SET);
5688       rtx cond_op0 = XEXP (cond, 0);
5689       rtx cond_op1 = XEXP (cond, 1);
5690       enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
5691       enum machine_mode m = mode;
5692       rtx z = 0, c1 = NULL_RTX;
5693
5694       if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
5695            || GET_CODE (t) == IOR || GET_CODE (t) == XOR
5696            || GET_CODE (t) == ASHIFT
5697            || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
5698           && rtx_equal_p (XEXP (t, 0), f))
5699         c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
5700
5701       /* If an identity-zero op is commutative, check whether there
5702          would be a match if we swapped the operands.  */
5703       else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
5704                 || GET_CODE (t) == XOR)
5705                && rtx_equal_p (XEXP (t, 1), f))
5706         c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
5707       else if (GET_CODE (t) == SIGN_EXTEND
5708                && (GET_CODE (XEXP (t, 0)) == PLUS
5709                    || GET_CODE (XEXP (t, 0)) == MINUS
5710                    || GET_CODE (XEXP (t, 0)) == IOR
5711                    || GET_CODE (XEXP (t, 0)) == XOR
5712                    || GET_CODE (XEXP (t, 0)) == ASHIFT
5713                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5714                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5715                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5716                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5717                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5718                && (num_sign_bit_copies (f, GET_MODE (f))
5719                    > (unsigned int)
5720                      (GET_MODE_BITSIZE (mode)
5721                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
5722         {
5723           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5724           extend_op = SIGN_EXTEND;
5725           m = GET_MODE (XEXP (t, 0));
5726         }
5727       else if (GET_CODE (t) == SIGN_EXTEND
5728                && (GET_CODE (XEXP (t, 0)) == PLUS
5729                    || GET_CODE (XEXP (t, 0)) == IOR
5730                    || GET_CODE (XEXP (t, 0)) == XOR)
5731                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5732                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5733                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5734                && (num_sign_bit_copies (f, GET_MODE (f))
5735                    > (unsigned int)
5736                      (GET_MODE_BITSIZE (mode)
5737                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
5738         {
5739           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5740           extend_op = SIGN_EXTEND;
5741           m = GET_MODE (XEXP (t, 0));
5742         }
5743       else if (GET_CODE (t) == ZERO_EXTEND
5744                && (GET_CODE (XEXP (t, 0)) == PLUS
5745                    || GET_CODE (XEXP (t, 0)) == MINUS
5746                    || GET_CODE (XEXP (t, 0)) == IOR
5747                    || GET_CODE (XEXP (t, 0)) == XOR
5748                    || GET_CODE (XEXP (t, 0)) == ASHIFT
5749                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5750                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5751                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5752                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5753                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5754                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5755                && ((nonzero_bits (f, GET_MODE (f))
5756                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
5757                    == 0))
5758         {
5759           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5760           extend_op = ZERO_EXTEND;
5761           m = GET_MODE (XEXP (t, 0));
5762         }
5763       else if (GET_CODE (t) == ZERO_EXTEND
5764                && (GET_CODE (XEXP (t, 0)) == PLUS
5765                    || GET_CODE (XEXP (t, 0)) == IOR
5766                    || GET_CODE (XEXP (t, 0)) == XOR)
5767                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5768                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5769                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5770                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5771                && ((nonzero_bits (f, GET_MODE (f))
5772                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
5773                    == 0))
5774         {
5775           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5776           extend_op = ZERO_EXTEND;
5777           m = GET_MODE (XEXP (t, 0));
5778         }
5779
5780       if (z)
5781         {
5782           temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
5783                                                  cond_op0, cond_op1),
5784                         pc_rtx, pc_rtx, 0, 0);
5785           temp = simplify_gen_binary (MULT, m, temp,
5786                                       simplify_gen_binary (MULT, m, c1,
5787                                                            const_true_rtx));
5788           temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
5789           temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
5790
5791           if (extend_op != UNKNOWN)
5792             temp = simplify_gen_unary (extend_op, mode, temp, m);
5793
5794           return temp;
5795         }
5796     }
5797
5798   /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
5799      1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
5800      negation of a single bit, we can convert this operation to a shift.  We
5801      can actually do this more generally, but it doesn't seem worth it.  */
5802
5803   if (true_code == NE && XEXP (cond, 1) == const0_rtx
5804       && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
5805       && ((1 == nonzero_bits (XEXP (cond, 0), mode)
5806            && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
5807           || ((num_sign_bit_copies (XEXP (cond, 0), mode)
5808                == GET_MODE_BITSIZE (mode))
5809               && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
5810     return
5811       simplify_shift_const (NULL_RTX, ASHIFT, mode,
5812                             gen_lowpart (mode, XEXP (cond, 0)), i);
5813
5814   /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8.  */
5815   if (true_code == NE && XEXP (cond, 1) == const0_rtx
5816       && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
5817       && GET_MODE (XEXP (cond, 0)) == mode
5818       && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
5819           == nonzero_bits (XEXP (cond, 0), mode)
5820       && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
5821     return XEXP (cond, 0);
5822
5823   return x;
5824 }
5825 \f
5826 /* Simplify X, a SET expression.  Return the new expression.  */
5827
5828 static rtx
5829 simplify_set (rtx x)
5830 {
5831   rtx src = SET_SRC (x);
5832   rtx dest = SET_DEST (x);
5833   enum machine_mode mode
5834     = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
5835   rtx other_insn;
5836   rtx *cc_use;
5837
5838   /* (set (pc) (return)) gets written as (return).  */
5839   if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
5840     return src;
5841
5842   /* Now that we know for sure which bits of SRC we are using, see if we can
5843      simplify the expression for the object knowing that we only need the
5844      low-order bits.  */
5845
5846   if (GET_MODE_CLASS (mode) == MODE_INT
5847       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5848     {
5849       src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, 0);
5850       SUBST (SET_SRC (x), src);
5851     }
5852
5853   /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5854      the comparison result and try to simplify it unless we already have used
5855      undobuf.other_insn.  */
5856   if ((GET_MODE_CLASS (mode) == MODE_CC
5857        || GET_CODE (src) == COMPARE
5858        || CC0_P (dest))
5859       && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5860       && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5861       && COMPARISON_P (*cc_use)
5862       && rtx_equal_p (XEXP (*cc_use, 0), dest))
5863     {
5864       enum rtx_code old_code = GET_CODE (*cc_use);
5865       enum rtx_code new_code;
5866       rtx op0, op1, tmp;
5867       int other_changed = 0;
5868       enum machine_mode compare_mode = GET_MODE (dest);
5869
5870       if (GET_CODE (src) == COMPARE)
5871         op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5872       else
5873         op0 = src, op1 = CONST0_RTX (GET_MODE (src));
5874
5875       tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
5876                                            op0, op1);
5877       if (!tmp)
5878         new_code = old_code;
5879       else if (!CONSTANT_P (tmp))
5880         {
5881           new_code = GET_CODE (tmp);
5882           op0 = XEXP (tmp, 0);
5883           op1 = XEXP (tmp, 1);
5884         }
5885       else
5886         {
5887           rtx pat = PATTERN (other_insn);
5888           undobuf.other_insn = other_insn;
5889           SUBST (*cc_use, tmp);
5890
5891           /* Attempt to simplify CC user.  */
5892           if (GET_CODE (pat) == SET)
5893             {
5894               rtx new_rtx = simplify_rtx (SET_SRC (pat));
5895               if (new_rtx != NULL_RTX)
5896                 SUBST (SET_SRC (pat), new_rtx);
5897             }
5898
5899           /* Convert X into a no-op move.  */
5900           SUBST (SET_DEST (x), pc_rtx);
5901           SUBST (SET_SRC (x), pc_rtx);
5902           return x;
5903         }
5904
5905       /* Simplify our comparison, if possible.  */
5906       new_code = simplify_comparison (new_code, &op0, &op1);
5907
5908 #ifdef SELECT_CC_MODE
5909       /* If this machine has CC modes other than CCmode, check to see if we
5910          need to use a different CC mode here.  */
5911       if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
5912         compare_mode = GET_MODE (op0);
5913       else
5914         compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5915
5916 #ifndef HAVE_cc0
5917       /* If the mode changed, we have to change SET_DEST, the mode in the
5918          compare, and the mode in the place SET_DEST is used.  If SET_DEST is
5919          a hard register, just build new versions with the proper mode.  If it
5920          is a pseudo, we lose unless it is only time we set the pseudo, in
5921          which case we can safely change its mode.  */
5922       if (compare_mode != GET_MODE (dest))
5923         {
5924           if (can_change_dest_mode (dest, 0, compare_mode))
5925             {
5926               unsigned int regno = REGNO (dest);
5927               rtx new_dest;
5928
5929               if (regno < FIRST_PSEUDO_REGISTER)
5930                 new_dest = gen_rtx_REG (compare_mode, regno);
5931               else
5932                 {
5933                   SUBST_MODE (regno_reg_rtx[regno], compare_mode);
5934                   new_dest = regno_reg_rtx[regno];
5935                 }
5936
5937               SUBST (SET_DEST (x), new_dest);
5938               SUBST (XEXP (*cc_use, 0), new_dest);
5939               other_changed = 1;
5940
5941               dest = new_dest;
5942             }
5943         }
5944 #endif  /* cc0 */
5945 #endif  /* SELECT_CC_MODE */
5946
5947       /* If the code changed, we have to build a new comparison in
5948          undobuf.other_insn.  */
5949       if (new_code != old_code)
5950         {
5951           int other_changed_previously = other_changed;
5952           unsigned HOST_WIDE_INT mask;
5953           rtx old_cc_use = *cc_use;
5954
5955           SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5956                                           dest, const0_rtx));
5957           other_changed = 1;
5958
5959           /* If the only change we made was to change an EQ into an NE or
5960              vice versa, OP0 has only one bit that might be nonzero, and OP1
5961              is zero, check if changing the user of the condition code will
5962              produce a valid insn.  If it won't, we can keep the original code
5963              in that insn by surrounding our operation with an XOR.  */
5964
5965           if (((old_code == NE && new_code == EQ)
5966                || (old_code == EQ && new_code == NE))
5967               && ! other_changed_previously && op1 == const0_rtx
5968               && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5969               && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5970             {
5971               rtx pat = PATTERN (other_insn), note = 0;
5972
5973               if ((recog_for_combine (&pat, other_insn, &note) < 0
5974                    && ! check_asm_operands (pat)))
5975                 {
5976                   *cc_use = old_cc_use;
5977                   other_changed = 0;
5978
5979                   op0 = simplify_gen_binary (XOR, GET_MODE (op0),
5980                                              op0, GEN_INT (mask));
5981                 }
5982             }
5983         }
5984
5985       if (other_changed)
5986         undobuf.other_insn = other_insn;
5987
5988       /* Otherwise, if we didn't previously have a COMPARE in the
5989          correct mode, we need one.  */
5990       if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5991         {
5992           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5993           src = SET_SRC (x);
5994         }
5995       else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
5996         {
5997           SUBST (SET_SRC (x), op0);
5998           src = SET_SRC (x);
5999         }
6000       /* Otherwise, update the COMPARE if needed.  */
6001       else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6002         {
6003           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6004           src = SET_SRC (x);
6005         }
6006     }
6007   else
6008     {
6009       /* Get SET_SRC in a form where we have placed back any
6010          compound expressions.  Then do the checks below.  */
6011       src = make_compound_operation (src, SET);
6012       SUBST (SET_SRC (x), src);
6013     }
6014
6015   /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6016      and X being a REG or (subreg (reg)), we may be able to convert this to
6017      (set (subreg:m2 x) (op)).
6018
6019      We can always do this if M1 is narrower than M2 because that means that
6020      we only care about the low bits of the result.
6021
6022      However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6023      perform a narrower operation than requested since the high-order bits will
6024      be undefined.  On machine where it is defined, this transformation is safe
6025      as long as M1 and M2 have the same number of words.  */
6026
6027   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6028       && !OBJECT_P (SUBREG_REG (src))
6029       && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6030            / UNITS_PER_WORD)
6031           == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6032                + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6033 #ifndef WORD_REGISTER_OPERATIONS
6034       && (GET_MODE_SIZE (GET_MODE (src))
6035         < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6036 #endif
6037 #ifdef CANNOT_CHANGE_MODE_CLASS
6038       && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6039             && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6040                                          GET_MODE (SUBREG_REG (src)),
6041                                          GET_MODE (src)))
6042 #endif
6043       && (REG_P (dest)
6044           || (GET_CODE (dest) == SUBREG
6045               && REG_P (SUBREG_REG (dest)))))
6046     {
6047       SUBST (SET_DEST (x),
6048              gen_lowpart (GET_MODE (SUBREG_REG (src)),
6049                                       dest));
6050       SUBST (SET_SRC (x), SUBREG_REG (src));
6051
6052       src = SET_SRC (x), dest = SET_DEST (x);
6053     }
6054
6055 #ifdef HAVE_cc0
6056   /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6057      in SRC.  */
6058   if (dest == cc0_rtx
6059       && GET_CODE (src) == SUBREG
6060       && subreg_lowpart_p (src)
6061       && (GET_MODE_BITSIZE (GET_MODE (src))
6062           < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
6063     {
6064       rtx inner = SUBREG_REG (src);
6065       enum machine_mode inner_mode = GET_MODE (inner);
6066
6067       /* Here we make sure that we don't have a sign bit on.  */
6068       if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
6069           && (nonzero_bits (inner, inner_mode)
6070               < ((unsigned HOST_WIDE_INT) 1
6071                  << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
6072         {
6073           SUBST (SET_SRC (x), inner);
6074           src = SET_SRC (x);
6075         }
6076     }
6077 #endif
6078
6079 #ifdef LOAD_EXTEND_OP
6080   /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6081      would require a paradoxical subreg.  Replace the subreg with a
6082      zero_extend to avoid the reload that would otherwise be required.  */
6083
6084   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6085       && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6086       && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6087       && SUBREG_BYTE (src) == 0
6088       && (GET_MODE_SIZE (GET_MODE (src))
6089           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6090       && MEM_P (SUBREG_REG (src)))
6091     {
6092       SUBST (SET_SRC (x),
6093              gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6094                             GET_MODE (src), SUBREG_REG (src)));
6095
6096       src = SET_SRC (x);
6097     }
6098 #endif
6099
6100   /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6101      are comparing an item known to be 0 or -1 against 0, use a logical
6102      operation instead. Check for one of the arms being an IOR of the other
6103      arm with some value.  We compute three terms to be IOR'ed together.  In
6104      practice, at most two will be nonzero.  Then we do the IOR's.  */
6105
6106   if (GET_CODE (dest) != PC
6107       && GET_CODE (src) == IF_THEN_ELSE
6108       && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6109       && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6110       && XEXP (XEXP (src, 0), 1) == const0_rtx
6111       && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6112 #ifdef HAVE_conditional_move
6113       && ! can_conditionally_move_p (GET_MODE (src))
6114 #endif
6115       && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6116                                GET_MODE (XEXP (XEXP (src, 0), 0)))
6117           == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
6118       && ! side_effects_p (src))
6119     {
6120       rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6121                       ? XEXP (src, 1) : XEXP (src, 2));
6122       rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6123                    ? XEXP (src, 2) : XEXP (src, 1));
6124       rtx term1 = const0_rtx, term2, term3;
6125
6126       if (GET_CODE (true_rtx) == IOR
6127           && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6128         term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6129       else if (GET_CODE (true_rtx) == IOR
6130                && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6131         term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6132       else if (GET_CODE (false_rtx) == IOR
6133                && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6134         term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6135       else if (GET_CODE (false_rtx) == IOR
6136                && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6137         term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6138
6139       term2 = simplify_gen_binary (AND, GET_MODE (src),
6140                                    XEXP (XEXP (src, 0), 0), true_rtx);
6141       term3 = simplify_gen_binary (AND, GET_MODE (src),
6142                                    simplify_gen_unary (NOT, GET_MODE (src),
6143                                                        XEXP (XEXP (src, 0), 0),
6144                                                        GET_MODE (src)),
6145                                    false_rtx);
6146
6147       SUBST (SET_SRC (x),
6148              simplify_gen_binary (IOR, GET_MODE (src),
6149                                   simplify_gen_binary (IOR, GET_MODE (src),
6150                                                        term1, term2),
6151                                   term3));
6152
6153       src = SET_SRC (x);
6154     }
6155
6156   /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6157      whole thing fail.  */
6158   if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6159     return src;
6160   else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6161     return dest;
6162   else
6163     /* Convert this into a field assignment operation, if possible.  */
6164     return make_field_assignment (x);
6165 }
6166 \f
6167 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6168    result.  */
6169
6170 static rtx
6171 simplify_logical (rtx x)
6172 {
6173   enum machine_mode mode = GET_MODE (x);
6174   rtx op0 = XEXP (x, 0);
6175   rtx op1 = XEXP (x, 1);
6176
6177   switch (GET_CODE (x))
6178     {
6179     case AND:
6180       /* We can call simplify_and_const_int only if we don't lose
6181          any (sign) bits when converting INTVAL (op1) to
6182          "unsigned HOST_WIDE_INT".  */
6183       if (CONST_INT_P (op1)
6184           && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6185               || INTVAL (op1) > 0))
6186         {
6187           x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6188           if (GET_CODE (x) != AND)
6189             return x;
6190
6191           op0 = XEXP (x, 0);
6192           op1 = XEXP (x, 1);
6193         }
6194
6195       /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6196          apply the distributive law and then the inverse distributive
6197          law to see if things simplify.  */
6198       if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6199         {
6200           rtx result = distribute_and_simplify_rtx (x, 0);
6201           if (result)
6202             return result;
6203         }
6204       if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6205         {
6206           rtx result = distribute_and_simplify_rtx (x, 1);
6207           if (result)
6208             return result;
6209         }
6210       break;
6211
6212     case IOR:
6213       /* If we have (ior (and A B) C), apply the distributive law and then
6214          the inverse distributive law to see if things simplify.  */
6215
6216       if (GET_CODE (op0) == AND)
6217         {
6218           rtx result = distribute_and_simplify_rtx (x, 0);
6219           if (result)
6220             return result;
6221         }
6222
6223       if (GET_CODE (op1) == AND)
6224         {
6225           rtx result = distribute_and_simplify_rtx (x, 1);
6226           if (result)
6227             return result;
6228         }
6229       break;
6230
6231     default:
6232       gcc_unreachable ();
6233     }
6234
6235   return x;
6236 }
6237 \f
6238 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6239    operations" because they can be replaced with two more basic operations.
6240    ZERO_EXTEND is also considered "compound" because it can be replaced with
6241    an AND operation, which is simpler, though only one operation.
6242
6243    The function expand_compound_operation is called with an rtx expression
6244    and will convert it to the appropriate shifts and AND operations,
6245    simplifying at each stage.
6246
6247    The function make_compound_operation is called to convert an expression
6248    consisting of shifts and ANDs into the equivalent compound expression.
6249    It is the inverse of this function, loosely speaking.  */
6250
6251 static rtx
6252 expand_compound_operation (rtx x)
6253 {
6254   unsigned HOST_WIDE_INT pos = 0, len;
6255   int unsignedp = 0;
6256   unsigned int modewidth;
6257   rtx tem;
6258
6259   switch (GET_CODE (x))
6260     {
6261     case ZERO_EXTEND:
6262       unsignedp = 1;
6263     case SIGN_EXTEND:
6264       /* We can't necessarily use a const_int for a multiword mode;
6265          it depends on implicitly extending the value.
6266          Since we don't know the right way to extend it,
6267          we can't tell whether the implicit way is right.
6268
6269          Even for a mode that is no wider than a const_int,
6270          we can't win, because we need to sign extend one of its bits through
6271          the rest of it, and we don't know which bit.  */
6272       if (CONST_INT_P (XEXP (x, 0)))
6273         return x;
6274
6275       /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6276          (zero_extend:MODE FROM) or (sign_extend:MODE FROM).  It is for any MEM
6277          because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6278          reloaded. If not for that, MEM's would very rarely be safe.
6279
6280          Reject MODEs bigger than a word, because we might not be able
6281          to reference a two-register group starting with an arbitrary register
6282          (and currently gen_lowpart might crash for a SUBREG).  */
6283
6284       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6285         return x;
6286
6287       /* Reject MODEs that aren't scalar integers because turning vector
6288          or complex modes into shifts causes problems.  */
6289
6290       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6291         return x;
6292
6293       len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
6294       /* If the inner object has VOIDmode (the only way this can happen
6295          is if it is an ASM_OPERANDS), we can't do anything since we don't
6296          know how much masking to do.  */
6297       if (len == 0)
6298         return x;
6299
6300       break;
6301
6302     case ZERO_EXTRACT:
6303       unsignedp = 1;
6304
6305       /* ... fall through ...  */
6306
6307     case SIGN_EXTRACT:
6308       /* If the operand is a CLOBBER, just return it.  */
6309       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6310         return XEXP (x, 0);
6311
6312       if (!CONST_INT_P (XEXP (x, 1))
6313           || !CONST_INT_P (XEXP (x, 2))
6314           || GET_MODE (XEXP (x, 0)) == VOIDmode)
6315         return x;
6316
6317       /* Reject MODEs that aren't scalar integers because turning vector
6318          or complex modes into shifts causes problems.  */
6319
6320       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6321         return x;
6322
6323       len = INTVAL (XEXP (x, 1));
6324       pos = INTVAL (XEXP (x, 2));
6325
6326       /* This should stay within the object being extracted, fail otherwise.  */
6327       if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
6328         return x;
6329
6330       if (BITS_BIG_ENDIAN)
6331         pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
6332
6333       break;
6334
6335     default:
6336       return x;
6337     }
6338   /* Convert sign extension to zero extension, if we know that the high
6339      bit is not set, as this is easier to optimize.  It will be converted
6340      back to cheaper alternative in make_extraction.  */
6341   if (GET_CODE (x) == SIGN_EXTEND
6342       && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6343           && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6344                 & ~(((unsigned HOST_WIDE_INT)
6345                       GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6346                      >> 1))
6347                == 0)))
6348     {
6349       rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6350       rtx temp2 = expand_compound_operation (temp);
6351
6352       /* Make sure this is a profitable operation.  */
6353       if (rtx_cost (x, SET, optimize_this_for_speed_p)
6354           > rtx_cost (temp2, SET, optimize_this_for_speed_p))
6355        return temp2;
6356       else if (rtx_cost (x, SET, optimize_this_for_speed_p)
6357                > rtx_cost (temp, SET, optimize_this_for_speed_p))
6358        return temp;
6359       else
6360        return x;
6361     }
6362
6363   /* We can optimize some special cases of ZERO_EXTEND.  */
6364   if (GET_CODE (x) == ZERO_EXTEND)
6365     {
6366       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6367          know that the last value didn't have any inappropriate bits
6368          set.  */
6369       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6370           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6371           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6372           && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6373               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6374         return XEXP (XEXP (x, 0), 0);
6375
6376       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
6377       if (GET_CODE (XEXP (x, 0)) == SUBREG
6378           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6379           && subreg_lowpart_p (XEXP (x, 0))
6380           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6381           && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6382               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6383         return SUBREG_REG (XEXP (x, 0));
6384
6385       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6386          is a comparison and STORE_FLAG_VALUE permits.  This is like
6387          the first case, but it works even when GET_MODE (x) is larger
6388          than HOST_WIDE_INT.  */
6389       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6390           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6391           && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6392           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6393               <= HOST_BITS_PER_WIDE_INT)
6394           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
6395               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6396         return XEXP (XEXP (x, 0), 0);
6397
6398       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
6399       if (GET_CODE (XEXP (x, 0)) == SUBREG
6400           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6401           && subreg_lowpart_p (XEXP (x, 0))
6402           && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6403           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6404               <= HOST_BITS_PER_WIDE_INT)
6405           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
6406               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6407         return SUBREG_REG (XEXP (x, 0));
6408
6409     }
6410
6411   /* If we reach here, we want to return a pair of shifts.  The inner
6412      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
6413      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
6414      logical depending on the value of UNSIGNEDP.
6415
6416      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6417      converted into an AND of a shift.
6418
6419      We must check for the case where the left shift would have a negative
6420      count.  This can happen in a case like (x >> 31) & 255 on machines
6421      that can't shift by a constant.  On those machines, we would first
6422      combine the shift with the AND to produce a variable-position
6423      extraction.  Then the constant of 31 would be substituted in to produce
6424      a such a position.  */
6425
6426   modewidth = GET_MODE_BITSIZE (GET_MODE (x));
6427   if (modewidth + len >= pos)
6428     {
6429       enum machine_mode mode = GET_MODE (x);
6430       tem = gen_lowpart (mode, XEXP (x, 0));
6431       if (!tem || GET_CODE (tem) == CLOBBER)
6432         return x;
6433       tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6434                                   tem, modewidth - pos - len);
6435       tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6436                                   mode, tem, modewidth - len);
6437     }
6438   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6439     tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6440                                   simplify_shift_const (NULL_RTX, LSHIFTRT,
6441                                                         GET_MODE (x),
6442                                                         XEXP (x, 0), pos),
6443                                   ((HOST_WIDE_INT) 1 << len) - 1);
6444   else
6445     /* Any other cases we can't handle.  */
6446     return x;
6447
6448   /* If we couldn't do this for some reason, return the original
6449      expression.  */
6450   if (GET_CODE (tem) == CLOBBER)
6451     return x;
6452
6453   return tem;
6454 }
6455 \f
6456 /* X is a SET which contains an assignment of one object into
6457    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6458    or certain SUBREGS). If possible, convert it into a series of
6459    logical operations.
6460
6461    We half-heartedly support variable positions, but do not at all
6462    support variable lengths.  */
6463
6464 static const_rtx
6465 expand_field_assignment (const_rtx x)
6466 {
6467   rtx inner;
6468   rtx pos;                      /* Always counts from low bit.  */
6469   int len;
6470   rtx mask, cleared, masked;
6471   enum machine_mode compute_mode;
6472
6473   /* Loop until we find something we can't simplify.  */
6474   while (1)
6475     {
6476       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6477           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6478         {
6479           inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6480           len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
6481           pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6482         }
6483       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6484                && CONST_INT_P (XEXP (SET_DEST (x), 1)))
6485         {
6486           inner = XEXP (SET_DEST (x), 0);
6487           len = INTVAL (XEXP (SET_DEST (x), 1));
6488           pos = XEXP (SET_DEST (x), 2);
6489
6490           /* A constant position should stay within the width of INNER.  */
6491           if (CONST_INT_P (pos)
6492               && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
6493             break;
6494
6495           if (BITS_BIG_ENDIAN)
6496             {
6497               if (CONST_INT_P (pos))
6498                 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
6499                                - INTVAL (pos));
6500               else if (GET_CODE (pos) == MINUS
6501                        && CONST_INT_P (XEXP (pos, 1))
6502                        && (INTVAL (XEXP (pos, 1))
6503                            == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
6504                 /* If position is ADJUST - X, new position is X.  */
6505                 pos = XEXP (pos, 0);
6506               else
6507                 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6508                                            GEN_INT (GET_MODE_BITSIZE (
6509                                                     GET_MODE (inner))
6510                                                     - len),
6511                                            pos);
6512             }
6513         }
6514
6515       /* A SUBREG between two modes that occupy the same numbers of words
6516          can be done by moving the SUBREG to the source.  */
6517       else if (GET_CODE (SET_DEST (x)) == SUBREG
6518                /* We need SUBREGs to compute nonzero_bits properly.  */
6519                && nonzero_sign_valid
6520                && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6521                      + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6522                    == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6523                         + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6524         {
6525           x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6526                            gen_lowpart
6527                            (GET_MODE (SUBREG_REG (SET_DEST (x))),
6528                             SET_SRC (x)));
6529           continue;
6530         }
6531       else
6532         break;
6533
6534       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6535         inner = SUBREG_REG (inner);
6536
6537       compute_mode = GET_MODE (inner);
6538
6539       /* Don't attempt bitwise arithmetic on non scalar integer modes.  */
6540       if (! SCALAR_INT_MODE_P (compute_mode))
6541         {
6542           enum machine_mode imode;
6543
6544           /* Don't do anything for vector or complex integral types.  */
6545           if (! FLOAT_MODE_P (compute_mode))
6546             break;
6547
6548           /* Try to find an integral mode to pun with.  */
6549           imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6550           if (imode == BLKmode)
6551             break;
6552
6553           compute_mode = imode;
6554           inner = gen_lowpart (imode, inner);
6555         }
6556
6557       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
6558       if (len >= HOST_BITS_PER_WIDE_INT)
6559         break;
6560
6561       /* Now compute the equivalent expression.  Make a copy of INNER
6562          for the SET_DEST in case it is a MEM into which we will substitute;
6563          we don't want shared RTL in that case.  */
6564       mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
6565       cleared = simplify_gen_binary (AND, compute_mode,
6566                                      simplify_gen_unary (NOT, compute_mode,
6567                                        simplify_gen_binary (ASHIFT,
6568                                                             compute_mode,
6569                                                             mask, pos),
6570                                        compute_mode),
6571                                      inner);
6572       masked = simplify_gen_binary (ASHIFT, compute_mode,
6573                                     simplify_gen_binary (
6574                                       AND, compute_mode,
6575                                       gen_lowpart (compute_mode, SET_SRC (x)),
6576                                       mask),
6577                                     pos);
6578
6579       x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
6580                        simplify_gen_binary (IOR, compute_mode,
6581                                             cleared, masked));
6582     }
6583
6584   return x;
6585 }
6586 \f
6587 /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
6588    it is an RTX that represents a variable starting position; otherwise,
6589    POS is the (constant) starting bit position (counted from the LSB).
6590
6591    UNSIGNEDP is nonzero for an unsigned reference and zero for a
6592    signed reference.
6593
6594    IN_DEST is nonzero if this is a reference in the destination of a
6595    SET.  This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If nonzero,
6596    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6597    be used.
6598
6599    IN_COMPARE is nonzero if we are in a COMPARE.  This means that a
6600    ZERO_EXTRACT should be built even for bits starting at bit 0.
6601
6602    MODE is the desired mode of the result (if IN_DEST == 0).
6603
6604    The result is an RTX for the extraction or NULL_RTX if the target
6605    can't handle it.  */
6606
6607 static rtx
6608 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6609                  rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6610                  int in_dest, int in_compare)
6611 {
6612   /* This mode describes the size of the storage area
6613      to fetch the overall value from.  Within that, we
6614      ignore the POS lowest bits, etc.  */
6615   enum machine_mode is_mode = GET_MODE (inner);
6616   enum machine_mode inner_mode;
6617   enum machine_mode wanted_inner_mode;
6618   enum machine_mode wanted_inner_reg_mode = word_mode;
6619   enum machine_mode pos_mode = word_mode;
6620   enum machine_mode extraction_mode = word_mode;
6621   enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6622   rtx new_rtx = 0;
6623   rtx orig_pos_rtx = pos_rtx;
6624   HOST_WIDE_INT orig_pos;
6625
6626   if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6627     {
6628       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6629          consider just the QI as the memory to extract from.
6630          The subreg adds or removes high bits; its mode is
6631          irrelevant to the meaning of this extraction,
6632          since POS and LEN count from the lsb.  */
6633       if (MEM_P (SUBREG_REG (inner)))
6634         is_mode = GET_MODE (SUBREG_REG (inner));
6635       inner = SUBREG_REG (inner);
6636     }
6637   else if (GET_CODE (inner) == ASHIFT
6638            && CONST_INT_P (XEXP (inner, 1))
6639            && pos_rtx == 0 && pos == 0
6640            && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6641     {
6642       /* We're extracting the least significant bits of an rtx
6643          (ashift X (const_int C)), where LEN > C.  Extract the
6644          least significant (LEN - C) bits of X, giving an rtx
6645          whose mode is MODE, then shift it left C times.  */
6646       new_rtx = make_extraction (mode, XEXP (inner, 0),
6647                              0, 0, len - INTVAL (XEXP (inner, 1)),
6648                              unsignedp, in_dest, in_compare);
6649       if (new_rtx != 0)
6650         return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
6651     }
6652
6653   inner_mode = GET_MODE (inner);
6654
6655   if (pos_rtx && CONST_INT_P (pos_rtx))
6656     pos = INTVAL (pos_rtx), pos_rtx = 0;
6657
6658   /* See if this can be done without an extraction.  We never can if the
6659      width of the field is not the same as that of some integer mode. For
6660      registers, we can only avoid the extraction if the position is at the
6661      low-order bit and this is either not in the destination or we have the
6662      appropriate STRICT_LOW_PART operation available.
6663
6664      For MEM, we can avoid an extract if the field starts on an appropriate
6665      boundary and we can change the mode of the memory reference.  */
6666
6667   if (tmode != BLKmode
6668       && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6669            && !MEM_P (inner)
6670            && (inner_mode == tmode
6671                || !REG_P (inner)
6672                || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
6673                                          GET_MODE_BITSIZE (inner_mode))
6674                || reg_truncated_to_mode (tmode, inner))
6675            && (! in_dest
6676                || (REG_P (inner)
6677                    && have_insn_for (STRICT_LOW_PART, tmode))))
6678           || (MEM_P (inner) && pos_rtx == 0
6679               && (pos
6680                   % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6681                      : BITS_PER_UNIT)) == 0
6682               /* We can't do this if we are widening INNER_MODE (it
6683                  may not be aligned, for one thing).  */
6684               && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6685               && (inner_mode == tmode
6686                   || (! mode_dependent_address_p (XEXP (inner, 0))
6687                       && ! MEM_VOLATILE_P (inner))))))
6688     {
6689       /* If INNER is a MEM, make a new MEM that encompasses just the desired
6690          field.  If the original and current mode are the same, we need not
6691          adjust the offset.  Otherwise, we do if bytes big endian.
6692
6693          If INNER is not a MEM, get a piece consisting of just the field
6694          of interest (in this case POS % BITS_PER_WORD must be 0).  */
6695
6696       if (MEM_P (inner))
6697         {
6698           HOST_WIDE_INT offset;
6699
6700           /* POS counts from lsb, but make OFFSET count in memory order.  */
6701           if (BYTES_BIG_ENDIAN)
6702             offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6703           else
6704             offset = pos / BITS_PER_UNIT;
6705
6706           new_rtx = adjust_address_nv (inner, tmode, offset);
6707         }
6708       else if (REG_P (inner))
6709         {
6710           if (tmode != inner_mode)
6711             {
6712               /* We can't call gen_lowpart in a DEST since we
6713                  always want a SUBREG (see below) and it would sometimes
6714                  return a new hard register.  */
6715               if (pos || in_dest)
6716                 {
6717                   HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6718
6719                   if (WORDS_BIG_ENDIAN
6720                       && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6721                     final_word = ((GET_MODE_SIZE (inner_mode)
6722                                    - GET_MODE_SIZE (tmode))
6723                                   / UNITS_PER_WORD) - final_word;
6724
6725                   final_word *= UNITS_PER_WORD;
6726                   if (BYTES_BIG_ENDIAN &&
6727                       GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6728                     final_word += (GET_MODE_SIZE (inner_mode)
6729                                    - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6730
6731                   /* Avoid creating invalid subregs, for example when
6732                      simplifying (x>>32)&255.  */
6733                   if (!validate_subreg (tmode, inner_mode, inner, final_word))
6734                     return NULL_RTX;
6735
6736                   new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
6737                 }
6738               else
6739                 new_rtx = gen_lowpart (tmode, inner);
6740             }
6741           else
6742             new_rtx = inner;
6743         }
6744       else
6745         new_rtx = force_to_mode (inner, tmode,
6746                              len >= HOST_BITS_PER_WIDE_INT
6747                              ? ~(unsigned HOST_WIDE_INT) 0
6748                              : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6749                              0);
6750
6751       /* If this extraction is going into the destination of a SET,
6752          make a STRICT_LOW_PART unless we made a MEM.  */
6753
6754       if (in_dest)
6755         return (MEM_P (new_rtx) ? new_rtx
6756                 : (GET_CODE (new_rtx) != SUBREG
6757                    ? gen_rtx_CLOBBER (tmode, const0_rtx)
6758                    : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
6759
6760       if (mode == tmode)
6761         return new_rtx;
6762
6763       if (CONST_INT_P (new_rtx)
6764           || GET_CODE (new_rtx) == CONST_DOUBLE)
6765         return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6766                                          mode, new_rtx, tmode);
6767
6768       /* If we know that no extraneous bits are set, and that the high
6769          bit is not set, convert the extraction to the cheaper of
6770          sign and zero extension, that are equivalent in these cases.  */
6771       if (flag_expensive_optimizations
6772           && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6773               && ((nonzero_bits (new_rtx, tmode)
6774                    & ~(((unsigned HOST_WIDE_INT)
6775                         GET_MODE_MASK (tmode))
6776                        >> 1))
6777                   == 0)))
6778         {
6779           rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
6780           rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
6781
6782           /* Prefer ZERO_EXTENSION, since it gives more information to
6783              backends.  */
6784           if (rtx_cost (temp, SET, optimize_this_for_speed_p)
6785               <= rtx_cost (temp1, SET, optimize_this_for_speed_p))
6786             return temp;
6787           return temp1;
6788         }
6789
6790       /* Otherwise, sign- or zero-extend unless we already are in the
6791          proper mode.  */
6792
6793       return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6794                              mode, new_rtx));
6795     }
6796
6797   /* Unless this is a COMPARE or we have a funny memory reference,
6798      don't do anything with zero-extending field extracts starting at
6799      the low-order bit since they are simple AND operations.  */
6800   if (pos_rtx == 0 && pos == 0 && ! in_dest
6801       && ! in_compare && unsignedp)
6802     return 0;
6803
6804   /* Unless INNER is not MEM, reject this if we would be spanning bytes or
6805      if the position is not a constant and the length is not 1.  In all
6806      other cases, we would only be going outside our object in cases when
6807      an original shift would have been undefined.  */
6808   if (MEM_P (inner)
6809       && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6810           || (pos_rtx != 0 && len != 1)))
6811     return 0;
6812
6813   /* Get the mode to use should INNER not be a MEM, the mode for the position,
6814      and the mode for the result.  */
6815   if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6816     {
6817       wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6818       pos_mode = mode_for_extraction (EP_insv, 2);
6819       extraction_mode = mode_for_extraction (EP_insv, 3);
6820     }
6821
6822   if (! in_dest && unsignedp
6823       && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6824     {
6825       wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6826       pos_mode = mode_for_extraction (EP_extzv, 3);
6827       extraction_mode = mode_for_extraction (EP_extzv, 0);
6828     }
6829
6830   if (! in_dest && ! unsignedp
6831       && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6832     {
6833       wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6834       pos_mode = mode_for_extraction (EP_extv, 3);
6835       extraction_mode = mode_for_extraction (EP_extv, 0);
6836     }
6837
6838   /* Never narrow an object, since that might not be safe.  */
6839
6840   if (mode != VOIDmode
6841       && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6842     extraction_mode = mode;
6843
6844   if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6845       && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6846     pos_mode = GET_MODE (pos_rtx);
6847
6848   /* If this is not from memory, the desired mode is the preferred mode
6849      for an extraction pattern's first input operand, or word_mode if there
6850      is none.  */
6851   if (!MEM_P (inner))
6852     wanted_inner_mode = wanted_inner_reg_mode;
6853   else
6854     {
6855       /* Be careful not to go beyond the extracted object and maintain the
6856          natural alignment of the memory.  */
6857       wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
6858       while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
6859              > GET_MODE_BITSIZE (wanted_inner_mode))
6860         {
6861           wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
6862           gcc_assert (wanted_inner_mode != VOIDmode);
6863         }
6864
6865       /* If we have to change the mode of memory and cannot, the desired mode
6866          is EXTRACTION_MODE.  */
6867       if (inner_mode != wanted_inner_mode
6868           && (mode_dependent_address_p (XEXP (inner, 0))
6869               || MEM_VOLATILE_P (inner)
6870               || pos_rtx))
6871         wanted_inner_mode = extraction_mode;
6872     }
6873
6874   orig_pos = pos;
6875
6876   if (BITS_BIG_ENDIAN)
6877     {
6878       /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6879          BITS_BIG_ENDIAN style.  If position is constant, compute new
6880          position.  Otherwise, build subtraction.
6881          Note that POS is relative to the mode of the original argument.
6882          If it's a MEM we need to recompute POS relative to that.
6883          However, if we're extracting from (or inserting into) a register,
6884          we want to recompute POS relative to wanted_inner_mode.  */
6885       int width = (MEM_P (inner)
6886                    ? GET_MODE_BITSIZE (is_mode)
6887                    : GET_MODE_BITSIZE (wanted_inner_mode));
6888
6889       if (pos_rtx == 0)
6890         pos = width - len - pos;
6891       else
6892         pos_rtx
6893           = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6894       /* POS may be less than 0 now, but we check for that below.
6895          Note that it can only be less than 0 if !MEM_P (inner).  */
6896     }
6897
6898   /* If INNER has a wider mode, and this is a constant extraction, try to
6899      make it smaller and adjust the byte to point to the byte containing
6900      the value.  */
6901   if (wanted_inner_mode != VOIDmode
6902       && inner_mode != wanted_inner_mode
6903       && ! pos_rtx
6904       && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6905       && MEM_P (inner)
6906       && ! mode_dependent_address_p (XEXP (inner, 0))
6907       && ! MEM_VOLATILE_P (inner))
6908     {
6909       int offset = 0;
6910
6911       /* The computations below will be correct if the machine is big
6912          endian in both bits and bytes or little endian in bits and bytes.
6913          If it is mixed, we must adjust.  */
6914
6915       /* If bytes are big endian and we had a paradoxical SUBREG, we must
6916          adjust OFFSET to compensate.  */
6917       if (BYTES_BIG_ENDIAN
6918           && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6919         offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6920
6921       /* We can now move to the desired byte.  */
6922       offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
6923                 * GET_MODE_SIZE (wanted_inner_mode);
6924       pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6925
6926       if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6927           && is_mode != wanted_inner_mode)
6928         offset = (GET_MODE_SIZE (is_mode)
6929                   - GET_MODE_SIZE (wanted_inner_mode) - offset);
6930
6931       inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6932     }
6933
6934   /* If INNER is not memory, get it into the proper mode.  If we are changing
6935      its mode, POS must be a constant and smaller than the size of the new
6936      mode.  */
6937   else if (!MEM_P (inner))
6938     {
6939       /* On the LHS, don't create paradoxical subregs implicitely truncating
6940          the register unless TRULY_NOOP_TRUNCATION.  */
6941       if (in_dest
6942           && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (inner)),
6943                                      GET_MODE_BITSIZE (wanted_inner_mode)))
6944         return NULL_RTX;
6945
6946       if (GET_MODE (inner) != wanted_inner_mode
6947           && (pos_rtx != 0
6948               || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6949         return NULL_RTX;
6950
6951       if (orig_pos < 0)
6952         return NULL_RTX;
6953
6954       inner = force_to_mode (inner, wanted_inner_mode,
6955                              pos_rtx
6956                              || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6957                              ? ~(unsigned HOST_WIDE_INT) 0
6958                              : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6959                                 << orig_pos),
6960                              0);
6961     }
6962
6963   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
6964      have to zero extend.  Otherwise, we can just use a SUBREG.  */
6965   if (pos_rtx != 0
6966       && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6967     {
6968       rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6969
6970       /* If we know that no extraneous bits are set, and that the high
6971          bit is not set, convert extraction to cheaper one - either
6972          SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6973          cases.  */
6974       if (flag_expensive_optimizations
6975           && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6976               && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6977                    & ~(((unsigned HOST_WIDE_INT)
6978                         GET_MODE_MASK (GET_MODE (pos_rtx)))
6979                        >> 1))
6980                   == 0)))
6981         {
6982           rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6983
6984           /* Prefer ZERO_EXTENSION, since it gives more information to
6985              backends.  */
6986           if (rtx_cost (temp1, SET, optimize_this_for_speed_p)
6987               < rtx_cost (temp, SET, optimize_this_for_speed_p))
6988             temp = temp1;
6989         }
6990       pos_rtx = temp;
6991     }
6992   else if (pos_rtx != 0
6993            && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6994     pos_rtx = gen_lowpart (pos_mode, pos_rtx);
6995
6996   /* Make POS_RTX unless we already have it and it is correct.  If we don't
6997      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6998      be a CONST_INT.  */
6999   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7000     pos_rtx = orig_pos_rtx;
7001
7002   else if (pos_rtx == 0)
7003     pos_rtx = GEN_INT (pos);
7004
7005   /* Make the required operation.  See if we can use existing rtx.  */
7006   new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7007                          extraction_mode, inner, GEN_INT (len), pos_rtx);
7008   if (! in_dest)
7009     new_rtx = gen_lowpart (mode, new_rtx);
7010
7011   return new_rtx;
7012 }
7013 \f
7014 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7015    with any other operations in X.  Return X without that shift if so.  */
7016
7017 static rtx
7018 extract_left_shift (rtx x, int count)
7019 {
7020   enum rtx_code code = GET_CODE (x);
7021   enum machine_mode mode = GET_MODE (x);
7022   rtx tem;
7023
7024   switch (code)
7025     {
7026     case ASHIFT:
7027       /* This is the shift itself.  If it is wide enough, we will return
7028          either the value being shifted if the shift count is equal to
7029          COUNT or a shift for the difference.  */
7030       if (CONST_INT_P (XEXP (x, 1))
7031           && INTVAL (XEXP (x, 1)) >= count)
7032         return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7033                                      INTVAL (XEXP (x, 1)) - count);
7034       break;
7035
7036     case NEG:  case NOT:
7037       if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7038         return simplify_gen_unary (code, mode, tem, mode);
7039
7040       break;
7041
7042     case PLUS:  case IOR:  case XOR:  case AND:
7043       /* If we can safely shift this constant and we find the inner shift,
7044          make a new operation.  */
7045       if (CONST_INT_P (XEXP (x, 1))
7046           && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
7047           && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7048         return simplify_gen_binary (code, mode, tem,
7049                                     GEN_INT (INTVAL (XEXP (x, 1)) >> count));
7050
7051       break;
7052
7053     default:
7054       break;
7055     }
7056
7057   return 0;
7058 }
7059 \f
7060 /* Look at the expression rooted at X.  Look for expressions
7061    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7062    Form these expressions.
7063
7064    Return the new rtx, usually just X.
7065
7066    Also, for machines like the VAX that don't have logical shift insns,
7067    try to convert logical to arithmetic shift operations in cases where
7068    they are equivalent.  This undoes the canonicalizations to logical
7069    shifts done elsewhere.
7070
7071    We try, as much as possible, to re-use rtl expressions to save memory.
7072
7073    IN_CODE says what kind of expression we are processing.  Normally, it is
7074    SET.  In a memory address (inside a MEM, PLUS or minus, the latter two
7075    being kludges), it is MEM.  When processing the arguments of a comparison
7076    or a COMPARE against zero, it is COMPARE.  */
7077
7078 static rtx
7079 make_compound_operation (rtx x, enum rtx_code in_code)
7080 {
7081   enum rtx_code code = GET_CODE (x);
7082   enum machine_mode mode = GET_MODE (x);
7083   int mode_width = GET_MODE_BITSIZE (mode);
7084   rtx rhs, lhs;
7085   enum rtx_code next_code;
7086   int i, j;
7087   rtx new_rtx = 0;
7088   rtx tem;
7089   const char *fmt;
7090
7091   /* Select the code to be used in recursive calls.  Once we are inside an
7092      address, we stay there.  If we have a comparison, set to COMPARE,
7093      but once inside, go back to our default of SET.  */
7094
7095   next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
7096                : ((code == COMPARE || COMPARISON_P (x))
7097                   && XEXP (x, 1) == const0_rtx) ? COMPARE
7098                : in_code == COMPARE ? SET : in_code);
7099
7100   /* Process depending on the code of this operation.  If NEW is set
7101      nonzero, it will be returned.  */
7102
7103   switch (code)
7104     {
7105     case ASHIFT:
7106       /* Convert shifts by constants into multiplications if inside
7107          an address.  */
7108       if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7109           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7110           && INTVAL (XEXP (x, 1)) >= 0)
7111         {
7112           new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7113           new_rtx = gen_rtx_MULT (mode, new_rtx,
7114                               GEN_INT ((HOST_WIDE_INT) 1
7115                                        << INTVAL (XEXP (x, 1))));
7116         }
7117       break;
7118
7119     case AND:
7120       /* If the second operand is not a constant, we can't do anything
7121          with it.  */
7122       if (!CONST_INT_P (XEXP (x, 1)))
7123         break;
7124
7125       /* If the constant is a power of two minus one and the first operand
7126          is a logical right shift, make an extraction.  */
7127       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7128           && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
7129         {
7130           new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7131           new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7132                                  0, in_code == COMPARE);
7133         }
7134
7135       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
7136       else if (GET_CODE (XEXP (x, 0)) == SUBREG
7137                && subreg_lowpart_p (XEXP (x, 0))
7138                && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7139                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
7140         {
7141           new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7142                                          next_code);
7143           new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7144                                  XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7145                                  0, in_code == COMPARE);
7146         }
7147       /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)).  */
7148       else if ((GET_CODE (XEXP (x, 0)) == XOR
7149                 || GET_CODE (XEXP (x, 0)) == IOR)
7150                && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7151                && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7152                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
7153         {
7154           /* Apply the distributive law, and then try to make extractions.  */
7155           new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7156                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7157                                              XEXP (x, 1)),
7158                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7159                                              XEXP (x, 1)));
7160           new_rtx = make_compound_operation (new_rtx, in_code);
7161         }
7162
7163       /* If we are have (and (rotate X C) M) and C is larger than the number
7164          of bits in M, this is an extraction.  */
7165
7166       else if (GET_CODE (XEXP (x, 0)) == ROTATE
7167                && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7168                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
7169                && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7170         {
7171           new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7172           new_rtx = make_extraction (mode, new_rtx,
7173                                  (GET_MODE_BITSIZE (mode)
7174                                   - INTVAL (XEXP (XEXP (x, 0), 1))),
7175                                  NULL_RTX, i, 1, 0, in_code == COMPARE);
7176         }
7177
7178       /* On machines without logical shifts, if the operand of the AND is
7179          a logical shift and our mask turns off all the propagated sign
7180          bits, we can replace the logical shift with an arithmetic shift.  */
7181       else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7182                && !have_insn_for (LSHIFTRT, mode)
7183                && have_insn_for (ASHIFTRT, mode)
7184                && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7185                && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7186                && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7187                && mode_width <= HOST_BITS_PER_WIDE_INT)
7188         {
7189           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7190
7191           mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7192           if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7193             SUBST (XEXP (x, 0),
7194                    gen_rtx_ASHIFTRT (mode,
7195                                      make_compound_operation
7196                                      (XEXP (XEXP (x, 0), 0), next_code),
7197                                      XEXP (XEXP (x, 0), 1)));
7198         }
7199
7200       /* If the constant is one less than a power of two, this might be
7201          representable by an extraction even if no shift is present.
7202          If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7203          we are in a COMPARE.  */
7204       else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
7205         new_rtx = make_extraction (mode,
7206                                make_compound_operation (XEXP (x, 0),
7207                                                         next_code),
7208                                0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7209
7210       /* If we are in a comparison and this is an AND with a power of two,
7211          convert this into the appropriate bit extract.  */
7212       else if (in_code == COMPARE
7213                && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
7214         new_rtx = make_extraction (mode,
7215                                make_compound_operation (XEXP (x, 0),
7216                                                         next_code),
7217                                i, NULL_RTX, 1, 1, 0, 1);
7218
7219       break;
7220
7221     case LSHIFTRT:
7222       /* If the sign bit is known to be zero, replace this with an
7223          arithmetic shift.  */
7224       if (have_insn_for (ASHIFTRT, mode)
7225           && ! have_insn_for (LSHIFTRT, mode)
7226           && mode_width <= HOST_BITS_PER_WIDE_INT
7227           && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7228         {
7229           new_rtx = gen_rtx_ASHIFTRT (mode,
7230                                   make_compound_operation (XEXP (x, 0),
7231                                                            next_code),
7232                                   XEXP (x, 1));
7233           break;
7234         }
7235
7236       /* ... fall through ...  */
7237
7238     case ASHIFTRT:
7239       lhs = XEXP (x, 0);
7240       rhs = XEXP (x, 1);
7241
7242       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7243          this is a SIGN_EXTRACT.  */
7244       if (CONST_INT_P (rhs)
7245           && GET_CODE (lhs) == ASHIFT
7246           && CONST_INT_P (XEXP (lhs, 1))
7247           && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7248           && INTVAL (rhs) < mode_width)
7249         {
7250           new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7251           new_rtx = make_extraction (mode, new_rtx,
7252                                  INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7253                                  NULL_RTX, mode_width - INTVAL (rhs),
7254                                  code == LSHIFTRT, 0, in_code == COMPARE);
7255           break;
7256         }
7257
7258       /* See if we have operations between an ASHIFTRT and an ASHIFT.
7259          If so, try to merge the shifts into a SIGN_EXTEND.  We could
7260          also do this for some cases of SIGN_EXTRACT, but it doesn't
7261          seem worth the effort; the case checked for occurs on Alpha.  */
7262
7263       if (!OBJECT_P (lhs)
7264           && ! (GET_CODE (lhs) == SUBREG
7265                 && (OBJECT_P (SUBREG_REG (lhs))))
7266           && CONST_INT_P (rhs)
7267           && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7268           && INTVAL (rhs) < mode_width
7269           && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7270         new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7271                                0, NULL_RTX, mode_width - INTVAL (rhs),
7272                                code == LSHIFTRT, 0, in_code == COMPARE);
7273
7274       break;
7275
7276     case SUBREG:
7277       /* Call ourselves recursively on the inner expression.  If we are
7278          narrowing the object and it has a different RTL code from
7279          what it originally did, do this SUBREG as a force_to_mode.  */
7280
7281       tem = make_compound_operation (SUBREG_REG (x), in_code);
7282
7283       {
7284         rtx simplified = simplify_subreg (mode, tem, GET_MODE (SUBREG_REG (x)),
7285                                           SUBREG_BYTE (x));
7286
7287         if (simplified)
7288           tem = simplified;
7289
7290         if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
7291             && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
7292             && subreg_lowpart_p (x))
7293           {
7294             rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
7295                                        0);
7296
7297             /* If we have something other than a SUBREG, we might have
7298                done an expansion, so rerun ourselves.  */
7299             if (GET_CODE (newer) != SUBREG)
7300               newer = make_compound_operation (newer, in_code);
7301
7302             /* force_to_mode can expand compounds.  If it just re-expanded the
7303                compound use gen_lowpart instead to convert to the desired
7304                mode.  */
7305             if (rtx_equal_p (newer, x))
7306               return gen_lowpart (GET_MODE (x), tem);
7307
7308             return newer;
7309           }
7310
7311         if (simplified)
7312           return tem;
7313       }
7314       break;
7315
7316     default:
7317       break;
7318     }
7319
7320   if (new_rtx)
7321     {
7322       x = gen_lowpart (mode, new_rtx);
7323       code = GET_CODE (x);
7324     }
7325
7326   /* Now recursively process each operand of this operation.  */
7327   fmt = GET_RTX_FORMAT (code);
7328   for (i = 0; i < GET_RTX_LENGTH (code); i++)
7329     if (fmt[i] == 'e')
7330       {
7331         new_rtx = make_compound_operation (XEXP (x, i), next_code);
7332         SUBST (XEXP (x, i), new_rtx);
7333       }
7334     else if (fmt[i] == 'E')
7335       for (j = 0; j < XVECLEN (x, i); j++)
7336         {
7337           new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
7338           SUBST (XVECEXP (x, i, j), new_rtx);
7339         }
7340
7341   /* If this is a commutative operation, the changes to the operands
7342      may have made it noncanonical.  */
7343   if (COMMUTATIVE_ARITH_P (x)
7344       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7345     {
7346       tem = XEXP (x, 0);
7347       SUBST (XEXP (x, 0), XEXP (x, 1));
7348       SUBST (XEXP (x, 1), tem);
7349     }
7350
7351   return x;
7352 }
7353 \f
7354 /* Given M see if it is a value that would select a field of bits
7355    within an item, but not the entire word.  Return -1 if not.
7356    Otherwise, return the starting position of the field, where 0 is the
7357    low-order bit.
7358
7359    *PLEN is set to the length of the field.  */
7360
7361 static int
7362 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7363 {
7364   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
7365   int pos = exact_log2 (m & -m);
7366   int len = 0;
7367
7368   if (pos >= 0)
7369     /* Now shift off the low-order zero bits and see if we have a
7370        power of two minus 1.  */
7371     len = exact_log2 ((m >> pos) + 1);
7372
7373   if (len <= 0)
7374     pos = -1;
7375
7376   *plen = len;
7377   return pos;
7378 }
7379 \f
7380 /* If X refers to a register that equals REG in value, replace these
7381    references with REG.  */
7382 static rtx
7383 canon_reg_for_combine (rtx x, rtx reg)
7384 {
7385   rtx op0, op1, op2;
7386   const char *fmt;
7387   int i;
7388   bool copied;
7389
7390   enum rtx_code code = GET_CODE (x);
7391   switch (GET_RTX_CLASS (code))
7392     {
7393     case RTX_UNARY:
7394       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7395       if (op0 != XEXP (x, 0))
7396         return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7397                                    GET_MODE (reg));
7398       break;
7399
7400     case RTX_BIN_ARITH:
7401     case RTX_COMM_ARITH:
7402       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7403       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7404       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7405         return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7406       break;
7407
7408     case RTX_COMPARE:
7409     case RTX_COMM_COMPARE:
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_relational (GET_CODE (x), GET_MODE (x),
7414                                         GET_MODE (op0), op0, op1);
7415       break;
7416
7417     case RTX_TERNARY:
7418     case RTX_BITFIELD_OPS:
7419       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7420       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7421       op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7422       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7423         return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7424                                      GET_MODE (op0), op0, op1, op2);
7425
7426     case RTX_OBJ:
7427       if (REG_P (x))
7428         {
7429           if (rtx_equal_p (get_last_value (reg), x)
7430               || rtx_equal_p (reg, get_last_value (x)))
7431             return reg;
7432           else
7433             break;
7434         }
7435
7436       /* fall through */
7437
7438     default:
7439       fmt = GET_RTX_FORMAT (code);
7440       copied = false;
7441       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7442         if (fmt[i] == 'e')
7443           {
7444             rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7445             if (op != XEXP (x, i))
7446               {
7447                 if (!copied)
7448                   {
7449                     copied = true;
7450                     x = copy_rtx (x);
7451                   }
7452                 XEXP (x, i) = op;
7453               }
7454           }
7455         else if (fmt[i] == 'E')
7456           {
7457             int j;
7458             for (j = 0; j < XVECLEN (x, i); j++)
7459               {
7460                 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
7461                 if (op != XVECEXP (x, i, j))
7462                   {
7463                     if (!copied)
7464                       {
7465                         copied = true;
7466                         x = copy_rtx (x);
7467                       }
7468                     XVECEXP (x, i, j) = op;
7469                   }
7470               }
7471           }
7472
7473       break;
7474     }
7475
7476   return x;
7477 }
7478
7479 /* Return X converted to MODE.  If the value is already truncated to
7480    MODE we can just return a subreg even though in the general case we
7481    would need an explicit truncation.  */
7482
7483 static rtx
7484 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
7485 {
7486   if (!CONST_INT_P (x)
7487       && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
7488       && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
7489                                  GET_MODE_BITSIZE (GET_MODE (x)))
7490       && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
7491     {
7492       /* Bit-cast X into an integer mode.  */
7493       if (!SCALAR_INT_MODE_P (GET_MODE (x)))
7494         x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
7495       x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
7496                               x, GET_MODE (x));
7497     }
7498
7499   return gen_lowpart (mode, x);
7500 }
7501
7502 /* See if X can be simplified knowing that we will only refer to it in
7503    MODE and will only refer to those bits that are nonzero in MASK.
7504    If other bits are being computed or if masking operations are done
7505    that select a superset of the bits in MASK, they can sometimes be
7506    ignored.
7507
7508    Return a possibly simplified expression, but always convert X to
7509    MODE.  If X is a CONST_INT, AND the CONST_INT with MASK.
7510
7511    If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
7512    are all off in X.  This is used when X will be complemented, by either
7513    NOT, NEG, or XOR.  */
7514
7515 static rtx
7516 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
7517                int just_select)
7518 {
7519   enum rtx_code code = GET_CODE (x);
7520   int next_select = just_select || code == XOR || code == NOT || code == NEG;
7521   enum machine_mode op_mode;
7522   unsigned HOST_WIDE_INT fuller_mask, nonzero;
7523   rtx op0, op1, temp;
7524
7525   /* If this is a CALL or ASM_OPERANDS, don't do anything.  Some of the
7526      code below will do the wrong thing since the mode of such an
7527      expression is VOIDmode.
7528
7529      Also do nothing if X is a CLOBBER; this can happen if X was
7530      the return value from a call to gen_lowpart.  */
7531   if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
7532     return x;
7533
7534   /* We want to perform the operation is its present mode unless we know
7535      that the operation is valid in MODE, in which case we do the operation
7536      in MODE.  */
7537   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
7538               && have_insn_for (code, mode))
7539              ? mode : GET_MODE (x));
7540
7541   /* It is not valid to do a right-shift in a narrower mode
7542      than the one it came in with.  */
7543   if ((code == LSHIFTRT || code == ASHIFTRT)
7544       && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
7545     op_mode = GET_MODE (x);
7546
7547   /* Truncate MASK to fit OP_MODE.  */
7548   if (op_mode)
7549     mask &= GET_MODE_MASK (op_mode);
7550
7551   /* When we have an arithmetic operation, or a shift whose count we
7552      do not know, we need to assume that all bits up to the highest-order
7553      bit in MASK will be needed.  This is how we form such a mask.  */
7554   if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
7555     fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
7556   else
7557     fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
7558                    - 1);
7559
7560   /* Determine what bits of X are guaranteed to be (non)zero.  */
7561   nonzero = nonzero_bits (x, mode);
7562
7563   /* If none of the bits in X are needed, return a zero.  */
7564   if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
7565     x = const0_rtx;
7566
7567   /* If X is a CONST_INT, return a new one.  Do this here since the
7568      test below will fail.  */
7569   if (CONST_INT_P (x))
7570     {
7571       if (SCALAR_INT_MODE_P (mode))
7572         return gen_int_mode (INTVAL (x) & mask, mode);
7573       else
7574         {
7575           x = GEN_INT (INTVAL (x) & mask);
7576           return gen_lowpart_common (mode, x);
7577         }
7578     }
7579
7580   /* If X is narrower than MODE and we want all the bits in X's mode, just
7581      get X in the proper mode.  */
7582   if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
7583       && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
7584     return gen_lowpart (mode, x);
7585
7586   /* We can ignore the effect of a SUBREG if it narrows the mode or
7587      if the constant masks to zero all the bits the mode doesn't have.  */
7588   if (GET_CODE (x) == SUBREG
7589       && subreg_lowpart_p (x)
7590       && ((GET_MODE_SIZE (GET_MODE (x))
7591            < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7592           || (0 == (mask
7593                     & GET_MODE_MASK (GET_MODE (x))
7594                     & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
7595     return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
7596
7597   /* The arithmetic simplifications here only work for scalar integer modes.  */
7598   if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
7599     return gen_lowpart_or_truncate (mode, x);
7600
7601   switch (code)
7602     {
7603     case CLOBBER:
7604       /* If X is a (clobber (const_int)), return it since we know we are
7605          generating something that won't match.  */
7606       return x;
7607
7608     case SIGN_EXTEND:
7609     case ZERO_EXTEND:
7610     case ZERO_EXTRACT:
7611     case SIGN_EXTRACT:
7612       x = expand_compound_operation (x);
7613       if (GET_CODE (x) != code)
7614         return force_to_mode (x, mode, mask, next_select);
7615       break;
7616
7617     case TRUNCATE:
7618       /* Similarly for a truncate.  */
7619       return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7620
7621     case AND:
7622       /* If this is an AND with a constant, convert it into an AND
7623          whose constant is the AND of that constant with MASK.  If it
7624          remains an AND of MASK, delete it since it is redundant.  */
7625
7626       if (CONST_INT_P (XEXP (x, 1)))
7627         {
7628           x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
7629                                       mask & INTVAL (XEXP (x, 1)));
7630
7631           /* If X is still an AND, see if it is an AND with a mask that
7632              is just some low-order bits.  If so, and it is MASK, we don't
7633              need it.  */
7634
7635           if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
7636               && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
7637                   == mask))
7638             x = XEXP (x, 0);
7639
7640           /* If it remains an AND, try making another AND with the bits
7641              in the mode mask that aren't in MASK turned on.  If the
7642              constant in the AND is wide enough, this might make a
7643              cheaper constant.  */
7644
7645           if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
7646               && GET_MODE_MASK (GET_MODE (x)) != mask
7647               && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
7648             {
7649               HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
7650                                     | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
7651               int width = GET_MODE_BITSIZE (GET_MODE (x));
7652               rtx y;
7653
7654               /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
7655                  number, sign extend it.  */
7656               if (width > 0 && width < HOST_BITS_PER_WIDE_INT
7657                   && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7658                 cval |= (HOST_WIDE_INT) -1 << width;
7659
7660               y = simplify_gen_binary (AND, GET_MODE (x),
7661                                        XEXP (x, 0), GEN_INT (cval));
7662               if (rtx_cost (y, SET, optimize_this_for_speed_p)
7663                   < rtx_cost (x, SET, optimize_this_for_speed_p))
7664                 x = y;
7665             }
7666
7667           break;
7668         }
7669
7670       goto binop;
7671
7672     case PLUS:
7673       /* In (and (plus FOO C1) M), if M is a mask that just turns off
7674          low-order bits (as in an alignment operation) and FOO is already
7675          aligned to that boundary, mask C1 to that boundary as well.
7676          This may eliminate that PLUS and, later, the AND.  */
7677
7678       {
7679         unsigned int width = GET_MODE_BITSIZE (mode);
7680         unsigned HOST_WIDE_INT smask = mask;
7681
7682         /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
7683            number, sign extend it.  */
7684
7685         if (width < HOST_BITS_PER_WIDE_INT
7686             && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7687           smask |= (HOST_WIDE_INT) -1 << width;
7688
7689         if (CONST_INT_P (XEXP (x, 1))
7690             && exact_log2 (- smask) >= 0
7691             && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
7692             && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
7693           return force_to_mode (plus_constant (XEXP (x, 0),
7694                                                (INTVAL (XEXP (x, 1)) & smask)),
7695                                 mode, smask, next_select);
7696       }
7697
7698       /* ... fall through ...  */
7699
7700     case MULT:
7701       /* For PLUS, MINUS and MULT, we need any bits less significant than the
7702          most significant bit in MASK since carries from those bits will
7703          affect the bits we are interested in.  */
7704       mask = fuller_mask;
7705       goto binop;
7706
7707     case MINUS:
7708       /* If X is (minus C Y) where C's least set bit is larger than any bit
7709          in the mask, then we may replace with (neg Y).  */
7710       if (CONST_INT_P (XEXP (x, 0))
7711           && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
7712                                         & -INTVAL (XEXP (x, 0))))
7713               > mask))
7714         {
7715           x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
7716                                   GET_MODE (x));
7717           return force_to_mode (x, mode, mask, next_select);
7718         }
7719
7720       /* Similarly, if C contains every bit in the fuller_mask, then we may
7721          replace with (not Y).  */
7722       if (CONST_INT_P (XEXP (x, 0))
7723           && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
7724               == INTVAL (XEXP (x, 0))))
7725         {
7726           x = simplify_gen_unary (NOT, GET_MODE (x),
7727                                   XEXP (x, 1), GET_MODE (x));
7728           return force_to_mode (x, mode, mask, next_select);
7729         }
7730
7731       mask = fuller_mask;
7732       goto binop;
7733
7734     case IOR:
7735     case XOR:
7736       /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
7737          LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
7738          operation which may be a bitfield extraction.  Ensure that the
7739          constant we form is not wider than the mode of X.  */
7740
7741       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7742           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7743           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7744           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7745           && CONST_INT_P (XEXP (x, 1))
7746           && ((INTVAL (XEXP (XEXP (x, 0), 1))
7747                + floor_log2 (INTVAL (XEXP (x, 1))))
7748               < GET_MODE_BITSIZE (GET_MODE (x)))
7749           && (INTVAL (XEXP (x, 1))
7750               & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
7751         {
7752           temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
7753                           << INTVAL (XEXP (XEXP (x, 0), 1)));
7754           temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
7755                                       XEXP (XEXP (x, 0), 0), temp);
7756           x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
7757                                    XEXP (XEXP (x, 0), 1));
7758           return force_to_mode (x, mode, mask, next_select);
7759         }
7760
7761     binop:
7762       /* For most binary operations, just propagate into the operation and
7763          change the mode if we have an operation of that mode.  */
7764
7765       op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
7766       op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
7767
7768       /* If we ended up truncating both operands, truncate the result of the
7769          operation instead.  */
7770       if (GET_CODE (op0) == TRUNCATE
7771           && GET_CODE (op1) == TRUNCATE)
7772         {
7773           op0 = XEXP (op0, 0);
7774           op1 = XEXP (op1, 0);
7775         }
7776
7777       op0 = gen_lowpart_or_truncate (op_mode, op0);
7778       op1 = gen_lowpart_or_truncate (op_mode, op1);
7779
7780       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7781         x = simplify_gen_binary (code, op_mode, op0, op1);
7782       break;
7783
7784     case ASHIFT:
7785       /* For left shifts, do the same, but just for the first operand.
7786          However, we cannot do anything with shifts where we cannot
7787          guarantee that the counts are smaller than the size of the mode
7788          because such a count will have a different meaning in a
7789          wider mode.  */
7790
7791       if (! (CONST_INT_P (XEXP (x, 1))
7792              && INTVAL (XEXP (x, 1)) >= 0
7793              && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7794           && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7795                 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7796                     < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7797         break;
7798
7799       /* If the shift count is a constant and we can do arithmetic in
7800          the mode of the shift, refine which bits we need.  Otherwise, use the
7801          conservative form of the mask.  */
7802       if (CONST_INT_P (XEXP (x, 1))
7803           && INTVAL (XEXP (x, 1)) >= 0
7804           && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7805           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7806         mask >>= INTVAL (XEXP (x, 1));
7807       else
7808         mask = fuller_mask;
7809
7810       op0 = gen_lowpart_or_truncate (op_mode,
7811                                      force_to_mode (XEXP (x, 0), op_mode,
7812                                                     mask, next_select));
7813
7814       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7815         x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
7816       break;
7817
7818     case LSHIFTRT:
7819       /* Here we can only do something if the shift count is a constant,
7820          this shift constant is valid for the host, and we can do arithmetic
7821          in OP_MODE.  */
7822
7823       if (CONST_INT_P (XEXP (x, 1))
7824           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7825           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7826         {
7827           rtx inner = XEXP (x, 0);
7828           unsigned HOST_WIDE_INT inner_mask;
7829
7830           /* Select the mask of the bits we need for the shift operand.  */
7831           inner_mask = mask << INTVAL (XEXP (x, 1));
7832
7833           /* We can only change the mode of the shift if we can do arithmetic
7834              in the mode of the shift and INNER_MASK is no wider than the
7835              width of X's mode.  */
7836           if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
7837             op_mode = GET_MODE (x);
7838
7839           inner = force_to_mode (inner, op_mode, inner_mask, next_select);
7840
7841           if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7842             x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7843         }
7844
7845       /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7846          shift and AND produces only copies of the sign bit (C2 is one less
7847          than a power of two), we can do this with just a shift.  */
7848
7849       if (GET_CODE (x) == LSHIFTRT
7850           && CONST_INT_P (XEXP (x, 1))
7851           /* The shift puts one of the sign bit copies in the least significant
7852              bit.  */
7853           && ((INTVAL (XEXP (x, 1))
7854                + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7855               >= GET_MODE_BITSIZE (GET_MODE (x)))
7856           && exact_log2 (mask + 1) >= 0
7857           /* Number of bits left after the shift must be more than the mask
7858              needs.  */
7859           && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7860               <= GET_MODE_BITSIZE (GET_MODE (x)))
7861           /* Must be more sign bit copies than the mask needs.  */
7862           && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7863               >= exact_log2 (mask + 1)))
7864         x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7865                                  GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7866                                           - exact_log2 (mask + 1)));
7867
7868       goto shiftrt;
7869
7870     case ASHIFTRT:
7871       /* If we are just looking for the sign bit, we don't need this shift at
7872          all, even if it has a variable count.  */
7873       if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7874           && (mask == ((unsigned HOST_WIDE_INT) 1
7875                        << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7876         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7877
7878       /* If this is a shift by a constant, get a mask that contains those bits
7879          that are not copies of the sign bit.  We then have two cases:  If
7880          MASK only includes those bits, this can be a logical shift, which may
7881          allow simplifications.  If MASK is a single-bit field not within
7882          those bits, we are requesting a copy of the sign bit and hence can
7883          shift the sign bit to the appropriate location.  */
7884
7885       if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
7886           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7887         {
7888           int i;
7889
7890           /* If the considered data is wider than HOST_WIDE_INT, we can't
7891              represent a mask for all its bits in a single scalar.
7892              But we only care about the lower bits, so calculate these.  */
7893
7894           if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7895             {
7896               nonzero = ~(HOST_WIDE_INT) 0;
7897
7898               /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7899                  is the number of bits a full-width mask would have set.
7900                  We need only shift if these are fewer than nonzero can
7901                  hold.  If not, we must keep all bits set in nonzero.  */
7902
7903               if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7904                   < HOST_BITS_PER_WIDE_INT)
7905                 nonzero >>= INTVAL (XEXP (x, 1))
7906                             + HOST_BITS_PER_WIDE_INT
7907                             - GET_MODE_BITSIZE (GET_MODE (x)) ;
7908             }
7909           else
7910             {
7911               nonzero = GET_MODE_MASK (GET_MODE (x));
7912               nonzero >>= INTVAL (XEXP (x, 1));
7913             }
7914
7915           if ((mask & ~nonzero) == 0)
7916             {
7917               x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
7918                                         XEXP (x, 0), INTVAL (XEXP (x, 1)));
7919               if (GET_CODE (x) != ASHIFTRT)
7920                 return force_to_mode (x, mode, mask, next_select);
7921             }
7922
7923           else if ((i = exact_log2 (mask)) >= 0)
7924             {
7925               x = simplify_shift_const
7926                   (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7927                    GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7928
7929               if (GET_CODE (x) != ASHIFTRT)
7930                 return force_to_mode (x, mode, mask, next_select);
7931             }
7932         }
7933
7934       /* If MASK is 1, convert this to an LSHIFTRT.  This can be done
7935          even if the shift count isn't a constant.  */
7936       if (mask == 1)
7937         x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7938                                  XEXP (x, 0), XEXP (x, 1));
7939
7940     shiftrt:
7941
7942       /* If this is a zero- or sign-extension operation that just affects bits
7943          we don't care about, remove it.  Be sure the call above returned
7944          something that is still a shift.  */
7945
7946       if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7947           && CONST_INT_P (XEXP (x, 1))
7948           && INTVAL (XEXP (x, 1)) >= 0
7949           && (INTVAL (XEXP (x, 1))
7950               <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7951           && GET_CODE (XEXP (x, 0)) == ASHIFT
7952           && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7953         return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7954                               next_select);
7955
7956       break;
7957
7958     case ROTATE:
7959     case ROTATERT:
7960       /* If the shift count is constant and we can do computations
7961          in the mode of X, compute where the bits we care about are.
7962          Otherwise, we can't do anything.  Don't change the mode of
7963          the shift or propagate MODE into the shift, though.  */
7964       if (CONST_INT_P (XEXP (x, 1))
7965           && INTVAL (XEXP (x, 1)) >= 0)
7966         {
7967           temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7968                                             GET_MODE (x), GEN_INT (mask),
7969                                             XEXP (x, 1));
7970           if (temp && CONST_INT_P (temp))
7971             SUBST (XEXP (x, 0),
7972                    force_to_mode (XEXP (x, 0), GET_MODE (x),
7973                                   INTVAL (temp), next_select));
7974         }
7975       break;
7976
7977     case NEG:
7978       /* If we just want the low-order bit, the NEG isn't needed since it
7979          won't change the low-order bit.  */
7980       if (mask == 1)
7981         return force_to_mode (XEXP (x, 0), mode, mask, just_select);
7982
7983       /* We need any bits less significant than the most significant bit in
7984          MASK since carries from those bits will affect the bits we are
7985          interested in.  */
7986       mask = fuller_mask;
7987       goto unop;
7988
7989     case NOT:
7990       /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7991          same as the XOR case above.  Ensure that the constant we form is not
7992          wider than the mode of X.  */
7993
7994       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7995           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7996           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7997           && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7998               < GET_MODE_BITSIZE (GET_MODE (x)))
7999           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8000         {
8001           temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8002                                GET_MODE (x));
8003           temp = simplify_gen_binary (XOR, GET_MODE (x),
8004                                       XEXP (XEXP (x, 0), 0), temp);
8005           x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8006                                    temp, XEXP (XEXP (x, 0), 1));
8007
8008           return force_to_mode (x, mode, mask, next_select);
8009         }
8010
8011       /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8012          use the full mask inside the NOT.  */
8013       mask = fuller_mask;
8014
8015     unop:
8016       op0 = gen_lowpart_or_truncate (op_mode,
8017                                      force_to_mode (XEXP (x, 0), mode, mask,
8018                                                     next_select));
8019       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8020         x = simplify_gen_unary (code, op_mode, op0, op_mode);
8021       break;
8022
8023     case NE:
8024       /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8025          in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8026          which is equal to STORE_FLAG_VALUE.  */
8027       if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
8028           && GET_MODE (XEXP (x, 0)) == mode
8029           && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8030           && (nonzero_bits (XEXP (x, 0), mode)
8031               == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8032         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8033
8034       break;
8035
8036     case IF_THEN_ELSE:
8037       /* We have no way of knowing if the IF_THEN_ELSE can itself be
8038          written in a narrower mode.  We play it safe and do not do so.  */
8039
8040       SUBST (XEXP (x, 1),
8041              gen_lowpart_or_truncate (GET_MODE (x),
8042                                       force_to_mode (XEXP (x, 1), mode,
8043                                                      mask, next_select)));
8044       SUBST (XEXP (x, 2),
8045              gen_lowpart_or_truncate (GET_MODE (x),
8046                                       force_to_mode (XEXP (x, 2), mode,
8047                                                      mask, next_select)));
8048       break;
8049
8050     default:
8051       break;
8052     }
8053
8054   /* Ensure we return a value of the proper mode.  */
8055   return gen_lowpart_or_truncate (mode, x);
8056 }
8057 \f
8058 /* Return nonzero if X is an expression that has one of two values depending on
8059    whether some other value is zero or nonzero.  In that case, we return the
8060    value that is being tested, *PTRUE is set to the value if the rtx being
8061    returned has a nonzero value, and *PFALSE is set to the other alternative.
8062
8063    If we return zero, we set *PTRUE and *PFALSE to X.  */
8064
8065 static rtx
8066 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8067 {
8068   enum machine_mode mode = GET_MODE (x);
8069   enum rtx_code code = GET_CODE (x);
8070   rtx cond0, cond1, true0, true1, false0, false1;
8071   unsigned HOST_WIDE_INT nz;
8072
8073   /* If we are comparing a value against zero, we are done.  */
8074   if ((code == NE || code == EQ)
8075       && XEXP (x, 1) == const0_rtx)
8076     {
8077       *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8078       *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8079       return XEXP (x, 0);
8080     }
8081
8082   /* If this is a unary operation whose operand has one of two values, apply
8083      our opcode to compute those values.  */
8084   else if (UNARY_P (x)
8085            && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8086     {
8087       *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8088       *pfalse = simplify_gen_unary (code, mode, false0,
8089                                     GET_MODE (XEXP (x, 0)));
8090       return cond0;
8091     }
8092
8093   /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8094      make can't possibly match and would suppress other optimizations.  */
8095   else if (code == COMPARE)
8096     ;
8097
8098   /* If this is a binary operation, see if either side has only one of two
8099      values.  If either one does or if both do and they are conditional on
8100      the same value, compute the new true and false values.  */
8101   else if (BINARY_P (x))
8102     {
8103       cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8104       cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8105
8106       if ((cond0 != 0 || cond1 != 0)
8107           && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8108         {
8109           /* If if_then_else_cond returned zero, then true/false are the
8110              same rtl.  We must copy one of them to prevent invalid rtl
8111              sharing.  */
8112           if (cond0 == 0)
8113             true0 = copy_rtx (true0);
8114           else if (cond1 == 0)
8115             true1 = copy_rtx (true1);
8116
8117           if (COMPARISON_P (x))
8118             {
8119               *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8120                                                 true0, true1);
8121               *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8122                                                  false0, false1);
8123              }
8124           else
8125             {
8126               *ptrue = simplify_gen_binary (code, mode, true0, true1);
8127               *pfalse = simplify_gen_binary (code, mode, false0, false1);
8128             }
8129
8130           return cond0 ? cond0 : cond1;
8131         }
8132
8133       /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8134          operands is zero when the other is nonzero, and vice-versa,
8135          and STORE_FLAG_VALUE is 1 or -1.  */
8136
8137       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8138           && (code == PLUS || code == IOR || code == XOR || code == MINUS
8139               || code == UMAX)
8140           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8141         {
8142           rtx op0 = XEXP (XEXP (x, 0), 1);
8143           rtx op1 = XEXP (XEXP (x, 1), 1);
8144
8145           cond0 = XEXP (XEXP (x, 0), 0);
8146           cond1 = XEXP (XEXP (x, 1), 0);
8147
8148           if (COMPARISON_P (cond0)
8149               && COMPARISON_P (cond1)
8150               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8151                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8152                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8153                   || ((swap_condition (GET_CODE (cond0))
8154                        == reversed_comparison_code (cond1, NULL))
8155                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8156                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8157               && ! side_effects_p (x))
8158             {
8159               *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8160               *pfalse = simplify_gen_binary (MULT, mode,
8161                                              (code == MINUS
8162                                               ? simplify_gen_unary (NEG, mode,
8163                                                                     op1, mode)
8164                                               : op1),
8165                                               const_true_rtx);
8166               return cond0;
8167             }
8168         }
8169
8170       /* Similarly for MULT, AND and UMIN, except that for these the result
8171          is always zero.  */
8172       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8173           && (code == MULT || code == AND || code == UMIN)
8174           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8175         {
8176           cond0 = XEXP (XEXP (x, 0), 0);
8177           cond1 = XEXP (XEXP (x, 1), 0);
8178
8179           if (COMPARISON_P (cond0)
8180               && COMPARISON_P (cond1)
8181               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8182                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8183                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8184                   || ((swap_condition (GET_CODE (cond0))
8185                        == reversed_comparison_code (cond1, NULL))
8186                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8187                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8188               && ! side_effects_p (x))
8189             {
8190               *ptrue = *pfalse = const0_rtx;
8191               return cond0;
8192             }
8193         }
8194     }
8195
8196   else if (code == IF_THEN_ELSE)
8197     {
8198       /* If we have IF_THEN_ELSE already, extract the condition and
8199          canonicalize it if it is NE or EQ.  */
8200       cond0 = XEXP (x, 0);
8201       *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8202       if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8203         return XEXP (cond0, 0);
8204       else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8205         {
8206           *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8207           return XEXP (cond0, 0);
8208         }
8209       else
8210         return cond0;
8211     }
8212
8213   /* If X is a SUBREG, we can narrow both the true and false values
8214      if the inner expression, if there is a condition.  */
8215   else if (code == SUBREG
8216            && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8217                                                &true0, &false0)))
8218     {
8219       true0 = simplify_gen_subreg (mode, true0,
8220                                    GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8221       false0 = simplify_gen_subreg (mode, false0,
8222                                     GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8223       if (true0 && false0)
8224         {
8225           *ptrue = true0;
8226           *pfalse = false0;
8227           return cond0;
8228         }
8229     }
8230
8231   /* If X is a constant, this isn't special and will cause confusions
8232      if we treat it as such.  Likewise if it is equivalent to a constant.  */
8233   else if (CONSTANT_P (x)
8234            || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8235     ;
8236
8237   /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8238      will be least confusing to the rest of the compiler.  */
8239   else if (mode == BImode)
8240     {
8241       *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8242       return x;
8243     }
8244
8245   /* If X is known to be either 0 or -1, those are the true and
8246      false values when testing X.  */
8247   else if (x == constm1_rtx || x == const0_rtx
8248            || (mode != VOIDmode
8249                && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
8250     {
8251       *ptrue = constm1_rtx, *pfalse = const0_rtx;
8252       return x;
8253     }
8254
8255   /* Likewise for 0 or a single bit.  */
8256   else if (SCALAR_INT_MODE_P (mode)
8257            && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8258            && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
8259     {
8260       *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
8261       return x;
8262     }
8263
8264   /* Otherwise fail; show no condition with true and false values the same.  */
8265   *ptrue = *pfalse = x;
8266   return 0;
8267 }
8268 \f
8269 /* Return the value of expression X given the fact that condition COND
8270    is known to be true when applied to REG as its first operand and VAL
8271    as its second.  X is known to not be shared and so can be modified in
8272    place.
8273
8274    We only handle the simplest cases, and specifically those cases that
8275    arise with IF_THEN_ELSE expressions.  */
8276
8277 static rtx
8278 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8279 {
8280   enum rtx_code code = GET_CODE (x);
8281   rtx temp;
8282   const char *fmt;
8283   int i, j;
8284
8285   if (side_effects_p (x))
8286     return x;
8287
8288   /* If either operand of the condition is a floating point value,
8289      then we have to avoid collapsing an EQ comparison.  */
8290   if (cond == EQ
8291       && rtx_equal_p (x, reg)
8292       && ! FLOAT_MODE_P (GET_MODE (x))
8293       && ! FLOAT_MODE_P (GET_MODE (val)))
8294     return val;
8295
8296   if (cond == UNEQ && rtx_equal_p (x, reg))
8297     return val;
8298
8299   /* If X is (abs REG) and we know something about REG's relationship
8300      with zero, we may be able to simplify this.  */
8301
8302   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8303     switch (cond)
8304       {
8305       case GE:  case GT:  case EQ:
8306         return XEXP (x, 0);
8307       case LT:  case LE:
8308         return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8309                                    XEXP (x, 0),
8310                                    GET_MODE (XEXP (x, 0)));
8311       default:
8312         break;
8313       }
8314
8315   /* The only other cases we handle are MIN, MAX, and comparisons if the
8316      operands are the same as REG and VAL.  */
8317
8318   else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8319     {
8320       if (rtx_equal_p (XEXP (x, 0), val))
8321         cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8322
8323       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8324         {
8325           if (COMPARISON_P (x))
8326             {
8327               if (comparison_dominates_p (cond, code))
8328                 return const_true_rtx;
8329
8330               code = reversed_comparison_code (x, NULL);
8331               if (code != UNKNOWN
8332                   && comparison_dominates_p (cond, code))
8333                 return const0_rtx;
8334               else
8335                 return x;
8336             }
8337           else if (code == SMAX || code == SMIN
8338                    || code == UMIN || code == UMAX)
8339             {
8340               int unsignedp = (code == UMIN || code == UMAX);
8341
8342               /* Do not reverse the condition when it is NE or EQ.
8343                  This is because we cannot conclude anything about
8344                  the value of 'SMAX (x, y)' when x is not equal to y,
8345                  but we can when x equals y.  */
8346               if ((code == SMAX || code == UMAX)
8347                   && ! (cond == EQ || cond == NE))
8348                 cond = reverse_condition (cond);
8349
8350               switch (cond)
8351                 {
8352                 case GE:   case GT:
8353                   return unsignedp ? x : XEXP (x, 1);
8354                 case LE:   case LT:
8355                   return unsignedp ? x : XEXP (x, 0);
8356                 case GEU:  case GTU:
8357                   return unsignedp ? XEXP (x, 1) : x;
8358                 case LEU:  case LTU:
8359                   return unsignedp ? XEXP (x, 0) : x;
8360                 default:
8361                   break;
8362                 }
8363             }
8364         }
8365     }
8366   else if (code == SUBREG)
8367     {
8368       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8369       rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
8370
8371       if (SUBREG_REG (x) != r)
8372         {
8373           /* We must simplify subreg here, before we lose track of the
8374              original inner_mode.  */
8375           new_rtx = simplify_subreg (GET_MODE (x), r,
8376                                  inner_mode, SUBREG_BYTE (x));
8377           if (new_rtx)
8378             return new_rtx;
8379           else
8380             SUBST (SUBREG_REG (x), r);
8381         }
8382
8383       return x;
8384     }
8385   /* We don't have to handle SIGN_EXTEND here, because even in the
8386      case of replacing something with a modeless CONST_INT, a
8387      CONST_INT is already (supposed to be) a valid sign extension for
8388      its narrower mode, which implies it's already properly
8389      sign-extended for the wider mode.  Now, for ZERO_EXTEND, the
8390      story is different.  */
8391   else if (code == ZERO_EXTEND)
8392     {
8393       enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8394       rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
8395
8396       if (XEXP (x, 0) != r)
8397         {
8398           /* We must simplify the zero_extend here, before we lose
8399              track of the original inner_mode.  */
8400           new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8401                                           r, inner_mode);
8402           if (new_rtx)
8403             return new_rtx;
8404           else
8405             SUBST (XEXP (x, 0), r);
8406         }
8407
8408       return x;
8409     }
8410
8411   fmt = GET_RTX_FORMAT (code);
8412   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8413     {
8414       if (fmt[i] == 'e')
8415         SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8416       else if (fmt[i] == 'E')
8417         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8418           SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8419                                                 cond, reg, val));
8420     }
8421
8422   return x;
8423 }
8424 \f
8425 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8426    assignment as a field assignment.  */
8427
8428 static int
8429 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8430 {
8431   if (x == y || rtx_equal_p (x, y))
8432     return 1;
8433
8434   if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8435     return 0;
8436
8437   /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8438      Note that all SUBREGs of MEM are paradoxical; otherwise they
8439      would have been rewritten.  */
8440   if (MEM_P (x) && GET_CODE (y) == SUBREG
8441       && MEM_P (SUBREG_REG (y))
8442       && rtx_equal_p (SUBREG_REG (y),
8443                       gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8444     return 1;
8445
8446   if (MEM_P (y) && GET_CODE (x) == SUBREG
8447       && MEM_P (SUBREG_REG (x))
8448       && rtx_equal_p (SUBREG_REG (x),
8449                       gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8450     return 1;
8451
8452   /* We used to see if get_last_value of X and Y were the same but that's
8453      not correct.  In one direction, we'll cause the assignment to have
8454      the wrong destination and in the case, we'll import a register into this
8455      insn that might have already have been dead.   So fail if none of the
8456      above cases are true.  */
8457   return 0;
8458 }
8459 \f
8460 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8461    Return that assignment if so.
8462
8463    We only handle the most common cases.  */
8464
8465 static rtx
8466 make_field_assignment (rtx x)
8467 {
8468   rtx dest = SET_DEST (x);
8469   rtx src = SET_SRC (x);
8470   rtx assign;
8471   rtx rhs, lhs;
8472   HOST_WIDE_INT c1;
8473   HOST_WIDE_INT pos;
8474   unsigned HOST_WIDE_INT len;
8475   rtx other;
8476   enum machine_mode mode;
8477
8478   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
8479      a clear of a one-bit field.  We will have changed it to
8480      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
8481      for a SUBREG.  */
8482
8483   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
8484       && CONST_INT_P (XEXP (XEXP (src, 0), 0))
8485       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
8486       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8487     {
8488       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8489                                 1, 1, 1, 0);
8490       if (assign != 0)
8491         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8492       return x;
8493     }
8494
8495   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
8496       && subreg_lowpart_p (XEXP (src, 0))
8497       && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
8498           < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
8499       && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
8500       && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
8501       && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
8502       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8503     {
8504       assign = make_extraction (VOIDmode, dest, 0,
8505                                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
8506                                 1, 1, 1, 0);
8507       if (assign != 0)
8508         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8509       return x;
8510     }
8511
8512   /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
8513      one-bit field.  */
8514   if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
8515       && XEXP (XEXP (src, 0), 0) == const1_rtx
8516       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8517     {
8518       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8519                                 1, 1, 1, 0);
8520       if (assign != 0)
8521         return gen_rtx_SET (VOIDmode, assign, const1_rtx);
8522       return x;
8523     }
8524
8525   /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
8526      SRC is an AND with all bits of that field set, then we can discard
8527      the AND.  */
8528   if (GET_CODE (dest) == ZERO_EXTRACT
8529       && CONST_INT_P (XEXP (dest, 1))
8530       && GET_CODE (src) == AND
8531       && CONST_INT_P (XEXP (src, 1)))
8532     {
8533       HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
8534       unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
8535       unsigned HOST_WIDE_INT ze_mask;
8536
8537       if (width >= HOST_BITS_PER_WIDE_INT)
8538         ze_mask = -1;
8539       else
8540         ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
8541
8542       /* Complete overlap.  We can remove the source AND.  */
8543       if ((and_mask & ze_mask) == ze_mask)
8544         return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
8545
8546       /* Partial overlap.  We can reduce the source AND.  */
8547       if ((and_mask & ze_mask) != and_mask)
8548         {
8549           mode = GET_MODE (src);
8550           src = gen_rtx_AND (mode, XEXP (src, 0),
8551                              gen_int_mode (and_mask & ze_mask, mode));
8552           return gen_rtx_SET (VOIDmode, dest, src);
8553         }
8554     }
8555
8556   /* The other case we handle is assignments into a constant-position
8557      field.  They look like (ior/xor (and DEST C1) OTHER).  If C1 represents
8558      a mask that has all one bits except for a group of zero bits and
8559      OTHER is known to have zeros where C1 has ones, this is such an
8560      assignment.  Compute the position and length from C1.  Shift OTHER
8561      to the appropriate position, force it to the required mode, and
8562      make the extraction.  Check for the AND in both operands.  */
8563
8564   if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
8565     return x;
8566
8567   rhs = expand_compound_operation (XEXP (src, 0));
8568   lhs = expand_compound_operation (XEXP (src, 1));
8569
8570   if (GET_CODE (rhs) == AND
8571       && CONST_INT_P (XEXP (rhs, 1))
8572       && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
8573     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
8574   else if (GET_CODE (lhs) == AND
8575            && CONST_INT_P (XEXP (lhs, 1))
8576            && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
8577     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
8578   else
8579     return x;
8580
8581   pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
8582   if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
8583       || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
8584       || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
8585     return x;
8586
8587   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
8588   if (assign == 0)
8589     return x;
8590
8591   /* The mode to use for the source is the mode of the assignment, or of
8592      what is inside a possible STRICT_LOW_PART.  */
8593   mode = (GET_CODE (assign) == STRICT_LOW_PART
8594           ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
8595
8596   /* Shift OTHER right POS places and make it the source, restricting it
8597      to the proper length and mode.  */
8598
8599   src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
8600                                                      GET_MODE (src),
8601                                                      other, pos),
8602                                dest);
8603   src = force_to_mode (src, mode,
8604                        GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
8605                        ? ~(unsigned HOST_WIDE_INT) 0
8606                        : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
8607                        0);
8608
8609   /* If SRC is masked by an AND that does not make a difference in
8610      the value being stored, strip it.  */
8611   if (GET_CODE (assign) == ZERO_EXTRACT
8612       && CONST_INT_P (XEXP (assign, 1))
8613       && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
8614       && GET_CODE (src) == AND
8615       && CONST_INT_P (XEXP (src, 1))
8616       && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
8617           == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
8618     src = XEXP (src, 0);
8619
8620   return gen_rtx_SET (VOIDmode, assign, src);
8621 }
8622 \f
8623 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
8624    if so.  */
8625
8626 static rtx
8627 apply_distributive_law (rtx x)
8628 {
8629   enum rtx_code code = GET_CODE (x);
8630   enum rtx_code inner_code;
8631   rtx lhs, rhs, other;
8632   rtx tem;
8633
8634   /* Distributivity is not true for floating point as it can change the
8635      value.  So we don't do it unless -funsafe-math-optimizations.  */
8636   if (FLOAT_MODE_P (GET_MODE (x))
8637       && ! flag_unsafe_math_optimizations)
8638     return x;
8639
8640   /* The outer operation can only be one of the following:  */
8641   if (code != IOR && code != AND && code != XOR
8642       && code != PLUS && code != MINUS)
8643     return x;
8644
8645   lhs = XEXP (x, 0);
8646   rhs = XEXP (x, 1);
8647
8648   /* If either operand is a primitive we can't do anything, so get out
8649      fast.  */
8650   if (OBJECT_P (lhs) || OBJECT_P (rhs))
8651     return x;
8652
8653   lhs = expand_compound_operation (lhs);
8654   rhs = expand_compound_operation (rhs);
8655   inner_code = GET_CODE (lhs);
8656   if (inner_code != GET_CODE (rhs))
8657     return x;
8658
8659   /* See if the inner and outer operations distribute.  */
8660   switch (inner_code)
8661     {
8662     case LSHIFTRT:
8663     case ASHIFTRT:
8664     case AND:
8665     case IOR:
8666       /* These all distribute except over PLUS.  */
8667       if (code == PLUS || code == MINUS)
8668         return x;
8669       break;
8670
8671     case MULT:
8672       if (code != PLUS && code != MINUS)
8673         return x;
8674       break;
8675
8676     case ASHIFT:
8677       /* This is also a multiply, so it distributes over everything.  */
8678       break;
8679
8680     case SUBREG:
8681       /* Non-paradoxical SUBREGs distributes over all operations,
8682          provided the inner modes and byte offsets are the same, this
8683          is an extraction of a low-order part, we don't convert an fp
8684          operation to int or vice versa, this is not a vector mode,
8685          and we would not be converting a single-word operation into a
8686          multi-word operation.  The latter test is not required, but
8687          it prevents generating unneeded multi-word operations.  Some
8688          of the previous tests are redundant given the latter test,
8689          but are retained because they are required for correctness.
8690
8691          We produce the result slightly differently in this case.  */
8692
8693       if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
8694           || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
8695           || ! subreg_lowpart_p (lhs)
8696           || (GET_MODE_CLASS (GET_MODE (lhs))
8697               != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
8698           || (GET_MODE_SIZE (GET_MODE (lhs))
8699               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
8700           || VECTOR_MODE_P (GET_MODE (lhs))
8701           || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD
8702           /* Result might need to be truncated.  Don't change mode if
8703              explicit truncation is needed.  */
8704           || !TRULY_NOOP_TRUNCATION
8705                (GET_MODE_BITSIZE (GET_MODE (x)),
8706                 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (lhs)))))
8707         return x;
8708
8709       tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
8710                                  SUBREG_REG (lhs), SUBREG_REG (rhs));
8711       return gen_lowpart (GET_MODE (x), tem);
8712
8713     default:
8714       return x;
8715     }
8716
8717   /* Set LHS and RHS to the inner operands (A and B in the example
8718      above) and set OTHER to the common operand (C in the example).
8719      There is only one way to do this unless the inner operation is
8720      commutative.  */
8721   if (COMMUTATIVE_ARITH_P (lhs)
8722       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
8723     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
8724   else if (COMMUTATIVE_ARITH_P (lhs)
8725            && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
8726     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
8727   else if (COMMUTATIVE_ARITH_P (lhs)
8728            && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
8729     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
8730   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
8731     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
8732   else
8733     return x;
8734
8735   /* Form the new inner operation, seeing if it simplifies first.  */
8736   tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
8737
8738   /* There is one exception to the general way of distributing:
8739      (a | c) ^ (b | c) -> (a ^ b) & ~c  */
8740   if (code == XOR && inner_code == IOR)
8741     {
8742       inner_code = AND;
8743       other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
8744     }
8745
8746   /* We may be able to continuing distributing the result, so call
8747      ourselves recursively on the inner operation before forming the
8748      outer operation, which we return.  */
8749   return simplify_gen_binary (inner_code, GET_MODE (x),
8750                               apply_distributive_law (tem), other);
8751 }
8752
8753 /* See if X is of the form (* (+ A B) C), and if so convert to
8754    (+ (* A C) (* B C)) and try to simplify.
8755
8756    Most of the time, this results in no change.  However, if some of
8757    the operands are the same or inverses of each other, simplifications
8758    will result.
8759
8760    For example, (and (ior A B) (not B)) can occur as the result of
8761    expanding a bit field assignment.  When we apply the distributive
8762    law to this, we get (ior (and (A (not B))) (and (B (not B)))),
8763    which then simplifies to (and (A (not B))).
8764
8765    Note that no checks happen on the validity of applying the inverse
8766    distributive law.  This is pointless since we can do it in the
8767    few places where this routine is called.
8768
8769    N is the index of the term that is decomposed (the arithmetic operation,
8770    i.e. (+ A B) in the first example above).  !N is the index of the term that
8771    is distributed, i.e. of C in the first example above.  */
8772 static rtx
8773 distribute_and_simplify_rtx (rtx x, int n)
8774 {
8775   enum machine_mode mode;
8776   enum rtx_code outer_code, inner_code;
8777   rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
8778
8779   /* Distributivity is not true for floating point as it can change the
8780      value.  So we don't do it unless -funsafe-math-optimizations.  */
8781   if (FLOAT_MODE_P (GET_MODE (x))
8782       && ! flag_unsafe_math_optimizations)
8783     return NULL_RTX;
8784
8785   decomposed = XEXP (x, n);
8786   if (!ARITHMETIC_P (decomposed))
8787     return NULL_RTX;
8788
8789   mode = GET_MODE (x);
8790   outer_code = GET_CODE (x);
8791   distributed = XEXP (x, !n);
8792
8793   inner_code = GET_CODE (decomposed);
8794   inner_op0 = XEXP (decomposed, 0);
8795   inner_op1 = XEXP (decomposed, 1);
8796
8797   /* Special case (and (xor B C) (not A)), which is equivalent to
8798      (xor (ior A B) (ior A C))  */
8799   if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
8800     {
8801       distributed = XEXP (distributed, 0);
8802       outer_code = IOR;
8803     }
8804
8805   if (n == 0)
8806     {
8807       /* Distribute the second term.  */
8808       new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
8809       new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
8810     }
8811   else
8812     {
8813       /* Distribute the first term.  */
8814       new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
8815       new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
8816     }
8817
8818   tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
8819                                                      new_op0, new_op1));
8820   if (GET_CODE (tmp) != outer_code
8821       && rtx_cost (tmp, SET, optimize_this_for_speed_p)
8822          < rtx_cost (x, SET, optimize_this_for_speed_p))
8823     return tmp;
8824
8825   return NULL_RTX;
8826 }
8827 \f
8828 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
8829    in MODE.  Return an equivalent form, if different from (and VAROP
8830    (const_int CONSTOP)).  Otherwise, return NULL_RTX.  */
8831
8832 static rtx
8833 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
8834                           unsigned HOST_WIDE_INT constop)
8835 {
8836   unsigned HOST_WIDE_INT nonzero;
8837   unsigned HOST_WIDE_INT orig_constop;
8838   rtx orig_varop;
8839   int i;
8840
8841   orig_varop = varop;
8842   orig_constop = constop;
8843   if (GET_CODE (varop) == CLOBBER)
8844     return NULL_RTX;
8845
8846   /* Simplify VAROP knowing that we will be only looking at some of the
8847      bits in it.
8848
8849      Note by passing in CONSTOP, we guarantee that the bits not set in
8850      CONSTOP are not significant and will never be examined.  We must
8851      ensure that is the case by explicitly masking out those bits
8852      before returning.  */
8853   varop = force_to_mode (varop, mode, constop, 0);
8854
8855   /* If VAROP is a CLOBBER, we will fail so return it.  */
8856   if (GET_CODE (varop) == CLOBBER)
8857     return varop;
8858
8859   /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
8860      to VAROP and return the new constant.  */
8861   if (CONST_INT_P (varop))
8862     return gen_int_mode (INTVAL (varop) & constop, mode);
8863
8864   /* See what bits may be nonzero in VAROP.  Unlike the general case of
8865      a call to nonzero_bits, here we don't care about bits outside
8866      MODE.  */
8867
8868   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
8869
8870   /* Turn off all bits in the constant that are known to already be zero.
8871      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
8872      which is tested below.  */
8873
8874   constop &= nonzero;
8875
8876   /* If we don't have any bits left, return zero.  */
8877   if (constop == 0)
8878     return const0_rtx;
8879
8880   /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
8881      a power of two, we can replace this with an ASHIFT.  */
8882   if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
8883       && (i = exact_log2 (constop)) >= 0)
8884     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
8885
8886   /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8887      or XOR, then try to apply the distributive law.  This may eliminate
8888      operations if either branch can be simplified because of the AND.
8889      It may also make some cases more complex, but those cases probably
8890      won't match a pattern either with or without this.  */
8891
8892   if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
8893     return
8894       gen_lowpart
8895         (mode,
8896          apply_distributive_law
8897          (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
8898                                simplify_and_const_int (NULL_RTX,
8899                                                        GET_MODE (varop),
8900                                                        XEXP (varop, 0),
8901                                                        constop),
8902                                simplify_and_const_int (NULL_RTX,
8903                                                        GET_MODE (varop),
8904                                                        XEXP (varop, 1),
8905                                                        constop))));
8906
8907   /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
8908      the AND and see if one of the operands simplifies to zero.  If so, we
8909      may eliminate it.  */
8910
8911   if (GET_CODE (varop) == PLUS
8912       && exact_log2 (constop + 1) >= 0)
8913     {
8914       rtx o0, o1;
8915
8916       o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
8917       o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
8918       if (o0 == const0_rtx)
8919         return o1;
8920       if (o1 == const0_rtx)
8921         return o0;
8922     }
8923
8924   /* Make a SUBREG if necessary.  If we can't make it, fail.  */
8925   varop = gen_lowpart (mode, varop);
8926   if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
8927     return NULL_RTX;
8928
8929   /* If we are only masking insignificant bits, return VAROP.  */
8930   if (constop == nonzero)
8931     return varop;
8932
8933   if (varop == orig_varop && constop == orig_constop)
8934     return NULL_RTX;
8935
8936   /* Otherwise, return an AND.  */
8937   return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
8938 }
8939
8940
8941 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
8942    in MODE.
8943
8944    Return an equivalent form, if different from X.  Otherwise, return X.  If
8945    X is zero, we are to always construct the equivalent form.  */
8946
8947 static rtx
8948 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
8949                         unsigned HOST_WIDE_INT constop)
8950 {
8951   rtx tem = simplify_and_const_int_1 (mode, varop, constop);
8952   if (tem)
8953     return tem;
8954
8955   if (!x)
8956     x = simplify_gen_binary (AND, GET_MODE (varop), varop,
8957                              gen_int_mode (constop, mode));
8958   if (GET_MODE (x) != mode)
8959     x = gen_lowpart (mode, x);
8960   return x;
8961 }
8962 \f
8963 /* Given a REG, X, compute which bits in X can be nonzero.
8964    We don't care about bits outside of those defined in MODE.
8965
8966    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8967    a shift, AND, or zero_extract, we can do better.  */
8968
8969 static rtx
8970 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
8971                               const_rtx known_x ATTRIBUTE_UNUSED,
8972                               enum machine_mode known_mode ATTRIBUTE_UNUSED,
8973                               unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
8974                               unsigned HOST_WIDE_INT *nonzero)
8975 {
8976   rtx tem;
8977   reg_stat_type *rsp;
8978
8979   /* If X is a register whose nonzero bits value is current, use it.
8980      Otherwise, if X is a register whose value we can find, use that
8981      value.  Otherwise, use the previously-computed global nonzero bits
8982      for this register.  */
8983
8984   rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
8985   if (rsp->last_set_value != 0
8986       && (rsp->last_set_mode == mode
8987           || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
8988               && GET_MODE_CLASS (mode) == MODE_INT))
8989       && ((rsp->last_set_label >= label_tick_ebb_start
8990            && rsp->last_set_label < label_tick)
8991           || (rsp->last_set_label == label_tick
8992               && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
8993           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8994               && REG_N_SETS (REGNO (x)) == 1
8995               && !REGNO_REG_SET_P
8996                   (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
8997     {
8998       *nonzero &= rsp->last_set_nonzero_bits;
8999       return NULL;
9000     }
9001
9002   tem = get_last_value (x);
9003
9004   if (tem)
9005     {
9006 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9007       /* If X is narrower than MODE and TEM is a non-negative
9008          constant that would appear negative in the mode of X,
9009          sign-extend it for use in reg_nonzero_bits because some
9010          machines (maybe most) will actually do the sign-extension
9011          and this is the conservative approach.
9012
9013          ??? For 2.5, try to tighten up the MD files in this regard
9014          instead of this kludge.  */
9015
9016       if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
9017           && CONST_INT_P (tem)
9018           && INTVAL (tem) > 0
9019           && 0 != (INTVAL (tem)
9020                    & ((HOST_WIDE_INT) 1
9021                       << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
9022         tem = GEN_INT (INTVAL (tem)
9023                        | ((HOST_WIDE_INT) (-1)
9024                           << GET_MODE_BITSIZE (GET_MODE (x))));
9025 #endif
9026       return tem;
9027     }
9028   else if (nonzero_sign_valid && rsp->nonzero_bits)
9029     {
9030       unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9031
9032       if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
9033         /* We don't know anything about the upper bits.  */
9034         mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9035       *nonzero &= mask;
9036     }
9037
9038   return NULL;
9039 }
9040
9041 /* Return the number of bits at the high-order end of X that are known to
9042    be equal to the sign bit.  X will be used in mode MODE; if MODE is
9043    VOIDmode, X will be used in its own mode.  The returned value  will always
9044    be between 1 and the number of bits in MODE.  */
9045
9046 static rtx
9047 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
9048                                      const_rtx known_x ATTRIBUTE_UNUSED,
9049                                      enum machine_mode known_mode
9050                                      ATTRIBUTE_UNUSED,
9051                                      unsigned int known_ret ATTRIBUTE_UNUSED,
9052                                      unsigned int *result)
9053 {
9054   rtx tem;
9055   reg_stat_type *rsp;
9056
9057   rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
9058   if (rsp->last_set_value != 0
9059       && rsp->last_set_mode == mode
9060       && ((rsp->last_set_label >= label_tick_ebb_start
9061            && rsp->last_set_label < label_tick)
9062           || (rsp->last_set_label == label_tick
9063               && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9064           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9065               && REG_N_SETS (REGNO (x)) == 1
9066               && !REGNO_REG_SET_P
9067                   (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
9068     {
9069       *result = rsp->last_set_sign_bit_copies;
9070       return NULL;
9071     }
9072
9073   tem = get_last_value (x);
9074   if (tem != 0)
9075     return tem;
9076
9077   if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9078       && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
9079     *result = rsp->sign_bit_copies;
9080
9081   return NULL;
9082 }
9083 \f
9084 /* Return the number of "extended" bits there are in X, when interpreted
9085    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
9086    unsigned quantities, this is the number of high-order zero bits.
9087    For signed quantities, this is the number of copies of the sign bit
9088    minus 1.  In both case, this function returns the number of "spare"
9089    bits.  For example, if two quantities for which this function returns
9090    at least 1 are added, the addition is known not to overflow.
9091
9092    This function will always return 0 unless called during combine, which
9093    implies that it must be called from a define_split.  */
9094
9095 unsigned int
9096 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
9097 {
9098   if (nonzero_sign_valid == 0)
9099     return 0;
9100
9101   return (unsignedp
9102           ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9103              ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
9104                                - floor_log2 (nonzero_bits (x, mode)))
9105              : 0)
9106           : num_sign_bit_copies (x, mode) - 1);
9107 }
9108 \f
9109 /* This function is called from `simplify_shift_const' to merge two
9110    outer operations.  Specifically, we have already found that we need
9111    to perform operation *POP0 with constant *PCONST0 at the outermost
9112    position.  We would now like to also perform OP1 with constant CONST1
9113    (with *POP0 being done last).
9114
9115    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9116    the resulting operation.  *PCOMP_P is set to 1 if we would need to
9117    complement the innermost operand, otherwise it is unchanged.
9118
9119    MODE is the mode in which the operation will be done.  No bits outside
9120    the width of this mode matter.  It is assumed that the width of this mode
9121    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9122
9123    If *POP0 or OP1 are UNKNOWN, it means no operation is required.  Only NEG, PLUS,
9124    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
9125    result is simply *PCONST0.
9126
9127    If the resulting operation cannot be expressed as one operation, we
9128    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
9129
9130 static int
9131 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)
9132 {
9133   enum rtx_code op0 = *pop0;
9134   HOST_WIDE_INT const0 = *pconst0;
9135
9136   const0 &= GET_MODE_MASK (mode);
9137   const1 &= GET_MODE_MASK (mode);
9138
9139   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
9140   if (op0 == AND)
9141     const1 &= const0;
9142
9143   /* If OP0 or OP1 is UNKNOWN, this is easy.  Similarly if they are the same or
9144      if OP0 is SET.  */
9145
9146   if (op1 == UNKNOWN || op0 == SET)
9147     return 1;
9148
9149   else if (op0 == UNKNOWN)
9150     op0 = op1, const0 = const1;
9151
9152   else if (op0 == op1)
9153     {
9154       switch (op0)
9155         {
9156         case AND:
9157           const0 &= const1;
9158           break;
9159         case IOR:
9160           const0 |= const1;
9161           break;
9162         case XOR:
9163           const0 ^= const1;
9164           break;
9165         case PLUS:
9166           const0 += const1;
9167           break;
9168         case NEG:
9169           op0 = UNKNOWN;
9170           break;
9171         default:
9172           break;
9173         }
9174     }
9175
9176   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
9177   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9178     return 0;
9179
9180   /* If the two constants aren't the same, we can't do anything.  The
9181      remaining six cases can all be done.  */
9182   else if (const0 != const1)
9183     return 0;
9184
9185   else
9186     switch (op0)
9187       {
9188       case IOR:
9189         if (op1 == AND)
9190           /* (a & b) | b == b */
9191           op0 = SET;
9192         else /* op1 == XOR */
9193           /* (a ^ b) | b == a | b */
9194           {;}
9195         break;
9196
9197       case XOR:
9198         if (op1 == AND)
9199           /* (a & b) ^ b == (~a) & b */
9200           op0 = AND, *pcomp_p = 1;
9201         else /* op1 == IOR */
9202           /* (a | b) ^ b == a & ~b */
9203           op0 = AND, const0 = ~const0;
9204         break;
9205
9206       case AND:
9207         if (op1 == IOR)
9208           /* (a | b) & b == b */
9209         op0 = SET;
9210         else /* op1 == XOR */
9211           /* (a ^ b) & b) == (~a) & b */
9212           *pcomp_p = 1;
9213         break;
9214       default:
9215         break;
9216       }
9217
9218   /* Check for NO-OP cases.  */
9219   const0 &= GET_MODE_MASK (mode);
9220   if (const0 == 0
9221       && (op0 == IOR || op0 == XOR || op0 == PLUS))
9222     op0 = UNKNOWN;
9223   else if (const0 == 0 && op0 == AND)
9224     op0 = SET;
9225   else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9226            && op0 == AND)
9227     op0 = UNKNOWN;
9228
9229   *pop0 = op0;
9230
9231   /* ??? Slightly redundant with the above mask, but not entirely.
9232      Moving this above means we'd have to sign-extend the mode mask
9233      for the final test.  */
9234   if (op0 != UNKNOWN && op0 != NEG)
9235     *pconst0 = trunc_int_for_mode (const0, mode);
9236
9237   return 1;
9238 }
9239 \f
9240 /* A helper to simplify_shift_const_1 to determine the mode we can perform
9241    the shift in.  The original shift operation CODE is performed on OP in
9242    ORIG_MODE.  Return the wider mode MODE if we can perform the operation
9243    in that mode.  Return ORIG_MODE otherwise.  We can also assume that the
9244    result of the shift is subject to operation OUTER_CODE with operand
9245    OUTER_CONST.  */
9246
9247 static enum machine_mode
9248 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
9249                       enum machine_mode orig_mode, enum machine_mode mode,
9250                       enum rtx_code outer_code, HOST_WIDE_INT outer_const)
9251 {
9252   if (orig_mode == mode)
9253     return mode;
9254   gcc_assert (GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (orig_mode));
9255
9256   /* In general we can't perform in wider mode for right shift and rotate.  */
9257   switch (code)
9258     {
9259     case ASHIFTRT:
9260       /* We can still widen if the bits brought in from the left are identical
9261          to the sign bit of ORIG_MODE.  */
9262       if (num_sign_bit_copies (op, mode)
9263           > (unsigned) (GET_MODE_BITSIZE (mode)
9264                         - GET_MODE_BITSIZE (orig_mode)))
9265         return mode;
9266       return orig_mode;
9267
9268     case LSHIFTRT:
9269       /* Similarly here but with zero bits.  */
9270       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9271           && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
9272         return mode;
9273
9274       /* We can also widen if the bits brought in will be masked off.  This
9275          operation is performed in ORIG_MODE.  */
9276       if (outer_code == AND)
9277         {
9278           int care_bits = low_bitmask_len (orig_mode, outer_const);
9279
9280           if (care_bits >= 0
9281               && GET_MODE_BITSIZE (orig_mode) - care_bits >= count)
9282             return mode;
9283         }
9284       /* fall through */
9285
9286     case ROTATE:
9287       return orig_mode;
9288
9289     case ROTATERT:
9290       gcc_unreachable ();
9291
9292     default:
9293       return mode;
9294     }
9295 }
9296
9297 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
9298    The result of the shift is RESULT_MODE.  Return NULL_RTX if we cannot
9299    simplify it.  Otherwise, return a simplified value.
9300
9301    The shift is normally computed in the widest mode we find in VAROP, as
9302    long as it isn't a different number of words than RESULT_MODE.  Exceptions
9303    are ASHIFTRT and ROTATE, which are always done in their original mode.  */
9304
9305 static rtx
9306 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
9307                         rtx varop, int orig_count)
9308 {
9309   enum rtx_code orig_code = code;
9310   rtx orig_varop = varop;
9311   int count;
9312   enum machine_mode mode = result_mode;
9313   enum machine_mode shift_mode, tmode;
9314   unsigned int mode_words
9315     = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9316   /* We form (outer_op (code varop count) (outer_const)).  */
9317   enum rtx_code outer_op = UNKNOWN;
9318   HOST_WIDE_INT outer_const = 0;
9319   int complement_p = 0;
9320   rtx new_rtx, x;
9321
9322   /* Make sure and truncate the "natural" shift on the way in.  We don't
9323      want to do this inside the loop as it makes it more difficult to
9324      combine shifts.  */
9325   if (SHIFT_COUNT_TRUNCATED)
9326     orig_count &= GET_MODE_BITSIZE (mode) - 1;
9327
9328   /* If we were given an invalid count, don't do anything except exactly
9329      what was requested.  */
9330
9331   if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
9332     return NULL_RTX;
9333
9334   count = orig_count;
9335
9336   /* Unless one of the branches of the `if' in this loop does a `continue',
9337      we will `break' the loop after the `if'.  */
9338
9339   while (count != 0)
9340     {
9341       /* If we have an operand of (clobber (const_int 0)), fail.  */
9342       if (GET_CODE (varop) == CLOBBER)
9343         return NULL_RTX;
9344
9345       /* Convert ROTATERT to ROTATE.  */
9346       if (code == ROTATERT)
9347         {
9348           unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
9349           code = ROTATE;
9350           if (VECTOR_MODE_P (result_mode))
9351             count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9352           else
9353             count = bitsize - count;
9354         }
9355
9356       shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
9357                                          mode, outer_op, outer_const);
9358
9359       /* Handle cases where the count is greater than the size of the mode
9360          minus 1.  For ASHIFT, use the size minus one as the count (this can
9361          occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
9362          take the count modulo the size.  For other shifts, the result is
9363          zero.
9364
9365          Since these shifts are being produced by the compiler by combining
9366          multiple operations, each of which are defined, we know what the
9367          result is supposed to be.  */
9368
9369       if (count > (GET_MODE_BITSIZE (shift_mode) - 1))
9370         {
9371           if (code == ASHIFTRT)
9372             count = GET_MODE_BITSIZE (shift_mode) - 1;
9373           else if (code == ROTATE || code == ROTATERT)
9374             count %= GET_MODE_BITSIZE (shift_mode);
9375           else
9376             {
9377               /* We can't simply return zero because there may be an
9378                  outer op.  */
9379               varop = const0_rtx;
9380               count = 0;
9381               break;
9382             }
9383         }
9384
9385       /* If we discovered we had to complement VAROP, leave.  Making a NOT
9386          here would cause an infinite loop.  */
9387       if (complement_p)
9388         break;
9389
9390       /* An arithmetic right shift of a quantity known to be -1 or 0
9391          is a no-op.  */
9392       if (code == ASHIFTRT
9393           && (num_sign_bit_copies (varop, shift_mode)
9394               == GET_MODE_BITSIZE (shift_mode)))
9395         {
9396           count = 0;
9397           break;
9398         }
9399
9400       /* If we are doing an arithmetic right shift and discarding all but
9401          the sign bit copies, this is equivalent to doing a shift by the
9402          bitsize minus one.  Convert it into that shift because it will often
9403          allow other simplifications.  */
9404
9405       if (code == ASHIFTRT
9406           && (count + num_sign_bit_copies (varop, shift_mode)
9407               >= GET_MODE_BITSIZE (shift_mode)))
9408         count = GET_MODE_BITSIZE (shift_mode) - 1;
9409
9410       /* We simplify the tests below and elsewhere by converting
9411          ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9412          `make_compound_operation' will convert it to an ASHIFTRT for
9413          those machines (such as VAX) that don't have an LSHIFTRT.  */
9414       if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9415           && code == ASHIFTRT
9416           && ((nonzero_bits (varop, shift_mode)
9417                & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
9418               == 0))
9419         code = LSHIFTRT;
9420
9421       if (((code == LSHIFTRT
9422             && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9423             && !(nonzero_bits (varop, shift_mode) >> count))
9424            || (code == ASHIFT
9425                && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9426                && !((nonzero_bits (varop, shift_mode) << count)
9427                     & GET_MODE_MASK (shift_mode))))
9428           && !side_effects_p (varop))
9429         varop = const0_rtx;
9430
9431       switch (GET_CODE (varop))
9432         {
9433         case SIGN_EXTEND:
9434         case ZERO_EXTEND:
9435         case SIGN_EXTRACT:
9436         case ZERO_EXTRACT:
9437           new_rtx = expand_compound_operation (varop);
9438           if (new_rtx != varop)
9439             {
9440               varop = new_rtx;
9441               continue;
9442             }
9443           break;
9444
9445         case MEM:
9446           /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9447              minus the width of a smaller mode, we can do this with a
9448              SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
9449           if ((code == ASHIFTRT || code == LSHIFTRT)
9450               && ! mode_dependent_address_p (XEXP (varop, 0))
9451               && ! MEM_VOLATILE_P (varop)
9452               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9453                                          MODE_INT, 1)) != BLKmode)
9454             {
9455               new_rtx = adjust_address_nv (varop, tmode,
9456                                        BYTES_BIG_ENDIAN ? 0
9457                                        : count / BITS_PER_UNIT);
9458
9459               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9460                                      : ZERO_EXTEND, mode, new_rtx);
9461               count = 0;
9462               continue;
9463             }
9464           break;
9465
9466         case SUBREG:
9467           /* If VAROP is a SUBREG, strip it as long as the inner operand has
9468              the same number of words as what we've seen so far.  Then store
9469              the widest mode in MODE.  */
9470           if (subreg_lowpart_p (varop)
9471               && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9472                   > GET_MODE_SIZE (GET_MODE (varop)))
9473               && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9474                                   + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9475                  == mode_words)
9476             {
9477               varop = SUBREG_REG (varop);
9478               if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9479                 mode = GET_MODE (varop);
9480               continue;
9481             }
9482           break;
9483
9484         case MULT:
9485           /* Some machines use MULT instead of ASHIFT because MULT
9486              is cheaper.  But it is still better on those machines to
9487              merge two shifts into one.  */
9488           if (CONST_INT_P (XEXP (varop, 1))
9489               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9490             {
9491               varop
9492                 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
9493                                        XEXP (varop, 0),
9494                                        GEN_INT (exact_log2 (
9495                                                 INTVAL (XEXP (varop, 1)))));
9496               continue;
9497             }
9498           break;
9499
9500         case UDIV:
9501           /* Similar, for when divides are cheaper.  */
9502           if (CONST_INT_P (XEXP (varop, 1))
9503               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9504             {
9505               varop
9506                 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
9507                                        XEXP (varop, 0),
9508                                        GEN_INT (exact_log2 (
9509                                                 INTVAL (XEXP (varop, 1)))));
9510               continue;
9511             }
9512           break;
9513
9514         case ASHIFTRT:
9515           /* If we are extracting just the sign bit of an arithmetic
9516              right shift, that shift is not needed.  However, the sign
9517              bit of a wider mode may be different from what would be
9518              interpreted as the sign bit in a narrower mode, so, if
9519              the result is narrower, don't discard the shift.  */
9520           if (code == LSHIFTRT
9521               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9522               && (GET_MODE_BITSIZE (result_mode)
9523                   >= GET_MODE_BITSIZE (GET_MODE (varop))))
9524             {
9525               varop = XEXP (varop, 0);
9526               continue;
9527             }
9528
9529           /* ... fall through ...  */
9530
9531         case LSHIFTRT:
9532         case ASHIFT:
9533         case ROTATE:
9534           /* Here we have two nested shifts.  The result is usually the
9535              AND of a new shift with a mask.  We compute the result below.  */
9536           if (CONST_INT_P (XEXP (varop, 1))
9537               && INTVAL (XEXP (varop, 1)) >= 0
9538               && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9539               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9540               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9541               && !VECTOR_MODE_P (result_mode))
9542             {
9543               enum rtx_code first_code = GET_CODE (varop);
9544               unsigned int first_count = INTVAL (XEXP (varop, 1));
9545               unsigned HOST_WIDE_INT mask;
9546               rtx mask_rtx;
9547
9548               /* We have one common special case.  We can't do any merging if
9549                  the inner code is an ASHIFTRT of a smaller mode.  However, if
9550                  we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9551                  with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9552                  we can convert it to
9553                  (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9554                  This simplifies certain SIGN_EXTEND operations.  */
9555               if (code == ASHIFT && first_code == ASHIFTRT
9556                   && count == (GET_MODE_BITSIZE (result_mode)
9557                                - GET_MODE_BITSIZE (GET_MODE (varop))))
9558                 {
9559                   /* C3 has the low-order C1 bits zero.  */
9560
9561                   mask = (GET_MODE_MASK (mode)
9562                           & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
9563
9564                   varop = simplify_and_const_int (NULL_RTX, result_mode,
9565                                                   XEXP (varop, 0), mask);
9566                   varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
9567                                                 varop, count);
9568                   count = first_count;
9569                   code = ASHIFTRT;
9570                   continue;
9571                 }
9572
9573               /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
9574                  than C1 high-order bits equal to the sign bit, we can convert
9575                  this to either an ASHIFT or an ASHIFTRT depending on the
9576                  two counts.
9577
9578                  We cannot do this if VAROP's mode is not SHIFT_MODE.  */
9579
9580               if (code == ASHIFTRT && first_code == ASHIFT
9581                   && GET_MODE (varop) == shift_mode
9582                   && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
9583                       > first_count))
9584                 {
9585                   varop = XEXP (varop, 0);
9586                   count -= first_count;
9587                   if (count < 0)
9588                     {
9589                       count = -count;
9590                       code = ASHIFT;
9591                     }
9592
9593                   continue;
9594                 }
9595
9596               /* There are some cases we can't do.  If CODE is ASHIFTRT,
9597                  we can only do this if FIRST_CODE is also ASHIFTRT.
9598
9599                  We can't do the case when CODE is ROTATE and FIRST_CODE is
9600                  ASHIFTRT.
9601
9602                  If the mode of this shift is not the mode of the outer shift,
9603                  we can't do this if either shift is a right shift or ROTATE.
9604
9605                  Finally, we can't do any of these if the mode is too wide
9606                  unless the codes are the same.
9607
9608                  Handle the case where the shift codes are the same
9609                  first.  */
9610
9611               if (code == first_code)
9612                 {
9613                   if (GET_MODE (varop) != result_mode
9614                       && (code == ASHIFTRT || code == LSHIFTRT
9615                           || code == ROTATE))
9616                     break;
9617
9618                   count += first_count;
9619                   varop = XEXP (varop, 0);
9620                   continue;
9621                 }
9622
9623               if (code == ASHIFTRT
9624                   || (code == ROTATE && first_code == ASHIFTRT)
9625                   || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
9626                   || (GET_MODE (varop) != result_mode
9627                       && (first_code == ASHIFTRT || first_code == LSHIFTRT
9628                           || first_code == ROTATE
9629                           || code == ROTATE)))
9630                 break;
9631
9632               /* To compute the mask to apply after the shift, shift the
9633                  nonzero bits of the inner shift the same way the
9634                  outer shift will.  */
9635
9636               mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
9637
9638               mask_rtx
9639                 = simplify_const_binary_operation (code, result_mode, mask_rtx,
9640                                                    GEN_INT (count));
9641
9642               /* Give up if we can't compute an outer operation to use.  */
9643               if (mask_rtx == 0
9644                   || !CONST_INT_P (mask_rtx)
9645                   || ! merge_outer_ops (&outer_op, &outer_const, AND,
9646                                         INTVAL (mask_rtx),
9647                                         result_mode, &complement_p))
9648                 break;
9649
9650               /* If the shifts are in the same direction, we add the
9651                  counts.  Otherwise, we subtract them.  */
9652               if ((code == ASHIFTRT || code == LSHIFTRT)
9653                   == (first_code == ASHIFTRT || first_code == LSHIFTRT))
9654                 count += first_count;
9655               else
9656                 count -= first_count;
9657
9658               /* If COUNT is positive, the new shift is usually CODE,
9659                  except for the two exceptions below, in which case it is
9660                  FIRST_CODE.  If the count is negative, FIRST_CODE should
9661                  always be used  */
9662               if (count > 0
9663                   && ((first_code == ROTATE && code == ASHIFT)
9664                       || (first_code == ASHIFTRT && code == LSHIFTRT)))
9665                 code = first_code;
9666               else if (count < 0)
9667                 code = first_code, count = -count;
9668
9669               varop = XEXP (varop, 0);
9670               continue;
9671             }
9672
9673           /* If we have (A << B << C) for any shift, we can convert this to
9674              (A << C << B).  This wins if A is a constant.  Only try this if
9675              B is not a constant.  */
9676
9677           else if (GET_CODE (varop) == code
9678                    && CONST_INT_P (XEXP (varop, 0))
9679                    && !CONST_INT_P (XEXP (varop, 1)))
9680             {
9681               rtx new_rtx = simplify_const_binary_operation (code, mode,
9682                                                          XEXP (varop, 0),
9683                                                          GEN_INT (count));
9684               varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
9685               count = 0;
9686               continue;
9687             }
9688           break;
9689
9690         case NOT:
9691           if (VECTOR_MODE_P (mode))
9692             break;
9693
9694           /* Make this fit the case below.  */
9695           varop = gen_rtx_XOR (mode, XEXP (varop, 0),
9696                                GEN_INT (GET_MODE_MASK (mode)));
9697           continue;
9698
9699         case IOR:
9700         case AND:
9701         case XOR:
9702           /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9703              with C the size of VAROP - 1 and the shift is logical if
9704              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9705              we have an (le X 0) operation.   If we have an arithmetic shift
9706              and STORE_FLAG_VALUE is 1 or we have a logical shift with
9707              STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
9708
9709           if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
9710               && XEXP (XEXP (varop, 0), 1) == constm1_rtx
9711               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9712               && (code == LSHIFTRT || code == ASHIFTRT)
9713               && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9714               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9715             {
9716               count = 0;
9717               varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
9718                                   const0_rtx);
9719
9720               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9721                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9722
9723               continue;
9724             }
9725
9726           /* If we have (shift (logical)), move the logical to the outside
9727              to allow it to possibly combine with another logical and the
9728              shift to combine with another shift.  This also canonicalizes to
9729              what a ZERO_EXTRACT looks like.  Also, some machines have
9730              (and (shift)) insns.  */
9731
9732           if (CONST_INT_P (XEXP (varop, 1))
9733               /* We can't do this if we have (ashiftrt (xor))  and the
9734                  constant has its sign bit set in shift_mode.  */
9735               && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9736                    && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9737                                               shift_mode))
9738               && (new_rtx = simplify_const_binary_operation (code, result_mode,
9739                                                          XEXP (varop, 1),
9740                                                          GEN_INT (count))) != 0
9741               && CONST_INT_P (new_rtx)
9742               && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9743                                   INTVAL (new_rtx), result_mode, &complement_p))
9744             {
9745               varop = XEXP (varop, 0);
9746               continue;
9747             }
9748
9749           /* If we can't do that, try to simplify the shift in each arm of the
9750              logical expression, make a new logical expression, and apply
9751              the inverse distributive law.  This also can't be done
9752              for some (ashiftrt (xor)).  */
9753           if (CONST_INT_P (XEXP (varop, 1))
9754              && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9755                   && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9756                                              shift_mode)))
9757             {
9758               rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9759                                               XEXP (varop, 0), count);
9760               rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9761                                               XEXP (varop, 1), count);
9762
9763               varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
9764                                            lhs, rhs);
9765               varop = apply_distributive_law (varop);
9766
9767               count = 0;
9768               continue;
9769             }
9770           break;
9771
9772         case EQ:
9773           /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9774              says that the sign bit can be tested, FOO has mode MODE, C is
9775              GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9776              that may be nonzero.  */
9777           if (code == LSHIFTRT
9778               && XEXP (varop, 1) == const0_rtx
9779               && GET_MODE (XEXP (varop, 0)) == result_mode
9780               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9781               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9782               && STORE_FLAG_VALUE == -1
9783               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9784               && merge_outer_ops (&outer_op, &outer_const, XOR,
9785                                   (HOST_WIDE_INT) 1, result_mode,
9786                                   &complement_p))
9787             {
9788               varop = XEXP (varop, 0);
9789               count = 0;
9790               continue;
9791             }
9792           break;
9793
9794         case NEG:
9795           /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9796              than the number of bits in the mode is equivalent to A.  */
9797           if (code == LSHIFTRT
9798               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9799               && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9800             {
9801               varop = XEXP (varop, 0);
9802               count = 0;
9803               continue;
9804             }
9805
9806           /* NEG commutes with ASHIFT since it is multiplication.  Move the
9807              NEG outside to allow shifts to combine.  */
9808           if (code == ASHIFT
9809               && merge_outer_ops (&outer_op, &outer_const, NEG,
9810                                   (HOST_WIDE_INT) 0, result_mode,
9811                                   &complement_p))
9812             {
9813               varop = XEXP (varop, 0);
9814               continue;
9815             }
9816           break;
9817
9818         case PLUS:
9819           /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9820              is one less than the number of bits in the mode is
9821              equivalent to (xor A 1).  */
9822           if (code == LSHIFTRT
9823               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9824               && XEXP (varop, 1) == constm1_rtx
9825               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9826               && merge_outer_ops (&outer_op, &outer_const, XOR,
9827                                   (HOST_WIDE_INT) 1, result_mode,
9828                                   &complement_p))
9829             {
9830               count = 0;
9831               varop = XEXP (varop, 0);
9832               continue;
9833             }
9834
9835           /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9836              that might be nonzero in BAR are those being shifted out and those
9837              bits are known zero in FOO, we can replace the PLUS with FOO.
9838              Similarly in the other operand order.  This code occurs when
9839              we are computing the size of a variable-size array.  */
9840
9841           if ((code == ASHIFTRT || code == LSHIFTRT)
9842               && count < HOST_BITS_PER_WIDE_INT
9843               && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9844               && (nonzero_bits (XEXP (varop, 1), result_mode)
9845                   & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9846             {
9847               varop = XEXP (varop, 0);
9848               continue;
9849             }
9850           else if ((code == ASHIFTRT || code == LSHIFTRT)
9851                    && count < HOST_BITS_PER_WIDE_INT
9852                    && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9853                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9854                             >> count)
9855                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9856                             & nonzero_bits (XEXP (varop, 1),
9857                                                  result_mode)))
9858             {
9859               varop = XEXP (varop, 1);
9860               continue;
9861             }
9862
9863           /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
9864           if (code == ASHIFT
9865               && CONST_INT_P (XEXP (varop, 1))
9866               && (new_rtx = simplify_const_binary_operation (ASHIFT, result_mode,
9867                                                          XEXP (varop, 1),
9868                                                          GEN_INT (count))) != 0
9869               && CONST_INT_P (new_rtx)
9870               && merge_outer_ops (&outer_op, &outer_const, PLUS,
9871                                   INTVAL (new_rtx), result_mode, &complement_p))
9872             {
9873               varop = XEXP (varop, 0);
9874               continue;
9875             }
9876
9877           /* Check for 'PLUS signbit', which is the canonical form of 'XOR
9878              signbit', and attempt to change the PLUS to an XOR and move it to
9879              the outer operation as is done above in the AND/IOR/XOR case
9880              leg for shift(logical). See details in logical handling above
9881              for reasoning in doing so.  */
9882           if (code == LSHIFTRT
9883               && CONST_INT_P (XEXP (varop, 1))
9884               && mode_signbit_p (result_mode, XEXP (varop, 1))
9885               && (new_rtx = simplify_const_binary_operation (code, result_mode,
9886                                                          XEXP (varop, 1),
9887                                                          GEN_INT (count))) != 0
9888               && CONST_INT_P (new_rtx)
9889               && merge_outer_ops (&outer_op, &outer_const, XOR,
9890                                   INTVAL (new_rtx), result_mode, &complement_p))
9891             {
9892               varop = XEXP (varop, 0);
9893               continue;
9894             }
9895
9896           break;
9897
9898         case MINUS:
9899           /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9900              with C the size of VAROP - 1 and the shift is logical if
9901              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9902              we have a (gt X 0) operation.  If the shift is arithmetic with
9903              STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9904              we have a (neg (gt X 0)) operation.  */
9905
9906           if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9907               && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9908               && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9909               && (code == LSHIFTRT || code == ASHIFTRT)
9910               && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
9911               && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
9912               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9913             {
9914               count = 0;
9915               varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9916                                   const0_rtx);
9917
9918               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9919                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9920
9921               continue;
9922             }
9923           break;
9924
9925         case TRUNCATE:
9926           /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9927              if the truncate does not affect the value.  */
9928           if (code == LSHIFTRT
9929               && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9930               && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
9931               && (INTVAL (XEXP (XEXP (varop, 0), 1))
9932                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9933                       - GET_MODE_BITSIZE (GET_MODE (varop)))))
9934             {
9935               rtx varop_inner = XEXP (varop, 0);
9936
9937               varop_inner
9938                 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9939                                     XEXP (varop_inner, 0),
9940                                     GEN_INT
9941                                     (count + INTVAL (XEXP (varop_inner, 1))));
9942               varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9943               count = 0;
9944               continue;
9945             }
9946           break;
9947
9948         default:
9949           break;
9950         }
9951
9952       break;
9953     }
9954
9955   shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
9956                                      outer_op, outer_const);
9957
9958   /* We have now finished analyzing the shift.  The result should be
9959      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
9960      OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
9961      to the result of the shift.  OUTER_CONST is the relevant constant,
9962      but we must turn off all bits turned off in the shift.  */
9963
9964   if (outer_op == UNKNOWN
9965       && orig_code == code && orig_count == count
9966       && varop == orig_varop
9967       && shift_mode == GET_MODE (varop))
9968     return NULL_RTX;
9969
9970   /* Make a SUBREG if necessary.  If we can't make it, fail.  */
9971   varop = gen_lowpart (shift_mode, varop);
9972   if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9973     return NULL_RTX;
9974
9975   /* If we have an outer operation and we just made a shift, it is
9976      possible that we could have simplified the shift were it not
9977      for the outer operation.  So try to do the simplification
9978      recursively.  */
9979
9980   if (outer_op != UNKNOWN)
9981     x = simplify_shift_const_1 (code, shift_mode, varop, count);
9982   else
9983     x = NULL_RTX;
9984
9985   if (x == NULL_RTX)
9986     x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
9987
9988   /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9989      turn off all the bits that the shift would have turned off.  */
9990   if (orig_code == LSHIFTRT && result_mode != shift_mode)
9991     x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9992                                 GET_MODE_MASK (result_mode) >> orig_count);
9993
9994   /* Do the remainder of the processing in RESULT_MODE.  */
9995   x = gen_lowpart_or_truncate (result_mode, x);
9996
9997   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9998      operation.  */
9999   if (complement_p)
10000     x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10001
10002   if (outer_op != UNKNOWN)
10003     {
10004       if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10005           && GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
10006         outer_const = trunc_int_for_mode (outer_const, result_mode);
10007
10008       if (outer_op == AND)
10009         x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10010       else if (outer_op == SET)
10011         {
10012           /* This means that we have determined that the result is
10013              equivalent to a constant.  This should be rare.  */
10014           if (!side_effects_p (x))
10015             x = GEN_INT (outer_const);
10016         }
10017       else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10018         x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10019       else
10020         x = simplify_gen_binary (outer_op, result_mode, x,
10021                                  GEN_INT (outer_const));
10022     }
10023
10024   return x;
10025 }
10026
10027 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
10028    The result of the shift is RESULT_MODE.  If we cannot simplify it,
10029    return X or, if it is NULL, synthesize the expression with
10030    simplify_gen_binary.  Otherwise, return a simplified value.
10031
10032    The shift is normally computed in the widest mode we find in VAROP, as
10033    long as it isn't a different number of words than RESULT_MODE.  Exceptions
10034    are ASHIFTRT and ROTATE, which are always done in their original mode.  */
10035
10036 static rtx
10037 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
10038                       rtx varop, int count)
10039 {
10040   rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10041   if (tem)
10042     return tem;
10043
10044   if (!x)
10045     x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10046   if (GET_MODE (x) != result_mode)
10047     x = gen_lowpart (result_mode, x);
10048   return x;
10049 }
10050
10051 \f
10052 /* Like recog, but we receive the address of a pointer to a new pattern.
10053    We try to match the rtx that the pointer points to.
10054    If that fails, we may try to modify or replace the pattern,
10055    storing the replacement into the same pointer object.
10056
10057    Modifications include deletion or addition of CLOBBERs.
10058
10059    PNOTES is a pointer to a location where any REG_UNUSED notes added for
10060    the CLOBBERs are placed.
10061
10062    The value is the final insn code from the pattern ultimately matched,
10063    or -1.  */
10064
10065 static int
10066 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
10067 {
10068   rtx pat = *pnewpat;
10069   int insn_code_number;
10070   int num_clobbers_to_add = 0;
10071   int i;
10072   rtx notes = 0;
10073   rtx old_notes, old_pat;
10074
10075   /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10076      we use to indicate that something didn't match.  If we find such a
10077      thing, force rejection.  */
10078   if (GET_CODE (pat) == PARALLEL)
10079     for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10080       if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10081           && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10082         return -1;
10083
10084   old_pat = PATTERN (insn);
10085   old_notes = REG_NOTES (insn);
10086   PATTERN (insn) = pat;
10087   REG_NOTES (insn) = 0;
10088
10089   insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10090   if (dump_file && (dump_flags & TDF_DETAILS))
10091     {
10092       if (insn_code_number < 0)
10093         fputs ("Failed to match this instruction:\n", dump_file);
10094       else
10095         fputs ("Successfully matched this instruction:\n", dump_file);
10096       print_rtl_single (dump_file, pat);
10097     }
10098
10099   /* If it isn't, there is the possibility that we previously had an insn
10100      that clobbered some register as a side effect, but the combined
10101      insn doesn't need to do that.  So try once more without the clobbers
10102      unless this represents an ASM insn.  */
10103
10104   if (insn_code_number < 0 && ! check_asm_operands (pat)
10105       && GET_CODE (pat) == PARALLEL)
10106     {
10107       int pos;
10108
10109       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10110         if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10111           {
10112             if (i != pos)
10113               SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10114             pos++;
10115           }
10116
10117       SUBST_INT (XVECLEN (pat, 0), pos);
10118
10119       if (pos == 1)
10120         pat = XVECEXP (pat, 0, 0);
10121
10122       PATTERN (insn) = pat;
10123       insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10124       if (dump_file && (dump_flags & TDF_DETAILS))
10125         {
10126           if (insn_code_number < 0)
10127             fputs ("Failed to match this instruction:\n", dump_file);
10128           else
10129             fputs ("Successfully matched this instruction:\n", dump_file);
10130           print_rtl_single (dump_file, pat);
10131         }
10132     }
10133   PATTERN (insn) = old_pat;
10134   REG_NOTES (insn) = old_notes;
10135
10136   /* Recognize all noop sets, these will be killed by followup pass.  */
10137   if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10138     insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10139
10140   /* If we had any clobbers to add, make a new pattern than contains
10141      them.  Then check to make sure that all of them are dead.  */
10142   if (num_clobbers_to_add)
10143     {
10144       rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10145                                      rtvec_alloc (GET_CODE (pat) == PARALLEL
10146                                                   ? (XVECLEN (pat, 0)
10147                                                      + num_clobbers_to_add)
10148                                                   : num_clobbers_to_add + 1));
10149
10150       if (GET_CODE (pat) == PARALLEL)
10151         for (i = 0; i < XVECLEN (pat, 0); i++)
10152           XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10153       else
10154         XVECEXP (newpat, 0, 0) = pat;
10155
10156       add_clobbers (newpat, insn_code_number);
10157
10158       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10159            i < XVECLEN (newpat, 0); i++)
10160         {
10161           if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10162               && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10163             return -1;
10164           if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10165             {
10166               gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10167               notes = alloc_reg_note (REG_UNUSED,
10168                                       XEXP (XVECEXP (newpat, 0, i), 0), notes);
10169             }
10170         }
10171       pat = newpat;
10172     }
10173
10174   *pnewpat = pat;
10175   *pnotes = notes;
10176
10177   return insn_code_number;
10178 }
10179 \f
10180 /* Like gen_lowpart_general but for use by combine.  In combine it
10181    is not possible to create any new pseudoregs.  However, it is
10182    safe to create invalid memory addresses, because combine will
10183    try to recognize them and all they will do is make the combine
10184    attempt fail.
10185
10186    If for some reason this cannot do its job, an rtx
10187    (clobber (const_int 0)) is returned.
10188    An insn containing that will not be recognized.  */
10189
10190 static rtx
10191 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
10192 {
10193   enum machine_mode imode = GET_MODE (x);
10194   unsigned int osize = GET_MODE_SIZE (omode);
10195   unsigned int isize = GET_MODE_SIZE (imode);
10196   rtx result;
10197
10198   if (omode == imode)
10199     return x;
10200
10201   /* Return identity if this is a CONST or symbolic reference.  */
10202   if (omode == Pmode
10203       && (GET_CODE (x) == CONST
10204           || GET_CODE (x) == SYMBOL_REF
10205           || GET_CODE (x) == LABEL_REF))
10206     return x;
10207
10208   /* We can only support MODE being wider than a word if X is a
10209      constant integer or has a mode the same size.  */
10210   if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
10211       && ! ((imode == VOIDmode
10212              && (CONST_INT_P (x)
10213                  || GET_CODE (x) == CONST_DOUBLE))
10214             || isize == osize))
10215     goto fail;
10216
10217   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
10218      won't know what to do.  So we will strip off the SUBREG here and
10219      process normally.  */
10220   if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
10221     {
10222       x = SUBREG_REG (x);
10223
10224       /* For use in case we fall down into the address adjustments
10225          further below, we need to adjust the known mode and size of
10226          x; imode and isize, since we just adjusted x.  */
10227       imode = GET_MODE (x);
10228
10229       if (imode == omode)
10230         return x;
10231
10232       isize = GET_MODE_SIZE (imode);
10233     }
10234
10235   result = gen_lowpart_common (omode, x);
10236
10237   if (result)
10238     return result;
10239
10240   if (MEM_P (x))
10241     {
10242       int offset = 0;
10243
10244       /* Refuse to work on a volatile memory ref or one with a mode-dependent
10245          address.  */
10246       if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
10247         goto fail;
10248
10249       /* If we want to refer to something bigger than the original memref,
10250          generate a paradoxical subreg instead.  That will force a reload
10251          of the original memref X.  */
10252       if (isize < osize)
10253         return gen_rtx_SUBREG (omode, x, 0);
10254
10255       if (WORDS_BIG_ENDIAN)
10256         offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
10257
10258       /* Adjust the address so that the address-after-the-data is
10259          unchanged.  */
10260       if (BYTES_BIG_ENDIAN)
10261         offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
10262
10263       return adjust_address_nv (x, omode, offset);
10264     }
10265
10266   /* If X is a comparison operator, rewrite it in a new mode.  This
10267      probably won't match, but may allow further simplifications.  */
10268   else if (COMPARISON_P (x))
10269     return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
10270
10271   /* If we couldn't simplify X any other way, just enclose it in a
10272      SUBREG.  Normally, this SUBREG won't match, but some patterns may
10273      include an explicit SUBREG or we may simplify it further in combine.  */
10274   else
10275     {
10276       int offset = 0;
10277       rtx res;
10278
10279       offset = subreg_lowpart_offset (omode, imode);
10280       if (imode == VOIDmode)
10281         {
10282           imode = int_mode_for_mode (omode);
10283           x = gen_lowpart_common (imode, x);
10284           if (x == NULL)
10285             goto fail;
10286         }
10287       res = simplify_gen_subreg (omode, x, imode, offset);
10288       if (res)
10289         return res;
10290     }
10291
10292  fail:
10293   return gen_rtx_CLOBBER (omode, const0_rtx);
10294 }
10295 \f
10296 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
10297    comparison code that will be tested.
10298
10299    The result is a possibly different comparison code to use.  *POP0 and
10300    *POP1 may be updated.
10301
10302    It is possible that we might detect that a comparison is either always
10303    true or always false.  However, we do not perform general constant
10304    folding in combine, so this knowledge isn't useful.  Such tautologies
10305    should have been detected earlier.  Hence we ignore all such cases.  */
10306
10307 static enum rtx_code
10308 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
10309 {
10310   rtx op0 = *pop0;
10311   rtx op1 = *pop1;
10312   rtx tem, tem1;
10313   int i;
10314   enum machine_mode mode, tmode;
10315
10316   /* Try a few ways of applying the same transformation to both operands.  */
10317   while (1)
10318     {
10319 #ifndef WORD_REGISTER_OPERATIONS
10320       /* The test below this one won't handle SIGN_EXTENDs on these machines,
10321          so check specially.  */
10322       if (code != GTU && code != GEU && code != LTU && code != LEU
10323           && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
10324           && GET_CODE (XEXP (op0, 0)) == ASHIFT
10325           && GET_CODE (XEXP (op1, 0)) == ASHIFT
10326           && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
10327           && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
10328           && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
10329               == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
10330           && CONST_INT_P (XEXP (op0, 1))
10331           && XEXP (op0, 1) == XEXP (op1, 1)
10332           && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10333           && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
10334           && (INTVAL (XEXP (op0, 1))
10335               == (GET_MODE_BITSIZE (GET_MODE (op0))
10336                   - (GET_MODE_BITSIZE
10337                      (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
10338         {
10339           op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
10340           op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
10341         }
10342 #endif
10343
10344       /* If both operands are the same constant shift, see if we can ignore the
10345          shift.  We can if the shift is a rotate or if the bits shifted out of
10346          this shift are known to be zero for both inputs and if the type of
10347          comparison is compatible with the shift.  */
10348       if (GET_CODE (op0) == GET_CODE (op1)
10349           && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10350           && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
10351               || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
10352                   && (code != GT && code != LT && code != GE && code != LE))
10353               || (GET_CODE (op0) == ASHIFTRT
10354                   && (code != GTU && code != LTU
10355                       && code != GEU && code != LEU)))
10356           && CONST_INT_P (XEXP (op0, 1))
10357           && INTVAL (XEXP (op0, 1)) >= 0
10358           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10359           && XEXP (op0, 1) == XEXP (op1, 1))
10360         {
10361           enum machine_mode mode = GET_MODE (op0);
10362           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10363           int shift_count = INTVAL (XEXP (op0, 1));
10364
10365           if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
10366             mask &= (mask >> shift_count) << shift_count;
10367           else if (GET_CODE (op0) == ASHIFT)
10368             mask = (mask & (mask << shift_count)) >> shift_count;
10369
10370           if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
10371               && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
10372             op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
10373           else
10374             break;
10375         }
10376
10377       /* If both operands are AND's of a paradoxical SUBREG by constant, the
10378          SUBREGs are of the same mode, and, in both cases, the AND would
10379          be redundant if the comparison was done in the narrower mode,
10380          do the comparison in the narrower mode (e.g., we are AND'ing with 1
10381          and the operand's possibly nonzero bits are 0xffffff01; in that case
10382          if we only care about QImode, we don't need the AND).  This case
10383          occurs if the output mode of an scc insn is not SImode and
10384          STORE_FLAG_VALUE == 1 (e.g., the 386).
10385
10386          Similarly, check for a case where the AND's are ZERO_EXTEND
10387          operations from some narrower mode even though a SUBREG is not
10388          present.  */
10389
10390       else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
10391                && CONST_INT_P (XEXP (op0, 1))
10392                && CONST_INT_P (XEXP (op1, 1)))
10393         {
10394           rtx inner_op0 = XEXP (op0, 0);
10395           rtx inner_op1 = XEXP (op1, 0);
10396           HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
10397           HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
10398           int changed = 0;
10399
10400           if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
10401               && (GET_MODE_SIZE (GET_MODE (inner_op0))
10402                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
10403               && (GET_MODE (SUBREG_REG (inner_op0))
10404                   == GET_MODE (SUBREG_REG (inner_op1)))
10405               && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
10406                   <= HOST_BITS_PER_WIDE_INT)
10407               && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
10408                                              GET_MODE (SUBREG_REG (inner_op0)))))
10409               && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
10410                                              GET_MODE (SUBREG_REG (inner_op1))))))
10411             {
10412               op0 = SUBREG_REG (inner_op0);
10413               op1 = SUBREG_REG (inner_op1);
10414
10415               /* The resulting comparison is always unsigned since we masked
10416                  off the original sign bit.  */
10417               code = unsigned_condition (code);
10418
10419               changed = 1;
10420             }
10421
10422           else if (c0 == c1)
10423             for (tmode = GET_CLASS_NARROWEST_MODE
10424                  (GET_MODE_CLASS (GET_MODE (op0)));
10425                  tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10426               if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10427                 {
10428                   op0 = gen_lowpart (tmode, inner_op0);
10429                   op1 = gen_lowpart (tmode, inner_op1);
10430                   code = unsigned_condition (code);
10431                   changed = 1;
10432                   break;
10433                 }
10434
10435           if (! changed)
10436             break;
10437         }
10438
10439       /* If both operands are NOT, we can strip off the outer operation
10440          and adjust the comparison code for swapped operands; similarly for
10441          NEG, except that this must be an equality comparison.  */
10442       else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10443                || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10444                    && (code == EQ || code == NE)))
10445         op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10446
10447       else
10448         break;
10449     }
10450
10451   /* If the first operand is a constant, swap the operands and adjust the
10452      comparison code appropriately, but don't do this if the second operand
10453      is already a constant integer.  */
10454   if (swap_commutative_operands_p (op0, op1))
10455     {
10456       tem = op0, op0 = op1, op1 = tem;
10457       code = swap_condition (code);
10458     }
10459
10460   /* We now enter a loop during which we will try to simplify the comparison.
10461      For the most part, we only are concerned with comparisons with zero,
10462      but some things may really be comparisons with zero but not start
10463      out looking that way.  */
10464
10465   while (CONST_INT_P (op1))
10466     {
10467       enum machine_mode mode = GET_MODE (op0);
10468       unsigned int mode_width = GET_MODE_BITSIZE (mode);
10469       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10470       int equality_comparison_p;
10471       int sign_bit_comparison_p;
10472       int unsigned_comparison_p;
10473       HOST_WIDE_INT const_op;
10474
10475       /* We only want to handle integral modes.  This catches VOIDmode,
10476          CCmode, and the floating-point modes.  An exception is that we
10477          can handle VOIDmode if OP0 is a COMPARE or a comparison
10478          operation.  */
10479
10480       if (GET_MODE_CLASS (mode) != MODE_INT
10481           && ! (mode == VOIDmode
10482                 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
10483         break;
10484
10485       /* Get the constant we are comparing against and turn off all bits
10486          not on in our mode.  */
10487       const_op = INTVAL (op1);
10488       if (mode != VOIDmode)
10489         const_op = trunc_int_for_mode (const_op, mode);
10490       op1 = GEN_INT (const_op);
10491
10492       /* If we are comparing against a constant power of two and the value
10493          being compared can only have that single bit nonzero (e.g., it was
10494          `and'ed with that bit), we can replace this with a comparison
10495          with zero.  */
10496       if (const_op
10497           && (code == EQ || code == NE || code == GE || code == GEU
10498               || code == LT || code == LTU)
10499           && mode_width <= HOST_BITS_PER_WIDE_INT
10500           && exact_log2 (const_op) >= 0
10501           && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10502         {
10503           code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10504           op1 = const0_rtx, const_op = 0;
10505         }
10506
10507       /* Similarly, if we are comparing a value known to be either -1 or
10508          0 with -1, change it to the opposite comparison against zero.  */
10509
10510       if (const_op == -1
10511           && (code == EQ || code == NE || code == GT || code == LE
10512               || code == GEU || code == LTU)
10513           && num_sign_bit_copies (op0, mode) == mode_width)
10514         {
10515           code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10516           op1 = const0_rtx, const_op = 0;
10517         }
10518
10519       /* Do some canonicalizations based on the comparison code.  We prefer
10520          comparisons against zero and then prefer equality comparisons.
10521          If we can reduce the size of a constant, we will do that too.  */
10522
10523       switch (code)
10524         {
10525         case LT:
10526           /* < C is equivalent to <= (C - 1) */
10527           if (const_op > 0)
10528             {
10529               const_op -= 1;
10530               op1 = GEN_INT (const_op);
10531               code = LE;
10532               /* ... fall through to LE case below.  */
10533             }
10534           else
10535             break;
10536
10537         case LE:
10538           /* <= C is equivalent to < (C + 1); we do this for C < 0  */
10539           if (const_op < 0)
10540             {
10541               const_op += 1;
10542               op1 = GEN_INT (const_op);
10543               code = LT;
10544             }
10545
10546           /* If we are doing a <= 0 comparison on a value known to have
10547              a zero sign bit, we can replace this with == 0.  */
10548           else if (const_op == 0
10549                    && mode_width <= HOST_BITS_PER_WIDE_INT
10550                    && (nonzero_bits (op0, mode)
10551                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10552             code = EQ;
10553           break;
10554
10555         case GE:
10556           /* >= C is equivalent to > (C - 1).  */
10557           if (const_op > 0)
10558             {
10559               const_op -= 1;
10560               op1 = GEN_INT (const_op);
10561               code = GT;
10562               /* ... fall through to GT below.  */
10563             }
10564           else
10565             break;
10566
10567         case GT:
10568           /* > C is equivalent to >= (C + 1); we do this for C < 0.  */
10569           if (const_op < 0)
10570             {
10571               const_op += 1;
10572               op1 = GEN_INT (const_op);
10573               code = GE;
10574             }
10575
10576           /* If we are doing a > 0 comparison on a value known to have
10577              a zero sign bit, we can replace this with != 0.  */
10578           else if (const_op == 0
10579                    && mode_width <= HOST_BITS_PER_WIDE_INT
10580                    && (nonzero_bits (op0, mode)
10581                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10582             code = NE;
10583           break;
10584
10585         case LTU:
10586           /* < C is equivalent to <= (C - 1).  */
10587           if (const_op > 0)
10588             {
10589               const_op -= 1;
10590               op1 = GEN_INT (const_op);
10591               code = LEU;
10592               /* ... fall through ...  */
10593             }
10594
10595           /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
10596           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10597                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10598             {
10599               const_op = 0, op1 = const0_rtx;
10600               code = GE;
10601               break;
10602             }
10603           else
10604             break;
10605
10606         case LEU:
10607           /* unsigned <= 0 is equivalent to == 0 */
10608           if (const_op == 0)
10609             code = EQ;
10610
10611           /* (unsigned) <= 0x7fffffff is equivalent to >= 0.  */
10612           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10613                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10614             {
10615               const_op = 0, op1 = const0_rtx;
10616               code = GE;
10617             }
10618           break;
10619
10620         case GEU:
10621           /* >= C is equivalent to > (C - 1).  */
10622           if (const_op > 1)
10623             {
10624               const_op -= 1;
10625               op1 = GEN_INT (const_op);
10626               code = GTU;
10627               /* ... fall through ...  */
10628             }
10629
10630           /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
10631           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10632                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10633             {
10634               const_op = 0, op1 = const0_rtx;
10635               code = LT;
10636               break;
10637             }
10638           else
10639             break;
10640
10641         case GTU:
10642           /* unsigned > 0 is equivalent to != 0 */
10643           if (const_op == 0)
10644             code = NE;
10645
10646           /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
10647           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10648                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10649             {
10650               const_op = 0, op1 = const0_rtx;
10651               code = LT;
10652             }
10653           break;
10654
10655         default:
10656           break;
10657         }
10658
10659       /* Compute some predicates to simplify code below.  */
10660
10661       equality_comparison_p = (code == EQ || code == NE);
10662       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
10663       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
10664                                || code == GEU);
10665
10666       /* If this is a sign bit comparison and we can do arithmetic in
10667          MODE, say that we will only be needing the sign bit of OP0.  */
10668       if (sign_bit_comparison_p
10669           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10670         op0 = force_to_mode (op0, mode,
10671                              ((HOST_WIDE_INT) 1
10672                               << (GET_MODE_BITSIZE (mode) - 1)),
10673                              0);
10674
10675       /* Now try cases based on the opcode of OP0.  If none of the cases
10676          does a "continue", we exit this loop immediately after the
10677          switch.  */
10678
10679       switch (GET_CODE (op0))
10680         {
10681         case ZERO_EXTRACT:
10682           /* If we are extracting a single bit from a variable position in
10683              a constant that has only a single bit set and are comparing it
10684              with zero, we can convert this into an equality comparison
10685              between the position and the location of the single bit.  */
10686           /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
10687              have already reduced the shift count modulo the word size.  */
10688           if (!SHIFT_COUNT_TRUNCATED
10689               && CONST_INT_P (XEXP (op0, 0))
10690               && XEXP (op0, 1) == const1_rtx
10691               && equality_comparison_p && const_op == 0
10692               && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
10693             {
10694               if (BITS_BIG_ENDIAN)
10695                 {
10696                   enum machine_mode new_mode
10697                     = mode_for_extraction (EP_extzv, 1);
10698                   if (new_mode == MAX_MACHINE_MODE)
10699                     i = BITS_PER_WORD - 1 - i;
10700                   else
10701                     {
10702                       mode = new_mode;
10703                       i = (GET_MODE_BITSIZE (mode) - 1 - i);
10704                     }
10705                 }
10706
10707               op0 = XEXP (op0, 2);
10708               op1 = GEN_INT (i);
10709               const_op = i;
10710
10711               /* Result is nonzero iff shift count is equal to I.  */
10712               code = reverse_condition (code);
10713               continue;
10714             }
10715
10716           /* ... fall through ...  */
10717
10718         case SIGN_EXTRACT:
10719           tem = expand_compound_operation (op0);
10720           if (tem != op0)
10721             {
10722               op0 = tem;
10723               continue;
10724             }
10725           break;
10726
10727         case NOT:
10728           /* If testing for equality, we can take the NOT of the constant.  */
10729           if (equality_comparison_p
10730               && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10731             {
10732               op0 = XEXP (op0, 0);
10733               op1 = tem;
10734               continue;
10735             }
10736
10737           /* If just looking at the sign bit, reverse the sense of the
10738              comparison.  */
10739           if (sign_bit_comparison_p)
10740             {
10741               op0 = XEXP (op0, 0);
10742               code = (code == GE ? LT : GE);
10743               continue;
10744             }
10745           break;
10746
10747         case NEG:
10748           /* If testing for equality, we can take the NEG of the constant.  */
10749           if (equality_comparison_p
10750               && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10751             {
10752               op0 = XEXP (op0, 0);
10753               op1 = tem;
10754               continue;
10755             }
10756
10757           /* The remaining cases only apply to comparisons with zero.  */
10758           if (const_op != 0)
10759             break;
10760
10761           /* When X is ABS or is known positive,
10762              (neg X) is < 0 if and only if X != 0.  */
10763
10764           if (sign_bit_comparison_p
10765               && (GET_CODE (XEXP (op0, 0)) == ABS
10766                   || (mode_width <= HOST_BITS_PER_WIDE_INT
10767                       && (nonzero_bits (XEXP (op0, 0), mode)
10768                           & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10769             {
10770               op0 = XEXP (op0, 0);
10771               code = (code == LT ? NE : EQ);
10772               continue;
10773             }
10774
10775           /* If we have NEG of something whose two high-order bits are the
10776              same, we know that "(-a) < 0" is equivalent to "a > 0".  */
10777           if (num_sign_bit_copies (op0, mode) >= 2)
10778             {
10779               op0 = XEXP (op0, 0);
10780               code = swap_condition (code);
10781               continue;
10782             }
10783           break;
10784
10785         case ROTATE:
10786           /* If we are testing equality and our count is a constant, we
10787              can perform the inverse operation on our RHS.  */
10788           if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
10789               && (tem = simplify_binary_operation (ROTATERT, mode,
10790                                                    op1, XEXP (op0, 1))) != 0)
10791             {
10792               op0 = XEXP (op0, 0);
10793               op1 = tem;
10794               continue;
10795             }
10796
10797           /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10798              a particular bit.  Convert it to an AND of a constant of that
10799              bit.  This will be converted into a ZERO_EXTRACT.  */
10800           if (const_op == 0 && sign_bit_comparison_p
10801               && CONST_INT_P (XEXP (op0, 1))
10802               && mode_width <= HOST_BITS_PER_WIDE_INT)
10803             {
10804               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10805                                             ((HOST_WIDE_INT) 1
10806                                              << (mode_width - 1
10807                                                  - INTVAL (XEXP (op0, 1)))));
10808               code = (code == LT ? NE : EQ);
10809               continue;
10810             }
10811
10812           /* Fall through.  */
10813
10814         case ABS:
10815           /* ABS is ignorable inside an equality comparison with zero.  */
10816           if (const_op == 0 && equality_comparison_p)
10817             {
10818               op0 = XEXP (op0, 0);
10819               continue;
10820             }
10821           break;
10822
10823         case SIGN_EXTEND:
10824           /* Can simplify (compare (zero/sign_extend FOO) CONST) to
10825              (compare FOO CONST) if CONST fits in FOO's mode and we
10826              are either testing inequality or have an unsigned
10827              comparison with ZERO_EXTEND or a signed comparison with
10828              SIGN_EXTEND.  But don't do it if we don't have a compare
10829              insn of the given mode, since we'd have to revert it
10830              later on, and then we wouldn't know whether to sign- or
10831              zero-extend.  */
10832           mode = GET_MODE (XEXP (op0, 0));
10833           if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10834               && ! unsigned_comparison_p
10835               && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10836               && ((unsigned HOST_WIDE_INT) const_op
10837                   < (((unsigned HOST_WIDE_INT) 1
10838                       << (GET_MODE_BITSIZE (mode) - 1))))
10839               && have_insn_for (COMPARE, mode))
10840             {
10841               op0 = XEXP (op0, 0);
10842               continue;
10843             }
10844           break;
10845
10846         case SUBREG:
10847           /* Check for the case where we are comparing A - C1 with C2, that is
10848
10849                (subreg:MODE (plus (A) (-C1))) op (C2)
10850
10851              with C1 a constant, and try to lift the SUBREG, i.e. to do the
10852              comparison in the wider mode.  One of the following two conditions
10853              must be true in order for this to be valid:
10854
10855                1. The mode extension results in the same bit pattern being added
10856                   on both sides and the comparison is equality or unsigned.  As
10857                   C2 has been truncated to fit in MODE, the pattern can only be
10858                   all 0s or all 1s.
10859
10860                2. The mode extension results in the sign bit being copied on
10861                   each side.
10862
10863              The difficulty here is that we have predicates for A but not for
10864              (A - C1) so we need to check that C1 is within proper bounds so
10865              as to perturbate A as little as possible.  */
10866
10867           if (mode_width <= HOST_BITS_PER_WIDE_INT
10868               && subreg_lowpart_p (op0)
10869               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
10870               && GET_CODE (SUBREG_REG (op0)) == PLUS
10871               && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
10872             {
10873               enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
10874               rtx a = XEXP (SUBREG_REG (op0), 0);
10875               HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
10876
10877               if ((c1 > 0
10878                    && (unsigned HOST_WIDE_INT) c1
10879                        < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
10880                    && (equality_comparison_p || unsigned_comparison_p)
10881                    /* (A - C1) zero-extends if it is positive and sign-extends
10882                       if it is negative, C2 both zero- and sign-extends.  */
10883                    && ((0 == (nonzero_bits (a, inner_mode)
10884                               & ~GET_MODE_MASK (mode))
10885                         && const_op >= 0)
10886                        /* (A - C1) sign-extends if it is positive and 1-extends
10887                           if it is negative, C2 both sign- and 1-extends.  */
10888                        || (num_sign_bit_copies (a, inner_mode)
10889                            > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10890                                              - mode_width)
10891                            && const_op < 0)))
10892                   || ((unsigned HOST_WIDE_INT) c1
10893                        < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
10894                       /* (A - C1) always sign-extends, like C2.  */
10895                       && num_sign_bit_copies (a, inner_mode)
10896                          > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10897                                            - (mode_width - 1))))
10898                 {
10899                   op0 = SUBREG_REG (op0);
10900                   continue;
10901                 }
10902             }
10903
10904           /* If the inner mode is narrower and we are extracting the low part,
10905              we can treat the SUBREG as if it were a ZERO_EXTEND.  */
10906           if (subreg_lowpart_p (op0)
10907               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10908             /* Fall through */ ;
10909           else
10910             break;
10911
10912           /* ... fall through ...  */
10913
10914         case ZERO_EXTEND:
10915           mode = GET_MODE (XEXP (op0, 0));
10916           if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10917               && (unsigned_comparison_p || equality_comparison_p)
10918               && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10919               && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
10920               && have_insn_for (COMPARE, mode))
10921             {
10922               op0 = XEXP (op0, 0);
10923               continue;
10924             }
10925           break;
10926
10927         case PLUS:
10928           /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
10929              this for equality comparisons due to pathological cases involving
10930              overflows.  */
10931           if (equality_comparison_p
10932               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10933                                                         op1, XEXP (op0, 1))))
10934             {
10935               op0 = XEXP (op0, 0);
10936               op1 = tem;
10937               continue;
10938             }
10939
10940           /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
10941           if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10942               && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10943             {
10944               op0 = XEXP (XEXP (op0, 0), 0);
10945               code = (code == LT ? EQ : NE);
10946               continue;
10947             }
10948           break;
10949
10950         case MINUS:
10951           /* We used to optimize signed comparisons against zero, but that
10952              was incorrect.  Unsigned comparisons against zero (GTU, LEU)
10953              arrive here as equality comparisons, or (GEU, LTU) are
10954              optimized away.  No need to special-case them.  */
10955
10956           /* (eq (minus A B) C) -> (eq A (plus B C)) or
10957              (eq B (minus A C)), whichever simplifies.  We can only do
10958              this for equality comparisons due to pathological cases involving
10959              overflows.  */
10960           if (equality_comparison_p
10961               && 0 != (tem = simplify_binary_operation (PLUS, mode,
10962                                                         XEXP (op0, 1), op1)))
10963             {
10964               op0 = XEXP (op0, 0);
10965               op1 = tem;
10966               continue;
10967             }
10968
10969           if (equality_comparison_p
10970               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10971                                                         XEXP (op0, 0), op1)))
10972             {
10973               op0 = XEXP (op0, 1);
10974               op1 = tem;
10975               continue;
10976             }
10977
10978           /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10979              of bits in X minus 1, is one iff X > 0.  */
10980           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10981               && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
10982               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10983                  == mode_width - 1
10984               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10985             {
10986               op0 = XEXP (op0, 1);
10987               code = (code == GE ? LE : GT);
10988               continue;
10989             }
10990           break;
10991
10992         case XOR:
10993           /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
10994              if C is zero or B is a constant.  */
10995           if (equality_comparison_p
10996               && 0 != (tem = simplify_binary_operation (XOR, mode,
10997                                                         XEXP (op0, 1), op1)))
10998             {
10999               op0 = XEXP (op0, 0);
11000               op1 = tem;
11001               continue;
11002             }
11003           break;
11004
11005         case EQ:  case NE:
11006         case UNEQ:  case LTGT:
11007         case LT:  case LTU:  case UNLT:  case LE:  case LEU:  case UNLE:
11008         case GT:  case GTU:  case UNGT:  case GE:  case GEU:  case UNGE:
11009         case UNORDERED: case ORDERED:
11010           /* We can't do anything if OP0 is a condition code value, rather
11011              than an actual data value.  */
11012           if (const_op != 0
11013               || CC0_P (XEXP (op0, 0))
11014               || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11015             break;
11016
11017           /* Get the two operands being compared.  */
11018           if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11019             tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11020           else
11021             tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11022
11023           /* Check for the cases where we simply want the result of the
11024              earlier test or the opposite of that result.  */
11025           if (code == NE || code == EQ
11026               || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
11027                   && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11028                   && (STORE_FLAG_VALUE
11029                       & (((HOST_WIDE_INT) 1
11030                           << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
11031                   && (code == LT || code == GE)))
11032             {
11033               enum rtx_code new_code;
11034               if (code == LT || code == NE)
11035                 new_code = GET_CODE (op0);
11036               else
11037                 new_code = reversed_comparison_code (op0, NULL);
11038
11039               if (new_code != UNKNOWN)
11040                 {
11041                   code = new_code;
11042                   op0 = tem;
11043                   op1 = tem1;
11044                   continue;
11045                 }
11046             }
11047           break;
11048
11049         case IOR:
11050           /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11051              iff X <= 0.  */
11052           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11053               && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11054               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11055             {
11056               op0 = XEXP (op0, 1);
11057               code = (code == GE ? GT : LE);
11058               continue;
11059             }
11060           break;
11061
11062         case AND:
11063           /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
11064              will be converted to a ZERO_EXTRACT later.  */
11065           if (const_op == 0 && equality_comparison_p
11066               && GET_CODE (XEXP (op0, 0)) == ASHIFT
11067               && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11068             {
11069               op0 = simplify_and_const_int
11070                 (NULL_RTX, mode, gen_rtx_LSHIFTRT (mode,
11071                                                    XEXP (op0, 1),
11072                                                    XEXP (XEXP (op0, 0), 1)),
11073                  (HOST_WIDE_INT) 1);
11074               continue;
11075             }
11076
11077           /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11078              zero and X is a comparison and C1 and C2 describe only bits set
11079              in STORE_FLAG_VALUE, we can compare with X.  */
11080           if (const_op == 0 && equality_comparison_p
11081               && mode_width <= HOST_BITS_PER_WIDE_INT
11082               && CONST_INT_P (XEXP (op0, 1))
11083               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11084               && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11085               && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11086               && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11087             {
11088               mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11089                       << INTVAL (XEXP (XEXP (op0, 0), 1)));
11090               if ((~STORE_FLAG_VALUE & mask) == 0
11091                   && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11092                       || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11093                           && COMPARISON_P (tem))))
11094                 {
11095                   op0 = XEXP (XEXP (op0, 0), 0);
11096                   continue;
11097                 }
11098             }
11099
11100           /* If we are doing an equality comparison of an AND of a bit equal
11101              to the sign bit, replace this with a LT or GE comparison of
11102              the underlying value.  */
11103           if (equality_comparison_p
11104               && const_op == 0
11105               && CONST_INT_P (XEXP (op0, 1))
11106               && mode_width <= HOST_BITS_PER_WIDE_INT
11107               && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11108                   == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11109             {
11110               op0 = XEXP (op0, 0);
11111               code = (code == EQ ? GE : LT);
11112               continue;
11113             }
11114
11115           /* If this AND operation is really a ZERO_EXTEND from a narrower
11116              mode, the constant fits within that mode, and this is either an
11117              equality or unsigned comparison, try to do this comparison in
11118              the narrower mode.
11119
11120              Note that in:
11121
11122              (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11123              -> (ne:DI (reg:SI 4) (const_int 0))
11124
11125              unless TRULY_NOOP_TRUNCATION allows it or the register is
11126              known to hold a value of the required mode the
11127              transformation is invalid.  */
11128           if ((equality_comparison_p || unsigned_comparison_p)
11129               && CONST_INT_P (XEXP (op0, 1))
11130               && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
11131                                    & GET_MODE_MASK (mode))
11132                                   + 1)) >= 0
11133               && const_op >> i == 0
11134               && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11135               && (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
11136                                          GET_MODE_BITSIZE (GET_MODE (op0)))
11137                   || (REG_P (XEXP (op0, 0))
11138                       && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11139             {
11140               op0 = gen_lowpart (tmode, XEXP (op0, 0));
11141               continue;
11142             }
11143
11144           /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11145              fits in both M1 and M2 and the SUBREG is either paradoxical
11146              or represents the low part, permute the SUBREG and the AND
11147              and try again.  */
11148           if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11149             {
11150               unsigned HOST_WIDE_INT c1;
11151               tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11152               /* Require an integral mode, to avoid creating something like
11153                  (AND:SF ...).  */
11154               if (SCALAR_INT_MODE_P (tmode)
11155                   /* It is unsafe to commute the AND into the SUBREG if the
11156                      SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11157                      not defined.  As originally written the upper bits
11158                      have a defined value due to the AND operation.
11159                      However, if we commute the AND inside the SUBREG then
11160                      they no longer have defined values and the meaning of
11161                      the code has been changed.  */
11162                   && (0
11163 #ifdef WORD_REGISTER_OPERATIONS
11164                       || (mode_width > GET_MODE_BITSIZE (tmode)
11165                           && mode_width <= BITS_PER_WORD)
11166 #endif
11167                       || (mode_width <= GET_MODE_BITSIZE (tmode)
11168                           && subreg_lowpart_p (XEXP (op0, 0))))
11169                   && CONST_INT_P (XEXP (op0, 1))
11170                   && mode_width <= HOST_BITS_PER_WIDE_INT
11171                   && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
11172                   && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11173                   && (c1 & ~GET_MODE_MASK (tmode)) == 0
11174                   && c1 != mask
11175                   && c1 != GET_MODE_MASK (tmode))
11176                 {
11177                   op0 = simplify_gen_binary (AND, tmode,
11178                                              SUBREG_REG (XEXP (op0, 0)),
11179                                              gen_int_mode (c1, tmode));
11180                   op0 = gen_lowpart (mode, op0);
11181                   continue;
11182                 }
11183             }
11184
11185           /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0).  */
11186           if (const_op == 0 && equality_comparison_p
11187               && XEXP (op0, 1) == const1_rtx
11188               && GET_CODE (XEXP (op0, 0)) == NOT)
11189             {
11190               op0 = simplify_and_const_int
11191                 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
11192               code = (code == NE ? EQ : NE);
11193               continue;
11194             }
11195
11196           /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11197              (eq (and (lshiftrt X) 1) 0).
11198              Also handle the case where (not X) is expressed using xor.  */
11199           if (const_op == 0 && equality_comparison_p
11200               && XEXP (op0, 1) == const1_rtx
11201               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11202             {
11203               rtx shift_op = XEXP (XEXP (op0, 0), 0);
11204               rtx shift_count = XEXP (XEXP (op0, 0), 1);
11205
11206               if (GET_CODE (shift_op) == NOT
11207                   || (GET_CODE (shift_op) == XOR
11208                       && CONST_INT_P (XEXP (shift_op, 1))
11209                       && CONST_INT_P (shift_count)
11210                       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
11211                       && (INTVAL (XEXP (shift_op, 1))
11212                           == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
11213                 {
11214                   op0 = simplify_and_const_int
11215                     (NULL_RTX, mode,
11216                      gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
11217                      (HOST_WIDE_INT) 1);
11218                   code = (code == NE ? EQ : NE);
11219                   continue;
11220                 }
11221             }
11222           break;
11223
11224         case ASHIFT:
11225           /* If we have (compare (ashift FOO N) (const_int C)) and
11226              the high order N bits of FOO (N+1 if an inequality comparison)
11227              are known to be zero, we can do this by comparing FOO with C
11228              shifted right N bits so long as the low-order N bits of C are
11229              zero.  */
11230           if (CONST_INT_P (XEXP (op0, 1))
11231               && INTVAL (XEXP (op0, 1)) >= 0
11232               && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11233                   < HOST_BITS_PER_WIDE_INT)
11234               && ((const_op
11235                    & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
11236               && mode_width <= HOST_BITS_PER_WIDE_INT
11237               && (nonzero_bits (XEXP (op0, 0), mode)
11238                   & ~(mask >> (INTVAL (XEXP (op0, 1))
11239                                + ! equality_comparison_p))) == 0)
11240             {
11241               /* We must perform a logical shift, not an arithmetic one,
11242                  as we want the top N bits of C to be zero.  */
11243               unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11244
11245               temp >>= INTVAL (XEXP (op0, 1));
11246               op1 = gen_int_mode (temp, mode);
11247               op0 = XEXP (op0, 0);
11248               continue;
11249             }
11250
11251           /* If we are doing a sign bit comparison, it means we are testing
11252              a particular bit.  Convert it to the appropriate AND.  */
11253           if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
11254               && mode_width <= HOST_BITS_PER_WIDE_INT)
11255             {
11256               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11257                                             ((HOST_WIDE_INT) 1
11258                                              << (mode_width - 1
11259                                                  - INTVAL (XEXP (op0, 1)))));
11260               code = (code == LT ? NE : EQ);
11261               continue;
11262             }
11263
11264           /* If this an equality comparison with zero and we are shifting
11265              the low bit to the sign bit, we can convert this to an AND of the
11266              low-order bit.  */
11267           if (const_op == 0 && equality_comparison_p
11268               && CONST_INT_P (XEXP (op0, 1))
11269               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11270                  == mode_width - 1)
11271             {
11272               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11273                                             (HOST_WIDE_INT) 1);
11274               continue;
11275             }
11276           break;
11277
11278         case ASHIFTRT:
11279           /* If this is an equality comparison with zero, we can do this
11280              as a logical shift, which might be much simpler.  */
11281           if (equality_comparison_p && const_op == 0
11282               && CONST_INT_P (XEXP (op0, 1)))
11283             {
11284               op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11285                                           XEXP (op0, 0),
11286                                           INTVAL (XEXP (op0, 1)));
11287               continue;
11288             }
11289
11290           /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11291              do the comparison in a narrower mode.  */
11292           if (! unsigned_comparison_p
11293               && CONST_INT_P (XEXP (op0, 1))
11294               && GET_CODE (XEXP (op0, 0)) == ASHIFT
11295               && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11296               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11297                                          MODE_INT, 1)) != BLKmode
11298               && (((unsigned HOST_WIDE_INT) const_op
11299                    + (GET_MODE_MASK (tmode) >> 1) + 1)
11300                   <= GET_MODE_MASK (tmode)))
11301             {
11302               op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
11303               continue;
11304             }
11305
11306           /* Likewise if OP0 is a PLUS of a sign extension with a
11307              constant, which is usually represented with the PLUS
11308              between the shifts.  */
11309           if (! unsigned_comparison_p
11310               && CONST_INT_P (XEXP (op0, 1))
11311               && GET_CODE (XEXP (op0, 0)) == PLUS
11312               && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11313               && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11314               && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11315               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11316                                          MODE_INT, 1)) != BLKmode
11317               && (((unsigned HOST_WIDE_INT) const_op
11318                    + (GET_MODE_MASK (tmode) >> 1) + 1)
11319                   <= GET_MODE_MASK (tmode)))
11320             {
11321               rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11322               rtx add_const = XEXP (XEXP (op0, 0), 1);
11323               rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
11324                                                    add_const, XEXP (op0, 1));
11325
11326               op0 = simplify_gen_binary (PLUS, tmode,
11327                                          gen_lowpart (tmode, inner),
11328                                          new_const);
11329               continue;
11330             }
11331
11332           /* ... fall through ...  */
11333         case LSHIFTRT:
11334           /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11335              the low order N bits of FOO are known to be zero, we can do this
11336              by comparing FOO with C shifted left N bits so long as no
11337              overflow occurs.  */
11338           if (CONST_INT_P (XEXP (op0, 1))
11339               && INTVAL (XEXP (op0, 1)) >= 0
11340               && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11341               && mode_width <= HOST_BITS_PER_WIDE_INT
11342               && (nonzero_bits (XEXP (op0, 0), mode)
11343                   & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
11344               && (((unsigned HOST_WIDE_INT) const_op
11345                    + (GET_CODE (op0) != LSHIFTRT
11346                       ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11347                          + 1)
11348                       : 0))
11349                   <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11350             {
11351               /* If the shift was logical, then we must make the condition
11352                  unsigned.  */
11353               if (GET_CODE (op0) == LSHIFTRT)
11354                 code = unsigned_condition (code);
11355
11356               const_op <<= INTVAL (XEXP (op0, 1));
11357               op1 = GEN_INT (const_op);
11358               op0 = XEXP (op0, 0);
11359               continue;
11360             }
11361
11362           /* If we are using this shift to extract just the sign bit, we
11363              can replace this with an LT or GE comparison.  */
11364           if (const_op == 0
11365               && (equality_comparison_p || sign_bit_comparison_p)
11366               && CONST_INT_P (XEXP (op0, 1))
11367               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11368                  == mode_width - 1)
11369             {
11370               op0 = XEXP (op0, 0);
11371               code = (code == NE || code == GT ? LT : GE);
11372               continue;
11373             }
11374           break;
11375
11376         default:
11377           break;
11378         }
11379
11380       break;
11381     }
11382
11383   /* Now make any compound operations involved in this comparison.  Then,
11384      check for an outmost SUBREG on OP0 that is not doing anything or is
11385      paradoxical.  The latter transformation must only be performed when
11386      it is known that the "extra" bits will be the same in op0 and op1 or
11387      that they don't matter.  There are three cases to consider:
11388
11389      1. SUBREG_REG (op0) is a register.  In this case the bits are don't
11390      care bits and we can assume they have any convenient value.  So
11391      making the transformation is safe.
11392
11393      2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11394      In this case the upper bits of op0 are undefined.  We should not make
11395      the simplification in that case as we do not know the contents of
11396      those bits.
11397
11398      3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11399      UNKNOWN.  In that case we know those bits are zeros or ones.  We must
11400      also be sure that they are the same as the upper bits of op1.
11401
11402      We can never remove a SUBREG for a non-equality comparison because
11403      the sign bit is in a different place in the underlying object.  */
11404
11405   op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11406   op1 = make_compound_operation (op1, SET);
11407
11408   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11409       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11410       && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11411       && (code == NE || code == EQ))
11412     {
11413       if (GET_MODE_SIZE (GET_MODE (op0))
11414           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
11415         {
11416           /* For paradoxical subregs, allow case 1 as above.  Case 3 isn't
11417              implemented.  */
11418           if (REG_P (SUBREG_REG (op0)))
11419             {
11420               op0 = SUBREG_REG (op0);
11421               op1 = gen_lowpart (GET_MODE (op0), op1);
11422             }
11423         }
11424       else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11425                 <= HOST_BITS_PER_WIDE_INT)
11426                && (nonzero_bits (SUBREG_REG (op0),
11427                                  GET_MODE (SUBREG_REG (op0)))
11428                    & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11429         {
11430           tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11431
11432           if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11433                & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11434             op0 = SUBREG_REG (op0), op1 = tem;
11435         }
11436     }
11437
11438   /* We now do the opposite procedure: Some machines don't have compare
11439      insns in all modes.  If OP0's mode is an integer mode smaller than a
11440      word and we can't do a compare in that mode, see if there is a larger
11441      mode for which we can do the compare.  There are a number of cases in
11442      which we can use the wider mode.  */
11443
11444   mode = GET_MODE (op0);
11445   if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11446       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11447       && ! have_insn_for (COMPARE, mode))
11448     for (tmode = GET_MODE_WIDER_MODE (mode);
11449          (tmode != VOIDmode
11450           && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11451          tmode = GET_MODE_WIDER_MODE (tmode))
11452       if (have_insn_for (COMPARE, tmode))
11453         {
11454           int zero_extended;
11455
11456           /* If this is a test for negative, we can make an explicit
11457              test of the sign bit.  Test this first so we can use
11458              a paradoxical subreg to extend OP0.  */
11459
11460           if (op1 == const0_rtx && (code == LT || code == GE)
11461               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11462             {
11463               op0 = simplify_gen_binary (AND, tmode,
11464                                          gen_lowpart (tmode, op0),
11465                                          GEN_INT ((HOST_WIDE_INT) 1
11466                                                   << (GET_MODE_BITSIZE (mode)
11467                                                       - 1)));
11468               code = (code == LT) ? NE : EQ;
11469               break;
11470             }
11471
11472           /* If the only nonzero bits in OP0 and OP1 are those in the
11473              narrower mode and this is an equality or unsigned comparison,
11474              we can use the wider mode.  Similarly for sign-extended
11475              values, in which case it is true for all comparisons.  */
11476           zero_extended = ((code == EQ || code == NE
11477                             || code == GEU || code == GTU
11478                             || code == LEU || code == LTU)
11479                            && (nonzero_bits (op0, tmode)
11480                                & ~GET_MODE_MASK (mode)) == 0
11481                            && ((CONST_INT_P (op1)
11482                                 || (nonzero_bits (op1, tmode)
11483                                     & ~GET_MODE_MASK (mode)) == 0)));
11484
11485           if (zero_extended
11486               || ((num_sign_bit_copies (op0, tmode)
11487                    > (unsigned int) (GET_MODE_BITSIZE (tmode)
11488                                      - GET_MODE_BITSIZE (mode)))
11489                   && (num_sign_bit_copies (op1, tmode)
11490                       > (unsigned int) (GET_MODE_BITSIZE (tmode)
11491                                         - GET_MODE_BITSIZE (mode)))))
11492             {
11493               /* If OP0 is an AND and we don't have an AND in MODE either,
11494                  make a new AND in the proper mode.  */
11495               if (GET_CODE (op0) == AND
11496                   && !have_insn_for (AND, mode))
11497                 op0 = simplify_gen_binary (AND, tmode,
11498                                            gen_lowpart (tmode,
11499                                                         XEXP (op0, 0)),
11500                                            gen_lowpart (tmode,
11501                                                         XEXP (op0, 1)));
11502               else
11503                 {
11504                   if (zero_extended)
11505                     {
11506                       op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
11507                       op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
11508                     }
11509                   else
11510                     {
11511                       op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
11512                       op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
11513                     }
11514                   break;
11515                 }
11516             }
11517         }
11518
11519 #ifdef CANONICALIZE_COMPARISON
11520   /* If this machine only supports a subset of valid comparisons, see if we
11521      can convert an unsupported one into a supported one.  */
11522   CANONICALIZE_COMPARISON (code, op0, op1);
11523 #endif
11524
11525   *pop0 = op0;
11526   *pop1 = op1;
11527
11528   return code;
11529 }
11530 \f
11531 /* Utility function for record_value_for_reg.  Count number of
11532    rtxs in X.  */
11533 static int
11534 count_rtxs (rtx x)
11535 {
11536   enum rtx_code code = GET_CODE (x);
11537   const char *fmt;
11538   int i, j, ret = 1;
11539
11540   if (GET_RTX_CLASS (code) == '2'
11541       || GET_RTX_CLASS (code) == 'c')
11542     {
11543       rtx x0 = XEXP (x, 0);
11544       rtx x1 = XEXP (x, 1);
11545
11546       if (x0 == x1)
11547         return 1 + 2 * count_rtxs (x0);
11548
11549       if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11550            || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11551           && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11552         return 2 + 2 * count_rtxs (x0)
11553                + count_rtxs (x == XEXP (x1, 0)
11554                              ? XEXP (x1, 1) : XEXP (x1, 0));
11555
11556       if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11557            || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11558           && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11559         return 2 + 2 * count_rtxs (x1)
11560                + count_rtxs (x == XEXP (x0, 0)
11561                              ? XEXP (x0, 1) : XEXP (x0, 0));
11562     }
11563
11564   fmt = GET_RTX_FORMAT (code);
11565   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11566     if (fmt[i] == 'e')
11567       ret += count_rtxs (XEXP (x, i));
11568     else if (fmt[i] == 'E')
11569       for (j = 0; j < XVECLEN (x, i); j++)
11570         ret += count_rtxs (XVECEXP (x, i, j));
11571
11572   return ret;
11573 }
11574 \f
11575 /* Utility function for following routine.  Called when X is part of a value
11576    being stored into last_set_value.  Sets last_set_table_tick
11577    for each register mentioned.  Similar to mention_regs in cse.c  */
11578
11579 static void
11580 update_table_tick (rtx x)
11581 {
11582   enum rtx_code code = GET_CODE (x);
11583   const char *fmt = GET_RTX_FORMAT (code);
11584   int i, j;
11585
11586   if (code == REG)
11587     {
11588       unsigned int regno = REGNO (x);
11589       unsigned int endregno = END_REGNO (x);
11590       unsigned int r;
11591
11592       for (r = regno; r < endregno; r++)
11593         {
11594           reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, r);
11595           rsp->last_set_table_tick = label_tick;
11596         }
11597
11598       return;
11599     }
11600
11601   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11602     if (fmt[i] == 'e')
11603       {
11604         /* Check for identical subexpressions.  If x contains
11605            identical subexpression we only have to traverse one of
11606            them.  */
11607         if (i == 0 && ARITHMETIC_P (x))
11608           {
11609             /* Note that at this point x1 has already been
11610                processed.  */
11611             rtx x0 = XEXP (x, 0);
11612             rtx x1 = XEXP (x, 1);
11613
11614             /* If x0 and x1 are identical then there is no need to
11615                process x0.  */
11616             if (x0 == x1)
11617               break;
11618
11619             /* If x0 is identical to a subexpression of x1 then while
11620                processing x1, x0 has already been processed.  Thus we
11621                are done with x.  */
11622             if (ARITHMETIC_P (x1)
11623                 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11624               break;
11625
11626             /* If x1 is identical to a subexpression of x0 then we
11627                still have to process the rest of x0.  */
11628             if (ARITHMETIC_P (x0)
11629                 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11630               {
11631                 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
11632                 break;
11633               }
11634           }
11635
11636         update_table_tick (XEXP (x, i));
11637       }
11638     else if (fmt[i] == 'E')
11639       for (j = 0; j < XVECLEN (x, i); j++)
11640         update_table_tick (XVECEXP (x, i, j));
11641 }
11642
11643 /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
11644    are saying that the register is clobbered and we no longer know its
11645    value.  If INSN is zero, don't update reg_stat[].last_set; this is
11646    only permitted with VALUE also zero and is used to invalidate the
11647    register.  */
11648
11649 static void
11650 record_value_for_reg (rtx reg, rtx insn, rtx value)
11651 {
11652   unsigned int regno = REGNO (reg);
11653   unsigned int endregno = END_REGNO (reg);
11654   unsigned int i;
11655   reg_stat_type *rsp;
11656
11657   /* If VALUE contains REG and we have a previous value for REG, substitute
11658      the previous value.  */
11659   if (value && insn && reg_overlap_mentioned_p (reg, value))
11660     {
11661       rtx tem;
11662
11663       /* Set things up so get_last_value is allowed to see anything set up to
11664          our insn.  */
11665       subst_low_luid = DF_INSN_LUID (insn);
11666       tem = get_last_value (reg);
11667
11668       /* If TEM is simply a binary operation with two CLOBBERs as operands,
11669          it isn't going to be useful and will take a lot of time to process,
11670          so just use the CLOBBER.  */
11671
11672       if (tem)
11673         {
11674           if (ARITHMETIC_P (tem)
11675               && GET_CODE (XEXP (tem, 0)) == CLOBBER
11676               && GET_CODE (XEXP (tem, 1)) == CLOBBER)
11677             tem = XEXP (tem, 0);
11678           else if (count_occurrences (value, reg, 1) >= 2)
11679             {
11680               /* If there are two or more occurrences of REG in VALUE,
11681                  prevent the value from growing too much.  */
11682               if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
11683                 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
11684             }
11685
11686           value = replace_rtx (copy_rtx (value), reg, tem);
11687         }
11688     }
11689
11690   /* For each register modified, show we don't know its value, that
11691      we don't know about its bitwise content, that its value has been
11692      updated, and that we don't know the location of the death of the
11693      register.  */
11694   for (i = regno; i < endregno; i++)
11695     {
11696       rsp = VEC_index (reg_stat_type, reg_stat, i);
11697
11698       if (insn)
11699         rsp->last_set = insn;
11700
11701       rsp->last_set_value = 0;
11702       rsp->last_set_mode = VOIDmode;
11703       rsp->last_set_nonzero_bits = 0;
11704       rsp->last_set_sign_bit_copies = 0;
11705       rsp->last_death = 0;
11706       rsp->truncated_to_mode = VOIDmode;
11707     }
11708
11709   /* Mark registers that are being referenced in this value.  */
11710   if (value)
11711     update_table_tick (value);
11712
11713   /* Now update the status of each register being set.
11714      If someone is using this register in this block, set this register
11715      to invalid since we will get confused between the two lives in this
11716      basic block.  This makes using this register always invalid.  In cse, we
11717      scan the table to invalidate all entries using this register, but this
11718      is too much work for us.  */
11719
11720   for (i = regno; i < endregno; i++)
11721     {
11722       rsp = VEC_index (reg_stat_type, reg_stat, i);
11723       rsp->last_set_label = label_tick;
11724       if (!insn
11725           || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
11726         rsp->last_set_invalid = 1;
11727       else
11728         rsp->last_set_invalid = 0;
11729     }
11730
11731   /* The value being assigned might refer to X (like in "x++;").  In that
11732      case, we must replace it with (clobber (const_int 0)) to prevent
11733      infinite loops.  */
11734   rsp = VEC_index (reg_stat_type, reg_stat, regno);
11735   if (value && !get_last_value_validate (&value, insn, label_tick, 0))
11736     {
11737       value = copy_rtx (value);
11738       if (!get_last_value_validate (&value, insn, label_tick, 1))
11739         value = 0;
11740     }
11741
11742   /* For the main register being modified, update the value, the mode, the
11743      nonzero bits, and the number of sign bit copies.  */
11744
11745   rsp->last_set_value = value;
11746
11747   if (value)
11748     {
11749       enum machine_mode mode = GET_MODE (reg);
11750       subst_low_luid = DF_INSN_LUID (insn);
11751       rsp->last_set_mode = mode;
11752       if (GET_MODE_CLASS (mode) == MODE_INT
11753           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11754         mode = nonzero_bits_mode;
11755       rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
11756       rsp->last_set_sign_bit_copies
11757         = num_sign_bit_copies (value, GET_MODE (reg));
11758     }
11759 }
11760
11761 /* Called via note_stores from record_dead_and_set_regs to handle one
11762    SET or CLOBBER in an insn.  DATA is the instruction in which the
11763    set is occurring.  */
11764
11765 static void
11766 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
11767 {
11768   rtx record_dead_insn = (rtx) data;
11769
11770   if (GET_CODE (dest) == SUBREG)
11771     dest = SUBREG_REG (dest);
11772
11773   if (!record_dead_insn)
11774     {
11775       if (REG_P (dest))
11776         record_value_for_reg (dest, NULL_RTX, NULL_RTX);
11777       return;
11778     }
11779
11780   if (REG_P (dest))
11781     {
11782       /* If we are setting the whole register, we know its value.  Otherwise
11783          show that we don't know the value.  We can handle SUBREG in
11784          some cases.  */
11785       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11786         record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11787       else if (GET_CODE (setter) == SET
11788                && GET_CODE (SET_DEST (setter)) == SUBREG
11789                && SUBREG_REG (SET_DEST (setter)) == dest
11790                && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11791                && subreg_lowpart_p (SET_DEST (setter)))
11792         record_value_for_reg (dest, record_dead_insn,
11793                               gen_lowpart (GET_MODE (dest),
11794                                                        SET_SRC (setter)));
11795       else
11796         record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11797     }
11798   else if (MEM_P (dest)
11799            /* Ignore pushes, they clobber nothing.  */
11800            && ! push_operand (dest, GET_MODE (dest)))
11801     mem_last_set = DF_INSN_LUID (record_dead_insn);
11802 }
11803
11804 /* Update the records of when each REG was most recently set or killed
11805    for the things done by INSN.  This is the last thing done in processing
11806    INSN in the combiner loop.
11807
11808    We update reg_stat[], in particular fields last_set, last_set_value,
11809    last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
11810    last_death, and also the similar information mem_last_set (which insn
11811    most recently modified memory) and last_call_luid (which insn was the
11812    most recent subroutine call).  */
11813
11814 static void
11815 record_dead_and_set_regs (rtx insn)
11816 {
11817   rtx link;
11818   unsigned int i;
11819
11820   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11821     {
11822       if (REG_NOTE_KIND (link) == REG_DEAD
11823           && REG_P (XEXP (link, 0)))
11824         {
11825           unsigned int regno = REGNO (XEXP (link, 0));
11826           unsigned int endregno = END_REGNO (XEXP (link, 0));
11827
11828           for (i = regno; i < endregno; i++)
11829             {
11830               reg_stat_type *rsp;
11831
11832               rsp = VEC_index (reg_stat_type, reg_stat, i);
11833               rsp->last_death = insn;
11834             }
11835         }
11836       else if (REG_NOTE_KIND (link) == REG_INC)
11837         record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11838     }
11839
11840   if (CALL_P (insn))
11841     {
11842       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11843         if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11844           {
11845             reg_stat_type *rsp;
11846
11847             rsp = VEC_index (reg_stat_type, reg_stat, i);
11848             rsp->last_set_invalid = 1;
11849             rsp->last_set = insn;
11850             rsp->last_set_value = 0;
11851             rsp->last_set_mode = VOIDmode;
11852             rsp->last_set_nonzero_bits = 0;
11853             rsp->last_set_sign_bit_copies = 0;
11854             rsp->last_death = 0;
11855             rsp->truncated_to_mode = VOIDmode;
11856           }
11857
11858       last_call_luid = mem_last_set = DF_INSN_LUID (insn);
11859
11860       /* We can't combine into a call pattern.  Remember, though, that
11861          the return value register is set at this LUID.  We could
11862          still replace a register with the return value from the
11863          wrong subroutine call!  */
11864       note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
11865     }
11866   else
11867     note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11868 }
11869
11870 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11871    register present in the SUBREG, so for each such SUBREG go back and
11872    adjust nonzero and sign bit information of the registers that are
11873    known to have some zero/sign bits set.
11874
11875    This is needed because when combine blows the SUBREGs away, the
11876    information on zero/sign bits is lost and further combines can be
11877    missed because of that.  */
11878
11879 static void
11880 record_promoted_value (rtx insn, rtx subreg)
11881 {
11882   rtx links, set;
11883   unsigned int regno = REGNO (SUBREG_REG (subreg));
11884   enum machine_mode mode = GET_MODE (subreg);
11885
11886   if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11887     return;
11888
11889   for (links = LOG_LINKS (insn); links;)
11890     {
11891       reg_stat_type *rsp;
11892
11893       insn = XEXP (links, 0);
11894       set = single_set (insn);
11895
11896       if (! set || !REG_P (SET_DEST (set))
11897           || REGNO (SET_DEST (set)) != regno
11898           || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11899         {
11900           links = XEXP (links, 1);
11901           continue;
11902         }
11903
11904       rsp = VEC_index (reg_stat_type, reg_stat, regno);
11905       if (rsp->last_set == insn)
11906         {
11907           if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11908             rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
11909         }
11910
11911       if (REG_P (SET_SRC (set)))
11912         {
11913           regno = REGNO (SET_SRC (set));
11914           links = LOG_LINKS (insn);
11915         }
11916       else
11917         break;
11918     }
11919 }
11920
11921 /* Check if X, a register, is known to contain a value already
11922    truncated to MODE.  In this case we can use a subreg to refer to
11923    the truncated value even though in the generic case we would need
11924    an explicit truncation.  */
11925
11926 static bool
11927 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
11928 {
11929   reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
11930   enum machine_mode truncated = rsp->truncated_to_mode;
11931
11932   if (truncated == 0
11933       || rsp->truncation_label < label_tick_ebb_start)
11934     return false;
11935   if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
11936     return true;
11937   if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
11938                              GET_MODE_BITSIZE (truncated)))
11939     return true;
11940   return false;
11941 }
11942
11943 /* Callback for for_each_rtx.  If *P is a hard reg or a subreg record the mode
11944    that the register is accessed in.  For non-TRULY_NOOP_TRUNCATION targets we
11945    might be able to turn a truncate into a subreg using this information.
11946    Return -1 if traversing *P is complete or 0 otherwise.  */
11947
11948 static int
11949 record_truncated_value (rtx *p, void *data ATTRIBUTE_UNUSED)
11950 {
11951   rtx x = *p;
11952   enum machine_mode truncated_mode;
11953   reg_stat_type *rsp;
11954
11955   if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
11956     {
11957       enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
11958       truncated_mode = GET_MODE (x);
11959
11960       if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
11961         return -1;
11962
11963       if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (truncated_mode),
11964                                  GET_MODE_BITSIZE (original_mode)))
11965         return -1;
11966
11967       x = SUBREG_REG (x);
11968     }
11969   /* ??? For hard-regs we now record everything.  We might be able to
11970      optimize this using last_set_mode.  */
11971   else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
11972     truncated_mode = GET_MODE (x);
11973   else
11974     return 0;
11975
11976   rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
11977   if (rsp->truncated_to_mode == 0
11978       || rsp->truncation_label < label_tick_ebb_start
11979       || (GET_MODE_SIZE (truncated_mode)
11980           < GET_MODE_SIZE (rsp->truncated_to_mode)))
11981     {
11982       rsp->truncated_to_mode = truncated_mode;
11983       rsp->truncation_label = label_tick;
11984     }
11985
11986   return -1;
11987 }
11988
11989 /* Callback for note_uses.  Find hardregs and subregs of pseudos and
11990    the modes they are used in.  This can help truning TRUNCATEs into
11991    SUBREGs.  */
11992
11993 static void
11994 record_truncated_values (rtx *x, void *data ATTRIBUTE_UNUSED)
11995 {
11996   for_each_rtx (x, record_truncated_value, NULL);
11997 }
11998
11999 /* Scan X for promoted SUBREGs.  For each one found,
12000    note what it implies to the registers used in it.  */
12001
12002 static void
12003 check_promoted_subreg (rtx insn, rtx x)
12004 {
12005   if (GET_CODE (x) == SUBREG
12006       && SUBREG_PROMOTED_VAR_P (x)
12007       && REG_P (SUBREG_REG (x)))
12008     record_promoted_value (insn, x);
12009   else
12010     {
12011       const char *format = GET_RTX_FORMAT (GET_CODE (x));
12012       int i, j;
12013
12014       for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12015         switch (format[i])
12016           {
12017           case 'e':
12018             check_promoted_subreg (insn, XEXP (x, i));
12019             break;
12020           case 'V':
12021           case 'E':
12022             if (XVEC (x, i) != 0)
12023               for (j = 0; j < XVECLEN (x, i); j++)
12024                 check_promoted_subreg (insn, XVECEXP (x, i, j));
12025             break;
12026           }
12027     }
12028 }
12029 \f
12030 /* Verify that all the registers and memory references mentioned in *LOC are
12031    still valid.  *LOC was part of a value set in INSN when label_tick was
12032    equal to TICK.  Return 0 if some are not.  If REPLACE is nonzero, replace
12033    the invalid references with (clobber (const_int 0)) and return 1.  This
12034    replacement is useful because we often can get useful information about
12035    the form of a value (e.g., if it was produced by a shift that always
12036    produces -1 or 0) even though we don't know exactly what registers it
12037    was produced from.  */
12038
12039 static int
12040 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
12041 {
12042   rtx x = *loc;
12043   const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12044   int len = GET_RTX_LENGTH (GET_CODE (x));
12045   int i, j;
12046
12047   if (REG_P (x))
12048     {
12049       unsigned int regno = REGNO (x);
12050       unsigned int endregno = END_REGNO (x);
12051       unsigned int j;
12052
12053       for (j = regno; j < endregno; j++)
12054         {
12055           reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, j);
12056           if (rsp->last_set_invalid
12057               /* If this is a pseudo-register that was only set once and not
12058                  live at the beginning of the function, it is always valid.  */
12059               || (! (regno >= FIRST_PSEUDO_REGISTER
12060                      && REG_N_SETS (regno) == 1
12061                      && (!REGNO_REG_SET_P
12062                          (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno)))
12063                   && rsp->last_set_label > tick))
12064           {
12065             if (replace)
12066               *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12067             return replace;
12068           }
12069         }
12070
12071       return 1;
12072     }
12073   /* If this is a memory reference, make sure that there were no stores after
12074      it that might have clobbered the value.  We don't have alias info, so we
12075      assume any store invalidates it.  Moreover, we only have local UIDs, so
12076      we also assume that there were stores in the intervening basic blocks.  */
12077   else if (MEM_P (x) && !MEM_READONLY_P (x)
12078            && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12079     {
12080       if (replace)
12081         *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12082       return replace;
12083     }
12084
12085   for (i = 0; i < len; i++)
12086     {
12087       if (fmt[i] == 'e')
12088         {
12089           /* Check for identical subexpressions.  If x contains
12090              identical subexpression we only have to traverse one of
12091              them.  */
12092           if (i == 1 && ARITHMETIC_P (x))
12093             {
12094               /* Note that at this point x0 has already been checked
12095                  and found valid.  */
12096               rtx x0 = XEXP (x, 0);
12097               rtx x1 = XEXP (x, 1);
12098
12099               /* If x0 and x1 are identical then x is also valid.  */
12100               if (x0 == x1)
12101                 return 1;
12102
12103               /* If x1 is identical to a subexpression of x0 then
12104                  while checking x0, x1 has already been checked.  Thus
12105                  it is valid and so as x.  */
12106               if (ARITHMETIC_P (x0)
12107                   && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12108                 return 1;
12109
12110               /* If x0 is identical to a subexpression of x1 then x is
12111                  valid iff the rest of x1 is valid.  */
12112               if (ARITHMETIC_P (x1)
12113                   && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12114                 return
12115                   get_last_value_validate (&XEXP (x1,
12116                                                   x0 == XEXP (x1, 0) ? 1 : 0),
12117                                            insn, tick, replace);
12118             }
12119
12120           if (get_last_value_validate (&XEXP (x, i), insn, tick,
12121                                        replace) == 0)
12122             return 0;
12123         }
12124       else if (fmt[i] == 'E')
12125         for (j = 0; j < XVECLEN (x, i); j++)
12126           if (get_last_value_validate (&XVECEXP (x, i, j),
12127                                        insn, tick, replace) == 0)
12128             return 0;
12129     }
12130
12131   /* If we haven't found a reason for it to be invalid, it is valid.  */
12132   return 1;
12133 }
12134
12135 /* Get the last value assigned to X, if known.  Some registers
12136    in the value may be replaced with (clobber (const_int 0)) if their value
12137    is known longer known reliably.  */
12138
12139 static rtx
12140 get_last_value (const_rtx x)
12141 {
12142   unsigned int regno;
12143   rtx value;
12144   reg_stat_type *rsp;
12145
12146   /* If this is a non-paradoxical SUBREG, get the value of its operand and
12147      then convert it to the desired mode.  If this is a paradoxical SUBREG,
12148      we cannot predict what values the "extra" bits might have.  */
12149   if (GET_CODE (x) == SUBREG
12150       && subreg_lowpart_p (x)
12151       && (GET_MODE_SIZE (GET_MODE (x))
12152           <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
12153       && (value = get_last_value (SUBREG_REG (x))) != 0)
12154     return gen_lowpart (GET_MODE (x), value);
12155
12156   if (!REG_P (x))
12157     return 0;
12158
12159   regno = REGNO (x);
12160   rsp = VEC_index (reg_stat_type, reg_stat, regno);
12161   value = rsp->last_set_value;
12162
12163   /* If we don't have a value, or if it isn't for this basic block and
12164      it's either a hard register, set more than once, or it's a live
12165      at the beginning of the function, return 0.
12166
12167      Because if it's not live at the beginning of the function then the reg
12168      is always set before being used (is never used without being set).
12169      And, if it's set only once, and it's always set before use, then all
12170      uses must have the same last value, even if it's not from this basic
12171      block.  */
12172
12173   if (value == 0
12174       || (rsp->last_set_label < label_tick_ebb_start
12175           && (regno < FIRST_PSEUDO_REGISTER
12176               || REG_N_SETS (regno) != 1
12177               || REGNO_REG_SET_P
12178                  (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno))))
12179     return 0;
12180
12181   /* If the value was set in a later insn than the ones we are processing,
12182      we can't use it even if the register was only set once.  */
12183   if (rsp->last_set_label == label_tick
12184       && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12185     return 0;
12186
12187   /* If the value has all its registers valid, return it.  */
12188   if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12189     return value;
12190
12191   /* Otherwise, make a copy and replace any invalid register with
12192      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
12193
12194   value = copy_rtx (value);
12195   if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
12196     return value;
12197
12198   return 0;
12199 }
12200 \f
12201 /* Return nonzero if expression X refers to a REG or to memory
12202    that is set in an instruction more recent than FROM_LUID.  */
12203
12204 static int
12205 use_crosses_set_p (const_rtx x, int from_luid)
12206 {
12207   const char *fmt;
12208   int i;
12209   enum rtx_code code = GET_CODE (x);
12210
12211   if (code == REG)
12212     {
12213       unsigned int regno = REGNO (x);
12214       unsigned endreg = END_REGNO (x);
12215
12216 #ifdef PUSH_ROUNDING
12217       /* Don't allow uses of the stack pointer to be moved,
12218          because we don't know whether the move crosses a push insn.  */
12219       if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12220         return 1;
12221 #endif
12222       for (; regno < endreg; regno++)
12223         {
12224           reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
12225           if (rsp->last_set
12226               && rsp->last_set_label == label_tick
12227               && DF_INSN_LUID (rsp->last_set) > from_luid)
12228             return 1;
12229         }
12230       return 0;
12231     }
12232
12233   if (code == MEM && mem_last_set > from_luid)
12234     return 1;
12235
12236   fmt = GET_RTX_FORMAT (code);
12237
12238   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12239     {
12240       if (fmt[i] == 'E')
12241         {
12242           int j;
12243           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12244             if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
12245               return 1;
12246         }
12247       else if (fmt[i] == 'e'
12248                && use_crosses_set_p (XEXP (x, i), from_luid))
12249         return 1;
12250     }
12251   return 0;
12252 }
12253 \f
12254 /* Define three variables used for communication between the following
12255    routines.  */
12256
12257 static unsigned int reg_dead_regno, reg_dead_endregno;
12258 static int reg_dead_flag;
12259
12260 /* Function called via note_stores from reg_dead_at_p.
12261
12262    If DEST is within [reg_dead_regno, reg_dead_endregno), set
12263    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
12264
12265 static void
12266 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
12267 {
12268   unsigned int regno, endregno;
12269
12270   if (!REG_P (dest))
12271     return;
12272
12273   regno = REGNO (dest);
12274   endregno = END_REGNO (dest);
12275   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12276     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12277 }
12278
12279 /* Return nonzero if REG is known to be dead at INSN.
12280
12281    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
12282    referencing REG, it is dead.  If we hit a SET referencing REG, it is
12283    live.  Otherwise, see if it is live or dead at the start of the basic
12284    block we are in.  Hard regs marked as being live in NEWPAT_USED_REGS
12285    must be assumed to be always live.  */
12286
12287 static int
12288 reg_dead_at_p (rtx reg, rtx insn)
12289 {
12290   basic_block block;
12291   unsigned int i;
12292
12293   /* Set variables for reg_dead_at_p_1.  */
12294   reg_dead_regno = REGNO (reg);
12295   reg_dead_endregno = END_REGNO (reg);
12296
12297   reg_dead_flag = 0;
12298
12299   /* Check that reg isn't mentioned in NEWPAT_USED_REGS.  For fixed registers
12300      we allow the machine description to decide whether use-and-clobber
12301      patterns are OK.  */
12302   if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12303     {
12304       for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12305         if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
12306           return 0;
12307     }
12308
12309   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
12310      beginning of basic block.  */
12311   block = BLOCK_FOR_INSN (insn);
12312   for (;;)
12313     {
12314       if (INSN_P (insn))
12315         {
12316           note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12317           if (reg_dead_flag)
12318             return reg_dead_flag == 1 ? 1 : 0;
12319
12320           if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12321             return 1;
12322         }
12323
12324       if (insn == BB_HEAD (block))
12325         break;
12326
12327       insn = PREV_INSN (insn);
12328     }
12329
12330   /* Look at live-in sets for the basic block that we were in.  */
12331   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12332     if (REGNO_REG_SET_P (df_get_live_in (block), i))
12333       return 0;
12334
12335   return 1;
12336 }
12337 \f
12338 /* Note hard registers in X that are used.  */
12339
12340 static void
12341 mark_used_regs_combine (rtx x)
12342 {
12343   RTX_CODE code = GET_CODE (x);
12344   unsigned int regno;
12345   int i;
12346
12347   switch (code)
12348     {
12349     case LABEL_REF:
12350     case SYMBOL_REF:
12351     case CONST_INT:
12352     case CONST:
12353     case CONST_DOUBLE:
12354     case CONST_VECTOR:
12355     case PC:
12356     case ADDR_VEC:
12357     case ADDR_DIFF_VEC:
12358     case ASM_INPUT:
12359 #ifdef HAVE_cc0
12360     /* CC0 must die in the insn after it is set, so we don't need to take
12361        special note of it here.  */
12362     case CC0:
12363 #endif
12364       return;
12365
12366     case CLOBBER:
12367       /* If we are clobbering a MEM, mark any hard registers inside the
12368          address as used.  */
12369       if (MEM_P (XEXP (x, 0)))
12370         mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12371       return;
12372
12373     case REG:
12374       regno = REGNO (x);
12375       /* A hard reg in a wide mode may really be multiple registers.
12376          If so, mark all of them just like the first.  */
12377       if (regno < FIRST_PSEUDO_REGISTER)
12378         {
12379           /* None of this applies to the stack, frame or arg pointers.  */
12380           if (regno == STACK_POINTER_REGNUM
12381 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
12382               || regno == HARD_FRAME_POINTER_REGNUM
12383 #endif
12384 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12385               || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12386 #endif
12387               || regno == FRAME_POINTER_REGNUM)
12388             return;
12389
12390           add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
12391         }
12392       return;
12393
12394     case SET:
12395       {
12396         /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12397            the address.  */
12398         rtx testreg = SET_DEST (x);
12399
12400         while (GET_CODE (testreg) == SUBREG
12401                || GET_CODE (testreg) == ZERO_EXTRACT
12402                || GET_CODE (testreg) == STRICT_LOW_PART)
12403           testreg = XEXP (testreg, 0);
12404
12405         if (MEM_P (testreg))
12406           mark_used_regs_combine (XEXP (testreg, 0));
12407
12408         mark_used_regs_combine (SET_SRC (x));
12409       }
12410       return;
12411
12412     default:
12413       break;
12414     }
12415
12416   /* Recursively scan the operands of this expression.  */
12417
12418   {
12419     const char *fmt = GET_RTX_FORMAT (code);
12420
12421     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12422       {
12423         if (fmt[i] == 'e')
12424           mark_used_regs_combine (XEXP (x, i));
12425         else if (fmt[i] == 'E')
12426           {
12427             int j;
12428
12429             for (j = 0; j < XVECLEN (x, i); j++)
12430               mark_used_regs_combine (XVECEXP (x, i, j));
12431           }
12432       }
12433   }
12434 }
12435 \f
12436 /* Remove register number REGNO from the dead registers list of INSN.
12437
12438    Return the note used to record the death, if there was one.  */
12439
12440 rtx
12441 remove_death (unsigned int regno, rtx insn)
12442 {
12443   rtx note = find_regno_note (insn, REG_DEAD, regno);
12444
12445   if (note)
12446     remove_note (insn, note);
12447
12448   return note;
12449 }
12450
12451 /* For each register (hardware or pseudo) used within expression X, if its
12452    death is in an instruction with luid between FROM_LUID (inclusive) and
12453    TO_INSN (exclusive), put a REG_DEAD note for that register in the
12454    list headed by PNOTES.
12455
12456    That said, don't move registers killed by maybe_kill_insn.
12457
12458    This is done when X is being merged by combination into TO_INSN.  These
12459    notes will then be distributed as needed.  */
12460
12461 static void
12462 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx to_insn,
12463              rtx *pnotes)
12464 {
12465   const char *fmt;
12466   int len, i;
12467   enum rtx_code code = GET_CODE (x);
12468
12469   if (code == REG)
12470     {
12471       unsigned int regno = REGNO (x);
12472       rtx where_dead = VEC_index (reg_stat_type, reg_stat, regno)->last_death;
12473
12474       /* Don't move the register if it gets killed in between from and to.  */
12475       if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12476           && ! reg_referenced_p (x, maybe_kill_insn))
12477         return;
12478
12479       if (where_dead
12480           && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
12481           && DF_INSN_LUID (where_dead) >= from_luid
12482           && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
12483         {
12484           rtx note = remove_death (regno, where_dead);
12485
12486           /* It is possible for the call above to return 0.  This can occur
12487              when last_death points to I2 or I1 that we combined with.
12488              In that case make a new note.
12489
12490              We must also check for the case where X is a hard register
12491              and NOTE is a death note for a range of hard registers
12492              including X.  In that case, we must put REG_DEAD notes for
12493              the remaining registers in place of NOTE.  */
12494
12495           if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12496               && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12497                   > GET_MODE_SIZE (GET_MODE (x))))
12498             {
12499               unsigned int deadregno = REGNO (XEXP (note, 0));
12500               unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
12501               unsigned int ourend = END_HARD_REGNO (x);
12502               unsigned int i;
12503
12504               for (i = deadregno; i < deadend; i++)
12505                 if (i < regno || i >= ourend)
12506                   add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
12507             }
12508
12509           /* If we didn't find any note, or if we found a REG_DEAD note that
12510              covers only part of the given reg, and we have a multi-reg hard
12511              register, then to be safe we must check for REG_DEAD notes
12512              for each register other than the first.  They could have
12513              their own REG_DEAD notes lying around.  */
12514           else if ((note == 0
12515                     || (note != 0
12516                         && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12517                             < GET_MODE_SIZE (GET_MODE (x)))))
12518                    && regno < FIRST_PSEUDO_REGISTER
12519                    && hard_regno_nregs[regno][GET_MODE (x)] > 1)
12520             {
12521               unsigned int ourend = END_HARD_REGNO (x);
12522               unsigned int i, offset;
12523               rtx oldnotes = 0;
12524
12525               if (note)
12526                 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
12527               else
12528                 offset = 1;
12529
12530               for (i = regno + offset; i < ourend; i++)
12531                 move_deaths (regno_reg_rtx[i],
12532                              maybe_kill_insn, from_luid, to_insn, &oldnotes);
12533             }
12534
12535           if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12536             {
12537               XEXP (note, 1) = *pnotes;
12538               *pnotes = note;
12539             }
12540           else
12541             *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
12542         }
12543
12544       return;
12545     }
12546
12547   else if (GET_CODE (x) == SET)
12548     {
12549       rtx dest = SET_DEST (x);
12550
12551       move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
12552
12553       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
12554          that accesses one word of a multi-word item, some
12555          piece of everything register in the expression is used by
12556          this insn, so remove any old death.  */
12557       /* ??? So why do we test for equality of the sizes?  */
12558
12559       if (GET_CODE (dest) == ZERO_EXTRACT
12560           || GET_CODE (dest) == STRICT_LOW_PART
12561           || (GET_CODE (dest) == SUBREG
12562               && (((GET_MODE_SIZE (GET_MODE (dest))
12563                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
12564                   == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
12565                        + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
12566         {
12567           move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
12568           return;
12569         }
12570
12571       /* If this is some other SUBREG, we know it replaces the entire
12572          value, so use that as the destination.  */
12573       if (GET_CODE (dest) == SUBREG)
12574         dest = SUBREG_REG (dest);
12575
12576       /* If this is a MEM, adjust deaths of anything used in the address.
12577          For a REG (the only other possibility), the entire value is
12578          being replaced so the old value is not used in this insn.  */
12579
12580       if (MEM_P (dest))
12581         move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
12582                      to_insn, pnotes);
12583       return;
12584     }
12585
12586   else if (GET_CODE (x) == CLOBBER)
12587     return;
12588
12589   len = GET_RTX_LENGTH (code);
12590   fmt = GET_RTX_FORMAT (code);
12591
12592   for (i = 0; i < len; i++)
12593     {
12594       if (fmt[i] == 'E')
12595         {
12596           int j;
12597           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12598             move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
12599                          to_insn, pnotes);
12600         }
12601       else if (fmt[i] == 'e')
12602         move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
12603     }
12604 }
12605 \f
12606 /* Return 1 if X is the target of a bit-field assignment in BODY, the
12607    pattern of an insn.  X must be a REG.  */
12608
12609 static int
12610 reg_bitfield_target_p (rtx x, rtx body)
12611 {
12612   int i;
12613
12614   if (GET_CODE (body) == SET)
12615     {
12616       rtx dest = SET_DEST (body);
12617       rtx target;
12618       unsigned int regno, tregno, endregno, endtregno;
12619
12620       if (GET_CODE (dest) == ZERO_EXTRACT)
12621         target = XEXP (dest, 0);
12622       else if (GET_CODE (dest) == STRICT_LOW_PART)
12623         target = SUBREG_REG (XEXP (dest, 0));
12624       else
12625         return 0;
12626
12627       if (GET_CODE (target) == SUBREG)
12628         target = SUBREG_REG (target);
12629
12630       if (!REG_P (target))
12631         return 0;
12632
12633       tregno = REGNO (target), regno = REGNO (x);
12634       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
12635         return target == x;
12636
12637       endtregno = end_hard_regno (GET_MODE (target), tregno);
12638       endregno = end_hard_regno (GET_MODE (x), regno);
12639
12640       return endregno > tregno && regno < endtregno;
12641     }
12642
12643   else if (GET_CODE (body) == PARALLEL)
12644     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
12645       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
12646         return 1;
12647
12648   return 0;
12649 }
12650
12651 /* Return the next insn after INSN that is neither a NOTE nor a
12652    DEBUG_INSN.  This routine does not look inside SEQUENCEs.  */
12653
12654 static rtx
12655 next_nonnote_nondebug_insn (rtx insn)
12656 {
12657   while (insn)
12658     {
12659       insn = NEXT_INSN (insn);
12660       if (insn == 0)
12661         break;
12662       if (NOTE_P (insn))
12663         continue;
12664       if (DEBUG_INSN_P (insn))
12665         continue;
12666       break;
12667     }
12668
12669   return insn;
12670 }
12671
12672
12673 \f
12674 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
12675    as appropriate.  I3 and I2 are the insns resulting from the combination
12676    insns including FROM (I2 may be zero).
12677
12678    ELIM_I2 and ELIM_I1 are either zero or registers that we know will
12679    not need REG_DEAD notes because they are being substituted for.  This
12680    saves searching in the most common cases.
12681
12682    Each note in the list is either ignored or placed on some insns, depending
12683    on the type of note.  */
12684
12685 static void
12686 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
12687                   rtx elim_i1)
12688 {
12689   rtx note, next_note;
12690   rtx tem;
12691
12692   for (note = notes; note; note = next_note)
12693     {
12694       rtx place = 0, place2 = 0;
12695
12696       next_note = XEXP (note, 1);
12697       switch (REG_NOTE_KIND (note))
12698         {
12699         case REG_BR_PROB:
12700         case REG_BR_PRED:
12701           /* Doesn't matter much where we put this, as long as it's somewhere.
12702              It is preferable to keep these notes on branches, which is most
12703              likely to be i3.  */
12704           place = i3;
12705           break;
12706
12707         case REG_VALUE_PROFILE:
12708           /* Just get rid of this note, as it is unused later anyway.  */
12709           break;
12710
12711         case REG_NON_LOCAL_GOTO:
12712           if (JUMP_P (i3))
12713             place = i3;
12714           else
12715             {
12716               gcc_assert (i2 && JUMP_P (i2));
12717               place = i2;
12718             }
12719           break;
12720
12721         case REG_EH_REGION:
12722           /* These notes must remain with the call or trapping instruction.  */
12723           if (CALL_P (i3))
12724             place = i3;
12725           else if (i2 && CALL_P (i2))
12726             place = i2;
12727           else
12728             {
12729               gcc_assert (cfun->can_throw_non_call_exceptions);
12730               if (may_trap_p (i3))
12731                 place = i3;
12732               else if (i2 && may_trap_p (i2))
12733                 place = i2;
12734               /* ??? Otherwise assume we've combined things such that we
12735                  can now prove that the instructions can't trap.  Drop the
12736                  note in this case.  */
12737             }
12738           break;
12739
12740         case REG_NORETURN:
12741         case REG_SETJMP:
12742           /* These notes must remain with the call.  It should not be
12743              possible for both I2 and I3 to be a call.  */
12744           if (CALL_P (i3))
12745             place = i3;
12746           else
12747             {
12748               gcc_assert (i2 && CALL_P (i2));
12749               place = i2;
12750             }
12751           break;
12752
12753         case REG_UNUSED:
12754           /* Any clobbers for i3 may still exist, and so we must process
12755              REG_UNUSED notes from that insn.
12756
12757              Any clobbers from i2 or i1 can only exist if they were added by
12758              recog_for_combine.  In that case, recog_for_combine created the
12759              necessary REG_UNUSED notes.  Trying to keep any original
12760              REG_UNUSED notes from these insns can cause incorrect output
12761              if it is for the same register as the original i3 dest.
12762              In that case, we will notice that the register is set in i3,
12763              and then add a REG_UNUSED note for the destination of i3, which
12764              is wrong.  However, it is possible to have REG_UNUSED notes from
12765              i2 or i1 for register which were both used and clobbered, so
12766              we keep notes from i2 or i1 if they will turn into REG_DEAD
12767              notes.  */
12768
12769           /* If this register is set or clobbered in I3, put the note there
12770              unless there is one already.  */
12771           if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
12772             {
12773               if (from_insn != i3)
12774                 break;
12775
12776               if (! (REG_P (XEXP (note, 0))
12777                      ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
12778                      : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
12779                 place = i3;
12780             }
12781           /* Otherwise, if this register is used by I3, then this register
12782              now dies here, so we must put a REG_DEAD note here unless there
12783              is one already.  */
12784           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
12785                    && ! (REG_P (XEXP (note, 0))
12786                          ? find_regno_note (i3, REG_DEAD,
12787                                             REGNO (XEXP (note, 0)))
12788                          : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
12789             {
12790               PUT_REG_NOTE_KIND (note, REG_DEAD);
12791               place = i3;
12792             }
12793           break;
12794
12795         case REG_EQUAL:
12796         case REG_EQUIV:
12797         case REG_NOALIAS:
12798           /* These notes say something about results of an insn.  We can
12799              only support them if they used to be on I3 in which case they
12800              remain on I3.  Otherwise they are ignored.
12801
12802              If the note refers to an expression that is not a constant, we
12803              must also ignore the note since we cannot tell whether the
12804              equivalence is still true.  It might be possible to do
12805              slightly better than this (we only have a problem if I2DEST
12806              or I1DEST is present in the expression), but it doesn't
12807              seem worth the trouble.  */
12808
12809           if (from_insn == i3
12810               && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
12811             place = i3;
12812           break;
12813
12814         case REG_INC:
12815           /* These notes say something about how a register is used.  They must
12816              be present on any use of the register in I2 or I3.  */
12817           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
12818             place = i3;
12819
12820           if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
12821             {
12822               if (place)
12823                 place2 = i2;
12824               else
12825                 place = i2;
12826             }
12827           break;
12828
12829         case REG_LABEL_TARGET:
12830         case REG_LABEL_OPERAND:
12831           /* This can show up in several ways -- either directly in the
12832              pattern, or hidden off in the constant pool with (or without?)
12833              a REG_EQUAL note.  */
12834           /* ??? Ignore the without-reg_equal-note problem for now.  */
12835           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12836               || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12837                   && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12838                   && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12839             place = i3;
12840
12841           if (i2
12842               && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12843                   || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12844                       && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12845                       && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12846             {
12847               if (place)
12848                 place2 = i2;
12849               else
12850                 place = i2;
12851             }
12852
12853           /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
12854              as a JUMP_LABEL or decrement LABEL_NUSES if it's already
12855              there.  */
12856           if (place && JUMP_P (place)
12857               && REG_NOTE_KIND (note) == REG_LABEL_TARGET
12858               && (JUMP_LABEL (place) == NULL
12859                   || JUMP_LABEL (place) == XEXP (note, 0)))
12860             {
12861               rtx label = JUMP_LABEL (place);
12862
12863               if (!label)
12864                 JUMP_LABEL (place) = XEXP (note, 0);
12865               else if (LABEL_P (label))
12866                 LABEL_NUSES (label)--;
12867             }
12868
12869           if (place2 && JUMP_P (place2)
12870               && REG_NOTE_KIND (note) == REG_LABEL_TARGET
12871               && (JUMP_LABEL (place2) == NULL
12872                   || JUMP_LABEL (place2) == XEXP (note, 0)))
12873             {
12874               rtx label = JUMP_LABEL (place2);
12875
12876               if (!label)
12877                 JUMP_LABEL (place2) = XEXP (note, 0);
12878               else if (LABEL_P (label))
12879                 LABEL_NUSES (label)--;
12880               place2 = 0;
12881             }
12882           break;
12883
12884         case REG_NONNEG:
12885           /* This note says something about the value of a register prior
12886              to the execution of an insn.  It is too much trouble to see
12887              if the note is still correct in all situations.  It is better
12888              to simply delete it.  */
12889           break;
12890
12891         case REG_DEAD:
12892           /* If we replaced the right hand side of FROM_INSN with a
12893              REG_EQUAL note, the original use of the dying register
12894              will not have been combined into I3 and I2.  In such cases,
12895              FROM_INSN is guaranteed to be the first of the combined
12896              instructions, so we simply need to search back before
12897              FROM_INSN for the previous use or set of this register,
12898              then alter the notes there appropriately.
12899
12900              If the register is used as an input in I3, it dies there.
12901              Similarly for I2, if it is nonzero and adjacent to I3.
12902
12903              If the register is not used as an input in either I3 or I2
12904              and it is not one of the registers we were supposed to eliminate,
12905              there are two possibilities.  We might have a non-adjacent I2
12906              or we might have somehow eliminated an additional register
12907              from a computation.  For example, we might have had A & B where
12908              we discover that B will always be zero.  In this case we will
12909              eliminate the reference to A.
12910
12911              In both cases, we must search to see if we can find a previous
12912              use of A and put the death note there.  */
12913
12914           if (from_insn
12915               && from_insn == i2mod
12916               && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
12917             tem = from_insn;
12918           else
12919             {
12920               if (from_insn
12921                   && CALL_P (from_insn)
12922                   && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12923                 place = from_insn;
12924               else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12925                 place = i3;
12926               else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
12927                        && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12928                 place = i2;
12929               else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
12930                         && !(i2mod
12931                              && reg_overlap_mentioned_p (XEXP (note, 0),
12932                                                          i2mod_old_rhs)))
12933                        || rtx_equal_p (XEXP (note, 0), elim_i1))
12934                 break;
12935               tem = i3;
12936             }
12937
12938           if (place == 0)
12939             {
12940               basic_block bb = this_basic_block;
12941
12942               for (tem = PREV_INSN (tem); place == 0; tem = PREV_INSN (tem))
12943                 {
12944                   if (!NONDEBUG_INSN_P (tem))
12945                     {
12946                       if (tem == BB_HEAD (bb))
12947                         break;
12948                       continue;
12949                     }
12950
12951                   /* If the register is being set at TEM, see if that is all
12952                      TEM is doing.  If so, delete TEM.  Otherwise, make this
12953                      into a REG_UNUSED note instead. Don't delete sets to
12954                      global register vars.  */
12955                   if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
12956                        || !global_regs[REGNO (XEXP (note, 0))])
12957                       && reg_set_p (XEXP (note, 0), PATTERN (tem)))
12958                     {
12959                       rtx set = single_set (tem);
12960                       rtx inner_dest = 0;
12961 #ifdef HAVE_cc0
12962                       rtx cc0_setter = NULL_RTX;
12963 #endif
12964
12965                       if (set != 0)
12966                         for (inner_dest = SET_DEST (set);
12967                              (GET_CODE (inner_dest) == STRICT_LOW_PART
12968                               || GET_CODE (inner_dest) == SUBREG
12969                               || GET_CODE (inner_dest) == ZERO_EXTRACT);
12970                              inner_dest = XEXP (inner_dest, 0))
12971                           ;
12972
12973                       /* Verify that it was the set, and not a clobber that
12974                          modified the register.
12975
12976                          CC0 targets must be careful to maintain setter/user
12977                          pairs.  If we cannot delete the setter due to side
12978                          effects, mark the user with an UNUSED note instead
12979                          of deleting it.  */
12980
12981                       if (set != 0 && ! side_effects_p (SET_SRC (set))
12982                           && rtx_equal_p (XEXP (note, 0), inner_dest)
12983 #ifdef HAVE_cc0
12984                           && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12985                               || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12986                                   && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12987 #endif
12988                           )
12989                         {
12990                           /* Move the notes and links of TEM elsewhere.
12991                              This might delete other dead insns recursively.
12992                              First set the pattern to something that won't use
12993                              any register.  */
12994                           rtx old_notes = REG_NOTES (tem);
12995
12996                           PATTERN (tem) = pc_rtx;
12997                           REG_NOTES (tem) = NULL;
12998
12999                           distribute_notes (old_notes, tem, tem, NULL_RTX,
13000                                             NULL_RTX, NULL_RTX);
13001                           distribute_links (LOG_LINKS (tem));
13002
13003                           SET_INSN_DELETED (tem);
13004                           if (tem == i2)
13005                             i2 = NULL_RTX;
13006
13007 #ifdef HAVE_cc0
13008                           /* Delete the setter too.  */
13009                           if (cc0_setter)
13010                             {
13011                               PATTERN (cc0_setter) = pc_rtx;
13012                               old_notes = REG_NOTES (cc0_setter);
13013                               REG_NOTES (cc0_setter) = NULL;
13014
13015                               distribute_notes (old_notes, cc0_setter,
13016                                                 cc0_setter, NULL_RTX,
13017                                                 NULL_RTX, NULL_RTX);
13018                               distribute_links (LOG_LINKS (cc0_setter));
13019
13020                               SET_INSN_DELETED (cc0_setter);
13021                               if (cc0_setter == i2)
13022                                 i2 = NULL_RTX;
13023                             }
13024 #endif
13025                         }
13026                       else
13027                         {
13028                           PUT_REG_NOTE_KIND (note, REG_UNUSED);
13029
13030                           /*  If there isn't already a REG_UNUSED note, put one
13031                               here.  Do not place a REG_DEAD note, even if
13032                               the register is also used here; that would not
13033                               match the algorithm used in lifetime analysis
13034                               and can cause the consistency check in the
13035                               scheduler to fail.  */
13036                           if (! find_regno_note (tem, REG_UNUSED,
13037                                                  REGNO (XEXP (note, 0))))
13038                             place = tem;
13039                           break;
13040                         }
13041                     }
13042                   else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
13043                            || (CALL_P (tem)
13044                                && find_reg_fusage (tem, USE, XEXP (note, 0))))
13045                     {
13046                       place = tem;
13047
13048                       /* If we are doing a 3->2 combination, and we have a
13049                          register which formerly died in i3 and was not used
13050                          by i2, which now no longer dies in i3 and is used in
13051                          i2 but does not die in i2, and place is between i2
13052                          and i3, then we may need to move a link from place to
13053                          i2.  */
13054                       if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13055                           && from_insn
13056                           && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13057                           && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13058                         {
13059                           rtx links = LOG_LINKS (place);
13060                           LOG_LINKS (place) = 0;
13061                           distribute_links (links);
13062                         }
13063                       break;
13064                     }
13065
13066                   if (tem == BB_HEAD (bb))
13067                     break;
13068                 }
13069
13070             }
13071
13072           /* If the register is set or already dead at PLACE, we needn't do
13073              anything with this note if it is still a REG_DEAD note.
13074              We check here if it is set at all, not if is it totally replaced,
13075              which is what `dead_or_set_p' checks, so also check for it being
13076              set partially.  */
13077
13078           if (place && REG_NOTE_KIND (note) == REG_DEAD)
13079             {
13080               unsigned int regno = REGNO (XEXP (note, 0));
13081               reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
13082
13083               if (dead_or_set_p (place, XEXP (note, 0))
13084                   || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13085                 {
13086                   /* Unless the register previously died in PLACE, clear
13087                      last_death.  [I no longer understand why this is
13088                      being done.] */
13089                   if (rsp->last_death != place)
13090                     rsp->last_death = 0;
13091                   place = 0;
13092                 }
13093               else
13094                 rsp->last_death = place;
13095
13096               /* If this is a death note for a hard reg that is occupying
13097                  multiple registers, ensure that we are still using all
13098                  parts of the object.  If we find a piece of the object
13099                  that is unused, we must arrange for an appropriate REG_DEAD
13100                  note to be added for it.  However, we can't just emit a USE
13101                  and tag the note to it, since the register might actually
13102                  be dead; so we recourse, and the recursive call then finds
13103                  the previous insn that used this register.  */
13104
13105               if (place && regno < FIRST_PSEUDO_REGISTER
13106                   && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13107                 {
13108                   unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13109                   int all_used = 1;
13110                   unsigned int i;
13111
13112                   for (i = regno; i < endregno; i++)
13113                     if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13114                          && ! find_regno_fusage (place, USE, i))
13115                         || dead_or_set_regno_p (place, i))
13116                       all_used = 0;
13117
13118                   if (! all_used)
13119                     {
13120                       /* Put only REG_DEAD notes for pieces that are
13121                          not already dead or set.  */
13122
13123                       for (i = regno; i < endregno;
13124                            i += hard_regno_nregs[i][reg_raw_mode[i]])
13125                         {
13126                           rtx piece = regno_reg_rtx[i];
13127                           basic_block bb = this_basic_block;
13128
13129                           if (! dead_or_set_p (place, piece)
13130                               && ! reg_bitfield_target_p (piece,
13131                                                           PATTERN (place)))
13132                             {
13133                               rtx new_note = alloc_reg_note (REG_DEAD, piece,
13134                                                              NULL_RTX);
13135
13136                               distribute_notes (new_note, place, place,
13137                                                 NULL_RTX, NULL_RTX, NULL_RTX);
13138                             }
13139                           else if (! refers_to_regno_p (i, i + 1,
13140                                                         PATTERN (place), 0)
13141                                    && ! find_regno_fusage (place, USE, i))
13142                             for (tem = PREV_INSN (place); ;
13143                                  tem = PREV_INSN (tem))
13144                               {
13145                                 if (!NONDEBUG_INSN_P (tem))
13146                                   {
13147                                     if (tem == BB_HEAD (bb))
13148                                       break;
13149                                     continue;
13150                                   }
13151                                 if (dead_or_set_p (tem, piece)
13152                                     || reg_bitfield_target_p (piece,
13153                                                               PATTERN (tem)))
13154                                   {
13155                                     add_reg_note (tem, REG_UNUSED, piece);
13156                                     break;
13157                                   }
13158                               }
13159
13160                         }
13161
13162                       place = 0;
13163                     }
13164                 }
13165             }
13166           break;
13167
13168         default:
13169           /* Any other notes should not be present at this point in the
13170              compilation.  */
13171           gcc_unreachable ();
13172         }
13173
13174       if (place)
13175         {
13176           XEXP (note, 1) = REG_NOTES (place);
13177           REG_NOTES (place) = note;
13178         }
13179
13180       if (place2)
13181         add_reg_note (place2, REG_NOTE_KIND (note), XEXP (note, 0));
13182     }
13183 }
13184 \f
13185 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13186    I3, I2, and I1 to new locations.  This is also called to add a link
13187    pointing at I3 when I3's destination is changed.  */
13188
13189 static void
13190 distribute_links (rtx links)
13191 {
13192   rtx link, next_link;
13193
13194   for (link = links; link; link = next_link)
13195     {
13196       rtx place = 0;
13197       rtx insn;
13198       rtx set, reg;
13199
13200       next_link = XEXP (link, 1);
13201
13202       /* If the insn that this link points to is a NOTE or isn't a single
13203          set, ignore it.  In the latter case, it isn't clear what we
13204          can do other than ignore the link, since we can't tell which
13205          register it was for.  Such links wouldn't be used by combine
13206          anyway.
13207
13208          It is not possible for the destination of the target of the link to
13209          have been changed by combine.  The only potential of this is if we
13210          replace I3, I2, and I1 by I3 and I2.  But in that case the
13211          destination of I2 also remains unchanged.  */
13212
13213       if (NOTE_P (XEXP (link, 0))
13214           || (set = single_set (XEXP (link, 0))) == 0)
13215         continue;
13216
13217       reg = SET_DEST (set);
13218       while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
13219              || GET_CODE (reg) == STRICT_LOW_PART)
13220         reg = XEXP (reg, 0);
13221
13222       /* A LOG_LINK is defined as being placed on the first insn that uses
13223          a register and points to the insn that sets the register.  Start
13224          searching at the next insn after the target of the link and stop
13225          when we reach a set of the register or the end of the basic block.
13226
13227          Note that this correctly handles the link that used to point from
13228          I3 to I2.  Also note that not much searching is typically done here
13229          since most links don't point very far away.  */
13230
13231       for (insn = NEXT_INSN (XEXP (link, 0));
13232            (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
13233                      || BB_HEAD (this_basic_block->next_bb) != insn));
13234            insn = NEXT_INSN (insn))
13235         if (DEBUG_INSN_P (insn))
13236           continue;
13237         else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13238           {
13239             if (reg_referenced_p (reg, PATTERN (insn)))
13240               place = insn;
13241             break;
13242           }
13243         else if (CALL_P (insn)
13244                  && find_reg_fusage (insn, USE, reg))
13245           {
13246             place = insn;
13247             break;
13248           }
13249         else if (INSN_P (insn) && reg_set_p (reg, insn))
13250           break;
13251
13252       /* If we found a place to put the link, place it there unless there
13253          is already a link to the same insn as LINK at that point.  */
13254
13255       if (place)
13256         {
13257           rtx link2;
13258
13259           for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
13260             if (XEXP (link2, 0) == XEXP (link, 0))
13261               break;
13262
13263           if (link2 == 0)
13264             {
13265               XEXP (link, 1) = LOG_LINKS (place);
13266               LOG_LINKS (place) = link;
13267
13268               /* Set added_links_insn to the earliest insn we added a
13269                  link to.  */
13270               if (added_links_insn == 0
13271                   || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
13272                 added_links_insn = place;
13273             }
13274         }
13275     }
13276 }
13277 \f
13278 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
13279    Check whether the expression pointer to by LOC is a register or
13280    memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
13281    Otherwise return zero.  */
13282
13283 static int
13284 unmentioned_reg_p_1 (rtx *loc, void *expr)
13285 {
13286   rtx x = *loc;
13287
13288   if (x != NULL_RTX
13289       && (REG_P (x) || MEM_P (x))
13290       && ! reg_mentioned_p (x, (rtx) expr))
13291     return 1;
13292   return 0;
13293 }
13294
13295 /* Check for any register or memory mentioned in EQUIV that is not
13296    mentioned in EXPR.  This is used to restrict EQUIV to "specializations"
13297    of EXPR where some registers may have been replaced by constants.  */
13298
13299 static bool
13300 unmentioned_reg_p (rtx equiv, rtx expr)
13301 {
13302   return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
13303 }
13304 \f
13305 void
13306 dump_combine_stats (FILE *file)
13307 {
13308   fprintf
13309     (file,
13310      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13311      combine_attempts, combine_merges, combine_extras, combine_successes);
13312 }
13313
13314 void
13315 dump_combine_total_stats (FILE *file)
13316 {
13317   fprintf
13318     (file,
13319      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13320      total_attempts, total_merges, total_extras, total_successes);
13321 }
13322 \f
13323 static bool
13324 gate_handle_combine (void)
13325 {
13326   return (optimize > 0);
13327 }
13328
13329 /* Try combining insns through substitution.  */
13330 static unsigned int
13331 rest_of_handle_combine (void)
13332 {
13333   int rebuild_jump_labels_after_combine;
13334
13335   df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
13336   df_note_add_problem ();
13337   df_analyze ();
13338
13339   regstat_init_n_sets_and_refs ();
13340
13341   rebuild_jump_labels_after_combine
13342     = combine_instructions (get_insns (), max_reg_num ());
13343
13344   /* Combining insns may have turned an indirect jump into a
13345      direct jump.  Rebuild the JUMP_LABEL fields of jumping
13346      instructions.  */
13347   if (rebuild_jump_labels_after_combine)
13348     {
13349       timevar_push (TV_JUMP);
13350       rebuild_jump_labels (get_insns ());
13351       cleanup_cfg (0);
13352       timevar_pop (TV_JUMP);
13353     }
13354
13355   regstat_free_n_sets_and_refs ();
13356   return 0;
13357 }
13358
13359 struct rtl_opt_pass pass_combine =
13360 {
13361  {
13362   RTL_PASS,
13363   "combine",                            /* name */
13364   gate_handle_combine,                  /* gate */
13365   rest_of_handle_combine,               /* execute */
13366   NULL,                                 /* sub */
13367   NULL,                                 /* next */
13368   0,                                    /* static_pass_number */
13369   TV_COMBINE,                           /* tv_id */
13370   PROP_cfglayout,                       /* properties_required */
13371   0,                                    /* properties_provided */
13372   0,                                    /* properties_destroyed */
13373   0,                                    /* todo_flags_start */
13374   TODO_dump_func |
13375   TODO_df_finish | TODO_verify_rtl_sharing |
13376   TODO_ggc_collect,                     /* todo_flags_finish */
13377  }
13378 };