Daily bump.
[official-gcc.git] / gcc / cselib.c
blob30845edb4f6234abef56fa3eb0bc5a0b7cc6d1dc
1 /* Common subexpression elimination library for GNU compiler.
2 Copyright (C) 1987-2015 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"
24 #include "rtl.h"
25 #include "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"/* FIXME: For hashing DEBUG_EXPR & friends. */
35 #include "tm_p.h"
36 #include "regs.h"
37 #include "hard-reg-set.h"
38 #include "flags.h"
39 #include "insn-config.h"
40 #include "recog.h"
41 #include "hashtab.h"
42 #include "input.h"
43 #include "function.h"
44 #include "emit-rtl.h"
45 #include "diagnostic-core.h"
46 #include "ggc.h"
47 #include "hash-table.h"
48 #include "dumpfile.h"
49 #include "cselib.h"
50 #include "predict.h"
51 #include "basic-block.h"
52 #include "valtrack.h"
53 #include "params.h"
54 #include "alloc-pool.h"
55 #include "target.h"
56 #include "bitmap.h"
58 /* A list of cselib_val structures. */
59 struct elt_list {
60 struct elt_list *next;
61 cselib_val *elt;
64 static bool cselib_record_memory;
65 static bool cselib_preserve_constants;
66 static bool cselib_any_perm_equivs;
67 static inline void promote_debug_loc (struct elt_loc_list *l);
68 static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
69 static void new_elt_loc_list (cselib_val *, rtx);
70 static void unchain_one_value (cselib_val *);
71 static void unchain_one_elt_list (struct elt_list **);
72 static void unchain_one_elt_loc_list (struct elt_loc_list **);
73 static void remove_useless_values (void);
74 static int rtx_equal_for_cselib_1 (rtx, rtx, machine_mode);
75 static unsigned int cselib_hash_rtx (rtx, int, machine_mode);
76 static cselib_val *new_cselib_val (unsigned int, machine_mode, rtx);
77 static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
78 static cselib_val *cselib_lookup_mem (rtx, int);
79 static void cselib_invalidate_regno (unsigned int, machine_mode);
80 static void cselib_invalidate_mem (rtx);
81 static void cselib_record_set (rtx, cselib_val *, cselib_val *);
82 static void cselib_record_sets (rtx_insn *);
84 struct expand_value_data
86 bitmap regs_active;
87 cselib_expand_callback callback;
88 void *callback_arg;
89 bool dummy;
92 static rtx cselib_expand_value_rtx_1 (rtx, struct expand_value_data *, int);
94 /* There are three ways in which cselib can look up an rtx:
95 - for a REG, the reg_values table (which is indexed by regno) is used
96 - for a MEM, we recursively look up its address and then follow the
97 addr_list of that value
98 - for everything else, we compute a hash value and go through the hash
99 table. Since different rtx's can still have the same hash value,
100 this involves walking the table entries for a given value and comparing
101 the locations of the entries with the rtx we are looking up. */
103 struct cselib_hasher : typed_noop_remove <cselib_val>
105 typedef cselib_val *value_type;
106 struct key {
107 /* The rtx value and its mode (needed separately for constant
108 integers). */
109 machine_mode mode;
110 rtx x;
111 /* The mode of the contaning MEM, if any, otherwise VOIDmode. */
112 machine_mode memmode;
114 typedef key *compare_type;
115 static inline hashval_t hash (const cselib_val *);
116 static inline bool equal (const cselib_val *, const key *);
119 /* The hash function for our hash table. The value is always computed with
120 cselib_hash_rtx when adding an element; this function just extracts the
121 hash value from a cselib_val structure. */
123 inline hashval_t
124 cselib_hasher::hash (const cselib_val *v)
126 return v->hash;
129 /* The equality test for our hash table. The first argument V is a table
130 element (i.e. a cselib_val), while the second arg X is an rtx. We know
131 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
132 CONST of an appropriate mode. */
134 inline bool
135 cselib_hasher::equal (const cselib_val *v, const key *x_arg)
137 struct elt_loc_list *l;
138 rtx x = x_arg->x;
139 machine_mode mode = x_arg->mode;
140 machine_mode memmode = x_arg->memmode;
142 if (mode != GET_MODE (v->val_rtx))
143 return false;
145 if (GET_CODE (x) == VALUE)
146 return x == v->val_rtx;
148 /* We don't guarantee that distinct rtx's have different hash values,
149 so we need to do a comparison. */
150 for (l = v->locs; l; l = l->next)
151 if (rtx_equal_for_cselib_1 (l->loc, x, memmode))
153 promote_debug_loc (l);
154 return true;
157 return false;
160 /* A table that enables us to look up elts by their value. */
161 static hash_table<cselib_hasher> *cselib_hash_table;
163 /* A table to hold preserved values. */
164 static hash_table<cselib_hasher> *cselib_preserved_hash_table;
166 /* This is a global so we don't have to pass this through every function.
167 It is used in new_elt_loc_list to set SETTING_INSN. */
168 static rtx_insn *cselib_current_insn;
170 /* The unique id that the next create value will take. */
171 static unsigned int next_uid;
173 /* The number of registers we had when the varrays were last resized. */
174 static unsigned int cselib_nregs;
176 /* Count values without known locations, or with only locations that
177 wouldn't have been known except for debug insns. Whenever this
178 grows too big, we remove these useless values from the table.
180 Counting values with only debug values is a bit tricky. We don't
181 want to increment n_useless_values when we create a value for a
182 debug insn, for this would get n_useless_values out of sync, but we
183 want increment it if all locs in the list that were ever referenced
184 in nondebug insns are removed from the list.
186 In the general case, once we do that, we'd have to stop accepting
187 nondebug expressions in the loc list, to avoid having two values
188 equivalent that, without debug insns, would have been made into
189 separate values. However, because debug insns never introduce
190 equivalences themselves (no assignments), the only means for
191 growing loc lists is through nondebug assignments. If the locs
192 also happen to be referenced in debug insns, it will work just fine.
194 A consequence of this is that there's at most one debug-only loc in
195 each loc list. If we keep it in the first entry, testing whether
196 we have a debug-only loc list takes O(1).
198 Furthermore, since any additional entry in a loc list containing a
199 debug loc would have to come from an assignment (nondebug) that
200 references both the initial debug loc and the newly-equivalent loc,
201 the initial debug loc would be promoted to a nondebug loc, and the
202 loc list would not contain debug locs any more.
204 So the only case we have to be careful with in order to keep
205 n_useless_values in sync between debug and nondebug compilations is
206 to avoid incrementing n_useless_values when removing the single loc
207 from a value that turns out to not appear outside debug values. We
208 increment n_useless_debug_values instead, and leave such values
209 alone until, for other reasons, we garbage-collect useless
210 values. */
211 static int n_useless_values;
212 static int n_useless_debug_values;
214 /* Count values whose locs have been taken exclusively from debug
215 insns for the entire life of the value. */
216 static int n_debug_values;
218 /* Number of useless values before we remove them from the hash table. */
219 #define MAX_USELESS_VALUES 32
221 /* This table maps from register number to values. It does not
222 contain pointers to cselib_val structures, but rather elt_lists.
223 The purpose is to be able to refer to the same register in
224 different modes. The first element of the list defines the mode in
225 which the register was set; if the mode is unknown or the value is
226 no longer valid in that mode, ELT will be NULL for the first
227 element. */
228 static struct elt_list **reg_values;
229 static unsigned int reg_values_size;
230 #define REG_VALUES(i) reg_values[i]
232 /* The largest number of hard regs used by any entry added to the
233 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
234 static unsigned int max_value_regs;
236 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
237 in cselib_clear_table() for fast emptying. */
238 static unsigned int *used_regs;
239 static unsigned int n_used_regs;
241 /* We pass this to cselib_invalidate_mem to invalidate all of
242 memory for a non-const call instruction. */
243 static GTY(()) rtx callmem;
245 /* Set by discard_useless_locs if it deleted the last location of any
246 value. */
247 static int values_became_useless;
249 /* Used as stop element of the containing_mem list so we can check
250 presence in the list by checking the next pointer. */
251 static cselib_val dummy_val;
253 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
254 that is constant through the whole function and should never be
255 eliminated. */
256 static cselib_val *cfa_base_preserved_val;
257 static unsigned int cfa_base_preserved_regno = INVALID_REGNUM;
259 /* Used to list all values that contain memory reference.
260 May or may not contain the useless values - the list is compacted
261 each time memory is invalidated. */
262 static cselib_val *first_containing_mem = &dummy_val;
263 static alloc_pool elt_loc_list_pool, elt_list_pool, cselib_val_pool, value_pool;
265 /* If nonnull, cselib will call this function before freeing useless
266 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
267 void (*cselib_discard_hook) (cselib_val *);
269 /* If nonnull, cselib will call this function before recording sets or
270 even clobbering outputs of INSN. All the recorded sets will be
271 represented in the array sets[n_sets]. new_val_min can be used to
272 tell whether values present in sets are introduced by this
273 instruction. */
274 void (*cselib_record_sets_hook) (rtx_insn *insn, struct cselib_set *sets,
275 int n_sets);
277 #define PRESERVED_VALUE_P(RTX) \
278 (RTL_FLAG_CHECK1 ("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
280 #define SP_BASED_VALUE_P(RTX) \
281 (RTL_FLAG_CHECK1 ("SP_BASED_VALUE_P", (RTX), VALUE)->jump)
285 /* Allocate a struct elt_list and fill in its two elements with the
286 arguments. */
288 static inline struct elt_list *
289 new_elt_list (struct elt_list *next, cselib_val *elt)
291 struct elt_list *el;
292 el = (struct elt_list *) pool_alloc (elt_list_pool);
293 el->next = next;
294 el->elt = elt;
295 return el;
298 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
299 list. */
301 static inline void
302 new_elt_loc_list (cselib_val *val, rtx loc)
304 struct elt_loc_list *el, *next = val->locs;
306 gcc_checking_assert (!next || !next->setting_insn
307 || !DEBUG_INSN_P (next->setting_insn)
308 || cselib_current_insn == next->setting_insn);
310 /* If we're creating the first loc in a debug insn context, we've
311 just created a debug value. Count it. */
312 if (!next && cselib_current_insn && DEBUG_INSN_P (cselib_current_insn))
313 n_debug_values++;
315 val = canonical_cselib_val (val);
316 next = val->locs;
318 if (GET_CODE (loc) == VALUE)
320 loc = canonical_cselib_val (CSELIB_VAL_PTR (loc))->val_rtx;
322 gcc_checking_assert (PRESERVED_VALUE_P (loc)
323 == PRESERVED_VALUE_P (val->val_rtx));
325 if (val->val_rtx == loc)
326 return;
327 else if (val->uid > CSELIB_VAL_PTR (loc)->uid)
329 /* Reverse the insertion. */
330 new_elt_loc_list (CSELIB_VAL_PTR (loc), val->val_rtx);
331 return;
334 gcc_checking_assert (val->uid < CSELIB_VAL_PTR (loc)->uid);
336 if (CSELIB_VAL_PTR (loc)->locs)
338 /* Bring all locs from LOC to VAL. */
339 for (el = CSELIB_VAL_PTR (loc)->locs; el->next; el = el->next)
341 /* Adjust values that have LOC as canonical so that VAL
342 becomes their canonical. */
343 if (el->loc && GET_CODE (el->loc) == VALUE)
345 gcc_checking_assert (CSELIB_VAL_PTR (el->loc)->locs->loc
346 == loc);
347 CSELIB_VAL_PTR (el->loc)->locs->loc = val->val_rtx;
350 el->next = val->locs;
351 next = val->locs = CSELIB_VAL_PTR (loc)->locs;
354 if (CSELIB_VAL_PTR (loc)->addr_list)
356 /* Bring in addr_list into canonical node. */
357 struct elt_list *last = CSELIB_VAL_PTR (loc)->addr_list;
358 while (last->next)
359 last = last->next;
360 last->next = val->addr_list;
361 val->addr_list = CSELIB_VAL_PTR (loc)->addr_list;
362 CSELIB_VAL_PTR (loc)->addr_list = NULL;
365 if (CSELIB_VAL_PTR (loc)->next_containing_mem != NULL
366 && val->next_containing_mem == NULL)
368 /* Add VAL to the containing_mem list after LOC. LOC will
369 be removed when we notice it doesn't contain any
370 MEMs. */
371 val->next_containing_mem = CSELIB_VAL_PTR (loc)->next_containing_mem;
372 CSELIB_VAL_PTR (loc)->next_containing_mem = val;
375 /* Chain LOC back to VAL. */
376 el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
377 el->loc = val->val_rtx;
378 el->setting_insn = cselib_current_insn;
379 el->next = NULL;
380 CSELIB_VAL_PTR (loc)->locs = el;
383 el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
384 el->loc = loc;
385 el->setting_insn = cselib_current_insn;
386 el->next = next;
387 val->locs = el;
390 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
391 originating from a debug insn, maintaining the debug values
392 count. */
394 static inline void
395 promote_debug_loc (struct elt_loc_list *l)
397 if (l && l->setting_insn && DEBUG_INSN_P (l->setting_insn)
398 && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
400 n_debug_values--;
401 l->setting_insn = cselib_current_insn;
402 if (cselib_preserve_constants && l->next)
404 gcc_assert (l->next->setting_insn
405 && DEBUG_INSN_P (l->next->setting_insn)
406 && !l->next->next);
407 l->next->setting_insn = cselib_current_insn;
409 else
410 gcc_assert (!l->next);
414 /* The elt_list at *PL is no longer needed. Unchain it and free its
415 storage. */
417 static inline void
418 unchain_one_elt_list (struct elt_list **pl)
420 struct elt_list *l = *pl;
422 *pl = l->next;
423 pool_free (elt_list_pool, l);
426 /* Likewise for elt_loc_lists. */
428 static void
429 unchain_one_elt_loc_list (struct elt_loc_list **pl)
431 struct elt_loc_list *l = *pl;
433 *pl = l->next;
434 pool_free (elt_loc_list_pool, l);
437 /* Likewise for cselib_vals. This also frees the addr_list associated with
438 V. */
440 static void
441 unchain_one_value (cselib_val *v)
443 while (v->addr_list)
444 unchain_one_elt_list (&v->addr_list);
446 pool_free (cselib_val_pool, v);
449 /* Remove all entries from the hash table. Also used during
450 initialization. */
452 void
453 cselib_clear_table (void)
455 cselib_reset_table (1);
458 /* Return TRUE if V is a constant, a function invariant or a VALUE
459 equivalence; FALSE otherwise. */
461 static bool
462 invariant_or_equiv_p (cselib_val *v)
464 struct elt_loc_list *l;
466 if (v == cfa_base_preserved_val)
467 return true;
469 /* Keep VALUE equivalences around. */
470 for (l = v->locs; l; l = l->next)
471 if (GET_CODE (l->loc) == VALUE)
472 return true;
474 if (v->locs != NULL
475 && v->locs->next == NULL)
477 if (CONSTANT_P (v->locs->loc)
478 && (GET_CODE (v->locs->loc) != CONST
479 || !references_value_p (v->locs->loc, 0)))
480 return true;
481 /* Although a debug expr may be bound to different expressions,
482 we can preserve it as if it was constant, to get unification
483 and proper merging within var-tracking. */
484 if (GET_CODE (v->locs->loc) == DEBUG_EXPR
485 || GET_CODE (v->locs->loc) == DEBUG_IMPLICIT_PTR
486 || GET_CODE (v->locs->loc) == ENTRY_VALUE
487 || GET_CODE (v->locs->loc) == DEBUG_PARAMETER_REF)
488 return true;
490 /* (plus (value V) (const_int C)) is invariant iff V is invariant. */
491 if (GET_CODE (v->locs->loc) == PLUS
492 && CONST_INT_P (XEXP (v->locs->loc, 1))
493 && GET_CODE (XEXP (v->locs->loc, 0)) == VALUE
494 && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v->locs->loc, 0))))
495 return true;
498 return false;
501 /* Remove from hash table all VALUEs except constants, function
502 invariants and VALUE equivalences. */
505 preserve_constants_and_equivs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
507 cselib_val *v = *x;
509 if (invariant_or_equiv_p (v))
511 cselib_hasher::key lookup = {
512 GET_MODE (v->val_rtx), v->val_rtx, VOIDmode
514 cselib_val **slot
515 = cselib_preserved_hash_table->find_slot_with_hash (&lookup,
516 v->hash, INSERT);
517 gcc_assert (!*slot);
518 *slot = v;
521 cselib_hash_table->clear_slot (x);
523 return 1;
526 /* Remove all entries from the hash table, arranging for the next
527 value to be numbered NUM. */
529 void
530 cselib_reset_table (unsigned int num)
532 unsigned int i;
534 max_value_regs = 0;
536 if (cfa_base_preserved_val)
538 unsigned int regno = cfa_base_preserved_regno;
539 unsigned int new_used_regs = 0;
540 for (i = 0; i < n_used_regs; i++)
541 if (used_regs[i] == regno)
543 new_used_regs = 1;
544 continue;
546 else
547 REG_VALUES (used_regs[i]) = 0;
548 gcc_assert (new_used_regs == 1);
549 n_used_regs = new_used_regs;
550 used_regs[0] = regno;
551 max_value_regs
552 = hard_regno_nregs[regno][GET_MODE (cfa_base_preserved_val->locs->loc)];
554 else
556 for (i = 0; i < n_used_regs; i++)
557 REG_VALUES (used_regs[i]) = 0;
558 n_used_regs = 0;
561 if (cselib_preserve_constants)
562 cselib_hash_table->traverse <void *, preserve_constants_and_equivs>
563 (NULL);
564 else
566 cselib_hash_table->empty ();
567 gcc_checking_assert (!cselib_any_perm_equivs);
570 n_useless_values = 0;
571 n_useless_debug_values = 0;
572 n_debug_values = 0;
574 next_uid = num;
576 first_containing_mem = &dummy_val;
579 /* Return the number of the next value that will be generated. */
581 unsigned int
582 cselib_get_next_uid (void)
584 return next_uid;
587 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
588 INSERTing if requested. When X is part of the address of a MEM,
589 MEMMODE should specify the mode of the MEM. */
591 static cselib_val **
592 cselib_find_slot (machine_mode mode, rtx x, hashval_t hash,
593 enum insert_option insert, machine_mode memmode)
595 cselib_val **slot = NULL;
596 cselib_hasher::key lookup = { mode, x, memmode };
597 if (cselib_preserve_constants)
598 slot = cselib_preserved_hash_table->find_slot_with_hash (&lookup, hash,
599 NO_INSERT);
600 if (!slot)
601 slot = cselib_hash_table->find_slot_with_hash (&lookup, hash, insert);
602 return slot;
605 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
606 only return true for values which point to a cselib_val whose value
607 element has been set to zero, which implies the cselib_val will be
608 removed. */
611 references_value_p (const_rtx x, int only_useless)
613 const enum rtx_code code = GET_CODE (x);
614 const char *fmt = GET_RTX_FORMAT (code);
615 int i, j;
617 if (GET_CODE (x) == VALUE
618 && (! only_useless ||
619 (CSELIB_VAL_PTR (x)->locs == 0 && !PRESERVED_VALUE_P (x))))
620 return 1;
622 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
624 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
625 return 1;
626 else if (fmt[i] == 'E')
627 for (j = 0; j < XVECLEN (x, i); j++)
628 if (references_value_p (XVECEXP (x, i, j), only_useless))
629 return 1;
632 return 0;
635 /* For all locations found in X, delete locations that reference useless
636 values (i.e. values without any location). Called through
637 htab_traverse. */
640 discard_useless_locs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
642 cselib_val *v = *x;
643 struct elt_loc_list **p = &v->locs;
644 bool had_locs = v->locs != NULL;
645 rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
647 while (*p)
649 if (references_value_p ((*p)->loc, 1))
650 unchain_one_elt_loc_list (p);
651 else
652 p = &(*p)->next;
655 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
657 if (setting_insn && DEBUG_INSN_P (setting_insn))
658 n_useless_debug_values++;
659 else
660 n_useless_values++;
661 values_became_useless = 1;
663 return 1;
666 /* If X is a value with no locations, remove it from the hashtable. */
669 discard_useless_values (cselib_val **x, void *info ATTRIBUTE_UNUSED)
671 cselib_val *v = *x;
673 if (v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
675 if (cselib_discard_hook)
676 cselib_discard_hook (v);
678 CSELIB_VAL_PTR (v->val_rtx) = NULL;
679 cselib_hash_table->clear_slot (x);
680 unchain_one_value (v);
681 n_useless_values--;
684 return 1;
687 /* Clean out useless values (i.e. those which no longer have locations
688 associated with them) from the hash table. */
690 static void
691 remove_useless_values (void)
693 cselib_val **p, *v;
695 /* First pass: eliminate locations that reference the value. That in
696 turn can make more values useless. */
699 values_became_useless = 0;
700 cselib_hash_table->traverse <void *, discard_useless_locs> (NULL);
702 while (values_became_useless);
704 /* Second pass: actually remove the values. */
706 p = &first_containing_mem;
707 for (v = *p; v != &dummy_val; v = v->next_containing_mem)
708 if (v->locs && v == canonical_cselib_val (v))
710 *p = v;
711 p = &(*p)->next_containing_mem;
713 *p = &dummy_val;
715 n_useless_values += n_useless_debug_values;
716 n_debug_values -= n_useless_debug_values;
717 n_useless_debug_values = 0;
719 cselib_hash_table->traverse <void *, discard_useless_values> (NULL);
721 gcc_assert (!n_useless_values);
724 /* Arrange for a value to not be removed from the hash table even if
725 it becomes useless. */
727 void
728 cselib_preserve_value (cselib_val *v)
730 PRESERVED_VALUE_P (v->val_rtx) = 1;
733 /* Test whether a value is preserved. */
735 bool
736 cselib_preserved_value_p (cselib_val *v)
738 return PRESERVED_VALUE_P (v->val_rtx);
741 /* Arrange for a REG value to be assumed constant through the whole function,
742 never invalidated and preserved across cselib_reset_table calls. */
744 void
745 cselib_preserve_cfa_base_value (cselib_val *v, unsigned int regno)
747 if (cselib_preserve_constants
748 && v->locs
749 && REG_P (v->locs->loc))
751 cfa_base_preserved_val = v;
752 cfa_base_preserved_regno = regno;
756 /* Clean all non-constant expressions in the hash table, but retain
757 their values. */
759 void
760 cselib_preserve_only_values (void)
762 int i;
764 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
765 cselib_invalidate_regno (i, reg_raw_mode[i]);
767 cselib_invalidate_mem (callmem);
769 remove_useless_values ();
771 gcc_assert (first_containing_mem == &dummy_val);
774 /* Arrange for a value to be marked as based on stack pointer
775 for find_base_term purposes. */
777 void
778 cselib_set_value_sp_based (cselib_val *v)
780 SP_BASED_VALUE_P (v->val_rtx) = 1;
783 /* Test whether a value is based on stack pointer for
784 find_base_term purposes. */
786 bool
787 cselib_sp_based_value_p (cselib_val *v)
789 return SP_BASED_VALUE_P (v->val_rtx);
792 /* Return the mode in which a register was last set. If X is not a
793 register, return its mode. If the mode in which the register was
794 set is not known, or the value was already clobbered, return
795 VOIDmode. */
797 machine_mode
798 cselib_reg_set_mode (const_rtx x)
800 if (!REG_P (x))
801 return GET_MODE (x);
803 if (REG_VALUES (REGNO (x)) == NULL
804 || REG_VALUES (REGNO (x))->elt == NULL)
805 return VOIDmode;
807 return GET_MODE (REG_VALUES (REGNO (x))->elt->val_rtx);
810 /* Return nonzero if we can prove that X and Y contain the same value, taking
811 our gathered information into account. */
814 rtx_equal_for_cselib_p (rtx x, rtx y)
816 return rtx_equal_for_cselib_1 (x, y, VOIDmode);
819 /* If x is a PLUS or an autoinc operation, expand the operation,
820 storing the offset, if any, in *OFF. */
822 static rtx
823 autoinc_split (rtx x, rtx *off, machine_mode memmode)
825 switch (GET_CODE (x))
827 case PLUS:
828 *off = XEXP (x, 1);
829 return XEXP (x, 0);
831 case PRE_DEC:
832 if (memmode == VOIDmode)
833 return x;
835 *off = GEN_INT (-GET_MODE_SIZE (memmode));
836 return XEXP (x, 0);
837 break;
839 case PRE_INC:
840 if (memmode == VOIDmode)
841 return x;
843 *off = GEN_INT (GET_MODE_SIZE (memmode));
844 return XEXP (x, 0);
846 case PRE_MODIFY:
847 return XEXP (x, 1);
849 case POST_DEC:
850 case POST_INC:
851 case POST_MODIFY:
852 return XEXP (x, 0);
854 default:
855 return x;
859 /* Return nonzero if we can prove that X and Y contain the same value,
860 taking our gathered information into account. MEMMODE holds the
861 mode of the enclosing MEM, if any, as required to deal with autoinc
862 addressing modes. If X and Y are not (known to be) part of
863 addresses, MEMMODE should be VOIDmode. */
865 static int
866 rtx_equal_for_cselib_1 (rtx x, rtx y, machine_mode memmode)
868 enum rtx_code code;
869 const char *fmt;
870 int i;
872 if (REG_P (x) || MEM_P (x))
874 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0, memmode);
876 if (e)
877 x = e->val_rtx;
880 if (REG_P (y) || MEM_P (y))
882 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0, memmode);
884 if (e)
885 y = e->val_rtx;
888 if (x == y)
889 return 1;
891 if (GET_CODE (x) == VALUE)
893 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (x));
894 struct elt_loc_list *l;
896 if (GET_CODE (y) == VALUE)
897 return e == canonical_cselib_val (CSELIB_VAL_PTR (y));
899 for (l = e->locs; l; l = l->next)
901 rtx t = l->loc;
903 /* Avoid infinite recursion. We know we have the canonical
904 value, so we can just skip any values in the equivalence
905 list. */
906 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
907 continue;
908 else if (rtx_equal_for_cselib_1 (t, y, memmode))
909 return 1;
912 return 0;
914 else if (GET_CODE (y) == VALUE)
916 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (y));
917 struct elt_loc_list *l;
919 for (l = e->locs; l; l = l->next)
921 rtx t = l->loc;
923 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
924 continue;
925 else if (rtx_equal_for_cselib_1 (x, t, memmode))
926 return 1;
929 return 0;
932 if (GET_MODE (x) != GET_MODE (y))
933 return 0;
935 if (GET_CODE (x) != GET_CODE (y))
937 rtx xorig = x, yorig = y;
938 rtx xoff = NULL, yoff = NULL;
940 x = autoinc_split (x, &xoff, memmode);
941 y = autoinc_split (y, &yoff, memmode);
943 if (!xoff != !yoff)
944 return 0;
946 if (xoff && !rtx_equal_for_cselib_1 (xoff, yoff, memmode))
947 return 0;
949 /* Don't recurse if nothing changed. */
950 if (x != xorig || y != yorig)
951 return rtx_equal_for_cselib_1 (x, y, memmode);
953 return 0;
956 /* These won't be handled correctly by the code below. */
957 switch (GET_CODE (x))
959 CASE_CONST_UNIQUE:
960 case DEBUG_EXPR:
961 return 0;
963 case DEBUG_IMPLICIT_PTR:
964 return DEBUG_IMPLICIT_PTR_DECL (x)
965 == DEBUG_IMPLICIT_PTR_DECL (y);
967 case DEBUG_PARAMETER_REF:
968 return DEBUG_PARAMETER_REF_DECL (x)
969 == DEBUG_PARAMETER_REF_DECL (y);
971 case ENTRY_VALUE:
972 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
973 use rtx_equal_for_cselib_1 to compare the operands. */
974 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
976 case LABEL_REF:
977 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
979 case MEM:
980 /* We have to compare any autoinc operations in the addresses
981 using this MEM's mode. */
982 return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x));
984 default:
985 break;
988 code = GET_CODE (x);
989 fmt = GET_RTX_FORMAT (code);
991 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
993 int j;
995 switch (fmt[i])
997 case 'w':
998 if (XWINT (x, i) != XWINT (y, i))
999 return 0;
1000 break;
1002 case 'n':
1003 case 'i':
1004 if (XINT (x, i) != XINT (y, i))
1005 return 0;
1006 break;
1008 case 'V':
1009 case 'E':
1010 /* Two vectors must have the same length. */
1011 if (XVECLEN (x, i) != XVECLEN (y, i))
1012 return 0;
1014 /* And the corresponding elements must match. */
1015 for (j = 0; j < XVECLEN (x, i); j++)
1016 if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
1017 XVECEXP (y, i, j), memmode))
1018 return 0;
1019 break;
1021 case 'e':
1022 if (i == 1
1023 && targetm.commutative_p (x, UNKNOWN)
1024 && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode)
1025 && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode))
1026 return 1;
1027 if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode))
1028 return 0;
1029 break;
1031 case 'S':
1032 case 's':
1033 if (strcmp (XSTR (x, i), XSTR (y, i)))
1034 return 0;
1035 break;
1037 case 'u':
1038 /* These are just backpointers, so they don't matter. */
1039 break;
1041 case '0':
1042 case 't':
1043 break;
1045 /* It is believed that rtx's at this level will never
1046 contain anything but integers and other rtx's,
1047 except for within LABEL_REFs and SYMBOL_REFs. */
1048 default:
1049 gcc_unreachable ();
1052 return 1;
1055 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1056 For registers and memory locations, we look up their cselib_val structure
1057 and return its VALUE element.
1058 Possible reasons for return 0 are: the object is volatile, or we couldn't
1059 find a register or memory location in the table and CREATE is zero. If
1060 CREATE is nonzero, table elts are created for regs and mem.
1061 N.B. this hash function returns the same hash value for RTXes that
1062 differ only in the order of operands, thus it is suitable for comparisons
1063 that take commutativity into account.
1064 If we wanted to also support associative rules, we'd have to use a different
1065 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1066 MEMMODE indicates the mode of an enclosing MEM, and it's only
1067 used to compute autoinc values.
1068 We used to have a MODE argument for hashing for CONST_INTs, but that
1069 didn't make sense, since it caused spurious hash differences between
1070 (set (reg:SI 1) (const_int))
1071 (plus:SI (reg:SI 2) (reg:SI 1))
1073 (plus:SI (reg:SI 2) (const_int))
1074 If the mode is important in any context, it must be checked specifically
1075 in a comparison anyway, since relying on hash differences is unsafe. */
1077 static unsigned int
1078 cselib_hash_rtx (rtx x, int create, machine_mode memmode)
1080 cselib_val *e;
1081 int i, j;
1082 enum rtx_code code;
1083 const char *fmt;
1084 unsigned int hash = 0;
1086 code = GET_CODE (x);
1087 hash += (unsigned) code + (unsigned) GET_MODE (x);
1089 switch (code)
1091 case VALUE:
1092 e = CSELIB_VAL_PTR (x);
1093 return e->hash;
1095 case MEM:
1096 case REG:
1097 e = cselib_lookup (x, GET_MODE (x), create, memmode);
1098 if (! e)
1099 return 0;
1101 return e->hash;
1103 case DEBUG_EXPR:
1104 hash += ((unsigned) DEBUG_EXPR << 7)
1105 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
1106 return hash ? hash : (unsigned int) DEBUG_EXPR;
1108 case DEBUG_IMPLICIT_PTR:
1109 hash += ((unsigned) DEBUG_IMPLICIT_PTR << 7)
1110 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x));
1111 return hash ? hash : (unsigned int) DEBUG_IMPLICIT_PTR;
1113 case DEBUG_PARAMETER_REF:
1114 hash += ((unsigned) DEBUG_PARAMETER_REF << 7)
1115 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x));
1116 return hash ? hash : (unsigned int) DEBUG_PARAMETER_REF;
1118 case ENTRY_VALUE:
1119 /* ENTRY_VALUEs are function invariant, thus try to avoid
1120 recursing on argument if ENTRY_VALUE is one of the
1121 forms emitted by expand_debug_expr, otherwise
1122 ENTRY_VALUE hash would depend on the current value
1123 in some register or memory. */
1124 if (REG_P (ENTRY_VALUE_EXP (x)))
1125 hash += (unsigned int) REG
1126 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x))
1127 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x));
1128 else if (MEM_P (ENTRY_VALUE_EXP (x))
1129 && REG_P (XEXP (ENTRY_VALUE_EXP (x), 0)))
1130 hash += (unsigned int) MEM
1131 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x), 0))
1132 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x), 0));
1133 else
1134 hash += cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode);
1135 return hash ? hash : (unsigned int) ENTRY_VALUE;
1137 case CONST_INT:
1138 hash += ((unsigned) CONST_INT << 7) + UINTVAL (x);
1139 return hash ? hash : (unsigned int) CONST_INT;
1141 case CONST_WIDE_INT:
1142 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
1143 hash += CONST_WIDE_INT_ELT (x, i);
1144 return hash;
1146 case CONST_DOUBLE:
1147 /* This is like the general case, except that it only counts
1148 the integers representing the constant. */
1149 hash += (unsigned) code + (unsigned) GET_MODE (x);
1150 if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (x) == VOIDmode)
1151 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1152 + (unsigned) CONST_DOUBLE_HIGH (x));
1153 else
1154 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
1155 return hash ? hash : (unsigned int) CONST_DOUBLE;
1157 case CONST_FIXED:
1158 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1159 hash += fixed_hash (CONST_FIXED_VALUE (x));
1160 return hash ? hash : (unsigned int) CONST_FIXED;
1162 case CONST_VECTOR:
1164 int units;
1165 rtx elt;
1167 units = CONST_VECTOR_NUNITS (x);
1169 for (i = 0; i < units; ++i)
1171 elt = CONST_VECTOR_ELT (x, i);
1172 hash += cselib_hash_rtx (elt, 0, memmode);
1175 return hash;
1178 /* Assume there is only one rtx object for any given label. */
1179 case LABEL_REF:
1180 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1181 differences and differences between each stage's debugging dumps. */
1182 hash += (((unsigned int) LABEL_REF << 7)
1183 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x)));
1184 return hash ? hash : (unsigned int) LABEL_REF;
1186 case SYMBOL_REF:
1188 /* Don't hash on the symbol's address to avoid bootstrap differences.
1189 Different hash values may cause expressions to be recorded in
1190 different orders and thus different registers to be used in the
1191 final assembler. This also avoids differences in the dump files
1192 between various stages. */
1193 unsigned int h = 0;
1194 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1196 while (*p)
1197 h += (h << 7) + *p++; /* ??? revisit */
1199 hash += ((unsigned int) SYMBOL_REF << 7) + h;
1200 return hash ? hash : (unsigned int) SYMBOL_REF;
1203 case PRE_DEC:
1204 case PRE_INC:
1205 /* We can't compute these without knowing the MEM mode. */
1206 gcc_assert (memmode != VOIDmode);
1207 i = GET_MODE_SIZE (memmode);
1208 if (code == PRE_DEC)
1209 i = -i;
1210 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1211 like (mem:MEMMODE (plus (reg) (const_int I))). */
1212 hash += (unsigned) PLUS - (unsigned)code
1213 + cselib_hash_rtx (XEXP (x, 0), create, memmode)
1214 + cselib_hash_rtx (GEN_INT (i), create, memmode);
1215 return hash ? hash : 1 + (unsigned) PLUS;
1217 case PRE_MODIFY:
1218 gcc_assert (memmode != VOIDmode);
1219 return cselib_hash_rtx (XEXP (x, 1), create, memmode);
1221 case POST_DEC:
1222 case POST_INC:
1223 case POST_MODIFY:
1224 gcc_assert (memmode != VOIDmode);
1225 return cselib_hash_rtx (XEXP (x, 0), create, memmode);
1227 case PC:
1228 case CC0:
1229 case CALL:
1230 case UNSPEC_VOLATILE:
1231 return 0;
1233 case ASM_OPERANDS:
1234 if (MEM_VOLATILE_P (x))
1235 return 0;
1237 break;
1239 default:
1240 break;
1243 i = GET_RTX_LENGTH (code) - 1;
1244 fmt = GET_RTX_FORMAT (code);
1245 for (; i >= 0; i--)
1247 switch (fmt[i])
1249 case 'e':
1251 rtx tem = XEXP (x, i);
1252 unsigned int tem_hash = cselib_hash_rtx (tem, create, memmode);
1254 if (tem_hash == 0)
1255 return 0;
1257 hash += tem_hash;
1259 break;
1260 case 'E':
1261 for (j = 0; j < XVECLEN (x, i); j++)
1263 unsigned int tem_hash
1264 = cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
1266 if (tem_hash == 0)
1267 return 0;
1269 hash += tem_hash;
1271 break;
1273 case 's':
1275 const unsigned char *p = (const unsigned char *) XSTR (x, i);
1277 if (p)
1278 while (*p)
1279 hash += *p++;
1280 break;
1283 case 'i':
1284 hash += XINT (x, i);
1285 break;
1287 case '0':
1288 case 't':
1289 /* unused */
1290 break;
1292 default:
1293 gcc_unreachable ();
1297 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
1300 /* Create a new value structure for VALUE and initialize it. The mode of the
1301 value is MODE. */
1303 static inline cselib_val *
1304 new_cselib_val (unsigned int hash, machine_mode mode, rtx x)
1306 cselib_val *e = (cselib_val *) pool_alloc (cselib_val_pool);
1308 gcc_assert (hash);
1309 gcc_assert (next_uid);
1311 e->hash = hash;
1312 e->uid = next_uid++;
1313 /* We use an alloc pool to allocate this RTL construct because it
1314 accounts for about 8% of the overall memory usage. We know
1315 precisely when we can have VALUE RTXen (when cselib is active)
1316 so we don't need to put them in garbage collected memory.
1317 ??? Why should a VALUE be an RTX in the first place? */
1318 e->val_rtx = (rtx) pool_alloc (value_pool);
1319 memset (e->val_rtx, 0, RTX_HDR_SIZE);
1320 PUT_CODE (e->val_rtx, VALUE);
1321 PUT_MODE (e->val_rtx, mode);
1322 CSELIB_VAL_PTR (e->val_rtx) = e;
1323 e->addr_list = 0;
1324 e->locs = 0;
1325 e->next_containing_mem = 0;
1327 if (dump_file && (dump_flags & TDF_CSELIB))
1329 fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
1330 if (flag_dump_noaddr || flag_dump_unnumbered)
1331 fputs ("# ", dump_file);
1332 else
1333 fprintf (dump_file, "%p ", (void*)e);
1334 print_rtl_single (dump_file, x);
1335 fputc ('\n', dump_file);
1338 return e;
1341 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1342 contains the data at this address. X is a MEM that represents the
1343 value. Update the two value structures to represent this situation. */
1345 static void
1346 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
1348 struct elt_loc_list *l;
1350 addr_elt = canonical_cselib_val (addr_elt);
1351 mem_elt = canonical_cselib_val (mem_elt);
1353 /* Avoid duplicates. */
1354 for (l = mem_elt->locs; l; l = l->next)
1355 if (MEM_P (l->loc)
1356 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
1358 promote_debug_loc (l);
1359 return;
1362 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
1363 new_elt_loc_list (mem_elt,
1364 replace_equiv_address_nv (x, addr_elt->val_rtx));
1365 if (mem_elt->next_containing_mem == NULL)
1367 mem_elt->next_containing_mem = first_containing_mem;
1368 first_containing_mem = mem_elt;
1372 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1373 If CREATE, make a new one if we haven't seen it before. */
1375 static cselib_val *
1376 cselib_lookup_mem (rtx x, int create)
1378 machine_mode mode = GET_MODE (x);
1379 machine_mode addr_mode;
1380 cselib_val **slot;
1381 cselib_val *addr;
1382 cselib_val *mem_elt;
1383 struct elt_list *l;
1385 if (MEM_VOLATILE_P (x) || mode == BLKmode
1386 || !cselib_record_memory
1387 || (FLOAT_MODE_P (mode) && flag_float_store))
1388 return 0;
1390 addr_mode = GET_MODE (XEXP (x, 0));
1391 if (addr_mode == VOIDmode)
1392 addr_mode = Pmode;
1394 /* Look up the value for the address. */
1395 addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
1396 if (! addr)
1397 return 0;
1399 addr = canonical_cselib_val (addr);
1400 /* Find a value that describes a value of our mode at that address. */
1401 for (l = addr->addr_list; l; l = l->next)
1402 if (GET_MODE (l->elt->val_rtx) == mode)
1404 promote_debug_loc (l->elt->locs);
1405 return l->elt;
1408 if (! create)
1409 return 0;
1411 mem_elt = new_cselib_val (next_uid, mode, x);
1412 add_mem_for_addr (addr, mem_elt, x);
1413 slot = cselib_find_slot (mode, x, mem_elt->hash, INSERT, VOIDmode);
1414 *slot = mem_elt;
1415 return mem_elt;
1418 /* Search through the possible substitutions in P. We prefer a non reg
1419 substitution because this allows us to expand the tree further. If
1420 we find, just a reg, take the lowest regno. There may be several
1421 non-reg results, we just take the first one because they will all
1422 expand to the same place. */
1424 static rtx
1425 expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1426 int max_depth)
1428 rtx reg_result = NULL;
1429 unsigned int regno = UINT_MAX;
1430 struct elt_loc_list *p_in = p;
1432 for (; p; p = p->next)
1434 /* Return these right away to avoid returning stack pointer based
1435 expressions for frame pointer and vice versa, which is something
1436 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1437 for more details. */
1438 if (REG_P (p->loc)
1439 && (REGNO (p->loc) == STACK_POINTER_REGNUM
1440 || REGNO (p->loc) == FRAME_POINTER_REGNUM
1441 || REGNO (p->loc) == HARD_FRAME_POINTER_REGNUM
1442 || REGNO (p->loc) == cfa_base_preserved_regno))
1443 return p->loc;
1444 /* Avoid infinite recursion trying to expand a reg into a
1445 the same reg. */
1446 if ((REG_P (p->loc))
1447 && (REGNO (p->loc) < regno)
1448 && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
1450 reg_result = p->loc;
1451 regno = REGNO (p->loc);
1453 /* Avoid infinite recursion and do not try to expand the
1454 value. */
1455 else if (GET_CODE (p->loc) == VALUE
1456 && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1457 continue;
1458 else if (!REG_P (p->loc))
1460 rtx result, note;
1461 if (dump_file && (dump_flags & TDF_CSELIB))
1463 print_inline_rtx (dump_file, p->loc, 0);
1464 fprintf (dump_file, "\n");
1466 if (GET_CODE (p->loc) == LO_SUM
1467 && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1468 && p->setting_insn
1469 && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1470 && XEXP (note, 0) == XEXP (p->loc, 1))
1471 return XEXP (p->loc, 1);
1472 result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
1473 if (result)
1474 return result;
1479 if (regno != UINT_MAX)
1481 rtx result;
1482 if (dump_file && (dump_flags & TDF_CSELIB))
1483 fprintf (dump_file, "r%d\n", regno);
1485 result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
1486 if (result)
1487 return result;
1490 if (dump_file && (dump_flags & TDF_CSELIB))
1492 if (reg_result)
1494 print_inline_rtx (dump_file, reg_result, 0);
1495 fprintf (dump_file, "\n");
1497 else
1498 fprintf (dump_file, "NULL\n");
1500 return reg_result;
1504 /* Forward substitute and expand an expression out to its roots.
1505 This is the opposite of common subexpression. Because local value
1506 numbering is such a weak optimization, the expanded expression is
1507 pretty much unique (not from a pointer equals point of view but
1508 from a tree shape point of view.
1510 This function returns NULL if the expansion fails. The expansion
1511 will fail if there is no value number for one of the operands or if
1512 one of the operands has been overwritten between the current insn
1513 and the beginning of the basic block. For instance x has no
1514 expansion in:
1516 r1 <- r1 + 3
1517 x <- r1 + 8
1519 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1520 It is clear on return. */
1523 cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
1525 struct expand_value_data evd;
1527 evd.regs_active = regs_active;
1528 evd.callback = NULL;
1529 evd.callback_arg = NULL;
1530 evd.dummy = false;
1532 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1535 /* Same as cselib_expand_value_rtx, but using a callback to try to
1536 resolve some expressions. The CB function should return ORIG if it
1537 can't or does not want to deal with a certain RTX. Any other
1538 return value, including NULL, will be used as the expansion for
1539 VALUE, without any further changes. */
1542 cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1543 cselib_expand_callback cb, void *data)
1545 struct expand_value_data evd;
1547 evd.regs_active = regs_active;
1548 evd.callback = cb;
1549 evd.callback_arg = data;
1550 evd.dummy = false;
1552 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1555 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1556 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1557 would return NULL or non-NULL, without allocating new rtx. */
1559 bool
1560 cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1561 cselib_expand_callback cb, void *data)
1563 struct expand_value_data evd;
1565 evd.regs_active = regs_active;
1566 evd.callback = cb;
1567 evd.callback_arg = data;
1568 evd.dummy = true;
1570 return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1573 /* Internal implementation of cselib_expand_value_rtx and
1574 cselib_expand_value_rtx_cb. */
1576 static rtx
1577 cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1578 int max_depth)
1580 rtx copy, scopy;
1581 int i, j;
1582 RTX_CODE code;
1583 const char *format_ptr;
1584 machine_mode mode;
1586 code = GET_CODE (orig);
1588 /* For the context of dse, if we end up expand into a huge tree, we
1589 will not have a useful address, so we might as well just give up
1590 quickly. */
1591 if (max_depth <= 0)
1592 return NULL;
1594 switch (code)
1596 case REG:
1598 struct elt_list *l = REG_VALUES (REGNO (orig));
1600 if (l && l->elt == NULL)
1601 l = l->next;
1602 for (; l; l = l->next)
1603 if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1605 rtx result;
1606 unsigned regno = REGNO (orig);
1608 /* The only thing that we are not willing to do (this
1609 is requirement of dse and if others potential uses
1610 need this function we should add a parm to control
1611 it) is that we will not substitute the
1612 STACK_POINTER_REGNUM, FRAME_POINTER or the
1613 HARD_FRAME_POINTER.
1615 These expansions confuses the code that notices that
1616 stores into the frame go dead at the end of the
1617 function and that the frame is not effected by calls
1618 to subroutines. If you allow the
1619 STACK_POINTER_REGNUM substitution, then dse will
1620 think that parameter pushing also goes dead which is
1621 wrong. If you allow the FRAME_POINTER or the
1622 HARD_FRAME_POINTER then you lose the opportunity to
1623 make the frame assumptions. */
1624 if (regno == STACK_POINTER_REGNUM
1625 || regno == FRAME_POINTER_REGNUM
1626 || regno == HARD_FRAME_POINTER_REGNUM
1627 || regno == cfa_base_preserved_regno)
1628 return orig;
1630 bitmap_set_bit (evd->regs_active, regno);
1632 if (dump_file && (dump_flags & TDF_CSELIB))
1633 fprintf (dump_file, "expanding: r%d into: ", regno);
1635 result = expand_loc (l->elt->locs, evd, max_depth);
1636 bitmap_clear_bit (evd->regs_active, regno);
1638 if (result)
1639 return result;
1640 else
1641 return orig;
1645 CASE_CONST_ANY:
1646 case SYMBOL_REF:
1647 case CODE_LABEL:
1648 case PC:
1649 case CC0:
1650 case SCRATCH:
1651 /* SCRATCH must be shared because they represent distinct values. */
1652 return orig;
1653 case CLOBBER:
1654 if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1655 return orig;
1656 break;
1658 case CONST:
1659 if (shared_const_p (orig))
1660 return orig;
1661 break;
1663 case SUBREG:
1665 rtx subreg;
1667 if (evd->callback)
1669 subreg = evd->callback (orig, evd->regs_active, max_depth,
1670 evd->callback_arg);
1671 if (subreg != orig)
1672 return subreg;
1675 subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1676 max_depth - 1);
1677 if (!subreg)
1678 return NULL;
1679 scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1680 GET_MODE (SUBREG_REG (orig)),
1681 SUBREG_BYTE (orig));
1682 if (scopy == NULL
1683 || (GET_CODE (scopy) == SUBREG
1684 && !REG_P (SUBREG_REG (scopy))
1685 && !MEM_P (SUBREG_REG (scopy))))
1686 return NULL;
1688 return scopy;
1691 case VALUE:
1693 rtx result;
1695 if (dump_file && (dump_flags & TDF_CSELIB))
1697 fputs ("\nexpanding ", dump_file);
1698 print_rtl_single (dump_file, orig);
1699 fputs (" into...", dump_file);
1702 if (evd->callback)
1704 result = evd->callback (orig, evd->regs_active, max_depth,
1705 evd->callback_arg);
1707 if (result != orig)
1708 return result;
1711 result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
1712 return result;
1715 case DEBUG_EXPR:
1716 if (evd->callback)
1717 return evd->callback (orig, evd->regs_active, max_depth,
1718 evd->callback_arg);
1719 return orig;
1721 default:
1722 break;
1725 /* Copy the various flags, fields, and other information. We assume
1726 that all fields need copying, and then clear the fields that should
1727 not be copied. That is the sensible default behavior, and forces
1728 us to explicitly document why we are *not* copying a flag. */
1729 if (evd->dummy)
1730 copy = NULL;
1731 else
1732 copy = shallow_copy_rtx (orig);
1734 format_ptr = GET_RTX_FORMAT (code);
1736 for (i = 0; i < GET_RTX_LENGTH (code); i++)
1737 switch (*format_ptr++)
1739 case 'e':
1740 if (XEXP (orig, i) != NULL)
1742 rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1743 max_depth - 1);
1744 if (!result)
1745 return NULL;
1746 if (copy)
1747 XEXP (copy, i) = result;
1749 break;
1751 case 'E':
1752 case 'V':
1753 if (XVEC (orig, i) != NULL)
1755 if (copy)
1756 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1757 for (j = 0; j < XVECLEN (orig, i); j++)
1759 rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1760 evd, max_depth - 1);
1761 if (!result)
1762 return NULL;
1763 if (copy)
1764 XVECEXP (copy, i, j) = result;
1767 break;
1769 case 't':
1770 case 'w':
1771 case 'i':
1772 case 's':
1773 case 'S':
1774 case 'T':
1775 case 'u':
1776 case 'B':
1777 case '0':
1778 /* These are left unchanged. */
1779 break;
1781 default:
1782 gcc_unreachable ();
1785 if (evd->dummy)
1786 return orig;
1788 mode = GET_MODE (copy);
1789 /* If an operand has been simplified into CONST_INT, which doesn't
1790 have a mode and the mode isn't derivable from whole rtx's mode,
1791 try simplify_*_operation first with mode from original's operand
1792 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1793 scopy = copy;
1794 switch (GET_RTX_CLASS (code))
1796 case RTX_UNARY:
1797 if (CONST_INT_P (XEXP (copy, 0))
1798 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1800 scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1801 GET_MODE (XEXP (orig, 0)));
1802 if (scopy)
1803 return scopy;
1805 break;
1806 case RTX_COMM_ARITH:
1807 case RTX_BIN_ARITH:
1808 /* These expressions can derive operand modes from the whole rtx's mode. */
1809 break;
1810 case RTX_TERNARY:
1811 case RTX_BITFIELD_OPS:
1812 if (CONST_INT_P (XEXP (copy, 0))
1813 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1815 scopy = simplify_ternary_operation (code, mode,
1816 GET_MODE (XEXP (orig, 0)),
1817 XEXP (copy, 0), XEXP (copy, 1),
1818 XEXP (copy, 2));
1819 if (scopy)
1820 return scopy;
1822 break;
1823 case RTX_COMPARE:
1824 case RTX_COMM_COMPARE:
1825 if (CONST_INT_P (XEXP (copy, 0))
1826 && GET_MODE (XEXP (copy, 1)) == VOIDmode
1827 && (GET_MODE (XEXP (orig, 0)) != VOIDmode
1828 || GET_MODE (XEXP (orig, 1)) != VOIDmode))
1830 scopy = simplify_relational_operation (code, mode,
1831 (GET_MODE (XEXP (orig, 0))
1832 != VOIDmode)
1833 ? GET_MODE (XEXP (orig, 0))
1834 : GET_MODE (XEXP (orig, 1)),
1835 XEXP (copy, 0),
1836 XEXP (copy, 1));
1837 if (scopy)
1838 return scopy;
1840 break;
1841 default:
1842 break;
1844 scopy = simplify_rtx (copy);
1845 if (scopy)
1846 return scopy;
1847 return copy;
1850 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1851 with VALUE expressions. This way, it becomes independent of changes
1852 to registers and memory.
1853 X isn't actually modified; if modifications are needed, new rtl is
1854 allocated. However, the return value can share rtl with X.
1855 If X is within a MEM, MEMMODE must be the mode of the MEM. */
1858 cselib_subst_to_values (rtx x, machine_mode memmode)
1860 enum rtx_code code = GET_CODE (x);
1861 const char *fmt = GET_RTX_FORMAT (code);
1862 cselib_val *e;
1863 struct elt_list *l;
1864 rtx copy = x;
1865 int i;
1867 switch (code)
1869 case REG:
1870 l = REG_VALUES (REGNO (x));
1871 if (l && l->elt == NULL)
1872 l = l->next;
1873 for (; l; l = l->next)
1874 if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
1875 return l->elt->val_rtx;
1877 gcc_unreachable ();
1879 case MEM:
1880 e = cselib_lookup_mem (x, 0);
1881 /* This used to happen for autoincrements, but we deal with them
1882 properly now. Remove the if stmt for the next release. */
1883 if (! e)
1885 /* Assign a value that doesn't match any other. */
1886 e = new_cselib_val (next_uid, GET_MODE (x), x);
1888 return e->val_rtx;
1890 case ENTRY_VALUE:
1891 e = cselib_lookup (x, GET_MODE (x), 0, memmode);
1892 if (! e)
1893 break;
1894 return e->val_rtx;
1896 CASE_CONST_ANY:
1897 return x;
1899 case PRE_DEC:
1900 case PRE_INC:
1901 gcc_assert (memmode != VOIDmode);
1902 i = GET_MODE_SIZE (memmode);
1903 if (code == PRE_DEC)
1904 i = -i;
1905 return cselib_subst_to_values (plus_constant (GET_MODE (x),
1906 XEXP (x, 0), i),
1907 memmode);
1909 case PRE_MODIFY:
1910 gcc_assert (memmode != VOIDmode);
1911 return cselib_subst_to_values (XEXP (x, 1), memmode);
1913 case POST_DEC:
1914 case POST_INC:
1915 case POST_MODIFY:
1916 gcc_assert (memmode != VOIDmode);
1917 return cselib_subst_to_values (XEXP (x, 0), memmode);
1919 default:
1920 break;
1923 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1925 if (fmt[i] == 'e')
1927 rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
1929 if (t != XEXP (x, i))
1931 if (x == copy)
1932 copy = shallow_copy_rtx (x);
1933 XEXP (copy, i) = t;
1936 else if (fmt[i] == 'E')
1938 int j;
1940 for (j = 0; j < XVECLEN (x, i); j++)
1942 rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
1944 if (t != XVECEXP (x, i, j))
1946 if (XVEC (x, i) == XVEC (copy, i))
1948 if (x == copy)
1949 copy = shallow_copy_rtx (x);
1950 XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
1952 XVECEXP (copy, i, j) = t;
1958 return copy;
1961 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
1964 cselib_subst_to_values_from_insn (rtx x, machine_mode memmode, rtx_insn *insn)
1966 rtx ret;
1967 gcc_assert (!cselib_current_insn);
1968 cselib_current_insn = insn;
1969 ret = cselib_subst_to_values (x, memmode);
1970 cselib_current_insn = NULL;
1971 return ret;
1974 /* Look up the rtl expression X in our tables and return the value it
1975 has. If CREATE is zero, we return NULL if we don't know the value.
1976 Otherwise, we create a new one if possible, using mode MODE if X
1977 doesn't have a mode (i.e. because it's a constant). When X is part
1978 of an address, MEMMODE should be the mode of the enclosing MEM if
1979 we're tracking autoinc expressions. */
1981 static cselib_val *
1982 cselib_lookup_1 (rtx x, machine_mode mode,
1983 int create, machine_mode memmode)
1985 cselib_val **slot;
1986 cselib_val *e;
1987 unsigned int hashval;
1989 if (GET_MODE (x) != VOIDmode)
1990 mode = GET_MODE (x);
1992 if (GET_CODE (x) == VALUE)
1993 return CSELIB_VAL_PTR (x);
1995 if (REG_P (x))
1997 struct elt_list *l;
1998 unsigned int i = REGNO (x);
2000 l = REG_VALUES (i);
2001 if (l && l->elt == NULL)
2002 l = l->next;
2003 for (; l; l = l->next)
2004 if (mode == GET_MODE (l->elt->val_rtx))
2006 promote_debug_loc (l->elt->locs);
2007 return l->elt;
2010 if (! create)
2011 return 0;
2013 if (i < FIRST_PSEUDO_REGISTER)
2015 unsigned int n = hard_regno_nregs[i][mode];
2017 if (n > max_value_regs)
2018 max_value_regs = n;
2021 e = new_cselib_val (next_uid, GET_MODE (x), x);
2022 new_elt_loc_list (e, x);
2023 if (REG_VALUES (i) == 0)
2025 /* Maintain the invariant that the first entry of
2026 REG_VALUES, if present, must be the value used to set the
2027 register, or NULL. */
2028 used_regs[n_used_regs++] = i;
2029 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
2031 else if (cselib_preserve_constants
2032 && GET_MODE_CLASS (mode) == MODE_INT)
2034 /* During var-tracking, try harder to find equivalences
2035 for SUBREGs. If a setter sets say a DImode register
2036 and user uses that register only in SImode, add a lowpart
2037 subreg location. */
2038 struct elt_list *lwider = NULL;
2039 l = REG_VALUES (i);
2040 if (l && l->elt == NULL)
2041 l = l->next;
2042 for (; l; l = l->next)
2043 if (GET_MODE_CLASS (GET_MODE (l->elt->val_rtx)) == MODE_INT
2044 && GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2045 > GET_MODE_SIZE (mode)
2046 && (lwider == NULL
2047 || GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2048 < GET_MODE_SIZE (GET_MODE (lwider->elt->val_rtx))))
2050 struct elt_loc_list *el;
2051 if (i < FIRST_PSEUDO_REGISTER
2052 && hard_regno_nregs[i][GET_MODE (l->elt->val_rtx)] != 1)
2053 continue;
2054 for (el = l->elt->locs; el; el = el->next)
2055 if (!REG_P (el->loc))
2056 break;
2057 if (el)
2058 lwider = l;
2060 if (lwider)
2062 rtx sub = lowpart_subreg (mode, lwider->elt->val_rtx,
2063 GET_MODE (lwider->elt->val_rtx));
2064 if (sub)
2065 new_elt_loc_list (e, sub);
2068 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
2069 slot = cselib_find_slot (mode, x, e->hash, INSERT, memmode);
2070 *slot = e;
2071 return e;
2074 if (MEM_P (x))
2075 return cselib_lookup_mem (x, create);
2077 hashval = cselib_hash_rtx (x, create, memmode);
2078 /* Can't even create if hashing is not possible. */
2079 if (! hashval)
2080 return 0;
2082 slot = cselib_find_slot (mode, x, hashval,
2083 create ? INSERT : NO_INSERT, memmode);
2084 if (slot == 0)
2085 return 0;
2087 e = (cselib_val *) *slot;
2088 if (e)
2089 return e;
2091 e = new_cselib_val (hashval, mode, x);
2093 /* We have to fill the slot before calling cselib_subst_to_values:
2094 the hash table is inconsistent until we do so, and
2095 cselib_subst_to_values will need to do lookups. */
2096 *slot = e;
2097 new_elt_loc_list (e, cselib_subst_to_values (x, memmode));
2098 return e;
2101 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2103 cselib_val *
2104 cselib_lookup_from_insn (rtx x, machine_mode mode,
2105 int create, machine_mode memmode, rtx_insn *insn)
2107 cselib_val *ret;
2109 gcc_assert (!cselib_current_insn);
2110 cselib_current_insn = insn;
2112 ret = cselib_lookup (x, mode, create, memmode);
2114 cselib_current_insn = NULL;
2116 return ret;
2119 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2120 maintains invariants related with debug insns. */
2122 cselib_val *
2123 cselib_lookup (rtx x, machine_mode mode,
2124 int create, machine_mode memmode)
2126 cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
2128 /* ??? Should we return NULL if we're not to create an entry, the
2129 found loc is a debug loc and cselib_current_insn is not DEBUG?
2130 If so, we should also avoid converting val to non-DEBUG; probably
2131 easiest setting cselib_current_insn to NULL before the call
2132 above. */
2134 if (dump_file && (dump_flags & TDF_CSELIB))
2136 fputs ("cselib lookup ", dump_file);
2137 print_inline_rtx (dump_file, x, 2);
2138 fprintf (dump_file, " => %u:%u\n",
2139 ret ? ret->uid : 0,
2140 ret ? ret->hash : 0);
2143 return ret;
2146 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2147 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2148 is used to determine how many hard registers are being changed. If MODE
2149 is VOIDmode, then only REGNO is being changed; this is used when
2150 invalidating call clobbered registers across a call. */
2152 static void
2153 cselib_invalidate_regno (unsigned int regno, machine_mode mode)
2155 unsigned int endregno;
2156 unsigned int i;
2158 /* If we see pseudos after reload, something is _wrong_. */
2159 gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
2160 || reg_renumber[regno] < 0);
2162 /* Determine the range of registers that must be invalidated. For
2163 pseudos, only REGNO is affected. For hard regs, we must take MODE
2164 into account, and we must also invalidate lower register numbers
2165 if they contain values that overlap REGNO. */
2166 if (regno < FIRST_PSEUDO_REGISTER)
2168 gcc_assert (mode != VOIDmode);
2170 if (regno < max_value_regs)
2171 i = 0;
2172 else
2173 i = regno - max_value_regs;
2175 endregno = end_hard_regno (mode, regno);
2177 else
2179 i = regno;
2180 endregno = regno + 1;
2183 for (; i < endregno; i++)
2185 struct elt_list **l = &REG_VALUES (i);
2187 /* Go through all known values for this reg; if it overlaps the range
2188 we're invalidating, remove the value. */
2189 while (*l)
2191 cselib_val *v = (*l)->elt;
2192 bool had_locs;
2193 rtx setting_insn;
2194 struct elt_loc_list **p;
2195 unsigned int this_last = i;
2197 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
2198 this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
2200 if (this_last < regno || v == NULL
2201 || (v == cfa_base_preserved_val
2202 && i == cfa_base_preserved_regno))
2204 l = &(*l)->next;
2205 continue;
2208 /* We have an overlap. */
2209 if (*l == REG_VALUES (i))
2211 /* Maintain the invariant that the first entry of
2212 REG_VALUES, if present, must be the value used to set
2213 the register, or NULL. This is also nice because
2214 then we won't push the same regno onto user_regs
2215 multiple times. */
2216 (*l)->elt = NULL;
2217 l = &(*l)->next;
2219 else
2220 unchain_one_elt_list (l);
2222 v = canonical_cselib_val (v);
2224 had_locs = v->locs != NULL;
2225 setting_insn = v->locs ? v->locs->setting_insn : NULL;
2227 /* Now, we clear the mapping from value to reg. It must exist, so
2228 this code will crash intentionally if it doesn't. */
2229 for (p = &v->locs; ; p = &(*p)->next)
2231 rtx x = (*p)->loc;
2233 if (REG_P (x) && REGNO (x) == i)
2235 unchain_one_elt_loc_list (p);
2236 break;
2240 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2242 if (setting_insn && DEBUG_INSN_P (setting_insn))
2243 n_useless_debug_values++;
2244 else
2245 n_useless_values++;
2251 /* Invalidate any locations in the table which are changed because of a
2252 store to MEM_RTX. If this is called because of a non-const call
2253 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2255 static void
2256 cselib_invalidate_mem (rtx mem_rtx)
2258 cselib_val **vp, *v, *next;
2259 int num_mems = 0;
2260 rtx mem_addr;
2262 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2263 mem_rtx = canon_rtx (mem_rtx);
2265 vp = &first_containing_mem;
2266 for (v = *vp; v != &dummy_val; v = next)
2268 bool has_mem = false;
2269 struct elt_loc_list **p = &v->locs;
2270 bool had_locs = v->locs != NULL;
2271 rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
2273 while (*p)
2275 rtx x = (*p)->loc;
2276 cselib_val *addr;
2277 struct elt_list **mem_chain;
2279 /* MEMs may occur in locations only at the top level; below
2280 that every MEM or REG is substituted by its VALUE. */
2281 if (!MEM_P (x))
2283 p = &(*p)->next;
2284 continue;
2286 if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
2287 && ! canon_anti_dependence (x, false, mem_rtx,
2288 GET_MODE (mem_rtx), mem_addr))
2290 has_mem = true;
2291 num_mems++;
2292 p = &(*p)->next;
2293 continue;
2296 /* This one overlaps. */
2297 /* We must have a mapping from this MEM's address to the
2298 value (E). Remove that, too. */
2299 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
2300 addr = canonical_cselib_val (addr);
2301 gcc_checking_assert (v == canonical_cselib_val (v));
2302 mem_chain = &addr->addr_list;
2303 for (;;)
2305 cselib_val *canon = canonical_cselib_val ((*mem_chain)->elt);
2307 if (canon == v)
2309 unchain_one_elt_list (mem_chain);
2310 break;
2313 /* Record canonicalized elt. */
2314 (*mem_chain)->elt = canon;
2316 mem_chain = &(*mem_chain)->next;
2319 unchain_one_elt_loc_list (p);
2322 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2324 if (setting_insn && DEBUG_INSN_P (setting_insn))
2325 n_useless_debug_values++;
2326 else
2327 n_useless_values++;
2330 next = v->next_containing_mem;
2331 if (has_mem)
2333 *vp = v;
2334 vp = &(*vp)->next_containing_mem;
2336 else
2337 v->next_containing_mem = NULL;
2339 *vp = &dummy_val;
2342 /* Invalidate DEST, which is being assigned to or clobbered. */
2344 void
2345 cselib_invalidate_rtx (rtx dest)
2347 while (GET_CODE (dest) == SUBREG
2348 || GET_CODE (dest) == ZERO_EXTRACT
2349 || GET_CODE (dest) == STRICT_LOW_PART)
2350 dest = XEXP (dest, 0);
2352 if (REG_P (dest))
2353 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
2354 else if (MEM_P (dest))
2355 cselib_invalidate_mem (dest);
2358 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2360 static void
2361 cselib_invalidate_rtx_note_stores (rtx dest, const_rtx ignore ATTRIBUTE_UNUSED,
2362 void *data ATTRIBUTE_UNUSED)
2364 cselib_invalidate_rtx (dest);
2367 /* Record the result of a SET instruction. DEST is being set; the source
2368 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2369 describes its address. */
2371 static void
2372 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
2374 int dreg = REG_P (dest) ? (int) REGNO (dest) : -1;
2376 if (src_elt == 0 || side_effects_p (dest))
2377 return;
2379 if (dreg >= 0)
2381 if (dreg < FIRST_PSEUDO_REGISTER)
2383 unsigned int n = hard_regno_nregs[dreg][GET_MODE (dest)];
2385 if (n > max_value_regs)
2386 max_value_regs = n;
2389 if (REG_VALUES (dreg) == 0)
2391 used_regs[n_used_regs++] = dreg;
2392 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2394 else
2396 /* The register should have been invalidated. */
2397 gcc_assert (REG_VALUES (dreg)->elt == 0);
2398 REG_VALUES (dreg)->elt = src_elt;
2401 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2402 n_useless_values--;
2403 new_elt_loc_list (src_elt, dest);
2405 else if (MEM_P (dest) && dest_addr_elt != 0
2406 && cselib_record_memory)
2408 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2409 n_useless_values--;
2410 add_mem_for_addr (dest_addr_elt, src_elt, dest);
2414 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2416 void
2417 cselib_add_permanent_equiv (cselib_val *elt, rtx x, rtx_insn *insn)
2419 cselib_val *nelt;
2420 rtx_insn *save_cselib_current_insn = cselib_current_insn;
2422 gcc_checking_assert (elt);
2423 gcc_checking_assert (PRESERVED_VALUE_P (elt->val_rtx));
2424 gcc_checking_assert (!side_effects_p (x));
2426 cselib_current_insn = insn;
2428 nelt = cselib_lookup (x, GET_MODE (elt->val_rtx), 1, VOIDmode);
2430 if (nelt != elt)
2432 cselib_any_perm_equivs = true;
2434 if (!PRESERVED_VALUE_P (nelt->val_rtx))
2435 cselib_preserve_value (nelt);
2437 new_elt_loc_list (nelt, elt->val_rtx);
2440 cselib_current_insn = save_cselib_current_insn;
2443 /* Return TRUE if any permanent equivalences have been recorded since
2444 the table was last initialized. */
2445 bool
2446 cselib_have_permanent_equivalences (void)
2448 return cselib_any_perm_equivs;
2451 /* There is no good way to determine how many elements there can be
2452 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2453 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2455 struct cselib_record_autoinc_data
2457 struct cselib_set *sets;
2458 int n_sets;
2461 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2462 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2464 static int
2465 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
2466 rtx dest, rtx src, rtx srcoff, void *arg)
2468 struct cselib_record_autoinc_data *data;
2469 data = (struct cselib_record_autoinc_data *)arg;
2471 data->sets[data->n_sets].dest = dest;
2473 if (srcoff)
2474 data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
2475 else
2476 data->sets[data->n_sets].src = src;
2478 data->n_sets++;
2480 return 0;
2483 /* Record the effects of any sets and autoincs in INSN. */
2484 static void
2485 cselib_record_sets (rtx_insn *insn)
2487 int n_sets = 0;
2488 int i;
2489 struct cselib_set sets[MAX_SETS];
2490 rtx body = PATTERN (insn);
2491 rtx cond = 0;
2492 int n_sets_before_autoinc;
2493 struct cselib_record_autoinc_data data;
2495 body = PATTERN (insn);
2496 if (GET_CODE (body) == COND_EXEC)
2498 cond = COND_EXEC_TEST (body);
2499 body = COND_EXEC_CODE (body);
2502 /* Find all sets. */
2503 if (GET_CODE (body) == SET)
2505 sets[0].src = SET_SRC (body);
2506 sets[0].dest = SET_DEST (body);
2507 n_sets = 1;
2509 else if (GET_CODE (body) == PARALLEL)
2511 /* Look through the PARALLEL and record the values being
2512 set, if possible. Also handle any CLOBBERs. */
2513 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2515 rtx x = XVECEXP (body, 0, i);
2517 if (GET_CODE (x) == SET)
2519 sets[n_sets].src = SET_SRC (x);
2520 sets[n_sets].dest = SET_DEST (x);
2521 n_sets++;
2526 if (n_sets == 1
2527 && MEM_P (sets[0].src)
2528 && !cselib_record_memory
2529 && MEM_READONLY_P (sets[0].src))
2531 rtx note = find_reg_equal_equiv_note (insn);
2533 if (note && CONSTANT_P (XEXP (note, 0)))
2534 sets[0].src = XEXP (note, 0);
2537 data.sets = sets;
2538 data.n_sets = n_sets_before_autoinc = n_sets;
2539 for_each_inc_dec (PATTERN (insn), cselib_record_autoinc_cb, &data);
2540 n_sets = data.n_sets;
2542 /* Look up the values that are read. Do this before invalidating the
2543 locations that are written. */
2544 for (i = 0; i < n_sets; i++)
2546 rtx dest = sets[i].dest;
2548 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2549 the low part after invalidating any knowledge about larger modes. */
2550 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2551 sets[i].dest = dest = XEXP (dest, 0);
2553 /* We don't know how to record anything but REG or MEM. */
2554 if (REG_P (dest)
2555 || (MEM_P (dest) && cselib_record_memory))
2557 rtx src = sets[i].src;
2558 if (cond)
2559 src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
2560 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
2561 if (MEM_P (dest))
2563 machine_mode address_mode = get_address_mode (dest);
2565 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
2566 address_mode, 1,
2567 GET_MODE (dest));
2569 else
2570 sets[i].dest_addr_elt = 0;
2574 if (cselib_record_sets_hook)
2575 cselib_record_sets_hook (insn, sets, n_sets);
2577 /* Invalidate all locations written by this insn. Note that the elts we
2578 looked up in the previous loop aren't affected, just some of their
2579 locations may go away. */
2580 note_stores (body, cselib_invalidate_rtx_note_stores, NULL);
2582 for (i = n_sets_before_autoinc; i < n_sets; i++)
2583 cselib_invalidate_rtx (sets[i].dest);
2585 /* If this is an asm, look for duplicate sets. This can happen when the
2586 user uses the same value as an output multiple times. This is valid
2587 if the outputs are not actually used thereafter. Treat this case as
2588 if the value isn't actually set. We do this by smashing the destination
2589 to pc_rtx, so that we won't record the value later. */
2590 if (n_sets >= 2 && asm_noperands (body) >= 0)
2592 for (i = 0; i < n_sets; i++)
2594 rtx dest = sets[i].dest;
2595 if (REG_P (dest) || MEM_P (dest))
2597 int j;
2598 for (j = i + 1; j < n_sets; j++)
2599 if (rtx_equal_p (dest, sets[j].dest))
2601 sets[i].dest = pc_rtx;
2602 sets[j].dest = pc_rtx;
2608 /* Now enter the equivalences in our tables. */
2609 for (i = 0; i < n_sets; i++)
2611 rtx dest = sets[i].dest;
2612 if (REG_P (dest)
2613 || (MEM_P (dest) && cselib_record_memory))
2614 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2618 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
2620 bool
2621 fp_setter_insn (rtx insn)
2623 rtx expr, pat = NULL_RTX;
2625 if (!RTX_FRAME_RELATED_P (insn))
2626 return false;
2628 expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2629 if (expr)
2630 pat = XEXP (expr, 0);
2631 if (!modified_in_p (hard_frame_pointer_rtx, pat ? pat : insn))
2632 return false;
2634 /* Don't return true for frame pointer restores in the epilogue. */
2635 if (find_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx))
2636 return false;
2637 return true;
2640 /* Record the effects of INSN. */
2642 void
2643 cselib_process_insn (rtx_insn *insn)
2645 int i;
2646 rtx x;
2648 cselib_current_insn = insn;
2650 /* Forget everything at a CODE_LABEL or a setjmp. */
2651 if ((LABEL_P (insn)
2652 || (CALL_P (insn)
2653 && find_reg_note (insn, REG_SETJMP, NULL)))
2654 && !cselib_preserve_constants)
2656 cselib_reset_table (next_uid);
2657 cselib_current_insn = NULL;
2658 return;
2661 if (! INSN_P (insn))
2663 cselib_current_insn = NULL;
2664 return;
2667 /* If this is a call instruction, forget anything stored in a
2668 call clobbered register, or, if this is not a const call, in
2669 memory. */
2670 if (CALL_P (insn))
2672 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2673 if (call_used_regs[i]
2674 || (REG_VALUES (i) && REG_VALUES (i)->elt
2675 && HARD_REGNO_CALL_PART_CLOBBERED (i,
2676 GET_MODE (REG_VALUES (i)->elt->val_rtx))))
2677 cselib_invalidate_regno (i, reg_raw_mode[i]);
2679 /* Since it is not clear how cselib is going to be used, be
2680 conservative here and treat looping pure or const functions
2681 as if they were regular functions. */
2682 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
2683 || !(RTL_CONST_OR_PURE_CALL_P (insn)))
2684 cselib_invalidate_mem (callmem);
2687 cselib_record_sets (insn);
2689 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2690 after we have processed the insn. */
2691 if (CALL_P (insn))
2693 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
2694 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
2695 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
2696 /* Flush evertything on setjmp. */
2697 if (cselib_preserve_constants
2698 && find_reg_note (insn, REG_SETJMP, NULL))
2700 cselib_preserve_only_values ();
2701 cselib_reset_table (next_uid);
2705 /* On setter of the hard frame pointer if frame_pointer_needed,
2706 invalidate stack_pointer_rtx, so that sp and {,h}fp based
2707 VALUEs are distinct. */
2708 if (reload_completed
2709 && frame_pointer_needed
2710 && fp_setter_insn (insn))
2711 cselib_invalidate_rtx (stack_pointer_rtx);
2713 cselib_current_insn = NULL;
2715 if (n_useless_values > MAX_USELESS_VALUES
2716 /* remove_useless_values is linear in the hash table size. Avoid
2717 quadratic behavior for very large hashtables with very few
2718 useless elements. */
2719 && ((unsigned int)n_useless_values
2720 > (cselib_hash_table->elements () - n_debug_values) / 4))
2721 remove_useless_values ();
2724 /* Initialize cselib for one pass. The caller must also call
2725 init_alias_analysis. */
2727 void
2728 cselib_init (int record_what)
2730 elt_list_pool = create_alloc_pool ("elt_list",
2731 sizeof (struct elt_list), 10);
2732 elt_loc_list_pool = create_alloc_pool ("elt_loc_list",
2733 sizeof (struct elt_loc_list), 10);
2734 cselib_val_pool = create_alloc_pool ("cselib_val_list",
2735 sizeof (cselib_val), 10);
2736 value_pool = create_alloc_pool ("value", RTX_CODE_SIZE (VALUE), 100);
2737 cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
2738 cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
2739 cselib_any_perm_equivs = false;
2741 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2742 see canon_true_dependence. This is only created once. */
2743 if (! callmem)
2744 callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2746 cselib_nregs = max_reg_num ();
2748 /* We preserve reg_values to allow expensive clearing of the whole thing.
2749 Reallocate it however if it happens to be too large. */
2750 if (!reg_values || reg_values_size < cselib_nregs
2751 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
2753 free (reg_values);
2754 /* Some space for newly emit instructions so we don't end up
2755 reallocating in between passes. */
2756 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
2757 reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
2759 used_regs = XNEWVEC (unsigned int, cselib_nregs);
2760 n_used_regs = 0;
2761 cselib_hash_table = new hash_table<cselib_hasher> (31);
2762 if (cselib_preserve_constants)
2763 cselib_preserved_hash_table = new hash_table<cselib_hasher> (31);
2764 next_uid = 1;
2767 /* Called when the current user is done with cselib. */
2769 void
2770 cselib_finish (void)
2772 bool preserved = cselib_preserve_constants;
2773 cselib_discard_hook = NULL;
2774 cselib_preserve_constants = false;
2775 cselib_any_perm_equivs = false;
2776 cfa_base_preserved_val = NULL;
2777 cfa_base_preserved_regno = INVALID_REGNUM;
2778 free_alloc_pool (elt_list_pool);
2779 free_alloc_pool (elt_loc_list_pool);
2780 free_alloc_pool (cselib_val_pool);
2781 free_alloc_pool (value_pool);
2782 cselib_clear_table ();
2783 delete cselib_hash_table;
2784 cselib_hash_table = NULL;
2785 if (preserved)
2786 delete cselib_preserved_hash_table;
2787 cselib_preserved_hash_table = NULL;
2788 free (used_regs);
2789 used_regs = 0;
2790 n_useless_values = 0;
2791 n_useless_debug_values = 0;
2792 n_debug_values = 0;
2793 next_uid = 0;
2796 /* Dump the cselib_val *X to FILE *OUT. */
2799 dump_cselib_val (cselib_val **x, FILE *out)
2801 cselib_val *v = *x;
2802 bool need_lf = true;
2804 print_inline_rtx (out, v->val_rtx, 0);
2806 if (v->locs)
2808 struct elt_loc_list *l = v->locs;
2809 if (need_lf)
2811 fputc ('\n', out);
2812 need_lf = false;
2814 fputs (" locs:", out);
2817 if (l->setting_insn)
2818 fprintf (out, "\n from insn %i ",
2819 INSN_UID (l->setting_insn));
2820 else
2821 fprintf (out, "\n ");
2822 print_inline_rtx (out, l->loc, 4);
2824 while ((l = l->next));
2825 fputc ('\n', out);
2827 else
2829 fputs (" no locs", out);
2830 need_lf = true;
2833 if (v->addr_list)
2835 struct elt_list *e = v->addr_list;
2836 if (need_lf)
2838 fputc ('\n', out);
2839 need_lf = false;
2841 fputs (" addr list:", out);
2844 fputs ("\n ", out);
2845 print_inline_rtx (out, e->elt->val_rtx, 2);
2847 while ((e = e->next));
2848 fputc ('\n', out);
2850 else
2852 fputs (" no addrs", out);
2853 need_lf = true;
2856 if (v->next_containing_mem == &dummy_val)
2857 fputs (" last mem\n", out);
2858 else if (v->next_containing_mem)
2860 fputs (" next mem ", out);
2861 print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
2862 fputc ('\n', out);
2864 else if (need_lf)
2865 fputc ('\n', out);
2867 return 1;
2870 /* Dump to OUT everything in the CSELIB table. */
2872 void
2873 dump_cselib_table (FILE *out)
2875 fprintf (out, "cselib hash table:\n");
2876 cselib_hash_table->traverse <FILE *, dump_cselib_val> (out);
2877 fprintf (out, "cselib preserved hash table:\n");
2878 cselib_preserved_hash_table->traverse <FILE *, dump_cselib_val> (out);
2879 if (first_containing_mem != &dummy_val)
2881 fputs ("first mem ", out);
2882 print_inline_rtx (out, first_containing_mem->val_rtx, 2);
2883 fputc ('\n', out);
2885 fprintf (out, "next uid %i\n", next_uid);
2888 #include "gt-cselib.h"