* config/arm/elf.h (ASM_OUTPUT_ALIGNED_COMMON): Remove definition.
[official-gcc.git] / gcc / cselib.c
blobcf3394f237a290249133de5b1d2db5a92dc4d85b
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 PARAMS ((const void *, const void *));
44 static hashval_t get_value_hash PARAMS ((const void *));
45 static struct elt_list *new_elt_list PARAMS ((struct elt_list *,
46 cselib_val *));
47 static struct elt_loc_list *new_elt_loc_list PARAMS ((struct elt_loc_list *,
48 rtx));
49 static void unchain_one_value PARAMS ((cselib_val *));
50 static void unchain_one_elt_list PARAMS ((struct elt_list **));
51 static void unchain_one_elt_loc_list PARAMS ((struct elt_loc_list **));
52 static void clear_table PARAMS ((void));
53 static int discard_useless_locs PARAMS ((void **, void *));
54 static int discard_useless_values PARAMS ((void **, void *));
55 static void remove_useless_values PARAMS ((void));
56 static rtx wrap_constant PARAMS ((enum machine_mode, rtx));
57 static unsigned int hash_rtx PARAMS ((rtx, enum machine_mode, int));
58 static cselib_val *new_cselib_val PARAMS ((unsigned int,
59 enum machine_mode));
60 static void add_mem_for_addr PARAMS ((cselib_val *, cselib_val *,
61 rtx));
62 static cselib_val *cselib_lookup_mem PARAMS ((rtx, int));
63 static void cselib_invalidate_regno PARAMS ((unsigned int,
64 enum machine_mode));
65 static int cselib_mem_conflict_p PARAMS ((rtx, rtx));
66 static void cselib_invalidate_mem PARAMS ((rtx));
67 static void cselib_invalidate_rtx PARAMS ((rtx, rtx, void *));
68 static void cselib_record_set PARAMS ((rtx, cselib_val *,
69 cselib_val *));
70 static void cselib_record_sets PARAMS ((rtx));
72 /* There are three ways in which cselib can look up an rtx:
73 - for a REG, the reg_values table (which is indexed by regno) is used
74 - for a MEM, we recursively look up its address and then follow the
75 addr_list of that value
76 - for everything else, we compute a hash value and go through the hash
77 table. Since different rtx's can still have the same hash value,
78 this involves walking the table entries for a given value and comparing
79 the locations of the entries with the rtx we are looking up. */
81 /* A table that enables us to look up elts by their value. */
82 static GTY((param_is (cselib_val))) htab_t hash_table;
84 /* This is a global so we don't have to pass this through every function.
85 It is used in new_elt_loc_list to set SETTING_INSN. */
86 static rtx cselib_current_insn;
87 static bool cselib_current_insn_in_libcall;
89 /* Every new unknown value gets a unique number. */
90 static unsigned int next_unknown_value;
92 /* The number of registers we had when the varrays were last resized. */
93 static unsigned int cselib_nregs;
95 /* Count values without known locations. Whenever this grows too big, we
96 remove these useless values from the table. */
97 static int n_useless_values;
99 /* Number of useless values before we remove them from the hash table. */
100 #define MAX_USELESS_VALUES 32
102 /* This table maps from register number to values. It does not
103 contain pointers to cselib_val structures, but rather elt_lists.
104 The purpose is to be able to refer to the same register in
105 different modes. The first element of the list defines the mode in
106 which the register was set; if the mode is unknown or the value is
107 no longer valid in that mode, ELT will be NULL for the first
108 element. */
109 static GTY(()) varray_type reg_values;
110 static GTY((deletable (""))) varray_type reg_values_old;
111 #define REG_VALUES(I) VARRAY_ELT_LIST (reg_values, (I))
113 /* The largest number of hard regs used by any entry added to the
114 REG_VALUES table. Cleared on each clear_table() invocation. */
115 static unsigned int max_value_regs;
117 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
118 in clear_table() for fast emptying. */
119 static GTY(()) varray_type used_regs;
120 static GTY((deletable (""))) varray_type used_regs_old;
122 /* We pass this to cselib_invalidate_mem to invalidate all of
123 memory for a non-const call instruction. */
124 static GTY(()) rtx callmem;
126 /* Caches for unused structures. */
127 static GTY((deletable (""))) cselib_val *empty_vals;
128 static GTY((deletable (""))) struct elt_list *empty_elt_lists;
129 static GTY((deletable (""))) struct elt_loc_list *empty_elt_loc_lists;
131 /* Set by discard_useless_locs if it deleted the last location of any
132 value. */
133 static int values_became_useless;
135 /* Used as stop element of the containing_mem list so we can check
136 presence in the list by checking the next pointer. */
137 static cselib_val dummy_val;
139 /* Used to list all values that contain memory reference.
140 May or may not contain the useless values - the list is compacted
141 each time memory is invalidated. */
142 static cselib_val *first_containing_mem = &dummy_val;
145 /* Allocate a struct elt_list and fill in its two elements with the
146 arguments. */
148 static struct elt_list *
149 new_elt_list (next, elt)
150 struct elt_list *next;
151 cselib_val *elt;
153 struct elt_list *el = empty_elt_lists;
155 if (el)
156 empty_elt_lists = el->next;
157 else
158 el = (struct elt_list *) ggc_alloc (sizeof (struct elt_list));
159 el->next = next;
160 el->elt = elt;
161 return el;
164 /* Allocate a struct elt_loc_list and fill in its two elements with the
165 arguments. */
167 static struct elt_loc_list *
168 new_elt_loc_list (next, loc)
169 struct elt_loc_list *next;
170 rtx loc;
172 struct elt_loc_list *el = empty_elt_loc_lists;
174 if (el)
175 empty_elt_loc_lists = el->next;
176 else
177 el = (struct elt_loc_list *) ggc_alloc (sizeof (struct elt_loc_list));
178 el->next = next;
179 el->loc = loc;
180 el->setting_insn = cselib_current_insn;
181 el->in_libcall = cselib_current_insn_in_libcall;
182 return el;
185 /* The elt_list at *PL is no longer needed. Unchain it and free its
186 storage. */
188 static void
189 unchain_one_elt_list (pl)
190 struct elt_list **pl;
192 struct elt_list *l = *pl;
194 *pl = l->next;
195 l->next = empty_elt_lists;
196 empty_elt_lists = l;
199 /* Likewise for elt_loc_lists. */
201 static void
202 unchain_one_elt_loc_list (pl)
203 struct elt_loc_list **pl;
205 struct elt_loc_list *l = *pl;
207 *pl = l->next;
208 l->next = empty_elt_loc_lists;
209 empty_elt_loc_lists = l;
212 /* Likewise for cselib_vals. This also frees the addr_list associated with
213 V. */
215 static void
216 unchain_one_value (v)
217 cselib_val *v;
219 while (v->addr_list)
220 unchain_one_elt_list (&v->addr_list);
222 v->u.next_free = empty_vals;
223 empty_vals = v;
226 /* Remove all entries from the hash table. Also used during
227 initialization. If CLEAR_ALL isn't set, then only clear the entries
228 which are known to have been used. */
230 static void
231 clear_table ()
233 unsigned int i;
235 for (i = 0; i < VARRAY_ACTIVE_SIZE (used_regs); i++)
236 REG_VALUES (VARRAY_UINT (used_regs, i)) = 0;
238 max_value_regs = 0;
240 VARRAY_POP_ALL (used_regs);
242 htab_empty (hash_table);
244 n_useless_values = 0;
246 next_unknown_value = 0;
248 first_containing_mem = &dummy_val;
251 /* The equality test for our hash table. The first argument ENTRY is a table
252 element (i.e. a cselib_val), while the second arg X is an rtx. We know
253 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
254 CONST of an appropriate mode. */
256 static int
257 entry_and_rtx_equal_p (entry, x_arg)
258 const void *entry, *x_arg;
260 struct elt_loc_list *l;
261 const cselib_val *v = (const cselib_val *) entry;
262 rtx x = (rtx) x_arg;
263 enum machine_mode mode = GET_MODE (x);
265 if (GET_CODE (x) == CONST_INT
266 || (mode == VOIDmode && GET_CODE (x) == CONST_DOUBLE))
267 abort ();
268 if (mode != GET_MODE (v->u.val_rtx))
269 return 0;
271 /* Unwrap X if necessary. */
272 if (GET_CODE (x) == CONST
273 && (GET_CODE (XEXP (x, 0)) == CONST_INT
274 || GET_CODE (XEXP (x, 0)) == CONST_DOUBLE))
275 x = XEXP (x, 0);
277 /* We don't guarantee that distinct rtx's have different hash values,
278 so we need to do a comparison. */
279 for (l = v->locs; l; l = l->next)
280 if (rtx_equal_for_cselib_p (l->loc, x))
281 return 1;
283 return 0;
286 /* The hash function for our hash table. The value is always computed with
287 hash_rtx when adding an element; this function just extracts the hash
288 value from a cselib_val structure. */
290 static hashval_t
291 get_value_hash (entry)
292 const void *entry;
294 const cselib_val *v = (const cselib_val *) entry;
295 return v->value;
298 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
299 only return true for values which point to a cselib_val whose value
300 element has been set to zero, which implies the cselib_val will be
301 removed. */
304 references_value_p (x, only_useless)
305 rtx x;
306 int only_useless;
308 enum rtx_code code = GET_CODE (x);
309 const char *fmt = GET_RTX_FORMAT (code);
310 int i, j;
312 if (GET_CODE (x) == VALUE
313 && (! only_useless || CSELIB_VAL_PTR (x)->locs == 0))
314 return 1;
316 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
318 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
319 return 1;
320 else if (fmt[i] == 'E')
321 for (j = 0; j < XVECLEN (x, i); j++)
322 if (references_value_p (XVECEXP (x, i, j), only_useless))
323 return 1;
326 return 0;
329 /* For all locations found in X, delete locations that reference useless
330 values (i.e. values without any location). Called through
331 htab_traverse. */
333 static int
334 discard_useless_locs (x, info)
335 void **x;
336 void *info ATTRIBUTE_UNUSED;
338 cselib_val *v = (cselib_val *)*x;
339 struct elt_loc_list **p = &v->locs;
340 int had_locs = v->locs != 0;
342 while (*p)
344 if (references_value_p ((*p)->loc, 1))
345 unchain_one_elt_loc_list (p);
346 else
347 p = &(*p)->next;
350 if (had_locs && v->locs == 0)
352 n_useless_values++;
353 values_became_useless = 1;
355 return 1;
358 /* If X is a value with no locations, remove it from the hashtable. */
360 static int
361 discard_useless_values (x, info)
362 void **x;
363 void *info ATTRIBUTE_UNUSED;
365 cselib_val *v = (cselib_val *)*x;
367 if (v->locs == 0)
369 htab_clear_slot (hash_table, x);
370 unchain_one_value (v);
371 n_useless_values--;
374 return 1;
377 /* Clean out useless values (i.e. those which no longer have locations
378 associated with them) from the hash table. */
380 static void
381 remove_useless_values ()
383 cselib_val **p, *v;
384 /* First pass: eliminate locations that reference the value. That in
385 turn can make more values useless. */
388 values_became_useless = 0;
389 htab_traverse (hash_table, discard_useless_locs, 0);
391 while (values_became_useless);
393 /* Second pass: actually remove the values. */
394 htab_traverse (hash_table, discard_useless_values, 0);
396 p = &first_containing_mem;
397 for (v = *p; v != &dummy_val; v = v->next_containing_mem)
398 if (v->locs)
400 *p = v;
401 p = &(*p)->next_containing_mem;
403 *p = &dummy_val;
405 if (n_useless_values != 0)
406 abort ();
409 /* Return the mode in which a register was last set. If X is not a
410 register, return its mode. If the mode in which the register was
411 set is not known, or the value was already clobbered, return
412 VOIDmode. */
414 enum machine_mode
415 cselib_reg_set_mode (x)
416 rtx x;
418 if (GET_CODE (x) != REG)
419 return GET_MODE (x);
421 if (REG_VALUES (REGNO (x)) == NULL
422 || REG_VALUES (REGNO (x))->elt == NULL)
423 return VOIDmode;
425 return GET_MODE (REG_VALUES (REGNO (x))->elt->u.val_rtx);
428 /* Return nonzero if we can prove that X and Y contain the same value, taking
429 our gathered information into account. */
432 rtx_equal_for_cselib_p (x, y)
433 rtx x, y;
435 enum rtx_code code;
436 const char *fmt;
437 int i;
439 if (GET_CODE (x) == REG || GET_CODE (x) == MEM)
441 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0);
443 if (e)
444 x = e->u.val_rtx;
447 if (GET_CODE (y) == REG || GET_CODE (y) == MEM)
449 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0);
451 if (e)
452 y = e->u.val_rtx;
455 if (x == y)
456 return 1;
458 if (GET_CODE (x) == VALUE && GET_CODE (y) == VALUE)
459 return CSELIB_VAL_PTR (x) == CSELIB_VAL_PTR (y);
461 if (GET_CODE (x) == VALUE)
463 cselib_val *e = CSELIB_VAL_PTR (x);
464 struct elt_loc_list *l;
466 for (l = e->locs; l; l = l->next)
468 rtx t = l->loc;
470 /* Avoid infinite recursion. */
471 if (GET_CODE (t) == REG || GET_CODE (t) == MEM)
472 continue;
473 else if (rtx_equal_for_cselib_p (t, y))
474 return 1;
477 return 0;
480 if (GET_CODE (y) == VALUE)
482 cselib_val *e = CSELIB_VAL_PTR (y);
483 struct elt_loc_list *l;
485 for (l = e->locs; l; l = l->next)
487 rtx t = l->loc;
489 if (GET_CODE (t) == REG || GET_CODE (t) == MEM)
490 continue;
491 else if (rtx_equal_for_cselib_p (x, t))
492 return 1;
495 return 0;
498 if (GET_CODE (x) != GET_CODE (y) || GET_MODE (x) != GET_MODE (y))
499 return 0;
501 /* This won't be handled correctly by the code below. */
502 if (GET_CODE (x) == LABEL_REF)
503 return XEXP (x, 0) == XEXP (y, 0);
505 code = GET_CODE (x);
506 fmt = GET_RTX_FORMAT (code);
508 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
510 int j;
512 switch (fmt[i])
514 case 'w':
515 if (XWINT (x, i) != XWINT (y, i))
516 return 0;
517 break;
519 case 'n':
520 case 'i':
521 if (XINT (x, i) != XINT (y, i))
522 return 0;
523 break;
525 case 'V':
526 case 'E':
527 /* Two vectors must have the same length. */
528 if (XVECLEN (x, i) != XVECLEN (y, i))
529 return 0;
531 /* And the corresponding elements must match. */
532 for (j = 0; j < XVECLEN (x, i); j++)
533 if (! rtx_equal_for_cselib_p (XVECEXP (x, i, j),
534 XVECEXP (y, i, j)))
535 return 0;
536 break;
538 case 'e':
539 if (! rtx_equal_for_cselib_p (XEXP (x, i), XEXP (y, i)))
540 return 0;
541 break;
543 case 'S':
544 case 's':
545 if (strcmp (XSTR (x, i), XSTR (y, i)))
546 return 0;
547 break;
549 case 'u':
550 /* These are just backpointers, so they don't matter. */
551 break;
553 case '0':
554 case 't':
555 break;
557 /* It is believed that rtx's at this level will never
558 contain anything but integers and other rtx's,
559 except for within LABEL_REFs and SYMBOL_REFs. */
560 default:
561 abort ();
564 return 1;
567 /* We need to pass down the mode of constants through the hash table
568 functions. For that purpose, wrap them in a CONST of the appropriate
569 mode. */
570 static rtx
571 wrap_constant (mode, x)
572 enum machine_mode mode;
573 rtx x;
575 if (GET_CODE (x) != CONST_INT
576 && (GET_CODE (x) != CONST_DOUBLE || GET_MODE (x) != VOIDmode))
577 return x;
578 if (mode == VOIDmode)
579 abort ();
580 return gen_rtx_CONST (mode, x);
583 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
584 For registers and memory locations, we look up their cselib_val structure
585 and return its VALUE element.
586 Possible reasons for return 0 are: the object is volatile, or we couldn't
587 find a register or memory location in the table and CREATE is zero. If
588 CREATE is nonzero, table elts are created for regs and mem.
589 MODE is used in hashing for CONST_INTs only;
590 otherwise the mode of X is used. */
592 static unsigned int
593 hash_rtx (x, mode, create)
594 rtx x;
595 enum machine_mode mode;
596 int create;
598 cselib_val *e;
599 int i, j;
600 enum rtx_code code;
601 const char *fmt;
602 unsigned int hash = 0;
604 code = GET_CODE (x);
605 hash += (unsigned) code + (unsigned) GET_MODE (x);
607 switch (code)
609 case MEM:
610 case REG:
611 e = cselib_lookup (x, GET_MODE (x), create);
612 if (! e)
613 return 0;
615 return e->value;
617 case CONST_INT:
618 hash += ((unsigned) CONST_INT << 7) + (unsigned) mode + INTVAL (x);
619 return hash ? hash : (unsigned int) CONST_INT;
621 case CONST_DOUBLE:
622 /* This is like the general case, except that it only counts
623 the integers representing the constant. */
624 hash += (unsigned) code + (unsigned) GET_MODE (x);
625 if (GET_MODE (x) != VOIDmode)
626 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
627 else
628 hash += ((unsigned) CONST_DOUBLE_LOW (x)
629 + (unsigned) CONST_DOUBLE_HIGH (x));
630 return hash ? hash : (unsigned int) CONST_DOUBLE;
632 case CONST_VECTOR:
634 int units;
635 rtx elt;
637 units = CONST_VECTOR_NUNITS (x);
639 for (i = 0; i < units; ++i)
641 elt = CONST_VECTOR_ELT (x, i);
642 hash += hash_rtx (elt, GET_MODE (elt), 0);
645 return hash;
648 /* Assume there is only one rtx object for any given label. */
649 case LABEL_REF:
650 hash
651 += ((unsigned) LABEL_REF << 7) + (unsigned long) XEXP (x, 0);
652 return hash ? hash : (unsigned int) LABEL_REF;
654 case SYMBOL_REF:
655 hash
656 += ((unsigned) SYMBOL_REF << 7) + (unsigned long) XSTR (x, 0);
657 return hash ? hash : (unsigned int) SYMBOL_REF;
659 case PRE_DEC:
660 case PRE_INC:
661 case POST_DEC:
662 case POST_INC:
663 case POST_MODIFY:
664 case PRE_MODIFY:
665 case PC:
666 case CC0:
667 case CALL:
668 case UNSPEC_VOLATILE:
669 return 0;
671 case ASM_OPERANDS:
672 if (MEM_VOLATILE_P (x))
673 return 0;
675 break;
677 default:
678 break;
681 i = GET_RTX_LENGTH (code) - 1;
682 fmt = GET_RTX_FORMAT (code);
683 for (; i >= 0; i--)
685 if (fmt[i] == 'e')
687 rtx tem = XEXP (x, i);
688 unsigned int tem_hash = hash_rtx (tem, 0, create);
690 if (tem_hash == 0)
691 return 0;
693 hash += tem_hash;
695 else if (fmt[i] == 'E')
696 for (j = 0; j < XVECLEN (x, i); j++)
698 unsigned int tem_hash = hash_rtx (XVECEXP (x, i, j), 0, create);
700 if (tem_hash == 0)
701 return 0;
703 hash += tem_hash;
705 else if (fmt[i] == 's')
707 const unsigned char *p = (const unsigned char *) XSTR (x, i);
709 if (p)
710 while (*p)
711 hash += *p++;
713 else if (fmt[i] == 'i')
714 hash += XINT (x, i);
715 else if (fmt[i] == '0' || fmt[i] == 't')
716 /* unused */;
717 else
718 abort ();
721 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
724 /* Create a new value structure for VALUE and initialize it. The mode of the
725 value is MODE. */
727 static cselib_val *
728 new_cselib_val (value, mode)
729 unsigned int value;
730 enum machine_mode mode;
732 cselib_val *e = empty_vals;
734 if (e)
735 empty_vals = e->u.next_free;
736 else
737 e = (cselib_val *) ggc_alloc (sizeof (cselib_val));
739 if (value == 0)
740 abort ();
742 e->value = value;
743 e->u.val_rtx = gen_rtx_VALUE (mode);
744 CSELIB_VAL_PTR (e->u.val_rtx) = e;
745 e->addr_list = 0;
746 e->locs = 0;
747 e->next_containing_mem = 0;
748 return e;
751 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
752 contains the data at this address. X is a MEM that represents the
753 value. Update the two value structures to represent this situation. */
755 static void
756 add_mem_for_addr (addr_elt, mem_elt, x)
757 cselib_val *addr_elt, *mem_elt;
758 rtx x;
760 struct elt_loc_list *l;
762 /* Avoid duplicates. */
763 for (l = mem_elt->locs; l; l = l->next)
764 if (GET_CODE (l->loc) == MEM
765 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
766 return;
768 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
769 mem_elt->locs
770 = new_elt_loc_list (mem_elt->locs,
771 replace_equiv_address_nv (x, addr_elt->u.val_rtx));
772 if (mem_elt->next_containing_mem == NULL)
774 mem_elt->next_containing_mem = first_containing_mem;
775 first_containing_mem = mem_elt;
779 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
780 If CREATE, make a new one if we haven't seen it before. */
782 static cselib_val *
783 cselib_lookup_mem (x, create)
784 rtx x;
785 int create;
787 enum machine_mode mode = GET_MODE (x);
788 void **slot;
789 cselib_val *addr;
790 cselib_val *mem_elt;
791 struct elt_list *l;
793 if (MEM_VOLATILE_P (x) || mode == BLKmode
794 || (FLOAT_MODE_P (mode) && flag_float_store))
795 return 0;
797 /* Look up the value for the address. */
798 addr = cselib_lookup (XEXP (x, 0), mode, create);
799 if (! addr)
800 return 0;
802 /* Find a value that describes a value of our mode at that address. */
803 for (l = addr->addr_list; l; l = l->next)
804 if (GET_MODE (l->elt->u.val_rtx) == mode)
805 return l->elt;
807 if (! create)
808 return 0;
810 mem_elt = new_cselib_val (++next_unknown_value, mode);
811 add_mem_for_addr (addr, mem_elt, x);
812 slot = htab_find_slot_with_hash (hash_table, wrap_constant (mode, x),
813 mem_elt->value, INSERT);
814 *slot = mem_elt;
815 return mem_elt;
818 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
819 with VALUE expressions. This way, it becomes independent of changes
820 to registers and memory.
821 X isn't actually modified; if modifications are needed, new rtl is
822 allocated. However, the return value can share rtl with X. */
825 cselib_subst_to_values (x)
826 rtx x;
828 enum rtx_code code = GET_CODE (x);
829 const char *fmt = GET_RTX_FORMAT (code);
830 cselib_val *e;
831 struct elt_list *l;
832 rtx copy = x;
833 int i;
835 switch (code)
837 case REG:
838 l = REG_VALUES (REGNO (x));
839 if (l && l->elt == NULL)
840 l = l->next;
841 for (; l; l = l->next)
842 if (GET_MODE (l->elt->u.val_rtx) == GET_MODE (x))
843 return l->elt->u.val_rtx;
845 abort ();
847 case MEM:
848 e = cselib_lookup_mem (x, 0);
849 if (! e)
851 /* This happens for autoincrements. Assign a value that doesn't
852 match any other. */
853 e = new_cselib_val (++next_unknown_value, GET_MODE (x));
855 return e->u.val_rtx;
857 case CONST_DOUBLE:
858 case CONST_VECTOR:
859 case CONST_INT:
860 return x;
862 case POST_INC:
863 case PRE_INC:
864 case POST_DEC:
865 case PRE_DEC:
866 case POST_MODIFY:
867 case PRE_MODIFY:
868 e = new_cselib_val (++next_unknown_value, GET_MODE (x));
869 return e->u.val_rtx;
871 default:
872 break;
875 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
877 if (fmt[i] == 'e')
879 rtx t = cselib_subst_to_values (XEXP (x, i));
881 if (t != XEXP (x, i) && x == copy)
882 copy = shallow_copy_rtx (x);
884 XEXP (copy, i) = t;
886 else if (fmt[i] == 'E')
888 int j, k;
890 for (j = 0; j < XVECLEN (x, i); j++)
892 rtx t = cselib_subst_to_values (XVECEXP (x, i, j));
894 if (t != XVECEXP (x, i, j) && XVEC (x, i) == XVEC (copy, i))
896 if (x == copy)
897 copy = shallow_copy_rtx (x);
899 XVEC (copy, i) = rtvec_alloc (XVECLEN (x, i));
900 for (k = 0; k < j; k++)
901 XVECEXP (copy, i, k) = XVECEXP (x, i, k);
904 XVECEXP (copy, i, j) = t;
909 return copy;
912 /* Look up the rtl expression X in our tables and return the value it has.
913 If CREATE is zero, we return NULL if we don't know the value. Otherwise,
914 we create a new one if possible, using mode MODE if X doesn't have a mode
915 (i.e. because it's a constant). */
917 cselib_val *
918 cselib_lookup (x, mode, create)
919 rtx x;
920 enum machine_mode mode;
921 int create;
923 void **slot;
924 cselib_val *e;
925 unsigned int hashval;
927 if (GET_MODE (x) != VOIDmode)
928 mode = GET_MODE (x);
930 if (GET_CODE (x) == VALUE)
931 return CSELIB_VAL_PTR (x);
933 if (GET_CODE (x) == REG)
935 struct elt_list *l;
936 unsigned int i = REGNO (x);
938 l = REG_VALUES (i);
939 if (l && l->elt == NULL)
940 l = l->next;
941 for (; l; l = l->next)
942 if (mode == GET_MODE (l->elt->u.val_rtx))
943 return l->elt;
945 if (! create)
946 return 0;
948 if (i < FIRST_PSEUDO_REGISTER)
950 unsigned int n = HARD_REGNO_NREGS (i, mode);
952 if (n > max_value_regs)
953 max_value_regs = n;
956 e = new_cselib_val (++next_unknown_value, GET_MODE (x));
957 e->locs = new_elt_loc_list (e->locs, x);
958 if (REG_VALUES (i) == 0)
960 /* Maintain the invariant that the first entry of
961 REG_VALUES, if present, must be the value used to set the
962 register, or NULL. */
963 VARRAY_PUSH_UINT (used_regs, i);
964 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
966 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
967 slot = htab_find_slot_with_hash (hash_table, x, e->value, INSERT);
968 *slot = e;
969 return e;
972 if (GET_CODE (x) == MEM)
973 return cselib_lookup_mem (x, create);
975 hashval = hash_rtx (x, mode, create);
976 /* Can't even create if hashing is not possible. */
977 if (! hashval)
978 return 0;
980 slot = htab_find_slot_with_hash (hash_table, wrap_constant (mode, x),
981 hashval, create ? INSERT : NO_INSERT);
982 if (slot == 0)
983 return 0;
985 e = (cselib_val *) *slot;
986 if (e)
987 return e;
989 e = new_cselib_val (hashval, mode);
991 /* We have to fill the slot before calling cselib_subst_to_values:
992 the hash table is inconsistent until we do so, and
993 cselib_subst_to_values will need to do lookups. */
994 *slot = (void *) e;
995 e->locs = new_elt_loc_list (e->locs, cselib_subst_to_values (x));
996 return e;
999 /* Invalidate any entries in reg_values that overlap REGNO. This is called
1000 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
1001 is used to determine how many hard registers are being changed. If MODE
1002 is VOIDmode, then only REGNO is being changed; this is used when
1003 invalidating call clobbered registers across a call. */
1005 static void
1006 cselib_invalidate_regno (regno, mode)
1007 unsigned int regno;
1008 enum machine_mode mode;
1010 unsigned int endregno;
1011 unsigned int i;
1013 /* If we see pseudos after reload, something is _wrong_. */
1014 if (reload_completed && regno >= FIRST_PSEUDO_REGISTER
1015 && reg_renumber[regno] >= 0)
1016 abort ();
1018 /* Determine the range of registers that must be invalidated. For
1019 pseudos, only REGNO is affected. For hard regs, we must take MODE
1020 into account, and we must also invalidate lower register numbers
1021 if they contain values that overlap REGNO. */
1022 if (regno < FIRST_PSEUDO_REGISTER && mode != VOIDmode)
1024 if (regno < max_value_regs)
1025 i = 0;
1026 else
1027 i = regno - max_value_regs;
1029 endregno = regno + HARD_REGNO_NREGS (regno, mode);
1031 else
1033 i = regno;
1034 endregno = regno + 1;
1037 for (; i < endregno; i++)
1039 struct elt_list **l = &REG_VALUES (i);
1041 /* Go through all known values for this reg; if it overlaps the range
1042 we're invalidating, remove the value. */
1043 while (*l)
1045 cselib_val *v = (*l)->elt;
1046 struct elt_loc_list **p;
1047 unsigned int this_last = i;
1049 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
1050 this_last += HARD_REGNO_NREGS (i, GET_MODE (v->u.val_rtx)) - 1;
1052 if (this_last < regno || v == NULL)
1054 l = &(*l)->next;
1055 continue;
1058 /* We have an overlap. */
1059 if (*l == REG_VALUES (i))
1061 /* Maintain the invariant that the first entry of
1062 REG_VALUES, if present, must be the value used to set
1063 the register, or NULL. This is also nice because
1064 then we won't push the same regno onto user_regs
1065 multiple times. */
1066 (*l)->elt = NULL;
1067 l = &(*l)->next;
1069 else
1070 unchain_one_elt_list (l);
1072 /* Now, we clear the mapping from value to reg. It must exist, so
1073 this code will crash intentionally if it doesn't. */
1074 for (p = &v->locs; ; p = &(*p)->next)
1076 rtx x = (*p)->loc;
1078 if (GET_CODE (x) == REG && REGNO (x) == i)
1080 unchain_one_elt_loc_list (p);
1081 break;
1084 if (v->locs == 0)
1085 n_useless_values++;
1090 /* The memory at address MEM_BASE is being changed.
1091 Return whether this change will invalidate VAL. */
1093 static int
1094 cselib_mem_conflict_p (mem_base, val)
1095 rtx mem_base;
1096 rtx val;
1098 enum rtx_code code;
1099 const char *fmt;
1100 int i, j;
1102 code = GET_CODE (val);
1103 switch (code)
1105 /* Get rid of a few simple cases quickly. */
1106 case REG:
1107 case PC:
1108 case CC0:
1109 case SCRATCH:
1110 case CONST:
1111 case CONST_INT:
1112 case CONST_DOUBLE:
1113 case CONST_VECTOR:
1114 case SYMBOL_REF:
1115 case LABEL_REF:
1116 return 0;
1118 case MEM:
1119 if (GET_MODE (mem_base) == BLKmode
1120 || GET_MODE (val) == BLKmode
1121 || anti_dependence (val, mem_base))
1122 return 1;
1124 /* The address may contain nested MEMs. */
1125 break;
1127 default:
1128 break;
1131 fmt = GET_RTX_FORMAT (code);
1132 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1134 if (fmt[i] == 'e')
1136 if (cselib_mem_conflict_p (mem_base, XEXP (val, i)))
1137 return 1;
1139 else if (fmt[i] == 'E')
1140 for (j = 0; j < XVECLEN (val, i); j++)
1141 if (cselib_mem_conflict_p (mem_base, XVECEXP (val, i, j)))
1142 return 1;
1145 return 0;
1148 /* Invalidate any locations in the table which are changed because of a
1149 store to MEM_RTX. If this is called because of a non-const call
1150 instruction, MEM_RTX is (mem:BLK const0_rtx). */
1152 static void
1153 cselib_invalidate_mem (mem_rtx)
1154 rtx mem_rtx;
1156 cselib_val **vp, *v, *next;
1158 vp = &first_containing_mem;
1159 for (v = *vp; v != &dummy_val; v = next)
1161 bool has_mem = false;
1162 struct elt_loc_list **p = &v->locs;
1163 int had_locs = v->locs != 0;
1165 while (*p)
1167 rtx x = (*p)->loc;
1168 cselib_val *addr;
1169 struct elt_list **mem_chain;
1171 /* MEMs may occur in locations only at the top level; below
1172 that every MEM or REG is substituted by its VALUE. */
1173 if (GET_CODE (x) != MEM)
1175 p = &(*p)->next;
1176 continue;
1178 if (! cselib_mem_conflict_p (mem_rtx, x))
1180 has_mem = true;
1181 p = &(*p)->next;
1182 continue;
1185 /* This one overlaps. */
1186 /* We must have a mapping from this MEM's address to the
1187 value (E). Remove that, too. */
1188 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0);
1189 mem_chain = &addr->addr_list;
1190 for (;;)
1192 if ((*mem_chain)->elt == v)
1194 unchain_one_elt_list (mem_chain);
1195 break;
1198 mem_chain = &(*mem_chain)->next;
1201 unchain_one_elt_loc_list (p);
1204 if (had_locs && v->locs == 0)
1205 n_useless_values++;
1207 next = v->next_containing_mem;
1208 if (has_mem)
1210 *vp = v;
1211 vp = &(*vp)->next_containing_mem;
1213 else
1214 v->next_containing_mem = NULL;
1216 *vp = &dummy_val;
1219 /* Invalidate DEST, which is being assigned to or clobbered. The second and
1220 the third parameter exist so that this function can be passed to
1221 note_stores; they are ignored. */
1223 static void
1224 cselib_invalidate_rtx (dest, ignore, data)
1225 rtx dest;
1226 rtx ignore ATTRIBUTE_UNUSED;
1227 void *data ATTRIBUTE_UNUSED;
1229 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SIGN_EXTRACT
1230 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG)
1231 dest = XEXP (dest, 0);
1233 if (GET_CODE (dest) == REG)
1234 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
1235 else if (GET_CODE (dest) == MEM)
1236 cselib_invalidate_mem (dest);
1238 /* Some machines don't define AUTO_INC_DEC, but they still use push
1239 instructions. We need to catch that case here in order to
1240 invalidate the stack pointer correctly. Note that invalidating
1241 the stack pointer is different from invalidating DEST. */
1242 if (push_operand (dest, GET_MODE (dest)))
1243 cselib_invalidate_rtx (stack_pointer_rtx, NULL_RTX, NULL);
1246 /* Record the result of a SET instruction. DEST is being set; the source
1247 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
1248 describes its address. */
1250 static void
1251 cselib_record_set (dest, src_elt, dest_addr_elt)
1252 rtx dest;
1253 cselib_val *src_elt, *dest_addr_elt;
1255 int dreg = GET_CODE (dest) == REG ? (int) REGNO (dest) : -1;
1257 if (src_elt == 0 || side_effects_p (dest))
1258 return;
1260 if (dreg >= 0)
1262 if (dreg < FIRST_PSEUDO_REGISTER)
1264 unsigned int n = HARD_REGNO_NREGS (dreg, GET_MODE (dest));
1266 if (n > max_value_regs)
1267 max_value_regs = n;
1270 if (REG_VALUES (dreg) == 0)
1272 VARRAY_PUSH_UINT (used_regs, dreg);
1273 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
1275 else
1277 if (REG_VALUES (dreg)->elt == 0)
1278 REG_VALUES (dreg)->elt = src_elt;
1279 else
1280 /* The register should have been invalidated. */
1281 abort ();
1284 if (src_elt->locs == 0)
1285 n_useless_values--;
1286 src_elt->locs = new_elt_loc_list (src_elt->locs, dest);
1288 else if (GET_CODE (dest) == MEM && dest_addr_elt != 0)
1290 if (src_elt->locs == 0)
1291 n_useless_values--;
1292 add_mem_for_addr (dest_addr_elt, src_elt, dest);
1296 /* Describe a single set that is part of an insn. */
1297 struct set
1299 rtx src;
1300 rtx dest;
1301 cselib_val *src_elt;
1302 cselib_val *dest_addr_elt;
1305 /* There is no good way to determine how many elements there can be
1306 in a PARALLEL. Since it's fairly cheap, use a really large number. */
1307 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
1309 /* Record the effects of any sets in INSN. */
1310 static void
1311 cselib_record_sets (insn)
1312 rtx insn;
1314 int n_sets = 0;
1315 int i;
1316 struct set sets[MAX_SETS];
1317 rtx body = PATTERN (insn);
1318 rtx cond = 0;
1320 body = PATTERN (insn);
1321 if (GET_CODE (body) == COND_EXEC)
1323 cond = COND_EXEC_TEST (body);
1324 body = COND_EXEC_CODE (body);
1327 /* Find all sets. */
1328 if (GET_CODE (body) == SET)
1330 sets[0].src = SET_SRC (body);
1331 sets[0].dest = SET_DEST (body);
1332 n_sets = 1;
1334 else if (GET_CODE (body) == PARALLEL)
1336 /* Look through the PARALLEL and record the values being
1337 set, if possible. Also handle any CLOBBERs. */
1338 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
1340 rtx x = XVECEXP (body, 0, i);
1342 if (GET_CODE (x) == SET)
1344 sets[n_sets].src = SET_SRC (x);
1345 sets[n_sets].dest = SET_DEST (x);
1346 n_sets++;
1351 /* Look up the values that are read. Do this before invalidating the
1352 locations that are written. */
1353 for (i = 0; i < n_sets; i++)
1355 rtx dest = sets[i].dest;
1357 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
1358 the low part after invalidating any knowledge about larger modes. */
1359 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
1360 sets[i].dest = dest = XEXP (dest, 0);
1362 /* We don't know how to record anything but REG or MEM. */
1363 if (GET_CODE (dest) == REG || GET_CODE (dest) == MEM)
1365 rtx src = sets[i].src;
1366 if (cond)
1367 src = gen_rtx_IF_THEN_ELSE (GET_MODE (src), cond, src, dest);
1368 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1);
1369 if (GET_CODE (dest) == MEM)
1370 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0), Pmode, 1);
1371 else
1372 sets[i].dest_addr_elt = 0;
1376 /* Invalidate all locations written by this insn. Note that the elts we
1377 looked up in the previous loop aren't affected, just some of their
1378 locations may go away. */
1379 note_stores (body, cselib_invalidate_rtx, NULL);
1381 /* Now enter the equivalences in our tables. */
1382 for (i = 0; i < n_sets; i++)
1384 rtx dest = sets[i].dest;
1385 if (GET_CODE (dest) == REG || GET_CODE (dest) == MEM)
1386 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
1390 /* Record the effects of INSN. */
1392 void
1393 cselib_process_insn (insn)
1394 rtx insn;
1396 int i;
1397 rtx x;
1399 if (find_reg_note (insn, REG_LIBCALL, NULL))
1400 cselib_current_insn_in_libcall = true;
1401 if (find_reg_note (insn, REG_RETVAL, NULL))
1402 cselib_current_insn_in_libcall = false;
1403 cselib_current_insn = insn;
1405 /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp. */
1406 if (GET_CODE (insn) == CODE_LABEL
1407 || (GET_CODE (insn) == CALL_INSN
1408 && find_reg_note (insn, REG_SETJMP, NULL))
1409 || (GET_CODE (insn) == INSN
1410 && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
1411 && MEM_VOLATILE_P (PATTERN (insn))))
1413 clear_table ();
1414 return;
1417 if (! INSN_P (insn))
1419 cselib_current_insn = 0;
1420 return;
1423 /* If this is a call instruction, forget anything stored in a
1424 call clobbered register, or, if this is not a const call, in
1425 memory. */
1426 if (GET_CODE (insn) == CALL_INSN)
1428 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1429 if (call_used_regs[i])
1430 cselib_invalidate_regno (i, VOIDmode);
1432 if (! CONST_OR_PURE_CALL_P (insn))
1433 cselib_invalidate_mem (callmem);
1436 cselib_record_sets (insn);
1438 #ifdef AUTO_INC_DEC
1439 /* Clobber any registers which appear in REG_INC notes. We
1440 could keep track of the changes to their values, but it is
1441 unlikely to help. */
1442 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
1443 if (REG_NOTE_KIND (x) == REG_INC)
1444 cselib_invalidate_rtx (XEXP (x, 0), NULL_RTX, NULL);
1445 #endif
1447 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
1448 after we have processed the insn. */
1449 if (GET_CODE (insn) == CALL_INSN)
1450 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
1451 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
1452 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0), NULL_RTX, NULL);
1454 cselib_current_insn = 0;
1456 if (n_useless_values > MAX_USELESS_VALUES)
1457 remove_useless_values ();
1460 /* Make sure our varrays are big enough. Not called from any cselib routines;
1461 it must be called by the user if it allocated new registers. */
1463 void
1464 cselib_update_varray_sizes ()
1466 unsigned int nregs = max_reg_num ();
1468 if (nregs == cselib_nregs)
1469 return;
1471 cselib_nregs = nregs;
1472 VARRAY_GROW (reg_values, nregs);
1473 VARRAY_GROW (used_regs, nregs);
1476 /* Initialize cselib for one pass. The caller must also call
1477 init_alias_analysis. */
1479 void
1480 cselib_init ()
1482 /* This is only created once. */
1483 if (! callmem)
1484 callmem = gen_rtx_MEM (BLKmode, const0_rtx);
1486 cselib_nregs = max_reg_num ();
1487 if (reg_values_old != NULL && VARRAY_SIZE (reg_values_old) >= cselib_nregs)
1489 reg_values = reg_values_old;
1490 used_regs = used_regs_old;
1492 else
1494 VARRAY_ELT_LIST_INIT (reg_values, cselib_nregs, "reg_values");
1495 VARRAY_UINT_INIT (used_regs, cselib_nregs, "used_regs");
1497 hash_table = htab_create_ggc (31, get_value_hash, entry_and_rtx_equal_p,
1498 NULL);
1499 cselib_current_insn_in_libcall = false;
1502 /* Called when the current user is done with cselib. */
1504 void
1505 cselib_finish ()
1507 clear_table ();
1508 reg_values_old = reg_values;
1509 reg_values = 0;
1510 used_regs_old = used_regs;
1511 used_regs = 0;
1512 hash_table = 0;
1513 n_useless_values = 0;
1514 next_unknown_value = 0;
1517 #include "gt-cselib.h"