* decl.c (add_decl_to_level): Remove TREE_PERMANENT assertion.
[official-gcc.git] / gcc / cse.c
blob75952b0e128b9cf51c6645a0716c4c3dd3df0180
1 /* Common subexpression elimination for GNU compiler.
2 Copyright (C) 1987, 88, 89, 92-7, 1998, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 /* stdio.h must precede rtl.h for FFS. */
24 #include "system.h"
25 #include <setjmp.h>
27 #include "rtl.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "flags.h"
31 #include "real.h"
32 #include "insn-config.h"
33 #include "recog.h"
34 #include "function.h"
35 #include "expr.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "splay-tree.h"
39 #include "ggc.h"
41 /* The basic idea of common subexpression elimination is to go
42 through the code, keeping a record of expressions that would
43 have the same value at the current scan point, and replacing
44 expressions encountered with the cheapest equivalent expression.
46 It is too complicated to keep track of the different possibilities
47 when control paths merge; so, at each label, we forget all that is
48 known and start fresh. This can be described as processing each
49 basic block separately. Note, however, that these are not quite
50 the same as the basic blocks found by a later pass and used for
51 data flow analysis and register packing. We do not need to start fresh
52 after a conditional jump instruction if there is no label there.
54 We use two data structures to record the equivalent expressions:
55 a hash table for most expressions, and several vectors together
56 with "quantity numbers" to record equivalent (pseudo) registers.
58 The use of the special data structure for registers is desirable
59 because it is faster. It is possible because registers references
60 contain a fairly small number, the register number, taken from
61 a contiguously allocated series, and two register references are
62 identical if they have the same number. General expressions
63 do not have any such thing, so the only way to retrieve the
64 information recorded on an expression other than a register
65 is to keep it in a hash table.
67 Registers and "quantity numbers":
69 At the start of each basic block, all of the (hardware and pseudo)
70 registers used in the function are given distinct quantity
71 numbers to indicate their contents. During scan, when the code
72 copies one register into another, we copy the quantity number.
73 When a register is loaded in any other way, we allocate a new
74 quantity number to describe the value generated by this operation.
75 `reg_qty' records what quantity a register is currently thought
76 of as containing.
78 All real quantity numbers are greater than or equal to `max_reg'.
79 If register N has not been assigned a quantity, reg_qty[N] will equal N.
81 Quantity numbers below `max_reg' do not exist and none of the `qty_...'
82 variables should be referenced with an index below `max_reg'.
84 We also maintain a bidirectional chain of registers for each
85 quantity number. `qty_first_reg', `qty_last_reg',
86 `reg_next_eqv' and `reg_prev_eqv' hold these chains.
88 The first register in a chain is the one whose lifespan is least local.
89 Among equals, it is the one that was seen first.
90 We replace any equivalent register with that one.
92 If two registers have the same quantity number, it must be true that
93 REG expressions with `qty_mode' must be in the hash table for both
94 registers and must be in the same class.
96 The converse is not true. Since hard registers may be referenced in
97 any mode, two REG expressions might be equivalent in the hash table
98 but not have the same quantity number if the quantity number of one
99 of the registers is not the same mode as those expressions.
101 Constants and quantity numbers
103 When a quantity has a known constant value, that value is stored
104 in the appropriate element of qty_const. This is in addition to
105 putting the constant in the hash table as is usual for non-regs.
107 Whether a reg or a constant is preferred is determined by the configuration
108 macro CONST_COSTS and will often depend on the constant value. In any
109 event, expressions containing constants can be simplified, by fold_rtx.
111 When a quantity has a known nearly constant value (such as an address
112 of a stack slot), that value is stored in the appropriate element
113 of qty_const.
115 Integer constants don't have a machine mode. However, cse
116 determines the intended machine mode from the destination
117 of the instruction that moves the constant. The machine mode
118 is recorded in the hash table along with the actual RTL
119 constant expression so that different modes are kept separate.
121 Other expressions:
123 To record known equivalences among expressions in general
124 we use a hash table called `table'. It has a fixed number of buckets
125 that contain chains of `struct table_elt' elements for expressions.
126 These chains connect the elements whose expressions have the same
127 hash codes.
129 Other chains through the same elements connect the elements which
130 currently have equivalent values.
132 Register references in an expression are canonicalized before hashing
133 the expression. This is done using `reg_qty' and `qty_first_reg'.
134 The hash code of a register reference is computed using the quantity
135 number, not the register number.
137 When the value of an expression changes, it is necessary to remove from the
138 hash table not just that expression but all expressions whose values
139 could be different as a result.
141 1. If the value changing is in memory, except in special cases
142 ANYTHING referring to memory could be changed. That is because
143 nobody knows where a pointer does not point.
144 The function `invalidate_memory' removes what is necessary.
146 The special cases are when the address is constant or is
147 a constant plus a fixed register such as the frame pointer
148 or a static chain pointer. When such addresses are stored in,
149 we can tell exactly which other such addresses must be invalidated
150 due to overlap. `invalidate' does this.
151 All expressions that refer to non-constant
152 memory addresses are also invalidated. `invalidate_memory' does this.
154 2. If the value changing is a register, all expressions
155 containing references to that register, and only those,
156 must be removed.
158 Because searching the entire hash table for expressions that contain
159 a register is very slow, we try to figure out when it isn't necessary.
160 Precisely, this is necessary only when expressions have been
161 entered in the hash table using this register, and then the value has
162 changed, and then another expression wants to be added to refer to
163 the register's new value. This sequence of circumstances is rare
164 within any one basic block.
166 The vectors `reg_tick' and `reg_in_table' are used to detect this case.
167 reg_tick[i] is incremented whenever a value is stored in register i.
168 reg_in_table[i] holds -1 if no references to register i have been
169 entered in the table; otherwise, it contains the value reg_tick[i] had
170 when the references were entered. If we want to enter a reference
171 and reg_in_table[i] != reg_tick[i], we must scan and remove old references.
172 Until we want to enter a new entry, the mere fact that the two vectors
173 don't match makes the entries be ignored if anyone tries to match them.
175 Registers themselves are entered in the hash table as well as in
176 the equivalent-register chains. However, the vectors `reg_tick'
177 and `reg_in_table' do not apply to expressions which are simple
178 register references. These expressions are removed from the table
179 immediately when they become invalid, and this can be done even if
180 we do not immediately search for all the expressions that refer to
181 the register.
183 A CLOBBER rtx in an instruction invalidates its operand for further
184 reuse. A CLOBBER or SET rtx whose operand is a MEM:BLK
185 invalidates everything that resides in memory.
187 Related expressions:
189 Constant expressions that differ only by an additive integer
190 are called related. When a constant expression is put in
191 the table, the related expression with no constant term
192 is also entered. These are made to point at each other
193 so that it is possible to find out if there exists any
194 register equivalent to an expression related to a given expression. */
196 /* One plus largest register number used in this function. */
198 static int max_reg;
200 /* One plus largest instruction UID used in this function at time of
201 cse_main call. */
203 static int max_insn_uid;
205 /* Length of vectors indexed by quantity number.
206 We know in advance we will not need a quantity number this big. */
208 static int max_qty;
210 /* Next quantity number to be allocated.
211 This is 1 + the largest number needed so far. */
213 static int next_qty;
215 /* Indexed by quantity number, gives the first (or last) register
216 in the chain of registers that currently contain this quantity. */
218 static int *qty_first_reg;
219 static int *qty_last_reg;
221 /* Index by quantity number, gives the mode of the quantity. */
223 static enum machine_mode *qty_mode;
225 /* Indexed by quantity number, gives the rtx of the constant value of the
226 quantity, or zero if it does not have a known value.
227 A sum of the frame pointer (or arg pointer) plus a constant
228 can also be entered here. */
230 static rtx *qty_const;
232 /* Indexed by qty number, gives the insn that stored the constant value
233 recorded in `qty_const'. */
235 static rtx *qty_const_insn;
237 /* The next three variables are used to track when a comparison between a
238 quantity and some constant or register has been passed. In that case, we
239 know the results of the comparison in case we see it again. These variables
240 record a comparison that is known to be true. */
242 /* Indexed by qty number, gives the rtx code of a comparison with a known
243 result involving this quantity. If none, it is UNKNOWN. */
244 static enum rtx_code *qty_comparison_code;
246 /* Indexed by qty number, gives the constant being compared against in a
247 comparison of known result. If no such comparison, it is undefined.
248 If the comparison is not with a constant, it is zero. */
250 static rtx *qty_comparison_const;
252 /* Indexed by qty number, gives the quantity being compared against in a
253 comparison of known result. If no such comparison, if it undefined.
254 If the comparison is not with a register, it is -1. */
256 static int *qty_comparison_qty;
258 #ifdef HAVE_cc0
259 /* For machines that have a CC0, we do not record its value in the hash
260 table since its use is guaranteed to be the insn immediately following
261 its definition and any other insn is presumed to invalidate it.
263 Instead, we store below the value last assigned to CC0. If it should
264 happen to be a constant, it is stored in preference to the actual
265 assigned value. In case it is a constant, we store the mode in which
266 the constant should be interpreted. */
268 static rtx prev_insn_cc0;
269 static enum machine_mode prev_insn_cc0_mode;
270 #endif
272 /* Previous actual insn. 0 if at first insn of basic block. */
274 static rtx prev_insn;
276 /* Insn being scanned. */
278 static rtx this_insn;
280 /* Index by register number, gives the number of the next (or
281 previous) register in the chain of registers sharing the same
282 value.
284 Or -1 if this register is at the end of the chain.
286 If reg_qty[N] == N, reg_next_eqv[N] is undefined. */
288 static int *reg_next_eqv;
289 static int *reg_prev_eqv;
291 struct cse_reg_info {
292 union {
293 /* The number of times the register has been altered in the current
294 basic block. */
295 int reg_tick;
297 /* The next cse_reg_info structure in the free list. */
298 struct cse_reg_info* next;
299 } variant;
301 /* The REG_TICK value at which rtx's containing this register are
302 valid in the hash table. If this does not equal the current
303 reg_tick value, such expressions existing in the hash table are
304 invalid. */
305 int reg_in_table;
307 /* The quantity number of the register's current contents. */
308 int reg_qty;
311 /* A free list of cse_reg_info entries. */
312 static struct cse_reg_info *cse_reg_info_free_list;
314 /* A mapping from registers to cse_reg_info data structures. */
315 static splay_tree cse_reg_info_tree;
317 /* The last lookup we did into the cse_reg_info_tree. This allows us
318 to cache repeated lookups. */
319 static int cached_regno;
320 static struct cse_reg_info *cached_cse_reg_info;
322 /* A HARD_REG_SET containing all the hard registers for which there is
323 currently a REG expression in the hash table. Note the difference
324 from the above variables, which indicate if the REG is mentioned in some
325 expression in the table. */
327 static HARD_REG_SET hard_regs_in_table;
329 /* A HARD_REG_SET containing all the hard registers that are invalidated
330 by a CALL_INSN. */
332 static HARD_REG_SET regs_invalidated_by_call;
334 /* CUID of insn that starts the basic block currently being cse-processed. */
336 static int cse_basic_block_start;
338 /* CUID of insn that ends the basic block currently being cse-processed. */
340 static int cse_basic_block_end;
342 /* Vector mapping INSN_UIDs to cuids.
343 The cuids are like uids but increase monotonically always.
344 We use them to see whether a reg is used outside a given basic block. */
346 static int *uid_cuid;
348 /* Highest UID in UID_CUID. */
349 static int max_uid;
351 /* Get the cuid of an insn. */
353 #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
355 /* Nonzero if cse has altered conditional jump insns
356 in such a way that jump optimization should be redone. */
358 static int cse_jumps_altered;
360 /* Nonzero if we put a LABEL_REF into the hash table. Since we may have put
361 it into an INSN without a REG_LABEL, we have to rerun jump after CSE
362 to put in the note. */
363 static int recorded_label_ref;
365 /* canon_hash stores 1 in do_not_record
366 if it notices a reference to CC0, PC, or some other volatile
367 subexpression. */
369 static int do_not_record;
371 #ifdef LOAD_EXTEND_OP
373 /* Scratch rtl used when looking for load-extended copy of a MEM. */
374 static rtx memory_extend_rtx;
375 #endif
377 /* canon_hash stores 1 in hash_arg_in_memory
378 if it notices a reference to memory within the expression being hashed. */
380 static int hash_arg_in_memory;
382 /* canon_hash stores 1 in hash_arg_in_struct
383 if it notices a reference to memory that's part of a structure. */
385 static int hash_arg_in_struct;
387 /* The hash table contains buckets which are chains of `struct table_elt's,
388 each recording one expression's information.
389 That expression is in the `exp' field.
391 Those elements with the same hash code are chained in both directions
392 through the `next_same_hash' and `prev_same_hash' fields.
394 Each set of expressions with equivalent values
395 are on a two-way chain through the `next_same_value'
396 and `prev_same_value' fields, and all point with
397 the `first_same_value' field at the first element in
398 that chain. The chain is in order of increasing cost.
399 Each element's cost value is in its `cost' field.
401 The `in_memory' field is nonzero for elements that
402 involve any reference to memory. These elements are removed
403 whenever a write is done to an unidentified location in memory.
404 To be safe, we assume that a memory address is unidentified unless
405 the address is either a symbol constant or a constant plus
406 the frame pointer or argument pointer.
408 The `in_struct' field is nonzero for elements that
409 involve any reference to memory inside a structure or array.
411 The `related_value' field is used to connect related expressions
412 (that differ by adding an integer).
413 The related expressions are chained in a circular fashion.
414 `related_value' is zero for expressions for which this
415 chain is not useful.
417 The `cost' field stores the cost of this element's expression.
419 The `is_const' flag is set if the element is a constant (including
420 a fixed address).
422 The `flag' field is used as a temporary during some search routines.
424 The `mode' field is usually the same as GET_MODE (`exp'), but
425 if `exp' is a CONST_INT and has no machine mode then the `mode'
426 field is the mode it was being used as. Each constant is
427 recorded separately for each mode it is used with. */
430 struct table_elt
432 rtx exp;
433 struct table_elt *next_same_hash;
434 struct table_elt *prev_same_hash;
435 struct table_elt *next_same_value;
436 struct table_elt *prev_same_value;
437 struct table_elt *first_same_value;
438 struct table_elt *related_value;
439 int cost;
440 enum machine_mode mode;
441 char in_memory;
442 char in_struct;
443 char is_const;
444 char flag;
447 /* We don't want a lot of buckets, because we rarely have very many
448 things stored in the hash table, and a lot of buckets slows
449 down a lot of loops that happen frequently. */
450 #define NBUCKETS 31
452 /* Compute hash code of X in mode M. Special-case case where X is a pseudo
453 register (hard registers may require `do_not_record' to be set). */
455 #define HASH(X, M) \
456 (GET_CODE (X) == REG && REGNO (X) >= FIRST_PSEUDO_REGISTER \
457 ? (((unsigned) REG << 7) + (unsigned) REG_QTY (REGNO (X))) % NBUCKETS \
458 : canon_hash (X, M) % NBUCKETS)
460 /* Determine whether register number N is considered a fixed register for CSE.
461 It is desirable to replace other regs with fixed regs, to reduce need for
462 non-fixed hard regs.
463 A reg wins if it is either the frame pointer or designated as fixed,
464 but not if it is an overlapping register. */
465 #ifdef OVERLAPPING_REGNO_P
466 #define FIXED_REGNO_P(N) \
467 (((N) == FRAME_POINTER_REGNUM || (N) == HARD_FRAME_POINTER_REGNUM \
468 || fixed_regs[N] || global_regs[N]) \
469 && ! OVERLAPPING_REGNO_P ((N)))
470 #else
471 #define FIXED_REGNO_P(N) \
472 ((N) == FRAME_POINTER_REGNUM || (N) == HARD_FRAME_POINTER_REGNUM \
473 || fixed_regs[N] || global_regs[N])
474 #endif
476 /* Compute cost of X, as stored in the `cost' field of a table_elt. Fixed
477 hard registers and pointers into the frame are the cheapest with a cost
478 of 0. Next come pseudos with a cost of one and other hard registers with
479 a cost of 2. Aside from these special cases, call `rtx_cost'. */
481 #define CHEAP_REGNO(N) \
482 ((N) == FRAME_POINTER_REGNUM || (N) == HARD_FRAME_POINTER_REGNUM \
483 || (N) == STACK_POINTER_REGNUM || (N) == ARG_POINTER_REGNUM \
484 || ((N) >= FIRST_VIRTUAL_REGISTER && (N) <= LAST_VIRTUAL_REGISTER) \
485 || ((N) < FIRST_PSEUDO_REGISTER \
486 && FIXED_REGNO_P (N) && REGNO_REG_CLASS (N) != NO_REGS))
488 /* A register is cheap if it is a user variable assigned to the register
489 or if its register number always corresponds to a cheap register. */
491 #define CHEAP_REG(N) \
492 ((REG_USERVAR_P (N) && REGNO (N) < FIRST_PSEUDO_REGISTER) \
493 || CHEAP_REGNO (REGNO (N)))
495 #define COST(X) \
496 (GET_CODE (X) == REG \
497 ? (CHEAP_REG (X) ? 0 \
498 : REGNO (X) >= FIRST_PSEUDO_REGISTER ? 1 \
499 : 2) \
500 : notreg_cost(X))
502 /* Get the info associated with register N. */
504 #define GET_CSE_REG_INFO(N) \
505 (((N) == cached_regno && cached_cse_reg_info) \
506 ? cached_cse_reg_info : get_cse_reg_info ((N)))
508 /* Get the number of times this register has been updated in this
509 basic block. */
511 #define REG_TICK(N) ((GET_CSE_REG_INFO (N))->variant.reg_tick)
513 /* Get the point at which REG was recorded in the table. */
515 #define REG_IN_TABLE(N) ((GET_CSE_REG_INFO (N))->reg_in_table)
517 /* Get the quantity number for REG. */
519 #define REG_QTY(N) ((GET_CSE_REG_INFO (N))->reg_qty)
521 /* Determine if the quantity number for register X represents a valid index
522 into the `qty_...' variables. */
524 #define REGNO_QTY_VALID_P(N) (REG_QTY (N) != (N))
526 #ifdef ADDRESS_COST
527 /* The ADDRESS_COST macro does not deal with ADDRESSOF nodes. But,
528 during CSE, such nodes are present. Using an ADDRESSOF node which
529 refers to the address of a REG is a good thing because we can then
530 turn (MEM (ADDRESSSOF (REG))) into just plain REG. */
531 #define CSE_ADDRESS_COST(RTX) \
532 ((GET_CODE (RTX) == ADDRESSOF && REG_P (XEXP ((RTX), 0))) \
533 ? -1 : ADDRESS_COST(RTX))
534 #endif
536 static struct table_elt *table[NBUCKETS];
538 /* Chain of `struct table_elt's made so far for this function
539 but currently removed from the table. */
541 static struct table_elt *free_element_chain;
543 /* Number of `struct table_elt' structures made so far for this function. */
545 static int n_elements_made;
547 /* Maximum value `n_elements_made' has had so far in this compilation
548 for functions previously processed. */
550 static int max_elements_made;
552 /* Surviving equivalence class when two equivalence classes are merged
553 by recording the effects of a jump in the last insn. Zero if the
554 last insn was not a conditional jump. */
556 static struct table_elt *last_jump_equiv_class;
558 /* Set to the cost of a constant pool reference if one was found for a
559 symbolic constant. If this was found, it means we should try to
560 convert constants into constant pool entries if they don't fit in
561 the insn. */
563 static int constant_pool_entries_cost;
565 /* Define maximum length of a branch path. */
567 #define PATHLENGTH 10
569 /* This data describes a block that will be processed by cse_basic_block. */
571 struct cse_basic_block_data {
572 /* Lowest CUID value of insns in block. */
573 int low_cuid;
574 /* Highest CUID value of insns in block. */
575 int high_cuid;
576 /* Total number of SETs in block. */
577 int nsets;
578 /* Last insn in the block. */
579 rtx last;
580 /* Size of current branch path, if any. */
581 int path_size;
582 /* Current branch path, indicating which branches will be taken. */
583 struct branch_path {
584 /* The branch insn. */
585 rtx branch;
586 /* Whether it should be taken or not. AROUND is the same as taken
587 except that it is used when the destination label is not preceded
588 by a BARRIER. */
589 enum taken {TAKEN, NOT_TAKEN, AROUND} status;
590 } path[PATHLENGTH];
593 /* Nonzero if X has the form (PLUS frame-pointer integer). We check for
594 virtual regs here because the simplify_*_operation routines are called
595 by integrate.c, which is called before virtual register instantiation. */
597 #define FIXED_BASE_PLUS_P(X) \
598 ((X) == frame_pointer_rtx || (X) == hard_frame_pointer_rtx \
599 || (X) == arg_pointer_rtx \
600 || (X) == virtual_stack_vars_rtx \
601 || (X) == virtual_incoming_args_rtx \
602 || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
603 && (XEXP (X, 0) == frame_pointer_rtx \
604 || XEXP (X, 0) == hard_frame_pointer_rtx \
605 || XEXP (X, 0) == arg_pointer_rtx \
606 || XEXP (X, 0) == virtual_stack_vars_rtx \
607 || XEXP (X, 0) == virtual_incoming_args_rtx)) \
608 || GET_CODE (X) == ADDRESSOF)
610 /* Similar, but also allows reference to the stack pointer.
612 This used to include FIXED_BASE_PLUS_P, however, we can't assume that
613 arg_pointer_rtx by itself is nonzero, because on at least one machine,
614 the i960, the arg pointer is zero when it is unused. */
616 #define NONZERO_BASE_PLUS_P(X) \
617 ((X) == frame_pointer_rtx || (X) == hard_frame_pointer_rtx \
618 || (X) == virtual_stack_vars_rtx \
619 || (X) == virtual_incoming_args_rtx \
620 || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
621 && (XEXP (X, 0) == frame_pointer_rtx \
622 || XEXP (X, 0) == hard_frame_pointer_rtx \
623 || XEXP (X, 0) == arg_pointer_rtx \
624 || XEXP (X, 0) == virtual_stack_vars_rtx \
625 || XEXP (X, 0) == virtual_incoming_args_rtx)) \
626 || (X) == stack_pointer_rtx \
627 || (X) == virtual_stack_dynamic_rtx \
628 || (X) == virtual_outgoing_args_rtx \
629 || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
630 && (XEXP (X, 0) == stack_pointer_rtx \
631 || XEXP (X, 0) == virtual_stack_dynamic_rtx \
632 || XEXP (X, 0) == virtual_outgoing_args_rtx)) \
633 || GET_CODE (X) == ADDRESSOF)
635 static int notreg_cost PROTO((rtx));
636 static void new_basic_block PROTO((void));
637 static void make_new_qty PROTO((int));
638 static void make_regs_eqv PROTO((int, int));
639 static void delete_reg_equiv PROTO((int));
640 static int mention_regs PROTO((rtx));
641 static int insert_regs PROTO((rtx, struct table_elt *, int));
642 static void free_element PROTO((struct table_elt *));
643 static void remove_from_table PROTO((struct table_elt *, unsigned));
644 static struct table_elt *get_element PROTO((void));
645 static struct table_elt *lookup PROTO((rtx, unsigned, enum machine_mode)),
646 *lookup_for_remove PROTO((rtx, unsigned, enum machine_mode));
647 static rtx lookup_as_function PROTO((rtx, enum rtx_code));
648 static struct table_elt *insert PROTO((rtx, struct table_elt *, unsigned,
649 enum machine_mode));
650 static void merge_equiv_classes PROTO((struct table_elt *,
651 struct table_elt *));
652 static void invalidate PROTO((rtx, enum machine_mode));
653 static int cse_rtx_varies_p PROTO((rtx));
654 static void remove_invalid_refs PROTO((int));
655 static void remove_invalid_subreg_refs PROTO((int, int, enum machine_mode));
656 static void rehash_using_reg PROTO((rtx));
657 static void invalidate_memory PROTO((void));
658 static void invalidate_for_call PROTO((void));
659 static rtx use_related_value PROTO((rtx, struct table_elt *));
660 static unsigned canon_hash PROTO((rtx, enum machine_mode));
661 static unsigned safe_hash PROTO((rtx, enum machine_mode));
662 static int exp_equiv_p PROTO((rtx, rtx, int, int));
663 static void set_nonvarying_address_components PROTO((rtx, int, rtx *,
664 HOST_WIDE_INT *,
665 HOST_WIDE_INT *));
666 static int refers_to_p PROTO((rtx, rtx));
667 static rtx canon_reg PROTO((rtx, rtx));
668 static void find_best_addr PROTO((rtx, rtx *));
669 static enum rtx_code find_comparison_args PROTO((enum rtx_code, rtx *, rtx *,
670 enum machine_mode *,
671 enum machine_mode *));
672 static rtx cse_gen_binary PROTO((enum rtx_code, enum machine_mode,
673 rtx, rtx));
674 static rtx simplify_plus_minus PROTO((enum rtx_code, enum machine_mode,
675 rtx, rtx));
676 static rtx fold_rtx PROTO((rtx, rtx));
677 static rtx equiv_constant PROTO((rtx));
678 static void record_jump_equiv PROTO((rtx, int));
679 static void record_jump_cond PROTO((enum rtx_code, enum machine_mode,
680 rtx, rtx, int));
681 static void cse_insn PROTO((rtx, rtx));
682 static int note_mem_written PROTO((rtx));
683 static void invalidate_from_clobbers PROTO((rtx));
684 static rtx cse_process_notes PROTO((rtx, rtx));
685 static void cse_around_loop PROTO((rtx));
686 static void invalidate_skipped_set PROTO((rtx, rtx));
687 static void invalidate_skipped_block PROTO((rtx));
688 static void cse_check_loop_start PROTO((rtx, rtx));
689 static void cse_set_around_loop PROTO((rtx, rtx, rtx));
690 static rtx cse_basic_block PROTO((rtx, rtx, struct branch_path *, int));
691 static void count_reg_usage PROTO((rtx, int *, rtx, int));
692 extern void dump_class PROTO((struct table_elt*));
693 static void check_fold_consts PROTO((PTR));
694 static struct cse_reg_info* get_cse_reg_info PROTO((int));
695 static void free_cse_reg_info PROTO((splay_tree_value));
696 static void flush_hash_table PROTO((void));
698 /* Dump the expressions in the equivalence class indicated by CLASSP.
699 This function is used only for debugging. */
700 void
701 dump_class (classp)
702 struct table_elt *classp;
704 struct table_elt *elt;
706 fprintf (stderr, "Equivalence chain for ");
707 print_rtl (stderr, classp->exp);
708 fprintf (stderr, ": \n");
710 for (elt = classp->first_same_value; elt; elt = elt->next_same_value)
712 print_rtl (stderr, elt->exp);
713 fprintf (stderr, "\n");
717 /* Return an estimate of the cost of computing rtx X.
718 One use is in cse, to decide which expression to keep in the hash table.
719 Another is in rtl generation, to pick the cheapest way to multiply.
720 Other uses like the latter are expected in the future. */
722 /* Internal function, to compute cost when X is not a register; called
723 from COST macro to keep it simple. */
725 static int
726 notreg_cost (x)
727 rtx x;
729 return ((GET_CODE (x) == SUBREG
730 && GET_CODE (SUBREG_REG (x)) == REG
731 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT
732 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_INT
733 && (GET_MODE_SIZE (GET_MODE (x))
734 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
735 && subreg_lowpart_p (x)
736 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (x)),
737 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))))
738 ? (CHEAP_REG (SUBREG_REG (x)) ? 0
739 : (REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER ? 1
740 : 2))
741 : rtx_cost (x, SET) * 2);
744 /* Return the right cost to give to an operation
745 to make the cost of the corresponding register-to-register instruction
746 N times that of a fast register-to-register instruction. */
748 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
751 rtx_cost (x, outer_code)
752 rtx x;
753 enum rtx_code outer_code ATTRIBUTE_UNUSED;
755 register int i, j;
756 register enum rtx_code code;
757 register const char *fmt;
758 register int total;
760 if (x == 0)
761 return 0;
763 /* Compute the default costs of certain things.
764 Note that RTX_COSTS can override the defaults. */
766 code = GET_CODE (x);
767 switch (code)
769 case MULT:
770 /* Count multiplication by 2**n as a shift,
771 because if we are considering it, we would output it as a shift. */
772 if (GET_CODE (XEXP (x, 1)) == CONST_INT
773 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
774 total = 2;
775 else
776 total = COSTS_N_INSNS (5);
777 break;
778 case DIV:
779 case UDIV:
780 case MOD:
781 case UMOD:
782 total = COSTS_N_INSNS (7);
783 break;
784 case USE:
785 /* Used in loop.c and combine.c as a marker. */
786 total = 0;
787 break;
788 case ASM_OPERANDS:
789 /* We don't want these to be used in substitutions because
790 we have no way of validating the resulting insn. So assign
791 anything containing an ASM_OPERANDS a very high cost. */
792 total = 1000;
793 break;
794 default:
795 total = 2;
798 switch (code)
800 case REG:
801 return ! CHEAP_REG (x);
803 case SUBREG:
804 /* If we can't tie these modes, make this expensive. The larger
805 the mode, the more expensive it is. */
806 if (! MODES_TIEABLE_P (GET_MODE (x), GET_MODE (SUBREG_REG (x))))
807 return COSTS_N_INSNS (2
808 + GET_MODE_SIZE (GET_MODE (x)) / UNITS_PER_WORD);
809 return 2;
810 #ifdef RTX_COSTS
811 RTX_COSTS (x, code, outer_code);
812 #endif
813 #ifdef CONST_COSTS
814 CONST_COSTS (x, code, outer_code);
815 #endif
817 default:
818 #ifdef DEFAULT_RTX_COSTS
819 DEFAULT_RTX_COSTS(x, code, outer_code);
820 #endif
821 break;
824 /* Sum the costs of the sub-rtx's, plus cost of this operation,
825 which is already in total. */
827 fmt = GET_RTX_FORMAT (code);
828 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
829 if (fmt[i] == 'e')
830 total += rtx_cost (XEXP (x, i), code);
831 else if (fmt[i] == 'E')
832 for (j = 0; j < XVECLEN (x, i); j++)
833 total += rtx_cost (XVECEXP (x, i, j), code);
835 return total;
838 static struct cse_reg_info *
839 get_cse_reg_info (regno)
840 int regno;
842 struct cse_reg_info *cri;
843 splay_tree_node n;
845 /* See if we already have this entry. */
846 n = splay_tree_lookup (cse_reg_info_tree,
847 (splay_tree_key) regno);
848 if (n)
849 cri = (struct cse_reg_info *) (n->value);
850 else
852 /* Get a new cse_reg_info structure. */
853 if (cse_reg_info_free_list)
855 cri = cse_reg_info_free_list;
856 cse_reg_info_free_list = cri->variant.next;
858 else
859 cri = (struct cse_reg_info *) xmalloc (sizeof (struct cse_reg_info));
861 /* Initialize it. */
862 cri->variant.reg_tick = 0;
863 cri->reg_in_table = -1;
864 cri->reg_qty = regno;
866 splay_tree_insert (cse_reg_info_tree,
867 (splay_tree_key) regno,
868 (splay_tree_value) cri);
871 /* Cache this lookup; we tend to be looking up information about the
872 same register several times in a row. */
873 cached_regno = regno;
874 cached_cse_reg_info = cri;
876 return cri;
879 static void
880 free_cse_reg_info (v)
881 splay_tree_value v;
883 struct cse_reg_info *cri = (struct cse_reg_info *) v;
885 cri->variant.next = cse_reg_info_free_list;
886 cse_reg_info_free_list = cri;
889 /* Clear the hash table and initialize each register with its own quantity,
890 for a new basic block. */
892 static void
893 new_basic_block ()
895 register int i;
897 next_qty = max_reg;
899 if (cse_reg_info_tree)
901 splay_tree_delete (cse_reg_info_tree);
902 cached_cse_reg_info = 0;
905 cse_reg_info_tree = splay_tree_new (splay_tree_compare_ints, 0,
906 free_cse_reg_info);
908 CLEAR_HARD_REG_SET (hard_regs_in_table);
910 /* The per-quantity values used to be initialized here, but it is
911 much faster to initialize each as it is made in `make_new_qty'. */
913 for (i = 0; i < NBUCKETS; i++)
915 register struct table_elt *this, *next;
916 for (this = table[i]; this; this = next)
918 next = this->next_same_hash;
919 free_element (this);
923 bzero ((char *) table, sizeof table);
925 prev_insn = 0;
927 #ifdef HAVE_cc0
928 prev_insn_cc0 = 0;
929 #endif
932 /* Say that register REG contains a quantity not in any register before
933 and initialize that quantity. */
935 static void
936 make_new_qty (reg)
937 register int reg;
939 register int q;
941 if (next_qty >= max_qty)
942 abort ();
944 q = REG_QTY (reg) = next_qty++;
945 qty_first_reg[q] = reg;
946 qty_last_reg[q] = reg;
947 qty_const[q] = qty_const_insn[q] = 0;
948 qty_comparison_code[q] = UNKNOWN;
950 reg_next_eqv[reg] = reg_prev_eqv[reg] = -1;
953 /* Make reg NEW equivalent to reg OLD.
954 OLD is not changing; NEW is. */
956 static void
957 make_regs_eqv (new, old)
958 register int new, old;
960 register int lastr, firstr;
961 register int q = REG_QTY (old);
963 /* Nothing should become eqv until it has a "non-invalid" qty number. */
964 if (! REGNO_QTY_VALID_P (old))
965 abort ();
967 REG_QTY (new) = q;
968 firstr = qty_first_reg[q];
969 lastr = qty_last_reg[q];
971 /* Prefer fixed hard registers to anything. Prefer pseudo regs to other
972 hard regs. Among pseudos, if NEW will live longer than any other reg
973 of the same qty, and that is beyond the current basic block,
974 make it the new canonical replacement for this qty. */
975 if (! (firstr < FIRST_PSEUDO_REGISTER && FIXED_REGNO_P (firstr))
976 /* Certain fixed registers might be of the class NO_REGS. This means
977 that not only can they not be allocated by the compiler, but
978 they cannot be used in substitutions or canonicalizations
979 either. */
980 && (new >= FIRST_PSEUDO_REGISTER || REGNO_REG_CLASS (new) != NO_REGS)
981 && ((new < FIRST_PSEUDO_REGISTER && FIXED_REGNO_P (new))
982 || (new >= FIRST_PSEUDO_REGISTER
983 && (firstr < FIRST_PSEUDO_REGISTER
984 || ((uid_cuid[REGNO_LAST_UID (new)] > cse_basic_block_end
985 || (uid_cuid[REGNO_FIRST_UID (new)]
986 < cse_basic_block_start))
987 && (uid_cuid[REGNO_LAST_UID (new)]
988 > uid_cuid[REGNO_LAST_UID (firstr)]))))))
990 reg_prev_eqv[firstr] = new;
991 reg_next_eqv[new] = firstr;
992 reg_prev_eqv[new] = -1;
993 qty_first_reg[q] = new;
995 else
997 /* If NEW is a hard reg (known to be non-fixed), insert at end.
998 Otherwise, insert before any non-fixed hard regs that are at the
999 end. Registers of class NO_REGS cannot be used as an
1000 equivalent for anything. */
1001 while (lastr < FIRST_PSEUDO_REGISTER && reg_prev_eqv[lastr] >= 0
1002 && (REGNO_REG_CLASS (lastr) == NO_REGS || ! FIXED_REGNO_P (lastr))
1003 && new >= FIRST_PSEUDO_REGISTER)
1004 lastr = reg_prev_eqv[lastr];
1005 reg_next_eqv[new] = reg_next_eqv[lastr];
1006 if (reg_next_eqv[lastr] >= 0)
1007 reg_prev_eqv[reg_next_eqv[lastr]] = new;
1008 else
1009 qty_last_reg[q] = new;
1010 reg_next_eqv[lastr] = new;
1011 reg_prev_eqv[new] = lastr;
1015 /* Remove REG from its equivalence class. */
1017 static void
1018 delete_reg_equiv (reg)
1019 register int reg;
1021 register int q = REG_QTY (reg);
1022 register int p, n;
1024 /* If invalid, do nothing. */
1025 if (q == reg)
1026 return;
1028 p = reg_prev_eqv[reg];
1029 n = reg_next_eqv[reg];
1031 if (n != -1)
1032 reg_prev_eqv[n] = p;
1033 else
1034 qty_last_reg[q] = p;
1035 if (p != -1)
1036 reg_next_eqv[p] = n;
1037 else
1038 qty_first_reg[q] = n;
1040 REG_QTY (reg) = reg;
1043 /* Remove any invalid expressions from the hash table
1044 that refer to any of the registers contained in expression X.
1046 Make sure that newly inserted references to those registers
1047 as subexpressions will be considered valid.
1049 mention_regs is not called when a register itself
1050 is being stored in the table.
1052 Return 1 if we have done something that may have changed the hash code
1053 of X. */
1055 static int
1056 mention_regs (x)
1057 rtx x;
1059 register enum rtx_code code;
1060 register int i, j;
1061 register const char *fmt;
1062 register int changed = 0;
1064 if (x == 0)
1065 return 0;
1067 code = GET_CODE (x);
1068 if (code == REG)
1070 register int regno = REGNO (x);
1071 register int endregno
1072 = regno + (regno >= FIRST_PSEUDO_REGISTER ? 1
1073 : HARD_REGNO_NREGS (regno, GET_MODE (x)));
1074 int i;
1076 for (i = regno; i < endregno; i++)
1078 if (REG_IN_TABLE (i) >= 0 && REG_IN_TABLE (i) != REG_TICK (i))
1079 remove_invalid_refs (i);
1081 REG_IN_TABLE (i) = REG_TICK (i);
1084 return 0;
1087 /* If this is a SUBREG, we don't want to discard other SUBREGs of the same
1088 pseudo if they don't use overlapping words. We handle only pseudos
1089 here for simplicity. */
1090 if (code == SUBREG && GET_CODE (SUBREG_REG (x)) == REG
1091 && REGNO (SUBREG_REG (x)) >= FIRST_PSEUDO_REGISTER)
1093 int i = REGNO (SUBREG_REG (x));
1095 if (REG_IN_TABLE (i) >= 0 && REG_IN_TABLE (i) != REG_TICK (i))
1097 /* If reg_tick has been incremented more than once since
1098 reg_in_table was last set, that means that the entire
1099 register has been set before, so discard anything memorized
1100 for the entrire register, including all SUBREG expressions. */
1101 if (REG_IN_TABLE (i) != REG_TICK (i) - 1)
1102 remove_invalid_refs (i);
1103 else
1104 remove_invalid_subreg_refs (i, SUBREG_WORD (x), GET_MODE (x));
1107 REG_IN_TABLE (i) = REG_TICK (i);
1108 return 0;
1111 /* If X is a comparison or a COMPARE and either operand is a register
1112 that does not have a quantity, give it one. This is so that a later
1113 call to record_jump_equiv won't cause X to be assigned a different
1114 hash code and not found in the table after that call.
1116 It is not necessary to do this here, since rehash_using_reg can
1117 fix up the table later, but doing this here eliminates the need to
1118 call that expensive function in the most common case where the only
1119 use of the register is in the comparison. */
1121 if (code == COMPARE || GET_RTX_CLASS (code) == '<')
1123 if (GET_CODE (XEXP (x, 0)) == REG
1124 && ! REGNO_QTY_VALID_P (REGNO (XEXP (x, 0))))
1125 if (insert_regs (XEXP (x, 0), NULL_PTR, 0))
1127 rehash_using_reg (XEXP (x, 0));
1128 changed = 1;
1131 if (GET_CODE (XEXP (x, 1)) == REG
1132 && ! REGNO_QTY_VALID_P (REGNO (XEXP (x, 1))))
1133 if (insert_regs (XEXP (x, 1), NULL_PTR, 0))
1135 rehash_using_reg (XEXP (x, 1));
1136 changed = 1;
1140 fmt = GET_RTX_FORMAT (code);
1141 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1142 if (fmt[i] == 'e')
1143 changed |= mention_regs (XEXP (x, i));
1144 else if (fmt[i] == 'E')
1145 for (j = 0; j < XVECLEN (x, i); j++)
1146 changed |= mention_regs (XVECEXP (x, i, j));
1148 return changed;
1151 /* Update the register quantities for inserting X into the hash table
1152 with a value equivalent to CLASSP.
1153 (If the class does not contain a REG, it is irrelevant.)
1154 If MODIFIED is nonzero, X is a destination; it is being modified.
1155 Note that delete_reg_equiv should be called on a register
1156 before insert_regs is done on that register with MODIFIED != 0.
1158 Nonzero value means that elements of reg_qty have changed
1159 so X's hash code may be different. */
1161 static int
1162 insert_regs (x, classp, modified)
1163 rtx x;
1164 struct table_elt *classp;
1165 int modified;
1167 if (GET_CODE (x) == REG)
1169 register int regno = REGNO (x);
1171 /* If REGNO is in the equivalence table already but is of the
1172 wrong mode for that equivalence, don't do anything here. */
1174 if (REGNO_QTY_VALID_P (regno)
1175 && qty_mode[REG_QTY (regno)] != GET_MODE (x))
1176 return 0;
1178 if (modified || ! REGNO_QTY_VALID_P (regno))
1180 if (classp)
1181 for (classp = classp->first_same_value;
1182 classp != 0;
1183 classp = classp->next_same_value)
1184 if (GET_CODE (classp->exp) == REG
1185 && GET_MODE (classp->exp) == GET_MODE (x))
1187 make_regs_eqv (regno, REGNO (classp->exp));
1188 return 1;
1191 make_new_qty (regno);
1192 qty_mode[REG_QTY (regno)] = GET_MODE (x);
1193 return 1;
1196 return 0;
1199 /* If X is a SUBREG, we will likely be inserting the inner register in the
1200 table. If that register doesn't have an assigned quantity number at
1201 this point but does later, the insertion that we will be doing now will
1202 not be accessible because its hash code will have changed. So assign
1203 a quantity number now. */
1205 else if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == REG
1206 && ! REGNO_QTY_VALID_P (REGNO (SUBREG_REG (x))))
1208 int regno = REGNO (SUBREG_REG (x));
1210 insert_regs (SUBREG_REG (x), NULL_PTR, 0);
1211 /* Mention_regs checks if REG_TICK is exactly one larger than
1212 REG_IN_TABLE to find out if there was only a single preceding
1213 invalidation - for the SUBREG - or another one, which would be
1214 for the full register. Since we don't invalidate the SUBREG
1215 here first, we might have to bump up REG_TICK so that mention_regs
1216 will do the right thing. */
1217 if (REG_IN_TABLE (regno) >= 0
1218 && REG_TICK (regno) == REG_IN_TABLE (regno) + 1)
1219 REG_TICK (regno)++;
1220 mention_regs (x);
1221 return 1;
1223 else
1224 return mention_regs (x);
1227 /* Look in or update the hash table. */
1229 /* Put the element ELT on the list of free elements. */
1231 static void
1232 free_element (elt)
1233 struct table_elt *elt;
1235 elt->next_same_hash = free_element_chain;
1236 free_element_chain = elt;
1239 /* Return an element that is free for use. */
1241 static struct table_elt *
1242 get_element ()
1244 struct table_elt *elt = free_element_chain;
1245 if (elt)
1247 free_element_chain = elt->next_same_hash;
1248 return elt;
1250 n_elements_made++;
1251 return (struct table_elt *) oballoc (sizeof (struct table_elt));
1254 /* Remove table element ELT from use in the table.
1255 HASH is its hash code, made using the HASH macro.
1256 It's an argument because often that is known in advance
1257 and we save much time not recomputing it. */
1259 static void
1260 remove_from_table (elt, hash)
1261 register struct table_elt *elt;
1262 unsigned hash;
1264 if (elt == 0)
1265 return;
1267 /* Mark this element as removed. See cse_insn. */
1268 elt->first_same_value = 0;
1270 /* Remove the table element from its equivalence class. */
1273 register struct table_elt *prev = elt->prev_same_value;
1274 register struct table_elt *next = elt->next_same_value;
1276 if (next) next->prev_same_value = prev;
1278 if (prev)
1279 prev->next_same_value = next;
1280 else
1282 register struct table_elt *newfirst = next;
1283 while (next)
1285 next->first_same_value = newfirst;
1286 next = next->next_same_value;
1291 /* Remove the table element from its hash bucket. */
1294 register struct table_elt *prev = elt->prev_same_hash;
1295 register struct table_elt *next = elt->next_same_hash;
1297 if (next) next->prev_same_hash = prev;
1299 if (prev)
1300 prev->next_same_hash = next;
1301 else if (table[hash] == elt)
1302 table[hash] = next;
1303 else
1305 /* This entry is not in the proper hash bucket. This can happen
1306 when two classes were merged by `merge_equiv_classes'. Search
1307 for the hash bucket that it heads. This happens only very
1308 rarely, so the cost is acceptable. */
1309 for (hash = 0; hash < NBUCKETS; hash++)
1310 if (table[hash] == elt)
1311 table[hash] = next;
1315 /* Remove the table element from its related-value circular chain. */
1317 if (elt->related_value != 0 && elt->related_value != elt)
1319 register struct table_elt *p = elt->related_value;
1320 while (p->related_value != elt)
1321 p = p->related_value;
1322 p->related_value = elt->related_value;
1323 if (p->related_value == p)
1324 p->related_value = 0;
1327 free_element (elt);
1330 /* Look up X in the hash table and return its table element,
1331 or 0 if X is not in the table.
1333 MODE is the machine-mode of X, or if X is an integer constant
1334 with VOIDmode then MODE is the mode with which X will be used.
1336 Here we are satisfied to find an expression whose tree structure
1337 looks like X. */
1339 static struct table_elt *
1340 lookup (x, hash, mode)
1341 rtx x;
1342 unsigned hash;
1343 enum machine_mode mode;
1345 register struct table_elt *p;
1347 for (p = table[hash]; p; p = p->next_same_hash)
1348 if (mode == p->mode && ((x == p->exp && GET_CODE (x) == REG)
1349 || exp_equiv_p (x, p->exp, GET_CODE (x) != REG, 0)))
1350 return p;
1352 return 0;
1355 /* Like `lookup' but don't care whether the table element uses invalid regs.
1356 Also ignore discrepancies in the machine mode of a register. */
1358 static struct table_elt *
1359 lookup_for_remove (x, hash, mode)
1360 rtx x;
1361 unsigned hash;
1362 enum machine_mode mode;
1364 register struct table_elt *p;
1366 if (GET_CODE (x) == REG)
1368 int regno = REGNO (x);
1369 /* Don't check the machine mode when comparing registers;
1370 invalidating (REG:SI 0) also invalidates (REG:DF 0). */
1371 for (p = table[hash]; p; p = p->next_same_hash)
1372 if (GET_CODE (p->exp) == REG
1373 && REGNO (p->exp) == regno)
1374 return p;
1376 else
1378 for (p = table[hash]; p; p = p->next_same_hash)
1379 if (mode == p->mode && (x == p->exp || exp_equiv_p (x, p->exp, 0, 0)))
1380 return p;
1383 return 0;
1386 /* Look for an expression equivalent to X and with code CODE.
1387 If one is found, return that expression. */
1389 static rtx
1390 lookup_as_function (x, code)
1391 rtx x;
1392 enum rtx_code code;
1394 register struct table_elt *p = lookup (x, safe_hash (x, VOIDmode) % NBUCKETS,
1395 GET_MODE (x));
1396 /* If we are looking for a CONST_INT, the mode doesn't really matter, as
1397 long as we are narrowing. So if we looked in vain for a mode narrower
1398 than word_mode before, look for word_mode now. */
1399 if (p == 0 && code == CONST_INT
1400 && GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (word_mode))
1402 x = copy_rtx (x);
1403 PUT_MODE (x, word_mode);
1404 p = lookup (x, safe_hash (x, VOIDmode) % NBUCKETS, word_mode);
1407 if (p == 0)
1408 return 0;
1410 for (p = p->first_same_value; p; p = p->next_same_value)
1412 if (GET_CODE (p->exp) == code
1413 /* Make sure this is a valid entry in the table. */
1414 && exp_equiv_p (p->exp, p->exp, 1, 0))
1415 return p->exp;
1418 return 0;
1421 /* Insert X in the hash table, assuming HASH is its hash code
1422 and CLASSP is an element of the class it should go in
1423 (or 0 if a new class should be made).
1424 It is inserted at the proper position to keep the class in
1425 the order cheapest first.
1427 MODE is the machine-mode of X, or if X is an integer constant
1428 with VOIDmode then MODE is the mode with which X will be used.
1430 For elements of equal cheapness, the most recent one
1431 goes in front, except that the first element in the list
1432 remains first unless a cheaper element is added. The order of
1433 pseudo-registers does not matter, as canon_reg will be called to
1434 find the cheapest when a register is retrieved from the table.
1436 The in_memory field in the hash table element is set to 0.
1437 The caller must set it nonzero if appropriate.
1439 You should call insert_regs (X, CLASSP, MODIFY) before calling here,
1440 and if insert_regs returns a nonzero value
1441 you must then recompute its hash code before calling here.
1443 If necessary, update table showing constant values of quantities. */
1445 #define CHEAPER(X,Y) ((X)->cost < (Y)->cost)
1447 static struct table_elt *
1448 insert (x, classp, hash, mode)
1449 register rtx x;
1450 register struct table_elt *classp;
1451 unsigned hash;
1452 enum machine_mode mode;
1454 register struct table_elt *elt;
1456 /* If X is a register and we haven't made a quantity for it,
1457 something is wrong. */
1458 if (GET_CODE (x) == REG && ! REGNO_QTY_VALID_P (REGNO (x)))
1459 abort ();
1461 /* If X is a hard register, show it is being put in the table. */
1462 if (GET_CODE (x) == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
1464 int regno = REGNO (x);
1465 int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
1466 int i;
1468 for (i = regno; i < endregno; i++)
1469 SET_HARD_REG_BIT (hard_regs_in_table, i);
1472 /* If X is a label, show we recorded it. */
1473 if (GET_CODE (x) == LABEL_REF
1474 || (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == PLUS
1475 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF))
1476 recorded_label_ref = 1;
1478 /* Put an element for X into the right hash bucket. */
1480 elt = get_element ();
1481 elt->exp = x;
1482 elt->cost = COST (x);
1483 elt->next_same_value = 0;
1484 elt->prev_same_value = 0;
1485 elt->next_same_hash = table[hash];
1486 elt->prev_same_hash = 0;
1487 elt->related_value = 0;
1488 elt->in_memory = 0;
1489 elt->mode = mode;
1490 elt->is_const = (CONSTANT_P (x)
1491 /* GNU C++ takes advantage of this for `this'
1492 (and other const values). */
1493 || (RTX_UNCHANGING_P (x)
1494 && GET_CODE (x) == REG
1495 && REGNO (x) >= FIRST_PSEUDO_REGISTER)
1496 || FIXED_BASE_PLUS_P (x));
1498 if (table[hash])
1499 table[hash]->prev_same_hash = elt;
1500 table[hash] = elt;
1502 /* Put it into the proper value-class. */
1503 if (classp)
1505 classp = classp->first_same_value;
1506 if (CHEAPER (elt, classp))
1507 /* Insert at the head of the class */
1509 register struct table_elt *p;
1510 elt->next_same_value = classp;
1511 classp->prev_same_value = elt;
1512 elt->first_same_value = elt;
1514 for (p = classp; p; p = p->next_same_value)
1515 p->first_same_value = elt;
1517 else
1519 /* Insert not at head of the class. */
1520 /* Put it after the last element cheaper than X. */
1521 register struct table_elt *p, *next;
1522 for (p = classp; (next = p->next_same_value) && CHEAPER (next, elt);
1523 p = next);
1524 /* Put it after P and before NEXT. */
1525 elt->next_same_value = next;
1526 if (next)
1527 next->prev_same_value = elt;
1528 elt->prev_same_value = p;
1529 p->next_same_value = elt;
1530 elt->first_same_value = classp;
1533 else
1534 elt->first_same_value = elt;
1536 /* If this is a constant being set equivalent to a register or a register
1537 being set equivalent to a constant, note the constant equivalence.
1539 If this is a constant, it cannot be equivalent to a different constant,
1540 and a constant is the only thing that can be cheaper than a register. So
1541 we know the register is the head of the class (before the constant was
1542 inserted).
1544 If this is a register that is not already known equivalent to a
1545 constant, we must check the entire class.
1547 If this is a register that is already known equivalent to an insn,
1548 update `qty_const_insn' to show that `this_insn' is the latest
1549 insn making that quantity equivalent to the constant. */
1551 if (elt->is_const && classp && GET_CODE (classp->exp) == REG
1552 && GET_CODE (x) != REG)
1554 qty_const[REG_QTY (REGNO (classp->exp))]
1555 = gen_lowpart_if_possible (qty_mode[REG_QTY (REGNO (classp->exp))], x);
1556 qty_const_insn[REG_QTY (REGNO (classp->exp))] = this_insn;
1559 else if (GET_CODE (x) == REG && classp && ! qty_const[REG_QTY (REGNO (x))]
1560 && ! elt->is_const)
1562 register struct table_elt *p;
1564 for (p = classp; p != 0; p = p->next_same_value)
1566 if (p->is_const && GET_CODE (p->exp) != REG)
1568 qty_const[REG_QTY (REGNO (x))]
1569 = gen_lowpart_if_possible (GET_MODE (x), p->exp);
1570 qty_const_insn[REG_QTY (REGNO (x))] = this_insn;
1571 break;
1576 else if (GET_CODE (x) == REG && qty_const[REG_QTY (REGNO (x))]
1577 && GET_MODE (x) == qty_mode[REG_QTY (REGNO (x))])
1578 qty_const_insn[REG_QTY (REGNO (x))] = this_insn;
1580 /* If this is a constant with symbolic value,
1581 and it has a term with an explicit integer value,
1582 link it up with related expressions. */
1583 if (GET_CODE (x) == CONST)
1585 rtx subexp = get_related_value (x);
1586 unsigned subhash;
1587 struct table_elt *subelt, *subelt_prev;
1589 if (subexp != 0)
1591 /* Get the integer-free subexpression in the hash table. */
1592 subhash = safe_hash (subexp, mode) % NBUCKETS;
1593 subelt = lookup (subexp, subhash, mode);
1594 if (subelt == 0)
1595 subelt = insert (subexp, NULL_PTR, subhash, mode);
1596 /* Initialize SUBELT's circular chain if it has none. */
1597 if (subelt->related_value == 0)
1598 subelt->related_value = subelt;
1599 /* Find the element in the circular chain that precedes SUBELT. */
1600 subelt_prev = subelt;
1601 while (subelt_prev->related_value != subelt)
1602 subelt_prev = subelt_prev->related_value;
1603 /* Put new ELT into SUBELT's circular chain just before SUBELT.
1604 This way the element that follows SUBELT is the oldest one. */
1605 elt->related_value = subelt_prev->related_value;
1606 subelt_prev->related_value = elt;
1610 return elt;
1613 /* Given two equivalence classes, CLASS1 and CLASS2, put all the entries from
1614 CLASS2 into CLASS1. This is done when we have reached an insn which makes
1615 the two classes equivalent.
1617 CLASS1 will be the surviving class; CLASS2 should not be used after this
1618 call.
1620 Any invalid entries in CLASS2 will not be copied. */
1622 static void
1623 merge_equiv_classes (class1, class2)
1624 struct table_elt *class1, *class2;
1626 struct table_elt *elt, *next, *new;
1628 /* Ensure we start with the head of the classes. */
1629 class1 = class1->first_same_value;
1630 class2 = class2->first_same_value;
1632 /* If they were already equal, forget it. */
1633 if (class1 == class2)
1634 return;
1636 for (elt = class2; elt; elt = next)
1638 unsigned hash;
1639 rtx exp = elt->exp;
1640 enum machine_mode mode = elt->mode;
1642 next = elt->next_same_value;
1644 /* Remove old entry, make a new one in CLASS1's class.
1645 Don't do this for invalid entries as we cannot find their
1646 hash code (it also isn't necessary). */
1647 if (GET_CODE (exp) == REG || exp_equiv_p (exp, exp, 1, 0))
1649 hash_arg_in_memory = 0;
1650 hash_arg_in_struct = 0;
1651 hash = HASH (exp, mode);
1653 if (GET_CODE (exp) == REG)
1654 delete_reg_equiv (REGNO (exp));
1656 remove_from_table (elt, hash);
1658 if (insert_regs (exp, class1, 0))
1660 rehash_using_reg (exp);
1661 hash = HASH (exp, mode);
1663 new = insert (exp, class1, hash, mode);
1664 new->in_memory = hash_arg_in_memory;
1665 new->in_struct = hash_arg_in_struct;
1671 /* Flush the entire hash table. */
1673 static void
1674 flush_hash_table ()
1676 int i;
1677 struct table_elt *p;
1679 for (i = 0; i < NBUCKETS; i++)
1680 for (p = table[i]; p; p = table[i])
1682 /* Note that invalidate can remove elements
1683 after P in the current hash chain. */
1684 if (GET_CODE (p->exp) == REG)
1685 invalidate (p->exp, p->mode);
1686 else
1687 remove_from_table (p, i);
1692 /* Remove from the hash table, or mark as invalid,
1693 all expressions whose values could be altered by storing in X.
1694 X is a register, a subreg, or a memory reference with nonvarying address
1695 (because, when a memory reference with a varying address is stored in,
1696 all memory references are removed by invalidate_memory
1697 so specific invalidation is superfluous).
1698 FULL_MODE, if not VOIDmode, indicates that this much should be invalidated
1699 instead of just the amount indicated by the mode of X. This is only used
1700 for bitfield stores into memory.
1702 A nonvarying address may be just a register or just
1703 a symbol reference, or it may be either of those plus
1704 a numeric offset. */
1706 static void
1707 invalidate (x, full_mode)
1708 rtx x;
1709 enum machine_mode full_mode;
1711 register int i;
1712 register struct table_elt *p;
1714 /* If X is a register, dependencies on its contents
1715 are recorded through the qty number mechanism.
1716 Just change the qty number of the register,
1717 mark it as invalid for expressions that refer to it,
1718 and remove it itself. */
1720 if (GET_CODE (x) == REG)
1722 register int regno = REGNO (x);
1723 register unsigned hash = HASH (x, GET_MODE (x));
1725 /* Remove REGNO from any quantity list it might be on and indicate
1726 that its value might have changed. If it is a pseudo, remove its
1727 entry from the hash table.
1729 For a hard register, we do the first two actions above for any
1730 additional hard registers corresponding to X. Then, if any of these
1731 registers are in the table, we must remove any REG entries that
1732 overlap these registers. */
1734 delete_reg_equiv (regno);
1735 REG_TICK (regno)++;
1737 if (regno >= FIRST_PSEUDO_REGISTER)
1739 /* Because a register can be referenced in more than one mode,
1740 we might have to remove more than one table entry. */
1742 struct table_elt *elt;
1744 while ((elt = lookup_for_remove (x, hash, GET_MODE (x))))
1745 remove_from_table (elt, hash);
1747 else
1749 HOST_WIDE_INT in_table
1750 = TEST_HARD_REG_BIT (hard_regs_in_table, regno);
1751 int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
1752 int tregno, tendregno;
1753 register struct table_elt *p, *next;
1755 CLEAR_HARD_REG_BIT (hard_regs_in_table, regno);
1757 for (i = regno + 1; i < endregno; i++)
1759 in_table |= TEST_HARD_REG_BIT (hard_regs_in_table, i);
1760 CLEAR_HARD_REG_BIT (hard_regs_in_table, i);
1761 delete_reg_equiv (i);
1762 REG_TICK (i)++;
1765 if (in_table)
1766 for (hash = 0; hash < NBUCKETS; hash++)
1767 for (p = table[hash]; p; p = next)
1769 next = p->next_same_hash;
1771 if (GET_CODE (p->exp) != REG
1772 || REGNO (p->exp) >= FIRST_PSEUDO_REGISTER)
1773 continue;
1775 tregno = REGNO (p->exp);
1776 tendregno
1777 = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (p->exp));
1778 if (tendregno > regno && tregno < endregno)
1779 remove_from_table (p, hash);
1783 return;
1786 if (GET_CODE (x) == SUBREG)
1788 if (GET_CODE (SUBREG_REG (x)) != REG)
1789 abort ();
1790 invalidate (SUBREG_REG (x), VOIDmode);
1791 return;
1794 /* If X is a parallel, invalidate all of its elements. */
1796 if (GET_CODE (x) == PARALLEL)
1798 for (i = XVECLEN (x, 0) - 1; i >= 0 ; --i)
1799 invalidate (XVECEXP (x, 0, i), VOIDmode);
1800 return;
1803 /* If X is an expr_list, this is part of a disjoint return value;
1804 extract the location in question ignoring the offset. */
1806 if (GET_CODE (x) == EXPR_LIST)
1808 invalidate (XEXP (x, 0), VOIDmode);
1809 return;
1812 /* X is not a register; it must be a memory reference with
1813 a nonvarying address. Remove all hash table elements
1814 that refer to overlapping pieces of memory. */
1816 if (GET_CODE (x) != MEM)
1817 abort ();
1819 if (full_mode == VOIDmode)
1820 full_mode = GET_MODE (x);
1822 for (i = 0; i < NBUCKETS; i++)
1824 register struct table_elt *next;
1825 for (p = table[i]; p; p = next)
1827 next = p->next_same_hash;
1828 /* Invalidate ASM_OPERANDS which reference memory (this is easier
1829 than checking all the aliases). */
1830 if (p->in_memory
1831 && (GET_CODE (p->exp) != MEM
1832 || true_dependence (x, full_mode, p->exp, cse_rtx_varies_p)))
1833 remove_from_table (p, i);
1838 /* Remove all expressions that refer to register REGNO,
1839 since they are already invalid, and we are about to
1840 mark that register valid again and don't want the old
1841 expressions to reappear as valid. */
1843 static void
1844 remove_invalid_refs (regno)
1845 int regno;
1847 register int i;
1848 register struct table_elt *p, *next;
1850 for (i = 0; i < NBUCKETS; i++)
1851 for (p = table[i]; p; p = next)
1853 next = p->next_same_hash;
1854 if (GET_CODE (p->exp) != REG
1855 && refers_to_regno_p (regno, regno + 1, p->exp, NULL_PTR))
1856 remove_from_table (p, i);
1860 /* Likewise for a subreg with subreg_reg WORD and mode MODE. */
1861 static void
1862 remove_invalid_subreg_refs (regno, word, mode)
1863 int regno;
1864 int word;
1865 enum machine_mode mode;
1867 register int i;
1868 register struct table_elt *p, *next;
1869 int end = word + (GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD;
1871 for (i = 0; i < NBUCKETS; i++)
1872 for (p = table[i]; p; p = next)
1874 rtx exp;
1875 next = p->next_same_hash;
1877 exp = p->exp;
1878 if (GET_CODE (p->exp) != REG
1879 && (GET_CODE (exp) != SUBREG
1880 || GET_CODE (SUBREG_REG (exp)) != REG
1881 || REGNO (SUBREG_REG (exp)) != regno
1882 || (((SUBREG_WORD (exp)
1883 + (GET_MODE_SIZE (GET_MODE (exp)) - 1) / UNITS_PER_WORD)
1884 >= word)
1885 && SUBREG_WORD (exp) <= end))
1886 && refers_to_regno_p (regno, regno + 1, p->exp, NULL_PTR))
1887 remove_from_table (p, i);
1891 /* Recompute the hash codes of any valid entries in the hash table that
1892 reference X, if X is a register, or SUBREG_REG (X) if X is a SUBREG.
1894 This is called when we make a jump equivalence. */
1896 static void
1897 rehash_using_reg (x)
1898 rtx x;
1900 unsigned int i;
1901 struct table_elt *p, *next;
1902 unsigned hash;
1904 if (GET_CODE (x) == SUBREG)
1905 x = SUBREG_REG (x);
1907 /* If X is not a register or if the register is known not to be in any
1908 valid entries in the table, we have no work to do. */
1910 if (GET_CODE (x) != REG
1911 || REG_IN_TABLE (REGNO (x)) < 0
1912 || REG_IN_TABLE (REGNO (x)) != REG_TICK (REGNO (x)))
1913 return;
1915 /* Scan all hash chains looking for valid entries that mention X.
1916 If we find one and it is in the wrong hash chain, move it. We can skip
1917 objects that are registers, since they are handled specially. */
1919 for (i = 0; i < NBUCKETS; i++)
1920 for (p = table[i]; p; p = next)
1922 next = p->next_same_hash;
1923 if (GET_CODE (p->exp) != REG && reg_mentioned_p (x, p->exp)
1924 && exp_equiv_p (p->exp, p->exp, 1, 0)
1925 && i != (hash = safe_hash (p->exp, p->mode) % NBUCKETS))
1927 if (p->next_same_hash)
1928 p->next_same_hash->prev_same_hash = p->prev_same_hash;
1930 if (p->prev_same_hash)
1931 p->prev_same_hash->next_same_hash = p->next_same_hash;
1932 else
1933 table[i] = p->next_same_hash;
1935 p->next_same_hash = table[hash];
1936 p->prev_same_hash = 0;
1937 if (table[hash])
1938 table[hash]->prev_same_hash = p;
1939 table[hash] = p;
1944 /* Remove from the hash table any expression that is a call-clobbered
1945 register. Also update their TICK values. */
1947 static void
1948 invalidate_for_call ()
1950 int regno, endregno;
1951 int i;
1952 unsigned hash;
1953 struct table_elt *p, *next;
1954 int in_table = 0;
1956 /* Go through all the hard registers. For each that is clobbered in
1957 a CALL_INSN, remove the register from quantity chains and update
1958 reg_tick if defined. Also see if any of these registers is currently
1959 in the table. */
1961 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1962 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
1964 delete_reg_equiv (regno);
1965 if (REG_TICK (regno) >= 0)
1966 REG_TICK (regno)++;
1968 in_table |= (TEST_HARD_REG_BIT (hard_regs_in_table, regno) != 0);
1971 /* In the case where we have no call-clobbered hard registers in the
1972 table, we are done. Otherwise, scan the table and remove any
1973 entry that overlaps a call-clobbered register. */
1975 if (in_table)
1976 for (hash = 0; hash < NBUCKETS; hash++)
1977 for (p = table[hash]; p; p = next)
1979 next = p->next_same_hash;
1981 if (p->in_memory)
1983 remove_from_table (p, hash);
1984 continue;
1987 if (GET_CODE (p->exp) != REG
1988 || REGNO (p->exp) >= FIRST_PSEUDO_REGISTER)
1989 continue;
1991 regno = REGNO (p->exp);
1992 endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (p->exp));
1994 for (i = regno; i < endregno; i++)
1995 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1997 remove_from_table (p, hash);
1998 break;
2003 /* Given an expression X of type CONST,
2004 and ELT which is its table entry (or 0 if it
2005 is not in the hash table),
2006 return an alternate expression for X as a register plus integer.
2007 If none can be found, return 0. */
2009 static rtx
2010 use_related_value (x, elt)
2011 rtx x;
2012 struct table_elt *elt;
2014 register struct table_elt *relt = 0;
2015 register struct table_elt *p, *q;
2016 HOST_WIDE_INT offset;
2018 /* First, is there anything related known?
2019 If we have a table element, we can tell from that.
2020 Otherwise, must look it up. */
2022 if (elt != 0 && elt->related_value != 0)
2023 relt = elt;
2024 else if (elt == 0 && GET_CODE (x) == CONST)
2026 rtx subexp = get_related_value (x);
2027 if (subexp != 0)
2028 relt = lookup (subexp,
2029 safe_hash (subexp, GET_MODE (subexp)) % NBUCKETS,
2030 GET_MODE (subexp));
2033 if (relt == 0)
2034 return 0;
2036 /* Search all related table entries for one that has an
2037 equivalent register. */
2039 p = relt;
2040 while (1)
2042 /* This loop is strange in that it is executed in two different cases.
2043 The first is when X is already in the table. Then it is searching
2044 the RELATED_VALUE list of X's class (RELT). The second case is when
2045 X is not in the table. Then RELT points to a class for the related
2046 value.
2048 Ensure that, whatever case we are in, that we ignore classes that have
2049 the same value as X. */
2051 if (rtx_equal_p (x, p->exp))
2052 q = 0;
2053 else
2054 for (q = p->first_same_value; q; q = q->next_same_value)
2055 if (GET_CODE (q->exp) == REG)
2056 break;
2058 if (q)
2059 break;
2061 p = p->related_value;
2063 /* We went all the way around, so there is nothing to be found.
2064 Alternatively, perhaps RELT was in the table for some other reason
2065 and it has no related values recorded. */
2066 if (p == relt || p == 0)
2067 break;
2070 if (q == 0)
2071 return 0;
2073 offset = (get_integer_term (x) - get_integer_term (p->exp));
2074 /* Note: OFFSET may be 0 if P->xexp and X are related by commutativity. */
2075 return plus_constant (q->exp, offset);
2078 /* Hash an rtx. We are careful to make sure the value is never negative.
2079 Equivalent registers hash identically.
2080 MODE is used in hashing for CONST_INTs only;
2081 otherwise the mode of X is used.
2083 Store 1 in do_not_record if any subexpression is volatile.
2085 Store 1 in hash_arg_in_memory if X contains a MEM rtx
2086 which does not have the RTX_UNCHANGING_P bit set.
2087 In this case, also store 1 in hash_arg_in_struct
2088 if there is a MEM rtx which has the MEM_IN_STRUCT_P bit set.
2090 Note that cse_insn knows that the hash code of a MEM expression
2091 is just (int) MEM plus the hash code of the address. */
2093 static unsigned
2094 canon_hash (x, mode)
2095 rtx x;
2096 enum machine_mode mode;
2098 register int i, j;
2099 register unsigned hash = 0;
2100 register enum rtx_code code;
2101 register const char *fmt;
2103 /* repeat is used to turn tail-recursion into iteration. */
2104 repeat:
2105 if (x == 0)
2106 return hash;
2108 code = GET_CODE (x);
2109 switch (code)
2111 case REG:
2113 register int regno = REGNO (x);
2115 /* On some machines, we can't record any non-fixed hard register,
2116 because extending its life will cause reload problems. We
2117 consider ap, fp, and sp to be fixed for this purpose.
2119 We also consider CCmode registers to be fixed for this purpose;
2120 failure to do so leads to failure to simplify 0<100 type of
2121 conditionals.
2123 On all machines, we can't record any global registers. */
2125 if (regno < FIRST_PSEUDO_REGISTER
2126 && (global_regs[regno]
2127 || (SMALL_REGISTER_CLASSES
2128 && ! fixed_regs[regno]
2129 && regno != FRAME_POINTER_REGNUM
2130 && regno != HARD_FRAME_POINTER_REGNUM
2131 && regno != ARG_POINTER_REGNUM
2132 && regno != STACK_POINTER_REGNUM
2133 && GET_MODE_CLASS (GET_MODE (x)) != MODE_CC)))
2135 do_not_record = 1;
2136 return 0;
2138 hash += ((unsigned) REG << 7) + (unsigned) REG_QTY (regno);
2139 return hash;
2142 /* We handle SUBREG of a REG specially because the underlying
2143 reg changes its hash value with every value change; we don't
2144 want to have to forget unrelated subregs when one subreg changes. */
2145 case SUBREG:
2147 if (GET_CODE (SUBREG_REG (x)) == REG)
2149 hash += (((unsigned) SUBREG << 7)
2150 + REGNO (SUBREG_REG (x)) + SUBREG_WORD (x));
2151 return hash;
2153 break;
2156 case CONST_INT:
2158 unsigned HOST_WIDE_INT tem = INTVAL (x);
2159 hash += ((unsigned) CONST_INT << 7) + (unsigned) mode + tem;
2160 return hash;
2163 case CONST_DOUBLE:
2164 /* This is like the general case, except that it only counts
2165 the integers representing the constant. */
2166 hash += (unsigned) code + (unsigned) GET_MODE (x);
2167 if (GET_MODE (x) != VOIDmode)
2168 for (i = 2; i < GET_RTX_LENGTH (CONST_DOUBLE); i++)
2170 unsigned HOST_WIDE_INT tem = XWINT (x, i);
2171 hash += tem;
2173 else
2174 hash += ((unsigned) CONST_DOUBLE_LOW (x)
2175 + (unsigned) CONST_DOUBLE_HIGH (x));
2176 return hash;
2178 /* Assume there is only one rtx object for any given label. */
2179 case LABEL_REF:
2180 hash
2181 += ((unsigned) LABEL_REF << 7) + (unsigned long) XEXP (x, 0);
2182 return hash;
2184 case SYMBOL_REF:
2185 hash
2186 += ((unsigned) SYMBOL_REF << 7) + (unsigned long) XSTR (x, 0);
2187 return hash;
2189 case MEM:
2190 if (MEM_VOLATILE_P (x))
2192 do_not_record = 1;
2193 return 0;
2195 if (! RTX_UNCHANGING_P (x) || FIXED_BASE_PLUS_P (XEXP (x, 0)))
2197 hash_arg_in_memory = 1;
2198 if (MEM_IN_STRUCT_P (x)) hash_arg_in_struct = 1;
2200 /* Now that we have already found this special case,
2201 might as well speed it up as much as possible. */
2202 hash += (unsigned) MEM;
2203 x = XEXP (x, 0);
2204 goto repeat;
2206 case PRE_DEC:
2207 case PRE_INC:
2208 case POST_DEC:
2209 case POST_INC:
2210 case PC:
2211 case CC0:
2212 case CALL:
2213 case UNSPEC_VOLATILE:
2214 do_not_record = 1;
2215 return 0;
2217 case ASM_OPERANDS:
2218 if (MEM_VOLATILE_P (x))
2220 do_not_record = 1;
2221 return 0;
2223 break;
2225 default:
2226 break;
2229 i = GET_RTX_LENGTH (code) - 1;
2230 hash += (unsigned) code + (unsigned) GET_MODE (x);
2231 fmt = GET_RTX_FORMAT (code);
2232 for (; i >= 0; i--)
2234 if (fmt[i] == 'e')
2236 rtx tem = XEXP (x, i);
2238 /* If we are about to do the last recursive call
2239 needed at this level, change it into iteration.
2240 This function is called enough to be worth it. */
2241 if (i == 0)
2243 x = tem;
2244 goto repeat;
2246 hash += canon_hash (tem, 0);
2248 else if (fmt[i] == 'E')
2249 for (j = 0; j < XVECLEN (x, i); j++)
2250 hash += canon_hash (XVECEXP (x, i, j), 0);
2251 else if (fmt[i] == 's')
2253 register unsigned char *p = (unsigned char *) XSTR (x, i);
2254 if (p)
2255 while (*p)
2256 hash += *p++;
2258 else if (fmt[i] == 'i')
2260 register unsigned tem = XINT (x, i);
2261 hash += tem;
2263 else if (fmt[i] == '0' || fmt[i] == 't')
2264 /* unused */;
2265 else
2266 abort ();
2268 return hash;
2271 /* Like canon_hash but with no side effects. */
2273 static unsigned
2274 safe_hash (x, mode)
2275 rtx x;
2276 enum machine_mode mode;
2278 int save_do_not_record = do_not_record;
2279 int save_hash_arg_in_memory = hash_arg_in_memory;
2280 int save_hash_arg_in_struct = hash_arg_in_struct;
2281 unsigned hash = canon_hash (x, mode);
2282 hash_arg_in_memory = save_hash_arg_in_memory;
2283 hash_arg_in_struct = save_hash_arg_in_struct;
2284 do_not_record = save_do_not_record;
2285 return hash;
2288 /* Return 1 iff X and Y would canonicalize into the same thing,
2289 without actually constructing the canonicalization of either one.
2290 If VALIDATE is nonzero,
2291 we assume X is an expression being processed from the rtl
2292 and Y was found in the hash table. We check register refs
2293 in Y for being marked as valid.
2295 If EQUAL_VALUES is nonzero, we allow a register to match a constant value
2296 that is known to be in the register. Ordinarily, we don't allow them
2297 to match, because letting them match would cause unpredictable results
2298 in all the places that search a hash table chain for an equivalent
2299 for a given value. A possible equivalent that has different structure
2300 has its hash code computed from different data. Whether the hash code
2301 is the same as that of the given value is pure luck. */
2303 static int
2304 exp_equiv_p (x, y, validate, equal_values)
2305 rtx x, y;
2306 int validate;
2307 int equal_values;
2309 register int i, j;
2310 register enum rtx_code code;
2311 register const char *fmt;
2313 /* Note: it is incorrect to assume an expression is equivalent to itself
2314 if VALIDATE is nonzero. */
2315 if (x == y && !validate)
2316 return 1;
2317 if (x == 0 || y == 0)
2318 return x == y;
2320 code = GET_CODE (x);
2321 if (code != GET_CODE (y))
2323 if (!equal_values)
2324 return 0;
2326 /* If X is a constant and Y is a register or vice versa, they may be
2327 equivalent. We only have to validate if Y is a register. */
2328 if (CONSTANT_P (x) && GET_CODE (y) == REG
2329 && REGNO_QTY_VALID_P (REGNO (y))
2330 && GET_MODE (y) == qty_mode[REG_QTY (REGNO (y))]
2331 && rtx_equal_p (x, qty_const[REG_QTY (REGNO (y))])
2332 && (! validate || REG_IN_TABLE (REGNO (y)) == REG_TICK (REGNO (y))))
2333 return 1;
2335 if (CONSTANT_P (y) && code == REG
2336 && REGNO_QTY_VALID_P (REGNO (x))
2337 && GET_MODE (x) == qty_mode[REG_QTY (REGNO (x))]
2338 && rtx_equal_p (y, qty_const[REG_QTY (REGNO (x))]))
2339 return 1;
2341 return 0;
2344 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
2345 if (GET_MODE (x) != GET_MODE (y))
2346 return 0;
2348 switch (code)
2350 case PC:
2351 case CC0:
2352 return x == y;
2354 case CONST_INT:
2355 return INTVAL (x) == INTVAL (y);
2357 case LABEL_REF:
2358 return XEXP (x, 0) == XEXP (y, 0);
2360 case SYMBOL_REF:
2361 return XSTR (x, 0) == XSTR (y, 0);
2363 case REG:
2365 int regno = REGNO (y);
2366 int endregno
2367 = regno + (regno >= FIRST_PSEUDO_REGISTER ? 1
2368 : HARD_REGNO_NREGS (regno, GET_MODE (y)));
2369 int i;
2371 /* If the quantities are not the same, the expressions are not
2372 equivalent. If there are and we are not to validate, they
2373 are equivalent. Otherwise, ensure all regs are up-to-date. */
2375 if (REG_QTY (REGNO (x)) != REG_QTY (regno))
2376 return 0;
2378 if (! validate)
2379 return 1;
2381 for (i = regno; i < endregno; i++)
2382 if (REG_IN_TABLE (i) != REG_TICK (i))
2383 return 0;
2385 return 1;
2388 /* For commutative operations, check both orders. */
2389 case PLUS:
2390 case MULT:
2391 case AND:
2392 case IOR:
2393 case XOR:
2394 case NE:
2395 case EQ:
2396 return ((exp_equiv_p (XEXP (x, 0), XEXP (y, 0), validate, equal_values)
2397 && exp_equiv_p (XEXP (x, 1), XEXP (y, 1),
2398 validate, equal_values))
2399 || (exp_equiv_p (XEXP (x, 0), XEXP (y, 1),
2400 validate, equal_values)
2401 && exp_equiv_p (XEXP (x, 1), XEXP (y, 0),
2402 validate, equal_values)));
2404 default:
2405 break;
2408 /* Compare the elements. If any pair of corresponding elements
2409 fail to match, return 0 for the whole things. */
2411 fmt = GET_RTX_FORMAT (code);
2412 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2414 switch (fmt[i])
2416 case 'e':
2417 if (! exp_equiv_p (XEXP (x, i), XEXP (y, i), validate, equal_values))
2418 return 0;
2419 break;
2421 case 'E':
2422 if (XVECLEN (x, i) != XVECLEN (y, i))
2423 return 0;
2424 for (j = 0; j < XVECLEN (x, i); j++)
2425 if (! exp_equiv_p (XVECEXP (x, i, j), XVECEXP (y, i, j),
2426 validate, equal_values))
2427 return 0;
2428 break;
2430 case 's':
2431 if (strcmp (XSTR (x, i), XSTR (y, i)))
2432 return 0;
2433 break;
2435 case 'i':
2436 if (XINT (x, i) != XINT (y, i))
2437 return 0;
2438 break;
2440 case 'w':
2441 if (XWINT (x, i) != XWINT (y, i))
2442 return 0;
2443 break;
2445 case '0':
2446 case 't':
2447 break;
2449 default:
2450 abort ();
2454 return 1;
2457 /* Return 1 iff any subexpression of X matches Y.
2458 Here we do not require that X or Y be valid (for registers referred to)
2459 for being in the hash table. */
2461 static int
2462 refers_to_p (x, y)
2463 rtx x, y;
2465 register int i;
2466 register enum rtx_code code;
2467 register const char *fmt;
2469 repeat:
2470 if (x == y)
2471 return 1;
2472 if (x == 0 || y == 0)
2473 return 0;
2475 code = GET_CODE (x);
2476 /* If X as a whole has the same code as Y, they may match.
2477 If so, return 1. */
2478 if (code == GET_CODE (y))
2480 if (exp_equiv_p (x, y, 0, 1))
2481 return 1;
2484 /* X does not match, so try its subexpressions. */
2486 fmt = GET_RTX_FORMAT (code);
2487 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2488 if (fmt[i] == 'e')
2490 if (i == 0)
2492 x = XEXP (x, 0);
2493 goto repeat;
2495 else
2496 if (refers_to_p (XEXP (x, i), y))
2497 return 1;
2499 else if (fmt[i] == 'E')
2501 int j;
2502 for (j = 0; j < XVECLEN (x, i); j++)
2503 if (refers_to_p (XVECEXP (x, i, j), y))
2504 return 1;
2507 return 0;
2510 /* Given ADDR and SIZE (a memory address, and the size of the memory reference),
2511 set PBASE, PSTART, and PEND which correspond to the base of the address,
2512 the starting offset, and ending offset respectively.
2514 ADDR is known to be a nonvarying address. */
2516 /* ??? Despite what the comments say, this function is in fact frequently
2517 passed varying addresses. This does not appear to cause any problems. */
2519 static void
2520 set_nonvarying_address_components (addr, size, pbase, pstart, pend)
2521 rtx addr;
2522 int size;
2523 rtx *pbase;
2524 HOST_WIDE_INT *pstart, *pend;
2526 rtx base;
2527 HOST_WIDE_INT start, end;
2529 base = addr;
2530 start = 0;
2531 end = 0;
2533 if (flag_pic && GET_CODE (base) == PLUS
2534 && XEXP (base, 0) == pic_offset_table_rtx)
2535 base = XEXP (base, 1);
2537 /* Registers with nonvarying addresses usually have constant equivalents;
2538 but the frame pointer register is also possible. */
2539 if (GET_CODE (base) == REG
2540 && qty_const != 0
2541 && REGNO_QTY_VALID_P (REGNO (base))
2542 && qty_mode[REG_QTY (REGNO (base))] == GET_MODE (base)
2543 && qty_const[REG_QTY (REGNO (base))] != 0)
2544 base = qty_const[REG_QTY (REGNO (base))];
2545 else if (GET_CODE (base) == PLUS
2546 && GET_CODE (XEXP (base, 1)) == CONST_INT
2547 && GET_CODE (XEXP (base, 0)) == REG
2548 && qty_const != 0
2549 && REGNO_QTY_VALID_P (REGNO (XEXP (base, 0)))
2550 && (qty_mode[REG_QTY (REGNO (XEXP (base, 0)))]
2551 == GET_MODE (XEXP (base, 0)))
2552 && qty_const[REG_QTY (REGNO (XEXP (base, 0)))])
2554 start = INTVAL (XEXP (base, 1));
2555 base = qty_const[REG_QTY (REGNO (XEXP (base, 0)))];
2557 /* This can happen as the result of virtual register instantiation,
2558 if the initial offset is too large to be a valid address. */
2559 else if (GET_CODE (base) == PLUS
2560 && GET_CODE (XEXP (base, 0)) == REG
2561 && GET_CODE (XEXP (base, 1)) == REG
2562 && qty_const != 0
2563 && REGNO_QTY_VALID_P (REGNO (XEXP (base, 0)))
2564 && (qty_mode[REG_QTY (REGNO (XEXP (base, 0)))]
2565 == GET_MODE (XEXP (base, 0)))
2566 && qty_const[REG_QTY (REGNO (XEXP (base, 0)))]
2567 && REGNO_QTY_VALID_P (REGNO (XEXP (base, 1)))
2568 && (qty_mode[REG_QTY (REGNO (XEXP (base, 1)))]
2569 == GET_MODE (XEXP (base, 1)))
2570 && qty_const[REG_QTY (REGNO (XEXP (base, 1)))])
2572 rtx tem = qty_const[REG_QTY (REGNO (XEXP (base, 1)))];
2573 base = qty_const[REG_QTY (REGNO (XEXP (base, 0)))];
2575 /* One of the two values must be a constant. */
2576 if (GET_CODE (base) != CONST_INT)
2578 if (GET_CODE (tem) != CONST_INT)
2579 abort ();
2580 start = INTVAL (tem);
2582 else
2584 start = INTVAL (base);
2585 base = tem;
2589 /* Handle everything that we can find inside an address that has been
2590 viewed as constant. */
2592 while (1)
2594 /* If no part of this switch does a "continue", the code outside
2595 will exit this loop. */
2597 switch (GET_CODE (base))
2599 case LO_SUM:
2600 /* By definition, operand1 of a LO_SUM is the associated constant
2601 address. Use the associated constant address as the base
2602 instead. */
2603 base = XEXP (base, 1);
2604 continue;
2606 case CONST:
2607 /* Strip off CONST. */
2608 base = XEXP (base, 0);
2609 continue;
2611 case PLUS:
2612 if (GET_CODE (XEXP (base, 1)) == CONST_INT)
2614 start += INTVAL (XEXP (base, 1));
2615 base = XEXP (base, 0);
2616 continue;
2618 break;
2620 case AND:
2621 /* Handle the case of an AND which is the negative of a power of
2622 two. This is used to represent unaligned memory operations. */
2623 if (GET_CODE (XEXP (base, 1)) == CONST_INT
2624 && exact_log2 (- INTVAL (XEXP (base, 1))) > 0)
2626 set_nonvarying_address_components (XEXP (base, 0), size,
2627 pbase, pstart, pend);
2629 /* Assume the worst misalignment. START is affected, but not
2630 END, so compensate but adjusting SIZE. Don't lose any
2631 constant we already had. */
2633 size = *pend - *pstart - INTVAL (XEXP (base, 1)) - 1;
2634 start += *pstart + INTVAL (XEXP (base, 1)) + 1;
2635 end += *pend;
2636 base = *pbase;
2638 break;
2640 default:
2641 break;
2644 break;
2647 if (GET_CODE (base) == CONST_INT)
2649 start += INTVAL (base);
2650 base = const0_rtx;
2653 end = start + size;
2655 /* Set the return values. */
2656 *pbase = base;
2657 *pstart = start;
2658 *pend = end;
2661 /* Return 1 if X has a value that can vary even between two
2662 executions of the program. 0 means X can be compared reliably
2663 against certain constants or near-constants. */
2665 static int
2666 cse_rtx_varies_p (x)
2667 register rtx x;
2669 /* We need not check for X and the equivalence class being of the same
2670 mode because if X is equivalent to a constant in some mode, it
2671 doesn't vary in any mode. */
2673 if (GET_CODE (x) == REG
2674 && REGNO_QTY_VALID_P (REGNO (x))
2675 && GET_MODE (x) == qty_mode[REG_QTY (REGNO (x))]
2676 && qty_const[REG_QTY (REGNO (x))] != 0)
2677 return 0;
2679 if (GET_CODE (x) == PLUS
2680 && GET_CODE (XEXP (x, 1)) == CONST_INT
2681 && GET_CODE (XEXP (x, 0)) == REG
2682 && REGNO_QTY_VALID_P (REGNO (XEXP (x, 0)))
2683 && (GET_MODE (XEXP (x, 0))
2684 == qty_mode[REG_QTY (REGNO (XEXP (x, 0)))])
2685 && qty_const[REG_QTY (REGNO (XEXP (x, 0)))])
2686 return 0;
2688 /* This can happen as the result of virtual register instantiation, if
2689 the initial constant is too large to be a valid address. This gives
2690 us a three instruction sequence, load large offset into a register,
2691 load fp minus a constant into a register, then a MEM which is the
2692 sum of the two `constant' registers. */
2693 if (GET_CODE (x) == PLUS
2694 && GET_CODE (XEXP (x, 0)) == REG
2695 && GET_CODE (XEXP (x, 1)) == REG
2696 && REGNO_QTY_VALID_P (REGNO (XEXP (x, 0)))
2697 && (GET_MODE (XEXP (x, 0))
2698 == qty_mode[REG_QTY (REGNO (XEXP (x, 0)))])
2699 && qty_const[REG_QTY (REGNO (XEXP (x, 0)))]
2700 && REGNO_QTY_VALID_P (REGNO (XEXP (x, 1)))
2701 && (GET_MODE (XEXP (x, 1))
2702 == qty_mode[REG_QTY (REGNO (XEXP (x, 1)))])
2703 && qty_const[REG_QTY (REGNO (XEXP (x, 1)))])
2704 return 0;
2706 return rtx_varies_p (x);
2709 /* Canonicalize an expression:
2710 replace each register reference inside it
2711 with the "oldest" equivalent register.
2713 If INSN is non-zero and we are replacing a pseudo with a hard register
2714 or vice versa, validate_change is used to ensure that INSN remains valid
2715 after we make our substitution. The calls are made with IN_GROUP non-zero
2716 so apply_change_group must be called upon the outermost return from this
2717 function (unless INSN is zero). The result of apply_change_group can
2718 generally be discarded since the changes we are making are optional. */
2720 static rtx
2721 canon_reg (x, insn)
2722 rtx x;
2723 rtx insn;
2725 register int i;
2726 register enum rtx_code code;
2727 register const char *fmt;
2729 if (x == 0)
2730 return x;
2732 code = GET_CODE (x);
2733 switch (code)
2735 case PC:
2736 case CC0:
2737 case CONST:
2738 case CONST_INT:
2739 case CONST_DOUBLE:
2740 case SYMBOL_REF:
2741 case LABEL_REF:
2742 case ADDR_VEC:
2743 case ADDR_DIFF_VEC:
2744 return x;
2746 case REG:
2748 register int first;
2750 /* Never replace a hard reg, because hard regs can appear
2751 in more than one machine mode, and we must preserve the mode
2752 of each occurrence. Also, some hard regs appear in
2753 MEMs that are shared and mustn't be altered. Don't try to
2754 replace any reg that maps to a reg of class NO_REGS. */
2755 if (REGNO (x) < FIRST_PSEUDO_REGISTER
2756 || ! REGNO_QTY_VALID_P (REGNO (x)))
2757 return x;
2759 first = qty_first_reg[REG_QTY (REGNO (x))];
2760 return (first >= FIRST_PSEUDO_REGISTER ? regno_reg_rtx[first]
2761 : REGNO_REG_CLASS (first) == NO_REGS ? x
2762 : gen_rtx_REG (qty_mode[REG_QTY (REGNO (x))], first));
2765 default:
2766 break;
2769 fmt = GET_RTX_FORMAT (code);
2770 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2772 register int j;
2774 if (fmt[i] == 'e')
2776 rtx new = canon_reg (XEXP (x, i), insn);
2777 int insn_code;
2779 /* If replacing pseudo with hard reg or vice versa, ensure the
2780 insn remains valid. Likewise if the insn has MATCH_DUPs. */
2781 if (insn != 0 && new != 0
2782 && GET_CODE (new) == REG && GET_CODE (XEXP (x, i)) == REG
2783 && (((REGNO (new) < FIRST_PSEUDO_REGISTER)
2784 != (REGNO (XEXP (x, i)) < FIRST_PSEUDO_REGISTER))
2785 || (insn_code = recog_memoized (insn)) < 0
2786 || insn_data[insn_code].n_dups > 0))
2787 validate_change (insn, &XEXP (x, i), new, 1);
2788 else
2789 XEXP (x, i) = new;
2791 else if (fmt[i] == 'E')
2792 for (j = 0; j < XVECLEN (x, i); j++)
2793 XVECEXP (x, i, j) = canon_reg (XVECEXP (x, i, j), insn);
2796 return x;
2799 /* LOC is a location within INSN that is an operand address (the contents of
2800 a MEM). Find the best equivalent address to use that is valid for this
2801 insn.
2803 On most CISC machines, complicated address modes are costly, and rtx_cost
2804 is a good approximation for that cost. However, most RISC machines have
2805 only a few (usually only one) memory reference formats. If an address is
2806 valid at all, it is often just as cheap as any other address. Hence, for
2807 RISC machines, we use the configuration macro `ADDRESS_COST' to compare the
2808 costs of various addresses. For two addresses of equal cost, choose the one
2809 with the highest `rtx_cost' value as that has the potential of eliminating
2810 the most insns. For equal costs, we choose the first in the equivalence
2811 class. Note that we ignore the fact that pseudo registers are cheaper
2812 than hard registers here because we would also prefer the pseudo registers.
2815 static void
2816 find_best_addr (insn, loc)
2817 rtx insn;
2818 rtx *loc;
2820 struct table_elt *elt;
2821 rtx addr = *loc;
2822 #ifdef ADDRESS_COST
2823 struct table_elt *p;
2824 int found_better = 1;
2825 #endif
2826 int save_do_not_record = do_not_record;
2827 int save_hash_arg_in_memory = hash_arg_in_memory;
2828 int save_hash_arg_in_struct = hash_arg_in_struct;
2829 int addr_volatile;
2830 int regno;
2831 unsigned hash;
2833 /* Do not try to replace constant addresses or addresses of local and
2834 argument slots. These MEM expressions are made only once and inserted
2835 in many instructions, as well as being used to control symbol table
2836 output. It is not safe to clobber them.
2838 There are some uncommon cases where the address is already in a register
2839 for some reason, but we cannot take advantage of that because we have
2840 no easy way to unshare the MEM. In addition, looking up all stack
2841 addresses is costly. */
2842 if ((GET_CODE (addr) == PLUS
2843 && GET_CODE (XEXP (addr, 0)) == REG
2844 && GET_CODE (XEXP (addr, 1)) == CONST_INT
2845 && (regno = REGNO (XEXP (addr, 0)),
2846 regno == FRAME_POINTER_REGNUM || regno == HARD_FRAME_POINTER_REGNUM
2847 || regno == ARG_POINTER_REGNUM))
2848 || (GET_CODE (addr) == REG
2849 && (regno = REGNO (addr), regno == FRAME_POINTER_REGNUM
2850 || regno == HARD_FRAME_POINTER_REGNUM
2851 || regno == ARG_POINTER_REGNUM))
2852 || GET_CODE (addr) == ADDRESSOF
2853 || CONSTANT_ADDRESS_P (addr))
2854 return;
2856 /* If this address is not simply a register, try to fold it. This will
2857 sometimes simplify the expression. Many simplifications
2858 will not be valid, but some, usually applying the associative rule, will
2859 be valid and produce better code. */
2860 if (GET_CODE (addr) != REG)
2862 rtx folded = fold_rtx (copy_rtx (addr), NULL_RTX);
2864 if (1
2865 #ifdef ADDRESS_COST
2866 && (CSE_ADDRESS_COST (folded) < CSE_ADDRESS_COST (addr)
2867 || (CSE_ADDRESS_COST (folded) == CSE_ADDRESS_COST (addr)
2868 && rtx_cost (folded, MEM) > rtx_cost (addr, MEM)))
2869 #else
2870 && rtx_cost (folded, MEM) < rtx_cost (addr, MEM)
2871 #endif
2872 && validate_change (insn, loc, folded, 0))
2873 addr = folded;
2876 /* If this address is not in the hash table, we can't look for equivalences
2877 of the whole address. Also, ignore if volatile. */
2879 do_not_record = 0;
2880 hash = HASH (addr, Pmode);
2881 addr_volatile = do_not_record;
2882 do_not_record = save_do_not_record;
2883 hash_arg_in_memory = save_hash_arg_in_memory;
2884 hash_arg_in_struct = save_hash_arg_in_struct;
2886 if (addr_volatile)
2887 return;
2889 elt = lookup (addr, hash, Pmode);
2891 #ifndef ADDRESS_COST
2892 if (elt)
2894 int our_cost = elt->cost;
2896 /* Find the lowest cost below ours that works. */
2897 for (elt = elt->first_same_value; elt; elt = elt->next_same_value)
2898 if (elt->cost < our_cost
2899 && (GET_CODE (elt->exp) == REG
2900 || exp_equiv_p (elt->exp, elt->exp, 1, 0))
2901 && validate_change (insn, loc,
2902 canon_reg (copy_rtx (elt->exp), NULL_RTX), 0))
2903 return;
2905 #else
2907 if (elt)
2909 /* We need to find the best (under the criteria documented above) entry
2910 in the class that is valid. We use the `flag' field to indicate
2911 choices that were invalid and iterate until we can't find a better
2912 one that hasn't already been tried. */
2914 for (p = elt->first_same_value; p; p = p->next_same_value)
2915 p->flag = 0;
2917 while (found_better)
2919 int best_addr_cost = CSE_ADDRESS_COST (*loc);
2920 int best_rtx_cost = (elt->cost + 1) >> 1;
2921 struct table_elt *best_elt = elt;
2923 found_better = 0;
2924 for (p = elt->first_same_value; p; p = p->next_same_value)
2925 if (! p->flag)
2927 if ((GET_CODE (p->exp) == REG
2928 || exp_equiv_p (p->exp, p->exp, 1, 0))
2929 && (CSE_ADDRESS_COST (p->exp) < best_addr_cost
2930 || (CSE_ADDRESS_COST (p->exp) == best_addr_cost
2931 && (p->cost + 1) >> 1 > best_rtx_cost)))
2933 found_better = 1;
2934 best_addr_cost = CSE_ADDRESS_COST (p->exp);
2935 best_rtx_cost = (p->cost + 1) >> 1;
2936 best_elt = p;
2940 if (found_better)
2942 if (validate_change (insn, loc,
2943 canon_reg (copy_rtx (best_elt->exp),
2944 NULL_RTX), 0))
2945 return;
2946 else
2947 best_elt->flag = 1;
2952 /* If the address is a binary operation with the first operand a register
2953 and the second a constant, do the same as above, but looking for
2954 equivalences of the register. Then try to simplify before checking for
2955 the best address to use. This catches a few cases: First is when we
2956 have REG+const and the register is another REG+const. We can often merge
2957 the constants and eliminate one insn and one register. It may also be
2958 that a machine has a cheap REG+REG+const. Finally, this improves the
2959 code on the Alpha for unaligned byte stores. */
2961 if (flag_expensive_optimizations
2962 && (GET_RTX_CLASS (GET_CODE (*loc)) == '2'
2963 || GET_RTX_CLASS (GET_CODE (*loc)) == 'c')
2964 && GET_CODE (XEXP (*loc, 0)) == REG
2965 && GET_CODE (XEXP (*loc, 1)) == CONST_INT)
2967 rtx c = XEXP (*loc, 1);
2969 do_not_record = 0;
2970 hash = HASH (XEXP (*loc, 0), Pmode);
2971 do_not_record = save_do_not_record;
2972 hash_arg_in_memory = save_hash_arg_in_memory;
2973 hash_arg_in_struct = save_hash_arg_in_struct;
2975 elt = lookup (XEXP (*loc, 0), hash, Pmode);
2976 if (elt == 0)
2977 return;
2979 /* We need to find the best (under the criteria documented above) entry
2980 in the class that is valid. We use the `flag' field to indicate
2981 choices that were invalid and iterate until we can't find a better
2982 one that hasn't already been tried. */
2984 for (p = elt->first_same_value; p; p = p->next_same_value)
2985 p->flag = 0;
2987 while (found_better)
2989 int best_addr_cost = CSE_ADDRESS_COST (*loc);
2990 int best_rtx_cost = (COST (*loc) + 1) >> 1;
2991 struct table_elt *best_elt = elt;
2992 rtx best_rtx = *loc;
2993 int count;
2995 /* This is at worst case an O(n^2) algorithm, so limit our search
2996 to the first 32 elements on the list. This avoids trouble
2997 compiling code with very long basic blocks that can easily
2998 call cse_gen_binary so many times that we run out of memory. */
3000 found_better = 0;
3001 for (p = elt->first_same_value, count = 0;
3002 p && count < 32;
3003 p = p->next_same_value, count++)
3004 if (! p->flag
3005 && (GET_CODE (p->exp) == REG
3006 || exp_equiv_p (p->exp, p->exp, 1, 0)))
3008 rtx new = cse_gen_binary (GET_CODE (*loc), Pmode, p->exp, c);
3010 if ((CSE_ADDRESS_COST (new) < best_addr_cost
3011 || (CSE_ADDRESS_COST (new) == best_addr_cost
3012 && (COST (new) + 1) >> 1 > best_rtx_cost)))
3014 found_better = 1;
3015 best_addr_cost = CSE_ADDRESS_COST (new);
3016 best_rtx_cost = (COST (new) + 1) >> 1;
3017 best_elt = p;
3018 best_rtx = new;
3022 if (found_better)
3024 if (validate_change (insn, loc,
3025 canon_reg (copy_rtx (best_rtx),
3026 NULL_RTX), 0))
3027 return;
3028 else
3029 best_elt->flag = 1;
3033 #endif
3036 /* Given an operation (CODE, *PARG1, *PARG2), where code is a comparison
3037 operation (EQ, NE, GT, etc.), follow it back through the hash table and
3038 what values are being compared.
3040 *PARG1 and *PARG2 are updated to contain the rtx representing the values
3041 actually being compared. For example, if *PARG1 was (cc0) and *PARG2
3042 was (const_int 0), *PARG1 and *PARG2 will be set to the objects that were
3043 compared to produce cc0.
3045 The return value is the comparison operator and is either the code of
3046 A or the code corresponding to the inverse of the comparison. */
3048 static enum rtx_code
3049 find_comparison_args (code, parg1, parg2, pmode1, pmode2)
3050 enum rtx_code code;
3051 rtx *parg1, *parg2;
3052 enum machine_mode *pmode1, *pmode2;
3054 rtx arg1, arg2;
3056 arg1 = *parg1, arg2 = *parg2;
3058 /* If ARG2 is const0_rtx, see what ARG1 is equivalent to. */
3060 while (arg2 == CONST0_RTX (GET_MODE (arg1)))
3062 /* Set non-zero when we find something of interest. */
3063 rtx x = 0;
3064 int reverse_code = 0;
3065 struct table_elt *p = 0;
3067 /* If arg1 is a COMPARE, extract the comparison arguments from it.
3068 On machines with CC0, this is the only case that can occur, since
3069 fold_rtx will return the COMPARE or item being compared with zero
3070 when given CC0. */
3072 if (GET_CODE (arg1) == COMPARE && arg2 == const0_rtx)
3073 x = arg1;
3075 /* If ARG1 is a comparison operator and CODE is testing for
3076 STORE_FLAG_VALUE, get the inner arguments. */
3078 else if (GET_RTX_CLASS (GET_CODE (arg1)) == '<')
3080 if (code == NE
3081 || (GET_MODE_CLASS (GET_MODE (arg1)) == MODE_INT
3082 && code == LT && STORE_FLAG_VALUE == -1)
3083 #ifdef FLOAT_STORE_FLAG_VALUE
3084 || (GET_MODE_CLASS (GET_MODE (arg1)) == MODE_FLOAT
3085 && FLOAT_STORE_FLAG_VALUE < 0)
3086 #endif
3088 x = arg1;
3089 else if (code == EQ
3090 || (GET_MODE_CLASS (GET_MODE (arg1)) == MODE_INT
3091 && code == GE && STORE_FLAG_VALUE == -1)
3092 #ifdef FLOAT_STORE_FLAG_VALUE
3093 || (GET_MODE_CLASS (GET_MODE (arg1)) == MODE_FLOAT
3094 && FLOAT_STORE_FLAG_VALUE < 0)
3095 #endif
3097 x = arg1, reverse_code = 1;
3100 /* ??? We could also check for
3102 (ne (and (eq (...) (const_int 1))) (const_int 0))
3104 and related forms, but let's wait until we see them occurring. */
3106 if (x == 0)
3107 /* Look up ARG1 in the hash table and see if it has an equivalence
3108 that lets us see what is being compared. */
3109 p = lookup (arg1, safe_hash (arg1, GET_MODE (arg1)) % NBUCKETS,
3110 GET_MODE (arg1));
3111 if (p) p = p->first_same_value;
3113 for (; p; p = p->next_same_value)
3115 enum machine_mode inner_mode = GET_MODE (p->exp);
3117 /* If the entry isn't valid, skip it. */
3118 if (! exp_equiv_p (p->exp, p->exp, 1, 0))
3119 continue;
3121 if (GET_CODE (p->exp) == COMPARE
3122 /* Another possibility is that this machine has a compare insn
3123 that includes the comparison code. In that case, ARG1 would
3124 be equivalent to a comparison operation that would set ARG1 to
3125 either STORE_FLAG_VALUE or zero. If this is an NE operation,
3126 ORIG_CODE is the actual comparison being done; if it is an EQ,
3127 we must reverse ORIG_CODE. On machine with a negative value
3128 for STORE_FLAG_VALUE, also look at LT and GE operations. */
3129 || ((code == NE
3130 || (code == LT
3131 && GET_MODE_CLASS (inner_mode) == MODE_INT
3132 && (GET_MODE_BITSIZE (inner_mode)
3133 <= HOST_BITS_PER_WIDE_INT)
3134 && (STORE_FLAG_VALUE
3135 & ((HOST_WIDE_INT) 1
3136 << (GET_MODE_BITSIZE (inner_mode) - 1))))
3137 #ifdef FLOAT_STORE_FLAG_VALUE
3138 || (code == LT
3139 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
3140 && FLOAT_STORE_FLAG_VALUE < 0)
3141 #endif
3143 && GET_RTX_CLASS (GET_CODE (p->exp)) == '<'))
3145 x = p->exp;
3146 break;
3148 else if ((code == EQ
3149 || (code == GE
3150 && GET_MODE_CLASS (inner_mode) == MODE_INT
3151 && (GET_MODE_BITSIZE (inner_mode)
3152 <= HOST_BITS_PER_WIDE_INT)
3153 && (STORE_FLAG_VALUE
3154 & ((HOST_WIDE_INT) 1
3155 << (GET_MODE_BITSIZE (inner_mode) - 1))))
3156 #ifdef FLOAT_STORE_FLAG_VALUE
3157 || (code == GE
3158 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
3159 && FLOAT_STORE_FLAG_VALUE < 0)
3160 #endif
3162 && GET_RTX_CLASS (GET_CODE (p->exp)) == '<')
3164 reverse_code = 1;
3165 x = p->exp;
3166 break;
3169 /* If this is fp + constant, the equivalent is a better operand since
3170 it may let us predict the value of the comparison. */
3171 else if (NONZERO_BASE_PLUS_P (p->exp))
3173 arg1 = p->exp;
3174 continue;
3178 /* If we didn't find a useful equivalence for ARG1, we are done.
3179 Otherwise, set up for the next iteration. */
3180 if (x == 0)
3181 break;
3183 arg1 = XEXP (x, 0), arg2 = XEXP (x, 1);
3184 if (GET_RTX_CLASS (GET_CODE (x)) == '<')
3185 code = GET_CODE (x);
3187 if (reverse_code)
3188 code = reverse_condition (code);
3191 /* Return our results. Return the modes from before fold_rtx
3192 because fold_rtx might produce const_int, and then it's too late. */
3193 *pmode1 = GET_MODE (arg1), *pmode2 = GET_MODE (arg2);
3194 *parg1 = fold_rtx (arg1, 0), *parg2 = fold_rtx (arg2, 0);
3196 return code;
3199 /* Try to simplify a unary operation CODE whose output mode is to be
3200 MODE with input operand OP whose mode was originally OP_MODE.
3201 Return zero if no simplification can be made. */
3204 simplify_unary_operation (code, mode, op, op_mode)
3205 enum rtx_code code;
3206 enum machine_mode mode;
3207 rtx op;
3208 enum machine_mode op_mode;
3210 register int width = GET_MODE_BITSIZE (mode);
3212 /* The order of these tests is critical so that, for example, we don't
3213 check the wrong mode (input vs. output) for a conversion operation,
3214 such as FIX. At some point, this should be simplified. */
3216 #if !defined(REAL_IS_NOT_DOUBLE) || defined(REAL_ARITHMETIC)
3218 if (code == FLOAT && GET_MODE (op) == VOIDmode
3219 && (GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT))
3221 HOST_WIDE_INT hv, lv;
3222 REAL_VALUE_TYPE d;
3224 if (GET_CODE (op) == CONST_INT)
3225 lv = INTVAL (op), hv = INTVAL (op) < 0 ? -1 : 0;
3226 else
3227 lv = CONST_DOUBLE_LOW (op), hv = CONST_DOUBLE_HIGH (op);
3229 #ifdef REAL_ARITHMETIC
3230 REAL_VALUE_FROM_INT (d, lv, hv, mode);
3231 #else
3232 if (hv < 0)
3234 d = (double) (~ hv);
3235 d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
3236 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
3237 d += (double) (unsigned HOST_WIDE_INT) (~ lv);
3238 d = (- d - 1.0);
3240 else
3242 d = (double) hv;
3243 d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
3244 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
3245 d += (double) (unsigned HOST_WIDE_INT) lv;
3247 #endif /* REAL_ARITHMETIC */
3248 d = real_value_truncate (mode, d);
3249 return CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
3251 else if (code == UNSIGNED_FLOAT && GET_MODE (op) == VOIDmode
3252 && (GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT))
3254 HOST_WIDE_INT hv, lv;
3255 REAL_VALUE_TYPE d;
3257 if (GET_CODE (op) == CONST_INT)
3258 lv = INTVAL (op), hv = INTVAL (op) < 0 ? -1 : 0;
3259 else
3260 lv = CONST_DOUBLE_LOW (op), hv = CONST_DOUBLE_HIGH (op);
3262 if (op_mode == VOIDmode)
3264 /* We don't know how to interpret negative-looking numbers in
3265 this case, so don't try to fold those. */
3266 if (hv < 0)
3267 return 0;
3269 else if (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT * 2)
3271 else
3272 hv = 0, lv &= GET_MODE_MASK (op_mode);
3274 #ifdef REAL_ARITHMETIC
3275 REAL_VALUE_FROM_UNSIGNED_INT (d, lv, hv, mode);
3276 #else
3278 d = (double) (unsigned HOST_WIDE_INT) hv;
3279 d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
3280 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
3281 d += (double) (unsigned HOST_WIDE_INT) lv;
3282 #endif /* REAL_ARITHMETIC */
3283 d = real_value_truncate (mode, d);
3284 return CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
3286 #endif
3288 if (GET_CODE (op) == CONST_INT
3289 && width <= HOST_BITS_PER_WIDE_INT && width > 0)
3291 register HOST_WIDE_INT arg0 = INTVAL (op);
3292 register HOST_WIDE_INT val;
3294 switch (code)
3296 case NOT:
3297 val = ~ arg0;
3298 break;
3300 case NEG:
3301 val = - arg0;
3302 break;
3304 case ABS:
3305 val = (arg0 >= 0 ? arg0 : - arg0);
3306 break;
3308 case FFS:
3309 /* Don't use ffs here. Instead, get low order bit and then its
3310 number. If arg0 is zero, this will return 0, as desired. */
3311 arg0 &= GET_MODE_MASK (mode);
3312 val = exact_log2 (arg0 & (- arg0)) + 1;
3313 break;
3315 case TRUNCATE:
3316 val = arg0;
3317 break;
3319 case ZERO_EXTEND:
3320 if (op_mode == VOIDmode)
3321 op_mode = mode;
3322 if (GET_MODE_BITSIZE (op_mode) == HOST_BITS_PER_WIDE_INT)
3324 /* If we were really extending the mode,
3325 we would have to distinguish between zero-extension
3326 and sign-extension. */
3327 if (width != GET_MODE_BITSIZE (op_mode))
3328 abort ();
3329 val = arg0;
3331 else if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT)
3332 val = arg0 & ~((HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (op_mode));
3333 else
3334 return 0;
3335 break;
3337 case SIGN_EXTEND:
3338 if (op_mode == VOIDmode)
3339 op_mode = mode;
3340 if (GET_MODE_BITSIZE (op_mode) == HOST_BITS_PER_WIDE_INT)
3342 /* If we were really extending the mode,
3343 we would have to distinguish between zero-extension
3344 and sign-extension. */
3345 if (width != GET_MODE_BITSIZE (op_mode))
3346 abort ();
3347 val = arg0;
3349 else if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT)
3352 = arg0 & ~((HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (op_mode));
3353 if (val
3354 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (op_mode) - 1)))
3355 val -= (HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (op_mode);
3357 else
3358 return 0;
3359 break;
3361 case SQRT:
3362 return 0;
3364 default:
3365 abort ();
3368 val = trunc_int_for_mode (val, mode);
3370 return GEN_INT (val);
3373 /* We can do some operations on integer CONST_DOUBLEs. Also allow
3374 for a DImode operation on a CONST_INT. */
3375 else if (GET_MODE (op) == VOIDmode && width <= HOST_BITS_PER_INT * 2
3376 && (GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT))
3378 HOST_WIDE_INT l1, h1, lv, hv;
3380 if (GET_CODE (op) == CONST_DOUBLE)
3381 l1 = CONST_DOUBLE_LOW (op), h1 = CONST_DOUBLE_HIGH (op);
3382 else
3383 l1 = INTVAL (op), h1 = l1 < 0 ? -1 : 0;
3385 switch (code)
3387 case NOT:
3388 lv = ~ l1;
3389 hv = ~ h1;
3390 break;
3392 case NEG:
3393 neg_double (l1, h1, &lv, &hv);
3394 break;
3396 case ABS:
3397 if (h1 < 0)
3398 neg_double (l1, h1, &lv, &hv);
3399 else
3400 lv = l1, hv = h1;
3401 break;
3403 case FFS:
3404 hv = 0;
3405 if (l1 == 0)
3406 lv = HOST_BITS_PER_WIDE_INT + exact_log2 (h1 & (-h1)) + 1;
3407 else
3408 lv = exact_log2 (l1 & (-l1)) + 1;
3409 break;
3411 case TRUNCATE:
3412 /* This is just a change-of-mode, so do nothing. */
3413 lv = l1, hv = h1;
3414 break;
3416 case ZERO_EXTEND:
3417 if (op_mode == VOIDmode
3418 || GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT)
3419 return 0;
3421 hv = 0;
3422 lv = l1 & GET_MODE_MASK (op_mode);
3423 break;
3425 case SIGN_EXTEND:
3426 if (op_mode == VOIDmode
3427 || GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT)
3428 return 0;
3429 else
3431 lv = l1 & GET_MODE_MASK (op_mode);
3432 if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
3433 && (lv & ((HOST_WIDE_INT) 1
3434 << (GET_MODE_BITSIZE (op_mode) - 1))) != 0)
3435 lv -= (HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (op_mode);
3437 hv = (lv < 0) ? ~ (HOST_WIDE_INT) 0 : 0;
3439 break;
3441 case SQRT:
3442 return 0;
3444 default:
3445 return 0;
3448 return immed_double_const (lv, hv, mode);
3451 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
3452 else if (GET_CODE (op) == CONST_DOUBLE
3453 && GET_MODE_CLASS (mode) == MODE_FLOAT)
3455 REAL_VALUE_TYPE d;
3456 jmp_buf handler;
3457 rtx x;
3459 if (setjmp (handler))
3460 /* There used to be a warning here, but that is inadvisable.
3461 People may want to cause traps, and the natural way
3462 to do it should not get a warning. */
3463 return 0;
3465 set_float_handler (handler);
3467 REAL_VALUE_FROM_CONST_DOUBLE (d, op);
3469 switch (code)
3471 case NEG:
3472 d = REAL_VALUE_NEGATE (d);
3473 break;
3475 case ABS:
3476 if (REAL_VALUE_NEGATIVE (d))
3477 d = REAL_VALUE_NEGATE (d);
3478 break;
3480 case FLOAT_TRUNCATE:
3481 d = real_value_truncate (mode, d);
3482 break;
3484 case FLOAT_EXTEND:
3485 /* All this does is change the mode. */
3486 break;
3488 case FIX:
3489 d = REAL_VALUE_RNDZINT (d);
3490 break;
3492 case UNSIGNED_FIX:
3493 d = REAL_VALUE_UNSIGNED_RNDZINT (d);
3494 break;
3496 case SQRT:
3497 return 0;
3499 default:
3500 abort ();
3503 x = CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
3504 set_float_handler (NULL_PTR);
3505 return x;
3508 else if (GET_CODE (op) == CONST_DOUBLE
3509 && GET_MODE_CLASS (GET_MODE (op)) == MODE_FLOAT
3510 && GET_MODE_CLASS (mode) == MODE_INT
3511 && width <= HOST_BITS_PER_WIDE_INT && width > 0)
3513 REAL_VALUE_TYPE d;
3514 jmp_buf handler;
3515 HOST_WIDE_INT val;
3517 if (setjmp (handler))
3518 return 0;
3520 set_float_handler (handler);
3522 REAL_VALUE_FROM_CONST_DOUBLE (d, op);
3524 switch (code)
3526 case FIX:
3527 val = REAL_VALUE_FIX (d);
3528 break;
3530 case UNSIGNED_FIX:
3531 val = REAL_VALUE_UNSIGNED_FIX (d);
3532 break;
3534 default:
3535 abort ();
3538 set_float_handler (NULL_PTR);
3540 val = trunc_int_for_mode (val, mode);
3542 return GEN_INT (val);
3544 #endif
3545 /* This was formerly used only for non-IEEE float.
3546 eggert@twinsun.com says it is safe for IEEE also. */
3547 else
3549 /* There are some simplifications we can do even if the operands
3550 aren't constant. */
3551 switch (code)
3553 case NEG:
3554 case NOT:
3555 /* (not (not X)) == X, similarly for NEG. */
3556 if (GET_CODE (op) == code)
3557 return XEXP (op, 0);
3558 break;
3560 case SIGN_EXTEND:
3561 /* (sign_extend (truncate (minus (label_ref L1) (label_ref L2))))
3562 becomes just the MINUS if its mode is MODE. This allows
3563 folding switch statements on machines using casesi (such as
3564 the Vax). */
3565 if (GET_CODE (op) == TRUNCATE
3566 && GET_MODE (XEXP (op, 0)) == mode
3567 && GET_CODE (XEXP (op, 0)) == MINUS
3568 && GET_CODE (XEXP (XEXP (op, 0), 0)) == LABEL_REF
3569 && GET_CODE (XEXP (XEXP (op, 0), 1)) == LABEL_REF)
3570 return XEXP (op, 0);
3572 #ifdef POINTERS_EXTEND_UNSIGNED
3573 if (! POINTERS_EXTEND_UNSIGNED
3574 && mode == Pmode && GET_MODE (op) == ptr_mode
3575 && CONSTANT_P (op))
3576 return convert_memory_address (Pmode, op);
3577 #endif
3578 break;
3580 #ifdef POINTERS_EXTEND_UNSIGNED
3581 case ZERO_EXTEND:
3582 if (POINTERS_EXTEND_UNSIGNED
3583 && mode == Pmode && GET_MODE (op) == ptr_mode
3584 && CONSTANT_P (op))
3585 return convert_memory_address (Pmode, op);
3586 break;
3587 #endif
3589 default:
3590 break;
3593 return 0;
3597 /* Simplify a binary operation CODE with result mode MODE, operating on OP0
3598 and OP1. Return 0 if no simplification is possible.
3600 Don't use this for relational operations such as EQ or LT.
3601 Use simplify_relational_operation instead. */
3604 simplify_binary_operation (code, mode, op0, op1)
3605 enum rtx_code code;
3606 enum machine_mode mode;
3607 rtx op0, op1;
3609 register HOST_WIDE_INT arg0, arg1, arg0s, arg1s;
3610 HOST_WIDE_INT val;
3611 int width = GET_MODE_BITSIZE (mode);
3612 rtx tem;
3614 /* Relational operations don't work here. We must know the mode
3615 of the operands in order to do the comparison correctly.
3616 Assuming a full word can give incorrect results.
3617 Consider comparing 128 with -128 in QImode. */
3619 if (GET_RTX_CLASS (code) == '<')
3620 abort ();
3622 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
3623 if (GET_MODE_CLASS (mode) == MODE_FLOAT
3624 && GET_CODE (op0) == CONST_DOUBLE && GET_CODE (op1) == CONST_DOUBLE
3625 && mode == GET_MODE (op0) && mode == GET_MODE (op1))
3627 REAL_VALUE_TYPE f0, f1, value;
3628 jmp_buf handler;
3630 if (setjmp (handler))
3631 return 0;
3633 set_float_handler (handler);
3635 REAL_VALUE_FROM_CONST_DOUBLE (f0, op0);
3636 REAL_VALUE_FROM_CONST_DOUBLE (f1, op1);
3637 f0 = real_value_truncate (mode, f0);
3638 f1 = real_value_truncate (mode, f1);
3640 #ifdef REAL_ARITHMETIC
3641 #ifndef REAL_INFINITY
3642 if (code == DIV && REAL_VALUES_EQUAL (f1, dconst0))
3643 return 0;
3644 #endif
3645 REAL_ARITHMETIC (value, rtx_to_tree_code (code), f0, f1);
3646 #else
3647 switch (code)
3649 case PLUS:
3650 value = f0 + f1;
3651 break;
3652 case MINUS:
3653 value = f0 - f1;
3654 break;
3655 case MULT:
3656 value = f0 * f1;
3657 break;
3658 case DIV:
3659 #ifndef REAL_INFINITY
3660 if (f1 == 0)
3661 return 0;
3662 #endif
3663 value = f0 / f1;
3664 break;
3665 case SMIN:
3666 value = MIN (f0, f1);
3667 break;
3668 case SMAX:
3669 value = MAX (f0, f1);
3670 break;
3671 default:
3672 abort ();
3674 #endif
3676 value = real_value_truncate (mode, value);
3677 set_float_handler (NULL_PTR);
3678 return CONST_DOUBLE_FROM_REAL_VALUE (value, mode);
3680 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
3682 /* We can fold some multi-word operations. */
3683 if (GET_MODE_CLASS (mode) == MODE_INT
3684 && width == HOST_BITS_PER_WIDE_INT * 2
3685 && (GET_CODE (op0) == CONST_DOUBLE || GET_CODE (op0) == CONST_INT)
3686 && (GET_CODE (op1) == CONST_DOUBLE || GET_CODE (op1) == CONST_INT))
3688 HOST_WIDE_INT l1, l2, h1, h2, lv, hv;
3690 if (GET_CODE (op0) == CONST_DOUBLE)
3691 l1 = CONST_DOUBLE_LOW (op0), h1 = CONST_DOUBLE_HIGH (op0);
3692 else
3693 l1 = INTVAL (op0), h1 = l1 < 0 ? -1 : 0;
3695 if (GET_CODE (op1) == CONST_DOUBLE)
3696 l2 = CONST_DOUBLE_LOW (op1), h2 = CONST_DOUBLE_HIGH (op1);
3697 else
3698 l2 = INTVAL (op1), h2 = l2 < 0 ? -1 : 0;
3700 switch (code)
3702 case MINUS:
3703 /* A - B == A + (-B). */
3704 neg_double (l2, h2, &lv, &hv);
3705 l2 = lv, h2 = hv;
3707 /* .. fall through ... */
3709 case PLUS:
3710 add_double (l1, h1, l2, h2, &lv, &hv);
3711 break;
3713 case MULT:
3714 mul_double (l1, h1, l2, h2, &lv, &hv);
3715 break;
3717 case DIV: case MOD: case UDIV: case UMOD:
3718 /* We'd need to include tree.h to do this and it doesn't seem worth
3719 it. */
3720 return 0;
3722 case AND:
3723 lv = l1 & l2, hv = h1 & h2;
3724 break;
3726 case IOR:
3727 lv = l1 | l2, hv = h1 | h2;
3728 break;
3730 case XOR:
3731 lv = l1 ^ l2, hv = h1 ^ h2;
3732 break;
3734 case SMIN:
3735 if (h1 < h2
3736 || (h1 == h2
3737 && ((unsigned HOST_WIDE_INT) l1
3738 < (unsigned HOST_WIDE_INT) l2)))
3739 lv = l1, hv = h1;
3740 else
3741 lv = l2, hv = h2;
3742 break;
3744 case SMAX:
3745 if (h1 > h2
3746 || (h1 == h2
3747 && ((unsigned HOST_WIDE_INT) l1
3748 > (unsigned HOST_WIDE_INT) l2)))
3749 lv = l1, hv = h1;
3750 else
3751 lv = l2, hv = h2;
3752 break;
3754 case UMIN:
3755 if ((unsigned HOST_WIDE_INT) h1 < (unsigned HOST_WIDE_INT) h2
3756 || (h1 == h2
3757 && ((unsigned HOST_WIDE_INT) l1
3758 < (unsigned HOST_WIDE_INT) l2)))
3759 lv = l1, hv = h1;
3760 else
3761 lv = l2, hv = h2;
3762 break;
3764 case UMAX:
3765 if ((unsigned HOST_WIDE_INT) h1 > (unsigned HOST_WIDE_INT) h2
3766 || (h1 == h2
3767 && ((unsigned HOST_WIDE_INT) l1
3768 > (unsigned HOST_WIDE_INT) l2)))
3769 lv = l1, hv = h1;
3770 else
3771 lv = l2, hv = h2;
3772 break;
3774 case LSHIFTRT: case ASHIFTRT:
3775 case ASHIFT:
3776 case ROTATE: case ROTATERT:
3777 #ifdef SHIFT_COUNT_TRUNCATED
3778 if (SHIFT_COUNT_TRUNCATED)
3779 l2 &= (GET_MODE_BITSIZE (mode) - 1), h2 = 0;
3780 #endif
3782 if (h2 != 0 || l2 < 0 || l2 >= GET_MODE_BITSIZE (mode))
3783 return 0;
3785 if (code == LSHIFTRT || code == ASHIFTRT)
3786 rshift_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv,
3787 code == ASHIFTRT);
3788 else if (code == ASHIFT)
3789 lshift_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv, 1);
3790 else if (code == ROTATE)
3791 lrotate_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv);
3792 else /* code == ROTATERT */
3793 rrotate_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv);
3794 break;
3796 default:
3797 return 0;
3800 return immed_double_const (lv, hv, mode);
3803 if (GET_CODE (op0) != CONST_INT || GET_CODE (op1) != CONST_INT
3804 || width > HOST_BITS_PER_WIDE_INT || width == 0)
3806 /* Even if we can't compute a constant result,
3807 there are some cases worth simplifying. */
3809 switch (code)
3811 case PLUS:
3812 /* In IEEE floating point, x+0 is not the same as x. Similarly
3813 for the other optimizations below. */
3814 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
3815 && FLOAT_MODE_P (mode) && ! flag_fast_math)
3816 break;
3818 if (op1 == CONST0_RTX (mode))
3819 return op0;
3821 /* ((-a) + b) -> (b - a) and similarly for (a + (-b)) */
3822 if (GET_CODE (op0) == NEG)
3823 return cse_gen_binary (MINUS, mode, op1, XEXP (op0, 0));
3824 else if (GET_CODE (op1) == NEG)
3825 return cse_gen_binary (MINUS, mode, op0, XEXP (op1, 0));
3827 /* Handle both-operands-constant cases. We can only add
3828 CONST_INTs to constants since the sum of relocatable symbols
3829 can't be handled by most assemblers. Don't add CONST_INT
3830 to CONST_INT since overflow won't be computed properly if wider
3831 than HOST_BITS_PER_WIDE_INT. */
3833 if (CONSTANT_P (op0) && GET_MODE (op0) != VOIDmode
3834 && GET_CODE (op1) == CONST_INT)
3835 return plus_constant (op0, INTVAL (op1));
3836 else if (CONSTANT_P (op1) && GET_MODE (op1) != VOIDmode
3837 && GET_CODE (op0) == CONST_INT)
3838 return plus_constant (op1, INTVAL (op0));
3840 /* See if this is something like X * C - X or vice versa or
3841 if the multiplication is written as a shift. If so, we can
3842 distribute and make a new multiply, shift, or maybe just
3843 have X (if C is 2 in the example above). But don't make
3844 real multiply if we didn't have one before. */
3846 if (! FLOAT_MODE_P (mode))
3848 HOST_WIDE_INT coeff0 = 1, coeff1 = 1;
3849 rtx lhs = op0, rhs = op1;
3850 int had_mult = 0;
3852 if (GET_CODE (lhs) == NEG)
3853 coeff0 = -1, lhs = XEXP (lhs, 0);
3854 else if (GET_CODE (lhs) == MULT
3855 && GET_CODE (XEXP (lhs, 1)) == CONST_INT)
3857 coeff0 = INTVAL (XEXP (lhs, 1)), lhs = XEXP (lhs, 0);
3858 had_mult = 1;
3860 else if (GET_CODE (lhs) == ASHIFT
3861 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
3862 && INTVAL (XEXP (lhs, 1)) >= 0
3863 && INTVAL (XEXP (lhs, 1)) < HOST_BITS_PER_WIDE_INT)
3865 coeff0 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (lhs, 1));
3866 lhs = XEXP (lhs, 0);
3869 if (GET_CODE (rhs) == NEG)
3870 coeff1 = -1, rhs = XEXP (rhs, 0);
3871 else if (GET_CODE (rhs) == MULT
3872 && GET_CODE (XEXP (rhs, 1)) == CONST_INT)
3874 coeff1 = INTVAL (XEXP (rhs, 1)), rhs = XEXP (rhs, 0);
3875 had_mult = 1;
3877 else if (GET_CODE (rhs) == ASHIFT
3878 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
3879 && INTVAL (XEXP (rhs, 1)) >= 0
3880 && INTVAL (XEXP (rhs, 1)) < HOST_BITS_PER_WIDE_INT)
3882 coeff1 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (rhs, 1));
3883 rhs = XEXP (rhs, 0);
3886 if (rtx_equal_p (lhs, rhs))
3888 tem = cse_gen_binary (MULT, mode, lhs,
3889 GEN_INT (coeff0 + coeff1));
3890 return (GET_CODE (tem) == MULT && ! had_mult) ? 0 : tem;
3894 /* If one of the operands is a PLUS or a MINUS, see if we can
3895 simplify this by the associative law.
3896 Don't use the associative law for floating point.
3897 The inaccuracy makes it nonassociative,
3898 and subtle programs can break if operations are associated. */
3900 if (INTEGRAL_MODE_P (mode)
3901 && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
3902 || GET_CODE (op1) == PLUS || GET_CODE (op1) == MINUS)
3903 && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
3904 return tem;
3905 break;
3907 case COMPARE:
3908 #ifdef HAVE_cc0
3909 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
3910 using cc0, in which case we want to leave it as a COMPARE
3911 so we can distinguish it from a register-register-copy.
3913 In IEEE floating point, x-0 is not the same as x. */
3915 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3916 || ! FLOAT_MODE_P (mode) || flag_fast_math)
3917 && op1 == CONST0_RTX (mode))
3918 return op0;
3919 #else
3920 /* Do nothing here. */
3921 #endif
3922 break;
3924 case MINUS:
3925 /* None of these optimizations can be done for IEEE
3926 floating point. */
3927 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
3928 && FLOAT_MODE_P (mode) && ! flag_fast_math)
3929 break;
3931 /* We can't assume x-x is 0 even with non-IEEE floating point,
3932 but since it is zero except in very strange circumstances, we
3933 will treat it as zero with -ffast-math. */
3934 if (rtx_equal_p (op0, op1)
3935 && ! side_effects_p (op0)
3936 && (! FLOAT_MODE_P (mode) || flag_fast_math))
3937 return CONST0_RTX (mode);
3939 /* Change subtraction from zero into negation. */
3940 if (op0 == CONST0_RTX (mode))
3941 return gen_rtx_NEG (mode, op1);
3943 /* (-1 - a) is ~a. */
3944 if (op0 == constm1_rtx)
3945 return gen_rtx_NOT (mode, op1);
3947 /* Subtracting 0 has no effect. */
3948 if (op1 == CONST0_RTX (mode))
3949 return op0;
3951 /* See if this is something like X * C - X or vice versa or
3952 if the multiplication is written as a shift. If so, we can
3953 distribute and make a new multiply, shift, or maybe just
3954 have X (if C is 2 in the example above). But don't make
3955 real multiply if we didn't have one before. */
3957 if (! FLOAT_MODE_P (mode))
3959 HOST_WIDE_INT coeff0 = 1, coeff1 = 1;
3960 rtx lhs = op0, rhs = op1;
3961 int had_mult = 0;
3963 if (GET_CODE (lhs) == NEG)
3964 coeff0 = -1, lhs = XEXP (lhs, 0);
3965 else if (GET_CODE (lhs) == MULT
3966 && GET_CODE (XEXP (lhs, 1)) == CONST_INT)
3968 coeff0 = INTVAL (XEXP (lhs, 1)), lhs = XEXP (lhs, 0);
3969 had_mult = 1;
3971 else if (GET_CODE (lhs) == ASHIFT
3972 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
3973 && INTVAL (XEXP (lhs, 1)) >= 0
3974 && INTVAL (XEXP (lhs, 1)) < HOST_BITS_PER_WIDE_INT)
3976 coeff0 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (lhs, 1));
3977 lhs = XEXP (lhs, 0);
3980 if (GET_CODE (rhs) == NEG)
3981 coeff1 = - 1, rhs = XEXP (rhs, 0);
3982 else if (GET_CODE (rhs) == MULT
3983 && GET_CODE (XEXP (rhs, 1)) == CONST_INT)
3985 coeff1 = INTVAL (XEXP (rhs, 1)), rhs = XEXP (rhs, 0);
3986 had_mult = 1;
3988 else if (GET_CODE (rhs) == ASHIFT
3989 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
3990 && INTVAL (XEXP (rhs, 1)) >= 0
3991 && INTVAL (XEXP (rhs, 1)) < HOST_BITS_PER_WIDE_INT)
3993 coeff1 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (rhs, 1));
3994 rhs = XEXP (rhs, 0);
3997 if (rtx_equal_p (lhs, rhs))
3999 tem = cse_gen_binary (MULT, mode, lhs,
4000 GEN_INT (coeff0 - coeff1));
4001 return (GET_CODE (tem) == MULT && ! had_mult) ? 0 : tem;
4005 /* (a - (-b)) -> (a + b). */
4006 if (GET_CODE (op1) == NEG)
4007 return cse_gen_binary (PLUS, mode, op0, XEXP (op1, 0));
4009 /* If one of the operands is a PLUS or a MINUS, see if we can
4010 simplify this by the associative law.
4011 Don't use the associative law for floating point.
4012 The inaccuracy makes it nonassociative,
4013 and subtle programs can break if operations are associated. */
4015 if (INTEGRAL_MODE_P (mode)
4016 && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
4017 || GET_CODE (op1) == PLUS || GET_CODE (op1) == MINUS)
4018 && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
4019 return tem;
4021 /* Don't let a relocatable value get a negative coeff. */
4022 if (GET_CODE (op1) == CONST_INT && GET_MODE (op0) != VOIDmode)
4023 return plus_constant (op0, - INTVAL (op1));
4025 /* (x - (x & y)) -> (x & ~y) */
4026 if (GET_CODE (op1) == AND)
4028 if (rtx_equal_p (op0, XEXP (op1, 0)))
4029 return cse_gen_binary (AND, mode, op0,
4030 gen_rtx_NOT (mode, XEXP (op1, 1)));
4031 if (rtx_equal_p (op0, XEXP (op1, 1)))
4032 return cse_gen_binary (AND, mode, op0,
4033 gen_rtx_NOT (mode, XEXP (op1, 0)));
4035 break;
4037 case MULT:
4038 if (op1 == constm1_rtx)
4040 tem = simplify_unary_operation (NEG, mode, op0, mode);
4042 return tem ? tem : gen_rtx_NEG (mode, op0);
4045 /* In IEEE floating point, x*0 is not always 0. */
4046 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
4047 || ! FLOAT_MODE_P (mode) || flag_fast_math)
4048 && op1 == CONST0_RTX (mode)
4049 && ! side_effects_p (op0))
4050 return op1;
4052 /* In IEEE floating point, x*1 is not equivalent to x for nans.
4053 However, ANSI says we can drop signals,
4054 so we can do this anyway. */
4055 if (op1 == CONST1_RTX (mode))
4056 return op0;
4058 /* Convert multiply by constant power of two into shift unless
4059 we are still generating RTL. This test is a kludge. */
4060 if (GET_CODE (op1) == CONST_INT
4061 && (val = exact_log2 (INTVAL (op1))) >= 0
4062 /* If the mode is larger than the host word size, and the
4063 uppermost bit is set, then this isn't a power of two due
4064 to implicit sign extension. */
4065 && (width <= HOST_BITS_PER_WIDE_INT
4066 || val != HOST_BITS_PER_WIDE_INT - 1)
4067 && ! rtx_equal_function_value_matters)
4068 return gen_rtx_ASHIFT (mode, op0, GEN_INT (val));
4070 if (GET_CODE (op1) == CONST_DOUBLE
4071 && GET_MODE_CLASS (GET_MODE (op1)) == MODE_FLOAT)
4073 REAL_VALUE_TYPE d;
4074 jmp_buf handler;
4075 int op1is2, op1ism1;
4077 if (setjmp (handler))
4078 return 0;
4080 set_float_handler (handler);
4081 REAL_VALUE_FROM_CONST_DOUBLE (d, op1);
4082 op1is2 = REAL_VALUES_EQUAL (d, dconst2);
4083 op1ism1 = REAL_VALUES_EQUAL (d, dconstm1);
4084 set_float_handler (NULL_PTR);
4086 /* x*2 is x+x and x*(-1) is -x */
4087 if (op1is2 && GET_MODE (op0) == mode)
4088 return gen_rtx_PLUS (mode, op0, copy_rtx (op0));
4090 else if (op1ism1 && GET_MODE (op0) == mode)
4091 return gen_rtx_NEG (mode, op0);
4093 break;
4095 case IOR:
4096 if (op1 == const0_rtx)
4097 return op0;
4098 if (GET_CODE (op1) == CONST_INT
4099 && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode))
4100 return op1;
4101 if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
4102 return op0;
4103 /* A | (~A) -> -1 */
4104 if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
4105 || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
4106 && ! side_effects_p (op0)
4107 && GET_MODE_CLASS (mode) != MODE_CC)
4108 return constm1_rtx;
4109 break;
4111 case XOR:
4112 if (op1 == const0_rtx)
4113 return op0;
4114 if (GET_CODE (op1) == CONST_INT
4115 && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode))
4116 return gen_rtx_NOT (mode, op0);
4117 if (op0 == op1 && ! side_effects_p (op0)
4118 && GET_MODE_CLASS (mode) != MODE_CC)
4119 return const0_rtx;
4120 break;
4122 case AND:
4123 if (op1 == const0_rtx && ! side_effects_p (op0))
4124 return const0_rtx;
4125 if (GET_CODE (op1) == CONST_INT
4126 && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode))
4127 return op0;
4128 if (op0 == op1 && ! side_effects_p (op0)
4129 && GET_MODE_CLASS (mode) != MODE_CC)
4130 return op0;
4131 /* A & (~A) -> 0 */
4132 if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
4133 || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
4134 && ! side_effects_p (op0)
4135 && GET_MODE_CLASS (mode) != MODE_CC)
4136 return const0_rtx;
4137 break;
4139 case UDIV:
4140 /* Convert divide by power of two into shift (divide by 1 handled
4141 below). */
4142 if (GET_CODE (op1) == CONST_INT
4143 && (arg1 = exact_log2 (INTVAL (op1))) > 0)
4144 return gen_rtx_LSHIFTRT (mode, op0, GEN_INT (arg1));
4146 /* ... fall through ... */
4148 case DIV:
4149 if (op1 == CONST1_RTX (mode))
4150 return op0;
4152 /* In IEEE floating point, 0/x is not always 0. */
4153 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
4154 || ! FLOAT_MODE_P (mode) || flag_fast_math)
4155 && op0 == CONST0_RTX (mode)
4156 && ! side_effects_p (op1))
4157 return op0;
4159 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
4160 /* Change division by a constant into multiplication. Only do
4161 this with -ffast-math until an expert says it is safe in
4162 general. */
4163 else if (GET_CODE (op1) == CONST_DOUBLE
4164 && GET_MODE_CLASS (GET_MODE (op1)) == MODE_FLOAT
4165 && op1 != CONST0_RTX (mode)
4166 && flag_fast_math)
4168 REAL_VALUE_TYPE d;
4169 REAL_VALUE_FROM_CONST_DOUBLE (d, op1);
4171 if (! REAL_VALUES_EQUAL (d, dconst0))
4173 #if defined (REAL_ARITHMETIC)
4174 REAL_ARITHMETIC (d, rtx_to_tree_code (DIV), dconst1, d);
4175 return gen_rtx_MULT (mode, op0,
4176 CONST_DOUBLE_FROM_REAL_VALUE (d, mode));
4177 #else
4178 return
4179 gen_rtx_MULT (mode, op0,
4180 CONST_DOUBLE_FROM_REAL_VALUE (1./d, mode));
4181 #endif
4184 #endif
4185 break;
4187 case UMOD:
4188 /* Handle modulus by power of two (mod with 1 handled below). */
4189 if (GET_CODE (op1) == CONST_INT
4190 && exact_log2 (INTVAL (op1)) > 0)
4191 return gen_rtx_AND (mode, op0, GEN_INT (INTVAL (op1) - 1));
4193 /* ... fall through ... */
4195 case MOD:
4196 if ((op0 == const0_rtx || op1 == const1_rtx)
4197 && ! side_effects_p (op0) && ! side_effects_p (op1))
4198 return const0_rtx;
4199 break;
4201 case ROTATERT:
4202 case ROTATE:
4203 /* Rotating ~0 always results in ~0. */
4204 if (GET_CODE (op0) == CONST_INT && width <= HOST_BITS_PER_WIDE_INT
4205 && (unsigned HOST_WIDE_INT) INTVAL (op0) == GET_MODE_MASK (mode)
4206 && ! side_effects_p (op1))
4207 return op0;
4209 /* ... fall through ... */
4211 case ASHIFT:
4212 case ASHIFTRT:
4213 case LSHIFTRT:
4214 if (op1 == const0_rtx)
4215 return op0;
4216 if (op0 == const0_rtx && ! side_effects_p (op1))
4217 return op0;
4218 break;
4220 case SMIN:
4221 if (width <= HOST_BITS_PER_WIDE_INT && GET_CODE (op1) == CONST_INT
4222 && INTVAL (op1) == (HOST_WIDE_INT) 1 << (width -1)
4223 && ! side_effects_p (op0))
4224 return op1;
4225 else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
4226 return op0;
4227 break;
4229 case SMAX:
4230 if (width <= HOST_BITS_PER_WIDE_INT && GET_CODE (op1) == CONST_INT
4231 && ((unsigned HOST_WIDE_INT) INTVAL (op1)
4232 == (unsigned HOST_WIDE_INT) GET_MODE_MASK (mode) >> 1)
4233 && ! side_effects_p (op0))
4234 return op1;
4235 else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
4236 return op0;
4237 break;
4239 case UMIN:
4240 if (op1 == const0_rtx && ! side_effects_p (op0))
4241 return op1;
4242 else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
4243 return op0;
4244 break;
4246 case UMAX:
4247 if (op1 == constm1_rtx && ! side_effects_p (op0))
4248 return op1;
4249 else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
4250 return op0;
4251 break;
4253 default:
4254 abort ();
4257 return 0;
4260 /* Get the integer argument values in two forms:
4261 zero-extended in ARG0, ARG1 and sign-extended in ARG0S, ARG1S. */
4263 arg0 = INTVAL (op0);
4264 arg1 = INTVAL (op1);
4266 if (width < HOST_BITS_PER_WIDE_INT)
4268 arg0 &= ((HOST_WIDE_INT) 1 << width) - 1;
4269 arg1 &= ((HOST_WIDE_INT) 1 << width) - 1;
4271 arg0s = arg0;
4272 if (arg0s & ((HOST_WIDE_INT) 1 << (width - 1)))
4273 arg0s |= ((HOST_WIDE_INT) (-1) << width);
4275 arg1s = arg1;
4276 if (arg1s & ((HOST_WIDE_INT) 1 << (width - 1)))
4277 arg1s |= ((HOST_WIDE_INT) (-1) << width);
4279 else
4281 arg0s = arg0;
4282 arg1s = arg1;
4285 /* Compute the value of the arithmetic. */
4287 switch (code)
4289 case PLUS:
4290 val = arg0s + arg1s;
4291 break;
4293 case MINUS:
4294 val = arg0s - arg1s;
4295 break;
4297 case MULT:
4298 val = arg0s * arg1s;
4299 break;
4301 case DIV:
4302 if (arg1s == 0)
4303 return 0;
4304 val = arg0s / arg1s;
4305 break;
4307 case MOD:
4308 if (arg1s == 0)
4309 return 0;
4310 val = arg0s % arg1s;
4311 break;
4313 case UDIV:
4314 if (arg1 == 0)
4315 return 0;
4316 val = (unsigned HOST_WIDE_INT) arg0 / arg1;
4317 break;
4319 case UMOD:
4320 if (arg1 == 0)
4321 return 0;
4322 val = (unsigned HOST_WIDE_INT) arg0 % arg1;
4323 break;
4325 case AND:
4326 val = arg0 & arg1;
4327 break;
4329 case IOR:
4330 val = arg0 | arg1;
4331 break;
4333 case XOR:
4334 val = arg0 ^ arg1;
4335 break;
4337 case LSHIFTRT:
4338 /* If shift count is undefined, don't fold it; let the machine do
4339 what it wants. But truncate it if the machine will do that. */
4340 if (arg1 < 0)
4341 return 0;
4343 #ifdef SHIFT_COUNT_TRUNCATED
4344 if (SHIFT_COUNT_TRUNCATED)
4345 arg1 %= width;
4346 #endif
4348 val = ((unsigned HOST_WIDE_INT) arg0) >> arg1;
4349 break;
4351 case ASHIFT:
4352 if (arg1 < 0)
4353 return 0;
4355 #ifdef SHIFT_COUNT_TRUNCATED
4356 if (SHIFT_COUNT_TRUNCATED)
4357 arg1 %= width;
4358 #endif
4360 val = ((unsigned HOST_WIDE_INT) arg0) << arg1;
4361 break;
4363 case ASHIFTRT:
4364 if (arg1 < 0)
4365 return 0;
4367 #ifdef SHIFT_COUNT_TRUNCATED
4368 if (SHIFT_COUNT_TRUNCATED)
4369 arg1 %= width;
4370 #endif
4372 val = arg0s >> arg1;
4374 /* Bootstrap compiler may not have sign extended the right shift.
4375 Manually extend the sign to insure bootstrap cc matches gcc. */
4376 if (arg0s < 0 && arg1 > 0)
4377 val |= ((HOST_WIDE_INT) -1) << (HOST_BITS_PER_WIDE_INT - arg1);
4379 break;
4381 case ROTATERT:
4382 if (arg1 < 0)
4383 return 0;
4385 arg1 %= width;
4386 val = ((((unsigned HOST_WIDE_INT) arg0) << (width - arg1))
4387 | (((unsigned HOST_WIDE_INT) arg0) >> arg1));
4388 break;
4390 case ROTATE:
4391 if (arg1 < 0)
4392 return 0;
4394 arg1 %= width;
4395 val = ((((unsigned HOST_WIDE_INT) arg0) << arg1)
4396 | (((unsigned HOST_WIDE_INT) arg0) >> (width - arg1)));
4397 break;
4399 case COMPARE:
4400 /* Do nothing here. */
4401 return 0;
4403 case SMIN:
4404 val = arg0s <= arg1s ? arg0s : arg1s;
4405 break;
4407 case UMIN:
4408 val = ((unsigned HOST_WIDE_INT) arg0
4409 <= (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
4410 break;
4412 case SMAX:
4413 val = arg0s > arg1s ? arg0s : arg1s;
4414 break;
4416 case UMAX:
4417 val = ((unsigned HOST_WIDE_INT) arg0
4418 > (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
4419 break;
4421 default:
4422 abort ();
4425 val = trunc_int_for_mode (val, mode);
4427 return GEN_INT (val);
4430 /* Simplify a PLUS or MINUS, at least one of whose operands may be another
4431 PLUS or MINUS.
4433 Rather than test for specific case, we do this by a brute-force method
4434 and do all possible simplifications until no more changes occur. Then
4435 we rebuild the operation. */
4437 static rtx
4438 simplify_plus_minus (code, mode, op0, op1)
4439 enum rtx_code code;
4440 enum machine_mode mode;
4441 rtx op0, op1;
4443 rtx ops[8];
4444 int negs[8];
4445 rtx result, tem;
4446 int n_ops = 2, input_ops = 2, input_consts = 0, n_consts = 0;
4447 int first = 1, negate = 0, changed;
4448 int i, j;
4450 bzero ((char *) ops, sizeof ops);
4452 /* Set up the two operands and then expand them until nothing has been
4453 changed. If we run out of room in our array, give up; this should
4454 almost never happen. */
4456 ops[0] = op0, ops[1] = op1, negs[0] = 0, negs[1] = (code == MINUS);
4458 changed = 1;
4459 while (changed)
4461 changed = 0;
4463 for (i = 0; i < n_ops; i++)
4464 switch (GET_CODE (ops[i]))
4466 case PLUS:
4467 case MINUS:
4468 if (n_ops == 7)
4469 return 0;
4471 ops[n_ops] = XEXP (ops[i], 1);
4472 negs[n_ops++] = GET_CODE (ops[i]) == MINUS ? !negs[i] : negs[i];
4473 ops[i] = XEXP (ops[i], 0);
4474 input_ops++;
4475 changed = 1;
4476 break;
4478 case NEG:
4479 ops[i] = XEXP (ops[i], 0);
4480 negs[i] = ! negs[i];
4481 changed = 1;
4482 break;
4484 case CONST:
4485 ops[i] = XEXP (ops[i], 0);
4486 input_consts++;
4487 changed = 1;
4488 break;
4490 case NOT:
4491 /* ~a -> (-a - 1) */
4492 if (n_ops != 7)
4494 ops[n_ops] = constm1_rtx;
4495 negs[n_ops++] = negs[i];
4496 ops[i] = XEXP (ops[i], 0);
4497 negs[i] = ! negs[i];
4498 changed = 1;
4500 break;
4502 case CONST_INT:
4503 if (negs[i])
4504 ops[i] = GEN_INT (- INTVAL (ops[i])), negs[i] = 0, changed = 1;
4505 break;
4507 default:
4508 break;
4512 /* If we only have two operands, we can't do anything. */
4513 if (n_ops <= 2)
4514 return 0;
4516 /* Now simplify each pair of operands until nothing changes. The first
4517 time through just simplify constants against each other. */
4519 changed = 1;
4520 while (changed)
4522 changed = first;
4524 for (i = 0; i < n_ops - 1; i++)
4525 for (j = i + 1; j < n_ops; j++)
4526 if (ops[i] != 0 && ops[j] != 0
4527 && (! first || (CONSTANT_P (ops[i]) && CONSTANT_P (ops[j]))))
4529 rtx lhs = ops[i], rhs = ops[j];
4530 enum rtx_code ncode = PLUS;
4532 if (negs[i] && ! negs[j])
4533 lhs = ops[j], rhs = ops[i], ncode = MINUS;
4534 else if (! negs[i] && negs[j])
4535 ncode = MINUS;
4537 tem = simplify_binary_operation (ncode, mode, lhs, rhs);
4538 if (tem)
4540 ops[i] = tem, ops[j] = 0;
4541 negs[i] = negs[i] && negs[j];
4542 if (GET_CODE (tem) == NEG)
4543 ops[i] = XEXP (tem, 0), negs[i] = ! negs[i];
4545 if (GET_CODE (ops[i]) == CONST_INT && negs[i])
4546 ops[i] = GEN_INT (- INTVAL (ops[i])), negs[i] = 0;
4547 changed = 1;
4551 first = 0;
4554 /* Pack all the operands to the lower-numbered entries and give up if
4555 we didn't reduce the number of operands we had. Make sure we
4556 count a CONST as two operands. If we have the same number of
4557 operands, but have made more CONSTs than we had, this is also
4558 an improvement, so accept it. */
4560 for (i = 0, j = 0; j < n_ops; j++)
4561 if (ops[j] != 0)
4563 ops[i] = ops[j], negs[i++] = negs[j];
4564 if (GET_CODE (ops[j]) == CONST)
4565 n_consts++;
4568 if (i + n_consts > input_ops
4569 || (i + n_consts == input_ops && n_consts <= input_consts))
4570 return 0;
4572 n_ops = i;
4574 /* If we have a CONST_INT, put it last. */
4575 for (i = 0; i < n_ops - 1; i++)
4576 if (GET_CODE (ops[i]) == CONST_INT)
4578 tem = ops[n_ops - 1], ops[n_ops - 1] = ops[i] , ops[i] = tem;
4579 j = negs[n_ops - 1], negs[n_ops - 1] = negs[i], negs[i] = j;
4582 /* Put a non-negated operand first. If there aren't any, make all
4583 operands positive and negate the whole thing later. */
4584 for (i = 0; i < n_ops && negs[i]; i++)
4587 if (i == n_ops)
4589 for (i = 0; i < n_ops; i++)
4590 negs[i] = 0;
4591 negate = 1;
4593 else if (i != 0)
4595 tem = ops[0], ops[0] = ops[i], ops[i] = tem;
4596 j = negs[0], negs[0] = negs[i], negs[i] = j;
4599 /* Now make the result by performing the requested operations. */
4600 result = ops[0];
4601 for (i = 1; i < n_ops; i++)
4602 result = cse_gen_binary (negs[i] ? MINUS : PLUS, mode, result, ops[i]);
4604 return negate ? gen_rtx_NEG (mode, result) : result;
4607 /* Make a binary operation by properly ordering the operands and
4608 seeing if the expression folds. */
4610 static rtx
4611 cse_gen_binary (code, mode, op0, op1)
4612 enum rtx_code code;
4613 enum machine_mode mode;
4614 rtx op0, op1;
4616 rtx tem;
4618 /* Put complex operands first and constants second if commutative. */
4619 if (GET_RTX_CLASS (code) == 'c'
4620 && ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
4621 || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
4622 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
4623 || (GET_CODE (op0) == SUBREG
4624 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
4625 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')))
4626 tem = op0, op0 = op1, op1 = tem;
4628 /* If this simplifies, do it. */
4629 tem = simplify_binary_operation (code, mode, op0, op1);
4631 if (tem)
4632 return tem;
4634 /* Handle addition and subtraction of CONST_INT specially. Otherwise,
4635 just form the operation. */
4637 if (code == PLUS && GET_CODE (op1) == CONST_INT
4638 && GET_MODE (op0) != VOIDmode)
4639 return plus_constant (op0, INTVAL (op1));
4640 else if (code == MINUS && GET_CODE (op1) == CONST_INT
4641 && GET_MODE (op0) != VOIDmode)
4642 return plus_constant (op0, - INTVAL (op1));
4643 else
4644 return gen_rtx_fmt_ee (code, mode, op0, op1);
4647 struct cfc_args
4649 /* Input */
4650 rtx op0, op1;
4651 /* Output */
4652 int equal, op0lt, op1lt;
4655 static void
4656 check_fold_consts (data)
4657 PTR data;
4659 struct cfc_args * args = (struct cfc_args *) data;
4660 REAL_VALUE_TYPE d0, d1;
4662 REAL_VALUE_FROM_CONST_DOUBLE (d0, args->op0);
4663 REAL_VALUE_FROM_CONST_DOUBLE (d1, args->op1);
4664 args->equal = REAL_VALUES_EQUAL (d0, d1);
4665 args->op0lt = REAL_VALUES_LESS (d0, d1);
4666 args->op1lt = REAL_VALUES_LESS (d1, d0);
4669 /* Like simplify_binary_operation except used for relational operators.
4670 MODE is the mode of the operands, not that of the result. If MODE
4671 is VOIDmode, both operands must also be VOIDmode and we compare the
4672 operands in "infinite precision".
4674 If no simplification is possible, this function returns zero. Otherwise,
4675 it returns either const_true_rtx or const0_rtx. */
4678 simplify_relational_operation (code, mode, op0, op1)
4679 enum rtx_code code;
4680 enum machine_mode mode;
4681 rtx op0, op1;
4683 int equal, op0lt, op0ltu, op1lt, op1ltu;
4684 rtx tem;
4686 /* If op0 is a compare, extract the comparison arguments from it. */
4687 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
4688 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4690 /* We can't simplify MODE_CC values since we don't know what the
4691 actual comparison is. */
4692 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC
4693 #ifdef HAVE_cc0
4694 || op0 == cc0_rtx
4695 #endif
4697 return 0;
4699 /* For integer comparisons of A and B maybe we can simplify A - B and can
4700 then simplify a comparison of that with zero. If A and B are both either
4701 a register or a CONST_INT, this can't help; testing for these cases will
4702 prevent infinite recursion here and speed things up.
4704 If CODE is an unsigned comparison, then we can never do this optimization,
4705 because it gives an incorrect result if the subtraction wraps around zero.
4706 ANSI C defines unsigned operations such that they never overflow, and
4707 thus such cases can not be ignored. */
4709 if (INTEGRAL_MODE_P (mode) && op1 != const0_rtx
4710 && ! ((GET_CODE (op0) == REG || GET_CODE (op0) == CONST_INT)
4711 && (GET_CODE (op1) == REG || GET_CODE (op1) == CONST_INT))
4712 && 0 != (tem = simplify_binary_operation (MINUS, mode, op0, op1))
4713 && code != GTU && code != GEU && code != LTU && code != LEU)
4714 return simplify_relational_operation (signed_condition (code),
4715 mode, tem, const0_rtx);
4717 /* For non-IEEE floating-point, if the two operands are equal, we know the
4718 result. */
4719 if (rtx_equal_p (op0, op1)
4720 && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
4721 || ! FLOAT_MODE_P (GET_MODE (op0)) || flag_fast_math))
4722 equal = 1, op0lt = 0, op0ltu = 0, op1lt = 0, op1ltu = 0;
4724 /* If the operands are floating-point constants, see if we can fold
4725 the result. */
4726 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
4727 else if (GET_CODE (op0) == CONST_DOUBLE && GET_CODE (op1) == CONST_DOUBLE
4728 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
4730 struct cfc_args args;
4732 /* Setup input for check_fold_consts() */
4733 args.op0 = op0;
4734 args.op1 = op1;
4736 if (do_float_handler(check_fold_consts, (PTR) &args) == 0)
4737 /* We got an exception from check_fold_consts() */
4738 return 0;
4740 /* Receive output from check_fold_consts() */
4741 equal = args.equal;
4742 op0lt = op0ltu = args.op0lt;
4743 op1lt = op1ltu = args.op1lt;
4745 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
4747 /* Otherwise, see if the operands are both integers. */
4748 else if ((GET_MODE_CLASS (mode) == MODE_INT || mode == VOIDmode)
4749 && (GET_CODE (op0) == CONST_DOUBLE || GET_CODE (op0) == CONST_INT)
4750 && (GET_CODE (op1) == CONST_DOUBLE || GET_CODE (op1) == CONST_INT))
4752 int width = GET_MODE_BITSIZE (mode);
4753 HOST_WIDE_INT l0s, h0s, l1s, h1s;
4754 unsigned HOST_WIDE_INT l0u, h0u, l1u, h1u;
4756 /* Get the two words comprising each integer constant. */
4757 if (GET_CODE (op0) == CONST_DOUBLE)
4759 l0u = l0s = CONST_DOUBLE_LOW (op0);
4760 h0u = h0s = CONST_DOUBLE_HIGH (op0);
4762 else
4764 l0u = l0s = INTVAL (op0);
4765 h0u = h0s = l0s < 0 ? -1 : 0;
4768 if (GET_CODE (op1) == CONST_DOUBLE)
4770 l1u = l1s = CONST_DOUBLE_LOW (op1);
4771 h1u = h1s = CONST_DOUBLE_HIGH (op1);
4773 else
4775 l1u = l1s = INTVAL (op1);
4776 h1u = h1s = l1s < 0 ? -1 : 0;
4779 /* If WIDTH is nonzero and smaller than HOST_BITS_PER_WIDE_INT,
4780 we have to sign or zero-extend the values. */
4781 if (width != 0 && width <= HOST_BITS_PER_WIDE_INT)
4782 h0u = h1u = 0, h0s = l0s < 0 ? -1 : 0, h1s = l1s < 0 ? -1 : 0;
4784 if (width != 0 && width < HOST_BITS_PER_WIDE_INT)
4786 l0u &= ((HOST_WIDE_INT) 1 << width) - 1;
4787 l1u &= ((HOST_WIDE_INT) 1 << width) - 1;
4789 if (l0s & ((HOST_WIDE_INT) 1 << (width - 1)))
4790 l0s |= ((HOST_WIDE_INT) (-1) << width);
4792 if (l1s & ((HOST_WIDE_INT) 1 << (width - 1)))
4793 l1s |= ((HOST_WIDE_INT) (-1) << width);
4796 equal = (h0u == h1u && l0u == l1u);
4797 op0lt = (h0s < h1s || (h0s == h1s && l0s < l1s));
4798 op1lt = (h1s < h0s || (h1s == h0s && l1s < l0s));
4799 op0ltu = (h0u < h1u || (h0u == h1u && l0u < l1u));
4800 op1ltu = (h1u < h0u || (h1u == h0u && l1u < l0u));
4803 /* Otherwise, there are some code-specific tests we can make. */
4804 else
4806 switch (code)
4808 case EQ:
4809 /* References to the frame plus a constant or labels cannot
4810 be zero, but a SYMBOL_REF can due to #pragma weak. */
4811 if (((NONZERO_BASE_PLUS_P (op0) && op1 == const0_rtx)
4812 || GET_CODE (op0) == LABEL_REF)
4813 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4814 /* On some machines, the ap reg can be 0 sometimes. */
4815 && op0 != arg_pointer_rtx
4816 #endif
4818 return const0_rtx;
4819 break;
4821 case NE:
4822 if (((NONZERO_BASE_PLUS_P (op0) && op1 == const0_rtx)
4823 || GET_CODE (op0) == LABEL_REF)
4824 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
4825 && op0 != arg_pointer_rtx
4826 #endif
4828 return const_true_rtx;
4829 break;
4831 case GEU:
4832 /* Unsigned values are never negative. */
4833 if (op1 == const0_rtx)
4834 return const_true_rtx;
4835 break;
4837 case LTU:
4838 if (op1 == const0_rtx)
4839 return const0_rtx;
4840 break;
4842 case LEU:
4843 /* Unsigned values are never greater than the largest
4844 unsigned value. */
4845 if (GET_CODE (op1) == CONST_INT
4846 && (unsigned HOST_WIDE_INT) INTVAL (op1) == GET_MODE_MASK (mode)
4847 && INTEGRAL_MODE_P (mode))
4848 return const_true_rtx;
4849 break;
4851 case GTU:
4852 if (GET_CODE (op1) == CONST_INT
4853 && (unsigned HOST_WIDE_INT) INTVAL (op1) == GET_MODE_MASK (mode)
4854 && INTEGRAL_MODE_P (mode))
4855 return const0_rtx;
4856 break;
4858 default:
4859 break;
4862 return 0;
4865 /* If we reach here, EQUAL, OP0LT, OP0LTU, OP1LT, and OP1LTU are set
4866 as appropriate. */
4867 switch (code)
4869 case EQ:
4870 return equal ? const_true_rtx : const0_rtx;
4871 case NE:
4872 return ! equal ? const_true_rtx : const0_rtx;
4873 case LT:
4874 return op0lt ? const_true_rtx : const0_rtx;
4875 case GT:
4876 return op1lt ? const_true_rtx : const0_rtx;
4877 case LTU:
4878 return op0ltu ? const_true_rtx : const0_rtx;
4879 case GTU:
4880 return op1ltu ? const_true_rtx : const0_rtx;
4881 case LE:
4882 return equal || op0lt ? const_true_rtx : const0_rtx;
4883 case GE:
4884 return equal || op1lt ? const_true_rtx : const0_rtx;
4885 case LEU:
4886 return equal || op0ltu ? const_true_rtx : const0_rtx;
4887 case GEU:
4888 return equal || op1ltu ? const_true_rtx : const0_rtx;
4889 default:
4890 abort ();
4894 /* Simplify CODE, an operation with result mode MODE and three operands,
4895 OP0, OP1, and OP2. OP0_MODE was the mode of OP0 before it became
4896 a constant. Return 0 if no simplifications is possible. */
4899 simplify_ternary_operation (code, mode, op0_mode, op0, op1, op2)
4900 enum rtx_code code;
4901 enum machine_mode mode, op0_mode;
4902 rtx op0, op1, op2;
4904 int width = GET_MODE_BITSIZE (mode);
4906 /* VOIDmode means "infinite" precision. */
4907 if (width == 0)
4908 width = HOST_BITS_PER_WIDE_INT;
4910 switch (code)
4912 case SIGN_EXTRACT:
4913 case ZERO_EXTRACT:
4914 if (GET_CODE (op0) == CONST_INT
4915 && GET_CODE (op1) == CONST_INT
4916 && GET_CODE (op2) == CONST_INT
4917 && INTVAL (op1) + INTVAL (op2) <= GET_MODE_BITSIZE (op0_mode)
4918 && width <= HOST_BITS_PER_WIDE_INT)
4920 /* Extracting a bit-field from a constant */
4921 HOST_WIDE_INT val = INTVAL (op0);
4923 if (BITS_BIG_ENDIAN)
4924 val >>= (GET_MODE_BITSIZE (op0_mode)
4925 - INTVAL (op2) - INTVAL (op1));
4926 else
4927 val >>= INTVAL (op2);
4929 if (HOST_BITS_PER_WIDE_INT != INTVAL (op1))
4931 /* First zero-extend. */
4932 val &= ((HOST_WIDE_INT) 1 << INTVAL (op1)) - 1;
4933 /* If desired, propagate sign bit. */
4934 if (code == SIGN_EXTRACT
4935 && (val & ((HOST_WIDE_INT) 1 << (INTVAL (op1) - 1))))
4936 val |= ~ (((HOST_WIDE_INT) 1 << INTVAL (op1)) - 1);
4939 /* Clear the bits that don't belong in our mode,
4940 unless they and our sign bit are all one.
4941 So we get either a reasonable negative value or a reasonable
4942 unsigned value for this mode. */
4943 if (width < HOST_BITS_PER_WIDE_INT
4944 && ((val & ((HOST_WIDE_INT) (-1) << (width - 1)))
4945 != ((HOST_WIDE_INT) (-1) << (width - 1))))
4946 val &= ((HOST_WIDE_INT) 1 << width) - 1;
4948 return GEN_INT (val);
4950 break;
4952 case IF_THEN_ELSE:
4953 if (GET_CODE (op0) == CONST_INT)
4954 return op0 != const0_rtx ? op1 : op2;
4956 /* Convert a == b ? b : a to "a". */
4957 if (GET_CODE (op0) == NE && ! side_effects_p (op0)
4958 && rtx_equal_p (XEXP (op0, 0), op1)
4959 && rtx_equal_p (XEXP (op0, 1), op2))
4960 return op1;
4961 else if (GET_CODE (op0) == EQ && ! side_effects_p (op0)
4962 && rtx_equal_p (XEXP (op0, 1), op1)
4963 && rtx_equal_p (XEXP (op0, 0), op2))
4964 return op2;
4965 else if (GET_RTX_CLASS (GET_CODE (op0)) == '<' && ! side_effects_p (op0))
4967 rtx temp;
4968 temp = simplify_relational_operation (GET_CODE (op0), op0_mode,
4969 XEXP (op0, 0), XEXP (op0, 1));
4970 /* See if any simplifications were possible. */
4971 if (temp == const0_rtx)
4972 return op2;
4973 else if (temp == const1_rtx)
4974 return op1;
4976 break;
4978 default:
4979 abort ();
4982 return 0;
4985 /* If X is a nontrivial arithmetic operation on an argument
4986 for which a constant value can be determined, return
4987 the result of operating on that value, as a constant.
4988 Otherwise, return X, possibly with one or more operands
4989 modified by recursive calls to this function.
4991 If X is a register whose contents are known, we do NOT
4992 return those contents here. equiv_constant is called to
4993 perform that task.
4995 INSN is the insn that we may be modifying. If it is 0, make a copy
4996 of X before modifying it. */
4998 static rtx
4999 fold_rtx (x, insn)
5000 rtx x;
5001 rtx insn;
5003 register enum rtx_code code;
5004 register enum machine_mode mode;
5005 register const char *fmt;
5006 register int i;
5007 rtx new = 0;
5008 int copied = 0;
5009 int must_swap = 0;
5011 /* Folded equivalents of first two operands of X. */
5012 rtx folded_arg0;
5013 rtx folded_arg1;
5015 /* Constant equivalents of first three operands of X;
5016 0 when no such equivalent is known. */
5017 rtx const_arg0;
5018 rtx const_arg1;
5019 rtx const_arg2;
5021 /* The mode of the first operand of X. We need this for sign and zero
5022 extends. */
5023 enum machine_mode mode_arg0;
5025 if (x == 0)
5026 return x;
5028 mode = GET_MODE (x);
5029 code = GET_CODE (x);
5030 switch (code)
5032 case CONST:
5033 case CONST_INT:
5034 case CONST_DOUBLE:
5035 case SYMBOL_REF:
5036 case LABEL_REF:
5037 case REG:
5038 /* No use simplifying an EXPR_LIST
5039 since they are used only for lists of args
5040 in a function call's REG_EQUAL note. */
5041 case EXPR_LIST:
5042 /* Changing anything inside an ADDRESSOF is incorrect; we don't
5043 want to (e.g.,) make (addressof (const_int 0)) just because
5044 the location is known to be zero. */
5045 case ADDRESSOF:
5046 return x;
5048 #ifdef HAVE_cc0
5049 case CC0:
5050 return prev_insn_cc0;
5051 #endif
5053 case PC:
5054 /* If the next insn is a CODE_LABEL followed by a jump table,
5055 PC's value is a LABEL_REF pointing to that label. That
5056 lets us fold switch statements on the Vax. */
5057 if (insn && GET_CODE (insn) == JUMP_INSN)
5059 rtx next = next_nonnote_insn (insn);
5061 if (next && GET_CODE (next) == CODE_LABEL
5062 && NEXT_INSN (next) != 0
5063 && GET_CODE (NEXT_INSN (next)) == JUMP_INSN
5064 && (GET_CODE (PATTERN (NEXT_INSN (next))) == ADDR_VEC
5065 || GET_CODE (PATTERN (NEXT_INSN (next))) == ADDR_DIFF_VEC))
5066 return gen_rtx_LABEL_REF (Pmode, next);
5068 break;
5070 case SUBREG:
5071 /* See if we previously assigned a constant value to this SUBREG. */
5072 if ((new = lookup_as_function (x, CONST_INT)) != 0
5073 || (new = lookup_as_function (x, CONST_DOUBLE)) != 0)
5074 return new;
5076 /* If this is a paradoxical SUBREG, we have no idea what value the
5077 extra bits would have. However, if the operand is equivalent
5078 to a SUBREG whose operand is the same as our mode, and all the
5079 modes are within a word, we can just use the inner operand
5080 because these SUBREGs just say how to treat the register.
5082 Similarly if we find an integer constant. */
5084 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
5086 enum machine_mode imode = GET_MODE (SUBREG_REG (x));
5087 struct table_elt *elt;
5089 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
5090 && GET_MODE_SIZE (imode) <= UNITS_PER_WORD
5091 && (elt = lookup (SUBREG_REG (x), HASH (SUBREG_REG (x), imode),
5092 imode)) != 0)
5093 for (elt = elt->first_same_value;
5094 elt; elt = elt->next_same_value)
5096 if (CONSTANT_P (elt->exp)
5097 && GET_MODE (elt->exp) == VOIDmode)
5098 return elt->exp;
5100 if (GET_CODE (elt->exp) == SUBREG
5101 && GET_MODE (SUBREG_REG (elt->exp)) == mode
5102 && exp_equiv_p (elt->exp, elt->exp, 1, 0))
5103 return copy_rtx (SUBREG_REG (elt->exp));
5106 return x;
5109 /* Fold SUBREG_REG. If it changed, see if we can simplify the SUBREG.
5110 We might be able to if the SUBREG is extracting a single word in an
5111 integral mode or extracting the low part. */
5113 folded_arg0 = fold_rtx (SUBREG_REG (x), insn);
5114 const_arg0 = equiv_constant (folded_arg0);
5115 if (const_arg0)
5116 folded_arg0 = const_arg0;
5118 if (folded_arg0 != SUBREG_REG (x))
5120 new = 0;
5122 if (GET_MODE_CLASS (mode) == MODE_INT
5123 && GET_MODE_SIZE (mode) == UNITS_PER_WORD
5124 && GET_MODE (SUBREG_REG (x)) != VOIDmode)
5125 new = operand_subword (folded_arg0, SUBREG_WORD (x), 0,
5126 GET_MODE (SUBREG_REG (x)));
5127 if (new == 0 && subreg_lowpart_p (x))
5128 new = gen_lowpart_if_possible (mode, folded_arg0);
5129 if (new)
5130 return new;
5133 /* If this is a narrowing SUBREG and our operand is a REG, see if
5134 we can find an equivalence for REG that is an arithmetic operation
5135 in a wider mode where both operands are paradoxical SUBREGs
5136 from objects of our result mode. In that case, we couldn't report
5137 an equivalent value for that operation, since we don't know what the
5138 extra bits will be. But we can find an equivalence for this SUBREG
5139 by folding that operation is the narrow mode. This allows us to
5140 fold arithmetic in narrow modes when the machine only supports
5141 word-sized arithmetic.
5143 Also look for a case where we have a SUBREG whose operand is the
5144 same as our result. If both modes are smaller than a word, we
5145 are simply interpreting a register in different modes and we
5146 can use the inner value. */
5148 if (GET_CODE (folded_arg0) == REG
5149 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (folded_arg0))
5150 && subreg_lowpart_p (x))
5152 struct table_elt *elt;
5154 /* We can use HASH here since we know that canon_hash won't be
5155 called. */
5156 elt = lookup (folded_arg0,
5157 HASH (folded_arg0, GET_MODE (folded_arg0)),
5158 GET_MODE (folded_arg0));
5160 if (elt)
5161 elt = elt->first_same_value;
5163 for (; elt; elt = elt->next_same_value)
5165 enum rtx_code eltcode = GET_CODE (elt->exp);
5167 /* Just check for unary and binary operations. */
5168 if (GET_RTX_CLASS (GET_CODE (elt->exp)) == '1'
5169 && GET_CODE (elt->exp) != SIGN_EXTEND
5170 && GET_CODE (elt->exp) != ZERO_EXTEND
5171 && GET_CODE (XEXP (elt->exp, 0)) == SUBREG
5172 && GET_MODE (SUBREG_REG (XEXP (elt->exp, 0))) == mode)
5174 rtx op0 = SUBREG_REG (XEXP (elt->exp, 0));
5176 if (GET_CODE (op0) != REG && ! CONSTANT_P (op0))
5177 op0 = fold_rtx (op0, NULL_RTX);
5179 op0 = equiv_constant (op0);
5180 if (op0)
5181 new = simplify_unary_operation (GET_CODE (elt->exp), mode,
5182 op0, mode);
5184 else if ((GET_RTX_CLASS (GET_CODE (elt->exp)) == '2'
5185 || GET_RTX_CLASS (GET_CODE (elt->exp)) == 'c')
5186 && eltcode != DIV && eltcode != MOD
5187 && eltcode != UDIV && eltcode != UMOD
5188 && eltcode != ASHIFTRT && eltcode != LSHIFTRT
5189 && eltcode != ROTATE && eltcode != ROTATERT
5190 && ((GET_CODE (XEXP (elt->exp, 0)) == SUBREG
5191 && (GET_MODE (SUBREG_REG (XEXP (elt->exp, 0)))
5192 == mode))
5193 || CONSTANT_P (XEXP (elt->exp, 0)))
5194 && ((GET_CODE (XEXP (elt->exp, 1)) == SUBREG
5195 && (GET_MODE (SUBREG_REG (XEXP (elt->exp, 1)))
5196 == mode))
5197 || CONSTANT_P (XEXP (elt->exp, 1))))
5199 rtx op0 = gen_lowpart_common (mode, XEXP (elt->exp, 0));
5200 rtx op1 = gen_lowpart_common (mode, XEXP (elt->exp, 1));
5202 if (op0 && GET_CODE (op0) != REG && ! CONSTANT_P (op0))
5203 op0 = fold_rtx (op0, NULL_RTX);
5205 if (op0)
5206 op0 = equiv_constant (op0);
5208 if (op1 && GET_CODE (op1) != REG && ! CONSTANT_P (op1))
5209 op1 = fold_rtx (op1, NULL_RTX);
5211 if (op1)
5212 op1 = equiv_constant (op1);
5214 /* If we are looking for the low SImode part of
5215 (ashift:DI c (const_int 32)), it doesn't work
5216 to compute that in SImode, because a 32-bit shift
5217 in SImode is unpredictable. We know the value is 0. */
5218 if (op0 && op1
5219 && GET_CODE (elt->exp) == ASHIFT
5220 && GET_CODE (op1) == CONST_INT
5221 && INTVAL (op1) >= GET_MODE_BITSIZE (mode))
5223 if (INTVAL (op1) < GET_MODE_BITSIZE (GET_MODE (elt->exp)))
5225 /* If the count fits in the inner mode's width,
5226 but exceeds the outer mode's width,
5227 the value will get truncated to 0
5228 by the subreg. */
5229 new = const0_rtx;
5230 else
5231 /* If the count exceeds even the inner mode's width,
5232 don't fold this expression. */
5233 new = 0;
5235 else if (op0 && op1)
5236 new = simplify_binary_operation (GET_CODE (elt->exp), mode,
5237 op0, op1);
5240 else if (GET_CODE (elt->exp) == SUBREG
5241 && GET_MODE (SUBREG_REG (elt->exp)) == mode
5242 && (GET_MODE_SIZE (GET_MODE (folded_arg0))
5243 <= UNITS_PER_WORD)
5244 && exp_equiv_p (elt->exp, elt->exp, 1, 0))
5245 new = copy_rtx (SUBREG_REG (elt->exp));
5247 if (new)
5248 return new;
5252 return x;
5254 case NOT:
5255 case NEG:
5256 /* If we have (NOT Y), see if Y is known to be (NOT Z).
5257 If so, (NOT Y) simplifies to Z. Similarly for NEG. */
5258 new = lookup_as_function (XEXP (x, 0), code);
5259 if (new)
5260 return fold_rtx (copy_rtx (XEXP (new, 0)), insn);
5261 break;
5263 case MEM:
5264 /* If we are not actually processing an insn, don't try to find the
5265 best address. Not only don't we care, but we could modify the
5266 MEM in an invalid way since we have no insn to validate against. */
5267 if (insn != 0)
5268 find_best_addr (insn, &XEXP (x, 0));
5271 /* Even if we don't fold in the insn itself,
5272 we can safely do so here, in hopes of getting a constant. */
5273 rtx addr = fold_rtx (XEXP (x, 0), NULL_RTX);
5274 rtx base = 0;
5275 HOST_WIDE_INT offset = 0;
5277 if (GET_CODE (addr) == REG
5278 && REGNO_QTY_VALID_P (REGNO (addr))
5279 && GET_MODE (addr) == qty_mode[REG_QTY (REGNO (addr))]
5280 && qty_const[REG_QTY (REGNO (addr))] != 0)
5281 addr = qty_const[REG_QTY (REGNO (addr))];
5283 /* If address is constant, split it into a base and integer offset. */
5284 if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == LABEL_REF)
5285 base = addr;
5286 else if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS
5287 && GET_CODE (XEXP (XEXP (addr, 0), 1)) == CONST_INT)
5289 base = XEXP (XEXP (addr, 0), 0);
5290 offset = INTVAL (XEXP (XEXP (addr, 0), 1));
5292 else if (GET_CODE (addr) == LO_SUM
5293 && GET_CODE (XEXP (addr, 1)) == SYMBOL_REF)
5294 base = XEXP (addr, 1);
5295 else if (GET_CODE (addr) == ADDRESSOF)
5296 return change_address (x, VOIDmode, addr);
5298 /* If this is a constant pool reference, we can fold it into its
5299 constant to allow better value tracking. */
5300 if (base && GET_CODE (base) == SYMBOL_REF
5301 && CONSTANT_POOL_ADDRESS_P (base))
5303 rtx constant = get_pool_constant (base);
5304 enum machine_mode const_mode = get_pool_mode (base);
5305 rtx new;
5307 if (CONSTANT_P (constant) && GET_CODE (constant) != CONST_INT)
5308 constant_pool_entries_cost = COST (constant);
5310 /* If we are loading the full constant, we have an equivalence. */
5311 if (offset == 0 && mode == const_mode)
5312 return constant;
5314 /* If this actually isn't a constant (weird!), we can't do
5315 anything. Otherwise, handle the two most common cases:
5316 extracting a word from a multi-word constant, and extracting
5317 the low-order bits. Other cases don't seem common enough to
5318 worry about. */
5319 if (! CONSTANT_P (constant))
5320 return x;
5322 if (GET_MODE_CLASS (mode) == MODE_INT
5323 && GET_MODE_SIZE (mode) == UNITS_PER_WORD
5324 && offset % UNITS_PER_WORD == 0
5325 && (new = operand_subword (constant,
5326 offset / UNITS_PER_WORD,
5327 0, const_mode)) != 0)
5328 return new;
5330 if (((BYTES_BIG_ENDIAN
5331 && offset == GET_MODE_SIZE (GET_MODE (constant)) - 1)
5332 || (! BYTES_BIG_ENDIAN && offset == 0))
5333 && (new = gen_lowpart_if_possible (mode, constant)) != 0)
5334 return new;
5337 /* If this is a reference to a label at a known position in a jump
5338 table, we also know its value. */
5339 if (base && GET_CODE (base) == LABEL_REF)
5341 rtx label = XEXP (base, 0);
5342 rtx table_insn = NEXT_INSN (label);
5344 if (table_insn && GET_CODE (table_insn) == JUMP_INSN
5345 && GET_CODE (PATTERN (table_insn)) == ADDR_VEC)
5347 rtx table = PATTERN (table_insn);
5349 if (offset >= 0
5350 && (offset / GET_MODE_SIZE (GET_MODE (table))
5351 < XVECLEN (table, 0)))
5352 return XVECEXP (table, 0,
5353 offset / GET_MODE_SIZE (GET_MODE (table)));
5355 if (table_insn && GET_CODE (table_insn) == JUMP_INSN
5356 && GET_CODE (PATTERN (table_insn)) == ADDR_DIFF_VEC)
5358 rtx table = PATTERN (table_insn);
5360 if (offset >= 0
5361 && (offset / GET_MODE_SIZE (GET_MODE (table))
5362 < XVECLEN (table, 1)))
5364 offset /= GET_MODE_SIZE (GET_MODE (table));
5365 new = gen_rtx_MINUS (Pmode, XVECEXP (table, 1, offset),
5366 XEXP (table, 0));
5368 if (GET_MODE (table) != Pmode)
5369 new = gen_rtx_TRUNCATE (GET_MODE (table), new);
5371 /* Indicate this is a constant. This isn't a
5372 valid form of CONST, but it will only be used
5373 to fold the next insns and then discarded, so
5374 it should be safe.
5376 Note this expression must be explicitly discarded,
5377 by cse_insn, else it may end up in a REG_EQUAL note
5378 and "escape" to cause problems elsewhere. */
5379 return gen_rtx_CONST (GET_MODE (new), new);
5384 return x;
5387 case ASM_OPERANDS:
5388 for (i = XVECLEN (x, 3) - 1; i >= 0; i--)
5389 validate_change (insn, &XVECEXP (x, 3, i),
5390 fold_rtx (XVECEXP (x, 3, i), insn), 0);
5391 break;
5393 default:
5394 break;
5397 const_arg0 = 0;
5398 const_arg1 = 0;
5399 const_arg2 = 0;
5400 mode_arg0 = VOIDmode;
5402 /* Try folding our operands.
5403 Then see which ones have constant values known. */
5405 fmt = GET_RTX_FORMAT (code);
5406 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5407 if (fmt[i] == 'e')
5409 rtx arg = XEXP (x, i);
5410 rtx folded_arg = arg, const_arg = 0;
5411 enum machine_mode mode_arg = GET_MODE (arg);
5412 rtx cheap_arg, expensive_arg;
5413 rtx replacements[2];
5414 int j;
5416 /* Most arguments are cheap, so handle them specially. */
5417 switch (GET_CODE (arg))
5419 case REG:
5420 /* This is the same as calling equiv_constant; it is duplicated
5421 here for speed. */
5422 if (REGNO_QTY_VALID_P (REGNO (arg))
5423 && qty_const[REG_QTY (REGNO (arg))] != 0
5424 && GET_CODE (qty_const[REG_QTY (REGNO (arg))]) != REG
5425 && GET_CODE (qty_const[REG_QTY (REGNO (arg))]) != PLUS)
5426 const_arg
5427 = gen_lowpart_if_possible (GET_MODE (arg),
5428 qty_const[REG_QTY (REGNO (arg))]);
5429 break;
5431 case CONST:
5432 case CONST_INT:
5433 case SYMBOL_REF:
5434 case LABEL_REF:
5435 case CONST_DOUBLE:
5436 const_arg = arg;
5437 break;
5439 #ifdef HAVE_cc0
5440 case CC0:
5441 folded_arg = prev_insn_cc0;
5442 mode_arg = prev_insn_cc0_mode;
5443 const_arg = equiv_constant (folded_arg);
5444 break;
5445 #endif
5447 default:
5448 folded_arg = fold_rtx (arg, insn);
5449 const_arg = equiv_constant (folded_arg);
5452 /* For the first three operands, see if the operand
5453 is constant or equivalent to a constant. */
5454 switch (i)
5456 case 0:
5457 folded_arg0 = folded_arg;
5458 const_arg0 = const_arg;
5459 mode_arg0 = mode_arg;
5460 break;
5461 case 1:
5462 folded_arg1 = folded_arg;
5463 const_arg1 = const_arg;
5464 break;
5465 case 2:
5466 const_arg2 = const_arg;
5467 break;
5470 /* Pick the least expensive of the folded argument and an
5471 equivalent constant argument. */
5472 if (const_arg == 0 || const_arg == folded_arg
5473 || COST (const_arg) > COST (folded_arg))
5474 cheap_arg = folded_arg, expensive_arg = const_arg;
5475 else
5476 cheap_arg = const_arg, expensive_arg = folded_arg;
5478 /* Try to replace the operand with the cheapest of the two
5479 possibilities. If it doesn't work and this is either of the first
5480 two operands of a commutative operation, try swapping them.
5481 If THAT fails, try the more expensive, provided it is cheaper
5482 than what is already there. */
5484 if (cheap_arg == XEXP (x, i))
5485 continue;
5487 if (insn == 0 && ! copied)
5489 x = copy_rtx (x);
5490 copied = 1;
5493 replacements[0] = cheap_arg, replacements[1] = expensive_arg;
5494 for (j = 0;
5495 j < 2 && replacements[j]
5496 && COST (replacements[j]) < COST (XEXP (x, i));
5497 j++)
5499 if (validate_change (insn, &XEXP (x, i), replacements[j], 0))
5500 break;
5502 if (code == NE || code == EQ || GET_RTX_CLASS (code) == 'c')
5504 validate_change (insn, &XEXP (x, i), XEXP (x, 1 - i), 1);
5505 validate_change (insn, &XEXP (x, 1 - i), replacements[j], 1);
5507 if (apply_change_group ())
5509 /* Swap them back to be invalid so that this loop can
5510 continue and flag them to be swapped back later. */
5511 rtx tem;
5513 tem = XEXP (x, 0); XEXP (x, 0) = XEXP (x, 1);
5514 XEXP (x, 1) = tem;
5515 must_swap = 1;
5516 break;
5522 else
5524 if (fmt[i] == 'E')
5525 /* Don't try to fold inside of a vector of expressions.
5526 Doing nothing is harmless. */
5527 {;}
5530 /* If a commutative operation, place a constant integer as the second
5531 operand unless the first operand is also a constant integer. Otherwise,
5532 place any constant second unless the first operand is also a constant. */
5534 if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
5536 if (must_swap || (const_arg0
5537 && (const_arg1 == 0
5538 || (GET_CODE (const_arg0) == CONST_INT
5539 && GET_CODE (const_arg1) != CONST_INT))))
5541 register rtx tem = XEXP (x, 0);
5543 if (insn == 0 && ! copied)
5545 x = copy_rtx (x);
5546 copied = 1;
5549 validate_change (insn, &XEXP (x, 0), XEXP (x, 1), 1);
5550 validate_change (insn, &XEXP (x, 1), tem, 1);
5551 if (apply_change_group ())
5553 tem = const_arg0, const_arg0 = const_arg1, const_arg1 = tem;
5554 tem = folded_arg0, folded_arg0 = folded_arg1, folded_arg1 = tem;
5559 /* If X is an arithmetic operation, see if we can simplify it. */
5561 switch (GET_RTX_CLASS (code))
5563 case '1':
5565 int is_const = 0;
5567 /* We can't simplify extension ops unless we know the
5568 original mode. */
5569 if ((code == ZERO_EXTEND || code == SIGN_EXTEND)
5570 && mode_arg0 == VOIDmode)
5571 break;
5573 /* If we had a CONST, strip it off and put it back later if we
5574 fold. */
5575 if (const_arg0 != 0 && GET_CODE (const_arg0) == CONST)
5576 is_const = 1, const_arg0 = XEXP (const_arg0, 0);
5578 new = simplify_unary_operation (code, mode,
5579 const_arg0 ? const_arg0 : folded_arg0,
5580 mode_arg0);
5581 if (new != 0 && is_const)
5582 new = gen_rtx_CONST (mode, new);
5584 break;
5586 case '<':
5587 /* See what items are actually being compared and set FOLDED_ARG[01]
5588 to those values and CODE to the actual comparison code. If any are
5589 constant, set CONST_ARG0 and CONST_ARG1 appropriately. We needn't
5590 do anything if both operands are already known to be constant. */
5592 if (const_arg0 == 0 || const_arg1 == 0)
5594 struct table_elt *p0, *p1;
5595 rtx true = const_true_rtx, false = const0_rtx;
5596 enum machine_mode mode_arg1;
5598 #ifdef FLOAT_STORE_FLAG_VALUE
5599 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5601 true = CONST_DOUBLE_FROM_REAL_VALUE (FLOAT_STORE_FLAG_VALUE,
5602 mode);
5603 false = CONST0_RTX (mode);
5605 #endif
5607 code = find_comparison_args (code, &folded_arg0, &folded_arg1,
5608 &mode_arg0, &mode_arg1);
5609 const_arg0 = equiv_constant (folded_arg0);
5610 const_arg1 = equiv_constant (folded_arg1);
5612 /* If the mode is VOIDmode or a MODE_CC mode, we don't know
5613 what kinds of things are being compared, so we can't do
5614 anything with this comparison. */
5616 if (mode_arg0 == VOIDmode || GET_MODE_CLASS (mode_arg0) == MODE_CC)
5617 break;
5619 /* If we do not now have two constants being compared, see
5620 if we can nevertheless deduce some things about the
5621 comparison. */
5622 if (const_arg0 == 0 || const_arg1 == 0)
5624 /* Is FOLDED_ARG0 frame-pointer plus a constant? Or
5625 non-explicit constant? These aren't zero, but we
5626 don't know their sign. */
5627 if (const_arg1 == const0_rtx
5628 && (NONZERO_BASE_PLUS_P (folded_arg0)
5629 #if 0 /* Sad to say, on sysvr4, #pragma weak can make a symbol address
5630 come out as 0. */
5631 || GET_CODE (folded_arg0) == SYMBOL_REF
5632 #endif
5633 || GET_CODE (folded_arg0) == LABEL_REF
5634 || GET_CODE (folded_arg0) == CONST))
5636 if (code == EQ)
5637 return false;
5638 else if (code == NE)
5639 return true;
5642 /* See if the two operands are the same. We don't do this
5643 for IEEE floating-point since we can't assume x == x
5644 since x might be a NaN. */
5646 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5647 || ! FLOAT_MODE_P (mode_arg0) || flag_fast_math)
5648 && (folded_arg0 == folded_arg1
5649 || (GET_CODE (folded_arg0) == REG
5650 && GET_CODE (folded_arg1) == REG
5651 && (REG_QTY (REGNO (folded_arg0))
5652 == REG_QTY (REGNO (folded_arg1))))
5653 || ((p0 = lookup (folded_arg0,
5654 (safe_hash (folded_arg0, mode_arg0)
5655 % NBUCKETS), mode_arg0))
5656 && (p1 = lookup (folded_arg1,
5657 (safe_hash (folded_arg1, mode_arg0)
5658 % NBUCKETS), mode_arg0))
5659 && p0->first_same_value == p1->first_same_value)))
5660 return ((code == EQ || code == LE || code == GE
5661 || code == LEU || code == GEU)
5662 ? true : false);
5664 /* If FOLDED_ARG0 is a register, see if the comparison we are
5665 doing now is either the same as we did before or the reverse
5666 (we only check the reverse if not floating-point). */
5667 else if (GET_CODE (folded_arg0) == REG)
5669 int qty = REG_QTY (REGNO (folded_arg0));
5671 if (REGNO_QTY_VALID_P (REGNO (folded_arg0))
5672 && (comparison_dominates_p (qty_comparison_code[qty], code)
5673 || (comparison_dominates_p (qty_comparison_code[qty],
5674 reverse_condition (code))
5675 && ! FLOAT_MODE_P (mode_arg0)))
5676 && (rtx_equal_p (qty_comparison_const[qty], folded_arg1)
5677 || (const_arg1
5678 && rtx_equal_p (qty_comparison_const[qty],
5679 const_arg1))
5680 || (GET_CODE (folded_arg1) == REG
5681 && (REG_QTY (REGNO (folded_arg1))
5682 == qty_comparison_qty[qty]))))
5683 return (comparison_dominates_p (qty_comparison_code[qty],
5684 code)
5685 ? true : false);
5690 /* If we are comparing against zero, see if the first operand is
5691 equivalent to an IOR with a constant. If so, we may be able to
5692 determine the result of this comparison. */
5694 if (const_arg1 == const0_rtx)
5696 rtx y = lookup_as_function (folded_arg0, IOR);
5697 rtx inner_const;
5699 if (y != 0
5700 && (inner_const = equiv_constant (XEXP (y, 1))) != 0
5701 && GET_CODE (inner_const) == CONST_INT
5702 && INTVAL (inner_const) != 0)
5704 int sign_bitnum = GET_MODE_BITSIZE (mode_arg0) - 1;
5705 int has_sign = (HOST_BITS_PER_WIDE_INT >= sign_bitnum
5706 && (INTVAL (inner_const)
5707 & ((HOST_WIDE_INT) 1 << sign_bitnum)));
5708 rtx true = const_true_rtx, false = const0_rtx;
5710 #ifdef FLOAT_STORE_FLAG_VALUE
5711 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5713 true = CONST_DOUBLE_FROM_REAL_VALUE (FLOAT_STORE_FLAG_VALUE,
5714 mode);
5715 false = CONST0_RTX (mode);
5717 #endif
5719 switch (code)
5721 case EQ:
5722 return false;
5723 case NE:
5724 return true;
5725 case LT: case LE:
5726 if (has_sign)
5727 return true;
5728 break;
5729 case GT: case GE:
5730 if (has_sign)
5731 return false;
5732 break;
5733 default:
5734 break;
5739 new = simplify_relational_operation (code, mode_arg0,
5740 const_arg0 ? const_arg0 : folded_arg0,
5741 const_arg1 ? const_arg1 : folded_arg1);
5742 #ifdef FLOAT_STORE_FLAG_VALUE
5743 if (new != 0 && GET_MODE_CLASS (mode) == MODE_FLOAT)
5744 new = ((new == const0_rtx) ? CONST0_RTX (mode)
5745 : CONST_DOUBLE_FROM_REAL_VALUE (FLOAT_STORE_FLAG_VALUE, mode));
5746 #endif
5747 break;
5749 case '2':
5750 case 'c':
5751 switch (code)
5753 case PLUS:
5754 /* If the second operand is a LABEL_REF, see if the first is a MINUS
5755 with that LABEL_REF as its second operand. If so, the result is
5756 the first operand of that MINUS. This handles switches with an
5757 ADDR_DIFF_VEC table. */
5758 if (const_arg1 && GET_CODE (const_arg1) == LABEL_REF)
5760 rtx y
5761 = GET_CODE (folded_arg0) == MINUS ? folded_arg0
5762 : lookup_as_function (folded_arg0, MINUS);
5764 if (y != 0 && GET_CODE (XEXP (y, 1)) == LABEL_REF
5765 && XEXP (XEXP (y, 1), 0) == XEXP (const_arg1, 0))
5766 return XEXP (y, 0);
5768 /* Now try for a CONST of a MINUS like the above. */
5769 if ((y = (GET_CODE (folded_arg0) == CONST ? folded_arg0
5770 : lookup_as_function (folded_arg0, CONST))) != 0
5771 && GET_CODE (XEXP (y, 0)) == MINUS
5772 && GET_CODE (XEXP (XEXP (y, 0), 1)) == LABEL_REF
5773 && XEXP (XEXP (XEXP (y, 0),1), 0) == XEXP (const_arg1, 0))
5774 return XEXP (XEXP (y, 0), 0);
5777 /* Likewise if the operands are in the other order. */
5778 if (const_arg0 && GET_CODE (const_arg0) == LABEL_REF)
5780 rtx y
5781 = GET_CODE (folded_arg1) == MINUS ? folded_arg1
5782 : lookup_as_function (folded_arg1, MINUS);
5784 if (y != 0 && GET_CODE (XEXP (y, 1)) == LABEL_REF
5785 && XEXP (XEXP (y, 1), 0) == XEXP (const_arg0, 0))
5786 return XEXP (y, 0);
5788 /* Now try for a CONST of a MINUS like the above. */
5789 if ((y = (GET_CODE (folded_arg1) == CONST ? folded_arg1
5790 : lookup_as_function (folded_arg1, CONST))) != 0
5791 && GET_CODE (XEXP (y, 0)) == MINUS
5792 && GET_CODE (XEXP (XEXP (y, 0), 1)) == LABEL_REF
5793 && XEXP (XEXP (XEXP (y, 0),1), 0) == XEXP (const_arg0, 0))
5794 return XEXP (XEXP (y, 0), 0);
5797 /* If second operand is a register equivalent to a negative
5798 CONST_INT, see if we can find a register equivalent to the
5799 positive constant. Make a MINUS if so. Don't do this for
5800 a non-negative constant since we might then alternate between
5801 chosing positive and negative constants. Having the positive
5802 constant previously-used is the more common case. Be sure
5803 the resulting constant is non-negative; if const_arg1 were
5804 the smallest negative number this would overflow: depending
5805 on the mode, this would either just be the same value (and
5806 hence not save anything) or be incorrect. */
5807 if (const_arg1 != 0 && GET_CODE (const_arg1) == CONST_INT
5808 && INTVAL (const_arg1) < 0
5809 /* This used to test
5811 - INTVAL (const_arg1) >= 0
5813 But The Sun V5.0 compilers mis-compiled that test. So
5814 instead we test for the problematic value in a more direct
5815 manner and hope the Sun compilers get it correct. */
5816 && INTVAL (const_arg1) !=
5817 ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1))
5818 && GET_CODE (folded_arg1) == REG)
5820 rtx new_const = GEN_INT (- INTVAL (const_arg1));
5821 struct table_elt *p
5822 = lookup (new_const, safe_hash (new_const, mode) % NBUCKETS,
5823 mode);
5825 if (p)
5826 for (p = p->first_same_value; p; p = p->next_same_value)
5827 if (GET_CODE (p->exp) == REG)
5828 return cse_gen_binary (MINUS, mode, folded_arg0,
5829 canon_reg (p->exp, NULL_RTX));
5831 goto from_plus;
5833 case MINUS:
5834 /* If we have (MINUS Y C), see if Y is known to be (PLUS Z C2).
5835 If so, produce (PLUS Z C2-C). */
5836 if (const_arg1 != 0 && GET_CODE (const_arg1) == CONST_INT)
5838 rtx y = lookup_as_function (XEXP (x, 0), PLUS);
5839 if (y && GET_CODE (XEXP (y, 1)) == CONST_INT)
5840 return fold_rtx (plus_constant (copy_rtx (y),
5841 -INTVAL (const_arg1)),
5842 NULL_RTX);
5845 /* ... fall through ... */
5847 from_plus:
5848 case SMIN: case SMAX: case UMIN: case UMAX:
5849 case IOR: case AND: case XOR:
5850 case MULT: case DIV: case UDIV:
5851 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
5852 /* If we have (<op> <reg> <const_int>) for an associative OP and REG
5853 is known to be of similar form, we may be able to replace the
5854 operation with a combined operation. This may eliminate the
5855 intermediate operation if every use is simplified in this way.
5856 Note that the similar optimization done by combine.c only works
5857 if the intermediate operation's result has only one reference. */
5859 if (GET_CODE (folded_arg0) == REG
5860 && const_arg1 && GET_CODE (const_arg1) == CONST_INT)
5862 int is_shift
5863 = (code == ASHIFT || code == ASHIFTRT || code == LSHIFTRT);
5864 rtx y = lookup_as_function (folded_arg0, code);
5865 rtx inner_const;
5866 enum rtx_code associate_code;
5867 rtx new_const;
5869 if (y == 0
5870 || 0 == (inner_const
5871 = equiv_constant (fold_rtx (XEXP (y, 1), 0)))
5872 || GET_CODE (inner_const) != CONST_INT
5873 /* If we have compiled a statement like
5874 "if (x == (x & mask1))", and now are looking at
5875 "x & mask2", we will have a case where the first operand
5876 of Y is the same as our first operand. Unless we detect
5877 this case, an infinite loop will result. */
5878 || XEXP (y, 0) == folded_arg0)
5879 break;
5881 /* Don't associate these operations if they are a PLUS with the
5882 same constant and it is a power of two. These might be doable
5883 with a pre- or post-increment. Similarly for two subtracts of
5884 identical powers of two with post decrement. */
5886 if (code == PLUS && INTVAL (const_arg1) == INTVAL (inner_const)
5887 && ((HAVE_PRE_INCREMENT
5888 && exact_log2 (INTVAL (const_arg1)) >= 0)
5889 || (HAVE_POST_INCREMENT
5890 && exact_log2 (INTVAL (const_arg1)) >= 0)
5891 || (HAVE_PRE_DECREMENT
5892 && exact_log2 (- INTVAL (const_arg1)) >= 0)
5893 || (HAVE_POST_DECREMENT
5894 && exact_log2 (- INTVAL (const_arg1)) >= 0)))
5895 break;
5897 /* Compute the code used to compose the constants. For example,
5898 A/C1/C2 is A/(C1 * C2), so if CODE == DIV, we want MULT. */
5900 associate_code
5901 = (code == MULT || code == DIV || code == UDIV ? MULT
5902 : is_shift || code == PLUS || code == MINUS ? PLUS : code);
5904 new_const = simplify_binary_operation (associate_code, mode,
5905 const_arg1, inner_const);
5907 if (new_const == 0)
5908 break;
5910 /* If we are associating shift operations, don't let this
5911 produce a shift of the size of the object or larger.
5912 This could occur when we follow a sign-extend by a right
5913 shift on a machine that does a sign-extend as a pair
5914 of shifts. */
5916 if (is_shift && GET_CODE (new_const) == CONST_INT
5917 && INTVAL (new_const) >= GET_MODE_BITSIZE (mode))
5919 /* As an exception, we can turn an ASHIFTRT of this
5920 form into a shift of the number of bits - 1. */
5921 if (code == ASHIFTRT)
5922 new_const = GEN_INT (GET_MODE_BITSIZE (mode) - 1);
5923 else
5924 break;
5927 y = copy_rtx (XEXP (y, 0));
5929 /* If Y contains our first operand (the most common way this
5930 can happen is if Y is a MEM), we would do into an infinite
5931 loop if we tried to fold it. So don't in that case. */
5933 if (! reg_mentioned_p (folded_arg0, y))
5934 y = fold_rtx (y, insn);
5936 return cse_gen_binary (code, mode, y, new_const);
5938 break;
5940 default:
5941 break;
5944 new = simplify_binary_operation (code, mode,
5945 const_arg0 ? const_arg0 : folded_arg0,
5946 const_arg1 ? const_arg1 : folded_arg1);
5947 break;
5949 case 'o':
5950 /* (lo_sum (high X) X) is simply X. */
5951 if (code == LO_SUM && const_arg0 != 0
5952 && GET_CODE (const_arg0) == HIGH
5953 && rtx_equal_p (XEXP (const_arg0, 0), const_arg1))
5954 return const_arg1;
5955 break;
5957 case '3':
5958 case 'b':
5959 new = simplify_ternary_operation (code, mode, mode_arg0,
5960 const_arg0 ? const_arg0 : folded_arg0,
5961 const_arg1 ? const_arg1 : folded_arg1,
5962 const_arg2 ? const_arg2 : XEXP (x, 2));
5963 break;
5965 case 'x':
5966 /* Always eliminate CONSTANT_P_RTX at this stage. */
5967 if (code == CONSTANT_P_RTX)
5968 return (const_arg0 ? const1_rtx : const0_rtx);
5969 break;
5972 return new ? new : x;
5975 /* Return a constant value currently equivalent to X.
5976 Return 0 if we don't know one. */
5978 static rtx
5979 equiv_constant (x)
5980 rtx x;
5982 if (GET_CODE (x) == REG
5983 && REGNO_QTY_VALID_P (REGNO (x))
5984 && qty_const[REG_QTY (REGNO (x))])
5985 x = gen_lowpart_if_possible (GET_MODE (x), qty_const[REG_QTY (REGNO (x))]);
5987 if (x == 0 || CONSTANT_P (x))
5988 return x;
5990 /* If X is a MEM, try to fold it outside the context of any insn to see if
5991 it might be equivalent to a constant. That handles the case where it
5992 is a constant-pool reference. Then try to look it up in the hash table
5993 in case it is something whose value we have seen before. */
5995 if (GET_CODE (x) == MEM)
5997 struct table_elt *elt;
5999 x = fold_rtx (x, NULL_RTX);
6000 if (CONSTANT_P (x))
6001 return x;
6003 elt = lookup (x, safe_hash (x, GET_MODE (x)) % NBUCKETS, GET_MODE (x));
6004 if (elt == 0)
6005 return 0;
6007 for (elt = elt->first_same_value; elt; elt = elt->next_same_value)
6008 if (elt->is_const && CONSTANT_P (elt->exp))
6009 return elt->exp;
6012 return 0;
6015 /* Assuming that X is an rtx (e.g., MEM, REG or SUBREG) for a fixed-point
6016 number, return an rtx (MEM, SUBREG, or CONST_INT) that refers to the
6017 least-significant part of X.
6018 MODE specifies how big a part of X to return.
6020 If the requested operation cannot be done, 0 is returned.
6022 This is similar to gen_lowpart in emit-rtl.c. */
6025 gen_lowpart_if_possible (mode, x)
6026 enum machine_mode mode;
6027 register rtx x;
6029 rtx result = gen_lowpart_common (mode, x);
6031 if (result)
6032 return result;
6033 else if (GET_CODE (x) == MEM)
6035 /* This is the only other case we handle. */
6036 register int offset = 0;
6037 rtx new;
6039 if (WORDS_BIG_ENDIAN)
6040 offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
6041 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
6042 if (BYTES_BIG_ENDIAN)
6043 /* Adjust the address so that the address-after-the-data is
6044 unchanged. */
6045 offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
6046 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
6047 new = gen_rtx_MEM (mode, plus_constant (XEXP (x, 0), offset));
6048 if (! memory_address_p (mode, XEXP (new, 0)))
6049 return 0;
6050 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x);
6051 MEM_COPY_ATTRIBUTES (new, x);
6052 return new;
6054 else
6055 return 0;
6058 /* Given INSN, a jump insn, TAKEN indicates if we are following the "taken"
6059 branch. It will be zero if not.
6061 In certain cases, this can cause us to add an equivalence. For example,
6062 if we are following the taken case of
6063 if (i == 2)
6064 we can add the fact that `i' and '2' are now equivalent.
6066 In any case, we can record that this comparison was passed. If the same
6067 comparison is seen later, we will know its value. */
6069 static void
6070 record_jump_equiv (insn, taken)
6071 rtx insn;
6072 int taken;
6074 int cond_known_true;
6075 rtx op0, op1;
6076 enum machine_mode mode, mode0, mode1;
6077 int reversed_nonequality = 0;
6078 enum rtx_code code;
6080 /* Ensure this is the right kind of insn. */
6081 if (! condjump_p (insn) || simplejump_p (insn))
6082 return;
6084 /* See if this jump condition is known true or false. */
6085 if (taken)
6086 cond_known_true = (XEXP (SET_SRC (PATTERN (insn)), 2) == pc_rtx);
6087 else
6088 cond_known_true = (XEXP (SET_SRC (PATTERN (insn)), 1) == pc_rtx);
6090 /* Get the type of comparison being done and the operands being compared.
6091 If we had to reverse a non-equality condition, record that fact so we
6092 know that it isn't valid for floating-point. */
6093 code = GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 0));
6094 op0 = fold_rtx (XEXP (XEXP (SET_SRC (PATTERN (insn)), 0), 0), insn);
6095 op1 = fold_rtx (XEXP (XEXP (SET_SRC (PATTERN (insn)), 0), 1), insn);
6097 code = find_comparison_args (code, &op0, &op1, &mode0, &mode1);
6098 if (! cond_known_true)
6100 reversed_nonequality = (code != EQ && code != NE);
6101 code = reverse_condition (code);
6104 /* The mode is the mode of the non-constant. */
6105 mode = mode0;
6106 if (mode1 != VOIDmode)
6107 mode = mode1;
6109 record_jump_cond (code, mode, op0, op1, reversed_nonequality);
6112 /* We know that comparison CODE applied to OP0 and OP1 in MODE is true.
6113 REVERSED_NONEQUALITY is nonzero if CODE had to be swapped.
6114 Make any useful entries we can with that information. Called from
6115 above function and called recursively. */
6117 static void
6118 record_jump_cond (code, mode, op0, op1, reversed_nonequality)
6119 enum rtx_code code;
6120 enum machine_mode mode;
6121 rtx op0, op1;
6122 int reversed_nonequality;
6124 unsigned op0_hash, op1_hash;
6125 int op0_in_memory, op0_in_struct, op1_in_memory, op1_in_struct;
6126 struct table_elt *op0_elt, *op1_elt;
6128 /* If OP0 and OP1 are known equal, and either is a paradoxical SUBREG,
6129 we know that they are also equal in the smaller mode (this is also
6130 true for all smaller modes whether or not there is a SUBREG, but
6131 is not worth testing for with no SUBREG). */
6133 /* Note that GET_MODE (op0) may not equal MODE. */
6134 if (code == EQ && GET_CODE (op0) == SUBREG
6135 && (GET_MODE_SIZE (GET_MODE (op0))
6136 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0)))))
6138 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
6139 rtx tem = gen_lowpart_if_possible (inner_mode, op1);
6141 record_jump_cond (code, mode, SUBREG_REG (op0),
6142 tem ? tem : gen_rtx_SUBREG (inner_mode, op1, 0),
6143 reversed_nonequality);
6146 if (code == EQ && GET_CODE (op1) == SUBREG
6147 && (GET_MODE_SIZE (GET_MODE (op1))
6148 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op1)))))
6150 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op1));
6151 rtx tem = gen_lowpart_if_possible (inner_mode, op0);
6153 record_jump_cond (code, mode, SUBREG_REG (op1),
6154 tem ? tem : gen_rtx_SUBREG (inner_mode, op0, 0),
6155 reversed_nonequality);
6158 /* Similarly, if this is an NE comparison, and either is a SUBREG
6159 making a smaller mode, we know the whole thing is also NE. */
6161 /* Note that GET_MODE (op0) may not equal MODE;
6162 if we test MODE instead, we can get an infinite recursion
6163 alternating between two modes each wider than MODE. */
6165 if (code == NE && GET_CODE (op0) == SUBREG
6166 && subreg_lowpart_p (op0)
6167 && (GET_MODE_SIZE (GET_MODE (op0))
6168 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0)))))
6170 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
6171 rtx tem = gen_lowpart_if_possible (inner_mode, op1);
6173 record_jump_cond (code, mode, SUBREG_REG (op0),
6174 tem ? tem : gen_rtx_SUBREG (inner_mode, op1, 0),
6175 reversed_nonequality);
6178 if (code == NE && GET_CODE (op1) == SUBREG
6179 && subreg_lowpart_p (op1)
6180 && (GET_MODE_SIZE (GET_MODE (op1))
6181 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (op1)))))
6183 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op1));
6184 rtx tem = gen_lowpart_if_possible (inner_mode, op0);
6186 record_jump_cond (code, mode, SUBREG_REG (op1),
6187 tem ? tem : gen_rtx_SUBREG (inner_mode, op0, 0),
6188 reversed_nonequality);
6191 /* Hash both operands. */
6193 do_not_record = 0;
6194 hash_arg_in_memory = 0;
6195 hash_arg_in_struct = 0;
6196 op0_hash = HASH (op0, mode);
6197 op0_in_memory = hash_arg_in_memory;
6198 op0_in_struct = hash_arg_in_struct;
6200 if (do_not_record)
6201 return;
6203 do_not_record = 0;
6204 hash_arg_in_memory = 0;
6205 hash_arg_in_struct = 0;
6206 op1_hash = HASH (op1, mode);
6207 op1_in_memory = hash_arg_in_memory;
6208 op1_in_struct = hash_arg_in_struct;
6210 if (do_not_record)
6211 return;
6213 /* Look up both operands. */
6214 op0_elt = lookup (op0, op0_hash, mode);
6215 op1_elt = lookup (op1, op1_hash, mode);
6217 /* If both operands are already equivalent or if they are not in the
6218 table but are identical, do nothing. */
6219 if ((op0_elt != 0 && op1_elt != 0
6220 && op0_elt->first_same_value == op1_elt->first_same_value)
6221 || op0 == op1 || rtx_equal_p (op0, op1))
6222 return;
6224 /* If we aren't setting two things equal all we can do is save this
6225 comparison. Similarly if this is floating-point. In the latter
6226 case, OP1 might be zero and both -0.0 and 0.0 are equal to it.
6227 If we record the equality, we might inadvertently delete code
6228 whose intent was to change -0 to +0. */
6230 if (code != EQ || FLOAT_MODE_P (GET_MODE (op0)))
6232 /* If we reversed a floating-point comparison, if OP0 is not a
6233 register, or if OP1 is neither a register or constant, we can't
6234 do anything. */
6236 if (GET_CODE (op1) != REG)
6237 op1 = equiv_constant (op1);
6239 if ((reversed_nonequality && FLOAT_MODE_P (mode))
6240 || GET_CODE (op0) != REG || op1 == 0)
6241 return;
6243 /* Put OP0 in the hash table if it isn't already. This gives it a
6244 new quantity number. */
6245 if (op0_elt == 0)
6247 if (insert_regs (op0, NULL_PTR, 0))
6249 rehash_using_reg (op0);
6250 op0_hash = HASH (op0, mode);
6252 /* If OP0 is contained in OP1, this changes its hash code
6253 as well. Faster to rehash than to check, except
6254 for the simple case of a constant. */
6255 if (! CONSTANT_P (op1))
6256 op1_hash = HASH (op1,mode);
6259 op0_elt = insert (op0, NULL_PTR, op0_hash, mode);
6260 op0_elt->in_memory = op0_in_memory;
6261 op0_elt->in_struct = op0_in_struct;
6264 qty_comparison_code[REG_QTY (REGNO (op0))] = code;
6265 if (GET_CODE (op1) == REG)
6267 /* Look it up again--in case op0 and op1 are the same. */
6268 op1_elt = lookup (op1, op1_hash, mode);
6270 /* Put OP1 in the hash table so it gets a new quantity number. */
6271 if (op1_elt == 0)
6273 if (insert_regs (op1, NULL_PTR, 0))
6275 rehash_using_reg (op1);
6276 op1_hash = HASH (op1, mode);
6279 op1_elt = insert (op1, NULL_PTR, op1_hash, mode);
6280 op1_elt->in_memory = op1_in_memory;
6281 op1_elt->in_struct = op1_in_struct;
6284 qty_comparison_qty[REG_QTY (REGNO (op0))] = REG_QTY (REGNO (op1));
6285 qty_comparison_const[REG_QTY (REGNO (op0))] = 0;
6287 else
6289 qty_comparison_qty[REG_QTY (REGNO (op0))] = -1;
6290 qty_comparison_const[REG_QTY (REGNO (op0))] = op1;
6293 return;
6296 /* If either side is still missing an equivalence, make it now,
6297 then merge the equivalences. */
6299 if (op0_elt == 0)
6301 if (insert_regs (op0, NULL_PTR, 0))
6303 rehash_using_reg (op0);
6304 op0_hash = HASH (op0, mode);
6307 op0_elt = insert (op0, NULL_PTR, op0_hash, mode);
6308 op0_elt->in_memory = op0_in_memory;
6309 op0_elt->in_struct = op0_in_struct;
6312 if (op1_elt == 0)
6314 if (insert_regs (op1, NULL_PTR, 0))
6316 rehash_using_reg (op1);
6317 op1_hash = HASH (op1, mode);
6320 op1_elt = insert (op1, NULL_PTR, op1_hash, mode);
6321 op1_elt->in_memory = op1_in_memory;
6322 op1_elt->in_struct = op1_in_struct;
6325 merge_equiv_classes (op0_elt, op1_elt);
6326 last_jump_equiv_class = op0_elt;
6329 /* CSE processing for one instruction.
6330 First simplify sources and addresses of all assignments
6331 in the instruction, using previously-computed equivalents values.
6332 Then install the new sources and destinations in the table
6333 of available values.
6335 If LIBCALL_INSN is nonzero, don't record any equivalence made in
6336 the insn. It means that INSN is inside libcall block. In this
6337 case LIBCALL_INSN is the corresponding insn with REG_LIBCALL. */
6339 /* Data on one SET contained in the instruction. */
6341 struct set
6343 /* The SET rtx itself. */
6344 rtx rtl;
6345 /* The SET_SRC of the rtx (the original value, if it is changing). */
6346 rtx src;
6347 /* The hash-table element for the SET_SRC of the SET. */
6348 struct table_elt *src_elt;
6349 /* Hash value for the SET_SRC. */
6350 unsigned src_hash;
6351 /* Hash value for the SET_DEST. */
6352 unsigned dest_hash;
6353 /* The SET_DEST, with SUBREG, etc., stripped. */
6354 rtx inner_dest;
6355 /* Place where the pointer to the INNER_DEST was found. */
6356 rtx *inner_dest_loc;
6357 /* Nonzero if the SET_SRC is in memory. */
6358 char src_in_memory;
6359 /* Nonzero if the SET_SRC is in a structure. */
6360 char src_in_struct;
6361 /* Nonzero if the SET_SRC contains something
6362 whose value cannot be predicted and understood. */
6363 char src_volatile;
6364 /* Original machine mode, in case it becomes a CONST_INT. */
6365 enum machine_mode mode;
6366 /* A constant equivalent for SET_SRC, if any. */
6367 rtx src_const;
6368 /* Hash value of constant equivalent for SET_SRC. */
6369 unsigned src_const_hash;
6370 /* Table entry for constant equivalent for SET_SRC, if any. */
6371 struct table_elt *src_const_elt;
6374 static void
6375 cse_insn (insn, libcall_insn)
6376 rtx insn;
6377 rtx libcall_insn;
6379 register rtx x = PATTERN (insn);
6380 register int i;
6381 rtx tem;
6382 register int n_sets = 0;
6384 #ifdef HAVE_cc0
6385 /* Records what this insn does to set CC0. */
6386 rtx this_insn_cc0 = 0;
6387 enum machine_mode this_insn_cc0_mode = VOIDmode;
6388 #endif
6390 rtx src_eqv = 0;
6391 struct table_elt *src_eqv_elt = 0;
6392 int src_eqv_volatile = 0;
6393 int src_eqv_in_memory = 0;
6394 int src_eqv_in_struct = 0;
6395 unsigned src_eqv_hash = 0;
6397 struct set *sets = NULL_PTR;
6399 this_insn = insn;
6401 /* Find all the SETs and CLOBBERs in this instruction.
6402 Record all the SETs in the array `set' and count them.
6403 Also determine whether there is a CLOBBER that invalidates
6404 all memory references, or all references at varying addresses. */
6406 if (GET_CODE (insn) == CALL_INSN)
6408 for (tem = CALL_INSN_FUNCTION_USAGE (insn); tem; tem = XEXP (tem, 1))
6409 if (GET_CODE (XEXP (tem, 0)) == CLOBBER)
6410 invalidate (SET_DEST (XEXP (tem, 0)), VOIDmode);
6413 if (GET_CODE (x) == SET)
6415 sets = (struct set *) alloca (sizeof (struct set));
6416 sets[0].rtl = x;
6418 /* Ignore SETs that are unconditional jumps.
6419 They never need cse processing, so this does not hurt.
6420 The reason is not efficiency but rather
6421 so that we can test at the end for instructions
6422 that have been simplified to unconditional jumps
6423 and not be misled by unchanged instructions
6424 that were unconditional jumps to begin with. */
6425 if (SET_DEST (x) == pc_rtx
6426 && GET_CODE (SET_SRC (x)) == LABEL_REF)
6429 /* Don't count call-insns, (set (reg 0) (call ...)), as a set.
6430 The hard function value register is used only once, to copy to
6431 someplace else, so it isn't worth cse'ing (and on 80386 is unsafe)!
6432 Ensure we invalidate the destination register. On the 80386 no
6433 other code would invalidate it since it is a fixed_reg.
6434 We need not check the return of apply_change_group; see canon_reg. */
6436 else if (GET_CODE (SET_SRC (x)) == CALL)
6438 canon_reg (SET_SRC (x), insn);
6439 apply_change_group ();
6440 fold_rtx (SET_SRC (x), insn);
6441 invalidate (SET_DEST (x), VOIDmode);
6443 else
6444 n_sets = 1;
6446 else if (GET_CODE (x) == PARALLEL)
6448 register int lim = XVECLEN (x, 0);
6450 sets = (struct set *) alloca (lim * sizeof (struct set));
6452 /* Find all regs explicitly clobbered in this insn,
6453 and ensure they are not replaced with any other regs
6454 elsewhere in this insn.
6455 When a reg that is clobbered is also used for input,
6456 we should presume that that is for a reason,
6457 and we should not substitute some other register
6458 which is not supposed to be clobbered.
6459 Therefore, this loop cannot be merged into the one below
6460 because a CALL may precede a CLOBBER and refer to the
6461 value clobbered. We must not let a canonicalization do
6462 anything in that case. */
6463 for (i = 0; i < lim; i++)
6465 register rtx y = XVECEXP (x, 0, i);
6466 if (GET_CODE (y) == CLOBBER)
6468 rtx clobbered = XEXP (y, 0);
6470 if (GET_CODE (clobbered) == REG
6471 || GET_CODE (clobbered) == SUBREG)
6472 invalidate (clobbered, VOIDmode);
6473 else if (GET_CODE (clobbered) == STRICT_LOW_PART
6474 || GET_CODE (clobbered) == ZERO_EXTRACT)
6475 invalidate (XEXP (clobbered, 0), GET_MODE (clobbered));
6479 for (i = 0; i < lim; i++)
6481 register rtx y = XVECEXP (x, 0, i);
6482 if (GET_CODE (y) == SET)
6484 /* As above, we ignore unconditional jumps and call-insns and
6485 ignore the result of apply_change_group. */
6486 if (GET_CODE (SET_SRC (y)) == CALL)
6488 canon_reg (SET_SRC (y), insn);
6489 apply_change_group ();
6490 fold_rtx (SET_SRC (y), insn);
6491 invalidate (SET_DEST (y), VOIDmode);
6493 else if (SET_DEST (y) == pc_rtx
6494 && GET_CODE (SET_SRC (y)) == LABEL_REF)
6496 else
6497 sets[n_sets++].rtl = y;
6499 else if (GET_CODE (y) == CLOBBER)
6501 /* If we clobber memory, canon the address.
6502 This does nothing when a register is clobbered
6503 because we have already invalidated the reg. */
6504 if (GET_CODE (XEXP (y, 0)) == MEM)
6505 canon_reg (XEXP (y, 0), NULL_RTX);
6507 else if (GET_CODE (y) == USE
6508 && ! (GET_CODE (XEXP (y, 0)) == REG
6509 && REGNO (XEXP (y, 0)) < FIRST_PSEUDO_REGISTER))
6510 canon_reg (y, NULL_RTX);
6511 else if (GET_CODE (y) == CALL)
6513 /* The result of apply_change_group can be ignored; see
6514 canon_reg. */
6515 canon_reg (y, insn);
6516 apply_change_group ();
6517 fold_rtx (y, insn);
6521 else if (GET_CODE (x) == CLOBBER)
6523 if (GET_CODE (XEXP (x, 0)) == MEM)
6524 canon_reg (XEXP (x, 0), NULL_RTX);
6527 /* Canonicalize a USE of a pseudo register or memory location. */
6528 else if (GET_CODE (x) == USE
6529 && ! (GET_CODE (XEXP (x, 0)) == REG
6530 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER))
6531 canon_reg (XEXP (x, 0), NULL_RTX);
6532 else if (GET_CODE (x) == CALL)
6534 /* The result of apply_change_group can be ignored; see canon_reg. */
6535 canon_reg (x, insn);
6536 apply_change_group ();
6537 fold_rtx (x, insn);
6540 /* Store the equivalent value in SRC_EQV, if different, or if the DEST
6541 is a STRICT_LOW_PART. The latter condition is necessary because SRC_EQV
6542 is handled specially for this case, and if it isn't set, then there will
6543 be no equivalence for the destination. */
6544 if (n_sets == 1 && REG_NOTES (insn) != 0
6545 && (tem = find_reg_note (insn, REG_EQUAL, NULL_RTX)) != 0
6546 && (! rtx_equal_p (XEXP (tem, 0), SET_SRC (sets[0].rtl))
6547 || GET_CODE (SET_DEST (sets[0].rtl)) == STRICT_LOW_PART))
6548 src_eqv = canon_reg (XEXP (tem, 0), NULL_RTX);
6550 /* Canonicalize sources and addresses of destinations.
6551 We do this in a separate pass to avoid problems when a MATCH_DUP is
6552 present in the insn pattern. In that case, we want to ensure that
6553 we don't break the duplicate nature of the pattern. So we will replace
6554 both operands at the same time. Otherwise, we would fail to find an
6555 equivalent substitution in the loop calling validate_change below.
6557 We used to suppress canonicalization of DEST if it appears in SRC,
6558 but we don't do this any more. */
6560 for (i = 0; i < n_sets; i++)
6562 rtx dest = SET_DEST (sets[i].rtl);
6563 rtx src = SET_SRC (sets[i].rtl);
6564 rtx new = canon_reg (src, insn);
6565 int insn_code;
6567 if ((GET_CODE (new) == REG && GET_CODE (src) == REG
6568 && ((REGNO (new) < FIRST_PSEUDO_REGISTER)
6569 != (REGNO (src) < FIRST_PSEUDO_REGISTER)))
6570 || (insn_code = recog_memoized (insn)) < 0
6571 || insn_data[insn_code].n_dups > 0)
6572 validate_change (insn, &SET_SRC (sets[i].rtl), new, 1);
6573 else
6574 SET_SRC (sets[i].rtl) = new;
6576 if (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
6578 validate_change (insn, &XEXP (dest, 1),
6579 canon_reg (XEXP (dest, 1), insn), 1);
6580 validate_change (insn, &XEXP (dest, 2),
6581 canon_reg (XEXP (dest, 2), insn), 1);
6584 while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
6585 || GET_CODE (dest) == ZERO_EXTRACT
6586 || GET_CODE (dest) == SIGN_EXTRACT)
6587 dest = XEXP (dest, 0);
6589 if (GET_CODE (dest) == MEM)
6590 canon_reg (dest, insn);
6593 /* Now that we have done all the replacements, we can apply the change
6594 group and see if they all work. Note that this will cause some
6595 canonicalizations that would have worked individually not to be applied
6596 because some other canonicalization didn't work, but this should not
6597 occur often.
6599 The result of apply_change_group can be ignored; see canon_reg. */
6601 apply_change_group ();
6603 /* Set sets[i].src_elt to the class each source belongs to.
6604 Detect assignments from or to volatile things
6605 and set set[i] to zero so they will be ignored
6606 in the rest of this function.
6608 Nothing in this loop changes the hash table or the register chains. */
6610 for (i = 0; i < n_sets; i++)
6612 register rtx src, dest;
6613 register rtx src_folded;
6614 register struct table_elt *elt = 0, *p;
6615 enum machine_mode mode;
6616 rtx src_eqv_here;
6617 rtx src_const = 0;
6618 rtx src_related = 0;
6619 struct table_elt *src_const_elt = 0;
6620 int src_cost = 10000, src_eqv_cost = 10000, src_folded_cost = 10000;
6621 int src_related_cost = 10000, src_elt_cost = 10000;
6622 /* Set non-zero if we need to call force_const_mem on with the
6623 contents of src_folded before using it. */
6624 int src_folded_force_flag = 0;
6626 dest = SET_DEST (sets[i].rtl);
6627 src = SET_SRC (sets[i].rtl);
6629 /* If SRC is a constant that has no machine mode,
6630 hash it with the destination's machine mode.
6631 This way we can keep different modes separate. */
6633 mode = GET_MODE (src) == VOIDmode ? GET_MODE (dest) : GET_MODE (src);
6634 sets[i].mode = mode;
6636 if (src_eqv)
6638 enum machine_mode eqvmode = mode;
6639 if (GET_CODE (dest) == STRICT_LOW_PART)
6640 eqvmode = GET_MODE (SUBREG_REG (XEXP (dest, 0)));
6641 do_not_record = 0;
6642 hash_arg_in_memory = 0;
6643 hash_arg_in_struct = 0;
6644 src_eqv = fold_rtx (src_eqv, insn);
6645 src_eqv_hash = HASH (src_eqv, eqvmode);
6647 /* Find the equivalence class for the equivalent expression. */
6649 if (!do_not_record)
6650 src_eqv_elt = lookup (src_eqv, src_eqv_hash, eqvmode);
6652 src_eqv_volatile = do_not_record;
6653 src_eqv_in_memory = hash_arg_in_memory;
6654 src_eqv_in_struct = hash_arg_in_struct;
6657 /* If this is a STRICT_LOW_PART assignment, src_eqv corresponds to the
6658 value of the INNER register, not the destination. So it is not
6659 a valid substitution for the source. But save it for later. */
6660 if (GET_CODE (dest) == STRICT_LOW_PART)
6661 src_eqv_here = 0;
6662 else
6663 src_eqv_here = src_eqv;
6665 /* Simplify and foldable subexpressions in SRC. Then get the fully-
6666 simplified result, which may not necessarily be valid. */
6667 src_folded = fold_rtx (src, insn);
6669 #if 0
6670 /* ??? This caused bad code to be generated for the m68k port with -O2.
6671 Suppose src is (CONST_INT -1), and that after truncation src_folded
6672 is (CONST_INT 3). Suppose src_folded is then used for src_const.
6673 At the end we will add src and src_const to the same equivalence
6674 class. We now have 3 and -1 on the same equivalence class. This
6675 causes later instructions to be mis-optimized. */
6676 /* If storing a constant in a bitfield, pre-truncate the constant
6677 so we will be able to record it later. */
6678 if (GET_CODE (SET_DEST (sets[i].rtl)) == ZERO_EXTRACT
6679 || GET_CODE (SET_DEST (sets[i].rtl)) == SIGN_EXTRACT)
6681 rtx width = XEXP (SET_DEST (sets[i].rtl), 1);
6683 if (GET_CODE (src) == CONST_INT
6684 && GET_CODE (width) == CONST_INT
6685 && INTVAL (width) < HOST_BITS_PER_WIDE_INT
6686 && (INTVAL (src) & ((HOST_WIDE_INT) (-1) << INTVAL (width))))
6687 src_folded
6688 = GEN_INT (INTVAL (src) & (((HOST_WIDE_INT) 1
6689 << INTVAL (width)) - 1));
6691 #endif
6693 /* Compute SRC's hash code, and also notice if it
6694 should not be recorded at all. In that case,
6695 prevent any further processing of this assignment. */
6696 do_not_record = 0;
6697 hash_arg_in_memory = 0;
6698 hash_arg_in_struct = 0;
6700 sets[i].src = src;
6701 sets[i].src_hash = HASH (src, mode);
6702 sets[i].src_volatile = do_not_record;
6703 sets[i].src_in_memory = hash_arg_in_memory;
6704 sets[i].src_in_struct = hash_arg_in_struct;
6706 /* If SRC is a MEM, there is a REG_EQUIV note for SRC, and DEST is
6707 a pseudo that is set more than once, do not record SRC. Using
6708 SRC as a replacement for anything else will be incorrect in that
6709 situation. Note that this usually occurs only for stack slots,
6710 in which case all the RTL would be referring to SRC, so we don't
6711 lose any optimization opportunities by not having SRC in the
6712 hash table. */
6714 if (GET_CODE (src) == MEM
6715 && find_reg_note (insn, REG_EQUIV, src) != 0
6716 && GET_CODE (dest) == REG
6717 && REGNO (dest) >= FIRST_PSEUDO_REGISTER
6718 && REG_N_SETS (REGNO (dest)) != 1)
6719 sets[i].src_volatile = 1;
6721 #if 0
6722 /* It is no longer clear why we used to do this, but it doesn't
6723 appear to still be needed. So let's try without it since this
6724 code hurts cse'ing widened ops. */
6725 /* If source is a perverse subreg (such as QI treated as an SI),
6726 treat it as volatile. It may do the work of an SI in one context
6727 where the extra bits are not being used, but cannot replace an SI
6728 in general. */
6729 if (GET_CODE (src) == SUBREG
6730 && (GET_MODE_SIZE (GET_MODE (src))
6731 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))))
6732 sets[i].src_volatile = 1;
6733 #endif
6735 /* Locate all possible equivalent forms for SRC. Try to replace
6736 SRC in the insn with each cheaper equivalent.
6738 We have the following types of equivalents: SRC itself, a folded
6739 version, a value given in a REG_EQUAL note, or a value related
6740 to a constant.
6742 Each of these equivalents may be part of an additional class
6743 of equivalents (if more than one is in the table, they must be in
6744 the same class; we check for this).
6746 If the source is volatile, we don't do any table lookups.
6748 We note any constant equivalent for possible later use in a
6749 REG_NOTE. */
6751 if (!sets[i].src_volatile)
6752 elt = lookup (src, sets[i].src_hash, mode);
6754 sets[i].src_elt = elt;
6756 if (elt && src_eqv_here && src_eqv_elt)
6758 if (elt->first_same_value != src_eqv_elt->first_same_value)
6760 /* The REG_EQUAL is indicating that two formerly distinct
6761 classes are now equivalent. So merge them. */
6762 merge_equiv_classes (elt, src_eqv_elt);
6763 src_eqv_hash = HASH (src_eqv, elt->mode);
6764 src_eqv_elt = lookup (src_eqv, src_eqv_hash, elt->mode);
6767 src_eqv_here = 0;
6770 else if (src_eqv_elt)
6771 elt = src_eqv_elt;
6773 /* Try to find a constant somewhere and record it in `src_const'.
6774 Record its table element, if any, in `src_const_elt'. Look in
6775 any known equivalences first. (If the constant is not in the
6776 table, also set `sets[i].src_const_hash'). */
6777 if (elt)
6778 for (p = elt->first_same_value; p; p = p->next_same_value)
6779 if (p->is_const)
6781 src_const = p->exp;
6782 src_const_elt = elt;
6783 break;
6786 if (src_const == 0
6787 && (CONSTANT_P (src_folded)
6788 /* Consider (minus (label_ref L1) (label_ref L2)) as
6789 "constant" here so we will record it. This allows us
6790 to fold switch statements when an ADDR_DIFF_VEC is used. */
6791 || (GET_CODE (src_folded) == MINUS
6792 && GET_CODE (XEXP (src_folded, 0)) == LABEL_REF
6793 && GET_CODE (XEXP (src_folded, 1)) == LABEL_REF)))
6794 src_const = src_folded, src_const_elt = elt;
6795 else if (src_const == 0 && src_eqv_here && CONSTANT_P (src_eqv_here))
6796 src_const = src_eqv_here, src_const_elt = src_eqv_elt;
6798 /* If we don't know if the constant is in the table, get its
6799 hash code and look it up. */
6800 if (src_const && src_const_elt == 0)
6802 sets[i].src_const_hash = HASH (src_const, mode);
6803 src_const_elt = lookup (src_const, sets[i].src_const_hash, mode);
6806 sets[i].src_const = src_const;
6807 sets[i].src_const_elt = src_const_elt;
6809 /* If the constant and our source are both in the table, mark them as
6810 equivalent. Otherwise, if a constant is in the table but the source
6811 isn't, set ELT to it. */
6812 if (src_const_elt && elt
6813 && src_const_elt->first_same_value != elt->first_same_value)
6814 merge_equiv_classes (elt, src_const_elt);
6815 else if (src_const_elt && elt == 0)
6816 elt = src_const_elt;
6818 /* See if there is a register linearly related to a constant
6819 equivalent of SRC. */
6820 if (src_const
6821 && (GET_CODE (src_const) == CONST
6822 || (src_const_elt && src_const_elt->related_value != 0)))
6824 src_related = use_related_value (src_const, src_const_elt);
6825 if (src_related)
6827 struct table_elt *src_related_elt
6828 = lookup (src_related, HASH (src_related, mode), mode);
6829 if (src_related_elt && elt)
6831 if (elt->first_same_value
6832 != src_related_elt->first_same_value)
6833 /* This can occur when we previously saw a CONST
6834 involving a SYMBOL_REF and then see the SYMBOL_REF
6835 twice. Merge the involved classes. */
6836 merge_equiv_classes (elt, src_related_elt);
6838 src_related = 0;
6839 src_related_elt = 0;
6841 else if (src_related_elt && elt == 0)
6842 elt = src_related_elt;
6846 /* See if we have a CONST_INT that is already in a register in a
6847 wider mode. */
6849 if (src_const && src_related == 0 && GET_CODE (src_const) == CONST_INT
6850 && GET_MODE_CLASS (mode) == MODE_INT
6851 && GET_MODE_BITSIZE (mode) < BITS_PER_WORD)
6853 enum machine_mode wider_mode;
6855 for (wider_mode = GET_MODE_WIDER_MODE (mode);
6856 GET_MODE_BITSIZE (wider_mode) <= BITS_PER_WORD
6857 && src_related == 0;
6858 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
6860 struct table_elt *const_elt
6861 = lookup (src_const, HASH (src_const, wider_mode), wider_mode);
6863 if (const_elt == 0)
6864 continue;
6866 for (const_elt = const_elt->first_same_value;
6867 const_elt; const_elt = const_elt->next_same_value)
6868 if (GET_CODE (const_elt->exp) == REG)
6870 src_related = gen_lowpart_if_possible (mode,
6871 const_elt->exp);
6872 break;
6877 /* Another possibility is that we have an AND with a constant in
6878 a mode narrower than a word. If so, it might have been generated
6879 as part of an "if" which would narrow the AND. If we already
6880 have done the AND in a wider mode, we can use a SUBREG of that
6881 value. */
6883 if (flag_expensive_optimizations && ! src_related
6884 && GET_CODE (src) == AND && GET_CODE (XEXP (src, 1)) == CONST_INT
6885 && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
6887 enum machine_mode tmode;
6888 rtx new_and = gen_rtx_AND (VOIDmode, NULL_RTX, XEXP (src, 1));
6890 for (tmode = GET_MODE_WIDER_MODE (mode);
6891 GET_MODE_SIZE (tmode) <= UNITS_PER_WORD;
6892 tmode = GET_MODE_WIDER_MODE (tmode))
6894 rtx inner = gen_lowpart_if_possible (tmode, XEXP (src, 0));
6895 struct table_elt *larger_elt;
6897 if (inner)
6899 PUT_MODE (new_and, tmode);
6900 XEXP (new_and, 0) = inner;
6901 larger_elt = lookup (new_and, HASH (new_and, tmode), tmode);
6902 if (larger_elt == 0)
6903 continue;
6905 for (larger_elt = larger_elt->first_same_value;
6906 larger_elt; larger_elt = larger_elt->next_same_value)
6907 if (GET_CODE (larger_elt->exp) == REG)
6909 src_related
6910 = gen_lowpart_if_possible (mode, larger_elt->exp);
6911 break;
6914 if (src_related)
6915 break;
6920 #ifdef LOAD_EXTEND_OP
6921 /* See if a MEM has already been loaded with a widening operation;
6922 if it has, we can use a subreg of that. Many CISC machines
6923 also have such operations, but this is only likely to be
6924 beneficial these machines. */
6926 if (flag_expensive_optimizations && src_related == 0
6927 && (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
6928 && GET_MODE_CLASS (mode) == MODE_INT
6929 && GET_CODE (src) == MEM && ! do_not_record
6930 && LOAD_EXTEND_OP (mode) != NIL)
6932 enum machine_mode tmode;
6934 /* Set what we are trying to extend and the operation it might
6935 have been extended with. */
6936 PUT_CODE (memory_extend_rtx, LOAD_EXTEND_OP (mode));
6937 XEXP (memory_extend_rtx, 0) = src;
6939 for (tmode = GET_MODE_WIDER_MODE (mode);
6940 GET_MODE_SIZE (tmode) <= UNITS_PER_WORD;
6941 tmode = GET_MODE_WIDER_MODE (tmode))
6943 struct table_elt *larger_elt;
6945 PUT_MODE (memory_extend_rtx, tmode);
6946 larger_elt = lookup (memory_extend_rtx,
6947 HASH (memory_extend_rtx, tmode), tmode);
6948 if (larger_elt == 0)
6949 continue;
6951 for (larger_elt = larger_elt->first_same_value;
6952 larger_elt; larger_elt = larger_elt->next_same_value)
6953 if (GET_CODE (larger_elt->exp) == REG)
6955 src_related = gen_lowpart_if_possible (mode,
6956 larger_elt->exp);
6957 break;
6960 if (src_related)
6961 break;
6964 #endif /* LOAD_EXTEND_OP */
6966 if (src == src_folded)
6967 src_folded = 0;
6969 /* At this point, ELT, if non-zero, points to a class of expressions
6970 equivalent to the source of this SET and SRC, SRC_EQV, SRC_FOLDED,
6971 and SRC_RELATED, if non-zero, each contain additional equivalent
6972 expressions. Prune these latter expressions by deleting expressions
6973 already in the equivalence class.
6975 Check for an equivalent identical to the destination. If found,
6976 this is the preferred equivalent since it will likely lead to
6977 elimination of the insn. Indicate this by placing it in
6978 `src_related'. */
6980 if (elt) elt = elt->first_same_value;
6981 for (p = elt; p; p = p->next_same_value)
6983 enum rtx_code code = GET_CODE (p->exp);
6985 /* If the expression is not valid, ignore it. Then we do not
6986 have to check for validity below. In most cases, we can use
6987 `rtx_equal_p', since canonicalization has already been done. */
6988 if (code != REG && ! exp_equiv_p (p->exp, p->exp, 1, 0))
6989 continue;
6991 /* Also skip paradoxical subregs, unless that's what we're
6992 looking for. */
6993 if (code == SUBREG
6994 && (GET_MODE_SIZE (GET_MODE (p->exp))
6995 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (p->exp))))
6996 && ! (src != 0
6997 && GET_CODE (src) == SUBREG
6998 && GET_MODE (src) == GET_MODE (p->exp)
6999 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
7000 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (p->exp))))))
7001 continue;
7003 if (src && GET_CODE (src) == code && rtx_equal_p (src, p->exp))
7004 src = 0;
7005 else if (src_folded && GET_CODE (src_folded) == code
7006 && rtx_equal_p (src_folded, p->exp))
7007 src_folded = 0;
7008 else if (src_eqv_here && GET_CODE (src_eqv_here) == code
7009 && rtx_equal_p (src_eqv_here, p->exp))
7010 src_eqv_here = 0;
7011 else if (src_related && GET_CODE (src_related) == code
7012 && rtx_equal_p (src_related, p->exp))
7013 src_related = 0;
7015 /* This is the same as the destination of the insns, we want
7016 to prefer it. Copy it to src_related. The code below will
7017 then give it a negative cost. */
7018 if (GET_CODE (dest) == code && rtx_equal_p (p->exp, dest))
7019 src_related = dest;
7023 /* Find the cheapest valid equivalent, trying all the available
7024 possibilities. Prefer items not in the hash table to ones
7025 that are when they are equal cost. Note that we can never
7026 worsen an insn as the current contents will also succeed.
7027 If we find an equivalent identical to the destination, use it as best,
7028 since this insn will probably be eliminated in that case. */
7029 if (src)
7031 if (rtx_equal_p (src, dest))
7032 src_cost = -1;
7033 else
7034 src_cost = COST (src);
7037 if (src_eqv_here)
7039 if (rtx_equal_p (src_eqv_here, dest))
7040 src_eqv_cost = -1;
7041 else
7042 src_eqv_cost = COST (src_eqv_here);
7045 if (src_folded)
7047 if (rtx_equal_p (src_folded, dest))
7048 src_folded_cost = -1;
7049 else
7050 src_folded_cost = COST (src_folded);
7053 if (src_related)
7055 if (rtx_equal_p (src_related, dest))
7056 src_related_cost = -1;
7057 else
7058 src_related_cost = COST (src_related);
7061 /* If this was an indirect jump insn, a known label will really be
7062 cheaper even though it looks more expensive. */
7063 if (dest == pc_rtx && src_const && GET_CODE (src_const) == LABEL_REF)
7064 src_folded = src_const, src_folded_cost = -1;
7066 /* Terminate loop when replacement made. This must terminate since
7067 the current contents will be tested and will always be valid. */
7068 while (1)
7070 rtx trial, old_src;
7072 /* Skip invalid entries. */
7073 while (elt && GET_CODE (elt->exp) != REG
7074 && ! exp_equiv_p (elt->exp, elt->exp, 1, 0))
7075 elt = elt->next_same_value;
7077 /* A paradoxical subreg would be bad here: it'll be the right
7078 size, but later may be adjusted so that the upper bits aren't
7079 what we want. So reject it. */
7080 if (elt != 0
7081 && GET_CODE (elt->exp) == SUBREG
7082 && (GET_MODE_SIZE (GET_MODE (elt->exp))
7083 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (elt->exp))))
7084 /* It is okay, though, if the rtx we're trying to match
7085 will ignore any of the bits we can't predict. */
7086 && ! (src != 0
7087 && GET_CODE (src) == SUBREG
7088 && GET_MODE (src) == GET_MODE (elt->exp)
7089 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
7090 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (elt->exp))))))
7092 elt = elt->next_same_value;
7093 continue;
7096 if (elt) src_elt_cost = elt->cost;
7098 /* Find cheapest and skip it for the next time. For items
7099 of equal cost, use this order:
7100 src_folded, src, src_eqv, src_related and hash table entry. */
7101 if (src_folded_cost <= src_cost
7102 && src_folded_cost <= src_eqv_cost
7103 && src_folded_cost <= src_related_cost
7104 && src_folded_cost <= src_elt_cost)
7106 trial = src_folded, src_folded_cost = 10000;
7107 if (src_folded_force_flag)
7108 trial = force_const_mem (mode, trial);
7110 else if (src_cost <= src_eqv_cost
7111 && src_cost <= src_related_cost
7112 && src_cost <= src_elt_cost)
7113 trial = src, src_cost = 10000;
7114 else if (src_eqv_cost <= src_related_cost
7115 && src_eqv_cost <= src_elt_cost)
7116 trial = copy_rtx (src_eqv_here), src_eqv_cost = 10000;
7117 else if (src_related_cost <= src_elt_cost)
7118 trial = copy_rtx (src_related), src_related_cost = 10000;
7119 else
7121 trial = copy_rtx (elt->exp);
7122 elt = elt->next_same_value;
7123 src_elt_cost = 10000;
7126 /* We don't normally have an insn matching (set (pc) (pc)), so
7127 check for this separately here. We will delete such an
7128 insn below.
7130 Tablejump insns contain a USE of the table, so simply replacing
7131 the operand with the constant won't match. This is simply an
7132 unconditional branch, however, and is therefore valid. Just
7133 insert the substitution here and we will delete and re-emit
7134 the insn later. */
7136 /* Keep track of the original SET_SRC so that we can fix notes
7137 on libcall instructions. */
7138 old_src = SET_SRC (sets[i].rtl);
7140 if (n_sets == 1 && dest == pc_rtx
7141 && (trial == pc_rtx
7142 || (GET_CODE (trial) == LABEL_REF
7143 && ! condjump_p (insn))))
7145 /* If TRIAL is a label in front of a jump table, we are
7146 really falling through the switch (this is how casesi
7147 insns work), so we must branch around the table. */
7148 if (GET_CODE (trial) == CODE_LABEL
7149 && NEXT_INSN (trial) != 0
7150 && GET_CODE (NEXT_INSN (trial)) == JUMP_INSN
7151 && (GET_CODE (PATTERN (NEXT_INSN (trial))) == ADDR_DIFF_VEC
7152 || GET_CODE (PATTERN (NEXT_INSN (trial))) == ADDR_VEC))
7154 trial = gen_rtx_LABEL_REF (Pmode, get_label_after (trial));
7156 SET_SRC (sets[i].rtl) = trial;
7157 cse_jumps_altered = 1;
7158 break;
7161 /* Look for a substitution that makes a valid insn. */
7162 else if (validate_change (insn, &SET_SRC (sets[i].rtl), trial, 0))
7164 /* If we just made a substitution inside a libcall, then we
7165 need to make the same substitution in any notes attached
7166 to the RETVAL insn. */
7167 if (libcall_insn
7168 && (GET_CODE (old_src) == REG
7169 || GET_CODE (old_src) == SUBREG
7170 || GET_CODE (old_src) == MEM))
7171 replace_rtx (REG_NOTES (libcall_insn), old_src,
7172 canon_reg (SET_SRC (sets[i].rtl), insn));
7174 /* The result of apply_change_group can be ignored; see
7175 canon_reg. */
7177 validate_change (insn, &SET_SRC (sets[i].rtl),
7178 canon_reg (SET_SRC (sets[i].rtl), insn),
7180 apply_change_group ();
7181 break;
7184 /* If we previously found constant pool entries for
7185 constants and this is a constant, try making a
7186 pool entry. Put it in src_folded unless we already have done
7187 this since that is where it likely came from. */
7189 else if (constant_pool_entries_cost
7190 && CONSTANT_P (trial)
7191 && ! (GET_CODE (trial) == CONST
7192 && GET_CODE (XEXP (trial, 0)) == TRUNCATE)
7193 && (src_folded == 0
7194 || (GET_CODE (src_folded) != MEM
7195 && ! src_folded_force_flag))
7196 && GET_MODE_CLASS (mode) != MODE_CC
7197 && mode != VOIDmode)
7199 src_folded_force_flag = 1;
7200 src_folded = trial;
7201 src_folded_cost = constant_pool_entries_cost;
7205 src = SET_SRC (sets[i].rtl);
7207 /* In general, it is good to have a SET with SET_SRC == SET_DEST.
7208 However, there is an important exception: If both are registers
7209 that are not the head of their equivalence class, replace SET_SRC
7210 with the head of the class. If we do not do this, we will have
7211 both registers live over a portion of the basic block. This way,
7212 their lifetimes will likely abut instead of overlapping. */
7213 if (GET_CODE (dest) == REG
7214 && REGNO_QTY_VALID_P (REGNO (dest))
7215 && qty_mode[REG_QTY (REGNO (dest))] == GET_MODE (dest)
7216 && qty_first_reg[REG_QTY (REGNO (dest))] != REGNO (dest)
7217 && GET_CODE (src) == REG && REGNO (src) == REGNO (dest)
7218 /* Don't do this if the original insn had a hard reg as
7219 SET_SRC or SET_DEST. */
7220 && (GET_CODE (sets[i].src) != REG
7221 || REGNO (sets[i].src) >= FIRST_PSEUDO_REGISTER)
7222 && (GET_CODE (dest) != REG || REGNO (dest) >= FIRST_PSEUDO_REGISTER))
7223 /* We can't call canon_reg here because it won't do anything if
7224 SRC is a hard register. */
7226 int first = qty_first_reg[REG_QTY (REGNO (src))];
7227 rtx new_src
7228 = (first >= FIRST_PSEUDO_REGISTER
7229 ? regno_reg_rtx[first] : gen_rtx_REG (GET_MODE (src), first));
7231 /* We must use validate-change even for this, because this
7232 might be a special no-op instruction, suitable only to
7233 tag notes onto. */
7234 if (validate_change (insn, &SET_SRC (sets[i].rtl), new_src, 0))
7236 src = new_src;
7237 /* If we had a constant that is cheaper than what we are now
7238 setting SRC to, use that constant. We ignored it when we
7239 thought we could make this into a no-op. */
7240 if (src_const && COST (src_const) < COST (src)
7241 && validate_change (insn, &SET_SRC (sets[i].rtl), src_const,
7243 src = src_const;
7247 /* If we made a change, recompute SRC values. */
7248 if (src != sets[i].src)
7250 do_not_record = 0;
7251 hash_arg_in_memory = 0;
7252 hash_arg_in_struct = 0;
7253 sets[i].src = src;
7254 sets[i].src_hash = HASH (src, mode);
7255 sets[i].src_volatile = do_not_record;
7256 sets[i].src_in_memory = hash_arg_in_memory;
7257 sets[i].src_in_struct = hash_arg_in_struct;
7258 sets[i].src_elt = lookup (src, sets[i].src_hash, mode);
7261 /* If this is a single SET, we are setting a register, and we have an
7262 equivalent constant, we want to add a REG_NOTE. We don't want
7263 to write a REG_EQUAL note for a constant pseudo since verifying that
7264 that pseudo hasn't been eliminated is a pain. Such a note also
7265 won't help anything.
7267 Avoid a REG_EQUAL note for (CONST (MINUS (LABEL_REF) (LABEL_REF)))
7268 which can be created for a reference to a compile time computable
7269 entry in a jump table. */
7271 if (n_sets == 1 && src_const && GET_CODE (dest) == REG
7272 && GET_CODE (src_const) != REG
7273 && ! (GET_CODE (src_const) == CONST
7274 && GET_CODE (XEXP (src_const, 0)) == MINUS
7275 && GET_CODE (XEXP (XEXP (src_const, 0), 0)) == LABEL_REF
7276 && GET_CODE (XEXP (XEXP (src_const, 0), 1)) == LABEL_REF))
7278 tem = find_reg_note (insn, REG_EQUAL, NULL_RTX);
7280 /* Make sure that the rtx is not shared with any other insn. */
7281 src_const = copy_rtx (src_const);
7283 /* Record the actual constant value in a REG_EQUAL note, making
7284 a new one if one does not already exist. */
7285 if (tem)
7286 XEXP (tem, 0) = src_const;
7287 else
7288 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL,
7289 src_const, REG_NOTES (insn));
7291 /* If storing a constant value in a register that
7292 previously held the constant value 0,
7293 record this fact with a REG_WAS_0 note on this insn.
7295 Note that the *register* is required to have previously held 0,
7296 not just any register in the quantity and we must point to the
7297 insn that set that register to zero.
7299 Rather than track each register individually, we just see if
7300 the last set for this quantity was for this register. */
7302 if (REGNO_QTY_VALID_P (REGNO (dest))
7303 && qty_const[REG_QTY (REGNO (dest))] == const0_rtx)
7305 /* See if we previously had a REG_WAS_0 note. */
7306 rtx note = find_reg_note (insn, REG_WAS_0, NULL_RTX);
7307 rtx const_insn = qty_const_insn[REG_QTY (REGNO (dest))];
7309 if ((tem = single_set (const_insn)) != 0
7310 && rtx_equal_p (SET_DEST (tem), dest))
7312 if (note)
7313 XEXP (note, 0) = const_insn;
7314 else
7315 REG_NOTES (insn)
7316 = gen_rtx_INSN_LIST (REG_WAS_0, const_insn,
7317 REG_NOTES (insn));
7322 /* Now deal with the destination. */
7323 do_not_record = 0;
7324 sets[i].inner_dest_loc = &SET_DEST (sets[0].rtl);
7326 /* Look within any SIGN_EXTRACT or ZERO_EXTRACT
7327 to the MEM or REG within it. */
7328 while (GET_CODE (dest) == SIGN_EXTRACT
7329 || GET_CODE (dest) == ZERO_EXTRACT
7330 || GET_CODE (dest) == SUBREG
7331 || GET_CODE (dest) == STRICT_LOW_PART)
7333 sets[i].inner_dest_loc = &XEXP (dest, 0);
7334 dest = XEXP (dest, 0);
7337 sets[i].inner_dest = dest;
7339 if (GET_CODE (dest) == MEM)
7341 #ifdef PUSH_ROUNDING
7342 /* Stack pushes invalidate the stack pointer. */
7343 rtx addr = XEXP (dest, 0);
7344 if ((GET_CODE (addr) == PRE_DEC || GET_CODE (addr) == PRE_INC
7345 || GET_CODE (addr) == POST_DEC || GET_CODE (addr) == POST_INC)
7346 && XEXP (addr, 0) == stack_pointer_rtx)
7347 invalidate (stack_pointer_rtx, Pmode);
7348 #endif
7349 dest = fold_rtx (dest, insn);
7352 /* Compute the hash code of the destination now,
7353 before the effects of this instruction are recorded,
7354 since the register values used in the address computation
7355 are those before this instruction. */
7356 sets[i].dest_hash = HASH (dest, mode);
7358 /* Don't enter a bit-field in the hash table
7359 because the value in it after the store
7360 may not equal what was stored, due to truncation. */
7362 if (GET_CODE (SET_DEST (sets[i].rtl)) == ZERO_EXTRACT
7363 || GET_CODE (SET_DEST (sets[i].rtl)) == SIGN_EXTRACT)
7365 rtx width = XEXP (SET_DEST (sets[i].rtl), 1);
7367 if (src_const != 0 && GET_CODE (src_const) == CONST_INT
7368 && GET_CODE (width) == CONST_INT
7369 && INTVAL (width) < HOST_BITS_PER_WIDE_INT
7370 && ! (INTVAL (src_const)
7371 & ((HOST_WIDE_INT) (-1) << INTVAL (width))))
7372 /* Exception: if the value is constant,
7373 and it won't be truncated, record it. */
7375 else
7377 /* This is chosen so that the destination will be invalidated
7378 but no new value will be recorded.
7379 We must invalidate because sometimes constant
7380 values can be recorded for bitfields. */
7381 sets[i].src_elt = 0;
7382 sets[i].src_volatile = 1;
7383 src_eqv = 0;
7384 src_eqv_elt = 0;
7388 /* If only one set in a JUMP_INSN and it is now a no-op, we can delete
7389 the insn. */
7390 else if (n_sets == 1 && dest == pc_rtx && src == pc_rtx)
7392 /* One less use of the label this insn used to jump to. */
7393 if (JUMP_LABEL (insn) != 0)
7394 --LABEL_NUSES (JUMP_LABEL (insn));
7395 PUT_CODE (insn, NOTE);
7396 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
7397 NOTE_SOURCE_FILE (insn) = 0;
7398 cse_jumps_altered = 1;
7399 /* No more processing for this set. */
7400 sets[i].rtl = 0;
7403 /* If this SET is now setting PC to a label, we know it used to
7404 be a conditional or computed branch. So we see if we can follow
7405 it. If it was a computed branch, delete it and re-emit. */
7406 else if (dest == pc_rtx && GET_CODE (src) == LABEL_REF)
7408 rtx p;
7410 /* If this is not in the format for a simple branch and
7411 we are the only SET in it, re-emit it. */
7412 if (! simplejump_p (insn) && n_sets == 1)
7414 rtx new = emit_jump_insn_before (gen_jump (XEXP (src, 0)), insn);
7415 JUMP_LABEL (new) = XEXP (src, 0);
7416 LABEL_NUSES (XEXP (src, 0))++;
7417 delete_insn (insn);
7418 insn = new;
7420 else
7421 /* Otherwise, force rerecognition, since it probably had
7422 a different pattern before.
7423 This shouldn't really be necessary, since whatever
7424 changed the source value above should have done this.
7425 Until the right place is found, might as well do this here. */
7426 INSN_CODE (insn) = -1;
7428 /* Now that we've converted this jump to an unconditional jump,
7429 there is dead code after it. Delete the dead code until we
7430 reach a BARRIER, the end of the function, or a label. Do
7431 not delete NOTEs except for NOTE_INSN_DELETED since later
7432 phases assume these notes are retained. */
7434 never_reached_warning (insn);
7436 p = insn;
7438 while (NEXT_INSN (p) != 0
7439 && GET_CODE (NEXT_INSN (p)) != BARRIER
7440 && GET_CODE (NEXT_INSN (p)) != CODE_LABEL)
7442 /* Note, we must update P with the return value from
7443 delete_insn, otherwise we could get an infinite loop
7444 if NEXT_INSN (p) had INSN_DELETED_P set. */
7445 if (GET_CODE (NEXT_INSN (p)) != NOTE
7446 || NOTE_LINE_NUMBER (NEXT_INSN (p)) == NOTE_INSN_DELETED)
7447 p = PREV_INSN (delete_insn (NEXT_INSN (p)));
7448 else
7449 p = NEXT_INSN (p);
7452 /* If we don't have a BARRIER immediately after INSN, put one there.
7453 Much code assumes that there are no NOTEs between a JUMP_INSN and
7454 BARRIER. */
7456 if (NEXT_INSN (insn) == 0
7457 || GET_CODE (NEXT_INSN (insn)) != BARRIER)
7458 emit_barrier_before (NEXT_INSN (insn));
7460 /* We might have two BARRIERs separated by notes. Delete the second
7461 one if so. */
7463 if (p != insn && NEXT_INSN (p) != 0
7464 && GET_CODE (NEXT_INSN (p)) == BARRIER)
7465 delete_insn (NEXT_INSN (p));
7467 cse_jumps_altered = 1;
7468 sets[i].rtl = 0;
7471 /* If destination is volatile, invalidate it and then do no further
7472 processing for this assignment. */
7474 else if (do_not_record)
7476 if (GET_CODE (dest) == REG || GET_CODE (dest) == SUBREG
7477 || GET_CODE (dest) == MEM)
7478 invalidate (dest, VOIDmode);
7479 else if (GET_CODE (dest) == STRICT_LOW_PART
7480 || GET_CODE (dest) == ZERO_EXTRACT)
7481 invalidate (XEXP (dest, 0), GET_MODE (dest));
7482 sets[i].rtl = 0;
7485 if (sets[i].rtl != 0 && dest != SET_DEST (sets[i].rtl))
7486 sets[i].dest_hash = HASH (SET_DEST (sets[i].rtl), mode);
7488 #ifdef HAVE_cc0
7489 /* If setting CC0, record what it was set to, or a constant, if it
7490 is equivalent to a constant. If it is being set to a floating-point
7491 value, make a COMPARE with the appropriate constant of 0. If we
7492 don't do this, later code can interpret this as a test against
7493 const0_rtx, which can cause problems if we try to put it into an
7494 insn as a floating-point operand. */
7495 if (dest == cc0_rtx)
7497 this_insn_cc0 = src_const && mode != VOIDmode ? src_const : src;
7498 this_insn_cc0_mode = mode;
7499 if (FLOAT_MODE_P (mode))
7500 this_insn_cc0 = gen_rtx_COMPARE (VOIDmode, this_insn_cc0,
7501 CONST0_RTX (mode));
7503 #endif
7506 /* Now enter all non-volatile source expressions in the hash table
7507 if they are not already present.
7508 Record their equivalence classes in src_elt.
7509 This way we can insert the corresponding destinations into
7510 the same classes even if the actual sources are no longer in them
7511 (having been invalidated). */
7513 if (src_eqv && src_eqv_elt == 0 && sets[0].rtl != 0 && ! src_eqv_volatile
7514 && ! rtx_equal_p (src_eqv, SET_DEST (sets[0].rtl)))
7516 register struct table_elt *elt;
7517 register struct table_elt *classp = sets[0].src_elt;
7518 rtx dest = SET_DEST (sets[0].rtl);
7519 enum machine_mode eqvmode = GET_MODE (dest);
7521 if (GET_CODE (dest) == STRICT_LOW_PART)
7523 eqvmode = GET_MODE (SUBREG_REG (XEXP (dest, 0)));
7524 classp = 0;
7526 if (insert_regs (src_eqv, classp, 0))
7528 rehash_using_reg (src_eqv);
7529 src_eqv_hash = HASH (src_eqv, eqvmode);
7531 elt = insert (src_eqv, classp, src_eqv_hash, eqvmode);
7532 elt->in_memory = src_eqv_in_memory;
7533 elt->in_struct = src_eqv_in_struct;
7534 src_eqv_elt = elt;
7536 /* Check to see if src_eqv_elt is the same as a set source which
7537 does not yet have an elt, and if so set the elt of the set source
7538 to src_eqv_elt. */
7539 for (i = 0; i < n_sets; i++)
7540 if (sets[i].rtl && sets[i].src_elt == 0
7541 && rtx_equal_p (SET_SRC (sets[i].rtl), src_eqv))
7542 sets[i].src_elt = src_eqv_elt;
7545 for (i = 0; i < n_sets; i++)
7546 if (sets[i].rtl && ! sets[i].src_volatile
7547 && ! rtx_equal_p (SET_SRC (sets[i].rtl), SET_DEST (sets[i].rtl)))
7549 if (GET_CODE (SET_DEST (sets[i].rtl)) == STRICT_LOW_PART)
7551 /* REG_EQUAL in setting a STRICT_LOW_PART
7552 gives an equivalent for the entire destination register,
7553 not just for the subreg being stored in now.
7554 This is a more interesting equivalence, so we arrange later
7555 to treat the entire reg as the destination. */
7556 sets[i].src_elt = src_eqv_elt;
7557 sets[i].src_hash = src_eqv_hash;
7559 else
7561 /* Insert source and constant equivalent into hash table, if not
7562 already present. */
7563 register struct table_elt *classp = src_eqv_elt;
7564 register rtx src = sets[i].src;
7565 register rtx dest = SET_DEST (sets[i].rtl);
7566 enum machine_mode mode
7567 = GET_MODE (src) == VOIDmode ? GET_MODE (dest) : GET_MODE (src);
7569 /* Don't put a hard register source into the table if this is
7570 the last insn of a libcall. */
7571 if (sets[i].src_elt == 0
7572 && (GET_CODE (src) != REG
7573 || REGNO (src) >= FIRST_PSEUDO_REGISTER
7574 || ! find_reg_note (insn, REG_RETVAL, NULL_RTX)))
7576 register struct table_elt *elt;
7578 /* Note that these insert_regs calls cannot remove
7579 any of the src_elt's, because they would have failed to
7580 match if not still valid. */
7581 if (insert_regs (src, classp, 0))
7583 rehash_using_reg (src);
7584 sets[i].src_hash = HASH (src, mode);
7586 elt = insert (src, classp, sets[i].src_hash, mode);
7587 elt->in_memory = sets[i].src_in_memory;
7588 elt->in_struct = sets[i].src_in_struct;
7589 sets[i].src_elt = classp = elt;
7592 if (sets[i].src_const && sets[i].src_const_elt == 0
7593 && src != sets[i].src_const
7594 && ! rtx_equal_p (sets[i].src_const, src))
7595 sets[i].src_elt = insert (sets[i].src_const, classp,
7596 sets[i].src_const_hash, mode);
7599 else if (sets[i].src_elt == 0)
7600 /* If we did not insert the source into the hash table (e.g., it was
7601 volatile), note the equivalence class for the REG_EQUAL value, if any,
7602 so that the destination goes into that class. */
7603 sets[i].src_elt = src_eqv_elt;
7605 invalidate_from_clobbers (x);
7607 /* Some registers are invalidated by subroutine calls. Memory is
7608 invalidated by non-constant calls. */
7610 if (GET_CODE (insn) == CALL_INSN)
7612 if (! CONST_CALL_P (insn))
7613 invalidate_memory ();
7614 invalidate_for_call ();
7617 /* Now invalidate everything set by this instruction.
7618 If a SUBREG or other funny destination is being set,
7619 sets[i].rtl is still nonzero, so here we invalidate the reg
7620 a part of which is being set. */
7622 for (i = 0; i < n_sets; i++)
7623 if (sets[i].rtl)
7625 /* We can't use the inner dest, because the mode associated with
7626 a ZERO_EXTRACT is significant. */
7627 register rtx dest = SET_DEST (sets[i].rtl);
7629 /* Needed for registers to remove the register from its
7630 previous quantity's chain.
7631 Needed for memory if this is a nonvarying address, unless
7632 we have just done an invalidate_memory that covers even those. */
7633 if (GET_CODE (dest) == REG || GET_CODE (dest) == SUBREG
7634 || GET_CODE (dest) == MEM)
7635 invalidate (dest, VOIDmode);
7636 else if (GET_CODE (dest) == STRICT_LOW_PART
7637 || GET_CODE (dest) == ZERO_EXTRACT)
7638 invalidate (XEXP (dest, 0), GET_MODE (dest));
7641 /* A volatile ASM invalidates everything. */
7642 if (GET_CODE (insn) == INSN
7643 && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
7644 && MEM_VOLATILE_P (PATTERN (insn)))
7645 flush_hash_table ();
7647 /* Make sure registers mentioned in destinations
7648 are safe for use in an expression to be inserted.
7649 This removes from the hash table
7650 any invalid entry that refers to one of these registers.
7652 We don't care about the return value from mention_regs because
7653 we are going to hash the SET_DEST values unconditionally. */
7655 for (i = 0; i < n_sets; i++)
7657 if (sets[i].rtl)
7659 rtx x = SET_DEST (sets[i].rtl);
7661 if (GET_CODE (x) != REG)
7662 mention_regs (x);
7663 else
7665 /* We used to rely on all references to a register becoming
7666 inaccessible when a register changes to a new quantity,
7667 since that changes the hash code. However, that is not
7668 safe, since after NBUCKETS new quantities we get a
7669 hash 'collision' of a register with its own invalid
7670 entries. And since SUBREGs have been changed not to
7671 change their hash code with the hash code of the register,
7672 it wouldn't work any longer at all. So we have to check
7673 for any invalid references lying around now.
7674 This code is similar to the REG case in mention_regs,
7675 but it knows that reg_tick has been incremented, and
7676 it leaves reg_in_table as -1 . */
7677 register int regno = REGNO (x);
7678 register int endregno
7679 = regno + (regno >= FIRST_PSEUDO_REGISTER ? 1
7680 : HARD_REGNO_NREGS (regno, GET_MODE (x)));
7681 int i;
7683 for (i = regno; i < endregno; i++)
7685 if (REG_IN_TABLE (i) >= 0)
7687 remove_invalid_refs (i);
7688 REG_IN_TABLE (i) = -1;
7695 /* We may have just removed some of the src_elt's from the hash table.
7696 So replace each one with the current head of the same class. */
7698 for (i = 0; i < n_sets; i++)
7699 if (sets[i].rtl)
7701 if (sets[i].src_elt && sets[i].src_elt->first_same_value == 0)
7702 /* If elt was removed, find current head of same class,
7703 or 0 if nothing remains of that class. */
7705 register struct table_elt *elt = sets[i].src_elt;
7707 while (elt && elt->prev_same_value)
7708 elt = elt->prev_same_value;
7710 while (elt && elt->first_same_value == 0)
7711 elt = elt->next_same_value;
7712 sets[i].src_elt = elt ? elt->first_same_value : 0;
7716 /* Now insert the destinations into their equivalence classes. */
7718 for (i = 0; i < n_sets; i++)
7719 if (sets[i].rtl)
7721 register rtx dest = SET_DEST (sets[i].rtl);
7722 rtx inner_dest = sets[i].inner_dest;
7723 register struct table_elt *elt;
7725 /* Don't record value if we are not supposed to risk allocating
7726 floating-point values in registers that might be wider than
7727 memory. */
7728 if ((flag_float_store
7729 && GET_CODE (dest) == MEM
7730 && FLOAT_MODE_P (GET_MODE (dest)))
7731 /* Don't record BLKmode values, because we don't know the
7732 size of it, and can't be sure that other BLKmode values
7733 have the same or smaller size. */
7734 || GET_MODE (dest) == BLKmode
7735 /* Don't record values of destinations set inside a libcall block
7736 since we might delete the libcall. Things should have been set
7737 up so we won't want to reuse such a value, but we play it safe
7738 here. */
7739 || libcall_insn
7740 /* If we didn't put a REG_EQUAL value or a source into the hash
7741 table, there is no point is recording DEST. */
7742 || sets[i].src_elt == 0
7743 /* If DEST is a paradoxical SUBREG and SRC is a ZERO_EXTEND
7744 or SIGN_EXTEND, don't record DEST since it can cause
7745 some tracking to be wrong.
7747 ??? Think about this more later. */
7748 || (GET_CODE (dest) == SUBREG
7749 && (GET_MODE_SIZE (GET_MODE (dest))
7750 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))))
7751 && (GET_CODE (sets[i].src) == SIGN_EXTEND
7752 || GET_CODE (sets[i].src) == ZERO_EXTEND)))
7753 continue;
7755 /* STRICT_LOW_PART isn't part of the value BEING set,
7756 and neither is the SUBREG inside it.
7757 Note that in this case SETS[I].SRC_ELT is really SRC_EQV_ELT. */
7758 if (GET_CODE (dest) == STRICT_LOW_PART)
7759 dest = SUBREG_REG (XEXP (dest, 0));
7761 if (GET_CODE (dest) == REG || GET_CODE (dest) == SUBREG)
7762 /* Registers must also be inserted into chains for quantities. */
7763 if (insert_regs (dest, sets[i].src_elt, 1))
7765 /* If `insert_regs' changes something, the hash code must be
7766 recalculated. */
7767 rehash_using_reg (dest);
7768 sets[i].dest_hash = HASH (dest, GET_MODE (dest));
7771 if (GET_CODE (inner_dest) == MEM
7772 && GET_CODE (XEXP (inner_dest, 0)) == ADDRESSOF)
7773 /* Given (SET (MEM (ADDRESSOF (X))) Y) we don't want to say
7774 that (MEM (ADDRESSOF (X))) is equivalent to Y.
7775 Consider the case in which the address of the MEM is
7776 passed to a function, which alters the MEM. Then, if we
7777 later use Y instead of the MEM we'll miss the update. */
7778 elt = insert (dest, 0, sets[i].dest_hash, GET_MODE (dest));
7779 else
7780 elt = insert (dest, sets[i].src_elt,
7781 sets[i].dest_hash, GET_MODE (dest));
7783 elt->in_memory = (GET_CODE (sets[i].inner_dest) == MEM
7784 && (! RTX_UNCHANGING_P (sets[i].inner_dest)
7785 || FIXED_BASE_PLUS_P (XEXP (sets[i].inner_dest,
7786 0))));
7788 if (elt->in_memory)
7790 /* This implicitly assumes a whole struct
7791 need not have MEM_IN_STRUCT_P.
7792 But a whole struct is *supposed* to have MEM_IN_STRUCT_P. */
7793 elt->in_struct = (MEM_IN_STRUCT_P (sets[i].inner_dest)
7794 || sets[i].inner_dest != SET_DEST (sets[i].rtl));
7797 /* If we have (set (subreg:m1 (reg:m2 foo) 0) (bar:m1)), M1 is no
7798 narrower than M2, and both M1 and M2 are the same number of words,
7799 we are also doing (set (reg:m2 foo) (subreg:m2 (bar:m1) 0)) so
7800 make that equivalence as well.
7802 However, BAR may have equivalences for which gen_lowpart_if_possible
7803 will produce a simpler value than gen_lowpart_if_possible applied to
7804 BAR (e.g., if BAR was ZERO_EXTENDed from M2), so we will scan all
7805 BAR's equivalences. If we don't get a simplified form, make
7806 the SUBREG. It will not be used in an equivalence, but will
7807 cause two similar assignments to be detected.
7809 Note the loop below will find SUBREG_REG (DEST) since we have
7810 already entered SRC and DEST of the SET in the table. */
7812 if (GET_CODE (dest) == SUBREG
7813 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))) - 1)
7814 / UNITS_PER_WORD)
7815 == (GET_MODE_SIZE (GET_MODE (dest)) - 1)/ UNITS_PER_WORD)
7816 && (GET_MODE_SIZE (GET_MODE (dest))
7817 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))))
7818 && sets[i].src_elt != 0)
7820 enum machine_mode new_mode = GET_MODE (SUBREG_REG (dest));
7821 struct table_elt *elt, *classp = 0;
7823 for (elt = sets[i].src_elt->first_same_value; elt;
7824 elt = elt->next_same_value)
7826 rtx new_src = 0;
7827 unsigned src_hash;
7828 struct table_elt *src_elt;
7830 /* Ignore invalid entries. */
7831 if (GET_CODE (elt->exp) != REG
7832 && ! exp_equiv_p (elt->exp, elt->exp, 1, 0))
7833 continue;
7835 new_src = gen_lowpart_if_possible (new_mode, elt->exp);
7836 if (new_src == 0)
7837 new_src = gen_rtx_SUBREG (new_mode, elt->exp, 0);
7839 src_hash = HASH (new_src, new_mode);
7840 src_elt = lookup (new_src, src_hash, new_mode);
7842 /* Put the new source in the hash table is if isn't
7843 already. */
7844 if (src_elt == 0)
7846 if (insert_regs (new_src, classp, 0))
7848 rehash_using_reg (new_src);
7849 src_hash = HASH (new_src, new_mode);
7851 src_elt = insert (new_src, classp, src_hash, new_mode);
7852 src_elt->in_memory = elt->in_memory;
7853 src_elt->in_struct = elt->in_struct;
7855 else if (classp && classp != src_elt->first_same_value)
7856 /* Show that two things that we've seen before are
7857 actually the same. */
7858 merge_equiv_classes (src_elt, classp);
7860 classp = src_elt->first_same_value;
7861 /* Ignore invalid entries. */
7862 while (classp
7863 && GET_CODE (classp->exp) != REG
7864 && ! exp_equiv_p (classp->exp, classp->exp, 1, 0))
7865 classp = classp->next_same_value;
7870 /* Special handling for (set REG0 REG1)
7871 where REG0 is the "cheapest", cheaper than REG1.
7872 After cse, REG1 will probably not be used in the sequel,
7873 so (if easily done) change this insn to (set REG1 REG0) and
7874 replace REG1 with REG0 in the previous insn that computed their value.
7875 Then REG1 will become a dead store and won't cloud the situation
7876 for later optimizations.
7878 Do not make this change if REG1 is a hard register, because it will
7879 then be used in the sequel and we may be changing a two-operand insn
7880 into a three-operand insn.
7882 Also do not do this if we are operating on a copy of INSN.
7884 Also don't do this if INSN ends a libcall; this would cause an unrelated
7885 register to be set in the middle of a libcall, and we then get bad code
7886 if the libcall is deleted. */
7888 if (n_sets == 1 && sets[0].rtl && GET_CODE (SET_DEST (sets[0].rtl)) == REG
7889 && NEXT_INSN (PREV_INSN (insn)) == insn
7890 && GET_CODE (SET_SRC (sets[0].rtl)) == REG
7891 && REGNO (SET_SRC (sets[0].rtl)) >= FIRST_PSEUDO_REGISTER
7892 && REGNO_QTY_VALID_P (REGNO (SET_SRC (sets[0].rtl)))
7893 && (qty_first_reg[REG_QTY (REGNO (SET_SRC (sets[0].rtl)))]
7894 == REGNO (SET_DEST (sets[0].rtl)))
7895 && ! find_reg_note (insn, REG_RETVAL, NULL_RTX))
7897 rtx prev = PREV_INSN (insn);
7898 while (prev && GET_CODE (prev) == NOTE)
7899 prev = PREV_INSN (prev);
7901 if (prev && GET_CODE (prev) == INSN && GET_CODE (PATTERN (prev)) == SET
7902 && SET_DEST (PATTERN (prev)) == SET_SRC (sets[0].rtl))
7904 rtx dest = SET_DEST (sets[0].rtl);
7905 rtx note = find_reg_note (prev, REG_EQUIV, NULL_RTX);
7907 validate_change (prev, & SET_DEST (PATTERN (prev)), dest, 1);
7908 validate_change (insn, & SET_DEST (sets[0].rtl),
7909 SET_SRC (sets[0].rtl), 1);
7910 validate_change (insn, & SET_SRC (sets[0].rtl), dest, 1);
7911 apply_change_group ();
7913 /* If REG1 was equivalent to a constant, REG0 is not. */
7914 if (note)
7915 PUT_REG_NOTE_KIND (note, REG_EQUAL);
7917 /* If there was a REG_WAS_0 note on PREV, remove it. Move
7918 any REG_WAS_0 note on INSN to PREV. */
7919 note = find_reg_note (prev, REG_WAS_0, NULL_RTX);
7920 if (note)
7921 remove_note (prev, note);
7923 note = find_reg_note (insn, REG_WAS_0, NULL_RTX);
7924 if (note)
7926 remove_note (insn, note);
7927 XEXP (note, 1) = REG_NOTES (prev);
7928 REG_NOTES (prev) = note;
7931 /* If INSN has a REG_EQUAL note, and this note mentions REG0,
7932 then we must delete it, because the value in REG0 has changed. */
7933 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
7934 if (note && reg_mentioned_p (dest, XEXP (note, 0)))
7935 remove_note (insn, note);
7939 /* If this is a conditional jump insn, record any known equivalences due to
7940 the condition being tested. */
7942 last_jump_equiv_class = 0;
7943 if (GET_CODE (insn) == JUMP_INSN
7944 && n_sets == 1 && GET_CODE (x) == SET
7945 && GET_CODE (SET_SRC (x)) == IF_THEN_ELSE)
7946 record_jump_equiv (insn, 0);
7948 #ifdef HAVE_cc0
7949 /* If the previous insn set CC0 and this insn no longer references CC0,
7950 delete the previous insn. Here we use the fact that nothing expects CC0
7951 to be valid over an insn, which is true until the final pass. */
7952 if (prev_insn && GET_CODE (prev_insn) == INSN
7953 && (tem = single_set (prev_insn)) != 0
7954 && SET_DEST (tem) == cc0_rtx
7955 && ! reg_mentioned_p (cc0_rtx, x))
7957 PUT_CODE (prev_insn, NOTE);
7958 NOTE_LINE_NUMBER (prev_insn) = NOTE_INSN_DELETED;
7959 NOTE_SOURCE_FILE (prev_insn) = 0;
7962 prev_insn_cc0 = this_insn_cc0;
7963 prev_insn_cc0_mode = this_insn_cc0_mode;
7964 #endif
7966 prev_insn = insn;
7969 /* Remove from the hash table all expressions that reference memory. */
7970 static void
7971 invalidate_memory ()
7973 register int i;
7974 register struct table_elt *p, *next;
7976 for (i = 0; i < NBUCKETS; i++)
7977 for (p = table[i]; p; p = next)
7979 next = p->next_same_hash;
7980 if (p->in_memory)
7981 remove_from_table (p, i);
7985 /* XXX ??? The name of this function bears little resemblance to
7986 what this function actually does. FIXME. */
7987 static int
7988 note_mem_written (addr)
7989 register rtx addr;
7991 /* Pushing or popping the stack invalidates just the stack pointer. */
7992 if ((GET_CODE (addr) == PRE_DEC || GET_CODE (addr) == PRE_INC
7993 || GET_CODE (addr) == POST_DEC || GET_CODE (addr) == POST_INC)
7994 && GET_CODE (XEXP (addr, 0)) == REG
7995 && REGNO (XEXP (addr, 0)) == STACK_POINTER_REGNUM)
7997 if (REG_TICK (STACK_POINTER_REGNUM) >= 0)
7998 REG_TICK (STACK_POINTER_REGNUM)++;
8000 /* This should be *very* rare. */
8001 if (TEST_HARD_REG_BIT (hard_regs_in_table, STACK_POINTER_REGNUM))
8002 invalidate (stack_pointer_rtx, VOIDmode);
8003 return 1;
8005 return 0;
8008 /* Perform invalidation on the basis of everything about an insn
8009 except for invalidating the actual places that are SET in it.
8010 This includes the places CLOBBERed, and anything that might
8011 alias with something that is SET or CLOBBERed.
8013 X is the pattern of the insn. */
8015 static void
8016 invalidate_from_clobbers (x)
8017 rtx x;
8019 if (GET_CODE (x) == CLOBBER)
8021 rtx ref = XEXP (x, 0);
8022 if (ref)
8024 if (GET_CODE (ref) == REG || GET_CODE (ref) == SUBREG
8025 || GET_CODE (ref) == MEM)
8026 invalidate (ref, VOIDmode);
8027 else if (GET_CODE (ref) == STRICT_LOW_PART
8028 || GET_CODE (ref) == ZERO_EXTRACT)
8029 invalidate (XEXP (ref, 0), GET_MODE (ref));
8032 else if (GET_CODE (x) == PARALLEL)
8034 register int i;
8035 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
8037 register rtx y = XVECEXP (x, 0, i);
8038 if (GET_CODE (y) == CLOBBER)
8040 rtx ref = XEXP (y, 0);
8041 if (GET_CODE (ref) == REG || GET_CODE (ref) == SUBREG
8042 || GET_CODE (ref) == MEM)
8043 invalidate (ref, VOIDmode);
8044 else if (GET_CODE (ref) == STRICT_LOW_PART
8045 || GET_CODE (ref) == ZERO_EXTRACT)
8046 invalidate (XEXP (ref, 0), GET_MODE (ref));
8052 /* Process X, part of the REG_NOTES of an insn. Look at any REG_EQUAL notes
8053 and replace any registers in them with either an equivalent constant
8054 or the canonical form of the register. If we are inside an address,
8055 only do this if the address remains valid.
8057 OBJECT is 0 except when within a MEM in which case it is the MEM.
8059 Return the replacement for X. */
8061 static rtx
8062 cse_process_notes (x, object)
8063 rtx x;
8064 rtx object;
8066 enum rtx_code code = GET_CODE (x);
8067 const char *fmt = GET_RTX_FORMAT (code);
8068 int i;
8070 switch (code)
8072 case CONST_INT:
8073 case CONST:
8074 case SYMBOL_REF:
8075 case LABEL_REF:
8076 case CONST_DOUBLE:
8077 case PC:
8078 case CC0:
8079 case LO_SUM:
8080 return x;
8082 case MEM:
8083 XEXP (x, 0) = cse_process_notes (XEXP (x, 0), x);
8084 return x;
8086 case EXPR_LIST:
8087 case INSN_LIST:
8088 if (REG_NOTE_KIND (x) == REG_EQUAL)
8089 XEXP (x, 0) = cse_process_notes (XEXP (x, 0), NULL_RTX);
8090 if (XEXP (x, 1))
8091 XEXP (x, 1) = cse_process_notes (XEXP (x, 1), NULL_RTX);
8092 return x;
8094 case SIGN_EXTEND:
8095 case ZERO_EXTEND:
8096 case SUBREG:
8098 rtx new = cse_process_notes (XEXP (x, 0), object);
8099 /* We don't substitute VOIDmode constants into these rtx,
8100 since they would impede folding. */
8101 if (GET_MODE (new) != VOIDmode)
8102 validate_change (object, &XEXP (x, 0), new, 0);
8103 return x;
8106 case REG:
8107 i = REG_QTY (REGNO (x));
8109 /* Return a constant or a constant register. */
8110 if (REGNO_QTY_VALID_P (REGNO (x))
8111 && qty_const[i] != 0
8112 && (CONSTANT_P (qty_const[i])
8113 || GET_CODE (qty_const[i]) == REG))
8115 rtx new = gen_lowpart_if_possible (GET_MODE (x), qty_const[i]);
8116 if (new)
8117 return new;
8120 /* Otherwise, canonicalize this register. */
8121 return canon_reg (x, NULL_RTX);
8123 default:
8124 break;
8127 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8128 if (fmt[i] == 'e')
8129 validate_change (object, &XEXP (x, i),
8130 cse_process_notes (XEXP (x, i), object), 0);
8132 return x;
8135 /* Find common subexpressions between the end test of a loop and the beginning
8136 of the loop. LOOP_START is the CODE_LABEL at the start of a loop.
8138 Often we have a loop where an expression in the exit test is used
8139 in the body of the loop. For example "while (*p) *q++ = *p++;".
8140 Because of the way we duplicate the loop exit test in front of the loop,
8141 however, we don't detect that common subexpression. This will be caught
8142 when global cse is implemented, but this is a quite common case.
8144 This function handles the most common cases of these common expressions.
8145 It is called after we have processed the basic block ending with the
8146 NOTE_INSN_LOOP_END note that ends a loop and the previous JUMP_INSN
8147 jumps to a label used only once. */
8149 static void
8150 cse_around_loop (loop_start)
8151 rtx loop_start;
8153 rtx insn;
8154 int i;
8155 struct table_elt *p;
8157 /* If the jump at the end of the loop doesn't go to the start, we don't
8158 do anything. */
8159 for (insn = PREV_INSN (loop_start);
8160 insn && (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) >= 0);
8161 insn = PREV_INSN (insn))
8164 if (insn == 0
8165 || GET_CODE (insn) != NOTE
8166 || NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG)
8167 return;
8169 /* If the last insn of the loop (the end test) was an NE comparison,
8170 we will interpret it as an EQ comparison, since we fell through
8171 the loop. Any equivalences resulting from that comparison are
8172 therefore not valid and must be invalidated. */
8173 if (last_jump_equiv_class)
8174 for (p = last_jump_equiv_class->first_same_value; p;
8175 p = p->next_same_value)
8177 if (GET_CODE (p->exp) == MEM || GET_CODE (p->exp) == REG
8178 || (GET_CODE (p->exp) == SUBREG
8179 && GET_CODE (SUBREG_REG (p->exp)) == REG))
8180 invalidate (p->exp, VOIDmode);
8181 else if (GET_CODE (p->exp) == STRICT_LOW_PART
8182 || GET_CODE (p->exp) == ZERO_EXTRACT)
8183 invalidate (XEXP (p->exp, 0), GET_MODE (p->exp));
8186 /* Process insns starting after LOOP_START until we hit a CALL_INSN or
8187 a CODE_LABEL (we could handle a CALL_INSN, but it isn't worth it).
8189 The only thing we do with SET_DEST is invalidate entries, so we
8190 can safely process each SET in order. It is slightly less efficient
8191 to do so, but we only want to handle the most common cases.
8193 The gen_move_insn call in cse_set_around_loop may create new pseudos.
8194 These pseudos won't have valid entries in any of the tables indexed
8195 by register number, such as reg_qty. We avoid out-of-range array
8196 accesses by not processing any instructions created after cse started. */
8198 for (insn = NEXT_INSN (loop_start);
8199 GET_CODE (insn) != CALL_INSN && GET_CODE (insn) != CODE_LABEL
8200 && INSN_UID (insn) < max_insn_uid
8201 && ! (GET_CODE (insn) == NOTE
8202 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END);
8203 insn = NEXT_INSN (insn))
8205 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
8206 && (GET_CODE (PATTERN (insn)) == SET
8207 || GET_CODE (PATTERN (insn)) == CLOBBER))
8208 cse_set_around_loop (PATTERN (insn), insn, loop_start);
8209 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
8210 && GET_CODE (PATTERN (insn)) == PARALLEL)
8211 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
8212 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET
8213 || GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == CLOBBER)
8214 cse_set_around_loop (XVECEXP (PATTERN (insn), 0, i), insn,
8215 loop_start);
8219 /* Process one SET of an insn that was skipped. We ignore CLOBBERs
8220 since they are done elsewhere. This function is called via note_stores. */
8222 static void
8223 invalidate_skipped_set (dest, set)
8224 rtx set;
8225 rtx dest;
8227 enum rtx_code code = GET_CODE (dest);
8229 if (code == MEM
8230 && ! note_mem_written (dest) /* If this is not a stack push ... */
8231 /* There are times when an address can appear varying and be a PLUS
8232 during this scan when it would be a fixed address were we to know
8233 the proper equivalences. So invalidate all memory if there is
8234 a BLKmode or nonscalar memory reference or a reference to a
8235 variable address. */
8236 && (MEM_IN_STRUCT_P (dest) || GET_MODE (dest) == BLKmode
8237 || cse_rtx_varies_p (XEXP (dest, 0))))
8239 invalidate_memory ();
8240 return;
8243 if (GET_CODE (set) == CLOBBER
8244 #ifdef HAVE_cc0
8245 || dest == cc0_rtx
8246 #endif
8247 || dest == pc_rtx)
8248 return;
8250 if (code == STRICT_LOW_PART || code == ZERO_EXTRACT)
8251 invalidate (XEXP (dest, 0), GET_MODE (dest));
8252 else if (code == REG || code == SUBREG || code == MEM)
8253 invalidate (dest, VOIDmode);
8256 /* Invalidate all insns from START up to the end of the function or the
8257 next label. This called when we wish to CSE around a block that is
8258 conditionally executed. */
8260 static void
8261 invalidate_skipped_block (start)
8262 rtx start;
8264 rtx insn;
8266 for (insn = start; insn && GET_CODE (insn) != CODE_LABEL;
8267 insn = NEXT_INSN (insn))
8269 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
8270 continue;
8272 if (GET_CODE (insn) == CALL_INSN)
8274 if (! CONST_CALL_P (insn))
8275 invalidate_memory ();
8276 invalidate_for_call ();
8279 invalidate_from_clobbers (PATTERN (insn));
8280 note_stores (PATTERN (insn), invalidate_skipped_set);
8284 /* Used for communication between the following two routines; contains a
8285 value to be checked for modification. */
8287 static rtx cse_check_loop_start_value;
8289 /* If modifying X will modify the value in CSE_CHECK_LOOP_START_VALUE,
8290 indicate that fact by setting CSE_CHECK_LOOP_START_VALUE to 0. */
8292 static void
8293 cse_check_loop_start (x, set)
8294 rtx x;
8295 rtx set ATTRIBUTE_UNUSED;
8297 if (cse_check_loop_start_value == 0
8298 || GET_CODE (x) == CC0 || GET_CODE (x) == PC)
8299 return;
8301 if ((GET_CODE (x) == MEM && GET_CODE (cse_check_loop_start_value) == MEM)
8302 || reg_overlap_mentioned_p (x, cse_check_loop_start_value))
8303 cse_check_loop_start_value = 0;
8306 /* X is a SET or CLOBBER contained in INSN that was found near the start of
8307 a loop that starts with the label at LOOP_START.
8309 If X is a SET, we see if its SET_SRC is currently in our hash table.
8310 If so, we see if it has a value equal to some register used only in the
8311 loop exit code (as marked by jump.c).
8313 If those two conditions are true, we search backwards from the start of
8314 the loop to see if that same value was loaded into a register that still
8315 retains its value at the start of the loop.
8317 If so, we insert an insn after the load to copy the destination of that
8318 load into the equivalent register and (try to) replace our SET_SRC with that
8319 register.
8321 In any event, we invalidate whatever this SET or CLOBBER modifies. */
8323 static void
8324 cse_set_around_loop (x, insn, loop_start)
8325 rtx x;
8326 rtx insn;
8327 rtx loop_start;
8329 struct table_elt *src_elt;
8331 /* If this is a SET, see if we can replace SET_SRC, but ignore SETs that
8332 are setting PC or CC0 or whose SET_SRC is already a register. */
8333 if (GET_CODE (x) == SET
8334 && GET_CODE (SET_DEST (x)) != PC && GET_CODE (SET_DEST (x)) != CC0
8335 && GET_CODE (SET_SRC (x)) != REG)
8337 src_elt = lookup (SET_SRC (x),
8338 HASH (SET_SRC (x), GET_MODE (SET_DEST (x))),
8339 GET_MODE (SET_DEST (x)));
8341 if (src_elt)
8342 for (src_elt = src_elt->first_same_value; src_elt;
8343 src_elt = src_elt->next_same_value)
8344 if (GET_CODE (src_elt->exp) == REG && REG_LOOP_TEST_P (src_elt->exp)
8345 && COST (src_elt->exp) < COST (SET_SRC (x)))
8347 rtx p, set;
8349 /* Look for an insn in front of LOOP_START that sets
8350 something in the desired mode to SET_SRC (x) before we hit
8351 a label or CALL_INSN. */
8353 for (p = prev_nonnote_insn (loop_start);
8354 p && GET_CODE (p) != CALL_INSN
8355 && GET_CODE (p) != CODE_LABEL;
8356 p = prev_nonnote_insn (p))
8357 if ((set = single_set (p)) != 0
8358 && GET_CODE (SET_DEST (set)) == REG
8359 && GET_MODE (SET_DEST (set)) == src_elt->mode
8360 && rtx_equal_p (SET_SRC (set), SET_SRC (x)))
8362 /* We now have to ensure that nothing between P
8363 and LOOP_START modified anything referenced in
8364 SET_SRC (x). We know that nothing within the loop
8365 can modify it, or we would have invalidated it in
8366 the hash table. */
8367 rtx q;
8369 cse_check_loop_start_value = SET_SRC (x);
8370 for (q = p; q != loop_start; q = NEXT_INSN (q))
8371 if (GET_RTX_CLASS (GET_CODE (q)) == 'i')
8372 note_stores (PATTERN (q), cse_check_loop_start);
8374 /* If nothing was changed and we can replace our
8375 SET_SRC, add an insn after P to copy its destination
8376 to what we will be replacing SET_SRC with. */
8377 if (cse_check_loop_start_value
8378 && validate_change (insn, &SET_SRC (x),
8379 src_elt->exp, 0))
8381 /* If this creates new pseudos, this is unsafe,
8382 because the regno of new pseudo is unsuitable
8383 to index into reg_qty when cse_insn processes
8384 the new insn. Therefore, if a new pseudo was
8385 created, discard this optimization. */
8386 int nregs = max_reg_num ();
8387 rtx move
8388 = gen_move_insn (src_elt->exp, SET_DEST (set));
8389 if (nregs != max_reg_num ())
8391 if (! validate_change (insn, &SET_SRC (x),
8392 SET_SRC (set), 0))
8393 abort ();
8395 else
8396 emit_insn_after (move, p);
8398 break;
8403 /* Now invalidate anything modified by X. */
8404 note_mem_written (SET_DEST (x));
8406 /* See comment on similar code in cse_insn for explanation of these tests. */
8407 if (GET_CODE (SET_DEST (x)) == REG || GET_CODE (SET_DEST (x)) == SUBREG
8408 || GET_CODE (SET_DEST (x)) == MEM)
8409 invalidate (SET_DEST (x), VOIDmode);
8410 else if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
8411 || GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
8412 invalidate (XEXP (SET_DEST (x), 0), GET_MODE (SET_DEST (x)));
8415 /* Find the end of INSN's basic block and return its range,
8416 the total number of SETs in all the insns of the block, the last insn of the
8417 block, and the branch path.
8419 The branch path indicates which branches should be followed. If a non-zero
8420 path size is specified, the block should be rescanned and a different set
8421 of branches will be taken. The branch path is only used if
8422 FLAG_CSE_FOLLOW_JUMPS or FLAG_CSE_SKIP_BLOCKS is non-zero.
8424 DATA is a pointer to a struct cse_basic_block_data, defined below, that is
8425 used to describe the block. It is filled in with the information about
8426 the current block. The incoming structure's branch path, if any, is used
8427 to construct the output branch path. */
8429 void
8430 cse_end_of_basic_block (insn, data, follow_jumps, after_loop, skip_blocks)
8431 rtx insn;
8432 struct cse_basic_block_data *data;
8433 int follow_jumps;
8434 int after_loop;
8435 int skip_blocks;
8437 rtx p = insn, q;
8438 int nsets = 0;
8439 int low_cuid = INSN_CUID (insn), high_cuid = INSN_CUID (insn);
8440 rtx next = GET_RTX_CLASS (GET_CODE (insn)) == 'i' ? insn : next_real_insn (insn);
8441 int path_size = data->path_size;
8442 int path_entry = 0;
8443 int i;
8445 /* Update the previous branch path, if any. If the last branch was
8446 previously TAKEN, mark it NOT_TAKEN. If it was previously NOT_TAKEN,
8447 shorten the path by one and look at the previous branch. We know that
8448 at least one branch must have been taken if PATH_SIZE is non-zero. */
8449 while (path_size > 0)
8451 if (data->path[path_size - 1].status != NOT_TAKEN)
8453 data->path[path_size - 1].status = NOT_TAKEN;
8454 break;
8456 else
8457 path_size--;
8460 /* Scan to end of this basic block. */
8461 while (p && GET_CODE (p) != CODE_LABEL)
8463 /* Don't cse out the end of a loop. This makes a difference
8464 only for the unusual loops that always execute at least once;
8465 all other loops have labels there so we will stop in any case.
8466 Cse'ing out the end of the loop is dangerous because it
8467 might cause an invariant expression inside the loop
8468 to be reused after the end of the loop. This would make it
8469 hard to move the expression out of the loop in loop.c,
8470 especially if it is one of several equivalent expressions
8471 and loop.c would like to eliminate it.
8473 If we are running after loop.c has finished, we can ignore
8474 the NOTE_INSN_LOOP_END. */
8476 if (! after_loop && GET_CODE (p) == NOTE
8477 && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
8478 break;
8480 /* Don't cse over a call to setjmp; on some machines (eg vax)
8481 the regs restored by the longjmp come from
8482 a later time than the setjmp. */
8483 if (GET_CODE (p) == NOTE
8484 && NOTE_LINE_NUMBER (p) == NOTE_INSN_SETJMP)
8485 break;
8487 /* A PARALLEL can have lots of SETs in it,
8488 especially if it is really an ASM_OPERANDS. */
8489 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
8490 && GET_CODE (PATTERN (p)) == PARALLEL)
8491 nsets += XVECLEN (PATTERN (p), 0);
8492 else if (GET_CODE (p) != NOTE)
8493 nsets += 1;
8495 /* Ignore insns made by CSE; they cannot affect the boundaries of
8496 the basic block. */
8498 if (INSN_UID (p) <= max_uid && INSN_CUID (p) > high_cuid)
8499 high_cuid = INSN_CUID (p);
8500 if (INSN_UID (p) <= max_uid && INSN_CUID (p) < low_cuid)
8501 low_cuid = INSN_CUID (p);
8503 /* See if this insn is in our branch path. If it is and we are to
8504 take it, do so. */
8505 if (path_entry < path_size && data->path[path_entry].branch == p)
8507 if (data->path[path_entry].status != NOT_TAKEN)
8508 p = JUMP_LABEL (p);
8510 /* Point to next entry in path, if any. */
8511 path_entry++;
8514 /* If this is a conditional jump, we can follow it if -fcse-follow-jumps
8515 was specified, we haven't reached our maximum path length, there are
8516 insns following the target of the jump, this is the only use of the
8517 jump label, and the target label is preceded by a BARRIER.
8519 Alternatively, we can follow the jump if it branches around a
8520 block of code and there are no other branches into the block.
8521 In this case invalidate_skipped_block will be called to invalidate any
8522 registers set in the block when following the jump. */
8524 else if ((follow_jumps || skip_blocks) && path_size < PATHLENGTH - 1
8525 && GET_CODE (p) == JUMP_INSN
8526 && GET_CODE (PATTERN (p)) == SET
8527 && GET_CODE (SET_SRC (PATTERN (p))) == IF_THEN_ELSE
8528 && JUMP_LABEL (p) != 0
8529 && LABEL_NUSES (JUMP_LABEL (p)) == 1
8530 && NEXT_INSN (JUMP_LABEL (p)) != 0)
8532 for (q = PREV_INSN (JUMP_LABEL (p)); q; q = PREV_INSN (q))
8533 if ((GET_CODE (q) != NOTE
8534 || NOTE_LINE_NUMBER (q) == NOTE_INSN_LOOP_END
8535 || NOTE_LINE_NUMBER (q) == NOTE_INSN_SETJMP)
8536 && (GET_CODE (q) != CODE_LABEL || LABEL_NUSES (q) != 0))
8537 break;
8539 /* If we ran into a BARRIER, this code is an extension of the
8540 basic block when the branch is taken. */
8541 if (follow_jumps && q != 0 && GET_CODE (q) == BARRIER)
8543 /* Don't allow ourself to keep walking around an
8544 always-executed loop. */
8545 if (next_real_insn (q) == next)
8547 p = NEXT_INSN (p);
8548 continue;
8551 /* Similarly, don't put a branch in our path more than once. */
8552 for (i = 0; i < path_entry; i++)
8553 if (data->path[i].branch == p)
8554 break;
8556 if (i != path_entry)
8557 break;
8559 data->path[path_entry].branch = p;
8560 data->path[path_entry++].status = TAKEN;
8562 /* This branch now ends our path. It was possible that we
8563 didn't see this branch the last time around (when the
8564 insn in front of the target was a JUMP_INSN that was
8565 turned into a no-op). */
8566 path_size = path_entry;
8568 p = JUMP_LABEL (p);
8569 /* Mark block so we won't scan it again later. */
8570 PUT_MODE (NEXT_INSN (p), QImode);
8572 /* Detect a branch around a block of code. */
8573 else if (skip_blocks && q != 0 && GET_CODE (q) != CODE_LABEL)
8575 register rtx tmp;
8577 if (next_real_insn (q) == next)
8579 p = NEXT_INSN (p);
8580 continue;
8583 for (i = 0; i < path_entry; i++)
8584 if (data->path[i].branch == p)
8585 break;
8587 if (i != path_entry)
8588 break;
8590 /* This is no_labels_between_p (p, q) with an added check for
8591 reaching the end of a function (in case Q precedes P). */
8592 for (tmp = NEXT_INSN (p); tmp && tmp != q; tmp = NEXT_INSN (tmp))
8593 if (GET_CODE (tmp) == CODE_LABEL)
8594 break;
8596 if (tmp == q)
8598 data->path[path_entry].branch = p;
8599 data->path[path_entry++].status = AROUND;
8601 path_size = path_entry;
8603 p = JUMP_LABEL (p);
8604 /* Mark block so we won't scan it again later. */
8605 PUT_MODE (NEXT_INSN (p), QImode);
8609 p = NEXT_INSN (p);
8612 data->low_cuid = low_cuid;
8613 data->high_cuid = high_cuid;
8614 data->nsets = nsets;
8615 data->last = p;
8617 /* If all jumps in the path are not taken, set our path length to zero
8618 so a rescan won't be done. */
8619 for (i = path_size - 1; i >= 0; i--)
8620 if (data->path[i].status != NOT_TAKEN)
8621 break;
8623 if (i == -1)
8624 data->path_size = 0;
8625 else
8626 data->path_size = path_size;
8628 /* End the current branch path. */
8629 data->path[path_size].branch = 0;
8632 /* Perform cse on the instructions of a function.
8633 F is the first instruction.
8634 NREGS is one plus the highest pseudo-reg number used in the instruction.
8636 AFTER_LOOP is 1 if this is the cse call done after loop optimization
8637 (only if -frerun-cse-after-loop).
8639 Returns 1 if jump_optimize should be redone due to simplifications
8640 in conditional jump instructions. */
8643 cse_main (f, nregs, after_loop, file)
8644 rtx f;
8645 int nregs;
8646 int after_loop;
8647 FILE *file;
8649 struct cse_basic_block_data val;
8650 register rtx insn = f;
8651 register int i;
8653 cse_jumps_altered = 0;
8654 recorded_label_ref = 0;
8655 constant_pool_entries_cost = 0;
8656 val.path_size = 0;
8658 init_recog ();
8659 init_alias_analysis ();
8661 max_reg = nregs;
8663 max_insn_uid = get_max_uid ();
8665 reg_next_eqv = (int *) alloca (nregs * sizeof (int));
8666 reg_prev_eqv = (int *) alloca (nregs * sizeof (int));
8668 #ifdef LOAD_EXTEND_OP
8670 /* Allocate scratch rtl here. cse_insn will fill in the memory reference
8671 and change the code and mode as appropriate. */
8672 memory_extend_rtx = gen_rtx_ZERO_EXTEND (VOIDmode, NULL_RTX);
8673 #endif
8675 /* Discard all the free elements of the previous function
8676 since they are allocated in the temporarily obstack. */
8677 bzero ((char *) table, sizeof table);
8678 free_element_chain = 0;
8679 n_elements_made = 0;
8681 /* Find the largest uid. */
8683 max_uid = get_max_uid ();
8684 uid_cuid = (int *) alloca ((max_uid + 1) * sizeof (int));
8685 bzero ((char *) uid_cuid, (max_uid + 1) * sizeof (int));
8687 /* Compute the mapping from uids to cuids.
8688 CUIDs are numbers assigned to insns, like uids,
8689 except that cuids increase monotonically through the code.
8690 Don't assign cuids to line-number NOTEs, so that the distance in cuids
8691 between two insns is not affected by -g. */
8693 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
8695 if (GET_CODE (insn) != NOTE
8696 || NOTE_LINE_NUMBER (insn) < 0)
8697 INSN_CUID (insn) = ++i;
8698 else
8699 /* Give a line number note the same cuid as preceding insn. */
8700 INSN_CUID (insn) = i;
8703 /* Initialize which registers are clobbered by calls. */
8705 CLEAR_HARD_REG_SET (regs_invalidated_by_call);
8707 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8708 if ((call_used_regs[i]
8709 /* Used to check !fixed_regs[i] here, but that isn't safe;
8710 fixed regs are still call-clobbered, and sched can get
8711 confused if they can "live across calls".
8713 The frame pointer is always preserved across calls. The arg
8714 pointer is if it is fixed. The stack pointer usually is, unless
8715 RETURN_POPS_ARGS, in which case an explicit CLOBBER
8716 will be present. If we are generating PIC code, the PIC offset
8717 table register is preserved across calls. */
8719 && i != STACK_POINTER_REGNUM
8720 && i != FRAME_POINTER_REGNUM
8721 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
8722 && i != HARD_FRAME_POINTER_REGNUM
8723 #endif
8724 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
8725 && ! (i == ARG_POINTER_REGNUM && fixed_regs[i])
8726 #endif
8727 #if defined (PIC_OFFSET_TABLE_REGNUM) && !defined (PIC_OFFSET_TABLE_REG_CALL_CLOBBERED)
8728 && ! (i == PIC_OFFSET_TABLE_REGNUM && flag_pic)
8729 #endif
8731 || global_regs[i])
8732 SET_HARD_REG_BIT (regs_invalidated_by_call, i);
8734 if (ggc_p)
8735 ggc_push_context ();
8737 /* Loop over basic blocks.
8738 Compute the maximum number of qty's needed for each basic block
8739 (which is 2 for each SET). */
8740 insn = f;
8741 while (insn)
8743 cse_end_of_basic_block (insn, &val, flag_cse_follow_jumps, after_loop,
8744 flag_cse_skip_blocks);
8746 /* If this basic block was already processed or has no sets, skip it. */
8747 if (val.nsets == 0 || GET_MODE (insn) == QImode)
8749 PUT_MODE (insn, VOIDmode);
8750 insn = (val.last ? NEXT_INSN (val.last) : 0);
8751 val.path_size = 0;
8752 continue;
8755 cse_basic_block_start = val.low_cuid;
8756 cse_basic_block_end = val.high_cuid;
8757 max_qty = val.nsets * 2;
8759 if (file)
8760 fnotice (file, ";; Processing block from %d to %d, %d sets.\n",
8761 INSN_UID (insn), val.last ? INSN_UID (val.last) : 0,
8762 val.nsets);
8764 /* Make MAX_QTY bigger to give us room to optimize
8765 past the end of this basic block, if that should prove useful. */
8766 if (max_qty < 500)
8767 max_qty = 500;
8769 max_qty += max_reg;
8771 /* If this basic block is being extended by following certain jumps,
8772 (see `cse_end_of_basic_block'), we reprocess the code from the start.
8773 Otherwise, we start after this basic block. */
8774 if (val.path_size > 0)
8775 cse_basic_block (insn, val.last, val.path, 0);
8776 else
8778 int old_cse_jumps_altered = cse_jumps_altered;
8779 rtx temp;
8781 /* When cse changes a conditional jump to an unconditional
8782 jump, we want to reprocess the block, since it will give
8783 us a new branch path to investigate. */
8784 cse_jumps_altered = 0;
8785 temp = cse_basic_block (insn, val.last, val.path, ! after_loop);
8786 if (cse_jumps_altered == 0
8787 || (flag_cse_follow_jumps == 0 && flag_cse_skip_blocks == 0))
8788 insn = temp;
8790 cse_jumps_altered |= old_cse_jumps_altered;
8793 if (ggc_p)
8794 ggc_collect ();
8796 #ifdef USE_C_ALLOCA
8797 alloca (0);
8798 #endif
8801 if (ggc_p)
8802 ggc_pop_context ();
8804 /* Tell refers_to_mem_p that qty_const info is not available. */
8805 qty_const = 0;
8807 if (max_elements_made < n_elements_made)
8808 max_elements_made = n_elements_made;
8810 return cse_jumps_altered || recorded_label_ref;
8813 /* Process a single basic block. FROM and TO and the limits of the basic
8814 block. NEXT_BRANCH points to the branch path when following jumps or
8815 a null path when not following jumps.
8817 AROUND_LOOP is non-zero if we are to try to cse around to the start of a
8818 loop. This is true when we are being called for the last time on a
8819 block and this CSE pass is before loop.c. */
8821 static rtx
8822 cse_basic_block (from, to, next_branch, around_loop)
8823 register rtx from, to;
8824 struct branch_path *next_branch;
8825 int around_loop;
8827 register rtx insn;
8828 int to_usage = 0;
8829 rtx libcall_insn = NULL_RTX;
8830 int num_insns = 0;
8832 /* Each of these arrays is undefined before max_reg, so only allocate
8833 the space actually needed and adjust the start below. */
8835 qty_first_reg = (int *) alloca ((max_qty - max_reg) * sizeof (int));
8836 qty_last_reg = (int *) alloca ((max_qty - max_reg) * sizeof (int));
8837 qty_mode = (enum machine_mode *) alloca ((max_qty - max_reg)
8838 * sizeof (enum machine_mode));
8839 qty_const = (rtx *) alloca ((max_qty - max_reg) * sizeof (rtx));
8840 qty_const_insn = (rtx *) alloca ((max_qty - max_reg) * sizeof (rtx));
8841 qty_comparison_code
8842 = (enum rtx_code *) alloca ((max_qty - max_reg) * sizeof (enum rtx_code));
8843 qty_comparison_qty = (int *) alloca ((max_qty - max_reg) * sizeof (int));
8844 qty_comparison_const = (rtx *) alloca ((max_qty - max_reg) * sizeof (rtx));
8846 qty_first_reg -= max_reg;
8847 qty_last_reg -= max_reg;
8848 qty_mode -= max_reg;
8849 qty_const -= max_reg;
8850 qty_const_insn -= max_reg;
8851 qty_comparison_code -= max_reg;
8852 qty_comparison_qty -= max_reg;
8853 qty_comparison_const -= max_reg;
8855 new_basic_block ();
8857 /* TO might be a label. If so, protect it from being deleted. */
8858 if (to != 0 && GET_CODE (to) == CODE_LABEL)
8859 ++LABEL_NUSES (to);
8861 for (insn = from; insn != to; insn = NEXT_INSN (insn))
8863 register enum rtx_code code = GET_CODE (insn);
8865 /* If we have processed 1,000 insns, flush the hash table to
8866 avoid extreme quadratic behavior. We must not include NOTEs
8867 in the count since there may be more or them when generating
8868 debugging information. If we clear the table at different
8869 times, code generated with -g -O might be different than code
8870 generated with -O but not -g.
8872 ??? This is a real kludge and needs to be done some other way.
8873 Perhaps for 2.9. */
8874 if (code != NOTE && num_insns++ > 1000)
8876 flush_hash_table ();
8877 num_insns = 0;
8880 /* See if this is a branch that is part of the path. If so, and it is
8881 to be taken, do so. */
8882 if (next_branch->branch == insn)
8884 enum taken status = next_branch++->status;
8885 if (status != NOT_TAKEN)
8887 if (status == TAKEN)
8888 record_jump_equiv (insn, 1);
8889 else
8890 invalidate_skipped_block (NEXT_INSN (insn));
8892 /* Set the last insn as the jump insn; it doesn't affect cc0.
8893 Then follow this branch. */
8894 #ifdef HAVE_cc0
8895 prev_insn_cc0 = 0;
8896 #endif
8897 prev_insn = insn;
8898 insn = JUMP_LABEL (insn);
8899 continue;
8903 if (GET_MODE (insn) == QImode)
8904 PUT_MODE (insn, VOIDmode);
8906 if (GET_RTX_CLASS (code) == 'i')
8908 rtx p;
8910 /* Process notes first so we have all notes in canonical forms when
8911 looking for duplicate operations. */
8913 if (REG_NOTES (insn))
8914 REG_NOTES (insn) = cse_process_notes (REG_NOTES (insn), NULL_RTX);
8916 /* Track when we are inside in LIBCALL block. Inside such a block,
8917 we do not want to record destinations. The last insn of a
8918 LIBCALL block is not considered to be part of the block, since
8919 its destination is the result of the block and hence should be
8920 recorded. */
8922 if ((p = find_reg_note (insn, REG_LIBCALL, NULL_RTX)))
8923 libcall_insn = XEXP (p, 0);
8924 else if (find_reg_note (insn, REG_RETVAL, NULL_RTX))
8925 libcall_insn = NULL_RTX;
8927 cse_insn (insn, libcall_insn);
8930 /* If INSN is now an unconditional jump, skip to the end of our
8931 basic block by pretending that we just did the last insn in the
8932 basic block. If we are jumping to the end of our block, show
8933 that we can have one usage of TO. */
8935 if (simplejump_p (insn))
8937 if (to == 0)
8938 return 0;
8940 if (JUMP_LABEL (insn) == to)
8941 to_usage = 1;
8943 /* Maybe TO was deleted because the jump is unconditional.
8944 If so, there is nothing left in this basic block. */
8945 /* ??? Perhaps it would be smarter to set TO
8946 to whatever follows this insn,
8947 and pretend the basic block had always ended here. */
8948 if (INSN_DELETED_P (to))
8949 break;
8951 insn = PREV_INSN (to);
8954 /* See if it is ok to keep on going past the label
8955 which used to end our basic block. Remember that we incremented
8956 the count of that label, so we decrement it here. If we made
8957 a jump unconditional, TO_USAGE will be one; in that case, we don't
8958 want to count the use in that jump. */
8960 if (to != 0 && NEXT_INSN (insn) == to
8961 && GET_CODE (to) == CODE_LABEL && --LABEL_NUSES (to) == to_usage)
8963 struct cse_basic_block_data val;
8964 rtx prev;
8966 insn = NEXT_INSN (to);
8968 if (LABEL_NUSES (to) == 0)
8969 insn = delete_insn (to);
8971 /* If TO was the last insn in the function, we are done. */
8972 if (insn == 0)
8973 return 0;
8975 /* If TO was preceded by a BARRIER we are done with this block
8976 because it has no continuation. */
8977 prev = prev_nonnote_insn (to);
8978 if (prev && GET_CODE (prev) == BARRIER)
8979 return insn;
8981 /* Find the end of the following block. Note that we won't be
8982 following branches in this case. */
8983 to_usage = 0;
8984 val.path_size = 0;
8985 cse_end_of_basic_block (insn, &val, 0, 0, 0);
8987 /* If the tables we allocated have enough space left
8988 to handle all the SETs in the next basic block,
8989 continue through it. Otherwise, return,
8990 and that block will be scanned individually. */
8991 if (val.nsets * 2 + next_qty > max_qty)
8992 break;
8994 cse_basic_block_start = val.low_cuid;
8995 cse_basic_block_end = val.high_cuid;
8996 to = val.last;
8998 /* Prevent TO from being deleted if it is a label. */
8999 if (to != 0 && GET_CODE (to) == CODE_LABEL)
9000 ++LABEL_NUSES (to);
9002 /* Back up so we process the first insn in the extension. */
9003 insn = PREV_INSN (insn);
9007 if (next_qty > max_qty)
9008 abort ();
9010 /* If we are running before loop.c, we stopped on a NOTE_INSN_LOOP_END, and
9011 the previous insn is the only insn that branches to the head of a loop,
9012 we can cse into the loop. Don't do this if we changed the jump
9013 structure of a loop unless we aren't going to be following jumps. */
9015 if ((cse_jumps_altered == 0
9016 || (flag_cse_follow_jumps == 0 && flag_cse_skip_blocks == 0))
9017 && around_loop && to != 0
9018 && GET_CODE (to) == NOTE && NOTE_LINE_NUMBER (to) == NOTE_INSN_LOOP_END
9019 && GET_CODE (PREV_INSN (to)) == JUMP_INSN
9020 && JUMP_LABEL (PREV_INSN (to)) != 0
9021 && LABEL_NUSES (JUMP_LABEL (PREV_INSN (to))) == 1)
9022 cse_around_loop (JUMP_LABEL (PREV_INSN (to)));
9024 return to ? NEXT_INSN (to) : 0;
9027 /* Count the number of times registers are used (not set) in X.
9028 COUNTS is an array in which we accumulate the count, INCR is how much
9029 we count each register usage.
9031 Don't count a usage of DEST, which is the SET_DEST of a SET which
9032 contains X in its SET_SRC. This is because such a SET does not
9033 modify the liveness of DEST. */
9035 static void
9036 count_reg_usage (x, counts, dest, incr)
9037 rtx x;
9038 int *counts;
9039 rtx dest;
9040 int incr;
9042 enum rtx_code code;
9043 const char *fmt;
9044 int i, j;
9046 if (x == 0)
9047 return;
9049 switch (code = GET_CODE (x))
9051 case REG:
9052 if (x != dest)
9053 counts[REGNO (x)] += incr;
9054 return;
9056 case PC:
9057 case CC0:
9058 case CONST:
9059 case CONST_INT:
9060 case CONST_DOUBLE:
9061 case SYMBOL_REF:
9062 case LABEL_REF:
9063 return;
9065 case CLOBBER:
9066 /* If we are clobbering a MEM, mark any registers inside the address
9067 as being used. */
9068 if (GET_CODE (XEXP (x, 0)) == MEM)
9069 count_reg_usage (XEXP (XEXP (x, 0), 0), counts, NULL_RTX, incr);
9070 return;
9072 case SET:
9073 /* Unless we are setting a REG, count everything in SET_DEST. */
9074 if (GET_CODE (SET_DEST (x)) != REG)
9075 count_reg_usage (SET_DEST (x), counts, NULL_RTX, incr);
9077 /* If SRC has side-effects, then we can't delete this insn, so the
9078 usage of SET_DEST inside SRC counts.
9080 ??? Strictly-speaking, we might be preserving this insn
9081 because some other SET has side-effects, but that's hard
9082 to do and can't happen now. */
9083 count_reg_usage (SET_SRC (x), counts,
9084 side_effects_p (SET_SRC (x)) ? NULL_RTX : SET_DEST (x),
9085 incr);
9086 return;
9088 case CALL_INSN:
9089 count_reg_usage (CALL_INSN_FUNCTION_USAGE (x), counts, NULL_RTX, incr);
9091 /* ... falls through ... */
9092 case INSN:
9093 case JUMP_INSN:
9094 count_reg_usage (PATTERN (x), counts, NULL_RTX, incr);
9096 /* Things used in a REG_EQUAL note aren't dead since loop may try to
9097 use them. */
9099 count_reg_usage (REG_NOTES (x), counts, NULL_RTX, incr);
9100 return;
9102 case EXPR_LIST:
9103 case INSN_LIST:
9104 if (REG_NOTE_KIND (x) == REG_EQUAL
9105 || (REG_NOTE_KIND (x) != REG_NONNEG && GET_CODE (XEXP (x,0)) == USE))
9106 count_reg_usage (XEXP (x, 0), counts, NULL_RTX, incr);
9107 count_reg_usage (XEXP (x, 1), counts, NULL_RTX, incr);
9108 return;
9110 default:
9111 break;
9114 fmt = GET_RTX_FORMAT (code);
9115 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9117 if (fmt[i] == 'e')
9118 count_reg_usage (XEXP (x, i), counts, dest, incr);
9119 else if (fmt[i] == 'E')
9120 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9121 count_reg_usage (XVECEXP (x, i, j), counts, dest, incr);
9125 /* Scan all the insns and delete any that are dead; i.e., they store a register
9126 that is never used or they copy a register to itself.
9128 This is used to remove insns made obviously dead by cse, loop or other
9129 optimizations. It improves the heuristics in loop since it won't try to
9130 move dead invariants out of loops or make givs for dead quantities. The
9131 remaining passes of the compilation are also sped up. */
9133 void
9134 delete_trivially_dead_insns (insns, nreg)
9135 rtx insns;
9136 int nreg;
9138 int *counts = (int *) alloca (nreg * sizeof (int));
9139 rtx insn, prev;
9140 #ifdef HAVE_cc0
9141 rtx tem;
9142 #endif
9143 int i;
9144 int in_libcall = 0, dead_libcall = 0;
9146 /* First count the number of times each register is used. */
9147 bzero ((char *) counts, sizeof (int) * nreg);
9148 for (insn = next_real_insn (insns); insn; insn = next_real_insn (insn))
9149 count_reg_usage (insn, counts, NULL_RTX, 1);
9151 /* Go from the last insn to the first and delete insns that only set unused
9152 registers or copy a register to itself. As we delete an insn, remove
9153 usage counts for registers it uses.
9155 The first jump optimization pass may leave a real insn as the last
9156 insn in the function. We must not skip that insn or we may end
9157 up deleting code that is not really dead. */
9158 insn = get_last_insn ();
9159 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
9160 insn = prev_real_insn (insn);
9162 for ( ; insn; insn = prev)
9164 int live_insn = 0;
9165 rtx note;
9167 prev = prev_real_insn (insn);
9169 /* Don't delete any insns that are part of a libcall block unless
9170 we can delete the whole libcall block.
9172 Flow or loop might get confused if we did that. Remember
9173 that we are scanning backwards. */
9174 if (find_reg_note (insn, REG_RETVAL, NULL_RTX))
9176 in_libcall = 1;
9177 live_insn = 1;
9178 dead_libcall = 0;
9180 /* See if there's a REG_EQUAL note on this insn and try to
9181 replace the source with the REG_EQUAL expression.
9183 We assume that insns with REG_RETVALs can only be reg->reg
9184 copies at this point. */
9185 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
9186 if (note)
9188 rtx set = single_set (insn);
9189 if (set
9190 && validate_change (insn, &SET_SRC (set), XEXP (note, 0), 0))
9192 remove_note (insn,
9193 find_reg_note (insn, REG_RETVAL, NULL_RTX));
9194 dead_libcall = 1;
9198 else if (in_libcall)
9199 live_insn = ! dead_libcall;
9200 else if (GET_CODE (PATTERN (insn)) == SET)
9202 if (GET_CODE (SET_DEST (PATTERN (insn))) == REG
9203 && SET_DEST (PATTERN (insn)) == SET_SRC (PATTERN (insn)))
9206 #ifdef HAVE_cc0
9207 else if (GET_CODE (SET_DEST (PATTERN (insn))) == CC0
9208 && ! side_effects_p (SET_SRC (PATTERN (insn)))
9209 && ((tem = next_nonnote_insn (insn)) == 0
9210 || GET_RTX_CLASS (GET_CODE (tem)) != 'i'
9211 || ! reg_referenced_p (cc0_rtx, PATTERN (tem))))
9213 #endif
9214 else if (GET_CODE (SET_DEST (PATTERN (insn))) != REG
9215 || REGNO (SET_DEST (PATTERN (insn))) < FIRST_PSEUDO_REGISTER
9216 || counts[REGNO (SET_DEST (PATTERN (insn)))] != 0
9217 || side_effects_p (SET_SRC (PATTERN (insn)))
9218 /* An ADDRESSOF expression can turn into a use of the
9219 internal arg pointer, so always consider the
9220 internal arg pointer live. If it is truly dead,
9221 flow will delete the initializing insn. */
9222 || (SET_DEST (PATTERN (insn))
9223 == current_function_internal_arg_pointer))
9224 live_insn = 1;
9226 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
9227 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
9229 rtx elt = XVECEXP (PATTERN (insn), 0, i);
9231 if (GET_CODE (elt) == SET)
9233 if (GET_CODE (SET_DEST (elt)) == REG
9234 && SET_DEST (elt) == SET_SRC (elt))
9237 #ifdef HAVE_cc0
9238 else if (GET_CODE (SET_DEST (elt)) == CC0
9239 && ! side_effects_p (SET_SRC (elt))
9240 && ((tem = next_nonnote_insn (insn)) == 0
9241 || GET_RTX_CLASS (GET_CODE (tem)) != 'i'
9242 || ! reg_referenced_p (cc0_rtx, PATTERN (tem))))
9244 #endif
9245 else if (GET_CODE (SET_DEST (elt)) != REG
9246 || REGNO (SET_DEST (elt)) < FIRST_PSEUDO_REGISTER
9247 || counts[REGNO (SET_DEST (elt))] != 0
9248 || side_effects_p (SET_SRC (elt))
9249 /* An ADDRESSOF expression can turn into a use of the
9250 internal arg pointer, so always consider the
9251 internal arg pointer live. If it is truly dead,
9252 flow will delete the initializing insn. */
9253 || (SET_DEST (elt)
9254 == current_function_internal_arg_pointer))
9255 live_insn = 1;
9257 else if (GET_CODE (elt) != CLOBBER && GET_CODE (elt) != USE)
9258 live_insn = 1;
9260 else
9261 live_insn = 1;
9263 /* If this is a dead insn, delete it and show registers in it aren't
9264 being used. */
9266 if (! live_insn)
9268 count_reg_usage (insn, counts, NULL_RTX, -1);
9269 delete_insn (insn);
9272 if (find_reg_note (insn, REG_LIBCALL, NULL_RTX))
9274 in_libcall = 0;
9275 dead_libcall = 0;