PR middle-end/66633
[official-gcc.git] / gcc / cselib.c
blob6c04813dcc126f837cfae47377ab489a7716df26
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 "alias.h"
26 #include "symtab.h"
27 #include "tree.h"/* FIXME: For hashing DEBUG_EXPR & friends. */
28 #include "tm_p.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "flags.h"
32 #include "insn-config.h"
33 #include "recog.h"
34 #include "function.h"
35 #include "emit-rtl.h"
36 #include "diagnostic-core.h"
37 #include "dumpfile.h"
38 #include "alloc-pool.h"
39 #include "cselib.h"
40 #include "predict.h"
41 #include "basic-block.h"
42 #include "valtrack.h"
43 #include "params.h"
44 #include "alloc-pool.h"
45 #include "target.h"
46 #include "bitmap.h"
48 /* A list of cselib_val structures. */
49 struct elt_list
51 struct elt_list *next;
52 cselib_val *elt;
54 /* Pool allocation new operator. */
55 inline void *operator new (size_t)
57 return pool.allocate ();
60 /* Delete operator utilizing pool allocation. */
61 inline void operator delete (void *ptr)
63 pool.remove ((elt_list *) ptr);
66 /* Memory allocation pool. */
67 static pool_allocator<elt_list> pool;
70 static bool cselib_record_memory;
71 static bool cselib_preserve_constants;
72 static bool cselib_any_perm_equivs;
73 static inline void promote_debug_loc (struct elt_loc_list *l);
74 static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
75 static void new_elt_loc_list (cselib_val *, rtx);
76 static void unchain_one_value (cselib_val *);
77 static void unchain_one_elt_list (struct elt_list **);
78 static void unchain_one_elt_loc_list (struct elt_loc_list **);
79 static void remove_useless_values (void);
80 static int rtx_equal_for_cselib_1 (rtx, rtx, machine_mode);
81 static unsigned int cselib_hash_rtx (rtx, int, machine_mode);
82 static cselib_val *new_cselib_val (unsigned int, machine_mode, rtx);
83 static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
84 static cselib_val *cselib_lookup_mem (rtx, int);
85 static void cselib_invalidate_regno (unsigned int, machine_mode);
86 static void cselib_invalidate_mem (rtx);
87 static void cselib_record_set (rtx, cselib_val *, cselib_val *);
88 static void cselib_record_sets (rtx_insn *);
90 struct expand_value_data
92 bitmap regs_active;
93 cselib_expand_callback callback;
94 void *callback_arg;
95 bool dummy;
98 static rtx cselib_expand_value_rtx_1 (rtx, struct expand_value_data *, int);
100 /* There are three ways in which cselib can look up an rtx:
101 - for a REG, the reg_values table (which is indexed by regno) is used
102 - for a MEM, we recursively look up its address and then follow the
103 addr_list of that value
104 - for everything else, we compute a hash value and go through the hash
105 table. Since different rtx's can still have the same hash value,
106 this involves walking the table entries for a given value and comparing
107 the locations of the entries with the rtx we are looking up. */
109 struct cselib_hasher : nofree_ptr_hash <cselib_val>
111 struct key {
112 /* The rtx value and its mode (needed separately for constant
113 integers). */
114 machine_mode mode;
115 rtx x;
116 /* The mode of the contaning MEM, if any, otherwise VOIDmode. */
117 machine_mode memmode;
119 typedef key *compare_type;
120 static inline hashval_t hash (const cselib_val *);
121 static inline bool equal (const cselib_val *, const key *);
124 /* The hash function for our hash table. The value is always computed with
125 cselib_hash_rtx when adding an element; this function just extracts the
126 hash value from a cselib_val structure. */
128 inline hashval_t
129 cselib_hasher::hash (const cselib_val *v)
131 return v->hash;
134 /* The equality test for our hash table. The first argument V is a table
135 element (i.e. a cselib_val), while the second arg X is an rtx. We know
136 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
137 CONST of an appropriate mode. */
139 inline bool
140 cselib_hasher::equal (const cselib_val *v, const key *x_arg)
142 struct elt_loc_list *l;
143 rtx x = x_arg->x;
144 machine_mode mode = x_arg->mode;
145 machine_mode memmode = x_arg->memmode;
147 if (mode != GET_MODE (v->val_rtx))
148 return false;
150 if (GET_CODE (x) == VALUE)
151 return x == v->val_rtx;
153 /* We don't guarantee that distinct rtx's have different hash values,
154 so we need to do a comparison. */
155 for (l = v->locs; l; l = l->next)
156 if (rtx_equal_for_cselib_1 (l->loc, x, memmode))
158 promote_debug_loc (l);
159 return true;
162 return false;
165 /* A table that enables us to look up elts by their value. */
166 static hash_table<cselib_hasher> *cselib_hash_table;
168 /* A table to hold preserved values. */
169 static hash_table<cselib_hasher> *cselib_preserved_hash_table;
171 /* This is a global so we don't have to pass this through every function.
172 It is used in new_elt_loc_list to set SETTING_INSN. */
173 static rtx_insn *cselib_current_insn;
175 /* The unique id that the next create value will take. */
176 static unsigned int next_uid;
178 /* The number of registers we had when the varrays were last resized. */
179 static unsigned int cselib_nregs;
181 /* Count values without known locations, or with only locations that
182 wouldn't have been known except for debug insns. Whenever this
183 grows too big, we remove these useless values from the table.
185 Counting values with only debug values is a bit tricky. We don't
186 want to increment n_useless_values when we create a value for a
187 debug insn, for this would get n_useless_values out of sync, but we
188 want increment it if all locs in the list that were ever referenced
189 in nondebug insns are removed from the list.
191 In the general case, once we do that, we'd have to stop accepting
192 nondebug expressions in the loc list, to avoid having two values
193 equivalent that, without debug insns, would have been made into
194 separate values. However, because debug insns never introduce
195 equivalences themselves (no assignments), the only means for
196 growing loc lists is through nondebug assignments. If the locs
197 also happen to be referenced in debug insns, it will work just fine.
199 A consequence of this is that there's at most one debug-only loc in
200 each loc list. If we keep it in the first entry, testing whether
201 we have a debug-only loc list takes O(1).
203 Furthermore, since any additional entry in a loc list containing a
204 debug loc would have to come from an assignment (nondebug) that
205 references both the initial debug loc and the newly-equivalent loc,
206 the initial debug loc would be promoted to a nondebug loc, and the
207 loc list would not contain debug locs any more.
209 So the only case we have to be careful with in order to keep
210 n_useless_values in sync between debug and nondebug compilations is
211 to avoid incrementing n_useless_values when removing the single loc
212 from a value that turns out to not appear outside debug values. We
213 increment n_useless_debug_values instead, and leave such values
214 alone until, for other reasons, we garbage-collect useless
215 values. */
216 static int n_useless_values;
217 static int n_useless_debug_values;
219 /* Count values whose locs have been taken exclusively from debug
220 insns for the entire life of the value. */
221 static int n_debug_values;
223 /* Number of useless values before we remove them from the hash table. */
224 #define MAX_USELESS_VALUES 32
226 /* This table maps from register number to values. It does not
227 contain pointers to cselib_val structures, but rather elt_lists.
228 The purpose is to be able to refer to the same register in
229 different modes. The first element of the list defines the mode in
230 which the register was set; if the mode is unknown or the value is
231 no longer valid in that mode, ELT will be NULL for the first
232 element. */
233 static struct elt_list **reg_values;
234 static unsigned int reg_values_size;
235 #define REG_VALUES(i) reg_values[i]
237 /* The largest number of hard regs used by any entry added to the
238 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
239 static unsigned int max_value_regs;
241 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
242 in cselib_clear_table() for fast emptying. */
243 static unsigned int *used_regs;
244 static unsigned int n_used_regs;
246 /* We pass this to cselib_invalidate_mem to invalidate all of
247 memory for a non-const call instruction. */
248 static GTY(()) rtx callmem;
250 /* Set by discard_useless_locs if it deleted the last location of any
251 value. */
252 static int values_became_useless;
254 /* Used as stop element of the containing_mem list so we can check
255 presence in the list by checking the next pointer. */
256 static cselib_val dummy_val;
258 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
259 that is constant through the whole function and should never be
260 eliminated. */
261 static cselib_val *cfa_base_preserved_val;
262 static unsigned int cfa_base_preserved_regno = INVALID_REGNUM;
264 /* Used to list all values that contain memory reference.
265 May or may not contain the useless values - the list is compacted
266 each time memory is invalidated. */
267 static cselib_val *first_containing_mem = &dummy_val;
269 pool_allocator<elt_list> elt_list::pool ("elt_list", 10);
270 pool_allocator<elt_loc_list> elt_loc_list::pool ("elt_loc_list", 10);
271 pool_allocator<cselib_val> cselib_val::pool ("cselib_val_list", 10);
273 static pool_allocator<rtx_def> value_pool ("value", 100, RTX_CODE_SIZE (VALUE),
274 true);
276 /* If nonnull, cselib will call this function before freeing useless
277 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
278 void (*cselib_discard_hook) (cselib_val *);
280 /* If nonnull, cselib will call this function before recording sets or
281 even clobbering outputs of INSN. All the recorded sets will be
282 represented in the array sets[n_sets]. new_val_min can be used to
283 tell whether values present in sets are introduced by this
284 instruction. */
285 void (*cselib_record_sets_hook) (rtx_insn *insn, struct cselib_set *sets,
286 int n_sets);
288 #define PRESERVED_VALUE_P(RTX) \
289 (RTL_FLAG_CHECK1 ("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
291 #define SP_BASED_VALUE_P(RTX) \
292 (RTL_FLAG_CHECK1 ("SP_BASED_VALUE_P", (RTX), VALUE)->jump)
296 /* Allocate a struct elt_list and fill in its two elements with the
297 arguments. */
299 static inline struct elt_list *
300 new_elt_list (struct elt_list *next, cselib_val *elt)
302 elt_list *el = new elt_list ();
303 el->next = next;
304 el->elt = elt;
305 return el;
308 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
309 list. */
311 static inline void
312 new_elt_loc_list (cselib_val *val, rtx loc)
314 struct elt_loc_list *el, *next = val->locs;
316 gcc_checking_assert (!next || !next->setting_insn
317 || !DEBUG_INSN_P (next->setting_insn)
318 || cselib_current_insn == next->setting_insn);
320 /* If we're creating the first loc in a debug insn context, we've
321 just created a debug value. Count it. */
322 if (!next && cselib_current_insn && DEBUG_INSN_P (cselib_current_insn))
323 n_debug_values++;
325 val = canonical_cselib_val (val);
326 next = val->locs;
328 if (GET_CODE (loc) == VALUE)
330 loc = canonical_cselib_val (CSELIB_VAL_PTR (loc))->val_rtx;
332 gcc_checking_assert (PRESERVED_VALUE_P (loc)
333 == PRESERVED_VALUE_P (val->val_rtx));
335 if (val->val_rtx == loc)
336 return;
337 else if (val->uid > CSELIB_VAL_PTR (loc)->uid)
339 /* Reverse the insertion. */
340 new_elt_loc_list (CSELIB_VAL_PTR (loc), val->val_rtx);
341 return;
344 gcc_checking_assert (val->uid < CSELIB_VAL_PTR (loc)->uid);
346 if (CSELIB_VAL_PTR (loc)->locs)
348 /* Bring all locs from LOC to VAL. */
349 for (el = CSELIB_VAL_PTR (loc)->locs; el->next; el = el->next)
351 /* Adjust values that have LOC as canonical so that VAL
352 becomes their canonical. */
353 if (el->loc && GET_CODE (el->loc) == VALUE)
355 gcc_checking_assert (CSELIB_VAL_PTR (el->loc)->locs->loc
356 == loc);
357 CSELIB_VAL_PTR (el->loc)->locs->loc = val->val_rtx;
360 el->next = val->locs;
361 next = val->locs = CSELIB_VAL_PTR (loc)->locs;
364 if (CSELIB_VAL_PTR (loc)->addr_list)
366 /* Bring in addr_list into canonical node. */
367 struct elt_list *last = CSELIB_VAL_PTR (loc)->addr_list;
368 while (last->next)
369 last = last->next;
370 last->next = val->addr_list;
371 val->addr_list = CSELIB_VAL_PTR (loc)->addr_list;
372 CSELIB_VAL_PTR (loc)->addr_list = NULL;
375 if (CSELIB_VAL_PTR (loc)->next_containing_mem != NULL
376 && val->next_containing_mem == NULL)
378 /* Add VAL to the containing_mem list after LOC. LOC will
379 be removed when we notice it doesn't contain any
380 MEMs. */
381 val->next_containing_mem = CSELIB_VAL_PTR (loc)->next_containing_mem;
382 CSELIB_VAL_PTR (loc)->next_containing_mem = val;
385 /* Chain LOC back to VAL. */
386 el = new elt_loc_list;
387 el->loc = val->val_rtx;
388 el->setting_insn = cselib_current_insn;
389 el->next = NULL;
390 CSELIB_VAL_PTR (loc)->locs = el;
393 el = new elt_loc_list;
394 el->loc = loc;
395 el->setting_insn = cselib_current_insn;
396 el->next = next;
397 val->locs = el;
400 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
401 originating from a debug insn, maintaining the debug values
402 count. */
404 static inline void
405 promote_debug_loc (struct elt_loc_list *l)
407 if (l && l->setting_insn && DEBUG_INSN_P (l->setting_insn)
408 && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
410 n_debug_values--;
411 l->setting_insn = cselib_current_insn;
412 if (cselib_preserve_constants && l->next)
414 gcc_assert (l->next->setting_insn
415 && DEBUG_INSN_P (l->next->setting_insn)
416 && !l->next->next);
417 l->next->setting_insn = cselib_current_insn;
419 else
420 gcc_assert (!l->next);
424 /* The elt_list at *PL is no longer needed. Unchain it and free its
425 storage. */
427 static inline void
428 unchain_one_elt_list (struct elt_list **pl)
430 struct elt_list *l = *pl;
432 *pl = l->next;
433 delete l;
436 /* Likewise for elt_loc_lists. */
438 static void
439 unchain_one_elt_loc_list (struct elt_loc_list **pl)
441 struct elt_loc_list *l = *pl;
443 *pl = l->next;
444 delete l;
447 /* Likewise for cselib_vals. This also frees the addr_list associated with
448 V. */
450 static void
451 unchain_one_value (cselib_val *v)
453 while (v->addr_list)
454 unchain_one_elt_list (&v->addr_list);
456 delete v;
459 /* Remove all entries from the hash table. Also used during
460 initialization. */
462 void
463 cselib_clear_table (void)
465 cselib_reset_table (1);
468 /* Return TRUE if V is a constant, a function invariant or a VALUE
469 equivalence; FALSE otherwise. */
471 static bool
472 invariant_or_equiv_p (cselib_val *v)
474 struct elt_loc_list *l;
476 if (v == cfa_base_preserved_val)
477 return true;
479 /* Keep VALUE equivalences around. */
480 for (l = v->locs; l; l = l->next)
481 if (GET_CODE (l->loc) == VALUE)
482 return true;
484 if (v->locs != NULL
485 && v->locs->next == NULL)
487 if (CONSTANT_P (v->locs->loc)
488 && (GET_CODE (v->locs->loc) != CONST
489 || !references_value_p (v->locs->loc, 0)))
490 return true;
491 /* Although a debug expr may be bound to different expressions,
492 we can preserve it as if it was constant, to get unification
493 and proper merging within var-tracking. */
494 if (GET_CODE (v->locs->loc) == DEBUG_EXPR
495 || GET_CODE (v->locs->loc) == DEBUG_IMPLICIT_PTR
496 || GET_CODE (v->locs->loc) == ENTRY_VALUE
497 || GET_CODE (v->locs->loc) == DEBUG_PARAMETER_REF)
498 return true;
500 /* (plus (value V) (const_int C)) is invariant iff V is invariant. */
501 if (GET_CODE (v->locs->loc) == PLUS
502 && CONST_INT_P (XEXP (v->locs->loc, 1))
503 && GET_CODE (XEXP (v->locs->loc, 0)) == VALUE
504 && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v->locs->loc, 0))))
505 return true;
508 return false;
511 /* Remove from hash table all VALUEs except constants, function
512 invariants and VALUE equivalences. */
515 preserve_constants_and_equivs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
517 cselib_val *v = *x;
519 if (invariant_or_equiv_p (v))
521 cselib_hasher::key lookup = {
522 GET_MODE (v->val_rtx), v->val_rtx, VOIDmode
524 cselib_val **slot
525 = cselib_preserved_hash_table->find_slot_with_hash (&lookup,
526 v->hash, INSERT);
527 gcc_assert (!*slot);
528 *slot = v;
531 cselib_hash_table->clear_slot (x);
533 return 1;
536 /* Remove all entries from the hash table, arranging for the next
537 value to be numbered NUM. */
539 void
540 cselib_reset_table (unsigned int num)
542 unsigned int i;
544 max_value_regs = 0;
546 if (cfa_base_preserved_val)
548 unsigned int regno = cfa_base_preserved_regno;
549 unsigned int new_used_regs = 0;
550 for (i = 0; i < n_used_regs; i++)
551 if (used_regs[i] == regno)
553 new_used_regs = 1;
554 continue;
556 else
557 REG_VALUES (used_regs[i]) = 0;
558 gcc_assert (new_used_regs == 1);
559 n_used_regs = new_used_regs;
560 used_regs[0] = regno;
561 max_value_regs
562 = hard_regno_nregs[regno][GET_MODE (cfa_base_preserved_val->locs->loc)];
564 else
566 for (i = 0; i < n_used_regs; i++)
567 REG_VALUES (used_regs[i]) = 0;
568 n_used_regs = 0;
571 if (cselib_preserve_constants)
572 cselib_hash_table->traverse <void *, preserve_constants_and_equivs>
573 (NULL);
574 else
576 cselib_hash_table->empty ();
577 gcc_checking_assert (!cselib_any_perm_equivs);
580 n_useless_values = 0;
581 n_useless_debug_values = 0;
582 n_debug_values = 0;
584 next_uid = num;
586 first_containing_mem = &dummy_val;
589 /* Return the number of the next value that will be generated. */
591 unsigned int
592 cselib_get_next_uid (void)
594 return next_uid;
597 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
598 INSERTing if requested. When X is part of the address of a MEM,
599 MEMMODE should specify the mode of the MEM. */
601 static cselib_val **
602 cselib_find_slot (machine_mode mode, rtx x, hashval_t hash,
603 enum insert_option insert, machine_mode memmode)
605 cselib_val **slot = NULL;
606 cselib_hasher::key lookup = { mode, x, memmode };
607 if (cselib_preserve_constants)
608 slot = cselib_preserved_hash_table->find_slot_with_hash (&lookup, hash,
609 NO_INSERT);
610 if (!slot)
611 slot = cselib_hash_table->find_slot_with_hash (&lookup, hash, insert);
612 return slot;
615 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
616 only return true for values which point to a cselib_val whose value
617 element has been set to zero, which implies the cselib_val will be
618 removed. */
621 references_value_p (const_rtx x, int only_useless)
623 const enum rtx_code code = GET_CODE (x);
624 const char *fmt = GET_RTX_FORMAT (code);
625 int i, j;
627 if (GET_CODE (x) == VALUE
628 && (! only_useless ||
629 (CSELIB_VAL_PTR (x)->locs == 0 && !PRESERVED_VALUE_P (x))))
630 return 1;
632 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
634 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
635 return 1;
636 else if (fmt[i] == 'E')
637 for (j = 0; j < XVECLEN (x, i); j++)
638 if (references_value_p (XVECEXP (x, i, j), only_useless))
639 return 1;
642 return 0;
645 /* For all locations found in X, delete locations that reference useless
646 values (i.e. values without any location). Called through
647 htab_traverse. */
650 discard_useless_locs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
652 cselib_val *v = *x;
653 struct elt_loc_list **p = &v->locs;
654 bool had_locs = v->locs != NULL;
655 rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
657 while (*p)
659 if (references_value_p ((*p)->loc, 1))
660 unchain_one_elt_loc_list (p);
661 else
662 p = &(*p)->next;
665 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
667 if (setting_insn && DEBUG_INSN_P (setting_insn))
668 n_useless_debug_values++;
669 else
670 n_useless_values++;
671 values_became_useless = 1;
673 return 1;
676 /* If X is a value with no locations, remove it from the hashtable. */
679 discard_useless_values (cselib_val **x, void *info ATTRIBUTE_UNUSED)
681 cselib_val *v = *x;
683 if (v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
685 if (cselib_discard_hook)
686 cselib_discard_hook (v);
688 CSELIB_VAL_PTR (v->val_rtx) = NULL;
689 cselib_hash_table->clear_slot (x);
690 unchain_one_value (v);
691 n_useless_values--;
694 return 1;
697 /* Clean out useless values (i.e. those which no longer have locations
698 associated with them) from the hash table. */
700 static void
701 remove_useless_values (void)
703 cselib_val **p, *v;
705 /* First pass: eliminate locations that reference the value. That in
706 turn can make more values useless. */
709 values_became_useless = 0;
710 cselib_hash_table->traverse <void *, discard_useless_locs> (NULL);
712 while (values_became_useless);
714 /* Second pass: actually remove the values. */
716 p = &first_containing_mem;
717 for (v = *p; v != &dummy_val; v = v->next_containing_mem)
718 if (v->locs && v == canonical_cselib_val (v))
720 *p = v;
721 p = &(*p)->next_containing_mem;
723 *p = &dummy_val;
725 n_useless_values += n_useless_debug_values;
726 n_debug_values -= n_useless_debug_values;
727 n_useless_debug_values = 0;
729 cselib_hash_table->traverse <void *, discard_useless_values> (NULL);
731 gcc_assert (!n_useless_values);
734 /* Arrange for a value to not be removed from the hash table even if
735 it becomes useless. */
737 void
738 cselib_preserve_value (cselib_val *v)
740 PRESERVED_VALUE_P (v->val_rtx) = 1;
743 /* Test whether a value is preserved. */
745 bool
746 cselib_preserved_value_p (cselib_val *v)
748 return PRESERVED_VALUE_P (v->val_rtx);
751 /* Arrange for a REG value to be assumed constant through the whole function,
752 never invalidated and preserved across cselib_reset_table calls. */
754 void
755 cselib_preserve_cfa_base_value (cselib_val *v, unsigned int regno)
757 if (cselib_preserve_constants
758 && v->locs
759 && REG_P (v->locs->loc))
761 cfa_base_preserved_val = v;
762 cfa_base_preserved_regno = regno;
766 /* Clean all non-constant expressions in the hash table, but retain
767 their values. */
769 void
770 cselib_preserve_only_values (void)
772 int i;
774 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
775 cselib_invalidate_regno (i, reg_raw_mode[i]);
777 cselib_invalidate_mem (callmem);
779 remove_useless_values ();
781 gcc_assert (first_containing_mem == &dummy_val);
784 /* Arrange for a value to be marked as based on stack pointer
785 for find_base_term purposes. */
787 void
788 cselib_set_value_sp_based (cselib_val *v)
790 SP_BASED_VALUE_P (v->val_rtx) = 1;
793 /* Test whether a value is based on stack pointer for
794 find_base_term purposes. */
796 bool
797 cselib_sp_based_value_p (cselib_val *v)
799 return SP_BASED_VALUE_P (v->val_rtx);
802 /* Return the mode in which a register was last set. If X is not a
803 register, return its mode. If the mode in which the register was
804 set is not known, or the value was already clobbered, return
805 VOIDmode. */
807 machine_mode
808 cselib_reg_set_mode (const_rtx x)
810 if (!REG_P (x))
811 return GET_MODE (x);
813 if (REG_VALUES (REGNO (x)) == NULL
814 || REG_VALUES (REGNO (x))->elt == NULL)
815 return VOIDmode;
817 return GET_MODE (REG_VALUES (REGNO (x))->elt->val_rtx);
820 /* Return nonzero if we can prove that X and Y contain the same value, taking
821 our gathered information into account. */
824 rtx_equal_for_cselib_p (rtx x, rtx y)
826 return rtx_equal_for_cselib_1 (x, y, VOIDmode);
829 /* If x is a PLUS or an autoinc operation, expand the operation,
830 storing the offset, if any, in *OFF. */
832 static rtx
833 autoinc_split (rtx x, rtx *off, machine_mode memmode)
835 switch (GET_CODE (x))
837 case PLUS:
838 *off = XEXP (x, 1);
839 return XEXP (x, 0);
841 case PRE_DEC:
842 if (memmode == VOIDmode)
843 return x;
845 *off = GEN_INT (-GET_MODE_SIZE (memmode));
846 return XEXP (x, 0);
847 break;
849 case PRE_INC:
850 if (memmode == VOIDmode)
851 return x;
853 *off = GEN_INT (GET_MODE_SIZE (memmode));
854 return XEXP (x, 0);
856 case PRE_MODIFY:
857 return XEXP (x, 1);
859 case POST_DEC:
860 case POST_INC:
861 case POST_MODIFY:
862 return XEXP (x, 0);
864 default:
865 return x;
869 /* Return nonzero if we can prove that X and Y contain the same value,
870 taking our gathered information into account. MEMMODE holds the
871 mode of the enclosing MEM, if any, as required to deal with autoinc
872 addressing modes. If X and Y are not (known to be) part of
873 addresses, MEMMODE should be VOIDmode. */
875 static int
876 rtx_equal_for_cselib_1 (rtx x, rtx y, machine_mode memmode)
878 enum rtx_code code;
879 const char *fmt;
880 int i;
882 if (REG_P (x) || MEM_P (x))
884 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0, memmode);
886 if (e)
887 x = e->val_rtx;
890 if (REG_P (y) || MEM_P (y))
892 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0, memmode);
894 if (e)
895 y = e->val_rtx;
898 if (x == y)
899 return 1;
901 if (GET_CODE (x) == VALUE)
903 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (x));
904 struct elt_loc_list *l;
906 if (GET_CODE (y) == VALUE)
907 return e == canonical_cselib_val (CSELIB_VAL_PTR (y));
909 for (l = e->locs; l; l = l->next)
911 rtx t = l->loc;
913 /* Avoid infinite recursion. We know we have the canonical
914 value, so we can just skip any values in the equivalence
915 list. */
916 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
917 continue;
918 else if (rtx_equal_for_cselib_1 (t, y, memmode))
919 return 1;
922 return 0;
924 else if (GET_CODE (y) == VALUE)
926 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (y));
927 struct elt_loc_list *l;
929 for (l = e->locs; l; l = l->next)
931 rtx t = l->loc;
933 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
934 continue;
935 else if (rtx_equal_for_cselib_1 (x, t, memmode))
936 return 1;
939 return 0;
942 if (GET_MODE (x) != GET_MODE (y))
943 return 0;
945 if (GET_CODE (x) != GET_CODE (y))
947 rtx xorig = x, yorig = y;
948 rtx xoff = NULL, yoff = NULL;
950 x = autoinc_split (x, &xoff, memmode);
951 y = autoinc_split (y, &yoff, memmode);
953 if (!xoff != !yoff)
954 return 0;
956 if (xoff && !rtx_equal_for_cselib_1 (xoff, yoff, memmode))
957 return 0;
959 /* Don't recurse if nothing changed. */
960 if (x != xorig || y != yorig)
961 return rtx_equal_for_cselib_1 (x, y, memmode);
963 return 0;
966 /* These won't be handled correctly by the code below. */
967 switch (GET_CODE (x))
969 CASE_CONST_UNIQUE:
970 case DEBUG_EXPR:
971 return 0;
973 case DEBUG_IMPLICIT_PTR:
974 return DEBUG_IMPLICIT_PTR_DECL (x)
975 == DEBUG_IMPLICIT_PTR_DECL (y);
977 case DEBUG_PARAMETER_REF:
978 return DEBUG_PARAMETER_REF_DECL (x)
979 == DEBUG_PARAMETER_REF_DECL (y);
981 case ENTRY_VALUE:
982 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
983 use rtx_equal_for_cselib_1 to compare the operands. */
984 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
986 case LABEL_REF:
987 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
989 case REG:
990 return REGNO (x) == REGNO (y);
992 case MEM:
993 /* We have to compare any autoinc operations in the addresses
994 using this MEM's mode. */
995 return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x));
997 default:
998 break;
1001 code = GET_CODE (x);
1002 fmt = GET_RTX_FORMAT (code);
1004 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1006 int j;
1008 switch (fmt[i])
1010 case 'w':
1011 if (XWINT (x, i) != XWINT (y, i))
1012 return 0;
1013 break;
1015 case 'n':
1016 case 'i':
1017 if (XINT (x, i) != XINT (y, i))
1018 return 0;
1019 break;
1021 case 'V':
1022 case 'E':
1023 /* Two vectors must have the same length. */
1024 if (XVECLEN (x, i) != XVECLEN (y, i))
1025 return 0;
1027 /* And the corresponding elements must match. */
1028 for (j = 0; j < XVECLEN (x, i); j++)
1029 if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
1030 XVECEXP (y, i, j), memmode))
1031 return 0;
1032 break;
1034 case 'e':
1035 if (i == 1
1036 && targetm.commutative_p (x, UNKNOWN)
1037 && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode)
1038 && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode))
1039 return 1;
1040 if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode))
1041 return 0;
1042 break;
1044 case 'S':
1045 case 's':
1046 if (strcmp (XSTR (x, i), XSTR (y, i)))
1047 return 0;
1048 break;
1050 case 'u':
1051 /* These are just backpointers, so they don't matter. */
1052 break;
1054 case '0':
1055 case 't':
1056 break;
1058 /* It is believed that rtx's at this level will never
1059 contain anything but integers and other rtx's,
1060 except for within LABEL_REFs and SYMBOL_REFs. */
1061 default:
1062 gcc_unreachable ();
1065 return 1;
1068 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1069 For registers and memory locations, we look up their cselib_val structure
1070 and return its VALUE element.
1071 Possible reasons for return 0 are: the object is volatile, or we couldn't
1072 find a register or memory location in the table and CREATE is zero. If
1073 CREATE is nonzero, table elts are created for regs and mem.
1074 N.B. this hash function returns the same hash value for RTXes that
1075 differ only in the order of operands, thus it is suitable for comparisons
1076 that take commutativity into account.
1077 If we wanted to also support associative rules, we'd have to use a different
1078 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1079 MEMMODE indicates the mode of an enclosing MEM, and it's only
1080 used to compute autoinc values.
1081 We used to have a MODE argument for hashing for CONST_INTs, but that
1082 didn't make sense, since it caused spurious hash differences between
1083 (set (reg:SI 1) (const_int))
1084 (plus:SI (reg:SI 2) (reg:SI 1))
1086 (plus:SI (reg:SI 2) (const_int))
1087 If the mode is important in any context, it must be checked specifically
1088 in a comparison anyway, since relying on hash differences is unsafe. */
1090 static unsigned int
1091 cselib_hash_rtx (rtx x, int create, machine_mode memmode)
1093 cselib_val *e;
1094 int i, j;
1095 enum rtx_code code;
1096 const char *fmt;
1097 unsigned int hash = 0;
1099 code = GET_CODE (x);
1100 hash += (unsigned) code + (unsigned) GET_MODE (x);
1102 switch (code)
1104 case VALUE:
1105 e = CSELIB_VAL_PTR (x);
1106 return e->hash;
1108 case MEM:
1109 case REG:
1110 e = cselib_lookup (x, GET_MODE (x), create, memmode);
1111 if (! e)
1112 return 0;
1114 return e->hash;
1116 case DEBUG_EXPR:
1117 hash += ((unsigned) DEBUG_EXPR << 7)
1118 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
1119 return hash ? hash : (unsigned int) DEBUG_EXPR;
1121 case DEBUG_IMPLICIT_PTR:
1122 hash += ((unsigned) DEBUG_IMPLICIT_PTR << 7)
1123 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x));
1124 return hash ? hash : (unsigned int) DEBUG_IMPLICIT_PTR;
1126 case DEBUG_PARAMETER_REF:
1127 hash += ((unsigned) DEBUG_PARAMETER_REF << 7)
1128 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x));
1129 return hash ? hash : (unsigned int) DEBUG_PARAMETER_REF;
1131 case ENTRY_VALUE:
1132 /* ENTRY_VALUEs are function invariant, thus try to avoid
1133 recursing on argument if ENTRY_VALUE is one of the
1134 forms emitted by expand_debug_expr, otherwise
1135 ENTRY_VALUE hash would depend on the current value
1136 in some register or memory. */
1137 if (REG_P (ENTRY_VALUE_EXP (x)))
1138 hash += (unsigned int) REG
1139 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x))
1140 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x));
1141 else if (MEM_P (ENTRY_VALUE_EXP (x))
1142 && REG_P (XEXP (ENTRY_VALUE_EXP (x), 0)))
1143 hash += (unsigned int) MEM
1144 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x), 0))
1145 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x), 0));
1146 else
1147 hash += cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode);
1148 return hash ? hash : (unsigned int) ENTRY_VALUE;
1150 case CONST_INT:
1151 hash += ((unsigned) CONST_INT << 7) + UINTVAL (x);
1152 return hash ? hash : (unsigned int) CONST_INT;
1154 case CONST_WIDE_INT:
1155 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
1156 hash += CONST_WIDE_INT_ELT (x, i);
1157 return hash;
1159 case CONST_DOUBLE:
1160 /* This is like the general case, except that it only counts
1161 the integers representing the constant. */
1162 hash += (unsigned) code + (unsigned) GET_MODE (x);
1163 if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (x) == VOIDmode)
1164 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1165 + (unsigned) CONST_DOUBLE_HIGH (x));
1166 else
1167 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
1168 return hash ? hash : (unsigned int) CONST_DOUBLE;
1170 case CONST_FIXED:
1171 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1172 hash += fixed_hash (CONST_FIXED_VALUE (x));
1173 return hash ? hash : (unsigned int) CONST_FIXED;
1175 case CONST_VECTOR:
1177 int units;
1178 rtx elt;
1180 units = CONST_VECTOR_NUNITS (x);
1182 for (i = 0; i < units; ++i)
1184 elt = CONST_VECTOR_ELT (x, i);
1185 hash += cselib_hash_rtx (elt, 0, memmode);
1188 return hash;
1191 /* Assume there is only one rtx object for any given label. */
1192 case LABEL_REF:
1193 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1194 differences and differences between each stage's debugging dumps. */
1195 hash += (((unsigned int) LABEL_REF << 7)
1196 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x)));
1197 return hash ? hash : (unsigned int) LABEL_REF;
1199 case SYMBOL_REF:
1201 /* Don't hash on the symbol's address to avoid bootstrap differences.
1202 Different hash values may cause expressions to be recorded in
1203 different orders and thus different registers to be used in the
1204 final assembler. This also avoids differences in the dump files
1205 between various stages. */
1206 unsigned int h = 0;
1207 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1209 while (*p)
1210 h += (h << 7) + *p++; /* ??? revisit */
1212 hash += ((unsigned int) SYMBOL_REF << 7) + h;
1213 return hash ? hash : (unsigned int) SYMBOL_REF;
1216 case PRE_DEC:
1217 case PRE_INC:
1218 /* We can't compute these without knowing the MEM mode. */
1219 gcc_assert (memmode != VOIDmode);
1220 i = GET_MODE_SIZE (memmode);
1221 if (code == PRE_DEC)
1222 i = -i;
1223 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1224 like (mem:MEMMODE (plus (reg) (const_int I))). */
1225 hash += (unsigned) PLUS - (unsigned)code
1226 + cselib_hash_rtx (XEXP (x, 0), create, memmode)
1227 + cselib_hash_rtx (GEN_INT (i), create, memmode);
1228 return hash ? hash : 1 + (unsigned) PLUS;
1230 case PRE_MODIFY:
1231 gcc_assert (memmode != VOIDmode);
1232 return cselib_hash_rtx (XEXP (x, 1), create, memmode);
1234 case POST_DEC:
1235 case POST_INC:
1236 case POST_MODIFY:
1237 gcc_assert (memmode != VOIDmode);
1238 return cselib_hash_rtx (XEXP (x, 0), create, memmode);
1240 case PC:
1241 case CC0:
1242 case CALL:
1243 case UNSPEC_VOLATILE:
1244 return 0;
1246 case ASM_OPERANDS:
1247 if (MEM_VOLATILE_P (x))
1248 return 0;
1250 break;
1252 default:
1253 break;
1256 i = GET_RTX_LENGTH (code) - 1;
1257 fmt = GET_RTX_FORMAT (code);
1258 for (; i >= 0; i--)
1260 switch (fmt[i])
1262 case 'e':
1264 rtx tem = XEXP (x, i);
1265 unsigned int tem_hash = cselib_hash_rtx (tem, create, memmode);
1267 if (tem_hash == 0)
1268 return 0;
1270 hash += tem_hash;
1272 break;
1273 case 'E':
1274 for (j = 0; j < XVECLEN (x, i); j++)
1276 unsigned int tem_hash
1277 = cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
1279 if (tem_hash == 0)
1280 return 0;
1282 hash += tem_hash;
1284 break;
1286 case 's':
1288 const unsigned char *p = (const unsigned char *) XSTR (x, i);
1290 if (p)
1291 while (*p)
1292 hash += *p++;
1293 break;
1296 case 'i':
1297 hash += XINT (x, i);
1298 break;
1300 case '0':
1301 case 't':
1302 /* unused */
1303 break;
1305 default:
1306 gcc_unreachable ();
1310 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
1313 /* Create a new value structure for VALUE and initialize it. The mode of the
1314 value is MODE. */
1316 static inline cselib_val *
1317 new_cselib_val (unsigned int hash, machine_mode mode, rtx x)
1319 cselib_val *e = new cselib_val;
1321 gcc_assert (hash);
1322 gcc_assert (next_uid);
1324 e->hash = hash;
1325 e->uid = next_uid++;
1326 /* We use an alloc pool to allocate this RTL construct because it
1327 accounts for about 8% of the overall memory usage. We know
1328 precisely when we can have VALUE RTXen (when cselib is active)
1329 so we don't need to put them in garbage collected memory.
1330 ??? Why should a VALUE be an RTX in the first place? */
1331 e->val_rtx = value_pool.allocate ();
1332 memset (e->val_rtx, 0, RTX_HDR_SIZE);
1333 PUT_CODE (e->val_rtx, VALUE);
1334 PUT_MODE (e->val_rtx, mode);
1335 CSELIB_VAL_PTR (e->val_rtx) = e;
1336 e->addr_list = 0;
1337 e->locs = 0;
1338 e->next_containing_mem = 0;
1340 if (dump_file && (dump_flags & TDF_CSELIB))
1342 fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
1343 if (flag_dump_noaddr || flag_dump_unnumbered)
1344 fputs ("# ", dump_file);
1345 else
1346 fprintf (dump_file, "%p ", (void*)e);
1347 print_rtl_single (dump_file, x);
1348 fputc ('\n', dump_file);
1351 return e;
1354 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1355 contains the data at this address. X is a MEM that represents the
1356 value. Update the two value structures to represent this situation. */
1358 static void
1359 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
1361 struct elt_loc_list *l;
1363 addr_elt = canonical_cselib_val (addr_elt);
1364 mem_elt = canonical_cselib_val (mem_elt);
1366 /* Avoid duplicates. */
1367 for (l = mem_elt->locs; l; l = l->next)
1368 if (MEM_P (l->loc)
1369 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
1371 promote_debug_loc (l);
1372 return;
1375 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
1376 new_elt_loc_list (mem_elt,
1377 replace_equiv_address_nv (x, addr_elt->val_rtx));
1378 if (mem_elt->next_containing_mem == NULL)
1380 mem_elt->next_containing_mem = first_containing_mem;
1381 first_containing_mem = mem_elt;
1385 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1386 If CREATE, make a new one if we haven't seen it before. */
1388 static cselib_val *
1389 cselib_lookup_mem (rtx x, int create)
1391 machine_mode mode = GET_MODE (x);
1392 machine_mode addr_mode;
1393 cselib_val **slot;
1394 cselib_val *addr;
1395 cselib_val *mem_elt;
1396 struct elt_list *l;
1398 if (MEM_VOLATILE_P (x) || mode == BLKmode
1399 || !cselib_record_memory
1400 || (FLOAT_MODE_P (mode) && flag_float_store))
1401 return 0;
1403 addr_mode = GET_MODE (XEXP (x, 0));
1404 if (addr_mode == VOIDmode)
1405 addr_mode = Pmode;
1407 /* Look up the value for the address. */
1408 addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
1409 if (! addr)
1410 return 0;
1412 addr = canonical_cselib_val (addr);
1413 /* Find a value that describes a value of our mode at that address. */
1414 for (l = addr->addr_list; l; l = l->next)
1415 if (GET_MODE (l->elt->val_rtx) == mode)
1417 promote_debug_loc (l->elt->locs);
1418 return l->elt;
1421 if (! create)
1422 return 0;
1424 mem_elt = new_cselib_val (next_uid, mode, x);
1425 add_mem_for_addr (addr, mem_elt, x);
1426 slot = cselib_find_slot (mode, x, mem_elt->hash, INSERT, VOIDmode);
1427 *slot = mem_elt;
1428 return mem_elt;
1431 /* Search through the possible substitutions in P. We prefer a non reg
1432 substitution because this allows us to expand the tree further. If
1433 we find, just a reg, take the lowest regno. There may be several
1434 non-reg results, we just take the first one because they will all
1435 expand to the same place. */
1437 static rtx
1438 expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1439 int max_depth)
1441 rtx reg_result = NULL;
1442 unsigned int regno = UINT_MAX;
1443 struct elt_loc_list *p_in = p;
1445 for (; p; p = p->next)
1447 /* Return these right away to avoid returning stack pointer based
1448 expressions for frame pointer and vice versa, which is something
1449 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1450 for more details. */
1451 if (REG_P (p->loc)
1452 && (REGNO (p->loc) == STACK_POINTER_REGNUM
1453 || REGNO (p->loc) == FRAME_POINTER_REGNUM
1454 || REGNO (p->loc) == HARD_FRAME_POINTER_REGNUM
1455 || REGNO (p->loc) == cfa_base_preserved_regno))
1456 return p->loc;
1457 /* Avoid infinite recursion trying to expand a reg into a
1458 the same reg. */
1459 if ((REG_P (p->loc))
1460 && (REGNO (p->loc) < regno)
1461 && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
1463 reg_result = p->loc;
1464 regno = REGNO (p->loc);
1466 /* Avoid infinite recursion and do not try to expand the
1467 value. */
1468 else if (GET_CODE (p->loc) == VALUE
1469 && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1470 continue;
1471 else if (!REG_P (p->loc))
1473 rtx result, note;
1474 if (dump_file && (dump_flags & TDF_CSELIB))
1476 print_inline_rtx (dump_file, p->loc, 0);
1477 fprintf (dump_file, "\n");
1479 if (GET_CODE (p->loc) == LO_SUM
1480 && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1481 && p->setting_insn
1482 && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1483 && XEXP (note, 0) == XEXP (p->loc, 1))
1484 return XEXP (p->loc, 1);
1485 result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
1486 if (result)
1487 return result;
1492 if (regno != UINT_MAX)
1494 rtx result;
1495 if (dump_file && (dump_flags & TDF_CSELIB))
1496 fprintf (dump_file, "r%d\n", regno);
1498 result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
1499 if (result)
1500 return result;
1503 if (dump_file && (dump_flags & TDF_CSELIB))
1505 if (reg_result)
1507 print_inline_rtx (dump_file, reg_result, 0);
1508 fprintf (dump_file, "\n");
1510 else
1511 fprintf (dump_file, "NULL\n");
1513 return reg_result;
1517 /* Forward substitute and expand an expression out to its roots.
1518 This is the opposite of common subexpression. Because local value
1519 numbering is such a weak optimization, the expanded expression is
1520 pretty much unique (not from a pointer equals point of view but
1521 from a tree shape point of view.
1523 This function returns NULL if the expansion fails. The expansion
1524 will fail if there is no value number for one of the operands or if
1525 one of the operands has been overwritten between the current insn
1526 and the beginning of the basic block. For instance x has no
1527 expansion in:
1529 r1 <- r1 + 3
1530 x <- r1 + 8
1532 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1533 It is clear on return. */
1536 cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
1538 struct expand_value_data evd;
1540 evd.regs_active = regs_active;
1541 evd.callback = NULL;
1542 evd.callback_arg = NULL;
1543 evd.dummy = false;
1545 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1548 /* Same as cselib_expand_value_rtx, but using a callback to try to
1549 resolve some expressions. The CB function should return ORIG if it
1550 can't or does not want to deal with a certain RTX. Any other
1551 return value, including NULL, will be used as the expansion for
1552 VALUE, without any further changes. */
1555 cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1556 cselib_expand_callback cb, void *data)
1558 struct expand_value_data evd;
1560 evd.regs_active = regs_active;
1561 evd.callback = cb;
1562 evd.callback_arg = data;
1563 evd.dummy = false;
1565 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1568 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1569 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1570 would return NULL or non-NULL, without allocating new rtx. */
1572 bool
1573 cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1574 cselib_expand_callback cb, void *data)
1576 struct expand_value_data evd;
1578 evd.regs_active = regs_active;
1579 evd.callback = cb;
1580 evd.callback_arg = data;
1581 evd.dummy = true;
1583 return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1586 /* Internal implementation of cselib_expand_value_rtx and
1587 cselib_expand_value_rtx_cb. */
1589 static rtx
1590 cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1591 int max_depth)
1593 rtx copy, scopy;
1594 int i, j;
1595 RTX_CODE code;
1596 const char *format_ptr;
1597 machine_mode mode;
1599 code = GET_CODE (orig);
1601 /* For the context of dse, if we end up expand into a huge tree, we
1602 will not have a useful address, so we might as well just give up
1603 quickly. */
1604 if (max_depth <= 0)
1605 return NULL;
1607 switch (code)
1609 case REG:
1611 struct elt_list *l = REG_VALUES (REGNO (orig));
1613 if (l && l->elt == NULL)
1614 l = l->next;
1615 for (; l; l = l->next)
1616 if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1618 rtx result;
1619 unsigned regno = REGNO (orig);
1621 /* The only thing that we are not willing to do (this
1622 is requirement of dse and if others potential uses
1623 need this function we should add a parm to control
1624 it) is that we will not substitute the
1625 STACK_POINTER_REGNUM, FRAME_POINTER or the
1626 HARD_FRAME_POINTER.
1628 These expansions confuses the code that notices that
1629 stores into the frame go dead at the end of the
1630 function and that the frame is not effected by calls
1631 to subroutines. If you allow the
1632 STACK_POINTER_REGNUM substitution, then dse will
1633 think that parameter pushing also goes dead which is
1634 wrong. If you allow the FRAME_POINTER or the
1635 HARD_FRAME_POINTER then you lose the opportunity to
1636 make the frame assumptions. */
1637 if (regno == STACK_POINTER_REGNUM
1638 || regno == FRAME_POINTER_REGNUM
1639 || regno == HARD_FRAME_POINTER_REGNUM
1640 || regno == cfa_base_preserved_regno)
1641 return orig;
1643 bitmap_set_bit (evd->regs_active, regno);
1645 if (dump_file && (dump_flags & TDF_CSELIB))
1646 fprintf (dump_file, "expanding: r%d into: ", regno);
1648 result = expand_loc (l->elt->locs, evd, max_depth);
1649 bitmap_clear_bit (evd->regs_active, regno);
1651 if (result)
1652 return result;
1653 else
1654 return orig;
1658 CASE_CONST_ANY:
1659 case SYMBOL_REF:
1660 case CODE_LABEL:
1661 case PC:
1662 case CC0:
1663 case SCRATCH:
1664 /* SCRATCH must be shared because they represent distinct values. */
1665 return orig;
1666 case CLOBBER:
1667 if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1668 return orig;
1669 break;
1671 case CONST:
1672 if (shared_const_p (orig))
1673 return orig;
1674 break;
1676 case SUBREG:
1678 rtx subreg;
1680 if (evd->callback)
1682 subreg = evd->callback (orig, evd->regs_active, max_depth,
1683 evd->callback_arg);
1684 if (subreg != orig)
1685 return subreg;
1688 subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1689 max_depth - 1);
1690 if (!subreg)
1691 return NULL;
1692 scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1693 GET_MODE (SUBREG_REG (orig)),
1694 SUBREG_BYTE (orig));
1695 if (scopy == NULL
1696 || (GET_CODE (scopy) == SUBREG
1697 && !REG_P (SUBREG_REG (scopy))
1698 && !MEM_P (SUBREG_REG (scopy))))
1699 return NULL;
1701 return scopy;
1704 case VALUE:
1706 rtx result;
1708 if (dump_file && (dump_flags & TDF_CSELIB))
1710 fputs ("\nexpanding ", dump_file);
1711 print_rtl_single (dump_file, orig);
1712 fputs (" into...", dump_file);
1715 if (evd->callback)
1717 result = evd->callback (orig, evd->regs_active, max_depth,
1718 evd->callback_arg);
1720 if (result != orig)
1721 return result;
1724 result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
1725 return result;
1728 case DEBUG_EXPR:
1729 if (evd->callback)
1730 return evd->callback (orig, evd->regs_active, max_depth,
1731 evd->callback_arg);
1732 return orig;
1734 default:
1735 break;
1738 /* Copy the various flags, fields, and other information. We assume
1739 that all fields need copying, and then clear the fields that should
1740 not be copied. That is the sensible default behavior, and forces
1741 us to explicitly document why we are *not* copying a flag. */
1742 if (evd->dummy)
1743 copy = NULL;
1744 else
1745 copy = shallow_copy_rtx (orig);
1747 format_ptr = GET_RTX_FORMAT (code);
1749 for (i = 0; i < GET_RTX_LENGTH (code); i++)
1750 switch (*format_ptr++)
1752 case 'e':
1753 if (XEXP (orig, i) != NULL)
1755 rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1756 max_depth - 1);
1757 if (!result)
1758 return NULL;
1759 if (copy)
1760 XEXP (copy, i) = result;
1762 break;
1764 case 'E':
1765 case 'V':
1766 if (XVEC (orig, i) != NULL)
1768 if (copy)
1769 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1770 for (j = 0; j < XVECLEN (orig, i); j++)
1772 rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1773 evd, max_depth - 1);
1774 if (!result)
1775 return NULL;
1776 if (copy)
1777 XVECEXP (copy, i, j) = result;
1780 break;
1782 case 't':
1783 case 'w':
1784 case 'i':
1785 case 's':
1786 case 'S':
1787 case 'T':
1788 case 'u':
1789 case 'B':
1790 case '0':
1791 /* These are left unchanged. */
1792 break;
1794 default:
1795 gcc_unreachable ();
1798 if (evd->dummy)
1799 return orig;
1801 mode = GET_MODE (copy);
1802 /* If an operand has been simplified into CONST_INT, which doesn't
1803 have a mode and the mode isn't derivable from whole rtx's mode,
1804 try simplify_*_operation first with mode from original's operand
1805 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1806 scopy = copy;
1807 switch (GET_RTX_CLASS (code))
1809 case RTX_UNARY:
1810 if (CONST_INT_P (XEXP (copy, 0))
1811 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1813 scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1814 GET_MODE (XEXP (orig, 0)));
1815 if (scopy)
1816 return scopy;
1818 break;
1819 case RTX_COMM_ARITH:
1820 case RTX_BIN_ARITH:
1821 /* These expressions can derive operand modes from the whole rtx's mode. */
1822 break;
1823 case RTX_TERNARY:
1824 case RTX_BITFIELD_OPS:
1825 if (CONST_INT_P (XEXP (copy, 0))
1826 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1828 scopy = simplify_ternary_operation (code, mode,
1829 GET_MODE (XEXP (orig, 0)),
1830 XEXP (copy, 0), XEXP (copy, 1),
1831 XEXP (copy, 2));
1832 if (scopy)
1833 return scopy;
1835 break;
1836 case RTX_COMPARE:
1837 case RTX_COMM_COMPARE:
1838 if (CONST_INT_P (XEXP (copy, 0))
1839 && GET_MODE (XEXP (copy, 1)) == VOIDmode
1840 && (GET_MODE (XEXP (orig, 0)) != VOIDmode
1841 || GET_MODE (XEXP (orig, 1)) != VOIDmode))
1843 scopy = simplify_relational_operation (code, mode,
1844 (GET_MODE (XEXP (orig, 0))
1845 != VOIDmode)
1846 ? GET_MODE (XEXP (orig, 0))
1847 : GET_MODE (XEXP (orig, 1)),
1848 XEXP (copy, 0),
1849 XEXP (copy, 1));
1850 if (scopy)
1851 return scopy;
1853 break;
1854 default:
1855 break;
1857 scopy = simplify_rtx (copy);
1858 if (scopy)
1859 return scopy;
1860 return copy;
1863 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1864 with VALUE expressions. This way, it becomes independent of changes
1865 to registers and memory.
1866 X isn't actually modified; if modifications are needed, new rtl is
1867 allocated. However, the return value can share rtl with X.
1868 If X is within a MEM, MEMMODE must be the mode of the MEM. */
1871 cselib_subst_to_values (rtx x, machine_mode memmode)
1873 enum rtx_code code = GET_CODE (x);
1874 const char *fmt = GET_RTX_FORMAT (code);
1875 cselib_val *e;
1876 struct elt_list *l;
1877 rtx copy = x;
1878 int i;
1880 switch (code)
1882 case REG:
1883 l = REG_VALUES (REGNO (x));
1884 if (l && l->elt == NULL)
1885 l = l->next;
1886 for (; l; l = l->next)
1887 if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
1888 return l->elt->val_rtx;
1890 gcc_unreachable ();
1892 case MEM:
1893 e = cselib_lookup_mem (x, 0);
1894 /* This used to happen for autoincrements, but we deal with them
1895 properly now. Remove the if stmt for the next release. */
1896 if (! e)
1898 /* Assign a value that doesn't match any other. */
1899 e = new_cselib_val (next_uid, GET_MODE (x), x);
1901 return e->val_rtx;
1903 case ENTRY_VALUE:
1904 e = cselib_lookup (x, GET_MODE (x), 0, memmode);
1905 if (! e)
1906 break;
1907 return e->val_rtx;
1909 CASE_CONST_ANY:
1910 return x;
1912 case PRE_DEC:
1913 case PRE_INC:
1914 gcc_assert (memmode != VOIDmode);
1915 i = GET_MODE_SIZE (memmode);
1916 if (code == PRE_DEC)
1917 i = -i;
1918 return cselib_subst_to_values (plus_constant (GET_MODE (x),
1919 XEXP (x, 0), i),
1920 memmode);
1922 case PRE_MODIFY:
1923 gcc_assert (memmode != VOIDmode);
1924 return cselib_subst_to_values (XEXP (x, 1), memmode);
1926 case POST_DEC:
1927 case POST_INC:
1928 case POST_MODIFY:
1929 gcc_assert (memmode != VOIDmode);
1930 return cselib_subst_to_values (XEXP (x, 0), memmode);
1932 default:
1933 break;
1936 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1938 if (fmt[i] == 'e')
1940 rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
1942 if (t != XEXP (x, i))
1944 if (x == copy)
1945 copy = shallow_copy_rtx (x);
1946 XEXP (copy, i) = t;
1949 else if (fmt[i] == 'E')
1951 int j;
1953 for (j = 0; j < XVECLEN (x, i); j++)
1955 rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
1957 if (t != XVECEXP (x, i, j))
1959 if (XVEC (x, i) == XVEC (copy, i))
1961 if (x == copy)
1962 copy = shallow_copy_rtx (x);
1963 XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
1965 XVECEXP (copy, i, j) = t;
1971 return copy;
1974 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
1977 cselib_subst_to_values_from_insn (rtx x, machine_mode memmode, rtx_insn *insn)
1979 rtx ret;
1980 gcc_assert (!cselib_current_insn);
1981 cselib_current_insn = insn;
1982 ret = cselib_subst_to_values (x, memmode);
1983 cselib_current_insn = NULL;
1984 return ret;
1987 /* Look up the rtl expression X in our tables and return the value it
1988 has. If CREATE is zero, we return NULL if we don't know the value.
1989 Otherwise, we create a new one if possible, using mode MODE if X
1990 doesn't have a mode (i.e. because it's a constant). When X is part
1991 of an address, MEMMODE should be the mode of the enclosing MEM if
1992 we're tracking autoinc expressions. */
1994 static cselib_val *
1995 cselib_lookup_1 (rtx x, machine_mode mode,
1996 int create, machine_mode memmode)
1998 cselib_val **slot;
1999 cselib_val *e;
2000 unsigned int hashval;
2002 if (GET_MODE (x) != VOIDmode)
2003 mode = GET_MODE (x);
2005 if (GET_CODE (x) == VALUE)
2006 return CSELIB_VAL_PTR (x);
2008 if (REG_P (x))
2010 struct elt_list *l;
2011 unsigned int i = REGNO (x);
2013 l = REG_VALUES (i);
2014 if (l && l->elt == NULL)
2015 l = l->next;
2016 for (; l; l = l->next)
2017 if (mode == GET_MODE (l->elt->val_rtx))
2019 promote_debug_loc (l->elt->locs);
2020 return l->elt;
2023 if (! create)
2024 return 0;
2026 if (i < FIRST_PSEUDO_REGISTER)
2028 unsigned int n = hard_regno_nregs[i][mode];
2030 if (n > max_value_regs)
2031 max_value_regs = n;
2034 e = new_cselib_val (next_uid, GET_MODE (x), x);
2035 new_elt_loc_list (e, x);
2036 if (REG_VALUES (i) == 0)
2038 /* Maintain the invariant that the first entry of
2039 REG_VALUES, if present, must be the value used to set the
2040 register, or NULL. */
2041 used_regs[n_used_regs++] = i;
2042 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
2044 else if (cselib_preserve_constants
2045 && GET_MODE_CLASS (mode) == MODE_INT)
2047 /* During var-tracking, try harder to find equivalences
2048 for SUBREGs. If a setter sets say a DImode register
2049 and user uses that register only in SImode, add a lowpart
2050 subreg location. */
2051 struct elt_list *lwider = NULL;
2052 l = REG_VALUES (i);
2053 if (l && l->elt == NULL)
2054 l = l->next;
2055 for (; l; l = l->next)
2056 if (GET_MODE_CLASS (GET_MODE (l->elt->val_rtx)) == MODE_INT
2057 && GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2058 > GET_MODE_SIZE (mode)
2059 && (lwider == NULL
2060 || GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2061 < GET_MODE_SIZE (GET_MODE (lwider->elt->val_rtx))))
2063 struct elt_loc_list *el;
2064 if (i < FIRST_PSEUDO_REGISTER
2065 && hard_regno_nregs[i][GET_MODE (l->elt->val_rtx)] != 1)
2066 continue;
2067 for (el = l->elt->locs; el; el = el->next)
2068 if (!REG_P (el->loc))
2069 break;
2070 if (el)
2071 lwider = l;
2073 if (lwider)
2075 rtx sub = lowpart_subreg (mode, lwider->elt->val_rtx,
2076 GET_MODE (lwider->elt->val_rtx));
2077 if (sub)
2078 new_elt_loc_list (e, sub);
2081 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
2082 slot = cselib_find_slot (mode, x, e->hash, INSERT, memmode);
2083 *slot = e;
2084 return e;
2087 if (MEM_P (x))
2088 return cselib_lookup_mem (x, create);
2090 hashval = cselib_hash_rtx (x, create, memmode);
2091 /* Can't even create if hashing is not possible. */
2092 if (! hashval)
2093 return 0;
2095 slot = cselib_find_slot (mode, x, hashval,
2096 create ? INSERT : NO_INSERT, memmode);
2097 if (slot == 0)
2098 return 0;
2100 e = (cselib_val *) *slot;
2101 if (e)
2102 return e;
2104 e = new_cselib_val (hashval, mode, x);
2106 /* We have to fill the slot before calling cselib_subst_to_values:
2107 the hash table is inconsistent until we do so, and
2108 cselib_subst_to_values will need to do lookups. */
2109 *slot = e;
2110 new_elt_loc_list (e, cselib_subst_to_values (x, memmode));
2111 return e;
2114 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2116 cselib_val *
2117 cselib_lookup_from_insn (rtx x, machine_mode mode,
2118 int create, machine_mode memmode, rtx_insn *insn)
2120 cselib_val *ret;
2122 gcc_assert (!cselib_current_insn);
2123 cselib_current_insn = insn;
2125 ret = cselib_lookup (x, mode, create, memmode);
2127 cselib_current_insn = NULL;
2129 return ret;
2132 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2133 maintains invariants related with debug insns. */
2135 cselib_val *
2136 cselib_lookup (rtx x, machine_mode mode,
2137 int create, machine_mode memmode)
2139 cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
2141 /* ??? Should we return NULL if we're not to create an entry, the
2142 found loc is a debug loc and cselib_current_insn is not DEBUG?
2143 If so, we should also avoid converting val to non-DEBUG; probably
2144 easiest setting cselib_current_insn to NULL before the call
2145 above. */
2147 if (dump_file && (dump_flags & TDF_CSELIB))
2149 fputs ("cselib lookup ", dump_file);
2150 print_inline_rtx (dump_file, x, 2);
2151 fprintf (dump_file, " => %u:%u\n",
2152 ret ? ret->uid : 0,
2153 ret ? ret->hash : 0);
2156 return ret;
2159 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2160 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2161 is used to determine how many hard registers are being changed. If MODE
2162 is VOIDmode, then only REGNO is being changed; this is used when
2163 invalidating call clobbered registers across a call. */
2165 static void
2166 cselib_invalidate_regno (unsigned int regno, machine_mode mode)
2168 unsigned int endregno;
2169 unsigned int i;
2171 /* If we see pseudos after reload, something is _wrong_. */
2172 gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
2173 || reg_renumber[regno] < 0);
2175 /* Determine the range of registers that must be invalidated. For
2176 pseudos, only REGNO is affected. For hard regs, we must take MODE
2177 into account, and we must also invalidate lower register numbers
2178 if they contain values that overlap REGNO. */
2179 if (regno < FIRST_PSEUDO_REGISTER)
2181 gcc_assert (mode != VOIDmode);
2183 if (regno < max_value_regs)
2184 i = 0;
2185 else
2186 i = regno - max_value_regs;
2188 endregno = end_hard_regno (mode, regno);
2190 else
2192 i = regno;
2193 endregno = regno + 1;
2196 for (; i < endregno; i++)
2198 struct elt_list **l = &REG_VALUES (i);
2200 /* Go through all known values for this reg; if it overlaps the range
2201 we're invalidating, remove the value. */
2202 while (*l)
2204 cselib_val *v = (*l)->elt;
2205 bool had_locs;
2206 rtx_insn *setting_insn;
2207 struct elt_loc_list **p;
2208 unsigned int this_last = i;
2210 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
2211 this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
2213 if (this_last < regno || v == NULL
2214 || (v == cfa_base_preserved_val
2215 && i == cfa_base_preserved_regno))
2217 l = &(*l)->next;
2218 continue;
2221 /* We have an overlap. */
2222 if (*l == REG_VALUES (i))
2224 /* Maintain the invariant that the first entry of
2225 REG_VALUES, if present, must be the value used to set
2226 the register, or NULL. This is also nice because
2227 then we won't push the same regno onto user_regs
2228 multiple times. */
2229 (*l)->elt = NULL;
2230 l = &(*l)->next;
2232 else
2233 unchain_one_elt_list (l);
2235 v = canonical_cselib_val (v);
2237 had_locs = v->locs != NULL;
2238 setting_insn = v->locs ? v->locs->setting_insn : NULL;
2240 /* Now, we clear the mapping from value to reg. It must exist, so
2241 this code will crash intentionally if it doesn't. */
2242 for (p = &v->locs; ; p = &(*p)->next)
2244 rtx x = (*p)->loc;
2246 if (REG_P (x) && REGNO (x) == i)
2248 unchain_one_elt_loc_list (p);
2249 break;
2253 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2255 if (setting_insn && DEBUG_INSN_P (setting_insn))
2256 n_useless_debug_values++;
2257 else
2258 n_useless_values++;
2264 /* Invalidate any locations in the table which are changed because of a
2265 store to MEM_RTX. If this is called because of a non-const call
2266 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2268 static void
2269 cselib_invalidate_mem (rtx mem_rtx)
2271 cselib_val **vp, *v, *next;
2272 int num_mems = 0;
2273 rtx mem_addr;
2275 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2276 mem_rtx = canon_rtx (mem_rtx);
2278 vp = &first_containing_mem;
2279 for (v = *vp; v != &dummy_val; v = next)
2281 bool has_mem = false;
2282 struct elt_loc_list **p = &v->locs;
2283 bool had_locs = v->locs != NULL;
2284 rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
2286 while (*p)
2288 rtx x = (*p)->loc;
2289 cselib_val *addr;
2290 struct elt_list **mem_chain;
2292 /* MEMs may occur in locations only at the top level; below
2293 that every MEM or REG is substituted by its VALUE. */
2294 if (!MEM_P (x))
2296 p = &(*p)->next;
2297 continue;
2299 if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
2300 && ! canon_anti_dependence (x, false, mem_rtx,
2301 GET_MODE (mem_rtx), mem_addr))
2303 has_mem = true;
2304 num_mems++;
2305 p = &(*p)->next;
2306 continue;
2309 /* This one overlaps. */
2310 /* We must have a mapping from this MEM's address to the
2311 value (E). Remove that, too. */
2312 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
2313 addr = canonical_cselib_val (addr);
2314 gcc_checking_assert (v == canonical_cselib_val (v));
2315 mem_chain = &addr->addr_list;
2316 for (;;)
2318 cselib_val *canon = canonical_cselib_val ((*mem_chain)->elt);
2320 if (canon == v)
2322 unchain_one_elt_list (mem_chain);
2323 break;
2326 /* Record canonicalized elt. */
2327 (*mem_chain)->elt = canon;
2329 mem_chain = &(*mem_chain)->next;
2332 unchain_one_elt_loc_list (p);
2335 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2337 if (setting_insn && DEBUG_INSN_P (setting_insn))
2338 n_useless_debug_values++;
2339 else
2340 n_useless_values++;
2343 next = v->next_containing_mem;
2344 if (has_mem)
2346 *vp = v;
2347 vp = &(*vp)->next_containing_mem;
2349 else
2350 v->next_containing_mem = NULL;
2352 *vp = &dummy_val;
2355 /* Invalidate DEST, which is being assigned to or clobbered. */
2357 void
2358 cselib_invalidate_rtx (rtx dest)
2360 while (GET_CODE (dest) == SUBREG
2361 || GET_CODE (dest) == ZERO_EXTRACT
2362 || GET_CODE (dest) == STRICT_LOW_PART)
2363 dest = XEXP (dest, 0);
2365 if (REG_P (dest))
2366 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
2367 else if (MEM_P (dest))
2368 cselib_invalidate_mem (dest);
2371 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2373 static void
2374 cselib_invalidate_rtx_note_stores (rtx dest, const_rtx ignore ATTRIBUTE_UNUSED,
2375 void *data ATTRIBUTE_UNUSED)
2377 cselib_invalidate_rtx (dest);
2380 /* Record the result of a SET instruction. DEST is being set; the source
2381 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2382 describes its address. */
2384 static void
2385 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
2387 if (src_elt == 0 || side_effects_p (dest))
2388 return;
2390 if (REG_P (dest))
2392 unsigned int dreg = REGNO (dest);
2393 if (dreg < FIRST_PSEUDO_REGISTER)
2395 unsigned int n = REG_NREGS (dest);
2397 if (n > max_value_regs)
2398 max_value_regs = n;
2401 if (REG_VALUES (dreg) == 0)
2403 used_regs[n_used_regs++] = dreg;
2404 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2406 else
2408 /* The register should have been invalidated. */
2409 gcc_assert (REG_VALUES (dreg)->elt == 0);
2410 REG_VALUES (dreg)->elt = src_elt;
2413 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2414 n_useless_values--;
2415 new_elt_loc_list (src_elt, dest);
2417 else if (MEM_P (dest) && dest_addr_elt != 0
2418 && cselib_record_memory)
2420 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2421 n_useless_values--;
2422 add_mem_for_addr (dest_addr_elt, src_elt, dest);
2426 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2428 void
2429 cselib_add_permanent_equiv (cselib_val *elt, rtx x, rtx_insn *insn)
2431 cselib_val *nelt;
2432 rtx_insn *save_cselib_current_insn = cselib_current_insn;
2434 gcc_checking_assert (elt);
2435 gcc_checking_assert (PRESERVED_VALUE_P (elt->val_rtx));
2436 gcc_checking_assert (!side_effects_p (x));
2438 cselib_current_insn = insn;
2440 nelt = cselib_lookup (x, GET_MODE (elt->val_rtx), 1, VOIDmode);
2442 if (nelt != elt)
2444 cselib_any_perm_equivs = true;
2446 if (!PRESERVED_VALUE_P (nelt->val_rtx))
2447 cselib_preserve_value (nelt);
2449 new_elt_loc_list (nelt, elt->val_rtx);
2452 cselib_current_insn = save_cselib_current_insn;
2455 /* Return TRUE if any permanent equivalences have been recorded since
2456 the table was last initialized. */
2457 bool
2458 cselib_have_permanent_equivalences (void)
2460 return cselib_any_perm_equivs;
2463 /* There is no good way to determine how many elements there can be
2464 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2465 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2467 struct cselib_record_autoinc_data
2469 struct cselib_set *sets;
2470 int n_sets;
2473 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2474 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2476 static int
2477 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
2478 rtx dest, rtx src, rtx srcoff, void *arg)
2480 struct cselib_record_autoinc_data *data;
2481 data = (struct cselib_record_autoinc_data *)arg;
2483 data->sets[data->n_sets].dest = dest;
2485 if (srcoff)
2486 data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
2487 else
2488 data->sets[data->n_sets].src = src;
2490 data->n_sets++;
2492 return 0;
2495 /* Record the effects of any sets and autoincs in INSN. */
2496 static void
2497 cselib_record_sets (rtx_insn *insn)
2499 int n_sets = 0;
2500 int i;
2501 struct cselib_set sets[MAX_SETS];
2502 rtx body = PATTERN (insn);
2503 rtx cond = 0;
2504 int n_sets_before_autoinc;
2505 struct cselib_record_autoinc_data data;
2507 body = PATTERN (insn);
2508 if (GET_CODE (body) == COND_EXEC)
2510 cond = COND_EXEC_TEST (body);
2511 body = COND_EXEC_CODE (body);
2514 /* Find all sets. */
2515 if (GET_CODE (body) == SET)
2517 sets[0].src = SET_SRC (body);
2518 sets[0].dest = SET_DEST (body);
2519 n_sets = 1;
2521 else if (GET_CODE (body) == PARALLEL)
2523 /* Look through the PARALLEL and record the values being
2524 set, if possible. Also handle any CLOBBERs. */
2525 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2527 rtx x = XVECEXP (body, 0, i);
2529 if (GET_CODE (x) == SET)
2531 sets[n_sets].src = SET_SRC (x);
2532 sets[n_sets].dest = SET_DEST (x);
2533 n_sets++;
2538 if (n_sets == 1
2539 && MEM_P (sets[0].src)
2540 && !cselib_record_memory
2541 && MEM_READONLY_P (sets[0].src))
2543 rtx note = find_reg_equal_equiv_note (insn);
2545 if (note && CONSTANT_P (XEXP (note, 0)))
2546 sets[0].src = XEXP (note, 0);
2549 data.sets = sets;
2550 data.n_sets = n_sets_before_autoinc = n_sets;
2551 for_each_inc_dec (PATTERN (insn), cselib_record_autoinc_cb, &data);
2552 n_sets = data.n_sets;
2554 /* Look up the values that are read. Do this before invalidating the
2555 locations that are written. */
2556 for (i = 0; i < n_sets; i++)
2558 rtx dest = sets[i].dest;
2560 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2561 the low part after invalidating any knowledge about larger modes. */
2562 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2563 sets[i].dest = dest = XEXP (dest, 0);
2565 /* We don't know how to record anything but REG or MEM. */
2566 if (REG_P (dest)
2567 || (MEM_P (dest) && cselib_record_memory))
2569 rtx src = sets[i].src;
2570 if (cond)
2571 src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
2572 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
2573 if (MEM_P (dest))
2575 machine_mode address_mode = get_address_mode (dest);
2577 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
2578 address_mode, 1,
2579 GET_MODE (dest));
2581 else
2582 sets[i].dest_addr_elt = 0;
2586 if (cselib_record_sets_hook)
2587 cselib_record_sets_hook (insn, sets, n_sets);
2589 /* Invalidate all locations written by this insn. Note that the elts we
2590 looked up in the previous loop aren't affected, just some of their
2591 locations may go away. */
2592 note_stores (body, cselib_invalidate_rtx_note_stores, NULL);
2594 for (i = n_sets_before_autoinc; i < n_sets; i++)
2595 cselib_invalidate_rtx (sets[i].dest);
2597 /* If this is an asm, look for duplicate sets. This can happen when the
2598 user uses the same value as an output multiple times. This is valid
2599 if the outputs are not actually used thereafter. Treat this case as
2600 if the value isn't actually set. We do this by smashing the destination
2601 to pc_rtx, so that we won't record the value later. */
2602 if (n_sets >= 2 && asm_noperands (body) >= 0)
2604 for (i = 0; i < n_sets; i++)
2606 rtx dest = sets[i].dest;
2607 if (REG_P (dest) || MEM_P (dest))
2609 int j;
2610 for (j = i + 1; j < n_sets; j++)
2611 if (rtx_equal_p (dest, sets[j].dest))
2613 sets[i].dest = pc_rtx;
2614 sets[j].dest = pc_rtx;
2620 /* Now enter the equivalences in our tables. */
2621 for (i = 0; i < n_sets; i++)
2623 rtx dest = sets[i].dest;
2624 if (REG_P (dest)
2625 || (MEM_P (dest) && cselib_record_memory))
2626 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2630 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
2632 bool
2633 fp_setter_insn (rtx_insn *insn)
2635 rtx expr, pat = NULL_RTX;
2637 if (!RTX_FRAME_RELATED_P (insn))
2638 return false;
2640 expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2641 if (expr)
2642 pat = XEXP (expr, 0);
2643 if (!modified_in_p (hard_frame_pointer_rtx, pat ? pat : insn))
2644 return false;
2646 /* Don't return true for frame pointer restores in the epilogue. */
2647 if (find_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx))
2648 return false;
2649 return true;
2652 /* Record the effects of INSN. */
2654 void
2655 cselib_process_insn (rtx_insn *insn)
2657 int i;
2658 rtx x;
2660 cselib_current_insn = insn;
2662 /* Forget everything at a CODE_LABEL or a setjmp. */
2663 if ((LABEL_P (insn)
2664 || (CALL_P (insn)
2665 && find_reg_note (insn, REG_SETJMP, NULL)))
2666 && !cselib_preserve_constants)
2668 cselib_reset_table (next_uid);
2669 cselib_current_insn = NULL;
2670 return;
2673 if (! INSN_P (insn))
2675 cselib_current_insn = NULL;
2676 return;
2679 /* If this is a call instruction, forget anything stored in a
2680 call clobbered register, or, if this is not a const call, in
2681 memory. */
2682 if (CALL_P (insn))
2684 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2685 if (call_used_regs[i]
2686 || (REG_VALUES (i) && REG_VALUES (i)->elt
2687 && HARD_REGNO_CALL_PART_CLOBBERED (i,
2688 GET_MODE (REG_VALUES (i)->elt->val_rtx))))
2689 cselib_invalidate_regno (i, reg_raw_mode[i]);
2691 /* Since it is not clear how cselib is going to be used, be
2692 conservative here and treat looping pure or const functions
2693 as if they were regular functions. */
2694 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
2695 || !(RTL_CONST_OR_PURE_CALL_P (insn)))
2696 cselib_invalidate_mem (callmem);
2699 cselib_record_sets (insn);
2701 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2702 after we have processed the insn. */
2703 if (CALL_P (insn))
2705 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
2706 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
2707 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
2708 /* Flush evertything on setjmp. */
2709 if (cselib_preserve_constants
2710 && find_reg_note (insn, REG_SETJMP, NULL))
2712 cselib_preserve_only_values ();
2713 cselib_reset_table (next_uid);
2717 /* On setter of the hard frame pointer if frame_pointer_needed,
2718 invalidate stack_pointer_rtx, so that sp and {,h}fp based
2719 VALUEs are distinct. */
2720 if (reload_completed
2721 && frame_pointer_needed
2722 && fp_setter_insn (insn))
2723 cselib_invalidate_rtx (stack_pointer_rtx);
2725 cselib_current_insn = NULL;
2727 if (n_useless_values > MAX_USELESS_VALUES
2728 /* remove_useless_values is linear in the hash table size. Avoid
2729 quadratic behavior for very large hashtables with very few
2730 useless elements. */
2731 && ((unsigned int)n_useless_values
2732 > (cselib_hash_table->elements () - n_debug_values) / 4))
2733 remove_useless_values ();
2736 /* Initialize cselib for one pass. The caller must also call
2737 init_alias_analysis. */
2739 void
2740 cselib_init (int record_what)
2742 cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
2743 cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
2744 cselib_any_perm_equivs = false;
2746 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2747 see canon_true_dependence. This is only created once. */
2748 if (! callmem)
2749 callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2751 cselib_nregs = max_reg_num ();
2753 /* We preserve reg_values to allow expensive clearing of the whole thing.
2754 Reallocate it however if it happens to be too large. */
2755 if (!reg_values || reg_values_size < cselib_nregs
2756 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
2758 free (reg_values);
2759 /* Some space for newly emit instructions so we don't end up
2760 reallocating in between passes. */
2761 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
2762 reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
2764 used_regs = XNEWVEC (unsigned int, cselib_nregs);
2765 n_used_regs = 0;
2766 cselib_hash_table = new hash_table<cselib_hasher> (31);
2767 if (cselib_preserve_constants)
2768 cselib_preserved_hash_table = new hash_table<cselib_hasher> (31);
2769 next_uid = 1;
2772 /* Called when the current user is done with cselib. */
2774 void
2775 cselib_finish (void)
2777 bool preserved = cselib_preserve_constants;
2778 cselib_discard_hook = NULL;
2779 cselib_preserve_constants = false;
2780 cselib_any_perm_equivs = false;
2781 cfa_base_preserved_val = NULL;
2782 cfa_base_preserved_regno = INVALID_REGNUM;
2783 elt_list::pool.release ();
2784 elt_loc_list::pool.release ();
2785 cselib_val::pool.release ();
2786 value_pool.release ();
2787 cselib_clear_table ();
2788 delete cselib_hash_table;
2789 cselib_hash_table = NULL;
2790 if (preserved)
2791 delete cselib_preserved_hash_table;
2792 cselib_preserved_hash_table = NULL;
2793 free (used_regs);
2794 used_regs = 0;
2795 n_useless_values = 0;
2796 n_useless_debug_values = 0;
2797 n_debug_values = 0;
2798 next_uid = 0;
2801 /* Dump the cselib_val *X to FILE *OUT. */
2804 dump_cselib_val (cselib_val **x, FILE *out)
2806 cselib_val *v = *x;
2807 bool need_lf = true;
2809 print_inline_rtx (out, v->val_rtx, 0);
2811 if (v->locs)
2813 struct elt_loc_list *l = v->locs;
2814 if (need_lf)
2816 fputc ('\n', out);
2817 need_lf = false;
2819 fputs (" locs:", out);
2822 if (l->setting_insn)
2823 fprintf (out, "\n from insn %i ",
2824 INSN_UID (l->setting_insn));
2825 else
2826 fprintf (out, "\n ");
2827 print_inline_rtx (out, l->loc, 4);
2829 while ((l = l->next));
2830 fputc ('\n', out);
2832 else
2834 fputs (" no locs", out);
2835 need_lf = true;
2838 if (v->addr_list)
2840 struct elt_list *e = v->addr_list;
2841 if (need_lf)
2843 fputc ('\n', out);
2844 need_lf = false;
2846 fputs (" addr list:", out);
2849 fputs ("\n ", out);
2850 print_inline_rtx (out, e->elt->val_rtx, 2);
2852 while ((e = e->next));
2853 fputc ('\n', out);
2855 else
2857 fputs (" no addrs", out);
2858 need_lf = true;
2861 if (v->next_containing_mem == &dummy_val)
2862 fputs (" last mem\n", out);
2863 else if (v->next_containing_mem)
2865 fputs (" next mem ", out);
2866 print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
2867 fputc ('\n', out);
2869 else if (need_lf)
2870 fputc ('\n', out);
2872 return 1;
2875 /* Dump to OUT everything in the CSELIB table. */
2877 void
2878 dump_cselib_table (FILE *out)
2880 fprintf (out, "cselib hash table:\n");
2881 cselib_hash_table->traverse <FILE *, dump_cselib_val> (out);
2882 fprintf (out, "cselib preserved hash table:\n");
2883 cselib_preserved_hash_table->traverse <FILE *, dump_cselib_val> (out);
2884 if (first_containing_mem != &dummy_val)
2886 fputs ("first mem ", out);
2887 print_inline_rtx (out, first_containing_mem->val_rtx, 2);
2888 fputc ('\n', out);
2890 fprintf (out, "next uid %i\n", next_uid);
2893 #include "gt-cselib.h"