* MAINTAINERS: Update my email address.
[official-gcc.git] / gcc / cselib.c
blob0dfb1a6c11893da531eec0ca026ab87e127179b9
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, 2004 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"
42 #include "params.h"
43 #include "alloc-pool.h"
45 static bool cselib_record_memory;
46 static int entry_and_rtx_equal_p (const void *, const void *);
47 static hashval_t get_value_hash (const void *);
48 static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
49 static struct elt_loc_list *new_elt_loc_list (struct elt_loc_list *, rtx);
50 static void unchain_one_value (cselib_val *);
51 static void unchain_one_elt_list (struct elt_list **);
52 static void unchain_one_elt_loc_list (struct elt_loc_list **);
53 static void clear_table (void);
54 static int discard_useless_locs (void **, void *);
55 static int discard_useless_values (void **, void *);
56 static void remove_useless_values (void);
57 static rtx wrap_constant (enum machine_mode, rtx);
58 static unsigned int hash_rtx (rtx, enum machine_mode, int);
59 static cselib_val *new_cselib_val (unsigned int, enum machine_mode);
60 static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
61 static cselib_val *cselib_lookup_mem (rtx, int);
62 static void cselib_invalidate_regno (unsigned int, enum machine_mode);
63 static void cselib_invalidate_mem (rtx);
64 static void cselib_invalidate_rtx (rtx, rtx, void *);
65 static void cselib_record_set (rtx, cselib_val *, cselib_val *);
66 static void cselib_record_sets (rtx);
68 /* There are three ways in which cselib can look up an rtx:
69 - for a REG, the reg_values table (which is indexed by regno) is used
70 - for a MEM, we recursively look up its address and then follow the
71 addr_list of that value
72 - for everything else, we compute a hash value and go through the hash
73 table. Since different rtx's can still have the same hash value,
74 this involves walking the table entries for a given value and comparing
75 the locations of the entries with the rtx we are looking up. */
77 /* A table that enables us to look up elts by their value. */
78 static htab_t hash_table;
80 /* This is a global so we don't have to pass this through every function.
81 It is used in new_elt_loc_list to set SETTING_INSN. */
82 static rtx cselib_current_insn;
83 static bool cselib_current_insn_in_libcall;
85 /* Every new unknown value gets a unique number. */
86 static unsigned int next_unknown_value;
88 /* The number of registers we had when the varrays were last resized. */
89 static unsigned int cselib_nregs;
91 /* Count values without known locations. Whenever this grows too big, we
92 remove these useless values from the table. */
93 static int n_useless_values;
95 /* Number of useless values before we remove them from the hash table. */
96 #define MAX_USELESS_VALUES 32
98 /* This table maps from register number to values. It does not
99 contain pointers to cselib_val structures, but rather elt_lists.
100 The purpose is to be able to refer to the same register in
101 different modes. The first element of the list defines the mode in
102 which the register was set; if the mode is unknown or the value is
103 no longer valid in that mode, ELT will be NULL for the first
104 element. */
105 struct elt_list **reg_values;
106 unsigned int reg_values_size;
107 #define REG_VALUES(i) reg_values[i]
109 /* The largest number of hard regs used by any entry added to the
110 REG_VALUES table. Cleared on each clear_table() invocation. */
111 static unsigned int max_value_regs;
113 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
114 in clear_table() for fast emptying. */
115 static unsigned int *used_regs;
116 static unsigned int n_used_regs;
118 /* We pass this to cselib_invalidate_mem to invalidate all of
119 memory for a non-const call instruction. */
120 static GTY(()) rtx callmem;
122 /* Set by discard_useless_locs if it deleted the last location of any
123 value. */
124 static int values_became_useless;
126 /* Used as stop element of the containing_mem list so we can check
127 presence in the list by checking the next pointer. */
128 static cselib_val dummy_val;
130 /* Used to list all values that contain memory reference.
131 May or may not contain the useless values - the list is compacted
132 each time memory is invalidated. */
133 static cselib_val *first_containing_mem = &dummy_val;
134 static alloc_pool elt_loc_list_pool, elt_list_pool, cselib_val_pool, value_pool;
137 /* Allocate a struct elt_list and fill in its two elements with the
138 arguments. */
140 static inline struct elt_list *
141 new_elt_list (struct elt_list *next, cselib_val *elt)
143 struct elt_list *el;
144 el = pool_alloc (elt_list_pool);
145 el->next = next;
146 el->elt = elt;
147 return el;
150 /* Allocate a struct elt_loc_list and fill in its two elements with the
151 arguments. */
153 static inline struct elt_loc_list *
154 new_elt_loc_list (struct elt_loc_list *next, rtx loc)
156 struct elt_loc_list *el;
157 el = pool_alloc (elt_loc_list_pool);
158 el->next = next;
159 el->loc = loc;
160 el->canon_loc = NULL;
161 el->setting_insn = cselib_current_insn;
162 el->in_libcall = cselib_current_insn_in_libcall;
163 return el;
166 /* The elt_list at *PL is no longer needed. Unchain it and free its
167 storage. */
169 static inline void
170 unchain_one_elt_list (struct elt_list **pl)
172 struct elt_list *l = *pl;
174 *pl = l->next;
175 pool_free (elt_list_pool, l);
178 /* Likewise for elt_loc_lists. */
180 static void
181 unchain_one_elt_loc_list (struct elt_loc_list **pl)
183 struct elt_loc_list *l = *pl;
185 *pl = l->next;
186 pool_free (elt_loc_list_pool, l);
189 /* Likewise for cselib_vals. This also frees the addr_list associated with
190 V. */
192 static void
193 unchain_one_value (cselib_val *v)
195 while (v->addr_list)
196 unchain_one_elt_list (&v->addr_list);
198 pool_free (cselib_val_pool, v);
201 /* Remove all entries from the hash table. Also used during
202 initialization. If CLEAR_ALL isn't set, then only clear the entries
203 which are known to have been used. */
205 static void
206 clear_table (void)
208 unsigned int i;
210 for (i = 0; i < n_used_regs; i++)
211 REG_VALUES (used_regs[i]) = 0;
213 max_value_regs = 0;
215 n_used_regs = 0;
217 htab_empty (hash_table);
219 n_useless_values = 0;
221 next_unknown_value = 0;
223 first_containing_mem = &dummy_val;
226 /* The equality test for our hash table. The first argument ENTRY is a table
227 element (i.e. a cselib_val), while the second arg X is an rtx. We know
228 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
229 CONST of an appropriate mode. */
231 static int
232 entry_and_rtx_equal_p (const void *entry, const void *x_arg)
234 struct elt_loc_list *l;
235 const cselib_val *v = (const cselib_val *) entry;
236 rtx x = (rtx) x_arg;
237 enum machine_mode mode = GET_MODE (x);
239 if (GET_CODE (x) == CONST_INT
240 || (mode == VOIDmode && GET_CODE (x) == CONST_DOUBLE))
241 abort ();
242 if (mode != GET_MODE (v->u.val_rtx))
243 return 0;
245 /* Unwrap X if necessary. */
246 if (GET_CODE (x) == CONST
247 && (GET_CODE (XEXP (x, 0)) == CONST_INT
248 || GET_CODE (XEXP (x, 0)) == CONST_DOUBLE))
249 x = XEXP (x, 0);
251 /* We don't guarantee that distinct rtx's have different hash values,
252 so we need to do a comparison. */
253 for (l = v->locs; l; l = l->next)
254 if (rtx_equal_for_cselib_p (l->loc, x))
255 return 1;
257 return 0;
260 /* The hash function for our hash table. The value is always computed with
261 hash_rtx when adding an element; this function just extracts the hash
262 value from a cselib_val structure. */
264 static hashval_t
265 get_value_hash (const void *entry)
267 const cselib_val *v = (const cselib_val *) entry;
268 return v->value;
271 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
272 only return true for values which point to a cselib_val whose value
273 element has been set to zero, which implies the cselib_val will be
274 removed. */
277 references_value_p (rtx x, int only_useless)
279 enum rtx_code code = GET_CODE (x);
280 const char *fmt = GET_RTX_FORMAT (code);
281 int i, j;
283 if (GET_CODE (x) == VALUE
284 && (! only_useless || CSELIB_VAL_PTR (x)->locs == 0))
285 return 1;
287 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
289 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
290 return 1;
291 else if (fmt[i] == 'E')
292 for (j = 0; j < XVECLEN (x, i); j++)
293 if (references_value_p (XVECEXP (x, i, j), only_useless))
294 return 1;
297 return 0;
300 /* For all locations found in X, delete locations that reference useless
301 values (i.e. values without any location). Called through
302 htab_traverse. */
304 static int
305 discard_useless_locs (void **x, void *info ATTRIBUTE_UNUSED)
307 cselib_val *v = (cselib_val *)*x;
308 struct elt_loc_list **p = &v->locs;
309 int had_locs = v->locs != 0;
311 while (*p)
313 if (references_value_p ((*p)->loc, 1))
314 unchain_one_elt_loc_list (p);
315 else
316 p = &(*p)->next;
319 if (had_locs && v->locs == 0)
321 n_useless_values++;
322 values_became_useless = 1;
324 return 1;
327 /* If X is a value with no locations, remove it from the hashtable. */
329 static int
330 discard_useless_values (void **x, void *info ATTRIBUTE_UNUSED)
332 cselib_val *v = (cselib_val *)*x;
334 if (v->locs == 0)
336 CSELIB_VAL_PTR (v->u.val_rtx) = NULL;
337 htab_clear_slot (hash_table, x);
338 unchain_one_value (v);
339 n_useless_values--;
342 return 1;
345 /* Clean out useless values (i.e. those which no longer have locations
346 associated with them) from the hash table. */
348 static void
349 remove_useless_values (void)
351 cselib_val **p, *v;
352 /* First pass: eliminate locations that reference the value. That in
353 turn can make more values useless. */
356 values_became_useless = 0;
357 htab_traverse (hash_table, discard_useless_locs, 0);
359 while (values_became_useless);
361 /* Second pass: actually remove the values. */
363 p = &first_containing_mem;
364 for (v = *p; v != &dummy_val; v = v->next_containing_mem)
365 if (v->locs)
367 *p = v;
368 p = &(*p)->next_containing_mem;
370 *p = &dummy_val;
372 htab_traverse (hash_table, discard_useless_values, 0);
374 if (n_useless_values != 0)
375 abort ();
378 /* Return the mode in which a register was last set. If X is not a
379 register, return its mode. If the mode in which the register was
380 set is not known, or the value was already clobbered, return
381 VOIDmode. */
383 enum machine_mode
384 cselib_reg_set_mode (rtx x)
386 if (GET_CODE (x) != REG)
387 return GET_MODE (x);
389 if (REG_VALUES (REGNO (x)) == NULL
390 || REG_VALUES (REGNO (x))->elt == NULL)
391 return VOIDmode;
393 return GET_MODE (REG_VALUES (REGNO (x))->elt->u.val_rtx);
396 /* Return nonzero if we can prove that X and Y contain the same value, taking
397 our gathered information into account. */
400 rtx_equal_for_cselib_p (rtx x, rtx y)
402 enum rtx_code code;
403 const char *fmt;
404 int i;
406 if (GET_CODE (x) == REG || GET_CODE (x) == MEM)
408 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0);
410 if (e)
411 x = e->u.val_rtx;
414 if (GET_CODE (y) == REG || GET_CODE (y) == MEM)
416 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0);
418 if (e)
419 y = e->u.val_rtx;
422 if (x == y)
423 return 1;
425 if (GET_CODE (x) == VALUE && GET_CODE (y) == VALUE)
426 return CSELIB_VAL_PTR (x) == CSELIB_VAL_PTR (y);
428 if (GET_CODE (x) == VALUE)
430 cselib_val *e = CSELIB_VAL_PTR (x);
431 struct elt_loc_list *l;
433 for (l = e->locs; l; l = l->next)
435 rtx t = l->loc;
437 /* Avoid infinite recursion. */
438 if (GET_CODE (t) == REG || GET_CODE (t) == MEM)
439 continue;
440 else if (rtx_equal_for_cselib_p (t, y))
441 return 1;
444 return 0;
447 if (GET_CODE (y) == VALUE)
449 cselib_val *e = CSELIB_VAL_PTR (y);
450 struct elt_loc_list *l;
452 for (l = e->locs; l; l = l->next)
454 rtx t = l->loc;
456 if (GET_CODE (t) == REG || GET_CODE (t) == MEM)
457 continue;
458 else if (rtx_equal_for_cselib_p (x, t))
459 return 1;
462 return 0;
465 if (GET_CODE (x) != GET_CODE (y) || GET_MODE (x) != GET_MODE (y))
466 return 0;
468 /* This won't be handled correctly by the code below. */
469 if (GET_CODE (x) == LABEL_REF)
470 return XEXP (x, 0) == XEXP (y, 0);
472 code = GET_CODE (x);
473 fmt = GET_RTX_FORMAT (code);
475 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
477 int j;
479 switch (fmt[i])
481 case 'w':
482 if (XWINT (x, i) != XWINT (y, i))
483 return 0;
484 break;
486 case 'n':
487 case 'i':
488 if (XINT (x, i) != XINT (y, i))
489 return 0;
490 break;
492 case 'V':
493 case 'E':
494 /* Two vectors must have the same length. */
495 if (XVECLEN (x, i) != XVECLEN (y, i))
496 return 0;
498 /* And the corresponding elements must match. */
499 for (j = 0; j < XVECLEN (x, i); j++)
500 if (! rtx_equal_for_cselib_p (XVECEXP (x, i, j),
501 XVECEXP (y, i, j)))
502 return 0;
503 break;
505 case 'e':
506 if (! rtx_equal_for_cselib_p (XEXP (x, i), XEXP (y, i)))
507 return 0;
508 break;
510 case 'S':
511 case 's':
512 if (strcmp (XSTR (x, i), XSTR (y, i)))
513 return 0;
514 break;
516 case 'u':
517 /* These are just backpointers, so they don't matter. */
518 break;
520 case '0':
521 case 't':
522 break;
524 /* It is believed that rtx's at this level will never
525 contain anything but integers and other rtx's,
526 except for within LABEL_REFs and SYMBOL_REFs. */
527 default:
528 abort ();
531 return 1;
534 /* We need to pass down the mode of constants through the hash table
535 functions. For that purpose, wrap them in a CONST of the appropriate
536 mode. */
537 static rtx
538 wrap_constant (enum machine_mode mode, rtx x)
540 if (GET_CODE (x) != CONST_INT
541 && (GET_CODE (x) != CONST_DOUBLE || GET_MODE (x) != VOIDmode))
542 return x;
543 if (mode == VOIDmode)
544 abort ();
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. */
557 static unsigned int
558 hash_rtx (rtx x, enum machine_mode mode, int create)
560 cselib_val *e;
561 int i, j;
562 enum rtx_code code;
563 const char *fmt;
564 unsigned int hash = 0;
566 code = GET_CODE (x);
567 hash += (unsigned) code + (unsigned) GET_MODE (x);
569 switch (code)
571 case MEM:
572 case REG:
573 e = cselib_lookup (x, GET_MODE (x), create);
574 if (! e)
575 return 0;
577 return e->value;
579 case CONST_INT:
580 hash += ((unsigned) CONST_INT << 7) + (unsigned) mode + INTVAL (x);
581 return hash ? hash : (unsigned int) CONST_INT;
583 case CONST_DOUBLE:
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));
589 else
590 hash += ((unsigned) CONST_DOUBLE_LOW (x)
591 + (unsigned) CONST_DOUBLE_HIGH (x));
592 return hash ? hash : (unsigned int) CONST_DOUBLE;
594 case CONST_VECTOR:
596 int units;
597 rtx elt;
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);
607 return hash;
610 /* Assume there is only one rtx object for any given label. */
611 case LABEL_REF:
612 hash
613 += ((unsigned) LABEL_REF << 7) + (unsigned long) XEXP (x, 0);
614 return hash ? hash : (unsigned int) LABEL_REF;
616 case SYMBOL_REF:
617 hash
618 += ((unsigned) SYMBOL_REF << 7) + (unsigned long) XSTR (x, 0);
619 return hash ? hash : (unsigned int) SYMBOL_REF;
621 case PRE_DEC:
622 case PRE_INC:
623 case POST_DEC:
624 case POST_INC:
625 case POST_MODIFY:
626 case PRE_MODIFY:
627 case PC:
628 case CC0:
629 case CALL:
630 case UNSPEC_VOLATILE:
631 return 0;
633 case ASM_OPERANDS:
634 if (MEM_VOLATILE_P (x))
635 return 0;
637 break;
639 default:
640 break;
643 i = GET_RTX_LENGTH (code) - 1;
644 fmt = GET_RTX_FORMAT (code);
645 for (; i >= 0; i--)
647 if (fmt[i] == 'e')
649 rtx tem = XEXP (x, i);
650 unsigned int tem_hash = hash_rtx (tem, 0, create);
652 if (tem_hash == 0)
653 return 0;
655 hash += tem_hash;
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);
662 if (tem_hash == 0)
663 return 0;
665 hash += tem_hash;
667 else if (fmt[i] == 's')
669 const unsigned char *p = (const unsigned char *) XSTR (x, i);
671 if (p)
672 while (*p)
673 hash += *p++;
675 else if (fmt[i] == 'i')
676 hash += XINT (x, i);
677 else if (fmt[i] == '0' || fmt[i] == 't')
678 /* unused */;
679 else
680 abort ();
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
687 value is MODE. */
689 static inline cselib_val *
690 new_cselib_val (unsigned int value, enum machine_mode mode)
692 cselib_val *e = pool_alloc (cselib_val_pool);
694 #ifdef ENABLE_CHECKING
695 if (value == 0)
696 abort ();
697 #endif
699 e->value = value;
700 /* We use custom method to allocate this RTL construct because it accounts
701 about 8% of overall memory usage. */
702 e->u.val_rtx = pool_alloc (value_pool);
703 memset (e->u.val_rtx, 0, RTX_HDR_SIZE);
704 PUT_CODE (e->u.val_rtx, VALUE);
705 PUT_MODE (e->u.val_rtx, mode);
706 CSELIB_VAL_PTR (e->u.val_rtx) = e;
707 e->addr_list = 0;
708 e->locs = 0;
709 e->next_containing_mem = 0;
710 return e;
713 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
714 contains the data at this address. X is a MEM that represents the
715 value. Update the two value structures to represent this situation. */
717 static void
718 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
720 struct elt_loc_list *l;
722 /* Avoid duplicates. */
723 for (l = mem_elt->locs; l; l = l->next)
724 if (GET_CODE (l->loc) == MEM
725 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
726 return;
728 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
729 mem_elt->locs
730 = new_elt_loc_list (mem_elt->locs,
731 replace_equiv_address_nv (x, addr_elt->u.val_rtx));
732 if (mem_elt->next_containing_mem == NULL)
734 mem_elt->next_containing_mem = first_containing_mem;
735 first_containing_mem = mem_elt;
739 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
740 If CREATE, make a new one if we haven't seen it before. */
742 static cselib_val *
743 cselib_lookup_mem (rtx x, int create)
745 enum machine_mode mode = GET_MODE (x);
746 void **slot;
747 cselib_val *addr;
748 cselib_val *mem_elt;
749 struct elt_list *l;
751 if (MEM_VOLATILE_P (x) || mode == BLKmode
752 || !cselib_record_memory
753 || (FLOAT_MODE_P (mode) && flag_float_store))
754 return 0;
756 /* Look up the value for the address. */
757 addr = cselib_lookup (XEXP (x, 0), mode, create);
758 if (! addr)
759 return 0;
761 /* Find a value that describes a value of our mode at that address. */
762 for (l = addr->addr_list; l; l = l->next)
763 if (GET_MODE (l->elt->u.val_rtx) == mode)
764 return l->elt;
766 if (! create)
767 return 0;
769 mem_elt = new_cselib_val (++next_unknown_value, mode);
770 add_mem_for_addr (addr, mem_elt, x);
771 slot = htab_find_slot_with_hash (hash_table, wrap_constant (mode, x),
772 mem_elt->value, INSERT);
773 *slot = mem_elt;
774 return mem_elt;
777 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
778 with VALUE expressions. This way, it becomes independent of changes
779 to registers and memory.
780 X isn't actually modified; if modifications are needed, new rtl is
781 allocated. However, the return value can share rtl with X. */
784 cselib_subst_to_values (rtx x)
786 enum rtx_code code = GET_CODE (x);
787 const char *fmt = GET_RTX_FORMAT (code);
788 cselib_val *e;
789 struct elt_list *l;
790 rtx copy = x;
791 int i;
793 switch (code)
795 case REG:
796 l = REG_VALUES (REGNO (x));
797 if (l && l->elt == NULL)
798 l = l->next;
799 for (; l; l = l->next)
800 if (GET_MODE (l->elt->u.val_rtx) == GET_MODE (x))
801 return l->elt->u.val_rtx;
803 abort ();
805 case MEM:
806 e = cselib_lookup_mem (x, 0);
807 if (! e)
809 /* This happens for autoincrements. Assign a value that doesn't
810 match any other. */
811 e = new_cselib_val (++next_unknown_value, GET_MODE (x));
813 return e->u.val_rtx;
815 case CONST_DOUBLE:
816 case CONST_VECTOR:
817 case CONST_INT:
818 return x;
820 case POST_INC:
821 case PRE_INC:
822 case POST_DEC:
823 case PRE_DEC:
824 case POST_MODIFY:
825 case PRE_MODIFY:
826 e = new_cselib_val (++next_unknown_value, GET_MODE (x));
827 return e->u.val_rtx;
829 default:
830 break;
833 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
835 if (fmt[i] == 'e')
837 rtx t = cselib_subst_to_values (XEXP (x, i));
839 if (t != XEXP (x, i) && x == copy)
840 copy = shallow_copy_rtx (x);
842 XEXP (copy, i) = t;
844 else if (fmt[i] == 'E')
846 int j, k;
848 for (j = 0; j < XVECLEN (x, i); j++)
850 rtx t = cselib_subst_to_values (XVECEXP (x, i, j));
852 if (t != XVECEXP (x, i, j) && XVEC (x, i) == XVEC (copy, i))
854 if (x == copy)
855 copy = shallow_copy_rtx (x);
857 XVEC (copy, i) = rtvec_alloc (XVECLEN (x, i));
858 for (k = 0; k < j; k++)
859 XVECEXP (copy, i, k) = XVECEXP (x, i, k);
862 XVECEXP (copy, i, j) = t;
867 return copy;
870 /* Look up the rtl expression X in our tables and return the value it has.
871 If CREATE is zero, we return NULL if we don't know the value. Otherwise,
872 we create a new one if possible, using mode MODE if X doesn't have a mode
873 (i.e. because it's a constant). */
875 cselib_val *
876 cselib_lookup (rtx x, enum machine_mode mode, int create)
878 void **slot;
879 cselib_val *e;
880 unsigned int hashval;
882 if (GET_MODE (x) != VOIDmode)
883 mode = GET_MODE (x);
885 if (GET_CODE (x) == VALUE)
886 return CSELIB_VAL_PTR (x);
888 if (GET_CODE (x) == REG)
890 struct elt_list *l;
891 unsigned int i = REGNO (x);
893 l = REG_VALUES (i);
894 if (l && l->elt == NULL)
895 l = l->next;
896 for (; l; l = l->next)
897 if (mode == GET_MODE (l->elt->u.val_rtx))
898 return l->elt;
900 if (! create)
901 return 0;
903 if (i < FIRST_PSEUDO_REGISTER)
905 unsigned int n = hard_regno_nregs[i][mode];
907 if (n > max_value_regs)
908 max_value_regs = n;
911 e = new_cselib_val (++next_unknown_value, GET_MODE (x));
912 e->locs = new_elt_loc_list (e->locs, x);
913 if (REG_VALUES (i) == 0)
915 /* Maintain the invariant that the first entry of
916 REG_VALUES, if present, must be the value used to set the
917 register, or NULL. */
918 used_regs[n_used_regs++] = i;
919 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
921 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
922 slot = htab_find_slot_with_hash (hash_table, x, e->value, INSERT);
923 *slot = e;
924 return e;
927 if (GET_CODE (x) == MEM)
928 return cselib_lookup_mem (x, create);
930 hashval = hash_rtx (x, mode, create);
931 /* Can't even create if hashing is not possible. */
932 if (! hashval)
933 return 0;
935 slot = htab_find_slot_with_hash (hash_table, wrap_constant (mode, x),
936 hashval, create ? INSERT : NO_INSERT);
937 if (slot == 0)
938 return 0;
940 e = (cselib_val *) *slot;
941 if (e)
942 return e;
944 e = new_cselib_val (hashval, mode);
946 /* We have to fill the slot before calling cselib_subst_to_values:
947 the hash table is inconsistent until we do so, and
948 cselib_subst_to_values will need to do lookups. */
949 *slot = (void *) e;
950 e->locs = new_elt_loc_list (e->locs, cselib_subst_to_values (x));
951 return e;
954 /* Invalidate any entries in reg_values that overlap REGNO. This is called
955 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
956 is used to determine how many hard registers are being changed. If MODE
957 is VOIDmode, then only REGNO is being changed; this is used when
958 invalidating call clobbered registers across a call. */
960 static void
961 cselib_invalidate_regno (unsigned int regno, enum machine_mode mode)
963 unsigned int endregno;
964 unsigned int i;
966 /* If we see pseudos after reload, something is _wrong_. */
967 if (reload_completed && regno >= FIRST_PSEUDO_REGISTER
968 && reg_renumber[regno] >= 0)
969 abort ();
971 /* Determine the range of registers that must be invalidated. For
972 pseudos, only REGNO is affected. For hard regs, we must take MODE
973 into account, and we must also invalidate lower register numbers
974 if they contain values that overlap REGNO. */
975 if (regno < FIRST_PSEUDO_REGISTER)
977 if (mode == VOIDmode)
978 abort ();
980 if (regno < max_value_regs)
981 i = 0;
982 else
983 i = regno - max_value_regs;
985 endregno = regno + hard_regno_nregs[regno][mode];
987 else
989 i = regno;
990 endregno = regno + 1;
993 for (; i < endregno; i++)
995 struct elt_list **l = &REG_VALUES (i);
997 /* Go through all known values for this reg; if it overlaps the range
998 we're invalidating, remove the value. */
999 while (*l)
1001 cselib_val *v = (*l)->elt;
1002 struct elt_loc_list **p;
1003 unsigned int this_last = i;
1005 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
1006 this_last += hard_regno_nregs[i][GET_MODE (v->u.val_rtx)] - 1;
1008 if (this_last < regno || v == NULL)
1010 l = &(*l)->next;
1011 continue;
1014 /* We have an overlap. */
1015 if (*l == REG_VALUES (i))
1017 /* Maintain the invariant that the first entry of
1018 REG_VALUES, if present, must be the value used to set
1019 the register, or NULL. This is also nice because
1020 then we won't push the same regno onto user_regs
1021 multiple times. */
1022 (*l)->elt = NULL;
1023 l = &(*l)->next;
1025 else
1026 unchain_one_elt_list (l);
1028 /* Now, we clear the mapping from value to reg. It must exist, so
1029 this code will crash intentionally if it doesn't. */
1030 for (p = &v->locs; ; p = &(*p)->next)
1032 rtx x = (*p)->loc;
1034 if (GET_CODE (x) == REG && REGNO (x) == i)
1036 unchain_one_elt_loc_list (p);
1037 break;
1040 if (v->locs == 0)
1041 n_useless_values++;
1046 /* Return 1 if X has a value that can vary even between two
1047 executions of the program. 0 means X can be compared reliably
1048 against certain constants or near-constants. */
1050 static int
1051 cselib_rtx_varies_p (rtx x ATTRIBUTE_UNUSED, int from_alias ATTRIBUTE_UNUSED)
1053 /* We actually don't need to verify very hard. This is because
1054 if X has actually changed, we invalidate the memory anyway,
1055 so assume that all common memory addresses are
1056 invariant. */
1057 return 0;
1060 /* Invalidate any locations in the table which are changed because of a
1061 store to MEM_RTX. If this is called because of a non-const call
1062 instruction, MEM_RTX is (mem:BLK const0_rtx). */
1064 static void
1065 cselib_invalidate_mem (rtx mem_rtx)
1067 cselib_val **vp, *v, *next;
1068 int num_mems = 0;
1069 rtx mem_addr;
1071 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
1072 mem_rtx = canon_rtx (mem_rtx);
1074 vp = &first_containing_mem;
1075 for (v = *vp; v != &dummy_val; v = next)
1077 bool has_mem = false;
1078 struct elt_loc_list **p = &v->locs;
1079 int had_locs = v->locs != 0;
1081 while (*p)
1083 rtx x = (*p)->loc;
1084 rtx canon_x = (*p)->canon_loc;
1085 cselib_val *addr;
1086 struct elt_list **mem_chain;
1088 /* MEMs may occur in locations only at the top level; below
1089 that every MEM or REG is substituted by its VALUE. */
1090 if (GET_CODE (x) != MEM)
1092 p = &(*p)->next;
1093 continue;
1095 if (!canon_x)
1096 canon_x = (*p)->canon_loc = canon_rtx (x);
1097 if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
1098 && ! canon_true_dependence (mem_rtx, GET_MODE (mem_rtx), mem_addr,
1099 x, cselib_rtx_varies_p))
1101 has_mem = true;
1102 num_mems++;
1103 p = &(*p)->next;
1104 continue;
1107 /* This one overlaps. */
1108 /* We must have a mapping from this MEM's address to the
1109 value (E). Remove that, too. */
1110 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0);
1111 mem_chain = &addr->addr_list;
1112 for (;;)
1114 if ((*mem_chain)->elt == v)
1116 unchain_one_elt_list (mem_chain);
1117 break;
1120 mem_chain = &(*mem_chain)->next;
1123 unchain_one_elt_loc_list (p);
1126 if (had_locs && v->locs == 0)
1127 n_useless_values++;
1129 next = v->next_containing_mem;
1130 if (has_mem)
1132 *vp = v;
1133 vp = &(*vp)->next_containing_mem;
1135 else
1136 v->next_containing_mem = NULL;
1138 *vp = &dummy_val;
1141 /* Invalidate DEST, which is being assigned to or clobbered. The second and
1142 the third parameter exist so that this function can be passed to
1143 note_stores; they are ignored. */
1145 static void
1146 cselib_invalidate_rtx (rtx dest, rtx ignore ATTRIBUTE_UNUSED,
1147 void *data ATTRIBUTE_UNUSED)
1149 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SIGN_EXTRACT
1150 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG)
1151 dest = XEXP (dest, 0);
1153 if (GET_CODE (dest) == REG)
1154 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
1155 else if (GET_CODE (dest) == MEM)
1156 cselib_invalidate_mem (dest);
1158 /* Some machines don't define AUTO_INC_DEC, but they still use push
1159 instructions. We need to catch that case here in order to
1160 invalidate the stack pointer correctly. Note that invalidating
1161 the stack pointer is different from invalidating DEST. */
1162 if (push_operand (dest, GET_MODE (dest)))
1163 cselib_invalidate_rtx (stack_pointer_rtx, NULL_RTX, NULL);
1166 /* Record the result of a SET instruction. DEST is being set; the source
1167 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
1168 describes its address. */
1170 static void
1171 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
1173 int dreg = GET_CODE (dest) == REG ? (int) REGNO (dest) : -1;
1175 if (src_elt == 0 || side_effects_p (dest))
1176 return;
1178 if (dreg >= 0)
1180 if (dreg < FIRST_PSEUDO_REGISTER)
1182 unsigned int n = hard_regno_nregs[dreg][GET_MODE (dest)];
1184 if (n > max_value_regs)
1185 max_value_regs = n;
1188 if (REG_VALUES (dreg) == 0)
1190 used_regs[n_used_regs++] = dreg;
1191 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
1193 else
1195 if (REG_VALUES (dreg)->elt == 0)
1196 REG_VALUES (dreg)->elt = src_elt;
1197 else
1198 /* The register should have been invalidated. */
1199 abort ();
1202 if (src_elt->locs == 0)
1203 n_useless_values--;
1204 src_elt->locs = new_elt_loc_list (src_elt->locs, dest);
1206 else if (GET_CODE (dest) == MEM && dest_addr_elt != 0
1207 && cselib_record_memory)
1209 if (src_elt->locs == 0)
1210 n_useless_values--;
1211 add_mem_for_addr (dest_addr_elt, src_elt, dest);
1215 /* Describe a single set that is part of an insn. */
1216 struct set
1218 rtx src;
1219 rtx dest;
1220 cselib_val *src_elt;
1221 cselib_val *dest_addr_elt;
1224 /* There is no good way to determine how many elements there can be
1225 in a PARALLEL. Since it's fairly cheap, use a really large number. */
1226 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
1228 /* Record the effects of any sets in INSN. */
1229 static void
1230 cselib_record_sets (rtx insn)
1232 int n_sets = 0;
1233 int i;
1234 struct set sets[MAX_SETS];
1235 rtx body = PATTERN (insn);
1236 rtx cond = 0;
1238 body = PATTERN (insn);
1239 if (GET_CODE (body) == COND_EXEC)
1241 cond = COND_EXEC_TEST (body);
1242 body = COND_EXEC_CODE (body);
1245 /* Find all sets. */
1246 if (GET_CODE (body) == SET)
1248 sets[0].src = SET_SRC (body);
1249 sets[0].dest = SET_DEST (body);
1250 n_sets = 1;
1252 else if (GET_CODE (body) == PARALLEL)
1254 /* Look through the PARALLEL and record the values being
1255 set, if possible. Also handle any CLOBBERs. */
1256 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
1258 rtx x = XVECEXP (body, 0, i);
1260 if (GET_CODE (x) == SET)
1262 sets[n_sets].src = SET_SRC (x);
1263 sets[n_sets].dest = SET_DEST (x);
1264 n_sets++;
1269 /* Look up the values that are read. Do this before invalidating the
1270 locations that are written. */
1271 for (i = 0; i < n_sets; i++)
1273 rtx dest = sets[i].dest;
1275 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
1276 the low part after invalidating any knowledge about larger modes. */
1277 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
1278 sets[i].dest = dest = XEXP (dest, 0);
1280 /* We don't know how to record anything but REG or MEM. */
1281 if (GET_CODE (dest) == REG
1282 || (GET_CODE (dest) == MEM && cselib_record_memory))
1284 rtx src = sets[i].src;
1285 if (cond)
1286 src = gen_rtx_IF_THEN_ELSE (GET_MODE (src), cond, src, dest);
1287 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1);
1288 if (GET_CODE (dest) == MEM)
1289 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0), Pmode, 1);
1290 else
1291 sets[i].dest_addr_elt = 0;
1295 /* Invalidate all locations written by this insn. Note that the elts we
1296 looked up in the previous loop aren't affected, just some of their
1297 locations may go away. */
1298 note_stores (body, cselib_invalidate_rtx, NULL);
1300 /* If this is an asm, look for duplicate sets. This can happen when the
1301 user uses the same value as an output multiple times. This is valid
1302 if the outputs are not actually used thereafter. Treat this case as
1303 if the value isn't actually set. We do this by smashing the destination
1304 to pc_rtx, so that we won't record the value later. */
1305 if (n_sets >= 2 && asm_noperands (body) >= 0)
1307 for (i = 0; i < n_sets; i++)
1309 rtx dest = sets[i].dest;
1310 if (GET_CODE (dest) == REG || GET_CODE (dest) == MEM)
1312 int j;
1313 for (j = i + 1; j < n_sets; j++)
1314 if (rtx_equal_p (dest, sets[j].dest))
1316 sets[i].dest = pc_rtx;
1317 sets[j].dest = pc_rtx;
1323 /* Now enter the equivalences in our tables. */
1324 for (i = 0; i < n_sets; i++)
1326 rtx dest = sets[i].dest;
1327 if (GET_CODE (dest) == REG
1328 || (GET_CODE (dest) == MEM && cselib_record_memory))
1329 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
1333 /* Record the effects of INSN. */
1335 void
1336 cselib_process_insn (rtx insn)
1338 int i;
1339 rtx x;
1341 if (find_reg_note (insn, REG_LIBCALL, NULL))
1342 cselib_current_insn_in_libcall = true;
1343 if (find_reg_note (insn, REG_RETVAL, NULL))
1344 cselib_current_insn_in_libcall = false;
1345 cselib_current_insn = insn;
1347 /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp. */
1348 if (GET_CODE (insn) == CODE_LABEL
1349 || (GET_CODE (insn) == CALL_INSN
1350 && find_reg_note (insn, REG_SETJMP, NULL))
1351 || (GET_CODE (insn) == INSN
1352 && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
1353 && MEM_VOLATILE_P (PATTERN (insn))))
1355 clear_table ();
1356 return;
1359 if (! INSN_P (insn))
1361 cselib_current_insn = 0;
1362 return;
1365 /* If this is a call instruction, forget anything stored in a
1366 call clobbered register, or, if this is not a const call, in
1367 memory. */
1368 if (GET_CODE (insn) == CALL_INSN)
1370 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1371 if (call_used_regs[i])
1372 cselib_invalidate_regno (i, reg_raw_mode[i]);
1374 if (! CONST_OR_PURE_CALL_P (insn))
1375 cselib_invalidate_mem (callmem);
1378 cselib_record_sets (insn);
1380 #ifdef AUTO_INC_DEC
1381 /* Clobber any registers which appear in REG_INC notes. We
1382 could keep track of the changes to their values, but it is
1383 unlikely to help. */
1384 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
1385 if (REG_NOTE_KIND (x) == REG_INC)
1386 cselib_invalidate_rtx (XEXP (x, 0), NULL_RTX, NULL);
1387 #endif
1389 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
1390 after we have processed the insn. */
1391 if (GET_CODE (insn) == CALL_INSN)
1392 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
1393 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
1394 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0), NULL_RTX, NULL);
1396 cselib_current_insn = 0;
1398 if (n_useless_values > MAX_USELESS_VALUES)
1399 remove_useless_values ();
1402 /* Initialize cselib for one pass. The caller must also call
1403 init_alias_analysis. */
1405 void
1406 cselib_init (bool record_memory)
1408 elt_list_pool = create_alloc_pool ("elt_list",
1409 sizeof (struct elt_list), 10);
1410 elt_loc_list_pool = create_alloc_pool ("elt_loc_list",
1411 sizeof (struct elt_loc_list), 10);
1412 cselib_val_pool = create_alloc_pool ("cselib_val_list",
1413 sizeof (cselib_val), 10);
1414 value_pool = create_alloc_pool ("value",
1415 RTX_SIZE (VALUE), 100);
1416 cselib_record_memory = record_memory;
1417 /* This is only created once. */
1418 if (! callmem)
1419 callmem = gen_rtx_MEM (BLKmode, const0_rtx);
1421 cselib_nregs = max_reg_num ();
1423 /* We preserve reg_values to allow expensive clearing of the whole thing.
1424 Reallocate it however if it happens to be too large. */
1425 if (!reg_values || reg_values_size < cselib_nregs
1426 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
1428 if (reg_values)
1429 free (reg_values);
1430 /* Some space for newly emit instructions so we don't end up
1431 reallocating in between passes. */
1432 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
1433 reg_values = xcalloc (reg_values_size, sizeof (reg_values));
1435 used_regs = xmalloc (sizeof (*used_regs) * cselib_nregs);
1436 n_used_regs = 0;
1437 hash_table = htab_create (31, get_value_hash, entry_and_rtx_equal_p, NULL);
1438 cselib_current_insn_in_libcall = false;
1441 /* Called when the current user is done with cselib. */
1443 void
1444 cselib_finish (void)
1446 free_alloc_pool (elt_list_pool);
1447 free_alloc_pool (elt_loc_list_pool);
1448 free_alloc_pool (cselib_val_pool);
1449 free_alloc_pool (value_pool);
1450 clear_table ();
1451 htab_delete (hash_table);
1452 free (used_regs);
1453 used_regs = 0;
1454 hash_table = 0;
1455 n_useless_values = 0;
1456 next_unknown_value = 0;
1459 #include "gt-cselib.h"