2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / cselib.c
blob8cb325d61a5ece14ba4879e4520edd5346736258
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
10 version.
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
15 for more details.
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
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "flags.h"
32 #include "real.h"
33 #include "insn-config.h"
34 #include "recog.h"
35 #include "function.h"
36 #include "expr.h"
37 #include "toplev.h"
38 #include "output.h"
39 #include "ggc.h"
40 #include "hashtab.h"
41 #include "cselib.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
102 element. */
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
126 value. */
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
140 arguments. */
142 static struct elt_list *
143 new_elt_list (struct elt_list *next, cselib_val *elt)
145 struct elt_list *el = empty_elt_lists;
147 if (el)
148 empty_elt_lists = el->next;
149 else
150 el = ggc_alloc (sizeof (struct elt_list));
151 el->next = next;
152 el->elt = elt;
153 return el;
156 /* Allocate a struct elt_loc_list and fill in its two elements with the
157 arguments. */
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;
164 if (el)
165 empty_elt_loc_lists = el->next;
166 else
167 el = ggc_alloc (sizeof (struct elt_loc_list));
168 el->next = next;
169 el->loc = loc;
170 el->setting_insn = cselib_current_insn;
171 el->in_libcall = cselib_current_insn_in_libcall;
172 return el;
175 /* The elt_list at *PL is no longer needed. Unchain it and free its
176 storage. */
178 static void
179 unchain_one_elt_list (struct elt_list **pl)
181 struct elt_list *l = *pl;
183 *pl = l->next;
184 l->next = empty_elt_lists;
185 empty_elt_lists = l;
188 /* Likewise for elt_loc_lists. */
190 static void
191 unchain_one_elt_loc_list (struct elt_loc_list **pl)
193 struct elt_loc_list *l = *pl;
195 *pl = l->next;
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
201 V. */
203 static void
204 unchain_one_value (cselib_val *v)
206 while (v->addr_list)
207 unchain_one_elt_list (&v->addr_list);
209 v->u.next_free = empty_vals;
210 empty_vals = v;
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. */
217 static void
218 clear_table (void)
220 unsigned int i;
222 for (i = 0; i < VARRAY_ACTIVE_SIZE (used_regs); i++)
223 REG_VALUES (VARRAY_UINT (used_regs, i)) = 0;
225 max_value_regs = 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. */
243 static int
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;
248 rtx x = (rtx) x_arg;
249 enum machine_mode mode = GET_MODE (x);
251 if (GET_CODE (x) == CONST_INT
252 || (mode == VOIDmode && GET_CODE (x) == CONST_DOUBLE))
253 abort ();
254 if (mode != GET_MODE (v->u.val_rtx))
255 return 0;
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))
261 x = XEXP (x, 0);
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))
267 return 1;
269 return 0;
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. */
276 static hashval_t
277 get_value_hash (const void *entry)
279 const cselib_val *v = (const cselib_val *) entry;
280 return v->value;
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
286 removed. */
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);
293 int i, j;
295 if (GET_CODE (x) == VALUE
296 && (! only_useless || CSELIB_VAL_PTR (x)->locs == 0))
297 return 1;
299 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
301 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
302 return 1;
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))
306 return 1;
309 return 0;
312 /* For all locations found in X, delete locations that reference useless
313 values (i.e. values without any location). Called through
314 htab_traverse. */
316 static int
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;
323 while (*p)
325 if (references_value_p ((*p)->loc, 1))
326 unchain_one_elt_loc_list (p);
327 else
328 p = &(*p)->next;
331 if (had_locs && v->locs == 0)
333 n_useless_values++;
334 values_became_useless = 1;
336 return 1;
339 /* If X is a value with no locations, remove it from the hashtable. */
341 static int
342 discard_useless_values (void **x, void *info ATTRIBUTE_UNUSED)
344 cselib_val *v = (cselib_val *)*x;
346 if (v->locs == 0)
348 htab_clear_slot (hash_table, x);
349 unchain_one_value (v);
350 n_useless_values--;
353 return 1;
356 /* Clean out useless values (i.e. those which no longer have locations
357 associated with them) from the hash table. */
359 static void
360 remove_useless_values (void)
362 cselib_val **p, *v;
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)
377 if (v->locs)
379 *p = v;
380 p = &(*p)->next_containing_mem;
382 *p = &dummy_val;
384 if (n_useless_values != 0)
385 abort ();
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
391 VOIDmode. */
393 enum machine_mode
394 cselib_reg_set_mode (rtx x)
396 if (GET_CODE (x) != REG)
397 return GET_MODE (x);
399 if (REG_VALUES (REGNO (x)) == NULL
400 || REG_VALUES (REGNO (x))->elt == NULL)
401 return VOIDmode;
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)
412 enum rtx_code code;
413 const char *fmt;
414 int i;
416 if (GET_CODE (x) == REG || GET_CODE (x) == MEM)
418 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0);
420 if (e)
421 x = e->u.val_rtx;
424 if (GET_CODE (y) == REG || GET_CODE (y) == MEM)
426 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0);
428 if (e)
429 y = e->u.val_rtx;
432 if (x == y)
433 return 1;
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)
445 rtx t = l->loc;
447 /* Avoid infinite recursion. */
448 if (GET_CODE (t) == REG || GET_CODE (t) == MEM)
449 continue;
450 else if (rtx_equal_for_cselib_p (t, y))
451 return 1;
454 return 0;
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)
464 rtx t = l->loc;
466 if (GET_CODE (t) == REG || GET_CODE (t) == MEM)
467 continue;
468 else if (rtx_equal_for_cselib_p (x, t))
469 return 1;
472 return 0;
475 if (GET_CODE (x) != GET_CODE (y) || GET_MODE (x) != GET_MODE (y))
476 return 0;
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);
482 code = GET_CODE (x);
483 fmt = GET_RTX_FORMAT (code);
485 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
487 int j;
489 switch (fmt[i])
491 case 'w':
492 if (XWINT (x, i) != XWINT (y, i))
493 return 0;
494 break;
496 case 'n':
497 case 'i':
498 if (XINT (x, i) != XINT (y, i))
499 return 0;
500 break;
502 case 'V':
503 case 'E':
504 /* Two vectors must have the same length. */
505 if (XVECLEN (x, i) != XVECLEN (y, i))
506 return 0;
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),
511 XVECEXP (y, i, j)))
512 return 0;
513 break;
515 case 'e':
516 if (! rtx_equal_for_cselib_p (XEXP (x, i), XEXP (y, i)))
517 return 0;
518 break;
520 case 'S':
521 case 's':
522 if (strcmp (XSTR (x, i), XSTR (y, i)))
523 return 0;
524 break;
526 case 'u':
527 /* These are just backpointers, so they don't matter. */
528 break;
530 case '0':
531 case 't':
532 break;
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. */
537 default:
538 abort ();
541 return 1;
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
546 mode. */
547 static rtx
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))
552 return x;
553 if (mode == VOIDmode)
554 abort ();
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. */
567 static unsigned int
568 hash_rtx (rtx x, enum machine_mode mode, int create)
570 cselib_val *e;
571 int i, j;
572 enum rtx_code code;
573 const char *fmt;
574 unsigned int hash = 0;
576 code = GET_CODE (x);
577 hash += (unsigned) code + (unsigned) GET_MODE (x);
579 switch (code)
581 case MEM:
582 case REG:
583 e = cselib_lookup (x, GET_MODE (x), create);
584 if (! e)
585 return 0;
587 return e->value;
589 case CONST_INT:
590 hash += ((unsigned) CONST_INT << 7) + (unsigned) mode + INTVAL (x);
591 return hash ? hash : (unsigned int) CONST_INT;
593 case CONST_DOUBLE:
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));
599 else
600 hash += ((unsigned) CONST_DOUBLE_LOW (x)
601 + (unsigned) CONST_DOUBLE_HIGH (x));
602 return hash ? hash : (unsigned int) CONST_DOUBLE;
604 case CONST_VECTOR:
606 int units;
607 rtx elt;
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);
617 return hash;
620 /* Assume there is only one rtx object for any given label. */
621 case LABEL_REF:
622 hash
623 += ((unsigned) LABEL_REF << 7) + (unsigned long) XEXP (x, 0);
624 return hash ? hash : (unsigned int) LABEL_REF;
626 case SYMBOL_REF:
627 hash
628 += ((unsigned) SYMBOL_REF << 7) + (unsigned long) XSTR (x, 0);
629 return hash ? hash : (unsigned int) SYMBOL_REF;
631 case PRE_DEC:
632 case PRE_INC:
633 case POST_DEC:
634 case POST_INC:
635 case POST_MODIFY:
636 case PRE_MODIFY:
637 case PC:
638 case CC0:
639 case CALL:
640 case UNSPEC_VOLATILE:
641 return 0;
643 case ASM_OPERANDS:
644 if (MEM_VOLATILE_P (x))
645 return 0;
647 break;
649 default:
650 break;
653 i = GET_RTX_LENGTH (code) - 1;
654 fmt = GET_RTX_FORMAT (code);
655 for (; i >= 0; i--)
657 if (fmt[i] == 'e')
659 rtx tem = XEXP (x, i);
660 unsigned int tem_hash = hash_rtx (tem, 0, create);
662 if (tem_hash == 0)
663 return 0;
665 hash += tem_hash;
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);
672 if (tem_hash == 0)
673 return 0;
675 hash += tem_hash;
677 else if (fmt[i] == 's')
679 const unsigned char *p = (const unsigned char *) XSTR (x, i);
681 if (p)
682 while (*p)
683 hash += *p++;
685 else if (fmt[i] == 'i')
686 hash += XINT (x, i);
687 else if (fmt[i] == '0' || fmt[i] == 't')
688 /* unused */;
689 else
690 abort ();
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
697 value is MODE. */
699 static cselib_val *
700 new_cselib_val (unsigned int value, enum machine_mode mode)
702 cselib_val *e = empty_vals;
704 if (e)
705 empty_vals = e->u.next_free;
706 else
707 e = ggc_alloc (sizeof (cselib_val));
709 if (value == 0)
710 abort ();
712 e->value = value;
713 e->u.val_rtx = gen_rtx_VALUE (mode);
714 CSELIB_VAL_PTR (e->u.val_rtx) = e;
715 e->addr_list = 0;
716 e->locs = 0;
717 e->next_containing_mem = 0;
718 return e;
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. */
725 static void
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)
734 return;
736 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
737 mem_elt->locs
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. */
750 static cselib_val *
751 cselib_lookup_mem (rtx x, int create)
753 enum machine_mode mode = GET_MODE (x);
754 void **slot;
755 cselib_val *addr;
756 cselib_val *mem_elt;
757 struct elt_list *l;
759 if (MEM_VOLATILE_P (x) || mode == BLKmode
760 || (FLOAT_MODE_P (mode) && flag_float_store))
761 return 0;
763 /* Look up the value for the address. */
764 addr = cselib_lookup (XEXP (x, 0), mode, create);
765 if (! addr)
766 return 0;
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)
771 return l->elt;
773 if (! create)
774 return 0;
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);
780 *slot = mem_elt;
781 return mem_elt;
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);
795 cselib_val *e;
796 struct elt_list *l;
797 rtx copy = x;
798 int i;
800 switch (code)
802 case REG:
803 l = REG_VALUES (REGNO (x));
804 if (l && l->elt == NULL)
805 l = l->next;
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;
810 abort ();
812 case MEM:
813 e = cselib_lookup_mem (x, 0);
814 if (! e)
816 /* This happens for autoincrements. Assign a value that doesn't
817 match any other. */
818 e = new_cselib_val (++next_unknown_value, GET_MODE (x));
820 return e->u.val_rtx;
822 case CONST_DOUBLE:
823 case CONST_VECTOR:
824 case CONST_INT:
825 return x;
827 case POST_INC:
828 case PRE_INC:
829 case POST_DEC:
830 case PRE_DEC:
831 case POST_MODIFY:
832 case PRE_MODIFY:
833 e = new_cselib_val (++next_unknown_value, GET_MODE (x));
834 return e->u.val_rtx;
836 default:
837 break;
840 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
842 if (fmt[i] == 'e')
844 rtx t = cselib_subst_to_values (XEXP (x, i));
846 if (t != XEXP (x, i) && x == copy)
847 copy = shallow_copy_rtx (x);
849 XEXP (copy, i) = t;
851 else if (fmt[i] == 'E')
853 int j, k;
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))
861 if (x == copy)
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;
874 return copy;
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). */
882 cselib_val *
883 cselib_lookup (rtx x, enum machine_mode mode, int create)
885 void **slot;
886 cselib_val *e;
887 unsigned int hashval;
889 if (GET_MODE (x) != VOIDmode)
890 mode = GET_MODE (x);
892 if (GET_CODE (x) == VALUE)
893 return CSELIB_VAL_PTR (x);
895 if (GET_CODE (x) == REG)
897 struct elt_list *l;
898 unsigned int i = REGNO (x);
900 l = REG_VALUES (i);
901 if (l && l->elt == NULL)
902 l = l->next;
903 for (; l; l = l->next)
904 if (mode == GET_MODE (l->elt->u.val_rtx))
905 return l->elt;
907 if (! create)
908 return 0;
910 if (i < FIRST_PSEUDO_REGISTER)
912 unsigned int n = HARD_REGNO_NREGS (i, mode);
914 if (n > max_value_regs)
915 max_value_regs = n;
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);
930 *slot = e;
931 return e;
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. */
939 if (! hashval)
940 return 0;
942 slot = htab_find_slot_with_hash (hash_table, wrap_constant (mode, x),
943 hashval, create ? INSERT : NO_INSERT);
944 if (slot == 0)
945 return 0;
947 e = (cselib_val *) *slot;
948 if (e)
949 return e;
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. */
956 *slot = (void *) e;
957 e->locs = new_elt_loc_list (e->locs, cselib_subst_to_values (x));
958 return e;
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. */
967 static void
968 cselib_invalidate_regno (unsigned int regno, enum machine_mode mode)
970 unsigned int endregno;
971 unsigned int i;
973 /* If we see pseudos after reload, something is _wrong_. */
974 if (reload_completed && regno >= FIRST_PSEUDO_REGISTER
975 && reg_renumber[regno] >= 0)
976 abort ();
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)
985 abort ();
987 if (regno < max_value_regs)
988 i = 0;
989 else
990 i = regno - max_value_regs;
992 endregno = regno + HARD_REGNO_NREGS (regno, mode);
994 else
996 i = regno;
997 endregno = regno + 1;
1000 for (; i < endregno; i++)
1002 struct elt_list **l = &REG_VALUES (i);
1004 /* Go through all known values for this reg; if it overlaps the range
1005 we're invalidating, remove the value. */
1006 while (*l)
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)
1017 l = &(*l)->next;
1018 continue;
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
1028 multiple times. */
1029 (*l)->elt = NULL;
1030 l = &(*l)->next;
1032 else
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)
1039 rtx x = (*p)->loc;
1041 if (GET_CODE (x) == REG && REGNO (x) == i)
1043 unchain_one_elt_loc_list (p);
1044 break;
1047 if (v->locs == 0)
1048 n_useless_values++;
1053 /* The memory at address MEM_BASE is being changed.
1054 Return whether this change will invalidate VAL. */
1056 static int
1057 cselib_mem_conflict_p (rtx mem_base, rtx val)
1059 enum rtx_code code;
1060 const char *fmt;
1061 int i, j;
1063 code = GET_CODE (val);
1064 switch (code)
1066 /* Get rid of a few simple cases quickly. */
1067 case REG:
1068 case PC:
1069 case CC0:
1070 case SCRATCH:
1071 case CONST:
1072 case CONST_INT:
1073 case CONST_DOUBLE:
1074 case CONST_VECTOR:
1075 case SYMBOL_REF:
1076 case LABEL_REF:
1077 return 0;
1079 case MEM:
1080 if (GET_MODE (mem_base) == BLKmode
1081 || GET_MODE (val) == BLKmode
1082 || anti_dependence (val, mem_base))
1083 return 1;
1085 /* The address may contain nested MEMs. */
1086 break;
1088 default:
1089 break;
1092 fmt = GET_RTX_FORMAT (code);
1093 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1095 if (fmt[i] == 'e')
1097 if (cselib_mem_conflict_p (mem_base, XEXP (val, i)))
1098 return 1;
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)))
1103 return 1;
1106 return 0;
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). */
1113 static void
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;
1125 while (*p)
1127 rtx x = (*p)->loc;
1128 cselib_val *addr;
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)
1135 p = &(*p)->next;
1136 continue;
1138 if (! cselib_mem_conflict_p (mem_rtx, x))
1140 has_mem = true;
1141 p = &(*p)->next;
1142 continue;
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;
1150 for (;;)
1152 if ((*mem_chain)->elt == v)
1154 unchain_one_elt_list (mem_chain);
1155 break;
1158 mem_chain = &(*mem_chain)->next;
1161 unchain_one_elt_loc_list (p);
1164 if (had_locs && v->locs == 0)
1165 n_useless_values++;
1167 next = v->next_containing_mem;
1168 if (has_mem)
1170 *vp = v;
1171 vp = &(*vp)->next_containing_mem;
1173 else
1174 v->next_containing_mem = NULL;
1176 *vp = &dummy_val;
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. */
1183 static void
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. */
1208 static void
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))
1214 return;
1216 if (dreg >= 0)
1218 if (dreg < FIRST_PSEUDO_REGISTER)
1220 unsigned int n = HARD_REGNO_NREGS (dreg, GET_MODE (dest));
1222 if (n > max_value_regs)
1223 max_value_regs = n;
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);
1231 else
1233 if (REG_VALUES (dreg)->elt == 0)
1234 REG_VALUES (dreg)->elt = src_elt;
1235 else
1236 /* The register should have been invalidated. */
1237 abort ();
1240 if (src_elt->locs == 0)
1241 n_useless_values--;
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)
1247 n_useless_values--;
1248 add_mem_for_addr (dest_addr_elt, src_elt, dest);
1252 /* Describe a single set that is part of an insn. */
1253 struct set
1255 rtx src;
1256 rtx dest;
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. */
1266 static void
1267 cselib_record_sets (rtx insn)
1269 int n_sets = 0;
1270 int i;
1271 struct set sets[MAX_SETS];
1272 rtx body = PATTERN (insn);
1273 rtx cond = 0;
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);
1287 n_sets = 1;
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);
1301 n_sets++;
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;
1321 if (cond)
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);
1326 else
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. */
1347 void
1348 cselib_process_insn (rtx insn)
1350 int i;
1351 rtx x;
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))))
1367 clear_table ();
1368 return;
1371 if (! INSN_P (insn))
1373 cselib_current_insn = 0;
1374 return;
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
1379 memory. */
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);
1392 #ifdef AUTO_INC_DEC
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);
1399 #endif
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. */
1417 void
1418 cselib_update_varray_sizes (void)
1420 unsigned int nregs = max_reg_num ();
1422 if (nregs == cselib_nregs)
1423 return;
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. */
1433 void
1434 cselib_init (void)
1436 /* This is only created once. */
1437 if (! callmem)
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;
1446 else
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,
1452 NULL);
1453 cselib_current_insn_in_libcall = false;
1456 /* Called when the current user is done with cselib. */
1458 void
1459 cselib_finish (void)
1461 clear_table ();
1462 reg_values_old = reg_values;
1463 reg_values = 0;
1464 used_regs_old = used_regs;
1465 used_regs = 0;
1466 hash_table = 0;
1467 n_useless_values = 0;
1468 next_unknown_value = 0;
1471 #include "gt-cselib.h"