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 "tree.h"/* FIXME: For hashing DEBUG_EXPR & friends. */
31 #include "hard-reg-set.h"
33 #include "insn-config.h"
38 #include "diagnostic-core.h"
40 #include "alloc-pool.h"
43 #include "basic-block.h"
46 #include "alloc-pool.h"
50 /* A list of cselib_val structures. */
53 struct elt_list
*next
;
56 /* Pool allocation new operator. */
57 inline void *operator new (size_t)
59 return pool
.allocate ();
62 /* Delete operator utilizing pool allocation. */
63 inline void operator delete (void *ptr
)
65 pool
.remove ((elt_list
*) ptr
);
68 /* Memory allocation pool. */
69 static pool_allocator
<elt_list
> pool
;
72 static bool cselib_record_memory
;
73 static bool cselib_preserve_constants
;
74 static bool cselib_any_perm_equivs
;
75 static inline void promote_debug_loc (struct elt_loc_list
*l
);
76 static struct elt_list
*new_elt_list (struct elt_list
*, cselib_val
*);
77 static void new_elt_loc_list (cselib_val
*, rtx
);
78 static void unchain_one_value (cselib_val
*);
79 static void unchain_one_elt_list (struct elt_list
**);
80 static void unchain_one_elt_loc_list (struct elt_loc_list
**);
81 static void remove_useless_values (void);
82 static int rtx_equal_for_cselib_1 (rtx
, rtx
, machine_mode
);
83 static unsigned int cselib_hash_rtx (rtx
, int, machine_mode
);
84 static cselib_val
*new_cselib_val (unsigned int, machine_mode
, rtx
);
85 static void add_mem_for_addr (cselib_val
*, cselib_val
*, rtx
);
86 static cselib_val
*cselib_lookup_mem (rtx
, int);
87 static void cselib_invalidate_regno (unsigned int, machine_mode
);
88 static void cselib_invalidate_mem (rtx
);
89 static void cselib_record_set (rtx
, cselib_val
*, cselib_val
*);
90 static void cselib_record_sets (rtx_insn
*);
92 struct expand_value_data
95 cselib_expand_callback callback
;
100 static rtx
cselib_expand_value_rtx_1 (rtx
, struct expand_value_data
*, int);
102 /* There are three ways in which cselib can look up an rtx:
103 - for a REG, the reg_values table (which is indexed by regno) is used
104 - for a MEM, we recursively look up its address and then follow the
105 addr_list of that value
106 - for everything else, we compute a hash value and go through the hash
107 table. Since different rtx's can still have the same hash value,
108 this involves walking the table entries for a given value and comparing
109 the locations of the entries with the rtx we are looking up. */
111 struct cselib_hasher
: typed_noop_remove
<cselib_val
>
113 typedef cselib_val
*value_type
;
115 /* The rtx value and its mode (needed separately for constant
119 /* The mode of the contaning MEM, if any, otherwise VOIDmode. */
120 machine_mode memmode
;
122 typedef key
*compare_type
;
123 static inline hashval_t
hash (const cselib_val
*);
124 static inline bool equal (const cselib_val
*, const key
*);
127 /* The hash function for our hash table. The value is always computed with
128 cselib_hash_rtx when adding an element; this function just extracts the
129 hash value from a cselib_val structure. */
132 cselib_hasher::hash (const cselib_val
*v
)
137 /* The equality test for our hash table. The first argument V is a table
138 element (i.e. a cselib_val), while the second arg X is an rtx. We know
139 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
140 CONST of an appropriate mode. */
143 cselib_hasher::equal (const cselib_val
*v
, const key
*x_arg
)
145 struct elt_loc_list
*l
;
147 machine_mode mode
= x_arg
->mode
;
148 machine_mode memmode
= x_arg
->memmode
;
150 if (mode
!= GET_MODE (v
->val_rtx
))
153 if (GET_CODE (x
) == VALUE
)
154 return x
== v
->val_rtx
;
156 /* We don't guarantee that distinct rtx's have different hash values,
157 so we need to do a comparison. */
158 for (l
= v
->locs
; l
; l
= l
->next
)
159 if (rtx_equal_for_cselib_1 (l
->loc
, x
, memmode
))
161 promote_debug_loc (l
);
168 /* A table that enables us to look up elts by their value. */
169 static hash_table
<cselib_hasher
> *cselib_hash_table
;
171 /* A table to hold preserved values. */
172 static hash_table
<cselib_hasher
> *cselib_preserved_hash_table
;
174 /* This is a global so we don't have to pass this through every function.
175 It is used in new_elt_loc_list to set SETTING_INSN. */
176 static rtx_insn
*cselib_current_insn
;
178 /* The unique id that the next create value will take. */
179 static unsigned int next_uid
;
181 /* The number of registers we had when the varrays were last resized. */
182 static unsigned int cselib_nregs
;
184 /* Count values without known locations, or with only locations that
185 wouldn't have been known except for debug insns. Whenever this
186 grows too big, we remove these useless values from the table.
188 Counting values with only debug values is a bit tricky. We don't
189 want to increment n_useless_values when we create a value for a
190 debug insn, for this would get n_useless_values out of sync, but we
191 want increment it if all locs in the list that were ever referenced
192 in nondebug insns are removed from the list.
194 In the general case, once we do that, we'd have to stop accepting
195 nondebug expressions in the loc list, to avoid having two values
196 equivalent that, without debug insns, would have been made into
197 separate values. However, because debug insns never introduce
198 equivalences themselves (no assignments), the only means for
199 growing loc lists is through nondebug assignments. If the locs
200 also happen to be referenced in debug insns, it will work just fine.
202 A consequence of this is that there's at most one debug-only loc in
203 each loc list. If we keep it in the first entry, testing whether
204 we have a debug-only loc list takes O(1).
206 Furthermore, since any additional entry in a loc list containing a
207 debug loc would have to come from an assignment (nondebug) that
208 references both the initial debug loc and the newly-equivalent loc,
209 the initial debug loc would be promoted to a nondebug loc, and the
210 loc list would not contain debug locs any more.
212 So the only case we have to be careful with in order to keep
213 n_useless_values in sync between debug and nondebug compilations is
214 to avoid incrementing n_useless_values when removing the single loc
215 from a value that turns out to not appear outside debug values. We
216 increment n_useless_debug_values instead, and leave such values
217 alone until, for other reasons, we garbage-collect useless
219 static int n_useless_values
;
220 static int n_useless_debug_values
;
222 /* Count values whose locs have been taken exclusively from debug
223 insns for the entire life of the value. */
224 static int n_debug_values
;
226 /* Number of useless values before we remove them from the hash table. */
227 #define MAX_USELESS_VALUES 32
229 /* This table maps from register number to values. It does not
230 contain pointers to cselib_val structures, but rather elt_lists.
231 The purpose is to be able to refer to the same register in
232 different modes. The first element of the list defines the mode in
233 which the register was set; if the mode is unknown or the value is
234 no longer valid in that mode, ELT will be NULL for the first
236 static struct elt_list
**reg_values
;
237 static unsigned int reg_values_size
;
238 #define REG_VALUES(i) reg_values[i]
240 /* The largest number of hard regs used by any entry added to the
241 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
242 static unsigned int max_value_regs
;
244 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
245 in cselib_clear_table() for fast emptying. */
246 static unsigned int *used_regs
;
247 static unsigned int n_used_regs
;
249 /* We pass this to cselib_invalidate_mem to invalidate all of
250 memory for a non-const call instruction. */
251 static GTY(()) rtx callmem
;
253 /* Set by discard_useless_locs if it deleted the last location of any
255 static int values_became_useless
;
257 /* Used as stop element of the containing_mem list so we can check
258 presence in the list by checking the next pointer. */
259 static cselib_val dummy_val
;
261 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
262 that is constant through the whole function and should never be
264 static cselib_val
*cfa_base_preserved_val
;
265 static unsigned int cfa_base_preserved_regno
= INVALID_REGNUM
;
267 /* Used to list all values that contain memory reference.
268 May or may not contain the useless values - the list is compacted
269 each time memory is invalidated. */
270 static cselib_val
*first_containing_mem
= &dummy_val
;
272 pool_allocator
<elt_list
> elt_list::pool ("elt_list", 10);
273 pool_allocator
<elt_loc_list
> elt_loc_list::pool ("elt_loc_list", 10);
274 pool_allocator
<cselib_val
> cselib_val::pool ("cselib_val_list", 10);
276 static pool_allocator
<rtx_def
> value_pool ("value", 100, RTX_CODE_SIZE (VALUE
),
279 /* If nonnull, cselib will call this function before freeing useless
280 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
281 void (*cselib_discard_hook
) (cselib_val
*);
283 /* If nonnull, cselib will call this function before recording sets or
284 even clobbering outputs of INSN. All the recorded sets will be
285 represented in the array sets[n_sets]. new_val_min can be used to
286 tell whether values present in sets are introduced by this
288 void (*cselib_record_sets_hook
) (rtx_insn
*insn
, struct cselib_set
*sets
,
291 #define PRESERVED_VALUE_P(RTX) \
292 (RTL_FLAG_CHECK1 ("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
294 #define SP_BASED_VALUE_P(RTX) \
295 (RTL_FLAG_CHECK1 ("SP_BASED_VALUE_P", (RTX), VALUE)->jump)
299 /* Allocate a struct elt_list and fill in its two elements with the
302 static inline struct elt_list
*
303 new_elt_list (struct elt_list
*next
, cselib_val
*elt
)
305 elt_list
*el
= new elt_list ();
311 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
315 new_elt_loc_list (cselib_val
*val
, rtx loc
)
317 struct elt_loc_list
*el
, *next
= val
->locs
;
319 gcc_checking_assert (!next
|| !next
->setting_insn
320 || !DEBUG_INSN_P (next
->setting_insn
)
321 || cselib_current_insn
== next
->setting_insn
);
323 /* If we're creating the first loc in a debug insn context, we've
324 just created a debug value. Count it. */
325 if (!next
&& cselib_current_insn
&& DEBUG_INSN_P (cselib_current_insn
))
328 val
= canonical_cselib_val (val
);
331 if (GET_CODE (loc
) == VALUE
)
333 loc
= canonical_cselib_val (CSELIB_VAL_PTR (loc
))->val_rtx
;
335 gcc_checking_assert (PRESERVED_VALUE_P (loc
)
336 == PRESERVED_VALUE_P (val
->val_rtx
));
338 if (val
->val_rtx
== loc
)
340 else if (val
->uid
> CSELIB_VAL_PTR (loc
)->uid
)
342 /* Reverse the insertion. */
343 new_elt_loc_list (CSELIB_VAL_PTR (loc
), val
->val_rtx
);
347 gcc_checking_assert (val
->uid
< CSELIB_VAL_PTR (loc
)->uid
);
349 if (CSELIB_VAL_PTR (loc
)->locs
)
351 /* Bring all locs from LOC to VAL. */
352 for (el
= CSELIB_VAL_PTR (loc
)->locs
; el
->next
; el
= el
->next
)
354 /* Adjust values that have LOC as canonical so that VAL
355 becomes their canonical. */
356 if (el
->loc
&& GET_CODE (el
->loc
) == VALUE
)
358 gcc_checking_assert (CSELIB_VAL_PTR (el
->loc
)->locs
->loc
360 CSELIB_VAL_PTR (el
->loc
)->locs
->loc
= val
->val_rtx
;
363 el
->next
= val
->locs
;
364 next
= val
->locs
= CSELIB_VAL_PTR (loc
)->locs
;
367 if (CSELIB_VAL_PTR (loc
)->addr_list
)
369 /* Bring in addr_list into canonical node. */
370 struct elt_list
*last
= CSELIB_VAL_PTR (loc
)->addr_list
;
373 last
->next
= val
->addr_list
;
374 val
->addr_list
= CSELIB_VAL_PTR (loc
)->addr_list
;
375 CSELIB_VAL_PTR (loc
)->addr_list
= NULL
;
378 if (CSELIB_VAL_PTR (loc
)->next_containing_mem
!= NULL
379 && val
->next_containing_mem
== NULL
)
381 /* Add VAL to the containing_mem list after LOC. LOC will
382 be removed when we notice it doesn't contain any
384 val
->next_containing_mem
= CSELIB_VAL_PTR (loc
)->next_containing_mem
;
385 CSELIB_VAL_PTR (loc
)->next_containing_mem
= val
;
388 /* Chain LOC back to VAL. */
389 el
= new elt_loc_list
;
390 el
->loc
= val
->val_rtx
;
391 el
->setting_insn
= cselib_current_insn
;
393 CSELIB_VAL_PTR (loc
)->locs
= el
;
396 el
= new elt_loc_list
;
398 el
->setting_insn
= cselib_current_insn
;
403 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
404 originating from a debug insn, maintaining the debug values
408 promote_debug_loc (struct elt_loc_list
*l
)
410 if (l
&& l
->setting_insn
&& DEBUG_INSN_P (l
->setting_insn
)
411 && (!cselib_current_insn
|| !DEBUG_INSN_P (cselib_current_insn
)))
414 l
->setting_insn
= cselib_current_insn
;
415 if (cselib_preserve_constants
&& l
->next
)
417 gcc_assert (l
->next
->setting_insn
418 && DEBUG_INSN_P (l
->next
->setting_insn
)
420 l
->next
->setting_insn
= cselib_current_insn
;
423 gcc_assert (!l
->next
);
427 /* The elt_list at *PL is no longer needed. Unchain it and free its
431 unchain_one_elt_list (struct elt_list
**pl
)
433 struct elt_list
*l
= *pl
;
439 /* Likewise for elt_loc_lists. */
442 unchain_one_elt_loc_list (struct elt_loc_list
**pl
)
444 struct elt_loc_list
*l
= *pl
;
450 /* Likewise for cselib_vals. This also frees the addr_list associated with
454 unchain_one_value (cselib_val
*v
)
457 unchain_one_elt_list (&v
->addr_list
);
462 /* Remove all entries from the hash table. Also used during
466 cselib_clear_table (void)
468 cselib_reset_table (1);
471 /* Return TRUE if V is a constant, a function invariant or a VALUE
472 equivalence; FALSE otherwise. */
475 invariant_or_equiv_p (cselib_val
*v
)
477 struct elt_loc_list
*l
;
479 if (v
== cfa_base_preserved_val
)
482 /* Keep VALUE equivalences around. */
483 for (l
= v
->locs
; l
; l
= l
->next
)
484 if (GET_CODE (l
->loc
) == VALUE
)
488 && v
->locs
->next
== NULL
)
490 if (CONSTANT_P (v
->locs
->loc
)
491 && (GET_CODE (v
->locs
->loc
) != CONST
492 || !references_value_p (v
->locs
->loc
, 0)))
494 /* Although a debug expr may be bound to different expressions,
495 we can preserve it as if it was constant, to get unification
496 and proper merging within var-tracking. */
497 if (GET_CODE (v
->locs
->loc
) == DEBUG_EXPR
498 || GET_CODE (v
->locs
->loc
) == DEBUG_IMPLICIT_PTR
499 || GET_CODE (v
->locs
->loc
) == ENTRY_VALUE
500 || GET_CODE (v
->locs
->loc
) == DEBUG_PARAMETER_REF
)
503 /* (plus (value V) (const_int C)) is invariant iff V is invariant. */
504 if (GET_CODE (v
->locs
->loc
) == PLUS
505 && CONST_INT_P (XEXP (v
->locs
->loc
, 1))
506 && GET_CODE (XEXP (v
->locs
->loc
, 0)) == VALUE
507 && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v
->locs
->loc
, 0))))
514 /* Remove from hash table all VALUEs except constants, function
515 invariants and VALUE equivalences. */
518 preserve_constants_and_equivs (cselib_val
**x
, void *info ATTRIBUTE_UNUSED
)
522 if (invariant_or_equiv_p (v
))
524 cselib_hasher::key lookup
= {
525 GET_MODE (v
->val_rtx
), v
->val_rtx
, VOIDmode
528 = cselib_preserved_hash_table
->find_slot_with_hash (&lookup
,
534 cselib_hash_table
->clear_slot (x
);
539 /* Remove all entries from the hash table, arranging for the next
540 value to be numbered NUM. */
543 cselib_reset_table (unsigned int num
)
549 if (cfa_base_preserved_val
)
551 unsigned int regno
= cfa_base_preserved_regno
;
552 unsigned int new_used_regs
= 0;
553 for (i
= 0; i
< n_used_regs
; i
++)
554 if (used_regs
[i
] == regno
)
560 REG_VALUES (used_regs
[i
]) = 0;
561 gcc_assert (new_used_regs
== 1);
562 n_used_regs
= new_used_regs
;
563 used_regs
[0] = regno
;
565 = hard_regno_nregs
[regno
][GET_MODE (cfa_base_preserved_val
->locs
->loc
)];
569 for (i
= 0; i
< n_used_regs
; i
++)
570 REG_VALUES (used_regs
[i
]) = 0;
574 if (cselib_preserve_constants
)
575 cselib_hash_table
->traverse
<void *, preserve_constants_and_equivs
>
579 cselib_hash_table
->empty ();
580 gcc_checking_assert (!cselib_any_perm_equivs
);
583 n_useless_values
= 0;
584 n_useless_debug_values
= 0;
589 first_containing_mem
= &dummy_val
;
592 /* Return the number of the next value that will be generated. */
595 cselib_get_next_uid (void)
600 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
601 INSERTing if requested. When X is part of the address of a MEM,
602 MEMMODE should specify the mode of the MEM. */
605 cselib_find_slot (machine_mode mode
, rtx x
, hashval_t hash
,
606 enum insert_option insert
, machine_mode memmode
)
608 cselib_val
**slot
= NULL
;
609 cselib_hasher::key lookup
= { mode
, x
, memmode
};
610 if (cselib_preserve_constants
)
611 slot
= cselib_preserved_hash_table
->find_slot_with_hash (&lookup
, hash
,
614 slot
= cselib_hash_table
->find_slot_with_hash (&lookup
, hash
, insert
);
618 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
619 only return true for values which point to a cselib_val whose value
620 element has been set to zero, which implies the cselib_val will be
624 references_value_p (const_rtx x
, int only_useless
)
626 const enum rtx_code code
= GET_CODE (x
);
627 const char *fmt
= GET_RTX_FORMAT (code
);
630 if (GET_CODE (x
) == VALUE
631 && (! only_useless
||
632 (CSELIB_VAL_PTR (x
)->locs
== 0 && !PRESERVED_VALUE_P (x
))))
635 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
637 if (fmt
[i
] == 'e' && references_value_p (XEXP (x
, i
), only_useless
))
639 else if (fmt
[i
] == 'E')
640 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
641 if (references_value_p (XVECEXP (x
, i
, j
), only_useless
))
648 /* For all locations found in X, delete locations that reference useless
649 values (i.e. values without any location). Called through
653 discard_useless_locs (cselib_val
**x
, void *info ATTRIBUTE_UNUSED
)
656 struct elt_loc_list
**p
= &v
->locs
;
657 bool had_locs
= v
->locs
!= NULL
;
658 rtx_insn
*setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
662 if (references_value_p ((*p
)->loc
, 1))
663 unchain_one_elt_loc_list (p
);
668 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
670 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
671 n_useless_debug_values
++;
674 values_became_useless
= 1;
679 /* If X is a value with no locations, remove it from the hashtable. */
682 discard_useless_values (cselib_val
**x
, void *info ATTRIBUTE_UNUSED
)
686 if (v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
688 if (cselib_discard_hook
)
689 cselib_discard_hook (v
);
691 CSELIB_VAL_PTR (v
->val_rtx
) = NULL
;
692 cselib_hash_table
->clear_slot (x
);
693 unchain_one_value (v
);
700 /* Clean out useless values (i.e. those which no longer have locations
701 associated with them) from the hash table. */
704 remove_useless_values (void)
708 /* First pass: eliminate locations that reference the value. That in
709 turn can make more values useless. */
712 values_became_useless
= 0;
713 cselib_hash_table
->traverse
<void *, discard_useless_locs
> (NULL
);
715 while (values_became_useless
);
717 /* Second pass: actually remove the values. */
719 p
= &first_containing_mem
;
720 for (v
= *p
; v
!= &dummy_val
; v
= v
->next_containing_mem
)
721 if (v
->locs
&& v
== canonical_cselib_val (v
))
724 p
= &(*p
)->next_containing_mem
;
728 n_useless_values
+= n_useless_debug_values
;
729 n_debug_values
-= n_useless_debug_values
;
730 n_useless_debug_values
= 0;
732 cselib_hash_table
->traverse
<void *, discard_useless_values
> (NULL
);
734 gcc_assert (!n_useless_values
);
737 /* Arrange for a value to not be removed from the hash table even if
738 it becomes useless. */
741 cselib_preserve_value (cselib_val
*v
)
743 PRESERVED_VALUE_P (v
->val_rtx
) = 1;
746 /* Test whether a value is preserved. */
749 cselib_preserved_value_p (cselib_val
*v
)
751 return PRESERVED_VALUE_P (v
->val_rtx
);
754 /* Arrange for a REG value to be assumed constant through the whole function,
755 never invalidated and preserved across cselib_reset_table calls. */
758 cselib_preserve_cfa_base_value (cselib_val
*v
, unsigned int regno
)
760 if (cselib_preserve_constants
762 && REG_P (v
->locs
->loc
))
764 cfa_base_preserved_val
= v
;
765 cfa_base_preserved_regno
= regno
;
769 /* Clean all non-constant expressions in the hash table, but retain
773 cselib_preserve_only_values (void)
777 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
778 cselib_invalidate_regno (i
, reg_raw_mode
[i
]);
780 cselib_invalidate_mem (callmem
);
782 remove_useless_values ();
784 gcc_assert (first_containing_mem
== &dummy_val
);
787 /* Arrange for a value to be marked as based on stack pointer
788 for find_base_term purposes. */
791 cselib_set_value_sp_based (cselib_val
*v
)
793 SP_BASED_VALUE_P (v
->val_rtx
) = 1;
796 /* Test whether a value is based on stack pointer for
797 find_base_term purposes. */
800 cselib_sp_based_value_p (cselib_val
*v
)
802 return SP_BASED_VALUE_P (v
->val_rtx
);
805 /* Return the mode in which a register was last set. If X is not a
806 register, return its mode. If the mode in which the register was
807 set is not known, or the value was already clobbered, return
811 cselib_reg_set_mode (const_rtx x
)
816 if (REG_VALUES (REGNO (x
)) == NULL
817 || REG_VALUES (REGNO (x
))->elt
== NULL
)
820 return GET_MODE (REG_VALUES (REGNO (x
))->elt
->val_rtx
);
823 /* Return nonzero if we can prove that X and Y contain the same value, taking
824 our gathered information into account. */
827 rtx_equal_for_cselib_p (rtx x
, rtx y
)
829 return rtx_equal_for_cselib_1 (x
, y
, VOIDmode
);
832 /* If x is a PLUS or an autoinc operation, expand the operation,
833 storing the offset, if any, in *OFF. */
836 autoinc_split (rtx x
, rtx
*off
, machine_mode memmode
)
838 switch (GET_CODE (x
))
845 if (memmode
== VOIDmode
)
848 *off
= GEN_INT (-GET_MODE_SIZE (memmode
));
853 if (memmode
== VOIDmode
)
856 *off
= GEN_INT (GET_MODE_SIZE (memmode
));
872 /* Return nonzero if we can prove that X and Y contain the same value,
873 taking our gathered information into account. MEMMODE holds the
874 mode of the enclosing MEM, if any, as required to deal with autoinc
875 addressing modes. If X and Y are not (known to be) part of
876 addresses, MEMMODE should be VOIDmode. */
879 rtx_equal_for_cselib_1 (rtx x
, rtx y
, machine_mode memmode
)
885 if (REG_P (x
) || MEM_P (x
))
887 cselib_val
*e
= cselib_lookup (x
, GET_MODE (x
), 0, memmode
);
893 if (REG_P (y
) || MEM_P (y
))
895 cselib_val
*e
= cselib_lookup (y
, GET_MODE (y
), 0, memmode
);
904 if (GET_CODE (x
) == VALUE
)
906 cselib_val
*e
= canonical_cselib_val (CSELIB_VAL_PTR (x
));
907 struct elt_loc_list
*l
;
909 if (GET_CODE (y
) == VALUE
)
910 return e
== canonical_cselib_val (CSELIB_VAL_PTR (y
));
912 for (l
= e
->locs
; l
; l
= l
->next
)
916 /* Avoid infinite recursion. We know we have the canonical
917 value, so we can just skip any values in the equivalence
919 if (REG_P (t
) || MEM_P (t
) || GET_CODE (t
) == VALUE
)
921 else if (rtx_equal_for_cselib_1 (t
, y
, memmode
))
927 else if (GET_CODE (y
) == VALUE
)
929 cselib_val
*e
= canonical_cselib_val (CSELIB_VAL_PTR (y
));
930 struct elt_loc_list
*l
;
932 for (l
= e
->locs
; l
; l
= l
->next
)
936 if (REG_P (t
) || MEM_P (t
) || GET_CODE (t
) == VALUE
)
938 else if (rtx_equal_for_cselib_1 (x
, t
, memmode
))
945 if (GET_MODE (x
) != GET_MODE (y
))
948 if (GET_CODE (x
) != GET_CODE (y
))
950 rtx xorig
= x
, yorig
= y
;
951 rtx xoff
= NULL
, yoff
= NULL
;
953 x
= autoinc_split (x
, &xoff
, memmode
);
954 y
= autoinc_split (y
, &yoff
, memmode
);
959 if (xoff
&& !rtx_equal_for_cselib_1 (xoff
, yoff
, memmode
))
962 /* Don't recurse if nothing changed. */
963 if (x
!= xorig
|| y
!= yorig
)
964 return rtx_equal_for_cselib_1 (x
, y
, memmode
);
969 /* These won't be handled correctly by the code below. */
970 switch (GET_CODE (x
))
976 case DEBUG_IMPLICIT_PTR
:
977 return DEBUG_IMPLICIT_PTR_DECL (x
)
978 == DEBUG_IMPLICIT_PTR_DECL (y
);
980 case DEBUG_PARAMETER_REF
:
981 return DEBUG_PARAMETER_REF_DECL (x
)
982 == DEBUG_PARAMETER_REF_DECL (y
);
985 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
986 use rtx_equal_for_cselib_1 to compare the operands. */
987 return rtx_equal_p (ENTRY_VALUE_EXP (x
), ENTRY_VALUE_EXP (y
));
990 return LABEL_REF_LABEL (x
) == LABEL_REF_LABEL (y
);
993 return REGNO (x
) == REGNO (y
);
996 /* We have to compare any autoinc operations in the addresses
997 using this MEM's mode. */
998 return rtx_equal_for_cselib_1 (XEXP (x
, 0), XEXP (y
, 0), GET_MODE (x
));
1004 code
= GET_CODE (x
);
1005 fmt
= GET_RTX_FORMAT (code
);
1007 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1014 if (XWINT (x
, i
) != XWINT (y
, i
))
1020 if (XINT (x
, i
) != XINT (y
, i
))
1026 /* Two vectors must have the same length. */
1027 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
1030 /* And the corresponding elements must match. */
1031 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1032 if (! rtx_equal_for_cselib_1 (XVECEXP (x
, i
, j
),
1033 XVECEXP (y
, i
, j
), memmode
))
1039 && targetm
.commutative_p (x
, UNKNOWN
)
1040 && rtx_equal_for_cselib_1 (XEXP (x
, 1), XEXP (y
, 0), memmode
)
1041 && rtx_equal_for_cselib_1 (XEXP (x
, 0), XEXP (y
, 1), memmode
))
1043 if (! rtx_equal_for_cselib_1 (XEXP (x
, i
), XEXP (y
, i
), memmode
))
1049 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
1054 /* These are just backpointers, so they don't matter. */
1061 /* It is believed that rtx's at this level will never
1062 contain anything but integers and other rtx's,
1063 except for within LABEL_REFs and SYMBOL_REFs. */
1071 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1072 For registers and memory locations, we look up their cselib_val structure
1073 and return its VALUE element.
1074 Possible reasons for return 0 are: the object is volatile, or we couldn't
1075 find a register or memory location in the table and CREATE is zero. If
1076 CREATE is nonzero, table elts are created for regs and mem.
1077 N.B. this hash function returns the same hash value for RTXes that
1078 differ only in the order of operands, thus it is suitable for comparisons
1079 that take commutativity into account.
1080 If we wanted to also support associative rules, we'd have to use a different
1081 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1082 MEMMODE indicates the mode of an enclosing MEM, and it's only
1083 used to compute autoinc values.
1084 We used to have a MODE argument for hashing for CONST_INTs, but that
1085 didn't make sense, since it caused spurious hash differences between
1086 (set (reg:SI 1) (const_int))
1087 (plus:SI (reg:SI 2) (reg:SI 1))
1089 (plus:SI (reg:SI 2) (const_int))
1090 If the mode is important in any context, it must be checked specifically
1091 in a comparison anyway, since relying on hash differences is unsafe. */
1094 cselib_hash_rtx (rtx x
, int create
, machine_mode memmode
)
1100 unsigned int hash
= 0;
1102 code
= GET_CODE (x
);
1103 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
1108 e
= CSELIB_VAL_PTR (x
);
1113 e
= cselib_lookup (x
, GET_MODE (x
), create
, memmode
);
1120 hash
+= ((unsigned) DEBUG_EXPR
<< 7)
1121 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x
));
1122 return hash
? hash
: (unsigned int) DEBUG_EXPR
;
1124 case DEBUG_IMPLICIT_PTR
:
1125 hash
+= ((unsigned) DEBUG_IMPLICIT_PTR
<< 7)
1126 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x
));
1127 return hash
? hash
: (unsigned int) DEBUG_IMPLICIT_PTR
;
1129 case DEBUG_PARAMETER_REF
:
1130 hash
+= ((unsigned) DEBUG_PARAMETER_REF
<< 7)
1131 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x
));
1132 return hash
? hash
: (unsigned int) DEBUG_PARAMETER_REF
;
1135 /* ENTRY_VALUEs are function invariant, thus try to avoid
1136 recursing on argument if ENTRY_VALUE is one of the
1137 forms emitted by expand_debug_expr, otherwise
1138 ENTRY_VALUE hash would depend on the current value
1139 in some register or memory. */
1140 if (REG_P (ENTRY_VALUE_EXP (x
)))
1141 hash
+= (unsigned int) REG
1142 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x
))
1143 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x
));
1144 else if (MEM_P (ENTRY_VALUE_EXP (x
))
1145 && REG_P (XEXP (ENTRY_VALUE_EXP (x
), 0)))
1146 hash
+= (unsigned int) MEM
1147 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x
), 0))
1148 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x
), 0));
1150 hash
+= cselib_hash_rtx (ENTRY_VALUE_EXP (x
), create
, memmode
);
1151 return hash
? hash
: (unsigned int) ENTRY_VALUE
;
1154 hash
+= ((unsigned) CONST_INT
<< 7) + UINTVAL (x
);
1155 return hash
? hash
: (unsigned int) CONST_INT
;
1157 case CONST_WIDE_INT
:
1158 for (i
= 0; i
< CONST_WIDE_INT_NUNITS (x
); i
++)
1159 hash
+= CONST_WIDE_INT_ELT (x
, i
);
1163 /* This is like the general case, except that it only counts
1164 the integers representing the constant. */
1165 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
1166 if (TARGET_SUPPORTS_WIDE_INT
== 0 && GET_MODE (x
) == VOIDmode
)
1167 hash
+= ((unsigned) CONST_DOUBLE_LOW (x
)
1168 + (unsigned) CONST_DOUBLE_HIGH (x
));
1170 hash
+= real_hash (CONST_DOUBLE_REAL_VALUE (x
));
1171 return hash
? hash
: (unsigned int) CONST_DOUBLE
;
1174 hash
+= (unsigned int) code
+ (unsigned int) GET_MODE (x
);
1175 hash
+= fixed_hash (CONST_FIXED_VALUE (x
));
1176 return hash
? hash
: (unsigned int) CONST_FIXED
;
1183 units
= CONST_VECTOR_NUNITS (x
);
1185 for (i
= 0; i
< units
; ++i
)
1187 elt
= CONST_VECTOR_ELT (x
, i
);
1188 hash
+= cselib_hash_rtx (elt
, 0, memmode
);
1194 /* Assume there is only one rtx object for any given label. */
1196 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1197 differences and differences between each stage's debugging dumps. */
1198 hash
+= (((unsigned int) LABEL_REF
<< 7)
1199 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x
)));
1200 return hash
? hash
: (unsigned int) LABEL_REF
;
1204 /* Don't hash on the symbol's address to avoid bootstrap differences.
1205 Different hash values may cause expressions to be recorded in
1206 different orders and thus different registers to be used in the
1207 final assembler. This also avoids differences in the dump files
1208 between various stages. */
1210 const unsigned char *p
= (const unsigned char *) XSTR (x
, 0);
1213 h
+= (h
<< 7) + *p
++; /* ??? revisit */
1215 hash
+= ((unsigned int) SYMBOL_REF
<< 7) + h
;
1216 return hash
? hash
: (unsigned int) SYMBOL_REF
;
1221 /* We can't compute these without knowing the MEM mode. */
1222 gcc_assert (memmode
!= VOIDmode
);
1223 i
= GET_MODE_SIZE (memmode
);
1224 if (code
== PRE_DEC
)
1226 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1227 like (mem:MEMMODE (plus (reg) (const_int I))). */
1228 hash
+= (unsigned) PLUS
- (unsigned)code
1229 + cselib_hash_rtx (XEXP (x
, 0), create
, memmode
)
1230 + cselib_hash_rtx (GEN_INT (i
), create
, memmode
);
1231 return hash
? hash
: 1 + (unsigned) PLUS
;
1234 gcc_assert (memmode
!= VOIDmode
);
1235 return cselib_hash_rtx (XEXP (x
, 1), create
, memmode
);
1240 gcc_assert (memmode
!= VOIDmode
);
1241 return cselib_hash_rtx (XEXP (x
, 0), create
, memmode
);
1246 case UNSPEC_VOLATILE
:
1250 if (MEM_VOLATILE_P (x
))
1259 i
= GET_RTX_LENGTH (code
) - 1;
1260 fmt
= GET_RTX_FORMAT (code
);
1267 rtx tem
= XEXP (x
, i
);
1268 unsigned int tem_hash
= cselib_hash_rtx (tem
, create
, memmode
);
1277 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1279 unsigned int tem_hash
1280 = cselib_hash_rtx (XVECEXP (x
, i
, j
), create
, memmode
);
1291 const unsigned char *p
= (const unsigned char *) XSTR (x
, i
);
1300 hash
+= XINT (x
, i
);
1313 return hash
? hash
: 1 + (unsigned int) GET_CODE (x
);
1316 /* Create a new value structure for VALUE and initialize it. The mode of the
1319 static inline cselib_val
*
1320 new_cselib_val (unsigned int hash
, machine_mode mode
, rtx x
)
1322 cselib_val
*e
= new cselib_val
;
1325 gcc_assert (next_uid
);
1328 e
->uid
= next_uid
++;
1329 /* We use an alloc pool to allocate this RTL construct because it
1330 accounts for about 8% of the overall memory usage. We know
1331 precisely when we can have VALUE RTXen (when cselib is active)
1332 so we don't need to put them in garbage collected memory.
1333 ??? Why should a VALUE be an RTX in the first place? */
1334 e
->val_rtx
= value_pool
.allocate ();
1335 memset (e
->val_rtx
, 0, RTX_HDR_SIZE
);
1336 PUT_CODE (e
->val_rtx
, VALUE
);
1337 PUT_MODE (e
->val_rtx
, mode
);
1338 CSELIB_VAL_PTR (e
->val_rtx
) = e
;
1341 e
->next_containing_mem
= 0;
1343 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1345 fprintf (dump_file
, "cselib value %u:%u ", e
->uid
, hash
);
1346 if (flag_dump_noaddr
|| flag_dump_unnumbered
)
1347 fputs ("# ", dump_file
);
1349 fprintf (dump_file
, "%p ", (void*)e
);
1350 print_rtl_single (dump_file
, x
);
1351 fputc ('\n', dump_file
);
1357 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1358 contains the data at this address. X is a MEM that represents the
1359 value. Update the two value structures to represent this situation. */
1362 add_mem_for_addr (cselib_val
*addr_elt
, cselib_val
*mem_elt
, rtx x
)
1364 struct elt_loc_list
*l
;
1366 addr_elt
= canonical_cselib_val (addr_elt
);
1367 mem_elt
= canonical_cselib_val (mem_elt
);
1369 /* Avoid duplicates. */
1370 for (l
= mem_elt
->locs
; l
; l
= l
->next
)
1372 && CSELIB_VAL_PTR (XEXP (l
->loc
, 0)) == addr_elt
)
1374 promote_debug_loc (l
);
1378 addr_elt
->addr_list
= new_elt_list (addr_elt
->addr_list
, mem_elt
);
1379 new_elt_loc_list (mem_elt
,
1380 replace_equiv_address_nv (x
, addr_elt
->val_rtx
));
1381 if (mem_elt
->next_containing_mem
== NULL
)
1383 mem_elt
->next_containing_mem
= first_containing_mem
;
1384 first_containing_mem
= mem_elt
;
1388 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1389 If CREATE, make a new one if we haven't seen it before. */
1392 cselib_lookup_mem (rtx x
, int create
)
1394 machine_mode mode
= GET_MODE (x
);
1395 machine_mode addr_mode
;
1398 cselib_val
*mem_elt
;
1401 if (MEM_VOLATILE_P (x
) || mode
== BLKmode
1402 || !cselib_record_memory
1403 || (FLOAT_MODE_P (mode
) && flag_float_store
))
1406 addr_mode
= GET_MODE (XEXP (x
, 0));
1407 if (addr_mode
== VOIDmode
)
1410 /* Look up the value for the address. */
1411 addr
= cselib_lookup (XEXP (x
, 0), addr_mode
, create
, mode
);
1415 addr
= canonical_cselib_val (addr
);
1416 /* Find a value that describes a value of our mode at that address. */
1417 for (l
= addr
->addr_list
; l
; l
= l
->next
)
1418 if (GET_MODE (l
->elt
->val_rtx
) == mode
)
1420 promote_debug_loc (l
->elt
->locs
);
1427 mem_elt
= new_cselib_val (next_uid
, mode
, x
);
1428 add_mem_for_addr (addr
, mem_elt
, x
);
1429 slot
= cselib_find_slot (mode
, x
, mem_elt
->hash
, INSERT
, VOIDmode
);
1434 /* Search through the possible substitutions in P. We prefer a non reg
1435 substitution because this allows us to expand the tree further. If
1436 we find, just a reg, take the lowest regno. There may be several
1437 non-reg results, we just take the first one because they will all
1438 expand to the same place. */
1441 expand_loc (struct elt_loc_list
*p
, struct expand_value_data
*evd
,
1444 rtx reg_result
= NULL
;
1445 unsigned int regno
= UINT_MAX
;
1446 struct elt_loc_list
*p_in
= p
;
1448 for (; p
; p
= p
->next
)
1450 /* Return these right away to avoid returning stack pointer based
1451 expressions for frame pointer and vice versa, which is something
1452 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1453 for more details. */
1455 && (REGNO (p
->loc
) == STACK_POINTER_REGNUM
1456 || REGNO (p
->loc
) == FRAME_POINTER_REGNUM
1457 || REGNO (p
->loc
) == HARD_FRAME_POINTER_REGNUM
1458 || REGNO (p
->loc
) == cfa_base_preserved_regno
))
1460 /* Avoid infinite recursion trying to expand a reg into a
1462 if ((REG_P (p
->loc
))
1463 && (REGNO (p
->loc
) < regno
)
1464 && !bitmap_bit_p (evd
->regs_active
, REGNO (p
->loc
)))
1466 reg_result
= p
->loc
;
1467 regno
= REGNO (p
->loc
);
1469 /* Avoid infinite recursion and do not try to expand the
1471 else if (GET_CODE (p
->loc
) == VALUE
1472 && CSELIB_VAL_PTR (p
->loc
)->locs
== p_in
)
1474 else if (!REG_P (p
->loc
))
1477 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1479 print_inline_rtx (dump_file
, p
->loc
, 0);
1480 fprintf (dump_file
, "\n");
1482 if (GET_CODE (p
->loc
) == LO_SUM
1483 && GET_CODE (XEXP (p
->loc
, 1)) == SYMBOL_REF
1485 && (note
= find_reg_note (p
->setting_insn
, REG_EQUAL
, NULL_RTX
))
1486 && XEXP (note
, 0) == XEXP (p
->loc
, 1))
1487 return XEXP (p
->loc
, 1);
1488 result
= cselib_expand_value_rtx_1 (p
->loc
, evd
, max_depth
- 1);
1495 if (regno
!= UINT_MAX
)
1498 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1499 fprintf (dump_file
, "r%d\n", regno
);
1501 result
= cselib_expand_value_rtx_1 (reg_result
, evd
, max_depth
- 1);
1506 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1510 print_inline_rtx (dump_file
, reg_result
, 0);
1511 fprintf (dump_file
, "\n");
1514 fprintf (dump_file
, "NULL\n");
1520 /* Forward substitute and expand an expression out to its roots.
1521 This is the opposite of common subexpression. Because local value
1522 numbering is such a weak optimization, the expanded expression is
1523 pretty much unique (not from a pointer equals point of view but
1524 from a tree shape point of view.
1526 This function returns NULL if the expansion fails. The expansion
1527 will fail if there is no value number for one of the operands or if
1528 one of the operands has been overwritten between the current insn
1529 and the beginning of the basic block. For instance x has no
1535 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1536 It is clear on return. */
1539 cselib_expand_value_rtx (rtx orig
, bitmap regs_active
, int max_depth
)
1541 struct expand_value_data evd
;
1543 evd
.regs_active
= regs_active
;
1544 evd
.callback
= NULL
;
1545 evd
.callback_arg
= NULL
;
1548 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
);
1551 /* Same as cselib_expand_value_rtx, but using a callback to try to
1552 resolve some expressions. The CB function should return ORIG if it
1553 can't or does not want to deal with a certain RTX. Any other
1554 return value, including NULL, will be used as the expansion for
1555 VALUE, without any further changes. */
1558 cselib_expand_value_rtx_cb (rtx orig
, bitmap regs_active
, int max_depth
,
1559 cselib_expand_callback cb
, void *data
)
1561 struct expand_value_data evd
;
1563 evd
.regs_active
= regs_active
;
1565 evd
.callback_arg
= data
;
1568 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
);
1571 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1572 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1573 would return NULL or non-NULL, without allocating new rtx. */
1576 cselib_dummy_expand_value_rtx_cb (rtx orig
, bitmap regs_active
, int max_depth
,
1577 cselib_expand_callback cb
, void *data
)
1579 struct expand_value_data evd
;
1581 evd
.regs_active
= regs_active
;
1583 evd
.callback_arg
= data
;
1586 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
) != NULL
;
1589 /* Internal implementation of cselib_expand_value_rtx and
1590 cselib_expand_value_rtx_cb. */
1593 cselib_expand_value_rtx_1 (rtx orig
, struct expand_value_data
*evd
,
1599 const char *format_ptr
;
1602 code
= GET_CODE (orig
);
1604 /* For the context of dse, if we end up expand into a huge tree, we
1605 will not have a useful address, so we might as well just give up
1614 struct elt_list
*l
= REG_VALUES (REGNO (orig
));
1616 if (l
&& l
->elt
== NULL
)
1618 for (; l
; l
= l
->next
)
1619 if (GET_MODE (l
->elt
->val_rtx
) == GET_MODE (orig
))
1622 unsigned regno
= REGNO (orig
);
1624 /* The only thing that we are not willing to do (this
1625 is requirement of dse and if others potential uses
1626 need this function we should add a parm to control
1627 it) is that we will not substitute the
1628 STACK_POINTER_REGNUM, FRAME_POINTER or the
1631 These expansions confuses the code that notices that
1632 stores into the frame go dead at the end of the
1633 function and that the frame is not effected by calls
1634 to subroutines. If you allow the
1635 STACK_POINTER_REGNUM substitution, then dse will
1636 think that parameter pushing also goes dead which is
1637 wrong. If you allow the FRAME_POINTER or the
1638 HARD_FRAME_POINTER then you lose the opportunity to
1639 make the frame assumptions. */
1640 if (regno
== STACK_POINTER_REGNUM
1641 || regno
== FRAME_POINTER_REGNUM
1642 || regno
== HARD_FRAME_POINTER_REGNUM
1643 || regno
== cfa_base_preserved_regno
)
1646 bitmap_set_bit (evd
->regs_active
, regno
);
1648 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1649 fprintf (dump_file
, "expanding: r%d into: ", regno
);
1651 result
= expand_loc (l
->elt
->locs
, evd
, max_depth
);
1652 bitmap_clear_bit (evd
->regs_active
, regno
);
1667 /* SCRATCH must be shared because they represent distinct values. */
1670 if (REG_P (XEXP (orig
, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig
, 0))))
1675 if (shared_const_p (orig
))
1685 subreg
= evd
->callback (orig
, evd
->regs_active
, max_depth
,
1691 subreg
= cselib_expand_value_rtx_1 (SUBREG_REG (orig
), evd
,
1695 scopy
= simplify_gen_subreg (GET_MODE (orig
), subreg
,
1696 GET_MODE (SUBREG_REG (orig
)),
1697 SUBREG_BYTE (orig
));
1699 || (GET_CODE (scopy
) == SUBREG
1700 && !REG_P (SUBREG_REG (scopy
))
1701 && !MEM_P (SUBREG_REG (scopy
))))
1711 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1713 fputs ("\nexpanding ", dump_file
);
1714 print_rtl_single (dump_file
, orig
);
1715 fputs (" into...", dump_file
);
1720 result
= evd
->callback (orig
, evd
->regs_active
, max_depth
,
1727 result
= expand_loc (CSELIB_VAL_PTR (orig
)->locs
, evd
, max_depth
);
1733 return evd
->callback (orig
, evd
->regs_active
, max_depth
,
1741 /* Copy the various flags, fields, and other information. We assume
1742 that all fields need copying, and then clear the fields that should
1743 not be copied. That is the sensible default behavior, and forces
1744 us to explicitly document why we are *not* copying a flag. */
1748 copy
= shallow_copy_rtx (orig
);
1750 format_ptr
= GET_RTX_FORMAT (code
);
1752 for (i
= 0; i
< GET_RTX_LENGTH (code
); i
++)
1753 switch (*format_ptr
++)
1756 if (XEXP (orig
, i
) != NULL
)
1758 rtx result
= cselib_expand_value_rtx_1 (XEXP (orig
, i
), evd
,
1763 XEXP (copy
, i
) = result
;
1769 if (XVEC (orig
, i
) != NULL
)
1772 XVEC (copy
, i
) = rtvec_alloc (XVECLEN (orig
, i
));
1773 for (j
= 0; j
< XVECLEN (orig
, i
); j
++)
1775 rtx result
= cselib_expand_value_rtx_1 (XVECEXP (orig
, i
, j
),
1776 evd
, max_depth
- 1);
1780 XVECEXP (copy
, i
, j
) = result
;
1794 /* These are left unchanged. */
1804 mode
= GET_MODE (copy
);
1805 /* If an operand has been simplified into CONST_INT, which doesn't
1806 have a mode and the mode isn't derivable from whole rtx's mode,
1807 try simplify_*_operation first with mode from original's operand
1808 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1810 switch (GET_RTX_CLASS (code
))
1813 if (CONST_INT_P (XEXP (copy
, 0))
1814 && GET_MODE (XEXP (orig
, 0)) != VOIDmode
)
1816 scopy
= simplify_unary_operation (code
, mode
, XEXP (copy
, 0),
1817 GET_MODE (XEXP (orig
, 0)));
1822 case RTX_COMM_ARITH
:
1824 /* These expressions can derive operand modes from the whole rtx's mode. */
1827 case RTX_BITFIELD_OPS
:
1828 if (CONST_INT_P (XEXP (copy
, 0))
1829 && GET_MODE (XEXP (orig
, 0)) != VOIDmode
)
1831 scopy
= simplify_ternary_operation (code
, mode
,
1832 GET_MODE (XEXP (orig
, 0)),
1833 XEXP (copy
, 0), XEXP (copy
, 1),
1840 case RTX_COMM_COMPARE
:
1841 if (CONST_INT_P (XEXP (copy
, 0))
1842 && GET_MODE (XEXP (copy
, 1)) == VOIDmode
1843 && (GET_MODE (XEXP (orig
, 0)) != VOIDmode
1844 || GET_MODE (XEXP (orig
, 1)) != VOIDmode
))
1846 scopy
= simplify_relational_operation (code
, mode
,
1847 (GET_MODE (XEXP (orig
, 0))
1849 ? GET_MODE (XEXP (orig
, 0))
1850 : GET_MODE (XEXP (orig
, 1)),
1860 scopy
= simplify_rtx (copy
);
1866 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1867 with VALUE expressions. This way, it becomes independent of changes
1868 to registers and memory.
1869 X isn't actually modified; if modifications are needed, new rtl is
1870 allocated. However, the return value can share rtl with X.
1871 If X is within a MEM, MEMMODE must be the mode of the MEM. */
1874 cselib_subst_to_values (rtx x
, machine_mode memmode
)
1876 enum rtx_code code
= GET_CODE (x
);
1877 const char *fmt
= GET_RTX_FORMAT (code
);
1886 l
= REG_VALUES (REGNO (x
));
1887 if (l
&& l
->elt
== NULL
)
1889 for (; l
; l
= l
->next
)
1890 if (GET_MODE (l
->elt
->val_rtx
) == GET_MODE (x
))
1891 return l
->elt
->val_rtx
;
1896 e
= cselib_lookup_mem (x
, 0);
1897 /* This used to happen for autoincrements, but we deal with them
1898 properly now. Remove the if stmt for the next release. */
1901 /* Assign a value that doesn't match any other. */
1902 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
1907 e
= cselib_lookup (x
, GET_MODE (x
), 0, memmode
);
1917 gcc_assert (memmode
!= VOIDmode
);
1918 i
= GET_MODE_SIZE (memmode
);
1919 if (code
== PRE_DEC
)
1921 return cselib_subst_to_values (plus_constant (GET_MODE (x
),
1926 gcc_assert (memmode
!= VOIDmode
);
1927 return cselib_subst_to_values (XEXP (x
, 1), memmode
);
1932 gcc_assert (memmode
!= VOIDmode
);
1933 return cselib_subst_to_values (XEXP (x
, 0), memmode
);
1939 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1943 rtx t
= cselib_subst_to_values (XEXP (x
, i
), memmode
);
1945 if (t
!= XEXP (x
, i
))
1948 copy
= shallow_copy_rtx (x
);
1952 else if (fmt
[i
] == 'E')
1956 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1958 rtx t
= cselib_subst_to_values (XVECEXP (x
, i
, j
), memmode
);
1960 if (t
!= XVECEXP (x
, i
, j
))
1962 if (XVEC (x
, i
) == XVEC (copy
, i
))
1965 copy
= shallow_copy_rtx (x
);
1966 XVEC (copy
, i
) = shallow_copy_rtvec (XVEC (x
, i
));
1968 XVECEXP (copy
, i
, j
) = t
;
1977 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
1980 cselib_subst_to_values_from_insn (rtx x
, machine_mode memmode
, rtx_insn
*insn
)
1983 gcc_assert (!cselib_current_insn
);
1984 cselib_current_insn
= insn
;
1985 ret
= cselib_subst_to_values (x
, memmode
);
1986 cselib_current_insn
= NULL
;
1990 /* Look up the rtl expression X in our tables and return the value it
1991 has. If CREATE is zero, we return NULL if we don't know the value.
1992 Otherwise, we create a new one if possible, using mode MODE if X
1993 doesn't have a mode (i.e. because it's a constant). When X is part
1994 of an address, MEMMODE should be the mode of the enclosing MEM if
1995 we're tracking autoinc expressions. */
1998 cselib_lookup_1 (rtx x
, machine_mode mode
,
1999 int create
, machine_mode memmode
)
2003 unsigned int hashval
;
2005 if (GET_MODE (x
) != VOIDmode
)
2006 mode
= GET_MODE (x
);
2008 if (GET_CODE (x
) == VALUE
)
2009 return CSELIB_VAL_PTR (x
);
2014 unsigned int i
= REGNO (x
);
2017 if (l
&& l
->elt
== NULL
)
2019 for (; l
; l
= l
->next
)
2020 if (mode
== GET_MODE (l
->elt
->val_rtx
))
2022 promote_debug_loc (l
->elt
->locs
);
2029 if (i
< FIRST_PSEUDO_REGISTER
)
2031 unsigned int n
= hard_regno_nregs
[i
][mode
];
2033 if (n
> max_value_regs
)
2037 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
2038 new_elt_loc_list (e
, x
);
2039 if (REG_VALUES (i
) == 0)
2041 /* Maintain the invariant that the first entry of
2042 REG_VALUES, if present, must be the value used to set the
2043 register, or NULL. */
2044 used_regs
[n_used_regs
++] = i
;
2045 REG_VALUES (i
) = new_elt_list (REG_VALUES (i
), NULL
);
2047 else if (cselib_preserve_constants
2048 && GET_MODE_CLASS (mode
) == MODE_INT
)
2050 /* During var-tracking, try harder to find equivalences
2051 for SUBREGs. If a setter sets say a DImode register
2052 and user uses that register only in SImode, add a lowpart
2054 struct elt_list
*lwider
= NULL
;
2056 if (l
&& l
->elt
== NULL
)
2058 for (; l
; l
= l
->next
)
2059 if (GET_MODE_CLASS (GET_MODE (l
->elt
->val_rtx
)) == MODE_INT
2060 && GET_MODE_SIZE (GET_MODE (l
->elt
->val_rtx
))
2061 > GET_MODE_SIZE (mode
)
2063 || GET_MODE_SIZE (GET_MODE (l
->elt
->val_rtx
))
2064 < GET_MODE_SIZE (GET_MODE (lwider
->elt
->val_rtx
))))
2066 struct elt_loc_list
*el
;
2067 if (i
< FIRST_PSEUDO_REGISTER
2068 && hard_regno_nregs
[i
][GET_MODE (l
->elt
->val_rtx
)] != 1)
2070 for (el
= l
->elt
->locs
; el
; el
= el
->next
)
2071 if (!REG_P (el
->loc
))
2078 rtx sub
= lowpart_subreg (mode
, lwider
->elt
->val_rtx
,
2079 GET_MODE (lwider
->elt
->val_rtx
));
2081 new_elt_loc_list (e
, sub
);
2084 REG_VALUES (i
)->next
= new_elt_list (REG_VALUES (i
)->next
, e
);
2085 slot
= cselib_find_slot (mode
, x
, e
->hash
, INSERT
, memmode
);
2091 return cselib_lookup_mem (x
, create
);
2093 hashval
= cselib_hash_rtx (x
, create
, memmode
);
2094 /* Can't even create if hashing is not possible. */
2098 slot
= cselib_find_slot (mode
, x
, hashval
,
2099 create
? INSERT
: NO_INSERT
, memmode
);
2103 e
= (cselib_val
*) *slot
;
2107 e
= new_cselib_val (hashval
, mode
, x
);
2109 /* We have to fill the slot before calling cselib_subst_to_values:
2110 the hash table is inconsistent until we do so, and
2111 cselib_subst_to_values will need to do lookups. */
2113 new_elt_loc_list (e
, cselib_subst_to_values (x
, memmode
));
2117 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2120 cselib_lookup_from_insn (rtx x
, machine_mode mode
,
2121 int create
, machine_mode memmode
, rtx_insn
*insn
)
2125 gcc_assert (!cselib_current_insn
);
2126 cselib_current_insn
= insn
;
2128 ret
= cselib_lookup (x
, mode
, create
, memmode
);
2130 cselib_current_insn
= NULL
;
2135 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2136 maintains invariants related with debug insns. */
2139 cselib_lookup (rtx x
, machine_mode mode
,
2140 int create
, machine_mode memmode
)
2142 cselib_val
*ret
= cselib_lookup_1 (x
, mode
, create
, memmode
);
2144 /* ??? Should we return NULL if we're not to create an entry, the
2145 found loc is a debug loc and cselib_current_insn is not DEBUG?
2146 If so, we should also avoid converting val to non-DEBUG; probably
2147 easiest setting cselib_current_insn to NULL before the call
2150 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
2152 fputs ("cselib lookup ", dump_file
);
2153 print_inline_rtx (dump_file
, x
, 2);
2154 fprintf (dump_file
, " => %u:%u\n",
2156 ret
? ret
->hash
: 0);
2162 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2163 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2164 is used to determine how many hard registers are being changed. If MODE
2165 is VOIDmode, then only REGNO is being changed; this is used when
2166 invalidating call clobbered registers across a call. */
2169 cselib_invalidate_regno (unsigned int regno
, machine_mode mode
)
2171 unsigned int endregno
;
2174 /* If we see pseudos after reload, something is _wrong_. */
2175 gcc_assert (!reload_completed
|| regno
< FIRST_PSEUDO_REGISTER
2176 || reg_renumber
[regno
] < 0);
2178 /* Determine the range of registers that must be invalidated. For
2179 pseudos, only REGNO is affected. For hard regs, we must take MODE
2180 into account, and we must also invalidate lower register numbers
2181 if they contain values that overlap REGNO. */
2182 if (regno
< FIRST_PSEUDO_REGISTER
)
2184 gcc_assert (mode
!= VOIDmode
);
2186 if (regno
< max_value_regs
)
2189 i
= regno
- max_value_regs
;
2191 endregno
= end_hard_regno (mode
, regno
);
2196 endregno
= regno
+ 1;
2199 for (; i
< endregno
; i
++)
2201 struct elt_list
**l
= ®_VALUES (i
);
2203 /* Go through all known values for this reg; if it overlaps the range
2204 we're invalidating, remove the value. */
2207 cselib_val
*v
= (*l
)->elt
;
2209 rtx_insn
*setting_insn
;
2210 struct elt_loc_list
**p
;
2211 unsigned int this_last
= i
;
2213 if (i
< FIRST_PSEUDO_REGISTER
&& v
!= NULL
)
2214 this_last
= end_hard_regno (GET_MODE (v
->val_rtx
), i
) - 1;
2216 if (this_last
< regno
|| v
== NULL
2217 || (v
== cfa_base_preserved_val
2218 && i
== cfa_base_preserved_regno
))
2224 /* We have an overlap. */
2225 if (*l
== REG_VALUES (i
))
2227 /* Maintain the invariant that the first entry of
2228 REG_VALUES, if present, must be the value used to set
2229 the register, or NULL. This is also nice because
2230 then we won't push the same regno onto user_regs
2236 unchain_one_elt_list (l
);
2238 v
= canonical_cselib_val (v
);
2240 had_locs
= v
->locs
!= NULL
;
2241 setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
2243 /* Now, we clear the mapping from value to reg. It must exist, so
2244 this code will crash intentionally if it doesn't. */
2245 for (p
= &v
->locs
; ; p
= &(*p
)->next
)
2249 if (REG_P (x
) && REGNO (x
) == i
)
2251 unchain_one_elt_loc_list (p
);
2256 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
2258 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
2259 n_useless_debug_values
++;
2267 /* Invalidate any locations in the table which are changed because of a
2268 store to MEM_RTX. If this is called because of a non-const call
2269 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2272 cselib_invalidate_mem (rtx mem_rtx
)
2274 cselib_val
**vp
, *v
, *next
;
2278 mem_addr
= canon_rtx (get_addr (XEXP (mem_rtx
, 0)));
2279 mem_rtx
= canon_rtx (mem_rtx
);
2281 vp
= &first_containing_mem
;
2282 for (v
= *vp
; v
!= &dummy_val
; v
= next
)
2284 bool has_mem
= false;
2285 struct elt_loc_list
**p
= &v
->locs
;
2286 bool had_locs
= v
->locs
!= NULL
;
2287 rtx_insn
*setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
2293 struct elt_list
**mem_chain
;
2295 /* MEMs may occur in locations only at the top level; below
2296 that every MEM or REG is substituted by its VALUE. */
2302 if (num_mems
< PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS
)
2303 && ! canon_anti_dependence (x
, false, mem_rtx
,
2304 GET_MODE (mem_rtx
), mem_addr
))
2312 /* This one overlaps. */
2313 /* We must have a mapping from this MEM's address to the
2314 value (E). Remove that, too. */
2315 addr
= cselib_lookup (XEXP (x
, 0), VOIDmode
, 0, GET_MODE (x
));
2316 addr
= canonical_cselib_val (addr
);
2317 gcc_checking_assert (v
== canonical_cselib_val (v
));
2318 mem_chain
= &addr
->addr_list
;
2321 cselib_val
*canon
= canonical_cselib_val ((*mem_chain
)->elt
);
2325 unchain_one_elt_list (mem_chain
);
2329 /* Record canonicalized elt. */
2330 (*mem_chain
)->elt
= canon
;
2332 mem_chain
= &(*mem_chain
)->next
;
2335 unchain_one_elt_loc_list (p
);
2338 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
2340 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
2341 n_useless_debug_values
++;
2346 next
= v
->next_containing_mem
;
2350 vp
= &(*vp
)->next_containing_mem
;
2353 v
->next_containing_mem
= NULL
;
2358 /* Invalidate DEST, which is being assigned to or clobbered. */
2361 cselib_invalidate_rtx (rtx dest
)
2363 while (GET_CODE (dest
) == SUBREG
2364 || GET_CODE (dest
) == ZERO_EXTRACT
2365 || GET_CODE (dest
) == STRICT_LOW_PART
)
2366 dest
= XEXP (dest
, 0);
2369 cselib_invalidate_regno (REGNO (dest
), GET_MODE (dest
));
2370 else if (MEM_P (dest
))
2371 cselib_invalidate_mem (dest
);
2374 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2377 cselib_invalidate_rtx_note_stores (rtx dest
, const_rtx ignore ATTRIBUTE_UNUSED
,
2378 void *data ATTRIBUTE_UNUSED
)
2380 cselib_invalidate_rtx (dest
);
2383 /* Record the result of a SET instruction. DEST is being set; the source
2384 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2385 describes its address. */
2388 cselib_record_set (rtx dest
, cselib_val
*src_elt
, cselib_val
*dest_addr_elt
)
2390 if (src_elt
== 0 || side_effects_p (dest
))
2395 unsigned int dreg
= REGNO (dest
);
2396 if (dreg
< FIRST_PSEUDO_REGISTER
)
2398 unsigned int n
= REG_NREGS (dest
);
2400 if (n
> max_value_regs
)
2404 if (REG_VALUES (dreg
) == 0)
2406 used_regs
[n_used_regs
++] = dreg
;
2407 REG_VALUES (dreg
) = new_elt_list (REG_VALUES (dreg
), src_elt
);
2411 /* The register should have been invalidated. */
2412 gcc_assert (REG_VALUES (dreg
)->elt
== 0);
2413 REG_VALUES (dreg
)->elt
= src_elt
;
2416 if (src_elt
->locs
== 0 && !PRESERVED_VALUE_P (src_elt
->val_rtx
))
2418 new_elt_loc_list (src_elt
, dest
);
2420 else if (MEM_P (dest
) && dest_addr_elt
!= 0
2421 && cselib_record_memory
)
2423 if (src_elt
->locs
== 0 && !PRESERVED_VALUE_P (src_elt
->val_rtx
))
2425 add_mem_for_addr (dest_addr_elt
, src_elt
, dest
);
2429 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2432 cselib_add_permanent_equiv (cselib_val
*elt
, rtx x
, rtx_insn
*insn
)
2435 rtx_insn
*save_cselib_current_insn
= cselib_current_insn
;
2437 gcc_checking_assert (elt
);
2438 gcc_checking_assert (PRESERVED_VALUE_P (elt
->val_rtx
));
2439 gcc_checking_assert (!side_effects_p (x
));
2441 cselib_current_insn
= insn
;
2443 nelt
= cselib_lookup (x
, GET_MODE (elt
->val_rtx
), 1, VOIDmode
);
2447 cselib_any_perm_equivs
= true;
2449 if (!PRESERVED_VALUE_P (nelt
->val_rtx
))
2450 cselib_preserve_value (nelt
);
2452 new_elt_loc_list (nelt
, elt
->val_rtx
);
2455 cselib_current_insn
= save_cselib_current_insn
;
2458 /* Return TRUE if any permanent equivalences have been recorded since
2459 the table was last initialized. */
2461 cselib_have_permanent_equivalences (void)
2463 return cselib_any_perm_equivs
;
2466 /* There is no good way to determine how many elements there can be
2467 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2468 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2470 struct cselib_record_autoinc_data
2472 struct cselib_set
*sets
;
2476 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2477 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2480 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED
, rtx op ATTRIBUTE_UNUSED
,
2481 rtx dest
, rtx src
, rtx srcoff
, void *arg
)
2483 struct cselib_record_autoinc_data
*data
;
2484 data
= (struct cselib_record_autoinc_data
*)arg
;
2486 data
->sets
[data
->n_sets
].dest
= dest
;
2489 data
->sets
[data
->n_sets
].src
= gen_rtx_PLUS (GET_MODE (src
), src
, srcoff
);
2491 data
->sets
[data
->n_sets
].src
= src
;
2498 /* Record the effects of any sets and autoincs in INSN. */
2500 cselib_record_sets (rtx_insn
*insn
)
2504 struct cselib_set sets
[MAX_SETS
];
2505 rtx body
= PATTERN (insn
);
2507 int n_sets_before_autoinc
;
2508 struct cselib_record_autoinc_data data
;
2510 body
= PATTERN (insn
);
2511 if (GET_CODE (body
) == COND_EXEC
)
2513 cond
= COND_EXEC_TEST (body
);
2514 body
= COND_EXEC_CODE (body
);
2517 /* Find all sets. */
2518 if (GET_CODE (body
) == SET
)
2520 sets
[0].src
= SET_SRC (body
);
2521 sets
[0].dest
= SET_DEST (body
);
2524 else if (GET_CODE (body
) == PARALLEL
)
2526 /* Look through the PARALLEL and record the values being
2527 set, if possible. Also handle any CLOBBERs. */
2528 for (i
= XVECLEN (body
, 0) - 1; i
>= 0; --i
)
2530 rtx x
= XVECEXP (body
, 0, i
);
2532 if (GET_CODE (x
) == SET
)
2534 sets
[n_sets
].src
= SET_SRC (x
);
2535 sets
[n_sets
].dest
= SET_DEST (x
);
2542 && MEM_P (sets
[0].src
)
2543 && !cselib_record_memory
2544 && MEM_READONLY_P (sets
[0].src
))
2546 rtx note
= find_reg_equal_equiv_note (insn
);
2548 if (note
&& CONSTANT_P (XEXP (note
, 0)))
2549 sets
[0].src
= XEXP (note
, 0);
2553 data
.n_sets
= n_sets_before_autoinc
= n_sets
;
2554 for_each_inc_dec (PATTERN (insn
), cselib_record_autoinc_cb
, &data
);
2555 n_sets
= data
.n_sets
;
2557 /* Look up the values that are read. Do this before invalidating the
2558 locations that are written. */
2559 for (i
= 0; i
< n_sets
; i
++)
2561 rtx dest
= sets
[i
].dest
;
2563 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2564 the low part after invalidating any knowledge about larger modes. */
2565 if (GET_CODE (sets
[i
].dest
) == STRICT_LOW_PART
)
2566 sets
[i
].dest
= dest
= XEXP (dest
, 0);
2568 /* We don't know how to record anything but REG or MEM. */
2570 || (MEM_P (dest
) && cselib_record_memory
))
2572 rtx src
= sets
[i
].src
;
2574 src
= gen_rtx_IF_THEN_ELSE (GET_MODE (dest
), cond
, src
, dest
);
2575 sets
[i
].src_elt
= cselib_lookup (src
, GET_MODE (dest
), 1, VOIDmode
);
2578 machine_mode address_mode
= get_address_mode (dest
);
2580 sets
[i
].dest_addr_elt
= cselib_lookup (XEXP (dest
, 0),
2585 sets
[i
].dest_addr_elt
= 0;
2589 if (cselib_record_sets_hook
)
2590 cselib_record_sets_hook (insn
, sets
, n_sets
);
2592 /* Invalidate all locations written by this insn. Note that the elts we
2593 looked up in the previous loop aren't affected, just some of their
2594 locations may go away. */
2595 note_stores (body
, cselib_invalidate_rtx_note_stores
, NULL
);
2597 for (i
= n_sets_before_autoinc
; i
< n_sets
; i
++)
2598 cselib_invalidate_rtx (sets
[i
].dest
);
2600 /* If this is an asm, look for duplicate sets. This can happen when the
2601 user uses the same value as an output multiple times. This is valid
2602 if the outputs are not actually used thereafter. Treat this case as
2603 if the value isn't actually set. We do this by smashing the destination
2604 to pc_rtx, so that we won't record the value later. */
2605 if (n_sets
>= 2 && asm_noperands (body
) >= 0)
2607 for (i
= 0; i
< n_sets
; i
++)
2609 rtx dest
= sets
[i
].dest
;
2610 if (REG_P (dest
) || MEM_P (dest
))
2613 for (j
= i
+ 1; j
< n_sets
; j
++)
2614 if (rtx_equal_p (dest
, sets
[j
].dest
))
2616 sets
[i
].dest
= pc_rtx
;
2617 sets
[j
].dest
= pc_rtx
;
2623 /* Now enter the equivalences in our tables. */
2624 for (i
= 0; i
< n_sets
; i
++)
2626 rtx dest
= sets
[i
].dest
;
2628 || (MEM_P (dest
) && cselib_record_memory
))
2629 cselib_record_set (dest
, sets
[i
].src_elt
, sets
[i
].dest_addr_elt
);
2633 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
2636 fp_setter_insn (rtx_insn
*insn
)
2638 rtx expr
, pat
= NULL_RTX
;
2640 if (!RTX_FRAME_RELATED_P (insn
))
2643 expr
= find_reg_note (insn
, REG_FRAME_RELATED_EXPR
, NULL_RTX
);
2645 pat
= XEXP (expr
, 0);
2646 if (!modified_in_p (hard_frame_pointer_rtx
, pat
? pat
: insn
))
2649 /* Don't return true for frame pointer restores in the epilogue. */
2650 if (find_reg_note (insn
, REG_CFA_RESTORE
, hard_frame_pointer_rtx
))
2655 /* Record the effects of INSN. */
2658 cselib_process_insn (rtx_insn
*insn
)
2663 cselib_current_insn
= insn
;
2665 /* Forget everything at a CODE_LABEL or a setjmp. */
2668 && find_reg_note (insn
, REG_SETJMP
, NULL
)))
2669 && !cselib_preserve_constants
)
2671 cselib_reset_table (next_uid
);
2672 cselib_current_insn
= NULL
;
2676 if (! INSN_P (insn
))
2678 cselib_current_insn
= NULL
;
2682 /* If this is a call instruction, forget anything stored in a
2683 call clobbered register, or, if this is not a const call, in
2687 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
2688 if (call_used_regs
[i
]
2689 || (REG_VALUES (i
) && REG_VALUES (i
)->elt
2690 && HARD_REGNO_CALL_PART_CLOBBERED (i
,
2691 GET_MODE (REG_VALUES (i
)->elt
->val_rtx
))))
2692 cselib_invalidate_regno (i
, reg_raw_mode
[i
]);
2694 /* Since it is not clear how cselib is going to be used, be
2695 conservative here and treat looping pure or const functions
2696 as if they were regular functions. */
2697 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn
)
2698 || !(RTL_CONST_OR_PURE_CALL_P (insn
)))
2699 cselib_invalidate_mem (callmem
);
2702 cselib_record_sets (insn
);
2704 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2705 after we have processed the insn. */
2708 for (x
= CALL_INSN_FUNCTION_USAGE (insn
); x
; x
= XEXP (x
, 1))
2709 if (GET_CODE (XEXP (x
, 0)) == CLOBBER
)
2710 cselib_invalidate_rtx (XEXP (XEXP (x
, 0), 0));
2711 /* Flush evertything on setjmp. */
2712 if (cselib_preserve_constants
2713 && find_reg_note (insn
, REG_SETJMP
, NULL
))
2715 cselib_preserve_only_values ();
2716 cselib_reset_table (next_uid
);
2720 /* On setter of the hard frame pointer if frame_pointer_needed,
2721 invalidate stack_pointer_rtx, so that sp and {,h}fp based
2722 VALUEs are distinct. */
2723 if (reload_completed
2724 && frame_pointer_needed
2725 && fp_setter_insn (insn
))
2726 cselib_invalidate_rtx (stack_pointer_rtx
);
2728 cselib_current_insn
= NULL
;
2730 if (n_useless_values
> MAX_USELESS_VALUES
2731 /* remove_useless_values is linear in the hash table size. Avoid
2732 quadratic behavior for very large hashtables with very few
2733 useless elements. */
2734 && ((unsigned int)n_useless_values
2735 > (cselib_hash_table
->elements () - n_debug_values
) / 4))
2736 remove_useless_values ();
2739 /* Initialize cselib for one pass. The caller must also call
2740 init_alias_analysis. */
2743 cselib_init (int record_what
)
2745 cselib_record_memory
= record_what
& CSELIB_RECORD_MEMORY
;
2746 cselib_preserve_constants
= record_what
& CSELIB_PRESERVE_CONSTANTS
;
2747 cselib_any_perm_equivs
= false;
2749 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2750 see canon_true_dependence. This is only created once. */
2752 callmem
= gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
));
2754 cselib_nregs
= max_reg_num ();
2756 /* We preserve reg_values to allow expensive clearing of the whole thing.
2757 Reallocate it however if it happens to be too large. */
2758 if (!reg_values
|| reg_values_size
< cselib_nregs
2759 || (reg_values_size
> 10 && reg_values_size
> cselib_nregs
* 4))
2762 /* Some space for newly emit instructions so we don't end up
2763 reallocating in between passes. */
2764 reg_values_size
= cselib_nregs
+ (63 + cselib_nregs
) / 16;
2765 reg_values
= XCNEWVEC (struct elt_list
*, reg_values_size
);
2767 used_regs
= XNEWVEC (unsigned int, cselib_nregs
);
2769 cselib_hash_table
= new hash_table
<cselib_hasher
> (31);
2770 if (cselib_preserve_constants
)
2771 cselib_preserved_hash_table
= new hash_table
<cselib_hasher
> (31);
2775 /* Called when the current user is done with cselib. */
2778 cselib_finish (void)
2780 bool preserved
= cselib_preserve_constants
;
2781 cselib_discard_hook
= NULL
;
2782 cselib_preserve_constants
= false;
2783 cselib_any_perm_equivs
= false;
2784 cfa_base_preserved_val
= NULL
;
2785 cfa_base_preserved_regno
= INVALID_REGNUM
;
2786 elt_list::pool
.release ();
2787 elt_loc_list::pool
.release ();
2788 cselib_val::pool
.release ();
2789 value_pool
.release ();
2790 cselib_clear_table ();
2791 delete cselib_hash_table
;
2792 cselib_hash_table
= NULL
;
2794 delete cselib_preserved_hash_table
;
2795 cselib_preserved_hash_table
= NULL
;
2798 n_useless_values
= 0;
2799 n_useless_debug_values
= 0;
2804 /* Dump the cselib_val *X to FILE *OUT. */
2807 dump_cselib_val (cselib_val
**x
, FILE *out
)
2810 bool need_lf
= true;
2812 print_inline_rtx (out
, v
->val_rtx
, 0);
2816 struct elt_loc_list
*l
= v
->locs
;
2822 fputs (" locs:", out
);
2825 if (l
->setting_insn
)
2826 fprintf (out
, "\n from insn %i ",
2827 INSN_UID (l
->setting_insn
));
2829 fprintf (out
, "\n ");
2830 print_inline_rtx (out
, l
->loc
, 4);
2832 while ((l
= l
->next
));
2837 fputs (" no locs", out
);
2843 struct elt_list
*e
= v
->addr_list
;
2849 fputs (" addr list:", out
);
2853 print_inline_rtx (out
, e
->elt
->val_rtx
, 2);
2855 while ((e
= e
->next
));
2860 fputs (" no addrs", out
);
2864 if (v
->next_containing_mem
== &dummy_val
)
2865 fputs (" last mem\n", out
);
2866 else if (v
->next_containing_mem
)
2868 fputs (" next mem ", out
);
2869 print_inline_rtx (out
, v
->next_containing_mem
->val_rtx
, 2);
2878 /* Dump to OUT everything in the CSELIB table. */
2881 dump_cselib_table (FILE *out
)
2883 fprintf (out
, "cselib hash table:\n");
2884 cselib_hash_table
->traverse
<FILE *, dump_cselib_val
> (out
);
2885 fprintf (out
, "cselib preserved hash table:\n");
2886 cselib_preserved_hash_table
->traverse
<FILE *, dump_cselib_val
> (out
);
2887 if (first_containing_mem
!= &dummy_val
)
2889 fputs ("first mem ", out
);
2890 print_inline_rtx (out
, first_containing_mem
->val_rtx
, 2);
2893 fprintf (out
, "next uid %i\n", next_uid
);
2896 #include "gt-cselib.h"