Allow overriding the libiberty used for building the LTO plugin.
[official-gcc.git] / gcc / cselib.c
blob00a04baab6e70655761792bc343ea73250120261
1 /* Common subexpression elimination library for GNU compiler.
2 Copyright (C) 1987-2014 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 static bool cselib_record_memory;
53 static bool cselib_preserve_constants;
54 static bool cselib_any_perm_equivs;
55 static inline void promote_debug_loc (struct elt_loc_list *l);
56 static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
57 static void new_elt_loc_list (cselib_val *, rtx);
58 static void unchain_one_value (cselib_val *);
59 static void unchain_one_elt_list (struct elt_list **);
60 static void unchain_one_elt_loc_list (struct elt_loc_list **);
61 static void remove_useless_values (void);
62 static int rtx_equal_for_cselib_1 (rtx, rtx, enum machine_mode);
63 static unsigned int cselib_hash_rtx (rtx, int, enum machine_mode);
64 static cselib_val *new_cselib_val (unsigned int, enum machine_mode, rtx);
65 static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
66 static cselib_val *cselib_lookup_mem (rtx, int);
67 static void cselib_invalidate_regno (unsigned int, enum machine_mode);
68 static void cselib_invalidate_mem (rtx);
69 static void cselib_record_set (rtx, cselib_val *, cselib_val *);
70 static void cselib_record_sets (rtx);
72 struct expand_value_data
74 bitmap regs_active;
75 cselib_expand_callback callback;
76 void *callback_arg;
77 bool dummy;
80 static rtx cselib_expand_value_rtx_1 (rtx, struct expand_value_data *, int);
82 /* There are three ways in which cselib can look up an rtx:
83 - for a REG, the reg_values table (which is indexed by regno) is used
84 - for a MEM, we recursively look up its address and then follow the
85 addr_list of that value
86 - for everything else, we compute a hash value and go through the hash
87 table. Since different rtx's can still have the same hash value,
88 this involves walking the table entries for a given value and comparing
89 the locations of the entries with the rtx we are looking up. */
91 struct cselib_hasher : typed_noop_remove <cselib_val>
93 typedef cselib_val value_type;
94 struct compare_type {
95 /* The rtx value and its mode (needed separately for constant
96 integers). */
97 enum machine_mode mode;
98 rtx x;
99 /* The mode of the contaning MEM, if any, otherwise VOIDmode. */
100 enum machine_mode memmode;
102 static inline hashval_t hash (const value_type *);
103 static inline bool equal (const value_type *, const compare_type *);
106 /* The hash function for our hash table. The value is always computed with
107 cselib_hash_rtx when adding an element; this function just extracts the
108 hash value from a cselib_val structure. */
110 inline hashval_t
111 cselib_hasher::hash (const value_type *v)
113 return v->hash;
116 /* The equality test for our hash table. The first argument V is a table
117 element (i.e. a cselib_val), while the second arg X is an rtx. We know
118 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
119 CONST of an appropriate mode. */
121 inline bool
122 cselib_hasher::equal (const value_type *v, const compare_type *x_arg)
124 struct elt_loc_list *l;
125 rtx x = x_arg->x;
126 enum machine_mode mode = x_arg->mode;
127 enum machine_mode memmode = x_arg->memmode;
129 if (mode != GET_MODE (v->val_rtx))
130 return false;
132 if (GET_CODE (x) == VALUE)
133 return x == v->val_rtx;
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, 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 /* A table to hold preserved values. */
151 static hash_table <cselib_hasher> cselib_preserved_hash_table;
153 /* This is a global so we don't have to pass this through every function.
154 It is used in new_elt_loc_list to set SETTING_INSN. */
155 static rtx cselib_current_insn;
157 /* The unique id that the next create value will take. */
158 static unsigned int next_uid;
160 /* The number of registers we had when the varrays were last resized. */
161 static unsigned int cselib_nregs;
163 /* Count values without known locations, or with only locations that
164 wouldn't have been known except for debug insns. Whenever this
165 grows too big, we remove these useless values from the table.
167 Counting values with only debug values is a bit tricky. We don't
168 want to increment n_useless_values when we create a value for a
169 debug insn, for this would get n_useless_values out of sync, but we
170 want increment it if all locs in the list that were ever referenced
171 in nondebug insns are removed from the list.
173 In the general case, once we do that, we'd have to stop accepting
174 nondebug expressions in the loc list, to avoid having two values
175 equivalent that, without debug insns, would have been made into
176 separate values. However, because debug insns never introduce
177 equivalences themselves (no assignments), the only means for
178 growing loc lists is through nondebug assignments. If the locs
179 also happen to be referenced in debug insns, it will work just fine.
181 A consequence of this is that there's at most one debug-only loc in
182 each loc list. If we keep it in the first entry, testing whether
183 we have a debug-only loc list takes O(1).
185 Furthermore, since any additional entry in a loc list containing a
186 debug loc would have to come from an assignment (nondebug) that
187 references both the initial debug loc and the newly-equivalent loc,
188 the initial debug loc would be promoted to a nondebug loc, and the
189 loc list would not contain debug locs any more.
191 So the only case we have to be careful with in order to keep
192 n_useless_values in sync between debug and nondebug compilations is
193 to avoid incrementing n_useless_values when removing the single loc
194 from a value that turns out to not appear outside debug values. We
195 increment n_useless_debug_values instead, and leave such values
196 alone until, for other reasons, we garbage-collect useless
197 values. */
198 static int n_useless_values;
199 static int n_useless_debug_values;
201 /* Count values whose locs have been taken exclusively from debug
202 insns for the entire life of the value. */
203 static int n_debug_values;
205 /* Number of useless values before we remove them from the hash table. */
206 #define MAX_USELESS_VALUES 32
208 /* This table maps from register number to values. It does not
209 contain pointers to cselib_val structures, but rather elt_lists.
210 The purpose is to be able to refer to the same register in
211 different modes. The first element of the list defines the mode in
212 which the register was set; if the mode is unknown or the value is
213 no longer valid in that mode, ELT will be NULL for the first
214 element. */
215 static struct elt_list **reg_values;
216 static unsigned int reg_values_size;
217 #define REG_VALUES(i) reg_values[i]
219 /* The largest number of hard regs used by any entry added to the
220 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
221 static unsigned int max_value_regs;
223 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
224 in cselib_clear_table() for fast emptying. */
225 static unsigned int *used_regs;
226 static unsigned int n_used_regs;
228 /* We pass this to cselib_invalidate_mem to invalidate all of
229 memory for a non-const call instruction. */
230 static GTY(()) rtx callmem;
232 /* Set by discard_useless_locs if it deleted the last location of any
233 value. */
234 static int values_became_useless;
236 /* Used as stop element of the containing_mem list so we can check
237 presence in the list by checking the next pointer. */
238 static cselib_val dummy_val;
240 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
241 that is constant through the whole function and should never be
242 eliminated. */
243 static cselib_val *cfa_base_preserved_val;
244 static unsigned int cfa_base_preserved_regno = INVALID_REGNUM;
246 /* Used to list all values that contain memory reference.
247 May or may not contain the useless values - the list is compacted
248 each time memory is invalidated. */
249 static cselib_val *first_containing_mem = &dummy_val;
250 static alloc_pool elt_loc_list_pool, elt_list_pool, cselib_val_pool, value_pool;
252 /* If nonnull, cselib will call this function before freeing useless
253 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
254 void (*cselib_discard_hook) (cselib_val *);
256 /* If nonnull, cselib will call this function before recording sets or
257 even clobbering outputs of INSN. All the recorded sets will be
258 represented in the array sets[n_sets]. new_val_min can be used to
259 tell whether values present in sets are introduced by this
260 instruction. */
261 void (*cselib_record_sets_hook) (rtx insn, struct cselib_set *sets,
262 int n_sets);
264 #define PRESERVED_VALUE_P(RTX) \
265 (RTL_FLAG_CHECK1 ("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
267 #define SP_BASED_VALUE_P(RTX) \
268 (RTL_FLAG_CHECK1 ("SP_BASED_VALUE_P", (RTX), VALUE)->jump)
272 /* Allocate a struct elt_list and fill in its two elements with the
273 arguments. */
275 static inline struct elt_list *
276 new_elt_list (struct elt_list *next, cselib_val *elt)
278 struct elt_list *el;
279 el = (struct elt_list *) pool_alloc (elt_list_pool);
280 el->next = next;
281 el->elt = elt;
282 return el;
285 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
286 list. */
288 static inline void
289 new_elt_loc_list (cselib_val *val, rtx loc)
291 struct elt_loc_list *el, *next = val->locs;
293 gcc_checking_assert (!next || !next->setting_insn
294 || !DEBUG_INSN_P (next->setting_insn)
295 || cselib_current_insn == next->setting_insn);
297 /* If we're creating the first loc in a debug insn context, we've
298 just created a debug value. Count it. */
299 if (!next && cselib_current_insn && DEBUG_INSN_P (cselib_current_insn))
300 n_debug_values++;
302 val = canonical_cselib_val (val);
303 next = val->locs;
305 if (GET_CODE (loc) == VALUE)
307 loc = canonical_cselib_val (CSELIB_VAL_PTR (loc))->val_rtx;
309 gcc_checking_assert (PRESERVED_VALUE_P (loc)
310 == PRESERVED_VALUE_P (val->val_rtx));
312 if (val->val_rtx == loc)
313 return;
314 else if (val->uid > CSELIB_VAL_PTR (loc)->uid)
316 /* Reverse the insertion. */
317 new_elt_loc_list (CSELIB_VAL_PTR (loc), val->val_rtx);
318 return;
321 gcc_checking_assert (val->uid < CSELIB_VAL_PTR (loc)->uid);
323 if (CSELIB_VAL_PTR (loc)->locs)
325 /* Bring all locs from LOC to VAL. */
326 for (el = CSELIB_VAL_PTR (loc)->locs; el->next; el = el->next)
328 /* Adjust values that have LOC as canonical so that VAL
329 becomes their canonical. */
330 if (el->loc && GET_CODE (el->loc) == VALUE)
332 gcc_checking_assert (CSELIB_VAL_PTR (el->loc)->locs->loc
333 == loc);
334 CSELIB_VAL_PTR (el->loc)->locs->loc = val->val_rtx;
337 el->next = val->locs;
338 next = val->locs = CSELIB_VAL_PTR (loc)->locs;
341 if (CSELIB_VAL_PTR (loc)->addr_list)
343 /* Bring in addr_list into canonical node. */
344 struct elt_list *last = CSELIB_VAL_PTR (loc)->addr_list;
345 while (last->next)
346 last = last->next;
347 last->next = val->addr_list;
348 val->addr_list = CSELIB_VAL_PTR (loc)->addr_list;
349 CSELIB_VAL_PTR (loc)->addr_list = NULL;
352 if (CSELIB_VAL_PTR (loc)->next_containing_mem != NULL
353 && val->next_containing_mem == NULL)
355 /* Add VAL to the containing_mem list after LOC. LOC will
356 be removed when we notice it doesn't contain any
357 MEMs. */
358 val->next_containing_mem = CSELIB_VAL_PTR (loc)->next_containing_mem;
359 CSELIB_VAL_PTR (loc)->next_containing_mem = val;
362 /* Chain LOC back to VAL. */
363 el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
364 el->loc = val->val_rtx;
365 el->setting_insn = cselib_current_insn;
366 el->next = NULL;
367 CSELIB_VAL_PTR (loc)->locs = el;
370 el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
371 el->loc = loc;
372 el->setting_insn = cselib_current_insn;
373 el->next = next;
374 val->locs = el;
377 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
378 originating from a debug insn, maintaining the debug values
379 count. */
381 static inline void
382 promote_debug_loc (struct elt_loc_list *l)
384 if (l && l->setting_insn && DEBUG_INSN_P (l->setting_insn)
385 && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
387 n_debug_values--;
388 l->setting_insn = cselib_current_insn;
389 if (cselib_preserve_constants && l->next)
391 gcc_assert (l->next->setting_insn
392 && DEBUG_INSN_P (l->next->setting_insn)
393 && !l->next->next);
394 l->next->setting_insn = cselib_current_insn;
396 else
397 gcc_assert (!l->next);
401 /* The elt_list at *PL is no longer needed. Unchain it and free its
402 storage. */
404 static inline void
405 unchain_one_elt_list (struct elt_list **pl)
407 struct elt_list *l = *pl;
409 *pl = l->next;
410 pool_free (elt_list_pool, l);
413 /* Likewise for elt_loc_lists. */
415 static void
416 unchain_one_elt_loc_list (struct elt_loc_list **pl)
418 struct elt_loc_list *l = *pl;
420 *pl = l->next;
421 pool_free (elt_loc_list_pool, l);
424 /* Likewise for cselib_vals. This also frees the addr_list associated with
425 V. */
427 static void
428 unchain_one_value (cselib_val *v)
430 while (v->addr_list)
431 unchain_one_elt_list (&v->addr_list);
433 pool_free (cselib_val_pool, v);
436 /* Remove all entries from the hash table. Also used during
437 initialization. */
439 void
440 cselib_clear_table (void)
442 cselib_reset_table (1);
445 /* Return TRUE if V is a constant, a function invariant or a VALUE
446 equivalence; FALSE otherwise. */
448 static bool
449 invariant_or_equiv_p (cselib_val *v)
451 struct elt_loc_list *l;
453 if (v == cfa_base_preserved_val)
454 return true;
456 /* Keep VALUE equivalences around. */
457 for (l = v->locs; l; l = l->next)
458 if (GET_CODE (l->loc) == VALUE)
459 return true;
461 if (v->locs != NULL
462 && v->locs->next == NULL)
464 if (CONSTANT_P (v->locs->loc)
465 && (GET_CODE (v->locs->loc) != CONST
466 || !references_value_p (v->locs->loc, 0)))
467 return true;
468 /* Although a debug expr may be bound to different expressions,
469 we can preserve it as if it was constant, to get unification
470 and proper merging within var-tracking. */
471 if (GET_CODE (v->locs->loc) == DEBUG_EXPR
472 || GET_CODE (v->locs->loc) == DEBUG_IMPLICIT_PTR
473 || GET_CODE (v->locs->loc) == ENTRY_VALUE
474 || GET_CODE (v->locs->loc) == DEBUG_PARAMETER_REF)
475 return true;
477 /* (plus (value V) (const_int C)) is invariant iff V is invariant. */
478 if (GET_CODE (v->locs->loc) == PLUS
479 && CONST_INT_P (XEXP (v->locs->loc, 1))
480 && GET_CODE (XEXP (v->locs->loc, 0)) == VALUE
481 && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v->locs->loc, 0))))
482 return true;
485 return false;
488 /* Remove from hash table all VALUEs except constants, function
489 invariants and VALUE equivalences. */
492 preserve_constants_and_equivs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
494 cselib_val *v = *x;
496 if (invariant_or_equiv_p (v))
498 cselib_hasher::compare_type lookup = {
499 GET_MODE (v->val_rtx), v->val_rtx, VOIDmode
501 cselib_val **slot
502 = cselib_preserved_hash_table.find_slot_with_hash (&lookup,
503 v->hash, INSERT);
504 gcc_assert (!*slot);
505 *slot = v;
508 cselib_hash_table.clear_slot (x);
510 return 1;
513 /* Remove all entries from the hash table, arranging for the next
514 value to be numbered NUM. */
516 void
517 cselib_reset_table (unsigned int num)
519 unsigned int i;
521 max_value_regs = 0;
523 if (cfa_base_preserved_val)
525 unsigned int regno = cfa_base_preserved_regno;
526 unsigned int new_used_regs = 0;
527 for (i = 0; i < n_used_regs; i++)
528 if (used_regs[i] == regno)
530 new_used_regs = 1;
531 continue;
533 else
534 REG_VALUES (used_regs[i]) = 0;
535 gcc_assert (new_used_regs == 1);
536 n_used_regs = new_used_regs;
537 used_regs[0] = regno;
538 max_value_regs
539 = hard_regno_nregs[regno][GET_MODE (cfa_base_preserved_val->locs->loc)];
541 else
543 for (i = 0; i < n_used_regs; i++)
544 REG_VALUES (used_regs[i]) = 0;
545 n_used_regs = 0;
548 if (cselib_preserve_constants)
549 cselib_hash_table.traverse <void *, preserve_constants_and_equivs> (NULL);
550 else
552 cselib_hash_table.empty ();
553 gcc_checking_assert (!cselib_any_perm_equivs);
556 n_useless_values = 0;
557 n_useless_debug_values = 0;
558 n_debug_values = 0;
560 next_uid = num;
562 first_containing_mem = &dummy_val;
565 /* Return the number of the next value that will be generated. */
567 unsigned int
568 cselib_get_next_uid (void)
570 return next_uid;
573 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
574 INSERTing if requested. When X is part of the address of a MEM,
575 MEMMODE should specify the mode of the MEM. */
577 static cselib_val **
578 cselib_find_slot (enum machine_mode mode, rtx x, hashval_t hash,
579 enum insert_option insert, enum machine_mode memmode)
581 cselib_val **slot = NULL;
582 cselib_hasher::compare_type lookup = { mode, x, memmode };
583 if (cselib_preserve_constants)
584 slot = cselib_preserved_hash_table.find_slot_with_hash (&lookup, hash,
585 NO_INSERT);
586 if (!slot)
587 slot = cselib_hash_table.find_slot_with_hash (&lookup, hash, insert);
588 return slot;
591 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
592 only return true for values which point to a cselib_val whose value
593 element has been set to zero, which implies the cselib_val will be
594 removed. */
597 references_value_p (const_rtx x, int only_useless)
599 const enum rtx_code code = GET_CODE (x);
600 const char *fmt = GET_RTX_FORMAT (code);
601 int i, j;
603 if (GET_CODE (x) == VALUE
604 && (! only_useless ||
605 (CSELIB_VAL_PTR (x)->locs == 0 && !PRESERVED_VALUE_P (x))))
606 return 1;
608 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
610 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
611 return 1;
612 else if (fmt[i] == 'E')
613 for (j = 0; j < XVECLEN (x, i); j++)
614 if (references_value_p (XVECEXP (x, i, j), only_useless))
615 return 1;
618 return 0;
621 /* For all locations found in X, delete locations that reference useless
622 values (i.e. values without any location). Called through
623 htab_traverse. */
626 discard_useless_locs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
628 cselib_val *v = *x;
629 struct elt_loc_list **p = &v->locs;
630 bool had_locs = v->locs != NULL;
631 rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
633 while (*p)
635 if (references_value_p ((*p)->loc, 1))
636 unchain_one_elt_loc_list (p);
637 else
638 p = &(*p)->next;
641 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
643 if (setting_insn && DEBUG_INSN_P (setting_insn))
644 n_useless_debug_values++;
645 else
646 n_useless_values++;
647 values_became_useless = 1;
649 return 1;
652 /* If X is a value with no locations, remove it from the hashtable. */
655 discard_useless_values (cselib_val **x, void *info ATTRIBUTE_UNUSED)
657 cselib_val *v = *x;
659 if (v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
661 if (cselib_discard_hook)
662 cselib_discard_hook (v);
664 CSELIB_VAL_PTR (v->val_rtx) = NULL;
665 cselib_hash_table.clear_slot (x);
666 unchain_one_value (v);
667 n_useless_values--;
670 return 1;
673 /* Clean out useless values (i.e. those which no longer have locations
674 associated with them) from the hash table. */
676 static void
677 remove_useless_values (void)
679 cselib_val **p, *v;
681 /* First pass: eliminate locations that reference the value. That in
682 turn can make more values useless. */
685 values_became_useless = 0;
686 cselib_hash_table.traverse <void *, discard_useless_locs> (NULL);
688 while (values_became_useless);
690 /* Second pass: actually remove the values. */
692 p = &first_containing_mem;
693 for (v = *p; v != &dummy_val; v = v->next_containing_mem)
694 if (v->locs && v == canonical_cselib_val (v))
696 *p = v;
697 p = &(*p)->next_containing_mem;
699 *p = &dummy_val;
701 n_useless_values += n_useless_debug_values;
702 n_debug_values -= n_useless_debug_values;
703 n_useless_debug_values = 0;
705 cselib_hash_table.traverse <void *, discard_useless_values> (NULL);
707 gcc_assert (!n_useless_values);
710 /* Arrange for a value to not be removed from the hash table even if
711 it becomes useless. */
713 void
714 cselib_preserve_value (cselib_val *v)
716 PRESERVED_VALUE_P (v->val_rtx) = 1;
719 /* Test whether a value is preserved. */
721 bool
722 cselib_preserved_value_p (cselib_val *v)
724 return PRESERVED_VALUE_P (v->val_rtx);
727 /* Arrange for a REG value to be assumed constant through the whole function,
728 never invalidated and preserved across cselib_reset_table calls. */
730 void
731 cselib_preserve_cfa_base_value (cselib_val *v, unsigned int regno)
733 if (cselib_preserve_constants
734 && v->locs
735 && REG_P (v->locs->loc))
737 cfa_base_preserved_val = v;
738 cfa_base_preserved_regno = regno;
742 /* Clean all non-constant expressions in the hash table, but retain
743 their values. */
745 void
746 cselib_preserve_only_values (void)
748 int i;
750 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
751 cselib_invalidate_regno (i, reg_raw_mode[i]);
753 cselib_invalidate_mem (callmem);
755 remove_useless_values ();
757 gcc_assert (first_containing_mem == &dummy_val);
760 /* Arrange for a value to be marked as based on stack pointer
761 for find_base_term purposes. */
763 void
764 cselib_set_value_sp_based (cselib_val *v)
766 SP_BASED_VALUE_P (v->val_rtx) = 1;
769 /* Test whether a value is based on stack pointer for
770 find_base_term purposes. */
772 bool
773 cselib_sp_based_value_p (cselib_val *v)
775 return SP_BASED_VALUE_P (v->val_rtx);
778 /* Return the mode in which a register was last set. If X is not a
779 register, return its mode. If the mode in which the register was
780 set is not known, or the value was already clobbered, return
781 VOIDmode. */
783 enum machine_mode
784 cselib_reg_set_mode (const_rtx x)
786 if (!REG_P (x))
787 return GET_MODE (x);
789 if (REG_VALUES (REGNO (x)) == NULL
790 || REG_VALUES (REGNO (x))->elt == NULL)
791 return VOIDmode;
793 return GET_MODE (REG_VALUES (REGNO (x))->elt->val_rtx);
796 /* Return nonzero if we can prove that X and Y contain the same value, taking
797 our gathered information into account. */
800 rtx_equal_for_cselib_p (rtx x, rtx y)
802 return rtx_equal_for_cselib_1 (x, y, VOIDmode);
805 /* If x is a PLUS or an autoinc operation, expand the operation,
806 storing the offset, if any, in *OFF. */
808 static rtx
809 autoinc_split (rtx x, rtx *off, enum machine_mode memmode)
811 switch (GET_CODE (x))
813 case PLUS:
814 *off = XEXP (x, 1);
815 return XEXP (x, 0);
817 case PRE_DEC:
818 if (memmode == VOIDmode)
819 return x;
821 *off = GEN_INT (-GET_MODE_SIZE (memmode));
822 return XEXP (x, 0);
823 break;
825 case PRE_INC:
826 if (memmode == VOIDmode)
827 return x;
829 *off = GEN_INT (GET_MODE_SIZE (memmode));
830 return XEXP (x, 0);
832 case PRE_MODIFY:
833 return XEXP (x, 1);
835 case POST_DEC:
836 case POST_INC:
837 case POST_MODIFY:
838 return XEXP (x, 0);
840 default:
841 return x;
845 /* Return nonzero if we can prove that X and Y contain the same value,
846 taking our gathered information into account. MEMMODE holds the
847 mode of the enclosing MEM, if any, as required to deal with autoinc
848 addressing modes. If X and Y are not (known to be) part of
849 addresses, MEMMODE should be VOIDmode. */
851 static int
852 rtx_equal_for_cselib_1 (rtx x, rtx y, enum machine_mode memmode)
854 enum rtx_code code;
855 const char *fmt;
856 int i;
858 if (REG_P (x) || MEM_P (x))
860 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0, memmode);
862 if (e)
863 x = e->val_rtx;
866 if (REG_P (y) || MEM_P (y))
868 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0, memmode);
870 if (e)
871 y = e->val_rtx;
874 if (x == y)
875 return 1;
877 if (GET_CODE (x) == VALUE)
879 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (x));
880 struct elt_loc_list *l;
882 if (GET_CODE (y) == VALUE)
883 return e == canonical_cselib_val (CSELIB_VAL_PTR (y));
885 for (l = e->locs; l; l = l->next)
887 rtx t = l->loc;
889 /* Avoid infinite recursion. We know we have the canonical
890 value, so we can just skip any values in the equivalence
891 list. */
892 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
893 continue;
894 else if (rtx_equal_for_cselib_1 (t, y, memmode))
895 return 1;
898 return 0;
900 else if (GET_CODE (y) == VALUE)
902 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (y));
903 struct elt_loc_list *l;
905 for (l = e->locs; l; l = l->next)
907 rtx t = l->loc;
909 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
910 continue;
911 else if (rtx_equal_for_cselib_1 (x, t, memmode))
912 return 1;
915 return 0;
918 if (GET_MODE (x) != GET_MODE (y))
919 return 0;
921 if (GET_CODE (x) != GET_CODE (y))
923 rtx xorig = x, yorig = y;
924 rtx xoff = NULL, yoff = NULL;
926 x = autoinc_split (x, &xoff, memmode);
927 y = autoinc_split (y, &yoff, memmode);
929 if (!xoff != !yoff)
930 return 0;
932 if (xoff && !rtx_equal_for_cselib_1 (xoff, yoff, memmode))
933 return 0;
935 /* Don't recurse if nothing changed. */
936 if (x != xorig || y != yorig)
937 return rtx_equal_for_cselib_1 (x, y, memmode);
939 return 0;
942 /* These won't be handled correctly by the code below. */
943 switch (GET_CODE (x))
945 CASE_CONST_UNIQUE:
946 case DEBUG_EXPR:
947 return 0;
949 case DEBUG_IMPLICIT_PTR:
950 return DEBUG_IMPLICIT_PTR_DECL (x)
951 == DEBUG_IMPLICIT_PTR_DECL (y);
953 case DEBUG_PARAMETER_REF:
954 return DEBUG_PARAMETER_REF_DECL (x)
955 == DEBUG_PARAMETER_REF_DECL (y);
957 case ENTRY_VALUE:
958 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
959 use rtx_equal_for_cselib_1 to compare the operands. */
960 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
962 case LABEL_REF:
963 return XEXP (x, 0) == XEXP (y, 0);
965 case MEM:
966 /* We have to compare any autoinc operations in the addresses
967 using this MEM's mode. */
968 return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x));
970 default:
971 break;
974 code = GET_CODE (x);
975 fmt = GET_RTX_FORMAT (code);
977 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
979 int j;
981 switch (fmt[i])
983 case 'w':
984 if (XWINT (x, i) != XWINT (y, i))
985 return 0;
986 break;
988 case 'n':
989 case 'i':
990 if (XINT (x, i) != XINT (y, i))
991 return 0;
992 break;
994 case 'V':
995 case 'E':
996 /* Two vectors must have the same length. */
997 if (XVECLEN (x, i) != XVECLEN (y, i))
998 return 0;
1000 /* And the corresponding elements must match. */
1001 for (j = 0; j < XVECLEN (x, i); j++)
1002 if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
1003 XVECEXP (y, i, j), memmode))
1004 return 0;
1005 break;
1007 case 'e':
1008 if (i == 1
1009 && targetm.commutative_p (x, UNKNOWN)
1010 && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode)
1011 && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode))
1012 return 1;
1013 if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode))
1014 return 0;
1015 break;
1017 case 'S':
1018 case 's':
1019 if (strcmp (XSTR (x, i), XSTR (y, i)))
1020 return 0;
1021 break;
1023 case 'u':
1024 /* These are just backpointers, so they don't matter. */
1025 break;
1027 case '0':
1028 case 't':
1029 break;
1031 /* It is believed that rtx's at this level will never
1032 contain anything but integers and other rtx's,
1033 except for within LABEL_REFs and SYMBOL_REFs. */
1034 default:
1035 gcc_unreachable ();
1038 return 1;
1041 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1042 For registers and memory locations, we look up their cselib_val structure
1043 and return its VALUE element.
1044 Possible reasons for return 0 are: the object is volatile, or we couldn't
1045 find a register or memory location in the table and CREATE is zero. If
1046 CREATE is nonzero, table elts are created for regs and mem.
1047 N.B. this hash function returns the same hash value for RTXes that
1048 differ only in the order of operands, thus it is suitable for comparisons
1049 that take commutativity into account.
1050 If we wanted to also support associative rules, we'd have to use a different
1051 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1052 MEMMODE indicates the mode of an enclosing MEM, and it's only
1053 used to compute autoinc values.
1054 We used to have a MODE argument for hashing for CONST_INTs, but that
1055 didn't make sense, since it caused spurious hash differences between
1056 (set (reg:SI 1) (const_int))
1057 (plus:SI (reg:SI 2) (reg:SI 1))
1059 (plus:SI (reg:SI 2) (const_int))
1060 If the mode is important in any context, it must be checked specifically
1061 in a comparison anyway, since relying on hash differences is unsafe. */
1063 static unsigned int
1064 cselib_hash_rtx (rtx x, int create, enum machine_mode memmode)
1066 cselib_val *e;
1067 int i, j;
1068 enum rtx_code code;
1069 const char *fmt;
1070 unsigned int hash = 0;
1072 code = GET_CODE (x);
1073 hash += (unsigned) code + (unsigned) GET_MODE (x);
1075 switch (code)
1077 case VALUE:
1078 e = CSELIB_VAL_PTR (x);
1079 return e->hash;
1081 case MEM:
1082 case REG:
1083 e = cselib_lookup (x, GET_MODE (x), create, memmode);
1084 if (! e)
1085 return 0;
1087 return e->hash;
1089 case DEBUG_EXPR:
1090 hash += ((unsigned) DEBUG_EXPR << 7)
1091 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
1092 return hash ? hash : (unsigned int) DEBUG_EXPR;
1094 case DEBUG_IMPLICIT_PTR:
1095 hash += ((unsigned) DEBUG_IMPLICIT_PTR << 7)
1096 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x));
1097 return hash ? hash : (unsigned int) DEBUG_IMPLICIT_PTR;
1099 case DEBUG_PARAMETER_REF:
1100 hash += ((unsigned) DEBUG_PARAMETER_REF << 7)
1101 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x));
1102 return hash ? hash : (unsigned int) DEBUG_PARAMETER_REF;
1104 case ENTRY_VALUE:
1105 /* ENTRY_VALUEs are function invariant, thus try to avoid
1106 recursing on argument if ENTRY_VALUE is one of the
1107 forms emitted by expand_debug_expr, otherwise
1108 ENTRY_VALUE hash would depend on the current value
1109 in some register or memory. */
1110 if (REG_P (ENTRY_VALUE_EXP (x)))
1111 hash += (unsigned int) REG
1112 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x))
1113 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x));
1114 else if (MEM_P (ENTRY_VALUE_EXP (x))
1115 && REG_P (XEXP (ENTRY_VALUE_EXP (x), 0)))
1116 hash += (unsigned int) MEM
1117 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x), 0))
1118 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x), 0));
1119 else
1120 hash += cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode);
1121 return hash ? hash : (unsigned int) ENTRY_VALUE;
1123 case CONST_INT:
1124 hash += ((unsigned) CONST_INT << 7) + UINTVAL (x);
1125 return hash ? hash : (unsigned int) CONST_INT;
1127 case CONST_WIDE_INT:
1128 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
1129 hash += CONST_WIDE_INT_ELT (x, i);
1130 return hash;
1132 case CONST_DOUBLE:
1133 /* This is like the general case, except that it only counts
1134 the integers representing the constant. */
1135 hash += (unsigned) code + (unsigned) GET_MODE (x);
1136 if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (x) == VOIDmode)
1137 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1138 + (unsigned) CONST_DOUBLE_HIGH (x));
1139 else
1140 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
1141 return hash ? hash : (unsigned int) CONST_DOUBLE;
1143 case CONST_FIXED:
1144 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1145 hash += fixed_hash (CONST_FIXED_VALUE (x));
1146 return hash ? hash : (unsigned int) CONST_FIXED;
1148 case CONST_VECTOR:
1150 int units;
1151 rtx elt;
1153 units = CONST_VECTOR_NUNITS (x);
1155 for (i = 0; i < units; ++i)
1157 elt = CONST_VECTOR_ELT (x, i);
1158 hash += cselib_hash_rtx (elt, 0, memmode);
1161 return hash;
1164 /* Assume there is only one rtx object for any given label. */
1165 case LABEL_REF:
1166 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1167 differences and differences between each stage's debugging dumps. */
1168 hash += (((unsigned int) LABEL_REF << 7)
1169 + CODE_LABEL_NUMBER (XEXP (x, 0)));
1170 return hash ? hash : (unsigned int) LABEL_REF;
1172 case SYMBOL_REF:
1174 /* Don't hash on the symbol's address to avoid bootstrap differences.
1175 Different hash values may cause expressions to be recorded in
1176 different orders and thus different registers to be used in the
1177 final assembler. This also avoids differences in the dump files
1178 between various stages. */
1179 unsigned int h = 0;
1180 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1182 while (*p)
1183 h += (h << 7) + *p++; /* ??? revisit */
1185 hash += ((unsigned int) SYMBOL_REF << 7) + h;
1186 return hash ? hash : (unsigned int) SYMBOL_REF;
1189 case PRE_DEC:
1190 case PRE_INC:
1191 /* We can't compute these without knowing the MEM mode. */
1192 gcc_assert (memmode != VOIDmode);
1193 i = GET_MODE_SIZE (memmode);
1194 if (code == PRE_DEC)
1195 i = -i;
1196 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1197 like (mem:MEMMODE (plus (reg) (const_int I))). */
1198 hash += (unsigned) PLUS - (unsigned)code
1199 + cselib_hash_rtx (XEXP (x, 0), create, memmode)
1200 + cselib_hash_rtx (GEN_INT (i), create, memmode);
1201 return hash ? hash : 1 + (unsigned) PLUS;
1203 case PRE_MODIFY:
1204 gcc_assert (memmode != VOIDmode);
1205 return cselib_hash_rtx (XEXP (x, 1), create, memmode);
1207 case POST_DEC:
1208 case POST_INC:
1209 case POST_MODIFY:
1210 gcc_assert (memmode != VOIDmode);
1211 return cselib_hash_rtx (XEXP (x, 0), create, memmode);
1213 case PC:
1214 case CC0:
1215 case CALL:
1216 case UNSPEC_VOLATILE:
1217 return 0;
1219 case ASM_OPERANDS:
1220 if (MEM_VOLATILE_P (x))
1221 return 0;
1223 break;
1225 default:
1226 break;
1229 i = GET_RTX_LENGTH (code) - 1;
1230 fmt = GET_RTX_FORMAT (code);
1231 for (; i >= 0; i--)
1233 switch (fmt[i])
1235 case 'e':
1237 rtx tem = XEXP (x, i);
1238 unsigned int tem_hash = cselib_hash_rtx (tem, create, memmode);
1240 if (tem_hash == 0)
1241 return 0;
1243 hash += tem_hash;
1245 break;
1246 case 'E':
1247 for (j = 0; j < XVECLEN (x, i); j++)
1249 unsigned int tem_hash
1250 = cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
1252 if (tem_hash == 0)
1253 return 0;
1255 hash += tem_hash;
1257 break;
1259 case 's':
1261 const unsigned char *p = (const unsigned char *) XSTR (x, i);
1263 if (p)
1264 while (*p)
1265 hash += *p++;
1266 break;
1269 case 'i':
1270 hash += XINT (x, i);
1271 break;
1273 case '0':
1274 case 't':
1275 /* unused */
1276 break;
1278 default:
1279 gcc_unreachable ();
1283 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
1286 /* Create a new value structure for VALUE and initialize it. The mode of the
1287 value is MODE. */
1289 static inline cselib_val *
1290 new_cselib_val (unsigned int hash, enum machine_mode mode, rtx x)
1292 cselib_val *e = (cselib_val *) pool_alloc (cselib_val_pool);
1294 gcc_assert (hash);
1295 gcc_assert (next_uid);
1297 e->hash = hash;
1298 e->uid = next_uid++;
1299 /* We use an alloc pool to allocate this RTL construct because it
1300 accounts for about 8% of the overall memory usage. We know
1301 precisely when we can have VALUE RTXen (when cselib is active)
1302 so we don't need to put them in garbage collected memory.
1303 ??? Why should a VALUE be an RTX in the first place? */
1304 e->val_rtx = (rtx) pool_alloc (value_pool);
1305 memset (e->val_rtx, 0, RTX_HDR_SIZE);
1306 PUT_CODE (e->val_rtx, VALUE);
1307 PUT_MODE (e->val_rtx, mode);
1308 CSELIB_VAL_PTR (e->val_rtx) = e;
1309 e->addr_list = 0;
1310 e->locs = 0;
1311 e->next_containing_mem = 0;
1313 if (dump_file && (dump_flags & TDF_CSELIB))
1315 fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
1316 if (flag_dump_noaddr || flag_dump_unnumbered)
1317 fputs ("# ", dump_file);
1318 else
1319 fprintf (dump_file, "%p ", (void*)e);
1320 print_rtl_single (dump_file, x);
1321 fputc ('\n', dump_file);
1324 return e;
1327 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1328 contains the data at this address. X is a MEM that represents the
1329 value. Update the two value structures to represent this situation. */
1331 static void
1332 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
1334 struct elt_loc_list *l;
1336 addr_elt = canonical_cselib_val (addr_elt);
1337 mem_elt = canonical_cselib_val (mem_elt);
1339 /* Avoid duplicates. */
1340 for (l = mem_elt->locs; l; l = l->next)
1341 if (MEM_P (l->loc)
1342 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
1344 promote_debug_loc (l);
1345 return;
1348 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
1349 new_elt_loc_list (mem_elt,
1350 replace_equiv_address_nv (x, addr_elt->val_rtx));
1351 if (mem_elt->next_containing_mem == NULL)
1353 mem_elt->next_containing_mem = first_containing_mem;
1354 first_containing_mem = mem_elt;
1358 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1359 If CREATE, make a new one if we haven't seen it before. */
1361 static cselib_val *
1362 cselib_lookup_mem (rtx x, int create)
1364 enum machine_mode mode = GET_MODE (x);
1365 enum machine_mode addr_mode;
1366 cselib_val **slot;
1367 cselib_val *addr;
1368 cselib_val *mem_elt;
1369 struct elt_list *l;
1371 if (MEM_VOLATILE_P (x) || mode == BLKmode
1372 || !cselib_record_memory
1373 || (FLOAT_MODE_P (mode) && flag_float_store))
1374 return 0;
1376 addr_mode = GET_MODE (XEXP (x, 0));
1377 if (addr_mode == VOIDmode)
1378 addr_mode = Pmode;
1380 /* Look up the value for the address. */
1381 addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
1382 if (! addr)
1383 return 0;
1385 addr = canonical_cselib_val (addr);
1386 /* Find a value that describes a value of our mode at that address. */
1387 for (l = addr->addr_list; l; l = l->next)
1388 if (GET_MODE (l->elt->val_rtx) == mode)
1390 promote_debug_loc (l->elt->locs);
1391 return l->elt;
1394 if (! create)
1395 return 0;
1397 mem_elt = new_cselib_val (next_uid, mode, x);
1398 add_mem_for_addr (addr, mem_elt, x);
1399 slot = cselib_find_slot (mode, x, mem_elt->hash, INSERT, VOIDmode);
1400 *slot = mem_elt;
1401 return mem_elt;
1404 /* Search through the possible substitutions in P. We prefer a non reg
1405 substitution because this allows us to expand the tree further. If
1406 we find, just a reg, take the lowest regno. There may be several
1407 non-reg results, we just take the first one because they will all
1408 expand to the same place. */
1410 static rtx
1411 expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1412 int max_depth)
1414 rtx reg_result = NULL;
1415 unsigned int regno = UINT_MAX;
1416 struct elt_loc_list *p_in = p;
1418 for (; p; p = p->next)
1420 /* Return these right away to avoid returning stack pointer based
1421 expressions for frame pointer and vice versa, which is something
1422 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1423 for more details. */
1424 if (REG_P (p->loc)
1425 && (REGNO (p->loc) == STACK_POINTER_REGNUM
1426 || REGNO (p->loc) == FRAME_POINTER_REGNUM
1427 || REGNO (p->loc) == HARD_FRAME_POINTER_REGNUM
1428 || REGNO (p->loc) == cfa_base_preserved_regno))
1429 return p->loc;
1430 /* Avoid infinite recursion trying to expand a reg into a
1431 the same reg. */
1432 if ((REG_P (p->loc))
1433 && (REGNO (p->loc) < regno)
1434 && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
1436 reg_result = p->loc;
1437 regno = REGNO (p->loc);
1439 /* Avoid infinite recursion and do not try to expand the
1440 value. */
1441 else if (GET_CODE (p->loc) == VALUE
1442 && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1443 continue;
1444 else if (!REG_P (p->loc))
1446 rtx result, note;
1447 if (dump_file && (dump_flags & TDF_CSELIB))
1449 print_inline_rtx (dump_file, p->loc, 0);
1450 fprintf (dump_file, "\n");
1452 if (GET_CODE (p->loc) == LO_SUM
1453 && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1454 && p->setting_insn
1455 && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1456 && XEXP (note, 0) == XEXP (p->loc, 1))
1457 return XEXP (p->loc, 1);
1458 result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
1459 if (result)
1460 return result;
1465 if (regno != UINT_MAX)
1467 rtx result;
1468 if (dump_file && (dump_flags & TDF_CSELIB))
1469 fprintf (dump_file, "r%d\n", regno);
1471 result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
1472 if (result)
1473 return result;
1476 if (dump_file && (dump_flags & TDF_CSELIB))
1478 if (reg_result)
1480 print_inline_rtx (dump_file, reg_result, 0);
1481 fprintf (dump_file, "\n");
1483 else
1484 fprintf (dump_file, "NULL\n");
1486 return reg_result;
1490 /* Forward substitute and expand an expression out to its roots.
1491 This is the opposite of common subexpression. Because local value
1492 numbering is such a weak optimization, the expanded expression is
1493 pretty much unique (not from a pointer equals point of view but
1494 from a tree shape point of view.
1496 This function returns NULL if the expansion fails. The expansion
1497 will fail if there is no value number for one of the operands or if
1498 one of the operands has been overwritten between the current insn
1499 and the beginning of the basic block. For instance x has no
1500 expansion in:
1502 r1 <- r1 + 3
1503 x <- r1 + 8
1505 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1506 It is clear on return. */
1509 cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
1511 struct expand_value_data evd;
1513 evd.regs_active = regs_active;
1514 evd.callback = NULL;
1515 evd.callback_arg = NULL;
1516 evd.dummy = false;
1518 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1521 /* Same as cselib_expand_value_rtx, but using a callback to try to
1522 resolve some expressions. The CB function should return ORIG if it
1523 can't or does not want to deal with a certain RTX. Any other
1524 return value, including NULL, will be used as the expansion for
1525 VALUE, without any further changes. */
1528 cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1529 cselib_expand_callback cb, void *data)
1531 struct expand_value_data evd;
1533 evd.regs_active = regs_active;
1534 evd.callback = cb;
1535 evd.callback_arg = data;
1536 evd.dummy = false;
1538 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1541 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1542 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1543 would return NULL or non-NULL, without allocating new rtx. */
1545 bool
1546 cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1547 cselib_expand_callback cb, void *data)
1549 struct expand_value_data evd;
1551 evd.regs_active = regs_active;
1552 evd.callback = cb;
1553 evd.callback_arg = data;
1554 evd.dummy = true;
1556 return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1559 /* Internal implementation of cselib_expand_value_rtx and
1560 cselib_expand_value_rtx_cb. */
1562 static rtx
1563 cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1564 int max_depth)
1566 rtx copy, scopy;
1567 int i, j;
1568 RTX_CODE code;
1569 const char *format_ptr;
1570 enum machine_mode mode;
1572 code = GET_CODE (orig);
1574 /* For the context of dse, if we end up expand into a huge tree, we
1575 will not have a useful address, so we might as well just give up
1576 quickly. */
1577 if (max_depth <= 0)
1578 return NULL;
1580 switch (code)
1582 case REG:
1584 struct elt_list *l = REG_VALUES (REGNO (orig));
1586 if (l && l->elt == NULL)
1587 l = l->next;
1588 for (; l; l = l->next)
1589 if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1591 rtx result;
1592 unsigned regno = REGNO (orig);
1594 /* The only thing that we are not willing to do (this
1595 is requirement of dse and if others potential uses
1596 need this function we should add a parm to control
1597 it) is that we will not substitute the
1598 STACK_POINTER_REGNUM, FRAME_POINTER or the
1599 HARD_FRAME_POINTER.
1601 These expansions confuses the code that notices that
1602 stores into the frame go dead at the end of the
1603 function and that the frame is not effected by calls
1604 to subroutines. If you allow the
1605 STACK_POINTER_REGNUM substitution, then dse will
1606 think that parameter pushing also goes dead which is
1607 wrong. If you allow the FRAME_POINTER or the
1608 HARD_FRAME_POINTER then you lose the opportunity to
1609 make the frame assumptions. */
1610 if (regno == STACK_POINTER_REGNUM
1611 || regno == FRAME_POINTER_REGNUM
1612 || regno == HARD_FRAME_POINTER_REGNUM
1613 || regno == cfa_base_preserved_regno)
1614 return orig;
1616 bitmap_set_bit (evd->regs_active, regno);
1618 if (dump_file && (dump_flags & TDF_CSELIB))
1619 fprintf (dump_file, "expanding: r%d into: ", regno);
1621 result = expand_loc (l->elt->locs, evd, max_depth);
1622 bitmap_clear_bit (evd->regs_active, regno);
1624 if (result)
1625 return result;
1626 else
1627 return orig;
1631 CASE_CONST_ANY:
1632 case SYMBOL_REF:
1633 case CODE_LABEL:
1634 case PC:
1635 case CC0:
1636 case SCRATCH:
1637 /* SCRATCH must be shared because they represent distinct values. */
1638 return orig;
1639 case CLOBBER:
1640 if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1641 return orig;
1642 break;
1644 case CONST:
1645 if (shared_const_p (orig))
1646 return orig;
1647 break;
1649 case SUBREG:
1651 rtx subreg;
1653 if (evd->callback)
1655 subreg = evd->callback (orig, evd->regs_active, max_depth,
1656 evd->callback_arg);
1657 if (subreg != orig)
1658 return subreg;
1661 subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1662 max_depth - 1);
1663 if (!subreg)
1664 return NULL;
1665 scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1666 GET_MODE (SUBREG_REG (orig)),
1667 SUBREG_BYTE (orig));
1668 if (scopy == NULL
1669 || (GET_CODE (scopy) == SUBREG
1670 && !REG_P (SUBREG_REG (scopy))
1671 && !MEM_P (SUBREG_REG (scopy))))
1672 return NULL;
1674 return scopy;
1677 case VALUE:
1679 rtx result;
1681 if (dump_file && (dump_flags & TDF_CSELIB))
1683 fputs ("\nexpanding ", dump_file);
1684 print_rtl_single (dump_file, orig);
1685 fputs (" into...", dump_file);
1688 if (evd->callback)
1690 result = evd->callback (orig, evd->regs_active, max_depth,
1691 evd->callback_arg);
1693 if (result != orig)
1694 return result;
1697 result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
1698 return result;
1701 case DEBUG_EXPR:
1702 if (evd->callback)
1703 return evd->callback (orig, evd->regs_active, max_depth,
1704 evd->callback_arg);
1705 return orig;
1707 default:
1708 break;
1711 /* Copy the various flags, fields, and other information. We assume
1712 that all fields need copying, and then clear the fields that should
1713 not be copied. That is the sensible default behavior, and forces
1714 us to explicitly document why we are *not* copying a flag. */
1715 if (evd->dummy)
1716 copy = NULL;
1717 else
1718 copy = shallow_copy_rtx (orig);
1720 format_ptr = GET_RTX_FORMAT (code);
1722 for (i = 0; i < GET_RTX_LENGTH (code); i++)
1723 switch (*format_ptr++)
1725 case 'e':
1726 if (XEXP (orig, i) != NULL)
1728 rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1729 max_depth - 1);
1730 if (!result)
1731 return NULL;
1732 if (copy)
1733 XEXP (copy, i) = result;
1735 break;
1737 case 'E':
1738 case 'V':
1739 if (XVEC (orig, i) != NULL)
1741 if (copy)
1742 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1743 for (j = 0; j < XVECLEN (orig, i); j++)
1745 rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1746 evd, max_depth - 1);
1747 if (!result)
1748 return NULL;
1749 if (copy)
1750 XVECEXP (copy, i, j) = result;
1753 break;
1755 case 't':
1756 case 'w':
1757 case 'i':
1758 case 's':
1759 case 'S':
1760 case 'T':
1761 case 'u':
1762 case 'B':
1763 case '0':
1764 /* These are left unchanged. */
1765 break;
1767 default:
1768 gcc_unreachable ();
1771 if (evd->dummy)
1772 return orig;
1774 mode = GET_MODE (copy);
1775 /* If an operand has been simplified into CONST_INT, which doesn't
1776 have a mode and the mode isn't derivable from whole rtx's mode,
1777 try simplify_*_operation first with mode from original's operand
1778 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1779 scopy = copy;
1780 switch (GET_RTX_CLASS (code))
1782 case RTX_UNARY:
1783 if (CONST_INT_P (XEXP (copy, 0))
1784 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1786 scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1787 GET_MODE (XEXP (orig, 0)));
1788 if (scopy)
1789 return scopy;
1791 break;
1792 case RTX_COMM_ARITH:
1793 case RTX_BIN_ARITH:
1794 /* These expressions can derive operand modes from the whole rtx's mode. */
1795 break;
1796 case RTX_TERNARY:
1797 case RTX_BITFIELD_OPS:
1798 if (CONST_INT_P (XEXP (copy, 0))
1799 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1801 scopy = simplify_ternary_operation (code, mode,
1802 GET_MODE (XEXP (orig, 0)),
1803 XEXP (copy, 0), XEXP (copy, 1),
1804 XEXP (copy, 2));
1805 if (scopy)
1806 return scopy;
1808 break;
1809 case RTX_COMPARE:
1810 case RTX_COMM_COMPARE:
1811 if (CONST_INT_P (XEXP (copy, 0))
1812 && GET_MODE (XEXP (copy, 1)) == VOIDmode
1813 && (GET_MODE (XEXP (orig, 0)) != VOIDmode
1814 || GET_MODE (XEXP (orig, 1)) != VOIDmode))
1816 scopy = simplify_relational_operation (code, mode,
1817 (GET_MODE (XEXP (orig, 0))
1818 != VOIDmode)
1819 ? GET_MODE (XEXP (orig, 0))
1820 : GET_MODE (XEXP (orig, 1)),
1821 XEXP (copy, 0),
1822 XEXP (copy, 1));
1823 if (scopy)
1824 return scopy;
1826 break;
1827 default:
1828 break;
1830 scopy = simplify_rtx (copy);
1831 if (scopy)
1832 return scopy;
1833 return copy;
1836 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1837 with VALUE expressions. This way, it becomes independent of changes
1838 to registers and memory.
1839 X isn't actually modified; if modifications are needed, new rtl is
1840 allocated. However, the return value can share rtl with X.
1841 If X is within a MEM, MEMMODE must be the mode of the MEM. */
1844 cselib_subst_to_values (rtx x, enum machine_mode memmode)
1846 enum rtx_code code = GET_CODE (x);
1847 const char *fmt = GET_RTX_FORMAT (code);
1848 cselib_val *e;
1849 struct elt_list *l;
1850 rtx copy = x;
1851 int i;
1853 switch (code)
1855 case REG:
1856 l = REG_VALUES (REGNO (x));
1857 if (l && l->elt == NULL)
1858 l = l->next;
1859 for (; l; l = l->next)
1860 if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
1861 return l->elt->val_rtx;
1863 gcc_unreachable ();
1865 case MEM:
1866 e = cselib_lookup_mem (x, 0);
1867 /* This used to happen for autoincrements, but we deal with them
1868 properly now. Remove the if stmt for the next release. */
1869 if (! e)
1871 /* Assign a value that doesn't match any other. */
1872 e = new_cselib_val (next_uid, GET_MODE (x), x);
1874 return e->val_rtx;
1876 case ENTRY_VALUE:
1877 e = cselib_lookup (x, GET_MODE (x), 0, memmode);
1878 if (! e)
1879 break;
1880 return e->val_rtx;
1882 CASE_CONST_ANY:
1883 return x;
1885 case PRE_DEC:
1886 case PRE_INC:
1887 gcc_assert (memmode != VOIDmode);
1888 i = GET_MODE_SIZE (memmode);
1889 if (code == PRE_DEC)
1890 i = -i;
1891 return cselib_subst_to_values (plus_constant (GET_MODE (x),
1892 XEXP (x, 0), i),
1893 memmode);
1895 case PRE_MODIFY:
1896 gcc_assert (memmode != VOIDmode);
1897 return cselib_subst_to_values (XEXP (x, 1), memmode);
1899 case POST_DEC:
1900 case POST_INC:
1901 case POST_MODIFY:
1902 gcc_assert (memmode != VOIDmode);
1903 return cselib_subst_to_values (XEXP (x, 0), memmode);
1905 default:
1906 break;
1909 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1911 if (fmt[i] == 'e')
1913 rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
1915 if (t != XEXP (x, i))
1917 if (x == copy)
1918 copy = shallow_copy_rtx (x);
1919 XEXP (copy, i) = t;
1922 else if (fmt[i] == 'E')
1924 int j;
1926 for (j = 0; j < XVECLEN (x, i); j++)
1928 rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
1930 if (t != XVECEXP (x, i, j))
1932 if (XVEC (x, i) == XVEC (copy, i))
1934 if (x == copy)
1935 copy = shallow_copy_rtx (x);
1936 XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
1938 XVECEXP (copy, i, j) = t;
1944 return copy;
1947 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
1950 cselib_subst_to_values_from_insn (rtx x, enum machine_mode memmode, rtx insn)
1952 rtx ret;
1953 gcc_assert (!cselib_current_insn);
1954 cselib_current_insn = insn;
1955 ret = cselib_subst_to_values (x, memmode);
1956 cselib_current_insn = NULL;
1957 return ret;
1960 /* Look up the rtl expression X in our tables and return the value it
1961 has. If CREATE is zero, we return NULL if we don't know the value.
1962 Otherwise, we create a new one if possible, using mode MODE if X
1963 doesn't have a mode (i.e. because it's a constant). When X is part
1964 of an address, MEMMODE should be the mode of the enclosing MEM if
1965 we're tracking autoinc expressions. */
1967 static cselib_val *
1968 cselib_lookup_1 (rtx x, enum machine_mode mode,
1969 int create, enum machine_mode memmode)
1971 cselib_val **slot;
1972 cselib_val *e;
1973 unsigned int hashval;
1975 if (GET_MODE (x) != VOIDmode)
1976 mode = GET_MODE (x);
1978 if (GET_CODE (x) == VALUE)
1979 return CSELIB_VAL_PTR (x);
1981 if (REG_P (x))
1983 struct elt_list *l;
1984 unsigned int i = REGNO (x);
1986 l = REG_VALUES (i);
1987 if (l && l->elt == NULL)
1988 l = l->next;
1989 for (; l; l = l->next)
1990 if (mode == GET_MODE (l->elt->val_rtx))
1992 promote_debug_loc (l->elt->locs);
1993 return l->elt;
1996 if (! create)
1997 return 0;
1999 if (i < FIRST_PSEUDO_REGISTER)
2001 unsigned int n = hard_regno_nregs[i][mode];
2003 if (n > max_value_regs)
2004 max_value_regs = n;
2007 e = new_cselib_val (next_uid, GET_MODE (x), x);
2008 new_elt_loc_list (e, x);
2009 if (REG_VALUES (i) == 0)
2011 /* Maintain the invariant that the first entry of
2012 REG_VALUES, if present, must be the value used to set the
2013 register, or NULL. */
2014 used_regs[n_used_regs++] = i;
2015 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
2017 else if (cselib_preserve_constants
2018 && GET_MODE_CLASS (mode) == MODE_INT)
2020 /* During var-tracking, try harder to find equivalences
2021 for SUBREGs. If a setter sets say a DImode register
2022 and user uses that register only in SImode, add a lowpart
2023 subreg location. */
2024 struct elt_list *lwider = NULL;
2025 l = REG_VALUES (i);
2026 if (l && l->elt == NULL)
2027 l = l->next;
2028 for (; l; l = l->next)
2029 if (GET_MODE_CLASS (GET_MODE (l->elt->val_rtx)) == MODE_INT
2030 && GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2031 > GET_MODE_SIZE (mode)
2032 && (lwider == NULL
2033 || GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2034 < GET_MODE_SIZE (GET_MODE (lwider->elt->val_rtx))))
2036 struct elt_loc_list *el;
2037 if (i < FIRST_PSEUDO_REGISTER
2038 && hard_regno_nregs[i][GET_MODE (l->elt->val_rtx)] != 1)
2039 continue;
2040 for (el = l->elt->locs; el; el = el->next)
2041 if (!REG_P (el->loc))
2042 break;
2043 if (el)
2044 lwider = l;
2046 if (lwider)
2048 rtx sub = lowpart_subreg (mode, lwider->elt->val_rtx,
2049 GET_MODE (lwider->elt->val_rtx));
2050 if (sub)
2051 new_elt_loc_list (e, sub);
2054 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
2055 slot = cselib_find_slot (mode, x, e->hash, INSERT, memmode);
2056 *slot = e;
2057 return e;
2060 if (MEM_P (x))
2061 return cselib_lookup_mem (x, create);
2063 hashval = cselib_hash_rtx (x, create, memmode);
2064 /* Can't even create if hashing is not possible. */
2065 if (! hashval)
2066 return 0;
2068 slot = cselib_find_slot (mode, x, hashval,
2069 create ? INSERT : NO_INSERT, memmode);
2070 if (slot == 0)
2071 return 0;
2073 e = (cselib_val *) *slot;
2074 if (e)
2075 return e;
2077 e = new_cselib_val (hashval, mode, x);
2079 /* We have to fill the slot before calling cselib_subst_to_values:
2080 the hash table is inconsistent until we do so, and
2081 cselib_subst_to_values will need to do lookups. */
2082 *slot = e;
2083 new_elt_loc_list (e, cselib_subst_to_values (x, memmode));
2084 return e;
2087 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2089 cselib_val *
2090 cselib_lookup_from_insn (rtx x, enum machine_mode mode,
2091 int create, enum machine_mode memmode, rtx insn)
2093 cselib_val *ret;
2095 gcc_assert (!cselib_current_insn);
2096 cselib_current_insn = insn;
2098 ret = cselib_lookup (x, mode, create, memmode);
2100 cselib_current_insn = NULL;
2102 return ret;
2105 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2106 maintains invariants related with debug insns. */
2108 cselib_val *
2109 cselib_lookup (rtx x, enum machine_mode mode,
2110 int create, enum machine_mode memmode)
2112 cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
2114 /* ??? Should we return NULL if we're not to create an entry, the
2115 found loc is a debug loc and cselib_current_insn is not DEBUG?
2116 If so, we should also avoid converting val to non-DEBUG; probably
2117 easiest setting cselib_current_insn to NULL before the call
2118 above. */
2120 if (dump_file && (dump_flags & TDF_CSELIB))
2122 fputs ("cselib lookup ", dump_file);
2123 print_inline_rtx (dump_file, x, 2);
2124 fprintf (dump_file, " => %u:%u\n",
2125 ret ? ret->uid : 0,
2126 ret ? ret->hash : 0);
2129 return ret;
2132 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2133 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2134 is used to determine how many hard registers are being changed. If MODE
2135 is VOIDmode, then only REGNO is being changed; this is used when
2136 invalidating call clobbered registers across a call. */
2138 static void
2139 cselib_invalidate_regno (unsigned int regno, enum machine_mode mode)
2141 unsigned int endregno;
2142 unsigned int i;
2144 /* If we see pseudos after reload, something is _wrong_. */
2145 gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
2146 || reg_renumber[regno] < 0);
2148 /* Determine the range of registers that must be invalidated. For
2149 pseudos, only REGNO is affected. For hard regs, we must take MODE
2150 into account, and we must also invalidate lower register numbers
2151 if they contain values that overlap REGNO. */
2152 if (regno < FIRST_PSEUDO_REGISTER)
2154 gcc_assert (mode != VOIDmode);
2156 if (regno < max_value_regs)
2157 i = 0;
2158 else
2159 i = regno - max_value_regs;
2161 endregno = end_hard_regno (mode, regno);
2163 else
2165 i = regno;
2166 endregno = regno + 1;
2169 for (; i < endregno; i++)
2171 struct elt_list **l = &REG_VALUES (i);
2173 /* Go through all known values for this reg; if it overlaps the range
2174 we're invalidating, remove the value. */
2175 while (*l)
2177 cselib_val *v = (*l)->elt;
2178 bool had_locs;
2179 rtx setting_insn;
2180 struct elt_loc_list **p;
2181 unsigned int this_last = i;
2183 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
2184 this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
2186 if (this_last < regno || v == NULL
2187 || (v == cfa_base_preserved_val
2188 && i == cfa_base_preserved_regno))
2190 l = &(*l)->next;
2191 continue;
2194 /* We have an overlap. */
2195 if (*l == REG_VALUES (i))
2197 /* Maintain the invariant that the first entry of
2198 REG_VALUES, if present, must be the value used to set
2199 the register, or NULL. This is also nice because
2200 then we won't push the same regno onto user_regs
2201 multiple times. */
2202 (*l)->elt = NULL;
2203 l = &(*l)->next;
2205 else
2206 unchain_one_elt_list (l);
2208 v = canonical_cselib_val (v);
2210 had_locs = v->locs != NULL;
2211 setting_insn = v->locs ? v->locs->setting_insn : NULL;
2213 /* Now, we clear the mapping from value to reg. It must exist, so
2214 this code will crash intentionally if it doesn't. */
2215 for (p = &v->locs; ; p = &(*p)->next)
2217 rtx x = (*p)->loc;
2219 if (REG_P (x) && REGNO (x) == i)
2221 unchain_one_elt_loc_list (p);
2222 break;
2226 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2228 if (setting_insn && DEBUG_INSN_P (setting_insn))
2229 n_useless_debug_values++;
2230 else
2231 n_useless_values++;
2237 /* Invalidate any locations in the table which are changed because of a
2238 store to MEM_RTX. If this is called because of a non-const call
2239 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2241 static void
2242 cselib_invalidate_mem (rtx mem_rtx)
2244 cselib_val **vp, *v, *next;
2245 int num_mems = 0;
2246 rtx mem_addr;
2248 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2249 mem_rtx = canon_rtx (mem_rtx);
2251 vp = &first_containing_mem;
2252 for (v = *vp; v != &dummy_val; v = next)
2254 bool has_mem = false;
2255 struct elt_loc_list **p = &v->locs;
2256 bool had_locs = v->locs != NULL;
2257 rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
2259 while (*p)
2261 rtx x = (*p)->loc;
2262 cselib_val *addr;
2263 struct elt_list **mem_chain;
2265 /* MEMs may occur in locations only at the top level; below
2266 that every MEM or REG is substituted by its VALUE. */
2267 if (!MEM_P (x))
2269 p = &(*p)->next;
2270 continue;
2272 if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
2273 && ! canon_anti_dependence (x, false, mem_rtx,
2274 GET_MODE (mem_rtx), mem_addr))
2276 has_mem = true;
2277 num_mems++;
2278 p = &(*p)->next;
2279 continue;
2282 /* This one overlaps. */
2283 /* We must have a mapping from this MEM's address to the
2284 value (E). Remove that, too. */
2285 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
2286 addr = canonical_cselib_val (addr);
2287 gcc_checking_assert (v == canonical_cselib_val (v));
2288 mem_chain = &addr->addr_list;
2289 for (;;)
2291 cselib_val *canon = canonical_cselib_val ((*mem_chain)->elt);
2293 if (canon == v)
2295 unchain_one_elt_list (mem_chain);
2296 break;
2299 /* Record canonicalized elt. */
2300 (*mem_chain)->elt = canon;
2302 mem_chain = &(*mem_chain)->next;
2305 unchain_one_elt_loc_list (p);
2308 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2310 if (setting_insn && DEBUG_INSN_P (setting_insn))
2311 n_useless_debug_values++;
2312 else
2313 n_useless_values++;
2316 next = v->next_containing_mem;
2317 if (has_mem)
2319 *vp = v;
2320 vp = &(*vp)->next_containing_mem;
2322 else
2323 v->next_containing_mem = NULL;
2325 *vp = &dummy_val;
2328 /* Invalidate DEST, which is being assigned to or clobbered. */
2330 void
2331 cselib_invalidate_rtx (rtx dest)
2333 while (GET_CODE (dest) == SUBREG
2334 || GET_CODE (dest) == ZERO_EXTRACT
2335 || GET_CODE (dest) == STRICT_LOW_PART)
2336 dest = XEXP (dest, 0);
2338 if (REG_P (dest))
2339 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
2340 else if (MEM_P (dest))
2341 cselib_invalidate_mem (dest);
2344 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2346 static void
2347 cselib_invalidate_rtx_note_stores (rtx dest, const_rtx ignore ATTRIBUTE_UNUSED,
2348 void *data ATTRIBUTE_UNUSED)
2350 cselib_invalidate_rtx (dest);
2353 /* Record the result of a SET instruction. DEST is being set; the source
2354 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2355 describes its address. */
2357 static void
2358 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
2360 int dreg = REG_P (dest) ? (int) REGNO (dest) : -1;
2362 if (src_elt == 0 || side_effects_p (dest))
2363 return;
2365 if (dreg >= 0)
2367 if (dreg < FIRST_PSEUDO_REGISTER)
2369 unsigned int n = hard_regno_nregs[dreg][GET_MODE (dest)];
2371 if (n > max_value_regs)
2372 max_value_regs = n;
2375 if (REG_VALUES (dreg) == 0)
2377 used_regs[n_used_regs++] = dreg;
2378 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2380 else
2382 /* The register should have been invalidated. */
2383 gcc_assert (REG_VALUES (dreg)->elt == 0);
2384 REG_VALUES (dreg)->elt = src_elt;
2387 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2388 n_useless_values--;
2389 new_elt_loc_list (src_elt, dest);
2391 else if (MEM_P (dest) && dest_addr_elt != 0
2392 && cselib_record_memory)
2394 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2395 n_useless_values--;
2396 add_mem_for_addr (dest_addr_elt, src_elt, dest);
2400 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2402 void
2403 cselib_add_permanent_equiv (cselib_val *elt, rtx x, rtx insn)
2405 cselib_val *nelt;
2406 rtx save_cselib_current_insn = cselib_current_insn;
2408 gcc_checking_assert (elt);
2409 gcc_checking_assert (PRESERVED_VALUE_P (elt->val_rtx));
2410 gcc_checking_assert (!side_effects_p (x));
2412 cselib_current_insn = insn;
2414 nelt = cselib_lookup (x, GET_MODE (elt->val_rtx), 1, VOIDmode);
2416 if (nelt != elt)
2418 cselib_any_perm_equivs = true;
2420 if (!PRESERVED_VALUE_P (nelt->val_rtx))
2421 cselib_preserve_value (nelt);
2423 new_elt_loc_list (nelt, elt->val_rtx);
2426 cselib_current_insn = save_cselib_current_insn;
2429 /* Return TRUE if any permanent equivalences have been recorded since
2430 the table was last initialized. */
2431 bool
2432 cselib_have_permanent_equivalences (void)
2434 return cselib_any_perm_equivs;
2437 /* There is no good way to determine how many elements there can be
2438 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2439 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2441 struct cselib_record_autoinc_data
2443 struct cselib_set *sets;
2444 int n_sets;
2447 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2448 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2450 static int
2451 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
2452 rtx dest, rtx src, rtx srcoff, void *arg)
2454 struct cselib_record_autoinc_data *data;
2455 data = (struct cselib_record_autoinc_data *)arg;
2457 data->sets[data->n_sets].dest = dest;
2459 if (srcoff)
2460 data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
2461 else
2462 data->sets[data->n_sets].src = src;
2464 data->n_sets++;
2466 return -1;
2469 /* Record the effects of any sets and autoincs in INSN. */
2470 static void
2471 cselib_record_sets (rtx insn)
2473 int n_sets = 0;
2474 int i;
2475 struct cselib_set sets[MAX_SETS];
2476 rtx body = PATTERN (insn);
2477 rtx cond = 0;
2478 int n_sets_before_autoinc;
2479 struct cselib_record_autoinc_data data;
2481 body = PATTERN (insn);
2482 if (GET_CODE (body) == COND_EXEC)
2484 cond = COND_EXEC_TEST (body);
2485 body = COND_EXEC_CODE (body);
2488 /* Find all sets. */
2489 if (GET_CODE (body) == SET)
2491 sets[0].src = SET_SRC (body);
2492 sets[0].dest = SET_DEST (body);
2493 n_sets = 1;
2495 else if (GET_CODE (body) == PARALLEL)
2497 /* Look through the PARALLEL and record the values being
2498 set, if possible. Also handle any CLOBBERs. */
2499 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2501 rtx x = XVECEXP (body, 0, i);
2503 if (GET_CODE (x) == SET)
2505 sets[n_sets].src = SET_SRC (x);
2506 sets[n_sets].dest = SET_DEST (x);
2507 n_sets++;
2512 if (n_sets == 1
2513 && MEM_P (sets[0].src)
2514 && !cselib_record_memory
2515 && MEM_READONLY_P (sets[0].src))
2517 rtx note = find_reg_equal_equiv_note (insn);
2519 if (note && CONSTANT_P (XEXP (note, 0)))
2520 sets[0].src = XEXP (note, 0);
2523 data.sets = sets;
2524 data.n_sets = n_sets_before_autoinc = n_sets;
2525 for_each_inc_dec (&insn, cselib_record_autoinc_cb, &data);
2526 n_sets = data.n_sets;
2528 /* Look up the values that are read. Do this before invalidating the
2529 locations that are written. */
2530 for (i = 0; i < n_sets; i++)
2532 rtx dest = sets[i].dest;
2534 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2535 the low part after invalidating any knowledge about larger modes. */
2536 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2537 sets[i].dest = dest = XEXP (dest, 0);
2539 /* We don't know how to record anything but REG or MEM. */
2540 if (REG_P (dest)
2541 || (MEM_P (dest) && cselib_record_memory))
2543 rtx src = sets[i].src;
2544 if (cond)
2545 src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
2546 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
2547 if (MEM_P (dest))
2549 enum machine_mode address_mode = get_address_mode (dest);
2551 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
2552 address_mode, 1,
2553 GET_MODE (dest));
2555 else
2556 sets[i].dest_addr_elt = 0;
2560 if (cselib_record_sets_hook)
2561 cselib_record_sets_hook (insn, sets, n_sets);
2563 /* Invalidate all locations written by this insn. Note that the elts we
2564 looked up in the previous loop aren't affected, just some of their
2565 locations may go away. */
2566 note_stores (body, cselib_invalidate_rtx_note_stores, NULL);
2568 for (i = n_sets_before_autoinc; i < n_sets; i++)
2569 cselib_invalidate_rtx (sets[i].dest);
2571 /* If this is an asm, look for duplicate sets. This can happen when the
2572 user uses the same value as an output multiple times. This is valid
2573 if the outputs are not actually used thereafter. Treat this case as
2574 if the value isn't actually set. We do this by smashing the destination
2575 to pc_rtx, so that we won't record the value later. */
2576 if (n_sets >= 2 && asm_noperands (body) >= 0)
2578 for (i = 0; i < n_sets; i++)
2580 rtx dest = sets[i].dest;
2581 if (REG_P (dest) || MEM_P (dest))
2583 int j;
2584 for (j = i + 1; j < n_sets; j++)
2585 if (rtx_equal_p (dest, sets[j].dest))
2587 sets[i].dest = pc_rtx;
2588 sets[j].dest = pc_rtx;
2594 /* Now enter the equivalences in our tables. */
2595 for (i = 0; i < n_sets; i++)
2597 rtx dest = sets[i].dest;
2598 if (REG_P (dest)
2599 || (MEM_P (dest) && cselib_record_memory))
2600 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2604 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
2606 bool
2607 fp_setter_insn (rtx insn)
2609 rtx expr, pat = NULL_RTX;
2611 if (!RTX_FRAME_RELATED_P (insn))
2612 return false;
2614 expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2615 if (expr)
2616 pat = XEXP (expr, 0);
2617 if (!modified_in_p (hard_frame_pointer_rtx, pat ? pat : insn))
2618 return false;
2620 /* Don't return true for frame pointer restores in the epilogue. */
2621 if (find_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx))
2622 return false;
2623 return true;
2626 /* Record the effects of INSN. */
2628 void
2629 cselib_process_insn (rtx insn)
2631 int i;
2632 rtx x;
2634 cselib_current_insn = insn;
2636 /* Forget everything at a CODE_LABEL or a setjmp. */
2637 if ((LABEL_P (insn)
2638 || (CALL_P (insn)
2639 && find_reg_note (insn, REG_SETJMP, NULL)))
2640 && !cselib_preserve_constants)
2642 cselib_reset_table (next_uid);
2643 cselib_current_insn = NULL_RTX;
2644 return;
2647 if (! INSN_P (insn))
2649 cselib_current_insn = NULL_RTX;
2650 return;
2653 /* If this is a call instruction, forget anything stored in a
2654 call clobbered register, or, if this is not a const call, in
2655 memory. */
2656 if (CALL_P (insn))
2658 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2659 if (call_used_regs[i]
2660 || (REG_VALUES (i) && REG_VALUES (i)->elt
2661 && HARD_REGNO_CALL_PART_CLOBBERED (i,
2662 GET_MODE (REG_VALUES (i)->elt->val_rtx))))
2663 cselib_invalidate_regno (i, reg_raw_mode[i]);
2665 /* Since it is not clear how cselib is going to be used, be
2666 conservative here and treat looping pure or const functions
2667 as if they were regular functions. */
2668 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
2669 || !(RTL_CONST_OR_PURE_CALL_P (insn)))
2670 cselib_invalidate_mem (callmem);
2673 cselib_record_sets (insn);
2675 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2676 after we have processed the insn. */
2677 if (CALL_P (insn))
2679 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
2680 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
2681 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
2682 /* Flush evertything on setjmp. */
2683 if (cselib_preserve_constants
2684 && find_reg_note (insn, REG_SETJMP, NULL))
2686 cselib_preserve_only_values ();
2687 cselib_reset_table (next_uid);
2691 /* On setter of the hard frame pointer if frame_pointer_needed,
2692 invalidate stack_pointer_rtx, so that sp and {,h}fp based
2693 VALUEs are distinct. */
2694 if (reload_completed
2695 && frame_pointer_needed
2696 && fp_setter_insn (insn))
2697 cselib_invalidate_rtx (stack_pointer_rtx);
2699 cselib_current_insn = NULL_RTX;
2701 if (n_useless_values > MAX_USELESS_VALUES
2702 /* remove_useless_values is linear in the hash table size. Avoid
2703 quadratic behavior for very large hashtables with very few
2704 useless elements. */
2705 && ((unsigned int)n_useless_values
2706 > (cselib_hash_table.elements () - n_debug_values) / 4))
2707 remove_useless_values ();
2710 /* Initialize cselib for one pass. The caller must also call
2711 init_alias_analysis. */
2713 void
2714 cselib_init (int record_what)
2716 elt_list_pool = create_alloc_pool ("elt_list",
2717 sizeof (struct elt_list), 10);
2718 elt_loc_list_pool = create_alloc_pool ("elt_loc_list",
2719 sizeof (struct elt_loc_list), 10);
2720 cselib_val_pool = create_alloc_pool ("cselib_val_list",
2721 sizeof (cselib_val), 10);
2722 value_pool = create_alloc_pool ("value", RTX_CODE_SIZE (VALUE), 100);
2723 cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
2724 cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
2725 cselib_any_perm_equivs = false;
2727 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2728 see canon_true_dependence. This is only created once. */
2729 if (! callmem)
2730 callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2732 cselib_nregs = max_reg_num ();
2734 /* We preserve reg_values to allow expensive clearing of the whole thing.
2735 Reallocate it however if it happens to be too large. */
2736 if (!reg_values || reg_values_size < cselib_nregs
2737 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
2739 free (reg_values);
2740 /* Some space for newly emit instructions so we don't end up
2741 reallocating in between passes. */
2742 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
2743 reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
2745 used_regs = XNEWVEC (unsigned int, cselib_nregs);
2746 n_used_regs = 0;
2747 cselib_hash_table.create (31);
2748 if (cselib_preserve_constants)
2749 cselib_preserved_hash_table.create (31);
2750 next_uid = 1;
2753 /* Called when the current user is done with cselib. */
2755 void
2756 cselib_finish (void)
2758 bool preserved = cselib_preserve_constants;
2759 cselib_discard_hook = NULL;
2760 cselib_preserve_constants = false;
2761 cselib_any_perm_equivs = false;
2762 cfa_base_preserved_val = NULL;
2763 cfa_base_preserved_regno = INVALID_REGNUM;
2764 free_alloc_pool (elt_list_pool);
2765 free_alloc_pool (elt_loc_list_pool);
2766 free_alloc_pool (cselib_val_pool);
2767 free_alloc_pool (value_pool);
2768 cselib_clear_table ();
2769 cselib_hash_table.dispose ();
2770 if (preserved)
2771 cselib_preserved_hash_table.dispose ();
2772 free (used_regs);
2773 used_regs = 0;
2774 n_useless_values = 0;
2775 n_useless_debug_values = 0;
2776 n_debug_values = 0;
2777 next_uid = 0;
2780 /* Dump the cselib_val *X to FILE *OUT. */
2783 dump_cselib_val (cselib_val **x, FILE *out)
2785 cselib_val *v = *x;
2786 bool need_lf = true;
2788 print_inline_rtx (out, v->val_rtx, 0);
2790 if (v->locs)
2792 struct elt_loc_list *l = v->locs;
2793 if (need_lf)
2795 fputc ('\n', out);
2796 need_lf = false;
2798 fputs (" locs:", out);
2801 if (l->setting_insn)
2802 fprintf (out, "\n from insn %i ",
2803 INSN_UID (l->setting_insn));
2804 else
2805 fprintf (out, "\n ");
2806 print_inline_rtx (out, l->loc, 4);
2808 while ((l = l->next));
2809 fputc ('\n', out);
2811 else
2813 fputs (" no locs", out);
2814 need_lf = true;
2817 if (v->addr_list)
2819 struct elt_list *e = v->addr_list;
2820 if (need_lf)
2822 fputc ('\n', out);
2823 need_lf = false;
2825 fputs (" addr list:", out);
2828 fputs ("\n ", out);
2829 print_inline_rtx (out, e->elt->val_rtx, 2);
2831 while ((e = e->next));
2832 fputc ('\n', out);
2834 else
2836 fputs (" no addrs", out);
2837 need_lf = true;
2840 if (v->next_containing_mem == &dummy_val)
2841 fputs (" last mem\n", out);
2842 else if (v->next_containing_mem)
2844 fputs (" next mem ", out);
2845 print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
2846 fputc ('\n', out);
2848 else if (need_lf)
2849 fputc ('\n', out);
2851 return 1;
2854 /* Dump to OUT everything in the CSELIB table. */
2856 void
2857 dump_cselib_table (FILE *out)
2859 fprintf (out, "cselib hash table:\n");
2860 cselib_hash_table.traverse <FILE *, dump_cselib_val> (out);
2861 fprintf (out, "cselib preserved hash table:\n");
2862 cselib_preserved_hash_table.traverse <FILE *, dump_cselib_val> (out);
2863 if (first_containing_mem != &dummy_val)
2865 fputs ("first mem ", out);
2866 print_inline_rtx (out, first_containing_mem->val_rtx, 2);
2867 fputc ('\n', out);
2869 fprintf (out, "next uid %i\n", next_uid);
2872 #include "gt-cselib.h"