OSDN Git Service

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