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