re PR target/36634 (-msecure-plt combine gives invalid call insn)
[official-gcc.git] / gcc / cse.c
blob86c0f5408dac5c111d31edfd86c2f685384c73c7
1 /* Common subexpression elimination for GNU compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 /* stdio.h must precede rtl.h for FFS. */
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "hard-reg-set.h"
30 #include "regs.h"
31 #include "basic-block.h"
32 #include "flags.h"
33 #include "real.h"
34 #include "insn-config.h"
35 #include "recog.h"
36 #include "function.h"
37 #include "expr.h"
38 #include "toplev.h"
39 #include "output.h"
40 #include "ggc.h"
41 #include "timevar.h"
42 #include "except.h"
43 #include "target.h"
44 #include "params.h"
45 #include "rtlhooks-def.h"
46 #include "tree-pass.h"
47 #include "df.h"
48 #include "dbgcnt.h"
50 /* The basic idea of common subexpression elimination is to go
51 through the code, keeping a record of expressions that would
52 have the same value at the current scan point, and replacing
53 expressions encountered with the cheapest equivalent expression.
55 It is too complicated to keep track of the different possibilities
56 when control paths merge in this code; so, at each label, we forget all
57 that is known and start fresh. This can be described as processing each
58 extended basic block separately. We have a separate pass to perform
59 global CSE.
61 Note CSE can turn a conditional or computed jump into a nop or
62 an unconditional jump. When this occurs we arrange to run the jump
63 optimizer after CSE to delete the unreachable code.
65 We use two data structures to record the equivalent expressions:
66 a hash table for most expressions, and a vector of "quantity
67 numbers" to record equivalent (pseudo) registers.
69 The use of the special data structure for registers is desirable
70 because it is faster. It is possible because registers references
71 contain a fairly small number, the register number, taken from
72 a contiguously allocated series, and two register references are
73 identical if they have the same number. General expressions
74 do not have any such thing, so the only way to retrieve the
75 information recorded on an expression other than a register
76 is to keep it in a hash table.
78 Registers and "quantity numbers":
80 At the start of each basic block, all of the (hardware and pseudo)
81 registers used in the function are given distinct quantity
82 numbers to indicate their contents. During scan, when the code
83 copies one register into another, we copy the quantity number.
84 When a register is loaded in any other way, we allocate a new
85 quantity number to describe the value generated by this operation.
86 `REG_QTY (N)' records what quantity register N is currently thought
87 of as containing.
89 All real quantity numbers are greater than or equal to zero.
90 If register N has not been assigned a quantity, `REG_QTY (N)' will
91 equal -N - 1, which is always negative.
93 Quantity numbers below zero do not exist and none of the `qty_table'
94 entries should be referenced with a negative index.
96 We also maintain a bidirectional chain of registers for each
97 quantity number. The `qty_table` members `first_reg' and `last_reg',
98 and `reg_eqv_table' members `next' and `prev' hold these chains.
100 The first register in a chain is the one whose lifespan is least local.
101 Among equals, it is the one that was seen first.
102 We replace any equivalent register with that one.
104 If two registers have the same quantity number, it must be true that
105 REG expressions with qty_table `mode' must be in the hash table for both
106 registers and must be in the same class.
108 The converse is not true. Since hard registers may be referenced in
109 any mode, two REG expressions might be equivalent in the hash table
110 but not have the same quantity number if the quantity number of one
111 of the registers is not the same mode as those expressions.
113 Constants and quantity numbers
115 When a quantity has a known constant value, that value is stored
116 in the appropriate qty_table `const_rtx'. This is in addition to
117 putting the constant in the hash table as is usual for non-regs.
119 Whether a reg or a constant is preferred is determined by the configuration
120 macro CONST_COSTS and will often depend on the constant value. In any
121 event, expressions containing constants can be simplified, by fold_rtx.
123 When a quantity has a known nearly constant value (such as an address
124 of a stack slot), that value is stored in the appropriate qty_table
125 `const_rtx'.
127 Integer constants don't have a machine mode. However, cse
128 determines the intended machine mode from the destination
129 of the instruction that moves the constant. The machine mode
130 is recorded in the hash table along with the actual RTL
131 constant expression so that different modes are kept separate.
133 Other expressions:
135 To record known equivalences among expressions in general
136 we use a hash table called `table'. It has a fixed number of buckets
137 that contain chains of `struct table_elt' elements for expressions.
138 These chains connect the elements whose expressions have the same
139 hash codes.
141 Other chains through the same elements connect the elements which
142 currently have equivalent values.
144 Register references in an expression are canonicalized before hashing
145 the expression. This is done using `reg_qty' and qty_table `first_reg'.
146 The hash code of a register reference is computed using the quantity
147 number, not the register number.
149 When the value of an expression changes, it is necessary to remove from the
150 hash table not just that expression but all expressions whose values
151 could be different as a result.
153 1. If the value changing is in memory, except in special cases
154 ANYTHING referring to memory could be changed. That is because
155 nobody knows where a pointer does not point.
156 The function `invalidate_memory' removes what is necessary.
158 The special cases are when the address is constant or is
159 a constant plus a fixed register such as the frame pointer
160 or a static chain pointer. When such addresses are stored in,
161 we can tell exactly which other such addresses must be invalidated
162 due to overlap. `invalidate' does this.
163 All expressions that refer to non-constant
164 memory addresses are also invalidated. `invalidate_memory' does this.
166 2. If the value changing is a register, all expressions
167 containing references to that register, and only those,
168 must be removed.
170 Because searching the entire hash table for expressions that contain
171 a register is very slow, we try to figure out when it isn't necessary.
172 Precisely, this is necessary only when expressions have been
173 entered in the hash table using this register, and then the value has
174 changed, and then another expression wants to be added to refer to
175 the register's new value. This sequence of circumstances is rare
176 within any one basic block.
178 `REG_TICK' and `REG_IN_TABLE', accessors for members of
179 cse_reg_info, are used to detect this case. REG_TICK (i) is
180 incremented whenever a value is stored in register i.
181 REG_IN_TABLE (i) holds -1 if no references to register i have been
182 entered in the table; otherwise, it contains the value REG_TICK (i)
183 had when the references were entered. If we want to enter a
184 reference and REG_IN_TABLE (i) != REG_TICK (i), we must scan and
185 remove old references. Until we want to enter a new entry, the
186 mere fact that the two vectors don't match makes the entries be
187 ignored if anyone tries to match them.
189 Registers themselves are entered in the hash table as well as in
190 the equivalent-register chains. However, `REG_TICK' and
191 `REG_IN_TABLE' do not apply to expressions which are simple
192 register references. These expressions are removed from the table
193 immediately when they become invalid, and this can be done even if
194 we do not immediately search for all the expressions that refer to
195 the register.
197 A CLOBBER rtx in an instruction invalidates its operand for further
198 reuse. A CLOBBER or SET rtx whose operand is a MEM:BLK
199 invalidates everything that resides in memory.
201 Related expressions:
203 Constant expressions that differ only by an additive integer
204 are called related. When a constant expression is put in
205 the table, the related expression with no constant term
206 is also entered. These are made to point at each other
207 so that it is possible to find out if there exists any
208 register equivalent to an expression related to a given expression. */
210 /* Length of qty_table vector. We know in advance we will not need
211 a quantity number this big. */
213 static int max_qty;
215 /* Next quantity number to be allocated.
216 This is 1 + the largest number needed so far. */
218 static int next_qty;
220 /* Per-qty information tracking.
222 `first_reg' and `last_reg' track the head and tail of the
223 chain of registers which currently contain this quantity.
225 `mode' contains the machine mode of this quantity.
227 `const_rtx' holds the rtx of the constant value of this
228 quantity, if known. A summations of the frame/arg pointer
229 and a constant can also be entered here. When this holds
230 a known value, `const_insn' is the insn which stored the
231 constant value.
233 `comparison_{code,const,qty}' are used to track when a
234 comparison between a quantity and some constant or register has
235 been passed. In such a case, we know the results of the comparison
236 in case we see it again. These members record a comparison that
237 is known to be true. `comparison_code' holds the rtx code of such
238 a comparison, else it is set to UNKNOWN and the other two
239 comparison members are undefined. `comparison_const' holds
240 the constant being compared against, or zero if the comparison
241 is not against a constant. `comparison_qty' holds the quantity
242 being compared against when the result is known. If the comparison
243 is not with a register, `comparison_qty' is -1. */
245 struct qty_table_elem
247 rtx const_rtx;
248 rtx const_insn;
249 rtx comparison_const;
250 int comparison_qty;
251 unsigned int first_reg, last_reg;
252 /* The sizes of these fields should match the sizes of the
253 code and mode fields of struct rtx_def (see rtl.h). */
254 ENUM_BITFIELD(rtx_code) comparison_code : 16;
255 ENUM_BITFIELD(machine_mode) mode : 8;
258 /* The table of all qtys, indexed by qty number. */
259 static struct qty_table_elem *qty_table;
261 /* Structure used to pass arguments via for_each_rtx to function
262 cse_change_cc_mode. */
263 struct change_cc_mode_args
265 rtx insn;
266 rtx newreg;
269 #ifdef HAVE_cc0
270 /* For machines that have a CC0, we do not record its value in the hash
271 table since its use is guaranteed to be the insn immediately following
272 its definition and any other insn is presumed to invalidate it.
274 Instead, we store below the current and last value assigned to CC0.
275 If it should happen to be a constant, it is stored in preference
276 to the actual assigned value. In case it is a constant, we store
277 the mode in which the constant should be interpreted. */
279 static rtx this_insn_cc0, prev_insn_cc0;
280 static enum machine_mode this_insn_cc0_mode, prev_insn_cc0_mode;
281 #endif
283 /* Insn being scanned. */
285 static rtx this_insn;
287 /* Index by register number, gives the number of the next (or
288 previous) register in the chain of registers sharing the same
289 value.
291 Or -1 if this register is at the end of the chain.
293 If REG_QTY (N) == -N - 1, reg_eqv_table[N].next is undefined. */
295 /* Per-register equivalence chain. */
296 struct reg_eqv_elem
298 int next, prev;
301 /* The table of all register equivalence chains. */
302 static struct reg_eqv_elem *reg_eqv_table;
304 struct cse_reg_info
306 /* The timestamp at which this register is initialized. */
307 unsigned int timestamp;
309 /* The quantity number of the register's current contents. */
310 int reg_qty;
312 /* The number of times the register has been altered in the current
313 basic block. */
314 int reg_tick;
316 /* The REG_TICK value at which rtx's containing this register are
317 valid in the hash table. If this does not equal the current
318 reg_tick value, such expressions existing in the hash table are
319 invalid. */
320 int reg_in_table;
322 /* The SUBREG that was set when REG_TICK was last incremented. Set
323 to -1 if the last store was to the whole register, not a subreg. */
324 unsigned int subreg_ticked;
327 /* A table of cse_reg_info indexed by register numbers. */
328 static struct cse_reg_info *cse_reg_info_table;
330 /* The size of the above table. */
331 static unsigned int cse_reg_info_table_size;
333 /* The index of the first entry that has not been initialized. */
334 static unsigned int cse_reg_info_table_first_uninitialized;
336 /* The timestamp at the beginning of the current run of
337 cse_extended_basic_block. We increment this variable at the beginning of
338 the current run of cse_extended_basic_block. The timestamp field of a
339 cse_reg_info entry matches the value of this variable if and only
340 if the entry has been initialized during the current run of
341 cse_extended_basic_block. */
342 static unsigned int cse_reg_info_timestamp;
344 /* A HARD_REG_SET containing all the hard registers for which there is
345 currently a REG expression in the hash table. Note the difference
346 from the above variables, which indicate if the REG is mentioned in some
347 expression in the table. */
349 static HARD_REG_SET hard_regs_in_table;
351 /* True if CSE has altered the CFG. */
352 static bool cse_cfg_altered;
354 /* True if CSE has altered conditional jump insns in such a way
355 that jump optimization should be redone. */
356 static bool cse_jumps_altered;
358 /* True if we put a LABEL_REF into the hash table for an INSN
359 without a REG_LABEL_OPERAND, we have to rerun jump after CSE
360 to put in the note. */
361 static bool recorded_label_ref;
363 /* canon_hash stores 1 in do_not_record
364 if it notices a reference to CC0, PC, or some other volatile
365 subexpression. */
367 static int do_not_record;
369 /* canon_hash stores 1 in hash_arg_in_memory
370 if it notices a reference to memory within the expression being hashed. */
372 static int hash_arg_in_memory;
374 /* The hash table contains buckets which are chains of `struct table_elt's,
375 each recording one expression's information.
376 That expression is in the `exp' field.
378 The canon_exp field contains a canonical (from the point of view of
379 alias analysis) version of the `exp' field.
381 Those elements with the same hash code are chained in both directions
382 through the `next_same_hash' and `prev_same_hash' fields.
384 Each set of expressions with equivalent values
385 are on a two-way chain through the `next_same_value'
386 and `prev_same_value' fields, and all point with
387 the `first_same_value' field at the first element in
388 that chain. The chain is in order of increasing cost.
389 Each element's cost value is in its `cost' field.
391 The `in_memory' field is nonzero for elements that
392 involve any reference to memory. These elements are removed
393 whenever a write is done to an unidentified location in memory.
394 To be safe, we assume that a memory address is unidentified unless
395 the address is either a symbol constant or a constant plus
396 the frame pointer or argument pointer.
398 The `related_value' field is used to connect related expressions
399 (that differ by adding an integer).
400 The related expressions are chained in a circular fashion.
401 `related_value' is zero for expressions for which this
402 chain is not useful.
404 The `cost' field stores the cost of this element's expression.
405 The `regcost' field stores the value returned by approx_reg_cost for
406 this element's expression.
408 The `is_const' flag is set if the element is a constant (including
409 a fixed address).
411 The `flag' field is used as a temporary during some search routines.
413 The `mode' field is usually the same as GET_MODE (`exp'), but
414 if `exp' is a CONST_INT and has no machine mode then the `mode'
415 field is the mode it was being used as. Each constant is
416 recorded separately for each mode it is used with. */
418 struct table_elt
420 rtx exp;
421 rtx canon_exp;
422 struct table_elt *next_same_hash;
423 struct table_elt *prev_same_hash;
424 struct table_elt *next_same_value;
425 struct table_elt *prev_same_value;
426 struct table_elt *first_same_value;
427 struct table_elt *related_value;
428 int cost;
429 int regcost;
430 /* The size of this field should match the size
431 of the mode field of struct rtx_def (see rtl.h). */
432 ENUM_BITFIELD(machine_mode) mode : 8;
433 char in_memory;
434 char is_const;
435 char flag;
438 /* We don't want a lot of buckets, because we rarely have very many
439 things stored in the hash table, and a lot of buckets slows
440 down a lot of loops that happen frequently. */
441 #define HASH_SHIFT 5
442 #define HASH_SIZE (1 << HASH_SHIFT)
443 #define HASH_MASK (HASH_SIZE - 1)
445 /* Compute hash code of X in mode M. Special-case case where X is a pseudo
446 register (hard registers may require `do_not_record' to be set). */
448 #define HASH(X, M) \
449 ((REG_P (X) && REGNO (X) >= FIRST_PSEUDO_REGISTER \
450 ? (((unsigned) REG << 7) + (unsigned) REG_QTY (REGNO (X))) \
451 : canon_hash (X, M)) & HASH_MASK)
453 /* Like HASH, but without side-effects. */
454 #define SAFE_HASH(X, M) \
455 ((REG_P (X) && REGNO (X) >= FIRST_PSEUDO_REGISTER \
456 ? (((unsigned) REG << 7) + (unsigned) REG_QTY (REGNO (X))) \
457 : safe_hash (X, M)) & HASH_MASK)
459 /* Determine whether register number N is considered a fixed register for the
460 purpose of approximating register costs.
461 It is desirable to replace other regs with fixed regs, to reduce need for
462 non-fixed hard regs.
463 A reg wins if it is either the frame pointer or designated as fixed. */
464 #define FIXED_REGNO_P(N) \
465 ((N) == FRAME_POINTER_REGNUM || (N) == HARD_FRAME_POINTER_REGNUM \
466 || fixed_regs[N] || global_regs[N])
468 /* Compute cost of X, as stored in the `cost' field of a table_elt. Fixed
469 hard registers and pointers into the frame are the cheapest with a cost
470 of 0. Next come pseudos with a cost of one and other hard registers with
471 a cost of 2. Aside from these special cases, call `rtx_cost'. */
473 #define CHEAP_REGNO(N) \
474 (REGNO_PTR_FRAME_P(N) \
475 || (HARD_REGISTER_NUM_P (N) \
476 && FIXED_REGNO_P (N) && REGNO_REG_CLASS (N) != NO_REGS))
478 #define COST(X) (REG_P (X) ? 0 : notreg_cost (X, SET))
479 #define COST_IN(X,OUTER) (REG_P (X) ? 0 : notreg_cost (X, OUTER))
481 /* Get the number of times this register has been updated in this
482 basic block. */
484 #define REG_TICK(N) (get_cse_reg_info (N)->reg_tick)
486 /* Get the point at which REG was recorded in the table. */
488 #define REG_IN_TABLE(N) (get_cse_reg_info (N)->reg_in_table)
490 /* Get the SUBREG set at the last increment to REG_TICK (-1 if not a
491 SUBREG). */
493 #define SUBREG_TICKED(N) (get_cse_reg_info (N)->subreg_ticked)
495 /* Get the quantity number for REG. */
497 #define REG_QTY(N) (get_cse_reg_info (N)->reg_qty)
499 /* Determine if the quantity number for register X represents a valid index
500 into the qty_table. */
502 #define REGNO_QTY_VALID_P(N) (REG_QTY (N) >= 0)
504 static struct table_elt *table[HASH_SIZE];
506 /* Chain of `struct table_elt's made so far for this function
507 but currently removed from the table. */
509 static struct table_elt *free_element_chain;
511 /* Set to the cost of a constant pool reference if one was found for a
512 symbolic constant. If this was found, it means we should try to
513 convert constants into constant pool entries if they don't fit in
514 the insn. */
516 static int constant_pool_entries_cost;
517 static int constant_pool_entries_regcost;
519 /* This data describes a block that will be processed by
520 cse_extended_basic_block. */
522 struct cse_basic_block_data
524 /* Total number of SETs in block. */
525 int nsets;
526 /* Size of current branch path, if any. */
527 int path_size;
528 /* Current path, indicating which basic_blocks will be processed. */
529 struct branch_path
531 /* The basic block for this path entry. */
532 basic_block bb;
533 } *path;
537 /* Pointers to the live in/live out bitmaps for the boundaries of the
538 current EBB. */
539 static bitmap cse_ebb_live_in, cse_ebb_live_out;
541 /* A simple bitmap to track which basic blocks have been visited
542 already as part of an already processed extended basic block. */
543 static sbitmap cse_visited_basic_blocks;
545 static bool fixed_base_plus_p (rtx x);
546 static int notreg_cost (rtx, enum rtx_code);
547 static int approx_reg_cost_1 (rtx *, void *);
548 static int approx_reg_cost (rtx);
549 static int preferable (int, int, int, int);
550 static void new_basic_block (void);
551 static void make_new_qty (unsigned int, enum machine_mode);
552 static void make_regs_eqv (unsigned int, unsigned int);
553 static void delete_reg_equiv (unsigned int);
554 static int mention_regs (rtx);
555 static int insert_regs (rtx, struct table_elt *, int);
556 static void remove_from_table (struct table_elt *, unsigned);
557 static void remove_pseudo_from_table (rtx, unsigned);
558 static struct table_elt *lookup (rtx, unsigned, enum machine_mode);
559 static struct table_elt *lookup_for_remove (rtx, unsigned, enum machine_mode);
560 static rtx lookup_as_function (rtx, enum rtx_code);
561 static struct table_elt *insert (rtx, struct table_elt *, unsigned,
562 enum machine_mode);
563 static void merge_equiv_classes (struct table_elt *, struct table_elt *);
564 static void invalidate (rtx, enum machine_mode);
565 static bool cse_rtx_varies_p (const_rtx, bool);
566 static void remove_invalid_refs (unsigned int);
567 static void remove_invalid_subreg_refs (unsigned int, unsigned int,
568 enum machine_mode);
569 static void rehash_using_reg (rtx);
570 static void invalidate_memory (void);
571 static void invalidate_for_call (void);
572 static rtx use_related_value (rtx, struct table_elt *);
574 static inline unsigned canon_hash (rtx, enum machine_mode);
575 static inline unsigned safe_hash (rtx, enum machine_mode);
576 static unsigned hash_rtx_string (const char *);
578 static rtx canon_reg (rtx, rtx);
579 static enum rtx_code find_comparison_args (enum rtx_code, rtx *, rtx *,
580 enum machine_mode *,
581 enum machine_mode *);
582 static rtx fold_rtx (rtx, rtx);
583 static rtx equiv_constant (rtx);
584 static void record_jump_equiv (rtx, bool);
585 static void record_jump_cond (enum rtx_code, enum machine_mode, rtx, rtx,
586 int);
587 static void cse_insn (rtx);
588 static void cse_prescan_path (struct cse_basic_block_data *);
589 static void invalidate_from_clobbers (rtx);
590 static rtx cse_process_notes (rtx, rtx, bool *);
591 static void cse_extended_basic_block (struct cse_basic_block_data *);
592 static void count_reg_usage (rtx, int *, rtx, int);
593 static int check_for_label_ref (rtx *, void *);
594 extern void dump_class (struct table_elt*);
595 static void get_cse_reg_info_1 (unsigned int regno);
596 static struct cse_reg_info * get_cse_reg_info (unsigned int regno);
597 static int check_dependence (rtx *, void *);
599 static void flush_hash_table (void);
600 static bool insn_live_p (rtx, int *);
601 static bool set_live_p (rtx, rtx, int *);
602 static int cse_change_cc_mode (rtx *, void *);
603 static void cse_change_cc_mode_insn (rtx, rtx);
604 static void cse_change_cc_mode_insns (rtx, rtx, rtx);
605 static enum machine_mode cse_cc_succs (basic_block, rtx, rtx, bool);
608 #undef RTL_HOOKS_GEN_LOWPART
609 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_if_possible
611 static const struct rtl_hooks cse_rtl_hooks = RTL_HOOKS_INITIALIZER;
613 /* Nonzero if X has the form (PLUS frame-pointer integer). We check for
614 virtual regs here because the simplify_*_operation routines are called
615 by integrate.c, which is called before virtual register instantiation. */
617 static bool
618 fixed_base_plus_p (rtx x)
620 switch (GET_CODE (x))
622 case REG:
623 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx)
624 return true;
625 if (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])
626 return true;
627 if (REGNO (x) >= FIRST_VIRTUAL_REGISTER
628 && REGNO (x) <= LAST_VIRTUAL_REGISTER)
629 return true;
630 return false;
632 case PLUS:
633 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
634 return false;
635 return fixed_base_plus_p (XEXP (x, 0));
637 default:
638 return false;
642 /* Dump the expressions in the equivalence class indicated by CLASSP.
643 This function is used only for debugging. */
644 void
645 dump_class (struct table_elt *classp)
647 struct table_elt *elt;
649 fprintf (stderr, "Equivalence chain for ");
650 print_rtl (stderr, classp->exp);
651 fprintf (stderr, ": \n");
653 for (elt = classp->first_same_value; elt; elt = elt->next_same_value)
655 print_rtl (stderr, elt->exp);
656 fprintf (stderr, "\n");
660 /* Subroutine of approx_reg_cost; called through for_each_rtx. */
662 static int
663 approx_reg_cost_1 (rtx *xp, void *data)
665 rtx x = *xp;
666 int *cost_p = (int *) data;
668 if (x && REG_P (x))
670 unsigned int regno = REGNO (x);
672 if (! CHEAP_REGNO (regno))
674 if (regno < FIRST_PSEUDO_REGISTER)
676 if (SMALL_REGISTER_CLASSES)
677 return 1;
678 *cost_p += 2;
680 else
681 *cost_p += 1;
685 return 0;
688 /* Return an estimate of the cost of the registers used in an rtx.
689 This is mostly the number of different REG expressions in the rtx;
690 however for some exceptions like fixed registers we use a cost of
691 0. If any other hard register reference occurs, return MAX_COST. */
693 static int
694 approx_reg_cost (rtx x)
696 int cost = 0;
698 if (for_each_rtx (&x, approx_reg_cost_1, (void *) &cost))
699 return MAX_COST;
701 return cost;
704 /* Return a negative value if an rtx A, whose costs are given by COST_A
705 and REGCOST_A, is more desirable than an rtx B.
706 Return a positive value if A is less desirable, or 0 if the two are
707 equally good. */
708 static int
709 preferable (int cost_a, int regcost_a, int cost_b, int regcost_b)
711 /* First, get rid of cases involving expressions that are entirely
712 unwanted. */
713 if (cost_a != cost_b)
715 if (cost_a == MAX_COST)
716 return 1;
717 if (cost_b == MAX_COST)
718 return -1;
721 /* Avoid extending lifetimes of hardregs. */
722 if (regcost_a != regcost_b)
724 if (regcost_a == MAX_COST)
725 return 1;
726 if (regcost_b == MAX_COST)
727 return -1;
730 /* Normal operation costs take precedence. */
731 if (cost_a != cost_b)
732 return cost_a - cost_b;
733 /* Only if these are identical consider effects on register pressure. */
734 if (regcost_a != regcost_b)
735 return regcost_a - regcost_b;
736 return 0;
739 /* Internal function, to compute cost when X is not a register; called
740 from COST macro to keep it simple. */
742 static int
743 notreg_cost (rtx x, enum rtx_code outer)
745 return ((GET_CODE (x) == SUBREG
746 && REG_P (SUBREG_REG (x))
747 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT
748 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_INT
749 && (GET_MODE_SIZE (GET_MODE (x))
750 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
751 && subreg_lowpart_p (x)
752 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (x)),
753 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))))
755 : rtx_cost (x, outer) * 2);
759 /* Initialize CSE_REG_INFO_TABLE. */
761 static void
762 init_cse_reg_info (unsigned int nregs)
764 /* Do we need to grow the table? */
765 if (nregs > cse_reg_info_table_size)
767 unsigned int new_size;
769 if (cse_reg_info_table_size < 2048)
771 /* Compute a new size that is a power of 2 and no smaller
772 than the large of NREGS and 64. */
773 new_size = (cse_reg_info_table_size
774 ? cse_reg_info_table_size : 64);
776 while (new_size < nregs)
777 new_size *= 2;
779 else
781 /* If we need a big table, allocate just enough to hold
782 NREGS registers. */
783 new_size = nregs;
786 /* Reallocate the table with NEW_SIZE entries. */
787 if (cse_reg_info_table)
788 free (cse_reg_info_table);
789 cse_reg_info_table = XNEWVEC (struct cse_reg_info, new_size);
790 cse_reg_info_table_size = new_size;
791 cse_reg_info_table_first_uninitialized = 0;
794 /* Do we have all of the first NREGS entries initialized? */
795 if (cse_reg_info_table_first_uninitialized < nregs)
797 unsigned int old_timestamp = cse_reg_info_timestamp - 1;
798 unsigned int i;
800 /* Put the old timestamp on newly allocated entries so that they
801 will all be considered out of date. We do not touch those
802 entries beyond the first NREGS entries to be nice to the
803 virtual memory. */
804 for (i = cse_reg_info_table_first_uninitialized; i < nregs; i++)
805 cse_reg_info_table[i].timestamp = old_timestamp;
807 cse_reg_info_table_first_uninitialized = nregs;
811 /* Given REGNO, initialize the cse_reg_info entry for REGNO. */
813 static void
814 get_cse_reg_info_1 (unsigned int regno)
816 /* Set TIMESTAMP field to CSE_REG_INFO_TIMESTAMP so that this
817 entry will be considered to have been initialized. */
818 cse_reg_info_table[regno].timestamp = cse_reg_info_timestamp;
820 /* Initialize the rest of the entry. */
821 cse_reg_info_table[regno].reg_tick = 1;
822 cse_reg_info_table[regno].reg_in_table = -1;
823 cse_reg_info_table[regno].subreg_ticked = -1;
824 cse_reg_info_table[regno].reg_qty = -regno - 1;
827 /* Find a cse_reg_info entry for REGNO. */
829 static inline struct cse_reg_info *
830 get_cse_reg_info (unsigned int regno)
832 struct cse_reg_info *p = &cse_reg_info_table[regno];
834 /* If this entry has not been initialized, go ahead and initialize
835 it. */
836 if (p->timestamp != cse_reg_info_timestamp)
837 get_cse_reg_info_1 (regno);
839 return p;
842 /* Clear the hash table and initialize each register with its own quantity,
843 for a new basic block. */
845 static void
846 new_basic_block (void)
848 int i;
850 next_qty = 0;
852 /* Invalidate cse_reg_info_table. */
853 cse_reg_info_timestamp++;
855 /* Clear out hash table state for this pass. */
856 CLEAR_HARD_REG_SET (hard_regs_in_table);
858 /* The per-quantity values used to be initialized here, but it is
859 much faster to initialize each as it is made in `make_new_qty'. */
861 for (i = 0; i < HASH_SIZE; i++)
863 struct table_elt *first;
865 first = table[i];
866 if (first != NULL)
868 struct table_elt *last = first;
870 table[i] = NULL;
872 while (last->next_same_hash != NULL)
873 last = last->next_same_hash;
875 /* Now relink this hash entire chain into
876 the free element list. */
878 last->next_same_hash = free_element_chain;
879 free_element_chain = first;
883 #ifdef HAVE_cc0
884 prev_insn_cc0 = 0;
885 #endif
888 /* Say that register REG contains a quantity in mode MODE not in any
889 register before and initialize that quantity. */
891 static void
892 make_new_qty (unsigned int reg, enum machine_mode mode)
894 int q;
895 struct qty_table_elem *ent;
896 struct reg_eqv_elem *eqv;
898 gcc_assert (next_qty < max_qty);
900 q = REG_QTY (reg) = next_qty++;
901 ent = &qty_table[q];
902 ent->first_reg = reg;
903 ent->last_reg = reg;
904 ent->mode = mode;
905 ent->const_rtx = ent->const_insn = NULL_RTX;
906 ent->comparison_code = UNKNOWN;
908 eqv = &reg_eqv_table[reg];
909 eqv->next = eqv->prev = -1;
912 /* Make reg NEW equivalent to reg OLD.
913 OLD is not changing; NEW is. */
915 static void
916 make_regs_eqv (unsigned int new, unsigned int old)
918 unsigned int lastr, firstr;
919 int q = REG_QTY (old);
920 struct qty_table_elem *ent;
922 ent = &qty_table[q];
924 /* Nothing should become eqv until it has a "non-invalid" qty number. */
925 gcc_assert (REGNO_QTY_VALID_P (old));
927 REG_QTY (new) = q;
928 firstr = ent->first_reg;
929 lastr = ent->last_reg;
931 /* Prefer fixed hard registers to anything. Prefer pseudo regs to other
932 hard regs. Among pseudos, if NEW will live longer than any other reg
933 of the same qty, and that is beyond the current basic block,
934 make it the new canonical replacement for this qty. */
935 if (! (firstr < FIRST_PSEUDO_REGISTER && FIXED_REGNO_P (firstr))
936 /* Certain fixed registers might be of the class NO_REGS. This means
937 that not only can they not be allocated by the compiler, but
938 they cannot be used in substitutions or canonicalizations
939 either. */
940 && (new >= FIRST_PSEUDO_REGISTER || REGNO_REG_CLASS (new) != NO_REGS)
941 && ((new < FIRST_PSEUDO_REGISTER && FIXED_REGNO_P (new))
942 || (new >= FIRST_PSEUDO_REGISTER
943 && (firstr < FIRST_PSEUDO_REGISTER
944 || (bitmap_bit_p (cse_ebb_live_out, new)
945 && !bitmap_bit_p (cse_ebb_live_out, firstr))
946 || (bitmap_bit_p (cse_ebb_live_in, new)
947 && !bitmap_bit_p (cse_ebb_live_in, firstr))))))
949 reg_eqv_table[firstr].prev = new;
950 reg_eqv_table[new].next = firstr;
951 reg_eqv_table[new].prev = -1;
952 ent->first_reg = new;
954 else
956 /* If NEW is a hard reg (known to be non-fixed), insert at end.
957 Otherwise, insert before any non-fixed hard regs that are at the
958 end. Registers of class NO_REGS cannot be used as an
959 equivalent for anything. */
960 while (lastr < FIRST_PSEUDO_REGISTER && reg_eqv_table[lastr].prev >= 0
961 && (REGNO_REG_CLASS (lastr) == NO_REGS || ! FIXED_REGNO_P (lastr))
962 && new >= FIRST_PSEUDO_REGISTER)
963 lastr = reg_eqv_table[lastr].prev;
964 reg_eqv_table[new].next = reg_eqv_table[lastr].next;
965 if (reg_eqv_table[lastr].next >= 0)
966 reg_eqv_table[reg_eqv_table[lastr].next].prev = new;
967 else
968 qty_table[q].last_reg = new;
969 reg_eqv_table[lastr].next = new;
970 reg_eqv_table[new].prev = lastr;
974 /* Remove REG from its equivalence class. */
976 static void
977 delete_reg_equiv (unsigned int reg)
979 struct qty_table_elem *ent;
980 int q = REG_QTY (reg);
981 int p, n;
983 /* If invalid, do nothing. */
984 if (! REGNO_QTY_VALID_P (reg))
985 return;
987 ent = &qty_table[q];
989 p = reg_eqv_table[reg].prev;
990 n = reg_eqv_table[reg].next;
992 if (n != -1)
993 reg_eqv_table[n].prev = p;
994 else
995 ent->last_reg = p;
996 if (p != -1)
997 reg_eqv_table[p].next = n;
998 else
999 ent->first_reg = n;
1001 REG_QTY (reg) = -reg - 1;
1004 /* Remove any invalid expressions from the hash table
1005 that refer to any of the registers contained in expression X.
1007 Make sure that newly inserted references to those registers
1008 as subexpressions will be considered valid.
1010 mention_regs is not called when a register itself
1011 is being stored in the table.
1013 Return 1 if we have done something that may have changed the hash code
1014 of X. */
1016 static int
1017 mention_regs (rtx x)
1019 enum rtx_code code;
1020 int i, j;
1021 const char *fmt;
1022 int changed = 0;
1024 if (x == 0)
1025 return 0;
1027 code = GET_CODE (x);
1028 if (code == REG)
1030 unsigned int regno = REGNO (x);
1031 unsigned int endregno = END_REGNO (x);
1032 unsigned int i;
1034 for (i = regno; i < endregno; i++)
1036 if (REG_IN_TABLE (i) >= 0 && REG_IN_TABLE (i) != REG_TICK (i))
1037 remove_invalid_refs (i);
1039 REG_IN_TABLE (i) = REG_TICK (i);
1040 SUBREG_TICKED (i) = -1;
1043 return 0;
1046 /* If this is a SUBREG, we don't want to discard other SUBREGs of the same
1047 pseudo if they don't use overlapping words. We handle only pseudos
1048 here for simplicity. */
1049 if (code == SUBREG && REG_P (SUBREG_REG (x))
1050 && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER)
1052 unsigned int i = REGNO (SUBREG_REG (x));
1054 if (REG_IN_TABLE (i) >= 0 && REG_IN_TABLE (i) != REG_TICK (i))
1056 /* If REG_IN_TABLE (i) differs from REG_TICK (i) by one, and
1057 the last store to this register really stored into this
1058 subreg, then remove the memory of this subreg.
1059 Otherwise, remove any memory of the entire register and
1060 all its subregs from the table. */
1061 if (REG_TICK (i) - REG_IN_TABLE (i) > 1
1062 || SUBREG_TICKED (i) != REGNO (SUBREG_REG (x)))
1063 remove_invalid_refs (i);
1064 else
1065 remove_invalid_subreg_refs (i, SUBREG_BYTE (x), GET_MODE (x));
1068 REG_IN_TABLE (i) = REG_TICK (i);
1069 SUBREG_TICKED (i) = REGNO (SUBREG_REG (x));
1070 return 0;
1073 /* If X is a comparison or a COMPARE and either operand is a register
1074 that does not have a quantity, give it one. This is so that a later
1075 call to record_jump_equiv won't cause X to be assigned a different
1076 hash code and not found in the table after that call.
1078 It is not necessary to do this here, since rehash_using_reg can
1079 fix up the table later, but doing this here eliminates the need to
1080 call that expensive function in the most common case where the only
1081 use of the register is in the comparison. */
1083 if (code == COMPARE || COMPARISON_P (x))
1085 if (REG_P (XEXP (x, 0))
1086 && ! REGNO_QTY_VALID_P (REGNO (XEXP (x, 0))))
1087 if (insert_regs (XEXP (x, 0), NULL, 0))
1089 rehash_using_reg (XEXP (x, 0));
1090 changed = 1;
1093 if (REG_P (XEXP (x, 1))
1094 && ! REGNO_QTY_VALID_P (REGNO (XEXP (x, 1))))
1095 if (insert_regs (XEXP (x, 1), NULL, 0))
1097 rehash_using_reg (XEXP (x, 1));
1098 changed = 1;
1102 fmt = GET_RTX_FORMAT (code);
1103 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1104 if (fmt[i] == 'e')
1105 changed |= mention_regs (XEXP (x, i));
1106 else if (fmt[i] == 'E')
1107 for (j = 0; j < XVECLEN (x, i); j++)
1108 changed |= mention_regs (XVECEXP (x, i, j));
1110 return changed;
1113 /* Update the register quantities for inserting X into the hash table
1114 with a value equivalent to CLASSP.
1115 (If the class does not contain a REG, it is irrelevant.)
1116 If MODIFIED is nonzero, X is a destination; it is being modified.
1117 Note that delete_reg_equiv should be called on a register
1118 before insert_regs is done on that register with MODIFIED != 0.
1120 Nonzero value means that elements of reg_qty have changed
1121 so X's hash code may be different. */
1123 static int
1124 insert_regs (rtx x, struct table_elt *classp, int modified)
1126 if (REG_P (x))
1128 unsigned int regno = REGNO (x);
1129 int qty_valid;
1131 /* If REGNO is in the equivalence table already but is of the
1132 wrong mode for that equivalence, don't do anything here. */
1134 qty_valid = REGNO_QTY_VALID_P (regno);
1135 if (qty_valid)
1137 struct qty_table_elem *ent = &qty_table[REG_QTY (regno)];
1139 if (ent->mode != GET_MODE (x))
1140 return 0;
1143 if (modified || ! qty_valid)
1145 if (classp)
1146 for (classp = classp->first_same_value;
1147 classp != 0;
1148 classp = classp->next_same_value)
1149 if (REG_P (classp->exp)
1150 && GET_MODE (classp->exp) == GET_MODE (x))
1152 unsigned c_regno = REGNO (classp->exp);
1154 gcc_assert (REGNO_QTY_VALID_P (c_regno));
1156 /* Suppose that 5 is hard reg and 100 and 101 are
1157 pseudos. Consider
1159 (set (reg:si 100) (reg:si 5))
1160 (set (reg:si 5) (reg:si 100))
1161 (set (reg:di 101) (reg:di 5))
1163 We would now set REG_QTY (101) = REG_QTY (5), but the
1164 entry for 5 is in SImode. When we use this later in
1165 copy propagation, we get the register in wrong mode. */
1166 if (qty_table[REG_QTY (c_regno)].mode != GET_MODE (x))
1167 continue;
1169 make_regs_eqv (regno, c_regno);
1170 return 1;
1173 /* Mention_regs for a SUBREG checks if REG_TICK is exactly one larger
1174 than REG_IN_TABLE to find out if there was only a single preceding
1175 invalidation - for the SUBREG - or another one, which would be
1176 for the full register. However, if we find here that REG_TICK
1177 indicates that the register is invalid, it means that it has
1178 been invalidated in a separate operation. The SUBREG might be used
1179 now (then this is a recursive call), or we might use the full REG
1180 now and a SUBREG of it later. So bump up REG_TICK so that
1181 mention_regs will do the right thing. */
1182 if (! modified
1183 && REG_IN_TABLE (regno) >= 0
1184 && REG_TICK (regno) == REG_IN_TABLE (regno) + 1)
1185 REG_TICK (regno)++;
1186 make_new_qty (regno, GET_MODE (x));
1187 return 1;
1190 return 0;
1193 /* If X is a SUBREG, we will likely be inserting the inner register in the
1194 table. If that register doesn't have an assigned quantity number at
1195 this point but does later, the insertion that we will be doing now will
1196 not be accessible because its hash code will have changed. So assign
1197 a quantity number now. */
1199 else if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x))
1200 && ! REGNO_QTY_VALID_P (REGNO (SUBREG_REG (x))))
1202 insert_regs (SUBREG_REG (x), NULL, 0);
1203 mention_regs (x);
1204 return 1;
1206 else
1207 return mention_regs (x);
1210 /* Look in or update the hash table. */
1212 /* Remove table element ELT from use in the table.
1213 HASH is its hash code, made using the HASH macro.
1214 It's an argument because often that is known in advance
1215 and we save much time not recomputing it. */
1217 static void
1218 remove_from_table (struct table_elt *elt, unsigned int hash)
1220 if (elt == 0)
1221 return;
1223 /* Mark this element as removed. See cse_insn. */
1224 elt->first_same_value = 0;
1226 /* Remove the table element from its equivalence class. */
1229 struct table_elt *prev = elt->prev_same_value;
1230 struct table_elt *next = elt->next_same_value;
1232 if (next)
1233 next->prev_same_value = prev;
1235 if (prev)
1236 prev->next_same_value = next;
1237 else
1239 struct table_elt *newfirst = next;
1240 while (next)
1242 next->first_same_value = newfirst;
1243 next = next->next_same_value;
1248 /* Remove the table element from its hash bucket. */
1251 struct table_elt *prev = elt->prev_same_hash;
1252 struct table_elt *next = elt->next_same_hash;
1254 if (next)
1255 next->prev_same_hash = prev;
1257 if (prev)
1258 prev->next_same_hash = next;
1259 else if (table[hash] == elt)
1260 table[hash] = next;
1261 else
1263 /* This entry is not in the proper hash bucket. This can happen
1264 when two classes were merged by `merge_equiv_classes'. Search
1265 for the hash bucket that it heads. This happens only very
1266 rarely, so the cost is acceptable. */
1267 for (hash = 0; hash < HASH_SIZE; hash++)
1268 if (table[hash] == elt)
1269 table[hash] = next;
1273 /* Remove the table element from its related-value circular chain. */
1275 if (elt->related_value != 0 && elt->related_value != elt)
1277 struct table_elt *p = elt->related_value;
1279 while (p->related_value != elt)
1280 p = p->related_value;
1281 p->related_value = elt->related_value;
1282 if (p->related_value == p)
1283 p->related_value = 0;
1286 /* Now add it to the free element chain. */
1287 elt->next_same_hash = free_element_chain;
1288 free_element_chain = elt;
1291 /* Same as above, but X is a pseudo-register. */
1293 static void
1294 remove_pseudo_from_table (rtx x, unsigned int hash)
1296 struct table_elt *elt;
1298 /* Because a pseudo-register can be referenced in more than one
1299 mode, we might have to remove more than one table entry. */
1300 while ((elt = lookup_for_remove (x, hash, VOIDmode)))
1301 remove_from_table (elt, hash);
1304 /* Look up X in the hash table and return its table element,
1305 or 0 if X is not in the table.
1307 MODE is the machine-mode of X, or if X is an integer constant
1308 with VOIDmode then MODE is the mode with which X will be used.
1310 Here we are satisfied to find an expression whose tree structure
1311 looks like X. */
1313 static struct table_elt *
1314 lookup (rtx x, unsigned int hash, enum machine_mode mode)
1316 struct table_elt *p;
1318 for (p = table[hash]; p; p = p->next_same_hash)
1319 if (mode == p->mode && ((x == p->exp && REG_P (x))
1320 || exp_equiv_p (x, p->exp, !REG_P (x), false)))
1321 return p;
1323 return 0;
1326 /* Like `lookup' but don't care whether the table element uses invalid regs.
1327 Also ignore discrepancies in the machine mode of a register. */
1329 static struct table_elt *
1330 lookup_for_remove (rtx x, unsigned int hash, enum machine_mode mode)
1332 struct table_elt *p;
1334 if (REG_P (x))
1336 unsigned int regno = REGNO (x);
1338 /* Don't check the machine mode when comparing registers;
1339 invalidating (REG:SI 0) also invalidates (REG:DF 0). */
1340 for (p = table[hash]; p; p = p->next_same_hash)
1341 if (REG_P (p->exp)
1342 && REGNO (p->exp) == regno)
1343 return p;
1345 else
1347 for (p = table[hash]; p; p = p->next_same_hash)
1348 if (mode == p->mode
1349 && (x == p->exp || exp_equiv_p (x, p->exp, 0, false)))
1350 return p;
1353 return 0;
1356 /* Look for an expression equivalent to X and with code CODE.
1357 If one is found, return that expression. */
1359 static rtx
1360 lookup_as_function (rtx x, enum rtx_code code)
1362 struct table_elt *p
1363 = lookup (x, SAFE_HASH (x, VOIDmode), GET_MODE (x));
1365 /* If we are looking for a CONST_INT, the mode doesn't really matter, as
1366 long as we are narrowing. So if we looked in vain for a mode narrower
1367 than word_mode before, look for word_mode now. */
1368 if (p == 0 && code == CONST_INT
1369 && GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (word_mode))
1371 x = copy_rtx (x);
1372 PUT_MODE (x, word_mode);
1373 p = lookup (x, SAFE_HASH (x, VOIDmode), word_mode);
1376 if (p == 0)
1377 return 0;
1379 for (p = p->first_same_value; p; p = p->next_same_value)
1380 if (GET_CODE (p->exp) == code
1381 /* Make sure this is a valid entry in the table. */
1382 && exp_equiv_p (p->exp, p->exp, 1, false))
1383 return p->exp;
1385 return 0;
1388 /* Insert X in the hash table, assuming HASH is its hash code
1389 and CLASSP is an element of the class it should go in
1390 (or 0 if a new class should be made).
1391 It is inserted at the proper position to keep the class in
1392 the order cheapest first.
1394 MODE is the machine-mode of X, or if X is an integer constant
1395 with VOIDmode then MODE is the mode with which X will be used.
1397 For elements of equal cheapness, the most recent one
1398 goes in front, except that the first element in the list
1399 remains first unless a cheaper element is added. The order of
1400 pseudo-registers does not matter, as canon_reg will be called to
1401 find the cheapest when a register is retrieved from the table.
1403 The in_memory field in the hash table element is set to 0.
1404 The caller must set it nonzero if appropriate.
1406 You should call insert_regs (X, CLASSP, MODIFY) before calling here,
1407 and if insert_regs returns a nonzero value
1408 you must then recompute its hash code before calling here.
1410 If necessary, update table showing constant values of quantities. */
1412 #define CHEAPER(X, Y) \
1413 (preferable ((X)->cost, (X)->regcost, (Y)->cost, (Y)->regcost) < 0)
1415 static struct table_elt *
1416 insert (rtx x, struct table_elt *classp, unsigned int hash, enum machine_mode mode)
1418 struct table_elt *elt;
1420 /* If X is a register and we haven't made a quantity for it,
1421 something is wrong. */
1422 gcc_assert (!REG_P (x) || REGNO_QTY_VALID_P (REGNO (x)));
1424 /* If X is a hard register, show it is being put in the table. */
1425 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
1426 add_to_hard_reg_set (&hard_regs_in_table, GET_MODE (x), REGNO (x));
1428 /* Put an element for X into the right hash bucket. */
1430 elt = free_element_chain;
1431 if (elt)
1432 free_element_chain = elt->next_same_hash;
1433 else
1434 elt = XNEW (struct table_elt);
1436 elt->exp = x;
1437 elt->canon_exp = NULL_RTX;
1438 elt->cost = COST (x);
1439 elt->regcost = approx_reg_cost (x);
1440 elt->next_same_value = 0;
1441 elt->prev_same_value = 0;
1442 elt->next_same_hash = table[hash];
1443 elt->prev_same_hash = 0;
1444 elt->related_value = 0;
1445 elt->in_memory = 0;
1446 elt->mode = mode;
1447 elt->is_const = (CONSTANT_P (x) || fixed_base_plus_p (x));
1449 if (table[hash])
1450 table[hash]->prev_same_hash = elt;
1451 table[hash] = elt;
1453 /* Put it into the proper value-class. */
1454 if (classp)
1456 classp = classp->first_same_value;
1457 if (CHEAPER (elt, classp))
1458 /* Insert at the head of the class. */
1460 struct table_elt *p;
1461 elt->next_same_value = classp;
1462 classp->prev_same_value = elt;
1463 elt->first_same_value = elt;
1465 for (p = classp; p; p = p->next_same_value)
1466 p->first_same_value = elt;
1468 else
1470 /* Insert not at head of the class. */
1471 /* Put it after the last element cheaper than X. */
1472 struct table_elt *p, *next;
1474 for (p = classp; (next = p->next_same_value) && CHEAPER (next, elt);
1475 p = next);
1477 /* Put it after P and before NEXT. */
1478 elt->next_same_value = next;
1479 if (next)
1480 next->prev_same_value = elt;
1482 elt->prev_same_value = p;
1483 p->next_same_value = elt;
1484 elt->first_same_value = classp;
1487 else
1488 elt->first_same_value = elt;
1490 /* If this is a constant being set equivalent to a register or a register
1491 being set equivalent to a constant, note the constant equivalence.
1493 If this is a constant, it cannot be equivalent to a different constant,
1494 and a constant is the only thing that can be cheaper than a register. So
1495 we know the register is the head of the class (before the constant was
1496 inserted).
1498 If this is a register that is not already known equivalent to a
1499 constant, we must check the entire class.
1501 If this is a register that is already known equivalent to an insn,
1502 update the qtys `const_insn' to show that `this_insn' is the latest
1503 insn making that quantity equivalent to the constant. */
1505 if (elt->is_const && classp && REG_P (classp->exp)
1506 && !REG_P (x))
1508 int exp_q = REG_QTY (REGNO (classp->exp));
1509 struct qty_table_elem *exp_ent = &qty_table[exp_q];
1511 exp_ent->const_rtx = gen_lowpart (exp_ent->mode, x);
1512 exp_ent->const_insn = this_insn;
1515 else if (REG_P (x)
1516 && classp
1517 && ! qty_table[REG_QTY (REGNO (x))].const_rtx
1518 && ! elt->is_const)
1520 struct table_elt *p;
1522 for (p = classp; p != 0; p = p->next_same_value)
1524 if (p->is_const && !REG_P (p->exp))
1526 int x_q = REG_QTY (REGNO (x));
1527 struct qty_table_elem *x_ent = &qty_table[x_q];
1529 x_ent->const_rtx
1530 = gen_lowpart (GET_MODE (x), p->exp);
1531 x_ent->const_insn = this_insn;
1532 break;
1537 else if (REG_P (x)
1538 && qty_table[REG_QTY (REGNO (x))].const_rtx
1539 && GET_MODE (x) == qty_table[REG_QTY (REGNO (x))].mode)
1540 qty_table[REG_QTY (REGNO (x))].const_insn = this_insn;
1542 /* If this is a constant with symbolic value,
1543 and it has a term with an explicit integer value,
1544 link it up with related expressions. */
1545 if (GET_CODE (x) == CONST)
1547 rtx subexp = get_related_value (x);
1548 unsigned subhash;
1549 struct table_elt *subelt, *subelt_prev;
1551 if (subexp != 0)
1553 /* Get the integer-free subexpression in the hash table. */
1554 subhash = SAFE_HASH (subexp, mode);
1555 subelt = lookup (subexp, subhash, mode);
1556 if (subelt == 0)
1557 subelt = insert (subexp, NULL, subhash, mode);
1558 /* Initialize SUBELT's circular chain if it has none. */
1559 if (subelt->related_value == 0)
1560 subelt->related_value = subelt;
1561 /* Find the element in the circular chain that precedes SUBELT. */
1562 subelt_prev = subelt;
1563 while (subelt_prev->related_value != subelt)
1564 subelt_prev = subelt_prev->related_value;
1565 /* Put new ELT into SUBELT's circular chain just before SUBELT.
1566 This way the element that follows SUBELT is the oldest one. */
1567 elt->related_value = subelt_prev->related_value;
1568 subelt_prev->related_value = elt;
1572 return elt;
1575 /* Given two equivalence classes, CLASS1 and CLASS2, put all the entries from
1576 CLASS2 into CLASS1. This is done when we have reached an insn which makes
1577 the two classes equivalent.
1579 CLASS1 will be the surviving class; CLASS2 should not be used after this
1580 call.
1582 Any invalid entries in CLASS2 will not be copied. */
1584 static void
1585 merge_equiv_classes (struct table_elt *class1, struct table_elt *class2)
1587 struct table_elt *elt, *next, *new;
1589 /* Ensure we start with the head of the classes. */
1590 class1 = class1->first_same_value;
1591 class2 = class2->first_same_value;
1593 /* If they were already equal, forget it. */
1594 if (class1 == class2)
1595 return;
1597 for (elt = class2; elt; elt = next)
1599 unsigned int hash;
1600 rtx exp = elt->exp;
1601 enum machine_mode mode = elt->mode;
1603 next = elt->next_same_value;
1605 /* Remove old entry, make a new one in CLASS1's class.
1606 Don't do this for invalid entries as we cannot find their
1607 hash code (it also isn't necessary). */
1608 if (REG_P (exp) || exp_equiv_p (exp, exp, 1, false))
1610 bool need_rehash = false;
1612 hash_arg_in_memory = 0;
1613 hash = HASH (exp, mode);
1615 if (REG_P (exp))
1617 need_rehash = REGNO_QTY_VALID_P (REGNO (exp));
1618 delete_reg_equiv (REGNO (exp));
1621 if (REG_P (exp) && REGNO (exp) >= FIRST_PSEUDO_REGISTER)
1622 remove_pseudo_from_table (exp, hash);
1623 else
1624 remove_from_table (elt, hash);
1626 if (insert_regs (exp, class1, 0) || need_rehash)
1628 rehash_using_reg (exp);
1629 hash = HASH (exp, mode);
1631 new = insert (exp, class1, hash, mode);
1632 new->in_memory = hash_arg_in_memory;
1637 /* Flush the entire hash table. */
1639 static void
1640 flush_hash_table (void)
1642 int i;
1643 struct table_elt *p;
1645 for (i = 0; i < HASH_SIZE; i++)
1646 for (p = table[i]; p; p = table[i])
1648 /* Note that invalidate can remove elements
1649 after P in the current hash chain. */
1650 if (REG_P (p->exp))
1651 invalidate (p->exp, VOIDmode);
1652 else
1653 remove_from_table (p, i);
1657 /* Function called for each rtx to check whether true dependence exist. */
1658 struct check_dependence_data
1660 enum machine_mode mode;
1661 rtx exp;
1662 rtx addr;
1665 static int
1666 check_dependence (rtx *x, void *data)
1668 struct check_dependence_data *d = (struct check_dependence_data *) data;
1669 if (*x && MEM_P (*x))
1670 return canon_true_dependence (d->exp, d->mode, d->addr, *x,
1671 cse_rtx_varies_p);
1672 else
1673 return 0;
1676 /* Remove from the hash table, or mark as invalid, all expressions whose
1677 values could be altered by storing in X. X is a register, a subreg, or
1678 a memory reference with nonvarying address (because, when a memory
1679 reference with a varying address is stored in, all memory references are
1680 removed by invalidate_memory so specific invalidation is superfluous).
1681 FULL_MODE, if not VOIDmode, indicates that this much should be
1682 invalidated instead of just the amount indicated by the mode of X. This
1683 is only used for bitfield stores into memory.
1685 A nonvarying address may be just a register or just a symbol reference,
1686 or it may be either of those plus a numeric offset. */
1688 static void
1689 invalidate (rtx x, enum machine_mode full_mode)
1691 int i;
1692 struct table_elt *p;
1693 rtx addr;
1695 switch (GET_CODE (x))
1697 case REG:
1699 /* If X is a register, dependencies on its contents are recorded
1700 through the qty number mechanism. Just change the qty number of
1701 the register, mark it as invalid for expressions that refer to it,
1702 and remove it itself. */
1703 unsigned int regno = REGNO (x);
1704 unsigned int hash = HASH (x, GET_MODE (x));
1706 /* Remove REGNO from any quantity list it might be on and indicate
1707 that its value might have changed. If it is a pseudo, remove its
1708 entry from the hash table.
1710 For a hard register, we do the first two actions above for any
1711 additional hard registers corresponding to X. Then, if any of these
1712 registers are in the table, we must remove any REG entries that
1713 overlap these registers. */
1715 delete_reg_equiv (regno);
1716 REG_TICK (regno)++;
1717 SUBREG_TICKED (regno) = -1;
1719 if (regno >= FIRST_PSEUDO_REGISTER)
1720 remove_pseudo_from_table (x, hash);
1721 else
1723 HOST_WIDE_INT in_table
1724 = TEST_HARD_REG_BIT (hard_regs_in_table, regno);
1725 unsigned int endregno = END_HARD_REGNO (x);
1726 unsigned int tregno, tendregno, rn;
1727 struct table_elt *p, *next;
1729 CLEAR_HARD_REG_BIT (hard_regs_in_table, regno);
1731 for (rn = regno + 1; rn < endregno; rn++)
1733 in_table |= TEST_HARD_REG_BIT (hard_regs_in_table, rn);
1734 CLEAR_HARD_REG_BIT (hard_regs_in_table, rn);
1735 delete_reg_equiv (rn);
1736 REG_TICK (rn)++;
1737 SUBREG_TICKED (rn) = -1;
1740 if (in_table)
1741 for (hash = 0; hash < HASH_SIZE; hash++)
1742 for (p = table[hash]; p; p = next)
1744 next = p->next_same_hash;
1746 if (!REG_P (p->exp)
1747 || REGNO (p->exp) >= FIRST_PSEUDO_REGISTER)
1748 continue;
1750 tregno = REGNO (p->exp);
1751 tendregno = END_HARD_REGNO (p->exp);
1752 if (tendregno > regno && tregno < endregno)
1753 remove_from_table (p, hash);
1757 return;
1759 case SUBREG:
1760 invalidate (SUBREG_REG (x), VOIDmode);
1761 return;
1763 case PARALLEL:
1764 for (i = XVECLEN (x, 0) - 1; i >= 0; --i)
1765 invalidate (XVECEXP (x, 0, i), VOIDmode);
1766 return;
1768 case EXPR_LIST:
1769 /* This is part of a disjoint return value; extract the location in
1770 question ignoring the offset. */
1771 invalidate (XEXP (x, 0), VOIDmode);
1772 return;
1774 case MEM:
1775 addr = canon_rtx (get_addr (XEXP (x, 0)));
1776 /* Calculate the canonical version of X here so that
1777 true_dependence doesn't generate new RTL for X on each call. */
1778 x = canon_rtx (x);
1780 /* Remove all hash table elements that refer to overlapping pieces of
1781 memory. */
1782 if (full_mode == VOIDmode)
1783 full_mode = GET_MODE (x);
1785 for (i = 0; i < HASH_SIZE; i++)
1787 struct table_elt *next;
1789 for (p = table[i]; p; p = next)
1791 next = p->next_same_hash;
1792 if (p->in_memory)
1794 struct check_dependence_data d;
1796 /* Just canonicalize the expression once;
1797 otherwise each time we call invalidate
1798 true_dependence will canonicalize the
1799 expression again. */
1800 if (!p->canon_exp)
1801 p->canon_exp = canon_rtx (p->exp);
1802 d.exp = x;
1803 d.addr = addr;
1804 d.mode = full_mode;
1805 if (for_each_rtx (&p->canon_exp, check_dependence, &d))
1806 remove_from_table (p, i);
1810 return;
1812 default:
1813 gcc_unreachable ();
1817 /* Remove all expressions that refer to register REGNO,
1818 since they are already invalid, and we are about to
1819 mark that register valid again and don't want the old
1820 expressions to reappear as valid. */
1822 static void
1823 remove_invalid_refs (unsigned int regno)
1825 unsigned int i;
1826 struct table_elt *p, *next;
1828 for (i = 0; i < HASH_SIZE; i++)
1829 for (p = table[i]; p; p = next)
1831 next = p->next_same_hash;
1832 if (!REG_P (p->exp)
1833 && refers_to_regno_p (regno, regno + 1, p->exp, (rtx *) 0))
1834 remove_from_table (p, i);
1838 /* Likewise for a subreg with subreg_reg REGNO, subreg_byte OFFSET,
1839 and mode MODE. */
1840 static void
1841 remove_invalid_subreg_refs (unsigned int regno, unsigned int offset,
1842 enum machine_mode mode)
1844 unsigned int i;
1845 struct table_elt *p, *next;
1846 unsigned int end = offset + (GET_MODE_SIZE (mode) - 1);
1848 for (i = 0; i < HASH_SIZE; i++)
1849 for (p = table[i]; p; p = next)
1851 rtx exp = p->exp;
1852 next = p->next_same_hash;
1854 if (!REG_P (exp)
1855 && (GET_CODE (exp) != SUBREG
1856 || !REG_P (SUBREG_REG (exp))
1857 || REGNO (SUBREG_REG (exp)) != regno
1858 || (((SUBREG_BYTE (exp)
1859 + (GET_MODE_SIZE (GET_MODE (exp)) - 1)) >= offset)
1860 && SUBREG_BYTE (exp) <= end))
1861 && refers_to_regno_p (regno, regno + 1, p->exp, (rtx *) 0))
1862 remove_from_table (p, i);
1866 /* Recompute the hash codes of any valid entries in the hash table that
1867 reference X, if X is a register, or SUBREG_REG (X) if X is a SUBREG.
1869 This is called when we make a jump equivalence. */
1871 static void
1872 rehash_using_reg (rtx x)
1874 unsigned int i;
1875 struct table_elt *p, *next;
1876 unsigned hash;
1878 if (GET_CODE (x) == SUBREG)
1879 x = SUBREG_REG (x);
1881 /* If X is not a register or if the register is known not to be in any
1882 valid entries in the table, we have no work to do. */
1884 if (!REG_P (x)
1885 || REG_IN_TABLE (REGNO (x)) < 0
1886 || REG_IN_TABLE (REGNO (x)) != REG_TICK (REGNO (x)))
1887 return;
1889 /* Scan all hash chains looking for valid entries that mention X.
1890 If we find one and it is in the wrong hash chain, move it. */
1892 for (i = 0; i < HASH_SIZE; i++)
1893 for (p = table[i]; p; p = next)
1895 next = p->next_same_hash;
1896 if (reg_mentioned_p (x, p->exp)
1897 && exp_equiv_p (p->exp, p->exp, 1, false)
1898 && i != (hash = SAFE_HASH (p->exp, p->mode)))
1900 if (p->next_same_hash)
1901 p->next_same_hash->prev_same_hash = p->prev_same_hash;
1903 if (p->prev_same_hash)
1904 p->prev_same_hash->next_same_hash = p->next_same_hash;
1905 else
1906 table[i] = p->next_same_hash;
1908 p->next_same_hash = table[hash];
1909 p->prev_same_hash = 0;
1910 if (table[hash])
1911 table[hash]->prev_same_hash = p;
1912 table[hash] = p;
1917 /* Remove from the hash table any expression that is a call-clobbered
1918 register. Also update their TICK values. */
1920 static void
1921 invalidate_for_call (void)
1923 unsigned int regno, endregno;
1924 unsigned int i;
1925 unsigned hash;
1926 struct table_elt *p, *next;
1927 int in_table = 0;
1929 /* Go through all the hard registers. For each that is clobbered in
1930 a CALL_INSN, remove the register from quantity chains and update
1931 reg_tick if defined. Also see if any of these registers is currently
1932 in the table. */
1934 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1935 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
1937 delete_reg_equiv (regno);
1938 if (REG_TICK (regno) >= 0)
1940 REG_TICK (regno)++;
1941 SUBREG_TICKED (regno) = -1;
1944 in_table |= (TEST_HARD_REG_BIT (hard_regs_in_table, regno) != 0);
1947 /* In the case where we have no call-clobbered hard registers in the
1948 table, we are done. Otherwise, scan the table and remove any
1949 entry that overlaps a call-clobbered register. */
1951 if (in_table)
1952 for (hash = 0; hash < HASH_SIZE; hash++)
1953 for (p = table[hash]; p; p = next)
1955 next = p->next_same_hash;
1957 if (!REG_P (p->exp)
1958 || REGNO (p->exp) >= FIRST_PSEUDO_REGISTER)
1959 continue;
1961 regno = REGNO (p->exp);
1962 endregno = END_HARD_REGNO (p->exp);
1964 for (i = regno; i < endregno; i++)
1965 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1967 remove_from_table (p, hash);
1968 break;
1973 /* Given an expression X of type CONST,
1974 and ELT which is its table entry (or 0 if it
1975 is not in the hash table),
1976 return an alternate expression for X as a register plus integer.
1977 If none can be found, return 0. */
1979 static rtx
1980 use_related_value (rtx x, struct table_elt *elt)
1982 struct table_elt *relt = 0;
1983 struct table_elt *p, *q;
1984 HOST_WIDE_INT offset;
1986 /* First, is there anything related known?
1987 If we have a table element, we can tell from that.
1988 Otherwise, must look it up. */
1990 if (elt != 0 && elt->related_value != 0)
1991 relt = elt;
1992 else if (elt == 0 && GET_CODE (x) == CONST)
1994 rtx subexp = get_related_value (x);
1995 if (subexp != 0)
1996 relt = lookup (subexp,
1997 SAFE_HASH (subexp, GET_MODE (subexp)),
1998 GET_MODE (subexp));
2001 if (relt == 0)
2002 return 0;
2004 /* Search all related table entries for one that has an
2005 equivalent register. */
2007 p = relt;
2008 while (1)
2010 /* This loop is strange in that it is executed in two different cases.
2011 The first is when X is already in the table. Then it is searching
2012 the RELATED_VALUE list of X's class (RELT). The second case is when
2013 X is not in the table. Then RELT points to a class for the related
2014 value.
2016 Ensure that, whatever case we are in, that we ignore classes that have
2017 the same value as X. */
2019 if (rtx_equal_p (x, p->exp))
2020 q = 0;
2021 else
2022 for (q = p->first_same_value; q; q = q->next_same_value)
2023 if (REG_P (q->exp))
2024 break;
2026 if (q)
2027 break;
2029 p = p->related_value;
2031 /* We went all the way around, so there is nothing to be found.
2032 Alternatively, perhaps RELT was in the table for some other reason
2033 and it has no related values recorded. */
2034 if (p == relt || p == 0)
2035 break;
2038 if (q == 0)
2039 return 0;
2041 offset = (get_integer_term (x) - get_integer_term (p->exp));
2042 /* Note: OFFSET may be 0 if P->xexp and X are related by commutativity. */
2043 return plus_constant (q->exp, offset);
2046 /* Hash a string. Just add its bytes up. */
2047 static inline unsigned
2048 hash_rtx_string (const char *ps)
2050 unsigned hash = 0;
2051 const unsigned char *p = (const unsigned char *) ps;
2053 if (p)
2054 while (*p)
2055 hash += *p++;
2057 return hash;
2060 /* Hash an rtx. We are careful to make sure the value is never negative.
2061 Equivalent registers hash identically.
2062 MODE is used in hashing for CONST_INTs only;
2063 otherwise the mode of X is used.
2065 Store 1 in DO_NOT_RECORD_P if any subexpression is volatile.
2067 If HASH_ARG_IN_MEMORY_P is not NULL, store 1 in it if X contains
2068 a MEM rtx which does not have the RTX_UNCHANGING_P bit set.
2070 Note that cse_insn knows that the hash code of a MEM expression
2071 is just (int) MEM plus the hash code of the address. */
2073 unsigned
2074 hash_rtx (const_rtx x, enum machine_mode mode, int *do_not_record_p,
2075 int *hash_arg_in_memory_p, bool have_reg_qty)
2077 int i, j;
2078 unsigned hash = 0;
2079 enum rtx_code code;
2080 const char *fmt;
2082 /* Used to turn recursion into iteration. We can't rely on GCC's
2083 tail-recursion elimination since we need to keep accumulating values
2084 in HASH. */
2085 repeat:
2086 if (x == 0)
2087 return hash;
2089 code = GET_CODE (x);
2090 switch (code)
2092 case REG:
2094 unsigned int regno = REGNO (x);
2096 if (!reload_completed)
2098 /* On some machines, we can't record any non-fixed hard register,
2099 because extending its life will cause reload problems. We
2100 consider ap, fp, sp, gp to be fixed for this purpose.
2102 We also consider CCmode registers to be fixed for this purpose;
2103 failure to do so leads to failure to simplify 0<100 type of
2104 conditionals.
2106 On all machines, we can't record any global registers.
2107 Nor should we record any register that is in a small
2108 class, as defined by CLASS_LIKELY_SPILLED_P. */
2109 bool record;
2111 if (regno >= FIRST_PSEUDO_REGISTER)
2112 record = true;
2113 else if (x == frame_pointer_rtx
2114 || x == hard_frame_pointer_rtx
2115 || x == arg_pointer_rtx
2116 || x == stack_pointer_rtx
2117 || x == pic_offset_table_rtx)
2118 record = true;
2119 else if (global_regs[regno])
2120 record = false;
2121 else if (fixed_regs[regno])
2122 record = true;
2123 else if (GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
2124 record = true;
2125 else if (SMALL_REGISTER_CLASSES)
2126 record = false;
2127 else if (CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (regno)))
2128 record = false;
2129 else
2130 record = true;
2132 if (!record)
2134 *do_not_record_p = 1;
2135 return 0;
2139 hash += ((unsigned int) REG << 7);
2140 hash += (have_reg_qty ? (unsigned) REG_QTY (regno) : regno);
2141 return hash;
2144 /* We handle SUBREG of a REG specially because the underlying
2145 reg changes its hash value with every value change; we don't
2146 want to have to forget unrelated subregs when one subreg changes. */
2147 case SUBREG:
2149 if (REG_P (SUBREG_REG (x)))
2151 hash += (((unsigned int) SUBREG << 7)
2152 + REGNO (SUBREG_REG (x))
2153 + (SUBREG_BYTE (x) / UNITS_PER_WORD));
2154 return hash;
2156 break;
2159 case CONST_INT:
2160 hash += (((unsigned int) CONST_INT << 7) + (unsigned int) mode
2161 + (unsigned int) INTVAL (x));
2162 return hash;
2164 case CONST_DOUBLE:
2165 /* This is like the general case, except that it only counts
2166 the integers representing the constant. */
2167 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
2168 if (GET_MODE (x) != VOIDmode)
2169 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
2170 else
2171 hash += ((unsigned int) CONST_DOUBLE_LOW (x)
2172 + (unsigned int) CONST_DOUBLE_HIGH (x));
2173 return hash;
2175 case CONST_FIXED:
2176 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
2177 hash += fixed_hash (CONST_FIXED_VALUE (x));
2178 return hash;
2180 case CONST_VECTOR:
2182 int units;
2183 rtx elt;
2185 units = CONST_VECTOR_NUNITS (x);
2187 for (i = 0; i < units; ++i)
2189 elt = CONST_VECTOR_ELT (x, i);
2190 hash += hash_rtx (elt, GET_MODE (elt), do_not_record_p,
2191 hash_arg_in_memory_p, have_reg_qty);
2194 return hash;
2197 /* Assume there is only one rtx object for any given label. */
2198 case LABEL_REF:
2199 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
2200 differences and differences between each stage's debugging dumps. */
2201 hash += (((unsigned int) LABEL_REF << 7)
2202 + CODE_LABEL_NUMBER (XEXP (x, 0)));
2203 return hash;
2205 case SYMBOL_REF:
2207 /* Don't hash on the symbol's address to avoid bootstrap differences.
2208 Different hash values may cause expressions to be recorded in
2209 different orders and thus different registers to be used in the
2210 final assembler. This also avoids differences in the dump files
2211 between various stages. */
2212 unsigned int h = 0;
2213 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
2215 while (*p)
2216 h += (h << 7) + *p++; /* ??? revisit */
2218 hash += ((unsigned int) SYMBOL_REF << 7) + h;
2219 return hash;
2222 case MEM:
2223 /* We don't record if marked volatile or if BLKmode since we don't
2224 know the size of the move. */
2225 if (MEM_VOLATILE_P (x) || GET_MODE (x) == BLKmode)
2227 *do_not_record_p = 1;
2228 return 0;
2230 if (hash_arg_in_memory_p && !MEM_READONLY_P (x))
2231 *hash_arg_in_memory_p = 1;
2233 /* Now that we have already found this special case,
2234 might as well speed it up as much as possible. */
2235 hash += (unsigned) MEM;
2236 x = XEXP (x, 0);
2237 goto repeat;
2239 case USE:
2240 /* A USE that mentions non-volatile memory needs special
2241 handling since the MEM may be BLKmode which normally
2242 prevents an entry from being made. Pure calls are
2243 marked by a USE which mentions BLKmode memory.
2244 See calls.c:emit_call_1. */
2245 if (MEM_P (XEXP (x, 0))
2246 && ! MEM_VOLATILE_P (XEXP (x, 0)))
2248 hash += (unsigned) USE;
2249 x = XEXP (x, 0);
2251 if (hash_arg_in_memory_p && !MEM_READONLY_P (x))
2252 *hash_arg_in_memory_p = 1;
2254 /* Now that we have already found this special case,
2255 might as well speed it up as much as possible. */
2256 hash += (unsigned) MEM;
2257 x = XEXP (x, 0);
2258 goto repeat;
2260 break;
2262 case PRE_DEC:
2263 case PRE_INC:
2264 case POST_DEC:
2265 case POST_INC:
2266 case PRE_MODIFY:
2267 case POST_MODIFY:
2268 case PC:
2269 case CC0:
2270 case CALL:
2271 case UNSPEC_VOLATILE:
2272 *do_not_record_p = 1;
2273 return 0;
2275 case ASM_OPERANDS:
2276 if (MEM_VOLATILE_P (x))
2278 *do_not_record_p = 1;
2279 return 0;
2281 else
2283 /* We don't want to take the filename and line into account. */
2284 hash += (unsigned) code + (unsigned) GET_MODE (x)
2285 + hash_rtx_string (ASM_OPERANDS_TEMPLATE (x))
2286 + hash_rtx_string (ASM_OPERANDS_OUTPUT_CONSTRAINT (x))
2287 + (unsigned) ASM_OPERANDS_OUTPUT_IDX (x);
2289 if (ASM_OPERANDS_INPUT_LENGTH (x))
2291 for (i = 1; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
2293 hash += (hash_rtx (ASM_OPERANDS_INPUT (x, i),
2294 GET_MODE (ASM_OPERANDS_INPUT (x, i)),
2295 do_not_record_p, hash_arg_in_memory_p,
2296 have_reg_qty)
2297 + hash_rtx_string
2298 (ASM_OPERANDS_INPUT_CONSTRAINT (x, i)));
2301 hash += hash_rtx_string (ASM_OPERANDS_INPUT_CONSTRAINT (x, 0));
2302 x = ASM_OPERANDS_INPUT (x, 0);
2303 mode = GET_MODE (x);
2304 goto repeat;
2307 return hash;
2309 break;
2311 default:
2312 break;
2315 i = GET_RTX_LENGTH (code) - 1;
2316 hash += (unsigned) code + (unsigned) GET_MODE (x);
2317 fmt = GET_RTX_FORMAT (code);
2318 for (; i >= 0; i--)
2320 switch (fmt[i])
2322 case 'e':
2323 /* If we are about to do the last recursive call
2324 needed at this level, change it into iteration.
2325 This function is called enough to be worth it. */
2326 if (i == 0)
2328 x = XEXP (x, i);
2329 goto repeat;
2332 hash += hash_rtx (XEXP (x, i), 0, do_not_record_p,
2333 hash_arg_in_memory_p, have_reg_qty);
2334 break;
2336 case 'E':
2337 for (j = 0; j < XVECLEN (x, i); j++)
2338 hash += hash_rtx (XVECEXP (x, i, j), 0, do_not_record_p,
2339 hash_arg_in_memory_p, have_reg_qty);
2340 break;
2342 case 's':
2343 hash += hash_rtx_string (XSTR (x, i));
2344 break;
2346 case 'i':
2347 hash += (unsigned int) XINT (x, i);
2348 break;
2350 case '0': case 't':
2351 /* Unused. */
2352 break;
2354 default:
2355 gcc_unreachable ();
2359 return hash;
2362 /* Hash an rtx X for cse via hash_rtx.
2363 Stores 1 in do_not_record if any subexpression is volatile.
2364 Stores 1 in hash_arg_in_memory if X contains a mem rtx which
2365 does not have the RTX_UNCHANGING_P bit set. */
2367 static inline unsigned
2368 canon_hash (rtx x, enum machine_mode mode)
2370 return hash_rtx (x, mode, &do_not_record, &hash_arg_in_memory, true);
2373 /* Like canon_hash but with no side effects, i.e. do_not_record
2374 and hash_arg_in_memory are not changed. */
2376 static inline unsigned
2377 safe_hash (rtx x, enum machine_mode mode)
2379 int dummy_do_not_record;
2380 return hash_rtx (x, mode, &dummy_do_not_record, NULL, true);
2383 /* Return 1 iff X and Y would canonicalize into the same thing,
2384 without actually constructing the canonicalization of either one.
2385 If VALIDATE is nonzero,
2386 we assume X is an expression being processed from the rtl
2387 and Y was found in the hash table. We check register refs
2388 in Y for being marked as valid.
2390 If FOR_GCSE is true, we compare X and Y for equivalence for GCSE. */
2393 exp_equiv_p (const_rtx x, const_rtx y, int validate, bool for_gcse)
2395 int i, j;
2396 enum rtx_code code;
2397 const char *fmt;
2399 /* Note: it is incorrect to assume an expression is equivalent to itself
2400 if VALIDATE is nonzero. */
2401 if (x == y && !validate)
2402 return 1;
2404 if (x == 0 || y == 0)
2405 return x == y;
2407 code = GET_CODE (x);
2408 if (code != GET_CODE (y))
2409 return 0;
2411 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
2412 if (GET_MODE (x) != GET_MODE (y))
2413 return 0;
2415 switch (code)
2417 case PC:
2418 case CC0:
2419 case CONST_INT:
2420 case CONST_DOUBLE:
2421 case CONST_FIXED:
2422 return x == y;
2424 case LABEL_REF:
2425 return XEXP (x, 0) == XEXP (y, 0);
2427 case SYMBOL_REF:
2428 return XSTR (x, 0) == XSTR (y, 0);
2430 case REG:
2431 if (for_gcse)
2432 return REGNO (x) == REGNO (y);
2433 else
2435 unsigned int regno = REGNO (y);
2436 unsigned int i;
2437 unsigned int endregno = END_REGNO (y);
2439 /* If the quantities are not the same, the expressions are not
2440 equivalent. If there are and we are not to validate, they
2441 are equivalent. Otherwise, ensure all regs are up-to-date. */
2443 if (REG_QTY (REGNO (x)) != REG_QTY (regno))
2444 return 0;
2446 if (! validate)
2447 return 1;
2449 for (i = regno; i < endregno; i++)
2450 if (REG_IN_TABLE (i) != REG_TICK (i))
2451 return 0;
2453 return 1;
2456 case MEM:
2457 if (for_gcse)
2459 /* A volatile mem should not be considered equivalent to any
2460 other. */
2461 if (MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
2462 return 0;
2464 /* Can't merge two expressions in different alias sets, since we
2465 can decide that the expression is transparent in a block when
2466 it isn't, due to it being set with the different alias set.
2468 Also, can't merge two expressions with different MEM_ATTRS.
2469 They could e.g. be two different entities allocated into the
2470 same space on the stack (see e.g. PR25130). In that case, the
2471 MEM addresses can be the same, even though the two MEMs are
2472 absolutely not equivalent.
2474 But because really all MEM attributes should be the same for
2475 equivalent MEMs, we just use the invariant that MEMs that have
2476 the same attributes share the same mem_attrs data structure. */
2477 if (MEM_ATTRS (x) != MEM_ATTRS (y))
2478 return 0;
2480 break;
2482 /* For commutative operations, check both orders. */
2483 case PLUS:
2484 case MULT:
2485 case AND:
2486 case IOR:
2487 case XOR:
2488 case NE:
2489 case EQ:
2490 return ((exp_equiv_p (XEXP (x, 0), XEXP (y, 0),
2491 validate, for_gcse)
2492 && exp_equiv_p (XEXP (x, 1), XEXP (y, 1),
2493 validate, for_gcse))
2494 || (exp_equiv_p (XEXP (x, 0), XEXP (y, 1),
2495 validate, for_gcse)
2496 && exp_equiv_p (XEXP (x, 1), XEXP (y, 0),
2497 validate, for_gcse)));
2499 case ASM_OPERANDS:
2500 /* We don't use the generic code below because we want to
2501 disregard filename and line numbers. */
2503 /* A volatile asm isn't equivalent to any other. */
2504 if (MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
2505 return 0;
2507 if (GET_MODE (x) != GET_MODE (y)
2508 || strcmp (ASM_OPERANDS_TEMPLATE (x), ASM_OPERANDS_TEMPLATE (y))
2509 || strcmp (ASM_OPERANDS_OUTPUT_CONSTRAINT (x),
2510 ASM_OPERANDS_OUTPUT_CONSTRAINT (y))
2511 || ASM_OPERANDS_OUTPUT_IDX (x) != ASM_OPERANDS_OUTPUT_IDX (y)
2512 || ASM_OPERANDS_INPUT_LENGTH (x) != ASM_OPERANDS_INPUT_LENGTH (y))
2513 return 0;
2515 if (ASM_OPERANDS_INPUT_LENGTH (x))
2517 for (i = ASM_OPERANDS_INPUT_LENGTH (x) - 1; i >= 0; i--)
2518 if (! exp_equiv_p (ASM_OPERANDS_INPUT (x, i),
2519 ASM_OPERANDS_INPUT (y, i),
2520 validate, for_gcse)
2521 || strcmp (ASM_OPERANDS_INPUT_CONSTRAINT (x, i),
2522 ASM_OPERANDS_INPUT_CONSTRAINT (y, i)))
2523 return 0;
2526 return 1;
2528 default:
2529 break;
2532 /* Compare the elements. If any pair of corresponding elements
2533 fail to match, return 0 for the whole thing. */
2535 fmt = GET_RTX_FORMAT (code);
2536 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2538 switch (fmt[i])
2540 case 'e':
2541 if (! exp_equiv_p (XEXP (x, i), XEXP (y, i),
2542 validate, for_gcse))
2543 return 0;
2544 break;
2546 case 'E':
2547 if (XVECLEN (x, i) != XVECLEN (y, i))
2548 return 0;
2549 for (j = 0; j < XVECLEN (x, i); j++)
2550 if (! exp_equiv_p (XVECEXP (x, i, j), XVECEXP (y, i, j),
2551 validate, for_gcse))
2552 return 0;
2553 break;
2555 case 's':
2556 if (strcmp (XSTR (x, i), XSTR (y, i)))
2557 return 0;
2558 break;
2560 case 'i':
2561 if (XINT (x, i) != XINT (y, i))
2562 return 0;
2563 break;
2565 case 'w':
2566 if (XWINT (x, i) != XWINT (y, i))
2567 return 0;
2568 break;
2570 case '0':
2571 case 't':
2572 break;
2574 default:
2575 gcc_unreachable ();
2579 return 1;
2582 /* Return 1 if X has a value that can vary even between two
2583 executions of the program. 0 means X can be compared reliably
2584 against certain constants or near-constants. */
2586 static bool
2587 cse_rtx_varies_p (const_rtx x, bool from_alias)
2589 /* We need not check for X and the equivalence class being of the same
2590 mode because if X is equivalent to a constant in some mode, it
2591 doesn't vary in any mode. */
2593 if (REG_P (x)
2594 && REGNO_QTY_VALID_P (REGNO (x)))
2596 int x_q = REG_QTY (REGNO (x));
2597 struct qty_table_elem *x_ent = &qty_table[x_q];
2599 if (GET_MODE (x) == x_ent->mode
2600 && x_ent->const_rtx != NULL_RTX)
2601 return 0;
2604 if (GET_CODE (x) == PLUS
2605 && GET_CODE (XEXP (x, 1)) == CONST_INT
2606 && REG_P (XEXP (x, 0))
2607 && REGNO_QTY_VALID_P (REGNO (XEXP (x, 0))))
2609 int x0_q = REG_QTY (REGNO (XEXP (x, 0)));
2610 struct qty_table_elem *x0_ent = &qty_table[x0_q];
2612 if ((GET_MODE (XEXP (x, 0)) == x0_ent->mode)
2613 && x0_ent->const_rtx != NULL_RTX)
2614 return 0;
2617 /* This can happen as the result of virtual register instantiation, if
2618 the initial constant is too large to be a valid address. This gives
2619 us a three instruction sequence, load large offset into a register,
2620 load fp minus a constant into a register, then a MEM which is the
2621 sum of the two `constant' registers. */
2622 if (GET_CODE (x) == PLUS
2623 && REG_P (XEXP (x, 0))
2624 && REG_P (XEXP (x, 1))
2625 && REGNO_QTY_VALID_P (REGNO (XEXP (x, 0)))
2626 && REGNO_QTY_VALID_P (REGNO (XEXP (x, 1))))
2628 int x0_q = REG_QTY (REGNO (XEXP (x, 0)));
2629 int x1_q = REG_QTY (REGNO (XEXP (x, 1)));
2630 struct qty_table_elem *x0_ent = &qty_table[x0_q];
2631 struct qty_table_elem *x1_ent = &qty_table[x1_q];
2633 if ((GET_MODE (XEXP (x, 0)) == x0_ent->mode)
2634 && x0_ent->const_rtx != NULL_RTX
2635 && (GET_MODE (XEXP (x, 1)) == x1_ent->mode)
2636 && x1_ent->const_rtx != NULL_RTX)
2637 return 0;
2640 return rtx_varies_p (x, from_alias);
2643 /* Subroutine of canon_reg. Pass *XLOC through canon_reg, and validate
2644 the result if necessary. INSN is as for canon_reg. */
2646 static void
2647 validate_canon_reg (rtx *xloc, rtx insn)
2649 if (*xloc)
2651 rtx new = canon_reg (*xloc, insn);
2653 /* If replacing pseudo with hard reg or vice versa, ensure the
2654 insn remains valid. Likewise if the insn has MATCH_DUPs. */
2655 gcc_assert (insn && new);
2656 validate_change (insn, xloc, new, 1);
2660 /* Canonicalize an expression:
2661 replace each register reference inside it
2662 with the "oldest" equivalent register.
2664 If INSN is nonzero validate_change is used to ensure that INSN remains valid
2665 after we make our substitution. The calls are made with IN_GROUP nonzero
2666 so apply_change_group must be called upon the outermost return from this
2667 function (unless INSN is zero). The result of apply_change_group can
2668 generally be discarded since the changes we are making are optional. */
2670 static rtx
2671 canon_reg (rtx x, rtx insn)
2673 int i;
2674 enum rtx_code code;
2675 const char *fmt;
2677 if (x == 0)
2678 return x;
2680 code = GET_CODE (x);
2681 switch (code)
2683 case PC:
2684 case CC0:
2685 case CONST:
2686 case CONST_INT:
2687 case CONST_DOUBLE:
2688 case CONST_FIXED:
2689 case CONST_VECTOR:
2690 case SYMBOL_REF:
2691 case LABEL_REF:
2692 case ADDR_VEC:
2693 case ADDR_DIFF_VEC:
2694 return x;
2696 case REG:
2698 int first;
2699 int q;
2700 struct qty_table_elem *ent;
2702 /* Never replace a hard reg, because hard regs can appear
2703 in more than one machine mode, and we must preserve the mode
2704 of each occurrence. Also, some hard regs appear in
2705 MEMs that are shared and mustn't be altered. Don't try to
2706 replace any reg that maps to a reg of class NO_REGS. */
2707 if (REGNO (x) < FIRST_PSEUDO_REGISTER
2708 || ! REGNO_QTY_VALID_P (REGNO (x)))
2709 return x;
2711 q = REG_QTY (REGNO (x));
2712 ent = &qty_table[q];
2713 first = ent->first_reg;
2714 return (first >= FIRST_PSEUDO_REGISTER ? regno_reg_rtx[first]
2715 : REGNO_REG_CLASS (first) == NO_REGS ? x
2716 : gen_rtx_REG (ent->mode, first));
2719 default:
2720 break;
2723 fmt = GET_RTX_FORMAT (code);
2724 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2726 int j;
2728 if (fmt[i] == 'e')
2729 validate_canon_reg (&XEXP (x, i), insn);
2730 else if (fmt[i] == 'E')
2731 for (j = 0; j < XVECLEN (x, i); j++)
2732 validate_canon_reg (&XVECEXP (x, i, j), insn);
2735 return x;
2738 /* Given an operation (CODE, *PARG1, *PARG2), where code is a comparison
2739 operation (EQ, NE, GT, etc.), follow it back through the hash table and
2740 what values are being compared.
2742 *PARG1 and *PARG2 are updated to contain the rtx representing the values
2743 actually being compared. For example, if *PARG1 was (cc0) and *PARG2
2744 was (const_int 0), *PARG1 and *PARG2 will be set to the objects that were
2745 compared to produce cc0.
2747 The return value is the comparison operator and is either the code of
2748 A or the code corresponding to the inverse of the comparison. */
2750 static enum rtx_code
2751 find_comparison_args (enum rtx_code code, rtx *parg1, rtx *parg2,
2752 enum machine_mode *pmode1, enum machine_mode *pmode2)
2754 rtx arg1, arg2;
2756 arg1 = *parg1, arg2 = *parg2;
2758 /* If ARG2 is const0_rtx, see what ARG1 is equivalent to. */
2760 while (arg2 == CONST0_RTX (GET_MODE (arg1)))
2762 /* Set nonzero when we find something of interest. */
2763 rtx x = 0;
2764 int reverse_code = 0;
2765 struct table_elt *p = 0;
2767 /* If arg1 is a COMPARE, extract the comparison arguments from it.
2768 On machines with CC0, this is the only case that can occur, since
2769 fold_rtx will return the COMPARE or item being compared with zero
2770 when given CC0. */
2772 if (GET_CODE (arg1) == COMPARE && arg2 == const0_rtx)
2773 x = arg1;
2775 /* If ARG1 is a comparison operator and CODE is testing for
2776 STORE_FLAG_VALUE, get the inner arguments. */
2778 else if (COMPARISON_P (arg1))
2780 #ifdef FLOAT_STORE_FLAG_VALUE
2781 REAL_VALUE_TYPE fsfv;
2782 #endif
2784 if (code == NE
2785 || (GET_MODE_CLASS (GET_MODE (arg1)) == MODE_INT
2786 && code == LT && STORE_FLAG_VALUE == -1)
2787 #ifdef FLOAT_STORE_FLAG_VALUE
2788 || (SCALAR_FLOAT_MODE_P (GET_MODE (arg1))
2789 && (fsfv = FLOAT_STORE_FLAG_VALUE (GET_MODE (arg1)),
2790 REAL_VALUE_NEGATIVE (fsfv)))
2791 #endif
2793 x = arg1;
2794 else if (code == EQ
2795 || (GET_MODE_CLASS (GET_MODE (arg1)) == MODE_INT
2796 && code == GE && STORE_FLAG_VALUE == -1)
2797 #ifdef FLOAT_STORE_FLAG_VALUE
2798 || (SCALAR_FLOAT_MODE_P (GET_MODE (arg1))
2799 && (fsfv = FLOAT_STORE_FLAG_VALUE (GET_MODE (arg1)),
2800 REAL_VALUE_NEGATIVE (fsfv)))
2801 #endif
2803 x = arg1, reverse_code = 1;
2806 /* ??? We could also check for
2808 (ne (and (eq (...) (const_int 1))) (const_int 0))
2810 and related forms, but let's wait until we see them occurring. */
2812 if (x == 0)
2813 /* Look up ARG1 in the hash table and see if it has an equivalence
2814 that lets us see what is being compared. */
2815 p = lookup (arg1, SAFE_HASH (arg1, GET_MODE (arg1)), GET_MODE (arg1));
2816 if (p)
2818 p = p->first_same_value;
2820 /* If what we compare is already known to be constant, that is as
2821 good as it gets.
2822 We need to break the loop in this case, because otherwise we
2823 can have an infinite loop when looking at a reg that is known
2824 to be a constant which is the same as a comparison of a reg
2825 against zero which appears later in the insn stream, which in
2826 turn is constant and the same as the comparison of the first reg
2827 against zero... */
2828 if (p->is_const)
2829 break;
2832 for (; p; p = p->next_same_value)
2834 enum machine_mode inner_mode = GET_MODE (p->exp);
2835 #ifdef FLOAT_STORE_FLAG_VALUE
2836 REAL_VALUE_TYPE fsfv;
2837 #endif
2839 /* If the entry isn't valid, skip it. */
2840 if (! exp_equiv_p (p->exp, p->exp, 1, false))
2841 continue;
2843 if (GET_CODE (p->exp) == COMPARE
2844 /* Another possibility is that this machine has a compare insn
2845 that includes the comparison code. In that case, ARG1 would
2846 be equivalent to a comparison operation that would set ARG1 to
2847 either STORE_FLAG_VALUE or zero. If this is an NE operation,
2848 ORIG_CODE is the actual comparison being done; if it is an EQ,
2849 we must reverse ORIG_CODE. On machine with a negative value
2850 for STORE_FLAG_VALUE, also look at LT and GE operations. */
2851 || ((code == NE
2852 || (code == LT
2853 && GET_MODE_CLASS (inner_mode) == MODE_INT
2854 && (GET_MODE_BITSIZE (inner_mode)
2855 <= HOST_BITS_PER_WIDE_INT)
2856 && (STORE_FLAG_VALUE
2857 & ((HOST_WIDE_INT) 1
2858 << (GET_MODE_BITSIZE (inner_mode) - 1))))
2859 #ifdef FLOAT_STORE_FLAG_VALUE
2860 || (code == LT
2861 && SCALAR_FLOAT_MODE_P (inner_mode)
2862 && (fsfv = FLOAT_STORE_FLAG_VALUE (GET_MODE (arg1)),
2863 REAL_VALUE_NEGATIVE (fsfv)))
2864 #endif
2866 && COMPARISON_P (p->exp)))
2868 x = p->exp;
2869 break;
2871 else if ((code == EQ
2872 || (code == GE
2873 && GET_MODE_CLASS (inner_mode) == MODE_INT
2874 && (GET_MODE_BITSIZE (inner_mode)
2875 <= HOST_BITS_PER_WIDE_INT)
2876 && (STORE_FLAG_VALUE
2877 & ((HOST_WIDE_INT) 1
2878 << (GET_MODE_BITSIZE (inner_mode) - 1))))
2879 #ifdef FLOAT_STORE_FLAG_VALUE
2880 || (code == GE
2881 && SCALAR_FLOAT_MODE_P (inner_mode)
2882 && (fsfv = FLOAT_STORE_FLAG_VALUE (GET_MODE (arg1)),
2883 REAL_VALUE_NEGATIVE (fsfv)))
2884 #endif
2886 && COMPARISON_P (p->exp))
2888 reverse_code = 1;
2889 x = p->exp;
2890 break;
2893 /* If this non-trapping address, e.g. fp + constant, the
2894 equivalent is a better operand since it may let us predict
2895 the value of the comparison. */
2896 else if (!rtx_addr_can_trap_p (p->exp))
2898 arg1 = p->exp;
2899 continue;
2903 /* If we didn't find a useful equivalence for ARG1, we are done.
2904 Otherwise, set up for the next iteration. */
2905 if (x == 0)
2906 break;
2908 /* If we need to reverse the comparison, make sure that that is
2909 possible -- we can't necessarily infer the value of GE from LT
2910 with floating-point operands. */
2911 if (reverse_code)
2913 enum rtx_code reversed = reversed_comparison_code (x, NULL_RTX);
2914 if (reversed == UNKNOWN)
2915 break;
2916 else
2917 code = reversed;
2919 else if (COMPARISON_P (x))
2920 code = GET_CODE (x);
2921 arg1 = XEXP (x, 0), arg2 = XEXP (x, 1);
2924 /* Return our results. Return the modes from before fold_rtx
2925 because fold_rtx might produce const_int, and then it's too late. */
2926 *pmode1 = GET_MODE (arg1), *pmode2 = GET_MODE (arg2);
2927 *parg1 = fold_rtx (arg1, 0), *parg2 = fold_rtx (arg2, 0);
2929 return code;
2932 /* If X is a nontrivial arithmetic operation on an argument for which
2933 a constant value can be determined, return the result of operating
2934 on that value, as a constant. Otherwise, return X, possibly with
2935 one or more operands changed to a forward-propagated constant.
2937 If X is a register whose contents are known, we do NOT return
2938 those contents here; equiv_constant is called to perform that task.
2939 For SUBREGs and MEMs, we do that both here and in equiv_constant.
2941 INSN is the insn that we may be modifying. If it is 0, make a copy
2942 of X before modifying it. */
2944 static rtx
2945 fold_rtx (rtx x, rtx insn)
2947 enum rtx_code code;
2948 enum machine_mode mode;
2949 const char *fmt;
2950 int i;
2951 rtx new = 0;
2952 int changed = 0;
2954 /* Operands of X. */
2955 rtx folded_arg0;
2956 rtx folded_arg1;
2958 /* Constant equivalents of first three operands of X;
2959 0 when no such equivalent is known. */
2960 rtx const_arg0;
2961 rtx const_arg1;
2962 rtx const_arg2;
2964 /* The mode of the first operand of X. We need this for sign and zero
2965 extends. */
2966 enum machine_mode mode_arg0;
2968 if (x == 0)
2969 return x;
2971 /* Try to perform some initial simplifications on X. */
2972 code = GET_CODE (x);
2973 switch (code)
2975 case MEM:
2976 case SUBREG:
2977 if ((new = equiv_constant (x)) != NULL_RTX)
2978 return new;
2979 return x;
2981 case CONST:
2982 case CONST_INT:
2983 case CONST_DOUBLE:
2984 case CONST_FIXED:
2985 case CONST_VECTOR:
2986 case SYMBOL_REF:
2987 case LABEL_REF:
2988 case REG:
2989 case PC:
2990 /* No use simplifying an EXPR_LIST
2991 since they are used only for lists of args
2992 in a function call's REG_EQUAL note. */
2993 case EXPR_LIST:
2994 return x;
2996 #ifdef HAVE_cc0
2997 case CC0:
2998 return prev_insn_cc0;
2999 #endif
3001 case ASM_OPERANDS:
3002 if (insn)
3004 for (i = ASM_OPERANDS_INPUT_LENGTH (x) - 1; i >= 0; i--)
3005 validate_change (insn, &ASM_OPERANDS_INPUT (x, i),
3006 fold_rtx (ASM_OPERANDS_INPUT (x, i), insn), 0);
3008 return x;
3010 #ifdef NO_FUNCTION_CSE
3011 case CALL:
3012 if (CONSTANT_P (XEXP (XEXP (x, 0), 0)))
3013 return x;
3014 break;
3015 #endif
3017 /* Anything else goes through the loop below. */
3018 default:
3019 break;
3022 mode = GET_MODE (x);
3023 const_arg0 = 0;
3024 const_arg1 = 0;
3025 const_arg2 = 0;
3026 mode_arg0 = VOIDmode;
3028 /* Try folding our operands.
3029 Then see which ones have constant values known. */
3031 fmt = GET_RTX_FORMAT (code);
3032 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3033 if (fmt[i] == 'e')
3035 rtx folded_arg = XEXP (x, i), const_arg;
3036 enum machine_mode mode_arg = GET_MODE (folded_arg);
3038 switch (GET_CODE (folded_arg))
3040 case MEM:
3041 case REG:
3042 case SUBREG:
3043 const_arg = equiv_constant (folded_arg);
3044 break;
3046 case CONST:
3047 case CONST_INT:
3048 case SYMBOL_REF:
3049 case LABEL_REF:
3050 case CONST_DOUBLE:
3051 case CONST_FIXED:
3052 case CONST_VECTOR:
3053 const_arg = folded_arg;
3054 break;
3056 #ifdef HAVE_cc0
3057 case CC0:
3058 folded_arg = prev_insn_cc0;
3059 mode_arg = prev_insn_cc0_mode;
3060 const_arg = equiv_constant (folded_arg);
3061 break;
3062 #endif
3064 default:
3065 folded_arg = fold_rtx (folded_arg, insn);
3066 const_arg = equiv_constant (folded_arg);
3067 break;
3070 /* For the first three operands, see if the operand
3071 is constant or equivalent to a constant. */
3072 switch (i)
3074 case 0:
3075 folded_arg0 = folded_arg;
3076 const_arg0 = const_arg;
3077 mode_arg0 = mode_arg;
3078 break;
3079 case 1:
3080 folded_arg1 = folded_arg;
3081 const_arg1 = const_arg;
3082 break;
3083 case 2:
3084 const_arg2 = const_arg;
3085 break;
3088 /* Pick the least expensive of the argument and an equivalent constant
3089 argument. */
3090 if (const_arg != 0
3091 && const_arg != folded_arg
3092 && COST_IN (const_arg, code) <= COST_IN (folded_arg, code)
3094 /* It's not safe to substitute the operand of a conversion
3095 operator with a constant, as the conversion's identity
3096 depends upon the mode of its operand. This optimization
3097 is handled by the call to simplify_unary_operation. */
3098 && (GET_RTX_CLASS (code) != RTX_UNARY
3099 || GET_MODE (const_arg) == mode_arg0
3100 || (code != ZERO_EXTEND
3101 && code != SIGN_EXTEND
3102 && code != TRUNCATE
3103 && code != FLOAT_TRUNCATE
3104 && code != FLOAT_EXTEND
3105 && code != FLOAT
3106 && code != FIX
3107 && code != UNSIGNED_FLOAT
3108 && code != UNSIGNED_FIX)))
3109 folded_arg = const_arg;
3111 if (folded_arg == XEXP (x, i))
3112 continue;
3114 if (insn == NULL_RTX && !changed)
3115 x = copy_rtx (x);
3116 changed = 1;
3117 validate_unshare_change (insn, &XEXP (x, i), folded_arg, 1);
3120 if (changed)
3122 /* Canonicalize X if necessary, and keep const_argN and folded_argN
3123 consistent with the order in X. */
3124 if (canonicalize_change_group (insn, x))
3126 rtx tem;
3127 tem = const_arg0, const_arg0 = const_arg1, const_arg1 = tem;
3128 tem = folded_arg0, folded_arg0 = folded_arg1, folded_arg1 = tem;
3131 apply_change_group ();
3134 /* If X is an arithmetic operation, see if we can simplify it. */
3136 switch (GET_RTX_CLASS (code))
3138 case RTX_UNARY:
3140 int is_const = 0;
3142 /* We can't simplify extension ops unless we know the
3143 original mode. */
3144 if ((code == ZERO_EXTEND || code == SIGN_EXTEND)
3145 && mode_arg0 == VOIDmode)
3146 break;
3148 /* If we had a CONST, strip it off and put it back later if we
3149 fold. */
3150 if (const_arg0 != 0 && GET_CODE (const_arg0) == CONST)
3151 is_const = 1, const_arg0 = XEXP (const_arg0, 0);
3153 new = simplify_unary_operation (code, mode,
3154 const_arg0 ? const_arg0 : folded_arg0,
3155 mode_arg0);
3156 /* NEG of PLUS could be converted into MINUS, but that causes
3157 expressions of the form
3158 (CONST (MINUS (CONST_INT) (SYMBOL_REF)))
3159 which many ports mistakenly treat as LEGITIMATE_CONSTANT_P.
3160 FIXME: those ports should be fixed. */
3161 if (new != 0 && is_const
3162 && GET_CODE (new) == PLUS
3163 && (GET_CODE (XEXP (new, 0)) == SYMBOL_REF
3164 || GET_CODE (XEXP (new, 0)) == LABEL_REF)
3165 && GET_CODE (XEXP (new, 1)) == CONST_INT)
3166 new = gen_rtx_CONST (mode, new);
3168 break;
3170 case RTX_COMPARE:
3171 case RTX_COMM_COMPARE:
3172 /* See what items are actually being compared and set FOLDED_ARG[01]
3173 to those values and CODE to the actual comparison code. If any are
3174 constant, set CONST_ARG0 and CONST_ARG1 appropriately. We needn't
3175 do anything if both operands are already known to be constant. */
3177 /* ??? Vector mode comparisons are not supported yet. */
3178 if (VECTOR_MODE_P (mode))
3179 break;
3181 if (const_arg0 == 0 || const_arg1 == 0)
3183 struct table_elt *p0, *p1;
3184 rtx true_rtx = const_true_rtx, false_rtx = const0_rtx;
3185 enum machine_mode mode_arg1;
3187 #ifdef FLOAT_STORE_FLAG_VALUE
3188 if (SCALAR_FLOAT_MODE_P (mode))
3190 true_rtx = (CONST_DOUBLE_FROM_REAL_VALUE
3191 (FLOAT_STORE_FLAG_VALUE (mode), mode));
3192 false_rtx = CONST0_RTX (mode);
3194 #endif
3196 code = find_comparison_args (code, &folded_arg0, &folded_arg1,
3197 &mode_arg0, &mode_arg1);
3199 /* If the mode is VOIDmode or a MODE_CC mode, we don't know
3200 what kinds of things are being compared, so we can't do
3201 anything with this comparison. */
3203 if (mode_arg0 == VOIDmode || GET_MODE_CLASS (mode_arg0) == MODE_CC)
3204 break;
3206 const_arg0 = equiv_constant (folded_arg0);
3207 const_arg1 = equiv_constant (folded_arg1);
3209 /* If we do not now have two constants being compared, see
3210 if we can nevertheless deduce some things about the
3211 comparison. */
3212 if (const_arg0 == 0 || const_arg1 == 0)
3214 if (const_arg1 != NULL)
3216 rtx cheapest_simplification;
3217 int cheapest_cost;
3218 rtx simp_result;
3219 struct table_elt *p;
3221 /* See if we can find an equivalent of folded_arg0
3222 that gets us a cheaper expression, possibly a
3223 constant through simplifications. */
3224 p = lookup (folded_arg0, SAFE_HASH (folded_arg0, mode_arg0),
3225 mode_arg0);
3227 if (p != NULL)
3229 cheapest_simplification = x;
3230 cheapest_cost = COST (x);
3232 for (p = p->first_same_value; p != NULL; p = p->next_same_value)
3234 int cost;
3236 /* If the entry isn't valid, skip it. */
3237 if (! exp_equiv_p (p->exp, p->exp, 1, false))
3238 continue;
3240 /* Try to simplify using this equivalence. */
3241 simp_result
3242 = simplify_relational_operation (code, mode,
3243 mode_arg0,
3244 p->exp,
3245 const_arg1);
3247 if (simp_result == NULL)
3248 continue;
3250 cost = COST (simp_result);
3251 if (cost < cheapest_cost)
3253 cheapest_cost = cost;
3254 cheapest_simplification = simp_result;
3258 /* If we have a cheaper expression now, use that
3259 and try folding it further, from the top. */
3260 if (cheapest_simplification != x)
3261 return fold_rtx (copy_rtx (cheapest_simplification),
3262 insn);
3266 /* See if the two operands are the same. */
3268 if ((REG_P (folded_arg0)
3269 && REG_P (folded_arg1)
3270 && (REG_QTY (REGNO (folded_arg0))
3271 == REG_QTY (REGNO (folded_arg1))))
3272 || ((p0 = lookup (folded_arg0,
3273 SAFE_HASH (folded_arg0, mode_arg0),
3274 mode_arg0))
3275 && (p1 = lookup (folded_arg1,
3276 SAFE_HASH (folded_arg1, mode_arg0),
3277 mode_arg0))
3278 && p0->first_same_value == p1->first_same_value))
3279 folded_arg1 = folded_arg0;
3281 /* If FOLDED_ARG0 is a register, see if the comparison we are
3282 doing now is either the same as we did before or the reverse
3283 (we only check the reverse if not floating-point). */
3284 else if (REG_P (folded_arg0))
3286 int qty = REG_QTY (REGNO (folded_arg0));
3288 if (REGNO_QTY_VALID_P (REGNO (folded_arg0)))
3290 struct qty_table_elem *ent = &qty_table[qty];
3292 if ((comparison_dominates_p (ent->comparison_code, code)
3293 || (! FLOAT_MODE_P (mode_arg0)
3294 && comparison_dominates_p (ent->comparison_code,
3295 reverse_condition (code))))
3296 && (rtx_equal_p (ent->comparison_const, folded_arg1)
3297 || (const_arg1
3298 && rtx_equal_p (ent->comparison_const,
3299 const_arg1))
3300 || (REG_P (folded_arg1)
3301 && (REG_QTY (REGNO (folded_arg1)) == ent->comparison_qty))))
3302 return (comparison_dominates_p (ent->comparison_code, code)
3303 ? true_rtx : false_rtx);
3309 /* If we are comparing against zero, see if the first operand is
3310 equivalent to an IOR with a constant. If so, we may be able to
3311 determine the result of this comparison. */
3312 if (const_arg1 == const0_rtx && !const_arg0)
3314 rtx y = lookup_as_function (folded_arg0, IOR);
3315 rtx inner_const;
3317 if (y != 0
3318 && (inner_const = equiv_constant (XEXP (y, 1))) != 0
3319 && GET_CODE (inner_const) == CONST_INT
3320 && INTVAL (inner_const) != 0)
3321 folded_arg0 = gen_rtx_IOR (mode_arg0, XEXP (y, 0), inner_const);
3325 rtx op0 = const_arg0 ? const_arg0 : folded_arg0;
3326 rtx op1 = const_arg1 ? const_arg1 : folded_arg1;
3327 new = simplify_relational_operation (code, mode, mode_arg0, op0, op1);
3329 break;
3331 case RTX_BIN_ARITH:
3332 case RTX_COMM_ARITH:
3333 switch (code)
3335 case PLUS:
3336 /* If the second operand is a LABEL_REF, see if the first is a MINUS
3337 with that LABEL_REF as its second operand. If so, the result is
3338 the first operand of that MINUS. This handles switches with an
3339 ADDR_DIFF_VEC table. */
3340 if (const_arg1 && GET_CODE (const_arg1) == LABEL_REF)
3342 rtx y
3343 = GET_CODE (folded_arg0) == MINUS ? folded_arg0
3344 : lookup_as_function (folded_arg0, MINUS);
3346 if (y != 0 && GET_CODE (XEXP (y, 1)) == LABEL_REF
3347 && XEXP (XEXP (y, 1), 0) == XEXP (const_arg1, 0))
3348 return XEXP (y, 0);
3350 /* Now try for a CONST of a MINUS like the above. */
3351 if ((y = (GET_CODE (folded_arg0) == CONST ? folded_arg0
3352 : lookup_as_function (folded_arg0, CONST))) != 0
3353 && GET_CODE (XEXP (y, 0)) == MINUS
3354 && GET_CODE (XEXP (XEXP (y, 0), 1)) == LABEL_REF
3355 && XEXP (XEXP (XEXP (y, 0), 1), 0) == XEXP (const_arg1, 0))
3356 return XEXP (XEXP (y, 0), 0);
3359 /* Likewise if the operands are in the other order. */
3360 if (const_arg0 && GET_CODE (const_arg0) == LABEL_REF)
3362 rtx y
3363 = GET_CODE (folded_arg1) == MINUS ? folded_arg1
3364 : lookup_as_function (folded_arg1, MINUS);
3366 if (y != 0 && GET_CODE (XEXP (y, 1)) == LABEL_REF
3367 && XEXP (XEXP (y, 1), 0) == XEXP (const_arg0, 0))
3368 return XEXP (y, 0);
3370 /* Now try for a CONST of a MINUS like the above. */
3371 if ((y = (GET_CODE (folded_arg1) == CONST ? folded_arg1
3372 : lookup_as_function (folded_arg1, CONST))) != 0
3373 && GET_CODE (XEXP (y, 0)) == MINUS
3374 && GET_CODE (XEXP (XEXP (y, 0), 1)) == LABEL_REF
3375 && XEXP (XEXP (XEXP (y, 0), 1), 0) == XEXP (const_arg0, 0))
3376 return XEXP (XEXP (y, 0), 0);
3379 /* If second operand is a register equivalent to a negative
3380 CONST_INT, see if we can find a register equivalent to the
3381 positive constant. Make a MINUS if so. Don't do this for
3382 a non-negative constant since we might then alternate between
3383 choosing positive and negative constants. Having the positive
3384 constant previously-used is the more common case. Be sure
3385 the resulting constant is non-negative; if const_arg1 were
3386 the smallest negative number this would overflow: depending
3387 on the mode, this would either just be the same value (and
3388 hence not save anything) or be incorrect. */
3389 if (const_arg1 != 0 && GET_CODE (const_arg1) == CONST_INT
3390 && INTVAL (const_arg1) < 0
3391 /* This used to test
3393 -INTVAL (const_arg1) >= 0
3395 But The Sun V5.0 compilers mis-compiled that test. So
3396 instead we test for the problematic value in a more direct
3397 manner and hope the Sun compilers get it correct. */
3398 && INTVAL (const_arg1) !=
3399 ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1))
3400 && REG_P (folded_arg1))
3402 rtx new_const = GEN_INT (-INTVAL (const_arg1));
3403 struct table_elt *p
3404 = lookup (new_const, SAFE_HASH (new_const, mode), mode);
3406 if (p)
3407 for (p = p->first_same_value; p; p = p->next_same_value)
3408 if (REG_P (p->exp))
3409 return simplify_gen_binary (MINUS, mode, folded_arg0,
3410 canon_reg (p->exp, NULL_RTX));
3412 goto from_plus;
3414 case MINUS:
3415 /* If we have (MINUS Y C), see if Y is known to be (PLUS Z C2).
3416 If so, produce (PLUS Z C2-C). */
3417 if (const_arg1 != 0 && GET_CODE (const_arg1) == CONST_INT)
3419 rtx y = lookup_as_function (XEXP (x, 0), PLUS);
3420 if (y && GET_CODE (XEXP (y, 1)) == CONST_INT)
3421 return fold_rtx (plus_constant (copy_rtx (y),
3422 -INTVAL (const_arg1)),
3423 NULL_RTX);
3426 /* Fall through. */
3428 from_plus:
3429 case SMIN: case SMAX: case UMIN: case UMAX:
3430 case IOR: case AND: case XOR:
3431 case MULT:
3432 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3433 /* If we have (<op> <reg> <const_int>) for an associative OP and REG
3434 is known to be of similar form, we may be able to replace the
3435 operation with a combined operation. This may eliminate the
3436 intermediate operation if every use is simplified in this way.
3437 Note that the similar optimization done by combine.c only works
3438 if the intermediate operation's result has only one reference. */
3440 if (REG_P (folded_arg0)
3441 && const_arg1 && GET_CODE (const_arg1) == CONST_INT)
3443 int is_shift
3444 = (code == ASHIFT || code == ASHIFTRT || code == LSHIFTRT);
3445 rtx y, inner_const, new_const;
3446 enum rtx_code associate_code;
3448 if (is_shift
3449 && (INTVAL (const_arg1) >= GET_MODE_BITSIZE (mode)
3450 || INTVAL (const_arg1) < 0))
3452 if (SHIFT_COUNT_TRUNCATED)
3453 const_arg1 = GEN_INT (INTVAL (const_arg1)
3454 & (GET_MODE_BITSIZE (mode) - 1));
3455 else
3456 break;
3459 y = lookup_as_function (folded_arg0, code);
3460 if (y == 0)
3461 break;
3463 /* If we have compiled a statement like
3464 "if (x == (x & mask1))", and now are looking at
3465 "x & mask2", we will have a case where the first operand
3466 of Y is the same as our first operand. Unless we detect
3467 this case, an infinite loop will result. */
3468 if (XEXP (y, 0) == folded_arg0)
3469 break;
3471 inner_const = equiv_constant (fold_rtx (XEXP (y, 1), 0));
3472 if (!inner_const || GET_CODE (inner_const) != CONST_INT)
3473 break;
3475 /* Don't associate these operations if they are a PLUS with the
3476 same constant and it is a power of two. These might be doable
3477 with a pre- or post-increment. Similarly for two subtracts of
3478 identical powers of two with post decrement. */
3480 if (code == PLUS && const_arg1 == inner_const
3481 && ((HAVE_PRE_INCREMENT
3482 && exact_log2 (INTVAL (const_arg1)) >= 0)
3483 || (HAVE_POST_INCREMENT
3484 && exact_log2 (INTVAL (const_arg1)) >= 0)
3485 || (HAVE_PRE_DECREMENT
3486 && exact_log2 (- INTVAL (const_arg1)) >= 0)
3487 || (HAVE_POST_DECREMENT
3488 && exact_log2 (- INTVAL (const_arg1)) >= 0)))
3489 break;
3491 /* ??? Vector mode shifts by scalar
3492 shift operand are not supported yet. */
3493 if (is_shift && VECTOR_MODE_P (mode))
3494 break;
3496 if (is_shift
3497 && (INTVAL (inner_const) >= GET_MODE_BITSIZE (mode)
3498 || INTVAL (inner_const) < 0))
3500 if (SHIFT_COUNT_TRUNCATED)
3501 inner_const = GEN_INT (INTVAL (inner_const)
3502 & (GET_MODE_BITSIZE (mode) - 1));
3503 else
3504 break;
3507 /* Compute the code used to compose the constants. For example,
3508 A-C1-C2 is A-(C1 + C2), so if CODE == MINUS, we want PLUS. */
3510 associate_code = (is_shift || code == MINUS ? PLUS : code);
3512 new_const = simplify_binary_operation (associate_code, mode,
3513 const_arg1, inner_const);
3515 if (new_const == 0)
3516 break;
3518 /* If we are associating shift operations, don't let this
3519 produce a shift of the size of the object or larger.
3520 This could occur when we follow a sign-extend by a right
3521 shift on a machine that does a sign-extend as a pair
3522 of shifts. */
3524 if (is_shift
3525 && GET_CODE (new_const) == CONST_INT
3526 && INTVAL (new_const) >= GET_MODE_BITSIZE (mode))
3528 /* As an exception, we can turn an ASHIFTRT of this
3529 form into a shift of the number of bits - 1. */
3530 if (code == ASHIFTRT)
3531 new_const = GEN_INT (GET_MODE_BITSIZE (mode) - 1);
3532 else if (!side_effects_p (XEXP (y, 0)))
3533 return CONST0_RTX (mode);
3534 else
3535 break;
3538 y = copy_rtx (XEXP (y, 0));
3540 /* If Y contains our first operand (the most common way this
3541 can happen is if Y is a MEM), we would do into an infinite
3542 loop if we tried to fold it. So don't in that case. */
3544 if (! reg_mentioned_p (folded_arg0, y))
3545 y = fold_rtx (y, insn);
3547 return simplify_gen_binary (code, mode, y, new_const);
3549 break;
3551 case DIV: case UDIV:
3552 /* ??? The associative optimization performed immediately above is
3553 also possible for DIV and UDIV using associate_code of MULT.
3554 However, we would need extra code to verify that the
3555 multiplication does not overflow, that is, there is no overflow
3556 in the calculation of new_const. */
3557 break;
3559 default:
3560 break;
3563 new = simplify_binary_operation (code, mode,
3564 const_arg0 ? const_arg0 : folded_arg0,
3565 const_arg1 ? const_arg1 : folded_arg1);
3566 break;
3568 case RTX_OBJ:
3569 /* (lo_sum (high X) X) is simply X. */
3570 if (code == LO_SUM && const_arg0 != 0
3571 && GET_CODE (const_arg0) == HIGH
3572 && rtx_equal_p (XEXP (const_arg0, 0), const_arg1))
3573 return const_arg1;
3574 break;
3576 case RTX_TERNARY:
3577 case RTX_BITFIELD_OPS:
3578 new = simplify_ternary_operation (code, mode, mode_arg0,
3579 const_arg0 ? const_arg0 : folded_arg0,
3580 const_arg1 ? const_arg1 : folded_arg1,
3581 const_arg2 ? const_arg2 : XEXP (x, 2));
3582 break;
3584 default:
3585 break;
3588 return new ? new : x;
3591 /* Return a constant value currently equivalent to X.
3592 Return 0 if we don't know one. */
3594 static rtx
3595 equiv_constant (rtx x)
3597 if (REG_P (x)
3598 && REGNO_QTY_VALID_P (REGNO (x)))
3600 int x_q = REG_QTY (REGNO (x));
3601 struct qty_table_elem *x_ent = &qty_table[x_q];
3603 if (x_ent->const_rtx)
3604 x = gen_lowpart (GET_MODE (x), x_ent->const_rtx);
3607 if (x == 0 || CONSTANT_P (x))
3608 return x;
3610 if (GET_CODE (x) == SUBREG)
3612 rtx new;
3614 /* See if we previously assigned a constant value to this SUBREG. */
3615 if ((new = lookup_as_function (x, CONST_INT)) != 0
3616 || (new = lookup_as_function (x, CONST_DOUBLE)) != 0
3617 || (new = lookup_as_function (x, CONST_FIXED)) != 0)
3618 return new;
3620 if (REG_P (SUBREG_REG (x))
3621 && (new = equiv_constant (SUBREG_REG (x))) != 0)
3622 return simplify_subreg (GET_MODE (x), SUBREG_REG (x),
3623 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
3625 return 0;
3628 /* If X is a MEM, see if it is a constant-pool reference, or look it up in
3629 the hash table in case its value was seen before. */
3631 if (MEM_P (x))
3633 struct table_elt *elt;
3635 x = avoid_constant_pool_reference (x);
3636 if (CONSTANT_P (x))
3637 return x;
3639 elt = lookup (x, SAFE_HASH (x, GET_MODE (x)), GET_MODE (x));
3640 if (elt == 0)
3641 return 0;
3643 for (elt = elt->first_same_value; elt; elt = elt->next_same_value)
3644 if (elt->is_const && CONSTANT_P (elt->exp))
3645 return elt->exp;
3648 return 0;
3651 /* Given INSN, a jump insn, TAKEN indicates if we are following the
3652 "taken" branch.
3654 In certain cases, this can cause us to add an equivalence. For example,
3655 if we are following the taken case of
3656 if (i == 2)
3657 we can add the fact that `i' and '2' are now equivalent.
3659 In any case, we can record that this comparison was passed. If the same
3660 comparison is seen later, we will know its value. */
3662 static void
3663 record_jump_equiv (rtx insn, bool taken)
3665 int cond_known_true;
3666 rtx op0, op1;
3667 rtx set;
3668 enum machine_mode mode, mode0, mode1;
3669 int reversed_nonequality = 0;
3670 enum rtx_code code;
3672 /* Ensure this is the right kind of insn. */
3673 gcc_assert (any_condjump_p (insn));
3675 set = pc_set (insn);
3677 /* See if this jump condition is known true or false. */
3678 if (taken)
3679 cond_known_true = (XEXP (SET_SRC (set), 2) == pc_rtx);
3680 else
3681 cond_known_true = (XEXP (SET_SRC (set), 1) == pc_rtx);
3683 /* Get the type of comparison being done and the operands being compared.
3684 If we had to reverse a non-equality condition, record that fact so we
3685 know that it isn't valid for floating-point. */
3686 code = GET_CODE (XEXP (SET_SRC (set), 0));
3687 op0 = fold_rtx (XEXP (XEXP (SET_SRC (set), 0), 0), insn);
3688 op1 = fold_rtx (XEXP (XEXP (SET_SRC (set), 0), 1), insn);
3690 code = find_comparison_args (code, &op0, &op1, &mode0, &mode1);
3691 if (! cond_known_true)
3693 code = reversed_comparison_code_parts (code, op0, op1, insn);
3695 /* Don't remember if we can't find the inverse. */
3696 if (code == UNKNOWN)
3697 return;
3700 /* The mode is the mode of the non-constant. */
3701 mode = mode0;
3702 if (mode1 != VOIDmode)
3703 mode = mode1;
3705 record_jump_cond (code, mode, op0, op1, reversed_nonequality);
3708 /* Yet another form of subreg creation. In this case, we want something in
3709 MODE, and we should assume OP has MODE iff it is naturally modeless. */
3711 static rtx
3712 record_jump_cond_subreg (enum machine_mode mode, rtx op)
3714 enum machine_mode op_mode = GET_MODE (op);
3715 if (op_mode == mode || op_mode == VOIDmode)
3716 return op;
3717 return lowpart_subreg (mode, op, op_mode);
3720 /* We know that comparison CODE applied to OP0 and OP1 in MODE is true.
3721 REVERSED_NONEQUALITY is nonzero if CODE had to be swapped.
3722 Make any useful entries we can with that information. Called from
3723 above function and called recursively. */
3725 static void
3726 record_jump_cond (enum rtx_code code, enum machine_mode mode, rtx op0,
3727 rtx op1, int reversed_nonequality)
3729 unsigned op0_hash, op1_hash;
3730 int op0_in_memory, op1_in_memory;
3731 struct table_elt *op0_elt, *op1_elt;
3733 /* If OP0 and OP1 are known equal, and either is a paradoxical SUBREG,
3734 we know that they are also equal in the smaller mode (this is also
3735 true for all smaller modes whether or not there is a SUBREG, but
3736 is not worth testing for with no SUBREG). */
3738 /* Note that GET_MODE (op0) may not equal MODE. */
3739 if (code == EQ && GET_CODE (op0) == SUBREG
3740 && (GET_MODE_SIZE (GET_MODE (op0))
3741 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0)))))
3743 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
3744 rtx tem = record_jump_cond_subreg (inner_mode, op1);
3745 if (tem)
3746 record_jump_cond (code, mode, SUBREG_REG (op0), tem,
3747 reversed_nonequality);
3750 if (code == EQ && GET_CODE (op1) == SUBREG
3751 && (GET_MODE_SIZE (GET_MODE (op1))
3752 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op1)))))
3754 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op1));
3755 rtx tem = record_jump_cond_subreg (inner_mode, op0);
3756 if (tem)
3757 record_jump_cond (code, mode, SUBREG_REG (op1), tem,
3758 reversed_nonequality);
3761 /* Similarly, if this is an NE comparison, and either is a SUBREG
3762 making a smaller mode, we know the whole thing is also NE. */
3764 /* Note that GET_MODE (op0) may not equal MODE;
3765 if we test MODE instead, we can get an infinite recursion
3766 alternating between two modes each wider than MODE. */
3768 if (code == NE && GET_CODE (op0) == SUBREG
3769 && subreg_lowpart_p (op0)
3770 && (GET_MODE_SIZE (GET_MODE (op0))
3771 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0)))))
3773 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
3774 rtx tem = record_jump_cond_subreg (inner_mode, op1);
3775 if (tem)
3776 record_jump_cond (code, mode, SUBREG_REG (op0), tem,
3777 reversed_nonequality);
3780 if (code == NE && GET_CODE (op1) == SUBREG
3781 && subreg_lowpart_p (op1)
3782 && (GET_MODE_SIZE (GET_MODE (op1))
3783 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (op1)))))
3785 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op1));
3786 rtx tem = record_jump_cond_subreg (inner_mode, op0);
3787 if (tem)
3788 record_jump_cond (code, mode, SUBREG_REG (op1), tem,
3789 reversed_nonequality);
3792 /* Hash both operands. */
3794 do_not_record = 0;
3795 hash_arg_in_memory = 0;
3796 op0_hash = HASH (op0, mode);
3797 op0_in_memory = hash_arg_in_memory;
3799 if (do_not_record)
3800 return;
3802 do_not_record = 0;
3803 hash_arg_in_memory = 0;
3804 op1_hash = HASH (op1, mode);
3805 op1_in_memory = hash_arg_in_memory;
3807 if (do_not_record)
3808 return;
3810 /* Look up both operands. */
3811 op0_elt = lookup (op0, op0_hash, mode);
3812 op1_elt = lookup (op1, op1_hash, mode);
3814 /* If both operands are already equivalent or if they are not in the
3815 table but are identical, do nothing. */
3816 if ((op0_elt != 0 && op1_elt != 0
3817 && op0_elt->first_same_value == op1_elt->first_same_value)
3818 || op0 == op1 || rtx_equal_p (op0, op1))
3819 return;
3821 /* If we aren't setting two things equal all we can do is save this
3822 comparison. Similarly if this is floating-point. In the latter
3823 case, OP1 might be zero and both -0.0 and 0.0 are equal to it.
3824 If we record the equality, we might inadvertently delete code
3825 whose intent was to change -0 to +0. */
3827 if (code != EQ || FLOAT_MODE_P (GET_MODE (op0)))
3829 struct qty_table_elem *ent;
3830 int qty;
3832 /* If we reversed a floating-point comparison, if OP0 is not a
3833 register, or if OP1 is neither a register or constant, we can't
3834 do anything. */
3836 if (!REG_P (op1))
3837 op1 = equiv_constant (op1);
3839 if ((reversed_nonequality && FLOAT_MODE_P (mode))
3840 || !REG_P (op0) || op1 == 0)
3841 return;
3843 /* Put OP0 in the hash table if it isn't already. This gives it a
3844 new quantity number. */
3845 if (op0_elt == 0)
3847 if (insert_regs (op0, NULL, 0))
3849 rehash_using_reg (op0);
3850 op0_hash = HASH (op0, mode);
3852 /* If OP0 is contained in OP1, this changes its hash code
3853 as well. Faster to rehash than to check, except
3854 for the simple case of a constant. */
3855 if (! CONSTANT_P (op1))
3856 op1_hash = HASH (op1,mode);
3859 op0_elt = insert (op0, NULL, op0_hash, mode);
3860 op0_elt->in_memory = op0_in_memory;
3863 qty = REG_QTY (REGNO (op0));
3864 ent = &qty_table[qty];
3866 ent->comparison_code = code;
3867 if (REG_P (op1))
3869 /* Look it up again--in case op0 and op1 are the same. */
3870 op1_elt = lookup (op1, op1_hash, mode);
3872 /* Put OP1 in the hash table so it gets a new quantity number. */
3873 if (op1_elt == 0)
3875 if (insert_regs (op1, NULL, 0))
3877 rehash_using_reg (op1);
3878 op1_hash = HASH (op1, mode);
3881 op1_elt = insert (op1, NULL, op1_hash, mode);
3882 op1_elt->in_memory = op1_in_memory;
3885 ent->comparison_const = NULL_RTX;
3886 ent->comparison_qty = REG_QTY (REGNO (op1));
3888 else
3890 ent->comparison_const = op1;
3891 ent->comparison_qty = -1;
3894 return;
3897 /* If either side is still missing an equivalence, make it now,
3898 then merge the equivalences. */
3900 if (op0_elt == 0)
3902 if (insert_regs (op0, NULL, 0))
3904 rehash_using_reg (op0);
3905 op0_hash = HASH (op0, mode);
3908 op0_elt = insert (op0, NULL, op0_hash, mode);
3909 op0_elt->in_memory = op0_in_memory;
3912 if (op1_elt == 0)
3914 if (insert_regs (op1, NULL, 0))
3916 rehash_using_reg (op1);
3917 op1_hash = HASH (op1, mode);
3920 op1_elt = insert (op1, NULL, op1_hash, mode);
3921 op1_elt->in_memory = op1_in_memory;
3924 merge_equiv_classes (op0_elt, op1_elt);
3927 /* CSE processing for one instruction.
3928 First simplify sources and addresses of all assignments
3929 in the instruction, using previously-computed equivalents values.
3930 Then install the new sources and destinations in the table
3931 of available values. */
3933 /* Data on one SET contained in the instruction. */
3935 struct set
3937 /* The SET rtx itself. */
3938 rtx rtl;
3939 /* The SET_SRC of the rtx (the original value, if it is changing). */
3940 rtx src;
3941 /* The hash-table element for the SET_SRC of the SET. */
3942 struct table_elt *src_elt;
3943 /* Hash value for the SET_SRC. */
3944 unsigned src_hash;
3945 /* Hash value for the SET_DEST. */
3946 unsigned dest_hash;
3947 /* The SET_DEST, with SUBREG, etc., stripped. */
3948 rtx inner_dest;
3949 /* Nonzero if the SET_SRC is in memory. */
3950 char src_in_memory;
3951 /* Nonzero if the SET_SRC contains something
3952 whose value cannot be predicted and understood. */
3953 char src_volatile;
3954 /* Original machine mode, in case it becomes a CONST_INT.
3955 The size of this field should match the size of the mode
3956 field of struct rtx_def (see rtl.h). */
3957 ENUM_BITFIELD(machine_mode) mode : 8;
3958 /* A constant equivalent for SET_SRC, if any. */
3959 rtx src_const;
3960 /* Hash value of constant equivalent for SET_SRC. */
3961 unsigned src_const_hash;
3962 /* Table entry for constant equivalent for SET_SRC, if any. */
3963 struct table_elt *src_const_elt;
3964 /* Table entry for the destination address. */
3965 struct table_elt *dest_addr_elt;
3968 static void
3969 cse_insn (rtx insn)
3971 rtx x = PATTERN (insn);
3972 int i;
3973 rtx tem;
3974 int n_sets = 0;
3976 rtx src_eqv = 0;
3977 struct table_elt *src_eqv_elt = 0;
3978 int src_eqv_volatile = 0;
3979 int src_eqv_in_memory = 0;
3980 unsigned src_eqv_hash = 0;
3982 struct set *sets = (struct set *) 0;
3984 this_insn = insn;
3985 #ifdef HAVE_cc0
3986 /* Records what this insn does to set CC0. */
3987 this_insn_cc0 = 0;
3988 this_insn_cc0_mode = VOIDmode;
3989 #endif
3991 /* Find all the SETs and CLOBBERs in this instruction.
3992 Record all the SETs in the array `set' and count them.
3993 Also determine whether there is a CLOBBER that invalidates
3994 all memory references, or all references at varying addresses. */
3996 if (CALL_P (insn))
3998 for (tem = CALL_INSN_FUNCTION_USAGE (insn); tem; tem = XEXP (tem, 1))
4000 if (GET_CODE (XEXP (tem, 0)) == CLOBBER)
4001 invalidate (SET_DEST (XEXP (tem, 0)), VOIDmode);
4002 XEXP (tem, 0) = canon_reg (XEXP (tem, 0), insn);
4006 if (GET_CODE (x) == SET)
4008 sets = XALLOCA (struct set);
4009 sets[0].rtl = x;
4011 /* Ignore SETs that are unconditional jumps.
4012 They never need cse processing, so this does not hurt.
4013 The reason is not efficiency but rather
4014 so that we can test at the end for instructions
4015 that have been simplified to unconditional jumps
4016 and not be misled by unchanged instructions
4017 that were unconditional jumps to begin with. */
4018 if (SET_DEST (x) == pc_rtx
4019 && GET_CODE (SET_SRC (x)) == LABEL_REF)
4022 /* Don't count call-insns, (set (reg 0) (call ...)), as a set.
4023 The hard function value register is used only once, to copy to
4024 someplace else, so it isn't worth cse'ing (and on 80386 is unsafe)!
4025 Ensure we invalidate the destination register. On the 80386 no
4026 other code would invalidate it since it is a fixed_reg.
4027 We need not check the return of apply_change_group; see canon_reg. */
4029 else if (GET_CODE (SET_SRC (x)) == CALL)
4031 canon_reg (SET_SRC (x), insn);
4032 apply_change_group ();
4033 fold_rtx (SET_SRC (x), insn);
4034 invalidate (SET_DEST (x), VOIDmode);
4036 else
4037 n_sets = 1;
4039 else if (GET_CODE (x) == PARALLEL)
4041 int lim = XVECLEN (x, 0);
4043 sets = XALLOCAVEC (struct set, lim);
4045 /* Find all regs explicitly clobbered in this insn,
4046 and ensure they are not replaced with any other regs
4047 elsewhere in this insn.
4048 When a reg that is clobbered is also used for input,
4049 we should presume that that is for a reason,
4050 and we should not substitute some other register
4051 which is not supposed to be clobbered.
4052 Therefore, this loop cannot be merged into the one below
4053 because a CALL may precede a CLOBBER and refer to the
4054 value clobbered. We must not let a canonicalization do
4055 anything in that case. */
4056 for (i = 0; i < lim; i++)
4058 rtx y = XVECEXP (x, 0, i);
4059 if (GET_CODE (y) == CLOBBER)
4061 rtx clobbered = XEXP (y, 0);
4063 if (REG_P (clobbered)
4064 || GET_CODE (clobbered) == SUBREG)
4065 invalidate (clobbered, VOIDmode);
4066 else if (GET_CODE (clobbered) == STRICT_LOW_PART
4067 || GET_CODE (clobbered) == ZERO_EXTRACT)
4068 invalidate (XEXP (clobbered, 0), GET_MODE (clobbered));
4072 for (i = 0; i < lim; i++)
4074 rtx y = XVECEXP (x, 0, i);
4075 if (GET_CODE (y) == SET)
4077 /* As above, we ignore unconditional jumps and call-insns and
4078 ignore the result of apply_change_group. */
4079 if (GET_CODE (SET_SRC (y)) == CALL)
4081 canon_reg (SET_SRC (y), insn);
4082 apply_change_group ();
4083 fold_rtx (SET_SRC (y), insn);
4084 invalidate (SET_DEST (y), VOIDmode);
4086 else if (SET_DEST (y) == pc_rtx
4087 && GET_CODE (SET_SRC (y)) == LABEL_REF)
4089 else
4090 sets[n_sets++].rtl = y;
4092 else if (GET_CODE (y) == CLOBBER)
4094 /* If we clobber memory, canon the address.
4095 This does nothing when a register is clobbered
4096 because we have already invalidated the reg. */
4097 if (MEM_P (XEXP (y, 0)))
4098 canon_reg (XEXP (y, 0), insn);
4100 else if (GET_CODE (y) == USE
4101 && ! (REG_P (XEXP (y, 0))
4102 && REGNO (XEXP (y, 0)) < FIRST_PSEUDO_REGISTER))
4103 canon_reg (y, insn);
4104 else if (GET_CODE (y) == CALL)
4106 /* The result of apply_change_group can be ignored; see
4107 canon_reg. */
4108 canon_reg (y, insn);
4109 apply_change_group ();
4110 fold_rtx (y, insn);
4114 else if (GET_CODE (x) == CLOBBER)
4116 if (MEM_P (XEXP (x, 0)))
4117 canon_reg (XEXP (x, 0), insn);
4120 /* Canonicalize a USE of a pseudo register or memory location. */
4121 else if (GET_CODE (x) == USE
4122 && ! (REG_P (XEXP (x, 0))
4123 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER))
4124 canon_reg (XEXP (x, 0), insn);
4125 else if (GET_CODE (x) == CALL)
4127 /* The result of apply_change_group can be ignored; see canon_reg. */
4128 canon_reg (x, insn);
4129 apply_change_group ();
4130 fold_rtx (x, insn);
4133 /* Store the equivalent value in SRC_EQV, if different, or if the DEST
4134 is a STRICT_LOW_PART. The latter condition is necessary because SRC_EQV
4135 is handled specially for this case, and if it isn't set, then there will
4136 be no equivalence for the destination. */
4137 if (n_sets == 1 && REG_NOTES (insn) != 0
4138 && (tem = find_reg_note (insn, REG_EQUAL, NULL_RTX)) != 0
4139 && (! rtx_equal_p (XEXP (tem, 0), SET_SRC (sets[0].rtl))
4140 || GET_CODE (SET_DEST (sets[0].rtl)) == STRICT_LOW_PART))
4142 /* The result of apply_change_group can be ignored; see canon_reg. */
4143 canon_reg (XEXP (tem, 0), insn);
4144 apply_change_group ();
4145 src_eqv = fold_rtx (XEXP (tem, 0), insn);
4146 XEXP (tem, 0) = copy_rtx (src_eqv);
4147 df_notes_rescan (insn);
4150 /* Canonicalize sources and addresses of destinations.
4151 We do this in a separate pass to avoid problems when a MATCH_DUP is
4152 present in the insn pattern. In that case, we want to ensure that
4153 we don't break the duplicate nature of the pattern. So we will replace
4154 both operands at the same time. Otherwise, we would fail to find an
4155 equivalent substitution in the loop calling validate_change below.
4157 We used to suppress canonicalization of DEST if it appears in SRC,
4158 but we don't do this any more. */
4160 for (i = 0; i < n_sets; i++)
4162 rtx dest = SET_DEST (sets[i].rtl);
4163 rtx src = SET_SRC (sets[i].rtl);
4164 rtx new = canon_reg (src, insn);
4166 validate_change (insn, &SET_SRC (sets[i].rtl), new, 1);
4168 if (GET_CODE (dest) == ZERO_EXTRACT)
4170 validate_change (insn, &XEXP (dest, 1),
4171 canon_reg (XEXP (dest, 1), insn), 1);
4172 validate_change (insn, &XEXP (dest, 2),
4173 canon_reg (XEXP (dest, 2), insn), 1);
4176 while (GET_CODE (dest) == SUBREG
4177 || GET_CODE (dest) == ZERO_EXTRACT
4178 || GET_CODE (dest) == STRICT_LOW_PART)
4179 dest = XEXP (dest, 0);
4181 if (MEM_P (dest))
4182 canon_reg (dest, insn);
4185 /* Now that we have done all the replacements, we can apply the change
4186 group and see if they all work. Note that this will cause some
4187 canonicalizations that would have worked individually not to be applied
4188 because some other canonicalization didn't work, but this should not
4189 occur often.
4191 The result of apply_change_group can be ignored; see canon_reg. */
4193 apply_change_group ();
4195 /* Set sets[i].src_elt to the class each source belongs to.
4196 Detect assignments from or to volatile things
4197 and set set[i] to zero so they will be ignored
4198 in the rest of this function.
4200 Nothing in this loop changes the hash table or the register chains. */
4202 for (i = 0; i < n_sets; i++)
4204 rtx src, dest;
4205 rtx src_folded;
4206 struct table_elt *elt = 0, *p;
4207 enum machine_mode mode;
4208 rtx src_eqv_here;
4209 rtx src_const = 0;
4210 rtx src_related = 0;
4211 struct table_elt *src_const_elt = 0;
4212 int src_cost = MAX_COST;
4213 int src_eqv_cost = MAX_COST;
4214 int src_folded_cost = MAX_COST;
4215 int src_related_cost = MAX_COST;
4216 int src_elt_cost = MAX_COST;
4217 int src_regcost = MAX_COST;
4218 int src_eqv_regcost = MAX_COST;
4219 int src_folded_regcost = MAX_COST;
4220 int src_related_regcost = MAX_COST;
4221 int src_elt_regcost = MAX_COST;
4222 /* Set nonzero if we need to call force_const_mem on with the
4223 contents of src_folded before using it. */
4224 int src_folded_force_flag = 0;
4226 dest = SET_DEST (sets[i].rtl);
4227 src = SET_SRC (sets[i].rtl);
4229 /* If SRC is a constant that has no machine mode,
4230 hash it with the destination's machine mode.
4231 This way we can keep different modes separate. */
4233 mode = GET_MODE (src) == VOIDmode ? GET_MODE (dest) : GET_MODE (src);
4234 sets[i].mode = mode;
4236 if (src_eqv)
4238 enum machine_mode eqvmode = mode;
4239 if (GET_CODE (dest) == STRICT_LOW_PART)
4240 eqvmode = GET_MODE (SUBREG_REG (XEXP (dest, 0)));
4241 do_not_record = 0;
4242 hash_arg_in_memory = 0;
4243 src_eqv_hash = HASH (src_eqv, eqvmode);
4245 /* Find the equivalence class for the equivalent expression. */
4247 if (!do_not_record)
4248 src_eqv_elt = lookup (src_eqv, src_eqv_hash, eqvmode);
4250 src_eqv_volatile = do_not_record;
4251 src_eqv_in_memory = hash_arg_in_memory;
4254 /* If this is a STRICT_LOW_PART assignment, src_eqv corresponds to the
4255 value of the INNER register, not the destination. So it is not
4256 a valid substitution for the source. But save it for later. */
4257 if (GET_CODE (dest) == STRICT_LOW_PART)
4258 src_eqv_here = 0;
4259 else
4260 src_eqv_here = src_eqv;
4262 /* Simplify and foldable subexpressions in SRC. Then get the fully-
4263 simplified result, which may not necessarily be valid. */
4264 src_folded = fold_rtx (src, insn);
4266 #if 0
4267 /* ??? This caused bad code to be generated for the m68k port with -O2.
4268 Suppose src is (CONST_INT -1), and that after truncation src_folded
4269 is (CONST_INT 3). Suppose src_folded is then used for src_const.
4270 At the end we will add src and src_const to the same equivalence
4271 class. We now have 3 and -1 on the same equivalence class. This
4272 causes later instructions to be mis-optimized. */
4273 /* If storing a constant in a bitfield, pre-truncate the constant
4274 so we will be able to record it later. */
4275 if (GET_CODE (SET_DEST (sets[i].rtl)) == ZERO_EXTRACT)
4277 rtx width = XEXP (SET_DEST (sets[i].rtl), 1);
4279 if (GET_CODE (src) == CONST_INT
4280 && GET_CODE (width) == CONST_INT
4281 && INTVAL (width) < HOST_BITS_PER_WIDE_INT
4282 && (INTVAL (src) & ((HOST_WIDE_INT) (-1) << INTVAL (width))))
4283 src_folded
4284 = GEN_INT (INTVAL (src) & (((HOST_WIDE_INT) 1
4285 << INTVAL (width)) - 1));
4287 #endif
4289 /* Compute SRC's hash code, and also notice if it
4290 should not be recorded at all. In that case,
4291 prevent any further processing of this assignment. */
4292 do_not_record = 0;
4293 hash_arg_in_memory = 0;
4295 sets[i].src = src;
4296 sets[i].src_hash = HASH (src, mode);
4297 sets[i].src_volatile = do_not_record;
4298 sets[i].src_in_memory = hash_arg_in_memory;
4300 /* If SRC is a MEM, there is a REG_EQUIV note for SRC, and DEST is
4301 a pseudo, do not record SRC. Using SRC as a replacement for
4302 anything else will be incorrect in that situation. Note that
4303 this usually occurs only for stack slots, in which case all the
4304 RTL would be referring to SRC, so we don't lose any optimization
4305 opportunities by not having SRC in the hash table. */
4307 if (MEM_P (src)
4308 && find_reg_note (insn, REG_EQUIV, NULL_RTX) != 0
4309 && REG_P (dest)
4310 && REGNO (dest) >= FIRST_PSEUDO_REGISTER)
4311 sets[i].src_volatile = 1;
4313 #if 0
4314 /* It is no longer clear why we used to do this, but it doesn't
4315 appear to still be needed. So let's try without it since this
4316 code hurts cse'ing widened ops. */
4317 /* If source is a paradoxical subreg (such as QI treated as an SI),
4318 treat it as volatile. It may do the work of an SI in one context
4319 where the extra bits are not being used, but cannot replace an SI
4320 in general. */
4321 if (GET_CODE (src) == SUBREG
4322 && (GET_MODE_SIZE (GET_MODE (src))
4323 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))))
4324 sets[i].src_volatile = 1;
4325 #endif
4327 /* Locate all possible equivalent forms for SRC. Try to replace
4328 SRC in the insn with each cheaper equivalent.
4330 We have the following types of equivalents: SRC itself, a folded
4331 version, a value given in a REG_EQUAL note, or a value related
4332 to a constant.
4334 Each of these equivalents may be part of an additional class
4335 of equivalents (if more than one is in the table, they must be in
4336 the same class; we check for this).
4338 If the source is volatile, we don't do any table lookups.
4340 We note any constant equivalent for possible later use in a
4341 REG_NOTE. */
4343 if (!sets[i].src_volatile)
4344 elt = lookup (src, sets[i].src_hash, mode);
4346 sets[i].src_elt = elt;
4348 if (elt && src_eqv_here && src_eqv_elt)
4350 if (elt->first_same_value != src_eqv_elt->first_same_value)
4352 /* The REG_EQUAL is indicating that two formerly distinct
4353 classes are now equivalent. So merge them. */
4354 merge_equiv_classes (elt, src_eqv_elt);
4355 src_eqv_hash = HASH (src_eqv, elt->mode);
4356 src_eqv_elt = lookup (src_eqv, src_eqv_hash, elt->mode);
4359 src_eqv_here = 0;
4362 else if (src_eqv_elt)
4363 elt = src_eqv_elt;
4365 /* Try to find a constant somewhere and record it in `src_const'.
4366 Record its table element, if any, in `src_const_elt'. Look in
4367 any known equivalences first. (If the constant is not in the
4368 table, also set `sets[i].src_const_hash'). */
4369 if (elt)
4370 for (p = elt->first_same_value; p; p = p->next_same_value)
4371 if (p->is_const)
4373 src_const = p->exp;
4374 src_const_elt = elt;
4375 break;
4378 if (src_const == 0
4379 && (CONSTANT_P (src_folded)
4380 /* Consider (minus (label_ref L1) (label_ref L2)) as
4381 "constant" here so we will record it. This allows us
4382 to fold switch statements when an ADDR_DIFF_VEC is used. */
4383 || (GET_CODE (src_folded) == MINUS
4384 && GET_CODE (XEXP (src_folded, 0)) == LABEL_REF
4385 && GET_CODE (XEXP (src_folded, 1)) == LABEL_REF)))
4386 src_const = src_folded, src_const_elt = elt;
4387 else if (src_const == 0 && src_eqv_here && CONSTANT_P (src_eqv_here))
4388 src_const = src_eqv_here, src_const_elt = src_eqv_elt;
4390 /* If we don't know if the constant is in the table, get its
4391 hash code and look it up. */
4392 if (src_const && src_const_elt == 0)
4394 sets[i].src_const_hash = HASH (src_const, mode);
4395 src_const_elt = lookup (src_const, sets[i].src_const_hash, mode);
4398 sets[i].src_const = src_const;
4399 sets[i].src_const_elt = src_const_elt;
4401 /* If the constant and our source are both in the table, mark them as
4402 equivalent. Otherwise, if a constant is in the table but the source
4403 isn't, set ELT to it. */
4404 if (src_const_elt && elt
4405 && src_const_elt->first_same_value != elt->first_same_value)
4406 merge_equiv_classes (elt, src_const_elt);
4407 else if (src_const_elt && elt == 0)
4408 elt = src_const_elt;
4410 /* See if there is a register linearly related to a constant
4411 equivalent of SRC. */
4412 if (src_const
4413 && (GET_CODE (src_const) == CONST
4414 || (src_const_elt && src_const_elt->related_value != 0)))
4416 src_related = use_related_value (src_const, src_const_elt);
4417 if (src_related)
4419 struct table_elt *src_related_elt
4420 = lookup (src_related, HASH (src_related, mode), mode);
4421 if (src_related_elt && elt)
4423 if (elt->first_same_value
4424 != src_related_elt->first_same_value)
4425 /* This can occur when we previously saw a CONST
4426 involving a SYMBOL_REF and then see the SYMBOL_REF
4427 twice. Merge the involved classes. */
4428 merge_equiv_classes (elt, src_related_elt);
4430 src_related = 0;
4431 src_related_elt = 0;
4433 else if (src_related_elt && elt == 0)
4434 elt = src_related_elt;
4438 /* See if we have a CONST_INT that is already in a register in a
4439 wider mode. */
4441 if (src_const && src_related == 0 && GET_CODE (src_const) == CONST_INT
4442 && GET_MODE_CLASS (mode) == MODE_INT
4443 && GET_MODE_BITSIZE (mode) < BITS_PER_WORD)
4445 enum machine_mode wider_mode;
4447 for (wider_mode = GET_MODE_WIDER_MODE (mode);
4448 GET_MODE_BITSIZE (wider_mode) <= BITS_PER_WORD
4449 && src_related == 0;
4450 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
4452 struct table_elt *const_elt
4453 = lookup (src_const, HASH (src_const, wider_mode), wider_mode);
4455 if (const_elt == 0)
4456 continue;
4458 for (const_elt = const_elt->first_same_value;
4459 const_elt; const_elt = const_elt->next_same_value)
4460 if (REG_P (const_elt->exp))
4462 src_related = gen_lowpart (mode, const_elt->exp);
4463 break;
4468 /* Another possibility is that we have an AND with a constant in
4469 a mode narrower than a word. If so, it might have been generated
4470 as part of an "if" which would narrow the AND. If we already
4471 have done the AND in a wider mode, we can use a SUBREG of that
4472 value. */
4474 if (flag_expensive_optimizations && ! src_related
4475 && GET_CODE (src) == AND && GET_CODE (XEXP (src, 1)) == CONST_INT
4476 && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
4478 enum machine_mode tmode;
4479 rtx new_and = gen_rtx_AND (VOIDmode, NULL_RTX, XEXP (src, 1));
4481 for (tmode = GET_MODE_WIDER_MODE (mode);
4482 GET_MODE_SIZE (tmode) <= UNITS_PER_WORD;
4483 tmode = GET_MODE_WIDER_MODE (tmode))
4485 rtx inner = gen_lowpart (tmode, XEXP (src, 0));
4486 struct table_elt *larger_elt;
4488 if (inner)
4490 PUT_MODE (new_and, tmode);
4491 XEXP (new_and, 0) = inner;
4492 larger_elt = lookup (new_and, HASH (new_and, tmode), tmode);
4493 if (larger_elt == 0)
4494 continue;
4496 for (larger_elt = larger_elt->first_same_value;
4497 larger_elt; larger_elt = larger_elt->next_same_value)
4498 if (REG_P (larger_elt->exp))
4500 src_related
4501 = gen_lowpart (mode, larger_elt->exp);
4502 break;
4505 if (src_related)
4506 break;
4511 #ifdef LOAD_EXTEND_OP
4512 /* See if a MEM has already been loaded with a widening operation;
4513 if it has, we can use a subreg of that. Many CISC machines
4514 also have such operations, but this is only likely to be
4515 beneficial on these machines. */
4517 if (flag_expensive_optimizations && src_related == 0
4518 && (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
4519 && GET_MODE_CLASS (mode) == MODE_INT
4520 && MEM_P (src) && ! do_not_record
4521 && LOAD_EXTEND_OP (mode) != UNKNOWN)
4523 struct rtx_def memory_extend_buf;
4524 rtx memory_extend_rtx = &memory_extend_buf;
4525 enum machine_mode tmode;
4527 /* Set what we are trying to extend and the operation it might
4528 have been extended with. */
4529 memset (memory_extend_rtx, 0, sizeof(*memory_extend_rtx));
4530 PUT_CODE (memory_extend_rtx, LOAD_EXTEND_OP (mode));
4531 XEXP (memory_extend_rtx, 0) = src;
4533 for (tmode = GET_MODE_WIDER_MODE (mode);
4534 GET_MODE_SIZE (tmode) <= UNITS_PER_WORD;
4535 tmode = GET_MODE_WIDER_MODE (tmode))
4537 struct table_elt *larger_elt;
4539 PUT_MODE (memory_extend_rtx, tmode);
4540 larger_elt = lookup (memory_extend_rtx,
4541 HASH (memory_extend_rtx, tmode), tmode);
4542 if (larger_elt == 0)
4543 continue;
4545 for (larger_elt = larger_elt->first_same_value;
4546 larger_elt; larger_elt = larger_elt->next_same_value)
4547 if (REG_P (larger_elt->exp))
4549 src_related = gen_lowpart (mode, larger_elt->exp);
4550 break;
4553 if (src_related)
4554 break;
4557 #endif /* LOAD_EXTEND_OP */
4559 if (src == src_folded)
4560 src_folded = 0;
4562 /* At this point, ELT, if nonzero, points to a class of expressions
4563 equivalent to the source of this SET and SRC, SRC_EQV, SRC_FOLDED,
4564 and SRC_RELATED, if nonzero, each contain additional equivalent
4565 expressions. Prune these latter expressions by deleting expressions
4566 already in the equivalence class.
4568 Check for an equivalent identical to the destination. If found,
4569 this is the preferred equivalent since it will likely lead to
4570 elimination of the insn. Indicate this by placing it in
4571 `src_related'. */
4573 if (elt)
4574 elt = elt->first_same_value;
4575 for (p = elt; p; p = p->next_same_value)
4577 enum rtx_code code = GET_CODE (p->exp);
4579 /* If the expression is not valid, ignore it. Then we do not
4580 have to check for validity below. In most cases, we can use
4581 `rtx_equal_p', since canonicalization has already been done. */
4582 if (code != REG && ! exp_equiv_p (p->exp, p->exp, 1, false))
4583 continue;
4585 /* Also skip paradoxical subregs, unless that's what we're
4586 looking for. */
4587 if (code == SUBREG
4588 && (GET_MODE_SIZE (GET_MODE (p->exp))
4589 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (p->exp))))
4590 && ! (src != 0
4591 && GET_CODE (src) == SUBREG
4592 && GET_MODE (src) == GET_MODE (p->exp)
4593 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
4594 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (p->exp))))))
4595 continue;
4597 if (src && GET_CODE (src) == code && rtx_equal_p (src, p->exp))
4598 src = 0;
4599 else if (src_folded && GET_CODE (src_folded) == code
4600 && rtx_equal_p (src_folded, p->exp))
4601 src_folded = 0;
4602 else if (src_eqv_here && GET_CODE (src_eqv_here) == code
4603 && rtx_equal_p (src_eqv_here, p->exp))
4604 src_eqv_here = 0;
4605 else if (src_related && GET_CODE (src_related) == code
4606 && rtx_equal_p (src_related, p->exp))
4607 src_related = 0;
4609 /* This is the same as the destination of the insns, we want
4610 to prefer it. Copy it to src_related. The code below will
4611 then give it a negative cost. */
4612 if (GET_CODE (dest) == code && rtx_equal_p (p->exp, dest))
4613 src_related = dest;
4616 /* Find the cheapest valid equivalent, trying all the available
4617 possibilities. Prefer items not in the hash table to ones
4618 that are when they are equal cost. Note that we can never
4619 worsen an insn as the current contents will also succeed.
4620 If we find an equivalent identical to the destination, use it as best,
4621 since this insn will probably be eliminated in that case. */
4622 if (src)
4624 if (rtx_equal_p (src, dest))
4625 src_cost = src_regcost = -1;
4626 else
4628 src_cost = COST (src);
4629 src_regcost = approx_reg_cost (src);
4633 if (src_eqv_here)
4635 if (rtx_equal_p (src_eqv_here, dest))
4636 src_eqv_cost = src_eqv_regcost = -1;
4637 else
4639 src_eqv_cost = COST (src_eqv_here);
4640 src_eqv_regcost = approx_reg_cost (src_eqv_here);
4644 if (src_folded)
4646 if (rtx_equal_p (src_folded, dest))
4647 src_folded_cost = src_folded_regcost = -1;
4648 else
4650 src_folded_cost = COST (src_folded);
4651 src_folded_regcost = approx_reg_cost (src_folded);
4655 if (src_related)
4657 if (rtx_equal_p (src_related, dest))
4658 src_related_cost = src_related_regcost = -1;
4659 else
4661 src_related_cost = COST (src_related);
4662 src_related_regcost = approx_reg_cost (src_related);
4666 /* If this was an indirect jump insn, a known label will really be
4667 cheaper even though it looks more expensive. */
4668 if (dest == pc_rtx && src_const && GET_CODE (src_const) == LABEL_REF)
4669 src_folded = src_const, src_folded_cost = src_folded_regcost = -1;
4671 /* Terminate loop when replacement made. This must terminate since
4672 the current contents will be tested and will always be valid. */
4673 while (1)
4675 rtx trial;
4677 /* Skip invalid entries. */
4678 while (elt && !REG_P (elt->exp)
4679 && ! exp_equiv_p (elt->exp, elt->exp, 1, false))
4680 elt = elt->next_same_value;
4682 /* A paradoxical subreg would be bad here: it'll be the right
4683 size, but later may be adjusted so that the upper bits aren't
4684 what we want. So reject it. */
4685 if (elt != 0
4686 && GET_CODE (elt->exp) == SUBREG
4687 && (GET_MODE_SIZE (GET_MODE (elt->exp))
4688 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (elt->exp))))
4689 /* It is okay, though, if the rtx we're trying to match
4690 will ignore any of the bits we can't predict. */
4691 && ! (src != 0
4692 && GET_CODE (src) == SUBREG
4693 && GET_MODE (src) == GET_MODE (elt->exp)
4694 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
4695 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (elt->exp))))))
4697 elt = elt->next_same_value;
4698 continue;
4701 if (elt)
4703 src_elt_cost = elt->cost;
4704 src_elt_regcost = elt->regcost;
4707 /* Find cheapest and skip it for the next time. For items
4708 of equal cost, use this order:
4709 src_folded, src, src_eqv, src_related and hash table entry. */
4710 if (src_folded
4711 && preferable (src_folded_cost, src_folded_regcost,
4712 src_cost, src_regcost) <= 0
4713 && preferable (src_folded_cost, src_folded_regcost,
4714 src_eqv_cost, src_eqv_regcost) <= 0
4715 && preferable (src_folded_cost, src_folded_regcost,
4716 src_related_cost, src_related_regcost) <= 0
4717 && preferable (src_folded_cost, src_folded_regcost,
4718 src_elt_cost, src_elt_regcost) <= 0)
4720 trial = src_folded, src_folded_cost = MAX_COST;
4721 if (src_folded_force_flag)
4723 rtx forced = force_const_mem (mode, trial);
4724 if (forced)
4725 trial = forced;
4728 else if (src
4729 && preferable (src_cost, src_regcost,
4730 src_eqv_cost, src_eqv_regcost) <= 0
4731 && preferable (src_cost, src_regcost,
4732 src_related_cost, src_related_regcost) <= 0
4733 && preferable (src_cost, src_regcost,
4734 src_elt_cost, src_elt_regcost) <= 0)
4735 trial = src, src_cost = MAX_COST;
4736 else if (src_eqv_here
4737 && preferable (src_eqv_cost, src_eqv_regcost,
4738 src_related_cost, src_related_regcost) <= 0
4739 && preferable (src_eqv_cost, src_eqv_regcost,
4740 src_elt_cost, src_elt_regcost) <= 0)
4741 trial = src_eqv_here, src_eqv_cost = MAX_COST;
4742 else if (src_related
4743 && preferable (src_related_cost, src_related_regcost,
4744 src_elt_cost, src_elt_regcost) <= 0)
4745 trial = src_related, src_related_cost = MAX_COST;
4746 else
4748 trial = elt->exp;
4749 elt = elt->next_same_value;
4750 src_elt_cost = MAX_COST;
4753 /* Avoid creation of overlapping memory moves. */
4754 if (MEM_P (trial) && MEM_P (SET_DEST (sets[i].rtl)))
4756 rtx src, dest;
4758 /* BLKmode moves are not handled by cse anyway. */
4759 if (GET_MODE (trial) == BLKmode)
4760 break;
4762 src = canon_rtx (trial);
4763 dest = canon_rtx (SET_DEST (sets[i].rtl));
4765 if (!MEM_P (src) || !MEM_P (dest)
4766 || !nonoverlapping_memrefs_p (src, dest))
4767 break;
4770 /* We don't normally have an insn matching (set (pc) (pc)), so
4771 check for this separately here. We will delete such an
4772 insn below.
4774 For other cases such as a table jump or conditional jump
4775 where we know the ultimate target, go ahead and replace the
4776 operand. While that may not make a valid insn, we will
4777 reemit the jump below (and also insert any necessary
4778 barriers). */
4779 if (n_sets == 1 && dest == pc_rtx
4780 && (trial == pc_rtx
4781 || (GET_CODE (trial) == LABEL_REF
4782 && ! condjump_p (insn))))
4784 /* Don't substitute non-local labels, this confuses CFG. */
4785 if (GET_CODE (trial) == LABEL_REF
4786 && LABEL_REF_NONLOCAL_P (trial))
4787 continue;
4789 SET_SRC (sets[i].rtl) = trial;
4790 cse_jumps_altered = true;
4791 break;
4794 /* Reject certain invalid forms of CONST that we create. */
4795 else if (CONSTANT_P (trial)
4796 && GET_CODE (trial) == CONST
4797 /* Reject cases that will cause decode_rtx_const to
4798 die. On the alpha when simplifying a switch, we
4799 get (const (truncate (minus (label_ref)
4800 (label_ref)))). */
4801 && (GET_CODE (XEXP (trial, 0)) == TRUNCATE
4802 /* Likewise on IA-64, except without the
4803 truncate. */
4804 || (GET_CODE (XEXP (trial, 0)) == MINUS
4805 && GET_CODE (XEXP (XEXP (trial, 0), 0)) == LABEL_REF
4806 && GET_CODE (XEXP (XEXP (trial, 0), 1)) == LABEL_REF)))
4807 /* Do nothing for this case. */
4810 /* Look for a substitution that makes a valid insn. */
4811 else if (validate_unshare_change
4812 (insn, &SET_SRC (sets[i].rtl), trial, 0))
4814 rtx new = canon_reg (SET_SRC (sets[i].rtl), insn);
4816 /* The result of apply_change_group can be ignored; see
4817 canon_reg. */
4819 validate_change (insn, &SET_SRC (sets[i].rtl), new, 1);
4820 apply_change_group ();
4822 break;
4825 /* If we previously found constant pool entries for
4826 constants and this is a constant, try making a
4827 pool entry. Put it in src_folded unless we already have done
4828 this since that is where it likely came from. */
4830 else if (constant_pool_entries_cost
4831 && CONSTANT_P (trial)
4832 && (src_folded == 0
4833 || (!MEM_P (src_folded)
4834 && ! src_folded_force_flag))
4835 && GET_MODE_CLASS (mode) != MODE_CC
4836 && mode != VOIDmode)
4838 src_folded_force_flag = 1;
4839 src_folded = trial;
4840 src_folded_cost = constant_pool_entries_cost;
4841 src_folded_regcost = constant_pool_entries_regcost;
4845 src = SET_SRC (sets[i].rtl);
4847 /* In general, it is good to have a SET with SET_SRC == SET_DEST.
4848 However, there is an important exception: If both are registers
4849 that are not the head of their equivalence class, replace SET_SRC
4850 with the head of the class. If we do not do this, we will have
4851 both registers live over a portion of the basic block. This way,
4852 their lifetimes will likely abut instead of overlapping. */
4853 if (REG_P (dest)
4854 && REGNO_QTY_VALID_P (REGNO (dest)))
4856 int dest_q = REG_QTY (REGNO (dest));
4857 struct qty_table_elem *dest_ent = &qty_table[dest_q];
4859 if (dest_ent->mode == GET_MODE (dest)
4860 && dest_ent->first_reg != REGNO (dest)
4861 && REG_P (src) && REGNO (src) == REGNO (dest)
4862 /* Don't do this if the original insn had a hard reg as
4863 SET_SRC or SET_DEST. */
4864 && (!REG_P (sets[i].src)
4865 || REGNO (sets[i].src) >= FIRST_PSEUDO_REGISTER)
4866 && (!REG_P (dest) || REGNO (dest) >= FIRST_PSEUDO_REGISTER))
4867 /* We can't call canon_reg here because it won't do anything if
4868 SRC is a hard register. */
4870 int src_q = REG_QTY (REGNO (src));
4871 struct qty_table_elem *src_ent = &qty_table[src_q];
4872 int first = src_ent->first_reg;
4873 rtx new_src
4874 = (first >= FIRST_PSEUDO_REGISTER
4875 ? regno_reg_rtx[first] : gen_rtx_REG (GET_MODE (src), first));
4877 /* We must use validate-change even for this, because this
4878 might be a special no-op instruction, suitable only to
4879 tag notes onto. */
4880 if (validate_change (insn, &SET_SRC (sets[i].rtl), new_src, 0))
4882 src = new_src;
4883 /* If we had a constant that is cheaper than what we are now
4884 setting SRC to, use that constant. We ignored it when we
4885 thought we could make this into a no-op. */
4886 if (src_const && COST (src_const) < COST (src)
4887 && validate_change (insn, &SET_SRC (sets[i].rtl),
4888 src_const, 0))
4889 src = src_const;
4894 /* If we made a change, recompute SRC values. */
4895 if (src != sets[i].src)
4897 do_not_record = 0;
4898 hash_arg_in_memory = 0;
4899 sets[i].src = src;
4900 sets[i].src_hash = HASH (src, mode);
4901 sets[i].src_volatile = do_not_record;
4902 sets[i].src_in_memory = hash_arg_in_memory;
4903 sets[i].src_elt = lookup (src, sets[i].src_hash, mode);
4906 /* If this is a single SET, we are setting a register, and we have an
4907 equivalent constant, we want to add a REG_NOTE. We don't want
4908 to write a REG_EQUAL note for a constant pseudo since verifying that
4909 that pseudo hasn't been eliminated is a pain. Such a note also
4910 won't help anything.
4912 Avoid a REG_EQUAL note for (CONST (MINUS (LABEL_REF) (LABEL_REF)))
4913 which can be created for a reference to a compile time computable
4914 entry in a jump table. */
4916 if (n_sets == 1 && src_const && REG_P (dest)
4917 && !REG_P (src_const)
4918 && ! (GET_CODE (src_const) == CONST
4919 && GET_CODE (XEXP (src_const, 0)) == MINUS
4920 && GET_CODE (XEXP (XEXP (src_const, 0), 0)) == LABEL_REF
4921 && GET_CODE (XEXP (XEXP (src_const, 0), 1)) == LABEL_REF))
4923 /* We only want a REG_EQUAL note if src_const != src. */
4924 if (! rtx_equal_p (src, src_const))
4926 /* Make sure that the rtx is not shared. */
4927 src_const = copy_rtx (src_const);
4929 /* Record the actual constant value in a REG_EQUAL note,
4930 making a new one if one does not already exist. */
4931 set_unique_reg_note (insn, REG_EQUAL, src_const);
4932 df_notes_rescan (insn);
4936 /* Now deal with the destination. */
4937 do_not_record = 0;
4939 /* Look within any ZERO_EXTRACT to the MEM or REG within it. */
4940 while (GET_CODE (dest) == SUBREG
4941 || GET_CODE (dest) == ZERO_EXTRACT
4942 || GET_CODE (dest) == STRICT_LOW_PART)
4943 dest = XEXP (dest, 0);
4945 sets[i].inner_dest = dest;
4947 if (MEM_P (dest))
4949 #ifdef PUSH_ROUNDING
4950 /* Stack pushes invalidate the stack pointer. */
4951 rtx addr = XEXP (dest, 0);
4952 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC
4953 && XEXP (addr, 0) == stack_pointer_rtx)
4954 invalidate (stack_pointer_rtx, VOIDmode);
4955 #endif
4956 dest = fold_rtx (dest, insn);
4959 /* Compute the hash code of the destination now,
4960 before the effects of this instruction are recorded,
4961 since the register values used in the address computation
4962 are those before this instruction. */
4963 sets[i].dest_hash = HASH (dest, mode);
4965 /* Don't enter a bit-field in the hash table
4966 because the value in it after the store
4967 may not equal what was stored, due to truncation. */
4969 if (GET_CODE (SET_DEST (sets[i].rtl)) == ZERO_EXTRACT)
4971 rtx width = XEXP (SET_DEST (sets[i].rtl), 1);
4973 if (src_const != 0 && GET_CODE (src_const) == CONST_INT
4974 && GET_CODE (width) == CONST_INT
4975 && INTVAL (width) < HOST_BITS_PER_WIDE_INT
4976 && ! (INTVAL (src_const)
4977 & ((HOST_WIDE_INT) (-1) << INTVAL (width))))
4978 /* Exception: if the value is constant,
4979 and it won't be truncated, record it. */
4981 else
4983 /* This is chosen so that the destination will be invalidated
4984 but no new value will be recorded.
4985 We must invalidate because sometimes constant
4986 values can be recorded for bitfields. */
4987 sets[i].src_elt = 0;
4988 sets[i].src_volatile = 1;
4989 src_eqv = 0;
4990 src_eqv_elt = 0;
4994 /* If only one set in a JUMP_INSN and it is now a no-op, we can delete
4995 the insn. */
4996 else if (n_sets == 1 && dest == pc_rtx && src == pc_rtx)
4998 /* One less use of the label this insn used to jump to. */
4999 delete_insn_and_edges (insn);
5000 cse_jumps_altered = true;
5001 /* No more processing for this set. */
5002 sets[i].rtl = 0;
5005 /* If this SET is now setting PC to a label, we know it used to
5006 be a conditional or computed branch. */
5007 else if (dest == pc_rtx && GET_CODE (src) == LABEL_REF
5008 && !LABEL_REF_NONLOCAL_P (src))
5010 /* We reemit the jump in as many cases as possible just in
5011 case the form of an unconditional jump is significantly
5012 different than a computed jump or conditional jump.
5014 If this insn has multiple sets, then reemitting the
5015 jump is nontrivial. So instead we just force rerecognition
5016 and hope for the best. */
5017 if (n_sets == 1)
5019 rtx new, note;
5021 new = emit_jump_insn_before (gen_jump (XEXP (src, 0)), insn);
5022 JUMP_LABEL (new) = XEXP (src, 0);
5023 LABEL_NUSES (XEXP (src, 0))++;
5025 /* Make sure to copy over REG_NON_LOCAL_GOTO. */
5026 note = find_reg_note (insn, REG_NON_LOCAL_GOTO, 0);
5027 if (note)
5029 XEXP (note, 1) = NULL_RTX;
5030 REG_NOTES (new) = note;
5033 delete_insn_and_edges (insn);
5034 insn = new;
5036 else
5037 INSN_CODE (insn) = -1;
5039 /* Do not bother deleting any unreachable code, let jump do it. */
5040 cse_jumps_altered = true;
5041 sets[i].rtl = 0;
5044 /* If destination is volatile, invalidate it and then do no further
5045 processing for this assignment. */
5047 else if (do_not_record)
5049 if (REG_P (dest) || GET_CODE (dest) == SUBREG)
5050 invalidate (dest, VOIDmode);
5051 else if (MEM_P (dest))
5052 invalidate (dest, VOIDmode);
5053 else if (GET_CODE (dest) == STRICT_LOW_PART
5054 || GET_CODE (dest) == ZERO_EXTRACT)
5055 invalidate (XEXP (dest, 0), GET_MODE (dest));
5056 sets[i].rtl = 0;
5059 if (sets[i].rtl != 0 && dest != SET_DEST (sets[i].rtl))
5060 sets[i].dest_hash = HASH (SET_DEST (sets[i].rtl), mode);
5062 #ifdef HAVE_cc0
5063 /* If setting CC0, record what it was set to, or a constant, if it
5064 is equivalent to a constant. If it is being set to a floating-point
5065 value, make a COMPARE with the appropriate constant of 0. If we
5066 don't do this, later code can interpret this as a test against
5067 const0_rtx, which can cause problems if we try to put it into an
5068 insn as a floating-point operand. */
5069 if (dest == cc0_rtx)
5071 this_insn_cc0 = src_const && mode != VOIDmode ? src_const : src;
5072 this_insn_cc0_mode = mode;
5073 if (FLOAT_MODE_P (mode))
5074 this_insn_cc0 = gen_rtx_COMPARE (VOIDmode, this_insn_cc0,
5075 CONST0_RTX (mode));
5077 #endif
5080 /* Now enter all non-volatile source expressions in the hash table
5081 if they are not already present.
5082 Record their equivalence classes in src_elt.
5083 This way we can insert the corresponding destinations into
5084 the same classes even if the actual sources are no longer in them
5085 (having been invalidated). */
5087 if (src_eqv && src_eqv_elt == 0 && sets[0].rtl != 0 && ! src_eqv_volatile
5088 && ! rtx_equal_p (src_eqv, SET_DEST (sets[0].rtl)))
5090 struct table_elt *elt;
5091 struct table_elt *classp = sets[0].src_elt;
5092 rtx dest = SET_DEST (sets[0].rtl);
5093 enum machine_mode eqvmode = GET_MODE (dest);
5095 if (GET_CODE (dest) == STRICT_LOW_PART)
5097 eqvmode = GET_MODE (SUBREG_REG (XEXP (dest, 0)));
5098 classp = 0;
5100 if (insert_regs (src_eqv, classp, 0))
5102 rehash_using_reg (src_eqv);
5103 src_eqv_hash = HASH (src_eqv, eqvmode);
5105 elt = insert (src_eqv, classp, src_eqv_hash, eqvmode);
5106 elt->in_memory = src_eqv_in_memory;
5107 src_eqv_elt = elt;
5109 /* Check to see if src_eqv_elt is the same as a set source which
5110 does not yet have an elt, and if so set the elt of the set source
5111 to src_eqv_elt. */
5112 for (i = 0; i < n_sets; i++)
5113 if (sets[i].rtl && sets[i].src_elt == 0
5114 && rtx_equal_p (SET_SRC (sets[i].rtl), src_eqv))
5115 sets[i].src_elt = src_eqv_elt;
5118 for (i = 0; i < n_sets; i++)
5119 if (sets[i].rtl && ! sets[i].src_volatile
5120 && ! rtx_equal_p (SET_SRC (sets[i].rtl), SET_DEST (sets[i].rtl)))
5122 if (GET_CODE (SET_DEST (sets[i].rtl)) == STRICT_LOW_PART)
5124 /* REG_EQUAL in setting a STRICT_LOW_PART
5125 gives an equivalent for the entire destination register,
5126 not just for the subreg being stored in now.
5127 This is a more interesting equivalence, so we arrange later
5128 to treat the entire reg as the destination. */
5129 sets[i].src_elt = src_eqv_elt;
5130 sets[i].src_hash = src_eqv_hash;
5132 else
5134 /* Insert source and constant equivalent into hash table, if not
5135 already present. */
5136 struct table_elt *classp = src_eqv_elt;
5137 rtx src = sets[i].src;
5138 rtx dest = SET_DEST (sets[i].rtl);
5139 enum machine_mode mode
5140 = GET_MODE (src) == VOIDmode ? GET_MODE (dest) : GET_MODE (src);
5142 /* It's possible that we have a source value known to be
5143 constant but don't have a REG_EQUAL note on the insn.
5144 Lack of a note will mean src_eqv_elt will be NULL. This
5145 can happen where we've generated a SUBREG to access a
5146 CONST_INT that is already in a register in a wider mode.
5147 Ensure that the source expression is put in the proper
5148 constant class. */
5149 if (!classp)
5150 classp = sets[i].src_const_elt;
5152 if (sets[i].src_elt == 0)
5154 struct table_elt *elt;
5156 /* Note that these insert_regs calls cannot remove
5157 any of the src_elt's, because they would have failed to
5158 match if not still valid. */
5159 if (insert_regs (src, classp, 0))
5161 rehash_using_reg (src);
5162 sets[i].src_hash = HASH (src, mode);
5164 elt = insert (src, classp, sets[i].src_hash, mode);
5165 elt->in_memory = sets[i].src_in_memory;
5166 sets[i].src_elt = classp = elt;
5168 if (sets[i].src_const && sets[i].src_const_elt == 0
5169 && src != sets[i].src_const
5170 && ! rtx_equal_p (sets[i].src_const, src))
5171 sets[i].src_elt = insert (sets[i].src_const, classp,
5172 sets[i].src_const_hash, mode);
5175 else if (sets[i].src_elt == 0)
5176 /* If we did not insert the source into the hash table (e.g., it was
5177 volatile), note the equivalence class for the REG_EQUAL value, if any,
5178 so that the destination goes into that class. */
5179 sets[i].src_elt = src_eqv_elt;
5181 /* Record destination addresses in the hash table. This allows us to
5182 check if they are invalidated by other sets. */
5183 for (i = 0; i < n_sets; i++)
5185 if (sets[i].rtl)
5187 rtx x = sets[i].inner_dest;
5188 struct table_elt *elt;
5189 enum machine_mode mode;
5190 unsigned hash;
5192 if (MEM_P (x))
5194 x = XEXP (x, 0);
5195 mode = GET_MODE (x);
5196 hash = HASH (x, mode);
5197 elt = lookup (x, hash, mode);
5198 if (!elt)
5200 if (insert_regs (x, NULL, 0))
5202 rtx dest = SET_DEST (sets[i].rtl);
5204 rehash_using_reg (x);
5205 hash = HASH (x, mode);
5206 sets[i].dest_hash = HASH (dest, GET_MODE (dest));
5208 elt = insert (x, NULL, hash, mode);
5211 sets[i].dest_addr_elt = elt;
5213 else
5214 sets[i].dest_addr_elt = NULL;
5218 invalidate_from_clobbers (x);
5220 /* Some registers are invalidated by subroutine calls. Memory is
5221 invalidated by non-constant calls. */
5223 if (CALL_P (insn))
5225 if (!(RTL_CONST_OR_PURE_CALL_P (insn)))
5226 invalidate_memory ();
5227 invalidate_for_call ();
5230 /* Now invalidate everything set by this instruction.
5231 If a SUBREG or other funny destination is being set,
5232 sets[i].rtl is still nonzero, so here we invalidate the reg
5233 a part of which is being set. */
5235 for (i = 0; i < n_sets; i++)
5236 if (sets[i].rtl)
5238 /* We can't use the inner dest, because the mode associated with
5239 a ZERO_EXTRACT is significant. */
5240 rtx dest = SET_DEST (sets[i].rtl);
5242 /* Needed for registers to remove the register from its
5243 previous quantity's chain.
5244 Needed for memory if this is a nonvarying address, unless
5245 we have just done an invalidate_memory that covers even those. */
5246 if (REG_P (dest) || GET_CODE (dest) == SUBREG)
5247 invalidate (dest, VOIDmode);
5248 else if (MEM_P (dest))
5249 invalidate (dest, VOIDmode);
5250 else if (GET_CODE (dest) == STRICT_LOW_PART
5251 || GET_CODE (dest) == ZERO_EXTRACT)
5252 invalidate (XEXP (dest, 0), GET_MODE (dest));
5255 /* A volatile ASM invalidates everything. */
5256 if (NONJUMP_INSN_P (insn)
5257 && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
5258 && MEM_VOLATILE_P (PATTERN (insn)))
5259 flush_hash_table ();
5261 /* Don't cse over a call to setjmp; on some machines (eg VAX)
5262 the regs restored by the longjmp come from a later time
5263 than the setjmp. */
5264 if (CALL_P (insn) && find_reg_note (insn, REG_SETJMP, NULL))
5266 flush_hash_table ();
5267 goto done;
5270 /* Make sure registers mentioned in destinations
5271 are safe for use in an expression to be inserted.
5272 This removes from the hash table
5273 any invalid entry that refers to one of these registers.
5275 We don't care about the return value from mention_regs because
5276 we are going to hash the SET_DEST values unconditionally. */
5278 for (i = 0; i < n_sets; i++)
5280 if (sets[i].rtl)
5282 rtx x = SET_DEST (sets[i].rtl);
5284 if (!REG_P (x))
5285 mention_regs (x);
5286 else
5288 /* We used to rely on all references to a register becoming
5289 inaccessible when a register changes to a new quantity,
5290 since that changes the hash code. However, that is not
5291 safe, since after HASH_SIZE new quantities we get a
5292 hash 'collision' of a register with its own invalid
5293 entries. And since SUBREGs have been changed not to
5294 change their hash code with the hash code of the register,
5295 it wouldn't work any longer at all. So we have to check
5296 for any invalid references lying around now.
5297 This code is similar to the REG case in mention_regs,
5298 but it knows that reg_tick has been incremented, and
5299 it leaves reg_in_table as -1 . */
5300 unsigned int regno = REGNO (x);
5301 unsigned int endregno = END_REGNO (x);
5302 unsigned int i;
5304 for (i = regno; i < endregno; i++)
5306 if (REG_IN_TABLE (i) >= 0)
5308 remove_invalid_refs (i);
5309 REG_IN_TABLE (i) = -1;
5316 /* We may have just removed some of the src_elt's from the hash table.
5317 So replace each one with the current head of the same class.
5318 Also check if destination addresses have been removed. */
5320 for (i = 0; i < n_sets; i++)
5321 if (sets[i].rtl)
5323 if (sets[i].dest_addr_elt
5324 && sets[i].dest_addr_elt->first_same_value == 0)
5326 /* The elt was removed, which means this destination is not
5327 valid after this instruction. */
5328 sets[i].rtl = NULL_RTX;
5330 else if (sets[i].src_elt && sets[i].src_elt->first_same_value == 0)
5331 /* If elt was removed, find current head of same class,
5332 or 0 if nothing remains of that class. */
5334 struct table_elt *elt = sets[i].src_elt;
5336 while (elt && elt->prev_same_value)
5337 elt = elt->prev_same_value;
5339 while (elt && elt->first_same_value == 0)
5340 elt = elt->next_same_value;
5341 sets[i].src_elt = elt ? elt->first_same_value : 0;
5345 /* Now insert the destinations into their equivalence classes. */
5347 for (i = 0; i < n_sets; i++)
5348 if (sets[i].rtl)
5350 rtx dest = SET_DEST (sets[i].rtl);
5351 struct table_elt *elt;
5353 /* Don't record value if we are not supposed to risk allocating
5354 floating-point values in registers that might be wider than
5355 memory. */
5356 if ((flag_float_store
5357 && MEM_P (dest)
5358 && FLOAT_MODE_P (GET_MODE (dest)))
5359 /* Don't record BLKmode values, because we don't know the
5360 size of it, and can't be sure that other BLKmode values
5361 have the same or smaller size. */
5362 || GET_MODE (dest) == BLKmode
5363 /* If we didn't put a REG_EQUAL value or a source into the hash
5364 table, there is no point is recording DEST. */
5365 || sets[i].src_elt == 0
5366 /* If DEST is a paradoxical SUBREG and SRC is a ZERO_EXTEND
5367 or SIGN_EXTEND, don't record DEST since it can cause
5368 some tracking to be wrong.
5370 ??? Think about this more later. */
5371 || (GET_CODE (dest) == SUBREG
5372 && (GET_MODE_SIZE (GET_MODE (dest))
5373 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))))
5374 && (GET_CODE (sets[i].src) == SIGN_EXTEND
5375 || GET_CODE (sets[i].src) == ZERO_EXTEND)))
5376 continue;
5378 /* STRICT_LOW_PART isn't part of the value BEING set,
5379 and neither is the SUBREG inside it.
5380 Note that in this case SETS[I].SRC_ELT is really SRC_EQV_ELT. */
5381 if (GET_CODE (dest) == STRICT_LOW_PART)
5382 dest = SUBREG_REG (XEXP (dest, 0));
5384 if (REG_P (dest) || GET_CODE (dest) == SUBREG)
5385 /* Registers must also be inserted into chains for quantities. */
5386 if (insert_regs (dest, sets[i].src_elt, 1))
5388 /* If `insert_regs' changes something, the hash code must be
5389 recalculated. */
5390 rehash_using_reg (dest);
5391 sets[i].dest_hash = HASH (dest, GET_MODE (dest));
5394 elt = insert (dest, sets[i].src_elt,
5395 sets[i].dest_hash, GET_MODE (dest));
5397 elt->in_memory = (MEM_P (sets[i].inner_dest)
5398 && !MEM_READONLY_P (sets[i].inner_dest));
5400 /* If we have (set (subreg:m1 (reg:m2 foo) 0) (bar:m1)), M1 is no
5401 narrower than M2, and both M1 and M2 are the same number of words,
5402 we are also doing (set (reg:m2 foo) (subreg:m2 (bar:m1) 0)) so
5403 make that equivalence as well.
5405 However, BAR may have equivalences for which gen_lowpart
5406 will produce a simpler value than gen_lowpart applied to
5407 BAR (e.g., if BAR was ZERO_EXTENDed from M2), so we will scan all
5408 BAR's equivalences. If we don't get a simplified form, make
5409 the SUBREG. It will not be used in an equivalence, but will
5410 cause two similar assignments to be detected.
5412 Note the loop below will find SUBREG_REG (DEST) since we have
5413 already entered SRC and DEST of the SET in the table. */
5415 if (GET_CODE (dest) == SUBREG
5416 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))) - 1)
5417 / UNITS_PER_WORD)
5418 == (GET_MODE_SIZE (GET_MODE (dest)) - 1) / UNITS_PER_WORD)
5419 && (GET_MODE_SIZE (GET_MODE (dest))
5420 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))))
5421 && sets[i].src_elt != 0)
5423 enum machine_mode new_mode = GET_MODE (SUBREG_REG (dest));
5424 struct table_elt *elt, *classp = 0;
5426 for (elt = sets[i].src_elt->first_same_value; elt;
5427 elt = elt->next_same_value)
5429 rtx new_src = 0;
5430 unsigned src_hash;
5431 struct table_elt *src_elt;
5432 int byte = 0;
5434 /* Ignore invalid entries. */
5435 if (!REG_P (elt->exp)
5436 && ! exp_equiv_p (elt->exp, elt->exp, 1, false))
5437 continue;
5439 /* We may have already been playing subreg games. If the
5440 mode is already correct for the destination, use it. */
5441 if (GET_MODE (elt->exp) == new_mode)
5442 new_src = elt->exp;
5443 else
5445 /* Calculate big endian correction for the SUBREG_BYTE.
5446 We have already checked that M1 (GET_MODE (dest))
5447 is not narrower than M2 (new_mode). */
5448 if (BYTES_BIG_ENDIAN)
5449 byte = (GET_MODE_SIZE (GET_MODE (dest))
5450 - GET_MODE_SIZE (new_mode));
5452 new_src = simplify_gen_subreg (new_mode, elt->exp,
5453 GET_MODE (dest), byte);
5456 /* The call to simplify_gen_subreg fails if the value
5457 is VOIDmode, yet we can't do any simplification, e.g.
5458 for EXPR_LISTs denoting function call results.
5459 It is invalid to construct a SUBREG with a VOIDmode
5460 SUBREG_REG, hence a zero new_src means we can't do
5461 this substitution. */
5462 if (! new_src)
5463 continue;
5465 src_hash = HASH (new_src, new_mode);
5466 src_elt = lookup (new_src, src_hash, new_mode);
5468 /* Put the new source in the hash table is if isn't
5469 already. */
5470 if (src_elt == 0)
5472 if (insert_regs (new_src, classp, 0))
5474 rehash_using_reg (new_src);
5475 src_hash = HASH (new_src, new_mode);
5477 src_elt = insert (new_src, classp, src_hash, new_mode);
5478 src_elt->in_memory = elt->in_memory;
5480 else if (classp && classp != src_elt->first_same_value)
5481 /* Show that two things that we've seen before are
5482 actually the same. */
5483 merge_equiv_classes (src_elt, classp);
5485 classp = src_elt->first_same_value;
5486 /* Ignore invalid entries. */
5487 while (classp
5488 && !REG_P (classp->exp)
5489 && ! exp_equiv_p (classp->exp, classp->exp, 1, false))
5490 classp = classp->next_same_value;
5495 /* Special handling for (set REG0 REG1) where REG0 is the
5496 "cheapest", cheaper than REG1. After cse, REG1 will probably not
5497 be used in the sequel, so (if easily done) change this insn to
5498 (set REG1 REG0) and replace REG1 with REG0 in the previous insn
5499 that computed their value. Then REG1 will become a dead store
5500 and won't cloud the situation for later optimizations.
5502 Do not make this change if REG1 is a hard register, because it will
5503 then be used in the sequel and we may be changing a two-operand insn
5504 into a three-operand insn.
5506 Also do not do this if we are operating on a copy of INSN. */
5508 if (n_sets == 1 && sets[0].rtl && REG_P (SET_DEST (sets[0].rtl))
5509 && NEXT_INSN (PREV_INSN (insn)) == insn
5510 && REG_P (SET_SRC (sets[0].rtl))
5511 && REGNO (SET_SRC (sets[0].rtl)) >= FIRST_PSEUDO_REGISTER
5512 && REGNO_QTY_VALID_P (REGNO (SET_SRC (sets[0].rtl))))
5514 int src_q = REG_QTY (REGNO (SET_SRC (sets[0].rtl)));
5515 struct qty_table_elem *src_ent = &qty_table[src_q];
5517 if (src_ent->first_reg == REGNO (SET_DEST (sets[0].rtl)))
5519 /* Scan for the previous nonnote insn, but stop at a basic
5520 block boundary. */
5521 rtx prev = insn;
5522 rtx bb_head = BB_HEAD (BLOCK_FOR_INSN (insn));
5525 prev = PREV_INSN (prev);
5527 while (prev != bb_head && NOTE_P (prev));
5529 /* Do not swap the registers around if the previous instruction
5530 attaches a REG_EQUIV note to REG1.
5532 ??? It's not entirely clear whether we can transfer a REG_EQUIV
5533 from the pseudo that originally shadowed an incoming argument
5534 to another register. Some uses of REG_EQUIV might rely on it
5535 being attached to REG1 rather than REG2.
5537 This section previously turned the REG_EQUIV into a REG_EQUAL
5538 note. We cannot do that because REG_EQUIV may provide an
5539 uninitialized stack slot when REG_PARM_STACK_SPACE is used. */
5540 if (NONJUMP_INSN_P (prev)
5541 && GET_CODE (PATTERN (prev)) == SET
5542 && SET_DEST (PATTERN (prev)) == SET_SRC (sets[0].rtl)
5543 && ! find_reg_note (prev, REG_EQUIV, NULL_RTX))
5545 rtx dest = SET_DEST (sets[0].rtl);
5546 rtx src = SET_SRC (sets[0].rtl);
5547 rtx note;
5549 validate_change (prev, &SET_DEST (PATTERN (prev)), dest, 1);
5550 validate_change (insn, &SET_DEST (sets[0].rtl), src, 1);
5551 validate_change (insn, &SET_SRC (sets[0].rtl), dest, 1);
5552 apply_change_group ();
5554 /* If INSN has a REG_EQUAL note, and this note mentions
5555 REG0, then we must delete it, because the value in
5556 REG0 has changed. If the note's value is REG1, we must
5557 also delete it because that is now this insn's dest. */
5558 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
5559 if (note != 0
5560 && (reg_mentioned_p (dest, XEXP (note, 0))
5561 || rtx_equal_p (src, XEXP (note, 0))))
5562 remove_note (insn, note);
5567 done:;
5570 /* Remove from the hash table all expressions that reference memory. */
5572 static void
5573 invalidate_memory (void)
5575 int i;
5576 struct table_elt *p, *next;
5578 for (i = 0; i < HASH_SIZE; i++)
5579 for (p = table[i]; p; p = next)
5581 next = p->next_same_hash;
5582 if (p->in_memory)
5583 remove_from_table (p, i);
5587 /* Perform invalidation on the basis of everything about an insn
5588 except for invalidating the actual places that are SET in it.
5589 This includes the places CLOBBERed, and anything that might
5590 alias with something that is SET or CLOBBERed.
5592 X is the pattern of the insn. */
5594 static void
5595 invalidate_from_clobbers (rtx x)
5597 if (GET_CODE (x) == CLOBBER)
5599 rtx ref = XEXP (x, 0);
5600 if (ref)
5602 if (REG_P (ref) || GET_CODE (ref) == SUBREG
5603 || MEM_P (ref))
5604 invalidate (ref, VOIDmode);
5605 else if (GET_CODE (ref) == STRICT_LOW_PART
5606 || GET_CODE (ref) == ZERO_EXTRACT)
5607 invalidate (XEXP (ref, 0), GET_MODE (ref));
5610 else if (GET_CODE (x) == PARALLEL)
5612 int i;
5613 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
5615 rtx y = XVECEXP (x, 0, i);
5616 if (GET_CODE (y) == CLOBBER)
5618 rtx ref = XEXP (y, 0);
5619 if (REG_P (ref) || GET_CODE (ref) == SUBREG
5620 || MEM_P (ref))
5621 invalidate (ref, VOIDmode);
5622 else if (GET_CODE (ref) == STRICT_LOW_PART
5623 || GET_CODE (ref) == ZERO_EXTRACT)
5624 invalidate (XEXP (ref, 0), GET_MODE (ref));
5630 /* Process X, part of the REG_NOTES of an insn. Look at any REG_EQUAL notes
5631 and replace any registers in them with either an equivalent constant
5632 or the canonical form of the register. If we are inside an address,
5633 only do this if the address remains valid.
5635 OBJECT is 0 except when within a MEM in which case it is the MEM.
5637 Return the replacement for X. */
5639 static rtx
5640 cse_process_notes_1 (rtx x, rtx object, bool *changed)
5642 enum rtx_code code = GET_CODE (x);
5643 const char *fmt = GET_RTX_FORMAT (code);
5644 int i;
5646 switch (code)
5648 case CONST_INT:
5649 case CONST:
5650 case SYMBOL_REF:
5651 case LABEL_REF:
5652 case CONST_DOUBLE:
5653 case CONST_FIXED:
5654 case CONST_VECTOR:
5655 case PC:
5656 case CC0:
5657 case LO_SUM:
5658 return x;
5660 case MEM:
5661 validate_change (x, &XEXP (x, 0),
5662 cse_process_notes (XEXP (x, 0), x, changed), 0);
5663 return x;
5665 case EXPR_LIST:
5666 case INSN_LIST:
5667 if (REG_NOTE_KIND (x) == REG_EQUAL)
5668 XEXP (x, 0) = cse_process_notes (XEXP (x, 0), NULL_RTX, changed);
5669 if (XEXP (x, 1))
5670 XEXP (x, 1) = cse_process_notes (XEXP (x, 1), NULL_RTX, changed);
5671 return x;
5673 case SIGN_EXTEND:
5674 case ZERO_EXTEND:
5675 case SUBREG:
5677 rtx new = cse_process_notes (XEXP (x, 0), object, changed);
5678 /* We don't substitute VOIDmode constants into these rtx,
5679 since they would impede folding. */
5680 if (GET_MODE (new) != VOIDmode)
5681 validate_change (object, &XEXP (x, 0), new, 0);
5682 return x;
5685 case REG:
5686 i = REG_QTY (REGNO (x));
5688 /* Return a constant or a constant register. */
5689 if (REGNO_QTY_VALID_P (REGNO (x)))
5691 struct qty_table_elem *ent = &qty_table[i];
5693 if (ent->const_rtx != NULL_RTX
5694 && (CONSTANT_P (ent->const_rtx)
5695 || REG_P (ent->const_rtx)))
5697 rtx new = gen_lowpart (GET_MODE (x), ent->const_rtx);
5698 if (new)
5699 return copy_rtx (new);
5703 /* Otherwise, canonicalize this register. */
5704 return canon_reg (x, NULL_RTX);
5706 default:
5707 break;
5710 for (i = 0; i < GET_RTX_LENGTH (code); i++)
5711 if (fmt[i] == 'e')
5712 validate_change (object, &XEXP (x, i),
5713 cse_process_notes (XEXP (x, i), object, changed), 0);
5715 return x;
5718 static rtx
5719 cse_process_notes (rtx x, rtx object, bool *changed)
5721 rtx new = cse_process_notes_1 (x, object, changed);
5722 if (new != x)
5723 *changed = true;
5724 return new;
5728 /* Find a path in the CFG, starting with FIRST_BB to perform CSE on.
5730 DATA is a pointer to a struct cse_basic_block_data, that is used to
5731 describe the path.
5732 It is filled with a queue of basic blocks, starting with FIRST_BB
5733 and following a trace through the CFG.
5735 If all paths starting at FIRST_BB have been followed, or no new path
5736 starting at FIRST_BB can be constructed, this function returns FALSE.
5737 Otherwise, DATA->path is filled and the function returns TRUE indicating
5738 that a path to follow was found.
5740 If FOLLOW_JUMPS is false, the maximum path length is 1 and the only
5741 block in the path will be FIRST_BB. */
5743 static bool
5744 cse_find_path (basic_block first_bb, struct cse_basic_block_data *data,
5745 int follow_jumps)
5747 basic_block bb;
5748 edge e;
5749 int path_size;
5751 SET_BIT (cse_visited_basic_blocks, first_bb->index);
5753 /* See if there is a previous path. */
5754 path_size = data->path_size;
5756 /* There is a previous path. Make sure it started with FIRST_BB. */
5757 if (path_size)
5758 gcc_assert (data->path[0].bb == first_bb);
5760 /* There was only one basic block in the last path. Clear the path and
5761 return, so that paths starting at another basic block can be tried. */
5762 if (path_size == 1)
5764 path_size = 0;
5765 goto done;
5768 /* If the path was empty from the beginning, construct a new path. */
5769 if (path_size == 0)
5770 data->path[path_size++].bb = first_bb;
5771 else
5773 /* Otherwise, path_size must be equal to or greater than 2, because
5774 a previous path exists that is at least two basic blocks long.
5776 Update the previous branch path, if any. If the last branch was
5777 previously along the branch edge, take the fallthrough edge now. */
5778 while (path_size >= 2)
5780 basic_block last_bb_in_path, previous_bb_in_path;
5781 edge e;
5783 --path_size;
5784 last_bb_in_path = data->path[path_size].bb;
5785 previous_bb_in_path = data->path[path_size - 1].bb;
5787 /* If we previously followed a path along the branch edge, try
5788 the fallthru edge now. */
5789 if (EDGE_COUNT (previous_bb_in_path->succs) == 2
5790 && any_condjump_p (BB_END (previous_bb_in_path))
5791 && (e = find_edge (previous_bb_in_path, last_bb_in_path))
5792 && e == BRANCH_EDGE (previous_bb_in_path))
5794 bb = FALLTHRU_EDGE (previous_bb_in_path)->dest;
5795 if (bb != EXIT_BLOCK_PTR
5796 && single_pred_p (bb)
5797 /* We used to assert here that we would only see blocks
5798 that we have not visited yet. But we may end up
5799 visiting basic blocks twice if the CFG has changed
5800 in this run of cse_main, because when the CFG changes
5801 the topological sort of the CFG also changes. A basic
5802 blocks that previously had more than two predecessors
5803 may now have a single predecessor, and become part of
5804 a path that starts at another basic block.
5806 We still want to visit each basic block only once, so
5807 halt the path here if we have already visited BB. */
5808 && !TEST_BIT (cse_visited_basic_blocks, bb->index))
5810 SET_BIT (cse_visited_basic_blocks, bb->index);
5811 data->path[path_size++].bb = bb;
5812 break;
5816 data->path[path_size].bb = NULL;
5819 /* If only one block remains in the path, bail. */
5820 if (path_size == 1)
5822 path_size = 0;
5823 goto done;
5827 /* Extend the path if possible. */
5828 if (follow_jumps)
5830 bb = data->path[path_size - 1].bb;
5831 while (bb && path_size < PARAM_VALUE (PARAM_MAX_CSE_PATH_LENGTH))
5833 if (single_succ_p (bb))
5834 e = single_succ_edge (bb);
5835 else if (EDGE_COUNT (bb->succs) == 2
5836 && any_condjump_p (BB_END (bb)))
5838 /* First try to follow the branch. If that doesn't lead
5839 to a useful path, follow the fallthru edge. */
5840 e = BRANCH_EDGE (bb);
5841 if (!single_pred_p (e->dest))
5842 e = FALLTHRU_EDGE (bb);
5844 else
5845 e = NULL;
5847 if (e && e->dest != EXIT_BLOCK_PTR
5848 && single_pred_p (e->dest)
5849 /* Avoid visiting basic blocks twice. The large comment
5850 above explains why this can happen. */
5851 && !TEST_BIT (cse_visited_basic_blocks, e->dest->index))
5853 basic_block bb2 = e->dest;
5854 SET_BIT (cse_visited_basic_blocks, bb2->index);
5855 data->path[path_size++].bb = bb2;
5856 bb = bb2;
5858 else
5859 bb = NULL;
5863 done:
5864 data->path_size = path_size;
5865 return path_size != 0;
5868 /* Dump the path in DATA to file F. NSETS is the number of sets
5869 in the path. */
5871 static void
5872 cse_dump_path (struct cse_basic_block_data *data, int nsets, FILE *f)
5874 int path_entry;
5876 fprintf (f, ";; Following path with %d sets: ", nsets);
5877 for (path_entry = 0; path_entry < data->path_size; path_entry++)
5878 fprintf (f, "%d ", (data->path[path_entry].bb)->index);
5879 fputc ('\n', dump_file);
5880 fflush (f);
5884 /* Return true if BB has exception handling successor edges. */
5886 static bool
5887 have_eh_succ_edges (basic_block bb)
5889 edge e;
5890 edge_iterator ei;
5892 FOR_EACH_EDGE (e, ei, bb->succs)
5893 if (e->flags & EDGE_EH)
5894 return true;
5896 return false;
5900 /* Scan to the end of the path described by DATA. Return an estimate of
5901 the total number of SETs of all insns in the path. */
5903 static void
5904 cse_prescan_path (struct cse_basic_block_data *data)
5906 int nsets = 0;
5907 int path_size = data->path_size;
5908 int path_entry;
5910 /* Scan to end of each basic block in the path. */
5911 for (path_entry = 0; path_entry < path_size; path_entry++)
5913 basic_block bb;
5914 rtx insn;
5916 bb = data->path[path_entry].bb;
5918 FOR_BB_INSNS (bb, insn)
5920 if (!INSN_P (insn))
5921 continue;
5923 /* A PARALLEL can have lots of SETs in it,
5924 especially if it is really an ASM_OPERANDS. */
5925 if (GET_CODE (PATTERN (insn)) == PARALLEL)
5926 nsets += XVECLEN (PATTERN (insn), 0);
5927 else
5928 nsets += 1;
5932 data->nsets = nsets;
5935 /* Process a single extended basic block described by EBB_DATA. */
5937 static void
5938 cse_extended_basic_block (struct cse_basic_block_data *ebb_data)
5940 int path_size = ebb_data->path_size;
5941 int path_entry;
5942 int num_insns = 0;
5944 /* Allocate the space needed by qty_table. */
5945 qty_table = XNEWVEC (struct qty_table_elem, max_qty);
5947 new_basic_block ();
5948 cse_ebb_live_in = df_get_live_in (ebb_data->path[0].bb);
5949 cse_ebb_live_out = df_get_live_out (ebb_data->path[path_size - 1].bb);
5950 for (path_entry = 0; path_entry < path_size; path_entry++)
5952 basic_block bb;
5953 rtx insn;
5955 bb = ebb_data->path[path_entry].bb;
5957 /* Invalidate recorded information for eh regs if there is an EH
5958 edge pointing to that bb. */
5959 if (bb_has_eh_pred (bb))
5961 struct df_ref **def_rec;
5963 for (def_rec = df_get_artificial_defs (bb->index); *def_rec; def_rec++)
5965 struct df_ref *def = *def_rec;
5966 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
5967 invalidate (DF_REF_REG (def), GET_MODE (DF_REF_REG (def)));
5971 FOR_BB_INSNS (bb, insn)
5973 /* If we have processed 1,000 insns, flush the hash table to
5974 avoid extreme quadratic behavior. We must not include NOTEs
5975 in the count since there may be more of them when generating
5976 debugging information. If we clear the table at different
5977 times, code generated with -g -O might be different than code
5978 generated with -O but not -g.
5980 FIXME: This is a real kludge and needs to be done some other
5981 way. */
5982 if (INSN_P (insn)
5983 && num_insns++ > PARAM_VALUE (PARAM_MAX_CSE_INSNS))
5985 flush_hash_table ();
5986 num_insns = 0;
5989 if (INSN_P (insn))
5991 /* Process notes first so we have all notes in canonical forms
5992 when looking for duplicate operations. */
5993 if (REG_NOTES (insn))
5995 bool changed = false;
5996 REG_NOTES (insn) = cse_process_notes (REG_NOTES (insn),
5997 NULL_RTX, &changed);
5998 if (changed)
5999 df_notes_rescan (insn);
6002 cse_insn (insn);
6004 /* If we haven't already found an insn where we added a LABEL_REF,
6005 check this one. */
6006 if (INSN_P (insn) && !recorded_label_ref
6007 && for_each_rtx (&PATTERN (insn), check_for_label_ref,
6008 (void *) insn))
6009 recorded_label_ref = true;
6011 #ifdef HAVE_cc0
6012 /* If the previous insn set CC0 and this insn no longer
6013 references CC0, delete the previous insn. Here we use
6014 fact that nothing expects CC0 to be valid over an insn,
6015 which is true until the final pass. */
6017 rtx prev_insn, tem;
6019 prev_insn = PREV_INSN (insn);
6020 if (prev_insn && NONJUMP_INSN_P (prev_insn)
6021 && (tem = single_set (prev_insn)) != 0
6022 && SET_DEST (tem) == cc0_rtx
6023 && ! reg_mentioned_p (cc0_rtx, PATTERN (insn)))
6024 delete_insn (prev_insn);
6027 /* If this insn is not the last insn in the basic block,
6028 it will be PREV_INSN(insn) in the next iteration. If
6029 we recorded any CC0-related information for this insn,
6030 remember it. */
6031 if (insn != BB_END (bb))
6033 prev_insn_cc0 = this_insn_cc0;
6034 prev_insn_cc0_mode = this_insn_cc0_mode;
6036 #endif
6040 /* With non-call exceptions, we are not always able to update
6041 the CFG properly inside cse_insn. So clean up possibly
6042 redundant EH edges here. */
6043 if (flag_non_call_exceptions && have_eh_succ_edges (bb))
6044 cse_cfg_altered |= purge_dead_edges (bb);
6046 /* If we changed a conditional jump, we may have terminated
6047 the path we are following. Check that by verifying that
6048 the edge we would take still exists. If the edge does
6049 not exist anymore, purge the remainder of the path.
6050 Note that this will cause us to return to the caller. */
6051 if (path_entry < path_size - 1)
6053 basic_block next_bb = ebb_data->path[path_entry + 1].bb;
6054 if (!find_edge (bb, next_bb))
6058 path_size--;
6060 /* If we truncate the path, we must also reset the
6061 visited bit on the remaining blocks in the path,
6062 or we will never visit them at all. */
6063 RESET_BIT (cse_visited_basic_blocks,
6064 ebb_data->path[path_size].bb->index);
6065 ebb_data->path[path_size].bb = NULL;
6067 while (path_size - 1 != path_entry);
6068 ebb_data->path_size = path_size;
6072 /* If this is a conditional jump insn, record any known
6073 equivalences due to the condition being tested. */
6074 insn = BB_END (bb);
6075 if (path_entry < path_size - 1
6076 && JUMP_P (insn)
6077 && single_set (insn)
6078 && any_condjump_p (insn))
6080 basic_block next_bb = ebb_data->path[path_entry + 1].bb;
6081 bool taken = (next_bb == BRANCH_EDGE (bb)->dest);
6082 record_jump_equiv (insn, taken);
6085 #ifdef HAVE_cc0
6086 /* Clear the CC0-tracking related insns, they can't provide
6087 useful information across basic block boundaries. */
6088 prev_insn_cc0 = 0;
6089 #endif
6092 gcc_assert (next_qty <= max_qty);
6094 free (qty_table);
6098 /* Perform cse on the instructions of a function.
6099 F is the first instruction.
6100 NREGS is one plus the highest pseudo-reg number used in the instruction.
6102 Return 2 if jump optimizations should be redone due to simplifications
6103 in conditional jump instructions.
6104 Return 1 if the CFG should be cleaned up because it has been modified.
6105 Return 0 otherwise. */
6108 cse_main (rtx f ATTRIBUTE_UNUSED, int nregs)
6110 struct cse_basic_block_data ebb_data;
6111 basic_block bb;
6112 int *rc_order = XNEWVEC (int, last_basic_block);
6113 int i, n_blocks;
6115 df_set_flags (DF_LR_RUN_DCE);
6116 df_analyze ();
6117 df_set_flags (DF_DEFER_INSN_RESCAN);
6119 reg_scan (get_insns (), max_reg_num ());
6120 init_cse_reg_info (nregs);
6122 ebb_data.path = XNEWVEC (struct branch_path,
6123 PARAM_VALUE (PARAM_MAX_CSE_PATH_LENGTH));
6125 cse_cfg_altered = false;
6126 cse_jumps_altered = false;
6127 recorded_label_ref = false;
6128 constant_pool_entries_cost = 0;
6129 constant_pool_entries_regcost = 0;
6130 ebb_data.path_size = 0;
6131 ebb_data.nsets = 0;
6132 rtl_hooks = cse_rtl_hooks;
6134 init_recog ();
6135 init_alias_analysis ();
6137 reg_eqv_table = XNEWVEC (struct reg_eqv_elem, nregs);
6139 /* Set up the table of already visited basic blocks. */
6140 cse_visited_basic_blocks = sbitmap_alloc (last_basic_block);
6141 sbitmap_zero (cse_visited_basic_blocks);
6143 /* Loop over basic blocks in reverse completion order (RPO),
6144 excluding the ENTRY and EXIT blocks. */
6145 n_blocks = pre_and_rev_post_order_compute (NULL, rc_order, false);
6146 i = 0;
6147 while (i < n_blocks)
6149 /* Find the first block in the RPO queue that we have not yet
6150 processed before. */
6153 bb = BASIC_BLOCK (rc_order[i++]);
6155 while (TEST_BIT (cse_visited_basic_blocks, bb->index)
6156 && i < n_blocks);
6158 /* Find all paths starting with BB, and process them. */
6159 while (cse_find_path (bb, &ebb_data, flag_cse_follow_jumps))
6161 /* Pre-scan the path. */
6162 cse_prescan_path (&ebb_data);
6164 /* If this basic block has no sets, skip it. */
6165 if (ebb_data.nsets == 0)
6166 continue;
6168 /* Get a reasonable estimate for the maximum number of qty's
6169 needed for this path. For this, we take the number of sets
6170 and multiply that by MAX_RECOG_OPERANDS. */
6171 max_qty = ebb_data.nsets * MAX_RECOG_OPERANDS;
6173 /* Dump the path we're about to process. */
6174 if (dump_file)
6175 cse_dump_path (&ebb_data, ebb_data.nsets, dump_file);
6177 cse_extended_basic_block (&ebb_data);
6181 /* Clean up. */
6182 end_alias_analysis ();
6183 free (reg_eqv_table);
6184 free (ebb_data.path);
6185 sbitmap_free (cse_visited_basic_blocks);
6186 free (rc_order);
6187 rtl_hooks = general_rtl_hooks;
6189 if (cse_jumps_altered || recorded_label_ref)
6190 return 2;
6191 else if (cse_cfg_altered)
6192 return 1;
6193 else
6194 return 0;
6197 /* Called via for_each_rtx to see if an insn is using a LABEL_REF for
6198 which there isn't a REG_LABEL_OPERAND note.
6199 Return one if so. DATA is the insn. */
6201 static int
6202 check_for_label_ref (rtx *rtl, void *data)
6204 rtx insn = (rtx) data;
6206 /* If this insn uses a LABEL_REF and there isn't a REG_LABEL_OPERAND
6207 note for it, we must rerun jump since it needs to place the note. If
6208 this is a LABEL_REF for a CODE_LABEL that isn't in the insn chain,
6209 don't do this since no REG_LABEL_OPERAND will be added. */
6210 return (GET_CODE (*rtl) == LABEL_REF
6211 && ! LABEL_REF_NONLOCAL_P (*rtl)
6212 && (!JUMP_P (insn)
6213 || !label_is_jump_target_p (XEXP (*rtl, 0), insn))
6214 && LABEL_P (XEXP (*rtl, 0))
6215 && INSN_UID (XEXP (*rtl, 0)) != 0
6216 && ! find_reg_note (insn, REG_LABEL_OPERAND, XEXP (*rtl, 0)));
6219 /* Count the number of times registers are used (not set) in X.
6220 COUNTS is an array in which we accumulate the count, INCR is how much
6221 we count each register usage.
6223 Don't count a usage of DEST, which is the SET_DEST of a SET which
6224 contains X in its SET_SRC. This is because such a SET does not
6225 modify the liveness of DEST.
6226 DEST is set to pc_rtx for a trapping insn, which means that we must count
6227 uses of a SET_DEST regardless because the insn can't be deleted here. */
6229 static void
6230 count_reg_usage (rtx x, int *counts, rtx dest, int incr)
6232 enum rtx_code code;
6233 rtx note;
6234 const char *fmt;
6235 int i, j;
6237 if (x == 0)
6238 return;
6240 switch (code = GET_CODE (x))
6242 case REG:
6243 if (x != dest)
6244 counts[REGNO (x)] += incr;
6245 return;
6247 case PC:
6248 case CC0:
6249 case CONST:
6250 case CONST_INT:
6251 case CONST_DOUBLE:
6252 case CONST_FIXED:
6253 case CONST_VECTOR:
6254 case SYMBOL_REF:
6255 case LABEL_REF:
6256 return;
6258 case CLOBBER:
6259 /* If we are clobbering a MEM, mark any registers inside the address
6260 as being used. */
6261 if (MEM_P (XEXP (x, 0)))
6262 count_reg_usage (XEXP (XEXP (x, 0), 0), counts, NULL_RTX, incr);
6263 return;
6265 case SET:
6266 /* Unless we are setting a REG, count everything in SET_DEST. */
6267 if (!REG_P (SET_DEST (x)))
6268 count_reg_usage (SET_DEST (x), counts, NULL_RTX, incr);
6269 count_reg_usage (SET_SRC (x), counts,
6270 dest ? dest : SET_DEST (x),
6271 incr);
6272 return;
6274 case CALL_INSN:
6275 case INSN:
6276 case JUMP_INSN:
6277 /* We expect dest to be NULL_RTX here. If the insn may trap, mark
6278 this fact by setting DEST to pc_rtx. */
6279 if (flag_non_call_exceptions && may_trap_p (PATTERN (x)))
6280 dest = pc_rtx;
6281 if (code == CALL_INSN)
6282 count_reg_usage (CALL_INSN_FUNCTION_USAGE (x), counts, dest, incr);
6283 count_reg_usage (PATTERN (x), counts, dest, incr);
6285 /* Things used in a REG_EQUAL note aren't dead since loop may try to
6286 use them. */
6288 note = find_reg_equal_equiv_note (x);
6289 if (note)
6291 rtx eqv = XEXP (note, 0);
6293 if (GET_CODE (eqv) == EXPR_LIST)
6294 /* This REG_EQUAL note describes the result of a function call.
6295 Process all the arguments. */
6298 count_reg_usage (XEXP (eqv, 0), counts, dest, incr);
6299 eqv = XEXP (eqv, 1);
6301 while (eqv && GET_CODE (eqv) == EXPR_LIST);
6302 else
6303 count_reg_usage (eqv, counts, dest, incr);
6305 return;
6307 case EXPR_LIST:
6308 if (REG_NOTE_KIND (x) == REG_EQUAL
6309 || (REG_NOTE_KIND (x) != REG_NONNEG && GET_CODE (XEXP (x,0)) == USE)
6310 /* FUNCTION_USAGE expression lists may include (CLOBBER (mem /u)),
6311 involving registers in the address. */
6312 || GET_CODE (XEXP (x, 0)) == CLOBBER)
6313 count_reg_usage (XEXP (x, 0), counts, NULL_RTX, incr);
6315 count_reg_usage (XEXP (x, 1), counts, NULL_RTX, incr);
6316 return;
6318 case ASM_OPERANDS:
6319 /* If the asm is volatile, then this insn cannot be deleted,
6320 and so the inputs *must* be live. */
6321 if (MEM_VOLATILE_P (x))
6322 dest = NULL_RTX;
6323 /* Iterate over just the inputs, not the constraints as well. */
6324 for (i = ASM_OPERANDS_INPUT_LENGTH (x) - 1; i >= 0; i--)
6325 count_reg_usage (ASM_OPERANDS_INPUT (x, i), counts, dest, incr);
6326 return;
6328 case INSN_LIST:
6329 gcc_unreachable ();
6331 default:
6332 break;
6335 fmt = GET_RTX_FORMAT (code);
6336 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6338 if (fmt[i] == 'e')
6339 count_reg_usage (XEXP (x, i), counts, dest, incr);
6340 else if (fmt[i] == 'E')
6341 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6342 count_reg_usage (XVECEXP (x, i, j), counts, dest, incr);
6346 /* Return true if set is live. */
6347 static bool
6348 set_live_p (rtx set, rtx insn ATTRIBUTE_UNUSED, /* Only used with HAVE_cc0. */
6349 int *counts)
6351 #ifdef HAVE_cc0
6352 rtx tem;
6353 #endif
6355 if (set_noop_p (set))
6358 #ifdef HAVE_cc0
6359 else if (GET_CODE (SET_DEST (set)) == CC0
6360 && !side_effects_p (SET_SRC (set))
6361 && ((tem = next_nonnote_insn (insn)) == 0
6362 || !INSN_P (tem)
6363 || !reg_referenced_p (cc0_rtx, PATTERN (tem))))
6364 return false;
6365 #endif
6366 else if (!REG_P (SET_DEST (set))
6367 || REGNO (SET_DEST (set)) < FIRST_PSEUDO_REGISTER
6368 || counts[REGNO (SET_DEST (set))] != 0
6369 || side_effects_p (SET_SRC (set)))
6370 return true;
6371 return false;
6374 /* Return true if insn is live. */
6376 static bool
6377 insn_live_p (rtx insn, int *counts)
6379 int i;
6380 if (flag_non_call_exceptions && may_trap_p (PATTERN (insn)))
6381 return true;
6382 else if (GET_CODE (PATTERN (insn)) == SET)
6383 return set_live_p (PATTERN (insn), insn, counts);
6384 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
6386 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
6388 rtx elt = XVECEXP (PATTERN (insn), 0, i);
6390 if (GET_CODE (elt) == SET)
6392 if (set_live_p (elt, insn, counts))
6393 return true;
6395 else if (GET_CODE (elt) != CLOBBER && GET_CODE (elt) != USE)
6396 return true;
6398 return false;
6400 else
6401 return true;
6404 /* Scan all the insns and delete any that are dead; i.e., they store a register
6405 that is never used or they copy a register to itself.
6407 This is used to remove insns made obviously dead by cse, loop or other
6408 optimizations. It improves the heuristics in loop since it won't try to
6409 move dead invariants out of loops or make givs for dead quantities. The
6410 remaining passes of the compilation are also sped up. */
6413 delete_trivially_dead_insns (rtx insns, int nreg)
6415 int *counts;
6416 rtx insn, prev;
6417 int ndead = 0;
6419 timevar_push (TV_DELETE_TRIVIALLY_DEAD);
6420 /* First count the number of times each register is used. */
6421 counts = XCNEWVEC (int, nreg);
6422 for (insn = insns; insn; insn = NEXT_INSN (insn))
6423 if (INSN_P (insn))
6424 count_reg_usage (insn, counts, NULL_RTX, 1);
6426 /* Go from the last insn to the first and delete insns that only set unused
6427 registers or copy a register to itself. As we delete an insn, remove
6428 usage counts for registers it uses.
6430 The first jump optimization pass may leave a real insn as the last
6431 insn in the function. We must not skip that insn or we may end
6432 up deleting code that is not really dead. */
6433 for (insn = get_last_insn (); insn; insn = prev)
6435 int live_insn = 0;
6437 prev = PREV_INSN (insn);
6438 if (!INSN_P (insn))
6439 continue;
6441 live_insn = insn_live_p (insn, counts);
6443 /* If this is a dead insn, delete it and show registers in it aren't
6444 being used. */
6446 if (! live_insn && dbg_cnt (delete_trivial_dead))
6448 count_reg_usage (insn, counts, NULL_RTX, -1);
6449 delete_insn_and_edges (insn);
6450 ndead++;
6454 if (dump_file && ndead)
6455 fprintf (dump_file, "Deleted %i trivially dead insns\n",
6456 ndead);
6457 /* Clean up. */
6458 free (counts);
6459 timevar_pop (TV_DELETE_TRIVIALLY_DEAD);
6460 return ndead;
6463 /* This function is called via for_each_rtx. The argument, NEWREG, is
6464 a condition code register with the desired mode. If we are looking
6465 at the same register in a different mode, replace it with
6466 NEWREG. */
6468 static int
6469 cse_change_cc_mode (rtx *loc, void *data)
6471 struct change_cc_mode_args* args = (struct change_cc_mode_args*)data;
6473 if (*loc
6474 && REG_P (*loc)
6475 && REGNO (*loc) == REGNO (args->newreg)
6476 && GET_MODE (*loc) != GET_MODE (args->newreg))
6478 validate_change (args->insn, loc, args->newreg, 1);
6480 return -1;
6482 return 0;
6485 /* Change the mode of any reference to the register REGNO (NEWREG) to
6486 GET_MODE (NEWREG) in INSN. */
6488 static void
6489 cse_change_cc_mode_insn (rtx insn, rtx newreg)
6491 struct change_cc_mode_args args;
6492 int success;
6494 if (!INSN_P (insn))
6495 return;
6497 args.insn = insn;
6498 args.newreg = newreg;
6500 for_each_rtx (&PATTERN (insn), cse_change_cc_mode, &args);
6501 for_each_rtx (&REG_NOTES (insn), cse_change_cc_mode, &args);
6503 /* If the following assertion was triggered, there is most probably
6504 something wrong with the cc_modes_compatible back end function.
6505 CC modes only can be considered compatible if the insn - with the mode
6506 replaced by any of the compatible modes - can still be recognized. */
6507 success = apply_change_group ();
6508 gcc_assert (success);
6511 /* Change the mode of any reference to the register REGNO (NEWREG) to
6512 GET_MODE (NEWREG), starting at START. Stop before END. Stop at
6513 any instruction which modifies NEWREG. */
6515 static void
6516 cse_change_cc_mode_insns (rtx start, rtx end, rtx newreg)
6518 rtx insn;
6520 for (insn = start; insn != end; insn = NEXT_INSN (insn))
6522 if (! INSN_P (insn))
6523 continue;
6525 if (reg_set_p (newreg, insn))
6526 return;
6528 cse_change_cc_mode_insn (insn, newreg);
6532 /* BB is a basic block which finishes with CC_REG as a condition code
6533 register which is set to CC_SRC. Look through the successors of BB
6534 to find blocks which have a single predecessor (i.e., this one),
6535 and look through those blocks for an assignment to CC_REG which is
6536 equivalent to CC_SRC. CAN_CHANGE_MODE indicates whether we are
6537 permitted to change the mode of CC_SRC to a compatible mode. This
6538 returns VOIDmode if no equivalent assignments were found.
6539 Otherwise it returns the mode which CC_SRC should wind up with.
6541 The main complexity in this function is handling the mode issues.
6542 We may have more than one duplicate which we can eliminate, and we
6543 try to find a mode which will work for multiple duplicates. */
6545 static enum machine_mode
6546 cse_cc_succs (basic_block bb, rtx cc_reg, rtx cc_src, bool can_change_mode)
6548 bool found_equiv;
6549 enum machine_mode mode;
6550 unsigned int insn_count;
6551 edge e;
6552 rtx insns[2];
6553 enum machine_mode modes[2];
6554 rtx last_insns[2];
6555 unsigned int i;
6556 rtx newreg;
6557 edge_iterator ei;
6559 /* We expect to have two successors. Look at both before picking
6560 the final mode for the comparison. If we have more successors
6561 (i.e., some sort of table jump, although that seems unlikely),
6562 then we require all beyond the first two to use the same
6563 mode. */
6565 found_equiv = false;
6566 mode = GET_MODE (cc_src);
6567 insn_count = 0;
6568 FOR_EACH_EDGE (e, ei, bb->succs)
6570 rtx insn;
6571 rtx end;
6573 if (e->flags & EDGE_COMPLEX)
6574 continue;
6576 if (EDGE_COUNT (e->dest->preds) != 1
6577 || e->dest == EXIT_BLOCK_PTR)
6578 continue;
6580 end = NEXT_INSN (BB_END (e->dest));
6581 for (insn = BB_HEAD (e->dest); insn != end; insn = NEXT_INSN (insn))
6583 rtx set;
6585 if (! INSN_P (insn))
6586 continue;
6588 /* If CC_SRC is modified, we have to stop looking for
6589 something which uses it. */
6590 if (modified_in_p (cc_src, insn))
6591 break;
6593 /* Check whether INSN sets CC_REG to CC_SRC. */
6594 set = single_set (insn);
6595 if (set
6596 && REG_P (SET_DEST (set))
6597 && REGNO (SET_DEST (set)) == REGNO (cc_reg))
6599 bool found;
6600 enum machine_mode set_mode;
6601 enum machine_mode comp_mode;
6603 found = false;
6604 set_mode = GET_MODE (SET_SRC (set));
6605 comp_mode = set_mode;
6606 if (rtx_equal_p (cc_src, SET_SRC (set)))
6607 found = true;
6608 else if (GET_CODE (cc_src) == COMPARE
6609 && GET_CODE (SET_SRC (set)) == COMPARE
6610 && mode != set_mode
6611 && rtx_equal_p (XEXP (cc_src, 0),
6612 XEXP (SET_SRC (set), 0))
6613 && rtx_equal_p (XEXP (cc_src, 1),
6614 XEXP (SET_SRC (set), 1)))
6617 comp_mode = targetm.cc_modes_compatible (mode, set_mode);
6618 if (comp_mode != VOIDmode
6619 && (can_change_mode || comp_mode == mode))
6620 found = true;
6623 if (found)
6625 found_equiv = true;
6626 if (insn_count < ARRAY_SIZE (insns))
6628 insns[insn_count] = insn;
6629 modes[insn_count] = set_mode;
6630 last_insns[insn_count] = end;
6631 ++insn_count;
6633 if (mode != comp_mode)
6635 gcc_assert (can_change_mode);
6636 mode = comp_mode;
6638 /* The modified insn will be re-recognized later. */
6639 PUT_MODE (cc_src, mode);
6642 else
6644 if (set_mode != mode)
6646 /* We found a matching expression in the
6647 wrong mode, but we don't have room to
6648 store it in the array. Punt. This case
6649 should be rare. */
6650 break;
6652 /* INSN sets CC_REG to a value equal to CC_SRC
6653 with the right mode. We can simply delete
6654 it. */
6655 delete_insn (insn);
6658 /* We found an instruction to delete. Keep looking,
6659 in the hopes of finding a three-way jump. */
6660 continue;
6663 /* We found an instruction which sets the condition
6664 code, so don't look any farther. */
6665 break;
6668 /* If INSN sets CC_REG in some other way, don't look any
6669 farther. */
6670 if (reg_set_p (cc_reg, insn))
6671 break;
6674 /* If we fell off the bottom of the block, we can keep looking
6675 through successors. We pass CAN_CHANGE_MODE as false because
6676 we aren't prepared to handle compatibility between the
6677 further blocks and this block. */
6678 if (insn == end)
6680 enum machine_mode submode;
6682 submode = cse_cc_succs (e->dest, cc_reg, cc_src, false);
6683 if (submode != VOIDmode)
6685 gcc_assert (submode == mode);
6686 found_equiv = true;
6687 can_change_mode = false;
6692 if (! found_equiv)
6693 return VOIDmode;
6695 /* Now INSN_COUNT is the number of instructions we found which set
6696 CC_REG to a value equivalent to CC_SRC. The instructions are in
6697 INSNS. The modes used by those instructions are in MODES. */
6699 newreg = NULL_RTX;
6700 for (i = 0; i < insn_count; ++i)
6702 if (modes[i] != mode)
6704 /* We need to change the mode of CC_REG in INSNS[i] and
6705 subsequent instructions. */
6706 if (! newreg)
6708 if (GET_MODE (cc_reg) == mode)
6709 newreg = cc_reg;
6710 else
6711 newreg = gen_rtx_REG (mode, REGNO (cc_reg));
6713 cse_change_cc_mode_insns (NEXT_INSN (insns[i]), last_insns[i],
6714 newreg);
6717 delete_insn_and_edges (insns[i]);
6720 return mode;
6723 /* If we have a fixed condition code register (or two), walk through
6724 the instructions and try to eliminate duplicate assignments. */
6726 static void
6727 cse_condition_code_reg (void)
6729 unsigned int cc_regno_1;
6730 unsigned int cc_regno_2;
6731 rtx cc_reg_1;
6732 rtx cc_reg_2;
6733 basic_block bb;
6735 if (! targetm.fixed_condition_code_regs (&cc_regno_1, &cc_regno_2))
6736 return;
6738 cc_reg_1 = gen_rtx_REG (CCmode, cc_regno_1);
6739 if (cc_regno_2 != INVALID_REGNUM)
6740 cc_reg_2 = gen_rtx_REG (CCmode, cc_regno_2);
6741 else
6742 cc_reg_2 = NULL_RTX;
6744 FOR_EACH_BB (bb)
6746 rtx last_insn;
6747 rtx cc_reg;
6748 rtx insn;
6749 rtx cc_src_insn;
6750 rtx cc_src;
6751 enum machine_mode mode;
6752 enum machine_mode orig_mode;
6754 /* Look for blocks which end with a conditional jump based on a
6755 condition code register. Then look for the instruction which
6756 sets the condition code register. Then look through the
6757 successor blocks for instructions which set the condition
6758 code register to the same value. There are other possible
6759 uses of the condition code register, but these are by far the
6760 most common and the ones which we are most likely to be able
6761 to optimize. */
6763 last_insn = BB_END (bb);
6764 if (!JUMP_P (last_insn))
6765 continue;
6767 if (reg_referenced_p (cc_reg_1, PATTERN (last_insn)))
6768 cc_reg = cc_reg_1;
6769 else if (cc_reg_2 && reg_referenced_p (cc_reg_2, PATTERN (last_insn)))
6770 cc_reg = cc_reg_2;
6771 else
6772 continue;
6774 cc_src_insn = NULL_RTX;
6775 cc_src = NULL_RTX;
6776 for (insn = PREV_INSN (last_insn);
6777 insn && insn != PREV_INSN (BB_HEAD (bb));
6778 insn = PREV_INSN (insn))
6780 rtx set;
6782 if (! INSN_P (insn))
6783 continue;
6784 set = single_set (insn);
6785 if (set
6786 && REG_P (SET_DEST (set))
6787 && REGNO (SET_DEST (set)) == REGNO (cc_reg))
6789 cc_src_insn = insn;
6790 cc_src = SET_SRC (set);
6791 break;
6793 else if (reg_set_p (cc_reg, insn))
6794 break;
6797 if (! cc_src_insn)
6798 continue;
6800 if (modified_between_p (cc_src, cc_src_insn, NEXT_INSN (last_insn)))
6801 continue;
6803 /* Now CC_REG is a condition code register used for a
6804 conditional jump at the end of the block, and CC_SRC, in
6805 CC_SRC_INSN, is the value to which that condition code
6806 register is set, and CC_SRC is still meaningful at the end of
6807 the basic block. */
6809 orig_mode = GET_MODE (cc_src);
6810 mode = cse_cc_succs (bb, cc_reg, cc_src, true);
6811 if (mode != VOIDmode)
6813 gcc_assert (mode == GET_MODE (cc_src));
6814 if (mode != orig_mode)
6816 rtx newreg = gen_rtx_REG (mode, REGNO (cc_reg));
6818 cse_change_cc_mode_insn (cc_src_insn, newreg);
6820 /* Do the same in the following insns that use the
6821 current value of CC_REG within BB. */
6822 cse_change_cc_mode_insns (NEXT_INSN (cc_src_insn),
6823 NEXT_INSN (last_insn),
6824 newreg);
6831 /* Perform common subexpression elimination. Nonzero value from
6832 `cse_main' means that jumps were simplified and some code may now
6833 be unreachable, so do jump optimization again. */
6834 static bool
6835 gate_handle_cse (void)
6837 return optimize > 0;
6840 static unsigned int
6841 rest_of_handle_cse (void)
6843 int tem;
6845 if (dump_file)
6846 dump_flow_info (dump_file, dump_flags);
6848 tem = cse_main (get_insns (), max_reg_num ());
6850 /* If we are not running more CSE passes, then we are no longer
6851 expecting CSE to be run. But always rerun it in a cheap mode. */
6852 cse_not_expected = !flag_rerun_cse_after_loop && !flag_gcse;
6854 if (tem == 2)
6856 timevar_push (TV_JUMP);
6857 rebuild_jump_labels (get_insns ());
6858 cleanup_cfg (0);
6859 timevar_pop (TV_JUMP);
6861 else if (tem == 1 || optimize > 1)
6862 cleanup_cfg (0);
6864 return 0;
6867 struct rtl_opt_pass pass_cse =
6870 RTL_PASS,
6871 "cse1", /* name */
6872 gate_handle_cse, /* gate */
6873 rest_of_handle_cse, /* execute */
6874 NULL, /* sub */
6875 NULL, /* next */
6876 0, /* static_pass_number */
6877 TV_CSE, /* tv_id */
6878 0, /* properties_required */
6879 0, /* properties_provided */
6880 0, /* properties_destroyed */
6881 0, /* todo_flags_start */
6882 TODO_df_finish | TODO_verify_rtl_sharing |
6883 TODO_dump_func |
6884 TODO_ggc_collect |
6885 TODO_verify_flow, /* todo_flags_finish */
6890 static bool
6891 gate_handle_cse2 (void)
6893 return optimize > 0 && flag_rerun_cse_after_loop;
6896 /* Run second CSE pass after loop optimizations. */
6897 static unsigned int
6898 rest_of_handle_cse2 (void)
6900 int tem;
6902 if (dump_file)
6903 dump_flow_info (dump_file, dump_flags);
6905 tem = cse_main (get_insns (), max_reg_num ());
6907 /* Run a pass to eliminate duplicated assignments to condition code
6908 registers. We have to run this after bypass_jumps, because it
6909 makes it harder for that pass to determine whether a jump can be
6910 bypassed safely. */
6911 cse_condition_code_reg ();
6913 delete_trivially_dead_insns (get_insns (), max_reg_num ());
6915 if (tem == 2)
6917 timevar_push (TV_JUMP);
6918 rebuild_jump_labels (get_insns ());
6919 cleanup_cfg (0);
6920 timevar_pop (TV_JUMP);
6922 else if (tem == 1)
6923 cleanup_cfg (0);
6925 cse_not_expected = 1;
6926 return 0;
6930 struct rtl_opt_pass pass_cse2 =
6933 RTL_PASS,
6934 "cse2", /* name */
6935 gate_handle_cse2, /* gate */
6936 rest_of_handle_cse2, /* execute */
6937 NULL, /* sub */
6938 NULL, /* next */
6939 0, /* static_pass_number */
6940 TV_CSE2, /* tv_id */
6941 0, /* properties_required */
6942 0, /* properties_provided */
6943 0, /* properties_destroyed */
6944 0, /* todo_flags_start */
6945 TODO_df_finish | TODO_verify_rtl_sharing |
6946 TODO_dump_func |
6947 TODO_ggc_collect |
6948 TODO_verify_flow /* todo_flags_finish */