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"
27 #include "tree.h"/* FIXME: For hashing DEBUG_EXPR & friends. */
30 #include "hard-reg-set.h"
32 #include "insn-config.h"
36 #include "diagnostic-core.h"
38 #include "alloc-pool.h"
41 #include "basic-block.h"
44 #include "alloc-pool.h"
48 /* A list of cselib_val structures. */
51 struct elt_list
*next
;
54 /* Pool allocation new operator. */
55 inline void *operator new (size_t)
57 return pool
.allocate ();
60 /* Delete operator utilizing pool allocation. */
61 inline void operator delete (void *ptr
)
63 pool
.remove ((elt_list
*) ptr
);
66 /* Memory allocation pool. */
67 static pool_allocator
<elt_list
> pool
;
70 static bool cselib_record_memory
;
71 static bool cselib_preserve_constants
;
72 static bool cselib_any_perm_equivs
;
73 static inline void promote_debug_loc (struct elt_loc_list
*l
);
74 static struct elt_list
*new_elt_list (struct elt_list
*, cselib_val
*);
75 static void new_elt_loc_list (cselib_val
*, rtx
);
76 static void unchain_one_value (cselib_val
*);
77 static void unchain_one_elt_list (struct elt_list
**);
78 static void unchain_one_elt_loc_list (struct elt_loc_list
**);
79 static void remove_useless_values (void);
80 static int rtx_equal_for_cselib_1 (rtx
, rtx
, machine_mode
);
81 static unsigned int cselib_hash_rtx (rtx
, int, machine_mode
);
82 static cselib_val
*new_cselib_val (unsigned int, machine_mode
, rtx
);
83 static void add_mem_for_addr (cselib_val
*, cselib_val
*, rtx
);
84 static cselib_val
*cselib_lookup_mem (rtx
, int);
85 static void cselib_invalidate_regno (unsigned int, machine_mode
);
86 static void cselib_invalidate_mem (rtx
);
87 static void cselib_record_set (rtx
, cselib_val
*, cselib_val
*);
88 static void cselib_record_sets (rtx_insn
*);
90 struct expand_value_data
93 cselib_expand_callback callback
;
98 static rtx
cselib_expand_value_rtx_1 (rtx
, struct expand_value_data
*, int);
100 /* There are three ways in which cselib can look up an rtx:
101 - for a REG, the reg_values table (which is indexed by regno) is used
102 - for a MEM, we recursively look up its address and then follow the
103 addr_list of that value
104 - for everything else, we compute a hash value and go through the hash
105 table. Since different rtx's can still have the same hash value,
106 this involves walking the table entries for a given value and comparing
107 the locations of the entries with the rtx we are looking up. */
109 struct cselib_hasher
: nofree_ptr_hash
<cselib_val
>
112 /* The rtx value and its mode (needed separately for constant
116 /* The mode of the contaning MEM, if any, otherwise VOIDmode. */
117 machine_mode memmode
;
119 typedef key
*compare_type
;
120 static inline hashval_t
hash (const cselib_val
*);
121 static inline bool equal (const cselib_val
*, const key
*);
124 /* The hash function for our hash table. The value is always computed with
125 cselib_hash_rtx when adding an element; this function just extracts the
126 hash value from a cselib_val structure. */
129 cselib_hasher::hash (const cselib_val
*v
)
134 /* The equality test for our hash table. The first argument V is a table
135 element (i.e. a cselib_val), while the second arg X is an rtx. We know
136 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
137 CONST of an appropriate mode. */
140 cselib_hasher::equal (const cselib_val
*v
, const key
*x_arg
)
142 struct elt_loc_list
*l
;
144 machine_mode mode
= x_arg
->mode
;
145 machine_mode memmode
= x_arg
->memmode
;
147 if (mode
!= GET_MODE (v
->val_rtx
))
150 if (GET_CODE (x
) == VALUE
)
151 return x
== v
->val_rtx
;
153 /* We don't guarantee that distinct rtx's have different hash values,
154 so we need to do a comparison. */
155 for (l
= v
->locs
; l
; l
= l
->next
)
156 if (rtx_equal_for_cselib_1 (l
->loc
, x
, memmode
))
158 promote_debug_loc (l
);
165 /* A table that enables us to look up elts by their value. */
166 static hash_table
<cselib_hasher
> *cselib_hash_table
;
168 /* A table to hold preserved values. */
169 static hash_table
<cselib_hasher
> *cselib_preserved_hash_table
;
171 /* This is a global so we don't have to pass this through every function.
172 It is used in new_elt_loc_list to set SETTING_INSN. */
173 static rtx_insn
*cselib_current_insn
;
175 /* The unique id that the next create value will take. */
176 static unsigned int next_uid
;
178 /* The number of registers we had when the varrays were last resized. */
179 static unsigned int cselib_nregs
;
181 /* Count values without known locations, or with only locations that
182 wouldn't have been known except for debug insns. Whenever this
183 grows too big, we remove these useless values from the table.
185 Counting values with only debug values is a bit tricky. We don't
186 want to increment n_useless_values when we create a value for a
187 debug insn, for this would get n_useless_values out of sync, but we
188 want increment it if all locs in the list that were ever referenced
189 in nondebug insns are removed from the list.
191 In the general case, once we do that, we'd have to stop accepting
192 nondebug expressions in the loc list, to avoid having two values
193 equivalent that, without debug insns, would have been made into
194 separate values. However, because debug insns never introduce
195 equivalences themselves (no assignments), the only means for
196 growing loc lists is through nondebug assignments. If the locs
197 also happen to be referenced in debug insns, it will work just fine.
199 A consequence of this is that there's at most one debug-only loc in
200 each loc list. If we keep it in the first entry, testing whether
201 we have a debug-only loc list takes O(1).
203 Furthermore, since any additional entry in a loc list containing a
204 debug loc would have to come from an assignment (nondebug) that
205 references both the initial debug loc and the newly-equivalent loc,
206 the initial debug loc would be promoted to a nondebug loc, and the
207 loc list would not contain debug locs any more.
209 So the only case we have to be careful with in order to keep
210 n_useless_values in sync between debug and nondebug compilations is
211 to avoid incrementing n_useless_values when removing the single loc
212 from a value that turns out to not appear outside debug values. We
213 increment n_useless_debug_values instead, and leave such values
214 alone until, for other reasons, we garbage-collect useless
216 static int n_useless_values
;
217 static int n_useless_debug_values
;
219 /* Count values whose locs have been taken exclusively from debug
220 insns for the entire life of the value. */
221 static int n_debug_values
;
223 /* Number of useless values before we remove them from the hash table. */
224 #define MAX_USELESS_VALUES 32
226 /* This table maps from register number to values. It does not
227 contain pointers to cselib_val structures, but rather elt_lists.
228 The purpose is to be able to refer to the same register in
229 different modes. The first element of the list defines the mode in
230 which the register was set; if the mode is unknown or the value is
231 no longer valid in that mode, ELT will be NULL for the first
233 static struct elt_list
**reg_values
;
234 static unsigned int reg_values_size
;
235 #define REG_VALUES(i) reg_values[i]
237 /* The largest number of hard regs used by any entry added to the
238 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
239 static unsigned int max_value_regs
;
241 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
242 in cselib_clear_table() for fast emptying. */
243 static unsigned int *used_regs
;
244 static unsigned int n_used_regs
;
246 /* We pass this to cselib_invalidate_mem to invalidate all of
247 memory for a non-const call instruction. */
248 static GTY(()) rtx callmem
;
250 /* Set by discard_useless_locs if it deleted the last location of any
252 static int values_became_useless
;
254 /* Used as stop element of the containing_mem list so we can check
255 presence in the list by checking the next pointer. */
256 static cselib_val dummy_val
;
258 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
259 that is constant through the whole function and should never be
261 static cselib_val
*cfa_base_preserved_val
;
262 static unsigned int cfa_base_preserved_regno
= INVALID_REGNUM
;
264 /* Used to list all values that contain memory reference.
265 May or may not contain the useless values - the list is compacted
266 each time memory is invalidated. */
267 static cselib_val
*first_containing_mem
= &dummy_val
;
269 pool_allocator
<elt_list
> elt_list::pool ("elt_list", 10);
270 pool_allocator
<elt_loc_list
> elt_loc_list::pool ("elt_loc_list", 10);
271 pool_allocator
<cselib_val
> cselib_val::pool ("cselib_val_list", 10);
273 static pool_allocator
<rtx_def
> value_pool ("value", 100, RTX_CODE_SIZE (VALUE
),
276 /* If nonnull, cselib will call this function before freeing useless
277 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
278 void (*cselib_discard_hook
) (cselib_val
*);
280 /* If nonnull, cselib will call this function before recording sets or
281 even clobbering outputs of INSN. All the recorded sets will be
282 represented in the array sets[n_sets]. new_val_min can be used to
283 tell whether values present in sets are introduced by this
285 void (*cselib_record_sets_hook
) (rtx_insn
*insn
, struct cselib_set
*sets
,
288 #define PRESERVED_VALUE_P(RTX) \
289 (RTL_FLAG_CHECK1 ("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
291 #define SP_BASED_VALUE_P(RTX) \
292 (RTL_FLAG_CHECK1 ("SP_BASED_VALUE_P", (RTX), VALUE)->jump)
296 /* Allocate a struct elt_list and fill in its two elements with the
299 static inline struct elt_list
*
300 new_elt_list (struct elt_list
*next
, cselib_val
*elt
)
302 elt_list
*el
= new elt_list ();
308 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
312 new_elt_loc_list (cselib_val
*val
, rtx loc
)
314 struct elt_loc_list
*el
, *next
= val
->locs
;
316 gcc_checking_assert (!next
|| !next
->setting_insn
317 || !DEBUG_INSN_P (next
->setting_insn
)
318 || cselib_current_insn
== next
->setting_insn
);
320 /* If we're creating the first loc in a debug insn context, we've
321 just created a debug value. Count it. */
322 if (!next
&& cselib_current_insn
&& DEBUG_INSN_P (cselib_current_insn
))
325 val
= canonical_cselib_val (val
);
328 if (GET_CODE (loc
) == VALUE
)
330 loc
= canonical_cselib_val (CSELIB_VAL_PTR (loc
))->val_rtx
;
332 gcc_checking_assert (PRESERVED_VALUE_P (loc
)
333 == PRESERVED_VALUE_P (val
->val_rtx
));
335 if (val
->val_rtx
== loc
)
337 else if (val
->uid
> CSELIB_VAL_PTR (loc
)->uid
)
339 /* Reverse the insertion. */
340 new_elt_loc_list (CSELIB_VAL_PTR (loc
), val
->val_rtx
);
344 gcc_checking_assert (val
->uid
< CSELIB_VAL_PTR (loc
)->uid
);
346 if (CSELIB_VAL_PTR (loc
)->locs
)
348 /* Bring all locs from LOC to VAL. */
349 for (el
= CSELIB_VAL_PTR (loc
)->locs
; el
->next
; el
= el
->next
)
351 /* Adjust values that have LOC as canonical so that VAL
352 becomes their canonical. */
353 if (el
->loc
&& GET_CODE (el
->loc
) == VALUE
)
355 gcc_checking_assert (CSELIB_VAL_PTR (el
->loc
)->locs
->loc
357 CSELIB_VAL_PTR (el
->loc
)->locs
->loc
= val
->val_rtx
;
360 el
->next
= val
->locs
;
361 next
= val
->locs
= CSELIB_VAL_PTR (loc
)->locs
;
364 if (CSELIB_VAL_PTR (loc
)->addr_list
)
366 /* Bring in addr_list into canonical node. */
367 struct elt_list
*last
= CSELIB_VAL_PTR (loc
)->addr_list
;
370 last
->next
= val
->addr_list
;
371 val
->addr_list
= CSELIB_VAL_PTR (loc
)->addr_list
;
372 CSELIB_VAL_PTR (loc
)->addr_list
= NULL
;
375 if (CSELIB_VAL_PTR (loc
)->next_containing_mem
!= NULL
376 && val
->next_containing_mem
== NULL
)
378 /* Add VAL to the containing_mem list after LOC. LOC will
379 be removed when we notice it doesn't contain any
381 val
->next_containing_mem
= CSELIB_VAL_PTR (loc
)->next_containing_mem
;
382 CSELIB_VAL_PTR (loc
)->next_containing_mem
= val
;
385 /* Chain LOC back to VAL. */
386 el
= new elt_loc_list
;
387 el
->loc
= val
->val_rtx
;
388 el
->setting_insn
= cselib_current_insn
;
390 CSELIB_VAL_PTR (loc
)->locs
= el
;
393 el
= new elt_loc_list
;
395 el
->setting_insn
= cselib_current_insn
;
400 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
401 originating from a debug insn, maintaining the debug values
405 promote_debug_loc (struct elt_loc_list
*l
)
407 if (l
&& l
->setting_insn
&& DEBUG_INSN_P (l
->setting_insn
)
408 && (!cselib_current_insn
|| !DEBUG_INSN_P (cselib_current_insn
)))
411 l
->setting_insn
= cselib_current_insn
;
412 if (cselib_preserve_constants
&& l
->next
)
414 gcc_assert (l
->next
->setting_insn
415 && DEBUG_INSN_P (l
->next
->setting_insn
)
417 l
->next
->setting_insn
= cselib_current_insn
;
420 gcc_assert (!l
->next
);
424 /* The elt_list at *PL is no longer needed. Unchain it and free its
428 unchain_one_elt_list (struct elt_list
**pl
)
430 struct elt_list
*l
= *pl
;
436 /* Likewise for elt_loc_lists. */
439 unchain_one_elt_loc_list (struct elt_loc_list
**pl
)
441 struct elt_loc_list
*l
= *pl
;
447 /* Likewise for cselib_vals. This also frees the addr_list associated with
451 unchain_one_value (cselib_val
*v
)
454 unchain_one_elt_list (&v
->addr_list
);
459 /* Remove all entries from the hash table. Also used during
463 cselib_clear_table (void)
465 cselib_reset_table (1);
468 /* Return TRUE if V is a constant, a function invariant or a VALUE
469 equivalence; FALSE otherwise. */
472 invariant_or_equiv_p (cselib_val
*v
)
474 struct elt_loc_list
*l
;
476 if (v
== cfa_base_preserved_val
)
479 /* Keep VALUE equivalences around. */
480 for (l
= v
->locs
; l
; l
= l
->next
)
481 if (GET_CODE (l
->loc
) == VALUE
)
485 && v
->locs
->next
== NULL
)
487 if (CONSTANT_P (v
->locs
->loc
)
488 && (GET_CODE (v
->locs
->loc
) != CONST
489 || !references_value_p (v
->locs
->loc
, 0)))
491 /* Although a debug expr may be bound to different expressions,
492 we can preserve it as if it was constant, to get unification
493 and proper merging within var-tracking. */
494 if (GET_CODE (v
->locs
->loc
) == DEBUG_EXPR
495 || GET_CODE (v
->locs
->loc
) == DEBUG_IMPLICIT_PTR
496 || GET_CODE (v
->locs
->loc
) == ENTRY_VALUE
497 || GET_CODE (v
->locs
->loc
) == DEBUG_PARAMETER_REF
)
500 /* (plus (value V) (const_int C)) is invariant iff V is invariant. */
501 if (GET_CODE (v
->locs
->loc
) == PLUS
502 && CONST_INT_P (XEXP (v
->locs
->loc
, 1))
503 && GET_CODE (XEXP (v
->locs
->loc
, 0)) == VALUE
504 && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v
->locs
->loc
, 0))))
511 /* Remove from hash table all VALUEs except constants, function
512 invariants and VALUE equivalences. */
515 preserve_constants_and_equivs (cselib_val
**x
, void *info ATTRIBUTE_UNUSED
)
519 if (invariant_or_equiv_p (v
))
521 cselib_hasher::key lookup
= {
522 GET_MODE (v
->val_rtx
), v
->val_rtx
, VOIDmode
525 = cselib_preserved_hash_table
->find_slot_with_hash (&lookup
,
531 cselib_hash_table
->clear_slot (x
);
536 /* Remove all entries from the hash table, arranging for the next
537 value to be numbered NUM. */
540 cselib_reset_table (unsigned int num
)
546 if (cfa_base_preserved_val
)
548 unsigned int regno
= cfa_base_preserved_regno
;
549 unsigned int new_used_regs
= 0;
550 for (i
= 0; i
< n_used_regs
; i
++)
551 if (used_regs
[i
] == regno
)
557 REG_VALUES (used_regs
[i
]) = 0;
558 gcc_assert (new_used_regs
== 1);
559 n_used_regs
= new_used_regs
;
560 used_regs
[0] = regno
;
562 = hard_regno_nregs
[regno
][GET_MODE (cfa_base_preserved_val
->locs
->loc
)];
566 for (i
= 0; i
< n_used_regs
; i
++)
567 REG_VALUES (used_regs
[i
]) = 0;
571 if (cselib_preserve_constants
)
572 cselib_hash_table
->traverse
<void *, preserve_constants_and_equivs
>
576 cselib_hash_table
->empty ();
577 gcc_checking_assert (!cselib_any_perm_equivs
);
580 n_useless_values
= 0;
581 n_useless_debug_values
= 0;
586 first_containing_mem
= &dummy_val
;
589 /* Return the number of the next value that will be generated. */
592 cselib_get_next_uid (void)
597 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
598 INSERTing if requested. When X is part of the address of a MEM,
599 MEMMODE should specify the mode of the MEM. */
602 cselib_find_slot (machine_mode mode
, rtx x
, hashval_t hash
,
603 enum insert_option insert
, machine_mode memmode
)
605 cselib_val
**slot
= NULL
;
606 cselib_hasher::key lookup
= { mode
, x
, memmode
};
607 if (cselib_preserve_constants
)
608 slot
= cselib_preserved_hash_table
->find_slot_with_hash (&lookup
, hash
,
611 slot
= cselib_hash_table
->find_slot_with_hash (&lookup
, hash
, insert
);
615 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
616 only return true for values which point to a cselib_val whose value
617 element has been set to zero, which implies the cselib_val will be
621 references_value_p (const_rtx x
, int only_useless
)
623 const enum rtx_code code
= GET_CODE (x
);
624 const char *fmt
= GET_RTX_FORMAT (code
);
627 if (GET_CODE (x
) == VALUE
628 && (! only_useless
||
629 (CSELIB_VAL_PTR (x
)->locs
== 0 && !PRESERVED_VALUE_P (x
))))
632 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
634 if (fmt
[i
] == 'e' && references_value_p (XEXP (x
, i
), only_useless
))
636 else if (fmt
[i
] == 'E')
637 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
638 if (references_value_p (XVECEXP (x
, i
, j
), only_useless
))
645 /* For all locations found in X, delete locations that reference useless
646 values (i.e. values without any location). Called through
650 discard_useless_locs (cselib_val
**x
, void *info ATTRIBUTE_UNUSED
)
653 struct elt_loc_list
**p
= &v
->locs
;
654 bool had_locs
= v
->locs
!= NULL
;
655 rtx_insn
*setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
659 if (references_value_p ((*p
)->loc
, 1))
660 unchain_one_elt_loc_list (p
);
665 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
667 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
668 n_useless_debug_values
++;
671 values_became_useless
= 1;
676 /* If X is a value with no locations, remove it from the hashtable. */
679 discard_useless_values (cselib_val
**x
, void *info ATTRIBUTE_UNUSED
)
683 if (v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
685 if (cselib_discard_hook
)
686 cselib_discard_hook (v
);
688 CSELIB_VAL_PTR (v
->val_rtx
) = NULL
;
689 cselib_hash_table
->clear_slot (x
);
690 unchain_one_value (v
);
697 /* Clean out useless values (i.e. those which no longer have locations
698 associated with them) from the hash table. */
701 remove_useless_values (void)
705 /* First pass: eliminate locations that reference the value. That in
706 turn can make more values useless. */
709 values_became_useless
= 0;
710 cselib_hash_table
->traverse
<void *, discard_useless_locs
> (NULL
);
712 while (values_became_useless
);
714 /* Second pass: actually remove the values. */
716 p
= &first_containing_mem
;
717 for (v
= *p
; v
!= &dummy_val
; v
= v
->next_containing_mem
)
718 if (v
->locs
&& v
== canonical_cselib_val (v
))
721 p
= &(*p
)->next_containing_mem
;
725 n_useless_values
+= n_useless_debug_values
;
726 n_debug_values
-= n_useless_debug_values
;
727 n_useless_debug_values
= 0;
729 cselib_hash_table
->traverse
<void *, discard_useless_values
> (NULL
);
731 gcc_assert (!n_useless_values
);
734 /* Arrange for a value to not be removed from the hash table even if
735 it becomes useless. */
738 cselib_preserve_value (cselib_val
*v
)
740 PRESERVED_VALUE_P (v
->val_rtx
) = 1;
743 /* Test whether a value is preserved. */
746 cselib_preserved_value_p (cselib_val
*v
)
748 return PRESERVED_VALUE_P (v
->val_rtx
);
751 /* Arrange for a REG value to be assumed constant through the whole function,
752 never invalidated and preserved across cselib_reset_table calls. */
755 cselib_preserve_cfa_base_value (cselib_val
*v
, unsigned int regno
)
757 if (cselib_preserve_constants
759 && REG_P (v
->locs
->loc
))
761 cfa_base_preserved_val
= v
;
762 cfa_base_preserved_regno
= regno
;
766 /* Clean all non-constant expressions in the hash table, but retain
770 cselib_preserve_only_values (void)
774 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
775 cselib_invalidate_regno (i
, reg_raw_mode
[i
]);
777 cselib_invalidate_mem (callmem
);
779 remove_useless_values ();
781 gcc_assert (first_containing_mem
== &dummy_val
);
784 /* Arrange for a value to be marked as based on stack pointer
785 for find_base_term purposes. */
788 cselib_set_value_sp_based (cselib_val
*v
)
790 SP_BASED_VALUE_P (v
->val_rtx
) = 1;
793 /* Test whether a value is based on stack pointer for
794 find_base_term purposes. */
797 cselib_sp_based_value_p (cselib_val
*v
)
799 return SP_BASED_VALUE_P (v
->val_rtx
);
802 /* Return the mode in which a register was last set. If X is not a
803 register, return its mode. If the mode in which the register was
804 set is not known, or the value was already clobbered, return
808 cselib_reg_set_mode (const_rtx x
)
813 if (REG_VALUES (REGNO (x
)) == NULL
814 || REG_VALUES (REGNO (x
))->elt
== NULL
)
817 return GET_MODE (REG_VALUES (REGNO (x
))->elt
->val_rtx
);
820 /* Return nonzero if we can prove that X and Y contain the same value, taking
821 our gathered information into account. */
824 rtx_equal_for_cselib_p (rtx x
, rtx y
)
826 return rtx_equal_for_cselib_1 (x
, y
, VOIDmode
);
829 /* If x is a PLUS or an autoinc operation, expand the operation,
830 storing the offset, if any, in *OFF. */
833 autoinc_split (rtx x
, rtx
*off
, machine_mode memmode
)
835 switch (GET_CODE (x
))
842 if (memmode
== VOIDmode
)
845 *off
= GEN_INT (-GET_MODE_SIZE (memmode
));
850 if (memmode
== VOIDmode
)
853 *off
= GEN_INT (GET_MODE_SIZE (memmode
));
869 /* Return nonzero if we can prove that X and Y contain the same value,
870 taking our gathered information into account. MEMMODE holds the
871 mode of the enclosing MEM, if any, as required to deal with autoinc
872 addressing modes. If X and Y are not (known to be) part of
873 addresses, MEMMODE should be VOIDmode. */
876 rtx_equal_for_cselib_1 (rtx x
, rtx y
, machine_mode memmode
)
882 if (REG_P (x
) || MEM_P (x
))
884 cselib_val
*e
= cselib_lookup (x
, GET_MODE (x
), 0, memmode
);
890 if (REG_P (y
) || MEM_P (y
))
892 cselib_val
*e
= cselib_lookup (y
, GET_MODE (y
), 0, memmode
);
901 if (GET_CODE (x
) == VALUE
)
903 cselib_val
*e
= canonical_cselib_val (CSELIB_VAL_PTR (x
));
904 struct elt_loc_list
*l
;
906 if (GET_CODE (y
) == VALUE
)
907 return e
== canonical_cselib_val (CSELIB_VAL_PTR (y
));
909 for (l
= e
->locs
; l
; l
= l
->next
)
913 /* Avoid infinite recursion. We know we have the canonical
914 value, so we can just skip any values in the equivalence
916 if (REG_P (t
) || MEM_P (t
) || GET_CODE (t
) == VALUE
)
918 else if (rtx_equal_for_cselib_1 (t
, y
, memmode
))
924 else if (GET_CODE (y
) == VALUE
)
926 cselib_val
*e
= canonical_cselib_val (CSELIB_VAL_PTR (y
));
927 struct elt_loc_list
*l
;
929 for (l
= e
->locs
; l
; l
= l
->next
)
933 if (REG_P (t
) || MEM_P (t
) || GET_CODE (t
) == VALUE
)
935 else if (rtx_equal_for_cselib_1 (x
, t
, memmode
))
942 if (GET_MODE (x
) != GET_MODE (y
))
945 if (GET_CODE (x
) != GET_CODE (y
))
947 rtx xorig
= x
, yorig
= y
;
948 rtx xoff
= NULL
, yoff
= NULL
;
950 x
= autoinc_split (x
, &xoff
, memmode
);
951 y
= autoinc_split (y
, &yoff
, memmode
);
956 if (xoff
&& !rtx_equal_for_cselib_1 (xoff
, yoff
, memmode
))
959 /* Don't recurse if nothing changed. */
960 if (x
!= xorig
|| y
!= yorig
)
961 return rtx_equal_for_cselib_1 (x
, y
, memmode
);
966 /* These won't be handled correctly by the code below. */
967 switch (GET_CODE (x
))
973 case DEBUG_IMPLICIT_PTR
:
974 return DEBUG_IMPLICIT_PTR_DECL (x
)
975 == DEBUG_IMPLICIT_PTR_DECL (y
);
977 case DEBUG_PARAMETER_REF
:
978 return DEBUG_PARAMETER_REF_DECL (x
)
979 == DEBUG_PARAMETER_REF_DECL (y
);
982 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
983 use rtx_equal_for_cselib_1 to compare the operands. */
984 return rtx_equal_p (ENTRY_VALUE_EXP (x
), ENTRY_VALUE_EXP (y
));
987 return LABEL_REF_LABEL (x
) == LABEL_REF_LABEL (y
);
990 return REGNO (x
) == REGNO (y
);
993 /* We have to compare any autoinc operations in the addresses
994 using this MEM's mode. */
995 return rtx_equal_for_cselib_1 (XEXP (x
, 0), XEXP (y
, 0), GET_MODE (x
));
1001 code
= GET_CODE (x
);
1002 fmt
= GET_RTX_FORMAT (code
);
1004 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1011 if (XWINT (x
, i
) != XWINT (y
, i
))
1017 if (XINT (x
, i
) != XINT (y
, i
))
1023 /* Two vectors must have the same length. */
1024 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
1027 /* And the corresponding elements must match. */
1028 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1029 if (! rtx_equal_for_cselib_1 (XVECEXP (x
, i
, j
),
1030 XVECEXP (y
, i
, j
), memmode
))
1036 && targetm
.commutative_p (x
, UNKNOWN
)
1037 && rtx_equal_for_cselib_1 (XEXP (x
, 1), XEXP (y
, 0), memmode
)
1038 && rtx_equal_for_cselib_1 (XEXP (x
, 0), XEXP (y
, 1), memmode
))
1040 if (! rtx_equal_for_cselib_1 (XEXP (x
, i
), XEXP (y
, i
), memmode
))
1046 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
1051 /* These are just backpointers, so they don't matter. */
1058 /* It is believed that rtx's at this level will never
1059 contain anything but integers and other rtx's,
1060 except for within LABEL_REFs and SYMBOL_REFs. */
1068 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1069 For registers and memory locations, we look up their cselib_val structure
1070 and return its VALUE element.
1071 Possible reasons for return 0 are: the object is volatile, or we couldn't
1072 find a register or memory location in the table and CREATE is zero. If
1073 CREATE is nonzero, table elts are created for regs and mem.
1074 N.B. this hash function returns the same hash value for RTXes that
1075 differ only in the order of operands, thus it is suitable for comparisons
1076 that take commutativity into account.
1077 If we wanted to also support associative rules, we'd have to use a different
1078 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1079 MEMMODE indicates the mode of an enclosing MEM, and it's only
1080 used to compute autoinc values.
1081 We used to have a MODE argument for hashing for CONST_INTs, but that
1082 didn't make sense, since it caused spurious hash differences between
1083 (set (reg:SI 1) (const_int))
1084 (plus:SI (reg:SI 2) (reg:SI 1))
1086 (plus:SI (reg:SI 2) (const_int))
1087 If the mode is important in any context, it must be checked specifically
1088 in a comparison anyway, since relying on hash differences is unsafe. */
1091 cselib_hash_rtx (rtx x
, int create
, machine_mode memmode
)
1097 unsigned int hash
= 0;
1099 code
= GET_CODE (x
);
1100 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
1105 e
= CSELIB_VAL_PTR (x
);
1110 e
= cselib_lookup (x
, GET_MODE (x
), create
, memmode
);
1117 hash
+= ((unsigned) DEBUG_EXPR
<< 7)
1118 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x
));
1119 return hash
? hash
: (unsigned int) DEBUG_EXPR
;
1121 case DEBUG_IMPLICIT_PTR
:
1122 hash
+= ((unsigned) DEBUG_IMPLICIT_PTR
<< 7)
1123 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x
));
1124 return hash
? hash
: (unsigned int) DEBUG_IMPLICIT_PTR
;
1126 case DEBUG_PARAMETER_REF
:
1127 hash
+= ((unsigned) DEBUG_PARAMETER_REF
<< 7)
1128 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x
));
1129 return hash
? hash
: (unsigned int) DEBUG_PARAMETER_REF
;
1132 /* ENTRY_VALUEs are function invariant, thus try to avoid
1133 recursing on argument if ENTRY_VALUE is one of the
1134 forms emitted by expand_debug_expr, otherwise
1135 ENTRY_VALUE hash would depend on the current value
1136 in some register or memory. */
1137 if (REG_P (ENTRY_VALUE_EXP (x
)))
1138 hash
+= (unsigned int) REG
1139 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x
))
1140 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x
));
1141 else if (MEM_P (ENTRY_VALUE_EXP (x
))
1142 && REG_P (XEXP (ENTRY_VALUE_EXP (x
), 0)))
1143 hash
+= (unsigned int) MEM
1144 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x
), 0))
1145 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x
), 0));
1147 hash
+= cselib_hash_rtx (ENTRY_VALUE_EXP (x
), create
, memmode
);
1148 return hash
? hash
: (unsigned int) ENTRY_VALUE
;
1151 hash
+= ((unsigned) CONST_INT
<< 7) + UINTVAL (x
);
1152 return hash
? hash
: (unsigned int) CONST_INT
;
1154 case CONST_WIDE_INT
:
1155 for (i
= 0; i
< CONST_WIDE_INT_NUNITS (x
); i
++)
1156 hash
+= CONST_WIDE_INT_ELT (x
, i
);
1160 /* This is like the general case, except that it only counts
1161 the integers representing the constant. */
1162 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
1163 if (TARGET_SUPPORTS_WIDE_INT
== 0 && GET_MODE (x
) == VOIDmode
)
1164 hash
+= ((unsigned) CONST_DOUBLE_LOW (x
)
1165 + (unsigned) CONST_DOUBLE_HIGH (x
));
1167 hash
+= real_hash (CONST_DOUBLE_REAL_VALUE (x
));
1168 return hash
? hash
: (unsigned int) CONST_DOUBLE
;
1171 hash
+= (unsigned int) code
+ (unsigned int) GET_MODE (x
);
1172 hash
+= fixed_hash (CONST_FIXED_VALUE (x
));
1173 return hash
? hash
: (unsigned int) CONST_FIXED
;
1180 units
= CONST_VECTOR_NUNITS (x
);
1182 for (i
= 0; i
< units
; ++i
)
1184 elt
= CONST_VECTOR_ELT (x
, i
);
1185 hash
+= cselib_hash_rtx (elt
, 0, memmode
);
1191 /* Assume there is only one rtx object for any given label. */
1193 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1194 differences and differences between each stage's debugging dumps. */
1195 hash
+= (((unsigned int) LABEL_REF
<< 7)
1196 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x
)));
1197 return hash
? hash
: (unsigned int) LABEL_REF
;
1201 /* Don't hash on the symbol's address to avoid bootstrap differences.
1202 Different hash values may cause expressions to be recorded in
1203 different orders and thus different registers to be used in the
1204 final assembler. This also avoids differences in the dump files
1205 between various stages. */
1207 const unsigned char *p
= (const unsigned char *) XSTR (x
, 0);
1210 h
+= (h
<< 7) + *p
++; /* ??? revisit */
1212 hash
+= ((unsigned int) SYMBOL_REF
<< 7) + h
;
1213 return hash
? hash
: (unsigned int) SYMBOL_REF
;
1218 /* We can't compute these without knowing the MEM mode. */
1219 gcc_assert (memmode
!= VOIDmode
);
1220 i
= GET_MODE_SIZE (memmode
);
1221 if (code
== PRE_DEC
)
1223 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1224 like (mem:MEMMODE (plus (reg) (const_int I))). */
1225 hash
+= (unsigned) PLUS
- (unsigned)code
1226 + cselib_hash_rtx (XEXP (x
, 0), create
, memmode
)
1227 + cselib_hash_rtx (GEN_INT (i
), create
, memmode
);
1228 return hash
? hash
: 1 + (unsigned) PLUS
;
1231 gcc_assert (memmode
!= VOIDmode
);
1232 return cselib_hash_rtx (XEXP (x
, 1), create
, memmode
);
1237 gcc_assert (memmode
!= VOIDmode
);
1238 return cselib_hash_rtx (XEXP (x
, 0), create
, memmode
);
1243 case UNSPEC_VOLATILE
:
1247 if (MEM_VOLATILE_P (x
))
1256 i
= GET_RTX_LENGTH (code
) - 1;
1257 fmt
= GET_RTX_FORMAT (code
);
1264 rtx tem
= XEXP (x
, i
);
1265 unsigned int tem_hash
= cselib_hash_rtx (tem
, create
, memmode
);
1274 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1276 unsigned int tem_hash
1277 = cselib_hash_rtx (XVECEXP (x
, i
, j
), create
, memmode
);
1288 const unsigned char *p
= (const unsigned char *) XSTR (x
, i
);
1297 hash
+= XINT (x
, i
);
1310 return hash
? hash
: 1 + (unsigned int) GET_CODE (x
);
1313 /* Create a new value structure for VALUE and initialize it. The mode of the
1316 static inline cselib_val
*
1317 new_cselib_val (unsigned int hash
, machine_mode mode
, rtx x
)
1319 cselib_val
*e
= new cselib_val
;
1322 gcc_assert (next_uid
);
1325 e
->uid
= next_uid
++;
1326 /* We use an alloc pool to allocate this RTL construct because it
1327 accounts for about 8% of the overall memory usage. We know
1328 precisely when we can have VALUE RTXen (when cselib is active)
1329 so we don't need to put them in garbage collected memory.
1330 ??? Why should a VALUE be an RTX in the first place? */
1331 e
->val_rtx
= value_pool
.allocate ();
1332 memset (e
->val_rtx
, 0, RTX_HDR_SIZE
);
1333 PUT_CODE (e
->val_rtx
, VALUE
);
1334 PUT_MODE (e
->val_rtx
, mode
);
1335 CSELIB_VAL_PTR (e
->val_rtx
) = e
;
1338 e
->next_containing_mem
= 0;
1340 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1342 fprintf (dump_file
, "cselib value %u:%u ", e
->uid
, hash
);
1343 if (flag_dump_noaddr
|| flag_dump_unnumbered
)
1344 fputs ("# ", dump_file
);
1346 fprintf (dump_file
, "%p ", (void*)e
);
1347 print_rtl_single (dump_file
, x
);
1348 fputc ('\n', dump_file
);
1354 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1355 contains the data at this address. X is a MEM that represents the
1356 value. Update the two value structures to represent this situation. */
1359 add_mem_for_addr (cselib_val
*addr_elt
, cselib_val
*mem_elt
, rtx x
)
1361 struct elt_loc_list
*l
;
1363 addr_elt
= canonical_cselib_val (addr_elt
);
1364 mem_elt
= canonical_cselib_val (mem_elt
);
1366 /* Avoid duplicates. */
1367 for (l
= mem_elt
->locs
; l
; l
= l
->next
)
1369 && CSELIB_VAL_PTR (XEXP (l
->loc
, 0)) == addr_elt
)
1371 promote_debug_loc (l
);
1375 addr_elt
->addr_list
= new_elt_list (addr_elt
->addr_list
, mem_elt
);
1376 new_elt_loc_list (mem_elt
,
1377 replace_equiv_address_nv (x
, addr_elt
->val_rtx
));
1378 if (mem_elt
->next_containing_mem
== NULL
)
1380 mem_elt
->next_containing_mem
= first_containing_mem
;
1381 first_containing_mem
= mem_elt
;
1385 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1386 If CREATE, make a new one if we haven't seen it before. */
1389 cselib_lookup_mem (rtx x
, int create
)
1391 machine_mode mode
= GET_MODE (x
);
1392 machine_mode addr_mode
;
1395 cselib_val
*mem_elt
;
1398 if (MEM_VOLATILE_P (x
) || mode
== BLKmode
1399 || !cselib_record_memory
1400 || (FLOAT_MODE_P (mode
) && flag_float_store
))
1403 addr_mode
= GET_MODE (XEXP (x
, 0));
1404 if (addr_mode
== VOIDmode
)
1407 /* Look up the value for the address. */
1408 addr
= cselib_lookup (XEXP (x
, 0), addr_mode
, create
, mode
);
1412 addr
= canonical_cselib_val (addr
);
1413 /* Find a value that describes a value of our mode at that address. */
1414 for (l
= addr
->addr_list
; l
; l
= l
->next
)
1415 if (GET_MODE (l
->elt
->val_rtx
) == mode
)
1417 promote_debug_loc (l
->elt
->locs
);
1424 mem_elt
= new_cselib_val (next_uid
, mode
, x
);
1425 add_mem_for_addr (addr
, mem_elt
, x
);
1426 slot
= cselib_find_slot (mode
, x
, mem_elt
->hash
, INSERT
, VOIDmode
);
1431 /* Search through the possible substitutions in P. We prefer a non reg
1432 substitution because this allows us to expand the tree further. If
1433 we find, just a reg, take the lowest regno. There may be several
1434 non-reg results, we just take the first one because they will all
1435 expand to the same place. */
1438 expand_loc (struct elt_loc_list
*p
, struct expand_value_data
*evd
,
1441 rtx reg_result
= NULL
;
1442 unsigned int regno
= UINT_MAX
;
1443 struct elt_loc_list
*p_in
= p
;
1445 for (; p
; p
= p
->next
)
1447 /* Return these right away to avoid returning stack pointer based
1448 expressions for frame pointer and vice versa, which is something
1449 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1450 for more details. */
1452 && (REGNO (p
->loc
) == STACK_POINTER_REGNUM
1453 || REGNO (p
->loc
) == FRAME_POINTER_REGNUM
1454 || REGNO (p
->loc
) == HARD_FRAME_POINTER_REGNUM
1455 || REGNO (p
->loc
) == cfa_base_preserved_regno
))
1457 /* Avoid infinite recursion trying to expand a reg into a
1459 if ((REG_P (p
->loc
))
1460 && (REGNO (p
->loc
) < regno
)
1461 && !bitmap_bit_p (evd
->regs_active
, REGNO (p
->loc
)))
1463 reg_result
= p
->loc
;
1464 regno
= REGNO (p
->loc
);
1466 /* Avoid infinite recursion and do not try to expand the
1468 else if (GET_CODE (p
->loc
) == VALUE
1469 && CSELIB_VAL_PTR (p
->loc
)->locs
== p_in
)
1471 else if (!REG_P (p
->loc
))
1474 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1476 print_inline_rtx (dump_file
, p
->loc
, 0);
1477 fprintf (dump_file
, "\n");
1479 if (GET_CODE (p
->loc
) == LO_SUM
1480 && GET_CODE (XEXP (p
->loc
, 1)) == SYMBOL_REF
1482 && (note
= find_reg_note (p
->setting_insn
, REG_EQUAL
, NULL_RTX
))
1483 && XEXP (note
, 0) == XEXP (p
->loc
, 1))
1484 return XEXP (p
->loc
, 1);
1485 result
= cselib_expand_value_rtx_1 (p
->loc
, evd
, max_depth
- 1);
1492 if (regno
!= UINT_MAX
)
1495 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1496 fprintf (dump_file
, "r%d\n", regno
);
1498 result
= cselib_expand_value_rtx_1 (reg_result
, evd
, max_depth
- 1);
1503 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1507 print_inline_rtx (dump_file
, reg_result
, 0);
1508 fprintf (dump_file
, "\n");
1511 fprintf (dump_file
, "NULL\n");
1517 /* Forward substitute and expand an expression out to its roots.
1518 This is the opposite of common subexpression. Because local value
1519 numbering is such a weak optimization, the expanded expression is
1520 pretty much unique (not from a pointer equals point of view but
1521 from a tree shape point of view.
1523 This function returns NULL if the expansion fails. The expansion
1524 will fail if there is no value number for one of the operands or if
1525 one of the operands has been overwritten between the current insn
1526 and the beginning of the basic block. For instance x has no
1532 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1533 It is clear on return. */
1536 cselib_expand_value_rtx (rtx orig
, bitmap regs_active
, int max_depth
)
1538 struct expand_value_data evd
;
1540 evd
.regs_active
= regs_active
;
1541 evd
.callback
= NULL
;
1542 evd
.callback_arg
= NULL
;
1545 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
);
1548 /* Same as cselib_expand_value_rtx, but using a callback to try to
1549 resolve some expressions. The CB function should return ORIG if it
1550 can't or does not want to deal with a certain RTX. Any other
1551 return value, including NULL, will be used as the expansion for
1552 VALUE, without any further changes. */
1555 cselib_expand_value_rtx_cb (rtx orig
, bitmap regs_active
, int max_depth
,
1556 cselib_expand_callback cb
, void *data
)
1558 struct expand_value_data evd
;
1560 evd
.regs_active
= regs_active
;
1562 evd
.callback_arg
= data
;
1565 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
);
1568 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1569 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1570 would return NULL or non-NULL, without allocating new rtx. */
1573 cselib_dummy_expand_value_rtx_cb (rtx orig
, bitmap regs_active
, int max_depth
,
1574 cselib_expand_callback cb
, void *data
)
1576 struct expand_value_data evd
;
1578 evd
.regs_active
= regs_active
;
1580 evd
.callback_arg
= data
;
1583 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
) != NULL
;
1586 /* Internal implementation of cselib_expand_value_rtx and
1587 cselib_expand_value_rtx_cb. */
1590 cselib_expand_value_rtx_1 (rtx orig
, struct expand_value_data
*evd
,
1596 const char *format_ptr
;
1599 code
= GET_CODE (orig
);
1601 /* For the context of dse, if we end up expand into a huge tree, we
1602 will not have a useful address, so we might as well just give up
1611 struct elt_list
*l
= REG_VALUES (REGNO (orig
));
1613 if (l
&& l
->elt
== NULL
)
1615 for (; l
; l
= l
->next
)
1616 if (GET_MODE (l
->elt
->val_rtx
) == GET_MODE (orig
))
1619 unsigned regno
= REGNO (orig
);
1621 /* The only thing that we are not willing to do (this
1622 is requirement of dse and if others potential uses
1623 need this function we should add a parm to control
1624 it) is that we will not substitute the
1625 STACK_POINTER_REGNUM, FRAME_POINTER or the
1628 These expansions confuses the code that notices that
1629 stores into the frame go dead at the end of the
1630 function and that the frame is not effected by calls
1631 to subroutines. If you allow the
1632 STACK_POINTER_REGNUM substitution, then dse will
1633 think that parameter pushing also goes dead which is
1634 wrong. If you allow the FRAME_POINTER or the
1635 HARD_FRAME_POINTER then you lose the opportunity to
1636 make the frame assumptions. */
1637 if (regno
== STACK_POINTER_REGNUM
1638 || regno
== FRAME_POINTER_REGNUM
1639 || regno
== HARD_FRAME_POINTER_REGNUM
1640 || regno
== cfa_base_preserved_regno
)
1643 bitmap_set_bit (evd
->regs_active
, regno
);
1645 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1646 fprintf (dump_file
, "expanding: r%d into: ", regno
);
1648 result
= expand_loc (l
->elt
->locs
, evd
, max_depth
);
1649 bitmap_clear_bit (evd
->regs_active
, regno
);
1664 /* SCRATCH must be shared because they represent distinct values. */
1667 if (REG_P (XEXP (orig
, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig
, 0))))
1672 if (shared_const_p (orig
))
1682 subreg
= evd
->callback (orig
, evd
->regs_active
, max_depth
,
1688 subreg
= cselib_expand_value_rtx_1 (SUBREG_REG (orig
), evd
,
1692 scopy
= simplify_gen_subreg (GET_MODE (orig
), subreg
,
1693 GET_MODE (SUBREG_REG (orig
)),
1694 SUBREG_BYTE (orig
));
1696 || (GET_CODE (scopy
) == SUBREG
1697 && !REG_P (SUBREG_REG (scopy
))
1698 && !MEM_P (SUBREG_REG (scopy
))))
1708 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1710 fputs ("\nexpanding ", dump_file
);
1711 print_rtl_single (dump_file
, orig
);
1712 fputs (" into...", dump_file
);
1717 result
= evd
->callback (orig
, evd
->regs_active
, max_depth
,
1724 result
= expand_loc (CSELIB_VAL_PTR (orig
)->locs
, evd
, max_depth
);
1730 return evd
->callback (orig
, evd
->regs_active
, max_depth
,
1738 /* Copy the various flags, fields, and other information. We assume
1739 that all fields need copying, and then clear the fields that should
1740 not be copied. That is the sensible default behavior, and forces
1741 us to explicitly document why we are *not* copying a flag. */
1745 copy
= shallow_copy_rtx (orig
);
1747 format_ptr
= GET_RTX_FORMAT (code
);
1749 for (i
= 0; i
< GET_RTX_LENGTH (code
); i
++)
1750 switch (*format_ptr
++)
1753 if (XEXP (orig
, i
) != NULL
)
1755 rtx result
= cselib_expand_value_rtx_1 (XEXP (orig
, i
), evd
,
1760 XEXP (copy
, i
) = result
;
1766 if (XVEC (orig
, i
) != NULL
)
1769 XVEC (copy
, i
) = rtvec_alloc (XVECLEN (orig
, i
));
1770 for (j
= 0; j
< XVECLEN (orig
, i
); j
++)
1772 rtx result
= cselib_expand_value_rtx_1 (XVECEXP (orig
, i
, j
),
1773 evd
, max_depth
- 1);
1777 XVECEXP (copy
, i
, j
) = result
;
1791 /* These are left unchanged. */
1801 mode
= GET_MODE (copy
);
1802 /* If an operand has been simplified into CONST_INT, which doesn't
1803 have a mode and the mode isn't derivable from whole rtx's mode,
1804 try simplify_*_operation first with mode from original's operand
1805 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1807 switch (GET_RTX_CLASS (code
))
1810 if (CONST_INT_P (XEXP (copy
, 0))
1811 && GET_MODE (XEXP (orig
, 0)) != VOIDmode
)
1813 scopy
= simplify_unary_operation (code
, mode
, XEXP (copy
, 0),
1814 GET_MODE (XEXP (orig
, 0)));
1819 case RTX_COMM_ARITH
:
1821 /* These expressions can derive operand modes from the whole rtx's mode. */
1824 case RTX_BITFIELD_OPS
:
1825 if (CONST_INT_P (XEXP (copy
, 0))
1826 && GET_MODE (XEXP (orig
, 0)) != VOIDmode
)
1828 scopy
= simplify_ternary_operation (code
, mode
,
1829 GET_MODE (XEXP (orig
, 0)),
1830 XEXP (copy
, 0), XEXP (copy
, 1),
1837 case RTX_COMM_COMPARE
:
1838 if (CONST_INT_P (XEXP (copy
, 0))
1839 && GET_MODE (XEXP (copy
, 1)) == VOIDmode
1840 && (GET_MODE (XEXP (orig
, 0)) != VOIDmode
1841 || GET_MODE (XEXP (orig
, 1)) != VOIDmode
))
1843 scopy
= simplify_relational_operation (code
, mode
,
1844 (GET_MODE (XEXP (orig
, 0))
1846 ? GET_MODE (XEXP (orig
, 0))
1847 : GET_MODE (XEXP (orig
, 1)),
1857 scopy
= simplify_rtx (copy
);
1863 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1864 with VALUE expressions. This way, it becomes independent of changes
1865 to registers and memory.
1866 X isn't actually modified; if modifications are needed, new rtl is
1867 allocated. However, the return value can share rtl with X.
1868 If X is within a MEM, MEMMODE must be the mode of the MEM. */
1871 cselib_subst_to_values (rtx x
, machine_mode memmode
)
1873 enum rtx_code code
= GET_CODE (x
);
1874 const char *fmt
= GET_RTX_FORMAT (code
);
1883 l
= REG_VALUES (REGNO (x
));
1884 if (l
&& l
->elt
== NULL
)
1886 for (; l
; l
= l
->next
)
1887 if (GET_MODE (l
->elt
->val_rtx
) == GET_MODE (x
))
1888 return l
->elt
->val_rtx
;
1893 e
= cselib_lookup_mem (x
, 0);
1894 /* This used to happen for autoincrements, but we deal with them
1895 properly now. Remove the if stmt for the next release. */
1898 /* Assign a value that doesn't match any other. */
1899 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
1904 e
= cselib_lookup (x
, GET_MODE (x
), 0, memmode
);
1914 gcc_assert (memmode
!= VOIDmode
);
1915 i
= GET_MODE_SIZE (memmode
);
1916 if (code
== PRE_DEC
)
1918 return cselib_subst_to_values (plus_constant (GET_MODE (x
),
1923 gcc_assert (memmode
!= VOIDmode
);
1924 return cselib_subst_to_values (XEXP (x
, 1), memmode
);
1929 gcc_assert (memmode
!= VOIDmode
);
1930 return cselib_subst_to_values (XEXP (x
, 0), memmode
);
1936 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1940 rtx t
= cselib_subst_to_values (XEXP (x
, i
), memmode
);
1942 if (t
!= XEXP (x
, i
))
1945 copy
= shallow_copy_rtx (x
);
1949 else if (fmt
[i
] == 'E')
1953 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1955 rtx t
= cselib_subst_to_values (XVECEXP (x
, i
, j
), memmode
);
1957 if (t
!= XVECEXP (x
, i
, j
))
1959 if (XVEC (x
, i
) == XVEC (copy
, i
))
1962 copy
= shallow_copy_rtx (x
);
1963 XVEC (copy
, i
) = shallow_copy_rtvec (XVEC (x
, i
));
1965 XVECEXP (copy
, i
, j
) = t
;
1974 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
1977 cselib_subst_to_values_from_insn (rtx x
, machine_mode memmode
, rtx_insn
*insn
)
1980 gcc_assert (!cselib_current_insn
);
1981 cselib_current_insn
= insn
;
1982 ret
= cselib_subst_to_values (x
, memmode
);
1983 cselib_current_insn
= NULL
;
1987 /* Look up the rtl expression X in our tables and return the value it
1988 has. If CREATE is zero, we return NULL if we don't know the value.
1989 Otherwise, we create a new one if possible, using mode MODE if X
1990 doesn't have a mode (i.e. because it's a constant). When X is part
1991 of an address, MEMMODE should be the mode of the enclosing MEM if
1992 we're tracking autoinc expressions. */
1995 cselib_lookup_1 (rtx x
, machine_mode mode
,
1996 int create
, machine_mode memmode
)
2000 unsigned int hashval
;
2002 if (GET_MODE (x
) != VOIDmode
)
2003 mode
= GET_MODE (x
);
2005 if (GET_CODE (x
) == VALUE
)
2006 return CSELIB_VAL_PTR (x
);
2011 unsigned int i
= REGNO (x
);
2014 if (l
&& l
->elt
== NULL
)
2016 for (; l
; l
= l
->next
)
2017 if (mode
== GET_MODE (l
->elt
->val_rtx
))
2019 promote_debug_loc (l
->elt
->locs
);
2026 if (i
< FIRST_PSEUDO_REGISTER
)
2028 unsigned int n
= hard_regno_nregs
[i
][mode
];
2030 if (n
> max_value_regs
)
2034 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
2035 new_elt_loc_list (e
, x
);
2036 if (REG_VALUES (i
) == 0)
2038 /* Maintain the invariant that the first entry of
2039 REG_VALUES, if present, must be the value used to set the
2040 register, or NULL. */
2041 used_regs
[n_used_regs
++] = i
;
2042 REG_VALUES (i
) = new_elt_list (REG_VALUES (i
), NULL
);
2044 else if (cselib_preserve_constants
2045 && GET_MODE_CLASS (mode
) == MODE_INT
)
2047 /* During var-tracking, try harder to find equivalences
2048 for SUBREGs. If a setter sets say a DImode register
2049 and user uses that register only in SImode, add a lowpart
2051 struct elt_list
*lwider
= NULL
;
2053 if (l
&& l
->elt
== NULL
)
2055 for (; l
; l
= l
->next
)
2056 if (GET_MODE_CLASS (GET_MODE (l
->elt
->val_rtx
)) == MODE_INT
2057 && GET_MODE_SIZE (GET_MODE (l
->elt
->val_rtx
))
2058 > GET_MODE_SIZE (mode
)
2060 || GET_MODE_SIZE (GET_MODE (l
->elt
->val_rtx
))
2061 < GET_MODE_SIZE (GET_MODE (lwider
->elt
->val_rtx
))))
2063 struct elt_loc_list
*el
;
2064 if (i
< FIRST_PSEUDO_REGISTER
2065 && hard_regno_nregs
[i
][GET_MODE (l
->elt
->val_rtx
)] != 1)
2067 for (el
= l
->elt
->locs
; el
; el
= el
->next
)
2068 if (!REG_P (el
->loc
))
2075 rtx sub
= lowpart_subreg (mode
, lwider
->elt
->val_rtx
,
2076 GET_MODE (lwider
->elt
->val_rtx
));
2078 new_elt_loc_list (e
, sub
);
2081 REG_VALUES (i
)->next
= new_elt_list (REG_VALUES (i
)->next
, e
);
2082 slot
= cselib_find_slot (mode
, x
, e
->hash
, INSERT
, memmode
);
2088 return cselib_lookup_mem (x
, create
);
2090 hashval
= cselib_hash_rtx (x
, create
, memmode
);
2091 /* Can't even create if hashing is not possible. */
2095 slot
= cselib_find_slot (mode
, x
, hashval
,
2096 create
? INSERT
: NO_INSERT
, memmode
);
2100 e
= (cselib_val
*) *slot
;
2104 e
= new_cselib_val (hashval
, mode
, x
);
2106 /* We have to fill the slot before calling cselib_subst_to_values:
2107 the hash table is inconsistent until we do so, and
2108 cselib_subst_to_values will need to do lookups. */
2110 new_elt_loc_list (e
, cselib_subst_to_values (x
, memmode
));
2114 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2117 cselib_lookup_from_insn (rtx x
, machine_mode mode
,
2118 int create
, machine_mode memmode
, rtx_insn
*insn
)
2122 gcc_assert (!cselib_current_insn
);
2123 cselib_current_insn
= insn
;
2125 ret
= cselib_lookup (x
, mode
, create
, memmode
);
2127 cselib_current_insn
= NULL
;
2132 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2133 maintains invariants related with debug insns. */
2136 cselib_lookup (rtx x
, machine_mode mode
,
2137 int create
, machine_mode memmode
)
2139 cselib_val
*ret
= cselib_lookup_1 (x
, mode
, create
, memmode
);
2141 /* ??? Should we return NULL if we're not to create an entry, the
2142 found loc is a debug loc and cselib_current_insn is not DEBUG?
2143 If so, we should also avoid converting val to non-DEBUG; probably
2144 easiest setting cselib_current_insn to NULL before the call
2147 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
2149 fputs ("cselib lookup ", dump_file
);
2150 print_inline_rtx (dump_file
, x
, 2);
2151 fprintf (dump_file
, " => %u:%u\n",
2153 ret
? ret
->hash
: 0);
2159 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2160 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2161 is used to determine how many hard registers are being changed. If MODE
2162 is VOIDmode, then only REGNO is being changed; this is used when
2163 invalidating call clobbered registers across a call. */
2166 cselib_invalidate_regno (unsigned int regno
, machine_mode mode
)
2168 unsigned int endregno
;
2171 /* If we see pseudos after reload, something is _wrong_. */
2172 gcc_assert (!reload_completed
|| regno
< FIRST_PSEUDO_REGISTER
2173 || reg_renumber
[regno
] < 0);
2175 /* Determine the range of registers that must be invalidated. For
2176 pseudos, only REGNO is affected. For hard regs, we must take MODE
2177 into account, and we must also invalidate lower register numbers
2178 if they contain values that overlap REGNO. */
2179 if (regno
< FIRST_PSEUDO_REGISTER
)
2181 gcc_assert (mode
!= VOIDmode
);
2183 if (regno
< max_value_regs
)
2186 i
= regno
- max_value_regs
;
2188 endregno
= end_hard_regno (mode
, regno
);
2193 endregno
= regno
+ 1;
2196 for (; i
< endregno
; i
++)
2198 struct elt_list
**l
= ®_VALUES (i
);
2200 /* Go through all known values for this reg; if it overlaps the range
2201 we're invalidating, remove the value. */
2204 cselib_val
*v
= (*l
)->elt
;
2206 rtx_insn
*setting_insn
;
2207 struct elt_loc_list
**p
;
2208 unsigned int this_last
= i
;
2210 if (i
< FIRST_PSEUDO_REGISTER
&& v
!= NULL
)
2211 this_last
= end_hard_regno (GET_MODE (v
->val_rtx
), i
) - 1;
2213 if (this_last
< regno
|| v
== NULL
2214 || (v
== cfa_base_preserved_val
2215 && i
== cfa_base_preserved_regno
))
2221 /* We have an overlap. */
2222 if (*l
== REG_VALUES (i
))
2224 /* Maintain the invariant that the first entry of
2225 REG_VALUES, if present, must be the value used to set
2226 the register, or NULL. This is also nice because
2227 then we won't push the same regno onto user_regs
2233 unchain_one_elt_list (l
);
2235 v
= canonical_cselib_val (v
);
2237 had_locs
= v
->locs
!= NULL
;
2238 setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
2240 /* Now, we clear the mapping from value to reg. It must exist, so
2241 this code will crash intentionally if it doesn't. */
2242 for (p
= &v
->locs
; ; p
= &(*p
)->next
)
2246 if (REG_P (x
) && REGNO (x
) == i
)
2248 unchain_one_elt_loc_list (p
);
2253 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
2255 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
2256 n_useless_debug_values
++;
2264 /* Invalidate any locations in the table which are changed because of a
2265 store to MEM_RTX. If this is called because of a non-const call
2266 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2269 cselib_invalidate_mem (rtx mem_rtx
)
2271 cselib_val
**vp
, *v
, *next
;
2275 mem_addr
= canon_rtx (get_addr (XEXP (mem_rtx
, 0)));
2276 mem_rtx
= canon_rtx (mem_rtx
);
2278 vp
= &first_containing_mem
;
2279 for (v
= *vp
; v
!= &dummy_val
; v
= next
)
2281 bool has_mem
= false;
2282 struct elt_loc_list
**p
= &v
->locs
;
2283 bool had_locs
= v
->locs
!= NULL
;
2284 rtx_insn
*setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
2290 struct elt_list
**mem_chain
;
2292 /* MEMs may occur in locations only at the top level; below
2293 that every MEM or REG is substituted by its VALUE. */
2299 if (num_mems
< PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS
)
2300 && ! canon_anti_dependence (x
, false, mem_rtx
,
2301 GET_MODE (mem_rtx
), mem_addr
))
2309 /* This one overlaps. */
2310 /* We must have a mapping from this MEM's address to the
2311 value (E). Remove that, too. */
2312 addr
= cselib_lookup (XEXP (x
, 0), VOIDmode
, 0, GET_MODE (x
));
2313 addr
= canonical_cselib_val (addr
);
2314 gcc_checking_assert (v
== canonical_cselib_val (v
));
2315 mem_chain
= &addr
->addr_list
;
2318 cselib_val
*canon
= canonical_cselib_val ((*mem_chain
)->elt
);
2322 unchain_one_elt_list (mem_chain
);
2326 /* Record canonicalized elt. */
2327 (*mem_chain
)->elt
= canon
;
2329 mem_chain
= &(*mem_chain
)->next
;
2332 unchain_one_elt_loc_list (p
);
2335 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
2337 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
2338 n_useless_debug_values
++;
2343 next
= v
->next_containing_mem
;
2347 vp
= &(*vp
)->next_containing_mem
;
2350 v
->next_containing_mem
= NULL
;
2355 /* Invalidate DEST, which is being assigned to or clobbered. */
2358 cselib_invalidate_rtx (rtx dest
)
2360 while (GET_CODE (dest
) == SUBREG
2361 || GET_CODE (dest
) == ZERO_EXTRACT
2362 || GET_CODE (dest
) == STRICT_LOW_PART
)
2363 dest
= XEXP (dest
, 0);
2366 cselib_invalidate_regno (REGNO (dest
), GET_MODE (dest
));
2367 else if (MEM_P (dest
))
2368 cselib_invalidate_mem (dest
);
2371 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2374 cselib_invalidate_rtx_note_stores (rtx dest
, const_rtx ignore ATTRIBUTE_UNUSED
,
2375 void *data ATTRIBUTE_UNUSED
)
2377 cselib_invalidate_rtx (dest
);
2380 /* Record the result of a SET instruction. DEST is being set; the source
2381 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2382 describes its address. */
2385 cselib_record_set (rtx dest
, cselib_val
*src_elt
, cselib_val
*dest_addr_elt
)
2387 if (src_elt
== 0 || side_effects_p (dest
))
2392 unsigned int dreg
= REGNO (dest
);
2393 if (dreg
< FIRST_PSEUDO_REGISTER
)
2395 unsigned int n
= REG_NREGS (dest
);
2397 if (n
> max_value_regs
)
2401 if (REG_VALUES (dreg
) == 0)
2403 used_regs
[n_used_regs
++] = dreg
;
2404 REG_VALUES (dreg
) = new_elt_list (REG_VALUES (dreg
), src_elt
);
2408 /* The register should have been invalidated. */
2409 gcc_assert (REG_VALUES (dreg
)->elt
== 0);
2410 REG_VALUES (dreg
)->elt
= src_elt
;
2413 if (src_elt
->locs
== 0 && !PRESERVED_VALUE_P (src_elt
->val_rtx
))
2415 new_elt_loc_list (src_elt
, dest
);
2417 else if (MEM_P (dest
) && dest_addr_elt
!= 0
2418 && cselib_record_memory
)
2420 if (src_elt
->locs
== 0 && !PRESERVED_VALUE_P (src_elt
->val_rtx
))
2422 add_mem_for_addr (dest_addr_elt
, src_elt
, dest
);
2426 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2429 cselib_add_permanent_equiv (cselib_val
*elt
, rtx x
, rtx_insn
*insn
)
2432 rtx_insn
*save_cselib_current_insn
= cselib_current_insn
;
2434 gcc_checking_assert (elt
);
2435 gcc_checking_assert (PRESERVED_VALUE_P (elt
->val_rtx
));
2436 gcc_checking_assert (!side_effects_p (x
));
2438 cselib_current_insn
= insn
;
2440 nelt
= cselib_lookup (x
, GET_MODE (elt
->val_rtx
), 1, VOIDmode
);
2444 cselib_any_perm_equivs
= true;
2446 if (!PRESERVED_VALUE_P (nelt
->val_rtx
))
2447 cselib_preserve_value (nelt
);
2449 new_elt_loc_list (nelt
, elt
->val_rtx
);
2452 cselib_current_insn
= save_cselib_current_insn
;
2455 /* Return TRUE if any permanent equivalences have been recorded since
2456 the table was last initialized. */
2458 cselib_have_permanent_equivalences (void)
2460 return cselib_any_perm_equivs
;
2463 /* There is no good way to determine how many elements there can be
2464 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2465 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2467 struct cselib_record_autoinc_data
2469 struct cselib_set
*sets
;
2473 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2474 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2477 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED
, rtx op ATTRIBUTE_UNUSED
,
2478 rtx dest
, rtx src
, rtx srcoff
, void *arg
)
2480 struct cselib_record_autoinc_data
*data
;
2481 data
= (struct cselib_record_autoinc_data
*)arg
;
2483 data
->sets
[data
->n_sets
].dest
= dest
;
2486 data
->sets
[data
->n_sets
].src
= gen_rtx_PLUS (GET_MODE (src
), src
, srcoff
);
2488 data
->sets
[data
->n_sets
].src
= src
;
2495 /* Record the effects of any sets and autoincs in INSN. */
2497 cselib_record_sets (rtx_insn
*insn
)
2501 struct cselib_set sets
[MAX_SETS
];
2502 rtx body
= PATTERN (insn
);
2504 int n_sets_before_autoinc
;
2505 struct cselib_record_autoinc_data data
;
2507 body
= PATTERN (insn
);
2508 if (GET_CODE (body
) == COND_EXEC
)
2510 cond
= COND_EXEC_TEST (body
);
2511 body
= COND_EXEC_CODE (body
);
2514 /* Find all sets. */
2515 if (GET_CODE (body
) == SET
)
2517 sets
[0].src
= SET_SRC (body
);
2518 sets
[0].dest
= SET_DEST (body
);
2521 else if (GET_CODE (body
) == PARALLEL
)
2523 /* Look through the PARALLEL and record the values being
2524 set, if possible. Also handle any CLOBBERs. */
2525 for (i
= XVECLEN (body
, 0) - 1; i
>= 0; --i
)
2527 rtx x
= XVECEXP (body
, 0, i
);
2529 if (GET_CODE (x
) == SET
)
2531 sets
[n_sets
].src
= SET_SRC (x
);
2532 sets
[n_sets
].dest
= SET_DEST (x
);
2539 && MEM_P (sets
[0].src
)
2540 && !cselib_record_memory
2541 && MEM_READONLY_P (sets
[0].src
))
2543 rtx note
= find_reg_equal_equiv_note (insn
);
2545 if (note
&& CONSTANT_P (XEXP (note
, 0)))
2546 sets
[0].src
= XEXP (note
, 0);
2550 data
.n_sets
= n_sets_before_autoinc
= n_sets
;
2551 for_each_inc_dec (PATTERN (insn
), cselib_record_autoinc_cb
, &data
);
2552 n_sets
= data
.n_sets
;
2554 /* Look up the values that are read. Do this before invalidating the
2555 locations that are written. */
2556 for (i
= 0; i
< n_sets
; i
++)
2558 rtx dest
= sets
[i
].dest
;
2560 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2561 the low part after invalidating any knowledge about larger modes. */
2562 if (GET_CODE (sets
[i
].dest
) == STRICT_LOW_PART
)
2563 sets
[i
].dest
= dest
= XEXP (dest
, 0);
2565 /* We don't know how to record anything but REG or MEM. */
2567 || (MEM_P (dest
) && cselib_record_memory
))
2569 rtx src
= sets
[i
].src
;
2571 src
= gen_rtx_IF_THEN_ELSE (GET_MODE (dest
), cond
, src
, dest
);
2572 sets
[i
].src_elt
= cselib_lookup (src
, GET_MODE (dest
), 1, VOIDmode
);
2575 machine_mode address_mode
= get_address_mode (dest
);
2577 sets
[i
].dest_addr_elt
= cselib_lookup (XEXP (dest
, 0),
2582 sets
[i
].dest_addr_elt
= 0;
2586 if (cselib_record_sets_hook
)
2587 cselib_record_sets_hook (insn
, sets
, n_sets
);
2589 /* Invalidate all locations written by this insn. Note that the elts we
2590 looked up in the previous loop aren't affected, just some of their
2591 locations may go away. */
2592 note_stores (body
, cselib_invalidate_rtx_note_stores
, NULL
);
2594 for (i
= n_sets_before_autoinc
; i
< n_sets
; i
++)
2595 cselib_invalidate_rtx (sets
[i
].dest
);
2597 /* If this is an asm, look for duplicate sets. This can happen when the
2598 user uses the same value as an output multiple times. This is valid
2599 if the outputs are not actually used thereafter. Treat this case as
2600 if the value isn't actually set. We do this by smashing the destination
2601 to pc_rtx, so that we won't record the value later. */
2602 if (n_sets
>= 2 && asm_noperands (body
) >= 0)
2604 for (i
= 0; i
< n_sets
; i
++)
2606 rtx dest
= sets
[i
].dest
;
2607 if (REG_P (dest
) || MEM_P (dest
))
2610 for (j
= i
+ 1; j
< n_sets
; j
++)
2611 if (rtx_equal_p (dest
, sets
[j
].dest
))
2613 sets
[i
].dest
= pc_rtx
;
2614 sets
[j
].dest
= pc_rtx
;
2620 /* Now enter the equivalences in our tables. */
2621 for (i
= 0; i
< n_sets
; i
++)
2623 rtx dest
= sets
[i
].dest
;
2625 || (MEM_P (dest
) && cselib_record_memory
))
2626 cselib_record_set (dest
, sets
[i
].src_elt
, sets
[i
].dest_addr_elt
);
2630 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
2633 fp_setter_insn (rtx_insn
*insn
)
2635 rtx expr
, pat
= NULL_RTX
;
2637 if (!RTX_FRAME_RELATED_P (insn
))
2640 expr
= find_reg_note (insn
, REG_FRAME_RELATED_EXPR
, NULL_RTX
);
2642 pat
= XEXP (expr
, 0);
2643 if (!modified_in_p (hard_frame_pointer_rtx
, pat
? pat
: insn
))
2646 /* Don't return true for frame pointer restores in the epilogue. */
2647 if (find_reg_note (insn
, REG_CFA_RESTORE
, hard_frame_pointer_rtx
))
2652 /* Record the effects of INSN. */
2655 cselib_process_insn (rtx_insn
*insn
)
2660 cselib_current_insn
= insn
;
2662 /* Forget everything at a CODE_LABEL or a setjmp. */
2665 && find_reg_note (insn
, REG_SETJMP
, NULL
)))
2666 && !cselib_preserve_constants
)
2668 cselib_reset_table (next_uid
);
2669 cselib_current_insn
= NULL
;
2673 if (! INSN_P (insn
))
2675 cselib_current_insn
= NULL
;
2679 /* If this is a call instruction, forget anything stored in a
2680 call clobbered register, or, if this is not a const call, in
2684 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
2685 if (call_used_regs
[i
]
2686 || (REG_VALUES (i
) && REG_VALUES (i
)->elt
2687 && HARD_REGNO_CALL_PART_CLOBBERED (i
,
2688 GET_MODE (REG_VALUES (i
)->elt
->val_rtx
))))
2689 cselib_invalidate_regno (i
, reg_raw_mode
[i
]);
2691 /* Since it is not clear how cselib is going to be used, be
2692 conservative here and treat looping pure or const functions
2693 as if they were regular functions. */
2694 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn
)
2695 || !(RTL_CONST_OR_PURE_CALL_P (insn
)))
2696 cselib_invalidate_mem (callmem
);
2699 cselib_record_sets (insn
);
2701 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2702 after we have processed the insn. */
2705 for (x
= CALL_INSN_FUNCTION_USAGE (insn
); x
; x
= XEXP (x
, 1))
2706 if (GET_CODE (XEXP (x
, 0)) == CLOBBER
)
2707 cselib_invalidate_rtx (XEXP (XEXP (x
, 0), 0));
2708 /* Flush evertything on setjmp. */
2709 if (cselib_preserve_constants
2710 && find_reg_note (insn
, REG_SETJMP
, NULL
))
2712 cselib_preserve_only_values ();
2713 cselib_reset_table (next_uid
);
2717 /* On setter of the hard frame pointer if frame_pointer_needed,
2718 invalidate stack_pointer_rtx, so that sp and {,h}fp based
2719 VALUEs are distinct. */
2720 if (reload_completed
2721 && frame_pointer_needed
2722 && fp_setter_insn (insn
))
2723 cselib_invalidate_rtx (stack_pointer_rtx
);
2725 cselib_current_insn
= NULL
;
2727 if (n_useless_values
> MAX_USELESS_VALUES
2728 /* remove_useless_values is linear in the hash table size. Avoid
2729 quadratic behavior for very large hashtables with very few
2730 useless elements. */
2731 && ((unsigned int)n_useless_values
2732 > (cselib_hash_table
->elements () - n_debug_values
) / 4))
2733 remove_useless_values ();
2736 /* Initialize cselib for one pass. The caller must also call
2737 init_alias_analysis. */
2740 cselib_init (int record_what
)
2742 cselib_record_memory
= record_what
& CSELIB_RECORD_MEMORY
;
2743 cselib_preserve_constants
= record_what
& CSELIB_PRESERVE_CONSTANTS
;
2744 cselib_any_perm_equivs
= false;
2746 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2747 see canon_true_dependence. This is only created once. */
2749 callmem
= gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
));
2751 cselib_nregs
= max_reg_num ();
2753 /* We preserve reg_values to allow expensive clearing of the whole thing.
2754 Reallocate it however if it happens to be too large. */
2755 if (!reg_values
|| reg_values_size
< cselib_nregs
2756 || (reg_values_size
> 10 && reg_values_size
> cselib_nregs
* 4))
2759 /* Some space for newly emit instructions so we don't end up
2760 reallocating in between passes. */
2761 reg_values_size
= cselib_nregs
+ (63 + cselib_nregs
) / 16;
2762 reg_values
= XCNEWVEC (struct elt_list
*, reg_values_size
);
2764 used_regs
= XNEWVEC (unsigned int, cselib_nregs
);
2766 cselib_hash_table
= new hash_table
<cselib_hasher
> (31);
2767 if (cselib_preserve_constants
)
2768 cselib_preserved_hash_table
= new hash_table
<cselib_hasher
> (31);
2772 /* Called when the current user is done with cselib. */
2775 cselib_finish (void)
2777 bool preserved
= cselib_preserve_constants
;
2778 cselib_discard_hook
= NULL
;
2779 cselib_preserve_constants
= false;
2780 cselib_any_perm_equivs
= false;
2781 cfa_base_preserved_val
= NULL
;
2782 cfa_base_preserved_regno
= INVALID_REGNUM
;
2783 elt_list::pool
.release ();
2784 elt_loc_list::pool
.release ();
2785 cselib_val::pool
.release ();
2786 value_pool
.release ();
2787 cselib_clear_table ();
2788 delete cselib_hash_table
;
2789 cselib_hash_table
= NULL
;
2791 delete cselib_preserved_hash_table
;
2792 cselib_preserved_hash_table
= NULL
;
2795 n_useless_values
= 0;
2796 n_useless_debug_values
= 0;
2801 /* Dump the cselib_val *X to FILE *OUT. */
2804 dump_cselib_val (cselib_val
**x
, FILE *out
)
2807 bool need_lf
= true;
2809 print_inline_rtx (out
, v
->val_rtx
, 0);
2813 struct elt_loc_list
*l
= v
->locs
;
2819 fputs (" locs:", out
);
2822 if (l
->setting_insn
)
2823 fprintf (out
, "\n from insn %i ",
2824 INSN_UID (l
->setting_insn
));
2826 fprintf (out
, "\n ");
2827 print_inline_rtx (out
, l
->loc
, 4);
2829 while ((l
= l
->next
));
2834 fputs (" no locs", out
);
2840 struct elt_list
*e
= v
->addr_list
;
2846 fputs (" addr list:", out
);
2850 print_inline_rtx (out
, e
->elt
->val_rtx
, 2);
2852 while ((e
= e
->next
));
2857 fputs (" no addrs", out
);
2861 if (v
->next_containing_mem
== &dummy_val
)
2862 fputs (" last mem\n", out
);
2863 else if (v
->next_containing_mem
)
2865 fputs (" next mem ", out
);
2866 print_inline_rtx (out
, v
->next_containing_mem
->val_rtx
, 2);
2875 /* Dump to OUT everything in the CSELIB table. */
2878 dump_cselib_table (FILE *out
)
2880 fprintf (out
, "cselib hash table:\n");
2881 cselib_hash_table
->traverse
<FILE *, dump_cselib_val
> (out
);
2882 fprintf (out
, "cselib preserved hash table:\n");
2883 cselib_preserved_hash_table
->traverse
<FILE *, dump_cselib_val
> (out
);
2884 if (first_containing_mem
!= &dummy_val
)
2886 fputs ("first mem ", out
);
2887 print_inline_rtx (out
, first_containing_mem
->val_rtx
, 2);
2890 fprintf (out
, "next uid %i\n", next_uid
);
2893 #include "gt-cselib.h"