2013-11-27 Kugan Vivekanandarajah <kuganv@linaro.org>
[official-gcc.git] / gcc / cselib.c
blobe201f5e7c490c57850b4d8e1d8335b4802078c39
1 /* Common subexpression elimination library for GNU compiler.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
25 #include "rtl.h"
26 #include "tree.h"/* FIXME: For hashing DEBUG_EXPR & friends. */
27 #include "tm_p.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "flags.h"
31 #include "insn-config.h"
32 #include "recog.h"
33 #include "function.h"
34 #include "emit-rtl.h"
35 #include "diagnostic-core.h"
36 #include "ggc.h"
37 #include "hash-table.h"
38 #include "dumpfile.h"
39 #include "cselib.h"
40 #include "valtrack.h"
41 #include "params.h"
42 #include "alloc-pool.h"
43 #include "target.h"
44 #include "bitmap.h"
46 /* A list of cselib_val structures. */
47 struct elt_list {
48 struct elt_list *next;
49 cselib_val *elt;
52 /* See the documentation of cselib_find_slot below. */
53 static enum machine_mode find_slot_memmode;
55 static bool cselib_record_memory;
56 static bool cselib_preserve_constants;
57 static bool cselib_any_perm_equivs;
58 static inline void promote_debug_loc (struct elt_loc_list *l);
59 static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
60 static void new_elt_loc_list (cselib_val *, rtx);
61 static void unchain_one_value (cselib_val *);
62 static void unchain_one_elt_list (struct elt_list **);
63 static void unchain_one_elt_loc_list (struct elt_loc_list **);
64 static void remove_useless_values (void);
65 static int rtx_equal_for_cselib_1 (rtx, rtx, enum machine_mode);
66 static unsigned int cselib_hash_rtx (rtx, int, enum machine_mode);
67 static cselib_val *new_cselib_val (unsigned int, enum machine_mode, rtx);
68 static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
69 static cselib_val *cselib_lookup_mem (rtx, int);
70 static void cselib_invalidate_regno (unsigned int, enum machine_mode);
71 static void cselib_invalidate_mem (rtx);
72 static void cselib_record_set (rtx, cselib_val *, cselib_val *);
73 static void cselib_record_sets (rtx);
75 struct expand_value_data
77 bitmap regs_active;
78 cselib_expand_callback callback;
79 void *callback_arg;
80 bool dummy;
83 static rtx cselib_expand_value_rtx_1 (rtx, struct expand_value_data *, int);
85 /* There are three ways in which cselib can look up an rtx:
86 - for a REG, the reg_values table (which is indexed by regno) is used
87 - for a MEM, we recursively look up its address and then follow the
88 addr_list of that value
89 - for everything else, we compute a hash value and go through the hash
90 table. Since different rtx's can still have the same hash value,
91 this involves walking the table entries for a given value and comparing
92 the locations of the entries with the rtx we are looking up. */
94 struct cselib_hasher : typed_noop_remove <cselib_val>
96 typedef cselib_val value_type;
97 typedef rtx_def compare_type;
98 static inline hashval_t hash (const value_type *);
99 static inline bool equal (const value_type *, const compare_type *);
102 /* The hash function for our hash table. The value is always computed with
103 cselib_hash_rtx when adding an element; this function just extracts the
104 hash value from a cselib_val structure. */
106 inline hashval_t
107 cselib_hasher::hash (const value_type *v)
109 return v->hash;
112 /* The equality test for our hash table. The first argument V is a table
113 element (i.e. a cselib_val), while the second arg X is an rtx. We know
114 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
115 CONST of an appropriate mode. */
117 inline bool
118 cselib_hasher::equal (const value_type *v, const compare_type *x_arg)
120 struct elt_loc_list *l;
121 rtx x = CONST_CAST_RTX (x_arg);
122 enum machine_mode mode = GET_MODE (x);
124 gcc_assert (!CONST_SCALAR_INT_P (x) && GET_CODE (x) != CONST_FIXED);
126 if (mode != GET_MODE (v->val_rtx))
127 return false;
129 /* Unwrap X if necessary. */
130 if (GET_CODE (x) == CONST
131 && (CONST_SCALAR_INT_P (XEXP (x, 0))
132 || GET_CODE (XEXP (x, 0)) == CONST_FIXED))
133 x = XEXP (x, 0);
135 /* We don't guarantee that distinct rtx's have different hash values,
136 so we need to do a comparison. */
137 for (l = v->locs; l; l = l->next)
138 if (rtx_equal_for_cselib_1 (l->loc, x, find_slot_memmode))
140 promote_debug_loc (l);
141 return true;
144 return false;
147 /* A table that enables us to look up elts by their value. */
148 static hash_table <cselib_hasher> cselib_hash_table;
150 /* This is a global so we don't have to pass this through every function.
151 It is used in new_elt_loc_list to set SETTING_INSN. */
152 static rtx cselib_current_insn;
154 /* The unique id that the next create value will take. */
155 static unsigned int next_uid;
157 /* The number of registers we had when the varrays were last resized. */
158 static unsigned int cselib_nregs;
160 /* Count values without known locations, or with only locations that
161 wouldn't have been known except for debug insns. Whenever this
162 grows too big, we remove these useless values from the table.
164 Counting values with only debug values is a bit tricky. We don't
165 want to increment n_useless_values when we create a value for a
166 debug insn, for this would get n_useless_values out of sync, but we
167 want increment it if all locs in the list that were ever referenced
168 in nondebug insns are removed from the list.
170 In the general case, once we do that, we'd have to stop accepting
171 nondebug expressions in the loc list, to avoid having two values
172 equivalent that, without debug insns, would have been made into
173 separate values. However, because debug insns never introduce
174 equivalences themselves (no assignments), the only means for
175 growing loc lists is through nondebug assignments. If the locs
176 also happen to be referenced in debug insns, it will work just fine.
178 A consequence of this is that there's at most one debug-only loc in
179 each loc list. If we keep it in the first entry, testing whether
180 we have a debug-only loc list takes O(1).
182 Furthermore, since any additional entry in a loc list containing a
183 debug loc would have to come from an assignment (nondebug) that
184 references both the initial debug loc and the newly-equivalent loc,
185 the initial debug loc would be promoted to a nondebug loc, and the
186 loc list would not contain debug locs any more.
188 So the only case we have to be careful with in order to keep
189 n_useless_values in sync between debug and nondebug compilations is
190 to avoid incrementing n_useless_values when removing the single loc
191 from a value that turns out to not appear outside debug values. We
192 increment n_useless_debug_values instead, and leave such values
193 alone until, for other reasons, we garbage-collect useless
194 values. */
195 static int n_useless_values;
196 static int n_useless_debug_values;
198 /* Count values whose locs have been taken exclusively from debug
199 insns for the entire life of the value. */
200 static int n_debug_values;
202 /* Number of useless values before we remove them from the hash table. */
203 #define MAX_USELESS_VALUES 32
205 /* This table maps from register number to values. It does not
206 contain pointers to cselib_val structures, but rather elt_lists.
207 The purpose is to be able to refer to the same register in
208 different modes. The first element of the list defines the mode in
209 which the register was set; if the mode is unknown or the value is
210 no longer valid in that mode, ELT will be NULL for the first
211 element. */
212 static struct elt_list **reg_values;
213 static unsigned int reg_values_size;
214 #define REG_VALUES(i) reg_values[i]
216 /* The largest number of hard regs used by any entry added to the
217 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
218 static unsigned int max_value_regs;
220 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
221 in cselib_clear_table() for fast emptying. */
222 static unsigned int *used_regs;
223 static unsigned int n_used_regs;
225 /* We pass this to cselib_invalidate_mem to invalidate all of
226 memory for a non-const call instruction. */
227 static GTY(()) rtx callmem;
229 /* Set by discard_useless_locs if it deleted the last location of any
230 value. */
231 static int values_became_useless;
233 /* Used as stop element of the containing_mem list so we can check
234 presence in the list by checking the next pointer. */
235 static cselib_val dummy_val;
237 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
238 that is constant through the whole function and should never be
239 eliminated. */
240 static cselib_val *cfa_base_preserved_val;
241 static unsigned int cfa_base_preserved_regno = INVALID_REGNUM;
243 /* Used to list all values that contain memory reference.
244 May or may not contain the useless values - the list is compacted
245 each time memory is invalidated. */
246 static cselib_val *first_containing_mem = &dummy_val;
247 static alloc_pool elt_loc_list_pool, elt_list_pool, cselib_val_pool, value_pool;
249 /* If nonnull, cselib will call this function before freeing useless
250 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
251 void (*cselib_discard_hook) (cselib_val *);
253 /* If nonnull, cselib will call this function before recording sets or
254 even clobbering outputs of INSN. All the recorded sets will be
255 represented in the array sets[n_sets]. new_val_min can be used to
256 tell whether values present in sets are introduced by this
257 instruction. */
258 void (*cselib_record_sets_hook) (rtx insn, struct cselib_set *sets,
259 int n_sets);
261 #define PRESERVED_VALUE_P(RTX) \
262 (RTL_FLAG_CHECK1 ("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
264 #define SP_BASED_VALUE_P(RTX) \
265 (RTL_FLAG_CHECK1 ("SP_BASED_VALUE_P", (RTX), VALUE)->jump)
269 /* Allocate a struct elt_list and fill in its two elements with the
270 arguments. */
272 static inline struct elt_list *
273 new_elt_list (struct elt_list *next, cselib_val *elt)
275 struct elt_list *el;
276 el = (struct elt_list *) pool_alloc (elt_list_pool);
277 el->next = next;
278 el->elt = elt;
279 return el;
282 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
283 list. */
285 static inline void
286 new_elt_loc_list (cselib_val *val, rtx loc)
288 struct elt_loc_list *el, *next = val->locs;
290 gcc_checking_assert (!next || !next->setting_insn
291 || !DEBUG_INSN_P (next->setting_insn)
292 || cselib_current_insn == next->setting_insn);
294 /* If we're creating the first loc in a debug insn context, we've
295 just created a debug value. Count it. */
296 if (!next && cselib_current_insn && DEBUG_INSN_P (cselib_current_insn))
297 n_debug_values++;
299 val = canonical_cselib_val (val);
300 next = val->locs;
302 if (GET_CODE (loc) == VALUE)
304 loc = canonical_cselib_val (CSELIB_VAL_PTR (loc))->val_rtx;
306 gcc_checking_assert (PRESERVED_VALUE_P (loc)
307 == PRESERVED_VALUE_P (val->val_rtx));
309 if (val->val_rtx == loc)
310 return;
311 else if (val->uid > CSELIB_VAL_PTR (loc)->uid)
313 /* Reverse the insertion. */
314 new_elt_loc_list (CSELIB_VAL_PTR (loc), val->val_rtx);
315 return;
318 gcc_checking_assert (val->uid < CSELIB_VAL_PTR (loc)->uid);
320 if (CSELIB_VAL_PTR (loc)->locs)
322 /* Bring all locs from LOC to VAL. */
323 for (el = CSELIB_VAL_PTR (loc)->locs; el->next; el = el->next)
325 /* Adjust values that have LOC as canonical so that VAL
326 becomes their canonical. */
327 if (el->loc && GET_CODE (el->loc) == VALUE)
329 gcc_checking_assert (CSELIB_VAL_PTR (el->loc)->locs->loc
330 == loc);
331 CSELIB_VAL_PTR (el->loc)->locs->loc = val->val_rtx;
334 el->next = val->locs;
335 next = val->locs = CSELIB_VAL_PTR (loc)->locs;
338 if (CSELIB_VAL_PTR (loc)->addr_list)
340 /* Bring in addr_list into canonical node. */
341 struct elt_list *last = CSELIB_VAL_PTR (loc)->addr_list;
342 while (last->next)
343 last = last->next;
344 last->next = val->addr_list;
345 val->addr_list = CSELIB_VAL_PTR (loc)->addr_list;
346 CSELIB_VAL_PTR (loc)->addr_list = NULL;
349 if (CSELIB_VAL_PTR (loc)->next_containing_mem != NULL
350 && val->next_containing_mem == NULL)
352 /* Add VAL to the containing_mem list after LOC. LOC will
353 be removed when we notice it doesn't contain any
354 MEMs. */
355 val->next_containing_mem = CSELIB_VAL_PTR (loc)->next_containing_mem;
356 CSELIB_VAL_PTR (loc)->next_containing_mem = val;
359 /* Chain LOC back to VAL. */
360 el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
361 el->loc = val->val_rtx;
362 el->setting_insn = cselib_current_insn;
363 el->next = NULL;
364 CSELIB_VAL_PTR (loc)->locs = el;
367 el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
368 el->loc = loc;
369 el->setting_insn = cselib_current_insn;
370 el->next = next;
371 val->locs = el;
374 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
375 originating from a debug insn, maintaining the debug values
376 count. */
378 static inline void
379 promote_debug_loc (struct elt_loc_list *l)
381 if (l && l->setting_insn && DEBUG_INSN_P (l->setting_insn)
382 && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
384 n_debug_values--;
385 l->setting_insn = cselib_current_insn;
386 if (cselib_preserve_constants && l->next)
388 gcc_assert (l->next->setting_insn
389 && DEBUG_INSN_P (l->next->setting_insn)
390 && !l->next->next);
391 l->next->setting_insn = cselib_current_insn;
393 else
394 gcc_assert (!l->next);
398 /* The elt_list at *PL is no longer needed. Unchain it and free its
399 storage. */
401 static inline void
402 unchain_one_elt_list (struct elt_list **pl)
404 struct elt_list *l = *pl;
406 *pl = l->next;
407 pool_free (elt_list_pool, l);
410 /* Likewise for elt_loc_lists. */
412 static void
413 unchain_one_elt_loc_list (struct elt_loc_list **pl)
415 struct elt_loc_list *l = *pl;
417 *pl = l->next;
418 pool_free (elt_loc_list_pool, l);
421 /* Likewise for cselib_vals. This also frees the addr_list associated with
422 V. */
424 static void
425 unchain_one_value (cselib_val *v)
427 while (v->addr_list)
428 unchain_one_elt_list (&v->addr_list);
430 pool_free (cselib_val_pool, v);
433 /* Remove all entries from the hash table. Also used during
434 initialization. */
436 void
437 cselib_clear_table (void)
439 cselib_reset_table (1);
442 /* Return TRUE if V is a constant, a function invariant or a VALUE
443 equivalence; FALSE otherwise. */
445 static bool
446 invariant_or_equiv_p (cselib_val *v)
448 struct elt_loc_list *l;
450 if (v == cfa_base_preserved_val)
451 return true;
453 /* Keep VALUE equivalences around. */
454 for (l = v->locs; l; l = l->next)
455 if (GET_CODE (l->loc) == VALUE)
456 return true;
458 if (v->locs != NULL
459 && v->locs->next == NULL)
461 if (CONSTANT_P (v->locs->loc)
462 && (GET_CODE (v->locs->loc) != CONST
463 || !references_value_p (v->locs->loc, 0)))
464 return true;
465 /* Although a debug expr may be bound to different expressions,
466 we can preserve it as if it was constant, to get unification
467 and proper merging within var-tracking. */
468 if (GET_CODE (v->locs->loc) == DEBUG_EXPR
469 || GET_CODE (v->locs->loc) == DEBUG_IMPLICIT_PTR
470 || GET_CODE (v->locs->loc) == ENTRY_VALUE
471 || GET_CODE (v->locs->loc) == DEBUG_PARAMETER_REF)
472 return true;
474 /* (plus (value V) (const_int C)) is invariant iff V is invariant. */
475 if (GET_CODE (v->locs->loc) == PLUS
476 && CONST_INT_P (XEXP (v->locs->loc, 1))
477 && GET_CODE (XEXP (v->locs->loc, 0)) == VALUE
478 && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v->locs->loc, 0))))
479 return true;
482 return false;
485 /* Remove from hash table all VALUEs except constants, function
486 invariants and VALUE equivalences. */
489 preserve_constants_and_equivs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
491 cselib_val *v = *x;
493 if (!invariant_or_equiv_p (v))
494 cselib_hash_table.clear_slot (x);
495 return 1;
498 /* Remove all entries from the hash table, arranging for the next
499 value to be numbered NUM. */
501 void
502 cselib_reset_table (unsigned int num)
504 unsigned int i;
506 max_value_regs = 0;
508 if (cfa_base_preserved_val)
510 unsigned int regno = cfa_base_preserved_regno;
511 unsigned int new_used_regs = 0;
512 for (i = 0; i < n_used_regs; i++)
513 if (used_regs[i] == regno)
515 new_used_regs = 1;
516 continue;
518 else
519 REG_VALUES (used_regs[i]) = 0;
520 gcc_assert (new_used_regs == 1);
521 n_used_regs = new_used_regs;
522 used_regs[0] = regno;
523 max_value_regs
524 = hard_regno_nregs[regno][GET_MODE (cfa_base_preserved_val->locs->loc)];
526 else
528 for (i = 0; i < n_used_regs; i++)
529 REG_VALUES (used_regs[i]) = 0;
530 n_used_regs = 0;
533 if (cselib_preserve_constants)
534 cselib_hash_table.traverse <void *, preserve_constants_and_equivs> (NULL);
535 else
537 cselib_hash_table.empty ();
538 gcc_checking_assert (!cselib_any_perm_equivs);
541 n_useless_values = 0;
542 n_useless_debug_values = 0;
543 n_debug_values = 0;
545 next_uid = num;
547 first_containing_mem = &dummy_val;
550 /* Return the number of the next value that will be generated. */
552 unsigned int
553 cselib_get_next_uid (void)
555 return next_uid;
558 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
559 INSERTing if requested. When X is part of the address of a MEM,
560 MEMMODE should specify the mode of the MEM. While searching the
561 table, MEMMODE is held in FIND_SLOT_MEMMODE, so that autoinc RTXs
562 in X can be resolved. */
564 static cselib_val **
565 cselib_find_slot (rtx x, hashval_t hash, enum insert_option insert,
566 enum machine_mode memmode)
568 cselib_val **slot;
569 find_slot_memmode = memmode;
570 slot = cselib_hash_table.find_slot_with_hash (x, hash, insert);
571 find_slot_memmode = VOIDmode;
572 return slot;
575 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
576 only return true for values which point to a cselib_val whose value
577 element has been set to zero, which implies the cselib_val will be
578 removed. */
581 references_value_p (const_rtx x, int only_useless)
583 const enum rtx_code code = GET_CODE (x);
584 const char *fmt = GET_RTX_FORMAT (code);
585 int i, j;
587 if (GET_CODE (x) == VALUE
588 && (! only_useless ||
589 (CSELIB_VAL_PTR (x)->locs == 0 && !PRESERVED_VALUE_P (x))))
590 return 1;
592 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
594 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
595 return 1;
596 else if (fmt[i] == 'E')
597 for (j = 0; j < XVECLEN (x, i); j++)
598 if (references_value_p (XVECEXP (x, i, j), only_useless))
599 return 1;
602 return 0;
605 /* For all locations found in X, delete locations that reference useless
606 values (i.e. values without any location). Called through
607 htab_traverse. */
610 discard_useless_locs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
612 cselib_val *v = *x;
613 struct elt_loc_list **p = &v->locs;
614 bool had_locs = v->locs != NULL;
615 rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
617 while (*p)
619 if (references_value_p ((*p)->loc, 1))
620 unchain_one_elt_loc_list (p);
621 else
622 p = &(*p)->next;
625 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
627 if (setting_insn && DEBUG_INSN_P (setting_insn))
628 n_useless_debug_values++;
629 else
630 n_useless_values++;
631 values_became_useless = 1;
633 return 1;
636 /* If X is a value with no locations, remove it from the hashtable. */
639 discard_useless_values (cselib_val **x, void *info ATTRIBUTE_UNUSED)
641 cselib_val *v = *x;
643 if (v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
645 if (cselib_discard_hook)
646 cselib_discard_hook (v);
648 CSELIB_VAL_PTR (v->val_rtx) = NULL;
649 cselib_hash_table.clear_slot (x);
650 unchain_one_value (v);
651 n_useless_values--;
654 return 1;
657 /* Clean out useless values (i.e. those which no longer have locations
658 associated with them) from the hash table. */
660 static void
661 remove_useless_values (void)
663 cselib_val **p, *v;
665 /* First pass: eliminate locations that reference the value. That in
666 turn can make more values useless. */
669 values_became_useless = 0;
670 cselib_hash_table.traverse <void *, discard_useless_locs> (NULL);
672 while (values_became_useless);
674 /* Second pass: actually remove the values. */
676 p = &first_containing_mem;
677 for (v = *p; v != &dummy_val; v = v->next_containing_mem)
678 if (v->locs && v == canonical_cselib_val (v))
680 *p = v;
681 p = &(*p)->next_containing_mem;
683 *p = &dummy_val;
685 n_useless_values += n_useless_debug_values;
686 n_debug_values -= n_useless_debug_values;
687 n_useless_debug_values = 0;
689 cselib_hash_table.traverse <void *, discard_useless_values> (NULL);
691 gcc_assert (!n_useless_values);
694 /* Arrange for a value to not be removed from the hash table even if
695 it becomes useless. */
697 void
698 cselib_preserve_value (cselib_val *v)
700 PRESERVED_VALUE_P (v->val_rtx) = 1;
703 /* Test whether a value is preserved. */
705 bool
706 cselib_preserved_value_p (cselib_val *v)
708 return PRESERVED_VALUE_P (v->val_rtx);
711 /* Arrange for a REG value to be assumed constant through the whole function,
712 never invalidated and preserved across cselib_reset_table calls. */
714 void
715 cselib_preserve_cfa_base_value (cselib_val *v, unsigned int regno)
717 if (cselib_preserve_constants
718 && v->locs
719 && REG_P (v->locs->loc))
721 cfa_base_preserved_val = v;
722 cfa_base_preserved_regno = regno;
726 /* Clean all non-constant expressions in the hash table, but retain
727 their values. */
729 void
730 cselib_preserve_only_values (void)
732 int i;
734 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
735 cselib_invalidate_regno (i, reg_raw_mode[i]);
737 cselib_invalidate_mem (callmem);
739 remove_useless_values ();
741 gcc_assert (first_containing_mem == &dummy_val);
744 /* Arrange for a value to be marked as based on stack pointer
745 for find_base_term purposes. */
747 void
748 cselib_set_value_sp_based (cselib_val *v)
750 SP_BASED_VALUE_P (v->val_rtx) = 1;
753 /* Test whether a value is based on stack pointer for
754 find_base_term purposes. */
756 bool
757 cselib_sp_based_value_p (cselib_val *v)
759 return SP_BASED_VALUE_P (v->val_rtx);
762 /* Return the mode in which a register was last set. If X is not a
763 register, return its mode. If the mode in which the register was
764 set is not known, or the value was already clobbered, return
765 VOIDmode. */
767 enum machine_mode
768 cselib_reg_set_mode (const_rtx x)
770 if (!REG_P (x))
771 return GET_MODE (x);
773 if (REG_VALUES (REGNO (x)) == NULL
774 || REG_VALUES (REGNO (x))->elt == NULL)
775 return VOIDmode;
777 return GET_MODE (REG_VALUES (REGNO (x))->elt->val_rtx);
780 /* Return nonzero if we can prove that X and Y contain the same value, taking
781 our gathered information into account. */
784 rtx_equal_for_cselib_p (rtx x, rtx y)
786 return rtx_equal_for_cselib_1 (x, y, VOIDmode);
789 /* If x is a PLUS or an autoinc operation, expand the operation,
790 storing the offset, if any, in *OFF. */
792 static rtx
793 autoinc_split (rtx x, rtx *off, enum machine_mode memmode)
795 switch (GET_CODE (x))
797 case PLUS:
798 *off = XEXP (x, 1);
799 return XEXP (x, 0);
801 case PRE_DEC:
802 if (memmode == VOIDmode)
803 return x;
805 *off = GEN_INT (-GET_MODE_SIZE (memmode));
806 return XEXP (x, 0);
807 break;
809 case PRE_INC:
810 if (memmode == VOIDmode)
811 return x;
813 *off = GEN_INT (GET_MODE_SIZE (memmode));
814 return XEXP (x, 0);
816 case PRE_MODIFY:
817 return XEXP (x, 1);
819 case POST_DEC:
820 case POST_INC:
821 case POST_MODIFY:
822 return XEXP (x, 0);
824 default:
825 return x;
829 /* Return nonzero if we can prove that X and Y contain the same value,
830 taking our gathered information into account. MEMMODE holds the
831 mode of the enclosing MEM, if any, as required to deal with autoinc
832 addressing modes. If X and Y are not (known to be) part of
833 addresses, MEMMODE should be VOIDmode. */
835 static int
836 rtx_equal_for_cselib_1 (rtx x, rtx y, enum machine_mode memmode)
838 enum rtx_code code;
839 const char *fmt;
840 int i;
842 if (REG_P (x) || MEM_P (x))
844 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0, memmode);
846 if (e)
847 x = e->val_rtx;
850 if (REG_P (y) || MEM_P (y))
852 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0, memmode);
854 if (e)
855 y = e->val_rtx;
858 if (x == y)
859 return 1;
861 if (GET_CODE (x) == VALUE)
863 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (x));
864 struct elt_loc_list *l;
866 if (GET_CODE (y) == VALUE)
867 return e == canonical_cselib_val (CSELIB_VAL_PTR (y));
869 for (l = e->locs; l; l = l->next)
871 rtx t = l->loc;
873 /* Avoid infinite recursion. We know we have the canonical
874 value, so we can just skip any values in the equivalence
875 list. */
876 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
877 continue;
878 else if (rtx_equal_for_cselib_1 (t, y, memmode))
879 return 1;
882 return 0;
884 else if (GET_CODE (y) == VALUE)
886 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (y));
887 struct elt_loc_list *l;
889 for (l = e->locs; l; l = l->next)
891 rtx t = l->loc;
893 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
894 continue;
895 else if (rtx_equal_for_cselib_1 (x, t, memmode))
896 return 1;
899 return 0;
902 if (GET_MODE (x) != GET_MODE (y))
903 return 0;
905 if (GET_CODE (x) != GET_CODE (y))
907 rtx xorig = x, yorig = y;
908 rtx xoff = NULL, yoff = NULL;
910 x = autoinc_split (x, &xoff, memmode);
911 y = autoinc_split (y, &yoff, memmode);
913 if (!xoff != !yoff)
914 return 0;
916 if (xoff && !rtx_equal_for_cselib_1 (xoff, yoff, memmode))
917 return 0;
919 /* Don't recurse if nothing changed. */
920 if (x != xorig || y != yorig)
921 return rtx_equal_for_cselib_1 (x, y, memmode);
923 return 0;
926 /* These won't be handled correctly by the code below. */
927 switch (GET_CODE (x))
929 case CONST_DOUBLE:
930 case CONST_FIXED:
931 case DEBUG_EXPR:
932 return 0;
934 case DEBUG_IMPLICIT_PTR:
935 return DEBUG_IMPLICIT_PTR_DECL (x)
936 == DEBUG_IMPLICIT_PTR_DECL (y);
938 case DEBUG_PARAMETER_REF:
939 return DEBUG_PARAMETER_REF_DECL (x)
940 == DEBUG_PARAMETER_REF_DECL (y);
942 case ENTRY_VALUE:
943 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
944 use rtx_equal_for_cselib_1 to compare the operands. */
945 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
947 case LABEL_REF:
948 return XEXP (x, 0) == XEXP (y, 0);
950 case MEM:
951 /* We have to compare any autoinc operations in the addresses
952 using this MEM's mode. */
953 return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x));
955 default:
956 break;
959 code = GET_CODE (x);
960 fmt = GET_RTX_FORMAT (code);
962 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
964 int j;
966 switch (fmt[i])
968 case 'w':
969 if (XWINT (x, i) != XWINT (y, i))
970 return 0;
971 break;
973 case 'n':
974 case 'i':
975 if (XINT (x, i) != XINT (y, i))
976 return 0;
977 break;
979 case 'V':
980 case 'E':
981 /* Two vectors must have the same length. */
982 if (XVECLEN (x, i) != XVECLEN (y, i))
983 return 0;
985 /* And the corresponding elements must match. */
986 for (j = 0; j < XVECLEN (x, i); j++)
987 if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
988 XVECEXP (y, i, j), memmode))
989 return 0;
990 break;
992 case 'e':
993 if (i == 1
994 && targetm.commutative_p (x, UNKNOWN)
995 && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode)
996 && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode))
997 return 1;
998 if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode))
999 return 0;
1000 break;
1002 case 'S':
1003 case 's':
1004 if (strcmp (XSTR (x, i), XSTR (y, i)))
1005 return 0;
1006 break;
1008 case 'u':
1009 /* These are just backpointers, so they don't matter. */
1010 break;
1012 case '0':
1013 case 't':
1014 break;
1016 /* It is believed that rtx's at this level will never
1017 contain anything but integers and other rtx's,
1018 except for within LABEL_REFs and SYMBOL_REFs. */
1019 default:
1020 gcc_unreachable ();
1023 return 1;
1026 /* We need to pass down the mode of constants through the hash table
1027 functions. For that purpose, wrap them in a CONST of the appropriate
1028 mode. */
1029 static rtx
1030 wrap_constant (enum machine_mode mode, rtx x)
1032 if (!CONST_SCALAR_INT_P (x) && GET_CODE (x) != CONST_FIXED)
1033 return x;
1034 gcc_assert (mode != VOIDmode);
1035 return gen_rtx_CONST (mode, x);
1038 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1039 For registers and memory locations, we look up their cselib_val structure
1040 and return its VALUE element.
1041 Possible reasons for return 0 are: the object is volatile, or we couldn't
1042 find a register or memory location in the table and CREATE is zero. If
1043 CREATE is nonzero, table elts are created for regs and mem.
1044 N.B. this hash function returns the same hash value for RTXes that
1045 differ only in the order of operands, thus it is suitable for comparisons
1046 that take commutativity into account.
1047 If we wanted to also support associative rules, we'd have to use a different
1048 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1049 MEMMODE indicates the mode of an enclosing MEM, and it's only
1050 used to compute autoinc values.
1051 We used to have a MODE argument for hashing for CONST_INTs, but that
1052 didn't make sense, since it caused spurious hash differences between
1053 (set (reg:SI 1) (const_int))
1054 (plus:SI (reg:SI 2) (reg:SI 1))
1056 (plus:SI (reg:SI 2) (const_int))
1057 If the mode is important in any context, it must be checked specifically
1058 in a comparison anyway, since relying on hash differences is unsafe. */
1060 static unsigned int
1061 cselib_hash_rtx (rtx x, int create, enum machine_mode memmode)
1063 cselib_val *e;
1064 int i, j;
1065 enum rtx_code code;
1066 const char *fmt;
1067 unsigned int hash = 0;
1069 code = GET_CODE (x);
1070 hash += (unsigned) code + (unsigned) GET_MODE (x);
1072 switch (code)
1074 case VALUE:
1075 e = CSELIB_VAL_PTR (x);
1076 return e->hash;
1078 case MEM:
1079 case REG:
1080 e = cselib_lookup (x, GET_MODE (x), create, memmode);
1081 if (! e)
1082 return 0;
1084 return e->hash;
1086 case DEBUG_EXPR:
1087 hash += ((unsigned) DEBUG_EXPR << 7)
1088 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
1089 return hash ? hash : (unsigned int) DEBUG_EXPR;
1091 case DEBUG_IMPLICIT_PTR:
1092 hash += ((unsigned) DEBUG_IMPLICIT_PTR << 7)
1093 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x));
1094 return hash ? hash : (unsigned int) DEBUG_IMPLICIT_PTR;
1096 case DEBUG_PARAMETER_REF:
1097 hash += ((unsigned) DEBUG_PARAMETER_REF << 7)
1098 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x));
1099 return hash ? hash : (unsigned int) DEBUG_PARAMETER_REF;
1101 case ENTRY_VALUE:
1102 /* ENTRY_VALUEs are function invariant, thus try to avoid
1103 recursing on argument if ENTRY_VALUE is one of the
1104 forms emitted by expand_debug_expr, otherwise
1105 ENTRY_VALUE hash would depend on the current value
1106 in some register or memory. */
1107 if (REG_P (ENTRY_VALUE_EXP (x)))
1108 hash += (unsigned int) REG
1109 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x))
1110 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x));
1111 else if (MEM_P (ENTRY_VALUE_EXP (x))
1112 && REG_P (XEXP (ENTRY_VALUE_EXP (x), 0)))
1113 hash += (unsigned int) MEM
1114 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x), 0))
1115 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x), 0));
1116 else
1117 hash += cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode);
1118 return hash ? hash : (unsigned int) ENTRY_VALUE;
1120 case CONST_INT:
1121 hash += ((unsigned) CONST_INT << 7) + INTVAL (x);
1122 return hash ? hash : (unsigned int) CONST_INT;
1124 case CONST_DOUBLE:
1125 /* This is like the general case, except that it only counts
1126 the integers representing the constant. */
1127 hash += (unsigned) code + (unsigned) GET_MODE (x);
1128 if (GET_MODE (x) != VOIDmode)
1129 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
1130 else
1131 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1132 + (unsigned) CONST_DOUBLE_HIGH (x));
1133 return hash ? hash : (unsigned int) CONST_DOUBLE;
1135 case CONST_FIXED:
1136 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1137 hash += fixed_hash (CONST_FIXED_VALUE (x));
1138 return hash ? hash : (unsigned int) CONST_FIXED;
1140 case CONST_VECTOR:
1142 int units;
1143 rtx elt;
1145 units = CONST_VECTOR_NUNITS (x);
1147 for (i = 0; i < units; ++i)
1149 elt = CONST_VECTOR_ELT (x, i);
1150 hash += cselib_hash_rtx (elt, 0, memmode);
1153 return hash;
1156 /* Assume there is only one rtx object for any given label. */
1157 case LABEL_REF:
1158 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1159 differences and differences between each stage's debugging dumps. */
1160 hash += (((unsigned int) LABEL_REF << 7)
1161 + CODE_LABEL_NUMBER (XEXP (x, 0)));
1162 return hash ? hash : (unsigned int) LABEL_REF;
1164 case SYMBOL_REF:
1166 /* Don't hash on the symbol's address to avoid bootstrap differences.
1167 Different hash values may cause expressions to be recorded in
1168 different orders and thus different registers to be used in the
1169 final assembler. This also avoids differences in the dump files
1170 between various stages. */
1171 unsigned int h = 0;
1172 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1174 while (*p)
1175 h += (h << 7) + *p++; /* ??? revisit */
1177 hash += ((unsigned int) SYMBOL_REF << 7) + h;
1178 return hash ? hash : (unsigned int) SYMBOL_REF;
1181 case PRE_DEC:
1182 case PRE_INC:
1183 /* We can't compute these without knowing the MEM mode. */
1184 gcc_assert (memmode != VOIDmode);
1185 i = GET_MODE_SIZE (memmode);
1186 if (code == PRE_DEC)
1187 i = -i;
1188 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1189 like (mem:MEMMODE (plus (reg) (const_int I))). */
1190 hash += (unsigned) PLUS - (unsigned)code
1191 + cselib_hash_rtx (XEXP (x, 0), create, memmode)
1192 + cselib_hash_rtx (GEN_INT (i), create, memmode);
1193 return hash ? hash : 1 + (unsigned) PLUS;
1195 case PRE_MODIFY:
1196 gcc_assert (memmode != VOIDmode);
1197 return cselib_hash_rtx (XEXP (x, 1), create, memmode);
1199 case POST_DEC:
1200 case POST_INC:
1201 case POST_MODIFY:
1202 gcc_assert (memmode != VOIDmode);
1203 return cselib_hash_rtx (XEXP (x, 0), create, memmode);
1205 case PC:
1206 case CC0:
1207 case CALL:
1208 case UNSPEC_VOLATILE:
1209 return 0;
1211 case ASM_OPERANDS:
1212 if (MEM_VOLATILE_P (x))
1213 return 0;
1215 break;
1217 default:
1218 break;
1221 i = GET_RTX_LENGTH (code) - 1;
1222 fmt = GET_RTX_FORMAT (code);
1223 for (; i >= 0; i--)
1225 switch (fmt[i])
1227 case 'e':
1229 rtx tem = XEXP (x, i);
1230 unsigned int tem_hash = cselib_hash_rtx (tem, create, memmode);
1232 if (tem_hash == 0)
1233 return 0;
1235 hash += tem_hash;
1237 break;
1238 case 'E':
1239 for (j = 0; j < XVECLEN (x, i); j++)
1241 unsigned int tem_hash
1242 = cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
1244 if (tem_hash == 0)
1245 return 0;
1247 hash += tem_hash;
1249 break;
1251 case 's':
1253 const unsigned char *p = (const unsigned char *) XSTR (x, i);
1255 if (p)
1256 while (*p)
1257 hash += *p++;
1258 break;
1261 case 'i':
1262 hash += XINT (x, i);
1263 break;
1265 case '0':
1266 case 't':
1267 /* unused */
1268 break;
1270 default:
1271 gcc_unreachable ();
1275 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
1278 /* Create a new value structure for VALUE and initialize it. The mode of the
1279 value is MODE. */
1281 static inline cselib_val *
1282 new_cselib_val (unsigned int hash, enum machine_mode mode, rtx x)
1284 cselib_val *e = (cselib_val *) pool_alloc (cselib_val_pool);
1286 gcc_assert (hash);
1287 gcc_assert (next_uid);
1289 e->hash = hash;
1290 e->uid = next_uid++;
1291 /* We use an alloc pool to allocate this RTL construct because it
1292 accounts for about 8% of the overall memory usage. We know
1293 precisely when we can have VALUE RTXen (when cselib is active)
1294 so we don't need to put them in garbage collected memory.
1295 ??? Why should a VALUE be an RTX in the first place? */
1296 e->val_rtx = (rtx) pool_alloc (value_pool);
1297 memset (e->val_rtx, 0, RTX_HDR_SIZE);
1298 PUT_CODE (e->val_rtx, VALUE);
1299 PUT_MODE (e->val_rtx, mode);
1300 CSELIB_VAL_PTR (e->val_rtx) = e;
1301 e->addr_list = 0;
1302 e->locs = 0;
1303 e->next_containing_mem = 0;
1305 if (dump_file && (dump_flags & TDF_CSELIB))
1307 fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
1308 if (flag_dump_noaddr || flag_dump_unnumbered)
1309 fputs ("# ", dump_file);
1310 else
1311 fprintf (dump_file, "%p ", (void*)e);
1312 print_rtl_single (dump_file, x);
1313 fputc ('\n', dump_file);
1316 return e;
1319 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1320 contains the data at this address. X is a MEM that represents the
1321 value. Update the two value structures to represent this situation. */
1323 static void
1324 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
1326 struct elt_loc_list *l;
1328 addr_elt = canonical_cselib_val (addr_elt);
1329 mem_elt = canonical_cselib_val (mem_elt);
1331 /* Avoid duplicates. */
1332 for (l = mem_elt->locs; l; l = l->next)
1333 if (MEM_P (l->loc)
1334 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
1336 promote_debug_loc (l);
1337 return;
1340 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
1341 new_elt_loc_list (mem_elt,
1342 replace_equiv_address_nv (x, addr_elt->val_rtx));
1343 if (mem_elt->next_containing_mem == NULL)
1345 mem_elt->next_containing_mem = first_containing_mem;
1346 first_containing_mem = mem_elt;
1350 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1351 If CREATE, make a new one if we haven't seen it before. */
1353 static cselib_val *
1354 cselib_lookup_mem (rtx x, int create)
1356 enum machine_mode mode = GET_MODE (x);
1357 enum machine_mode addr_mode;
1358 cselib_val **slot;
1359 cselib_val *addr;
1360 cselib_val *mem_elt;
1361 struct elt_list *l;
1363 if (MEM_VOLATILE_P (x) || mode == BLKmode
1364 || !cselib_record_memory
1365 || (FLOAT_MODE_P (mode) && flag_float_store))
1366 return 0;
1368 addr_mode = GET_MODE (XEXP (x, 0));
1369 if (addr_mode == VOIDmode)
1370 addr_mode = Pmode;
1372 /* Look up the value for the address. */
1373 addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
1374 if (! addr)
1375 return 0;
1377 addr = canonical_cselib_val (addr);
1378 /* Find a value that describes a value of our mode at that address. */
1379 for (l = addr->addr_list; l; l = l->next)
1380 if (GET_MODE (l->elt->val_rtx) == mode)
1382 promote_debug_loc (l->elt->locs);
1383 return l->elt;
1386 if (! create)
1387 return 0;
1389 mem_elt = new_cselib_val (next_uid, mode, x);
1390 add_mem_for_addr (addr, mem_elt, x);
1391 slot = cselib_find_slot (wrap_constant (mode, x), mem_elt->hash,
1392 INSERT, mode);
1393 *slot = mem_elt;
1394 return mem_elt;
1397 /* Search through the possible substitutions in P. We prefer a non reg
1398 substitution because this allows us to expand the tree further. If
1399 we find, just a reg, take the lowest regno. There may be several
1400 non-reg results, we just take the first one because they will all
1401 expand to the same place. */
1403 static rtx
1404 expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1405 int max_depth)
1407 rtx reg_result = NULL;
1408 unsigned int regno = UINT_MAX;
1409 struct elt_loc_list *p_in = p;
1411 for (; p; p = p->next)
1413 /* Return these right away to avoid returning stack pointer based
1414 expressions for frame pointer and vice versa, which is something
1415 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1416 for more details. */
1417 if (REG_P (p->loc)
1418 && (REGNO (p->loc) == STACK_POINTER_REGNUM
1419 || REGNO (p->loc) == FRAME_POINTER_REGNUM
1420 || REGNO (p->loc) == HARD_FRAME_POINTER_REGNUM
1421 || REGNO (p->loc) == cfa_base_preserved_regno))
1422 return p->loc;
1423 /* Avoid infinite recursion trying to expand a reg into a
1424 the same reg. */
1425 if ((REG_P (p->loc))
1426 && (REGNO (p->loc) < regno)
1427 && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
1429 reg_result = p->loc;
1430 regno = REGNO (p->loc);
1432 /* Avoid infinite recursion and do not try to expand the
1433 value. */
1434 else if (GET_CODE (p->loc) == VALUE
1435 && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1436 continue;
1437 else if (!REG_P (p->loc))
1439 rtx result, note;
1440 if (dump_file && (dump_flags & TDF_CSELIB))
1442 print_inline_rtx (dump_file, p->loc, 0);
1443 fprintf (dump_file, "\n");
1445 if (GET_CODE (p->loc) == LO_SUM
1446 && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1447 && p->setting_insn
1448 && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1449 && XEXP (note, 0) == XEXP (p->loc, 1))
1450 return XEXP (p->loc, 1);
1451 result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
1452 if (result)
1453 return result;
1458 if (regno != UINT_MAX)
1460 rtx result;
1461 if (dump_file && (dump_flags & TDF_CSELIB))
1462 fprintf (dump_file, "r%d\n", regno);
1464 result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
1465 if (result)
1466 return result;
1469 if (dump_file && (dump_flags & TDF_CSELIB))
1471 if (reg_result)
1473 print_inline_rtx (dump_file, reg_result, 0);
1474 fprintf (dump_file, "\n");
1476 else
1477 fprintf (dump_file, "NULL\n");
1479 return reg_result;
1483 /* Forward substitute and expand an expression out to its roots.
1484 This is the opposite of common subexpression. Because local value
1485 numbering is such a weak optimization, the expanded expression is
1486 pretty much unique (not from a pointer equals point of view but
1487 from a tree shape point of view.
1489 This function returns NULL if the expansion fails. The expansion
1490 will fail if there is no value number for one of the operands or if
1491 one of the operands has been overwritten between the current insn
1492 and the beginning of the basic block. For instance x has no
1493 expansion in:
1495 r1 <- r1 + 3
1496 x <- r1 + 8
1498 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1499 It is clear on return. */
1502 cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
1504 struct expand_value_data evd;
1506 evd.regs_active = regs_active;
1507 evd.callback = NULL;
1508 evd.callback_arg = NULL;
1509 evd.dummy = false;
1511 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1514 /* Same as cselib_expand_value_rtx, but using a callback to try to
1515 resolve some expressions. The CB function should return ORIG if it
1516 can't or does not want to deal with a certain RTX. Any other
1517 return value, including NULL, will be used as the expansion for
1518 VALUE, without any further changes. */
1521 cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1522 cselib_expand_callback cb, void *data)
1524 struct expand_value_data evd;
1526 evd.regs_active = regs_active;
1527 evd.callback = cb;
1528 evd.callback_arg = data;
1529 evd.dummy = false;
1531 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1534 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1535 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1536 would return NULL or non-NULL, without allocating new rtx. */
1538 bool
1539 cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1540 cselib_expand_callback cb, void *data)
1542 struct expand_value_data evd;
1544 evd.regs_active = regs_active;
1545 evd.callback = cb;
1546 evd.callback_arg = data;
1547 evd.dummy = true;
1549 return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1552 /* Internal implementation of cselib_expand_value_rtx and
1553 cselib_expand_value_rtx_cb. */
1555 static rtx
1556 cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1557 int max_depth)
1559 rtx copy, scopy;
1560 int i, j;
1561 RTX_CODE code;
1562 const char *format_ptr;
1563 enum machine_mode mode;
1565 code = GET_CODE (orig);
1567 /* For the context of dse, if we end up expand into a huge tree, we
1568 will not have a useful address, so we might as well just give up
1569 quickly. */
1570 if (max_depth <= 0)
1571 return NULL;
1573 switch (code)
1575 case REG:
1577 struct elt_list *l = REG_VALUES (REGNO (orig));
1579 if (l && l->elt == NULL)
1580 l = l->next;
1581 for (; l; l = l->next)
1582 if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1584 rtx result;
1585 unsigned regno = REGNO (orig);
1587 /* The only thing that we are not willing to do (this
1588 is requirement of dse and if others potential uses
1589 need this function we should add a parm to control
1590 it) is that we will not substitute the
1591 STACK_POINTER_REGNUM, FRAME_POINTER or the
1592 HARD_FRAME_POINTER.
1594 These expansions confuses the code that notices that
1595 stores into the frame go dead at the end of the
1596 function and that the frame is not effected by calls
1597 to subroutines. If you allow the
1598 STACK_POINTER_REGNUM substitution, then dse will
1599 think that parameter pushing also goes dead which is
1600 wrong. If you allow the FRAME_POINTER or the
1601 HARD_FRAME_POINTER then you lose the opportunity to
1602 make the frame assumptions. */
1603 if (regno == STACK_POINTER_REGNUM
1604 || regno == FRAME_POINTER_REGNUM
1605 || regno == HARD_FRAME_POINTER_REGNUM
1606 || regno == cfa_base_preserved_regno)
1607 return orig;
1609 bitmap_set_bit (evd->regs_active, regno);
1611 if (dump_file && (dump_flags & TDF_CSELIB))
1612 fprintf (dump_file, "expanding: r%d into: ", regno);
1614 result = expand_loc (l->elt->locs, evd, max_depth);
1615 bitmap_clear_bit (evd->regs_active, regno);
1617 if (result)
1618 return result;
1619 else
1620 return orig;
1624 CASE_CONST_ANY:
1625 case SYMBOL_REF:
1626 case CODE_LABEL:
1627 case PC:
1628 case CC0:
1629 case SCRATCH:
1630 /* SCRATCH must be shared because they represent distinct values. */
1631 return orig;
1632 case CLOBBER:
1633 if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1634 return orig;
1635 break;
1637 case CONST:
1638 if (shared_const_p (orig))
1639 return orig;
1640 break;
1642 case SUBREG:
1644 rtx subreg;
1646 if (evd->callback)
1648 subreg = evd->callback (orig, evd->regs_active, max_depth,
1649 evd->callback_arg);
1650 if (subreg != orig)
1651 return subreg;
1654 subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1655 max_depth - 1);
1656 if (!subreg)
1657 return NULL;
1658 scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1659 GET_MODE (SUBREG_REG (orig)),
1660 SUBREG_BYTE (orig));
1661 if (scopy == NULL
1662 || (GET_CODE (scopy) == SUBREG
1663 && !REG_P (SUBREG_REG (scopy))
1664 && !MEM_P (SUBREG_REG (scopy))))
1665 return NULL;
1667 return scopy;
1670 case VALUE:
1672 rtx result;
1674 if (dump_file && (dump_flags & TDF_CSELIB))
1676 fputs ("\nexpanding ", dump_file);
1677 print_rtl_single (dump_file, orig);
1678 fputs (" into...", dump_file);
1681 if (evd->callback)
1683 result = evd->callback (orig, evd->regs_active, max_depth,
1684 evd->callback_arg);
1686 if (result != orig)
1687 return result;
1690 result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
1691 return result;
1694 case DEBUG_EXPR:
1695 if (evd->callback)
1696 return evd->callback (orig, evd->regs_active, max_depth,
1697 evd->callback_arg);
1698 return orig;
1700 default:
1701 break;
1704 /* Copy the various flags, fields, and other information. We assume
1705 that all fields need copying, and then clear the fields that should
1706 not be copied. That is the sensible default behavior, and forces
1707 us to explicitly document why we are *not* copying a flag. */
1708 if (evd->dummy)
1709 copy = NULL;
1710 else
1711 copy = shallow_copy_rtx (orig);
1713 format_ptr = GET_RTX_FORMAT (code);
1715 for (i = 0; i < GET_RTX_LENGTH (code); i++)
1716 switch (*format_ptr++)
1718 case 'e':
1719 if (XEXP (orig, i) != NULL)
1721 rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1722 max_depth - 1);
1723 if (!result)
1724 return NULL;
1725 if (copy)
1726 XEXP (copy, i) = result;
1728 break;
1730 case 'E':
1731 case 'V':
1732 if (XVEC (orig, i) != NULL)
1734 if (copy)
1735 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1736 for (j = 0; j < XVECLEN (orig, i); j++)
1738 rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1739 evd, max_depth - 1);
1740 if (!result)
1741 return NULL;
1742 if (copy)
1743 XVECEXP (copy, i, j) = result;
1746 break;
1748 case 't':
1749 case 'w':
1750 case 'i':
1751 case 's':
1752 case 'S':
1753 case 'T':
1754 case 'u':
1755 case 'B':
1756 case '0':
1757 /* These are left unchanged. */
1758 break;
1760 default:
1761 gcc_unreachable ();
1764 if (evd->dummy)
1765 return orig;
1767 mode = GET_MODE (copy);
1768 /* If an operand has been simplified into CONST_INT, which doesn't
1769 have a mode and the mode isn't derivable from whole rtx's mode,
1770 try simplify_*_operation first with mode from original's operand
1771 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1772 scopy = copy;
1773 switch (GET_RTX_CLASS (code))
1775 case RTX_UNARY:
1776 if (CONST_INT_P (XEXP (copy, 0))
1777 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1779 scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1780 GET_MODE (XEXP (orig, 0)));
1781 if (scopy)
1782 return scopy;
1784 break;
1785 case RTX_COMM_ARITH:
1786 case RTX_BIN_ARITH:
1787 /* These expressions can derive operand modes from the whole rtx's mode. */
1788 break;
1789 case RTX_TERNARY:
1790 case RTX_BITFIELD_OPS:
1791 if (CONST_INT_P (XEXP (copy, 0))
1792 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1794 scopy = simplify_ternary_operation (code, mode,
1795 GET_MODE (XEXP (orig, 0)),
1796 XEXP (copy, 0), XEXP (copy, 1),
1797 XEXP (copy, 2));
1798 if (scopy)
1799 return scopy;
1801 break;
1802 case RTX_COMPARE:
1803 case RTX_COMM_COMPARE:
1804 if (CONST_INT_P (XEXP (copy, 0))
1805 && GET_MODE (XEXP (copy, 1)) == VOIDmode
1806 && (GET_MODE (XEXP (orig, 0)) != VOIDmode
1807 || GET_MODE (XEXP (orig, 1)) != VOIDmode))
1809 scopy = simplify_relational_operation (code, mode,
1810 (GET_MODE (XEXP (orig, 0))
1811 != VOIDmode)
1812 ? GET_MODE (XEXP (orig, 0))
1813 : GET_MODE (XEXP (orig, 1)),
1814 XEXP (copy, 0),
1815 XEXP (copy, 1));
1816 if (scopy)
1817 return scopy;
1819 break;
1820 default:
1821 break;
1823 scopy = simplify_rtx (copy);
1824 if (scopy)
1825 return scopy;
1826 return copy;
1829 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1830 with VALUE expressions. This way, it becomes independent of changes
1831 to registers and memory.
1832 X isn't actually modified; if modifications are needed, new rtl is
1833 allocated. However, the return value can share rtl with X.
1834 If X is within a MEM, MEMMODE must be the mode of the MEM. */
1837 cselib_subst_to_values (rtx x, enum machine_mode memmode)
1839 enum rtx_code code = GET_CODE (x);
1840 const char *fmt = GET_RTX_FORMAT (code);
1841 cselib_val *e;
1842 struct elt_list *l;
1843 rtx copy = x;
1844 int i;
1846 switch (code)
1848 case REG:
1849 l = REG_VALUES (REGNO (x));
1850 if (l && l->elt == NULL)
1851 l = l->next;
1852 for (; l; l = l->next)
1853 if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
1854 return l->elt->val_rtx;
1856 gcc_unreachable ();
1858 case MEM:
1859 e = cselib_lookup_mem (x, 0);
1860 /* This used to happen for autoincrements, but we deal with them
1861 properly now. Remove the if stmt for the next release. */
1862 if (! e)
1864 /* Assign a value that doesn't match any other. */
1865 e = new_cselib_val (next_uid, GET_MODE (x), x);
1867 return e->val_rtx;
1869 case ENTRY_VALUE:
1870 e = cselib_lookup (x, GET_MODE (x), 0, memmode);
1871 if (! e)
1872 break;
1873 return e->val_rtx;
1875 CASE_CONST_ANY:
1876 return x;
1878 case PRE_DEC:
1879 case PRE_INC:
1880 gcc_assert (memmode != VOIDmode);
1881 i = GET_MODE_SIZE (memmode);
1882 if (code == PRE_DEC)
1883 i = -i;
1884 return cselib_subst_to_values (plus_constant (GET_MODE (x),
1885 XEXP (x, 0), i),
1886 memmode);
1888 case PRE_MODIFY:
1889 gcc_assert (memmode != VOIDmode);
1890 return cselib_subst_to_values (XEXP (x, 1), memmode);
1892 case POST_DEC:
1893 case POST_INC:
1894 case POST_MODIFY:
1895 gcc_assert (memmode != VOIDmode);
1896 return cselib_subst_to_values (XEXP (x, 0), memmode);
1898 default:
1899 break;
1902 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1904 if (fmt[i] == 'e')
1906 rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
1908 if (t != XEXP (x, i))
1910 if (x == copy)
1911 copy = shallow_copy_rtx (x);
1912 XEXP (copy, i) = t;
1915 else if (fmt[i] == 'E')
1917 int j;
1919 for (j = 0; j < XVECLEN (x, i); j++)
1921 rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
1923 if (t != XVECEXP (x, i, j))
1925 if (XVEC (x, i) == XVEC (copy, i))
1927 if (x == copy)
1928 copy = shallow_copy_rtx (x);
1929 XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
1931 XVECEXP (copy, i, j) = t;
1937 return copy;
1940 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
1943 cselib_subst_to_values_from_insn (rtx x, enum machine_mode memmode, rtx insn)
1945 rtx ret;
1946 gcc_assert (!cselib_current_insn);
1947 cselib_current_insn = insn;
1948 ret = cselib_subst_to_values (x, memmode);
1949 cselib_current_insn = NULL;
1950 return ret;
1953 /* Look up the rtl expression X in our tables and return the value it
1954 has. If CREATE is zero, we return NULL if we don't know the value.
1955 Otherwise, we create a new one if possible, using mode MODE if X
1956 doesn't have a mode (i.e. because it's a constant). When X is part
1957 of an address, MEMMODE should be the mode of the enclosing MEM if
1958 we're tracking autoinc expressions. */
1960 static cselib_val *
1961 cselib_lookup_1 (rtx x, enum machine_mode mode,
1962 int create, enum machine_mode memmode)
1964 cselib_val **slot;
1965 cselib_val *e;
1966 unsigned int hashval;
1968 if (GET_MODE (x) != VOIDmode)
1969 mode = GET_MODE (x);
1971 if (GET_CODE (x) == VALUE)
1972 return CSELIB_VAL_PTR (x);
1974 if (REG_P (x))
1976 struct elt_list *l;
1977 unsigned int i = REGNO (x);
1979 l = REG_VALUES (i);
1980 if (l && l->elt == NULL)
1981 l = l->next;
1982 for (; l; l = l->next)
1983 if (mode == GET_MODE (l->elt->val_rtx))
1985 promote_debug_loc (l->elt->locs);
1986 return l->elt;
1989 if (! create)
1990 return 0;
1992 if (i < FIRST_PSEUDO_REGISTER)
1994 unsigned int n = hard_regno_nregs[i][mode];
1996 if (n > max_value_regs)
1997 max_value_regs = n;
2000 e = new_cselib_val (next_uid, GET_MODE (x), x);
2001 new_elt_loc_list (e, x);
2002 if (REG_VALUES (i) == 0)
2004 /* Maintain the invariant that the first entry of
2005 REG_VALUES, if present, must be the value used to set the
2006 register, or NULL. */
2007 used_regs[n_used_regs++] = i;
2008 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
2010 else if (cselib_preserve_constants
2011 && GET_MODE_CLASS (mode) == MODE_INT)
2013 /* During var-tracking, try harder to find equivalences
2014 for SUBREGs. If a setter sets say a DImode register
2015 and user uses that register only in SImode, add a lowpart
2016 subreg location. */
2017 struct elt_list *lwider = NULL;
2018 l = REG_VALUES (i);
2019 if (l && l->elt == NULL)
2020 l = l->next;
2021 for (; l; l = l->next)
2022 if (GET_MODE_CLASS (GET_MODE (l->elt->val_rtx)) == MODE_INT
2023 && GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2024 > GET_MODE_SIZE (mode)
2025 && (lwider == NULL
2026 || GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2027 < GET_MODE_SIZE (GET_MODE (lwider->elt->val_rtx))))
2029 struct elt_loc_list *el;
2030 if (i < FIRST_PSEUDO_REGISTER
2031 && hard_regno_nregs[i][GET_MODE (l->elt->val_rtx)] != 1)
2032 continue;
2033 for (el = l->elt->locs; el; el = el->next)
2034 if (!REG_P (el->loc))
2035 break;
2036 if (el)
2037 lwider = l;
2039 if (lwider)
2041 rtx sub = lowpart_subreg (mode, lwider->elt->val_rtx,
2042 GET_MODE (lwider->elt->val_rtx));
2043 if (sub)
2044 new_elt_loc_list (e, sub);
2047 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
2048 slot = cselib_find_slot (x, e->hash, INSERT, memmode);
2049 *slot = e;
2050 return e;
2053 if (MEM_P (x))
2054 return cselib_lookup_mem (x, create);
2056 hashval = cselib_hash_rtx (x, create, memmode);
2057 /* Can't even create if hashing is not possible. */
2058 if (! hashval)
2059 return 0;
2061 slot = cselib_find_slot (wrap_constant (mode, x), hashval,
2062 create ? INSERT : NO_INSERT, memmode);
2063 if (slot == 0)
2064 return 0;
2066 e = (cselib_val *) *slot;
2067 if (e)
2068 return e;
2070 e = new_cselib_val (hashval, mode, x);
2072 /* We have to fill the slot before calling cselib_subst_to_values:
2073 the hash table is inconsistent until we do so, and
2074 cselib_subst_to_values will need to do lookups. */
2075 *slot = e;
2076 new_elt_loc_list (e, cselib_subst_to_values (x, memmode));
2077 return e;
2080 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2082 cselib_val *
2083 cselib_lookup_from_insn (rtx x, enum machine_mode mode,
2084 int create, enum machine_mode memmode, rtx insn)
2086 cselib_val *ret;
2088 gcc_assert (!cselib_current_insn);
2089 cselib_current_insn = insn;
2091 ret = cselib_lookup (x, mode, create, memmode);
2093 cselib_current_insn = NULL;
2095 return ret;
2098 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2099 maintains invariants related with debug insns. */
2101 cselib_val *
2102 cselib_lookup (rtx x, enum machine_mode mode,
2103 int create, enum machine_mode memmode)
2105 cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
2107 /* ??? Should we return NULL if we're not to create an entry, the
2108 found loc is a debug loc and cselib_current_insn is not DEBUG?
2109 If so, we should also avoid converting val to non-DEBUG; probably
2110 easiest setting cselib_current_insn to NULL before the call
2111 above. */
2113 if (dump_file && (dump_flags & TDF_CSELIB))
2115 fputs ("cselib lookup ", dump_file);
2116 print_inline_rtx (dump_file, x, 2);
2117 fprintf (dump_file, " => %u:%u\n",
2118 ret ? ret->uid : 0,
2119 ret ? ret->hash : 0);
2122 return ret;
2125 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2126 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2127 is used to determine how many hard registers are being changed. If MODE
2128 is VOIDmode, then only REGNO is being changed; this is used when
2129 invalidating call clobbered registers across a call. */
2131 static void
2132 cselib_invalidate_regno (unsigned int regno, enum machine_mode mode)
2134 unsigned int endregno;
2135 unsigned int i;
2137 /* If we see pseudos after reload, something is _wrong_. */
2138 gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
2139 || reg_renumber[regno] < 0);
2141 /* Determine the range of registers that must be invalidated. For
2142 pseudos, only REGNO is affected. For hard regs, we must take MODE
2143 into account, and we must also invalidate lower register numbers
2144 if they contain values that overlap REGNO. */
2145 if (regno < FIRST_PSEUDO_REGISTER)
2147 gcc_assert (mode != VOIDmode);
2149 if (regno < max_value_regs)
2150 i = 0;
2151 else
2152 i = regno - max_value_regs;
2154 endregno = end_hard_regno (mode, regno);
2156 else
2158 i = regno;
2159 endregno = regno + 1;
2162 for (; i < endregno; i++)
2164 struct elt_list **l = &REG_VALUES (i);
2166 /* Go through all known values for this reg; if it overlaps the range
2167 we're invalidating, remove the value. */
2168 while (*l)
2170 cselib_val *v = (*l)->elt;
2171 bool had_locs;
2172 rtx setting_insn;
2173 struct elt_loc_list **p;
2174 unsigned int this_last = i;
2176 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
2177 this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
2179 if (this_last < regno || v == NULL
2180 || (v == cfa_base_preserved_val
2181 && i == cfa_base_preserved_regno))
2183 l = &(*l)->next;
2184 continue;
2187 /* We have an overlap. */
2188 if (*l == REG_VALUES (i))
2190 /* Maintain the invariant that the first entry of
2191 REG_VALUES, if present, must be the value used to set
2192 the register, or NULL. This is also nice because
2193 then we won't push the same regno onto user_regs
2194 multiple times. */
2195 (*l)->elt = NULL;
2196 l = &(*l)->next;
2198 else
2199 unchain_one_elt_list (l);
2201 v = canonical_cselib_val (v);
2203 had_locs = v->locs != NULL;
2204 setting_insn = v->locs ? v->locs->setting_insn : NULL;
2206 /* Now, we clear the mapping from value to reg. It must exist, so
2207 this code will crash intentionally if it doesn't. */
2208 for (p = &v->locs; ; p = &(*p)->next)
2210 rtx x = (*p)->loc;
2212 if (REG_P (x) && REGNO (x) == i)
2214 unchain_one_elt_loc_list (p);
2215 break;
2219 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2221 if (setting_insn && DEBUG_INSN_P (setting_insn))
2222 n_useless_debug_values++;
2223 else
2224 n_useless_values++;
2230 /* Invalidate any locations in the table which are changed because of a
2231 store to MEM_RTX. If this is called because of a non-const call
2232 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2234 static void
2235 cselib_invalidate_mem (rtx mem_rtx)
2237 cselib_val **vp, *v, *next;
2238 int num_mems = 0;
2239 rtx mem_addr;
2241 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2242 mem_rtx = canon_rtx (mem_rtx);
2244 vp = &first_containing_mem;
2245 for (v = *vp; v != &dummy_val; v = next)
2247 bool has_mem = false;
2248 struct elt_loc_list **p = &v->locs;
2249 bool had_locs = v->locs != NULL;
2250 rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
2252 while (*p)
2254 rtx x = (*p)->loc;
2255 cselib_val *addr;
2256 struct elt_list **mem_chain;
2258 /* MEMs may occur in locations only at the top level; below
2259 that every MEM or REG is substituted by its VALUE. */
2260 if (!MEM_P (x))
2262 p = &(*p)->next;
2263 continue;
2265 if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
2266 && ! canon_anti_dependence (x, false, mem_rtx,
2267 GET_MODE (mem_rtx), mem_addr))
2269 has_mem = true;
2270 num_mems++;
2271 p = &(*p)->next;
2272 continue;
2275 /* This one overlaps. */
2276 /* We must have a mapping from this MEM's address to the
2277 value (E). Remove that, too. */
2278 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
2279 addr = canonical_cselib_val (addr);
2280 gcc_checking_assert (v == canonical_cselib_val (v));
2281 mem_chain = &addr->addr_list;
2282 for (;;)
2284 cselib_val *canon = canonical_cselib_val ((*mem_chain)->elt);
2286 if (canon == v)
2288 unchain_one_elt_list (mem_chain);
2289 break;
2292 /* Record canonicalized elt. */
2293 (*mem_chain)->elt = canon;
2295 mem_chain = &(*mem_chain)->next;
2298 unchain_one_elt_loc_list (p);
2301 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2303 if (setting_insn && DEBUG_INSN_P (setting_insn))
2304 n_useless_debug_values++;
2305 else
2306 n_useless_values++;
2309 next = v->next_containing_mem;
2310 if (has_mem)
2312 *vp = v;
2313 vp = &(*vp)->next_containing_mem;
2315 else
2316 v->next_containing_mem = NULL;
2318 *vp = &dummy_val;
2321 /* Invalidate DEST, which is being assigned to or clobbered. */
2323 void
2324 cselib_invalidate_rtx (rtx dest)
2326 while (GET_CODE (dest) == SUBREG
2327 || GET_CODE (dest) == ZERO_EXTRACT
2328 || GET_CODE (dest) == STRICT_LOW_PART)
2329 dest = XEXP (dest, 0);
2331 if (REG_P (dest))
2332 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
2333 else if (MEM_P (dest))
2334 cselib_invalidate_mem (dest);
2337 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2339 static void
2340 cselib_invalidate_rtx_note_stores (rtx dest, const_rtx ignore ATTRIBUTE_UNUSED,
2341 void *data ATTRIBUTE_UNUSED)
2343 cselib_invalidate_rtx (dest);
2346 /* Record the result of a SET instruction. DEST is being set; the source
2347 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2348 describes its address. */
2350 static void
2351 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
2353 int dreg = REG_P (dest) ? (int) REGNO (dest) : -1;
2355 if (src_elt == 0 || side_effects_p (dest))
2356 return;
2358 if (dreg >= 0)
2360 if (dreg < FIRST_PSEUDO_REGISTER)
2362 unsigned int n = hard_regno_nregs[dreg][GET_MODE (dest)];
2364 if (n > max_value_regs)
2365 max_value_regs = n;
2368 if (REG_VALUES (dreg) == 0)
2370 used_regs[n_used_regs++] = dreg;
2371 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2373 else
2375 /* The register should have been invalidated. */
2376 gcc_assert (REG_VALUES (dreg)->elt == 0);
2377 REG_VALUES (dreg)->elt = src_elt;
2380 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2381 n_useless_values--;
2382 new_elt_loc_list (src_elt, dest);
2384 else if (MEM_P (dest) && dest_addr_elt != 0
2385 && cselib_record_memory)
2387 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2388 n_useless_values--;
2389 add_mem_for_addr (dest_addr_elt, src_elt, dest);
2393 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2395 void
2396 cselib_add_permanent_equiv (cselib_val *elt, rtx x, rtx insn)
2398 cselib_val *nelt;
2399 rtx save_cselib_current_insn = cselib_current_insn;
2401 gcc_checking_assert (elt);
2402 gcc_checking_assert (PRESERVED_VALUE_P (elt->val_rtx));
2403 gcc_checking_assert (!side_effects_p (x));
2405 cselib_current_insn = insn;
2407 nelt = cselib_lookup (x, GET_MODE (elt->val_rtx), 1, VOIDmode);
2409 if (nelt != elt)
2411 cselib_any_perm_equivs = true;
2413 if (!PRESERVED_VALUE_P (nelt->val_rtx))
2414 cselib_preserve_value (nelt);
2416 new_elt_loc_list (nelt, elt->val_rtx);
2419 cselib_current_insn = save_cselib_current_insn;
2422 /* Return TRUE if any permanent equivalences have been recorded since
2423 the table was last initialized. */
2424 bool
2425 cselib_have_permanent_equivalences (void)
2427 return cselib_any_perm_equivs;
2430 /* There is no good way to determine how many elements there can be
2431 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2432 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2434 struct cselib_record_autoinc_data
2436 struct cselib_set *sets;
2437 int n_sets;
2440 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2441 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2443 static int
2444 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
2445 rtx dest, rtx src, rtx srcoff, void *arg)
2447 struct cselib_record_autoinc_data *data;
2448 data = (struct cselib_record_autoinc_data *)arg;
2450 data->sets[data->n_sets].dest = dest;
2452 if (srcoff)
2453 data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
2454 else
2455 data->sets[data->n_sets].src = src;
2457 data->n_sets++;
2459 return -1;
2462 /* Record the effects of any sets and autoincs in INSN. */
2463 static void
2464 cselib_record_sets (rtx insn)
2466 int n_sets = 0;
2467 int i;
2468 struct cselib_set sets[MAX_SETS];
2469 rtx body = PATTERN (insn);
2470 rtx cond = 0;
2471 int n_sets_before_autoinc;
2472 struct cselib_record_autoinc_data data;
2474 body = PATTERN (insn);
2475 if (GET_CODE (body) == COND_EXEC)
2477 cond = COND_EXEC_TEST (body);
2478 body = COND_EXEC_CODE (body);
2481 /* Find all sets. */
2482 if (GET_CODE (body) == SET)
2484 sets[0].src = SET_SRC (body);
2485 sets[0].dest = SET_DEST (body);
2486 n_sets = 1;
2488 else if (GET_CODE (body) == PARALLEL)
2490 /* Look through the PARALLEL and record the values being
2491 set, if possible. Also handle any CLOBBERs. */
2492 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2494 rtx x = XVECEXP (body, 0, i);
2496 if (GET_CODE (x) == SET)
2498 sets[n_sets].src = SET_SRC (x);
2499 sets[n_sets].dest = SET_DEST (x);
2500 n_sets++;
2505 if (n_sets == 1
2506 && MEM_P (sets[0].src)
2507 && !cselib_record_memory
2508 && MEM_READONLY_P (sets[0].src))
2510 rtx note = find_reg_equal_equiv_note (insn);
2512 if (note && CONSTANT_P (XEXP (note, 0)))
2513 sets[0].src = XEXP (note, 0);
2516 data.sets = sets;
2517 data.n_sets = n_sets_before_autoinc = n_sets;
2518 for_each_inc_dec (&insn, cselib_record_autoinc_cb, &data);
2519 n_sets = data.n_sets;
2521 /* Look up the values that are read. Do this before invalidating the
2522 locations that are written. */
2523 for (i = 0; i < n_sets; i++)
2525 rtx dest = sets[i].dest;
2527 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2528 the low part after invalidating any knowledge about larger modes. */
2529 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2530 sets[i].dest = dest = XEXP (dest, 0);
2532 /* We don't know how to record anything but REG or MEM. */
2533 if (REG_P (dest)
2534 || (MEM_P (dest) && cselib_record_memory))
2536 rtx src = sets[i].src;
2537 if (cond)
2538 src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
2539 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
2540 if (MEM_P (dest))
2542 enum machine_mode address_mode = get_address_mode (dest);
2544 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
2545 address_mode, 1,
2546 GET_MODE (dest));
2548 else
2549 sets[i].dest_addr_elt = 0;
2553 if (cselib_record_sets_hook)
2554 cselib_record_sets_hook (insn, sets, n_sets);
2556 /* Invalidate all locations written by this insn. Note that the elts we
2557 looked up in the previous loop aren't affected, just some of their
2558 locations may go away. */
2559 note_stores (body, cselib_invalidate_rtx_note_stores, NULL);
2561 for (i = n_sets_before_autoinc; i < n_sets; i++)
2562 cselib_invalidate_rtx (sets[i].dest);
2564 /* If this is an asm, look for duplicate sets. This can happen when the
2565 user uses the same value as an output multiple times. This is valid
2566 if the outputs are not actually used thereafter. Treat this case as
2567 if the value isn't actually set. We do this by smashing the destination
2568 to pc_rtx, so that we won't record the value later. */
2569 if (n_sets >= 2 && asm_noperands (body) >= 0)
2571 for (i = 0; i < n_sets; i++)
2573 rtx dest = sets[i].dest;
2574 if (REG_P (dest) || MEM_P (dest))
2576 int j;
2577 for (j = i + 1; j < n_sets; j++)
2578 if (rtx_equal_p (dest, sets[j].dest))
2580 sets[i].dest = pc_rtx;
2581 sets[j].dest = pc_rtx;
2587 /* Now enter the equivalences in our tables. */
2588 for (i = 0; i < n_sets; i++)
2590 rtx dest = sets[i].dest;
2591 if (REG_P (dest)
2592 || (MEM_P (dest) && cselib_record_memory))
2593 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2597 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
2599 bool
2600 fp_setter_insn (rtx insn)
2602 rtx expr, pat = NULL_RTX;
2604 if (!RTX_FRAME_RELATED_P (insn))
2605 return false;
2607 expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2608 if (expr)
2609 pat = XEXP (expr, 0);
2610 if (!modified_in_p (hard_frame_pointer_rtx, pat ? pat : insn))
2611 return false;
2613 /* Don't return true for frame pointer restores in the epilogue. */
2614 if (find_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx))
2615 return false;
2616 return true;
2619 /* Record the effects of INSN. */
2621 void
2622 cselib_process_insn (rtx insn)
2624 int i;
2625 rtx x;
2627 cselib_current_insn = insn;
2629 /* Forget everything at a CODE_LABEL, a volatile insn, or a setjmp. */
2630 if ((LABEL_P (insn)
2631 || (CALL_P (insn)
2632 && find_reg_note (insn, REG_SETJMP, NULL))
2633 || (NONJUMP_INSN_P (insn)
2634 && volatile_insn_p (PATTERN (insn))))
2635 && !cselib_preserve_constants)
2637 cselib_reset_table (next_uid);
2638 cselib_current_insn = NULL_RTX;
2639 return;
2642 if (! INSN_P (insn))
2644 cselib_current_insn = NULL_RTX;
2645 return;
2648 /* If this is a call instruction, forget anything stored in a
2649 call clobbered register, or, if this is not a const call, in
2650 memory. */
2651 if (CALL_P (insn))
2653 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2654 if (call_used_regs[i]
2655 || (REG_VALUES (i) && REG_VALUES (i)->elt
2656 && HARD_REGNO_CALL_PART_CLOBBERED (i,
2657 GET_MODE (REG_VALUES (i)->elt->val_rtx))))
2658 cselib_invalidate_regno (i, reg_raw_mode[i]);
2660 /* Since it is not clear how cselib is going to be used, be
2661 conservative here and treat looping pure or const functions
2662 as if they were regular functions. */
2663 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
2664 || !(RTL_CONST_OR_PURE_CALL_P (insn)))
2665 cselib_invalidate_mem (callmem);
2668 cselib_record_sets (insn);
2670 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2671 after we have processed the insn. */
2672 if (CALL_P (insn))
2674 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
2675 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
2676 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
2677 /* Flush evertything on setjmp. */
2678 if (cselib_preserve_constants
2679 && find_reg_note (insn, REG_SETJMP, NULL))
2681 cselib_preserve_only_values ();
2682 cselib_reset_table (next_uid);
2686 /* On setter of the hard frame pointer if frame_pointer_needed,
2687 invalidate stack_pointer_rtx, so that sp and {,h}fp based
2688 VALUEs are distinct. */
2689 if (reload_completed
2690 && frame_pointer_needed
2691 && fp_setter_insn (insn))
2692 cselib_invalidate_rtx (stack_pointer_rtx);
2694 cselib_current_insn = NULL_RTX;
2696 if (n_useless_values > MAX_USELESS_VALUES
2697 /* remove_useless_values is linear in the hash table size. Avoid
2698 quadratic behavior for very large hashtables with very few
2699 useless elements. */
2700 && ((unsigned int)n_useless_values
2701 > (cselib_hash_table.elements () - n_debug_values) / 4))
2702 remove_useless_values ();
2705 /* Initialize cselib for one pass. The caller must also call
2706 init_alias_analysis. */
2708 void
2709 cselib_init (int record_what)
2711 elt_list_pool = create_alloc_pool ("elt_list",
2712 sizeof (struct elt_list), 10);
2713 elt_loc_list_pool = create_alloc_pool ("elt_loc_list",
2714 sizeof (struct elt_loc_list), 10);
2715 cselib_val_pool = create_alloc_pool ("cselib_val_list",
2716 sizeof (cselib_val), 10);
2717 value_pool = create_alloc_pool ("value", RTX_CODE_SIZE (VALUE), 100);
2718 cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
2719 cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
2720 cselib_any_perm_equivs = false;
2722 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2723 see canon_true_dependence. This is only created once. */
2724 if (! callmem)
2725 callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2727 cselib_nregs = max_reg_num ();
2729 /* We preserve reg_values to allow expensive clearing of the whole thing.
2730 Reallocate it however if it happens to be too large. */
2731 if (!reg_values || reg_values_size < cselib_nregs
2732 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
2734 free (reg_values);
2735 /* Some space for newly emit instructions so we don't end up
2736 reallocating in between passes. */
2737 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
2738 reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
2740 used_regs = XNEWVEC (unsigned int, cselib_nregs);
2741 n_used_regs = 0;
2742 cselib_hash_table.create (31);
2743 next_uid = 1;
2746 /* Called when the current user is done with cselib. */
2748 void
2749 cselib_finish (void)
2751 cselib_discard_hook = NULL;
2752 cselib_preserve_constants = false;
2753 cselib_any_perm_equivs = false;
2754 cfa_base_preserved_val = NULL;
2755 cfa_base_preserved_regno = INVALID_REGNUM;
2756 free_alloc_pool (elt_list_pool);
2757 free_alloc_pool (elt_loc_list_pool);
2758 free_alloc_pool (cselib_val_pool);
2759 free_alloc_pool (value_pool);
2760 cselib_clear_table ();
2761 cselib_hash_table.dispose ();
2762 free (used_regs);
2763 used_regs = 0;
2764 n_useless_values = 0;
2765 n_useless_debug_values = 0;
2766 n_debug_values = 0;
2767 next_uid = 0;
2770 /* Dump the cselib_val *X to FILE *OUT. */
2773 dump_cselib_val (cselib_val **x, FILE *out)
2775 cselib_val *v = *x;
2776 bool need_lf = true;
2778 print_inline_rtx (out, v->val_rtx, 0);
2780 if (v->locs)
2782 struct elt_loc_list *l = v->locs;
2783 if (need_lf)
2785 fputc ('\n', out);
2786 need_lf = false;
2788 fputs (" locs:", out);
2791 if (l->setting_insn)
2792 fprintf (out, "\n from insn %i ",
2793 INSN_UID (l->setting_insn));
2794 else
2795 fprintf (out, "\n ");
2796 print_inline_rtx (out, l->loc, 4);
2798 while ((l = l->next));
2799 fputc ('\n', out);
2801 else
2803 fputs (" no locs", out);
2804 need_lf = true;
2807 if (v->addr_list)
2809 struct elt_list *e = v->addr_list;
2810 if (need_lf)
2812 fputc ('\n', out);
2813 need_lf = false;
2815 fputs (" addr list:", out);
2818 fputs ("\n ", out);
2819 print_inline_rtx (out, e->elt->val_rtx, 2);
2821 while ((e = e->next));
2822 fputc ('\n', out);
2824 else
2826 fputs (" no addrs", out);
2827 need_lf = true;
2830 if (v->next_containing_mem == &dummy_val)
2831 fputs (" last mem\n", out);
2832 else if (v->next_containing_mem)
2834 fputs (" next mem ", out);
2835 print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
2836 fputc ('\n', out);
2838 else if (need_lf)
2839 fputc ('\n', out);
2841 return 1;
2844 /* Dump to OUT everything in the CSELIB table. */
2846 void
2847 dump_cselib_table (FILE *out)
2849 fprintf (out, "cselib hash table:\n");
2850 cselib_hash_table.traverse <FILE *, dump_cselib_val> (out);
2851 if (first_containing_mem != &dummy_val)
2853 fputs ("first mem ", out);
2854 print_inline_rtx (out, first_containing_mem->val_rtx, 2);
2855 fputc ('\n', out);
2857 fprintf (out, "next uid %i\n", next_uid);
2860 #include "gt-cselib.h"