OSDN Git Service

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