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 GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
29 #include "hard-reg-set.h"
32 #include "insn-config.h"
43 static int entry_and_rtx_equal_p PARAMS ((const void *, const void *));
44 static unsigned int 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 rtx cselib_subst_to_values PARAMS ((rtx));
64 static void cselib_invalidate_regno PARAMS ((unsigned int,
66 static int cselib_mem_conflict_p PARAMS ((rtx, rtx));
67 static int cselib_invalidate_mem_1 PARAMS ((void **, void *));
68 static void cselib_invalidate_mem PARAMS ((rtx));
69 static void cselib_invalidate_rtx PARAMS ((rtx, rtx, void *));
70 static void cselib_record_set PARAMS ((rtx, cselib_val *,
72 static void cselib_record_sets PARAMS ((rtx));
74 /* There are three ways in which cselib can look up an rtx:
75 - for a REG, the reg_values table (which is indexed by regno) is used
76 - for a MEM, we recursively look up its address and then follow the
77 addr_list of that value
78 - for everything else, we compute a hash value and go through the hash
79 table. Since different rtx's can still have the same hash value,
80 this involves walking the table entries for a given value and comparing
81 the locations of the entries with the rtx we are looking up. */
83 /* A table that enables us to look up elts by their value. */
84 static htab_t hash_table;
86 /* This is a global so we don't have to pass this through every function.
87 It is used in new_elt_loc_list to set SETTING_INSN. */
88 static rtx cselib_current_insn;
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 varray_type reg_values;
107 #define REG_VALUES(I) VARRAY_ELT_LIST (reg_values, (I))
109 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
110 in clear_table() for fast emptying. */
111 static varray_type used_regs;
113 /* We pass this to cselib_invalidate_mem to invalidate all of
114 memory for a non-const call instruction. */
117 /* Memory for our structures is allocated from this obstack. */
118 static struct obstack cselib_obstack;
120 /* Used to quickly free all memory. */
121 static char *cselib_startobj;
123 /* Caches for unused structures. */
124 static cselib_val *empty_vals;
125 static struct elt_list *empty_elt_lists;
126 static 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 *) obstack_alloc (&cselib_obstack,
147 sizeof (struct elt_list));
153 /* Allocate a struct elt_loc_list and fill in its two elements with the
156 static struct elt_loc_list *
157 new_elt_loc_list (next, loc)
158 struct elt_loc_list *next;
161 struct elt_loc_list *el = empty_elt_loc_lists;
164 empty_elt_loc_lists = el->next;
166 el = (struct elt_loc_list *) obstack_alloc (&cselib_obstack,
167 sizeof (struct elt_loc_list));
170 el->setting_insn = cselib_current_insn;
174 /* The elt_list at *PL is no longer needed. Unchain it and free its
178 unchain_one_elt_list (pl)
179 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 (pl)
192 struct elt_loc_list **pl;
194 struct elt_loc_list *l = *pl;
197 l->next = empty_elt_loc_lists;
198 empty_elt_loc_lists = l;
201 /* Likewise for cselib_vals. This also frees the addr_list associated with
205 unchain_one_value (v)
209 unchain_one_elt_list (&v->addr_list);
211 v->u.next_free = empty_vals;
215 /* Remove all entries from the hash table. Also used during
216 initialization. If CLEAR_ALL isn't set, then only clear the entries
217 which are known to have been used. */
220 clear_table (clear_all)
226 for (i = 0; i < cselib_nregs; i++)
229 for (i = 0; i < VARRAY_ACTIVE_SIZE (used_regs); i++)
230 REG_VALUES (VARRAY_UINT (used_regs, i)) = 0;
232 VARRAY_POP_ALL (used_regs);
234 htab_empty (hash_table);
235 obstack_free (&cselib_obstack, cselib_startobj);
239 empty_elt_loc_lists = 0;
240 n_useless_values = 0;
242 next_unknown_value = 0;
245 /* The equality test for our hash table. The first argument ENTRY is a table
246 element (i.e. a cselib_val), while the second arg X is an rtx. We know
247 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
248 CONST of an appropriate mode. */
251 entry_and_rtx_equal_p (entry, x_arg)
252 const void *entry, *x_arg;
254 struct elt_loc_list *l;
255 const cselib_val *v = (const cselib_val *) entry;
257 enum machine_mode mode = GET_MODE (x);
259 if (GET_CODE (x) == CONST_INT
260 || (mode == VOIDmode && GET_CODE (x) == CONST_DOUBLE))
262 if (mode != GET_MODE (v->u.val_rtx))
265 /* Unwrap X if necessary. */
266 if (GET_CODE (x) == CONST
267 && (GET_CODE (XEXP (x, 0)) == CONST_INT
268 || GET_CODE (XEXP (x, 0)) == CONST_DOUBLE))
271 /* We don't guarantee that distinct rtx's have different hash values,
272 so we need to do a comparison. */
273 for (l = v->locs; l; l = l->next)
274 if (rtx_equal_for_cselib_p (l->loc, x))
280 /* The hash function for our hash table. The value is always computed with
281 hash_rtx when adding an element; this function just extracts the hash
282 value from a cselib_val structure. */
285 get_value_hash (entry)
288 const cselib_val *v = (const cselib_val *) entry;
292 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
293 only return true for values which point to a cselib_val whose value
294 element has been set to zero, which implies the cselib_val will be
298 references_value_p (x, only_useless)
302 enum rtx_code code = GET_CODE (x);
303 const char *fmt = GET_RTX_FORMAT (code);
306 if (GET_CODE (x) == VALUE
307 && (! only_useless || CSELIB_VAL_PTR (x)->locs == 0))
310 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
312 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
314 else if (fmt[i] == 'E')
315 for (j = 0; j < XVECLEN (x, i); j++)
316 if (references_value_p (XVECEXP (x, i, j), only_useless))
323 /* For all locations found in X, delete locations that reference useless
324 values (i.e. values without any location). Called through
328 discard_useless_locs (x, info)
330 void *info ATTRIBUTE_UNUSED;
332 cselib_val *v = (cselib_val *)*x;
333 struct elt_loc_list **p = &v->locs;
334 int had_locs = v->locs != 0;
338 if (references_value_p ((*p)->loc, 1))
339 unchain_one_elt_loc_list (p);
344 if (had_locs && v->locs == 0)
347 values_became_useless = 1;
352 /* If X is a value with no locations, remove it from the hashtable. */
355 discard_useless_values (x, info)
357 void *info ATTRIBUTE_UNUSED;
359 cselib_val *v = (cselib_val *)*x;
363 htab_clear_slot (hash_table, x);
364 unchain_one_value (v);
371 /* Clean out useless values (i.e. those which no longer have locations
372 associated with them) from the hash table. */
375 remove_useless_values ()
377 /* First pass: eliminate locations that reference the value. That in
378 turn can make more values useless. */
381 values_became_useless = 0;
382 htab_traverse (hash_table, discard_useless_locs, 0);
384 while (values_became_useless);
386 /* Second pass: actually remove the values. */
387 htab_traverse (hash_table, discard_useless_values, 0);
389 if (n_useless_values != 0)
393 /* Return nonzero if we can prove that X and Y contain the same value, taking
394 our gathered information into account. */
397 rtx_equal_for_cselib_p (x, y)
404 if (GET_CODE (x) == REG || GET_CODE (x) == MEM)
406 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0);
412 if (GET_CODE (y) == REG || GET_CODE (y) == MEM)
414 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0);
423 if (GET_CODE (x) == VALUE && GET_CODE (y) == VALUE)
424 return CSELIB_VAL_PTR (x) == CSELIB_VAL_PTR (y);
426 if (GET_CODE (x) == VALUE)
428 cselib_val *e = CSELIB_VAL_PTR (x);
429 struct elt_loc_list *l;
431 for (l = e->locs; l; l = l->next)
435 /* Avoid infinite recursion. */
436 if (GET_CODE (t) == REG || GET_CODE (t) == MEM)
438 else if (rtx_equal_for_cselib_p (t, y))
445 if (GET_CODE (y) == VALUE)
447 cselib_val *e = CSELIB_VAL_PTR (y);
448 struct elt_loc_list *l;
450 for (l = e->locs; l; l = l->next)
454 if (GET_CODE (t) == REG || GET_CODE (t) == MEM)
456 else if (rtx_equal_for_cselib_p (x, t))
463 if (GET_CODE (x) != GET_CODE (y) || GET_MODE (x) != GET_MODE (y))
466 /* This won't be handled correctly by the code below. */
467 if (GET_CODE (x) == LABEL_REF)
468 return XEXP (x, 0) == XEXP (y, 0);
471 fmt = GET_RTX_FORMAT (code);
473 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
480 if (XWINT (x, i) != XWINT (y, i))
486 if (XINT (x, i) != XINT (y, i))
492 /* Two vectors must have the same length. */
493 if (XVECLEN (x, i) != XVECLEN (y, i))
496 /* And the corresponding elements must match. */
497 for (j = 0; j < XVECLEN (x, i); j++)
498 if (! rtx_equal_for_cselib_p (XVECEXP (x, i, j),
504 if (! rtx_equal_for_cselib_p (XEXP (x, i), XEXP (y, i)))
510 if (strcmp (XSTR (x, i), XSTR (y, i)))
515 /* These are just backpointers, so they don't matter. */
522 /* It is believed that rtx's at this level will never
523 contain anything but integers and other rtx's,
524 except for within LABEL_REFs and SYMBOL_REFs. */
532 /* We need to pass down the mode of constants through the hash table
533 functions. For that purpose, wrap them in a CONST of the appropriate
536 wrap_constant (mode, x)
537 enum machine_mode mode;
540 if (GET_CODE (x) != CONST_INT
541 && (GET_CODE (x) != CONST_DOUBLE || GET_MODE (x) != VOIDmode))
543 if (mode == VOIDmode)
545 return gen_rtx_CONST (mode, x);
548 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
549 For registers and memory locations, we look up their cselib_val structure
550 and return its VALUE element.
551 Possible reasons for return 0 are: the object is volatile, or we couldn't
552 find a register or memory location in the table and CREATE is zero. If
553 CREATE is nonzero, table elts are created for regs and mem.
554 MODE is used in hashing for CONST_INTs only;
555 otherwise the mode of X is used. */
558 hash_rtx (x, mode, create)
560 enum machine_mode mode;
567 unsigned int hash = 0;
569 /* repeat is used to turn tail-recursion into iteration. */
572 hash += (unsigned) code + (unsigned) GET_MODE (x);
578 e = cselib_lookup (x, GET_MODE (x), create);
585 hash += ((unsigned) CONST_INT << 7) + (unsigned) mode + INTVAL (x);
586 return hash ? hash : (unsigned int) CONST_INT;
589 /* This is like the general case, except that it only counts
590 the integers representing the constant. */
591 hash += (unsigned) code + (unsigned) GET_MODE (x);
592 if (GET_MODE (x) != VOIDmode)
593 for (i = 2; i < GET_RTX_LENGTH (CONST_DOUBLE); i++)
594 hash += XWINT (x, i);
596 hash += ((unsigned) CONST_DOUBLE_LOW (x)
597 + (unsigned) CONST_DOUBLE_HIGH (x));
598 return hash ? hash : (unsigned int) CONST_DOUBLE;
600 /* Assume there is only one rtx object for any given label. */
603 += ((unsigned) LABEL_REF << 7) + (unsigned long) XEXP (x, 0);
604 return hash ? hash : (unsigned int) LABEL_REF;
608 += ((unsigned) SYMBOL_REF << 7) + (unsigned long) XSTR (x, 0);
609 return hash ? hash : (unsigned int) SYMBOL_REF;
620 case UNSPEC_VOLATILE:
624 if (MEM_VOLATILE_P (x))
633 i = GET_RTX_LENGTH (code) - 1;
634 fmt = GET_RTX_FORMAT (code);
639 rtx tem = XEXP (x, i);
640 unsigned int tem_hash;
642 /* If we are about to do the last recursive call
643 needed at this level, change it into iteration.
644 This function is called enough to be worth it. */
651 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 *) obstack_alloc (&cselib_obstack, 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;
722 struct elt_loc_list *l;
724 /* Avoid duplicates. */
725 for (l = mem_elt->locs; l; l = l->next)
726 if (GET_CODE (l->loc) == MEM
727 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
730 new = gen_rtx_MEM (GET_MODE (x), addr_elt->u.val_rtx);
731 MEM_COPY_ATTRIBUTES (new, x);
733 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
734 mem_elt->locs = new_elt_loc_list (mem_elt->locs, new);
737 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
738 If CREATE, make a new one if we haven't seen it before. */
741 cselib_lookup_mem (x, create)
745 enum machine_mode mode = GET_MODE (x);
751 if (MEM_VOLATILE_P (x) || mode == BLKmode
752 || (FLOAT_MODE_P (mode) && flag_float_store))
755 /* Look up the value for the address. */
756 addr = cselib_lookup (XEXP (x, 0), mode, create);
760 /* Find a value that describes a value of our mode at that address. */
761 for (l = addr->addr_list; l; l = l->next)
762 if (GET_MODE (l->elt->u.val_rtx) == mode)
768 mem_elt = new_cselib_val (++next_unknown_value, mode);
769 add_mem_for_addr (addr, mem_elt, x);
770 slot = htab_find_slot_with_hash (hash_table, wrap_constant (mode, x),
771 mem_elt->value, INSERT);
776 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
777 with VALUE expressions. This way, it becomes independent of changes
778 to registers and memory.
779 X isn't actually modified; if modifications are needed, new rtl is
780 allocated. However, the return value can share rtl with X. */
783 cselib_subst_to_values (x)
786 enum rtx_code code = GET_CODE (x);
787 const char *fmt = GET_RTX_FORMAT (code);
796 for (l = REG_VALUES (REGNO (x)); l; l = l->next)
797 if (GET_MODE (l->elt->u.val_rtx) == GET_MODE (x))
798 return l->elt->u.val_rtx;
803 e = cselib_lookup_mem (x, 0);
808 /* CONST_DOUBLEs must be special-cased here so that we won't try to
809 look up the CONST_DOUBLE_MEM inside. */
818 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
822 rtx t = cselib_subst_to_values (XEXP (x, i));
824 if (t != XEXP (x, i) && x == copy)
825 copy = shallow_copy_rtx (x);
829 else if (fmt[i] == 'E')
833 for (j = 0; j < XVECLEN (x, i); j++)
835 rtx t = cselib_subst_to_values (XVECEXP (x, i, j));
837 if (t != XVECEXP (x, i, j) && XVEC (x, i) == XVEC (copy, i))
840 copy = shallow_copy_rtx (x);
842 XVEC (copy, i) = rtvec_alloc (XVECLEN (x, i));
843 for (k = 0; k < j; k++)
844 XVECEXP (copy, i, k) = XVECEXP (x, i, k);
847 XVECEXP (copy, i, j) = t;
855 /* Look up the rtl expression X in our tables and return the value it has.
856 If CREATE is zero, we return NULL if we don't know the value. Otherwise,
857 we create a new one if possible, using mode MODE if X doesn't have a mode
858 (i.e. because it's a constant). */
861 cselib_lookup (x, mode, create)
863 enum machine_mode mode;
868 unsigned int hashval;
870 if (GET_MODE (x) != VOIDmode)
873 if (GET_CODE (x) == VALUE)
874 return CSELIB_VAL_PTR (x);
876 if (GET_CODE (x) == REG)
879 unsigned int i = REGNO (x);
881 for (l = REG_VALUES (i); l; l = l->next)
882 if (mode == GET_MODE (l->elt->u.val_rtx))
888 e = new_cselib_val (++next_unknown_value, GET_MODE (x));
889 e->locs = new_elt_loc_list (e->locs, x);
890 if (REG_VALUES (i) == 0)
891 VARRAY_PUSH_UINT (used_regs, i);
892 REG_VALUES (i) = new_elt_list (REG_VALUES (i), e);
893 slot = htab_find_slot_with_hash (hash_table, x, e->value, INSERT);
898 if (GET_CODE (x) == MEM)
899 return cselib_lookup_mem (x, create);
901 hashval = hash_rtx (x, mode, create);
902 /* Can't even create if hashing is not possible. */
906 slot = htab_find_slot_with_hash (hash_table, wrap_constant (mode, x),
907 hashval, create ? INSERT : NO_INSERT);
911 e = (cselib_val *) *slot;
915 e = new_cselib_val (hashval, mode);
917 /* We have to fill the slot before calling cselib_subst_to_values:
918 the hash table is inconsistent until we do so, and
919 cselib_subst_to_values will need to do lookups. */
921 e->locs = new_elt_loc_list (e->locs, cselib_subst_to_values (x));
925 /* Invalidate any entries in reg_values that overlap REGNO. This is called
926 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
927 is used to determine how many hard registers are being changed. If MODE
928 is VOIDmode, then only REGNO is being changed; this is used when
929 invalidating call clobbered registers across a call. */
932 cselib_invalidate_regno (regno, mode)
934 enum machine_mode mode;
936 unsigned int endregno;
939 /* If we see pseudos after reload, something is _wrong_. */
940 if (reload_completed && regno >= FIRST_PSEUDO_REGISTER
941 && reg_renumber[regno] >= 0)
944 /* Determine the range of registers that must be invalidated. For
945 pseudos, only REGNO is affected. For hard regs, we must take MODE
946 into account, and we must also invalidate lower register numbers
947 if they contain values that overlap REGNO. */
948 endregno = regno + 1;
949 if (regno < FIRST_PSEUDO_REGISTER && mode != VOIDmode)
950 endregno = regno + HARD_REGNO_NREGS (regno, mode);
952 for (i = 0; i < endregno; i++)
954 struct elt_list **l = ®_VALUES (i);
956 /* Go through all known values for this reg; if it overlaps the range
957 we're invalidating, remove the value. */
960 cselib_val *v = (*l)->elt;
961 struct elt_loc_list **p;
962 unsigned int this_last = i;
964 if (i < FIRST_PSEUDO_REGISTER)
965 this_last += HARD_REGNO_NREGS (i, GET_MODE (v->u.val_rtx)) - 1;
967 if (this_last < regno)
973 /* We have an overlap. */
974 unchain_one_elt_list (l);
976 /* Now, we clear the mapping from value to reg. It must exist, so
977 this code will crash intentionally if it doesn't. */
978 for (p = &v->locs; ; p = &(*p)->next)
982 if (GET_CODE (x) == REG && REGNO (x) == i)
984 unchain_one_elt_loc_list (p);
994 /* The memory at address MEM_BASE is being changed.
995 Return whether this change will invalidate VAL. */
998 cselib_mem_conflict_p (mem_base, val)
1006 code = GET_CODE (val);
1009 /* Get rid of a few simple cases quickly. */
1022 if (GET_MODE (mem_base) == BLKmode
1023 || GET_MODE (val) == BLKmode
1024 || anti_dependence (val, mem_base))
1027 /* The address may contain nested MEMs. */
1034 fmt = GET_RTX_FORMAT (code);
1035 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1039 if (cselib_mem_conflict_p (mem_base, XEXP (val, i)))
1042 else if (fmt[i] == 'E')
1043 for (j = 0; j < XVECLEN (val, i); j++)
1044 if (cselib_mem_conflict_p (mem_base, XVECEXP (val, i, j)))
1051 /* For the value found in SLOT, walk its locations to determine if any overlap
1052 INFO (which is a MEM rtx). */
1055 cselib_invalidate_mem_1 (slot, info)
1059 cselib_val *v = (cselib_val *) *slot;
1060 rtx mem_rtx = (rtx) info;
1061 struct elt_loc_list **p = &v->locs;
1062 int had_locs = v->locs != 0;
1068 struct elt_list **mem_chain;
1070 /* MEMs may occur in locations only at the top level; below
1071 that every MEM or REG is substituted by its VALUE. */
1072 if (GET_CODE (x) != MEM
1073 || ! cselib_mem_conflict_p (mem_rtx, x))
1079 /* This one overlaps. */
1080 /* We must have a mapping from this MEM's address to the
1081 value (E). Remove that, too. */
1082 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0);
1083 mem_chain = &addr->addr_list;
1086 if ((*mem_chain)->elt == v)
1088 unchain_one_elt_list (mem_chain);
1092 mem_chain = &(*mem_chain)->next;
1095 unchain_one_elt_loc_list (p);
1098 if (had_locs && v->locs == 0)
1104 /* Invalidate any locations in the table which are changed because of a
1105 store to MEM_RTX. If this is called because of a non-const call
1106 instruction, MEM_RTX is (mem:BLK const0_rtx). */
1109 cselib_invalidate_mem (mem_rtx)
1112 htab_traverse (hash_table, cselib_invalidate_mem_1, mem_rtx);
1115 /* Invalidate DEST, which is being assigned to or clobbered. The second and
1116 the third parameter exist so that this function can be passed to
1117 note_stores; they are ignored. */
1120 cselib_invalidate_rtx (dest, ignore, data)
1122 rtx ignore ATTRIBUTE_UNUSED;
1123 void *data ATTRIBUTE_UNUSED;
1125 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SIGN_EXTRACT
1126 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG)
1127 dest = XEXP (dest, 0);
1129 if (GET_CODE (dest) == REG)
1130 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
1131 else if (GET_CODE (dest) == MEM)
1132 cselib_invalidate_mem (dest);
1134 /* Some machines don't define AUTO_INC_DEC, but they still use push
1135 instructions. We need to catch that case here in order to
1136 invalidate the stack pointer correctly. Note that invalidating
1137 the stack pointer is different from invalidating DEST. */
1138 if (push_operand (dest, GET_MODE (dest)))
1139 cselib_invalidate_rtx (stack_pointer_rtx, NULL_RTX, NULL);
1142 /* Record the result of a SET instruction. DEST is being set; the source
1143 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
1144 describes its address. */
1147 cselib_record_set (dest, src_elt, dest_addr_elt)
1149 cselib_val *src_elt, *dest_addr_elt;
1151 int dreg = GET_CODE (dest) == REG ? (int) REGNO (dest) : -1;
1153 if (src_elt == 0 || side_effects_p (dest))
1158 if (REG_VALUES (dreg) == 0)
1159 VARRAY_PUSH_UINT (used_regs, dreg);
1161 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
1162 if (src_elt->locs == 0)
1164 src_elt->locs = new_elt_loc_list (src_elt->locs, dest);
1166 else if (GET_CODE (dest) == MEM && dest_addr_elt != 0)
1168 if (src_elt->locs == 0)
1170 add_mem_for_addr (dest_addr_elt, src_elt, dest);
1174 /* Describe a single set that is part of an insn. */
1179 cselib_val *src_elt;
1180 cselib_val *dest_addr_elt;
1183 /* There is no good way to determine how many elements there can be
1184 in a PARALLEL. Since it's fairly cheap, use a really large number. */
1185 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
1187 /* Record the effects of any sets in INSN. */
1189 cselib_record_sets (insn)
1194 struct set sets[MAX_SETS];
1195 rtx body = PATTERN (insn);
1197 body = PATTERN (insn);
1198 /* Find all sets. */
1199 if (GET_CODE (body) == SET)
1201 sets[0].src = SET_SRC (body);
1202 sets[0].dest = SET_DEST (body);
1205 else if (GET_CODE (body) == PARALLEL)
1207 /* Look through the PARALLEL and record the values being
1208 set, if possible. Also handle any CLOBBERs. */
1209 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
1211 rtx x = XVECEXP (body, 0, i);
1213 if (GET_CODE (x) == SET)
1215 sets[n_sets].src = SET_SRC (x);
1216 sets[n_sets].dest = SET_DEST (x);
1222 /* Look up the values that are read. Do this before invalidating the
1223 locations that are written. */
1224 for (i = 0; i < n_sets; i++)
1226 rtx dest = sets[i].dest;
1228 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
1229 the low part after invalidating any knowledge about larger modes. */
1230 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
1231 sets[i].dest = dest = XEXP (dest, 0);
1233 /* We don't know how to record anything but REG or MEM. */
1234 if (GET_CODE (dest) == REG || GET_CODE (dest) == MEM)
1236 sets[i].src_elt = cselib_lookup (sets[i].src, GET_MODE (dest), 1);
1237 if (GET_CODE (dest) == MEM)
1238 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0), Pmode, 1);
1240 sets[i].dest_addr_elt = 0;
1244 /* Invalidate all locations written by this insn. Note that the elts we
1245 looked up in the previous loop aren't affected, just some of their
1246 locations may go away. */
1247 note_stores (body, cselib_invalidate_rtx, NULL);
1249 /* Now enter the equivalences in our tables. */
1250 for (i = 0; i < n_sets; i++)
1252 rtx dest = sets[i].dest;
1253 if (GET_CODE (dest) == REG || GET_CODE (dest) == MEM)
1254 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
1258 /* Record the effects of INSN. */
1261 cselib_process_insn (insn)
1267 cselib_current_insn = insn;
1269 /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp. */
1270 if (GET_CODE (insn) == CODE_LABEL
1271 || (GET_CODE (insn) == NOTE
1272 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
1273 || (GET_CODE (insn) == INSN
1274 && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
1275 && MEM_VOLATILE_P (PATTERN (insn))))
1281 if (! INSN_P (insn))
1283 cselib_current_insn = 0;
1287 /* If this is a call instruction, forget anything stored in a
1288 call clobbered register, or, if this is not a const call, in
1290 if (GET_CODE (insn) == CALL_INSN)
1292 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1293 if (call_used_regs[i])
1294 cselib_invalidate_regno (i, VOIDmode);
1296 if (! CONST_CALL_P (insn))
1297 cselib_invalidate_mem (callmem);
1300 cselib_record_sets (insn);
1303 /* Clobber any registers which appear in REG_INC notes. We
1304 could keep track of the changes to their values, but it is
1305 unlikely to help. */
1306 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
1307 if (REG_NOTE_KIND (x) == REG_INC)
1308 cselib_invalidate_rtx (XEXP (x, 0), NULL_RTX, NULL);
1311 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
1312 after we have processed the insn. */
1313 if (GET_CODE (insn) == CALL_INSN)
1314 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
1315 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
1316 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0), NULL_RTX, NULL);
1318 cselib_current_insn = 0;
1320 if (n_useless_values > MAX_USELESS_VALUES)
1321 remove_useless_values ();
1324 /* Make sure our varrays are big enough. Not called from any cselib routines;
1325 it must be called by the user if it allocated new registers. */
1328 cselib_update_varray_sizes ()
1330 unsigned int nregs = max_reg_num ();
1332 if (nregs == cselib_nregs)
1335 cselib_nregs = nregs;
1336 VARRAY_GROW (reg_values, nregs);
1337 VARRAY_GROW (used_regs, nregs);
1340 /* Initialize cselib for one pass. The caller must also call
1341 init_alias_analysis. */
1346 /* These are only created once. */
1349 gcc_obstack_init (&cselib_obstack);
1350 cselib_startobj = obstack_alloc (&cselib_obstack, 0);
1352 callmem = gen_rtx_MEM (BLKmode, const0_rtx);
1353 ggc_add_rtx_root (&callmem, 1);
1356 cselib_nregs = max_reg_num ();
1357 VARRAY_ELT_LIST_INIT (reg_values, cselib_nregs, "reg_values");
1358 VARRAY_UINT_INIT (used_regs, cselib_nregs, "used_regs");
1359 hash_table = htab_create (31, get_value_hash, entry_and_rtx_equal_p, NULL);
1363 /* Called when the current user is done with cselib. */
1369 VARRAY_FREE (reg_values);
1370 VARRAY_FREE (used_regs);
1371 htab_delete (hash_table);