* config.gcc: Add an extra_header for ARM targets.
[official-gcc.git] / gcc / cselib.c
blob3ea7c236abdb126b6d5f0ce2801f863815d61f4a
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)
1024 if (mode == VOIDmode)
1025 abort ();
1027 if (regno < max_value_regs)
1028 i = 0;
1029 else
1030 i = regno - max_value_regs;
1032 endregno = regno + HARD_REGNO_NREGS (regno, mode);
1034 else
1036 i = regno;
1037 endregno = regno + 1;
1040 for (; i < endregno; i++)
1042 struct elt_list **l = &REG_VALUES (i);
1044 /* Go through all known values for this reg; if it overlaps the range
1045 we're invalidating, remove the value. */
1046 while (*l)
1048 cselib_val *v = (*l)->elt;
1049 struct elt_loc_list **p;
1050 unsigned int this_last = i;
1052 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
1053 this_last += HARD_REGNO_NREGS (i, GET_MODE (v->u.val_rtx)) - 1;
1055 if (this_last < regno || v == NULL)
1057 l = &(*l)->next;
1058 continue;
1061 /* We have an overlap. */
1062 if (*l == REG_VALUES (i))
1064 /* Maintain the invariant that the first entry of
1065 REG_VALUES, if present, must be the value used to set
1066 the register, or NULL. This is also nice because
1067 then we won't push the same regno onto user_regs
1068 multiple times. */
1069 (*l)->elt = NULL;
1070 l = &(*l)->next;
1072 else
1073 unchain_one_elt_list (l);
1075 /* Now, we clear the mapping from value to reg. It must exist, so
1076 this code will crash intentionally if it doesn't. */
1077 for (p = &v->locs; ; p = &(*p)->next)
1079 rtx x = (*p)->loc;
1081 if (GET_CODE (x) == REG && REGNO (x) == i)
1083 unchain_one_elt_loc_list (p);
1084 break;
1087 if (v->locs == 0)
1088 n_useless_values++;
1093 /* The memory at address MEM_BASE is being changed.
1094 Return whether this change will invalidate VAL. */
1096 static int
1097 cselib_mem_conflict_p (mem_base, val)
1098 rtx mem_base;
1099 rtx val;
1101 enum rtx_code code;
1102 const char *fmt;
1103 int i, j;
1105 code = GET_CODE (val);
1106 switch (code)
1108 /* Get rid of a few simple cases quickly. */
1109 case REG:
1110 case PC:
1111 case CC0:
1112 case SCRATCH:
1113 case CONST:
1114 case CONST_INT:
1115 case CONST_DOUBLE:
1116 case CONST_VECTOR:
1117 case SYMBOL_REF:
1118 case LABEL_REF:
1119 return 0;
1121 case MEM:
1122 if (GET_MODE (mem_base) == BLKmode
1123 || GET_MODE (val) == BLKmode
1124 || anti_dependence (val, mem_base))
1125 return 1;
1127 /* The address may contain nested MEMs. */
1128 break;
1130 default:
1131 break;
1134 fmt = GET_RTX_FORMAT (code);
1135 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1137 if (fmt[i] == 'e')
1139 if (cselib_mem_conflict_p (mem_base, XEXP (val, i)))
1140 return 1;
1142 else if (fmt[i] == 'E')
1143 for (j = 0; j < XVECLEN (val, i); j++)
1144 if (cselib_mem_conflict_p (mem_base, XVECEXP (val, i, j)))
1145 return 1;
1148 return 0;
1151 /* Invalidate any locations in the table which are changed because of a
1152 store to MEM_RTX. If this is called because of a non-const call
1153 instruction, MEM_RTX is (mem:BLK const0_rtx). */
1155 static void
1156 cselib_invalidate_mem (mem_rtx)
1157 rtx mem_rtx;
1159 cselib_val **vp, *v, *next;
1161 vp = &first_containing_mem;
1162 for (v = *vp; v != &dummy_val; v = next)
1164 bool has_mem = false;
1165 struct elt_loc_list **p = &v->locs;
1166 int had_locs = v->locs != 0;
1168 while (*p)
1170 rtx x = (*p)->loc;
1171 cselib_val *addr;
1172 struct elt_list **mem_chain;
1174 /* MEMs may occur in locations only at the top level; below
1175 that every MEM or REG is substituted by its VALUE. */
1176 if (GET_CODE (x) != MEM)
1178 p = &(*p)->next;
1179 continue;
1181 if (! cselib_mem_conflict_p (mem_rtx, x))
1183 has_mem = true;
1184 p = &(*p)->next;
1185 continue;
1188 /* This one overlaps. */
1189 /* We must have a mapping from this MEM's address to the
1190 value (E). Remove that, too. */
1191 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0);
1192 mem_chain = &addr->addr_list;
1193 for (;;)
1195 if ((*mem_chain)->elt == v)
1197 unchain_one_elt_list (mem_chain);
1198 break;
1201 mem_chain = &(*mem_chain)->next;
1204 unchain_one_elt_loc_list (p);
1207 if (had_locs && v->locs == 0)
1208 n_useless_values++;
1210 next = v->next_containing_mem;
1211 if (has_mem)
1213 *vp = v;
1214 vp = &(*vp)->next_containing_mem;
1216 else
1217 v->next_containing_mem = NULL;
1219 *vp = &dummy_val;
1222 /* Invalidate DEST, which is being assigned to or clobbered. The second and
1223 the third parameter exist so that this function can be passed to
1224 note_stores; they are ignored. */
1226 static void
1227 cselib_invalidate_rtx (dest, ignore, data)
1228 rtx dest;
1229 rtx ignore ATTRIBUTE_UNUSED;
1230 void *data ATTRIBUTE_UNUSED;
1232 while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SIGN_EXTRACT
1233 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG)
1234 dest = XEXP (dest, 0);
1236 if (GET_CODE (dest) == REG)
1237 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
1238 else if (GET_CODE (dest) == MEM)
1239 cselib_invalidate_mem (dest);
1241 /* Some machines don't define AUTO_INC_DEC, but they still use push
1242 instructions. We need to catch that case here in order to
1243 invalidate the stack pointer correctly. Note that invalidating
1244 the stack pointer is different from invalidating DEST. */
1245 if (push_operand (dest, GET_MODE (dest)))
1246 cselib_invalidate_rtx (stack_pointer_rtx, NULL_RTX, NULL);
1249 /* Record the result of a SET instruction. DEST is being set; the source
1250 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
1251 describes its address. */
1253 static void
1254 cselib_record_set (dest, src_elt, dest_addr_elt)
1255 rtx dest;
1256 cselib_val *src_elt, *dest_addr_elt;
1258 int dreg = GET_CODE (dest) == REG ? (int) REGNO (dest) : -1;
1260 if (src_elt == 0 || side_effects_p (dest))
1261 return;
1263 if (dreg >= 0)
1265 if (dreg < FIRST_PSEUDO_REGISTER)
1267 unsigned int n = HARD_REGNO_NREGS (dreg, GET_MODE (dest));
1269 if (n > max_value_regs)
1270 max_value_regs = n;
1273 if (REG_VALUES (dreg) == 0)
1275 VARRAY_PUSH_UINT (used_regs, dreg);
1276 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
1278 else
1280 if (REG_VALUES (dreg)->elt == 0)
1281 REG_VALUES (dreg)->elt = src_elt;
1282 else
1283 /* The register should have been invalidated. */
1284 abort ();
1287 if (src_elt->locs == 0)
1288 n_useless_values--;
1289 src_elt->locs = new_elt_loc_list (src_elt->locs, dest);
1291 else if (GET_CODE (dest) == MEM && dest_addr_elt != 0)
1293 if (src_elt->locs == 0)
1294 n_useless_values--;
1295 add_mem_for_addr (dest_addr_elt, src_elt, dest);
1299 /* Describe a single set that is part of an insn. */
1300 struct set
1302 rtx src;
1303 rtx dest;
1304 cselib_val *src_elt;
1305 cselib_val *dest_addr_elt;
1308 /* There is no good way to determine how many elements there can be
1309 in a PARALLEL. Since it's fairly cheap, use a really large number. */
1310 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
1312 /* Record the effects of any sets in INSN. */
1313 static void
1314 cselib_record_sets (insn)
1315 rtx insn;
1317 int n_sets = 0;
1318 int i;
1319 struct set sets[MAX_SETS];
1320 rtx body = PATTERN (insn);
1321 rtx cond = 0;
1323 body = PATTERN (insn);
1324 if (GET_CODE (body) == COND_EXEC)
1326 cond = COND_EXEC_TEST (body);
1327 body = COND_EXEC_CODE (body);
1330 /* Find all sets. */
1331 if (GET_CODE (body) == SET)
1333 sets[0].src = SET_SRC (body);
1334 sets[0].dest = SET_DEST (body);
1335 n_sets = 1;
1337 else if (GET_CODE (body) == PARALLEL)
1339 /* Look through the PARALLEL and record the values being
1340 set, if possible. Also handle any CLOBBERs. */
1341 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
1343 rtx x = XVECEXP (body, 0, i);
1345 if (GET_CODE (x) == SET)
1347 sets[n_sets].src = SET_SRC (x);
1348 sets[n_sets].dest = SET_DEST (x);
1349 n_sets++;
1354 /* Look up the values that are read. Do this before invalidating the
1355 locations that are written. */
1356 for (i = 0; i < n_sets; i++)
1358 rtx dest = sets[i].dest;
1360 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
1361 the low part after invalidating any knowledge about larger modes. */
1362 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
1363 sets[i].dest = dest = XEXP (dest, 0);
1365 /* We don't know how to record anything but REG or MEM. */
1366 if (GET_CODE (dest) == REG || GET_CODE (dest) == MEM)
1368 rtx src = sets[i].src;
1369 if (cond)
1370 src = gen_rtx_IF_THEN_ELSE (GET_MODE (src), cond, src, dest);
1371 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1);
1372 if (GET_CODE (dest) == MEM)
1373 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0), Pmode, 1);
1374 else
1375 sets[i].dest_addr_elt = 0;
1379 /* Invalidate all locations written by this insn. Note that the elts we
1380 looked up in the previous loop aren't affected, just some of their
1381 locations may go away. */
1382 note_stores (body, cselib_invalidate_rtx, NULL);
1384 /* Now enter the equivalences in our tables. */
1385 for (i = 0; i < n_sets; i++)
1387 rtx dest = sets[i].dest;
1388 if (GET_CODE (dest) == REG || GET_CODE (dest) == MEM)
1389 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
1393 /* Record the effects of INSN. */
1395 void
1396 cselib_process_insn (insn)
1397 rtx insn;
1399 int i;
1400 rtx x;
1402 if (find_reg_note (insn, REG_LIBCALL, NULL))
1403 cselib_current_insn_in_libcall = true;
1404 if (find_reg_note (insn, REG_RETVAL, NULL))
1405 cselib_current_insn_in_libcall = false;
1406 cselib_current_insn = insn;
1408 /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp. */
1409 if (GET_CODE (insn) == CODE_LABEL
1410 || (GET_CODE (insn) == CALL_INSN
1411 && find_reg_note (insn, REG_SETJMP, NULL))
1412 || (GET_CODE (insn) == INSN
1413 && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
1414 && MEM_VOLATILE_P (PATTERN (insn))))
1416 clear_table ();
1417 return;
1420 if (! INSN_P (insn))
1422 cselib_current_insn = 0;
1423 return;
1426 /* If this is a call instruction, forget anything stored in a
1427 call clobbered register, or, if this is not a const call, in
1428 memory. */
1429 if (GET_CODE (insn) == CALL_INSN)
1431 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1432 if (call_used_regs[i])
1433 cselib_invalidate_regno (i, reg_raw_mode[i]);
1435 if (! CONST_OR_PURE_CALL_P (insn))
1436 cselib_invalidate_mem (callmem);
1439 cselib_record_sets (insn);
1441 #ifdef AUTO_INC_DEC
1442 /* Clobber any registers which appear in REG_INC notes. We
1443 could keep track of the changes to their values, but it is
1444 unlikely to help. */
1445 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
1446 if (REG_NOTE_KIND (x) == REG_INC)
1447 cselib_invalidate_rtx (XEXP (x, 0), NULL_RTX, NULL);
1448 #endif
1450 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
1451 after we have processed the insn. */
1452 if (GET_CODE (insn) == CALL_INSN)
1453 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
1454 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
1455 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0), NULL_RTX, NULL);
1457 cselib_current_insn = 0;
1459 if (n_useless_values > MAX_USELESS_VALUES)
1460 remove_useless_values ();
1463 /* Make sure our varrays are big enough. Not called from any cselib routines;
1464 it must be called by the user if it allocated new registers. */
1466 void
1467 cselib_update_varray_sizes ()
1469 unsigned int nregs = max_reg_num ();
1471 if (nregs == cselib_nregs)
1472 return;
1474 cselib_nregs = nregs;
1475 VARRAY_GROW (reg_values, nregs);
1476 VARRAY_GROW (used_regs, nregs);
1479 /* Initialize cselib for one pass. The caller must also call
1480 init_alias_analysis. */
1482 void
1483 cselib_init ()
1485 /* This is only created once. */
1486 if (! callmem)
1487 callmem = gen_rtx_MEM (BLKmode, const0_rtx);
1489 cselib_nregs = max_reg_num ();
1490 if (reg_values_old != NULL && VARRAY_SIZE (reg_values_old) >= cselib_nregs)
1492 reg_values = reg_values_old;
1493 used_regs = used_regs_old;
1495 else
1497 VARRAY_ELT_LIST_INIT (reg_values, cselib_nregs, "reg_values");
1498 VARRAY_UINT_INIT (used_regs, cselib_nregs, "used_regs");
1500 hash_table = htab_create_ggc (31, get_value_hash, entry_and_rtx_equal_p,
1501 NULL);
1502 cselib_current_insn_in_libcall = false;
1505 /* Called when the current user is done with cselib. */
1507 void
1508 cselib_finish ()
1510 clear_table ();
1511 reg_values_old = reg_values;
1512 reg_values = 0;
1513 used_regs_old = used_regs;
1514 used_regs = 0;
1515 hash_table = 0;
1516 n_useless_values = 0;
1517 next_unknown_value = 0;
1520 #include "gt-cselib.h"