gcc/testsuite/
[official-gcc.git] / gcc / cselib.c
blob4fb5010cbf2877ba85703d2525ba51eaec0d6b3c
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 "hashtab.h"
34 #include "hash-set.h"
35 #include "vec.h"
36 #include "machmode.h"
37 #include "input.h"
38 #include "function.h"
39 #include "emit-rtl.h"
40 #include "diagnostic-core.h"
41 #include "ggc.h"
42 #include "hash-table.h"
43 #include "dumpfile.h"
44 #include "cselib.h"
45 #include "valtrack.h"
46 #include "params.h"
47 #include "alloc-pool.h"
48 #include "target.h"
49 #include "bitmap.h"
51 /* A list of cselib_val structures. */
52 struct elt_list {
53 struct elt_list *next;
54 cselib_val *elt;
57 static bool cselib_record_memory;
58 static bool cselib_preserve_constants;
59 static bool cselib_any_perm_equivs;
60 static inline void promote_debug_loc (struct elt_loc_list *l);
61 static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
62 static void new_elt_loc_list (cselib_val *, rtx);
63 static void unchain_one_value (cselib_val *);
64 static void unchain_one_elt_list (struct elt_list **);
65 static void unchain_one_elt_loc_list (struct elt_loc_list **);
66 static void remove_useless_values (void);
67 static int rtx_equal_for_cselib_1 (rtx, rtx, enum machine_mode);
68 static unsigned int cselib_hash_rtx (rtx, int, enum machine_mode);
69 static cselib_val *new_cselib_val (unsigned int, enum machine_mode, rtx);
70 static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
71 static cselib_val *cselib_lookup_mem (rtx, int);
72 static void cselib_invalidate_regno (unsigned int, enum machine_mode);
73 static void cselib_invalidate_mem (rtx);
74 static void cselib_record_set (rtx, cselib_val *, cselib_val *);
75 static void cselib_record_sets (rtx_insn *);
77 struct expand_value_data
79 bitmap regs_active;
80 cselib_expand_callback callback;
81 void *callback_arg;
82 bool dummy;
85 static rtx cselib_expand_value_rtx_1 (rtx, struct expand_value_data *, int);
87 /* There are three ways in which cselib can look up an rtx:
88 - for a REG, the reg_values table (which is indexed by regno) is used
89 - for a MEM, we recursively look up its address and then follow the
90 addr_list of that value
91 - for everything else, we compute a hash value and go through the hash
92 table. Since different rtx's can still have the same hash value,
93 this involves walking the table entries for a given value and comparing
94 the locations of the entries with the rtx we are looking up. */
96 struct cselib_hasher : typed_noop_remove <cselib_val>
98 typedef cselib_val value_type;
99 struct compare_type {
100 /* The rtx value and its mode (needed separately for constant
101 integers). */
102 enum machine_mode mode;
103 rtx x;
104 /* The mode of the contaning MEM, if any, otherwise VOIDmode. */
105 enum machine_mode memmode;
107 static inline hashval_t hash (const value_type *);
108 static inline bool equal (const value_type *, const compare_type *);
111 /* The hash function for our hash table. The value is always computed with
112 cselib_hash_rtx when adding an element; this function just extracts the
113 hash value from a cselib_val structure. */
115 inline hashval_t
116 cselib_hasher::hash (const value_type *v)
118 return v->hash;
121 /* The equality test for our hash table. The first argument V is a table
122 element (i.e. a cselib_val), while the second arg X is an rtx. We know
123 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
124 CONST of an appropriate mode. */
126 inline bool
127 cselib_hasher::equal (const value_type *v, const compare_type *x_arg)
129 struct elt_loc_list *l;
130 rtx x = x_arg->x;
131 enum machine_mode mode = x_arg->mode;
132 enum machine_mode memmode = x_arg->memmode;
134 if (mode != GET_MODE (v->val_rtx))
135 return false;
137 if (GET_CODE (x) == VALUE)
138 return x == v->val_rtx;
140 /* We don't guarantee that distinct rtx's have different hash values,
141 so we need to do a comparison. */
142 for (l = v->locs; l; l = l->next)
143 if (rtx_equal_for_cselib_1 (l->loc, x, memmode))
145 promote_debug_loc (l);
146 return true;
149 return false;
152 /* A table that enables us to look up elts by their value. */
153 static hash_table<cselib_hasher> *cselib_hash_table;
155 /* A table to hold preserved values. */
156 static hash_table<cselib_hasher> *cselib_preserved_hash_table;
158 /* This is a global so we don't have to pass this through every function.
159 It is used in new_elt_loc_list to set SETTING_INSN. */
160 static rtx_insn *cselib_current_insn;
162 /* The unique id that the next create value will take. */
163 static unsigned int next_uid;
165 /* The number of registers we had when the varrays were last resized. */
166 static unsigned int cselib_nregs;
168 /* Count values without known locations, or with only locations that
169 wouldn't have been known except for debug insns. Whenever this
170 grows too big, we remove these useless values from the table.
172 Counting values with only debug values is a bit tricky. We don't
173 want to increment n_useless_values when we create a value for a
174 debug insn, for this would get n_useless_values out of sync, but we
175 want increment it if all locs in the list that were ever referenced
176 in nondebug insns are removed from the list.
178 In the general case, once we do that, we'd have to stop accepting
179 nondebug expressions in the loc list, to avoid having two values
180 equivalent that, without debug insns, would have been made into
181 separate values. However, because debug insns never introduce
182 equivalences themselves (no assignments), the only means for
183 growing loc lists is through nondebug assignments. If the locs
184 also happen to be referenced in debug insns, it will work just fine.
186 A consequence of this is that there's at most one debug-only loc in
187 each loc list. If we keep it in the first entry, testing whether
188 we have a debug-only loc list takes O(1).
190 Furthermore, since any additional entry in a loc list containing a
191 debug loc would have to come from an assignment (nondebug) that
192 references both the initial debug loc and the newly-equivalent loc,
193 the initial debug loc would be promoted to a nondebug loc, and the
194 loc list would not contain debug locs any more.
196 So the only case we have to be careful with in order to keep
197 n_useless_values in sync between debug and nondebug compilations is
198 to avoid incrementing n_useless_values when removing the single loc
199 from a value that turns out to not appear outside debug values. We
200 increment n_useless_debug_values instead, and leave such values
201 alone until, for other reasons, we garbage-collect useless
202 values. */
203 static int n_useless_values;
204 static int n_useless_debug_values;
206 /* Count values whose locs have been taken exclusively from debug
207 insns for the entire life of the value. */
208 static int n_debug_values;
210 /* Number of useless values before we remove them from the hash table. */
211 #define MAX_USELESS_VALUES 32
213 /* This table maps from register number to values. It does not
214 contain pointers to cselib_val structures, but rather elt_lists.
215 The purpose is to be able to refer to the same register in
216 different modes. The first element of the list defines the mode in
217 which the register was set; if the mode is unknown or the value is
218 no longer valid in that mode, ELT will be NULL for the first
219 element. */
220 static struct elt_list **reg_values;
221 static unsigned int reg_values_size;
222 #define REG_VALUES(i) reg_values[i]
224 /* The largest number of hard regs used by any entry added to the
225 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
226 static unsigned int max_value_regs;
228 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
229 in cselib_clear_table() for fast emptying. */
230 static unsigned int *used_regs;
231 static unsigned int n_used_regs;
233 /* We pass this to cselib_invalidate_mem to invalidate all of
234 memory for a non-const call instruction. */
235 static GTY(()) rtx callmem;
237 /* Set by discard_useless_locs if it deleted the last location of any
238 value. */
239 static int values_became_useless;
241 /* Used as stop element of the containing_mem list so we can check
242 presence in the list by checking the next pointer. */
243 static cselib_val dummy_val;
245 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
246 that is constant through the whole function and should never be
247 eliminated. */
248 static cselib_val *cfa_base_preserved_val;
249 static unsigned int cfa_base_preserved_regno = INVALID_REGNUM;
251 /* Used to list all values that contain memory reference.
252 May or may not contain the useless values - the list is compacted
253 each time memory is invalidated. */
254 static cselib_val *first_containing_mem = &dummy_val;
255 static alloc_pool elt_loc_list_pool, elt_list_pool, cselib_val_pool, value_pool;
257 /* If nonnull, cselib will call this function before freeing useless
258 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
259 void (*cselib_discard_hook) (cselib_val *);
261 /* If nonnull, cselib will call this function before recording sets or
262 even clobbering outputs of INSN. All the recorded sets will be
263 represented in the array sets[n_sets]. new_val_min can be used to
264 tell whether values present in sets are introduced by this
265 instruction. */
266 void (*cselib_record_sets_hook) (rtx_insn *insn, struct cselib_set *sets,
267 int n_sets);
269 #define PRESERVED_VALUE_P(RTX) \
270 (RTL_FLAG_CHECK1 ("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
272 #define SP_BASED_VALUE_P(RTX) \
273 (RTL_FLAG_CHECK1 ("SP_BASED_VALUE_P", (RTX), VALUE)->jump)
277 /* Allocate a struct elt_list and fill in its two elements with the
278 arguments. */
280 static inline struct elt_list *
281 new_elt_list (struct elt_list *next, cselib_val *elt)
283 struct elt_list *el;
284 el = (struct elt_list *) pool_alloc (elt_list_pool);
285 el->next = next;
286 el->elt = elt;
287 return el;
290 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
291 list. */
293 static inline void
294 new_elt_loc_list (cselib_val *val, rtx loc)
296 struct elt_loc_list *el, *next = val->locs;
298 gcc_checking_assert (!next || !next->setting_insn
299 || !DEBUG_INSN_P (next->setting_insn)
300 || cselib_current_insn == next->setting_insn);
302 /* If we're creating the first loc in a debug insn context, we've
303 just created a debug value. Count it. */
304 if (!next && cselib_current_insn && DEBUG_INSN_P (cselib_current_insn))
305 n_debug_values++;
307 val = canonical_cselib_val (val);
308 next = val->locs;
310 if (GET_CODE (loc) == VALUE)
312 loc = canonical_cselib_val (CSELIB_VAL_PTR (loc))->val_rtx;
314 gcc_checking_assert (PRESERVED_VALUE_P (loc)
315 == PRESERVED_VALUE_P (val->val_rtx));
317 if (val->val_rtx == loc)
318 return;
319 else if (val->uid > CSELIB_VAL_PTR (loc)->uid)
321 /* Reverse the insertion. */
322 new_elt_loc_list (CSELIB_VAL_PTR (loc), val->val_rtx);
323 return;
326 gcc_checking_assert (val->uid < CSELIB_VAL_PTR (loc)->uid);
328 if (CSELIB_VAL_PTR (loc)->locs)
330 /* Bring all locs from LOC to VAL. */
331 for (el = CSELIB_VAL_PTR (loc)->locs; el->next; el = el->next)
333 /* Adjust values that have LOC as canonical so that VAL
334 becomes their canonical. */
335 if (el->loc && GET_CODE (el->loc) == VALUE)
337 gcc_checking_assert (CSELIB_VAL_PTR (el->loc)->locs->loc
338 == loc);
339 CSELIB_VAL_PTR (el->loc)->locs->loc = val->val_rtx;
342 el->next = val->locs;
343 next = val->locs = CSELIB_VAL_PTR (loc)->locs;
346 if (CSELIB_VAL_PTR (loc)->addr_list)
348 /* Bring in addr_list into canonical node. */
349 struct elt_list *last = CSELIB_VAL_PTR (loc)->addr_list;
350 while (last->next)
351 last = last->next;
352 last->next = val->addr_list;
353 val->addr_list = CSELIB_VAL_PTR (loc)->addr_list;
354 CSELIB_VAL_PTR (loc)->addr_list = NULL;
357 if (CSELIB_VAL_PTR (loc)->next_containing_mem != NULL
358 && val->next_containing_mem == NULL)
360 /* Add VAL to the containing_mem list after LOC. LOC will
361 be removed when we notice it doesn't contain any
362 MEMs. */
363 val->next_containing_mem = CSELIB_VAL_PTR (loc)->next_containing_mem;
364 CSELIB_VAL_PTR (loc)->next_containing_mem = val;
367 /* Chain LOC back to VAL. */
368 el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
369 el->loc = val->val_rtx;
370 el->setting_insn = cselib_current_insn;
371 el->next = NULL;
372 CSELIB_VAL_PTR (loc)->locs = el;
375 el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
376 el->loc = loc;
377 el->setting_insn = cselib_current_insn;
378 el->next = next;
379 val->locs = el;
382 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
383 originating from a debug insn, maintaining the debug values
384 count. */
386 static inline void
387 promote_debug_loc (struct elt_loc_list *l)
389 if (l && l->setting_insn && DEBUG_INSN_P (l->setting_insn)
390 && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
392 n_debug_values--;
393 l->setting_insn = cselib_current_insn;
394 if (cselib_preserve_constants && l->next)
396 gcc_assert (l->next->setting_insn
397 && DEBUG_INSN_P (l->next->setting_insn)
398 && !l->next->next);
399 l->next->setting_insn = cselib_current_insn;
401 else
402 gcc_assert (!l->next);
406 /* The elt_list at *PL is no longer needed. Unchain it and free its
407 storage. */
409 static inline void
410 unchain_one_elt_list (struct elt_list **pl)
412 struct elt_list *l = *pl;
414 *pl = l->next;
415 pool_free (elt_list_pool, l);
418 /* Likewise for elt_loc_lists. */
420 static void
421 unchain_one_elt_loc_list (struct elt_loc_list **pl)
423 struct elt_loc_list *l = *pl;
425 *pl = l->next;
426 pool_free (elt_loc_list_pool, l);
429 /* Likewise for cselib_vals. This also frees the addr_list associated with
430 V. */
432 static void
433 unchain_one_value (cselib_val *v)
435 while (v->addr_list)
436 unchain_one_elt_list (&v->addr_list);
438 pool_free (cselib_val_pool, v);
441 /* Remove all entries from the hash table. Also used during
442 initialization. */
444 void
445 cselib_clear_table (void)
447 cselib_reset_table (1);
450 /* Return TRUE if V is a constant, a function invariant or a VALUE
451 equivalence; FALSE otherwise. */
453 static bool
454 invariant_or_equiv_p (cselib_val *v)
456 struct elt_loc_list *l;
458 if (v == cfa_base_preserved_val)
459 return true;
461 /* Keep VALUE equivalences around. */
462 for (l = v->locs; l; l = l->next)
463 if (GET_CODE (l->loc) == VALUE)
464 return true;
466 if (v->locs != NULL
467 && v->locs->next == NULL)
469 if (CONSTANT_P (v->locs->loc)
470 && (GET_CODE (v->locs->loc) != CONST
471 || !references_value_p (v->locs->loc, 0)))
472 return true;
473 /* Although a debug expr may be bound to different expressions,
474 we can preserve it as if it was constant, to get unification
475 and proper merging within var-tracking. */
476 if (GET_CODE (v->locs->loc) == DEBUG_EXPR
477 || GET_CODE (v->locs->loc) == DEBUG_IMPLICIT_PTR
478 || GET_CODE (v->locs->loc) == ENTRY_VALUE
479 || GET_CODE (v->locs->loc) == DEBUG_PARAMETER_REF)
480 return true;
482 /* (plus (value V) (const_int C)) is invariant iff V is invariant. */
483 if (GET_CODE (v->locs->loc) == PLUS
484 && CONST_INT_P (XEXP (v->locs->loc, 1))
485 && GET_CODE (XEXP (v->locs->loc, 0)) == VALUE
486 && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v->locs->loc, 0))))
487 return true;
490 return false;
493 /* Remove from hash table all VALUEs except constants, function
494 invariants and VALUE equivalences. */
497 preserve_constants_and_equivs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
499 cselib_val *v = *x;
501 if (invariant_or_equiv_p (v))
503 cselib_hasher::compare_type lookup = {
504 GET_MODE (v->val_rtx), v->val_rtx, VOIDmode
506 cselib_val **slot
507 = cselib_preserved_hash_table->find_slot_with_hash (&lookup,
508 v->hash, INSERT);
509 gcc_assert (!*slot);
510 *slot = v;
513 cselib_hash_table->clear_slot (x);
515 return 1;
518 /* Remove all entries from the hash table, arranging for the next
519 value to be numbered NUM. */
521 void
522 cselib_reset_table (unsigned int num)
524 unsigned int i;
526 max_value_regs = 0;
528 if (cfa_base_preserved_val)
530 unsigned int regno = cfa_base_preserved_regno;
531 unsigned int new_used_regs = 0;
532 for (i = 0; i < n_used_regs; i++)
533 if (used_regs[i] == regno)
535 new_used_regs = 1;
536 continue;
538 else
539 REG_VALUES (used_regs[i]) = 0;
540 gcc_assert (new_used_regs == 1);
541 n_used_regs = new_used_regs;
542 used_regs[0] = regno;
543 max_value_regs
544 = hard_regno_nregs[regno][GET_MODE (cfa_base_preserved_val->locs->loc)];
546 else
548 for (i = 0; i < n_used_regs; i++)
549 REG_VALUES (used_regs[i]) = 0;
550 n_used_regs = 0;
553 if (cselib_preserve_constants)
554 cselib_hash_table->traverse <void *, preserve_constants_and_equivs>
555 (NULL);
556 else
558 cselib_hash_table->empty ();
559 gcc_checking_assert (!cselib_any_perm_equivs);
562 n_useless_values = 0;
563 n_useless_debug_values = 0;
564 n_debug_values = 0;
566 next_uid = num;
568 first_containing_mem = &dummy_val;
571 /* Return the number of the next value that will be generated. */
573 unsigned int
574 cselib_get_next_uid (void)
576 return next_uid;
579 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
580 INSERTing if requested. When X is part of the address of a MEM,
581 MEMMODE should specify the mode of the MEM. */
583 static cselib_val **
584 cselib_find_slot (enum machine_mode mode, rtx x, hashval_t hash,
585 enum insert_option insert, enum machine_mode memmode)
587 cselib_val **slot = NULL;
588 cselib_hasher::compare_type lookup = { mode, x, memmode };
589 if (cselib_preserve_constants)
590 slot = cselib_preserved_hash_table->find_slot_with_hash (&lookup, hash,
591 NO_INSERT);
592 if (!slot)
593 slot = cselib_hash_table->find_slot_with_hash (&lookup, hash, insert);
594 return slot;
597 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
598 only return true for values which point to a cselib_val whose value
599 element has been set to zero, which implies the cselib_val will be
600 removed. */
603 references_value_p (const_rtx x, int only_useless)
605 const enum rtx_code code = GET_CODE (x);
606 const char *fmt = GET_RTX_FORMAT (code);
607 int i, j;
609 if (GET_CODE (x) == VALUE
610 && (! only_useless ||
611 (CSELIB_VAL_PTR (x)->locs == 0 && !PRESERVED_VALUE_P (x))))
612 return 1;
614 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
616 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
617 return 1;
618 else if (fmt[i] == 'E')
619 for (j = 0; j < XVECLEN (x, i); j++)
620 if (references_value_p (XVECEXP (x, i, j), only_useless))
621 return 1;
624 return 0;
627 /* For all locations found in X, delete locations that reference useless
628 values (i.e. values without any location). Called through
629 htab_traverse. */
632 discard_useless_locs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
634 cselib_val *v = *x;
635 struct elt_loc_list **p = &v->locs;
636 bool had_locs = v->locs != NULL;
637 rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
639 while (*p)
641 if (references_value_p ((*p)->loc, 1))
642 unchain_one_elt_loc_list (p);
643 else
644 p = &(*p)->next;
647 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
649 if (setting_insn && DEBUG_INSN_P (setting_insn))
650 n_useless_debug_values++;
651 else
652 n_useless_values++;
653 values_became_useless = 1;
655 return 1;
658 /* If X is a value with no locations, remove it from the hashtable. */
661 discard_useless_values (cselib_val **x, void *info ATTRIBUTE_UNUSED)
663 cselib_val *v = *x;
665 if (v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
667 if (cselib_discard_hook)
668 cselib_discard_hook (v);
670 CSELIB_VAL_PTR (v->val_rtx) = NULL;
671 cselib_hash_table->clear_slot (x);
672 unchain_one_value (v);
673 n_useless_values--;
676 return 1;
679 /* Clean out useless values (i.e. those which no longer have locations
680 associated with them) from the hash table. */
682 static void
683 remove_useless_values (void)
685 cselib_val **p, *v;
687 /* First pass: eliminate locations that reference the value. That in
688 turn can make more values useless. */
691 values_became_useless = 0;
692 cselib_hash_table->traverse <void *, discard_useless_locs> (NULL);
694 while (values_became_useless);
696 /* Second pass: actually remove the values. */
698 p = &first_containing_mem;
699 for (v = *p; v != &dummy_val; v = v->next_containing_mem)
700 if (v->locs && v == canonical_cselib_val (v))
702 *p = v;
703 p = &(*p)->next_containing_mem;
705 *p = &dummy_val;
707 n_useless_values += n_useless_debug_values;
708 n_debug_values -= n_useless_debug_values;
709 n_useless_debug_values = 0;
711 cselib_hash_table->traverse <void *, discard_useless_values> (NULL);
713 gcc_assert (!n_useless_values);
716 /* Arrange for a value to not be removed from the hash table even if
717 it becomes useless. */
719 void
720 cselib_preserve_value (cselib_val *v)
722 PRESERVED_VALUE_P (v->val_rtx) = 1;
725 /* Test whether a value is preserved. */
727 bool
728 cselib_preserved_value_p (cselib_val *v)
730 return PRESERVED_VALUE_P (v->val_rtx);
733 /* Arrange for a REG value to be assumed constant through the whole function,
734 never invalidated and preserved across cselib_reset_table calls. */
736 void
737 cselib_preserve_cfa_base_value (cselib_val *v, unsigned int regno)
739 if (cselib_preserve_constants
740 && v->locs
741 && REG_P (v->locs->loc))
743 cfa_base_preserved_val = v;
744 cfa_base_preserved_regno = regno;
748 /* Clean all non-constant expressions in the hash table, but retain
749 their values. */
751 void
752 cselib_preserve_only_values (void)
754 int i;
756 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
757 cselib_invalidate_regno (i, reg_raw_mode[i]);
759 cselib_invalidate_mem (callmem);
761 remove_useless_values ();
763 gcc_assert (first_containing_mem == &dummy_val);
766 /* Arrange for a value to be marked as based on stack pointer
767 for find_base_term purposes. */
769 void
770 cselib_set_value_sp_based (cselib_val *v)
772 SP_BASED_VALUE_P (v->val_rtx) = 1;
775 /* Test whether a value is based on stack pointer for
776 find_base_term purposes. */
778 bool
779 cselib_sp_based_value_p (cselib_val *v)
781 return SP_BASED_VALUE_P (v->val_rtx);
784 /* Return the mode in which a register was last set. If X is not a
785 register, return its mode. If the mode in which the register was
786 set is not known, or the value was already clobbered, return
787 VOIDmode. */
789 enum machine_mode
790 cselib_reg_set_mode (const_rtx x)
792 if (!REG_P (x))
793 return GET_MODE (x);
795 if (REG_VALUES (REGNO (x)) == NULL
796 || REG_VALUES (REGNO (x))->elt == NULL)
797 return VOIDmode;
799 return GET_MODE (REG_VALUES (REGNO (x))->elt->val_rtx);
802 /* Return nonzero if we can prove that X and Y contain the same value, taking
803 our gathered information into account. */
806 rtx_equal_for_cselib_p (rtx x, rtx y)
808 return rtx_equal_for_cselib_1 (x, y, VOIDmode);
811 /* If x is a PLUS or an autoinc operation, expand the operation,
812 storing the offset, if any, in *OFF. */
814 static rtx
815 autoinc_split (rtx x, rtx *off, enum machine_mode memmode)
817 switch (GET_CODE (x))
819 case PLUS:
820 *off = XEXP (x, 1);
821 return XEXP (x, 0);
823 case PRE_DEC:
824 if (memmode == VOIDmode)
825 return x;
827 *off = GEN_INT (-GET_MODE_SIZE (memmode));
828 return XEXP (x, 0);
829 break;
831 case PRE_INC:
832 if (memmode == VOIDmode)
833 return x;
835 *off = GEN_INT (GET_MODE_SIZE (memmode));
836 return XEXP (x, 0);
838 case PRE_MODIFY:
839 return XEXP (x, 1);
841 case POST_DEC:
842 case POST_INC:
843 case POST_MODIFY:
844 return XEXP (x, 0);
846 default:
847 return x;
851 /* Return nonzero if we can prove that X and Y contain the same value,
852 taking our gathered information into account. MEMMODE holds the
853 mode of the enclosing MEM, if any, as required to deal with autoinc
854 addressing modes. If X and Y are not (known to be) part of
855 addresses, MEMMODE should be VOIDmode. */
857 static int
858 rtx_equal_for_cselib_1 (rtx x, rtx y, enum machine_mode memmode)
860 enum rtx_code code;
861 const char *fmt;
862 int i;
864 if (REG_P (x) || MEM_P (x))
866 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0, memmode);
868 if (e)
869 x = e->val_rtx;
872 if (REG_P (y) || MEM_P (y))
874 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0, memmode);
876 if (e)
877 y = e->val_rtx;
880 if (x == y)
881 return 1;
883 if (GET_CODE (x) == VALUE)
885 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (x));
886 struct elt_loc_list *l;
888 if (GET_CODE (y) == VALUE)
889 return e == canonical_cselib_val (CSELIB_VAL_PTR (y));
891 for (l = e->locs; l; l = l->next)
893 rtx t = l->loc;
895 /* Avoid infinite recursion. We know we have the canonical
896 value, so we can just skip any values in the equivalence
897 list. */
898 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
899 continue;
900 else if (rtx_equal_for_cselib_1 (t, y, memmode))
901 return 1;
904 return 0;
906 else if (GET_CODE (y) == VALUE)
908 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (y));
909 struct elt_loc_list *l;
911 for (l = e->locs; l; l = l->next)
913 rtx t = l->loc;
915 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
916 continue;
917 else if (rtx_equal_for_cselib_1 (x, t, memmode))
918 return 1;
921 return 0;
924 if (GET_MODE (x) != GET_MODE (y))
925 return 0;
927 if (GET_CODE (x) != GET_CODE (y))
929 rtx xorig = x, yorig = y;
930 rtx xoff = NULL, yoff = NULL;
932 x = autoinc_split (x, &xoff, memmode);
933 y = autoinc_split (y, &yoff, memmode);
935 if (!xoff != !yoff)
936 return 0;
938 if (xoff && !rtx_equal_for_cselib_1 (xoff, yoff, memmode))
939 return 0;
941 /* Don't recurse if nothing changed. */
942 if (x != xorig || y != yorig)
943 return rtx_equal_for_cselib_1 (x, y, memmode);
945 return 0;
948 /* These won't be handled correctly by the code below. */
949 switch (GET_CODE (x))
951 CASE_CONST_UNIQUE:
952 case DEBUG_EXPR:
953 return 0;
955 case DEBUG_IMPLICIT_PTR:
956 return DEBUG_IMPLICIT_PTR_DECL (x)
957 == DEBUG_IMPLICIT_PTR_DECL (y);
959 case DEBUG_PARAMETER_REF:
960 return DEBUG_PARAMETER_REF_DECL (x)
961 == DEBUG_PARAMETER_REF_DECL (y);
963 case ENTRY_VALUE:
964 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
965 use rtx_equal_for_cselib_1 to compare the operands. */
966 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
968 case LABEL_REF:
969 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
971 case MEM:
972 /* We have to compare any autoinc operations in the addresses
973 using this MEM's mode. */
974 return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x));
976 default:
977 break;
980 code = GET_CODE (x);
981 fmt = GET_RTX_FORMAT (code);
983 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
985 int j;
987 switch (fmt[i])
989 case 'w':
990 if (XWINT (x, i) != XWINT (y, i))
991 return 0;
992 break;
994 case 'n':
995 case 'i':
996 if (XINT (x, i) != XINT (y, i))
997 return 0;
998 break;
1000 case 'V':
1001 case 'E':
1002 /* Two vectors must have the same length. */
1003 if (XVECLEN (x, i) != XVECLEN (y, i))
1004 return 0;
1006 /* And the corresponding elements must match. */
1007 for (j = 0; j < XVECLEN (x, i); j++)
1008 if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
1009 XVECEXP (y, i, j), memmode))
1010 return 0;
1011 break;
1013 case 'e':
1014 if (i == 1
1015 && targetm.commutative_p (x, UNKNOWN)
1016 && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode)
1017 && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode))
1018 return 1;
1019 if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode))
1020 return 0;
1021 break;
1023 case 'S':
1024 case 's':
1025 if (strcmp (XSTR (x, i), XSTR (y, i)))
1026 return 0;
1027 break;
1029 case 'u':
1030 /* These are just backpointers, so they don't matter. */
1031 break;
1033 case '0':
1034 case 't':
1035 break;
1037 /* It is believed that rtx's at this level will never
1038 contain anything but integers and other rtx's,
1039 except for within LABEL_REFs and SYMBOL_REFs. */
1040 default:
1041 gcc_unreachable ();
1044 return 1;
1047 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1048 For registers and memory locations, we look up their cselib_val structure
1049 and return its VALUE element.
1050 Possible reasons for return 0 are: the object is volatile, or we couldn't
1051 find a register or memory location in the table and CREATE is zero. If
1052 CREATE is nonzero, table elts are created for regs and mem.
1053 N.B. this hash function returns the same hash value for RTXes that
1054 differ only in the order of operands, thus it is suitable for comparisons
1055 that take commutativity into account.
1056 If we wanted to also support associative rules, we'd have to use a different
1057 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1058 MEMMODE indicates the mode of an enclosing MEM, and it's only
1059 used to compute autoinc values.
1060 We used to have a MODE argument for hashing for CONST_INTs, but that
1061 didn't make sense, since it caused spurious hash differences between
1062 (set (reg:SI 1) (const_int))
1063 (plus:SI (reg:SI 2) (reg:SI 1))
1065 (plus:SI (reg:SI 2) (const_int))
1066 If the mode is important in any context, it must be checked specifically
1067 in a comparison anyway, since relying on hash differences is unsafe. */
1069 static unsigned int
1070 cselib_hash_rtx (rtx x, int create, enum machine_mode memmode)
1072 cselib_val *e;
1073 int i, j;
1074 enum rtx_code code;
1075 const char *fmt;
1076 unsigned int hash = 0;
1078 code = GET_CODE (x);
1079 hash += (unsigned) code + (unsigned) GET_MODE (x);
1081 switch (code)
1083 case VALUE:
1084 e = CSELIB_VAL_PTR (x);
1085 return e->hash;
1087 case MEM:
1088 case REG:
1089 e = cselib_lookup (x, GET_MODE (x), create, memmode);
1090 if (! e)
1091 return 0;
1093 return e->hash;
1095 case DEBUG_EXPR:
1096 hash += ((unsigned) DEBUG_EXPR << 7)
1097 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
1098 return hash ? hash : (unsigned int) DEBUG_EXPR;
1100 case DEBUG_IMPLICIT_PTR:
1101 hash += ((unsigned) DEBUG_IMPLICIT_PTR << 7)
1102 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x));
1103 return hash ? hash : (unsigned int) DEBUG_IMPLICIT_PTR;
1105 case DEBUG_PARAMETER_REF:
1106 hash += ((unsigned) DEBUG_PARAMETER_REF << 7)
1107 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x));
1108 return hash ? hash : (unsigned int) DEBUG_PARAMETER_REF;
1110 case ENTRY_VALUE:
1111 /* ENTRY_VALUEs are function invariant, thus try to avoid
1112 recursing on argument if ENTRY_VALUE is one of the
1113 forms emitted by expand_debug_expr, otherwise
1114 ENTRY_VALUE hash would depend on the current value
1115 in some register or memory. */
1116 if (REG_P (ENTRY_VALUE_EXP (x)))
1117 hash += (unsigned int) REG
1118 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x))
1119 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x));
1120 else if (MEM_P (ENTRY_VALUE_EXP (x))
1121 && REG_P (XEXP (ENTRY_VALUE_EXP (x), 0)))
1122 hash += (unsigned int) MEM
1123 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x), 0))
1124 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x), 0));
1125 else
1126 hash += cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode);
1127 return hash ? hash : (unsigned int) ENTRY_VALUE;
1129 case CONST_INT:
1130 hash += ((unsigned) CONST_INT << 7) + UINTVAL (x);
1131 return hash ? hash : (unsigned int) CONST_INT;
1133 case CONST_WIDE_INT:
1134 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
1135 hash += CONST_WIDE_INT_ELT (x, i);
1136 return hash;
1138 case CONST_DOUBLE:
1139 /* This is like the general case, except that it only counts
1140 the integers representing the constant. */
1141 hash += (unsigned) code + (unsigned) GET_MODE (x);
1142 if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (x) == VOIDmode)
1143 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1144 + (unsigned) CONST_DOUBLE_HIGH (x));
1145 else
1146 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
1147 return hash ? hash : (unsigned int) CONST_DOUBLE;
1149 case CONST_FIXED:
1150 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1151 hash += fixed_hash (CONST_FIXED_VALUE (x));
1152 return hash ? hash : (unsigned int) CONST_FIXED;
1154 case CONST_VECTOR:
1156 int units;
1157 rtx elt;
1159 units = CONST_VECTOR_NUNITS (x);
1161 for (i = 0; i < units; ++i)
1163 elt = CONST_VECTOR_ELT (x, i);
1164 hash += cselib_hash_rtx (elt, 0, memmode);
1167 return hash;
1170 /* Assume there is only one rtx object for any given label. */
1171 case LABEL_REF:
1172 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1173 differences and differences between each stage's debugging dumps. */
1174 hash += (((unsigned int) LABEL_REF << 7)
1175 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x)));
1176 return hash ? hash : (unsigned int) LABEL_REF;
1178 case SYMBOL_REF:
1180 /* Don't hash on the symbol's address to avoid bootstrap differences.
1181 Different hash values may cause expressions to be recorded in
1182 different orders and thus different registers to be used in the
1183 final assembler. This also avoids differences in the dump files
1184 between various stages. */
1185 unsigned int h = 0;
1186 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1188 while (*p)
1189 h += (h << 7) + *p++; /* ??? revisit */
1191 hash += ((unsigned int) SYMBOL_REF << 7) + h;
1192 return hash ? hash : (unsigned int) SYMBOL_REF;
1195 case PRE_DEC:
1196 case PRE_INC:
1197 /* We can't compute these without knowing the MEM mode. */
1198 gcc_assert (memmode != VOIDmode);
1199 i = GET_MODE_SIZE (memmode);
1200 if (code == PRE_DEC)
1201 i = -i;
1202 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1203 like (mem:MEMMODE (plus (reg) (const_int I))). */
1204 hash += (unsigned) PLUS - (unsigned)code
1205 + cselib_hash_rtx (XEXP (x, 0), create, memmode)
1206 + cselib_hash_rtx (GEN_INT (i), create, memmode);
1207 return hash ? hash : 1 + (unsigned) PLUS;
1209 case PRE_MODIFY:
1210 gcc_assert (memmode != VOIDmode);
1211 return cselib_hash_rtx (XEXP (x, 1), create, memmode);
1213 case POST_DEC:
1214 case POST_INC:
1215 case POST_MODIFY:
1216 gcc_assert (memmode != VOIDmode);
1217 return cselib_hash_rtx (XEXP (x, 0), create, memmode);
1219 case PC:
1220 case CC0:
1221 case CALL:
1222 case UNSPEC_VOLATILE:
1223 return 0;
1225 case ASM_OPERANDS:
1226 if (MEM_VOLATILE_P (x))
1227 return 0;
1229 break;
1231 default:
1232 break;
1235 i = GET_RTX_LENGTH (code) - 1;
1236 fmt = GET_RTX_FORMAT (code);
1237 for (; i >= 0; i--)
1239 switch (fmt[i])
1241 case 'e':
1243 rtx tem = XEXP (x, i);
1244 unsigned int tem_hash = cselib_hash_rtx (tem, create, memmode);
1246 if (tem_hash == 0)
1247 return 0;
1249 hash += tem_hash;
1251 break;
1252 case 'E':
1253 for (j = 0; j < XVECLEN (x, i); j++)
1255 unsigned int tem_hash
1256 = cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
1258 if (tem_hash == 0)
1259 return 0;
1261 hash += tem_hash;
1263 break;
1265 case 's':
1267 const unsigned char *p = (const unsigned char *) XSTR (x, i);
1269 if (p)
1270 while (*p)
1271 hash += *p++;
1272 break;
1275 case 'i':
1276 hash += XINT (x, i);
1277 break;
1279 case '0':
1280 case 't':
1281 /* unused */
1282 break;
1284 default:
1285 gcc_unreachable ();
1289 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
1292 /* Create a new value structure for VALUE and initialize it. The mode of the
1293 value is MODE. */
1295 static inline cselib_val *
1296 new_cselib_val (unsigned int hash, enum machine_mode mode, rtx x)
1298 cselib_val *e = (cselib_val *) pool_alloc (cselib_val_pool);
1300 gcc_assert (hash);
1301 gcc_assert (next_uid);
1303 e->hash = hash;
1304 e->uid = next_uid++;
1305 /* We use an alloc pool to allocate this RTL construct because it
1306 accounts for about 8% of the overall memory usage. We know
1307 precisely when we can have VALUE RTXen (when cselib is active)
1308 so we don't need to put them in garbage collected memory.
1309 ??? Why should a VALUE be an RTX in the first place? */
1310 e->val_rtx = (rtx) pool_alloc (value_pool);
1311 memset (e->val_rtx, 0, RTX_HDR_SIZE);
1312 PUT_CODE (e->val_rtx, VALUE);
1313 PUT_MODE (e->val_rtx, mode);
1314 CSELIB_VAL_PTR (e->val_rtx) = e;
1315 e->addr_list = 0;
1316 e->locs = 0;
1317 e->next_containing_mem = 0;
1319 if (dump_file && (dump_flags & TDF_CSELIB))
1321 fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
1322 if (flag_dump_noaddr || flag_dump_unnumbered)
1323 fputs ("# ", dump_file);
1324 else
1325 fprintf (dump_file, "%p ", (void*)e);
1326 print_rtl_single (dump_file, x);
1327 fputc ('\n', dump_file);
1330 return e;
1333 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1334 contains the data at this address. X is a MEM that represents the
1335 value. Update the two value structures to represent this situation. */
1337 static void
1338 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
1340 struct elt_loc_list *l;
1342 addr_elt = canonical_cselib_val (addr_elt);
1343 mem_elt = canonical_cselib_val (mem_elt);
1345 /* Avoid duplicates. */
1346 for (l = mem_elt->locs; l; l = l->next)
1347 if (MEM_P (l->loc)
1348 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
1350 promote_debug_loc (l);
1351 return;
1354 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
1355 new_elt_loc_list (mem_elt,
1356 replace_equiv_address_nv (x, addr_elt->val_rtx));
1357 if (mem_elt->next_containing_mem == NULL)
1359 mem_elt->next_containing_mem = first_containing_mem;
1360 first_containing_mem = mem_elt;
1364 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1365 If CREATE, make a new one if we haven't seen it before. */
1367 static cselib_val *
1368 cselib_lookup_mem (rtx x, int create)
1370 enum machine_mode mode = GET_MODE (x);
1371 enum machine_mode addr_mode;
1372 cselib_val **slot;
1373 cselib_val *addr;
1374 cselib_val *mem_elt;
1375 struct elt_list *l;
1377 if (MEM_VOLATILE_P (x) || mode == BLKmode
1378 || !cselib_record_memory
1379 || (FLOAT_MODE_P (mode) && flag_float_store))
1380 return 0;
1382 addr_mode = GET_MODE (XEXP (x, 0));
1383 if (addr_mode == VOIDmode)
1384 addr_mode = Pmode;
1386 /* Look up the value for the address. */
1387 addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
1388 if (! addr)
1389 return 0;
1391 addr = canonical_cselib_val (addr);
1392 /* Find a value that describes a value of our mode at that address. */
1393 for (l = addr->addr_list; l; l = l->next)
1394 if (GET_MODE (l->elt->val_rtx) == mode)
1396 promote_debug_loc (l->elt->locs);
1397 return l->elt;
1400 if (! create)
1401 return 0;
1403 mem_elt = new_cselib_val (next_uid, mode, x);
1404 add_mem_for_addr (addr, mem_elt, x);
1405 slot = cselib_find_slot (mode, x, mem_elt->hash, INSERT, VOIDmode);
1406 *slot = mem_elt;
1407 return mem_elt;
1410 /* Search through the possible substitutions in P. We prefer a non reg
1411 substitution because this allows us to expand the tree further. If
1412 we find, just a reg, take the lowest regno. There may be several
1413 non-reg results, we just take the first one because they will all
1414 expand to the same place. */
1416 static rtx
1417 expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1418 int max_depth)
1420 rtx reg_result = NULL;
1421 unsigned int regno = UINT_MAX;
1422 struct elt_loc_list *p_in = p;
1424 for (; p; p = p->next)
1426 /* Return these right away to avoid returning stack pointer based
1427 expressions for frame pointer and vice versa, which is something
1428 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1429 for more details. */
1430 if (REG_P (p->loc)
1431 && (REGNO (p->loc) == STACK_POINTER_REGNUM
1432 || REGNO (p->loc) == FRAME_POINTER_REGNUM
1433 || REGNO (p->loc) == HARD_FRAME_POINTER_REGNUM
1434 || REGNO (p->loc) == cfa_base_preserved_regno))
1435 return p->loc;
1436 /* Avoid infinite recursion trying to expand a reg into a
1437 the same reg. */
1438 if ((REG_P (p->loc))
1439 && (REGNO (p->loc) < regno)
1440 && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
1442 reg_result = p->loc;
1443 regno = REGNO (p->loc);
1445 /* Avoid infinite recursion and do not try to expand the
1446 value. */
1447 else if (GET_CODE (p->loc) == VALUE
1448 && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1449 continue;
1450 else if (!REG_P (p->loc))
1452 rtx result, note;
1453 if (dump_file && (dump_flags & TDF_CSELIB))
1455 print_inline_rtx (dump_file, p->loc, 0);
1456 fprintf (dump_file, "\n");
1458 if (GET_CODE (p->loc) == LO_SUM
1459 && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1460 && p->setting_insn
1461 && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1462 && XEXP (note, 0) == XEXP (p->loc, 1))
1463 return XEXP (p->loc, 1);
1464 result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
1465 if (result)
1466 return result;
1471 if (regno != UINT_MAX)
1473 rtx result;
1474 if (dump_file && (dump_flags & TDF_CSELIB))
1475 fprintf (dump_file, "r%d\n", regno);
1477 result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
1478 if (result)
1479 return result;
1482 if (dump_file && (dump_flags & TDF_CSELIB))
1484 if (reg_result)
1486 print_inline_rtx (dump_file, reg_result, 0);
1487 fprintf (dump_file, "\n");
1489 else
1490 fprintf (dump_file, "NULL\n");
1492 return reg_result;
1496 /* Forward substitute and expand an expression out to its roots.
1497 This is the opposite of common subexpression. Because local value
1498 numbering is such a weak optimization, the expanded expression is
1499 pretty much unique (not from a pointer equals point of view but
1500 from a tree shape point of view.
1502 This function returns NULL if the expansion fails. The expansion
1503 will fail if there is no value number for one of the operands or if
1504 one of the operands has been overwritten between the current insn
1505 and the beginning of the basic block. For instance x has no
1506 expansion in:
1508 r1 <- r1 + 3
1509 x <- r1 + 8
1511 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1512 It is clear on return. */
1515 cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
1517 struct expand_value_data evd;
1519 evd.regs_active = regs_active;
1520 evd.callback = NULL;
1521 evd.callback_arg = NULL;
1522 evd.dummy = false;
1524 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1527 /* Same as cselib_expand_value_rtx, but using a callback to try to
1528 resolve some expressions. The CB function should return ORIG if it
1529 can't or does not want to deal with a certain RTX. Any other
1530 return value, including NULL, will be used as the expansion for
1531 VALUE, without any further changes. */
1534 cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1535 cselib_expand_callback cb, void *data)
1537 struct expand_value_data evd;
1539 evd.regs_active = regs_active;
1540 evd.callback = cb;
1541 evd.callback_arg = data;
1542 evd.dummy = false;
1544 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1547 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1548 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1549 would return NULL or non-NULL, without allocating new rtx. */
1551 bool
1552 cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1553 cselib_expand_callback cb, void *data)
1555 struct expand_value_data evd;
1557 evd.regs_active = regs_active;
1558 evd.callback = cb;
1559 evd.callback_arg = data;
1560 evd.dummy = true;
1562 return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1565 /* Internal implementation of cselib_expand_value_rtx and
1566 cselib_expand_value_rtx_cb. */
1568 static rtx
1569 cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1570 int max_depth)
1572 rtx copy, scopy;
1573 int i, j;
1574 RTX_CODE code;
1575 const char *format_ptr;
1576 enum machine_mode mode;
1578 code = GET_CODE (orig);
1580 /* For the context of dse, if we end up expand into a huge tree, we
1581 will not have a useful address, so we might as well just give up
1582 quickly. */
1583 if (max_depth <= 0)
1584 return NULL;
1586 switch (code)
1588 case REG:
1590 struct elt_list *l = REG_VALUES (REGNO (orig));
1592 if (l && l->elt == NULL)
1593 l = l->next;
1594 for (; l; l = l->next)
1595 if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1597 rtx result;
1598 unsigned regno = REGNO (orig);
1600 /* The only thing that we are not willing to do (this
1601 is requirement of dse and if others potential uses
1602 need this function we should add a parm to control
1603 it) is that we will not substitute the
1604 STACK_POINTER_REGNUM, FRAME_POINTER or the
1605 HARD_FRAME_POINTER.
1607 These expansions confuses the code that notices that
1608 stores into the frame go dead at the end of the
1609 function and that the frame is not effected by calls
1610 to subroutines. If you allow the
1611 STACK_POINTER_REGNUM substitution, then dse will
1612 think that parameter pushing also goes dead which is
1613 wrong. If you allow the FRAME_POINTER or the
1614 HARD_FRAME_POINTER then you lose the opportunity to
1615 make the frame assumptions. */
1616 if (regno == STACK_POINTER_REGNUM
1617 || regno == FRAME_POINTER_REGNUM
1618 || regno == HARD_FRAME_POINTER_REGNUM
1619 || regno == cfa_base_preserved_regno)
1620 return orig;
1622 bitmap_set_bit (evd->regs_active, regno);
1624 if (dump_file && (dump_flags & TDF_CSELIB))
1625 fprintf (dump_file, "expanding: r%d into: ", regno);
1627 result = expand_loc (l->elt->locs, evd, max_depth);
1628 bitmap_clear_bit (evd->regs_active, regno);
1630 if (result)
1631 return result;
1632 else
1633 return orig;
1637 CASE_CONST_ANY:
1638 case SYMBOL_REF:
1639 case CODE_LABEL:
1640 case PC:
1641 case CC0:
1642 case SCRATCH:
1643 /* SCRATCH must be shared because they represent distinct values. */
1644 return orig;
1645 case CLOBBER:
1646 if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1647 return orig;
1648 break;
1650 case CONST:
1651 if (shared_const_p (orig))
1652 return orig;
1653 break;
1655 case SUBREG:
1657 rtx subreg;
1659 if (evd->callback)
1661 subreg = evd->callback (orig, evd->regs_active, max_depth,
1662 evd->callback_arg);
1663 if (subreg != orig)
1664 return subreg;
1667 subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1668 max_depth - 1);
1669 if (!subreg)
1670 return NULL;
1671 scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1672 GET_MODE (SUBREG_REG (orig)),
1673 SUBREG_BYTE (orig));
1674 if (scopy == NULL
1675 || (GET_CODE (scopy) == SUBREG
1676 && !REG_P (SUBREG_REG (scopy))
1677 && !MEM_P (SUBREG_REG (scopy))))
1678 return NULL;
1680 return scopy;
1683 case VALUE:
1685 rtx result;
1687 if (dump_file && (dump_flags & TDF_CSELIB))
1689 fputs ("\nexpanding ", dump_file);
1690 print_rtl_single (dump_file, orig);
1691 fputs (" into...", dump_file);
1694 if (evd->callback)
1696 result = evd->callback (orig, evd->regs_active, max_depth,
1697 evd->callback_arg);
1699 if (result != orig)
1700 return result;
1703 result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
1704 return result;
1707 case DEBUG_EXPR:
1708 if (evd->callback)
1709 return evd->callback (orig, evd->regs_active, max_depth,
1710 evd->callback_arg);
1711 return orig;
1713 default:
1714 break;
1717 /* Copy the various flags, fields, and other information. We assume
1718 that all fields need copying, and then clear the fields that should
1719 not be copied. That is the sensible default behavior, and forces
1720 us to explicitly document why we are *not* copying a flag. */
1721 if (evd->dummy)
1722 copy = NULL;
1723 else
1724 copy = shallow_copy_rtx (orig);
1726 format_ptr = GET_RTX_FORMAT (code);
1728 for (i = 0; i < GET_RTX_LENGTH (code); i++)
1729 switch (*format_ptr++)
1731 case 'e':
1732 if (XEXP (orig, i) != NULL)
1734 rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1735 max_depth - 1);
1736 if (!result)
1737 return NULL;
1738 if (copy)
1739 XEXP (copy, i) = result;
1741 break;
1743 case 'E':
1744 case 'V':
1745 if (XVEC (orig, i) != NULL)
1747 if (copy)
1748 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1749 for (j = 0; j < XVECLEN (orig, i); j++)
1751 rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1752 evd, max_depth - 1);
1753 if (!result)
1754 return NULL;
1755 if (copy)
1756 XVECEXP (copy, i, j) = result;
1759 break;
1761 case 't':
1762 case 'w':
1763 case 'i':
1764 case 's':
1765 case 'S':
1766 case 'T':
1767 case 'u':
1768 case 'B':
1769 case '0':
1770 /* These are left unchanged. */
1771 break;
1773 default:
1774 gcc_unreachable ();
1777 if (evd->dummy)
1778 return orig;
1780 mode = GET_MODE (copy);
1781 /* If an operand has been simplified into CONST_INT, which doesn't
1782 have a mode and the mode isn't derivable from whole rtx's mode,
1783 try simplify_*_operation first with mode from original's operand
1784 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1785 scopy = copy;
1786 switch (GET_RTX_CLASS (code))
1788 case RTX_UNARY:
1789 if (CONST_INT_P (XEXP (copy, 0))
1790 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1792 scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1793 GET_MODE (XEXP (orig, 0)));
1794 if (scopy)
1795 return scopy;
1797 break;
1798 case RTX_COMM_ARITH:
1799 case RTX_BIN_ARITH:
1800 /* These expressions can derive operand modes from the whole rtx's mode. */
1801 break;
1802 case RTX_TERNARY:
1803 case RTX_BITFIELD_OPS:
1804 if (CONST_INT_P (XEXP (copy, 0))
1805 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1807 scopy = simplify_ternary_operation (code, mode,
1808 GET_MODE (XEXP (orig, 0)),
1809 XEXP (copy, 0), XEXP (copy, 1),
1810 XEXP (copy, 2));
1811 if (scopy)
1812 return scopy;
1814 break;
1815 case RTX_COMPARE:
1816 case RTX_COMM_COMPARE:
1817 if (CONST_INT_P (XEXP (copy, 0))
1818 && GET_MODE (XEXP (copy, 1)) == VOIDmode
1819 && (GET_MODE (XEXP (orig, 0)) != VOIDmode
1820 || GET_MODE (XEXP (orig, 1)) != VOIDmode))
1822 scopy = simplify_relational_operation (code, mode,
1823 (GET_MODE (XEXP (orig, 0))
1824 != VOIDmode)
1825 ? GET_MODE (XEXP (orig, 0))
1826 : GET_MODE (XEXP (orig, 1)),
1827 XEXP (copy, 0),
1828 XEXP (copy, 1));
1829 if (scopy)
1830 return scopy;
1832 break;
1833 default:
1834 break;
1836 scopy = simplify_rtx (copy);
1837 if (scopy)
1838 return scopy;
1839 return copy;
1842 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1843 with VALUE expressions. This way, it becomes independent of changes
1844 to registers and memory.
1845 X isn't actually modified; if modifications are needed, new rtl is
1846 allocated. However, the return value can share rtl with X.
1847 If X is within a MEM, MEMMODE must be the mode of the MEM. */
1850 cselib_subst_to_values (rtx x, enum machine_mode memmode)
1852 enum rtx_code code = GET_CODE (x);
1853 const char *fmt = GET_RTX_FORMAT (code);
1854 cselib_val *e;
1855 struct elt_list *l;
1856 rtx copy = x;
1857 int i;
1859 switch (code)
1861 case REG:
1862 l = REG_VALUES (REGNO (x));
1863 if (l && l->elt == NULL)
1864 l = l->next;
1865 for (; l; l = l->next)
1866 if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
1867 return l->elt->val_rtx;
1869 gcc_unreachable ();
1871 case MEM:
1872 e = cselib_lookup_mem (x, 0);
1873 /* This used to happen for autoincrements, but we deal with them
1874 properly now. Remove the if stmt for the next release. */
1875 if (! e)
1877 /* Assign a value that doesn't match any other. */
1878 e = new_cselib_val (next_uid, GET_MODE (x), x);
1880 return e->val_rtx;
1882 case ENTRY_VALUE:
1883 e = cselib_lookup (x, GET_MODE (x), 0, memmode);
1884 if (! e)
1885 break;
1886 return e->val_rtx;
1888 CASE_CONST_ANY:
1889 return x;
1891 case PRE_DEC:
1892 case PRE_INC:
1893 gcc_assert (memmode != VOIDmode);
1894 i = GET_MODE_SIZE (memmode);
1895 if (code == PRE_DEC)
1896 i = -i;
1897 return cselib_subst_to_values (plus_constant (GET_MODE (x),
1898 XEXP (x, 0), i),
1899 memmode);
1901 case PRE_MODIFY:
1902 gcc_assert (memmode != VOIDmode);
1903 return cselib_subst_to_values (XEXP (x, 1), memmode);
1905 case POST_DEC:
1906 case POST_INC:
1907 case POST_MODIFY:
1908 gcc_assert (memmode != VOIDmode);
1909 return cselib_subst_to_values (XEXP (x, 0), memmode);
1911 default:
1912 break;
1915 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1917 if (fmt[i] == 'e')
1919 rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
1921 if (t != XEXP (x, i))
1923 if (x == copy)
1924 copy = shallow_copy_rtx (x);
1925 XEXP (copy, i) = t;
1928 else if (fmt[i] == 'E')
1930 int j;
1932 for (j = 0; j < XVECLEN (x, i); j++)
1934 rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
1936 if (t != XVECEXP (x, i, j))
1938 if (XVEC (x, i) == XVEC (copy, i))
1940 if (x == copy)
1941 copy = shallow_copy_rtx (x);
1942 XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
1944 XVECEXP (copy, i, j) = t;
1950 return copy;
1953 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
1956 cselib_subst_to_values_from_insn (rtx x, enum machine_mode memmode, rtx_insn *insn)
1958 rtx ret;
1959 gcc_assert (!cselib_current_insn);
1960 cselib_current_insn = insn;
1961 ret = cselib_subst_to_values (x, memmode);
1962 cselib_current_insn = NULL;
1963 return ret;
1966 /* Look up the rtl expression X in our tables and return the value it
1967 has. If CREATE is zero, we return NULL if we don't know the value.
1968 Otherwise, we create a new one if possible, using mode MODE if X
1969 doesn't have a mode (i.e. because it's a constant). When X is part
1970 of an address, MEMMODE should be the mode of the enclosing MEM if
1971 we're tracking autoinc expressions. */
1973 static cselib_val *
1974 cselib_lookup_1 (rtx x, enum machine_mode mode,
1975 int create, enum machine_mode memmode)
1977 cselib_val **slot;
1978 cselib_val *e;
1979 unsigned int hashval;
1981 if (GET_MODE (x) != VOIDmode)
1982 mode = GET_MODE (x);
1984 if (GET_CODE (x) == VALUE)
1985 return CSELIB_VAL_PTR (x);
1987 if (REG_P (x))
1989 struct elt_list *l;
1990 unsigned int i = REGNO (x);
1992 l = REG_VALUES (i);
1993 if (l && l->elt == NULL)
1994 l = l->next;
1995 for (; l; l = l->next)
1996 if (mode == GET_MODE (l->elt->val_rtx))
1998 promote_debug_loc (l->elt->locs);
1999 return l->elt;
2002 if (! create)
2003 return 0;
2005 if (i < FIRST_PSEUDO_REGISTER)
2007 unsigned int n = hard_regno_nregs[i][mode];
2009 if (n > max_value_regs)
2010 max_value_regs = n;
2013 e = new_cselib_val (next_uid, GET_MODE (x), x);
2014 new_elt_loc_list (e, x);
2015 if (REG_VALUES (i) == 0)
2017 /* Maintain the invariant that the first entry of
2018 REG_VALUES, if present, must be the value used to set the
2019 register, or NULL. */
2020 used_regs[n_used_regs++] = i;
2021 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
2023 else if (cselib_preserve_constants
2024 && GET_MODE_CLASS (mode) == MODE_INT)
2026 /* During var-tracking, try harder to find equivalences
2027 for SUBREGs. If a setter sets say a DImode register
2028 and user uses that register only in SImode, add a lowpart
2029 subreg location. */
2030 struct elt_list *lwider = NULL;
2031 l = REG_VALUES (i);
2032 if (l && l->elt == NULL)
2033 l = l->next;
2034 for (; l; l = l->next)
2035 if (GET_MODE_CLASS (GET_MODE (l->elt->val_rtx)) == MODE_INT
2036 && GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2037 > GET_MODE_SIZE (mode)
2038 && (lwider == NULL
2039 || GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2040 < GET_MODE_SIZE (GET_MODE (lwider->elt->val_rtx))))
2042 struct elt_loc_list *el;
2043 if (i < FIRST_PSEUDO_REGISTER
2044 && hard_regno_nregs[i][GET_MODE (l->elt->val_rtx)] != 1)
2045 continue;
2046 for (el = l->elt->locs; el; el = el->next)
2047 if (!REG_P (el->loc))
2048 break;
2049 if (el)
2050 lwider = l;
2052 if (lwider)
2054 rtx sub = lowpart_subreg (mode, lwider->elt->val_rtx,
2055 GET_MODE (lwider->elt->val_rtx));
2056 if (sub)
2057 new_elt_loc_list (e, sub);
2060 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
2061 slot = cselib_find_slot (mode, x, e->hash, INSERT, memmode);
2062 *slot = e;
2063 return e;
2066 if (MEM_P (x))
2067 return cselib_lookup_mem (x, create);
2069 hashval = cselib_hash_rtx (x, create, memmode);
2070 /* Can't even create if hashing is not possible. */
2071 if (! hashval)
2072 return 0;
2074 slot = cselib_find_slot (mode, x, hashval,
2075 create ? INSERT : NO_INSERT, memmode);
2076 if (slot == 0)
2077 return 0;
2079 e = (cselib_val *) *slot;
2080 if (e)
2081 return e;
2083 e = new_cselib_val (hashval, mode, x);
2085 /* We have to fill the slot before calling cselib_subst_to_values:
2086 the hash table is inconsistent until we do so, and
2087 cselib_subst_to_values will need to do lookups. */
2088 *slot = e;
2089 new_elt_loc_list (e, cselib_subst_to_values (x, memmode));
2090 return e;
2093 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2095 cselib_val *
2096 cselib_lookup_from_insn (rtx x, enum machine_mode mode,
2097 int create, enum machine_mode memmode, rtx_insn *insn)
2099 cselib_val *ret;
2101 gcc_assert (!cselib_current_insn);
2102 cselib_current_insn = insn;
2104 ret = cselib_lookup (x, mode, create, memmode);
2106 cselib_current_insn = NULL;
2108 return ret;
2111 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2112 maintains invariants related with debug insns. */
2114 cselib_val *
2115 cselib_lookup (rtx x, enum machine_mode mode,
2116 int create, enum machine_mode memmode)
2118 cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
2120 /* ??? Should we return NULL if we're not to create an entry, the
2121 found loc is a debug loc and cselib_current_insn is not DEBUG?
2122 If so, we should also avoid converting val to non-DEBUG; probably
2123 easiest setting cselib_current_insn to NULL before the call
2124 above. */
2126 if (dump_file && (dump_flags & TDF_CSELIB))
2128 fputs ("cselib lookup ", dump_file);
2129 print_inline_rtx (dump_file, x, 2);
2130 fprintf (dump_file, " => %u:%u\n",
2131 ret ? ret->uid : 0,
2132 ret ? ret->hash : 0);
2135 return ret;
2138 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2139 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2140 is used to determine how many hard registers are being changed. If MODE
2141 is VOIDmode, then only REGNO is being changed; this is used when
2142 invalidating call clobbered registers across a call. */
2144 static void
2145 cselib_invalidate_regno (unsigned int regno, enum machine_mode mode)
2147 unsigned int endregno;
2148 unsigned int i;
2150 /* If we see pseudos after reload, something is _wrong_. */
2151 gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
2152 || reg_renumber[regno] < 0);
2154 /* Determine the range of registers that must be invalidated. For
2155 pseudos, only REGNO is affected. For hard regs, we must take MODE
2156 into account, and we must also invalidate lower register numbers
2157 if they contain values that overlap REGNO. */
2158 if (regno < FIRST_PSEUDO_REGISTER)
2160 gcc_assert (mode != VOIDmode);
2162 if (regno < max_value_regs)
2163 i = 0;
2164 else
2165 i = regno - max_value_regs;
2167 endregno = end_hard_regno (mode, regno);
2169 else
2171 i = regno;
2172 endregno = regno + 1;
2175 for (; i < endregno; i++)
2177 struct elt_list **l = &REG_VALUES (i);
2179 /* Go through all known values for this reg; if it overlaps the range
2180 we're invalidating, remove the value. */
2181 while (*l)
2183 cselib_val *v = (*l)->elt;
2184 bool had_locs;
2185 rtx setting_insn;
2186 struct elt_loc_list **p;
2187 unsigned int this_last = i;
2189 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
2190 this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
2192 if (this_last < regno || v == NULL
2193 || (v == cfa_base_preserved_val
2194 && i == cfa_base_preserved_regno))
2196 l = &(*l)->next;
2197 continue;
2200 /* We have an overlap. */
2201 if (*l == REG_VALUES (i))
2203 /* Maintain the invariant that the first entry of
2204 REG_VALUES, if present, must be the value used to set
2205 the register, or NULL. This is also nice because
2206 then we won't push the same regno onto user_regs
2207 multiple times. */
2208 (*l)->elt = NULL;
2209 l = &(*l)->next;
2211 else
2212 unchain_one_elt_list (l);
2214 v = canonical_cselib_val (v);
2216 had_locs = v->locs != NULL;
2217 setting_insn = v->locs ? v->locs->setting_insn : NULL;
2219 /* Now, we clear the mapping from value to reg. It must exist, so
2220 this code will crash intentionally if it doesn't. */
2221 for (p = &v->locs; ; p = &(*p)->next)
2223 rtx x = (*p)->loc;
2225 if (REG_P (x) && REGNO (x) == i)
2227 unchain_one_elt_loc_list (p);
2228 break;
2232 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2234 if (setting_insn && DEBUG_INSN_P (setting_insn))
2235 n_useless_debug_values++;
2236 else
2237 n_useless_values++;
2243 /* Invalidate any locations in the table which are changed because of a
2244 store to MEM_RTX. If this is called because of a non-const call
2245 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2247 static void
2248 cselib_invalidate_mem (rtx mem_rtx)
2250 cselib_val **vp, *v, *next;
2251 int num_mems = 0;
2252 rtx mem_addr;
2254 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2255 mem_rtx = canon_rtx (mem_rtx);
2257 vp = &first_containing_mem;
2258 for (v = *vp; v != &dummy_val; v = next)
2260 bool has_mem = false;
2261 struct elt_loc_list **p = &v->locs;
2262 bool had_locs = v->locs != NULL;
2263 rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
2265 while (*p)
2267 rtx x = (*p)->loc;
2268 cselib_val *addr;
2269 struct elt_list **mem_chain;
2271 /* MEMs may occur in locations only at the top level; below
2272 that every MEM or REG is substituted by its VALUE. */
2273 if (!MEM_P (x))
2275 p = &(*p)->next;
2276 continue;
2278 if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
2279 && ! canon_anti_dependence (x, false, mem_rtx,
2280 GET_MODE (mem_rtx), mem_addr))
2282 has_mem = true;
2283 num_mems++;
2284 p = &(*p)->next;
2285 continue;
2288 /* This one overlaps. */
2289 /* We must have a mapping from this MEM's address to the
2290 value (E). Remove that, too. */
2291 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
2292 addr = canonical_cselib_val (addr);
2293 gcc_checking_assert (v == canonical_cselib_val (v));
2294 mem_chain = &addr->addr_list;
2295 for (;;)
2297 cselib_val *canon = canonical_cselib_val ((*mem_chain)->elt);
2299 if (canon == v)
2301 unchain_one_elt_list (mem_chain);
2302 break;
2305 /* Record canonicalized elt. */
2306 (*mem_chain)->elt = canon;
2308 mem_chain = &(*mem_chain)->next;
2311 unchain_one_elt_loc_list (p);
2314 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2316 if (setting_insn && DEBUG_INSN_P (setting_insn))
2317 n_useless_debug_values++;
2318 else
2319 n_useless_values++;
2322 next = v->next_containing_mem;
2323 if (has_mem)
2325 *vp = v;
2326 vp = &(*vp)->next_containing_mem;
2328 else
2329 v->next_containing_mem = NULL;
2331 *vp = &dummy_val;
2334 /* Invalidate DEST, which is being assigned to or clobbered. */
2336 void
2337 cselib_invalidate_rtx (rtx dest)
2339 while (GET_CODE (dest) == SUBREG
2340 || GET_CODE (dest) == ZERO_EXTRACT
2341 || GET_CODE (dest) == STRICT_LOW_PART)
2342 dest = XEXP (dest, 0);
2344 if (REG_P (dest))
2345 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
2346 else if (MEM_P (dest))
2347 cselib_invalidate_mem (dest);
2350 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2352 static void
2353 cselib_invalidate_rtx_note_stores (rtx dest, const_rtx ignore ATTRIBUTE_UNUSED,
2354 void *data ATTRIBUTE_UNUSED)
2356 cselib_invalidate_rtx (dest);
2359 /* Record the result of a SET instruction. DEST is being set; the source
2360 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2361 describes its address. */
2363 static void
2364 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
2366 int dreg = REG_P (dest) ? (int) REGNO (dest) : -1;
2368 if (src_elt == 0 || side_effects_p (dest))
2369 return;
2371 if (dreg >= 0)
2373 if (dreg < FIRST_PSEUDO_REGISTER)
2375 unsigned int n = hard_regno_nregs[dreg][GET_MODE (dest)];
2377 if (n > max_value_regs)
2378 max_value_regs = n;
2381 if (REG_VALUES (dreg) == 0)
2383 used_regs[n_used_regs++] = dreg;
2384 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2386 else
2388 /* The register should have been invalidated. */
2389 gcc_assert (REG_VALUES (dreg)->elt == 0);
2390 REG_VALUES (dreg)->elt = src_elt;
2393 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2394 n_useless_values--;
2395 new_elt_loc_list (src_elt, dest);
2397 else if (MEM_P (dest) && dest_addr_elt != 0
2398 && cselib_record_memory)
2400 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2401 n_useless_values--;
2402 add_mem_for_addr (dest_addr_elt, src_elt, dest);
2406 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2408 void
2409 cselib_add_permanent_equiv (cselib_val *elt, rtx x, rtx_insn *insn)
2411 cselib_val *nelt;
2412 rtx_insn *save_cselib_current_insn = cselib_current_insn;
2414 gcc_checking_assert (elt);
2415 gcc_checking_assert (PRESERVED_VALUE_P (elt->val_rtx));
2416 gcc_checking_assert (!side_effects_p (x));
2418 cselib_current_insn = insn;
2420 nelt = cselib_lookup (x, GET_MODE (elt->val_rtx), 1, VOIDmode);
2422 if (nelt != elt)
2424 cselib_any_perm_equivs = true;
2426 if (!PRESERVED_VALUE_P (nelt->val_rtx))
2427 cselib_preserve_value (nelt);
2429 new_elt_loc_list (nelt, elt->val_rtx);
2432 cselib_current_insn = save_cselib_current_insn;
2435 /* Return TRUE if any permanent equivalences have been recorded since
2436 the table was last initialized. */
2437 bool
2438 cselib_have_permanent_equivalences (void)
2440 return cselib_any_perm_equivs;
2443 /* There is no good way to determine how many elements there can be
2444 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2445 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2447 struct cselib_record_autoinc_data
2449 struct cselib_set *sets;
2450 int n_sets;
2453 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2454 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2456 static int
2457 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
2458 rtx dest, rtx src, rtx srcoff, void *arg)
2460 struct cselib_record_autoinc_data *data;
2461 data = (struct cselib_record_autoinc_data *)arg;
2463 data->sets[data->n_sets].dest = dest;
2465 if (srcoff)
2466 data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
2467 else
2468 data->sets[data->n_sets].src = src;
2470 data->n_sets++;
2472 return 0;
2475 /* Record the effects of any sets and autoincs in INSN. */
2476 static void
2477 cselib_record_sets (rtx_insn *insn)
2479 int n_sets = 0;
2480 int i;
2481 struct cselib_set sets[MAX_SETS];
2482 rtx body = PATTERN (insn);
2483 rtx cond = 0;
2484 int n_sets_before_autoinc;
2485 struct cselib_record_autoinc_data data;
2487 body = PATTERN (insn);
2488 if (GET_CODE (body) == COND_EXEC)
2490 cond = COND_EXEC_TEST (body);
2491 body = COND_EXEC_CODE (body);
2494 /* Find all sets. */
2495 if (GET_CODE (body) == SET)
2497 sets[0].src = SET_SRC (body);
2498 sets[0].dest = SET_DEST (body);
2499 n_sets = 1;
2501 else if (GET_CODE (body) == PARALLEL)
2503 /* Look through the PARALLEL and record the values being
2504 set, if possible. Also handle any CLOBBERs. */
2505 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2507 rtx x = XVECEXP (body, 0, i);
2509 if (GET_CODE (x) == SET)
2511 sets[n_sets].src = SET_SRC (x);
2512 sets[n_sets].dest = SET_DEST (x);
2513 n_sets++;
2518 if (n_sets == 1
2519 && MEM_P (sets[0].src)
2520 && !cselib_record_memory
2521 && MEM_READONLY_P (sets[0].src))
2523 rtx note = find_reg_equal_equiv_note (insn);
2525 if (note && CONSTANT_P (XEXP (note, 0)))
2526 sets[0].src = XEXP (note, 0);
2529 data.sets = sets;
2530 data.n_sets = n_sets_before_autoinc = n_sets;
2531 for_each_inc_dec (PATTERN (insn), cselib_record_autoinc_cb, &data);
2532 n_sets = data.n_sets;
2534 /* Look up the values that are read. Do this before invalidating the
2535 locations that are written. */
2536 for (i = 0; i < n_sets; i++)
2538 rtx dest = sets[i].dest;
2540 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2541 the low part after invalidating any knowledge about larger modes. */
2542 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2543 sets[i].dest = dest = XEXP (dest, 0);
2545 /* We don't know how to record anything but REG or MEM. */
2546 if (REG_P (dest)
2547 || (MEM_P (dest) && cselib_record_memory))
2549 rtx src = sets[i].src;
2550 if (cond)
2551 src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
2552 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
2553 if (MEM_P (dest))
2555 enum machine_mode address_mode = get_address_mode (dest);
2557 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
2558 address_mode, 1,
2559 GET_MODE (dest));
2561 else
2562 sets[i].dest_addr_elt = 0;
2566 if (cselib_record_sets_hook)
2567 cselib_record_sets_hook (insn, sets, n_sets);
2569 /* Invalidate all locations written by this insn. Note that the elts we
2570 looked up in the previous loop aren't affected, just some of their
2571 locations may go away. */
2572 note_stores (body, cselib_invalidate_rtx_note_stores, NULL);
2574 for (i = n_sets_before_autoinc; i < n_sets; i++)
2575 cselib_invalidate_rtx (sets[i].dest);
2577 /* If this is an asm, look for duplicate sets. This can happen when the
2578 user uses the same value as an output multiple times. This is valid
2579 if the outputs are not actually used thereafter. Treat this case as
2580 if the value isn't actually set. We do this by smashing the destination
2581 to pc_rtx, so that we won't record the value later. */
2582 if (n_sets >= 2 && asm_noperands (body) >= 0)
2584 for (i = 0; i < n_sets; i++)
2586 rtx dest = sets[i].dest;
2587 if (REG_P (dest) || MEM_P (dest))
2589 int j;
2590 for (j = i + 1; j < n_sets; j++)
2591 if (rtx_equal_p (dest, sets[j].dest))
2593 sets[i].dest = pc_rtx;
2594 sets[j].dest = pc_rtx;
2600 /* Now enter the equivalences in our tables. */
2601 for (i = 0; i < n_sets; i++)
2603 rtx dest = sets[i].dest;
2604 if (REG_P (dest)
2605 || (MEM_P (dest) && cselib_record_memory))
2606 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2610 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
2612 bool
2613 fp_setter_insn (rtx insn)
2615 rtx expr, pat = NULL_RTX;
2617 if (!RTX_FRAME_RELATED_P (insn))
2618 return false;
2620 expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2621 if (expr)
2622 pat = XEXP (expr, 0);
2623 if (!modified_in_p (hard_frame_pointer_rtx, pat ? pat : insn))
2624 return false;
2626 /* Don't return true for frame pointer restores in the epilogue. */
2627 if (find_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx))
2628 return false;
2629 return true;
2632 /* Record the effects of INSN. */
2634 void
2635 cselib_process_insn (rtx_insn *insn)
2637 int i;
2638 rtx x;
2640 cselib_current_insn = insn;
2642 /* Forget everything at a CODE_LABEL or a setjmp. */
2643 if ((LABEL_P (insn)
2644 || (CALL_P (insn)
2645 && find_reg_note (insn, REG_SETJMP, NULL)))
2646 && !cselib_preserve_constants)
2648 cselib_reset_table (next_uid);
2649 cselib_current_insn = NULL;
2650 return;
2653 if (! INSN_P (insn))
2655 cselib_current_insn = NULL;
2656 return;
2659 /* If this is a call instruction, forget anything stored in a
2660 call clobbered register, or, if this is not a const call, in
2661 memory. */
2662 if (CALL_P (insn))
2664 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2665 if (call_used_regs[i]
2666 || (REG_VALUES (i) && REG_VALUES (i)->elt
2667 && HARD_REGNO_CALL_PART_CLOBBERED (i,
2668 GET_MODE (REG_VALUES (i)->elt->val_rtx))))
2669 cselib_invalidate_regno (i, reg_raw_mode[i]);
2671 /* Since it is not clear how cselib is going to be used, be
2672 conservative here and treat looping pure or const functions
2673 as if they were regular functions. */
2674 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
2675 || !(RTL_CONST_OR_PURE_CALL_P (insn)))
2676 cselib_invalidate_mem (callmem);
2679 cselib_record_sets (insn);
2681 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2682 after we have processed the insn. */
2683 if (CALL_P (insn))
2685 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
2686 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
2687 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
2688 /* Flush evertything on setjmp. */
2689 if (cselib_preserve_constants
2690 && find_reg_note (insn, REG_SETJMP, NULL))
2692 cselib_preserve_only_values ();
2693 cselib_reset_table (next_uid);
2697 /* On setter of the hard frame pointer if frame_pointer_needed,
2698 invalidate stack_pointer_rtx, so that sp and {,h}fp based
2699 VALUEs are distinct. */
2700 if (reload_completed
2701 && frame_pointer_needed
2702 && fp_setter_insn (insn))
2703 cselib_invalidate_rtx (stack_pointer_rtx);
2705 cselib_current_insn = NULL;
2707 if (n_useless_values > MAX_USELESS_VALUES
2708 /* remove_useless_values is linear in the hash table size. Avoid
2709 quadratic behavior for very large hashtables with very few
2710 useless elements. */
2711 && ((unsigned int)n_useless_values
2712 > (cselib_hash_table->elements () - n_debug_values) / 4))
2713 remove_useless_values ();
2716 /* Initialize cselib for one pass. The caller must also call
2717 init_alias_analysis. */
2719 void
2720 cselib_init (int record_what)
2722 elt_list_pool = create_alloc_pool ("elt_list",
2723 sizeof (struct elt_list), 10);
2724 elt_loc_list_pool = create_alloc_pool ("elt_loc_list",
2725 sizeof (struct elt_loc_list), 10);
2726 cselib_val_pool = create_alloc_pool ("cselib_val_list",
2727 sizeof (cselib_val), 10);
2728 value_pool = create_alloc_pool ("value", RTX_CODE_SIZE (VALUE), 100);
2729 cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
2730 cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
2731 cselib_any_perm_equivs = false;
2733 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2734 see canon_true_dependence. This is only created once. */
2735 if (! callmem)
2736 callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2738 cselib_nregs = max_reg_num ();
2740 /* We preserve reg_values to allow expensive clearing of the whole thing.
2741 Reallocate it however if it happens to be too large. */
2742 if (!reg_values || reg_values_size < cselib_nregs
2743 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
2745 free (reg_values);
2746 /* Some space for newly emit instructions so we don't end up
2747 reallocating in between passes. */
2748 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
2749 reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
2751 used_regs = XNEWVEC (unsigned int, cselib_nregs);
2752 n_used_regs = 0;
2753 cselib_hash_table = new hash_table<cselib_hasher> (31);
2754 if (cselib_preserve_constants)
2755 cselib_preserved_hash_table = new hash_table<cselib_hasher> (31);
2756 next_uid = 1;
2759 /* Called when the current user is done with cselib. */
2761 void
2762 cselib_finish (void)
2764 bool preserved = cselib_preserve_constants;
2765 cselib_discard_hook = NULL;
2766 cselib_preserve_constants = false;
2767 cselib_any_perm_equivs = false;
2768 cfa_base_preserved_val = NULL;
2769 cfa_base_preserved_regno = INVALID_REGNUM;
2770 free_alloc_pool (elt_list_pool);
2771 free_alloc_pool (elt_loc_list_pool);
2772 free_alloc_pool (cselib_val_pool);
2773 free_alloc_pool (value_pool);
2774 cselib_clear_table ();
2775 delete cselib_hash_table;
2776 cselib_hash_table = NULL;
2777 if (preserved)
2778 delete cselib_preserved_hash_table;
2779 cselib_preserved_hash_table = NULL;
2780 free (used_regs);
2781 used_regs = 0;
2782 n_useless_values = 0;
2783 n_useless_debug_values = 0;
2784 n_debug_values = 0;
2785 next_uid = 0;
2788 /* Dump the cselib_val *X to FILE *OUT. */
2791 dump_cselib_val (cselib_val **x, FILE *out)
2793 cselib_val *v = *x;
2794 bool need_lf = true;
2796 print_inline_rtx (out, v->val_rtx, 0);
2798 if (v->locs)
2800 struct elt_loc_list *l = v->locs;
2801 if (need_lf)
2803 fputc ('\n', out);
2804 need_lf = false;
2806 fputs (" locs:", out);
2809 if (l->setting_insn)
2810 fprintf (out, "\n from insn %i ",
2811 INSN_UID (l->setting_insn));
2812 else
2813 fprintf (out, "\n ");
2814 print_inline_rtx (out, l->loc, 4);
2816 while ((l = l->next));
2817 fputc ('\n', out);
2819 else
2821 fputs (" no locs", out);
2822 need_lf = true;
2825 if (v->addr_list)
2827 struct elt_list *e = v->addr_list;
2828 if (need_lf)
2830 fputc ('\n', out);
2831 need_lf = false;
2833 fputs (" addr list:", out);
2836 fputs ("\n ", out);
2837 print_inline_rtx (out, e->elt->val_rtx, 2);
2839 while ((e = e->next));
2840 fputc ('\n', out);
2842 else
2844 fputs (" no addrs", out);
2845 need_lf = true;
2848 if (v->next_containing_mem == &dummy_val)
2849 fputs (" last mem\n", out);
2850 else if (v->next_containing_mem)
2852 fputs (" next mem ", out);
2853 print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
2854 fputc ('\n', out);
2856 else if (need_lf)
2857 fputc ('\n', out);
2859 return 1;
2862 /* Dump to OUT everything in the CSELIB table. */
2864 void
2865 dump_cselib_table (FILE *out)
2867 fprintf (out, "cselib hash table:\n");
2868 cselib_hash_table->traverse <FILE *, dump_cselib_val> (out);
2869 fprintf (out, "cselib preserved hash table:\n");
2870 cselib_preserved_hash_table->traverse <FILE *, dump_cselib_val> (out);
2871 if (first_containing_mem != &dummy_val)
2873 fputs ("first mem ", out);
2874 print_inline_rtx (out, first_containing_mem->val_rtx, 2);
2875 fputc ('\n', out);
2877 fprintf (out, "next uid %i\n", next_uid);
2880 #include "gt-cselib.h"