OSDN Git Service

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