1 /* Common subexpression elimination library for GNU compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
28 #include "double-int.h"
34 #include "tree.h"/* FIXME: For hashing DEBUG_EXPR & friends. */
37 #include "hard-reg-set.h"
39 #include "insn-config.h"
45 #include "diagnostic-core.h"
47 #include "hash-table.h"
51 #include "basic-block.h"
54 #include "alloc-pool.h"
58 /* A list of cselib_val structures. */
60 struct elt_list
*next
;
64 static bool cselib_record_memory
;
65 static bool cselib_preserve_constants
;
66 static bool cselib_any_perm_equivs
;
67 static inline void promote_debug_loc (struct elt_loc_list
*l
);
68 static struct elt_list
*new_elt_list (struct elt_list
*, cselib_val
*);
69 static void new_elt_loc_list (cselib_val
*, rtx
);
70 static void unchain_one_value (cselib_val
*);
71 static void unchain_one_elt_list (struct elt_list
**);
72 static void unchain_one_elt_loc_list (struct elt_loc_list
**);
73 static void remove_useless_values (void);
74 static int rtx_equal_for_cselib_1 (rtx
, rtx
, machine_mode
);
75 static unsigned int cselib_hash_rtx (rtx
, int, machine_mode
);
76 static cselib_val
*new_cselib_val (unsigned int, machine_mode
, rtx
);
77 static void add_mem_for_addr (cselib_val
*, cselib_val
*, rtx
);
78 static cselib_val
*cselib_lookup_mem (rtx
, int);
79 static void cselib_invalidate_regno (unsigned int, machine_mode
);
80 static void cselib_invalidate_mem (rtx
);
81 static void cselib_record_set (rtx
, cselib_val
*, cselib_val
*);
82 static void cselib_record_sets (rtx_insn
*);
84 struct expand_value_data
87 cselib_expand_callback callback
;
92 static rtx
cselib_expand_value_rtx_1 (rtx
, struct expand_value_data
*, int);
94 /* There are three ways in which cselib can look up an rtx:
95 - for a REG, the reg_values table (which is indexed by regno) is used
96 - for a MEM, we recursively look up its address and then follow the
97 addr_list of that value
98 - for everything else, we compute a hash value and go through the hash
99 table. Since different rtx's can still have the same hash value,
100 this involves walking the table entries for a given value and comparing
101 the locations of the entries with the rtx we are looking up. */
103 struct cselib_hasher
: typed_noop_remove
<cselib_val
>
105 typedef cselib_val
*value_type
;
107 /* The rtx value and its mode (needed separately for constant
111 /* The mode of the contaning MEM, if any, otherwise VOIDmode. */
112 machine_mode memmode
;
114 typedef key
*compare_type
;
115 static inline hashval_t
hash (const cselib_val
*);
116 static inline bool equal (const cselib_val
*, const key
*);
119 /* The hash function for our hash table. The value is always computed with
120 cselib_hash_rtx when adding an element; this function just extracts the
121 hash value from a cselib_val structure. */
124 cselib_hasher::hash (const cselib_val
*v
)
129 /* The equality test for our hash table. The first argument V is a table
130 element (i.e. a cselib_val), while the second arg X is an rtx. We know
131 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
132 CONST of an appropriate mode. */
135 cselib_hasher::equal (const cselib_val
*v
, const key
*x_arg
)
137 struct elt_loc_list
*l
;
139 machine_mode mode
= x_arg
->mode
;
140 machine_mode memmode
= x_arg
->memmode
;
142 if (mode
!= GET_MODE (v
->val_rtx
))
145 if (GET_CODE (x
) == VALUE
)
146 return x
== v
->val_rtx
;
148 /* We don't guarantee that distinct rtx's have different hash values,
149 so we need to do a comparison. */
150 for (l
= v
->locs
; l
; l
= l
->next
)
151 if (rtx_equal_for_cselib_1 (l
->loc
, x
, memmode
))
153 promote_debug_loc (l
);
160 /* A table that enables us to look up elts by their value. */
161 static hash_table
<cselib_hasher
> *cselib_hash_table
;
163 /* A table to hold preserved values. */
164 static hash_table
<cselib_hasher
> *cselib_preserved_hash_table
;
166 /* This is a global so we don't have to pass this through every function.
167 It is used in new_elt_loc_list to set SETTING_INSN. */
168 static rtx_insn
*cselib_current_insn
;
170 /* The unique id that the next create value will take. */
171 static unsigned int next_uid
;
173 /* The number of registers we had when the varrays were last resized. */
174 static unsigned int cselib_nregs
;
176 /* Count values without known locations, or with only locations that
177 wouldn't have been known except for debug insns. Whenever this
178 grows too big, we remove these useless values from the table.
180 Counting values with only debug values is a bit tricky. We don't
181 want to increment n_useless_values when we create a value for a
182 debug insn, for this would get n_useless_values out of sync, but we
183 want increment it if all locs in the list that were ever referenced
184 in nondebug insns are removed from the list.
186 In the general case, once we do that, we'd have to stop accepting
187 nondebug expressions in the loc list, to avoid having two values
188 equivalent that, without debug insns, would have been made into
189 separate values. However, because debug insns never introduce
190 equivalences themselves (no assignments), the only means for
191 growing loc lists is through nondebug assignments. If the locs
192 also happen to be referenced in debug insns, it will work just fine.
194 A consequence of this is that there's at most one debug-only loc in
195 each loc list. If we keep it in the first entry, testing whether
196 we have a debug-only loc list takes O(1).
198 Furthermore, since any additional entry in a loc list containing a
199 debug loc would have to come from an assignment (nondebug) that
200 references both the initial debug loc and the newly-equivalent loc,
201 the initial debug loc would be promoted to a nondebug loc, and the
202 loc list would not contain debug locs any more.
204 So the only case we have to be careful with in order to keep
205 n_useless_values in sync between debug and nondebug compilations is
206 to avoid incrementing n_useless_values when removing the single loc
207 from a value that turns out to not appear outside debug values. We
208 increment n_useless_debug_values instead, and leave such values
209 alone until, for other reasons, we garbage-collect useless
211 static int n_useless_values
;
212 static int n_useless_debug_values
;
214 /* Count values whose locs have been taken exclusively from debug
215 insns for the entire life of the value. */
216 static int n_debug_values
;
218 /* Number of useless values before we remove them from the hash table. */
219 #define MAX_USELESS_VALUES 32
221 /* This table maps from register number to values. It does not
222 contain pointers to cselib_val structures, but rather elt_lists.
223 The purpose is to be able to refer to the same register in
224 different modes. The first element of the list defines the mode in
225 which the register was set; if the mode is unknown or the value is
226 no longer valid in that mode, ELT will be NULL for the first
228 static struct elt_list
**reg_values
;
229 static unsigned int reg_values_size
;
230 #define REG_VALUES(i) reg_values[i]
232 /* The largest number of hard regs used by any entry added to the
233 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
234 static unsigned int max_value_regs
;
236 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
237 in cselib_clear_table() for fast emptying. */
238 static unsigned int *used_regs
;
239 static unsigned int n_used_regs
;
241 /* We pass this to cselib_invalidate_mem to invalidate all of
242 memory for a non-const call instruction. */
243 static GTY(()) rtx callmem
;
245 /* Set by discard_useless_locs if it deleted the last location of any
247 static int values_became_useless
;
249 /* Used as stop element of the containing_mem list so we can check
250 presence in the list by checking the next pointer. */
251 static cselib_val dummy_val
;
253 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
254 that is constant through the whole function and should never be
256 static cselib_val
*cfa_base_preserved_val
;
257 static unsigned int cfa_base_preserved_regno
= INVALID_REGNUM
;
259 /* Used to list all values that contain memory reference.
260 May or may not contain the useless values - the list is compacted
261 each time memory is invalidated. */
262 static cselib_val
*first_containing_mem
= &dummy_val
;
263 static alloc_pool elt_loc_list_pool
, elt_list_pool
, cselib_val_pool
, value_pool
;
265 /* If nonnull, cselib will call this function before freeing useless
266 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
267 void (*cselib_discard_hook
) (cselib_val
*);
269 /* If nonnull, cselib will call this function before recording sets or
270 even clobbering outputs of INSN. All the recorded sets will be
271 represented in the array sets[n_sets]. new_val_min can be used to
272 tell whether values present in sets are introduced by this
274 void (*cselib_record_sets_hook
) (rtx_insn
*insn
, struct cselib_set
*sets
,
277 #define PRESERVED_VALUE_P(RTX) \
278 (RTL_FLAG_CHECK1 ("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
280 #define SP_BASED_VALUE_P(RTX) \
281 (RTL_FLAG_CHECK1 ("SP_BASED_VALUE_P", (RTX), VALUE)->jump)
285 /* Allocate a struct elt_list and fill in its two elements with the
288 static inline struct elt_list
*
289 new_elt_list (struct elt_list
*next
, cselib_val
*elt
)
292 el
= (struct elt_list
*) pool_alloc (elt_list_pool
);
298 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
302 new_elt_loc_list (cselib_val
*val
, rtx loc
)
304 struct elt_loc_list
*el
, *next
= val
->locs
;
306 gcc_checking_assert (!next
|| !next
->setting_insn
307 || !DEBUG_INSN_P (next
->setting_insn
)
308 || cselib_current_insn
== next
->setting_insn
);
310 /* If we're creating the first loc in a debug insn context, we've
311 just created a debug value. Count it. */
312 if (!next
&& cselib_current_insn
&& DEBUG_INSN_P (cselib_current_insn
))
315 val
= canonical_cselib_val (val
);
318 if (GET_CODE (loc
) == VALUE
)
320 loc
= canonical_cselib_val (CSELIB_VAL_PTR (loc
))->val_rtx
;
322 gcc_checking_assert (PRESERVED_VALUE_P (loc
)
323 == PRESERVED_VALUE_P (val
->val_rtx
));
325 if (val
->val_rtx
== loc
)
327 else if (val
->uid
> CSELIB_VAL_PTR (loc
)->uid
)
329 /* Reverse the insertion. */
330 new_elt_loc_list (CSELIB_VAL_PTR (loc
), val
->val_rtx
);
334 gcc_checking_assert (val
->uid
< CSELIB_VAL_PTR (loc
)->uid
);
336 if (CSELIB_VAL_PTR (loc
)->locs
)
338 /* Bring all locs from LOC to VAL. */
339 for (el
= CSELIB_VAL_PTR (loc
)->locs
; el
->next
; el
= el
->next
)
341 /* Adjust values that have LOC as canonical so that VAL
342 becomes their canonical. */
343 if (el
->loc
&& GET_CODE (el
->loc
) == VALUE
)
345 gcc_checking_assert (CSELIB_VAL_PTR (el
->loc
)->locs
->loc
347 CSELIB_VAL_PTR (el
->loc
)->locs
->loc
= val
->val_rtx
;
350 el
->next
= val
->locs
;
351 next
= val
->locs
= CSELIB_VAL_PTR (loc
)->locs
;
354 if (CSELIB_VAL_PTR (loc
)->addr_list
)
356 /* Bring in addr_list into canonical node. */
357 struct elt_list
*last
= CSELIB_VAL_PTR (loc
)->addr_list
;
360 last
->next
= val
->addr_list
;
361 val
->addr_list
= CSELIB_VAL_PTR (loc
)->addr_list
;
362 CSELIB_VAL_PTR (loc
)->addr_list
= NULL
;
365 if (CSELIB_VAL_PTR (loc
)->next_containing_mem
!= NULL
366 && val
->next_containing_mem
== NULL
)
368 /* Add VAL to the containing_mem list after LOC. LOC will
369 be removed when we notice it doesn't contain any
371 val
->next_containing_mem
= CSELIB_VAL_PTR (loc
)->next_containing_mem
;
372 CSELIB_VAL_PTR (loc
)->next_containing_mem
= val
;
375 /* Chain LOC back to VAL. */
376 el
= (struct elt_loc_list
*) pool_alloc (elt_loc_list_pool
);
377 el
->loc
= val
->val_rtx
;
378 el
->setting_insn
= cselib_current_insn
;
380 CSELIB_VAL_PTR (loc
)->locs
= el
;
383 el
= (struct elt_loc_list
*) pool_alloc (elt_loc_list_pool
);
385 el
->setting_insn
= cselib_current_insn
;
390 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
391 originating from a debug insn, maintaining the debug values
395 promote_debug_loc (struct elt_loc_list
*l
)
397 if (l
&& l
->setting_insn
&& DEBUG_INSN_P (l
->setting_insn
)
398 && (!cselib_current_insn
|| !DEBUG_INSN_P (cselib_current_insn
)))
401 l
->setting_insn
= cselib_current_insn
;
402 if (cselib_preserve_constants
&& l
->next
)
404 gcc_assert (l
->next
->setting_insn
405 && DEBUG_INSN_P (l
->next
->setting_insn
)
407 l
->next
->setting_insn
= cselib_current_insn
;
410 gcc_assert (!l
->next
);
414 /* The elt_list at *PL is no longer needed. Unchain it and free its
418 unchain_one_elt_list (struct elt_list
**pl
)
420 struct elt_list
*l
= *pl
;
423 pool_free (elt_list_pool
, l
);
426 /* Likewise for elt_loc_lists. */
429 unchain_one_elt_loc_list (struct elt_loc_list
**pl
)
431 struct elt_loc_list
*l
= *pl
;
434 pool_free (elt_loc_list_pool
, l
);
437 /* Likewise for cselib_vals. This also frees the addr_list associated with
441 unchain_one_value (cselib_val
*v
)
444 unchain_one_elt_list (&v
->addr_list
);
446 pool_free (cselib_val_pool
, v
);
449 /* Remove all entries from the hash table. Also used during
453 cselib_clear_table (void)
455 cselib_reset_table (1);
458 /* Return TRUE if V is a constant, a function invariant or a VALUE
459 equivalence; FALSE otherwise. */
462 invariant_or_equiv_p (cselib_val
*v
)
464 struct elt_loc_list
*l
;
466 if (v
== cfa_base_preserved_val
)
469 /* Keep VALUE equivalences around. */
470 for (l
= v
->locs
; l
; l
= l
->next
)
471 if (GET_CODE (l
->loc
) == VALUE
)
475 && v
->locs
->next
== NULL
)
477 if (CONSTANT_P (v
->locs
->loc
)
478 && (GET_CODE (v
->locs
->loc
) != CONST
479 || !references_value_p (v
->locs
->loc
, 0)))
481 /* Although a debug expr may be bound to different expressions,
482 we can preserve it as if it was constant, to get unification
483 and proper merging within var-tracking. */
484 if (GET_CODE (v
->locs
->loc
) == DEBUG_EXPR
485 || GET_CODE (v
->locs
->loc
) == DEBUG_IMPLICIT_PTR
486 || GET_CODE (v
->locs
->loc
) == ENTRY_VALUE
487 || GET_CODE (v
->locs
->loc
) == DEBUG_PARAMETER_REF
)
490 /* (plus (value V) (const_int C)) is invariant iff V is invariant. */
491 if (GET_CODE (v
->locs
->loc
) == PLUS
492 && CONST_INT_P (XEXP (v
->locs
->loc
, 1))
493 && GET_CODE (XEXP (v
->locs
->loc
, 0)) == VALUE
494 && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v
->locs
->loc
, 0))))
501 /* Remove from hash table all VALUEs except constants, function
502 invariants and VALUE equivalences. */
505 preserve_constants_and_equivs (cselib_val
**x
, void *info ATTRIBUTE_UNUSED
)
509 if (invariant_or_equiv_p (v
))
511 cselib_hasher::key lookup
= {
512 GET_MODE (v
->val_rtx
), v
->val_rtx
, VOIDmode
515 = cselib_preserved_hash_table
->find_slot_with_hash (&lookup
,
521 cselib_hash_table
->clear_slot (x
);
526 /* Remove all entries from the hash table, arranging for the next
527 value to be numbered NUM. */
530 cselib_reset_table (unsigned int num
)
536 if (cfa_base_preserved_val
)
538 unsigned int regno
= cfa_base_preserved_regno
;
539 unsigned int new_used_regs
= 0;
540 for (i
= 0; i
< n_used_regs
; i
++)
541 if (used_regs
[i
] == regno
)
547 REG_VALUES (used_regs
[i
]) = 0;
548 gcc_assert (new_used_regs
== 1);
549 n_used_regs
= new_used_regs
;
550 used_regs
[0] = regno
;
552 = hard_regno_nregs
[regno
][GET_MODE (cfa_base_preserved_val
->locs
->loc
)];
556 for (i
= 0; i
< n_used_regs
; i
++)
557 REG_VALUES (used_regs
[i
]) = 0;
561 if (cselib_preserve_constants
)
562 cselib_hash_table
->traverse
<void *, preserve_constants_and_equivs
>
566 cselib_hash_table
->empty ();
567 gcc_checking_assert (!cselib_any_perm_equivs
);
570 n_useless_values
= 0;
571 n_useless_debug_values
= 0;
576 first_containing_mem
= &dummy_val
;
579 /* Return the number of the next value that will be generated. */
582 cselib_get_next_uid (void)
587 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
588 INSERTing if requested. When X is part of the address of a MEM,
589 MEMMODE should specify the mode of the MEM. */
592 cselib_find_slot (machine_mode mode
, rtx x
, hashval_t hash
,
593 enum insert_option insert
, machine_mode memmode
)
595 cselib_val
**slot
= NULL
;
596 cselib_hasher::key lookup
= { mode
, x
, memmode
};
597 if (cselib_preserve_constants
)
598 slot
= cselib_preserved_hash_table
->find_slot_with_hash (&lookup
, hash
,
601 slot
= cselib_hash_table
->find_slot_with_hash (&lookup
, hash
, insert
);
605 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
606 only return true for values which point to a cselib_val whose value
607 element has been set to zero, which implies the cselib_val will be
611 references_value_p (const_rtx x
, int only_useless
)
613 const enum rtx_code code
= GET_CODE (x
);
614 const char *fmt
= GET_RTX_FORMAT (code
);
617 if (GET_CODE (x
) == VALUE
618 && (! only_useless
||
619 (CSELIB_VAL_PTR (x
)->locs
== 0 && !PRESERVED_VALUE_P (x
))))
622 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
624 if (fmt
[i
] == 'e' && references_value_p (XEXP (x
, i
), only_useless
))
626 else if (fmt
[i
] == 'E')
627 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
628 if (references_value_p (XVECEXP (x
, i
, j
), only_useless
))
635 /* For all locations found in X, delete locations that reference useless
636 values (i.e. values without any location). Called through
640 discard_useless_locs (cselib_val
**x
, void *info ATTRIBUTE_UNUSED
)
643 struct elt_loc_list
**p
= &v
->locs
;
644 bool had_locs
= v
->locs
!= NULL
;
645 rtx_insn
*setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
649 if (references_value_p ((*p
)->loc
, 1))
650 unchain_one_elt_loc_list (p
);
655 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
657 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
658 n_useless_debug_values
++;
661 values_became_useless
= 1;
666 /* If X is a value with no locations, remove it from the hashtable. */
669 discard_useless_values (cselib_val
**x
, void *info ATTRIBUTE_UNUSED
)
673 if (v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
675 if (cselib_discard_hook
)
676 cselib_discard_hook (v
);
678 CSELIB_VAL_PTR (v
->val_rtx
) = NULL
;
679 cselib_hash_table
->clear_slot (x
);
680 unchain_one_value (v
);
687 /* Clean out useless values (i.e. those which no longer have locations
688 associated with them) from the hash table. */
691 remove_useless_values (void)
695 /* First pass: eliminate locations that reference the value. That in
696 turn can make more values useless. */
699 values_became_useless
= 0;
700 cselib_hash_table
->traverse
<void *, discard_useless_locs
> (NULL
);
702 while (values_became_useless
);
704 /* Second pass: actually remove the values. */
706 p
= &first_containing_mem
;
707 for (v
= *p
; v
!= &dummy_val
; v
= v
->next_containing_mem
)
708 if (v
->locs
&& v
== canonical_cselib_val (v
))
711 p
= &(*p
)->next_containing_mem
;
715 n_useless_values
+= n_useless_debug_values
;
716 n_debug_values
-= n_useless_debug_values
;
717 n_useless_debug_values
= 0;
719 cselib_hash_table
->traverse
<void *, discard_useless_values
> (NULL
);
721 gcc_assert (!n_useless_values
);
724 /* Arrange for a value to not be removed from the hash table even if
725 it becomes useless. */
728 cselib_preserve_value (cselib_val
*v
)
730 PRESERVED_VALUE_P (v
->val_rtx
) = 1;
733 /* Test whether a value is preserved. */
736 cselib_preserved_value_p (cselib_val
*v
)
738 return PRESERVED_VALUE_P (v
->val_rtx
);
741 /* Arrange for a REG value to be assumed constant through the whole function,
742 never invalidated and preserved across cselib_reset_table calls. */
745 cselib_preserve_cfa_base_value (cselib_val
*v
, unsigned int regno
)
747 if (cselib_preserve_constants
749 && REG_P (v
->locs
->loc
))
751 cfa_base_preserved_val
= v
;
752 cfa_base_preserved_regno
= regno
;
756 /* Clean all non-constant expressions in the hash table, but retain
760 cselib_preserve_only_values (void)
764 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
765 cselib_invalidate_regno (i
, reg_raw_mode
[i
]);
767 cselib_invalidate_mem (callmem
);
769 remove_useless_values ();
771 gcc_assert (first_containing_mem
== &dummy_val
);
774 /* Arrange for a value to be marked as based on stack pointer
775 for find_base_term purposes. */
778 cselib_set_value_sp_based (cselib_val
*v
)
780 SP_BASED_VALUE_P (v
->val_rtx
) = 1;
783 /* Test whether a value is based on stack pointer for
784 find_base_term purposes. */
787 cselib_sp_based_value_p (cselib_val
*v
)
789 return SP_BASED_VALUE_P (v
->val_rtx
);
792 /* Return the mode in which a register was last set. If X is not a
793 register, return its mode. If the mode in which the register was
794 set is not known, or the value was already clobbered, return
798 cselib_reg_set_mode (const_rtx x
)
803 if (REG_VALUES (REGNO (x
)) == NULL
804 || REG_VALUES (REGNO (x
))->elt
== NULL
)
807 return GET_MODE (REG_VALUES (REGNO (x
))->elt
->val_rtx
);
810 /* Return nonzero if we can prove that X and Y contain the same value, taking
811 our gathered information into account. */
814 rtx_equal_for_cselib_p (rtx x
, rtx y
)
816 return rtx_equal_for_cselib_1 (x
, y
, VOIDmode
);
819 /* If x is a PLUS or an autoinc operation, expand the operation,
820 storing the offset, if any, in *OFF. */
823 autoinc_split (rtx x
, rtx
*off
, machine_mode memmode
)
825 switch (GET_CODE (x
))
832 if (memmode
== VOIDmode
)
835 *off
= GEN_INT (-GET_MODE_SIZE (memmode
));
840 if (memmode
== VOIDmode
)
843 *off
= GEN_INT (GET_MODE_SIZE (memmode
));
859 /* Return nonzero if we can prove that X and Y contain the same value,
860 taking our gathered information into account. MEMMODE holds the
861 mode of the enclosing MEM, if any, as required to deal with autoinc
862 addressing modes. If X and Y are not (known to be) part of
863 addresses, MEMMODE should be VOIDmode. */
866 rtx_equal_for_cselib_1 (rtx x
, rtx y
, machine_mode memmode
)
872 if (REG_P (x
) || MEM_P (x
))
874 cselib_val
*e
= cselib_lookup (x
, GET_MODE (x
), 0, memmode
);
880 if (REG_P (y
) || MEM_P (y
))
882 cselib_val
*e
= cselib_lookup (y
, GET_MODE (y
), 0, memmode
);
891 if (GET_CODE (x
) == VALUE
)
893 cselib_val
*e
= canonical_cselib_val (CSELIB_VAL_PTR (x
));
894 struct elt_loc_list
*l
;
896 if (GET_CODE (y
) == VALUE
)
897 return e
== canonical_cselib_val (CSELIB_VAL_PTR (y
));
899 for (l
= e
->locs
; l
; l
= l
->next
)
903 /* Avoid infinite recursion. We know we have the canonical
904 value, so we can just skip any values in the equivalence
906 if (REG_P (t
) || MEM_P (t
) || GET_CODE (t
) == VALUE
)
908 else if (rtx_equal_for_cselib_1 (t
, y
, memmode
))
914 else if (GET_CODE (y
) == VALUE
)
916 cselib_val
*e
= canonical_cselib_val (CSELIB_VAL_PTR (y
));
917 struct elt_loc_list
*l
;
919 for (l
= e
->locs
; l
; l
= l
->next
)
923 if (REG_P (t
) || MEM_P (t
) || GET_CODE (t
) == VALUE
)
925 else if (rtx_equal_for_cselib_1 (x
, t
, memmode
))
932 if (GET_MODE (x
) != GET_MODE (y
))
935 if (GET_CODE (x
) != GET_CODE (y
))
937 rtx xorig
= x
, yorig
= y
;
938 rtx xoff
= NULL
, yoff
= NULL
;
940 x
= autoinc_split (x
, &xoff
, memmode
);
941 y
= autoinc_split (y
, &yoff
, memmode
);
946 if (xoff
&& !rtx_equal_for_cselib_1 (xoff
, yoff
, memmode
))
949 /* Don't recurse if nothing changed. */
950 if (x
!= xorig
|| y
!= yorig
)
951 return rtx_equal_for_cselib_1 (x
, y
, memmode
);
956 /* These won't be handled correctly by the code below. */
957 switch (GET_CODE (x
))
963 case DEBUG_IMPLICIT_PTR
:
964 return DEBUG_IMPLICIT_PTR_DECL (x
)
965 == DEBUG_IMPLICIT_PTR_DECL (y
);
967 case DEBUG_PARAMETER_REF
:
968 return DEBUG_PARAMETER_REF_DECL (x
)
969 == DEBUG_PARAMETER_REF_DECL (y
);
972 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
973 use rtx_equal_for_cselib_1 to compare the operands. */
974 return rtx_equal_p (ENTRY_VALUE_EXP (x
), ENTRY_VALUE_EXP (y
));
977 return LABEL_REF_LABEL (x
) == LABEL_REF_LABEL (y
);
980 return REGNO (x
) == REGNO (y
);
983 /* We have to compare any autoinc operations in the addresses
984 using this MEM's mode. */
985 return rtx_equal_for_cselib_1 (XEXP (x
, 0), XEXP (y
, 0), GET_MODE (x
));
992 fmt
= GET_RTX_FORMAT (code
);
994 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1001 if (XWINT (x
, i
) != XWINT (y
, i
))
1007 if (XINT (x
, i
) != XINT (y
, i
))
1013 /* Two vectors must have the same length. */
1014 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
1017 /* And the corresponding elements must match. */
1018 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1019 if (! rtx_equal_for_cselib_1 (XVECEXP (x
, i
, j
),
1020 XVECEXP (y
, i
, j
), memmode
))
1026 && targetm
.commutative_p (x
, UNKNOWN
)
1027 && rtx_equal_for_cselib_1 (XEXP (x
, 1), XEXP (y
, 0), memmode
)
1028 && rtx_equal_for_cselib_1 (XEXP (x
, 0), XEXP (y
, 1), memmode
))
1030 if (! rtx_equal_for_cselib_1 (XEXP (x
, i
), XEXP (y
, i
), memmode
))
1036 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
1041 /* These are just backpointers, so they don't matter. */
1048 /* It is believed that rtx's at this level will never
1049 contain anything but integers and other rtx's,
1050 except for within LABEL_REFs and SYMBOL_REFs. */
1058 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1059 For registers and memory locations, we look up their cselib_val structure
1060 and return its VALUE element.
1061 Possible reasons for return 0 are: the object is volatile, or we couldn't
1062 find a register or memory location in the table and CREATE is zero. If
1063 CREATE is nonzero, table elts are created for regs and mem.
1064 N.B. this hash function returns the same hash value for RTXes that
1065 differ only in the order of operands, thus it is suitable for comparisons
1066 that take commutativity into account.
1067 If we wanted to also support associative rules, we'd have to use a different
1068 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1069 MEMMODE indicates the mode of an enclosing MEM, and it's only
1070 used to compute autoinc values.
1071 We used to have a MODE argument for hashing for CONST_INTs, but that
1072 didn't make sense, since it caused spurious hash differences between
1073 (set (reg:SI 1) (const_int))
1074 (plus:SI (reg:SI 2) (reg:SI 1))
1076 (plus:SI (reg:SI 2) (const_int))
1077 If the mode is important in any context, it must be checked specifically
1078 in a comparison anyway, since relying on hash differences is unsafe. */
1081 cselib_hash_rtx (rtx x
, int create
, machine_mode memmode
)
1087 unsigned int hash
= 0;
1089 code
= GET_CODE (x
);
1090 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
1095 e
= CSELIB_VAL_PTR (x
);
1100 e
= cselib_lookup (x
, GET_MODE (x
), create
, memmode
);
1107 hash
+= ((unsigned) DEBUG_EXPR
<< 7)
1108 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x
));
1109 return hash
? hash
: (unsigned int) DEBUG_EXPR
;
1111 case DEBUG_IMPLICIT_PTR
:
1112 hash
+= ((unsigned) DEBUG_IMPLICIT_PTR
<< 7)
1113 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x
));
1114 return hash
? hash
: (unsigned int) DEBUG_IMPLICIT_PTR
;
1116 case DEBUG_PARAMETER_REF
:
1117 hash
+= ((unsigned) DEBUG_PARAMETER_REF
<< 7)
1118 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x
));
1119 return hash
? hash
: (unsigned int) DEBUG_PARAMETER_REF
;
1122 /* ENTRY_VALUEs are function invariant, thus try to avoid
1123 recursing on argument if ENTRY_VALUE is one of the
1124 forms emitted by expand_debug_expr, otherwise
1125 ENTRY_VALUE hash would depend on the current value
1126 in some register or memory. */
1127 if (REG_P (ENTRY_VALUE_EXP (x
)))
1128 hash
+= (unsigned int) REG
1129 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x
))
1130 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x
));
1131 else if (MEM_P (ENTRY_VALUE_EXP (x
))
1132 && REG_P (XEXP (ENTRY_VALUE_EXP (x
), 0)))
1133 hash
+= (unsigned int) MEM
1134 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x
), 0))
1135 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x
), 0));
1137 hash
+= cselib_hash_rtx (ENTRY_VALUE_EXP (x
), create
, memmode
);
1138 return hash
? hash
: (unsigned int) ENTRY_VALUE
;
1141 hash
+= ((unsigned) CONST_INT
<< 7) + UINTVAL (x
);
1142 return hash
? hash
: (unsigned int) CONST_INT
;
1144 case CONST_WIDE_INT
:
1145 for (i
= 0; i
< CONST_WIDE_INT_NUNITS (x
); i
++)
1146 hash
+= CONST_WIDE_INT_ELT (x
, i
);
1150 /* This is like the general case, except that it only counts
1151 the integers representing the constant. */
1152 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
1153 if (TARGET_SUPPORTS_WIDE_INT
== 0 && GET_MODE (x
) == VOIDmode
)
1154 hash
+= ((unsigned) CONST_DOUBLE_LOW (x
)
1155 + (unsigned) CONST_DOUBLE_HIGH (x
));
1157 hash
+= real_hash (CONST_DOUBLE_REAL_VALUE (x
));
1158 return hash
? hash
: (unsigned int) CONST_DOUBLE
;
1161 hash
+= (unsigned int) code
+ (unsigned int) GET_MODE (x
);
1162 hash
+= fixed_hash (CONST_FIXED_VALUE (x
));
1163 return hash
? hash
: (unsigned int) CONST_FIXED
;
1170 units
= CONST_VECTOR_NUNITS (x
);
1172 for (i
= 0; i
< units
; ++i
)
1174 elt
= CONST_VECTOR_ELT (x
, i
);
1175 hash
+= cselib_hash_rtx (elt
, 0, memmode
);
1181 /* Assume there is only one rtx object for any given label. */
1183 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1184 differences and differences between each stage's debugging dumps. */
1185 hash
+= (((unsigned int) LABEL_REF
<< 7)
1186 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x
)));
1187 return hash
? hash
: (unsigned int) LABEL_REF
;
1191 /* Don't hash on the symbol's address to avoid bootstrap differences.
1192 Different hash values may cause expressions to be recorded in
1193 different orders and thus different registers to be used in the
1194 final assembler. This also avoids differences in the dump files
1195 between various stages. */
1197 const unsigned char *p
= (const unsigned char *) XSTR (x
, 0);
1200 h
+= (h
<< 7) + *p
++; /* ??? revisit */
1202 hash
+= ((unsigned int) SYMBOL_REF
<< 7) + h
;
1203 return hash
? hash
: (unsigned int) SYMBOL_REF
;
1208 /* We can't compute these without knowing the MEM mode. */
1209 gcc_assert (memmode
!= VOIDmode
);
1210 i
= GET_MODE_SIZE (memmode
);
1211 if (code
== PRE_DEC
)
1213 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1214 like (mem:MEMMODE (plus (reg) (const_int I))). */
1215 hash
+= (unsigned) PLUS
- (unsigned)code
1216 + cselib_hash_rtx (XEXP (x
, 0), create
, memmode
)
1217 + cselib_hash_rtx (GEN_INT (i
), create
, memmode
);
1218 return hash
? hash
: 1 + (unsigned) PLUS
;
1221 gcc_assert (memmode
!= VOIDmode
);
1222 return cselib_hash_rtx (XEXP (x
, 1), create
, memmode
);
1227 gcc_assert (memmode
!= VOIDmode
);
1228 return cselib_hash_rtx (XEXP (x
, 0), create
, memmode
);
1233 case UNSPEC_VOLATILE
:
1237 if (MEM_VOLATILE_P (x
))
1246 i
= GET_RTX_LENGTH (code
) - 1;
1247 fmt
= GET_RTX_FORMAT (code
);
1254 rtx tem
= XEXP (x
, i
);
1255 unsigned int tem_hash
= cselib_hash_rtx (tem
, create
, memmode
);
1264 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1266 unsigned int tem_hash
1267 = cselib_hash_rtx (XVECEXP (x
, i
, j
), create
, memmode
);
1278 const unsigned char *p
= (const unsigned char *) XSTR (x
, i
);
1287 hash
+= XINT (x
, i
);
1300 return hash
? hash
: 1 + (unsigned int) GET_CODE (x
);
1303 /* Create a new value structure for VALUE and initialize it. The mode of the
1306 static inline cselib_val
*
1307 new_cselib_val (unsigned int hash
, machine_mode mode
, rtx x
)
1309 cselib_val
*e
= (cselib_val
*) pool_alloc (cselib_val_pool
);
1312 gcc_assert (next_uid
);
1315 e
->uid
= next_uid
++;
1316 /* We use an alloc pool to allocate this RTL construct because it
1317 accounts for about 8% of the overall memory usage. We know
1318 precisely when we can have VALUE RTXen (when cselib is active)
1319 so we don't need to put them in garbage collected memory.
1320 ??? Why should a VALUE be an RTX in the first place? */
1321 e
->val_rtx
= (rtx
) pool_alloc (value_pool
);
1322 memset (e
->val_rtx
, 0, RTX_HDR_SIZE
);
1323 PUT_CODE (e
->val_rtx
, VALUE
);
1324 PUT_MODE (e
->val_rtx
, mode
);
1325 CSELIB_VAL_PTR (e
->val_rtx
) = e
;
1328 e
->next_containing_mem
= 0;
1330 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1332 fprintf (dump_file
, "cselib value %u:%u ", e
->uid
, hash
);
1333 if (flag_dump_noaddr
|| flag_dump_unnumbered
)
1334 fputs ("# ", dump_file
);
1336 fprintf (dump_file
, "%p ", (void*)e
);
1337 print_rtl_single (dump_file
, x
);
1338 fputc ('\n', dump_file
);
1344 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1345 contains the data at this address. X is a MEM that represents the
1346 value. Update the two value structures to represent this situation. */
1349 add_mem_for_addr (cselib_val
*addr_elt
, cselib_val
*mem_elt
, rtx x
)
1351 struct elt_loc_list
*l
;
1353 addr_elt
= canonical_cselib_val (addr_elt
);
1354 mem_elt
= canonical_cselib_val (mem_elt
);
1356 /* Avoid duplicates. */
1357 for (l
= mem_elt
->locs
; l
; l
= l
->next
)
1359 && CSELIB_VAL_PTR (XEXP (l
->loc
, 0)) == addr_elt
)
1361 promote_debug_loc (l
);
1365 addr_elt
->addr_list
= new_elt_list (addr_elt
->addr_list
, mem_elt
);
1366 new_elt_loc_list (mem_elt
,
1367 replace_equiv_address_nv (x
, addr_elt
->val_rtx
));
1368 if (mem_elt
->next_containing_mem
== NULL
)
1370 mem_elt
->next_containing_mem
= first_containing_mem
;
1371 first_containing_mem
= mem_elt
;
1375 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1376 If CREATE, make a new one if we haven't seen it before. */
1379 cselib_lookup_mem (rtx x
, int create
)
1381 machine_mode mode
= GET_MODE (x
);
1382 machine_mode addr_mode
;
1385 cselib_val
*mem_elt
;
1388 if (MEM_VOLATILE_P (x
) || mode
== BLKmode
1389 || !cselib_record_memory
1390 || (FLOAT_MODE_P (mode
) && flag_float_store
))
1393 addr_mode
= GET_MODE (XEXP (x
, 0));
1394 if (addr_mode
== VOIDmode
)
1397 /* Look up the value for the address. */
1398 addr
= cselib_lookup (XEXP (x
, 0), addr_mode
, create
, mode
);
1402 addr
= canonical_cselib_val (addr
);
1403 /* Find a value that describes a value of our mode at that address. */
1404 for (l
= addr
->addr_list
; l
; l
= l
->next
)
1405 if (GET_MODE (l
->elt
->val_rtx
) == mode
)
1407 promote_debug_loc (l
->elt
->locs
);
1414 mem_elt
= new_cselib_val (next_uid
, mode
, x
);
1415 add_mem_for_addr (addr
, mem_elt
, x
);
1416 slot
= cselib_find_slot (mode
, x
, mem_elt
->hash
, INSERT
, VOIDmode
);
1421 /* Search through the possible substitutions in P. We prefer a non reg
1422 substitution because this allows us to expand the tree further. If
1423 we find, just a reg, take the lowest regno. There may be several
1424 non-reg results, we just take the first one because they will all
1425 expand to the same place. */
1428 expand_loc (struct elt_loc_list
*p
, struct expand_value_data
*evd
,
1431 rtx reg_result
= NULL
;
1432 unsigned int regno
= UINT_MAX
;
1433 struct elt_loc_list
*p_in
= p
;
1435 for (; p
; p
= p
->next
)
1437 /* Return these right away to avoid returning stack pointer based
1438 expressions for frame pointer and vice versa, which is something
1439 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1440 for more details. */
1442 && (REGNO (p
->loc
) == STACK_POINTER_REGNUM
1443 || REGNO (p
->loc
) == FRAME_POINTER_REGNUM
1444 || REGNO (p
->loc
) == HARD_FRAME_POINTER_REGNUM
1445 || REGNO (p
->loc
) == cfa_base_preserved_regno
))
1447 /* Avoid infinite recursion trying to expand a reg into a
1449 if ((REG_P (p
->loc
))
1450 && (REGNO (p
->loc
) < regno
)
1451 && !bitmap_bit_p (evd
->regs_active
, REGNO (p
->loc
)))
1453 reg_result
= p
->loc
;
1454 regno
= REGNO (p
->loc
);
1456 /* Avoid infinite recursion and do not try to expand the
1458 else if (GET_CODE (p
->loc
) == VALUE
1459 && CSELIB_VAL_PTR (p
->loc
)->locs
== p_in
)
1461 else if (!REG_P (p
->loc
))
1464 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1466 print_inline_rtx (dump_file
, p
->loc
, 0);
1467 fprintf (dump_file
, "\n");
1469 if (GET_CODE (p
->loc
) == LO_SUM
1470 && GET_CODE (XEXP (p
->loc
, 1)) == SYMBOL_REF
1472 && (note
= find_reg_note (p
->setting_insn
, REG_EQUAL
, NULL_RTX
))
1473 && XEXP (note
, 0) == XEXP (p
->loc
, 1))
1474 return XEXP (p
->loc
, 1);
1475 result
= cselib_expand_value_rtx_1 (p
->loc
, evd
, max_depth
- 1);
1482 if (regno
!= UINT_MAX
)
1485 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1486 fprintf (dump_file
, "r%d\n", regno
);
1488 result
= cselib_expand_value_rtx_1 (reg_result
, evd
, max_depth
- 1);
1493 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1497 print_inline_rtx (dump_file
, reg_result
, 0);
1498 fprintf (dump_file
, "\n");
1501 fprintf (dump_file
, "NULL\n");
1507 /* Forward substitute and expand an expression out to its roots.
1508 This is the opposite of common subexpression. Because local value
1509 numbering is such a weak optimization, the expanded expression is
1510 pretty much unique (not from a pointer equals point of view but
1511 from a tree shape point of view.
1513 This function returns NULL if the expansion fails. The expansion
1514 will fail if there is no value number for one of the operands or if
1515 one of the operands has been overwritten between the current insn
1516 and the beginning of the basic block. For instance x has no
1522 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1523 It is clear on return. */
1526 cselib_expand_value_rtx (rtx orig
, bitmap regs_active
, int max_depth
)
1528 struct expand_value_data evd
;
1530 evd
.regs_active
= regs_active
;
1531 evd
.callback
= NULL
;
1532 evd
.callback_arg
= NULL
;
1535 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
);
1538 /* Same as cselib_expand_value_rtx, but using a callback to try to
1539 resolve some expressions. The CB function should return ORIG if it
1540 can't or does not want to deal with a certain RTX. Any other
1541 return value, including NULL, will be used as the expansion for
1542 VALUE, without any further changes. */
1545 cselib_expand_value_rtx_cb (rtx orig
, bitmap regs_active
, int max_depth
,
1546 cselib_expand_callback cb
, void *data
)
1548 struct expand_value_data evd
;
1550 evd
.regs_active
= regs_active
;
1552 evd
.callback_arg
= data
;
1555 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
);
1558 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1559 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1560 would return NULL or non-NULL, without allocating new rtx. */
1563 cselib_dummy_expand_value_rtx_cb (rtx orig
, bitmap regs_active
, int max_depth
,
1564 cselib_expand_callback cb
, void *data
)
1566 struct expand_value_data evd
;
1568 evd
.regs_active
= regs_active
;
1570 evd
.callback_arg
= data
;
1573 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
) != NULL
;
1576 /* Internal implementation of cselib_expand_value_rtx and
1577 cselib_expand_value_rtx_cb. */
1580 cselib_expand_value_rtx_1 (rtx orig
, struct expand_value_data
*evd
,
1586 const char *format_ptr
;
1589 code
= GET_CODE (orig
);
1591 /* For the context of dse, if we end up expand into a huge tree, we
1592 will not have a useful address, so we might as well just give up
1601 struct elt_list
*l
= REG_VALUES (REGNO (orig
));
1603 if (l
&& l
->elt
== NULL
)
1605 for (; l
; l
= l
->next
)
1606 if (GET_MODE (l
->elt
->val_rtx
) == GET_MODE (orig
))
1609 unsigned regno
= REGNO (orig
);
1611 /* The only thing that we are not willing to do (this
1612 is requirement of dse and if others potential uses
1613 need this function we should add a parm to control
1614 it) is that we will not substitute the
1615 STACK_POINTER_REGNUM, FRAME_POINTER or the
1618 These expansions confuses the code that notices that
1619 stores into the frame go dead at the end of the
1620 function and that the frame is not effected by calls
1621 to subroutines. If you allow the
1622 STACK_POINTER_REGNUM substitution, then dse will
1623 think that parameter pushing also goes dead which is
1624 wrong. If you allow the FRAME_POINTER or the
1625 HARD_FRAME_POINTER then you lose the opportunity to
1626 make the frame assumptions. */
1627 if (regno
== STACK_POINTER_REGNUM
1628 || regno
== FRAME_POINTER_REGNUM
1629 || regno
== HARD_FRAME_POINTER_REGNUM
1630 || regno
== cfa_base_preserved_regno
)
1633 bitmap_set_bit (evd
->regs_active
, regno
);
1635 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1636 fprintf (dump_file
, "expanding: r%d into: ", regno
);
1638 result
= expand_loc (l
->elt
->locs
, evd
, max_depth
);
1639 bitmap_clear_bit (evd
->regs_active
, regno
);
1654 /* SCRATCH must be shared because they represent distinct values. */
1657 if (REG_P (XEXP (orig
, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig
, 0))))
1662 if (shared_const_p (orig
))
1672 subreg
= evd
->callback (orig
, evd
->regs_active
, max_depth
,
1678 subreg
= cselib_expand_value_rtx_1 (SUBREG_REG (orig
), evd
,
1682 scopy
= simplify_gen_subreg (GET_MODE (orig
), subreg
,
1683 GET_MODE (SUBREG_REG (orig
)),
1684 SUBREG_BYTE (orig
));
1686 || (GET_CODE (scopy
) == SUBREG
1687 && !REG_P (SUBREG_REG (scopy
))
1688 && !MEM_P (SUBREG_REG (scopy
))))
1698 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1700 fputs ("\nexpanding ", dump_file
);
1701 print_rtl_single (dump_file
, orig
);
1702 fputs (" into...", dump_file
);
1707 result
= evd
->callback (orig
, evd
->regs_active
, max_depth
,
1714 result
= expand_loc (CSELIB_VAL_PTR (orig
)->locs
, evd
, max_depth
);
1720 return evd
->callback (orig
, evd
->regs_active
, max_depth
,
1728 /* Copy the various flags, fields, and other information. We assume
1729 that all fields need copying, and then clear the fields that should
1730 not be copied. That is the sensible default behavior, and forces
1731 us to explicitly document why we are *not* copying a flag. */
1735 copy
= shallow_copy_rtx (orig
);
1737 format_ptr
= GET_RTX_FORMAT (code
);
1739 for (i
= 0; i
< GET_RTX_LENGTH (code
); i
++)
1740 switch (*format_ptr
++)
1743 if (XEXP (orig
, i
) != NULL
)
1745 rtx result
= cselib_expand_value_rtx_1 (XEXP (orig
, i
), evd
,
1750 XEXP (copy
, i
) = result
;
1756 if (XVEC (orig
, i
) != NULL
)
1759 XVEC (copy
, i
) = rtvec_alloc (XVECLEN (orig
, i
));
1760 for (j
= 0; j
< XVECLEN (orig
, i
); j
++)
1762 rtx result
= cselib_expand_value_rtx_1 (XVECEXP (orig
, i
, j
),
1763 evd
, max_depth
- 1);
1767 XVECEXP (copy
, i
, j
) = result
;
1781 /* These are left unchanged. */
1791 mode
= GET_MODE (copy
);
1792 /* If an operand has been simplified into CONST_INT, which doesn't
1793 have a mode and the mode isn't derivable from whole rtx's mode,
1794 try simplify_*_operation first with mode from original's operand
1795 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1797 switch (GET_RTX_CLASS (code
))
1800 if (CONST_INT_P (XEXP (copy
, 0))
1801 && GET_MODE (XEXP (orig
, 0)) != VOIDmode
)
1803 scopy
= simplify_unary_operation (code
, mode
, XEXP (copy
, 0),
1804 GET_MODE (XEXP (orig
, 0)));
1809 case RTX_COMM_ARITH
:
1811 /* These expressions can derive operand modes from the whole rtx's mode. */
1814 case RTX_BITFIELD_OPS
:
1815 if (CONST_INT_P (XEXP (copy
, 0))
1816 && GET_MODE (XEXP (orig
, 0)) != VOIDmode
)
1818 scopy
= simplify_ternary_operation (code
, mode
,
1819 GET_MODE (XEXP (orig
, 0)),
1820 XEXP (copy
, 0), XEXP (copy
, 1),
1827 case RTX_COMM_COMPARE
:
1828 if (CONST_INT_P (XEXP (copy
, 0))
1829 && GET_MODE (XEXP (copy
, 1)) == VOIDmode
1830 && (GET_MODE (XEXP (orig
, 0)) != VOIDmode
1831 || GET_MODE (XEXP (orig
, 1)) != VOIDmode
))
1833 scopy
= simplify_relational_operation (code
, mode
,
1834 (GET_MODE (XEXP (orig
, 0))
1836 ? GET_MODE (XEXP (orig
, 0))
1837 : GET_MODE (XEXP (orig
, 1)),
1847 scopy
= simplify_rtx (copy
);
1853 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1854 with VALUE expressions. This way, it becomes independent of changes
1855 to registers and memory.
1856 X isn't actually modified; if modifications are needed, new rtl is
1857 allocated. However, the return value can share rtl with X.
1858 If X is within a MEM, MEMMODE must be the mode of the MEM. */
1861 cselib_subst_to_values (rtx x
, machine_mode memmode
)
1863 enum rtx_code code
= GET_CODE (x
);
1864 const char *fmt
= GET_RTX_FORMAT (code
);
1873 l
= REG_VALUES (REGNO (x
));
1874 if (l
&& l
->elt
== NULL
)
1876 for (; l
; l
= l
->next
)
1877 if (GET_MODE (l
->elt
->val_rtx
) == GET_MODE (x
))
1878 return l
->elt
->val_rtx
;
1883 e
= cselib_lookup_mem (x
, 0);
1884 /* This used to happen for autoincrements, but we deal with them
1885 properly now. Remove the if stmt for the next release. */
1888 /* Assign a value that doesn't match any other. */
1889 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
1894 e
= cselib_lookup (x
, GET_MODE (x
), 0, memmode
);
1904 gcc_assert (memmode
!= VOIDmode
);
1905 i
= GET_MODE_SIZE (memmode
);
1906 if (code
== PRE_DEC
)
1908 return cselib_subst_to_values (plus_constant (GET_MODE (x
),
1913 gcc_assert (memmode
!= VOIDmode
);
1914 return cselib_subst_to_values (XEXP (x
, 1), memmode
);
1919 gcc_assert (memmode
!= VOIDmode
);
1920 return cselib_subst_to_values (XEXP (x
, 0), memmode
);
1926 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1930 rtx t
= cselib_subst_to_values (XEXP (x
, i
), memmode
);
1932 if (t
!= XEXP (x
, i
))
1935 copy
= shallow_copy_rtx (x
);
1939 else if (fmt
[i
] == 'E')
1943 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1945 rtx t
= cselib_subst_to_values (XVECEXP (x
, i
, j
), memmode
);
1947 if (t
!= XVECEXP (x
, i
, j
))
1949 if (XVEC (x
, i
) == XVEC (copy
, i
))
1952 copy
= shallow_copy_rtx (x
);
1953 XVEC (copy
, i
) = shallow_copy_rtvec (XVEC (x
, i
));
1955 XVECEXP (copy
, i
, j
) = t
;
1964 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
1967 cselib_subst_to_values_from_insn (rtx x
, machine_mode memmode
, rtx_insn
*insn
)
1970 gcc_assert (!cselib_current_insn
);
1971 cselib_current_insn
= insn
;
1972 ret
= cselib_subst_to_values (x
, memmode
);
1973 cselib_current_insn
= NULL
;
1977 /* Look up the rtl expression X in our tables and return the value it
1978 has. If CREATE is zero, we return NULL if we don't know the value.
1979 Otherwise, we create a new one if possible, using mode MODE if X
1980 doesn't have a mode (i.e. because it's a constant). When X is part
1981 of an address, MEMMODE should be the mode of the enclosing MEM if
1982 we're tracking autoinc expressions. */
1985 cselib_lookup_1 (rtx x
, machine_mode mode
,
1986 int create
, machine_mode memmode
)
1990 unsigned int hashval
;
1992 if (GET_MODE (x
) != VOIDmode
)
1993 mode
= GET_MODE (x
);
1995 if (GET_CODE (x
) == VALUE
)
1996 return CSELIB_VAL_PTR (x
);
2001 unsigned int i
= REGNO (x
);
2004 if (l
&& l
->elt
== NULL
)
2006 for (; l
; l
= l
->next
)
2007 if (mode
== GET_MODE (l
->elt
->val_rtx
))
2009 promote_debug_loc (l
->elt
->locs
);
2016 if (i
< FIRST_PSEUDO_REGISTER
)
2018 unsigned int n
= hard_regno_nregs
[i
][mode
];
2020 if (n
> max_value_regs
)
2024 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
2025 new_elt_loc_list (e
, x
);
2026 if (REG_VALUES (i
) == 0)
2028 /* Maintain the invariant that the first entry of
2029 REG_VALUES, if present, must be the value used to set the
2030 register, or NULL. */
2031 used_regs
[n_used_regs
++] = i
;
2032 REG_VALUES (i
) = new_elt_list (REG_VALUES (i
), NULL
);
2034 else if (cselib_preserve_constants
2035 && GET_MODE_CLASS (mode
) == MODE_INT
)
2037 /* During var-tracking, try harder to find equivalences
2038 for SUBREGs. If a setter sets say a DImode register
2039 and user uses that register only in SImode, add a lowpart
2041 struct elt_list
*lwider
= NULL
;
2043 if (l
&& l
->elt
== NULL
)
2045 for (; l
; l
= l
->next
)
2046 if (GET_MODE_CLASS (GET_MODE (l
->elt
->val_rtx
)) == MODE_INT
2047 && GET_MODE_SIZE (GET_MODE (l
->elt
->val_rtx
))
2048 > GET_MODE_SIZE (mode
)
2050 || GET_MODE_SIZE (GET_MODE (l
->elt
->val_rtx
))
2051 < GET_MODE_SIZE (GET_MODE (lwider
->elt
->val_rtx
))))
2053 struct elt_loc_list
*el
;
2054 if (i
< FIRST_PSEUDO_REGISTER
2055 && hard_regno_nregs
[i
][GET_MODE (l
->elt
->val_rtx
)] != 1)
2057 for (el
= l
->elt
->locs
; el
; el
= el
->next
)
2058 if (!REG_P (el
->loc
))
2065 rtx sub
= lowpart_subreg (mode
, lwider
->elt
->val_rtx
,
2066 GET_MODE (lwider
->elt
->val_rtx
));
2068 new_elt_loc_list (e
, sub
);
2071 REG_VALUES (i
)->next
= new_elt_list (REG_VALUES (i
)->next
, e
);
2072 slot
= cselib_find_slot (mode
, x
, e
->hash
, INSERT
, memmode
);
2078 return cselib_lookup_mem (x
, create
);
2080 hashval
= cselib_hash_rtx (x
, create
, memmode
);
2081 /* Can't even create if hashing is not possible. */
2085 slot
= cselib_find_slot (mode
, x
, hashval
,
2086 create
? INSERT
: NO_INSERT
, memmode
);
2090 e
= (cselib_val
*) *slot
;
2094 e
= new_cselib_val (hashval
, mode
, x
);
2096 /* We have to fill the slot before calling cselib_subst_to_values:
2097 the hash table is inconsistent until we do so, and
2098 cselib_subst_to_values will need to do lookups. */
2100 new_elt_loc_list (e
, cselib_subst_to_values (x
, memmode
));
2104 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2107 cselib_lookup_from_insn (rtx x
, machine_mode mode
,
2108 int create
, machine_mode memmode
, rtx_insn
*insn
)
2112 gcc_assert (!cselib_current_insn
);
2113 cselib_current_insn
= insn
;
2115 ret
= cselib_lookup (x
, mode
, create
, memmode
);
2117 cselib_current_insn
= NULL
;
2122 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2123 maintains invariants related with debug insns. */
2126 cselib_lookup (rtx x
, machine_mode mode
,
2127 int create
, machine_mode memmode
)
2129 cselib_val
*ret
= cselib_lookup_1 (x
, mode
, create
, memmode
);
2131 /* ??? Should we return NULL if we're not to create an entry, the
2132 found loc is a debug loc and cselib_current_insn is not DEBUG?
2133 If so, we should also avoid converting val to non-DEBUG; probably
2134 easiest setting cselib_current_insn to NULL before the call
2137 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
2139 fputs ("cselib lookup ", dump_file
);
2140 print_inline_rtx (dump_file
, x
, 2);
2141 fprintf (dump_file
, " => %u:%u\n",
2143 ret
? ret
->hash
: 0);
2149 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2150 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2151 is used to determine how many hard registers are being changed. If MODE
2152 is VOIDmode, then only REGNO is being changed; this is used when
2153 invalidating call clobbered registers across a call. */
2156 cselib_invalidate_regno (unsigned int regno
, machine_mode mode
)
2158 unsigned int endregno
;
2161 /* If we see pseudos after reload, something is _wrong_. */
2162 gcc_assert (!reload_completed
|| regno
< FIRST_PSEUDO_REGISTER
2163 || reg_renumber
[regno
] < 0);
2165 /* Determine the range of registers that must be invalidated. For
2166 pseudos, only REGNO is affected. For hard regs, we must take MODE
2167 into account, and we must also invalidate lower register numbers
2168 if they contain values that overlap REGNO. */
2169 if (regno
< FIRST_PSEUDO_REGISTER
)
2171 gcc_assert (mode
!= VOIDmode
);
2173 if (regno
< max_value_regs
)
2176 i
= regno
- max_value_regs
;
2178 endregno
= end_hard_regno (mode
, regno
);
2183 endregno
= regno
+ 1;
2186 for (; i
< endregno
; i
++)
2188 struct elt_list
**l
= ®_VALUES (i
);
2190 /* Go through all known values for this reg; if it overlaps the range
2191 we're invalidating, remove the value. */
2194 cselib_val
*v
= (*l
)->elt
;
2196 rtx_insn
*setting_insn
;
2197 struct elt_loc_list
**p
;
2198 unsigned int this_last
= i
;
2200 if (i
< FIRST_PSEUDO_REGISTER
&& v
!= NULL
)
2201 this_last
= end_hard_regno (GET_MODE (v
->val_rtx
), i
) - 1;
2203 if (this_last
< regno
|| v
== NULL
2204 || (v
== cfa_base_preserved_val
2205 && i
== cfa_base_preserved_regno
))
2211 /* We have an overlap. */
2212 if (*l
== REG_VALUES (i
))
2214 /* Maintain the invariant that the first entry of
2215 REG_VALUES, if present, must be the value used to set
2216 the register, or NULL. This is also nice because
2217 then we won't push the same regno onto user_regs
2223 unchain_one_elt_list (l
);
2225 v
= canonical_cselib_val (v
);
2227 had_locs
= v
->locs
!= NULL
;
2228 setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
2230 /* Now, we clear the mapping from value to reg. It must exist, so
2231 this code will crash intentionally if it doesn't. */
2232 for (p
= &v
->locs
; ; p
= &(*p
)->next
)
2236 if (REG_P (x
) && REGNO (x
) == i
)
2238 unchain_one_elt_loc_list (p
);
2243 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
2245 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
2246 n_useless_debug_values
++;
2254 /* Invalidate any locations in the table which are changed because of a
2255 store to MEM_RTX. If this is called because of a non-const call
2256 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2259 cselib_invalidate_mem (rtx mem_rtx
)
2261 cselib_val
**vp
, *v
, *next
;
2265 mem_addr
= canon_rtx (get_addr (XEXP (mem_rtx
, 0)));
2266 mem_rtx
= canon_rtx (mem_rtx
);
2268 vp
= &first_containing_mem
;
2269 for (v
= *vp
; v
!= &dummy_val
; v
= next
)
2271 bool has_mem
= false;
2272 struct elt_loc_list
**p
= &v
->locs
;
2273 bool had_locs
= v
->locs
!= NULL
;
2274 rtx_insn
*setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
2280 struct elt_list
**mem_chain
;
2282 /* MEMs may occur in locations only at the top level; below
2283 that every MEM or REG is substituted by its VALUE. */
2289 if (num_mems
< PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS
)
2290 && ! canon_anti_dependence (x
, false, mem_rtx
,
2291 GET_MODE (mem_rtx
), mem_addr
))
2299 /* This one overlaps. */
2300 /* We must have a mapping from this MEM's address to the
2301 value (E). Remove that, too. */
2302 addr
= cselib_lookup (XEXP (x
, 0), VOIDmode
, 0, GET_MODE (x
));
2303 addr
= canonical_cselib_val (addr
);
2304 gcc_checking_assert (v
== canonical_cselib_val (v
));
2305 mem_chain
= &addr
->addr_list
;
2308 cselib_val
*canon
= canonical_cselib_val ((*mem_chain
)->elt
);
2312 unchain_one_elt_list (mem_chain
);
2316 /* Record canonicalized elt. */
2317 (*mem_chain
)->elt
= canon
;
2319 mem_chain
= &(*mem_chain
)->next
;
2322 unchain_one_elt_loc_list (p
);
2325 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
2327 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
2328 n_useless_debug_values
++;
2333 next
= v
->next_containing_mem
;
2337 vp
= &(*vp
)->next_containing_mem
;
2340 v
->next_containing_mem
= NULL
;
2345 /* Invalidate DEST, which is being assigned to or clobbered. */
2348 cselib_invalidate_rtx (rtx dest
)
2350 while (GET_CODE (dest
) == SUBREG
2351 || GET_CODE (dest
) == ZERO_EXTRACT
2352 || GET_CODE (dest
) == STRICT_LOW_PART
)
2353 dest
= XEXP (dest
, 0);
2356 cselib_invalidate_regno (REGNO (dest
), GET_MODE (dest
));
2357 else if (MEM_P (dest
))
2358 cselib_invalidate_mem (dest
);
2361 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2364 cselib_invalidate_rtx_note_stores (rtx dest
, const_rtx ignore ATTRIBUTE_UNUSED
,
2365 void *data ATTRIBUTE_UNUSED
)
2367 cselib_invalidate_rtx (dest
);
2370 /* Record the result of a SET instruction. DEST is being set; the source
2371 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2372 describes its address. */
2375 cselib_record_set (rtx dest
, cselib_val
*src_elt
, cselib_val
*dest_addr_elt
)
2377 if (src_elt
== 0 || side_effects_p (dest
))
2382 unsigned int dreg
= REGNO (dest
);
2383 if (dreg
< FIRST_PSEUDO_REGISTER
)
2385 unsigned int n
= REG_NREGS (dest
);
2387 if (n
> max_value_regs
)
2391 if (REG_VALUES (dreg
) == 0)
2393 used_regs
[n_used_regs
++] = dreg
;
2394 REG_VALUES (dreg
) = new_elt_list (REG_VALUES (dreg
), src_elt
);
2398 /* The register should have been invalidated. */
2399 gcc_assert (REG_VALUES (dreg
)->elt
== 0);
2400 REG_VALUES (dreg
)->elt
= src_elt
;
2403 if (src_elt
->locs
== 0 && !PRESERVED_VALUE_P (src_elt
->val_rtx
))
2405 new_elt_loc_list (src_elt
, dest
);
2407 else if (MEM_P (dest
) && dest_addr_elt
!= 0
2408 && cselib_record_memory
)
2410 if (src_elt
->locs
== 0 && !PRESERVED_VALUE_P (src_elt
->val_rtx
))
2412 add_mem_for_addr (dest_addr_elt
, src_elt
, dest
);
2416 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2419 cselib_add_permanent_equiv (cselib_val
*elt
, rtx x
, rtx_insn
*insn
)
2422 rtx_insn
*save_cselib_current_insn
= cselib_current_insn
;
2424 gcc_checking_assert (elt
);
2425 gcc_checking_assert (PRESERVED_VALUE_P (elt
->val_rtx
));
2426 gcc_checking_assert (!side_effects_p (x
));
2428 cselib_current_insn
= insn
;
2430 nelt
= cselib_lookup (x
, GET_MODE (elt
->val_rtx
), 1, VOIDmode
);
2434 cselib_any_perm_equivs
= true;
2436 if (!PRESERVED_VALUE_P (nelt
->val_rtx
))
2437 cselib_preserve_value (nelt
);
2439 new_elt_loc_list (nelt
, elt
->val_rtx
);
2442 cselib_current_insn
= save_cselib_current_insn
;
2445 /* Return TRUE if any permanent equivalences have been recorded since
2446 the table was last initialized. */
2448 cselib_have_permanent_equivalences (void)
2450 return cselib_any_perm_equivs
;
2453 /* There is no good way to determine how many elements there can be
2454 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2455 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2457 struct cselib_record_autoinc_data
2459 struct cselib_set
*sets
;
2463 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2464 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2467 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED
, rtx op ATTRIBUTE_UNUSED
,
2468 rtx dest
, rtx src
, rtx srcoff
, void *arg
)
2470 struct cselib_record_autoinc_data
*data
;
2471 data
= (struct cselib_record_autoinc_data
*)arg
;
2473 data
->sets
[data
->n_sets
].dest
= dest
;
2476 data
->sets
[data
->n_sets
].src
= gen_rtx_PLUS (GET_MODE (src
), src
, srcoff
);
2478 data
->sets
[data
->n_sets
].src
= src
;
2485 /* Record the effects of any sets and autoincs in INSN. */
2487 cselib_record_sets (rtx_insn
*insn
)
2491 struct cselib_set sets
[MAX_SETS
];
2492 rtx body
= PATTERN (insn
);
2494 int n_sets_before_autoinc
;
2495 struct cselib_record_autoinc_data data
;
2497 body
= PATTERN (insn
);
2498 if (GET_CODE (body
) == COND_EXEC
)
2500 cond
= COND_EXEC_TEST (body
);
2501 body
= COND_EXEC_CODE (body
);
2504 /* Find all sets. */
2505 if (GET_CODE (body
) == SET
)
2507 sets
[0].src
= SET_SRC (body
);
2508 sets
[0].dest
= SET_DEST (body
);
2511 else if (GET_CODE (body
) == PARALLEL
)
2513 /* Look through the PARALLEL and record the values being
2514 set, if possible. Also handle any CLOBBERs. */
2515 for (i
= XVECLEN (body
, 0) - 1; i
>= 0; --i
)
2517 rtx x
= XVECEXP (body
, 0, i
);
2519 if (GET_CODE (x
) == SET
)
2521 sets
[n_sets
].src
= SET_SRC (x
);
2522 sets
[n_sets
].dest
= SET_DEST (x
);
2529 && MEM_P (sets
[0].src
)
2530 && !cselib_record_memory
2531 && MEM_READONLY_P (sets
[0].src
))
2533 rtx note
= find_reg_equal_equiv_note (insn
);
2535 if (note
&& CONSTANT_P (XEXP (note
, 0)))
2536 sets
[0].src
= XEXP (note
, 0);
2540 data
.n_sets
= n_sets_before_autoinc
= n_sets
;
2541 for_each_inc_dec (PATTERN (insn
), cselib_record_autoinc_cb
, &data
);
2542 n_sets
= data
.n_sets
;
2544 /* Look up the values that are read. Do this before invalidating the
2545 locations that are written. */
2546 for (i
= 0; i
< n_sets
; i
++)
2548 rtx dest
= sets
[i
].dest
;
2550 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2551 the low part after invalidating any knowledge about larger modes. */
2552 if (GET_CODE (sets
[i
].dest
) == STRICT_LOW_PART
)
2553 sets
[i
].dest
= dest
= XEXP (dest
, 0);
2555 /* We don't know how to record anything but REG or MEM. */
2557 || (MEM_P (dest
) && cselib_record_memory
))
2559 rtx src
= sets
[i
].src
;
2561 src
= gen_rtx_IF_THEN_ELSE (GET_MODE (dest
), cond
, src
, dest
);
2562 sets
[i
].src_elt
= cselib_lookup (src
, GET_MODE (dest
), 1, VOIDmode
);
2565 machine_mode address_mode
= get_address_mode (dest
);
2567 sets
[i
].dest_addr_elt
= cselib_lookup (XEXP (dest
, 0),
2572 sets
[i
].dest_addr_elt
= 0;
2576 if (cselib_record_sets_hook
)
2577 cselib_record_sets_hook (insn
, sets
, n_sets
);
2579 /* Invalidate all locations written by this insn. Note that the elts we
2580 looked up in the previous loop aren't affected, just some of their
2581 locations may go away. */
2582 note_stores (body
, cselib_invalidate_rtx_note_stores
, NULL
);
2584 for (i
= n_sets_before_autoinc
; i
< n_sets
; i
++)
2585 cselib_invalidate_rtx (sets
[i
].dest
);
2587 /* If this is an asm, look for duplicate sets. This can happen when the
2588 user uses the same value as an output multiple times. This is valid
2589 if the outputs are not actually used thereafter. Treat this case as
2590 if the value isn't actually set. We do this by smashing the destination
2591 to pc_rtx, so that we won't record the value later. */
2592 if (n_sets
>= 2 && asm_noperands (body
) >= 0)
2594 for (i
= 0; i
< n_sets
; i
++)
2596 rtx dest
= sets
[i
].dest
;
2597 if (REG_P (dest
) || MEM_P (dest
))
2600 for (j
= i
+ 1; j
< n_sets
; j
++)
2601 if (rtx_equal_p (dest
, sets
[j
].dest
))
2603 sets
[i
].dest
= pc_rtx
;
2604 sets
[j
].dest
= pc_rtx
;
2610 /* Now enter the equivalences in our tables. */
2611 for (i
= 0; i
< n_sets
; i
++)
2613 rtx dest
= sets
[i
].dest
;
2615 || (MEM_P (dest
) && cselib_record_memory
))
2616 cselib_record_set (dest
, sets
[i
].src_elt
, sets
[i
].dest_addr_elt
);
2620 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
2623 fp_setter_insn (rtx_insn
*insn
)
2625 rtx expr
, pat
= NULL_RTX
;
2627 if (!RTX_FRAME_RELATED_P (insn
))
2630 expr
= find_reg_note (insn
, REG_FRAME_RELATED_EXPR
, NULL_RTX
);
2632 pat
= XEXP (expr
, 0);
2633 if (!modified_in_p (hard_frame_pointer_rtx
, pat
? pat
: insn
))
2636 /* Don't return true for frame pointer restores in the epilogue. */
2637 if (find_reg_note (insn
, REG_CFA_RESTORE
, hard_frame_pointer_rtx
))
2642 /* Record the effects of INSN. */
2645 cselib_process_insn (rtx_insn
*insn
)
2650 cselib_current_insn
= insn
;
2652 /* Forget everything at a CODE_LABEL or a setjmp. */
2655 && find_reg_note (insn
, REG_SETJMP
, NULL
)))
2656 && !cselib_preserve_constants
)
2658 cselib_reset_table (next_uid
);
2659 cselib_current_insn
= NULL
;
2663 if (! INSN_P (insn
))
2665 cselib_current_insn
= NULL
;
2669 /* If this is a call instruction, forget anything stored in a
2670 call clobbered register, or, if this is not a const call, in
2674 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
2675 if (call_used_regs
[i
]
2676 || (REG_VALUES (i
) && REG_VALUES (i
)->elt
2677 && HARD_REGNO_CALL_PART_CLOBBERED (i
,
2678 GET_MODE (REG_VALUES (i
)->elt
->val_rtx
))))
2679 cselib_invalidate_regno (i
, reg_raw_mode
[i
]);
2681 /* Since it is not clear how cselib is going to be used, be
2682 conservative here and treat looping pure or const functions
2683 as if they were regular functions. */
2684 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn
)
2685 || !(RTL_CONST_OR_PURE_CALL_P (insn
)))
2686 cselib_invalidate_mem (callmem
);
2689 cselib_record_sets (insn
);
2691 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2692 after we have processed the insn. */
2695 for (x
= CALL_INSN_FUNCTION_USAGE (insn
); x
; x
= XEXP (x
, 1))
2696 if (GET_CODE (XEXP (x
, 0)) == CLOBBER
)
2697 cselib_invalidate_rtx (XEXP (XEXP (x
, 0), 0));
2698 /* Flush evertything on setjmp. */
2699 if (cselib_preserve_constants
2700 && find_reg_note (insn
, REG_SETJMP
, NULL
))
2702 cselib_preserve_only_values ();
2703 cselib_reset_table (next_uid
);
2707 /* On setter of the hard frame pointer if frame_pointer_needed,
2708 invalidate stack_pointer_rtx, so that sp and {,h}fp based
2709 VALUEs are distinct. */
2710 if (reload_completed
2711 && frame_pointer_needed
2712 && fp_setter_insn (insn
))
2713 cselib_invalidate_rtx (stack_pointer_rtx
);
2715 cselib_current_insn
= NULL
;
2717 if (n_useless_values
> MAX_USELESS_VALUES
2718 /* remove_useless_values is linear in the hash table size. Avoid
2719 quadratic behavior for very large hashtables with very few
2720 useless elements. */
2721 && ((unsigned int)n_useless_values
2722 > (cselib_hash_table
->elements () - n_debug_values
) / 4))
2723 remove_useless_values ();
2726 /* Initialize cselib for one pass. The caller must also call
2727 init_alias_analysis. */
2730 cselib_init (int record_what
)
2732 elt_list_pool
= create_alloc_pool ("elt_list",
2733 sizeof (struct elt_list
), 10);
2734 elt_loc_list_pool
= create_alloc_pool ("elt_loc_list",
2735 sizeof (struct elt_loc_list
), 10);
2736 cselib_val_pool
= create_alloc_pool ("cselib_val_list",
2737 sizeof (cselib_val
), 10);
2738 value_pool
= create_alloc_pool ("value", RTX_CODE_SIZE (VALUE
), 100);
2739 cselib_record_memory
= record_what
& CSELIB_RECORD_MEMORY
;
2740 cselib_preserve_constants
= record_what
& CSELIB_PRESERVE_CONSTANTS
;
2741 cselib_any_perm_equivs
= false;
2743 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2744 see canon_true_dependence. This is only created once. */
2746 callmem
= gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
));
2748 cselib_nregs
= max_reg_num ();
2750 /* We preserve reg_values to allow expensive clearing of the whole thing.
2751 Reallocate it however if it happens to be too large. */
2752 if (!reg_values
|| reg_values_size
< cselib_nregs
2753 || (reg_values_size
> 10 && reg_values_size
> cselib_nregs
* 4))
2756 /* Some space for newly emit instructions so we don't end up
2757 reallocating in between passes. */
2758 reg_values_size
= cselib_nregs
+ (63 + cselib_nregs
) / 16;
2759 reg_values
= XCNEWVEC (struct elt_list
*, reg_values_size
);
2761 used_regs
= XNEWVEC (unsigned int, cselib_nregs
);
2763 cselib_hash_table
= new hash_table
<cselib_hasher
> (31);
2764 if (cselib_preserve_constants
)
2765 cselib_preserved_hash_table
= new hash_table
<cselib_hasher
> (31);
2769 /* Called when the current user is done with cselib. */
2772 cselib_finish (void)
2774 bool preserved
= cselib_preserve_constants
;
2775 cselib_discard_hook
= NULL
;
2776 cselib_preserve_constants
= false;
2777 cselib_any_perm_equivs
= false;
2778 cfa_base_preserved_val
= NULL
;
2779 cfa_base_preserved_regno
= INVALID_REGNUM
;
2780 free_alloc_pool (elt_list_pool
);
2781 free_alloc_pool (elt_loc_list_pool
);
2782 free_alloc_pool (cselib_val_pool
);
2783 free_alloc_pool (value_pool
);
2784 cselib_clear_table ();
2785 delete cselib_hash_table
;
2786 cselib_hash_table
= NULL
;
2788 delete cselib_preserved_hash_table
;
2789 cselib_preserved_hash_table
= NULL
;
2792 n_useless_values
= 0;
2793 n_useless_debug_values
= 0;
2798 /* Dump the cselib_val *X to FILE *OUT. */
2801 dump_cselib_val (cselib_val
**x
, FILE *out
)
2804 bool need_lf
= true;
2806 print_inline_rtx (out
, v
->val_rtx
, 0);
2810 struct elt_loc_list
*l
= v
->locs
;
2816 fputs (" locs:", out
);
2819 if (l
->setting_insn
)
2820 fprintf (out
, "\n from insn %i ",
2821 INSN_UID (l
->setting_insn
));
2823 fprintf (out
, "\n ");
2824 print_inline_rtx (out
, l
->loc
, 4);
2826 while ((l
= l
->next
));
2831 fputs (" no locs", out
);
2837 struct elt_list
*e
= v
->addr_list
;
2843 fputs (" addr list:", out
);
2847 print_inline_rtx (out
, e
->elt
->val_rtx
, 2);
2849 while ((e
= e
->next
));
2854 fputs (" no addrs", out
);
2858 if (v
->next_containing_mem
== &dummy_val
)
2859 fputs (" last mem\n", out
);
2860 else if (v
->next_containing_mem
)
2862 fputs (" next mem ", out
);
2863 print_inline_rtx (out
, v
->next_containing_mem
->val_rtx
, 2);
2872 /* Dump to OUT everything in the CSELIB table. */
2875 dump_cselib_table (FILE *out
)
2877 fprintf (out
, "cselib hash table:\n");
2878 cselib_hash_table
->traverse
<FILE *, dump_cselib_val
> (out
);
2879 fprintf (out
, "cselib preserved hash table:\n");
2880 cselib_preserved_hash_table
->traverse
<FILE *, dump_cselib_val
> (out
);
2881 if (first_containing_mem
!= &dummy_val
)
2883 fputs ("first mem ", out
);
2884 print_inline_rtx (out
, first_containing_mem
->val_rtx
, 2);
2887 fprintf (out
, "next uid %i\n", next_uid
);
2890 #include "gt-cselib.h"