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
))
704 return XEXP (x
, 0) == XEXP (y
, 0);
711 fmt
= GET_RTX_FORMAT (code
);
713 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
720 if (XWINT (x
, i
) != XWINT (y
, i
))
726 if (XINT (x
, i
) != XINT (y
, i
))
732 /* Two vectors must have the same length. */
733 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
736 /* And the corresponding elements must match. */
737 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
738 if (! rtx_equal_for_cselib_p (XVECEXP (x
, i
, j
),
745 && targetm
.commutative_p (x
, UNKNOWN
)
746 && rtx_equal_for_cselib_p (XEXP (x
, 1), XEXP (y
, 0))
747 && rtx_equal_for_cselib_p (XEXP (x
, 0), XEXP (y
, 1)))
749 if (! rtx_equal_for_cselib_p (XEXP (x
, i
), XEXP (y
, i
)))
755 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
760 /* These are just backpointers, so they don't matter. */
767 /* It is believed that rtx's at this level will never
768 contain anything but integers and other rtx's,
769 except for within LABEL_REFs and SYMBOL_REFs. */
777 /* We need to pass down the mode of constants through the hash table
778 functions. For that purpose, wrap them in a CONST of the appropriate
781 wrap_constant (enum machine_mode mode
, rtx x
)
783 if (!CONST_INT_P (x
) && GET_CODE (x
) != CONST_FIXED
784 && (GET_CODE (x
) != CONST_DOUBLE
|| GET_MODE (x
) != VOIDmode
))
786 gcc_assert (mode
!= VOIDmode
);
787 return gen_rtx_CONST (mode
, x
);
790 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
791 For registers and memory locations, we look up their cselib_val structure
792 and return its VALUE element.
793 Possible reasons for return 0 are: the object is volatile, or we couldn't
794 find a register or memory location in the table and CREATE is zero. If
795 CREATE is nonzero, table elts are created for regs and mem.
796 N.B. this hash function returns the same hash value for RTXes that
797 differ only in the order of operands, thus it is suitable for comparisons
798 that take commutativity into account.
799 If we wanted to also support associative rules, we'd have to use a different
800 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
801 We used to have a MODE argument for hashing for CONST_INTs, but that
802 didn't make sense, since it caused spurious hash differences between
803 (set (reg:SI 1) (const_int))
804 (plus:SI (reg:SI 2) (reg:SI 1))
806 (plus:SI (reg:SI 2) (const_int))
807 If the mode is important in any context, it must be checked specifically
808 in a comparison anyway, since relying on hash differences is unsafe. */
811 cselib_hash_rtx (rtx x
, int create
)
817 unsigned int hash
= 0;
820 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
826 e
= cselib_lookup (x
, GET_MODE (x
), create
);
833 hash
+= ((unsigned) DEBUG_EXPR
<< 7)
834 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x
));
835 return hash
? hash
: (unsigned int) DEBUG_EXPR
;
838 hash
+= ((unsigned) CONST_INT
<< 7) + INTVAL (x
);
839 return hash
? hash
: (unsigned int) CONST_INT
;
842 /* This is like the general case, except that it only counts
843 the integers representing the constant. */
844 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
845 if (GET_MODE (x
) != VOIDmode
)
846 hash
+= real_hash (CONST_DOUBLE_REAL_VALUE (x
));
848 hash
+= ((unsigned) CONST_DOUBLE_LOW (x
)
849 + (unsigned) CONST_DOUBLE_HIGH (x
));
850 return hash
? hash
: (unsigned int) CONST_DOUBLE
;
853 hash
+= (unsigned int) code
+ (unsigned int) GET_MODE (x
);
854 hash
+= fixed_hash (CONST_FIXED_VALUE (x
));
855 return hash
? hash
: (unsigned int) CONST_FIXED
;
862 units
= CONST_VECTOR_NUNITS (x
);
864 for (i
= 0; i
< units
; ++i
)
866 elt
= CONST_VECTOR_ELT (x
, i
);
867 hash
+= cselib_hash_rtx (elt
, 0);
873 /* Assume there is only one rtx object for any given label. */
875 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
876 differences and differences between each stage's debugging dumps. */
877 hash
+= (((unsigned int) LABEL_REF
<< 7)
878 + CODE_LABEL_NUMBER (XEXP (x
, 0)));
879 return hash
? hash
: (unsigned int) LABEL_REF
;
883 /* Don't hash on the symbol's address to avoid bootstrap differences.
884 Different hash values may cause expressions to be recorded in
885 different orders and thus different registers to be used in the
886 final assembler. This also avoids differences in the dump files
887 between various stages. */
889 const unsigned char *p
= (const unsigned char *) XSTR (x
, 0);
892 h
+= (h
<< 7) + *p
++; /* ??? revisit */
894 hash
+= ((unsigned int) SYMBOL_REF
<< 7) + h
;
895 return hash
? hash
: (unsigned int) SYMBOL_REF
;
907 case UNSPEC_VOLATILE
:
911 if (MEM_VOLATILE_P (x
))
920 i
= GET_RTX_LENGTH (code
) - 1;
921 fmt
= GET_RTX_FORMAT (code
);
928 rtx tem
= XEXP (x
, i
);
929 unsigned int tem_hash
= cselib_hash_rtx (tem
, create
);
938 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
940 unsigned int tem_hash
941 = cselib_hash_rtx (XVECEXP (x
, i
, j
), create
);
952 const unsigned char *p
= (const unsigned char *) XSTR (x
, i
);
974 return hash
? hash
: 1 + (unsigned int) GET_CODE (x
);
977 /* Create a new value structure for VALUE and initialize it. The mode of the
980 static inline cselib_val
*
981 new_cselib_val (unsigned int hash
, enum machine_mode mode
, rtx x
)
983 cselib_val
*e
= (cselib_val
*) pool_alloc (cselib_val_pool
);
986 gcc_assert (next_uid
);
990 /* We use an alloc pool to allocate this RTL construct because it
991 accounts for about 8% of the overall memory usage. We know
992 precisely when we can have VALUE RTXen (when cselib is active)
993 so we don't need to put them in garbage collected memory.
994 ??? Why should a VALUE be an RTX in the first place? */
995 e
->val_rtx
= (rtx
) pool_alloc (value_pool
);
996 memset (e
->val_rtx
, 0, RTX_HDR_SIZE
);
997 PUT_CODE (e
->val_rtx
, VALUE
);
998 PUT_MODE (e
->val_rtx
, mode
);
999 CSELIB_VAL_PTR (e
->val_rtx
) = e
;
1002 e
->next_containing_mem
= 0;
1004 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1006 fprintf (dump_file
, "cselib value %u:%u ", e
->uid
, hash
);
1007 if (flag_dump_noaddr
|| flag_dump_unnumbered
)
1008 fputs ("# ", dump_file
);
1010 fprintf (dump_file
, "%p ", (void*)e
);
1011 print_rtl_single (dump_file
, x
);
1012 fputc ('\n', dump_file
);
1018 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1019 contains the data at this address. X is a MEM that represents the
1020 value. Update the two value structures to represent this situation. */
1023 add_mem_for_addr (cselib_val
*addr_elt
, cselib_val
*mem_elt
, rtx x
)
1025 struct elt_loc_list
*l
;
1027 /* Avoid duplicates. */
1028 for (l
= mem_elt
->locs
; l
; l
= l
->next
)
1030 && CSELIB_VAL_PTR (XEXP (l
->loc
, 0)) == addr_elt
)
1032 promote_debug_loc (l
);
1036 addr_elt
->addr_list
= new_elt_list (addr_elt
->addr_list
, mem_elt
);
1038 = new_elt_loc_list (mem_elt
->locs
,
1039 replace_equiv_address_nv (x
, addr_elt
->val_rtx
));
1040 if (mem_elt
->next_containing_mem
== NULL
)
1042 mem_elt
->next_containing_mem
= first_containing_mem
;
1043 first_containing_mem
= mem_elt
;
1047 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1048 If CREATE, make a new one if we haven't seen it before. */
1051 cselib_lookup_mem (rtx x
, int create
)
1053 enum machine_mode mode
= GET_MODE (x
);
1056 cselib_val
*mem_elt
;
1059 if (MEM_VOLATILE_P (x
) || mode
== BLKmode
1060 || !cselib_record_memory
1061 || (FLOAT_MODE_P (mode
) && flag_float_store
))
1064 /* Look up the value for the address. */
1065 addr
= cselib_lookup (XEXP (x
, 0), mode
, create
);
1069 /* Find a value that describes a value of our mode at that address. */
1070 for (l
= addr
->addr_list
; l
; l
= l
->next
)
1071 if (GET_MODE (l
->elt
->val_rtx
) == mode
)
1073 promote_debug_loc (l
->elt
->locs
);
1080 mem_elt
= new_cselib_val (next_uid
, mode
, x
);
1081 add_mem_for_addr (addr
, mem_elt
, x
);
1082 slot
= htab_find_slot_with_hash (cselib_hash_table
, wrap_constant (mode
, x
),
1083 mem_elt
->hash
, INSERT
);
1088 /* Search thru the possible substitutions in P. We prefer a non reg
1089 substitution because this allows us to expand the tree further. If
1090 we find, just a reg, take the lowest regno. There may be several
1091 non-reg results, we just take the first one because they will all
1092 expand to the same place. */
1095 expand_loc (struct elt_loc_list
*p
, struct expand_value_data
*evd
,
1098 rtx reg_result
= NULL
;
1099 unsigned int regno
= UINT_MAX
;
1100 struct elt_loc_list
*p_in
= p
;
1102 for (; p
; p
= p
-> next
)
1104 /* Avoid infinite recursion trying to expand a reg into a
1106 if ((REG_P (p
->loc
))
1107 && (REGNO (p
->loc
) < regno
)
1108 && !bitmap_bit_p (evd
->regs_active
, REGNO (p
->loc
)))
1110 reg_result
= p
->loc
;
1111 regno
= REGNO (p
->loc
);
1113 /* Avoid infinite recursion and do not try to expand the
1115 else if (GET_CODE (p
->loc
) == VALUE
1116 && CSELIB_VAL_PTR (p
->loc
)->locs
== p_in
)
1118 else if (!REG_P (p
->loc
))
1121 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1123 print_inline_rtx (dump_file
, p
->loc
, 0);
1124 fprintf (dump_file
, "\n");
1126 if (GET_CODE (p
->loc
) == LO_SUM
1127 && GET_CODE (XEXP (p
->loc
, 1)) == SYMBOL_REF
1129 && (note
= find_reg_note (p
->setting_insn
, REG_EQUAL
, NULL_RTX
))
1130 && XEXP (note
, 0) == XEXP (p
->loc
, 1))
1131 return XEXP (p
->loc
, 1);
1132 result
= cselib_expand_value_rtx_1 (p
->loc
, evd
, max_depth
- 1);
1139 if (regno
!= UINT_MAX
)
1142 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1143 fprintf (dump_file
, "r%d\n", regno
);
1145 result
= cselib_expand_value_rtx_1 (reg_result
, evd
, max_depth
- 1);
1150 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1154 print_inline_rtx (dump_file
, reg_result
, 0);
1155 fprintf (dump_file
, "\n");
1158 fprintf (dump_file
, "NULL\n");
1164 /* Forward substitute and expand an expression out to its roots.
1165 This is the opposite of common subexpression. Because local value
1166 numbering is such a weak optimization, the expanded expression is
1167 pretty much unique (not from a pointer equals point of view but
1168 from a tree shape point of view.
1170 This function returns NULL if the expansion fails. The expansion
1171 will fail if there is no value number for one of the operands or if
1172 one of the operands has been overwritten between the current insn
1173 and the beginning of the basic block. For instance x has no
1179 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1180 It is clear on return. */
1183 cselib_expand_value_rtx (rtx orig
, bitmap regs_active
, int max_depth
)
1185 struct expand_value_data evd
;
1187 evd
.regs_active
= regs_active
;
1188 evd
.callback
= NULL
;
1189 evd
.callback_arg
= NULL
;
1192 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
);
1195 /* Same as cselib_expand_value_rtx, but using a callback to try to
1196 resolve some expressions. The CB function should return ORIG if it
1197 can't or does not want to deal with a certain RTX. Any other
1198 return value, including NULL, will be used as the expansion for
1199 VALUE, without any further changes. */
1202 cselib_expand_value_rtx_cb (rtx orig
, bitmap regs_active
, int max_depth
,
1203 cselib_expand_callback cb
, void *data
)
1205 struct expand_value_data evd
;
1207 evd
.regs_active
= regs_active
;
1209 evd
.callback_arg
= data
;
1212 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
);
1215 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1216 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1217 would return NULL or non-NULL, without allocating new rtx. */
1220 cselib_dummy_expand_value_rtx_cb (rtx orig
, bitmap regs_active
, int max_depth
,
1221 cselib_expand_callback cb
, void *data
)
1223 struct expand_value_data evd
;
1225 evd
.regs_active
= regs_active
;
1227 evd
.callback_arg
= data
;
1230 return cselib_expand_value_rtx_1 (orig
, &evd
, max_depth
) != NULL
;
1233 /* Internal implementation of cselib_expand_value_rtx and
1234 cselib_expand_value_rtx_cb. */
1237 cselib_expand_value_rtx_1 (rtx orig
, struct expand_value_data
*evd
,
1243 const char *format_ptr
;
1244 enum machine_mode mode
;
1246 code
= GET_CODE (orig
);
1248 /* For the context of dse, if we end up expand into a huge tree, we
1249 will not have a useful address, so we might as well just give up
1258 struct elt_list
*l
= REG_VALUES (REGNO (orig
));
1260 if (l
&& l
->elt
== NULL
)
1262 for (; l
; l
= l
->next
)
1263 if (GET_MODE (l
->elt
->val_rtx
) == GET_MODE (orig
))
1266 int regno
= REGNO (orig
);
1268 /* The only thing that we are not willing to do (this
1269 is requirement of dse and if others potential uses
1270 need this function we should add a parm to control
1271 it) is that we will not substitute the
1272 STACK_POINTER_REGNUM, FRAME_POINTER or the
1275 These expansions confuses the code that notices that
1276 stores into the frame go dead at the end of the
1277 function and that the frame is not effected by calls
1278 to subroutines. If you allow the
1279 STACK_POINTER_REGNUM substitution, then dse will
1280 think that parameter pushing also goes dead which is
1281 wrong. If you allow the FRAME_POINTER or the
1282 HARD_FRAME_POINTER then you lose the opportunity to
1283 make the frame assumptions. */
1284 if (regno
== STACK_POINTER_REGNUM
1285 || regno
== FRAME_POINTER_REGNUM
1286 || regno
== HARD_FRAME_POINTER_REGNUM
)
1289 bitmap_set_bit (evd
->regs_active
, regno
);
1291 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1292 fprintf (dump_file
, "expanding: r%d into: ", regno
);
1294 result
= expand_loc (l
->elt
->locs
, evd
, max_depth
);
1295 bitmap_clear_bit (evd
->regs_active
, regno
);
1312 /* SCRATCH must be shared because they represent distinct values. */
1315 if (REG_P (XEXP (orig
, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig
, 0))))
1320 if (shared_const_p (orig
))
1330 subreg
= evd
->callback (orig
, evd
->regs_active
, max_depth
,
1336 subreg
= cselib_expand_value_rtx_1 (SUBREG_REG (orig
), evd
,
1340 scopy
= simplify_gen_subreg (GET_MODE (orig
), subreg
,
1341 GET_MODE (SUBREG_REG (orig
)),
1342 SUBREG_BYTE (orig
));
1344 || (GET_CODE (scopy
) == SUBREG
1345 && !REG_P (SUBREG_REG (scopy
))
1346 && !MEM_P (SUBREG_REG (scopy
))))
1356 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1358 fputs ("\nexpanding ", dump_file
);
1359 print_rtl_single (dump_file
, orig
);
1360 fputs (" into...", dump_file
);
1365 result
= evd
->callback (orig
, evd
->regs_active
, max_depth
,
1372 result
= expand_loc (CSELIB_VAL_PTR (orig
)->locs
, evd
, max_depth
);
1378 return evd
->callback (orig
, evd
->regs_active
, max_depth
,
1386 /* Copy the various flags, fields, and other information. We assume
1387 that all fields need copying, and then clear the fields that should
1388 not be copied. That is the sensible default behavior, and forces
1389 us to explicitly document why we are *not* copying a flag. */
1393 copy
= shallow_copy_rtx (orig
);
1395 format_ptr
= GET_RTX_FORMAT (code
);
1397 for (i
= 0; i
< GET_RTX_LENGTH (code
); i
++)
1398 switch (*format_ptr
++)
1401 if (XEXP (orig
, i
) != NULL
)
1403 rtx result
= cselib_expand_value_rtx_1 (XEXP (orig
, i
), evd
,
1408 XEXP (copy
, i
) = result
;
1414 if (XVEC (orig
, i
) != NULL
)
1417 XVEC (copy
, i
) = rtvec_alloc (XVECLEN (orig
, i
));
1418 for (j
= 0; j
< XVECLEN (orig
, i
); j
++)
1420 rtx result
= cselib_expand_value_rtx_1 (XVECEXP (orig
, i
, j
),
1421 evd
, max_depth
- 1);
1425 XVECEXP (copy
, i
, j
) = result
;
1439 /* These are left unchanged. */
1449 mode
= GET_MODE (copy
);
1450 /* If an operand has been simplified into CONST_INT, which doesn't
1451 have a mode and the mode isn't derivable from whole rtx's mode,
1452 try simplify_*_operation first with mode from original's operand
1453 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1455 switch (GET_RTX_CLASS (code
))
1458 if (CONST_INT_P (XEXP (copy
, 0))
1459 && GET_MODE (XEXP (orig
, 0)) != VOIDmode
)
1461 scopy
= simplify_unary_operation (code
, mode
, XEXP (copy
, 0),
1462 GET_MODE (XEXP (orig
, 0)));
1467 case RTX_COMM_ARITH
:
1469 /* These expressions can derive operand modes from the whole rtx's mode. */
1472 case RTX_BITFIELD_OPS
:
1473 if (CONST_INT_P (XEXP (copy
, 0))
1474 && GET_MODE (XEXP (orig
, 0)) != VOIDmode
)
1476 scopy
= simplify_ternary_operation (code
, mode
,
1477 GET_MODE (XEXP (orig
, 0)),
1478 XEXP (copy
, 0), XEXP (copy
, 1),
1485 case RTX_COMM_COMPARE
:
1486 if (CONST_INT_P (XEXP (copy
, 0))
1487 && GET_MODE (XEXP (copy
, 1)) == VOIDmode
1488 && (GET_MODE (XEXP (orig
, 0)) != VOIDmode
1489 || GET_MODE (XEXP (orig
, 1)) != VOIDmode
))
1491 scopy
= simplify_relational_operation (code
, mode
,
1492 (GET_MODE (XEXP (orig
, 0))
1494 ? GET_MODE (XEXP (orig
, 0))
1495 : GET_MODE (XEXP (orig
, 1)),
1505 scopy
= simplify_rtx (copy
);
1511 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1512 with VALUE expressions. This way, it becomes independent of changes
1513 to registers and memory.
1514 X isn't actually modified; if modifications are needed, new rtl is
1515 allocated. However, the return value can share rtl with X. */
1518 cselib_subst_to_values (rtx x
)
1520 enum rtx_code code
= GET_CODE (x
);
1521 const char *fmt
= GET_RTX_FORMAT (code
);
1530 l
= REG_VALUES (REGNO (x
));
1531 if (l
&& l
->elt
== NULL
)
1533 for (; l
; l
= l
->next
)
1534 if (GET_MODE (l
->elt
->val_rtx
) == GET_MODE (x
))
1535 return l
->elt
->val_rtx
;
1540 e
= cselib_lookup_mem (x
, 0);
1543 /* This happens for autoincrements. Assign a value that doesn't
1545 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
1561 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
1568 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1572 rtx t
= cselib_subst_to_values (XEXP (x
, i
));
1574 if (t
!= XEXP (x
, i
))
1577 copy
= shallow_copy_rtx (x
);
1581 else if (fmt
[i
] == 'E')
1585 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1587 rtx t
= cselib_subst_to_values (XVECEXP (x
, i
, j
));
1589 if (t
!= XVECEXP (x
, i
, j
))
1591 if (XVEC (x
, i
) == XVEC (copy
, i
))
1594 copy
= shallow_copy_rtx (x
);
1595 XVEC (copy
, i
) = shallow_copy_rtvec (XVEC (x
, i
));
1597 XVECEXP (copy
, i
, j
) = t
;
1606 /* Look up the rtl expression X in our tables and return the value it has.
1607 If CREATE is zero, we return NULL if we don't know the value. Otherwise,
1608 we create a new one if possible, using mode MODE if X doesn't have a mode
1609 (i.e. because it's a constant). */
1612 cselib_lookup_1 (rtx x
, enum machine_mode mode
, int create
)
1616 unsigned int hashval
;
1618 if (GET_MODE (x
) != VOIDmode
)
1619 mode
= GET_MODE (x
);
1621 if (GET_CODE (x
) == VALUE
)
1622 return CSELIB_VAL_PTR (x
);
1627 unsigned int i
= REGNO (x
);
1630 if (l
&& l
->elt
== NULL
)
1632 for (; l
; l
= l
->next
)
1633 if (mode
== GET_MODE (l
->elt
->val_rtx
))
1635 promote_debug_loc (l
->elt
->locs
);
1642 if (i
< FIRST_PSEUDO_REGISTER
)
1644 unsigned int n
= hard_regno_nregs
[i
][mode
];
1646 if (n
> max_value_regs
)
1650 e
= new_cselib_val (next_uid
, GET_MODE (x
), x
);
1651 e
->locs
= new_elt_loc_list (e
->locs
, x
);
1652 if (REG_VALUES (i
) == 0)
1654 /* Maintain the invariant that the first entry of
1655 REG_VALUES, if present, must be the value used to set the
1656 register, or NULL. */
1657 used_regs
[n_used_regs
++] = i
;
1658 REG_VALUES (i
) = new_elt_list (REG_VALUES (i
), NULL
);
1660 REG_VALUES (i
)->next
= new_elt_list (REG_VALUES (i
)->next
, e
);
1661 slot
= htab_find_slot_with_hash (cselib_hash_table
, x
, e
->hash
, INSERT
);
1667 return cselib_lookup_mem (x
, create
);
1669 hashval
= cselib_hash_rtx (x
, create
);
1670 /* Can't even create if hashing is not possible. */
1674 slot
= htab_find_slot_with_hash (cselib_hash_table
, wrap_constant (mode
, x
),
1675 hashval
, create
? INSERT
: NO_INSERT
);
1679 e
= (cselib_val
*) *slot
;
1683 e
= new_cselib_val (hashval
, mode
, x
);
1685 /* We have to fill the slot before calling cselib_subst_to_values:
1686 the hash table is inconsistent until we do so, and
1687 cselib_subst_to_values will need to do lookups. */
1689 e
->locs
= new_elt_loc_list (e
->locs
, cselib_subst_to_values (x
));
1693 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
1696 cselib_lookup_from_insn (rtx x
, enum machine_mode mode
,
1697 int create
, rtx insn
)
1701 gcc_assert (!cselib_current_insn
);
1702 cselib_current_insn
= insn
;
1704 ret
= cselib_lookup (x
, mode
, create
);
1706 cselib_current_insn
= NULL
;
1711 /* Wrapper for cselib_lookup_1, that logs the lookup result and
1712 maintains invariants related with debug insns. */
1715 cselib_lookup (rtx x
, enum machine_mode mode
, int create
)
1717 cselib_val
*ret
= cselib_lookup_1 (x
, mode
, create
);
1719 /* ??? Should we return NULL if we're not to create an entry, the
1720 found loc is a debug loc and cselib_current_insn is not DEBUG?
1721 If so, we should also avoid converting val to non-DEBUG; probably
1722 easiest setting cselib_current_insn to NULL before the call
1725 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1727 fputs ("cselib lookup ", dump_file
);
1728 print_inline_rtx (dump_file
, x
, 2);
1729 fprintf (dump_file
, " => %u:%u\n",
1731 ret
? ret
->hash
: 0);
1737 /* Invalidate any entries in reg_values that overlap REGNO. This is called
1738 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
1739 is used to determine how many hard registers are being changed. If MODE
1740 is VOIDmode, then only REGNO is being changed; this is used when
1741 invalidating call clobbered registers across a call. */
1744 cselib_invalidate_regno (unsigned int regno
, enum machine_mode mode
)
1746 unsigned int endregno
;
1749 /* If we see pseudos after reload, something is _wrong_. */
1750 gcc_assert (!reload_completed
|| regno
< FIRST_PSEUDO_REGISTER
1751 || reg_renumber
[regno
] < 0);
1753 /* Determine the range of registers that must be invalidated. For
1754 pseudos, only REGNO is affected. For hard regs, we must take MODE
1755 into account, and we must also invalidate lower register numbers
1756 if they contain values that overlap REGNO. */
1757 if (regno
< FIRST_PSEUDO_REGISTER
)
1759 gcc_assert (mode
!= VOIDmode
);
1761 if (regno
< max_value_regs
)
1764 i
= regno
- max_value_regs
;
1766 endregno
= end_hard_regno (mode
, regno
);
1771 endregno
= regno
+ 1;
1774 for (; i
< endregno
; i
++)
1776 struct elt_list
**l
= ®_VALUES (i
);
1778 /* Go through all known values for this reg; if it overlaps the range
1779 we're invalidating, remove the value. */
1782 cselib_val
*v
= (*l
)->elt
;
1785 struct elt_loc_list
**p
;
1786 unsigned int this_last
= i
;
1788 if (i
< FIRST_PSEUDO_REGISTER
&& v
!= NULL
)
1789 this_last
= end_hard_regno (GET_MODE (v
->val_rtx
), i
) - 1;
1791 if (this_last
< regno
|| v
== NULL
1792 || (v
== cfa_base_preserved_val
1793 && i
== cfa_base_preserved_regno
))
1799 /* We have an overlap. */
1800 if (*l
== REG_VALUES (i
))
1802 /* Maintain the invariant that the first entry of
1803 REG_VALUES, if present, must be the value used to set
1804 the register, or NULL. This is also nice because
1805 then we won't push the same regno onto user_regs
1811 unchain_one_elt_list (l
);
1813 had_locs
= v
->locs
!= NULL
;
1814 setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
1816 /* Now, we clear the mapping from value to reg. It must exist, so
1817 this code will crash intentionally if it doesn't. */
1818 for (p
= &v
->locs
; ; p
= &(*p
)->next
)
1822 if (REG_P (x
) && REGNO (x
) == i
)
1824 unchain_one_elt_loc_list (p
);
1829 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
1831 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
1832 n_useless_debug_values
++;
1840 /* Return 1 if X has a value that can vary even between two
1841 executions of the program. 0 means X can be compared reliably
1842 against certain constants or near-constants. */
1845 cselib_rtx_varies_p (const_rtx x ATTRIBUTE_UNUSED
, bool from_alias ATTRIBUTE_UNUSED
)
1847 /* We actually don't need to verify very hard. This is because
1848 if X has actually changed, we invalidate the memory anyway,
1849 so assume that all common memory addresses are
1854 /* Invalidate any locations in the table which are changed because of a
1855 store to MEM_RTX. If this is called because of a non-const call
1856 instruction, MEM_RTX is (mem:BLK const0_rtx). */
1859 cselib_invalidate_mem (rtx mem_rtx
)
1861 cselib_val
**vp
, *v
, *next
;
1865 mem_addr
= canon_rtx (get_addr (XEXP (mem_rtx
, 0)));
1866 mem_rtx
= canon_rtx (mem_rtx
);
1868 vp
= &first_containing_mem
;
1869 for (v
= *vp
; v
!= &dummy_val
; v
= next
)
1871 bool has_mem
= false;
1872 struct elt_loc_list
**p
= &v
->locs
;
1873 bool had_locs
= v
->locs
!= NULL
;
1874 rtx setting_insn
= v
->locs
? v
->locs
->setting_insn
: NULL
;
1880 struct elt_list
**mem_chain
;
1882 /* MEMs may occur in locations only at the top level; below
1883 that every MEM or REG is substituted by its VALUE. */
1889 if (num_mems
< PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS
)
1890 && ! canon_true_dependence (mem_rtx
, GET_MODE (mem_rtx
), mem_addr
,
1891 x
, NULL_RTX
, cselib_rtx_varies_p
))
1899 /* This one overlaps. */
1900 /* We must have a mapping from this MEM's address to the
1901 value (E). Remove that, too. */
1902 addr
= cselib_lookup (XEXP (x
, 0), VOIDmode
, 0);
1903 mem_chain
= &addr
->addr_list
;
1906 if ((*mem_chain
)->elt
== v
)
1908 unchain_one_elt_list (mem_chain
);
1912 mem_chain
= &(*mem_chain
)->next
;
1915 unchain_one_elt_loc_list (p
);
1918 if (had_locs
&& v
->locs
== 0 && !PRESERVED_VALUE_P (v
->val_rtx
))
1920 if (setting_insn
&& DEBUG_INSN_P (setting_insn
))
1921 n_useless_debug_values
++;
1926 next
= v
->next_containing_mem
;
1930 vp
= &(*vp
)->next_containing_mem
;
1933 v
->next_containing_mem
= NULL
;
1938 /* Invalidate DEST, which is being assigned to or clobbered. */
1941 cselib_invalidate_rtx (rtx dest
)
1943 while (GET_CODE (dest
) == SUBREG
1944 || GET_CODE (dest
) == ZERO_EXTRACT
1945 || GET_CODE (dest
) == STRICT_LOW_PART
)
1946 dest
= XEXP (dest
, 0);
1949 cselib_invalidate_regno (REGNO (dest
), GET_MODE (dest
));
1950 else if (MEM_P (dest
))
1951 cselib_invalidate_mem (dest
);
1953 /* Some machines don't define AUTO_INC_DEC, but they still use push
1954 instructions. We need to catch that case here in order to
1955 invalidate the stack pointer correctly. Note that invalidating
1956 the stack pointer is different from invalidating DEST. */
1957 if (push_operand (dest
, GET_MODE (dest
)))
1958 cselib_invalidate_rtx (stack_pointer_rtx
);
1961 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
1964 cselib_invalidate_rtx_note_stores (rtx dest
, const_rtx ignore ATTRIBUTE_UNUSED
,
1965 void *data ATTRIBUTE_UNUSED
)
1967 cselib_invalidate_rtx (dest
);
1970 /* Record the result of a SET instruction. DEST is being set; the source
1971 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
1972 describes its address. */
1975 cselib_record_set (rtx dest
, cselib_val
*src_elt
, cselib_val
*dest_addr_elt
)
1977 int dreg
= REG_P (dest
) ? (int) REGNO (dest
) : -1;
1979 if (src_elt
== 0 || side_effects_p (dest
))
1984 if (dreg
< FIRST_PSEUDO_REGISTER
)
1986 unsigned int n
= hard_regno_nregs
[dreg
][GET_MODE (dest
)];
1988 if (n
> max_value_regs
)
1992 if (REG_VALUES (dreg
) == 0)
1994 used_regs
[n_used_regs
++] = dreg
;
1995 REG_VALUES (dreg
) = new_elt_list (REG_VALUES (dreg
), src_elt
);
1999 /* The register should have been invalidated. */
2000 gcc_assert (REG_VALUES (dreg
)->elt
== 0);
2001 REG_VALUES (dreg
)->elt
= src_elt
;
2004 if (src_elt
->locs
== 0 && !PRESERVED_VALUE_P (src_elt
->val_rtx
))
2006 src_elt
->locs
= new_elt_loc_list (src_elt
->locs
, dest
);
2008 else if (MEM_P (dest
) && dest_addr_elt
!= 0
2009 && cselib_record_memory
)
2011 if (src_elt
->locs
== 0 && !PRESERVED_VALUE_P (src_elt
->val_rtx
))
2013 add_mem_for_addr (dest_addr_elt
, src_elt
, dest
);
2017 /* There is no good way to determine how many elements there can be
2018 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2019 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2021 /* Record the effects of any sets in INSN. */
2023 cselib_record_sets (rtx insn
)
2027 struct cselib_set sets
[MAX_SETS
];
2028 rtx body
= PATTERN (insn
);
2031 body
= PATTERN (insn
);
2032 if (GET_CODE (body
) == COND_EXEC
)
2034 cond
= COND_EXEC_TEST (body
);
2035 body
= COND_EXEC_CODE (body
);
2038 /* Find all sets. */
2039 if (GET_CODE (body
) == SET
)
2041 sets
[0].src
= SET_SRC (body
);
2042 sets
[0].dest
= SET_DEST (body
);
2045 else if (GET_CODE (body
) == PARALLEL
)
2047 /* Look through the PARALLEL and record the values being
2048 set, if possible. Also handle any CLOBBERs. */
2049 for (i
= XVECLEN (body
, 0) - 1; i
>= 0; --i
)
2051 rtx x
= XVECEXP (body
, 0, i
);
2053 if (GET_CODE (x
) == SET
)
2055 sets
[n_sets
].src
= SET_SRC (x
);
2056 sets
[n_sets
].dest
= SET_DEST (x
);
2063 && MEM_P (sets
[0].src
)
2064 && !cselib_record_memory
2065 && MEM_READONLY_P (sets
[0].src
))
2067 rtx note
= find_reg_equal_equiv_note (insn
);
2069 if (note
&& CONSTANT_P (XEXP (note
, 0)))
2070 sets
[0].src
= XEXP (note
, 0);
2073 /* Look up the values that are read. Do this before invalidating the
2074 locations that are written. */
2075 for (i
= 0; i
< n_sets
; i
++)
2077 rtx dest
= sets
[i
].dest
;
2079 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2080 the low part after invalidating any knowledge about larger modes. */
2081 if (GET_CODE (sets
[i
].dest
) == STRICT_LOW_PART
)
2082 sets
[i
].dest
= dest
= XEXP (dest
, 0);
2084 /* We don't know how to record anything but REG or MEM. */
2086 || (MEM_P (dest
) && cselib_record_memory
))
2088 rtx src
= sets
[i
].src
;
2090 src
= gen_rtx_IF_THEN_ELSE (GET_MODE (dest
), cond
, src
, dest
);
2091 sets
[i
].src_elt
= cselib_lookup (src
, GET_MODE (dest
), 1);
2094 enum machine_mode address_mode
2095 = targetm
.addr_space
.address_mode (MEM_ADDR_SPACE (dest
));
2097 sets
[i
].dest_addr_elt
= cselib_lookup (XEXP (dest
, 0),
2101 sets
[i
].dest_addr_elt
= 0;
2105 if (cselib_record_sets_hook
)
2106 cselib_record_sets_hook (insn
, sets
, n_sets
);
2108 /* Invalidate all locations written by this insn. Note that the elts we
2109 looked up in the previous loop aren't affected, just some of their
2110 locations may go away. */
2111 note_stores (body
, cselib_invalidate_rtx_note_stores
, NULL
);
2113 /* If this is an asm, look for duplicate sets. This can happen when the
2114 user uses the same value as an output multiple times. This is valid
2115 if the outputs are not actually used thereafter. Treat this case as
2116 if the value isn't actually set. We do this by smashing the destination
2117 to pc_rtx, so that we won't record the value later. */
2118 if (n_sets
>= 2 && asm_noperands (body
) >= 0)
2120 for (i
= 0; i
< n_sets
; i
++)
2122 rtx dest
= sets
[i
].dest
;
2123 if (REG_P (dest
) || MEM_P (dest
))
2126 for (j
= i
+ 1; j
< n_sets
; j
++)
2127 if (rtx_equal_p (dest
, sets
[j
].dest
))
2129 sets
[i
].dest
= pc_rtx
;
2130 sets
[j
].dest
= pc_rtx
;
2136 /* Now enter the equivalences in our tables. */
2137 for (i
= 0; i
< n_sets
; i
++)
2139 rtx dest
= sets
[i
].dest
;
2141 || (MEM_P (dest
) && cselib_record_memory
))
2142 cselib_record_set (dest
, sets
[i
].src_elt
, sets
[i
].dest_addr_elt
);
2146 /* Record the effects of INSN. */
2149 cselib_process_insn (rtx insn
)
2154 cselib_current_insn
= insn
;
2156 /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp. */
2159 && find_reg_note (insn
, REG_SETJMP
, NULL
))
2160 || (NONJUMP_INSN_P (insn
)
2161 && GET_CODE (PATTERN (insn
)) == ASM_OPERANDS
2162 && MEM_VOLATILE_P (PATTERN (insn
))))
2164 cselib_reset_table (next_uid
);
2165 cselib_current_insn
= NULL_RTX
;
2169 if (! INSN_P (insn
))
2171 cselib_current_insn
= NULL_RTX
;
2175 /* If this is a call instruction, forget anything stored in a
2176 call clobbered register, or, if this is not a const call, in
2180 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
2181 if (call_used_regs
[i
]
2182 || (REG_VALUES (i
) && REG_VALUES (i
)->elt
2183 && HARD_REGNO_CALL_PART_CLOBBERED (i
,
2184 GET_MODE (REG_VALUES (i
)->elt
->val_rtx
))))
2185 cselib_invalidate_regno (i
, reg_raw_mode
[i
]);
2187 /* Since it is not clear how cselib is going to be used, be
2188 conservative here and treat looping pure or const functions
2189 as if they were regular functions. */
2190 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn
)
2191 || !(RTL_CONST_OR_PURE_CALL_P (insn
)))
2192 cselib_invalidate_mem (callmem
);
2195 cselib_record_sets (insn
);
2198 /* Clobber any registers which appear in REG_INC notes. We
2199 could keep track of the changes to their values, but it is
2200 unlikely to help. */
2201 for (x
= REG_NOTES (insn
); x
; x
= XEXP (x
, 1))
2202 if (REG_NOTE_KIND (x
) == REG_INC
)
2203 cselib_invalidate_rtx (XEXP (x
, 0));
2206 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2207 after we have processed the insn. */
2209 for (x
= CALL_INSN_FUNCTION_USAGE (insn
); x
; x
= XEXP (x
, 1))
2210 if (GET_CODE (XEXP (x
, 0)) == CLOBBER
)
2211 cselib_invalidate_rtx (XEXP (XEXP (x
, 0), 0));
2213 cselib_current_insn
= NULL_RTX
;
2215 if (n_useless_values
> MAX_USELESS_VALUES
2216 /* remove_useless_values is linear in the hash table size. Avoid
2217 quadratic behavior for very large hashtables with very few
2218 useless elements. */
2219 && ((unsigned int)n_useless_values
2220 > (cselib_hash_table
->n_elements
2221 - cselib_hash_table
->n_deleted
2222 - n_debug_values
) / 4))
2223 remove_useless_values ();
2226 /* Initialize cselib for one pass. The caller must also call
2227 init_alias_analysis. */
2230 cselib_init (int record_what
)
2232 elt_list_pool
= create_alloc_pool ("elt_list",
2233 sizeof (struct elt_list
), 10);
2234 elt_loc_list_pool
= create_alloc_pool ("elt_loc_list",
2235 sizeof (struct elt_loc_list
), 10);
2236 cselib_val_pool
= create_alloc_pool ("cselib_val_list",
2237 sizeof (cselib_val
), 10);
2238 value_pool
= create_alloc_pool ("value", RTX_CODE_SIZE (VALUE
), 100);
2239 cselib_record_memory
= record_what
& CSELIB_RECORD_MEMORY
;
2240 cselib_preserve_constants
= record_what
& CSELIB_PRESERVE_CONSTANTS
;
2242 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2243 see canon_true_dependence. This is only created once. */
2245 callmem
= gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
));
2247 cselib_nregs
= max_reg_num ();
2249 /* We preserve reg_values to allow expensive clearing of the whole thing.
2250 Reallocate it however if it happens to be too large. */
2251 if (!reg_values
|| reg_values_size
< cselib_nregs
2252 || (reg_values_size
> 10 && reg_values_size
> cselib_nregs
* 4))
2256 /* Some space for newly emit instructions so we don't end up
2257 reallocating in between passes. */
2258 reg_values_size
= cselib_nregs
+ (63 + cselib_nregs
) / 16;
2259 reg_values
= XCNEWVEC (struct elt_list
*, reg_values_size
);
2261 used_regs
= XNEWVEC (unsigned int, cselib_nregs
);
2263 cselib_hash_table
= htab_create (31, get_value_hash
,
2264 entry_and_rtx_equal_p
, NULL
);
2268 /* Called when the current user is done with cselib. */
2271 cselib_finish (void)
2273 cselib_discard_hook
= NULL
;
2274 cselib_preserve_constants
= false;
2275 cfa_base_preserved_val
= NULL
;
2276 cfa_base_preserved_regno
= INVALID_REGNUM
;
2277 free_alloc_pool (elt_list_pool
);
2278 free_alloc_pool (elt_loc_list_pool
);
2279 free_alloc_pool (cselib_val_pool
);
2280 free_alloc_pool (value_pool
);
2281 cselib_clear_table ();
2282 htab_delete (cselib_hash_table
);
2285 cselib_hash_table
= 0;
2286 n_useless_values
= 0;
2287 n_useless_debug_values
= 0;
2292 /* Dump the cselib_val *X to FILE *info. */
2295 dump_cselib_val (void **x
, void *info
)
2297 cselib_val
*v
= (cselib_val
*)*x
;
2298 FILE *out
= (FILE *)info
;
2299 bool need_lf
= true;
2301 print_inline_rtx (out
, v
->val_rtx
, 0);
2305 struct elt_loc_list
*l
= v
->locs
;
2311 fputs (" locs:", out
);
2314 fprintf (out
, "\n from insn %i ",
2315 INSN_UID (l
->setting_insn
));
2316 print_inline_rtx (out
, l
->loc
, 4);
2318 while ((l
= l
->next
));
2323 fputs (" no locs", out
);
2329 struct elt_list
*e
= v
->addr_list
;
2335 fputs (" addr list:", out
);
2339 print_inline_rtx (out
, e
->elt
->val_rtx
, 2);
2341 while ((e
= e
->next
));
2346 fputs (" no addrs", out
);
2350 if (v
->next_containing_mem
== &dummy_val
)
2351 fputs (" last mem\n", out
);
2352 else if (v
->next_containing_mem
)
2354 fputs (" next mem ", out
);
2355 print_inline_rtx (out
, v
->next_containing_mem
->val_rtx
, 2);
2364 /* Dump to OUT everything in the CSELIB table. */
2367 dump_cselib_table (FILE *out
)
2369 fprintf (out
, "cselib hash table:\n");
2370 htab_traverse (cselib_hash_table
, dump_cselib_val
, out
);
2371 if (first_containing_mem
!= &dummy_val
)
2373 fputs ("first mem ", out
);
2374 print_inline_rtx (out
, first_containing_mem
->val_rtx
, 2);
2377 fprintf (out
, "next uid %i\n", next_uid
);
2380 #include "gt-cselib.h"