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 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 ((int));
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 int cselib_invalidate_mem_1
PARAMS ((void **, void *));
67 static void cselib_invalidate_mem
PARAMS ((rtx
));
68 static void cselib_invalidate_rtx
PARAMS ((rtx
, rtx
, void *));
69 static void cselib_record_set
PARAMS ((rtx
, cselib_val
*,
71 static void cselib_record_sets
PARAMS ((rtx
));
73 /* There are three ways in which cselib can look up an rtx:
74 - for a REG, the reg_values table (which is indexed by regno) is used
75 - for a MEM, we recursively look up its address and then follow the
76 addr_list of that value
77 - for everything else, we compute a hash value and go through the hash
78 table. Since different rtx's can still have the same hash value,
79 this involves walking the table entries for a given value and comparing
80 the locations of the entries with the rtx we are looking up. */
82 /* A table that enables us to look up elts by their value. */
83 static GTY((param_is (cselib_val
))) htab_t hash_table
;
85 /* This is a global so we don't have to pass this through every function.
86 It is used in new_elt_loc_list to set SETTING_INSN. */
87 static rtx cselib_current_insn
;
88 static bool cselib_current_insn_in_libcall
;
90 /* Every new unknown value gets a unique number. */
91 static unsigned int next_unknown_value
;
93 /* The number of registers we had when the varrays were last resized. */
94 static unsigned int cselib_nregs
;
96 /* Count values without known locations. Whenever this grows too big, we
97 remove these useless values from the table. */
98 static int n_useless_values
;
100 /* Number of useless values before we remove them from the hash table. */
101 #define MAX_USELESS_VALUES 32
103 /* This table maps from register number to values. It does not contain
104 pointers to cselib_val structures, but rather elt_lists. The purpose is
105 to be able to refer to the same register in different modes. */
106 static GTY(()) varray_type reg_values
;
107 static GTY((deletable (""))) varray_type reg_values_old
;
108 #define REG_VALUES(I) VARRAY_ELT_LIST (reg_values, (I))
110 /* The largest number of hard regs used by any entry added to the
111 REG_VALUES table. Cleared on each clear_table() invocation. */
112 static unsigned int max_value_regs
;
114 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
115 in clear_table() for fast emptying. */
116 static GTY(()) varray_type used_regs
;
117 static GTY((deletable (""))) varray_type used_regs_old
;
119 /* We pass this to cselib_invalidate_mem to invalidate all of
120 memory for a non-const call instruction. */
121 static GTY(()) rtx callmem
;
123 /* Caches for unused structures. */
124 static GTY((deletable (""))) cselib_val
*empty_vals
;
125 static GTY((deletable (""))) struct elt_list
*empty_elt_lists
;
126 static GTY((deletable (""))) struct elt_loc_list
*empty_elt_loc_lists
;
128 /* Set by discard_useless_locs if it deleted the last location of any
130 static int values_became_useless
;
133 /* Allocate a struct elt_list and fill in its two elements with the
136 static struct elt_list
*
137 new_elt_list (next
, elt
)
138 struct elt_list
*next
;
141 struct elt_list
*el
= empty_elt_lists
;
144 empty_elt_lists
= el
->next
;
146 el
= (struct elt_list
*) ggc_alloc (sizeof (struct elt_list
));
152 /* Allocate a struct elt_loc_list and fill in its two elements with the
155 static struct elt_loc_list
*
156 new_elt_loc_list (next
, loc
)
157 struct elt_loc_list
*next
;
160 struct elt_loc_list
*el
= empty_elt_loc_lists
;
163 empty_elt_loc_lists
= el
->next
;
165 el
= (struct elt_loc_list
*) ggc_alloc (sizeof (struct elt_loc_list
));
168 el
->setting_insn
= cselib_current_insn
;
169 el
->in_libcall
= cselib_current_insn_in_libcall
;
173 /* The elt_list at *PL is no longer needed. Unchain it and free its
177 unchain_one_elt_list (pl
)
178 struct elt_list
**pl
;
180 struct elt_list
*l
= *pl
;
183 l
->next
= empty_elt_lists
;
187 /* Likewise for elt_loc_lists. */
190 unchain_one_elt_loc_list (pl
)
191 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 (v
)
208 unchain_one_elt_list (&v
->addr_list
);
210 v
->u
.next_free
= empty_vals
;
214 /* Remove all entries from the hash table. Also used during
215 initialization. If CLEAR_ALL isn't set, then only clear the entries
216 which are known to have been used. */
219 clear_table (clear_all
)
225 for (i
= 0; i
< cselib_nregs
; i
++)
228 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (used_regs
); i
++)
229 REG_VALUES (VARRAY_UINT (used_regs
, i
)) = 0;
233 VARRAY_POP_ALL (used_regs
);
235 htab_empty (hash_table
);
237 n_useless_values
= 0;
239 next_unknown_value
= 0;
242 /* The equality test for our hash table. The first argument ENTRY is a table
243 element (i.e. a cselib_val), while the second arg X is an rtx. We know
244 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
245 CONST of an appropriate mode. */
248 entry_and_rtx_equal_p (entry
, x_arg
)
249 const void *entry
, *x_arg
;
251 struct elt_loc_list
*l
;
252 const cselib_val
*v
= (const cselib_val
*) entry
;
254 enum machine_mode mode
= GET_MODE (x
);
256 if (GET_CODE (x
) == CONST_INT
257 || (mode
== VOIDmode
&& GET_CODE (x
) == CONST_DOUBLE
))
259 if (mode
!= GET_MODE (v
->u
.val_rtx
))
262 /* Unwrap X if necessary. */
263 if (GET_CODE (x
) == CONST
264 && (GET_CODE (XEXP (x
, 0)) == CONST_INT
265 || GET_CODE (XEXP (x
, 0)) == CONST_DOUBLE
))
268 /* We don't guarantee that distinct rtx's have different hash values,
269 so we need to do a comparison. */
270 for (l
= v
->locs
; l
; l
= l
->next
)
271 if (rtx_equal_for_cselib_p (l
->loc
, x
))
277 /* The hash function for our hash table. The value is always computed with
278 hash_rtx when adding an element; this function just extracts the hash
279 value from a cselib_val structure. */
282 get_value_hash (entry
)
285 const cselib_val
*v
= (const cselib_val
*) entry
;
289 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
290 only return true for values which point to a cselib_val whose value
291 element has been set to zero, which implies the cselib_val will be
295 references_value_p (x
, only_useless
)
299 enum rtx_code code
= GET_CODE (x
);
300 const char *fmt
= GET_RTX_FORMAT (code
);
303 if (GET_CODE (x
) == VALUE
304 && (! only_useless
|| CSELIB_VAL_PTR (x
)->locs
== 0))
307 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
309 if (fmt
[i
] == 'e' && references_value_p (XEXP (x
, i
), only_useless
))
311 else if (fmt
[i
] == 'E')
312 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
313 if (references_value_p (XVECEXP (x
, i
, j
), only_useless
))
320 /* For all locations found in X, delete locations that reference useless
321 values (i.e. values without any location). Called through
325 discard_useless_locs (x
, info
)
327 void *info ATTRIBUTE_UNUSED
;
329 cselib_val
*v
= (cselib_val
*)*x
;
330 struct elt_loc_list
**p
= &v
->locs
;
331 int had_locs
= v
->locs
!= 0;
335 if (references_value_p ((*p
)->loc
, 1))
336 unchain_one_elt_loc_list (p
);
341 if (had_locs
&& v
->locs
== 0)
344 values_became_useless
= 1;
349 /* If X is a value with no locations, remove it from the hashtable. */
352 discard_useless_values (x
, info
)
354 void *info ATTRIBUTE_UNUSED
;
356 cselib_val
*v
= (cselib_val
*)*x
;
360 htab_clear_slot (hash_table
, x
);
361 unchain_one_value (v
);
368 /* Clean out useless values (i.e. those which no longer have locations
369 associated with them) from the hash table. */
372 remove_useless_values ()
374 /* First pass: eliminate locations that reference the value. That in
375 turn can make more values useless. */
378 values_became_useless
= 0;
379 htab_traverse (hash_table
, discard_useless_locs
, 0);
381 while (values_became_useless
);
383 /* Second pass: actually remove the values. */
384 htab_traverse (hash_table
, discard_useless_values
, 0);
386 if (n_useless_values
!= 0)
390 /* Return nonzero if we can prove that X and Y contain the same value, taking
391 our gathered information into account. */
394 rtx_equal_for_cselib_p (x
, y
)
401 if (GET_CODE (x
) == REG
|| GET_CODE (x
) == MEM
)
403 cselib_val
*e
= cselib_lookup (x
, GET_MODE (x
), 0);
409 if (GET_CODE (y
) == REG
|| GET_CODE (y
) == MEM
)
411 cselib_val
*e
= cselib_lookup (y
, GET_MODE (y
), 0);
420 if (GET_CODE (x
) == VALUE
&& GET_CODE (y
) == VALUE
)
421 return CSELIB_VAL_PTR (x
) == CSELIB_VAL_PTR (y
);
423 if (GET_CODE (x
) == VALUE
)
425 cselib_val
*e
= CSELIB_VAL_PTR (x
);
426 struct elt_loc_list
*l
;
428 for (l
= e
->locs
; l
; l
= l
->next
)
432 /* Avoid infinite recursion. */
433 if (GET_CODE (t
) == REG
|| GET_CODE (t
) == MEM
)
435 else if (rtx_equal_for_cselib_p (t
, y
))
442 if (GET_CODE (y
) == VALUE
)
444 cselib_val
*e
= CSELIB_VAL_PTR (y
);
445 struct elt_loc_list
*l
;
447 for (l
= e
->locs
; l
; l
= l
->next
)
451 if (GET_CODE (t
) == REG
|| GET_CODE (t
) == MEM
)
453 else if (rtx_equal_for_cselib_p (x
, t
))
460 if (GET_CODE (x
) != GET_CODE (y
) || GET_MODE (x
) != GET_MODE (y
))
463 /* This won't be handled correctly by the code below. */
464 if (GET_CODE (x
) == LABEL_REF
)
465 return XEXP (x
, 0) == XEXP (y
, 0);
468 fmt
= GET_RTX_FORMAT (code
);
470 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
477 if (XWINT (x
, i
) != XWINT (y
, i
))
483 if (XINT (x
, i
) != XINT (y
, i
))
489 /* Two vectors must have the same length. */
490 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
493 /* And the corresponding elements must match. */
494 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
495 if (! rtx_equal_for_cselib_p (XVECEXP (x
, i
, j
),
501 if (! rtx_equal_for_cselib_p (XEXP (x
, i
), XEXP (y
, i
)))
507 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
512 /* These are just backpointers, so they don't matter. */
519 /* It is believed that rtx's at this level will never
520 contain anything but integers and other rtx's,
521 except for within LABEL_REFs and SYMBOL_REFs. */
529 /* We need to pass down the mode of constants through the hash table
530 functions. For that purpose, wrap them in a CONST of the appropriate
533 wrap_constant (mode
, x
)
534 enum machine_mode mode
;
537 if (GET_CODE (x
) != CONST_INT
538 && (GET_CODE (x
) != CONST_DOUBLE
|| GET_MODE (x
) != VOIDmode
))
540 if (mode
== VOIDmode
)
542 return gen_rtx_CONST (mode
, x
);
545 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
546 For registers and memory locations, we look up their cselib_val structure
547 and return its VALUE element.
548 Possible reasons for return 0 are: the object is volatile, or we couldn't
549 find a register or memory location in the table and CREATE is zero. If
550 CREATE is nonzero, table elts are created for regs and mem.
551 MODE is used in hashing for CONST_INTs only;
552 otherwise the mode of X is used. */
555 hash_rtx (x
, mode
, create
)
557 enum machine_mode mode
;
564 unsigned int hash
= 0;
567 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
573 e
= cselib_lookup (x
, GET_MODE (x
), create
);
580 hash
+= ((unsigned) CONST_INT
<< 7) + (unsigned) mode
+ INTVAL (x
);
581 return hash
? hash
: (unsigned int) CONST_INT
;
584 /* This is like the general case, except that it only counts
585 the integers representing the constant. */
586 hash
+= (unsigned) code
+ (unsigned) GET_MODE (x
);
587 if (GET_MODE (x
) != VOIDmode
)
588 hash
+= real_hash (CONST_DOUBLE_REAL_VALUE (x
));
590 hash
+= ((unsigned) CONST_DOUBLE_LOW (x
)
591 + (unsigned) CONST_DOUBLE_HIGH (x
));
592 return hash
? hash
: (unsigned int) CONST_DOUBLE
;
599 units
= CONST_VECTOR_NUNITS (x
);
601 for (i
= 0; i
< units
; ++i
)
603 elt
= CONST_VECTOR_ELT (x
, i
);
604 hash
+= hash_rtx (elt
, GET_MODE (elt
), 0);
610 /* Assume there is only one rtx object for any given label. */
613 += ((unsigned) LABEL_REF
<< 7) + (unsigned long) XEXP (x
, 0);
614 return hash
? hash
: (unsigned int) LABEL_REF
;
618 += ((unsigned) SYMBOL_REF
<< 7) + (unsigned long) XSTR (x
, 0);
619 return hash
? hash
: (unsigned int) SYMBOL_REF
;
630 case UNSPEC_VOLATILE
:
634 if (MEM_VOLATILE_P (x
))
643 i
= GET_RTX_LENGTH (code
) - 1;
644 fmt
= GET_RTX_FORMAT (code
);
649 rtx tem
= XEXP (x
, i
);
650 unsigned int tem_hash
= hash_rtx (tem
, 0, create
);
657 else if (fmt
[i
] == 'E')
658 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
660 unsigned int tem_hash
= hash_rtx (XVECEXP (x
, i
, j
), 0, create
);
667 else if (fmt
[i
] == 's')
669 const unsigned char *p
= (const unsigned char *) XSTR (x
, i
);
675 else if (fmt
[i
] == 'i')
677 else if (fmt
[i
] == '0' || fmt
[i
] == 't')
683 return hash
? hash
: 1 + (unsigned int) GET_CODE (x
);
686 /* Create a new value structure for VALUE and initialize it. The mode of the
690 new_cselib_val (value
, mode
)
692 enum machine_mode mode
;
694 cselib_val
*e
= empty_vals
;
697 empty_vals
= e
->u
.next_free
;
699 e
= (cselib_val
*) ggc_alloc (sizeof (cselib_val
));
705 e
->u
.val_rtx
= gen_rtx_VALUE (mode
);
706 CSELIB_VAL_PTR (e
->u
.val_rtx
) = e
;
712 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
713 contains the data at this address. X is a MEM that represents the
714 value. Update the two value structures to represent this situation. */
717 add_mem_for_addr (addr_elt
, mem_elt
, x
)
718 cselib_val
*addr_elt
, *mem_elt
;
721 struct elt_loc_list
*l
;
723 /* Avoid duplicates. */
724 for (l
= mem_elt
->locs
; l
; l
= l
->next
)
725 if (GET_CODE (l
->loc
) == MEM
726 && CSELIB_VAL_PTR (XEXP (l
->loc
, 0)) == addr_elt
)
729 addr_elt
->addr_list
= new_elt_list (addr_elt
->addr_list
, mem_elt
);
731 = new_elt_loc_list (mem_elt
->locs
,
732 replace_equiv_address_nv (x
, addr_elt
->u
.val_rtx
));
735 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
736 If CREATE, make a new one if we haven't seen it before. */
739 cselib_lookup_mem (x
, create
)
743 enum machine_mode mode
= GET_MODE (x
);
749 if (MEM_VOLATILE_P (x
) || mode
== BLKmode
750 || (FLOAT_MODE_P (mode
) && flag_float_store
))
753 /* Look up the value for the address. */
754 addr
= cselib_lookup (XEXP (x
, 0), mode
, create
);
758 /* Find a value that describes a value of our mode at that address. */
759 for (l
= addr
->addr_list
; l
; l
= l
->next
)
760 if (GET_MODE (l
->elt
->u
.val_rtx
) == mode
)
766 mem_elt
= new_cselib_val (++next_unknown_value
, mode
);
767 add_mem_for_addr (addr
, mem_elt
, x
);
768 slot
= htab_find_slot_with_hash (hash_table
, wrap_constant (mode
, x
),
769 mem_elt
->value
, INSERT
);
774 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
775 with VALUE expressions. This way, it becomes independent of changes
776 to registers and memory.
777 X isn't actually modified; if modifications are needed, new rtl is
778 allocated. However, the return value can share rtl with X. */
781 cselib_subst_to_values (x
)
784 enum rtx_code code
= GET_CODE (x
);
785 const char *fmt
= GET_RTX_FORMAT (code
);
794 for (l
= REG_VALUES (REGNO (x
)); l
; l
= l
->next
)
795 if (GET_MODE (l
->elt
->u
.val_rtx
) == GET_MODE (x
))
796 return l
->elt
->u
.val_rtx
;
801 e
= cselib_lookup_mem (x
, 0);
804 /* This happens for autoincrements. Assign a value that doesn't
806 e
= new_cselib_val (++next_unknown_value
, GET_MODE (x
));
821 e
= new_cselib_val (++next_unknown_value
, GET_MODE (x
));
828 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
832 rtx t
= cselib_subst_to_values (XEXP (x
, i
));
834 if (t
!= XEXP (x
, i
) && x
== copy
)
835 copy
= shallow_copy_rtx (x
);
839 else if (fmt
[i
] == 'E')
843 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
845 rtx t
= cselib_subst_to_values (XVECEXP (x
, i
, j
));
847 if (t
!= XVECEXP (x
, i
, j
) && XVEC (x
, i
) == XVEC (copy
, i
))
850 copy
= shallow_copy_rtx (x
);
852 XVEC (copy
, i
) = rtvec_alloc (XVECLEN (x
, i
));
853 for (k
= 0; k
< j
; k
++)
854 XVECEXP (copy
, i
, k
) = XVECEXP (x
, i
, k
);
857 XVECEXP (copy
, i
, j
) = t
;
865 /* Look up the rtl expression X in our tables and return the value it has.
866 If CREATE is zero, we return NULL if we don't know the value. Otherwise,
867 we create a new one if possible, using mode MODE if X doesn't have a mode
868 (i.e. because it's a constant). */
871 cselib_lookup (x
, mode
, create
)
873 enum machine_mode mode
;
878 unsigned int hashval
;
880 if (GET_MODE (x
) != VOIDmode
)
883 if (GET_CODE (x
) == VALUE
)
884 return CSELIB_VAL_PTR (x
);
886 if (GET_CODE (x
) == REG
)
889 unsigned int i
= REGNO (x
);
891 for (l
= REG_VALUES (i
); l
; l
= l
->next
)
892 if (mode
== GET_MODE (l
->elt
->u
.val_rtx
))
898 if (i
< FIRST_PSEUDO_REGISTER
)
900 unsigned int n
= HARD_REGNO_NREGS (i
, mode
);
902 if (n
> max_value_regs
)
906 e
= new_cselib_val (++next_unknown_value
, GET_MODE (x
));
907 e
->locs
= new_elt_loc_list (e
->locs
, x
);
908 if (REG_VALUES (i
) == 0)
909 VARRAY_PUSH_UINT (used_regs
, i
);
910 REG_VALUES (i
) = new_elt_list (REG_VALUES (i
), e
);
911 slot
= htab_find_slot_with_hash (hash_table
, x
, e
->value
, INSERT
);
916 if (GET_CODE (x
) == MEM
)
917 return cselib_lookup_mem (x
, create
);
919 hashval
= hash_rtx (x
, mode
, create
);
920 /* Can't even create if hashing is not possible. */
924 slot
= htab_find_slot_with_hash (hash_table
, wrap_constant (mode
, x
),
925 hashval
, create
? INSERT
: NO_INSERT
);
929 e
= (cselib_val
*) *slot
;
933 e
= new_cselib_val (hashval
, mode
);
935 /* We have to fill the slot before calling cselib_subst_to_values:
936 the hash table is inconsistent until we do so, and
937 cselib_subst_to_values will need to do lookups. */
939 e
->locs
= new_elt_loc_list (e
->locs
, cselib_subst_to_values (x
));
943 /* Invalidate any entries in reg_values that overlap REGNO. This is called
944 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
945 is used to determine how many hard registers are being changed. If MODE
946 is VOIDmode, then only REGNO is being changed; this is used when
947 invalidating call clobbered registers across a call. */
950 cselib_invalidate_regno (regno
, mode
)
952 enum machine_mode mode
;
954 unsigned int endregno
;
957 /* If we see pseudos after reload, something is _wrong_. */
958 if (reload_completed
&& regno
>= FIRST_PSEUDO_REGISTER
959 && reg_renumber
[regno
] >= 0)
962 /* Determine the range of registers that must be invalidated. For
963 pseudos, only REGNO is affected. For hard regs, we must take MODE
964 into account, and we must also invalidate lower register numbers
965 if they contain values that overlap REGNO. */
966 if (regno
< FIRST_PSEUDO_REGISTER
&& mode
!= VOIDmode
)
968 if (regno
< max_value_regs
)
971 i
= regno
- max_value_regs
;
973 endregno
= regno
+ HARD_REGNO_NREGS (regno
, mode
);
978 endregno
= regno
+ 1;
981 for (; i
< endregno
; i
++)
983 struct elt_list
**l
= ®_VALUES (i
);
985 /* Go through all known values for this reg; if it overlaps the range
986 we're invalidating, remove the value. */
989 cselib_val
*v
= (*l
)->elt
;
990 struct elt_loc_list
**p
;
991 unsigned int this_last
= i
;
993 if (i
< FIRST_PSEUDO_REGISTER
)
994 this_last
+= HARD_REGNO_NREGS (i
, GET_MODE (v
->u
.val_rtx
)) - 1;
996 if (this_last
< regno
)
1002 /* We have an overlap. */
1003 unchain_one_elt_list (l
);
1005 /* Now, we clear the mapping from value to reg. It must exist, so
1006 this code will crash intentionally if it doesn't. */
1007 for (p
= &v
->locs
; ; p
= &(*p
)->next
)
1011 if (GET_CODE (x
) == REG
&& REGNO (x
) == i
)
1013 unchain_one_elt_loc_list (p
);
1023 /* The memory at address MEM_BASE is being changed.
1024 Return whether this change will invalidate VAL. */
1027 cselib_mem_conflict_p (mem_base
, val
)
1035 code
= GET_CODE (val
);
1038 /* Get rid of a few simple cases quickly. */
1052 if (GET_MODE (mem_base
) == BLKmode
1053 || GET_MODE (val
) == BLKmode
1054 || anti_dependence (val
, mem_base
))
1057 /* The address may contain nested MEMs. */
1064 fmt
= GET_RTX_FORMAT (code
);
1065 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1069 if (cselib_mem_conflict_p (mem_base
, XEXP (val
, i
)))
1072 else if (fmt
[i
] == 'E')
1073 for (j
= 0; j
< XVECLEN (val
, i
); j
++)
1074 if (cselib_mem_conflict_p (mem_base
, XVECEXP (val
, i
, j
)))
1081 /* For the value found in SLOT, walk its locations to determine if any overlap
1082 INFO (which is a MEM rtx). */
1085 cselib_invalidate_mem_1 (slot
, info
)
1089 cselib_val
*v
= (cselib_val
*) *slot
;
1090 rtx mem_rtx
= (rtx
) info
;
1091 struct elt_loc_list
**p
= &v
->locs
;
1092 int had_locs
= v
->locs
!= 0;
1098 struct elt_list
**mem_chain
;
1100 /* MEMs may occur in locations only at the top level; below
1101 that every MEM or REG is substituted by its VALUE. */
1102 if (GET_CODE (x
) != MEM
1103 || ! cselib_mem_conflict_p (mem_rtx
, x
))
1109 /* This one overlaps. */
1110 /* We must have a mapping from this MEM's address to the
1111 value (E). Remove that, too. */
1112 addr
= cselib_lookup (XEXP (x
, 0), VOIDmode
, 0);
1113 mem_chain
= &addr
->addr_list
;
1116 if ((*mem_chain
)->elt
== v
)
1118 unchain_one_elt_list (mem_chain
);
1122 mem_chain
= &(*mem_chain
)->next
;
1125 unchain_one_elt_loc_list (p
);
1128 if (had_locs
&& v
->locs
== 0)
1134 /* Invalidate any locations in the table which are changed because of a
1135 store to MEM_RTX. If this is called because of a non-const call
1136 instruction, MEM_RTX is (mem:BLK const0_rtx). */
1139 cselib_invalidate_mem (mem_rtx
)
1142 htab_traverse (hash_table
, cselib_invalidate_mem_1
, mem_rtx
);
1145 /* Invalidate DEST, which is being assigned to or clobbered. The second and
1146 the third parameter exist so that this function can be passed to
1147 note_stores; they are ignored. */
1150 cselib_invalidate_rtx (dest
, ignore
, data
)
1152 rtx ignore ATTRIBUTE_UNUSED
;
1153 void *data ATTRIBUTE_UNUSED
;
1155 while (GET_CODE (dest
) == STRICT_LOW_PART
|| GET_CODE (dest
) == SIGN_EXTRACT
1156 || GET_CODE (dest
) == ZERO_EXTRACT
|| GET_CODE (dest
) == SUBREG
)
1157 dest
= XEXP (dest
, 0);
1159 if (GET_CODE (dest
) == REG
)
1160 cselib_invalidate_regno (REGNO (dest
), GET_MODE (dest
));
1161 else if (GET_CODE (dest
) == MEM
)
1162 cselib_invalidate_mem (dest
);
1164 /* Some machines don't define AUTO_INC_DEC, but they still use push
1165 instructions. We need to catch that case here in order to
1166 invalidate the stack pointer correctly. Note that invalidating
1167 the stack pointer is different from invalidating DEST. */
1168 if (push_operand (dest
, GET_MODE (dest
)))
1169 cselib_invalidate_rtx (stack_pointer_rtx
, NULL_RTX
, NULL
);
1172 /* Record the result of a SET instruction. DEST is being set; the source
1173 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
1174 describes its address. */
1177 cselib_record_set (dest
, src_elt
, dest_addr_elt
)
1179 cselib_val
*src_elt
, *dest_addr_elt
;
1181 int dreg
= GET_CODE (dest
) == REG
? (int) REGNO (dest
) : -1;
1183 if (src_elt
== 0 || side_effects_p (dest
))
1188 if (REG_VALUES (dreg
) == 0)
1189 VARRAY_PUSH_UINT (used_regs
, dreg
);
1191 if (dreg
< FIRST_PSEUDO_REGISTER
)
1193 unsigned int n
= HARD_REGNO_NREGS (dreg
, GET_MODE (dest
));
1195 if (n
> max_value_regs
)
1199 REG_VALUES (dreg
) = new_elt_list (REG_VALUES (dreg
), src_elt
);
1200 if (src_elt
->locs
== 0)
1202 src_elt
->locs
= new_elt_loc_list (src_elt
->locs
, dest
);
1204 else if (GET_CODE (dest
) == MEM
&& dest_addr_elt
!= 0)
1206 if (src_elt
->locs
== 0)
1208 add_mem_for_addr (dest_addr_elt
, src_elt
, dest
);
1212 /* Describe a single set that is part of an insn. */
1217 cselib_val
*src_elt
;
1218 cselib_val
*dest_addr_elt
;
1221 /* There is no good way to determine how many elements there can be
1222 in a PARALLEL. Since it's fairly cheap, use a really large number. */
1223 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
1225 /* Record the effects of any sets in INSN. */
1227 cselib_record_sets (insn
)
1232 struct set sets
[MAX_SETS
];
1233 rtx body
= PATTERN (insn
);
1236 body
= PATTERN (insn
);
1237 if (GET_CODE (body
) == COND_EXEC
)
1239 cond
= COND_EXEC_TEST (body
);
1240 body
= COND_EXEC_CODE (body
);
1243 /* Find all sets. */
1244 if (GET_CODE (body
) == SET
)
1246 sets
[0].src
= SET_SRC (body
);
1247 sets
[0].dest
= SET_DEST (body
);
1250 else if (GET_CODE (body
) == PARALLEL
)
1252 /* Look through the PARALLEL and record the values being
1253 set, if possible. Also handle any CLOBBERs. */
1254 for (i
= XVECLEN (body
, 0) - 1; i
>= 0; --i
)
1256 rtx x
= XVECEXP (body
, 0, i
);
1258 if (GET_CODE (x
) == SET
)
1260 sets
[n_sets
].src
= SET_SRC (x
);
1261 sets
[n_sets
].dest
= SET_DEST (x
);
1267 /* Look up the values that are read. Do this before invalidating the
1268 locations that are written. */
1269 for (i
= 0; i
< n_sets
; i
++)
1271 rtx dest
= sets
[i
].dest
;
1273 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
1274 the low part after invalidating any knowledge about larger modes. */
1275 if (GET_CODE (sets
[i
].dest
) == STRICT_LOW_PART
)
1276 sets
[i
].dest
= dest
= XEXP (dest
, 0);
1278 /* We don't know how to record anything but REG or MEM. */
1279 if (GET_CODE (dest
) == REG
|| GET_CODE (dest
) == MEM
)
1281 rtx src
= sets
[i
].src
;
1283 src
= gen_rtx_IF_THEN_ELSE (GET_MODE (src
), cond
, src
, dest
);
1284 sets
[i
].src_elt
= cselib_lookup (src
, GET_MODE (dest
), 1);
1285 if (GET_CODE (dest
) == MEM
)
1286 sets
[i
].dest_addr_elt
= cselib_lookup (XEXP (dest
, 0), Pmode
, 1);
1288 sets
[i
].dest_addr_elt
= 0;
1292 /* Invalidate all locations written by this insn. Note that the elts we
1293 looked up in the previous loop aren't affected, just some of their
1294 locations may go away. */
1295 note_stores (body
, cselib_invalidate_rtx
, NULL
);
1297 /* Now enter the equivalences in our tables. */
1298 for (i
= 0; i
< n_sets
; i
++)
1300 rtx dest
= sets
[i
].dest
;
1301 if (GET_CODE (dest
) == REG
|| GET_CODE (dest
) == MEM
)
1302 cselib_record_set (dest
, sets
[i
].src_elt
, sets
[i
].dest_addr_elt
);
1306 /* Record the effects of INSN. */
1309 cselib_process_insn (insn
)
1315 if (find_reg_note (insn
, REG_LIBCALL
, NULL
))
1316 cselib_current_insn_in_libcall
= true;
1317 if (find_reg_note (insn
, REG_RETVAL
, NULL
))
1318 cselib_current_insn_in_libcall
= false;
1319 cselib_current_insn
= insn
;
1321 /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp. */
1322 if (GET_CODE (insn
) == CODE_LABEL
1323 || (GET_CODE (insn
) == CALL_INSN
1324 && find_reg_note (insn
, REG_SETJMP
, NULL
))
1325 || (GET_CODE (insn
) == INSN
1326 && GET_CODE (PATTERN (insn
)) == ASM_OPERANDS
1327 && MEM_VOLATILE_P (PATTERN (insn
))))
1333 if (! INSN_P (insn
))
1335 cselib_current_insn
= 0;
1339 /* If this is a call instruction, forget anything stored in a
1340 call clobbered register, or, if this is not a const call, in
1342 if (GET_CODE (insn
) == CALL_INSN
)
1344 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1345 if (call_used_regs
[i
])
1346 cselib_invalidate_regno (i
, VOIDmode
);
1348 if (! CONST_OR_PURE_CALL_P (insn
))
1349 cselib_invalidate_mem (callmem
);
1352 cselib_record_sets (insn
);
1355 /* Clobber any registers which appear in REG_INC notes. We
1356 could keep track of the changes to their values, but it is
1357 unlikely to help. */
1358 for (x
= REG_NOTES (insn
); x
; x
= XEXP (x
, 1))
1359 if (REG_NOTE_KIND (x
) == REG_INC
)
1360 cselib_invalidate_rtx (XEXP (x
, 0), NULL_RTX
, NULL
);
1363 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
1364 after we have processed the insn. */
1365 if (GET_CODE (insn
) == CALL_INSN
)
1366 for (x
= CALL_INSN_FUNCTION_USAGE (insn
); x
; x
= XEXP (x
, 1))
1367 if (GET_CODE (XEXP (x
, 0)) == CLOBBER
)
1368 cselib_invalidate_rtx (XEXP (XEXP (x
, 0), 0), NULL_RTX
, NULL
);
1370 cselib_current_insn
= 0;
1372 if (n_useless_values
> MAX_USELESS_VALUES
)
1373 remove_useless_values ();
1376 /* Make sure our varrays are big enough. Not called from any cselib routines;
1377 it must be called by the user if it allocated new registers. */
1380 cselib_update_varray_sizes ()
1382 unsigned int nregs
= max_reg_num ();
1384 if (nregs
== cselib_nregs
)
1387 cselib_nregs
= nregs
;
1388 VARRAY_GROW (reg_values
, nregs
);
1389 VARRAY_GROW (used_regs
, nregs
);
1392 /* Initialize cselib for one pass. The caller must also call
1393 init_alias_analysis. */
1398 /* This is only created once. */
1400 callmem
= gen_rtx_MEM (BLKmode
, const0_rtx
);
1402 cselib_nregs
= max_reg_num ();
1403 if (reg_values_old
!= NULL
&& VARRAY_SIZE (reg_values_old
) >= cselib_nregs
)
1405 reg_values
= reg_values_old
;
1406 used_regs
= used_regs_old
;
1407 VARRAY_CLEAR (reg_values
);
1408 VARRAY_CLEAR (used_regs
);
1412 VARRAY_ELT_LIST_INIT (reg_values
, cselib_nregs
, "reg_values");
1413 VARRAY_UINT_INIT (used_regs
, cselib_nregs
, "used_regs");
1415 hash_table
= htab_create_ggc (31, get_value_hash
, entry_and_rtx_equal_p
,
1418 cselib_current_insn_in_libcall
= false;
1421 /* Called when the current user is done with cselib. */
1426 reg_values_old
= reg_values
;
1428 used_regs_old
= used_regs
;
1431 n_useless_values
= 0;
1432 next_unknown_value
= 0;
1435 #include "gt-cselib.h"