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
4 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 struct elt_loc_list
*new_elt_loc_list (struct elt_loc_list
*, 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 unsigned int cselib_hash_rtx (rtx
, int);
66 static cselib_val
*new_cselib_val (unsigned int, enum machine_mode
, rtx
);
67 static void add_mem_for_addr (cselib_val
*, cselib_val
*, rtx
);
68 static cselib_val
*cselib_lookup_mem (rtx
, int);
69 static void cselib_invalidate_regno (unsigned int, enum machine_mode
);
70 static void cselib_invalidate_mem (rtx
);
71 static void cselib_record_set (rtx
, cselib_val
*, cselib_val
*);
72 static void cselib_record_sets (rtx
);
74 struct expand_value_data
77 cselib_expand_callback callback
;
82 static rtx
cselib_expand_value_rtx_1 (rtx
, struct expand_value_data
*, int);
84 /* There are three ways in which cselib can look up an rtx:
85 - for a REG, the reg_values table (which is indexed by regno) is used
86 - for a MEM, we recursively look up its address and then follow the
87 addr_list of that value
88 - for everything else, we compute a hash value and go through the hash
89 table. Since different rtx's can still have the same hash value,
90 this involves walking the table entries for a given value and comparing
91 the locations of the entries with the rtx we are looking up. */
93 /* A table that enables us to look up elts by their value. */
94 static htab_t cselib_hash_table
;
96 /* This is a global so we don't have to pass this through every function.
97 It is used in new_elt_loc_list to set SETTING_INSN. */
98 static rtx cselib_current_insn
;
100 /* The unique id that the next create value will take. */
101 static unsigned int next_uid
;
103 /* The number of registers we had when the varrays were last resized. */
104 static unsigned int cselib_nregs
;
106 /* Count values without known locations, or with only locations that
107 wouldn't have been known except for debug insns. Whenever this
108 grows too big, we remove these useless values from the table.
110 Counting values with only debug values is a bit tricky. We don't
111 want to increment n_useless_values when we create a value for a
112 debug insn, for this would get n_useless_values out of sync, but we
113 want increment it if all locs in the list that were ever referenced
114 in nondebug insns are removed from the list.
116 In the general case, once we do that, we'd have to stop accepting
117 nondebug expressions in the loc list, to avoid having two values
118 equivalent that, without debug insns, would have been made into
119 separate values. However, because debug insns never introduce
120 equivalences themselves (no assignments), the only means for
121 growing loc lists is through nondebug assignments. If the locs
122 also happen to be referenced in debug insns, it will work just fine.
124 A consequence of this is that there's at most one debug-only loc in
125 each loc list. If we keep it in the first entry, testing whether
126 we have a debug-only loc list takes O(1).
128 Furthermore, since any additional entry in a loc list containing a
129 debug loc would have to come from an assignment (nondebug) that
130 references both the initial debug loc and the newly-equivalent loc,
131 the initial debug loc would be promoted to a nondebug loc, and the
132 loc list would not contain debug locs any more.
134 So the only case we have to be careful with in order to keep
135 n_useless_values in sync between debug and nondebug compilations is
136 to avoid incrementing n_useless_values when removing the single loc
137 from a value that turns out to not appear outside debug values. We
138 increment n_useless_debug_values instead, and leave such values
139 alone until, for other reasons, we garbage-collect useless
141 static int n_useless_values
;
142 static int n_useless_debug_values
;
144 /* Count values whose locs have been taken exclusively from debug
145 insns for the entire life of the value. */
146 static int n_debug_values
;
148 /* Number of useless values before we remove them from the hash table. */
149 #define MAX_USELESS_VALUES 32
151 /* This table maps from register number to values. It does not
152 contain pointers to cselib_val structures, but rather elt_lists.
153 The purpose is to be able to refer to the same register in
154 different modes. The first element of the list defines the mode in
155 which the register was set; if the mode is unknown or the value is
156 no longer valid in that mode, ELT will be NULL for the first
158 static struct elt_list
**reg_values
;
159 static unsigned int reg_values_size
;
160 #define REG_VALUES(i) reg_values[i]
162 /* The largest number of hard regs used by any entry added to the
163 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
164 static unsigned int max_value_regs
;
166 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
167 in cselib_clear_table() for fast emptying. */
168 static unsigned int *used_regs
;
169 static unsigned int n_used_regs
;
171 /* We pass this to cselib_invalidate_mem to invalidate all of
172 memory for a non-const call instruction. */
173 static GTY(()) rtx callmem
;
175 /* Set by discard_useless_locs if it deleted the last location of any
177 static int values_became_useless
;
179 /* Used as stop element of the containing_mem list so we can check
180 presence in the list by checking the next pointer. */
181 static cselib_val dummy_val
;
183 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
184 that is constant through the whole function and should never be
186 static cselib_val
*cfa_base_preserved_val
;
187 static unsigned int cfa_base_preserved_regno
;
189 /* Used to list all values that contain memory reference.
190 May or may not contain the useless values - the list is compacted
191 each time memory is invalidated. */
192 static cselib_val
*first_containing_mem
= &dummy_val
;
193 static alloc_pool elt_loc_list_pool
, elt_list_pool
, cselib_val_pool
, value_pool
;
195 /* If nonnull, cselib will call this function before freeing useless
196 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
197 void (*cselib_discard_hook
) (cselib_val
*);
199 /* If nonnull, cselib will call this function before recording sets or
200 even clobbering outputs of INSN. All the recorded sets will be
201 represented in the array sets[n_sets]. new_val_min can be used to
202 tell whether values present in sets are introduced by this
204 void (*cselib_record_sets_hook
) (rtx insn
, struct cselib_set
*sets
,
207 #define PRESERVED_VALUE_P(RTX) \
208 (RTL_FLAG_CHECK1("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
212 /* Allocate a struct elt_list and fill in its two elements with the
215 static inline struct elt_list
*
216 new_elt_list (struct elt_list
*next
, cselib_val
*elt
)
219 el
= (struct elt_list
*) pool_alloc (elt_list_pool
);
225 /* Allocate a struct elt_loc_list and fill in its two elements with the
228 static inline struct elt_loc_list
*
229 new_elt_loc_list (struct elt_loc_list
*next
, rtx loc
)
231 struct elt_loc_list
*el
;
232 el
= (struct elt_loc_list
*) pool_alloc (elt_loc_list_pool
);
235 el
->setting_insn
= cselib_current_insn
;
236 gcc_assert (!next
|| !next
->setting_insn
237 || !DEBUG_INSN_P (next
->setting_insn
));
239 /* If we're creating the first loc in a debug insn context, we've
240 just created a debug value. Count it. */
241 if (!next
&& cselib_current_insn
&& DEBUG_INSN_P (cselib_current_insn
))
247 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
248 originating from a debug insn, maintaining the debug values
252 promote_debug_loc (struct elt_loc_list
*l
)
254 if (l
->setting_insn
&& DEBUG_INSN_P (l
->setting_insn
)
255 && (!cselib_current_insn
|| !DEBUG_INSN_P (cselib_current_insn
)))
258 l
->setting_insn
= cselib_current_insn
;
259 gcc_assert (!l
->next
);
263 /* The elt_list at *PL is no longer needed. Unchain it and free its
267 unchain_one_elt_list (struct elt_list
**pl
)
269 struct elt_list
*l
= *pl
;
272 pool_free (elt_list_pool
, l
);
275 /* Likewise for elt_loc_lists. */
278 unchain_one_elt_loc_list (struct elt_loc_list
**pl
)
280 struct elt_loc_list
*l
= *pl
;
283 pool_free (elt_loc_list_pool
, l
);
286 /* Likewise for cselib_vals. This also frees the addr_list associated with
290 unchain_one_value (cselib_val
*v
)
293 unchain_one_elt_list (&v
->addr_list
);
295 pool_free (cselib_val_pool
, v
);
298 /* Remove all entries from the hash table. Also used during
302 cselib_clear_table (void)
304 cselib_reset_table (1);
307 /* Remove from hash table all VALUEs except constants. */
310 preserve_only_constants (void **x
, void *info ATTRIBUTE_UNUSED
)
312 cselib_val
*v
= (cselib_val
*)*x
;
315 && v
->locs
->next
== NULL
)
317 if (CONSTANT_P (v
->locs
->loc
)
318 && (GET_CODE (v
->locs
->loc
) != CONST
319 || !references_value_p (v
->locs
->loc
, 0)))
321 if (cfa_base_preserved_val
)
323 if (v
== cfa_base_preserved_val
)
325 if (GET_CODE (v
->locs
->loc
) == PLUS
326 && CONST_INT_P (XEXP (v
->locs
->loc
, 1))
327 && XEXP (v
->locs
->loc
, 0) == cfa_base_preserved_val
->val_rtx
)
332 htab_clear_slot (cselib_hash_table
, x
);
336 /* Remove all entries from the hash table, arranging for the next
337 value to be numbered NUM. */
340 cselib_reset_table (unsigned int num
)
346 if (cfa_base_preserved_val
)
348 unsigned int regno
= cfa_base_preserved_regno
;
349 unsigned int new_used_regs
= 0;
350 for (i
= 0; i
< n_used_regs
; i
++)
351 if (used_regs
[i
] == regno
)
357 REG_VALUES (used_regs
[i
]) = 0;
358 gcc_assert (new_used_regs
== 1);
359 n_used_regs
= new_used_regs
;
360 used_regs
[0] = regno
;
362 = hard_regno_nregs
[regno
][GET_MODE (cfa_base_preserved_val
->locs
->loc
)];
366 for (i
= 0; i
< n_used_regs
; i
++)
367 REG_VALUES (used_regs
[i
]) = 0;
371 if (cselib_preserve_constants
)
372 htab_traverse (cselib_hash_table
, preserve_only_constants
, NULL
);
374 htab_empty (cselib_hash_table
);
376 n_useless_values
= 0;
377 n_useless_debug_values
= 0;
382 first_containing_mem
= &dummy_val
;
385 /* Return the number of the next value that will be generated. */
388 cselib_get_next_uid (void)
393 /* The equality test for our hash table. The first argument ENTRY is a table
394 element (i.e. a cselib_val), while the second arg X is an rtx. We know
395 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
396 CONST of an appropriate mode. */
399 entry_and_rtx_equal_p (const void *entry
, const void *x_arg
)
401 struct elt_loc_list
*l
;
402 const cselib_val
*const v
= (const cselib_val
*) entry
;
403 rtx x
= CONST_CAST_RTX ((const_rtx
)x_arg
);
404 enum machine_mode mode
= GET_MODE (x
);
406 gcc_assert (!CONST_INT_P (x
) && GET_CODE (x
) != CONST_FIXED
407 && (mode
!= VOIDmode
|| GET_CODE (x
) != CONST_DOUBLE
));
409 if (mode
!= GET_MODE (v
->val_rtx
))
412 /* Unwrap X if necessary. */
413 if (GET_CODE (x
) == CONST
414 && (CONST_INT_P (XEXP (x
, 0))
415 || GET_CODE (XEXP (x
, 0)) == CONST_FIXED
416 || GET_CODE (XEXP (x
, 0)) == CONST_DOUBLE
))
419 /* We don't guarantee that distinct rtx's have different hash values,
420 so we need to do a comparison. */
421 for (l
= v
->locs
; l
; l
= l
->next
)
422 if (rtx_equal_for_cselib_p (l
->loc
, x
))
424 promote_debug_loc (l
);
431 /* The hash function for our hash table. The value is always computed with
432 cselib_hash_rtx when adding an element; this function just extracts the
433 hash value from a cselib_val structure. */
436 get_value_hash (const void *entry
)
438 const cselib_val
*const v
= (const cselib_val
*) entry
;
442 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
443 only return true for values which point to a cselib_val whose value
444 element has been set to zero, which implies the cselib_val will be
448 references_value_p (const_rtx x
, int only_useless
)
450 const enum rtx_code code
= GET_CODE (x
);
451 const char *fmt
= GET_RTX_FORMAT (code
);
454 if (GET_CODE (x
) == VALUE
455 && (! only_useless
|| CSELIB_VAL_PTR (x
)->locs
== 0))
458 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
460 if (fmt
[i
] == 'e' && references_value_p (XEXP (x
, i
), only_useless
))
462 else if (fmt
[i
] == 'E')
463 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
464 if (references_value_p (XVECEXP (x
, i
, j
), only_useless
))
471 /* For all locations found in X, delete locations that reference useless
472 values (i.e. values without any location). Called through
476 discard_useless_locs (void **x
, void *info ATTRIBUTE_UNUSED
)
478 cselib_val
*v
= (cselib_val
*)*x
;
479 struct elt_loc_list
**p
= &v
->locs
;
480 bool had_locs
= v
->locs
!= NULL
;
481 rtx setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
485 if (references_value_p ((*p
)->loc
, 1))
486 unchain_one_elt_loc_list (p
);
491 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
493 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
494 n_useless_debug_values
++;
497 values_became_useless
= 1;
502 /* If X is a value with no locations, remove it from the hashtable. */
505 discard_useless_values (void **x
, void *info ATTRIBUTE_UNUSED
)
507 cselib_val
*v
= (cselib_val
*)*x
;
509 if (v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
511 if (cselib_discard_hook
)
512 cselib_discard_hook (v
);
514 CSELIB_VAL_PTR (v
->val_rtx
) = NULL
;
515 htab_clear_slot (cselib_hash_table
, x
);
516 unchain_one_value (v
);
523 /* Clean out useless values (i.e. those which no longer have locations
524 associated with them) from the hash table. */
527 remove_useless_values (void)
531 /* First pass: eliminate locations that reference the value. That in
532 turn can make more values useless. */
535 values_became_useless
= 0;
536 htab_traverse (cselib_hash_table
, discard_useless_locs
, 0);
538 while (values_became_useless
);
540 /* Second pass: actually remove the values. */
542 p
= &first_containing_mem
;
543 for (v
= *p
; v
!= &dummy_val
; v
= v
->next_containing_mem
)
547 p
= &(*p
)->next_containing_mem
;
551 n_useless_values
+= n_useless_debug_values
;
552 n_debug_values
-= n_useless_debug_values
;
553 n_useless_debug_values
= 0;
555 htab_traverse (cselib_hash_table
, discard_useless_values
, 0);
557 gcc_assert (!n_useless_values
);
560 /* Arrange for a value to not be removed from the hash table even if
561 it becomes useless. */
564 cselib_preserve_value (cselib_val
*v
)
566 PRESERVED_VALUE_P (v
->val_rtx
) = 1;
569 /* Test whether a value is preserved. */
572 cselib_preserved_value_p (cselib_val
*v
)
574 return PRESERVED_VALUE_P (v
->val_rtx
);
577 /* Arrange for a REG value to be assumed constant through the whole function,
578 never invalidated and preserved across cselib_reset_table calls. */
581 cselib_preserve_cfa_base_value (cselib_val
*v
, unsigned int regno
)
583 if (cselib_preserve_constants
585 && REG_P (v
->locs
->loc
))
587 cfa_base_preserved_val
= v
;
588 cfa_base_preserved_regno
= regno
;
592 /* Clean all non-constant expressions in the hash table, but retain
596 cselib_preserve_only_values (void)
600 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
601 cselib_invalidate_regno (i
, reg_raw_mode
[i
]);
603 cselib_invalidate_mem (callmem
);
605 remove_useless_values ();
607 gcc_assert (first_containing_mem
== &dummy_val
);
610 /* Return the mode in which a register was last set. If X is not a
611 register, return its mode. If the mode in which the register was
612 set is not known, or the value was already clobbered, return
616 cselib_reg_set_mode (const_rtx x
)
621 if (REG_VALUES (REGNO (x
)) == NULL
622 || REG_VALUES (REGNO (x
))->elt
== NULL
)
625 return GET_MODE (REG_VALUES (REGNO (x
))->elt
->val_rtx
);
628 /* Return nonzero if we can prove that X and Y contain the same value, taking
629 our gathered information into account. */
632 rtx_equal_for_cselib_p (rtx x
, rtx y
)
638 if (REG_P (x
) || MEM_P (x
))
640 cselib_val
*e
= cselib_lookup (x
, GET_MODE (x
), 0);
646 if (REG_P (y
) || MEM_P (y
))
648 cselib_val
*e
= cselib_lookup (y
, GET_MODE (y
), 0);
657 if (GET_CODE (x
) == VALUE
&& GET_CODE (y
) == VALUE
)
658 return CSELIB_VAL_PTR (x
) == CSELIB_VAL_PTR (y
);
660 if (GET_CODE (x
) == VALUE
)
662 cselib_val
*e
= CSELIB_VAL_PTR (x
);
663 struct elt_loc_list
*l
;
665 for (l
= e
->locs
; l
; l
= l
->next
)
669 /* Avoid infinite recursion. */
670 if (REG_P (t
) || MEM_P (t
))
672 else if (rtx_equal_for_cselib_p (t
, y
))
679 if (GET_CODE (y
) == VALUE
)
681 cselib_val
*e
= CSELIB_VAL_PTR (y
);
682 struct elt_loc_list
*l
;
684 for (l
= e
->locs
; l
; l
= l
->next
)
688 if (REG_P (t
) || MEM_P (t
))
690 else if (rtx_equal_for_cselib_p (x
, t
))
697 if (GET_CODE (x
) != GET_CODE (y
) || GET_MODE (x
) != GET_MODE (y
))
700 /* These won't be handled correctly by the code below. */
701 switch (GET_CODE (x
))
708 case DEBUG_IMPLICIT_PTR
:
709 return DEBUG_IMPLICIT_PTR_DECL (x
)
710 == DEBUG_IMPLICIT_PTR_DECL (y
);
713 return XEXP (x
, 0) == XEXP (y
, 0);
720 fmt
= GET_RTX_FORMAT (code
);
722 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
729 if (XWINT (x
, i
) != XWINT (y
, i
))
735 if (XINT (x
, i
) != XINT (y
, i
))
741 /* Two vectors must have the same length. */
742 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
745 /* And the corresponding elements must match. */
746 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
747 if (! rtx_equal_for_cselib_p (XVECEXP (x
, i
, j
),
754 && targetm
.commutative_p (x
, UNKNOWN
)
755 && rtx_equal_for_cselib_p (XEXP (x
, 1), XEXP (y
, 0))
756 && rtx_equal_for_cselib_p (XEXP (x
, 0), XEXP (y
, 1)))
758 if (! rtx_equal_for_cselib_p (XEXP (x
, i
), XEXP (y
, i
)))
764 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
769 /* These are just backpointers, so they don't matter. */
776 /* It is believed that rtx's at this level will never
777 contain anything but integers and other rtx's,
778 except for within LABEL_REFs and SYMBOL_REFs. */
786 /* We need to pass down the mode of constants through the hash table
787 functions. For that purpose, wrap them in a CONST of the appropriate
790 wrap_constant (enum machine_mode mode
, rtx x
)
792 if (!CONST_INT_P (x
) && GET_CODE (x
) != CONST_FIXED
793 && (GET_CODE (x
) != CONST_DOUBLE
|| GET_MODE (x
) != VOIDmode
))
795 gcc_assert (mode
!= VOIDmode
);
796 return gen_rtx_CONST (mode
, x
);
799 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
800 For registers and memory locations, we look up their cselib_val structure
801 and return its VALUE element.
802 Possible reasons for return 0 are: the object is volatile, or we couldn't
803 find a register or memory location in the table and CREATE is zero. If
804 CREATE is nonzero, table elts are created for regs and mem.
805 N.B. this hash function returns the same hash value for RTXes that
806 differ only in the order of operands, thus it is suitable for comparisons
807 that take commutativity into account.
808 If we wanted to also support associative rules, we'd have to use a different
809 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
810 We used to have a MODE argument for hashing for CONST_INTs, but that
811 didn't make sense, since it caused spurious hash differences between
812 (set (reg:SI 1) (const_int))
813 (plus:SI (reg:SI 2) (reg:SI 1))
815 (plus:SI (reg:SI 2) (const_int))
816 If the mode is important in any context, it must be checked specifically
817 in a comparison anyway, since relying on hash differences is unsafe. */
820 cselib_hash_rtx (rtx x
, int create
)
826 unsigned int hash
= 0;
829 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
835 e
= cselib_lookup (x
, GET_MODE (x
), create
);
842 hash
+= ((unsigned) DEBUG_EXPR
<< 7)
843 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x
));
844 return hash
? hash
: (unsigned int) DEBUG_EXPR
;
846 case DEBUG_IMPLICIT_PTR
:
847 hash
+= ((unsigned) DEBUG_IMPLICIT_PTR
<< 7)
848 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x
));
849 return hash
? hash
: (unsigned int) DEBUG_IMPLICIT_PTR
;
852 hash
+= ((unsigned) CONST_INT
<< 7) + INTVAL (x
);
853 return hash
? hash
: (unsigned int) CONST_INT
;
856 /* This is like the general case, except that it only counts
857 the integers representing the constant. */
858 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
859 if (GET_MODE (x
) != VOIDmode
)
860 hash
+= real_hash (CONST_DOUBLE_REAL_VALUE (x
));
862 hash
+= ((unsigned) CONST_DOUBLE_LOW (x
)
863 + (unsigned) CONST_DOUBLE_HIGH (x
));
864 return hash
? hash
: (unsigned int) CONST_DOUBLE
;
867 hash
+= (unsigned int) code
+ (unsigned int) GET_MODE (x
);
868 hash
+= fixed_hash (CONST_FIXED_VALUE (x
));
869 return hash
? hash
: (unsigned int) CONST_FIXED
;
876 units
= CONST_VECTOR_NUNITS (x
);
878 for (i
= 0; i
< units
; ++i
)
880 elt
= CONST_VECTOR_ELT (x
, i
);
881 hash
+= cselib_hash_rtx (elt
, 0);
887 /* Assume there is only one rtx object for any given label. */
889 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
890 differences and differences between each stage's debugging dumps. */
891 hash
+= (((unsigned int) LABEL_REF
<< 7)
892 + CODE_LABEL_NUMBER (XEXP (x
, 0)));
893 return hash
? hash
: (unsigned int) LABEL_REF
;
897 /* Don't hash on the symbol's address to avoid bootstrap differences.
898 Different hash values may cause expressions to be recorded in
899 different orders and thus different registers to be used in the
900 final assembler. This also avoids differences in the dump files
901 between various stages. */
903 const unsigned char *p
= (const unsigned char *) XSTR (x
, 0);
906 h
+= (h
<< 7) + *p
++; /* ??? revisit */
908 hash
+= ((unsigned int) SYMBOL_REF
<< 7) + h
;
909 return hash
? hash
: (unsigned int) SYMBOL_REF
;
921 case UNSPEC_VOLATILE
:
925 if (MEM_VOLATILE_P (x
))
934 i
= GET_RTX_LENGTH (code
) - 1;
935 fmt
= GET_RTX_FORMAT (code
);
942 rtx tem
= XEXP (x
, i
);
943 unsigned int tem_hash
= cselib_hash_rtx (tem
, create
);
952 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
954 unsigned int tem_hash
955 = cselib_hash_rtx (XVECEXP (x
, i
, j
), create
);
966 const unsigned char *p
= (const unsigned char *) XSTR (x
, i
);
988 return hash
? hash
: 1 + (unsigned int) GET_CODE (x
);
991 /* Create a new value structure for VALUE and initialize it. The mode of the
994 static inline cselib_val
*
995 new_cselib_val (unsigned int hash
, enum machine_mode mode
, rtx x
)
997 cselib_val
*e
= (cselib_val
*) pool_alloc (cselib_val_pool
);
1000 gcc_assert (next_uid
);
1003 e
->uid
= next_uid
++;
1004 /* We use an alloc pool to allocate this RTL construct because it
1005 accounts for about 8% of the overall memory usage. We know
1006 precisely when we can have VALUE RTXen (when cselib is active)
1007 so we don't need to put them in garbage collected memory.
1008 ??? Why should a VALUE be an RTX in the first place? */
1009 e
->val_rtx
= (rtx
) pool_alloc (value_pool
);
1010 memset (e
->val_rtx
, 0, RTX_HDR_SIZE
);
1011 PUT_CODE (e
->val_rtx
, VALUE
);
1012 PUT_MODE (e
->val_rtx
, mode
);
1013 CSELIB_VAL_PTR (e
->val_rtx
) = e
;
1016 e
->next_containing_mem
= 0;
1018 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1020 fprintf (dump_file
, "cselib value %u:%u ", e
->uid
, hash
);
1021 if (flag_dump_noaddr
|| flag_dump_unnumbered
)
1022 fputs ("# ", dump_file
);
1024 fprintf (dump_file
, "%p ", (void*)e
);
1025 print_rtl_single (dump_file
, x
);
1026 fputc ('\n', dump_file
);
1032 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1033 contains the data at this address. X is a MEM that represents the
1034 value. Update the two value structures to represent this situation. */
1037 add_mem_for_addr (cselib_val
*addr_elt
, cselib_val
*mem_elt
, rtx x
)
1039 struct elt_loc_list
*l
;
1041 /* Avoid duplicates. */
1042 for (l
= mem_elt
->locs
; l
; l
= l
->next
)
1044 && CSELIB_VAL_PTR (XEXP (l
->loc
, 0)) == addr_elt
)
1046 promote_debug_loc (l
);
1050 addr_elt
->addr_list
= new_elt_list (addr_elt
->addr_list
, mem_elt
);
1052 = new_elt_loc_list (mem_elt
->locs
,
1053 replace_equiv_address_nv (x
, addr_elt
->val_rtx
));
1054 if (mem_elt
->next_containing_mem
== NULL
)
1056 mem_elt
->next_containing_mem
= first_containing_mem
;
1057 first_containing_mem
= mem_elt
;
1061 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1062 If CREATE, make a new one if we haven't seen it before. */
1065 cselib_lookup_mem (rtx x
, int create
)
1067 enum machine_mode mode
= GET_MODE (x
);
1070 cselib_val
*mem_elt
;
1073 if (MEM_VOLATILE_P (x
) || mode
== BLKmode
1074 || !cselib_record_memory
1075 || (FLOAT_MODE_P (mode
) && flag_float_store
))
1078 /* Look up the value for the address. */
1079 addr
= cselib_lookup (XEXP (x
, 0), mode
, create
);
1083 /* Find a value that describes a value of our mode at that address. */
1084 for (l
= addr
->addr_list
; l
; l
= l
->next
)
1085 if (GET_MODE (l
->elt
->val_rtx
) == mode
)
1087 promote_debug_loc (l
->elt
->locs
);
1094 mem_elt
= new_cselib_val (next_uid
, mode
, x
);
1095 add_mem_for_addr (addr
, mem_elt
, x
);
1096 slot
= htab_find_slot_with_hash (cselib_hash_table
, wrap_constant (mode
, x
),
1097 mem_elt
->hash
, INSERT
);
1102 /* Search thru the possible substitutions in P. We prefer a non reg
1103 substitution because this allows us to expand the tree further. If
1104 we find, just a reg, take the lowest regno. There may be several
1105 non-reg results, we just take the first one because they will all
1106 expand to the same place. */
1109 expand_loc (struct elt_loc_list
*p
, struct expand_value_data
*evd
,
1112 rtx reg_result
= NULL
;
1113 unsigned int regno
= UINT_MAX
;
1114 struct elt_loc_list
*p_in
= p
;
1116 for (; p
; p
= p
-> next
)
1118 /* Avoid infinite recursion trying to expand a reg into a
1120 if ((REG_P (p
->loc
))
1121 && (REGNO (p
->loc
) < regno
)
1122 && !bitmap_bit_p (evd
->regs_active
, REGNO (p
->loc
)))
1124 reg_result
= p
->loc
;
1125 regno
= REGNO (p
->loc
);
1127 /* Avoid infinite recursion and do not try to expand the
1129 else if (GET_CODE (p
->loc
) == VALUE
1130 && CSELIB_VAL_PTR (p
->loc
)->locs
== p_in
)
1132 else if (!REG_P (p
->loc
))
1135 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1137 print_inline_rtx (dump_file
, p
->loc
, 0);
1138 fprintf (dump_file
, "\n");
1140 if (GET_CODE (p
->loc
) == LO_SUM
1141 && GET_CODE (XEXP (p
->loc
, 1)) == SYMBOL_REF
1143 && (note
= find_reg_note (p
->setting_insn
, REG_EQUAL
, NULL_RTX
))
1144 && XEXP (note
, 0) == XEXP (p
->loc
, 1))
1145 return XEXP (p
->loc
, 1);
1146 result
= cselib_expand_value_rtx_1 (p
->loc
, evd
, max_depth
- 1);
1153 if (regno
!= UINT_MAX
)
1156 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1157 fprintf (dump_file
, "r%d\n", regno
);
1159 result
= cselib_expand_value_rtx_1 (reg_result
, evd
, max_depth
- 1);
1164 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1168 print_inline_rtx (dump_file
, reg_result
, 0);
1169 fprintf (dump_file
, "\n");
1172 fprintf (dump_file
, "NULL\n");
1178 /* Forward substitute and expand an expression out to its roots.
1179 This is the opposite of common subexpression. Because local value
1180 numbering is such a weak optimization, the expanded expression is
1181 pretty much unique (not from a pointer equals point of view but
1182 from a tree shape point of view.
1184 This function returns NULL if the expansion fails. The expansion
1185 will fail if there is no value number for one of the operands or if
1186 one of the operands has been overwritten between the current insn
1187 and the beginning of the basic block. For instance x has no
1193 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1194 It is clear on return. */
1197 cselib_expand_value_rtx (rtx orig
, bitmap regs_active
, int max_depth
)
1199 struct expand_value_data evd
;
1201 evd
.regs_active
= regs_active
;
1202 evd
.callback
= NULL
;
1203 evd
.callback_arg
= NULL
;
1206 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
);
1209 /* Same as cselib_expand_value_rtx, but using a callback to try to
1210 resolve some expressions. The CB function should return ORIG if it
1211 can't or does not want to deal with a certain RTX. Any other
1212 return value, including NULL, will be used as the expansion for
1213 VALUE, without any further changes. */
1216 cselib_expand_value_rtx_cb (rtx orig
, bitmap regs_active
, int max_depth
,
1217 cselib_expand_callback cb
, void *data
)
1219 struct expand_value_data evd
;
1221 evd
.regs_active
= regs_active
;
1223 evd
.callback_arg
= data
;
1226 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
);
1229 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1230 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1231 would return NULL or non-NULL, without allocating new rtx. */
1234 cselib_dummy_expand_value_rtx_cb (rtx orig
, bitmap regs_active
, int max_depth
,
1235 cselib_expand_callback cb
, void *data
)
1237 struct expand_value_data evd
;
1239 evd
.regs_active
= regs_active
;
1241 evd
.callback_arg
= data
;
1244 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
) != NULL
;
1247 /* Internal implementation of cselib_expand_value_rtx and
1248 cselib_expand_value_rtx_cb. */
1251 cselib_expand_value_rtx_1 (rtx orig
, struct expand_value_data
*evd
,
1257 const char *format_ptr
;
1258 enum machine_mode mode
;
1260 code
= GET_CODE (orig
);
1262 /* For the context of dse, if we end up expand into a huge tree, we
1263 will not have a useful address, so we might as well just give up
1272 struct elt_list
*l
= REG_VALUES (REGNO (orig
));
1274 if (l
&& l
->elt
== NULL
)
1276 for (; l
; l
= l
->next
)
1277 if (GET_MODE (l
->elt
->val_rtx
) == GET_MODE (orig
))
1280 int regno
= REGNO (orig
);
1282 /* The only thing that we are not willing to do (this
1283 is requirement of dse and if others potential uses
1284 need this function we should add a parm to control
1285 it) is that we will not substitute the
1286 STACK_POINTER_REGNUM, FRAME_POINTER or the
1289 These expansions confuses the code that notices that
1290 stores into the frame go dead at the end of the
1291 function and that the frame is not effected by calls
1292 to subroutines. If you allow the
1293 STACK_POINTER_REGNUM substitution, then dse will
1294 think that parameter pushing also goes dead which is
1295 wrong. If you allow the FRAME_POINTER or the
1296 HARD_FRAME_POINTER then you lose the opportunity to
1297 make the frame assumptions. */
1298 if (regno
== STACK_POINTER_REGNUM
1299 || regno
== FRAME_POINTER_REGNUM
1300 || regno
== HARD_FRAME_POINTER_REGNUM
)
1303 bitmap_set_bit (evd
->regs_active
, regno
);
1305 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1306 fprintf (dump_file
, "expanding: r%d into: ", regno
);
1308 result
= expand_loc (l
->elt
->locs
, evd
, max_depth
);
1309 bitmap_clear_bit (evd
->regs_active
, regno
);
1326 /* SCRATCH must be shared because they represent distinct values. */
1329 if (REG_P (XEXP (orig
, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig
, 0))))
1334 if (shared_const_p (orig
))
1344 subreg
= evd
->callback (orig
, evd
->regs_active
, max_depth
,
1350 subreg
= cselib_expand_value_rtx_1 (SUBREG_REG (orig
), evd
,
1354 scopy
= simplify_gen_subreg (GET_MODE (orig
), subreg
,
1355 GET_MODE (SUBREG_REG (orig
)),
1356 SUBREG_BYTE (orig
));
1358 || (GET_CODE (scopy
) == SUBREG
1359 && !REG_P (SUBREG_REG (scopy
))
1360 && !MEM_P (SUBREG_REG (scopy
))))
1370 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1372 fputs ("\nexpanding ", dump_file
);
1373 print_rtl_single (dump_file
, orig
);
1374 fputs (" into...", dump_file
);
1379 result
= evd
->callback (orig
, evd
->regs_active
, max_depth
,
1386 result
= expand_loc (CSELIB_VAL_PTR (orig
)->locs
, evd
, max_depth
);
1392 return evd
->callback (orig
, evd
->regs_active
, max_depth
,
1400 /* Copy the various flags, fields, and other information. We assume
1401 that all fields need copying, and then clear the fields that should
1402 not be copied. That is the sensible default behavior, and forces
1403 us to explicitly document why we are *not* copying a flag. */
1407 copy
= shallow_copy_rtx (orig
);
1409 format_ptr
= GET_RTX_FORMAT (code
);
1411 for (i
= 0; i
< GET_RTX_LENGTH (code
); i
++)
1412 switch (*format_ptr
++)
1415 if (XEXP (orig
, i
) != NULL
)
1417 rtx result
= cselib_expand_value_rtx_1 (XEXP (orig
, i
), evd
,
1422 XEXP (copy
, i
) = result
;
1428 if (XVEC (orig
, i
) != NULL
)
1431 XVEC (copy
, i
) = rtvec_alloc (XVECLEN (orig
, i
));
1432 for (j
= 0; j
< XVECLEN (orig
, i
); j
++)
1434 rtx result
= cselib_expand_value_rtx_1 (XVECEXP (orig
, i
, j
),
1435 evd
, max_depth
- 1);
1439 XVECEXP (copy
, i
, j
) = result
;
1453 /* These are left unchanged. */
1463 mode
= GET_MODE (copy
);
1464 /* If an operand has been simplified into CONST_INT, which doesn't
1465 have a mode and the mode isn't derivable from whole rtx's mode,
1466 try simplify_*_operation first with mode from original's operand
1467 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1469 switch (GET_RTX_CLASS (code
))
1472 if (CONST_INT_P (XEXP (copy
, 0))
1473 && GET_MODE (XEXP (orig
, 0)) != VOIDmode
)
1475 scopy
= simplify_unary_operation (code
, mode
, XEXP (copy
, 0),
1476 GET_MODE (XEXP (orig
, 0)));
1481 case RTX_COMM_ARITH
:
1483 /* These expressions can derive operand modes from the whole rtx's mode. */
1486 case RTX_BITFIELD_OPS
:
1487 if (CONST_INT_P (XEXP (copy
, 0))
1488 && GET_MODE (XEXP (orig
, 0)) != VOIDmode
)
1490 scopy
= simplify_ternary_operation (code
, mode
,
1491 GET_MODE (XEXP (orig
, 0)),
1492 XEXP (copy
, 0), XEXP (copy
, 1),
1499 case RTX_COMM_COMPARE
:
1500 if (CONST_INT_P (XEXP (copy
, 0))
1501 && GET_MODE (XEXP (copy
, 1)) == VOIDmode
1502 && (GET_MODE (XEXP (orig
, 0)) != VOIDmode
1503 || GET_MODE (XEXP (orig
, 1)) != VOIDmode
))
1505 scopy
= simplify_relational_operation (code
, mode
,
1506 (GET_MODE (XEXP (orig
, 0))
1508 ? GET_MODE (XEXP (orig
, 0))
1509 : GET_MODE (XEXP (orig
, 1)),
1519 scopy
= simplify_rtx (copy
);
1525 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1526 with VALUE expressions. This way, it becomes independent of changes
1527 to registers and memory.
1528 X isn't actually modified; if modifications are needed, new rtl is
1529 allocated. However, the return value can share rtl with X. */
1532 cselib_subst_to_values (rtx x
)
1534 enum rtx_code code
= GET_CODE (x
);
1535 const char *fmt
= GET_RTX_FORMAT (code
);
1544 l
= REG_VALUES (REGNO (x
));
1545 if (l
&& l
->elt
== NULL
)
1547 for (; l
; l
= l
->next
)
1548 if (GET_MODE (l
->elt
->val_rtx
) == GET_MODE (x
))
1549 return l
->elt
->val_rtx
;
1554 e
= cselib_lookup_mem (x
, 0);
1557 /* This happens for autoincrements. Assign a value that doesn't
1559 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
1575 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
1582 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1586 rtx t
= cselib_subst_to_values (XEXP (x
, i
));
1588 if (t
!= XEXP (x
, i
))
1591 copy
= shallow_copy_rtx (x
);
1595 else if (fmt
[i
] == 'E')
1599 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1601 rtx t
= cselib_subst_to_values (XVECEXP (x
, i
, j
));
1603 if (t
!= XVECEXP (x
, i
, j
))
1605 if (XVEC (x
, i
) == XVEC (copy
, i
))
1608 copy
= shallow_copy_rtx (x
);
1609 XVEC (copy
, i
) = shallow_copy_rtvec (XVEC (x
, i
));
1611 XVECEXP (copy
, i
, j
) = t
;
1620 /* Look up the rtl expression X in our tables and return the value it has.
1621 If CREATE is zero, we return NULL if we don't know the value. Otherwise,
1622 we create a new one if possible, using mode MODE if X doesn't have a mode
1623 (i.e. because it's a constant). */
1626 cselib_lookup_1 (rtx x
, enum machine_mode mode
, int create
)
1630 unsigned int hashval
;
1632 if (GET_MODE (x
) != VOIDmode
)
1633 mode
= GET_MODE (x
);
1635 if (GET_CODE (x
) == VALUE
)
1636 return CSELIB_VAL_PTR (x
);
1641 unsigned int i
= REGNO (x
);
1644 if (l
&& l
->elt
== NULL
)
1646 for (; l
; l
= l
->next
)
1647 if (mode
== GET_MODE (l
->elt
->val_rtx
))
1649 promote_debug_loc (l
->elt
->locs
);
1656 if (i
< FIRST_PSEUDO_REGISTER
)
1658 unsigned int n
= hard_regno_nregs
[i
][mode
];
1660 if (n
> max_value_regs
)
1664 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
1665 e
->locs
= new_elt_loc_list (e
->locs
, x
);
1666 if (REG_VALUES (i
) == 0)
1668 /* Maintain the invariant that the first entry of
1669 REG_VALUES, if present, must be the value used to set the
1670 register, or NULL. */
1671 used_regs
[n_used_regs
++] = i
;
1672 REG_VALUES (i
) = new_elt_list (REG_VALUES (i
), NULL
);
1674 REG_VALUES (i
)->next
= new_elt_list (REG_VALUES (i
)->next
, e
);
1675 slot
= htab_find_slot_with_hash (cselib_hash_table
, x
, e
->hash
, INSERT
);
1681 return cselib_lookup_mem (x
, create
);
1683 hashval
= cselib_hash_rtx (x
, create
);
1684 /* Can't even create if hashing is not possible. */
1688 slot
= htab_find_slot_with_hash (cselib_hash_table
, wrap_constant (mode
, x
),
1689 hashval
, create
? INSERT
: NO_INSERT
);
1693 e
= (cselib_val
*) *slot
;
1697 e
= new_cselib_val (hashval
, mode
, x
);
1699 /* We have to fill the slot before calling cselib_subst_to_values:
1700 the hash table is inconsistent until we do so, and
1701 cselib_subst_to_values will need to do lookups. */
1703 e
->locs
= new_elt_loc_list (e
->locs
, cselib_subst_to_values (x
));
1707 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
1710 cselib_lookup_from_insn (rtx x
, enum machine_mode mode
,
1711 int create
, rtx insn
)
1715 gcc_assert (!cselib_current_insn
);
1716 cselib_current_insn
= insn
;
1718 ret
= cselib_lookup (x
, mode
, create
);
1720 cselib_current_insn
= NULL
;
1725 /* Wrapper for cselib_lookup_1, that logs the lookup result and
1726 maintains invariants related with debug insns. */
1729 cselib_lookup (rtx x
, enum machine_mode mode
, int create
)
1731 cselib_val
*ret
= cselib_lookup_1 (x
, mode
, create
);
1733 /* ??? Should we return NULL if we're not to create an entry, the
1734 found loc is a debug loc and cselib_current_insn is not DEBUG?
1735 If so, we should also avoid converting val to non-DEBUG; probably
1736 easiest setting cselib_current_insn to NULL before the call
1739 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1741 fputs ("cselib lookup ", dump_file
);
1742 print_inline_rtx (dump_file
, x
, 2);
1743 fprintf (dump_file
, " => %u:%u\n",
1745 ret
? ret
->hash
: 0);
1751 /* Invalidate any entries in reg_values that overlap REGNO. This is called
1752 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
1753 is used to determine how many hard registers are being changed. If MODE
1754 is VOIDmode, then only REGNO is being changed; this is used when
1755 invalidating call clobbered registers across a call. */
1758 cselib_invalidate_regno (unsigned int regno
, enum machine_mode mode
)
1760 unsigned int endregno
;
1763 /* If we see pseudos after reload, something is _wrong_. */
1764 gcc_assert (!reload_completed
|| regno
< FIRST_PSEUDO_REGISTER
1765 || reg_renumber
[regno
] < 0);
1767 /* Determine the range of registers that must be invalidated. For
1768 pseudos, only REGNO is affected. For hard regs, we must take MODE
1769 into account, and we must also invalidate lower register numbers
1770 if they contain values that overlap REGNO. */
1771 if (regno
< FIRST_PSEUDO_REGISTER
)
1773 gcc_assert (mode
!= VOIDmode
);
1775 if (regno
< max_value_regs
)
1778 i
= regno
- max_value_regs
;
1780 endregno
= end_hard_regno (mode
, regno
);
1785 endregno
= regno
+ 1;
1788 for (; i
< endregno
; i
++)
1790 struct elt_list
**l
= ®_VALUES (i
);
1792 /* Go through all known values for this reg; if it overlaps the range
1793 we're invalidating, remove the value. */
1796 cselib_val
*v
= (*l
)->elt
;
1799 struct elt_loc_list
**p
;
1800 unsigned int this_last
= i
;
1802 if (i
< FIRST_PSEUDO_REGISTER
&& v
!= NULL
)
1803 this_last
= end_hard_regno (GET_MODE (v
->val_rtx
), i
) - 1;
1805 if (this_last
< regno
|| v
== NULL
1806 || (v
== cfa_base_preserved_val
1807 && i
== cfa_base_preserved_regno
))
1813 /* We have an overlap. */
1814 if (*l
== REG_VALUES (i
))
1816 /* Maintain the invariant that the first entry of
1817 REG_VALUES, if present, must be the value used to set
1818 the register, or NULL. This is also nice because
1819 then we won't push the same regno onto user_regs
1825 unchain_one_elt_list (l
);
1827 had_locs
= v
->locs
!= NULL
;
1828 setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
1830 /* Now, we clear the mapping from value to reg. It must exist, so
1831 this code will crash intentionally if it doesn't. */
1832 for (p
= &v
->locs
; ; p
= &(*p
)->next
)
1836 if (REG_P (x
) && REGNO (x
) == i
)
1838 unchain_one_elt_loc_list (p
);
1843 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
1845 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
1846 n_useless_debug_values
++;
1854 /* Return 1 if X has a value that can vary even between two
1855 executions of the program. 0 means X can be compared reliably
1856 against certain constants or near-constants. */
1859 cselib_rtx_varies_p (const_rtx x ATTRIBUTE_UNUSED
, bool from_alias ATTRIBUTE_UNUSED
)
1861 /* We actually don't need to verify very hard. This is because
1862 if X has actually changed, we invalidate the memory anyway,
1863 so assume that all common memory addresses are
1868 /* Invalidate any locations in the table which are changed because of a
1869 store to MEM_RTX. If this is called because of a non-const call
1870 instruction, MEM_RTX is (mem:BLK const0_rtx). */
1873 cselib_invalidate_mem (rtx mem_rtx
)
1875 cselib_val
**vp
, *v
, *next
;
1879 mem_addr
= canon_rtx (get_addr (XEXP (mem_rtx
, 0)));
1880 mem_rtx
= canon_rtx (mem_rtx
);
1882 vp
= &first_containing_mem
;
1883 for (v
= *vp
; v
!= &dummy_val
; v
= next
)
1885 bool has_mem
= false;
1886 struct elt_loc_list
**p
= &v
->locs
;
1887 bool had_locs
= v
->locs
!= NULL
;
1888 rtx setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
1894 struct elt_list
**mem_chain
;
1896 /* MEMs may occur in locations only at the top level; below
1897 that every MEM or REG is substituted by its VALUE. */
1903 if (num_mems
< PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS
)
1904 && ! canon_true_dependence (mem_rtx
, GET_MODE (mem_rtx
), mem_addr
,
1905 x
, NULL_RTX
, cselib_rtx_varies_p
))
1913 /* This one overlaps. */
1914 /* We must have a mapping from this MEM's address to the
1915 value (E). Remove that, too. */
1916 addr
= cselib_lookup (XEXP (x
, 0), VOIDmode
, 0);
1917 mem_chain
= &addr
->addr_list
;
1920 if ((*mem_chain
)->elt
== v
)
1922 unchain_one_elt_list (mem_chain
);
1926 mem_chain
= &(*mem_chain
)->next
;
1929 unchain_one_elt_loc_list (p
);
1932 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
1934 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
1935 n_useless_debug_values
++;
1940 next
= v
->next_containing_mem
;
1944 vp
= &(*vp
)->next_containing_mem
;
1947 v
->next_containing_mem
= NULL
;
1952 /* Invalidate DEST, which is being assigned to or clobbered. */
1955 cselib_invalidate_rtx (rtx dest
)
1957 while (GET_CODE (dest
) == SUBREG
1958 || GET_CODE (dest
) == ZERO_EXTRACT
1959 || GET_CODE (dest
) == STRICT_LOW_PART
)
1960 dest
= XEXP (dest
, 0);
1963 cselib_invalidate_regno (REGNO (dest
), GET_MODE (dest
));
1964 else if (MEM_P (dest
))
1965 cselib_invalidate_mem (dest
);
1967 /* Some machines don't define AUTO_INC_DEC, but they still use push
1968 instructions. We need to catch that case here in order to
1969 invalidate the stack pointer correctly. Note that invalidating
1970 the stack pointer is different from invalidating DEST. */
1971 if (push_operand (dest
, GET_MODE (dest
)))
1972 cselib_invalidate_rtx (stack_pointer_rtx
);
1975 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
1978 cselib_invalidate_rtx_note_stores (rtx dest
, const_rtx ignore ATTRIBUTE_UNUSED
,
1979 void *data ATTRIBUTE_UNUSED
)
1981 cselib_invalidate_rtx (dest
);
1984 /* Record the result of a SET instruction. DEST is being set; the source
1985 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
1986 describes its address. */
1989 cselib_record_set (rtx dest
, cselib_val
*src_elt
, cselib_val
*dest_addr_elt
)
1991 int dreg
= REG_P (dest
) ? (int) REGNO (dest
) : -1;
1993 if (src_elt
== 0 || side_effects_p (dest
))
1998 if (dreg
< FIRST_PSEUDO_REGISTER
)
2000 unsigned int n
= hard_regno_nregs
[dreg
][GET_MODE (dest
)];
2002 if (n
> max_value_regs
)
2006 if (REG_VALUES (dreg
) == 0)
2008 used_regs
[n_used_regs
++] = dreg
;
2009 REG_VALUES (dreg
) = new_elt_list (REG_VALUES (dreg
), src_elt
);
2013 /* The register should have been invalidated. */
2014 gcc_assert (REG_VALUES (dreg
)->elt
== 0);
2015 REG_VALUES (dreg
)->elt
= src_elt
;
2018 if (src_elt
->locs
== 0 && !PRESERVED_VALUE_P (src_elt
->val_rtx
))
2020 src_elt
->locs
= new_elt_loc_list (src_elt
->locs
, dest
);
2022 else if (MEM_P (dest
) && dest_addr_elt
!= 0
2023 && cselib_record_memory
)
2025 if (src_elt
->locs
== 0 && !PRESERVED_VALUE_P (src_elt
->val_rtx
))
2027 add_mem_for_addr (dest_addr_elt
, src_elt
, dest
);
2031 /* There is no good way to determine how many elements there can be
2032 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2033 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2035 /* Record the effects of any sets in INSN. */
2037 cselib_record_sets (rtx insn
)
2041 struct cselib_set sets
[MAX_SETS
];
2042 rtx body
= PATTERN (insn
);
2045 body
= PATTERN (insn
);
2046 if (GET_CODE (body
) == COND_EXEC
)
2048 cond
= COND_EXEC_TEST (body
);
2049 body
= COND_EXEC_CODE (body
);
2052 /* Find all sets. */
2053 if (GET_CODE (body
) == SET
)
2055 sets
[0].src
= SET_SRC (body
);
2056 sets
[0].dest
= SET_DEST (body
);
2059 else if (GET_CODE (body
) == PARALLEL
)
2061 /* Look through the PARALLEL and record the values being
2062 set, if possible. Also handle any CLOBBERs. */
2063 for (i
= XVECLEN (body
, 0) - 1; i
>= 0; --i
)
2065 rtx x
= XVECEXP (body
, 0, i
);
2067 if (GET_CODE (x
) == SET
)
2069 sets
[n_sets
].src
= SET_SRC (x
);
2070 sets
[n_sets
].dest
= SET_DEST (x
);
2077 && MEM_P (sets
[0].src
)
2078 && !cselib_record_memory
2079 && MEM_READONLY_P (sets
[0].src
))
2081 rtx note
= find_reg_equal_equiv_note (insn
);
2083 if (note
&& CONSTANT_P (XEXP (note
, 0)))
2084 sets
[0].src
= XEXP (note
, 0);
2087 /* Look up the values that are read. Do this before invalidating the
2088 locations that are written. */
2089 for (i
= 0; i
< n_sets
; i
++)
2091 rtx dest
= sets
[i
].dest
;
2093 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2094 the low part after invalidating any knowledge about larger modes. */
2095 if (GET_CODE (sets
[i
].dest
) == STRICT_LOW_PART
)
2096 sets
[i
].dest
= dest
= XEXP (dest
, 0);
2098 /* We don't know how to record anything but REG or MEM. */
2100 || (MEM_P (dest
) && cselib_record_memory
))
2102 rtx src
= sets
[i
].src
;
2104 src
= gen_rtx_IF_THEN_ELSE (GET_MODE (dest
), cond
, src
, dest
);
2105 sets
[i
].src_elt
= cselib_lookup (src
, GET_MODE (dest
), 1);
2108 enum machine_mode address_mode
2109 = targetm
.addr_space
.address_mode (MEM_ADDR_SPACE (dest
));
2111 sets
[i
].dest_addr_elt
= cselib_lookup (XEXP (dest
, 0),
2115 sets
[i
].dest_addr_elt
= 0;
2119 if (cselib_record_sets_hook
)
2120 cselib_record_sets_hook (insn
, sets
, n_sets
);
2122 /* Invalidate all locations written by this insn. Note that the elts we
2123 looked up in the previous loop aren't affected, just some of their
2124 locations may go away. */
2125 note_stores (body
, cselib_invalidate_rtx_note_stores
, NULL
);
2127 /* If this is an asm, look for duplicate sets. This can happen when the
2128 user uses the same value as an output multiple times. This is valid
2129 if the outputs are not actually used thereafter. Treat this case as
2130 if the value isn't actually set. We do this by smashing the destination
2131 to pc_rtx, so that we won't record the value later. */
2132 if (n_sets
>= 2 && asm_noperands (body
) >= 0)
2134 for (i
= 0; i
< n_sets
; i
++)
2136 rtx dest
= sets
[i
].dest
;
2137 if (REG_P (dest
) || MEM_P (dest
))
2140 for (j
= i
+ 1; j
< n_sets
; j
++)
2141 if (rtx_equal_p (dest
, sets
[j
].dest
))
2143 sets
[i
].dest
= pc_rtx
;
2144 sets
[j
].dest
= pc_rtx
;
2150 /* Now enter the equivalences in our tables. */
2151 for (i
= 0; i
< n_sets
; i
++)
2153 rtx dest
= sets
[i
].dest
;
2155 || (MEM_P (dest
) && cselib_record_memory
))
2156 cselib_record_set (dest
, sets
[i
].src_elt
, sets
[i
].dest_addr_elt
);
2160 /* Record the effects of INSN. */
2163 cselib_process_insn (rtx insn
)
2168 cselib_current_insn
= insn
;
2170 /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp. */
2173 && find_reg_note (insn
, REG_SETJMP
, NULL
))
2174 || (NONJUMP_INSN_P (insn
)
2175 && GET_CODE (PATTERN (insn
)) == ASM_OPERANDS
2176 && MEM_VOLATILE_P (PATTERN (insn
))))
2178 cselib_reset_table (next_uid
);
2179 cselib_current_insn
= NULL_RTX
;
2183 if (! INSN_P (insn
))
2185 cselib_current_insn
= NULL_RTX
;
2189 /* If this is a call instruction, forget anything stored in a
2190 call clobbered register, or, if this is not a const call, in
2194 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
2195 if (call_used_regs
[i
]
2196 || (REG_VALUES (i
) && REG_VALUES (i
)->elt
2197 && HARD_REGNO_CALL_PART_CLOBBERED (i
,
2198 GET_MODE (REG_VALUES (i
)->elt
->val_rtx
))))
2199 cselib_invalidate_regno (i
, reg_raw_mode
[i
]);
2201 /* Since it is not clear how cselib is going to be used, be
2202 conservative here and treat looping pure or const functions
2203 as if they were regular functions. */
2204 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn
)
2205 || !(RTL_CONST_OR_PURE_CALL_P (insn
)))
2206 cselib_invalidate_mem (callmem
);
2209 cselib_record_sets (insn
);
2212 /* Clobber any registers which appear in REG_INC notes. We
2213 could keep track of the changes to their values, but it is
2214 unlikely to help. */
2215 for (x
= REG_NOTES (insn
); x
; x
= XEXP (x
, 1))
2216 if (REG_NOTE_KIND (x
) == REG_INC
)
2217 cselib_invalidate_rtx (XEXP (x
, 0));
2220 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2221 after we have processed the insn. */
2223 for (x
= CALL_INSN_FUNCTION_USAGE (insn
); x
; x
= XEXP (x
, 1))
2224 if (GET_CODE (XEXP (x
, 0)) == CLOBBER
)
2225 cselib_invalidate_rtx (XEXP (XEXP (x
, 0), 0));
2227 cselib_current_insn
= NULL_RTX
;
2229 if (n_useless_values
> MAX_USELESS_VALUES
2230 /* remove_useless_values is linear in the hash table size. Avoid
2231 quadratic behavior for very large hashtables with very few
2232 useless elements. */
2233 && ((unsigned int)n_useless_values
2234 > (cselib_hash_table
->n_elements
2235 - cselib_hash_table
->n_deleted
2236 - n_debug_values
) / 4))
2237 remove_useless_values ();
2240 /* Initialize cselib for one pass. The caller must also call
2241 init_alias_analysis. */
2244 cselib_init (int record_what
)
2246 elt_list_pool
= create_alloc_pool ("elt_list",
2247 sizeof (struct elt_list
), 10);
2248 elt_loc_list_pool
= create_alloc_pool ("elt_loc_list",
2249 sizeof (struct elt_loc_list
), 10);
2250 cselib_val_pool
= create_alloc_pool ("cselib_val_list",
2251 sizeof (cselib_val
), 10);
2252 value_pool
= create_alloc_pool ("value", RTX_CODE_SIZE (VALUE
), 100);
2253 cselib_record_memory
= record_what
& CSELIB_RECORD_MEMORY
;
2254 cselib_preserve_constants
= record_what
& CSELIB_PRESERVE_CONSTANTS
;
2256 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2257 see canon_true_dependence. This is only created once. */
2259 callmem
= gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
));
2261 cselib_nregs
= max_reg_num ();
2263 /* We preserve reg_values to allow expensive clearing of the whole thing.
2264 Reallocate it however if it happens to be too large. */
2265 if (!reg_values
|| reg_values_size
< cselib_nregs
2266 || (reg_values_size
> 10 && reg_values_size
> cselib_nregs
* 4))
2270 /* Some space for newly emit instructions so we don't end up
2271 reallocating in between passes. */
2272 reg_values_size
= cselib_nregs
+ (63 + cselib_nregs
) / 16;
2273 reg_values
= XCNEWVEC (struct elt_list
*, reg_values_size
);
2275 used_regs
= XNEWVEC (unsigned int, cselib_nregs
);
2277 cselib_hash_table
= htab_create (31, get_value_hash
,
2278 entry_and_rtx_equal_p
, NULL
);
2282 /* Called when the current user is done with cselib. */
2285 cselib_finish (void)
2287 cselib_discard_hook
= NULL
;
2288 cselib_preserve_constants
= false;
2289 cfa_base_preserved_val
= NULL
;
2290 cfa_base_preserved_regno
= INVALID_REGNUM
;
2291 free_alloc_pool (elt_list_pool
);
2292 free_alloc_pool (elt_loc_list_pool
);
2293 free_alloc_pool (cselib_val_pool
);
2294 free_alloc_pool (value_pool
);
2295 cselib_clear_table ();
2296 htab_delete (cselib_hash_table
);
2299 cselib_hash_table
= 0;
2300 n_useless_values
= 0;
2301 n_useless_debug_values
= 0;
2306 /* Dump the cselib_val *X to FILE *info. */
2309 dump_cselib_val (void **x
, void *info
)
2311 cselib_val
*v
= (cselib_val
*)*x
;
2312 FILE *out
= (FILE *)info
;
2313 bool need_lf
= true;
2315 print_inline_rtx (out
, v
->val_rtx
, 0);
2319 struct elt_loc_list
*l
= v
->locs
;
2325 fputs (" locs:", out
);
2328 fprintf (out
, "\n from insn %i ",
2329 INSN_UID (l
->setting_insn
));
2330 print_inline_rtx (out
, l
->loc
, 4);
2332 while ((l
= l
->next
));
2337 fputs (" no locs", out
);
2343 struct elt_list
*e
= v
->addr_list
;
2349 fputs (" addr list:", out
);
2353 print_inline_rtx (out
, e
->elt
->val_rtx
, 2);
2355 while ((e
= e
->next
));
2360 fputs (" no addrs", out
);
2364 if (v
->next_containing_mem
== &dummy_val
)
2365 fputs (" last mem\n", out
);
2366 else if (v
->next_containing_mem
)
2368 fputs (" next mem ", out
);
2369 print_inline_rtx (out
, v
->next_containing_mem
->val_rtx
, 2);
2378 /* Dump to OUT everything in the CSELIB table. */
2381 dump_cselib_table (FILE *out
)
2383 fprintf (out
, "cselib hash table:\n");
2384 htab_traverse (cselib_hash_table
, dump_cselib_val
, out
);
2385 if (first_containing_mem
!= &dummy_val
)
2387 fputs ("first mem ", out
);
2388 print_inline_rtx (out
, first_containing_mem
->val_rtx
, 2);
2391 fprintf (out
, "next uid %i\n", next_uid
);
2394 #include "gt-cselib.h"