[RS6000] push_secondary_reload ICE
[official-gcc.git] / gcc / cselib.c
blob0c5183cf7307e9dd53d8a0c71c6913a80ee2d647
1 /* Common subexpression elimination library for GNU compiler.
2 Copyright (C) 1987-2016 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 "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "df.h"
28 #include "tm_p.h"
29 #include "regs.h"
30 #include "emit-rtl.h"
31 #include "dumpfile.h"
32 #include "cselib.h"
33 #include "params.h"
35 /* A list of cselib_val structures. */
36 struct elt_list
38 struct elt_list *next;
39 cselib_val *elt;
42 static bool cselib_record_memory;
43 static bool cselib_preserve_constants;
44 static bool cselib_any_perm_equivs;
45 static inline void promote_debug_loc (struct elt_loc_list *l);
46 static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
47 static void new_elt_loc_list (cselib_val *, rtx);
48 static void unchain_one_value (cselib_val *);
49 static void unchain_one_elt_list (struct elt_list **);
50 static void unchain_one_elt_loc_list (struct elt_loc_list **);
51 static void remove_useless_values (void);
52 static unsigned int cselib_hash_rtx (rtx, int, machine_mode);
53 static cselib_val *new_cselib_val (unsigned int, machine_mode, rtx);
54 static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
55 static cselib_val *cselib_lookup_mem (rtx, int);
56 static void cselib_invalidate_regno (unsigned int, machine_mode);
57 static void cselib_invalidate_mem (rtx);
58 static void cselib_record_set (rtx, cselib_val *, cselib_val *);
59 static void cselib_record_sets (rtx_insn *);
61 struct expand_value_data
63 bitmap regs_active;
64 cselib_expand_callback callback;
65 void *callback_arg;
66 bool dummy;
69 static rtx cselib_expand_value_rtx_1 (rtx, struct expand_value_data *, int);
71 /* There are three ways in which cselib can look up an rtx:
72 - for a REG, the reg_values table (which is indexed by regno) is used
73 - for a MEM, we recursively look up its address and then follow the
74 addr_list of that value
75 - for everything else, we compute a hash value and go through the hash
76 table. Since different rtx's can still have the same hash value,
77 this involves walking the table entries for a given value and comparing
78 the locations of the entries with the rtx we are looking up. */
80 struct cselib_hasher : nofree_ptr_hash <cselib_val>
82 struct key {
83 /* The rtx value and its mode (needed separately for constant
84 integers). */
85 machine_mode mode;
86 rtx x;
87 /* The mode of the contaning MEM, if any, otherwise VOIDmode. */
88 machine_mode memmode;
90 typedef key *compare_type;
91 static inline hashval_t hash (const cselib_val *);
92 static inline bool equal (const cselib_val *, const key *);
95 /* The hash function for our hash table. The value is always computed with
96 cselib_hash_rtx when adding an element; this function just extracts the
97 hash value from a cselib_val structure. */
99 inline hashval_t
100 cselib_hasher::hash (const cselib_val *v)
102 return v->hash;
105 /* The equality test for our hash table. The first argument V is a table
106 element (i.e. a cselib_val), while the second arg X is an rtx. We know
107 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
108 CONST of an appropriate mode. */
110 inline bool
111 cselib_hasher::equal (const cselib_val *v, const key *x_arg)
113 struct elt_loc_list *l;
114 rtx x = x_arg->x;
115 machine_mode mode = x_arg->mode;
116 machine_mode memmode = x_arg->memmode;
118 if (mode != GET_MODE (v->val_rtx))
119 return false;
121 if (GET_CODE (x) == VALUE)
122 return x == v->val_rtx;
124 /* We don't guarantee that distinct rtx's have different hash values,
125 so we need to do a comparison. */
126 for (l = v->locs; l; l = l->next)
127 if (rtx_equal_for_cselib_1 (l->loc, x, memmode))
129 promote_debug_loc (l);
130 return true;
133 return false;
136 /* A table that enables us to look up elts by their value. */
137 static hash_table<cselib_hasher> *cselib_hash_table;
139 /* A table to hold preserved values. */
140 static hash_table<cselib_hasher> *cselib_preserved_hash_table;
142 /* This is a global so we don't have to pass this through every function.
143 It is used in new_elt_loc_list to set SETTING_INSN. */
144 static rtx_insn *cselib_current_insn;
146 /* The unique id that the next create value will take. */
147 static unsigned int next_uid;
149 /* The number of registers we had when the varrays were last resized. */
150 static unsigned int cselib_nregs;
152 /* Count values without known locations, or with only locations that
153 wouldn't have been known except for debug insns. Whenever this
154 grows too big, we remove these useless values from the table.
156 Counting values with only debug values is a bit tricky. We don't
157 want to increment n_useless_values when we create a value for a
158 debug insn, for this would get n_useless_values out of sync, but we
159 want increment it if all locs in the list that were ever referenced
160 in nondebug insns are removed from the list.
162 In the general case, once we do that, we'd have to stop accepting
163 nondebug expressions in the loc list, to avoid having two values
164 equivalent that, without debug insns, would have been made into
165 separate values. However, because debug insns never introduce
166 equivalences themselves (no assignments), the only means for
167 growing loc lists is through nondebug assignments. If the locs
168 also happen to be referenced in debug insns, it will work just fine.
170 A consequence of this is that there's at most one debug-only loc in
171 each loc list. If we keep it in the first entry, testing whether
172 we have a debug-only loc list takes O(1).
174 Furthermore, since any additional entry in a loc list containing a
175 debug loc would have to come from an assignment (nondebug) that
176 references both the initial debug loc and the newly-equivalent loc,
177 the initial debug loc would be promoted to a nondebug loc, and the
178 loc list would not contain debug locs any more.
180 So the only case we have to be careful with in order to keep
181 n_useless_values in sync between debug and nondebug compilations is
182 to avoid incrementing n_useless_values when removing the single loc
183 from a value that turns out to not appear outside debug values. We
184 increment n_useless_debug_values instead, and leave such values
185 alone until, for other reasons, we garbage-collect useless
186 values. */
187 static int n_useless_values;
188 static int n_useless_debug_values;
190 /* Count values whose locs have been taken exclusively from debug
191 insns for the entire life of the value. */
192 static int n_debug_values;
194 /* Number of useless values before we remove them from the hash table. */
195 #define MAX_USELESS_VALUES 32
197 /* This table maps from register number to values. It does not
198 contain pointers to cselib_val structures, but rather elt_lists.
199 The purpose is to be able to refer to the same register in
200 different modes. The first element of the list defines the mode in
201 which the register was set; if the mode is unknown or the value is
202 no longer valid in that mode, ELT will be NULL for the first
203 element. */
204 static struct elt_list **reg_values;
205 static unsigned int reg_values_size;
206 #define REG_VALUES(i) reg_values[i]
208 /* The largest number of hard regs used by any entry added to the
209 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
210 static unsigned int max_value_regs;
212 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
213 in cselib_clear_table() for fast emptying. */
214 static unsigned int *used_regs;
215 static unsigned int n_used_regs;
217 /* We pass this to cselib_invalidate_mem to invalidate all of
218 memory for a non-const call instruction. */
219 static GTY(()) rtx callmem;
221 /* Set by discard_useless_locs if it deleted the last location of any
222 value. */
223 static int values_became_useless;
225 /* Used as stop element of the containing_mem list so we can check
226 presence in the list by checking the next pointer. */
227 static cselib_val dummy_val;
229 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
230 that is constant through the whole function and should never be
231 eliminated. */
232 static cselib_val *cfa_base_preserved_val;
233 static unsigned int cfa_base_preserved_regno = INVALID_REGNUM;
235 /* Used to list all values that contain memory reference.
236 May or may not contain the useless values - the list is compacted
237 each time memory is invalidated. */
238 static cselib_val *first_containing_mem = &dummy_val;
240 static object_allocator<elt_list> elt_list_pool ("elt_list");
241 static object_allocator<elt_loc_list> elt_loc_list_pool ("elt_loc_list");
242 static object_allocator<cselib_val> cselib_val_pool ("cselib_val_list");
244 static pool_allocator value_pool ("value", RTX_CODE_SIZE (VALUE));
246 /* If nonnull, cselib will call this function before freeing useless
247 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
248 void (*cselib_discard_hook) (cselib_val *);
250 /* If nonnull, cselib will call this function before recording sets or
251 even clobbering outputs of INSN. All the recorded sets will be
252 represented in the array sets[n_sets]. new_val_min can be used to
253 tell whether values present in sets are introduced by this
254 instruction. */
255 void (*cselib_record_sets_hook) (rtx_insn *insn, struct cselib_set *sets,
256 int n_sets);
258 #define PRESERVED_VALUE_P(RTX) \
259 (RTL_FLAG_CHECK1 ("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
261 #define SP_BASED_VALUE_P(RTX) \
262 (RTL_FLAG_CHECK1 ("SP_BASED_VALUE_P", (RTX), VALUE)->jump)
266 /* Allocate a struct elt_list and fill in its two elements with the
267 arguments. */
269 static inline struct elt_list *
270 new_elt_list (struct elt_list *next, cselib_val *elt)
272 elt_list *el = elt_list_pool.allocate ();
273 el->next = next;
274 el->elt = elt;
275 return el;
278 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
279 list. */
281 static inline void
282 new_elt_loc_list (cselib_val *val, rtx loc)
284 struct elt_loc_list *el, *next = val->locs;
286 gcc_checking_assert (!next || !next->setting_insn
287 || !DEBUG_INSN_P (next->setting_insn)
288 || cselib_current_insn == next->setting_insn);
290 /* If we're creating the first loc in a debug insn context, we've
291 just created a debug value. Count it. */
292 if (!next && cselib_current_insn && DEBUG_INSN_P (cselib_current_insn))
293 n_debug_values++;
295 val = canonical_cselib_val (val);
296 next = val->locs;
298 if (GET_CODE (loc) == VALUE)
300 loc = canonical_cselib_val (CSELIB_VAL_PTR (loc))->val_rtx;
302 gcc_checking_assert (PRESERVED_VALUE_P (loc)
303 == PRESERVED_VALUE_P (val->val_rtx));
305 if (val->val_rtx == loc)
306 return;
307 else if (val->uid > CSELIB_VAL_PTR (loc)->uid)
309 /* Reverse the insertion. */
310 new_elt_loc_list (CSELIB_VAL_PTR (loc), val->val_rtx);
311 return;
314 gcc_checking_assert (val->uid < CSELIB_VAL_PTR (loc)->uid);
316 if (CSELIB_VAL_PTR (loc)->locs)
318 /* Bring all locs from LOC to VAL. */
319 for (el = CSELIB_VAL_PTR (loc)->locs; el->next; el = el->next)
321 /* Adjust values that have LOC as canonical so that VAL
322 becomes their canonical. */
323 if (el->loc && GET_CODE (el->loc) == VALUE)
325 gcc_checking_assert (CSELIB_VAL_PTR (el->loc)->locs->loc
326 == loc);
327 CSELIB_VAL_PTR (el->loc)->locs->loc = val->val_rtx;
330 el->next = val->locs;
331 next = val->locs = CSELIB_VAL_PTR (loc)->locs;
334 if (CSELIB_VAL_PTR (loc)->addr_list)
336 /* Bring in addr_list into canonical node. */
337 struct elt_list *last = CSELIB_VAL_PTR (loc)->addr_list;
338 while (last->next)
339 last = last->next;
340 last->next = val->addr_list;
341 val->addr_list = CSELIB_VAL_PTR (loc)->addr_list;
342 CSELIB_VAL_PTR (loc)->addr_list = NULL;
345 if (CSELIB_VAL_PTR (loc)->next_containing_mem != NULL
346 && val->next_containing_mem == NULL)
348 /* Add VAL to the containing_mem list after LOC. LOC will
349 be removed when we notice it doesn't contain any
350 MEMs. */
351 val->next_containing_mem = CSELIB_VAL_PTR (loc)->next_containing_mem;
352 CSELIB_VAL_PTR (loc)->next_containing_mem = val;
355 /* Chain LOC back to VAL. */
356 el = elt_loc_list_pool.allocate ();
357 el->loc = val->val_rtx;
358 el->setting_insn = cselib_current_insn;
359 el->next = NULL;
360 CSELIB_VAL_PTR (loc)->locs = el;
363 el = elt_loc_list_pool.allocate ();
364 el->loc = loc;
365 el->setting_insn = cselib_current_insn;
366 el->next = next;
367 val->locs = el;
370 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
371 originating from a debug insn, maintaining the debug values
372 count. */
374 static inline void
375 promote_debug_loc (struct elt_loc_list *l)
377 if (l && l->setting_insn && DEBUG_INSN_P (l->setting_insn)
378 && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
380 n_debug_values--;
381 l->setting_insn = cselib_current_insn;
382 if (cselib_preserve_constants && l->next)
384 gcc_assert (l->next->setting_insn
385 && DEBUG_INSN_P (l->next->setting_insn)
386 && !l->next->next);
387 l->next->setting_insn = cselib_current_insn;
389 else
390 gcc_assert (!l->next);
394 /* The elt_list at *PL is no longer needed. Unchain it and free its
395 storage. */
397 static inline void
398 unchain_one_elt_list (struct elt_list **pl)
400 struct elt_list *l = *pl;
402 *pl = l->next;
403 elt_list_pool.remove (l);
406 /* Likewise for elt_loc_lists. */
408 static void
409 unchain_one_elt_loc_list (struct elt_loc_list **pl)
411 struct elt_loc_list *l = *pl;
413 *pl = l->next;
414 elt_loc_list_pool.remove (l);
417 /* Likewise for cselib_vals. This also frees the addr_list associated with
418 V. */
420 static void
421 unchain_one_value (cselib_val *v)
423 while (v->addr_list)
424 unchain_one_elt_list (&v->addr_list);
426 cselib_val_pool.remove (v);
429 /* Remove all entries from the hash table. Also used during
430 initialization. */
432 void
433 cselib_clear_table (void)
435 cselib_reset_table (1);
438 /* Return TRUE if V is a constant, a function invariant or a VALUE
439 equivalence; FALSE otherwise. */
441 static bool
442 invariant_or_equiv_p (cselib_val *v)
444 struct elt_loc_list *l;
446 if (v == cfa_base_preserved_val)
447 return true;
449 /* Keep VALUE equivalences around. */
450 for (l = v->locs; l; l = l->next)
451 if (GET_CODE (l->loc) == VALUE)
452 return true;
454 if (v->locs != NULL
455 && v->locs->next == NULL)
457 if (CONSTANT_P (v->locs->loc)
458 && (GET_CODE (v->locs->loc) != CONST
459 || !references_value_p (v->locs->loc, 0)))
460 return true;
461 /* Although a debug expr may be bound to different expressions,
462 we can preserve it as if it was constant, to get unification
463 and proper merging within var-tracking. */
464 if (GET_CODE (v->locs->loc) == DEBUG_EXPR
465 || GET_CODE (v->locs->loc) == DEBUG_IMPLICIT_PTR
466 || GET_CODE (v->locs->loc) == ENTRY_VALUE
467 || GET_CODE (v->locs->loc) == DEBUG_PARAMETER_REF)
468 return true;
470 /* (plus (value V) (const_int C)) is invariant iff V is invariant. */
471 if (GET_CODE (v->locs->loc) == PLUS
472 && CONST_INT_P (XEXP (v->locs->loc, 1))
473 && GET_CODE (XEXP (v->locs->loc, 0)) == VALUE
474 && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v->locs->loc, 0))))
475 return true;
478 return false;
481 /* Remove from hash table all VALUEs except constants, function
482 invariants and VALUE equivalences. */
485 preserve_constants_and_equivs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
487 cselib_val *v = *x;
489 if (invariant_or_equiv_p (v))
491 cselib_hasher::key lookup = {
492 GET_MODE (v->val_rtx), v->val_rtx, VOIDmode
494 cselib_val **slot
495 = cselib_preserved_hash_table->find_slot_with_hash (&lookup,
496 v->hash, INSERT);
497 gcc_assert (!*slot);
498 *slot = v;
501 cselib_hash_table->clear_slot (x);
503 return 1;
506 /* Remove all entries from the hash table, arranging for the next
507 value to be numbered NUM. */
509 void
510 cselib_reset_table (unsigned int num)
512 unsigned int i;
514 max_value_regs = 0;
516 if (cfa_base_preserved_val)
518 unsigned int regno = cfa_base_preserved_regno;
519 unsigned int new_used_regs = 0;
520 for (i = 0; i < n_used_regs; i++)
521 if (used_regs[i] == regno)
523 new_used_regs = 1;
524 continue;
526 else
527 REG_VALUES (used_regs[i]) = 0;
528 gcc_assert (new_used_regs == 1);
529 n_used_regs = new_used_regs;
530 used_regs[0] = regno;
531 max_value_regs
532 = hard_regno_nregs[regno][GET_MODE (cfa_base_preserved_val->locs->loc)];
534 else
536 for (i = 0; i < n_used_regs; i++)
537 REG_VALUES (used_regs[i]) = 0;
538 n_used_regs = 0;
541 if (cselib_preserve_constants)
542 cselib_hash_table->traverse <void *, preserve_constants_and_equivs>
543 (NULL);
544 else
546 cselib_hash_table->empty ();
547 gcc_checking_assert (!cselib_any_perm_equivs);
550 n_useless_values = 0;
551 n_useless_debug_values = 0;
552 n_debug_values = 0;
554 next_uid = num;
556 first_containing_mem = &dummy_val;
559 /* Return the number of the next value that will be generated. */
561 unsigned int
562 cselib_get_next_uid (void)
564 return next_uid;
567 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
568 INSERTing if requested. When X is part of the address of a MEM,
569 MEMMODE should specify the mode of the MEM. */
571 static cselib_val **
572 cselib_find_slot (machine_mode mode, rtx x, hashval_t hash,
573 enum insert_option insert, machine_mode memmode)
575 cselib_val **slot = NULL;
576 cselib_hasher::key lookup = { mode, x, memmode };
577 if (cselib_preserve_constants)
578 slot = cselib_preserved_hash_table->find_slot_with_hash (&lookup, hash,
579 NO_INSERT);
580 if (!slot)
581 slot = cselib_hash_table->find_slot_with_hash (&lookup, hash, insert);
582 return slot;
585 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
586 only return true for values which point to a cselib_val whose value
587 element has been set to zero, which implies the cselib_val will be
588 removed. */
591 references_value_p (const_rtx x, int only_useless)
593 const enum rtx_code code = GET_CODE (x);
594 const char *fmt = GET_RTX_FORMAT (code);
595 int i, j;
597 if (GET_CODE (x) == VALUE
598 && (! only_useless ||
599 (CSELIB_VAL_PTR (x)->locs == 0 && !PRESERVED_VALUE_P (x))))
600 return 1;
602 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
604 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
605 return 1;
606 else if (fmt[i] == 'E')
607 for (j = 0; j < XVECLEN (x, i); j++)
608 if (references_value_p (XVECEXP (x, i, j), only_useless))
609 return 1;
612 return 0;
615 /* For all locations found in X, delete locations that reference useless
616 values (i.e. values without any location). Called through
617 htab_traverse. */
620 discard_useless_locs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
622 cselib_val *v = *x;
623 struct elt_loc_list **p = &v->locs;
624 bool had_locs = v->locs != NULL;
625 rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
627 while (*p)
629 if (references_value_p ((*p)->loc, 1))
630 unchain_one_elt_loc_list (p);
631 else
632 p = &(*p)->next;
635 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
637 if (setting_insn && DEBUG_INSN_P (setting_insn))
638 n_useless_debug_values++;
639 else
640 n_useless_values++;
641 values_became_useless = 1;
643 return 1;
646 /* If X is a value with no locations, remove it from the hashtable. */
649 discard_useless_values (cselib_val **x, void *info ATTRIBUTE_UNUSED)
651 cselib_val *v = *x;
653 if (v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
655 if (cselib_discard_hook)
656 cselib_discard_hook (v);
658 CSELIB_VAL_PTR (v->val_rtx) = NULL;
659 cselib_hash_table->clear_slot (x);
660 unchain_one_value (v);
661 n_useless_values--;
664 return 1;
667 /* Clean out useless values (i.e. those which no longer have locations
668 associated with them) from the hash table. */
670 static void
671 remove_useless_values (void)
673 cselib_val **p, *v;
675 /* First pass: eliminate locations that reference the value. That in
676 turn can make more values useless. */
679 values_became_useless = 0;
680 cselib_hash_table->traverse <void *, discard_useless_locs> (NULL);
682 while (values_became_useless);
684 /* Second pass: actually remove the values. */
686 p = &first_containing_mem;
687 for (v = *p; v != &dummy_val; v = v->next_containing_mem)
688 if (v->locs && v == canonical_cselib_val (v))
690 *p = v;
691 p = &(*p)->next_containing_mem;
693 *p = &dummy_val;
695 n_useless_values += n_useless_debug_values;
696 n_debug_values -= n_useless_debug_values;
697 n_useless_debug_values = 0;
699 cselib_hash_table->traverse <void *, discard_useless_values> (NULL);
701 gcc_assert (!n_useless_values);
704 /* Arrange for a value to not be removed from the hash table even if
705 it becomes useless. */
707 void
708 cselib_preserve_value (cselib_val *v)
710 PRESERVED_VALUE_P (v->val_rtx) = 1;
713 /* Test whether a value is preserved. */
715 bool
716 cselib_preserved_value_p (cselib_val *v)
718 return PRESERVED_VALUE_P (v->val_rtx);
721 /* Arrange for a REG value to be assumed constant through the whole function,
722 never invalidated and preserved across cselib_reset_table calls. */
724 void
725 cselib_preserve_cfa_base_value (cselib_val *v, unsigned int regno)
727 if (cselib_preserve_constants
728 && v->locs
729 && REG_P (v->locs->loc))
731 cfa_base_preserved_val = v;
732 cfa_base_preserved_regno = regno;
736 /* Clean all non-constant expressions in the hash table, but retain
737 their values. */
739 void
740 cselib_preserve_only_values (void)
742 int i;
744 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
745 cselib_invalidate_regno (i, reg_raw_mode[i]);
747 cselib_invalidate_mem (callmem);
749 remove_useless_values ();
751 gcc_assert (first_containing_mem == &dummy_val);
754 /* Arrange for a value to be marked as based on stack pointer
755 for find_base_term purposes. */
757 void
758 cselib_set_value_sp_based (cselib_val *v)
760 SP_BASED_VALUE_P (v->val_rtx) = 1;
763 /* Test whether a value is based on stack pointer for
764 find_base_term purposes. */
766 bool
767 cselib_sp_based_value_p (cselib_val *v)
769 return SP_BASED_VALUE_P (v->val_rtx);
772 /* Return the mode in which a register was last set. If X is not a
773 register, return its mode. If the mode in which the register was
774 set is not known, or the value was already clobbered, return
775 VOIDmode. */
777 machine_mode
778 cselib_reg_set_mode (const_rtx x)
780 if (!REG_P (x))
781 return GET_MODE (x);
783 if (REG_VALUES (REGNO (x)) == NULL
784 || REG_VALUES (REGNO (x))->elt == NULL)
785 return VOIDmode;
787 return GET_MODE (REG_VALUES (REGNO (x))->elt->val_rtx);
790 /* If x is a PLUS or an autoinc operation, expand the operation,
791 storing the offset, if any, in *OFF. */
793 static rtx
794 autoinc_split (rtx x, rtx *off, machine_mode memmode)
796 switch (GET_CODE (x))
798 case PLUS:
799 *off = XEXP (x, 1);
800 return XEXP (x, 0);
802 case PRE_DEC:
803 if (memmode == VOIDmode)
804 return x;
806 *off = GEN_INT (-GET_MODE_SIZE (memmode));
807 return XEXP (x, 0);
808 break;
810 case PRE_INC:
811 if (memmode == VOIDmode)
812 return x;
814 *off = GEN_INT (GET_MODE_SIZE (memmode));
815 return XEXP (x, 0);
817 case PRE_MODIFY:
818 return XEXP (x, 1);
820 case POST_DEC:
821 case POST_INC:
822 case POST_MODIFY:
823 return XEXP (x, 0);
825 default:
826 return x;
830 /* Return nonzero if we can prove that X and Y contain the same value,
831 taking our gathered information into account. MEMMODE holds the
832 mode of the enclosing MEM, if any, as required to deal with autoinc
833 addressing modes. If X and Y are not (known to be) part of
834 addresses, MEMMODE should be VOIDmode. */
837 rtx_equal_for_cselib_1 (rtx x, rtx y, machine_mode memmode)
839 enum rtx_code code;
840 const char *fmt;
841 int i;
843 if (REG_P (x) || MEM_P (x))
845 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0, memmode);
847 if (e)
848 x = e->val_rtx;
851 if (REG_P (y) || MEM_P (y))
853 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0, memmode);
855 if (e)
856 y = e->val_rtx;
859 if (x == y)
860 return 1;
862 if (GET_CODE (x) == VALUE)
864 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (x));
865 struct elt_loc_list *l;
867 if (GET_CODE (y) == VALUE)
868 return e == canonical_cselib_val (CSELIB_VAL_PTR (y));
870 for (l = e->locs; l; l = l->next)
872 rtx t = l->loc;
874 /* Avoid infinite recursion. We know we have the canonical
875 value, so we can just skip any values in the equivalence
876 list. */
877 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
878 continue;
879 else if (rtx_equal_for_cselib_1 (t, y, memmode))
880 return 1;
883 return 0;
885 else if (GET_CODE (y) == VALUE)
887 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (y));
888 struct elt_loc_list *l;
890 for (l = e->locs; l; l = l->next)
892 rtx t = l->loc;
894 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
895 continue;
896 else if (rtx_equal_for_cselib_1 (x, t, memmode))
897 return 1;
900 return 0;
903 if (GET_MODE (x) != GET_MODE (y))
904 return 0;
906 if (GET_CODE (x) != GET_CODE (y))
908 rtx xorig = x, yorig = y;
909 rtx xoff = NULL, yoff = NULL;
911 x = autoinc_split (x, &xoff, memmode);
912 y = autoinc_split (y, &yoff, memmode);
914 if (!xoff != !yoff)
915 return 0;
917 if (xoff && !rtx_equal_for_cselib_1 (xoff, yoff, memmode))
918 return 0;
920 /* Don't recurse if nothing changed. */
921 if (x != xorig || y != yorig)
922 return rtx_equal_for_cselib_1 (x, y, memmode);
924 return 0;
927 /* These won't be handled correctly by the code below. */
928 switch (GET_CODE (x))
930 CASE_CONST_UNIQUE:
931 case DEBUG_EXPR:
932 return 0;
934 case DEBUG_IMPLICIT_PTR:
935 return DEBUG_IMPLICIT_PTR_DECL (x)
936 == DEBUG_IMPLICIT_PTR_DECL (y);
938 case DEBUG_PARAMETER_REF:
939 return DEBUG_PARAMETER_REF_DECL (x)
940 == DEBUG_PARAMETER_REF_DECL (y);
942 case ENTRY_VALUE:
943 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
944 use rtx_equal_for_cselib_1 to compare the operands. */
945 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
947 case LABEL_REF:
948 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
950 case REG:
951 return REGNO (x) == REGNO (y);
953 case MEM:
954 /* We have to compare any autoinc operations in the addresses
955 using this MEM's mode. */
956 return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x));
958 default:
959 break;
962 code = GET_CODE (x);
963 fmt = GET_RTX_FORMAT (code);
965 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
967 int j;
969 switch (fmt[i])
971 case 'w':
972 if (XWINT (x, i) != XWINT (y, i))
973 return 0;
974 break;
976 case 'n':
977 case 'i':
978 if (XINT (x, i) != XINT (y, i))
979 return 0;
980 break;
982 case 'V':
983 case 'E':
984 /* Two vectors must have the same length. */
985 if (XVECLEN (x, i) != XVECLEN (y, i))
986 return 0;
988 /* And the corresponding elements must match. */
989 for (j = 0; j < XVECLEN (x, i); j++)
990 if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
991 XVECEXP (y, i, j), memmode))
992 return 0;
993 break;
995 case 'e':
996 if (i == 1
997 && targetm.commutative_p (x, UNKNOWN)
998 && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode)
999 && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode))
1000 return 1;
1001 if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode))
1002 return 0;
1003 break;
1005 case 'S':
1006 case 's':
1007 if (strcmp (XSTR (x, i), XSTR (y, i)))
1008 return 0;
1009 break;
1011 case 'u':
1012 /* These are just backpointers, so they don't matter. */
1013 break;
1015 case '0':
1016 case 't':
1017 break;
1019 /* It is believed that rtx's at this level will never
1020 contain anything but integers and other rtx's,
1021 except for within LABEL_REFs and SYMBOL_REFs. */
1022 default:
1023 gcc_unreachable ();
1026 return 1;
1029 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1030 For registers and memory locations, we look up their cselib_val structure
1031 and return its VALUE element.
1032 Possible reasons for return 0 are: the object is volatile, or we couldn't
1033 find a register or memory location in the table and CREATE is zero. If
1034 CREATE is nonzero, table elts are created for regs and mem.
1035 N.B. this hash function returns the same hash value for RTXes that
1036 differ only in the order of operands, thus it is suitable for comparisons
1037 that take commutativity into account.
1038 If we wanted to also support associative rules, we'd have to use a different
1039 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1040 MEMMODE indicates the mode of an enclosing MEM, and it's only
1041 used to compute autoinc values.
1042 We used to have a MODE argument for hashing for CONST_INTs, but that
1043 didn't make sense, since it caused spurious hash differences between
1044 (set (reg:SI 1) (const_int))
1045 (plus:SI (reg:SI 2) (reg:SI 1))
1047 (plus:SI (reg:SI 2) (const_int))
1048 If the mode is important in any context, it must be checked specifically
1049 in a comparison anyway, since relying on hash differences is unsafe. */
1051 static unsigned int
1052 cselib_hash_rtx (rtx x, int create, machine_mode memmode)
1054 cselib_val *e;
1055 int i, j;
1056 enum rtx_code code;
1057 const char *fmt;
1058 unsigned int hash = 0;
1060 code = GET_CODE (x);
1061 hash += (unsigned) code + (unsigned) GET_MODE (x);
1063 switch (code)
1065 case VALUE:
1066 e = CSELIB_VAL_PTR (x);
1067 return e->hash;
1069 case MEM:
1070 case REG:
1071 e = cselib_lookup (x, GET_MODE (x), create, memmode);
1072 if (! e)
1073 return 0;
1075 return e->hash;
1077 case DEBUG_EXPR:
1078 hash += ((unsigned) DEBUG_EXPR << 7)
1079 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
1080 return hash ? hash : (unsigned int) DEBUG_EXPR;
1082 case DEBUG_IMPLICIT_PTR:
1083 hash += ((unsigned) DEBUG_IMPLICIT_PTR << 7)
1084 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x));
1085 return hash ? hash : (unsigned int) DEBUG_IMPLICIT_PTR;
1087 case DEBUG_PARAMETER_REF:
1088 hash += ((unsigned) DEBUG_PARAMETER_REF << 7)
1089 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x));
1090 return hash ? hash : (unsigned int) DEBUG_PARAMETER_REF;
1092 case ENTRY_VALUE:
1093 /* ENTRY_VALUEs are function invariant, thus try to avoid
1094 recursing on argument if ENTRY_VALUE is one of the
1095 forms emitted by expand_debug_expr, otherwise
1096 ENTRY_VALUE hash would depend on the current value
1097 in some register or memory. */
1098 if (REG_P (ENTRY_VALUE_EXP (x)))
1099 hash += (unsigned int) REG
1100 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x))
1101 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x));
1102 else if (MEM_P (ENTRY_VALUE_EXP (x))
1103 && REG_P (XEXP (ENTRY_VALUE_EXP (x), 0)))
1104 hash += (unsigned int) MEM
1105 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x), 0))
1106 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x), 0));
1107 else
1108 hash += cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode);
1109 return hash ? hash : (unsigned int) ENTRY_VALUE;
1111 case CONST_INT:
1112 hash += ((unsigned) CONST_INT << 7) + UINTVAL (x);
1113 return hash ? hash : (unsigned int) CONST_INT;
1115 case CONST_WIDE_INT:
1116 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
1117 hash += CONST_WIDE_INT_ELT (x, i);
1118 return hash;
1120 case CONST_DOUBLE:
1121 /* This is like the general case, except that it only counts
1122 the integers representing the constant. */
1123 hash += (unsigned) code + (unsigned) GET_MODE (x);
1124 if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (x) == VOIDmode)
1125 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1126 + (unsigned) CONST_DOUBLE_HIGH (x));
1127 else
1128 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
1129 return hash ? hash : (unsigned int) CONST_DOUBLE;
1131 case CONST_FIXED:
1132 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1133 hash += fixed_hash (CONST_FIXED_VALUE (x));
1134 return hash ? hash : (unsigned int) CONST_FIXED;
1136 case CONST_VECTOR:
1138 int units;
1139 rtx elt;
1141 units = CONST_VECTOR_NUNITS (x);
1143 for (i = 0; i < units; ++i)
1145 elt = CONST_VECTOR_ELT (x, i);
1146 hash += cselib_hash_rtx (elt, 0, memmode);
1149 return hash;
1152 /* Assume there is only one rtx object for any given label. */
1153 case LABEL_REF:
1154 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1155 differences and differences between each stage's debugging dumps. */
1156 hash += (((unsigned int) LABEL_REF << 7)
1157 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x)));
1158 return hash ? hash : (unsigned int) LABEL_REF;
1160 case SYMBOL_REF:
1162 /* Don't hash on the symbol's address to avoid bootstrap differences.
1163 Different hash values may cause expressions to be recorded in
1164 different orders and thus different registers to be used in the
1165 final assembler. This also avoids differences in the dump files
1166 between various stages. */
1167 unsigned int h = 0;
1168 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1170 while (*p)
1171 h += (h << 7) + *p++; /* ??? revisit */
1173 hash += ((unsigned int) SYMBOL_REF << 7) + h;
1174 return hash ? hash : (unsigned int) SYMBOL_REF;
1177 case PRE_DEC:
1178 case PRE_INC:
1179 /* We can't compute these without knowing the MEM mode. */
1180 gcc_assert (memmode != VOIDmode);
1181 i = GET_MODE_SIZE (memmode);
1182 if (code == PRE_DEC)
1183 i = -i;
1184 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1185 like (mem:MEMMODE (plus (reg) (const_int I))). */
1186 hash += (unsigned) PLUS - (unsigned)code
1187 + cselib_hash_rtx (XEXP (x, 0), create, memmode)
1188 + cselib_hash_rtx (GEN_INT (i), create, memmode);
1189 return hash ? hash : 1 + (unsigned) PLUS;
1191 case PRE_MODIFY:
1192 gcc_assert (memmode != VOIDmode);
1193 return cselib_hash_rtx (XEXP (x, 1), create, memmode);
1195 case POST_DEC:
1196 case POST_INC:
1197 case POST_MODIFY:
1198 gcc_assert (memmode != VOIDmode);
1199 return cselib_hash_rtx (XEXP (x, 0), create, memmode);
1201 case PC:
1202 case CC0:
1203 case CALL:
1204 case UNSPEC_VOLATILE:
1205 return 0;
1207 case ASM_OPERANDS:
1208 if (MEM_VOLATILE_P (x))
1209 return 0;
1211 break;
1213 default:
1214 break;
1217 i = GET_RTX_LENGTH (code) - 1;
1218 fmt = GET_RTX_FORMAT (code);
1219 for (; i >= 0; i--)
1221 switch (fmt[i])
1223 case 'e':
1225 rtx tem = XEXP (x, i);
1226 unsigned int tem_hash = cselib_hash_rtx (tem, create, memmode);
1228 if (tem_hash == 0)
1229 return 0;
1231 hash += tem_hash;
1233 break;
1234 case 'E':
1235 for (j = 0; j < XVECLEN (x, i); j++)
1237 unsigned int tem_hash
1238 = cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
1240 if (tem_hash == 0)
1241 return 0;
1243 hash += tem_hash;
1245 break;
1247 case 's':
1249 const unsigned char *p = (const unsigned char *) XSTR (x, i);
1251 if (p)
1252 while (*p)
1253 hash += *p++;
1254 break;
1257 case 'i':
1258 hash += XINT (x, i);
1259 break;
1261 case '0':
1262 case 't':
1263 /* unused */
1264 break;
1266 default:
1267 gcc_unreachable ();
1271 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
1274 /* Create a new value structure for VALUE and initialize it. The mode of the
1275 value is MODE. */
1277 static inline cselib_val *
1278 new_cselib_val (unsigned int hash, machine_mode mode, rtx x)
1280 cselib_val *e = cselib_val_pool.allocate ();
1282 gcc_assert (hash);
1283 gcc_assert (next_uid);
1285 e->hash = hash;
1286 e->uid = next_uid++;
1287 /* We use an alloc pool to allocate this RTL construct because it
1288 accounts for about 8% of the overall memory usage. We know
1289 precisely when we can have VALUE RTXen (when cselib is active)
1290 so we don't need to put them in garbage collected memory.
1291 ??? Why should a VALUE be an RTX in the first place? */
1292 e->val_rtx = (rtx_def*) value_pool.allocate ();
1293 memset (e->val_rtx, 0, RTX_HDR_SIZE);
1294 PUT_CODE (e->val_rtx, VALUE);
1295 PUT_MODE (e->val_rtx, mode);
1296 CSELIB_VAL_PTR (e->val_rtx) = e;
1297 e->addr_list = 0;
1298 e->locs = 0;
1299 e->next_containing_mem = 0;
1301 if (dump_file && (dump_flags & TDF_CSELIB))
1303 fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
1304 if (flag_dump_noaddr || flag_dump_unnumbered)
1305 fputs ("# ", dump_file);
1306 else
1307 fprintf (dump_file, "%p ", (void*)e);
1308 print_rtl_single (dump_file, x);
1309 fputc ('\n', dump_file);
1312 return e;
1315 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1316 contains the data at this address. X is a MEM that represents the
1317 value. Update the two value structures to represent this situation. */
1319 static void
1320 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
1322 addr_elt = canonical_cselib_val (addr_elt);
1323 mem_elt = canonical_cselib_val (mem_elt);
1325 /* Avoid duplicates. */
1326 addr_space_t as = MEM_ADDR_SPACE (x);
1327 for (elt_loc_list *l = mem_elt->locs; l; l = l->next)
1328 if (MEM_P (l->loc)
1329 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt
1330 && MEM_ADDR_SPACE (l->loc) == as)
1332 promote_debug_loc (l);
1333 return;
1336 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
1337 new_elt_loc_list (mem_elt,
1338 replace_equiv_address_nv (x, addr_elt->val_rtx));
1339 if (mem_elt->next_containing_mem == NULL)
1341 mem_elt->next_containing_mem = first_containing_mem;
1342 first_containing_mem = mem_elt;
1346 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1347 If CREATE, make a new one if we haven't seen it before. */
1349 static cselib_val *
1350 cselib_lookup_mem (rtx x, int create)
1352 machine_mode mode = GET_MODE (x);
1353 machine_mode addr_mode;
1354 cselib_val **slot;
1355 cselib_val *addr;
1356 cselib_val *mem_elt;
1358 if (MEM_VOLATILE_P (x) || mode == BLKmode
1359 || !cselib_record_memory
1360 || (FLOAT_MODE_P (mode) && flag_float_store))
1361 return 0;
1363 addr_mode = GET_MODE (XEXP (x, 0));
1364 if (addr_mode == VOIDmode)
1365 addr_mode = Pmode;
1367 /* Look up the value for the address. */
1368 addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
1369 if (! addr)
1370 return 0;
1371 addr = canonical_cselib_val (addr);
1373 /* Find a value that describes a value of our mode at that address. */
1374 addr_space_t as = MEM_ADDR_SPACE (x);
1375 for (elt_list *l = addr->addr_list; l; l = l->next)
1376 if (GET_MODE (l->elt->val_rtx) == mode)
1378 for (elt_loc_list *l2 = l->elt->locs; l2; l2 = l2->next)
1379 if (MEM_P (l2->loc) && MEM_ADDR_SPACE (l2->loc) == as)
1381 promote_debug_loc (l->elt->locs);
1382 return l->elt;
1386 if (! create)
1387 return 0;
1389 mem_elt = new_cselib_val (next_uid, mode, x);
1390 add_mem_for_addr (addr, mem_elt, x);
1391 slot = cselib_find_slot (mode, x, mem_elt->hash, INSERT, VOIDmode);
1392 *slot = mem_elt;
1393 return mem_elt;
1396 /* Search through the possible substitutions in P. We prefer a non reg
1397 substitution because this allows us to expand the tree further. If
1398 we find, just a reg, take the lowest regno. There may be several
1399 non-reg results, we just take the first one because they will all
1400 expand to the same place. */
1402 static rtx
1403 expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1404 int max_depth)
1406 rtx reg_result = NULL;
1407 unsigned int regno = UINT_MAX;
1408 struct elt_loc_list *p_in = p;
1410 for (; p; p = p->next)
1412 /* Return these right away to avoid returning stack pointer based
1413 expressions for frame pointer and vice versa, which is something
1414 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1415 for more details. */
1416 if (REG_P (p->loc)
1417 && (REGNO (p->loc) == STACK_POINTER_REGNUM
1418 || REGNO (p->loc) == FRAME_POINTER_REGNUM
1419 || REGNO (p->loc) == HARD_FRAME_POINTER_REGNUM
1420 || REGNO (p->loc) == cfa_base_preserved_regno))
1421 return p->loc;
1422 /* Avoid infinite recursion trying to expand a reg into a
1423 the same reg. */
1424 if ((REG_P (p->loc))
1425 && (REGNO (p->loc) < regno)
1426 && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
1428 reg_result = p->loc;
1429 regno = REGNO (p->loc);
1431 /* Avoid infinite recursion and do not try to expand the
1432 value. */
1433 else if (GET_CODE (p->loc) == VALUE
1434 && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1435 continue;
1436 else if (!REG_P (p->loc))
1438 rtx result, note;
1439 if (dump_file && (dump_flags & TDF_CSELIB))
1441 print_inline_rtx (dump_file, p->loc, 0);
1442 fprintf (dump_file, "\n");
1444 if (GET_CODE (p->loc) == LO_SUM
1445 && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1446 && p->setting_insn
1447 && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1448 && XEXP (note, 0) == XEXP (p->loc, 1))
1449 return XEXP (p->loc, 1);
1450 result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
1451 if (result)
1452 return result;
1457 if (regno != UINT_MAX)
1459 rtx result;
1460 if (dump_file && (dump_flags & TDF_CSELIB))
1461 fprintf (dump_file, "r%d\n", regno);
1463 result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
1464 if (result)
1465 return result;
1468 if (dump_file && (dump_flags & TDF_CSELIB))
1470 if (reg_result)
1472 print_inline_rtx (dump_file, reg_result, 0);
1473 fprintf (dump_file, "\n");
1475 else
1476 fprintf (dump_file, "NULL\n");
1478 return reg_result;
1482 /* Forward substitute and expand an expression out to its roots.
1483 This is the opposite of common subexpression. Because local value
1484 numbering is such a weak optimization, the expanded expression is
1485 pretty much unique (not from a pointer equals point of view but
1486 from a tree shape point of view.
1488 This function returns NULL if the expansion fails. The expansion
1489 will fail if there is no value number for one of the operands or if
1490 one of the operands has been overwritten between the current insn
1491 and the beginning of the basic block. For instance x has no
1492 expansion in:
1494 r1 <- r1 + 3
1495 x <- r1 + 8
1497 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1498 It is clear on return. */
1501 cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
1503 struct expand_value_data evd;
1505 evd.regs_active = regs_active;
1506 evd.callback = NULL;
1507 evd.callback_arg = NULL;
1508 evd.dummy = false;
1510 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1513 /* Same as cselib_expand_value_rtx, but using a callback to try to
1514 resolve some expressions. The CB function should return ORIG if it
1515 can't or does not want to deal with a certain RTX. Any other
1516 return value, including NULL, will be used as the expansion for
1517 VALUE, without any further changes. */
1520 cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1521 cselib_expand_callback cb, void *data)
1523 struct expand_value_data evd;
1525 evd.regs_active = regs_active;
1526 evd.callback = cb;
1527 evd.callback_arg = data;
1528 evd.dummy = false;
1530 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1533 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1534 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1535 would return NULL or non-NULL, without allocating new rtx. */
1537 bool
1538 cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1539 cselib_expand_callback cb, void *data)
1541 struct expand_value_data evd;
1543 evd.regs_active = regs_active;
1544 evd.callback = cb;
1545 evd.callback_arg = data;
1546 evd.dummy = true;
1548 return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1551 /* Internal implementation of cselib_expand_value_rtx and
1552 cselib_expand_value_rtx_cb. */
1554 static rtx
1555 cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1556 int max_depth)
1558 rtx copy, scopy;
1559 int i, j;
1560 RTX_CODE code;
1561 const char *format_ptr;
1562 machine_mode mode;
1564 code = GET_CODE (orig);
1566 /* For the context of dse, if we end up expand into a huge tree, we
1567 will not have a useful address, so we might as well just give up
1568 quickly. */
1569 if (max_depth <= 0)
1570 return NULL;
1572 switch (code)
1574 case REG:
1576 struct elt_list *l = REG_VALUES (REGNO (orig));
1578 if (l && l->elt == NULL)
1579 l = l->next;
1580 for (; l; l = l->next)
1581 if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1583 rtx result;
1584 unsigned regno = REGNO (orig);
1586 /* The only thing that we are not willing to do (this
1587 is requirement of dse and if others potential uses
1588 need this function we should add a parm to control
1589 it) is that we will not substitute the
1590 STACK_POINTER_REGNUM, FRAME_POINTER or the
1591 HARD_FRAME_POINTER.
1593 These expansions confuses the code that notices that
1594 stores into the frame go dead at the end of the
1595 function and that the frame is not effected by calls
1596 to subroutines. If you allow the
1597 STACK_POINTER_REGNUM substitution, then dse will
1598 think that parameter pushing also goes dead which is
1599 wrong. If you allow the FRAME_POINTER or the
1600 HARD_FRAME_POINTER then you lose the opportunity to
1601 make the frame assumptions. */
1602 if (regno == STACK_POINTER_REGNUM
1603 || regno == FRAME_POINTER_REGNUM
1604 || regno == HARD_FRAME_POINTER_REGNUM
1605 || regno == cfa_base_preserved_regno)
1606 return orig;
1608 bitmap_set_bit (evd->regs_active, regno);
1610 if (dump_file && (dump_flags & TDF_CSELIB))
1611 fprintf (dump_file, "expanding: r%d into: ", regno);
1613 result = expand_loc (l->elt->locs, evd, max_depth);
1614 bitmap_clear_bit (evd->regs_active, regno);
1616 if (result)
1617 return result;
1618 else
1619 return orig;
1623 CASE_CONST_ANY:
1624 case SYMBOL_REF:
1625 case CODE_LABEL:
1626 case PC:
1627 case CC0:
1628 case SCRATCH:
1629 /* SCRATCH must be shared because they represent distinct values. */
1630 return orig;
1631 case CLOBBER:
1632 if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1633 return orig;
1634 break;
1636 case CONST:
1637 if (shared_const_p (orig))
1638 return orig;
1639 break;
1641 case SUBREG:
1643 rtx subreg;
1645 if (evd->callback)
1647 subreg = evd->callback (orig, evd->regs_active, max_depth,
1648 evd->callback_arg);
1649 if (subreg != orig)
1650 return subreg;
1653 subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1654 max_depth - 1);
1655 if (!subreg)
1656 return NULL;
1657 scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1658 GET_MODE (SUBREG_REG (orig)),
1659 SUBREG_BYTE (orig));
1660 if (scopy == NULL
1661 || (GET_CODE (scopy) == SUBREG
1662 && !REG_P (SUBREG_REG (scopy))
1663 && !MEM_P (SUBREG_REG (scopy))))
1664 return NULL;
1666 return scopy;
1669 case VALUE:
1671 rtx result;
1673 if (dump_file && (dump_flags & TDF_CSELIB))
1675 fputs ("\nexpanding ", dump_file);
1676 print_rtl_single (dump_file, orig);
1677 fputs (" into...", dump_file);
1680 if (evd->callback)
1682 result = evd->callback (orig, evd->regs_active, max_depth,
1683 evd->callback_arg);
1685 if (result != orig)
1686 return result;
1689 result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
1690 return result;
1693 case DEBUG_EXPR:
1694 if (evd->callback)
1695 return evd->callback (orig, evd->regs_active, max_depth,
1696 evd->callback_arg);
1697 return orig;
1699 default:
1700 break;
1703 /* Copy the various flags, fields, and other information. We assume
1704 that all fields need copying, and then clear the fields that should
1705 not be copied. That is the sensible default behavior, and forces
1706 us to explicitly document why we are *not* copying a flag. */
1707 if (evd->dummy)
1708 copy = NULL;
1709 else
1710 copy = shallow_copy_rtx (orig);
1712 format_ptr = GET_RTX_FORMAT (code);
1714 for (i = 0; i < GET_RTX_LENGTH (code); i++)
1715 switch (*format_ptr++)
1717 case 'e':
1718 if (XEXP (orig, i) != NULL)
1720 rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1721 max_depth - 1);
1722 if (!result)
1723 return NULL;
1724 if (copy)
1725 XEXP (copy, i) = result;
1727 break;
1729 case 'E':
1730 case 'V':
1731 if (XVEC (orig, i) != NULL)
1733 if (copy)
1734 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1735 for (j = 0; j < XVECLEN (orig, i); j++)
1737 rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1738 evd, max_depth - 1);
1739 if (!result)
1740 return NULL;
1741 if (copy)
1742 XVECEXP (copy, i, j) = result;
1745 break;
1747 case 't':
1748 case 'w':
1749 case 'i':
1750 case 's':
1751 case 'S':
1752 case 'T':
1753 case 'u':
1754 case 'B':
1755 case '0':
1756 /* These are left unchanged. */
1757 break;
1759 default:
1760 gcc_unreachable ();
1763 if (evd->dummy)
1764 return orig;
1766 mode = GET_MODE (copy);
1767 /* If an operand has been simplified into CONST_INT, which doesn't
1768 have a mode and the mode isn't derivable from whole rtx's mode,
1769 try simplify_*_operation first with mode from original's operand
1770 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1771 scopy = copy;
1772 switch (GET_RTX_CLASS (code))
1774 case RTX_UNARY:
1775 if (CONST_INT_P (XEXP (copy, 0))
1776 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1778 scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1779 GET_MODE (XEXP (orig, 0)));
1780 if (scopy)
1781 return scopy;
1783 break;
1784 case RTX_COMM_ARITH:
1785 case RTX_BIN_ARITH:
1786 /* These expressions can derive operand modes from the whole rtx's mode. */
1787 break;
1788 case RTX_TERNARY:
1789 case RTX_BITFIELD_OPS:
1790 if (CONST_INT_P (XEXP (copy, 0))
1791 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1793 scopy = simplify_ternary_operation (code, mode,
1794 GET_MODE (XEXP (orig, 0)),
1795 XEXP (copy, 0), XEXP (copy, 1),
1796 XEXP (copy, 2));
1797 if (scopy)
1798 return scopy;
1800 break;
1801 case RTX_COMPARE:
1802 case RTX_COMM_COMPARE:
1803 if (CONST_INT_P (XEXP (copy, 0))
1804 && GET_MODE (XEXP (copy, 1)) == VOIDmode
1805 && (GET_MODE (XEXP (orig, 0)) != VOIDmode
1806 || GET_MODE (XEXP (orig, 1)) != VOIDmode))
1808 scopy = simplify_relational_operation (code, mode,
1809 (GET_MODE (XEXP (orig, 0))
1810 != VOIDmode)
1811 ? GET_MODE (XEXP (orig, 0))
1812 : GET_MODE (XEXP (orig, 1)),
1813 XEXP (copy, 0),
1814 XEXP (copy, 1));
1815 if (scopy)
1816 return scopy;
1818 break;
1819 default:
1820 break;
1822 scopy = simplify_rtx (copy);
1823 if (scopy)
1824 return scopy;
1825 return copy;
1828 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1829 with VALUE expressions. This way, it becomes independent of changes
1830 to registers and memory.
1831 X isn't actually modified; if modifications are needed, new rtl is
1832 allocated. However, the return value can share rtl with X.
1833 If X is within a MEM, MEMMODE must be the mode of the MEM. */
1836 cselib_subst_to_values (rtx x, machine_mode memmode)
1838 enum rtx_code code = GET_CODE (x);
1839 const char *fmt = GET_RTX_FORMAT (code);
1840 cselib_val *e;
1841 struct elt_list *l;
1842 rtx copy = x;
1843 int i;
1845 switch (code)
1847 case REG:
1848 l = REG_VALUES (REGNO (x));
1849 if (l && l->elt == NULL)
1850 l = l->next;
1851 for (; l; l = l->next)
1852 if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
1853 return l->elt->val_rtx;
1855 gcc_unreachable ();
1857 case MEM:
1858 e = cselib_lookup_mem (x, 0);
1859 /* This used to happen for autoincrements, but we deal with them
1860 properly now. Remove the if stmt for the next release. */
1861 if (! e)
1863 /* Assign a value that doesn't match any other. */
1864 e = new_cselib_val (next_uid, GET_MODE (x), x);
1866 return e->val_rtx;
1868 case ENTRY_VALUE:
1869 e = cselib_lookup (x, GET_MODE (x), 0, memmode);
1870 if (! e)
1871 break;
1872 return e->val_rtx;
1874 CASE_CONST_ANY:
1875 return x;
1877 case PRE_DEC:
1878 case PRE_INC:
1879 gcc_assert (memmode != VOIDmode);
1880 i = GET_MODE_SIZE (memmode);
1881 if (code == PRE_DEC)
1882 i = -i;
1883 return cselib_subst_to_values (plus_constant (GET_MODE (x),
1884 XEXP (x, 0), i),
1885 memmode);
1887 case PRE_MODIFY:
1888 gcc_assert (memmode != VOIDmode);
1889 return cselib_subst_to_values (XEXP (x, 1), memmode);
1891 case POST_DEC:
1892 case POST_INC:
1893 case POST_MODIFY:
1894 gcc_assert (memmode != VOIDmode);
1895 return cselib_subst_to_values (XEXP (x, 0), memmode);
1897 default:
1898 break;
1901 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1903 if (fmt[i] == 'e')
1905 rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
1907 if (t != XEXP (x, i))
1909 if (x == copy)
1910 copy = shallow_copy_rtx (x);
1911 XEXP (copy, i) = t;
1914 else if (fmt[i] == 'E')
1916 int j;
1918 for (j = 0; j < XVECLEN (x, i); j++)
1920 rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
1922 if (t != XVECEXP (x, i, j))
1924 if (XVEC (x, i) == XVEC (copy, i))
1926 if (x == copy)
1927 copy = shallow_copy_rtx (x);
1928 XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
1930 XVECEXP (copy, i, j) = t;
1936 return copy;
1939 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
1942 cselib_subst_to_values_from_insn (rtx x, machine_mode memmode, rtx_insn *insn)
1944 rtx ret;
1945 gcc_assert (!cselib_current_insn);
1946 cselib_current_insn = insn;
1947 ret = cselib_subst_to_values (x, memmode);
1948 cselib_current_insn = NULL;
1949 return ret;
1952 /* Look up the rtl expression X in our tables and return the value it
1953 has. If CREATE is zero, we return NULL if we don't know the value.
1954 Otherwise, we create a new one if possible, using mode MODE if X
1955 doesn't have a mode (i.e. because it's a constant). When X is part
1956 of an address, MEMMODE should be the mode of the enclosing MEM if
1957 we're tracking autoinc expressions. */
1959 static cselib_val *
1960 cselib_lookup_1 (rtx x, machine_mode mode,
1961 int create, machine_mode memmode)
1963 cselib_val **slot;
1964 cselib_val *e;
1965 unsigned int hashval;
1967 if (GET_MODE (x) != VOIDmode)
1968 mode = GET_MODE (x);
1970 if (GET_CODE (x) == VALUE)
1971 return CSELIB_VAL_PTR (x);
1973 if (REG_P (x))
1975 struct elt_list *l;
1976 unsigned int i = REGNO (x);
1978 l = REG_VALUES (i);
1979 if (l && l->elt == NULL)
1980 l = l->next;
1981 for (; l; l = l->next)
1982 if (mode == GET_MODE (l->elt->val_rtx))
1984 promote_debug_loc (l->elt->locs);
1985 return l->elt;
1988 if (! create)
1989 return 0;
1991 if (i < FIRST_PSEUDO_REGISTER)
1993 unsigned int n = hard_regno_nregs[i][mode];
1995 if (n > max_value_regs)
1996 max_value_regs = n;
1999 e = new_cselib_val (next_uid, GET_MODE (x), x);
2000 new_elt_loc_list (e, x);
2001 if (REG_VALUES (i) == 0)
2003 /* Maintain the invariant that the first entry of
2004 REG_VALUES, if present, must be the value used to set the
2005 register, or NULL. */
2006 used_regs[n_used_regs++] = i;
2007 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
2009 else if (cselib_preserve_constants
2010 && GET_MODE_CLASS (mode) == MODE_INT)
2012 /* During var-tracking, try harder to find equivalences
2013 for SUBREGs. If a setter sets say a DImode register
2014 and user uses that register only in SImode, add a lowpart
2015 subreg location. */
2016 struct elt_list *lwider = NULL;
2017 l = REG_VALUES (i);
2018 if (l && l->elt == NULL)
2019 l = l->next;
2020 for (; l; l = l->next)
2021 if (GET_MODE_CLASS (GET_MODE (l->elt->val_rtx)) == MODE_INT
2022 && GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2023 > GET_MODE_SIZE (mode)
2024 && (lwider == NULL
2025 || GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2026 < GET_MODE_SIZE (GET_MODE (lwider->elt->val_rtx))))
2028 struct elt_loc_list *el;
2029 if (i < FIRST_PSEUDO_REGISTER
2030 && hard_regno_nregs[i][GET_MODE (l->elt->val_rtx)] != 1)
2031 continue;
2032 for (el = l->elt->locs; el; el = el->next)
2033 if (!REG_P (el->loc))
2034 break;
2035 if (el)
2036 lwider = l;
2038 if (lwider)
2040 rtx sub = lowpart_subreg (mode, lwider->elt->val_rtx,
2041 GET_MODE (lwider->elt->val_rtx));
2042 if (sub)
2043 new_elt_loc_list (e, sub);
2046 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
2047 slot = cselib_find_slot (mode, x, e->hash, INSERT, memmode);
2048 *slot = e;
2049 return e;
2052 if (MEM_P (x))
2053 return cselib_lookup_mem (x, create);
2055 hashval = cselib_hash_rtx (x, create, memmode);
2056 /* Can't even create if hashing is not possible. */
2057 if (! hashval)
2058 return 0;
2060 slot = cselib_find_slot (mode, x, hashval,
2061 create ? INSERT : NO_INSERT, memmode);
2062 if (slot == 0)
2063 return 0;
2065 e = (cselib_val *) *slot;
2066 if (e)
2067 return e;
2069 e = new_cselib_val (hashval, mode, x);
2071 /* We have to fill the slot before calling cselib_subst_to_values:
2072 the hash table is inconsistent until we do so, and
2073 cselib_subst_to_values will need to do lookups. */
2074 *slot = e;
2075 new_elt_loc_list (e, cselib_subst_to_values (x, memmode));
2076 return e;
2079 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2081 cselib_val *
2082 cselib_lookup_from_insn (rtx x, machine_mode mode,
2083 int create, machine_mode memmode, rtx_insn *insn)
2085 cselib_val *ret;
2087 gcc_assert (!cselib_current_insn);
2088 cselib_current_insn = insn;
2090 ret = cselib_lookup (x, mode, create, memmode);
2092 cselib_current_insn = NULL;
2094 return ret;
2097 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2098 maintains invariants related with debug insns. */
2100 cselib_val *
2101 cselib_lookup (rtx x, machine_mode mode,
2102 int create, machine_mode memmode)
2104 cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
2106 /* ??? Should we return NULL if we're not to create an entry, the
2107 found loc is a debug loc and cselib_current_insn is not DEBUG?
2108 If so, we should also avoid converting val to non-DEBUG; probably
2109 easiest setting cselib_current_insn to NULL before the call
2110 above. */
2112 if (dump_file && (dump_flags & TDF_CSELIB))
2114 fputs ("cselib lookup ", dump_file);
2115 print_inline_rtx (dump_file, x, 2);
2116 fprintf (dump_file, " => %u:%u\n",
2117 ret ? ret->uid : 0,
2118 ret ? ret->hash : 0);
2121 return ret;
2124 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2125 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2126 is used to determine how many hard registers are being changed. If MODE
2127 is VOIDmode, then only REGNO is being changed; this is used when
2128 invalidating call clobbered registers across a call. */
2130 static void
2131 cselib_invalidate_regno (unsigned int regno, machine_mode mode)
2133 unsigned int endregno;
2134 unsigned int i;
2136 /* If we see pseudos after reload, something is _wrong_. */
2137 gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
2138 || reg_renumber[regno] < 0);
2140 /* Determine the range of registers that must be invalidated. For
2141 pseudos, only REGNO is affected. For hard regs, we must take MODE
2142 into account, and we must also invalidate lower register numbers
2143 if they contain values that overlap REGNO. */
2144 if (regno < FIRST_PSEUDO_REGISTER)
2146 gcc_assert (mode != VOIDmode);
2148 if (regno < max_value_regs)
2149 i = 0;
2150 else
2151 i = regno - max_value_regs;
2153 endregno = end_hard_regno (mode, regno);
2155 else
2157 i = regno;
2158 endregno = regno + 1;
2161 for (; i < endregno; i++)
2163 struct elt_list **l = &REG_VALUES (i);
2165 /* Go through all known values for this reg; if it overlaps the range
2166 we're invalidating, remove the value. */
2167 while (*l)
2169 cselib_val *v = (*l)->elt;
2170 bool had_locs;
2171 rtx_insn *setting_insn;
2172 struct elt_loc_list **p;
2173 unsigned int this_last = i;
2175 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
2176 this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
2178 if (this_last < regno || v == NULL
2179 || (v == cfa_base_preserved_val
2180 && i == cfa_base_preserved_regno))
2182 l = &(*l)->next;
2183 continue;
2186 /* We have an overlap. */
2187 if (*l == REG_VALUES (i))
2189 /* Maintain the invariant that the first entry of
2190 REG_VALUES, if present, must be the value used to set
2191 the register, or NULL. This is also nice because
2192 then we won't push the same regno onto user_regs
2193 multiple times. */
2194 (*l)->elt = NULL;
2195 l = &(*l)->next;
2197 else
2198 unchain_one_elt_list (l);
2200 v = canonical_cselib_val (v);
2202 had_locs = v->locs != NULL;
2203 setting_insn = v->locs ? v->locs->setting_insn : NULL;
2205 /* Now, we clear the mapping from value to reg. It must exist, so
2206 this code will crash intentionally if it doesn't. */
2207 for (p = &v->locs; ; p = &(*p)->next)
2209 rtx x = (*p)->loc;
2211 if (REG_P (x) && REGNO (x) == i)
2213 unchain_one_elt_loc_list (p);
2214 break;
2218 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2220 if (setting_insn && DEBUG_INSN_P (setting_insn))
2221 n_useless_debug_values++;
2222 else
2223 n_useless_values++;
2229 /* Invalidate any locations in the table which are changed because of a
2230 store to MEM_RTX. If this is called because of a non-const call
2231 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2233 static void
2234 cselib_invalidate_mem (rtx mem_rtx)
2236 cselib_val **vp, *v, *next;
2237 int num_mems = 0;
2238 rtx mem_addr;
2240 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2241 mem_rtx = canon_rtx (mem_rtx);
2243 vp = &first_containing_mem;
2244 for (v = *vp; v != &dummy_val; v = next)
2246 bool has_mem = false;
2247 struct elt_loc_list **p = &v->locs;
2248 bool had_locs = v->locs != NULL;
2249 rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
2251 while (*p)
2253 rtx x = (*p)->loc;
2254 cselib_val *addr;
2255 struct elt_list **mem_chain;
2257 /* MEMs may occur in locations only at the top level; below
2258 that every MEM or REG is substituted by its VALUE. */
2259 if (!MEM_P (x))
2261 p = &(*p)->next;
2262 continue;
2264 if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
2265 && ! canon_anti_dependence (x, false, mem_rtx,
2266 GET_MODE (mem_rtx), mem_addr))
2268 has_mem = true;
2269 num_mems++;
2270 p = &(*p)->next;
2271 continue;
2274 /* This one overlaps. */
2275 /* We must have a mapping from this MEM's address to the
2276 value (E). Remove that, too. */
2277 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
2278 addr = canonical_cselib_val (addr);
2279 gcc_checking_assert (v == canonical_cselib_val (v));
2280 mem_chain = &addr->addr_list;
2281 for (;;)
2283 cselib_val *canon = canonical_cselib_val ((*mem_chain)->elt);
2285 if (canon == v)
2287 unchain_one_elt_list (mem_chain);
2288 break;
2291 /* Record canonicalized elt. */
2292 (*mem_chain)->elt = canon;
2294 mem_chain = &(*mem_chain)->next;
2297 unchain_one_elt_loc_list (p);
2300 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2302 if (setting_insn && DEBUG_INSN_P (setting_insn))
2303 n_useless_debug_values++;
2304 else
2305 n_useless_values++;
2308 next = v->next_containing_mem;
2309 if (has_mem)
2311 *vp = v;
2312 vp = &(*vp)->next_containing_mem;
2314 else
2315 v->next_containing_mem = NULL;
2317 *vp = &dummy_val;
2320 /* Invalidate DEST, which is being assigned to or clobbered. */
2322 void
2323 cselib_invalidate_rtx (rtx dest)
2325 while (GET_CODE (dest) == SUBREG
2326 || GET_CODE (dest) == ZERO_EXTRACT
2327 || GET_CODE (dest) == STRICT_LOW_PART)
2328 dest = XEXP (dest, 0);
2330 if (REG_P (dest))
2331 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
2332 else if (MEM_P (dest))
2333 cselib_invalidate_mem (dest);
2336 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2338 static void
2339 cselib_invalidate_rtx_note_stores (rtx dest, const_rtx ignore ATTRIBUTE_UNUSED,
2340 void *data ATTRIBUTE_UNUSED)
2342 cselib_invalidate_rtx (dest);
2345 /* Record the result of a SET instruction. DEST is being set; the source
2346 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2347 describes its address. */
2349 static void
2350 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
2352 if (src_elt == 0 || side_effects_p (dest))
2353 return;
2355 if (REG_P (dest))
2357 unsigned int dreg = REGNO (dest);
2358 if (dreg < FIRST_PSEUDO_REGISTER)
2360 unsigned int n = REG_NREGS (dest);
2362 if (n > max_value_regs)
2363 max_value_regs = n;
2366 if (REG_VALUES (dreg) == 0)
2368 used_regs[n_used_regs++] = dreg;
2369 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2371 else
2373 /* The register should have been invalidated. */
2374 gcc_assert (REG_VALUES (dreg)->elt == 0);
2375 REG_VALUES (dreg)->elt = src_elt;
2378 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2379 n_useless_values--;
2380 new_elt_loc_list (src_elt, dest);
2382 else if (MEM_P (dest) && dest_addr_elt != 0
2383 && cselib_record_memory)
2385 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2386 n_useless_values--;
2387 add_mem_for_addr (dest_addr_elt, src_elt, dest);
2391 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2393 void
2394 cselib_add_permanent_equiv (cselib_val *elt, rtx x, rtx_insn *insn)
2396 cselib_val *nelt;
2397 rtx_insn *save_cselib_current_insn = cselib_current_insn;
2399 gcc_checking_assert (elt);
2400 gcc_checking_assert (PRESERVED_VALUE_P (elt->val_rtx));
2401 gcc_checking_assert (!side_effects_p (x));
2403 cselib_current_insn = insn;
2405 nelt = cselib_lookup (x, GET_MODE (elt->val_rtx), 1, VOIDmode);
2407 if (nelt != elt)
2409 cselib_any_perm_equivs = true;
2411 if (!PRESERVED_VALUE_P (nelt->val_rtx))
2412 cselib_preserve_value (nelt);
2414 new_elt_loc_list (nelt, elt->val_rtx);
2417 cselib_current_insn = save_cselib_current_insn;
2420 /* Return TRUE if any permanent equivalences have been recorded since
2421 the table was last initialized. */
2422 bool
2423 cselib_have_permanent_equivalences (void)
2425 return cselib_any_perm_equivs;
2428 /* There is no good way to determine how many elements there can be
2429 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2430 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2432 struct cselib_record_autoinc_data
2434 struct cselib_set *sets;
2435 int n_sets;
2438 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2439 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2441 static int
2442 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
2443 rtx dest, rtx src, rtx srcoff, void *arg)
2445 struct cselib_record_autoinc_data *data;
2446 data = (struct cselib_record_autoinc_data *)arg;
2448 data->sets[data->n_sets].dest = dest;
2450 if (srcoff)
2451 data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
2452 else
2453 data->sets[data->n_sets].src = src;
2455 data->n_sets++;
2457 return 0;
2460 /* Record the effects of any sets and autoincs in INSN. */
2461 static void
2462 cselib_record_sets (rtx_insn *insn)
2464 int n_sets = 0;
2465 int i;
2466 struct cselib_set sets[MAX_SETS];
2467 rtx body = PATTERN (insn);
2468 rtx cond = 0;
2469 int n_sets_before_autoinc;
2470 struct cselib_record_autoinc_data data;
2472 body = PATTERN (insn);
2473 if (GET_CODE (body) == COND_EXEC)
2475 cond = COND_EXEC_TEST (body);
2476 body = COND_EXEC_CODE (body);
2479 /* Find all sets. */
2480 if (GET_CODE (body) == SET)
2482 sets[0].src = SET_SRC (body);
2483 sets[0].dest = SET_DEST (body);
2484 n_sets = 1;
2486 else if (GET_CODE (body) == PARALLEL)
2488 /* Look through the PARALLEL and record the values being
2489 set, if possible. Also handle any CLOBBERs. */
2490 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2492 rtx x = XVECEXP (body, 0, i);
2494 if (GET_CODE (x) == SET)
2496 sets[n_sets].src = SET_SRC (x);
2497 sets[n_sets].dest = SET_DEST (x);
2498 n_sets++;
2503 if (n_sets == 1
2504 && MEM_P (sets[0].src)
2505 && !cselib_record_memory
2506 && MEM_READONLY_P (sets[0].src))
2508 rtx note = find_reg_equal_equiv_note (insn);
2510 if (note && CONSTANT_P (XEXP (note, 0)))
2511 sets[0].src = XEXP (note, 0);
2514 data.sets = sets;
2515 data.n_sets = n_sets_before_autoinc = n_sets;
2516 for_each_inc_dec (PATTERN (insn), cselib_record_autoinc_cb, &data);
2517 n_sets = data.n_sets;
2519 /* Look up the values that are read. Do this before invalidating the
2520 locations that are written. */
2521 for (i = 0; i < n_sets; i++)
2523 rtx dest = sets[i].dest;
2525 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2526 the low part after invalidating any knowledge about larger modes. */
2527 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2528 sets[i].dest = dest = XEXP (dest, 0);
2530 /* We don't know how to record anything but REG or MEM. */
2531 if (REG_P (dest)
2532 || (MEM_P (dest) && cselib_record_memory))
2534 rtx src = sets[i].src;
2535 if (cond)
2536 src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
2537 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
2538 if (MEM_P (dest))
2540 machine_mode address_mode = get_address_mode (dest);
2542 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
2543 address_mode, 1,
2544 GET_MODE (dest));
2546 else
2547 sets[i].dest_addr_elt = 0;
2551 if (cselib_record_sets_hook)
2552 cselib_record_sets_hook (insn, sets, n_sets);
2554 /* Invalidate all locations written by this insn. Note that the elts we
2555 looked up in the previous loop aren't affected, just some of their
2556 locations may go away. */
2557 note_stores (body, cselib_invalidate_rtx_note_stores, NULL);
2559 for (i = n_sets_before_autoinc; i < n_sets; i++)
2560 cselib_invalidate_rtx (sets[i].dest);
2562 /* If this is an asm, look for duplicate sets. This can happen when the
2563 user uses the same value as an output multiple times. This is valid
2564 if the outputs are not actually used thereafter. Treat this case as
2565 if the value isn't actually set. We do this by smashing the destination
2566 to pc_rtx, so that we won't record the value later. */
2567 if (n_sets >= 2 && asm_noperands (body) >= 0)
2569 for (i = 0; i < n_sets; i++)
2571 rtx dest = sets[i].dest;
2572 if (REG_P (dest) || MEM_P (dest))
2574 int j;
2575 for (j = i + 1; j < n_sets; j++)
2576 if (rtx_equal_p (dest, sets[j].dest))
2578 sets[i].dest = pc_rtx;
2579 sets[j].dest = pc_rtx;
2585 /* Now enter the equivalences in our tables. */
2586 for (i = 0; i < n_sets; i++)
2588 rtx dest = sets[i].dest;
2589 if (REG_P (dest)
2590 || (MEM_P (dest) && cselib_record_memory))
2591 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2595 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
2597 bool
2598 fp_setter_insn (rtx_insn *insn)
2600 rtx expr, pat = NULL_RTX;
2602 if (!RTX_FRAME_RELATED_P (insn))
2603 return false;
2605 expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2606 if (expr)
2607 pat = XEXP (expr, 0);
2608 if (!modified_in_p (hard_frame_pointer_rtx, pat ? pat : insn))
2609 return false;
2611 /* Don't return true for frame pointer restores in the epilogue. */
2612 if (find_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx))
2613 return false;
2614 return true;
2617 /* Record the effects of INSN. */
2619 void
2620 cselib_process_insn (rtx_insn *insn)
2622 int i;
2623 rtx x;
2625 cselib_current_insn = insn;
2627 /* Forget everything at a CODE_LABEL or a setjmp. */
2628 if ((LABEL_P (insn)
2629 || (CALL_P (insn)
2630 && find_reg_note (insn, REG_SETJMP, NULL)))
2631 && !cselib_preserve_constants)
2633 cselib_reset_table (next_uid);
2634 cselib_current_insn = NULL;
2635 return;
2638 if (! INSN_P (insn))
2640 cselib_current_insn = NULL;
2641 return;
2644 /* If this is a call instruction, forget anything stored in a
2645 call clobbered register, or, if this is not a const call, in
2646 memory. */
2647 if (CALL_P (insn))
2649 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2650 if (call_used_regs[i]
2651 || (REG_VALUES (i) && REG_VALUES (i)->elt
2652 && HARD_REGNO_CALL_PART_CLOBBERED (i,
2653 GET_MODE (REG_VALUES (i)->elt->val_rtx))))
2654 cselib_invalidate_regno (i, reg_raw_mode[i]);
2656 /* Since it is not clear how cselib is going to be used, be
2657 conservative here and treat looping pure or const functions
2658 as if they were regular functions. */
2659 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
2660 || !(RTL_CONST_OR_PURE_CALL_P (insn)))
2661 cselib_invalidate_mem (callmem);
2662 else
2663 /* For const/pure calls, invalidate any argument slots because
2664 they are owned by the callee. */
2665 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
2666 if (GET_CODE (XEXP (x, 0)) == USE
2667 && MEM_P (XEXP (XEXP (x, 0), 0)))
2668 cselib_invalidate_mem (XEXP (XEXP (x, 0), 0));
2671 cselib_record_sets (insn);
2673 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2674 after we have processed the insn. */
2675 if (CALL_P (insn))
2677 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
2678 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
2679 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
2680 /* Flush evertything on setjmp. */
2681 if (cselib_preserve_constants
2682 && find_reg_note (insn, REG_SETJMP, NULL))
2684 cselib_preserve_only_values ();
2685 cselib_reset_table (next_uid);
2689 /* On setter of the hard frame pointer if frame_pointer_needed,
2690 invalidate stack_pointer_rtx, so that sp and {,h}fp based
2691 VALUEs are distinct. */
2692 if (reload_completed
2693 && frame_pointer_needed
2694 && fp_setter_insn (insn))
2695 cselib_invalidate_rtx (stack_pointer_rtx);
2697 cselib_current_insn = NULL;
2699 if (n_useless_values > MAX_USELESS_VALUES
2700 /* remove_useless_values is linear in the hash table size. Avoid
2701 quadratic behavior for very large hashtables with very few
2702 useless elements. */
2703 && ((unsigned int)n_useless_values
2704 > (cselib_hash_table->elements () - n_debug_values) / 4))
2705 remove_useless_values ();
2708 /* Initialize cselib for one pass. The caller must also call
2709 init_alias_analysis. */
2711 void
2712 cselib_init (int record_what)
2714 cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
2715 cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
2716 cselib_any_perm_equivs = false;
2718 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2719 see canon_true_dependence. This is only created once. */
2720 if (! callmem)
2721 callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2723 cselib_nregs = max_reg_num ();
2725 /* We preserve reg_values to allow expensive clearing of the whole thing.
2726 Reallocate it however if it happens to be too large. */
2727 if (!reg_values || reg_values_size < cselib_nregs
2728 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
2730 free (reg_values);
2731 /* Some space for newly emit instructions so we don't end up
2732 reallocating in between passes. */
2733 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
2734 reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
2736 used_regs = XNEWVEC (unsigned int, cselib_nregs);
2737 n_used_regs = 0;
2738 cselib_hash_table = new hash_table<cselib_hasher> (31);
2739 if (cselib_preserve_constants)
2740 cselib_preserved_hash_table = new hash_table<cselib_hasher> (31);
2741 next_uid = 1;
2744 /* Called when the current user is done with cselib. */
2746 void
2747 cselib_finish (void)
2749 bool preserved = cselib_preserve_constants;
2750 cselib_discard_hook = NULL;
2751 cselib_preserve_constants = false;
2752 cselib_any_perm_equivs = false;
2753 cfa_base_preserved_val = NULL;
2754 cfa_base_preserved_regno = INVALID_REGNUM;
2755 elt_list_pool.release ();
2756 elt_loc_list_pool.release ();
2757 cselib_val_pool.release ();
2758 value_pool.release ();
2759 cselib_clear_table ();
2760 delete cselib_hash_table;
2761 cselib_hash_table = NULL;
2762 if (preserved)
2763 delete cselib_preserved_hash_table;
2764 cselib_preserved_hash_table = NULL;
2765 free (used_regs);
2766 used_regs = 0;
2767 n_useless_values = 0;
2768 n_useless_debug_values = 0;
2769 n_debug_values = 0;
2770 next_uid = 0;
2773 /* Dump the cselib_val *X to FILE *OUT. */
2776 dump_cselib_val (cselib_val **x, FILE *out)
2778 cselib_val *v = *x;
2779 bool need_lf = true;
2781 print_inline_rtx (out, v->val_rtx, 0);
2783 if (v->locs)
2785 struct elt_loc_list *l = v->locs;
2786 if (need_lf)
2788 fputc ('\n', out);
2789 need_lf = false;
2791 fputs (" locs:", out);
2794 if (l->setting_insn)
2795 fprintf (out, "\n from insn %i ",
2796 INSN_UID (l->setting_insn));
2797 else
2798 fprintf (out, "\n ");
2799 print_inline_rtx (out, l->loc, 4);
2801 while ((l = l->next));
2802 fputc ('\n', out);
2804 else
2806 fputs (" no locs", out);
2807 need_lf = true;
2810 if (v->addr_list)
2812 struct elt_list *e = v->addr_list;
2813 if (need_lf)
2815 fputc ('\n', out);
2816 need_lf = false;
2818 fputs (" addr list:", out);
2821 fputs ("\n ", out);
2822 print_inline_rtx (out, e->elt->val_rtx, 2);
2824 while ((e = e->next));
2825 fputc ('\n', out);
2827 else
2829 fputs (" no addrs", out);
2830 need_lf = true;
2833 if (v->next_containing_mem == &dummy_val)
2834 fputs (" last mem\n", out);
2835 else if (v->next_containing_mem)
2837 fputs (" next mem ", out);
2838 print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
2839 fputc ('\n', out);
2841 else if (need_lf)
2842 fputc ('\n', out);
2844 return 1;
2847 /* Dump to OUT everything in the CSELIB table. */
2849 void
2850 dump_cselib_table (FILE *out)
2852 fprintf (out, "cselib hash table:\n");
2853 cselib_hash_table->traverse <FILE *, dump_cselib_val> (out);
2854 fprintf (out, "cselib preserved hash table:\n");
2855 cselib_preserved_hash_table->traverse <FILE *, dump_cselib_val> (out);
2856 if (first_containing_mem != &dummy_val)
2858 fputs ("first mem ", out);
2859 print_inline_rtx (out, first_containing_mem->val_rtx, 2);
2860 fputc ('\n', out);
2862 fprintf (out, "next uid %i\n", next_uid);
2865 #include "gt-cselib.h"