1 /* Common subexpression elimination library for GNU compiler.
2 Copyright (C) 1987-2025 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"
34 #include "function-abi.h"
38 /* A list of cselib_val structures. */
41 struct elt_list
*next
;
45 static bool cselib_record_memory
;
46 static bool cselib_preserve_constants
;
47 static bool cselib_any_perm_equivs
;
48 static inline void promote_debug_loc (struct elt_loc_list
*l
);
49 static struct elt_list
*new_elt_list (struct elt_list
*, cselib_val
*);
50 static void new_elt_loc_list (cselib_val
*, rtx
);
51 static void unchain_one_value (cselib_val
*);
52 static void unchain_one_elt_list (struct elt_list
**);
53 static void unchain_one_elt_loc_list (struct elt_loc_list
**);
54 static void remove_useless_values (void);
55 static hashval_t
cselib_hash_rtx (rtx
, int, machine_mode
);
56 static cselib_val
*new_cselib_val (unsigned int, machine_mode
, rtx
);
57 static void add_mem_for_addr (cselib_val
*, cselib_val
*, rtx
);
58 static cselib_val
*cselib_lookup_mem (rtx
, int);
59 static void cselib_invalidate_regno (unsigned int, machine_mode
);
60 static void cselib_invalidate_mem (rtx
);
61 static void cselib_record_set (rtx
, cselib_val
*, cselib_val
*);
62 static void cselib_record_sets (rtx_insn
*);
63 static rtx
autoinc_split (rtx
, rtx
*, machine_mode
);
65 #define PRESERVED_VALUE_P(RTX) \
66 (RTL_FLAG_CHECK1 ("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
68 #define SP_BASED_VALUE_P(RTX) \
69 (RTL_FLAG_CHECK1 ("SP_BASED_VALUE_P", (RTX), VALUE)->jump)
71 #define SP_DERIVED_VALUE_P(RTX) \
72 (RTL_FLAG_CHECK1 ("SP_DERIVED_VALUE_P", (RTX), VALUE)->call)
74 struct expand_value_data
77 cselib_expand_callback callback
;
82 static rtx
cselib_expand_value_rtx_1 (rtx
, struct expand_value_data
*, int);
84 /* This is a global so we don't have to pass this through every function.
85 It is used in new_elt_loc_list to set SETTING_INSN. */
86 static rtx_insn
*cselib_current_insn
;
88 /* There are three ways in which cselib can look up an rtx:
89 - for a REG, the reg_values table (which is indexed by regno) is used
90 - for a MEM, we recursively look up its address and then follow the
91 addr_list of that value
92 - for everything else, we compute a hash value and go through the hash
93 table. Since different rtx's can still have the same hash value,
94 this involves walking the table entries for a given value and comparing
95 the locations of the entries with the rtx we are looking up. */
97 struct cselib_hasher
: nofree_ptr_hash
<cselib_val
>
100 /* The rtx value and its mode (needed separately for constant
104 /* The mode of the contaning MEM, if any, otherwise VOIDmode. */
105 machine_mode memmode
;
107 typedef key
*compare_type
;
108 static inline hashval_t
hash (const cselib_val
*);
109 static inline bool equal (const cselib_val
*, const key
*);
112 /* The hash function for our hash table. The value is always computed with
113 cselib_hash_rtx when adding an element; this function just extracts the
114 hash value from a cselib_val structure. */
117 cselib_hasher::hash (const cselib_val
*v
)
122 /* The equality test for our hash table. The first argument V is a table
123 element (i.e. a cselib_val), while the second arg X is an rtx. We know
124 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
125 CONST of an appropriate mode. */
128 cselib_hasher::equal (const cselib_val
*v
, const key
*x_arg
)
130 struct elt_loc_list
*l
;
132 machine_mode mode
= x_arg
->mode
;
133 machine_mode memmode
= x_arg
->memmode
;
135 if (mode
!= GET_MODE (v
->val_rtx
))
138 if (GET_CODE (x
) == VALUE
)
139 return x
== v
->val_rtx
;
141 if (SP_DERIVED_VALUE_P (v
->val_rtx
) && GET_MODE (x
) == Pmode
)
144 if (autoinc_split (x
, &xoff
, memmode
) == v
->val_rtx
&& xoff
== NULL_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 (l
->setting_insn
&& DEBUG_INSN_P (l
->setting_insn
)
152 && (!cselib_current_insn
|| !DEBUG_INSN_P (cselib_current_insn
)))
154 rtx_insn
*save_cselib_current_insn
= cselib_current_insn
;
155 /* If l is so far a debug only loc, without debug stmts it
156 would never be compared to x at all, so temporarily pretend
157 current instruction is that DEBUG_INSN so that we don't
158 promote other debug locs even for unsuccessful comparison. */
159 cselib_current_insn
= l
->setting_insn
;
160 bool match
= rtx_equal_for_cselib_1 (l
->loc
, x
, memmode
, 0);
161 cselib_current_insn
= save_cselib_current_insn
;
164 promote_debug_loc (l
);
168 else if (rtx_equal_for_cselib_1 (l
->loc
, x
, memmode
, 0))
174 /* A table that enables us to look up elts by their value. */
175 static hash_table
<cselib_hasher
> *cselib_hash_table
;
177 /* A table to hold preserved values. */
178 static hash_table
<cselib_hasher
> *cselib_preserved_hash_table
;
180 /* The unique id that the next create value will take. */
181 static unsigned int next_uid
;
183 /* The number of registers we had when the varrays were last resized. */
184 static unsigned int cselib_nregs
;
186 /* Count values without known locations, or with only locations that
187 wouldn't have been known except for debug insns. Whenever this
188 grows too big, we remove these useless values from the table.
190 Counting values with only debug values is a bit tricky. We don't
191 want to increment n_useless_values when we create a value for a
192 debug insn, for this would get n_useless_values out of sync, but we
193 want increment it if all locs in the list that were ever referenced
194 in nondebug insns are removed from the list.
196 In the general case, once we do that, we'd have to stop accepting
197 nondebug expressions in the loc list, to avoid having two values
198 equivalent that, without debug insns, would have been made into
199 separate values. However, because debug insns never introduce
200 equivalences themselves (no assignments), the only means for
201 growing loc lists is through nondebug assignments. If the locs
202 also happen to be referenced in debug insns, it will work just fine.
204 A consequence of this is that there's at most one debug-only loc in
205 each loc list. If we keep it in the first entry, testing whether
206 we have a debug-only loc list takes O(1).
208 Furthermore, since any additional entry in a loc list containing a
209 debug loc would have to come from an assignment (nondebug) that
210 references both the initial debug loc and the newly-equivalent loc,
211 the initial debug loc would be promoted to a nondebug loc, and the
212 loc list would not contain debug locs any more.
214 So the only case we have to be careful with in order to keep
215 n_useless_values in sync between debug and nondebug compilations is
216 to avoid incrementing n_useless_values when removing the single loc
217 from a value that turns out to not appear outside debug values. We
218 increment n_useless_debug_values instead, and leave such values
219 alone until, for other reasons, we garbage-collect useless
221 static int n_useless_values
;
222 static int n_useless_debug_values
;
224 /* Count values whose locs have been taken exclusively from debug
225 insns for the entire life of the value. */
226 static int n_debug_values
;
228 /* Number of useless values before we remove them from the hash table. */
229 #define MAX_USELESS_VALUES 32
231 /* This table maps from register number to values. It does not
232 contain pointers to cselib_val structures, but rather elt_lists.
233 The purpose is to be able to refer to the same register in
234 different modes. The first element of the list defines the mode in
235 which the register was set; if the mode is unknown or the value is
236 no longer valid in that mode, ELT will be NULL for the first
238 static struct elt_list
**reg_values
;
239 static unsigned int reg_values_size
;
240 #define REG_VALUES(i) reg_values[i]
242 /* The largest number of hard regs used by any entry added to the
243 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
244 static unsigned int max_value_regs
;
246 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
247 in cselib_clear_table() for fast emptying. */
248 static unsigned int *used_regs
;
249 static unsigned int n_used_regs
;
251 /* We pass this to cselib_invalidate_mem to invalidate all of
252 memory for a non-const call instruction and memory below stack pointer
253 for const/pure calls. */
254 static GTY(()) rtx callmem
[2];
256 /* Set by discard_useless_locs if it deleted the last location of any
258 static int values_became_useless
;
260 /* Used as stop element of the containing_mem list so we can check
261 presence in the list by checking the next pointer. */
262 static cselib_val dummy_val
;
264 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
265 that is constant through the whole function and should never be
267 static cselib_val
*cfa_base_preserved_val
;
268 static unsigned int cfa_base_preserved_regno
= INVALID_REGNUM
;
270 /* Used to list all values that contain memory reference.
271 May or may not contain the useless values - the list is compacted
272 each time memory is invalidated. */
273 static cselib_val
*first_containing_mem
= &dummy_val
;
275 static object_allocator
<elt_list
> elt_list_pool ("elt_list");
276 static object_allocator
<elt_loc_list
> elt_loc_list_pool ("elt_loc_list");
277 static object_allocator
<cselib_val
> cselib_val_pool ("cselib_val_list");
279 static pool_allocator
value_pool ("value", RTX_CODE_SIZE (VALUE
));
281 /* If nonnull, cselib will call this function before freeing useless
282 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
283 void (*cselib_discard_hook
) (cselib_val
*);
285 /* If nonnull, cselib will call this function before recording sets or
286 even clobbering outputs of INSN. All the recorded sets will be
287 represented in the array sets[n_sets]. new_val_min can be used to
288 tell whether values present in sets are introduced by this
290 void (*cselib_record_sets_hook
) (rtx_insn
*insn
, struct cselib_set
*sets
,
295 /* Allocate a struct elt_list and fill in its two elements with the
298 static inline struct elt_list
*
299 new_elt_list (struct elt_list
*next
, cselib_val
*elt
)
301 elt_list
*el
= elt_list_pool
.allocate ();
307 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
311 new_elt_loc_list (cselib_val
*val
, rtx loc
)
313 struct elt_loc_list
*el
, *next
= val
->locs
;
315 gcc_checking_assert (!next
|| !next
->setting_insn
316 || !DEBUG_INSN_P (next
->setting_insn
)
317 || cselib_current_insn
== next
->setting_insn
);
319 /* If we're creating the first loc in a debug insn context, we've
320 just created a debug value. Count it. */
321 if (!next
&& cselib_current_insn
&& DEBUG_INSN_P (cselib_current_insn
))
324 val
= canonical_cselib_val (val
);
327 if (GET_CODE (loc
) == VALUE
)
329 loc
= canonical_cselib_val (CSELIB_VAL_PTR (loc
))->val_rtx
;
331 gcc_checking_assert (PRESERVED_VALUE_P (loc
)
332 == PRESERVED_VALUE_P (val
->val_rtx
));
334 if (val
->val_rtx
== loc
)
336 else if (val
->uid
> CSELIB_VAL_PTR (loc
)->uid
)
338 /* Reverse the insertion. */
339 new_elt_loc_list (CSELIB_VAL_PTR (loc
), val
->val_rtx
);
343 gcc_checking_assert (val
->uid
< CSELIB_VAL_PTR (loc
)->uid
);
345 if (CSELIB_VAL_PTR (loc
)->locs
)
347 /* Bring all locs from LOC to VAL. */
348 for (el
= CSELIB_VAL_PTR (loc
)->locs
; el
->next
; el
= el
->next
)
350 /* Adjust values that have LOC as canonical so that VAL
351 becomes their canonical. */
352 if (el
->loc
&& GET_CODE (el
->loc
) == VALUE
)
354 gcc_checking_assert (CSELIB_VAL_PTR (el
->loc
)->locs
->loc
356 CSELIB_VAL_PTR (el
->loc
)->locs
->loc
= val
->val_rtx
;
359 el
->next
= val
->locs
;
360 next
= val
->locs
= CSELIB_VAL_PTR (loc
)->locs
;
363 if (CSELIB_VAL_PTR (loc
)->addr_list
)
365 /* Bring in addr_list into canonical node. */
366 struct elt_list
*last
= CSELIB_VAL_PTR (loc
)->addr_list
;
369 last
->next
= val
->addr_list
;
370 val
->addr_list
= CSELIB_VAL_PTR (loc
)->addr_list
;
371 CSELIB_VAL_PTR (loc
)->addr_list
= NULL
;
374 if (CSELIB_VAL_PTR (loc
)->next_containing_mem
!= NULL
375 && val
->next_containing_mem
== NULL
)
377 /* Add VAL to the containing_mem list after LOC. LOC will
378 be removed when we notice it doesn't contain any
380 val
->next_containing_mem
= CSELIB_VAL_PTR (loc
)->next_containing_mem
;
381 CSELIB_VAL_PTR (loc
)->next_containing_mem
= val
;
384 /* Chain LOC back to VAL. */
385 el
= elt_loc_list_pool
.allocate ();
386 el
->loc
= val
->val_rtx
;
387 el
->setting_insn
= cselib_current_insn
;
389 CSELIB_VAL_PTR (loc
)->locs
= el
;
392 el
= elt_loc_list_pool
.allocate ();
394 el
->setting_insn
= cselib_current_insn
;
399 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
400 originating from a debug insn, maintaining the debug values
404 promote_debug_loc (struct elt_loc_list
*l
)
406 if (l
&& l
->setting_insn
&& DEBUG_INSN_P (l
->setting_insn
)
407 && (!cselib_current_insn
|| !DEBUG_INSN_P (cselib_current_insn
)))
410 l
->setting_insn
= cselib_current_insn
;
411 if (cselib_preserve_constants
&& l
->next
)
413 gcc_assert (l
->next
->setting_insn
414 && DEBUG_INSN_P (l
->next
->setting_insn
)
416 l
->next
->setting_insn
= cselib_current_insn
;
419 gcc_assert (!l
->next
);
423 /* The elt_list at *PL is no longer needed. Unchain it and free its
427 unchain_one_elt_list (struct elt_list
**pl
)
429 struct elt_list
*l
= *pl
;
432 elt_list_pool
.remove (l
);
435 /* Likewise for elt_loc_lists. */
438 unchain_one_elt_loc_list (struct elt_loc_list
**pl
)
440 struct elt_loc_list
*l
= *pl
;
443 elt_loc_list_pool
.remove (l
);
446 /* Likewise for cselib_vals. This also frees the addr_list associated with
450 unchain_one_value (cselib_val
*v
)
453 unchain_one_elt_list (&v
->addr_list
);
455 cselib_val_pool
.remove (v
);
458 /* Remove all entries from the hash table. Also used during
462 cselib_clear_table (void)
464 cselib_reset_table (1);
467 /* Return TRUE if V is a constant, a function invariant or a VALUE
468 equivalence; FALSE otherwise. */
471 invariant_or_equiv_p (cselib_val
*v
)
473 struct elt_loc_list
*l
;
475 if (v
== cfa_base_preserved_val
)
478 /* Keep VALUE equivalences around. */
479 for (l
= v
->locs
; l
; l
= l
->next
)
480 if (GET_CODE (l
->loc
) == VALUE
)
484 && v
->locs
->next
== NULL
)
486 if (CONSTANT_P (v
->locs
->loc
)
487 && (GET_CODE (v
->locs
->loc
) != CONST
488 || !references_value_p (v
->locs
->loc
, 0)))
490 /* Although a debug expr may be bound to different expressions,
491 we can preserve it as if it was constant, to get unification
492 and proper merging within var-tracking. */
493 if (GET_CODE (v
->locs
->loc
) == DEBUG_EXPR
494 || GET_CODE (v
->locs
->loc
) == DEBUG_IMPLICIT_PTR
495 || GET_CODE (v
->locs
->loc
) == ENTRY_VALUE
496 || GET_CODE (v
->locs
->loc
) == DEBUG_PARAMETER_REF
)
499 /* (plus (value V) (const_int C)) is invariant iff V is invariant. */
500 if (GET_CODE (v
->locs
->loc
) == PLUS
501 && CONST_INT_P (XEXP (v
->locs
->loc
, 1))
502 && GET_CODE (XEXP (v
->locs
->loc
, 0)) == VALUE
503 && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v
->locs
->loc
, 0))))
510 /* Remove from hash table all VALUEs except constants, function
511 invariants and VALUE equivalences. */
514 preserve_constants_and_equivs (cselib_val
**x
, void *info ATTRIBUTE_UNUSED
)
518 if (invariant_or_equiv_p (v
))
520 cselib_hasher::key lookup
= {
521 GET_MODE (v
->val_rtx
), v
->val_rtx
, VOIDmode
524 = cselib_preserved_hash_table
->find_slot_with_hash (&lookup
,
530 cselib_hash_table
->clear_slot (x
);
535 /* Remove all entries from the hash table, arranging for the next
536 value to be numbered NUM. */
539 cselib_reset_table (unsigned int num
)
545 if (cfa_base_preserved_val
)
547 unsigned int regno
= cfa_base_preserved_regno
;
548 unsigned int new_used_regs
= 0;
549 for (i
= 0; i
< n_used_regs
; i
++)
550 if (used_regs
[i
] == regno
)
556 REG_VALUES (used_regs
[i
]) = 0;
557 gcc_assert (new_used_regs
== 1);
558 n_used_regs
= new_used_regs
;
559 used_regs
[0] = regno
;
561 = hard_regno_nregs (regno
,
562 GET_MODE (cfa_base_preserved_val
->locs
->loc
));
564 /* If cfa_base is sp + const_int, need to preserve also the
565 SP_DERIVED_VALUE_P value. */
566 for (struct elt_loc_list
*l
= cfa_base_preserved_val
->locs
;
568 if (GET_CODE (l
->loc
) == PLUS
569 && GET_CODE (XEXP (l
->loc
, 0)) == VALUE
570 && SP_DERIVED_VALUE_P (XEXP (l
->loc
, 0))
571 && CONST_INT_P (XEXP (l
->loc
, 1)))
573 if (! invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (l
->loc
, 0))))
575 rtx val
= cfa_base_preserved_val
->val_rtx
;
576 rtx_insn
*save_cselib_current_insn
= cselib_current_insn
;
577 cselib_current_insn
= l
->setting_insn
;
578 new_elt_loc_list (CSELIB_VAL_PTR (XEXP (l
->loc
, 0)),
579 plus_constant (Pmode
, val
,
580 -UINTVAL (XEXP (l
->loc
, 1))));
581 cselib_current_insn
= save_cselib_current_insn
;
588 for (i
= 0; i
< n_used_regs
; i
++)
589 REG_VALUES (used_regs
[i
]) = 0;
593 if (cselib_preserve_constants
)
594 cselib_hash_table
->traverse
<void *, preserve_constants_and_equivs
> (NULL
);
597 cselib_hash_table
->empty ();
598 gcc_checking_assert (!cselib_any_perm_equivs
);
601 n_useless_values
= 0;
602 n_useless_debug_values
= 0;
607 first_containing_mem
= &dummy_val
;
610 /* Return the number of the next value that will be generated. */
613 cselib_get_next_uid (void)
618 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
619 INSERTing if requested. When X is part of the address of a MEM,
620 MEMMODE should specify the mode of the MEM. */
623 cselib_find_slot (machine_mode mode
, rtx x
, hashval_t hash
,
624 enum insert_option insert
, machine_mode memmode
)
626 cselib_val
**slot
= NULL
;
627 cselib_hasher::key lookup
= { mode
, x
, memmode
};
628 if (cselib_preserve_constants
)
629 slot
= cselib_preserved_hash_table
->find_slot_with_hash (&lookup
, hash
,
632 slot
= cselib_hash_table
->find_slot_with_hash (&lookup
, hash
, insert
);
636 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
637 only return true for values which point to a cselib_val whose value
638 element has been set to zero, which implies the cselib_val will be
642 references_value_p (const_rtx x
, int only_useless
)
644 const enum rtx_code code
= GET_CODE (x
);
645 const char *fmt
= GET_RTX_FORMAT (code
);
648 if (GET_CODE (x
) == VALUE
650 || (CSELIB_VAL_PTR (x
)->locs
== 0 && !PRESERVED_VALUE_P (x
))))
653 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
655 if (fmt
[i
] == 'e' && references_value_p (XEXP (x
, i
), only_useless
))
657 else if (fmt
[i
] == 'E')
658 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
659 if (references_value_p (XVECEXP (x
, i
, j
), only_useless
))
666 /* Return true if V is a useless VALUE and can be discarded as such. */
669 cselib_useless_value_p (cselib_val
*v
)
672 && !PRESERVED_VALUE_P (v
->val_rtx
)
673 && !SP_DERIVED_VALUE_P (v
->val_rtx
));
676 /* For all locations found in X, delete locations that reference useless
677 values (i.e. values without any location). Called through
681 discard_useless_locs (cselib_val
**x
, void *info ATTRIBUTE_UNUSED
)
684 struct elt_loc_list
**p
= &v
->locs
;
685 bool had_locs
= v
->locs
!= NULL
;
686 rtx_insn
*setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
690 if (references_value_p ((*p
)->loc
, 1))
691 unchain_one_elt_loc_list (p
);
696 if (had_locs
&& cselib_useless_value_p (v
))
698 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
699 n_useless_debug_values
++;
702 values_became_useless
= 1;
707 /* If X is a value with no locations, remove it from the hashtable. */
710 discard_useless_values (cselib_val
**x
, void *info ATTRIBUTE_UNUSED
)
714 if (v
->locs
== 0 && cselib_useless_value_p (v
))
716 if (cselib_discard_hook
)
717 cselib_discard_hook (v
);
719 CSELIB_VAL_PTR (v
->val_rtx
) = NULL
;
720 cselib_hash_table
->clear_slot (x
);
721 unchain_one_value (v
);
728 /* Clean out useless values (i.e. those which no longer have locations
729 associated with them) from the hash table. */
732 remove_useless_values (void)
736 /* First pass: eliminate locations that reference the value. That in
737 turn can make more values useless. */
740 values_became_useless
= 0;
741 cselib_hash_table
->traverse
<void *, discard_useless_locs
> (NULL
);
743 while (values_became_useless
);
745 /* Second pass: actually remove the values. */
747 p
= &first_containing_mem
;
748 for (v
= *p
; v
!= &dummy_val
; v
= v
->next_containing_mem
)
749 if (v
->locs
&& v
== canonical_cselib_val (v
))
752 p
= &(*p
)->next_containing_mem
;
756 if (cselib_preserve_constants
)
757 cselib_preserved_hash_table
->traverse
<void *,
758 discard_useless_locs
> (NULL
);
759 gcc_assert (!values_became_useless
);
761 n_useless_values
+= n_useless_debug_values
;
762 n_debug_values
-= n_useless_debug_values
;
763 n_useless_debug_values
= 0;
765 cselib_hash_table
->traverse
<void *, discard_useless_values
> (NULL
);
767 gcc_assert (!n_useless_values
);
770 /* Arrange for a value to not be removed from the hash table even if
771 it becomes useless. */
774 cselib_preserve_value (cselib_val
*v
)
776 PRESERVED_VALUE_P (v
->val_rtx
) = 1;
779 /* Test whether a value is preserved. */
782 cselib_preserved_value_p (cselib_val
*v
)
784 return PRESERVED_VALUE_P (v
->val_rtx
);
787 /* Arrange for a REG value to be assumed constant through the whole function,
788 never invalidated and preserved across cselib_reset_table calls. */
791 cselib_preserve_cfa_base_value (cselib_val
*v
, unsigned int regno
)
793 if (cselib_preserve_constants
795 && REG_P (v
->locs
->loc
))
797 cfa_base_preserved_val
= v
;
798 cfa_base_preserved_regno
= regno
;
802 /* Clean all non-constant expressions in the hash table, but retain
806 cselib_preserve_only_values (void)
810 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
811 cselib_invalidate_regno (i
, reg_raw_mode
[i
]);
813 cselib_invalidate_mem (callmem
[0]);
815 remove_useless_values ();
817 gcc_assert (first_containing_mem
== &dummy_val
);
820 /* Arrange for a value to be marked as based on stack pointer
821 for find_base_term purposes. */
824 cselib_set_value_sp_based (cselib_val
*v
)
826 SP_BASED_VALUE_P (v
->val_rtx
) = 1;
829 /* Test whether a value is based on stack pointer for
830 find_base_term purposes. */
833 cselib_sp_based_value_p (cselib_val
*v
)
835 return SP_BASED_VALUE_P (v
->val_rtx
);
838 /* Return the mode in which a register was last set. If X is not a
839 register, return its mode. If the mode in which the register was
840 set is not known, or the value was already clobbered, return
844 cselib_reg_set_mode (const_rtx x
)
849 if (REG_VALUES (REGNO (x
)) == NULL
850 || REG_VALUES (REGNO (x
))->elt
== NULL
)
853 return GET_MODE (REG_VALUES (REGNO (x
))->elt
->val_rtx
);
856 /* If x is a PLUS or an autoinc operation, expand the operation,
857 storing the offset, if any, in *OFF. */
860 autoinc_split (rtx x
, rtx
*off
, machine_mode memmode
)
862 switch (GET_CODE (x
))
870 if (memmode
== VOIDmode
)
873 *off
= gen_int_mode (-GET_MODE_SIZE (memmode
), GET_MODE (x
));
878 if (memmode
== VOIDmode
)
881 *off
= gen_int_mode (GET_MODE_SIZE (memmode
), GET_MODE (x
));
899 if (GET_MODE (x
) == Pmode
900 && (REG_P (x
) || MEM_P (x
) || GET_CODE (x
) == VALUE
)
901 && (*off
== NULL_RTX
|| CONST_INT_P (*off
)))
904 if (GET_CODE (x
) == VALUE
)
905 e
= CSELIB_VAL_PTR (x
);
907 e
= cselib_lookup (x
, GET_MODE (x
), 0, memmode
);
910 if (SP_DERIVED_VALUE_P (e
->val_rtx
)
911 && (*off
== NULL_RTX
|| *off
== const0_rtx
))
916 for (struct elt_loc_list
*l
= e
->locs
; l
; l
= l
->next
)
917 if (GET_CODE (l
->loc
) == PLUS
918 && GET_CODE (XEXP (l
->loc
, 0)) == VALUE
919 && SP_DERIVED_VALUE_P (XEXP (l
->loc
, 0))
920 && CONST_INT_P (XEXP (l
->loc
, 1)))
922 if (*off
== NULL_RTX
)
923 *off
= XEXP (l
->loc
, 1);
925 *off
= plus_constant (Pmode
, *off
,
926 INTVAL (XEXP (l
->loc
, 1)));
927 if (*off
== const0_rtx
)
929 return XEXP (l
->loc
, 0);
936 /* Return true if we can prove that X and Y contain the same value,
937 taking our gathered information into account. MEMMODE holds the
938 mode of the enclosing MEM, if any, as required to deal with autoinc
939 addressing modes. If X and Y are not (known to be) part of
940 addresses, MEMMODE should be VOIDmode. */
943 rtx_equal_for_cselib_1 (rtx x
, rtx y
, machine_mode memmode
, int depth
)
949 if (REG_P (x
) || MEM_P (x
))
951 cselib_val
*e
= cselib_lookup (x
, GET_MODE (x
), 0, memmode
);
957 if (REG_P (y
) || MEM_P (y
))
959 cselib_val
*e
= cselib_lookup (y
, GET_MODE (y
), 0, memmode
);
968 if (GET_CODE (x
) == VALUE
)
970 cselib_val
*e
= canonical_cselib_val (CSELIB_VAL_PTR (x
));
971 struct elt_loc_list
*l
;
973 if (GET_CODE (y
) == VALUE
)
974 return e
== canonical_cselib_val (CSELIB_VAL_PTR (y
));
976 if ((SP_DERIVED_VALUE_P (x
)
977 || SP_DERIVED_VALUE_P (e
->val_rtx
))
978 && GET_MODE (y
) == Pmode
)
981 rtx yr
= autoinc_split (y
, &yoff
, memmode
);
982 if ((yr
== x
|| yr
== e
->val_rtx
) && yoff
== NULL_RTX
)
989 for (l
= e
->locs
; l
; l
= l
->next
)
993 /* Avoid infinite recursion. We know we have the canonical
994 value, so we can just skip any values in the equivalence
996 if (REG_P (t
) || MEM_P (t
) || GET_CODE (t
) == VALUE
)
998 else if (rtx_equal_for_cselib_1 (t
, y
, memmode
, depth
+ 1))
1004 else if (GET_CODE (y
) == VALUE
)
1006 cselib_val
*e
= canonical_cselib_val (CSELIB_VAL_PTR (y
));
1007 struct elt_loc_list
*l
;
1009 if ((SP_DERIVED_VALUE_P (y
)
1010 || SP_DERIVED_VALUE_P (e
->val_rtx
))
1011 && GET_MODE (x
) == Pmode
)
1014 rtx xr
= autoinc_split (x
, &xoff
, memmode
);
1015 if ((xr
== y
|| xr
== e
->val_rtx
) && xoff
== NULL_RTX
)
1022 for (l
= e
->locs
; l
; l
= l
->next
)
1026 if (REG_P (t
) || MEM_P (t
) || GET_CODE (t
) == VALUE
)
1028 else if (rtx_equal_for_cselib_1 (x
, t
, memmode
, depth
+ 1))
1035 if (GET_MODE (x
) != GET_MODE (y
))
1038 if (GET_CODE (x
) != GET_CODE (y
)
1039 || (GET_CODE (x
) == PLUS
1040 && GET_MODE (x
) == Pmode
1041 && CONST_INT_P (XEXP (x
, 1))
1042 && CONST_INT_P (XEXP (y
, 1))))
1044 rtx xorig
= x
, yorig
= y
;
1045 rtx xoff
= NULL
, yoff
= NULL
;
1047 x
= autoinc_split (x
, &xoff
, memmode
);
1048 y
= autoinc_split (y
, &yoff
, memmode
);
1050 /* Don't recurse if nothing changed. */
1051 if (x
!= xorig
|| y
!= yorig
)
1056 if (xoff
&& !rtx_equal_for_cselib_1 (xoff
, yoff
, memmode
, depth
))
1059 return rtx_equal_for_cselib_1 (x
, y
, memmode
, depth
);
1062 if (GET_CODE (xorig
) != GET_CODE (yorig
))
1066 /* These won't be handled correctly by the code below. */
1067 switch (GET_CODE (x
))
1074 if (!same_vector_encodings_p (x
, y
))
1078 case DEBUG_IMPLICIT_PTR
:
1079 return DEBUG_IMPLICIT_PTR_DECL (x
)
1080 == DEBUG_IMPLICIT_PTR_DECL (y
);
1082 case DEBUG_PARAMETER_REF
:
1083 return DEBUG_PARAMETER_REF_DECL (x
)
1084 == DEBUG_PARAMETER_REF_DECL (y
);
1087 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
1088 use rtx_equal_for_cselib_1 to compare the operands. */
1089 return rtx_equal_p (ENTRY_VALUE_EXP (x
), ENTRY_VALUE_EXP (y
));
1092 return label_ref_label (x
) == label_ref_label (y
);
1095 return REGNO (x
) == REGNO (y
);
1098 /* We have to compare any autoinc operations in the addresses
1099 using this MEM's mode. */
1100 return rtx_equal_for_cselib_1 (XEXP (x
, 0), XEXP (y
, 0), GET_MODE (x
),
1107 code
= GET_CODE (x
);
1108 fmt
= GET_RTX_FORMAT (code
);
1110 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1117 if (XWINT (x
, i
) != XWINT (y
, i
))
1123 if (XINT (x
, i
) != XINT (y
, i
))
1128 if (XLOC (x
, i
) != XLOC (y
, i
))
1133 if (maybe_ne (SUBREG_BYTE (x
), SUBREG_BYTE (y
)))
1139 /* Two vectors must have the same length. */
1140 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
1143 /* And the corresponding elements must match. */
1144 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1145 if (! rtx_equal_for_cselib_1 (XVECEXP (x
, i
, j
),
1146 XVECEXP (y
, i
, j
), memmode
, depth
))
1152 && targetm
.commutative_p (x
, UNKNOWN
)
1153 && rtx_equal_for_cselib_1 (XEXP (x
, 1), XEXP (y
, 0), memmode
,
1155 && rtx_equal_for_cselib_1 (XEXP (x
, 0), XEXP (y
, 1), memmode
,
1158 if (! rtx_equal_for_cselib_1 (XEXP (x
, i
), XEXP (y
, i
), memmode
,
1165 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
1170 /* These are just backpointers, so they don't matter. */
1177 /* It is believed that rtx's at this level will never
1178 contain anything but integers and other rtx's,
1179 except for within LABEL_REFs and SYMBOL_REFs. */
1187 /* Wrapper for rtx_equal_for_cselib_p to determine whether a SET is
1188 truly redundant, taking into account aliasing information. */
1190 cselib_redundant_set_p (rtx set
)
1192 gcc_assert (GET_CODE (set
) == SET
);
1193 rtx dest
= SET_DEST (set
);
1194 if (cselib_reg_set_mode (dest
) != GET_MODE (dest
))
1197 if (!rtx_equal_for_cselib_p (dest
, SET_SRC (set
)))
1200 while (GET_CODE (dest
) == SUBREG
1201 || GET_CODE (dest
) == ZERO_EXTRACT
1202 || GET_CODE (dest
) == STRICT_LOW_PART
)
1203 dest
= XEXP (dest
, 0);
1205 if (!flag_strict_aliasing
|| !MEM_P (dest
))
1208 /* For a store we need to check that suppressing it will not change
1209 the effective alias set. */
1210 rtx dest_addr
= XEXP (dest
, 0);
1212 /* Lookup the equivalents to the original dest (rather than just the
1214 cselib_val
*src_val
= cselib_lookup (SET_DEST (set
),
1215 GET_MODE (SET_DEST (set
)),
1220 /* Walk the list of source equivalents to find the MEM accessing
1221 the same location. */
1222 for (elt_loc_list
*l
= src_val
->locs
; l
; l
= l
->next
)
1224 rtx src_equiv
= l
->loc
;
1225 while (GET_CODE (src_equiv
) == SUBREG
1226 || GET_CODE (src_equiv
) == ZERO_EXTRACT
1227 || GET_CODE (src_equiv
) == STRICT_LOW_PART
)
1228 src_equiv
= XEXP (src_equiv
, 0);
1230 if (MEM_P (src_equiv
))
1232 /* Match the MEMs by comparing the addresses. We can
1233 only remove the later store if the earlier aliases at
1234 least all the accesses of the later one. */
1235 if (rtx_equal_for_cselib_1 (dest_addr
, XEXP (src_equiv
, 0),
1236 GET_MODE (dest
), 0))
1237 return mems_same_for_tbaa_p (src_equiv
, dest
);
1242 /* We failed to find a recorded value in the cselib history, so try
1243 the source of this set; this catches cases such as *p = *q when p
1244 and q have the same value. */
1245 rtx src
= SET_SRC (set
);
1246 while (GET_CODE (src
) == SUBREG
)
1247 src
= XEXP (src
, 0);
1250 && rtx_equal_for_cselib_1 (dest_addr
, XEXP (src
, 0), GET_MODE (dest
), 0))
1251 return mems_same_for_tbaa_p (src
, dest
);
1256 /* Helper function for cselib_hash_rtx. Arguments like for cselib_hash_rtx,
1257 except that it hashes (plus:P x c). */
1260 cselib_hash_plus_const_int (rtx x
, HOST_WIDE_INT c
, int create
,
1261 machine_mode memmode
)
1263 cselib_val
*e
= cselib_lookup (x
, GET_MODE (x
), create
, memmode
);
1267 if (! SP_DERIVED_VALUE_P (e
->val_rtx
))
1268 for (struct elt_loc_list
*l
= e
->locs
; l
; l
= l
->next
)
1269 if (GET_CODE (l
->loc
) == PLUS
1270 && GET_CODE (XEXP (l
->loc
, 0)) == VALUE
1271 && SP_DERIVED_VALUE_P (XEXP (l
->loc
, 0))
1272 && CONST_INT_P (XEXP (l
->loc
, 1)))
1274 e
= CSELIB_VAL_PTR (XEXP (l
->loc
, 0));
1275 c
= trunc_int_for_mode (c
+ UINTVAL (XEXP (l
->loc
, 1)), Pmode
);
1282 hash
.add_int (PLUS
);
1283 hash
.add_int (GET_MODE (x
));
1284 hash
.merge_hash (e
->hash
);
1287 return hash
.end () ? hash
.end () : 1 + (unsigned int) PLUS
;
1290 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1291 For registers and memory locations, we look up their cselib_val structure
1292 and return its VALUE element.
1293 Possible reasons for return 0 are: the object is volatile, or we couldn't
1294 find a register or memory location in the table and CREATE is zero. If
1295 CREATE is nonzero, table elts are created for regs and mem.
1296 N.B. this hash function returns the same hash value for RTXes that
1297 differ only in the order of operands, thus it is suitable for comparisons
1298 that take commutativity into account.
1299 If we wanted to also support associative rules, we'd have to use a different
1300 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1301 MEMMODE indicates the mode of an enclosing MEM, and it's only
1302 used to compute autoinc values.
1303 We used to have a MODE argument for hashing for CONST_INTs, but that
1304 didn't make sense, since it caused spurious hash differences between
1305 (set (reg:SI 1) (const_int))
1306 (plus:SI (reg:SI 2) (reg:SI 1))
1308 (plus:SI (reg:SI 2) (const_int))
1309 If the mode is important in any context, it must be checked specifically
1310 in a comparison anyway, since relying on hash differences is unsafe. */
1313 cselib_hash_rtx (rtx x
, int create
, machine_mode memmode
)
1322 code
= GET_CODE (x
);
1323 hash
.add_int (code
);
1324 hash
.add_int (GET_MODE (x
));
1329 e
= CSELIB_VAL_PTR (x
);
1334 e
= cselib_lookup (x
, GET_MODE (x
), create
, memmode
);
1341 hash
.add_int (DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x
)));
1342 return hash
.end () ? hash
.end() : (unsigned int) DEBUG_EXPR
;
1344 case DEBUG_IMPLICIT_PTR
:
1345 hash
.add_int (DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x
)));
1346 return hash
.end () ? hash
.end () : (unsigned int) DEBUG_IMPLICIT_PTR
;
1348 case DEBUG_PARAMETER_REF
:
1349 hash
.add_int (DECL_UID (DEBUG_PARAMETER_REF_DECL (x
)));
1350 return hash
.end () ? hash
.end () : (unsigned int) DEBUG_PARAMETER_REF
;
1353 /* ENTRY_VALUEs are function invariant, thus try to avoid
1354 recursing on argument if ENTRY_VALUE is one of the
1355 forms emitted by expand_debug_expr, otherwise
1356 ENTRY_VALUE hash would depend on the current value
1357 in some register or memory. */
1358 if (REG_P (ENTRY_VALUE_EXP (x
)))
1359 hash
.add_int ((unsigned int) REG
1360 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x
))
1361 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x
)));
1362 else if (MEM_P (ENTRY_VALUE_EXP (x
))
1363 && REG_P (XEXP (ENTRY_VALUE_EXP (x
), 0)))
1364 hash
.add_int ((unsigned int) MEM
1365 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x
), 0))
1366 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x
), 0)));
1368 hash
.add_int (cselib_hash_rtx (ENTRY_VALUE_EXP (x
), create
, memmode
));
1369 return hash
.end () ? hash
.end () : (unsigned int) ENTRY_VALUE
;
1372 hash
.add_hwi (UINTVAL (x
));
1373 return hash
.end () ? hash
.end () : (unsigned int) CONST_INT
;
1375 case CONST_WIDE_INT
:
1376 for (i
= 0; i
< CONST_WIDE_INT_NUNITS (x
); i
++)
1377 hash
.add_hwi (CONST_WIDE_INT_ELT (x
, i
));
1378 return hash
.end () ? hash
.end () : (unsigned int) CONST_WIDE_INT
;
1380 case CONST_POLY_INT
:
1382 for (unsigned int i
= 0; i
< NUM_POLY_INT_COEFFS
; ++i
)
1383 hash
.add_wide_int (CONST_POLY_INT_COEFFS (x
)[i
]);
1384 return hash
.end () ? hash
.end () : (unsigned int) CONST_POLY_INT
;
1388 /* This is like the general case, except that it only counts
1389 the integers representing the constant. */
1390 if (TARGET_SUPPORTS_WIDE_INT
== 0 && GET_MODE (x
) == VOIDmode
)
1392 hash
.add_hwi (CONST_DOUBLE_LOW (x
));
1393 hash
.add_hwi (CONST_DOUBLE_HIGH (x
));
1396 hash
.merge_hash (real_hash (CONST_DOUBLE_REAL_VALUE (x
)));
1397 return hash
.end () ? hash
.end () : (unsigned int) CONST_DOUBLE
;
1400 hash
.merge_hash (fixed_hash (CONST_FIXED_VALUE (x
)));
1401 return hash
.end () ? hash
.end () : (unsigned int) CONST_FIXED
;
1408 units
= const_vector_encoded_nelts (x
);
1410 for (i
= 0; i
< units
; ++i
)
1412 elt
= CONST_VECTOR_ENCODED_ELT (x
, i
);
1413 hash
.merge_hash (cselib_hash_rtx (elt
, 0, memmode
));
1416 return hash
.end () ? hash
.end () : (unsigned int) CONST_VECTOR
;
1419 /* Assume there is only one rtx object for any given label. */
1421 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1422 differences and differences between each stage's debugging dumps. */
1423 hash
.add_int (CODE_LABEL_NUMBER (label_ref_label (x
)));
1424 return hash
.end () ? hash
.end () : (unsigned int) LABEL_REF
;
1428 /* Don't hash on the symbol's address to avoid bootstrap differences.
1429 Different hash values may cause expressions to be recorded in
1430 different orders and thus different registers to be used in the
1431 final assembler. This also avoids differences in the dump files
1432 between various stages. */
1433 const char *p
= (const char *) XSTR (x
, 0);
1436 hash
.add (p
, strlen (p
));
1438 return hash
.end () ? hash
.end () : (unsigned int) SYMBOL_REF
;
1444 /* We can't compute these without knowing the MEM mode. */
1445 gcc_assert (memmode
!= VOIDmode
);
1446 offset
= GET_MODE_SIZE (memmode
);
1447 if (code
== PRE_DEC
)
1449 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1450 like (mem:MEMMODE (plus (reg) (const_int I))). */
1451 if (GET_MODE (x
) == Pmode
1452 && (REG_P (XEXP (x
, 0))
1453 || MEM_P (XEXP (x
, 0))
1454 || GET_CODE (XEXP (x
, 0)) == VALUE
))
1457 if (offset
.is_constant (&c
))
1458 return cselib_hash_plus_const_int (XEXP (x
, 0),
1459 trunc_int_for_mode (c
, Pmode
),
1463 hashval_t tem_hash
= cselib_hash_rtx (XEXP (x
, 0), create
, memmode
);
1466 hash
.merge_hash (tem_hash
);
1467 tem_hash
= cselib_hash_rtx (gen_int_mode (offset
, GET_MODE (x
)),
1471 hash
.merge_hash (tem_hash
);
1472 return hash
.end () ? hash
.end () : 1 + (unsigned) PLUS
;
1477 gcc_assert (memmode
!= VOIDmode
);
1478 hashval_t tem_hash
= cselib_hash_rtx (XEXP (x
, 1), create
, memmode
);
1481 hash
.merge_hash (tem_hash
);
1482 return hash
.end () ? hash
.end () : 1 + (unsigned) PRE_MODIFY
;
1489 gcc_assert (memmode
!= VOIDmode
);
1490 hashval_t tem_hash
= cselib_hash_rtx (XEXP (x
, 0), create
, memmode
);
1493 hash
.merge_hash (tem_hash
);
1494 return hash
.end () ? hash
.end () : 1 + (unsigned) code
;
1499 case UNSPEC_VOLATILE
:
1503 if (MEM_VOLATILE_P (x
))
1509 if (GET_MODE (x
) == Pmode
1510 && (REG_P (XEXP (x
, 0))
1511 || MEM_P (XEXP (x
, 0))
1512 || GET_CODE (XEXP (x
, 0)) == VALUE
)
1513 && CONST_INT_P (XEXP (x
, 1)))
1514 return cselib_hash_plus_const_int (XEXP (x
, 0), INTVAL (XEXP (x
, 1)),
1522 i
= GET_RTX_LENGTH (code
) - 1;
1523 fmt
= GET_RTX_FORMAT (code
);
1525 if (COMMUTATIVE_P (x
))
1527 gcc_assert (i
== 1 && fmt
[0] == 'e' && fmt
[1] == 'e');
1528 hashval_t tem1_hash
= cselib_hash_rtx (XEXP (x
, 1), create
, memmode
);
1531 hashval_t tem0_hash
= cselib_hash_rtx (XEXP (x
, 0), create
, memmode
);
1534 hash
.add_commutative (tem0_hash
, tem1_hash
);
1535 return hash
.end () ? hash
.end () : 1 + (unsigned int) GET_CODE (x
);
1544 rtx tem
= XEXP (x
, i
);
1545 hashval_t tem_hash
= cselib_hash_rtx (tem
, create
, memmode
);
1548 hash
.merge_hash (tem_hash
);
1552 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1555 = cselib_hash_rtx (XVECEXP (x
, i
, j
), create
, memmode
);
1558 hash
.merge_hash (tem_hash
);
1564 const char *p
= (const char *) XSTR (x
, i
);
1567 hash
.add (p
, strlen (p
));
1572 hash
.add_hwi (XINT (x
, i
));
1576 hash
.add_hwi (XLOC (x
, i
));
1580 hash
.add_int (constant_lower_bound (SUBREG_BYTE (x
)));
1593 return hash
.end () ? hash
.end () : 1 + (unsigned int) GET_CODE (x
);
1596 /* Create a new value structure for VALUE and initialize it. The mode of the
1599 static inline cselib_val
*
1600 new_cselib_val (hashval_t hash
, machine_mode mode
, rtx x
)
1602 cselib_val
*e
= cselib_val_pool
.allocate ();
1605 gcc_assert (next_uid
);
1608 e
->uid
= next_uid
++;
1609 /* We use an alloc pool to allocate this RTL construct because it
1610 accounts for about 8% of the overall memory usage. We know
1611 precisely when we can have VALUE RTXen (when cselib is active)
1612 so we don't need to put them in garbage collected memory.
1613 ??? Why should a VALUE be an RTX in the first place? */
1614 e
->val_rtx
= (rtx_def
*) value_pool
.allocate ();
1615 memset (e
->val_rtx
, 0, RTX_HDR_SIZE
);
1616 PUT_CODE (e
->val_rtx
, VALUE
);
1617 PUT_MODE (e
->val_rtx
, mode
);
1618 CSELIB_VAL_PTR (e
->val_rtx
) = e
;
1621 e
->next_containing_mem
= 0;
1623 scalar_int_mode int_mode
;
1624 if (REG_P (x
) && is_int_mode (mode
, &int_mode
)
1625 && GET_MODE_SIZE (int_mode
) > 1
1626 && REG_VALUES (REGNO (x
)) != NULL
1627 && (!cselib_current_insn
|| !DEBUG_INSN_P (cselib_current_insn
)))
1629 rtx copy
= shallow_copy_rtx (x
);
1630 scalar_int_mode narrow_mode_iter
;
1631 FOR_EACH_MODE_UNTIL (narrow_mode_iter
, int_mode
)
1633 PUT_MODE_RAW (copy
, narrow_mode_iter
);
1634 cselib_val
*v
= cselib_lookup (copy
, narrow_mode_iter
, 0, VOIDmode
);
1637 rtx sub
= lowpart_subreg (narrow_mode_iter
, e
->val_rtx
, int_mode
);
1639 new_elt_loc_list (v
, sub
);
1644 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1646 fprintf (dump_file
, "cselib value %u:%u ", e
->uid
, hash
);
1647 if (flag_dump_noaddr
|| flag_dump_unnumbered
)
1648 fputs ("# ", dump_file
);
1650 fprintf (dump_file
, "%p ", (void*)e
);
1651 print_rtl_single (dump_file
, x
);
1652 fputc ('\n', dump_file
);
1658 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1659 contains the data at this address. X is a MEM that represents the
1660 value. Update the two value structures to represent this situation. */
1663 add_mem_for_addr (cselib_val
*addr_elt
, cselib_val
*mem_elt
, rtx x
)
1665 addr_elt
= canonical_cselib_val (addr_elt
);
1666 mem_elt
= canonical_cselib_val (mem_elt
);
1668 /* Avoid duplicates. */
1669 addr_space_t as
= MEM_ADDR_SPACE (x
);
1670 for (elt_loc_list
*l
= mem_elt
->locs
; l
; l
= l
->next
)
1672 && CSELIB_VAL_PTR (XEXP (l
->loc
, 0)) == addr_elt
1673 && MEM_ADDR_SPACE (l
->loc
) == as
)
1675 promote_debug_loc (l
);
1679 addr_elt
->addr_list
= new_elt_list (addr_elt
->addr_list
, mem_elt
);
1680 new_elt_loc_list (mem_elt
,
1681 replace_equiv_address_nv (x
, addr_elt
->val_rtx
));
1682 if (mem_elt
->next_containing_mem
== NULL
)
1684 mem_elt
->next_containing_mem
= first_containing_mem
;
1685 first_containing_mem
= mem_elt
;
1689 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1690 If CREATE, make a new one if we haven't seen it before. */
1693 cselib_lookup_mem (rtx x
, int create
)
1695 machine_mode mode
= GET_MODE (x
);
1696 machine_mode addr_mode
;
1699 cselib_val
*mem_elt
;
1701 if (MEM_VOLATILE_P (x
) || mode
== BLKmode
1702 || !cselib_record_memory
1703 || (FLOAT_MODE_P (mode
) && flag_float_store
))
1706 addr_mode
= GET_MODE (XEXP (x
, 0));
1707 if (addr_mode
== VOIDmode
)
1710 /* Look up the value for the address. */
1711 addr
= cselib_lookup (XEXP (x
, 0), addr_mode
, create
, mode
);
1714 addr
= canonical_cselib_val (addr
);
1716 /* Find a value that describes a value of our mode at that address. */
1717 addr_space_t as
= MEM_ADDR_SPACE (x
);
1718 for (elt_list
*l
= addr
->addr_list
; l
; l
= l
->next
)
1719 if (GET_MODE (l
->elt
->val_rtx
) == mode
)
1721 for (elt_loc_list
*l2
= l
->elt
->locs
; l2
; l2
= l2
->next
)
1722 if (MEM_P (l2
->loc
) && MEM_ADDR_SPACE (l2
->loc
) == as
)
1724 promote_debug_loc (l
->elt
->locs
);
1732 mem_elt
= new_cselib_val (next_uid
, mode
, x
);
1733 add_mem_for_addr (addr
, mem_elt
, x
);
1734 slot
= cselib_find_slot (mode
, x
, mem_elt
->hash
, INSERT
, VOIDmode
);
1739 /* Search through the possible substitutions in P. We prefer a non reg
1740 substitution because this allows us to expand the tree further. If
1741 we find, just a reg, take the lowest regno. There may be several
1742 non-reg results, we just take the first one because they will all
1743 expand to the same place. */
1746 expand_loc (struct elt_loc_list
*p
, struct expand_value_data
*evd
,
1749 rtx reg_result
= NULL
;
1750 unsigned int regno
= UINT_MAX
;
1751 struct elt_loc_list
*p_in
= p
;
1753 for (; p
; p
= p
->next
)
1755 /* Return these right away to avoid returning stack pointer based
1756 expressions for frame pointer and vice versa, which is something
1757 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1758 for more details. */
1760 && (REGNO (p
->loc
) == STACK_POINTER_REGNUM
1761 || REGNO (p
->loc
) == FRAME_POINTER_REGNUM
1762 || REGNO (p
->loc
) == HARD_FRAME_POINTER_REGNUM
1763 || REGNO (p
->loc
) == cfa_base_preserved_regno
))
1765 /* Avoid infinite recursion trying to expand a reg into a
1767 if ((REG_P (p
->loc
))
1768 && (REGNO (p
->loc
) < regno
)
1769 && !bitmap_bit_p (evd
->regs_active
, REGNO (p
->loc
)))
1771 reg_result
= p
->loc
;
1772 regno
= REGNO (p
->loc
);
1774 /* Avoid infinite recursion and do not try to expand the
1776 else if (GET_CODE (p
->loc
) == VALUE
1777 && CSELIB_VAL_PTR (p
->loc
)->locs
== p_in
)
1779 else if (!REG_P (p
->loc
))
1782 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1784 print_inline_rtx (dump_file
, p
->loc
, 0);
1785 fprintf (dump_file
, "\n");
1787 if (GET_CODE (p
->loc
) == LO_SUM
1788 && GET_CODE (XEXP (p
->loc
, 1)) == SYMBOL_REF
1790 && (note
= find_reg_note (p
->setting_insn
, REG_EQUAL
, NULL_RTX
))
1791 && XEXP (note
, 0) == XEXP (p
->loc
, 1))
1792 return XEXP (p
->loc
, 1);
1793 result
= cselib_expand_value_rtx_1 (p
->loc
, evd
, max_depth
- 1);
1800 if (regno
!= UINT_MAX
)
1803 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1804 fprintf (dump_file
, "r%d\n", regno
);
1806 result
= cselib_expand_value_rtx_1 (reg_result
, evd
, max_depth
- 1);
1811 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1815 print_inline_rtx (dump_file
, reg_result
, 0);
1816 fprintf (dump_file
, "\n");
1819 fprintf (dump_file
, "NULL\n");
1825 /* Forward substitute and expand an expression out to its roots.
1826 This is the opposite of common subexpression. Because local value
1827 numbering is such a weak optimization, the expanded expression is
1828 pretty much unique (not from a pointer equals point of view but
1829 from a tree shape point of view.
1831 This function returns NULL if the expansion fails. The expansion
1832 will fail if there is no value number for one of the operands or if
1833 one of the operands has been overwritten between the current insn
1834 and the beginning of the basic block. For instance x has no
1840 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1841 It is clear on return. */
1844 cselib_expand_value_rtx (rtx orig
, bitmap regs_active
, int max_depth
)
1846 struct expand_value_data evd
;
1848 evd
.regs_active
= regs_active
;
1849 evd
.callback
= NULL
;
1850 evd
.callback_arg
= NULL
;
1853 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
);
1856 /* Same as cselib_expand_value_rtx, but using a callback to try to
1857 resolve some expressions. The CB function should return ORIG if it
1858 can't or does not want to deal with a certain RTX. Any other
1859 return value, including NULL, will be used as the expansion for
1860 VALUE, without any further changes. */
1863 cselib_expand_value_rtx_cb (rtx orig
, bitmap regs_active
, int max_depth
,
1864 cselib_expand_callback cb
, void *data
)
1866 struct expand_value_data evd
;
1868 evd
.regs_active
= regs_active
;
1870 evd
.callback_arg
= data
;
1873 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
);
1876 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1877 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1878 would return NULL or non-NULL, without allocating new rtx. */
1881 cselib_dummy_expand_value_rtx_cb (rtx orig
, bitmap regs_active
, int max_depth
,
1882 cselib_expand_callback cb
, void *data
)
1884 struct expand_value_data evd
;
1886 evd
.regs_active
= regs_active
;
1888 evd
.callback_arg
= data
;
1891 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
) != NULL
;
1894 /* Internal implementation of cselib_expand_value_rtx and
1895 cselib_expand_value_rtx_cb. */
1898 cselib_expand_value_rtx_1 (rtx orig
, struct expand_value_data
*evd
,
1904 const char *format_ptr
;
1907 code
= GET_CODE (orig
);
1909 /* For the context of dse, if we end up expand into a huge tree, we
1910 will not have a useful address, so we might as well just give up
1919 struct elt_list
*l
= REG_VALUES (REGNO (orig
));
1921 if (l
&& l
->elt
== NULL
)
1923 for (; l
; l
= l
->next
)
1924 if (GET_MODE (l
->elt
->val_rtx
) == GET_MODE (orig
))
1927 unsigned regno
= REGNO (orig
);
1929 /* The only thing that we are not willing to do (this
1930 is requirement of dse and if others potential uses
1931 need this function we should add a parm to control
1932 it) is that we will not substitute the
1933 STACK_POINTER_REGNUM, FRAME_POINTER or the
1936 These expansions confuses the code that notices that
1937 stores into the frame go dead at the end of the
1938 function and that the frame is not effected by calls
1939 to subroutines. If you allow the
1940 STACK_POINTER_REGNUM substitution, then dse will
1941 think that parameter pushing also goes dead which is
1942 wrong. If you allow the FRAME_POINTER or the
1943 HARD_FRAME_POINTER then you lose the opportunity to
1944 make the frame assumptions. */
1945 if (regno
== STACK_POINTER_REGNUM
1946 || regno
== FRAME_POINTER_REGNUM
1947 || regno
== HARD_FRAME_POINTER_REGNUM
1948 || regno
== cfa_base_preserved_regno
)
1951 bitmap_set_bit (evd
->regs_active
, regno
);
1953 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1954 fprintf (dump_file
, "expanding: r%d into: ", regno
);
1956 result
= expand_loc (l
->elt
->locs
, evd
, max_depth
);
1957 bitmap_clear_bit (evd
->regs_active
, regno
);
1972 /* SCRATCH must be shared because they represent distinct values. */
1975 if (REG_P (XEXP (orig
, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig
, 0))))
1980 if (shared_const_p (orig
))
1990 subreg
= evd
->callback (orig
, evd
->regs_active
, max_depth
,
1996 subreg
= cselib_expand_value_rtx_1 (SUBREG_REG (orig
), evd
,
2000 scopy
= simplify_gen_subreg (GET_MODE (orig
), subreg
,
2001 GET_MODE (SUBREG_REG (orig
)),
2002 SUBREG_BYTE (orig
));
2004 || (GET_CODE (scopy
) == SUBREG
2005 && !REG_P (SUBREG_REG (scopy
))
2006 && !MEM_P (SUBREG_REG (scopy
))))
2016 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
2018 fputs ("\nexpanding ", dump_file
);
2019 print_rtl_single (dump_file
, orig
);
2020 fputs (" into...", dump_file
);
2025 result
= evd
->callback (orig
, evd
->regs_active
, max_depth
,
2032 result
= expand_loc (CSELIB_VAL_PTR (orig
)->locs
, evd
, max_depth
);
2038 return evd
->callback (orig
, evd
->regs_active
, max_depth
,
2046 /* Copy the various flags, fields, and other information. We assume
2047 that all fields need copying, and then clear the fields that should
2048 not be copied. That is the sensible default behavior, and forces
2049 us to explicitly document why we are *not* copying a flag. */
2053 copy
= shallow_copy_rtx (orig
);
2055 format_ptr
= GET_RTX_FORMAT (code
);
2057 for (i
= 0; i
< GET_RTX_LENGTH (code
); i
++)
2058 switch (*format_ptr
++)
2061 if (XEXP (orig
, i
) != NULL
)
2063 rtx result
= cselib_expand_value_rtx_1 (XEXP (orig
, i
), evd
,
2068 XEXP (copy
, i
) = result
;
2074 if (XVEC (orig
, i
) != NULL
)
2077 XVEC (copy
, i
) = rtvec_alloc (XVECLEN (orig
, i
));
2078 for (j
= 0; j
< XVECLEN (orig
, i
); j
++)
2080 rtx result
= cselib_expand_value_rtx_1 (XVECEXP (orig
, i
, j
),
2081 evd
, max_depth
- 1);
2085 XVECEXP (copy
, i
, j
) = result
;
2100 /* These are left unchanged. */
2110 mode
= GET_MODE (copy
);
2111 /* If an operand has been simplified into CONST_INT, which doesn't
2112 have a mode and the mode isn't derivable from whole rtx's mode,
2113 try simplify_*_operation first with mode from original's operand
2114 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
2116 switch (GET_RTX_CLASS (code
))
2119 if (CONST_INT_P (XEXP (copy
, 0))
2120 && GET_MODE (XEXP (orig
, 0)) != VOIDmode
)
2122 scopy
= simplify_unary_operation (code
, mode
, XEXP (copy
, 0),
2123 GET_MODE (XEXP (orig
, 0)));
2128 case RTX_COMM_ARITH
:
2130 /* These expressions can derive operand modes from the whole rtx's mode. */
2133 case RTX_BITFIELD_OPS
:
2134 if (CONST_INT_P (XEXP (copy
, 0))
2135 && GET_MODE (XEXP (orig
, 0)) != VOIDmode
)
2137 scopy
= simplify_ternary_operation (code
, mode
,
2138 GET_MODE (XEXP (orig
, 0)),
2139 XEXP (copy
, 0), XEXP (copy
, 1),
2146 case RTX_COMM_COMPARE
:
2147 if (CONST_INT_P (XEXP (copy
, 0))
2148 && GET_MODE (XEXP (copy
, 1)) == VOIDmode
2149 && (GET_MODE (XEXP (orig
, 0)) != VOIDmode
2150 || GET_MODE (XEXP (orig
, 1)) != VOIDmode
))
2152 scopy
= simplify_relational_operation (code
, mode
,
2153 (GET_MODE (XEXP (orig
, 0))
2155 ? GET_MODE (XEXP (orig
, 0))
2156 : GET_MODE (XEXP (orig
, 1)),
2166 scopy
= simplify_rtx (copy
);
2172 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
2173 with VALUE expressions. This way, it becomes independent of changes
2174 to registers and memory.
2175 X isn't actually modified; if modifications are needed, new rtl is
2176 allocated. However, the return value can share rtl with X.
2177 If X is within a MEM, MEMMODE must be the mode of the MEM. */
2180 cselib_subst_to_values (rtx x
, machine_mode memmode
)
2182 enum rtx_code code
= GET_CODE (x
);
2183 const char *fmt
= GET_RTX_FORMAT (code
);
2193 l
= REG_VALUES (REGNO (x
));
2194 if (l
&& l
->elt
== NULL
)
2196 for (; l
; l
= l
->next
)
2197 if (GET_MODE (l
->elt
->val_rtx
) == GET_MODE (x
))
2198 return l
->elt
->val_rtx
;
2203 e
= cselib_lookup_mem (x
, 0);
2204 /* This used to happen for autoincrements, but we deal with them
2205 properly now. Remove the if stmt for the next release. */
2208 /* Assign a value that doesn't match any other. */
2209 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
2214 e
= cselib_lookup (x
, GET_MODE (x
), 0, memmode
);
2224 gcc_assert (memmode
!= VOIDmode
);
2225 offset
= GET_MODE_SIZE (memmode
);
2226 if (code
== PRE_DEC
)
2228 return cselib_subst_to_values (plus_constant (GET_MODE (x
),
2229 XEXP (x
, 0), offset
),
2233 gcc_assert (memmode
!= VOIDmode
);
2234 return cselib_subst_to_values (XEXP (x
, 1), memmode
);
2239 gcc_assert (memmode
!= VOIDmode
);
2240 return cselib_subst_to_values (XEXP (x
, 0), memmode
);
2243 if (GET_MODE (x
) == Pmode
&& CONST_INT_P (XEXP (x
, 1)))
2245 rtx t
= cselib_subst_to_values (XEXP (x
, 0), memmode
);
2246 if (GET_CODE (t
) == VALUE
)
2248 if (SP_DERIVED_VALUE_P (t
) && XEXP (x
, 1) == const0_rtx
)
2250 for (struct elt_loc_list
*l
= CSELIB_VAL_PTR (t
)->locs
;
2252 if (GET_CODE (l
->loc
) == PLUS
2253 && GET_CODE (XEXP (l
->loc
, 0)) == VALUE
2254 && SP_DERIVED_VALUE_P (XEXP (l
->loc
, 0))
2255 && CONST_INT_P (XEXP (l
->loc
, 1)))
2256 return plus_constant (Pmode
, l
->loc
, INTVAL (XEXP (x
, 1)));
2258 if (t
!= XEXP (x
, 0))
2260 copy
= shallow_copy_rtx (x
);
2270 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2274 rtx t
= cselib_subst_to_values (XEXP (x
, i
), memmode
);
2276 if (t
!= XEXP (x
, i
))
2279 copy
= shallow_copy_rtx (x
);
2283 else if (fmt
[i
] == 'E')
2287 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2289 rtx t
= cselib_subst_to_values (XVECEXP (x
, i
, j
), memmode
);
2291 if (t
!= XVECEXP (x
, i
, j
))
2293 if (XVEC (x
, i
) == XVEC (copy
, i
))
2296 copy
= shallow_copy_rtx (x
);
2297 XVEC (copy
, i
) = shallow_copy_rtvec (XVEC (x
, i
));
2299 XVECEXP (copy
, i
, j
) = t
;
2308 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
2311 cselib_subst_to_values_from_insn (rtx x
, machine_mode memmode
, rtx_insn
*insn
)
2314 gcc_assert (!cselib_current_insn
);
2315 cselib_current_insn
= insn
;
2316 ret
= cselib_subst_to_values (x
, memmode
);
2317 cselib_current_insn
= NULL
;
2321 /* Look up the rtl expression X in our tables and return the value it
2322 has. If CREATE is zero, we return NULL if we don't know the value.
2323 Otherwise, we create a new one if possible, using mode MODE if X
2324 doesn't have a mode (i.e. because it's a constant). When X is part
2325 of an address, MEMMODE should be the mode of the enclosing MEM if
2326 we're tracking autoinc expressions. */
2329 cselib_lookup_1 (rtx x
, machine_mode mode
,
2330 int create
, machine_mode memmode
)
2335 if (GET_MODE (x
) != VOIDmode
)
2336 mode
= GET_MODE (x
);
2338 if (GET_CODE (x
) == VALUE
)
2339 return CSELIB_VAL_PTR (x
);
2344 unsigned int i
= REGNO (x
);
2347 if (l
&& l
->elt
== NULL
)
2349 for (; l
; l
= l
->next
)
2350 if (mode
== GET_MODE (l
->elt
->val_rtx
))
2352 promote_debug_loc (l
->elt
->locs
);
2359 if (i
< FIRST_PSEUDO_REGISTER
)
2361 unsigned int n
= hard_regno_nregs (i
, mode
);
2363 if (n
> max_value_regs
)
2367 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
2368 if (GET_MODE (x
) == Pmode
&& x
== stack_pointer_rtx
)
2369 SP_DERIVED_VALUE_P (e
->val_rtx
) = 1;
2370 new_elt_loc_list (e
, x
);
2372 scalar_int_mode int_mode
;
2373 if (REG_VALUES (i
) == 0)
2375 /* Maintain the invariant that the first entry of
2376 REG_VALUES, if present, must be the value used to set the
2377 register, or NULL. */
2378 used_regs
[n_used_regs
++] = i
;
2379 REG_VALUES (i
) = new_elt_list (REG_VALUES (i
), NULL
);
2381 else if (cselib_preserve_constants
2382 && is_int_mode (mode
, &int_mode
))
2384 /* During var-tracking, try harder to find equivalences
2385 for SUBREGs. If a setter sets say a DImode register
2386 and user uses that register only in SImode, add a lowpart
2388 struct elt_list
*lwider
= NULL
;
2389 scalar_int_mode lmode
;
2391 if (l
&& l
->elt
== NULL
)
2393 for (; l
; l
= l
->next
)
2394 if (is_int_mode (GET_MODE (l
->elt
->val_rtx
), &lmode
)
2395 && GET_MODE_SIZE (lmode
) > GET_MODE_SIZE (int_mode
)
2397 || partial_subreg_p (lmode
,
2398 GET_MODE (lwider
->elt
->val_rtx
))))
2400 struct elt_loc_list
*el
;
2401 if (i
< FIRST_PSEUDO_REGISTER
2402 && hard_regno_nregs (i
, lmode
) != 1)
2404 for (el
= l
->elt
->locs
; el
; el
= el
->next
)
2405 if (!REG_P (el
->loc
))
2412 rtx sub
= lowpart_subreg (int_mode
, lwider
->elt
->val_rtx
,
2413 GET_MODE (lwider
->elt
->val_rtx
));
2415 new_elt_loc_list (e
, sub
);
2418 REG_VALUES (i
)->next
= new_elt_list (REG_VALUES (i
)->next
, e
);
2419 slot
= cselib_find_slot (mode
, x
, e
->hash
, INSERT
, memmode
);
2425 return cselib_lookup_mem (x
, create
);
2427 hashval_t hashval
= cselib_hash_rtx (x
, create
, memmode
);
2428 /* Can't even create if hashing is not possible. */
2432 slot
= cselib_find_slot (mode
, x
, hashval
,
2433 create
? INSERT
: NO_INSERT
, memmode
);
2437 e
= (cselib_val
*) *slot
;
2441 e
= new_cselib_val (hashval
, mode
, x
);
2443 /* We have to fill the slot before calling cselib_subst_to_values:
2444 the hash table is inconsistent until we do so, and
2445 cselib_subst_to_values will need to do lookups. */
2447 rtx v
= cselib_subst_to_values (x
, memmode
);
2449 /* If cselib_preserve_constants, we might get a SP_DERIVED_VALUE_P
2450 VALUE that isn't in the hash tables anymore. */
2451 if (GET_CODE (v
) == VALUE
&& SP_DERIVED_VALUE_P (v
) && PRESERVED_VALUE_P (v
))
2452 PRESERVED_VALUE_P (e
->val_rtx
) = 1;
2454 new_elt_loc_list (e
, v
);
2458 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2461 cselib_lookup_from_insn (rtx x
, machine_mode mode
,
2462 int create
, machine_mode memmode
, rtx_insn
*insn
)
2466 gcc_assert (!cselib_current_insn
);
2467 cselib_current_insn
= insn
;
2469 ret
= cselib_lookup (x
, mode
, create
, memmode
);
2471 cselib_current_insn
= NULL
;
2476 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2477 maintains invariants related with debug insns. */
2480 cselib_lookup (rtx x
, machine_mode mode
,
2481 int create
, machine_mode memmode
)
2483 cselib_val
*ret
= cselib_lookup_1 (x
, mode
, create
, memmode
);
2485 /* ??? Should we return NULL if we're not to create an entry, the
2486 found loc is a debug loc and cselib_current_insn is not DEBUG?
2487 If so, we should also avoid converting val to non-DEBUG; probably
2488 easiest setting cselib_current_insn to NULL before the call
2491 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
2493 fputs ("cselib lookup ", dump_file
);
2494 print_inline_rtx (dump_file
, x
, 2);
2495 fprintf (dump_file
, " => %u:%u\n",
2497 ret
? ret
->hash
: 0);
2503 /* Invalidate the value at *L, which is part of REG_VALUES (REGNO). */
2506 cselib_invalidate_regno_val (unsigned int regno
, struct elt_list
**l
)
2508 cselib_val
*v
= (*l
)->elt
;
2509 if (*l
== REG_VALUES (regno
))
2511 /* Maintain the invariant that the first entry of
2512 REG_VALUES, if present, must be the value used to set
2513 the register, or NULL. This is also nice because
2514 then we won't push the same regno onto user_regs
2520 unchain_one_elt_list (l
);
2522 v
= canonical_cselib_val (v
);
2524 bool had_locs
= v
->locs
!= NULL
;
2525 rtx_insn
*setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
2527 /* Now, we clear the mapping from value to reg. It must exist, so
2528 this code will crash intentionally if it doesn't. */
2529 for (elt_loc_list
**p
= &v
->locs
; ; p
= &(*p
)->next
)
2533 if (REG_P (x
) && REGNO (x
) == regno
)
2535 unchain_one_elt_loc_list (p
);
2540 if (had_locs
&& cselib_useless_value_p (v
))
2542 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
2543 n_useless_debug_values
++;
2549 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2550 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2551 is used to determine how many hard registers are being changed. If MODE
2552 is VOIDmode, then only REGNO is being changed; this is used when
2553 invalidating call clobbered registers across a call. */
2556 cselib_invalidate_regno (unsigned int regno
, machine_mode mode
)
2558 unsigned int endregno
;
2561 /* If we see pseudos after reload, something is _wrong_. */
2562 gcc_assert (!reload_completed
|| regno
< FIRST_PSEUDO_REGISTER
2563 || reg_renumber
[regno
] < 0);
2565 /* Determine the range of registers that must be invalidated. For
2566 pseudos, only REGNO is affected. For hard regs, we must take MODE
2567 into account, and we must also invalidate lower register numbers
2568 if they contain values that overlap REGNO. */
2569 if (regno
< FIRST_PSEUDO_REGISTER
)
2571 gcc_assert (mode
!= VOIDmode
);
2573 if (regno
< max_value_regs
)
2576 i
= regno
- max_value_regs
;
2578 endregno
= end_hard_regno (mode
, regno
);
2583 endregno
= regno
+ 1;
2586 for (; i
< endregno
; i
++)
2588 struct elt_list
**l
= ®_VALUES (i
);
2590 /* Go through all known values for this reg; if it overlaps the range
2591 we're invalidating, remove the value. */
2594 cselib_val
*v
= (*l
)->elt
;
2595 unsigned int this_last
= i
;
2597 if (i
< FIRST_PSEUDO_REGISTER
&& v
!= NULL
)
2598 this_last
= end_hard_regno (GET_MODE (v
->val_rtx
), i
) - 1;
2600 if (this_last
< regno
|| v
== NULL
2601 || (v
== cfa_base_preserved_val
2602 && i
== cfa_base_preserved_regno
))
2608 /* We have an overlap. */
2609 cselib_invalidate_regno_val (i
, l
);
2614 /* Invalidate any locations in the table which are changed because of a
2615 store to MEM_RTX. If this is called because of a non-const call
2616 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2619 cselib_invalidate_mem (rtx mem_rtx
)
2621 cselib_val
**vp
, *v
, *next
;
2625 mem_addr
= canon_rtx (get_addr (XEXP (mem_rtx
, 0)));
2626 mem_rtx
= canon_rtx (mem_rtx
);
2628 vp
= &first_containing_mem
;
2629 for (v
= *vp
; v
!= &dummy_val
; v
= next
)
2631 bool has_mem
= false;
2632 struct elt_loc_list
**p
= &v
->locs
;
2633 bool had_locs
= v
->locs
!= NULL
;
2634 rtx_insn
*setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
2635 rtx sp_base
= NULL_RTX
;
2636 HOST_WIDE_INT sp_off
= 0;
2642 struct elt_list
**mem_chain
;
2644 /* MEMs may occur in locations only at the top level; below
2645 that every MEM or REG is substituted by its VALUE. */
2652 /* When invalidating memory below the stack pointer for const/pure
2653 calls and alloca/VLAs aren't used, attempt to optimize. Values
2654 stored into area sometimes below the stack pointer shouldn't be
2655 addressable and should be stored just through stack pointer
2656 derived expressions, so don't invalidate MEMs not using stack
2657 derived addresses, or if the MEMs clearly aren't below the stack
2658 pointer. This isn't a fully conservative approach, the hope is
2659 that invalidating more MEMs than this isn't actually needed. */
2660 if (mem_rtx
== callmem
[1]
2661 && num_mems
< param_max_cselib_memory_locations
2662 && GET_CODE (XEXP (x
, 0)) == VALUE
2663 && !cfun
->calls_alloca
)
2665 cselib_val
*v2
= CSELIB_VAL_PTR (XEXP (x
, 0));
2666 rtx x_base
= NULL_RTX
;
2667 HOST_WIDE_INT x_off
= 0;
2668 if (SP_DERIVED_VALUE_P (v2
->val_rtx
))
2669 x_base
= v2
->val_rtx
;
2671 for (struct elt_loc_list
*l
= v2
->locs
; l
; l
= l
->next
)
2672 if (GET_CODE (l
->loc
) == PLUS
2673 && GET_CODE (XEXP (l
->loc
, 0)) == VALUE
2674 && SP_DERIVED_VALUE_P (XEXP (l
->loc
, 0))
2675 && CONST_INT_P (XEXP (l
->loc
, 1)))
2677 x_base
= XEXP (l
->loc
, 0);
2678 x_off
= INTVAL (XEXP (l
->loc
, 1));
2681 /* If x_base is NULL here, don't invalidate x as its address
2682 isn't derived from sp such that it could be in outgoing
2683 argument area of some call in !ACCUMULATE_OUTGOING_ARGS
2687 if (sp_base
== NULL_RTX
)
2690 = cselib_lookup_1 (stack_pointer_rtx
, Pmode
, 0,
2693 if (SP_DERIVED_VALUE_P (v3
->val_rtx
))
2694 sp_base
= v3
->val_rtx
;
2696 for (struct elt_loc_list
*l
= v3
->locs
;
2698 if (GET_CODE (l
->loc
) == PLUS
2699 && GET_CODE (XEXP (l
->loc
, 0)) == VALUE
2700 && SP_DERIVED_VALUE_P (XEXP (l
->loc
, 0))
2701 && CONST_INT_P (XEXP (l
->loc
, 1)))
2703 sp_base
= XEXP (l
->loc
, 0);
2704 sp_off
= INTVAL (XEXP (l
->loc
, 1));
2708 if (sp_base
== NULL_RTX
)
2711 /* Otherwise, if x_base and sp_base are the same,
2712 we know that x_base + x_off is the x's address and
2713 sp_base + sp_off is current value of stack pointer,
2714 so try to determine if x is certainly not below stack
2716 if (sp_base
== x_base
)
2718 if (STACK_GROWS_DOWNWARD
)
2720 HOST_WIDE_INT off
= sp_off
;
2721 #ifdef STACK_ADDRESS_OFFSET
2722 /* On SPARC take stack pointer bias into account as
2724 off
+= (STACK_ADDRESS_OFFSET
2725 - FIRST_PARM_OFFSET (current_function_decl
));
2728 /* x is at or above the current stack pointer,
2729 no need to invalidate it. */
2735 enum machine_mode mode
= GET_MODE (x
);
2736 if ((MEM_SIZE_KNOWN_P (x
)
2737 && MEM_SIZE (x
).is_constant (&sz
))
2739 && GET_MODE_SIZE (mode
).is_constant (&sz
)))
2741 && ((HOST_WIDE_INT
) ((unsigned HOST_WIDE_INT
)
2742 x_off
+ sz
) <= sp_off
))
2743 /* x's end is below or at the current stack
2744 pointer in !STACK_GROWS_DOWNWARD target,
2745 no need to invalidate it. */
2750 if (x_base
== NULL_RTX
)
2759 if (num_mems
< param_max_cselib_memory_locations
2760 && ! canon_anti_dependence (x
, false, mem_rtx
,
2761 GET_MODE (mem_rtx
), mem_addr
))
2769 /* This one overlaps. */
2770 /* We must have a mapping from this MEM's address to the
2771 value (E). Remove that, too. */
2772 addr
= cselib_lookup (XEXP (x
, 0), VOIDmode
, 0, GET_MODE (x
));
2773 addr
= canonical_cselib_val (addr
);
2774 gcc_checking_assert (v
== canonical_cselib_val (v
));
2775 mem_chain
= &addr
->addr_list
;
2778 cselib_val
*canon
= canonical_cselib_val ((*mem_chain
)->elt
);
2782 unchain_one_elt_list (mem_chain
);
2786 /* Record canonicalized elt. */
2787 (*mem_chain
)->elt
= canon
;
2789 mem_chain
= &(*mem_chain
)->next
;
2792 unchain_one_elt_loc_list (p
);
2795 if (had_locs
&& cselib_useless_value_p (v
))
2797 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
2798 n_useless_debug_values
++;
2803 next
= v
->next_containing_mem
;
2807 vp
= &(*vp
)->next_containing_mem
;
2810 v
->next_containing_mem
= NULL
;
2815 /* Invalidate DEST. */
2818 cselib_invalidate_rtx (rtx dest
)
2820 while (GET_CODE (dest
) == SUBREG
2821 || GET_CODE (dest
) == ZERO_EXTRACT
2822 || GET_CODE (dest
) == STRICT_LOW_PART
)
2823 dest
= XEXP (dest
, 0);
2826 cselib_invalidate_regno (REGNO (dest
), GET_MODE (dest
));
2827 else if (MEM_P (dest
))
2828 cselib_invalidate_mem (dest
);
2831 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2834 cselib_invalidate_rtx_note_stores (rtx dest
, const_rtx
,
2835 void *data ATTRIBUTE_UNUSED
)
2837 cselib_invalidate_rtx (dest
);
2840 /* Record the result of a SET instruction. DEST is being set; the source
2841 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2842 describes its address. */
2845 cselib_record_set (rtx dest
, cselib_val
*src_elt
, cselib_val
*dest_addr_elt
)
2847 if (src_elt
== 0 || side_effects_p (dest
))
2852 unsigned int dreg
= REGNO (dest
);
2853 if (dreg
< FIRST_PSEUDO_REGISTER
)
2855 unsigned int n
= REG_NREGS (dest
);
2857 if (n
> max_value_regs
)
2861 if (REG_VALUES (dreg
) == 0)
2863 used_regs
[n_used_regs
++] = dreg
;
2864 REG_VALUES (dreg
) = new_elt_list (REG_VALUES (dreg
), src_elt
);
2868 /* The register should have been invalidated. */
2869 gcc_assert (REG_VALUES (dreg
)->elt
== 0);
2870 REG_VALUES (dreg
)->elt
= src_elt
;
2873 if (cselib_useless_value_p (src_elt
))
2875 new_elt_loc_list (src_elt
, dest
);
2877 else if (MEM_P (dest
) && dest_addr_elt
!= 0
2878 && cselib_record_memory
)
2880 if (cselib_useless_value_p (src_elt
))
2882 add_mem_for_addr (dest_addr_elt
, src_elt
, dest
);
2886 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2889 cselib_add_permanent_equiv (cselib_val
*elt
, rtx x
, rtx_insn
*insn
)
2892 rtx_insn
*save_cselib_current_insn
= cselib_current_insn
;
2894 gcc_checking_assert (elt
);
2895 gcc_checking_assert (PRESERVED_VALUE_P (elt
->val_rtx
));
2896 gcc_checking_assert (!side_effects_p (x
));
2898 cselib_current_insn
= insn
;
2900 nelt
= cselib_lookup (x
, GET_MODE (elt
->val_rtx
), 1, VOIDmode
);
2904 cselib_any_perm_equivs
= true;
2906 if (!PRESERVED_VALUE_P (nelt
->val_rtx
))
2907 cselib_preserve_value (nelt
);
2909 new_elt_loc_list (nelt
, elt
->val_rtx
);
2912 cselib_current_insn
= save_cselib_current_insn
;
2915 /* Return TRUE if any permanent equivalences have been recorded since
2916 the table was last initialized. */
2918 cselib_have_permanent_equivalences (void)
2920 return cselib_any_perm_equivs
;
2923 /* Record stack_pointer_rtx to be equal to
2924 (plus:P cfa_base_preserved_val offset). Used by var-tracking
2925 at the start of basic blocks for !frame_pointer_needed functions. */
2928 cselib_record_sp_cfa_base_equiv (HOST_WIDE_INT offset
, rtx_insn
*insn
)
2930 rtx sp_derived_value
= NULL_RTX
;
2931 for (struct elt_loc_list
*l
= cfa_base_preserved_val
->locs
; l
; l
= l
->next
)
2932 if (GET_CODE (l
->loc
) == VALUE
2933 && SP_DERIVED_VALUE_P (l
->loc
))
2935 sp_derived_value
= l
->loc
;
2938 else if (GET_CODE (l
->loc
) == PLUS
2939 && GET_CODE (XEXP (l
->loc
, 0)) == VALUE
2940 && SP_DERIVED_VALUE_P (XEXP (l
->loc
, 0))
2941 && CONST_INT_P (XEXP (l
->loc
, 1)))
2943 sp_derived_value
= XEXP (l
->loc
, 0);
2944 offset
= offset
+ UINTVAL (XEXP (l
->loc
, 1));
2947 if (sp_derived_value
== NULL_RTX
)
2950 = cselib_lookup_from_insn (plus_constant (Pmode
, sp_derived_value
, offset
),
2951 Pmode
, 1, VOIDmode
, insn
);
2954 PRESERVED_VALUE_P (val
->val_rtx
) = 1;
2955 cselib_record_set (stack_pointer_rtx
, val
, NULL
);
2959 /* Return true if V is SP_DERIVED_VALUE_P (or SP_DERIVED_VALUE_P + CONST_INT)
2960 that can be expressed using cfa_base_preserved_val + CONST_INT. */
2963 cselib_sp_derived_value_p (cselib_val
*v
)
2965 if (!SP_DERIVED_VALUE_P (v
->val_rtx
))
2966 for (struct elt_loc_list
*l
= v
->locs
; l
; l
= l
->next
)
2967 if (GET_CODE (l
->loc
) == PLUS
2968 && GET_CODE (XEXP (l
->loc
, 0)) == VALUE
2969 && SP_DERIVED_VALUE_P (XEXP (l
->loc
, 0))
2970 && CONST_INT_P (XEXP (l
->loc
, 1)))
2971 v
= CSELIB_VAL_PTR (XEXP (l
->loc
, 0));
2972 if (!SP_DERIVED_VALUE_P (v
->val_rtx
))
2974 for (struct elt_loc_list
*l
= v
->locs
; l
; l
= l
->next
)
2975 if (l
->loc
== cfa_base_preserved_val
->val_rtx
)
2977 else if (GET_CODE (l
->loc
) == PLUS
2978 && XEXP (l
->loc
, 0) == cfa_base_preserved_val
->val_rtx
2979 && CONST_INT_P (XEXP (l
->loc
, 1)))
2984 /* There is no good way to determine how many elements there can be
2985 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2986 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2988 struct cselib_record_autoinc_data
2990 struct cselib_set
*sets
;
2994 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2995 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2998 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED
, rtx op ATTRIBUTE_UNUSED
,
2999 rtx dest
, rtx src
, rtx srcoff
, void *arg
)
3001 struct cselib_record_autoinc_data
*data
;
3002 data
= (struct cselib_record_autoinc_data
*)arg
;
3004 data
->sets
[data
->n_sets
].dest
= dest
;
3007 data
->sets
[data
->n_sets
].src
= gen_rtx_PLUS (GET_MODE (src
), src
, srcoff
);
3009 data
->sets
[data
->n_sets
].src
= src
;
3016 /* Record the effects of any sets and autoincs in INSN. */
3018 cselib_record_sets (rtx_insn
*insn
)
3022 struct cselib_set sets
[MAX_SETS
];
3024 int n_sets_before_autoinc
;
3025 int n_strict_low_parts
= 0;
3026 struct cselib_record_autoinc_data data
;
3028 rtx body
= PATTERN (insn
);
3029 if (GET_CODE (body
) == COND_EXEC
)
3031 cond
= COND_EXEC_TEST (body
);
3032 body
= COND_EXEC_CODE (body
);
3035 /* Find all sets. */
3036 if (GET_CODE (body
) == SET
)
3038 sets
[0].src
= SET_SRC (body
);
3039 sets
[0].dest
= SET_DEST (body
);
3042 else if (GET_CODE (body
) == PARALLEL
)
3044 /* Look through the PARALLEL and record the values being
3045 set, if possible. Also handle any CLOBBERs. */
3046 for (i
= XVECLEN (body
, 0) - 1; i
>= 0; --i
)
3048 rtx x
= XVECEXP (body
, 0, i
);
3050 if (GET_CODE (x
) == SET
)
3052 sets
[n_sets
].src
= SET_SRC (x
);
3053 sets
[n_sets
].dest
= SET_DEST (x
);
3060 && MEM_P (sets
[0].src
)
3061 && !cselib_record_memory
3062 && MEM_READONLY_P (sets
[0].src
))
3064 rtx note
= find_reg_equal_equiv_note (insn
);
3066 if (note
&& CONSTANT_P (XEXP (note
, 0)))
3067 sets
[0].src
= XEXP (note
, 0);
3071 data
.n_sets
= n_sets_before_autoinc
= n_sets
;
3072 for_each_inc_dec (PATTERN (insn
), cselib_record_autoinc_cb
, &data
);
3073 n_sets
= data
.n_sets
;
3075 /* Look up the values that are read. Do this before invalidating the
3076 locations that are written. */
3077 for (i
= 0; i
< n_sets
; i
++)
3079 rtx dest
= sets
[i
].dest
;
3082 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
3083 the low part after invalidating any knowledge about larger modes. */
3084 if (GET_CODE (sets
[i
].dest
) == STRICT_LOW_PART
)
3085 sets
[i
].dest
= dest
= XEXP (dest
, 0);
3087 /* We don't know how to record anything but REG or MEM. */
3089 || (MEM_P (dest
) && cselib_record_memory
))
3091 rtx src
= sets
[i
].src
;
3093 src
= gen_rtx_IF_THEN_ELSE (GET_MODE (dest
), cond
, src
, dest
);
3094 sets
[i
].src_elt
= cselib_lookup (src
, GET_MODE (dest
), 1, VOIDmode
);
3097 machine_mode address_mode
= get_address_mode (dest
);
3099 sets
[i
].dest_addr_elt
= cselib_lookup (XEXP (dest
, 0),
3104 sets
[i
].dest_addr_elt
= 0;
3107 /* Improve handling of STRICT_LOW_PART if the current value is known
3108 to be const0_rtx, then the low bits will be set to dest and higher
3109 bits will remain zero. Used in code like:
3111 {di:SI=0;clobber flags:CC;}
3112 flags:CCNO=cmp(bx:SI,0)
3113 strict_low_part(di:QI)=flags:CCNO<=0
3115 where we can note both that di:QI=flags:CCNO<=0 and
3116 also that because di:SI is known to be 0 and strict_low_part(di:QI)
3117 preserves the upper bits that di:SI=zero_extend(flags:CCNO<=0). */
3118 scalar_int_mode mode
;
3120 && cselib_record_sets_hook
3122 && HARD_REGISTER_P (dest
)
3124 && is_a
<scalar_int_mode
> (GET_MODE (dest
), &mode
)
3125 && n_sets
+ n_strict_low_parts
< MAX_SETS
)
3127 opt_scalar_int_mode wider_mode_iter
;
3128 FOR_EACH_WIDER_MODE (wider_mode_iter
, mode
)
3130 scalar_int_mode wider_mode
= wider_mode_iter
.require ();
3131 if (GET_MODE_PRECISION (wider_mode
) > BITS_PER_WORD
)
3134 rtx reg
= gen_lowpart (wider_mode
, dest
);
3138 cselib_val
*v
= cselib_lookup (reg
, wider_mode
, 0, VOIDmode
);
3142 struct elt_loc_list
*l
;
3143 for (l
= v
->locs
; l
; l
= l
->next
)
3144 if (l
->loc
== const0_rtx
)
3150 sets
[n_sets
+ n_strict_low_parts
].dest
= reg
;
3151 sets
[n_sets
+ n_strict_low_parts
].src
= dest
;
3152 sets
[n_sets
+ n_strict_low_parts
++].src_elt
= sets
[i
].src_elt
;
3158 if (cselib_record_sets_hook
)
3159 cselib_record_sets_hook (insn
, sets
, n_sets
);
3161 /* Invalidate all locations written by this insn. Note that the elts we
3162 looked up in the previous loop aren't affected, just some of their
3163 locations may go away. */
3164 note_pattern_stores (body
, cselib_invalidate_rtx_note_stores
, NULL
);
3166 for (i
= n_sets_before_autoinc
; i
< n_sets
; i
++)
3167 cselib_invalidate_rtx (sets
[i
].dest
);
3169 /* If this is an asm, look for duplicate sets. This can happen when the
3170 user uses the same value as an output multiple times. This is valid
3171 if the outputs are not actually used thereafter. Treat this case as
3172 if the value isn't actually set. We do this by smashing the destination
3173 to pc_rtx, so that we won't record the value later. */
3174 if (n_sets
>= 2 && asm_noperands (body
) >= 0)
3176 for (i
= 0; i
< n_sets
; i
++)
3178 rtx dest
= sets
[i
].dest
;
3179 if (REG_P (dest
) || MEM_P (dest
))
3182 for (j
= i
+ 1; j
< n_sets
; j
++)
3183 if (rtx_equal_p (dest
, sets
[j
].dest
))
3185 sets
[i
].dest
= pc_rtx
;
3186 sets
[j
].dest
= pc_rtx
;
3192 /* Now enter the equivalences in our tables. */
3193 for (i
= 0; i
< n_sets
; i
++)
3195 rtx dest
= sets
[i
].dest
;
3197 || (MEM_P (dest
) && cselib_record_memory
))
3198 cselib_record_set (dest
, sets
[i
].src_elt
, sets
[i
].dest_addr_elt
);
3201 /* And deal with STRICT_LOW_PART. */
3202 for (i
= 0; i
< n_strict_low_parts
; i
++)
3204 if (! PRESERVED_VALUE_P (sets
[n_sets
+ i
].src_elt
->val_rtx
))
3206 machine_mode dest_mode
= GET_MODE (sets
[n_sets
+ i
].dest
);
3208 = cselib_lookup (sets
[n_sets
+ i
].dest
, dest_mode
, 1, VOIDmode
);
3209 cselib_preserve_value (v
);
3210 rtx r
= gen_rtx_ZERO_EXTEND (dest_mode
,
3211 sets
[n_sets
+ i
].src_elt
->val_rtx
);
3212 cselib_add_permanent_equiv (v
, r
, insn
);
3216 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
3219 fp_setter_insn (rtx_insn
*insn
)
3221 rtx expr
, pat
= NULL_RTX
;
3223 if (!RTX_FRAME_RELATED_P (insn
))
3226 expr
= find_reg_note (insn
, REG_FRAME_RELATED_EXPR
, NULL_RTX
);
3228 pat
= XEXP (expr
, 0);
3229 if (!modified_in_p (hard_frame_pointer_rtx
, pat
? pat
: insn
))
3232 /* Don't return true for frame pointer restores in the epilogue. */
3233 if (find_reg_note (insn
, REG_CFA_RESTORE
, hard_frame_pointer_rtx
))
3238 /* V is one of the values in REG_VALUES (REGNO). Return true if it
3239 would be invalidated by CALLEE_ABI. */
3242 cselib_invalidated_by_call_p (const function_abi
&callee_abi
,
3243 unsigned int regno
, cselib_val
*v
)
3245 machine_mode mode
= GET_MODE (v
->val_rtx
);
3246 if (mode
== VOIDmode
)
3248 v
= REG_VALUES (regno
)->elt
;
3250 /* If we don't know what the mode of the constant value is, and we
3251 don't know what mode the register was set in, conservatively
3252 assume that the register is clobbered. The value's going to be
3253 essentially useless in this case anyway. */
3255 mode
= GET_MODE (v
->val_rtx
);
3257 return callee_abi
.clobbers_reg_p (mode
, regno
);
3260 /* Record the effects of INSN. */
3263 cselib_process_insn (rtx_insn
*insn
)
3268 cselib_current_insn
= insn
;
3270 /* Forget everything at a CODE_LABEL or a setjmp. */
3273 && find_reg_note (insn
, REG_SETJMP
, NULL
)))
3274 && !cselib_preserve_constants
)
3276 cselib_reset_table (next_uid
);
3277 cselib_current_insn
= NULL
;
3281 if (! INSN_P (insn
))
3283 cselib_current_insn
= NULL
;
3287 /* If this is a call instruction, forget anything stored in a
3288 call clobbered register, or, if this is not a const call, in
3292 function_abi callee_abi
= insn_callee_abi (insn
);
3293 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
3295 elt_list
**l
= ®_VALUES (i
);
3298 cselib_val
*v
= (*l
)->elt
;
3299 if (v
&& cselib_invalidated_by_call_p (callee_abi
, i
, v
))
3300 cselib_invalidate_regno_val (i
, l
);
3306 /* Since it is not clear how cselib is going to be used, be
3307 conservative here and treat looping pure or const functions
3308 as if they were regular functions. */
3309 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn
)
3310 || !(RTL_CONST_OR_PURE_CALL_P (insn
)))
3311 cselib_invalidate_mem (callmem
[0]);
3314 /* For const/pure calls, invalidate any argument slots because
3315 they are owned by the callee. */
3316 for (x
= CALL_INSN_FUNCTION_USAGE (insn
); x
; x
= XEXP (x
, 1))
3317 if (GET_CODE (XEXP (x
, 0)) == USE
3318 && MEM_P (XEXP (XEXP (x
, 0), 0)))
3319 cselib_invalidate_mem (XEXP (XEXP (x
, 0), 0));
3320 /* And invalidate memory below the stack (or above for
3321 !STACK_GROWS_DOWNWARD), as even const/pure call can invalidate
3322 that. Do this only if !ACCUMULATE_OUTGOING_ARGS or if
3323 cfun->calls_alloca, otherwise the stack pointer shouldn't be
3324 changing in the middle of the function and nothing should be
3325 stored below the stack pointer. */
3326 if (!ACCUMULATE_OUTGOING_ARGS
|| cfun
->calls_alloca
)
3327 cselib_invalidate_mem (callmem
[1]);
3331 cselib_record_sets (insn
);
3333 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
3334 after we have processed the insn. */
3337 for (x
= CALL_INSN_FUNCTION_USAGE (insn
); x
; x
= XEXP (x
, 1))
3338 if (GET_CODE (XEXP (x
, 0)) == CLOBBER
)
3339 cselib_invalidate_rtx (XEXP (XEXP (x
, 0), 0));
3341 /* Flush everything on setjmp. */
3342 if (cselib_preserve_constants
3343 && find_reg_note (insn
, REG_SETJMP
, NULL
))
3345 cselib_preserve_only_values ();
3346 cselib_reset_table (next_uid
);
3350 /* On setter of the hard frame pointer if frame_pointer_needed,
3351 invalidate stack_pointer_rtx, so that sp and {,h}fp based
3352 VALUEs are distinct. */
3353 if (reload_completed
3354 && frame_pointer_needed
3355 && fp_setter_insn (insn
))
3356 cselib_invalidate_rtx (stack_pointer_rtx
);
3358 cselib_current_insn
= NULL
;
3360 if (n_useless_values
> MAX_USELESS_VALUES
3361 /* remove_useless_values is linear in the hash table size. Avoid
3362 quadratic behavior for very large hashtables with very few
3363 useless elements. */
3364 && ((unsigned int)n_useless_values
3365 > (cselib_hash_table
->elements () - n_debug_values
) / 4))
3366 remove_useless_values ();
3369 /* Initialize cselib for one pass. The caller must also call
3370 init_alias_analysis. */
3373 cselib_init (int record_what
)
3375 cselib_record_memory
= record_what
& CSELIB_RECORD_MEMORY
;
3376 cselib_preserve_constants
= record_what
& CSELIB_PRESERVE_CONSTANTS
;
3377 cselib_any_perm_equivs
= false;
3379 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
3380 see canon_true_dependence. This is only created once. */
3382 callmem
[0] = gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
));
3383 /* Similarly create a MEM representing roughly everything below
3384 the stack for STACK_GROWS_DOWNWARD targets or everything above
3385 it otherwise. Do this only when !ACCUMULATE_OUTGOING_ARGS or
3386 if cfun->calls_alloca, otherwise the stack pointer shouldn't be
3387 changing in the middle of the function and nothing should be stored
3388 below the stack pointer. */
3389 if (!callmem
[1] && (!ACCUMULATE_OUTGOING_ARGS
|| cfun
->calls_alloca
))
3391 if (STACK_GROWS_DOWNWARD
)
3393 unsigned HOST_WIDE_INT off
= -(GET_MODE_MASK (Pmode
) >> 1);
3394 #ifdef STACK_ADDRESS_OFFSET
3395 /* On SPARC take stack pointer bias into account as well. */
3396 off
+= (STACK_ADDRESS_OFFSET
3397 - FIRST_PARM_OFFSET (current_function_decl
));
3399 callmem
[1] = plus_constant (Pmode
, stack_pointer_rtx
, off
);
3402 callmem
[1] = stack_pointer_rtx
;
3403 callmem
[1] = gen_rtx_MEM (BLKmode
, callmem
[1]);
3404 set_mem_size (callmem
[1], GET_MODE_MASK (Pmode
) >> 1);
3407 cselib_nregs
= max_reg_num ();
3409 /* We preserve reg_values to allow expensive clearing of the whole thing.
3410 Reallocate it however if it happens to be too large. */
3411 if (!reg_values
|| reg_values_size
< cselib_nregs
3412 || (reg_values_size
> 10 && reg_values_size
> cselib_nregs
* 4))
3415 /* Some space for newly emit instructions so we don't end up
3416 reallocating in between passes. */
3417 reg_values_size
= cselib_nregs
+ (63 + cselib_nregs
) / 16;
3418 reg_values
= XCNEWVEC (struct elt_list
*, reg_values_size
);
3420 used_regs
= XNEWVEC (unsigned int, cselib_nregs
);
3422 /* FIXME: enable sanitization (PR87845) */
3424 = new hash_table
<cselib_hasher
> (31, /* ggc */ false,
3425 /* sanitize_eq_and_hash */ false);
3426 if (cselib_preserve_constants
)
3427 cselib_preserved_hash_table
3428 = new hash_table
<cselib_hasher
> (31, /* ggc */ false,
3429 /* sanitize_eq_and_hash */ false);
3433 /* Called when the current user is done with cselib. */
3436 cselib_finish (void)
3438 bool preserved
= cselib_preserve_constants
;
3439 cselib_discard_hook
= NULL
;
3440 cselib_preserve_constants
= false;
3441 cselib_any_perm_equivs
= false;
3442 cfa_base_preserved_val
= NULL
;
3443 cfa_base_preserved_regno
= INVALID_REGNUM
;
3444 elt_list_pool
.release ();
3445 elt_loc_list_pool
.release ();
3446 cselib_val_pool
.release ();
3447 value_pool
.release ();
3448 cselib_clear_table ();
3449 delete cselib_hash_table
;
3450 cselib_hash_table
= NULL
;
3452 delete cselib_preserved_hash_table
;
3453 cselib_preserved_hash_table
= NULL
;
3456 n_useless_values
= 0;
3457 n_useless_debug_values
= 0;
3462 /* Dump the cselib_val *X to FILE *OUT. */
3465 dump_cselib_val (cselib_val
**x
, FILE *out
)
3468 bool need_lf
= true;
3470 print_inline_rtx (out
, v
->val_rtx
, 0);
3474 struct elt_loc_list
*l
= v
->locs
;
3480 fputs (" locs:", out
);
3483 if (l
->setting_insn
)
3484 fprintf (out
, "\n from insn %i ",
3485 INSN_UID (l
->setting_insn
));
3487 fprintf (out
, "\n ");
3488 print_inline_rtx (out
, l
->loc
, 4);
3490 while ((l
= l
->next
));
3495 fputs (" no locs", out
);
3501 struct elt_list
*e
= v
->addr_list
;
3507 fputs (" addr list:", out
);
3511 print_inline_rtx (out
, e
->elt
->val_rtx
, 2);
3513 while ((e
= e
->next
));
3518 fputs (" no addrs", out
);
3522 if (v
->next_containing_mem
== &dummy_val
)
3523 fputs (" last mem\n", out
);
3524 else if (v
->next_containing_mem
)
3526 fputs (" next mem ", out
);
3527 print_inline_rtx (out
, v
->next_containing_mem
->val_rtx
, 2);
3536 /* Dump to OUT everything in the CSELIB table. */
3539 dump_cselib_table (FILE *out
)
3541 fprintf (out
, "cselib hash table:\n");
3542 cselib_hash_table
->traverse
<FILE *, dump_cselib_val
> (out
);
3543 fprintf (out
, "cselib preserved hash table:\n");
3544 cselib_preserved_hash_table
->traverse
<FILE *, dump_cselib_val
> (out
);
3545 if (first_containing_mem
!= &dummy_val
)
3547 fputs ("first mem ", out
);
3548 print_inline_rtx (out
, first_containing_mem
->val_rtx
, 2);
3551 fprintf (out
, "next uid %i\n", next_uid
);
3554 #include "gt-cselib.h"