1 /* Common subexpression elimination library for GNU compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
4 2012 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
30 #include "hard-reg-set.h"
32 #include "insn-config.h"
36 #include "diagnostic-core.h"
40 #include "tree-pass.h"
43 #include "alloc-pool.h"
47 /* A list of cselib_val structures. */
49 struct elt_list
*next
;
53 static bool cselib_record_memory
;
54 static bool cselib_preserve_constants
;
55 static int entry_and_rtx_equal_p (const void *, const void *);
56 static hashval_t
get_value_hash (const void *);
57 static struct elt_list
*new_elt_list (struct elt_list
*, cselib_val
*);
58 static void new_elt_loc_list (cselib_val
*, rtx
);
59 static void unchain_one_value (cselib_val
*);
60 static void unchain_one_elt_list (struct elt_list
**);
61 static void unchain_one_elt_loc_list (struct elt_loc_list
**);
62 static int discard_useless_locs (void **, void *);
63 static int discard_useless_values (void **, void *);
64 static void remove_useless_values (void);
65 static int rtx_equal_for_cselib_1 (rtx
, rtx
, enum machine_mode
);
66 static unsigned int cselib_hash_rtx (rtx
, int, enum machine_mode
);
67 static cselib_val
*new_cselib_val (unsigned int, enum machine_mode
, rtx
);
68 static void add_mem_for_addr (cselib_val
*, cselib_val
*, rtx
);
69 static cselib_val
*cselib_lookup_mem (rtx
, int);
70 static void cselib_invalidate_regno (unsigned int, enum machine_mode
);
71 static void cselib_invalidate_mem (rtx
);
72 static void cselib_record_set (rtx
, cselib_val
*, cselib_val
*);
73 static void cselib_record_sets (rtx
);
75 struct expand_value_data
78 cselib_expand_callback callback
;
83 static rtx
cselib_expand_value_rtx_1 (rtx
, struct expand_value_data
*, int);
85 /* There are three ways in which cselib can look up an rtx:
86 - for a REG, the reg_values table (which is indexed by regno) is used
87 - for a MEM, we recursively look up its address and then follow the
88 addr_list of that value
89 - for everything else, we compute a hash value and go through the hash
90 table. Since different rtx's can still have the same hash value,
91 this involves walking the table entries for a given value and comparing
92 the locations of the entries with the rtx we are looking up. */
94 /* A table that enables us to look up elts by their value. */
95 static htab_t cselib_hash_table
;
97 /* This is a global so we don't have to pass this through every function.
98 It is used in new_elt_loc_list to set SETTING_INSN. */
99 static rtx cselib_current_insn
;
101 /* The unique id that the next create value will take. */
102 static unsigned int next_uid
;
104 /* The number of registers we had when the varrays were last resized. */
105 static unsigned int cselib_nregs
;
107 /* Count values without known locations, or with only locations that
108 wouldn't have been known except for debug insns. Whenever this
109 grows too big, we remove these useless values from the table.
111 Counting values with only debug values is a bit tricky. We don't
112 want to increment n_useless_values when we create a value for a
113 debug insn, for this would get n_useless_values out of sync, but we
114 want increment it if all locs in the list that were ever referenced
115 in nondebug insns are removed from the list.
117 In the general case, once we do that, we'd have to stop accepting
118 nondebug expressions in the loc list, to avoid having two values
119 equivalent that, without debug insns, would have been made into
120 separate values. However, because debug insns never introduce
121 equivalences themselves (no assignments), the only means for
122 growing loc lists is through nondebug assignments. If the locs
123 also happen to be referenced in debug insns, it will work just fine.
125 A consequence of this is that there's at most one debug-only loc in
126 each loc list. If we keep it in the first entry, testing whether
127 we have a debug-only loc list takes O(1).
129 Furthermore, since any additional entry in a loc list containing a
130 debug loc would have to come from an assignment (nondebug) that
131 references both the initial debug loc and the newly-equivalent loc,
132 the initial debug loc would be promoted to a nondebug loc, and the
133 loc list would not contain debug locs any more.
135 So the only case we have to be careful with in order to keep
136 n_useless_values in sync between debug and nondebug compilations is
137 to avoid incrementing n_useless_values when removing the single loc
138 from a value that turns out to not appear outside debug values. We
139 increment n_useless_debug_values instead, and leave such values
140 alone until, for other reasons, we garbage-collect useless
142 static int n_useless_values
;
143 static int n_useless_debug_values
;
145 /* Count values whose locs have been taken exclusively from debug
146 insns for the entire life of the value. */
147 static int n_debug_values
;
149 /* Number of useless values before we remove them from the hash table. */
150 #define MAX_USELESS_VALUES 32
152 /* This table maps from register number to values. It does not
153 contain pointers to cselib_val structures, but rather elt_lists.
154 The purpose is to be able to refer to the same register in
155 different modes. The first element of the list defines the mode in
156 which the register was set; if the mode is unknown or the value is
157 no longer valid in that mode, ELT will be NULL for the first
159 static struct elt_list
**reg_values
;
160 static unsigned int reg_values_size
;
161 #define REG_VALUES(i) reg_values[i]
163 /* The largest number of hard regs used by any entry added to the
164 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
165 static unsigned int max_value_regs
;
167 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
168 in cselib_clear_table() for fast emptying. */
169 static unsigned int *used_regs
;
170 static unsigned int n_used_regs
;
172 /* We pass this to cselib_invalidate_mem to invalidate all of
173 memory for a non-const call instruction. */
174 static GTY(()) rtx callmem
;
176 /* Set by discard_useless_locs if it deleted the last location of any
178 static int values_became_useless
;
180 /* Used as stop element of the containing_mem list so we can check
181 presence in the list by checking the next pointer. */
182 static cselib_val dummy_val
;
184 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
185 that is constant through the whole function and should never be
187 static cselib_val
*cfa_base_preserved_val
;
188 static unsigned int cfa_base_preserved_regno
= INVALID_REGNUM
;
190 /* Used to list all values that contain memory reference.
191 May or may not contain the useless values - the list is compacted
192 each time memory is invalidated. */
193 static cselib_val
*first_containing_mem
= &dummy_val
;
194 static alloc_pool elt_loc_list_pool
, elt_list_pool
, cselib_val_pool
, value_pool
;
196 /* If nonnull, cselib will call this function before freeing useless
197 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
198 void (*cselib_discard_hook
) (cselib_val
*);
200 /* If nonnull, cselib will call this function before recording sets or
201 even clobbering outputs of INSN. All the recorded sets will be
202 represented in the array sets[n_sets]. new_val_min can be used to
203 tell whether values present in sets are introduced by this
205 void (*cselib_record_sets_hook
) (rtx insn
, struct cselib_set
*sets
,
208 #define PRESERVED_VALUE_P(RTX) \
209 (RTL_FLAG_CHECK1("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
213 /* Allocate a struct elt_list and fill in its two elements with the
216 static inline struct elt_list
*
217 new_elt_list (struct elt_list
*next
, cselib_val
*elt
)
220 el
= (struct elt_list
*) pool_alloc (elt_list_pool
);
226 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
230 new_elt_loc_list (cselib_val
*val
, rtx loc
)
232 struct elt_loc_list
*el
, *next
= val
->locs
;
234 gcc_checking_assert (!next
|| !next
->setting_insn
235 || !DEBUG_INSN_P (next
->setting_insn
)
236 || cselib_current_insn
== next
->setting_insn
);
238 /* If we're creating the first loc in a debug insn context, we've
239 just created a debug value. Count it. */
240 if (!next
&& cselib_current_insn
&& DEBUG_INSN_P (cselib_current_insn
))
243 val
= canonical_cselib_val (val
);
246 if (GET_CODE (loc
) == VALUE
)
248 loc
= canonical_cselib_val (CSELIB_VAL_PTR (loc
))->val_rtx
;
250 gcc_checking_assert (PRESERVED_VALUE_P (loc
)
251 == PRESERVED_VALUE_P (val
->val_rtx
));
253 if (val
->val_rtx
== loc
)
255 else if (val
->uid
> CSELIB_VAL_PTR (loc
)->uid
)
257 /* Reverse the insertion. */
258 new_elt_loc_list (CSELIB_VAL_PTR (loc
), val
->val_rtx
);
262 gcc_checking_assert (val
->uid
< CSELIB_VAL_PTR (loc
)->uid
);
264 if (CSELIB_VAL_PTR (loc
)->locs
)
266 /* Bring all locs from LOC to VAL. */
267 for (el
= CSELIB_VAL_PTR (loc
)->locs
; el
->next
; el
= el
->next
)
269 /* Adjust values that have LOC as canonical so that VAL
270 becomes their canonical. */
271 if (el
->loc
&& GET_CODE (el
->loc
) == VALUE
)
273 gcc_checking_assert (CSELIB_VAL_PTR (el
->loc
)->locs
->loc
275 CSELIB_VAL_PTR (el
->loc
)->locs
->loc
= val
->val_rtx
;
278 el
->next
= val
->locs
;
279 next
= val
->locs
= CSELIB_VAL_PTR (loc
)->locs
;
282 if (CSELIB_VAL_PTR (loc
)->addr_list
)
284 /* Bring in addr_list into canonical node. */
285 struct elt_list
*last
= CSELIB_VAL_PTR (loc
)->addr_list
;
288 last
->next
= val
->addr_list
;
289 val
->addr_list
= CSELIB_VAL_PTR (loc
)->addr_list
;
290 CSELIB_VAL_PTR (loc
)->addr_list
= NULL
;
293 if (CSELIB_VAL_PTR (loc
)->next_containing_mem
!= NULL
294 && val
->next_containing_mem
== NULL
)
296 /* Add VAL to the containing_mem list after LOC. LOC will
297 be removed when we notice it doesn't contain any
299 val
->next_containing_mem
= CSELIB_VAL_PTR (loc
)->next_containing_mem
;
300 CSELIB_VAL_PTR (loc
)->next_containing_mem
= val
;
303 /* Chain LOC back to VAL. */
304 el
= (struct elt_loc_list
*) pool_alloc (elt_loc_list_pool
);
305 el
->loc
= val
->val_rtx
;
306 el
->setting_insn
= cselib_current_insn
;
308 CSELIB_VAL_PTR (loc
)->locs
= el
;
311 el
= (struct elt_loc_list
*) pool_alloc (elt_loc_list_pool
);
313 el
->setting_insn
= cselib_current_insn
;
318 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
319 originating from a debug insn, maintaining the debug values
323 promote_debug_loc (struct elt_loc_list
*l
)
325 if (l
->setting_insn
&& DEBUG_INSN_P (l
->setting_insn
)
326 && (!cselib_current_insn
|| !DEBUG_INSN_P (cselib_current_insn
)))
329 l
->setting_insn
= cselib_current_insn
;
330 if (cselib_preserve_constants
&& l
->next
)
332 gcc_assert (l
->next
->setting_insn
333 && DEBUG_INSN_P (l
->next
->setting_insn
)
335 l
->next
->setting_insn
= cselib_current_insn
;
338 gcc_assert (!l
->next
);
342 /* The elt_list at *PL is no longer needed. Unchain it and free its
346 unchain_one_elt_list (struct elt_list
**pl
)
348 struct elt_list
*l
= *pl
;
351 pool_free (elt_list_pool
, l
);
354 /* Likewise for elt_loc_lists. */
357 unchain_one_elt_loc_list (struct elt_loc_list
**pl
)
359 struct elt_loc_list
*l
= *pl
;
362 pool_free (elt_loc_list_pool
, l
);
365 /* Likewise for cselib_vals. This also frees the addr_list associated with
369 unchain_one_value (cselib_val
*v
)
372 unchain_one_elt_list (&v
->addr_list
);
374 pool_free (cselib_val_pool
, v
);
377 /* Remove all entries from the hash table. Also used during
381 cselib_clear_table (void)
383 cselib_reset_table (1);
386 /* Remove from hash table all VALUEs except constants
387 and function invariants. */
390 preserve_only_constants (void **x
, void *info ATTRIBUTE_UNUSED
)
392 cselib_val
*v
= (cselib_val
*)*x
;
393 struct elt_loc_list
*l
;
396 && v
->locs
->next
== NULL
)
398 if (CONSTANT_P (v
->locs
->loc
)
399 && (GET_CODE (v
->locs
->loc
) != CONST
400 || !references_value_p (v
->locs
->loc
, 0)))
402 /* Although a debug expr may be bound to different expressions,
403 we can preserve it as if it was constant, to get unification
404 and proper merging within var-tracking. */
405 if (GET_CODE (v
->locs
->loc
) == DEBUG_EXPR
406 || GET_CODE (v
->locs
->loc
) == DEBUG_IMPLICIT_PTR
407 || GET_CODE (v
->locs
->loc
) == ENTRY_VALUE
408 || GET_CODE (v
->locs
->loc
) == DEBUG_PARAMETER_REF
)
410 if (cfa_base_preserved_val
)
412 if (v
== cfa_base_preserved_val
)
414 if (GET_CODE (v
->locs
->loc
) == PLUS
415 && CONST_INT_P (XEXP (v
->locs
->loc
, 1))
416 && XEXP (v
->locs
->loc
, 0) == cfa_base_preserved_val
->val_rtx
)
421 /* Keep VALUE equivalences around. */
422 for (l
= v
->locs
; l
; l
= l
->next
)
423 if (GET_CODE (l
->loc
) == VALUE
)
426 htab_clear_slot (cselib_hash_table
, x
);
430 /* Remove all entries from the hash table, arranging for the next
431 value to be numbered NUM. */
434 cselib_reset_table (unsigned int num
)
440 if (cfa_base_preserved_val
)
442 unsigned int regno
= cfa_base_preserved_regno
;
443 unsigned int new_used_regs
= 0;
444 for (i
= 0; i
< n_used_regs
; i
++)
445 if (used_regs
[i
] == regno
)
451 REG_VALUES (used_regs
[i
]) = 0;
452 gcc_assert (new_used_regs
== 1);
453 n_used_regs
= new_used_regs
;
454 used_regs
[0] = regno
;
456 = hard_regno_nregs
[regno
][GET_MODE (cfa_base_preserved_val
->locs
->loc
)];
460 for (i
= 0; i
< n_used_regs
; i
++)
461 REG_VALUES (used_regs
[i
]) = 0;
465 if (cselib_preserve_constants
)
466 htab_traverse (cselib_hash_table
, preserve_only_constants
, NULL
);
468 htab_empty (cselib_hash_table
);
470 n_useless_values
= 0;
471 n_useless_debug_values
= 0;
476 first_containing_mem
= &dummy_val
;
479 /* Return the number of the next value that will be generated. */
482 cselib_get_next_uid (void)
487 /* See the documentation of cselib_find_slot below. */
488 static enum machine_mode find_slot_memmode
;
490 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
491 INSERTing if requested. When X is part of the address of a MEM,
492 MEMMODE should specify the mode of the MEM. While searching the
493 table, MEMMODE is held in FIND_SLOT_MEMMODE, so that autoinc RTXs
494 in X can be resolved. */
497 cselib_find_slot (rtx x
, hashval_t hash
, enum insert_option insert
,
498 enum machine_mode memmode
)
501 find_slot_memmode
= memmode
;
502 slot
= htab_find_slot_with_hash (cselib_hash_table
, x
, hash
, insert
);
503 find_slot_memmode
= VOIDmode
;
507 /* The equality test for our hash table. The first argument ENTRY is a table
508 element (i.e. a cselib_val), while the second arg X is an rtx. We know
509 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
510 CONST of an appropriate mode. */
513 entry_and_rtx_equal_p (const void *entry
, const void *x_arg
)
515 struct elt_loc_list
*l
;
516 const cselib_val
*const v
= (const cselib_val
*) entry
;
517 rtx x
= CONST_CAST_RTX ((const_rtx
)x_arg
);
518 enum machine_mode mode
= GET_MODE (x
);
520 gcc_assert (!CONST_INT_P (x
) && GET_CODE (x
) != CONST_FIXED
521 && (mode
!= VOIDmode
|| GET_CODE (x
) != CONST_DOUBLE
));
523 if (mode
!= GET_MODE (v
->val_rtx
))
526 /* Unwrap X if necessary. */
527 if (GET_CODE (x
) == CONST
528 && (CONST_INT_P (XEXP (x
, 0))
529 || GET_CODE (XEXP (x
, 0)) == CONST_FIXED
530 || GET_CODE (XEXP (x
, 0)) == CONST_DOUBLE
))
533 /* We don't guarantee that distinct rtx's have different hash values,
534 so we need to do a comparison. */
535 for (l
= v
->locs
; l
; l
= l
->next
)
536 if (rtx_equal_for_cselib_1 (l
->loc
, x
, find_slot_memmode
))
538 promote_debug_loc (l
);
545 /* The hash function for our hash table. The value is always computed with
546 cselib_hash_rtx when adding an element; this function just extracts the
547 hash value from a cselib_val structure. */
550 get_value_hash (const void *entry
)
552 const cselib_val
*const v
= (const cselib_val
*) entry
;
556 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
557 only return true for values which point to a cselib_val whose value
558 element has been set to zero, which implies the cselib_val will be
562 references_value_p (const_rtx x
, int only_useless
)
564 const enum rtx_code code
= GET_CODE (x
);
565 const char *fmt
= GET_RTX_FORMAT (code
);
568 if (GET_CODE (x
) == VALUE
569 && (! only_useless
||
570 (CSELIB_VAL_PTR (x
)->locs
== 0 && !PRESERVED_VALUE_P (x
))))
573 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
575 if (fmt
[i
] == 'e' && references_value_p (XEXP (x
, i
), only_useless
))
577 else if (fmt
[i
] == 'E')
578 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
579 if (references_value_p (XVECEXP (x
, i
, j
), only_useless
))
586 /* For all locations found in X, delete locations that reference useless
587 values (i.e. values without any location). Called through
591 discard_useless_locs (void **x
, void *info ATTRIBUTE_UNUSED
)
593 cselib_val
*v
= (cselib_val
*)*x
;
594 struct elt_loc_list
**p
= &v
->locs
;
595 bool had_locs
= v
->locs
!= NULL
;
596 rtx setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
600 if (references_value_p ((*p
)->loc
, 1))
601 unchain_one_elt_loc_list (p
);
606 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
608 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
609 n_useless_debug_values
++;
612 values_became_useless
= 1;
617 /* If X is a value with no locations, remove it from the hashtable. */
620 discard_useless_values (void **x
, void *info ATTRIBUTE_UNUSED
)
622 cselib_val
*v
= (cselib_val
*)*x
;
624 if (v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
626 if (cselib_discard_hook
)
627 cselib_discard_hook (v
);
629 CSELIB_VAL_PTR (v
->val_rtx
) = NULL
;
630 htab_clear_slot (cselib_hash_table
, x
);
631 unchain_one_value (v
);
638 /* Clean out useless values (i.e. those which no longer have locations
639 associated with them) from the hash table. */
642 remove_useless_values (void)
646 /* First pass: eliminate locations that reference the value. That in
647 turn can make more values useless. */
650 values_became_useless
= 0;
651 htab_traverse (cselib_hash_table
, discard_useless_locs
, 0);
653 while (values_became_useless
);
655 /* Second pass: actually remove the values. */
657 p
= &first_containing_mem
;
658 for (v
= *p
; v
!= &dummy_val
; v
= v
->next_containing_mem
)
659 if (v
->locs
&& v
== canonical_cselib_val (v
))
662 p
= &(*p
)->next_containing_mem
;
666 n_useless_values
+= n_useless_debug_values
;
667 n_debug_values
-= n_useless_debug_values
;
668 n_useless_debug_values
= 0;
670 htab_traverse (cselib_hash_table
, discard_useless_values
, 0);
672 gcc_assert (!n_useless_values
);
675 /* Arrange for a value to not be removed from the hash table even if
676 it becomes useless. */
679 cselib_preserve_value (cselib_val
*v
)
681 PRESERVED_VALUE_P (v
->val_rtx
) = 1;
684 /* Test whether a value is preserved. */
687 cselib_preserved_value_p (cselib_val
*v
)
689 return PRESERVED_VALUE_P (v
->val_rtx
);
692 /* Arrange for a REG value to be assumed constant through the whole function,
693 never invalidated and preserved across cselib_reset_table calls. */
696 cselib_preserve_cfa_base_value (cselib_val
*v
, unsigned int regno
)
698 if (cselib_preserve_constants
700 && REG_P (v
->locs
->loc
))
702 cfa_base_preserved_val
= v
;
703 cfa_base_preserved_regno
= regno
;
707 /* Clean all non-constant expressions in the hash table, but retain
711 cselib_preserve_only_values (void)
715 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
716 cselib_invalidate_regno (i
, reg_raw_mode
[i
]);
718 cselib_invalidate_mem (callmem
);
720 remove_useless_values ();
722 gcc_assert (first_containing_mem
== &dummy_val
);
725 /* Return the mode in which a register was last set. If X is not a
726 register, return its mode. If the mode in which the register was
727 set is not known, or the value was already clobbered, return
731 cselib_reg_set_mode (const_rtx x
)
736 if (REG_VALUES (REGNO (x
)) == NULL
737 || REG_VALUES (REGNO (x
))->elt
== NULL
)
740 return GET_MODE (REG_VALUES (REGNO (x
))->elt
->val_rtx
);
743 /* Return nonzero if we can prove that X and Y contain the same value, taking
744 our gathered information into account. */
747 rtx_equal_for_cselib_p (rtx x
, rtx y
)
749 return rtx_equal_for_cselib_1 (x
, y
, VOIDmode
);
752 /* If x is a PLUS or an autoinc operation, expand the operation,
753 storing the offset, if any, in *OFF. */
756 autoinc_split (rtx x
, rtx
*off
, enum machine_mode memmode
)
758 switch (GET_CODE (x
))
765 if (memmode
== VOIDmode
)
768 *off
= GEN_INT (-GET_MODE_SIZE (memmode
));
773 if (memmode
== VOIDmode
)
776 *off
= GEN_INT (GET_MODE_SIZE (memmode
));
792 /* Return nonzero if we can prove that X and Y contain the same value,
793 taking our gathered information into account. MEMMODE holds the
794 mode of the enclosing MEM, if any, as required to deal with autoinc
795 addressing modes. If X and Y are not (known to be) part of
796 addresses, MEMMODE should be VOIDmode. */
799 rtx_equal_for_cselib_1 (rtx x
, rtx y
, enum machine_mode memmode
)
805 if (REG_P (x
) || MEM_P (x
))
807 cselib_val
*e
= cselib_lookup (x
, GET_MODE (x
), 0, memmode
);
813 if (REG_P (y
) || MEM_P (y
))
815 cselib_val
*e
= cselib_lookup (y
, GET_MODE (y
), 0, memmode
);
824 if (GET_CODE (x
) == VALUE
)
826 cselib_val
*e
= canonical_cselib_val (CSELIB_VAL_PTR (x
));
827 struct elt_loc_list
*l
;
829 if (GET_CODE (y
) == VALUE
)
830 return e
== canonical_cselib_val (CSELIB_VAL_PTR (y
));
832 for (l
= e
->locs
; l
; l
= l
->next
)
836 /* Avoid infinite recursion. We know we have the canonical
837 value, so we can just skip any values in the equivalence
839 if (REG_P (t
) || MEM_P (t
) || GET_CODE (t
) == VALUE
)
841 else if (rtx_equal_for_cselib_1 (t
, y
, memmode
))
847 else if (GET_CODE (y
) == VALUE
)
849 cselib_val
*e
= canonical_cselib_val (CSELIB_VAL_PTR (y
));
850 struct elt_loc_list
*l
;
852 for (l
= e
->locs
; l
; l
= l
->next
)
856 if (REG_P (t
) || MEM_P (t
) || GET_CODE (t
) == VALUE
)
858 else if (rtx_equal_for_cselib_1 (x
, t
, memmode
))
865 if (GET_MODE (x
) != GET_MODE (y
))
868 if (GET_CODE (x
) != GET_CODE (y
))
870 rtx xorig
= x
, yorig
= y
;
871 rtx xoff
= NULL
, yoff
= NULL
;
873 x
= autoinc_split (x
, &xoff
, memmode
);
874 y
= autoinc_split (y
, &yoff
, memmode
);
879 if (xoff
&& !rtx_equal_for_cselib_1 (xoff
, yoff
, memmode
))
882 /* Don't recurse if nothing changed. */
883 if (x
!= xorig
|| y
!= yorig
)
884 return rtx_equal_for_cselib_1 (x
, y
, memmode
);
889 /* These won't be handled correctly by the code below. */
890 switch (GET_CODE (x
))
897 case DEBUG_IMPLICIT_PTR
:
898 return DEBUG_IMPLICIT_PTR_DECL (x
)
899 == DEBUG_IMPLICIT_PTR_DECL (y
);
901 case DEBUG_PARAMETER_REF
:
902 return DEBUG_PARAMETER_REF_DECL (x
)
903 == DEBUG_PARAMETER_REF_DECL (y
);
906 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
907 use rtx_equal_for_cselib_1 to compare the operands. */
908 return rtx_equal_p (ENTRY_VALUE_EXP (x
), ENTRY_VALUE_EXP (y
));
911 return XEXP (x
, 0) == XEXP (y
, 0);
914 /* We have to compare any autoinc operations in the addresses
915 using this MEM's mode. */
916 return rtx_equal_for_cselib_1 (XEXP (x
, 0), XEXP (y
, 0), GET_MODE (x
));
923 fmt
= GET_RTX_FORMAT (code
);
925 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
932 if (XWINT (x
, i
) != XWINT (y
, i
))
938 if (XINT (x
, i
) != XINT (y
, i
))
944 /* Two vectors must have the same length. */
945 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
948 /* And the corresponding elements must match. */
949 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
950 if (! rtx_equal_for_cselib_1 (XVECEXP (x
, i
, j
),
951 XVECEXP (y
, i
, j
), memmode
))
957 && targetm
.commutative_p (x
, UNKNOWN
)
958 && rtx_equal_for_cselib_1 (XEXP (x
, 1), XEXP (y
, 0), memmode
)
959 && rtx_equal_for_cselib_1 (XEXP (x
, 0), XEXP (y
, 1), memmode
))
961 if (! rtx_equal_for_cselib_1 (XEXP (x
, i
), XEXP (y
, i
), memmode
))
967 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
972 /* These are just backpointers, so they don't matter. */
979 /* It is believed that rtx's at this level will never
980 contain anything but integers and other rtx's,
981 except for within LABEL_REFs and SYMBOL_REFs. */
989 /* We need to pass down the mode of constants through the hash table
990 functions. For that purpose, wrap them in a CONST of the appropriate
993 wrap_constant (enum machine_mode mode
, rtx x
)
995 if (!CONST_INT_P (x
) && GET_CODE (x
) != CONST_FIXED
996 && (GET_CODE (x
) != CONST_DOUBLE
|| GET_MODE (x
) != VOIDmode
))
998 gcc_assert (mode
!= VOIDmode
);
999 return gen_rtx_CONST (mode
, x
);
1002 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1003 For registers and memory locations, we look up their cselib_val structure
1004 and return its VALUE element.
1005 Possible reasons for return 0 are: the object is volatile, or we couldn't
1006 find a register or memory location in the table and CREATE is zero. If
1007 CREATE is nonzero, table elts are created for regs and mem.
1008 N.B. this hash function returns the same hash value for RTXes that
1009 differ only in the order of operands, thus it is suitable for comparisons
1010 that take commutativity into account.
1011 If we wanted to also support associative rules, we'd have to use a different
1012 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1013 MEMMODE indicates the mode of an enclosing MEM, and it's only
1014 used to compute autoinc values.
1015 We used to have a MODE argument for hashing for CONST_INTs, but that
1016 didn't make sense, since it caused spurious hash differences between
1017 (set (reg:SI 1) (const_int))
1018 (plus:SI (reg:SI 2) (reg:SI 1))
1020 (plus:SI (reg:SI 2) (const_int))
1021 If the mode is important in any context, it must be checked specifically
1022 in a comparison anyway, since relying on hash differences is unsafe. */
1025 cselib_hash_rtx (rtx x
, int create
, enum machine_mode memmode
)
1031 unsigned int hash
= 0;
1033 code
= GET_CODE (x
);
1034 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
1039 e
= CSELIB_VAL_PTR (x
);
1044 e
= cselib_lookup (x
, GET_MODE (x
), create
, memmode
);
1051 hash
+= ((unsigned) DEBUG_EXPR
<< 7)
1052 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x
));
1053 return hash
? hash
: (unsigned int) DEBUG_EXPR
;
1055 case DEBUG_IMPLICIT_PTR
:
1056 hash
+= ((unsigned) DEBUG_IMPLICIT_PTR
<< 7)
1057 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x
));
1058 return hash
? hash
: (unsigned int) DEBUG_IMPLICIT_PTR
;
1060 case DEBUG_PARAMETER_REF
:
1061 hash
+= ((unsigned) DEBUG_PARAMETER_REF
<< 7)
1062 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x
));
1063 return hash
? hash
: (unsigned int) DEBUG_PARAMETER_REF
;
1066 /* ENTRY_VALUEs are function invariant, thus try to avoid
1067 recursing on argument if ENTRY_VALUE is one of the
1068 forms emitted by expand_debug_expr, otherwise
1069 ENTRY_VALUE hash would depend on the current value
1070 in some register or memory. */
1071 if (REG_P (ENTRY_VALUE_EXP (x
)))
1072 hash
+= (unsigned int) REG
1073 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x
))
1074 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x
));
1075 else if (MEM_P (ENTRY_VALUE_EXP (x
))
1076 && REG_P (XEXP (ENTRY_VALUE_EXP (x
), 0)))
1077 hash
+= (unsigned int) MEM
1078 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x
), 0))
1079 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x
), 0));
1081 hash
+= cselib_hash_rtx (ENTRY_VALUE_EXP (x
), create
, memmode
);
1082 return hash
? hash
: (unsigned int) ENTRY_VALUE
;
1085 hash
+= ((unsigned) CONST_INT
<< 7) + INTVAL (x
);
1086 return hash
? hash
: (unsigned int) CONST_INT
;
1089 /* This is like the general case, except that it only counts
1090 the integers representing the constant. */
1091 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
1092 if (GET_MODE (x
) != VOIDmode
)
1093 hash
+= real_hash (CONST_DOUBLE_REAL_VALUE (x
));
1095 hash
+= ((unsigned) CONST_DOUBLE_LOW (x
)
1096 + (unsigned) CONST_DOUBLE_HIGH (x
));
1097 return hash
? hash
: (unsigned int) CONST_DOUBLE
;
1100 hash
+= (unsigned int) code
+ (unsigned int) GET_MODE (x
);
1101 hash
+= fixed_hash (CONST_FIXED_VALUE (x
));
1102 return hash
? hash
: (unsigned int) CONST_FIXED
;
1109 units
= CONST_VECTOR_NUNITS (x
);
1111 for (i
= 0; i
< units
; ++i
)
1113 elt
= CONST_VECTOR_ELT (x
, i
);
1114 hash
+= cselib_hash_rtx (elt
, 0, memmode
);
1120 /* Assume there is only one rtx object for any given label. */
1122 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1123 differences and differences between each stage's debugging dumps. */
1124 hash
+= (((unsigned int) LABEL_REF
<< 7)
1125 + CODE_LABEL_NUMBER (XEXP (x
, 0)));
1126 return hash
? hash
: (unsigned int) LABEL_REF
;
1130 /* Don't hash on the symbol's address to avoid bootstrap differences.
1131 Different hash values may cause expressions to be recorded in
1132 different orders and thus different registers to be used in the
1133 final assembler. This also avoids differences in the dump files
1134 between various stages. */
1136 const unsigned char *p
= (const unsigned char *) XSTR (x
, 0);
1139 h
+= (h
<< 7) + *p
++; /* ??? revisit */
1141 hash
+= ((unsigned int) SYMBOL_REF
<< 7) + h
;
1142 return hash
? hash
: (unsigned int) SYMBOL_REF
;
1147 /* We can't compute these without knowing the MEM mode. */
1148 gcc_assert (memmode
!= VOIDmode
);
1149 i
= GET_MODE_SIZE (memmode
);
1150 if (code
== PRE_DEC
)
1152 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1153 like (mem:MEMMODE (plus (reg) (const_int I))). */
1154 hash
+= (unsigned) PLUS
- (unsigned)code
1155 + cselib_hash_rtx (XEXP (x
, 0), create
, memmode
)
1156 + cselib_hash_rtx (GEN_INT (i
), create
, memmode
);
1157 return hash
? hash
: 1 + (unsigned) PLUS
;
1160 gcc_assert (memmode
!= VOIDmode
);
1161 return cselib_hash_rtx (XEXP (x
, 1), create
, memmode
);
1166 gcc_assert (memmode
!= VOIDmode
);
1167 return cselib_hash_rtx (XEXP (x
, 0), create
, memmode
);
1172 case UNSPEC_VOLATILE
:
1176 if (MEM_VOLATILE_P (x
))
1185 i
= GET_RTX_LENGTH (code
) - 1;
1186 fmt
= GET_RTX_FORMAT (code
);
1193 rtx tem
= XEXP (x
, i
);
1194 unsigned int tem_hash
= cselib_hash_rtx (tem
, create
, memmode
);
1203 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1205 unsigned int tem_hash
1206 = cselib_hash_rtx (XVECEXP (x
, i
, j
), create
, memmode
);
1217 const unsigned char *p
= (const unsigned char *) XSTR (x
, i
);
1226 hash
+= XINT (x
, i
);
1239 return hash
? hash
: 1 + (unsigned int) GET_CODE (x
);
1242 /* Create a new value structure for VALUE and initialize it. The mode of the
1245 static inline cselib_val
*
1246 new_cselib_val (unsigned int hash
, enum machine_mode mode
, rtx x
)
1248 cselib_val
*e
= (cselib_val
*) pool_alloc (cselib_val_pool
);
1251 gcc_assert (next_uid
);
1254 e
->uid
= next_uid
++;
1255 /* We use an alloc pool to allocate this RTL construct because it
1256 accounts for about 8% of the overall memory usage. We know
1257 precisely when we can have VALUE RTXen (when cselib is active)
1258 so we don't need to put them in garbage collected memory.
1259 ??? Why should a VALUE be an RTX in the first place? */
1260 e
->val_rtx
= (rtx
) pool_alloc (value_pool
);
1261 memset (e
->val_rtx
, 0, RTX_HDR_SIZE
);
1262 PUT_CODE (e
->val_rtx
, VALUE
);
1263 PUT_MODE (e
->val_rtx
, mode
);
1264 CSELIB_VAL_PTR (e
->val_rtx
) = e
;
1267 e
->next_containing_mem
= 0;
1269 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1271 fprintf (dump_file
, "cselib value %u:%u ", e
->uid
, hash
);
1272 if (flag_dump_noaddr
|| flag_dump_unnumbered
)
1273 fputs ("# ", dump_file
);
1275 fprintf (dump_file
, "%p ", (void*)e
);
1276 print_rtl_single (dump_file
, x
);
1277 fputc ('\n', dump_file
);
1283 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1284 contains the data at this address. X is a MEM that represents the
1285 value. Update the two value structures to represent this situation. */
1288 add_mem_for_addr (cselib_val
*addr_elt
, cselib_val
*mem_elt
, rtx x
)
1290 struct elt_loc_list
*l
;
1292 addr_elt
= canonical_cselib_val (addr_elt
);
1293 mem_elt
= canonical_cselib_val (mem_elt
);
1295 /* Avoid duplicates. */
1296 for (l
= mem_elt
->locs
; l
; l
= l
->next
)
1298 && CSELIB_VAL_PTR (XEXP (l
->loc
, 0)) == addr_elt
)
1300 promote_debug_loc (l
);
1304 addr_elt
->addr_list
= new_elt_list (addr_elt
->addr_list
, mem_elt
);
1305 new_elt_loc_list (mem_elt
,
1306 replace_equiv_address_nv (x
, addr_elt
->val_rtx
));
1307 if (mem_elt
->next_containing_mem
== NULL
)
1309 mem_elt
->next_containing_mem
= first_containing_mem
;
1310 first_containing_mem
= mem_elt
;
1314 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1315 If CREATE, make a new one if we haven't seen it before. */
1318 cselib_lookup_mem (rtx x
, int create
)
1320 enum machine_mode mode
= GET_MODE (x
);
1321 enum machine_mode addr_mode
;
1324 cselib_val
*mem_elt
;
1327 if (MEM_VOLATILE_P (x
) || mode
== BLKmode
1328 || !cselib_record_memory
1329 || (FLOAT_MODE_P (mode
) && flag_float_store
))
1332 addr_mode
= GET_MODE (XEXP (x
, 0));
1333 if (addr_mode
== VOIDmode
)
1336 /* Look up the value for the address. */
1337 addr
= cselib_lookup (XEXP (x
, 0), addr_mode
, create
, mode
);
1341 addr
= canonical_cselib_val (addr
);
1342 /* Find a value that describes a value of our mode at that address. */
1343 for (l
= addr
->addr_list
; l
; l
= l
->next
)
1344 if (GET_MODE (l
->elt
->val_rtx
) == mode
)
1346 promote_debug_loc (l
->elt
->locs
);
1353 mem_elt
= new_cselib_val (next_uid
, mode
, x
);
1354 add_mem_for_addr (addr
, mem_elt
, x
);
1355 slot
= cselib_find_slot (wrap_constant (mode
, x
), mem_elt
->hash
,
1361 /* Search thru the possible substitutions in P. We prefer a non reg
1362 substitution because this allows us to expand the tree further. If
1363 we find, just a reg, take the lowest regno. There may be several
1364 non-reg results, we just take the first one because they will all
1365 expand to the same place. */
1368 expand_loc (struct elt_loc_list
*p
, struct expand_value_data
*evd
,
1371 rtx reg_result
= NULL
;
1372 unsigned int regno
= UINT_MAX
;
1373 struct elt_loc_list
*p_in
= p
;
1375 for (; p
; p
= p
->next
)
1377 /* Return these right away to avoid returning stack pointer based
1378 expressions for frame pointer and vice versa, which is something
1379 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1380 for more details. */
1382 && (REGNO (p
->loc
) == STACK_POINTER_REGNUM
1383 || REGNO (p
->loc
) == FRAME_POINTER_REGNUM
1384 || REGNO (p
->loc
) == HARD_FRAME_POINTER_REGNUM
1385 || REGNO (p
->loc
) == cfa_base_preserved_regno
))
1387 /* Avoid infinite recursion trying to expand a reg into a
1389 if ((REG_P (p
->loc
))
1390 && (REGNO (p
->loc
) < regno
)
1391 && !bitmap_bit_p (evd
->regs_active
, REGNO (p
->loc
)))
1393 reg_result
= p
->loc
;
1394 regno
= REGNO (p
->loc
);
1396 /* Avoid infinite recursion and do not try to expand the
1398 else if (GET_CODE (p
->loc
) == VALUE
1399 && CSELIB_VAL_PTR (p
->loc
)->locs
== p_in
)
1401 else if (!REG_P (p
->loc
))
1404 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1406 print_inline_rtx (dump_file
, p
->loc
, 0);
1407 fprintf (dump_file
, "\n");
1409 if (GET_CODE (p
->loc
) == LO_SUM
1410 && GET_CODE (XEXP (p
->loc
, 1)) == SYMBOL_REF
1412 && (note
= find_reg_note (p
->setting_insn
, REG_EQUAL
, NULL_RTX
))
1413 && XEXP (note
, 0) == XEXP (p
->loc
, 1))
1414 return XEXP (p
->loc
, 1);
1415 result
= cselib_expand_value_rtx_1 (p
->loc
, evd
, max_depth
- 1);
1422 if (regno
!= UINT_MAX
)
1425 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1426 fprintf (dump_file
, "r%d\n", regno
);
1428 result
= cselib_expand_value_rtx_1 (reg_result
, evd
, max_depth
- 1);
1433 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1437 print_inline_rtx (dump_file
, reg_result
, 0);
1438 fprintf (dump_file
, "\n");
1441 fprintf (dump_file
, "NULL\n");
1447 /* Forward substitute and expand an expression out to its roots.
1448 This is the opposite of common subexpression. Because local value
1449 numbering is such a weak optimization, the expanded expression is
1450 pretty much unique (not from a pointer equals point of view but
1451 from a tree shape point of view.
1453 This function returns NULL if the expansion fails. The expansion
1454 will fail if there is no value number for one of the operands or if
1455 one of the operands has been overwritten between the current insn
1456 and the beginning of the basic block. For instance x has no
1462 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1463 It is clear on return. */
1466 cselib_expand_value_rtx (rtx orig
, bitmap regs_active
, int max_depth
)
1468 struct expand_value_data evd
;
1470 evd
.regs_active
= regs_active
;
1471 evd
.callback
= NULL
;
1472 evd
.callback_arg
= NULL
;
1475 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
);
1478 /* Same as cselib_expand_value_rtx, but using a callback to try to
1479 resolve some expressions. The CB function should return ORIG if it
1480 can't or does not want to deal with a certain RTX. Any other
1481 return value, including NULL, will be used as the expansion for
1482 VALUE, without any further changes. */
1485 cselib_expand_value_rtx_cb (rtx orig
, bitmap regs_active
, int max_depth
,
1486 cselib_expand_callback cb
, void *data
)
1488 struct expand_value_data evd
;
1490 evd
.regs_active
= regs_active
;
1492 evd
.callback_arg
= data
;
1495 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
);
1498 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1499 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1500 would return NULL or non-NULL, without allocating new rtx. */
1503 cselib_dummy_expand_value_rtx_cb (rtx orig
, bitmap regs_active
, int max_depth
,
1504 cselib_expand_callback cb
, void *data
)
1506 struct expand_value_data evd
;
1508 evd
.regs_active
= regs_active
;
1510 evd
.callback_arg
= data
;
1513 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
) != NULL
;
1516 /* Internal implementation of cselib_expand_value_rtx and
1517 cselib_expand_value_rtx_cb. */
1520 cselib_expand_value_rtx_1 (rtx orig
, struct expand_value_data
*evd
,
1526 const char *format_ptr
;
1527 enum machine_mode mode
;
1529 code
= GET_CODE (orig
);
1531 /* For the context of dse, if we end up expand into a huge tree, we
1532 will not have a useful address, so we might as well just give up
1541 struct elt_list
*l
= REG_VALUES (REGNO (orig
));
1543 if (l
&& l
->elt
== NULL
)
1545 for (; l
; l
= l
->next
)
1546 if (GET_MODE (l
->elt
->val_rtx
) == GET_MODE (orig
))
1549 unsigned regno
= REGNO (orig
);
1551 /* The only thing that we are not willing to do (this
1552 is requirement of dse and if others potential uses
1553 need this function we should add a parm to control
1554 it) is that we will not substitute the
1555 STACK_POINTER_REGNUM, FRAME_POINTER or the
1558 These expansions confuses the code that notices that
1559 stores into the frame go dead at the end of the
1560 function and that the frame is not effected by calls
1561 to subroutines. If you allow the
1562 STACK_POINTER_REGNUM substitution, then dse will
1563 think that parameter pushing also goes dead which is
1564 wrong. If you allow the FRAME_POINTER or the
1565 HARD_FRAME_POINTER then you lose the opportunity to
1566 make the frame assumptions. */
1567 if (regno
== STACK_POINTER_REGNUM
1568 || regno
== FRAME_POINTER_REGNUM
1569 || regno
== HARD_FRAME_POINTER_REGNUM
1570 || regno
== cfa_base_preserved_regno
)
1573 bitmap_set_bit (evd
->regs_active
, regno
);
1575 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1576 fprintf (dump_file
, "expanding: r%d into: ", regno
);
1578 result
= expand_loc (l
->elt
->locs
, evd
, max_depth
);
1579 bitmap_clear_bit (evd
->regs_active
, regno
);
1596 /* SCRATCH must be shared because they represent distinct values. */
1599 if (REG_P (XEXP (orig
, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig
, 0))))
1604 if (shared_const_p (orig
))
1614 subreg
= evd
->callback (orig
, evd
->regs_active
, max_depth
,
1620 subreg
= cselib_expand_value_rtx_1 (SUBREG_REG (orig
), evd
,
1624 scopy
= simplify_gen_subreg (GET_MODE (orig
), subreg
,
1625 GET_MODE (SUBREG_REG (orig
)),
1626 SUBREG_BYTE (orig
));
1628 || (GET_CODE (scopy
) == SUBREG
1629 && !REG_P (SUBREG_REG (scopy
))
1630 && !MEM_P (SUBREG_REG (scopy
))))
1640 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
1642 fputs ("\nexpanding ", dump_file
);
1643 print_rtl_single (dump_file
, orig
);
1644 fputs (" into...", dump_file
);
1649 result
= evd
->callback (orig
, evd
->regs_active
, max_depth
,
1656 result
= expand_loc (CSELIB_VAL_PTR (orig
)->locs
, evd
, max_depth
);
1662 return evd
->callback (orig
, evd
->regs_active
, max_depth
,
1670 /* Copy the various flags, fields, and other information. We assume
1671 that all fields need copying, and then clear the fields that should
1672 not be copied. That is the sensible default behavior, and forces
1673 us to explicitly document why we are *not* copying a flag. */
1677 copy
= shallow_copy_rtx (orig
);
1679 format_ptr
= GET_RTX_FORMAT (code
);
1681 for (i
= 0; i
< GET_RTX_LENGTH (code
); i
++)
1682 switch (*format_ptr
++)
1685 if (XEXP (orig
, i
) != NULL
)
1687 rtx result
= cselib_expand_value_rtx_1 (XEXP (orig
, i
), evd
,
1692 XEXP (copy
, i
) = result
;
1698 if (XVEC (orig
, i
) != NULL
)
1701 XVEC (copy
, i
) = rtvec_alloc (XVECLEN (orig
, i
));
1702 for (j
= 0; j
< XVECLEN (orig
, i
); j
++)
1704 rtx result
= cselib_expand_value_rtx_1 (XVECEXP (orig
, i
, j
),
1705 evd
, max_depth
- 1);
1709 XVECEXP (copy
, i
, j
) = result
;
1723 /* These are left unchanged. */
1733 mode
= GET_MODE (copy
);
1734 /* If an operand has been simplified into CONST_INT, which doesn't
1735 have a mode and the mode isn't derivable from whole rtx's mode,
1736 try simplify_*_operation first with mode from original's operand
1737 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1739 switch (GET_RTX_CLASS (code
))
1742 if (CONST_INT_P (XEXP (copy
, 0))
1743 && GET_MODE (XEXP (orig
, 0)) != VOIDmode
)
1745 scopy
= simplify_unary_operation (code
, mode
, XEXP (copy
, 0),
1746 GET_MODE (XEXP (orig
, 0)));
1751 case RTX_COMM_ARITH
:
1753 /* These expressions can derive operand modes from the whole rtx's mode. */
1756 case RTX_BITFIELD_OPS
:
1757 if (CONST_INT_P (XEXP (copy
, 0))
1758 && GET_MODE (XEXP (orig
, 0)) != VOIDmode
)
1760 scopy
= simplify_ternary_operation (code
, mode
,
1761 GET_MODE (XEXP (orig
, 0)),
1762 XEXP (copy
, 0), XEXP (copy
, 1),
1769 case RTX_COMM_COMPARE
:
1770 if (CONST_INT_P (XEXP (copy
, 0))
1771 && GET_MODE (XEXP (copy
, 1)) == VOIDmode
1772 && (GET_MODE (XEXP (orig
, 0)) != VOIDmode
1773 || GET_MODE (XEXP (orig
, 1)) != VOIDmode
))
1775 scopy
= simplify_relational_operation (code
, mode
,
1776 (GET_MODE (XEXP (orig
, 0))
1778 ? GET_MODE (XEXP (orig
, 0))
1779 : GET_MODE (XEXP (orig
, 1)),
1789 scopy
= simplify_rtx (copy
);
1795 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1796 with VALUE expressions. This way, it becomes independent of changes
1797 to registers and memory.
1798 X isn't actually modified; if modifications are needed, new rtl is
1799 allocated. However, the return value can share rtl with X.
1800 If X is within a MEM, MEMMODE must be the mode of the MEM. */
1803 cselib_subst_to_values (rtx x
, enum machine_mode memmode
)
1805 enum rtx_code code
= GET_CODE (x
);
1806 const char *fmt
= GET_RTX_FORMAT (code
);
1815 l
= REG_VALUES (REGNO (x
));
1816 if (l
&& l
->elt
== NULL
)
1818 for (; l
; l
= l
->next
)
1819 if (GET_MODE (l
->elt
->val_rtx
) == GET_MODE (x
))
1820 return l
->elt
->val_rtx
;
1825 e
= cselib_lookup_mem (x
, 0);
1826 /* This used to happen for autoincrements, but we deal with them
1827 properly now. Remove the if stmt for the next release. */
1830 /* Assign a value that doesn't match any other. */
1831 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
1836 e
= cselib_lookup (x
, GET_MODE (x
), 0, memmode
);
1849 gcc_assert (memmode
!= VOIDmode
);
1850 i
= GET_MODE_SIZE (memmode
);
1851 if (code
== PRE_DEC
)
1853 return cselib_subst_to_values (plus_constant (XEXP (x
, 0), i
),
1857 gcc_assert (memmode
!= VOIDmode
);
1858 return cselib_subst_to_values (XEXP (x
, 1), memmode
);
1863 gcc_assert (memmode
!= VOIDmode
);
1864 return cselib_subst_to_values (XEXP (x
, 0), memmode
);
1870 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1874 rtx t
= cselib_subst_to_values (XEXP (x
, i
), memmode
);
1876 if (t
!= XEXP (x
, i
))
1879 copy
= shallow_copy_rtx (x
);
1883 else if (fmt
[i
] == 'E')
1887 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1889 rtx t
= cselib_subst_to_values (XVECEXP (x
, i
, j
), memmode
);
1891 if (t
!= XVECEXP (x
, i
, j
))
1893 if (XVEC (x
, i
) == XVEC (copy
, i
))
1896 copy
= shallow_copy_rtx (x
);
1897 XVEC (copy
, i
) = shallow_copy_rtvec (XVEC (x
, i
));
1899 XVECEXP (copy
, i
, j
) = t
;
1908 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
1911 cselib_subst_to_values_from_insn (rtx x
, enum machine_mode memmode
, rtx insn
)
1914 gcc_assert (!cselib_current_insn
);
1915 cselib_current_insn
= insn
;
1916 ret
= cselib_subst_to_values (x
, memmode
);
1917 cselib_current_insn
= NULL
;
1921 /* Look up the rtl expression X in our tables and return the value it
1922 has. If CREATE is zero, we return NULL if we don't know the value.
1923 Otherwise, we create a new one if possible, using mode MODE if X
1924 doesn't have a mode (i.e. because it's a constant). When X is part
1925 of an address, MEMMODE should be the mode of the enclosing MEM if
1926 we're tracking autoinc expressions. */
1929 cselib_lookup_1 (rtx x
, enum machine_mode mode
,
1930 int create
, enum machine_mode memmode
)
1934 unsigned int hashval
;
1936 if (GET_MODE (x
) != VOIDmode
)
1937 mode
= GET_MODE (x
);
1939 if (GET_CODE (x
) == VALUE
)
1940 return CSELIB_VAL_PTR (x
);
1945 unsigned int i
= REGNO (x
);
1948 if (l
&& l
->elt
== NULL
)
1950 for (; l
; l
= l
->next
)
1951 if (mode
== GET_MODE (l
->elt
->val_rtx
))
1953 promote_debug_loc (l
->elt
->locs
);
1960 if (i
< FIRST_PSEUDO_REGISTER
)
1962 unsigned int n
= hard_regno_nregs
[i
][mode
];
1964 if (n
> max_value_regs
)
1968 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
1969 new_elt_loc_list (e
, x
);
1970 if (REG_VALUES (i
) == 0)
1972 /* Maintain the invariant that the first entry of
1973 REG_VALUES, if present, must be the value used to set the
1974 register, or NULL. */
1975 used_regs
[n_used_regs
++] = i
;
1976 REG_VALUES (i
) = new_elt_list (REG_VALUES (i
), NULL
);
1978 else if (cselib_preserve_constants
1979 && GET_MODE_CLASS (mode
) == MODE_INT
)
1981 /* During var-tracking, try harder to find equivalences
1982 for SUBREGs. If a setter sets say a DImode register
1983 and user uses that register only in SImode, add a lowpart
1985 struct elt_list
*lwider
= NULL
;
1987 if (l
&& l
->elt
== NULL
)
1989 for (; l
; l
= l
->next
)
1990 if (GET_MODE_CLASS (GET_MODE (l
->elt
->val_rtx
)) == MODE_INT
1991 && GET_MODE_SIZE (GET_MODE (l
->elt
->val_rtx
))
1992 > GET_MODE_SIZE (mode
)
1994 || GET_MODE_SIZE (GET_MODE (l
->elt
->val_rtx
))
1995 < GET_MODE_SIZE (GET_MODE (lwider
->elt
->val_rtx
))))
1997 struct elt_loc_list
*el
;
1998 if (i
< FIRST_PSEUDO_REGISTER
1999 && hard_regno_nregs
[i
][GET_MODE (l
->elt
->val_rtx
)] != 1)
2001 for (el
= l
->elt
->locs
; el
; el
= el
->next
)
2002 if (!REG_P (el
->loc
))
2009 rtx sub
= lowpart_subreg (mode
, lwider
->elt
->val_rtx
,
2010 GET_MODE (lwider
->elt
->val_rtx
));
2012 new_elt_loc_list (e
, sub
);
2015 REG_VALUES (i
)->next
= new_elt_list (REG_VALUES (i
)->next
, e
);
2016 slot
= cselib_find_slot (x
, e
->hash
, INSERT
, memmode
);
2022 return cselib_lookup_mem (x
, create
);
2024 hashval
= cselib_hash_rtx (x
, create
, memmode
);
2025 /* Can't even create if hashing is not possible. */
2029 slot
= cselib_find_slot (wrap_constant (mode
, x
), hashval
,
2030 create
? INSERT
: NO_INSERT
, memmode
);
2034 e
= (cselib_val
*) *slot
;
2038 e
= new_cselib_val (hashval
, mode
, x
);
2040 /* We have to fill the slot before calling cselib_subst_to_values:
2041 the hash table is inconsistent until we do so, and
2042 cselib_subst_to_values will need to do lookups. */
2044 new_elt_loc_list (e
, cselib_subst_to_values (x
, memmode
));
2048 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2051 cselib_lookup_from_insn (rtx x
, enum machine_mode mode
,
2052 int create
, enum machine_mode memmode
, rtx insn
)
2056 gcc_assert (!cselib_current_insn
);
2057 cselib_current_insn
= insn
;
2059 ret
= cselib_lookup (x
, mode
, create
, memmode
);
2061 cselib_current_insn
= NULL
;
2066 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2067 maintains invariants related with debug insns. */
2070 cselib_lookup (rtx x
, enum machine_mode mode
,
2071 int create
, enum machine_mode memmode
)
2073 cselib_val
*ret
= cselib_lookup_1 (x
, mode
, create
, memmode
);
2075 /* ??? Should we return NULL if we're not to create an entry, the
2076 found loc is a debug loc and cselib_current_insn is not DEBUG?
2077 If so, we should also avoid converting val to non-DEBUG; probably
2078 easiest setting cselib_current_insn to NULL before the call
2081 if (dump_file
&& (dump_flags
& TDF_CSELIB
))
2083 fputs ("cselib lookup ", dump_file
);
2084 print_inline_rtx (dump_file
, x
, 2);
2085 fprintf (dump_file
, " => %u:%u\n",
2087 ret
? ret
->hash
: 0);
2093 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2094 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2095 is used to determine how many hard registers are being changed. If MODE
2096 is VOIDmode, then only REGNO is being changed; this is used when
2097 invalidating call clobbered registers across a call. */
2100 cselib_invalidate_regno (unsigned int regno
, enum machine_mode mode
)
2102 unsigned int endregno
;
2105 /* If we see pseudos after reload, something is _wrong_. */
2106 gcc_assert (!reload_completed
|| regno
< FIRST_PSEUDO_REGISTER
2107 || reg_renumber
[regno
] < 0);
2109 /* Determine the range of registers that must be invalidated. For
2110 pseudos, only REGNO is affected. For hard regs, we must take MODE
2111 into account, and we must also invalidate lower register numbers
2112 if they contain values that overlap REGNO. */
2113 if (regno
< FIRST_PSEUDO_REGISTER
)
2115 gcc_assert (mode
!= VOIDmode
);
2117 if (regno
< max_value_regs
)
2120 i
= regno
- max_value_regs
;
2122 endregno
= end_hard_regno (mode
, regno
);
2127 endregno
= regno
+ 1;
2130 for (; i
< endregno
; i
++)
2132 struct elt_list
**l
= ®_VALUES (i
);
2134 /* Go through all known values for this reg; if it overlaps the range
2135 we're invalidating, remove the value. */
2138 cselib_val
*v
= (*l
)->elt
;
2141 struct elt_loc_list
**p
;
2142 unsigned int this_last
= i
;
2144 if (i
< FIRST_PSEUDO_REGISTER
&& v
!= NULL
)
2145 this_last
= end_hard_regno (GET_MODE (v
->val_rtx
), i
) - 1;
2147 if (this_last
< regno
|| v
== NULL
2148 || (v
== cfa_base_preserved_val
2149 && i
== cfa_base_preserved_regno
))
2155 /* We have an overlap. */
2156 if (*l
== REG_VALUES (i
))
2158 /* Maintain the invariant that the first entry of
2159 REG_VALUES, if present, must be the value used to set
2160 the register, or NULL. This is also nice because
2161 then we won't push the same regno onto user_regs
2167 unchain_one_elt_list (l
);
2169 v
= canonical_cselib_val (v
);
2171 had_locs
= v
->locs
!= NULL
;
2172 setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
2174 /* Now, we clear the mapping from value to reg. It must exist, so
2175 this code will crash intentionally if it doesn't. */
2176 for (p
= &v
->locs
; ; p
= &(*p
)->next
)
2180 if (REG_P (x
) && REGNO (x
) == i
)
2182 unchain_one_elt_loc_list (p
);
2187 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
2189 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
2190 n_useless_debug_values
++;
2198 /* Invalidate any locations in the table which are changed because of a
2199 store to MEM_RTX. If this is called because of a non-const call
2200 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2203 cselib_invalidate_mem (rtx mem_rtx
)
2205 cselib_val
**vp
, *v
, *next
;
2209 mem_addr
= canon_rtx (get_addr (XEXP (mem_rtx
, 0)));
2210 mem_rtx
= canon_rtx (mem_rtx
);
2212 vp
= &first_containing_mem
;
2213 for (v
= *vp
; v
!= &dummy_val
; v
= next
)
2215 bool has_mem
= false;
2216 struct elt_loc_list
**p
= &v
->locs
;
2217 bool had_locs
= v
->locs
!= NULL
;
2218 rtx setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
2224 struct elt_list
**mem_chain
;
2226 /* MEMs may occur in locations only at the top level; below
2227 that every MEM or REG is substituted by its VALUE. */
2233 if (num_mems
< PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS
)
2234 && ! canon_true_dependence (mem_rtx
, GET_MODE (mem_rtx
),
2235 mem_addr
, x
, NULL_RTX
))
2243 /* This one overlaps. */
2244 /* We must have a mapping from this MEM's address to the
2245 value (E). Remove that, too. */
2246 addr
= cselib_lookup (XEXP (x
, 0), VOIDmode
, 0, GET_MODE (x
));
2247 addr
= canonical_cselib_val (addr
);
2248 gcc_checking_assert (v
== canonical_cselib_val (v
));
2249 mem_chain
= &addr
->addr_list
;
2252 cselib_val
*canon
= canonical_cselib_val ((*mem_chain
)->elt
);
2256 unchain_one_elt_list (mem_chain
);
2260 /* Record canonicalized elt. */
2261 (*mem_chain
)->elt
= canon
;
2263 mem_chain
= &(*mem_chain
)->next
;
2266 unchain_one_elt_loc_list (p
);
2269 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
2271 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
2272 n_useless_debug_values
++;
2277 next
= v
->next_containing_mem
;
2281 vp
= &(*vp
)->next_containing_mem
;
2284 v
->next_containing_mem
= NULL
;
2289 /* Invalidate DEST, which is being assigned to or clobbered. */
2292 cselib_invalidate_rtx (rtx dest
)
2294 while (GET_CODE (dest
) == SUBREG
2295 || GET_CODE (dest
) == ZERO_EXTRACT
2296 || GET_CODE (dest
) == STRICT_LOW_PART
)
2297 dest
= XEXP (dest
, 0);
2300 cselib_invalidate_regno (REGNO (dest
), GET_MODE (dest
));
2301 else if (MEM_P (dest
))
2302 cselib_invalidate_mem (dest
);
2305 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2308 cselib_invalidate_rtx_note_stores (rtx dest
, const_rtx ignore ATTRIBUTE_UNUSED
,
2309 void *data ATTRIBUTE_UNUSED
)
2311 cselib_invalidate_rtx (dest
);
2314 /* Record the result of a SET instruction. DEST is being set; the source
2315 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2316 describes its address. */
2319 cselib_record_set (rtx dest
, cselib_val
*src_elt
, cselib_val
*dest_addr_elt
)
2321 int dreg
= REG_P (dest
) ? (int) REGNO (dest
) : -1;
2323 if (src_elt
== 0 || side_effects_p (dest
))
2328 if (dreg
< FIRST_PSEUDO_REGISTER
)
2330 unsigned int n
= hard_regno_nregs
[dreg
][GET_MODE (dest
)];
2332 if (n
> max_value_regs
)
2336 if (REG_VALUES (dreg
) == 0)
2338 used_regs
[n_used_regs
++] = dreg
;
2339 REG_VALUES (dreg
) = new_elt_list (REG_VALUES (dreg
), src_elt
);
2343 /* The register should have been invalidated. */
2344 gcc_assert (REG_VALUES (dreg
)->elt
== 0);
2345 REG_VALUES (dreg
)->elt
= src_elt
;
2348 if (src_elt
->locs
== 0 && !PRESERVED_VALUE_P (src_elt
->val_rtx
))
2350 new_elt_loc_list (src_elt
, dest
);
2352 else if (MEM_P (dest
) && dest_addr_elt
!= 0
2353 && cselib_record_memory
)
2355 if (src_elt
->locs
== 0 && !PRESERVED_VALUE_P (src_elt
->val_rtx
))
2357 add_mem_for_addr (dest_addr_elt
, src_elt
, dest
);
2361 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2364 cselib_add_permanent_equiv (cselib_val
*elt
, rtx x
, rtx insn
)
2367 rtx save_cselib_current_insn
= cselib_current_insn
;
2369 gcc_checking_assert (elt
);
2370 gcc_checking_assert (PRESERVED_VALUE_P (elt
->val_rtx
));
2371 gcc_checking_assert (!side_effects_p (x
));
2373 cselib_current_insn
= insn
;
2375 nelt
= cselib_lookup (x
, GET_MODE (elt
->val_rtx
), 1, VOIDmode
);
2379 if (!PRESERVED_VALUE_P (nelt
->val_rtx
))
2380 cselib_preserve_value (nelt
);
2382 new_elt_loc_list (nelt
, elt
->val_rtx
);
2385 cselib_current_insn
= save_cselib_current_insn
;
2388 /* There is no good way to determine how many elements there can be
2389 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2390 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2392 struct cselib_record_autoinc_data
2394 struct cselib_set
*sets
;
2398 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2399 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2402 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED
, rtx op ATTRIBUTE_UNUSED
,
2403 rtx dest
, rtx src
, rtx srcoff
, void *arg
)
2405 struct cselib_record_autoinc_data
*data
;
2406 data
= (struct cselib_record_autoinc_data
*)arg
;
2408 data
->sets
[data
->n_sets
].dest
= dest
;
2411 data
->sets
[data
->n_sets
].src
= gen_rtx_PLUS (GET_MODE (src
), src
, srcoff
);
2413 data
->sets
[data
->n_sets
].src
= src
;
2420 /* Record the effects of any sets and autoincs in INSN. */
2422 cselib_record_sets (rtx insn
)
2426 struct cselib_set sets
[MAX_SETS
];
2427 rtx body
= PATTERN (insn
);
2429 int n_sets_before_autoinc
;
2430 struct cselib_record_autoinc_data data
;
2432 body
= PATTERN (insn
);
2433 if (GET_CODE (body
) == COND_EXEC
)
2435 cond
= COND_EXEC_TEST (body
);
2436 body
= COND_EXEC_CODE (body
);
2439 /* Find all sets. */
2440 if (GET_CODE (body
) == SET
)
2442 sets
[0].src
= SET_SRC (body
);
2443 sets
[0].dest
= SET_DEST (body
);
2446 else if (GET_CODE (body
) == PARALLEL
)
2448 /* Look through the PARALLEL and record the values being
2449 set, if possible. Also handle any CLOBBERs. */
2450 for (i
= XVECLEN (body
, 0) - 1; i
>= 0; --i
)
2452 rtx x
= XVECEXP (body
, 0, i
);
2454 if (GET_CODE (x
) == SET
)
2456 sets
[n_sets
].src
= SET_SRC (x
);
2457 sets
[n_sets
].dest
= SET_DEST (x
);
2464 && MEM_P (sets
[0].src
)
2465 && !cselib_record_memory
2466 && MEM_READONLY_P (sets
[0].src
))
2468 rtx note
= find_reg_equal_equiv_note (insn
);
2470 if (note
&& CONSTANT_P (XEXP (note
, 0)))
2471 sets
[0].src
= XEXP (note
, 0);
2475 data
.n_sets
= n_sets_before_autoinc
= n_sets
;
2476 for_each_inc_dec (&insn
, cselib_record_autoinc_cb
, &data
);
2477 n_sets
= data
.n_sets
;
2479 /* Look up the values that are read. Do this before invalidating the
2480 locations that are written. */
2481 for (i
= 0; i
< n_sets
; i
++)
2483 rtx dest
= sets
[i
].dest
;
2485 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2486 the low part after invalidating any knowledge about larger modes. */
2487 if (GET_CODE (sets
[i
].dest
) == STRICT_LOW_PART
)
2488 sets
[i
].dest
= dest
= XEXP (dest
, 0);
2490 /* We don't know how to record anything but REG or MEM. */
2492 || (MEM_P (dest
) && cselib_record_memory
))
2494 rtx src
= sets
[i
].src
;
2496 src
= gen_rtx_IF_THEN_ELSE (GET_MODE (dest
), cond
, src
, dest
);
2497 sets
[i
].src_elt
= cselib_lookup (src
, GET_MODE (dest
), 1, VOIDmode
);
2500 enum machine_mode address_mode
2501 = targetm
.addr_space
.address_mode (MEM_ADDR_SPACE (dest
));
2503 sets
[i
].dest_addr_elt
= cselib_lookup (XEXP (dest
, 0),
2508 sets
[i
].dest_addr_elt
= 0;
2512 if (cselib_record_sets_hook
)
2513 cselib_record_sets_hook (insn
, sets
, n_sets
);
2515 /* Invalidate all locations written by this insn. Note that the elts we
2516 looked up in the previous loop aren't affected, just some of their
2517 locations may go away. */
2518 note_stores (body
, cselib_invalidate_rtx_note_stores
, NULL
);
2520 for (i
= n_sets_before_autoinc
; i
< n_sets
; i
++)
2521 cselib_invalidate_rtx (sets
[i
].dest
);
2523 /* If this is an asm, look for duplicate sets. This can happen when the
2524 user uses the same value as an output multiple times. This is valid
2525 if the outputs are not actually used thereafter. Treat this case as
2526 if the value isn't actually set. We do this by smashing the destination
2527 to pc_rtx, so that we won't record the value later. */
2528 if (n_sets
>= 2 && asm_noperands (body
) >= 0)
2530 for (i
= 0; i
< n_sets
; i
++)
2532 rtx dest
= sets
[i
].dest
;
2533 if (REG_P (dest
) || MEM_P (dest
))
2536 for (j
= i
+ 1; j
< n_sets
; j
++)
2537 if (rtx_equal_p (dest
, sets
[j
].dest
))
2539 sets
[i
].dest
= pc_rtx
;
2540 sets
[j
].dest
= pc_rtx
;
2546 /* Now enter the equivalences in our tables. */
2547 for (i
= 0; i
< n_sets
; i
++)
2549 rtx dest
= sets
[i
].dest
;
2551 || (MEM_P (dest
) && cselib_record_memory
))
2552 cselib_record_set (dest
, sets
[i
].src_elt
, sets
[i
].dest_addr_elt
);
2556 /* Record the effects of INSN. */
2559 cselib_process_insn (rtx insn
)
2564 cselib_current_insn
= insn
;
2566 /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp. */
2569 && find_reg_note (insn
, REG_SETJMP
, NULL
))
2570 || (NONJUMP_INSN_P (insn
)
2571 && GET_CODE (PATTERN (insn
)) == ASM_OPERANDS
2572 && MEM_VOLATILE_P (PATTERN (insn
))))
2574 cselib_reset_table (next_uid
);
2575 cselib_current_insn
= NULL_RTX
;
2579 if (! INSN_P (insn
))
2581 cselib_current_insn
= NULL_RTX
;
2585 /* If this is a call instruction, forget anything stored in a
2586 call clobbered register, or, if this is not a const call, in
2590 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
2591 if (call_used_regs
[i
]
2592 || (REG_VALUES (i
) && REG_VALUES (i
)->elt
2593 && HARD_REGNO_CALL_PART_CLOBBERED (i
,
2594 GET_MODE (REG_VALUES (i
)->elt
->val_rtx
))))
2595 cselib_invalidate_regno (i
, reg_raw_mode
[i
]);
2597 /* Since it is not clear how cselib is going to be used, be
2598 conservative here and treat looping pure or const functions
2599 as if they were regular functions. */
2600 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn
)
2601 || !(RTL_CONST_OR_PURE_CALL_P (insn
)))
2602 cselib_invalidate_mem (callmem
);
2605 cselib_record_sets (insn
);
2607 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2608 after we have processed the insn. */
2610 for (x
= CALL_INSN_FUNCTION_USAGE (insn
); x
; x
= XEXP (x
, 1))
2611 if (GET_CODE (XEXP (x
, 0)) == CLOBBER
)
2612 cselib_invalidate_rtx (XEXP (XEXP (x
, 0), 0));
2614 cselib_current_insn
= NULL_RTX
;
2616 if (n_useless_values
> MAX_USELESS_VALUES
2617 /* remove_useless_values is linear in the hash table size. Avoid
2618 quadratic behavior for very large hashtables with very few
2619 useless elements. */
2620 && ((unsigned int)n_useless_values
2621 > (cselib_hash_table
->n_elements
2622 - cselib_hash_table
->n_deleted
2623 - n_debug_values
) / 4))
2624 remove_useless_values ();
2627 /* Initialize cselib for one pass. The caller must also call
2628 init_alias_analysis. */
2631 cselib_init (int record_what
)
2633 elt_list_pool
= create_alloc_pool ("elt_list",
2634 sizeof (struct elt_list
), 10);
2635 elt_loc_list_pool
= create_alloc_pool ("elt_loc_list",
2636 sizeof (struct elt_loc_list
), 10);
2637 cselib_val_pool
= create_alloc_pool ("cselib_val_list",
2638 sizeof (cselib_val
), 10);
2639 value_pool
= create_alloc_pool ("value", RTX_CODE_SIZE (VALUE
), 100);
2640 cselib_record_memory
= record_what
& CSELIB_RECORD_MEMORY
;
2641 cselib_preserve_constants
= record_what
& CSELIB_PRESERVE_CONSTANTS
;
2643 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2644 see canon_true_dependence. This is only created once. */
2646 callmem
= gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
));
2648 cselib_nregs
= max_reg_num ();
2650 /* We preserve reg_values to allow expensive clearing of the whole thing.
2651 Reallocate it however if it happens to be too large. */
2652 if (!reg_values
|| reg_values_size
< cselib_nregs
2653 || (reg_values_size
> 10 && reg_values_size
> cselib_nregs
* 4))
2656 /* Some space for newly emit instructions so we don't end up
2657 reallocating in between passes. */
2658 reg_values_size
= cselib_nregs
+ (63 + cselib_nregs
) / 16;
2659 reg_values
= XCNEWVEC (struct elt_list
*, reg_values_size
);
2661 used_regs
= XNEWVEC (unsigned int, cselib_nregs
);
2663 cselib_hash_table
= htab_create (31, get_value_hash
,
2664 entry_and_rtx_equal_p
, NULL
);
2668 /* Called when the current user is done with cselib. */
2671 cselib_finish (void)
2673 cselib_discard_hook
= NULL
;
2674 cselib_preserve_constants
= false;
2675 cfa_base_preserved_val
= NULL
;
2676 cfa_base_preserved_regno
= INVALID_REGNUM
;
2677 free_alloc_pool (elt_list_pool
);
2678 free_alloc_pool (elt_loc_list_pool
);
2679 free_alloc_pool (cselib_val_pool
);
2680 free_alloc_pool (value_pool
);
2681 cselib_clear_table ();
2682 htab_delete (cselib_hash_table
);
2685 cselib_hash_table
= 0;
2686 n_useless_values
= 0;
2687 n_useless_debug_values
= 0;
2692 /* Dump the cselib_val *X to FILE *info. */
2695 dump_cselib_val (void **x
, void *info
)
2697 cselib_val
*v
= (cselib_val
*)*x
;
2698 FILE *out
= (FILE *)info
;
2699 bool need_lf
= true;
2701 print_inline_rtx (out
, v
->val_rtx
, 0);
2705 struct elt_loc_list
*l
= v
->locs
;
2711 fputs (" locs:", out
);
2714 if (l
->setting_insn
)
2715 fprintf (out
, "\n from insn %i ",
2716 INSN_UID (l
->setting_insn
));
2718 fprintf (out
, "\n ");
2719 print_inline_rtx (out
, l
->loc
, 4);
2721 while ((l
= l
->next
));
2726 fputs (" no locs", out
);
2732 struct elt_list
*e
= v
->addr_list
;
2738 fputs (" addr list:", out
);
2742 print_inline_rtx (out
, e
->elt
->val_rtx
, 2);
2744 while ((e
= e
->next
));
2749 fputs (" no addrs", out
);
2753 if (v
->next_containing_mem
== &dummy_val
)
2754 fputs (" last mem\n", out
);
2755 else if (v
->next_containing_mem
)
2757 fputs (" next mem ", out
);
2758 print_inline_rtx (out
, v
->next_containing_mem
->val_rtx
, 2);
2767 /* Dump to OUT everything in the CSELIB table. */
2770 dump_cselib_table (FILE *out
)
2772 fprintf (out
, "cselib hash table:\n");
2773 htab_traverse (cselib_hash_table
, dump_cselib_val
, out
);
2774 if (first_containing_mem
!= &dummy_val
)
2776 fputs ("first mem ", out
);
2777 print_inline_rtx (out
, first_containing_mem
->val_rtx
, 2);
2780 fprintf (out
, "next uid %i\n", next_uid
);
2783 #include "gt-cselib.h"