1 /* Common subexpression elimination library for GNU compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
4 2012 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
28 #include "tree.h"/* FIXME: For hashing DEBUG_EXPR & friends. */
31 #include "hard-reg-set.h"
33 #include "insn-config.h"
37 #include "diagnostic-core.h"
44 #include "alloc-pool.h"
48 /* A list of cselib_val structures. */
50 struct elt_list
*next
;
54 static bool cselib_record_memory
;
55 static bool cselib_preserve_constants
;
56 static bool cselib_any_perm_equivs
;
57 static int entry_and_rtx_equal_p (const void *, const void *);
58 static hashval_t
get_value_hash (const void *);
59 static struct elt_list
*new_elt_list (struct elt_list
*, cselib_val
*);
60 static void new_elt_loc_list (cselib_val
*, rtx
);
61 static void unchain_one_value (cselib_val
*);
62 static void unchain_one_elt_list (struct elt_list
**);
63 static void unchain_one_elt_loc_list (struct elt_loc_list
**);
64 static int discard_useless_locs (void **, void *);
65 static int discard_useless_values (void **, void *);
66 static void remove_useless_values (void);
67 static int rtx_equal_for_cselib_1 (rtx
, rtx
, enum machine_mode
);
68 static unsigned int cselib_hash_rtx (rtx
, int, enum machine_mode
);
69 static cselib_val
*new_cselib_val (unsigned int, enum machine_mode
, rtx
);
70 static void add_mem_for_addr (cselib_val
*, cselib_val
*, rtx
);
71 static cselib_val
*cselib_lookup_mem (rtx
, int);
72 static void cselib_invalidate_regno (unsigned int, enum machine_mode
);
73 static void cselib_invalidate_mem (rtx
);
74 static void cselib_record_set (rtx
, cselib_val
*, cselib_val
*);
75 static void cselib_record_sets (rtx
);
77 struct expand_value_data
80 cselib_expand_callback callback
;
85 static rtx
cselib_expand_value_rtx_1 (rtx
, struct expand_value_data
*, int);
87 /* There are three ways in which cselib can look up an rtx:
88 - for a REG, the reg_values table (which is indexed by regno) is used
89 - for a MEM, we recursively look up its address and then follow the
90 addr_list of that value
91 - for everything else, we compute a hash value and go through the hash
92 table. Since different rtx's can still have the same hash value,
93 this involves walking the table entries for a given value and comparing
94 the locations of the entries with the rtx we are looking up. */
96 /* A table that enables us to look up elts by their value. */
97 static htab_t cselib_hash_table
;
99 /* This is a global so we don't have to pass this through every function.
100 It is used in new_elt_loc_list to set SETTING_INSN. */
101 static rtx cselib_current_insn
;
103 /* The unique id that the next create value will take. */
104 static unsigned int next_uid
;
106 /* The number of registers we had when the varrays were last resized. */
107 static unsigned int cselib_nregs
;
109 /* Count values without known locations, or with only locations that
110 wouldn't have been known except for debug insns. Whenever this
111 grows too big, we remove these useless values from the table.
113 Counting values with only debug values is a bit tricky. We don't
114 want to increment n_useless_values when we create a value for a
115 debug insn, for this would get n_useless_values out of sync, but we
116 want increment it if all locs in the list that were ever referenced
117 in nondebug insns are removed from the list.
119 In the general case, once we do that, we'd have to stop accepting
120 nondebug expressions in the loc list, to avoid having two values
121 equivalent that, without debug insns, would have been made into
122 separate values. However, because debug insns never introduce
123 equivalences themselves (no assignments), the only means for
124 growing loc lists is through nondebug assignments. If the locs
125 also happen to be referenced in debug insns, it will work just fine.
127 A consequence of this is that there's at most one debug-only loc in
128 each loc list. If we keep it in the first entry, testing whether
129 we have a debug-only loc list takes O(1).
131 Furthermore, since any additional entry in a loc list containing a
132 debug loc would have to come from an assignment (nondebug) that
133 references both the initial debug loc and the newly-equivalent loc,
134 the initial debug loc would be promoted to a nondebug loc, and the
135 loc list would not contain debug locs any more.
137 So the only case we have to be careful with in order to keep
138 n_useless_values in sync between debug and nondebug compilations is
139 to avoid incrementing n_useless_values when removing the single loc
140 from a value that turns out to not appear outside debug values. We
141 increment n_useless_debug_values instead, and leave such values
142 alone until, for other reasons, we garbage-collect useless
144 static int n_useless_values
;
145 static int n_useless_debug_values
;
147 /* Count values whose locs have been taken exclusively from debug
148 insns for the entire life of the value. */
149 static int n_debug_values
;
151 /* Number of useless values before we remove them from the hash table. */
152 #define MAX_USELESS_VALUES 32
154 /* This table maps from register number to values. It does not
155 contain pointers to cselib_val structures, but rather elt_lists.
156 The purpose is to be able to refer to the same register in
157 different modes. The first element of the list defines the mode in
158 which the register was set; if the mode is unknown or the value is
159 no longer valid in that mode, ELT will be NULL for the first
161 static struct elt_list
**reg_values
;
162 static unsigned int reg_values_size
;
163 #define REG_VALUES(i) reg_values[i]
165 /* The largest number of hard regs used by any entry added to the
166 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
167 static unsigned int max_value_regs
;
169 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
170 in cselib_clear_table() for fast emptying. */
171 static unsigned int *used_regs
;
172 static unsigned int n_used_regs
;
174 /* We pass this to cselib_invalidate_mem to invalidate all of
175 memory for a non-const call instruction. */
176 static GTY(()) rtx callmem
;
178 /* Set by discard_useless_locs if it deleted the last location of any
180 static int values_became_useless
;
182 /* Used as stop element of the containing_mem list so we can check
183 presence in the list by checking the next pointer. */
184 static cselib_val dummy_val
;
186 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
187 that is constant through the whole function and should never be
189 static cselib_val
*cfa_base_preserved_val
;
190 static unsigned int cfa_base_preserved_regno
= INVALID_REGNUM
;
192 /* Used to list all values that contain memory reference.
193 May or may not contain the useless values - the list is compacted
194 each time memory is invalidated. */
195 static cselib_val
*first_containing_mem
= &dummy_val
;
196 static alloc_pool elt_loc_list_pool
, elt_list_pool
, cselib_val_pool
, value_pool
;
198 /* If nonnull, cselib will call this function before freeing useless
199 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
200 void (*cselib_discard_hook
) (cselib_val
*);
202 /* If nonnull, cselib will call this function before recording sets or
203 even clobbering outputs of INSN. All the recorded sets will be
204 represented in the array sets[n_sets]. new_val_min can be used to
205 tell whether values present in sets are introduced by this
207 void (*cselib_record_sets_hook
) (rtx insn
, struct cselib_set
*sets
,
210 #define PRESERVED_VALUE_P(RTX) \
211 (RTL_FLAG_CHECK1("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
213 #define SP_BASED_VALUE_P(RTX) \
214 (RTL_FLAG_CHECK1("SP_BASED_VALUE_P", (RTX), VALUE)->jump)
218 /* Allocate a struct elt_list and fill in its two elements with the
221 static inline struct elt_list
*
222 new_elt_list (struct elt_list
*next
, cselib_val
*elt
)
225 el
= (struct elt_list
*) pool_alloc (elt_list_pool
);
231 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
235 new_elt_loc_list (cselib_val
*val
, rtx loc
)
237 struct elt_loc_list
*el
, *next
= val
->locs
;
239 gcc_checking_assert (!next
|| !next
->setting_insn
240 || !DEBUG_INSN_P (next
->setting_insn
)
241 || cselib_current_insn
== next
->setting_insn
);
243 /* If we're creating the first loc in a debug insn context, we've
244 just created a debug value. Count it. */
245 if (!next
&& cselib_current_insn
&& DEBUG_INSN_P (cselib_current_insn
))
248 val
= canonical_cselib_val (val
);
251 if (GET_CODE (loc
) == VALUE
)
253 loc
= canonical_cselib_val (CSELIB_VAL_PTR (loc
))->val_rtx
;
255 gcc_checking_assert (PRESERVED_VALUE_P (loc
)
256 == PRESERVED_VALUE_P (val
->val_rtx
));
258 if (val
->val_rtx
== loc
)
260 else if (val
->uid
> CSELIB_VAL_PTR (loc
)->uid
)
262 /* Reverse the insertion. */
263 new_elt_loc_list (CSELIB_VAL_PTR (loc
), val
->val_rtx
);
267 gcc_checking_assert (val
->uid
< CSELIB_VAL_PTR (loc
)->uid
);
269 if (CSELIB_VAL_PTR (loc
)->locs
)
271 /* Bring all locs from LOC to VAL. */
272 for (el
= CSELIB_VAL_PTR (loc
)->locs
; el
->next
; el
= el
->next
)
274 /* Adjust values that have LOC as canonical so that VAL
275 becomes their canonical. */
276 if (el
->loc
&& GET_CODE (el
->loc
) == VALUE
)
278 gcc_checking_assert (CSELIB_VAL_PTR (el
->loc
)->locs
->loc
280 CSELIB_VAL_PTR (el
->loc
)->locs
->loc
= val
->val_rtx
;
283 el
->next
= val
->locs
;
284 next
= val
->locs
= CSELIB_VAL_PTR (loc
)->locs
;
287 if (CSELIB_VAL_PTR (loc
)->addr_list
)
289 /* Bring in addr_list into canonical node. */
290 struct elt_list
*last
= CSELIB_VAL_PTR (loc
)->addr_list
;
293 last
->next
= val
->addr_list
;
294 val
->addr_list
= CSELIB_VAL_PTR (loc
)->addr_list
;
295 CSELIB_VAL_PTR (loc
)->addr_list
= NULL
;
298 if (CSELIB_VAL_PTR (loc
)->next_containing_mem
!= NULL
299 && val
->next_containing_mem
== NULL
)
301 /* Add VAL to the containing_mem list after LOC. LOC will
302 be removed when we notice it doesn't contain any
304 val
->next_containing_mem
= CSELIB_VAL_PTR (loc
)->next_containing_mem
;
305 CSELIB_VAL_PTR (loc
)->next_containing_mem
= val
;
308 /* Chain LOC back to VAL. */
309 el
= (struct elt_loc_list
*) pool_alloc (elt_loc_list_pool
);
310 el
->loc
= val
->val_rtx
;
311 el
->setting_insn
= cselib_current_insn
;
313 CSELIB_VAL_PTR (loc
)->locs
= el
;
316 el
= (struct elt_loc_list
*) pool_alloc (elt_loc_list_pool
);
318 el
->setting_insn
= cselib_current_insn
;
323 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
324 originating from a debug insn, maintaining the debug values
328 promote_debug_loc (struct elt_loc_list
*l
)
330 if (l
&& l
->setting_insn
&& DEBUG_INSN_P (l
->setting_insn
)
331 && (!cselib_current_insn
|| !DEBUG_INSN_P (cselib_current_insn
)))
334 l
->setting_insn
= cselib_current_insn
;
335 if (cselib_preserve_constants
&& l
->next
)
337 gcc_assert (l
->next
->setting_insn
338 && DEBUG_INSN_P (l
->next
->setting_insn
)
340 l
->next
->setting_insn
= cselib_current_insn
;
343 gcc_assert (!l
->next
);
347 /* The elt_list at *PL is no longer needed. Unchain it and free its
351 unchain_one_elt_list (struct elt_list
**pl
)
353 struct elt_list
*l
= *pl
;
356 pool_free (elt_list_pool
, l
);
359 /* Likewise for elt_loc_lists. */
362 unchain_one_elt_loc_list (struct elt_loc_list
**pl
)
364 struct elt_loc_list
*l
= *pl
;
367 pool_free (elt_loc_list_pool
, l
);
370 /* Likewise for cselib_vals. This also frees the addr_list associated with
374 unchain_one_value (cselib_val
*v
)
377 unchain_one_elt_list (&v
->addr_list
);
379 pool_free (cselib_val_pool
, v
);
382 /* Remove all entries from the hash table. Also used during
386 cselib_clear_table (void)
388 cselib_reset_table (1);
391 /* Return TRUE if V is a constant, a function invariant or a VALUE
392 equivalence; FALSE otherwise. */
395 invariant_or_equiv_p (cselib_val
*v
)
397 struct elt_loc_list
*l
;
399 if (v
== cfa_base_preserved_val
)
402 /* Keep VALUE equivalences around. */
403 for (l
= v
->locs
; l
; l
= l
->next
)
404 if (GET_CODE (l
->loc
) == VALUE
)
408 && v
->locs
->next
== NULL
)
410 if (CONSTANT_P (v
->locs
->loc
)
411 && (GET_CODE (v
->locs
->loc
) != CONST
412 || !references_value_p (v
->locs
->loc
, 0)))
414 /* Although a debug expr may be bound to different expressions,
415 we can preserve it as if it was constant, to get unification
416 and proper merging within var-tracking. */
417 if (GET_CODE (v
->locs
->loc
) == DEBUG_EXPR
418 || GET_CODE (v
->locs
->loc
) == DEBUG_IMPLICIT_PTR
419 || GET_CODE (v
->locs
->loc
) == ENTRY_VALUE
420 || GET_CODE (v
->locs
->loc
) == DEBUG_PARAMETER_REF
)
423 /* (plus (value V) (const_int C)) is invariant iff V is invariant. */
424 if (GET_CODE (v
->locs
->loc
) == PLUS
425 && CONST_INT_P (XEXP (v
->locs
->loc
, 1))
426 && GET_CODE (XEXP (v
->locs
->loc
, 0)) == VALUE
427 && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v
->locs
->loc
, 0))))
434 /* Remove from hash table all VALUEs except constants, function
435 invariants and VALUE equivalences. */
438 preserve_constants_and_equivs (void **x
, void *info ATTRIBUTE_UNUSED
)
440 cselib_val
*v
= (cselib_val
*)*x
;
442 if (!invariant_or_equiv_p (v
))
443 htab_clear_slot (cselib_hash_table
, x
);
447 /* Remove all entries from the hash table, arranging for the next
448 value to be numbered NUM. */
451 cselib_reset_table (unsigned int num
)
457 if (cfa_base_preserved_val
)
459 unsigned int regno
= cfa_base_preserved_regno
;
460 unsigned int new_used_regs
= 0;
461 for (i
= 0; i
< n_used_regs
; i
++)
462 if (used_regs
[i
] == regno
)
468 REG_VALUES (used_regs
[i
]) = 0;
469 gcc_assert (new_used_regs
== 1);
470 n_used_regs
= new_used_regs
;
471 used_regs
[0] = regno
;
473 = hard_regno_nregs
[regno
][GET_MODE (cfa_base_preserved_val
->locs
->loc
)];
477 for (i
= 0; i
< n_used_regs
; i
++)
478 REG_VALUES (used_regs
[i
]) = 0;
482 if (cselib_preserve_constants
)
483 htab_traverse (cselib_hash_table
, preserve_constants_and_equivs
, NULL
);
486 htab_empty (cselib_hash_table
);
487 gcc_checking_assert (!cselib_any_perm_equivs
);
490 n_useless_values
= 0;
491 n_useless_debug_values
= 0;
496 first_containing_mem
= &dummy_val
;
499 /* Return the number of the next value that will be generated. */
502 cselib_get_next_uid (void)
507 /* See the documentation of cselib_find_slot below. */
508 static enum machine_mode find_slot_memmode
;
510 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
511 INSERTing if requested. When X is part of the address of a MEM,
512 MEMMODE should specify the mode of the MEM. While searching the
513 table, MEMMODE is held in FIND_SLOT_MEMMODE, so that autoinc RTXs
514 in X can be resolved. */
517 cselib_find_slot (rtx x
, hashval_t hash
, enum insert_option insert
,
518 enum machine_mode memmode
)
521 find_slot_memmode
= memmode
;
522 slot
= htab_find_slot_with_hash (cselib_hash_table
, x
, hash
, insert
);
523 find_slot_memmode
= VOIDmode
;
527 /* The equality test for our hash table. The first argument ENTRY is a table
528 element (i.e. a cselib_val), while the second arg X is an rtx. We know
529 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
530 CONST of an appropriate mode. */
533 entry_and_rtx_equal_p (const void *entry
, const void *x_arg
)
535 struct elt_loc_list
*l
;
536 const cselib_val
*const v
= (const cselib_val
*) entry
;
537 rtx x
= CONST_CAST_RTX ((const_rtx
)x_arg
);
538 enum machine_mode mode
= GET_MODE (x
);
540 gcc_assert (!CONST_INT_P (x
) && GET_CODE (x
) != CONST_FIXED
541 && (mode
!= VOIDmode
|| GET_CODE (x
) != CONST_DOUBLE
));
543 if (mode
!= GET_MODE (v
->val_rtx
))
546 /* Unwrap X if necessary. */
547 if (GET_CODE (x
) == CONST
548 && (CONST_INT_P (XEXP (x
, 0))
549 || GET_CODE (XEXP (x
, 0)) == CONST_FIXED
550 || GET_CODE (XEXP (x
, 0)) == CONST_DOUBLE
))
553 /* We don't guarantee that distinct rtx's have different hash values,
554 so we need to do a comparison. */
555 for (l
= v
->locs
; l
; l
= l
->next
)
556 if (rtx_equal_for_cselib_1 (l
->loc
, x
, find_slot_memmode
))
558 promote_debug_loc (l
);
565 /* The hash function for our hash table. The value is always computed with
566 cselib_hash_rtx when adding an element; this function just extracts the
567 hash value from a cselib_val structure. */
570 get_value_hash (const void *entry
)
572 const cselib_val
*const v
= (const cselib_val
*) entry
;
576 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
577 only return true for values which point to a cselib_val whose value
578 element has been set to zero, which implies the cselib_val will be
582 references_value_p (const_rtx x
, int only_useless
)
584 const enum rtx_code code
= GET_CODE (x
);
585 const char *fmt
= GET_RTX_FORMAT (code
);
588 if (GET_CODE (x
) == VALUE
589 && (! only_useless
||
590 (CSELIB_VAL_PTR (x
)->locs
== 0 && !PRESERVED_VALUE_P (x
))))
593 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
595 if (fmt
[i
] == 'e' && references_value_p (XEXP (x
, i
), only_useless
))
597 else if (fmt
[i
] == 'E')
598 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
599 if (references_value_p (XVECEXP (x
, i
, j
), only_useless
))
606 /* For all locations found in X, delete locations that reference useless
607 values (i.e. values without any location). Called through
611 discard_useless_locs (void **x
, void *info ATTRIBUTE_UNUSED
)
613 cselib_val
*v
= (cselib_val
*)*x
;
614 struct elt_loc_list
**p
= &v
->locs
;
615 bool had_locs
= v
->locs
!= NULL
;
616 rtx setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
620 if (references_value_p ((*p
)->loc
, 1))
621 unchain_one_elt_loc_list (p
);
626 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
628 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
629 n_useless_debug_values
++;
632 values_became_useless
= 1;
637 /* If X is a value with no locations, remove it from the hashtable. */
640 discard_useless_values (void **x
, void *info ATTRIBUTE_UNUSED
)
642 cselib_val
*v
= (cselib_val
*)*x
;
644 if (v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
646 if (cselib_discard_hook
)
647 cselib_discard_hook (v
);
649 CSELIB_VAL_PTR (v
->val_rtx
) = NULL
;
650 htab_clear_slot (cselib_hash_table
, x
);
651 unchain_one_value (v
);
658 /* Clean out useless values (i.e. those which no longer have locations
659 associated with them) from the hash table. */
662 remove_useless_values (void)
666 /* First pass: eliminate locations that reference the value. That in
667 turn can make more values useless. */
670 values_became_useless
= 0;
671 htab_traverse (cselib_hash_table
, discard_useless_locs
, 0);
673 while (values_became_useless
);
675 /* Second pass: actually remove the values. */
677 p
= &first_containing_mem
;
678 for (v
= *p
; v
!= &dummy_val
; v
= v
->next_containing_mem
)
679 if (v
->locs
&& v
== canonical_cselib_val (v
))
682 p
= &(*p
)->next_containing_mem
;
686 n_useless_values
+= n_useless_debug_values
;
687 n_debug_values
-= n_useless_debug_values
;
688 n_useless_debug_values
= 0;
690 htab_traverse (cselib_hash_table
, discard_useless_values
, 0);
692 gcc_assert (!n_useless_values
);
695 /* Arrange for a value to not be removed from the hash table even if
696 it becomes useless. */
699 cselib_preserve_value (cselib_val
*v
)
701 PRESERVED_VALUE_P (v
->val_rtx
) = 1;
704 /* Test whether a value is preserved. */
707 cselib_preserved_value_p (cselib_val
*v
)
709 return PRESERVED_VALUE_P (v
->val_rtx
);
712 /* Arrange for a REG value to be assumed constant through the whole function,
713 never invalidated and preserved across cselib_reset_table calls. */
716 cselib_preserve_cfa_base_value (cselib_val
*v
, unsigned int regno
)
718 if (cselib_preserve_constants
720 && REG_P (v
->locs
->loc
))
722 cfa_base_preserved_val
= v
;
723 cfa_base_preserved_regno
= regno
;
727 /* Clean all non-constant expressions in the hash table, but retain
731 cselib_preserve_only_values (void)
735 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
736 cselib_invalidate_regno (i
, reg_raw_mode
[i
]);
738 cselib_invalidate_mem (callmem
);
740 remove_useless_values ();
742 gcc_assert (first_containing_mem
== &dummy_val
);
745 /* Arrange for a value to be marked as based on stack pointer
746 for find_base_term purposes. */
749 cselib_set_value_sp_based (cselib_val
*v
)
751 SP_BASED_VALUE_P (v
->val_rtx
) = 1;
754 /* Test whether a value is based on stack pointer for
755 find_base_term purposes. */
758 cselib_sp_based_value_p (cselib_val
*v
)
760 return SP_BASED_VALUE_P (v
->val_rtx
);
763 /* Return the mode in which a register was last set. If X is not a
764 register, return its mode. If the mode in which the register was
765 set is not known, or the value was already clobbered, return
769 cselib_reg_set_mode (const_rtx x
)
774 if (REG_VALUES (REGNO (x
)) == NULL
775 || REG_VALUES (REGNO (x
))->elt
== NULL
)
778 return GET_MODE (REG_VALUES (REGNO (x
))->elt
->val_rtx
);
781 /* Return nonzero if we can prove that X and Y contain the same value, taking
782 our gathered information into account. */
785 rtx_equal_for_cselib_p (rtx x
, rtx y
)
787 return rtx_equal_for_cselib_1 (x
, y
, VOIDmode
);
790 /* If x is a PLUS or an autoinc operation, expand the operation,
791 storing the offset, if any, in *OFF. */
794 autoinc_split (rtx x
, rtx
*off
, enum machine_mode memmode
)
796 switch (GET_CODE (x
))
803 if (memmode
== VOIDmode
)
806 *off
= GEN_INT (-GET_MODE_SIZE (memmode
));
811 if (memmode
== VOIDmode
)
814 *off
= GEN_INT (GET_MODE_SIZE (memmode
));
830 /* Return nonzero if we can prove that X and Y contain the same value,
831 taking our gathered information into account. MEMMODE holds the
832 mode of the enclosing MEM, if any, as required to deal with autoinc
833 addressing modes. If X and Y are not (known to be) part of
834 addresses, MEMMODE should be VOIDmode. */
837 rtx_equal_for_cselib_1 (rtx x
, rtx y
, enum machine_mode memmode
)
843 if (REG_P (x
) || MEM_P (x
))
845 cselib_val
*e
= cselib_lookup (x
, GET_MODE (x
), 0, memmode
);
851 if (REG_P (y
) || MEM_P (y
))
853 cselib_val
*e
= cselib_lookup (y
, GET_MODE (y
), 0, memmode
);
862 if (GET_CODE (x
) == VALUE
)
864 cselib_val
*e
= canonical_cselib_val (CSELIB_VAL_PTR (x
));
865 struct elt_loc_list
*l
;
867 if (GET_CODE (y
) == VALUE
)
868 return e
== canonical_cselib_val (CSELIB_VAL_PTR (y
));
870 for (l
= e
->locs
; l
; l
= l
->next
)
874 /* Avoid infinite recursion. We know we have the canonical
875 value, so we can just skip any values in the equivalence
877 if (REG_P (t
) || MEM_P (t
) || GET_CODE (t
) == VALUE
)
879 else if (rtx_equal_for_cselib_1 (t
, y
, memmode
))
885 else if (GET_CODE (y
) == VALUE
)
887 cselib_val
*e
= canonical_cselib_val (CSELIB_VAL_PTR (y
));
888 struct elt_loc_list
*l
;
890 for (l
= e
->locs
; l
; l
= l
->next
)
894 if (REG_P (t
) || MEM_P (t
) || GET_CODE (t
) == VALUE
)
896 else if (rtx_equal_for_cselib_1 (x
, t
, memmode
))
903 if (GET_MODE (x
) != GET_MODE (y
))
906 if (GET_CODE (x
) != GET_CODE (y
))
908 rtx xorig
= x
, yorig
= y
;
909 rtx xoff
= NULL
, yoff
= NULL
;
911 x
= autoinc_split (x
, &xoff
, memmode
);
912 y
= autoinc_split (y
, &yoff
, memmode
);
917 if (xoff
&& !rtx_equal_for_cselib_1 (xoff
, yoff
, memmode
))
920 /* Don't recurse if nothing changed. */
921 if (x
!= xorig
|| y
!= yorig
)
922 return rtx_equal_for_cselib_1 (x
, y
, memmode
);
927 /* These won't be handled correctly by the code below. */
928 switch (GET_CODE (x
))
935 case DEBUG_IMPLICIT_PTR
:
936 return DEBUG_IMPLICIT_PTR_DECL (x
)
937 == DEBUG_IMPLICIT_PTR_DECL (y
);
939 case DEBUG_PARAMETER_REF
:
940 return DEBUG_PARAMETER_REF_DECL (x
)
941 == DEBUG_PARAMETER_REF_DECL (y
);
944 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
945 use rtx_equal_for_cselib_1 to compare the operands. */
946 return rtx_equal_p (ENTRY_VALUE_EXP (x
), ENTRY_VALUE_EXP (y
));
949 return XEXP (x
, 0) == XEXP (y
, 0);
952 /* We have to compare any autoinc operations in the addresses
953 using this MEM's mode. */
954 return rtx_equal_for_cselib_1 (XEXP (x
, 0), XEXP (y
, 0), GET_MODE (x
));
961 fmt
= GET_RTX_FORMAT (code
);
963 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
970 if (XWINT (x
, i
) != XWINT (y
, i
))
976 if (XINT (x
, i
) != XINT (y
, i
))
982 /* Two vectors must have the same length. */
983 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
986 /* And the corresponding elements must match. */
987 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
988 if (! rtx_equal_for_cselib_1 (XVECEXP (x
, i
, j
),
989 XVECEXP (y
, i
, j
), memmode
))
995 && targetm
.commutative_p (x
, UNKNOWN
)
996 && rtx_equal_for_cselib_1 (XEXP (x
, 1), XEXP (y
, 0), memmode
)
997 && rtx_equal_for_cselib_1 (XEXP (x
, 0), XEXP (y
, 1), memmode
))
999 if (! rtx_equal_for_cselib_1 (XEXP (x
, i
), XEXP (y
, i
), memmode
))
1005 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
1010 /* These are just backpointers, so they don't matter. */
1017 /* It is believed that rtx's at this level will never
1018 contain anything but integers and other rtx's,
1019 except for within LABEL_REFs and SYMBOL_REFs. */
1027 /* We need to pass down the mode of constants through the hash table
1028 functions. For that purpose, wrap them in a CONST of the appropriate
1031 wrap_constant (enum machine_mode mode
, rtx x
)
1033 if (!CONST_INT_P (x
)
1034 && GET_CODE (x
) != CONST_FIXED
1035 && !CONST_DOUBLE_AS_INT_P (x
))
1037 gcc_assert (mode
!= VOIDmode
);
1038 return gen_rtx_CONST (mode
, x
);
1041 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1042 For registers and memory locations, we look up their cselib_val structure
1043 and return its VALUE element.
1044 Possible reasons for return 0 are: the object is volatile, or we couldn't
1045 find a register or memory location in the table and CREATE is zero. If
1046 CREATE is nonzero, table elts are created for regs and mem.
1047 N.B. this hash function returns the same hash value for RTXes that
1048 differ only in the order of operands, thus it is suitable for comparisons
1049 that take commutativity into account.
1050 If we wanted to also support associative rules, we'd have to use a different
1051 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1052 MEMMODE indicates the mode of an enclosing MEM, and it's only
1053 used to compute autoinc values.
1054 We used to have a MODE argument for hashing for CONST_INTs, but that
1055 didn't make sense, since it caused spurious hash differences between
1056 (set (reg:SI 1) (const_int))
1057 (plus:SI (reg:SI 2) (reg:SI 1))
1059 (plus:SI (reg:SI 2) (const_int))
1060 If the mode is important in any context, it must be checked specifically
1061 in a comparison anyway, since relying on hash differences is unsafe. */
1064 cselib_hash_rtx (rtx x
, int create
, enum machine_mode memmode
)
1070 unsigned int hash
= 0;
1072 code
= GET_CODE (x
);
1073 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
1078 e
= CSELIB_VAL_PTR (x
);
1083 e
= cselib_lookup (x
, GET_MODE (x
), create
, memmode
);
1090 hash
+= ((unsigned) DEBUG_EXPR
<< 7)
1091 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x
));
1092 return hash
? hash
: (unsigned int) DEBUG_EXPR
;
1094 case DEBUG_IMPLICIT_PTR
:
1095 hash
+= ((unsigned) DEBUG_IMPLICIT_PTR
<< 7)
1096 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x
));
1097 return hash
? hash
: (unsigned int) DEBUG_IMPLICIT_PTR
;
1099 case DEBUG_PARAMETER_REF
:
1100 hash
+= ((unsigned) DEBUG_PARAMETER_REF
<< 7)
1101 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x
));
1102 return hash
? hash
: (unsigned int) DEBUG_PARAMETER_REF
;
1105 /* ENTRY_VALUEs are function invariant, thus try to avoid
1106 recursing on argument if ENTRY_VALUE is one of the
1107 forms emitted by expand_debug_expr, otherwise
1108 ENTRY_VALUE hash would depend on the current value
1109 in some register or memory. */
1110 if (REG_P (ENTRY_VALUE_EXP (x
)))
1111 hash
+= (unsigned int) REG
1112 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x
))
1113 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x
));
1114 else if (MEM_P (ENTRY_VALUE_EXP (x
))
1115 && REG_P (XEXP (ENTRY_VALUE_EXP (x
), 0)))
1116 hash
+= (unsigned int) MEM
1117 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x
), 0))
1118 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x
), 0));
1120 hash
+= cselib_hash_rtx (ENTRY_VALUE_EXP (x
), create
, memmode
);
1121 return hash
? hash
: (unsigned int) ENTRY_VALUE
;
1124 hash
+= ((unsigned) CONST_INT
<< 7) + INTVAL (x
);
1125 return hash
? hash
: (unsigned int) CONST_INT
;
1128 /* This is like the general case, except that it only counts
1129 the integers representing the constant. */
1130 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
1131 if (GET_MODE (x
) != VOIDmode
)
1132 hash
+= real_hash (CONST_DOUBLE_REAL_VALUE (x
));
1134 hash
+= ((unsigned) CONST_DOUBLE_LOW (x
)
1135 + (unsigned) CONST_DOUBLE_HIGH (x
));
1136 return hash
? hash
: (unsigned int) CONST_DOUBLE
;
1139 hash
+= (unsigned int) code
+ (unsigned int) GET_MODE (x
);
1140 hash
+= fixed_hash (CONST_FIXED_VALUE (x
));
1141 return hash
? hash
: (unsigned int) CONST_FIXED
;
1148 units
= CONST_VECTOR_NUNITS (x
);
1150 for (i
= 0; i
< units
; ++i
)
1152 elt
= CONST_VECTOR_ELT (x
, i
);
1153 hash
+= cselib_hash_rtx (elt
, 0, memmode
);
1159 /* Assume there is only one rtx object for any given label. */
1161 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1162 differences and differences between each stage's debugging dumps. */
1163 hash
+= (((unsigned int) LABEL_REF
<< 7)
1164 + CODE_LABEL_NUMBER (XEXP (x
, 0)));
1165 return hash
? hash
: (unsigned int) LABEL_REF
;
1169 /* Don't hash on the symbol's address to avoid bootstrap differences.
1170 Different hash values may cause expressions to be recorded in
1171 different orders and thus different registers to be used in the
1172 final assembler. This also avoids differences in the dump files
1173 between various stages. */
1175 const unsigned char *p
= (const unsigned char *) XSTR (x
, 0);
1178 h
+= (h
<< 7) + *p
++; /* ??? revisit */
1180 hash
+= ((unsigned int) SYMBOL_REF
<< 7) + h
;
1181 return hash
? hash
: (unsigned int) SYMBOL_REF
;
1186 /* We can't compute these without knowing the MEM mode. */
1187 gcc_assert (memmode
!= VOIDmode
);
1188 i
= GET_MODE_SIZE (memmode
);
1189 if (code
== PRE_DEC
)
1191 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1192 like (mem:MEMMODE (plus (reg) (const_int I))). */
1193 hash
+= (unsigned) PLUS
- (unsigned)code
1194 + cselib_hash_rtx (XEXP (x
, 0), create
, memmode
)
1195 + cselib_hash_rtx (GEN_INT (i
), create
, memmode
);
1196 return hash
? hash
: 1 + (unsigned) PLUS
;
1199 gcc_assert (memmode
!= VOIDmode
);
1200 return cselib_hash_rtx (XEXP (x
, 1), create
, memmode
);
1205 gcc_assert (memmode
!= VOIDmode
);
1206 return cselib_hash_rtx (XEXP (x
, 0), create
, memmode
);
1211 case UNSPEC_VOLATILE
:
1215 if (MEM_VOLATILE_P (x
))
1224 i
= GET_RTX_LENGTH (code
) - 1;
1225 fmt
= GET_RTX_FORMAT (code
);
1232 rtx tem
= XEXP (x
, i
);
1233 unsigned int tem_hash
= cselib_hash_rtx (tem
, create
, memmode
);
1242 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1244 unsigned int tem_hash
1245 = cselib_hash_rtx (XVECEXP (x
, i
, j
), create
, memmode
);
1256 const unsigned char *p
= (const unsigned char *) XSTR (x
, i
);
1265 hash
+= XINT (x
, i
);
1278 return hash
? hash
: 1 + (unsigned int) GET_CODE (x
);
1281 /* Create a new value structure for VALUE and initialize it. The mode of the
1284 static inline cselib_val
*
1285 new_cselib_val (unsigned int hash
, enum machine_mode mode
, rtx x
)
1287 cselib_val
*e
= (cselib_val
*) pool_alloc (cselib_val_pool
);
1290 gcc_assert (next_uid
);
1293 e
->uid
= next_uid
++;
1294 /* We use an alloc pool to allocate this RTL construct because it
1295 accounts for about 8% of the overall memory usage. We know
1296 precisely when we can have VALUE RTXen (when cselib is active)
1297 so we don't need to put them in garbage collected memory.
1298 ??? Why should a VALUE be an RTX in the first place? */
1299 e
->val_rtx
= (rtx
) pool_alloc (value_pool
);
1300 memset (e
->val_rtx
, 0, RTX_HDR_SIZE
);
1301 PUT_CODE (e
->val_rtx
, VALUE
);
1302 PUT_MODE (e
->val_rtx
, mode
);
1303 CSELIB_VAL_PTR (e
->val_rtx
) = e
;
1306 e
->next_containing_mem
= 0;
1308 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1310 fprintf (dump_file
, "cselib value %u:%u ", e
->uid
, hash
);
1311 if (flag_dump_noaddr
|| flag_dump_unnumbered
)
1312 fputs ("# ", dump_file
);
1314 fprintf (dump_file
, "%p ", (void*)e
);
1315 print_rtl_single (dump_file
, x
);
1316 fputc ('\n', dump_file
);
1322 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1323 contains the data at this address. X is a MEM that represents the
1324 value. Update the two value structures to represent this situation. */
1327 add_mem_for_addr (cselib_val
*addr_elt
, cselib_val
*mem_elt
, rtx x
)
1329 struct elt_loc_list
*l
;
1331 addr_elt
= canonical_cselib_val (addr_elt
);
1332 mem_elt
= canonical_cselib_val (mem_elt
);
1334 /* Avoid duplicates. */
1335 for (l
= mem_elt
->locs
; l
; l
= l
->next
)
1337 && CSELIB_VAL_PTR (XEXP (l
->loc
, 0)) == addr_elt
)
1339 promote_debug_loc (l
);
1343 addr_elt
->addr_list
= new_elt_list (addr_elt
->addr_list
, mem_elt
);
1344 new_elt_loc_list (mem_elt
,
1345 replace_equiv_address_nv (x
, addr_elt
->val_rtx
));
1346 if (mem_elt
->next_containing_mem
== NULL
)
1348 mem_elt
->next_containing_mem
= first_containing_mem
;
1349 first_containing_mem
= mem_elt
;
1353 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1354 If CREATE, make a new one if we haven't seen it before. */
1357 cselib_lookup_mem (rtx x
, int create
)
1359 enum machine_mode mode
= GET_MODE (x
);
1360 enum machine_mode addr_mode
;
1363 cselib_val
*mem_elt
;
1366 if (MEM_VOLATILE_P (x
) || mode
== BLKmode
1367 || !cselib_record_memory
1368 || (FLOAT_MODE_P (mode
) && flag_float_store
))
1371 addr_mode
= GET_MODE (XEXP (x
, 0));
1372 if (addr_mode
== VOIDmode
)
1375 /* Look up the value for the address. */
1376 addr
= cselib_lookup (XEXP (x
, 0), addr_mode
, create
, mode
);
1380 addr
= canonical_cselib_val (addr
);
1381 /* Find a value that describes a value of our mode at that address. */
1382 for (l
= addr
->addr_list
; l
; l
= l
->next
)
1383 if (GET_MODE (l
->elt
->val_rtx
) == mode
)
1385 promote_debug_loc (l
->elt
->locs
);
1392 mem_elt
= new_cselib_val (next_uid
, mode
, x
);
1393 add_mem_for_addr (addr
, mem_elt
, x
);
1394 slot
= cselib_find_slot (wrap_constant (mode
, x
), mem_elt
->hash
,
1400 /* Search through the possible substitutions in P. We prefer a non reg
1401 substitution because this allows us to expand the tree further. If
1402 we find, just a reg, take the lowest regno. There may be several
1403 non-reg results, we just take the first one because they will all
1404 expand to the same place. */
1407 expand_loc (struct elt_loc_list
*p
, struct expand_value_data
*evd
,
1410 rtx reg_result
= NULL
;
1411 unsigned int regno
= UINT_MAX
;
1412 struct elt_loc_list
*p_in
= p
;
1414 for (; p
; p
= p
->next
)
1416 /* Return these right away to avoid returning stack pointer based
1417 expressions for frame pointer and vice versa, which is something
1418 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1419 for more details. */
1421 && (REGNO (p
->loc
) == STACK_POINTER_REGNUM
1422 || REGNO (p
->loc
) == FRAME_POINTER_REGNUM
1423 || REGNO (p
->loc
) == HARD_FRAME_POINTER_REGNUM
1424 || REGNO (p
->loc
) == cfa_base_preserved_regno
))
1426 /* Avoid infinite recursion trying to expand a reg into a
1428 if ((REG_P (p
->loc
))
1429 && (REGNO (p
->loc
) < regno
)
1430 && !bitmap_bit_p (evd
->regs_active
, REGNO (p
->loc
)))
1432 reg_result
= p
->loc
;
1433 regno
= REGNO (p
->loc
);
1435 /* Avoid infinite recursion and do not try to expand the
1437 else if (GET_CODE (p
->loc
) == VALUE
1438 && CSELIB_VAL_PTR (p
->loc
)->locs
== p_in
)
1440 else if (!REG_P (p
->loc
))
1443 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1445 print_inline_rtx (dump_file
, p
->loc
, 0);
1446 fprintf (dump_file
, "\n");
1448 if (GET_CODE (p
->loc
) == LO_SUM
1449 && GET_CODE (XEXP (p
->loc
, 1)) == SYMBOL_REF
1451 && (note
= find_reg_note (p
->setting_insn
, REG_EQUAL
, NULL_RTX
))
1452 && XEXP (note
, 0) == XEXP (p
->loc
, 1))
1453 return XEXP (p
->loc
, 1);
1454 result
= cselib_expand_value_rtx_1 (p
->loc
, evd
, max_depth
- 1);
1461 if (regno
!= UINT_MAX
)
1464 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1465 fprintf (dump_file
, "r%d\n", regno
);
1467 result
= cselib_expand_value_rtx_1 (reg_result
, evd
, max_depth
- 1);
1472 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1476 print_inline_rtx (dump_file
, reg_result
, 0);
1477 fprintf (dump_file
, "\n");
1480 fprintf (dump_file
, "NULL\n");
1486 /* Forward substitute and expand an expression out to its roots.
1487 This is the opposite of common subexpression. Because local value
1488 numbering is such a weak optimization, the expanded expression is
1489 pretty much unique (not from a pointer equals point of view but
1490 from a tree shape point of view.
1492 This function returns NULL if the expansion fails. The expansion
1493 will fail if there is no value number for one of the operands or if
1494 one of the operands has been overwritten between the current insn
1495 and the beginning of the basic block. For instance x has no
1501 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1502 It is clear on return. */
1505 cselib_expand_value_rtx (rtx orig
, bitmap regs_active
, int max_depth
)
1507 struct expand_value_data evd
;
1509 evd
.regs_active
= regs_active
;
1510 evd
.callback
= NULL
;
1511 evd
.callback_arg
= NULL
;
1514 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
);
1517 /* Same as cselib_expand_value_rtx, but using a callback to try to
1518 resolve some expressions. The CB function should return ORIG if it
1519 can't or does not want to deal with a certain RTX. Any other
1520 return value, including NULL, will be used as the expansion for
1521 VALUE, without any further changes. */
1524 cselib_expand_value_rtx_cb (rtx orig
, bitmap regs_active
, int max_depth
,
1525 cselib_expand_callback cb
, void *data
)
1527 struct expand_value_data evd
;
1529 evd
.regs_active
= regs_active
;
1531 evd
.callback_arg
= data
;
1534 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
);
1537 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1538 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1539 would return NULL or non-NULL, without allocating new rtx. */
1542 cselib_dummy_expand_value_rtx_cb (rtx orig
, bitmap regs_active
, int max_depth
,
1543 cselib_expand_callback cb
, void *data
)
1545 struct expand_value_data evd
;
1547 evd
.regs_active
= regs_active
;
1549 evd
.callback_arg
= data
;
1552 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
) != NULL
;
1555 /* Internal implementation of cselib_expand_value_rtx and
1556 cselib_expand_value_rtx_cb. */
1559 cselib_expand_value_rtx_1 (rtx orig
, struct expand_value_data
*evd
,
1565 const char *format_ptr
;
1566 enum machine_mode mode
;
1568 code
= GET_CODE (orig
);
1570 /* For the context of dse, if we end up expand into a huge tree, we
1571 will not have a useful address, so we might as well just give up
1580 struct elt_list
*l
= REG_VALUES (REGNO (orig
));
1582 if (l
&& l
->elt
== NULL
)
1584 for (; l
; l
= l
->next
)
1585 if (GET_MODE (l
->elt
->val_rtx
) == GET_MODE (orig
))
1588 unsigned regno
= REGNO (orig
);
1590 /* The only thing that we are not willing to do (this
1591 is requirement of dse and if others potential uses
1592 need this function we should add a parm to control
1593 it) is that we will not substitute the
1594 STACK_POINTER_REGNUM, FRAME_POINTER or the
1597 These expansions confuses the code that notices that
1598 stores into the frame go dead at the end of the
1599 function and that the frame is not effected by calls
1600 to subroutines. If you allow the
1601 STACK_POINTER_REGNUM substitution, then dse will
1602 think that parameter pushing also goes dead which is
1603 wrong. If you allow the FRAME_POINTER or the
1604 HARD_FRAME_POINTER then you lose the opportunity to
1605 make the frame assumptions. */
1606 if (regno
== STACK_POINTER_REGNUM
1607 || regno
== FRAME_POINTER_REGNUM
1608 || regno
== HARD_FRAME_POINTER_REGNUM
1609 || regno
== cfa_base_preserved_regno
)
1612 bitmap_set_bit (evd
->regs_active
, regno
);
1614 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1615 fprintf (dump_file
, "expanding: r%d into: ", regno
);
1617 result
= expand_loc (l
->elt
->locs
, evd
, max_depth
);
1618 bitmap_clear_bit (evd
->regs_active
, regno
);
1633 /* SCRATCH must be shared because they represent distinct values. */
1636 if (REG_P (XEXP (orig
, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig
, 0))))
1641 if (shared_const_p (orig
))
1651 subreg
= evd
->callback (orig
, evd
->regs_active
, max_depth
,
1657 subreg
= cselib_expand_value_rtx_1 (SUBREG_REG (orig
), evd
,
1661 scopy
= simplify_gen_subreg (GET_MODE (orig
), subreg
,
1662 GET_MODE (SUBREG_REG (orig
)),
1663 SUBREG_BYTE (orig
));
1665 || (GET_CODE (scopy
) == SUBREG
1666 && !REG_P (SUBREG_REG (scopy
))
1667 && !MEM_P (SUBREG_REG (scopy
))))
1677 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1679 fputs ("\nexpanding ", dump_file
);
1680 print_rtl_single (dump_file
, orig
);
1681 fputs (" into...", dump_file
);
1686 result
= evd
->callback (orig
, evd
->regs_active
, max_depth
,
1693 result
= expand_loc (CSELIB_VAL_PTR (orig
)->locs
, evd
, max_depth
);
1699 return evd
->callback (orig
, evd
->regs_active
, max_depth
,
1707 /* Copy the various flags, fields, and other information. We assume
1708 that all fields need copying, and then clear the fields that should
1709 not be copied. That is the sensible default behavior, and forces
1710 us to explicitly document why we are *not* copying a flag. */
1714 copy
= shallow_copy_rtx (orig
);
1716 format_ptr
= GET_RTX_FORMAT (code
);
1718 for (i
= 0; i
< GET_RTX_LENGTH (code
); i
++)
1719 switch (*format_ptr
++)
1722 if (XEXP (orig
, i
) != NULL
)
1724 rtx result
= cselib_expand_value_rtx_1 (XEXP (orig
, i
), evd
,
1729 XEXP (copy
, i
) = result
;
1735 if (XVEC (orig
, i
) != NULL
)
1738 XVEC (copy
, i
) = rtvec_alloc (XVECLEN (orig
, i
));
1739 for (j
= 0; j
< XVECLEN (orig
, i
); j
++)
1741 rtx result
= cselib_expand_value_rtx_1 (XVECEXP (orig
, i
, j
),
1742 evd
, max_depth
- 1);
1746 XVECEXP (copy
, i
, j
) = result
;
1760 /* These are left unchanged. */
1770 mode
= GET_MODE (copy
);
1771 /* If an operand has been simplified into CONST_INT, which doesn't
1772 have a mode and the mode isn't derivable from whole rtx's mode,
1773 try simplify_*_operation first with mode from original's operand
1774 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1776 switch (GET_RTX_CLASS (code
))
1779 if (CONST_INT_P (XEXP (copy
, 0))
1780 && GET_MODE (XEXP (orig
, 0)) != VOIDmode
)
1782 scopy
= simplify_unary_operation (code
, mode
, XEXP (copy
, 0),
1783 GET_MODE (XEXP (orig
, 0)));
1788 case RTX_COMM_ARITH
:
1790 /* These expressions can derive operand modes from the whole rtx's mode. */
1793 case RTX_BITFIELD_OPS
:
1794 if (CONST_INT_P (XEXP (copy
, 0))
1795 && GET_MODE (XEXP (orig
, 0)) != VOIDmode
)
1797 scopy
= simplify_ternary_operation (code
, mode
,
1798 GET_MODE (XEXP (orig
, 0)),
1799 XEXP (copy
, 0), XEXP (copy
, 1),
1806 case RTX_COMM_COMPARE
:
1807 if (CONST_INT_P (XEXP (copy
, 0))
1808 && GET_MODE (XEXP (copy
, 1)) == VOIDmode
1809 && (GET_MODE (XEXP (orig
, 0)) != VOIDmode
1810 || GET_MODE (XEXP (orig
, 1)) != VOIDmode
))
1812 scopy
= simplify_relational_operation (code
, mode
,
1813 (GET_MODE (XEXP (orig
, 0))
1815 ? GET_MODE (XEXP (orig
, 0))
1816 : GET_MODE (XEXP (orig
, 1)),
1826 scopy
= simplify_rtx (copy
);
1832 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1833 with VALUE expressions. This way, it becomes independent of changes
1834 to registers and memory.
1835 X isn't actually modified; if modifications are needed, new rtl is
1836 allocated. However, the return value can share rtl with X.
1837 If X is within a MEM, MEMMODE must be the mode of the MEM. */
1840 cselib_subst_to_values (rtx x
, enum machine_mode memmode
)
1842 enum rtx_code code
= GET_CODE (x
);
1843 const char *fmt
= GET_RTX_FORMAT (code
);
1852 l
= REG_VALUES (REGNO (x
));
1853 if (l
&& l
->elt
== NULL
)
1855 for (; l
; l
= l
->next
)
1856 if (GET_MODE (l
->elt
->val_rtx
) == GET_MODE (x
))
1857 return l
->elt
->val_rtx
;
1862 e
= cselib_lookup_mem (x
, 0);
1863 /* This used to happen for autoincrements, but we deal with them
1864 properly now. Remove the if stmt for the next release. */
1867 /* Assign a value that doesn't match any other. */
1868 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
1873 e
= cselib_lookup (x
, GET_MODE (x
), 0, memmode
);
1883 gcc_assert (memmode
!= VOIDmode
);
1884 i
= GET_MODE_SIZE (memmode
);
1885 if (code
== PRE_DEC
)
1887 return cselib_subst_to_values (plus_constant (GET_MODE (x
),
1892 gcc_assert (memmode
!= VOIDmode
);
1893 return cselib_subst_to_values (XEXP (x
, 1), memmode
);
1898 gcc_assert (memmode
!= VOIDmode
);
1899 return cselib_subst_to_values (XEXP (x
, 0), memmode
);
1905 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1909 rtx t
= cselib_subst_to_values (XEXP (x
, i
), memmode
);
1911 if (t
!= XEXP (x
, i
))
1914 copy
= shallow_copy_rtx (x
);
1918 else if (fmt
[i
] == 'E')
1922 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1924 rtx t
= cselib_subst_to_values (XVECEXP (x
, i
, j
), memmode
);
1926 if (t
!= XVECEXP (x
, i
, j
))
1928 if (XVEC (x
, i
) == XVEC (copy
, i
))
1931 copy
= shallow_copy_rtx (x
);
1932 XVEC (copy
, i
) = shallow_copy_rtvec (XVEC (x
, i
));
1934 XVECEXP (copy
, i
, j
) = t
;
1943 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
1946 cselib_subst_to_values_from_insn (rtx x
, enum machine_mode memmode
, rtx insn
)
1949 gcc_assert (!cselib_current_insn
);
1950 cselib_current_insn
= insn
;
1951 ret
= cselib_subst_to_values (x
, memmode
);
1952 cselib_current_insn
= NULL
;
1956 /* Look up the rtl expression X in our tables and return the value it
1957 has. If CREATE is zero, we return NULL if we don't know the value.
1958 Otherwise, we create a new one if possible, using mode MODE if X
1959 doesn't have a mode (i.e. because it's a constant). When X is part
1960 of an address, MEMMODE should be the mode of the enclosing MEM if
1961 we're tracking autoinc expressions. */
1964 cselib_lookup_1 (rtx x
, enum machine_mode mode
,
1965 int create
, enum machine_mode memmode
)
1969 unsigned int hashval
;
1971 if (GET_MODE (x
) != VOIDmode
)
1972 mode
= GET_MODE (x
);
1974 if (GET_CODE (x
) == VALUE
)
1975 return CSELIB_VAL_PTR (x
);
1980 unsigned int i
= REGNO (x
);
1983 if (l
&& l
->elt
== NULL
)
1985 for (; l
; l
= l
->next
)
1986 if (mode
== GET_MODE (l
->elt
->val_rtx
))
1988 promote_debug_loc (l
->elt
->locs
);
1995 if (i
< FIRST_PSEUDO_REGISTER
)
1997 unsigned int n
= hard_regno_nregs
[i
][mode
];
1999 if (n
> max_value_regs
)
2003 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
2004 new_elt_loc_list (e
, x
);
2005 if (REG_VALUES (i
) == 0)
2007 /* Maintain the invariant that the first entry of
2008 REG_VALUES, if present, must be the value used to set the
2009 register, or NULL. */
2010 used_regs
[n_used_regs
++] = i
;
2011 REG_VALUES (i
) = new_elt_list (REG_VALUES (i
), NULL
);
2013 else if (cselib_preserve_constants
2014 && GET_MODE_CLASS (mode
) == MODE_INT
)
2016 /* During var-tracking, try harder to find equivalences
2017 for SUBREGs. If a setter sets say a DImode register
2018 and user uses that register only in SImode, add a lowpart
2020 struct elt_list
*lwider
= NULL
;
2022 if (l
&& l
->elt
== NULL
)
2024 for (; l
; l
= l
->next
)
2025 if (GET_MODE_CLASS (GET_MODE (l
->elt
->val_rtx
)) == MODE_INT
2026 && GET_MODE_SIZE (GET_MODE (l
->elt
->val_rtx
))
2027 > GET_MODE_SIZE (mode
)
2029 || GET_MODE_SIZE (GET_MODE (l
->elt
->val_rtx
))
2030 < GET_MODE_SIZE (GET_MODE (lwider
->elt
->val_rtx
))))
2032 struct elt_loc_list
*el
;
2033 if (i
< FIRST_PSEUDO_REGISTER
2034 && hard_regno_nregs
[i
][GET_MODE (l
->elt
->val_rtx
)] != 1)
2036 for (el
= l
->elt
->locs
; el
; el
= el
->next
)
2037 if (!REG_P (el
->loc
))
2044 rtx sub
= lowpart_subreg (mode
, lwider
->elt
->val_rtx
,
2045 GET_MODE (lwider
->elt
->val_rtx
));
2047 new_elt_loc_list (e
, sub
);
2050 REG_VALUES (i
)->next
= new_elt_list (REG_VALUES (i
)->next
, e
);
2051 slot
= cselib_find_slot (x
, e
->hash
, INSERT
, memmode
);
2057 return cselib_lookup_mem (x
, create
);
2059 hashval
= cselib_hash_rtx (x
, create
, memmode
);
2060 /* Can't even create if hashing is not possible. */
2064 slot
= cselib_find_slot (wrap_constant (mode
, x
), hashval
,
2065 create
? INSERT
: NO_INSERT
, memmode
);
2069 e
= (cselib_val
*) *slot
;
2073 e
= new_cselib_val (hashval
, mode
, x
);
2075 /* We have to fill the slot before calling cselib_subst_to_values:
2076 the hash table is inconsistent until we do so, and
2077 cselib_subst_to_values will need to do lookups. */
2079 new_elt_loc_list (e
, cselib_subst_to_values (x
, memmode
));
2083 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2086 cselib_lookup_from_insn (rtx x
, enum machine_mode mode
,
2087 int create
, enum machine_mode memmode
, rtx insn
)
2091 gcc_assert (!cselib_current_insn
);
2092 cselib_current_insn
= insn
;
2094 ret
= cselib_lookup (x
, mode
, create
, memmode
);
2096 cselib_current_insn
= NULL
;
2101 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2102 maintains invariants related with debug insns. */
2105 cselib_lookup (rtx x
, enum machine_mode mode
,
2106 int create
, enum machine_mode memmode
)
2108 cselib_val
*ret
= cselib_lookup_1 (x
, mode
, create
, memmode
);
2110 /* ??? Should we return NULL if we're not to create an entry, the
2111 found loc is a debug loc and cselib_current_insn is not DEBUG?
2112 If so, we should also avoid converting val to non-DEBUG; probably
2113 easiest setting cselib_current_insn to NULL before the call
2116 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
2118 fputs ("cselib lookup ", dump_file
);
2119 print_inline_rtx (dump_file
, x
, 2);
2120 fprintf (dump_file
, " => %u:%u\n",
2122 ret
? ret
->hash
: 0);
2128 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2129 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2130 is used to determine how many hard registers are being changed. If MODE
2131 is VOIDmode, then only REGNO is being changed; this is used when
2132 invalidating call clobbered registers across a call. */
2135 cselib_invalidate_regno (unsigned int regno
, enum machine_mode mode
)
2137 unsigned int endregno
;
2140 /* If we see pseudos after reload, something is _wrong_. */
2141 gcc_assert (!reload_completed
|| regno
< FIRST_PSEUDO_REGISTER
2142 || reg_renumber
[regno
] < 0);
2144 /* Determine the range of registers that must be invalidated. For
2145 pseudos, only REGNO is affected. For hard regs, we must take MODE
2146 into account, and we must also invalidate lower register numbers
2147 if they contain values that overlap REGNO. */
2148 if (regno
< FIRST_PSEUDO_REGISTER
)
2150 gcc_assert (mode
!= VOIDmode
);
2152 if (regno
< max_value_regs
)
2155 i
= regno
- max_value_regs
;
2157 endregno
= end_hard_regno (mode
, regno
);
2162 endregno
= regno
+ 1;
2165 for (; i
< endregno
; i
++)
2167 struct elt_list
**l
= ®_VALUES (i
);
2169 /* Go through all known values for this reg; if it overlaps the range
2170 we're invalidating, remove the value. */
2173 cselib_val
*v
= (*l
)->elt
;
2176 struct elt_loc_list
**p
;
2177 unsigned int this_last
= i
;
2179 if (i
< FIRST_PSEUDO_REGISTER
&& v
!= NULL
)
2180 this_last
= end_hard_regno (GET_MODE (v
->val_rtx
), i
) - 1;
2182 if (this_last
< regno
|| v
== NULL
2183 || (v
== cfa_base_preserved_val
2184 && i
== cfa_base_preserved_regno
))
2190 /* We have an overlap. */
2191 if (*l
== REG_VALUES (i
))
2193 /* Maintain the invariant that the first entry of
2194 REG_VALUES, if present, must be the value used to set
2195 the register, or NULL. This is also nice because
2196 then we won't push the same regno onto user_regs
2202 unchain_one_elt_list (l
);
2204 v
= canonical_cselib_val (v
);
2206 had_locs
= v
->locs
!= NULL
;
2207 setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
2209 /* Now, we clear the mapping from value to reg. It must exist, so
2210 this code will crash intentionally if it doesn't. */
2211 for (p
= &v
->locs
; ; p
= &(*p
)->next
)
2215 if (REG_P (x
) && REGNO (x
) == i
)
2217 unchain_one_elt_loc_list (p
);
2222 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
2224 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
2225 n_useless_debug_values
++;
2233 /* Invalidate any locations in the table which are changed because of a
2234 store to MEM_RTX. If this is called because of a non-const call
2235 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2238 cselib_invalidate_mem (rtx mem_rtx
)
2240 cselib_val
**vp
, *v
, *next
;
2244 mem_addr
= canon_rtx (get_addr (XEXP (mem_rtx
, 0)));
2245 mem_rtx
= canon_rtx (mem_rtx
);
2247 vp
= &first_containing_mem
;
2248 for (v
= *vp
; v
!= &dummy_val
; v
= next
)
2250 bool has_mem
= false;
2251 struct elt_loc_list
**p
= &v
->locs
;
2252 bool had_locs
= v
->locs
!= NULL
;
2253 rtx setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
2259 struct elt_list
**mem_chain
;
2261 /* MEMs may occur in locations only at the top level; below
2262 that every MEM or REG is substituted by its VALUE. */
2268 if (num_mems
< PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS
)
2269 && ! canon_true_dependence (mem_rtx
, GET_MODE (mem_rtx
),
2270 mem_addr
, x
, NULL_RTX
))
2278 /* This one overlaps. */
2279 /* We must have a mapping from this MEM's address to the
2280 value (E). Remove that, too. */
2281 addr
= cselib_lookup (XEXP (x
, 0), VOIDmode
, 0, GET_MODE (x
));
2282 addr
= canonical_cselib_val (addr
);
2283 gcc_checking_assert (v
== canonical_cselib_val (v
));
2284 mem_chain
= &addr
->addr_list
;
2287 cselib_val
*canon
= canonical_cselib_val ((*mem_chain
)->elt
);
2291 unchain_one_elt_list (mem_chain
);
2295 /* Record canonicalized elt. */
2296 (*mem_chain
)->elt
= canon
;
2298 mem_chain
= &(*mem_chain
)->next
;
2301 unchain_one_elt_loc_list (p
);
2304 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
2306 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
2307 n_useless_debug_values
++;
2312 next
= v
->next_containing_mem
;
2316 vp
= &(*vp
)->next_containing_mem
;
2319 v
->next_containing_mem
= NULL
;
2324 /* Invalidate DEST, which is being assigned to or clobbered. */
2327 cselib_invalidate_rtx (rtx dest
)
2329 while (GET_CODE (dest
) == SUBREG
2330 || GET_CODE (dest
) == ZERO_EXTRACT
2331 || GET_CODE (dest
) == STRICT_LOW_PART
)
2332 dest
= XEXP (dest
, 0);
2335 cselib_invalidate_regno (REGNO (dest
), GET_MODE (dest
));
2336 else if (MEM_P (dest
))
2337 cselib_invalidate_mem (dest
);
2340 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2343 cselib_invalidate_rtx_note_stores (rtx dest
, const_rtx ignore ATTRIBUTE_UNUSED
,
2344 void *data ATTRIBUTE_UNUSED
)
2346 cselib_invalidate_rtx (dest
);
2349 /* Record the result of a SET instruction. DEST is being set; the source
2350 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2351 describes its address. */
2354 cselib_record_set (rtx dest
, cselib_val
*src_elt
, cselib_val
*dest_addr_elt
)
2356 int dreg
= REG_P (dest
) ? (int) REGNO (dest
) : -1;
2358 if (src_elt
== 0 || side_effects_p (dest
))
2363 if (dreg
< FIRST_PSEUDO_REGISTER
)
2365 unsigned int n
= hard_regno_nregs
[dreg
][GET_MODE (dest
)];
2367 if (n
> max_value_regs
)
2371 if (REG_VALUES (dreg
) == 0)
2373 used_regs
[n_used_regs
++] = dreg
;
2374 REG_VALUES (dreg
) = new_elt_list (REG_VALUES (dreg
), src_elt
);
2378 /* The register should have been invalidated. */
2379 gcc_assert (REG_VALUES (dreg
)->elt
== 0);
2380 REG_VALUES (dreg
)->elt
= src_elt
;
2383 if (src_elt
->locs
== 0 && !PRESERVED_VALUE_P (src_elt
->val_rtx
))
2385 new_elt_loc_list (src_elt
, dest
);
2387 else if (MEM_P (dest
) && dest_addr_elt
!= 0
2388 && cselib_record_memory
)
2390 if (src_elt
->locs
== 0 && !PRESERVED_VALUE_P (src_elt
->val_rtx
))
2392 add_mem_for_addr (dest_addr_elt
, src_elt
, dest
);
2396 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2399 cselib_add_permanent_equiv (cselib_val
*elt
, rtx x
, rtx insn
)
2402 rtx save_cselib_current_insn
= cselib_current_insn
;
2404 gcc_checking_assert (elt
);
2405 gcc_checking_assert (PRESERVED_VALUE_P (elt
->val_rtx
));
2406 gcc_checking_assert (!side_effects_p (x
));
2408 cselib_current_insn
= insn
;
2410 nelt
= cselib_lookup (x
, GET_MODE (elt
->val_rtx
), 1, VOIDmode
);
2414 cselib_any_perm_equivs
= true;
2416 if (!PRESERVED_VALUE_P (nelt
->val_rtx
))
2417 cselib_preserve_value (nelt
);
2419 new_elt_loc_list (nelt
, elt
->val_rtx
);
2422 cselib_current_insn
= save_cselib_current_insn
;
2425 /* Return TRUE if any permanent equivalences have been recorded since
2426 the table was last initialized. */
2428 cselib_have_permanent_equivalences (void)
2430 return cselib_any_perm_equivs
;
2433 /* There is no good way to determine how many elements there can be
2434 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2435 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2437 struct cselib_record_autoinc_data
2439 struct cselib_set
*sets
;
2443 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2444 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2447 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED
, rtx op ATTRIBUTE_UNUSED
,
2448 rtx dest
, rtx src
, rtx srcoff
, void *arg
)
2450 struct cselib_record_autoinc_data
*data
;
2451 data
= (struct cselib_record_autoinc_data
*)arg
;
2453 data
->sets
[data
->n_sets
].dest
= dest
;
2456 data
->sets
[data
->n_sets
].src
= gen_rtx_PLUS (GET_MODE (src
), src
, srcoff
);
2458 data
->sets
[data
->n_sets
].src
= src
;
2465 /* Record the effects of any sets and autoincs in INSN. */
2467 cselib_record_sets (rtx insn
)
2471 struct cselib_set sets
[MAX_SETS
];
2472 rtx body
= PATTERN (insn
);
2474 int n_sets_before_autoinc
;
2475 struct cselib_record_autoinc_data data
;
2477 body
= PATTERN (insn
);
2478 if (GET_CODE (body
) == COND_EXEC
)
2480 cond
= COND_EXEC_TEST (body
);
2481 body
= COND_EXEC_CODE (body
);
2484 /* Find all sets. */
2485 if (GET_CODE (body
) == SET
)
2487 sets
[0].src
= SET_SRC (body
);
2488 sets
[0].dest
= SET_DEST (body
);
2491 else if (GET_CODE (body
) == PARALLEL
)
2493 /* Look through the PARALLEL and record the values being
2494 set, if possible. Also handle any CLOBBERs. */
2495 for (i
= XVECLEN (body
, 0) - 1; i
>= 0; --i
)
2497 rtx x
= XVECEXP (body
, 0, i
);
2499 if (GET_CODE (x
) == SET
)
2501 sets
[n_sets
].src
= SET_SRC (x
);
2502 sets
[n_sets
].dest
= SET_DEST (x
);
2509 && MEM_P (sets
[0].src
)
2510 && !cselib_record_memory
2511 && MEM_READONLY_P (sets
[0].src
))
2513 rtx note
= find_reg_equal_equiv_note (insn
);
2515 if (note
&& CONSTANT_P (XEXP (note
, 0)))
2516 sets
[0].src
= XEXP (note
, 0);
2520 data
.n_sets
= n_sets_before_autoinc
= n_sets
;
2521 for_each_inc_dec (&insn
, cselib_record_autoinc_cb
, &data
);
2522 n_sets
= data
.n_sets
;
2524 /* Look up the values that are read. Do this before invalidating the
2525 locations that are written. */
2526 for (i
= 0; i
< n_sets
; i
++)
2528 rtx dest
= sets
[i
].dest
;
2530 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2531 the low part after invalidating any knowledge about larger modes. */
2532 if (GET_CODE (sets
[i
].dest
) == STRICT_LOW_PART
)
2533 sets
[i
].dest
= dest
= XEXP (dest
, 0);
2535 /* We don't know how to record anything but REG or MEM. */
2537 || (MEM_P (dest
) && cselib_record_memory
))
2539 rtx src
= sets
[i
].src
;
2541 src
= gen_rtx_IF_THEN_ELSE (GET_MODE (dest
), cond
, src
, dest
);
2542 sets
[i
].src_elt
= cselib_lookup (src
, GET_MODE (dest
), 1, VOIDmode
);
2545 enum machine_mode address_mode
= get_address_mode (dest
);
2547 sets
[i
].dest_addr_elt
= cselib_lookup (XEXP (dest
, 0),
2552 sets
[i
].dest_addr_elt
= 0;
2556 if (cselib_record_sets_hook
)
2557 cselib_record_sets_hook (insn
, sets
, n_sets
);
2559 /* Invalidate all locations written by this insn. Note that the elts we
2560 looked up in the previous loop aren't affected, just some of their
2561 locations may go away. */
2562 note_stores (body
, cselib_invalidate_rtx_note_stores
, NULL
);
2564 for (i
= n_sets_before_autoinc
; i
< n_sets
; i
++)
2565 cselib_invalidate_rtx (sets
[i
].dest
);
2567 /* If this is an asm, look for duplicate sets. This can happen when the
2568 user uses the same value as an output multiple times. This is valid
2569 if the outputs are not actually used thereafter. Treat this case as
2570 if the value isn't actually set. We do this by smashing the destination
2571 to pc_rtx, so that we won't record the value later. */
2572 if (n_sets
>= 2 && asm_noperands (body
) >= 0)
2574 for (i
= 0; i
< n_sets
; i
++)
2576 rtx dest
= sets
[i
].dest
;
2577 if (REG_P (dest
) || MEM_P (dest
))
2580 for (j
= i
+ 1; j
< n_sets
; j
++)
2581 if (rtx_equal_p (dest
, sets
[j
].dest
))
2583 sets
[i
].dest
= pc_rtx
;
2584 sets
[j
].dest
= pc_rtx
;
2590 /* Now enter the equivalences in our tables. */
2591 for (i
= 0; i
< n_sets
; i
++)
2593 rtx dest
= sets
[i
].dest
;
2595 || (MEM_P (dest
) && cselib_record_memory
))
2596 cselib_record_set (dest
, sets
[i
].src_elt
, sets
[i
].dest_addr_elt
);
2600 /* Record the effects of INSN. */
2603 cselib_process_insn (rtx insn
)
2608 cselib_current_insn
= insn
;
2610 /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp. */
2613 && find_reg_note (insn
, REG_SETJMP
, NULL
))
2614 || (NONJUMP_INSN_P (insn
)
2615 && GET_CODE (PATTERN (insn
)) == ASM_OPERANDS
2616 && MEM_VOLATILE_P (PATTERN (insn
))))
2618 cselib_reset_table (next_uid
);
2619 cselib_current_insn
= NULL_RTX
;
2623 if (! INSN_P (insn
))
2625 cselib_current_insn
= NULL_RTX
;
2629 /* If this is a call instruction, forget anything stored in a
2630 call clobbered register, or, if this is not a const call, in
2634 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
2635 if (call_used_regs
[i
]
2636 || (REG_VALUES (i
) && REG_VALUES (i
)->elt
2637 && HARD_REGNO_CALL_PART_CLOBBERED (i
,
2638 GET_MODE (REG_VALUES (i
)->elt
->val_rtx
))))
2639 cselib_invalidate_regno (i
, reg_raw_mode
[i
]);
2641 /* Since it is not clear how cselib is going to be used, be
2642 conservative here and treat looping pure or const functions
2643 as if they were regular functions. */
2644 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn
)
2645 || !(RTL_CONST_OR_PURE_CALL_P (insn
)))
2646 cselib_invalidate_mem (callmem
);
2649 cselib_record_sets (insn
);
2651 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2652 after we have processed the insn. */
2654 for (x
= CALL_INSN_FUNCTION_USAGE (insn
); x
; x
= XEXP (x
, 1))
2655 if (GET_CODE (XEXP (x
, 0)) == CLOBBER
)
2656 cselib_invalidate_rtx (XEXP (XEXP (x
, 0), 0));
2658 cselib_current_insn
= NULL_RTX
;
2660 if (n_useless_values
> MAX_USELESS_VALUES
2661 /* remove_useless_values is linear in the hash table size. Avoid
2662 quadratic behavior for very large hashtables with very few
2663 useless elements. */
2664 && ((unsigned int)n_useless_values
2665 > (cselib_hash_table
->n_elements
2666 - cselib_hash_table
->n_deleted
2667 - n_debug_values
) / 4))
2668 remove_useless_values ();
2671 /* Initialize cselib for one pass. The caller must also call
2672 init_alias_analysis. */
2675 cselib_init (int record_what
)
2677 elt_list_pool
= create_alloc_pool ("elt_list",
2678 sizeof (struct elt_list
), 10);
2679 elt_loc_list_pool
= create_alloc_pool ("elt_loc_list",
2680 sizeof (struct elt_loc_list
), 10);
2681 cselib_val_pool
= create_alloc_pool ("cselib_val_list",
2682 sizeof (cselib_val
), 10);
2683 value_pool
= create_alloc_pool ("value", RTX_CODE_SIZE (VALUE
), 100);
2684 cselib_record_memory
= record_what
& CSELIB_RECORD_MEMORY
;
2685 cselib_preserve_constants
= record_what
& CSELIB_PRESERVE_CONSTANTS
;
2686 cselib_any_perm_equivs
= false;
2688 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2689 see canon_true_dependence. This is only created once. */
2691 callmem
= gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
));
2693 cselib_nregs
= max_reg_num ();
2695 /* We preserve reg_values to allow expensive clearing of the whole thing.
2696 Reallocate it however if it happens to be too large. */
2697 if (!reg_values
|| reg_values_size
< cselib_nregs
2698 || (reg_values_size
> 10 && reg_values_size
> cselib_nregs
* 4))
2701 /* Some space for newly emit instructions so we don't end up
2702 reallocating in between passes. */
2703 reg_values_size
= cselib_nregs
+ (63 + cselib_nregs
) / 16;
2704 reg_values
= XCNEWVEC (struct elt_list
*, reg_values_size
);
2706 used_regs
= XNEWVEC (unsigned int, cselib_nregs
);
2708 cselib_hash_table
= htab_create (31, get_value_hash
,
2709 entry_and_rtx_equal_p
, NULL
);
2713 /* Called when the current user is done with cselib. */
2716 cselib_finish (void)
2718 cselib_discard_hook
= NULL
;
2719 cselib_preserve_constants
= false;
2720 cselib_any_perm_equivs
= false;
2721 cfa_base_preserved_val
= NULL
;
2722 cfa_base_preserved_regno
= INVALID_REGNUM
;
2723 free_alloc_pool (elt_list_pool
);
2724 free_alloc_pool (elt_loc_list_pool
);
2725 free_alloc_pool (cselib_val_pool
);
2726 free_alloc_pool (value_pool
);
2727 cselib_clear_table ();
2728 htab_delete (cselib_hash_table
);
2731 cselib_hash_table
= 0;
2732 n_useless_values
= 0;
2733 n_useless_debug_values
= 0;
2738 /* Dump the cselib_val *X to FILE *info. */
2741 dump_cselib_val (void **x
, void *info
)
2743 cselib_val
*v
= (cselib_val
*)*x
;
2744 FILE *out
= (FILE *)info
;
2745 bool need_lf
= true;
2747 print_inline_rtx (out
, v
->val_rtx
, 0);
2751 struct elt_loc_list
*l
= v
->locs
;
2757 fputs (" locs:", out
);
2760 if (l
->setting_insn
)
2761 fprintf (out
, "\n from insn %i ",
2762 INSN_UID (l
->setting_insn
));
2764 fprintf (out
, "\n ");
2765 print_inline_rtx (out
, l
->loc
, 4);
2767 while ((l
= l
->next
));
2772 fputs (" no locs", out
);
2778 struct elt_list
*e
= v
->addr_list
;
2784 fputs (" addr list:", out
);
2788 print_inline_rtx (out
, e
->elt
->val_rtx
, 2);
2790 while ((e
= e
->next
));
2795 fputs (" no addrs", out
);
2799 if (v
->next_containing_mem
== &dummy_val
)
2800 fputs (" last mem\n", out
);
2801 else if (v
->next_containing_mem
)
2803 fputs (" next mem ", out
);
2804 print_inline_rtx (out
, v
->next_containing_mem
->val_rtx
, 2);
2813 /* Dump to OUT everything in the CSELIB table. */
2816 dump_cselib_table (FILE *out
)
2818 fprintf (out
, "cselib hash table:\n");
2819 htab_traverse (cselib_hash_table
, dump_cselib_val
, out
);
2820 if (first_containing_mem
!= &dummy_val
)
2822 fputs ("first mem ", out
);
2823 print_inline_rtx (out
, first_containing_mem
->val_rtx
, 2);
2826 fprintf (out
, "next uid %i\n", next_uid
);
2829 #include "gt-cselib.h"