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"
40 #include "tree-pass.h"
43 #include "alloc-pool.h"
46 static bool cselib_record_memory;
47 static bool cselib_preserve_constants;
48 static int entry_and_rtx_equal_p (const void *, const void *);
49 static hashval_t get_value_hash (const void *);
50 static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
51 static struct elt_loc_list *new_elt_loc_list (struct elt_loc_list *, rtx);
52 static void unchain_one_value (cselib_val *);
53 static void unchain_one_elt_list (struct elt_list **);
54 static void unchain_one_elt_loc_list (struct elt_loc_list **);
55 static int discard_useless_locs (void **, void *);
56 static int discard_useless_values (void **, void *);
57 static void remove_useless_values (void);
58 static unsigned int cselib_hash_rtx (rtx, int);
59 static cselib_val *new_cselib_val (unsigned int, enum machine_mode, rtx);
60 static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
61 static cselib_val *cselib_lookup_mem (rtx, int);
62 static void cselib_invalidate_regno (unsigned int, enum machine_mode);
63 static void cselib_invalidate_mem (rtx);
64 static void cselib_record_set (rtx, cselib_val *, cselib_val *);
65 static void cselib_record_sets (rtx);
67 struct expand_value_data
70 cselib_expand_callback callback;
75 static rtx cselib_expand_value_rtx_1 (rtx, struct expand_value_data *, int);
77 /* There are three ways in which cselib can look up an rtx:
78 - for a REG, the reg_values table (which is indexed by regno) is used
79 - for a MEM, we recursively look up its address and then follow the
80 addr_list of that value
81 - for everything else, we compute a hash value and go through the hash
82 table. Since different rtx's can still have the same hash value,
83 this involves walking the table entries for a given value and comparing
84 the locations of the entries with the rtx we are looking up. */
86 /* A table that enables us to look up elts by their value. */
87 static htab_t cselib_hash_table;
89 /* This is a global so we don't have to pass this through every function.
90 It is used in new_elt_loc_list to set SETTING_INSN. */
91 static rtx cselib_current_insn;
93 /* The unique id that the next create value will take. */
94 static unsigned int next_uid;
96 /* The number of registers we had when the varrays were last resized. */
97 static unsigned int cselib_nregs;
99 /* Count values without known locations, or with only locations that
100 wouldn't have been known except for debug insns. Whenever this
101 grows too big, we remove these useless values from the table.
103 Counting values with only debug values is a bit tricky. We don't
104 want to increment n_useless_values when we create a value for a
105 debug insn, for this would get n_useless_values out of sync, but we
106 want increment it if all locs in the list that were ever referenced
107 in nondebug insns are removed from the list.
109 In the general case, once we do that, we'd have to stop accepting
110 nondebug expressions in the loc list, to avoid having two values
111 equivalent that, without debug insns, would have been made into
112 separate values. However, because debug insns never introduce
113 equivalences themselves (no assignments), the only means for
114 growing loc lists is through nondebug assignments. If the locs
115 also happen to be referenced in debug insns, it will work just fine.
117 A consequence of this is that there's at most one debug-only loc in
118 each loc list. If we keep it in the first entry, testing whether
119 we have a debug-only loc list takes O(1).
121 Furthermore, since any additional entry in a loc list containing a
122 debug loc would have to come from an assignment (nondebug) that
123 references both the initial debug loc and the newly-equivalent loc,
124 the initial debug loc would be promoted to a nondebug loc, and the
125 loc list would not contain debug locs any more.
127 So the only case we have to be careful with in order to keep
128 n_useless_values in sync between debug and nondebug compilations is
129 to avoid incrementing n_useless_values when removing the single loc
130 from a value that turns out to not appear outside debug values. We
131 increment n_useless_debug_values instead, and leave such values
132 alone until, for other reasons, we garbage-collect useless
134 static int n_useless_values;
135 static int n_useless_debug_values;
137 /* Count values whose locs have been taken exclusively from debug
138 insns for the entire life of the value. */
139 static int n_debug_values;
141 /* Number of useless values before we remove them from the hash table. */
142 #define MAX_USELESS_VALUES 32
144 /* This table maps from register number to values. It does not
145 contain pointers to cselib_val structures, but rather elt_lists.
146 The purpose is to be able to refer to the same register in
147 different modes. The first element of the list defines the mode in
148 which the register was set; if the mode is unknown or the value is
149 no longer valid in that mode, ELT will be NULL for the first
151 static struct elt_list **reg_values;
152 static unsigned int reg_values_size;
153 #define REG_VALUES(i) reg_values[i]
155 /* The largest number of hard regs used by any entry added to the
156 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
157 static unsigned int max_value_regs;
159 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
160 in cselib_clear_table() for fast emptying. */
161 static unsigned int *used_regs;
162 static unsigned int n_used_regs;
164 /* We pass this to cselib_invalidate_mem to invalidate all of
165 memory for a non-const call instruction. */
166 static GTY(()) rtx callmem;
168 /* Set by discard_useless_locs if it deleted the last location of any
170 static int values_became_useless;
172 /* Used as stop element of the containing_mem list so we can check
173 presence in the list by checking the next pointer. */
174 static cselib_val dummy_val;
176 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
177 that is constant through the whole function and should never be
179 static cselib_val *cfa_base_preserved_val;
181 /* Used to list all values that contain memory reference.
182 May or may not contain the useless values - the list is compacted
183 each time memory is invalidated. */
184 static cselib_val *first_containing_mem = &dummy_val;
185 static alloc_pool elt_loc_list_pool, elt_list_pool, cselib_val_pool, value_pool;
187 /* If nonnull, cselib will call this function before freeing useless
188 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
189 void (*cselib_discard_hook) (cselib_val *);
191 /* If nonnull, cselib will call this function before recording sets or
192 even clobbering outputs of INSN. All the recorded sets will be
193 represented in the array sets[n_sets]. new_val_min can be used to
194 tell whether values present in sets are introduced by this
196 void (*cselib_record_sets_hook) (rtx insn, struct cselib_set *sets,
199 #define PRESERVED_VALUE_P(RTX) \
200 (RTL_FLAG_CHECK1("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
204 /* Allocate a struct elt_list and fill in its two elements with the
207 static inline struct elt_list *
208 new_elt_list (struct elt_list *next, cselib_val *elt)
211 el = (struct elt_list *) pool_alloc (elt_list_pool);
217 /* Allocate a struct elt_loc_list and fill in its two elements with the
220 static inline struct elt_loc_list *
221 new_elt_loc_list (struct elt_loc_list *next, rtx loc)
223 struct elt_loc_list *el;
224 el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
227 el->setting_insn = cselib_current_insn;
228 gcc_assert (!next || !next->setting_insn
229 || !DEBUG_INSN_P (next->setting_insn));
231 /* If we're creating the first loc in a debug insn context, we've
232 just created a debug value. Count it. */
233 if (!next && cselib_current_insn && DEBUG_INSN_P (cselib_current_insn))
239 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
240 originating from a debug insn, maintaining the debug values
244 promote_debug_loc (struct elt_loc_list *l)
246 if (l->setting_insn && DEBUG_INSN_P (l->setting_insn)
247 && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
250 l->setting_insn = cselib_current_insn;
251 gcc_assert (!l->next);
255 /* The elt_list at *PL is no longer needed. Unchain it and free its
259 unchain_one_elt_list (struct elt_list **pl)
261 struct elt_list *l = *pl;
264 pool_free (elt_list_pool, l);
267 /* Likewise for elt_loc_lists. */
270 unchain_one_elt_loc_list (struct elt_loc_list **pl)
272 struct elt_loc_list *l = *pl;
275 pool_free (elt_loc_list_pool, l);
278 /* Likewise for cselib_vals. This also frees the addr_list associated with
282 unchain_one_value (cselib_val *v)
285 unchain_one_elt_list (&v->addr_list);
287 pool_free (cselib_val_pool, v);
290 /* Remove all entries from the hash table. Also used during
294 cselib_clear_table (void)
296 cselib_reset_table (1);
299 /* Remove from hash table all VALUEs except constants. */
302 preserve_only_constants (void **x, void *info ATTRIBUTE_UNUSED)
304 cselib_val *v = (cselib_val *)*x;
307 && v->locs->next == NULL)
309 if (CONSTANT_P (v->locs->loc)
310 && (GET_CODE (v->locs->loc) != CONST
311 || !references_value_p (v->locs->loc, 0)))
313 if (cfa_base_preserved_val)
315 if (v == cfa_base_preserved_val)
317 if (GET_CODE (v->locs->loc) == PLUS
318 && CONST_INT_P (XEXP (v->locs->loc, 1))
319 && XEXP (v->locs->loc, 0) == cfa_base_preserved_val->val_rtx)
324 htab_clear_slot (cselib_hash_table, x);
328 /* Remove all entries from the hash table, arranging for the next
329 value to be numbered NUM. */
332 cselib_reset_table (unsigned int num)
338 if (cfa_base_preserved_val)
340 unsigned int regno = REGNO (cfa_base_preserved_val->locs->loc);
341 unsigned int new_used_regs = 0;
342 for (i = 0; i < n_used_regs; i++)
343 if (used_regs[i] == regno)
349 REG_VALUES (used_regs[i]) = 0;
350 gcc_assert (new_used_regs == 1);
351 n_used_regs = new_used_regs;
352 used_regs[0] = regno;
354 = hard_regno_nregs[regno][GET_MODE (cfa_base_preserved_val->locs->loc)];
358 for (i = 0; i < n_used_regs; i++)
359 REG_VALUES (used_regs[i]) = 0;
363 if (cselib_preserve_constants)
364 htab_traverse (cselib_hash_table, preserve_only_constants, NULL);
366 htab_empty (cselib_hash_table);
368 n_useless_values = 0;
369 n_useless_debug_values = 0;
374 first_containing_mem = &dummy_val;
377 /* Return the number of the next value that will be generated. */
380 cselib_get_next_uid (void)
385 /* The equality test for our hash table. The first argument ENTRY is a table
386 element (i.e. a cselib_val), while the second arg X is an rtx. We know
387 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
388 CONST of an appropriate mode. */
391 entry_and_rtx_equal_p (const void *entry, const void *x_arg)
393 struct elt_loc_list *l;
394 const cselib_val *const v = (const cselib_val *) entry;
395 rtx x = CONST_CAST_RTX ((const_rtx)x_arg);
396 enum machine_mode mode = GET_MODE (x);
398 gcc_assert (!CONST_INT_P (x) && GET_CODE (x) != CONST_FIXED
399 && (mode != VOIDmode || GET_CODE (x) != CONST_DOUBLE));
401 if (mode != GET_MODE (v->val_rtx))
404 /* Unwrap X if necessary. */
405 if (GET_CODE (x) == CONST
406 && (CONST_INT_P (XEXP (x, 0))
407 || GET_CODE (XEXP (x, 0)) == CONST_FIXED
408 || GET_CODE (XEXP (x, 0)) == CONST_DOUBLE))
411 /* We don't guarantee that distinct rtx's have different hash values,
412 so we need to do a comparison. */
413 for (l = v->locs; l; l = l->next)
414 if (rtx_equal_for_cselib_p (l->loc, x))
416 promote_debug_loc (l);
423 /* The hash function for our hash table. The value is always computed with
424 cselib_hash_rtx when adding an element; this function just extracts the
425 hash value from a cselib_val structure. */
428 get_value_hash (const void *entry)
430 const cselib_val *const v = (const cselib_val *) entry;
434 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
435 only return true for values which point to a cselib_val whose value
436 element has been set to zero, which implies the cselib_val will be
440 references_value_p (const_rtx x, int only_useless)
442 const enum rtx_code code = GET_CODE (x);
443 const char *fmt = GET_RTX_FORMAT (code);
446 if (GET_CODE (x) == VALUE
447 && (! only_useless || CSELIB_VAL_PTR (x)->locs == 0))
450 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
452 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
454 else if (fmt[i] == 'E')
455 for (j = 0; j < XVECLEN (x, i); j++)
456 if (references_value_p (XVECEXP (x, i, j), only_useless))
463 /* For all locations found in X, delete locations that reference useless
464 values (i.e. values without any location). Called through
468 discard_useless_locs (void **x, void *info ATTRIBUTE_UNUSED)
470 cselib_val *v = (cselib_val *)*x;
471 struct elt_loc_list **p = &v->locs;
472 bool had_locs = v->locs != NULL;
473 rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
477 if (references_value_p ((*p)->loc, 1))
478 unchain_one_elt_loc_list (p);
483 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
485 if (setting_insn && DEBUG_INSN_P (setting_insn))
486 n_useless_debug_values++;
489 values_became_useless = 1;
494 /* If X is a value with no locations, remove it from the hashtable. */
497 discard_useless_values (void **x, void *info ATTRIBUTE_UNUSED)
499 cselib_val *v = (cselib_val *)*x;
501 if (v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
503 if (cselib_discard_hook)
504 cselib_discard_hook (v);
506 CSELIB_VAL_PTR (v->val_rtx) = NULL;
507 htab_clear_slot (cselib_hash_table, x);
508 unchain_one_value (v);
515 /* Clean out useless values (i.e. those which no longer have locations
516 associated with them) from the hash table. */
519 remove_useless_values (void)
523 /* First pass: eliminate locations that reference the value. That in
524 turn can make more values useless. */
527 values_became_useless = 0;
528 htab_traverse (cselib_hash_table, discard_useless_locs, 0);
530 while (values_became_useless);
532 /* Second pass: actually remove the values. */
534 p = &first_containing_mem;
535 for (v = *p; v != &dummy_val; v = v->next_containing_mem)
539 p = &(*p)->next_containing_mem;
543 n_useless_values += n_useless_debug_values;
544 n_debug_values -= n_useless_debug_values;
545 n_useless_debug_values = 0;
547 htab_traverse (cselib_hash_table, discard_useless_values, 0);
549 gcc_assert (!n_useless_values);
552 /* Arrange for a value to not be removed from the hash table even if
553 it becomes useless. */
556 cselib_preserve_value (cselib_val *v)
558 PRESERVED_VALUE_P (v->val_rtx) = 1;
561 /* Test whether a value is preserved. */
564 cselib_preserved_value_p (cselib_val *v)
566 return PRESERVED_VALUE_P (v->val_rtx);
569 /* Arrange for a REG value to be assumed constant through the whole function,
570 never invalidated and preserved across cselib_reset_table calls. */
573 cselib_preserve_cfa_base_value (cselib_val *v)
575 if (cselib_preserve_constants
577 && REG_P (v->locs->loc))
578 cfa_base_preserved_val = v;
581 /* Clean all non-constant expressions in the hash table, but retain
585 cselib_preserve_only_values (void)
589 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
590 cselib_invalidate_regno (i, reg_raw_mode[i]);
592 cselib_invalidate_mem (callmem);
594 remove_useless_values ();
596 gcc_assert (first_containing_mem == &dummy_val);
599 /* Return the mode in which a register was last set. If X is not a
600 register, return its mode. If the mode in which the register was
601 set is not known, or the value was already clobbered, return
605 cselib_reg_set_mode (const_rtx x)
610 if (REG_VALUES (REGNO (x)) == NULL
611 || REG_VALUES (REGNO (x))->elt == NULL)
614 return GET_MODE (REG_VALUES (REGNO (x))->elt->val_rtx);
617 /* Return nonzero if we can prove that X and Y contain the same value, taking
618 our gathered information into account. */
621 rtx_equal_for_cselib_p (rtx x, rtx y)
627 if (REG_P (x) || MEM_P (x))
629 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0);
635 if (REG_P (y) || MEM_P (y))
637 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0);
646 if (GET_CODE (x) == VALUE && GET_CODE (y) == VALUE)
647 return CSELIB_VAL_PTR (x) == CSELIB_VAL_PTR (y);
649 if (GET_CODE (x) == VALUE)
651 cselib_val *e = CSELIB_VAL_PTR (x);
652 struct elt_loc_list *l;
654 for (l = e->locs; l; l = l->next)
658 /* Avoid infinite recursion. */
659 if (REG_P (t) || MEM_P (t))
661 else if (rtx_equal_for_cselib_p (t, y))
668 if (GET_CODE (y) == VALUE)
670 cselib_val *e = CSELIB_VAL_PTR (y);
671 struct elt_loc_list *l;
673 for (l = e->locs; l; l = l->next)
677 if (REG_P (t) || MEM_P (t))
679 else if (rtx_equal_for_cselib_p (x, t))
686 if (GET_CODE (x) != GET_CODE (y) || GET_MODE (x) != GET_MODE (y))
689 /* These won't be handled correctly by the code below. */
690 switch (GET_CODE (x))
698 return XEXP (x, 0) == XEXP (y, 0);
705 fmt = GET_RTX_FORMAT (code);
707 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
714 if (XWINT (x, i) != XWINT (y, i))
720 if (XINT (x, i) != XINT (y, i))
726 /* Two vectors must have the same length. */
727 if (XVECLEN (x, i) != XVECLEN (y, i))
730 /* And the corresponding elements must match. */
731 for (j = 0; j < XVECLEN (x, i); j++)
732 if (! rtx_equal_for_cselib_p (XVECEXP (x, i, j),
739 && targetm.commutative_p (x, UNKNOWN)
740 && rtx_equal_for_cselib_p (XEXP (x, 1), XEXP (y, 0))
741 && rtx_equal_for_cselib_p (XEXP (x, 0), XEXP (y, 1)))
743 if (! rtx_equal_for_cselib_p (XEXP (x, i), XEXP (y, i)))
749 if (strcmp (XSTR (x, i), XSTR (y, i)))
754 /* These are just backpointers, so they don't matter. */
761 /* It is believed that rtx's at this level will never
762 contain anything but integers and other rtx's,
763 except for within LABEL_REFs and SYMBOL_REFs. */
771 /* We need to pass down the mode of constants through the hash table
772 functions. For that purpose, wrap them in a CONST of the appropriate
775 wrap_constant (enum machine_mode mode, rtx x)
777 if (!CONST_INT_P (x) && GET_CODE (x) != CONST_FIXED
778 && (GET_CODE (x) != CONST_DOUBLE || GET_MODE (x) != VOIDmode))
780 gcc_assert (mode != VOIDmode);
781 return gen_rtx_CONST (mode, x);
784 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
785 For registers and memory locations, we look up their cselib_val structure
786 and return its VALUE element.
787 Possible reasons for return 0 are: the object is volatile, or we couldn't
788 find a register or memory location in the table and CREATE is zero. If
789 CREATE is nonzero, table elts are created for regs and mem.
790 N.B. this hash function returns the same hash value for RTXes that
791 differ only in the order of operands, thus it is suitable for comparisons
792 that take commutativity into account.
793 If we wanted to also support associative rules, we'd have to use a different
794 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
795 We used to have a MODE argument for hashing for CONST_INTs, but that
796 didn't make sense, since it caused spurious hash differences between
797 (set (reg:SI 1) (const_int))
798 (plus:SI (reg:SI 2) (reg:SI 1))
800 (plus:SI (reg:SI 2) (const_int))
801 If the mode is important in any context, it must be checked specifically
802 in a comparison anyway, since relying on hash differences is unsafe. */
805 cselib_hash_rtx (rtx x, int create)
811 unsigned int hash = 0;
814 hash += (unsigned) code + (unsigned) GET_MODE (x);
820 e = cselib_lookup (x, GET_MODE (x), create);
827 hash += ((unsigned) DEBUG_EXPR << 7)
828 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
829 return hash ? hash : (unsigned int) DEBUG_EXPR;
832 hash += ((unsigned) CONST_INT << 7) + INTVAL (x);
833 return hash ? hash : (unsigned int) CONST_INT;
836 /* This is like the general case, except that it only counts
837 the integers representing the constant. */
838 hash += (unsigned) code + (unsigned) GET_MODE (x);
839 if (GET_MODE (x) != VOIDmode)
840 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
842 hash += ((unsigned) CONST_DOUBLE_LOW (x)
843 + (unsigned) CONST_DOUBLE_HIGH (x));
844 return hash ? hash : (unsigned int) CONST_DOUBLE;
847 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
848 hash += fixed_hash (CONST_FIXED_VALUE (x));
849 return hash ? hash : (unsigned int) CONST_FIXED;
856 units = CONST_VECTOR_NUNITS (x);
858 for (i = 0; i < units; ++i)
860 elt = CONST_VECTOR_ELT (x, i);
861 hash += cselib_hash_rtx (elt, 0);
867 /* Assume there is only one rtx object for any given label. */
869 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
870 differences and differences between each stage's debugging dumps. */
871 hash += (((unsigned int) LABEL_REF << 7)
872 + CODE_LABEL_NUMBER (XEXP (x, 0)));
873 return hash ? hash : (unsigned int) LABEL_REF;
877 /* Don't hash on the symbol's address to avoid bootstrap differences.
878 Different hash values may cause expressions to be recorded in
879 different orders and thus different registers to be used in the
880 final assembler. This also avoids differences in the dump files
881 between various stages. */
883 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
886 h += (h << 7) + *p++; /* ??? revisit */
888 hash += ((unsigned int) SYMBOL_REF << 7) + h;
889 return hash ? hash : (unsigned int) SYMBOL_REF;
901 case UNSPEC_VOLATILE:
905 if (MEM_VOLATILE_P (x))
914 i = GET_RTX_LENGTH (code) - 1;
915 fmt = GET_RTX_FORMAT (code);
922 rtx tem = XEXP (x, i);
923 unsigned int tem_hash = cselib_hash_rtx (tem, create);
932 for (j = 0; j < XVECLEN (x, i); j++)
934 unsigned int tem_hash
935 = cselib_hash_rtx (XVECEXP (x, i, j), create);
946 const unsigned char *p = (const unsigned char *) XSTR (x, i);
968 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
971 /* Create a new value structure for VALUE and initialize it. The mode of the
974 static inline cselib_val *
975 new_cselib_val (unsigned int hash, enum machine_mode mode, rtx x)
977 cselib_val *e = (cselib_val *) pool_alloc (cselib_val_pool);
980 gcc_assert (next_uid);
984 /* We use an alloc pool to allocate this RTL construct because it
985 accounts for about 8% of the overall memory usage. We know
986 precisely when we can have VALUE RTXen (when cselib is active)
987 so we don't need to put them in garbage collected memory.
988 ??? Why should a VALUE be an RTX in the first place? */
989 e->val_rtx = (rtx) pool_alloc (value_pool);
990 memset (e->val_rtx, 0, RTX_HDR_SIZE);
991 PUT_CODE (e->val_rtx, VALUE);
992 PUT_MODE (e->val_rtx, mode);
993 CSELIB_VAL_PTR (e->val_rtx) = e;
996 e->next_containing_mem = 0;
998 if (dump_file && (dump_flags & TDF_DETAILS))
1000 fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
1001 if (flag_dump_noaddr || flag_dump_unnumbered)
1002 fputs ("# ", dump_file);
1004 fprintf (dump_file, "%p ", (void*)e);
1005 print_rtl_single (dump_file, x);
1006 fputc ('\n', dump_file);
1012 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1013 contains the data at this address. X is a MEM that represents the
1014 value. Update the two value structures to represent this situation. */
1017 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
1019 struct elt_loc_list *l;
1021 /* Avoid duplicates. */
1022 for (l = mem_elt->locs; l; l = l->next)
1024 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
1026 promote_debug_loc (l);
1030 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
1032 = new_elt_loc_list (mem_elt->locs,
1033 replace_equiv_address_nv (x, addr_elt->val_rtx));
1034 if (mem_elt->next_containing_mem == NULL)
1036 mem_elt->next_containing_mem = first_containing_mem;
1037 first_containing_mem = mem_elt;
1041 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1042 If CREATE, make a new one if we haven't seen it before. */
1045 cselib_lookup_mem (rtx x, int create)
1047 enum machine_mode mode = GET_MODE (x);
1050 cselib_val *mem_elt;
1053 if (MEM_VOLATILE_P (x) || mode == BLKmode
1054 || !cselib_record_memory
1055 || (FLOAT_MODE_P (mode) && flag_float_store))
1058 /* Look up the value for the address. */
1059 addr = cselib_lookup (XEXP (x, 0), mode, create);
1063 /* Find a value that describes a value of our mode at that address. */
1064 for (l = addr->addr_list; l; l = l->next)
1065 if (GET_MODE (l->elt->val_rtx) == mode)
1067 promote_debug_loc (l->elt->locs);
1074 mem_elt = new_cselib_val (next_uid, mode, x);
1075 add_mem_for_addr (addr, mem_elt, x);
1076 slot = htab_find_slot_with_hash (cselib_hash_table, wrap_constant (mode, x),
1077 mem_elt->hash, INSERT);
1082 /* Search thru the possible substitutions in P. We prefer a non reg
1083 substitution because this allows us to expand the tree further. If
1084 we find, just a reg, take the lowest regno. There may be several
1085 non-reg results, we just take the first one because they will all
1086 expand to the same place. */
1089 expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1092 rtx reg_result = NULL;
1093 unsigned int regno = UINT_MAX;
1094 struct elt_loc_list *p_in = p;
1096 for (; p; p = p -> next)
1098 /* Avoid infinite recursion trying to expand a reg into a
1100 if ((REG_P (p->loc))
1101 && (REGNO (p->loc) < regno)
1102 && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
1104 reg_result = p->loc;
1105 regno = REGNO (p->loc);
1107 /* Avoid infinite recursion and do not try to expand the
1109 else if (GET_CODE (p->loc) == VALUE
1110 && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1112 else if (!REG_P (p->loc))
1115 if (dump_file && (dump_flags & TDF_DETAILS))
1117 print_inline_rtx (dump_file, p->loc, 0);
1118 fprintf (dump_file, "\n");
1120 if (GET_CODE (p->loc) == LO_SUM
1121 && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1123 && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1124 && XEXP (note, 0) == XEXP (p->loc, 1))
1125 return XEXP (p->loc, 1);
1126 result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
1133 if (regno != UINT_MAX)
1136 if (dump_file && (dump_flags & TDF_DETAILS))
1137 fprintf (dump_file, "r%d\n", regno);
1139 result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
1144 if (dump_file && (dump_flags & TDF_DETAILS))
1148 print_inline_rtx (dump_file, reg_result, 0);
1149 fprintf (dump_file, "\n");
1152 fprintf (dump_file, "NULL\n");
1158 /* Forward substitute and expand an expression out to its roots.
1159 This is the opposite of common subexpression. Because local value
1160 numbering is such a weak optimization, the expanded expression is
1161 pretty much unique (not from a pointer equals point of view but
1162 from a tree shape point of view.
1164 This function returns NULL if the expansion fails. The expansion
1165 will fail if there is no value number for one of the operands or if
1166 one of the operands has been overwritten between the current insn
1167 and the beginning of the basic block. For instance x has no
1173 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1174 It is clear on return. */
1177 cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
1179 struct expand_value_data evd;
1181 evd.regs_active = regs_active;
1182 evd.callback = NULL;
1183 evd.callback_arg = NULL;
1186 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1189 /* Same as cselib_expand_value_rtx, but using a callback to try to
1190 resolve some expressions. The CB function should return ORIG if it
1191 can't or does not want to deal with a certain RTX. Any other
1192 return value, including NULL, will be used as the expansion for
1193 VALUE, without any further changes. */
1196 cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1197 cselib_expand_callback cb, void *data)
1199 struct expand_value_data evd;
1201 evd.regs_active = regs_active;
1203 evd.callback_arg = data;
1206 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1209 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1210 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1211 would return NULL or non-NULL, without allocating new rtx. */
1214 cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1215 cselib_expand_callback cb, void *data)
1217 struct expand_value_data evd;
1219 evd.regs_active = regs_active;
1221 evd.callback_arg = data;
1224 return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1227 /* Internal implementation of cselib_expand_value_rtx and
1228 cselib_expand_value_rtx_cb. */
1231 cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1237 const char *format_ptr;
1238 enum machine_mode mode;
1240 code = GET_CODE (orig);
1242 /* For the context of dse, if we end up expand into a huge tree, we
1243 will not have a useful address, so we might as well just give up
1252 struct elt_list *l = REG_VALUES (REGNO (orig));
1254 if (l && l->elt == NULL)
1256 for (; l; l = l->next)
1257 if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1260 int regno = REGNO (orig);
1262 /* The only thing that we are not willing to do (this
1263 is requirement of dse and if others potential uses
1264 need this function we should add a parm to control
1265 it) is that we will not substitute the
1266 STACK_POINTER_REGNUM, FRAME_POINTER or the
1269 These expansions confuses the code that notices that
1270 stores into the frame go dead at the end of the
1271 function and that the frame is not effected by calls
1272 to subroutines. If you allow the
1273 STACK_POINTER_REGNUM substitution, then dse will
1274 think that parameter pushing also goes dead which is
1275 wrong. If you allow the FRAME_POINTER or the
1276 HARD_FRAME_POINTER then you lose the opportunity to
1277 make the frame assumptions. */
1278 if (regno == STACK_POINTER_REGNUM
1279 || regno == FRAME_POINTER_REGNUM
1280 || regno == HARD_FRAME_POINTER_REGNUM)
1283 bitmap_set_bit (evd->regs_active, regno);
1285 if (dump_file && (dump_flags & TDF_DETAILS))
1286 fprintf (dump_file, "expanding: r%d into: ", regno);
1288 result = expand_loc (l->elt->locs, evd, max_depth);
1289 bitmap_clear_bit (evd->regs_active, regno);
1306 /* SCRATCH must be shared because they represent distinct values. */
1309 if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1314 if (shared_const_p (orig))
1324 subreg = evd->callback (orig, evd->regs_active, max_depth,
1330 subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1334 scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1335 GET_MODE (SUBREG_REG (orig)),
1336 SUBREG_BYTE (orig));
1338 || (GET_CODE (scopy) == SUBREG
1339 && !REG_P (SUBREG_REG (scopy))
1340 && !MEM_P (SUBREG_REG (scopy))))
1350 if (dump_file && (dump_flags & TDF_DETAILS))
1352 fputs ("\nexpanding ", dump_file);
1353 print_rtl_single (dump_file, orig);
1354 fputs (" into...", dump_file);
1359 result = evd->callback (orig, evd->regs_active, max_depth,
1366 result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
1372 return evd->callback (orig, evd->regs_active, max_depth,
1380 /* Copy the various flags, fields, and other information. We assume
1381 that all fields need copying, and then clear the fields that should
1382 not be copied. That is the sensible default behavior, and forces
1383 us to explicitly document why we are *not* copying a flag. */
1387 copy = shallow_copy_rtx (orig);
1389 format_ptr = GET_RTX_FORMAT (code);
1391 for (i = 0; i < GET_RTX_LENGTH (code); i++)
1392 switch (*format_ptr++)
1395 if (XEXP (orig, i) != NULL)
1397 rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1402 XEXP (copy, i) = result;
1408 if (XVEC (orig, i) != NULL)
1411 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1412 for (j = 0; j < XVECLEN (orig, i); j++)
1414 rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1415 evd, max_depth - 1);
1419 XVECEXP (copy, i, j) = result;
1433 /* These are left unchanged. */
1443 mode = GET_MODE (copy);
1444 /* If an operand has been simplified into CONST_INT, which doesn't
1445 have a mode and the mode isn't derivable from whole rtx's mode,
1446 try simplify_*_operation first with mode from original's operand
1447 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1449 switch (GET_RTX_CLASS (code))
1452 if (CONST_INT_P (XEXP (copy, 0))
1453 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1455 scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1456 GET_MODE (XEXP (orig, 0)));
1461 case RTX_COMM_ARITH:
1463 /* These expressions can derive operand modes from the whole rtx's mode. */
1466 case RTX_BITFIELD_OPS:
1467 if (CONST_INT_P (XEXP (copy, 0))
1468 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1470 scopy = simplify_ternary_operation (code, mode,
1471 GET_MODE (XEXP (orig, 0)),
1472 XEXP (copy, 0), XEXP (copy, 1),
1479 case RTX_COMM_COMPARE:
1480 if (CONST_INT_P (XEXP (copy, 0))
1481 && GET_MODE (XEXP (copy, 1)) == VOIDmode
1482 && (GET_MODE (XEXP (orig, 0)) != VOIDmode
1483 || GET_MODE (XEXP (orig, 1)) != VOIDmode))
1485 scopy = simplify_relational_operation (code, mode,
1486 (GET_MODE (XEXP (orig, 0))
1488 ? GET_MODE (XEXP (orig, 0))
1489 : GET_MODE (XEXP (orig, 1)),
1499 scopy = simplify_rtx (copy);
1505 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1506 with VALUE expressions. This way, it becomes independent of changes
1507 to registers and memory.
1508 X isn't actually modified; if modifications are needed, new rtl is
1509 allocated. However, the return value can share rtl with X. */
1512 cselib_subst_to_values (rtx x)
1514 enum rtx_code code = GET_CODE (x);
1515 const char *fmt = GET_RTX_FORMAT (code);
1524 l = REG_VALUES (REGNO (x));
1525 if (l && l->elt == NULL)
1527 for (; l; l = l->next)
1528 if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
1529 return l->elt->val_rtx;
1534 e = cselib_lookup_mem (x, 0);
1537 /* This happens for autoincrements. Assign a value that doesn't
1539 e = new_cselib_val (next_uid, GET_MODE (x), x);
1555 e = new_cselib_val (next_uid, GET_MODE (x), x);
1562 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1566 rtx t = cselib_subst_to_values (XEXP (x, i));
1568 if (t != XEXP (x, i))
1571 copy = shallow_copy_rtx (x);
1575 else if (fmt[i] == 'E')
1579 for (j = 0; j < XVECLEN (x, i); j++)
1581 rtx t = cselib_subst_to_values (XVECEXP (x, i, j));
1583 if (t != XVECEXP (x, i, j))
1585 if (XVEC (x, i) == XVEC (copy, i))
1588 copy = shallow_copy_rtx (x);
1589 XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
1591 XVECEXP (copy, i, j) = t;
1600 /* Look up the rtl expression X in our tables and return the value it has.
1601 If CREATE is zero, we return NULL if we don't know the value. Otherwise,
1602 we create a new one if possible, using mode MODE if X doesn't have a mode
1603 (i.e. because it's a constant). */
1606 cselib_lookup_1 (rtx x, enum machine_mode mode, int create)
1610 unsigned int hashval;
1612 if (GET_MODE (x) != VOIDmode)
1613 mode = GET_MODE (x);
1615 if (GET_CODE (x) == VALUE)
1616 return CSELIB_VAL_PTR (x);
1621 unsigned int i = REGNO (x);
1624 if (l && l->elt == NULL)
1626 for (; l; l = l->next)
1627 if (mode == GET_MODE (l->elt->val_rtx))
1629 promote_debug_loc (l->elt->locs);
1636 if (i < FIRST_PSEUDO_REGISTER)
1638 unsigned int n = hard_regno_nregs[i][mode];
1640 if (n > max_value_regs)
1644 e = new_cselib_val (next_uid, GET_MODE (x), x);
1645 e->locs = new_elt_loc_list (e->locs, x);
1646 if (REG_VALUES (i) == 0)
1648 /* Maintain the invariant that the first entry of
1649 REG_VALUES, if present, must be the value used to set the
1650 register, or NULL. */
1651 used_regs[n_used_regs++] = i;
1652 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
1654 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
1655 slot = htab_find_slot_with_hash (cselib_hash_table, x, e->hash, INSERT);
1661 return cselib_lookup_mem (x, create);
1663 hashval = cselib_hash_rtx (x, create);
1664 /* Can't even create if hashing is not possible. */
1668 slot = htab_find_slot_with_hash (cselib_hash_table, wrap_constant (mode, x),
1669 hashval, create ? INSERT : NO_INSERT);
1673 e = (cselib_val *) *slot;
1677 e = new_cselib_val (hashval, mode, x);
1679 /* We have to fill the slot before calling cselib_subst_to_values:
1680 the hash table is inconsistent until we do so, and
1681 cselib_subst_to_values will need to do lookups. */
1683 e->locs = new_elt_loc_list (e->locs, cselib_subst_to_values (x));
1687 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
1690 cselib_lookup_from_insn (rtx x, enum machine_mode mode,
1691 int create, rtx insn)
1695 gcc_assert (!cselib_current_insn);
1696 cselib_current_insn = insn;
1698 ret = cselib_lookup (x, mode, create);
1700 cselib_current_insn = NULL;
1705 /* Wrapper for cselib_lookup_1, that logs the lookup result and
1706 maintains invariants related with debug insns. */
1709 cselib_lookup (rtx x, enum machine_mode mode, int create)
1711 cselib_val *ret = cselib_lookup_1 (x, mode, create);
1713 /* ??? Should we return NULL if we're not to create an entry, the
1714 found loc is a debug loc and cselib_current_insn is not DEBUG?
1715 If so, we should also avoid converting val to non-DEBUG; probably
1716 easiest setting cselib_current_insn to NULL before the call
1719 if (dump_file && (dump_flags & TDF_DETAILS))
1721 fputs ("cselib lookup ", dump_file);
1722 print_inline_rtx (dump_file, x, 2);
1723 fprintf (dump_file, " => %u:%u\n",
1725 ret ? ret->hash : 0);
1731 /* Invalidate any entries in reg_values that overlap REGNO. This is called
1732 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
1733 is used to determine how many hard registers are being changed. If MODE
1734 is VOIDmode, then only REGNO is being changed; this is used when
1735 invalidating call clobbered registers across a call. */
1738 cselib_invalidate_regno (unsigned int regno, enum machine_mode mode)
1740 unsigned int endregno;
1743 /* If we see pseudos after reload, something is _wrong_. */
1744 gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
1745 || reg_renumber[regno] < 0);
1747 /* Determine the range of registers that must be invalidated. For
1748 pseudos, only REGNO is affected. For hard regs, we must take MODE
1749 into account, and we must also invalidate lower register numbers
1750 if they contain values that overlap REGNO. */
1751 if (regno < FIRST_PSEUDO_REGISTER)
1753 gcc_assert (mode != VOIDmode);
1755 if (regno < max_value_regs)
1758 i = regno - max_value_regs;
1760 endregno = end_hard_regno (mode, regno);
1765 endregno = regno + 1;
1768 for (; i < endregno; i++)
1770 struct elt_list **l = ®_VALUES (i);
1772 /* Go through all known values for this reg; if it overlaps the range
1773 we're invalidating, remove the value. */
1776 cselib_val *v = (*l)->elt;
1779 struct elt_loc_list **p;
1780 unsigned int this_last = i;
1782 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
1783 this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
1785 if (this_last < regno || v == NULL || v == cfa_base_preserved_val)
1791 /* We have an overlap. */
1792 if (*l == REG_VALUES (i))
1794 /* Maintain the invariant that the first entry of
1795 REG_VALUES, if present, must be the value used to set
1796 the register, or NULL. This is also nice because
1797 then we won't push the same regno onto user_regs
1803 unchain_one_elt_list (l);
1805 had_locs = v->locs != NULL;
1806 setting_insn = v->locs ? v->locs->setting_insn : NULL;
1808 /* Now, we clear the mapping from value to reg. It must exist, so
1809 this code will crash intentionally if it doesn't. */
1810 for (p = &v->locs; ; p = &(*p)->next)
1814 if (REG_P (x) && REGNO (x) == i)
1816 unchain_one_elt_loc_list (p);
1821 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
1823 if (setting_insn && DEBUG_INSN_P (setting_insn))
1824 n_useless_debug_values++;
1832 /* Return 1 if X has a value that can vary even between two
1833 executions of the program. 0 means X can be compared reliably
1834 against certain constants or near-constants. */
1837 cselib_rtx_varies_p (const_rtx x ATTRIBUTE_UNUSED, bool from_alias ATTRIBUTE_UNUSED)
1839 /* We actually don't need to verify very hard. This is because
1840 if X has actually changed, we invalidate the memory anyway,
1841 so assume that all common memory addresses are
1846 /* Invalidate any locations in the table which are changed because of a
1847 store to MEM_RTX. If this is called because of a non-const call
1848 instruction, MEM_RTX is (mem:BLK const0_rtx). */
1851 cselib_invalidate_mem (rtx mem_rtx)
1853 cselib_val **vp, *v, *next;
1857 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
1858 mem_rtx = canon_rtx (mem_rtx);
1860 vp = &first_containing_mem;
1861 for (v = *vp; v != &dummy_val; v = next)
1863 bool has_mem = false;
1864 struct elt_loc_list **p = &v->locs;
1865 bool had_locs = v->locs != NULL;
1866 rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
1872 struct elt_list **mem_chain;
1874 /* MEMs may occur in locations only at the top level; below
1875 that every MEM or REG is substituted by its VALUE. */
1881 if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
1882 && ! canon_true_dependence (mem_rtx, GET_MODE (mem_rtx), mem_addr,
1883 x, NULL_RTX, cselib_rtx_varies_p))
1891 /* This one overlaps. */
1892 /* We must have a mapping from this MEM's address to the
1893 value (E). Remove that, too. */
1894 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0);
1895 mem_chain = &addr->addr_list;
1898 if ((*mem_chain)->elt == v)
1900 unchain_one_elt_list (mem_chain);
1904 mem_chain = &(*mem_chain)->next;
1907 unchain_one_elt_loc_list (p);
1910 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
1912 if (setting_insn && DEBUG_INSN_P (setting_insn))
1913 n_useless_debug_values++;
1918 next = v->next_containing_mem;
1922 vp = &(*vp)->next_containing_mem;
1925 v->next_containing_mem = NULL;
1930 /* Invalidate DEST, which is being assigned to or clobbered. */
1933 cselib_invalidate_rtx (rtx dest)
1935 while (GET_CODE (dest) == SUBREG
1936 || GET_CODE (dest) == ZERO_EXTRACT
1937 || GET_CODE (dest) == STRICT_LOW_PART)
1938 dest = XEXP (dest, 0);
1941 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
1942 else if (MEM_P (dest))
1943 cselib_invalidate_mem (dest);
1945 /* Some machines don't define AUTO_INC_DEC, but they still use push
1946 instructions. We need to catch that case here in order to
1947 invalidate the stack pointer correctly. Note that invalidating
1948 the stack pointer is different from invalidating DEST. */
1949 if (push_operand (dest, GET_MODE (dest)))
1950 cselib_invalidate_rtx (stack_pointer_rtx);
1953 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
1956 cselib_invalidate_rtx_note_stores (rtx dest, const_rtx ignore ATTRIBUTE_UNUSED,
1957 void *data ATTRIBUTE_UNUSED)
1959 cselib_invalidate_rtx (dest);
1962 /* Record the result of a SET instruction. DEST is being set; the source
1963 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
1964 describes its address. */
1967 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
1969 int dreg = REG_P (dest) ? (int) REGNO (dest) : -1;
1971 if (src_elt == 0 || side_effects_p (dest))
1976 if (dreg < FIRST_PSEUDO_REGISTER)
1978 unsigned int n = hard_regno_nregs[dreg][GET_MODE (dest)];
1980 if (n > max_value_regs)
1984 if (REG_VALUES (dreg) == 0)
1986 used_regs[n_used_regs++] = dreg;
1987 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
1991 /* The register should have been invalidated. */
1992 gcc_assert (REG_VALUES (dreg)->elt == 0);
1993 REG_VALUES (dreg)->elt = src_elt;
1996 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
1998 src_elt->locs = new_elt_loc_list (src_elt->locs, dest);
2000 else if (MEM_P (dest) && dest_addr_elt != 0
2001 && cselib_record_memory)
2003 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2005 add_mem_for_addr (dest_addr_elt, src_elt, dest);
2009 /* There is no good way to determine how many elements there can be
2010 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2011 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2013 /* Record the effects of any sets in INSN. */
2015 cselib_record_sets (rtx insn)
2019 struct cselib_set sets[MAX_SETS];
2020 rtx body = PATTERN (insn);
2023 body = PATTERN (insn);
2024 if (GET_CODE (body) == COND_EXEC)
2026 cond = COND_EXEC_TEST (body);
2027 body = COND_EXEC_CODE (body);
2030 /* Find all sets. */
2031 if (GET_CODE (body) == SET)
2033 sets[0].src = SET_SRC (body);
2034 sets[0].dest = SET_DEST (body);
2037 else if (GET_CODE (body) == PARALLEL)
2039 /* Look through the PARALLEL and record the values being
2040 set, if possible. Also handle any CLOBBERs. */
2041 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2043 rtx x = XVECEXP (body, 0, i);
2045 if (GET_CODE (x) == SET)
2047 sets[n_sets].src = SET_SRC (x);
2048 sets[n_sets].dest = SET_DEST (x);
2055 && MEM_P (sets[0].src)
2056 && !cselib_record_memory
2057 && MEM_READONLY_P (sets[0].src))
2059 rtx note = find_reg_equal_equiv_note (insn);
2061 if (note && CONSTANT_P (XEXP (note, 0)))
2062 sets[0].src = XEXP (note, 0);
2065 /* Look up the values that are read. Do this before invalidating the
2066 locations that are written. */
2067 for (i = 0; i < n_sets; i++)
2069 rtx dest = sets[i].dest;
2071 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2072 the low part after invalidating any knowledge about larger modes. */
2073 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2074 sets[i].dest = dest = XEXP (dest, 0);
2076 /* We don't know how to record anything but REG or MEM. */
2078 || (MEM_P (dest) && cselib_record_memory))
2080 rtx src = sets[i].src;
2082 src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
2083 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1);
2086 enum machine_mode address_mode
2087 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (dest));
2089 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
2093 sets[i].dest_addr_elt = 0;
2097 if (cselib_record_sets_hook)
2098 cselib_record_sets_hook (insn, sets, n_sets);
2100 /* Invalidate all locations written by this insn. Note that the elts we
2101 looked up in the previous loop aren't affected, just some of their
2102 locations may go away. */
2103 note_stores (body, cselib_invalidate_rtx_note_stores, NULL);
2105 /* If this is an asm, look for duplicate sets. This can happen when the
2106 user uses the same value as an output multiple times. This is valid
2107 if the outputs are not actually used thereafter. Treat this case as
2108 if the value isn't actually set. We do this by smashing the destination
2109 to pc_rtx, so that we won't record the value later. */
2110 if (n_sets >= 2 && asm_noperands (body) >= 0)
2112 for (i = 0; i < n_sets; i++)
2114 rtx dest = sets[i].dest;
2115 if (REG_P (dest) || MEM_P (dest))
2118 for (j = i + 1; j < n_sets; j++)
2119 if (rtx_equal_p (dest, sets[j].dest))
2121 sets[i].dest = pc_rtx;
2122 sets[j].dest = pc_rtx;
2128 /* Now enter the equivalences in our tables. */
2129 for (i = 0; i < n_sets; i++)
2131 rtx dest = sets[i].dest;
2133 || (MEM_P (dest) && cselib_record_memory))
2134 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2138 /* Record the effects of INSN. */
2141 cselib_process_insn (rtx insn)
2146 cselib_current_insn = insn;
2148 /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp. */
2151 && find_reg_note (insn, REG_SETJMP, NULL))
2152 || (NONJUMP_INSN_P (insn)
2153 && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
2154 && MEM_VOLATILE_P (PATTERN (insn))))
2156 cselib_reset_table (next_uid);
2157 cselib_current_insn = NULL_RTX;
2161 if (! INSN_P (insn))
2163 cselib_current_insn = NULL_RTX;
2167 /* If this is a call instruction, forget anything stored in a
2168 call clobbered register, or, if this is not a const call, in
2172 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2173 if (call_used_regs[i]
2174 || (REG_VALUES (i) && REG_VALUES (i)->elt
2175 && HARD_REGNO_CALL_PART_CLOBBERED (i,
2176 GET_MODE (REG_VALUES (i)->elt->val_rtx))))
2177 cselib_invalidate_regno (i, reg_raw_mode[i]);
2179 /* Since it is not clear how cselib is going to be used, be
2180 conservative here and treat looping pure or const functions
2181 as if they were regular functions. */
2182 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
2183 || !(RTL_CONST_OR_PURE_CALL_P (insn)))
2184 cselib_invalidate_mem (callmem);
2187 cselib_record_sets (insn);
2190 /* Clobber any registers which appear in REG_INC notes. We
2191 could keep track of the changes to their values, but it is
2192 unlikely to help. */
2193 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
2194 if (REG_NOTE_KIND (x) == REG_INC)
2195 cselib_invalidate_rtx (XEXP (x, 0));
2198 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2199 after we have processed the insn. */
2201 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
2202 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
2203 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
2205 cselib_current_insn = NULL_RTX;
2207 if (n_useless_values > MAX_USELESS_VALUES
2208 /* remove_useless_values is linear in the hash table size. Avoid
2209 quadratic behavior for very large hashtables with very few
2210 useless elements. */
2211 && ((unsigned int)n_useless_values
2212 > (cselib_hash_table->n_elements
2213 - cselib_hash_table->n_deleted
2214 - n_debug_values) / 4))
2215 remove_useless_values ();
2218 /* Initialize cselib for one pass. The caller must also call
2219 init_alias_analysis. */
2222 cselib_init (int record_what)
2224 elt_list_pool = create_alloc_pool ("elt_list",
2225 sizeof (struct elt_list), 10);
2226 elt_loc_list_pool = create_alloc_pool ("elt_loc_list",
2227 sizeof (struct elt_loc_list), 10);
2228 cselib_val_pool = create_alloc_pool ("cselib_val_list",
2229 sizeof (cselib_val), 10);
2230 value_pool = create_alloc_pool ("value", RTX_CODE_SIZE (VALUE), 100);
2231 cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
2232 cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
2234 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2235 see canon_true_dependence. This is only created once. */
2237 callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2239 cselib_nregs = max_reg_num ();
2241 /* We preserve reg_values to allow expensive clearing of the whole thing.
2242 Reallocate it however if it happens to be too large. */
2243 if (!reg_values || reg_values_size < cselib_nregs
2244 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
2248 /* Some space for newly emit instructions so we don't end up
2249 reallocating in between passes. */
2250 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
2251 reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
2253 used_regs = XNEWVEC (unsigned int, cselib_nregs);
2255 cselib_hash_table = htab_create (31, get_value_hash,
2256 entry_and_rtx_equal_p, NULL);
2260 /* Called when the current user is done with cselib. */
2263 cselib_finish (void)
2265 cselib_discard_hook = NULL;
2266 cselib_preserve_constants = false;
2267 cfa_base_preserved_val = NULL;
2268 free_alloc_pool (elt_list_pool);
2269 free_alloc_pool (elt_loc_list_pool);
2270 free_alloc_pool (cselib_val_pool);
2271 free_alloc_pool (value_pool);
2272 cselib_clear_table ();
2273 htab_delete (cselib_hash_table);
2276 cselib_hash_table = 0;
2277 n_useless_values = 0;
2278 n_useless_debug_values = 0;
2283 /* Dump the cselib_val *X to FILE *info. */
2286 dump_cselib_val (void **x, void *info)
2288 cselib_val *v = (cselib_val *)*x;
2289 FILE *out = (FILE *)info;
2290 bool need_lf = true;
2292 print_inline_rtx (out, v->val_rtx, 0);
2296 struct elt_loc_list *l = v->locs;
2302 fputs (" locs:", out);
2305 fprintf (out, "\n from insn %i ",
2306 INSN_UID (l->setting_insn));
2307 print_inline_rtx (out, l->loc, 4);
2309 while ((l = l->next));
2314 fputs (" no locs", out);
2320 struct elt_list *e = v->addr_list;
2326 fputs (" addr list:", out);
2330 print_inline_rtx (out, e->elt->val_rtx, 2);
2332 while ((e = e->next));
2337 fputs (" no addrs", out);
2341 if (v->next_containing_mem == &dummy_val)
2342 fputs (" last mem\n", out);
2343 else if (v->next_containing_mem)
2345 fputs (" next mem ", out);
2346 print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
2355 /* Dump to OUT everything in the CSELIB table. */
2358 dump_cselib_table (FILE *out)
2360 fprintf (out, "cselib hash table:\n");
2361 htab_traverse (cselib_hash_table, dump_cselib_val, out);
2362 if (first_containing_mem != &dummy_val)
2364 fputs ("first mem ", out);
2365 print_inline_rtx (out, first_containing_mem->val_rtx, 2);
2368 fprintf (out, "next uid %i\n", next_uid);
2371 #include "gt-cselib.h"