Fix typo/copy'n'pasto.
[official-gcc.git] / gcc / cselib.c
blob0fcfe285eb515373f35e4da68d552936e2604dfe
1 /* Common subexpression elimination library for GNU compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
25 #include "rtl.h"
26 #include "tree.h"/* FIXME: For hashing DEBUG_EXPR & friends. */
27 #include "tm_p.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "flags.h"
31 #include "insn-config.h"
32 #include "recog.h"
33 #include "function.h"
34 #include "emit-rtl.h"
35 #include "diagnostic-core.h"
36 #include "ggc.h"
37 #include "hash-table.h"
38 #include "dumpfile.h"
39 #include "cselib.h"
40 #include "valtrack.h"
41 #include "params.h"
42 #include "alloc-pool.h"
43 #include "target.h"
44 #include "bitmap.h"
46 /* A list of cselib_val structures. */
47 struct elt_list {
48 struct elt_list *next;
49 cselib_val *elt;
52 /* See the documentation of cselib_find_slot below. */
53 static enum machine_mode find_slot_memmode;
55 static bool cselib_record_memory;
56 static bool cselib_preserve_constants;
57 static bool cselib_any_perm_equivs;
58 static inline void promote_debug_loc (struct elt_loc_list *l);
59 static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
60 static void new_elt_loc_list (cselib_val *, rtx);
61 static void unchain_one_value (cselib_val *);
62 static void unchain_one_elt_list (struct elt_list **);
63 static void unchain_one_elt_loc_list (struct elt_loc_list **);
64 static void remove_useless_values (void);
65 static int rtx_equal_for_cselib_1 (rtx, rtx, enum machine_mode);
66 static unsigned int cselib_hash_rtx (rtx, int, enum machine_mode);
67 static cselib_val *new_cselib_val (unsigned int, enum machine_mode, rtx);
68 static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
69 static cselib_val *cselib_lookup_mem (rtx, int);
70 static void cselib_invalidate_regno (unsigned int, enum machine_mode);
71 static void cselib_invalidate_mem (rtx);
72 static void cselib_record_set (rtx, cselib_val *, cselib_val *);
73 static void cselib_record_sets (rtx);
75 struct expand_value_data
77 bitmap regs_active;
78 cselib_expand_callback callback;
79 void *callback_arg;
80 bool dummy;
83 static rtx cselib_expand_value_rtx_1 (rtx, struct expand_value_data *, int);
85 /* There are three ways in which cselib can look up an rtx:
86 - for a REG, the reg_values table (which is indexed by regno) is used
87 - for a MEM, we recursively look up its address and then follow the
88 addr_list of that value
89 - for everything else, we compute a hash value and go through the hash
90 table. Since different rtx's can still have the same hash value,
91 this involves walking the table entries for a given value and comparing
92 the locations of the entries with the rtx we are looking up. */
94 struct cselib_hasher : typed_noop_remove <cselib_val>
96 typedef cselib_val value_type;
97 typedef rtx_def compare_type;
98 static inline hashval_t hash (const value_type *);
99 static inline bool equal (const value_type *, const compare_type *);
102 /* The hash function for our hash table. The value is always computed with
103 cselib_hash_rtx when adding an element; this function just extracts the
104 hash value from a cselib_val structure. */
106 inline hashval_t
107 cselib_hasher::hash (const value_type *v)
109 return v->hash;
112 /* The equality test for our hash table. The first argument V is a table
113 element (i.e. a cselib_val), while the second arg X is an rtx. We know
114 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
115 CONST of an appropriate mode. */
117 inline bool
118 cselib_hasher::equal (const value_type *v, const compare_type *x_arg)
120 struct elt_loc_list *l;
121 rtx x = CONST_CAST_RTX (x_arg);
122 enum machine_mode mode = GET_MODE (x);
124 gcc_assert (!CONST_SCALAR_INT_P (x) && GET_CODE (x) != CONST_FIXED);
126 if (mode != GET_MODE (v->val_rtx))
127 return false;
129 /* Unwrap X if necessary. */
130 if (GET_CODE (x) == CONST
131 && (CONST_SCALAR_INT_P (XEXP (x, 0))
132 || GET_CODE (XEXP (x, 0)) == CONST_FIXED))
133 x = XEXP (x, 0);
135 if (GET_CODE (x) == VALUE)
136 return x == v->val_rtx;
138 /* We don't guarantee that distinct rtx's have different hash values,
139 so we need to do a comparison. */
140 for (l = v->locs; l; l = l->next)
141 if (rtx_equal_for_cselib_1 (l->loc, x, find_slot_memmode))
143 promote_debug_loc (l);
144 return true;
147 return false;
150 /* A table that enables us to look up elts by their value. */
151 static hash_table <cselib_hasher> cselib_hash_table;
153 /* A table to hold preserved values. */
154 static hash_table <cselib_hasher> cselib_preserved_hash_table;
156 /* This is a global so we don't have to pass this through every function.
157 It is used in new_elt_loc_list to set SETTING_INSN. */
158 static rtx cselib_current_insn;
160 /* The unique id that the next create value will take. */
161 static unsigned int next_uid;
163 /* The number of registers we had when the varrays were last resized. */
164 static unsigned int cselib_nregs;
166 /* Count values without known locations, or with only locations that
167 wouldn't have been known except for debug insns. Whenever this
168 grows too big, we remove these useless values from the table.
170 Counting values with only debug values is a bit tricky. We don't
171 want to increment n_useless_values when we create a value for a
172 debug insn, for this would get n_useless_values out of sync, but we
173 want increment it if all locs in the list that were ever referenced
174 in nondebug insns are removed from the list.
176 In the general case, once we do that, we'd have to stop accepting
177 nondebug expressions in the loc list, to avoid having two values
178 equivalent that, without debug insns, would have been made into
179 separate values. However, because debug insns never introduce
180 equivalences themselves (no assignments), the only means for
181 growing loc lists is through nondebug assignments. If the locs
182 also happen to be referenced in debug insns, it will work just fine.
184 A consequence of this is that there's at most one debug-only loc in
185 each loc list. If we keep it in the first entry, testing whether
186 we have a debug-only loc list takes O(1).
188 Furthermore, since any additional entry in a loc list containing a
189 debug loc would have to come from an assignment (nondebug) that
190 references both the initial debug loc and the newly-equivalent loc,
191 the initial debug loc would be promoted to a nondebug loc, and the
192 loc list would not contain debug locs any more.
194 So the only case we have to be careful with in order to keep
195 n_useless_values in sync between debug and nondebug compilations is
196 to avoid incrementing n_useless_values when removing the single loc
197 from a value that turns out to not appear outside debug values. We
198 increment n_useless_debug_values instead, and leave such values
199 alone until, for other reasons, we garbage-collect useless
200 values. */
201 static int n_useless_values;
202 static int n_useless_debug_values;
204 /* Count values whose locs have been taken exclusively from debug
205 insns for the entire life of the value. */
206 static int n_debug_values;
208 /* Number of useless values before we remove them from the hash table. */
209 #define MAX_USELESS_VALUES 32
211 /* This table maps from register number to values. It does not
212 contain pointers to cselib_val structures, but rather elt_lists.
213 The purpose is to be able to refer to the same register in
214 different modes. The first element of the list defines the mode in
215 which the register was set; if the mode is unknown or the value is
216 no longer valid in that mode, ELT will be NULL for the first
217 element. */
218 static struct elt_list **reg_values;
219 static unsigned int reg_values_size;
220 #define REG_VALUES(i) reg_values[i]
222 /* The largest number of hard regs used by any entry added to the
223 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
224 static unsigned int max_value_regs;
226 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
227 in cselib_clear_table() for fast emptying. */
228 static unsigned int *used_regs;
229 static unsigned int n_used_regs;
231 /* We pass this to cselib_invalidate_mem to invalidate all of
232 memory for a non-const call instruction. */
233 static GTY(()) rtx callmem;
235 /* Set by discard_useless_locs if it deleted the last location of any
236 value. */
237 static int values_became_useless;
239 /* Used as stop element of the containing_mem list so we can check
240 presence in the list by checking the next pointer. */
241 static cselib_val dummy_val;
243 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
244 that is constant through the whole function and should never be
245 eliminated. */
246 static cselib_val *cfa_base_preserved_val;
247 static unsigned int cfa_base_preserved_regno = INVALID_REGNUM;
249 /* Used to list all values that contain memory reference.
250 May or may not contain the useless values - the list is compacted
251 each time memory is invalidated. */
252 static cselib_val *first_containing_mem = &dummy_val;
253 static alloc_pool elt_loc_list_pool, elt_list_pool, cselib_val_pool, value_pool;
255 /* If nonnull, cselib will call this function before freeing useless
256 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
257 void (*cselib_discard_hook) (cselib_val *);
259 /* If nonnull, cselib will call this function before recording sets or
260 even clobbering outputs of INSN. All the recorded sets will be
261 represented in the array sets[n_sets]. new_val_min can be used to
262 tell whether values present in sets are introduced by this
263 instruction. */
264 void (*cselib_record_sets_hook) (rtx insn, struct cselib_set *sets,
265 int n_sets);
267 #define PRESERVED_VALUE_P(RTX) \
268 (RTL_FLAG_CHECK1 ("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
270 #define SP_BASED_VALUE_P(RTX) \
271 (RTL_FLAG_CHECK1 ("SP_BASED_VALUE_P", (RTX), VALUE)->jump)
275 /* Allocate a struct elt_list and fill in its two elements with the
276 arguments. */
278 static inline struct elt_list *
279 new_elt_list (struct elt_list *next, cselib_val *elt)
281 struct elt_list *el;
282 el = (struct elt_list *) pool_alloc (elt_list_pool);
283 el->next = next;
284 el->elt = elt;
285 return el;
288 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
289 list. */
291 static inline void
292 new_elt_loc_list (cselib_val *val, rtx loc)
294 struct elt_loc_list *el, *next = val->locs;
296 gcc_checking_assert (!next || !next->setting_insn
297 || !DEBUG_INSN_P (next->setting_insn)
298 || cselib_current_insn == next->setting_insn);
300 /* If we're creating the first loc in a debug insn context, we've
301 just created a debug value. Count it. */
302 if (!next && cselib_current_insn && DEBUG_INSN_P (cselib_current_insn))
303 n_debug_values++;
305 val = canonical_cselib_val (val);
306 next = val->locs;
308 if (GET_CODE (loc) == VALUE)
310 loc = canonical_cselib_val (CSELIB_VAL_PTR (loc))->val_rtx;
312 gcc_checking_assert (PRESERVED_VALUE_P (loc)
313 == PRESERVED_VALUE_P (val->val_rtx));
315 if (val->val_rtx == loc)
316 return;
317 else if (val->uid > CSELIB_VAL_PTR (loc)->uid)
319 /* Reverse the insertion. */
320 new_elt_loc_list (CSELIB_VAL_PTR (loc), val->val_rtx);
321 return;
324 gcc_checking_assert (val->uid < CSELIB_VAL_PTR (loc)->uid);
326 if (CSELIB_VAL_PTR (loc)->locs)
328 /* Bring all locs from LOC to VAL. */
329 for (el = CSELIB_VAL_PTR (loc)->locs; el->next; el = el->next)
331 /* Adjust values that have LOC as canonical so that VAL
332 becomes their canonical. */
333 if (el->loc && GET_CODE (el->loc) == VALUE)
335 gcc_checking_assert (CSELIB_VAL_PTR (el->loc)->locs->loc
336 == loc);
337 CSELIB_VAL_PTR (el->loc)->locs->loc = val->val_rtx;
340 el->next = val->locs;
341 next = val->locs = CSELIB_VAL_PTR (loc)->locs;
344 if (CSELIB_VAL_PTR (loc)->addr_list)
346 /* Bring in addr_list into canonical node. */
347 struct elt_list *last = CSELIB_VAL_PTR (loc)->addr_list;
348 while (last->next)
349 last = last->next;
350 last->next = val->addr_list;
351 val->addr_list = CSELIB_VAL_PTR (loc)->addr_list;
352 CSELIB_VAL_PTR (loc)->addr_list = NULL;
355 if (CSELIB_VAL_PTR (loc)->next_containing_mem != NULL
356 && val->next_containing_mem == NULL)
358 /* Add VAL to the containing_mem list after LOC. LOC will
359 be removed when we notice it doesn't contain any
360 MEMs. */
361 val->next_containing_mem = CSELIB_VAL_PTR (loc)->next_containing_mem;
362 CSELIB_VAL_PTR (loc)->next_containing_mem = val;
365 /* Chain LOC back to VAL. */
366 el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
367 el->loc = val->val_rtx;
368 el->setting_insn = cselib_current_insn;
369 el->next = NULL;
370 CSELIB_VAL_PTR (loc)->locs = el;
373 el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
374 el->loc = loc;
375 el->setting_insn = cselib_current_insn;
376 el->next = next;
377 val->locs = el;
380 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
381 originating from a debug insn, maintaining the debug values
382 count. */
384 static inline void
385 promote_debug_loc (struct elt_loc_list *l)
387 if (l && l->setting_insn && DEBUG_INSN_P (l->setting_insn)
388 && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
390 n_debug_values--;
391 l->setting_insn = cselib_current_insn;
392 if (cselib_preserve_constants && l->next)
394 gcc_assert (l->next->setting_insn
395 && DEBUG_INSN_P (l->next->setting_insn)
396 && !l->next->next);
397 l->next->setting_insn = cselib_current_insn;
399 else
400 gcc_assert (!l->next);
404 /* The elt_list at *PL is no longer needed. Unchain it and free its
405 storage. */
407 static inline void
408 unchain_one_elt_list (struct elt_list **pl)
410 struct elt_list *l = *pl;
412 *pl = l->next;
413 pool_free (elt_list_pool, l);
416 /* Likewise for elt_loc_lists. */
418 static void
419 unchain_one_elt_loc_list (struct elt_loc_list **pl)
421 struct elt_loc_list *l = *pl;
423 *pl = l->next;
424 pool_free (elt_loc_list_pool, l);
427 /* Likewise for cselib_vals. This also frees the addr_list associated with
428 V. */
430 static void
431 unchain_one_value (cselib_val *v)
433 while (v->addr_list)
434 unchain_one_elt_list (&v->addr_list);
436 pool_free (cselib_val_pool, v);
439 /* Remove all entries from the hash table. Also used during
440 initialization. */
442 void
443 cselib_clear_table (void)
445 cselib_reset_table (1);
448 /* Return TRUE if V is a constant, a function invariant or a VALUE
449 equivalence; FALSE otherwise. */
451 static bool
452 invariant_or_equiv_p (cselib_val *v)
454 struct elt_loc_list *l;
456 if (v == cfa_base_preserved_val)
457 return true;
459 /* Keep VALUE equivalences around. */
460 for (l = v->locs; l; l = l->next)
461 if (GET_CODE (l->loc) == VALUE)
462 return true;
464 if (v->locs != NULL
465 && v->locs->next == NULL)
467 if (CONSTANT_P (v->locs->loc)
468 && (GET_CODE (v->locs->loc) != CONST
469 || !references_value_p (v->locs->loc, 0)))
470 return true;
471 /* Although a debug expr may be bound to different expressions,
472 we can preserve it as if it was constant, to get unification
473 and proper merging within var-tracking. */
474 if (GET_CODE (v->locs->loc) == DEBUG_EXPR
475 || GET_CODE (v->locs->loc) == DEBUG_IMPLICIT_PTR
476 || GET_CODE (v->locs->loc) == ENTRY_VALUE
477 || GET_CODE (v->locs->loc) == DEBUG_PARAMETER_REF)
478 return true;
480 /* (plus (value V) (const_int C)) is invariant iff V is invariant. */
481 if (GET_CODE (v->locs->loc) == PLUS
482 && CONST_INT_P (XEXP (v->locs->loc, 1))
483 && GET_CODE (XEXP (v->locs->loc, 0)) == VALUE
484 && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v->locs->loc, 0))))
485 return true;
488 return false;
491 /* Remove from hash table all VALUEs except constants, function
492 invariants and VALUE equivalences. */
495 preserve_constants_and_equivs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
497 cselib_val *v = *x;
499 if (invariant_or_equiv_p (v))
501 cselib_val **slot
502 = cselib_preserved_hash_table.find_slot_with_hash (v->val_rtx,
503 v->hash, INSERT);
504 gcc_assert (!*slot);
505 *slot = v;
508 cselib_hash_table.clear_slot (x);
510 return 1;
513 /* Remove all entries from the hash table, arranging for the next
514 value to be numbered NUM. */
516 void
517 cselib_reset_table (unsigned int num)
519 unsigned int i;
521 max_value_regs = 0;
523 if (cfa_base_preserved_val)
525 unsigned int regno = cfa_base_preserved_regno;
526 unsigned int new_used_regs = 0;
527 for (i = 0; i < n_used_regs; i++)
528 if (used_regs[i] == regno)
530 new_used_regs = 1;
531 continue;
533 else
534 REG_VALUES (used_regs[i]) = 0;
535 gcc_assert (new_used_regs == 1);
536 n_used_regs = new_used_regs;
537 used_regs[0] = regno;
538 max_value_regs
539 = hard_regno_nregs[regno][GET_MODE (cfa_base_preserved_val->locs->loc)];
541 else
543 for (i = 0; i < n_used_regs; i++)
544 REG_VALUES (used_regs[i]) = 0;
545 n_used_regs = 0;
548 if (cselib_preserve_constants)
549 cselib_hash_table.traverse <void *, preserve_constants_and_equivs> (NULL);
550 else
552 cselib_hash_table.empty ();
553 gcc_checking_assert (!cselib_any_perm_equivs);
556 n_useless_values = 0;
557 n_useless_debug_values = 0;
558 n_debug_values = 0;
560 next_uid = num;
562 first_containing_mem = &dummy_val;
565 /* Return the number of the next value that will be generated. */
567 unsigned int
568 cselib_get_next_uid (void)
570 return next_uid;
573 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
574 INSERTing if requested. When X is part of the address of a MEM,
575 MEMMODE should specify the mode of the MEM. While searching the
576 table, MEMMODE is held in FIND_SLOT_MEMMODE, so that autoinc RTXs
577 in X can be resolved. */
579 static cselib_val **
580 cselib_find_slot (rtx x, hashval_t hash, enum insert_option insert,
581 enum machine_mode memmode)
583 cselib_val **slot = NULL;
584 find_slot_memmode = memmode;
585 if (cselib_preserve_constants)
586 slot = cselib_preserved_hash_table.find_slot_with_hash (x, hash,
587 NO_INSERT);
588 if (!slot)
589 slot = cselib_hash_table.find_slot_with_hash (x, hash, insert);
590 find_slot_memmode = VOIDmode;
591 return slot;
594 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
595 only return true for values which point to a cselib_val whose value
596 element has been set to zero, which implies the cselib_val will be
597 removed. */
600 references_value_p (const_rtx x, int only_useless)
602 const enum rtx_code code = GET_CODE (x);
603 const char *fmt = GET_RTX_FORMAT (code);
604 int i, j;
606 if (GET_CODE (x) == VALUE
607 && (! only_useless ||
608 (CSELIB_VAL_PTR (x)->locs == 0 && !PRESERVED_VALUE_P (x))))
609 return 1;
611 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
613 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
614 return 1;
615 else if (fmt[i] == 'E')
616 for (j = 0; j < XVECLEN (x, i); j++)
617 if (references_value_p (XVECEXP (x, i, j), only_useless))
618 return 1;
621 return 0;
624 /* For all locations found in X, delete locations that reference useless
625 values (i.e. values without any location). Called through
626 htab_traverse. */
629 discard_useless_locs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
631 cselib_val *v = *x;
632 struct elt_loc_list **p = &v->locs;
633 bool had_locs = v->locs != NULL;
634 rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
636 while (*p)
638 if (references_value_p ((*p)->loc, 1))
639 unchain_one_elt_loc_list (p);
640 else
641 p = &(*p)->next;
644 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
646 if (setting_insn && DEBUG_INSN_P (setting_insn))
647 n_useless_debug_values++;
648 else
649 n_useless_values++;
650 values_became_useless = 1;
652 return 1;
655 /* If X is a value with no locations, remove it from the hashtable. */
658 discard_useless_values (cselib_val **x, void *info ATTRIBUTE_UNUSED)
660 cselib_val *v = *x;
662 if (v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
664 if (cselib_discard_hook)
665 cselib_discard_hook (v);
667 CSELIB_VAL_PTR (v->val_rtx) = NULL;
668 cselib_hash_table.clear_slot (x);
669 unchain_one_value (v);
670 n_useless_values--;
673 return 1;
676 /* Clean out useless values (i.e. those which no longer have locations
677 associated with them) from the hash table. */
679 static void
680 remove_useless_values (void)
682 cselib_val **p, *v;
684 if (n_useless_values <= MAX_USELESS_VALUES
685 /* remove_useless_values is linear in the hash table size. Avoid
686 quadratic behavior for very large hashtables with very few
687 useless elements. */
688 || ((unsigned int)n_useless_values
689 <= (cselib_hash_table.elements () - n_debug_values) / 4))
690 return;
692 /* First pass: eliminate locations that reference the value. That in
693 turn can make more values useless. */
696 values_became_useless = 0;
697 cselib_hash_table.traverse <void *, discard_useless_locs> (NULL);
699 while (values_became_useless);
701 /* Second pass: actually remove the values. */
703 p = &first_containing_mem;
704 for (v = *p; v != &dummy_val; v = v->next_containing_mem)
705 if (v->locs && v == canonical_cselib_val (v))
707 *p = v;
708 p = &(*p)->next_containing_mem;
710 *p = &dummy_val;
712 n_useless_values += n_useless_debug_values;
713 n_debug_values -= n_useless_debug_values;
714 n_useless_debug_values = 0;
716 cselib_hash_table.traverse <void *, discard_useless_values> (NULL);
718 gcc_assert (!n_useless_values);
721 /* Arrange for a value to not be removed from the hash table even if
722 it becomes useless. */
724 void
725 cselib_preserve_value (cselib_val *v)
727 PRESERVED_VALUE_P (v->val_rtx) = 1;
730 /* Test whether a value is preserved. */
732 bool
733 cselib_preserved_value_p (cselib_val *v)
735 return PRESERVED_VALUE_P (v->val_rtx);
738 /* Arrange for a REG value to be assumed constant through the whole function,
739 never invalidated and preserved across cselib_reset_table calls. */
741 void
742 cselib_preserve_cfa_base_value (cselib_val *v, unsigned int regno)
744 if (cselib_preserve_constants
745 && v->locs
746 && REG_P (v->locs->loc))
748 cfa_base_preserved_val = v;
749 cfa_base_preserved_regno = regno;
753 /* Clean all non-constant expressions in the hash table, but retain
754 their values. */
756 void
757 cselib_preserve_only_values (void)
759 int i;
761 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
762 cselib_invalidate_regno (i, reg_raw_mode[i]);
764 cselib_invalidate_mem (callmem);
766 remove_useless_values ();
768 gcc_assert (first_containing_mem == &dummy_val);
771 /* Arrange for a value to be marked as based on stack pointer
772 for find_base_term purposes. */
774 void
775 cselib_set_value_sp_based (cselib_val *v)
777 SP_BASED_VALUE_P (v->val_rtx) = 1;
780 /* Test whether a value is based on stack pointer for
781 find_base_term purposes. */
783 bool
784 cselib_sp_based_value_p (cselib_val *v)
786 return SP_BASED_VALUE_P (v->val_rtx);
789 /* Return the mode in which a register was last set. If X is not a
790 register, return its mode. If the mode in which the register was
791 set is not known, or the value was already clobbered, return
792 VOIDmode. */
794 enum machine_mode
795 cselib_reg_set_mode (const_rtx x)
797 if (!REG_P (x))
798 return GET_MODE (x);
800 if (REG_VALUES (REGNO (x)) == NULL
801 || REG_VALUES (REGNO (x))->elt == NULL)
802 return VOIDmode;
804 return GET_MODE (REG_VALUES (REGNO (x))->elt->val_rtx);
807 /* Return nonzero if we can prove that X and Y contain the same value, taking
808 our gathered information into account. */
811 rtx_equal_for_cselib_p (rtx x, rtx y)
813 return rtx_equal_for_cselib_1 (x, y, VOIDmode);
816 /* If x is a PLUS or an autoinc operation, expand the operation,
817 storing the offset, if any, in *OFF. */
819 static rtx
820 autoinc_split (rtx x, rtx *off, enum machine_mode memmode)
822 switch (GET_CODE (x))
824 case PLUS:
825 *off = XEXP (x, 1);
826 return XEXP (x, 0);
828 case PRE_DEC:
829 if (memmode == VOIDmode)
830 return x;
832 *off = GEN_INT (-GET_MODE_SIZE (memmode));
833 return XEXP (x, 0);
834 break;
836 case PRE_INC:
837 if (memmode == VOIDmode)
838 return x;
840 *off = GEN_INT (GET_MODE_SIZE (memmode));
841 return XEXP (x, 0);
843 case PRE_MODIFY:
844 return XEXP (x, 1);
846 case POST_DEC:
847 case POST_INC:
848 case POST_MODIFY:
849 return XEXP (x, 0);
851 default:
852 return x;
856 /* Return nonzero if we can prove that X and Y contain the same value,
857 taking our gathered information into account. MEMMODE holds the
858 mode of the enclosing MEM, if any, as required to deal with autoinc
859 addressing modes. If X and Y are not (known to be) part of
860 addresses, MEMMODE should be VOIDmode. */
862 static int
863 rtx_equal_for_cselib_1 (rtx x, rtx y, enum machine_mode memmode)
865 enum rtx_code code;
866 const char *fmt;
867 int i;
869 if (REG_P (x) || MEM_P (x))
871 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0, memmode);
873 if (e)
874 x = e->val_rtx;
877 if (REG_P (y) || MEM_P (y))
879 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0, memmode);
881 if (e)
882 y = e->val_rtx;
885 if (x == y)
886 return 1;
888 if (GET_CODE (x) == VALUE)
890 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (x));
891 struct elt_loc_list *l;
893 if (GET_CODE (y) == VALUE)
894 return e == canonical_cselib_val (CSELIB_VAL_PTR (y));
896 for (l = e->locs; l; l = l->next)
898 rtx t = l->loc;
900 /* Avoid infinite recursion. We know we have the canonical
901 value, so we can just skip any values in the equivalence
902 list. */
903 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
904 continue;
905 else if (rtx_equal_for_cselib_1 (t, y, memmode))
906 return 1;
909 return 0;
911 else if (GET_CODE (y) == VALUE)
913 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (y));
914 struct elt_loc_list *l;
916 for (l = e->locs; l; l = l->next)
918 rtx t = l->loc;
920 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
921 continue;
922 else if (rtx_equal_for_cselib_1 (x, t, memmode))
923 return 1;
926 return 0;
929 if (GET_MODE (x) != GET_MODE (y))
930 return 0;
932 if (GET_CODE (x) != GET_CODE (y))
934 rtx xorig = x, yorig = y;
935 rtx xoff = NULL, yoff = NULL;
937 x = autoinc_split (x, &xoff, memmode);
938 y = autoinc_split (y, &yoff, memmode);
940 if (!xoff != !yoff)
941 return 0;
943 if (xoff && !rtx_equal_for_cselib_1 (xoff, yoff, memmode))
944 return 0;
946 /* Don't recurse if nothing changed. */
947 if (x != xorig || y != yorig)
948 return rtx_equal_for_cselib_1 (x, y, memmode);
950 return 0;
953 /* These won't be handled correctly by the code below. */
954 switch (GET_CODE (x))
956 case CONST_DOUBLE:
957 case CONST_FIXED:
958 case DEBUG_EXPR:
959 return 0;
961 case DEBUG_IMPLICIT_PTR:
962 return DEBUG_IMPLICIT_PTR_DECL (x)
963 == DEBUG_IMPLICIT_PTR_DECL (y);
965 case DEBUG_PARAMETER_REF:
966 return DEBUG_PARAMETER_REF_DECL (x)
967 == DEBUG_PARAMETER_REF_DECL (y);
969 case ENTRY_VALUE:
970 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
971 use rtx_equal_for_cselib_1 to compare the operands. */
972 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
974 case LABEL_REF:
975 return XEXP (x, 0) == XEXP (y, 0);
977 case MEM:
978 /* We have to compare any autoinc operations in the addresses
979 using this MEM's mode. */
980 return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x));
982 default:
983 break;
986 code = GET_CODE (x);
987 fmt = GET_RTX_FORMAT (code);
989 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
991 int j;
993 switch (fmt[i])
995 case 'w':
996 if (XWINT (x, i) != XWINT (y, i))
997 return 0;
998 break;
1000 case 'n':
1001 case 'i':
1002 if (XINT (x, i) != XINT (y, i))
1003 return 0;
1004 break;
1006 case 'V':
1007 case 'E':
1008 /* Two vectors must have the same length. */
1009 if (XVECLEN (x, i) != XVECLEN (y, i))
1010 return 0;
1012 /* And the corresponding elements must match. */
1013 for (j = 0; j < XVECLEN (x, i); j++)
1014 if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
1015 XVECEXP (y, i, j), memmode))
1016 return 0;
1017 break;
1019 case 'e':
1020 if (i == 1
1021 && targetm.commutative_p (x, UNKNOWN)
1022 && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode)
1023 && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode))
1024 return 1;
1025 if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode))
1026 return 0;
1027 break;
1029 case 'S':
1030 case 's':
1031 if (strcmp (XSTR (x, i), XSTR (y, i)))
1032 return 0;
1033 break;
1035 case 'u':
1036 /* These are just backpointers, so they don't matter. */
1037 break;
1039 case '0':
1040 case 't':
1041 break;
1043 /* It is believed that rtx's at this level will never
1044 contain anything but integers and other rtx's,
1045 except for within LABEL_REFs and SYMBOL_REFs. */
1046 default:
1047 gcc_unreachable ();
1050 return 1;
1053 /* We need to pass down the mode of constants through the hash table
1054 functions. For that purpose, wrap them in a CONST of the appropriate
1055 mode. */
1056 static rtx
1057 wrap_constant (enum machine_mode mode, rtx x)
1059 if (!CONST_SCALAR_INT_P (x) && GET_CODE (x) != CONST_FIXED)
1060 return x;
1061 gcc_assert (mode != VOIDmode);
1062 return gen_rtx_CONST (mode, x);
1065 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1066 For registers and memory locations, we look up their cselib_val structure
1067 and return its VALUE element.
1068 Possible reasons for return 0 are: the object is volatile, or we couldn't
1069 find a register or memory location in the table and CREATE is zero. If
1070 CREATE is nonzero, table elts are created for regs and mem.
1071 N.B. this hash function returns the same hash value for RTXes that
1072 differ only in the order of operands, thus it is suitable for comparisons
1073 that take commutativity into account.
1074 If we wanted to also support associative rules, we'd have to use a different
1075 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1076 MEMMODE indicates the mode of an enclosing MEM, and it's only
1077 used to compute autoinc values.
1078 We used to have a MODE argument for hashing for CONST_INTs, but that
1079 didn't make sense, since it caused spurious hash differences between
1080 (set (reg:SI 1) (const_int))
1081 (plus:SI (reg:SI 2) (reg:SI 1))
1083 (plus:SI (reg:SI 2) (const_int))
1084 If the mode is important in any context, it must be checked specifically
1085 in a comparison anyway, since relying on hash differences is unsafe. */
1087 static unsigned int
1088 cselib_hash_rtx (rtx x, int create, enum machine_mode memmode)
1090 cselib_val *e;
1091 int i, j;
1092 enum rtx_code code;
1093 const char *fmt;
1094 unsigned int hash = 0;
1096 code = GET_CODE (x);
1097 hash += (unsigned) code + (unsigned) GET_MODE (x);
1099 switch (code)
1101 case VALUE:
1102 e = CSELIB_VAL_PTR (x);
1103 return e->hash;
1105 case MEM:
1106 case REG:
1107 e = cselib_lookup (x, GET_MODE (x), create, memmode);
1108 if (! e)
1109 return 0;
1111 return e->hash;
1113 case DEBUG_EXPR:
1114 hash += ((unsigned) DEBUG_EXPR << 7)
1115 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
1116 return hash ? hash : (unsigned int) DEBUG_EXPR;
1118 case DEBUG_IMPLICIT_PTR:
1119 hash += ((unsigned) DEBUG_IMPLICIT_PTR << 7)
1120 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x));
1121 return hash ? hash : (unsigned int) DEBUG_IMPLICIT_PTR;
1123 case DEBUG_PARAMETER_REF:
1124 hash += ((unsigned) DEBUG_PARAMETER_REF << 7)
1125 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x));
1126 return hash ? hash : (unsigned int) DEBUG_PARAMETER_REF;
1128 case ENTRY_VALUE:
1129 /* ENTRY_VALUEs are function invariant, thus try to avoid
1130 recursing on argument if ENTRY_VALUE is one of the
1131 forms emitted by expand_debug_expr, otherwise
1132 ENTRY_VALUE hash would depend on the current value
1133 in some register or memory. */
1134 if (REG_P (ENTRY_VALUE_EXP (x)))
1135 hash += (unsigned int) REG
1136 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x))
1137 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x));
1138 else if (MEM_P (ENTRY_VALUE_EXP (x))
1139 && REG_P (XEXP (ENTRY_VALUE_EXP (x), 0)))
1140 hash += (unsigned int) MEM
1141 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x), 0))
1142 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x), 0));
1143 else
1144 hash += cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode);
1145 return hash ? hash : (unsigned int) ENTRY_VALUE;
1147 case CONST_INT:
1148 hash += ((unsigned) CONST_INT << 7) + INTVAL (x);
1149 return hash ? hash : (unsigned int) CONST_INT;
1151 case CONST_DOUBLE:
1152 /* This is like the general case, except that it only counts
1153 the integers representing the constant. */
1154 hash += (unsigned) code + (unsigned) GET_MODE (x);
1155 if (GET_MODE (x) != VOIDmode)
1156 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
1157 else
1158 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1159 + (unsigned) CONST_DOUBLE_HIGH (x));
1160 return hash ? hash : (unsigned int) CONST_DOUBLE;
1162 case CONST_FIXED:
1163 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1164 hash += fixed_hash (CONST_FIXED_VALUE (x));
1165 return hash ? hash : (unsigned int) CONST_FIXED;
1167 case CONST_VECTOR:
1169 int units;
1170 rtx elt;
1172 units = CONST_VECTOR_NUNITS (x);
1174 for (i = 0; i < units; ++i)
1176 elt = CONST_VECTOR_ELT (x, i);
1177 hash += cselib_hash_rtx (elt, 0, memmode);
1180 return hash;
1183 /* Assume there is only one rtx object for any given label. */
1184 case LABEL_REF:
1185 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1186 differences and differences between each stage's debugging dumps. */
1187 hash += (((unsigned int) LABEL_REF << 7)
1188 + CODE_LABEL_NUMBER (XEXP (x, 0)));
1189 return hash ? hash : (unsigned int) LABEL_REF;
1191 case SYMBOL_REF:
1193 /* Don't hash on the symbol's address to avoid bootstrap differences.
1194 Different hash values may cause expressions to be recorded in
1195 different orders and thus different registers to be used in the
1196 final assembler. This also avoids differences in the dump files
1197 between various stages. */
1198 unsigned int h = 0;
1199 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1201 while (*p)
1202 h += (h << 7) + *p++; /* ??? revisit */
1204 hash += ((unsigned int) SYMBOL_REF << 7) + h;
1205 return hash ? hash : (unsigned int) SYMBOL_REF;
1208 case PRE_DEC:
1209 case PRE_INC:
1210 /* We can't compute these without knowing the MEM mode. */
1211 gcc_assert (memmode != VOIDmode);
1212 i = GET_MODE_SIZE (memmode);
1213 if (code == PRE_DEC)
1214 i = -i;
1215 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1216 like (mem:MEMMODE (plus (reg) (const_int I))). */
1217 hash += (unsigned) PLUS - (unsigned)code
1218 + cselib_hash_rtx (XEXP (x, 0), create, memmode)
1219 + cselib_hash_rtx (GEN_INT (i), create, memmode);
1220 return hash ? hash : 1 + (unsigned) PLUS;
1222 case PRE_MODIFY:
1223 gcc_assert (memmode != VOIDmode);
1224 return cselib_hash_rtx (XEXP (x, 1), create, memmode);
1226 case POST_DEC:
1227 case POST_INC:
1228 case POST_MODIFY:
1229 gcc_assert (memmode != VOIDmode);
1230 return cselib_hash_rtx (XEXP (x, 0), create, memmode);
1232 case PC:
1233 case CC0:
1234 case CALL:
1235 case UNSPEC_VOLATILE:
1236 return 0;
1238 case ASM_OPERANDS:
1239 if (MEM_VOLATILE_P (x))
1240 return 0;
1242 break;
1244 default:
1245 break;
1248 i = GET_RTX_LENGTH (code) - 1;
1249 fmt = GET_RTX_FORMAT (code);
1250 for (; i >= 0; i--)
1252 switch (fmt[i])
1254 case 'e':
1256 rtx tem = XEXP (x, i);
1257 unsigned int tem_hash = cselib_hash_rtx (tem, create, memmode);
1259 if (tem_hash == 0)
1260 return 0;
1262 hash += tem_hash;
1264 break;
1265 case 'E':
1266 for (j = 0; j < XVECLEN (x, i); j++)
1268 unsigned int tem_hash
1269 = cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
1271 if (tem_hash == 0)
1272 return 0;
1274 hash += tem_hash;
1276 break;
1278 case 's':
1280 const unsigned char *p = (const unsigned char *) XSTR (x, i);
1282 if (p)
1283 while (*p)
1284 hash += *p++;
1285 break;
1288 case 'i':
1289 hash += XINT (x, i);
1290 break;
1292 case '0':
1293 case 't':
1294 /* unused */
1295 break;
1297 default:
1298 gcc_unreachable ();
1302 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
1305 /* Create a new value structure for VALUE and initialize it. The mode of the
1306 value is MODE. */
1308 static inline cselib_val *
1309 new_cselib_val (unsigned int hash, enum machine_mode mode, rtx x)
1311 cselib_val *e = (cselib_val *) pool_alloc (cselib_val_pool);
1313 gcc_assert (hash);
1314 gcc_assert (next_uid);
1316 e->hash = hash;
1317 e->uid = next_uid++;
1318 /* We use an alloc pool to allocate this RTL construct because it
1319 accounts for about 8% of the overall memory usage. We know
1320 precisely when we can have VALUE RTXen (when cselib is active)
1321 so we don't need to put them in garbage collected memory.
1322 ??? Why should a VALUE be an RTX in the first place? */
1323 e->val_rtx = (rtx) pool_alloc (value_pool);
1324 memset (e->val_rtx, 0, RTX_HDR_SIZE);
1325 PUT_CODE (e->val_rtx, VALUE);
1326 PUT_MODE (e->val_rtx, mode);
1327 CSELIB_VAL_PTR (e->val_rtx) = e;
1328 e->addr_list = 0;
1329 e->locs = 0;
1330 e->next_containing_mem = 0;
1332 if (dump_file && (dump_flags & TDF_CSELIB))
1334 fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
1335 if (flag_dump_noaddr || flag_dump_unnumbered)
1336 fputs ("# ", dump_file);
1337 else
1338 fprintf (dump_file, "%p ", (void*)e);
1339 print_rtl_single (dump_file, x);
1340 fputc ('\n', dump_file);
1343 return e;
1346 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1347 contains the data at this address. X is a MEM that represents the
1348 value. Update the two value structures to represent this situation. */
1350 static void
1351 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
1353 struct elt_loc_list *l;
1355 addr_elt = canonical_cselib_val (addr_elt);
1356 mem_elt = canonical_cselib_val (mem_elt);
1358 /* Avoid duplicates. */
1359 for (l = mem_elt->locs; l; l = l->next)
1360 if (MEM_P (l->loc)
1361 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
1363 promote_debug_loc (l);
1364 return;
1367 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
1368 new_elt_loc_list (mem_elt,
1369 replace_equiv_address_nv (x, addr_elt->val_rtx));
1370 if (mem_elt->next_containing_mem == NULL)
1372 mem_elt->next_containing_mem = first_containing_mem;
1373 first_containing_mem = mem_elt;
1377 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1378 If CREATE, make a new one if we haven't seen it before. */
1380 static cselib_val *
1381 cselib_lookup_mem (rtx x, int create)
1383 enum machine_mode mode = GET_MODE (x);
1384 enum machine_mode addr_mode;
1385 cselib_val **slot;
1386 cselib_val *addr;
1387 cselib_val *mem_elt;
1388 struct elt_list *l;
1390 if (MEM_VOLATILE_P (x) || mode == BLKmode
1391 || !cselib_record_memory
1392 || (FLOAT_MODE_P (mode) && flag_float_store))
1393 return 0;
1395 addr_mode = GET_MODE (XEXP (x, 0));
1396 if (addr_mode == VOIDmode)
1397 addr_mode = Pmode;
1399 /* Look up the value for the address. */
1400 addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
1401 if (! addr)
1402 return 0;
1404 addr = canonical_cselib_val (addr);
1405 /* Find a value that describes a value of our mode at that address. */
1406 for (l = addr->addr_list; l; l = l->next)
1407 if (GET_MODE (l->elt->val_rtx) == mode)
1409 promote_debug_loc (l->elt->locs);
1410 return l->elt;
1413 if (! create)
1414 return 0;
1416 mem_elt = new_cselib_val (next_uid, mode, x);
1417 add_mem_for_addr (addr, mem_elt, x);
1418 slot = cselib_find_slot (wrap_constant (mode, x), mem_elt->hash,
1419 INSERT, mode);
1420 *slot = mem_elt;
1421 return mem_elt;
1424 /* Search through the possible substitutions in P. We prefer a non reg
1425 substitution because this allows us to expand the tree further. If
1426 we find, just a reg, take the lowest regno. There may be several
1427 non-reg results, we just take the first one because they will all
1428 expand to the same place. */
1430 static rtx
1431 expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1432 int max_depth)
1434 rtx reg_result = NULL;
1435 unsigned int regno = UINT_MAX;
1436 struct elt_loc_list *p_in = p;
1438 for (; p; p = p->next)
1440 /* Return these right away to avoid returning stack pointer based
1441 expressions for frame pointer and vice versa, which is something
1442 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1443 for more details. */
1444 if (REG_P (p->loc)
1445 && (REGNO (p->loc) == STACK_POINTER_REGNUM
1446 || REGNO (p->loc) == FRAME_POINTER_REGNUM
1447 || REGNO (p->loc) == HARD_FRAME_POINTER_REGNUM
1448 || REGNO (p->loc) == cfa_base_preserved_regno))
1449 return p->loc;
1450 /* Avoid infinite recursion trying to expand a reg into a
1451 the same reg. */
1452 if ((REG_P (p->loc))
1453 && (REGNO (p->loc) < regno)
1454 && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
1456 reg_result = p->loc;
1457 regno = REGNO (p->loc);
1459 /* Avoid infinite recursion and do not try to expand the
1460 value. */
1461 else if (GET_CODE (p->loc) == VALUE
1462 && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1463 continue;
1464 else if (!REG_P (p->loc))
1466 rtx result, note;
1467 if (dump_file && (dump_flags & TDF_CSELIB))
1469 print_inline_rtx (dump_file, p->loc, 0);
1470 fprintf (dump_file, "\n");
1472 if (GET_CODE (p->loc) == LO_SUM
1473 && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1474 && p->setting_insn
1475 && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1476 && XEXP (note, 0) == XEXP (p->loc, 1))
1477 return XEXP (p->loc, 1);
1478 result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
1479 if (result)
1480 return result;
1485 if (regno != UINT_MAX)
1487 rtx result;
1488 if (dump_file && (dump_flags & TDF_CSELIB))
1489 fprintf (dump_file, "r%d\n", regno);
1491 result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
1492 if (result)
1493 return result;
1496 if (dump_file && (dump_flags & TDF_CSELIB))
1498 if (reg_result)
1500 print_inline_rtx (dump_file, reg_result, 0);
1501 fprintf (dump_file, "\n");
1503 else
1504 fprintf (dump_file, "NULL\n");
1506 return reg_result;
1510 /* Forward substitute and expand an expression out to its roots.
1511 This is the opposite of common subexpression. Because local value
1512 numbering is such a weak optimization, the expanded expression is
1513 pretty much unique (not from a pointer equals point of view but
1514 from a tree shape point of view.
1516 This function returns NULL if the expansion fails. The expansion
1517 will fail if there is no value number for one of the operands or if
1518 one of the operands has been overwritten between the current insn
1519 and the beginning of the basic block. For instance x has no
1520 expansion in:
1522 r1 <- r1 + 3
1523 x <- r1 + 8
1525 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1526 It is clear on return. */
1529 cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
1531 struct expand_value_data evd;
1533 evd.regs_active = regs_active;
1534 evd.callback = NULL;
1535 evd.callback_arg = NULL;
1536 evd.dummy = false;
1538 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1541 /* Same as cselib_expand_value_rtx, but using a callback to try to
1542 resolve some expressions. The CB function should return ORIG if it
1543 can't or does not want to deal with a certain RTX. Any other
1544 return value, including NULL, will be used as the expansion for
1545 VALUE, without any further changes. */
1548 cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1549 cselib_expand_callback cb, void *data)
1551 struct expand_value_data evd;
1553 evd.regs_active = regs_active;
1554 evd.callback = cb;
1555 evd.callback_arg = data;
1556 evd.dummy = false;
1558 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1561 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1562 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1563 would return NULL or non-NULL, without allocating new rtx. */
1565 bool
1566 cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1567 cselib_expand_callback cb, void *data)
1569 struct expand_value_data evd;
1571 evd.regs_active = regs_active;
1572 evd.callback = cb;
1573 evd.callback_arg = data;
1574 evd.dummy = true;
1576 return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1579 /* Internal implementation of cselib_expand_value_rtx and
1580 cselib_expand_value_rtx_cb. */
1582 static rtx
1583 cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1584 int max_depth)
1586 rtx copy, scopy;
1587 int i, j;
1588 RTX_CODE code;
1589 const char *format_ptr;
1590 enum machine_mode mode;
1592 code = GET_CODE (orig);
1594 /* For the context of dse, if we end up expand into a huge tree, we
1595 will not have a useful address, so we might as well just give up
1596 quickly. */
1597 if (max_depth <= 0)
1598 return NULL;
1600 switch (code)
1602 case REG:
1604 struct elt_list *l = REG_VALUES (REGNO (orig));
1606 if (l && l->elt == NULL)
1607 l = l->next;
1608 for (; l; l = l->next)
1609 if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1611 rtx result;
1612 unsigned regno = REGNO (orig);
1614 /* The only thing that we are not willing to do (this
1615 is requirement of dse and if others potential uses
1616 need this function we should add a parm to control
1617 it) is that we will not substitute the
1618 STACK_POINTER_REGNUM, FRAME_POINTER or the
1619 HARD_FRAME_POINTER.
1621 These expansions confuses the code that notices that
1622 stores into the frame go dead at the end of the
1623 function and that the frame is not effected by calls
1624 to subroutines. If you allow the
1625 STACK_POINTER_REGNUM substitution, then dse will
1626 think that parameter pushing also goes dead which is
1627 wrong. If you allow the FRAME_POINTER or the
1628 HARD_FRAME_POINTER then you lose the opportunity to
1629 make the frame assumptions. */
1630 if (regno == STACK_POINTER_REGNUM
1631 || regno == FRAME_POINTER_REGNUM
1632 || regno == HARD_FRAME_POINTER_REGNUM
1633 || regno == cfa_base_preserved_regno)
1634 return orig;
1636 bitmap_set_bit (evd->regs_active, regno);
1638 if (dump_file && (dump_flags & TDF_CSELIB))
1639 fprintf (dump_file, "expanding: r%d into: ", regno);
1641 result = expand_loc (l->elt->locs, evd, max_depth);
1642 bitmap_clear_bit (evd->regs_active, regno);
1644 if (result)
1645 return result;
1646 else
1647 return orig;
1651 CASE_CONST_ANY:
1652 case SYMBOL_REF:
1653 case CODE_LABEL:
1654 case PC:
1655 case CC0:
1656 case SCRATCH:
1657 /* SCRATCH must be shared because they represent distinct values. */
1658 return orig;
1659 case CLOBBER:
1660 if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1661 return orig;
1662 break;
1664 case CONST:
1665 if (shared_const_p (orig))
1666 return orig;
1667 break;
1669 case SUBREG:
1671 rtx subreg;
1673 if (evd->callback)
1675 subreg = evd->callback (orig, evd->regs_active, max_depth,
1676 evd->callback_arg);
1677 if (subreg != orig)
1678 return subreg;
1681 subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1682 max_depth - 1);
1683 if (!subreg)
1684 return NULL;
1685 scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1686 GET_MODE (SUBREG_REG (orig)),
1687 SUBREG_BYTE (orig));
1688 if (scopy == NULL
1689 || (GET_CODE (scopy) == SUBREG
1690 && !REG_P (SUBREG_REG (scopy))
1691 && !MEM_P (SUBREG_REG (scopy))))
1692 return NULL;
1694 return scopy;
1697 case VALUE:
1699 rtx result;
1701 if (dump_file && (dump_flags & TDF_CSELIB))
1703 fputs ("\nexpanding ", dump_file);
1704 print_rtl_single (dump_file, orig);
1705 fputs (" into...", dump_file);
1708 if (evd->callback)
1710 result = evd->callback (orig, evd->regs_active, max_depth,
1711 evd->callback_arg);
1713 if (result != orig)
1714 return result;
1717 result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
1718 return result;
1721 case DEBUG_EXPR:
1722 if (evd->callback)
1723 return evd->callback (orig, evd->regs_active, max_depth,
1724 evd->callback_arg);
1725 return orig;
1727 default:
1728 break;
1731 /* Copy the various flags, fields, and other information. We assume
1732 that all fields need copying, and then clear the fields that should
1733 not be copied. That is the sensible default behavior, and forces
1734 us to explicitly document why we are *not* copying a flag. */
1735 if (evd->dummy)
1736 copy = NULL;
1737 else
1738 copy = shallow_copy_rtx (orig);
1740 format_ptr = GET_RTX_FORMAT (code);
1742 for (i = 0; i < GET_RTX_LENGTH (code); i++)
1743 switch (*format_ptr++)
1745 case 'e':
1746 if (XEXP (orig, i) != NULL)
1748 rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1749 max_depth - 1);
1750 if (!result)
1751 return NULL;
1752 if (copy)
1753 XEXP (copy, i) = result;
1755 break;
1757 case 'E':
1758 case 'V':
1759 if (XVEC (orig, i) != NULL)
1761 if (copy)
1762 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1763 for (j = 0; j < XVECLEN (orig, i); j++)
1765 rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1766 evd, max_depth - 1);
1767 if (!result)
1768 return NULL;
1769 if (copy)
1770 XVECEXP (copy, i, j) = result;
1773 break;
1775 case 't':
1776 case 'w':
1777 case 'i':
1778 case 's':
1779 case 'S':
1780 case 'T':
1781 case 'u':
1782 case 'B':
1783 case '0':
1784 /* These are left unchanged. */
1785 break;
1787 default:
1788 gcc_unreachable ();
1791 if (evd->dummy)
1792 return orig;
1794 mode = GET_MODE (copy);
1795 /* If an operand has been simplified into CONST_INT, which doesn't
1796 have a mode and the mode isn't derivable from whole rtx's mode,
1797 try simplify_*_operation first with mode from original's operand
1798 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1799 scopy = copy;
1800 switch (GET_RTX_CLASS (code))
1802 case RTX_UNARY:
1803 if (CONST_INT_P (XEXP (copy, 0))
1804 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1806 scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1807 GET_MODE (XEXP (orig, 0)));
1808 if (scopy)
1809 return scopy;
1811 break;
1812 case RTX_COMM_ARITH:
1813 case RTX_BIN_ARITH:
1814 /* These expressions can derive operand modes from the whole rtx's mode. */
1815 break;
1816 case RTX_TERNARY:
1817 case RTX_BITFIELD_OPS:
1818 if (CONST_INT_P (XEXP (copy, 0))
1819 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1821 scopy = simplify_ternary_operation (code, mode,
1822 GET_MODE (XEXP (orig, 0)),
1823 XEXP (copy, 0), XEXP (copy, 1),
1824 XEXP (copy, 2));
1825 if (scopy)
1826 return scopy;
1828 break;
1829 case RTX_COMPARE:
1830 case RTX_COMM_COMPARE:
1831 if (CONST_INT_P (XEXP (copy, 0))
1832 && GET_MODE (XEXP (copy, 1)) == VOIDmode
1833 && (GET_MODE (XEXP (orig, 0)) != VOIDmode
1834 || GET_MODE (XEXP (orig, 1)) != VOIDmode))
1836 scopy = simplify_relational_operation (code, mode,
1837 (GET_MODE (XEXP (orig, 0))
1838 != VOIDmode)
1839 ? GET_MODE (XEXP (orig, 0))
1840 : GET_MODE (XEXP (orig, 1)),
1841 XEXP (copy, 0),
1842 XEXP (copy, 1));
1843 if (scopy)
1844 return scopy;
1846 break;
1847 default:
1848 break;
1850 scopy = simplify_rtx (copy);
1851 if (scopy)
1852 return scopy;
1853 return copy;
1856 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1857 with VALUE expressions. This way, it becomes independent of changes
1858 to registers and memory.
1859 X isn't actually modified; if modifications are needed, new rtl is
1860 allocated. However, the return value can share rtl with X.
1861 If X is within a MEM, MEMMODE must be the mode of the MEM. */
1864 cselib_subst_to_values (rtx x, enum machine_mode memmode)
1866 enum rtx_code code = GET_CODE (x);
1867 const char *fmt = GET_RTX_FORMAT (code);
1868 cselib_val *e;
1869 struct elt_list *l;
1870 rtx copy = x;
1871 int i;
1873 switch (code)
1875 case REG:
1876 l = REG_VALUES (REGNO (x));
1877 if (l && l->elt == NULL)
1878 l = l->next;
1879 for (; l; l = l->next)
1880 if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
1881 return l->elt->val_rtx;
1883 gcc_unreachable ();
1885 case MEM:
1886 e = cselib_lookup_mem (x, 0);
1887 /* This used to happen for autoincrements, but we deal with them
1888 properly now. Remove the if stmt for the next release. */
1889 if (! e)
1891 /* Assign a value that doesn't match any other. */
1892 e = new_cselib_val (next_uid, GET_MODE (x), x);
1894 return e->val_rtx;
1896 case ENTRY_VALUE:
1897 e = cselib_lookup (x, GET_MODE (x), 0, memmode);
1898 if (! e)
1899 break;
1900 return e->val_rtx;
1902 CASE_CONST_ANY:
1903 return x;
1905 case PRE_DEC:
1906 case PRE_INC:
1907 gcc_assert (memmode != VOIDmode);
1908 i = GET_MODE_SIZE (memmode);
1909 if (code == PRE_DEC)
1910 i = -i;
1911 return cselib_subst_to_values (plus_constant (GET_MODE (x),
1912 XEXP (x, 0), i),
1913 memmode);
1915 case PRE_MODIFY:
1916 gcc_assert (memmode != VOIDmode);
1917 return cselib_subst_to_values (XEXP (x, 1), memmode);
1919 case POST_DEC:
1920 case POST_INC:
1921 case POST_MODIFY:
1922 gcc_assert (memmode != VOIDmode);
1923 return cselib_subst_to_values (XEXP (x, 0), memmode);
1925 default:
1926 break;
1929 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1931 if (fmt[i] == 'e')
1933 rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
1935 if (t != XEXP (x, i))
1937 if (x == copy)
1938 copy = shallow_copy_rtx (x);
1939 XEXP (copy, i) = t;
1942 else if (fmt[i] == 'E')
1944 int j;
1946 for (j = 0; j < XVECLEN (x, i); j++)
1948 rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
1950 if (t != XVECEXP (x, i, j))
1952 if (XVEC (x, i) == XVEC (copy, i))
1954 if (x == copy)
1955 copy = shallow_copy_rtx (x);
1956 XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
1958 XVECEXP (copy, i, j) = t;
1964 return copy;
1967 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
1970 cselib_subst_to_values_from_insn (rtx x, enum machine_mode memmode, rtx insn)
1972 rtx ret;
1973 gcc_assert (!cselib_current_insn);
1974 cselib_current_insn = insn;
1975 ret = cselib_subst_to_values (x, memmode);
1976 cselib_current_insn = NULL;
1977 return ret;
1980 /* Look up the rtl expression X in our tables and return the value it
1981 has. If CREATE is zero, we return NULL if we don't know the value.
1982 Otherwise, we create a new one if possible, using mode MODE if X
1983 doesn't have a mode (i.e. because it's a constant). When X is part
1984 of an address, MEMMODE should be the mode of the enclosing MEM if
1985 we're tracking autoinc expressions. */
1987 static cselib_val *
1988 cselib_lookup_1 (rtx x, enum machine_mode mode,
1989 int create, enum machine_mode memmode)
1991 cselib_val **slot;
1992 cselib_val *e;
1993 unsigned int hashval;
1995 if (GET_MODE (x) != VOIDmode)
1996 mode = GET_MODE (x);
1998 if (GET_CODE (x) == VALUE)
1999 return CSELIB_VAL_PTR (x);
2001 if (REG_P (x))
2003 struct elt_list *l;
2004 unsigned int i = REGNO (x);
2006 l = REG_VALUES (i);
2007 if (l && l->elt == NULL)
2008 l = l->next;
2009 for (; l; l = l->next)
2010 if (mode == GET_MODE (l->elt->val_rtx))
2012 promote_debug_loc (l->elt->locs);
2013 return l->elt;
2016 if (! create)
2017 return 0;
2019 if (i < FIRST_PSEUDO_REGISTER)
2021 unsigned int n = hard_regno_nregs[i][mode];
2023 if (n > max_value_regs)
2024 max_value_regs = n;
2027 e = new_cselib_val (next_uid, GET_MODE (x), x);
2028 new_elt_loc_list (e, x);
2029 if (REG_VALUES (i) == 0)
2031 /* Maintain the invariant that the first entry of
2032 REG_VALUES, if present, must be the value used to set the
2033 register, or NULL. */
2034 used_regs[n_used_regs++] = i;
2035 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
2037 else if (cselib_preserve_constants
2038 && GET_MODE_CLASS (mode) == MODE_INT)
2040 /* During var-tracking, try harder to find equivalences
2041 for SUBREGs. If a setter sets say a DImode register
2042 and user uses that register only in SImode, add a lowpart
2043 subreg location. */
2044 struct elt_list *lwider = NULL;
2045 l = REG_VALUES (i);
2046 if (l && l->elt == NULL)
2047 l = l->next;
2048 for (; l; l = l->next)
2049 if (GET_MODE_CLASS (GET_MODE (l->elt->val_rtx)) == MODE_INT
2050 && GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2051 > GET_MODE_SIZE (mode)
2052 && (lwider == NULL
2053 || GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2054 < GET_MODE_SIZE (GET_MODE (lwider->elt->val_rtx))))
2056 struct elt_loc_list *el;
2057 if (i < FIRST_PSEUDO_REGISTER
2058 && hard_regno_nregs[i][GET_MODE (l->elt->val_rtx)] != 1)
2059 continue;
2060 for (el = l->elt->locs; el; el = el->next)
2061 if (!REG_P (el->loc))
2062 break;
2063 if (el)
2064 lwider = l;
2066 if (lwider)
2068 rtx sub = lowpart_subreg (mode, lwider->elt->val_rtx,
2069 GET_MODE (lwider->elt->val_rtx));
2070 if (sub)
2071 new_elt_loc_list (e, sub);
2074 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
2075 slot = cselib_find_slot (x, e->hash, INSERT, memmode);
2076 *slot = e;
2077 return e;
2080 if (MEM_P (x))
2081 return cselib_lookup_mem (x, create);
2083 hashval = cselib_hash_rtx (x, create, memmode);
2084 /* Can't even create if hashing is not possible. */
2085 if (! hashval)
2086 return 0;
2088 slot = cselib_find_slot (wrap_constant (mode, x), hashval,
2089 create ? INSERT : NO_INSERT, memmode);
2090 if (slot == 0)
2091 return 0;
2093 e = (cselib_val *) *slot;
2094 if (e)
2095 return e;
2097 e = new_cselib_val (hashval, mode, x);
2099 /* We have to fill the slot before calling cselib_subst_to_values:
2100 the hash table is inconsistent until we do so, and
2101 cselib_subst_to_values will need to do lookups. */
2102 *slot = e;
2103 new_elt_loc_list (e, cselib_subst_to_values (x, memmode));
2104 return e;
2107 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2109 cselib_val *
2110 cselib_lookup_from_insn (rtx x, enum machine_mode mode,
2111 int create, enum machine_mode memmode, rtx insn)
2113 cselib_val *ret;
2115 gcc_assert (!cselib_current_insn);
2116 cselib_current_insn = insn;
2118 ret = cselib_lookup (x, mode, create, memmode);
2120 cselib_current_insn = NULL;
2122 return ret;
2125 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2126 maintains invariants related with debug insns. */
2128 cselib_val *
2129 cselib_lookup (rtx x, enum machine_mode mode,
2130 int create, enum machine_mode memmode)
2132 cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
2134 /* ??? Should we return NULL if we're not to create an entry, the
2135 found loc is a debug loc and cselib_current_insn is not DEBUG?
2136 If so, we should also avoid converting val to non-DEBUG; probably
2137 easiest setting cselib_current_insn to NULL before the call
2138 above. */
2140 if (dump_file && (dump_flags & TDF_CSELIB))
2142 fputs ("cselib lookup ", dump_file);
2143 print_inline_rtx (dump_file, x, 2);
2144 fprintf (dump_file, " => %u:%u\n",
2145 ret ? ret->uid : 0,
2146 ret ? ret->hash : 0);
2149 return ret;
2152 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2153 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2154 is used to determine how many hard registers are being changed. If MODE
2155 is VOIDmode, then only REGNO is being changed; this is used when
2156 invalidating call clobbered registers across a call. */
2158 static void
2159 cselib_invalidate_regno (unsigned int regno, enum machine_mode mode)
2161 unsigned int endregno;
2162 unsigned int i;
2164 /* If we see pseudos after reload, something is _wrong_. */
2165 gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
2166 || reg_renumber[regno] < 0);
2168 /* Determine the range of registers that must be invalidated. For
2169 pseudos, only REGNO is affected. For hard regs, we must take MODE
2170 into account, and we must also invalidate lower register numbers
2171 if they contain values that overlap REGNO. */
2172 if (regno < FIRST_PSEUDO_REGISTER)
2174 gcc_assert (mode != VOIDmode);
2176 if (regno < max_value_regs)
2177 i = 0;
2178 else
2179 i = regno - max_value_regs;
2181 endregno = end_hard_regno (mode, regno);
2183 else
2185 i = regno;
2186 endregno = regno + 1;
2189 for (; i < endregno; i++)
2191 struct elt_list **l = &REG_VALUES (i);
2193 /* Go through all known values for this reg; if it overlaps the range
2194 we're invalidating, remove the value. */
2195 while (*l)
2197 cselib_val *v = (*l)->elt;
2198 bool had_locs;
2199 rtx setting_insn;
2200 struct elt_loc_list **p;
2201 unsigned int this_last = i;
2203 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
2204 this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
2206 if (this_last < regno || v == NULL
2207 || (v == cfa_base_preserved_val
2208 && i == cfa_base_preserved_regno))
2210 l = &(*l)->next;
2211 continue;
2214 /* We have an overlap. */
2215 if (*l == REG_VALUES (i))
2217 /* Maintain the invariant that the first entry of
2218 REG_VALUES, if present, must be the value used to set
2219 the register, or NULL. This is also nice because
2220 then we won't push the same regno onto user_regs
2221 multiple times. */
2222 (*l)->elt = NULL;
2223 l = &(*l)->next;
2225 else
2226 unchain_one_elt_list (l);
2228 v = canonical_cselib_val (v);
2230 had_locs = v->locs != NULL;
2231 setting_insn = v->locs ? v->locs->setting_insn : NULL;
2233 /* Now, we clear the mapping from value to reg. It must exist, so
2234 this code will crash intentionally if it doesn't. */
2235 for (p = &v->locs; ; p = &(*p)->next)
2237 rtx x = (*p)->loc;
2239 if (REG_P (x) && REGNO (x) == i)
2241 unchain_one_elt_loc_list (p);
2242 break;
2246 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2248 if (setting_insn && DEBUG_INSN_P (setting_insn))
2249 n_useless_debug_values++;
2250 else
2251 n_useless_values++;
2257 /* Invalidate any locations in the table which are changed because of a
2258 store to MEM_RTX. If this is called because of a non-const call
2259 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2261 static void
2262 cselib_invalidate_mem (rtx mem_rtx)
2264 cselib_val **vp, *v, *next;
2265 int num_mems = 0;
2266 rtx mem_addr;
2268 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2269 mem_rtx = canon_rtx (mem_rtx);
2271 vp = &first_containing_mem;
2272 for (v = *vp; v != &dummy_val; v = next)
2274 bool has_mem = false;
2275 struct elt_loc_list **p = &v->locs;
2276 bool had_locs = v->locs != NULL;
2277 rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
2279 while (*p)
2281 rtx x = (*p)->loc;
2282 cselib_val *addr;
2283 struct elt_list **mem_chain;
2285 /* MEMs may occur in locations only at the top level; below
2286 that every MEM or REG is substituted by its VALUE. */
2287 if (!MEM_P (x))
2289 p = &(*p)->next;
2290 continue;
2292 if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
2293 && ! canon_anti_dependence (x, false, mem_rtx,
2294 GET_MODE (mem_rtx), mem_addr))
2296 has_mem = true;
2297 num_mems++;
2298 p = &(*p)->next;
2299 continue;
2302 /* This one overlaps. */
2303 /* We must have a mapping from this MEM's address to the
2304 value (E). Remove that, too. */
2305 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
2306 addr = canonical_cselib_val (addr);
2307 gcc_checking_assert (v == canonical_cselib_val (v));
2308 mem_chain = &addr->addr_list;
2309 for (;;)
2311 cselib_val *canon = canonical_cselib_val ((*mem_chain)->elt);
2313 if (canon == v)
2315 unchain_one_elt_list (mem_chain);
2316 break;
2319 /* Record canonicalized elt. */
2320 (*mem_chain)->elt = canon;
2322 mem_chain = &(*mem_chain)->next;
2325 unchain_one_elt_loc_list (p);
2328 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2330 if (setting_insn && DEBUG_INSN_P (setting_insn))
2331 n_useless_debug_values++;
2332 else
2333 n_useless_values++;
2336 next = v->next_containing_mem;
2337 if (has_mem)
2339 *vp = v;
2340 vp = &(*vp)->next_containing_mem;
2342 else
2343 v->next_containing_mem = NULL;
2345 *vp = &dummy_val;
2348 /* Invalidate DEST, which is being assigned to or clobbered. */
2350 void
2351 cselib_invalidate_rtx (rtx dest)
2353 while (GET_CODE (dest) == SUBREG
2354 || GET_CODE (dest) == ZERO_EXTRACT
2355 || GET_CODE (dest) == STRICT_LOW_PART)
2356 dest = XEXP (dest, 0);
2358 if (REG_P (dest))
2359 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
2360 else if (MEM_P (dest))
2361 cselib_invalidate_mem (dest);
2364 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2366 static void
2367 cselib_invalidate_rtx_note_stores (rtx dest, const_rtx ignore ATTRIBUTE_UNUSED,
2368 void *data ATTRIBUTE_UNUSED)
2370 cselib_invalidate_rtx (dest);
2373 /* Record the result of a SET instruction. DEST is being set; the source
2374 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2375 describes its address. */
2377 static void
2378 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
2380 int dreg = REG_P (dest) ? (int) REGNO (dest) : -1;
2382 if (src_elt == 0 || side_effects_p (dest))
2383 return;
2385 if (dreg >= 0)
2387 if (dreg < FIRST_PSEUDO_REGISTER)
2389 unsigned int n = hard_regno_nregs[dreg][GET_MODE (dest)];
2391 if (n > max_value_regs)
2392 max_value_regs = n;
2395 if (REG_VALUES (dreg) == 0)
2397 used_regs[n_used_regs++] = dreg;
2398 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2400 else
2402 /* The register should have been invalidated. */
2403 gcc_assert (REG_VALUES (dreg)->elt == 0);
2404 REG_VALUES (dreg)->elt = src_elt;
2407 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2408 n_useless_values--;
2409 new_elt_loc_list (src_elt, dest);
2411 else if (MEM_P (dest) && dest_addr_elt != 0
2412 && cselib_record_memory)
2414 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2415 n_useless_values--;
2416 add_mem_for_addr (dest_addr_elt, src_elt, dest);
2420 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2422 void
2423 cselib_add_permanent_equiv (cselib_val *elt, rtx x, rtx insn)
2425 cselib_val *nelt;
2426 rtx save_cselib_current_insn = cselib_current_insn;
2428 gcc_checking_assert (elt);
2429 gcc_checking_assert (PRESERVED_VALUE_P (elt->val_rtx));
2430 gcc_checking_assert (!side_effects_p (x));
2432 cselib_current_insn = insn;
2434 nelt = cselib_lookup (x, GET_MODE (elt->val_rtx), 1, VOIDmode);
2436 if (nelt != elt)
2438 cselib_any_perm_equivs = true;
2440 if (!PRESERVED_VALUE_P (nelt->val_rtx))
2441 cselib_preserve_value (nelt);
2443 new_elt_loc_list (nelt, elt->val_rtx);
2446 cselib_current_insn = save_cselib_current_insn;
2449 /* Return TRUE if any permanent equivalences have been recorded since
2450 the table was last initialized. */
2451 bool
2452 cselib_have_permanent_equivalences (void)
2454 return cselib_any_perm_equivs;
2457 /* There is no good way to determine how many elements there can be
2458 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2459 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2461 struct cselib_record_autoinc_data
2463 struct cselib_set *sets;
2464 int n_sets;
2467 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2468 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2470 static int
2471 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
2472 rtx dest, rtx src, rtx srcoff, void *arg)
2474 struct cselib_record_autoinc_data *data;
2475 data = (struct cselib_record_autoinc_data *)arg;
2477 data->sets[data->n_sets].dest = dest;
2479 if (srcoff)
2480 data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
2481 else
2482 data->sets[data->n_sets].src = src;
2484 data->n_sets++;
2486 return -1;
2489 /* Record the effects of any sets and autoincs in INSN. */
2490 static void
2491 cselib_record_sets (rtx insn)
2493 int n_sets = 0;
2494 int i;
2495 struct cselib_set sets[MAX_SETS];
2496 rtx body = PATTERN (insn);
2497 rtx cond = 0;
2498 int n_sets_before_autoinc;
2499 struct cselib_record_autoinc_data data;
2501 body = PATTERN (insn);
2502 if (GET_CODE (body) == COND_EXEC)
2504 cond = COND_EXEC_TEST (body);
2505 body = COND_EXEC_CODE (body);
2508 /* Find all sets. */
2509 if (GET_CODE (body) == SET)
2511 sets[0].src = SET_SRC (body);
2512 sets[0].dest = SET_DEST (body);
2513 n_sets = 1;
2515 else if (GET_CODE (body) == PARALLEL)
2517 /* Look through the PARALLEL and record the values being
2518 set, if possible. Also handle any CLOBBERs. */
2519 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2521 rtx x = XVECEXP (body, 0, i);
2523 if (GET_CODE (x) == SET)
2525 sets[n_sets].src = SET_SRC (x);
2526 sets[n_sets].dest = SET_DEST (x);
2527 n_sets++;
2532 if (n_sets == 1
2533 && MEM_P (sets[0].src)
2534 && !cselib_record_memory
2535 && MEM_READONLY_P (sets[0].src))
2537 rtx note = find_reg_equal_equiv_note (insn);
2539 if (note && CONSTANT_P (XEXP (note, 0)))
2540 sets[0].src = XEXP (note, 0);
2543 data.sets = sets;
2544 data.n_sets = n_sets_before_autoinc = n_sets;
2545 for_each_inc_dec (&insn, cselib_record_autoinc_cb, &data);
2546 n_sets = data.n_sets;
2548 /* Look up the values that are read. Do this before invalidating the
2549 locations that are written. */
2550 for (i = 0; i < n_sets; i++)
2552 rtx dest = sets[i].dest;
2554 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2555 the low part after invalidating any knowledge about larger modes. */
2556 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2557 sets[i].dest = dest = XEXP (dest, 0);
2559 /* We don't know how to record anything but REG or MEM. */
2560 if (REG_P (dest)
2561 || (MEM_P (dest) && cselib_record_memory))
2563 rtx src = sets[i].src;
2564 if (cond)
2565 src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
2566 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
2567 if (MEM_P (dest))
2569 enum machine_mode address_mode = get_address_mode (dest);
2571 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
2572 address_mode, 1,
2573 GET_MODE (dest));
2575 else
2576 sets[i].dest_addr_elt = 0;
2580 if (cselib_record_sets_hook)
2581 cselib_record_sets_hook (insn, sets, n_sets);
2583 /* Invalidate all locations written by this insn. Note that the elts we
2584 looked up in the previous loop aren't affected, just some of their
2585 locations may go away. */
2586 note_stores (body, cselib_invalidate_rtx_note_stores, NULL);
2588 for (i = n_sets_before_autoinc; i < n_sets; i++)
2589 cselib_invalidate_rtx (sets[i].dest);
2591 /* If this is an asm, look for duplicate sets. This can happen when the
2592 user uses the same value as an output multiple times. This is valid
2593 if the outputs are not actually used thereafter. Treat this case as
2594 if the value isn't actually set. We do this by smashing the destination
2595 to pc_rtx, so that we won't record the value later. */
2596 if (n_sets >= 2 && asm_noperands (body) >= 0)
2598 for (i = 0; i < n_sets; i++)
2600 rtx dest = sets[i].dest;
2601 if (REG_P (dest) || MEM_P (dest))
2603 int j;
2604 for (j = i + 1; j < n_sets; j++)
2605 if (rtx_equal_p (dest, sets[j].dest))
2607 sets[i].dest = pc_rtx;
2608 sets[j].dest = pc_rtx;
2614 /* Now enter the equivalences in our tables. */
2615 for (i = 0; i < n_sets; i++)
2617 rtx dest = sets[i].dest;
2618 if (REG_P (dest)
2619 || (MEM_P (dest) && cselib_record_memory))
2620 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2624 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
2626 bool
2627 fp_setter_insn (rtx insn)
2629 rtx expr, pat = NULL_RTX;
2631 if (!RTX_FRAME_RELATED_P (insn))
2632 return false;
2634 expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2635 if (expr)
2636 pat = XEXP (expr, 0);
2637 if (!modified_in_p (hard_frame_pointer_rtx, pat ? pat : insn))
2638 return false;
2640 /* Don't return true for frame pointer restores in the epilogue. */
2641 if (find_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx))
2642 return false;
2643 return true;
2646 /* Record the effects of INSN. */
2648 void
2649 cselib_process_insn (rtx insn)
2651 int i;
2652 rtx x;
2654 cselib_current_insn = insn;
2656 /* Forget everything at a CODE_LABEL, a volatile insn, or a setjmp. */
2657 if ((LABEL_P (insn)
2658 || (CALL_P (insn)
2659 && find_reg_note (insn, REG_SETJMP, NULL))
2660 || (NONJUMP_INSN_P (insn)
2661 && volatile_insn_p (PATTERN (insn))))
2662 && !cselib_preserve_constants)
2664 cselib_reset_table (next_uid);
2665 cselib_current_insn = NULL_RTX;
2666 return;
2669 if (! INSN_P (insn))
2671 cselib_current_insn = NULL_RTX;
2672 return;
2675 /* If this is a call instruction, forget anything stored in a
2676 call clobbered register, or, if this is not a const call, in
2677 memory. */
2678 if (CALL_P (insn))
2680 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2681 if (call_used_regs[i]
2682 || (REG_VALUES (i) && REG_VALUES (i)->elt
2683 && HARD_REGNO_CALL_PART_CLOBBERED (i,
2684 GET_MODE (REG_VALUES (i)->elt->val_rtx))))
2685 cselib_invalidate_regno (i, reg_raw_mode[i]);
2687 /* Since it is not clear how cselib is going to be used, be
2688 conservative here and treat looping pure or const functions
2689 as if they were regular functions. */
2690 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
2691 || !(RTL_CONST_OR_PURE_CALL_P (insn)))
2692 cselib_invalidate_mem (callmem);
2695 cselib_record_sets (insn);
2697 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2698 after we have processed the insn. */
2699 if (CALL_P (insn))
2701 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
2702 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
2703 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
2704 /* Flush evertything on setjmp. */
2705 if (cselib_preserve_constants
2706 && find_reg_note (insn, REG_SETJMP, NULL))
2708 cselib_preserve_only_values ();
2709 cselib_reset_table (next_uid);
2713 /* On setter of the hard frame pointer if frame_pointer_needed,
2714 invalidate stack_pointer_rtx, so that sp and {,h}fp based
2715 VALUEs are distinct. */
2716 if (reload_completed
2717 && frame_pointer_needed
2718 && fp_setter_insn (insn))
2719 cselib_invalidate_rtx (stack_pointer_rtx);
2721 cselib_current_insn = NULL_RTX;
2723 remove_useless_values ();
2726 /* Initialize cselib for one pass. The caller must also call
2727 init_alias_analysis. */
2729 void
2730 cselib_init (int record_what)
2732 elt_list_pool = create_alloc_pool ("elt_list",
2733 sizeof (struct elt_list), 10);
2734 elt_loc_list_pool = create_alloc_pool ("elt_loc_list",
2735 sizeof (struct elt_loc_list), 10);
2736 cselib_val_pool = create_alloc_pool ("cselib_val_list",
2737 sizeof (cselib_val), 10);
2738 value_pool = create_alloc_pool ("value", RTX_CODE_SIZE (VALUE), 100);
2739 cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
2740 cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
2741 cselib_any_perm_equivs = false;
2743 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2744 see canon_true_dependence. This is only created once. */
2745 if (! callmem)
2746 callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2748 cselib_nregs = max_reg_num ();
2750 /* We preserve reg_values to allow expensive clearing of the whole thing.
2751 Reallocate it however if it happens to be too large. */
2752 if (!reg_values || reg_values_size < cselib_nregs
2753 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
2755 free (reg_values);
2756 /* Some space for newly emit instructions so we don't end up
2757 reallocating in between passes. */
2758 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
2759 reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
2761 used_regs = XNEWVEC (unsigned int, cselib_nregs);
2762 n_used_regs = 0;
2763 cselib_hash_table.create (31);
2764 if (cselib_preserve_constants)
2765 cselib_preserved_hash_table.create (31);
2766 next_uid = 1;
2769 /* Called when the current user is done with cselib. */
2771 void
2772 cselib_finish (void)
2774 bool preserved = cselib_preserve_constants;
2775 cselib_discard_hook = NULL;
2776 cselib_preserve_constants = false;
2777 cselib_any_perm_equivs = false;
2778 cfa_base_preserved_val = NULL;
2779 cfa_base_preserved_regno = INVALID_REGNUM;
2780 free_alloc_pool (elt_list_pool);
2781 free_alloc_pool (elt_loc_list_pool);
2782 free_alloc_pool (cselib_val_pool);
2783 free_alloc_pool (value_pool);
2784 cselib_clear_table ();
2785 cselib_hash_table.dispose ();
2786 if (preserved)
2787 cselib_preserved_hash_table.dispose ();
2788 free (used_regs);
2789 used_regs = 0;
2790 n_useless_values = 0;
2791 n_useless_debug_values = 0;
2792 n_debug_values = 0;
2793 next_uid = 0;
2796 /* Dump the cselib_val *X to FILE *OUT. */
2799 dump_cselib_val (cselib_val **x, FILE *out)
2801 cselib_val *v = *x;
2802 bool need_lf = true;
2804 print_inline_rtx (out, v->val_rtx, 0);
2806 if (v->locs)
2808 struct elt_loc_list *l = v->locs;
2809 if (need_lf)
2811 fputc ('\n', out);
2812 need_lf = false;
2814 fputs (" locs:", out);
2817 if (l->setting_insn)
2818 fprintf (out, "\n from insn %i ",
2819 INSN_UID (l->setting_insn));
2820 else
2821 fprintf (out, "\n ");
2822 print_inline_rtx (out, l->loc, 4);
2824 while ((l = l->next));
2825 fputc ('\n', out);
2827 else
2829 fputs (" no locs", out);
2830 need_lf = true;
2833 if (v->addr_list)
2835 struct elt_list *e = v->addr_list;
2836 if (need_lf)
2838 fputc ('\n', out);
2839 need_lf = false;
2841 fputs (" addr list:", out);
2844 fputs ("\n ", out);
2845 print_inline_rtx (out, e->elt->val_rtx, 2);
2847 while ((e = e->next));
2848 fputc ('\n', out);
2850 else
2852 fputs (" no addrs", out);
2853 need_lf = true;
2856 if (v->next_containing_mem == &dummy_val)
2857 fputs (" last mem\n", out);
2858 else if (v->next_containing_mem)
2860 fputs (" next mem ", out);
2861 print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
2862 fputc ('\n', out);
2864 else if (need_lf)
2865 fputc ('\n', out);
2867 return 1;
2870 /* Dump to OUT everything in the CSELIB table. */
2872 void
2873 dump_cselib_table (FILE *out)
2875 fprintf (out, "cselib hash table:\n");
2876 cselib_hash_table.traverse <FILE *, dump_cselib_val> (out);
2877 fprintf (out, "cselib preserved hash table:\n");
2878 cselib_preserved_hash_table.traverse <FILE *, dump_cselib_val> (out);
2879 if (first_containing_mem != &dummy_val)
2881 fputs ("first mem ", out);
2882 print_inline_rtx (out, first_containing_mem->val_rtx, 2);
2883 fputc ('\n', out);
2885 fprintf (out, "next uid %i\n", next_uid);
2888 #include "gt-cselib.h"