OSDN Git Service

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