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 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
30 #include "hard-reg-set.h"
33 #include "insn-config.h"
43 static int entry_and_rtx_equal_p
PARAMS ((const void *, const void *));
44 static hashval_t get_value_hash
PARAMS ((const void *));
45 static struct elt_list
*new_elt_list
PARAMS ((struct elt_list
*,
47 static struct elt_loc_list
*new_elt_loc_list
PARAMS ((struct elt_loc_list
*,
49 static void unchain_one_value
PARAMS ((cselib_val
*));
50 static void unchain_one_elt_list
PARAMS ((struct elt_list
**));
51 static void unchain_one_elt_loc_list
PARAMS ((struct elt_loc_list
**));
52 static void clear_table
PARAMS ((void));
53 static int discard_useless_locs
PARAMS ((void **, void *));
54 static int discard_useless_values
PARAMS ((void **, void *));
55 static void remove_useless_values
PARAMS ((void));
56 static rtx wrap_constant
PARAMS ((enum machine_mode
, rtx
));
57 static unsigned int hash_rtx
PARAMS ((rtx
, enum machine_mode
, int));
58 static cselib_val
*new_cselib_val
PARAMS ((unsigned int,
60 static void add_mem_for_addr
PARAMS ((cselib_val
*, cselib_val
*,
62 static cselib_val
*cselib_lookup_mem
PARAMS ((rtx
, int));
63 static void cselib_invalidate_regno
PARAMS ((unsigned int,
65 static int cselib_mem_conflict_p
PARAMS ((rtx
, rtx
));
66 static void cselib_invalidate_mem
PARAMS ((rtx
));
67 static void cselib_invalidate_rtx
PARAMS ((rtx
, rtx
, void *));
68 static void cselib_record_set
PARAMS ((rtx
, cselib_val
*,
70 static void cselib_record_sets
PARAMS ((rtx
));
72 /* There are three ways in which cselib can look up an rtx:
73 - for a REG, the reg_values table (which is indexed by regno) is used
74 - for a MEM, we recursively look up its address and then follow the
75 addr_list of that value
76 - for everything else, we compute a hash value and go through the hash
77 table. Since different rtx's can still have the same hash value,
78 this involves walking the table entries for a given value and comparing
79 the locations of the entries with the rtx we are looking up. */
81 /* A table that enables us to look up elts by their value. */
82 static GTY((param_is (cselib_val
))) htab_t hash_table
;
84 /* This is a global so we don't have to pass this through every function.
85 It is used in new_elt_loc_list to set SETTING_INSN. */
86 static rtx cselib_current_insn
;
87 static bool cselib_current_insn_in_libcall
;
89 /* Every new unknown value gets a unique number. */
90 static unsigned int next_unknown_value
;
92 /* The number of registers we had when the varrays were last resized. */
93 static unsigned int cselib_nregs
;
95 /* Count values without known locations. Whenever this grows too big, we
96 remove these useless values from the table. */
97 static int n_useless_values
;
99 /* Number of useless values before we remove them from the hash table. */
100 #define MAX_USELESS_VALUES 32
102 /* This table maps from register number to values. It does not
103 contain pointers to cselib_val structures, but rather elt_lists.
104 The purpose is to be able to refer to the same register in
105 different modes. The first element of the list defines the mode in
106 which the register was set; if the mode is unknown or the value is
107 no longer valid in that mode, ELT will be NULL for the first
109 static GTY(()) varray_type reg_values
;
110 static GTY((deletable (""))) varray_type reg_values_old
;
111 #define REG_VALUES(I) VARRAY_ELT_LIST (reg_values, (I))
113 /* The largest number of hard regs used by any entry added to the
114 REG_VALUES table. Cleared on each clear_table() invocation. */
115 static unsigned int max_value_regs
;
117 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
118 in clear_table() for fast emptying. */
119 static GTY(()) varray_type used_regs
;
120 static GTY((deletable (""))) varray_type used_regs_old
;
122 /* We pass this to cselib_invalidate_mem to invalidate all of
123 memory for a non-const call instruction. */
124 static GTY(()) rtx callmem
;
126 /* Caches for unused structures. */
127 static GTY((deletable (""))) cselib_val
*empty_vals
;
128 static GTY((deletable (""))) struct elt_list
*empty_elt_lists
;
129 static GTY((deletable (""))) struct elt_loc_list
*empty_elt_loc_lists
;
131 /* Set by discard_useless_locs if it deleted the last location of any
133 static int values_became_useless
;
135 /* Used as stop element of the containing_mem list so we can check
136 presence in the list by checking the next pointer. */
137 static cselib_val dummy_val
;
139 /* Used to list all values that contain memory reference.
140 May or may not contain the useless values - the list is compacted
141 each time memory is invalidated. */
142 static cselib_val
*first_containing_mem
= &dummy_val
;
145 /* Allocate a struct elt_list and fill in its two elements with the
148 static struct elt_list
*
149 new_elt_list (next
, elt
)
150 struct elt_list
*next
;
153 struct elt_list
*el
= empty_elt_lists
;
156 empty_elt_lists
= el
->next
;
158 el
= (struct elt_list
*) ggc_alloc (sizeof (struct elt_list
));
164 /* Allocate a struct elt_loc_list and fill in its two elements with the
167 static struct elt_loc_list
*
168 new_elt_loc_list (next
, loc
)
169 struct elt_loc_list
*next
;
172 struct elt_loc_list
*el
= empty_elt_loc_lists
;
175 empty_elt_loc_lists
= el
->next
;
177 el
= (struct elt_loc_list
*) ggc_alloc (sizeof (struct elt_loc_list
));
180 el
->setting_insn
= cselib_current_insn
;
181 el
->in_libcall
= cselib_current_insn_in_libcall
;
185 /* The elt_list at *PL is no longer needed. Unchain it and free its
189 unchain_one_elt_list (pl
)
190 struct elt_list
**pl
;
192 struct elt_list
*l
= *pl
;
195 l
->next
= empty_elt_lists
;
199 /* Likewise for elt_loc_lists. */
202 unchain_one_elt_loc_list (pl
)
203 struct elt_loc_list
**pl
;
205 struct elt_loc_list
*l
= *pl
;
208 l
->next
= empty_elt_loc_lists
;
209 empty_elt_loc_lists
= l
;
212 /* Likewise for cselib_vals. This also frees the addr_list associated with
216 unchain_one_value (v
)
220 unchain_one_elt_list (&v
->addr_list
);
222 v
->u
.next_free
= empty_vals
;
226 /* Remove all entries from the hash table. Also used during
227 initialization. If CLEAR_ALL isn't set, then only clear the entries
228 which are known to have been used. */
235 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (used_regs
); i
++)
236 REG_VALUES (VARRAY_UINT (used_regs
, i
)) = 0;
240 VARRAY_POP_ALL (used_regs
);
242 htab_empty (hash_table
);
244 n_useless_values
= 0;
246 next_unknown_value
= 0;
248 first_containing_mem
= &dummy_val
;
251 /* The equality test for our hash table. The first argument ENTRY is a table
252 element (i.e. a cselib_val), while the second arg X is an rtx. We know
253 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
254 CONST of an appropriate mode. */
257 entry_and_rtx_equal_p (entry
, x_arg
)
258 const void *entry
, *x_arg
;
260 struct elt_loc_list
*l
;
261 const cselib_val
*v
= (const cselib_val
*) entry
;
263 enum machine_mode mode
= GET_MODE (x
);
265 if (GET_CODE (x
) == CONST_INT
266 || (mode
== VOIDmode
&& GET_CODE (x
) == CONST_DOUBLE
))
268 if (mode
!= GET_MODE (v
->u
.val_rtx
))
271 /* Unwrap X if necessary. */
272 if (GET_CODE (x
) == CONST
273 && (GET_CODE (XEXP (x
, 0)) == CONST_INT
274 || GET_CODE (XEXP (x
, 0)) == CONST_DOUBLE
))
277 /* We don't guarantee that distinct rtx's have different hash values,
278 so we need to do a comparison. */
279 for (l
= v
->locs
; l
; l
= l
->next
)
280 if (rtx_equal_for_cselib_p (l
->loc
, x
))
286 /* The hash function for our hash table. The value is always computed with
287 hash_rtx when adding an element; this function just extracts the hash
288 value from a cselib_val structure. */
291 get_value_hash (entry
)
294 const cselib_val
*v
= (const cselib_val
*) entry
;
298 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
299 only return true for values which point to a cselib_val whose value
300 element has been set to zero, which implies the cselib_val will be
304 references_value_p (x
, only_useless
)
308 enum rtx_code code
= GET_CODE (x
);
309 const char *fmt
= GET_RTX_FORMAT (code
);
312 if (GET_CODE (x
) == VALUE
313 && (! only_useless
|| CSELIB_VAL_PTR (x
)->locs
== 0))
316 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
318 if (fmt
[i
] == 'e' && references_value_p (XEXP (x
, i
), only_useless
))
320 else if (fmt
[i
] == 'E')
321 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
322 if (references_value_p (XVECEXP (x
, i
, j
), only_useless
))
329 /* For all locations found in X, delete locations that reference useless
330 values (i.e. values without any location). Called through
334 discard_useless_locs (x
, info
)
336 void *info ATTRIBUTE_UNUSED
;
338 cselib_val
*v
= (cselib_val
*)*x
;
339 struct elt_loc_list
**p
= &v
->locs
;
340 int had_locs
= v
->locs
!= 0;
344 if (references_value_p ((*p
)->loc
, 1))
345 unchain_one_elt_loc_list (p
);
350 if (had_locs
&& v
->locs
== 0)
353 values_became_useless
= 1;
358 /* If X is a value with no locations, remove it from the hashtable. */
361 discard_useless_values (x
, info
)
363 void *info ATTRIBUTE_UNUSED
;
365 cselib_val
*v
= (cselib_val
*)*x
;
369 htab_clear_slot (hash_table
, x
);
370 unchain_one_value (v
);
377 /* Clean out useless values (i.e. those which no longer have locations
378 associated with them) from the hash table. */
381 remove_useless_values ()
384 /* First pass: eliminate locations that reference the value. That in
385 turn can make more values useless. */
388 values_became_useless
= 0;
389 htab_traverse (hash_table
, discard_useless_locs
, 0);
391 while (values_became_useless
);
393 /* Second pass: actually remove the values. */
394 htab_traverse (hash_table
, discard_useless_values
, 0);
396 p
= &first_containing_mem
;
397 for (v
= *p
; v
!= &dummy_val
; v
= v
->next_containing_mem
)
401 p
= &(*p
)->next_containing_mem
;
405 if (n_useless_values
!= 0)
409 /* Return the mode in which a register was last set. If X is not a
410 register, return its mode. If the mode in which the register was
411 set is not known, or the value was already clobbered, return
415 cselib_reg_set_mode (x
)
418 if (GET_CODE (x
) != REG
)
421 if (REG_VALUES (REGNO (x
)) == NULL
422 || REG_VALUES (REGNO (x
))->elt
== NULL
)
425 return GET_MODE (REG_VALUES (REGNO (x
))->elt
->u
.val_rtx
);
428 /* Return nonzero if we can prove that X and Y contain the same value, taking
429 our gathered information into account. */
432 rtx_equal_for_cselib_p (x
, y
)
439 if (GET_CODE (x
) == REG
|| GET_CODE (x
) == MEM
)
441 cselib_val
*e
= cselib_lookup (x
, GET_MODE (x
), 0);
447 if (GET_CODE (y
) == REG
|| GET_CODE (y
) == MEM
)
449 cselib_val
*e
= cselib_lookup (y
, GET_MODE (y
), 0);
458 if (GET_CODE (x
) == VALUE
&& GET_CODE (y
) == VALUE
)
459 return CSELIB_VAL_PTR (x
) == CSELIB_VAL_PTR (y
);
461 if (GET_CODE (x
) == VALUE
)
463 cselib_val
*e
= CSELIB_VAL_PTR (x
);
464 struct elt_loc_list
*l
;
466 for (l
= e
->locs
; l
; l
= l
->next
)
470 /* Avoid infinite recursion. */
471 if (GET_CODE (t
) == REG
|| GET_CODE (t
) == MEM
)
473 else if (rtx_equal_for_cselib_p (t
, y
))
480 if (GET_CODE (y
) == VALUE
)
482 cselib_val
*e
= CSELIB_VAL_PTR (y
);
483 struct elt_loc_list
*l
;
485 for (l
= e
->locs
; l
; l
= l
->next
)
489 if (GET_CODE (t
) == REG
|| GET_CODE (t
) == MEM
)
491 else if (rtx_equal_for_cselib_p (x
, t
))
498 if (GET_CODE (x
) != GET_CODE (y
) || GET_MODE (x
) != GET_MODE (y
))
501 /* This won't be handled correctly by the code below. */
502 if (GET_CODE (x
) == LABEL_REF
)
503 return XEXP (x
, 0) == XEXP (y
, 0);
506 fmt
= GET_RTX_FORMAT (code
);
508 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
515 if (XWINT (x
, i
) != XWINT (y
, i
))
521 if (XINT (x
, i
) != XINT (y
, i
))
527 /* Two vectors must have the same length. */
528 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
531 /* And the corresponding elements must match. */
532 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
533 if (! rtx_equal_for_cselib_p (XVECEXP (x
, i
, j
),
539 if (! rtx_equal_for_cselib_p (XEXP (x
, i
), XEXP (y
, i
)))
545 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
550 /* These are just backpointers, so they don't matter. */
557 /* It is believed that rtx's at this level will never
558 contain anything but integers and other rtx's,
559 except for within LABEL_REFs and SYMBOL_REFs. */
567 /* We need to pass down the mode of constants through the hash table
568 functions. For that purpose, wrap them in a CONST of the appropriate
571 wrap_constant (mode
, x
)
572 enum machine_mode mode
;
575 if (GET_CODE (x
) != CONST_INT
576 && (GET_CODE (x
) != CONST_DOUBLE
|| GET_MODE (x
) != VOIDmode
))
578 if (mode
== VOIDmode
)
580 return gen_rtx_CONST (mode
, x
);
583 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
584 For registers and memory locations, we look up their cselib_val structure
585 and return its VALUE element.
586 Possible reasons for return 0 are: the object is volatile, or we couldn't
587 find a register or memory location in the table and CREATE is zero. If
588 CREATE is nonzero, table elts are created for regs and mem.
589 MODE is used in hashing for CONST_INTs only;
590 otherwise the mode of X is used. */
593 hash_rtx (x
, mode
, create
)
595 enum machine_mode mode
;
602 unsigned int hash
= 0;
605 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
611 e
= cselib_lookup (x
, GET_MODE (x
), create
);
618 hash
+= ((unsigned) CONST_INT
<< 7) + (unsigned) mode
+ INTVAL (x
);
619 return hash
? hash
: (unsigned int) CONST_INT
;
622 /* This is like the general case, except that it only counts
623 the integers representing the constant. */
624 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
625 if (GET_MODE (x
) != VOIDmode
)
626 hash
+= real_hash (CONST_DOUBLE_REAL_VALUE (x
));
628 hash
+= ((unsigned) CONST_DOUBLE_LOW (x
)
629 + (unsigned) CONST_DOUBLE_HIGH (x
));
630 return hash
? hash
: (unsigned int) CONST_DOUBLE
;
637 units
= CONST_VECTOR_NUNITS (x
);
639 for (i
= 0; i
< units
; ++i
)
641 elt
= CONST_VECTOR_ELT (x
, i
);
642 hash
+= hash_rtx (elt
, GET_MODE (elt
), 0);
648 /* Assume there is only one rtx object for any given label. */
651 += ((unsigned) LABEL_REF
<< 7) + (unsigned long) XEXP (x
, 0);
652 return hash
? hash
: (unsigned int) LABEL_REF
;
656 += ((unsigned) SYMBOL_REF
<< 7) + (unsigned long) XSTR (x
, 0);
657 return hash
? hash
: (unsigned int) SYMBOL_REF
;
668 case UNSPEC_VOLATILE
:
672 if (MEM_VOLATILE_P (x
))
681 i
= GET_RTX_LENGTH (code
) - 1;
682 fmt
= GET_RTX_FORMAT (code
);
687 rtx tem
= XEXP (x
, i
);
688 unsigned int tem_hash
= hash_rtx (tem
, 0, create
);
695 else if (fmt
[i
] == 'E')
696 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
698 unsigned int tem_hash
= hash_rtx (XVECEXP (x
, i
, j
), 0, create
);
705 else if (fmt
[i
] == 's')
707 const unsigned char *p
= (const unsigned char *) XSTR (x
, i
);
713 else if (fmt
[i
] == 'i')
715 else if (fmt
[i
] == '0' || fmt
[i
] == 't')
721 return hash
? hash
: 1 + (unsigned int) GET_CODE (x
);
724 /* Create a new value structure for VALUE and initialize it. The mode of the
728 new_cselib_val (value
, mode
)
730 enum machine_mode mode
;
732 cselib_val
*e
= empty_vals
;
735 empty_vals
= e
->u
.next_free
;
737 e
= (cselib_val
*) ggc_alloc (sizeof (cselib_val
));
743 e
->u
.val_rtx
= gen_rtx_VALUE (mode
);
744 CSELIB_VAL_PTR (e
->u
.val_rtx
) = e
;
747 e
->next_containing_mem
= 0;
751 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
752 contains the data at this address. X is a MEM that represents the
753 value. Update the two value structures to represent this situation. */
756 add_mem_for_addr (addr_elt
, mem_elt
, x
)
757 cselib_val
*addr_elt
, *mem_elt
;
760 struct elt_loc_list
*l
;
762 /* Avoid duplicates. */
763 for (l
= mem_elt
->locs
; l
; l
= l
->next
)
764 if (GET_CODE (l
->loc
) == MEM
765 && CSELIB_VAL_PTR (XEXP (l
->loc
, 0)) == addr_elt
)
768 addr_elt
->addr_list
= new_elt_list (addr_elt
->addr_list
, mem_elt
);
770 = new_elt_loc_list (mem_elt
->locs
,
771 replace_equiv_address_nv (x
, addr_elt
->u
.val_rtx
));
772 if (mem_elt
->next_containing_mem
== NULL
)
774 mem_elt
->next_containing_mem
= first_containing_mem
;
775 first_containing_mem
= mem_elt
;
779 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
780 If CREATE, make a new one if we haven't seen it before. */
783 cselib_lookup_mem (x
, create
)
787 enum machine_mode mode
= GET_MODE (x
);
793 if (MEM_VOLATILE_P (x
) || mode
== BLKmode
794 || (FLOAT_MODE_P (mode
) && flag_float_store
))
797 /* Look up the value for the address. */
798 addr
= cselib_lookup (XEXP (x
, 0), mode
, create
);
802 /* Find a value that describes a value of our mode at that address. */
803 for (l
= addr
->addr_list
; l
; l
= l
->next
)
804 if (GET_MODE (l
->elt
->u
.val_rtx
) == mode
)
810 mem_elt
= new_cselib_val (++next_unknown_value
, mode
);
811 add_mem_for_addr (addr
, mem_elt
, x
);
812 slot
= htab_find_slot_with_hash (hash_table
, wrap_constant (mode
, x
),
813 mem_elt
->value
, INSERT
);
818 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
819 with VALUE expressions. This way, it becomes independent of changes
820 to registers and memory.
821 X isn't actually modified; if modifications are needed, new rtl is
822 allocated. However, the return value can share rtl with X. */
825 cselib_subst_to_values (x
)
828 enum rtx_code code
= GET_CODE (x
);
829 const char *fmt
= GET_RTX_FORMAT (code
);
838 l
= REG_VALUES (REGNO (x
));
839 if (l
&& l
->elt
== NULL
)
841 for (; l
; l
= l
->next
)
842 if (GET_MODE (l
->elt
->u
.val_rtx
) == GET_MODE (x
))
843 return l
->elt
->u
.val_rtx
;
848 e
= cselib_lookup_mem (x
, 0);
851 /* This happens for autoincrements. Assign a value that doesn't
853 e
= new_cselib_val (++next_unknown_value
, GET_MODE (x
));
868 e
= new_cselib_val (++next_unknown_value
, GET_MODE (x
));
875 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
879 rtx t
= cselib_subst_to_values (XEXP (x
, i
));
881 if (t
!= XEXP (x
, i
) && x
== copy
)
882 copy
= shallow_copy_rtx (x
);
886 else if (fmt
[i
] == 'E')
890 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
892 rtx t
= cselib_subst_to_values (XVECEXP (x
, i
, j
));
894 if (t
!= XVECEXP (x
, i
, j
) && XVEC (x
, i
) == XVEC (copy
, i
))
897 copy
= shallow_copy_rtx (x
);
899 XVEC (copy
, i
) = rtvec_alloc (XVECLEN (x
, i
));
900 for (k
= 0; k
< j
; k
++)
901 XVECEXP (copy
, i
, k
) = XVECEXP (x
, i
, k
);
904 XVECEXP (copy
, i
, j
) = t
;
912 /* Look up the rtl expression X in our tables and return the value it has.
913 If CREATE is zero, we return NULL if we don't know the value. Otherwise,
914 we create a new one if possible, using mode MODE if X doesn't have a mode
915 (i.e. because it's a constant). */
918 cselib_lookup (x
, mode
, create
)
920 enum machine_mode mode
;
925 unsigned int hashval
;
927 if (GET_MODE (x
) != VOIDmode
)
930 if (GET_CODE (x
) == VALUE
)
931 return CSELIB_VAL_PTR (x
);
933 if (GET_CODE (x
) == REG
)
936 unsigned int i
= REGNO (x
);
939 if (l
&& l
->elt
== NULL
)
941 for (; l
; l
= l
->next
)
942 if (mode
== GET_MODE (l
->elt
->u
.val_rtx
))
948 if (i
< FIRST_PSEUDO_REGISTER
)
950 unsigned int n
= HARD_REGNO_NREGS (i
, mode
);
952 if (n
> max_value_regs
)
956 e
= new_cselib_val (++next_unknown_value
, GET_MODE (x
));
957 e
->locs
= new_elt_loc_list (e
->locs
, x
);
958 if (REG_VALUES (i
) == 0)
960 /* Maintain the invariant that the first entry of
961 REG_VALUES, if present, must be the value used to set the
962 register, or NULL. */
963 VARRAY_PUSH_UINT (used_regs
, i
);
964 REG_VALUES (i
) = new_elt_list (REG_VALUES (i
), NULL
);
966 REG_VALUES (i
)->next
= new_elt_list (REG_VALUES (i
)->next
, e
);
967 slot
= htab_find_slot_with_hash (hash_table
, x
, e
->value
, INSERT
);
972 if (GET_CODE (x
) == MEM
)
973 return cselib_lookup_mem (x
, create
);
975 hashval
= hash_rtx (x
, mode
, create
);
976 /* Can't even create if hashing is not possible. */
980 slot
= htab_find_slot_with_hash (hash_table
, wrap_constant (mode
, x
),
981 hashval
, create
? INSERT
: NO_INSERT
);
985 e
= (cselib_val
*) *slot
;
989 e
= new_cselib_val (hashval
, mode
);
991 /* We have to fill the slot before calling cselib_subst_to_values:
992 the hash table is inconsistent until we do so, and
993 cselib_subst_to_values will need to do lookups. */
995 e
->locs
= new_elt_loc_list (e
->locs
, cselib_subst_to_values (x
));
999 /* Invalidate any entries in reg_values that overlap REGNO. This is called
1000 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
1001 is used to determine how many hard registers are being changed. If MODE
1002 is VOIDmode, then only REGNO is being changed; this is used when
1003 invalidating call clobbered registers across a call. */
1006 cselib_invalidate_regno (regno
, mode
)
1008 enum machine_mode mode
;
1010 unsigned int endregno
;
1013 /* If we see pseudos after reload, something is _wrong_. */
1014 if (reload_completed
&& regno
>= FIRST_PSEUDO_REGISTER
1015 && reg_renumber
[regno
] >= 0)
1018 /* Determine the range of registers that must be invalidated. For
1019 pseudos, only REGNO is affected. For hard regs, we must take MODE
1020 into account, and we must also invalidate lower register numbers
1021 if they contain values that overlap REGNO. */
1022 if (regno
< FIRST_PSEUDO_REGISTER
)
1024 if (mode
== VOIDmode
)
1027 if (regno
< max_value_regs
)
1030 i
= regno
- max_value_regs
;
1032 endregno
= regno
+ HARD_REGNO_NREGS (regno
, mode
);
1037 endregno
= regno
+ 1;
1040 for (; i
< endregno
; i
++)
1042 struct elt_list
**l
= ®_VALUES (i
);
1044 /* Go through all known values for this reg; if it overlaps the range
1045 we're invalidating, remove the value. */
1048 cselib_val
*v
= (*l
)->elt
;
1049 struct elt_loc_list
**p
;
1050 unsigned int this_last
= i
;
1052 if (i
< FIRST_PSEUDO_REGISTER
&& v
!= NULL
)
1053 this_last
+= HARD_REGNO_NREGS (i
, GET_MODE (v
->u
.val_rtx
)) - 1;
1055 if (this_last
< regno
|| v
== NULL
)
1061 /* We have an overlap. */
1062 if (*l
== REG_VALUES (i
))
1064 /* Maintain the invariant that the first entry of
1065 REG_VALUES, if present, must be the value used to set
1066 the register, or NULL. This is also nice because
1067 then we won't push the same regno onto user_regs
1073 unchain_one_elt_list (l
);
1075 /* Now, we clear the mapping from value to reg. It must exist, so
1076 this code will crash intentionally if it doesn't. */
1077 for (p
= &v
->locs
; ; p
= &(*p
)->next
)
1081 if (GET_CODE (x
) == REG
&& REGNO (x
) == i
)
1083 unchain_one_elt_loc_list (p
);
1093 /* The memory at address MEM_BASE is being changed.
1094 Return whether this change will invalidate VAL. */
1097 cselib_mem_conflict_p (mem_base
, val
)
1105 code
= GET_CODE (val
);
1108 /* Get rid of a few simple cases quickly. */
1122 if (GET_MODE (mem_base
) == BLKmode
1123 || GET_MODE (val
) == BLKmode
1124 || anti_dependence (val
, mem_base
))
1127 /* The address may contain nested MEMs. */
1134 fmt
= GET_RTX_FORMAT (code
);
1135 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1139 if (cselib_mem_conflict_p (mem_base
, XEXP (val
, i
)))
1142 else if (fmt
[i
] == 'E')
1143 for (j
= 0; j
< XVECLEN (val
, i
); j
++)
1144 if (cselib_mem_conflict_p (mem_base
, XVECEXP (val
, i
, j
)))
1151 /* Invalidate any locations in the table which are changed because of a
1152 store to MEM_RTX. If this is called because of a non-const call
1153 instruction, MEM_RTX is (mem:BLK const0_rtx). */
1156 cselib_invalidate_mem (mem_rtx
)
1159 cselib_val
**vp
, *v
, *next
;
1161 vp
= &first_containing_mem
;
1162 for (v
= *vp
; v
!= &dummy_val
; v
= next
)
1164 bool has_mem
= false;
1165 struct elt_loc_list
**p
= &v
->locs
;
1166 int had_locs
= v
->locs
!= 0;
1172 struct elt_list
**mem_chain
;
1174 /* MEMs may occur in locations only at the top level; below
1175 that every MEM or REG is substituted by its VALUE. */
1176 if (GET_CODE (x
) != MEM
)
1181 if (! cselib_mem_conflict_p (mem_rtx
, x
))
1188 /* This one overlaps. */
1189 /* We must have a mapping from this MEM's address to the
1190 value (E). Remove that, too. */
1191 addr
= cselib_lookup (XEXP (x
, 0), VOIDmode
, 0);
1192 mem_chain
= &addr
->addr_list
;
1195 if ((*mem_chain
)->elt
== v
)
1197 unchain_one_elt_list (mem_chain
);
1201 mem_chain
= &(*mem_chain
)->next
;
1204 unchain_one_elt_loc_list (p
);
1207 if (had_locs
&& v
->locs
== 0)
1210 next
= v
->next_containing_mem
;
1214 vp
= &(*vp
)->next_containing_mem
;
1217 v
->next_containing_mem
= NULL
;
1222 /* Invalidate DEST, which is being assigned to or clobbered. The second and
1223 the third parameter exist so that this function can be passed to
1224 note_stores; they are ignored. */
1227 cselib_invalidate_rtx (dest
, ignore
, data
)
1229 rtx ignore ATTRIBUTE_UNUSED
;
1230 void *data ATTRIBUTE_UNUSED
;
1232 while (GET_CODE (dest
) == STRICT_LOW_PART
|| GET_CODE (dest
) == SIGN_EXTRACT
1233 || GET_CODE (dest
) == ZERO_EXTRACT
|| GET_CODE (dest
) == SUBREG
)
1234 dest
= XEXP (dest
, 0);
1236 if (GET_CODE (dest
) == REG
)
1237 cselib_invalidate_regno (REGNO (dest
), GET_MODE (dest
));
1238 else if (GET_CODE (dest
) == MEM
)
1239 cselib_invalidate_mem (dest
);
1241 /* Some machines don't define AUTO_INC_DEC, but they still use push
1242 instructions. We need to catch that case here in order to
1243 invalidate the stack pointer correctly. Note that invalidating
1244 the stack pointer is different from invalidating DEST. */
1245 if (push_operand (dest
, GET_MODE (dest
)))
1246 cselib_invalidate_rtx (stack_pointer_rtx
, NULL_RTX
, NULL
);
1249 /* Record the result of a SET instruction. DEST is being set; the source
1250 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
1251 describes its address. */
1254 cselib_record_set (dest
, src_elt
, dest_addr_elt
)
1256 cselib_val
*src_elt
, *dest_addr_elt
;
1258 int dreg
= GET_CODE (dest
) == REG
? (int) REGNO (dest
) : -1;
1260 if (src_elt
== 0 || side_effects_p (dest
))
1265 if (dreg
< FIRST_PSEUDO_REGISTER
)
1267 unsigned int n
= HARD_REGNO_NREGS (dreg
, GET_MODE (dest
));
1269 if (n
> max_value_regs
)
1273 if (REG_VALUES (dreg
) == 0)
1275 VARRAY_PUSH_UINT (used_regs
, dreg
);
1276 REG_VALUES (dreg
) = new_elt_list (REG_VALUES (dreg
), src_elt
);
1280 if (REG_VALUES (dreg
)->elt
== 0)
1281 REG_VALUES (dreg
)->elt
= src_elt
;
1283 /* The register should have been invalidated. */
1287 if (src_elt
->locs
== 0)
1289 src_elt
->locs
= new_elt_loc_list (src_elt
->locs
, dest
);
1291 else if (GET_CODE (dest
) == MEM
&& dest_addr_elt
!= 0)
1293 if (src_elt
->locs
== 0)
1295 add_mem_for_addr (dest_addr_elt
, src_elt
, dest
);
1299 /* Describe a single set that is part of an insn. */
1304 cselib_val
*src_elt
;
1305 cselib_val
*dest_addr_elt
;
1308 /* There is no good way to determine how many elements there can be
1309 in a PARALLEL. Since it's fairly cheap, use a really large number. */
1310 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
1312 /* Record the effects of any sets in INSN. */
1314 cselib_record_sets (insn
)
1319 struct set sets
[MAX_SETS
];
1320 rtx body
= PATTERN (insn
);
1323 body
= PATTERN (insn
);
1324 if (GET_CODE (body
) == COND_EXEC
)
1326 cond
= COND_EXEC_TEST (body
);
1327 body
= COND_EXEC_CODE (body
);
1330 /* Find all sets. */
1331 if (GET_CODE (body
) == SET
)
1333 sets
[0].src
= SET_SRC (body
);
1334 sets
[0].dest
= SET_DEST (body
);
1337 else if (GET_CODE (body
) == PARALLEL
)
1339 /* Look through the PARALLEL and record the values being
1340 set, if possible. Also handle any CLOBBERs. */
1341 for (i
= XVECLEN (body
, 0) - 1; i
>= 0; --i
)
1343 rtx x
= XVECEXP (body
, 0, i
);
1345 if (GET_CODE (x
) == SET
)
1347 sets
[n_sets
].src
= SET_SRC (x
);
1348 sets
[n_sets
].dest
= SET_DEST (x
);
1354 /* Look up the values that are read. Do this before invalidating the
1355 locations that are written. */
1356 for (i
= 0; i
< n_sets
; i
++)
1358 rtx dest
= sets
[i
].dest
;
1360 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
1361 the low part after invalidating any knowledge about larger modes. */
1362 if (GET_CODE (sets
[i
].dest
) == STRICT_LOW_PART
)
1363 sets
[i
].dest
= dest
= XEXP (dest
, 0);
1365 /* We don't know how to record anything but REG or MEM. */
1366 if (GET_CODE (dest
) == REG
|| GET_CODE (dest
) == MEM
)
1368 rtx src
= sets
[i
].src
;
1370 src
= gen_rtx_IF_THEN_ELSE (GET_MODE (src
), cond
, src
, dest
);
1371 sets
[i
].src_elt
= cselib_lookup (src
, GET_MODE (dest
), 1);
1372 if (GET_CODE (dest
) == MEM
)
1373 sets
[i
].dest_addr_elt
= cselib_lookup (XEXP (dest
, 0), Pmode
, 1);
1375 sets
[i
].dest_addr_elt
= 0;
1379 /* Invalidate all locations written by this insn. Note that the elts we
1380 looked up in the previous loop aren't affected, just some of their
1381 locations may go away. */
1382 note_stores (body
, cselib_invalidate_rtx
, NULL
);
1384 /* Now enter the equivalences in our tables. */
1385 for (i
= 0; i
< n_sets
; i
++)
1387 rtx dest
= sets
[i
].dest
;
1388 if (GET_CODE (dest
) == REG
|| GET_CODE (dest
) == MEM
)
1389 cselib_record_set (dest
, sets
[i
].src_elt
, sets
[i
].dest_addr_elt
);
1393 /* Record the effects of INSN. */
1396 cselib_process_insn (insn
)
1402 if (find_reg_note (insn
, REG_LIBCALL
, NULL
))
1403 cselib_current_insn_in_libcall
= true;
1404 if (find_reg_note (insn
, REG_RETVAL
, NULL
))
1405 cselib_current_insn_in_libcall
= false;
1406 cselib_current_insn
= insn
;
1408 /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp. */
1409 if (GET_CODE (insn
) == CODE_LABEL
1410 || (GET_CODE (insn
) == CALL_INSN
1411 && find_reg_note (insn
, REG_SETJMP
, NULL
))
1412 || (GET_CODE (insn
) == INSN
1413 && GET_CODE (PATTERN (insn
)) == ASM_OPERANDS
1414 && MEM_VOLATILE_P (PATTERN (insn
))))
1420 if (! INSN_P (insn
))
1422 cselib_current_insn
= 0;
1426 /* If this is a call instruction, forget anything stored in a
1427 call clobbered register, or, if this is not a const call, in
1429 if (GET_CODE (insn
) == CALL_INSN
)
1431 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1432 if (call_used_regs
[i
])
1433 cselib_invalidate_regno (i
, reg_raw_mode
[i
]);
1435 if (! CONST_OR_PURE_CALL_P (insn
))
1436 cselib_invalidate_mem (callmem
);
1439 cselib_record_sets (insn
);
1442 /* Clobber any registers which appear in REG_INC notes. We
1443 could keep track of the changes to their values, but it is
1444 unlikely to help. */
1445 for (x
= REG_NOTES (insn
); x
; x
= XEXP (x
, 1))
1446 if (REG_NOTE_KIND (x
) == REG_INC
)
1447 cselib_invalidate_rtx (XEXP (x
, 0), NULL_RTX
, NULL
);
1450 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
1451 after we have processed the insn. */
1452 if (GET_CODE (insn
) == CALL_INSN
)
1453 for (x
= CALL_INSN_FUNCTION_USAGE (insn
); x
; x
= XEXP (x
, 1))
1454 if (GET_CODE (XEXP (x
, 0)) == CLOBBER
)
1455 cselib_invalidate_rtx (XEXP (XEXP (x
, 0), 0), NULL_RTX
, NULL
);
1457 cselib_current_insn
= 0;
1459 if (n_useless_values
> MAX_USELESS_VALUES
)
1460 remove_useless_values ();
1463 /* Make sure our varrays are big enough. Not called from any cselib routines;
1464 it must be called by the user if it allocated new registers. */
1467 cselib_update_varray_sizes ()
1469 unsigned int nregs
= max_reg_num ();
1471 if (nregs
== cselib_nregs
)
1474 cselib_nregs
= nregs
;
1475 VARRAY_GROW (reg_values
, nregs
);
1476 VARRAY_GROW (used_regs
, nregs
);
1479 /* Initialize cselib for one pass. The caller must also call
1480 init_alias_analysis. */
1485 /* This is only created once. */
1487 callmem
= gen_rtx_MEM (BLKmode
, const0_rtx
);
1489 cselib_nregs
= max_reg_num ();
1490 if (reg_values_old
!= NULL
&& VARRAY_SIZE (reg_values_old
) >= cselib_nregs
)
1492 reg_values
= reg_values_old
;
1493 used_regs
= used_regs_old
;
1497 VARRAY_ELT_LIST_INIT (reg_values
, cselib_nregs
, "reg_values");
1498 VARRAY_UINT_INIT (used_regs
, cselib_nregs
, "used_regs");
1500 hash_table
= htab_create_ggc (31, get_value_hash
, entry_and_rtx_equal_p
,
1502 cselib_current_insn_in_libcall
= false;
1505 /* Called when the current user is done with cselib. */
1511 reg_values_old
= reg_values
;
1513 used_regs_old
= used_regs
;
1516 n_useless_values
= 0;
1517 next_unknown_value
= 0;
1520 #include "gt-cselib.h"