OSDN Git Service

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