OSDN Git Service

* cse.c (delete_trivially_dead_insns): Replace alloca with
[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 (sets[i].rtl && sets[i].src_elt == 0
5734             && rtx_equal_p (SET_SRC (sets[i].rtl), src_eqv))
5735           sets[i].src_elt = src_eqv_elt;
5736     }
5737
5738   for (i = 0; i < n_sets; i++)
5739     if (sets[i].rtl && ! sets[i].src_volatile
5740         && ! rtx_equal_p (SET_SRC (sets[i].rtl), SET_DEST (sets[i].rtl)))
5741       {
5742         if (GET_CODE (SET_DEST (sets[i].rtl)) == STRICT_LOW_PART)
5743           {
5744             /* REG_EQUAL in setting a STRICT_LOW_PART
5745                gives an equivalent for the entire destination register,
5746                not just for the subreg being stored in now.
5747                This is a more interesting equivalence, so we arrange later
5748                to treat the entire reg as the destination.  */
5749             sets[i].src_elt = src_eqv_elt;
5750             sets[i].src_hash = src_eqv_hash;
5751           }
5752         else
5753           {
5754             /* Insert source and constant equivalent into hash table, if not
5755                already present.  */
5756             register struct table_elt *classp = src_eqv_elt;
5757             register rtx src = sets[i].src;
5758             register rtx dest = SET_DEST (sets[i].rtl);
5759             enum machine_mode mode
5760               = GET_MODE (src) == VOIDmode ? GET_MODE (dest) : GET_MODE (src);
5761
5762             if (sets[i].src_elt == 0)
5763               {
5764                 /* Don't put a hard register source into the table if this is
5765                    the last insn of a libcall.  In this case, we only need
5766                    to put src_eqv_elt in src_elt.  */
5767                 if (GET_CODE (src) != REG
5768                     || REGNO (src) >= FIRST_PSEUDO_REGISTER
5769                     || ! find_reg_note (insn, REG_RETVAL, NULL_RTX))
5770                   {
5771                     register struct table_elt *elt;
5772
5773                     /* Note that these insert_regs calls cannot remove
5774                        any of the src_elt's, because they would have failed to
5775                        match if not still valid.  */
5776                     if (insert_regs (src, classp, 0))
5777                       {
5778                         rehash_using_reg (src);
5779                         sets[i].src_hash = HASH (src, mode);
5780                       }
5781                     elt = insert (src, classp, sets[i].src_hash, mode);
5782                     elt->in_memory = sets[i].src_in_memory;
5783                     elt->in_struct = sets[i].src_in_struct;
5784                     sets[i].src_elt = classp = elt;
5785                   }
5786                 else
5787                   sets[i].src_elt = classp;
5788               }
5789             if (sets[i].src_const && sets[i].src_const_elt == 0
5790                 && src != sets[i].src_const
5791                 && ! rtx_equal_p (sets[i].src_const, src))
5792               sets[i].src_elt = insert (sets[i].src_const, classp,
5793                                         sets[i].src_const_hash, mode);
5794           }
5795       }
5796     else if (sets[i].src_elt == 0)
5797       /* If we did not insert the source into the hash table (e.g., it was
5798          volatile), note the equivalence class for the REG_EQUAL value, if any,
5799          so that the destination goes into that class.  */
5800       sets[i].src_elt = src_eqv_elt;
5801
5802   invalidate_from_clobbers (x);
5803
5804   /* Some registers are invalidated by subroutine calls.  Memory is 
5805      invalidated by non-constant calls.  */
5806
5807   if (GET_CODE (insn) == CALL_INSN)
5808     {
5809       if (! CONST_CALL_P (insn))
5810         invalidate_memory ();
5811       invalidate_for_call ();
5812     }
5813
5814   /* Now invalidate everything set by this instruction.
5815      If a SUBREG or other funny destination is being set,
5816      sets[i].rtl is still nonzero, so here we invalidate the reg
5817      a part of which is being set.  */
5818
5819   for (i = 0; i < n_sets; i++)
5820     if (sets[i].rtl)
5821       {
5822         /* We can't use the inner dest, because the mode associated with
5823            a ZERO_EXTRACT is significant.  */
5824         register rtx dest = SET_DEST (sets[i].rtl);
5825
5826         /* Needed for registers to remove the register from its
5827            previous quantity's chain.
5828            Needed for memory if this is a nonvarying address, unless
5829            we have just done an invalidate_memory that covers even those.  */
5830         if (GET_CODE (dest) == REG || GET_CODE (dest) == SUBREG
5831             || GET_CODE (dest) == MEM)
5832           invalidate (dest, VOIDmode);
5833         else if (GET_CODE (dest) == STRICT_LOW_PART
5834                  || GET_CODE (dest) == ZERO_EXTRACT)
5835           invalidate (XEXP (dest, 0), GET_MODE (dest));
5836       }
5837
5838   /* A volatile ASM invalidates everything.  */
5839   if (GET_CODE (insn) == INSN
5840       && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
5841       && MEM_VOLATILE_P (PATTERN (insn)))
5842     flush_hash_table ();
5843
5844   /* Make sure registers mentioned in destinations
5845      are safe for use in an expression to be inserted.
5846      This removes from the hash table
5847      any invalid entry that refers to one of these registers.
5848
5849      We don't care about the return value from mention_regs because
5850      we are going to hash the SET_DEST values unconditionally.  */
5851
5852   for (i = 0; i < n_sets; i++)
5853     {
5854       if (sets[i].rtl)
5855         {
5856           rtx x = SET_DEST (sets[i].rtl);
5857
5858           if (GET_CODE (x) != REG)
5859             mention_regs (x);
5860           else
5861             {
5862               /* We used to rely on all references to a register becoming
5863                  inaccessible when a register changes to a new quantity,
5864                  since that changes the hash code.  However, that is not
5865                  safe, since after NBUCKETS new quantities we get a
5866                  hash 'collision' of a register with its own invalid
5867                  entries.  And since SUBREGs have been changed not to
5868                  change their hash code with the hash code of the register,
5869                  it wouldn't work any longer at all.  So we have to check
5870                  for any invalid references lying around now.
5871                  This code is similar to the REG case in mention_regs,
5872                  but it knows that reg_tick has been incremented, and
5873                  it leaves reg_in_table as -1 .  */
5874               register int regno = REGNO (x);
5875               register int endregno
5876                 = regno + (regno >= FIRST_PSEUDO_REGISTER ? 1
5877                            : HARD_REGNO_NREGS (regno, GET_MODE (x)));
5878               int i;
5879
5880               for (i = regno; i < endregno; i++)
5881                 {
5882                   if (REG_IN_TABLE (i) >= 0)
5883                     {
5884                       remove_invalid_refs (i);
5885                       REG_IN_TABLE (i) = -1;
5886                     }
5887                 }
5888             }
5889         }
5890     }
5891
5892   /* We may have just removed some of the src_elt's from the hash table.
5893      So replace each one with the current head of the same class.  */
5894
5895   for (i = 0; i < n_sets; i++)
5896     if (sets[i].rtl)
5897       {
5898         if (sets[i].src_elt && sets[i].src_elt->first_same_value == 0)
5899           /* If elt was removed, find current head of same class,
5900              or 0 if nothing remains of that class.  */
5901           {
5902             register struct table_elt *elt = sets[i].src_elt;
5903
5904             while (elt && elt->prev_same_value)
5905               elt = elt->prev_same_value;
5906
5907             while (elt && elt->first_same_value == 0)
5908               elt = elt->next_same_value;
5909             sets[i].src_elt = elt ? elt->first_same_value : 0;
5910           }
5911       }
5912
5913   /* Now insert the destinations into their equivalence classes.  */
5914
5915   for (i = 0; i < n_sets; i++)
5916     if (sets[i].rtl)
5917       {
5918         register rtx dest = SET_DEST (sets[i].rtl);
5919         rtx inner_dest = sets[i].inner_dest;
5920         register struct table_elt *elt;
5921
5922         /* Don't record value if we are not supposed to risk allocating
5923            floating-point values in registers that might be wider than
5924            memory.  */
5925         if ((flag_float_store
5926              && GET_CODE (dest) == MEM
5927              && FLOAT_MODE_P (GET_MODE (dest)))
5928             /* Don't record BLKmode values, because we don't know the
5929                size of it, and can't be sure that other BLKmode values
5930                have the same or smaller size.  */
5931             || GET_MODE (dest) == BLKmode
5932             /* Don't record values of destinations set inside a libcall block
5933                since we might delete the libcall.  Things should have been set
5934                up so we won't want to reuse such a value, but we play it safe
5935                here.  */
5936             || libcall_insn
5937             /* If we didn't put a REG_EQUAL value or a source into the hash
5938                table, there is no point is recording DEST.  */
5939             || sets[i].src_elt == 0
5940             /* If DEST is a paradoxical SUBREG and SRC is a ZERO_EXTEND
5941                or SIGN_EXTEND, don't record DEST since it can cause
5942                some tracking to be wrong.
5943
5944                ??? Think about this more later.  */
5945             || (GET_CODE (dest) == SUBREG
5946                 && (GET_MODE_SIZE (GET_MODE (dest))
5947                     > GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))))
5948                 && (GET_CODE (sets[i].src) == SIGN_EXTEND
5949                     || GET_CODE (sets[i].src) == ZERO_EXTEND)))
5950           continue;
5951
5952         /* STRICT_LOW_PART isn't part of the value BEING set,
5953            and neither is the SUBREG inside it.
5954            Note that in this case SETS[I].SRC_ELT is really SRC_EQV_ELT.  */
5955         if (GET_CODE (dest) == STRICT_LOW_PART)
5956           dest = SUBREG_REG (XEXP (dest, 0));
5957
5958         if (GET_CODE (dest) == REG || GET_CODE (dest) == SUBREG)
5959           /* Registers must also be inserted into chains for quantities.  */
5960           if (insert_regs (dest, sets[i].src_elt, 1))
5961             {
5962               /* If `insert_regs' changes something, the hash code must be
5963                  recalculated.  */
5964               rehash_using_reg (dest);
5965               sets[i].dest_hash = HASH (dest, GET_MODE (dest));
5966             }
5967
5968         if (GET_CODE (inner_dest) == MEM
5969             && GET_CODE (XEXP (inner_dest, 0)) == ADDRESSOF)
5970           /* Given (SET (MEM (ADDRESSOF (X))) Y) we don't want to say
5971              that (MEM (ADDRESSOF (X))) is equivalent to Y. 
5972              Consider the case in which the address of the MEM is
5973              passed to a function, which alters the MEM.  Then, if we
5974              later use Y instead of the MEM we'll miss the update.  */
5975           elt = insert (dest, 0, sets[i].dest_hash, GET_MODE (dest));
5976         else
5977           elt = insert (dest, sets[i].src_elt,
5978                         sets[i].dest_hash, GET_MODE (dest));
5979
5980         elt->in_memory = (GET_CODE (sets[i].inner_dest) == MEM
5981                           && (! RTX_UNCHANGING_P (sets[i].inner_dest)
5982                               || FIXED_BASE_PLUS_P (XEXP (sets[i].inner_dest,
5983                                                           0))));
5984
5985         if (elt->in_memory)
5986           {
5987             /* This implicitly assumes a whole struct
5988                need not have MEM_IN_STRUCT_P.
5989                But a whole struct is *supposed* to have MEM_IN_STRUCT_P.  */
5990             elt->in_struct = (MEM_IN_STRUCT_P (sets[i].inner_dest)
5991                               || sets[i].inner_dest != SET_DEST (sets[i].rtl));
5992           }
5993
5994         /* If we have (set (subreg:m1 (reg:m2 foo) 0) (bar:m1)), M1 is no
5995            narrower than M2, and both M1 and M2 are the same number of words,
5996            we are also doing (set (reg:m2 foo) (subreg:m2 (bar:m1) 0)) so
5997            make that equivalence as well.
5998
5999            However, BAR may have equivalences for which gen_lowpart_if_possible
6000            will produce a simpler value than gen_lowpart_if_possible applied to
6001            BAR (e.g., if BAR was ZERO_EXTENDed from M2), so we will scan all
6002            BAR's equivalences.  If we don't get a simplified form, make 
6003            the SUBREG.  It will not be used in an equivalence, but will
6004            cause two similar assignments to be detected.
6005
6006            Note the loop below will find SUBREG_REG (DEST) since we have
6007            already entered SRC and DEST of the SET in the table.  */
6008
6009         if (GET_CODE (dest) == SUBREG
6010             && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))) - 1)
6011                  / UNITS_PER_WORD)
6012                 == (GET_MODE_SIZE (GET_MODE (dest)) - 1)/ UNITS_PER_WORD)
6013             && (GET_MODE_SIZE (GET_MODE (dest))
6014                 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))))
6015             && sets[i].src_elt != 0)
6016           {
6017             enum machine_mode new_mode = GET_MODE (SUBREG_REG (dest));
6018             struct table_elt *elt, *classp = 0;
6019
6020             for (elt = sets[i].src_elt->first_same_value; elt;
6021                  elt = elt->next_same_value)
6022               {
6023                 rtx new_src = 0;
6024                 unsigned src_hash;
6025                 struct table_elt *src_elt;
6026
6027                 /* Ignore invalid entries.  */
6028                 if (GET_CODE (elt->exp) != REG
6029                     && ! exp_equiv_p (elt->exp, elt->exp, 1, 0))
6030                   continue;
6031
6032                 new_src = gen_lowpart_if_possible (new_mode, elt->exp);
6033                 if (new_src == 0)
6034                   new_src = gen_rtx_SUBREG (new_mode, elt->exp, 0);
6035
6036                 src_hash = HASH (new_src, new_mode);
6037                 src_elt = lookup (new_src, src_hash, new_mode);
6038
6039                 /* Put the new source in the hash table is if isn't
6040                    already.  */
6041                 if (src_elt == 0)
6042                   {
6043                     if (insert_regs (new_src, classp, 0))
6044                       {
6045                         rehash_using_reg (new_src);
6046                         src_hash = HASH (new_src, new_mode);
6047                       }
6048                     src_elt = insert (new_src, classp, src_hash, new_mode);
6049                     src_elt->in_memory = elt->in_memory;
6050                     src_elt->in_struct = elt->in_struct;
6051                   }
6052                 else if (classp && classp != src_elt->first_same_value)
6053                   /* Show that two things that we've seen before are 
6054                      actually the same.  */
6055                   merge_equiv_classes (src_elt, classp);
6056
6057                 classp = src_elt->first_same_value;
6058                 /* Ignore invalid entries.  */
6059                 while (classp
6060                        && GET_CODE (classp->exp) != REG
6061                        && ! exp_equiv_p (classp->exp, classp->exp, 1, 0))
6062                   classp = classp->next_same_value;
6063               }
6064           }
6065       }
6066
6067   /* Special handling for (set REG0 REG1)
6068      where REG0 is the "cheapest", cheaper than REG1.
6069      After cse, REG1 will probably not be used in the sequel, 
6070      so (if easily done) change this insn to (set REG1 REG0) and
6071      replace REG1 with REG0 in the previous insn that computed their value.
6072      Then REG1 will become a dead store and won't cloud the situation
6073      for later optimizations.
6074
6075      Do not make this change if REG1 is a hard register, because it will
6076      then be used in the sequel and we may be changing a two-operand insn
6077      into a three-operand insn.
6078
6079      Also do not do this if we are operating on a copy of INSN.
6080
6081      Also don't do this if INSN ends a libcall; this would cause an unrelated
6082      register to be set in the middle of a libcall, and we then get bad code
6083      if the libcall is deleted.  */
6084
6085   if (n_sets == 1 && sets[0].rtl && GET_CODE (SET_DEST (sets[0].rtl)) == REG
6086       && NEXT_INSN (PREV_INSN (insn)) == insn
6087       && GET_CODE (SET_SRC (sets[0].rtl)) == REG
6088       && REGNO (SET_SRC (sets[0].rtl)) >= FIRST_PSEUDO_REGISTER
6089       && REGNO_QTY_VALID_P (REGNO (SET_SRC (sets[0].rtl)))
6090       && (qty_first_reg[REG_QTY (REGNO (SET_SRC (sets[0].rtl)))]
6091           == REGNO (SET_DEST (sets[0].rtl)))
6092       && ! find_reg_note (insn, REG_RETVAL, NULL_RTX))
6093     {
6094       rtx prev = PREV_INSN (insn);
6095       while (prev && GET_CODE (prev) == NOTE)
6096         prev = PREV_INSN (prev);
6097
6098       if (prev && GET_CODE (prev) == INSN && GET_CODE (PATTERN (prev)) == SET
6099           && SET_DEST (PATTERN (prev)) == SET_SRC (sets[0].rtl))
6100         {
6101           rtx dest = SET_DEST (sets[0].rtl);
6102           rtx note = find_reg_note (prev, REG_EQUIV, NULL_RTX);
6103
6104           validate_change (prev, & SET_DEST (PATTERN (prev)), dest, 1);
6105           validate_change (insn, & SET_DEST (sets[0].rtl),
6106                            SET_SRC (sets[0].rtl), 1);
6107           validate_change (insn, & SET_SRC (sets[0].rtl), dest, 1);
6108           apply_change_group ();
6109
6110           /* If REG1 was equivalent to a constant, REG0 is not.  */
6111           if (note)
6112             PUT_REG_NOTE_KIND (note, REG_EQUAL);
6113
6114           /* If there was a REG_WAS_0 note on PREV, remove it.  Move
6115              any REG_WAS_0 note on INSN to PREV.  */
6116           note = find_reg_note (prev, REG_WAS_0, NULL_RTX);
6117           if (note)
6118             remove_note (prev, note);
6119
6120           note = find_reg_note (insn, REG_WAS_0, NULL_RTX);
6121           if (note)
6122             {
6123               remove_note (insn, note);
6124               XEXP (note, 1) = REG_NOTES (prev);
6125               REG_NOTES (prev) = note;
6126             }
6127
6128           /* If INSN has a REG_EQUAL note, and this note mentions REG0,
6129              then we must delete it, because the value in REG0 has changed.  */
6130           note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
6131           if (note && reg_mentioned_p (dest, XEXP (note, 0)))
6132             remove_note (insn, note);
6133         }
6134     }
6135
6136   /* If this is a conditional jump insn, record any known equivalences due to
6137      the condition being tested.  */
6138
6139   last_jump_equiv_class = 0;
6140   if (GET_CODE (insn) == JUMP_INSN
6141       && n_sets == 1 && GET_CODE (x) == SET
6142       && GET_CODE (SET_SRC (x)) == IF_THEN_ELSE)
6143     record_jump_equiv (insn, 0);
6144
6145 #ifdef HAVE_cc0
6146   /* If the previous insn set CC0 and this insn no longer references CC0,
6147      delete the previous insn.  Here we use the fact that nothing expects CC0
6148      to be valid over an insn, which is true until the final pass.  */
6149   if (prev_insn && GET_CODE (prev_insn) == INSN
6150       && (tem = single_set (prev_insn)) != 0
6151       && SET_DEST (tem) == cc0_rtx
6152       && ! reg_mentioned_p (cc0_rtx, x))
6153     {
6154       PUT_CODE (prev_insn, NOTE);
6155       NOTE_LINE_NUMBER (prev_insn) = NOTE_INSN_DELETED;
6156       NOTE_SOURCE_FILE (prev_insn) = 0;
6157     }
6158
6159   prev_insn_cc0 = this_insn_cc0;
6160   prev_insn_cc0_mode = this_insn_cc0_mode;
6161 #endif
6162
6163   prev_insn = insn;
6164 }
6165 \f
6166 /* Remove from the hash table all expressions that reference memory.  */
6167
6168 static void
6169 invalidate_memory ()
6170 {
6171   register int i;
6172   register struct table_elt *p, *next;
6173
6174   for (i = 0; i < NBUCKETS; i++)
6175     for (p = table[i]; p; p = next)
6176       {
6177         next = p->next_same_hash;
6178         if (p->in_memory)
6179           remove_from_table (p, i);
6180       }
6181 }
6182
6183 #ifdef AUTO_INC_DEC
6184
6185 /* If ADDR is an address that implicitly affects the stack pointer, return
6186    1 and update the register tables to show the effect.  Else, return 0.  */
6187
6188 static int
6189 addr_affects_sp_p (addr)
6190      register rtx addr;
6191 {
6192   if ((GET_CODE (addr) == PRE_DEC || GET_CODE (addr) == PRE_INC
6193        || GET_CODE (addr) == POST_DEC || GET_CODE (addr) == POST_INC)
6194       && GET_CODE (XEXP (addr, 0)) == REG
6195       && REGNO (XEXP (addr, 0)) == STACK_POINTER_REGNUM)
6196     {
6197       if (REG_TICK (STACK_POINTER_REGNUM) >= 0)
6198         REG_TICK (STACK_POINTER_REGNUM)++;
6199
6200       /* This should be *very* rare.  */
6201       if (TEST_HARD_REG_BIT (hard_regs_in_table, STACK_POINTER_REGNUM))
6202         invalidate (stack_pointer_rtx, VOIDmode);
6203
6204       return 1;
6205     }
6206
6207   return 0;
6208 }
6209 #endif
6210
6211 /* Perform invalidation on the basis of everything about an insn
6212    except for invalidating the actual places that are SET in it.
6213    This includes the places CLOBBERed, and anything that might
6214    alias with something that is SET or CLOBBERed.
6215
6216    X is the pattern of the insn.  */
6217
6218 static void
6219 invalidate_from_clobbers (x)
6220      rtx x;
6221 {
6222   if (GET_CODE (x) == CLOBBER)
6223     {
6224       rtx ref = XEXP (x, 0);
6225       if (ref)
6226         {
6227           if (GET_CODE (ref) == REG || GET_CODE (ref) == SUBREG
6228               || GET_CODE (ref) == MEM)
6229             invalidate (ref, VOIDmode);
6230           else if (GET_CODE (ref) == STRICT_LOW_PART
6231                    || GET_CODE (ref) == ZERO_EXTRACT)
6232             invalidate (XEXP (ref, 0), GET_MODE (ref));
6233         }
6234     }
6235   else if (GET_CODE (x) == PARALLEL)
6236     {
6237       register int i;
6238       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
6239         {
6240           register rtx y = XVECEXP (x, 0, i);
6241           if (GET_CODE (y) == CLOBBER)
6242             {
6243               rtx ref = XEXP (y, 0);
6244               if (GET_CODE (ref) == REG || GET_CODE (ref) == SUBREG
6245                   || GET_CODE (ref) == MEM)
6246                 invalidate (ref, VOIDmode);
6247               else if (GET_CODE (ref) == STRICT_LOW_PART
6248                        || GET_CODE (ref) == ZERO_EXTRACT)
6249                 invalidate (XEXP (ref, 0), GET_MODE (ref));
6250             }
6251         }
6252     }
6253 }
6254 \f
6255 /* Process X, part of the REG_NOTES of an insn.  Look at any REG_EQUAL notes
6256    and replace any registers in them with either an equivalent constant
6257    or the canonical form of the register.  If we are inside an address,
6258    only do this if the address remains valid.
6259
6260    OBJECT is 0 except when within a MEM in which case it is the MEM.
6261
6262    Return the replacement for X.  */
6263
6264 static rtx
6265 cse_process_notes (x, object)
6266      rtx x;
6267      rtx object;
6268 {
6269   enum rtx_code code = GET_CODE (x);
6270   const char *fmt = GET_RTX_FORMAT (code);
6271   int i;
6272
6273   switch (code)
6274     {
6275     case CONST_INT:
6276     case CONST:
6277     case SYMBOL_REF:
6278     case LABEL_REF:
6279     case CONST_DOUBLE:
6280     case PC:
6281     case CC0:
6282     case LO_SUM:
6283       return x;
6284
6285     case MEM:
6286       XEXP (x, 0) = cse_process_notes (XEXP (x, 0), x);
6287       return x;
6288
6289     case EXPR_LIST:
6290     case INSN_LIST:
6291       if (REG_NOTE_KIND (x) == REG_EQUAL)
6292         XEXP (x, 0) = cse_process_notes (XEXP (x, 0), NULL_RTX);
6293       if (XEXP (x, 1))
6294         XEXP (x, 1) = cse_process_notes (XEXP (x, 1), NULL_RTX);
6295       return x;
6296
6297     case SIGN_EXTEND:
6298     case ZERO_EXTEND:
6299     case SUBREG:
6300       {
6301         rtx new = cse_process_notes (XEXP (x, 0), object);
6302         /* We don't substitute VOIDmode constants into these rtx,
6303            since they would impede folding.  */
6304         if (GET_MODE (new) != VOIDmode)
6305           validate_change (object, &XEXP (x, 0), new, 0);
6306         return x;
6307       }
6308
6309     case REG:
6310       i = REG_QTY (REGNO (x));
6311
6312       /* Return a constant or a constant register.  */
6313       if (REGNO_QTY_VALID_P (REGNO (x))
6314           && qty_const[i] != 0
6315           && (CONSTANT_P (qty_const[i])
6316               || GET_CODE (qty_const[i]) == REG))
6317         {
6318           rtx new = gen_lowpart_if_possible (GET_MODE (x), qty_const[i]);
6319           if (new)
6320             return new;
6321         }
6322
6323       /* Otherwise, canonicalize this register.  */
6324       return canon_reg (x, NULL_RTX);
6325       
6326     default:
6327       break;
6328     }
6329
6330   for (i = 0; i < GET_RTX_LENGTH (code); i++)
6331     if (fmt[i] == 'e')
6332       validate_change (object, &XEXP (x, i),
6333                        cse_process_notes (XEXP (x, i), object), 0);
6334
6335   return x;
6336 }
6337 \f
6338 /* Find common subexpressions between the end test of a loop and the beginning
6339    of the loop.  LOOP_START is the CODE_LABEL at the start of a loop.
6340
6341    Often we have a loop where an expression in the exit test is used
6342    in the body of the loop.  For example "while (*p) *q++ = *p++;".
6343    Because of the way we duplicate the loop exit test in front of the loop,
6344    however, we don't detect that common subexpression.  This will be caught
6345    when global cse is implemented, but this is a quite common case.
6346
6347    This function handles the most common cases of these common expressions.
6348    It is called after we have processed the basic block ending with the
6349    NOTE_INSN_LOOP_END note that ends a loop and the previous JUMP_INSN
6350    jumps to a label used only once.  */
6351
6352 static void
6353 cse_around_loop (loop_start)
6354      rtx loop_start;
6355 {
6356   rtx insn;
6357   int i;
6358   struct table_elt *p;
6359
6360   /* If the jump at the end of the loop doesn't go to the start, we don't
6361      do anything.  */
6362   for (insn = PREV_INSN (loop_start);
6363        insn && (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) >= 0);
6364        insn = PREV_INSN (insn))
6365     ;
6366
6367   if (insn == 0
6368       || GET_CODE (insn) != NOTE
6369       || NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG)
6370     return;
6371
6372   /* If the last insn of the loop (the end test) was an NE comparison,
6373      we will interpret it as an EQ comparison, since we fell through
6374      the loop.  Any equivalences resulting from that comparison are
6375      therefore not valid and must be invalidated.  */
6376   if (last_jump_equiv_class)
6377     for (p = last_jump_equiv_class->first_same_value; p;
6378          p = p->next_same_value)
6379       {
6380         if (GET_CODE (p->exp) == MEM || GET_CODE (p->exp) == REG
6381             || (GET_CODE (p->exp) == SUBREG
6382                 && GET_CODE (SUBREG_REG (p->exp)) == REG))
6383           invalidate (p->exp, VOIDmode);
6384         else if (GET_CODE (p->exp) == STRICT_LOW_PART
6385                  || GET_CODE (p->exp) == ZERO_EXTRACT)
6386           invalidate (XEXP (p->exp, 0), GET_MODE (p->exp));
6387       }
6388
6389   /* Process insns starting after LOOP_START until we hit a CALL_INSN or
6390      a CODE_LABEL (we could handle a CALL_INSN, but it isn't worth it).
6391
6392      The only thing we do with SET_DEST is invalidate entries, so we
6393      can safely process each SET in order.  It is slightly less efficient
6394      to do so, but we only want to handle the most common cases.
6395
6396      The gen_move_insn call in cse_set_around_loop may create new pseudos.
6397      These pseudos won't have valid entries in any of the tables indexed
6398      by register number, such as reg_qty.  We avoid out-of-range array
6399      accesses by not processing any instructions created after cse started.  */
6400
6401   for (insn = NEXT_INSN (loop_start);
6402        GET_CODE (insn) != CALL_INSN && GET_CODE (insn) != CODE_LABEL
6403        && INSN_UID (insn) < max_insn_uid
6404        && ! (GET_CODE (insn) == NOTE
6405              && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END);
6406        insn = NEXT_INSN (insn))
6407     {
6408       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
6409           && (GET_CODE (PATTERN (insn)) == SET
6410               || GET_CODE (PATTERN (insn)) == CLOBBER))
6411         cse_set_around_loop (PATTERN (insn), insn, loop_start);
6412       else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
6413                && GET_CODE (PATTERN (insn)) == PARALLEL)
6414         for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
6415           if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET
6416               || GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == CLOBBER)
6417             cse_set_around_loop (XVECEXP (PATTERN (insn), 0, i), insn,
6418                                  loop_start);
6419     }
6420 }
6421 \f
6422 /* Process one SET of an insn that was skipped.  We ignore CLOBBERs
6423    since they are done elsewhere.  This function is called via note_stores.  */
6424
6425 static void
6426 invalidate_skipped_set (dest, set, data)
6427      rtx set;
6428      rtx dest;
6429      void *data ATTRIBUTE_UNUSED;
6430 {
6431   enum rtx_code code = GET_CODE (dest);
6432
6433   if (code == MEM
6434 #ifdef AUTO_INC_DEC
6435       && ! addr_affects_sp_p (dest)     /* If this is not a stack push ... */
6436 #endif
6437       /* There are times when an address can appear varying and be a PLUS
6438          during this scan when it would be a fixed address were we to know
6439          the proper equivalences.  So invalidate all memory if there is
6440          a BLKmode or nonscalar memory reference or a reference to a
6441          variable address.  */
6442       && (MEM_IN_STRUCT_P (dest) || GET_MODE (dest) == BLKmode
6443           || cse_rtx_varies_p (XEXP (dest, 0))))
6444     {
6445       invalidate_memory ();
6446       return;
6447     }
6448
6449   if (GET_CODE (set) == CLOBBER
6450 #ifdef HAVE_cc0
6451       || dest == cc0_rtx
6452 #endif
6453       || dest == pc_rtx)
6454     return;
6455
6456   if (code == STRICT_LOW_PART || code == ZERO_EXTRACT)
6457     invalidate (XEXP (dest, 0), GET_MODE (dest));
6458   else if (code == REG || code == SUBREG || code == MEM)
6459     invalidate (dest, VOIDmode);
6460 }
6461
6462 /* Invalidate all insns from START up to the end of the function or the
6463    next label.  This called when we wish to CSE around a block that is
6464    conditionally executed.  */
6465
6466 static void
6467 invalidate_skipped_block (start)
6468      rtx start;
6469 {
6470   rtx insn;
6471
6472   for (insn = start; insn && GET_CODE (insn) != CODE_LABEL;
6473        insn = NEXT_INSN (insn))
6474     {
6475       if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
6476         continue;
6477
6478       if (GET_CODE (insn) == CALL_INSN)
6479         {
6480           if (! CONST_CALL_P (insn))
6481             invalidate_memory ();
6482           invalidate_for_call ();
6483         }
6484
6485       invalidate_from_clobbers (PATTERN (insn));
6486       note_stores (PATTERN (insn), invalidate_skipped_set, NULL);
6487     }
6488 }
6489 \f
6490 /* If modifying X will modify the value in *DATA (which is really an
6491    `rtx *'), indicate that fact by setting the pointed to value to
6492    NULL_RTX.  */
6493
6494 static void
6495 cse_check_loop_start (x, set, data)
6496      rtx x;
6497      rtx set ATTRIBUTE_UNUSED;
6498      void *data;
6499 {
6500   rtx *cse_check_loop_start_value = (rtx *) data;
6501
6502   if (*cse_check_loop_start_value == NULL_RTX
6503       || GET_CODE (x) == CC0 || GET_CODE (x) == PC)
6504     return;
6505
6506   if ((GET_CODE (x) == MEM && GET_CODE (*cse_check_loop_start_value) == MEM)
6507       || reg_overlap_mentioned_p (x, *cse_check_loop_start_value))
6508     *cse_check_loop_start_value = NULL_RTX;
6509 }
6510
6511 /* X is a SET or CLOBBER contained in INSN that was found near the start of
6512    a loop that starts with the label at LOOP_START.
6513
6514    If X is a SET, we see if its SET_SRC is currently in our hash table.
6515    If so, we see if it has a value equal to some register used only in the
6516    loop exit code (as marked by jump.c).
6517
6518    If those two conditions are true, we search backwards from the start of
6519    the loop to see if that same value was loaded into a register that still
6520    retains its value at the start of the loop.
6521
6522    If so, we insert an insn after the load to copy the destination of that
6523    load into the equivalent register and (try to) replace our SET_SRC with that
6524    register.
6525
6526    In any event, we invalidate whatever this SET or CLOBBER modifies.  */
6527
6528 static void
6529 cse_set_around_loop (x, insn, loop_start)
6530      rtx x;
6531      rtx insn;
6532      rtx loop_start;
6533 {
6534   struct table_elt *src_elt;
6535
6536   /* If this is a SET, see if we can replace SET_SRC, but ignore SETs that
6537      are setting PC or CC0 or whose SET_SRC is already a register.  */
6538   if (GET_CODE (x) == SET
6539       && GET_CODE (SET_DEST (x)) != PC && GET_CODE (SET_DEST (x)) != CC0
6540       && GET_CODE (SET_SRC (x)) != REG)
6541     {
6542       src_elt = lookup (SET_SRC (x),
6543                         HASH (SET_SRC (x), GET_MODE (SET_DEST (x))),
6544                         GET_MODE (SET_DEST (x)));
6545
6546       if (src_elt)
6547         for (src_elt = src_elt->first_same_value; src_elt;
6548              src_elt = src_elt->next_same_value)
6549           if (GET_CODE (src_elt->exp) == REG && REG_LOOP_TEST_P (src_elt->exp)
6550               && COST (src_elt->exp) < COST (SET_SRC (x)))
6551             {
6552               rtx p, set;
6553
6554               /* Look for an insn in front of LOOP_START that sets
6555                  something in the desired mode to SET_SRC (x) before we hit
6556                  a label or CALL_INSN.  */
6557
6558               for (p = prev_nonnote_insn (loop_start);
6559                    p && GET_CODE (p) != CALL_INSN
6560                    && GET_CODE (p) != CODE_LABEL;
6561                    p = prev_nonnote_insn  (p))
6562                 if ((set = single_set (p)) != 0
6563                     && GET_CODE (SET_DEST (set)) == REG
6564                     && GET_MODE (SET_DEST (set)) == src_elt->mode
6565                     && rtx_equal_p (SET_SRC (set), SET_SRC (x)))
6566                   {
6567                     /* We now have to ensure that nothing between P
6568                        and LOOP_START modified anything referenced in
6569                        SET_SRC (x).  We know that nothing within the loop
6570                        can modify it, or we would have invalidated it in
6571                        the hash table.  */
6572                     rtx q;
6573                     rtx cse_check_loop_start_value = SET_SRC (x);
6574                     for (q = p; q != loop_start; q = NEXT_INSN (q))
6575                       if (GET_RTX_CLASS (GET_CODE (q)) == 'i')
6576                         note_stores (PATTERN (q),
6577                                      cse_check_loop_start,
6578                                      &cse_check_loop_start_value);
6579
6580                     /* If nothing was changed and we can replace our
6581                        SET_SRC, add an insn after P to copy its destination
6582                        to what we will be replacing SET_SRC with.  */
6583                     if (cse_check_loop_start_value
6584                         && validate_change (insn, &SET_SRC (x),
6585                                             src_elt->exp, 0))
6586                       {
6587                         /* If this creates new pseudos, this is unsafe,
6588                            because the regno of new pseudo is unsuitable
6589                            to index into reg_qty when cse_insn processes
6590                            the new insn.  Therefore, if a new pseudo was
6591                            created, discard this optimization.  */
6592                         int nregs = max_reg_num ();
6593                         rtx move
6594                           = gen_move_insn (src_elt->exp, SET_DEST (set));
6595                         if (nregs != max_reg_num ())
6596                           {
6597                             if (! validate_change (insn, &SET_SRC (x),
6598                                                    SET_SRC (set), 0))
6599                               abort ();
6600                           }
6601                         else
6602                           emit_insn_after (move, p);
6603                       }
6604                     break;
6605                   }
6606             }
6607     }
6608
6609 #ifdef AUTO_INC_DEC
6610   /* Deal with the destination of X affecting the stack pointer.  */
6611   addr_affects_sp_p (SET_DEST (x));
6612 #endif
6613
6614   /* See comment on similar code in cse_insn for explanation of these
6615      tests.  */
6616   if (GET_CODE (SET_DEST (x)) == REG || GET_CODE (SET_DEST (x)) == SUBREG
6617       || GET_CODE (SET_DEST (x)) == MEM)
6618     invalidate (SET_DEST (x), VOIDmode);
6619   else if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6620            || GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
6621     invalidate (XEXP (SET_DEST (x), 0), GET_MODE (SET_DEST (x)));
6622 }
6623 \f
6624 /* Find the end of INSN's basic block and return its range,
6625    the total number of SETs in all the insns of the block, the last insn of the
6626    block, and the branch path.
6627
6628    The branch path indicates which branches should be followed.  If a non-zero
6629    path size is specified, the block should be rescanned and a different set
6630    of branches will be taken.  The branch path is only used if
6631    FLAG_CSE_FOLLOW_JUMPS or FLAG_CSE_SKIP_BLOCKS is non-zero.
6632
6633    DATA is a pointer to a struct cse_basic_block_data, defined below, that is
6634    used to describe the block.  It is filled in with the information about
6635    the current block.  The incoming structure's branch path, if any, is used
6636    to construct the output branch path.  */
6637
6638 void
6639 cse_end_of_basic_block (insn, data, follow_jumps, after_loop, skip_blocks)
6640      rtx insn;
6641      struct cse_basic_block_data *data;
6642      int follow_jumps;
6643      int after_loop;
6644      int skip_blocks;
6645 {
6646   rtx p = insn, q;
6647   int nsets = 0;
6648   int low_cuid = INSN_CUID (insn), high_cuid = INSN_CUID (insn);
6649   rtx next = GET_RTX_CLASS (GET_CODE (insn)) == 'i' ? insn : next_real_insn (insn);
6650   int path_size = data->path_size;
6651   int path_entry = 0;
6652   int i;
6653
6654   /* Update the previous branch path, if any.  If the last branch was
6655      previously TAKEN, mark it NOT_TAKEN.  If it was previously NOT_TAKEN,
6656      shorten the path by one and look at the previous branch.  We know that
6657      at least one branch must have been taken if PATH_SIZE is non-zero.  */
6658   while (path_size > 0)
6659     {
6660       if (data->path[path_size - 1].status != NOT_TAKEN)
6661         {
6662           data->path[path_size - 1].status = NOT_TAKEN;
6663           break;
6664         }
6665       else
6666         path_size--;
6667     }
6668
6669   /* If the first instruction is marked with QImode, that means we've
6670      already processed this block.  Our caller will look at DATA->LAST
6671      to figure out where to go next.  We want to return the next block
6672      in the instruction stream, not some branched-to block somewhere
6673      else.  We accomplish this by pretending our called forbid us to
6674      follow jumps, or skip blocks.  */
6675   if (GET_MODE (insn) == QImode)
6676     follow_jumps = skip_blocks = 0;
6677
6678   /* Scan to end of this basic block.  */
6679   while (p && GET_CODE (p) != CODE_LABEL)
6680     {
6681       /* Don't cse out the end of a loop.  This makes a difference
6682          only for the unusual loops that always execute at least once;
6683          all other loops have labels there so we will stop in any case.
6684          Cse'ing out the end of the loop is dangerous because it
6685          might cause an invariant expression inside the loop
6686          to be reused after the end of the loop.  This would make it
6687          hard to move the expression out of the loop in loop.c,
6688          especially if it is one of several equivalent expressions
6689          and loop.c would like to eliminate it.
6690
6691          If we are running after loop.c has finished, we can ignore
6692          the NOTE_INSN_LOOP_END.  */
6693
6694       if (! after_loop && GET_CODE (p) == NOTE
6695           && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
6696         break;
6697
6698       /* Don't cse over a call to setjmp; on some machines (eg vax)
6699          the regs restored by the longjmp come from
6700          a later time than the setjmp.  */
6701       if (GET_CODE (p) == NOTE
6702           && NOTE_LINE_NUMBER (p) == NOTE_INSN_SETJMP)
6703         break;
6704
6705       /* A PARALLEL can have lots of SETs in it,
6706          especially if it is really an ASM_OPERANDS.  */
6707       if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
6708           && GET_CODE (PATTERN (p)) == PARALLEL)
6709         nsets += XVECLEN (PATTERN (p), 0);
6710       else if (GET_CODE (p) != NOTE)
6711         nsets += 1;
6712         
6713       /* Ignore insns made by CSE; they cannot affect the boundaries of
6714          the basic block.  */
6715
6716       if (INSN_UID (p) <= max_uid && INSN_CUID (p) > high_cuid)
6717         high_cuid = INSN_CUID (p);
6718       if (INSN_UID (p) <= max_uid && INSN_CUID (p) < low_cuid)
6719         low_cuid = INSN_CUID (p);
6720
6721       /* See if this insn is in our branch path.  If it is and we are to
6722          take it, do so.  */
6723       if (path_entry < path_size && data->path[path_entry].branch == p)
6724         {
6725           if (data->path[path_entry].status != NOT_TAKEN)
6726             p = JUMP_LABEL (p);
6727           
6728           /* Point to next entry in path, if any.  */
6729           path_entry++;
6730         }
6731
6732       /* If this is a conditional jump, we can follow it if -fcse-follow-jumps
6733          was specified, we haven't reached our maximum path length, there are
6734          insns following the target of the jump, this is the only use of the
6735          jump label, and the target label is preceded by a BARRIER.
6736
6737          Alternatively, we can follow the jump if it branches around a
6738          block of code and there are no other branches into the block.
6739          In this case invalidate_skipped_block will be called to invalidate any
6740          registers set in the block when following the jump.  */
6741
6742       else if ((follow_jumps || skip_blocks) && path_size < PATHLENGTH - 1
6743                && GET_CODE (p) == JUMP_INSN
6744                && GET_CODE (PATTERN (p)) == SET
6745                && GET_CODE (SET_SRC (PATTERN (p))) == IF_THEN_ELSE
6746                && JUMP_LABEL (p) != 0
6747                && LABEL_NUSES (JUMP_LABEL (p)) == 1
6748                && NEXT_INSN (JUMP_LABEL (p)) != 0)
6749         {
6750           for (q = PREV_INSN (JUMP_LABEL (p)); q; q = PREV_INSN (q))
6751             if ((GET_CODE (q) != NOTE
6752                  || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END
6753                  || NOTE_LINE_NUMBER (q) == NOTE_INSN_SETJMP)
6754                 && (GET_CODE (q) != CODE_LABEL || LABEL_NUSES (q) != 0))
6755               break;
6756
6757           /* If we ran into a BARRIER, this code is an extension of the
6758              basic block when the branch is taken.  */
6759           if (follow_jumps && q != 0 && GET_CODE (q) == BARRIER)
6760             {
6761               /* Don't allow ourself to keep walking around an
6762                  always-executed loop.  */
6763               if (next_real_insn (q) == next)
6764                 {
6765                   p = NEXT_INSN (p);
6766                   continue;
6767                 }
6768
6769               /* Similarly, don't put a branch in our path more than once.  */
6770               for (i = 0; i < path_entry; i++)
6771                 if (data->path[i].branch == p)
6772                   break;
6773
6774               if (i != path_entry)
6775                 break;
6776
6777               data->path[path_entry].branch = p;
6778               data->path[path_entry++].status = TAKEN;
6779
6780               /* This branch now ends our path.  It was possible that we
6781                  didn't see this branch the last time around (when the
6782                  insn in front of the target was a JUMP_INSN that was
6783                  turned into a no-op).  */
6784               path_size = path_entry;
6785
6786               p = JUMP_LABEL (p);
6787               /* Mark block so we won't scan it again later.  */
6788               PUT_MODE (NEXT_INSN (p), QImode);
6789             }
6790           /* Detect a branch around a block of code.  */
6791           else if (skip_blocks && q != 0 && GET_CODE (q) != CODE_LABEL)
6792             {
6793               register rtx tmp;
6794
6795               if (next_real_insn (q) == next)
6796                 {
6797                   p = NEXT_INSN (p);
6798                   continue;
6799                 }
6800
6801               for (i = 0; i < path_entry; i++)
6802                 if (data->path[i].branch == p)
6803                   break;
6804
6805               if (i != path_entry)
6806                 break;
6807
6808               /* This is no_labels_between_p (p, q) with an added check for
6809                  reaching the end of a function (in case Q precedes P).  */
6810               for (tmp = NEXT_INSN (p); tmp && tmp != q; tmp = NEXT_INSN (tmp))
6811                 if (GET_CODE (tmp) == CODE_LABEL)
6812                   break;
6813               
6814               if (tmp == q)
6815                 {
6816                   data->path[path_entry].branch = p;
6817                   data->path[path_entry++].status = AROUND;
6818
6819                   path_size = path_entry;
6820
6821                   p = JUMP_LABEL (p);
6822                   /* Mark block so we won't scan it again later.  */
6823                   PUT_MODE (NEXT_INSN (p), QImode);
6824                 }
6825             }
6826         }
6827       p = NEXT_INSN (p);
6828     }
6829
6830   data->low_cuid = low_cuid;
6831   data->high_cuid = high_cuid;
6832   data->nsets = nsets;
6833   data->last = p;
6834
6835   /* If all jumps in the path are not taken, set our path length to zero
6836      so a rescan won't be done.  */
6837   for (i = path_size - 1; i >= 0; i--)
6838     if (data->path[i].status != NOT_TAKEN)
6839       break;
6840
6841   if (i == -1)
6842     data->path_size = 0;
6843   else
6844     data->path_size = path_size;
6845
6846   /* End the current branch path.  */
6847   data->path[path_size].branch = 0;
6848 }
6849 \f
6850 /* Perform cse on the instructions of a function.
6851    F is the first instruction.
6852    NREGS is one plus the highest pseudo-reg number used in the instruction.
6853
6854    AFTER_LOOP is 1 if this is the cse call done after loop optimization
6855    (only if -frerun-cse-after-loop).
6856
6857    Returns 1 if jump_optimize should be redone due to simplifications
6858    in conditional jump instructions.  */
6859
6860 int
6861 cse_main (f, nregs, after_loop, file)
6862      rtx f;
6863      int nregs;
6864      int after_loop;
6865      FILE *file;
6866 {
6867   struct cse_basic_block_data val;
6868   register rtx insn = f;
6869   register int i;
6870
6871   cse_jumps_altered = 0;
6872   recorded_label_ref = 0;
6873   constant_pool_entries_cost = 0;
6874   val.path_size = 0;
6875
6876   init_recog ();
6877   init_alias_analysis ();
6878
6879   max_reg = nregs;
6880
6881   max_insn_uid = get_max_uid ();
6882
6883   reg_next_eqv = (int *) xmalloc (nregs * sizeof (int));
6884   reg_prev_eqv = (int *) xmalloc (nregs * sizeof (int));
6885
6886 #ifdef LOAD_EXTEND_OP
6887
6888   /* Allocate scratch rtl here.  cse_insn will fill in the memory reference
6889      and change the code and mode as appropriate.  */
6890   memory_extend_rtx = gen_rtx_ZERO_EXTEND (VOIDmode, NULL_RTX);
6891 #endif
6892
6893   /* Discard all the free elements of the previous function
6894      since they are allocated in the temporarily obstack.  */
6895   bzero ((char *) table, sizeof table);
6896   free_element_chain = 0;
6897   n_elements_made = 0;
6898
6899   /* Find the largest uid.  */
6900
6901   max_uid = get_max_uid ();
6902   uid_cuid = (int *) xcalloc (max_uid + 1, sizeof (int));
6903
6904   /* Compute the mapping from uids to cuids.
6905      CUIDs are numbers assigned to insns, like uids,
6906      except that cuids increase monotonically through the code.
6907      Don't assign cuids to line-number NOTEs, so that the distance in cuids
6908      between two insns is not affected by -g.  */
6909
6910   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
6911     {
6912       if (GET_CODE (insn) != NOTE
6913           || NOTE_LINE_NUMBER (insn) < 0)
6914         INSN_CUID (insn) = ++i;
6915       else
6916         /* Give a line number note the same cuid as preceding insn.  */
6917         INSN_CUID (insn) = i;
6918     }
6919
6920   /* Initialize which registers are clobbered by calls.  */
6921
6922   CLEAR_HARD_REG_SET (regs_invalidated_by_call);
6923
6924   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
6925     if ((call_used_regs[i]
6926          /* Used to check !fixed_regs[i] here, but that isn't safe;
6927             fixed regs are still call-clobbered, and sched can get
6928             confused if they can "live across calls".
6929
6930             The frame pointer is always preserved across calls.  The arg
6931             pointer is if it is fixed.  The stack pointer usually is, unless
6932             RETURN_POPS_ARGS, in which case an explicit CLOBBER
6933             will be present.  If we are generating PIC code, the PIC offset
6934             table register is preserved across calls.  */
6935
6936          && i != STACK_POINTER_REGNUM
6937          && i != FRAME_POINTER_REGNUM
6938 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
6939          && i != HARD_FRAME_POINTER_REGNUM
6940 #endif
6941 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
6942          && ! (i == ARG_POINTER_REGNUM && fixed_regs[i])
6943 #endif
6944 #if defined (PIC_OFFSET_TABLE_REGNUM) && !defined (PIC_OFFSET_TABLE_REG_CALL_CLOBBERED)
6945          && ! (i == PIC_OFFSET_TABLE_REGNUM && flag_pic)
6946 #endif
6947          )
6948         || global_regs[i])
6949       SET_HARD_REG_BIT (regs_invalidated_by_call, i);
6950
6951   if (ggc_p)
6952     ggc_push_context ();
6953
6954   /* Loop over basic blocks.
6955      Compute the maximum number of qty's needed for each basic block
6956      (which is 2 for each SET).  */
6957   insn = f;
6958   while (insn)
6959     {
6960       cse_end_of_basic_block (insn, &val, flag_cse_follow_jumps, after_loop,
6961                               flag_cse_skip_blocks);
6962
6963       /* If this basic block was already processed or has no sets, skip it.  */
6964       if (val.nsets == 0 || GET_MODE (insn) == QImode)
6965         {
6966           PUT_MODE (insn, VOIDmode);
6967           insn = (val.last ? NEXT_INSN (val.last) : 0);
6968           val.path_size = 0;
6969           continue;
6970         }
6971
6972       cse_basic_block_start = val.low_cuid;
6973       cse_basic_block_end = val.high_cuid;
6974       max_qty = val.nsets * 2;
6975       
6976       if (file)
6977         fnotice (file, ";; Processing block from %d to %d, %d sets.\n",
6978                  INSN_UID (insn), val.last ? INSN_UID (val.last) : 0,
6979                  val.nsets);
6980
6981       /* Make MAX_QTY bigger to give us room to optimize
6982          past the end of this basic block, if that should prove useful.  */
6983       if (max_qty < 500)
6984         max_qty = 500;
6985
6986       max_qty += max_reg;
6987
6988       /* If this basic block is being extended by following certain jumps,
6989          (see `cse_end_of_basic_block'), we reprocess the code from the start.
6990          Otherwise, we start after this basic block.  */
6991       if (val.path_size > 0)
6992         cse_basic_block (insn, val.last, val.path, 0);
6993       else
6994         {
6995           int old_cse_jumps_altered = cse_jumps_altered;
6996           rtx temp;
6997
6998           /* When cse changes a conditional jump to an unconditional
6999              jump, we want to reprocess the block, since it will give
7000              us a new branch path to investigate.  */
7001           cse_jumps_altered = 0;
7002           temp = cse_basic_block (insn, val.last, val.path, ! after_loop);
7003           if (cse_jumps_altered == 0
7004               || (flag_cse_follow_jumps == 0 && flag_cse_skip_blocks == 0))
7005             insn = temp;
7006
7007           cse_jumps_altered |= old_cse_jumps_altered;
7008         }
7009
7010       if (ggc_p)
7011         ggc_collect ();
7012
7013 #ifdef USE_C_ALLOCA
7014       alloca (0);
7015 #endif
7016     }
7017
7018   if (ggc_p)
7019     ggc_pop_context ();
7020
7021   /* Tell refers_to_mem_p that qty_const info is not available.  */
7022   qty_const = 0;
7023
7024   if (max_elements_made < n_elements_made)
7025     max_elements_made = n_elements_made;
7026
7027   /* Clean up.  */
7028   end_alias_analysis ();
7029   free (uid_cuid);
7030   free (reg_next_eqv);
7031   free (reg_prev_eqv);
7032
7033   return cse_jumps_altered || recorded_label_ref;
7034 }
7035
7036 /* Process a single basic block.  FROM and TO and the limits of the basic
7037    block.  NEXT_BRANCH points to the branch path when following jumps or
7038    a null path when not following jumps.
7039
7040    AROUND_LOOP is non-zero if we are to try to cse around to the start of a
7041    loop.  This is true when we are being called for the last time on a
7042    block and this CSE pass is before loop.c.  */
7043
7044 static rtx
7045 cse_basic_block (from, to, next_branch, around_loop)
7046      register rtx from, to;
7047      struct branch_path *next_branch;
7048      int around_loop;
7049 {
7050   register rtx insn;
7051   int to_usage = 0;
7052   rtx libcall_insn = NULL_RTX;
7053   int num_insns = 0;
7054
7055   /* Each of these arrays is undefined before max_reg, so only allocate
7056      the space actually needed and adjust the start below.  */
7057
7058   qty_first_reg = (int *) xmalloc ((max_qty - max_reg) * sizeof (int));
7059   qty_last_reg = (int *) xmalloc ((max_qty - max_reg) * sizeof (int));
7060   qty_mode = (enum machine_mode *) xmalloc ((max_qty - max_reg)
7061                                            * sizeof (enum machine_mode));
7062   qty_const = (rtx *) xmalloc ((max_qty - max_reg) * sizeof (rtx));
7063   qty_const_insn = (rtx *) xmalloc ((max_qty - max_reg) * sizeof (rtx));
7064   qty_comparison_code
7065     = (enum rtx_code *) xmalloc ((max_qty - max_reg) * sizeof (enum rtx_code));
7066   qty_comparison_qty = (int *) xmalloc ((max_qty - max_reg) * sizeof (int));
7067   qty_comparison_const = (rtx *) xmalloc ((max_qty - max_reg) * sizeof (rtx));
7068
7069   qty_first_reg -= max_reg;
7070   qty_last_reg -= max_reg;
7071   qty_mode -= max_reg;
7072   qty_const -= max_reg;
7073   qty_const_insn -= max_reg;
7074   qty_comparison_code -= max_reg;
7075   qty_comparison_qty -= max_reg;
7076   qty_comparison_const -= max_reg;
7077
7078   new_basic_block ();
7079
7080   /* TO might be a label.  If so, protect it from being deleted.  */
7081   if (to != 0 && GET_CODE (to) == CODE_LABEL)
7082     ++LABEL_NUSES (to);
7083
7084   for (insn = from; insn != to; insn = NEXT_INSN (insn))
7085     {
7086       register enum rtx_code code = GET_CODE (insn);
7087
7088       /* If we have processed 1,000 insns, flush the hash table to
7089          avoid extreme quadratic behavior.  We must not include NOTEs
7090          in the count since there may be more or them when generating
7091          debugging information.  If we clear the table at different
7092          times, code generated with -g -O might be different than code
7093          generated with -O but not -g.
7094
7095          ??? This is a real kludge and needs to be done some other way.
7096          Perhaps for 2.9.  */
7097       if (code != NOTE && num_insns++ > 1000)
7098         {
7099           flush_hash_table ();
7100           num_insns = 0;
7101         }
7102
7103       /* See if this is a branch that is part of the path.  If so, and it is
7104          to be taken, do so.  */
7105       if (next_branch->branch == insn)
7106         {
7107           enum taken status = next_branch++->status;
7108           if (status != NOT_TAKEN)
7109             {
7110               if (status == TAKEN)
7111                 record_jump_equiv (insn, 1);
7112               else
7113                 invalidate_skipped_block (NEXT_INSN (insn));
7114
7115               /* Set the last insn as the jump insn; it doesn't affect cc0.
7116                  Then follow this branch.  */
7117 #ifdef HAVE_cc0
7118               prev_insn_cc0 = 0;
7119 #endif
7120               prev_insn = insn;
7121               insn = JUMP_LABEL (insn);
7122               continue;
7123             }
7124         }
7125         
7126       if (GET_MODE (insn) == QImode)
7127         PUT_MODE (insn, VOIDmode);
7128
7129       if (GET_RTX_CLASS (code) == 'i')
7130         {
7131           rtx p;
7132
7133           /* Process notes first so we have all notes in canonical forms when
7134              looking for duplicate operations.  */
7135
7136           if (REG_NOTES (insn))
7137             REG_NOTES (insn) = cse_process_notes (REG_NOTES (insn), NULL_RTX);
7138
7139           /* Track when we are inside in LIBCALL block.  Inside such a block,
7140              we do not want to record destinations.  The last insn of a
7141              LIBCALL block is not considered to be part of the block, since
7142              its destination is the result of the block and hence should be
7143              recorded.  */
7144
7145           if ((p = find_reg_note (insn, REG_LIBCALL, NULL_RTX)))
7146             libcall_insn = XEXP (p, 0);
7147           else if (find_reg_note (insn, REG_RETVAL, NULL_RTX))
7148             libcall_insn = NULL_RTX;
7149
7150           cse_insn (insn, libcall_insn);
7151         }
7152
7153       /* If INSN is now an unconditional jump, skip to the end of our
7154          basic block by pretending that we just did the last insn in the
7155          basic block.  If we are jumping to the end of our block, show
7156          that we can have one usage of TO.  */
7157
7158       if (simplejump_p (insn))
7159         {
7160           if (to == 0)
7161             return 0;
7162
7163           if (JUMP_LABEL (insn) == to)
7164             to_usage = 1;
7165
7166           /* Maybe TO was deleted because the jump is unconditional.
7167              If so, there is nothing left in this basic block.  */
7168           /* ??? Perhaps it would be smarter to set TO
7169              to whatever follows this insn, 
7170              and pretend the basic block had always ended here.  */
7171           if (INSN_DELETED_P (to))
7172             break;
7173
7174           insn = PREV_INSN (to);
7175         }
7176
7177       /* See if it is ok to keep on going past the label
7178          which used to end our basic block.  Remember that we incremented
7179          the count of that label, so we decrement it here.  If we made
7180          a jump unconditional, TO_USAGE will be one; in that case, we don't
7181          want to count the use in that jump.  */
7182
7183       if (to != 0 && NEXT_INSN (insn) == to
7184           && GET_CODE (to) == CODE_LABEL && --LABEL_NUSES (to) == to_usage)
7185         {
7186           struct cse_basic_block_data val;
7187           rtx prev;
7188
7189           insn = NEXT_INSN (to);
7190
7191           /* If TO was the last insn in the function, we are done.  */
7192           if (insn == 0)
7193             return 0;
7194
7195           /* If TO was preceded by a BARRIER we are done with this block
7196              because it has no continuation.  */
7197           prev = prev_nonnote_insn (to);
7198           if (prev && GET_CODE (prev) == BARRIER)
7199             return insn;
7200
7201           /* Find the end of the following block.  Note that we won't be
7202              following branches in this case.  */
7203           to_usage = 0;
7204           val.path_size = 0;
7205           cse_end_of_basic_block (insn, &val, 0, 0, 0);
7206
7207           /* If the tables we allocated have enough space left
7208              to handle all the SETs in the next basic block,
7209              continue through it.  Otherwise, return,
7210              and that block will be scanned individually.  */
7211           if (val.nsets * 2 + next_qty > max_qty)
7212             break;
7213
7214           cse_basic_block_start = val.low_cuid;
7215           cse_basic_block_end = val.high_cuid;
7216           to = val.last;
7217
7218           /* Prevent TO from being deleted if it is a label.  */
7219           if (to != 0 && GET_CODE (to) == CODE_LABEL)
7220             ++LABEL_NUSES (to);
7221
7222           /* Back up so we process the first insn in the extension.  */
7223           insn = PREV_INSN (insn);
7224         }
7225     }
7226
7227   if (next_qty > max_qty)
7228     abort ();
7229
7230   /* If we are running before loop.c, we stopped on a NOTE_INSN_LOOP_END, and
7231      the previous insn is the only insn that branches to the head of a loop,
7232      we can cse into the loop.  Don't do this if we changed the jump
7233      structure of a loop unless we aren't going to be following jumps.  */
7234
7235   if ((cse_jumps_altered == 0
7236        || (flag_cse_follow_jumps == 0 && flag_cse_skip_blocks == 0))
7237       && around_loop && to != 0
7238       && GET_CODE (to) == NOTE && NOTE_LINE_NUMBER (to) == NOTE_INSN_LOOP_END
7239       && GET_CODE (PREV_INSN (to)) == JUMP_INSN
7240       && JUMP_LABEL (PREV_INSN (to)) != 0
7241       && LABEL_NUSES (JUMP_LABEL (PREV_INSN (to))) == 1)
7242     cse_around_loop (JUMP_LABEL (PREV_INSN (to)));
7243
7244   free (qty_first_reg + max_reg);
7245   free (qty_last_reg + max_reg);
7246   free (qty_mode + max_reg);
7247   free (qty_const + max_reg);
7248   free (qty_const_insn + max_reg);
7249   free (qty_comparison_code + max_reg);
7250   free (qty_comparison_qty + max_reg);
7251   free (qty_comparison_const + max_reg);
7252
7253   return to ? NEXT_INSN (to) : 0;
7254 }
7255 \f
7256 /* Count the number of times registers are used (not set) in X.
7257    COUNTS is an array in which we accumulate the count, INCR is how much
7258    we count each register usage.  
7259
7260    Don't count a usage of DEST, which is the SET_DEST of a SET which 
7261    contains X in its SET_SRC.  This is because such a SET does not
7262    modify the liveness of DEST.  */
7263
7264 static void
7265 count_reg_usage (x, counts, dest, incr)
7266      rtx x;
7267      int *counts;
7268      rtx dest;
7269      int incr;
7270 {
7271   enum rtx_code code;
7272   const char *fmt;
7273   int i, j;
7274
7275   if (x == 0)
7276     return;
7277
7278   switch (code = GET_CODE (x))
7279     {
7280     case REG:
7281       if (x != dest)
7282         counts[REGNO (x)] += incr;
7283       return;
7284
7285     case PC:
7286     case CC0:
7287     case CONST:
7288     case CONST_INT:
7289     case CONST_DOUBLE:
7290     case SYMBOL_REF:
7291     case LABEL_REF:
7292       return;
7293
7294     case CLOBBER:                                                        
7295       /* If we are clobbering a MEM, mark any registers inside the address
7296          as being used.  */
7297       if (GET_CODE (XEXP (x, 0)) == MEM)
7298         count_reg_usage (XEXP (XEXP (x, 0), 0), counts, NULL_RTX, incr);
7299       return;
7300
7301     case SET:
7302       /* Unless we are setting a REG, count everything in SET_DEST.  */
7303       if (GET_CODE (SET_DEST (x)) != REG)
7304         count_reg_usage (SET_DEST (x), counts, NULL_RTX, incr);
7305
7306       /* If SRC has side-effects, then we can't delete this insn, so the
7307          usage of SET_DEST inside SRC counts.
7308
7309          ??? Strictly-speaking, we might be preserving this insn
7310          because some other SET has side-effects, but that's hard
7311          to do and can't happen now.  */
7312       count_reg_usage (SET_SRC (x), counts,
7313                        side_effects_p (SET_SRC (x)) ? NULL_RTX : SET_DEST (x),
7314                        incr);
7315       return;
7316
7317     case CALL_INSN:
7318       count_reg_usage (CALL_INSN_FUNCTION_USAGE (x), counts, NULL_RTX, incr);
7319
7320       /* ... falls through ...  */
7321     case INSN:
7322     case JUMP_INSN:
7323       count_reg_usage (PATTERN (x), counts, NULL_RTX, incr);
7324
7325       /* Things used in a REG_EQUAL note aren't dead since loop may try to
7326          use them.  */
7327
7328       count_reg_usage (REG_NOTES (x), counts, NULL_RTX, incr);
7329       return;
7330
7331     case EXPR_LIST:
7332     case INSN_LIST:
7333       if (REG_NOTE_KIND (x) == REG_EQUAL
7334           || (REG_NOTE_KIND (x) != REG_NONNEG && GET_CODE (XEXP (x,0)) == USE))
7335         count_reg_usage (XEXP (x, 0), counts, NULL_RTX, incr);
7336       count_reg_usage (XEXP (x, 1), counts, NULL_RTX, incr);
7337       return;
7338       
7339     default:
7340       break;
7341     }
7342
7343   fmt = GET_RTX_FORMAT (code);
7344   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7345     {
7346       if (fmt[i] == 'e')
7347         count_reg_usage (XEXP (x, i), counts, dest, incr);
7348       else if (fmt[i] == 'E')
7349         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7350           count_reg_usage (XVECEXP (x, i, j), counts, dest, incr);
7351     }
7352 }
7353 \f
7354 /* Scan all the insns and delete any that are dead; i.e., they store a register
7355    that is never used or they copy a register to itself.
7356
7357    This is used to remove insns made obviously dead by cse, loop or other
7358    optimizations.  It improves the heuristics in loop since it won't try to
7359    move dead invariants out of loops or make givs for dead quantities.  The
7360    remaining passes of the compilation are also sped up.  */
7361
7362 void
7363 delete_trivially_dead_insns (insns, nreg)
7364      rtx insns;
7365      int nreg;
7366 {
7367   int *counts;
7368   rtx insn, prev;
7369 #ifdef HAVE_cc0
7370   rtx tem;
7371 #endif
7372   int i;
7373   int in_libcall = 0, dead_libcall = 0;
7374
7375   /* First count the number of times each register is used.  */
7376   counts = (int *) xcalloc (nreg, sizeof (int));
7377   for (insn = next_real_insn (insns); insn; insn = next_real_insn (insn))
7378     count_reg_usage (insn, counts, NULL_RTX, 1);
7379
7380   /* Go from the last insn to the first and delete insns that only set unused
7381      registers or copy a register to itself.  As we delete an insn, remove
7382      usage counts for registers it uses. 
7383
7384      The first jump optimization pass may leave a real insn as the last
7385      insn in the function.   We must not skip that insn or we may end
7386      up deleting code that is not really dead.   */
7387   insn = get_last_insn ();
7388   if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
7389     insn = prev_real_insn (insn);
7390
7391   for ( ; insn; insn = prev)
7392     {
7393       int live_insn = 0;
7394       rtx note;
7395
7396       prev = prev_real_insn (insn);
7397
7398       /* Don't delete any insns that are part of a libcall block unless
7399          we can delete the whole libcall block.
7400
7401          Flow or loop might get confused if we did that.  Remember
7402          that we are scanning backwards.  */
7403       if (find_reg_note (insn, REG_RETVAL, NULL_RTX))
7404         {
7405           in_libcall = 1;
7406           live_insn = 1;
7407           dead_libcall = 0;
7408
7409           /* See if there's a REG_EQUAL note on this insn and try to
7410              replace the source with the REG_EQUAL expression.
7411         
7412              We assume that insns with REG_RETVALs can only be reg->reg
7413              copies at this point.  */
7414           note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
7415           if (note)
7416             {
7417               rtx set = single_set (insn);
7418               rtx new = simplify_rtx (XEXP (note, 0));
7419
7420               if (!new)
7421                 new = XEXP (note, 0);
7422
7423               if (set && validate_change (insn, &SET_SRC (set), new, 0))
7424                 {
7425                   remove_note (insn,
7426                                find_reg_note (insn, REG_RETVAL, NULL_RTX));
7427                   dead_libcall = 1;
7428                 }
7429             }
7430         }
7431       else if (in_libcall)
7432         live_insn = ! dead_libcall;
7433       else if (GET_CODE (PATTERN (insn)) == SET)
7434         {
7435           if (GET_CODE (SET_DEST (PATTERN (insn))) == REG
7436               && SET_DEST (PATTERN (insn)) == SET_SRC (PATTERN (insn)))
7437             ;
7438
7439 #ifdef HAVE_cc0
7440           else if (GET_CODE (SET_DEST (PATTERN (insn))) == CC0
7441                    && ! side_effects_p (SET_SRC (PATTERN (insn)))
7442                    && ((tem = next_nonnote_insn (insn)) == 0
7443                        || GET_RTX_CLASS (GET_CODE (tem)) != 'i'
7444                        || ! reg_referenced_p (cc0_rtx, PATTERN (tem))))
7445             ;
7446 #endif
7447           else if (GET_CODE (SET_DEST (PATTERN (insn))) != REG
7448                    || REGNO (SET_DEST (PATTERN (insn))) < FIRST_PSEUDO_REGISTER
7449                    || counts[REGNO (SET_DEST (PATTERN (insn)))] != 0
7450                    || side_effects_p (SET_SRC (PATTERN (insn)))
7451                    /* An ADDRESSOF expression can turn into a use of the
7452                       internal arg pointer, so always consider the
7453                       internal arg pointer live.  If it is truly dead,
7454                       flow will delete the initializing insn.  */
7455                    || (SET_DEST (PATTERN (insn))
7456                        == current_function_internal_arg_pointer))
7457             live_insn = 1;
7458         }
7459       else if (GET_CODE (PATTERN (insn)) == PARALLEL)
7460         for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
7461           {
7462             rtx elt = XVECEXP (PATTERN (insn), 0, i);
7463
7464             if (GET_CODE (elt) == SET)
7465               {
7466                 if (GET_CODE (SET_DEST (elt)) == REG
7467                     && SET_DEST (elt) == SET_SRC (elt))
7468                   ;
7469
7470 #ifdef HAVE_cc0
7471                 else if (GET_CODE (SET_DEST (elt)) == CC0
7472                          && ! side_effects_p (SET_SRC (elt))
7473                          && ((tem = next_nonnote_insn (insn)) == 0
7474                              || GET_RTX_CLASS (GET_CODE (tem)) != 'i'
7475                              || ! reg_referenced_p (cc0_rtx, PATTERN (tem))))
7476                   ;
7477 #endif
7478                 else if (GET_CODE (SET_DEST (elt)) != REG
7479                          || REGNO (SET_DEST (elt)) < FIRST_PSEUDO_REGISTER
7480                          || counts[REGNO (SET_DEST (elt))] != 0
7481                          || side_effects_p (SET_SRC (elt))
7482                          /* An ADDRESSOF expression can turn into a use of the
7483                             internal arg pointer, so always consider the
7484                             internal arg pointer live.  If it is truly dead,
7485                             flow will delete the initializing insn.  */
7486                          || (SET_DEST (elt)
7487                              == current_function_internal_arg_pointer))
7488                   live_insn = 1;
7489               }
7490             else if (GET_CODE (elt) != CLOBBER && GET_CODE (elt) != USE)
7491               live_insn = 1;
7492           }
7493       else
7494         live_insn = 1;
7495
7496       /* If this is a dead insn, delete it and show registers in it aren't
7497          being used.  */
7498
7499       if (! live_insn)
7500         {
7501           count_reg_usage (insn, counts, NULL_RTX, -1);
7502           delete_insn (insn);
7503         }
7504
7505       if (find_reg_note (insn, REG_LIBCALL, NULL_RTX))
7506         {
7507           in_libcall = 0;
7508           dead_libcall = 0;
7509         }
7510     }
7511
7512   /* Clean up.  */
7513   free (counts);
7514 }