* interface.c: Fix previous checkin (an incomplete patch
[official-gcc.git] / gcc / cselib.c
blob13fc5326dc0210dea6a2323bccb0faec94568f02
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, 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, 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 "emit-rtl.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"
44 #include "target.h"
46 static bool cselib_record_memory;
47 static int entry_and_rtx_equal_p (const void *, const void *);
48 static hashval_t get_value_hash (const void *);
49 static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
50 static struct elt_loc_list *new_elt_loc_list (struct elt_loc_list *, rtx);
51 static void unchain_one_value (cselib_val *);
52 static void unchain_one_elt_list (struct elt_list **);
53 static void unchain_one_elt_loc_list (struct elt_loc_list **);
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 cselib_hash_rtx (rtx, 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_record_set (rtx, cselib_val *, cselib_val *);
65 static void cselib_record_sets (rtx);
67 /* There are three ways in which cselib can look up an rtx:
68 - for a REG, the reg_values table (which is indexed by regno) is used
69 - for a MEM, we recursively look up its address and then follow the
70 addr_list of that value
71 - for everything else, we compute a hash value and go through the hash
72 table. Since different rtx's can still have the same hash value,
73 this involves walking the table entries for a given value and comparing
74 the locations of the entries with the rtx we are looking up. */
76 /* A table that enables us to look up elts by their value. */
77 static htab_t hash_table;
79 /* This is a global so we don't have to pass this through every function.
80 It is used in new_elt_loc_list to set SETTING_INSN. */
81 static rtx cselib_current_insn;
82 static bool cselib_current_insn_in_libcall;
84 /* Every new unknown value gets a unique number. */
85 static unsigned int next_unknown_value;
87 /* The number of registers we had when the varrays were last resized. */
88 static unsigned int cselib_nregs;
90 /* Count values without known locations. Whenever this grows too big, we
91 remove these useless values from the table. */
92 static int n_useless_values;
94 /* Number of useless values before we remove them from the hash table. */
95 #define MAX_USELESS_VALUES 32
97 /* This table maps from register number to values. It does not
98 contain pointers to cselib_val structures, but rather elt_lists.
99 The purpose is to be able to refer to the same register in
100 different modes. The first element of the list defines the mode in
101 which the register was set; if the mode is unknown or the value is
102 no longer valid in that mode, ELT will be NULL for the first
103 element. */
104 static struct elt_list **reg_values;
105 static unsigned int reg_values_size;
106 #define REG_VALUES(i) reg_values[i]
108 /* The largest number of hard regs used by any entry added to the
109 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
110 static unsigned int max_value_regs;
112 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
113 in cselib_clear_table() for fast emptying. */
114 static unsigned int *used_regs;
115 static unsigned int n_used_regs;
117 /* We pass this to cselib_invalidate_mem to invalidate all of
118 memory for a non-const call instruction. */
119 static GTY(()) rtx callmem;
121 /* Set by discard_useless_locs if it deleted the last location of any
122 value. */
123 static int values_became_useless;
125 /* Used as stop element of the containing_mem list so we can check
126 presence in the list by checking the next pointer. */
127 static cselib_val dummy_val;
129 /* Used to list all values that contain memory reference.
130 May or may not contain the useless values - the list is compacted
131 each time memory is invalidated. */
132 static cselib_val *first_containing_mem = &dummy_val;
133 static alloc_pool elt_loc_list_pool, elt_list_pool, cselib_val_pool, value_pool;
136 /* Allocate a struct elt_list and fill in its two elements with the
137 arguments. */
139 static inline struct elt_list *
140 new_elt_list (struct elt_list *next, cselib_val *elt)
142 struct elt_list *el;
143 el = pool_alloc (elt_list_pool);
144 el->next = next;
145 el->elt = elt;
146 return el;
149 /* Allocate a struct elt_loc_list and fill in its two elements with the
150 arguments. */
152 static inline struct elt_loc_list *
153 new_elt_loc_list (struct elt_loc_list *next, rtx loc)
155 struct elt_loc_list *el;
156 el = pool_alloc (elt_loc_list_pool);
157 el->next = next;
158 el->loc = loc;
159 el->setting_insn = cselib_current_insn;
160 el->in_libcall = cselib_current_insn_in_libcall;
161 return el;
164 /* The elt_list at *PL is no longer needed. Unchain it and free its
165 storage. */
167 static inline void
168 unchain_one_elt_list (struct elt_list **pl)
170 struct elt_list *l = *pl;
172 *pl = l->next;
173 pool_free (elt_list_pool, l);
176 /* Likewise for elt_loc_lists. */
178 static void
179 unchain_one_elt_loc_list (struct elt_loc_list **pl)
181 struct elt_loc_list *l = *pl;
183 *pl = l->next;
184 pool_free (elt_loc_list_pool, l);
187 /* Likewise for cselib_vals. This also frees the addr_list associated with
188 V. */
190 static void
191 unchain_one_value (cselib_val *v)
193 while (v->addr_list)
194 unchain_one_elt_list (&v->addr_list);
196 pool_free (cselib_val_pool, v);
199 /* Remove all entries from the hash table. Also used during
200 initialization. If CLEAR_ALL isn't set, then only clear the entries
201 which are known to have been used. */
203 void
204 cselib_clear_table (void)
206 unsigned int i;
208 for (i = 0; i < n_used_regs; i++)
209 REG_VALUES (used_regs[i]) = 0;
211 max_value_regs = 0;
213 n_used_regs = 0;
215 htab_empty (hash_table);
217 n_useless_values = 0;
219 next_unknown_value = 0;
221 first_containing_mem = &dummy_val;
224 /* The equality test for our hash table. The first argument ENTRY is a table
225 element (i.e. a cselib_val), while the second arg X is an rtx. We know
226 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
227 CONST of an appropriate mode. */
229 static int
230 entry_and_rtx_equal_p (const void *entry, const void *x_arg)
232 struct elt_loc_list *l;
233 const cselib_val *v = (const cselib_val *) entry;
234 rtx x = (rtx) x_arg;
235 enum machine_mode mode = GET_MODE (x);
237 gcc_assert (GET_CODE (x) != CONST_INT
238 && (mode != VOIDmode || GET_CODE (x) != CONST_DOUBLE));
240 if (mode != GET_MODE (v->u.val_rtx))
241 return 0;
243 /* Unwrap X if necessary. */
244 if (GET_CODE (x) == CONST
245 && (GET_CODE (XEXP (x, 0)) == CONST_INT
246 || GET_CODE (XEXP (x, 0)) == CONST_DOUBLE))
247 x = XEXP (x, 0);
249 /* We don't guarantee that distinct rtx's have different hash values,
250 so we need to do a comparison. */
251 for (l = v->locs; l; l = l->next)
252 if (rtx_equal_for_cselib_p (l->loc, x))
253 return 1;
255 return 0;
258 /* The hash function for our hash table. The value is always computed with
259 cselib_hash_rtx when adding an element; this function just extracts the
260 hash value from a cselib_val structure. */
262 static hashval_t
263 get_value_hash (const void *entry)
265 const cselib_val *v = (const cselib_val *) entry;
266 return v->value;
269 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
270 only return true for values which point to a cselib_val whose value
271 element has been set to zero, which implies the cselib_val will be
272 removed. */
275 references_value_p (rtx x, int only_useless)
277 enum rtx_code code = GET_CODE (x);
278 const char *fmt = GET_RTX_FORMAT (code);
279 int i, j;
281 if (GET_CODE (x) == VALUE
282 && (! only_useless || CSELIB_VAL_PTR (x)->locs == 0))
283 return 1;
285 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
287 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
288 return 1;
289 else if (fmt[i] == 'E')
290 for (j = 0; j < XVECLEN (x, i); j++)
291 if (references_value_p (XVECEXP (x, i, j), only_useless))
292 return 1;
295 return 0;
298 /* For all locations found in X, delete locations that reference useless
299 values (i.e. values without any location). Called through
300 htab_traverse. */
302 static int
303 discard_useless_locs (void **x, void *info ATTRIBUTE_UNUSED)
305 cselib_val *v = (cselib_val *)*x;
306 struct elt_loc_list **p = &v->locs;
307 int had_locs = v->locs != 0;
309 while (*p)
311 if (references_value_p ((*p)->loc, 1))
312 unchain_one_elt_loc_list (p);
313 else
314 p = &(*p)->next;
317 if (had_locs && v->locs == 0)
319 n_useless_values++;
320 values_became_useless = 1;
322 return 1;
325 /* If X is a value with no locations, remove it from the hashtable. */
327 static int
328 discard_useless_values (void **x, void *info ATTRIBUTE_UNUSED)
330 cselib_val *v = (cselib_val *)*x;
332 if (v->locs == 0)
334 CSELIB_VAL_PTR (v->u.val_rtx) = NULL;
335 htab_clear_slot (hash_table, x);
336 unchain_one_value (v);
337 n_useless_values--;
340 return 1;
343 /* Clean out useless values (i.e. those which no longer have locations
344 associated with them) from the hash table. */
346 static void
347 remove_useless_values (void)
349 cselib_val **p, *v;
350 /* First pass: eliminate locations that reference the value. That in
351 turn can make more values useless. */
354 values_became_useless = 0;
355 htab_traverse (hash_table, discard_useless_locs, 0);
357 while (values_became_useless);
359 /* Second pass: actually remove the values. */
361 p = &first_containing_mem;
362 for (v = *p; v != &dummy_val; v = v->next_containing_mem)
363 if (v->locs)
365 *p = v;
366 p = &(*p)->next_containing_mem;
368 *p = &dummy_val;
370 htab_traverse (hash_table, discard_useless_values, 0);
372 gcc_assert (!n_useless_values);
375 /* Return the mode in which a register was last set. If X is not a
376 register, return its mode. If the mode in which the register was
377 set is not known, or the value was already clobbered, return
378 VOIDmode. */
380 enum machine_mode
381 cselib_reg_set_mode (rtx x)
383 if (!REG_P (x))
384 return GET_MODE (x);
386 if (REG_VALUES (REGNO (x)) == NULL
387 || REG_VALUES (REGNO (x))->elt == NULL)
388 return VOIDmode;
390 return GET_MODE (REG_VALUES (REGNO (x))->elt->u.val_rtx);
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 (rtx x, rtx y)
399 enum rtx_code code;
400 const char *fmt;
401 int i;
403 if (REG_P (x) || MEM_P (x))
405 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0);
407 if (e)
408 x = e->u.val_rtx;
411 if (REG_P (y) || MEM_P (y))
413 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0);
415 if (e)
416 y = e->u.val_rtx;
419 if (x == y)
420 return 1;
422 if (GET_CODE (x) == VALUE && GET_CODE (y) == VALUE)
423 return CSELIB_VAL_PTR (x) == CSELIB_VAL_PTR (y);
425 if (GET_CODE (x) == VALUE)
427 cselib_val *e = CSELIB_VAL_PTR (x);
428 struct elt_loc_list *l;
430 for (l = e->locs; l; l = l->next)
432 rtx t = l->loc;
434 /* Avoid infinite recursion. */
435 if (REG_P (t) || MEM_P (t))
436 continue;
437 else if (rtx_equal_for_cselib_p (t, y))
438 return 1;
441 return 0;
444 if (GET_CODE (y) == VALUE)
446 cselib_val *e = CSELIB_VAL_PTR (y);
447 struct elt_loc_list *l;
449 for (l = e->locs; l; l = l->next)
451 rtx t = l->loc;
453 if (REG_P (t) || MEM_P (t))
454 continue;
455 else if (rtx_equal_for_cselib_p (x, t))
456 return 1;
459 return 0;
462 if (GET_CODE (x) != GET_CODE (y) || GET_MODE (x) != GET_MODE (y))
463 return 0;
465 /* These won't be handled correctly by the code below. */
466 switch (GET_CODE (x))
468 case CONST_DOUBLE:
469 return 0;
471 case LABEL_REF:
472 return XEXP (x, 0) == XEXP (y, 0);
474 default:
475 break;
478 code = GET_CODE (x);
479 fmt = GET_RTX_FORMAT (code);
481 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
483 int j;
485 switch (fmt[i])
487 case 'w':
488 if (XWINT (x, i) != XWINT (y, i))
489 return 0;
490 break;
492 case 'n':
493 case 'i':
494 if (XINT (x, i) != XINT (y, i))
495 return 0;
496 break;
498 case 'V':
499 case 'E':
500 /* Two vectors must have the same length. */
501 if (XVECLEN (x, i) != XVECLEN (y, i))
502 return 0;
504 /* And the corresponding elements must match. */
505 for (j = 0; j < XVECLEN (x, i); j++)
506 if (! rtx_equal_for_cselib_p (XVECEXP (x, i, j),
507 XVECEXP (y, i, j)))
508 return 0;
509 break;
511 case 'e':
512 if (i == 1
513 && targetm.commutative_p (x, UNKNOWN)
514 && rtx_equal_for_cselib_p (XEXP (x, 1), XEXP (y, 0))
515 && rtx_equal_for_cselib_p (XEXP (x, 0), XEXP (y, 1)))
516 return 1;
517 if (! rtx_equal_for_cselib_p (XEXP (x, i), XEXP (y, i)))
518 return 0;
519 break;
521 case 'S':
522 case 's':
523 if (strcmp (XSTR (x, i), XSTR (y, i)))
524 return 0;
525 break;
527 case 'u':
528 /* These are just backpointers, so they don't matter. */
529 break;
531 case '0':
532 case 't':
533 break;
535 /* It is believed that rtx's at this level will never
536 contain anything but integers and other rtx's,
537 except for within LABEL_REFs and SYMBOL_REFs. */
538 default:
539 gcc_unreachable ();
542 return 1;
545 /* We need to pass down the mode of constants through the hash table
546 functions. For that purpose, wrap them in a CONST of the appropriate
547 mode. */
548 static rtx
549 wrap_constant (enum machine_mode mode, rtx x)
551 if (GET_CODE (x) != CONST_INT
552 && (GET_CODE (x) != CONST_DOUBLE || GET_MODE (x) != VOIDmode))
553 return x;
554 gcc_assert (mode != VOIDmode);
555 return gen_rtx_CONST (mode, x);
558 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
559 For registers and memory locations, we look up their cselib_val structure
560 and return its VALUE element.
561 Possible reasons for return 0 are: the object is volatile, or we couldn't
562 find a register or memory location in the table and CREATE is zero. If
563 CREATE is nonzero, table elts are created for regs and mem.
564 N.B. this hash function returns the same hash value for RTXes that
565 differ only in the order of operands, thus it is suitable for comparisons
566 that take commutativity into account.
567 If we wanted to also support associative rules, we'd have to use a different
568 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
569 We used to have a MODE argument for hashing for CONST_INTs, but that
570 didn't make sense, since it caused spurious hash differences between
571 (set (reg:SI 1) (const_int))
572 (plus:SI (reg:SI 2) (reg:SI 1))
574 (plus:SI (reg:SI 2) (const_int))
575 If the mode is important in any context, it must be checked specifically
576 in a comparison anyway, since relying on hash differences is unsafe. */
578 static unsigned int
579 cselib_hash_rtx (rtx x, int create)
581 cselib_val *e;
582 int i, j;
583 enum rtx_code code;
584 const char *fmt;
585 unsigned int hash = 0;
587 code = GET_CODE (x);
588 hash += (unsigned) code + (unsigned) GET_MODE (x);
590 switch (code)
592 case MEM:
593 case REG:
594 e = cselib_lookup (x, GET_MODE (x), create);
595 if (! e)
596 return 0;
598 return e->value;
600 case CONST_INT:
601 hash += ((unsigned) CONST_INT << 7) + INTVAL (x);
602 return hash ? hash : (unsigned int) CONST_INT;
604 case CONST_DOUBLE:
605 /* This is like the general case, except that it only counts
606 the integers representing the constant. */
607 hash += (unsigned) code + (unsigned) GET_MODE (x);
608 if (GET_MODE (x) != VOIDmode)
609 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
610 else
611 hash += ((unsigned) CONST_DOUBLE_LOW (x)
612 + (unsigned) CONST_DOUBLE_HIGH (x));
613 return hash ? hash : (unsigned int) CONST_DOUBLE;
615 case CONST_VECTOR:
617 int units;
618 rtx elt;
620 units = CONST_VECTOR_NUNITS (x);
622 for (i = 0; i < units; ++i)
624 elt = CONST_VECTOR_ELT (x, i);
625 hash += cselib_hash_rtx (elt, 0);
628 return hash;
631 /* Assume there is only one rtx object for any given label. */
632 case LABEL_REF:
633 hash
634 += ((unsigned) LABEL_REF << 7) + (unsigned long) XEXP (x, 0);
635 return hash ? hash : (unsigned int) LABEL_REF;
637 case SYMBOL_REF:
638 hash
639 += ((unsigned) SYMBOL_REF << 7) + (unsigned long) XSTR (x, 0);
640 return hash ? hash : (unsigned int) SYMBOL_REF;
642 case PRE_DEC:
643 case PRE_INC:
644 case POST_DEC:
645 case POST_INC:
646 case POST_MODIFY:
647 case PRE_MODIFY:
648 case PC:
649 case CC0:
650 case CALL:
651 case UNSPEC_VOLATILE:
652 return 0;
654 case ASM_OPERANDS:
655 if (MEM_VOLATILE_P (x))
656 return 0;
658 break;
660 default:
661 break;
664 i = GET_RTX_LENGTH (code) - 1;
665 fmt = GET_RTX_FORMAT (code);
666 for (; i >= 0; i--)
668 switch (fmt[i])
670 case 'e':
672 rtx tem = XEXP (x, i);
673 unsigned int tem_hash = cselib_hash_rtx (tem, create);
675 if (tem_hash == 0)
676 return 0;
678 hash += tem_hash;
680 break;
681 case 'E':
682 for (j = 0; j < XVECLEN (x, i); j++)
684 unsigned int tem_hash
685 = cselib_hash_rtx (XVECEXP (x, i, j), create);
687 if (tem_hash == 0)
688 return 0;
690 hash += tem_hash;
692 break;
694 case 's':
696 const unsigned char *p = (const unsigned char *) XSTR (x, i);
698 if (p)
699 while (*p)
700 hash += *p++;
701 break;
704 case 'i':
705 hash += XINT (x, i);
706 break;
708 case '0':
709 case 't':
710 /* unused */
711 break;
713 default:
714 gcc_unreachable ();
718 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
721 /* Create a new value structure for VALUE and initialize it. The mode of the
722 value is MODE. */
724 static inline cselib_val *
725 new_cselib_val (unsigned int value, enum machine_mode mode)
727 cselib_val *e = pool_alloc (cselib_val_pool);
729 gcc_assert (value);
731 e->value = value;
732 /* We use an alloc pool to allocate this RTL construct because it
733 accounts for about 8% of the overall memory usage. We know
734 precisely when we can have VALUE RTXen (when cselib is active)
735 so we don't need to put them in garbage collected memory.
736 ??? Why should a VALUE be an RTX in the first place? */
737 e->u.val_rtx = pool_alloc (value_pool);
738 memset (e->u.val_rtx, 0, RTX_HDR_SIZE);
739 PUT_CODE (e->u.val_rtx, VALUE);
740 PUT_MODE (e->u.val_rtx, mode);
741 CSELIB_VAL_PTR (e->u.val_rtx) = e;
742 e->addr_list = 0;
743 e->locs = 0;
744 e->next_containing_mem = 0;
745 return e;
748 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
749 contains the data at this address. X is a MEM that represents the
750 value. Update the two value structures to represent this situation. */
752 static void
753 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
755 struct elt_loc_list *l;
757 /* Avoid duplicates. */
758 for (l = mem_elt->locs; l; l = l->next)
759 if (MEM_P (l->loc)
760 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
761 return;
763 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
764 mem_elt->locs
765 = new_elt_loc_list (mem_elt->locs,
766 replace_equiv_address_nv (x, addr_elt->u.val_rtx));
767 if (mem_elt->next_containing_mem == NULL)
769 mem_elt->next_containing_mem = first_containing_mem;
770 first_containing_mem = mem_elt;
774 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
775 If CREATE, make a new one if we haven't seen it before. */
777 static cselib_val *
778 cselib_lookup_mem (rtx x, int create)
780 enum machine_mode mode = GET_MODE (x);
781 void **slot;
782 cselib_val *addr;
783 cselib_val *mem_elt;
784 struct elt_list *l;
786 if (MEM_VOLATILE_P (x) || mode == BLKmode
787 || !cselib_record_memory
788 || (FLOAT_MODE_P (mode) && flag_float_store))
789 return 0;
791 /* Look up the value for the address. */
792 addr = cselib_lookup (XEXP (x, 0), mode, create);
793 if (! addr)
794 return 0;
796 /* Find a value that describes a value of our mode at that address. */
797 for (l = addr->addr_list; l; l = l->next)
798 if (GET_MODE (l->elt->u.val_rtx) == mode)
799 return l->elt;
801 if (! create)
802 return 0;
804 mem_elt = new_cselib_val (++next_unknown_value, mode);
805 add_mem_for_addr (addr, mem_elt, x);
806 slot = htab_find_slot_with_hash (hash_table, wrap_constant (mode, x),
807 mem_elt->value, INSERT);
808 *slot = mem_elt;
809 return mem_elt;
812 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
813 with VALUE expressions. This way, it becomes independent of changes
814 to registers and memory.
815 X isn't actually modified; if modifications are needed, new rtl is
816 allocated. However, the return value can share rtl with X. */
819 cselib_subst_to_values (rtx x)
821 enum rtx_code code = GET_CODE (x);
822 const char *fmt = GET_RTX_FORMAT (code);
823 cselib_val *e;
824 struct elt_list *l;
825 rtx copy = x;
826 int i;
828 switch (code)
830 case REG:
831 l = REG_VALUES (REGNO (x));
832 if (l && l->elt == NULL)
833 l = l->next;
834 for (; l; l = l->next)
835 if (GET_MODE (l->elt->u.val_rtx) == GET_MODE (x))
836 return l->elt->u.val_rtx;
838 gcc_unreachable ();
840 case MEM:
841 e = cselib_lookup_mem (x, 0);
842 if (! e)
844 /* This happens for autoincrements. Assign a value that doesn't
845 match any other. */
846 e = new_cselib_val (++next_unknown_value, GET_MODE (x));
848 return e->u.val_rtx;
850 case CONST_DOUBLE:
851 case CONST_VECTOR:
852 case CONST_INT:
853 return x;
855 case POST_INC:
856 case PRE_INC:
857 case POST_DEC:
858 case PRE_DEC:
859 case POST_MODIFY:
860 case PRE_MODIFY:
861 e = new_cselib_val (++next_unknown_value, GET_MODE (x));
862 return e->u.val_rtx;
864 default:
865 break;
868 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
870 if (fmt[i] == 'e')
872 rtx t = cselib_subst_to_values (XEXP (x, i));
874 if (t != XEXP (x, i) && x == copy)
875 copy = shallow_copy_rtx (x);
877 XEXP (copy, i) = t;
879 else if (fmt[i] == 'E')
881 int j, k;
883 for (j = 0; j < XVECLEN (x, i); j++)
885 rtx t = cselib_subst_to_values (XVECEXP (x, i, j));
887 if (t != XVECEXP (x, i, j) && XVEC (x, i) == XVEC (copy, i))
889 if (x == copy)
890 copy = shallow_copy_rtx (x);
892 XVEC (copy, i) = rtvec_alloc (XVECLEN (x, i));
893 for (k = 0; k < j; k++)
894 XVECEXP (copy, i, k) = XVECEXP (x, i, k);
897 XVECEXP (copy, i, j) = t;
902 return copy;
905 /* Look up the rtl expression X in our tables and return the value it has.
906 If CREATE is zero, we return NULL if we don't know the value. Otherwise,
907 we create a new one if possible, using mode MODE if X doesn't have a mode
908 (i.e. because it's a constant). */
910 cselib_val *
911 cselib_lookup (rtx x, enum machine_mode mode, int create)
913 void **slot;
914 cselib_val *e;
915 unsigned int hashval;
917 if (GET_MODE (x) != VOIDmode)
918 mode = GET_MODE (x);
920 if (GET_CODE (x) == VALUE)
921 return CSELIB_VAL_PTR (x);
923 if (REG_P (x))
925 struct elt_list *l;
926 unsigned int i = REGNO (x);
928 l = REG_VALUES (i);
929 if (l && l->elt == NULL)
930 l = l->next;
931 for (; l; l = l->next)
932 if (mode == GET_MODE (l->elt->u.val_rtx))
933 return l->elt;
935 if (! create)
936 return 0;
938 if (i < FIRST_PSEUDO_REGISTER)
940 unsigned int n = hard_regno_nregs[i][mode];
942 if (n > max_value_regs)
943 max_value_regs = n;
946 e = new_cselib_val (++next_unknown_value, GET_MODE (x));
947 e->locs = new_elt_loc_list (e->locs, x);
948 if (REG_VALUES (i) == 0)
950 /* Maintain the invariant that the first entry of
951 REG_VALUES, if present, must be the value used to set the
952 register, or NULL. */
953 used_regs[n_used_regs++] = i;
954 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
956 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
957 slot = htab_find_slot_with_hash (hash_table, x, e->value, INSERT);
958 *slot = e;
959 return e;
962 if (MEM_P (x))
963 return cselib_lookup_mem (x, create);
965 hashval = cselib_hash_rtx (x, create);
966 /* Can't even create if hashing is not possible. */
967 if (! hashval)
968 return 0;
970 slot = htab_find_slot_with_hash (hash_table, wrap_constant (mode, x),
971 hashval, create ? INSERT : NO_INSERT);
972 if (slot == 0)
973 return 0;
975 e = (cselib_val *) *slot;
976 if (e)
977 return e;
979 e = new_cselib_val (hashval, mode);
981 /* We have to fill the slot before calling cselib_subst_to_values:
982 the hash table is inconsistent until we do so, and
983 cselib_subst_to_values will need to do lookups. */
984 *slot = (void *) e;
985 e->locs = new_elt_loc_list (e->locs, cselib_subst_to_values (x));
986 return e;
989 /* Invalidate any entries in reg_values that overlap REGNO. This is called
990 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
991 is used to determine how many hard registers are being changed. If MODE
992 is VOIDmode, then only REGNO is being changed; this is used when
993 invalidating call clobbered registers across a call. */
995 static void
996 cselib_invalidate_regno (unsigned int regno, enum machine_mode mode)
998 unsigned int endregno;
999 unsigned int i;
1001 /* If we see pseudos after reload, something is _wrong_. */
1002 gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
1003 || reg_renumber[regno] < 0);
1005 /* Determine the range of registers that must be invalidated. For
1006 pseudos, only REGNO is affected. For hard regs, we must take MODE
1007 into account, and we must also invalidate lower register numbers
1008 if they contain values that overlap REGNO. */
1009 if (regno < FIRST_PSEUDO_REGISTER)
1011 gcc_assert (mode != VOIDmode);
1013 if (regno < max_value_regs)
1014 i = 0;
1015 else
1016 i = regno - max_value_regs;
1018 endregno = regno + hard_regno_nregs[regno][mode];
1020 else
1022 i = regno;
1023 endregno = regno + 1;
1026 for (; i < endregno; i++)
1028 struct elt_list **l = &REG_VALUES (i);
1030 /* Go through all known values for this reg; if it overlaps the range
1031 we're invalidating, remove the value. */
1032 while (*l)
1034 cselib_val *v = (*l)->elt;
1035 struct elt_loc_list **p;
1036 unsigned int this_last = i;
1038 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
1039 this_last += hard_regno_nregs[i][GET_MODE (v->u.val_rtx)] - 1;
1041 if (this_last < regno || v == NULL)
1043 l = &(*l)->next;
1044 continue;
1047 /* We have an overlap. */
1048 if (*l == REG_VALUES (i))
1050 /* Maintain the invariant that the first entry of
1051 REG_VALUES, if present, must be the value used to set
1052 the register, or NULL. This is also nice because
1053 then we won't push the same regno onto user_regs
1054 multiple times. */
1055 (*l)->elt = NULL;
1056 l = &(*l)->next;
1058 else
1059 unchain_one_elt_list (l);
1061 /* Now, we clear the mapping from value to reg. It must exist, so
1062 this code will crash intentionally if it doesn't. */
1063 for (p = &v->locs; ; p = &(*p)->next)
1065 rtx x = (*p)->loc;
1067 if (REG_P (x) && REGNO (x) == i)
1069 unchain_one_elt_loc_list (p);
1070 break;
1073 if (v->locs == 0)
1074 n_useless_values++;
1079 /* Return 1 if X has a value that can vary even between two
1080 executions of the program. 0 means X can be compared reliably
1081 against certain constants or near-constants. */
1083 static int
1084 cselib_rtx_varies_p (rtx x ATTRIBUTE_UNUSED, int from_alias ATTRIBUTE_UNUSED)
1086 /* We actually don't need to verify very hard. This is because
1087 if X has actually changed, we invalidate the memory anyway,
1088 so assume that all common memory addresses are
1089 invariant. */
1090 return 0;
1093 /* Invalidate any locations in the table which are changed because of a
1094 store to MEM_RTX. If this is called because of a non-const call
1095 instruction, MEM_RTX is (mem:BLK const0_rtx). */
1097 static void
1098 cselib_invalidate_mem (rtx mem_rtx)
1100 cselib_val **vp, *v, *next;
1101 int num_mems = 0;
1102 rtx mem_addr;
1104 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
1105 mem_rtx = canon_rtx (mem_rtx);
1107 vp = &first_containing_mem;
1108 for (v = *vp; v != &dummy_val; v = next)
1110 bool has_mem = false;
1111 struct elt_loc_list **p = &v->locs;
1112 int had_locs = v->locs != 0;
1114 while (*p)
1116 rtx x = (*p)->loc;
1117 cselib_val *addr;
1118 struct elt_list **mem_chain;
1120 /* MEMs may occur in locations only at the top level; below
1121 that every MEM or REG is substituted by its VALUE. */
1122 if (!MEM_P (x))
1124 p = &(*p)->next;
1125 continue;
1127 if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
1128 && ! canon_true_dependence (mem_rtx, GET_MODE (mem_rtx), mem_addr,
1129 x, cselib_rtx_varies_p))
1131 has_mem = true;
1132 num_mems++;
1133 p = &(*p)->next;
1134 continue;
1137 /* This one overlaps. */
1138 /* We must have a mapping from this MEM's address to the
1139 value (E). Remove that, too. */
1140 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0);
1141 mem_chain = &addr->addr_list;
1142 for (;;)
1144 if ((*mem_chain)->elt == v)
1146 unchain_one_elt_list (mem_chain);
1147 break;
1150 mem_chain = &(*mem_chain)->next;
1153 unchain_one_elt_loc_list (p);
1156 if (had_locs && v->locs == 0)
1157 n_useless_values++;
1159 next = v->next_containing_mem;
1160 if (has_mem)
1162 *vp = v;
1163 vp = &(*vp)->next_containing_mem;
1165 else
1166 v->next_containing_mem = NULL;
1168 *vp = &dummy_val;
1171 /* Invalidate DEST, which is being assigned to or clobbered. */
1173 void
1174 cselib_invalidate_rtx (rtx dest)
1176 while (GET_CODE (dest) == SUBREG
1177 || GET_CODE (dest) == ZERO_EXTRACT
1178 || GET_CODE (dest) == STRICT_LOW_PART)
1179 dest = XEXP (dest, 0);
1181 if (REG_P (dest))
1182 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
1183 else if (MEM_P (dest))
1184 cselib_invalidate_mem (dest);
1186 /* Some machines don't define AUTO_INC_DEC, but they still use push
1187 instructions. We need to catch that case here in order to
1188 invalidate the stack pointer correctly. Note that invalidating
1189 the stack pointer is different from invalidating DEST. */
1190 if (push_operand (dest, GET_MODE (dest)))
1191 cselib_invalidate_rtx (stack_pointer_rtx);
1194 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
1196 static void
1197 cselib_invalidate_rtx_note_stores (rtx dest, rtx ignore ATTRIBUTE_UNUSED,
1198 void *data ATTRIBUTE_UNUSED)
1200 cselib_invalidate_rtx (dest);
1203 /* Record the result of a SET instruction. DEST is being set; the source
1204 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
1205 describes its address. */
1207 static void
1208 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
1210 int dreg = REG_P (dest) ? (int) REGNO (dest) : -1;
1212 if (src_elt == 0 || side_effects_p (dest))
1213 return;
1215 if (dreg >= 0)
1217 if (dreg < FIRST_PSEUDO_REGISTER)
1219 unsigned int n = hard_regno_nregs[dreg][GET_MODE (dest)];
1221 if (n > max_value_regs)
1222 max_value_regs = n;
1225 if (REG_VALUES (dreg) == 0)
1227 used_regs[n_used_regs++] = dreg;
1228 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
1230 else
1232 /* The register should have been invalidated. */
1233 gcc_assert (REG_VALUES (dreg)->elt == 0);
1234 REG_VALUES (dreg)->elt = src_elt;
1237 if (src_elt->locs == 0)
1238 n_useless_values--;
1239 src_elt->locs = new_elt_loc_list (src_elt->locs, dest);
1241 else if (MEM_P (dest) && dest_addr_elt != 0
1242 && cselib_record_memory)
1244 if (src_elt->locs == 0)
1245 n_useless_values--;
1246 add_mem_for_addr (dest_addr_elt, src_elt, dest);
1250 /* Describe a single set that is part of an insn. */
1251 struct set
1253 rtx src;
1254 rtx dest;
1255 cselib_val *src_elt;
1256 cselib_val *dest_addr_elt;
1259 /* There is no good way to determine how many elements there can be
1260 in a PARALLEL. Since it's fairly cheap, use a really large number. */
1261 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
1263 /* Record the effects of any sets in INSN. */
1264 static void
1265 cselib_record_sets (rtx insn)
1267 int n_sets = 0;
1268 int i;
1269 struct set sets[MAX_SETS];
1270 rtx body = PATTERN (insn);
1271 rtx cond = 0;
1273 body = PATTERN (insn);
1274 if (GET_CODE (body) == COND_EXEC)
1276 cond = COND_EXEC_TEST (body);
1277 body = COND_EXEC_CODE (body);
1280 /* Find all sets. */
1281 if (GET_CODE (body) == SET)
1283 sets[0].src = SET_SRC (body);
1284 sets[0].dest = SET_DEST (body);
1285 n_sets = 1;
1287 else if (GET_CODE (body) == PARALLEL)
1289 /* Look through the PARALLEL and record the values being
1290 set, if possible. Also handle any CLOBBERs. */
1291 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
1293 rtx x = XVECEXP (body, 0, i);
1295 if (GET_CODE (x) == SET)
1297 sets[n_sets].src = SET_SRC (x);
1298 sets[n_sets].dest = SET_DEST (x);
1299 n_sets++;
1304 /* Look up the values that are read. Do this before invalidating the
1305 locations that are written. */
1306 for (i = 0; i < n_sets; i++)
1308 rtx dest = sets[i].dest;
1310 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
1311 the low part after invalidating any knowledge about larger modes. */
1312 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
1313 sets[i].dest = dest = XEXP (dest, 0);
1315 /* We don't know how to record anything but REG or MEM. */
1316 if (REG_P (dest)
1317 || (MEM_P (dest) && cselib_record_memory))
1319 rtx src = sets[i].src;
1320 if (cond)
1321 src = gen_rtx_IF_THEN_ELSE (GET_MODE (src), cond, src, dest);
1322 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1);
1323 if (MEM_P (dest))
1324 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0), Pmode, 1);
1325 else
1326 sets[i].dest_addr_elt = 0;
1330 /* Invalidate all locations written by this insn. Note that the elts we
1331 looked up in the previous loop aren't affected, just some of their
1332 locations may go away. */
1333 note_stores (body, cselib_invalidate_rtx_note_stores, NULL);
1335 /* If this is an asm, look for duplicate sets. This can happen when the
1336 user uses the same value as an output multiple times. This is valid
1337 if the outputs are not actually used thereafter. Treat this case as
1338 if the value isn't actually set. We do this by smashing the destination
1339 to pc_rtx, so that we won't record the value later. */
1340 if (n_sets >= 2 && asm_noperands (body) >= 0)
1342 for (i = 0; i < n_sets; i++)
1344 rtx dest = sets[i].dest;
1345 if (REG_P (dest) || MEM_P (dest))
1347 int j;
1348 for (j = i + 1; j < n_sets; j++)
1349 if (rtx_equal_p (dest, sets[j].dest))
1351 sets[i].dest = pc_rtx;
1352 sets[j].dest = pc_rtx;
1358 /* Now enter the equivalences in our tables. */
1359 for (i = 0; i < n_sets; i++)
1361 rtx dest = sets[i].dest;
1362 if (REG_P (dest)
1363 || (MEM_P (dest) && cselib_record_memory))
1364 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
1368 /* Record the effects of INSN. */
1370 void
1371 cselib_process_insn (rtx insn)
1373 int i;
1374 rtx x;
1376 if (find_reg_note (insn, REG_LIBCALL, NULL))
1377 cselib_current_insn_in_libcall = true;
1378 cselib_current_insn = insn;
1380 /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp. */
1381 if (LABEL_P (insn)
1382 || (CALL_P (insn)
1383 && find_reg_note (insn, REG_SETJMP, NULL))
1384 || (NONJUMP_INSN_P (insn)
1385 && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
1386 && MEM_VOLATILE_P (PATTERN (insn))))
1388 if (find_reg_note (insn, REG_RETVAL, NULL))
1389 cselib_current_insn_in_libcall = false;
1390 cselib_clear_table ();
1391 return;
1394 if (! INSN_P (insn))
1396 if (find_reg_note (insn, REG_RETVAL, NULL))
1397 cselib_current_insn_in_libcall = false;
1398 cselib_current_insn = 0;
1399 return;
1402 /* If this is a call instruction, forget anything stored in a
1403 call clobbered register, or, if this is not a const call, in
1404 memory. */
1405 if (CALL_P (insn))
1407 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1408 if (call_used_regs[i]
1409 || (REG_VALUES (i) && REG_VALUES (i)->elt
1410 && HARD_REGNO_CALL_PART_CLOBBERED (i,
1411 GET_MODE (REG_VALUES (i)->elt->u.val_rtx))))
1412 cselib_invalidate_regno (i, reg_raw_mode[i]);
1414 if (! CONST_OR_PURE_CALL_P (insn))
1415 cselib_invalidate_mem (callmem);
1418 cselib_record_sets (insn);
1420 #ifdef AUTO_INC_DEC
1421 /* Clobber any registers which appear in REG_INC notes. We
1422 could keep track of the changes to their values, but it is
1423 unlikely to help. */
1424 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
1425 if (REG_NOTE_KIND (x) == REG_INC)
1426 cselib_invalidate_rtx (XEXP (x, 0));
1427 #endif
1429 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
1430 after we have processed the insn. */
1431 if (CALL_P (insn))
1432 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
1433 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
1434 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
1436 if (find_reg_note (insn, REG_RETVAL, NULL))
1437 cselib_current_insn_in_libcall = false;
1438 cselib_current_insn = 0;
1440 if (n_useless_values > MAX_USELESS_VALUES)
1441 remove_useless_values ();
1444 /* Initialize cselib for one pass. The caller must also call
1445 init_alias_analysis. */
1447 void
1448 cselib_init (bool record_memory)
1450 elt_list_pool = create_alloc_pool ("elt_list",
1451 sizeof (struct elt_list), 10);
1452 elt_loc_list_pool = create_alloc_pool ("elt_loc_list",
1453 sizeof (struct elt_loc_list), 10);
1454 cselib_val_pool = create_alloc_pool ("cselib_val_list",
1455 sizeof (cselib_val), 10);
1456 value_pool = create_alloc_pool ("value",
1457 RTX_SIZE (VALUE), 100);
1458 cselib_record_memory = record_memory;
1459 /* This is only created once. */
1460 if (! callmem)
1461 callmem = gen_rtx_MEM (BLKmode, const0_rtx);
1463 cselib_nregs = max_reg_num ();
1465 /* We preserve reg_values to allow expensive clearing of the whole thing.
1466 Reallocate it however if it happens to be too large. */
1467 if (!reg_values || reg_values_size < cselib_nregs
1468 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
1470 if (reg_values)
1471 free (reg_values);
1472 /* Some space for newly emit instructions so we don't end up
1473 reallocating in between passes. */
1474 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
1475 reg_values = xcalloc (reg_values_size, sizeof (reg_values));
1477 used_regs = xmalloc (sizeof (*used_regs) * cselib_nregs);
1478 n_used_regs = 0;
1479 hash_table = htab_create (31, get_value_hash, entry_and_rtx_equal_p, NULL);
1480 cselib_current_insn_in_libcall = false;
1483 /* Called when the current user is done with cselib. */
1485 void
1486 cselib_finish (void)
1488 free_alloc_pool (elt_list_pool);
1489 free_alloc_pool (elt_loc_list_pool);
1490 free_alloc_pool (cselib_val_pool);
1491 free_alloc_pool (value_pool);
1492 cselib_clear_table ();
1493 htab_delete (hash_table);
1494 free (used_regs);
1495 used_regs = 0;
1496 hash_table = 0;
1497 n_useless_values = 0;
1498 next_unknown_value = 0;
1501 #include "gt-cselib.h"