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