OSDN Git Service

ca378bd1c897bb7263b7c422f4908a1f7201b2d7
[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
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 "real.h"
96 #include "toplev.h"
97 #include "target.h"
98 #include "optabs.h"
99 #include "insn-codes.h"
100 #include "rtlhooks-def.h"
101 /* Include output.h for dump_file.  */
102 #include "output.h"
103 #include "params.h"
104 #include "timevar.h"
105 #include "tree-pass.h"
106 #include "df.h"
107 #include "cgraph.h"
108
109 /* Number of attempts to combine instructions in this function.  */
110
111 static int combine_attempts;
112
113 /* Number of attempts that got as far as substitution in this function.  */
114
115 static int combine_merges;
116
117 /* Number of instructions combined with added SETs in this function.  */
118
119 static int combine_extras;
120
121 /* Number of instructions combined in this function.  */
122
123 static int combine_successes;
124
125 /* Totals over entire compilation.  */
126
127 static int total_attempts, total_merges, total_extras, total_successes;
128
129 /* combine_instructions may try to replace the right hand side of the
130    second instruction with the value of an associated REG_EQUAL note
131    before throwing it at try_combine.  That is problematic when there
132    is a REG_DEAD note for a register used in the old right hand side
133    and can cause distribute_notes to do wrong things.  This is the
134    second instruction if it has been so modified, null otherwise.  */
135
136 static rtx i2mod;
137
138 /* When I2MOD is nonnull, this is a copy of the old right hand side.  */
139
140 static rtx i2mod_old_rhs;
141
142 /* When I2MOD is nonnull, this is a copy of the new right hand side.  */
143
144 static rtx i2mod_new_rhs;
145 \f
146 typedef struct reg_stat_struct {
147   /* Record last point of death of (hard or pseudo) register n.  */
148   rtx                           last_death;
149
150   /* Record last point of modification of (hard or pseudo) register n.  */
151   rtx                           last_set;
152
153   /* The next group of fields allows the recording of the last value assigned
154      to (hard or pseudo) register n.  We use this information to see if an
155      operation being processed is redundant given a prior operation performed
156      on the register.  For example, an `and' with a constant is redundant if
157      all the zero bits are already known to be turned off.
158
159      We use an approach similar to that used by cse, but change it in the
160      following ways:
161
162      (1) We do not want to reinitialize at each label.
163      (2) It is useful, but not critical, to know the actual value assigned
164          to a register.  Often just its form is helpful.
165
166      Therefore, we maintain the following fields:
167
168      last_set_value             the last value assigned
169      last_set_label             records the value of label_tick when the
170                                 register was assigned
171      last_set_table_tick        records the value of label_tick when a
172                                 value using the register is assigned
173      last_set_invalid           set to nonzero when it is not valid
174                                 to use the value of this register in some
175                                 register's value
176
177      To understand the usage of these tables, it is important to understand
178      the distinction between the value in last_set_value being valid and
179      the register being validly contained in some other expression in the
180      table.
181
182      (The next two parameters are out of date).
183
184      reg_stat[i].last_set_value is valid if it is nonzero, and either
185      reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
186
187      Register I may validly appear in any expression returned for the value
188      of another register if reg_n_sets[i] is 1.  It may also appear in the
189      value for register J if reg_stat[j].last_set_invalid is zero, or
190      reg_stat[i].last_set_label < reg_stat[j].last_set_label.
191
192      If an expression is found in the table containing a register which may
193      not validly appear in an expression, the register is replaced by
194      something that won't match, (clobber (const_int 0)).  */
195
196   /* Record last value assigned to (hard or pseudo) register n.  */
197
198   rtx                           last_set_value;
199
200   /* Record the value of label_tick when an expression involving register n
201      is placed in last_set_value.  */
202
203   int                           last_set_table_tick;
204
205   /* Record the value of label_tick when the value for register n is placed in
206      last_set_value.  */
207
208   int                           last_set_label;
209
210   /* These fields are maintained in parallel with last_set_value and are
211      used to store the mode in which the register was last set, the bits
212      that were known to be zero when it was last set, and the number of
213      sign bits copies it was known to have when it was last set.  */
214
215   unsigned HOST_WIDE_INT        last_set_nonzero_bits;
216   char                          last_set_sign_bit_copies;
217   ENUM_BITFIELD(machine_mode)   last_set_mode : 8;
218
219   /* Set nonzero if references to register n in expressions should not be
220      used.  last_set_invalid is set nonzero when this register is being
221      assigned to and last_set_table_tick == label_tick.  */
222
223   char                          last_set_invalid;
224
225   /* Some registers that are set more than once and used in more than one
226      basic block are nevertheless always set in similar ways.  For example,
227      a QImode register may be loaded from memory in two places on a machine
228      where byte loads zero extend.
229
230      We record in the following fields if a register has some leading bits
231      that are always equal to the sign bit, and what we know about the
232      nonzero bits of a register, specifically which bits are known to be
233      zero.
234
235      If an entry is zero, it means that we don't know anything special.  */
236
237   unsigned char                 sign_bit_copies;
238
239   unsigned HOST_WIDE_INT        nonzero_bits;
240
241   /* Record the value of the label_tick when the last truncation
242      happened.  The field truncated_to_mode is only valid if
243      truncation_label == label_tick.  */
244
245   int                           truncation_label;
246
247   /* Record the last truncation seen for this register.  If truncation
248      is not a nop to this mode we might be able to save an explicit
249      truncation if we know that value already contains a truncated
250      value.  */
251
252   ENUM_BITFIELD(machine_mode)   truncated_to_mode : 8;
253 } reg_stat_type;
254
255 DEF_VEC_O(reg_stat_type);
256 DEF_VEC_ALLOC_O(reg_stat_type,heap);
257
258 static VEC(reg_stat_type,heap) *reg_stat;
259
260 /* Record the luid of the last insn that invalidated memory
261    (anything that writes memory, and subroutine calls, but not pushes).  */
262
263 static int mem_last_set;
264
265 /* Record the luid of the last CALL_INSN
266    so we can tell whether a potential combination crosses any calls.  */
267
268 static int last_call_luid;
269
270 /* When `subst' is called, this is the insn that is being modified
271    (by combining in a previous insn).  The PATTERN of this insn
272    is still the old pattern partially modified and it should not be
273    looked at, but this may be used to examine the successors of the insn
274    to judge whether a simplification is valid.  */
275
276 static rtx subst_insn;
277
278 /* This is the lowest LUID that `subst' is currently dealing with.
279    get_last_value will not return a value if the register was set at or
280    after this LUID.  If not for this mechanism, we could get confused if
281    I2 or I1 in try_combine were an insn that used the old value of a register
282    to obtain a new value.  In that case, we might erroneously get the
283    new value of the register when we wanted the old one.  */
284
285 static int subst_low_luid;
286
287 /* This contains any hard registers that are used in newpat; reg_dead_at_p
288    must consider all these registers to be always live.  */
289
290 static HARD_REG_SET newpat_used_regs;
291
292 /* This is an insn to which a LOG_LINKS entry has been added.  If this
293    insn is the earlier than I2 or I3, combine should rescan starting at
294    that location.  */
295
296 static rtx added_links_insn;
297
298 /* Basic block in which we are performing combines.  */
299 static basic_block this_basic_block;
300
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 label.  */
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 struct undo
345 {
346   struct undo *next;
347   enum { UNDO_RTX, UNDO_INT, UNDO_MODE } kind;
348   union { rtx r; int i; enum machine_mode m; } old_contents;
349   union { rtx *r; int *i; } where;
350 };
351
352 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
353    num_undo says how many are currently recorded.
354
355    other_insn is nonzero if we have modified some other insn in the process
356    of working on subst_insn.  It must be verified too.  */
357
358 struct undobuf
359 {
360   struct undo *undos;
361   struct undo *frees;
362   rtx other_insn;
363 };
364
365 static struct undobuf undobuf;
366
367 /* Number of times the pseudo being substituted for
368    was found and replaced.  */
369
370 static int n_occurrences;
371
372 static rtx reg_nonzero_bits_for_combine (const_rtx, enum machine_mode, const_rtx,
373                                          enum machine_mode,
374                                          unsigned HOST_WIDE_INT,
375                                          unsigned HOST_WIDE_INT *);
376 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, enum machine_mode, const_rtx,
377                                                 enum machine_mode,
378                                                 unsigned int, unsigned int *);
379 static void do_SUBST (rtx *, rtx);
380 static void do_SUBST_INT (int *, int);
381 static void init_reg_last (void);
382 static void setup_incoming_promotions (rtx);
383 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
384 static int cant_combine_insn_p (rtx);
385 static int can_combine_p (rtx, rtx, rtx, rtx, rtx *, rtx *);
386 static int combinable_i3pat (rtx, rtx *, rtx, rtx, int, rtx *);
387 static int contains_muldiv (rtx);
388 static rtx try_combine (rtx, rtx, rtx, int *);
389 static void undo_all (void);
390 static void undo_commit (void);
391 static rtx *find_split_point (rtx *, rtx);
392 static rtx subst (rtx, rtx, rtx, int, int);
393 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
394 static rtx simplify_if_then_else (rtx);
395 static rtx simplify_set (rtx);
396 static rtx simplify_logical (rtx);
397 static rtx expand_compound_operation (rtx);
398 static const_rtx expand_field_assignment (const_rtx);
399 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
400                             rtx, unsigned HOST_WIDE_INT, int, int, int);
401 static rtx extract_left_shift (rtx, int);
402 static rtx make_compound_operation (rtx, enum rtx_code);
403 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
404                               unsigned HOST_WIDE_INT *);
405 static rtx canon_reg_for_combine (rtx, rtx);
406 static rtx force_to_mode (rtx, enum machine_mode,
407                           unsigned HOST_WIDE_INT, int);
408 static rtx if_then_else_cond (rtx, rtx *, rtx *);
409 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
410 static int rtx_equal_for_field_assignment_p (rtx, rtx);
411 static rtx make_field_assignment (rtx);
412 static rtx apply_distributive_law (rtx);
413 static rtx distribute_and_simplify_rtx (rtx, int);
414 static rtx simplify_and_const_int_1 (enum machine_mode, rtx,
415                                      unsigned HOST_WIDE_INT);
416 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
417                                    unsigned HOST_WIDE_INT);
418 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
419                             HOST_WIDE_INT, enum machine_mode, int *);
420 static rtx simplify_shift_const_1 (enum rtx_code, enum machine_mode, rtx, int);
421 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
422                                  int);
423 static int recog_for_combine (rtx *, rtx, rtx *);
424 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
425 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
426 static void update_table_tick (rtx);
427 static void record_value_for_reg (rtx, rtx, rtx);
428 static void check_conversions (rtx, rtx);
429 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
430 static void record_dead_and_set_regs (rtx);
431 static int get_last_value_validate (rtx *, rtx, int, int);
432 static rtx get_last_value (const_rtx);
433 static int use_crosses_set_p (const_rtx, int);
434 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
435 static int reg_dead_at_p (rtx, rtx);
436 static void move_deaths (rtx, rtx, int, rtx, rtx *);
437 static int reg_bitfield_target_p (rtx, rtx);
438 static void distribute_notes (rtx, rtx, rtx, rtx, rtx, rtx);
439 static void distribute_links (rtx);
440 static void mark_used_regs_combine (rtx);
441 static void record_promoted_value (rtx, rtx);
442 static int unmentioned_reg_p_1 (rtx *, void *);
443 static bool unmentioned_reg_p (rtx, rtx);
444 static void record_truncated_value (rtx);
445 static bool reg_truncated_to_mode (enum machine_mode, const_rtx);
446 static rtx gen_lowpart_or_truncate (enum machine_mode, rtx);
447 \f
448
449 /* It is not safe to use ordinary gen_lowpart in combine.
450    See comments in gen_lowpart_for_combine.  */
451 #undef RTL_HOOKS_GEN_LOWPART
452 #define RTL_HOOKS_GEN_LOWPART              gen_lowpart_for_combine
453
454 /* Our implementation of gen_lowpart never emits a new pseudo.  */
455 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
456 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT      gen_lowpart_for_combine
457
458 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
459 #define RTL_HOOKS_REG_NONZERO_REG_BITS     reg_nonzero_bits_for_combine
460
461 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
462 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES  reg_num_sign_bit_copies_for_combine
463
464 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
465 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE    reg_truncated_to_mode
466
467 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
468
469 \f
470 /* Try to split PATTERN found in INSN.  This returns NULL_RTX if
471    PATTERN can not be split.  Otherwise, it returns an insn sequence.
472    This is a wrapper around split_insns which ensures that the
473    reg_stat vector is made larger if the splitter creates a new
474    register.  */
475
476 static rtx
477 combine_split_insns (rtx pattern, rtx insn)
478 {
479   rtx ret;
480   unsigned int nregs;
481
482   ret = split_insns (pattern, insn);
483   nregs = max_reg_num ();
484   if (nregs > VEC_length (reg_stat_type, reg_stat))
485     VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
486   return ret;
487 }
488
489 /* This is used by find_single_use to locate an rtx in LOC that
490    contains exactly one use of DEST, which is typically either a REG
491    or CC0.  It returns a pointer to the innermost rtx expression
492    containing DEST.  Appearances of DEST that are being used to
493    totally replace it are not counted.  */
494
495 static rtx *
496 find_single_use_1 (rtx dest, rtx *loc)
497 {
498   rtx x = *loc;
499   enum rtx_code code = GET_CODE (x);
500   rtx *result = NULL;
501   rtx *this_result;
502   int i;
503   const char *fmt;
504
505   switch (code)
506     {
507     case CONST_INT:
508     case CONST:
509     case LABEL_REF:
510     case SYMBOL_REF:
511     case CONST_DOUBLE:
512     case CONST_VECTOR:
513     case CLOBBER:
514       return 0;
515
516     case SET:
517       /* If the destination is anything other than CC0, PC, a REG or a SUBREG
518          of a REG that occupies all of the REG, the insn uses DEST if
519          it is mentioned in the destination or the source.  Otherwise, we
520          need just check the source.  */
521       if (GET_CODE (SET_DEST (x)) != CC0
522           && GET_CODE (SET_DEST (x)) != PC
523           && !REG_P (SET_DEST (x))
524           && ! (GET_CODE (SET_DEST (x)) == SUBREG
525                 && REG_P (SUBREG_REG (SET_DEST (x)))
526                 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
527                       + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
528                     == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
529                          + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
530         break;
531
532       return find_single_use_1 (dest, &SET_SRC (x));
533
534     case MEM:
535     case SUBREG:
536       return find_single_use_1 (dest, &XEXP (x, 0));
537
538     default:
539       break;
540     }
541
542   /* If it wasn't one of the common cases above, check each expression and
543      vector of this code.  Look for a unique usage of DEST.  */
544
545   fmt = GET_RTX_FORMAT (code);
546   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
547     {
548       if (fmt[i] == 'e')
549         {
550           if (dest == XEXP (x, i)
551               || (REG_P (dest) && REG_P (XEXP (x, i))
552                   && REGNO (dest) == REGNO (XEXP (x, i))))
553             this_result = loc;
554           else
555             this_result = find_single_use_1 (dest, &XEXP (x, i));
556
557           if (result == NULL)
558             result = this_result;
559           else if (this_result)
560             /* Duplicate usage.  */
561             return NULL;
562         }
563       else if (fmt[i] == 'E')
564         {
565           int j;
566
567           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
568             {
569               if (XVECEXP (x, i, j) == dest
570                   || (REG_P (dest)
571                       && REG_P (XVECEXP (x, i, j))
572                       && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
573                 this_result = loc;
574               else
575                 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
576
577               if (result == NULL)
578                 result = this_result;
579               else if (this_result)
580                 return NULL;
581             }
582         }
583     }
584
585   return result;
586 }
587
588
589 /* See if DEST, produced in INSN, is used only a single time in the
590    sequel.  If so, return a pointer to the innermost rtx expression in which
591    it is used.
592
593    If PLOC is nonzero, *PLOC is set to the insn containing the single use.
594
595    If DEST is cc0_rtx, we look only at the next insn.  In that case, we don't
596    care about REG_DEAD notes or LOG_LINKS.
597
598    Otherwise, we find the single use by finding an insn that has a
599    LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST.  If DEST is
600    only referenced once in that insn, we know that it must be the first
601    and last insn referencing DEST.  */
602
603 static rtx *
604 find_single_use (rtx dest, rtx insn, rtx *ploc)
605 {
606   rtx next;
607   rtx *result;
608   rtx link;
609
610 #ifdef HAVE_cc0
611   if (dest == cc0_rtx)
612     {
613       next = NEXT_INSN (insn);
614       if (next == 0
615           || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
616         return 0;
617
618       result = find_single_use_1 (dest, &PATTERN (next));
619       if (result && ploc)
620         *ploc = next;
621       return result;
622     }
623 #endif
624
625   if (!REG_P (dest))
626     return 0;
627
628   for (next = next_nonnote_insn (insn);
629        next != 0 && !LABEL_P (next);
630        next = next_nonnote_insn (next))
631     if (INSN_P (next) && dead_or_set_p (next, dest))
632       {
633         for (link = LOG_LINKS (next); link; link = XEXP (link, 1))
634           if (XEXP (link, 0) == insn)
635             break;
636
637         if (link)
638           {
639             result = find_single_use_1 (dest, &PATTERN (next));
640             if (ploc)
641               *ploc = next;
642             return result;
643           }
644       }
645
646   return 0;
647 }
648 \f
649 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
650    insn.  The substitution can be undone by undo_all.  If INTO is already
651    set to NEWVAL, do not record this change.  Because computing NEWVAL might
652    also call SUBST, we have to compute it before we put anything into
653    the undo table.  */
654
655 static void
656 do_SUBST (rtx *into, rtx newval)
657 {
658   struct undo *buf;
659   rtx oldval = *into;
660
661   if (oldval == newval)
662     return;
663
664   /* We'd like to catch as many invalid transformations here as
665      possible.  Unfortunately, there are way too many mode changes
666      that are perfectly valid, so we'd waste too much effort for
667      little gain doing the checks here.  Focus on catching invalid
668      transformations involving integer constants.  */
669   if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
670       && GET_CODE (newval) == CONST_INT)
671     {
672       /* Sanity check that we're replacing oldval with a CONST_INT
673          that is a valid sign-extension for the original mode.  */
674       gcc_assert (INTVAL (newval)
675                   == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
676
677       /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
678          CONST_INT is not valid, because after the replacement, the
679          original mode would be gone.  Unfortunately, we can't tell
680          when do_SUBST is called to replace the operand thereof, so we
681          perform this test on oldval instead, checking whether an
682          invalid replacement took place before we got here.  */
683       gcc_assert (!(GET_CODE (oldval) == SUBREG
684                     && GET_CODE (SUBREG_REG (oldval)) == CONST_INT));
685       gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
686                     && GET_CODE (XEXP (oldval, 0)) == CONST_INT));
687     }
688
689   if (undobuf.frees)
690     buf = undobuf.frees, undobuf.frees = buf->next;
691   else
692     buf = XNEW (struct undo);
693
694   buf->kind = UNDO_RTX;
695   buf->where.r = into;
696   buf->old_contents.r = oldval;
697   *into = newval;
698
699   buf->next = undobuf.undos, undobuf.undos = buf;
700 }
701
702 #define SUBST(INTO, NEWVAL)     do_SUBST(&(INTO), (NEWVAL))
703
704 /* Similar to SUBST, but NEWVAL is an int expression.  Note that substitution
705    for the value of a HOST_WIDE_INT value (including CONST_INT) is
706    not safe.  */
707
708 static void
709 do_SUBST_INT (int *into, int newval)
710 {
711   struct undo *buf;
712   int oldval = *into;
713
714   if (oldval == newval)
715     return;
716
717   if (undobuf.frees)
718     buf = undobuf.frees, undobuf.frees = buf->next;
719   else
720     buf = XNEW (struct undo);
721
722   buf->kind = UNDO_INT;
723   buf->where.i = into;
724   buf->old_contents.i = oldval;
725   *into = newval;
726
727   buf->next = undobuf.undos, undobuf.undos = buf;
728 }
729
730 #define SUBST_INT(INTO, NEWVAL)  do_SUBST_INT(&(INTO), (NEWVAL))
731
732 /* Similar to SUBST, but just substitute the mode.  This is used when
733    changing the mode of a pseudo-register, so that any other
734    references to the entry in the regno_reg_rtx array will change as
735    well.  */
736
737 static void
738 do_SUBST_MODE (rtx *into, enum machine_mode newval)
739 {
740   struct undo *buf;
741   enum machine_mode oldval = GET_MODE (*into);
742
743   if (oldval == newval)
744     return;
745
746   if (undobuf.frees)
747     buf = undobuf.frees, undobuf.frees = buf->next;
748   else
749     buf = XNEW (struct undo);
750
751   buf->kind = UNDO_MODE;
752   buf->where.r = into;
753   buf->old_contents.m = oldval;
754   PUT_MODE (*into, newval);
755
756   buf->next = undobuf.undos, undobuf.undos = buf;
757 }
758
759 #define SUBST_MODE(INTO, NEWVAL)  do_SUBST_MODE(&(INTO), (NEWVAL))
760 \f
761 /* Subroutine of try_combine.  Determine whether the combine replacement
762    patterns NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to
763    insn_rtx_cost that the original instruction sequence I1, I2, I3 and
764    undobuf.other_insn.  Note that I1 and/or NEWI2PAT may be NULL_RTX. 
765    NEWOTHERPAT and undobuf.other_insn may also both be NULL_RTX.  This
766    function returns false, if the costs of all instructions can be
767    estimated, and the replacements are more expensive than the original
768    sequence.  */
769
770 static bool
771 combine_validate_cost (rtx i1, rtx i2, rtx i3, rtx newpat, rtx newi2pat,
772                        rtx newotherpat)
773 {
774   int i1_cost, i2_cost, i3_cost;
775   int new_i2_cost, new_i3_cost;
776   int old_cost, new_cost;
777
778   /* Lookup the original insn_rtx_costs.  */
779   i2_cost = INSN_COST (i2);
780   i3_cost = INSN_COST (i3);
781
782   if (i1)
783     {
784       i1_cost = INSN_COST (i1);
785       old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0)
786                  ? i1_cost + i2_cost + i3_cost : 0;
787     }
788   else
789     {
790       old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
791       i1_cost = 0;
792     }
793
794   /* Calculate the replacement insn_rtx_costs.  */
795   new_i3_cost = insn_rtx_cost (newpat);
796   if (newi2pat)
797     {
798       new_i2_cost = insn_rtx_cost (newi2pat);
799       new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
800                  ? new_i2_cost + new_i3_cost : 0;
801     }
802   else
803     {
804       new_cost = new_i3_cost;
805       new_i2_cost = 0;
806     }
807
808   if (undobuf.other_insn)
809     {
810       int old_other_cost, new_other_cost;
811
812       old_other_cost = INSN_COST (undobuf.other_insn);
813       new_other_cost = insn_rtx_cost (newotherpat);
814       if (old_other_cost > 0 && new_other_cost > 0)
815         {
816           old_cost += old_other_cost;
817           new_cost += new_other_cost;
818         }
819       else
820         old_cost = 0;
821     }
822
823   /* Disallow this recombination if both new_cost and old_cost are
824      greater than zero, and new_cost is greater than old cost.  */
825   if (old_cost > 0
826       && new_cost > old_cost)
827     {
828       if (dump_file)
829         {
830           if (i1)
831             {
832               fprintf (dump_file,
833                        "rejecting combination of insns %d, %d and %d\n",
834                        INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
835               fprintf (dump_file, "original costs %d + %d + %d = %d\n",
836                        i1_cost, i2_cost, i3_cost, old_cost);
837             }
838           else
839             {
840               fprintf (dump_file,
841                        "rejecting combination of insns %d and %d\n",
842                        INSN_UID (i2), INSN_UID (i3));
843               fprintf (dump_file, "original costs %d + %d = %d\n",
844                        i2_cost, i3_cost, old_cost);
845             }
846
847           if (newi2pat)
848             {
849               fprintf (dump_file, "replacement costs %d + %d = %d\n",
850                        new_i2_cost, new_i3_cost, new_cost);
851             }
852           else
853             fprintf (dump_file, "replacement cost %d\n", new_cost);
854         }
855
856       return false;
857     }
858
859   /* Update the uid_insn_cost array with the replacement costs.  */
860   INSN_COST (i2) = new_i2_cost;
861   INSN_COST (i3) = new_i3_cost;
862   if (i1)
863     INSN_COST (i1) = 0;
864
865   return true;
866 }
867
868
869 /* Delete any insns that copy a register to itself.  */
870
871 static void
872 delete_noop_moves (void)
873 {
874   rtx insn, next;
875   basic_block bb;
876
877   FOR_EACH_BB (bb)
878     {
879       for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
880         {
881           next = NEXT_INSN (insn);
882           if (INSN_P (insn) && noop_move_p (insn))
883             {
884               rtx note;
885
886               /* If we're about to remove the first insn of a libcall
887                  then move the libcall note to the next real insn and
888                  update the retval note.  */
889               if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX))
890                        && XEXP (note, 0) != insn)
891                 {
892                   rtx new_libcall_insn = next_real_insn (insn);
893                   rtx retval_note = find_reg_note (XEXP (note, 0),
894                                                    REG_RETVAL, NULL_RTX);
895                   REG_NOTES (new_libcall_insn)
896                     = gen_rtx_INSN_LIST (REG_LIBCALL, XEXP (note, 0),
897                                          REG_NOTES (new_libcall_insn));
898                   XEXP (retval_note, 0) = new_libcall_insn;
899                 }
900
901               if (dump_file)
902                 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
903
904               delete_insn_and_edges (insn);
905             }
906         }
907     }
908 }
909
910 \f
911 /* Fill in log links field for all insns.  */
912
913 static void
914 create_log_links (void)
915 {
916   basic_block bb;
917   rtx *next_use, insn;
918   struct df_ref **def_vec, **use_vec;
919
920   next_use = XCNEWVEC (rtx, max_reg_num ());
921
922   /* Pass through each block from the end, recording the uses of each
923      register and establishing log links when def is encountered.
924      Note that we do not clear next_use array in order to save time,
925      so we have to test whether the use is in the same basic block as def.
926               
927      There are a few cases below when we do not consider the definition or
928      usage -- these are taken from original flow.c did. Don't ask me why it is
929      done this way; I don't know and if it works, I don't want to know.  */
930
931   FOR_EACH_BB (bb)
932     {
933       FOR_BB_INSNS_REVERSE (bb, insn)
934         {
935           if (!INSN_P (insn))
936             continue;
937
938           /* Log links are created only once.  */
939           gcc_assert (!LOG_LINKS (insn));
940
941           for (def_vec = DF_INSN_DEFS (insn); *def_vec; def_vec++)
942             {
943               struct df_ref *def = *def_vec;
944               int regno = DF_REF_REGNO (def);
945               rtx use_insn;
946
947               if (!next_use[regno])
948                 continue;
949
950               /* Do not consider if it is pre/post modification in MEM.  */
951               if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
952                 continue;
953
954               /* Do not make the log link for frame pointer.  */
955               if ((regno == FRAME_POINTER_REGNUM
956                    && (! reload_completed || frame_pointer_needed))
957 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
958                   || (regno == HARD_FRAME_POINTER_REGNUM
959                       && (! reload_completed || frame_pointer_needed))
960 #endif
961 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
962                   || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
963 #endif
964                   )
965                 continue;
966
967               use_insn = next_use[regno];
968               if (BLOCK_FOR_INSN (use_insn) == bb)
969                 {
970                   /* flow.c claimed:
971
972                      We don't build a LOG_LINK for hard registers contained
973                      in ASM_OPERANDs.  If these registers get replaced,
974                      we might wind up changing the semantics of the insn,
975                      even if reload can make what appear to be valid
976                      assignments later.  */
977                   if (regno >= FIRST_PSEUDO_REGISTER
978                       || asm_noperands (PATTERN (use_insn)) < 0)
979                     LOG_LINKS (use_insn) =
980                       alloc_INSN_LIST (insn, LOG_LINKS (use_insn));
981                 }
982               next_use[regno] = NULL_RTX;
983             }
984
985           for (use_vec = DF_INSN_USES (insn); *use_vec; use_vec++)
986             {
987               struct df_ref *use = *use_vec;
988               int regno = DF_REF_REGNO (use);
989
990               /* Do not consider the usage of the stack pointer
991                  by function call.  */
992               if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
993                 continue;
994
995               next_use[regno] = insn;
996             }
997         }
998     }
999
1000   free (next_use);
1001 }
1002
1003 /* Clear LOG_LINKS fields of insns.  */
1004
1005 static void
1006 clear_log_links (void)
1007 {
1008   rtx insn;
1009
1010   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1011     if (INSN_P (insn))
1012       free_INSN_LIST_list (&LOG_LINKS (insn));
1013 }
1014
1015
1016
1017 \f
1018 /* Main entry point for combiner.  F is the first insn of the function.
1019    NREGS is the first unused pseudo-reg number.
1020
1021    Return nonzero if the combiner has turned an indirect jump
1022    instruction into a direct jump.  */
1023 static int
1024 combine_instructions (rtx f, unsigned int nregs)
1025 {
1026   rtx insn, next;
1027 #ifdef HAVE_cc0
1028   rtx prev;
1029 #endif
1030   rtx links, nextlinks;
1031   rtx first;
1032
1033   int new_direct_jump_p = 0;
1034
1035   for (first = f; first && !INSN_P (first); )
1036     first = NEXT_INSN (first);
1037   if (!first)
1038     return 0;
1039
1040   combine_attempts = 0;
1041   combine_merges = 0;
1042   combine_extras = 0;
1043   combine_successes = 0;
1044
1045   rtl_hooks = combine_rtl_hooks;
1046
1047   VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
1048
1049   init_recog_no_volatile ();
1050
1051   /* Allocate array for insn info.  */
1052   max_uid_known = get_max_uid ();
1053   uid_log_links = XCNEWVEC (rtx, max_uid_known + 1);
1054   uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1055
1056   nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1057
1058   /* Don't use reg_stat[].nonzero_bits when computing it.  This can cause
1059      problems when, for example, we have j <<= 1 in a loop.  */
1060
1061   nonzero_sign_valid = 0;
1062
1063   /* Scan all SETs and see if we can deduce anything about what
1064      bits are known to be zero for some registers and how many copies
1065      of the sign bit are known to exist for those registers.
1066
1067      Also set any known values so that we can use it while searching
1068      for what bits are known to be set.  */
1069
1070   label_tick = label_tick_ebb_start = 1;
1071
1072   setup_incoming_promotions (first);
1073
1074   create_log_links ();
1075   FOR_EACH_BB (this_basic_block)
1076     {
1077       last_call_luid = 0;
1078       mem_last_set = -1;
1079       label_tick++;
1080       FOR_BB_INSNS (this_basic_block, insn)
1081         if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1082           {
1083             subst_low_luid = DF_INSN_LUID (insn);
1084             subst_insn = insn;
1085
1086             note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1087                          insn);
1088             record_dead_and_set_regs (insn);
1089
1090 #ifdef AUTO_INC_DEC
1091             for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1092               if (REG_NOTE_KIND (links) == REG_INC)
1093                 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1094                                                   insn);
1095 #endif
1096
1097             /* Record the current insn_rtx_cost of this instruction.  */
1098             if (NONJUMP_INSN_P (insn))
1099               INSN_COST (insn) = insn_rtx_cost (PATTERN (insn));
1100             if (dump_file)
1101               fprintf(dump_file, "insn_cost %d: %d\n",
1102                     INSN_UID (insn), INSN_COST (insn));
1103           }
1104         else if (LABEL_P (insn))
1105           label_tick_ebb_start = label_tick;
1106     }
1107
1108   nonzero_sign_valid = 1;
1109
1110   /* Now scan all the insns in forward order.  */
1111
1112   label_tick = label_tick_ebb_start = 1;
1113   init_reg_last ();
1114   setup_incoming_promotions (first);
1115
1116   FOR_EACH_BB (this_basic_block)
1117     {
1118       last_call_luid = 0;
1119       mem_last_set = -1;
1120       label_tick++;
1121       for (insn = BB_HEAD (this_basic_block);
1122            insn != NEXT_INSN (BB_END (this_basic_block));
1123            insn = next ? next : NEXT_INSN (insn))
1124         {
1125           next = 0;
1126           if (INSN_P (insn))
1127             {
1128               /* See if we know about function return values before this
1129                  insn based upon SUBREG flags.  */
1130               check_conversions (insn, PATTERN (insn));
1131
1132               /* Try this insn with each insn it links back to.  */
1133
1134               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1135                 if ((next = try_combine (insn, XEXP (links, 0),
1136                                          NULL_RTX, &new_direct_jump_p)) != 0)
1137                   goto retry;
1138
1139               /* Try each sequence of three linked insns ending with this one.  */
1140
1141               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1142                 {
1143                   rtx link = XEXP (links, 0);
1144
1145                   /* If the linked insn has been replaced by a note, then there
1146                      is no point in pursuing this chain any further.  */
1147                   if (NOTE_P (link))
1148                     continue;
1149
1150                   for (nextlinks = LOG_LINKS (link);
1151                        nextlinks;
1152                        nextlinks = XEXP (nextlinks, 1))
1153                     if ((next = try_combine (insn, link,
1154                                              XEXP (nextlinks, 0),
1155                                              &new_direct_jump_p)) != 0)
1156                       goto retry;
1157                 }
1158
1159 #ifdef HAVE_cc0
1160               /* Try to combine a jump insn that uses CC0
1161                  with a preceding insn that sets CC0, and maybe with its
1162                  logical predecessor as well.
1163                  This is how we make decrement-and-branch insns.
1164                  We need this special code because data flow connections
1165                  via CC0 do not get entered in LOG_LINKS.  */
1166
1167               if (JUMP_P (insn)
1168                   && (prev = prev_nonnote_insn (insn)) != 0
1169                   && NONJUMP_INSN_P (prev)
1170                   && sets_cc0_p (PATTERN (prev)))
1171                 {
1172                   if ((next = try_combine (insn, prev,
1173                                            NULL_RTX, &new_direct_jump_p)) != 0)
1174                     goto retry;
1175
1176                   for (nextlinks = LOG_LINKS (prev); nextlinks;
1177                        nextlinks = XEXP (nextlinks, 1))
1178                     if ((next = try_combine (insn, prev,
1179                                              XEXP (nextlinks, 0),
1180                                              &new_direct_jump_p)) != 0)
1181                       goto retry;
1182                 }
1183
1184               /* Do the same for an insn that explicitly references CC0.  */
1185               if (NONJUMP_INSN_P (insn)
1186                   && (prev = prev_nonnote_insn (insn)) != 0
1187                   && NONJUMP_INSN_P (prev)
1188                   && sets_cc0_p (PATTERN (prev))
1189                   && GET_CODE (PATTERN (insn)) == SET
1190                   && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1191                 {
1192                   if ((next = try_combine (insn, prev,
1193                                            NULL_RTX, &new_direct_jump_p)) != 0)
1194                     goto retry;
1195
1196                   for (nextlinks = LOG_LINKS (prev); nextlinks;
1197                        nextlinks = XEXP (nextlinks, 1))
1198                     if ((next = try_combine (insn, prev,
1199                                              XEXP (nextlinks, 0),
1200                                              &new_direct_jump_p)) != 0)
1201                       goto retry;
1202                 }
1203
1204               /* Finally, see if any of the insns that this insn links to
1205                  explicitly references CC0.  If so, try this insn, that insn,
1206                  and its predecessor if it sets CC0.  */
1207               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1208                 if (NONJUMP_INSN_P (XEXP (links, 0))
1209                     && GET_CODE (PATTERN (XEXP (links, 0))) == SET
1210                     && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
1211                     && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
1212                     && NONJUMP_INSN_P (prev)
1213                     && sets_cc0_p (PATTERN (prev))
1214                     && (next = try_combine (insn, XEXP (links, 0),
1215                                             prev, &new_direct_jump_p)) != 0)
1216                   goto retry;
1217 #endif
1218
1219               /* Try combining an insn with two different insns whose results it
1220                  uses.  */
1221               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1222                 for (nextlinks = XEXP (links, 1); nextlinks;
1223                      nextlinks = XEXP (nextlinks, 1))
1224                   if ((next = try_combine (insn, XEXP (links, 0),
1225                                            XEXP (nextlinks, 0),
1226                                            &new_direct_jump_p)) != 0)
1227                     goto retry;
1228
1229               /* Try this insn with each REG_EQUAL note it links back to.  */
1230               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1231                 {
1232                   rtx set, note;
1233                   rtx temp = XEXP (links, 0);
1234                   if ((set = single_set (temp)) != 0
1235                       && (note = find_reg_equal_equiv_note (temp)) != 0
1236                       && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1237                       /* Avoid using a register that may already been marked
1238                          dead by an earlier instruction.  */
1239                       && ! unmentioned_reg_p (note, SET_SRC (set))
1240                       && (GET_MODE (note) == VOIDmode
1241                           ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1242                           : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1243                     {
1244                       /* Temporarily replace the set's source with the
1245                          contents of the REG_EQUAL note.  The insn will
1246                          be deleted or recognized by try_combine.  */
1247                       rtx orig = SET_SRC (set);
1248                       SET_SRC (set) = note;
1249                       i2mod = temp;
1250                       i2mod_old_rhs = copy_rtx (orig);
1251                       i2mod_new_rhs = copy_rtx (note);
1252                       next = try_combine (insn, i2mod, NULL_RTX,
1253                                           &new_direct_jump_p);
1254                       i2mod = NULL_RTX;
1255                       if (next)
1256                         goto retry;
1257                       SET_SRC (set) = orig;
1258                     }
1259                 }
1260
1261               if (!NOTE_P (insn))
1262                 record_dead_and_set_regs (insn);
1263
1264             retry:
1265               ;
1266             }
1267           else if (LABEL_P (insn))
1268             label_tick_ebb_start = label_tick;
1269         }
1270     }
1271
1272   clear_log_links ();
1273   clear_bb_flags ();
1274   new_direct_jump_p |= purge_all_dead_edges ();
1275   delete_noop_moves ();
1276
1277   /* Clean up.  */
1278   free (uid_log_links);
1279   free (uid_insn_cost);
1280   VEC_free (reg_stat_type, heap, reg_stat);
1281
1282   {
1283     struct undo *undo, *next;
1284     for (undo = undobuf.frees; undo; undo = next)
1285       {
1286         next = undo->next;
1287         free (undo);
1288       }
1289     undobuf.frees = 0;
1290   }
1291
1292   total_attempts += combine_attempts;
1293   total_merges += combine_merges;
1294   total_extras += combine_extras;
1295   total_successes += combine_successes;
1296
1297   nonzero_sign_valid = 0;
1298   rtl_hooks = general_rtl_hooks;
1299
1300   /* Make recognizer allow volatile MEMs again.  */
1301   init_recog ();
1302
1303   return new_direct_jump_p;
1304 }
1305
1306 /* Wipe the last_xxx fields of reg_stat in preparation for another pass.  */
1307
1308 static void
1309 init_reg_last (void)
1310 {
1311   unsigned int i;
1312   reg_stat_type *p;
1313
1314   for (i = 0; VEC_iterate (reg_stat_type, reg_stat, i, p); ++i)
1315     memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1316 }
1317 \f
1318 /* Set up any promoted values for incoming argument registers.  */
1319
1320 static void
1321 setup_incoming_promotions (rtx first)
1322 {
1323   tree arg;
1324   bool strictly_local = false;
1325
1326   if (!targetm.calls.promote_function_args (TREE_TYPE (cfun->decl)))
1327     return;
1328
1329   for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1330        arg = TREE_CHAIN (arg))
1331     {
1332       rtx reg = DECL_INCOMING_RTL (arg);
1333       int uns1, uns3;
1334       enum machine_mode mode1, mode2, mode3, mode4;
1335
1336       /* Only continue if the incoming argument is in a register.  */
1337       if (!REG_P (reg))
1338         continue;
1339
1340       /* Determine, if possible, whether all call sites of the current
1341          function lie within the current compilation unit.  (This does
1342          take into account the exporting of a function via taking its
1343          address, and so forth.)  */
1344       if (flag_unit_at_a_time)
1345         strictly_local = cgraph_local_info (current_function_decl)->local;
1346
1347       /* The mode and signedness of the argument before any promotions happen
1348          (equal to the mode of the pseudo holding it at that stage).  */
1349       mode1 = TYPE_MODE (TREE_TYPE (arg));
1350       uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1351
1352       /* The mode and signedness of the argument after any source language and
1353          TARGET_PROMOTE_PROTOTYPES-driven promotions.  */
1354       mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1355       uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1356
1357       /* The mode and signedness of the argument as it is actually passed, 
1358          after any TARGET_PROMOTE_FUNCTION_ARGS-driven ABI promotions.  */
1359       mode3 = promote_mode (DECL_ARG_TYPE (arg), mode2, &uns3, 1);
1360
1361       /* The mode of the register in which the argument is being passed.  */
1362       mode4 = GET_MODE (reg);
1363
1364       /* Eliminate sign extensions in the callee when possible.  Only
1365          do this when:
1366          (a) a mode promotion has occurred;
1367          (b) the mode of the register is the same as the mode of
1368              the argument as it is passed; and
1369          (c) the signedness does not change across any of the promotions; and
1370          (d) when no language-level promotions (which we cannot guarantee
1371              will have been done by an external caller) are necessary,
1372              unless we know that this function is only ever called from
1373              the current compilation unit -- all of whose call sites will
1374              do the mode1 --> mode2 promotion.  */
1375       if (mode1 != mode3
1376           && mode3 == mode4
1377           && uns1 == uns3
1378           && (mode1 == mode2 || strictly_local))
1379         {
1380           /* Record that the value was promoted from mode1 to mode3,
1381              so that any sign extension at the head of the current
1382              function may be eliminated.  */
1383           rtx x;
1384           x = gen_rtx_CLOBBER (mode1, const0_rtx);
1385           x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1386           record_value_for_reg (reg, first, x);
1387         }
1388     }
1389 }
1390
1391 /* Called via note_stores.  If X is a pseudo that is narrower than
1392    HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1393
1394    If we are setting only a portion of X and we can't figure out what
1395    portion, assume all bits will be used since we don't know what will
1396    be happening.
1397
1398    Similarly, set how many bits of X are known to be copies of the sign bit
1399    at all locations in the function.  This is the smallest number implied
1400    by any set of X.  */
1401
1402 static void
1403 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1404 {
1405   rtx insn = (rtx) data;
1406   unsigned int num;
1407
1408   if (REG_P (x)
1409       && REGNO (x) >= FIRST_PSEUDO_REGISTER
1410       /* If this register is undefined at the start of the file, we can't
1411          say what its contents were.  */
1412       && ! REGNO_REG_SET_P
1413            (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x))
1414       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
1415     {
1416       reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
1417
1418       if (set == 0 || GET_CODE (set) == CLOBBER)
1419         {
1420           rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1421           rsp->sign_bit_copies = 1;
1422           return;
1423         }
1424
1425       /* If this register is being initialized using itself, and the
1426          register is uninitialized in this basic block, and there are
1427          no LOG_LINKS which set the register, then part of the
1428          register is uninitialized.  In that case we can't assume
1429          anything about the number of nonzero bits.
1430
1431          ??? We could do better if we checked this in
1432          reg_{nonzero_bits,num_sign_bit_copies}_for_combine.  Then we
1433          could avoid making assumptions about the insn which initially
1434          sets the register, while still using the information in other
1435          insns.  We would have to be careful to check every insn
1436          involved in the combination.  */
1437
1438       if (insn
1439           && reg_referenced_p (x, PATTERN (insn))
1440           && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1441                                REGNO (x)))
1442         {
1443           rtx link;
1444
1445           for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
1446             {
1447               if (dead_or_set_p (XEXP (link, 0), x))
1448                 break;
1449             }
1450           if (!link)
1451             {
1452               rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1453               rsp->sign_bit_copies = 1;
1454               return;
1455             }
1456         }
1457
1458       /* If this is a complex assignment, see if we can convert it into a
1459          simple assignment.  */
1460       set = expand_field_assignment (set);
1461
1462       /* If this is a simple assignment, or we have a paradoxical SUBREG,
1463          set what we know about X.  */
1464
1465       if (SET_DEST (set) == x
1466           || (GET_CODE (SET_DEST (set)) == SUBREG
1467               && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
1468                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
1469               && SUBREG_REG (SET_DEST (set)) == x))
1470         {
1471           rtx src = SET_SRC (set);
1472
1473 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1474           /* If X is narrower than a word and SRC is a non-negative
1475              constant that would appear negative in the mode of X,
1476              sign-extend it for use in reg_stat[].nonzero_bits because some
1477              machines (maybe most) will actually do the sign-extension
1478              and this is the conservative approach.
1479
1480              ??? For 2.5, try to tighten up the MD files in this regard
1481              instead of this kludge.  */
1482
1483           if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
1484               && GET_CODE (src) == CONST_INT
1485               && INTVAL (src) > 0
1486               && 0 != (INTVAL (src)
1487                        & ((HOST_WIDE_INT) 1
1488                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
1489             src = GEN_INT (INTVAL (src)
1490                            | ((HOST_WIDE_INT) (-1)
1491                               << GET_MODE_BITSIZE (GET_MODE (x))));
1492 #endif
1493
1494           /* Don't call nonzero_bits if it cannot change anything.  */
1495           if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1496             rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1497           num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1498           if (rsp->sign_bit_copies == 0
1499               || rsp->sign_bit_copies > num)
1500             rsp->sign_bit_copies = num;
1501         }
1502       else
1503         {
1504           rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1505           rsp->sign_bit_copies = 1;
1506         }
1507     }
1508 }
1509 \f
1510 /* See if INSN can be combined into I3.  PRED and SUCC are optionally
1511    insns that were previously combined into I3 or that will be combined
1512    into the merger of INSN and I3.
1513
1514    Return 0 if the combination is not allowed for any reason.
1515
1516    If the combination is allowed, *PDEST will be set to the single
1517    destination of INSN and *PSRC to the single source, and this function
1518    will return 1.  */
1519
1520 static int
1521 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
1522                rtx *pdest, rtx *psrc)
1523 {
1524   int i;
1525   const_rtx set = 0;
1526   rtx src, dest;
1527   rtx p;
1528 #ifdef AUTO_INC_DEC
1529   rtx link;
1530 #endif
1531   int all_adjacent = (succ ? (next_active_insn (insn) == succ
1532                               && next_active_insn (succ) == i3)
1533                       : next_active_insn (insn) == i3);
1534
1535   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1536      or a PARALLEL consisting of such a SET and CLOBBERs.
1537
1538      If INSN has CLOBBER parallel parts, ignore them for our processing.
1539      By definition, these happen during the execution of the insn.  When it
1540      is merged with another insn, all bets are off.  If they are, in fact,
1541      needed and aren't also supplied in I3, they may be added by
1542      recog_for_combine.  Otherwise, it won't match.
1543
1544      We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1545      note.
1546
1547      Get the source and destination of INSN.  If more than one, can't
1548      combine.  */
1549
1550   if (GET_CODE (PATTERN (insn)) == SET)
1551     set = PATTERN (insn);
1552   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1553            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1554     {
1555       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1556         {
1557           rtx elt = XVECEXP (PATTERN (insn), 0, i);
1558           rtx note;
1559
1560           switch (GET_CODE (elt))
1561             {
1562             /* This is important to combine floating point insns
1563                for the SH4 port.  */
1564             case USE:
1565               /* Combining an isolated USE doesn't make sense.
1566                  We depend here on combinable_i3pat to reject them.  */
1567               /* The code below this loop only verifies that the inputs of
1568                  the SET in INSN do not change.  We call reg_set_between_p
1569                  to verify that the REG in the USE does not change between
1570                  I3 and INSN.
1571                  If the USE in INSN was for a pseudo register, the matching
1572                  insn pattern will likely match any register; combining this
1573                  with any other USE would only be safe if we knew that the
1574                  used registers have identical values, or if there was
1575                  something to tell them apart, e.g. different modes.  For
1576                  now, we forgo such complicated tests and simply disallow
1577                  combining of USES of pseudo registers with any other USE.  */
1578               if (REG_P (XEXP (elt, 0))
1579                   && GET_CODE (PATTERN (i3)) == PARALLEL)
1580                 {
1581                   rtx i3pat = PATTERN (i3);
1582                   int i = XVECLEN (i3pat, 0) - 1;
1583                   unsigned int regno = REGNO (XEXP (elt, 0));
1584
1585                   do
1586                     {
1587                       rtx i3elt = XVECEXP (i3pat, 0, i);
1588
1589                       if (GET_CODE (i3elt) == USE
1590                           && REG_P (XEXP (i3elt, 0))
1591                           && (REGNO (XEXP (i3elt, 0)) == regno
1592                               ? reg_set_between_p (XEXP (elt, 0),
1593                                                    PREV_INSN (insn), i3)
1594                               : regno >= FIRST_PSEUDO_REGISTER))
1595                         return 0;
1596                     }
1597                   while (--i >= 0);
1598                 }
1599               break;
1600
1601               /* We can ignore CLOBBERs.  */
1602             case CLOBBER:
1603               break;
1604
1605             case SET:
1606               /* Ignore SETs whose result isn't used but not those that
1607                  have side-effects.  */
1608               if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1609                   && (!(note = find_reg_note (insn, REG_EH_REGION, NULL_RTX))
1610                       || INTVAL (XEXP (note, 0)) <= 0)
1611                   && ! side_effects_p (elt))
1612                 break;
1613
1614               /* If we have already found a SET, this is a second one and
1615                  so we cannot combine with this insn.  */
1616               if (set)
1617                 return 0;
1618
1619               set = elt;
1620               break;
1621
1622             default:
1623               /* Anything else means we can't combine.  */
1624               return 0;
1625             }
1626         }
1627
1628       if (set == 0
1629           /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1630              so don't do anything with it.  */
1631           || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1632         return 0;
1633     }
1634   else
1635     return 0;
1636
1637   if (set == 0)
1638     return 0;
1639
1640   set = expand_field_assignment (set);
1641   src = SET_SRC (set), dest = SET_DEST (set);
1642
1643   /* Don't eliminate a store in the stack pointer.  */
1644   if (dest == stack_pointer_rtx
1645       /* Don't combine with an insn that sets a register to itself if it has
1646          a REG_EQUAL note.  This may be part of a REG_NO_CONFLICT sequence.  */
1647       || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1648       /* Can't merge an ASM_OPERANDS.  */
1649       || GET_CODE (src) == ASM_OPERANDS
1650       /* Can't merge a function call.  */
1651       || GET_CODE (src) == CALL
1652       /* Don't eliminate a function call argument.  */
1653       || (CALL_P (i3)
1654           && (find_reg_fusage (i3, USE, dest)
1655               || (REG_P (dest)
1656                   && REGNO (dest) < FIRST_PSEUDO_REGISTER
1657                   && global_regs[REGNO (dest)])))
1658       /* Don't substitute into an incremented register.  */
1659       || FIND_REG_INC_NOTE (i3, dest)
1660       || (succ && FIND_REG_INC_NOTE (succ, dest))
1661       /* Don't substitute into a non-local goto, this confuses CFG.  */
1662       || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1663 #if 0
1664       /* Don't combine the end of a libcall into anything.  */
1665       /* ??? This gives worse code, and appears to be unnecessary, since no
1666          pass after flow uses REG_LIBCALL/REG_RETVAL notes.  Local-alloc does
1667          use REG_RETVAL notes for noconflict blocks, but other code here
1668          makes sure that those insns don't disappear.  */
1669       || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1670 #endif
1671       /* Make sure that DEST is not used after SUCC but before I3.  */
1672       || (succ && ! all_adjacent
1673           && reg_used_between_p (dest, succ, i3))
1674       /* Make sure that the value that is to be substituted for the register
1675          does not use any registers whose values alter in between.  However,
1676          If the insns are adjacent, a use can't cross a set even though we
1677          think it might (this can happen for a sequence of insns each setting
1678          the same destination; last_set of that register might point to
1679          a NOTE).  If INSN has a REG_EQUIV note, the register is always
1680          equivalent to the memory so the substitution is valid even if there
1681          are intervening stores.  Also, don't move a volatile asm or
1682          UNSPEC_VOLATILE across any other insns.  */
1683       || (! all_adjacent
1684           && (((!MEM_P (src)
1685                 || ! find_reg_note (insn, REG_EQUIV, src))
1686                && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1687               || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1688               || GET_CODE (src) == UNSPEC_VOLATILE))
1689       /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1690          better register allocation by not doing the combine.  */
1691       || find_reg_note (i3, REG_NO_CONFLICT, dest)
1692       || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1693       /* Don't combine across a CALL_INSN, because that would possibly
1694          change whether the life span of some REGs crosses calls or not,
1695          and it is a pain to update that information.
1696          Exception: if source is a constant, moving it later can't hurt.
1697          Accept that special case, because it helps -fforce-addr a lot.  */
1698       || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1699     return 0;
1700
1701   /* DEST must either be a REG or CC0.  */
1702   if (REG_P (dest))
1703     {
1704       /* If register alignment is being enforced for multi-word items in all
1705          cases except for parameters, it is possible to have a register copy
1706          insn referencing a hard register that is not allowed to contain the
1707          mode being copied and which would not be valid as an operand of most
1708          insns.  Eliminate this problem by not combining with such an insn.
1709
1710          Also, on some machines we don't want to extend the life of a hard
1711          register.  */
1712
1713       if (REG_P (src)
1714           && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1715                && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1716               /* Don't extend the life of a hard register unless it is
1717                  user variable (if we have few registers) or it can't
1718                  fit into the desired register (meaning something special
1719                  is going on).
1720                  Also avoid substituting a return register into I3, because
1721                  reload can't handle a conflict with constraints of other
1722                  inputs.  */
1723               || (REGNO (src) < FIRST_PSEUDO_REGISTER
1724                   && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1725         return 0;
1726     }
1727   else if (GET_CODE (dest) != CC0)
1728     return 0;
1729
1730
1731   if (GET_CODE (PATTERN (i3)) == PARALLEL)
1732     for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1733       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1734         {
1735           /* Don't substitute for a register intended as a clobberable
1736              operand.  */
1737           rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1738           if (rtx_equal_p (reg, dest))
1739             return 0;
1740
1741           /* If the clobber represents an earlyclobber operand, we must not
1742              substitute an expression containing the clobbered register.
1743              As we do not analyze the constraint strings here, we have to
1744              make the conservative assumption.  However, if the register is
1745              a fixed hard reg, the clobber cannot represent any operand;
1746              we leave it up to the machine description to either accept or
1747              reject use-and-clobber patterns.  */
1748           if (!REG_P (reg)
1749               || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1750               || !fixed_regs[REGNO (reg)])
1751             if (reg_overlap_mentioned_p (reg, src))
1752               return 0;
1753         }
1754
1755   /* If INSN contains anything volatile, or is an `asm' (whether volatile
1756      or not), reject, unless nothing volatile comes between it and I3 */
1757
1758   if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1759     {
1760       /* Make sure succ doesn't contain a volatile reference.  */
1761       if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1762         return 0;
1763
1764       for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1765         if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1766           return 0;
1767     }
1768
1769   /* If INSN is an asm, and DEST is a hard register, reject, since it has
1770      to be an explicit register variable, and was chosen for a reason.  */
1771
1772   if (GET_CODE (src) == ASM_OPERANDS
1773       && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1774     return 0;
1775
1776   /* If there are any volatile insns between INSN and I3, reject, because
1777      they might affect machine state.  */
1778
1779   for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1780     if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1781       return 0;
1782
1783   /* If INSN contains an autoincrement or autodecrement, make sure that
1784      register is not used between there and I3, and not already used in
1785      I3 either.  Neither must it be used in PRED or SUCC, if they exist.
1786      Also insist that I3 not be a jump; if it were one
1787      and the incremented register were spilled, we would lose.  */
1788
1789 #ifdef AUTO_INC_DEC
1790   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1791     if (REG_NOTE_KIND (link) == REG_INC
1792         && (JUMP_P (i3)
1793             || reg_used_between_p (XEXP (link, 0), insn, i3)
1794             || (pred != NULL_RTX
1795                 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1796             || (succ != NULL_RTX
1797                 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
1798             || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1799       return 0;
1800 #endif
1801
1802 #ifdef HAVE_cc0
1803   /* Don't combine an insn that follows a CC0-setting insn.
1804      An insn that uses CC0 must not be separated from the one that sets it.
1805      We do, however, allow I2 to follow a CC0-setting insn if that insn
1806      is passed as I1; in that case it will be deleted also.
1807      We also allow combining in this case if all the insns are adjacent
1808      because that would leave the two CC0 insns adjacent as well.
1809      It would be more logical to test whether CC0 occurs inside I1 or I2,
1810      but that would be much slower, and this ought to be equivalent.  */
1811
1812   p = prev_nonnote_insn (insn);
1813   if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1814       && ! all_adjacent)
1815     return 0;
1816 #endif
1817
1818   /* If we get here, we have passed all the tests and the combination is
1819      to be allowed.  */
1820
1821   *pdest = dest;
1822   *psrc = src;
1823
1824   return 1;
1825 }
1826 \f
1827 /* LOC is the location within I3 that contains its pattern or the component
1828    of a PARALLEL of the pattern.  We validate that it is valid for combining.
1829
1830    One problem is if I3 modifies its output, as opposed to replacing it
1831    entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1832    so would produce an insn that is not equivalent to the original insns.
1833
1834    Consider:
1835
1836          (set (reg:DI 101) (reg:DI 100))
1837          (set (subreg:SI (reg:DI 101) 0) <foo>)
1838
1839    This is NOT equivalent to:
1840
1841          (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1842                     (set (reg:DI 101) (reg:DI 100))])
1843
1844    Not only does this modify 100 (in which case it might still be valid
1845    if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1846
1847    We can also run into a problem if I2 sets a register that I1
1848    uses and I1 gets directly substituted into I3 (not via I2).  In that
1849    case, we would be getting the wrong value of I2DEST into I3, so we
1850    must reject the combination.  This case occurs when I2 and I1 both
1851    feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1852    If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1853    of a SET must prevent combination from occurring.
1854
1855    Before doing the above check, we first try to expand a field assignment
1856    into a set of logical operations.
1857
1858    If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1859    we place a register that is both set and used within I3.  If more than one
1860    such register is detected, we fail.
1861
1862    Return 1 if the combination is valid, zero otherwise.  */
1863
1864 static int
1865 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
1866                   int i1_not_in_src, rtx *pi3dest_killed)
1867 {
1868   rtx x = *loc;
1869
1870   if (GET_CODE (x) == SET)
1871     {
1872       rtx set = x ;
1873       rtx dest = SET_DEST (set);
1874       rtx src = SET_SRC (set);
1875       rtx inner_dest = dest;
1876       rtx subdest;
1877
1878       while (GET_CODE (inner_dest) == STRICT_LOW_PART
1879              || GET_CODE (inner_dest) == SUBREG
1880              || GET_CODE (inner_dest) == ZERO_EXTRACT)
1881         inner_dest = XEXP (inner_dest, 0);
1882
1883       /* Check for the case where I3 modifies its output, as discussed
1884          above.  We don't want to prevent pseudos from being combined
1885          into the address of a MEM, so only prevent the combination if
1886          i1 or i2 set the same MEM.  */
1887       if ((inner_dest != dest &&
1888            (!MEM_P (inner_dest)
1889             || rtx_equal_p (i2dest, inner_dest)
1890             || (i1dest && rtx_equal_p (i1dest, inner_dest)))
1891            && (reg_overlap_mentioned_p (i2dest, inner_dest)
1892                || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1893
1894           /* This is the same test done in can_combine_p except we can't test
1895              all_adjacent; we don't have to, since this instruction will stay
1896              in place, thus we are not considering increasing the lifetime of
1897              INNER_DEST.
1898
1899              Also, if this insn sets a function argument, combining it with
1900              something that might need a spill could clobber a previous
1901              function argument; the all_adjacent test in can_combine_p also
1902              checks this; here, we do a more specific test for this case.  */
1903
1904           || (REG_P (inner_dest)
1905               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1906               && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1907                                         GET_MODE (inner_dest))))
1908           || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1909         return 0;
1910
1911       /* If DEST is used in I3, it is being killed in this insn, so
1912          record that for later.  We have to consider paradoxical
1913          subregs here, since they kill the whole register, but we
1914          ignore partial subregs, STRICT_LOW_PART, etc.
1915          Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1916          STACK_POINTER_REGNUM, since these are always considered to be
1917          live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
1918       subdest = dest;
1919       if (GET_CODE (subdest) == SUBREG
1920           && (GET_MODE_SIZE (GET_MODE (subdest))
1921               >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
1922         subdest = SUBREG_REG (subdest);
1923       if (pi3dest_killed
1924           && REG_P (subdest)
1925           && reg_referenced_p (subdest, PATTERN (i3))
1926           && REGNO (subdest) != FRAME_POINTER_REGNUM
1927 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1928           && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
1929 #endif
1930 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1931           && (REGNO (subdest) != ARG_POINTER_REGNUM
1932               || ! fixed_regs [REGNO (subdest)])
1933 #endif
1934           && REGNO (subdest) != STACK_POINTER_REGNUM)
1935         {
1936           if (*pi3dest_killed)
1937             return 0;
1938
1939           *pi3dest_killed = subdest;
1940         }
1941     }
1942
1943   else if (GET_CODE (x) == PARALLEL)
1944     {
1945       int i;
1946
1947       for (i = 0; i < XVECLEN (x, 0); i++)
1948         if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1949                                 i1_not_in_src, pi3dest_killed))
1950           return 0;
1951     }
1952
1953   return 1;
1954 }
1955 \f
1956 /* Return 1 if X is an arithmetic expression that contains a multiplication
1957    and division.  We don't count multiplications by powers of two here.  */
1958
1959 static int
1960 contains_muldiv (rtx x)
1961 {
1962   switch (GET_CODE (x))
1963     {
1964     case MOD:  case DIV:  case UMOD:  case UDIV:
1965       return 1;
1966
1967     case MULT:
1968       return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1969                 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1970     default:
1971       if (BINARY_P (x))
1972         return contains_muldiv (XEXP (x, 0))
1973             || contains_muldiv (XEXP (x, 1));
1974
1975       if (UNARY_P (x))
1976         return contains_muldiv (XEXP (x, 0));
1977
1978       return 0;
1979     }
1980 }
1981 \f
1982 /* Determine whether INSN can be used in a combination.  Return nonzero if
1983    not.  This is used in try_combine to detect early some cases where we
1984    can't perform combinations.  */
1985
1986 static int
1987 cant_combine_insn_p (rtx insn)
1988 {
1989   rtx set;
1990   rtx src, dest;
1991
1992   /* If this isn't really an insn, we can't do anything.
1993      This can occur when flow deletes an insn that it has merged into an
1994      auto-increment address.  */
1995   if (! INSN_P (insn))
1996     return 1;
1997
1998   /* Never combine loads and stores involving hard regs that are likely
1999      to be spilled.  The register allocator can usually handle such
2000      reg-reg moves by tying.  If we allow the combiner to make
2001      substitutions of likely-spilled regs, reload might die.
2002      As an exception, we allow combinations involving fixed regs; these are
2003      not available to the register allocator so there's no risk involved.  */
2004
2005   set = single_set (insn);
2006   if (! set)
2007     return 0;
2008   src = SET_SRC (set);
2009   dest = SET_DEST (set);
2010   if (GET_CODE (src) == SUBREG)
2011     src = SUBREG_REG (src);
2012   if (GET_CODE (dest) == SUBREG)
2013     dest = SUBREG_REG (dest);
2014   if (REG_P (src) && REG_P (dest)
2015       && ((REGNO (src) < FIRST_PSEUDO_REGISTER
2016            && ! fixed_regs[REGNO (src)]
2017            && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
2018           || (REGNO (dest) < FIRST_PSEUDO_REGISTER
2019               && ! fixed_regs[REGNO (dest)]
2020               && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
2021     return 1;
2022
2023   return 0;
2024 }
2025
2026 struct likely_spilled_retval_info
2027 {
2028   unsigned regno, nregs;
2029   unsigned mask;
2030 };
2031
2032 /* Called via note_stores by likely_spilled_retval_p.  Remove from info->mask
2033    hard registers that are known to be written to / clobbered in full.  */
2034 static void
2035 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2036 {
2037   struct likely_spilled_retval_info *info = data;
2038   unsigned regno, nregs;
2039   unsigned new_mask;
2040
2041   if (!REG_P (XEXP (set, 0)))
2042     return;
2043   regno = REGNO (x);
2044   if (regno >= info->regno + info->nregs)
2045     return;
2046   nregs = hard_regno_nregs[regno][GET_MODE (x)];
2047   if (regno + nregs <= info->regno)
2048     return;
2049   new_mask = (2U << (nregs - 1)) - 1;
2050   if (regno < info->regno)
2051     new_mask >>= info->regno - regno;
2052   else
2053     new_mask <<= regno - info->regno;
2054   info->mask &= ~new_mask;
2055 }
2056
2057 /* Return nonzero iff part of the return value is live during INSN, and
2058    it is likely spilled.  This can happen when more than one insn is needed
2059    to copy the return value, e.g. when we consider to combine into the
2060    second copy insn for a complex value.  */
2061
2062 static int
2063 likely_spilled_retval_p (rtx insn)
2064 {
2065   rtx use = BB_END (this_basic_block);
2066   rtx reg, p;
2067   unsigned regno, nregs;
2068   /* We assume here that no machine mode needs more than
2069      32 hard registers when the value overlaps with a register
2070      for which FUNCTION_VALUE_REGNO_P is true.  */
2071   unsigned mask;
2072   struct likely_spilled_retval_info info;
2073
2074   if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2075     return 0;
2076   reg = XEXP (PATTERN (use), 0);
2077   if (!REG_P (reg) || !FUNCTION_VALUE_REGNO_P (REGNO (reg)))
2078     return 0;
2079   regno = REGNO (reg);
2080   nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2081   if (nregs == 1)
2082     return 0;
2083   mask = (2U << (nregs - 1)) - 1;
2084
2085   /* Disregard parts of the return value that are set later.  */
2086   info.regno = regno;
2087   info.nregs = nregs;
2088   info.mask = mask;
2089   for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2090     if (INSN_P (p))
2091       note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2092   mask = info.mask;
2093
2094   /* Check if any of the (probably) live return value registers is
2095      likely spilled.  */
2096   nregs --;
2097   do
2098     {
2099       if ((mask & 1 << nregs)
2100           && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (regno + nregs)))
2101         return 1;
2102     } while (nregs--);
2103   return 0;
2104 }
2105
2106 /* Adjust INSN after we made a change to its destination.
2107
2108    Changing the destination can invalidate notes that say something about
2109    the results of the insn and a LOG_LINK pointing to the insn.  */
2110
2111 static void
2112 adjust_for_new_dest (rtx insn)
2113 {
2114   /* For notes, be conservative and simply remove them.  */
2115   remove_reg_equal_equiv_notes (insn);
2116
2117   /* The new insn will have a destination that was previously the destination
2118      of an insn just above it.  Call distribute_links to make a LOG_LINK from
2119      the next use of that destination.  */
2120   distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
2121
2122   df_insn_rescan (insn);
2123 }
2124
2125 /* Return TRUE if combine can reuse reg X in mode MODE.
2126    ADDED_SETS is nonzero if the original set is still required.  */
2127 static bool
2128 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
2129 {
2130   unsigned int regno;
2131
2132   if (!REG_P(x))
2133     return false;
2134
2135   regno = REGNO (x);
2136   /* Allow hard registers if the new mode is legal, and occupies no more
2137      registers than the old mode.  */
2138   if (regno < FIRST_PSEUDO_REGISTER)
2139     return (HARD_REGNO_MODE_OK (regno, mode)
2140             && (hard_regno_nregs[regno][GET_MODE (x)]
2141                 >= hard_regno_nregs[regno][mode]));
2142
2143   /* Or a pseudo that is only used once.  */
2144   return (REG_N_SETS (regno) == 1 && !added_sets
2145           && !REG_USERVAR_P (x));
2146 }
2147
2148
2149 /* Check whether X, the destination of a set, refers to part of
2150    the register specified by REG.  */
2151
2152 static bool
2153 reg_subword_p (rtx x, rtx reg)
2154 {
2155   /* Check that reg is an integer mode register.  */
2156   if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2157     return false;
2158
2159   if (GET_CODE (x) == STRICT_LOW_PART
2160       || GET_CODE (x) == ZERO_EXTRACT)
2161     x = XEXP (x, 0);
2162
2163   return GET_CODE (x) == SUBREG
2164          && SUBREG_REG (x) == reg
2165          && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2166 }
2167
2168
2169 /* Try to combine the insns I1 and I2 into I3.
2170    Here I1 and I2 appear earlier than I3.
2171    I1 can be zero; then we combine just I2 into I3.
2172
2173    If we are combining three insns and the resulting insn is not recognized,
2174    try splitting it into two insns.  If that happens, I2 and I3 are retained
2175    and I1 is pseudo-deleted by turning it into a NOTE.  Otherwise, I1 and I2
2176    are pseudo-deleted.
2177
2178    Return 0 if the combination does not work.  Then nothing is changed.
2179    If we did the combination, return the insn at which combine should
2180    resume scanning.
2181
2182    Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2183    new direct jump instruction.  */
2184
2185 static rtx
2186 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
2187 {
2188   /* New patterns for I3 and I2, respectively.  */
2189   rtx newpat, newi2pat = 0;
2190   rtvec newpat_vec_with_clobbers = 0;
2191   int substed_i2 = 0, substed_i1 = 0;
2192   /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead.  */
2193   int added_sets_1, added_sets_2;
2194   /* Total number of SETs to put into I3.  */
2195   int total_sets;
2196   /* Nonzero if I2's body now appears in I3.  */
2197   int i2_is_used;
2198   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
2199   int insn_code_number, i2_code_number = 0, other_code_number = 0;
2200   /* Contains I3 if the destination of I3 is used in its source, which means
2201      that the old life of I3 is being killed.  If that usage is placed into
2202      I2 and not in I3, a REG_DEAD note must be made.  */
2203   rtx i3dest_killed = 0;
2204   /* SET_DEST and SET_SRC of I2 and I1.  */
2205   rtx i2dest, i2src, i1dest = 0, i1src = 0;
2206   /* PATTERN (I1) and PATTERN (I2), or a copy of it in certain cases.  */
2207   rtx i1pat = 0, i2pat = 0;
2208   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
2209   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2210   int i2dest_killed = 0, i1dest_killed = 0;
2211   int i1_feeds_i3 = 0;
2212   /* Notes that must be added to REG_NOTES in I3 and I2.  */
2213   rtx new_i3_notes, new_i2_notes;
2214   /* Notes that we substituted I3 into I2 instead of the normal case.  */
2215   int i3_subst_into_i2 = 0;
2216   /* Notes that I1, I2 or I3 is a MULT operation.  */
2217   int have_mult = 0;
2218   int swap_i2i3 = 0;
2219
2220   int maxreg;
2221   rtx temp;
2222   rtx link;
2223   rtx other_pat = 0;
2224   rtx new_other_notes;
2225   int i;
2226
2227   /* Exit early if one of the insns involved can't be used for
2228      combinations.  */
2229   if (cant_combine_insn_p (i3)
2230       || cant_combine_insn_p (i2)
2231       || (i1 && cant_combine_insn_p (i1))
2232       || likely_spilled_retval_p (i3)
2233       /* We also can't do anything if I3 has a
2234          REG_LIBCALL note since we don't want to disrupt the contiguity of a
2235          libcall.  */
2236 #if 0
2237       /* ??? This gives worse code, and appears to be unnecessary, since no
2238          pass after flow uses REG_LIBCALL/REG_RETVAL notes.  */
2239       || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
2240 #endif
2241       )
2242     return 0;
2243
2244   combine_attempts++;
2245   undobuf.other_insn = 0;
2246
2247   /* Reset the hard register usage information.  */
2248   CLEAR_HARD_REG_SET (newpat_used_regs);
2249
2250   /* If I1 and I2 both feed I3, they can be in any order.  To simplify the
2251      code below, set I1 to be the earlier of the two insns.  */
2252   if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2253     temp = i1, i1 = i2, i2 = temp;
2254
2255   added_links_insn = 0;
2256
2257   /* First check for one important special-case that the code below will
2258      not handle.  Namely, the case where I1 is zero, I2 is a PARALLEL
2259      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
2260      we may be able to replace that destination with the destination of I3.
2261      This occurs in the common code where we compute both a quotient and
2262      remainder into a structure, in which case we want to do the computation
2263      directly into the structure to avoid register-register copies.
2264
2265      Note that this case handles both multiple sets in I2 and also
2266      cases where I2 has a number of CLOBBER or PARALLELs.
2267
2268      We make very conservative checks below and only try to handle the
2269      most common cases of this.  For example, we only handle the case
2270      where I2 and I3 are adjacent to avoid making difficult register
2271      usage tests.  */
2272
2273   if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2274       && REG_P (SET_SRC (PATTERN (i3)))
2275       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2276       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2277       && GET_CODE (PATTERN (i2)) == PARALLEL
2278       && ! side_effects_p (SET_DEST (PATTERN (i3)))
2279       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2280          below would need to check what is inside (and reg_overlap_mentioned_p
2281          doesn't support those codes anyway).  Don't allow those destinations;
2282          the resulting insn isn't likely to be recognized anyway.  */
2283       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2284       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2285       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2286                                     SET_DEST (PATTERN (i3)))
2287       && next_real_insn (i2) == i3)
2288     {
2289       rtx p2 = PATTERN (i2);
2290
2291       /* Make sure that the destination of I3,
2292          which we are going to substitute into one output of I2,
2293          is not used within another output of I2.  We must avoid making this:
2294          (parallel [(set (mem (reg 69)) ...)
2295                     (set (reg 69) ...)])
2296          which is not well-defined as to order of actions.
2297          (Besides, reload can't handle output reloads for this.)
2298
2299          The problem can also happen if the dest of I3 is a memory ref,
2300          if another dest in I2 is an indirect memory ref.  */
2301       for (i = 0; i < XVECLEN (p2, 0); i++)
2302         if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2303              || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2304             && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2305                                         SET_DEST (XVECEXP (p2, 0, i))))
2306           break;
2307
2308       if (i == XVECLEN (p2, 0))
2309         for (i = 0; i < XVECLEN (p2, 0); i++)
2310           if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2311                || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2312               && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2313             {
2314               combine_merges++;
2315
2316               subst_insn = i3;
2317               subst_low_luid = DF_INSN_LUID (i2);
2318
2319               added_sets_2 = added_sets_1 = 0;
2320               i2dest = SET_SRC (PATTERN (i3));
2321               i2dest_killed = dead_or_set_p (i2, i2dest);
2322
2323               /* Replace the dest in I2 with our dest and make the resulting
2324                  insn the new pattern for I3.  Then skip to where we
2325                  validate the pattern.  Everything was set up above.  */
2326               SUBST (SET_DEST (XVECEXP (p2, 0, i)),
2327                      SET_DEST (PATTERN (i3)));
2328
2329               newpat = p2;
2330               i3_subst_into_i2 = 1;
2331               goto validate_replacement;
2332             }
2333     }
2334
2335   /* If I2 is setting a pseudo to a constant and I3 is setting some
2336      sub-part of it to another constant, merge them by making a new
2337      constant.  */
2338   if (i1 == 0
2339       && (temp = single_set (i2)) != 0
2340       && (GET_CODE (SET_SRC (temp)) == CONST_INT
2341           || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
2342       && GET_CODE (PATTERN (i3)) == SET
2343       && (GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT
2344           || GET_CODE (SET_SRC (PATTERN (i3))) == CONST_DOUBLE)
2345       && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp)))
2346     {
2347       rtx dest = SET_DEST (PATTERN (i3));
2348       int offset = -1;
2349       int width = 0;
2350
2351       if (GET_CODE (dest) == ZERO_EXTRACT)
2352         {
2353           if (GET_CODE (XEXP (dest, 1)) == CONST_INT
2354               && GET_CODE (XEXP (dest, 2)) == CONST_INT)
2355             {
2356               width = INTVAL (XEXP (dest, 1));
2357               offset = INTVAL (XEXP (dest, 2));
2358               dest = XEXP (dest, 0);
2359               if (BITS_BIG_ENDIAN)
2360                 offset = GET_MODE_BITSIZE (GET_MODE (dest)) - width - offset;
2361             }
2362         }
2363       else
2364         {
2365           if (GET_CODE (dest) == STRICT_LOW_PART)
2366             dest = XEXP (dest, 0);
2367           width = GET_MODE_BITSIZE (GET_MODE (dest));
2368           offset = 0;
2369         }
2370
2371       if (offset >= 0)
2372         {
2373           /* If this is the low part, we're done.  */
2374           if (subreg_lowpart_p (dest))
2375             ;
2376           /* Handle the case where inner is twice the size of outer.  */
2377           else if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2378                    == 2 * GET_MODE_BITSIZE (GET_MODE (dest)))
2379             offset += GET_MODE_BITSIZE (GET_MODE (dest));
2380           /* Otherwise give up for now.  */
2381           else
2382             offset = -1;
2383         }
2384
2385       if (offset >= 0
2386           && (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2387               <= HOST_BITS_PER_WIDE_INT * 2))
2388         {
2389           HOST_WIDE_INT mhi, ohi, ihi;
2390           HOST_WIDE_INT mlo, olo, ilo;
2391           rtx inner = SET_SRC (PATTERN (i3));
2392           rtx outer = SET_SRC (temp);
2393
2394           if (GET_CODE (outer) == CONST_INT)
2395             {
2396               olo = INTVAL (outer);
2397               ohi = olo < 0 ? -1 : 0;
2398             }
2399           else
2400             {
2401               olo = CONST_DOUBLE_LOW (outer);
2402               ohi = CONST_DOUBLE_HIGH (outer);
2403             }
2404
2405           if (GET_CODE (inner) == CONST_INT)
2406             {
2407               ilo = INTVAL (inner);
2408               ihi = ilo < 0 ? -1 : 0;
2409             }
2410           else
2411             {
2412               ilo = CONST_DOUBLE_LOW (inner);
2413               ihi = CONST_DOUBLE_HIGH (inner);
2414             }
2415
2416           if (width < HOST_BITS_PER_WIDE_INT)
2417             {
2418               mlo = ((unsigned HOST_WIDE_INT) 1 << width) - 1;
2419               mhi = 0;
2420             }
2421           else if (width < HOST_BITS_PER_WIDE_INT * 2)
2422             {
2423               mhi = ((unsigned HOST_WIDE_INT) 1
2424                      << (width - HOST_BITS_PER_WIDE_INT)) - 1;
2425               mlo = -1;
2426             }
2427           else
2428             {
2429               mlo = -1;
2430               mhi = -1;
2431             }
2432
2433           ilo &= mlo;
2434           ihi &= mhi;
2435
2436           if (offset >= HOST_BITS_PER_WIDE_INT)
2437             {
2438               mhi = mlo << (offset - HOST_BITS_PER_WIDE_INT);
2439               mlo = 0;
2440               ihi = ilo << (offset - HOST_BITS_PER_WIDE_INT);
2441               ilo = 0;
2442             }
2443           else if (offset > 0)
2444             {
2445               mhi = (mhi << offset) | ((unsigned HOST_WIDE_INT) mlo
2446                                        >> (HOST_BITS_PER_WIDE_INT - offset));
2447               mlo = mlo << offset;
2448               ihi = (ihi << offset) | ((unsigned HOST_WIDE_INT) ilo
2449                                        >> (HOST_BITS_PER_WIDE_INT - offset));
2450               ilo = ilo << offset;
2451             }
2452
2453           olo = (olo & ~mlo) | ilo;
2454           ohi = (ohi & ~mhi) | ihi;
2455
2456           combine_merges++;
2457           subst_insn = i3;
2458           subst_low_luid = DF_INSN_LUID (i2);
2459           added_sets_2 = added_sets_1 = 0;
2460           i2dest = SET_DEST (temp);
2461           i2dest_killed = dead_or_set_p (i2, i2dest);
2462
2463           SUBST (SET_SRC (temp),
2464                  immed_double_const (olo, ohi, GET_MODE (SET_DEST (temp))));
2465
2466           newpat = PATTERN (i2);
2467           goto validate_replacement;
2468         }
2469     }
2470
2471 #ifndef HAVE_cc0
2472   /* If we have no I1 and I2 looks like:
2473         (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2474                    (set Y OP)])
2475      make up a dummy I1 that is
2476         (set Y OP)
2477      and change I2 to be
2478         (set (reg:CC X) (compare:CC Y (const_int 0)))
2479
2480      (We can ignore any trailing CLOBBERs.)
2481
2482      This undoes a previous combination and allows us to match a branch-and-
2483      decrement insn.  */
2484
2485   if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2486       && XVECLEN (PATTERN (i2), 0) >= 2
2487       && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2488       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2489           == MODE_CC)
2490       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2491       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2492       && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2493       && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2494       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2495                       SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2496     {
2497       for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2498         if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2499           break;
2500
2501       if (i == 1)
2502         {
2503           /* We make I1 with the same INSN_UID as I2.  This gives it
2504              the same DF_INSN_LUID for value tracking.  Our fake I1 will
2505              never appear in the insn stream so giving it the same INSN_UID
2506              as I2 will not cause a problem.  */
2507
2508           i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
2509                              BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
2510                              XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX);
2511
2512           SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2513           SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2514                  SET_DEST (PATTERN (i1)));
2515         }
2516     }
2517 #endif
2518
2519   /* Verify that I2 and I1 are valid for combining.  */
2520   if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
2521       || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
2522     {
2523       undo_all ();
2524       return 0;
2525     }
2526
2527   /* Record whether I2DEST is used in I2SRC and similarly for the other
2528      cases.  Knowing this will help in register status updating below.  */
2529   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2530   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2531   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2532   i2dest_killed = dead_or_set_p (i2, i2dest);
2533   i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2534
2535   /* See if I1 directly feeds into I3.  It does if I1DEST is not used
2536      in I2SRC.  */
2537   i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
2538
2539   /* Ensure that I3's pattern can be the destination of combines.  */
2540   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
2541                           i1 && i2dest_in_i1src && i1_feeds_i3,
2542                           &i3dest_killed))
2543     {
2544       undo_all ();
2545       return 0;
2546     }
2547
2548   /* See if any of the insns is a MULT operation.  Unless one is, we will
2549      reject a combination that is, since it must be slower.  Be conservative
2550      here.  */
2551   if (GET_CODE (i2src) == MULT
2552       || (i1 != 0 && GET_CODE (i1src) == MULT)
2553       || (GET_CODE (PATTERN (i3)) == SET
2554           && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2555     have_mult = 1;
2556
2557   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2558      We used to do this EXCEPT in one case: I3 has a post-inc in an
2559      output operand.  However, that exception can give rise to insns like
2560         mov r3,(r3)+
2561      which is a famous insn on the PDP-11 where the value of r3 used as the
2562      source was model-dependent.  Avoid this sort of thing.  */
2563
2564 #if 0
2565   if (!(GET_CODE (PATTERN (i3)) == SET
2566         && REG_P (SET_SRC (PATTERN (i3)))
2567         && MEM_P (SET_DEST (PATTERN (i3)))
2568         && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2569             || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2570     /* It's not the exception.  */
2571 #endif
2572 #ifdef AUTO_INC_DEC
2573     for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2574       if (REG_NOTE_KIND (link) == REG_INC
2575           && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2576               || (i1 != 0
2577                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2578         {
2579           undo_all ();
2580           return 0;
2581         }
2582 #endif
2583
2584   /* See if the SETs in I1 or I2 need to be kept around in the merged
2585      instruction: whenever the value set there is still needed past I3.
2586      For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
2587
2588      For the SET in I1, we have two cases:  If I1 and I2 independently
2589      feed into I3, the set in I1 needs to be kept around if I1DEST dies
2590      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
2591      in I1 needs to be kept around unless I1DEST dies or is set in either
2592      I2 or I3.  We can distinguish these cases by seeing if I2SRC mentions
2593      I1DEST.  If so, we know I1 feeds into I2.  */
2594
2595   added_sets_2 = ! dead_or_set_p (i3, i2dest);
2596
2597   added_sets_1
2598     = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
2599                : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
2600
2601   /* If the set in I2 needs to be kept around, we must make a copy of
2602      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2603      PATTERN (I2), we are only substituting for the original I1DEST, not into
2604      an already-substituted copy.  This also prevents making self-referential
2605      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2606      I2DEST.  */
2607
2608   if (added_sets_2)
2609     {
2610       if (GET_CODE (PATTERN (i2)) == PARALLEL)
2611         i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
2612       else
2613         i2pat = copy_rtx (PATTERN (i2));
2614     }
2615
2616   if (added_sets_1)
2617     {
2618       if (GET_CODE (PATTERN (i1)) == PARALLEL)
2619         i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
2620       else
2621         i1pat = copy_rtx (PATTERN (i1));
2622     }
2623
2624   combine_merges++;
2625
2626   /* Substitute in the latest insn for the regs set by the earlier ones.  */
2627
2628   maxreg = max_reg_num ();
2629
2630   subst_insn = i3;
2631
2632 #ifndef HAVE_cc0
2633   /* Many machines that don't use CC0 have insns that can both perform an
2634      arithmetic operation and set the condition code.  These operations will
2635      be represented as a PARALLEL with the first element of the vector
2636      being a COMPARE of an arithmetic operation with the constant zero.
2637      The second element of the vector will set some pseudo to the result
2638      of the same arithmetic operation.  If we simplify the COMPARE, we won't
2639      match such a pattern and so will generate an extra insn.   Here we test
2640      for this case, where both the comparison and the operation result are
2641      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2642      I2SRC.  Later we will make the PARALLEL that contains I2.  */
2643
2644   if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2645       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2646       && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
2647       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2648     {
2649 #ifdef SELECT_CC_MODE
2650       rtx *cc_use;
2651       enum machine_mode compare_mode;
2652 #endif
2653
2654       newpat = PATTERN (i3);
2655       SUBST (XEXP (SET_SRC (newpat), 0), i2src);
2656
2657       i2_is_used = 1;
2658
2659 #ifdef SELECT_CC_MODE
2660       /* See if a COMPARE with the operand we substituted in should be done
2661          with the mode that is currently being used.  If not, do the same
2662          processing we do in `subst' for a SET; namely, if the destination
2663          is used only once, try to replace it with a register of the proper
2664          mode and also replace the COMPARE.  */
2665       if (undobuf.other_insn == 0
2666           && (cc_use = find_single_use (SET_DEST (newpat), i3,
2667                                         &undobuf.other_insn))
2668           && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
2669                                               i2src, const0_rtx))
2670               != GET_MODE (SET_DEST (newpat))))
2671         {
2672           if (can_change_dest_mode(SET_DEST (newpat), added_sets_2,
2673                                    compare_mode))
2674             {
2675               unsigned int regno = REGNO (SET_DEST (newpat));
2676               rtx new_dest;
2677
2678               if (regno < FIRST_PSEUDO_REGISTER)
2679                 new_dest = gen_rtx_REG (compare_mode, regno);
2680               else
2681                 {
2682                   SUBST_MODE (regno_reg_rtx[regno], compare_mode);
2683                   new_dest = regno_reg_rtx[regno];
2684                 }
2685
2686               SUBST (SET_DEST (newpat), new_dest);
2687               SUBST (XEXP (*cc_use, 0), new_dest);
2688               SUBST (SET_SRC (newpat),
2689                      gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
2690             }
2691           else
2692             undobuf.other_insn = 0;
2693         }
2694 #endif
2695     }
2696   else
2697 #endif
2698     {
2699       /* It is possible that the source of I2 or I1 may be performing
2700          an unneeded operation, such as a ZERO_EXTEND of something
2701          that is known to have the high part zero.  Handle that case
2702          by letting subst look at the innermost one of them.
2703
2704          Another way to do this would be to have a function that tries
2705          to simplify a single insn instead of merging two or more
2706          insns.  We don't do this because of the potential of infinite
2707          loops and because of the potential extra memory required.
2708          However, doing it the way we are is a bit of a kludge and
2709          doesn't catch all cases.
2710
2711          But only do this if -fexpensive-optimizations since it slows
2712          things down and doesn't usually win.
2713
2714          This is not done in the COMPARE case above because the
2715          unmodified I2PAT is used in the PARALLEL and so a pattern
2716          with a modified I2SRC would not match.  */
2717
2718       if (flag_expensive_optimizations)
2719         {
2720           /* Pass pc_rtx so no substitutions are done, just
2721              simplifications.  */
2722           if (i1)
2723             {
2724               subst_low_luid = DF_INSN_LUID (i1);
2725               i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
2726             }
2727           else
2728             {
2729               subst_low_luid = DF_INSN_LUID (i2);
2730               i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
2731             }
2732         }
2733
2734       n_occurrences = 0;                /* `subst' counts here */
2735
2736       /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
2737          need to make a unique copy of I2SRC each time we substitute it
2738          to avoid self-referential rtl.  */
2739
2740       subst_low_luid = DF_INSN_LUID (i2);
2741       newpat = subst (PATTERN (i3), i2dest, i2src, 0,
2742                       ! i1_feeds_i3 && i1dest_in_i1src);
2743       substed_i2 = 1;
2744
2745       /* Record whether i2's body now appears within i3's body.  */
2746       i2_is_used = n_occurrences;
2747     }
2748
2749   /* If we already got a failure, don't try to do more.  Otherwise,
2750      try to substitute in I1 if we have it.  */
2751
2752   if (i1 && GET_CODE (newpat) != CLOBBER)
2753     {
2754       /* Before we can do this substitution, we must redo the test done
2755          above (see detailed comments there) that ensures  that I1DEST
2756          isn't mentioned in any SETs in NEWPAT that are field assignments.  */
2757
2758       if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
2759                               0, (rtx*) 0))
2760         {
2761           undo_all ();
2762           return 0;
2763         }
2764
2765       n_occurrences = 0;
2766       subst_low_luid = DF_INSN_LUID (i1);
2767       newpat = subst (newpat, i1dest, i1src, 0, 0);
2768       substed_i1 = 1;
2769     }
2770
2771   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
2772      to count all the ways that I2SRC and I1SRC can be used.  */
2773   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
2774        && i2_is_used + added_sets_2 > 1)
2775       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2776           && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
2777               > 1))
2778       /* Fail if we tried to make a new register.  */
2779       || max_reg_num () != maxreg
2780       /* Fail if we couldn't do something and have a CLOBBER.  */
2781       || GET_CODE (newpat) == CLOBBER
2782       /* Fail if this new pattern is a MULT and we didn't have one before
2783          at the outer level.  */
2784       || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
2785           && ! have_mult))
2786     {
2787       undo_all ();
2788       return 0;
2789     }
2790
2791   /* If the actions of the earlier insns must be kept
2792      in addition to substituting them into the latest one,
2793      we must make a new PARALLEL for the latest insn
2794      to hold additional the SETs.  */
2795
2796   if (added_sets_1 || added_sets_2)
2797     {
2798       combine_extras++;
2799
2800       if (GET_CODE (newpat) == PARALLEL)
2801         {
2802           rtvec old = XVEC (newpat, 0);
2803           total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
2804           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2805           memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
2806                   sizeof (old->elem[0]) * old->num_elem);
2807         }
2808       else
2809         {
2810           rtx old = newpat;
2811           total_sets = 1 + added_sets_1 + added_sets_2;
2812           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2813           XVECEXP (newpat, 0, 0) = old;
2814         }
2815
2816       if (added_sets_1)
2817         XVECEXP (newpat, 0, --total_sets) = i1pat;
2818
2819       if (added_sets_2)
2820         {
2821           /* If there is no I1, use I2's body as is.  We used to also not do
2822              the subst call below if I2 was substituted into I3,
2823              but that could lose a simplification.  */
2824           if (i1 == 0)
2825             XVECEXP (newpat, 0, --total_sets) = i2pat;
2826           else
2827             /* See comment where i2pat is assigned.  */
2828             XVECEXP (newpat, 0, --total_sets)
2829               = subst (i2pat, i1dest, i1src, 0, 0);
2830         }
2831     }
2832
2833   /* We come here when we are replacing a destination in I2 with the
2834      destination of I3.  */
2835  validate_replacement:
2836
2837   /* Note which hard regs this insn has as inputs.  */
2838   mark_used_regs_combine (newpat);
2839
2840   /* If recog_for_combine fails, it strips existing clobbers.  If we'll
2841      consider splitting this pattern, we might need these clobbers.  */
2842   if (i1 && GET_CODE (newpat) == PARALLEL
2843       && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
2844     {
2845       int len = XVECLEN (newpat, 0);
2846
2847       newpat_vec_with_clobbers = rtvec_alloc (len);
2848       for (i = 0; i < len; i++)
2849         RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
2850     }
2851
2852   /* Is the result of combination a valid instruction?  */
2853   insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2854
2855   /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2856      the second SET's destination is a register that is unused and isn't
2857      marked as an instruction that might trap in an EH region.  In that case,
2858      we just need the first SET.   This can occur when simplifying a divmod
2859      insn.  We *must* test for this case here because the code below that
2860      splits two independent SETs doesn't handle this case correctly when it
2861      updates the register status.
2862
2863      It's pointless doing this if we originally had two sets, one from
2864      i3, and one from i2.  Combining then splitting the parallel results
2865      in the original i2 again plus an invalid insn (which we delete).
2866      The net effect is only to move instructions around, which makes
2867      debug info less accurate.
2868
2869      Also check the case where the first SET's destination is unused.
2870      That would not cause incorrect code, but does cause an unneeded
2871      insn to remain.  */
2872
2873   if (insn_code_number < 0
2874       && !(added_sets_2 && i1 == 0)
2875       && GET_CODE (newpat) == PARALLEL
2876       && XVECLEN (newpat, 0) == 2
2877       && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2878       && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2879       && asm_noperands (newpat) < 0)
2880     {
2881       rtx set0 = XVECEXP (newpat, 0, 0);
2882       rtx set1 = XVECEXP (newpat, 0, 1);
2883       rtx note;
2884
2885       if (((REG_P (SET_DEST (set1))
2886             && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
2887            || (GET_CODE (SET_DEST (set1)) == SUBREG
2888                && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
2889           && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2890               || INTVAL (XEXP (note, 0)) <= 0)
2891           && ! side_effects_p (SET_SRC (set1)))
2892         {
2893           newpat = set0;
2894           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2895         }
2896
2897       else if (((REG_P (SET_DEST (set0))
2898                  && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
2899                 || (GET_CODE (SET_DEST (set0)) == SUBREG
2900                     && find_reg_note (i3, REG_UNUSED,
2901                                       SUBREG_REG (SET_DEST (set0)))))
2902                && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2903                    || INTVAL (XEXP (note, 0)) <= 0)
2904                && ! side_effects_p (SET_SRC (set0)))
2905         {
2906           newpat = set1;
2907           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2908
2909           if (insn_code_number >= 0)
2910             {
2911               /* If we will be able to accept this, we have made a
2912                  change to the destination of I3.  This requires us to
2913                  do a few adjustments.  */
2914
2915               PATTERN (i3) = newpat;
2916               adjust_for_new_dest (i3);
2917             }
2918         }
2919     }
2920
2921   /* If we were combining three insns and the result is a simple SET
2922      with no ASM_OPERANDS that wasn't recognized, try to split it into two
2923      insns.  There are two ways to do this.  It can be split using a
2924      machine-specific method (like when you have an addition of a large
2925      constant) or by combine in the function find_split_point.  */
2926
2927   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2928       && asm_noperands (newpat) < 0)
2929     {
2930       rtx parallel, m_split, *split;
2931
2932       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
2933          use I2DEST as a scratch register will help.  In the latter case,
2934          convert I2DEST to the mode of the source of NEWPAT if we can.  */
2935
2936       m_split = combine_split_insns (newpat, i3);
2937
2938       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2939          inputs of NEWPAT.  */
2940
2941       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2942          possible to try that as a scratch reg.  This would require adding
2943          more code to make it work though.  */
2944
2945       if (m_split == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
2946         {
2947           enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
2948
2949           /* First try to split using the original register as a
2950              scratch register.  */
2951           parallel = gen_rtx_PARALLEL (VOIDmode,
2952                                        gen_rtvec (2, newpat,
2953                                                   gen_rtx_CLOBBER (VOIDmode,
2954                                                                    i2dest)));
2955           m_split = combine_split_insns (parallel, i3);
2956
2957           /* If that didn't work, try changing the mode of I2DEST if
2958              we can.  */
2959           if (m_split == 0
2960               && new_mode != GET_MODE (i2dest)
2961               && new_mode != VOIDmode
2962               && can_change_dest_mode (i2dest, added_sets_2, new_mode))
2963             {
2964               enum machine_mode old_mode = GET_MODE (i2dest);
2965               rtx ni2dest;
2966
2967               if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
2968                 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
2969               else
2970                 {
2971                   SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
2972                   ni2dest = regno_reg_rtx[REGNO (i2dest)];
2973                 }
2974
2975               parallel = (gen_rtx_PARALLEL
2976                           (VOIDmode,
2977                            gen_rtvec (2, newpat,
2978                                       gen_rtx_CLOBBER (VOIDmode,
2979                                                        ni2dest))));
2980               m_split = combine_split_insns (parallel, i3);
2981
2982               if (m_split == 0
2983                   && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2984                 {
2985                   struct undo *buf;
2986
2987                   PUT_MODE (regno_reg_rtx[REGNO (i2dest)], old_mode);
2988                   buf = undobuf.undos;
2989                   undobuf.undos = buf->next;
2990                   buf->next = undobuf.frees;
2991                   undobuf.frees = buf;
2992                 }
2993             }
2994         }
2995
2996       /* If recog_for_combine has discarded clobbers, try to use them
2997          again for the split.  */
2998       if (m_split == 0 && newpat_vec_with_clobbers)
2999         {
3000           parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3001           m_split = combine_split_insns (parallel, i3);
3002         }
3003
3004       if (m_split && NEXT_INSN (m_split) == NULL_RTX)
3005         {
3006           m_split = PATTERN (m_split);
3007           insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
3008           if (insn_code_number >= 0)
3009             newpat = m_split;
3010         }
3011       else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
3012                && (next_real_insn (i2) == i3
3013                    || ! use_crosses_set_p (PATTERN (m_split), DF_INSN_LUID (i2))))
3014         {
3015           rtx i2set, i3set;
3016           rtx newi3pat = PATTERN (NEXT_INSN (m_split));
3017           newi2pat = PATTERN (m_split);
3018
3019           i3set = single_set (NEXT_INSN (m_split));
3020           i2set = single_set (m_split);
3021
3022           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3023
3024           /* If I2 or I3 has multiple SETs, we won't know how to track
3025              register status, so don't use these insns.  If I2's destination
3026              is used between I2 and I3, we also can't use these insns.  */
3027
3028           if (i2_code_number >= 0 && i2set && i3set
3029               && (next_real_insn (i2) == i3
3030                   || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3031             insn_code_number = recog_for_combine (&newi3pat, i3,
3032                                                   &new_i3_notes);
3033           if (insn_code_number >= 0)
3034             newpat = newi3pat;
3035
3036           /* It is possible that both insns now set the destination of I3.
3037              If so, we must show an extra use of it.  */
3038
3039           if (insn_code_number >= 0)
3040             {
3041               rtx new_i3_dest = SET_DEST (i3set);
3042               rtx new_i2_dest = SET_DEST (i2set);
3043
3044               while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3045                      || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3046                      || GET_CODE (new_i3_dest) == SUBREG)
3047                 new_i3_dest = XEXP (new_i3_dest, 0);
3048
3049               while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3050                      || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3051                      || GET_CODE (new_i2_dest) == SUBREG)
3052                 new_i2_dest = XEXP (new_i2_dest, 0);
3053
3054               if (REG_P (new_i3_dest)
3055                   && REG_P (new_i2_dest)
3056                   && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3057                 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3058             }
3059         }
3060
3061       /* If we can split it and use I2DEST, go ahead and see if that
3062          helps things be recognized.  Verify that none of the registers
3063          are set between I2 and I3.  */
3064       if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
3065 #ifdef HAVE_cc0
3066           && REG_P (i2dest)
3067 #endif
3068           /* We need I2DEST in the proper mode.  If it is a hard register
3069              or the only use of a pseudo, we can change its mode.
3070              Make sure we don't change a hard register to have a mode that
3071              isn't valid for it, or change the number of registers.  */
3072           && (GET_MODE (*split) == GET_MODE (i2dest)
3073               || GET_MODE (*split) == VOIDmode
3074               || can_change_dest_mode (i2dest, added_sets_2,
3075                                        GET_MODE (*split)))
3076           && (next_real_insn (i2) == i3
3077               || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3078           /* We can't overwrite I2DEST if its value is still used by
3079              NEWPAT.  */
3080           && ! reg_referenced_p (i2dest, newpat))
3081         {
3082           rtx newdest = i2dest;
3083           enum rtx_code split_code = GET_CODE (*split);
3084           enum machine_mode split_mode = GET_MODE (*split);
3085           bool subst_done = false;
3086           newi2pat = NULL_RTX;
3087
3088           /* Get NEWDEST as a register in the proper mode.  We have already
3089              validated that we can do this.  */
3090           if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3091             {
3092               if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3093                 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3094               else
3095                 {
3096                   SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3097                   newdest = regno_reg_rtx[REGNO (i2dest)];
3098                 }
3099             }
3100
3101           /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3102              an ASHIFT.  This can occur if it was inside a PLUS and hence
3103              appeared to be a memory address.  This is a kludge.  */
3104           if (split_code == MULT
3105               && GET_CODE (XEXP (*split, 1)) == CONST_INT
3106               && INTVAL (XEXP (*split, 1)) > 0
3107               && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
3108             {
3109               SUBST (*split, gen_rtx_ASHIFT (split_mode,
3110                                              XEXP (*split, 0), GEN_INT (i)));
3111               /* Update split_code because we may not have a multiply
3112                  anymore.  */
3113               split_code = GET_CODE (*split);
3114             }
3115
3116 #ifdef INSN_SCHEDULING
3117           /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3118              be written as a ZERO_EXTEND.  */
3119           if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3120             {
3121 #ifdef LOAD_EXTEND_OP
3122               /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3123                  what it really is.  */
3124               if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3125                   == SIGN_EXTEND)
3126                 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3127                                                     SUBREG_REG (*split)));
3128               else
3129 #endif
3130                 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3131                                                     SUBREG_REG (*split)));
3132             }
3133 #endif
3134
3135           /* Attempt to split binary operators using arithmetic identities.  */
3136           if (BINARY_P (SET_SRC (newpat))
3137               && split_mode == GET_MODE (SET_SRC (newpat))
3138               && ! side_effects_p (SET_SRC (newpat)))
3139             {
3140               rtx setsrc = SET_SRC (newpat);
3141               enum machine_mode mode = GET_MODE (setsrc);
3142               enum rtx_code code = GET_CODE (setsrc);
3143               rtx src_op0 = XEXP (setsrc, 0);
3144               rtx src_op1 = XEXP (setsrc, 1);
3145
3146               /* Split "X = Y op Y" as "Z = Y; X = Z op Z".  */
3147               if (rtx_equal_p (src_op0, src_op1))
3148                 {
3149                   newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3150                   SUBST (XEXP (setsrc, 0), newdest);
3151                   SUBST (XEXP (setsrc, 1), newdest);
3152                   subst_done = true;
3153                 }
3154               /* Split "((P op Q) op R) op S" where op is PLUS or MULT.  */
3155               else if ((code == PLUS || code == MULT)
3156                        && GET_CODE (src_op0) == code
3157                        && GET_CODE (XEXP (src_op0, 0)) == code
3158                        && (INTEGRAL_MODE_P (mode)
3159                            || (FLOAT_MODE_P (mode)
3160                                && flag_unsafe_math_optimizations)))
3161                 {
3162                   rtx p = XEXP (XEXP (src_op0, 0), 0);
3163                   rtx q = XEXP (XEXP (src_op0, 0), 1);
3164                   rtx r = XEXP (src_op0, 1);
3165                   rtx s = src_op1;
3166
3167                   /* Split both "((X op Y) op X) op Y" and
3168                      "((X op Y) op Y) op X" as "T op T" where T is
3169                      "X op Y".  */
3170                   if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3171                        || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3172                     {
3173                       newi2pat = gen_rtx_SET (VOIDmode, newdest,
3174                                               XEXP (src_op0, 0));
3175                       SUBST (XEXP (setsrc, 0), newdest);
3176                       SUBST (XEXP (setsrc, 1), newdest);
3177                       subst_done = true;
3178                     }
3179                   /* Split "((X op X) op Y) op Y)" as "T op T" where
3180                      T is "X op Y".  */
3181                   else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3182                     {
3183                       rtx tmp = simplify_gen_binary (code, mode, p, r);
3184                       newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3185                       SUBST (XEXP (setsrc, 0), newdest);
3186                       SUBST (XEXP (setsrc, 1), newdest);
3187                       subst_done = true;
3188                     }
3189                 }
3190             }
3191
3192           if (!subst_done)
3193             {
3194               newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3195               SUBST (*split, newdest);
3196             }
3197
3198           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3199
3200           /* recog_for_combine might have added CLOBBERs to newi2pat.
3201              Make sure NEWPAT does not depend on the clobbered regs.  */
3202           if (GET_CODE (newi2pat) == PARALLEL)
3203             for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3204               if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3205                 {
3206                   rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3207                   if (reg_overlap_mentioned_p (reg, newpat))
3208                     {
3209                       undo_all ();
3210                       return 0;
3211                     }
3212                 }
3213
3214           /* If the split point was a MULT and we didn't have one before,
3215              don't use one now.  */
3216           if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3217             insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3218         }
3219     }
3220
3221   /* Check for a case where we loaded from memory in a narrow mode and
3222      then sign extended it, but we need both registers.  In that case,
3223      we have a PARALLEL with both loads from the same memory location.
3224      We can split this into a load from memory followed by a register-register
3225      copy.  This saves at least one insn, more if register allocation can
3226      eliminate the copy.
3227
3228      We cannot do this if the destination of the first assignment is a
3229      condition code register or cc0.  We eliminate this case by making sure
3230      the SET_DEST and SET_SRC have the same mode.
3231
3232      We cannot do this if the destination of the second assignment is
3233      a register that we have already assumed is zero-extended.  Similarly
3234      for a SUBREG of such a register.  */
3235
3236   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3237            && GET_CODE (newpat) == PARALLEL
3238            && XVECLEN (newpat, 0) == 2
3239            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3240            && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3241            && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3242                == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3243            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3244            && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3245                            XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3246            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3247                                    DF_INSN_LUID (i2))
3248            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3249            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3250            && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
3251                  (REG_P (temp)
3252                   && VEC_index (reg_stat_type, reg_stat,
3253                                 REGNO (temp))->nonzero_bits != 0
3254                   && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3255                   && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3256                   && (VEC_index (reg_stat_type, reg_stat,
3257                                  REGNO (temp))->nonzero_bits
3258                       != GET_MODE_MASK (word_mode))))
3259            && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3260                  && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3261                      (REG_P (temp)
3262                       && VEC_index (reg_stat_type, reg_stat,
3263                                     REGNO (temp))->nonzero_bits != 0
3264                       && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3265                       && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3266                       && (VEC_index (reg_stat_type, reg_stat,
3267                                      REGNO (temp))->nonzero_bits
3268                           != GET_MODE_MASK (word_mode)))))
3269            && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3270                                          SET_SRC (XVECEXP (newpat, 0, 1)))
3271            && ! find_reg_note (i3, REG_UNUSED,
3272                                SET_DEST (XVECEXP (newpat, 0, 0))))
3273     {
3274       rtx ni2dest;
3275
3276       newi2pat = XVECEXP (newpat, 0, 0);
3277       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3278       newpat = XVECEXP (newpat, 0, 1);
3279       SUBST (SET_SRC (newpat),
3280              gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3281       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3282
3283       if (i2_code_number >= 0)
3284         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3285
3286       if (insn_code_number >= 0)
3287         swap_i2i3 = 1;
3288     }
3289
3290   /* Similarly, check for a case where we have a PARALLEL of two independent
3291      SETs but we started with three insns.  In this case, we can do the sets
3292      as two separate insns.  This case occurs when some SET allows two
3293      other insns to combine, but the destination of that SET is still live.  */
3294
3295   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3296            && GET_CODE (newpat) == PARALLEL
3297            && XVECLEN (newpat, 0) == 2
3298            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3299            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3300            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3301            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3302            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3303            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3304            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3305                                    DF_INSN_LUID (i2))
3306            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3307                                   XVECEXP (newpat, 0, 0))
3308            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3309                                   XVECEXP (newpat, 0, 1))
3310            && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3311                  && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1))))
3312 #ifdef HAVE_cc0
3313            /* We cannot split the parallel into two sets if both sets
3314               reference cc0.  */
3315            && ! (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0))
3316                  && reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 1)))
3317 #endif
3318            )
3319     {
3320       /* Normally, it doesn't matter which of the two is done first,
3321          but it does if one references cc0.  In that case, it has to
3322          be first.  */
3323 #ifdef HAVE_cc0
3324       if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
3325         {
3326           newi2pat = XVECEXP (newpat, 0, 0);
3327           newpat = XVECEXP (newpat, 0, 1);
3328         }
3329       else
3330 #endif
3331         {
3332           newi2pat = XVECEXP (newpat, 0, 1);
3333           newpat = XVECEXP (newpat, 0, 0);
3334         }
3335
3336       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3337
3338       if (i2_code_number >= 0)
3339         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3340     }
3341
3342   /* If it still isn't recognized, fail and change things back the way they
3343      were.  */
3344   if ((insn_code_number < 0
3345        /* Is the result a reasonable ASM_OPERANDS?  */
3346        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3347     {
3348       undo_all ();
3349       return 0;
3350     }
3351
3352   /* If we had to change another insn, make sure it is valid also.  */
3353   if (undobuf.other_insn)
3354     {
3355       CLEAR_HARD_REG_SET (newpat_used_regs);
3356
3357       other_pat = PATTERN (undobuf.other_insn);
3358       other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3359                                              &new_other_notes);
3360
3361       if (other_code_number < 0 && ! check_asm_operands (other_pat))
3362         {
3363           undo_all ();
3364           return 0;
3365         }
3366     }
3367
3368 #ifdef HAVE_cc0
3369   /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3370      they are adjacent to each other or not.  */
3371   {
3372     rtx p = prev_nonnote_insn (i3);
3373     if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3374         && sets_cc0_p (newi2pat))
3375       {
3376         undo_all ();
3377         return 0;
3378       }
3379   }
3380 #endif
3381
3382   /* Only allow this combination if insn_rtx_costs reports that the
3383      replacement instructions are cheaper than the originals.  */
3384   if (!combine_validate_cost (i1, i2, i3, newpat, newi2pat, other_pat))
3385     {
3386       undo_all ();
3387       return 0;
3388     }
3389
3390   /* We now know that we can do this combination.  Merge the insns and
3391      update the status of registers and LOG_LINKS.  */
3392
3393   if (undobuf.other_insn)
3394     {
3395       rtx note, next;
3396
3397       PATTERN (undobuf.other_insn) = other_pat;
3398
3399       /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
3400          are still valid.  Then add any non-duplicate notes added by
3401          recog_for_combine.  */
3402       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
3403         {
3404           next = XEXP (note, 1);
3405
3406           if (REG_NOTE_KIND (note) == REG_UNUSED
3407               && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
3408             remove_note (undobuf.other_insn, note);
3409         }
3410
3411       distribute_notes (new_other_notes, undobuf.other_insn,
3412                         undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
3413     }
3414
3415   if (swap_i2i3)
3416     {
3417       rtx insn;
3418       rtx link;
3419       rtx ni2dest;
3420
3421       /* I3 now uses what used to be its destination and which is now
3422          I2's destination.  This requires us to do a few adjustments.  */
3423       PATTERN (i3) = newpat;
3424       adjust_for_new_dest (i3);
3425
3426       /* We need a LOG_LINK from I3 to I2.  But we used to have one,
3427          so we still will.
3428
3429          However, some later insn might be using I2's dest and have
3430          a LOG_LINK pointing at I3.  We must remove this link.
3431          The simplest way to remove the link is to point it at I1,
3432          which we know will be a NOTE.  */
3433
3434       /* newi2pat is usually a SET here; however, recog_for_combine might
3435          have added some clobbers.  */
3436       if (GET_CODE (newi2pat) == PARALLEL)
3437         ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
3438       else
3439         ni2dest = SET_DEST (newi2pat);
3440
3441       for (insn = NEXT_INSN (i3);
3442            insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3443                     || insn != BB_HEAD (this_basic_block->next_bb));
3444            insn = NEXT_INSN (insn))
3445         {
3446           if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
3447             {
3448               for (link = LOG_LINKS (insn); link;
3449                    link = XEXP (link, 1))
3450                 if (XEXP (link, 0) == i3)
3451                   XEXP (link, 0) = i1;
3452
3453               break;
3454             }
3455         }
3456     }
3457
3458   {
3459     rtx i3notes, i2notes, i1notes = 0;
3460     rtx i3links, i2links, i1links = 0;
3461     rtx midnotes = 0;
3462     unsigned int regno;
3463     /* Compute which registers we expect to eliminate.  newi2pat may be setting
3464        either i3dest or i2dest, so we must check it.  Also, i1dest may be the
3465        same as i3dest, in which case newi2pat may be setting i1dest.  */
3466     rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
3467                    || i2dest_in_i2src || i2dest_in_i1src
3468                    || !i2dest_killed
3469                    ? 0 : i2dest);
3470     rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
3471                    || (newi2pat && reg_set_p (i1dest, newi2pat))
3472                    || !i1dest_killed
3473                    ? 0 : i1dest);
3474
3475     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
3476        clear them.  */
3477     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
3478     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
3479     if (i1)
3480       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
3481
3482     /* Ensure that we do not have something that should not be shared but
3483        occurs multiple times in the new insns.  Check this by first
3484        resetting all the `used' flags and then copying anything is shared.  */
3485
3486     reset_used_flags (i3notes);
3487     reset_used_flags (i2notes);
3488     reset_used_flags (i1notes);
3489     reset_used_flags (newpat);
3490     reset_used_flags (newi2pat);
3491     if (undobuf.other_insn)
3492       reset_used_flags (PATTERN (undobuf.other_insn));
3493
3494     i3notes = copy_rtx_if_shared (i3notes);
3495     i2notes = copy_rtx_if_shared (i2notes);
3496     i1notes = copy_rtx_if_shared (i1notes);
3497     newpat = copy_rtx_if_shared (newpat);
3498     newi2pat = copy_rtx_if_shared (newi2pat);
3499     if (undobuf.other_insn)
3500       reset_used_flags (PATTERN (undobuf.other_insn));
3501
3502     INSN_CODE (i3) = insn_code_number;
3503     PATTERN (i3) = newpat;
3504
3505     if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
3506       {
3507         rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
3508
3509         reset_used_flags (call_usage);
3510         call_usage = copy_rtx (call_usage);
3511
3512         if (substed_i2)
3513           replace_rtx (call_usage, i2dest, i2src);
3514
3515         if (substed_i1)
3516           replace_rtx (call_usage, i1dest, i1src);
3517
3518         CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
3519       }
3520
3521     if (undobuf.other_insn)
3522       INSN_CODE (undobuf.other_insn) = other_code_number;
3523
3524     /* We had one special case above where I2 had more than one set and
3525        we replaced a destination of one of those sets with the destination
3526        of I3.  In that case, we have to update LOG_LINKS of insns later
3527        in this basic block.  Note that this (expensive) case is rare.
3528
3529        Also, in this case, we must pretend that all REG_NOTEs for I2
3530        actually came from I3, so that REG_UNUSED notes from I2 will be
3531        properly handled.  */
3532
3533     if (i3_subst_into_i2)
3534       {
3535         for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
3536           if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
3537                || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
3538               && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
3539               && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
3540               && ! find_reg_note (i2, REG_UNUSED,
3541                                   SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
3542             for (temp = NEXT_INSN (i2);
3543                  temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3544                           || BB_HEAD (this_basic_block) != temp);
3545                  temp = NEXT_INSN (temp))
3546               if (temp != i3 && INSN_P (temp))
3547                 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
3548                   if (XEXP (link, 0) == i2)
3549                     XEXP (link, 0) = i3;
3550
3551         if (i3notes)
3552           {
3553             rtx link = i3notes;
3554             while (XEXP (link, 1))
3555               link = XEXP (link, 1);
3556             XEXP (link, 1) = i2notes;
3557           }
3558         else
3559           i3notes = i2notes;
3560         i2notes = 0;
3561       }
3562
3563     LOG_LINKS (i3) = 0;
3564     REG_NOTES (i3) = 0;
3565     LOG_LINKS (i2) = 0;
3566     REG_NOTES (i2) = 0;
3567
3568     if (newi2pat)
3569       {
3570         INSN_CODE (i2) = i2_code_number;
3571         PATTERN (i2) = newi2pat;
3572       }
3573     else
3574       SET_INSN_DELETED (i2);
3575
3576     if (i1)
3577       {
3578         LOG_LINKS (i1) = 0;
3579         REG_NOTES (i1) = 0;
3580         SET_INSN_DELETED (i1);
3581       }
3582
3583     /* Get death notes for everything that is now used in either I3 or
3584        I2 and used to die in a previous insn.  If we built two new
3585        patterns, move from I1 to I2 then I2 to I3 so that we get the
3586        proper movement on registers that I2 modifies.  */
3587
3588     if (newi2pat)
3589       {
3590         move_deaths (newi2pat, NULL_RTX, DF_INSN_LUID (i1), i2, &midnotes);
3591         move_deaths (newpat, newi2pat, DF_INSN_LUID (i1), i3, &midnotes);
3592       }
3593     else
3594       move_deaths (newpat, NULL_RTX, i1 ? DF_INSN_LUID (i1) : DF_INSN_LUID (i2),
3595                    i3, &midnotes);
3596
3597     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
3598     if (i3notes)
3599       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
3600                         elim_i2, elim_i1);
3601     if (i2notes)
3602       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
3603                         elim_i2, elim_i1);
3604     if (i1notes)
3605       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
3606                         elim_i2, elim_i1);
3607     if (midnotes)
3608       distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3609                         elim_i2, elim_i1);
3610
3611     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
3612        know these are REG_UNUSED and want them to go to the desired insn,
3613        so we always pass it as i3.  */
3614
3615     if (newi2pat && new_i2_notes)
3616       distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3617     
3618     if (new_i3_notes)
3619       distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
3620
3621     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
3622        put a REG_DEAD note for it somewhere.  If NEWI2PAT exists and sets
3623        I3DEST, the death must be somewhere before I2, not I3.  If we passed I3
3624        in that case, it might delete I2.  Similarly for I2 and I1.
3625        Show an additional death due to the REG_DEAD note we make here.  If
3626        we discard it in distribute_notes, we will decrement it again.  */
3627
3628     if (i3dest_killed)
3629       {
3630         if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
3631           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
3632                                                NULL_RTX),
3633                             NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
3634         else
3635           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
3636                                                NULL_RTX),
3637                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3638                             elim_i2, elim_i1);
3639       }
3640
3641     if (i2dest_in_i2src)
3642       {
3643         if (newi2pat && reg_set_p (i2dest, newi2pat))
3644           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
3645                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3646         else
3647           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
3648                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3649                             NULL_RTX, NULL_RTX);
3650       }
3651
3652     if (i1dest_in_i1src)
3653       {
3654         if (newi2pat && reg_set_p (i1dest, newi2pat))
3655           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
3656                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3657         else
3658           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
3659                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3660                             NULL_RTX, NULL_RTX);
3661       }
3662
3663     distribute_links (i3links);
3664     distribute_links (i2links);
3665     distribute_links (i1links);
3666
3667     if (REG_P (i2dest))
3668       {
3669         rtx link;
3670         rtx i2_insn = 0, i2_val = 0, set;
3671
3672         /* The insn that used to set this register doesn't exist, and
3673            this life of the register may not exist either.  See if one of
3674            I3's links points to an insn that sets I2DEST.  If it does,
3675            that is now the last known value for I2DEST. If we don't update
3676            this and I2 set the register to a value that depended on its old
3677            contents, we will get confused.  If this insn is used, thing
3678            will be set correctly in combine_instructions.  */
3679
3680         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3681           if ((set = single_set (XEXP (link, 0))) != 0
3682               && rtx_equal_p (i2dest, SET_DEST (set)))
3683             i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
3684
3685         record_value_for_reg (i2dest, i2_insn, i2_val);
3686
3687         /* If the reg formerly set in I2 died only once and that was in I3,
3688            zero its use count so it won't make `reload' do any work.  */
3689         if (! added_sets_2
3690             && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
3691             && ! i2dest_in_i2src)
3692           {
3693             regno = REGNO (i2dest);
3694             INC_REG_N_SETS (regno, -1);
3695           }
3696       }
3697
3698     if (i1 && REG_P (i1dest))
3699       {
3700         rtx link;
3701         rtx i1_insn = 0, i1_val = 0, set;
3702
3703         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3704           if ((set = single_set (XEXP (link, 0))) != 0
3705               && rtx_equal_p (i1dest, SET_DEST (set)))
3706             i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
3707
3708         record_value_for_reg (i1dest, i1_insn, i1_val);
3709
3710         regno = REGNO (i1dest);
3711         if (! added_sets_1 && ! i1dest_in_i1src)
3712           INC_REG_N_SETS (regno, -1);
3713       }
3714
3715     /* Update reg_stat[].nonzero_bits et al for any changes that may have
3716        been made to this insn.  The order of
3717        set_nonzero_bits_and_sign_copies() is important.  Because newi2pat
3718        can affect nonzero_bits of newpat */
3719     if (newi2pat)
3720       note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
3721     note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
3722
3723     /* Set new_direct_jump_p if a new return or simple jump instruction
3724        has been created.
3725
3726        If I3 is now an unconditional jump, ensure that it has a
3727        BARRIER following it since it may have initially been a
3728        conditional jump.  It may also be the last nonnote insn.  */
3729
3730     if (returnjump_p (i3) || any_uncondjump_p (i3))
3731       {
3732         *new_direct_jump_p = 1;
3733         mark_jump_label (PATTERN (i3), i3, 0);
3734
3735         if ((temp = next_nonnote_insn (i3)) == NULL_RTX
3736             || !BARRIER_P (temp))
3737           emit_barrier_after (i3);
3738       }
3739
3740     if (undobuf.other_insn != NULL_RTX
3741         && (returnjump_p (undobuf.other_insn)
3742             || any_uncondjump_p (undobuf.other_insn)))
3743       {
3744         *new_direct_jump_p = 1;
3745
3746         if ((temp = next_nonnote_insn (undobuf.other_insn)) == NULL_RTX
3747             || !BARRIER_P (temp))
3748           emit_barrier_after (undobuf.other_insn);
3749       }
3750
3751     /* An NOOP jump does not need barrier, but it does need cleaning up
3752        of CFG.  */
3753     if (GET_CODE (newpat) == SET
3754         && SET_SRC (newpat) == pc_rtx
3755         && SET_DEST (newpat) == pc_rtx)
3756       *new_direct_jump_p = 1;
3757   }
3758   
3759   if (undobuf.other_insn != NULL_RTX)
3760     {
3761       if (dump_file)
3762         {
3763           fprintf (dump_file, "modifying other_insn ");
3764           dump_insn_slim (dump_file, undobuf.other_insn);
3765         }
3766       df_insn_rescan (undobuf.other_insn);
3767     }
3768
3769   if (i1 && !(NOTE_P(i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
3770     {
3771       if (dump_file)
3772         {
3773           fprintf (dump_file, "modifying insn i1 ");
3774           dump_insn_slim (dump_file, i1);
3775         }
3776       df_insn_rescan (i1);
3777     }
3778
3779   if (i2 && !(NOTE_P(i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
3780     {
3781       if (dump_file)
3782         {
3783           fprintf (dump_file, "modifying insn i2 ");
3784           dump_insn_slim (dump_file, i2);
3785         }
3786       df_insn_rescan (i2);
3787     }
3788
3789   if (i3 && !(NOTE_P(i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
3790     {
3791       if (dump_file)
3792         {
3793           fprintf (dump_file, "modifying insn i3 ");
3794           dump_insn_slim (dump_file, i3);
3795         }
3796       df_insn_rescan (i3);
3797     }
3798   
3799   combine_successes++;
3800   undo_commit ();
3801
3802   if (added_links_insn
3803       && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
3804       && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
3805     return added_links_insn;
3806   else
3807     return newi2pat ? i2 : i3;
3808 }
3809 \f
3810 /* Undo all the modifications recorded in undobuf.  */
3811
3812 static void
3813 undo_all (void)
3814 {
3815   struct undo *undo, *next;
3816
3817   for (undo = undobuf.undos; undo; undo = next)
3818     {
3819       next = undo->next;
3820       switch (undo->kind)
3821         {
3822         case UNDO_RTX:
3823           *undo->where.r = undo->old_contents.r;
3824           break;
3825         case UNDO_INT:
3826           *undo->where.i = undo->old_contents.i;
3827           break;
3828         case UNDO_MODE:
3829           PUT_MODE (*undo->where.r, undo->old_contents.m);
3830           break;
3831         default:
3832           gcc_unreachable ();
3833         }
3834
3835       undo->next = undobuf.frees;
3836       undobuf.frees = undo;
3837     }
3838
3839   undobuf.undos = 0;
3840 }
3841
3842 /* We've committed to accepting the changes we made.  Move all
3843    of the undos to the free list.  */
3844
3845 static void
3846 undo_commit (void)
3847 {
3848   struct undo *undo, *next;
3849
3850   for (undo = undobuf.undos; undo; undo = next)
3851     {
3852       next = undo->next;
3853       undo->next = undobuf.frees;
3854       undobuf.frees = undo;
3855     }
3856   undobuf.undos = 0;
3857 }
3858 \f
3859 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
3860    where we have an arithmetic expression and return that point.  LOC will
3861    be inside INSN.
3862
3863    try_combine will call this function to see if an insn can be split into
3864    two insns.  */
3865
3866 static rtx *
3867 find_split_point (rtx *loc, rtx insn)
3868 {
3869   rtx x = *loc;
3870   enum rtx_code code = GET_CODE (x);
3871   rtx *split;
3872   unsigned HOST_WIDE_INT len = 0;
3873   HOST_WIDE_INT pos = 0;
3874   int unsignedp = 0;
3875   rtx inner = NULL_RTX;
3876
3877   /* First special-case some codes.  */
3878   switch (code)
3879     {
3880     case SUBREG:
3881 #ifdef INSN_SCHEDULING
3882       /* If we are making a paradoxical SUBREG invalid, it becomes a split
3883          point.  */
3884       if (MEM_P (SUBREG_REG (x)))
3885         return loc;
3886 #endif
3887       return find_split_point (&SUBREG_REG (x), insn);
3888
3889     case MEM:
3890 #ifdef HAVE_lo_sum
3891       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
3892          using LO_SUM and HIGH.  */
3893       if (GET_CODE (XEXP (x, 0)) == CONST
3894           || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
3895         {
3896           SUBST (XEXP (x, 0),
3897                  gen_rtx_LO_SUM (Pmode,
3898                                  gen_rtx_HIGH (Pmode, XEXP (x, 0)),
3899                                  XEXP (x, 0)));
3900           return &XEXP (XEXP (x, 0), 0);
3901         }
3902 #endif
3903
3904       /* If we have a PLUS whose second operand is a constant and the
3905          address is not valid, perhaps will can split it up using
3906          the machine-specific way to split large constants.  We use
3907          the first pseudo-reg (one of the virtual regs) as a placeholder;
3908          it will not remain in the result.  */
3909       if (GET_CODE (XEXP (x, 0)) == PLUS
3910           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3911           && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
3912         {
3913           rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
3914           rtx seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
3915                                                       XEXP (x, 0)),
3916                                          subst_insn);
3917
3918           /* This should have produced two insns, each of which sets our
3919              placeholder.  If the source of the second is a valid address,
3920              we can make put both sources together and make a split point
3921              in the middle.  */
3922
3923           if (seq
3924               && NEXT_INSN (seq) != NULL_RTX
3925               && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
3926               && NONJUMP_INSN_P (seq)
3927               && GET_CODE (PATTERN (seq)) == SET
3928               && SET_DEST (PATTERN (seq)) == reg
3929               && ! reg_mentioned_p (reg,
3930                                     SET_SRC (PATTERN (seq)))
3931               && NONJUMP_INSN_P (NEXT_INSN (seq))
3932               && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
3933               && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
3934               && memory_address_p (GET_MODE (x),
3935                                    SET_SRC (PATTERN (NEXT_INSN (seq)))))
3936             {
3937               rtx src1 = SET_SRC (PATTERN (seq));
3938               rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
3939
3940               /* Replace the placeholder in SRC2 with SRC1.  If we can
3941                  find where in SRC2 it was placed, that can become our
3942                  split point and we can replace this address with SRC2.
3943                  Just try two obvious places.  */
3944
3945               src2 = replace_rtx (src2, reg, src1);
3946               split = 0;
3947               if (XEXP (src2, 0) == src1)
3948                 split = &XEXP (src2, 0);
3949               else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
3950                        && XEXP (XEXP (src2, 0), 0) == src1)
3951                 split = &XEXP (XEXP (src2, 0), 0);
3952
3953               if (split)
3954                 {
3955                   SUBST (XEXP (x, 0), src2);
3956                   return split;
3957                 }
3958             }
3959
3960           /* If that didn't work, perhaps the first operand is complex and
3961              needs to be computed separately, so make a split point there.
3962              This will occur on machines that just support REG + CONST
3963              and have a constant moved through some previous computation.  */
3964
3965           else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
3966                    && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
3967                          && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
3968             return &XEXP (XEXP (x, 0), 0);
3969         }
3970       break;
3971
3972     case SET:
3973 #ifdef HAVE_cc0
3974       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
3975          ZERO_EXTRACT, the most likely reason why this doesn't match is that
3976          we need to put the operand into a register.  So split at that
3977          point.  */
3978
3979       if (SET_DEST (x) == cc0_rtx
3980           && GET_CODE (SET_SRC (x)) != COMPARE
3981           && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
3982           && !OBJECT_P (SET_SRC (x))
3983           && ! (GET_CODE (SET_SRC (x)) == SUBREG
3984                 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
3985         return &SET_SRC (x);
3986 #endif
3987
3988       /* See if we can split SET_SRC as it stands.  */
3989       split = find_split_point (&SET_SRC (x), insn);
3990       if (split && split != &SET_SRC (x))
3991         return split;
3992
3993       /* See if we can split SET_DEST as it stands.  */
3994       split = find_split_point (&SET_DEST (x), insn);
3995       if (split && split != &SET_DEST (x))
3996         return split;
3997
3998       /* See if this is a bitfield assignment with everything constant.  If
3999          so, this is an IOR of an AND, so split it into that.  */
4000       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4001           && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
4002               <= HOST_BITS_PER_WIDE_INT)
4003           && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
4004           && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
4005           && GET_CODE (SET_SRC (x)) == CONST_INT
4006           && ((INTVAL (XEXP (SET_DEST (x), 1))
4007                + INTVAL (XEXP (SET_DEST (x), 2)))
4008               <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
4009           && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4010         {
4011           HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4012           unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4013           unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4014           rtx dest = XEXP (SET_DEST (x), 0);
4015           enum machine_mode mode = GET_MODE (dest);
4016           unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
4017           rtx or_mask;
4018
4019           if (BITS_BIG_ENDIAN)
4020             pos = GET_MODE_BITSIZE (mode) - len - pos;
4021
4022           or_mask = gen_int_mode (src << pos, mode);
4023           if (src == mask)
4024             SUBST (SET_SRC (x),
4025                    simplify_gen_binary (IOR, mode, dest, or_mask));
4026           else
4027             {
4028               rtx negmask = gen_int_mode (~(mask << pos), mode);
4029               SUBST (SET_SRC (x),
4030                      simplify_gen_binary (IOR, mode,
4031                                           simplify_gen_binary (AND, mode,
4032                                                                dest, negmask),
4033                                           or_mask));
4034             }
4035
4036           SUBST (SET_DEST (x), dest);
4037
4038           split = find_split_point (&SET_SRC (x), insn);
4039           if (split && split != &SET_SRC (x))
4040             return split;
4041         }
4042
4043       /* Otherwise, see if this is an operation that we can split into two.
4044          If so, try to split that.  */
4045       code = GET_CODE (SET_SRC (x));
4046
4047       switch (code)
4048         {
4049         case AND:
4050           /* If we are AND'ing with a large constant that is only a single
4051              bit and the result is only being used in a context where we
4052              need to know if it is zero or nonzero, replace it with a bit
4053              extraction.  This will avoid the large constant, which might
4054              have taken more than one insn to make.  If the constant were
4055              not a valid argument to the AND but took only one insn to make,
4056              this is no worse, but if it took more than one insn, it will
4057              be better.  */
4058
4059           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
4060               && REG_P (XEXP (SET_SRC (x), 0))
4061               && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4062               && REG_P (SET_DEST (x))
4063               && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
4064               && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4065               && XEXP (*split, 0) == SET_DEST (x)
4066               && XEXP (*split, 1) == const0_rtx)
4067             {
4068               rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4069                                                 XEXP (SET_SRC (x), 0),
4070                                                 pos, NULL_RTX, 1, 1, 0, 0);
4071               if (extraction != 0)
4072                 {
4073                   SUBST (SET_SRC (x), extraction);
4074                   return find_split_point (loc, insn);
4075                 }
4076             }
4077           break;
4078
4079         case NE:
4080           /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4081              is known to be on, this can be converted into a NEG of a shift.  */
4082           if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4083               && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4084               && 1 <= (pos = exact_log2
4085                        (nonzero_bits (XEXP (SET_SRC (x), 0),
4086                                       GET_MODE (XEXP (SET_SRC (x), 0))))))
4087             {
4088               enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4089
4090               SUBST (SET_SRC (x),
4091                      gen_rtx_NEG (mode,
4092                                   gen_rtx_LSHIFTRT (mode,
4093                                                     XEXP (SET_SRC (x), 0),
4094                                                     GEN_INT (pos))));
4095
4096               split = find_split_point (&SET_SRC (x), insn);
4097               if (split && split != &SET_SRC (x))
4098                 return split;
4099             }
4100           break;
4101
4102         case SIGN_EXTEND:
4103           inner = XEXP (SET_SRC (x), 0);
4104
4105           /* We can't optimize if either mode is a partial integer
4106              mode as we don't know how many bits are significant
4107              in those modes.  */
4108           if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4109               || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4110             break;
4111
4112           pos = 0;
4113           len = GET_MODE_BITSIZE (GET_MODE (inner));
4114           unsignedp = 0;
4115           break;
4116
4117         case SIGN_EXTRACT:
4118         case ZERO_EXTRACT:
4119           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
4120               && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
4121             {
4122               inner = XEXP (SET_SRC (x), 0);
4123               len = INTVAL (XEXP (SET_SRC (x), 1));
4124               pos = INTVAL (XEXP (SET_SRC (x), 2));
4125
4126               if (BITS_BIG_ENDIAN)
4127                 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
4128               unsignedp = (code == ZERO_EXTRACT);
4129             }
4130           break;
4131
4132         default:
4133           break;
4134         }
4135
4136       if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
4137         {
4138           enum machine_mode mode = GET_MODE (SET_SRC (x));
4139
4140           /* For unsigned, we have a choice of a shift followed by an
4141              AND or two shifts.  Use two shifts for field sizes where the
4142              constant might be too large.  We assume here that we can
4143              always at least get 8-bit constants in an AND insn, which is
4144              true for every current RISC.  */
4145
4146           if (unsignedp && len <= 8)
4147             {
4148               SUBST (SET_SRC (x),
4149                      gen_rtx_AND (mode,
4150                                   gen_rtx_LSHIFTRT
4151                                   (mode, gen_lowpart (mode, inner),
4152                                    GEN_INT (pos)),
4153                                   GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
4154
4155               split = find_split_point (&SET_SRC (x), insn);
4156               if (split && split != &SET_SRC (x))
4157                 return split;
4158             }
4159           else
4160             {
4161               SUBST (SET_SRC (x),
4162                      gen_rtx_fmt_ee
4163                      (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4164                       gen_rtx_ASHIFT (mode,
4165                                       gen_lowpart (mode, inner),
4166                                       GEN_INT (GET_MODE_BITSIZE (mode)
4167                                                - len - pos)),
4168                       GEN_INT (GET_MODE_BITSIZE (mode) - len)));
4169
4170               split = find_split_point (&SET_SRC (x), insn);
4171               if (split && split != &SET_SRC (x))
4172                 return split;
4173             }
4174         }
4175
4176       /* See if this is a simple operation with a constant as the second
4177          operand.  It might be that this constant is out of range and hence
4178          could be used as a split point.  */
4179       if (BINARY_P (SET_SRC (x))
4180           && CONSTANT_P (XEXP (SET_SRC (x), 1))
4181           && (OBJECT_P (XEXP (SET_SRC (x), 0))
4182               || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4183                   && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4184         return &XEXP (SET_SRC (x), 1);
4185
4186       /* Finally, see if this is a simple operation with its first operand
4187          not in a register.  The operation might require this operand in a
4188          register, so return it as a split point.  We can always do this
4189          because if the first operand were another operation, we would have
4190          already found it as a split point.  */
4191       if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4192           && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4193         return &XEXP (SET_SRC (x), 0);
4194
4195       return 0;
4196
4197     case AND:
4198     case IOR:
4199       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4200          it is better to write this as (not (ior A B)) so we can split it.
4201          Similarly for IOR.  */
4202       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4203         {
4204           SUBST (*loc,
4205                  gen_rtx_NOT (GET_MODE (x),
4206                               gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4207                                               GET_MODE (x),
4208                                               XEXP (XEXP (x, 0), 0),
4209                                               XEXP (XEXP (x, 1), 0))));
4210           return find_split_point (loc, insn);
4211         }
4212
4213       /* Many RISC machines have a large set of logical insns.  If the
4214          second operand is a NOT, put it first so we will try to split the
4215          other operand first.  */
4216       if (GET_CODE (XEXP (x, 1)) == NOT)
4217         {
4218           rtx tem = XEXP (x, 0);
4219           SUBST (XEXP (x, 0), XEXP (x, 1));
4220           SUBST (XEXP (x, 1), tem);
4221         }
4222       break;
4223
4224     default:
4225       break;
4226     }
4227
4228   /* Otherwise, select our actions depending on our rtx class.  */
4229   switch (GET_RTX_CLASS (code))
4230     {
4231     case RTX_BITFIELD_OPS:              /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
4232     case RTX_TERNARY:
4233       split = find_split_point (&XEXP (x, 2), insn);
4234       if (split)
4235         return split;
4236       /* ... fall through ...  */
4237     case RTX_BIN_ARITH:
4238     case RTX_COMM_ARITH:
4239     case RTX_COMPARE:
4240     case RTX_COMM_COMPARE:
4241       split = find_split_point (&XEXP (x, 1), insn);
4242       if (split)
4243         return split;
4244       /* ... fall through ...  */
4245     case RTX_UNARY:
4246       /* Some machines have (and (shift ...) ...) insns.  If X is not
4247          an AND, but XEXP (X, 0) is, use it as our split point.  */
4248       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4249         return &XEXP (x, 0);
4250
4251       split = find_split_point (&XEXP (x, 0), insn);
4252       if (split)
4253         return split;
4254       return loc;
4255
4256     default:
4257       /* Otherwise, we don't have a split point.  */
4258       return 0;
4259     }
4260 }
4261 \f
4262 /* Throughout X, replace FROM with TO, and return the result.
4263    The result is TO if X is FROM;
4264    otherwise the result is X, but its contents may have been modified.
4265    If they were modified, a record was made in undobuf so that
4266    undo_all will (among other things) return X to its original state.
4267
4268    If the number of changes necessary is too much to record to undo,
4269    the excess changes are not made, so the result is invalid.
4270    The changes already made can still be undone.
4271    undobuf.num_undo is incremented for such changes, so by testing that
4272    the caller can tell whether the result is valid.
4273
4274    `n_occurrences' is incremented each time FROM is replaced.
4275
4276    IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4277
4278    UNIQUE_COPY is nonzero if each substitution must be unique.  We do this
4279    by copying if `n_occurrences' is nonzero.  */
4280
4281 static rtx
4282 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
4283 {
4284   enum rtx_code code = GET_CODE (x);
4285   enum machine_mode op0_mode = VOIDmode;
4286   const char *fmt;
4287   int len, i;
4288   rtx new;
4289
4290 /* Two expressions are equal if they are identical copies of a shared
4291    RTX or if they are both registers with the same register number
4292    and mode.  */
4293
4294 #define COMBINE_RTX_EQUAL_P(X,Y)                        \
4295   ((X) == (Y)                                           \
4296    || (REG_P (X) && REG_P (Y)   \
4297        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4298
4299   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
4300     {
4301       n_occurrences++;
4302       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
4303     }
4304
4305   /* If X and FROM are the same register but different modes, they
4306      will not have been seen as equal above.  However, the log links code
4307      will make a LOG_LINKS entry for that case.  If we do nothing, we
4308      will try to rerecognize our original insn and, when it succeeds,
4309      we will delete the feeding insn, which is incorrect.
4310
4311      So force this insn not to match in this (rare) case.  */
4312   if (! in_dest && code == REG && REG_P (from)
4313       && reg_overlap_mentioned_p (x, from))
4314     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
4315
4316   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4317      of which may contain things that can be combined.  */
4318   if (code != MEM && code != LO_SUM && OBJECT_P (x))
4319     return x;
4320
4321   /* It is possible to have a subexpression appear twice in the insn.
4322      Suppose that FROM is a register that appears within TO.
4323      Then, after that subexpression has been scanned once by `subst',
4324      the second time it is scanned, TO may be found.  If we were
4325      to scan TO here, we would find FROM within it and create a
4326      self-referent rtl structure which is completely wrong.  */
4327   if (COMBINE_RTX_EQUAL_P (x, to))
4328     return to;
4329
4330   /* Parallel asm_operands need special attention because all of the
4331      inputs are shared across the arms.  Furthermore, unsharing the
4332      rtl results in recognition failures.  Failure to handle this case
4333      specially can result in circular rtl.
4334
4335      Solve this by doing a normal pass across the first entry of the
4336      parallel, and only processing the SET_DESTs of the subsequent
4337      entries.  Ug.  */
4338
4339   if (code == PARALLEL
4340       && GET_CODE (XVECEXP (x, 0, 0)) == SET
4341       && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
4342     {
4343       new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
4344
4345       /* If this substitution failed, this whole thing fails.  */
4346       if (GET_CODE (new) == CLOBBER
4347           && XEXP (new, 0) == const0_rtx)
4348         return new;
4349
4350       SUBST (XVECEXP (x, 0, 0), new);
4351
4352       for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
4353         {
4354           rtx dest = SET_DEST (XVECEXP (x, 0, i));
4355
4356           if (!REG_P (dest)
4357               && GET_CODE (dest) != CC0
4358               && GET_CODE (dest) != PC)
4359             {
4360               new = subst (dest, from, to, 0, unique_copy);
4361
4362               /* If this substitution failed, this whole thing fails.  */
4363               if (GET_CODE (new) == CLOBBER
4364                   && XEXP (new, 0) == const0_rtx)
4365                 return new;
4366
4367               SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
4368             }
4369         }
4370     }
4371   else
4372     {
4373       len = GET_RTX_LENGTH (code);
4374       fmt = GET_RTX_FORMAT (code);
4375
4376       /* We don't need to process a SET_DEST that is a register, CC0,
4377          or PC, so set up to skip this common case.  All other cases
4378          where we want to suppress replacing something inside a
4379          SET_SRC are handled via the IN_DEST operand.  */
4380       if (code == SET
4381           && (REG_P (SET_DEST (x))
4382               || GET_CODE (SET_DEST (x)) == CC0
4383               || GET_CODE (SET_DEST (x)) == PC))
4384         fmt = "ie";
4385
4386       /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
4387          constant.  */
4388       if (fmt[0] == 'e')
4389         op0_mode = GET_MODE (XEXP (x, 0));
4390
4391       for (i = 0; i < len; i++)
4392         {
4393           if (fmt[i] == 'E')
4394             {
4395               int j;
4396               for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4397                 {
4398                   if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
4399                     {
4400                       new = (unique_copy && n_occurrences
4401                              ? copy_rtx (to) : to);
4402                       n_occurrences++;
4403                     }
4404                   else
4405                     {
4406                       new = subst (XVECEXP (x, i, j), from, to, 0,
4407                                    unique_copy);
4408
4409                       /* If this substitution failed, this whole thing
4410                          fails.  */
4411                       if (GET_CODE (new) == CLOBBER
4412                           && XEXP (new, 0) == const0_rtx)
4413                         return new;
4414                     }
4415
4416                   SUBST (XVECEXP (x, i, j), new);
4417                 }
4418             }
4419           else if (fmt[i] == 'e')
4420             {
4421               /* If this is a register being set, ignore it.  */
4422               new = XEXP (x, i);
4423               if (in_dest
4424                   && i == 0
4425                   && (((code == SUBREG || code == ZERO_EXTRACT)
4426                        && REG_P (new))
4427                       || code == STRICT_LOW_PART))
4428                 ;
4429
4430               else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
4431                 {
4432                   /* In general, don't install a subreg involving two
4433                      modes not tieable.  It can worsen register
4434                      allocation, and can even make invalid reload
4435                      insns, since the reg inside may need to be copied
4436                      from in the outside mode, and that may be invalid
4437                      if it is an fp reg copied in integer mode.
4438
4439                      We allow two exceptions to this: It is valid if
4440                      it is inside another SUBREG and the mode of that
4441                      SUBREG and the mode of the inside of TO is
4442                      tieable and it is valid if X is a SET that copies
4443                      FROM to CC0.  */
4444
4445                   if (GET_CODE (to) == SUBREG
4446                       && ! MODES_TIEABLE_P (GET_MODE (to),
4447                                             GET_MODE (SUBREG_REG (to)))
4448                       && ! (code == SUBREG
4449                             && MODES_TIEABLE_P (GET_MODE (x),
4450                                                 GET_MODE (SUBREG_REG (to))))
4451 #ifdef HAVE_cc0
4452                       && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
4453 #endif
4454                       )
4455                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
4456
4457 #ifdef CANNOT_CHANGE_MODE_CLASS
4458                   if (code == SUBREG
4459                       && REG_P (to)
4460                       && REGNO (to) < FIRST_PSEUDO_REGISTER
4461                       && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
4462                                                    GET_MODE (to),
4463                                                    GET_MODE (x)))
4464                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
4465 #endif
4466
4467                   new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
4468                   n_occurrences++;
4469                 }
4470               else
4471                 /* If we are in a SET_DEST, suppress most cases unless we
4472                    have gone inside a MEM, in which case we want to
4473                    simplify the address.  We assume here that things that
4474                    are actually part of the destination have their inner
4475                    parts in the first expression.  This is true for SUBREG,
4476                    STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
4477                    things aside from REG and MEM that should appear in a
4478                    SET_DEST.  */
4479                 new = subst (XEXP (x, i), from, to,
4480                              (((in_dest
4481                                 && (code == SUBREG || code == STRICT_LOW_PART
4482                                     || code == ZERO_EXTRACT))
4483                                || code == SET)
4484                               && i == 0), unique_copy);
4485
4486               /* If we found that we will have to reject this combination,
4487                  indicate that by returning the CLOBBER ourselves, rather than
4488                  an expression containing it.  This will speed things up as
4489                  well as prevent accidents where two CLOBBERs are considered
4490                  to be equal, thus producing an incorrect simplification.  */
4491
4492               if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
4493                 return new;
4494
4495               if (GET_CODE (x) == SUBREG
4496                   && (GET_CODE (new) == CONST_INT
4497                       || GET_CODE (new) == CONST_DOUBLE))
4498                 {
4499                   enum machine_mode mode = GET_MODE (x);
4500
4501                   x = simplify_subreg (GET_MODE (x), new,
4502                                        GET_MODE (SUBREG_REG (x)),
4503                                        SUBREG_BYTE (x));
4504                   if (! x)
4505                     x = gen_rtx_CLOBBER (mode, const0_rtx);
4506                 }
4507               else if (GET_CODE (new) == CONST_INT
4508                        && GET_CODE (x) == ZERO_EXTEND)
4509                 {
4510                   x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
4511                                                 new, GET_MODE (XEXP (x, 0)));
4512                   gcc_assert (x);
4513                 }
4514               else
4515                 SUBST (XEXP (x, i), new);
4516             }
4517         }
4518     }
4519
4520   /* Check if we are loading something from the constant pool via float
4521      extension; in this case we would undo compress_float_constant
4522      optimization and degenerate constant load to an immediate value.  */
4523   if (GET_CODE (x) == FLOAT_EXTEND
4524       && MEM_P (XEXP (x, 0))
4525       && MEM_READONLY_P (XEXP (x, 0)))
4526     {
4527       rtx tmp = avoid_constant_pool_reference (x);
4528       if (x != tmp)
4529         return x;
4530     }
4531
4532   /* Try to simplify X.  If the simplification changed the code, it is likely
4533      that further simplification will help, so loop, but limit the number
4534      of repetitions that will be performed.  */
4535
4536   for (i = 0; i < 4; i++)
4537     {
4538       /* If X is sufficiently simple, don't bother trying to do anything
4539          with it.  */
4540       if (code != CONST_INT && code != REG && code != CLOBBER)
4541         x = combine_simplify_rtx (x, op0_mode, in_dest);
4542
4543       if (GET_CODE (x) == code)
4544         break;
4545
4546       code = GET_CODE (x);
4547
4548       /* We no longer know the original mode of operand 0 since we
4549          have changed the form of X)  */
4550       op0_mode = VOIDmode;
4551     }
4552
4553   return x;
4554 }
4555 \f
4556 /* Simplify X, a piece of RTL.  We just operate on the expression at the
4557    outer level; call `subst' to simplify recursively.  Return the new
4558    expression.
4559
4560    OP0_MODE is the original mode of XEXP (x, 0).  IN_DEST is nonzero
4561    if we are inside a SET_DEST.  */
4562
4563 static rtx
4564 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
4565 {
4566   enum rtx_code code = GET_CODE (x);
4567   enum machine_mode mode = GET_MODE (x);
4568   rtx temp;
4569   int i;
4570
4571   /* If this is a commutative operation, put a constant last and a complex
4572      expression first.  We don't need to do this for comparisons here.  */
4573   if (COMMUTATIVE_ARITH_P (x)
4574       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
4575     {
4576       temp = XEXP (x, 0);
4577       SUBST (XEXP (x, 0), XEXP (x, 1));
4578       SUBST (XEXP (x, 1), temp);
4579     }
4580
4581   /* If this is a simple operation applied to an IF_THEN_ELSE, try
4582      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
4583      things.  Check for cases where both arms are testing the same
4584      condition.
4585
4586      Don't do anything if all operands are very simple.  */
4587
4588   if ((BINARY_P (x)
4589        && ((!OBJECT_P (XEXP (x, 0))
4590             && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4591                   && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
4592            || (!OBJECT_P (XEXP (x, 1))
4593                && ! (GET_CODE (XEXP (x, 1)) == SUBREG
4594                      && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
4595       || (UNARY_P (x)
4596           && (!OBJECT_P (XEXP (x, 0))
4597                && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4598                      && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
4599     {
4600       rtx cond, true_rtx, false_rtx;
4601
4602       cond = if_then_else_cond (x, &true_rtx, &false_rtx);
4603       if (cond != 0
4604           /* If everything is a comparison, what we have is highly unlikely
4605              to be simpler, so don't use it.  */
4606           && ! (COMPARISON_P (x)
4607                 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
4608         {
4609           rtx cop1 = const0_rtx;
4610           enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
4611
4612           if (cond_code == NE && COMPARISON_P (cond))
4613             return x;
4614
4615           /* Simplify the alternative arms; this may collapse the true and
4616              false arms to store-flag values.  Be careful to use copy_rtx
4617              here since true_rtx or false_rtx might share RTL with x as a
4618              result of the if_then_else_cond call above.  */
4619           true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
4620           false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
4621
4622           /* If true_rtx and false_rtx are not general_operands, an if_then_else
4623              is unlikely to be simpler.  */
4624           if (general_operand (true_rtx, VOIDmode)
4625               && general_operand (false_rtx, VOIDmode))
4626             {
4627               enum rtx_code reversed;
4628
4629               /* Restarting if we generate a store-flag expression will cause
4630                  us to loop.  Just drop through in this case.  */
4631
4632               /* If the result values are STORE_FLAG_VALUE and zero, we can
4633                  just make the comparison operation.  */
4634               if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
4635                 x = simplify_gen_relational (cond_code, mode, VOIDmode,
4636                                              cond, cop1);
4637               else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
4638                        && ((reversed = reversed_comparison_code_parts
4639                                         (cond_code, cond, cop1, NULL))
4640                            != UNKNOWN))
4641                 x = simplify_gen_relational (reversed, mode, VOIDmode,
4642                                              cond, cop1);
4643
4644               /* Likewise, we can make the negate of a comparison operation
4645                  if the result values are - STORE_FLAG_VALUE and zero.  */
4646               else if (GET_CODE (true_rtx) == CONST_INT
4647                        && INTVAL (true_rtx) == - STORE_FLAG_VALUE
4648                        && false_rtx == const0_rtx)
4649                 x = simplify_gen_unary (NEG, mode,
4650                                         simplify_gen_relational (cond_code,
4651                                                                  mode, VOIDmode,
4652                                                                  cond, cop1),
4653                                         mode);
4654               else if (GET_CODE (false_rtx) == CONST_INT
4655                        && INTVAL (false_rtx) == - STORE_FLAG_VALUE
4656                        && true_rtx == const0_rtx
4657                        && ((reversed = reversed_comparison_code_parts
4658                                         (cond_code, cond, cop1, NULL))
4659                            != UNKNOWN))
4660                 x = simplify_gen_unary (NEG, mode,
4661                                         simplify_gen_relational (reversed,
4662                                                                  mode, VOIDmode,
4663                                                                  cond, cop1),
4664                                         mode);
4665               else
4666                 return gen_rtx_IF_THEN_ELSE (mode,
4667                                              simplify_gen_relational (cond_code,
4668                                                                       mode,
4669                                                                       VOIDmode,
4670                                                                       cond,
4671                                                                       cop1),
4672                                              true_rtx, false_rtx);
4673
4674               code = GET_CODE (x);
4675               op0_mode = VOIDmode;
4676             }
4677         }
4678     }
4679
4680   /* Try to fold this expression in case we have constants that weren't
4681      present before.  */
4682   temp = 0;
4683   switch (GET_RTX_CLASS (code))
4684     {
4685     case RTX_UNARY:
4686       if (op0_mode == VOIDmode)
4687         op0_mode = GET_MODE (XEXP (x, 0));
4688       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
4689       break;
4690     case RTX_COMPARE:
4691     case RTX_COMM_COMPARE:
4692       {
4693         enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
4694         if (cmp_mode == VOIDmode)
4695           {
4696             cmp_mode = GET_MODE (XEXP (x, 1));
4697             if (cmp_mode == VOIDmode)
4698               cmp_mode = op0_mode;
4699           }
4700         temp = simplify_relational_operation (code, mode, cmp_mode,
4701                                               XEXP (x, 0), XEXP (x, 1));
4702       }
4703       break;
4704     case RTX_COMM_ARITH:
4705     case RTX_BIN_ARITH:
4706       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
4707       break;
4708     case RTX_BITFIELD_OPS:
4709     case RTX_TERNARY:
4710       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
4711                                          XEXP (x, 1), XEXP (x, 2));
4712       break;
4713     default:
4714       break;
4715     }
4716
4717   if (temp)
4718     {
4719       x = temp;
4720       code = GET_CODE (temp);
4721       op0_mode = VOIDmode;
4722       mode = GET_MODE (temp);
4723     }
4724
4725   /* First see if we can apply the inverse distributive law.  */
4726   if (code == PLUS || code == MINUS
4727       || code == AND || code == IOR || code == XOR)
4728     {
4729       x = apply_distributive_law (x);
4730       code = GET_CODE (x);
4731       op0_mode = VOIDmode;
4732     }
4733
4734   /* If CODE is an associative operation not otherwise handled, see if we
4735      can associate some operands.  This can win if they are constants or
4736      if they are logically related (i.e. (a & b) & a).  */
4737   if ((code == PLUS || code == MINUS || code == MULT || code == DIV
4738        || code == AND || code == IOR || code == XOR
4739        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
4740       && ((INTEGRAL_MODE_P (mode) && code != DIV)
4741           || (flag_associative_math && FLOAT_MODE_P (mode))))
4742     {
4743       if (GET_CODE (XEXP (x, 0)) == code)
4744         {
4745           rtx other = XEXP (XEXP (x, 0), 0);
4746           rtx inner_op0 = XEXP (XEXP (x, 0), 1);
4747           rtx inner_op1 = XEXP (x, 1);
4748           rtx inner;
4749
4750           /* Make sure we pass the constant operand if any as the second
4751              one if this is a commutative operation.  */
4752           if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
4753             {
4754               rtx tem = inner_op0;
4755               inner_op0 = inner_op1;
4756               inner_op1 = tem;
4757             }
4758           inner = simplify_binary_operation (code == MINUS ? PLUS
4759                                              : code == DIV ? MULT
4760                                              : code,
4761                                              mode, inner_op0, inner_op1);
4762
4763           /* For commutative operations, try the other pair if that one
4764              didn't simplify.  */
4765           if (inner == 0 && COMMUTATIVE_ARITH_P (x))
4766             {
4767               other = XEXP (XEXP (x, 0), 1);
4768               inner = simplify_binary_operation (code, mode,
4769                                                  XEXP (XEXP (x, 0), 0),
4770                                                  XEXP (x, 1));
4771             }
4772
4773           if (inner)
4774             return simplify_gen_binary (code, mode, other, inner);
4775         }
4776     }
4777
4778   /* A little bit of algebraic simplification here.  */
4779   switch (code)
4780     {
4781     case MEM:
4782       /* Ensure that our address has any ASHIFTs converted to MULT in case
4783          address-recognizing predicates are called later.  */
4784       temp = make_compound_operation (XEXP (x, 0), MEM);
4785       SUBST (XEXP (x, 0), temp);
4786       break;
4787
4788     case SUBREG:
4789       if (op0_mode == VOIDmode)
4790         op0_mode = GET_MODE (SUBREG_REG (x));
4791
4792       /* See if this can be moved to simplify_subreg.  */
4793       if (CONSTANT_P (SUBREG_REG (x))
4794           && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
4795              /* Don't call gen_lowpart if the inner mode
4796                 is VOIDmode and we cannot simplify it, as SUBREG without
4797                 inner mode is invalid.  */
4798           && (GET_MODE (SUBREG_REG (x)) != VOIDmode
4799               || gen_lowpart_common (mode, SUBREG_REG (x))))
4800         return gen_lowpart (mode, SUBREG_REG (x));
4801
4802       if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
4803         break;
4804       {
4805         rtx temp;
4806         temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
4807                                 SUBREG_BYTE (x));
4808         if (temp)
4809           return temp;
4810       }
4811
4812       /* Don't change the mode of the MEM if that would change the meaning
4813          of the address.  */
4814       if (MEM_P (SUBREG_REG (x))
4815           && (MEM_VOLATILE_P (SUBREG_REG (x))
4816               || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
4817         return gen_rtx_CLOBBER (mode, const0_rtx);
4818
4819       /* Note that we cannot do any narrowing for non-constants since
4820          we might have been counting on using the fact that some bits were
4821          zero.  We now do this in the SET.  */
4822
4823       break;
4824
4825     case NEG:
4826       temp = expand_compound_operation (XEXP (x, 0));
4827
4828       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
4829          replaced by (lshiftrt X C).  This will convert
4830          (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
4831
4832       if (GET_CODE (temp) == ASHIFTRT
4833           && GET_CODE (XEXP (temp, 1)) == CONST_INT
4834           && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
4835         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
4836                                      INTVAL (XEXP (temp, 1)));
4837
4838       /* If X has only a single bit that might be nonzero, say, bit I, convert
4839          (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
4840          MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
4841          (sign_extract X 1 Y).  But only do this if TEMP isn't a register
4842          or a SUBREG of one since we'd be making the expression more
4843          complex if it was just a register.  */
4844
4845       if (!REG_P (temp)
4846           && ! (GET_CODE (temp) == SUBREG
4847                 && REG_P (SUBREG_REG (temp)))
4848           && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
4849         {
4850           rtx temp1 = simplify_shift_const
4851             (NULL_RTX, ASHIFTRT, mode,
4852              simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
4853                                    GET_MODE_BITSIZE (mode) - 1 - i),
4854              GET_MODE_BITSIZE (mode) - 1 - i);
4855
4856           /* If all we did was surround TEMP with the two shifts, we
4857              haven't improved anything, so don't use it.  Otherwise,
4858              we are better off with TEMP1.  */
4859           if (GET_CODE (temp1) != ASHIFTRT
4860               || GET_CODE (XEXP (temp1, 0)) != ASHIFT
4861               || XEXP (XEXP (temp1, 0), 0) != temp)
4862             return temp1;
4863         }
4864       break;
4865
4866     case TRUNCATE:
4867       /* We can't handle truncation to a partial integer mode here
4868          because we don't know the real bitsize of the partial
4869          integer mode.  */
4870       if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
4871         break;
4872
4873       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4874           && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4875                                     GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
4876         SUBST (XEXP (x, 0),
4877                force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
4878                               GET_MODE_MASK (mode), 0));
4879
4880       /* Similarly to what we do in simplify-rtx.c, a truncate of a register
4881          whose value is a comparison can be replaced with a subreg if
4882          STORE_FLAG_VALUE permits.  */
4883       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4884           && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4885           && (temp = get_last_value (XEXP (x, 0)))
4886           && COMPARISON_P (temp))
4887         return gen_lowpart (mode, XEXP (x, 0));
4888       break;
4889
4890 #ifdef HAVE_cc0
4891     case COMPARE:
4892       /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4893          using cc0, in which case we want to leave it as a COMPARE
4894          so we can distinguish it from a register-register-copy.  */
4895       if (XEXP (x, 1) == const0_rtx)
4896         return XEXP (x, 0);
4897
4898       /* x - 0 is the same as x unless x's mode has signed zeros and
4899          allows rounding towards -infinity.  Under those conditions,
4900          0 - 0 is -0.  */
4901       if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4902             && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4903           && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4904         return XEXP (x, 0);
4905       break;
4906 #endif
4907
4908     case CONST:
4909       /* (const (const X)) can become (const X).  Do it this way rather than
4910          returning the inner CONST since CONST can be shared with a
4911          REG_EQUAL note.  */
4912       if (GET_CODE (XEXP (x, 0)) == CONST)
4913         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4914       break;
4915
4916 #ifdef HAVE_lo_sum
4917     case LO_SUM:
4918       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
4919          can add in an offset.  find_split_point will split this address up
4920          again if it doesn't match.  */
4921       if (GET_CODE (XEXP (x, 0)) == HIGH
4922           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4923         return XEXP (x, 1);
4924       break;
4925 #endif
4926
4927     case PLUS:
4928       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4929          when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4930          bit-field and can be replaced by either a sign_extend or a
4931          sign_extract.  The `and' may be a zero_extend and the two
4932          <c>, -<c> constants may be reversed.  */
4933       if (GET_CODE (XEXP (x, 0)) == XOR
4934           && GET_CODE (XEXP (x, 1)) == CONST_INT
4935           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4936           && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4937           && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4938               || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4939           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4940           && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4941                && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4942                && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4943                    == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4944               || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4945                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4946                       == (unsigned int) i + 1))))
4947         return simplify_shift_const
4948           (NULL_RTX, ASHIFTRT, mode,
4949            simplify_shift_const (NULL_RTX, ASHIFT, mode,
4950                                  XEXP (XEXP (XEXP (x, 0), 0), 0),
4951                                  GET_MODE_BITSIZE (mode) - (i + 1)),
4952            GET_MODE_BITSIZE (mode) - (i + 1));
4953
4954       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4955          can become (ashiftrt (ashift (xor x 1) C) C) where C is
4956          the bitsize of the mode - 1.  This allows simplification of
4957          "a = (b & 8) == 0;"  */
4958       if (XEXP (x, 1) == constm1_rtx
4959           && !REG_P (XEXP (x, 0))
4960           && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4961                 && REG_P (SUBREG_REG (XEXP (x, 0))))
4962           && nonzero_bits (XEXP (x, 0), mode) == 1)
4963         return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4964            simplify_shift_const (NULL_RTX, ASHIFT, mode,
4965                                  gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4966                                  GET_MODE_BITSIZE (mode) - 1),
4967            GET_MODE_BITSIZE (mode) - 1);
4968
4969       /* If we are adding two things that have no bits in common, convert
4970          the addition into an IOR.  This will often be further simplified,
4971          for example in cases like ((a & 1) + (a & 2)), which can
4972          become a & 3.  */
4973
4974       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4975           && (nonzero_bits (XEXP (x, 0), mode)
4976               & nonzero_bits (XEXP (x, 1), mode)) == 0)
4977         {
4978           /* Try to simplify the expression further.  */
4979           rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4980           temp = combine_simplify_rtx (tor, mode, in_dest);
4981
4982           /* If we could, great.  If not, do not go ahead with the IOR
4983              replacement, since PLUS appears in many special purpose
4984              address arithmetic instructions.  */
4985           if (GET_CODE (temp) != CLOBBER && temp != tor)
4986             return temp;
4987         }
4988       break;
4989
4990     case MINUS:
4991       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4992          (and <foo> (const_int pow2-1))  */
4993       if (GET_CODE (XEXP (x, 1)) == AND
4994           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4995           && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4996           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4997         return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4998                                        -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4999       break;
5000
5001     case MULT:
5002       /* If we have (mult (plus A B) C), apply the distributive law and then
5003          the inverse distributive law to see if things simplify.  This
5004          occurs mostly in addresses, often when unrolling loops.  */
5005
5006       if (GET_CODE (XEXP (x, 0)) == PLUS)
5007         {
5008           rtx result = distribute_and_simplify_rtx (x, 0);
5009           if (result)
5010             return result;
5011         }
5012
5013       /* Try simplify a*(b/c) as (a*b)/c.  */
5014       if (FLOAT_MODE_P (mode) && flag_associative_math 
5015           && GET_CODE (XEXP (x, 0)) == DIV)
5016         {
5017           rtx tem = simplify_binary_operation (MULT, mode,
5018                                                XEXP (XEXP (x, 0), 0),
5019                                                XEXP (x, 1));
5020           if (tem)
5021             return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5022         }
5023       break;
5024
5025     case UDIV:
5026       /* If this is a divide by a power of two, treat it as a shift if
5027          its first operand is a shift.  */
5028       if (GET_CODE (XEXP (x, 1)) == CONST_INT
5029           && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
5030           && (GET_CODE (XEXP (x, 0)) == ASHIFT
5031               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5032               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5033               || GET_CODE (XEXP (x, 0)) == ROTATE
5034               || GET_CODE (XEXP (x, 0)) == ROTATERT))
5035         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5036       break;
5037
5038     case EQ:  case NE:
5039     case GT:  case GTU:  case GE:  case GEU:
5040     case LT:  case LTU:  case LE:  case LEU:
5041     case UNEQ:  case LTGT:
5042     case UNGT:  case UNGE:
5043     case UNLT:  case UNLE:
5044     case UNORDERED: case ORDERED:
5045       /* If the first operand is a condition code, we can't do anything
5046          with it.  */
5047       if (GET_CODE (XEXP (x, 0)) == COMPARE
5048           || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5049               && ! CC0_P (XEXP (x, 0))))
5050         {
5051           rtx op0 = XEXP (x, 0);
5052           rtx op1 = XEXP (x, 1);
5053           enum rtx_code new_code;
5054
5055           if (GET_CODE (op0) == COMPARE)
5056             op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5057
5058           /* Simplify our comparison, if possible.  */
5059           new_code = simplify_comparison (code, &op0, &op1);
5060
5061           /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5062              if only the low-order bit is possibly nonzero in X (such as when
5063              X is a ZERO_EXTRACT of one bit).  Similarly, we can convert EQ to
5064              (xor X 1) or (minus 1 X); we use the former.  Finally, if X is
5065              known to be either 0 or -1, NE becomes a NEG and EQ becomes
5066              (plus X 1).
5067
5068              Remove any ZERO_EXTRACT we made when thinking this was a
5069              comparison.  It may now be simpler to use, e.g., an AND.  If a
5070              ZERO_EXTRACT is indeed appropriate, it will be placed back by
5071              the call to make_compound_operation in the SET case.  */
5072
5073           if (STORE_FLAG_VALUE == 1
5074               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5075               && op1 == const0_rtx
5076               && mode == GET_MODE (op0)
5077               && nonzero_bits (op0, mode) == 1)
5078             return gen_lowpart (mode,
5079                                 expand_compound_operation (op0));
5080
5081           else if (STORE_FLAG_VALUE == 1
5082                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5083                    && op1 == const0_rtx
5084                    && mode == GET_MODE (op0)
5085                    && (num_sign_bit_copies (op0, mode)
5086                        == GET_MODE_BITSIZE (mode)))
5087             {
5088               op0 = expand_compound_operation (op0);
5089               return simplify_gen_unary (NEG, mode,
5090                                          gen_lowpart (mode, op0),
5091                                          mode);
5092             }
5093
5094           else if (STORE_FLAG_VALUE == 1
5095                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5096                    && op1 == const0_rtx
5097                    && mode == GET_MODE (op0)
5098                    && nonzero_bits (op0, mode) == 1)
5099             {
5100               op0 = expand_compound_operation (op0);
5101               return simplify_gen_binary (XOR, mode,
5102                                           gen_lowpart (mode, op0),
5103                                           const1_rtx);
5104             }
5105
5106           else if (STORE_FLAG_VALUE == 1
5107                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5108                    && op1 == const0_rtx
5109                    && mode == GET_MODE (op0)
5110                    && (num_sign_bit_copies (op0, mode)
5111                        == GET_MODE_BITSIZE (mode)))
5112             {
5113               op0 = expand_compound_operation (op0);
5114               return plus_constant (gen_lowpart (mode, op0), 1);
5115             }
5116
5117           /* If STORE_FLAG_VALUE is -1, we have cases similar to
5118              those above.  */
5119           if (STORE_FLAG_VALUE == -1
5120               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5121               && op1 == const0_rtx
5122               && (num_sign_bit_copies (op0, mode)
5123                   == GET_MODE_BITSIZE (mode)))
5124             return gen_lowpart (mode,
5125                                 expand_compound_operation (op0));
5126
5127           else if (STORE_FLAG_VALUE == -1
5128                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5129                    && op1 == const0_rtx
5130                    && mode == GET_MODE (op0)
5131                    && nonzero_bits (op0, mode) == 1)
5132             {
5133               op0 = expand_compound_operation (op0);
5134               return simplify_gen_unary (NEG, mode,
5135                                          gen_lowpart (mode, op0),
5136                                          mode);
5137             }
5138
5139           else if (STORE_FLAG_VALUE == -1
5140                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5141                    && op1 == const0_rtx
5142                    && mode == GET_MODE (op0)
5143                    && (num_sign_bit_copies (op0, mode)
5144                        == GET_MODE_BITSIZE (mode)))
5145             {
5146               op0 = expand_compound_operation (op0);
5147               return simplify_gen_unary (NOT, mode,
5148                                          gen_lowpart (mode, op0),
5149                                          mode);
5150             }
5151
5152           /* If X is 0/1, (eq X 0) is X-1.  */
5153           else if (STORE_FLAG_VALUE == -1
5154                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5155                    && op1 == const0_rtx
5156                    && mode == GET_MODE (op0)
5157                    && nonzero_bits (op0, mode) == 1)
5158             {
5159               op0 = expand_compound_operation (op0);
5160               return plus_constant (gen_lowpart (mode, op0), -1);
5161             }
5162
5163           /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5164              one bit that might be nonzero, we can convert (ne x 0) to
5165              (ashift x c) where C puts the bit in the sign bit.  Remove any
5166              AND with STORE_FLAG_VALUE when we are done, since we are only
5167              going to test the sign bit.  */
5168           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5169               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5170               && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5171                   == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5172               && op1 == const0_rtx
5173               && mode == GET_MODE (op0)
5174               && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5175             {
5176               x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5177                                         expand_compound_operation (op0),
5178                                         GET_MODE_BITSIZE (mode) - 1 - i);
5179               if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5180                 return XEXP (x, 0);
5181               else
5182                 return x;
5183             }
5184
5185           /* If the code changed, return a whole new comparison.  */
5186           if (new_code != code)
5187             return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5188
5189           /* Otherwise, keep this operation, but maybe change its operands.
5190              This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
5191           SUBST (XEXP (x, 0), op0);
5192           SUBST (XEXP (x, 1), op1);
5193         }
5194       break;
5195
5196     case IF_THEN_ELSE:
5197       return simplify_if_then_else (x);
5198
5199     case ZERO_EXTRACT:
5200     case SIGN_EXTRACT:
5201     case ZERO_EXTEND:
5202     case SIGN_EXTEND:
5203       /* If we are processing SET_DEST, we are done.  */
5204       if (in_dest)
5205         return x;
5206
5207       return expand_compound_operation (x);
5208
5209     case SET:
5210       return simplify_set (x);
5211
5212     case AND:
5213     case IOR:
5214       return simplify_logical (x);
5215
5216     case ASHIFT:
5217     case LSHIFTRT:
5218     case ASHIFTRT:
5219     case ROTATE:
5220     case ROTATERT:
5221       /* If this is a shift by a constant amount, simplify it.  */
5222       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5223         return simplify_shift_const (x, code, mode, XEXP (x, 0),
5224                                      INTVAL (XEXP (x, 1)));
5225
5226       else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5227         SUBST (XEXP (x, 1),
5228                force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5229                               ((HOST_WIDE_INT) 1
5230                                << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5231                               - 1,
5232                               0));
5233       break;
5234
5235     default:
5236       break;
5237     }
5238
5239   return x;
5240 }
5241 \f
5242 /* Simplify X, an IF_THEN_ELSE expression.  Return the new expression.  */
5243
5244 static rtx
5245 simplify_if_then_else (rtx x)
5246 {
5247   enum machine_mode mode = GET_MODE (x);
5248   rtx cond = XEXP (x, 0);
5249   rtx true_rtx = XEXP (x, 1);
5250   rtx false_rtx = XEXP (x, 2);
5251   enum rtx_code true_code = GET_CODE (cond);
5252   int comparison_p = COMPARISON_P (cond);
5253   rtx temp;
5254   int i;
5255   enum rtx_code false_code;
5256   rtx reversed;
5257
5258   /* Simplify storing of the truth value.  */
5259   if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5260     return simplify_gen_relational (true_code, mode, VOIDmode,
5261                                     XEXP (cond, 0), XEXP (cond, 1));
5262
5263   /* Also when the truth value has to be reversed.  */
5264   if (comparison_p
5265       && true_rtx == const0_rtx && false_rtx == const_true_rtx
5266       && (reversed = reversed_comparison (cond, mode)))
5267     return reversed;
5268
5269   /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5270      in it is being compared against certain values.  Get the true and false
5271      comparisons and see if that says anything about the value of each arm.  */
5272
5273   if (comparison_p
5274       && ((false_code = reversed_comparison_code (cond, NULL))
5275           != UNKNOWN)
5276       && REG_P (XEXP (cond, 0)))
5277     {
5278       HOST_WIDE_INT nzb;
5279       rtx from = XEXP (cond, 0);
5280       rtx true_val = XEXP (cond, 1);
5281       rtx false_val = true_val;
5282       int swapped = 0;
5283
5284       /* If FALSE_CODE is EQ, swap the codes and arms.  */
5285
5286       if (false_code == EQ)
5287         {
5288           swapped = 1, true_code = EQ, false_code = NE;
5289           temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5290         }
5291
5292       /* If we are comparing against zero and the expression being tested has
5293          only a single bit that might be nonzero, that is its value when it is
5294          not equal to zero.  Similarly if it is known to be -1 or 0.  */
5295
5296       if (true_code == EQ && true_val == const0_rtx
5297           && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5298         {
5299           false_code = EQ;
5300           false_val = GEN_INT (trunc_int_for_mode (nzb, GET_MODE (from)));
5301         }
5302       else if (true_code == EQ && true_val == const0_rtx
5303                && (num_sign_bit_copies (from, GET_MODE (from))
5304                    == GET_MODE_BITSIZE (GET_MODE (from))))
5305         {
5306           false_code = EQ;
5307           false_val = constm1_rtx;
5308         }
5309
5310       /* Now simplify an arm if we know the value of the register in the
5311          branch and it is used in the arm.  Be careful due to the potential
5312          of locally-shared RTL.  */
5313
5314       if (reg_mentioned_p (from, true_rtx))
5315         true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
5316                                       from, true_val),
5317                       pc_rtx, pc_rtx, 0, 0);
5318       if (reg_mentioned_p (from, false_rtx))
5319         false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
5320                                    from, false_val),
5321                        pc_rtx, pc_rtx, 0, 0);
5322
5323       SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
5324       SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
5325
5326       true_rtx = XEXP (x, 1);
5327       false_rtx = XEXP (x, 2);
5328       true_code = GET_CODE (cond);
5329     }
5330
5331   /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
5332      reversed, do so to avoid needing two sets of patterns for
5333      subtract-and-branch insns.  Similarly if we have a constant in the true
5334      arm, the false arm is the same as the first operand of the comparison, or
5335      the false arm is more complicated than the true arm.  */
5336
5337   if (comparison_p
5338       && reversed_comparison_code (cond, NULL) != UNKNOWN
5339       && (true_rtx == pc_rtx
5340           || (CONSTANT_P (true_rtx)
5341               && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
5342           || true_rtx == const0_rtx
5343           || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
5344           || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
5345               && !OBJECT_P (false_rtx))
5346           || reg_mentioned_p (true_rtx, false_rtx)
5347           || rtx_equal_p (false_rtx, XEXP (cond, 0))))
5348     {
5349       true_code = reversed_comparison_code (cond, NULL);
5350       SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
5351       SUBST (XEXP (x, 1), false_rtx);
5352       SUBST (XEXP (x, 2), true_rtx);
5353
5354       temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5355       cond = XEXP (x, 0);
5356
5357       /* It is possible that the conditional has been simplified out.  */
5358       true_code = GET_CODE (cond);
5359       comparison_p = COMPARISON_P (cond);
5360     }
5361
5362   /* If the two arms are identical, we don't need the comparison.  */
5363
5364   if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
5365     return true_rtx;
5366
5367   /* Convert a == b ? b : a to "a".  */
5368   if (true_code == EQ && ! side_effects_p (cond)
5369       && !HONOR_NANS (mode)
5370       && rtx_equal_p (XEXP (cond, 0), false_rtx)
5371       && rtx_equal_p (XEXP (cond, 1), true_rtx))
5372     return false_rtx;
5373   else if (true_code == NE && ! side_effects_p (cond)
5374            && !HONOR_NANS (mode)
5375            && rtx_equal_p (XEXP (cond, 0), true_rtx)
5376            && rtx_equal_p (XEXP (cond, 1), false_rtx))
5377     return true_rtx;
5378
5379   /* Look for cases where we have (abs x) or (neg (abs X)).  */
5380
5381   if (GET_MODE_CLASS (mode) == MODE_INT
5382       && GET_CODE (false_rtx) == NEG
5383       && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
5384       && comparison_p
5385       && rtx_equal_p (true_rtx, XEXP (cond, 0))
5386       && ! side_effects_p (true_rtx))
5387     switch (true_code)
5388       {
5389       case GT:
5390       case GE:
5391         return simplify_gen_unary (ABS, mode, true_rtx, mode);
5392       case LT:
5393       case LE:
5394         return
5395           simplify_gen_unary (NEG, mode,
5396                               simplify_gen_unary (ABS, mode, true_rtx, mode),
5397                               mode);
5398       default:
5399         break;
5400       }
5401
5402   /* Look for MIN or MAX.  */
5403
5404   if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
5405       && comparison_p
5406       && rtx_equal_p (XEXP (cond, 0), true_rtx)
5407       && rtx_equal_p (XEXP (cond, 1), false_rtx)
5408       && ! side_effects_p (cond))
5409     switch (true_code)
5410       {
5411       case GE:
5412       case GT:
5413         return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
5414       case LE:
5415       case LT:
5416         return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
5417       case GEU:
5418       case GTU:
5419         return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
5420       case LEU:
5421       case LTU:
5422         return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
5423       default:
5424         break;
5425       }
5426
5427   /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
5428      second operand is zero, this can be done as (OP Z (mult COND C2)) where
5429      C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
5430      SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
5431      We can do this kind of thing in some cases when STORE_FLAG_VALUE is
5432      neither 1 or -1, but it isn't worth checking for.  */
5433
5434   if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5435       && comparison_p
5436       && GET_MODE_CLASS (mode) == MODE_INT
5437       && ! side_effects_p (x))
5438     {
5439       rtx t = make_compound_operation (true_rtx, SET);
5440       rtx f = make_compound_operation (false_rtx, SET);
5441       rtx cond_op0 = XEXP (cond, 0);
5442       rtx cond_op1 = XEXP (cond, 1);
5443       enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
5444       enum machine_mode m = mode;
5445       rtx z = 0, c1 = NULL_RTX;
5446
5447       if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
5448            || GET_CODE (t) == IOR || GET_CODE (t) == XOR
5449            || GET_CODE (t) == ASHIFT
5450            || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
5451           && rtx_equal_p (XEXP (t, 0), f))
5452         c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
5453
5454       /* If an identity-zero op is commutative, check whether there
5455          would be a match if we swapped the operands.  */
5456       else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
5457                 || GET_CODE (t) == XOR)
5458                && rtx_equal_p (XEXP (t, 1), f))
5459         c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
5460       else if (GET_CODE (t) == SIGN_EXTEND
5461                && (GET_CODE (XEXP (t, 0)) == PLUS
5462                    || GET_CODE (XEXP (t, 0)) == MINUS
5463                    || GET_CODE (XEXP (t, 0)) == IOR
5464                    || GET_CODE (XEXP (t, 0)) == XOR
5465                    || GET_CODE (XEXP (t, 0)) == ASHIFT
5466                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5467                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5468                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5469                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5470                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5471                && (num_sign_bit_copies (f, GET_MODE (f))
5472                    > (unsigned int)
5473                      (GET_MODE_BITSIZE (mode)
5474                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
5475         {
5476           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5477           extend_op = SIGN_EXTEND;
5478           m = GET_MODE (XEXP (t, 0));
5479         }
5480       else if (GET_CODE (t) == SIGN_EXTEND
5481                && (GET_CODE (XEXP (t, 0)) == PLUS
5482                    || GET_CODE (XEXP (t, 0)) == IOR
5483                    || GET_CODE (XEXP (t, 0)) == XOR)
5484                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5485                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5486                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5487                && (num_sign_bit_copies (f, GET_MODE (f))
5488                    > (unsigned int)
5489                      (GET_MODE_BITSIZE (mode)
5490                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
5491         {
5492           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5493           extend_op = SIGN_EXTEND;
5494           m = GET_MODE (XEXP (t, 0));
5495         }
5496       else if (GET_CODE (t) == ZERO_EXTEND
5497                && (GET_CODE (XEXP (t, 0)) == PLUS
5498                    || GET_CODE (XEXP (t, 0)) == MINUS
5499                    || GET_CODE (XEXP (t, 0)) == IOR
5500                    || GET_CODE (XEXP (t, 0)) == XOR
5501                    || GET_CODE (XEXP (t, 0)) == ASHIFT
5502                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5503                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5504                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5505                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5506                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5507                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5508                && ((nonzero_bits (f, GET_MODE (f))
5509                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
5510                    == 0))
5511         {
5512           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5513           extend_op = ZERO_EXTEND;
5514           m = GET_MODE (XEXP (t, 0));
5515         }
5516       else if (GET_CODE (t) == ZERO_EXTEND
5517                && (GET_CODE (XEXP (t, 0)) == PLUS
5518                    || GET_CODE (XEXP (t, 0)) == IOR
5519                    || GET_CODE (XEXP (t, 0)) == XOR)
5520                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5521                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5522                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5523                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5524                && ((nonzero_bits (f, GET_MODE (f))
5525                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
5526                    == 0))
5527         {
5528           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5529           extend_op = ZERO_EXTEND;
5530           m = GET_MODE (XEXP (t, 0));
5531         }
5532
5533       if (z)
5534         {
5535           temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
5536                                                  cond_op0, cond_op1),
5537                         pc_rtx, pc_rtx, 0, 0);
5538           temp = simplify_gen_binary (MULT, m, temp,
5539                                       simplify_gen_binary (MULT, m, c1,
5540                                                            const_true_rtx));
5541           temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
5542           temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
5543
5544           if (extend_op != UNKNOWN)
5545             temp = simplify_gen_unary (extend_op, mode, temp, m);
5546
5547           return temp;
5548         }
5549     }
5550
5551   /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
5552      1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
5553      negation of a single bit, we can convert this operation to a shift.  We
5554      can actually do this more generally, but it doesn't seem worth it.  */
5555
5556   if (true_code == NE && XEXP (cond, 1) == const0_rtx
5557       && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5558       && ((1 == nonzero_bits (XEXP (cond, 0), mode)
5559            && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
5560           || ((num_sign_bit_copies (XEXP (cond, 0), mode)
5561                == GET_MODE_BITSIZE (mode))
5562               && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
5563     return
5564       simplify_shift_const (NULL_RTX, ASHIFT, mode,
5565                             gen_lowpart (mode, XEXP (cond, 0)), i);
5566
5567   /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8.  */
5568   if (true_code == NE && XEXP (cond, 1) == const0_rtx
5569       && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5570       && GET_MODE (XEXP (cond, 0)) == mode
5571       && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
5572           == nonzero_bits (XEXP (cond, 0), mode)
5573       && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
5574     return XEXP (cond, 0);
5575
5576   return x;
5577 }
5578 \f
5579 /* Simplify X, a SET expression.  Return the new expression.  */
5580
5581 static rtx
5582 simplify_set (rtx x)
5583 {
5584   rtx src = SET_SRC (x);
5585   rtx dest = SET_DEST (x);
5586   enum machine_mode mode
5587     = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
5588   rtx other_insn;
5589   rtx *cc_use;
5590
5591   /* (set (pc) (return)) gets written as (return).  */
5592   if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
5593     return src;
5594
5595   /* Now that we know for sure which bits of SRC we are using, see if we can
5596      simplify the expression for the object knowing that we only need the
5597      low-order bits.  */
5598
5599   if (GET_MODE_CLASS (mode) == MODE_INT
5600       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5601     {
5602       src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, 0);
5603       SUBST (SET_SRC (x), src);
5604     }
5605
5606   /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5607      the comparison result and try to simplify it unless we already have used
5608      undobuf.other_insn.  */
5609   if ((GET_MODE_CLASS (mode) == MODE_CC
5610        || GET_CODE (src) == COMPARE
5611        || CC0_P (dest))
5612       && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5613       && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5614       && COMPARISON_P (*cc_use)
5615       && rtx_equal_p (XEXP (*cc_use, 0), dest))
5616     {
5617       enum rtx_code old_code = GET_CODE (*cc_use);
5618       enum rtx_code new_code;
5619       rtx op0, op1, tmp;
5620       int other_changed = 0;
5621       enum machine_mode compare_mode = GET_MODE (dest);
5622
5623       if (GET_CODE (src) == COMPARE)
5624         op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5625       else
5626         op0 = src, op1 = CONST0_RTX (GET_MODE (src));
5627
5628       tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
5629                                            op0, op1);
5630       if (!tmp)
5631         new_code = old_code;
5632       else if (!CONSTANT_P (tmp))
5633         {
5634           new_code = GET_CODE (tmp);
5635           op0 = XEXP (tmp, 0);
5636           op1 = XEXP (tmp, 1);
5637         }
5638       else
5639         {
5640           rtx pat = PATTERN (other_insn);
5641           undobuf.other_insn = other_insn;
5642           SUBST (*cc_use, tmp);
5643
5644           /* Attempt to simplify CC user.  */
5645           if (GET_CODE (pat) == SET)
5646             {
5647               rtx new = simplify_rtx (SET_SRC (pat));
5648               if (new != NULL_RTX)
5649                 SUBST (SET_SRC (pat), new);
5650             }
5651
5652           /* Convert X into a no-op move.  */
5653           SUBST (SET_DEST (x), pc_rtx);
5654           SUBST (SET_SRC (x), pc_rtx);
5655           return x;
5656         }
5657
5658       /* Simplify our comparison, if possible.  */
5659       new_code = simplify_comparison (new_code, &op0, &op1);
5660
5661 #ifdef SELECT_CC_MODE
5662       /* If this machine has CC modes other than CCmode, check to see if we
5663          need to use a different CC mode here.  */
5664       if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
5665         compare_mode = GET_MODE (op0);
5666       else
5667         compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5668
5669 #ifndef HAVE_cc0
5670       /* If the mode changed, we have to change SET_DEST, the mode in the
5671          compare, and the mode in the place SET_DEST is used.  If SET_DEST is
5672          a hard register, just build new versions with the proper mode.  If it
5673          is a pseudo, we lose unless it is only time we set the pseudo, in
5674          which case we can safely change its mode.  */
5675       if (compare_mode != GET_MODE (dest))
5676         {
5677           if (can_change_dest_mode (dest, 0, compare_mode))
5678             {
5679               unsigned int regno = REGNO (dest);
5680               rtx new_dest;
5681
5682               if (regno < FIRST_PSEUDO_REGISTER)
5683                 new_dest = gen_rtx_REG (compare_mode, regno);
5684               else
5685                 {
5686                   SUBST_MODE (regno_reg_rtx[regno], compare_mode);
5687                   new_dest = regno_reg_rtx[regno];
5688                 }
5689
5690               SUBST (SET_DEST (x), new_dest);
5691               SUBST (XEXP (*cc_use, 0), new_dest);
5692               other_changed = 1;
5693
5694               dest = new_dest;
5695             }
5696         }
5697 #endif  /* cc0 */
5698 #endif  /* SELECT_CC_MODE */
5699
5700       /* If the code changed, we have to build a new comparison in
5701          undobuf.other_insn.  */
5702       if (new_code != old_code)
5703         {
5704           int other_changed_previously = other_changed;
5705           unsigned HOST_WIDE_INT mask;
5706
5707           SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5708                                           dest, const0_rtx));
5709           other_changed = 1;
5710
5711           /* If the only change we made was to change an EQ into an NE or
5712              vice versa, OP0 has only one bit that might be nonzero, and OP1
5713              is zero, check if changing the user of the condition code will
5714              produce a valid insn.  If it won't, we can keep the original code
5715              in that insn by surrounding our operation with an XOR.  */
5716
5717           if (((old_code == NE && new_code == EQ)
5718                || (old_code == EQ && new_code == NE))
5719               && ! other_changed_previously && op1 == const0_rtx
5720               && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5721               && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5722             {
5723               rtx pat = PATTERN (other_insn), note = 0;
5724
5725               if ((recog_for_combine (&pat, other_insn, &note) < 0
5726                    && ! check_asm_operands (pat)))
5727                 {
5728                   PUT_CODE (*cc_use, old_code);
5729                   other_changed = 0;
5730
5731                   op0 = simplify_gen_binary (XOR, GET_MODE (op0),
5732                                              op0, GEN_INT (mask));
5733                 }
5734             }
5735         }
5736
5737       if (other_changed)
5738         undobuf.other_insn = other_insn;
5739
5740 #ifdef HAVE_cc0
5741       /* If we are now comparing against zero, change our source if
5742          needed.  If we do not use cc0, we always have a COMPARE.  */
5743       if (op1 == const0_rtx && dest == cc0_rtx)
5744         {
5745           SUBST (SET_SRC (x), op0);
5746           src = op0;
5747         }
5748       else
5749 #endif
5750
5751       /* Otherwise, if we didn't previously have a COMPARE in the
5752          correct mode, we need one.  */
5753       if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5754         {
5755           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5756           src = SET_SRC (x);
5757         }
5758       else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
5759         {
5760           SUBST (SET_SRC (x), op0);
5761           src = SET_SRC (x);
5762         }
5763       /* Otherwise, update the COMPARE if needed.  */
5764       else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
5765         {
5766           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5767           src = SET_SRC (x);
5768         }
5769     }
5770   else
5771     {
5772       /* Get SET_SRC in a form where we have placed back any
5773          compound expressions.  Then do the checks below.  */
5774       src = make_compound_operation (src, SET);
5775       SUBST (SET_SRC (x), src);
5776     }
5777
5778   /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5779      and X being a REG or (subreg (reg)), we may be able to convert this to
5780      (set (subreg:m2 x) (op)).
5781
5782      We can always do this if M1 is narrower than M2 because that means that
5783      we only care about the low bits of the result.
5784
5785      However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
5786      perform a narrower operation than requested since the high-order bits will
5787      be undefined.  On machine where it is defined, this transformation is safe
5788      as long as M1 and M2 have the same number of words.  */
5789
5790   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5791       && !OBJECT_P (SUBREG_REG (src))
5792       && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5793            / UNITS_PER_WORD)
5794           == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5795                + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5796 #ifndef WORD_REGISTER_OPERATIONS
5797       && (GET_MODE_SIZE (GET_MODE (src))
5798         < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5799 #endif
5800 #ifdef CANNOT_CHANGE_MODE_CLASS
5801       && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
5802             && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5803                                          GET_MODE (SUBREG_REG (src)),
5804                                          GET_MODE (src)))
5805 #endif
5806       && (REG_P (dest)
5807           || (GET_CODE (dest) == SUBREG
5808               && REG_P (SUBREG_REG (dest)))))
5809     {
5810       SUBST (SET_DEST (x),
5811              gen_lowpart (GET_MODE (SUBREG_REG (src)),
5812                                       dest));
5813       SUBST (SET_SRC (x), SUBREG_REG (src));
5814
5815       src = SET_SRC (x), dest = SET_DEST (x);
5816     }
5817
5818 #ifdef HAVE_cc0
5819   /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5820      in SRC.  */
5821   if (dest == cc0_rtx
5822       && GET_CODE (src) == SUBREG
5823       && subreg_lowpart_p (src)
5824       && (GET_MODE_BITSIZE (GET_MODE (src))
5825           < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5826     {
5827       rtx inner = SUBREG_REG (src);
5828       enum machine_mode inner_mode = GET_MODE (inner);
5829
5830       /* Here we make sure that we don't have a sign bit on.  */
5831       if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5832           && (nonzero_bits (inner, inner_mode)
5833               < ((unsigned HOST_WIDE_INT) 1
5834                  << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5835         {
5836           SUBST (SET_SRC (x), inner);
5837           src = SET_SRC (x);
5838         }
5839     }
5840 #endif
5841
5842 #ifdef LOAD_EXTEND_OP
5843   /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5844      would require a paradoxical subreg.  Replace the subreg with a
5845      zero_extend to avoid the reload that would otherwise be required.  */
5846
5847   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5848       && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
5849       && SUBREG_BYTE (src) == 0
5850       && (GET_MODE_SIZE (GET_MODE (src))
5851           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5852       && MEM_P (SUBREG_REG (src)))
5853     {
5854       SUBST (SET_SRC (x),
5855              gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5856                             GET_MODE (src), SUBREG_REG (src)));
5857
5858       src = SET_SRC (x);
5859     }
5860 #endif
5861
5862   /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5863      are comparing an item known to be 0 or -1 against 0, use a logical
5864      operation instead. Check for one of the arms being an IOR of the other
5865      arm with some value.  We compute three terms to be IOR'ed together.  In
5866      practice, at most two will be nonzero.  Then we do the IOR's.  */
5867
5868   if (GET_CODE (dest) != PC
5869       && GET_CODE (src) == IF_THEN_ELSE
5870       && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5871       && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5872       && XEXP (XEXP (src, 0), 1) == const0_rtx
5873       && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5874 #ifdef HAVE_conditional_move
5875       && ! can_conditionally_move_p (GET_MODE (src))
5876 #endif
5877       && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5878                                GET_MODE (XEXP (XEXP (src, 0), 0)))
5879           == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5880       && ! side_effects_p (src))
5881     {
5882       rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5883                       ? XEXP (src, 1) : XEXP (src, 2));
5884       rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5885                    ? XEXP (src, 2) : XEXP (src, 1));
5886       rtx term1 = const0_rtx, term2, term3;
5887
5888       if (GET_CODE (true_rtx) == IOR
5889           && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5890         term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5891       else if (GET_CODE (true_rtx) == IOR
5892                && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5893         term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5894       else if (GET_CODE (false_rtx) == IOR
5895                && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5896         term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5897       else if (GET_CODE (false_rtx) == IOR
5898                && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5899         term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5900
5901       term2 = simplify_gen_binary (AND, GET_MODE (src),
5902                                    XEXP (XEXP (src, 0), 0), true_rtx);
5903       term3 = simplify_gen_binary (AND, GET_MODE (src),
5904                                    simplify_gen_unary (NOT, GET_MODE (src),
5905                                                        XEXP (XEXP (src, 0), 0),
5906                                                        GET_MODE (src)),
5907                                    false_rtx);
5908
5909       SUBST (SET_SRC (x),
5910              simplify_gen_binary (IOR, GET_MODE (src),
5911                                   simplify_gen_binary (IOR, GET_MODE (src),
5912                                                        term1, term2),
5913                                   term3));
5914
5915       src = SET_SRC (x);
5916     }
5917
5918   /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5919      whole thing fail.  */
5920   if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5921     return src;
5922   else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5923     return dest;
5924   else
5925     /* Convert this into a field assignment operation, if possible.  */
5926     return make_field_assignment (x);
5927 }
5928 \f
5929 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5930    result.  */
5931
5932 static rtx
5933 simplify_logical (rtx x)
5934 {
5935   enum machine_mode mode = GET_MODE (x);
5936   rtx op0 = XEXP (x, 0);
5937   rtx op1 = XEXP (x, 1);
5938
5939   switch (GET_CODE (x))
5940     {
5941     case AND:
5942       /* We can call simplify_and_const_int only if we don't lose
5943          any (sign) bits when converting INTVAL (op1) to
5944          "unsigned HOST_WIDE_INT".  */
5945       if (GET_CODE (op1) == CONST_INT
5946           && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5947               || INTVAL (op1) > 0))
5948         {
5949           x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5950           if (GET_CODE (x) != AND)
5951             return x;
5952
5953           op0 = XEXP (x, 0);
5954           op1 = XEXP (x, 1);
5955         }
5956
5957       /* If we have any of (and (ior A B) C) or (and (xor A B) C),
5958          apply the distributive law and then the inverse distributive
5959          law to see if things simplify.  */
5960       if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5961         {
5962           rtx result = distribute_and_simplify_rtx (x, 0);
5963           if (result)
5964             return result;
5965         }
5966       if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5967         {
5968           rtx result = distribute_and_simplify_rtx (x, 1);
5969           if (result)
5970             return result;
5971         }
5972       break;
5973
5974     case IOR:
5975       /* If we have (ior (and A B) C), apply the distributive law and then
5976          the inverse distributive law to see if things simplify.  */
5977
5978       if (GET_CODE (op0) == AND)
5979         {
5980           rtx result = distribute_and_simplify_rtx (x, 0);
5981           if (result)
5982             return result;
5983         }
5984
5985       if (GET_CODE (op1) == AND)
5986         {
5987           rtx result = distribute_and_simplify_rtx (x, 1);
5988           if (result)
5989             return result;
5990         }
5991       break;
5992
5993     default:
5994       gcc_unreachable ();
5995     }
5996
5997   return x;
5998 }
5999 \f
6000 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6001    operations" because they can be replaced with two more basic operations.
6002    ZERO_EXTEND is also considered "compound" because it can be replaced with
6003    an AND operation, which is simpler, though only one operation.
6004
6005    The function expand_compound_operation is called with an rtx expression
6006    and will convert it to the appropriate shifts and AND operations,
6007    simplifying at each stage.
6008
6009    The function make_compound_operation is called to convert an expression
6010    consisting of shifts and ANDs into the equivalent compound expression.
6011    It is the inverse of this function, loosely speaking.  */
6012
6013 static rtx
6014 expand_compound_operation (rtx x)
6015 {
6016   unsigned HOST_WIDE_INT pos = 0, len;
6017   int unsignedp = 0;
6018   unsigned int modewidth;
6019   rtx tem;
6020
6021   switch (GET_CODE (x))
6022     {
6023     case ZERO_EXTEND:
6024       unsignedp = 1;
6025     case SIGN_EXTEND:
6026       /* We can't necessarily use a const_int for a multiword mode;
6027          it depends on implicitly extending the value.
6028          Since we don't know the right way to extend it,
6029          we can't tell whether the implicit way is right.
6030
6031          Even for a mode that is no wider than a const_int,
6032          we can't win, because we need to sign extend one of its bits through
6033          the rest of it, and we don't know which bit.  */
6034       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
6035         return x;
6036
6037       /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6038          (zero_extend:MODE FROM) or (sign_extend:MODE FROM).  It is for any MEM
6039          because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6040          reloaded. If not for that, MEM's would very rarely be safe.
6041
6042          Reject MODEs bigger than a word, because we might not be able
6043          to reference a two-register group starting with an arbitrary register
6044          (and currently gen_lowpart might crash for a SUBREG).  */
6045
6046       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6047         return x;
6048
6049       /* Reject MODEs that aren't scalar integers because turning vector
6050          or complex modes into shifts causes problems.  */
6051
6052       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6053         return x;
6054
6055       len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
6056       /* If the inner object has VOIDmode (the only way this can happen
6057          is if it is an ASM_OPERANDS), we can't do anything since we don't
6058          know how much masking to do.  */
6059       if (len == 0)
6060         return x;
6061
6062       break;
6063
6064     case ZERO_EXTRACT:
6065       unsignedp = 1;
6066
6067       /* ... fall through ...  */
6068
6069     case SIGN_EXTRACT:
6070       /* If the operand is a CLOBBER, just return it.  */
6071       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6072         return XEXP (x, 0);
6073
6074       if (GET_CODE (XEXP (x, 1)) != CONST_INT
6075           || GET_CODE (XEXP (x, 2)) != CONST_INT
6076           || GET_MODE (XEXP (x, 0)) == VOIDmode)
6077         return x;
6078
6079       /* Reject MODEs that aren't scalar integers because turning vector
6080          or complex modes into shifts causes problems.  */
6081
6082       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6083         return x;
6084
6085       len = INTVAL (XEXP (x, 1));
6086       pos = INTVAL (XEXP (x, 2));
6087
6088       /* This should stay within the object being extracted, fail otherwise.  */
6089       if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
6090         return x;
6091
6092       if (BITS_BIG_ENDIAN)
6093         pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
6094
6095       break;
6096
6097     default:
6098       return x;
6099     }
6100   /* Convert sign extension to zero extension, if we know that the high
6101      bit is not set, as this is easier to optimize.  It will be converted
6102      back to cheaper alternative in make_extraction.  */
6103   if (GET_CODE (x) == SIGN_EXTEND
6104       && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6105           && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6106                 & ~(((unsigned HOST_WIDE_INT)
6107                       GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6108                      >> 1))
6109                == 0)))
6110     {
6111       rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6112       rtx temp2 = expand_compound_operation (temp);
6113
6114       /* Make sure this is a profitable operation.  */
6115       if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
6116        return temp2;
6117       else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
6118        return temp;
6119       else
6120        return x;
6121     }
6122
6123   /* We can optimize some special cases of ZERO_EXTEND.  */
6124   if (GET_CODE (x) == ZERO_EXTEND)
6125     {
6126       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6127          know that the last value didn't have any inappropriate bits
6128          set.  */
6129       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6130           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6131           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6132           && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6133               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6134         return XEXP (XEXP (x, 0), 0);
6135
6136       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
6137       if (GET_CODE (XEXP (x, 0)) == SUBREG
6138           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6139           && subreg_lowpart_p (XEXP (x, 0))
6140           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6141           && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6142               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6143         return SUBREG_REG (XEXP (x, 0));
6144
6145       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6146          is a comparison and STORE_FLAG_VALUE permits.  This is like
6147          the first case, but it works even when GET_MODE (x) is larger
6148          than HOST_WIDE_INT.  */
6149       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6150           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6151           && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6152           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6153               <= HOST_BITS_PER_WIDE_INT)
6154           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
6155               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6156         return XEXP (XEXP (x, 0), 0);
6157
6158       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
6159       if (GET_CODE (XEXP (x, 0)) == SUBREG
6160           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6161           && subreg_lowpart_p (XEXP (x, 0))
6162           && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6163           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6164               <= HOST_BITS_PER_WIDE_INT)
6165           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
6166               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6167         return SUBREG_REG (XEXP (x, 0));
6168
6169     }
6170
6171   /* If we reach here, we want to return a pair of shifts.  The inner
6172      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
6173      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
6174      logical depending on the value of UNSIGNEDP.
6175
6176      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6177      converted into an AND of a shift.
6178
6179      We must check for the case where the left shift would have a negative
6180      count.  This can happen in a case like (x >> 31) & 255 on machines
6181      that can't shift by a constant.  On those machines, we would first
6182      combine the shift with the AND to produce a variable-position
6183      extraction.  Then the constant of 31 would be substituted in to produce
6184      a such a position.  */
6185
6186   modewidth = GET_MODE_BITSIZE (GET_MODE (x));
6187   if (modewidth + len >= pos)
6188     {
6189       enum machine_mode mode = GET_MODE (x);
6190       tem = gen_lowpart (mode, XEXP (x, 0));
6191       if (!tem || GET_CODE (tem) == CLOBBER)
6192         return x;
6193       tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6194                                   tem, modewidth - pos - len);
6195       tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6196                                   mode, tem, modewidth - len);
6197     }
6198   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6199     tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6200                                   simplify_shift_const (NULL_RTX, LSHIFTRT,
6201                                                         GET_MODE (x),
6202                                                         XEXP (x, 0), pos),
6203                                   ((HOST_WIDE_INT) 1 << len) - 1);
6204   else
6205     /* Any other cases we can't handle.  */
6206     return x;
6207
6208   /* If we couldn't do this for some reason, return the original
6209      expression.  */
6210   if (GET_CODE (tem) == CLOBBER)
6211     return x;
6212
6213   return tem;
6214 }
6215 \f
6216 /* X is a SET which contains an assignment of one object into
6217    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6218    or certain SUBREGS). If possible, convert it into a series of
6219    logical operations.
6220
6221    We half-heartedly support variable positions, but do not at all
6222    support variable lengths.  */
6223
6224 static const_rtx
6225 expand_field_assignment (const_rtx x)
6226 {
6227   rtx inner;
6228   rtx pos;                      /* Always counts from low bit.  */
6229   int len;
6230   rtx mask, cleared, masked;
6231   enum machine_mode compute_mode;
6232
6233   /* Loop until we find something we can't simplify.  */
6234   while (1)
6235     {
6236       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6237           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6238         {
6239           inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6240           len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
6241           pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6242         }
6243       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6244                && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
6245         {
6246           inner = XEXP (SET_DEST (x), 0);
6247           len = INTVAL (XEXP (SET_DEST (x), 1));
6248           pos = XEXP (SET_DEST (x), 2);
6249
6250           /* A constant position should stay within the width of INNER.  */
6251           if (GET_CODE (pos) == CONST_INT
6252               && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
6253             break;
6254
6255           if (BITS_BIG_ENDIAN)
6256             {
6257               if (GET_CODE (pos) == CONST_INT)
6258                 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
6259                                - INTVAL (pos));
6260               else if (GET_CODE (pos) == MINUS
6261                        && GET_CODE (XEXP (pos, 1)) == CONST_INT
6262                        && (INTVAL (XEXP (pos, 1))
6263                            == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
6264                 /* If position is ADJUST - X, new position is X.  */
6265                 pos = XEXP (pos, 0);
6266               else
6267                 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6268                                            GEN_INT (GET_MODE_BITSIZE (
6269                                                     GET_MODE (inner))
6270                                                     - len),
6271                                            pos);
6272             }
6273         }
6274
6275       /* A SUBREG between two modes that occupy the same numbers of words
6276          can be done by moving the SUBREG to the source.  */
6277       else if (GET_CODE (SET_DEST (x)) == SUBREG
6278                /* We need SUBREGs to compute nonzero_bits properly.  */
6279                && nonzero_sign_valid
6280                && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6281                      + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6282                    == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6283                         + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6284         {
6285           x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6286                            gen_lowpart
6287                            (GET_MODE (SUBREG_REG (SET_DEST (x))),
6288                             SET_SRC (x)));
6289           continue;
6290         }
6291       else
6292         break;
6293
6294       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6295         inner = SUBREG_REG (inner);
6296
6297       compute_mode = GET_MODE (inner);
6298
6299       /* Don't attempt bitwise arithmetic on non scalar integer modes.  */
6300       if (! SCALAR_INT_MODE_P (compute_mode))
6301         {
6302           enum machine_mode imode;
6303
6304           /* Don't do anything for vector or complex integral types.  */
6305           if (! FLOAT_MODE_P (compute_mode))
6306             break;
6307
6308           /* Try to find an integral mode to pun with.  */
6309           imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6310           if (imode == BLKmode)
6311             break;
6312
6313           compute_mode = imode;
6314           inner = gen_lowpart (imode, inner);
6315         }
6316
6317       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
6318       if (len >= HOST_BITS_PER_WIDE_INT)
6319         break;
6320
6321       /* Now compute the equivalent expression.  Make a copy of INNER
6322          for the SET_DEST in case it is a MEM into which we will substitute;
6323          we don't want shared RTL in that case.  */
6324       mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
6325       cleared = simplify_gen_binary (AND, compute_mode,
6326                                      simplify_gen_unary (NOT, compute_mode,
6327                                        simplify_gen_binary (ASHIFT,
6328                                                             compute_mode,
6329                                                             mask, pos),
6330                                        compute_mode),
6331                                      inner);
6332       masked = simplify_gen_binary (ASHIFT, compute_mode,
6333                                     simplify_gen_binary (
6334                                       AND, compute_mode,
6335                                       gen_lowpart (compute_mode, SET_SRC (x)),
6336                                       mask),
6337                                     pos);
6338
6339       x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
6340                        simplify_gen_binary (IOR, compute_mode,
6341                                             cleared, masked));
6342     }
6343
6344   return x;
6345 }
6346 \f
6347 /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
6348    it is an RTX that represents a variable starting position; otherwise,
6349    POS is the (constant) starting bit position (counted from the LSB).
6350
6351    UNSIGNEDP is nonzero for an unsigned reference and zero for a
6352    signed reference.
6353
6354    IN_DEST is nonzero if this is a reference in the destination of a
6355    SET.  This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If nonzero,
6356    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6357    be used.
6358
6359    IN_COMPARE is nonzero if we are in a COMPARE.  This means that a
6360    ZERO_EXTRACT should be built even for bits starting at bit 0.
6361
6362    MODE is the desired mode of the result (if IN_DEST == 0).
6363
6364    The result is an RTX for the extraction or NULL_RTX if the target
6365    can't handle it.  */
6366
6367 static rtx
6368 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6369                  rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6370                  int in_dest, int in_compare)
6371 {
6372   /* This mode describes the size of the storage area
6373      to fetch the overall value from.  Within that, we
6374      ignore the POS lowest bits, etc.  */
6375   enum machine_mode is_mode = GET_MODE (inner);
6376   enum machine_mode inner_mode;
6377   enum machine_mode wanted_inner_mode;
6378   enum machine_mode wanted_inner_reg_mode = word_mode;
6379   enum machine_mode pos_mode = word_mode;
6380   enum machine_mode extraction_mode = word_mode;
6381   enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6382   rtx new = 0;
6383   rtx orig_pos_rtx = pos_rtx;
6384   HOST_WIDE_INT orig_pos;
6385
6386   if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6387     {
6388       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6389          consider just the QI as the memory to extract from.
6390          The subreg adds or removes high bits; its mode is
6391          irrelevant to the meaning of this extraction,
6392          since POS and LEN count from the lsb.  */
6393       if (MEM_P (SUBREG_REG (inner)))
6394         is_mode = GET_MODE (SUBREG_REG (inner));
6395       inner = SUBREG_REG (inner);
6396     }
6397   else if (GET_CODE (inner) == ASHIFT
6398            && GET_CODE (XEXP (inner, 1)) == CONST_INT
6399            && pos_rtx == 0 && pos == 0
6400            && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6401     {
6402       /* We're extracting the least significant bits of an rtx
6403          (ashift X (const_int C)), where LEN > C.  Extract the
6404          least significant (LEN - C) bits of X, giving an rtx
6405          whose mode is MODE, then shift it left C times.  */
6406       new = make_extraction (mode, XEXP (inner, 0),
6407                              0, 0, len - INTVAL (XEXP (inner, 1)),
6408                              unsignedp, in_dest, in_compare);
6409       if (new != 0)
6410         return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
6411     }
6412
6413   inner_mode = GET_MODE (inner);
6414
6415   if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
6416     pos = INTVAL (pos_rtx), pos_rtx = 0;
6417
6418   /* See if this can be done without an extraction.  We never can if the
6419      width of the field is not the same as that of some integer mode. For
6420      registers, we can only avoid the extraction if the position is at the
6421      low-order bit and this is either not in the destination or we have the
6422      appropriate STRICT_LOW_PART operation available.
6423
6424      For MEM, we can avoid an extract if the field starts on an appropriate
6425      boundary and we can change the mode of the memory reference.  */
6426
6427   if (tmode != BLKmode
6428       && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6429            && !MEM_P (inner)
6430            && (inner_mode == tmode
6431                || !REG_P (inner)
6432                || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
6433                                          GET_MODE_BITSIZE (inner_mode))
6434                || reg_truncated_to_mode (tmode, inner))
6435            && (! in_dest
6436                || (REG_P (inner)
6437                    && have_insn_for (STRICT_LOW_PART, tmode))))
6438           || (MEM_P (inner) && pos_rtx == 0
6439               && (pos
6440                   % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6441                      : BITS_PER_UNIT)) == 0
6442               /* We can't do this if we are widening INNER_MODE (it
6443                  may not be aligned, for one thing).  */
6444               && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6445               && (inner_mode == tmode
6446                   || (! mode_dependent_address_p (XEXP (inner, 0))
6447                       && ! MEM_VOLATILE_P (inner))))))
6448     {
6449       /* If INNER is a MEM, make a new MEM that encompasses just the desired
6450          field.  If the original and current mode are the same, we need not
6451          adjust the offset.  Otherwise, we do if bytes big endian.
6452
6453          If INNER is not a MEM, get a piece consisting of just the field
6454          of interest (in this case POS % BITS_PER_WORD must be 0).  */
6455
6456       if (MEM_P (inner))
6457         {
6458           HOST_WIDE_INT offset;
6459
6460           /* POS counts from lsb, but make OFFSET count in memory order.  */
6461           if (BYTES_BIG_ENDIAN)
6462             offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6463           else
6464             offset = pos / BITS_PER_UNIT;
6465
6466           new = adjust_address_nv (inner, tmode, offset);
6467         }
6468       else if (REG_P (inner))
6469         {
6470           if (tmode != inner_mode)
6471             {
6472               /* We can't call gen_lowpart in a DEST since we
6473                  always want a SUBREG (see below) and it would sometimes
6474                  return a new hard register.  */
6475               if (pos || in_dest)
6476                 {
6477                   HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6478
6479                   if (WORDS_BIG_ENDIAN
6480                       && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6481                     final_word = ((GET_MODE_SIZE (inner_mode)
6482                                    - GET_MODE_SIZE (tmode))
6483                                   / UNITS_PER_WORD) - final_word;
6484
6485                   final_word *= UNITS_PER_WORD;
6486                   if (BYTES_BIG_ENDIAN &&
6487                       GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6488                     final_word += (GET_MODE_SIZE (inner_mode)
6489                                    - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6490
6491                   /* Avoid creating invalid subregs, for example when
6492                      simplifying (x>>32)&255.  */
6493                   if (!validate_subreg (tmode, inner_mode, inner, final_word))
6494                     return NULL_RTX;
6495
6496                   new = gen_rtx_SUBREG (tmode, inner, final_word);
6497                 }
6498               else
6499                 new = gen_lowpart (tmode, inner);
6500             }
6501           else
6502             new = inner;
6503         }
6504       else
6505         new = force_to_mode (inner, tmode,
6506                              len >= HOST_BITS_PER_WIDE_INT
6507                              ? ~(unsigned HOST_WIDE_INT) 0
6508                              : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6509                              0);
6510
6511       /* If this extraction is going into the destination of a SET,
6512          make a STRICT_LOW_PART unless we made a MEM.  */
6513
6514       if (in_dest)
6515         return (MEM_P (new) ? new
6516                 : (GET_CODE (new) != SUBREG
6517                    ? gen_rtx_CLOBBER (tmode, const0_rtx)
6518                    : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
6519
6520       if (mode == tmode)
6521         return new;
6522
6523       if (GET_CODE (new) == CONST_INT)
6524         return gen_int_mode (INTVAL (new), mode);
6525
6526       /* If we know that no extraneous bits are set, and that the high
6527          bit is not set, convert the extraction to the cheaper of
6528          sign and zero extension, that are equivalent in these cases.  */
6529       if (flag_expensive_optimizations
6530           && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6531               && ((nonzero_bits (new, tmode)
6532                    & ~(((unsigned HOST_WIDE_INT)
6533                         GET_MODE_MASK (tmode))
6534                        >> 1))
6535                   == 0)))
6536         {
6537           rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6538           rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6539
6540           /* Prefer ZERO_EXTENSION, since it gives more information to
6541              backends.  */
6542           if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6543             return temp;
6544           return temp1;
6545         }
6546
6547       /* Otherwise, sign- or zero-extend unless we already are in the
6548          proper mode.  */
6549
6550       return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6551                              mode, new));
6552     }
6553
6554   /* Unless this is a COMPARE or we have a funny memory reference,
6555      don't do anything with zero-extending field extracts starting at
6556      the low-order bit since they are simple AND operations.  */
6557   if (pos_rtx == 0 && pos == 0 && ! in_dest
6558       && ! in_compare && unsignedp)
6559     return 0;
6560
6561   /* Unless INNER is not MEM, reject this if we would be spanning bytes or
6562      if the position is not a constant and the length is not 1.  In all
6563      other cases, we would only be going outside our object in cases when
6564      an original shift would have been undefined.  */
6565   if (MEM_P (inner)
6566       && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6567           || (pos_rtx != 0 && len != 1)))
6568     return 0;
6569
6570   /* Get the mode to use should INNER not be a MEM, the mode for the position,
6571      and the mode for the result.  */
6572   if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6573     {
6574       wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6575       pos_mode = mode_for_extraction (EP_insv, 2);
6576       extraction_mode = mode_for_extraction (EP_insv, 3);
6577     }
6578
6579   if (! in_dest && unsignedp
6580       && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6581     {
6582       wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6583       pos_mode = mode_for_extraction (EP_extzv, 3);
6584       extraction_mode = mode_for_extraction (EP_extzv, 0);
6585     }
6586
6587   if (! in_dest && ! unsignedp
6588       && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6589     {
6590       wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6591       pos_mode = mode_for_extraction (EP_extv, 3);
6592       extraction_mode = mode_for_extraction (EP_extv, 0);
6593     }
6594
6595   /* Never narrow an object, since that might not be safe.  */
6596
6597   if (mode != VOIDmode
6598       && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6599     extraction_mode = mode;
6600
6601   if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6602       && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6603     pos_mode = GET_MODE (pos_rtx);
6604
6605   /* If this is not from memory, the desired mode is the preferred mode
6606      for an extraction pattern's first input operand, or word_mode if there
6607      is none.  */
6608   if (!MEM_P (inner))
6609     wanted_inner_mode = wanted_inner_reg_mode;
6610   else
6611     {
6612       /* Be careful not to go beyond the extracted object and maintain the
6613          natural alignment of the memory.  */
6614       wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
6615       while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
6616              > GET_MODE_BITSIZE (wanted_inner_mode))
6617         {
6618           wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
6619           gcc_assert (wanted_inner_mode != VOIDmode);
6620         }
6621
6622       /* If we have to change the mode of memory and cannot, the desired mode
6623          is EXTRACTION_MODE.  */
6624       if (inner_mode != wanted_inner_mode
6625           && (mode_dependent_address_p (XEXP (inner, 0))
6626               || MEM_VOLATILE_P (inner)
6627               || pos_rtx))
6628         wanted_inner_mode = extraction_mode;
6629     }
6630
6631   orig_pos = pos;
6632
6633   if (BITS_BIG_ENDIAN)
6634     {
6635       /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6636          BITS_BIG_ENDIAN style.  If position is constant, compute new
6637          position.  Otherwise, build subtraction.
6638          Note that POS is relative to the mode of the original argument.
6639          If it's a MEM we need to recompute POS relative to that.
6640          However, if we're extracting from (or inserting into) a register,
6641          we want to recompute POS relative to wanted_inner_mode.  */
6642       int width = (MEM_P (inner)
6643                    ? GET_MODE_BITSIZE (is_mode)
6644                    : GET_MODE_BITSIZE (wanted_inner_mode));
6645
6646       if (pos_rtx == 0)
6647         pos = width - len - pos;
6648       else
6649         pos_rtx
6650           = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6651       /* POS may be less than 0 now, but we check for that below.
6652          Note that it can only be less than 0 if !MEM_P (inner).  */
6653     }
6654
6655   /* If INNER has a wider mode, and this is a constant extraction, try to
6656      make it smaller and adjust the byte to point to the byte containing
6657      the value.  */
6658   if (wanted_inner_mode != VOIDmode
6659       && inner_mode != wanted_inner_mode
6660       && ! pos_rtx
6661       && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6662       && MEM_P (inner)
6663       && ! mode_dependent_address_p (XEXP (inner, 0))
6664       && ! MEM_VOLATILE_P (inner))
6665     {
6666       int offset = 0;
6667
6668       /* The computations below will be correct if the machine is big
6669          endian in both bits and bytes or little endian in bits and bytes.
6670          If it is mixed, we must adjust.  */
6671
6672       /* If bytes are big endian and we had a paradoxical SUBREG, we must
6673          adjust OFFSET to compensate.  */
6674       if (BYTES_BIG_ENDIAN
6675           && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6676         offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6677
6678       /* We can now move to the desired byte.  */
6679       offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
6680                 * GET_MODE_SIZE (wanted_inner_mode);
6681       pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6682
6683       if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6684           && is_mode != wanted_inner_mode)
6685         offset = (GET_MODE_SIZE (is_mode)
6686                   - GET_MODE_SIZE (wanted_inner_mode) - offset);
6687
6688       inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6689     }
6690
6691   /* If INNER is not memory, we can always get it into the proper mode.  If we
6692      are changing its mode, POS must be a constant and smaller than the size
6693      of the new mode.  */
6694   else if (!MEM_P (inner))
6695     {
6696       if (GET_MODE (inner) != wanted_inner_mode
6697           && (pos_rtx != 0
6698               || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6699         return 0;
6700
6701       if (orig_pos < 0)
6702         return 0;
6703
6704       inner = force_to_mode (inner, wanted_inner_mode,
6705                              pos_rtx
6706                              || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6707                              ? ~(unsigned HOST_WIDE_INT) 0
6708                              : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6709                                 << orig_pos),
6710                              0);
6711     }
6712
6713   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
6714      have to zero extend.  Otherwise, we can just use a SUBREG.  */
6715   if (pos_rtx != 0
6716       && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6717     {
6718       rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6719
6720       /* If we know that no extraneous bits are set, and that the high
6721          bit is not set, convert extraction to cheaper one - either
6722          SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6723          cases.  */
6724       if (flag_expensive_optimizations
6725           && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6726               && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6727                    & ~(((unsigned HOST_WIDE_INT)
6728                         GET_MODE_MASK (GET_MODE (pos_rtx)))
6729                        >> 1))
6730                   == 0)))
6731         {
6732           rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6733
6734           /* Prefer ZERO_EXTENSION, since it gives more information to
6735              backends.  */
6736           if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6737             temp = temp1;
6738         }
6739       pos_rtx = temp;
6740     }
6741   else if (pos_rtx != 0
6742            && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6743     pos_rtx = gen_lowpart (pos_mode, pos_rtx);
6744
6745   /* Make POS_RTX unless we already have it and it is correct.  If we don't
6746      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6747      be a CONST_INT.  */
6748   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6749     pos_rtx = orig_pos_rtx;
6750
6751   else if (pos_rtx == 0)
6752     pos_rtx = GEN_INT (pos);
6753
6754   /* Make the required operation.  See if we can use existing rtx.  */
6755   new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6756                          extraction_mode, inner, GEN_INT (len), pos_rtx);
6757   if (! in_dest)
6758     new = gen_lowpart (mode, new);
6759
6760   return new;
6761 }
6762 \f
6763 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6764    with any other operations in X.  Return X without that shift if so.  */
6765
6766 static rtx
6767 extract_left_shift (rtx x, int count)
6768 {
6769   enum rtx_code code = GET_CODE (x);
6770   enum machine_mode mode = GET_MODE (x);
6771   rtx tem;
6772
6773   switch (code)
6774     {
6775     case ASHIFT:
6776       /* This is the shift itself.  If it is wide enough, we will return
6777          either the value being shifted if the shift count is equal to
6778          COUNT or a shift for the difference.  */
6779       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6780           && INTVAL (XEXP (x, 1)) >= count)
6781         return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6782                                      INTVAL (XEXP (x, 1)) - count);
6783       break;
6784
6785     case NEG:  case NOT:
6786       if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6787         return simplify_gen_unary (code, mode, tem, mode);
6788
6789       break;
6790
6791     case PLUS:  case IOR:  case XOR:  case AND:
6792       /* If we can safely shift this constant and we find the inner shift,
6793          make a new operation.  */
6794       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6795           && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6796           && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6797         return simplify_gen_binary (code, mode, tem,
6798                                     GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6799
6800       break;
6801
6802     default:
6803       break;
6804     }
6805
6806   return 0;
6807 }
6808 \f
6809 /* Look at the expression rooted at X.  Look for expressions
6810    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6811    Form these expressions.
6812
6813    Return the new rtx, usually just X.
6814
6815    Also, for machines like the VAX that don't have logical shift insns,
6816    try to convert logical to arithmetic shift operations in cases where
6817    they are equivalent.  This undoes the canonicalizations to logical
6818    shifts done elsewhere.
6819
6820    We try, as much as possible, to re-use rtl expressions to save memory.
6821
6822    IN_CODE says what kind of expression we are processing.  Normally, it is
6823    SET.  In a memory address (inside a MEM, PLUS or minus, the latter two
6824    being kludges), it is MEM.  When processing the arguments of a comparison
6825    or a COMPARE against zero, it is COMPARE.  */
6826
6827 static rtx
6828 make_compound_operation (rtx x, enum rtx_code in_code)
6829 {
6830   enum rtx_code code = GET_CODE (x);
6831   enum machine_mode mode = GET_MODE (x);
6832   int mode_width = GET_MODE_BITSIZE (mode);
6833   rtx rhs, lhs;
6834   enum rtx_code next_code;
6835   int i;
6836   rtx new = 0;
6837   rtx tem;
6838   const char *fmt;
6839
6840   /* Select the code to be used in recursive calls.  Once we are inside an
6841      address, we stay there.  If we have a comparison, set to COMPARE,
6842      but once inside, go back to our default of SET.  */
6843
6844   next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6845                : ((code == COMPARE || COMPARISON_P (x))
6846                   && XEXP (x, 1) == const0_rtx) ? COMPARE
6847                : in_code == COMPARE ? SET : in_code);
6848
6849   /* Process depending on the code of this operation.  If NEW is set
6850      nonzero, it will be returned.  */
6851
6852   switch (code)
6853     {
6854     case ASHIFT:
6855       /* Convert shifts by constants into multiplications if inside
6856          an address.  */
6857       if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6858           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6859           && INTVAL (XEXP (x, 1)) >= 0)
6860         {
6861           new = make_compound_operation (XEXP (x, 0), next_code);
6862           new = gen_rtx_MULT (mode, new,
6863                               GEN_INT ((HOST_WIDE_INT) 1
6864                                        << INTVAL (XEXP (x, 1))));
6865         }
6866       break;
6867
6868     case AND:
6869       /* If the second operand is not a constant, we can't do anything
6870          with it.  */
6871       if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6872         break;
6873
6874       /* If the constant is a power of two minus one and the first operand
6875          is a logical right shift, make an extraction.  */
6876       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6877           && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6878         {
6879           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6880           new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6881                                  0, in_code == COMPARE);
6882         }
6883
6884       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
6885       else if (GET_CODE (XEXP (x, 0)) == SUBREG
6886                && subreg_lowpart_p (XEXP (x, 0))
6887                && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6888                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6889         {
6890           new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6891                                          next_code);
6892           new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6893                                  XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6894                                  0, in_code == COMPARE);
6895         }
6896       /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)).  */
6897       else if ((GET_CODE (XEXP (x, 0)) == XOR
6898                 || GET_CODE (XEXP (x, 0)) == IOR)
6899                && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6900                && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6901                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6902         {
6903           /* Apply the distributive law, and then try to make extractions.  */
6904           new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6905                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6906                                              XEXP (x, 1)),
6907                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6908                                              XEXP (x, 1)));
6909           new = make_compound_operation (new, in_code);
6910         }
6911
6912       /* If we are have (and (rotate X C) M) and C is larger than the number
6913          of bits in M, this is an extraction.  */
6914
6915       else if (GET_CODE (XEXP (x, 0)) == ROTATE
6916                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6917                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6918                && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6919         {
6920           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6921           new = make_extraction (mode, new,
6922                                  (GET_MODE_BITSIZE (mode)
6923                                   - INTVAL (XEXP (XEXP (x, 0), 1))),
6924                                  NULL_RTX, i, 1, 0, in_code == COMPARE);
6925         }
6926
6927       /* On machines without logical shifts, if the operand of the AND is
6928          a logical shift and our mask turns off all the propagated sign
6929          bits, we can replace the logical shift with an arithmetic shift.  */
6930       else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6931                && !have_insn_for (LSHIFTRT, mode)
6932                && have_insn_for (ASHIFTRT, mode)
6933                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6934                && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6935                && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6936                && mode_width <= HOST_BITS_PER_WIDE_INT)
6937         {
6938           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6939
6940           mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6941           if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6942             SUBST (XEXP (x, 0),
6943                    gen_rtx_ASHIFTRT (mode,
6944                                      make_compound_operation
6945                                      (XEXP (XEXP (x, 0), 0), next_code),
6946                                      XEXP (XEXP (x, 0), 1)));
6947         }
6948
6949       /* If the constant is one less than a power of two, this might be
6950          representable by an extraction even if no shift is present.
6951          If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6952          we are in a COMPARE.  */
6953       else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6954         new = make_extraction (mode,
6955                                make_compound_operation (XEXP (x, 0),
6956                                                         next_code),
6957                                0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6958
6959       /* If we are in a comparison and this is an AND with a power of two,
6960          convert this into the appropriate bit extract.  */
6961       else if (in_code == COMPARE
6962                && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6963         new = make_extraction (mode,
6964                                make_compound_operation (XEXP (x, 0),
6965                                                         next_code),
6966                                i, NULL_RTX, 1, 1, 0, 1);
6967
6968       break;
6969
6970     case LSHIFTRT:
6971       /* If the sign bit is known to be zero, replace this with an
6972          arithmetic shift.  */
6973       if (have_insn_for (ASHIFTRT, mode)
6974           && ! have_insn_for (LSHIFTRT, mode)
6975           && mode_width <= HOST_BITS_PER_WIDE_INT
6976           && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6977         {
6978           new = gen_rtx_ASHIFTRT (mode,
6979                                   make_compound_operation (XEXP (x, 0),
6980                                                            next_code),
6981                                   XEXP (x, 1));
6982           break;
6983         }
6984
6985       /* ... fall through ...  */
6986
6987     case ASHIFTRT:
6988       lhs = XEXP (x, 0);
6989       rhs = XEXP (x, 1);
6990
6991       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6992          this is a SIGN_EXTRACT.  */
6993       if (GET_CODE (rhs) == CONST_INT
6994           && GET_CODE (lhs) == ASHIFT
6995           && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6996           && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6997         {
6998           new = make_compound_operation (XEXP (lhs, 0), next_code);
6999           new = make_extraction (mode, new,
7000                                  INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7001                                  NULL_RTX, mode_width - INTVAL (rhs),
7002                                  code == LSHIFTRT, 0, in_code == COMPARE);
7003           break;
7004         }
7005
7006       /* See if we have operations between an ASHIFTRT and an ASHIFT.
7007          If so, try to merge the shifts into a SIGN_EXTEND.  We could
7008          also do this for some cases of SIGN_EXTRACT, but it doesn't
7009          seem worth the effort; the case checked for occurs on Alpha.  */
7010
7011       if (!OBJECT_P (lhs)
7012           && ! (GET_CODE (lhs) == SUBREG
7013                 && (OBJECT_P (SUBREG_REG (lhs))))
7014           && GET_CODE (rhs) == CONST_INT
7015           && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7016           && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7017         new = make_extraction (mode, make_compound_operation (new, next_code),
7018                                0, NULL_RTX, mode_width - INTVAL (rhs),
7019                                code == LSHIFTRT, 0, in_code == COMPARE);
7020
7021       break;
7022
7023     case SUBREG:
7024       /* Call ourselves recursively on the inner expression.  If we are
7025          narrowing the object and it has a different RTL code from
7026          what it originally did, do this SUBREG as a force_to_mode.  */
7027
7028       tem = make_compound_operation (SUBREG_REG (x), in_code);
7029
7030       {
7031         rtx simplified;
7032         simplified = simplify_subreg (GET_MODE (x), tem, GET_MODE (tem),
7033                                       SUBREG_BYTE (x));
7034
7035         if (simplified)
7036           tem = simplified;
7037
7038         if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
7039             && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
7040             && subreg_lowpart_p (x))
7041           {
7042             rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
7043                                        0);
7044
7045             /* If we have something other than a SUBREG, we might have
7046                done an expansion, so rerun ourselves.  */
7047             if (GET_CODE (newer) != SUBREG)
7048               newer = make_compound_operation (newer, in_code);
7049
7050             return newer;
7051           }
7052
7053         if (simplified)
7054           return tem;
7055       }
7056       break;
7057
7058     default:
7059       break;
7060     }
7061
7062   if (new)
7063     {
7064       x = gen_lowpart (mode, new);
7065       code = GET_CODE (x);
7066     }
7067
7068   /* Now recursively process each operand of this operation.  */
7069   fmt = GET_RTX_FORMAT (code);
7070   for (i = 0; i < GET_RTX_LENGTH (code); i++)
7071     if (fmt[i] == 'e')
7072       {
7073         new = make_compound_operation (XEXP (x, i), next_code);
7074         SUBST (XEXP (x, i), new);
7075       }
7076
7077   /* If this is a commutative operation, the changes to the operands
7078      may have made it noncanonical.  */
7079   if (COMMUTATIVE_ARITH_P (x)
7080       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7081     {
7082       tem = XEXP (x, 0);
7083       SUBST (XEXP (x, 0), XEXP (x, 1));
7084       SUBST (XEXP (x, 1), tem);
7085     }
7086
7087   return x;
7088 }
7089 \f
7090 /* Given M see if it is a value that would select a field of bits
7091    within an item, but not the entire word.  Return -1 if not.
7092    Otherwise, return the starting position of the field, where 0 is the
7093    low-order bit.
7094
7095    *PLEN is set to the length of the field.  */
7096
7097 static int
7098 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7099 {
7100   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
7101   int pos = exact_log2 (m & -m);
7102   int len = 0;
7103
7104   if (pos >= 0)
7105     /* Now shift off the low-order zero bits and see if we have a
7106        power of two minus 1.  */
7107     len = exact_log2 ((m >> pos) + 1);
7108
7109   if (len <= 0)
7110     pos = -1;
7111
7112   *plen = len;
7113   return pos;
7114 }
7115 \f
7116 /* If X refers to a register that equals REG in value, replace these
7117    references with REG.  */
7118 static rtx
7119 canon_reg_for_combine (rtx x, rtx reg)
7120 {
7121   rtx op0, op1, op2;
7122   const char *fmt;
7123   int i;
7124   bool copied;
7125
7126   enum rtx_code code = GET_CODE (x);
7127   switch (GET_RTX_CLASS (code))
7128     {
7129     case RTX_UNARY:
7130       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7131       if (op0 != XEXP (x, 0))
7132         return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7133                                    GET_MODE (reg));
7134       break;
7135
7136     case RTX_BIN_ARITH:
7137     case RTX_COMM_ARITH:
7138       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7139       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7140       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7141         return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7142       break;
7143
7144     case RTX_COMPARE:
7145     case RTX_COMM_COMPARE:
7146       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7147       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7148       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7149         return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7150                                         GET_MODE (op0), op0, op1);
7151       break;
7152
7153     case RTX_TERNARY:
7154     case RTX_BITFIELD_OPS:
7155       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7156       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7157       op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7158       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7159         return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7160                                      GET_MODE (op0), op0, op1, op2);
7161
7162     case RTX_OBJ:
7163       if (REG_P (x))
7164         {
7165           if (rtx_equal_p (get_last_value (reg), x)
7166               || rtx_equal_p (reg, get_last_value (x)))
7167             return reg;
7168           else
7169             break;
7170         }
7171
7172       /* fall through */
7173
7174     default:
7175       fmt = GET_RTX_FORMAT (code);
7176       copied = false;
7177       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7178         if (fmt[i] == 'e')
7179           {
7180             rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7181             if (op != XEXP (x, i))
7182               {
7183                 if (!copied)
7184                   {
7185                     copied = true;
7186                     x = copy_rtx (x);
7187                   }
7188                 XEXP (x, i) = op;
7189               }
7190           }
7191         else if (fmt[i] == 'E')
7192           {
7193             int j;
7194             for (j = 0; j < XVECLEN (x, i); j++)
7195               {
7196                 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
7197                 if (op != XVECEXP (x, i, j))
7198                   {
7199                     if (!copied)
7200                       {
7201                         copied = true;
7202                         x = copy_rtx (x);
7203                       }
7204                     XVECEXP (x, i, j) = op;
7205                   }
7206               }
7207           }
7208
7209       break;
7210     }
7211
7212   return x;
7213 }
7214
7215 /* Return X converted to MODE.  If the value is already truncated to
7216    MODE we can just return a subreg even though in the general case we
7217    would need an explicit truncation.  */
7218
7219 static rtx
7220 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
7221 {
7222   if (GET_MODE_SIZE (GET_MODE (x)) <= GET_MODE_SIZE (mode)
7223       || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
7224                                 GET_MODE_BITSIZE (GET_MODE (x)))
7225       || (REG_P (x) && reg_truncated_to_mode (mode, x)))
7226     return gen_lowpart (mode, x);
7227   else
7228     return simplify_gen_unary (TRUNCATE, mode, x, GET_MODE (x));
7229 }
7230
7231 /* See if X can be simplified knowing that we will only refer to it in
7232    MODE and will only refer to those bits that are nonzero in MASK.
7233    If other bits are being computed or if masking operations are done
7234    that select a superset of the bits in MASK, they can sometimes be
7235    ignored.
7236
7237    Return a possibly simplified expression, but always convert X to
7238    MODE.  If X is a CONST_INT, AND the CONST_INT with MASK.
7239
7240    If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
7241    are all off in X.  This is used when X will be complemented, by either
7242    NOT, NEG, or XOR.  */
7243
7244 static rtx
7245 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
7246                int just_select)
7247 {
7248   enum rtx_code code = GET_CODE (x);
7249   int next_select = just_select || code == XOR || code == NOT || code == NEG;
7250   enum machine_mode op_mode;
7251   unsigned HOST_WIDE_INT fuller_mask, nonzero;
7252   rtx op0, op1, temp;
7253
7254   /* If this is a CALL or ASM_OPERANDS, don't do anything.  Some of the
7255      code below will do the wrong thing since the mode of such an
7256      expression is VOIDmode.
7257
7258      Also do nothing if X is a CLOBBER; this can happen if X was
7259      the return value from a call to gen_lowpart.  */
7260   if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
7261     return x;
7262
7263   /* We want to perform the operation is its present mode unless we know
7264      that the operation is valid in MODE, in which case we do the operation
7265      in MODE.  */
7266   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
7267               && have_insn_for (code, mode))
7268              ? mode : GET_MODE (x));
7269
7270   /* It is not valid to do a right-shift in a narrower mode
7271      than the one it came in with.  */
7272   if ((code == LSHIFTRT || code == ASHIFTRT)
7273       && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
7274     op_mode = GET_MODE (x);
7275
7276   /* Truncate MASK to fit OP_MODE.  */
7277   if (op_mode)
7278     mask &= GET_MODE_MASK (op_mode);
7279
7280   /* When we have an arithmetic operation, or a shift whose count we
7281      do not know, we need to assume that all bits up to the highest-order
7282      bit in MASK will be needed.  This is how we form such a mask.  */
7283   if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
7284     fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
7285   else
7286     fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
7287                    - 1);
7288
7289   /* Determine what bits of X are guaranteed to be (non)zero.  */
7290   nonzero = nonzero_bits (x, mode);
7291
7292   /* If none of the bits in X are needed, return a zero.  */
7293   if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
7294     x = const0_rtx;
7295
7296   /* If X is a CONST_INT, return a new one.  Do this here since the
7297      test below will fail.  */
7298   if (GET_CODE (x) == CONST_INT)
7299     {
7300       if (SCALAR_INT_MODE_P (mode))
7301         return gen_int_mode (INTVAL (x) & mask, mode);
7302       else
7303         {
7304           x = GEN_INT (INTVAL (x) & mask);
7305           return gen_lowpart_common (mode, x);
7306         }
7307     }
7308
7309   /* If X is narrower than MODE and we want all the bits in X's mode, just
7310      get X in the proper mode.  */
7311   if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
7312       && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
7313     return gen_lowpart (mode, x);
7314
7315   switch (code)
7316     {
7317     case CLOBBER:
7318       /* If X is a (clobber (const_int)), return it since we know we are
7319          generating something that won't match.  */
7320       return x;
7321
7322     case SIGN_EXTEND:
7323     case ZERO_EXTEND:
7324     case ZERO_EXTRACT:
7325     case SIGN_EXTRACT:
7326       x = expand_compound_operation (x);
7327       if (GET_CODE (x) != code)
7328         return force_to_mode (x, mode, mask, next_select);
7329       break;
7330
7331     case SUBREG:
7332       if (subreg_lowpart_p (x)
7333           /* We can ignore the effect of this SUBREG if it narrows the mode or
7334              if the constant masks to zero all the bits the mode doesn't
7335              have.  */
7336           && ((GET_MODE_SIZE (GET_MODE (x))
7337                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7338               || (0 == (mask
7339                         & GET_MODE_MASK (GET_MODE (x))
7340                         & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
7341         return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
7342       break;
7343
7344     case AND:
7345       /* If this is an AND with a constant, convert it into an AND
7346          whose constant is the AND of that constant with MASK.  If it
7347          remains an AND of MASK, delete it since it is redundant.  */
7348
7349       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7350         {
7351           x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
7352                                       mask & INTVAL (XEXP (x, 1)));
7353
7354           /* If X is still an AND, see if it is an AND with a mask that
7355              is just some low-order bits.  If so, and it is MASK, we don't
7356              need it.  */
7357
7358           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
7359               && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
7360                   == mask))
7361             x = XEXP (x, 0);
7362
7363           /* If it remains an AND, try making another AND with the bits
7364              in the mode mask that aren't in MASK turned on.  If the
7365              constant in the AND is wide enough, this might make a
7366              cheaper constant.  */
7367
7368           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
7369               && GET_MODE_MASK (GET_MODE (x)) != mask
7370               && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
7371             {
7372               HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
7373                                     | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
7374               int width = GET_MODE_BITSIZE (GET_MODE (x));
7375               rtx y;
7376
7377               /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
7378                  number, sign extend it.  */
7379               if (width > 0 && width < HOST_BITS_PER_WIDE_INT
7380                   && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7381                 cval |= (HOST_WIDE_INT) -1 << width;
7382
7383               y = simplify_gen_binary (AND, GET_MODE (x),
7384                                        XEXP (x, 0), GEN_INT (cval));
7385               if (rtx_cost (y, SET) < rtx_cost (x, SET))
7386                 x = y;
7387             }
7388
7389           break;
7390         }
7391
7392       goto binop;
7393
7394     case PLUS:
7395       /* In (and (plus FOO C1) M), if M is a mask that just turns off
7396          low-order bits (as in an alignment operation) and FOO is already
7397          aligned to that boundary, mask C1 to that boundary as well.
7398          This may eliminate that PLUS and, later, the AND.  */
7399
7400       {
7401         unsigned int width = GET_MODE_BITSIZE (mode);
7402         unsigned HOST_WIDE_INT smask = mask;
7403
7404         /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
7405            number, sign extend it.  */
7406
7407         if (width < HOST_BITS_PER_WIDE_INT
7408             && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7409           smask |= (HOST_WIDE_INT) -1 << width;
7410
7411         if (GET_CODE (XEXP (x, 1)) == CONST_INT
7412             && exact_log2 (- smask) >= 0
7413             && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
7414             && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
7415           return force_to_mode (plus_constant (XEXP (x, 0),
7416                                                (INTVAL (XEXP (x, 1)) & smask)),
7417                                 mode, smask, next_select);
7418       }
7419
7420       /* ... fall through ...  */
7421
7422     case MULT:
7423       /* For PLUS, MINUS and MULT, we need any bits less significant than the
7424          most significant bit in MASK since carries from those bits will
7425          affect the bits we are interested in.  */
7426       mask = fuller_mask;
7427       goto binop;
7428
7429     case MINUS:
7430       /* If X is (minus C Y) where C's least set bit is larger than any bit
7431          in the mask, then we may replace with (neg Y).  */
7432       if (GET_CODE (XEXP (x, 0)) == CONST_INT
7433           && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
7434                                         & -INTVAL (XEXP (x, 0))))
7435               > mask))
7436         {
7437           x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
7438                                   GET_MODE (x));
7439           return force_to_mode (x, mode, mask, next_select);
7440         }
7441
7442       /* Similarly, if C contains every bit in the fuller_mask, then we may
7443          replace with (not Y).  */
7444       if (GET_CODE (XEXP (x, 0)) == CONST_INT
7445           && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
7446               == INTVAL (XEXP (x, 0))))
7447         {
7448           x = simplify_gen_unary (NOT, GET_MODE (x),
7449                                   XEXP (x, 1), GET_MODE (x));
7450           return force_to_mode (x, mode, mask, next_select);
7451         }
7452
7453       mask = fuller_mask;
7454       goto binop;
7455
7456     case IOR:
7457     case XOR:
7458       /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
7459          LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
7460          operation which may be a bitfield extraction.  Ensure that the
7461          constant we form is not wider than the mode of X.  */
7462
7463       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7464           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7465           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7466           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7467           && GET_CODE (XEXP (x, 1)) == CONST_INT
7468           && ((INTVAL (XEXP (XEXP (x, 0), 1))
7469                + floor_log2 (INTVAL (XEXP (x, 1))))
7470               < GET_MODE_BITSIZE (GET_MODE (x)))
7471           && (INTVAL (XEXP (x, 1))
7472               & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
7473         {
7474           temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
7475                           << INTVAL (XEXP (XEXP (x, 0), 1)));
7476           temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
7477                                       XEXP (XEXP (x, 0), 0), temp);
7478           x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
7479                                    XEXP (XEXP (x, 0), 1));
7480           return force_to_mode (x, mode, mask, next_select);
7481         }
7482
7483     binop:
7484       /* For most binary operations, just propagate into the operation and
7485          change the mode if we have an operation of that mode.  */
7486
7487       op0 = gen_lowpart_or_truncate (op_mode,
7488                                      force_to_mode (XEXP (x, 0), mode, mask,
7489                                                     next_select));
7490       op1 = gen_lowpart_or_truncate (op_mode,
7491                                      force_to_mode (XEXP (x, 1), mode, mask,
7492                                         next_select));
7493
7494       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7495         x = simplify_gen_binary (code, op_mode, op0, op1);
7496       break;
7497
7498     case ASHIFT:
7499       /* For left shifts, do the same, but just for the first operand.
7500          However, we cannot do anything with shifts where we cannot
7501          guarantee that the counts are smaller than the size of the mode
7502          because such a count will have a different meaning in a
7503          wider mode.  */
7504
7505       if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
7506              && INTVAL (XEXP (x, 1)) >= 0
7507              && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7508           && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7509                 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7510                     < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7511         break;
7512
7513       /* If the shift count is a constant and we can do arithmetic in
7514          the mode of the shift, refine which bits we need.  Otherwise, use the
7515          conservative form of the mask.  */
7516       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7517           && INTVAL (XEXP (x, 1)) >= 0
7518           && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7519           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7520         mask >>= INTVAL (XEXP (x, 1));
7521       else
7522         mask = fuller_mask;
7523
7524       op0 = gen_lowpart_or_truncate (op_mode,
7525                                      force_to_mode (XEXP (x, 0), op_mode,
7526                                                     mask, next_select));
7527
7528       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7529         x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
7530       break;
7531
7532     case LSHIFTRT:
7533       /* Here we can only do something if the shift count is a constant,
7534          this shift constant is valid for the host, and we can do arithmetic
7535          in OP_MODE.  */
7536
7537       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7538           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7539           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7540         {
7541           rtx inner = XEXP (x, 0);
7542           unsigned HOST_WIDE_INT inner_mask;
7543
7544           /* Select the mask of the bits we need for the shift operand.  */
7545           inner_mask = mask << INTVAL (XEXP (x, 1));
7546
7547           /* We can only change the mode of the shift if we can do arithmetic
7548              in the mode of the shift and INNER_MASK is no wider than the
7549              width of X's mode.  */
7550           if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
7551             op_mode = GET_MODE (x);
7552
7553           inner = force_to_mode (inner, op_mode, inner_mask, next_select);
7554
7555           if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7556             x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7557         }
7558
7559       /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7560          shift and AND produces only copies of the sign bit (C2 is one less
7561          than a power of two), we can do this with just a shift.  */
7562
7563       if (GET_CODE (x) == LSHIFTRT
7564           && GET_CODE (XEXP (x, 1)) == CONST_INT
7565           /* The shift puts one of the sign bit copies in the least significant
7566              bit.  */
7567           && ((INTVAL (XEXP (x, 1))
7568                + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7569               >= GET_MODE_BITSIZE (GET_MODE (x)))
7570           && exact_log2 (mask + 1) >= 0
7571           /* Number of bits left after the shift must be more than the mask
7572              needs.  */
7573           && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7574               <= GET_MODE_BITSIZE (GET_MODE (x)))
7575           /* Must be more sign bit copies than the mask needs.  */
7576           && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7577               >= exact_log2 (mask + 1)))
7578         x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7579                                  GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7580                                           - exact_log2 (mask + 1)));
7581
7582       goto shiftrt;
7583
7584     case ASHIFTRT:
7585       /* If we are just looking for the sign bit, we don't need this shift at
7586          all, even if it has a variable count.  */
7587       if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7588           && (mask == ((unsigned HOST_WIDE_INT) 1
7589                        << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7590         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7591
7592       /* If this is a shift by a constant, get a mask that contains those bits
7593          that are not copies of the sign bit.  We then have two cases:  If
7594          MASK only includes those bits, this can be a logical shift, which may
7595          allow simplifications.  If MASK is a single-bit field not within
7596          those bits, we are requesting a copy of the sign bit and hence can
7597          shift the sign bit to the appropriate location.  */
7598
7599       if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7600           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7601         {
7602           int i;
7603
7604           /* If the considered data is wider than HOST_WIDE_INT, we can't
7605              represent a mask for all its bits in a single scalar.
7606              But we only care about the lower bits, so calculate these.  */
7607
7608           if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7609             {
7610               nonzero = ~(HOST_WIDE_INT) 0;
7611
7612               /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7613                  is the number of bits a full-width mask would have set.
7614                  We need only shift if these are fewer than nonzero can
7615                  hold.  If not, we must keep all bits set in nonzero.  */
7616
7617               if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7618                   < HOST_BITS_PER_WIDE_INT)
7619                 nonzero >>= INTVAL (XEXP (x, 1))
7620                             + HOST_BITS_PER_WIDE_INT
7621                             - GET_MODE_BITSIZE (GET_MODE (x)) ;
7622             }
7623           else
7624             {
7625               nonzero = GET_MODE_MASK (GET_MODE (x));
7626               nonzero >>= INTVAL (XEXP (x, 1));
7627             }
7628
7629           if ((mask & ~nonzero) == 0)
7630             {
7631               x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
7632                                         XEXP (x, 0), INTVAL (XEXP (x, 1)));
7633               if (GET_CODE (x) != ASHIFTRT)
7634                 return force_to_mode (x, mode, mask, next_select);
7635             }
7636
7637           else if ((i = exact_log2 (mask)) >= 0)
7638             {
7639               x = simplify_shift_const
7640                   (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7641                    GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7642
7643               if (GET_CODE (x) != ASHIFTRT)
7644                 return force_to_mode (x, mode, mask, next_select);
7645             }
7646         }
7647
7648       /* If MASK is 1, convert this to an LSHIFTRT.  This can be done
7649          even if the shift count isn't a constant.  */
7650       if (mask == 1)
7651         x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7652                                  XEXP (x, 0), XEXP (x, 1));
7653
7654     shiftrt:
7655
7656       /* If this is a zero- or sign-extension operation that just affects bits
7657          we don't care about, remove it.  Be sure the call above returned
7658          something that is still a shift.  */
7659
7660       if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7661           && GET_CODE (XEXP (x, 1)) == CONST_INT
7662           && INTVAL (XEXP (x, 1)) >= 0
7663           && (INTVAL (XEXP (x, 1))
7664               <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7665           && GET_CODE (XEXP (x, 0)) == ASHIFT
7666           && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7667         return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7668                               next_select);
7669
7670       break;
7671
7672     case ROTATE:
7673     case ROTATERT:
7674       /* If the shift count is constant and we can do computations
7675          in the mode of X, compute where the bits we care about are.
7676          Otherwise, we can't do anything.  Don't change the mode of
7677          the shift or propagate MODE into the shift, though.  */
7678       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7679           && INTVAL (XEXP (x, 1)) >= 0)
7680         {
7681           temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7682                                             GET_MODE (x), GEN_INT (mask),
7683                                             XEXP (x, 1));
7684           if (temp && GET_CODE (temp) == CONST_INT)
7685             SUBST (XEXP (x, 0),
7686                    force_to_mode (XEXP (x, 0), GET_MODE (x),
7687                                   INTVAL (temp), next_select));
7688         }
7689       break;
7690
7691     case NEG:
7692       /* If we just want the low-order bit, the NEG isn't needed since it
7693          won't change the low-order bit.  */
7694       if (mask == 1)
7695         return force_to_mode (XEXP (x, 0), mode, mask, just_select);
7696
7697       /* We need any bits less significant than the most significant bit in
7698          MASK since carries from those bits will affect the bits we are
7699          interested in.  */
7700       mask = fuller_mask;
7701       goto unop;
7702
7703     case NOT:
7704       /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7705          same as the XOR case above.  Ensure that the constant we form is not
7706          wider than the mode of X.  */
7707
7708       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7709           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7710           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7711           && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7712               < GET_MODE_BITSIZE (GET_MODE (x)))
7713           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7714         {
7715           temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7716                                GET_MODE (x));
7717           temp = simplify_gen_binary (XOR, GET_MODE (x),
7718                                       XEXP (XEXP (x, 0), 0), temp);
7719           x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7720                                    temp, XEXP (XEXP (x, 0), 1));
7721
7722           return force_to_mode (x, mode, mask, next_select);
7723         }
7724
7725       /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7726          use the full mask inside the NOT.  */
7727       mask = fuller_mask;
7728
7729     unop:
7730       op0 = gen_lowpart_or_truncate (op_mode,
7731                                      force_to_mode (XEXP (x, 0), mode, mask,
7732                                                     next_select));
7733       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7734         x = simplify_gen_unary (code, op_mode, op0, op_mode);
7735       break;
7736
7737     case NE:
7738       /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7739          in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7740          which is equal to STORE_FLAG_VALUE.  */
7741       if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7742           && GET_MODE (XEXP (x, 0)) == mode
7743           && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7744           && (nonzero_bits (XEXP (x, 0), mode)
7745               == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7746         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7747
7748       break;
7749
7750     case IF_THEN_ELSE:
7751       /* We have no way of knowing if the IF_THEN_ELSE can itself be
7752          written in a narrower mode.  We play it safe and do not do so.  */
7753
7754       SUBST (XEXP (x, 1),
7755              gen_lowpart_or_truncate (GET_MODE (x),
7756                                       force_to_mode (XEXP (x, 1), mode,
7757                                                      mask, next_select)));
7758       SUBST (XEXP (x, 2),
7759              gen_lowpart_or_truncate (GET_MODE (x),
7760                                       force_to_mode (XEXP (x, 2), mode,
7761                                                      mask, next_select)));
7762       break;
7763
7764     default:
7765       break;
7766     }
7767
7768   /* Ensure we return a value of the proper mode.  */
7769   return gen_lowpart_or_truncate (mode, x);
7770 }
7771 \f
7772 /* Return nonzero if X is an expression that has one of two values depending on
7773    whether some other value is zero or nonzero.  In that case, we return the
7774    value that is being tested, *PTRUE is set to the value if the rtx being
7775    returned has a nonzero value, and *PFALSE is set to the other alternative.
7776
7777    If we return zero, we set *PTRUE and *PFALSE to X.  */
7778
7779 static rtx
7780 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
7781 {
7782   enum machine_mode mode = GET_MODE (x);
7783   enum rtx_code code = GET_CODE (x);
7784   rtx cond0, cond1, true0, true1, false0, false1;
7785   unsigned HOST_WIDE_INT nz;
7786
7787   /* If we are comparing a value against zero, we are done.  */
7788   if ((code == NE || code == EQ)
7789       && XEXP (x, 1) == const0_rtx)
7790     {
7791       *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7792       *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7793       return XEXP (x, 0);
7794     }
7795
7796   /* If this is a unary operation whose operand has one of two values, apply
7797      our opcode to compute those values.  */
7798   else if (UNARY_P (x)
7799            && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7800     {
7801       *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7802       *pfalse = simplify_gen_unary (code, mode, false0,
7803                                     GET_MODE (XEXP (x, 0)));
7804       return cond0;
7805     }
7806
7807   /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7808      make can't possibly match and would suppress other optimizations.  */
7809   else if (code == COMPARE)
7810     ;
7811
7812   /* If this is a binary operation, see if either side has only one of two
7813      values.  If either one does or if both do and they are conditional on
7814      the same value, compute the new true and false values.  */
7815   else if (BINARY_P (x))
7816     {
7817       cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7818       cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7819
7820       if ((cond0 != 0 || cond1 != 0)
7821           && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7822         {
7823           /* If if_then_else_cond returned zero, then true/false are the
7824              same rtl.  We must copy one of them to prevent invalid rtl
7825              sharing.  */
7826           if (cond0 == 0)
7827             true0 = copy_rtx (true0);
7828           else if (cond1 == 0)
7829             true1 = copy_rtx (true1);
7830
7831           if (COMPARISON_P (x))
7832             {
7833               *ptrue = simplify_gen_relational (code, mode, VOIDmode,
7834                                                 true0, true1);
7835               *pfalse = simplify_gen_relational (code, mode, VOIDmode,
7836                                                  false0, false1);
7837              }
7838           else
7839             {
7840               *ptrue = simplify_gen_binary (code, mode, true0, true1);
7841               *pfalse = simplify_gen_binary (code, mode, false0, false1);
7842             }
7843
7844           return cond0 ? cond0 : cond1;
7845         }
7846
7847       /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7848          operands is zero when the other is nonzero, and vice-versa,
7849          and STORE_FLAG_VALUE is 1 or -1.  */
7850
7851       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7852           && (code == PLUS || code == IOR || code == XOR || code == MINUS
7853               || code == UMAX)
7854           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7855         {
7856           rtx op0 = XEXP (XEXP (x, 0), 1);
7857           rtx op1 = XEXP (XEXP (x, 1), 1);
7858
7859           cond0 = XEXP (XEXP (x, 0), 0);
7860           cond1 = XEXP (XEXP (x, 1), 0);
7861
7862           if (COMPARISON_P (cond0)
7863               && COMPARISON_P (cond1)
7864               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7865                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7866                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7867                   || ((swap_condition (GET_CODE (cond0))
7868                        == reversed_comparison_code (cond1, NULL))
7869                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7870                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7871               && ! side_effects_p (x))
7872             {
7873               *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
7874               *pfalse = simplify_gen_binary (MULT, mode,
7875                                              (code == MINUS
7876                                               ? simplify_gen_unary (NEG, mode,
7877                                                                     op1, mode)
7878                                               : op1),
7879                                               const_true_rtx);
7880               return cond0;
7881             }
7882         }
7883
7884       /* Similarly for MULT, AND and UMIN, except that for these the result
7885          is always zero.  */
7886       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7887           && (code == MULT || code == AND || code == UMIN)
7888           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7889         {
7890           cond0 = XEXP (XEXP (x, 0), 0);
7891           cond1 = XEXP (XEXP (x, 1), 0);
7892
7893           if (COMPARISON_P (cond0)
7894               && COMPARISON_P (cond1)
7895               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7896                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7897                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7898                   || ((swap_condition (GET_CODE (cond0))
7899                        == reversed_comparison_code (cond1, NULL))
7900                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7901                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7902               && ! side_effects_p (x))
7903             {
7904               *ptrue = *pfalse = const0_rtx;
7905               return cond0;
7906             }
7907         }
7908     }
7909
7910   else if (code == IF_THEN_ELSE)
7911     {
7912       /* If we have IF_THEN_ELSE already, extract the condition and
7913          canonicalize it if it is NE or EQ.  */
7914       cond0 = XEXP (x, 0);
7915       *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7916       if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7917         return XEXP (cond0, 0);
7918       else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7919         {
7920           *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7921           return XEXP (cond0, 0);
7922         }
7923       else
7924         return cond0;
7925     }
7926
7927   /* If X is a SUBREG, we can narrow both the true and false values
7928      if the inner expression, if there is a condition.  */
7929   else if (code == SUBREG
7930            && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7931                                                &true0, &false0)))
7932     {
7933       true0 = simplify_gen_subreg (mode, true0,
7934                                    GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7935       false0 = simplify_gen_subreg (mode, false0,
7936                                     GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7937       if (true0 && false0)
7938         {
7939           *ptrue = true0;
7940           *pfalse = false0;
7941           return cond0;
7942         }
7943     }
7944
7945   /* If X is a constant, this isn't special and will cause confusions
7946      if we treat it as such.  Likewise if it is equivalent to a constant.  */
7947   else if (CONSTANT_P (x)
7948            || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7949     ;
7950
7951   /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7952      will be least confusing to the rest of the compiler.  */
7953   else if (mode == BImode)
7954     {
7955       *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7956       return x;
7957     }
7958
7959   /* If X is known to be either 0 or -1, those are the true and
7960      false values when testing X.  */
7961   else if (x == constm1_rtx || x == const0_rtx
7962            || (mode != VOIDmode
7963                && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7964     {
7965       *ptrue = constm1_rtx, *pfalse = const0_rtx;
7966       return x;
7967     }
7968
7969   /* Likewise for 0 or a single bit.  */
7970   else if (SCALAR_INT_MODE_P (mode)
7971            && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7972            && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7973     {
7974       *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7975       return x;
7976     }
7977
7978   /* Otherwise fail; show no condition with true and false values the same.  */
7979   *ptrue = *pfalse = x;
7980   return 0;
7981 }
7982 \f
7983 /* Return the value of expression X given the fact that condition COND
7984    is known to be true when applied to REG as its first operand and VAL
7985    as its second.  X is known to not be shared and so can be modified in
7986    place.
7987
7988    We only handle the simplest cases, and specifically those cases that
7989    arise with IF_THEN_ELSE expressions.  */
7990
7991 static rtx
7992 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
7993 {
7994   enum rtx_code code = GET_CODE (x);
7995   rtx temp;
7996   const char *fmt;
7997   int i, j;
7998
7999   if (side_effects_p (x))
8000     return x;
8001
8002   /* If either operand of the condition is a floating point value,
8003      then we have to avoid collapsing an EQ comparison.  */
8004   if (cond == EQ
8005       && rtx_equal_p (x, reg)
8006       && ! FLOAT_MODE_P (GET_MODE (x))
8007       && ! FLOAT_MODE_P (GET_MODE (val)))
8008     return val;
8009
8010   if (cond == UNEQ && rtx_equal_p (x, reg))
8011     return val;
8012
8013   /* If X is (abs REG) and we know something about REG's relationship
8014      with zero, we may be able to simplify this.  */
8015
8016   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8017     switch (cond)
8018       {
8019       case GE:  case GT:  case EQ:
8020         return XEXP (x, 0);
8021       case LT:  case LE:
8022         return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8023                                    XEXP (x, 0),
8024                                    GET_MODE (XEXP (x, 0)));
8025       default:
8026         break;
8027       }
8028
8029   /* The only other cases we handle are MIN, MAX, and comparisons if the
8030      operands are the same as REG and VAL.  */
8031
8032   else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8033     {
8034       if (rtx_equal_p (XEXP (x, 0), val))
8035         cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8036
8037       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8038         {
8039           if (COMPARISON_P (x))
8040             {
8041               if (comparison_dominates_p (cond, code))
8042                 return const_true_rtx;
8043
8044               code = reversed_comparison_code (x, NULL);
8045               if (code != UNKNOWN
8046                   && comparison_dominates_p (cond, code))
8047                 return const0_rtx;
8048               else
8049                 return x;
8050             }
8051           else if (code == SMAX || code == SMIN
8052                    || code == UMIN || code == UMAX)
8053             {
8054               int unsignedp = (code == UMIN || code == UMAX);
8055
8056               /* Do not reverse the condition when it is NE or EQ.
8057                  This is because we cannot conclude anything about
8058                  the value of 'SMAX (x, y)' when x is not equal to y,
8059                  but we can when x equals y.  */
8060               if ((code == SMAX || code == UMAX)
8061                   && ! (cond == EQ || cond == NE))
8062                 cond = reverse_condition (cond);
8063
8064               switch (cond)
8065                 {
8066                 case GE:   case GT:
8067                   return unsignedp ? x : XEXP (x, 1);
8068                 case LE:   case LT:
8069                   return unsignedp ? x : XEXP (x, 0);
8070                 case GEU:  case GTU:
8071                   return unsignedp ? XEXP (x, 1) : x;
8072                 case LEU:  case LTU:
8073                   return unsignedp ? XEXP (x, 0) : x;
8074                 default:
8075                   break;
8076                 }
8077             }
8078         }
8079     }
8080   else if (code == SUBREG)
8081     {
8082       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8083       rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
8084
8085       if (SUBREG_REG (x) != r)
8086         {
8087           /* We must simplify subreg here, before we lose track of the
8088              original inner_mode.  */
8089           new = simplify_subreg (GET_MODE (x), r,
8090                                  inner_mode, SUBREG_BYTE (x));
8091           if (new)
8092             return new;
8093           else
8094             SUBST (SUBREG_REG (x), r);
8095         }
8096
8097       return x;
8098     }
8099   /* We don't have to handle SIGN_EXTEND here, because even in the
8100      case of replacing something with a modeless CONST_INT, a
8101      CONST_INT is already (supposed to be) a valid sign extension for
8102      its narrower mode, which implies it's already properly
8103      sign-extended for the wider mode.  Now, for ZERO_EXTEND, the
8104      story is different.  */
8105   else if (code == ZERO_EXTEND)
8106     {
8107       enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8108       rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
8109
8110       if (XEXP (x, 0) != r)
8111         {
8112           /* We must simplify the zero_extend here, before we lose
8113              track of the original inner_mode.  */
8114           new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8115                                           r, inner_mode);
8116           if (new)
8117             return new;
8118           else
8119             SUBST (XEXP (x, 0), r);
8120         }
8121
8122       return x;
8123     }
8124
8125   fmt = GET_RTX_FORMAT (code);
8126   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8127     {
8128       if (fmt[i] == 'e')
8129         SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8130       else if (fmt[i] == 'E')
8131         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8132           SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8133                                                 cond, reg, val));
8134     }
8135
8136   return x;
8137 }
8138 \f
8139 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8140    assignment as a field assignment.  */
8141
8142 static int
8143 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8144 {
8145   if (x == y || rtx_equal_p (x, y))
8146     return 1;
8147
8148   if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8149     return 0;
8150
8151   /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8152      Note that all SUBREGs of MEM are paradoxical; otherwise they
8153      would have been rewritten.  */
8154   if (MEM_P (x) && GET_CODE (y) == SUBREG
8155       && MEM_P (SUBREG_REG (y))
8156       && rtx_equal_p (SUBREG_REG (y),
8157                       gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8158     return 1;
8159
8160   if (MEM_P (y) && GET_CODE (x) == SUBREG
8161       && MEM_P (SUBREG_REG (x))
8162       && rtx_equal_p (SUBREG_REG (x),
8163                       gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8164     return 1;
8165
8166   /* We used to see if get_last_value of X and Y were the same but that's
8167      not correct.  In one direction, we'll cause the assignment to have
8168      the wrong destination and in the case, we'll import a register into this
8169      insn that might have already have been dead.   So fail if none of the
8170      above cases are true.  */
8171   return 0;
8172 }
8173 \f
8174 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8175    Return that assignment if so.
8176
8177    We only handle the most common cases.  */
8178
8179 static rtx
8180 make_field_assignment (rtx x)
8181 {
8182   rtx dest = SET_DEST (x);
8183   rtx src = SET_SRC (x);
8184   rtx assign;
8185   rtx rhs, lhs;
8186   HOST_WIDE_INT c1;
8187   HOST_WIDE_INT pos;
8188   unsigned HOST_WIDE_INT len;
8189   rtx other;
8190   enum machine_mode mode;
8191
8192   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
8193      a clear of a one-bit field.  We will have changed it to
8194      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
8195      for a SUBREG.  */
8196
8197   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
8198       && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
8199       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
8200       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8201     {
8202       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8203                                 1, 1, 1, 0);
8204       if (assign != 0)
8205         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8206       return x;
8207     }
8208
8209   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
8210       && subreg_lowpart_p (XEXP (src, 0))
8211       && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
8212           < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
8213       && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
8214       && GET_CODE (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == CONST_INT
8215       && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
8216       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8217     {
8218       assign = make_extraction (VOIDmode, dest, 0,
8219                                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
8220                                 1, 1, 1, 0);
8221       if (assign != 0)
8222         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8223       return x;
8224     }
8225
8226   /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
8227      one-bit field.  */
8228   if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
8229       && XEXP (XEXP (src, 0), 0) == const1_rtx
8230       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8231     {
8232       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8233                                 1, 1, 1, 0);
8234       if (assign != 0)
8235         return gen_rtx_SET (VOIDmode, assign, const1_rtx);
8236       return x;
8237     }
8238
8239   /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
8240      SRC is an AND with all bits of that field set, then we can discard
8241      the AND.  */
8242   if (GET_CODE (dest) == ZERO_EXTRACT
8243       && GET_CODE (XEXP (dest, 1)) == CONST_INT
8244       && GET_CODE (src) == AND
8245       && GET_CODE (XEXP (src, 1)) == CONST_INT)
8246     {
8247       HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
8248       unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
8249       unsigned HOST_WIDE_INT ze_mask;
8250
8251       if (width >= HOST_BITS_PER_WIDE_INT)
8252         ze_mask = -1;
8253       else
8254         ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
8255
8256       /* Complete overlap.  We can remove the source AND.  */
8257       if ((and_mask & ze_mask) == ze_mask)
8258         return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
8259
8260       /* Partial overlap.  We can reduce the source AND.  */
8261       if ((and_mask & ze_mask) != and_mask)
8262         {
8263           mode = GET_MODE (src);
8264           src = gen_rtx_AND (mode, XEXP (src, 0),
8265                              gen_int_mode (and_mask & ze_mask, mode));
8266           return gen_rtx_SET (VOIDmode, dest, src);
8267         }
8268     }
8269
8270   /* The other case we handle is assignments into a constant-position
8271      field.  They look like (ior/xor (and DEST C1) OTHER).  If C1 represents
8272      a mask that has all one bits except for a group of zero bits and
8273      OTHER is known to have zeros where C1 has ones, this is such an
8274      assignment.  Compute the position and length from C1.  Shift OTHER
8275      to the appropriate position, force it to the required mode, and
8276      make the extraction.  Check for the AND in both operands.  */
8277
8278   if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
8279     return x;
8280
8281   rhs = expand_compound_operation (XEXP (src, 0));
8282   lhs = expand_compound_operation (XEXP (src, 1));
8283
8284   if (GET_CODE (rhs) == AND
8285       && GET_CODE (XEXP (rhs, 1)) == CONST_INT
8286       && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
8287     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
8288   else if (GET_CODE (lhs) == AND
8289            && GET_CODE (XEXP (lhs, 1)) == CONST_INT
8290            && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
8291     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
8292   else
8293     return x;
8294
8295   pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
8296   if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
8297       || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
8298       || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
8299     return x;
8300
8301   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
8302   if (assign == 0)
8303     return x;
8304
8305   /* The mode to use for the source is the mode of the assignment, or of
8306      what is inside a possible STRICT_LOW_PART.  */
8307   mode = (GET_CODE (assign) == STRICT_LOW_PART
8308           ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
8309
8310   /* Shift OTHER right POS places and make it the source, restricting it
8311      to the proper length and mode.  */
8312
8313   src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
8314                                                      GET_MODE (src),
8315                                                      other, pos),
8316                                dest);
8317   src = force_to_mode (src, mode,
8318                        GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
8319                        ? ~(unsigned HOST_WIDE_INT) 0
8320                        : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
8321                        0);
8322
8323   /* If SRC is masked by an AND that does not make a difference in
8324      the value being stored, strip it.  */
8325   if (GET_CODE (assign) == ZERO_EXTRACT
8326       && GET_CODE (XEXP (assign, 1)) == CONST_INT
8327       && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
8328       && GET_CODE (src) == AND
8329       && GET_CODE (XEXP (src, 1)) == CONST_INT
8330       && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
8331           == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
8332     src = XEXP (src, 0);
8333
8334   return gen_rtx_SET (VOIDmode, assign, src);
8335 }
8336 \f
8337 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
8338    if so.  */
8339
8340 static rtx
8341 apply_distributive_law (rtx x)
8342 {
8343   enum rtx_code code = GET_CODE (x);
8344   enum rtx_code inner_code;
8345   rtx lhs, rhs, other;
8346   rtx tem;
8347
8348   /* Distributivity is not true for floating point as it can change the
8349      value.  So we don't do it unless -funsafe-math-optimizations.  */
8350   if (FLOAT_MODE_P (GET_MODE (x))
8351       && ! flag_unsafe_math_optimizations)
8352     return x;
8353
8354   /* The outer operation can only be one of the following:  */
8355   if (code != IOR && code != AND && code != XOR
8356       && code != PLUS && code != MINUS)
8357     return x;
8358
8359   lhs = XEXP (x, 0);
8360   rhs = XEXP (x, 1);
8361
8362   /* If either operand is a primitive we can't do anything, so get out
8363      fast.  */
8364   if (OBJECT_P (lhs) || OBJECT_P (rhs))
8365     return x;
8366
8367   lhs = expand_compound_operation (lhs);
8368   rhs = expand_compound_operation (rhs);
8369   inner_code = GET_CODE (lhs);
8370   if (inner_code != GET_CODE (rhs))
8371     return x;
8372
8373   /* See if the inner and outer operations distribute.  */
8374   switch (inner_code)
8375     {
8376     case LSHIFTRT:
8377     case ASHIFTRT:
8378     case AND:
8379     case IOR:
8380       /* These all distribute except over PLUS.  */
8381       if (code == PLUS || code == MINUS)
8382         return x;
8383       break;
8384
8385     case MULT:
8386       if (code != PLUS && code != MINUS)
8387         return x;
8388       break;
8389
8390     case ASHIFT:
8391       /* This is also a multiply, so it distributes over everything.  */
8392       break;
8393
8394     case SUBREG:
8395       /* Non-paradoxical SUBREGs distributes over all operations,
8396          provided the inner modes and byte offsets are the same, this
8397          is an extraction of a low-order part, we don't convert an fp
8398          operation to int or vice versa, this is not a vector mode,
8399          and we would not be converting a single-word operation into a
8400          multi-word operation.  The latter test is not required, but
8401          it prevents generating unneeded multi-word operations.  Some
8402          of the previous tests are redundant given the latter test,
8403          but are retained because they are required for correctness.
8404
8405          We produce the result slightly differently in this case.  */
8406
8407       if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
8408           || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
8409           || ! subreg_lowpart_p (lhs)
8410           || (GET_MODE_CLASS (GET_MODE (lhs))
8411               != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
8412           || (GET_MODE_SIZE (GET_MODE (lhs))
8413               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
8414           || VECTOR_MODE_P (GET_MODE (lhs))
8415           || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD
8416           /* Result might need to be truncated.  Don't change mode if
8417              explicit truncation is needed.  */
8418           || !TRULY_NOOP_TRUNCATION
8419                (GET_MODE_BITSIZE (GET_MODE (x)),
8420                 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (lhs)))))
8421         return x;
8422
8423       tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
8424                                  SUBREG_REG (lhs), SUBREG_REG (rhs));
8425       return gen_lowpart (GET_MODE (x), tem);
8426
8427     default:
8428       return x;
8429     }
8430
8431   /* Set LHS and RHS to the inner operands (A and B in the example
8432      above) and set OTHER to the common operand (C in the example).
8433      There is only one way to do this unless the inner operation is
8434      commutative.  */
8435   if (COMMUTATIVE_ARITH_P (lhs)
8436       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
8437     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
8438   else if (COMMUTATIVE_ARITH_P (lhs)
8439            && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
8440     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
8441   else if (COMMUTATIVE_ARITH_P (lhs)
8442            && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
8443     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
8444   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
8445     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
8446   else
8447     return x;
8448
8449   /* Form the new inner operation, seeing if it simplifies first.  */
8450   tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
8451
8452   /* There is one exception to the general way of distributing:
8453      (a | c) ^ (b | c) -> (a ^ b) & ~c  */
8454   if (code == XOR && inner_code == IOR)
8455     {
8456       inner_code = AND;
8457       other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
8458     }
8459
8460   /* We may be able to continuing distributing the result, so call
8461      ourselves recursively on the inner operation before forming the
8462      outer operation, which we return.  */
8463   return simplify_gen_binary (inner_code, GET_MODE (x),
8464                               apply_distributive_law (tem), other);
8465 }
8466
8467 /* See if X is of the form (* (+ A B) C), and if so convert to
8468    (+ (* A C) (* B C)) and try to simplify.
8469
8470    Most of the time, this results in no change.  However, if some of
8471    the operands are the same or inverses of each other, simplifications
8472    will result.
8473
8474    For example, (and (ior A B) (not B)) can occur as the result of
8475    expanding a bit field assignment.  When we apply the distributive
8476    law to this, we get (ior (and (A (not B))) (and (B (not B)))),
8477    which then simplifies to (and (A (not B))).
8478
8479    Note that no checks happen on the validity of applying the inverse
8480    distributive law.  This is pointless since we can do it in the
8481    few places where this routine is called.
8482
8483    N is the index of the term that is decomposed (the arithmetic operation,
8484    i.e. (+ A B) in the first example above).  !N is the index of the term that
8485    is distributed, i.e. of C in the first example above.  */
8486 static rtx
8487 distribute_and_simplify_rtx (rtx x, int n)
8488 {
8489   enum machine_mode mode;
8490   enum rtx_code outer_code, inner_code;
8491   rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
8492
8493   decomposed = XEXP (x, n);
8494   if (!ARITHMETIC_P (decomposed))
8495     return NULL_RTX;
8496
8497   mode = GET_MODE (x);
8498   outer_code = GET_CODE (x);
8499   distributed = XEXP (x, !n);
8500
8501   inner_code = GET_CODE (decomposed);
8502   inner_op0 = XEXP (decomposed, 0);
8503   inner_op1 = XEXP (decomposed, 1);
8504
8505   /* Special case (and (xor B C) (not A)), which is equivalent to
8506      (xor (ior A B) (ior A C))  */
8507   if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
8508     {
8509       distributed = XEXP (distributed, 0);
8510       outer_code = IOR;
8511     }
8512
8513   if (n == 0)
8514     {
8515       /* Distribute the second term.  */
8516       new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
8517       new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
8518     }
8519   else
8520     {
8521       /* Distribute the first term.  */
8522       new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
8523       new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
8524     }
8525
8526   tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
8527                                                      new_op0, new_op1));
8528   if (GET_CODE (tmp) != outer_code
8529       && rtx_cost (tmp, SET) < rtx_cost (x, SET))
8530     return tmp;
8531
8532   return NULL_RTX;
8533 }
8534 \f
8535 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
8536    in MODE.  Return an equivalent form, if different from (and VAROP
8537    (const_int CONSTOP)).  Otherwise, return NULL_RTX.  */
8538
8539 static rtx
8540 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
8541                           unsigned HOST_WIDE_INT constop)
8542 {
8543   unsigned HOST_WIDE_INT nonzero;
8544   unsigned HOST_WIDE_INT orig_constop;
8545   rtx orig_varop;
8546   int i;
8547
8548   orig_varop = varop;
8549   orig_constop = constop;
8550   if (GET_CODE (varop) == CLOBBER)
8551     return NULL_RTX;
8552
8553   /* Simplify VAROP knowing that we will be only looking at some of the
8554      bits in it.
8555
8556      Note by passing in CONSTOP, we guarantee that the bits not set in
8557      CONSTOP are not significant and will never be examined.  We must
8558      ensure that is the case by explicitly masking out those bits
8559      before returning.  */
8560   varop = force_to_mode (varop, mode, constop, 0);
8561
8562   /* If VAROP is a CLOBBER, we will fail so return it.  */
8563   if (GET_CODE (varop) == CLOBBER)
8564     return varop;
8565
8566   /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
8567      to VAROP and return the new constant.  */
8568   if (GET_CODE (varop) == CONST_INT)
8569     return gen_int_mode (INTVAL (varop) & constop, mode);
8570
8571   /* See what bits may be nonzero in VAROP.  Unlike the general case of
8572      a call to nonzero_bits, here we don't care about bits outside
8573      MODE.  */
8574
8575   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
8576
8577   /* Turn off all bits in the constant that are known to already be zero.
8578      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
8579      which is tested below.  */
8580
8581   constop &= nonzero;
8582
8583   /* If we don't have any bits left, return zero.  */
8584   if (constop == 0)
8585     return const0_rtx;
8586
8587   /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
8588      a power of two, we can replace this with an ASHIFT.  */
8589   if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
8590       && (i = exact_log2 (constop)) >= 0)
8591     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
8592
8593   /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8594      or XOR, then try to apply the distributive law.  This may eliminate
8595      operations if either branch can be simplified because of the AND.
8596      It may also make some cases more complex, but those cases probably
8597      won't match a pattern either with or without this.  */
8598
8599   if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
8600     return
8601       gen_lowpart
8602         (mode,
8603          apply_distributive_law
8604          (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
8605                                simplify_and_const_int (NULL_RTX,
8606                                                        GET_MODE (varop),
8607                                                        XEXP (varop, 0),
8608                                                        constop),
8609                                simplify_and_const_int (NULL_RTX,
8610                                                        GET_MODE (varop),
8611                                                        XEXP (varop, 1),
8612                                                        constop))));
8613
8614   /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
8615      the AND and see if one of the operands simplifies to zero.  If so, we
8616      may eliminate it.  */
8617
8618   if (GET_CODE (varop) == PLUS
8619       && exact_log2 (constop + 1) >= 0)
8620     {
8621       rtx o0, o1;
8622
8623       o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
8624       o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
8625       if (o0 == const0_rtx)
8626         return o1;
8627       if (o1 == const0_rtx)
8628         return o0;
8629     }
8630
8631   /* Make a SUBREG if necessary.  If we can't make it, fail.  */
8632   varop = gen_lowpart (mode, varop);
8633   if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
8634     return NULL_RTX;
8635
8636   /* If we are only masking insignificant bits, return VAROP.  */
8637   if (constop == nonzero)
8638     return varop;
8639
8640   if (varop == orig_varop && constop == orig_constop)
8641     return NULL_RTX;
8642
8643   /* Otherwise, return an AND.  */
8644   return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
8645 }
8646
8647
8648 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
8649    in MODE.
8650
8651    Return an equivalent form, if different from X.  Otherwise, return X.  If
8652    X is zero, we are to always construct the equivalent form.  */
8653
8654 static rtx
8655 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
8656                         unsigned HOST_WIDE_INT constop)
8657 {
8658   rtx tem = simplify_and_const_int_1 (mode, varop, constop);
8659   if (tem)
8660     return tem;
8661
8662   if (!x)
8663     x = simplify_gen_binary (AND, GET_MODE (varop), varop,
8664                              gen_int_mode (constop, mode));
8665   if (GET_MODE (x) != mode)
8666     x = gen_lowpart (mode, x);
8667   return x;
8668 }
8669 \f
8670 /* Given a REG, X, compute which bits in X can be nonzero.
8671    We don't care about bits outside of those defined in MODE.
8672
8673    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8674    a shift, AND, or zero_extract, we can do better.  */
8675
8676 static rtx
8677 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
8678                               const_rtx known_x ATTRIBUTE_UNUSED,
8679                               enum machine_mode known_mode ATTRIBUTE_UNUSED,
8680                               unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
8681                               unsigned HOST_WIDE_INT *nonzero)
8682 {
8683   rtx tem;
8684   reg_stat_type *rsp;
8685
8686   /* If X is a register whose nonzero bits value is current, use it.
8687      Otherwise, if X is a register whose value we can find, use that
8688      value.  Otherwise, use the previously-computed global nonzero bits
8689      for this register.  */
8690
8691   rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
8692   if (rsp->last_set_value != 0
8693       && (rsp->last_set_mode == mode
8694           || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
8695               && GET_MODE_CLASS (mode) == MODE_INT))
8696       && ((rsp->last_set_label >= label_tick_ebb_start
8697            && rsp->last_set_label < label_tick)
8698           || (rsp->last_set_label == label_tick
8699               && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
8700           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8701               && REG_N_SETS (REGNO (x)) == 1
8702               && !REGNO_REG_SET_P
8703                   (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
8704     {
8705       *nonzero &= rsp->last_set_nonzero_bits;
8706       return NULL;
8707     }
8708
8709   tem = get_last_value (x);
8710
8711   if (tem)
8712     {
8713 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8714       /* If X is narrower than MODE and TEM is a non-negative
8715          constant that would appear negative in the mode of X,
8716          sign-extend it for use in reg_nonzero_bits because some
8717          machines (maybe most) will actually do the sign-extension
8718          and this is the conservative approach.
8719
8720          ??? For 2.5, try to tighten up the MD files in this regard
8721          instead of this kludge.  */
8722
8723       if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
8724           && GET_CODE (tem) == CONST_INT
8725           && INTVAL (tem) > 0
8726           && 0 != (INTVAL (tem)
8727                    & ((HOST_WIDE_INT) 1
8728                       << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8729         tem = GEN_INT (INTVAL (tem)
8730                        | ((HOST_WIDE_INT) (-1)
8731                           << GET_MODE_BITSIZE (GET_MODE (x))));
8732 #endif
8733       return tem;
8734     }
8735   else if (nonzero_sign_valid && rsp->nonzero_bits)
8736     {
8737       unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
8738
8739       if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
8740         /* We don't know anything about the upper bits.  */
8741         mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8742       *nonzero &= mask;
8743     }
8744
8745   return NULL;
8746 }
8747
8748 /* Return the number of bits at the high-order end of X that are known to
8749    be equal to the sign bit.  X will be used in mode MODE; if MODE is
8750    VOIDmode, X will be used in its own mode.  The returned value  will always
8751    be between 1 and the number of bits in MODE.  */
8752
8753 static rtx
8754 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
8755                                      const_rtx known_x ATTRIBUTE_UNUSED,
8756                                      enum machine_mode known_mode
8757                                      ATTRIBUTE_UNUSED,
8758                                      unsigned int known_ret ATTRIBUTE_UNUSED,
8759                                      unsigned int *result)
8760 {
8761   rtx tem;
8762   reg_stat_type *rsp;
8763
8764   rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
8765   if (rsp->last_set_value != 0
8766       && rsp->last_set_mode == mode
8767       && ((rsp->last_set_label >= label_tick_ebb_start
8768            && rsp->last_set_label < label_tick)
8769           || (rsp->last_set_label == label_tick
8770               && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
8771           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8772               && REG_N_SETS (REGNO (x)) == 1
8773               && !REGNO_REG_SET_P
8774                   (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
8775     {
8776       *result = rsp->last_set_sign_bit_copies;
8777       return NULL;
8778     }
8779
8780   tem = get_last_value (x);
8781   if (tem != 0)
8782     return tem;
8783
8784   if (nonzero_sign_valid && rsp->sign_bit_copies != 0
8785       && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
8786     *result = rsp->sign_bit_copies;
8787
8788   return NULL;
8789 }
8790 \f
8791 /* Return the number of "extended" bits there are in X, when interpreted
8792    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
8793    unsigned quantities, this is the number of high-order zero bits.
8794    For signed quantities, this is the number of copies of the sign bit
8795    minus 1.  In both case, this function returns the number of "spare"
8796    bits.  For example, if two quantities for which this function returns
8797    at least 1 are added, the addition is known not to overflow.
8798
8799    This function will always return 0 unless called during combine, which
8800    implies that it must be called from a define_split.  */
8801
8802 unsigned int
8803 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
8804 {
8805   if (nonzero_sign_valid == 0)
8806     return 0;
8807
8808   return (unsignedp
8809           ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8810              ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
8811                                - floor_log2 (nonzero_bits (x, mode)))
8812              : 0)
8813           : num_sign_bit_copies (x, mode) - 1);
8814 }
8815 \f
8816 /* This function is called from `simplify_shift_const' to merge two
8817    outer operations.  Specifically, we have already found that we need
8818    to perform operation *POP0 with constant *PCONST0 at the outermost
8819    position.  We would now like to also perform OP1 with constant CONST1
8820    (with *POP0 being done last).
8821
8822    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8823    the resulting operation.  *PCOMP_P is set to 1 if we would need to
8824    complement the innermost operand, otherwise it is unchanged.
8825
8826    MODE is the mode in which the operation will be done.  No bits outside
8827    the width of this mode matter.  It is assumed that the width of this mode
8828    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8829
8830    If *POP0 or OP1 are UNKNOWN, it means no operation is required.  Only NEG, PLUS,
8831    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
8832    result is simply *PCONST0.
8833
8834    If the resulting operation cannot be expressed as one operation, we
8835    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
8836
8837 static int
8838 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)
8839 {
8840   enum rtx_code op0 = *pop0;
8841   HOST_WIDE_INT const0 = *pconst0;
8842
8843   const0 &= GET_MODE_MASK (mode);
8844   const1 &= GET_MODE_MASK (mode);
8845
8846   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
8847   if (op0 == AND)
8848     const1 &= const0;
8849
8850   /* If OP0 or OP1 is UNKNOWN, this is easy.  Similarly if they are the same or
8851      if OP0 is SET.  */
8852
8853   if (op1 == UNKNOWN || op0 == SET)
8854     return 1;
8855
8856   else if (op0 == UNKNOWN)
8857     op0 = op1, const0 = const1;
8858
8859   else if (op0 == op1)
8860     {
8861       switch (op0)
8862         {
8863         case AND:
8864           const0 &= const1;
8865           break;
8866         case IOR:
8867           const0 |= const1;
8868           break;
8869         case XOR:
8870           const0 ^= const1;
8871           break;
8872         case PLUS:
8873           const0 += const1;
8874           break;
8875         case NEG:
8876           op0 = UNKNOWN;
8877           break;
8878         default:
8879           break;
8880         }
8881     }
8882
8883   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
8884   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8885     return 0;
8886
8887   /* If the two constants aren't the same, we can't do anything.  The
8888      remaining six cases can all be done.  */
8889   else if (const0 != const1)
8890     return 0;
8891
8892   else
8893     switch (op0)
8894       {
8895       case IOR:
8896         if (op1 == AND)
8897           /* (a & b) | b == b */
8898           op0 = SET;
8899         else /* op1 == XOR */
8900           /* (a ^ b) | b == a | b */
8901           {;}
8902         break;
8903
8904       case XOR:
8905         if (op1 == AND)
8906           /* (a & b) ^ b == (~a) & b */
8907           op0 = AND, *pcomp_p = 1;
8908         else /* op1 == IOR */
8909           /* (a | b) ^ b == a & ~b */
8910           op0 = AND, const0 = ~const0;
8911         break;
8912
8913       case AND:
8914         if (op1 == IOR)
8915           /* (a | b) & b == b */
8916         op0 = SET;
8917         else /* op1 == XOR */
8918           /* (a ^ b) & b) == (~a) & b */
8919           *pcomp_p = 1;
8920         break;
8921       default:
8922         break;
8923       }
8924
8925   /* Check for NO-OP cases.  */
8926   const0 &= GET_MODE_MASK (mode);
8927   if (const0 == 0
8928       && (op0 == IOR || op0 == XOR || op0 == PLUS))
8929     op0 = UNKNOWN;
8930   else if (const0 == 0 && op0 == AND)
8931     op0 = SET;
8932   else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8933            && op0 == AND)
8934     op0 = UNKNOWN;
8935
8936   /* ??? Slightly redundant with the above mask, but not entirely.
8937      Moving this above means we'd have to sign-extend the mode mask
8938      for the final test.  */
8939   const0 = trunc_int_for_mode (const0, mode);
8940
8941   *pop0 = op0;
8942   *pconst0 = const0;
8943
8944   return 1;
8945 }
8946 \f
8947 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
8948    The result of the shift is RESULT_MODE.  Return NULL_RTX if we cannot
8949    simplify it.  Otherwise, return a simplified value.
8950
8951    The shift is normally computed in the widest mode we find in VAROP, as
8952    long as it isn't a different number of words than RESULT_MODE.  Exceptions
8953    are ASHIFTRT and ROTATE, which are always done in their original mode.  */
8954
8955 static rtx
8956 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
8957                         rtx varop, int orig_count)
8958 {
8959   enum rtx_code orig_code = code;
8960   rtx orig_varop = varop;
8961   int count;
8962   enum machine_mode mode = result_mode;
8963   enum machine_mode shift_mode, tmode;
8964   unsigned int mode_words
8965     = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8966   /* We form (outer_op (code varop count) (outer_const)).  */
8967   enum rtx_code outer_op = UNKNOWN;
8968   HOST_WIDE_INT outer_const = 0;
8969   int complement_p = 0;
8970   rtx new, x;
8971
8972   /* Make sure and truncate the "natural" shift on the way in.  We don't
8973      want to do this inside the loop as it makes it more difficult to
8974      combine shifts.  */
8975   if (SHIFT_COUNT_TRUNCATED)
8976     orig_count &= GET_MODE_BITSIZE (mode) - 1;
8977
8978   /* If we were given an invalid count, don't do anything except exactly
8979      what was requested.  */
8980
8981   if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
8982     return NULL_RTX;
8983
8984   count = orig_count;
8985
8986   /* Unless one of the branches of the `if' in this loop does a `continue',
8987      we will `break' the loop after the `if'.  */
8988
8989   while (count != 0)
8990     {
8991       /* If we have an operand of (clobber (const_int 0)), fail.  */
8992       if (GET_CODE (varop) == CLOBBER)
8993         return NULL_RTX;
8994
8995       /* If we discovered we had to complement VAROP, leave.  Making a NOT
8996          here would cause an infinite loop.  */
8997       if (complement_p)
8998         break;
8999
9000       /* Convert ROTATERT to ROTATE.  */
9001       if (code == ROTATERT)
9002         {
9003           unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
9004           code = ROTATE;
9005           if (VECTOR_MODE_P (result_mode))
9006             count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9007           else
9008             count = bitsize - count;
9009         }
9010
9011       /* We need to determine what mode we will do the shift in.  If the
9012          shift is a right shift or a ROTATE, we must always do it in the mode
9013          it was originally done in.  Otherwise, we can do it in MODE, the
9014          widest mode encountered.  */
9015       shift_mode
9016         = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9017            ? result_mode : mode);
9018
9019       /* Handle cases where the count is greater than the size of the mode
9020          minus 1.  For ASHIFT, use the size minus one as the count (this can
9021          occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
9022          take the count modulo the size.  For other shifts, the result is
9023          zero.
9024
9025          Since these shifts are being produced by the compiler by combining
9026          multiple operations, each of which are defined, we know what the
9027          result is supposed to be.  */
9028
9029       if (count > (GET_MODE_BITSIZE (shift_mode) - 1))
9030         {
9031           if (code == ASHIFTRT)
9032             count = GET_MODE_BITSIZE (shift_mode) - 1;
9033           else if (code == ROTATE || code == ROTATERT)
9034             count %= GET_MODE_BITSIZE (shift_mode);
9035           else
9036             {
9037               /* We can't simply return zero because there may be an
9038                  outer op.  */
9039               varop = const0_rtx;
9040               count = 0;
9041               break;
9042             }
9043         }
9044
9045       /* An arithmetic right shift of a quantity known to be -1 or 0
9046          is a no-op.  */
9047       if (code == ASHIFTRT
9048           && (num_sign_bit_copies (varop, shift_mode)
9049               == GET_MODE_BITSIZE (shift_mode)))
9050         {
9051           count = 0;
9052           break;
9053         }
9054
9055       /* If we are doing an arithmetic right shift and discarding all but
9056          the sign bit copies, this is equivalent to doing a shift by the
9057          bitsize minus one.  Convert it into that shift because it will often
9058          allow other simplifications.  */
9059
9060       if (code == ASHIFTRT
9061           && (count + num_sign_bit_copies (varop, shift_mode)
9062               >= GET_MODE_BITSIZE (shift_mode)))
9063         count = GET_MODE_BITSIZE (shift_mode) - 1;
9064
9065       /* We simplify the tests below and elsewhere by converting
9066          ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9067          `make_compound_operation' will convert it to an ASHIFTRT for
9068          those machines (such as VAX) that don't have an LSHIFTRT.  */
9069       if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9070           && code == ASHIFTRT
9071           && ((nonzero_bits (varop, shift_mode)
9072                & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
9073               == 0))
9074         code = LSHIFTRT;
9075
9076       if (((code == LSHIFTRT
9077             && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9078             && !(nonzero_bits (varop, shift_mode) >> count))
9079            || (code == ASHIFT
9080                && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9081                && !((nonzero_bits (varop, shift_mode) << count)
9082                     & GET_MODE_MASK (shift_mode))))
9083           && !side_effects_p (varop))
9084         varop = const0_rtx;
9085
9086       switch (GET_CODE (varop))
9087         {
9088         case SIGN_EXTEND:
9089         case ZERO_EXTEND:
9090         case SIGN_EXTRACT:
9091         case ZERO_EXTRACT:
9092           new = expand_compound_operation (varop);
9093           if (new != varop)
9094             {
9095               varop = new;
9096               continue;
9097             }
9098           break;
9099
9100         case MEM:
9101           /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9102              minus the width of a smaller mode, we can do this with a
9103              SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
9104           if ((code == ASHIFTRT || code == LSHIFTRT)
9105               && ! mode_dependent_address_p (XEXP (varop, 0))
9106               && ! MEM_VOLATILE_P (varop)
9107               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9108                                          MODE_INT, 1)) != BLKmode)
9109             {
9110               new = adjust_address_nv (varop, tmode,
9111                                        BYTES_BIG_ENDIAN ? 0
9112                                        : count / BITS_PER_UNIT);
9113
9114               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9115                                      : ZERO_EXTEND, mode, new);
9116               count = 0;
9117               continue;
9118             }
9119           break;
9120
9121         case SUBREG:
9122           /* If VAROP is a SUBREG, strip it as long as the inner operand has
9123              the same number of words as what we've seen so far.  Then store
9124              the widest mode in MODE.  */
9125           if (subreg_lowpart_p (varop)
9126               && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9127                   > GET_MODE_SIZE (GET_MODE (varop)))
9128               && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9129                                   + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9130                  == mode_words)
9131             {
9132               varop = SUBREG_REG (varop);
9133               if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9134                 mode = GET_MODE (varop);
9135               continue;
9136             }
9137           break;
9138
9139         case MULT:
9140           /* Some machines use MULT instead of ASHIFT because MULT
9141              is cheaper.  But it is still better on those machines to
9142              merge two shifts into one.  */
9143           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9144               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9145             {
9146               varop
9147                 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
9148                                        XEXP (varop, 0),
9149                                        GEN_INT (exact_log2 (
9150                                                 INTVAL (XEXP (varop, 1)))));
9151               continue;
9152             }
9153           break;
9154
9155         case UDIV:
9156           /* Similar, for when divides are cheaper.  */
9157           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9158               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9159             {
9160               varop
9161                 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
9162                                        XEXP (varop, 0),
9163                                        GEN_INT (exact_log2 (
9164                                                 INTVAL (XEXP (varop, 1)))));
9165               continue;
9166             }
9167           break;
9168
9169         case ASHIFTRT:
9170           /* If we are extracting just the sign bit of an arithmetic
9171              right shift, that shift is not needed.  However, the sign
9172              bit of a wider mode may be different from what would be
9173              interpreted as the sign bit in a narrower mode, so, if
9174              the result is narrower, don't discard the shift.  */
9175           if (code == LSHIFTRT
9176               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9177               && (GET_MODE_BITSIZE (result_mode)
9178                   >= GET_MODE_BITSIZE (GET_MODE (varop))))
9179             {
9180               varop = XEXP (varop, 0);
9181               continue;
9182             }
9183
9184           /* ... fall through ...  */
9185
9186         case LSHIFTRT:
9187         case ASHIFT:
9188         case ROTATE:
9189           /* Here we have two nested shifts.  The result is usually the
9190              AND of a new shift with a mask.  We compute the result below.  */
9191           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9192               && INTVAL (XEXP (varop, 1)) >= 0
9193               && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9194               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9195               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9196               && !VECTOR_MODE_P (result_mode))
9197             {
9198               enum rtx_code first_code = GET_CODE (varop);
9199               unsigned int first_count = INTVAL (XEXP (varop, 1));
9200               unsigned HOST_WIDE_INT mask;
9201               rtx mask_rtx;
9202
9203               /* We have one common special case.  We can't do any merging if
9204                  the inner code is an ASHIFTRT of a smaller mode.  However, if
9205                  we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9206                  with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9207                  we can convert it to
9208                  (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9209                  This simplifies certain SIGN_EXTEND operations.  */
9210               if (code == ASHIFT && first_code == ASHIFTRT
9211                   && count == (GET_MODE_BITSIZE (result_mode)
9212                                - GET_MODE_BITSIZE (GET_MODE (varop))))
9213                 {
9214                   /* C3 has the low-order C1 bits zero.  */
9215
9216                   mask = (GET_MODE_MASK (mode)
9217                           & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
9218
9219                   varop = simplify_and_const_int (NULL_RTX, result_mode,
9220                                                   XEXP (varop, 0), mask);
9221                   varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
9222                                                 varop, count);
9223                   count = first_count;
9224                   code = ASHIFTRT;
9225                   continue;
9226                 }
9227
9228               /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
9229                  than C1 high-order bits equal to the sign bit, we can convert
9230                  this to either an ASHIFT or an ASHIFTRT depending on the
9231                  two counts.
9232
9233                  We cannot do this if VAROP's mode is not SHIFT_MODE.  */
9234
9235               if (code == ASHIFTRT && first_code == ASHIFT
9236                   && GET_MODE (varop) == shift_mode
9237                   && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
9238                       > first_count))
9239                 {
9240                   varop = XEXP (varop, 0);
9241                   count -= first_count;
9242                   if (count < 0)
9243                     {
9244                       count = -count;
9245                       code = ASHIFT;
9246                     }
9247
9248                   continue;
9249                 }
9250
9251               /* There are some cases we can't do.  If CODE is ASHIFTRT,
9252                  we can only do this if FIRST_CODE is also ASHIFTRT.
9253
9254                  We can't do the case when CODE is ROTATE and FIRST_CODE is
9255                  ASHIFTRT.
9256
9257                  If the mode of this shift is not the mode of the outer shift,
9258                  we can't do this if either shift is a right shift or ROTATE.
9259
9260                  Finally, we can't do any of these if the mode is too wide
9261                  unless the codes are the same.
9262
9263                  Handle the case where the shift codes are the same
9264                  first.  */
9265
9266               if (code == first_code)
9267                 {
9268                   if (GET_MODE (varop) != result_mode
9269                       && (code == ASHIFTRT || code == LSHIFTRT
9270                           || code == ROTATE))
9271                     break;
9272
9273                   count += first_count;
9274                   varop = XEXP (varop, 0);
9275                   continue;
9276                 }
9277
9278               if (code == ASHIFTRT
9279                   || (code == ROTATE && first_code == ASHIFTRT)
9280                   || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
9281                   || (GET_MODE (varop) != result_mode
9282                       && (first_code == ASHIFTRT || first_code == LSHIFTRT
9283                           || first_code == ROTATE
9284                           || code == ROTATE)))
9285                 break;
9286
9287               /* To compute the mask to apply after the shift, shift the
9288                  nonzero bits of the inner shift the same way the
9289                  outer shift will.  */
9290
9291               mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
9292
9293               mask_rtx
9294                 = simplify_const_binary_operation (code, result_mode, mask_rtx,
9295                                                    GEN_INT (count));
9296
9297               /* Give up if we can't compute an outer operation to use.  */
9298               if (mask_rtx == 0
9299                   || GET_CODE (mask_rtx) != CONST_INT
9300                   || ! merge_outer_ops (&outer_op, &outer_const, AND,
9301                                         INTVAL (mask_rtx),
9302                                         result_mode, &complement_p))
9303                 break;
9304
9305               /* If the shifts are in the same direction, we add the
9306                  counts.  Otherwise, we subtract them.  */
9307               if ((code == ASHIFTRT || code == LSHIFTRT)
9308                   == (first_code == ASHIFTRT || first_code == LSHIFTRT))
9309                 count += first_count;
9310               else
9311                 count -= first_count;
9312
9313               /* If COUNT is positive, the new shift is usually CODE,
9314                  except for the two exceptions below, in which case it is
9315                  FIRST_CODE.  If the count is negative, FIRST_CODE should
9316                  always be used  */
9317               if (count > 0
9318                   && ((first_code == ROTATE && code == ASHIFT)
9319                       || (first_code == ASHIFTRT && code == LSHIFTRT)))
9320                 code = first_code;
9321               else if (count < 0)
9322                 code = first_code, count = -count;
9323
9324               varop = XEXP (varop, 0);
9325               continue;
9326             }
9327
9328           /* If we have (A << B << C) for any shift, we can convert this to
9329              (A << C << B).  This wins if A is a constant.  Only try this if
9330              B is not a constant.  */
9331
9332           else if (GET_CODE (varop) == code
9333                    && GET_CODE (XEXP (varop, 0)) == CONST_INT
9334                    && GET_CODE (XEXP (varop, 1)) != CONST_INT)
9335             {
9336               rtx new = simplify_const_binary_operation (code, mode,
9337                                                          XEXP (varop, 0),
9338                                                          GEN_INT (count));
9339               varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
9340               count = 0;
9341               continue;
9342             }
9343           break;
9344
9345         case NOT:
9346           /* Make this fit the case below.  */
9347           varop = gen_rtx_XOR (mode, XEXP (varop, 0),
9348                                GEN_INT (GET_MODE_MASK (mode)));
9349           continue;
9350
9351         case IOR:
9352         case AND:
9353         case XOR:
9354           /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9355              with C the size of VAROP - 1 and the shift is logical if
9356              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9357              we have an (le X 0) operation.   If we have an arithmetic shift
9358              and STORE_FLAG_VALUE is 1 or we have a logical shift with
9359              STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
9360
9361           if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
9362               && XEXP (XEXP (varop, 0), 1) == constm1_rtx
9363               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9364               && (code == LSHIFTRT || code == ASHIFTRT)
9365               && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9366               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9367             {
9368               count = 0;
9369               varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
9370                                   const0_rtx);
9371
9372               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9373                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9374
9375               continue;
9376             }
9377
9378           /* If we have (shift (logical)), move the logical to the outside
9379              to allow it to possibly combine with another logical and the
9380              shift to combine with another shift.  This also canonicalizes to
9381              what a ZERO_EXTRACT looks like.  Also, some machines have
9382              (and (shift)) insns.  */
9383
9384           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9385               /* We can't do this if we have (ashiftrt (xor))  and the
9386                  constant has its sign bit set in shift_mode.  */
9387               && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9388                    && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9389                                               shift_mode))
9390               && (new = simplify_const_binary_operation (code, result_mode,
9391                                                          XEXP (varop, 1),
9392                                                          GEN_INT (count))) != 0
9393               && GET_CODE (new) == CONST_INT
9394               && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9395                                   INTVAL (new), result_mode, &complement_p))
9396             {
9397               varop = XEXP (varop, 0);
9398               continue;
9399             }
9400
9401           /* If we can't do that, try to simplify the shift in each arm of the
9402              logical expression, make a new logical expression, and apply
9403              the inverse distributive law.  This also can't be done
9404              for some (ashiftrt (xor)).  */
9405           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9406              && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9407                   && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9408                                              shift_mode)))
9409             {
9410               rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9411                                               XEXP (varop, 0), count);
9412               rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9413                                               XEXP (varop, 1), count);
9414
9415               varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
9416                                            lhs, rhs);
9417               varop = apply_distributive_law (varop);
9418
9419               count = 0;
9420               continue;
9421             }
9422           break;
9423
9424         case EQ:
9425           /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9426              says that the sign bit can be tested, FOO has mode MODE, C is
9427              GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9428              that may be nonzero.  */
9429           if (code == LSHIFTRT
9430               && XEXP (varop, 1) == const0_rtx
9431               && GET_MODE (XEXP (varop, 0)) == result_mode
9432               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9433               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9434               && STORE_FLAG_VALUE == -1
9435               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9436               && merge_outer_ops (&outer_op, &outer_const, XOR,
9437                                   (HOST_WIDE_INT) 1, result_mode,
9438                                   &complement_p))
9439             {
9440               varop = XEXP (varop, 0);
9441               count = 0;
9442               continue;
9443             }
9444           break;
9445
9446         case NEG:
9447           /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9448              than the number of bits in the mode is equivalent to A.  */
9449           if (code == LSHIFTRT
9450               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9451               && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9452             {
9453               varop = XEXP (varop, 0);
9454               count = 0;
9455               continue;
9456             }
9457
9458           /* NEG commutes with ASHIFT since it is multiplication.  Move the
9459              NEG outside to allow shifts to combine.  */
9460           if (code == ASHIFT
9461               && merge_outer_ops (&outer_op, &outer_const, NEG,
9462                                   (HOST_WIDE_INT) 0, result_mode,
9463                                   &complement_p))
9464             {
9465               varop = XEXP (varop, 0);
9466               continue;
9467             }
9468           break;
9469
9470         case PLUS:
9471           /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9472              is one less than the number of bits in the mode is
9473              equivalent to (xor A 1).  */
9474           if (code == LSHIFTRT
9475               && count == (GET_MODE_BITSIZE (result_mode) - 1)
9476               && XEXP (varop, 1) == constm1_rtx
9477               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9478               && merge_outer_ops (&outer_op, &outer_const, XOR,
9479                                   (HOST_WIDE_INT) 1, result_mode,
9480                                   &complement_p))
9481             {
9482               count = 0;
9483               varop = XEXP (varop, 0);
9484               continue;
9485             }
9486
9487           /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9488              that might be nonzero in BAR are those being shifted out and those
9489              bits are known zero in FOO, we can replace the PLUS with FOO.
9490              Similarly in the other operand order.  This code occurs when
9491              we are computing the size of a variable-size array.  */
9492
9493           if ((code == ASHIFTRT || code == LSHIFTRT)
9494               && count < HOST_BITS_PER_WIDE_INT
9495               && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9496               && (nonzero_bits (XEXP (varop, 1), result_mode)
9497                   & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9498             {
9499               varop = XEXP (varop, 0);
9500               continue;
9501             }
9502           else if ((code == ASHIFTRT || code == LSHIFTRT)
9503                    && count < HOST_BITS_PER_WIDE_INT
9504                    && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9505                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9506                             >> count)
9507                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9508                             & nonzero_bits (XEXP (varop, 1),
9509                                                  result_mode)))
9510             {
9511               varop = XEXP (varop, 1);
9512               continue;
9513             }
9514
9515           /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
9516           if (code == ASHIFT
9517               && GET_CODE (XEXP (varop, 1)) == CONST_INT
9518               && (new = simplify_const_binary_operation (ASHIFT, result_mode,
9519                                                          XEXP (varop, 1),
9520                                                          GEN_INT (count))) != 0
9521               && GET_CODE (new) == CONST_INT
9522               && merge_outer_ops (&outer_op, &outer_const, PLUS,
9523                                   INTVAL (new), result_mode, &complement_p))
9524             {
9525               varop = XEXP (varop, 0);
9526               continue;
9527             }
9528
9529           /* Check for 'PLUS signbit', which is the canonical form of 'XOR
9530              signbit', and attempt to change the PLUS to an XOR and move it to
9531              the outer operation as is done above in the AND/IOR/XOR case
9532              leg for shift(logical). See details in logical handling above
9533              for reasoning in doing so.  */
9534           if (code == LSHIFTRT
9535               && GET_CODE (XEXP (varop, 1)) == CONST_INT
9536               && mode_signbit_p (result_mode, XEXP (varop, 1))
9537               && (new = simplify_const_binary_operation (code, result_mode,
9538                                                          XEXP (varop, 1),
9539                                                          GEN_INT (count))) != 0
9540               && GET_CODE (new) == CONST_INT
9541               && merge_outer_ops (&outer_op, &outer_const, XOR,
9542                                   INTVAL (new), result_mode, &complement_p))
9543             {
9544               varop = XEXP (varop, 0);
9545               continue;
9546             }
9547
9548           break;
9549
9550         case MINUS:
9551           /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9552              with C the size of VAROP - 1 and the shift is logical if
9553              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9554              we have a (gt X 0) operation.  If the shift is arithmetic with
9555              STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9556              we have a (neg (gt X 0)) operation.  */
9557
9558           if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9559               && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9560               && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9561               && (code == LSHIFTRT || code == ASHIFTRT)
9562               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9563               && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
9564               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9565             {
9566               count = 0;
9567               varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9568                                   const0_rtx);
9569
9570               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9571                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9572
9573               continue;
9574             }
9575           break;
9576
9577         case TRUNCATE:
9578           /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9579              if the truncate does not affect the value.  */
9580           if (code == LSHIFTRT
9581               && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9582               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9583               && (INTVAL (XEXP (XEXP (varop, 0), 1))
9584                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9585                       - GET_MODE_BITSIZE (GET_MODE (varop)))))
9586             {
9587               rtx varop_inner = XEXP (varop, 0);
9588
9589               varop_inner
9590                 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9591                                     XEXP (varop_inner, 0),
9592                                     GEN_INT
9593                                     (count + INTVAL (XEXP (varop_inner, 1))));
9594               varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9595               count = 0;
9596               continue;
9597             }
9598           break;
9599
9600         default:
9601           break;
9602         }
9603
9604       break;
9605     }
9606
9607   /* We need to determine what mode to do the shift in.  If the shift is
9608      a right shift or ROTATE, we must always do it in the mode it was
9609      originally done in.  Otherwise, we can do it in MODE, the widest mode
9610      encountered.  The code we care about is that of the shift that will
9611      actually be done, not the shift that was originally requested.  */
9612   shift_mode
9613     = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9614        ? result_mode : mode);
9615
9616   /* We have now finished analyzing the shift.  The result should be
9617      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
9618      OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
9619      to the result of the shift.  OUTER_CONST is the relevant constant,
9620      but we must turn off all bits turned off in the shift.  */
9621
9622   if (outer_op == UNKNOWN
9623       && orig_code == code && orig_count == count
9624       && varop == orig_varop
9625       && shift_mode == GET_MODE (varop))
9626     return NULL_RTX;
9627
9628   /* Make a SUBREG if necessary.  If we can't make it, fail.  */
9629   varop = gen_lowpart (shift_mode, varop);
9630   if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9631     return NULL_RTX;
9632
9633   /* If we have an outer operation and we just made a shift, it is
9634      possible that we could have simplified the shift were it not
9635      for the outer operation.  So try to do the simplification
9636      recursively.  */
9637
9638   if (outer_op != UNKNOWN)
9639     x = simplify_shift_const_1 (code, shift_mode, varop, count);
9640   else
9641     x = NULL_RTX;
9642
9643   if (x == NULL_RTX)
9644     x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
9645
9646   /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9647      turn off all the bits that the shift would have turned off.  */
9648   if (orig_code == LSHIFTRT && result_mode != shift_mode)
9649     x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9650                                 GET_MODE_MASK (result_mode) >> orig_count);
9651
9652   /* Do the remainder of the processing in RESULT_MODE.  */
9653   x = gen_lowpart_or_truncate (result_mode, x);
9654
9655   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9656      operation.  */
9657   if (complement_p)
9658     x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9659
9660   if (outer_op != UNKNOWN)
9661     {
9662       if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9663         outer_const = trunc_int_for_mode (outer_const, result_mode);
9664
9665       if (outer_op == AND)
9666         x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9667       else if (outer_op == SET)
9668         {
9669           /* This means that we have determined that the result is
9670              equivalent to a constant.  This should be rare.  */
9671           if (!side_effects_p (x))
9672             x = GEN_INT (outer_const);
9673         }
9674       else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
9675         x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
9676       else
9677         x = simplify_gen_binary (outer_op, result_mode, x,
9678                                  GEN_INT (outer_const));
9679     }
9680
9681   return x;
9682 }
9683
9684 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
9685    The result of the shift is RESULT_MODE.  If we cannot simplify it,
9686    return X or, if it is NULL, synthesize the expression with
9687    simplify_gen_binary.  Otherwise, return a simplified value.
9688
9689    The shift is normally computed in the widest mode we find in VAROP, as
9690    long as it isn't a different number of words than RESULT_MODE.  Exceptions
9691    are ASHIFTRT and ROTATE, which are always done in their original mode.  */
9692
9693 static rtx
9694 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
9695                       rtx varop, int count)
9696 {
9697   rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
9698   if (tem)
9699     return tem;
9700
9701   if (!x)
9702     x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
9703   if (GET_MODE (x) != result_mode)
9704     x = gen_lowpart (result_mode, x);
9705   return x;
9706 }
9707
9708 \f
9709 /* Like recog, but we receive the address of a pointer to a new pattern.
9710    We try to match the rtx that the pointer points to.
9711    If that fails, we may try to modify or replace the pattern,
9712    storing the replacement into the same pointer object.
9713
9714    Modifications include deletion or addition of CLOBBERs.
9715
9716    PNOTES is a pointer to a location where any REG_UNUSED notes added for
9717    the CLOBBERs are placed.
9718
9719    The value is the final insn code from the pattern ultimately matched,
9720    or -1.  */
9721
9722 static int
9723 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
9724 {
9725   rtx pat = *pnewpat;
9726   int insn_code_number;
9727   int num_clobbers_to_add = 0;
9728   int i;
9729   rtx notes = 0;
9730   rtx old_notes, old_pat;
9731
9732   /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9733      we use to indicate that something didn't match.  If we find such a
9734      thing, force rejection.  */
9735   if (GET_CODE (pat) == PARALLEL)
9736     for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9737       if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9738           && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9739         return -1;
9740
9741   old_pat = PATTERN (insn);
9742   old_notes = REG_NOTES (insn);
9743   PATTERN (insn) = pat;
9744   REG_NOTES (insn) = 0;
9745
9746   insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9747   if (dump_file && (dump_flags & TDF_DETAILS))
9748     {
9749       if (insn_code_number < 0)
9750         fputs ("Failed to match this instruction:\n", dump_file);
9751       else
9752         fputs ("Successfully matched this instruction:\n", dump_file);
9753       print_rtl_single (dump_file, pat);
9754     }
9755
9756   /* If it isn't, there is the possibility that we previously had an insn
9757      that clobbered some register as a side effect, but the combined
9758      insn doesn't need to do that.  So try once more without the clobbers
9759      unless this represents an ASM insn.  */
9760
9761   if (insn_code_number < 0 && ! check_asm_operands (pat)
9762       && GET_CODE (pat) == PARALLEL)
9763     {
9764       int pos;
9765
9766       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9767         if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9768           {
9769             if (i != pos)
9770               SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9771             pos++;
9772           }
9773
9774       SUBST_INT (XVECLEN (pat, 0), pos);
9775
9776       if (pos == 1)
9777         pat = XVECEXP (pat, 0, 0);
9778
9779       PATTERN (insn) = pat;
9780       insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9781       if (dump_file && (dump_flags & TDF_DETAILS))
9782         {
9783           if (insn_code_number < 0)
9784             fputs ("Failed to match this instruction:\n", dump_file);
9785           else
9786             fputs ("Successfully matched this instruction:\n", dump_file);
9787           print_rtl_single (dump_file, pat);
9788         }
9789     }
9790   PATTERN (insn) = old_pat;
9791   REG_NOTES (insn) = old_notes;
9792
9793   /* Recognize all noop sets, these will be killed by followup pass.  */
9794   if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
9795     insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
9796
9797   /* If we had any clobbers to add, make a new pattern than contains
9798      them.  Then check to make sure that all of them are dead.  */
9799   if (num_clobbers_to_add)
9800     {
9801       rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9802                                      rtvec_alloc (GET_CODE (pat) == PARALLEL
9803                                                   ? (XVECLEN (pat, 0)
9804                                                      + num_clobbers_to_add)
9805                                                   : num_clobbers_to_add + 1));
9806
9807       if (GET_CODE (pat) == PARALLEL)
9808         for (i = 0; i < XVECLEN (pat, 0); i++)
9809           XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9810       else
9811         XVECEXP (newpat, 0, 0) = pat;
9812
9813       add_clobbers (newpat, insn_code_number);
9814
9815       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9816            i < XVECLEN (newpat, 0); i++)
9817         {
9818           if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
9819               && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9820             return -1;
9821           if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH) 
9822             {
9823               gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
9824               notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9825                                          XEXP (XVECEXP (newpat, 0, i), 0), notes);
9826             }
9827         }
9828       pat = newpat;
9829     }
9830
9831   *pnewpat = pat;
9832   *pnotes = notes;
9833
9834   return insn_code_number;
9835 }
9836 \f
9837 /* Like gen_lowpart_general but for use by combine.  In combine it
9838    is not possible to create any new pseudoregs.  However, it is
9839    safe to create invalid memory addresses, because combine will
9840    try to recognize them and all they will do is make the combine
9841    attempt fail.
9842
9843    If for some reason this cannot do its job, an rtx
9844    (clobber (const_int 0)) is returned.
9845    An insn containing that will not be recognized.  */
9846
9847 static rtx
9848 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
9849 {
9850   enum machine_mode imode = GET_MODE (x);
9851   unsigned int osize = GET_MODE_SIZE (omode);
9852   unsigned int isize = GET_MODE_SIZE (imode);
9853   rtx result;
9854
9855   if (omode == imode)
9856     return x;
9857
9858   /* Return identity if this is a CONST or symbolic reference.  */
9859   if (omode == Pmode
9860       && (GET_CODE (x) == CONST
9861           || GET_CODE (x) == SYMBOL_REF
9862           || GET_CODE (x) == LABEL_REF))
9863     return x;
9864
9865   /* We can only support MODE being wider than a word if X is a
9866      constant integer or has a mode the same size.  */
9867   if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
9868       && ! ((imode == VOIDmode
9869              && (GET_CODE (x) == CONST_INT
9870                  || GET_CODE (x) == CONST_DOUBLE))
9871             || isize == osize))
9872     goto fail;
9873
9874   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
9875      won't know what to do.  So we will strip off the SUBREG here and
9876      process normally.  */
9877   if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
9878     {
9879       x = SUBREG_REG (x);
9880
9881       /* For use in case we fall down into the address adjustments
9882          further below, we need to adjust the known mode and size of
9883          x; imode and isize, since we just adjusted x.  */
9884       imode = GET_MODE (x);
9885
9886       if (imode == omode)
9887         return x;
9888
9889       isize = GET_MODE_SIZE (imode);
9890     }
9891
9892   result = gen_lowpart_common (omode, x);
9893
9894   if (result)
9895     return result;
9896
9897   if (MEM_P (x))
9898     {
9899       int offset = 0;
9900
9901       /* Refuse to work on a volatile memory ref or one with a mode-dependent
9902          address.  */
9903       if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9904         goto fail;
9905
9906       /* If we want to refer to something bigger than the original memref,
9907          generate a paradoxical subreg instead.  That will force a reload
9908          of the original memref X.  */
9909       if (isize < osize)
9910         return gen_rtx_SUBREG (omode, x, 0);
9911
9912       if (WORDS_BIG_ENDIAN)
9913         offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
9914
9915       /* Adjust the address so that the address-after-the-data is
9916          unchanged.  */
9917       if (BYTES_BIG_ENDIAN)
9918         offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
9919
9920       return adjust_address_nv (x, omode, offset);
9921     }
9922
9923   /* If X is a comparison operator, rewrite it in a new mode.  This
9924      probably won't match, but may allow further simplifications.  */
9925   else if (COMPARISON_P (x))
9926     return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
9927
9928   /* If we couldn't simplify X any other way, just enclose it in a
9929      SUBREG.  Normally, this SUBREG won't match, but some patterns may
9930      include an explicit SUBREG or we may simplify it further in combine.  */
9931   else
9932     {
9933       int offset = 0;
9934       rtx res;
9935
9936       offset = subreg_lowpart_offset (omode, imode);
9937       if (imode == VOIDmode)
9938         {
9939           imode = int_mode_for_mode (omode);
9940           x = gen_lowpart_common (imode, x);
9941           if (x == NULL)
9942             goto fail;
9943         }
9944       res = simplify_gen_subreg (omode, x, imode, offset);
9945       if (res)
9946         return res;
9947     }
9948
9949  fail:
9950   return gen_rtx_CLOBBER (imode, const0_rtx);
9951 }
9952 \f
9953 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9954    comparison code that will be tested.
9955
9956    The result is a possibly different comparison code to use.  *POP0 and
9957    *POP1 may be updated.
9958
9959    It is possible that we might detect that a comparison is either always
9960    true or always false.  However, we do not perform general constant
9961    folding in combine, so this knowledge isn't useful.  Such tautologies
9962    should have been detected earlier.  Hence we ignore all such cases.  */
9963
9964 static enum rtx_code
9965 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
9966 {
9967   rtx op0 = *pop0;
9968   rtx op1 = *pop1;
9969   rtx tem, tem1;
9970   int i;
9971   enum machine_mode mode, tmode;
9972
9973   /* Try a few ways of applying the same transformation to both operands.  */
9974   while (1)
9975     {
9976 #ifndef WORD_REGISTER_OPERATIONS
9977       /* The test below this one won't handle SIGN_EXTENDs on these machines,
9978          so check specially.  */
9979       if (code != GTU && code != GEU && code != LTU && code != LEU
9980           && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9981           && GET_CODE (XEXP (op0, 0)) == ASHIFT
9982           && GET_CODE (XEXP (op1, 0)) == ASHIFT
9983           && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9984           && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9985           && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9986               == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9987           && GET_CODE (XEXP (op0, 1)) == CONST_INT
9988           && XEXP (op0, 1) == XEXP (op1, 1)
9989           && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
9990           && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
9991           && (INTVAL (XEXP (op0, 1))
9992               == (GET_MODE_BITSIZE (GET_MODE (op0))
9993                   - (GET_MODE_BITSIZE
9994                      (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9995         {
9996           op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9997           op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9998         }
9999 #endif
10000
10001       /* If both operands are the same constant shift, see if we can ignore the
10002          shift.  We can if the shift is a rotate or if the bits shifted out of
10003          this shift are known to be zero for both inputs and if the type of
10004          comparison is compatible with the shift.  */
10005       if (GET_CODE (op0) == GET_CODE (op1)
10006           && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10007           && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
10008               || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
10009                   && (code != GT && code != LT && code != GE && code != LE))
10010               || (GET_CODE (op0) == ASHIFTRT
10011                   && (code != GTU && code != LTU
10012                       && code != GEU && code != LEU)))
10013           && GET_CODE (XEXP (op0, 1)) == CONST_INT
10014           && INTVAL (XEXP (op0, 1)) >= 0
10015           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10016           && XEXP (op0, 1) == XEXP (op1, 1))
10017         {
10018           enum machine_mode mode = GET_MODE (op0);
10019           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10020           int shift_count = INTVAL (XEXP (op0, 1));
10021
10022           if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
10023             mask &= (mask >> shift_count) << shift_count;
10024           else if (GET_CODE (op0) == ASHIFT)
10025             mask = (mask & (mask << shift_count)) >> shift_count;
10026
10027           if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
10028               && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
10029             op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
10030           else
10031             break;
10032         }
10033
10034       /* If both operands are AND's of a paradoxical SUBREG by constant, the
10035          SUBREGs are of the same mode, and, in both cases, the AND would
10036          be redundant if the comparison was done in the narrower mode,
10037          do the comparison in the narrower mode (e.g., we are AND'ing with 1
10038          and the operand's possibly nonzero bits are 0xffffff01; in that case
10039          if we only care about QImode, we don't need the AND).  This case
10040          occurs if the output mode of an scc insn is not SImode and
10041          STORE_FLAG_VALUE == 1 (e.g., the 386).
10042
10043          Similarly, check for a case where the AND's are ZERO_EXTEND
10044          operations from some narrower mode even though a SUBREG is not
10045          present.  */
10046
10047       else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
10048                && GET_CODE (XEXP (op0, 1)) == CONST_INT
10049                && GET_CODE (XEXP (op1, 1)) == CONST_INT)
10050         {
10051           rtx inner_op0 = XEXP (op0, 0);
10052           rtx inner_op1 = XEXP (op1, 0);
10053           HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
10054           HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
10055           int changed = 0;
10056
10057           if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
10058               && (GET_MODE_SIZE (GET_MODE (inner_op0))
10059                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
10060               && (GET_MODE (SUBREG_REG (inner_op0))
10061                   == GET_MODE (SUBREG_REG (inner_op1)))
10062               && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
10063                   <= HOST_BITS_PER_WIDE_INT)
10064               && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
10065                                              GET_MODE (SUBREG_REG (inner_op0)))))
10066               && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
10067                                              GET_MODE (SUBREG_REG (inner_op1))))))
10068             {
10069               op0 = SUBREG_REG (inner_op0);
10070               op1 = SUBREG_REG (inner_op1);
10071
10072               /* The resulting comparison is always unsigned since we masked
10073                  off the original sign bit.  */
10074               code = unsigned_condition (code);
10075
10076               changed = 1;
10077             }
10078
10079           else if (c0 == c1)
10080             for (tmode = GET_CLASS_NARROWEST_MODE
10081                  (GET_MODE_CLASS (GET_MODE (op0)));
10082                  tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10083               if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10084                 {
10085                   op0 = gen_lowpart (tmode, inner_op0);
10086                   op1 = gen_lowpart (tmode, inner_op1);
10087                   code = unsigned_condition (code);
10088                   changed = 1;
10089                   break;
10090                 }
10091
10092           if (! changed)
10093             break;
10094         }
10095
10096       /* If both operands are NOT, we can strip off the outer operation
10097          and adjust the comparison code for swapped operands; similarly for
10098          NEG, except that this must be an equality comparison.  */
10099       else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10100                || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10101                    && (code == EQ || code == NE)))
10102         op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10103
10104       else
10105         break;
10106     }
10107
10108   /* If the first operand is a constant, swap the operands and adjust the
10109      comparison code appropriately, but don't do this if the second operand
10110      is already a constant integer.  */
10111   if (swap_commutative_operands_p (op0, op1))
10112     {
10113       tem = op0, op0 = op1, op1 = tem;
10114       code = swap_condition (code);
10115     }
10116
10117   /* We now enter a loop during which we will try to simplify the comparison.
10118      For the most part, we only are concerned with comparisons with zero,
10119      but some things may really be comparisons with zero but not start
10120      out looking that way.  */
10121
10122   while (GET_CODE (op1) == CONST_INT)
10123     {
10124       enum machine_mode mode = GET_MODE (op0);
10125       unsigned int mode_width = GET_MODE_BITSIZE (mode);
10126       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10127       int equality_comparison_p;
10128       int sign_bit_comparison_p;
10129       int unsigned_comparison_p;
10130       HOST_WIDE_INT const_op;
10131
10132       /* We only want to handle integral modes.  This catches VOIDmode,
10133          CCmode, and the floating-point modes.  An exception is that we
10134          can handle VOIDmode if OP0 is a COMPARE or a comparison
10135          operation.  */
10136
10137       if (GET_MODE_CLASS (mode) != MODE_INT
10138           && ! (mode == VOIDmode
10139                 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
10140         break;
10141
10142       /* Get the constant we are comparing against and turn off all bits
10143          not on in our mode.  */
10144       const_op = INTVAL (op1);
10145       if (mode != VOIDmode)
10146         const_op = trunc_int_for_mode (const_op, mode);
10147       op1 = GEN_INT (const_op);
10148
10149       /* If we are comparing against a constant power of two and the value
10150          being compared can only have that single bit nonzero (e.g., it was
10151          `and'ed with that bit), we can replace this with a comparison
10152          with zero.  */
10153       if (const_op
10154           && (code == EQ || code == NE || code == GE || code == GEU
10155               || code == LT || code == LTU)
10156           && mode_width <= HOST_BITS_PER_WIDE_INT
10157           && exact_log2 (const_op) >= 0
10158           && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10159         {
10160           code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10161           op1 = const0_rtx, const_op = 0;
10162         }
10163
10164       /* Similarly, if we are comparing a value known to be either -1 or
10165          0 with -1, change it to the opposite comparison against zero.  */
10166
10167       if (const_op == -1
10168           && (code == EQ || code == NE || code == GT || code == LE
10169               || code == GEU || code == LTU)
10170           && num_sign_bit_copies (op0, mode) == mode_width)
10171         {
10172           code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10173           op1 = const0_rtx, const_op = 0;
10174         }
10175
10176       /* Do some canonicalizations based on the comparison code.  We prefer
10177          comparisons against zero and then prefer equality comparisons.
10178          If we can reduce the size of a constant, we will do that too.  */
10179
10180       switch (code)
10181         {
10182         case LT:
10183           /* < C is equivalent to <= (C - 1) */
10184           if (const_op > 0)
10185             {
10186               const_op -= 1;
10187               op1 = GEN_INT (const_op);
10188               code = LE;
10189               /* ... fall through to LE case below.  */
10190             }
10191           else
10192             break;
10193
10194         case LE:
10195           /* <= C is equivalent to < (C + 1); we do this for C < 0  */
10196           if (const_op < 0)
10197             {
10198               const_op += 1;
10199               op1 = GEN_INT (const_op);
10200               code = LT;
10201             }
10202
10203           /* If we are doing a <= 0 comparison on a value known to have
10204              a zero sign bit, we can replace this with == 0.  */
10205           else if (const_op == 0
10206                    && mode_width <= HOST_BITS_PER_WIDE_INT
10207                    && (nonzero_bits (op0, mode)
10208                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10209             code = EQ;
10210           break;
10211
10212         case GE:
10213           /* >= C is equivalent to > (C - 1).  */
10214           if (const_op > 0)
10215             {
10216               const_op -= 1;
10217               op1 = GEN_INT (const_op);
10218               code = GT;
10219               /* ... fall through to GT below.  */
10220             }
10221           else
10222             break;
10223
10224         case GT:
10225           /* > C is equivalent to >= (C + 1); we do this for C < 0.  */
10226           if (const_op < 0)
10227             {
10228               const_op += 1;
10229               op1 = GEN_INT (const_op);
10230               code = GE;
10231             }
10232
10233           /* If we are doing a > 0 comparison on a value known to have
10234              a zero sign bit, we can replace this with != 0.  */
10235           else if (const_op == 0
10236                    && mode_width <= HOST_BITS_PER_WIDE_INT
10237                    && (nonzero_bits (op0, mode)
10238                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10239             code = NE;
10240           break;
10241
10242         case LTU:
10243           /* < C is equivalent to <= (C - 1).  */
10244           if (const_op > 0)
10245             {
10246               const_op -= 1;
10247               op1 = GEN_INT (const_op);
10248               code = LEU;
10249               /* ... fall through ...  */
10250             }
10251
10252           /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
10253           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10254                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10255             {
10256               const_op = 0, op1 = const0_rtx;
10257               code = GE;
10258               break;
10259             }
10260           else
10261             break;
10262
10263         case LEU:
10264           /* unsigned <= 0 is equivalent to == 0 */
10265           if (const_op == 0)
10266             code = EQ;
10267
10268           /* (unsigned) <= 0x7fffffff is equivalent to >= 0.  */
10269           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10270                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10271             {
10272               const_op = 0, op1 = const0_rtx;
10273               code = GE;
10274             }
10275           break;
10276
10277         case GEU:
10278           /* >= C is equivalent to > (C - 1).  */
10279           if (const_op > 1)
10280             {
10281               const_op -= 1;
10282               op1 = GEN_INT (const_op);
10283               code = GTU;
10284               /* ... fall through ...  */
10285             }
10286
10287           /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
10288           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10289                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10290             {
10291               const_op = 0, op1 = const0_rtx;
10292               code = LT;
10293               break;
10294             }
10295           else
10296             break;
10297
10298         case GTU:
10299           /* unsigned > 0 is equivalent to != 0 */
10300           if (const_op == 0)
10301             code = NE;
10302
10303           /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
10304           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10305                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10306             {
10307               const_op = 0, op1 = const0_rtx;
10308               code = LT;
10309             }
10310           break;
10311
10312         default:
10313           break;
10314         }
10315
10316       /* Compute some predicates to simplify code below.  */
10317
10318       equality_comparison_p = (code == EQ || code == NE);
10319       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
10320       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
10321                                || code == GEU);
10322
10323       /* If this is a sign bit comparison and we can do arithmetic in
10324          MODE, say that we will only be needing the sign bit of OP0.  */
10325       if (sign_bit_comparison_p
10326           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10327         op0 = force_to_mode (op0, mode,
10328                              ((HOST_WIDE_INT) 1
10329                               << (GET_MODE_BITSIZE (mode) - 1)),
10330                              0);
10331
10332       /* Now try cases based on the opcode of OP0.  If none of the cases
10333          does a "continue", we exit this loop immediately after the
10334          switch.  */
10335
10336       switch (GET_CODE (op0))
10337         {
10338         case ZERO_EXTRACT:
10339           /* If we are extracting a single bit from a variable position in
10340              a constant that has only a single bit set and are comparing it
10341              with zero, we can convert this into an equality comparison
10342              between the position and the location of the single bit.  */
10343           /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
10344              have already reduced the shift count modulo the word size.  */
10345           if (!SHIFT_COUNT_TRUNCATED
10346               && GET_CODE (XEXP (op0, 0)) == CONST_INT
10347               && XEXP (op0, 1) == const1_rtx
10348               && equality_comparison_p && const_op == 0
10349               && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
10350             {
10351               if (BITS_BIG_ENDIAN)
10352                 {
10353                   enum machine_mode new_mode
10354                     = mode_for_extraction (EP_extzv, 1);
10355                   if (new_mode == MAX_MACHINE_MODE)
10356                     i = BITS_PER_WORD - 1 - i;
10357                   else
10358                     {
10359                       mode = new_mode;
10360                       i = (GET_MODE_BITSIZE (mode) - 1 - i);
10361                     }
10362                 }
10363
10364               op0 = XEXP (op0, 2);
10365               op1 = GEN_INT (i);
10366               const_op = i;
10367
10368               /* Result is nonzero iff shift count is equal to I.  */
10369               code = reverse_condition (code);
10370               continue;
10371             }
10372
10373           /* ... fall through ...  */
10374
10375         case SIGN_EXTRACT:
10376           tem = expand_compound_operation (op0);
10377           if (tem != op0)
10378             {
10379               op0 = tem;
10380               continue;
10381             }
10382           break;
10383
10384         case NOT:
10385           /* If testing for equality, we can take the NOT of the constant.  */
10386           if (equality_comparison_p
10387               && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10388             {
10389               op0 = XEXP (op0, 0);
10390               op1 = tem;
10391               continue;
10392             }
10393
10394           /* If just looking at the sign bit, reverse the sense of the
10395              comparison.  */
10396           if (sign_bit_comparison_p)
10397             {
10398               op0 = XEXP (op0, 0);
10399               code = (code == GE ? LT : GE);
10400               continue;
10401             }
10402           break;
10403
10404         case NEG:
10405           /* If testing for equality, we can take the NEG of the constant.  */
10406           if (equality_comparison_p
10407               && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10408             {
10409               op0 = XEXP (op0, 0);
10410               op1 = tem;
10411               continue;
10412             }
10413
10414           /* The remaining cases only apply to comparisons with zero.  */
10415           if (const_op != 0)
10416             break;
10417
10418           /* When X is ABS or is known positive,
10419              (neg X) is < 0 if and only if X != 0.  */
10420
10421           if (sign_bit_comparison_p
10422               && (GET_CODE (XEXP (op0, 0)) == ABS
10423                   || (mode_width <= HOST_BITS_PER_WIDE_INT
10424                       && (nonzero_bits (XEXP (op0, 0), mode)
10425                           & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10426             {
10427               op0 = XEXP (op0, 0);
10428               code = (code == LT ? NE : EQ);
10429               continue;
10430             }
10431
10432           /* If we have NEG of something whose two high-order bits are the
10433              same, we know that "(-a) < 0" is equivalent to "a > 0".  */
10434           if (num_sign_bit_copies (op0, mode) >= 2)
10435             {
10436               op0 = XEXP (op0, 0);
10437               code = swap_condition (code);
10438               continue;
10439             }
10440           break;
10441
10442         case ROTATE:
10443           /* If we are testing equality and our count is a constant, we
10444              can perform the inverse operation on our RHS.  */
10445           if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10446               && (tem = simplify_binary_operation (ROTATERT, mode,
10447                                                    op1, XEXP (op0, 1))) != 0)
10448             {
10449               op0 = XEXP (op0, 0);
10450               op1 = tem;
10451               continue;
10452             }
10453
10454           /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10455              a particular bit.  Convert it to an AND of a constant of that
10456              bit.  This will be converted into a ZERO_EXTRACT.  */
10457           if (const_op == 0 && sign_bit_comparison_p
10458               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10459               && mode_width <= HOST_BITS_PER_WIDE_INT)
10460             {
10461               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10462                                             ((HOST_WIDE_INT) 1
10463                                              << (mode_width - 1
10464                                                  - INTVAL (XEXP (op0, 1)))));
10465               code = (code == LT ? NE : EQ);
10466               continue;
10467             }
10468
10469           /* Fall through.  */
10470
10471         case ABS:
10472           /* ABS is ignorable inside an equality comparison with zero.  */
10473           if (const_op == 0 && equality_comparison_p)
10474             {
10475               op0 = XEXP (op0, 0);
10476               continue;
10477             }
10478           break;
10479
10480         case SIGN_EXTEND:
10481           /* Can simplify (compare (zero/sign_extend FOO) CONST) to
10482              (compare FOO CONST) if CONST fits in FOO's mode and we
10483              are either testing inequality or have an unsigned
10484              comparison with ZERO_EXTEND or a signed comparison with
10485              SIGN_EXTEND.  But don't do it if we don't have a compare
10486              insn of the given mode, since we'd have to revert it
10487              later on, and then we wouldn't know whether to sign- or
10488              zero-extend.  */
10489           mode = GET_MODE (XEXP (op0, 0));
10490           if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10491               && ! unsigned_comparison_p
10492               && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10493               && ((unsigned HOST_WIDE_INT) const_op
10494                   < (((unsigned HOST_WIDE_INT) 1
10495                       << (GET_MODE_BITSIZE (mode) - 1))))
10496               && optab_handler (cmp_optab, mode)->insn_code != CODE_FOR_nothing)
10497             {
10498               op0 = XEXP (op0, 0);
10499               continue;
10500             }
10501           break;
10502
10503         case SUBREG:
10504           /* Check for the case where we are comparing A - C1 with C2, that is
10505
10506                (subreg:MODE (plus (A) (-C1))) op (C2)
10507
10508              with C1 a constant, and try to lift the SUBREG, i.e. to do the
10509              comparison in the wider mode.  One of the following two conditions
10510              must be true in order for this to be valid:
10511
10512                1. The mode extension results in the same bit pattern being added
10513                   on both sides and the comparison is equality or unsigned.  As
10514                   C2 has been truncated to fit in MODE, the pattern can only be
10515                   all 0s or all 1s.
10516
10517                2. The mode extension results in the sign bit being copied on
10518                   each side.
10519
10520              The difficulty here is that we have predicates for A but not for
10521              (A - C1) so we need to check that C1 is within proper bounds so
10522              as to perturbate A as little as possible.  */
10523
10524           if (mode_width <= HOST_BITS_PER_WIDE_INT
10525               && subreg_lowpart_p (op0)
10526               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
10527               && GET_CODE (SUBREG_REG (op0)) == PLUS
10528               && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT)
10529             {
10530               enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
10531               rtx a = XEXP (SUBREG_REG (op0), 0);
10532               HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
10533
10534               if ((c1 > 0
10535                    && (unsigned HOST_WIDE_INT) c1
10536                        < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
10537                    && (equality_comparison_p || unsigned_comparison_p)
10538                    /* (A - C1) zero-extends if it is positive and sign-extends
10539                       if it is negative, C2 both zero- and sign-extends.  */
10540                    && ((0 == (nonzero_bits (a, inner_mode)
10541                               & ~GET_MODE_MASK (mode))
10542                         && const_op >= 0)
10543                        /* (A - C1) sign-extends if it is positive and 1-extends
10544                           if it is negative, C2 both sign- and 1-extends.  */
10545                        || (num_sign_bit_copies (a, inner_mode)
10546                            > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10547                                              - mode_width)
10548                            && const_op < 0)))
10549                   || ((unsigned HOST_WIDE_INT) c1
10550                        < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
10551                       /* (A - C1) always sign-extends, like C2.  */
10552                       && num_sign_bit_copies (a, inner_mode)
10553                          > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10554                                            - (mode_width - 1))))
10555                 {
10556                   op0 = SUBREG_REG (op0);
10557                   continue;
10558                 }
10559             }
10560
10561           /* If the inner mode is narrower and we are extracting the low part,
10562              we can treat the SUBREG as if it were a ZERO_EXTEND.  */
10563           if (subreg_lowpart_p (op0)
10564               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10565             /* Fall through */ ;
10566           else
10567             break;
10568
10569           /* ... fall through ...  */
10570
10571         case ZERO_EXTEND:
10572           mode = GET_MODE (XEXP (op0, 0));
10573           if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10574               && (unsigned_comparison_p || equality_comparison_p)
10575               && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10576               && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
10577               && optab_handler (cmp_optab, mode)->insn_code != CODE_FOR_nothing)
10578             {
10579               op0 = XEXP (op0, 0);
10580               continue;
10581             }
10582           break;
10583
10584         case PLUS:
10585           /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
10586              this for equality comparisons due to pathological cases involving
10587              overflows.  */
10588           if (equality_comparison_p
10589               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10590                                                         op1, XEXP (op0, 1))))
10591             {
10592               op0 = XEXP (op0, 0);
10593               op1 = tem;
10594               continue;
10595             }
10596
10597           /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
10598           if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10599               && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10600             {
10601               op0 = XEXP (XEXP (op0, 0), 0);
10602               code = (code == LT ? EQ : NE);
10603               continue;
10604             }
10605           break;
10606
10607         case MINUS:
10608           /* We used to optimize signed comparisons against zero, but that
10609              was incorrect.  Unsigned comparisons against zero (GTU, LEU)
10610              arrive here as equality comparisons, or (GEU, LTU) are
10611              optimized away.  No need to special-case them.  */
10612
10613           /* (eq (minus A B) C) -> (eq A (plus B C)) or
10614              (eq B (minus A C)), whichever simplifies.  We can only do
10615              this for equality comparisons due to pathological cases involving
10616              overflows.  */
10617           if (equality_comparison_p
10618               && 0 != (tem = simplify_binary_operation (PLUS, mode,
10619                                                         XEXP (op0, 1), op1)))
10620             {
10621               op0 = XEXP (op0, 0);
10622               op1 = tem;
10623               continue;
10624             }
10625
10626           if (equality_comparison_p
10627               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10628                                                         XEXP (op0, 0), op1)))
10629             {
10630               op0 = XEXP (op0, 1);
10631               op1 = tem;
10632               continue;
10633             }
10634
10635           /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10636              of bits in X minus 1, is one iff X > 0.  */
10637           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10638               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10639               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10640                  == mode_width - 1
10641               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10642             {
10643               op0 = XEXP (op0, 1);
10644               code = (code == GE ? LE : GT);
10645               continue;
10646             }
10647           break;
10648
10649         case XOR:
10650           /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
10651              if C is zero or B is a constant.  */
10652           if (equality_comparison_p
10653               && 0 != (tem = simplify_binary_operation (XOR, mode,
10654                                                         XEXP (op0, 1), op1)))
10655             {
10656               op0 = XEXP (op0, 0);
10657               op1 = tem;
10658               continue;
10659             }
10660           break;
10661
10662         case EQ:  case NE:
10663         case UNEQ:  case LTGT:
10664         case LT:  case LTU:  case UNLT:  case LE:  case LEU:  case UNLE:
10665         case GT:  case GTU:  case UNGT:  case GE:  case GEU:  case UNGE:
10666         case UNORDERED: case ORDERED:
10667           /* We can't do anything if OP0 is a condition code value, rather
10668              than an actual data value.  */
10669           if (const_op != 0
10670               || CC0_P (XEXP (op0, 0))
10671               || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10672             break;
10673
10674           /* Get the two operands being compared.  */
10675           if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10676             tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10677           else
10678             tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10679
10680           /* Check for the cases where we simply want the result of the
10681              earlier test or the opposite of that result.  */
10682           if (code == NE || code == EQ
10683               || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10684                   && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10685                   && (STORE_FLAG_VALUE
10686                       & (((HOST_WIDE_INT) 1
10687                           << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10688                   && (code == LT || code == GE)))
10689             {
10690               enum rtx_code new_code;
10691               if (code == LT || code == NE)
10692                 new_code = GET_CODE (op0);
10693               else
10694                 new_code = reversed_comparison_code (op0, NULL);
10695
10696               if (new_code != UNKNOWN)
10697                 {
10698                   code = new_code;
10699                   op0 = tem;
10700                   op1 = tem1;
10701                   continue;
10702                 }
10703             }
10704           break;
10705
10706         case IOR:
10707           /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10708              iff X <= 0.  */
10709           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10710               && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10711               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10712             {
10713               op0 = XEXP (op0, 1);
10714               code = (code == GE ? GT : LE);
10715               continue;
10716             }
10717           break;
10718
10719         case AND:
10720           /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
10721              will be converted to a ZERO_EXTRACT later.  */
10722           if (const_op == 0 && equality_comparison_p
10723               && GET_CODE (XEXP (op0, 0)) == ASHIFT
10724               && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10725             {
10726               op0 = simplify_and_const_int
10727                 (NULL_RTX, mode, gen_rtx_LSHIFTRT (mode,
10728                                                    XEXP (op0, 1),
10729                                                    XEXP (XEXP (op0, 0), 1)),
10730                  (HOST_WIDE_INT) 1);
10731               continue;
10732             }
10733
10734           /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10735              zero and X is a comparison and C1 and C2 describe only bits set
10736              in STORE_FLAG_VALUE, we can compare with X.  */
10737           if (const_op == 0 && equality_comparison_p
10738               && mode_width <= HOST_BITS_PER_WIDE_INT
10739               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10740               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10741               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10742               && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10743               && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10744             {
10745               mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10746                       << INTVAL (XEXP (XEXP (op0, 0), 1)));
10747               if ((~STORE_FLAG_VALUE & mask) == 0
10748                   && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
10749                       || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10750                           && COMPARISON_P (tem))))
10751                 {
10752                   op0 = XEXP (XEXP (op0, 0), 0);
10753                   continue;
10754                 }
10755             }
10756
10757           /* If we are doing an equality comparison of an AND of a bit equal
10758              to the sign bit, replace this with a LT or GE comparison of
10759              the underlying value.  */
10760           if (equality_comparison_p
10761               && const_op == 0
10762               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10763               && mode_width <= HOST_BITS_PER_WIDE_INT
10764               && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10765                   == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10766             {
10767               op0 = XEXP (op0, 0);
10768               code = (code == EQ ? GE : LT);
10769               continue;
10770             }
10771
10772           /* If this AND operation is really a ZERO_EXTEND from a narrower
10773              mode, the constant fits within that mode, and this is either an
10774              equality or unsigned comparison, try to do this comparison in
10775              the narrower mode.
10776
10777              Note that in:
10778
10779              (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
10780              -> (ne:DI (reg:SI 4) (const_int 0))
10781
10782              unless TRULY_NOOP_TRUNCATION allows it or the register is
10783              known to hold a value of the required mode the
10784              transformation is invalid.  */
10785           if ((equality_comparison_p || unsigned_comparison_p)
10786               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10787               && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10788                                    & GET_MODE_MASK (mode))
10789                                   + 1)) >= 0
10790               && const_op >> i == 0
10791               && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
10792               && (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
10793                                          GET_MODE_BITSIZE (GET_MODE (op0)))
10794                   || (REG_P (XEXP (op0, 0))
10795                       && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
10796             {
10797               op0 = gen_lowpart (tmode, XEXP (op0, 0));
10798               continue;
10799             }
10800
10801           /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
10802              fits in both M1 and M2 and the SUBREG is either paradoxical
10803              or represents the low part, permute the SUBREG and the AND
10804              and try again.  */
10805           if (GET_CODE (XEXP (op0, 0)) == SUBREG)
10806             {
10807               unsigned HOST_WIDE_INT c1;
10808               tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
10809               /* Require an integral mode, to avoid creating something like
10810                  (AND:SF ...).  */
10811               if (SCALAR_INT_MODE_P (tmode)
10812                   /* It is unsafe to commute the AND into the SUBREG if the
10813                      SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
10814                      not defined.  As originally written the upper bits
10815                      have a defined value due to the AND operation.
10816                      However, if we commute the AND inside the SUBREG then
10817                      they no longer have defined values and the meaning of
10818                      the code has been changed.  */
10819                   && (0
10820 #ifdef WORD_REGISTER_OPERATIONS
10821                       || (mode_width > GET_MODE_BITSIZE (tmode)
10822                           && mode_width <= BITS_PER_WORD)
10823 #endif
10824                       || (mode_width <= GET_MODE_BITSIZE (tmode)
10825                           && subreg_lowpart_p (XEXP (op0, 0))))
10826                   && GET_CODE (XEXP (op0, 1)) == CONST_INT
10827                   && mode_width <= HOST_BITS_PER_WIDE_INT
10828                   && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
10829                   && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
10830                   && (c1 & ~GET_MODE_MASK (tmode)) == 0
10831                   && c1 != mask
10832                   && c1 != GET_MODE_MASK (tmode))
10833                 {
10834                   op0 = simplify_gen_binary (AND, tmode,
10835                                              SUBREG_REG (XEXP (op0, 0)),
10836                                              gen_int_mode (c1, tmode));
10837                   op0 = gen_lowpart (mode, op0);
10838                   continue;
10839                 }
10840             }
10841
10842           /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0).  */
10843           if (const_op == 0 && equality_comparison_p
10844               && XEXP (op0, 1) == const1_rtx
10845               && GET_CODE (XEXP (op0, 0)) == NOT)
10846             {
10847               op0 = simplify_and_const_int
10848                 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
10849               code = (code == NE ? EQ : NE);
10850               continue;
10851             }
10852
10853           /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
10854              (eq (and (lshiftrt X) 1) 0).
10855              Also handle the case where (not X) is expressed using xor.  */
10856           if (const_op == 0 && equality_comparison_p
10857               && XEXP (op0, 1) == const1_rtx
10858               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
10859             {
10860               rtx shift_op = XEXP (XEXP (op0, 0), 0);
10861               rtx shift_count = XEXP (XEXP (op0, 0), 1);
10862
10863               if (GET_CODE (shift_op) == NOT
10864                   || (GET_CODE (shift_op) == XOR
10865                       && GET_CODE (XEXP (shift_op, 1)) == CONST_INT
10866                       && GET_CODE (shift_count) == CONST_INT
10867                       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10868                       && (INTVAL (XEXP (shift_op, 1))
10869                           == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
10870                 {
10871                   op0 = simplify_and_const_int
10872                     (NULL_RTX, mode,
10873                      gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
10874                      (HOST_WIDE_INT) 1);
10875                   code = (code == NE ? EQ : NE);
10876                   continue;
10877                 }
10878             }
10879           break;
10880
10881         case ASHIFT:
10882           /* If we have (compare (ashift FOO N) (const_int C)) and
10883              the high order N bits of FOO (N+1 if an inequality comparison)
10884              are known to be zero, we can do this by comparing FOO with C
10885              shifted right N bits so long as the low-order N bits of C are
10886              zero.  */
10887           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10888               && INTVAL (XEXP (op0, 1)) >= 0
10889               && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10890                   < HOST_BITS_PER_WIDE_INT)
10891               && ((const_op
10892                    & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10893               && mode_width <= HOST_BITS_PER_WIDE_INT
10894               && (nonzero_bits (XEXP (op0, 0), mode)
10895                   & ~(mask >> (INTVAL (XEXP (op0, 1))
10896                                + ! equality_comparison_p))) == 0)
10897             {
10898               /* We must perform a logical shift, not an arithmetic one,
10899                  as we want the top N bits of C to be zero.  */
10900               unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
10901
10902               temp >>= INTVAL (XEXP (op0, 1));
10903               op1 = gen_int_mode (temp, mode);
10904               op0 = XEXP (op0, 0);
10905               continue;
10906             }
10907
10908           /* If we are doing a sign bit comparison, it means we are testing
10909              a particular bit.  Convert it to the appropriate AND.  */
10910           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10911               && mode_width <= HOST_BITS_PER_WIDE_INT)
10912             {
10913               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10914                                             ((HOST_WIDE_INT) 1
10915                                              << (mode_width - 1
10916                                                  - INTVAL (XEXP (op0, 1)))));
10917               code = (code == LT ? NE : EQ);
10918               continue;
10919             }
10920
10921           /* If this an equality comparison with zero and we are shifting
10922              the low bit to the sign bit, we can convert this to an AND of the
10923              low-order bit.  */
10924           if (const_op == 0 && equality_comparison_p
10925               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10926               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10927                  == mode_width - 1)
10928             {
10929               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10930                                             (HOST_WIDE_INT) 1);
10931               continue;
10932             }
10933           break;
10934
10935         case ASHIFTRT:
10936           /* If this is an equality comparison with zero, we can do this
10937              as a logical shift, which might be much simpler.  */
10938           if (equality_comparison_p && const_op == 0
10939               && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10940             {
10941               op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10942                                           XEXP (op0, 0),
10943                                           INTVAL (XEXP (op0, 1)));
10944               continue;
10945             }
10946
10947           /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10948              do the comparison in a narrower mode.  */
10949           if (! unsigned_comparison_p
10950               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10951               && GET_CODE (XEXP (op0, 0)) == ASHIFT
10952               && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10953               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10954                                          MODE_INT, 1)) != BLKmode
10955               && (((unsigned HOST_WIDE_INT) const_op
10956                    + (GET_MODE_MASK (tmode) >> 1) + 1)
10957                   <= GET_MODE_MASK (tmode)))
10958             {
10959               op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
10960               continue;
10961             }
10962
10963           /* Likewise if OP0 is a PLUS of a sign extension with a
10964              constant, which is usually represented with the PLUS
10965              between the shifts.  */
10966           if (! unsigned_comparison_p
10967               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10968               && GET_CODE (XEXP (op0, 0)) == PLUS
10969               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10970               && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
10971               && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
10972               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10973                                          MODE_INT, 1)) != BLKmode
10974               && (((unsigned HOST_WIDE_INT) const_op
10975                    + (GET_MODE_MASK (tmode) >> 1) + 1)
10976                   <= GET_MODE_MASK (tmode)))
10977             {
10978               rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
10979               rtx add_const = XEXP (XEXP (op0, 0), 1);
10980               rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
10981                                                    add_const, XEXP (op0, 1));
10982
10983               op0 = simplify_gen_binary (PLUS, tmode,
10984                                          gen_lowpart (tmode, inner),
10985                                          new_const);
10986               continue;
10987             }
10988
10989           /* ... fall through ...  */
10990         case LSHIFTRT:
10991           /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10992              the low order N bits of FOO are known to be zero, we can do this
10993              by comparing FOO with C shifted left N bits so long as no
10994              overflow occurs.  */
10995           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10996               && INTVAL (XEXP (op0, 1)) >= 0
10997               && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10998               && mode_width <= HOST_BITS_PER_WIDE_INT
10999               && (nonzero_bits (XEXP (op0, 0), mode)
11000                   & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
11001               && (((unsigned HOST_WIDE_INT) const_op
11002                    + (GET_CODE (op0) != LSHIFTRT
11003                       ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11004                          + 1)
11005                       : 0))
11006                   <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11007             {
11008               /* If the shift was logical, then we must make the condition
11009                  unsigned.  */
11010               if (GET_CODE (op0) == LSHIFTRT)
11011                 code = unsigned_condition (code);
11012
11013               const_op <<= INTVAL (XEXP (op0, 1));
11014               op1 = GEN_INT (const_op);
11015               op0 = XEXP (op0, 0);
11016               continue;
11017             }
11018
11019           /* If we are using this shift to extract just the sign bit, we
11020              can replace this with an LT or GE comparison.  */
11021           if (const_op == 0
11022               && (equality_comparison_p || sign_bit_comparison_p)
11023               && GET_CODE (XEXP (op0, 1)) == CONST_INT
11024               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11025                  == mode_width - 1)
11026             {
11027               op0 = XEXP (op0, 0);
11028               code = (code == NE || code == GT ? LT : GE);
11029               continue;
11030             }
11031           break;
11032
11033         default:
11034           break;
11035         }
11036
11037       break;
11038     }
11039
11040   /* Now make any compound operations involved in this comparison.  Then,
11041      check for an outmost SUBREG on OP0 that is not doing anything or is
11042      paradoxical.  The latter transformation must only be performed when
11043      it is known that the "extra" bits will be the same in op0 and op1 or
11044      that they don't matter.  There are three cases to consider:
11045
11046      1. SUBREG_REG (op0) is a register.  In this case the bits are don't
11047      care bits and we can assume they have any convenient value.  So
11048      making the transformation is safe.
11049
11050      2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11051      In this case the upper bits of op0 are undefined.  We should not make
11052      the simplification in that case as we do not know the contents of
11053      those bits.
11054
11055      3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11056      UNKNOWN.  In that case we know those bits are zeros or ones.  We must
11057      also be sure that they are the same as the upper bits of op1.
11058
11059      We can never remove a SUBREG for a non-equality comparison because
11060      the sign bit is in a different place in the underlying object.  */
11061
11062   op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11063   op1 = make_compound_operation (op1, SET);
11064
11065   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11066       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11067       && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11068       && (code == NE || code == EQ))
11069     {
11070       if (GET_MODE_SIZE (GET_MODE (op0))
11071           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
11072         {
11073           /* For paradoxical subregs, allow case 1 as above.  Case 3 isn't
11074              implemented.  */
11075           if (REG_P (SUBREG_REG (op0)))
11076             {
11077               op0 = SUBREG_REG (op0);
11078               op1 = gen_lowpart (GET_MODE (op0), op1);
11079             }
11080         }
11081       else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11082                 <= HOST_BITS_PER_WIDE_INT)
11083                && (nonzero_bits (SUBREG_REG (op0),
11084                                  GET_MODE (SUBREG_REG (op0)))
11085                    & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11086         {
11087           tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11088
11089           if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11090                & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11091             op0 = SUBREG_REG (op0), op1 = tem;
11092         }
11093     }
11094
11095   /* We now do the opposite procedure: Some machines don't have compare
11096      insns in all modes.  If OP0's mode is an integer mode smaller than a
11097      word and we can't do a compare in that mode, see if there is a larger
11098      mode for which we can do the compare.  There are a number of cases in
11099      which we can use the wider mode.  */
11100
11101   mode = GET_MODE (op0);
11102   if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11103       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11104       && ! have_insn_for (COMPARE, mode))
11105     for (tmode = GET_MODE_WIDER_MODE (mode);
11106          (tmode != VOIDmode
11107           && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11108          tmode = GET_MODE_WIDER_MODE (tmode))
11109       if (have_insn_for (COMPARE, tmode))
11110         {
11111           int zero_extended;
11112
11113           /* If the only nonzero bits in OP0 and OP1 are those in the
11114              narrower mode and this is an equality or unsigned comparison,
11115              we can use the wider mode.  Similarly for sign-extended
11116              values, in which case it is true for all comparisons.  */
11117           zero_extended = ((code == EQ || code == NE
11118                             || code == GEU || code == GTU
11119                             || code == LEU || code == LTU)
11120                            && (nonzero_bits (op0, tmode)
11121                                & ~GET_MODE_MASK (mode)) == 0
11122                            && ((GET_CODE (op1) == CONST_INT
11123                                 || (nonzero_bits (op1, tmode)
11124                                     & ~GET_MODE_MASK (mode)) == 0)));
11125
11126           if (zero_extended
11127               || ((num_sign_bit_copies (op0, tmode)
11128                    > (unsigned int) (GET_MODE_BITSIZE (tmode)
11129                                      - GET_MODE_BITSIZE (mode)))
11130                   && (num_sign_bit_copies (op1, tmode)
11131                       > (unsigned int) (GET_MODE_BITSIZE (tmode)
11132                                         - GET_MODE_BITSIZE (mode)))))
11133             {
11134               /* If OP0 is an AND and we don't have an AND in MODE either,
11135                  make a new AND in the proper mode.  */
11136               if (GET_CODE (op0) == AND
11137                   && !have_insn_for (AND, mode))
11138                 op0 = simplify_gen_binary (AND, tmode,
11139                                            gen_lowpart (tmode,
11140                                                         XEXP (op0, 0)),
11141                                            gen_lowpart (tmode,
11142                                                         XEXP (op0, 1)));
11143
11144               op0 = gen_lowpart (tmode, op0);
11145               if (zero_extended && GET_CODE (op1) == CONST_INT)
11146                 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
11147               op1 = gen_lowpart (tmode, op1);
11148               break;
11149             }
11150
11151           /* If this is a test for negative, we can make an explicit
11152              test of the sign bit.  */
11153
11154           if (op1 == const0_rtx && (code == LT || code == GE)
11155               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11156             {
11157               op0 = simplify_gen_binary (AND, tmode,
11158                                          gen_lowpart (tmode, op0),
11159                                          GEN_INT ((HOST_WIDE_INT) 1
11160                                                   << (GET_MODE_BITSIZE (mode)
11161                                                       - 1)));
11162               code = (code == LT) ? NE : EQ;
11163               break;
11164             }
11165         }
11166
11167 #ifdef CANONICALIZE_COMPARISON
11168   /* If this machine only supports a subset of valid comparisons, see if we
11169      can convert an unsupported one into a supported one.  */
11170   CANONICALIZE_COMPARISON (code, op0, op1);
11171 #endif
11172
11173   *pop0 = op0;
11174   *pop1 = op1;
11175
11176   return code;
11177 }
11178 \f
11179 /* Utility function for record_value_for_reg.  Count number of
11180    rtxs in X.  */
11181 static int
11182 count_rtxs (rtx x)
11183 {
11184   enum rtx_code code = GET_CODE (x);
11185   const char *fmt;
11186   int i, ret = 1;
11187
11188   if (GET_RTX_CLASS (code) == '2'
11189       || GET_RTX_CLASS (code) == 'c')
11190     {
11191       rtx x0 = XEXP (x, 0);
11192       rtx x1 = XEXP (x, 1);
11193
11194       if (x0 == x1)
11195         return 1 + 2 * count_rtxs (x0);
11196
11197       if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11198            || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11199           && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11200         return 2 + 2 * count_rtxs (x0)
11201                + count_rtxs (x == XEXP (x1, 0)
11202                              ? XEXP (x1, 1) : XEXP (x1, 0));
11203
11204       if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11205            || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11206           && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11207         return 2 + 2 * count_rtxs (x1)
11208                + count_rtxs (x == XEXP (x0, 0)
11209                              ? XEXP (x0, 1) : XEXP (x0, 0));
11210     }
11211
11212   fmt = GET_RTX_FORMAT (code);
11213   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11214     if (fmt[i] == 'e')
11215       ret += count_rtxs (XEXP (x, i));
11216
11217   return ret;
11218 }
11219 \f
11220 /* Utility function for following routine.  Called when X is part of a value
11221    being stored into last_set_value.  Sets last_set_table_tick
11222    for each register mentioned.  Similar to mention_regs in cse.c  */
11223
11224 static void
11225 update_table_tick (rtx x)
11226 {
11227   enum rtx_code code = GET_CODE (x);
11228   const char *fmt = GET_RTX_FORMAT (code);
11229   int i;
11230
11231   if (code == REG)
11232     {
11233       unsigned int regno = REGNO (x);
11234       unsigned int endregno = END_REGNO (x);
11235       unsigned int r;
11236
11237       for (r = regno; r < endregno; r++)
11238         {
11239           reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, r);
11240           rsp->last_set_table_tick = label_tick;
11241         }
11242
11243       return;
11244     }
11245
11246   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11247     /* Note that we can't have an "E" in values stored; see
11248        get_last_value_validate.  */
11249     if (fmt[i] == 'e')
11250       {
11251         /* Check for identical subexpressions.  If x contains
11252            identical subexpression we only have to traverse one of
11253            them.  */
11254         if (i == 0 && ARITHMETIC_P (x))
11255           {
11256             /* Note that at this point x1 has already been
11257                processed.  */
11258             rtx x0 = XEXP (x, 0);
11259             rtx x1 = XEXP (x, 1);
11260
11261             /* If x0 and x1 are identical then there is no need to
11262                process x0.  */
11263             if (x0 == x1)
11264               break;
11265
11266             /* If x0 is identical to a subexpression of x1 then while
11267                processing x1, x0 has already been processed.  Thus we
11268                are done with x.  */
11269             if (ARITHMETIC_P (x1)
11270                 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11271               break;
11272
11273             /* If x1 is identical to a subexpression of x0 then we
11274                still have to process the rest of x0.  */
11275             if (ARITHMETIC_P (x0)
11276                 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11277               {
11278                 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
11279                 break;
11280               }
11281           }
11282
11283         update_table_tick (XEXP (x, i));
11284       }
11285 }
11286
11287 /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
11288    are saying that the register is clobbered and we no longer know its
11289    value.  If INSN is zero, don't update reg_stat[].last_set; this is
11290    only permitted with VALUE also zero and is used to invalidate the
11291    register.  */
11292
11293 static void
11294 record_value_for_reg (rtx reg, rtx insn, rtx value)
11295 {
11296   unsigned int regno = REGNO (reg);
11297   unsigned int endregno = END_REGNO (reg);
11298   unsigned int i;
11299   reg_stat_type *rsp;
11300
11301   /* If VALUE contains REG and we have a previous value for REG, substitute
11302      the previous value.  */
11303   if (value && insn && reg_overlap_mentioned_p (reg, value))
11304     {
11305       rtx tem;
11306
11307       /* Set things up so get_last_value is allowed to see anything set up to
11308          our insn.  */
11309       subst_low_luid = DF_INSN_LUID (insn);
11310       tem = get_last_value (reg);
11311
11312       /* If TEM is simply a binary operation with two CLOBBERs as operands,
11313          it isn't going to be useful and will take a lot of time to process,
11314          so just use the CLOBBER.  */
11315
11316       if (tem)
11317         {
11318           if (ARITHMETIC_P (tem)
11319               && GET_CODE (XEXP (tem, 0)) == CLOBBER
11320               && GET_CODE (XEXP (tem, 1)) == CLOBBER)
11321             tem = XEXP (tem, 0);
11322           else if (count_occurrences (value, reg, 1) >= 2)
11323             {
11324               /* If there are two or more occurrences of REG in VALUE,
11325                  prevent the value from growing too much.  */
11326               if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
11327                 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
11328             }
11329
11330           value = replace_rtx (copy_rtx (value), reg, tem);
11331         }
11332     }
11333
11334   /* For each register modified, show we don't know its value, that
11335      we don't know about its bitwise content, that its value has been
11336      updated, and that we don't know the location of the death of the
11337      register.  */
11338   for (i = regno; i < endregno; i++)
11339     {
11340       rsp = VEC_index (reg_stat_type, reg_stat, i);
11341
11342       if (insn)
11343         rsp->last_set = insn;
11344
11345       rsp->last_set_value = 0;
11346       rsp->last_set_mode = 0;
11347       rsp->last_set_nonzero_bits = 0;
11348       rsp->last_set_sign_bit_copies = 0;
11349       rsp->last_death = 0;
11350       rsp->truncated_to_mode = 0;
11351     }
11352
11353   /* Mark registers that are being referenced in this value.  */
11354   if (value)
11355     update_table_tick (value);
11356
11357   /* Now update the status of each register being set.
11358      If someone is using this register in this block, set this register
11359      to invalid since we will get confused between the two lives in this
11360      basic block.  This makes using this register always invalid.  In cse, we
11361      scan the table to invalidate all entries using this register, but this
11362      is too much work for us.  */
11363
11364   for (i = regno; i < endregno; i++)
11365     {
11366       rsp = VEC_index (reg_stat_type, reg_stat, i);
11367       rsp->last_set_label = label_tick;
11368       if (!insn
11369           || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
11370         rsp->last_set_invalid = 1;
11371       else
11372         rsp->last_set_invalid = 0;
11373     }
11374
11375   /* The value being assigned might refer to X (like in "x++;").  In that
11376      case, we must replace it with (clobber (const_int 0)) to prevent
11377      infinite loops.  */
11378   rsp = VEC_index (reg_stat_type, reg_stat, regno);
11379   if (value && ! get_last_value_validate (&value, insn,
11380                                           rsp->last_set_label, 0))
11381     {
11382       value = copy_rtx (value);
11383       if (! get_last_value_validate (&value, insn,
11384                                      rsp->last_set_label, 1))
11385         value = 0;
11386     }
11387
11388   /* For the main register being modified, update the value, the mode, the
11389      nonzero bits, and the number of sign bit copies.  */
11390
11391   rsp->last_set_value = value;
11392
11393   if (value)
11394     {
11395       enum machine_mode mode = GET_MODE (reg);
11396       subst_low_luid = DF_INSN_LUID (insn);
11397       rsp->last_set_mode = mode;
11398       if (GET_MODE_CLASS (mode) == MODE_INT
11399           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11400         mode = nonzero_bits_mode;
11401       rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
11402       rsp->last_set_sign_bit_copies
11403         = num_sign_bit_copies (value, GET_MODE (reg));
11404     }
11405 }
11406
11407 /* Called via note_stores from record_dead_and_set_regs to handle one
11408    SET or CLOBBER in an insn.  DATA is the instruction in which the
11409    set is occurring.  */
11410
11411 static void
11412 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
11413 {
11414   rtx record_dead_insn = (rtx) data;
11415
11416   if (GET_CODE (dest) == SUBREG)
11417     dest = SUBREG_REG (dest);
11418
11419   if (!record_dead_insn)
11420     {
11421       if (REG_P (dest))
11422         record_value_for_reg (dest, NULL_RTX, NULL_RTX);
11423       return;
11424     }
11425
11426   if (REG_P (dest))
11427     {
11428       /* If we are setting the whole register, we know its value.  Otherwise
11429          show that we don't know the value.  We can handle SUBREG in
11430          some cases.  */
11431       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11432         record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11433       else if (GET_CODE (setter) == SET
11434                && GET_CODE (SET_DEST (setter)) == SUBREG
11435                && SUBREG_REG (SET_DEST (setter)) == dest
11436                && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11437                && subreg_lowpart_p (SET_DEST (setter)))
11438         record_value_for_reg (dest, record_dead_insn,
11439                               gen_lowpart (GET_MODE (dest),
11440                                                        SET_SRC (setter)));
11441       else
11442         record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11443     }
11444   else if (MEM_P (dest)
11445            /* Ignore pushes, they clobber nothing.  */
11446            && ! push_operand (dest, GET_MODE (dest)))
11447     mem_last_set = DF_INSN_LUID (record_dead_insn);
11448 }
11449
11450 /* Update the records of when each REG was most recently set or killed
11451    for the things done by INSN.  This is the last thing done in processing
11452    INSN in the combiner loop.
11453
11454    We update reg_stat[], in particular fields last_set, last_set_value,
11455    last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
11456    last_death, and also the similar information mem_last_set (which insn
11457    most recently modified memory) and last_call_luid (which insn was the
11458    most recent subroutine call).  */
11459
11460 static void
11461 record_dead_and_set_regs (rtx insn)
11462 {
11463   rtx link;
11464   unsigned int i;
11465
11466   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11467     {
11468       if (REG_NOTE_KIND (link) == REG_DEAD
11469           && REG_P (XEXP (link, 0)))
11470         {
11471           unsigned int regno = REGNO (XEXP (link, 0));
11472           unsigned int endregno = END_REGNO (XEXP (link, 0));
11473
11474           for (i = regno; i < endregno; i++)
11475             {
11476               reg_stat_type *rsp;
11477
11478               rsp = VEC_index (reg_stat_type, reg_stat, i);
11479               rsp->last_death = insn;
11480             }
11481         }
11482       else if (REG_NOTE_KIND (link) == REG_INC)
11483         record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11484     }
11485
11486   if (CALL_P (insn))
11487     {
11488       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11489         if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11490           {
11491             reg_stat_type *rsp;
11492
11493             rsp = VEC_index (reg_stat_type, reg_stat, i);
11494             rsp->last_set_invalid = 1;
11495             rsp->last_set = insn;
11496             rsp->last_set_value = 0;
11497             rsp->last_set_mode = 0;
11498             rsp->last_set_nonzero_bits = 0;
11499             rsp->last_set_sign_bit_copies = 0;
11500             rsp->last_death = 0;
11501             rsp->truncated_to_mode = 0;
11502           }
11503
11504       last_call_luid = mem_last_set = DF_INSN_LUID (insn);
11505
11506       /* We can't combine into a call pattern.  Remember, though, that
11507          the return value register is set at this LUID.  We could
11508          still replace a register with the return value from the
11509          wrong subroutine call!  */
11510       note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
11511     }
11512   else
11513     note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11514 }
11515
11516 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11517    register present in the SUBREG, so for each such SUBREG go back and
11518    adjust nonzero and sign bit information of the registers that are
11519    known to have some zero/sign bits set.
11520
11521    This is needed because when combine blows the SUBREGs away, the
11522    information on zero/sign bits is lost and further combines can be
11523    missed because of that.  */
11524
11525 static void
11526 record_promoted_value (rtx insn, rtx subreg)
11527 {
11528   rtx links, set;
11529   unsigned int regno = REGNO (SUBREG_REG (subreg));
11530   enum machine_mode mode = GET_MODE (subreg);
11531
11532   if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11533     return;
11534
11535   for (links = LOG_LINKS (insn); links;)
11536     {
11537       reg_stat_type *rsp;
11538
11539       insn = XEXP (links, 0);
11540       set = single_set (insn);
11541
11542       if (! set || !REG_P (SET_DEST (set))
11543           || REGNO (SET_DEST (set)) != regno
11544           || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11545         {
11546           links = XEXP (links, 1);
11547           continue;
11548         }
11549
11550       rsp = VEC_index (reg_stat_type, reg_stat, regno);
11551       if (rsp->last_set == insn)
11552         {
11553           if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11554             rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
11555         }
11556
11557       if (REG_P (SET_SRC (set)))
11558         {
11559           regno = REGNO (SET_SRC (set));
11560           links = LOG_LINKS (insn);
11561         }
11562       else
11563         break;
11564     }
11565 }
11566
11567 /* Check if X, a register, is known to contain a value already
11568    truncated to MODE.  In this case we can use a subreg to refer to
11569    the truncated value even though in the generic case we would need
11570    an explicit truncation.  */
11571
11572 static bool
11573 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
11574 {
11575   reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
11576   enum machine_mode truncated = rsp->truncated_to_mode;
11577
11578   if (truncated == 0
11579       || rsp->truncation_label < label_tick_ebb_start)
11580     return false;
11581   if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
11582     return true;
11583   if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
11584                              GET_MODE_BITSIZE (truncated)))
11585     return true;
11586   return false;
11587 }
11588
11589 /* X is a REG or a SUBREG.  If X is some sort of a truncation record
11590    it.  For non-TRULY_NOOP_TRUNCATION targets we might be able to turn
11591    a truncate into a subreg using this information.  */
11592
11593 static void
11594 record_truncated_value (rtx x)
11595 {
11596   enum machine_mode truncated_mode;
11597   reg_stat_type *rsp;
11598
11599   if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
11600     {
11601       enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
11602       truncated_mode = GET_MODE (x);
11603
11604       if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
11605         return;
11606
11607       if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (truncated_mode),
11608                                  GET_MODE_BITSIZE (original_mode)))
11609         return;
11610
11611       x = SUBREG_REG (x);
11612     }
11613   /* ??? For hard-regs we now record everything.  We might be able to
11614      optimize this using last_set_mode.  */
11615   else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
11616     truncated_mode = GET_MODE (x);
11617   else
11618     return;
11619
11620   rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
11621   if (rsp->truncated_to_mode == 0
11622       || rsp->truncation_label < label_tick_ebb_start
11623       || (GET_MODE_SIZE (truncated_mode)
11624           < GET_MODE_SIZE (rsp->truncated_to_mode)))
11625     {
11626       rsp->truncated_to_mode = truncated_mode;
11627       rsp->truncation_label = label_tick;
11628     }
11629 }
11630
11631 /* Scan X for promoted SUBREGs and truncated REGs.  For each one
11632    found, note what it implies to the registers used in it.  */
11633
11634 static void
11635 check_conversions (rtx insn, rtx x)
11636 {
11637   if (GET_CODE (x) == SUBREG || REG_P (x))
11638     {
11639       if (GET_CODE (x) == SUBREG
11640           && SUBREG_PROMOTED_VAR_P (x)
11641           && REG_P (SUBREG_REG (x)))
11642         record_promoted_value (insn, x);
11643
11644       record_truncated_value (x);
11645     }
11646   else
11647     {
11648       const char *format = GET_RTX_FORMAT (GET_CODE (x));
11649       int i, j;
11650
11651       for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11652         switch (format[i])
11653           {
11654           case 'e':
11655             check_conversions (insn, XEXP (x, i));
11656             break;
11657           case 'V':
11658           case 'E':
11659             if (XVEC (x, i) != 0)
11660               for (j = 0; j < XVECLEN (x, i); j++)
11661                 check_conversions (insn, XVECEXP (x, i, j));
11662             break;
11663           }
11664     }
11665 }
11666 \f
11667 /* Utility routine for the following function.  Verify that all the registers
11668    mentioned in *LOC are valid when *LOC was part of a value set when
11669    label_tick == TICK.  Return 0 if some are not.
11670
11671    If REPLACE is nonzero, replace the invalid reference with
11672    (clobber (const_int 0)) and return 1.  This replacement is useful because
11673    we often can get useful information about the form of a value (e.g., if
11674    it was produced by a shift that always produces -1 or 0) even though
11675    we don't know exactly what registers it was produced from.  */
11676
11677 static int
11678 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
11679 {
11680   rtx x = *loc;
11681   const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11682   int len = GET_RTX_LENGTH (GET_CODE (x));
11683   int i;
11684
11685   if (REG_P (x))
11686     {
11687       unsigned int regno = REGNO (x);
11688       unsigned int endregno = END_REGNO (x);
11689       unsigned int j;
11690
11691       for (j = regno; j < endregno; j++)
11692         {
11693           reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, j);
11694           if (rsp->last_set_invalid
11695               /* If this is a pseudo-register that was only set once and not
11696                  live at the beginning of the function, it is always valid.  */
11697               || (! (regno >= FIRST_PSEUDO_REGISTER
11698                      && REG_N_SETS (regno) == 1
11699                      && (!REGNO_REG_SET_P
11700                          (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno)))
11701                   && rsp->last_set_label > tick))
11702           {
11703             if (replace)
11704               *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11705             return replace;
11706           }
11707         }
11708
11709       return 1;
11710     }
11711   /* If this is a memory reference, make sure that there were
11712      no stores after it that might have clobbered the value.  We don't
11713      have alias info, so we assume any store invalidates it.  */
11714   else if (MEM_P (x) && !MEM_READONLY_P (x)
11715            && DF_INSN_LUID (insn) <= mem_last_set)
11716     {
11717       if (replace)
11718         *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11719       return replace;
11720     }
11721
11722   for (i = 0; i < len; i++)
11723     {
11724       if (fmt[i] == 'e')
11725         {
11726           /* Check for identical subexpressions.  If x contains
11727              identical subexpression we only have to traverse one of
11728              them.  */
11729           if (i == 1 && ARITHMETIC_P (x))
11730             {
11731               /* Note that at this point x0 has already been checked
11732                  and found valid.  */
11733               rtx x0 = XEXP (x, 0);
11734               rtx x1 = XEXP (x, 1);
11735
11736               /* If x0 and x1 are identical then x is also valid.  */
11737               if (x0 == x1)
11738                 return 1;
11739
11740               /* If x1 is identical to a subexpression of x0 then
11741                  while checking x0, x1 has already been checked.  Thus
11742                  it is valid and so as x.  */
11743               if (ARITHMETIC_P (x0)
11744                   && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11745                 return 1;
11746
11747               /* If x0 is identical to a subexpression of x1 then x is
11748                  valid iff the rest of x1 is valid.  */
11749               if (ARITHMETIC_P (x1)
11750                   && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11751                 return
11752                   get_last_value_validate (&XEXP (x1,
11753                                                   x0 == XEXP (x1, 0) ? 1 : 0),
11754                                            insn, tick, replace);
11755             }
11756
11757           if (get_last_value_validate (&XEXP (x, i), insn, tick,
11758                                        replace) == 0)
11759             return 0;
11760         }
11761       /* Don't bother with these.  They shouldn't occur anyway.  */
11762       else if (fmt[i] == 'E')
11763         return 0;
11764     }
11765
11766   /* If we haven't found a reason for it to be invalid, it is valid.  */
11767   return 1;
11768 }
11769
11770 /* Get the last value assigned to X, if known.  Some registers
11771    in the value may be replaced with (clobber (const_int 0)) if their value
11772    is known longer known reliably.  */
11773
11774 static rtx
11775 get_last_value (const_rtx x)
11776 {
11777   unsigned int regno;
11778   rtx value;
11779   reg_stat_type *rsp;
11780
11781   /* If this is a non-paradoxical SUBREG, get the value of its operand and
11782      then convert it to the desired mode.  If this is a paradoxical SUBREG,
11783      we cannot predict what values the "extra" bits might have.  */
11784   if (GET_CODE (x) == SUBREG
11785       && subreg_lowpart_p (x)
11786       && (GET_MODE_SIZE (GET_MODE (x))
11787           <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11788       && (value = get_last_value (SUBREG_REG (x))) != 0)
11789     return gen_lowpart (GET_MODE (x), value);
11790
11791   if (!REG_P (x))
11792     return 0;
11793
11794   regno = REGNO (x);
11795   rsp = VEC_index (reg_stat_type, reg_stat, regno);
11796   value = rsp->last_set_value;
11797
11798   /* If we don't have a value, or if it isn't for this basic block and
11799      it's either a hard register, set more than once, or it's a live
11800      at the beginning of the function, return 0.
11801
11802      Because if it's not live at the beginning of the function then the reg
11803      is always set before being used (is never used without being set).
11804      And, if it's set only once, and it's always set before use, then all
11805      uses must have the same last value, even if it's not from this basic
11806      block.  */
11807
11808   if (value == 0
11809       || (rsp->last_set_label < label_tick_ebb_start
11810           && (regno < FIRST_PSEUDO_REGISTER
11811               || REG_N_SETS (regno) != 1
11812               || REGNO_REG_SET_P
11813                  (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno))))
11814     return 0;
11815
11816   /* If the value was set in a later insn than the ones we are processing,
11817      we can't use it even if the register was only set once.  */
11818   if (rsp->last_set_label == label_tick
11819       && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
11820     return 0;
11821
11822   /* If the value has all its registers valid, return it.  */
11823   if (get_last_value_validate (&value, rsp->last_set,
11824                                rsp->last_set_label, 0))
11825     return value;
11826
11827   /* Otherwise, make a copy and replace any invalid register with
11828      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
11829
11830   value = copy_rtx (value);
11831   if (get_last_value_validate (&value, rsp->last_set,
11832                                rsp->last_set_label, 1))
11833     return value;
11834
11835   return 0;
11836 }
11837 \f
11838 /* Return nonzero if expression X refers to a REG or to memory
11839    that is set in an instruction more recent than FROM_LUID.  */
11840
11841 static int
11842 use_crosses_set_p (const_rtx x, int from_luid)
11843 {
11844   const char *fmt;
11845   int i;
11846   enum rtx_code code = GET_CODE (x);
11847
11848   if (code == REG)
11849     {
11850       unsigned int regno = REGNO (x);
11851       unsigned endreg = END_REGNO (x);
11852
11853 #ifdef PUSH_ROUNDING
11854       /* Don't allow uses of the stack pointer to be moved,
11855          because we don't know whether the move crosses a push insn.  */
11856       if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11857         return 1;
11858 #endif
11859       for (; regno < endreg; regno++)
11860         {
11861           reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
11862           if (rsp->last_set
11863               && rsp->last_set_label == label_tick
11864               && DF_INSN_LUID (rsp->last_set) > from_luid)
11865             return 1;
11866         }
11867       return 0;
11868     }
11869
11870   if (code == MEM && mem_last_set > from_luid)
11871     return 1;
11872
11873   fmt = GET_RTX_FORMAT (code);
11874
11875   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11876     {
11877       if (fmt[i] == 'E')
11878         {
11879           int j;
11880           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11881             if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
11882               return 1;
11883         }
11884       else if (fmt[i] == 'e'
11885                && use_crosses_set_p (XEXP (x, i), from_luid))
11886         return 1;
11887     }
11888   return 0;
11889 }
11890 \f
11891 /* Define three variables used for communication between the following
11892    routines.  */
11893
11894 static unsigned int reg_dead_regno, reg_dead_endregno;
11895 static int reg_dead_flag;
11896
11897 /* Function called via note_stores from reg_dead_at_p.
11898
11899    If DEST is within [reg_dead_regno, reg_dead_endregno), set
11900    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
11901
11902 static void
11903 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
11904 {
11905   unsigned int regno, endregno;
11906
11907   if (!REG_P (dest))
11908     return;
11909
11910   regno = REGNO (dest);
11911   endregno = END_REGNO (dest);
11912   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11913     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11914 }
11915
11916 /* Return nonzero if REG is known to be dead at INSN.
11917
11918    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
11919    referencing REG, it is dead.  If we hit a SET referencing REG, it is
11920    live.  Otherwise, see if it is live or dead at the start of the basic
11921    block we are in.  Hard regs marked as being live in NEWPAT_USED_REGS
11922    must be assumed to be always live.  */
11923
11924 static int
11925 reg_dead_at_p (rtx reg, rtx insn)
11926 {
11927   basic_block block;
11928   unsigned int i;
11929
11930   /* Set variables for reg_dead_at_p_1.  */
11931   reg_dead_regno = REGNO (reg);
11932   reg_dead_endregno = END_REGNO (reg);
11933
11934   reg_dead_flag = 0;
11935
11936   /* Check that reg isn't mentioned in NEWPAT_USED_REGS.  For fixed registers
11937      we allow the machine description to decide whether use-and-clobber
11938      patterns are OK.  */
11939   if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11940     {
11941       for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11942         if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
11943           return 0;
11944     }
11945
11946   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11947      beginning of function.  */
11948   for (; insn && !LABEL_P (insn) && !BARRIER_P (insn);
11949        insn = prev_nonnote_insn (insn))
11950     {
11951       note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
11952       if (reg_dead_flag)
11953         return reg_dead_flag == 1 ? 1 : 0;
11954
11955       if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11956         return 1;
11957     }
11958
11959   /* Get the basic block that we were in.  */
11960   if (insn == 0)
11961     block = ENTRY_BLOCK_PTR->next_bb;
11962   else
11963     {
11964       FOR_EACH_BB (block)
11965         if (insn == BB_HEAD (block))
11966           break;
11967
11968       if (block == EXIT_BLOCK_PTR)
11969         return 0;
11970     }
11971
11972   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11973     if (REGNO_REG_SET_P (df_get_live_in (block), i))
11974       return 0;
11975
11976   return 1;
11977 }
11978 \f
11979 /* Note hard registers in X that are used.  */
11980
11981 static void
11982 mark_used_regs_combine (rtx x)
11983 {
11984   RTX_CODE code = GET_CODE (x);
11985   unsigned int regno;
11986   int i;
11987
11988   switch (code)
11989     {
11990     case LABEL_REF:
11991     case SYMBOL_REF:
11992     case CONST_INT:
11993     case CONST:
11994     case CONST_DOUBLE:
11995     case CONST_VECTOR:
11996     case PC:
11997     case ADDR_VEC:
11998     case ADDR_DIFF_VEC:
11999     case ASM_INPUT:
12000 #ifdef HAVE_cc0
12001     /* CC0 must die in the insn after it is set, so we don't need to take
12002        special note of it here.  */
12003     case CC0:
12004 #endif
12005       return;
12006
12007     case CLOBBER:
12008       /* If we are clobbering a MEM, mark any hard registers inside the
12009          address as used.  */
12010       if (MEM_P (XEXP (x, 0)))
12011         mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12012       return;
12013
12014     case REG:
12015       regno = REGNO (x);
12016       /* A hard reg in a wide mode may really be multiple registers.
12017          If so, mark all of them just like the first.  */
12018       if (regno < FIRST_PSEUDO_REGISTER)
12019         {
12020           /* None of this applies to the stack, frame or arg pointers.  */
12021           if (regno == STACK_POINTER_REGNUM
12022 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
12023               || regno == HARD_FRAME_POINTER_REGNUM
12024 #endif
12025 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12026               || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12027 #endif
12028               || regno == FRAME_POINTER_REGNUM)
12029             return;
12030
12031           add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
12032         }
12033       return;
12034
12035     case SET:
12036       {
12037         /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12038            the address.  */
12039         rtx testreg = SET_DEST (x);
12040
12041         while (GET_CODE (testreg) == SUBREG
12042                || GET_CODE (testreg) == ZERO_EXTRACT
12043                || GET_CODE (testreg) == STRICT_LOW_PART)
12044           testreg = XEXP (testreg, 0);
12045
12046         if (MEM_P (testreg))
12047           mark_used_regs_combine (XEXP (testreg, 0));
12048
12049         mark_used_regs_combine (SET_SRC (x));
12050       }
12051       return;
12052
12053     default:
12054       break;
12055     }
12056
12057   /* Recursively scan the operands of this expression.  */
12058
12059   {
12060     const char *fmt = GET_RTX_FORMAT (code);
12061
12062     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12063       {
12064         if (fmt[i] == 'e')
12065           mark_used_regs_combine (XEXP (x, i));
12066         else if (fmt[i] == 'E')
12067           {
12068             int j;
12069
12070             for (j = 0; j < XVECLEN (x, i); j++)
12071               mark_used_regs_combine (XVECEXP (x, i, j));
12072           }
12073       }
12074   }
12075 }
12076 \f
12077 /* Remove register number REGNO from the dead registers list of INSN.
12078
12079    Return the note used to record the death, if there was one.  */
12080
12081 rtx
12082 remove_death (unsigned int regno, rtx insn)
12083 {
12084   rtx note = find_regno_note (insn, REG_DEAD, regno);
12085
12086   if (note)
12087     remove_note (insn, note);
12088
12089   return note;
12090 }
12091
12092 /* For each register (hardware or pseudo) used within expression X, if its
12093    death is in an instruction with luid between FROM_LUID (inclusive) and
12094    TO_INSN (exclusive), put a REG_DEAD note for that register in the
12095    list headed by PNOTES.
12096
12097    That said, don't move registers killed by maybe_kill_insn.
12098
12099    This is done when X is being merged by combination into TO_INSN.  These
12100    notes will then be distributed as needed.  */
12101
12102 static void
12103 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx to_insn,
12104              rtx *pnotes)
12105 {
12106   const char *fmt;
12107   int len, i;
12108   enum rtx_code code = GET_CODE (x);
12109
12110   if (code == REG)
12111     {
12112       unsigned int regno = REGNO (x);
12113       rtx where_dead = VEC_index (reg_stat_type, reg_stat, regno)->last_death;
12114
12115       /* Don't move the register if it gets killed in between from and to.  */
12116       if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12117           && ! reg_referenced_p (x, maybe_kill_insn))
12118         return;
12119
12120       if (where_dead
12121           && DF_INSN_LUID (where_dead) >= from_luid
12122           && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
12123         {
12124           rtx note = remove_death (regno, where_dead);
12125
12126           /* It is possible for the call above to return 0.  This can occur
12127              when last_death points to I2 or I1 that we combined with.
12128              In that case make a new note.
12129
12130              We must also check for the case where X is a hard register
12131              and NOTE is a death note for a range of hard registers
12132              including X.  In that case, we must put REG_DEAD notes for
12133              the remaining registers in place of NOTE.  */
12134
12135           if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12136               && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12137                   > GET_MODE_SIZE (GET_MODE (x))))
12138             {
12139               unsigned int deadregno = REGNO (XEXP (note, 0));
12140               unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
12141               unsigned int ourend = END_HARD_REGNO (x);
12142               unsigned int i;
12143
12144               for (i = deadregno; i < deadend; i++)
12145                 if (i < regno || i >= ourend)
12146                   REG_NOTES (where_dead)
12147                     = gen_rtx_EXPR_LIST (REG_DEAD,
12148                                          regno_reg_rtx[i],
12149                                          REG_NOTES (where_dead));
12150             }
12151
12152           /* If we didn't find any note, or if we found a REG_DEAD note that
12153              covers only part of the given reg, and we have a multi-reg hard
12154              register, then to be safe we must check for REG_DEAD notes
12155              for each register other than the first.  They could have
12156              their own REG_DEAD notes lying around.  */
12157           else if ((note == 0
12158                     || (note != 0
12159                         && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12160                             < GET_MODE_SIZE (GET_MODE (x)))))
12161                    && regno < FIRST_PSEUDO_REGISTER
12162                    && hard_regno_nregs[regno][GET_MODE (x)] > 1)
12163             {
12164               unsigned int ourend = END_HARD_REGNO (x);
12165               unsigned int i, offset;
12166               rtx oldnotes = 0;
12167
12168               if (note)
12169                 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
12170               else
12171                 offset = 1;
12172
12173               for (i = regno + offset; i < ourend; i++)
12174                 move_deaths (regno_reg_rtx[i],
12175                              maybe_kill_insn, from_luid, to_insn, &oldnotes);
12176             }
12177
12178           if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12179             {
12180               XEXP (note, 1) = *pnotes;
12181               *pnotes = note;
12182             }
12183           else
12184             *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
12185         }
12186
12187       return;
12188     }
12189
12190   else if (GET_CODE (x) == SET)
12191     {
12192       rtx dest = SET_DEST (x);
12193
12194       move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
12195
12196       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
12197          that accesses one word of a multi-word item, some
12198          piece of everything register in the expression is used by
12199          this insn, so remove any old death.  */
12200       /* ??? So why do we test for equality of the sizes?  */
12201
12202       if (GET_CODE (dest) == ZERO_EXTRACT
12203           || GET_CODE (dest) == STRICT_LOW_PART
12204           || (GET_CODE (dest) == SUBREG
12205               && (((GET_MODE_SIZE (GET_MODE (dest))
12206                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
12207                   == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
12208                        + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
12209         {
12210           move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
12211           return;
12212         }
12213
12214       /* If this is some other SUBREG, we know it replaces the entire
12215          value, so use that as the destination.  */
12216       if (GET_CODE (dest) == SUBREG)
12217         dest = SUBREG_REG (dest);
12218
12219       /* If this is a MEM, adjust deaths of anything used in the address.
12220          For a REG (the only other possibility), the entire value is
12221          being replaced so the old value is not used in this insn.  */
12222
12223       if (MEM_P (dest))
12224         move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
12225                      to_insn, pnotes);
12226       return;
12227     }
12228
12229   else if (GET_CODE (x) == CLOBBER)
12230     return;
12231
12232   len = GET_RTX_LENGTH (code);
12233   fmt = GET_RTX_FORMAT (code);
12234
12235   for (i = 0; i < len; i++)
12236     {
12237       if (fmt[i] == 'E')
12238         {
12239           int j;
12240           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12241             move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
12242                          to_insn, pnotes);
12243         }
12244       else if (fmt[i] == 'e')
12245         move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
12246     }
12247 }
12248 \f
12249 /* Return 1 if X is the target of a bit-field assignment in BODY, the
12250    pattern of an insn.  X must be a REG.  */
12251
12252 static int
12253 reg_bitfield_target_p (rtx x, rtx body)
12254 {
12255   int i;
12256
12257   if (GET_CODE (body) == SET)
12258     {
12259       rtx dest = SET_DEST (body);
12260       rtx target;
12261       unsigned int regno, tregno, endregno, endtregno;
12262
12263       if (GET_CODE (dest) == ZERO_EXTRACT)
12264         target = XEXP (dest, 0);
12265       else if (GET_CODE (dest) == STRICT_LOW_PART)
12266         target = SUBREG_REG (XEXP (dest, 0));
12267       else
12268         return 0;
12269
12270       if (GET_CODE (target) == SUBREG)
12271         target = SUBREG_REG (target);
12272
12273       if (!REG_P (target))
12274         return 0;
12275
12276       tregno = REGNO (target), regno = REGNO (x);
12277       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
12278         return target == x;
12279
12280       endtregno = end_hard_regno (GET_MODE (target), tregno);
12281       endregno = end_hard_regno (GET_MODE (x), regno);
12282
12283       return endregno > tregno && regno < endtregno;
12284     }
12285
12286   else if (GET_CODE (body) == PARALLEL)
12287     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
12288       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
12289         return 1;
12290
12291   return 0;
12292 }
12293 \f
12294 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
12295    as appropriate.  I3 and I2 are the insns resulting from the combination
12296    insns including FROM (I2 may be zero).
12297
12298    ELIM_I2 and ELIM_I1 are either zero or registers that we know will
12299    not need REG_DEAD notes because they are being substituted for.  This
12300    saves searching in the most common cases.
12301
12302    Each note in the list is either ignored or placed on some insns, depending
12303    on the type of note.  */
12304
12305 static void
12306 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
12307                   rtx elim_i1)
12308 {
12309   rtx note, next_note;
12310   rtx tem;
12311
12312   for (note = notes; note; note = next_note)
12313     {
12314       rtx place = 0, place2 = 0;
12315
12316       next_note = XEXP (note, 1);
12317       switch (REG_NOTE_KIND (note))
12318         {
12319         case REG_BR_PROB:
12320         case REG_BR_PRED:
12321           /* Doesn't matter much where we put this, as long as it's somewhere.
12322              It is preferable to keep these notes on branches, which is most
12323              likely to be i3.  */
12324           place = i3;
12325           break;
12326
12327         case REG_VALUE_PROFILE:
12328           /* Just get rid of this note, as it is unused later anyway.  */
12329           break;
12330
12331         case REG_NON_LOCAL_GOTO:
12332           if (JUMP_P (i3))
12333             place = i3;
12334           else
12335             {
12336               gcc_assert (i2 && JUMP_P (i2));
12337               place = i2;
12338             }
12339           break;
12340
12341         case REG_EH_REGION:
12342           /* These notes must remain with the call or trapping instruction.  */
12343           if (CALL_P (i3))
12344             place = i3;
12345           else if (i2 && CALL_P (i2))
12346             place = i2;
12347           else
12348             {
12349               gcc_assert (flag_non_call_exceptions);
12350               if (may_trap_p (i3))
12351                 place = i3;
12352               else if (i2 && may_trap_p (i2))
12353                 place = i2;
12354               /* ??? Otherwise assume we've combined things such that we
12355                  can now prove that the instructions can't trap.  Drop the
12356                  note in this case.  */
12357             }
12358           break;
12359
12360         case REG_NORETURN:
12361         case REG_SETJMP:
12362           /* These notes must remain with the call.  It should not be
12363              possible for both I2 and I3 to be a call.  */
12364           if (CALL_P (i3))
12365             place = i3;
12366           else
12367             {
12368               gcc_assert (i2 && CALL_P (i2));
12369               place = i2;
12370             }
12371           break;
12372
12373         case REG_UNUSED:
12374           /* Any clobbers for i3 may still exist, and so we must process
12375              REG_UNUSED notes from that insn.
12376
12377              Any clobbers from i2 or i1 can only exist if they were added by
12378              recog_for_combine.  In that case, recog_for_combine created the
12379              necessary REG_UNUSED notes.  Trying to keep any original
12380              REG_UNUSED notes from these insns can cause incorrect output
12381              if it is for the same register as the original i3 dest.
12382              In that case, we will notice that the register is set in i3,
12383              and then add a REG_UNUSED note for the destination of i3, which
12384              is wrong.  However, it is possible to have REG_UNUSED notes from
12385              i2 or i1 for register which were both used and clobbered, so
12386              we keep notes from i2 or i1 if they will turn into REG_DEAD
12387              notes.  */
12388
12389           /* If this register is set or clobbered in I3, put the note there
12390              unless there is one already.  */
12391           if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
12392             {
12393               if (from_insn != i3)
12394                 break;
12395
12396               if (! (REG_P (XEXP (note, 0))
12397                      ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
12398                      : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
12399                 place = i3;
12400             }
12401           /* Otherwise, if this register is used by I3, then this register
12402              now dies here, so we must put a REG_DEAD note here unless there
12403              is one already.  */
12404           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
12405                    && ! (REG_P (XEXP (note, 0))
12406                          ? find_regno_note (i3, REG_DEAD,
12407                                             REGNO (XEXP (note, 0)))
12408                          : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
12409             {
12410               PUT_REG_NOTE_KIND (note, REG_DEAD);
12411               place = i3;
12412             }
12413           break;
12414
12415         case REG_EQUAL:
12416         case REG_EQUIV:
12417         case REG_NOALIAS:
12418           /* These notes say something about results of an insn.  We can
12419              only support them if they used to be on I3 in which case they
12420              remain on I3.  Otherwise they are ignored.
12421
12422              If the note refers to an expression that is not a constant, we
12423              must also ignore the note since we cannot tell whether the
12424              equivalence is still true.  It might be possible to do
12425              slightly better than this (we only have a problem if I2DEST
12426              or I1DEST is present in the expression), but it doesn't
12427              seem worth the trouble.  */
12428
12429           if (from_insn == i3
12430               && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
12431             place = i3;
12432           break;
12433
12434         case REG_INC:
12435         case REG_NO_CONFLICT:
12436           /* These notes say something about how a register is used.  They must
12437              be present on any use of the register in I2 or I3.  */
12438           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
12439             place = i3;
12440
12441           if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
12442             {
12443               if (place)
12444                 place2 = i2;
12445               else
12446                 place = i2;
12447             }
12448           break;
12449
12450         case REG_LABEL_TARGET:
12451         case REG_LABEL_OPERAND:
12452           /* This can show up in several ways -- either directly in the
12453              pattern, or hidden off in the constant pool with (or without?)
12454              a REG_EQUAL note.  */
12455           /* ??? Ignore the without-reg_equal-note problem for now.  */
12456           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12457               || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12458                   && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12459                   && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12460             place = i3;
12461
12462           if (i2
12463               && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12464                   || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12465                       && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12466                       && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12467             {
12468               if (place)
12469                 place2 = i2;
12470               else
12471                 place = i2;
12472             }
12473
12474           /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
12475              as a JUMP_LABEL or decrement LABEL_NUSES if it's already
12476              there.  */
12477           if (place && JUMP_P (place)
12478               && REG_NOTE_KIND (note) == REG_LABEL_TARGET
12479               && (JUMP_LABEL (place) == NULL
12480                   || JUMP_LABEL (place) == XEXP (note, 0)))
12481             {
12482               rtx label = JUMP_LABEL (place);
12483
12484               if (!label)
12485                 JUMP_LABEL (place) = XEXP (note, 0);
12486               else if (LABEL_P (label))
12487                 LABEL_NUSES (label)--;
12488             }
12489
12490           if (place2 && JUMP_P (place2)
12491               && REG_NOTE_KIND (note) == REG_LABEL_TARGET
12492               && (JUMP_LABEL (place2) == NULL
12493                   || JUMP_LABEL (place2) == XEXP (note, 0)))
12494             {
12495               rtx label = JUMP_LABEL (place2);
12496
12497               if (!label)
12498                 JUMP_LABEL (place2) = XEXP (note, 0);
12499               else if (LABEL_P (label))
12500                 LABEL_NUSES (label)--;
12501               place2 = 0;
12502             }
12503           break;
12504
12505         case REG_NONNEG:
12506           /* This note says something about the value of a register prior
12507              to the execution of an insn.  It is too much trouble to see
12508              if the note is still correct in all situations.  It is better
12509              to simply delete it.  */
12510           break;
12511
12512         case REG_LIBCALL_ID:
12513           /* If the insn previously containing this note still exists,
12514              put it back where it was.  Otherwise move it to the previous
12515              insn.  */
12516           if (!NOTE_P (from_insn))
12517             place = from_insn;
12518           else
12519             place = prev_real_insn (from_insn);
12520           break;
12521         case REG_RETVAL:
12522           /* If the insn previously containing this note still exists,
12523              put it back where it was.  Otherwise move it to the previous
12524              insn.  Adjust the corresponding REG_LIBCALL note.  */
12525           if (!NOTE_P (from_insn))
12526             place = from_insn;
12527           else
12528             {
12529               tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
12530               place = prev_real_insn (from_insn);
12531               if (tem && place)
12532                 XEXP (tem, 0) = place;
12533               /* If we're deleting the last remaining instruction of a
12534                  libcall sequence, don't add the notes.  */
12535               else if (XEXP (note, 0) == from_insn)
12536                 tem = place = 0;
12537               /* Don't add the dangling REG_RETVAL note.  */
12538               else if (! tem)
12539                 place = 0;
12540             }
12541           break;
12542
12543         case REG_LIBCALL:
12544           /* This is handled similarly to REG_RETVAL.  */
12545           if (!NOTE_P (from_insn))
12546             place = from_insn;
12547           else
12548             {
12549               tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
12550               place = next_real_insn (from_insn);
12551               if (tem && place)
12552                 XEXP (tem, 0) = place;
12553               /* If we're deleting the last remaining instruction of a
12554                  libcall sequence, don't add the notes.  */
12555               else if (XEXP (note, 0) == from_insn)
12556                 tem = place = 0;
12557               /* Don't add the dangling REG_LIBCALL note.  */
12558               else if (! tem)
12559                 place = 0;
12560             }
12561           break;
12562
12563         case REG_DEAD:
12564           /* If we replaced the right hand side of FROM_INSN with a
12565              REG_EQUAL note, the original use of the dying register
12566              will not have been combined into I3 and I2.  In such cases,
12567              FROM_INSN is guaranteed to be the first of the combined
12568              instructions, so we simply need to search back before
12569              FROM_INSN for the previous use or set of this register,
12570              then alter the notes there appropriately.
12571
12572              If the register is used as an input in I3, it dies there.
12573              Similarly for I2, if it is nonzero and adjacent to I3.
12574
12575              If the register is not used as an input in either I3 or I2
12576              and it is not one of the registers we were supposed to eliminate,
12577              there are two possibilities.  We might have a non-adjacent I2
12578              or we might have somehow eliminated an additional register
12579              from a computation.  For example, we might have had A & B where
12580              we discover that B will always be zero.  In this case we will
12581              eliminate the reference to A.
12582
12583              In both cases, we must search to see if we can find a previous
12584              use of A and put the death note there.  */
12585
12586           if (from_insn
12587               && from_insn == i2mod
12588               && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
12589             tem = from_insn;
12590           else
12591             {
12592               if (from_insn
12593                   && CALL_P (from_insn)
12594                   && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12595                 place = from_insn;
12596               else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12597                 place = i3;
12598               else if (i2 != 0 && next_nonnote_insn (i2) == i3
12599                        && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12600                 place = i2;
12601               else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
12602                         && !(i2mod
12603                              && reg_overlap_mentioned_p (XEXP (note, 0),
12604                                                          i2mod_old_rhs)))
12605                        || rtx_equal_p (XEXP (note, 0), elim_i1))
12606                 break;
12607               tem = i3;
12608             }
12609
12610           if (place == 0)
12611             {
12612               basic_block bb = this_basic_block;
12613
12614               for (tem = PREV_INSN (tem); place == 0; tem = PREV_INSN (tem))
12615                 {
12616                   if (! INSN_P (tem))
12617                     {
12618                       if (tem == BB_HEAD (bb))
12619                         break;
12620                       continue;
12621                     }
12622
12623                   /* If the register is being set at TEM, see if that is all
12624                      TEM is doing.  If so, delete TEM.  Otherwise, make this
12625                      into a REG_UNUSED note instead. Don't delete sets to
12626                      global register vars.  */
12627                   if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
12628                        || !global_regs[REGNO (XEXP (note, 0))])
12629                       && reg_set_p (XEXP (note, 0), PATTERN (tem)))
12630                     {
12631                       rtx set = single_set (tem);
12632                       rtx inner_dest = 0;
12633 #ifdef HAVE_cc0
12634                       rtx cc0_setter = NULL_RTX;
12635 #endif
12636
12637                       if (set != 0)
12638                         for (inner_dest = SET_DEST (set);
12639                              (GET_CODE (inner_dest) == STRICT_LOW_PART
12640                               || GET_CODE (inner_dest) == SUBREG
12641                               || GET_CODE (inner_dest) == ZERO_EXTRACT);
12642                              inner_dest = XEXP (inner_dest, 0))
12643                           ;
12644
12645                       /* Verify that it was the set, and not a clobber that
12646                          modified the register.
12647
12648                          CC0 targets must be careful to maintain setter/user
12649                          pairs.  If we cannot delete the setter due to side
12650                          effects, mark the user with an UNUSED note instead
12651                          of deleting it.  */
12652
12653                       if (set != 0 && ! side_effects_p (SET_SRC (set))
12654                           && rtx_equal_p (XEXP (note, 0), inner_dest)
12655 #ifdef HAVE_cc0
12656                           && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12657                               || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12658                                   && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12659 #endif
12660                           )
12661                         {
12662                           /* Move the notes and links of TEM elsewhere.
12663                              This might delete other dead insns recursively.
12664                              First set the pattern to something that won't use
12665                              any register.  */
12666                           rtx old_notes = REG_NOTES (tem);
12667
12668                           PATTERN (tem) = pc_rtx;
12669                           REG_NOTES (tem) = NULL;
12670
12671                           distribute_notes (old_notes, tem, tem, NULL_RTX,
12672                                             NULL_RTX, NULL_RTX);
12673                           distribute_links (LOG_LINKS (tem));
12674
12675                           SET_INSN_DELETED (tem);
12676
12677 #ifdef HAVE_cc0
12678                           /* Delete the setter too.  */
12679                           if (cc0_setter)
12680                             {
12681                               PATTERN (cc0_setter) = pc_rtx;
12682                               old_notes = REG_NOTES (cc0_setter);
12683                               REG_NOTES (cc0_setter) = NULL;
12684
12685                               distribute_notes (old_notes, cc0_setter,
12686                                                 cc0_setter, NULL_RTX,
12687                                                 NULL_RTX, NULL_RTX);
12688                               distribute_links (LOG_LINKS (cc0_setter));
12689
12690                               SET_INSN_DELETED (cc0_setter);
12691                             }
12692 #endif
12693                         }
12694                       else
12695                         {
12696                           PUT_REG_NOTE_KIND (note, REG_UNUSED);
12697
12698                           /*  If there isn't already a REG_UNUSED note, put one
12699                               here.  Do not place a REG_DEAD note, even if
12700                               the register is also used here; that would not
12701                               match the algorithm used in lifetime analysis
12702                               and can cause the consistency check in the
12703                               scheduler to fail.  */
12704                           if (! find_regno_note (tem, REG_UNUSED,
12705                                                  REGNO (XEXP (note, 0))))
12706                             place = tem;
12707                           break;
12708                         }
12709                     }
12710                   else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12711                            || (CALL_P (tem)
12712                                && find_reg_fusage (tem, USE, XEXP (note, 0))))
12713                     {
12714                       place = tem;
12715
12716                       /* If we are doing a 3->2 combination, and we have a
12717                          register which formerly died in i3 and was not used
12718                          by i2, which now no longer dies in i3 and is used in
12719                          i2 but does not die in i2, and place is between i2
12720                          and i3, then we may need to move a link from place to
12721                          i2.  */
12722                       if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
12723                           && from_insn
12724                           && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
12725                           && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12726                         {
12727                           rtx links = LOG_LINKS (place);
12728                           LOG_LINKS (place) = 0;
12729                           distribute_links (links);
12730                         }
12731                       break;
12732                     }
12733
12734                   if (tem == BB_HEAD (bb))
12735                     break;
12736                 }
12737
12738             }
12739
12740           /* If the register is set or already dead at PLACE, we needn't do
12741              anything with this note if it is still a REG_DEAD note.
12742              We check here if it is set at all, not if is it totally replaced,
12743              which is what `dead_or_set_p' checks, so also check for it being
12744              set partially.  */
12745
12746           if (place && REG_NOTE_KIND (note) == REG_DEAD)
12747             {
12748               unsigned int regno = REGNO (XEXP (note, 0));
12749               reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
12750
12751               if (dead_or_set_p (place, XEXP (note, 0))
12752                   || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12753                 {
12754                   /* Unless the register previously died in PLACE, clear
12755                      last_death.  [I no longer understand why this is
12756                      being done.] */
12757                   if (rsp->last_death != place)
12758                     rsp->last_death = 0;
12759                   place = 0;
12760                 }
12761               else
12762                 rsp->last_death = place;
12763
12764               /* If this is a death note for a hard reg that is occupying
12765                  multiple registers, ensure that we are still using all
12766                  parts of the object.  If we find a piece of the object
12767                  that is unused, we must arrange for an appropriate REG_DEAD
12768                  note to be added for it.  However, we can't just emit a USE
12769                  and tag the note to it, since the register might actually
12770                  be dead; so we recourse, and the recursive call then finds
12771                  the previous insn that used this register.  */
12772
12773               if (place && regno < FIRST_PSEUDO_REGISTER
12774                   && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
12775                 {
12776                   unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
12777                   int all_used = 1;
12778                   unsigned int i;
12779
12780                   for (i = regno; i < endregno; i++)
12781                     if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12782                          && ! find_regno_fusage (place, USE, i))
12783                         || dead_or_set_regno_p (place, i))
12784                       all_used = 0;
12785
12786                   if (! all_used)
12787                     {
12788                       /* Put only REG_DEAD notes for pieces that are
12789                          not already dead or set.  */
12790
12791                       for (i = regno; i < endregno;
12792                            i += hard_regno_nregs[i][reg_raw_mode[i]])
12793                         {
12794                           rtx piece = regno_reg_rtx[i];
12795                           basic_block bb = this_basic_block;
12796
12797                           if (! dead_or_set_p (place, piece)
12798                               && ! reg_bitfield_target_p (piece,
12799                                                           PATTERN (place)))
12800                             {
12801                               rtx new_note
12802                                 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12803
12804                               distribute_notes (new_note, place, place,
12805                                                 NULL_RTX, NULL_RTX, NULL_RTX);
12806                             }
12807                           else if (! refers_to_regno_p (i, i + 1,
12808                                                         PATTERN (place), 0)
12809                                    && ! find_regno_fusage (place, USE, i))
12810                             for (tem = PREV_INSN (place); ;
12811                                  tem = PREV_INSN (tem))
12812                               {
12813                                 if (! INSN_P (tem))
12814                                   {
12815                                     if (tem == BB_HEAD (bb))
12816                                       break;
12817                                     continue;
12818                                   }
12819                                 if (dead_or_set_p (tem, piece)
12820                                     || reg_bitfield_target_p (piece,
12821                                                               PATTERN (tem)))
12822                                   {
12823                                     REG_NOTES (tem)
12824                                       = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
12825                                                            REG_NOTES (tem));
12826                                     break;
12827                                   }
12828                               }
12829
12830                         }
12831
12832                       place = 0;
12833                     }
12834                 }
12835             }
12836           break;
12837
12838         default:
12839           /* Any other notes should not be present at this point in the
12840              compilation.  */
12841           gcc_unreachable ();
12842         }
12843
12844       if (place)
12845         {
12846           XEXP (note, 1) = REG_NOTES (place);
12847           REG_NOTES (place) = note;
12848         }
12849
12850       if (place2)
12851         REG_NOTES (place2) 
12852           = gen_rtx_fmt_ee (GET_CODE (note), REG_NOTE_KIND (note),
12853                             XEXP (note, 0), REG_NOTES (place2));
12854     }
12855 }
12856 \f
12857 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12858    I3, I2, and I1 to new locations.  This is also called to add a link
12859    pointing at I3 when I3's destination is changed.  */
12860
12861 static void
12862 distribute_links (rtx links)
12863 {
12864   rtx link, next_link;
12865
12866   for (link = links; link; link = next_link)
12867     {
12868       rtx place = 0;
12869       rtx insn;
12870       rtx set, reg;
12871
12872       next_link = XEXP (link, 1);
12873
12874       /* If the insn that this link points to is a NOTE or isn't a single
12875          set, ignore it.  In the latter case, it isn't clear what we
12876          can do other than ignore the link, since we can't tell which
12877          register it was for.  Such links wouldn't be used by combine
12878          anyway.
12879
12880          It is not possible for the destination of the target of the link to
12881          have been changed by combine.  The only potential of this is if we
12882          replace I3, I2, and I1 by I3 and I2.  But in that case the
12883          destination of I2 also remains unchanged.  */
12884
12885       if (NOTE_P (XEXP (link, 0))
12886           || (set = single_set (XEXP (link, 0))) == 0)
12887         continue;
12888
12889       reg = SET_DEST (set);
12890       while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12891              || GET_CODE (reg) == STRICT_LOW_PART)
12892         reg = XEXP (reg, 0);
12893
12894       /* A LOG_LINK is defined as being placed on the first insn that uses
12895          a register and points to the insn that sets the register.  Start
12896          searching at the next insn after the target of the link and stop
12897          when we reach a set of the register or the end of the basic block.
12898
12899          Note that this correctly handles the link that used to point from
12900          I3 to I2.  Also note that not much searching is typically done here
12901          since most links don't point very far away.  */
12902
12903       for (insn = NEXT_INSN (XEXP (link, 0));
12904            (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
12905                      || BB_HEAD (this_basic_block->next_bb) != insn));
12906            insn = NEXT_INSN (insn))
12907         if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12908           {
12909             if (reg_referenced_p (reg, PATTERN (insn)))
12910               place = insn;
12911             break;
12912           }
12913         else if (CALL_P (insn)
12914                  && find_reg_fusage (insn, USE, reg))
12915           {
12916             place = insn;
12917             break;
12918           }
12919         else if (INSN_P (insn) && reg_set_p (reg, insn))
12920           break;
12921
12922       /* If we found a place to put the link, place it there unless there
12923          is already a link to the same insn as LINK at that point.  */
12924
12925       if (place)
12926         {
12927           rtx link2;
12928
12929           for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12930             if (XEXP (link2, 0) == XEXP (link, 0))
12931               break;
12932
12933           if (link2 == 0)
12934             {
12935               XEXP (link, 1) = LOG_LINKS (place);
12936               LOG_LINKS (place) = link;
12937
12938               /* Set added_links_insn to the earliest insn we added a
12939                  link to.  */
12940               if (added_links_insn == 0
12941                   || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
12942                 added_links_insn = place;
12943             }
12944         }
12945     }
12946 }
12947 \f
12948 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
12949    Check whether the expression pointer to by LOC is a register or
12950    memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
12951    Otherwise return zero.  */
12952
12953 static int
12954 unmentioned_reg_p_1 (rtx *loc, void *expr)
12955 {
12956   rtx x = *loc;
12957
12958   if (x != NULL_RTX
12959       && (REG_P (x) || MEM_P (x))
12960       && ! reg_mentioned_p (x, (rtx) expr))
12961     return 1;
12962   return 0;
12963 }
12964
12965 /* Check for any register or memory mentioned in EQUIV that is not
12966    mentioned in EXPR.  This is used to restrict EQUIV to "specializations"
12967    of EXPR where some registers may have been replaced by constants.  */
12968
12969 static bool
12970 unmentioned_reg_p (rtx equiv, rtx expr)
12971 {
12972   return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
12973 }
12974 \f
12975 void
12976 dump_combine_stats (FILE *file)
12977 {
12978   fprintf
12979     (file,
12980      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12981      combine_attempts, combine_merges, combine_extras, combine_successes);
12982 }
12983
12984 void
12985 dump_combine_total_stats (FILE *file)
12986 {
12987   fprintf
12988     (file,
12989      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12990      total_attempts, total_merges, total_extras, total_successes);
12991 }
12992 \f
12993 static bool
12994 gate_handle_combine (void)
12995 {
12996   return (optimize > 0);
12997 }
12998
12999 /* Try combining insns through substitution.  */
13000 static unsigned int
13001 rest_of_handle_combine (void)
13002 {
13003   int rebuild_jump_labels_after_combine;
13004
13005   df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
13006   df_note_add_problem ();
13007   df_analyze ();
13008
13009   regstat_init_n_sets_and_refs ();
13010
13011   rebuild_jump_labels_after_combine
13012     = combine_instructions (get_insns (), max_reg_num ());
13013
13014   /* Combining insns may have turned an indirect jump into a
13015      direct jump.  Rebuild the JUMP_LABEL fields of jumping
13016      instructions.  */
13017   if (rebuild_jump_labels_after_combine)
13018     {
13019       timevar_push (TV_JUMP);
13020       rebuild_jump_labels (get_insns ());
13021       cleanup_cfg (0);
13022       timevar_pop (TV_JUMP);
13023     }
13024
13025   regstat_free_n_sets_and_refs ();
13026   return 0;
13027 }
13028
13029 struct tree_opt_pass pass_combine =
13030 {
13031   "combine",                            /* name */
13032   gate_handle_combine,                  /* gate */
13033   rest_of_handle_combine,               /* execute */
13034   NULL,                                 /* sub */
13035   NULL,                                 /* next */
13036   0,                                    /* static_pass_number */
13037   TV_COMBINE,                           /* tv_id */
13038   0,                                    /* properties_required */
13039   0,                                    /* properties_provided */
13040   0,                                    /* properties_destroyed */
13041   0,                                    /* todo_flags_start */
13042   TODO_dump_func |
13043   TODO_df_finish | TODO_verify_rtl_sharing |
13044   TODO_ggc_collect,                     /* todo_flags_finish */
13045   'c'                                   /* letter */
13046 };
13047