OSDN Git Service

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