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 (const void *, const void *);
44 static hashval_t
get_value_hash (const void *);
45 static struct elt_list
*new_elt_list (struct elt_list
*, cselib_val
*);
46 static struct elt_loc_list
*new_elt_loc_list (struct elt_loc_list
*, rtx
);
47 static void unchain_one_value (cselib_val
*);
48 static void unchain_one_elt_list (struct elt_list
**);
49 static void unchain_one_elt_loc_list (struct elt_loc_list
**);
50 static void clear_table (void);
51 static int discard_useless_locs (void **, void *);
52 static int discard_useless_values (void **, void *);
53 static void remove_useless_values (void);
54 static rtx
wrap_constant (enum machine_mode
, rtx
);
55 static unsigned int hash_rtx (rtx
, enum machine_mode
, int);
56 static cselib_val
*new_cselib_val (unsigned int, enum machine_mode
);
57 static void add_mem_for_addr (cselib_val
*, cselib_val
*, rtx
);
58 static cselib_val
*cselib_lookup_mem (rtx
, int);
59 static void cselib_invalidate_regno (unsigned int, enum machine_mode
);
60 static int cselib_mem_conflict_p (rtx
, rtx
);
61 static void cselib_invalidate_mem (rtx
);
62 static void cselib_invalidate_rtx (rtx
, rtx
, void *);
63 static void cselib_record_set (rtx
, cselib_val
*, cselib_val
*);
64 static void cselib_record_sets (rtx
);
66 /* There are three ways in which cselib can look up an rtx:
67 - for a REG, the reg_values table (which is indexed by regno) is used
68 - for a MEM, we recursively look up its address and then follow the
69 addr_list of that value
70 - for everything else, we compute a hash value and go through the hash
71 table. Since different rtx's can still have the same hash value,
72 this involves walking the table entries for a given value and comparing
73 the locations of the entries with the rtx we are looking up. */
75 /* A table that enables us to look up elts by their value. */
76 static GTY((param_is (cselib_val
))) htab_t hash_table
;
78 /* This is a global so we don't have to pass this through every function.
79 It is used in new_elt_loc_list to set SETTING_INSN. */
80 static rtx cselib_current_insn
;
81 static bool cselib_current_insn_in_libcall
;
83 /* Every new unknown value gets a unique number. */
84 static unsigned int next_unknown_value
;
86 /* The number of registers we had when the varrays were last resized. */
87 static unsigned int cselib_nregs
;
89 /* Count values without known locations. Whenever this grows too big, we
90 remove these useless values from the table. */
91 static int n_useless_values
;
93 /* Number of useless values before we remove them from the hash table. */
94 #define MAX_USELESS_VALUES 32
96 /* This table maps from register number to values. It does not
97 contain pointers to cselib_val structures, but rather elt_lists.
98 The purpose is to be able to refer to the same register in
99 different modes. The first element of the list defines the mode in
100 which the register was set; if the mode is unknown or the value is
101 no longer valid in that mode, ELT will be NULL for the first
103 static GTY(()) varray_type reg_values
;
104 static GTY((deletable (""))) varray_type reg_values_old
;
105 #define REG_VALUES(I) VARRAY_ELT_LIST (reg_values, (I))
107 /* The largest number of hard regs used by any entry added to the
108 REG_VALUES table. Cleared on each clear_table() invocation. */
109 static unsigned int max_value_regs
;
111 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
112 in clear_table() for fast emptying. */
113 static GTY(()) varray_type used_regs
;
114 static GTY((deletable (""))) varray_type used_regs_old
;
116 /* We pass this to cselib_invalidate_mem to invalidate all of
117 memory for a non-const call instruction. */
118 static GTY(()) rtx callmem
;
120 /* Caches for unused structures. */
121 static GTY((deletable (""))) cselib_val
*empty_vals
;
122 static GTY((deletable (""))) struct elt_list
*empty_elt_lists
;
123 static GTY((deletable (""))) struct elt_loc_list
*empty_elt_loc_lists
;
125 /* Set by discard_useless_locs if it deleted the last location of any
127 static int values_became_useless
;
129 /* Used as stop element of the containing_mem list so we can check
130 presence in the list by checking the next pointer. */
131 static cselib_val dummy_val
;
133 /* Used to list all values that contain memory reference.
134 May or may not contain the useless values - the list is compacted
135 each time memory is invalidated. */
136 static cselib_val
*first_containing_mem
= &dummy_val
;
139 /* Allocate a struct elt_list and fill in its two elements with the
142 static struct elt_list
*
143 new_elt_list (struct elt_list
*next
, cselib_val
*elt
)
145 struct elt_list
*el
= empty_elt_lists
;
148 empty_elt_lists
= el
->next
;
150 el
= ggc_alloc (sizeof (struct elt_list
));
156 /* Allocate a struct elt_loc_list and fill in its two elements with the
159 static struct elt_loc_list
*
160 new_elt_loc_list (struct elt_loc_list
*next
, rtx loc
)
162 struct elt_loc_list
*el
= empty_elt_loc_lists
;
165 empty_elt_loc_lists
= el
->next
;
167 el
= ggc_alloc (sizeof (struct elt_loc_list
));
170 el
->setting_insn
= cselib_current_insn
;
171 el
->in_libcall
= cselib_current_insn_in_libcall
;
175 /* The elt_list at *PL is no longer needed. Unchain it and free its
179 unchain_one_elt_list (struct elt_list
**pl
)
181 struct elt_list
*l
= *pl
;
184 l
->next
= empty_elt_lists
;
188 /* Likewise for elt_loc_lists. */
191 unchain_one_elt_loc_list (struct elt_loc_list
**pl
)
193 struct elt_loc_list
*l
= *pl
;
196 l
->next
= empty_elt_loc_lists
;
197 empty_elt_loc_lists
= l
;
200 /* Likewise for cselib_vals. This also frees the addr_list associated with
204 unchain_one_value (cselib_val
*v
)
207 unchain_one_elt_list (&v
->addr_list
);
209 v
->u
.next_free
= empty_vals
;
213 /* Remove all entries from the hash table. Also used during
214 initialization. If CLEAR_ALL isn't set, then only clear the entries
215 which are known to have been used. */
222 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (used_regs
); i
++)
223 REG_VALUES (VARRAY_UINT (used_regs
, i
)) = 0;
227 VARRAY_POP_ALL (used_regs
);
229 htab_empty (hash_table
);
231 n_useless_values
= 0;
233 next_unknown_value
= 0;
235 first_containing_mem
= &dummy_val
;
238 /* The equality test for our hash table. The first argument ENTRY is a table
239 element (i.e. a cselib_val), while the second arg X is an rtx. We know
240 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
241 CONST of an appropriate mode. */
244 entry_and_rtx_equal_p (const void *entry
, const void *x_arg
)
246 struct elt_loc_list
*l
;
247 const cselib_val
*v
= (const cselib_val
*) entry
;
249 enum machine_mode mode
= GET_MODE (x
);
251 if (GET_CODE (x
) == CONST_INT
252 || (mode
== VOIDmode
&& GET_CODE (x
) == CONST_DOUBLE
))
254 if (mode
!= GET_MODE (v
->u
.val_rtx
))
257 /* Unwrap X if necessary. */
258 if (GET_CODE (x
) == CONST
259 && (GET_CODE (XEXP (x
, 0)) == CONST_INT
260 || GET_CODE (XEXP (x
, 0)) == CONST_DOUBLE
))
263 /* We don't guarantee that distinct rtx's have different hash values,
264 so we need to do a comparison. */
265 for (l
= v
->locs
; l
; l
= l
->next
)
266 if (rtx_equal_for_cselib_p (l
->loc
, x
))
272 /* The hash function for our hash table. The value is always computed with
273 hash_rtx when adding an element; this function just extracts the hash
274 value from a cselib_val structure. */
277 get_value_hash (const void *entry
)
279 const cselib_val
*v
= (const cselib_val
*) entry
;
283 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
284 only return true for values which point to a cselib_val whose value
285 element has been set to zero, which implies the cselib_val will be
289 references_value_p (rtx x
, int only_useless
)
291 enum rtx_code code
= GET_CODE (x
);
292 const char *fmt
= GET_RTX_FORMAT (code
);
295 if (GET_CODE (x
) == VALUE
296 && (! only_useless
|| CSELIB_VAL_PTR (x
)->locs
== 0))
299 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
301 if (fmt
[i
] == 'e' && references_value_p (XEXP (x
, i
), only_useless
))
303 else if (fmt
[i
] == 'E')
304 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
305 if (references_value_p (XVECEXP (x
, i
, j
), only_useless
))
312 /* For all locations found in X, delete locations that reference useless
313 values (i.e. values without any location). Called through
317 discard_useless_locs (void **x
, void *info ATTRIBUTE_UNUSED
)
319 cselib_val
*v
= (cselib_val
*)*x
;
320 struct elt_loc_list
**p
= &v
->locs
;
321 int had_locs
= v
->locs
!= 0;
325 if (references_value_p ((*p
)->loc
, 1))
326 unchain_one_elt_loc_list (p
);
331 if (had_locs
&& v
->locs
== 0)
334 values_became_useless
= 1;
339 /* If X is a value with no locations, remove it from the hashtable. */
342 discard_useless_values (void **x
, void *info ATTRIBUTE_UNUSED
)
344 cselib_val
*v
= (cselib_val
*)*x
;
348 htab_clear_slot (hash_table
, x
);
349 unchain_one_value (v
);
356 /* Clean out useless values (i.e. those which no longer have locations
357 associated with them) from the hash table. */
360 remove_useless_values (void)
363 /* First pass: eliminate locations that reference the value. That in
364 turn can make more values useless. */
367 values_became_useless
= 0;
368 htab_traverse (hash_table
, discard_useless_locs
, 0);
370 while (values_became_useless
);
372 /* Second pass: actually remove the values. */
373 htab_traverse (hash_table
, discard_useless_values
, 0);
375 p
= &first_containing_mem
;
376 for (v
= *p
; v
!= &dummy_val
; v
= v
->next_containing_mem
)
380 p
= &(*p
)->next_containing_mem
;
384 if (n_useless_values
!= 0)
388 /* Return the mode in which a register was last set. If X is not a
389 register, return its mode. If the mode in which the register was
390 set is not known, or the value was already clobbered, return
394 cselib_reg_set_mode (rtx x
)
396 if (GET_CODE (x
) != REG
)
399 if (REG_VALUES (REGNO (x
)) == NULL
400 || REG_VALUES (REGNO (x
))->elt
== NULL
)
403 return GET_MODE (REG_VALUES (REGNO (x
))->elt
->u
.val_rtx
);
406 /* Return nonzero if we can prove that X and Y contain the same value, taking
407 our gathered information into account. */
410 rtx_equal_for_cselib_p (rtx x
, rtx y
)
416 if (GET_CODE (x
) == REG
|| GET_CODE (x
) == MEM
)
418 cselib_val
*e
= cselib_lookup (x
, GET_MODE (x
), 0);
424 if (GET_CODE (y
) == REG
|| GET_CODE (y
) == MEM
)
426 cselib_val
*e
= cselib_lookup (y
, GET_MODE (y
), 0);
435 if (GET_CODE (x
) == VALUE
&& GET_CODE (y
) == VALUE
)
436 return CSELIB_VAL_PTR (x
) == CSELIB_VAL_PTR (y
);
438 if (GET_CODE (x
) == VALUE
)
440 cselib_val
*e
= CSELIB_VAL_PTR (x
);
441 struct elt_loc_list
*l
;
443 for (l
= e
->locs
; l
; l
= l
->next
)
447 /* Avoid infinite recursion. */
448 if (GET_CODE (t
) == REG
|| GET_CODE (t
) == MEM
)
450 else if (rtx_equal_for_cselib_p (t
, y
))
457 if (GET_CODE (y
) == VALUE
)
459 cselib_val
*e
= CSELIB_VAL_PTR (y
);
460 struct elt_loc_list
*l
;
462 for (l
= e
->locs
; l
; l
= l
->next
)
466 if (GET_CODE (t
) == REG
|| GET_CODE (t
) == MEM
)
468 else if (rtx_equal_for_cselib_p (x
, t
))
475 if (GET_CODE (x
) != GET_CODE (y
) || GET_MODE (x
) != GET_MODE (y
))
478 /* This won't be handled correctly by the code below. */
479 if (GET_CODE (x
) == LABEL_REF
)
480 return XEXP (x
, 0) == XEXP (y
, 0);
483 fmt
= GET_RTX_FORMAT (code
);
485 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
492 if (XWINT (x
, i
) != XWINT (y
, i
))
498 if (XINT (x
, i
) != XINT (y
, i
))
504 /* Two vectors must have the same length. */
505 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
508 /* And the corresponding elements must match. */
509 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
510 if (! rtx_equal_for_cselib_p (XVECEXP (x
, i
, j
),
516 if (! rtx_equal_for_cselib_p (XEXP (x
, i
), XEXP (y
, i
)))
522 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
527 /* These are just backpointers, so they don't matter. */
534 /* It is believed that rtx's at this level will never
535 contain anything but integers and other rtx's,
536 except for within LABEL_REFs and SYMBOL_REFs. */
544 /* We need to pass down the mode of constants through the hash table
545 functions. For that purpose, wrap them in a CONST of the appropriate
548 wrap_constant (enum machine_mode mode
, rtx x
)
550 if (GET_CODE (x
) != CONST_INT
551 && (GET_CODE (x
) != CONST_DOUBLE
|| GET_MODE (x
) != VOIDmode
))
553 if (mode
== VOIDmode
)
555 return gen_rtx_CONST (mode
, x
);
558 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
559 For registers and memory locations, we look up their cselib_val structure
560 and return its VALUE element.
561 Possible reasons for return 0 are: the object is volatile, or we couldn't
562 find a register or memory location in the table and CREATE is zero. If
563 CREATE is nonzero, table elts are created for regs and mem.
564 MODE is used in hashing for CONST_INTs only;
565 otherwise the mode of X is used. */
568 hash_rtx (rtx x
, enum machine_mode mode
, int create
)
574 unsigned int hash
= 0;
577 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
583 e
= cselib_lookup (x
, GET_MODE (x
), create
);
590 hash
+= ((unsigned) CONST_INT
<< 7) + (unsigned) mode
+ INTVAL (x
);
591 return hash
? hash
: (unsigned int) CONST_INT
;
594 /* This is like the general case, except that it only counts
595 the integers representing the constant. */
596 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
597 if (GET_MODE (x
) != VOIDmode
)
598 hash
+= real_hash (CONST_DOUBLE_REAL_VALUE (x
));
600 hash
+= ((unsigned) CONST_DOUBLE_LOW (x
)
601 + (unsigned) CONST_DOUBLE_HIGH (x
));
602 return hash
? hash
: (unsigned int) CONST_DOUBLE
;
609 units
= CONST_VECTOR_NUNITS (x
);
611 for (i
= 0; i
< units
; ++i
)
613 elt
= CONST_VECTOR_ELT (x
, i
);
614 hash
+= hash_rtx (elt
, GET_MODE (elt
), 0);
620 /* Assume there is only one rtx object for any given label. */
623 += ((unsigned) LABEL_REF
<< 7) + (unsigned long) XEXP (x
, 0);
624 return hash
? hash
: (unsigned int) LABEL_REF
;
628 += ((unsigned) SYMBOL_REF
<< 7) + (unsigned long) XSTR (x
, 0);
629 return hash
? hash
: (unsigned int) SYMBOL_REF
;
640 case UNSPEC_VOLATILE
:
644 if (MEM_VOLATILE_P (x
))
653 i
= GET_RTX_LENGTH (code
) - 1;
654 fmt
= GET_RTX_FORMAT (code
);
659 rtx tem
= XEXP (x
, i
);
660 unsigned int tem_hash
= hash_rtx (tem
, 0, create
);
667 else if (fmt
[i
] == 'E')
668 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
670 unsigned int tem_hash
= hash_rtx (XVECEXP (x
, i
, j
), 0, create
);
677 else if (fmt
[i
] == 's')
679 const unsigned char *p
= (const unsigned char *) XSTR (x
, i
);
685 else if (fmt
[i
] == 'i')
687 else if (fmt
[i
] == '0' || fmt
[i
] == 't')
693 return hash
? hash
: 1 + (unsigned int) GET_CODE (x
);
696 /* Create a new value structure for VALUE and initialize it. The mode of the
700 new_cselib_val (unsigned int value
, enum machine_mode mode
)
702 cselib_val
*e
= empty_vals
;
705 empty_vals
= e
->u
.next_free
;
707 e
= ggc_alloc (sizeof (cselib_val
));
713 e
->u
.val_rtx
= gen_rtx_VALUE (mode
);
714 CSELIB_VAL_PTR (e
->u
.val_rtx
) = e
;
717 e
->next_containing_mem
= 0;
721 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
722 contains the data at this address. X is a MEM that represents the
723 value. Update the two value structures to represent this situation. */
726 add_mem_for_addr (cselib_val
*addr_elt
, cselib_val
*mem_elt
, rtx x
)
728 struct elt_loc_list
*l
;
730 /* Avoid duplicates. */
731 for (l
= mem_elt
->locs
; l
; l
= l
->next
)
732 if (GET_CODE (l
->loc
) == MEM
733 && CSELIB_VAL_PTR (XEXP (l
->loc
, 0)) == addr_elt
)
736 addr_elt
->addr_list
= new_elt_list (addr_elt
->addr_list
, mem_elt
);
738 = new_elt_loc_list (mem_elt
->locs
,
739 replace_equiv_address_nv (x
, addr_elt
->u
.val_rtx
));
740 if (mem_elt
->next_containing_mem
== NULL
)
742 mem_elt
->next_containing_mem
= first_containing_mem
;
743 first_containing_mem
= mem_elt
;
747 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
748 If CREATE, make a new one if we haven't seen it before. */
751 cselib_lookup_mem (rtx x
, int create
)
753 enum machine_mode mode
= GET_MODE (x
);
759 if (MEM_VOLATILE_P (x
) || mode
== BLKmode
760 || (FLOAT_MODE_P (mode
) && flag_float_store
))
763 /* Look up the value for the address. */
764 addr
= cselib_lookup (XEXP (x
, 0), mode
, create
);
768 /* Find a value that describes a value of our mode at that address. */
769 for (l
= addr
->addr_list
; l
; l
= l
->next
)
770 if (GET_MODE (l
->elt
->u
.val_rtx
) == mode
)
776 mem_elt
= new_cselib_val (++next_unknown_value
, mode
);
777 add_mem_for_addr (addr
, mem_elt
, x
);
778 slot
= htab_find_slot_with_hash (hash_table
, wrap_constant (mode
, x
),
779 mem_elt
->value
, INSERT
);
784 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
785 with VALUE expressions. This way, it becomes independent of changes
786 to registers and memory.
787 X isn't actually modified; if modifications are needed, new rtl is
788 allocated. However, the return value can share rtl with X. */
791 cselib_subst_to_values (rtx x
)
793 enum rtx_code code
= GET_CODE (x
);
794 const char *fmt
= GET_RTX_FORMAT (code
);
803 l
= REG_VALUES (REGNO (x
));
804 if (l
&& l
->elt
== NULL
)
806 for (; l
; l
= l
->next
)
807 if (GET_MODE (l
->elt
->u
.val_rtx
) == GET_MODE (x
))
808 return l
->elt
->u
.val_rtx
;
813 e
= cselib_lookup_mem (x
, 0);
816 /* This happens for autoincrements. Assign a value that doesn't
818 e
= new_cselib_val (++next_unknown_value
, GET_MODE (x
));
833 e
= new_cselib_val (++next_unknown_value
, GET_MODE (x
));
840 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
844 rtx t
= cselib_subst_to_values (XEXP (x
, i
));
846 if (t
!= XEXP (x
, i
) && x
== copy
)
847 copy
= shallow_copy_rtx (x
);
851 else if (fmt
[i
] == 'E')
855 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
857 rtx t
= cselib_subst_to_values (XVECEXP (x
, i
, j
));
859 if (t
!= XVECEXP (x
, i
, j
) && XVEC (x
, i
) == XVEC (copy
, i
))
862 copy
= shallow_copy_rtx (x
);
864 XVEC (copy
, i
) = rtvec_alloc (XVECLEN (x
, i
));
865 for (k
= 0; k
< j
; k
++)
866 XVECEXP (copy
, i
, k
) = XVECEXP (x
, i
, k
);
869 XVECEXP (copy
, i
, j
) = t
;
877 /* Look up the rtl expression X in our tables and return the value it has.
878 If CREATE is zero, we return NULL if we don't know the value. Otherwise,
879 we create a new one if possible, using mode MODE if X doesn't have a mode
880 (i.e. because it's a constant). */
883 cselib_lookup (rtx x
, enum machine_mode mode
, int create
)
887 unsigned int hashval
;
889 if (GET_MODE (x
) != VOIDmode
)
892 if (GET_CODE (x
) == VALUE
)
893 return CSELIB_VAL_PTR (x
);
895 if (GET_CODE (x
) == REG
)
898 unsigned int i
= REGNO (x
);
901 if (l
&& l
->elt
== NULL
)
903 for (; l
; l
= l
->next
)
904 if (mode
== GET_MODE (l
->elt
->u
.val_rtx
))
910 if (i
< FIRST_PSEUDO_REGISTER
)
912 unsigned int n
= HARD_REGNO_NREGS (i
, mode
);
914 if (n
> max_value_regs
)
918 e
= new_cselib_val (++next_unknown_value
, GET_MODE (x
));
919 e
->locs
= new_elt_loc_list (e
->locs
, x
);
920 if (REG_VALUES (i
) == 0)
922 /* Maintain the invariant that the first entry of
923 REG_VALUES, if present, must be the value used to set the
924 register, or NULL. */
925 VARRAY_PUSH_UINT (used_regs
, i
);
926 REG_VALUES (i
) = new_elt_list (REG_VALUES (i
), NULL
);
928 REG_VALUES (i
)->next
= new_elt_list (REG_VALUES (i
)->next
, e
);
929 slot
= htab_find_slot_with_hash (hash_table
, x
, e
->value
, INSERT
);
934 if (GET_CODE (x
) == MEM
)
935 return cselib_lookup_mem (x
, create
);
937 hashval
= hash_rtx (x
, mode
, create
);
938 /* Can't even create if hashing is not possible. */
942 slot
= htab_find_slot_with_hash (hash_table
, wrap_constant (mode
, x
),
943 hashval
, create
? INSERT
: NO_INSERT
);
947 e
= (cselib_val
*) *slot
;
951 e
= new_cselib_val (hashval
, mode
);
953 /* We have to fill the slot before calling cselib_subst_to_values:
954 the hash table is inconsistent until we do so, and
955 cselib_subst_to_values will need to do lookups. */
957 e
->locs
= new_elt_loc_list (e
->locs
, cselib_subst_to_values (x
));
961 /* Invalidate any entries in reg_values that overlap REGNO. This is called
962 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
963 is used to determine how many hard registers are being changed. If MODE
964 is VOIDmode, then only REGNO is being changed; this is used when
965 invalidating call clobbered registers across a call. */
968 cselib_invalidate_regno (unsigned int regno
, enum machine_mode mode
)
970 unsigned int endregno
;
973 /* If we see pseudos after reload, something is _wrong_. */
974 if (reload_completed
&& regno
>= FIRST_PSEUDO_REGISTER
975 && reg_renumber
[regno
] >= 0)
978 /* Determine the range of registers that must be invalidated. For
979 pseudos, only REGNO is affected. For hard regs, we must take MODE
980 into account, and we must also invalidate lower register numbers
981 if they contain values that overlap REGNO. */
982 if (regno
< FIRST_PSEUDO_REGISTER
)
984 if (mode
== VOIDmode
)
987 if (regno
< max_value_regs
)
990 i
= regno
- max_value_regs
;
992 endregno
= regno
+ HARD_REGNO_NREGS (regno
, mode
);
997 endregno
= regno
+ 1;
1000 for (; i
< endregno
; i
++)
1002 struct elt_list
**l
= ®_VALUES (i
);
1004 /* Go through all known values for this reg; if it overlaps the range
1005 we're invalidating, remove the value. */
1008 cselib_val
*v
= (*l
)->elt
;
1009 struct elt_loc_list
**p
;
1010 unsigned int this_last
= i
;
1012 if (i
< FIRST_PSEUDO_REGISTER
&& v
!= NULL
)
1013 this_last
+= HARD_REGNO_NREGS (i
, GET_MODE (v
->u
.val_rtx
)) - 1;
1015 if (this_last
< regno
|| v
== NULL
)
1021 /* We have an overlap. */
1022 if (*l
== REG_VALUES (i
))
1024 /* Maintain the invariant that the first entry of
1025 REG_VALUES, if present, must be the value used to set
1026 the register, or NULL. This is also nice because
1027 then we won't push the same regno onto user_regs
1033 unchain_one_elt_list (l
);
1035 /* Now, we clear the mapping from value to reg. It must exist, so
1036 this code will crash intentionally if it doesn't. */
1037 for (p
= &v
->locs
; ; p
= &(*p
)->next
)
1041 if (GET_CODE (x
) == REG
&& REGNO (x
) == i
)
1043 unchain_one_elt_loc_list (p
);
1053 /* The memory at address MEM_BASE is being changed.
1054 Return whether this change will invalidate VAL. */
1057 cselib_mem_conflict_p (rtx mem_base
, rtx val
)
1063 code
= GET_CODE (val
);
1066 /* Get rid of a few simple cases quickly. */
1080 if (GET_MODE (mem_base
) == BLKmode
1081 || GET_MODE (val
) == BLKmode
1082 || anti_dependence (val
, mem_base
))
1085 /* The address may contain nested MEMs. */
1092 fmt
= GET_RTX_FORMAT (code
);
1093 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1097 if (cselib_mem_conflict_p (mem_base
, XEXP (val
, i
)))
1100 else if (fmt
[i
] == 'E')
1101 for (j
= 0; j
< XVECLEN (val
, i
); j
++)
1102 if (cselib_mem_conflict_p (mem_base
, XVECEXP (val
, i
, j
)))
1109 /* Invalidate any locations in the table which are changed because of a
1110 store to MEM_RTX. If this is called because of a non-const call
1111 instruction, MEM_RTX is (mem:BLK const0_rtx). */
1114 cselib_invalidate_mem (rtx mem_rtx
)
1116 cselib_val
**vp
, *v
, *next
;
1118 vp
= &first_containing_mem
;
1119 for (v
= *vp
; v
!= &dummy_val
; v
= next
)
1121 bool has_mem
= false;
1122 struct elt_loc_list
**p
= &v
->locs
;
1123 int had_locs
= v
->locs
!= 0;
1129 struct elt_list
**mem_chain
;
1131 /* MEMs may occur in locations only at the top level; below
1132 that every MEM or REG is substituted by its VALUE. */
1133 if (GET_CODE (x
) != MEM
)
1138 if (! cselib_mem_conflict_p (mem_rtx
, x
))
1145 /* This one overlaps. */
1146 /* We must have a mapping from this MEM's address to the
1147 value (E). Remove that, too. */
1148 addr
= cselib_lookup (XEXP (x
, 0), VOIDmode
, 0);
1149 mem_chain
= &addr
->addr_list
;
1152 if ((*mem_chain
)->elt
== v
)
1154 unchain_one_elt_list (mem_chain
);
1158 mem_chain
= &(*mem_chain
)->next
;
1161 unchain_one_elt_loc_list (p
);
1164 if (had_locs
&& v
->locs
== 0)
1167 next
= v
->next_containing_mem
;
1171 vp
= &(*vp
)->next_containing_mem
;
1174 v
->next_containing_mem
= NULL
;
1179 /* Invalidate DEST, which is being assigned to or clobbered. The second and
1180 the third parameter exist so that this function can be passed to
1181 note_stores; they are ignored. */
1184 cselib_invalidate_rtx (rtx dest
, rtx ignore ATTRIBUTE_UNUSED
,
1185 void *data ATTRIBUTE_UNUSED
)
1187 while (GET_CODE (dest
) == STRICT_LOW_PART
|| GET_CODE (dest
) == SIGN_EXTRACT
1188 || GET_CODE (dest
) == ZERO_EXTRACT
|| GET_CODE (dest
) == SUBREG
)
1189 dest
= XEXP (dest
, 0);
1191 if (GET_CODE (dest
) == REG
)
1192 cselib_invalidate_regno (REGNO (dest
), GET_MODE (dest
));
1193 else if (GET_CODE (dest
) == MEM
)
1194 cselib_invalidate_mem (dest
);
1196 /* Some machines don't define AUTO_INC_DEC, but they still use push
1197 instructions. We need to catch that case here in order to
1198 invalidate the stack pointer correctly. Note that invalidating
1199 the stack pointer is different from invalidating DEST. */
1200 if (push_operand (dest
, GET_MODE (dest
)))
1201 cselib_invalidate_rtx (stack_pointer_rtx
, NULL_RTX
, NULL
);
1204 /* Record the result of a SET instruction. DEST is being set; the source
1205 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
1206 describes its address. */
1209 cselib_record_set (rtx dest
, cselib_val
*src_elt
, cselib_val
*dest_addr_elt
)
1211 int dreg
= GET_CODE (dest
) == REG
? (int) REGNO (dest
) : -1;
1213 if (src_elt
== 0 || side_effects_p (dest
))
1218 if (dreg
< FIRST_PSEUDO_REGISTER
)
1220 unsigned int n
= HARD_REGNO_NREGS (dreg
, GET_MODE (dest
));
1222 if (n
> max_value_regs
)
1226 if (REG_VALUES (dreg
) == 0)
1228 VARRAY_PUSH_UINT (used_regs
, dreg
);
1229 REG_VALUES (dreg
) = new_elt_list (REG_VALUES (dreg
), src_elt
);
1233 if (REG_VALUES (dreg
)->elt
== 0)
1234 REG_VALUES (dreg
)->elt
= src_elt
;
1236 /* The register should have been invalidated. */
1240 if (src_elt
->locs
== 0)
1242 src_elt
->locs
= new_elt_loc_list (src_elt
->locs
, dest
);
1244 else if (GET_CODE (dest
) == MEM
&& dest_addr_elt
!= 0)
1246 if (src_elt
->locs
== 0)
1248 add_mem_for_addr (dest_addr_elt
, src_elt
, dest
);
1252 /* Describe a single set that is part of an insn. */
1257 cselib_val
*src_elt
;
1258 cselib_val
*dest_addr_elt
;
1261 /* There is no good way to determine how many elements there can be
1262 in a PARALLEL. Since it's fairly cheap, use a really large number. */
1263 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
1265 /* Record the effects of any sets in INSN. */
1267 cselib_record_sets (rtx insn
)
1271 struct set sets
[MAX_SETS
];
1272 rtx body
= PATTERN (insn
);
1275 body
= PATTERN (insn
);
1276 if (GET_CODE (body
) == COND_EXEC
)
1278 cond
= COND_EXEC_TEST (body
);
1279 body
= COND_EXEC_CODE (body
);
1282 /* Find all sets. */
1283 if (GET_CODE (body
) == SET
)
1285 sets
[0].src
= SET_SRC (body
);
1286 sets
[0].dest
= SET_DEST (body
);
1289 else if (GET_CODE (body
) == PARALLEL
)
1291 /* Look through the PARALLEL and record the values being
1292 set, if possible. Also handle any CLOBBERs. */
1293 for (i
= XVECLEN (body
, 0) - 1; i
>= 0; --i
)
1295 rtx x
= XVECEXP (body
, 0, i
);
1297 if (GET_CODE (x
) == SET
)
1299 sets
[n_sets
].src
= SET_SRC (x
);
1300 sets
[n_sets
].dest
= SET_DEST (x
);
1306 /* Look up the values that are read. Do this before invalidating the
1307 locations that are written. */
1308 for (i
= 0; i
< n_sets
; i
++)
1310 rtx dest
= sets
[i
].dest
;
1312 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
1313 the low part after invalidating any knowledge about larger modes. */
1314 if (GET_CODE (sets
[i
].dest
) == STRICT_LOW_PART
)
1315 sets
[i
].dest
= dest
= XEXP (dest
, 0);
1317 /* We don't know how to record anything but REG or MEM. */
1318 if (GET_CODE (dest
) == REG
|| GET_CODE (dest
) == MEM
)
1320 rtx src
= sets
[i
].src
;
1322 src
= gen_rtx_IF_THEN_ELSE (GET_MODE (src
), cond
, src
, dest
);
1323 sets
[i
].src_elt
= cselib_lookup (src
, GET_MODE (dest
), 1);
1324 if (GET_CODE (dest
) == MEM
)
1325 sets
[i
].dest_addr_elt
= cselib_lookup (XEXP (dest
, 0), Pmode
, 1);
1327 sets
[i
].dest_addr_elt
= 0;
1331 /* Invalidate all locations written by this insn. Note that the elts we
1332 looked up in the previous loop aren't affected, just some of their
1333 locations may go away. */
1334 note_stores (body
, cselib_invalidate_rtx
, NULL
);
1336 /* Now enter the equivalences in our tables. */
1337 for (i
= 0; i
< n_sets
; i
++)
1339 rtx dest
= sets
[i
].dest
;
1340 if (GET_CODE (dest
) == REG
|| GET_CODE (dest
) == MEM
)
1341 cselib_record_set (dest
, sets
[i
].src_elt
, sets
[i
].dest_addr_elt
);
1345 /* Record the effects of INSN. */
1348 cselib_process_insn (rtx insn
)
1353 if (find_reg_note (insn
, REG_LIBCALL
, NULL
))
1354 cselib_current_insn_in_libcall
= true;
1355 if (find_reg_note (insn
, REG_RETVAL
, NULL
))
1356 cselib_current_insn_in_libcall
= false;
1357 cselib_current_insn
= insn
;
1359 /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp. */
1360 if (GET_CODE (insn
) == CODE_LABEL
1361 || (GET_CODE (insn
) == CALL_INSN
1362 && find_reg_note (insn
, REG_SETJMP
, NULL
))
1363 || (GET_CODE (insn
) == INSN
1364 && GET_CODE (PATTERN (insn
)) == ASM_OPERANDS
1365 && MEM_VOLATILE_P (PATTERN (insn
))))
1371 if (! INSN_P (insn
))
1373 cselib_current_insn
= 0;
1377 /* If this is a call instruction, forget anything stored in a
1378 call clobbered register, or, if this is not a const call, in
1380 if (GET_CODE (insn
) == CALL_INSN
)
1382 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1383 if (call_used_regs
[i
])
1384 cselib_invalidate_regno (i
, reg_raw_mode
[i
]);
1386 if (! CONST_OR_PURE_CALL_P (insn
))
1387 cselib_invalidate_mem (callmem
);
1390 cselib_record_sets (insn
);
1393 /* Clobber any registers which appear in REG_INC notes. We
1394 could keep track of the changes to their values, but it is
1395 unlikely to help. */
1396 for (x
= REG_NOTES (insn
); x
; x
= XEXP (x
, 1))
1397 if (REG_NOTE_KIND (x
) == REG_INC
)
1398 cselib_invalidate_rtx (XEXP (x
, 0), NULL_RTX
, NULL
);
1401 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
1402 after we have processed the insn. */
1403 if (GET_CODE (insn
) == CALL_INSN
)
1404 for (x
= CALL_INSN_FUNCTION_USAGE (insn
); x
; x
= XEXP (x
, 1))
1405 if (GET_CODE (XEXP (x
, 0)) == CLOBBER
)
1406 cselib_invalidate_rtx (XEXP (XEXP (x
, 0), 0), NULL_RTX
, NULL
);
1408 cselib_current_insn
= 0;
1410 if (n_useless_values
> MAX_USELESS_VALUES
)
1411 remove_useless_values ();
1414 /* Make sure our varrays are big enough. Not called from any cselib routines;
1415 it must be called by the user if it allocated new registers. */
1418 cselib_update_varray_sizes (void)
1420 unsigned int nregs
= max_reg_num ();
1422 if (nregs
== cselib_nregs
)
1425 cselib_nregs
= nregs
;
1426 VARRAY_GROW (reg_values
, nregs
);
1427 VARRAY_GROW (used_regs
, nregs
);
1430 /* Initialize cselib for one pass. The caller must also call
1431 init_alias_analysis. */
1436 /* This is only created once. */
1438 callmem
= gen_rtx_MEM (BLKmode
, const0_rtx
);
1440 cselib_nregs
= max_reg_num ();
1441 if (reg_values_old
!= NULL
&& VARRAY_SIZE (reg_values_old
) >= cselib_nregs
)
1443 reg_values
= reg_values_old
;
1444 used_regs
= used_regs_old
;
1448 VARRAY_ELT_LIST_INIT (reg_values
, cselib_nregs
, "reg_values");
1449 VARRAY_UINT_INIT (used_regs
, cselib_nregs
, "used_regs");
1451 hash_table
= htab_create_ggc (31, get_value_hash
, entry_and_rtx_equal_p
,
1453 cselib_current_insn_in_libcall
= false;
1456 /* Called when the current user is done with cselib. */
1459 cselib_finish (void)
1462 reg_values_old
= reg_values
;
1464 used_regs_old
= used_regs
;
1467 n_useless_values
= 0;
1468 next_unknown_value
= 0;
1471 #include "gt-cselib.h"