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"
41 #include "tree-pass.h"
44 #include "alloc-pool.h"
48 static bool cselib_record_memory
;
49 static bool cselib_preserve_constants
;
50 static int entry_and_rtx_equal_p (const void *, const void *);
51 static hashval_t
get_value_hash (const void *);
52 static struct elt_list
*new_elt_list (struct elt_list
*, cselib_val
*);
53 static struct elt_loc_list
*new_elt_loc_list (struct elt_loc_list
*, rtx
);
54 static void unchain_one_value (cselib_val
*);
55 static void unchain_one_elt_list (struct elt_list
**);
56 static void unchain_one_elt_loc_list (struct elt_loc_list
**);
57 static int discard_useless_locs (void **, void *);
58 static int discard_useless_values (void **, void *);
59 static void remove_useless_values (void);
60 static unsigned int cselib_hash_rtx (rtx
, int);
61 static cselib_val
*new_cselib_val (unsigned int, enum machine_mode
, rtx
);
62 static void add_mem_for_addr (cselib_val
*, cselib_val
*, rtx
);
63 static cselib_val
*cselib_lookup_mem (rtx
, int);
64 static void cselib_invalidate_regno (unsigned int, enum machine_mode
);
65 static void cselib_invalidate_mem (rtx
);
66 static void cselib_record_set (rtx
, cselib_val
*, cselib_val
*);
67 static void cselib_record_sets (rtx
);
69 struct expand_value_data
72 cselib_expand_callback callback
;
77 static rtx
cselib_expand_value_rtx_1 (rtx
, struct expand_value_data
*, int);
79 /* There are three ways in which cselib can look up an rtx:
80 - for a REG, the reg_values table (which is indexed by regno) is used
81 - for a MEM, we recursively look up its address and then follow the
82 addr_list of that value
83 - for everything else, we compute a hash value and go through the hash
84 table. Since different rtx's can still have the same hash value,
85 this involves walking the table entries for a given value and comparing
86 the locations of the entries with the rtx we are looking up. */
88 /* A table that enables us to look up elts by their value. */
89 static htab_t cselib_hash_table
;
91 /* This is a global so we don't have to pass this through every function.
92 It is used in new_elt_loc_list to set SETTING_INSN. */
93 static rtx cselib_current_insn
;
95 /* The unique id that the next create value will take. */
96 static unsigned int next_uid
;
98 /* The number of registers we had when the varrays were last resized. */
99 static unsigned int cselib_nregs
;
101 /* Count values without known locations, or with only locations that
102 wouldn't have been known except for debug insns. Whenever this
103 grows too big, we remove these useless values from the table.
105 Counting values with only debug values is a bit tricky. We don't
106 want to increment n_useless_values when we create a value for a
107 debug insn, for this would get n_useless_values out of sync, but we
108 want increment it if all locs in the list that were ever referenced
109 in nondebug insns are removed from the list.
111 In the general case, once we do that, we'd have to stop accepting
112 nondebug expressions in the loc list, to avoid having two values
113 equivalent that, without debug insns, would have been made into
114 separate values. However, because debug insns never introduce
115 equivalences themselves (no assignments), the only means for
116 growing loc lists is through nondebug assignments. If the locs
117 also happen to be referenced in debug insns, it will work just fine.
119 A consequence of this is that there's at most one debug-only loc in
120 each loc list. If we keep it in the first entry, testing whether
121 we have a debug-only loc list takes O(1).
123 Furthermore, since any additional entry in a loc list containing a
124 debug loc would have to come from an assignment (nondebug) that
125 references both the initial debug loc and the newly-equivalent loc,
126 the initial debug loc would be promoted to a nondebug loc, and the
127 loc list would not contain debug locs any more.
129 So the only case we have to be careful with in order to keep
130 n_useless_values in sync between debug and nondebug compilations is
131 to avoid incrementing n_useless_values when removing the single loc
132 from a value that turns out to not appear outside debug values. We
133 increment n_useless_debug_values instead, and leave such values
134 alone until, for other reasons, we garbage-collect useless
136 static int n_useless_values
;
137 static int n_useless_debug_values
;
139 /* Count values whose locs have been taken exclusively from debug
140 insns for the entire life of the value. */
141 static int n_debug_values
;
143 /* Number of useless values before we remove them from the hash table. */
144 #define MAX_USELESS_VALUES 32
146 /* This table maps from register number to values. It does not
147 contain pointers to cselib_val structures, but rather elt_lists.
148 The purpose is to be able to refer to the same register in
149 different modes. The first element of the list defines the mode in
150 which the register was set; if the mode is unknown or the value is
151 no longer valid in that mode, ELT will be NULL for the first
153 static struct elt_list
**reg_values
;
154 static unsigned int reg_values_size
;
155 #define REG_VALUES(i) reg_values[i]
157 /* The largest number of hard regs used by any entry added to the
158 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
159 static unsigned int max_value_regs
;
161 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
162 in cselib_clear_table() for fast emptying. */
163 static unsigned int *used_regs
;
164 static unsigned int n_used_regs
;
166 /* We pass this to cselib_invalidate_mem to invalidate all of
167 memory for a non-const call instruction. */
168 static GTY(()) rtx callmem
;
170 /* Set by discard_useless_locs if it deleted the last location of any
172 static int values_became_useless
;
174 /* Used as stop element of the containing_mem list so we can check
175 presence in the list by checking the next pointer. */
176 static cselib_val dummy_val
;
178 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
179 that is constant through the whole function and should never be
181 static cselib_val
*cfa_base_preserved_val
;
182 static unsigned int cfa_base_preserved_regno
;
184 /* Used to list all values that contain memory reference.
185 May or may not contain the useless values - the list is compacted
186 each time memory is invalidated. */
187 static cselib_val
*first_containing_mem
= &dummy_val
;
188 static alloc_pool elt_loc_list_pool
, elt_list_pool
, cselib_val_pool
, value_pool
;
190 /* If nonnull, cselib will call this function before freeing useless
191 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
192 void (*cselib_discard_hook
) (cselib_val
*);
194 /* If nonnull, cselib will call this function before recording sets or
195 even clobbering outputs of INSN. All the recorded sets will be
196 represented in the array sets[n_sets]. new_val_min can be used to
197 tell whether values present in sets are introduced by this
199 void (*cselib_record_sets_hook
) (rtx insn
, struct cselib_set
*sets
,
202 #define PRESERVED_VALUE_P(RTX) \
203 (RTL_FLAG_CHECK1("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
207 /* Allocate a struct elt_list and fill in its two elements with the
210 static inline struct elt_list
*
211 new_elt_list (struct elt_list
*next
, cselib_val
*elt
)
214 el
= (struct elt_list
*) pool_alloc (elt_list_pool
);
220 /* Allocate a struct elt_loc_list and fill in its two elements with the
223 static inline struct elt_loc_list
*
224 new_elt_loc_list (struct elt_loc_list
*next
, rtx loc
)
226 struct elt_loc_list
*el
;
227 el
= (struct elt_loc_list
*) pool_alloc (elt_loc_list_pool
);
230 el
->setting_insn
= cselib_current_insn
;
231 gcc_assert (!next
|| !next
->setting_insn
232 || !DEBUG_INSN_P (next
->setting_insn
));
234 /* If we're creating the first loc in a debug insn context, we've
235 just created a debug value. Count it. */
236 if (!next
&& cselib_current_insn
&& DEBUG_INSN_P (cselib_current_insn
))
242 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
243 originating from a debug insn, maintaining the debug values
247 promote_debug_loc (struct elt_loc_list
*l
)
249 if (l
->setting_insn
&& DEBUG_INSN_P (l
->setting_insn
)
250 && (!cselib_current_insn
|| !DEBUG_INSN_P (cselib_current_insn
)))
253 l
->setting_insn
= cselib_current_insn
;
254 gcc_assert (!l
->next
);
258 /* The elt_list at *PL is no longer needed. Unchain it and free its
262 unchain_one_elt_list (struct elt_list
**pl
)
264 struct elt_list
*l
= *pl
;
267 pool_free (elt_list_pool
, l
);
270 /* Likewise for elt_loc_lists. */
273 unchain_one_elt_loc_list (struct elt_loc_list
**pl
)
275 struct elt_loc_list
*l
= *pl
;
278 pool_free (elt_loc_list_pool
, l
);
281 /* Likewise for cselib_vals. This also frees the addr_list associated with
285 unchain_one_value (cselib_val
*v
)
288 unchain_one_elt_list (&v
->addr_list
);
290 pool_free (cselib_val_pool
, v
);
293 /* Remove all entries from the hash table. Also used during
297 cselib_clear_table (void)
299 cselib_reset_table (1);
302 /* Remove from hash table all VALUEs except constants. */
305 preserve_only_constants (void **x
, void *info ATTRIBUTE_UNUSED
)
307 cselib_val
*v
= (cselib_val
*)*x
;
310 && v
->locs
->next
== NULL
)
312 if (CONSTANT_P (v
->locs
->loc
)
313 && (GET_CODE (v
->locs
->loc
) != CONST
314 || !references_value_p (v
->locs
->loc
, 0)))
316 if (cfa_base_preserved_val
)
318 if (v
== cfa_base_preserved_val
)
320 if (GET_CODE (v
->locs
->loc
) == PLUS
321 && CONST_INT_P (XEXP (v
->locs
->loc
, 1))
322 && XEXP (v
->locs
->loc
, 0) == cfa_base_preserved_val
->val_rtx
)
327 htab_clear_slot (cselib_hash_table
, x
);
331 /* Remove all entries from the hash table, arranging for the next
332 value to be numbered NUM. */
335 cselib_reset_table (unsigned int num
)
341 if (cfa_base_preserved_val
)
343 unsigned int regno
= cfa_base_preserved_regno
;
344 unsigned int new_used_regs
= 0;
345 for (i
= 0; i
< n_used_regs
; i
++)
346 if (used_regs
[i
] == regno
)
352 REG_VALUES (used_regs
[i
]) = 0;
353 gcc_assert (new_used_regs
== 1);
354 n_used_regs
= new_used_regs
;
355 used_regs
[0] = regno
;
357 = hard_regno_nregs
[regno
][GET_MODE (cfa_base_preserved_val
->locs
->loc
)];
361 for (i
= 0; i
< n_used_regs
; i
++)
362 REG_VALUES (used_regs
[i
]) = 0;
366 if (cselib_preserve_constants
)
367 htab_traverse (cselib_hash_table
, preserve_only_constants
, NULL
);
369 htab_empty (cselib_hash_table
);
371 n_useless_values
= 0;
372 n_useless_debug_values
= 0;
377 first_containing_mem
= &dummy_val
;
380 /* Return the number of the next value that will be generated. */
383 cselib_get_next_uid (void)
388 /* The equality test for our hash table. The first argument ENTRY is a table
389 element (i.e. a cselib_val), while the second arg X is an rtx. We know
390 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
391 CONST of an appropriate mode. */
394 entry_and_rtx_equal_p (const void *entry
, const void *x_arg
)
396 struct elt_loc_list
*l
;
397 const cselib_val
*const v
= (const cselib_val
*) entry
;
398 rtx x
= CONST_CAST_RTX ((const_rtx
)x_arg
);
399 enum machine_mode mode
= GET_MODE (x
);
401 gcc_assert (!CONST_INT_P (x
) && GET_CODE (x
) != CONST_FIXED
402 && (mode
!= VOIDmode
|| GET_CODE (x
) != CONST_DOUBLE
));
404 if (mode
!= GET_MODE (v
->val_rtx
))
407 /* Unwrap X if necessary. */
408 if (GET_CODE (x
) == CONST
409 && (CONST_INT_P (XEXP (x
, 0))
410 || GET_CODE (XEXP (x
, 0)) == CONST_FIXED
411 || GET_CODE (XEXP (x
, 0)) == CONST_DOUBLE
))
414 /* We don't guarantee that distinct rtx's have different hash values,
415 so we need to do a comparison. */
416 for (l
= v
->locs
; l
; l
= l
->next
)
417 if (rtx_equal_for_cselib_p (l
->loc
, x
))
419 promote_debug_loc (l
);
426 /* The hash function for our hash table. The value is always computed with
427 cselib_hash_rtx when adding an element; this function just extracts the
428 hash value from a cselib_val structure. */
431 get_value_hash (const void *entry
)
433 const cselib_val
*const v
= (const cselib_val
*) entry
;
437 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
438 only return true for values which point to a cselib_val whose value
439 element has been set to zero, which implies the cselib_val will be
443 references_value_p (const_rtx x
, int only_useless
)
445 const enum rtx_code code
= GET_CODE (x
);
446 const char *fmt
= GET_RTX_FORMAT (code
);
449 if (GET_CODE (x
) == VALUE
450 && (! only_useless
|| CSELIB_VAL_PTR (x
)->locs
== 0))
453 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
455 if (fmt
[i
] == 'e' && references_value_p (XEXP (x
, i
), only_useless
))
457 else if (fmt
[i
] == 'E')
458 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
459 if (references_value_p (XVECEXP (x
, i
, j
), only_useless
))
466 /* For all locations found in X, delete locations that reference useless
467 values (i.e. values without any location). Called through
471 discard_useless_locs (void **x
, void *info ATTRIBUTE_UNUSED
)
473 cselib_val
*v
= (cselib_val
*)*x
;
474 struct elt_loc_list
**p
= &v
->locs
;
475 bool had_locs
= v
->locs
!= NULL
;
476 rtx setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
480 if (references_value_p ((*p
)->loc
, 1))
481 unchain_one_elt_loc_list (p
);
486 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
488 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
489 n_useless_debug_values
++;
492 values_became_useless
= 1;
497 /* If X is a value with no locations, remove it from the hashtable. */
500 discard_useless_values (void **x
, void *info ATTRIBUTE_UNUSED
)
502 cselib_val
*v
= (cselib_val
*)*x
;
504 if (v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
506 if (cselib_discard_hook
)
507 cselib_discard_hook (v
);
509 CSELIB_VAL_PTR (v
->val_rtx
) = NULL
;
510 htab_clear_slot (cselib_hash_table
, x
);
511 unchain_one_value (v
);
518 /* Clean out useless values (i.e. those which no longer have locations
519 associated with them) from the hash table. */
522 remove_useless_values (void)
526 /* First pass: eliminate locations that reference the value. That in
527 turn can make more values useless. */
530 values_became_useless
= 0;
531 htab_traverse (cselib_hash_table
, discard_useless_locs
, 0);
533 while (values_became_useless
);
535 /* Second pass: actually remove the values. */
537 p
= &first_containing_mem
;
538 for (v
= *p
; v
!= &dummy_val
; v
= v
->next_containing_mem
)
542 p
= &(*p
)->next_containing_mem
;
546 n_useless_values
+= n_useless_debug_values
;
547 n_debug_values
-= n_useless_debug_values
;
548 n_useless_debug_values
= 0;
550 htab_traverse (cselib_hash_table
, discard_useless_values
, 0);
552 gcc_assert (!n_useless_values
);
555 /* Arrange for a value to not be removed from the hash table even if
556 it becomes useless. */
559 cselib_preserve_value (cselib_val
*v
)
561 PRESERVED_VALUE_P (v
->val_rtx
) = 1;
564 /* Test whether a value is preserved. */
567 cselib_preserved_value_p (cselib_val
*v
)
569 return PRESERVED_VALUE_P (v
->val_rtx
);
572 /* Arrange for a REG value to be assumed constant through the whole function,
573 never invalidated and preserved across cselib_reset_table calls. */
576 cselib_preserve_cfa_base_value (cselib_val
*v
, unsigned int regno
)
578 if (cselib_preserve_constants
580 && REG_P (v
->locs
->loc
))
582 cfa_base_preserved_val
= v
;
583 cfa_base_preserved_regno
= regno
;
587 /* Clean all non-constant expressions in the hash table, but retain
591 cselib_preserve_only_values (void)
595 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
596 cselib_invalidate_regno (i
, reg_raw_mode
[i
]);
598 cselib_invalidate_mem (callmem
);
600 remove_useless_values ();
602 gcc_assert (first_containing_mem
== &dummy_val
);
605 /* Return the mode in which a register was last set. If X is not a
606 register, return its mode. If the mode in which the register was
607 set is not known, or the value was already clobbered, return
611 cselib_reg_set_mode (const_rtx x
)
616 if (REG_VALUES (REGNO (x
)) == NULL
617 || REG_VALUES (REGNO (x
))->elt
== NULL
)
620 return GET_MODE (REG_VALUES (REGNO (x
))->elt
->val_rtx
);
623 /* Return nonzero if we can prove that X and Y contain the same value, taking
624 our gathered information into account. */
627 rtx_equal_for_cselib_p (rtx x
, rtx y
)
633 if (REG_P (x
) || MEM_P (x
))
635 cselib_val
*e
= cselib_lookup (x
, GET_MODE (x
), 0);
641 if (REG_P (y
) || MEM_P (y
))
643 cselib_val
*e
= cselib_lookup (y
, GET_MODE (y
), 0);
652 if (GET_CODE (x
) == VALUE
&& GET_CODE (y
) == VALUE
)
653 return CSELIB_VAL_PTR (x
) == CSELIB_VAL_PTR (y
);
655 if (GET_CODE (x
) == VALUE
)
657 cselib_val
*e
= CSELIB_VAL_PTR (x
);
658 struct elt_loc_list
*l
;
660 for (l
= e
->locs
; l
; l
= l
->next
)
664 /* Avoid infinite recursion. */
665 if (REG_P (t
) || MEM_P (t
))
667 else if (rtx_equal_for_cselib_p (t
, y
))
674 if (GET_CODE (y
) == VALUE
)
676 cselib_val
*e
= CSELIB_VAL_PTR (y
);
677 struct elt_loc_list
*l
;
679 for (l
= e
->locs
; l
; l
= l
->next
)
683 if (REG_P (t
) || MEM_P (t
))
685 else if (rtx_equal_for_cselib_p (x
, t
))
692 if (GET_CODE (x
) != GET_CODE (y
) || GET_MODE (x
) != GET_MODE (y
))
695 /* These won't be handled correctly by the code below. */
696 switch (GET_CODE (x
))
703 case DEBUG_IMPLICIT_PTR
:
704 return DEBUG_IMPLICIT_PTR_DECL (x
)
705 == DEBUG_IMPLICIT_PTR_DECL (y
);
708 return XEXP (x
, 0) == XEXP (y
, 0);
715 fmt
= GET_RTX_FORMAT (code
);
717 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
724 if (XWINT (x
, i
) != XWINT (y
, i
))
730 if (XINT (x
, i
) != XINT (y
, i
))
736 /* Two vectors must have the same length. */
737 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
740 /* And the corresponding elements must match. */
741 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
742 if (! rtx_equal_for_cselib_p (XVECEXP (x
, i
, j
),
749 && targetm
.commutative_p (x
, UNKNOWN
)
750 && rtx_equal_for_cselib_p (XEXP (x
, 1), XEXP (y
, 0))
751 && rtx_equal_for_cselib_p (XEXP (x
, 0), XEXP (y
, 1)))
753 if (! rtx_equal_for_cselib_p (XEXP (x
, i
), XEXP (y
, i
)))
759 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
764 /* These are just backpointers, so they don't matter. */
771 /* It is believed that rtx's at this level will never
772 contain anything but integers and other rtx's,
773 except for within LABEL_REFs and SYMBOL_REFs. */
781 /* We need to pass down the mode of constants through the hash table
782 functions. For that purpose, wrap them in a CONST of the appropriate
785 wrap_constant (enum machine_mode mode
, rtx x
)
787 if (!CONST_INT_P (x
) && GET_CODE (x
) != CONST_FIXED
788 && (GET_CODE (x
) != CONST_DOUBLE
|| GET_MODE (x
) != VOIDmode
))
790 gcc_assert (mode
!= VOIDmode
);
791 return gen_rtx_CONST (mode
, x
);
794 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
795 For registers and memory locations, we look up their cselib_val structure
796 and return its VALUE element.
797 Possible reasons for return 0 are: the object is volatile, or we couldn't
798 find a register or memory location in the table and CREATE is zero. If
799 CREATE is nonzero, table elts are created for regs and mem.
800 N.B. this hash function returns the same hash value for RTXes that
801 differ only in the order of operands, thus it is suitable for comparisons
802 that take commutativity into account.
803 If we wanted to also support associative rules, we'd have to use a different
804 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
805 We used to have a MODE argument for hashing for CONST_INTs, but that
806 didn't make sense, since it caused spurious hash differences between
807 (set (reg:SI 1) (const_int))
808 (plus:SI (reg:SI 2) (reg:SI 1))
810 (plus:SI (reg:SI 2) (const_int))
811 If the mode is important in any context, it must be checked specifically
812 in a comparison anyway, since relying on hash differences is unsafe. */
815 cselib_hash_rtx (rtx x
, int create
)
821 unsigned int hash
= 0;
824 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
830 e
= cselib_lookup (x
, GET_MODE (x
), create
);
837 hash
+= ((unsigned) DEBUG_EXPR
<< 7)
838 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x
));
839 return hash
? hash
: (unsigned int) DEBUG_EXPR
;
841 case DEBUG_IMPLICIT_PTR
:
842 hash
+= ((unsigned) DEBUG_IMPLICIT_PTR
<< 7)
843 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x
));
844 return hash
? hash
: (unsigned int) DEBUG_IMPLICIT_PTR
;
847 hash
+= ((unsigned) CONST_INT
<< 7) + INTVAL (x
);
848 return hash
? hash
: (unsigned int) CONST_INT
;
851 /* This is like the general case, except that it only counts
852 the integers representing the constant. */
853 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
854 if (GET_MODE (x
) != VOIDmode
)
855 hash
+= real_hash (CONST_DOUBLE_REAL_VALUE (x
));
857 hash
+= ((unsigned) CONST_DOUBLE_LOW (x
)
858 + (unsigned) CONST_DOUBLE_HIGH (x
));
859 return hash
? hash
: (unsigned int) CONST_DOUBLE
;
862 hash
+= (unsigned int) code
+ (unsigned int) GET_MODE (x
);
863 hash
+= fixed_hash (CONST_FIXED_VALUE (x
));
864 return hash
? hash
: (unsigned int) CONST_FIXED
;
871 units
= CONST_VECTOR_NUNITS (x
);
873 for (i
= 0; i
< units
; ++i
)
875 elt
= CONST_VECTOR_ELT (x
, i
);
876 hash
+= cselib_hash_rtx (elt
, 0);
882 /* Assume there is only one rtx object for any given label. */
884 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
885 differences and differences between each stage's debugging dumps. */
886 hash
+= (((unsigned int) LABEL_REF
<< 7)
887 + CODE_LABEL_NUMBER (XEXP (x
, 0)));
888 return hash
? hash
: (unsigned int) LABEL_REF
;
892 /* Don't hash on the symbol's address to avoid bootstrap differences.
893 Different hash values may cause expressions to be recorded in
894 different orders and thus different registers to be used in the
895 final assembler. This also avoids differences in the dump files
896 between various stages. */
898 const unsigned char *p
= (const unsigned char *) XSTR (x
, 0);
901 h
+= (h
<< 7) + *p
++; /* ??? revisit */
903 hash
+= ((unsigned int) SYMBOL_REF
<< 7) + h
;
904 return hash
? hash
: (unsigned int) SYMBOL_REF
;
916 case UNSPEC_VOLATILE
:
920 if (MEM_VOLATILE_P (x
))
929 i
= GET_RTX_LENGTH (code
) - 1;
930 fmt
= GET_RTX_FORMAT (code
);
937 rtx tem
= XEXP (x
, i
);
938 unsigned int tem_hash
= cselib_hash_rtx (tem
, create
);
947 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
949 unsigned int tem_hash
950 = cselib_hash_rtx (XVECEXP (x
, i
, j
), create
);
961 const unsigned char *p
= (const unsigned char *) XSTR (x
, i
);
983 return hash
? hash
: 1 + (unsigned int) GET_CODE (x
);
986 /* Create a new value structure for VALUE and initialize it. The mode of the
989 static inline cselib_val
*
990 new_cselib_val (unsigned int hash
, enum machine_mode mode
, rtx x
)
992 cselib_val
*e
= (cselib_val
*) pool_alloc (cselib_val_pool
);
995 gcc_assert (next_uid
);
999 /* We use an alloc pool to allocate this RTL construct because it
1000 accounts for about 8% of the overall memory usage. We know
1001 precisely when we can have VALUE RTXen (when cselib is active)
1002 so we don't need to put them in garbage collected memory.
1003 ??? Why should a VALUE be an RTX in the first place? */
1004 e
->val_rtx
= (rtx
) pool_alloc (value_pool
);
1005 memset (e
->val_rtx
, 0, RTX_HDR_SIZE
);
1006 PUT_CODE (e
->val_rtx
, VALUE
);
1007 PUT_MODE (e
->val_rtx
, mode
);
1008 CSELIB_VAL_PTR (e
->val_rtx
) = e
;
1011 e
->next_containing_mem
= 0;
1013 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1015 fprintf (dump_file
, "cselib value %u:%u ", e
->uid
, hash
);
1016 if (flag_dump_noaddr
|| flag_dump_unnumbered
)
1017 fputs ("# ", dump_file
);
1019 fprintf (dump_file
, "%p ", (void*)e
);
1020 print_rtl_single (dump_file
, x
);
1021 fputc ('\n', dump_file
);
1027 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1028 contains the data at this address. X is a MEM that represents the
1029 value. Update the two value structures to represent this situation. */
1032 add_mem_for_addr (cselib_val
*addr_elt
, cselib_val
*mem_elt
, rtx x
)
1034 struct elt_loc_list
*l
;
1036 /* Avoid duplicates. */
1037 for (l
= mem_elt
->locs
; l
; l
= l
->next
)
1039 && CSELIB_VAL_PTR (XEXP (l
->loc
, 0)) == addr_elt
)
1041 promote_debug_loc (l
);
1045 addr_elt
->addr_list
= new_elt_list (addr_elt
->addr_list
, mem_elt
);
1047 = new_elt_loc_list (mem_elt
->locs
,
1048 replace_equiv_address_nv (x
, addr_elt
->val_rtx
));
1049 if (mem_elt
->next_containing_mem
== NULL
)
1051 mem_elt
->next_containing_mem
= first_containing_mem
;
1052 first_containing_mem
= mem_elt
;
1056 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1057 If CREATE, make a new one if we haven't seen it before. */
1060 cselib_lookup_mem (rtx x
, int create
)
1062 enum machine_mode mode
= GET_MODE (x
);
1065 cselib_val
*mem_elt
;
1068 if (MEM_VOLATILE_P (x
) || mode
== BLKmode
1069 || !cselib_record_memory
1070 || (FLOAT_MODE_P (mode
) && flag_float_store
))
1073 /* Look up the value for the address. */
1074 addr
= cselib_lookup (XEXP (x
, 0), mode
, create
);
1078 /* Find a value that describes a value of our mode at that address. */
1079 for (l
= addr
->addr_list
; l
; l
= l
->next
)
1080 if (GET_MODE (l
->elt
->val_rtx
) == mode
)
1082 promote_debug_loc (l
->elt
->locs
);
1089 mem_elt
= new_cselib_val (next_uid
, mode
, x
);
1090 add_mem_for_addr (addr
, mem_elt
, x
);
1091 slot
= htab_find_slot_with_hash (cselib_hash_table
, wrap_constant (mode
, x
),
1092 mem_elt
->hash
, INSERT
);
1097 /* Search thru the possible substitutions in P. We prefer a non reg
1098 substitution because this allows us to expand the tree further. If
1099 we find, just a reg, take the lowest regno. There may be several
1100 non-reg results, we just take the first one because they will all
1101 expand to the same place. */
1104 expand_loc (struct elt_loc_list
*p
, struct expand_value_data
*evd
,
1107 rtx reg_result
= NULL
;
1108 unsigned int regno
= UINT_MAX
;
1109 struct elt_loc_list
*p_in
= p
;
1111 for (; p
; p
= p
-> next
)
1113 /* Avoid infinite recursion trying to expand a reg into a
1115 if ((REG_P (p
->loc
))
1116 && (REGNO (p
->loc
) < regno
)
1117 && !bitmap_bit_p (evd
->regs_active
, REGNO (p
->loc
)))
1119 reg_result
= p
->loc
;
1120 regno
= REGNO (p
->loc
);
1122 /* Avoid infinite recursion and do not try to expand the
1124 else if (GET_CODE (p
->loc
) == VALUE
1125 && CSELIB_VAL_PTR (p
->loc
)->locs
== p_in
)
1127 else if (!REG_P (p
->loc
))
1130 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1132 print_inline_rtx (dump_file
, p
->loc
, 0);
1133 fprintf (dump_file
, "\n");
1135 if (GET_CODE (p
->loc
) == LO_SUM
1136 && GET_CODE (XEXP (p
->loc
, 1)) == SYMBOL_REF
1138 && (note
= find_reg_note (p
->setting_insn
, REG_EQUAL
, NULL_RTX
))
1139 && XEXP (note
, 0) == XEXP (p
->loc
, 1))
1140 return XEXP (p
->loc
, 1);
1141 result
= cselib_expand_value_rtx_1 (p
->loc
, evd
, max_depth
- 1);
1148 if (regno
!= UINT_MAX
)
1151 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1152 fprintf (dump_file
, "r%d\n", regno
);
1154 result
= cselib_expand_value_rtx_1 (reg_result
, evd
, max_depth
- 1);
1159 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1163 print_inline_rtx (dump_file
, reg_result
, 0);
1164 fprintf (dump_file
, "\n");
1167 fprintf (dump_file
, "NULL\n");
1173 /* Forward substitute and expand an expression out to its roots.
1174 This is the opposite of common subexpression. Because local value
1175 numbering is such a weak optimization, the expanded expression is
1176 pretty much unique (not from a pointer equals point of view but
1177 from a tree shape point of view.
1179 This function returns NULL if the expansion fails. The expansion
1180 will fail if there is no value number for one of the operands or if
1181 one of the operands has been overwritten between the current insn
1182 and the beginning of the basic block. For instance x has no
1188 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1189 It is clear on return. */
1192 cselib_expand_value_rtx (rtx orig
, bitmap regs_active
, int max_depth
)
1194 struct expand_value_data evd
;
1196 evd
.regs_active
= regs_active
;
1197 evd
.callback
= NULL
;
1198 evd
.callback_arg
= NULL
;
1201 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
);
1204 /* Same as cselib_expand_value_rtx, but using a callback to try to
1205 resolve some expressions. The CB function should return ORIG if it
1206 can't or does not want to deal with a certain RTX. Any other
1207 return value, including NULL, will be used as the expansion for
1208 VALUE, without any further changes. */
1211 cselib_expand_value_rtx_cb (rtx orig
, bitmap regs_active
, int max_depth
,
1212 cselib_expand_callback cb
, void *data
)
1214 struct expand_value_data evd
;
1216 evd
.regs_active
= regs_active
;
1218 evd
.callback_arg
= data
;
1221 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
);
1224 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1225 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1226 would return NULL or non-NULL, without allocating new rtx. */
1229 cselib_dummy_expand_value_rtx_cb (rtx orig
, bitmap regs_active
, int max_depth
,
1230 cselib_expand_callback cb
, void *data
)
1232 struct expand_value_data evd
;
1234 evd
.regs_active
= regs_active
;
1236 evd
.callback_arg
= data
;
1239 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
) != NULL
;
1242 /* Internal implementation of cselib_expand_value_rtx and
1243 cselib_expand_value_rtx_cb. */
1246 cselib_expand_value_rtx_1 (rtx orig
, struct expand_value_data
*evd
,
1252 const char *format_ptr
;
1253 enum machine_mode mode
;
1255 code
= GET_CODE (orig
);
1257 /* For the context of dse, if we end up expand into a huge tree, we
1258 will not have a useful address, so we might as well just give up
1267 struct elt_list
*l
= REG_VALUES (REGNO (orig
));
1269 if (l
&& l
->elt
== NULL
)
1271 for (; l
; l
= l
->next
)
1272 if (GET_MODE (l
->elt
->val_rtx
) == GET_MODE (orig
))
1275 int regno
= REGNO (orig
);
1277 /* The only thing that we are not willing to do (this
1278 is requirement of dse and if others potential uses
1279 need this function we should add a parm to control
1280 it) is that we will not substitute the
1281 STACK_POINTER_REGNUM, FRAME_POINTER or the
1284 These expansions confuses the code that notices that
1285 stores into the frame go dead at the end of the
1286 function and that the frame is not effected by calls
1287 to subroutines. If you allow the
1288 STACK_POINTER_REGNUM substitution, then dse will
1289 think that parameter pushing also goes dead which is
1290 wrong. If you allow the FRAME_POINTER or the
1291 HARD_FRAME_POINTER then you lose the opportunity to
1292 make the frame assumptions. */
1293 if (regno
== STACK_POINTER_REGNUM
1294 || regno
== FRAME_POINTER_REGNUM
1295 || regno
== HARD_FRAME_POINTER_REGNUM
)
1298 bitmap_set_bit (evd
->regs_active
, regno
);
1300 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1301 fprintf (dump_file
, "expanding: r%d into: ", regno
);
1303 result
= expand_loc (l
->elt
->locs
, evd
, max_depth
);
1304 bitmap_clear_bit (evd
->regs_active
, regno
);
1321 /* SCRATCH must be shared because they represent distinct values. */
1324 if (REG_P (XEXP (orig
, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig
, 0))))
1329 if (shared_const_p (orig
))
1339 subreg
= evd
->callback (orig
, evd
->regs_active
, max_depth
,
1345 subreg
= cselib_expand_value_rtx_1 (SUBREG_REG (orig
), evd
,
1349 scopy
= simplify_gen_subreg (GET_MODE (orig
), subreg
,
1350 GET_MODE (SUBREG_REG (orig
)),
1351 SUBREG_BYTE (orig
));
1353 || (GET_CODE (scopy
) == SUBREG
1354 && !REG_P (SUBREG_REG (scopy
))
1355 && !MEM_P (SUBREG_REG (scopy
))))
1365 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1367 fputs ("\nexpanding ", dump_file
);
1368 print_rtl_single (dump_file
, orig
);
1369 fputs (" into...", dump_file
);
1374 result
= evd
->callback (orig
, evd
->regs_active
, max_depth
,
1381 result
= expand_loc (CSELIB_VAL_PTR (orig
)->locs
, evd
, max_depth
);
1387 return evd
->callback (orig
, evd
->regs_active
, max_depth
,
1395 /* Copy the various flags, fields, and other information. We assume
1396 that all fields need copying, and then clear the fields that should
1397 not be copied. That is the sensible default behavior, and forces
1398 us to explicitly document why we are *not* copying a flag. */
1402 copy
= shallow_copy_rtx (orig
);
1404 format_ptr
= GET_RTX_FORMAT (code
);
1406 for (i
= 0; i
< GET_RTX_LENGTH (code
); i
++)
1407 switch (*format_ptr
++)
1410 if (XEXP (orig
, i
) != NULL
)
1412 rtx result
= cselib_expand_value_rtx_1 (XEXP (orig
, i
), evd
,
1417 XEXP (copy
, i
) = result
;
1423 if (XVEC (orig
, i
) != NULL
)
1426 XVEC (copy
, i
) = rtvec_alloc (XVECLEN (orig
, i
));
1427 for (j
= 0; j
< XVECLEN (orig
, i
); j
++)
1429 rtx result
= cselib_expand_value_rtx_1 (XVECEXP (orig
, i
, j
),
1430 evd
, max_depth
- 1);
1434 XVECEXP (copy
, i
, j
) = result
;
1448 /* These are left unchanged. */
1458 mode
= GET_MODE (copy
);
1459 /* If an operand has been simplified into CONST_INT, which doesn't
1460 have a mode and the mode isn't derivable from whole rtx's mode,
1461 try simplify_*_operation first with mode from original's operand
1462 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1464 switch (GET_RTX_CLASS (code
))
1467 if (CONST_INT_P (XEXP (copy
, 0))
1468 && GET_MODE (XEXP (orig
, 0)) != VOIDmode
)
1470 scopy
= simplify_unary_operation (code
, mode
, XEXP (copy
, 0),
1471 GET_MODE (XEXP (orig
, 0)));
1476 case RTX_COMM_ARITH
:
1478 /* These expressions can derive operand modes from the whole rtx's mode. */
1481 case RTX_BITFIELD_OPS
:
1482 if (CONST_INT_P (XEXP (copy
, 0))
1483 && GET_MODE (XEXP (orig
, 0)) != VOIDmode
)
1485 scopy
= simplify_ternary_operation (code
, mode
,
1486 GET_MODE (XEXP (orig
, 0)),
1487 XEXP (copy
, 0), XEXP (copy
, 1),
1494 case RTX_COMM_COMPARE
:
1495 if (CONST_INT_P (XEXP (copy
, 0))
1496 && GET_MODE (XEXP (copy
, 1)) == VOIDmode
1497 && (GET_MODE (XEXP (orig
, 0)) != VOIDmode
1498 || GET_MODE (XEXP (orig
, 1)) != VOIDmode
))
1500 scopy
= simplify_relational_operation (code
, mode
,
1501 (GET_MODE (XEXP (orig
, 0))
1503 ? GET_MODE (XEXP (orig
, 0))
1504 : GET_MODE (XEXP (orig
, 1)),
1514 scopy
= simplify_rtx (copy
);
1520 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1521 with VALUE expressions. This way, it becomes independent of changes
1522 to registers and memory.
1523 X isn't actually modified; if modifications are needed, new rtl is
1524 allocated. However, the return value can share rtl with X. */
1527 cselib_subst_to_values (rtx x
)
1529 enum rtx_code code
= GET_CODE (x
);
1530 const char *fmt
= GET_RTX_FORMAT (code
);
1539 l
= REG_VALUES (REGNO (x
));
1540 if (l
&& l
->elt
== NULL
)
1542 for (; l
; l
= l
->next
)
1543 if (GET_MODE (l
->elt
->val_rtx
) == GET_MODE (x
))
1544 return l
->elt
->val_rtx
;
1549 e
= cselib_lookup_mem (x
, 0);
1552 /* This happens for autoincrements. Assign a value that doesn't
1554 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
1570 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
1577 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1581 rtx t
= cselib_subst_to_values (XEXP (x
, i
));
1583 if (t
!= XEXP (x
, i
))
1586 copy
= shallow_copy_rtx (x
);
1590 else if (fmt
[i
] == 'E')
1594 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1596 rtx t
= cselib_subst_to_values (XVECEXP (x
, i
, j
));
1598 if (t
!= XVECEXP (x
, i
, j
))
1600 if (XVEC (x
, i
) == XVEC (copy
, i
))
1603 copy
= shallow_copy_rtx (x
);
1604 XVEC (copy
, i
) = shallow_copy_rtvec (XVEC (x
, i
));
1606 XVECEXP (copy
, i
, j
) = t
;
1615 /* Look up the rtl expression X in our tables and return the value it has.
1616 If CREATE is zero, we return NULL if we don't know the value. Otherwise,
1617 we create a new one if possible, using mode MODE if X doesn't have a mode
1618 (i.e. because it's a constant). */
1621 cselib_lookup_1 (rtx x
, enum machine_mode mode
, int create
)
1625 unsigned int hashval
;
1627 if (GET_MODE (x
) != VOIDmode
)
1628 mode
= GET_MODE (x
);
1630 if (GET_CODE (x
) == VALUE
)
1631 return CSELIB_VAL_PTR (x
);
1636 unsigned int i
= REGNO (x
);
1639 if (l
&& l
->elt
== NULL
)
1641 for (; l
; l
= l
->next
)
1642 if (mode
== GET_MODE (l
->elt
->val_rtx
))
1644 promote_debug_loc (l
->elt
->locs
);
1651 if (i
< FIRST_PSEUDO_REGISTER
)
1653 unsigned int n
= hard_regno_nregs
[i
][mode
];
1655 if (n
> max_value_regs
)
1659 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
1660 e
->locs
= new_elt_loc_list (e
->locs
, x
);
1661 if (REG_VALUES (i
) == 0)
1663 /* Maintain the invariant that the first entry of
1664 REG_VALUES, if present, must be the value used to set the
1665 register, or NULL. */
1666 used_regs
[n_used_regs
++] = i
;
1667 REG_VALUES (i
) = new_elt_list (REG_VALUES (i
), NULL
);
1669 REG_VALUES (i
)->next
= new_elt_list (REG_VALUES (i
)->next
, e
);
1670 slot
= htab_find_slot_with_hash (cselib_hash_table
, x
, e
->hash
, INSERT
);
1676 return cselib_lookup_mem (x
, create
);
1678 hashval
= cselib_hash_rtx (x
, create
);
1679 /* Can't even create if hashing is not possible. */
1683 slot
= htab_find_slot_with_hash (cselib_hash_table
, wrap_constant (mode
, x
),
1684 hashval
, create
? INSERT
: NO_INSERT
);
1688 e
= (cselib_val
*) *slot
;
1692 e
= new_cselib_val (hashval
, mode
, x
);
1694 /* We have to fill the slot before calling cselib_subst_to_values:
1695 the hash table is inconsistent until we do so, and
1696 cselib_subst_to_values will need to do lookups. */
1698 e
->locs
= new_elt_loc_list (e
->locs
, cselib_subst_to_values (x
));
1702 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
1705 cselib_lookup_from_insn (rtx x
, enum machine_mode mode
,
1706 int create
, rtx insn
)
1710 gcc_assert (!cselib_current_insn
);
1711 cselib_current_insn
= insn
;
1713 ret
= cselib_lookup (x
, mode
, create
);
1715 cselib_current_insn
= NULL
;
1720 /* Wrapper for cselib_lookup_1, that logs the lookup result and
1721 maintains invariants related with debug insns. */
1724 cselib_lookup (rtx x
, enum machine_mode mode
, int create
)
1726 cselib_val
*ret
= cselib_lookup_1 (x
, mode
, create
);
1728 /* ??? Should we return NULL if we're not to create an entry, the
1729 found loc is a debug loc and cselib_current_insn is not DEBUG?
1730 If so, we should also avoid converting val to non-DEBUG; probably
1731 easiest setting cselib_current_insn to NULL before the call
1734 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1736 fputs ("cselib lookup ", dump_file
);
1737 print_inline_rtx (dump_file
, x
, 2);
1738 fprintf (dump_file
, " => %u:%u\n",
1740 ret
? ret
->hash
: 0);
1746 /* Invalidate any entries in reg_values that overlap REGNO. This is called
1747 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
1748 is used to determine how many hard registers are being changed. If MODE
1749 is VOIDmode, then only REGNO is being changed; this is used when
1750 invalidating call clobbered registers across a call. */
1753 cselib_invalidate_regno (unsigned int regno
, enum machine_mode mode
)
1755 unsigned int endregno
;
1758 /* If we see pseudos after reload, something is _wrong_. */
1759 gcc_assert (!reload_completed
|| regno
< FIRST_PSEUDO_REGISTER
1760 || reg_renumber
[regno
] < 0);
1762 /* Determine the range of registers that must be invalidated. For
1763 pseudos, only REGNO is affected. For hard regs, we must take MODE
1764 into account, and we must also invalidate lower register numbers
1765 if they contain values that overlap REGNO. */
1766 if (regno
< FIRST_PSEUDO_REGISTER
)
1768 gcc_assert (mode
!= VOIDmode
);
1770 if (regno
< max_value_regs
)
1773 i
= regno
- max_value_regs
;
1775 endregno
= end_hard_regno (mode
, regno
);
1780 endregno
= regno
+ 1;
1783 for (; i
< endregno
; i
++)
1785 struct elt_list
**l
= ®_VALUES (i
);
1787 /* Go through all known values for this reg; if it overlaps the range
1788 we're invalidating, remove the value. */
1791 cselib_val
*v
= (*l
)->elt
;
1794 struct elt_loc_list
**p
;
1795 unsigned int this_last
= i
;
1797 if (i
< FIRST_PSEUDO_REGISTER
&& v
!= NULL
)
1798 this_last
= end_hard_regno (GET_MODE (v
->val_rtx
), i
) - 1;
1800 if (this_last
< regno
|| v
== NULL
1801 || (v
== cfa_base_preserved_val
1802 && i
== cfa_base_preserved_regno
))
1808 /* We have an overlap. */
1809 if (*l
== REG_VALUES (i
))
1811 /* Maintain the invariant that the first entry of
1812 REG_VALUES, if present, must be the value used to set
1813 the register, or NULL. This is also nice because
1814 then we won't push the same regno onto user_regs
1820 unchain_one_elt_list (l
);
1822 had_locs
= v
->locs
!= NULL
;
1823 setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
1825 /* Now, we clear the mapping from value to reg. It must exist, so
1826 this code will crash intentionally if it doesn't. */
1827 for (p
= &v
->locs
; ; p
= &(*p
)->next
)
1831 if (REG_P (x
) && REGNO (x
) == i
)
1833 unchain_one_elt_loc_list (p
);
1838 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
1840 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
1841 n_useless_debug_values
++;
1849 /* Return 1 if X has a value that can vary even between two
1850 executions of the program. 0 means X can be compared reliably
1851 against certain constants or near-constants. */
1854 cselib_rtx_varies_p (const_rtx x ATTRIBUTE_UNUSED
, bool from_alias ATTRIBUTE_UNUSED
)
1856 /* We actually don't need to verify very hard. This is because
1857 if X has actually changed, we invalidate the memory anyway,
1858 so assume that all common memory addresses are
1863 /* Invalidate any locations in the table which are changed because of a
1864 store to MEM_RTX. If this is called because of a non-const call
1865 instruction, MEM_RTX is (mem:BLK const0_rtx). */
1868 cselib_invalidate_mem (rtx mem_rtx
)
1870 cselib_val
**vp
, *v
, *next
;
1874 mem_addr
= canon_rtx (get_addr (XEXP (mem_rtx
, 0)));
1875 mem_rtx
= canon_rtx (mem_rtx
);
1877 vp
= &first_containing_mem
;
1878 for (v
= *vp
; v
!= &dummy_val
; v
= next
)
1880 bool has_mem
= false;
1881 struct elt_loc_list
**p
= &v
->locs
;
1882 bool had_locs
= v
->locs
!= NULL
;
1883 rtx setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
1889 struct elt_list
**mem_chain
;
1891 /* MEMs may occur in locations only at the top level; below
1892 that every MEM or REG is substituted by its VALUE. */
1898 if (num_mems
< PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS
)
1899 && ! canon_true_dependence (mem_rtx
, GET_MODE (mem_rtx
), mem_addr
,
1900 x
, NULL_RTX
, cselib_rtx_varies_p
))
1908 /* This one overlaps. */
1909 /* We must have a mapping from this MEM's address to the
1910 value (E). Remove that, too. */
1911 addr
= cselib_lookup (XEXP (x
, 0), VOIDmode
, 0);
1912 mem_chain
= &addr
->addr_list
;
1915 if ((*mem_chain
)->elt
== v
)
1917 unchain_one_elt_list (mem_chain
);
1921 mem_chain
= &(*mem_chain
)->next
;
1924 unchain_one_elt_loc_list (p
);
1927 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
1929 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
1930 n_useless_debug_values
++;
1935 next
= v
->next_containing_mem
;
1939 vp
= &(*vp
)->next_containing_mem
;
1942 v
->next_containing_mem
= NULL
;
1947 /* Invalidate DEST, which is being assigned to or clobbered. */
1950 cselib_invalidate_rtx (rtx dest
)
1952 while (GET_CODE (dest
) == SUBREG
1953 || GET_CODE (dest
) == ZERO_EXTRACT
1954 || GET_CODE (dest
) == STRICT_LOW_PART
)
1955 dest
= XEXP (dest
, 0);
1958 cselib_invalidate_regno (REGNO (dest
), GET_MODE (dest
));
1959 else if (MEM_P (dest
))
1960 cselib_invalidate_mem (dest
);
1962 /* Some machines don't define AUTO_INC_DEC, but they still use push
1963 instructions. We need to catch that case here in order to
1964 invalidate the stack pointer correctly. Note that invalidating
1965 the stack pointer is different from invalidating DEST. */
1966 if (push_operand (dest
, GET_MODE (dest
)))
1967 cselib_invalidate_rtx (stack_pointer_rtx
);
1970 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
1973 cselib_invalidate_rtx_note_stores (rtx dest
, const_rtx ignore ATTRIBUTE_UNUSED
,
1974 void *data ATTRIBUTE_UNUSED
)
1976 cselib_invalidate_rtx (dest
);
1979 /* Record the result of a SET instruction. DEST is being set; the source
1980 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
1981 describes its address. */
1984 cselib_record_set (rtx dest
, cselib_val
*src_elt
, cselib_val
*dest_addr_elt
)
1986 int dreg
= REG_P (dest
) ? (int) REGNO (dest
) : -1;
1988 if (src_elt
== 0 || side_effects_p (dest
))
1993 if (dreg
< FIRST_PSEUDO_REGISTER
)
1995 unsigned int n
= hard_regno_nregs
[dreg
][GET_MODE (dest
)];
1997 if (n
> max_value_regs
)
2001 if (REG_VALUES (dreg
) == 0)
2003 used_regs
[n_used_regs
++] = dreg
;
2004 REG_VALUES (dreg
) = new_elt_list (REG_VALUES (dreg
), src_elt
);
2008 /* The register should have been invalidated. */
2009 gcc_assert (REG_VALUES (dreg
)->elt
== 0);
2010 REG_VALUES (dreg
)->elt
= src_elt
;
2013 if (src_elt
->locs
== 0 && !PRESERVED_VALUE_P (src_elt
->val_rtx
))
2015 src_elt
->locs
= new_elt_loc_list (src_elt
->locs
, dest
);
2017 else if (MEM_P (dest
) && dest_addr_elt
!= 0
2018 && cselib_record_memory
)
2020 if (src_elt
->locs
== 0 && !PRESERVED_VALUE_P (src_elt
->val_rtx
))
2022 add_mem_for_addr (dest_addr_elt
, src_elt
, dest
);
2026 /* There is no good way to determine how many elements there can be
2027 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2028 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2030 /* Record the effects of any sets in INSN. */
2032 cselib_record_sets (rtx insn
)
2036 struct cselib_set sets
[MAX_SETS
];
2037 rtx body
= PATTERN (insn
);
2040 body
= PATTERN (insn
);
2041 if (GET_CODE (body
) == COND_EXEC
)
2043 cond
= COND_EXEC_TEST (body
);
2044 body
= COND_EXEC_CODE (body
);
2047 /* Find all sets. */
2048 if (GET_CODE (body
) == SET
)
2050 sets
[0].src
= SET_SRC (body
);
2051 sets
[0].dest
= SET_DEST (body
);
2054 else if (GET_CODE (body
) == PARALLEL
)
2056 /* Look through the PARALLEL and record the values being
2057 set, if possible. Also handle any CLOBBERs. */
2058 for (i
= XVECLEN (body
, 0) - 1; i
>= 0; --i
)
2060 rtx x
= XVECEXP (body
, 0, i
);
2062 if (GET_CODE (x
) == SET
)
2064 sets
[n_sets
].src
= SET_SRC (x
);
2065 sets
[n_sets
].dest
= SET_DEST (x
);
2072 && MEM_P (sets
[0].src
)
2073 && !cselib_record_memory
2074 && MEM_READONLY_P (sets
[0].src
))
2076 rtx note
= find_reg_equal_equiv_note (insn
);
2078 if (note
&& CONSTANT_P (XEXP (note
, 0)))
2079 sets
[0].src
= XEXP (note
, 0);
2082 /* Look up the values that are read. Do this before invalidating the
2083 locations that are written. */
2084 for (i
= 0; i
< n_sets
; i
++)
2086 rtx dest
= sets
[i
].dest
;
2088 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2089 the low part after invalidating any knowledge about larger modes. */
2090 if (GET_CODE (sets
[i
].dest
) == STRICT_LOW_PART
)
2091 sets
[i
].dest
= dest
= XEXP (dest
, 0);
2093 /* We don't know how to record anything but REG or MEM. */
2095 || (MEM_P (dest
) && cselib_record_memory
))
2097 rtx src
= sets
[i
].src
;
2099 src
= gen_rtx_IF_THEN_ELSE (GET_MODE (dest
), cond
, src
, dest
);
2100 sets
[i
].src_elt
= cselib_lookup (src
, GET_MODE (dest
), 1);
2103 enum machine_mode address_mode
2104 = targetm
.addr_space
.address_mode (MEM_ADDR_SPACE (dest
));
2106 sets
[i
].dest_addr_elt
= cselib_lookup (XEXP (dest
, 0),
2110 sets
[i
].dest_addr_elt
= 0;
2114 if (cselib_record_sets_hook
)
2115 cselib_record_sets_hook (insn
, sets
, n_sets
);
2117 /* Invalidate all locations written by this insn. Note that the elts we
2118 looked up in the previous loop aren't affected, just some of their
2119 locations may go away. */
2120 note_stores (body
, cselib_invalidate_rtx_note_stores
, NULL
);
2122 /* If this is an asm, look for duplicate sets. This can happen when the
2123 user uses the same value as an output multiple times. This is valid
2124 if the outputs are not actually used thereafter. Treat this case as
2125 if the value isn't actually set. We do this by smashing the destination
2126 to pc_rtx, so that we won't record the value later. */
2127 if (n_sets
>= 2 && asm_noperands (body
) >= 0)
2129 for (i
= 0; i
< n_sets
; i
++)
2131 rtx dest
= sets
[i
].dest
;
2132 if (REG_P (dest
) || MEM_P (dest
))
2135 for (j
= i
+ 1; j
< n_sets
; j
++)
2136 if (rtx_equal_p (dest
, sets
[j
].dest
))
2138 sets
[i
].dest
= pc_rtx
;
2139 sets
[j
].dest
= pc_rtx
;
2145 /* Now enter the equivalences in our tables. */
2146 for (i
= 0; i
< n_sets
; i
++)
2148 rtx dest
= sets
[i
].dest
;
2150 || (MEM_P (dest
) && cselib_record_memory
))
2151 cselib_record_set (dest
, sets
[i
].src_elt
, sets
[i
].dest_addr_elt
);
2155 /* Record the effects of INSN. */
2158 cselib_process_insn (rtx insn
)
2163 cselib_current_insn
= insn
;
2165 /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp. */
2168 && find_reg_note (insn
, REG_SETJMP
, NULL
))
2169 || (NONJUMP_INSN_P (insn
)
2170 && GET_CODE (PATTERN (insn
)) == ASM_OPERANDS
2171 && MEM_VOLATILE_P (PATTERN (insn
))))
2173 cselib_reset_table (next_uid
);
2174 cselib_current_insn
= NULL_RTX
;
2178 if (! INSN_P (insn
))
2180 cselib_current_insn
= NULL_RTX
;
2184 /* If this is a call instruction, forget anything stored in a
2185 call clobbered register, or, if this is not a const call, in
2189 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
2190 if (call_used_regs
[i
]
2191 || (REG_VALUES (i
) && REG_VALUES (i
)->elt
2192 && HARD_REGNO_CALL_PART_CLOBBERED (i
,
2193 GET_MODE (REG_VALUES (i
)->elt
->val_rtx
))))
2194 cselib_invalidate_regno (i
, reg_raw_mode
[i
]);
2196 /* Since it is not clear how cselib is going to be used, be
2197 conservative here and treat looping pure or const functions
2198 as if they were regular functions. */
2199 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn
)
2200 || !(RTL_CONST_OR_PURE_CALL_P (insn
)))
2201 cselib_invalidate_mem (callmem
);
2204 cselib_record_sets (insn
);
2207 /* Clobber any registers which appear in REG_INC notes. We
2208 could keep track of the changes to their values, but it is
2209 unlikely to help. */
2210 for (x
= REG_NOTES (insn
); x
; x
= XEXP (x
, 1))
2211 if (REG_NOTE_KIND (x
) == REG_INC
)
2212 cselib_invalidate_rtx (XEXP (x
, 0));
2215 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2216 after we have processed the insn. */
2218 for (x
= CALL_INSN_FUNCTION_USAGE (insn
); x
; x
= XEXP (x
, 1))
2219 if (GET_CODE (XEXP (x
, 0)) == CLOBBER
)
2220 cselib_invalidate_rtx (XEXP (XEXP (x
, 0), 0));
2222 cselib_current_insn
= NULL_RTX
;
2224 if (n_useless_values
> MAX_USELESS_VALUES
2225 /* remove_useless_values is linear in the hash table size. Avoid
2226 quadratic behavior for very large hashtables with very few
2227 useless elements. */
2228 && ((unsigned int)n_useless_values
2229 > (cselib_hash_table
->n_elements
2230 - cselib_hash_table
->n_deleted
2231 - n_debug_values
) / 4))
2232 remove_useless_values ();
2235 /* Initialize cselib for one pass. The caller must also call
2236 init_alias_analysis. */
2239 cselib_init (int record_what
)
2241 elt_list_pool
= create_alloc_pool ("elt_list",
2242 sizeof (struct elt_list
), 10);
2243 elt_loc_list_pool
= create_alloc_pool ("elt_loc_list",
2244 sizeof (struct elt_loc_list
), 10);
2245 cselib_val_pool
= create_alloc_pool ("cselib_val_list",
2246 sizeof (cselib_val
), 10);
2247 value_pool
= create_alloc_pool ("value", RTX_CODE_SIZE (VALUE
), 100);
2248 cselib_record_memory
= record_what
& CSELIB_RECORD_MEMORY
;
2249 cselib_preserve_constants
= record_what
& CSELIB_PRESERVE_CONSTANTS
;
2251 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2252 see canon_true_dependence. This is only created once. */
2254 callmem
= gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
));
2256 cselib_nregs
= max_reg_num ();
2258 /* We preserve reg_values to allow expensive clearing of the whole thing.
2259 Reallocate it however if it happens to be too large. */
2260 if (!reg_values
|| reg_values_size
< cselib_nregs
2261 || (reg_values_size
> 10 && reg_values_size
> cselib_nregs
* 4))
2265 /* Some space for newly emit instructions so we don't end up
2266 reallocating in between passes. */
2267 reg_values_size
= cselib_nregs
+ (63 + cselib_nregs
) / 16;
2268 reg_values
= XCNEWVEC (struct elt_list
*, reg_values_size
);
2270 used_regs
= XNEWVEC (unsigned int, cselib_nregs
);
2272 cselib_hash_table
= htab_create (31, get_value_hash
,
2273 entry_and_rtx_equal_p
, NULL
);
2277 /* Called when the current user is done with cselib. */
2280 cselib_finish (void)
2282 cselib_discard_hook
= NULL
;
2283 cselib_preserve_constants
= false;
2284 cfa_base_preserved_val
= NULL
;
2285 cfa_base_preserved_regno
= INVALID_REGNUM
;
2286 free_alloc_pool (elt_list_pool
);
2287 free_alloc_pool (elt_loc_list_pool
);
2288 free_alloc_pool (cselib_val_pool
);
2289 free_alloc_pool (value_pool
);
2290 cselib_clear_table ();
2291 htab_delete (cselib_hash_table
);
2294 cselib_hash_table
= 0;
2295 n_useless_values
= 0;
2296 n_useless_debug_values
= 0;
2301 /* Dump the cselib_val *X to FILE *info. */
2304 dump_cselib_val (void **x
, void *info
)
2306 cselib_val
*v
= (cselib_val
*)*x
;
2307 FILE *out
= (FILE *)info
;
2308 bool need_lf
= true;
2310 print_inline_rtx (out
, v
->val_rtx
, 0);
2314 struct elt_loc_list
*l
= v
->locs
;
2320 fputs (" locs:", out
);
2323 fprintf (out
, "\n from insn %i ",
2324 INSN_UID (l
->setting_insn
));
2325 print_inline_rtx (out
, l
->loc
, 4);
2327 while ((l
= l
->next
));
2332 fputs (" no locs", out
);
2338 struct elt_list
*e
= v
->addr_list
;
2344 fputs (" addr list:", out
);
2348 print_inline_rtx (out
, e
->elt
->val_rtx
, 2);
2350 while ((e
= e
->next
));
2355 fputs (" no addrs", out
);
2359 if (v
->next_containing_mem
== &dummy_val
)
2360 fputs (" last mem\n", out
);
2361 else if (v
->next_containing_mem
)
2363 fputs (" next mem ", out
);
2364 print_inline_rtx (out
, v
->next_containing_mem
->val_rtx
, 2);
2373 /* Dump to OUT everything in the CSELIB table. */
2376 dump_cselib_table (FILE *out
)
2378 fprintf (out
, "cselib hash table:\n");
2379 htab_traverse (cselib_hash_table
, dump_cselib_val
, out
);
2380 if (first_containing_mem
!= &dummy_val
)
2382 fputs ("first mem ", out
);
2383 print_inline_rtx (out
, first_containing_mem
->val_rtx
, 2);
2386 fprintf (out
, "next uid %i\n", next_uid
);
2389 #include "gt-cselib.h"