OSDN Git Service

2008-11-03 Sebastian Pop <sebastian.pop@amd.com>
[pf3gnuchains/gcc-fork.git] / gcc / cse.c
1 /* Common subexpression elimination for GNU compiler.
2    Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998
3    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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 #include "config.h"
23 /* stdio.h must precede rtl.h for FFS.  */
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "hard-reg-set.h"
30 #include "regs.h"
31 #include "basic-block.h"
32 #include "flags.h"
33 #include "real.h"
34 #include "insn-config.h"
35 #include "recog.h"
36 #include "function.h"
37 #include "expr.h"
38 #include "toplev.h"
39 #include "output.h"
40 #include "ggc.h"
41 #include "timevar.h"
42 #include "except.h"
43 #include "target.h"
44 #include "params.h"
45 #include "rtlhooks-def.h"
46 #include "tree-pass.h"
47 #include "df.h"
48 #include "dbgcnt.h"
49
50 /* The basic idea of common subexpression elimination is to go
51    through the code, keeping a record of expressions that would
52    have the same value at the current scan point, and replacing
53    expressions encountered with the cheapest equivalent expression.
54
55    It is too complicated to keep track of the different possibilities
56    when control paths merge in this code; so, at each label, we forget all
57    that is known and start fresh.  This can be described as processing each
58    extended basic block separately.  We have a separate pass to perform
59    global CSE.
60
61    Note CSE can turn a conditional or computed jump into a nop or
62    an unconditional jump.  When this occurs we arrange to run the jump
63    optimizer after CSE to delete the unreachable code.
64
65    We use two data structures to record the equivalent expressions:
66    a hash table for most expressions, and a vector of "quantity
67    numbers" to record equivalent (pseudo) registers.
68
69    The use of the special data structure for registers is desirable
70    because it is faster.  It is possible because registers references
71    contain a fairly small number, the register number, taken from
72    a contiguously allocated series, and two register references are
73    identical if they have the same number.  General expressions
74    do not have any such thing, so the only way to retrieve the
75    information recorded on an expression other than a register
76    is to keep it in a hash table.
77
78 Registers and "quantity numbers":
79
80    At the start of each basic block, all of the (hardware and pseudo)
81    registers used in the function are given distinct quantity
82    numbers to indicate their contents.  During scan, when the code
83    copies one register into another, we copy the quantity number.
84    When a register is loaded in any other way, we allocate a new
85    quantity number to describe the value generated by this operation.
86    `REG_QTY (N)' records what quantity register N is currently thought
87    of as containing.
88
89    All real quantity numbers are greater than or equal to zero.
90    If register N has not been assigned a quantity, `REG_QTY (N)' will
91    equal -N - 1, which is always negative.
92
93    Quantity numbers below zero do not exist and none of the `qty_table'
94    entries should be referenced with a negative index.
95
96    We also maintain a bidirectional chain of registers for each
97    quantity number.  The `qty_table` members `first_reg' and `last_reg',
98    and `reg_eqv_table' members `next' and `prev' hold these chains.
99
100    The first register in a chain is the one whose lifespan is least local.
101    Among equals, it is the one that was seen first.
102    We replace any equivalent register with that one.
103
104    If two registers have the same quantity number, it must be true that
105    REG expressions with qty_table `mode' must be in the hash table for both
106    registers and must be in the same class.
107
108    The converse is not true.  Since hard registers may be referenced in
109    any mode, two REG expressions might be equivalent in the hash table
110    but not have the same quantity number if the quantity number of one
111    of the registers is not the same mode as those expressions.
112
113 Constants and quantity numbers
114
115    When a quantity has a known constant value, that value is stored
116    in the appropriate qty_table `const_rtx'.  This is in addition to
117    putting the constant in the hash table as is usual for non-regs.
118
119    Whether a reg or a constant is preferred is determined by the configuration
120    macro CONST_COSTS and will often depend on the constant value.  In any
121    event, expressions containing constants can be simplified, by fold_rtx.
122
123    When a quantity has a known nearly constant value (such as an address
124    of a stack slot), that value is stored in the appropriate qty_table
125    `const_rtx'.
126
127    Integer constants don't have a machine mode.  However, cse
128    determines the intended machine mode from the destination
129    of the instruction that moves the constant.  The machine mode
130    is recorded in the hash table along with the actual RTL
131    constant expression so that different modes are kept separate.
132
133 Other expressions:
134
135    To record known equivalences among expressions in general
136    we use a hash table called `table'.  It has a fixed number of buckets
137    that contain chains of `struct table_elt' elements for expressions.
138    These chains connect the elements whose expressions have the same
139    hash codes.
140
141    Other chains through the same elements connect the elements which
142    currently have equivalent values.
143
144    Register references in an expression are canonicalized before hashing
145    the expression.  This is done using `reg_qty' and qty_table `first_reg'.
146    The hash code of a register reference is computed using the quantity
147    number, not the register number.
148
149    When the value of an expression changes, it is necessary to remove from the
150    hash table not just that expression but all expressions whose values
151    could be different as a result.
152
153      1. If the value changing is in memory, except in special cases
154      ANYTHING referring to memory could be changed.  That is because
155      nobody knows where a pointer does not point.
156      The function `invalidate_memory' removes what is necessary.
157
158      The special cases are when the address is constant or is
159      a constant plus a fixed register such as the frame pointer
160      or a static chain pointer.  When such addresses are stored in,
161      we can tell exactly which other such addresses must be invalidated
162      due to overlap.  `invalidate' does this.
163      All expressions that refer to non-constant
164      memory addresses are also invalidated.  `invalidate_memory' does this.
165
166      2. If the value changing is a register, all expressions
167      containing references to that register, and only those,
168      must be removed.
169
170    Because searching the entire hash table for expressions that contain
171    a register is very slow, we try to figure out when it isn't necessary.
172    Precisely, this is necessary only when expressions have been
173    entered in the hash table using this register, and then the value has
174    changed, and then another expression wants to be added to refer to
175    the register's new value.  This sequence of circumstances is rare
176    within any one basic block.
177
178    `REG_TICK' and `REG_IN_TABLE', accessors for members of
179    cse_reg_info, are used to detect this case.  REG_TICK (i) is
180    incremented whenever a value is stored in register i.
181    REG_IN_TABLE (i) holds -1 if no references to register i have been
182    entered in the table; otherwise, it contains the value REG_TICK (i)
183    had when the references were entered.  If we want to enter a
184    reference and REG_IN_TABLE (i) != REG_TICK (i), we must scan and
185    remove old references.  Until we want to enter a new entry, the
186    mere fact that the two vectors don't match makes the entries be
187    ignored if anyone tries to match them.
188
189    Registers themselves are entered in the hash table as well as in
190    the equivalent-register chains.  However, `REG_TICK' and
191    `REG_IN_TABLE' do not apply to expressions which are simple
192    register references.  These expressions are removed from the table
193    immediately when they become invalid, and this can be done even if
194    we do not immediately search for all the expressions that refer to
195    the register.
196
197    A CLOBBER rtx in an instruction invalidates its operand for further
198    reuse.  A CLOBBER or SET rtx whose operand is a MEM:BLK
199    invalidates everything that resides in memory.
200
201 Related expressions:
202
203    Constant expressions that differ only by an additive integer
204    are called related.  When a constant expression is put in
205    the table, the related expression with no constant term
206    is also entered.  These are made to point at each other
207    so that it is possible to find out if there exists any
208    register equivalent to an expression related to a given expression.  */
209
210 /* Length of qty_table vector.  We know in advance we will not need
211    a quantity number this big.  */
212
213 static int max_qty;
214
215 /* Next quantity number to be allocated.
216    This is 1 + the largest number needed so far.  */
217
218 static int next_qty;
219
220 /* Per-qty information tracking.
221
222    `first_reg' and `last_reg' track the head and tail of the
223    chain of registers which currently contain this quantity.
224
225    `mode' contains the machine mode of this quantity.
226
227    `const_rtx' holds the rtx of the constant value of this
228    quantity, if known.  A summations of the frame/arg pointer
229    and a constant can also be entered here.  When this holds
230    a known value, `const_insn' is the insn which stored the
231    constant value.
232
233    `comparison_{code,const,qty}' are used to track when a
234    comparison between a quantity and some constant or register has
235    been passed.  In such a case, we know the results of the comparison
236    in case we see it again.  These members record a comparison that
237    is known to be true.  `comparison_code' holds the rtx code of such
238    a comparison, else it is set to UNKNOWN and the other two
239    comparison members are undefined.  `comparison_const' holds
240    the constant being compared against, or zero if the comparison
241    is not against a constant.  `comparison_qty' holds the quantity
242    being compared against when the result is known.  If the comparison
243    is not with a register, `comparison_qty' is -1.  */
244
245 struct qty_table_elem
246 {
247   rtx const_rtx;
248   rtx const_insn;
249   rtx comparison_const;
250   int comparison_qty;
251   unsigned int first_reg, last_reg;
252   /* The sizes of these fields should match the sizes of the
253      code and mode fields of struct rtx_def (see rtl.h).  */
254   ENUM_BITFIELD(rtx_code) comparison_code : 16;
255   ENUM_BITFIELD(machine_mode) mode : 8;
256 };
257
258 /* The table of all qtys, indexed by qty number.  */
259 static struct qty_table_elem *qty_table;
260
261 /* Structure used to pass arguments via for_each_rtx to function
262    cse_change_cc_mode.  */
263 struct change_cc_mode_args
264 {
265   rtx insn;
266   rtx newreg;
267 };
268
269 #ifdef HAVE_cc0
270 /* For machines that have a CC0, we do not record its value in the hash
271    table since its use is guaranteed to be the insn immediately following
272    its definition and any other insn is presumed to invalidate it.
273
274    Instead, we store below the current and last value assigned to CC0.
275    If it should happen to be a constant, it is stored in preference
276    to the actual assigned value.  In case it is a constant, we store
277    the mode in which the constant should be interpreted.  */
278
279 static rtx this_insn_cc0, prev_insn_cc0;
280 static enum machine_mode this_insn_cc0_mode, prev_insn_cc0_mode;
281 #endif
282
283 /* Insn being scanned.  */
284
285 static rtx this_insn;
286 static bool optimize_this_for_speed_p;
287
288 /* Index by register number, gives the number of the next (or
289    previous) register in the chain of registers sharing the same
290    value.
291
292    Or -1 if this register is at the end of the chain.
293
294    If REG_QTY (N) == -N - 1, reg_eqv_table[N].next is undefined.  */
295
296 /* Per-register equivalence chain.  */
297 struct reg_eqv_elem
298 {
299   int next, prev;
300 };
301
302 /* The table of all register equivalence chains.  */
303 static struct reg_eqv_elem *reg_eqv_table;
304
305 struct cse_reg_info
306 {
307   /* The timestamp at which this register is initialized.  */
308   unsigned int timestamp;
309
310   /* The quantity number of the register's current contents.  */
311   int reg_qty;
312
313   /* The number of times the register has been altered in the current
314      basic block.  */
315   int reg_tick;
316
317   /* The REG_TICK value at which rtx's containing this register are
318      valid in the hash table.  If this does not equal the current
319      reg_tick value, such expressions existing in the hash table are
320      invalid.  */
321   int reg_in_table;
322
323   /* The SUBREG that was set when REG_TICK was last incremented.  Set
324      to -1 if the last store was to the whole register, not a subreg.  */
325   unsigned int subreg_ticked;
326 };
327
328 /* A table of cse_reg_info indexed by register numbers.  */
329 static struct cse_reg_info *cse_reg_info_table;
330
331 /* The size of the above table.  */
332 static unsigned int cse_reg_info_table_size;
333
334 /* The index of the first entry that has not been initialized.  */
335 static unsigned int cse_reg_info_table_first_uninitialized;
336
337 /* The timestamp at the beginning of the current run of
338    cse_extended_basic_block.  We increment this variable at the beginning of
339    the current run of cse_extended_basic_block.  The timestamp field of a
340    cse_reg_info entry matches the value of this variable if and only
341    if the entry has been initialized during the current run of
342    cse_extended_basic_block.  */
343 static unsigned int cse_reg_info_timestamp;
344
345 /* A HARD_REG_SET containing all the hard registers for which there is
346    currently a REG expression in the hash table.  Note the difference
347    from the above variables, which indicate if the REG is mentioned in some
348    expression in the table.  */
349
350 static HARD_REG_SET hard_regs_in_table;
351
352 /* True if CSE has altered the CFG.  */
353 static bool cse_cfg_altered;
354
355 /* True if CSE has altered conditional jump insns in such a way
356    that jump optimization should be redone.  */
357 static bool cse_jumps_altered;
358
359 /* True if we put a LABEL_REF into the hash table for an INSN
360    without a REG_LABEL_OPERAND, we have to rerun jump after CSE
361    to put in the note.  */
362 static bool recorded_label_ref;
363
364 /* canon_hash stores 1 in do_not_record
365    if it notices a reference to CC0, PC, or some other volatile
366    subexpression.  */
367
368 static int do_not_record;
369
370 /* canon_hash stores 1 in hash_arg_in_memory
371    if it notices a reference to memory within the expression being hashed.  */
372
373 static int hash_arg_in_memory;
374
375 /* The hash table contains buckets which are chains of `struct table_elt's,
376    each recording one expression's information.
377    That expression is in the `exp' field.
378
379    The canon_exp field contains a canonical (from the point of view of
380    alias analysis) version of the `exp' field.
381
382    Those elements with the same hash code are chained in both directions
383    through the `next_same_hash' and `prev_same_hash' fields.
384
385    Each set of expressions with equivalent values
386    are on a two-way chain through the `next_same_value'
387    and `prev_same_value' fields, and all point with
388    the `first_same_value' field at the first element in
389    that chain.  The chain is in order of increasing cost.
390    Each element's cost value is in its `cost' field.
391
392    The `in_memory' field is nonzero for elements that
393    involve any reference to memory.  These elements are removed
394    whenever a write is done to an unidentified location in memory.
395    To be safe, we assume that a memory address is unidentified unless
396    the address is either a symbol constant or a constant plus
397    the frame pointer or argument pointer.
398
399    The `related_value' field is used to connect related expressions
400    (that differ by adding an integer).
401    The related expressions are chained in a circular fashion.
402    `related_value' is zero for expressions for which this
403    chain is not useful.
404
405    The `cost' field stores the cost of this element's expression.
406    The `regcost' field stores the value returned by approx_reg_cost for
407    this element's expression.
408
409    The `is_const' flag is set if the element is a constant (including
410    a fixed address).
411
412    The `flag' field is used as a temporary during some search routines.
413
414    The `mode' field is usually the same as GET_MODE (`exp'), but
415    if `exp' is a CONST_INT and has no machine mode then the `mode'
416    field is the mode it was being used as.  Each constant is
417    recorded separately for each mode it is used with.  */
418
419 struct table_elt
420 {
421   rtx exp;
422   rtx canon_exp;
423   struct table_elt *next_same_hash;
424   struct table_elt *prev_same_hash;
425   struct table_elt *next_same_value;
426   struct table_elt *prev_same_value;
427   struct table_elt *first_same_value;
428   struct table_elt *related_value;
429   int cost;
430   int regcost;
431   /* The size of this field should match the size
432      of the mode field of struct rtx_def (see rtl.h).  */
433   ENUM_BITFIELD(machine_mode) mode : 8;
434   char in_memory;
435   char is_const;
436   char flag;
437 };
438
439 /* We don't want a lot of buckets, because we rarely have very many
440    things stored in the hash table, and a lot of buckets slows
441    down a lot of loops that happen frequently.  */
442 #define HASH_SHIFT      5
443 #define HASH_SIZE       (1 << HASH_SHIFT)
444 #define HASH_MASK       (HASH_SIZE - 1)
445
446 /* Compute hash code of X in mode M.  Special-case case where X is a pseudo
447    register (hard registers may require `do_not_record' to be set).  */
448
449 #define HASH(X, M)      \
450  ((REG_P (X) && REGNO (X) >= FIRST_PSEUDO_REGISTER      \
451   ? (((unsigned) REG << 7) + (unsigned) REG_QTY (REGNO (X)))    \
452   : canon_hash (X, M)) & HASH_MASK)
453
454 /* Like HASH, but without side-effects.  */
455 #define SAFE_HASH(X, M) \
456  ((REG_P (X) && REGNO (X) >= FIRST_PSEUDO_REGISTER      \
457   ? (((unsigned) REG << 7) + (unsigned) REG_QTY (REGNO (X)))    \
458   : safe_hash (X, M)) & HASH_MASK)
459
460 /* Determine whether register number N is considered a fixed register for the
461    purpose of approximating register costs.
462    It is desirable to replace other regs with fixed regs, to reduce need for
463    non-fixed hard regs.
464    A reg wins if it is either the frame pointer or designated as fixed.  */
465 #define FIXED_REGNO_P(N)  \
466   ((N) == FRAME_POINTER_REGNUM || (N) == HARD_FRAME_POINTER_REGNUM \
467    || fixed_regs[N] || global_regs[N])
468
469 /* Compute cost of X, as stored in the `cost' field of a table_elt.  Fixed
470    hard registers and pointers into the frame are the cheapest with a cost
471    of 0.  Next come pseudos with a cost of one and other hard registers with
472    a cost of 2.  Aside from these special cases, call `rtx_cost'.  */
473
474 #define CHEAP_REGNO(N)                                                  \
475   (REGNO_PTR_FRAME_P(N)                                                 \
476    || (HARD_REGISTER_NUM_P (N)                                          \
477        && FIXED_REGNO_P (N) && REGNO_REG_CLASS (N) != NO_REGS))
478
479 #define COST(X) (REG_P (X) ? 0 : notreg_cost (X, SET))
480 #define COST_IN(X,OUTER) (REG_P (X) ? 0 : notreg_cost (X, OUTER))
481
482 /* Get the number of times this register has been updated in this
483    basic block.  */
484
485 #define REG_TICK(N) (get_cse_reg_info (N)->reg_tick)
486
487 /* Get the point at which REG was recorded in the table.  */
488
489 #define REG_IN_TABLE(N) (get_cse_reg_info (N)->reg_in_table)
490
491 /* Get the SUBREG set at the last increment to REG_TICK (-1 if not a
492    SUBREG).  */
493
494 #define SUBREG_TICKED(N) (get_cse_reg_info (N)->subreg_ticked)
495
496 /* Get the quantity number for REG.  */
497
498 #define REG_QTY(N) (get_cse_reg_info (N)->reg_qty)
499
500 /* Determine if the quantity number for register X represents a valid index
501    into the qty_table.  */
502
503 #define REGNO_QTY_VALID_P(N) (REG_QTY (N) >= 0)
504
505 static struct table_elt *table[HASH_SIZE];
506
507 /* Chain of `struct table_elt's made so far for this function
508    but currently removed from the table.  */
509
510 static struct table_elt *free_element_chain;
511
512 /* Set to the cost of a constant pool reference if one was found for a
513    symbolic constant.  If this was found, it means we should try to
514    convert constants into constant pool entries if they don't fit in
515    the insn.  */
516
517 static int constant_pool_entries_cost;
518 static int constant_pool_entries_regcost;
519
520 /* This data describes a block that will be processed by
521    cse_extended_basic_block.  */
522
523 struct cse_basic_block_data
524 {
525   /* Total number of SETs in block.  */
526   int nsets;
527   /* Size of current branch path, if any.  */
528   int path_size;
529   /* Current path, indicating which basic_blocks will be processed.  */
530   struct branch_path
531     {
532       /* The basic block for this path entry.  */
533       basic_block bb;
534     } *path;
535 };
536
537
538 /* Pointers to the live in/live out bitmaps for the boundaries of the
539    current EBB.  */
540 static bitmap cse_ebb_live_in, cse_ebb_live_out;
541
542 /* A simple bitmap to track which basic blocks have been visited
543    already as part of an already processed extended basic block.  */
544 static sbitmap cse_visited_basic_blocks;
545
546 static bool fixed_base_plus_p (rtx x);
547 static int notreg_cost (rtx, enum rtx_code);
548 static int approx_reg_cost_1 (rtx *, void *);
549 static int approx_reg_cost (rtx);
550 static int preferable (int, int, int, int);
551 static void new_basic_block (void);
552 static void make_new_qty (unsigned int, enum machine_mode);
553 static void make_regs_eqv (unsigned int, unsigned int);
554 static void delete_reg_equiv (unsigned int);
555 static int mention_regs (rtx);
556 static int insert_regs (rtx, struct table_elt *, int);
557 static void remove_from_table (struct table_elt *, unsigned);
558 static void remove_pseudo_from_table (rtx, unsigned);
559 static struct table_elt *lookup (rtx, unsigned, enum machine_mode);
560 static struct table_elt *lookup_for_remove (rtx, unsigned, enum machine_mode);
561 static rtx lookup_as_function (rtx, enum rtx_code);
562 static struct table_elt *insert (rtx, struct table_elt *, unsigned,
563                                  enum machine_mode);
564 static void merge_equiv_classes (struct table_elt *, struct table_elt *);
565 static void invalidate (rtx, enum machine_mode);
566 static bool cse_rtx_varies_p (const_rtx, bool);
567 static void remove_invalid_refs (unsigned int);
568 static void remove_invalid_subreg_refs (unsigned int, unsigned int,
569                                         enum machine_mode);
570 static void rehash_using_reg (rtx);
571 static void invalidate_memory (void);
572 static void invalidate_for_call (void);
573 static rtx use_related_value (rtx, struct table_elt *);
574
575 static inline unsigned canon_hash (rtx, enum machine_mode);
576 static inline unsigned safe_hash (rtx, enum machine_mode);
577 static inline unsigned hash_rtx_string (const char *);
578
579 static rtx canon_reg (rtx, rtx);
580 static enum rtx_code find_comparison_args (enum rtx_code, rtx *, rtx *,
581                                            enum machine_mode *,
582                                            enum machine_mode *);
583 static rtx fold_rtx (rtx, rtx);
584 static rtx equiv_constant (rtx);
585 static void record_jump_equiv (rtx, bool);
586 static void record_jump_cond (enum rtx_code, enum machine_mode, rtx, rtx,
587                               int);
588 static void cse_insn (rtx);
589 static void cse_prescan_path (struct cse_basic_block_data *);
590 static void invalidate_from_clobbers (rtx);
591 static rtx cse_process_notes (rtx, rtx, bool *);
592 static void cse_extended_basic_block (struct cse_basic_block_data *);
593 static void count_reg_usage (rtx, int *, rtx, int);
594 static int check_for_label_ref (rtx *, void *);
595 extern void dump_class (struct table_elt*);
596 static void get_cse_reg_info_1 (unsigned int regno);
597 static struct cse_reg_info * get_cse_reg_info (unsigned int regno);
598 static int check_dependence (rtx *, void *);
599
600 static void flush_hash_table (void);
601 static bool insn_live_p (rtx, int *);
602 static bool set_live_p (rtx, rtx, int *);
603 static int cse_change_cc_mode (rtx *, void *);
604 static void cse_change_cc_mode_insn (rtx, rtx);
605 static void cse_change_cc_mode_insns (rtx, rtx, rtx);
606 static enum machine_mode cse_cc_succs (basic_block, basic_block, rtx, rtx,
607                                        bool);
608 \f
609
610 #undef RTL_HOOKS_GEN_LOWPART
611 #define RTL_HOOKS_GEN_LOWPART           gen_lowpart_if_possible
612
613 static const struct rtl_hooks cse_rtl_hooks = RTL_HOOKS_INITIALIZER;
614 \f
615 /* Nonzero if X has the form (PLUS frame-pointer integer).  We check for
616    virtual regs here because the simplify_*_operation routines are called
617    by integrate.c, which is called before virtual register instantiation.  */
618
619 static bool
620 fixed_base_plus_p (rtx x)
621 {
622   switch (GET_CODE (x))
623     {
624     case REG:
625       if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx)
626         return true;
627       if (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])
628         return true;
629       if (REGNO (x) >= FIRST_VIRTUAL_REGISTER
630           && REGNO (x) <= LAST_VIRTUAL_REGISTER)
631         return true;
632       return false;
633
634     case PLUS:
635       if (GET_CODE (XEXP (x, 1)) != CONST_INT)
636         return false;
637       return fixed_base_plus_p (XEXP (x, 0));
638
639     default:
640       return false;
641     }
642 }
643
644 /* Dump the expressions in the equivalence class indicated by CLASSP.
645    This function is used only for debugging.  */
646 void
647 dump_class (struct table_elt *classp)
648 {
649   struct table_elt *elt;
650
651   fprintf (stderr, "Equivalence chain for ");
652   print_rtl (stderr, classp->exp);
653   fprintf (stderr, ": \n");
654
655   for (elt = classp->first_same_value; elt; elt = elt->next_same_value)
656     {
657       print_rtl (stderr, elt->exp);
658       fprintf (stderr, "\n");
659     }
660 }
661
662 /* Subroutine of approx_reg_cost; called through for_each_rtx.  */
663
664 static int
665 approx_reg_cost_1 (rtx *xp, void *data)
666 {
667   rtx x = *xp;
668   int *cost_p = (int *) data;
669
670   if (x && REG_P (x))
671     {
672       unsigned int regno = REGNO (x);
673
674       if (! CHEAP_REGNO (regno))
675         {
676           if (regno < FIRST_PSEUDO_REGISTER)
677             {
678               if (SMALL_REGISTER_CLASSES)
679                 return 1;
680               *cost_p += 2;
681             }
682           else
683             *cost_p += 1;
684         }
685     }
686
687   return 0;
688 }
689
690 /* Return an estimate of the cost of the registers used in an rtx.
691    This is mostly the number of different REG expressions in the rtx;
692    however for some exceptions like fixed registers we use a cost of
693    0.  If any other hard register reference occurs, return MAX_COST.  */
694
695 static int
696 approx_reg_cost (rtx x)
697 {
698   int cost = 0;
699
700   if (for_each_rtx (&x, approx_reg_cost_1, (void *) &cost))
701     return MAX_COST;
702
703   return cost;
704 }
705
706 /* Return a negative value if an rtx A, whose costs are given by COST_A
707    and REGCOST_A, is more desirable than an rtx B.
708    Return a positive value if A is less desirable, or 0 if the two are
709    equally good.  */
710 static int
711 preferable (int cost_a, int regcost_a, int cost_b, int regcost_b)
712 {
713   /* First, get rid of cases involving expressions that are entirely
714      unwanted.  */
715   if (cost_a != cost_b)
716     {
717       if (cost_a == MAX_COST)
718         return 1;
719       if (cost_b == MAX_COST)
720         return -1;
721     }
722
723   /* Avoid extending lifetimes of hardregs.  */
724   if (regcost_a != regcost_b)
725     {
726       if (regcost_a == MAX_COST)
727         return 1;
728       if (regcost_b == MAX_COST)
729         return -1;
730     }
731
732   /* Normal operation costs take precedence.  */
733   if (cost_a != cost_b)
734     return cost_a - cost_b;
735   /* Only if these are identical consider effects on register pressure.  */
736   if (regcost_a != regcost_b)
737     return regcost_a - regcost_b;
738   return 0;
739 }
740
741 /* Internal function, to compute cost when X is not a register; called
742    from COST macro to keep it simple.  */
743
744 static int
745 notreg_cost (rtx x, enum rtx_code outer)
746 {
747   return ((GET_CODE (x) == SUBREG
748            && REG_P (SUBREG_REG (x))
749            && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT
750            && GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_INT
751            && (GET_MODE_SIZE (GET_MODE (x))
752                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
753            && subreg_lowpart_p (x)
754            && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (x)),
755                                      GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))))
756           ? 0
757           : rtx_cost (x, outer, optimize_this_for_speed_p) * 2);
758 }
759
760 \f
761 /* Initialize CSE_REG_INFO_TABLE.  */
762
763 static void
764 init_cse_reg_info (unsigned int nregs)
765 {
766   /* Do we need to grow the table?  */
767   if (nregs > cse_reg_info_table_size)
768     {
769       unsigned int new_size;
770
771       if (cse_reg_info_table_size < 2048)
772         {
773           /* Compute a new size that is a power of 2 and no smaller
774              than the large of NREGS and 64.  */
775           new_size = (cse_reg_info_table_size
776                       ? cse_reg_info_table_size : 64);
777
778           while (new_size < nregs)
779             new_size *= 2;
780         }
781       else
782         {
783           /* If we need a big table, allocate just enough to hold
784              NREGS registers.  */
785           new_size = nregs;
786         }
787
788       /* Reallocate the table with NEW_SIZE entries.  */
789       if (cse_reg_info_table)
790         free (cse_reg_info_table);
791       cse_reg_info_table = XNEWVEC (struct cse_reg_info, new_size);
792       cse_reg_info_table_size = new_size;
793       cse_reg_info_table_first_uninitialized = 0;
794     }
795
796   /* Do we have all of the first NREGS entries initialized?  */
797   if (cse_reg_info_table_first_uninitialized < nregs)
798     {
799       unsigned int old_timestamp = cse_reg_info_timestamp - 1;
800       unsigned int i;
801
802       /* Put the old timestamp on newly allocated entries so that they
803          will all be considered out of date.  We do not touch those
804          entries beyond the first NREGS entries to be nice to the
805          virtual memory.  */
806       for (i = cse_reg_info_table_first_uninitialized; i < nregs; i++)
807         cse_reg_info_table[i].timestamp = old_timestamp;
808
809       cse_reg_info_table_first_uninitialized = nregs;
810     }
811 }
812
813 /* Given REGNO, initialize the cse_reg_info entry for REGNO.  */
814
815 static void
816 get_cse_reg_info_1 (unsigned int regno)
817 {
818   /* Set TIMESTAMP field to CSE_REG_INFO_TIMESTAMP so that this
819      entry will be considered to have been initialized.  */
820   cse_reg_info_table[regno].timestamp = cse_reg_info_timestamp;
821
822   /* Initialize the rest of the entry.  */
823   cse_reg_info_table[regno].reg_tick = 1;
824   cse_reg_info_table[regno].reg_in_table = -1;
825   cse_reg_info_table[regno].subreg_ticked = -1;
826   cse_reg_info_table[regno].reg_qty = -regno - 1;
827 }
828
829 /* Find a cse_reg_info entry for REGNO.  */
830
831 static inline struct cse_reg_info *
832 get_cse_reg_info (unsigned int regno)
833 {
834   struct cse_reg_info *p = &cse_reg_info_table[regno];
835
836   /* If this entry has not been initialized, go ahead and initialize
837      it.  */
838   if (p->timestamp != cse_reg_info_timestamp)
839     get_cse_reg_info_1 (regno);
840
841   return p;
842 }
843
844 /* Clear the hash table and initialize each register with its own quantity,
845    for a new basic block.  */
846
847 static void
848 new_basic_block (void)
849 {
850   int i;
851
852   next_qty = 0;
853
854   /* Invalidate cse_reg_info_table.  */
855   cse_reg_info_timestamp++;
856
857   /* Clear out hash table state for this pass.  */
858   CLEAR_HARD_REG_SET (hard_regs_in_table);
859
860   /* The per-quantity values used to be initialized here, but it is
861      much faster to initialize each as it is made in `make_new_qty'.  */
862
863   for (i = 0; i < HASH_SIZE; i++)
864     {
865       struct table_elt *first;
866
867       first = table[i];
868       if (first != NULL)
869         {
870           struct table_elt *last = first;
871
872           table[i] = NULL;
873
874           while (last->next_same_hash != NULL)
875             last = last->next_same_hash;
876
877           /* Now relink this hash entire chain into
878              the free element list.  */
879
880           last->next_same_hash = free_element_chain;
881           free_element_chain = first;
882         }
883     }
884
885 #ifdef HAVE_cc0
886   prev_insn_cc0 = 0;
887 #endif
888 }
889
890 /* Say that register REG contains a quantity in mode MODE not in any
891    register before and initialize that quantity.  */
892
893 static void
894 make_new_qty (unsigned int reg, enum machine_mode mode)
895 {
896   int q;
897   struct qty_table_elem *ent;
898   struct reg_eqv_elem *eqv;
899
900   gcc_assert (next_qty < max_qty);
901
902   q = REG_QTY (reg) = next_qty++;
903   ent = &qty_table[q];
904   ent->first_reg = reg;
905   ent->last_reg = reg;
906   ent->mode = mode;
907   ent->const_rtx = ent->const_insn = NULL_RTX;
908   ent->comparison_code = UNKNOWN;
909
910   eqv = &reg_eqv_table[reg];
911   eqv->next = eqv->prev = -1;
912 }
913
914 /* Make reg NEW equivalent to reg OLD.
915    OLD is not changing; NEW is.  */
916
917 static void
918 make_regs_eqv (unsigned int new_reg, unsigned int old_reg)
919 {
920   unsigned int lastr, firstr;
921   int q = REG_QTY (old_reg);
922   struct qty_table_elem *ent;
923
924   ent = &qty_table[q];
925
926   /* Nothing should become eqv until it has a "non-invalid" qty number.  */
927   gcc_assert (REGNO_QTY_VALID_P (old_reg));
928
929   REG_QTY (new_reg) = q;
930   firstr = ent->first_reg;
931   lastr = ent->last_reg;
932
933   /* Prefer fixed hard registers to anything.  Prefer pseudo regs to other
934      hard regs.  Among pseudos, if NEW will live longer than any other reg
935      of the same qty, and that is beyond the current basic block,
936      make it the new canonical replacement for this qty.  */
937   if (! (firstr < FIRST_PSEUDO_REGISTER && FIXED_REGNO_P (firstr))
938       /* Certain fixed registers might be of the class NO_REGS.  This means
939          that not only can they not be allocated by the compiler, but
940          they cannot be used in substitutions or canonicalizations
941          either.  */
942       && (new_reg >= FIRST_PSEUDO_REGISTER || REGNO_REG_CLASS (new_reg) != NO_REGS)
943       && ((new_reg < FIRST_PSEUDO_REGISTER && FIXED_REGNO_P (new_reg))
944           || (new_reg >= FIRST_PSEUDO_REGISTER
945               && (firstr < FIRST_PSEUDO_REGISTER
946                   || (bitmap_bit_p (cse_ebb_live_out, new_reg)
947                       && !bitmap_bit_p (cse_ebb_live_out, firstr))
948                   || (bitmap_bit_p (cse_ebb_live_in, new_reg)
949                       && !bitmap_bit_p (cse_ebb_live_in, firstr))))))
950     {
951       reg_eqv_table[firstr].prev = new_reg;
952       reg_eqv_table[new_reg].next = firstr;
953       reg_eqv_table[new_reg].prev = -1;
954       ent->first_reg = new_reg;
955     }
956   else
957     {
958       /* If NEW is a hard reg (known to be non-fixed), insert at end.
959          Otherwise, insert before any non-fixed hard regs that are at the
960          end.  Registers of class NO_REGS cannot be used as an
961          equivalent for anything.  */
962       while (lastr < FIRST_PSEUDO_REGISTER && reg_eqv_table[lastr].prev >= 0
963              && (REGNO_REG_CLASS (lastr) == NO_REGS || ! FIXED_REGNO_P (lastr))
964              && new_reg >= FIRST_PSEUDO_REGISTER)
965         lastr = reg_eqv_table[lastr].prev;
966       reg_eqv_table[new_reg].next = reg_eqv_table[lastr].next;
967       if (reg_eqv_table[lastr].next >= 0)
968         reg_eqv_table[reg_eqv_table[lastr].next].prev = new_reg;
969       else
970         qty_table[q].last_reg = new_reg;
971       reg_eqv_table[lastr].next = new_reg;
972       reg_eqv_table[new_reg].prev = lastr;
973     }
974 }
975
976 /* Remove REG from its equivalence class.  */
977
978 static void
979 delete_reg_equiv (unsigned int reg)
980 {
981   struct qty_table_elem *ent;
982   int q = REG_QTY (reg);
983   int p, n;
984
985   /* If invalid, do nothing.  */
986   if (! REGNO_QTY_VALID_P (reg))
987     return;
988
989   ent = &qty_table[q];
990
991   p = reg_eqv_table[reg].prev;
992   n = reg_eqv_table[reg].next;
993
994   if (n != -1)
995     reg_eqv_table[n].prev = p;
996   else
997     ent->last_reg = p;
998   if (p != -1)
999     reg_eqv_table[p].next = n;
1000   else
1001     ent->first_reg = n;
1002
1003   REG_QTY (reg) = -reg - 1;
1004 }
1005
1006 /* Remove any invalid expressions from the hash table
1007    that refer to any of the registers contained in expression X.
1008
1009    Make sure that newly inserted references to those registers
1010    as subexpressions will be considered valid.
1011
1012    mention_regs is not called when a register itself
1013    is being stored in the table.
1014
1015    Return 1 if we have done something that may have changed the hash code
1016    of X.  */
1017
1018 static int
1019 mention_regs (rtx x)
1020 {
1021   enum rtx_code code;
1022   int i, j;
1023   const char *fmt;
1024   int changed = 0;
1025
1026   if (x == 0)
1027     return 0;
1028
1029   code = GET_CODE (x);
1030   if (code == REG)
1031     {
1032       unsigned int regno = REGNO (x);
1033       unsigned int endregno = END_REGNO (x);
1034       unsigned int i;
1035
1036       for (i = regno; i < endregno; i++)
1037         {
1038           if (REG_IN_TABLE (i) >= 0 && REG_IN_TABLE (i) != REG_TICK (i))
1039             remove_invalid_refs (i);
1040
1041           REG_IN_TABLE (i) = REG_TICK (i);
1042           SUBREG_TICKED (i) = -1;
1043         }
1044
1045       return 0;
1046     }
1047
1048   /* If this is a SUBREG, we don't want to discard other SUBREGs of the same
1049      pseudo if they don't use overlapping words.  We handle only pseudos
1050      here for simplicity.  */
1051   if (code == SUBREG && REG_P (SUBREG_REG (x))
1052       && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER)
1053     {
1054       unsigned int i = REGNO (SUBREG_REG (x));
1055
1056       if (REG_IN_TABLE (i) >= 0 && REG_IN_TABLE (i) != REG_TICK (i))
1057         {
1058           /* If REG_IN_TABLE (i) differs from REG_TICK (i) by one, and
1059              the last store to this register really stored into this
1060              subreg, then remove the memory of this subreg.
1061              Otherwise, remove any memory of the entire register and
1062              all its subregs from the table.  */
1063           if (REG_TICK (i) - REG_IN_TABLE (i) > 1
1064               || SUBREG_TICKED (i) != REGNO (SUBREG_REG (x)))
1065             remove_invalid_refs (i);
1066           else
1067             remove_invalid_subreg_refs (i, SUBREG_BYTE (x), GET_MODE (x));
1068         }
1069
1070       REG_IN_TABLE (i) = REG_TICK (i);
1071       SUBREG_TICKED (i) = REGNO (SUBREG_REG (x));
1072       return 0;
1073     }
1074
1075   /* If X is a comparison or a COMPARE and either operand is a register
1076      that does not have a quantity, give it one.  This is so that a later
1077      call to record_jump_equiv won't cause X to be assigned a different
1078      hash code and not found in the table after that call.
1079
1080      It is not necessary to do this here, since rehash_using_reg can
1081      fix up the table later, but doing this here eliminates the need to
1082      call that expensive function in the most common case where the only
1083      use of the register is in the comparison.  */
1084
1085   if (code == COMPARE || COMPARISON_P (x))
1086     {
1087       if (REG_P (XEXP (x, 0))
1088           && ! REGNO_QTY_VALID_P (REGNO (XEXP (x, 0))))
1089         if (insert_regs (XEXP (x, 0), NULL, 0))
1090           {
1091             rehash_using_reg (XEXP (x, 0));
1092             changed = 1;
1093           }
1094
1095       if (REG_P (XEXP (x, 1))
1096           && ! REGNO_QTY_VALID_P (REGNO (XEXP (x, 1))))
1097         if (insert_regs (XEXP (x, 1), NULL, 0))
1098           {
1099             rehash_using_reg (XEXP (x, 1));
1100             changed = 1;
1101           }
1102     }
1103
1104   fmt = GET_RTX_FORMAT (code);
1105   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1106     if (fmt[i] == 'e')
1107       changed |= mention_regs (XEXP (x, i));
1108     else if (fmt[i] == 'E')
1109       for (j = 0; j < XVECLEN (x, i); j++)
1110         changed |= mention_regs (XVECEXP (x, i, j));
1111
1112   return changed;
1113 }
1114
1115 /* Update the register quantities for inserting X into the hash table
1116    with a value equivalent to CLASSP.
1117    (If the class does not contain a REG, it is irrelevant.)
1118    If MODIFIED is nonzero, X is a destination; it is being modified.
1119    Note that delete_reg_equiv should be called on a register
1120    before insert_regs is done on that register with MODIFIED != 0.
1121
1122    Nonzero value means that elements of reg_qty have changed
1123    so X's hash code may be different.  */
1124
1125 static int
1126 insert_regs (rtx x, struct table_elt *classp, int modified)
1127 {
1128   if (REG_P (x))
1129     {
1130       unsigned int regno = REGNO (x);
1131       int qty_valid;
1132
1133       /* If REGNO is in the equivalence table already but is of the
1134          wrong mode for that equivalence, don't do anything here.  */
1135
1136       qty_valid = REGNO_QTY_VALID_P (regno);
1137       if (qty_valid)
1138         {
1139           struct qty_table_elem *ent = &qty_table[REG_QTY (regno)];
1140
1141           if (ent->mode != GET_MODE (x))
1142             return 0;
1143         }
1144
1145       if (modified || ! qty_valid)
1146         {
1147           if (classp)
1148             for (classp = classp->first_same_value;
1149                  classp != 0;
1150                  classp = classp->next_same_value)
1151               if (REG_P (classp->exp)
1152                   && GET_MODE (classp->exp) == GET_MODE (x))
1153                 {
1154                   unsigned c_regno = REGNO (classp->exp);
1155
1156                   gcc_assert (REGNO_QTY_VALID_P (c_regno));
1157
1158                   /* Suppose that 5 is hard reg and 100 and 101 are
1159                      pseudos.  Consider
1160
1161                      (set (reg:si 100) (reg:si 5))
1162                      (set (reg:si 5) (reg:si 100))
1163                      (set (reg:di 101) (reg:di 5))
1164
1165                      We would now set REG_QTY (101) = REG_QTY (5), but the
1166                      entry for 5 is in SImode.  When we use this later in
1167                      copy propagation, we get the register in wrong mode.  */
1168                   if (qty_table[REG_QTY (c_regno)].mode != GET_MODE (x))
1169                     continue;
1170
1171                   make_regs_eqv (regno, c_regno);
1172                   return 1;
1173                 }
1174
1175           /* Mention_regs for a SUBREG checks if REG_TICK is exactly one larger
1176              than REG_IN_TABLE to find out if there was only a single preceding
1177              invalidation - for the SUBREG - or another one, which would be
1178              for the full register.  However, if we find here that REG_TICK
1179              indicates that the register is invalid, it means that it has
1180              been invalidated in a separate operation.  The SUBREG might be used
1181              now (then this is a recursive call), or we might use the full REG
1182              now and a SUBREG of it later.  So bump up REG_TICK so that
1183              mention_regs will do the right thing.  */
1184           if (! modified
1185               && REG_IN_TABLE (regno) >= 0
1186               && REG_TICK (regno) == REG_IN_TABLE (regno) + 1)
1187             REG_TICK (regno)++;
1188           make_new_qty (regno, GET_MODE (x));
1189           return 1;
1190         }
1191
1192       return 0;
1193     }
1194
1195   /* If X is a SUBREG, we will likely be inserting the inner register in the
1196      table.  If that register doesn't have an assigned quantity number at
1197      this point but does later, the insertion that we will be doing now will
1198      not be accessible because its hash code will have changed.  So assign
1199      a quantity number now.  */
1200
1201   else if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x))
1202            && ! REGNO_QTY_VALID_P (REGNO (SUBREG_REG (x))))
1203     {
1204       insert_regs (SUBREG_REG (x), NULL, 0);
1205       mention_regs (x);
1206       return 1;
1207     }
1208   else
1209     return mention_regs (x);
1210 }
1211 \f
1212 /* Look in or update the hash table.  */
1213
1214 /* Remove table element ELT from use in the table.
1215    HASH is its hash code, made using the HASH macro.
1216    It's an argument because often that is known in advance
1217    and we save much time not recomputing it.  */
1218
1219 static void
1220 remove_from_table (struct table_elt *elt, unsigned int hash)
1221 {
1222   if (elt == 0)
1223     return;
1224
1225   /* Mark this element as removed.  See cse_insn.  */
1226   elt->first_same_value = 0;
1227
1228   /* Remove the table element from its equivalence class.  */
1229
1230   {
1231     struct table_elt *prev = elt->prev_same_value;
1232     struct table_elt *next = elt->next_same_value;
1233
1234     if (next)
1235       next->prev_same_value = prev;
1236
1237     if (prev)
1238       prev->next_same_value = next;
1239     else
1240       {
1241         struct table_elt *newfirst = next;
1242         while (next)
1243           {
1244             next->first_same_value = newfirst;
1245             next = next->next_same_value;
1246           }
1247       }
1248   }
1249
1250   /* Remove the table element from its hash bucket.  */
1251
1252   {
1253     struct table_elt *prev = elt->prev_same_hash;
1254     struct table_elt *next = elt->next_same_hash;
1255
1256     if (next)
1257       next->prev_same_hash = prev;
1258
1259     if (prev)
1260       prev->next_same_hash = next;
1261     else if (table[hash] == elt)
1262       table[hash] = next;
1263     else
1264       {
1265         /* This entry is not in the proper hash bucket.  This can happen
1266            when two classes were merged by `merge_equiv_classes'.  Search
1267            for the hash bucket that it heads.  This happens only very
1268            rarely, so the cost is acceptable.  */
1269         for (hash = 0; hash < HASH_SIZE; hash++)
1270           if (table[hash] == elt)
1271             table[hash] = next;
1272       }
1273   }
1274
1275   /* Remove the table element from its related-value circular chain.  */
1276
1277   if (elt->related_value != 0 && elt->related_value != elt)
1278     {
1279       struct table_elt *p = elt->related_value;
1280
1281       while (p->related_value != elt)
1282         p = p->related_value;
1283       p->related_value = elt->related_value;
1284       if (p->related_value == p)
1285         p->related_value = 0;
1286     }
1287
1288   /* Now add it to the free element chain.  */
1289   elt->next_same_hash = free_element_chain;
1290   free_element_chain = elt;
1291 }
1292
1293 /* Same as above, but X is a pseudo-register.  */
1294
1295 static void
1296 remove_pseudo_from_table (rtx x, unsigned int hash)
1297 {
1298   struct table_elt *elt;
1299
1300   /* Because a pseudo-register can be referenced in more than one
1301      mode, we might have to remove more than one table entry.  */
1302   while ((elt = lookup_for_remove (x, hash, VOIDmode)))
1303     remove_from_table (elt, hash);
1304 }
1305
1306 /* Look up X in the hash table and return its table element,
1307    or 0 if X is not in the table.
1308
1309    MODE is the machine-mode of X, or if X is an integer constant
1310    with VOIDmode then MODE is the mode with which X will be used.
1311
1312    Here we are satisfied to find an expression whose tree structure
1313    looks like X.  */
1314
1315 static struct table_elt *
1316 lookup (rtx x, unsigned int hash, enum machine_mode mode)
1317 {
1318   struct table_elt *p;
1319
1320   for (p = table[hash]; p; p = p->next_same_hash)
1321     if (mode == p->mode && ((x == p->exp && REG_P (x))
1322                             || exp_equiv_p (x, p->exp, !REG_P (x), false)))
1323       return p;
1324
1325   return 0;
1326 }
1327
1328 /* Like `lookup' but don't care whether the table element uses invalid regs.
1329    Also ignore discrepancies in the machine mode of a register.  */
1330
1331 static struct table_elt *
1332 lookup_for_remove (rtx x, unsigned int hash, enum machine_mode mode)
1333 {
1334   struct table_elt *p;
1335
1336   if (REG_P (x))
1337     {
1338       unsigned int regno = REGNO (x);
1339
1340       /* Don't check the machine mode when comparing registers;
1341          invalidating (REG:SI 0) also invalidates (REG:DF 0).  */
1342       for (p = table[hash]; p; p = p->next_same_hash)
1343         if (REG_P (p->exp)
1344             && REGNO (p->exp) == regno)
1345           return p;
1346     }
1347   else
1348     {
1349       for (p = table[hash]; p; p = p->next_same_hash)
1350         if (mode == p->mode
1351             && (x == p->exp || exp_equiv_p (x, p->exp, 0, false)))
1352           return p;
1353     }
1354
1355   return 0;
1356 }
1357
1358 /* Look for an expression equivalent to X and with code CODE.
1359    If one is found, return that expression.  */
1360
1361 static rtx
1362 lookup_as_function (rtx x, enum rtx_code code)
1363 {
1364   struct table_elt *p
1365     = lookup (x, SAFE_HASH (x, VOIDmode), GET_MODE (x));
1366
1367   /* If we are looking for a CONST_INT, the mode doesn't really matter, as
1368      long as we are narrowing.  So if we looked in vain for a mode narrower
1369      than word_mode before, look for word_mode now.  */
1370   if (p == 0 && code == CONST_INT
1371       && GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (word_mode))
1372     {
1373       x = copy_rtx (x);
1374       PUT_MODE (x, word_mode);
1375       p = lookup (x, SAFE_HASH (x, VOIDmode), word_mode);
1376     }
1377
1378   if (p == 0)
1379     return 0;
1380
1381   for (p = p->first_same_value; p; p = p->next_same_value)
1382     if (GET_CODE (p->exp) == code
1383         /* Make sure this is a valid entry in the table.  */
1384         && exp_equiv_p (p->exp, p->exp, 1, false))
1385       return p->exp;
1386
1387   return 0;
1388 }
1389
1390 /* Insert X in the hash table, assuming HASH is its hash code
1391    and CLASSP is an element of the class it should go in
1392    (or 0 if a new class should be made).
1393    It is inserted at the proper position to keep the class in
1394    the order cheapest first.
1395
1396    MODE is the machine-mode of X, or if X is an integer constant
1397    with VOIDmode then MODE is the mode with which X will be used.
1398
1399    For elements of equal cheapness, the most recent one
1400    goes in front, except that the first element in the list
1401    remains first unless a cheaper element is added.  The order of
1402    pseudo-registers does not matter, as canon_reg will be called to
1403    find the cheapest when a register is retrieved from the table.
1404
1405    The in_memory field in the hash table element is set to 0.
1406    The caller must set it nonzero if appropriate.
1407
1408    You should call insert_regs (X, CLASSP, MODIFY) before calling here,
1409    and if insert_regs returns a nonzero value
1410    you must then recompute its hash code before calling here.
1411
1412    If necessary, update table showing constant values of quantities.  */
1413
1414 #define CHEAPER(X, Y) \
1415  (preferable ((X)->cost, (X)->regcost, (Y)->cost, (Y)->regcost) < 0)
1416
1417 static struct table_elt *
1418 insert (rtx x, struct table_elt *classp, unsigned int hash, enum machine_mode mode)
1419 {
1420   struct table_elt *elt;
1421
1422   /* If X is a register and we haven't made a quantity for it,
1423      something is wrong.  */
1424   gcc_assert (!REG_P (x) || REGNO_QTY_VALID_P (REGNO (x)));
1425
1426   /* If X is a hard register, show it is being put in the table.  */
1427   if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
1428     add_to_hard_reg_set (&hard_regs_in_table, GET_MODE (x), REGNO (x));
1429
1430   /* Put an element for X into the right hash bucket.  */
1431
1432   elt = free_element_chain;
1433   if (elt)
1434     free_element_chain = elt->next_same_hash;
1435   else
1436     elt = XNEW (struct table_elt);
1437
1438   elt->exp = x;
1439   elt->canon_exp = NULL_RTX;
1440   elt->cost = COST (x);
1441   elt->regcost = approx_reg_cost (x);
1442   elt->next_same_value = 0;
1443   elt->prev_same_value = 0;
1444   elt->next_same_hash = table[hash];
1445   elt->prev_same_hash = 0;
1446   elt->related_value = 0;
1447   elt->in_memory = 0;
1448   elt->mode = mode;
1449   elt->is_const = (CONSTANT_P (x) || fixed_base_plus_p (x));
1450
1451   if (table[hash])
1452     table[hash]->prev_same_hash = elt;
1453   table[hash] = elt;
1454
1455   /* Put it into the proper value-class.  */
1456   if (classp)
1457     {
1458       classp = classp->first_same_value;
1459       if (CHEAPER (elt, classp))
1460         /* Insert at the head of the class.  */
1461         {
1462           struct table_elt *p;
1463           elt->next_same_value = classp;
1464           classp->prev_same_value = elt;
1465           elt->first_same_value = elt;
1466
1467           for (p = classp; p; p = p->next_same_value)
1468             p->first_same_value = elt;
1469         }
1470       else
1471         {
1472           /* Insert not at head of the class.  */
1473           /* Put it after the last element cheaper than X.  */
1474           struct table_elt *p, *next;
1475
1476           for (p = classp; (next = p->next_same_value) && CHEAPER (next, elt);
1477                p = next);
1478
1479           /* Put it after P and before NEXT.  */
1480           elt->next_same_value = next;
1481           if (next)
1482             next->prev_same_value = elt;
1483
1484           elt->prev_same_value = p;
1485           p->next_same_value = elt;
1486           elt->first_same_value = classp;
1487         }
1488     }
1489   else
1490     elt->first_same_value = elt;
1491
1492   /* If this is a constant being set equivalent to a register or a register
1493      being set equivalent to a constant, note the constant equivalence.
1494
1495      If this is a constant, it cannot be equivalent to a different constant,
1496      and a constant is the only thing that can be cheaper than a register.  So
1497      we know the register is the head of the class (before the constant was
1498      inserted).
1499
1500      If this is a register that is not already known equivalent to a
1501      constant, we must check the entire class.
1502
1503      If this is a register that is already known equivalent to an insn,
1504      update the qtys `const_insn' to show that `this_insn' is the latest
1505      insn making that quantity equivalent to the constant.  */
1506
1507   if (elt->is_const && classp && REG_P (classp->exp)
1508       && !REG_P (x))
1509     {
1510       int exp_q = REG_QTY (REGNO (classp->exp));
1511       struct qty_table_elem *exp_ent = &qty_table[exp_q];
1512
1513       exp_ent->const_rtx = gen_lowpart (exp_ent->mode, x);
1514       exp_ent->const_insn = this_insn;
1515     }
1516
1517   else if (REG_P (x)
1518            && classp
1519            && ! qty_table[REG_QTY (REGNO (x))].const_rtx
1520            && ! elt->is_const)
1521     {
1522       struct table_elt *p;
1523
1524       for (p = classp; p != 0; p = p->next_same_value)
1525         {
1526           if (p->is_const && !REG_P (p->exp))
1527             {
1528               int x_q = REG_QTY (REGNO (x));
1529               struct qty_table_elem *x_ent = &qty_table[x_q];
1530
1531               x_ent->const_rtx
1532                 = gen_lowpart (GET_MODE (x), p->exp);
1533               x_ent->const_insn = this_insn;
1534               break;
1535             }
1536         }
1537     }
1538
1539   else if (REG_P (x)
1540            && qty_table[REG_QTY (REGNO (x))].const_rtx
1541            && GET_MODE (x) == qty_table[REG_QTY (REGNO (x))].mode)
1542     qty_table[REG_QTY (REGNO (x))].const_insn = this_insn;
1543
1544   /* If this is a constant with symbolic value,
1545      and it has a term with an explicit integer value,
1546      link it up with related expressions.  */
1547   if (GET_CODE (x) == CONST)
1548     {
1549       rtx subexp = get_related_value (x);
1550       unsigned subhash;
1551       struct table_elt *subelt, *subelt_prev;
1552
1553       if (subexp != 0)
1554         {
1555           /* Get the integer-free subexpression in the hash table.  */
1556           subhash = SAFE_HASH (subexp, mode);
1557           subelt = lookup (subexp, subhash, mode);
1558           if (subelt == 0)
1559             subelt = insert (subexp, NULL, subhash, mode);
1560           /* Initialize SUBELT's circular chain if it has none.  */
1561           if (subelt->related_value == 0)
1562             subelt->related_value = subelt;
1563           /* Find the element in the circular chain that precedes SUBELT.  */
1564           subelt_prev = subelt;
1565           while (subelt_prev->related_value != subelt)
1566             subelt_prev = subelt_prev->related_value;
1567           /* Put new ELT into SUBELT's circular chain just before SUBELT.
1568              This way the element that follows SUBELT is the oldest one.  */
1569           elt->related_value = subelt_prev->related_value;
1570           subelt_prev->related_value = elt;
1571         }
1572     }
1573
1574   return elt;
1575 }
1576 \f
1577 /* Given two equivalence classes, CLASS1 and CLASS2, put all the entries from
1578    CLASS2 into CLASS1.  This is done when we have reached an insn which makes
1579    the two classes equivalent.
1580
1581    CLASS1 will be the surviving class; CLASS2 should not be used after this
1582    call.
1583
1584    Any invalid entries in CLASS2 will not be copied.  */
1585
1586 static void
1587 merge_equiv_classes (struct table_elt *class1, struct table_elt *class2)
1588 {
1589   struct table_elt *elt, *next, *new_elt;
1590
1591   /* Ensure we start with the head of the classes.  */
1592   class1 = class1->first_same_value;
1593   class2 = class2->first_same_value;
1594
1595   /* If they were already equal, forget it.  */
1596   if (class1 == class2)
1597     return;
1598
1599   for (elt = class2; elt; elt = next)
1600     {
1601       unsigned int hash;
1602       rtx exp = elt->exp;
1603       enum machine_mode mode = elt->mode;
1604
1605       next = elt->next_same_value;
1606
1607       /* Remove old entry, make a new one in CLASS1's class.
1608          Don't do this for invalid entries as we cannot find their
1609          hash code (it also isn't necessary).  */
1610       if (REG_P (exp) || exp_equiv_p (exp, exp, 1, false))
1611         {
1612           bool need_rehash = false;
1613
1614           hash_arg_in_memory = 0;
1615           hash = HASH (exp, mode);
1616
1617           if (REG_P (exp))
1618             {
1619               need_rehash = REGNO_QTY_VALID_P (REGNO (exp));
1620               delete_reg_equiv (REGNO (exp));
1621             }
1622
1623           if (REG_P (exp) && REGNO (exp) >= FIRST_PSEUDO_REGISTER)
1624             remove_pseudo_from_table (exp, hash);
1625           else
1626             remove_from_table (elt, hash);
1627
1628           if (insert_regs (exp, class1, 0) || need_rehash)
1629             {
1630               rehash_using_reg (exp);
1631               hash = HASH (exp, mode);
1632             }
1633           new_elt = insert (exp, class1, hash, mode);
1634           new_elt->in_memory = hash_arg_in_memory;
1635         }
1636     }
1637 }
1638 \f
1639 /* Flush the entire hash table.  */
1640
1641 static void
1642 flush_hash_table (void)
1643 {
1644   int i;
1645   struct table_elt *p;
1646
1647   for (i = 0; i < HASH_SIZE; i++)
1648     for (p = table[i]; p; p = table[i])
1649       {
1650         /* Note that invalidate can remove elements
1651            after P in the current hash chain.  */
1652         if (REG_P (p->exp))
1653           invalidate (p->exp, VOIDmode);
1654         else
1655           remove_from_table (p, i);
1656       }
1657 }
1658 \f
1659 /* Function called for each rtx to check whether true dependence exist.  */
1660 struct check_dependence_data
1661 {
1662   enum machine_mode mode;
1663   rtx exp;
1664   rtx addr;
1665 };
1666
1667 static int
1668 check_dependence (rtx *x, void *data)
1669 {
1670   struct check_dependence_data *d = (struct check_dependence_data *) data;
1671   if (*x && MEM_P (*x))
1672     return canon_true_dependence (d->exp, d->mode, d->addr, *x,
1673                                   cse_rtx_varies_p);
1674   else
1675     return 0;
1676 }
1677 \f
1678 /* Remove from the hash table, or mark as invalid, all expressions whose
1679    values could be altered by storing in X.  X is a register, a subreg, or
1680    a memory reference with nonvarying address (because, when a memory
1681    reference with a varying address is stored in, all memory references are
1682    removed by invalidate_memory so specific invalidation is superfluous).
1683    FULL_MODE, if not VOIDmode, indicates that this much should be
1684    invalidated instead of just the amount indicated by the mode of X.  This
1685    is only used for bitfield stores into memory.
1686
1687    A nonvarying address may be just a register or just a symbol reference,
1688    or it may be either of those plus a numeric offset.  */
1689
1690 static void
1691 invalidate (rtx x, enum machine_mode full_mode)
1692 {
1693   int i;
1694   struct table_elt *p;
1695   rtx addr;
1696
1697   switch (GET_CODE (x))
1698     {
1699     case REG:
1700       {
1701         /* If X is a register, dependencies on its contents are recorded
1702            through the qty number mechanism.  Just change the qty number of
1703            the register, mark it as invalid for expressions that refer to it,
1704            and remove it itself.  */
1705         unsigned int regno = REGNO (x);
1706         unsigned int hash = HASH (x, GET_MODE (x));
1707
1708         /* Remove REGNO from any quantity list it might be on and indicate
1709            that its value might have changed.  If it is a pseudo, remove its
1710            entry from the hash table.
1711
1712            For a hard register, we do the first two actions above for any
1713            additional hard registers corresponding to X.  Then, if any of these
1714            registers are in the table, we must remove any REG entries that
1715            overlap these registers.  */
1716
1717         delete_reg_equiv (regno);
1718         REG_TICK (regno)++;
1719         SUBREG_TICKED (regno) = -1;
1720
1721         if (regno >= FIRST_PSEUDO_REGISTER)
1722           remove_pseudo_from_table (x, hash);
1723         else
1724           {
1725             HOST_WIDE_INT in_table
1726               = TEST_HARD_REG_BIT (hard_regs_in_table, regno);
1727             unsigned int endregno = END_HARD_REGNO (x);
1728             unsigned int tregno, tendregno, rn;
1729             struct table_elt *p, *next;
1730
1731             CLEAR_HARD_REG_BIT (hard_regs_in_table, regno);
1732
1733             for (rn = regno + 1; rn < endregno; rn++)
1734               {
1735                 in_table |= TEST_HARD_REG_BIT (hard_regs_in_table, rn);
1736                 CLEAR_HARD_REG_BIT (hard_regs_in_table, rn);
1737                 delete_reg_equiv (rn);
1738                 REG_TICK (rn)++;
1739                 SUBREG_TICKED (rn) = -1;
1740               }
1741
1742             if (in_table)
1743               for (hash = 0; hash < HASH_SIZE; hash++)
1744                 for (p = table[hash]; p; p = next)
1745                   {
1746                     next = p->next_same_hash;
1747
1748                     if (!REG_P (p->exp)
1749                         || REGNO (p->exp) >= FIRST_PSEUDO_REGISTER)
1750                       continue;
1751
1752                     tregno = REGNO (p->exp);
1753                     tendregno = END_HARD_REGNO (p->exp);
1754                     if (tendregno > regno && tregno < endregno)
1755                       remove_from_table (p, hash);
1756                   }
1757           }
1758       }
1759       return;
1760
1761     case SUBREG:
1762       invalidate (SUBREG_REG (x), VOIDmode);
1763       return;
1764
1765     case PARALLEL:
1766       for (i = XVECLEN (x, 0) - 1; i >= 0; --i)
1767         invalidate (XVECEXP (x, 0, i), VOIDmode);
1768       return;
1769
1770     case EXPR_LIST:
1771       /* This is part of a disjoint return value; extract the location in
1772          question ignoring the offset.  */
1773       invalidate (XEXP (x, 0), VOIDmode);
1774       return;
1775
1776     case MEM:
1777       addr = canon_rtx (get_addr (XEXP (x, 0)));
1778       /* Calculate the canonical version of X here so that
1779          true_dependence doesn't generate new RTL for X on each call.  */
1780       x = canon_rtx (x);
1781
1782       /* Remove all hash table elements that refer to overlapping pieces of
1783          memory.  */
1784       if (full_mode == VOIDmode)
1785         full_mode = GET_MODE (x);
1786
1787       for (i = 0; i < HASH_SIZE; i++)
1788         {
1789           struct table_elt *next;
1790
1791           for (p = table[i]; p; p = next)
1792             {
1793               next = p->next_same_hash;
1794               if (p->in_memory)
1795                 {
1796                   struct check_dependence_data d;
1797
1798                   /* Just canonicalize the expression once;
1799                      otherwise each time we call invalidate
1800                      true_dependence will canonicalize the
1801                      expression again.  */
1802                   if (!p->canon_exp)
1803                     p->canon_exp = canon_rtx (p->exp);
1804                   d.exp = x;
1805                   d.addr = addr;
1806                   d.mode = full_mode;
1807                   if (for_each_rtx (&p->canon_exp, check_dependence, &d))
1808                     remove_from_table (p, i);
1809                 }
1810             }
1811         }
1812       return;
1813
1814     default:
1815       gcc_unreachable ();
1816     }
1817 }
1818 \f
1819 /* Remove all expressions that refer to register REGNO,
1820    since they are already invalid, and we are about to
1821    mark that register valid again and don't want the old
1822    expressions to reappear as valid.  */
1823
1824 static void
1825 remove_invalid_refs (unsigned int regno)
1826 {
1827   unsigned int i;
1828   struct table_elt *p, *next;
1829
1830   for (i = 0; i < HASH_SIZE; i++)
1831     for (p = table[i]; p; p = next)
1832       {
1833         next = p->next_same_hash;
1834         if (!REG_P (p->exp)
1835             && refers_to_regno_p (regno, regno + 1, p->exp, (rtx *) 0))
1836           remove_from_table (p, i);
1837       }
1838 }
1839
1840 /* Likewise for a subreg with subreg_reg REGNO, subreg_byte OFFSET,
1841    and mode MODE.  */
1842 static void
1843 remove_invalid_subreg_refs (unsigned int regno, unsigned int offset,
1844                             enum machine_mode mode)
1845 {
1846   unsigned int i;
1847   struct table_elt *p, *next;
1848   unsigned int end = offset + (GET_MODE_SIZE (mode) - 1);
1849
1850   for (i = 0; i < HASH_SIZE; i++)
1851     for (p = table[i]; p; p = next)
1852       {
1853         rtx exp = p->exp;
1854         next = p->next_same_hash;
1855
1856         if (!REG_P (exp)
1857             && (GET_CODE (exp) != SUBREG
1858                 || !REG_P (SUBREG_REG (exp))
1859                 || REGNO (SUBREG_REG (exp)) != regno
1860                 || (((SUBREG_BYTE (exp)
1861                       + (GET_MODE_SIZE (GET_MODE (exp)) - 1)) >= offset)
1862                     && SUBREG_BYTE (exp) <= end))
1863             && refers_to_regno_p (regno, regno + 1, p->exp, (rtx *) 0))
1864           remove_from_table (p, i);
1865       }
1866 }
1867 \f
1868 /* Recompute the hash codes of any valid entries in the hash table that
1869    reference X, if X is a register, or SUBREG_REG (X) if X is a SUBREG.
1870
1871    This is called when we make a jump equivalence.  */
1872
1873 static void
1874 rehash_using_reg (rtx x)
1875 {
1876   unsigned int i;
1877   struct table_elt *p, *next;
1878   unsigned hash;
1879
1880   if (GET_CODE (x) == SUBREG)
1881     x = SUBREG_REG (x);
1882
1883   /* If X is not a register or if the register is known not to be in any
1884      valid entries in the table, we have no work to do.  */
1885
1886   if (!REG_P (x)
1887       || REG_IN_TABLE (REGNO (x)) < 0
1888       || REG_IN_TABLE (REGNO (x)) != REG_TICK (REGNO (x)))
1889     return;
1890
1891   /* Scan all hash chains looking for valid entries that mention X.
1892      If we find one and it is in the wrong hash chain, move it.  */
1893
1894   for (i = 0; i < HASH_SIZE; i++)
1895     for (p = table[i]; p; p = next)
1896       {
1897         next = p->next_same_hash;
1898         if (reg_mentioned_p (x, p->exp)
1899             && exp_equiv_p (p->exp, p->exp, 1, false)
1900             && i != (hash = SAFE_HASH (p->exp, p->mode)))
1901           {
1902             if (p->next_same_hash)
1903               p->next_same_hash->prev_same_hash = p->prev_same_hash;
1904
1905             if (p->prev_same_hash)
1906               p->prev_same_hash->next_same_hash = p->next_same_hash;
1907             else
1908               table[i] = p->next_same_hash;
1909
1910             p->next_same_hash = table[hash];
1911             p->prev_same_hash = 0;
1912             if (table[hash])
1913               table[hash]->prev_same_hash = p;
1914             table[hash] = p;
1915           }
1916       }
1917 }
1918 \f
1919 /* Remove from the hash table any expression that is a call-clobbered
1920    register.  Also update their TICK values.  */
1921
1922 static void
1923 invalidate_for_call (void)
1924 {
1925   unsigned int regno, endregno;
1926   unsigned int i;
1927   unsigned hash;
1928   struct table_elt *p, *next;
1929   int in_table = 0;
1930
1931   /* Go through all the hard registers.  For each that is clobbered in
1932      a CALL_INSN, remove the register from quantity chains and update
1933      reg_tick if defined.  Also see if any of these registers is currently
1934      in the table.  */
1935
1936   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1937     if (TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
1938       {
1939         delete_reg_equiv (regno);
1940         if (REG_TICK (regno) >= 0)
1941           {
1942             REG_TICK (regno)++;
1943             SUBREG_TICKED (regno) = -1;
1944           }
1945
1946         in_table |= (TEST_HARD_REG_BIT (hard_regs_in_table, regno) != 0);
1947       }
1948
1949   /* In the case where we have no call-clobbered hard registers in the
1950      table, we are done.  Otherwise, scan the table and remove any
1951      entry that overlaps a call-clobbered register.  */
1952
1953   if (in_table)
1954     for (hash = 0; hash < HASH_SIZE; hash++)
1955       for (p = table[hash]; p; p = next)
1956         {
1957           next = p->next_same_hash;
1958
1959           if (!REG_P (p->exp)
1960               || REGNO (p->exp) >= FIRST_PSEUDO_REGISTER)
1961             continue;
1962
1963           regno = REGNO (p->exp);
1964           endregno = END_HARD_REGNO (p->exp);
1965
1966           for (i = regno; i < endregno; i++)
1967             if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1968               {
1969                 remove_from_table (p, hash);
1970                 break;
1971               }
1972         }
1973 }
1974 \f
1975 /* Given an expression X of type CONST,
1976    and ELT which is its table entry (or 0 if it
1977    is not in the hash table),
1978    return an alternate expression for X as a register plus integer.
1979    If none can be found, return 0.  */
1980
1981 static rtx
1982 use_related_value (rtx x, struct table_elt *elt)
1983 {
1984   struct table_elt *relt = 0;
1985   struct table_elt *p, *q;
1986   HOST_WIDE_INT offset;
1987
1988   /* First, is there anything related known?
1989      If we have a table element, we can tell from that.
1990      Otherwise, must look it up.  */
1991
1992   if (elt != 0 && elt->related_value != 0)
1993     relt = elt;
1994   else if (elt == 0 && GET_CODE (x) == CONST)
1995     {
1996       rtx subexp = get_related_value (x);
1997       if (subexp != 0)
1998         relt = lookup (subexp,
1999                        SAFE_HASH (subexp, GET_MODE (subexp)),
2000                        GET_MODE (subexp));
2001     }
2002
2003   if (relt == 0)
2004     return 0;
2005
2006   /* Search all related table entries for one that has an
2007      equivalent register.  */
2008
2009   p = relt;
2010   while (1)
2011     {
2012       /* This loop is strange in that it is executed in two different cases.
2013          The first is when X is already in the table.  Then it is searching
2014          the RELATED_VALUE list of X's class (RELT).  The second case is when
2015          X is not in the table.  Then RELT points to a class for the related
2016          value.
2017
2018          Ensure that, whatever case we are in, that we ignore classes that have
2019          the same value as X.  */
2020
2021       if (rtx_equal_p (x, p->exp))
2022         q = 0;
2023       else
2024         for (q = p->first_same_value; q; q = q->next_same_value)
2025           if (REG_P (q->exp))
2026             break;
2027
2028       if (q)
2029         break;
2030
2031       p = p->related_value;
2032
2033       /* We went all the way around, so there is nothing to be found.
2034          Alternatively, perhaps RELT was in the table for some other reason
2035          and it has no related values recorded.  */
2036       if (p == relt || p == 0)
2037         break;
2038     }
2039
2040   if (q == 0)
2041     return 0;
2042
2043   offset = (get_integer_term (x) - get_integer_term (p->exp));
2044   /* Note: OFFSET may be 0 if P->xexp and X are related by commutativity.  */
2045   return plus_constant (q->exp, offset);
2046 }
2047 \f
2048
2049 /* Hash a string.  Just add its bytes up.  */
2050 static inline unsigned
2051 hash_rtx_string (const char *ps)
2052 {
2053   unsigned hash = 0;
2054   const unsigned char *p = (const unsigned char *) ps;
2055
2056   if (p)
2057     while (*p)
2058       hash += *p++;
2059
2060   return hash;
2061 }
2062
2063 /* Same as hash_rtx, but call CB on each rtx if it is not NULL.  
2064    When the callback returns true, we continue with the new rtx.  */
2065
2066 unsigned
2067 hash_rtx_cb (const_rtx x, enum machine_mode mode,
2068              int *do_not_record_p, int *hash_arg_in_memory_p,
2069              bool have_reg_qty, hash_rtx_callback_function cb)
2070 {
2071   int i, j;
2072   unsigned hash = 0;
2073   enum rtx_code code;
2074   const char *fmt;
2075   enum machine_mode newmode;
2076   rtx newx;
2077
2078   /* Used to turn recursion into iteration.  We can't rely on GCC's
2079      tail-recursion elimination since we need to keep accumulating values
2080      in HASH.  */
2081  repeat:
2082   if (x == 0)
2083     return hash;
2084
2085   /* Invoke the callback first.  */
2086   if (cb != NULL 
2087       && ((*cb) (x, mode, &newx, &newmode)))
2088     {
2089       hash += hash_rtx_cb (newx, newmode, do_not_record_p,
2090                            hash_arg_in_memory_p, have_reg_qty, cb);
2091       return hash;
2092     }
2093
2094   code = GET_CODE (x);
2095   switch (code)
2096     {
2097     case REG:
2098       {
2099         unsigned int regno = REGNO (x);
2100
2101         if (do_not_record_p && !reload_completed)
2102           {
2103             /* On some machines, we can't record any non-fixed hard register,
2104                because extending its life will cause reload problems.  We
2105                consider ap, fp, sp, gp to be fixed for this purpose.
2106
2107                We also consider CCmode registers to be fixed for this purpose;
2108                failure to do so leads to failure to simplify 0<100 type of
2109                conditionals.
2110
2111                On all machines, we can't record any global registers.
2112                Nor should we record any register that is in a small
2113                class, as defined by CLASS_LIKELY_SPILLED_P.  */
2114             bool record;
2115
2116             if (regno >= FIRST_PSEUDO_REGISTER)
2117               record = true;
2118             else if (x == frame_pointer_rtx
2119                      || x == hard_frame_pointer_rtx
2120                      || x == arg_pointer_rtx
2121                      || x == stack_pointer_rtx
2122                      || x == pic_offset_table_rtx)
2123               record = true;
2124             else if (global_regs[regno])
2125               record = false;
2126             else if (fixed_regs[regno])
2127               record = true;
2128             else if (GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
2129               record = true;
2130             else if (SMALL_REGISTER_CLASSES)
2131               record = false;
2132             else if (CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (regno)))
2133               record = false;
2134             else
2135               record = true;
2136
2137             if (!record)
2138               {
2139                 *do_not_record_p = 1;
2140                 return 0;
2141               }
2142           }
2143
2144         hash += ((unsigned int) REG << 7);
2145         hash += (have_reg_qty ? (unsigned) REG_QTY (regno) : regno);
2146         return hash;
2147       }
2148
2149     /* We handle SUBREG of a REG specially because the underlying
2150        reg changes its hash value with every value change; we don't
2151        want to have to forget unrelated subregs when one subreg changes.  */
2152     case SUBREG:
2153       {
2154         if (REG_P (SUBREG_REG (x)))
2155           {
2156             hash += (((unsigned int) SUBREG << 7)
2157                      + REGNO (SUBREG_REG (x))
2158                      + (SUBREG_BYTE (x) / UNITS_PER_WORD));
2159             return hash;
2160           }
2161         break;
2162       }
2163
2164     case CONST_INT:
2165       hash += (((unsigned int) CONST_INT << 7) + (unsigned int) mode
2166                + (unsigned int) INTVAL (x));
2167       return hash;
2168
2169     case CONST_DOUBLE:
2170       /* This is like the general case, except that it only counts
2171          the integers representing the constant.  */
2172       hash += (unsigned int) code + (unsigned int) GET_MODE (x);
2173       if (GET_MODE (x) != VOIDmode)
2174         hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
2175       else
2176         hash += ((unsigned int) CONST_DOUBLE_LOW (x)
2177                  + (unsigned int) CONST_DOUBLE_HIGH (x));
2178       return hash;
2179
2180     case CONST_FIXED:
2181       hash += (unsigned int) code + (unsigned int) GET_MODE (x);
2182       hash += fixed_hash (CONST_FIXED_VALUE (x));
2183       return hash;
2184
2185     case CONST_VECTOR:
2186       {
2187         int units;
2188         rtx elt;
2189
2190         units = CONST_VECTOR_NUNITS (x);
2191
2192         for (i = 0; i < units; ++i)
2193           {
2194             elt = CONST_VECTOR_ELT (x, i);
2195             hash += hash_rtx_cb (elt, GET_MODE (elt),
2196                                  do_not_record_p, hash_arg_in_memory_p, 
2197                                  have_reg_qty, cb);
2198           }
2199
2200         return hash;
2201       }
2202
2203       /* Assume there is only one rtx object for any given label.  */
2204     case LABEL_REF:
2205       /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
2206          differences and differences between each stage's debugging dumps.  */
2207          hash += (((unsigned int) LABEL_REF << 7)
2208                   + CODE_LABEL_NUMBER (XEXP (x, 0)));
2209       return hash;
2210
2211     case SYMBOL_REF:
2212       {
2213         /* Don't hash on the symbol's address to avoid bootstrap differences.
2214            Different hash values may cause expressions to be recorded in
2215            different orders and thus different registers to be used in the
2216            final assembler.  This also avoids differences in the dump files
2217            between various stages.  */
2218         unsigned int h = 0;
2219         const unsigned char *p = (const unsigned char *) XSTR (x, 0);
2220
2221         while (*p)
2222           h += (h << 7) + *p++; /* ??? revisit */
2223
2224         hash += ((unsigned int) SYMBOL_REF << 7) + h;
2225         return hash;
2226       }
2227
2228     case MEM:
2229       /* We don't record if marked volatile or if BLKmode since we don't
2230          know the size of the move.  */
2231       if (do_not_record_p && (MEM_VOLATILE_P (x) || GET_MODE (x) == BLKmode))
2232         {
2233           *do_not_record_p = 1;
2234           return 0;
2235         }
2236       if (hash_arg_in_memory_p && !MEM_READONLY_P (x))
2237         *hash_arg_in_memory_p = 1;
2238
2239       /* Now that we have already found this special case,
2240          might as well speed it up as much as possible.  */
2241       hash += (unsigned) MEM;
2242       x = XEXP (x, 0);
2243       goto repeat;
2244
2245     case USE:
2246       /* A USE that mentions non-volatile memory needs special
2247          handling since the MEM may be BLKmode which normally
2248          prevents an entry from being made.  Pure calls are
2249          marked by a USE which mentions BLKmode memory.
2250          See calls.c:emit_call_1.  */
2251       if (MEM_P (XEXP (x, 0))
2252           && ! MEM_VOLATILE_P (XEXP (x, 0)))
2253         {
2254           hash += (unsigned) USE;
2255           x = XEXP (x, 0);
2256
2257           if (hash_arg_in_memory_p && !MEM_READONLY_P (x))
2258             *hash_arg_in_memory_p = 1;
2259
2260           /* Now that we have already found this special case,
2261              might as well speed it up as much as possible.  */
2262           hash += (unsigned) MEM;
2263           x = XEXP (x, 0);
2264           goto repeat;
2265         }
2266       break;
2267
2268     case PRE_DEC:
2269     case PRE_INC:
2270     case POST_DEC:
2271     case POST_INC:
2272     case PRE_MODIFY:
2273     case POST_MODIFY:
2274     case PC:
2275     case CC0:
2276     case CALL:
2277     case UNSPEC_VOLATILE:
2278       if (do_not_record_p) {
2279         *do_not_record_p = 1;
2280         return 0;
2281       }
2282       else
2283         return hash;
2284       break;
2285
2286     case ASM_OPERANDS:
2287       if (do_not_record_p && MEM_VOLATILE_P (x))
2288         {
2289           *do_not_record_p = 1;
2290           return 0;
2291         }
2292       else
2293         {
2294           /* We don't want to take the filename and line into account.  */
2295           hash += (unsigned) code + (unsigned) GET_MODE (x)
2296             + hash_rtx_string (ASM_OPERANDS_TEMPLATE (x))
2297             + hash_rtx_string (ASM_OPERANDS_OUTPUT_CONSTRAINT (x))
2298             + (unsigned) ASM_OPERANDS_OUTPUT_IDX (x);
2299
2300           if (ASM_OPERANDS_INPUT_LENGTH (x))
2301             {
2302               for (i = 1; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
2303                 {
2304                   hash += (hash_rtx_cb (ASM_OPERANDS_INPUT (x, i),
2305                                         GET_MODE (ASM_OPERANDS_INPUT (x, i)),
2306                                         do_not_record_p, hash_arg_in_memory_p,
2307                                         have_reg_qty, cb)
2308                            + hash_rtx_string
2309                            (ASM_OPERANDS_INPUT_CONSTRAINT (x, i)));
2310                 }
2311
2312               hash += hash_rtx_string (ASM_OPERANDS_INPUT_CONSTRAINT (x, 0));
2313               x = ASM_OPERANDS_INPUT (x, 0);
2314               mode = GET_MODE (x);
2315               goto repeat;
2316             }
2317
2318           return hash;
2319         }
2320       break;
2321
2322     default:
2323       break;
2324     }
2325
2326   i = GET_RTX_LENGTH (code) - 1;
2327   hash += (unsigned) code + (unsigned) GET_MODE (x);
2328   fmt = GET_RTX_FORMAT (code);
2329   for (; i >= 0; i--)
2330     {
2331       switch (fmt[i])
2332         {
2333         case 'e':
2334           /* If we are about to do the last recursive call
2335              needed at this level, change it into iteration.
2336              This function  is called enough to be worth it.  */
2337           if (i == 0)
2338             {
2339               x = XEXP (x, i);
2340               goto repeat;
2341             }
2342           
2343           hash += hash_rtx_cb (XEXP (x, i), 0, do_not_record_p,
2344                                hash_arg_in_memory_p,
2345                                have_reg_qty, cb);
2346           break;
2347
2348         case 'E':
2349           for (j = 0; j < XVECLEN (x, i); j++)
2350             hash += hash_rtx_cb (XVECEXP (x, i, j), 0, do_not_record_p,
2351                                  hash_arg_in_memory_p,
2352                                  have_reg_qty, cb);
2353           break;
2354
2355         case 's':
2356           hash += hash_rtx_string (XSTR (x, i));
2357           break;
2358
2359         case 'i':
2360           hash += (unsigned int) XINT (x, i);
2361           break;
2362
2363         case '0': case 't':
2364           /* Unused.  */
2365           break;
2366
2367         default:
2368           gcc_unreachable ();
2369         }
2370     }
2371
2372   return hash;
2373 }
2374
2375 /* Hash an rtx.  We are careful to make sure the value is never negative.
2376    Equivalent registers hash identically.
2377    MODE is used in hashing for CONST_INTs only;
2378    otherwise the mode of X is used.
2379
2380    Store 1 in DO_NOT_RECORD_P if any subexpression is volatile.
2381
2382    If HASH_ARG_IN_MEMORY_P is not NULL, store 1 in it if X contains
2383    a MEM rtx which does not have the RTX_UNCHANGING_P bit set.
2384
2385    Note that cse_insn knows that the hash code of a MEM expression
2386    is just (int) MEM plus the hash code of the address.  */
2387
2388 unsigned
2389 hash_rtx (const_rtx x, enum machine_mode mode, int *do_not_record_p,
2390           int *hash_arg_in_memory_p, bool have_reg_qty)
2391 {
2392   return hash_rtx_cb (x, mode, do_not_record_p,
2393                       hash_arg_in_memory_p, have_reg_qty, NULL);
2394 }
2395
2396 /* Hash an rtx X for cse via hash_rtx.
2397    Stores 1 in do_not_record if any subexpression is volatile.
2398    Stores 1 in hash_arg_in_memory if X contains a mem rtx which
2399    does not have the RTX_UNCHANGING_P bit set.  */
2400
2401 static inline unsigned
2402 canon_hash (rtx x, enum machine_mode mode)
2403 {
2404   return hash_rtx (x, mode, &do_not_record, &hash_arg_in_memory, true);
2405 }
2406
2407 /* Like canon_hash but with no side effects, i.e. do_not_record
2408    and hash_arg_in_memory are not changed.  */
2409
2410 static inline unsigned
2411 safe_hash (rtx x, enum machine_mode mode)
2412 {
2413   int dummy_do_not_record;
2414   return hash_rtx (x, mode, &dummy_do_not_record, NULL, true);
2415 }
2416 \f
2417 /* Return 1 iff X and Y would canonicalize into the same thing,
2418    without actually constructing the canonicalization of either one.
2419    If VALIDATE is nonzero,
2420    we assume X is an expression being processed from the rtl
2421    and Y was found in the hash table.  We check register refs
2422    in Y for being marked as valid.
2423
2424    If FOR_GCSE is true, we compare X and Y for equivalence for GCSE.  */
2425
2426 int
2427 exp_equiv_p (const_rtx x, const_rtx y, int validate, bool for_gcse)
2428 {
2429   int i, j;
2430   enum rtx_code code;
2431   const char *fmt;
2432
2433   /* Note: it is incorrect to assume an expression is equivalent to itself
2434      if VALIDATE is nonzero.  */
2435   if (x == y && !validate)
2436     return 1;
2437
2438   if (x == 0 || y == 0)
2439     return x == y;
2440
2441   code = GET_CODE (x);
2442   if (code != GET_CODE (y))
2443     return 0;
2444
2445   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.  */
2446   if (GET_MODE (x) != GET_MODE (y))
2447     return 0;
2448
2449   switch (code)
2450     {
2451     case PC:
2452     case CC0:
2453     case CONST_INT:
2454     case CONST_DOUBLE:
2455     case CONST_FIXED:
2456       return x == y;
2457
2458     case LABEL_REF:
2459       return XEXP (x, 0) == XEXP (y, 0);
2460
2461     case SYMBOL_REF:
2462       return XSTR (x, 0) == XSTR (y, 0);
2463
2464     case REG:
2465       if (for_gcse)
2466         return REGNO (x) == REGNO (y);
2467       else
2468         {
2469           unsigned int regno = REGNO (y);
2470           unsigned int i;
2471           unsigned int endregno = END_REGNO (y);
2472
2473           /* If the quantities are not the same, the expressions are not
2474              equivalent.  If there are and we are not to validate, they
2475              are equivalent.  Otherwise, ensure all regs are up-to-date.  */
2476
2477           if (REG_QTY (REGNO (x)) != REG_QTY (regno))
2478             return 0;
2479
2480           if (! validate)
2481             return 1;
2482
2483           for (i = regno; i < endregno; i++)
2484             if (REG_IN_TABLE (i) != REG_TICK (i))
2485               return 0;
2486
2487           return 1;
2488         }
2489
2490     case MEM:
2491       if (for_gcse)
2492         {
2493           /* A volatile mem should not be considered equivalent to any
2494              other.  */
2495           if (MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
2496             return 0;
2497
2498           /* Can't merge two expressions in different alias sets, since we
2499              can decide that the expression is transparent in a block when
2500              it isn't, due to it being set with the different alias set.
2501
2502              Also, can't merge two expressions with different MEM_ATTRS.
2503              They could e.g. be two different entities allocated into the
2504              same space on the stack (see e.g. PR25130).  In that case, the
2505              MEM addresses can be the same, even though the two MEMs are
2506              absolutely not equivalent.  
2507    
2508              But because really all MEM attributes should be the same for
2509              equivalent MEMs, we just use the invariant that MEMs that have
2510              the same attributes share the same mem_attrs data structure.  */
2511           if (MEM_ATTRS (x) != MEM_ATTRS (y))
2512             return 0;
2513         }
2514       break;
2515
2516     /*  For commutative operations, check both orders.  */
2517     case PLUS:
2518     case MULT:
2519     case AND:
2520     case IOR:
2521     case XOR:
2522     case NE:
2523     case EQ:
2524       return ((exp_equiv_p (XEXP (x, 0), XEXP (y, 0),
2525                              validate, for_gcse)
2526                && exp_equiv_p (XEXP (x, 1), XEXP (y, 1),
2527                                 validate, for_gcse))
2528               || (exp_equiv_p (XEXP (x, 0), XEXP (y, 1),
2529                                 validate, for_gcse)
2530                   && exp_equiv_p (XEXP (x, 1), XEXP (y, 0),
2531                                    validate, for_gcse)));
2532
2533     case ASM_OPERANDS:
2534       /* We don't use the generic code below because we want to
2535          disregard filename and line numbers.  */
2536
2537       /* A volatile asm isn't equivalent to any other.  */
2538       if (MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
2539         return 0;
2540
2541       if (GET_MODE (x) != GET_MODE (y)
2542           || strcmp (ASM_OPERANDS_TEMPLATE (x), ASM_OPERANDS_TEMPLATE (y))
2543           || strcmp (ASM_OPERANDS_OUTPUT_CONSTRAINT (x),
2544                      ASM_OPERANDS_OUTPUT_CONSTRAINT (y))
2545           || ASM_OPERANDS_OUTPUT_IDX (x) != ASM_OPERANDS_OUTPUT_IDX (y)
2546           || ASM_OPERANDS_INPUT_LENGTH (x) != ASM_OPERANDS_INPUT_LENGTH (y))
2547         return 0;
2548
2549       if (ASM_OPERANDS_INPUT_LENGTH (x))
2550         {
2551           for (i = ASM_OPERANDS_INPUT_LENGTH (x) - 1; i >= 0; i--)
2552             if (! exp_equiv_p (ASM_OPERANDS_INPUT (x, i),
2553                                ASM_OPERANDS_INPUT (y, i),
2554                                validate, for_gcse)
2555                 || strcmp (ASM_OPERANDS_INPUT_CONSTRAINT (x, i),
2556                            ASM_OPERANDS_INPUT_CONSTRAINT (y, i)))
2557               return 0;
2558         }
2559
2560       return 1;
2561
2562     default:
2563       break;
2564     }
2565
2566   /* Compare the elements.  If any pair of corresponding elements
2567      fail to match, return 0 for the whole thing.  */
2568
2569   fmt = GET_RTX_FORMAT (code);
2570   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2571     {
2572       switch (fmt[i])
2573         {
2574         case 'e':
2575           if (! exp_equiv_p (XEXP (x, i), XEXP (y, i),
2576                               validate, for_gcse))
2577             return 0;
2578           break;
2579
2580         case 'E':
2581           if (XVECLEN (x, i) != XVECLEN (y, i))
2582             return 0;
2583           for (j = 0; j < XVECLEN (x, i); j++)
2584             if (! exp_equiv_p (XVECEXP (x, i, j), XVECEXP (y, i, j),
2585                                 validate, for_gcse))
2586               return 0;
2587           break;
2588
2589         case 's':
2590           if (strcmp (XSTR (x, i), XSTR (y, i)))
2591             return 0;
2592           break;
2593
2594         case 'i':
2595           if (XINT (x, i) != XINT (y, i))
2596             return 0;
2597           break;
2598
2599         case 'w':
2600           if (XWINT (x, i) != XWINT (y, i))
2601             return 0;
2602           break;
2603
2604         case '0':
2605         case 't':
2606           break;
2607
2608         default:
2609           gcc_unreachable ();
2610         }
2611     }
2612
2613   return 1;
2614 }
2615 \f
2616 /* Return 1 if X has a value that can vary even between two
2617    executions of the program.  0 means X can be compared reliably
2618    against certain constants or near-constants.  */
2619
2620 static bool
2621 cse_rtx_varies_p (const_rtx x, bool from_alias)
2622 {
2623   /* We need not check for X and the equivalence class being of the same
2624      mode because if X is equivalent to a constant in some mode, it
2625      doesn't vary in any mode.  */
2626
2627   if (REG_P (x)
2628       && REGNO_QTY_VALID_P (REGNO (x)))
2629     {
2630       int x_q = REG_QTY (REGNO (x));
2631       struct qty_table_elem *x_ent = &qty_table[x_q];
2632
2633       if (GET_MODE (x) == x_ent->mode
2634           && x_ent->const_rtx != NULL_RTX)
2635         return 0;
2636     }
2637
2638   if (GET_CODE (x) == PLUS
2639       && GET_CODE (XEXP (x, 1)) == CONST_INT
2640       && REG_P (XEXP (x, 0))
2641       && REGNO_QTY_VALID_P (REGNO (XEXP (x, 0))))
2642     {
2643       int x0_q = REG_QTY (REGNO (XEXP (x, 0)));
2644       struct qty_table_elem *x0_ent = &qty_table[x0_q];
2645
2646       if ((GET_MODE (XEXP (x, 0)) == x0_ent->mode)
2647           && x0_ent->const_rtx != NULL_RTX)
2648         return 0;
2649     }
2650
2651   /* This can happen as the result of virtual register instantiation, if
2652      the initial constant is too large to be a valid address.  This gives
2653      us a three instruction sequence, load large offset into a register,
2654      load fp minus a constant into a register, then a MEM which is the
2655      sum of the two `constant' registers.  */
2656   if (GET_CODE (x) == PLUS
2657       && REG_P (XEXP (x, 0))
2658       && REG_P (XEXP (x, 1))
2659       && REGNO_QTY_VALID_P (REGNO (XEXP (x, 0)))
2660       && REGNO_QTY_VALID_P (REGNO (XEXP (x, 1))))
2661     {
2662       int x0_q = REG_QTY (REGNO (XEXP (x, 0)));
2663       int x1_q = REG_QTY (REGNO (XEXP (x, 1)));
2664       struct qty_table_elem *x0_ent = &qty_table[x0_q];
2665       struct qty_table_elem *x1_ent = &qty_table[x1_q];
2666
2667       if ((GET_MODE (XEXP (x, 0)) == x0_ent->mode)
2668           && x0_ent->const_rtx != NULL_RTX
2669           && (GET_MODE (XEXP (x, 1)) == x1_ent->mode)
2670           && x1_ent->const_rtx != NULL_RTX)
2671         return 0;
2672     }
2673
2674   return rtx_varies_p (x, from_alias);
2675 }
2676 \f
2677 /* Subroutine of canon_reg.  Pass *XLOC through canon_reg, and validate
2678    the result if necessary.  INSN is as for canon_reg.  */
2679
2680 static void
2681 validate_canon_reg (rtx *xloc, rtx insn)
2682 {
2683   if (*xloc)
2684     {
2685       rtx new_rtx = canon_reg (*xloc, insn);
2686
2687       /* If replacing pseudo with hard reg or vice versa, ensure the
2688          insn remains valid.  Likewise if the insn has MATCH_DUPs.  */
2689       gcc_assert (insn && new_rtx);
2690       validate_change (insn, xloc, new_rtx, 1);
2691     }
2692 }
2693
2694 /* Canonicalize an expression:
2695    replace each register reference inside it
2696    with the "oldest" equivalent register.
2697
2698    If INSN is nonzero validate_change is used to ensure that INSN remains valid
2699    after we make our substitution.  The calls are made with IN_GROUP nonzero
2700    so apply_change_group must be called upon the outermost return from this
2701    function (unless INSN is zero).  The result of apply_change_group can
2702    generally be discarded since the changes we are making are optional.  */
2703
2704 static rtx
2705 canon_reg (rtx x, rtx insn)
2706 {
2707   int i;
2708   enum rtx_code code;
2709   const char *fmt;
2710
2711   if (x == 0)
2712     return x;
2713
2714   code = GET_CODE (x);
2715   switch (code)
2716     {
2717     case PC:
2718     case CC0:
2719     case CONST:
2720     case CONST_INT:
2721     case CONST_DOUBLE:
2722     case CONST_FIXED:
2723     case CONST_VECTOR:
2724     case SYMBOL_REF:
2725     case LABEL_REF:
2726     case ADDR_VEC:
2727     case ADDR_DIFF_VEC:
2728       return x;
2729
2730     case REG:
2731       {
2732         int first;
2733         int q;
2734         struct qty_table_elem *ent;
2735
2736         /* Never replace a hard reg, because hard regs can appear
2737            in more than one machine mode, and we must preserve the mode
2738            of each occurrence.  Also, some hard regs appear in
2739            MEMs that are shared and mustn't be altered.  Don't try to
2740            replace any reg that maps to a reg of class NO_REGS.  */
2741         if (REGNO (x) < FIRST_PSEUDO_REGISTER
2742             || ! REGNO_QTY_VALID_P (REGNO (x)))
2743           return x;
2744
2745         q = REG_QTY (REGNO (x));
2746         ent = &qty_table[q];
2747         first = ent->first_reg;
2748         return (first >= FIRST_PSEUDO_REGISTER ? regno_reg_rtx[first]
2749                 : REGNO_REG_CLASS (first) == NO_REGS ? x
2750                 : gen_rtx_REG (ent->mode, first));
2751       }
2752
2753     default:
2754       break;
2755     }
2756
2757   fmt = GET_RTX_FORMAT (code);
2758   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2759     {
2760       int j;
2761
2762       if (fmt[i] == 'e')
2763         validate_canon_reg (&XEXP (x, i), insn);
2764       else if (fmt[i] == 'E')
2765         for (j = 0; j < XVECLEN (x, i); j++)
2766           validate_canon_reg (&XVECEXP (x, i, j), insn);
2767     }
2768
2769   return x;
2770 }
2771 \f
2772 /* Given an operation (CODE, *PARG1, *PARG2), where code is a comparison
2773    operation (EQ, NE, GT, etc.), follow it back through the hash table and
2774    what values are being compared.
2775
2776    *PARG1 and *PARG2 are updated to contain the rtx representing the values
2777    actually being compared.  For example, if *PARG1 was (cc0) and *PARG2
2778    was (const_int 0), *PARG1 and *PARG2 will be set to the objects that were
2779    compared to produce cc0.
2780
2781    The return value is the comparison operator and is either the code of
2782    A or the code corresponding to the inverse of the comparison.  */
2783
2784 static enum rtx_code
2785 find_comparison_args (enum rtx_code code, rtx *parg1, rtx *parg2,
2786                       enum machine_mode *pmode1, enum machine_mode *pmode2)
2787 {
2788   rtx arg1, arg2;
2789
2790   arg1 = *parg1, arg2 = *parg2;
2791
2792   /* If ARG2 is const0_rtx, see what ARG1 is equivalent to.  */
2793
2794   while (arg2 == CONST0_RTX (GET_MODE (arg1)))
2795     {
2796       /* Set nonzero when we find something of interest.  */
2797       rtx x = 0;
2798       int reverse_code = 0;
2799       struct table_elt *p = 0;
2800
2801       /* If arg1 is a COMPARE, extract the comparison arguments from it.
2802          On machines with CC0, this is the only case that can occur, since
2803          fold_rtx will return the COMPARE or item being compared with zero
2804          when given CC0.  */
2805
2806       if (GET_CODE (arg1) == COMPARE && arg2 == const0_rtx)
2807         x = arg1;
2808
2809       /* If ARG1 is a comparison operator and CODE is testing for
2810          STORE_FLAG_VALUE, get the inner arguments.  */
2811
2812       else if (COMPARISON_P (arg1))
2813         {
2814 #ifdef FLOAT_STORE_FLAG_VALUE
2815           REAL_VALUE_TYPE fsfv;
2816 #endif
2817
2818           if (code == NE
2819               || (GET_MODE_CLASS (GET_MODE (arg1)) == MODE_INT
2820                   && code == LT && STORE_FLAG_VALUE == -1)
2821 #ifdef FLOAT_STORE_FLAG_VALUE
2822               || (SCALAR_FLOAT_MODE_P (GET_MODE (arg1))
2823                   && (fsfv = FLOAT_STORE_FLAG_VALUE (GET_MODE (arg1)),
2824                       REAL_VALUE_NEGATIVE (fsfv)))
2825 #endif
2826               )
2827             x = arg1;
2828           else if (code == EQ
2829                    || (GET_MODE_CLASS (GET_MODE (arg1)) == MODE_INT
2830                        && code == GE && STORE_FLAG_VALUE == -1)
2831 #ifdef FLOAT_STORE_FLAG_VALUE
2832                    || (SCALAR_FLOAT_MODE_P (GET_MODE (arg1))
2833                        && (fsfv = FLOAT_STORE_FLAG_VALUE (GET_MODE (arg1)),
2834                            REAL_VALUE_NEGATIVE (fsfv)))
2835 #endif
2836                    )
2837             x = arg1, reverse_code = 1;
2838         }
2839
2840       /* ??? We could also check for
2841
2842          (ne (and (eq (...) (const_int 1))) (const_int 0))
2843
2844          and related forms, but let's wait until we see them occurring.  */
2845
2846       if (x == 0)
2847         /* Look up ARG1 in the hash table and see if it has an equivalence
2848            that lets us see what is being compared.  */
2849         p = lookup (arg1, SAFE_HASH (arg1, GET_MODE (arg1)), GET_MODE (arg1));
2850       if (p)
2851         {
2852           p = p->first_same_value;
2853
2854           /* If what we compare is already known to be constant, that is as
2855              good as it gets.
2856              We need to break the loop in this case, because otherwise we
2857              can have an infinite loop when looking at a reg that is known
2858              to be a constant which is the same as a comparison of a reg
2859              against zero which appears later in the insn stream, which in
2860              turn is constant and the same as the comparison of the first reg
2861              against zero...  */
2862           if (p->is_const)
2863             break;
2864         }
2865
2866       for (; p; p = p->next_same_value)
2867         {
2868           enum machine_mode inner_mode = GET_MODE (p->exp);
2869 #ifdef FLOAT_STORE_FLAG_VALUE
2870           REAL_VALUE_TYPE fsfv;
2871 #endif
2872
2873           /* If the entry isn't valid, skip it.  */
2874           if (! exp_equiv_p (p->exp, p->exp, 1, false))
2875             continue;
2876
2877           if (GET_CODE (p->exp) == COMPARE
2878               /* Another possibility is that this machine has a compare insn
2879                  that includes the comparison code.  In that case, ARG1 would
2880                  be equivalent to a comparison operation that would set ARG1 to
2881                  either STORE_FLAG_VALUE or zero.  If this is an NE operation,
2882                  ORIG_CODE is the actual comparison being done; if it is an EQ,
2883                  we must reverse ORIG_CODE.  On machine with a negative value
2884                  for STORE_FLAG_VALUE, also look at LT and GE operations.  */
2885               || ((code == NE
2886                    || (code == LT
2887                        && GET_MODE_CLASS (inner_mode) == MODE_INT
2888                        && (GET_MODE_BITSIZE (inner_mode)
2889                            <= HOST_BITS_PER_WIDE_INT)
2890                        && (STORE_FLAG_VALUE
2891                            & ((HOST_WIDE_INT) 1
2892                               << (GET_MODE_BITSIZE (inner_mode) - 1))))
2893 #ifdef FLOAT_STORE_FLAG_VALUE
2894                    || (code == LT
2895                        && SCALAR_FLOAT_MODE_P (inner_mode)
2896                        && (fsfv = FLOAT_STORE_FLAG_VALUE (GET_MODE (arg1)),
2897                            REAL_VALUE_NEGATIVE (fsfv)))
2898 #endif
2899                    )
2900                   && COMPARISON_P (p->exp)))
2901             {
2902               x = p->exp;
2903               break;
2904             }
2905           else if ((code == EQ
2906                     || (code == GE
2907                         && GET_MODE_CLASS (inner_mode) == MODE_INT
2908                         && (GET_MODE_BITSIZE (inner_mode)
2909                             <= HOST_BITS_PER_WIDE_INT)
2910                         && (STORE_FLAG_VALUE
2911                             & ((HOST_WIDE_INT) 1
2912                                << (GET_MODE_BITSIZE (inner_mode) - 1))))
2913 #ifdef FLOAT_STORE_FLAG_VALUE
2914                     || (code == GE
2915                         && SCALAR_FLOAT_MODE_P (inner_mode)
2916                         && (fsfv = FLOAT_STORE_FLAG_VALUE (GET_MODE (arg1)),
2917                             REAL_VALUE_NEGATIVE (fsfv)))
2918 #endif
2919                     )
2920                    && COMPARISON_P (p->exp))
2921             {
2922               reverse_code = 1;
2923               x = p->exp;
2924               break;
2925             }
2926
2927           /* If this non-trapping address, e.g. fp + constant, the
2928              equivalent is a better operand since it may let us predict
2929              the value of the comparison.  */
2930           else if (!rtx_addr_can_trap_p (p->exp))
2931             {
2932               arg1 = p->exp;
2933               continue;
2934             }
2935         }
2936
2937       /* If we didn't find a useful equivalence for ARG1, we are done.
2938          Otherwise, set up for the next iteration.  */
2939       if (x == 0)
2940         break;
2941
2942       /* If we need to reverse the comparison, make sure that that is
2943          possible -- we can't necessarily infer the value of GE from LT
2944          with floating-point operands.  */
2945       if (reverse_code)
2946         {
2947           enum rtx_code reversed = reversed_comparison_code (x, NULL_RTX);
2948           if (reversed == UNKNOWN)
2949             break;
2950           else
2951             code = reversed;
2952         }
2953       else if (COMPARISON_P (x))
2954         code = GET_CODE (x);
2955       arg1 = XEXP (x, 0), arg2 = XEXP (x, 1);
2956     }
2957
2958   /* Return our results.  Return the modes from before fold_rtx
2959      because fold_rtx might produce const_int, and then it's too late.  */
2960   *pmode1 = GET_MODE (arg1), *pmode2 = GET_MODE (arg2);
2961   *parg1 = fold_rtx (arg1, 0), *parg2 = fold_rtx (arg2, 0);
2962
2963   return code;
2964 }
2965 \f
2966 /* If X is a nontrivial arithmetic operation on an argument for which
2967    a constant value can be determined, return the result of operating
2968    on that value, as a constant.  Otherwise, return X, possibly with
2969    one or more operands changed to a forward-propagated constant.
2970
2971    If X is a register whose contents are known, we do NOT return
2972    those contents here; equiv_constant is called to perform that task.
2973    For SUBREGs and MEMs, we do that both here and in equiv_constant.
2974
2975    INSN is the insn that we may be modifying.  If it is 0, make a copy
2976    of X before modifying it.  */
2977
2978 static rtx
2979 fold_rtx (rtx x, rtx insn)
2980 {
2981   enum rtx_code code;
2982   enum machine_mode mode;
2983   const char *fmt;
2984   int i;
2985   rtx new_rtx = 0;
2986   int changed = 0;
2987
2988   /* Operands of X.  */
2989   rtx folded_arg0;
2990   rtx folded_arg1;
2991
2992   /* Constant equivalents of first three operands of X;
2993      0 when no such equivalent is known.  */
2994   rtx const_arg0;
2995   rtx const_arg1;
2996   rtx const_arg2;
2997
2998   /* The mode of the first operand of X.  We need this for sign and zero
2999      extends.  */
3000   enum machine_mode mode_arg0;
3001
3002   if (x == 0)
3003     return x;
3004
3005   /* Try to perform some initial simplifications on X.  */
3006   code = GET_CODE (x);
3007   switch (code)
3008     {
3009     case MEM:
3010     case SUBREG:
3011       if ((new_rtx = equiv_constant (x)) != NULL_RTX)
3012         return new_rtx;
3013       return x;
3014
3015     case CONST:
3016     case CONST_INT:
3017     case CONST_DOUBLE:
3018     case CONST_FIXED:
3019     case CONST_VECTOR:
3020     case SYMBOL_REF:
3021     case LABEL_REF:
3022     case REG:
3023     case PC:
3024       /* No use simplifying an EXPR_LIST
3025          since they are used only for lists of args
3026          in a function call's REG_EQUAL note.  */
3027     case EXPR_LIST:
3028       return x;
3029
3030 #ifdef HAVE_cc0
3031     case CC0:
3032       return prev_insn_cc0;
3033 #endif
3034
3035     case ASM_OPERANDS:
3036       if (insn)
3037         {
3038           for (i = ASM_OPERANDS_INPUT_LENGTH (x) - 1; i >= 0; i--)
3039             validate_change (insn, &ASM_OPERANDS_INPUT (x, i),
3040                              fold_rtx (ASM_OPERANDS_INPUT (x, i), insn), 0);
3041         }
3042       return x;
3043
3044 #ifdef NO_FUNCTION_CSE
3045     case CALL:
3046       if (CONSTANT_P (XEXP (XEXP (x, 0), 0)))
3047         return x;
3048       break;
3049 #endif
3050
3051     /* Anything else goes through the loop below.  */
3052     default:
3053       break;
3054     }
3055
3056   mode = GET_MODE (x);
3057   const_arg0 = 0;
3058   const_arg1 = 0;
3059   const_arg2 = 0;
3060   mode_arg0 = VOIDmode;
3061
3062   /* Try folding our operands.
3063      Then see which ones have constant values known.  */
3064
3065   fmt = GET_RTX_FORMAT (code);
3066   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3067     if (fmt[i] == 'e')
3068       {
3069         rtx folded_arg = XEXP (x, i), const_arg;
3070         enum machine_mode mode_arg = GET_MODE (folded_arg);
3071
3072         switch (GET_CODE (folded_arg))
3073           {
3074           case MEM:
3075           case REG:
3076           case SUBREG:
3077             const_arg = equiv_constant (folded_arg);
3078             break;
3079
3080           case CONST:
3081           case CONST_INT:
3082           case SYMBOL_REF:
3083           case LABEL_REF:
3084           case CONST_DOUBLE:
3085           case CONST_FIXED:
3086           case CONST_VECTOR:
3087             const_arg = folded_arg;
3088             break;
3089
3090 #ifdef HAVE_cc0
3091           case CC0:
3092             folded_arg = prev_insn_cc0;
3093             mode_arg = prev_insn_cc0_mode;
3094             const_arg = equiv_constant (folded_arg);
3095             break;
3096 #endif
3097
3098           default:
3099             folded_arg = fold_rtx (folded_arg, insn);
3100             const_arg = equiv_constant (folded_arg);
3101             break;
3102           }
3103
3104         /* For the first three operands, see if the operand
3105            is constant or equivalent to a constant.  */
3106         switch (i)
3107           {
3108           case 0:
3109             folded_arg0 = folded_arg;
3110             const_arg0 = const_arg;
3111             mode_arg0 = mode_arg;
3112             break;
3113           case 1:
3114             folded_arg1 = folded_arg;
3115             const_arg1 = const_arg;
3116             break;
3117           case 2:
3118             const_arg2 = const_arg;
3119             break;
3120           }
3121
3122         /* Pick the least expensive of the argument and an equivalent constant
3123            argument.  */
3124         if (const_arg != 0
3125             && const_arg != folded_arg
3126             && COST_IN (const_arg, code) <= COST_IN (folded_arg, code)
3127
3128             /* It's not safe to substitute the operand of a conversion
3129                operator with a constant, as the conversion's identity
3130                depends upon the mode of its operand.  This optimization
3131                is handled by the call to simplify_unary_operation.  */
3132             && (GET_RTX_CLASS (code) != RTX_UNARY
3133                 || GET_MODE (const_arg) == mode_arg0
3134                 || (code != ZERO_EXTEND
3135                     && code != SIGN_EXTEND
3136                     && code != TRUNCATE
3137                     && code != FLOAT_TRUNCATE
3138                     && code != FLOAT_EXTEND
3139                     && code != FLOAT
3140                     && code != FIX
3141                     && code != UNSIGNED_FLOAT
3142                     && code != UNSIGNED_FIX)))
3143           folded_arg = const_arg;
3144
3145         if (folded_arg == XEXP (x, i))
3146           continue;
3147
3148         if (insn == NULL_RTX && !changed)
3149           x = copy_rtx (x);
3150         changed = 1;
3151         validate_unshare_change (insn, &XEXP (x, i), folded_arg, 1);
3152       }
3153
3154   if (changed)
3155     {
3156       /* Canonicalize X if necessary, and keep const_argN and folded_argN
3157          consistent with the order in X.  */
3158       if (canonicalize_change_group (insn, x))
3159         {
3160           rtx tem;
3161           tem = const_arg0, const_arg0 = const_arg1, const_arg1 = tem;
3162           tem = folded_arg0, folded_arg0 = folded_arg1, folded_arg1 = tem;
3163         }
3164
3165       apply_change_group ();
3166     }
3167
3168   /* If X is an arithmetic operation, see if we can simplify it.  */
3169
3170   switch (GET_RTX_CLASS (code))
3171     {
3172     case RTX_UNARY:
3173       {
3174         int is_const = 0;
3175
3176         /* We can't simplify extension ops unless we know the
3177            original mode.  */
3178         if ((code == ZERO_EXTEND || code == SIGN_EXTEND)
3179             && mode_arg0 == VOIDmode)
3180           break;
3181
3182         /* If we had a CONST, strip it off and put it back later if we
3183            fold.  */
3184         if (const_arg0 != 0 && GET_CODE (const_arg0) == CONST)
3185           is_const = 1, const_arg0 = XEXP (const_arg0, 0);
3186
3187         new_rtx = simplify_unary_operation (code, mode,
3188                                         const_arg0 ? const_arg0 : folded_arg0,
3189                                         mode_arg0);
3190         /* NEG of PLUS could be converted into MINUS, but that causes
3191            expressions of the form
3192            (CONST (MINUS (CONST_INT) (SYMBOL_REF)))
3193            which many ports mistakenly treat as LEGITIMATE_CONSTANT_P.
3194            FIXME: those ports should be fixed.  */
3195         if (new_rtx != 0 && is_const
3196             && GET_CODE (new_rtx) == PLUS
3197             && (GET_CODE (XEXP (new_rtx, 0)) == SYMBOL_REF
3198                 || GET_CODE (XEXP (new_rtx, 0)) == LABEL_REF)
3199             && GET_CODE (XEXP (new_rtx, 1)) == CONST_INT)
3200           new_rtx = gen_rtx_CONST (mode, new_rtx);
3201       }
3202       break;
3203
3204     case RTX_COMPARE:
3205     case RTX_COMM_COMPARE:
3206       /* See what items are actually being compared and set FOLDED_ARG[01]
3207          to those values and CODE to the actual comparison code.  If any are
3208          constant, set CONST_ARG0 and CONST_ARG1 appropriately.  We needn't
3209          do anything if both operands are already known to be constant.  */
3210
3211       /* ??? Vector mode comparisons are not supported yet.  */
3212       if (VECTOR_MODE_P (mode))
3213         break;
3214
3215       if (const_arg0 == 0 || const_arg1 == 0)
3216         {
3217           struct table_elt *p0, *p1;
3218           rtx true_rtx, false_rtx;
3219           enum machine_mode mode_arg1;
3220
3221           if (SCALAR_FLOAT_MODE_P (mode))
3222             {
3223 #ifdef FLOAT_STORE_FLAG_VALUE
3224               true_rtx = (CONST_DOUBLE_FROM_REAL_VALUE
3225                           (FLOAT_STORE_FLAG_VALUE (mode), mode));
3226 #else
3227               true_rtx = NULL_RTX;
3228 #endif
3229               false_rtx = CONST0_RTX (mode);
3230             }
3231           else
3232             {
3233               true_rtx = const_true_rtx;
3234               false_rtx = const0_rtx;
3235             }
3236
3237           code = find_comparison_args (code, &folded_arg0, &folded_arg1,
3238                                        &mode_arg0, &mode_arg1);
3239
3240           /* If the mode is VOIDmode or a MODE_CC mode, we don't know
3241              what kinds of things are being compared, so we can't do
3242              anything with this comparison.  */
3243
3244           if (mode_arg0 == VOIDmode || GET_MODE_CLASS (mode_arg0) == MODE_CC)
3245             break;
3246
3247           const_arg0 = equiv_constant (folded_arg0);
3248           const_arg1 = equiv_constant (folded_arg1);
3249
3250           /* If we do not now have two constants being compared, see
3251              if we can nevertheless deduce some things about the
3252              comparison.  */
3253           if (const_arg0 == 0 || const_arg1 == 0)
3254             {
3255               if (const_arg1 != NULL)
3256                 {
3257                   rtx cheapest_simplification;
3258                   int cheapest_cost;
3259                   rtx simp_result;
3260                   struct table_elt *p;
3261
3262                   /* See if we can find an equivalent of folded_arg0
3263                      that gets us a cheaper expression, possibly a
3264                      constant through simplifications.  */
3265                   p = lookup (folded_arg0, SAFE_HASH (folded_arg0, mode_arg0),
3266                               mode_arg0);
3267                   
3268                   if (p != NULL)
3269                     {
3270                       cheapest_simplification = x;
3271                       cheapest_cost = COST (x);
3272
3273                       for (p = p->first_same_value; p != NULL; p = p->next_same_value)
3274                         {
3275                           int cost;
3276
3277                           /* If the entry isn't valid, skip it.  */
3278                           if (! exp_equiv_p (p->exp, p->exp, 1, false))
3279                             continue;
3280
3281                           /* Try to simplify using this equivalence.  */
3282                           simp_result
3283                             = simplify_relational_operation (code, mode,
3284                                                              mode_arg0,
3285                                                              p->exp,
3286                                                              const_arg1);
3287
3288                           if (simp_result == NULL)
3289                             continue;
3290
3291                           cost = COST (simp_result);
3292                           if (cost < cheapest_cost)
3293                             {
3294                               cheapest_cost = cost;
3295                               cheapest_simplification = simp_result;
3296                             }
3297                         }
3298
3299                       /* If we have a cheaper expression now, use that
3300                          and try folding it further, from the top.  */
3301                       if (cheapest_simplification != x)
3302                         return fold_rtx (copy_rtx (cheapest_simplification),
3303                                          insn);
3304                     }
3305                 }
3306
3307               /* See if the two operands are the same.  */
3308
3309               if ((REG_P (folded_arg0)
3310                    && REG_P (folded_arg1)
3311                    && (REG_QTY (REGNO (folded_arg0))
3312                        == REG_QTY (REGNO (folded_arg1))))
3313                   || ((p0 = lookup (folded_arg0,
3314                                     SAFE_HASH (folded_arg0, mode_arg0),
3315                                     mode_arg0))
3316                       && (p1 = lookup (folded_arg1,
3317                                        SAFE_HASH (folded_arg1, mode_arg0),
3318                                        mode_arg0))
3319                       && p0->first_same_value == p1->first_same_value))
3320                 folded_arg1 = folded_arg0;
3321
3322               /* If FOLDED_ARG0 is a register, see if the comparison we are
3323                  doing now is either the same as we did before or the reverse
3324                  (we only check the reverse if not floating-point).  */
3325               else if (REG_P (folded_arg0))
3326                 {
3327                   int qty = REG_QTY (REGNO (folded_arg0));
3328
3329                   if (REGNO_QTY_VALID_P (REGNO (folded_arg0)))
3330                     {
3331                       struct qty_table_elem *ent = &qty_table[qty];
3332
3333                       if ((comparison_dominates_p (ent->comparison_code, code)
3334                            || (! FLOAT_MODE_P (mode_arg0)
3335                                && comparison_dominates_p (ent->comparison_code,
3336                                                           reverse_condition (code))))
3337                           && (rtx_equal_p (ent->comparison_const, folded_arg1)
3338                               || (const_arg1
3339                                   && rtx_equal_p (ent->comparison_const,
3340                                                   const_arg1))
3341                               || (REG_P (folded_arg1)
3342                                   && (REG_QTY (REGNO (folded_arg1)) == ent->comparison_qty))))
3343                         {
3344                           if (comparison_dominates_p (ent->comparison_code, code))
3345                             {
3346                               if (true_rtx)
3347                                 return true_rtx;
3348                               else
3349                                 break;
3350                             }
3351                           else
3352                             return false_rtx;
3353                         }
3354                     }
3355                 }
3356             }
3357         }
3358
3359       /* If we are comparing against zero, see if the first operand is
3360          equivalent to an IOR with a constant.  If so, we may be able to
3361          determine the result of this comparison.  */
3362       if (const_arg1 == const0_rtx && !const_arg0)
3363         {
3364           rtx y = lookup_as_function (folded_arg0, IOR);
3365           rtx inner_const;
3366
3367           if (y != 0
3368               && (inner_const = equiv_constant (XEXP (y, 1))) != 0
3369               && GET_CODE (inner_const) == CONST_INT
3370               && INTVAL (inner_const) != 0)
3371             folded_arg0 = gen_rtx_IOR (mode_arg0, XEXP (y, 0), inner_const);
3372         }
3373
3374       {
3375         rtx op0 = const_arg0 ? const_arg0 : folded_arg0;
3376         rtx op1 = const_arg1 ? const_arg1 : folded_arg1;
3377         new_rtx = simplify_relational_operation (code, mode, mode_arg0, op0, op1);
3378       }
3379       break;
3380
3381     case RTX_BIN_ARITH:
3382     case RTX_COMM_ARITH:
3383       switch (code)
3384         {
3385         case PLUS:
3386           /* If the second operand is a LABEL_REF, see if the first is a MINUS
3387              with that LABEL_REF as its second operand.  If so, the result is
3388              the first operand of that MINUS.  This handles switches with an
3389              ADDR_DIFF_VEC table.  */
3390           if (const_arg1 && GET_CODE (const_arg1) == LABEL_REF)
3391             {
3392               rtx y
3393                 = GET_CODE (folded_arg0) == MINUS ? folded_arg0
3394                 : lookup_as_function (folded_arg0, MINUS);
3395
3396               if (y != 0 && GET_CODE (XEXP (y, 1)) == LABEL_REF
3397                   && XEXP (XEXP (y, 1), 0) == XEXP (const_arg1, 0))
3398                 return XEXP (y, 0);
3399
3400               /* Now try for a CONST of a MINUS like the above.  */
3401               if ((y = (GET_CODE (folded_arg0) == CONST ? folded_arg0
3402                         : lookup_as_function (folded_arg0, CONST))) != 0
3403                   && GET_CODE (XEXP (y, 0)) == MINUS
3404                   && GET_CODE (XEXP (XEXP (y, 0), 1)) == LABEL_REF
3405                   && XEXP (XEXP (XEXP (y, 0), 1), 0) == XEXP (const_arg1, 0))
3406                 return XEXP (XEXP (y, 0), 0);
3407             }
3408
3409           /* Likewise if the operands are in the other order.  */
3410           if (const_arg0 && GET_CODE (const_arg0) == LABEL_REF)
3411             {
3412               rtx y
3413                 = GET_CODE (folded_arg1) == MINUS ? folded_arg1
3414                 : lookup_as_function (folded_arg1, MINUS);
3415
3416               if (y != 0 && GET_CODE (XEXP (y, 1)) == LABEL_REF
3417                   && XEXP (XEXP (y, 1), 0) == XEXP (const_arg0, 0))
3418                 return XEXP (y, 0);
3419
3420               /* Now try for a CONST of a MINUS like the above.  */
3421               if ((y = (GET_CODE (folded_arg1) == CONST ? folded_arg1
3422                         : lookup_as_function (folded_arg1, CONST))) != 0
3423                   && GET_CODE (XEXP (y, 0)) == MINUS
3424                   && GET_CODE (XEXP (XEXP (y, 0), 1)) == LABEL_REF
3425                   && XEXP (XEXP (XEXP (y, 0), 1), 0) == XEXP (const_arg0, 0))
3426                 return XEXP (XEXP (y, 0), 0);
3427             }
3428
3429           /* If second operand is a register equivalent to a negative
3430              CONST_INT, see if we can find a register equivalent to the
3431              positive constant.  Make a MINUS if so.  Don't do this for
3432              a non-negative constant since we might then alternate between
3433              choosing positive and negative constants.  Having the positive
3434              constant previously-used is the more common case.  Be sure
3435              the resulting constant is non-negative; if const_arg1 were
3436              the smallest negative number this would overflow: depending
3437              on the mode, this would either just be the same value (and
3438              hence not save anything) or be incorrect.  */
3439           if (const_arg1 != 0 && GET_CODE (const_arg1) == CONST_INT
3440               && INTVAL (const_arg1) < 0
3441               /* This used to test
3442
3443                  -INTVAL (const_arg1) >= 0
3444
3445                  But The Sun V5.0 compilers mis-compiled that test.  So
3446                  instead we test for the problematic value in a more direct
3447                  manner and hope the Sun compilers get it correct.  */
3448               && INTVAL (const_arg1) !=
3449                 ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1))
3450               && REG_P (folded_arg1))
3451             {
3452               rtx new_const = GEN_INT (-INTVAL (const_arg1));
3453               struct table_elt *p
3454                 = lookup (new_const, SAFE_HASH (new_const, mode), mode);
3455
3456               if (p)
3457                 for (p = p->first_same_value; p; p = p->next_same_value)
3458                   if (REG_P (p->exp))
3459                     return simplify_gen_binary (MINUS, mode, folded_arg0,
3460                                                 canon_reg (p->exp, NULL_RTX));
3461             }
3462           goto from_plus;
3463
3464         case MINUS:
3465           /* If we have (MINUS Y C), see if Y is known to be (PLUS Z C2).
3466              If so, produce (PLUS Z C2-C).  */
3467           if (const_arg1 != 0 && GET_CODE (const_arg1) == CONST_INT)
3468             {
3469               rtx y = lookup_as_function (XEXP (x, 0), PLUS);
3470               if (y && GET_CODE (XEXP (y, 1)) == CONST_INT)
3471                 return fold_rtx (plus_constant (copy_rtx (y),
3472                                                 -INTVAL (const_arg1)),
3473                                  NULL_RTX);
3474             }
3475
3476           /* Fall through.  */
3477
3478         from_plus:
3479         case SMIN:    case SMAX:      case UMIN:    case UMAX:
3480         case IOR:     case AND:       case XOR:
3481         case MULT:
3482         case ASHIFT:  case LSHIFTRT:  case ASHIFTRT:
3483           /* If we have (<op> <reg> <const_int>) for an associative OP and REG
3484              is known to be of similar form, we may be able to replace the
3485              operation with a combined operation.  This may eliminate the
3486              intermediate operation if every use is simplified in this way.
3487              Note that the similar optimization done by combine.c only works
3488              if the intermediate operation's result has only one reference.  */
3489
3490           if (REG_P (folded_arg0)
3491               && const_arg1 && GET_CODE (const_arg1) == CONST_INT)
3492             {
3493               int is_shift
3494                 = (code == ASHIFT || code == ASHIFTRT || code == LSHIFTRT);
3495               rtx y, inner_const, new_const;
3496               enum rtx_code associate_code;
3497
3498               if (is_shift
3499                   && (INTVAL (const_arg1) >= GET_MODE_BITSIZE (mode)
3500                       || INTVAL (const_arg1) < 0))
3501                 {
3502                   if (SHIFT_COUNT_TRUNCATED)
3503                     const_arg1 = GEN_INT (INTVAL (const_arg1)
3504                                           & (GET_MODE_BITSIZE (mode) - 1));
3505                   else
3506                     break;
3507                 }
3508
3509               y = lookup_as_function (folded_arg0, code);
3510               if (y == 0)
3511                 break;
3512
3513               /* If we have compiled a statement like
3514                  "if (x == (x & mask1))", and now are looking at
3515                  "x & mask2", we will have a case where the first operand
3516                  of Y is the same as our first operand.  Unless we detect
3517                  this case, an infinite loop will result.  */
3518               if (XEXP (y, 0) == folded_arg0)
3519                 break;
3520
3521               inner_const = equiv_constant (fold_rtx (XEXP (y, 1), 0));
3522               if (!inner_const || GET_CODE (inner_const) != CONST_INT)
3523                 break;
3524
3525               /* Don't associate these operations if they are a PLUS with the
3526                  same constant and it is a power of two.  These might be doable
3527                  with a pre- or post-increment.  Similarly for two subtracts of
3528                  identical powers of two with post decrement.  */
3529
3530               if (code == PLUS && const_arg1 == inner_const
3531                   && ((HAVE_PRE_INCREMENT
3532                           && exact_log2 (INTVAL (const_arg1)) >= 0)
3533                       || (HAVE_POST_INCREMENT
3534                           && exact_log2 (INTVAL (const_arg1)) >= 0)
3535                       || (HAVE_PRE_DECREMENT
3536                           && exact_log2 (- INTVAL (const_arg1)) >= 0)
3537                       || (HAVE_POST_DECREMENT
3538                           && exact_log2 (- INTVAL (const_arg1)) >= 0)))
3539                 break;
3540
3541               /* ??? Vector mode shifts by scalar
3542                  shift operand are not supported yet.  */
3543               if (is_shift && VECTOR_MODE_P (mode))
3544                 break;
3545
3546               if (is_shift
3547                   && (INTVAL (inner_const) >= GET_MODE_BITSIZE (mode)
3548                       || INTVAL (inner_const) < 0))
3549                 {
3550                   if (SHIFT_COUNT_TRUNCATED)
3551                     inner_const = GEN_INT (INTVAL (inner_const)
3552                                            & (GET_MODE_BITSIZE (mode) - 1));
3553                   else
3554                     break;
3555                 }
3556
3557               /* Compute the code used to compose the constants.  For example,
3558                  A-C1-C2 is A-(C1 + C2), so if CODE == MINUS, we want PLUS.  */
3559
3560               associate_code = (is_shift || code == MINUS ? PLUS : code);
3561
3562               new_const = simplify_binary_operation (associate_code, mode,
3563                                                      const_arg1, inner_const);
3564
3565               if (new_const == 0)
3566                 break;
3567
3568               /* If we are associating shift operations, don't let this
3569                  produce a shift of the size of the object or larger.
3570                  This could occur when we follow a sign-extend by a right
3571                  shift on a machine that does a sign-extend as a pair
3572                  of shifts.  */
3573
3574               if (is_shift
3575                   && GET_CODE (new_const) == CONST_INT
3576                   && INTVAL (new_const) >= GET_MODE_BITSIZE (mode))
3577                 {
3578                   /* As an exception, we can turn an ASHIFTRT of this
3579                      form into a shift of the number of bits - 1.  */
3580                   if (code == ASHIFTRT)
3581                     new_const = GEN_INT (GET_MODE_BITSIZE (mode) - 1);
3582                   else if (!side_effects_p (XEXP (y, 0)))
3583                     return CONST0_RTX (mode);
3584                   else
3585                     break;
3586                 }
3587
3588               y = copy_rtx (XEXP (y, 0));
3589
3590               /* If Y contains our first operand (the most common way this
3591                  can happen is if Y is a MEM), we would do into an infinite
3592                  loop if we tried to fold it.  So don't in that case.  */
3593
3594               if (! reg_mentioned_p (folded_arg0, y))
3595                 y = fold_rtx (y, insn);
3596
3597               return simplify_gen_binary (code, mode, y, new_const);
3598             }
3599           break;
3600
3601         case DIV:       case UDIV:
3602           /* ??? The associative optimization performed immediately above is
3603              also possible for DIV and UDIV using associate_code of MULT.
3604              However, we would need extra code to verify that the
3605              multiplication does not overflow, that is, there is no overflow
3606              in the calculation of new_const.  */
3607           break;
3608
3609         default:
3610           break;
3611         }
3612
3613       new_rtx = simplify_binary_operation (code, mode,
3614                                        const_arg0 ? const_arg0 : folded_arg0,
3615                                        const_arg1 ? const_arg1 : folded_arg1);
3616       break;
3617
3618     case RTX_OBJ:
3619       /* (lo_sum (high X) X) is simply X.  */
3620       if (code == LO_SUM && const_arg0 != 0
3621           && GET_CODE (const_arg0) == HIGH
3622           && rtx_equal_p (XEXP (const_arg0, 0), const_arg1))
3623         return const_arg1;
3624       break;
3625
3626     case RTX_TERNARY:
3627     case RTX_BITFIELD_OPS:
3628       new_rtx = simplify_ternary_operation (code, mode, mode_arg0,
3629                                         const_arg0 ? const_arg0 : folded_arg0,
3630                                         const_arg1 ? const_arg1 : folded_arg1,
3631                                         const_arg2 ? const_arg2 : XEXP (x, 2));
3632       break;
3633
3634     default:
3635       break;
3636     }
3637
3638   return new_rtx ? new_rtx : x;
3639 }
3640 \f
3641 /* Return a constant value currently equivalent to X.
3642    Return 0 if we don't know one.  */
3643
3644 static rtx
3645 equiv_constant (rtx x)
3646 {
3647   if (REG_P (x)
3648       && REGNO_QTY_VALID_P (REGNO (x)))
3649     {
3650       int x_q = REG_QTY (REGNO (x));
3651       struct qty_table_elem *x_ent = &qty_table[x_q];
3652
3653       if (x_ent->const_rtx)
3654         x = gen_lowpart (GET_MODE (x), x_ent->const_rtx);
3655     }
3656
3657   if (x == 0 || CONSTANT_P (x))
3658     return x;
3659
3660   if (GET_CODE (x) == SUBREG)
3661     {
3662       rtx new_rtx;
3663
3664       /* See if we previously assigned a constant value to this SUBREG.  */
3665       if ((new_rtx = lookup_as_function (x, CONST_INT)) != 0
3666           || (new_rtx = lookup_as_function (x, CONST_DOUBLE)) != 0
3667           || (new_rtx = lookup_as_function (x, CONST_FIXED)) != 0)
3668         return new_rtx;
3669
3670       if (REG_P (SUBREG_REG (x))
3671           && (new_rtx = equiv_constant (SUBREG_REG (x))) != 0)
3672         return simplify_subreg (GET_MODE (x), SUBREG_REG (x),
3673                                 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
3674
3675       return 0;
3676     }
3677
3678   /* If X is a MEM, see if it is a constant-pool reference, or look it up in
3679      the hash table in case its value was seen before.  */
3680
3681   if (MEM_P (x))
3682     {
3683       struct table_elt *elt;
3684
3685       x = avoid_constant_pool_reference (x);
3686       if (CONSTANT_P (x))
3687         return x;
3688
3689       elt = lookup (x, SAFE_HASH (x, GET_MODE (x)), GET_MODE (x));
3690       if (elt == 0)
3691         return 0;
3692
3693       for (elt = elt->first_same_value; elt; elt = elt->next_same_value)
3694         if (elt->is_const && CONSTANT_P (elt->exp))
3695           return elt->exp;
3696     }
3697
3698   return 0;
3699 }
3700 \f
3701 /* Given INSN, a jump insn, TAKEN indicates if we are following the
3702    "taken" branch.
3703
3704    In certain cases, this can cause us to add an equivalence.  For example,
3705    if we are following the taken case of
3706         if (i == 2)
3707    we can add the fact that `i' and '2' are now equivalent.
3708
3709    In any case, we can record that this comparison was passed.  If the same
3710    comparison is seen later, we will know its value.  */
3711
3712 static void
3713 record_jump_equiv (rtx insn, bool taken)
3714 {
3715   int cond_known_true;
3716   rtx op0, op1;
3717   rtx set;
3718   enum machine_mode mode, mode0, mode1;
3719   int reversed_nonequality = 0;
3720   enum rtx_code code;
3721
3722   /* Ensure this is the right kind of insn.  */
3723   gcc_assert (any_condjump_p (insn));
3724
3725   set = pc_set (insn);
3726
3727   /* See if this jump condition is known true or false.  */
3728   if (taken)
3729     cond_known_true = (XEXP (SET_SRC (set), 2) == pc_rtx);
3730   else
3731     cond_known_true = (XEXP (SET_SRC (set), 1) == pc_rtx);
3732
3733   /* Get the type of comparison being done and the operands being compared.
3734      If we had to reverse a non-equality condition, record that fact so we
3735      know that it isn't valid for floating-point.  */
3736   code = GET_CODE (XEXP (SET_SRC (set), 0));
3737   op0 = fold_rtx (XEXP (XEXP (SET_SRC (set), 0), 0), insn);
3738   op1 = fold_rtx (XEXP (XEXP (SET_SRC (set), 0), 1), insn);
3739
3740   code = find_comparison_args (code, &op0, &op1, &mode0, &mode1);
3741   if (! cond_known_true)
3742     {
3743       code = reversed_comparison_code_parts (code, op0, op1, insn);
3744
3745       /* Don't remember if we can't find the inverse.  */
3746       if (code == UNKNOWN)
3747         return;
3748     }
3749
3750   /* The mode is the mode of the non-constant.  */
3751   mode = mode0;
3752   if (mode1 != VOIDmode)
3753     mode = mode1;
3754
3755   record_jump_cond (code, mode, op0, op1, reversed_nonequality);
3756 }
3757
3758 /* Yet another form of subreg creation.  In this case, we want something in
3759    MODE, and we should assume OP has MODE iff it is naturally modeless.  */
3760
3761 static rtx
3762 record_jump_cond_subreg (enum machine_mode mode, rtx op)
3763 {
3764   enum machine_mode op_mode = GET_MODE (op);
3765   if (op_mode == mode || op_mode == VOIDmode)
3766     return op;
3767   return lowpart_subreg (mode, op, op_mode);
3768 }
3769
3770 /* We know that comparison CODE applied to OP0 and OP1 in MODE is true.
3771    REVERSED_NONEQUALITY is nonzero if CODE had to be swapped.
3772    Make any useful entries we can with that information.  Called from
3773    above function and called recursively.  */
3774
3775 static void
3776 record_jump_cond (enum rtx_code code, enum machine_mode mode, rtx op0,
3777                   rtx op1, int reversed_nonequality)
3778 {
3779   unsigned op0_hash, op1_hash;
3780   int op0_in_memory, op1_in_memory;
3781   struct table_elt *op0_elt, *op1_elt;
3782
3783   /* If OP0 and OP1 are known equal, and either is a paradoxical SUBREG,
3784      we know that they are also equal in the smaller mode (this is also
3785      true for all smaller modes whether or not there is a SUBREG, but
3786      is not worth testing for with no SUBREG).  */
3787
3788   /* Note that GET_MODE (op0) may not equal MODE.  */
3789   if (code == EQ && GET_CODE (op0) == SUBREG
3790       && (GET_MODE_SIZE (GET_MODE (op0))
3791           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0)))))
3792     {
3793       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
3794       rtx tem = record_jump_cond_subreg (inner_mode, op1);
3795       if (tem)
3796         record_jump_cond (code, mode, SUBREG_REG (op0), tem,
3797                           reversed_nonequality);
3798     }
3799
3800   if (code == EQ && GET_CODE (op1) == SUBREG
3801       && (GET_MODE_SIZE (GET_MODE (op1))
3802           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op1)))))
3803     {
3804       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op1));
3805       rtx tem = record_jump_cond_subreg (inner_mode, op0);
3806       if (tem)
3807         record_jump_cond (code, mode, SUBREG_REG (op1), tem,
3808                           reversed_nonequality);
3809     }
3810
3811   /* Similarly, if this is an NE comparison, and either is a SUBREG
3812      making a smaller mode, we know the whole thing is also NE.  */
3813
3814   /* Note that GET_MODE (op0) may not equal MODE;
3815      if we test MODE instead, we can get an infinite recursion
3816      alternating between two modes each wider than MODE.  */
3817
3818   if (code == NE && GET_CODE (op0) == SUBREG
3819       && subreg_lowpart_p (op0)
3820       && (GET_MODE_SIZE (GET_MODE (op0))
3821           < GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0)))))
3822     {
3823       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
3824       rtx tem = record_jump_cond_subreg (inner_mode, op1);
3825       if (tem)
3826         record_jump_cond (code, mode, SUBREG_REG (op0), tem,
3827                           reversed_nonequality);
3828     }
3829
3830   if (code == NE && GET_CODE (op1) == SUBREG
3831       && subreg_lowpart_p (op1)
3832       && (GET_MODE_SIZE (GET_MODE (op1))
3833           < GET_MODE_SIZE (GET_MODE (SUBREG_REG (op1)))))
3834     {
3835       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op1));
3836       rtx tem = record_jump_cond_subreg (inner_mode, op0);
3837       if (tem)
3838         record_jump_cond (code, mode, SUBREG_REG (op1), tem,
3839                           reversed_nonequality);
3840     }
3841
3842   /* Hash both operands.  */
3843
3844   do_not_record = 0;
3845   hash_arg_in_memory = 0;
3846   op0_hash = HASH (op0, mode);
3847   op0_in_memory = hash_arg_in_memory;
3848
3849   if (do_not_record)
3850     return;
3851
3852   do_not_record = 0;
3853   hash_arg_in_memory = 0;
3854   op1_hash = HASH (op1, mode);
3855   op1_in_memory = hash_arg_in_memory;
3856
3857   if (do_not_record)
3858     return;
3859
3860   /* Look up both operands.  */
3861   op0_elt = lookup (op0, op0_hash, mode);
3862   op1_elt = lookup (op1, op1_hash, mode);
3863
3864   /* If both operands are already equivalent or if they are not in the
3865      table but are identical, do nothing.  */
3866   if ((op0_elt != 0 && op1_elt != 0
3867        && op0_elt->first_same_value == op1_elt->first_same_value)
3868       || op0 == op1 || rtx_equal_p (op0, op1))
3869     return;
3870
3871   /* If we aren't setting two things equal all we can do is save this
3872      comparison.   Similarly if this is floating-point.  In the latter
3873      case, OP1 might be zero and both -0.0 and 0.0 are equal to it.
3874      If we record the equality, we might inadvertently delete code
3875      whose intent was to change -0 to +0.  */
3876
3877   if (code != EQ || FLOAT_MODE_P (GET_MODE (op0)))
3878     {
3879       struct qty_table_elem *ent;
3880       int qty;
3881
3882       /* If we reversed a floating-point comparison, if OP0 is not a
3883          register, or if OP1 is neither a register or constant, we can't
3884          do anything.  */
3885
3886       if (!REG_P (op1))
3887         op1 = equiv_constant (op1);
3888
3889       if ((reversed_nonequality && FLOAT_MODE_P (mode))
3890           || !REG_P (op0) || op1 == 0)
3891         return;
3892
3893       /* Put OP0 in the hash table if it isn't already.  This gives it a
3894          new quantity number.  */
3895       if (op0_elt == 0)
3896         {
3897           if (insert_regs (op0, NULL, 0))
3898             {
3899               rehash_using_reg (op0);
3900               op0_hash = HASH (op0, mode);
3901
3902               /* If OP0 is contained in OP1, this changes its hash code
3903                  as well.  Faster to rehash than to check, except
3904                  for the simple case of a constant.  */
3905               if (! CONSTANT_P (op1))
3906                 op1_hash = HASH (op1,mode);
3907             }
3908
3909           op0_elt = insert (op0, NULL, op0_hash, mode);
3910           op0_elt->in_memory = op0_in_memory;
3911         }
3912
3913       qty = REG_QTY (REGNO (op0));
3914       ent = &qty_table[qty];
3915
3916       ent->comparison_code = code;
3917       if (REG_P (op1))
3918         {
3919           /* Look it up again--in case op0 and op1 are the same.  */
3920           op1_elt = lookup (op1, op1_hash, mode);
3921
3922           /* Put OP1 in the hash table so it gets a new quantity number.  */
3923           if (op1_elt == 0)
3924             {
3925               if (insert_regs (op1, NULL, 0))
3926                 {
3927                   rehash_using_reg (op1);
3928                   op1_hash = HASH (op1, mode);
3929                 }
3930
3931               op1_elt = insert (op1, NULL, op1_hash, mode);
3932               op1_elt->in_memory = op1_in_memory;
3933             }
3934
3935           ent->comparison_const = NULL_RTX;
3936           ent->comparison_qty = REG_QTY (REGNO (op1));
3937         }
3938       else
3939         {
3940           ent->comparison_const = op1;
3941           ent->comparison_qty = -1;
3942         }
3943
3944       return;
3945     }
3946
3947   /* If either side is still missing an equivalence, make it now,
3948      then merge the equivalences.  */
3949
3950   if (op0_elt == 0)
3951     {
3952       if (insert_regs (op0, NULL, 0))
3953         {
3954           rehash_using_reg (op0);
3955           op0_hash = HASH (op0, mode);
3956         }
3957
3958       op0_elt = insert (op0, NULL, op0_hash, mode);
3959       op0_elt->in_memory = op0_in_memory;
3960     }
3961
3962   if (op1_elt == 0)
3963     {
3964       if (insert_regs (op1, NULL, 0))
3965         {
3966           rehash_using_reg (op1);
3967           op1_hash = HASH (op1, mode);
3968         }
3969
3970       op1_elt = insert (op1, NULL, op1_hash, mode);
3971       op1_elt->in_memory = op1_in_memory;
3972     }
3973
3974   merge_equiv_classes (op0_elt, op1_elt);
3975 }
3976 \f
3977 /* CSE processing for one instruction.
3978    First simplify sources and addresses of all assignments
3979    in the instruction, using previously-computed equivalents values.
3980    Then install the new sources and destinations in the table
3981    of available values.  */
3982
3983 /* Data on one SET contained in the instruction.  */
3984
3985 struct set
3986 {
3987   /* The SET rtx itself.  */
3988   rtx rtl;
3989   /* The SET_SRC of the rtx (the original value, if it is changing).  */
3990   rtx src;
3991   /* The hash-table element for the SET_SRC of the SET.  */
3992   struct table_elt *src_elt;
3993   /* Hash value for the SET_SRC.  */
3994   unsigned src_hash;
3995   /* Hash value for the SET_DEST.  */
3996   unsigned dest_hash;
3997   /* The SET_DEST, with SUBREG, etc., stripped.  */
3998   rtx inner_dest;
3999   /* Nonzero if the SET_SRC is in memory.  */
4000   char src_in_memory;
4001   /* Nonzero if the SET_SRC contains something
4002      whose value cannot be predicted and understood.  */
4003   char src_volatile;
4004   /* Original machine mode, in case it becomes a CONST_INT.
4005      The size of this field should match the size of the mode
4006      field of struct rtx_def (see rtl.h).  */
4007   ENUM_BITFIELD(machine_mode) mode : 8;
4008   /* A constant equivalent for SET_SRC, if any.  */
4009   rtx src_const;
4010   /* Hash value of constant equivalent for SET_SRC.  */
4011   unsigned src_const_hash;
4012   /* Table entry for constant equivalent for SET_SRC, if any.  */
4013   struct table_elt *src_const_elt;
4014   /* Table entry for the destination address.  */
4015   struct table_elt *dest_addr_elt;
4016 };
4017
4018 static void
4019 cse_insn (rtx insn)
4020 {
4021   rtx x = PATTERN (insn);
4022   int i;
4023   rtx tem;
4024   int n_sets = 0;
4025
4026   rtx src_eqv = 0;
4027   struct table_elt *src_eqv_elt = 0;
4028   int src_eqv_volatile = 0;
4029   int src_eqv_in_memory = 0;
4030   unsigned src_eqv_hash = 0;
4031
4032   struct set *sets = (struct set *) 0;
4033
4034   this_insn = insn;
4035 #ifdef HAVE_cc0
4036   /* Records what this insn does to set CC0.  */
4037   this_insn_cc0 = 0;
4038   this_insn_cc0_mode = VOIDmode;
4039 #endif
4040
4041   /* Find all the SETs and CLOBBERs in this instruction.
4042      Record all the SETs in the array `set' and count them.
4043      Also determine whether there is a CLOBBER that invalidates
4044      all memory references, or all references at varying addresses.  */
4045
4046   if (CALL_P (insn))
4047     {
4048       for (tem = CALL_INSN_FUNCTION_USAGE (insn); tem; tem = XEXP (tem, 1))
4049         {
4050           if (GET_CODE (XEXP (tem, 0)) == CLOBBER)
4051             invalidate (SET_DEST (XEXP (tem, 0)), VOIDmode);
4052           XEXP (tem, 0) = canon_reg (XEXP (tem, 0), insn);
4053         }
4054     }
4055
4056   if (GET_CODE (x) == SET)
4057     {
4058       sets = XALLOCA (struct set);
4059       sets[0].rtl = x;
4060
4061       /* Ignore SETs that are unconditional jumps.
4062          They never need cse processing, so this does not hurt.
4063          The reason is not efficiency but rather
4064          so that we can test at the end for instructions
4065          that have been simplified to unconditional jumps
4066          and not be misled by unchanged instructions
4067          that were unconditional jumps to begin with.  */
4068       if (SET_DEST (x) == pc_rtx
4069           && GET_CODE (SET_SRC (x)) == LABEL_REF)
4070         ;
4071
4072       /* Don't count call-insns, (set (reg 0) (call ...)), as a set.
4073          The hard function value register is used only once, to copy to
4074          someplace else, so it isn't worth cse'ing (and on 80386 is unsafe)!
4075          Ensure we invalidate the destination register.  On the 80386 no
4076          other code would invalidate it since it is a fixed_reg.
4077          We need not check the return of apply_change_group; see canon_reg.  */
4078
4079       else if (GET_CODE (SET_SRC (x)) == CALL)
4080         {
4081           canon_reg (SET_SRC (x), insn);
4082           apply_change_group ();
4083           fold_rtx (SET_SRC (x), insn);
4084           invalidate (SET_DEST (x), VOIDmode);
4085         }
4086       else
4087         n_sets = 1;
4088     }
4089   else if (GET_CODE (x) == PARALLEL)
4090     {
4091       int lim = XVECLEN (x, 0);
4092
4093       sets = XALLOCAVEC (struct set, lim);
4094
4095       /* Find all regs explicitly clobbered in this insn,
4096          and ensure they are not replaced with any other regs
4097          elsewhere in this insn.
4098          When a reg that is clobbered is also used for input,
4099          we should presume that that is for a reason,
4100          and we should not substitute some other register
4101          which is not supposed to be clobbered.
4102          Therefore, this loop cannot be merged into the one below
4103          because a CALL may precede a CLOBBER and refer to the
4104          value clobbered.  We must not let a canonicalization do
4105          anything in that case.  */
4106       for (i = 0; i < lim; i++)
4107         {
4108           rtx y = XVECEXP (x, 0, i);
4109           if (GET_CODE (y) == CLOBBER)
4110             {
4111               rtx clobbered = XEXP (y, 0);
4112
4113               if (REG_P (clobbered)
4114                   || GET_CODE (clobbered) == SUBREG)
4115                 invalidate (clobbered, VOIDmode);
4116               else if (GET_CODE (clobbered) == STRICT_LOW_PART
4117                        || GET_CODE (clobbered) == ZERO_EXTRACT)
4118                 invalidate (XEXP (clobbered, 0), GET_MODE (clobbered));
4119             }
4120         }
4121
4122       for (i = 0; i < lim; i++)
4123         {
4124           rtx y = XVECEXP (x, 0, i);
4125           if (GET_CODE (y) == SET)
4126             {
4127               /* As above, we ignore unconditional jumps and call-insns and
4128                  ignore the result of apply_change_group.  */
4129               if (GET_CODE (SET_SRC (y)) == CALL)
4130                 {
4131                   canon_reg (SET_SRC (y), insn);
4132                   apply_change_group ();
4133                   fold_rtx (SET_SRC (y), insn);
4134                   invalidate (SET_DEST (y), VOIDmode);
4135                 }
4136               else if (SET_DEST (y) == pc_rtx
4137                        && GET_CODE (SET_SRC (y)) == LABEL_REF)
4138                 ;
4139               else
4140                 sets[n_sets++].rtl = y;
4141             }
4142           else if (GET_CODE (y) == CLOBBER)
4143             {
4144               /* If we clobber memory, canon the address.
4145                  This does nothing when a register is clobbered
4146                  because we have already invalidated the reg.  */
4147               if (MEM_P (XEXP (y, 0)))
4148                 canon_reg (XEXP (y, 0), insn);
4149             }
4150           else if (GET_CODE (y) == USE
4151                    && ! (REG_P (XEXP (y, 0))
4152                          && REGNO (XEXP (y, 0)) < FIRST_PSEUDO_REGISTER))
4153             canon_reg (y, insn);
4154           else if (GET_CODE (y) == CALL)
4155             {
4156               /* The result of apply_change_group can be ignored; see
4157                  canon_reg.  */
4158               canon_reg (y, insn);
4159               apply_change_group ();
4160               fold_rtx (y, insn);
4161             }
4162         }
4163     }
4164   else if (GET_CODE (x) == CLOBBER)
4165     {
4166       if (MEM_P (XEXP (x, 0)))
4167         canon_reg (XEXP (x, 0), insn);
4168     }
4169
4170   /* Canonicalize a USE of a pseudo register or memory location.  */
4171   else if (GET_CODE (x) == USE
4172            && ! (REG_P (XEXP (x, 0))
4173                  && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER))
4174     canon_reg (XEXP (x, 0), insn);
4175   else if (GET_CODE (x) == CALL)
4176     {
4177       /* The result of apply_change_group can be ignored; see canon_reg.  */
4178       canon_reg (x, insn);
4179       apply_change_group ();
4180       fold_rtx (x, insn);
4181     }
4182
4183   /* Store the equivalent value in SRC_EQV, if different, or if the DEST
4184      is a STRICT_LOW_PART.  The latter condition is necessary because SRC_EQV
4185      is handled specially for this case, and if it isn't set, then there will
4186      be no equivalence for the destination.  */
4187   if (n_sets == 1 && REG_NOTES (insn) != 0
4188       && (tem = find_reg_note (insn, REG_EQUAL, NULL_RTX)) != 0
4189       && (! rtx_equal_p (XEXP (tem, 0), SET_SRC (sets[0].rtl))
4190           || GET_CODE (SET_DEST (sets[0].rtl)) == STRICT_LOW_PART))
4191     {
4192       /* The result of apply_change_group can be ignored; see canon_reg.  */
4193       canon_reg (XEXP (tem, 0), insn);
4194       apply_change_group ();
4195       src_eqv = fold_rtx (XEXP (tem, 0), insn);
4196       XEXP (tem, 0) = copy_rtx (src_eqv);
4197       df_notes_rescan (insn);
4198     }
4199
4200   /* Canonicalize sources and addresses of destinations.
4201      We do this in a separate pass to avoid problems when a MATCH_DUP is
4202      present in the insn pattern.  In that case, we want to ensure that
4203      we don't break the duplicate nature of the pattern.  So we will replace
4204      both operands at the same time.  Otherwise, we would fail to find an
4205      equivalent substitution in the loop calling validate_change below.
4206
4207      We used to suppress canonicalization of DEST if it appears in SRC,
4208      but we don't do this any more.  */
4209
4210   for (i = 0; i < n_sets; i++)
4211     {
4212       rtx dest = SET_DEST (sets[i].rtl);
4213       rtx src = SET_SRC (sets[i].rtl);
4214       rtx new_rtx = canon_reg (src, insn);
4215
4216       validate_change (insn, &SET_SRC (sets[i].rtl), new_rtx, 1);
4217
4218       if (GET_CODE (dest) == ZERO_EXTRACT)
4219         {
4220           validate_change (insn, &XEXP (dest, 1),
4221                            canon_reg (XEXP (dest, 1), insn), 1);
4222           validate_change (insn, &XEXP (dest, 2),
4223                            canon_reg (XEXP (dest, 2), insn), 1);
4224         }
4225
4226       while (GET_CODE (dest) == SUBREG
4227              || GET_CODE (dest) == ZERO_EXTRACT
4228              || GET_CODE (dest) == STRICT_LOW_PART)
4229         dest = XEXP (dest, 0);
4230
4231       if (MEM_P (dest))
4232         canon_reg (dest, insn);
4233     }
4234
4235   /* Now that we have done all the replacements, we can apply the change
4236      group and see if they all work.  Note that this will cause some
4237      canonicalizations that would have worked individually not to be applied
4238      because some other canonicalization didn't work, but this should not
4239      occur often.
4240
4241      The result of apply_change_group can be ignored; see canon_reg.  */
4242
4243   apply_change_group ();
4244
4245   /* Set sets[i].src_elt to the class each source belongs to.
4246      Detect assignments from or to volatile things
4247      and set set[i] to zero so they will be ignored
4248      in the rest of this function.
4249
4250      Nothing in this loop changes the hash table or the register chains.  */
4251
4252   for (i = 0; i < n_sets; i++)
4253     {
4254       rtx src, dest;
4255       rtx src_folded;
4256       struct table_elt *elt = 0, *p;
4257       enum machine_mode mode;
4258       rtx src_eqv_here;
4259       rtx src_const = 0;
4260       rtx src_related = 0;
4261       struct table_elt *src_const_elt = 0;
4262       int src_cost = MAX_COST;
4263       int src_eqv_cost = MAX_COST;
4264       int src_folded_cost = MAX_COST;
4265       int src_related_cost = MAX_COST;
4266       int src_elt_cost = MAX_COST;
4267       int src_regcost = MAX_COST;
4268       int src_eqv_regcost = MAX_COST;
4269       int src_folded_regcost = MAX_COST;
4270       int src_related_regcost = MAX_COST;
4271       int src_elt_regcost = MAX_COST;
4272       /* Set nonzero if we need to call force_const_mem on with the
4273          contents of src_folded before using it.  */
4274       int src_folded_force_flag = 0;
4275
4276       dest = SET_DEST (sets[i].rtl);
4277       src = SET_SRC (sets[i].rtl);
4278
4279       /* If SRC is a constant that has no machine mode,
4280          hash it with the destination's machine mode.
4281          This way we can keep different modes separate.  */
4282
4283       mode = GET_MODE (src) == VOIDmode ? GET_MODE (dest) : GET_MODE (src);
4284       sets[i].mode = mode;
4285
4286       if (src_eqv)
4287         {
4288           enum machine_mode eqvmode = mode;
4289           if (GET_CODE (dest) == STRICT_LOW_PART)
4290             eqvmode = GET_MODE (SUBREG_REG (XEXP (dest, 0)));
4291           do_not_record = 0;
4292           hash_arg_in_memory = 0;
4293           src_eqv_hash = HASH (src_eqv, eqvmode);
4294
4295           /* Find the equivalence class for the equivalent expression.  */
4296
4297           if (!do_not_record)
4298             src_eqv_elt = lookup (src_eqv, src_eqv_hash, eqvmode);
4299
4300           src_eqv_volatile = do_not_record;
4301           src_eqv_in_memory = hash_arg_in_memory;
4302         }
4303
4304       /* If this is a STRICT_LOW_PART assignment, src_eqv corresponds to the
4305          value of the INNER register, not the destination.  So it is not
4306          a valid substitution for the source.  But save it for later.  */
4307       if (GET_CODE (dest) == STRICT_LOW_PART)
4308         src_eqv_here = 0;
4309       else
4310         src_eqv_here = src_eqv;
4311
4312       /* Simplify and foldable subexpressions in SRC.  Then get the fully-
4313          simplified result, which may not necessarily be valid.  */
4314       src_folded = fold_rtx (src, insn);
4315
4316 #if 0
4317       /* ??? This caused bad code to be generated for the m68k port with -O2.
4318          Suppose src is (CONST_INT -1), and that after truncation src_folded
4319          is (CONST_INT 3).  Suppose src_folded is then used for src_const.
4320          At the end we will add src and src_const to the same equivalence
4321          class.  We now have 3 and -1 on the same equivalence class.  This
4322          causes later instructions to be mis-optimized.  */
4323       /* If storing a constant in a bitfield, pre-truncate the constant
4324          so we will be able to record it later.  */
4325       if (GET_CODE (SET_DEST (sets[i].rtl)) == ZERO_EXTRACT)
4326         {
4327           rtx width = XEXP (SET_DEST (sets[i].rtl), 1);
4328
4329           if (GET_CODE (src) == CONST_INT
4330               && GET_CODE (width) == CONST_INT
4331               && INTVAL (width) < HOST_BITS_PER_WIDE_INT
4332               && (INTVAL (src) & ((HOST_WIDE_INT) (-1) << INTVAL (width))))
4333             src_folded
4334               = GEN_INT (INTVAL (src) & (((HOST_WIDE_INT) 1
4335                                           << INTVAL (width)) - 1));
4336         }
4337 #endif
4338
4339       /* Compute SRC's hash code, and also notice if it
4340          should not be recorded at all.  In that case,
4341          prevent any further processing of this assignment.  */
4342       do_not_record = 0;
4343       hash_arg_in_memory = 0;
4344
4345       sets[i].src = src;
4346       sets[i].src_hash = HASH (src, mode);
4347       sets[i].src_volatile = do_not_record;
4348       sets[i].src_in_memory = hash_arg_in_memory;
4349
4350       /* If SRC is a MEM, there is a REG_EQUIV note for SRC, and DEST is
4351          a pseudo, do not record SRC.  Using SRC as a replacement for
4352          anything else will be incorrect in that situation.  Note that
4353          this usually occurs only for stack slots, in which case all the
4354          RTL would be referring to SRC, so we don't lose any optimization
4355          opportunities by not having SRC in the hash table.  */
4356
4357       if (MEM_P (src)
4358           && find_reg_note (insn, REG_EQUIV, NULL_RTX) != 0
4359           && REG_P (dest)
4360           && REGNO (dest) >= FIRST_PSEUDO_REGISTER)
4361         sets[i].src_volatile = 1;
4362
4363 #if 0
4364       /* It is no longer clear why we used to do this, but it doesn't
4365          appear to still be needed.  So let's try without it since this
4366          code hurts cse'ing widened ops.  */
4367       /* If source is a paradoxical subreg (such as QI treated as an SI),
4368          treat it as volatile.  It may do the work of an SI in one context
4369          where the extra bits are not being used, but cannot replace an SI
4370          in general.  */
4371       if (GET_CODE (src) == SUBREG
4372           && (GET_MODE_SIZE (GET_MODE (src))
4373               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))))
4374         sets[i].src_volatile = 1;
4375 #endif
4376
4377       /* Locate all possible equivalent forms for SRC.  Try to replace
4378          SRC in the insn with each cheaper equivalent.
4379
4380          We have the following types of equivalents: SRC itself, a folded
4381          version, a value given in a REG_EQUAL note, or a value related
4382          to a constant.
4383
4384          Each of these equivalents may be part of an additional class
4385          of equivalents (if more than one is in the table, they must be in
4386          the same class; we check for this).
4387
4388          If the source is volatile, we don't do any table lookups.
4389
4390          We note any constant equivalent for possible later use in a
4391          REG_NOTE.  */
4392
4393       if (!sets[i].src_volatile)
4394         elt = lookup (src, sets[i].src_hash, mode);
4395
4396       sets[i].src_elt = elt;
4397
4398       if (elt && src_eqv_here && src_eqv_elt)
4399         {
4400           if (elt->first_same_value != src_eqv_elt->first_same_value)
4401             {
4402               /* The REG_EQUAL is indicating that two formerly distinct
4403                  classes are now equivalent.  So merge them.  */
4404               merge_equiv_classes (elt, src_eqv_elt);
4405               src_eqv_hash = HASH (src_eqv, elt->mode);
4406               src_eqv_elt = lookup (src_eqv, src_eqv_hash, elt->mode);
4407             }
4408
4409           src_eqv_here = 0;
4410         }
4411
4412       else if (src_eqv_elt)
4413         elt = src_eqv_elt;
4414
4415       /* Try to find a constant somewhere and record it in `src_const'.
4416          Record its table element, if any, in `src_const_elt'.  Look in
4417          any known equivalences first.  (If the constant is not in the
4418          table, also set `sets[i].src_const_hash').  */
4419       if (elt)
4420         for (p = elt->first_same_value; p; p = p->next_same_value)
4421           if (p->is_const)
4422             {
4423               src_const = p->exp;
4424               src_const_elt = elt;
4425               break;
4426             }
4427
4428       if (src_const == 0
4429           && (CONSTANT_P (src_folded)
4430               /* Consider (minus (label_ref L1) (label_ref L2)) as
4431                  "constant" here so we will record it. This allows us
4432                  to fold switch statements when an ADDR_DIFF_VEC is used.  */
4433               || (GET_CODE (src_folded) == MINUS
4434                   && GET_CODE (XEXP (src_folded, 0)) == LABEL_REF
4435                   && GET_CODE (XEXP (src_folded, 1)) == LABEL_REF)))
4436         src_const = src_folded, src_const_elt = elt;
4437       else if (src_const == 0 && src_eqv_here && CONSTANT_P (src_eqv_here))
4438         src_const = src_eqv_here, src_const_elt = src_eqv_elt;
4439
4440       /* If we don't know if the constant is in the table, get its
4441          hash code and look it up.  */
4442       if (src_const && src_const_elt == 0)
4443         {
4444           sets[i].src_const_hash = HASH (src_const, mode);
4445           src_const_elt = lookup (src_const, sets[i].src_const_hash, mode);
4446         }
4447
4448       sets[i].src_const = src_const;
4449       sets[i].src_const_elt = src_const_elt;
4450
4451       /* If the constant and our source are both in the table, mark them as
4452          equivalent.  Otherwise, if a constant is in the table but the source
4453          isn't, set ELT to it.  */
4454       if (src_const_elt && elt
4455           && src_const_elt->first_same_value != elt->first_same_value)
4456         merge_equiv_classes (elt, src_const_elt);
4457       else if (src_const_elt && elt == 0)
4458         elt = src_const_elt;
4459
4460       /* See if there is a register linearly related to a constant
4461          equivalent of SRC.  */
4462       if (src_const
4463           && (GET_CODE (src_const) == CONST
4464               || (src_const_elt && src_const_elt->related_value != 0)))
4465         {
4466           src_related = use_related_value (src_const, src_const_elt);
4467           if (src_related)
4468             {
4469               struct table_elt *src_related_elt
4470                 = lookup (src_related, HASH (src_related, mode), mode);
4471               if (src_related_elt && elt)
4472                 {
4473                   if (elt->first_same_value
4474                       != src_related_elt->first_same_value)
4475                     /* This can occur when we previously saw a CONST
4476                        involving a SYMBOL_REF and then see the SYMBOL_REF
4477                        twice.  Merge the involved classes.  */
4478                     merge_equiv_classes (elt, src_related_elt);
4479
4480                   src_related = 0;
4481                   src_related_elt = 0;
4482                 }
4483               else if (src_related_elt && elt == 0)
4484                 elt = src_related_elt;
4485             }
4486         }
4487
4488       /* See if we have a CONST_INT that is already in a register in a
4489          wider mode.  */
4490
4491       if (src_const && src_related == 0 && GET_CODE (src_const) == CONST_INT
4492           && GET_MODE_CLASS (mode) == MODE_INT
4493           && GET_MODE_BITSIZE (mode) < BITS_PER_WORD)
4494         {
4495           enum machine_mode wider_mode;
4496
4497           for (wider_mode = GET_MODE_WIDER_MODE (mode);
4498                GET_MODE_BITSIZE (wider_mode) <= BITS_PER_WORD
4499                && src_related == 0;
4500                wider_mode = GET_MODE_WIDER_MODE (wider_mode))
4501             {
4502               struct table_elt *const_elt
4503                 = lookup (src_const, HASH (src_const, wider_mode), wider_mode);
4504
4505               if (const_elt == 0)
4506                 continue;
4507
4508               for (const_elt = const_elt->first_same_value;
4509                    const_elt; const_elt = const_elt->next_same_value)
4510                 if (REG_P (const_elt->exp))
4511                   {
4512                     src_related = gen_lowpart (mode, const_elt->exp);
4513                     break;
4514                   }
4515             }
4516         }
4517
4518       /* Another possibility is that we have an AND with a constant in
4519          a mode narrower than a word.  If so, it might have been generated
4520          as part of an "if" which would narrow the AND.  If we already
4521          have done the AND in a wider mode, we can use a SUBREG of that
4522          value.  */
4523
4524       if (flag_expensive_optimizations && ! src_related
4525           && GET_CODE (src) == AND && GET_CODE (XEXP (src, 1)) == CONST_INT
4526           && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
4527         {
4528           enum machine_mode tmode;
4529           rtx new_and = gen_rtx_AND (VOIDmode, NULL_RTX, XEXP (src, 1));
4530
4531           for (tmode = GET_MODE_WIDER_MODE (mode);
4532                GET_MODE_SIZE (tmode) <= UNITS_PER_WORD;
4533                tmode = GET_MODE_WIDER_MODE (tmode))
4534             {
4535               rtx inner = gen_lowpart (tmode, XEXP (src, 0));
4536               struct table_elt *larger_elt;
4537
4538               if (inner)
4539                 {
4540                   PUT_MODE (new_and, tmode);
4541                   XEXP (new_and, 0) = inner;
4542                   larger_elt = lookup (new_and, HASH (new_and, tmode), tmode);
4543                   if (larger_elt == 0)
4544                     continue;
4545
4546                   for (larger_elt = larger_elt->first_same_value;
4547                        larger_elt; larger_elt = larger_elt->next_same_value)
4548                     if (REG_P (larger_elt->exp))
4549                       {
4550                         src_related
4551                           = gen_lowpart (mode, larger_elt->exp);
4552                         break;
4553                       }
4554
4555                   if (src_related)
4556                     break;
4557                 }
4558             }
4559         }
4560
4561 #ifdef LOAD_EXTEND_OP
4562       /* See if a MEM has already been loaded with a widening operation;
4563          if it has, we can use a subreg of that.  Many CISC machines
4564          also have such operations, but this is only likely to be
4565          beneficial on these machines.  */
4566
4567       if (flag_expensive_optimizations && src_related == 0
4568           && (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
4569           && GET_MODE_CLASS (mode) == MODE_INT
4570           && MEM_P (src) && ! do_not_record
4571           && LOAD_EXTEND_OP (mode) != UNKNOWN)
4572         {
4573           struct rtx_def memory_extend_buf;
4574           rtx memory_extend_rtx = &memory_extend_buf;
4575           enum machine_mode tmode;
4576
4577           /* Set what we are trying to extend and the operation it might
4578              have been extended with.  */
4579           memset (memory_extend_rtx, 0, sizeof(*memory_extend_rtx));
4580           PUT_CODE (memory_extend_rtx, LOAD_EXTEND_OP (mode));
4581           XEXP (memory_extend_rtx, 0) = src;
4582
4583           for (tmode = GET_MODE_WIDER_MODE (mode);
4584                GET_MODE_SIZE (tmode) <= UNITS_PER_WORD;
4585                tmode = GET_MODE_WIDER_MODE (tmode))
4586             {
4587               struct table_elt *larger_elt;
4588
4589               PUT_MODE (memory_extend_rtx, tmode);
4590               larger_elt = lookup (memory_extend_rtx,
4591                                    HASH (memory_extend_rtx, tmode), tmode);
4592               if (larger_elt == 0)
4593                 continue;
4594
4595               for (larger_elt = larger_elt->first_same_value;
4596                    larger_elt; larger_elt = larger_elt->next_same_value)
4597                 if (REG_P (larger_elt->exp))
4598                   {
4599                     src_related = gen_lowpart (mode, larger_elt->exp);
4600                     break;
4601                   }
4602
4603               if (src_related)
4604                 break;
4605             }
4606         }
4607 #endif /* LOAD_EXTEND_OP */
4608
4609       if (src == src_folded)
4610         src_folded = 0;
4611
4612       /* At this point, ELT, if nonzero, points to a class of expressions
4613          equivalent to the source of this SET and SRC, SRC_EQV, SRC_FOLDED,
4614          and SRC_RELATED, if nonzero, each contain additional equivalent
4615          expressions.  Prune these latter expressions by deleting expressions
4616          already in the equivalence class.
4617
4618          Check for an equivalent identical to the destination.  If found,
4619          this is the preferred equivalent since it will likely lead to
4620          elimination of the insn.  Indicate this by placing it in
4621          `src_related'.  */
4622
4623       if (elt)
4624         elt = elt->first_same_value;
4625       for (p = elt; p; p = p->next_same_value)
4626         {
4627           enum rtx_code code = GET_CODE (p->exp);
4628
4629           /* If the expression is not valid, ignore it.  Then we do not
4630              have to check for validity below.  In most cases, we can use
4631              `rtx_equal_p', since canonicalization has already been done.  */
4632           if (code != REG && ! exp_equiv_p (p->exp, p->exp, 1, false))
4633             continue;
4634
4635           /* Also skip paradoxical subregs, unless that's what we're
4636              looking for.  */
4637           if (code == SUBREG
4638               && (GET_MODE_SIZE (GET_MODE (p->exp))
4639                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (p->exp))))
4640               && ! (src != 0
4641                     && GET_CODE (src) == SUBREG
4642                     && GET_MODE (src) == GET_MODE (p->exp)
4643                     && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
4644                         < GET_MODE_SIZE (GET_MODE (SUBREG_REG (p->exp))))))
4645             continue;
4646
4647           if (src && GET_CODE (src) == code && rtx_equal_p (src, p->exp))
4648             src = 0;
4649           else if (src_folded && GET_CODE (src_folded) == code
4650                    && rtx_equal_p (src_folded, p->exp))
4651             src_folded = 0;
4652           else if (src_eqv_here && GET_CODE (src_eqv_here) == code
4653                    && rtx_equal_p (src_eqv_here, p->exp))
4654             src_eqv_here = 0;
4655           else if (src_related && GET_CODE (src_related) == code
4656                    && rtx_equal_p (src_related, p->exp))
4657             src_related = 0;
4658
4659           /* This is the same as the destination of the insns, we want
4660              to prefer it.  Copy it to src_related.  The code below will
4661              then give it a negative cost.  */
4662           if (GET_CODE (dest) == code && rtx_equal_p (p->exp, dest))
4663             src_related = dest;
4664         }
4665
4666       /* Find the cheapest valid equivalent, trying all the available
4667          possibilities.  Prefer items not in the hash table to ones
4668          that are when they are equal cost.  Note that we can never
4669          worsen an insn as the current contents will also succeed.
4670          If we find an equivalent identical to the destination, use it as best,
4671          since this insn will probably be eliminated in that case.  */
4672       if (src)
4673         {
4674           if (rtx_equal_p (src, dest))
4675             src_cost = src_regcost = -1;
4676           else
4677             {
4678               src_cost = COST (src);
4679               src_regcost = approx_reg_cost (src);
4680             }
4681         }
4682
4683       if (src_eqv_here)
4684         {
4685           if (rtx_equal_p (src_eqv_here, dest))
4686             src_eqv_cost = src_eqv_regcost = -1;
4687           else
4688             {
4689               src_eqv_cost = COST (src_eqv_here);
4690               src_eqv_regcost = approx_reg_cost (src_eqv_here);
4691             }
4692         }
4693
4694       if (src_folded)
4695         {
4696           if (rtx_equal_p (src_folded, dest))
4697             src_folded_cost = src_folded_regcost = -1;
4698           else
4699             {
4700               src_folded_cost = COST (src_folded);
4701               src_folded_regcost = approx_reg_cost (src_folded);
4702             }
4703         }
4704
4705       if (src_related)
4706         {
4707           if (rtx_equal_p (src_related, dest))
4708             src_related_cost = src_related_regcost = -1;
4709           else
4710             {
4711               src_related_cost = COST (src_related);
4712               src_related_regcost = approx_reg_cost (src_related);
4713             }
4714         }
4715
4716       /* If this was an indirect jump insn, a known label will really be
4717          cheaper even though it looks more expensive.  */
4718       if (dest == pc_rtx && src_const && GET_CODE (src_const) == LABEL_REF)
4719         src_folded = src_const, src_folded_cost = src_folded_regcost = -1;
4720
4721       /* Terminate loop when replacement made.  This must terminate since
4722          the current contents will be tested and will always be valid.  */
4723       while (1)
4724         {
4725           rtx trial;
4726
4727           /* Skip invalid entries.  */
4728           while (elt && !REG_P (elt->exp)
4729                  && ! exp_equiv_p (elt->exp, elt->exp, 1, false))
4730             elt = elt->next_same_value;
4731
4732           /* A paradoxical subreg would be bad here: it'll be the right
4733              size, but later may be adjusted so that the upper bits aren't
4734              what we want.  So reject it.  */
4735           if (elt != 0
4736               && GET_CODE (elt->exp) == SUBREG
4737               && (GET_MODE_SIZE (GET_MODE (elt->exp))
4738                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (elt->exp))))
4739               /* It is okay, though, if the rtx we're trying to match
4740                  will ignore any of the bits we can't predict.  */
4741               && ! (src != 0
4742                     && GET_CODE (src) == SUBREG
4743                     && GET_MODE (src) == GET_MODE (elt->exp)
4744                     && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
4745                         < GET_MODE_SIZE (GET_MODE (SUBREG_REG (elt->exp))))))
4746             {
4747               elt = elt->next_same_value;
4748               continue;
4749             }
4750
4751           if (elt)
4752             {
4753               src_elt_cost = elt->cost;
4754               src_elt_regcost = elt->regcost;
4755             }
4756
4757           /* Find cheapest and skip it for the next time.   For items
4758              of equal cost, use this order:
4759              src_folded, src, src_eqv, src_related and hash table entry.  */
4760           if (src_folded
4761               && preferable (src_folded_cost, src_folded_regcost,
4762                              src_cost, src_regcost) <= 0
4763               && preferable (src_folded_cost, src_folded_regcost,
4764                              src_eqv_cost, src_eqv_regcost) <= 0
4765               && preferable (src_folded_cost, src_folded_regcost,
4766                              src_related_cost, src_related_regcost) <= 0
4767               && preferable (src_folded_cost, src_folded_regcost,
4768                              src_elt_cost, src_elt_regcost) <= 0)
4769             {
4770               trial = src_folded, src_folded_cost = MAX_COST;
4771               if (src_folded_force_flag)
4772                 {
4773                   rtx forced = force_const_mem (mode, trial);
4774                   if (forced)
4775                     trial = forced;
4776                 }
4777             }
4778           else if (src
4779                    && preferable (src_cost, src_regcost,
4780                                   src_eqv_cost, src_eqv_regcost) <= 0
4781                    && preferable (src_cost, src_regcost,
4782                                   src_related_cost, src_related_regcost) <= 0
4783                    && preferable (src_cost, src_regcost,
4784                                   src_elt_cost, src_elt_regcost) <= 0)
4785             trial = src, src_cost = MAX_COST;
4786           else if (src_eqv_here
4787                    && preferable (src_eqv_cost, src_eqv_regcost,
4788                                   src_related_cost, src_related_regcost) <= 0
4789                    && preferable (src_eqv_cost, src_eqv_regcost,
4790                                   src_elt_cost, src_elt_regcost) <= 0)
4791             trial = src_eqv_here, src_eqv_cost = MAX_COST;
4792           else if (src_related
4793                    && preferable (src_related_cost, src_related_regcost,
4794                                   src_elt_cost, src_elt_regcost) <= 0)
4795             trial = src_related, src_related_cost = MAX_COST;
4796           else
4797             {
4798               trial = elt->exp;
4799               elt = elt->next_same_value;
4800               src_elt_cost = MAX_COST;
4801             }
4802
4803           /* Avoid creation of overlapping memory moves.  */
4804           if (MEM_P (trial) && MEM_P (SET_DEST (sets[i].rtl)))
4805             {
4806               rtx src, dest;
4807
4808               /* BLKmode moves are not handled by cse anyway.  */
4809               if (GET_MODE (trial) == BLKmode)
4810                 break;
4811
4812               src = canon_rtx (trial);
4813               dest = canon_rtx (SET_DEST (sets[i].rtl));
4814
4815               if (!MEM_P (src) || !MEM_P (dest)
4816                   || !nonoverlapping_memrefs_p (src, dest))
4817                 break;
4818             }
4819
4820           /* We don't normally have an insn matching (set (pc) (pc)), so
4821              check for this separately here.  We will delete such an
4822              insn below.
4823
4824              For other cases such as a table jump or conditional jump
4825              where we know the ultimate target, go ahead and replace the
4826              operand.  While that may not make a valid insn, we will
4827              reemit the jump below (and also insert any necessary
4828              barriers).  */
4829           if (n_sets == 1 && dest == pc_rtx
4830               && (trial == pc_rtx
4831                   || (GET_CODE (trial) == LABEL_REF
4832                       && ! condjump_p (insn))))
4833             {
4834               /* Don't substitute non-local labels, this confuses CFG.  */
4835               if (GET_CODE (trial) == LABEL_REF
4836                   && LABEL_REF_NONLOCAL_P (trial))
4837                 continue;
4838
4839               SET_SRC (sets[i].rtl) = trial;
4840               cse_jumps_altered = true;
4841               break;
4842             }
4843
4844           /* Reject certain invalid forms of CONST that we create.  */
4845           else if (CONSTANT_P (trial)
4846                    && GET_CODE (trial) == CONST
4847                    /* Reject cases that will cause decode_rtx_const to
4848                       die.  On the alpha when simplifying a switch, we
4849                       get (const (truncate (minus (label_ref)
4850                       (label_ref)))).  */
4851                    && (GET_CODE (XEXP (trial, 0)) == TRUNCATE
4852                        /* Likewise on IA-64, except without the
4853                           truncate.  */
4854                        || (GET_CODE (XEXP (trial, 0)) == MINUS
4855                            && GET_CODE (XEXP (XEXP (trial, 0), 0)) == LABEL_REF
4856                            && GET_CODE (XEXP (XEXP (trial, 0), 1)) == LABEL_REF)))
4857             /* Do nothing for this case.  */
4858             ;
4859
4860           /* Look for a substitution that makes a valid insn.  */
4861           else if (validate_unshare_change
4862                      (insn, &SET_SRC (sets[i].rtl), trial, 0))
4863             {
4864               rtx new_rtx = canon_reg (SET_SRC (sets[i].rtl), insn);
4865
4866               /* The result of apply_change_group can be ignored; see
4867                  canon_reg.  */
4868
4869               validate_change (insn, &SET_SRC (sets[i].rtl), new_rtx, 1);
4870               apply_change_group ();
4871
4872               break;
4873             }
4874
4875           /* If we previously found constant pool entries for
4876              constants and this is a constant, try making a
4877              pool entry.  Put it in src_folded unless we already have done
4878              this since that is where it likely came from.  */
4879
4880           else if (constant_pool_entries_cost
4881                    && CONSTANT_P (trial)
4882                    && (src_folded == 0
4883                        || (!MEM_P (src_folded)
4884                            && ! src_folded_force_flag))
4885                    && GET_MODE_CLASS (mode) != MODE_CC
4886                    && mode != VOIDmode)
4887             {
4888               src_folded_force_flag = 1;
4889               src_folded = trial;
4890               src_folded_cost = constant_pool_entries_cost;
4891               src_folded_regcost = constant_pool_entries_regcost;
4892             }
4893         }
4894
4895       src = SET_SRC (sets[i].rtl);
4896
4897       /* In general, it is good to have a SET with SET_SRC == SET_DEST.
4898          However, there is an important exception:  If both are registers
4899          that are not the head of their equivalence class, replace SET_SRC
4900          with the head of the class.  If we do not do this, we will have
4901          both registers live over a portion of the basic block.  This way,
4902          their lifetimes will likely abut instead of overlapping.  */
4903       if (REG_P (dest)
4904           && REGNO_QTY_VALID_P (REGNO (dest)))
4905         {
4906           int dest_q = REG_QTY (REGNO (dest));
4907           struct qty_table_elem *dest_ent = &qty_table[dest_q];
4908
4909           if (dest_ent->mode == GET_MODE (dest)
4910               && dest_ent->first_reg != REGNO (dest)
4911               && REG_P (src) && REGNO (src) == REGNO (dest)
4912               /* Don't do this if the original insn had a hard reg as
4913                  SET_SRC or SET_DEST.  */
4914               && (!REG_P (sets[i].src)
4915                   || REGNO (sets[i].src) >= FIRST_PSEUDO_REGISTER)
4916               && (!REG_P (dest) || REGNO (dest) >= FIRST_PSEUDO_REGISTER))
4917             /* We can't call canon_reg here because it won't do anything if
4918                SRC is a hard register.  */
4919             {
4920               int src_q = REG_QTY (REGNO (src));
4921               struct qty_table_elem *src_ent = &qty_table[src_q];
4922               int first = src_ent->first_reg;
4923               rtx new_src
4924                 = (first >= FIRST_PSEUDO_REGISTER
4925                    ? regno_reg_rtx[first] : gen_rtx_REG (GET_MODE (src), first));
4926
4927               /* We must use validate-change even for this, because this
4928                  might be a special no-op instruction, suitable only to
4929                  tag notes onto.  */
4930               if (validate_change (insn, &SET_SRC (sets[i].rtl), new_src, 0))
4931                 {
4932                   src = new_src;
4933                   /* If we had a constant that is cheaper than what we are now
4934                      setting SRC to, use that constant.  We ignored it when we
4935                      thought we could make this into a no-op.  */
4936                   if (src_const && COST (src_const) < COST (src)
4937                       && validate_change (insn, &SET_SRC (sets[i].rtl),
4938                                           src_const, 0))
4939                     src = src_const;
4940                 }
4941             }
4942         }
4943
4944       /* If we made a change, recompute SRC values.  */
4945       if (src != sets[i].src)
4946         {
4947           do_not_record = 0;
4948           hash_arg_in_memory = 0;
4949           sets[i].src = src;
4950           sets[i].src_hash = HASH (src, mode);
4951           sets[i].src_volatile = do_not_record;
4952           sets[i].src_in_memory = hash_arg_in_memory;
4953           sets[i].src_elt = lookup (src, sets[i].src_hash, mode);
4954         }
4955
4956       /* If this is a single SET, we are setting a register, and we have an
4957          equivalent constant, we want to add a REG_NOTE.   We don't want
4958          to write a REG_EQUAL note for a constant pseudo since verifying that
4959          that pseudo hasn't been eliminated is a pain.  Such a note also
4960          won't help anything.
4961
4962          Avoid a REG_EQUAL note for (CONST (MINUS (LABEL_REF) (LABEL_REF)))
4963          which can be created for a reference to a compile time computable
4964          entry in a jump table.  */
4965
4966       if (n_sets == 1 && src_const && REG_P (dest)
4967           && !REG_P (src_const)
4968           && ! (GET_CODE (src_const) == CONST
4969                 && GET_CODE (XEXP (src_const, 0)) == MINUS
4970                 && GET_CODE (XEXP (XEXP (src_const, 0), 0)) == LABEL_REF
4971                 && GET_CODE (XEXP (XEXP (src_const, 0), 1)) == LABEL_REF))
4972         {
4973           /* We only want a REG_EQUAL note if src_const != src.  */
4974           if (! rtx_equal_p (src, src_const))
4975             {
4976               /* Make sure that the rtx is not shared.  */
4977               src_const = copy_rtx (src_const);
4978
4979               /* Record the actual constant value in a REG_EQUAL note,
4980                  making a new one if one does not already exist.  */
4981               set_unique_reg_note (insn, REG_EQUAL, src_const);
4982               df_notes_rescan (insn);
4983             }
4984         }
4985
4986       /* Now deal with the destination.  */
4987       do_not_record = 0;
4988
4989       /* Look within any ZERO_EXTRACT to the MEM or REG within it.  */
4990       while (GET_CODE (dest) == SUBREG
4991              || GET_CODE (dest) == ZERO_EXTRACT
4992              || GET_CODE (dest) == STRICT_LOW_PART)
4993         dest = XEXP (dest, 0);
4994
4995       sets[i].inner_dest = dest;
4996
4997       if (MEM_P (dest))
4998         {
4999 #ifdef PUSH_ROUNDING
5000           /* Stack pushes invalidate the stack pointer.  */
5001           rtx addr = XEXP (dest, 0);
5002           if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC
5003               && XEXP (addr, 0) == stack_pointer_rtx)
5004             invalidate (stack_pointer_rtx, VOIDmode);
5005 #endif
5006           dest = fold_rtx (dest, insn);
5007         }
5008
5009       /* Compute the hash code of the destination now,
5010          before the effects of this instruction are recorded,
5011          since the register values used in the address computation
5012          are those before this instruction.  */
5013       sets[i].dest_hash = HASH (dest, mode);
5014
5015       /* Don't enter a bit-field in the hash table
5016          because the value in it after the store
5017          may not equal what was stored, due to truncation.  */
5018
5019       if (GET_CODE (SET_DEST (sets[i].rtl)) == ZERO_EXTRACT)
5020         {
5021           rtx width = XEXP (SET_DEST (sets[i].rtl), 1);
5022
5023           if (src_const != 0 && GET_CODE (src_const) == CONST_INT
5024               && GET_CODE (width) == CONST_INT
5025               && INTVAL (width) < HOST_BITS_PER_WIDE_INT
5026               && ! (INTVAL (src_const)
5027                     & ((HOST_WIDE_INT) (-1) << INTVAL (width))))
5028             /* Exception: if the value is constant,
5029                and it won't be truncated, record it.  */
5030             ;
5031           else
5032             {
5033               /* This is chosen so that the destination will be invalidated
5034                  but no new value will be recorded.
5035                  We must invalidate because sometimes constant
5036                  values can be recorded for bitfields.  */
5037               sets[i].src_elt = 0;
5038               sets[i].src_volatile = 1;
5039               src_eqv = 0;
5040               src_eqv_elt = 0;
5041             }
5042         }
5043
5044       /* If only one set in a JUMP_INSN and it is now a no-op, we can delete
5045          the insn.  */
5046       else if (n_sets == 1 && dest == pc_rtx && src == pc_rtx)
5047         {
5048           /* One less use of the label this insn used to jump to.  */
5049           delete_insn_and_edges (insn);
5050           cse_jumps_altered = true;
5051           /* No more processing for this set.  */
5052           sets[i].rtl = 0;
5053         }
5054
5055       /* If this SET is now setting PC to a label, we know it used to
5056          be a conditional or computed branch.  */
5057       else if (dest == pc_rtx && GET_CODE (src) == LABEL_REF
5058                && !LABEL_REF_NONLOCAL_P (src))
5059         {
5060           /* We reemit the jump in as many cases as possible just in
5061              case the form of an unconditional jump is significantly
5062              different than a computed jump or conditional jump.
5063
5064              If this insn has multiple sets, then reemitting the
5065              jump is nontrivial.  So instead we just force rerecognition
5066              and hope for the best.  */
5067           if (n_sets == 1)
5068             {
5069               rtx new_rtx, note;
5070
5071               new_rtx = emit_jump_insn_before (gen_jump (XEXP (src, 0)), insn);
5072               JUMP_LABEL (new_rtx) = XEXP (src, 0);
5073               LABEL_NUSES (XEXP (src, 0))++;
5074
5075               /* Make sure to copy over REG_NON_LOCAL_GOTO.  */
5076               note = find_reg_note (insn, REG_NON_LOCAL_GOTO, 0);
5077               if (note)
5078                 {
5079                   XEXP (note, 1) = NULL_RTX;
5080                   REG_NOTES (new_rtx) = note;
5081                 }
5082
5083               delete_insn_and_edges (insn);
5084               insn = new_rtx;
5085             }
5086           else
5087             INSN_CODE (insn) = -1;
5088
5089           /* Do not bother deleting any unreachable code, let jump do it.  */
5090           cse_jumps_altered = true;
5091           sets[i].rtl = 0;
5092         }
5093
5094       /* If destination is volatile, invalidate it and then do no further
5095          processing for this assignment.  */
5096
5097       else if (do_not_record)
5098         {
5099           if (REG_P (dest) || GET_CODE (dest) == SUBREG)
5100             invalidate (dest, VOIDmode);
5101           else if (MEM_P (dest))
5102             invalidate (dest, VOIDmode);
5103           else if (GET_CODE (dest) == STRICT_LOW_PART
5104                    || GET_CODE (dest) == ZERO_EXTRACT)
5105             invalidate (XEXP (dest, 0), GET_MODE (dest));
5106           sets[i].rtl = 0;
5107         }
5108
5109       if (sets[i].rtl != 0 && dest != SET_DEST (sets[i].rtl))
5110         sets[i].dest_hash = HASH (SET_DEST (sets[i].rtl), mode);
5111
5112 #ifdef HAVE_cc0
5113       /* If setting CC0, record what it was set to, or a constant, if it
5114          is equivalent to a constant.  If it is being set to a floating-point
5115          value, make a COMPARE with the appropriate constant of 0.  If we
5116          don't do this, later code can interpret this as a test against
5117          const0_rtx, which can cause problems if we try to put it into an
5118          insn as a floating-point operand.  */
5119       if (dest == cc0_rtx)
5120         {
5121           this_insn_cc0 = src_const && mode != VOIDmode ? src_const : src;
5122           this_insn_cc0_mode = mode;
5123           if (FLOAT_MODE_P (mode))
5124             this_insn_cc0 = gen_rtx_COMPARE (VOIDmode, this_insn_cc0,
5125                                              CONST0_RTX (mode));
5126         }
5127 #endif
5128     }
5129
5130   /* Now enter all non-volatile source expressions in the hash table
5131      if they are not already present.
5132      Record their equivalence classes in src_elt.
5133      This way we can insert the corresponding destinations into
5134      the same classes even if the actual sources are no longer in them
5135      (having been invalidated).  */
5136
5137   if (src_eqv && src_eqv_elt == 0 && sets[0].rtl != 0 && ! src_eqv_volatile
5138       && ! rtx_equal_p (src_eqv, SET_DEST (sets[0].rtl)))
5139     {
5140       struct table_elt *elt;
5141       struct table_elt *classp = sets[0].src_elt;
5142       rtx dest = SET_DEST (sets[0].rtl);
5143       enum machine_mode eqvmode = GET_MODE (dest);
5144
5145       if (GET_CODE (dest) == STRICT_LOW_PART)
5146         {
5147           eqvmode = GET_MODE (SUBREG_REG (XEXP (dest, 0)));
5148           classp = 0;
5149         }
5150       if (insert_regs (src_eqv, classp, 0))
5151         {
5152           rehash_using_reg (src_eqv);
5153           src_eqv_hash = HASH (src_eqv, eqvmode);
5154         }
5155       elt = insert (src_eqv, classp, src_eqv_hash, eqvmode);
5156       elt->in_memory = src_eqv_in_memory;
5157       src_eqv_elt = elt;
5158
5159       /* Check to see if src_eqv_elt is the same as a set source which
5160          does not yet have an elt, and if so set the elt of the set source
5161          to src_eqv_elt.  */
5162       for (i = 0; i < n_sets; i++)
5163         if (sets[i].rtl && sets[i].src_elt == 0
5164             && rtx_equal_p (SET_SRC (sets[i].rtl), src_eqv))
5165           sets[i].src_elt = src_eqv_elt;
5166     }
5167
5168   for (i = 0; i < n_sets; i++)
5169     if (sets[i].rtl && ! sets[i].src_volatile
5170         && ! rtx_equal_p (SET_SRC (sets[i].rtl), SET_DEST (sets[i].rtl)))
5171       {
5172         if (GET_CODE (SET_DEST (sets[i].rtl)) == STRICT_LOW_PART)
5173           {
5174             /* REG_EQUAL in setting a STRICT_LOW_PART
5175                gives an equivalent for the entire destination register,
5176                not just for the subreg being stored in now.
5177                This is a more interesting equivalence, so we arrange later
5178                to treat the entire reg as the destination.  */
5179             sets[i].src_elt = src_eqv_elt;
5180             sets[i].src_hash = src_eqv_hash;
5181           }
5182         else
5183           {
5184             /* Insert source and constant equivalent into hash table, if not
5185                already present.  */
5186             struct table_elt *classp = src_eqv_elt;
5187             rtx src = sets[i].src;
5188             rtx dest = SET_DEST (sets[i].rtl);
5189             enum machine_mode mode
5190               = GET_MODE (src) == VOIDmode ? GET_MODE (dest) : GET_MODE (src);
5191
5192             /* It's possible that we have a source value known to be
5193                constant but don't have a REG_EQUAL note on the insn.
5194                Lack of a note will mean src_eqv_elt will be NULL.  This
5195                can happen where we've generated a SUBREG to access a
5196                CONST_INT that is already in a register in a wider mode.
5197                Ensure that the source expression is put in the proper
5198                constant class.  */
5199             if (!classp)
5200               classp = sets[i].src_const_elt;
5201
5202             if (sets[i].src_elt == 0)
5203               {
5204                 struct table_elt *elt;
5205
5206                 /* Note that these insert_regs calls cannot remove
5207                    any of the src_elt's, because they would have failed to
5208                    match if not still valid.  */
5209                 if (insert_regs (src, classp, 0))
5210                   {
5211                     rehash_using_reg (src);
5212                     sets[i].src_hash = HASH (src, mode);
5213                   }
5214                 elt = insert (src, classp, sets[i].src_hash, mode);
5215                 elt->in_memory = sets[i].src_in_memory;
5216                 sets[i].src_elt = classp = elt;
5217               }
5218             if (sets[i].src_const && sets[i].src_const_elt == 0
5219                 && src != sets[i].src_const
5220                 && ! rtx_equal_p (sets[i].src_const, src))
5221               sets[i].src_elt = insert (sets[i].src_const, classp,
5222                                         sets[i].src_const_hash, mode);
5223           }
5224       }
5225     else if (sets[i].src_elt == 0)
5226       /* If we did not insert the source into the hash table (e.g., it was
5227          volatile), note the equivalence class for the REG_EQUAL value, if any,
5228          so that the destination goes into that class.  */
5229       sets[i].src_elt = src_eqv_elt;
5230
5231   /* Record destination addresses in the hash table.  This allows us to
5232      check if they are invalidated by other sets.  */
5233   for (i = 0; i < n_sets; i++)
5234     {
5235       if (sets[i].rtl)
5236         {
5237           rtx x = sets[i].inner_dest;
5238           struct table_elt *elt;
5239           enum machine_mode mode;
5240           unsigned hash;
5241
5242           if (MEM_P (x))
5243             {
5244               x = XEXP (x, 0);
5245               mode = GET_MODE (x);
5246               hash = HASH (x, mode);
5247               elt = lookup (x, hash, mode);
5248               if (!elt)
5249                 {
5250                   if (insert_regs (x, NULL, 0))
5251                     {
5252                       rtx dest = SET_DEST (sets[i].rtl);
5253
5254                       rehash_using_reg (x);
5255                       hash = HASH (x, mode);
5256                       sets[i].dest_hash = HASH (dest, GET_MODE (dest));
5257                     }
5258                   elt = insert (x, NULL, hash, mode);
5259                 }
5260
5261               sets[i].dest_addr_elt = elt;
5262             }
5263           else
5264             sets[i].dest_addr_elt = NULL;
5265         }
5266     }
5267
5268   invalidate_from_clobbers (x);
5269
5270   /* Some registers are invalidated by subroutine calls.  Memory is
5271      invalidated by non-constant calls.  */
5272
5273   if (CALL_P (insn))
5274     {
5275       if (!(RTL_CONST_OR_PURE_CALL_P (insn)))
5276         invalidate_memory ();
5277       invalidate_for_call ();
5278     }
5279
5280   /* Now invalidate everything set by this instruction.
5281      If a SUBREG or other funny destination is being set,
5282      sets[i].rtl is still nonzero, so here we invalidate the reg
5283      a part of which is being set.  */
5284
5285   for (i = 0; i < n_sets; i++)
5286     if (sets[i].rtl)
5287       {
5288         /* We can't use the inner dest, because the mode associated with
5289            a ZERO_EXTRACT is significant.  */
5290         rtx dest = SET_DEST (sets[i].rtl);
5291
5292         /* Needed for registers to remove the register from its
5293            previous quantity's chain.
5294            Needed for memory if this is a nonvarying address, unless
5295            we have just done an invalidate_memory that covers even those.  */
5296         if (REG_P (dest) || GET_CODE (dest) == SUBREG)
5297           invalidate (dest, VOIDmode);
5298         else if (MEM_P (dest))
5299           invalidate (dest, VOIDmode);
5300         else if (GET_CODE (dest) == STRICT_LOW_PART
5301                  || GET_CODE (dest) == ZERO_EXTRACT)
5302           invalidate (XEXP (dest, 0), GET_MODE (dest));
5303       }
5304
5305   /* A volatile ASM invalidates everything.  */
5306   if (NONJUMP_INSN_P (insn)
5307       && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
5308       && MEM_VOLATILE_P (PATTERN (insn)))
5309     flush_hash_table ();
5310
5311   /* Don't cse over a call to setjmp; on some machines (eg VAX)
5312      the regs restored by the longjmp come from a later time
5313      than the setjmp.  */
5314   if (CALL_P (insn) && find_reg_note (insn, REG_SETJMP, NULL))
5315     {
5316       flush_hash_table ();
5317       goto done;
5318     }
5319
5320   /* Make sure registers mentioned in destinations
5321      are safe for use in an expression to be inserted.
5322      This removes from the hash table
5323      any invalid entry that refers to one of these registers.
5324
5325      We don't care about the return value from mention_regs because
5326      we are going to hash the SET_DEST values unconditionally.  */
5327
5328   for (i = 0; i < n_sets; i++)
5329     {
5330       if (sets[i].rtl)
5331         {
5332           rtx x = SET_DEST (sets[i].rtl);
5333
5334           if (!REG_P (x))
5335             mention_regs (x);
5336           else
5337             {
5338               /* We used to rely on all references to a register becoming
5339                  inaccessible when a register changes to a new quantity,
5340                  since that changes the hash code.  However, that is not
5341                  safe, since after HASH_SIZE new quantities we get a
5342                  hash 'collision' of a register with its own invalid
5343                  entries.  And since SUBREGs have been changed not to
5344                  change their hash code with the hash code of the register,
5345                  it wouldn't work any longer at all.  So we have to check
5346                  for any invalid references lying around now.
5347                  This code is similar to the REG case in mention_regs,
5348                  but it knows that reg_tick has been incremented, and
5349                  it leaves reg_in_table as -1 .  */
5350               unsigned int regno = REGNO (x);
5351               unsigned int endregno = END_REGNO (x);
5352               unsigned int i;
5353
5354               for (i = regno; i < endregno; i++)
5355                 {
5356                   if (REG_IN_TABLE (i) >= 0)
5357                     {
5358                       remove_invalid_refs (i);
5359                       REG_IN_TABLE (i) = -1;
5360                     }
5361                 }
5362             }
5363         }
5364     }
5365
5366   /* We may have just removed some of the src_elt's from the hash table.
5367      So replace each one with the current head of the same class.
5368      Also check if destination addresses have been removed.  */
5369
5370   for (i = 0; i < n_sets; i++)
5371     if (sets[i].rtl)
5372       {
5373         if (sets[i].dest_addr_elt
5374             && sets[i].dest_addr_elt->first_same_value == 0)
5375           {
5376             /* The elt was removed, which means this destination is not
5377                valid after this instruction.  */
5378             sets[i].rtl = NULL_RTX;
5379           }
5380         else if (sets[i].src_elt && sets[i].src_elt->first_same_value == 0)
5381           /* If elt was removed, find current head of same class,
5382              or 0 if nothing remains of that class.  */
5383           {
5384             struct table_elt *elt = sets[i].src_elt;
5385
5386             while (elt && elt->prev_same_value)
5387               elt = elt->prev_same_value;
5388
5389             while (elt && elt->first_same_value == 0)
5390               elt = elt->next_same_value;
5391             sets[i].src_elt = elt ? elt->first_same_value : 0;
5392           }
5393       }
5394
5395   /* Now insert the destinations into their equivalence classes.  */
5396
5397   for (i = 0; i < n_sets; i++)
5398     if (sets[i].rtl)
5399       {
5400         rtx dest = SET_DEST (sets[i].rtl);
5401         struct table_elt *elt;
5402
5403         /* Don't record value if we are not supposed to risk allocating
5404            floating-point values in registers that might be wider than
5405            memory.  */
5406         if ((flag_float_store
5407              && MEM_P (dest)
5408              && FLOAT_MODE_P (GET_MODE (dest)))
5409             /* Don't record BLKmode values, because we don't know the
5410                size of it, and can't be sure that other BLKmode values
5411                have the same or smaller size.  */
5412             || GET_MODE (dest) == BLKmode
5413             /* If we didn't put a REG_EQUAL value or a source into the hash
5414                table, there is no point is recording DEST.  */
5415             || sets[i].src_elt == 0
5416             /* If DEST is a paradoxical SUBREG and SRC is a ZERO_EXTEND
5417                or SIGN_EXTEND, don't record DEST since it can cause
5418                some tracking to be wrong.
5419
5420                ??? Think about this more later.  */
5421             || (GET_CODE (dest) == SUBREG
5422                 && (GET_MODE_SIZE (GET_MODE (dest))
5423                     > GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))))
5424                 && (GET_CODE (sets[i].src) == SIGN_EXTEND
5425                     || GET_CODE (sets[i].src) == ZERO_EXTEND)))
5426           continue;
5427
5428         /* STRICT_LOW_PART isn't part of the value BEING set,
5429            and neither is the SUBREG inside it.
5430            Note that in this case SETS[I].SRC_ELT is really SRC_EQV_ELT.  */
5431         if (GET_CODE (dest) == STRICT_LOW_PART)
5432           dest = SUBREG_REG (XEXP (dest, 0));
5433
5434         if (REG_P (dest) || GET_CODE (dest) == SUBREG)
5435           /* Registers must also be inserted into chains for quantities.  */
5436           if (insert_regs (dest, sets[i].src_elt, 1))
5437             {
5438               /* If `insert_regs' changes something, the hash code must be
5439                  recalculated.  */
5440               rehash_using_reg (dest);
5441               sets[i].dest_hash = HASH (dest, GET_MODE (dest));
5442             }
5443
5444         elt = insert (dest, sets[i].src_elt,
5445                       sets[i].dest_hash, GET_MODE (dest));
5446
5447         elt->in_memory = (MEM_P (sets[i].inner_dest)
5448                           && !MEM_READONLY_P (sets[i].inner_dest));
5449
5450         /* If we have (set (subreg:m1 (reg:m2 foo) 0) (bar:m1)), M1 is no
5451            narrower than M2, and both M1 and M2 are the same number of words,
5452            we are also doing (set (reg:m2 foo) (subreg:m2 (bar:m1) 0)) so
5453            make that equivalence as well.
5454
5455            However, BAR may have equivalences for which gen_lowpart
5456            will produce a simpler value than gen_lowpart applied to
5457            BAR (e.g., if BAR was ZERO_EXTENDed from M2), so we will scan all
5458            BAR's equivalences.  If we don't get a simplified form, make
5459            the SUBREG.  It will not be used in an equivalence, but will
5460            cause two similar assignments to be detected.
5461
5462            Note the loop below will find SUBREG_REG (DEST) since we have
5463            already entered SRC and DEST of the SET in the table.  */
5464
5465         if (GET_CODE (dest) == SUBREG
5466             && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))) - 1)
5467                  / UNITS_PER_WORD)
5468                 == (GET_MODE_SIZE (GET_MODE (dest)) - 1) / UNITS_PER_WORD)
5469             && (GET_MODE_SIZE (GET_MODE (dest))
5470                 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))))
5471             && sets[i].src_elt != 0)
5472           {
5473             enum machine_mode new_mode = GET_MODE (SUBREG_REG (dest));
5474             struct table_elt *elt, *classp = 0;
5475
5476             for (elt = sets[i].src_elt->first_same_value; elt;
5477                  elt = elt->next_same_value)
5478               {
5479                 rtx new_src = 0;
5480                 unsigned src_hash;
5481                 struct table_elt *src_elt;
5482                 int byte = 0;
5483
5484                 /* Ignore invalid entries.  */
5485                 if (!REG_P (elt->exp)
5486                     && ! exp_equiv_p (elt->exp, elt->exp, 1, false))
5487                   continue;
5488
5489                 /* We may have already been playing subreg games.  If the
5490                    mode is already correct for the destination, use it.  */
5491                 if (GET_MODE (elt->exp) == new_mode)
5492                   new_src = elt->exp;
5493                 else
5494                   {
5495                     /* Calculate big endian correction for the SUBREG_BYTE.
5496                        We have already checked that M1 (GET_MODE (dest))
5497                        is not narrower than M2 (new_mode).  */
5498                     if (BYTES_BIG_ENDIAN)
5499                       byte = (GET_MODE_SIZE (GET_MODE (dest))
5500                               - GET_MODE_SIZE (new_mode));
5501
5502                     new_src = simplify_gen_subreg (new_mode, elt->exp,
5503                                                    GET_MODE (dest), byte);
5504                   }
5505
5506                 /* The call to simplify_gen_subreg fails if the value
5507                    is VOIDmode, yet we can't do any simplification, e.g.
5508                    for EXPR_LISTs denoting function call results.
5509                    It is invalid to construct a SUBREG with a VOIDmode
5510                    SUBREG_REG, hence a zero new_src means we can't do
5511                    this substitution.  */
5512                 if (! new_src)
5513                   continue;
5514
5515                 src_hash = HASH (new_src, new_mode);
5516                 src_elt = lookup (new_src, src_hash, new_mode);
5517
5518                 /* Put the new source in the hash table is if isn't
5519                    already.  */
5520                 if (src_elt == 0)
5521                   {
5522                     if (insert_regs (new_src, classp, 0))
5523                       {
5524                         rehash_using_reg (new_src);
5525                         src_hash = HASH (new_src, new_mode);
5526                       }
5527                     src_elt = insert (new_src, classp, src_hash, new_mode);
5528                     src_elt->in_memory = elt->in_memory;
5529                   }
5530                 else if (classp && classp != src_elt->first_same_value)
5531                   /* Show that two things that we've seen before are
5532                      actually the same.  */
5533                   merge_equiv_classes (src_elt, classp);
5534
5535                 classp = src_elt->first_same_value;
5536                 /* Ignore invalid entries.  */
5537                 while (classp
5538                        && !REG_P (classp->exp)
5539                        && ! exp_equiv_p (classp->exp, classp->exp, 1, false))
5540                   classp = classp->next_same_value;
5541               }
5542           }
5543       }
5544
5545   /* Special handling for (set REG0 REG1) where REG0 is the
5546      "cheapest", cheaper than REG1.  After cse, REG1 will probably not
5547      be used in the sequel, so (if easily done) change this insn to
5548      (set REG1 REG0) and replace REG1 with REG0 in the previous insn
5549      that computed their value.  Then REG1 will become a dead store
5550      and won't cloud the situation for later optimizations.
5551
5552      Do not make this change if REG1 is a hard register, because it will
5553      then be used in the sequel and we may be changing a two-operand insn
5554      into a three-operand insn.
5555
5556      Also do not do this if we are operating on a copy of INSN.  */
5557
5558   if (n_sets == 1 && sets[0].rtl && REG_P (SET_DEST (sets[0].rtl))
5559       && NEXT_INSN (PREV_INSN (insn)) == insn
5560       && REG_P (SET_SRC (sets[0].rtl))
5561       && REGNO (SET_SRC (sets[0].rtl)) >= FIRST_PSEUDO_REGISTER
5562       && REGNO_QTY_VALID_P (REGNO (SET_SRC (sets[0].rtl))))
5563     {
5564       int src_q = REG_QTY (REGNO (SET_SRC (sets[0].rtl)));
5565       struct qty_table_elem *src_ent = &qty_table[src_q];
5566
5567       if (src_ent->first_reg == REGNO (SET_DEST (sets[0].rtl)))
5568         {
5569           /* Scan for the previous nonnote insn, but stop at a basic
5570              block boundary.  */
5571           rtx prev = insn;
5572           rtx bb_head = BB_HEAD (BLOCK_FOR_INSN (insn));
5573           do
5574             {
5575               prev = PREV_INSN (prev);
5576             }
5577           while (prev != bb_head && NOTE_P (prev));
5578
5579           /* Do not swap the registers around if the previous instruction
5580              attaches a REG_EQUIV note to REG1.
5581
5582              ??? It's not entirely clear whether we can transfer a REG_EQUIV
5583              from the pseudo that originally shadowed an incoming argument
5584              to another register.  Some uses of REG_EQUIV might rely on it
5585              being attached to REG1 rather than REG2.
5586
5587              This section previously turned the REG_EQUIV into a REG_EQUAL
5588              note.  We cannot do that because REG_EQUIV may provide an
5589              uninitialized stack slot when REG_PARM_STACK_SPACE is used.  */
5590           if (NONJUMP_INSN_P (prev)
5591               && GET_CODE (PATTERN (prev)) == SET
5592               && SET_DEST (PATTERN (prev)) == SET_SRC (sets[0].rtl)
5593               && ! find_reg_note (prev, REG_EQUIV, NULL_RTX))
5594             {
5595               rtx dest = SET_DEST (sets[0].rtl);
5596               rtx src = SET_SRC (sets[0].rtl);
5597               rtx note;
5598
5599               validate_change (prev, &SET_DEST (PATTERN (prev)), dest, 1);
5600               validate_change (insn, &SET_DEST (sets[0].rtl), src, 1);
5601               validate_change (insn, &SET_SRC (sets[0].rtl), dest, 1);
5602               apply_change_group ();
5603
5604               /* If INSN has a REG_EQUAL note, and this note mentions
5605                  REG0, then we must delete it, because the value in
5606                  REG0 has changed.  If the note's value is REG1, we must
5607                  also delete it because that is now this insn's dest.  */
5608               note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
5609               if (note != 0
5610                   && (reg_mentioned_p (dest, XEXP (note, 0))
5611                       || rtx_equal_p (src, XEXP (note, 0))))
5612                 remove_note (insn, note);
5613             }
5614         }
5615     }
5616
5617 done:;
5618 }
5619 \f
5620 /* Remove from the hash table all expressions that reference memory.  */
5621
5622 static void
5623 invalidate_memory (void)
5624 {
5625   int i;
5626   struct table_elt *p, *next;
5627
5628   for (i = 0; i < HASH_SIZE; i++)
5629     for (p = table[i]; p; p = next)
5630       {
5631         next = p->next_same_hash;
5632         if (p->in_memory)
5633           remove_from_table (p, i);
5634       }
5635 }
5636
5637 /* Perform invalidation on the basis of everything about an insn
5638    except for invalidating the actual places that are SET in it.
5639    This includes the places CLOBBERed, and anything that might
5640    alias with something that is SET or CLOBBERed.
5641
5642    X is the pattern of the insn.  */
5643
5644 static void
5645 invalidate_from_clobbers (rtx x)
5646 {
5647   if (GET_CODE (x) == CLOBBER)
5648     {
5649       rtx ref = XEXP (x, 0);
5650       if (ref)
5651         {
5652           if (REG_P (ref) || GET_CODE (ref) == SUBREG
5653               || MEM_P (ref))
5654             invalidate (ref, VOIDmode);
5655           else if (GET_CODE (ref) == STRICT_LOW_PART
5656                    || GET_CODE (ref) == ZERO_EXTRACT)
5657             invalidate (XEXP (ref, 0), GET_MODE (ref));
5658         }
5659     }
5660   else if (GET_CODE (x) == PARALLEL)
5661     {
5662       int i;
5663       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
5664         {
5665           rtx y = XVECEXP (x, 0, i);
5666           if (GET_CODE (y) == CLOBBER)
5667             {
5668               rtx ref = XEXP (y, 0);
5669               if (REG_P (ref) || GET_CODE (ref) == SUBREG
5670                   || MEM_P (ref))
5671                 invalidate (ref, VOIDmode);
5672               else if (GET_CODE (ref) == STRICT_LOW_PART
5673                        || GET_CODE (ref) == ZERO_EXTRACT)
5674                 invalidate (XEXP (ref, 0), GET_MODE (ref));
5675             }
5676         }
5677     }
5678 }
5679 \f
5680 /* Process X, part of the REG_NOTES of an insn.  Look at any REG_EQUAL notes
5681    and replace any registers in them with either an equivalent constant
5682    or the canonical form of the register.  If we are inside an address,
5683    only do this if the address remains valid.
5684
5685    OBJECT is 0 except when within a MEM in which case it is the MEM.
5686
5687    Return the replacement for X.  */
5688
5689 static rtx
5690 cse_process_notes_1 (rtx x, rtx object, bool *changed)
5691 {
5692   enum rtx_code code = GET_CODE (x);
5693   const char *fmt = GET_RTX_FORMAT (code);
5694   int i;
5695
5696   switch (code)
5697     {
5698     case CONST_INT:
5699     case CONST:
5700     case SYMBOL_REF:
5701     case LABEL_REF:
5702     case CONST_DOUBLE:
5703     case CONST_FIXED:
5704     case CONST_VECTOR:
5705     case PC:
5706     case CC0:
5707     case LO_SUM:
5708       return x;
5709
5710     case MEM:
5711       validate_change (x, &XEXP (x, 0),
5712                        cse_process_notes (XEXP (x, 0), x, changed), 0);
5713       return x;
5714
5715     case EXPR_LIST:
5716     case INSN_LIST:
5717       if (REG_NOTE_KIND (x) == REG_EQUAL)
5718         XEXP (x, 0) = cse_process_notes (XEXP (x, 0), NULL_RTX, changed);
5719       if (XEXP (x, 1))
5720         XEXP (x, 1) = cse_process_notes (XEXP (x, 1), NULL_RTX, changed);
5721       return x;
5722
5723     case SIGN_EXTEND:
5724     case ZERO_EXTEND:
5725     case SUBREG:
5726       {
5727         rtx new_rtx = cse_process_notes (XEXP (x, 0), object, changed);
5728         /* We don't substitute VOIDmode constants into these rtx,
5729            since they would impede folding.  */
5730         if (GET_MODE (new_rtx) != VOIDmode)
5731           validate_change (object, &XEXP (x, 0), new_rtx, 0);
5732         return x;
5733       }
5734
5735     case REG:
5736       i = REG_QTY (REGNO (x));
5737
5738       /* Return a constant or a constant register.  */
5739       if (REGNO_QTY_VALID_P (REGNO (x)))
5740         {
5741           struct qty_table_elem *ent = &qty_table[i];
5742
5743           if (ent->const_rtx != NULL_RTX
5744               && (CONSTANT_P (ent->const_rtx)
5745                   || REG_P (ent->const_rtx)))
5746             {
5747               rtx new_rtx = gen_lowpart (GET_MODE (x), ent->const_rtx);
5748               if (new_rtx)
5749                 return copy_rtx (new_rtx);
5750             }
5751         }
5752
5753       /* Otherwise, canonicalize this register.  */
5754       return canon_reg (x, NULL_RTX);
5755
5756     default:
5757       break;
5758     }
5759
5760   for (i = 0; i < GET_RTX_LENGTH (code); i++)
5761     if (fmt[i] == 'e')
5762       validate_change (object, &XEXP (x, i),
5763                        cse_process_notes (XEXP (x, i), object, changed), 0);
5764
5765   return x;
5766 }
5767
5768 static rtx
5769 cse_process_notes (rtx x, rtx object, bool *changed)
5770 {
5771   rtx new_rtx = cse_process_notes_1 (x, object, changed);
5772   if (new_rtx != x)
5773     *changed = true;
5774   return new_rtx;
5775 }
5776
5777 \f
5778 /* Find a path in the CFG, starting with FIRST_BB to perform CSE on.
5779
5780    DATA is a pointer to a struct cse_basic_block_data, that is used to
5781    describe the path.
5782    It is filled with a queue of basic blocks, starting with FIRST_BB
5783    and following a trace through the CFG.
5784   
5785    If all paths starting at FIRST_BB have been followed, or no new path
5786    starting at FIRST_BB can be constructed, this function returns FALSE.
5787    Otherwise, DATA->path is filled and the function returns TRUE indicating
5788    that a path to follow was found.
5789
5790    If FOLLOW_JUMPS is false, the maximum path length is 1 and the only
5791    block in the path will be FIRST_BB.  */
5792
5793 static bool
5794 cse_find_path (basic_block first_bb, struct cse_basic_block_data *data,
5795                int follow_jumps)
5796 {
5797   basic_block bb;
5798   edge e;
5799   int path_size;
5800  
5801   SET_BIT (cse_visited_basic_blocks, first_bb->index);
5802
5803   /* See if there is a previous path.  */
5804   path_size = data->path_size;
5805
5806   /* There is a previous path.  Make sure it started with FIRST_BB.  */
5807   if (path_size)
5808     gcc_assert (data->path[0].bb == first_bb);
5809
5810   /* There was only one basic block in the last path.  Clear the path and
5811      return, so that paths starting at another basic block can be tried.  */
5812   if (path_size == 1)
5813     {
5814       path_size = 0;
5815       goto done;
5816     }
5817
5818   /* If the path was empty from the beginning, construct a new path.  */
5819   if (path_size == 0)
5820     data->path[path_size++].bb = first_bb;
5821   else
5822     {
5823       /* Otherwise, path_size must be equal to or greater than 2, because
5824          a previous path exists that is at least two basic blocks long.
5825
5826          Update the previous branch path, if any.  If the last branch was
5827          previously along the branch edge, take the fallthrough edge now.  */
5828       while (path_size >= 2)
5829         {
5830           basic_block last_bb_in_path, previous_bb_in_path;
5831           edge e;
5832
5833           --path_size;
5834           last_bb_in_path = data->path[path_size].bb;
5835           previous_bb_in_path = data->path[path_size - 1].bb;
5836
5837           /* If we previously followed a path along the branch edge, try
5838              the fallthru edge now.  */
5839           if (EDGE_COUNT (previous_bb_in_path->succs) == 2
5840               && any_condjump_p (BB_END (previous_bb_in_path))
5841               && (e = find_edge (previous_bb_in_path, last_bb_in_path))
5842               && e == BRANCH_EDGE (previous_bb_in_path))
5843             {
5844               bb = FALLTHRU_EDGE (previous_bb_in_path)->dest;
5845               if (bb != EXIT_BLOCK_PTR
5846                   && single_pred_p (bb)
5847                   /* We used to assert here that we would only see blocks
5848                      that we have not visited yet.  But we may end up
5849                      visiting basic blocks twice if the CFG has changed
5850                      in this run of cse_main, because when the CFG changes
5851                      the topological sort of the CFG also changes.  A basic
5852                      blocks that previously had more than two predecessors
5853                      may now have a single predecessor, and become part of
5854                      a path that starts at another basic block.
5855
5856                      We still want to visit each basic block only once, so
5857                      halt the path here if we have already visited BB.  */
5858                   && !TEST_BIT (cse_visited_basic_blocks, bb->index))
5859                 {
5860                   SET_BIT (cse_visited_basic_blocks, bb->index);
5861                   data->path[path_size++].bb = bb;
5862                   break;
5863                 }
5864             }
5865
5866           data->path[path_size].bb = NULL;
5867         }
5868
5869       /* If only one block remains in the path, bail.  */
5870       if (path_size == 1)
5871         {
5872           path_size = 0;
5873           goto done;
5874         }
5875     }
5876
5877   /* Extend the path if possible.  */
5878   if (follow_jumps)
5879     {
5880       bb = data->path[path_size - 1].bb;
5881       while (bb && path_size < PARAM_VALUE (PARAM_MAX_CSE_PATH_LENGTH))
5882         {
5883           if (single_succ_p (bb))
5884             e = single_succ_edge (bb);
5885           else if (EDGE_COUNT (bb->succs) == 2
5886                    && any_condjump_p (BB_END (bb)))
5887             {
5888               /* First try to follow the branch.  If that doesn't lead
5889                  to a useful path, follow the fallthru edge.  */
5890               e = BRANCH_EDGE (bb);
5891               if (!single_pred_p (e->dest))
5892                 e = FALLTHRU_EDGE (bb);
5893             }
5894           else
5895             e = NULL;
5896
5897           if (e && e->dest != EXIT_BLOCK_PTR
5898               && single_pred_p (e->dest)
5899               /* Avoid visiting basic blocks twice.  The large comment
5900                  above explains why this can happen.  */
5901               && !TEST_BIT (cse_visited_basic_blocks, e->dest->index))
5902             {
5903               basic_block bb2 = e->dest;
5904               SET_BIT (cse_visited_basic_blocks, bb2->index);
5905               data->path[path_size++].bb = bb2;
5906               bb = bb2;
5907             }
5908           else
5909             bb = NULL;
5910         }
5911     }
5912
5913 done:
5914   data->path_size = path_size;
5915   return path_size != 0;
5916 }
5917 \f
5918 /* Dump the path in DATA to file F.  NSETS is the number of sets
5919    in the path.  */
5920
5921 static void
5922 cse_dump_path (struct cse_basic_block_data *data, int nsets, FILE *f)
5923 {
5924   int path_entry;
5925
5926   fprintf (f, ";; Following path with %d sets: ", nsets);
5927   for (path_entry = 0; path_entry < data->path_size; path_entry++)
5928     fprintf (f, "%d ", (data->path[path_entry].bb)->index);
5929   fputc ('\n', dump_file);
5930   fflush (f);
5931 }
5932
5933 \f
5934 /* Return true if BB has exception handling successor edges.  */
5935
5936 static bool
5937 have_eh_succ_edges (basic_block bb)
5938 {
5939   edge e;
5940   edge_iterator ei;
5941
5942   FOR_EACH_EDGE (e, ei, bb->succs)
5943     if (e->flags & EDGE_EH)
5944       return true;
5945
5946   return false;
5947 }
5948
5949 \f
5950 /* Scan to the end of the path described by DATA.  Return an estimate of
5951    the total number of SETs of all insns in the path.  */
5952
5953 static void
5954 cse_prescan_path (struct cse_basic_block_data *data)
5955 {
5956   int nsets = 0;
5957   int path_size = data->path_size;
5958   int path_entry;
5959
5960   /* Scan to end of each basic block in the path.  */
5961   for (path_entry = 0; path_entry < path_size; path_entry++) 
5962     {
5963       basic_block bb;
5964       rtx insn;
5965
5966       bb = data->path[path_entry].bb;
5967
5968       FOR_BB_INSNS (bb, insn)
5969         {
5970           if (!INSN_P (insn))
5971             continue;
5972
5973           /* A PARALLEL can have lots of SETs in it,
5974              especially if it is really an ASM_OPERANDS.  */
5975           if (GET_CODE (PATTERN (insn)) == PARALLEL)
5976             nsets += XVECLEN (PATTERN (insn), 0);
5977           else
5978             nsets += 1;
5979         }
5980     }
5981
5982   data->nsets = nsets;
5983 }
5984 \f
5985 /* Process a single extended basic block described by EBB_DATA.  */
5986
5987 static void
5988 cse_extended_basic_block (struct cse_basic_block_data *ebb_data)
5989 {
5990   int path_size = ebb_data->path_size;
5991   int path_entry;
5992   int num_insns = 0;
5993
5994   /* Allocate the space needed by qty_table.  */
5995   qty_table = XNEWVEC (struct qty_table_elem, max_qty);
5996
5997   new_basic_block ();
5998   cse_ebb_live_in = df_get_live_in (ebb_data->path[0].bb);
5999   cse_ebb_live_out = df_get_live_out (ebb_data->path[path_size - 1].bb);
6000   for (path_entry = 0; path_entry < path_size; path_entry++)
6001     {
6002       basic_block bb;
6003       rtx insn;
6004
6005       bb = ebb_data->path[path_entry].bb;
6006
6007       /* Invalidate recorded information for eh regs if there is an EH
6008          edge pointing to that bb.  */
6009       if (bb_has_eh_pred (bb))
6010         {
6011           df_ref *def_rec;
6012
6013           for (def_rec = df_get_artificial_defs (bb->index); *def_rec; def_rec++)
6014             {
6015               df_ref def = *def_rec;
6016               if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
6017                 invalidate (DF_REF_REG (def), GET_MODE (DF_REF_REG (def)));
6018             }
6019         }
6020
6021       FOR_BB_INSNS (bb, insn)
6022         {
6023           optimize_this_for_speed_p = optimize_bb_for_speed_p (bb);
6024           /* If we have processed 1,000 insns, flush the hash table to
6025              avoid extreme quadratic behavior.  We must not include NOTEs
6026              in the count since there may be more of them when generating
6027              debugging information.  If we clear the table at different
6028              times, code generated with -g -O might be different than code
6029              generated with -O but not -g.
6030
6031              FIXME: This is a real kludge and needs to be done some other
6032                     way.  */
6033           if (INSN_P (insn)
6034               && num_insns++ > PARAM_VALUE (PARAM_MAX_CSE_INSNS))
6035             {
6036               flush_hash_table ();
6037               num_insns = 0;
6038             }
6039
6040           if (INSN_P (insn))
6041             {
6042               /* Process notes first so we have all notes in canonical forms
6043                  when looking for duplicate operations.  */
6044               if (REG_NOTES (insn))
6045                 {
6046                   bool changed = false;
6047                   REG_NOTES (insn) = cse_process_notes (REG_NOTES (insn),
6048                                                         NULL_RTX, &changed);
6049                   if (changed)
6050                     df_notes_rescan (insn);
6051                 }
6052
6053               cse_insn (insn);
6054
6055               /* If we haven't already found an insn where we added a LABEL_REF,
6056                  check this one.  */
6057               if (INSN_P (insn) && !recorded_label_ref
6058                   && for_each_rtx (&PATTERN (insn), check_for_label_ref,
6059                                    (void *) insn))
6060                 recorded_label_ref = true;
6061
6062 #ifdef HAVE_cc0
6063               /* If the previous insn set CC0 and this insn no longer
6064                  references CC0, delete the previous insn.  Here we use
6065                  fact that nothing expects CC0 to be valid over an insn,
6066                  which is true until the final pass.  */
6067               {
6068                 rtx prev_insn, tem;
6069
6070                 prev_insn = PREV_INSN (insn);
6071                 if (prev_insn && NONJUMP_INSN_P (prev_insn)
6072                     && (tem = single_set (prev_insn)) != 0
6073                     && SET_DEST (tem) == cc0_rtx
6074                     && ! reg_mentioned_p (cc0_rtx, PATTERN (insn)))
6075                   delete_insn (prev_insn);
6076               }
6077
6078               /* If this insn is not the last insn in the basic block,
6079                  it will be PREV_INSN(insn) in the next iteration.  If
6080                  we recorded any CC0-related information for this insn,
6081                  remember it.  */
6082               if (insn != BB_END (bb))
6083                 {
6084                   prev_insn_cc0 = this_insn_cc0;
6085                   prev_insn_cc0_mode = this_insn_cc0_mode;
6086                 }
6087 #endif
6088             }
6089         }
6090
6091       /* With non-call exceptions, we are not always able to update
6092          the CFG properly inside cse_insn.  So clean up possibly
6093          redundant EH edges here.  */
6094       if (flag_non_call_exceptions && have_eh_succ_edges (bb))
6095         cse_cfg_altered |= purge_dead_edges (bb);
6096
6097       /* If we changed a conditional jump, we may have terminated
6098          the path we are following.  Check that by verifying that
6099          the edge we would take still exists.  If the edge does
6100          not exist anymore, purge the remainder of the path.
6101          Note that this will cause us to return to the caller.  */
6102       if (path_entry < path_size - 1)
6103         {
6104           basic_block next_bb = ebb_data->path[path_entry + 1].bb;
6105           if (!find_edge (bb, next_bb))
6106             {
6107               do
6108                 {
6109                   path_size--;
6110
6111                   /* If we truncate the path, we must also reset the
6112                      visited bit on the remaining blocks in the path,
6113                      or we will never visit them at all.  */
6114                   RESET_BIT (cse_visited_basic_blocks,
6115                              ebb_data->path[path_size].bb->index);
6116                   ebb_data->path[path_size].bb = NULL;
6117                 }
6118               while (path_size - 1 != path_entry);
6119               ebb_data->path_size = path_size;
6120             }
6121         }
6122
6123       /* If this is a conditional jump insn, record any known
6124          equivalences due to the condition being tested.  */
6125       insn = BB_END (bb);
6126       if (path_entry < path_size - 1
6127           && JUMP_P (insn)
6128           && single_set (insn)
6129           && any_condjump_p (insn))
6130         {
6131           basic_block next_bb = ebb_data->path[path_entry + 1].bb;
6132           bool taken = (next_bb == BRANCH_EDGE (bb)->dest);
6133           record_jump_equiv (insn, taken);
6134         }
6135
6136 #ifdef HAVE_cc0
6137       /* Clear the CC0-tracking related insns, they can't provide
6138          useful information across basic block boundaries.  */
6139       prev_insn_cc0 = 0;
6140 #endif
6141     }
6142
6143   gcc_assert (next_qty <= max_qty);
6144
6145   free (qty_table);
6146 }
6147
6148 \f
6149 /* Perform cse on the instructions of a function.
6150    F is the first instruction.
6151    NREGS is one plus the highest pseudo-reg number used in the instruction.
6152
6153    Return 2 if jump optimizations should be redone due to simplifications
6154    in conditional jump instructions.
6155    Return 1 if the CFG should be cleaned up because it has been modified.
6156    Return 0 otherwise.  */
6157
6158 int
6159 cse_main (rtx f ATTRIBUTE_UNUSED, int nregs)
6160 {
6161   struct cse_basic_block_data ebb_data;
6162   basic_block bb;
6163   int *rc_order = XNEWVEC (int, last_basic_block);
6164   int i, n_blocks;
6165
6166   df_set_flags (DF_LR_RUN_DCE);
6167   df_analyze ();
6168   df_set_flags (DF_DEFER_INSN_RESCAN);
6169
6170   reg_scan (get_insns (), max_reg_num ());
6171   init_cse_reg_info (nregs);
6172
6173   ebb_data.path = XNEWVEC (struct branch_path,
6174                            PARAM_VALUE (PARAM_MAX_CSE_PATH_LENGTH));
6175
6176   cse_cfg_altered = false;
6177   cse_jumps_altered = false;
6178   recorded_label_ref = false;
6179   constant_pool_entries_cost = 0;
6180   constant_pool_entries_regcost = 0;
6181   ebb_data.path_size = 0;
6182   ebb_data.nsets = 0;
6183   rtl_hooks = cse_rtl_hooks;
6184
6185   init_recog ();
6186   init_alias_analysis ();
6187
6188   reg_eqv_table = XNEWVEC (struct reg_eqv_elem, nregs);
6189
6190   /* Set up the table of already visited basic blocks.  */
6191   cse_visited_basic_blocks = sbitmap_alloc (last_basic_block);
6192   sbitmap_zero (cse_visited_basic_blocks);
6193
6194   /* Loop over basic blocks in reverse completion order (RPO),
6195      excluding the ENTRY and EXIT blocks.  */
6196   n_blocks = pre_and_rev_post_order_compute (NULL, rc_order, false);
6197   i = 0;
6198   while (i < n_blocks)
6199     {
6200       /* Find the first block in the RPO queue that we have not yet
6201          processed before.  */
6202       do
6203         {
6204           bb = BASIC_BLOCK (rc_order[i++]);
6205         }
6206       while (TEST_BIT (cse_visited_basic_blocks, bb->index)
6207              && i < n_blocks);
6208
6209       /* Find all paths starting with BB, and process them.  */
6210       while (cse_find_path (bb, &ebb_data, flag_cse_follow_jumps))
6211         {
6212           /* Pre-scan the path.  */
6213           cse_prescan_path (&ebb_data);
6214
6215           /* If this basic block has no sets, skip it.  */
6216           if (ebb_data.nsets == 0)
6217             continue;
6218
6219           /* Get a reasonable estimate for the maximum number of qty's
6220              needed for this path.  For this, we take the number of sets
6221              and multiply that by MAX_RECOG_OPERANDS.  */
6222           max_qty = ebb_data.nsets * MAX_RECOG_OPERANDS;
6223
6224           /* Dump the path we're about to process.  */
6225           if (dump_file)
6226             cse_dump_path (&ebb_data, ebb_data.nsets, dump_file);
6227
6228           cse_extended_basic_block (&ebb_data);
6229         }
6230     }
6231
6232   /* Clean up.  */
6233   end_alias_analysis ();
6234   free (reg_eqv_table);
6235   free (ebb_data.path);
6236   sbitmap_free (cse_visited_basic_blocks);
6237   free (rc_order);
6238   rtl_hooks = general_rtl_hooks;
6239
6240   if (cse_jumps_altered || recorded_label_ref)
6241     return 2;
6242   else if (cse_cfg_altered)
6243     return 1;
6244   else
6245     return 0;
6246 }
6247 \f
6248 /* Called via for_each_rtx to see if an insn is using a LABEL_REF for
6249    which there isn't a REG_LABEL_OPERAND note.
6250    Return one if so.  DATA is the insn.  */
6251
6252 static int
6253 check_for_label_ref (rtx *rtl, void *data)
6254 {
6255   rtx insn = (rtx) data;
6256
6257   /* If this insn uses a LABEL_REF and there isn't a REG_LABEL_OPERAND
6258      note for it, we must rerun jump since it needs to place the note.  If
6259      this is a LABEL_REF for a CODE_LABEL that isn't in the insn chain,
6260      don't do this since no REG_LABEL_OPERAND will be added.  */
6261   return (GET_CODE (*rtl) == LABEL_REF
6262           && ! LABEL_REF_NONLOCAL_P (*rtl)
6263           && (!JUMP_P (insn)
6264               || !label_is_jump_target_p (XEXP (*rtl, 0), insn))
6265           && LABEL_P (XEXP (*rtl, 0))
6266           && INSN_UID (XEXP (*rtl, 0)) != 0
6267           && ! find_reg_note (insn, REG_LABEL_OPERAND, XEXP (*rtl, 0)));
6268 }
6269 \f
6270 /* Count the number of times registers are used (not set) in X.
6271    COUNTS is an array in which we accumulate the count, INCR is how much
6272    we count each register usage.
6273
6274    Don't count a usage of DEST, which is the SET_DEST of a SET which
6275    contains X in its SET_SRC.  This is because such a SET does not
6276    modify the liveness of DEST.
6277    DEST is set to pc_rtx for a trapping insn, which means that we must count
6278    uses of a SET_DEST regardless because the insn can't be deleted here.  */
6279
6280 static void
6281 count_reg_usage (rtx x, int *counts, rtx dest, int incr)
6282 {
6283   enum rtx_code code;
6284   rtx note;
6285   const char *fmt;
6286   int i, j;
6287
6288   if (x == 0)
6289     return;
6290
6291   switch (code = GET_CODE (x))
6292     {
6293     case REG:
6294       if (x != dest)
6295         counts[REGNO (x)] += incr;
6296       return;
6297
6298     case PC:
6299     case CC0:
6300     case CONST:
6301     case CONST_INT:
6302     case CONST_DOUBLE:
6303     case CONST_FIXED:
6304     case CONST_VECTOR:
6305     case SYMBOL_REF:
6306     case LABEL_REF:
6307       return;
6308
6309     case CLOBBER:
6310       /* If we are clobbering a MEM, mark any registers inside the address
6311          as being used.  */
6312       if (MEM_P (XEXP (x, 0)))
6313         count_reg_usage (XEXP (XEXP (x, 0), 0), counts, NULL_RTX, incr);
6314       return;
6315
6316     case SET:
6317       /* Unless we are setting a REG, count everything in SET_DEST.  */
6318       if (!REG_P (SET_DEST (x)))
6319         count_reg_usage (SET_DEST (x), counts, NULL_RTX, incr);
6320       count_reg_usage (SET_SRC (x), counts,
6321                        dest ? dest : SET_DEST (x),
6322                        incr);
6323       return;
6324
6325     case CALL_INSN:
6326     case INSN:
6327     case JUMP_INSN:
6328     /* We expect dest to be NULL_RTX here.  If the insn may trap, mark
6329        this fact by setting DEST to pc_rtx.  */
6330       if (flag_non_call_exceptions && may_trap_p (PATTERN (x)))
6331         dest = pc_rtx;
6332       if (code == CALL_INSN)
6333         count_reg_usage (CALL_INSN_FUNCTION_USAGE (x), counts, dest, incr);
6334       count_reg_usage (PATTERN (x), counts, dest, incr);
6335
6336       /* Things used in a REG_EQUAL note aren't dead since loop may try to
6337          use them.  */
6338
6339       note = find_reg_equal_equiv_note (x);
6340       if (note)
6341         {
6342           rtx eqv = XEXP (note, 0);
6343
6344           if (GET_CODE (eqv) == EXPR_LIST)
6345           /* This REG_EQUAL note describes the result of a function call.
6346              Process all the arguments.  */
6347             do
6348               {
6349                 count_reg_usage (XEXP (eqv, 0), counts, dest, incr);
6350                 eqv = XEXP (eqv, 1);
6351               }
6352             while (eqv && GET_CODE (eqv) == EXPR_LIST);
6353           else
6354             count_reg_usage (eqv, counts, dest, incr);
6355         }
6356       return;
6357
6358     case EXPR_LIST:
6359       if (REG_NOTE_KIND (x) == REG_EQUAL
6360           || (REG_NOTE_KIND (x) != REG_NONNEG && GET_CODE (XEXP (x,0)) == USE)
6361           /* FUNCTION_USAGE expression lists may include (CLOBBER (mem /u)),
6362              involving registers in the address.  */
6363           || GET_CODE (XEXP (x, 0)) == CLOBBER)
6364         count_reg_usage (XEXP (x, 0), counts, NULL_RTX, incr);
6365
6366       count_reg_usage (XEXP (x, 1), counts, NULL_RTX, incr);
6367       return;
6368
6369     case ASM_OPERANDS:
6370       /* If the asm is volatile, then this insn cannot be deleted,
6371          and so the inputs *must* be live.  */
6372       if (MEM_VOLATILE_P (x))
6373         dest = NULL_RTX;
6374       /* Iterate over just the inputs, not the constraints as well.  */
6375       for (i = ASM_OPERANDS_INPUT_LENGTH (x) - 1; i >= 0; i--)
6376         count_reg_usage (ASM_OPERANDS_INPUT (x, i), counts, dest, incr);
6377       return;
6378
6379     case INSN_LIST:
6380       gcc_unreachable ();
6381
6382     default:
6383       break;
6384     }
6385
6386   fmt = GET_RTX_FORMAT (code);
6387   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6388     {
6389       if (fmt[i] == 'e')
6390         count_reg_usage (XEXP (x, i), counts, dest, incr);
6391       else if (fmt[i] == 'E')
6392         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6393           count_reg_usage (XVECEXP (x, i, j), counts, dest, incr);
6394     }
6395 }
6396 \f
6397 /* Return true if set is live.  */
6398 static bool
6399 set_live_p (rtx set, rtx insn ATTRIBUTE_UNUSED, /* Only used with HAVE_cc0.  */
6400             int *counts)
6401 {
6402 #ifdef HAVE_cc0
6403   rtx tem;
6404 #endif
6405
6406   if (set_noop_p (set))
6407     ;
6408
6409 #ifdef HAVE_cc0
6410   else if (GET_CODE (SET_DEST (set)) == CC0
6411            && !side_effects_p (SET_SRC (set))
6412            && ((tem = next_nonnote_insn (insn)) == 0
6413                || !INSN_P (tem)
6414                || !reg_referenced_p (cc0_rtx, PATTERN (tem))))
6415     return false;
6416 #endif
6417   else if (!REG_P (SET_DEST (set))
6418            || REGNO (SET_DEST (set)) < FIRST_PSEUDO_REGISTER
6419            || counts[REGNO (SET_DEST (set))] != 0
6420            || side_effects_p (SET_SRC (set)))
6421     return true;
6422   return false;
6423 }
6424
6425 /* Return true if insn is live.  */
6426
6427 static bool
6428 insn_live_p (rtx insn, int *counts)
6429 {
6430   int i;
6431   if (flag_non_call_exceptions && may_trap_p (PATTERN (insn)))
6432     return true;
6433   else if (GET_CODE (PATTERN (insn)) == SET)
6434     return set_live_p (PATTERN (insn), insn, counts);
6435   else if (GET_CODE (PATTERN (insn)) == PARALLEL)
6436     {
6437       for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
6438         {
6439           rtx elt = XVECEXP (PATTERN (insn), 0, i);
6440
6441           if (GET_CODE (elt) == SET)
6442             {
6443               if (set_live_p (elt, insn, counts))
6444                 return true;
6445             }
6446           else if (GET_CODE (elt) != CLOBBER && GET_CODE (elt) != USE)
6447             return true;
6448         }
6449       return false;
6450     }
6451   else
6452     return true;
6453 }
6454
6455 /* Scan all the insns and delete any that are dead; i.e., they store a register
6456    that is never used or they copy a register to itself.
6457
6458    This is used to remove insns made obviously dead by cse, loop or other
6459    optimizations.  It improves the heuristics in loop since it won't try to
6460    move dead invariants out of loops or make givs for dead quantities.  The
6461    remaining passes of the compilation are also sped up.  */
6462
6463 int
6464 delete_trivially_dead_insns (rtx insns, int nreg)
6465 {
6466   int *counts;
6467   rtx insn, prev;
6468   int ndead = 0;
6469
6470   timevar_push (TV_DELETE_TRIVIALLY_DEAD);
6471   /* First count the number of times each register is used.  */
6472   counts = XCNEWVEC (int, nreg);
6473   for (insn = insns; insn; insn = NEXT_INSN (insn))
6474     if (INSN_P (insn))
6475       count_reg_usage (insn, counts, NULL_RTX, 1);
6476
6477   /* Go from the last insn to the first and delete insns that only set unused
6478      registers or copy a register to itself.  As we delete an insn, remove
6479      usage counts for registers it uses.
6480
6481      The first jump optimization pass may leave a real insn as the last
6482      insn in the function.   We must not skip that insn or we may end
6483      up deleting code that is not really dead.  */
6484   for (insn = get_last_insn (); insn; insn = prev)
6485     {
6486       int live_insn = 0;
6487
6488       prev = PREV_INSN (insn);
6489       if (!INSN_P (insn))
6490         continue;
6491
6492       live_insn = insn_live_p (insn, counts);
6493
6494       /* If this is a dead insn, delete it and show registers in it aren't
6495          being used.  */
6496
6497       if (! live_insn && dbg_cnt (delete_trivial_dead))
6498         {
6499           count_reg_usage (insn, counts, NULL_RTX, -1);
6500           delete_insn_and_edges (insn);
6501           ndead++;
6502         }
6503     }
6504
6505   if (dump_file && ndead)
6506     fprintf (dump_file, "Deleted %i trivially dead insns\n",
6507              ndead);
6508   /* Clean up.  */
6509   free (counts);
6510   timevar_pop (TV_DELETE_TRIVIALLY_DEAD);
6511   return ndead;
6512 }
6513
6514 /* This function is called via for_each_rtx.  The argument, NEWREG, is
6515    a condition code register with the desired mode.  If we are looking
6516    at the same register in a different mode, replace it with
6517    NEWREG.  */
6518
6519 static int
6520 cse_change_cc_mode (rtx *loc, void *data)
6521 {
6522   struct change_cc_mode_args* args = (struct change_cc_mode_args*)data;
6523
6524   if (*loc
6525       && REG_P (*loc)
6526       && REGNO (*loc) == REGNO (args->newreg)
6527       && GET_MODE (*loc) != GET_MODE (args->newreg))
6528     {
6529       validate_change (args->insn, loc, args->newreg, 1);
6530       
6531       return -1;
6532     }
6533   return 0;
6534 }
6535
6536 /* Change the mode of any reference to the register REGNO (NEWREG) to
6537    GET_MODE (NEWREG) in INSN.  */
6538
6539 static void
6540 cse_change_cc_mode_insn (rtx insn, rtx newreg)
6541 {
6542   struct change_cc_mode_args args;
6543   int success;
6544
6545   if (!INSN_P (insn))
6546     return;
6547
6548   args.insn = insn;
6549   args.newreg = newreg;
6550   
6551   for_each_rtx (&PATTERN (insn), cse_change_cc_mode, &args);
6552   for_each_rtx (&REG_NOTES (insn), cse_change_cc_mode, &args);
6553   
6554   /* If the following assertion was triggered, there is most probably
6555      something wrong with the cc_modes_compatible back end function.
6556      CC modes only can be considered compatible if the insn - with the mode
6557      replaced by any of the compatible modes - can still be recognized.  */
6558   success = apply_change_group ();
6559   gcc_assert (success);
6560 }
6561
6562 /* Change the mode of any reference to the register REGNO (NEWREG) to
6563    GET_MODE (NEWREG), starting at START.  Stop before END.  Stop at
6564    any instruction which modifies NEWREG.  */
6565
6566 static void
6567 cse_change_cc_mode_insns (rtx start, rtx end, rtx newreg)
6568 {
6569   rtx insn;
6570
6571   for (insn = start; insn != end; insn = NEXT_INSN (insn))
6572     {
6573       if (! INSN_P (insn))
6574         continue;
6575
6576       if (reg_set_p (newreg, insn))
6577         return;
6578
6579       cse_change_cc_mode_insn (insn, newreg);
6580     }
6581 }
6582
6583 /* BB is a basic block which finishes with CC_REG as a condition code
6584    register which is set to CC_SRC.  Look through the successors of BB
6585    to find blocks which have a single predecessor (i.e., this one),
6586    and look through those blocks for an assignment to CC_REG which is
6587    equivalent to CC_SRC.  CAN_CHANGE_MODE indicates whether we are
6588    permitted to change the mode of CC_SRC to a compatible mode.  This
6589    returns VOIDmode if no equivalent assignments were found.
6590    Otherwise it returns the mode which CC_SRC should wind up with.
6591    ORIG_BB should be the same as BB in the outermost cse_cc_succs call,
6592    but is passed unmodified down to recursive calls in order to prevent
6593    endless recursion.
6594
6595    The main complexity in this function is handling the mode issues.
6596    We may have more than one duplicate which we can eliminate, and we
6597    try to find a mode which will work for multiple duplicates.  */
6598
6599 static enum machine_mode
6600 cse_cc_succs (basic_block bb, basic_block orig_bb, rtx cc_reg, rtx cc_src,
6601               bool can_change_mode)
6602 {
6603   bool found_equiv;
6604   enum machine_mode mode;
6605   unsigned int insn_count;
6606   edge e;
6607   rtx insns[2];
6608   enum machine_mode modes[2];
6609   rtx last_insns[2];
6610   unsigned int i;
6611   rtx newreg;
6612   edge_iterator ei;
6613
6614   /* We expect to have two successors.  Look at both before picking
6615      the final mode for the comparison.  If we have more successors
6616      (i.e., some sort of table jump, although that seems unlikely),
6617      then we require all beyond the first two to use the same
6618      mode.  */
6619
6620   found_equiv = false;
6621   mode = GET_MODE (cc_src);
6622   insn_count = 0;
6623   FOR_EACH_EDGE (e, ei, bb->succs)
6624     {
6625       rtx insn;
6626       rtx end;
6627
6628       if (e->flags & EDGE_COMPLEX)
6629         continue;
6630
6631       if (EDGE_COUNT (e->dest->preds) != 1
6632           || e->dest == EXIT_BLOCK_PTR
6633           /* Avoid endless recursion on unreachable blocks.  */
6634           || e->dest == orig_bb)
6635         continue;
6636
6637       end = NEXT_INSN (BB_END (e->dest));
6638       for (insn = BB_HEAD (e->dest); insn != end; insn = NEXT_INSN (insn))
6639         {
6640           rtx set;
6641
6642           if (! INSN_P (insn))
6643             continue;
6644
6645           /* If CC_SRC is modified, we have to stop looking for
6646              something which uses it.  */
6647           if (modified_in_p (cc_src, insn))
6648             break;
6649
6650           /* Check whether INSN sets CC_REG to CC_SRC.  */
6651           set = single_set (insn);
6652           if (set
6653               && REG_P (SET_DEST (set))
6654               && REGNO (SET_DEST (set)) == REGNO (cc_reg))
6655             {
6656               bool found;
6657               enum machine_mode set_mode;
6658               enum machine_mode comp_mode;
6659
6660               found = false;
6661               set_mode = GET_MODE (SET_SRC (set));
6662               comp_mode = set_mode;
6663               if (rtx_equal_p (cc_src, SET_SRC (set)))
6664                 found = true;
6665               else if (GET_CODE (cc_src) == COMPARE
6666                        && GET_CODE (SET_SRC (set)) == COMPARE
6667                        && mode != set_mode
6668                        && rtx_equal_p (XEXP (cc_src, 0),
6669                                        XEXP (SET_SRC (set), 0))
6670                        && rtx_equal_p (XEXP (cc_src, 1),
6671                                        XEXP (SET_SRC (set), 1)))
6672                            
6673                 {
6674                   comp_mode = targetm.cc_modes_compatible (mode, set_mode);
6675                   if (comp_mode != VOIDmode
6676                       && (can_change_mode || comp_mode == mode))
6677                     found = true;
6678                 }
6679
6680               if (found)
6681                 {
6682                   found_equiv = true;
6683                   if (insn_count < ARRAY_SIZE (insns))
6684                     {
6685                       insns[insn_count] = insn;
6686                       modes[insn_count] = set_mode;
6687                       last_insns[insn_count] = end;
6688                       ++insn_count;
6689
6690                       if (mode != comp_mode)
6691                         {
6692                           gcc_assert (can_change_mode);
6693                           mode = comp_mode;
6694
6695                           /* The modified insn will be re-recognized later.  */
6696                           PUT_MODE (cc_src, mode);
6697                         }
6698                     }
6699                   else
6700                     {
6701                       if (set_mode != mode)
6702                         {
6703                           /* We found a matching expression in the
6704                              wrong mode, but we don't have room to
6705                              store it in the array.  Punt.  This case
6706                              should be rare.  */
6707                           break;
6708                         }
6709                       /* INSN sets CC_REG to a value equal to CC_SRC
6710                          with the right mode.  We can simply delete
6711                          it.  */
6712                       delete_insn (insn);
6713                     }
6714
6715                   /* We found an instruction to delete.  Keep looking,
6716                      in the hopes of finding a three-way jump.  */
6717                   continue;
6718                 }
6719
6720               /* We found an instruction which sets the condition
6721                  code, so don't look any farther.  */
6722               break;
6723             }
6724
6725           /* If INSN sets CC_REG in some other way, don't look any
6726              farther.  */
6727           if (reg_set_p (cc_reg, insn))
6728             break;
6729         }
6730
6731       /* If we fell off the bottom of the block, we can keep looking
6732          through successors.  We pass CAN_CHANGE_MODE as false because
6733          we aren't prepared to handle compatibility between the
6734          further blocks and this block.  */
6735       if (insn == end)
6736         {
6737           enum machine_mode submode;
6738
6739           submode = cse_cc_succs (e->dest, orig_bb, cc_reg, cc_src, false);
6740           if (submode != VOIDmode)
6741             {
6742               gcc_assert (submode == mode);
6743               found_equiv = true;
6744               can_change_mode = false;
6745             }
6746         }
6747     }
6748
6749   if (! found_equiv)
6750     return VOIDmode;
6751
6752   /* Now INSN_COUNT is the number of instructions we found which set
6753      CC_REG to a value equivalent to CC_SRC.  The instructions are in
6754      INSNS.  The modes used by those instructions are in MODES.  */
6755
6756   newreg = NULL_RTX;
6757   for (i = 0; i < insn_count; ++i)
6758     {
6759       if (modes[i] != mode)
6760         {
6761           /* We need to change the mode of CC_REG in INSNS[i] and
6762              subsequent instructions.  */
6763           if (! newreg)
6764             {
6765               if (GET_MODE (cc_reg) == mode)
6766                 newreg = cc_reg;
6767               else
6768                 newreg = gen_rtx_REG (mode, REGNO (cc_reg));
6769             }
6770           cse_change_cc_mode_insns (NEXT_INSN (insns[i]), last_insns[i],
6771                                     newreg);
6772         }
6773
6774       delete_insn_and_edges (insns[i]);
6775     }
6776
6777   return mode;
6778 }
6779
6780 /* If we have a fixed condition code register (or two), walk through
6781    the instructions and try to eliminate duplicate assignments.  */
6782
6783 static void
6784 cse_condition_code_reg (void)
6785 {
6786   unsigned int cc_regno_1;
6787   unsigned int cc_regno_2;
6788   rtx cc_reg_1;
6789   rtx cc_reg_2;
6790   basic_block bb;
6791
6792   if (! targetm.fixed_condition_code_regs (&cc_regno_1, &cc_regno_2))
6793     return;
6794
6795   cc_reg_1 = gen_rtx_REG (CCmode, cc_regno_1);
6796   if (cc_regno_2 != INVALID_REGNUM)
6797     cc_reg_2 = gen_rtx_REG (CCmode, cc_regno_2);
6798   else
6799     cc_reg_2 = NULL_RTX;
6800
6801   FOR_EACH_BB (bb)
6802     {
6803       rtx last_insn;
6804       rtx cc_reg;
6805       rtx insn;
6806       rtx cc_src_insn;
6807       rtx cc_src;
6808       enum machine_mode mode;
6809       enum machine_mode orig_mode;
6810
6811       /* Look for blocks which end with a conditional jump based on a
6812          condition code register.  Then look for the instruction which
6813          sets the condition code register.  Then look through the
6814          successor blocks for instructions which set the condition
6815          code register to the same value.  There are other possible
6816          uses of the condition code register, but these are by far the
6817          most common and the ones which we are most likely to be able
6818          to optimize.  */
6819
6820       last_insn = BB_END (bb);
6821       if (!JUMP_P (last_insn))
6822         continue;
6823
6824       if (reg_referenced_p (cc_reg_1, PATTERN (last_insn)))
6825         cc_reg = cc_reg_1;
6826       else if (cc_reg_2 && reg_referenced_p (cc_reg_2, PATTERN (last_insn)))
6827         cc_reg = cc_reg_2;
6828       else
6829         continue;
6830
6831       cc_src_insn = NULL_RTX;
6832       cc_src = NULL_RTX;
6833       for (insn = PREV_INSN (last_insn);
6834            insn && insn != PREV_INSN (BB_HEAD (bb));
6835            insn = PREV_INSN (insn))
6836         {
6837           rtx set;
6838
6839           if (! INSN_P (insn))
6840             continue;
6841           set = single_set (insn);
6842           if (set
6843               && REG_P (SET_DEST (set))
6844               && REGNO (SET_DEST (set)) == REGNO (cc_reg))
6845             {
6846               cc_src_insn = insn;
6847               cc_src = SET_SRC (set);
6848               break;
6849             }
6850           else if (reg_set_p (cc_reg, insn))
6851             break;
6852         }
6853
6854       if (! cc_src_insn)
6855         continue;
6856
6857       if (modified_between_p (cc_src, cc_src_insn, NEXT_INSN (last_insn)))
6858         continue;
6859
6860       /* Now CC_REG is a condition code register used for a
6861          conditional jump at the end of the block, and CC_SRC, in
6862          CC_SRC_INSN, is the value to which that condition code
6863          register is set, and CC_SRC is still meaningful at the end of
6864          the basic block.  */
6865
6866       orig_mode = GET_MODE (cc_src);
6867       mode = cse_cc_succs (bb, bb, cc_reg, cc_src, true);
6868       if (mode != VOIDmode)
6869         {
6870           gcc_assert (mode == GET_MODE (cc_src));
6871           if (mode != orig_mode)
6872             {
6873               rtx newreg = gen_rtx_REG (mode, REGNO (cc_reg));
6874
6875               cse_change_cc_mode_insn (cc_src_insn, newreg);
6876
6877               /* Do the same in the following insns that use the
6878                  current value of CC_REG within BB.  */
6879               cse_change_cc_mode_insns (NEXT_INSN (cc_src_insn),
6880                                         NEXT_INSN (last_insn),
6881                                         newreg);
6882             }
6883         }
6884     }
6885 }
6886 \f
6887
6888 /* Perform common subexpression elimination.  Nonzero value from
6889    `cse_main' means that jumps were simplified and some code may now
6890    be unreachable, so do jump optimization again.  */
6891 static bool
6892 gate_handle_cse (void)
6893 {
6894   return optimize > 0;
6895 }
6896
6897 static unsigned int
6898 rest_of_handle_cse (void)
6899 {
6900   int tem;
6901
6902   if (dump_file)
6903     dump_flow_info (dump_file, dump_flags);
6904
6905   tem = cse_main (get_insns (), max_reg_num ());
6906
6907   /* If we are not running more CSE passes, then we are no longer
6908      expecting CSE to be run.  But always rerun it in a cheap mode.  */
6909   cse_not_expected = !flag_rerun_cse_after_loop && !flag_gcse;
6910
6911   if (tem == 2)
6912     {
6913       timevar_push (TV_JUMP);
6914       rebuild_jump_labels (get_insns ());
6915       cleanup_cfg (0);
6916       timevar_pop (TV_JUMP);
6917     }
6918   else if (tem == 1 || optimize > 1)
6919     cleanup_cfg (0);
6920
6921   return 0;
6922 }
6923
6924 struct rtl_opt_pass pass_cse =
6925 {
6926  {
6927   RTL_PASS,
6928   "cse1",                               /* name */
6929   gate_handle_cse,                      /* gate */   
6930   rest_of_handle_cse,                   /* execute */       
6931   NULL,                                 /* sub */
6932   NULL,                                 /* next */
6933   0,                                    /* static_pass_number */
6934   TV_CSE,                               /* tv_id */
6935   0,                                    /* properties_required */
6936   0,                                    /* properties_provided */
6937   0,                                    /* properties_destroyed */
6938   0,                                    /* todo_flags_start */
6939   TODO_df_finish | TODO_verify_rtl_sharing |
6940   TODO_dump_func |
6941   TODO_ggc_collect |
6942   TODO_verify_flow,                     /* todo_flags_finish */
6943  }
6944 };
6945
6946
6947 static bool
6948 gate_handle_cse2 (void)
6949 {
6950   return optimize > 0 && flag_rerun_cse_after_loop;
6951 }
6952
6953 /* Run second CSE pass after loop optimizations.  */
6954 static unsigned int
6955 rest_of_handle_cse2 (void)
6956 {
6957   int tem;
6958
6959   if (dump_file)
6960     dump_flow_info (dump_file, dump_flags);
6961
6962   tem = cse_main (get_insns (), max_reg_num ());
6963
6964   /* Run a pass to eliminate duplicated assignments to condition code
6965      registers.  We have to run this after bypass_jumps, because it
6966      makes it harder for that pass to determine whether a jump can be
6967      bypassed safely.  */
6968   cse_condition_code_reg ();
6969
6970   delete_trivially_dead_insns (get_insns (), max_reg_num ());
6971
6972   if (tem == 2)
6973     {
6974       timevar_push (TV_JUMP);
6975       rebuild_jump_labels (get_insns ());
6976       cleanup_cfg (0);
6977       timevar_pop (TV_JUMP);
6978     }
6979   else if (tem == 1)
6980     cleanup_cfg (0);
6981
6982   cse_not_expected = 1;
6983   return 0;
6984 }
6985
6986
6987 struct rtl_opt_pass pass_cse2 =
6988 {
6989  {
6990   RTL_PASS,
6991   "cse2",                               /* name */
6992   gate_handle_cse2,                     /* gate */   
6993   rest_of_handle_cse2,                  /* execute */       
6994   NULL,                                 /* sub */
6995   NULL,                                 /* next */
6996   0,                                    /* static_pass_number */
6997   TV_CSE2,                              /* tv_id */
6998   0,                                    /* properties_required */
6999   0,                                    /* properties_provided */
7000   0,                                    /* properties_destroyed */
7001   0,                                    /* todo_flags_start */
7002   TODO_df_finish | TODO_verify_rtl_sharing |
7003   TODO_dump_func |
7004   TODO_ggc_collect |
7005   TODO_verify_flow                      /* todo_flags_finish */
7006  }
7007 };
7008