2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / cselib.c
blob7ccaab4b811f28a3e260de0d5cb392fa750e99f2
1 /* Common subexpression elimination library for GNU compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "rtl.h"
25 #include "input.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "tree.h"/* FIXME: For hashing DEBUG_EXPR & friends. */
29 #include "tm_p.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "flags.h"
33 #include "insn-config.h"
34 #include "recog.h"
35 #include "input.h"
36 #include "function.h"
37 #include "emit-rtl.h"
38 #include "diagnostic-core.h"
39 #include "dumpfile.h"
40 #include "alloc-pool.h"
41 #include "cselib.h"
42 #include "predict.h"
43 #include "basic-block.h"
44 #include "valtrack.h"
45 #include "params.h"
46 #include "alloc-pool.h"
47 #include "target.h"
48 #include "bitmap.h"
50 /* A list of cselib_val structures. */
51 struct elt_list
53 struct elt_list *next;
54 cselib_val *elt;
56 /* Pool allocation new operator. */
57 inline void *operator new (size_t)
59 return pool.allocate ();
62 /* Delete operator utilizing pool allocation. */
63 inline void operator delete (void *ptr)
65 pool.remove ((elt_list *) ptr);
68 /* Memory allocation pool. */
69 static pool_allocator<elt_list> pool;
72 static bool cselib_record_memory;
73 static bool cselib_preserve_constants;
74 static bool cselib_any_perm_equivs;
75 static inline void promote_debug_loc (struct elt_loc_list *l);
76 static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
77 static void new_elt_loc_list (cselib_val *, rtx);
78 static void unchain_one_value (cselib_val *);
79 static void unchain_one_elt_list (struct elt_list **);
80 static void unchain_one_elt_loc_list (struct elt_loc_list **);
81 static void remove_useless_values (void);
82 static int rtx_equal_for_cselib_1 (rtx, rtx, machine_mode);
83 static unsigned int cselib_hash_rtx (rtx, int, machine_mode);
84 static cselib_val *new_cselib_val (unsigned int, machine_mode, rtx);
85 static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
86 static cselib_val *cselib_lookup_mem (rtx, int);
87 static void cselib_invalidate_regno (unsigned int, machine_mode);
88 static void cselib_invalidate_mem (rtx);
89 static void cselib_record_set (rtx, cselib_val *, cselib_val *);
90 static void cselib_record_sets (rtx_insn *);
92 struct expand_value_data
94 bitmap regs_active;
95 cselib_expand_callback callback;
96 void *callback_arg;
97 bool dummy;
100 static rtx cselib_expand_value_rtx_1 (rtx, struct expand_value_data *, int);
102 /* There are three ways in which cselib can look up an rtx:
103 - for a REG, the reg_values table (which is indexed by regno) is used
104 - for a MEM, we recursively look up its address and then follow the
105 addr_list of that value
106 - for everything else, we compute a hash value and go through the hash
107 table. Since different rtx's can still have the same hash value,
108 this involves walking the table entries for a given value and comparing
109 the locations of the entries with the rtx we are looking up. */
111 struct cselib_hasher : typed_noop_remove <cselib_val>
113 typedef cselib_val *value_type;
114 struct key {
115 /* The rtx value and its mode (needed separately for constant
116 integers). */
117 machine_mode mode;
118 rtx x;
119 /* The mode of the contaning MEM, if any, otherwise VOIDmode. */
120 machine_mode memmode;
122 typedef key *compare_type;
123 static inline hashval_t hash (const cselib_val *);
124 static inline bool equal (const cselib_val *, const key *);
127 /* The hash function for our hash table. The value is always computed with
128 cselib_hash_rtx when adding an element; this function just extracts the
129 hash value from a cselib_val structure. */
131 inline hashval_t
132 cselib_hasher::hash (const cselib_val *v)
134 return v->hash;
137 /* The equality test for our hash table. The first argument V is a table
138 element (i.e. a cselib_val), while the second arg X is an rtx. We know
139 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
140 CONST of an appropriate mode. */
142 inline bool
143 cselib_hasher::equal (const cselib_val *v, const key *x_arg)
145 struct elt_loc_list *l;
146 rtx x = x_arg->x;
147 machine_mode mode = x_arg->mode;
148 machine_mode memmode = x_arg->memmode;
150 if (mode != GET_MODE (v->val_rtx))
151 return false;
153 if (GET_CODE (x) == VALUE)
154 return x == v->val_rtx;
156 /* We don't guarantee that distinct rtx's have different hash values,
157 so we need to do a comparison. */
158 for (l = v->locs; l; l = l->next)
159 if (rtx_equal_for_cselib_1 (l->loc, x, memmode))
161 promote_debug_loc (l);
162 return true;
165 return false;
168 /* A table that enables us to look up elts by their value. */
169 static hash_table<cselib_hasher> *cselib_hash_table;
171 /* A table to hold preserved values. */
172 static hash_table<cselib_hasher> *cselib_preserved_hash_table;
174 /* This is a global so we don't have to pass this through every function.
175 It is used in new_elt_loc_list to set SETTING_INSN. */
176 static rtx_insn *cselib_current_insn;
178 /* The unique id that the next create value will take. */
179 static unsigned int next_uid;
181 /* The number of registers we had when the varrays were last resized. */
182 static unsigned int cselib_nregs;
184 /* Count values without known locations, or with only locations that
185 wouldn't have been known except for debug insns. Whenever this
186 grows too big, we remove these useless values from the table.
188 Counting values with only debug values is a bit tricky. We don't
189 want to increment n_useless_values when we create a value for a
190 debug insn, for this would get n_useless_values out of sync, but we
191 want increment it if all locs in the list that were ever referenced
192 in nondebug insns are removed from the list.
194 In the general case, once we do that, we'd have to stop accepting
195 nondebug expressions in the loc list, to avoid having two values
196 equivalent that, without debug insns, would have been made into
197 separate values. However, because debug insns never introduce
198 equivalences themselves (no assignments), the only means for
199 growing loc lists is through nondebug assignments. If the locs
200 also happen to be referenced in debug insns, it will work just fine.
202 A consequence of this is that there's at most one debug-only loc in
203 each loc list. If we keep it in the first entry, testing whether
204 we have a debug-only loc list takes O(1).
206 Furthermore, since any additional entry in a loc list containing a
207 debug loc would have to come from an assignment (nondebug) that
208 references both the initial debug loc and the newly-equivalent loc,
209 the initial debug loc would be promoted to a nondebug loc, and the
210 loc list would not contain debug locs any more.
212 So the only case we have to be careful with in order to keep
213 n_useless_values in sync between debug and nondebug compilations is
214 to avoid incrementing n_useless_values when removing the single loc
215 from a value that turns out to not appear outside debug values. We
216 increment n_useless_debug_values instead, and leave such values
217 alone until, for other reasons, we garbage-collect useless
218 values. */
219 static int n_useless_values;
220 static int n_useless_debug_values;
222 /* Count values whose locs have been taken exclusively from debug
223 insns for the entire life of the value. */
224 static int n_debug_values;
226 /* Number of useless values before we remove them from the hash table. */
227 #define MAX_USELESS_VALUES 32
229 /* This table maps from register number to values. It does not
230 contain pointers to cselib_val structures, but rather elt_lists.
231 The purpose is to be able to refer to the same register in
232 different modes. The first element of the list defines the mode in
233 which the register was set; if the mode is unknown or the value is
234 no longer valid in that mode, ELT will be NULL for the first
235 element. */
236 static struct elt_list **reg_values;
237 static unsigned int reg_values_size;
238 #define REG_VALUES(i) reg_values[i]
240 /* The largest number of hard regs used by any entry added to the
241 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
242 static unsigned int max_value_regs;
244 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
245 in cselib_clear_table() for fast emptying. */
246 static unsigned int *used_regs;
247 static unsigned int n_used_regs;
249 /* We pass this to cselib_invalidate_mem to invalidate all of
250 memory for a non-const call instruction. */
251 static GTY(()) rtx callmem;
253 /* Set by discard_useless_locs if it deleted the last location of any
254 value. */
255 static int values_became_useless;
257 /* Used as stop element of the containing_mem list so we can check
258 presence in the list by checking the next pointer. */
259 static cselib_val dummy_val;
261 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
262 that is constant through the whole function and should never be
263 eliminated. */
264 static cselib_val *cfa_base_preserved_val;
265 static unsigned int cfa_base_preserved_regno = INVALID_REGNUM;
267 /* Used to list all values that contain memory reference.
268 May or may not contain the useless values - the list is compacted
269 each time memory is invalidated. */
270 static cselib_val *first_containing_mem = &dummy_val;
272 pool_allocator<elt_list> elt_list::pool ("elt_list", 10);
273 pool_allocator<elt_loc_list> elt_loc_list::pool ("elt_loc_list", 10);
274 pool_allocator<cselib_val> cselib_val::pool ("cselib_val_list", 10);
276 static pool_allocator<rtx_def> value_pool ("value", 100, RTX_CODE_SIZE (VALUE),
277 true);
279 /* If nonnull, cselib will call this function before freeing useless
280 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
281 void (*cselib_discard_hook) (cselib_val *);
283 /* If nonnull, cselib will call this function before recording sets or
284 even clobbering outputs of INSN. All the recorded sets will be
285 represented in the array sets[n_sets]. new_val_min can be used to
286 tell whether values present in sets are introduced by this
287 instruction. */
288 void (*cselib_record_sets_hook) (rtx_insn *insn, struct cselib_set *sets,
289 int n_sets);
291 #define PRESERVED_VALUE_P(RTX) \
292 (RTL_FLAG_CHECK1 ("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
294 #define SP_BASED_VALUE_P(RTX) \
295 (RTL_FLAG_CHECK1 ("SP_BASED_VALUE_P", (RTX), VALUE)->jump)
299 /* Allocate a struct elt_list and fill in its two elements with the
300 arguments. */
302 static inline struct elt_list *
303 new_elt_list (struct elt_list *next, cselib_val *elt)
305 elt_list *el = new elt_list ();
306 el->next = next;
307 el->elt = elt;
308 return el;
311 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
312 list. */
314 static inline void
315 new_elt_loc_list (cselib_val *val, rtx loc)
317 struct elt_loc_list *el, *next = val->locs;
319 gcc_checking_assert (!next || !next->setting_insn
320 || !DEBUG_INSN_P (next->setting_insn)
321 || cselib_current_insn == next->setting_insn);
323 /* If we're creating the first loc in a debug insn context, we've
324 just created a debug value. Count it. */
325 if (!next && cselib_current_insn && DEBUG_INSN_P (cselib_current_insn))
326 n_debug_values++;
328 val = canonical_cselib_val (val);
329 next = val->locs;
331 if (GET_CODE (loc) == VALUE)
333 loc = canonical_cselib_val (CSELIB_VAL_PTR (loc))->val_rtx;
335 gcc_checking_assert (PRESERVED_VALUE_P (loc)
336 == PRESERVED_VALUE_P (val->val_rtx));
338 if (val->val_rtx == loc)
339 return;
340 else if (val->uid > CSELIB_VAL_PTR (loc)->uid)
342 /* Reverse the insertion. */
343 new_elt_loc_list (CSELIB_VAL_PTR (loc), val->val_rtx);
344 return;
347 gcc_checking_assert (val->uid < CSELIB_VAL_PTR (loc)->uid);
349 if (CSELIB_VAL_PTR (loc)->locs)
351 /* Bring all locs from LOC to VAL. */
352 for (el = CSELIB_VAL_PTR (loc)->locs; el->next; el = el->next)
354 /* Adjust values that have LOC as canonical so that VAL
355 becomes their canonical. */
356 if (el->loc && GET_CODE (el->loc) == VALUE)
358 gcc_checking_assert (CSELIB_VAL_PTR (el->loc)->locs->loc
359 == loc);
360 CSELIB_VAL_PTR (el->loc)->locs->loc = val->val_rtx;
363 el->next = val->locs;
364 next = val->locs = CSELIB_VAL_PTR (loc)->locs;
367 if (CSELIB_VAL_PTR (loc)->addr_list)
369 /* Bring in addr_list into canonical node. */
370 struct elt_list *last = CSELIB_VAL_PTR (loc)->addr_list;
371 while (last->next)
372 last = last->next;
373 last->next = val->addr_list;
374 val->addr_list = CSELIB_VAL_PTR (loc)->addr_list;
375 CSELIB_VAL_PTR (loc)->addr_list = NULL;
378 if (CSELIB_VAL_PTR (loc)->next_containing_mem != NULL
379 && val->next_containing_mem == NULL)
381 /* Add VAL to the containing_mem list after LOC. LOC will
382 be removed when we notice it doesn't contain any
383 MEMs. */
384 val->next_containing_mem = CSELIB_VAL_PTR (loc)->next_containing_mem;
385 CSELIB_VAL_PTR (loc)->next_containing_mem = val;
388 /* Chain LOC back to VAL. */
389 el = new elt_loc_list;
390 el->loc = val->val_rtx;
391 el->setting_insn = cselib_current_insn;
392 el->next = NULL;
393 CSELIB_VAL_PTR (loc)->locs = el;
396 el = new elt_loc_list;
397 el->loc = loc;
398 el->setting_insn = cselib_current_insn;
399 el->next = next;
400 val->locs = el;
403 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
404 originating from a debug insn, maintaining the debug values
405 count. */
407 static inline void
408 promote_debug_loc (struct elt_loc_list *l)
410 if (l && l->setting_insn && DEBUG_INSN_P (l->setting_insn)
411 && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
413 n_debug_values--;
414 l->setting_insn = cselib_current_insn;
415 if (cselib_preserve_constants && l->next)
417 gcc_assert (l->next->setting_insn
418 && DEBUG_INSN_P (l->next->setting_insn)
419 && !l->next->next);
420 l->next->setting_insn = cselib_current_insn;
422 else
423 gcc_assert (!l->next);
427 /* The elt_list at *PL is no longer needed. Unchain it and free its
428 storage. */
430 static inline void
431 unchain_one_elt_list (struct elt_list **pl)
433 struct elt_list *l = *pl;
435 *pl = l->next;
436 delete l;
439 /* Likewise for elt_loc_lists. */
441 static void
442 unchain_one_elt_loc_list (struct elt_loc_list **pl)
444 struct elt_loc_list *l = *pl;
446 *pl = l->next;
447 delete l;
450 /* Likewise for cselib_vals. This also frees the addr_list associated with
451 V. */
453 static void
454 unchain_one_value (cselib_val *v)
456 while (v->addr_list)
457 unchain_one_elt_list (&v->addr_list);
459 delete v;
462 /* Remove all entries from the hash table. Also used during
463 initialization. */
465 void
466 cselib_clear_table (void)
468 cselib_reset_table (1);
471 /* Return TRUE if V is a constant, a function invariant or a VALUE
472 equivalence; FALSE otherwise. */
474 static bool
475 invariant_or_equiv_p (cselib_val *v)
477 struct elt_loc_list *l;
479 if (v == cfa_base_preserved_val)
480 return true;
482 /* Keep VALUE equivalences around. */
483 for (l = v->locs; l; l = l->next)
484 if (GET_CODE (l->loc) == VALUE)
485 return true;
487 if (v->locs != NULL
488 && v->locs->next == NULL)
490 if (CONSTANT_P (v->locs->loc)
491 && (GET_CODE (v->locs->loc) != CONST
492 || !references_value_p (v->locs->loc, 0)))
493 return true;
494 /* Although a debug expr may be bound to different expressions,
495 we can preserve it as if it was constant, to get unification
496 and proper merging within var-tracking. */
497 if (GET_CODE (v->locs->loc) == DEBUG_EXPR
498 || GET_CODE (v->locs->loc) == DEBUG_IMPLICIT_PTR
499 || GET_CODE (v->locs->loc) == ENTRY_VALUE
500 || GET_CODE (v->locs->loc) == DEBUG_PARAMETER_REF)
501 return true;
503 /* (plus (value V) (const_int C)) is invariant iff V is invariant. */
504 if (GET_CODE (v->locs->loc) == PLUS
505 && CONST_INT_P (XEXP (v->locs->loc, 1))
506 && GET_CODE (XEXP (v->locs->loc, 0)) == VALUE
507 && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v->locs->loc, 0))))
508 return true;
511 return false;
514 /* Remove from hash table all VALUEs except constants, function
515 invariants and VALUE equivalences. */
518 preserve_constants_and_equivs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
520 cselib_val *v = *x;
522 if (invariant_or_equiv_p (v))
524 cselib_hasher::key lookup = {
525 GET_MODE (v->val_rtx), v->val_rtx, VOIDmode
527 cselib_val **slot
528 = cselib_preserved_hash_table->find_slot_with_hash (&lookup,
529 v->hash, INSERT);
530 gcc_assert (!*slot);
531 *slot = v;
534 cselib_hash_table->clear_slot (x);
536 return 1;
539 /* Remove all entries from the hash table, arranging for the next
540 value to be numbered NUM. */
542 void
543 cselib_reset_table (unsigned int num)
545 unsigned int i;
547 max_value_regs = 0;
549 if (cfa_base_preserved_val)
551 unsigned int regno = cfa_base_preserved_regno;
552 unsigned int new_used_regs = 0;
553 for (i = 0; i < n_used_regs; i++)
554 if (used_regs[i] == regno)
556 new_used_regs = 1;
557 continue;
559 else
560 REG_VALUES (used_regs[i]) = 0;
561 gcc_assert (new_used_regs == 1);
562 n_used_regs = new_used_regs;
563 used_regs[0] = regno;
564 max_value_regs
565 = hard_regno_nregs[regno][GET_MODE (cfa_base_preserved_val->locs->loc)];
567 else
569 for (i = 0; i < n_used_regs; i++)
570 REG_VALUES (used_regs[i]) = 0;
571 n_used_regs = 0;
574 if (cselib_preserve_constants)
575 cselib_hash_table->traverse <void *, preserve_constants_and_equivs>
576 (NULL);
577 else
579 cselib_hash_table->empty ();
580 gcc_checking_assert (!cselib_any_perm_equivs);
583 n_useless_values = 0;
584 n_useless_debug_values = 0;
585 n_debug_values = 0;
587 next_uid = num;
589 first_containing_mem = &dummy_val;
592 /* Return the number of the next value that will be generated. */
594 unsigned int
595 cselib_get_next_uid (void)
597 return next_uid;
600 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
601 INSERTing if requested. When X is part of the address of a MEM,
602 MEMMODE should specify the mode of the MEM. */
604 static cselib_val **
605 cselib_find_slot (machine_mode mode, rtx x, hashval_t hash,
606 enum insert_option insert, machine_mode memmode)
608 cselib_val **slot = NULL;
609 cselib_hasher::key lookup = { mode, x, memmode };
610 if (cselib_preserve_constants)
611 slot = cselib_preserved_hash_table->find_slot_with_hash (&lookup, hash,
612 NO_INSERT);
613 if (!slot)
614 slot = cselib_hash_table->find_slot_with_hash (&lookup, hash, insert);
615 return slot;
618 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
619 only return true for values which point to a cselib_val whose value
620 element has been set to zero, which implies the cselib_val will be
621 removed. */
624 references_value_p (const_rtx x, int only_useless)
626 const enum rtx_code code = GET_CODE (x);
627 const char *fmt = GET_RTX_FORMAT (code);
628 int i, j;
630 if (GET_CODE (x) == VALUE
631 && (! only_useless ||
632 (CSELIB_VAL_PTR (x)->locs == 0 && !PRESERVED_VALUE_P (x))))
633 return 1;
635 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
637 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
638 return 1;
639 else if (fmt[i] == 'E')
640 for (j = 0; j < XVECLEN (x, i); j++)
641 if (references_value_p (XVECEXP (x, i, j), only_useless))
642 return 1;
645 return 0;
648 /* For all locations found in X, delete locations that reference useless
649 values (i.e. values without any location). Called through
650 htab_traverse. */
653 discard_useless_locs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
655 cselib_val *v = *x;
656 struct elt_loc_list **p = &v->locs;
657 bool had_locs = v->locs != NULL;
658 rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
660 while (*p)
662 if (references_value_p ((*p)->loc, 1))
663 unchain_one_elt_loc_list (p);
664 else
665 p = &(*p)->next;
668 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
670 if (setting_insn && DEBUG_INSN_P (setting_insn))
671 n_useless_debug_values++;
672 else
673 n_useless_values++;
674 values_became_useless = 1;
676 return 1;
679 /* If X is a value with no locations, remove it from the hashtable. */
682 discard_useless_values (cselib_val **x, void *info ATTRIBUTE_UNUSED)
684 cselib_val *v = *x;
686 if (v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
688 if (cselib_discard_hook)
689 cselib_discard_hook (v);
691 CSELIB_VAL_PTR (v->val_rtx) = NULL;
692 cselib_hash_table->clear_slot (x);
693 unchain_one_value (v);
694 n_useless_values--;
697 return 1;
700 /* Clean out useless values (i.e. those which no longer have locations
701 associated with them) from the hash table. */
703 static void
704 remove_useless_values (void)
706 cselib_val **p, *v;
708 /* First pass: eliminate locations that reference the value. That in
709 turn can make more values useless. */
712 values_became_useless = 0;
713 cselib_hash_table->traverse <void *, discard_useless_locs> (NULL);
715 while (values_became_useless);
717 /* Second pass: actually remove the values. */
719 p = &first_containing_mem;
720 for (v = *p; v != &dummy_val; v = v->next_containing_mem)
721 if (v->locs && v == canonical_cselib_val (v))
723 *p = v;
724 p = &(*p)->next_containing_mem;
726 *p = &dummy_val;
728 n_useless_values += n_useless_debug_values;
729 n_debug_values -= n_useless_debug_values;
730 n_useless_debug_values = 0;
732 cselib_hash_table->traverse <void *, discard_useless_values> (NULL);
734 gcc_assert (!n_useless_values);
737 /* Arrange for a value to not be removed from the hash table even if
738 it becomes useless. */
740 void
741 cselib_preserve_value (cselib_val *v)
743 PRESERVED_VALUE_P (v->val_rtx) = 1;
746 /* Test whether a value is preserved. */
748 bool
749 cselib_preserved_value_p (cselib_val *v)
751 return PRESERVED_VALUE_P (v->val_rtx);
754 /* Arrange for a REG value to be assumed constant through the whole function,
755 never invalidated and preserved across cselib_reset_table calls. */
757 void
758 cselib_preserve_cfa_base_value (cselib_val *v, unsigned int regno)
760 if (cselib_preserve_constants
761 && v->locs
762 && REG_P (v->locs->loc))
764 cfa_base_preserved_val = v;
765 cfa_base_preserved_regno = regno;
769 /* Clean all non-constant expressions in the hash table, but retain
770 their values. */
772 void
773 cselib_preserve_only_values (void)
775 int i;
777 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
778 cselib_invalidate_regno (i, reg_raw_mode[i]);
780 cselib_invalidate_mem (callmem);
782 remove_useless_values ();
784 gcc_assert (first_containing_mem == &dummy_val);
787 /* Arrange for a value to be marked as based on stack pointer
788 for find_base_term purposes. */
790 void
791 cselib_set_value_sp_based (cselib_val *v)
793 SP_BASED_VALUE_P (v->val_rtx) = 1;
796 /* Test whether a value is based on stack pointer for
797 find_base_term purposes. */
799 bool
800 cselib_sp_based_value_p (cselib_val *v)
802 return SP_BASED_VALUE_P (v->val_rtx);
805 /* Return the mode in which a register was last set. If X is not a
806 register, return its mode. If the mode in which the register was
807 set is not known, or the value was already clobbered, return
808 VOIDmode. */
810 machine_mode
811 cselib_reg_set_mode (const_rtx x)
813 if (!REG_P (x))
814 return GET_MODE (x);
816 if (REG_VALUES (REGNO (x)) == NULL
817 || REG_VALUES (REGNO (x))->elt == NULL)
818 return VOIDmode;
820 return GET_MODE (REG_VALUES (REGNO (x))->elt->val_rtx);
823 /* Return nonzero if we can prove that X and Y contain the same value, taking
824 our gathered information into account. */
827 rtx_equal_for_cselib_p (rtx x, rtx y)
829 return rtx_equal_for_cselib_1 (x, y, VOIDmode);
832 /* If x is a PLUS or an autoinc operation, expand the operation,
833 storing the offset, if any, in *OFF. */
835 static rtx
836 autoinc_split (rtx x, rtx *off, machine_mode memmode)
838 switch (GET_CODE (x))
840 case PLUS:
841 *off = XEXP (x, 1);
842 return XEXP (x, 0);
844 case PRE_DEC:
845 if (memmode == VOIDmode)
846 return x;
848 *off = GEN_INT (-GET_MODE_SIZE (memmode));
849 return XEXP (x, 0);
850 break;
852 case PRE_INC:
853 if (memmode == VOIDmode)
854 return x;
856 *off = GEN_INT (GET_MODE_SIZE (memmode));
857 return XEXP (x, 0);
859 case PRE_MODIFY:
860 return XEXP (x, 1);
862 case POST_DEC:
863 case POST_INC:
864 case POST_MODIFY:
865 return XEXP (x, 0);
867 default:
868 return x;
872 /* Return nonzero if we can prove that X and Y contain the same value,
873 taking our gathered information into account. MEMMODE holds the
874 mode of the enclosing MEM, if any, as required to deal with autoinc
875 addressing modes. If X and Y are not (known to be) part of
876 addresses, MEMMODE should be VOIDmode. */
878 static int
879 rtx_equal_for_cselib_1 (rtx x, rtx y, machine_mode memmode)
881 enum rtx_code code;
882 const char *fmt;
883 int i;
885 if (REG_P (x) || MEM_P (x))
887 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0, memmode);
889 if (e)
890 x = e->val_rtx;
893 if (REG_P (y) || MEM_P (y))
895 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0, memmode);
897 if (e)
898 y = e->val_rtx;
901 if (x == y)
902 return 1;
904 if (GET_CODE (x) == VALUE)
906 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (x));
907 struct elt_loc_list *l;
909 if (GET_CODE (y) == VALUE)
910 return e == canonical_cselib_val (CSELIB_VAL_PTR (y));
912 for (l = e->locs; l; l = l->next)
914 rtx t = l->loc;
916 /* Avoid infinite recursion. We know we have the canonical
917 value, so we can just skip any values in the equivalence
918 list. */
919 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
920 continue;
921 else if (rtx_equal_for_cselib_1 (t, y, memmode))
922 return 1;
925 return 0;
927 else if (GET_CODE (y) == VALUE)
929 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (y));
930 struct elt_loc_list *l;
932 for (l = e->locs; l; l = l->next)
934 rtx t = l->loc;
936 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
937 continue;
938 else if (rtx_equal_for_cselib_1 (x, t, memmode))
939 return 1;
942 return 0;
945 if (GET_MODE (x) != GET_MODE (y))
946 return 0;
948 if (GET_CODE (x) != GET_CODE (y))
950 rtx xorig = x, yorig = y;
951 rtx xoff = NULL, yoff = NULL;
953 x = autoinc_split (x, &xoff, memmode);
954 y = autoinc_split (y, &yoff, memmode);
956 if (!xoff != !yoff)
957 return 0;
959 if (xoff && !rtx_equal_for_cselib_1 (xoff, yoff, memmode))
960 return 0;
962 /* Don't recurse if nothing changed. */
963 if (x != xorig || y != yorig)
964 return rtx_equal_for_cselib_1 (x, y, memmode);
966 return 0;
969 /* These won't be handled correctly by the code below. */
970 switch (GET_CODE (x))
972 CASE_CONST_UNIQUE:
973 case DEBUG_EXPR:
974 return 0;
976 case DEBUG_IMPLICIT_PTR:
977 return DEBUG_IMPLICIT_PTR_DECL (x)
978 == DEBUG_IMPLICIT_PTR_DECL (y);
980 case DEBUG_PARAMETER_REF:
981 return DEBUG_PARAMETER_REF_DECL (x)
982 == DEBUG_PARAMETER_REF_DECL (y);
984 case ENTRY_VALUE:
985 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
986 use rtx_equal_for_cselib_1 to compare the operands. */
987 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
989 case LABEL_REF:
990 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
992 case REG:
993 return REGNO (x) == REGNO (y);
995 case MEM:
996 /* We have to compare any autoinc operations in the addresses
997 using this MEM's mode. */
998 return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x));
1000 default:
1001 break;
1004 code = GET_CODE (x);
1005 fmt = GET_RTX_FORMAT (code);
1007 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1009 int j;
1011 switch (fmt[i])
1013 case 'w':
1014 if (XWINT (x, i) != XWINT (y, i))
1015 return 0;
1016 break;
1018 case 'n':
1019 case 'i':
1020 if (XINT (x, i) != XINT (y, i))
1021 return 0;
1022 break;
1024 case 'V':
1025 case 'E':
1026 /* Two vectors must have the same length. */
1027 if (XVECLEN (x, i) != XVECLEN (y, i))
1028 return 0;
1030 /* And the corresponding elements must match. */
1031 for (j = 0; j < XVECLEN (x, i); j++)
1032 if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
1033 XVECEXP (y, i, j), memmode))
1034 return 0;
1035 break;
1037 case 'e':
1038 if (i == 1
1039 && targetm.commutative_p (x, UNKNOWN)
1040 && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode)
1041 && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode))
1042 return 1;
1043 if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode))
1044 return 0;
1045 break;
1047 case 'S':
1048 case 's':
1049 if (strcmp (XSTR (x, i), XSTR (y, i)))
1050 return 0;
1051 break;
1053 case 'u':
1054 /* These are just backpointers, so they don't matter. */
1055 break;
1057 case '0':
1058 case 't':
1059 break;
1061 /* It is believed that rtx's at this level will never
1062 contain anything but integers and other rtx's,
1063 except for within LABEL_REFs and SYMBOL_REFs. */
1064 default:
1065 gcc_unreachable ();
1068 return 1;
1071 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1072 For registers and memory locations, we look up their cselib_val structure
1073 and return its VALUE element.
1074 Possible reasons for return 0 are: the object is volatile, or we couldn't
1075 find a register or memory location in the table and CREATE is zero. If
1076 CREATE is nonzero, table elts are created for regs and mem.
1077 N.B. this hash function returns the same hash value for RTXes that
1078 differ only in the order of operands, thus it is suitable for comparisons
1079 that take commutativity into account.
1080 If we wanted to also support associative rules, we'd have to use a different
1081 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1082 MEMMODE indicates the mode of an enclosing MEM, and it's only
1083 used to compute autoinc values.
1084 We used to have a MODE argument for hashing for CONST_INTs, but that
1085 didn't make sense, since it caused spurious hash differences between
1086 (set (reg:SI 1) (const_int))
1087 (plus:SI (reg:SI 2) (reg:SI 1))
1089 (plus:SI (reg:SI 2) (const_int))
1090 If the mode is important in any context, it must be checked specifically
1091 in a comparison anyway, since relying on hash differences is unsafe. */
1093 static unsigned int
1094 cselib_hash_rtx (rtx x, int create, machine_mode memmode)
1096 cselib_val *e;
1097 int i, j;
1098 enum rtx_code code;
1099 const char *fmt;
1100 unsigned int hash = 0;
1102 code = GET_CODE (x);
1103 hash += (unsigned) code + (unsigned) GET_MODE (x);
1105 switch (code)
1107 case VALUE:
1108 e = CSELIB_VAL_PTR (x);
1109 return e->hash;
1111 case MEM:
1112 case REG:
1113 e = cselib_lookup (x, GET_MODE (x), create, memmode);
1114 if (! e)
1115 return 0;
1117 return e->hash;
1119 case DEBUG_EXPR:
1120 hash += ((unsigned) DEBUG_EXPR << 7)
1121 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
1122 return hash ? hash : (unsigned int) DEBUG_EXPR;
1124 case DEBUG_IMPLICIT_PTR:
1125 hash += ((unsigned) DEBUG_IMPLICIT_PTR << 7)
1126 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x));
1127 return hash ? hash : (unsigned int) DEBUG_IMPLICIT_PTR;
1129 case DEBUG_PARAMETER_REF:
1130 hash += ((unsigned) DEBUG_PARAMETER_REF << 7)
1131 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x));
1132 return hash ? hash : (unsigned int) DEBUG_PARAMETER_REF;
1134 case ENTRY_VALUE:
1135 /* ENTRY_VALUEs are function invariant, thus try to avoid
1136 recursing on argument if ENTRY_VALUE is one of the
1137 forms emitted by expand_debug_expr, otherwise
1138 ENTRY_VALUE hash would depend on the current value
1139 in some register or memory. */
1140 if (REG_P (ENTRY_VALUE_EXP (x)))
1141 hash += (unsigned int) REG
1142 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x))
1143 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x));
1144 else if (MEM_P (ENTRY_VALUE_EXP (x))
1145 && REG_P (XEXP (ENTRY_VALUE_EXP (x), 0)))
1146 hash += (unsigned int) MEM
1147 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x), 0))
1148 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x), 0));
1149 else
1150 hash += cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode);
1151 return hash ? hash : (unsigned int) ENTRY_VALUE;
1153 case CONST_INT:
1154 hash += ((unsigned) CONST_INT << 7) + UINTVAL (x);
1155 return hash ? hash : (unsigned int) CONST_INT;
1157 case CONST_WIDE_INT:
1158 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
1159 hash += CONST_WIDE_INT_ELT (x, i);
1160 return hash;
1162 case CONST_DOUBLE:
1163 /* This is like the general case, except that it only counts
1164 the integers representing the constant. */
1165 hash += (unsigned) code + (unsigned) GET_MODE (x);
1166 if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (x) == VOIDmode)
1167 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1168 + (unsigned) CONST_DOUBLE_HIGH (x));
1169 else
1170 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
1171 return hash ? hash : (unsigned int) CONST_DOUBLE;
1173 case CONST_FIXED:
1174 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1175 hash += fixed_hash (CONST_FIXED_VALUE (x));
1176 return hash ? hash : (unsigned int) CONST_FIXED;
1178 case CONST_VECTOR:
1180 int units;
1181 rtx elt;
1183 units = CONST_VECTOR_NUNITS (x);
1185 for (i = 0; i < units; ++i)
1187 elt = CONST_VECTOR_ELT (x, i);
1188 hash += cselib_hash_rtx (elt, 0, memmode);
1191 return hash;
1194 /* Assume there is only one rtx object for any given label. */
1195 case LABEL_REF:
1196 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1197 differences and differences between each stage's debugging dumps. */
1198 hash += (((unsigned int) LABEL_REF << 7)
1199 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x)));
1200 return hash ? hash : (unsigned int) LABEL_REF;
1202 case SYMBOL_REF:
1204 /* Don't hash on the symbol's address to avoid bootstrap differences.
1205 Different hash values may cause expressions to be recorded in
1206 different orders and thus different registers to be used in the
1207 final assembler. This also avoids differences in the dump files
1208 between various stages. */
1209 unsigned int h = 0;
1210 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1212 while (*p)
1213 h += (h << 7) + *p++; /* ??? revisit */
1215 hash += ((unsigned int) SYMBOL_REF << 7) + h;
1216 return hash ? hash : (unsigned int) SYMBOL_REF;
1219 case PRE_DEC:
1220 case PRE_INC:
1221 /* We can't compute these without knowing the MEM mode. */
1222 gcc_assert (memmode != VOIDmode);
1223 i = GET_MODE_SIZE (memmode);
1224 if (code == PRE_DEC)
1225 i = -i;
1226 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1227 like (mem:MEMMODE (plus (reg) (const_int I))). */
1228 hash += (unsigned) PLUS - (unsigned)code
1229 + cselib_hash_rtx (XEXP (x, 0), create, memmode)
1230 + cselib_hash_rtx (GEN_INT (i), create, memmode);
1231 return hash ? hash : 1 + (unsigned) PLUS;
1233 case PRE_MODIFY:
1234 gcc_assert (memmode != VOIDmode);
1235 return cselib_hash_rtx (XEXP (x, 1), create, memmode);
1237 case POST_DEC:
1238 case POST_INC:
1239 case POST_MODIFY:
1240 gcc_assert (memmode != VOIDmode);
1241 return cselib_hash_rtx (XEXP (x, 0), create, memmode);
1243 case PC:
1244 case CC0:
1245 case CALL:
1246 case UNSPEC_VOLATILE:
1247 return 0;
1249 case ASM_OPERANDS:
1250 if (MEM_VOLATILE_P (x))
1251 return 0;
1253 break;
1255 default:
1256 break;
1259 i = GET_RTX_LENGTH (code) - 1;
1260 fmt = GET_RTX_FORMAT (code);
1261 for (; i >= 0; i--)
1263 switch (fmt[i])
1265 case 'e':
1267 rtx tem = XEXP (x, i);
1268 unsigned int tem_hash = cselib_hash_rtx (tem, create, memmode);
1270 if (tem_hash == 0)
1271 return 0;
1273 hash += tem_hash;
1275 break;
1276 case 'E':
1277 for (j = 0; j < XVECLEN (x, i); j++)
1279 unsigned int tem_hash
1280 = cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
1282 if (tem_hash == 0)
1283 return 0;
1285 hash += tem_hash;
1287 break;
1289 case 's':
1291 const unsigned char *p = (const unsigned char *) XSTR (x, i);
1293 if (p)
1294 while (*p)
1295 hash += *p++;
1296 break;
1299 case 'i':
1300 hash += XINT (x, i);
1301 break;
1303 case '0':
1304 case 't':
1305 /* unused */
1306 break;
1308 default:
1309 gcc_unreachable ();
1313 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
1316 /* Create a new value structure for VALUE and initialize it. The mode of the
1317 value is MODE. */
1319 static inline cselib_val *
1320 new_cselib_val (unsigned int hash, machine_mode mode, rtx x)
1322 cselib_val *e = new cselib_val;
1324 gcc_assert (hash);
1325 gcc_assert (next_uid);
1327 e->hash = hash;
1328 e->uid = next_uid++;
1329 /* We use an alloc pool to allocate this RTL construct because it
1330 accounts for about 8% of the overall memory usage. We know
1331 precisely when we can have VALUE RTXen (when cselib is active)
1332 so we don't need to put them in garbage collected memory.
1333 ??? Why should a VALUE be an RTX in the first place? */
1334 e->val_rtx = value_pool.allocate ();
1335 memset (e->val_rtx, 0, RTX_HDR_SIZE);
1336 PUT_CODE (e->val_rtx, VALUE);
1337 PUT_MODE (e->val_rtx, mode);
1338 CSELIB_VAL_PTR (e->val_rtx) = e;
1339 e->addr_list = 0;
1340 e->locs = 0;
1341 e->next_containing_mem = 0;
1343 if (dump_file && (dump_flags & TDF_CSELIB))
1345 fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
1346 if (flag_dump_noaddr || flag_dump_unnumbered)
1347 fputs ("# ", dump_file);
1348 else
1349 fprintf (dump_file, "%p ", (void*)e);
1350 print_rtl_single (dump_file, x);
1351 fputc ('\n', dump_file);
1354 return e;
1357 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1358 contains the data at this address. X is a MEM that represents the
1359 value. Update the two value structures to represent this situation. */
1361 static void
1362 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
1364 struct elt_loc_list *l;
1366 addr_elt = canonical_cselib_val (addr_elt);
1367 mem_elt = canonical_cselib_val (mem_elt);
1369 /* Avoid duplicates. */
1370 for (l = mem_elt->locs; l; l = l->next)
1371 if (MEM_P (l->loc)
1372 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
1374 promote_debug_loc (l);
1375 return;
1378 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
1379 new_elt_loc_list (mem_elt,
1380 replace_equiv_address_nv (x, addr_elt->val_rtx));
1381 if (mem_elt->next_containing_mem == NULL)
1383 mem_elt->next_containing_mem = first_containing_mem;
1384 first_containing_mem = mem_elt;
1388 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1389 If CREATE, make a new one if we haven't seen it before. */
1391 static cselib_val *
1392 cselib_lookup_mem (rtx x, int create)
1394 machine_mode mode = GET_MODE (x);
1395 machine_mode addr_mode;
1396 cselib_val **slot;
1397 cselib_val *addr;
1398 cselib_val *mem_elt;
1399 struct elt_list *l;
1401 if (MEM_VOLATILE_P (x) || mode == BLKmode
1402 || !cselib_record_memory
1403 || (FLOAT_MODE_P (mode) && flag_float_store))
1404 return 0;
1406 addr_mode = GET_MODE (XEXP (x, 0));
1407 if (addr_mode == VOIDmode)
1408 addr_mode = Pmode;
1410 /* Look up the value for the address. */
1411 addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
1412 if (! addr)
1413 return 0;
1415 addr = canonical_cselib_val (addr);
1416 /* Find a value that describes a value of our mode at that address. */
1417 for (l = addr->addr_list; l; l = l->next)
1418 if (GET_MODE (l->elt->val_rtx) == mode)
1420 promote_debug_loc (l->elt->locs);
1421 return l->elt;
1424 if (! create)
1425 return 0;
1427 mem_elt = new_cselib_val (next_uid, mode, x);
1428 add_mem_for_addr (addr, mem_elt, x);
1429 slot = cselib_find_slot (mode, x, mem_elt->hash, INSERT, VOIDmode);
1430 *slot = mem_elt;
1431 return mem_elt;
1434 /* Search through the possible substitutions in P. We prefer a non reg
1435 substitution because this allows us to expand the tree further. If
1436 we find, just a reg, take the lowest regno. There may be several
1437 non-reg results, we just take the first one because they will all
1438 expand to the same place. */
1440 static rtx
1441 expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1442 int max_depth)
1444 rtx reg_result = NULL;
1445 unsigned int regno = UINT_MAX;
1446 struct elt_loc_list *p_in = p;
1448 for (; p; p = p->next)
1450 /* Return these right away to avoid returning stack pointer based
1451 expressions for frame pointer and vice versa, which is something
1452 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1453 for more details. */
1454 if (REG_P (p->loc)
1455 && (REGNO (p->loc) == STACK_POINTER_REGNUM
1456 || REGNO (p->loc) == FRAME_POINTER_REGNUM
1457 || REGNO (p->loc) == HARD_FRAME_POINTER_REGNUM
1458 || REGNO (p->loc) == cfa_base_preserved_regno))
1459 return p->loc;
1460 /* Avoid infinite recursion trying to expand a reg into a
1461 the same reg. */
1462 if ((REG_P (p->loc))
1463 && (REGNO (p->loc) < regno)
1464 && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
1466 reg_result = p->loc;
1467 regno = REGNO (p->loc);
1469 /* Avoid infinite recursion and do not try to expand the
1470 value. */
1471 else if (GET_CODE (p->loc) == VALUE
1472 && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1473 continue;
1474 else if (!REG_P (p->loc))
1476 rtx result, note;
1477 if (dump_file && (dump_flags & TDF_CSELIB))
1479 print_inline_rtx (dump_file, p->loc, 0);
1480 fprintf (dump_file, "\n");
1482 if (GET_CODE (p->loc) == LO_SUM
1483 && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1484 && p->setting_insn
1485 && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1486 && XEXP (note, 0) == XEXP (p->loc, 1))
1487 return XEXP (p->loc, 1);
1488 result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
1489 if (result)
1490 return result;
1495 if (regno != UINT_MAX)
1497 rtx result;
1498 if (dump_file && (dump_flags & TDF_CSELIB))
1499 fprintf (dump_file, "r%d\n", regno);
1501 result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
1502 if (result)
1503 return result;
1506 if (dump_file && (dump_flags & TDF_CSELIB))
1508 if (reg_result)
1510 print_inline_rtx (dump_file, reg_result, 0);
1511 fprintf (dump_file, "\n");
1513 else
1514 fprintf (dump_file, "NULL\n");
1516 return reg_result;
1520 /* Forward substitute and expand an expression out to its roots.
1521 This is the opposite of common subexpression. Because local value
1522 numbering is such a weak optimization, the expanded expression is
1523 pretty much unique (not from a pointer equals point of view but
1524 from a tree shape point of view.
1526 This function returns NULL if the expansion fails. The expansion
1527 will fail if there is no value number for one of the operands or if
1528 one of the operands has been overwritten between the current insn
1529 and the beginning of the basic block. For instance x has no
1530 expansion in:
1532 r1 <- r1 + 3
1533 x <- r1 + 8
1535 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1536 It is clear on return. */
1539 cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
1541 struct expand_value_data evd;
1543 evd.regs_active = regs_active;
1544 evd.callback = NULL;
1545 evd.callback_arg = NULL;
1546 evd.dummy = false;
1548 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1551 /* Same as cselib_expand_value_rtx, but using a callback to try to
1552 resolve some expressions. The CB function should return ORIG if it
1553 can't or does not want to deal with a certain RTX. Any other
1554 return value, including NULL, will be used as the expansion for
1555 VALUE, without any further changes. */
1558 cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1559 cselib_expand_callback cb, void *data)
1561 struct expand_value_data evd;
1563 evd.regs_active = regs_active;
1564 evd.callback = cb;
1565 evd.callback_arg = data;
1566 evd.dummy = false;
1568 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1571 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1572 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1573 would return NULL or non-NULL, without allocating new rtx. */
1575 bool
1576 cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1577 cselib_expand_callback cb, void *data)
1579 struct expand_value_data evd;
1581 evd.regs_active = regs_active;
1582 evd.callback = cb;
1583 evd.callback_arg = data;
1584 evd.dummy = true;
1586 return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1589 /* Internal implementation of cselib_expand_value_rtx and
1590 cselib_expand_value_rtx_cb. */
1592 static rtx
1593 cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1594 int max_depth)
1596 rtx copy, scopy;
1597 int i, j;
1598 RTX_CODE code;
1599 const char *format_ptr;
1600 machine_mode mode;
1602 code = GET_CODE (orig);
1604 /* For the context of dse, if we end up expand into a huge tree, we
1605 will not have a useful address, so we might as well just give up
1606 quickly. */
1607 if (max_depth <= 0)
1608 return NULL;
1610 switch (code)
1612 case REG:
1614 struct elt_list *l = REG_VALUES (REGNO (orig));
1616 if (l && l->elt == NULL)
1617 l = l->next;
1618 for (; l; l = l->next)
1619 if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1621 rtx result;
1622 unsigned regno = REGNO (orig);
1624 /* The only thing that we are not willing to do (this
1625 is requirement of dse and if others potential uses
1626 need this function we should add a parm to control
1627 it) is that we will not substitute the
1628 STACK_POINTER_REGNUM, FRAME_POINTER or the
1629 HARD_FRAME_POINTER.
1631 These expansions confuses the code that notices that
1632 stores into the frame go dead at the end of the
1633 function and that the frame is not effected by calls
1634 to subroutines. If you allow the
1635 STACK_POINTER_REGNUM substitution, then dse will
1636 think that parameter pushing also goes dead which is
1637 wrong. If you allow the FRAME_POINTER or the
1638 HARD_FRAME_POINTER then you lose the opportunity to
1639 make the frame assumptions. */
1640 if (regno == STACK_POINTER_REGNUM
1641 || regno == FRAME_POINTER_REGNUM
1642 || regno == HARD_FRAME_POINTER_REGNUM
1643 || regno == cfa_base_preserved_regno)
1644 return orig;
1646 bitmap_set_bit (evd->regs_active, regno);
1648 if (dump_file && (dump_flags & TDF_CSELIB))
1649 fprintf (dump_file, "expanding: r%d into: ", regno);
1651 result = expand_loc (l->elt->locs, evd, max_depth);
1652 bitmap_clear_bit (evd->regs_active, regno);
1654 if (result)
1655 return result;
1656 else
1657 return orig;
1661 CASE_CONST_ANY:
1662 case SYMBOL_REF:
1663 case CODE_LABEL:
1664 case PC:
1665 case CC0:
1666 case SCRATCH:
1667 /* SCRATCH must be shared because they represent distinct values. */
1668 return orig;
1669 case CLOBBER:
1670 if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1671 return orig;
1672 break;
1674 case CONST:
1675 if (shared_const_p (orig))
1676 return orig;
1677 break;
1679 case SUBREG:
1681 rtx subreg;
1683 if (evd->callback)
1685 subreg = evd->callback (orig, evd->regs_active, max_depth,
1686 evd->callback_arg);
1687 if (subreg != orig)
1688 return subreg;
1691 subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1692 max_depth - 1);
1693 if (!subreg)
1694 return NULL;
1695 scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1696 GET_MODE (SUBREG_REG (orig)),
1697 SUBREG_BYTE (orig));
1698 if (scopy == NULL
1699 || (GET_CODE (scopy) == SUBREG
1700 && !REG_P (SUBREG_REG (scopy))
1701 && !MEM_P (SUBREG_REG (scopy))))
1702 return NULL;
1704 return scopy;
1707 case VALUE:
1709 rtx result;
1711 if (dump_file && (dump_flags & TDF_CSELIB))
1713 fputs ("\nexpanding ", dump_file);
1714 print_rtl_single (dump_file, orig);
1715 fputs (" into...", dump_file);
1718 if (evd->callback)
1720 result = evd->callback (orig, evd->regs_active, max_depth,
1721 evd->callback_arg);
1723 if (result != orig)
1724 return result;
1727 result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
1728 return result;
1731 case DEBUG_EXPR:
1732 if (evd->callback)
1733 return evd->callback (orig, evd->regs_active, max_depth,
1734 evd->callback_arg);
1735 return orig;
1737 default:
1738 break;
1741 /* Copy the various flags, fields, and other information. We assume
1742 that all fields need copying, and then clear the fields that should
1743 not be copied. That is the sensible default behavior, and forces
1744 us to explicitly document why we are *not* copying a flag. */
1745 if (evd->dummy)
1746 copy = NULL;
1747 else
1748 copy = shallow_copy_rtx (orig);
1750 format_ptr = GET_RTX_FORMAT (code);
1752 for (i = 0; i < GET_RTX_LENGTH (code); i++)
1753 switch (*format_ptr++)
1755 case 'e':
1756 if (XEXP (orig, i) != NULL)
1758 rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1759 max_depth - 1);
1760 if (!result)
1761 return NULL;
1762 if (copy)
1763 XEXP (copy, i) = result;
1765 break;
1767 case 'E':
1768 case 'V':
1769 if (XVEC (orig, i) != NULL)
1771 if (copy)
1772 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1773 for (j = 0; j < XVECLEN (orig, i); j++)
1775 rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1776 evd, max_depth - 1);
1777 if (!result)
1778 return NULL;
1779 if (copy)
1780 XVECEXP (copy, i, j) = result;
1783 break;
1785 case 't':
1786 case 'w':
1787 case 'i':
1788 case 's':
1789 case 'S':
1790 case 'T':
1791 case 'u':
1792 case 'B':
1793 case '0':
1794 /* These are left unchanged. */
1795 break;
1797 default:
1798 gcc_unreachable ();
1801 if (evd->dummy)
1802 return orig;
1804 mode = GET_MODE (copy);
1805 /* If an operand has been simplified into CONST_INT, which doesn't
1806 have a mode and the mode isn't derivable from whole rtx's mode,
1807 try simplify_*_operation first with mode from original's operand
1808 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1809 scopy = copy;
1810 switch (GET_RTX_CLASS (code))
1812 case RTX_UNARY:
1813 if (CONST_INT_P (XEXP (copy, 0))
1814 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1816 scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1817 GET_MODE (XEXP (orig, 0)));
1818 if (scopy)
1819 return scopy;
1821 break;
1822 case RTX_COMM_ARITH:
1823 case RTX_BIN_ARITH:
1824 /* These expressions can derive operand modes from the whole rtx's mode. */
1825 break;
1826 case RTX_TERNARY:
1827 case RTX_BITFIELD_OPS:
1828 if (CONST_INT_P (XEXP (copy, 0))
1829 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1831 scopy = simplify_ternary_operation (code, mode,
1832 GET_MODE (XEXP (orig, 0)),
1833 XEXP (copy, 0), XEXP (copy, 1),
1834 XEXP (copy, 2));
1835 if (scopy)
1836 return scopy;
1838 break;
1839 case RTX_COMPARE:
1840 case RTX_COMM_COMPARE:
1841 if (CONST_INT_P (XEXP (copy, 0))
1842 && GET_MODE (XEXP (copy, 1)) == VOIDmode
1843 && (GET_MODE (XEXP (orig, 0)) != VOIDmode
1844 || GET_MODE (XEXP (orig, 1)) != VOIDmode))
1846 scopy = simplify_relational_operation (code, mode,
1847 (GET_MODE (XEXP (orig, 0))
1848 != VOIDmode)
1849 ? GET_MODE (XEXP (orig, 0))
1850 : GET_MODE (XEXP (orig, 1)),
1851 XEXP (copy, 0),
1852 XEXP (copy, 1));
1853 if (scopy)
1854 return scopy;
1856 break;
1857 default:
1858 break;
1860 scopy = simplify_rtx (copy);
1861 if (scopy)
1862 return scopy;
1863 return copy;
1866 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1867 with VALUE expressions. This way, it becomes independent of changes
1868 to registers and memory.
1869 X isn't actually modified; if modifications are needed, new rtl is
1870 allocated. However, the return value can share rtl with X.
1871 If X is within a MEM, MEMMODE must be the mode of the MEM. */
1874 cselib_subst_to_values (rtx x, machine_mode memmode)
1876 enum rtx_code code = GET_CODE (x);
1877 const char *fmt = GET_RTX_FORMAT (code);
1878 cselib_val *e;
1879 struct elt_list *l;
1880 rtx copy = x;
1881 int i;
1883 switch (code)
1885 case REG:
1886 l = REG_VALUES (REGNO (x));
1887 if (l && l->elt == NULL)
1888 l = l->next;
1889 for (; l; l = l->next)
1890 if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
1891 return l->elt->val_rtx;
1893 gcc_unreachable ();
1895 case MEM:
1896 e = cselib_lookup_mem (x, 0);
1897 /* This used to happen for autoincrements, but we deal with them
1898 properly now. Remove the if stmt for the next release. */
1899 if (! e)
1901 /* Assign a value that doesn't match any other. */
1902 e = new_cselib_val (next_uid, GET_MODE (x), x);
1904 return e->val_rtx;
1906 case ENTRY_VALUE:
1907 e = cselib_lookup (x, GET_MODE (x), 0, memmode);
1908 if (! e)
1909 break;
1910 return e->val_rtx;
1912 CASE_CONST_ANY:
1913 return x;
1915 case PRE_DEC:
1916 case PRE_INC:
1917 gcc_assert (memmode != VOIDmode);
1918 i = GET_MODE_SIZE (memmode);
1919 if (code == PRE_DEC)
1920 i = -i;
1921 return cselib_subst_to_values (plus_constant (GET_MODE (x),
1922 XEXP (x, 0), i),
1923 memmode);
1925 case PRE_MODIFY:
1926 gcc_assert (memmode != VOIDmode);
1927 return cselib_subst_to_values (XEXP (x, 1), memmode);
1929 case POST_DEC:
1930 case POST_INC:
1931 case POST_MODIFY:
1932 gcc_assert (memmode != VOIDmode);
1933 return cselib_subst_to_values (XEXP (x, 0), memmode);
1935 default:
1936 break;
1939 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1941 if (fmt[i] == 'e')
1943 rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
1945 if (t != XEXP (x, i))
1947 if (x == copy)
1948 copy = shallow_copy_rtx (x);
1949 XEXP (copy, i) = t;
1952 else if (fmt[i] == 'E')
1954 int j;
1956 for (j = 0; j < XVECLEN (x, i); j++)
1958 rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
1960 if (t != XVECEXP (x, i, j))
1962 if (XVEC (x, i) == XVEC (copy, i))
1964 if (x == copy)
1965 copy = shallow_copy_rtx (x);
1966 XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
1968 XVECEXP (copy, i, j) = t;
1974 return copy;
1977 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
1980 cselib_subst_to_values_from_insn (rtx x, machine_mode memmode, rtx_insn *insn)
1982 rtx ret;
1983 gcc_assert (!cselib_current_insn);
1984 cselib_current_insn = insn;
1985 ret = cselib_subst_to_values (x, memmode);
1986 cselib_current_insn = NULL;
1987 return ret;
1990 /* Look up the rtl expression X in our tables and return the value it
1991 has. If CREATE is zero, we return NULL if we don't know the value.
1992 Otherwise, we create a new one if possible, using mode MODE if X
1993 doesn't have a mode (i.e. because it's a constant). When X is part
1994 of an address, MEMMODE should be the mode of the enclosing MEM if
1995 we're tracking autoinc expressions. */
1997 static cselib_val *
1998 cselib_lookup_1 (rtx x, machine_mode mode,
1999 int create, machine_mode memmode)
2001 cselib_val **slot;
2002 cselib_val *e;
2003 unsigned int hashval;
2005 if (GET_MODE (x) != VOIDmode)
2006 mode = GET_MODE (x);
2008 if (GET_CODE (x) == VALUE)
2009 return CSELIB_VAL_PTR (x);
2011 if (REG_P (x))
2013 struct elt_list *l;
2014 unsigned int i = REGNO (x);
2016 l = REG_VALUES (i);
2017 if (l && l->elt == NULL)
2018 l = l->next;
2019 for (; l; l = l->next)
2020 if (mode == GET_MODE (l->elt->val_rtx))
2022 promote_debug_loc (l->elt->locs);
2023 return l->elt;
2026 if (! create)
2027 return 0;
2029 if (i < FIRST_PSEUDO_REGISTER)
2031 unsigned int n = hard_regno_nregs[i][mode];
2033 if (n > max_value_regs)
2034 max_value_regs = n;
2037 e = new_cselib_val (next_uid, GET_MODE (x), x);
2038 new_elt_loc_list (e, x);
2039 if (REG_VALUES (i) == 0)
2041 /* Maintain the invariant that the first entry of
2042 REG_VALUES, if present, must be the value used to set the
2043 register, or NULL. */
2044 used_regs[n_used_regs++] = i;
2045 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
2047 else if (cselib_preserve_constants
2048 && GET_MODE_CLASS (mode) == MODE_INT)
2050 /* During var-tracking, try harder to find equivalences
2051 for SUBREGs. If a setter sets say a DImode register
2052 and user uses that register only in SImode, add a lowpart
2053 subreg location. */
2054 struct elt_list *lwider = NULL;
2055 l = REG_VALUES (i);
2056 if (l && l->elt == NULL)
2057 l = l->next;
2058 for (; l; l = l->next)
2059 if (GET_MODE_CLASS (GET_MODE (l->elt->val_rtx)) == MODE_INT
2060 && GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2061 > GET_MODE_SIZE (mode)
2062 && (lwider == NULL
2063 || GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2064 < GET_MODE_SIZE (GET_MODE (lwider->elt->val_rtx))))
2066 struct elt_loc_list *el;
2067 if (i < FIRST_PSEUDO_REGISTER
2068 && hard_regno_nregs[i][GET_MODE (l->elt->val_rtx)] != 1)
2069 continue;
2070 for (el = l->elt->locs; el; el = el->next)
2071 if (!REG_P (el->loc))
2072 break;
2073 if (el)
2074 lwider = l;
2076 if (lwider)
2078 rtx sub = lowpart_subreg (mode, lwider->elt->val_rtx,
2079 GET_MODE (lwider->elt->val_rtx));
2080 if (sub)
2081 new_elt_loc_list (e, sub);
2084 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
2085 slot = cselib_find_slot (mode, x, e->hash, INSERT, memmode);
2086 *slot = e;
2087 return e;
2090 if (MEM_P (x))
2091 return cselib_lookup_mem (x, create);
2093 hashval = cselib_hash_rtx (x, create, memmode);
2094 /* Can't even create if hashing is not possible. */
2095 if (! hashval)
2096 return 0;
2098 slot = cselib_find_slot (mode, x, hashval,
2099 create ? INSERT : NO_INSERT, memmode);
2100 if (slot == 0)
2101 return 0;
2103 e = (cselib_val *) *slot;
2104 if (e)
2105 return e;
2107 e = new_cselib_val (hashval, mode, x);
2109 /* We have to fill the slot before calling cselib_subst_to_values:
2110 the hash table is inconsistent until we do so, and
2111 cselib_subst_to_values will need to do lookups. */
2112 *slot = e;
2113 new_elt_loc_list (e, cselib_subst_to_values (x, memmode));
2114 return e;
2117 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2119 cselib_val *
2120 cselib_lookup_from_insn (rtx x, machine_mode mode,
2121 int create, machine_mode memmode, rtx_insn *insn)
2123 cselib_val *ret;
2125 gcc_assert (!cselib_current_insn);
2126 cselib_current_insn = insn;
2128 ret = cselib_lookup (x, mode, create, memmode);
2130 cselib_current_insn = NULL;
2132 return ret;
2135 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2136 maintains invariants related with debug insns. */
2138 cselib_val *
2139 cselib_lookup (rtx x, machine_mode mode,
2140 int create, machine_mode memmode)
2142 cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
2144 /* ??? Should we return NULL if we're not to create an entry, the
2145 found loc is a debug loc and cselib_current_insn is not DEBUG?
2146 If so, we should also avoid converting val to non-DEBUG; probably
2147 easiest setting cselib_current_insn to NULL before the call
2148 above. */
2150 if (dump_file && (dump_flags & TDF_CSELIB))
2152 fputs ("cselib lookup ", dump_file);
2153 print_inline_rtx (dump_file, x, 2);
2154 fprintf (dump_file, " => %u:%u\n",
2155 ret ? ret->uid : 0,
2156 ret ? ret->hash : 0);
2159 return ret;
2162 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2163 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2164 is used to determine how many hard registers are being changed. If MODE
2165 is VOIDmode, then only REGNO is being changed; this is used when
2166 invalidating call clobbered registers across a call. */
2168 static void
2169 cselib_invalidate_regno (unsigned int regno, machine_mode mode)
2171 unsigned int endregno;
2172 unsigned int i;
2174 /* If we see pseudos after reload, something is _wrong_. */
2175 gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
2176 || reg_renumber[regno] < 0);
2178 /* Determine the range of registers that must be invalidated. For
2179 pseudos, only REGNO is affected. For hard regs, we must take MODE
2180 into account, and we must also invalidate lower register numbers
2181 if they contain values that overlap REGNO. */
2182 if (regno < FIRST_PSEUDO_REGISTER)
2184 gcc_assert (mode != VOIDmode);
2186 if (regno < max_value_regs)
2187 i = 0;
2188 else
2189 i = regno - max_value_regs;
2191 endregno = end_hard_regno (mode, regno);
2193 else
2195 i = regno;
2196 endregno = regno + 1;
2199 for (; i < endregno; i++)
2201 struct elt_list **l = &REG_VALUES (i);
2203 /* Go through all known values for this reg; if it overlaps the range
2204 we're invalidating, remove the value. */
2205 while (*l)
2207 cselib_val *v = (*l)->elt;
2208 bool had_locs;
2209 rtx_insn *setting_insn;
2210 struct elt_loc_list **p;
2211 unsigned int this_last = i;
2213 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
2214 this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
2216 if (this_last < regno || v == NULL
2217 || (v == cfa_base_preserved_val
2218 && i == cfa_base_preserved_regno))
2220 l = &(*l)->next;
2221 continue;
2224 /* We have an overlap. */
2225 if (*l == REG_VALUES (i))
2227 /* Maintain the invariant that the first entry of
2228 REG_VALUES, if present, must be the value used to set
2229 the register, or NULL. This is also nice because
2230 then we won't push the same regno onto user_regs
2231 multiple times. */
2232 (*l)->elt = NULL;
2233 l = &(*l)->next;
2235 else
2236 unchain_one_elt_list (l);
2238 v = canonical_cselib_val (v);
2240 had_locs = v->locs != NULL;
2241 setting_insn = v->locs ? v->locs->setting_insn : NULL;
2243 /* Now, we clear the mapping from value to reg. It must exist, so
2244 this code will crash intentionally if it doesn't. */
2245 for (p = &v->locs; ; p = &(*p)->next)
2247 rtx x = (*p)->loc;
2249 if (REG_P (x) && REGNO (x) == i)
2251 unchain_one_elt_loc_list (p);
2252 break;
2256 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2258 if (setting_insn && DEBUG_INSN_P (setting_insn))
2259 n_useless_debug_values++;
2260 else
2261 n_useless_values++;
2267 /* Invalidate any locations in the table which are changed because of a
2268 store to MEM_RTX. If this is called because of a non-const call
2269 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2271 static void
2272 cselib_invalidate_mem (rtx mem_rtx)
2274 cselib_val **vp, *v, *next;
2275 int num_mems = 0;
2276 rtx mem_addr;
2278 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2279 mem_rtx = canon_rtx (mem_rtx);
2281 vp = &first_containing_mem;
2282 for (v = *vp; v != &dummy_val; v = next)
2284 bool has_mem = false;
2285 struct elt_loc_list **p = &v->locs;
2286 bool had_locs = v->locs != NULL;
2287 rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
2289 while (*p)
2291 rtx x = (*p)->loc;
2292 cselib_val *addr;
2293 struct elt_list **mem_chain;
2295 /* MEMs may occur in locations only at the top level; below
2296 that every MEM or REG is substituted by its VALUE. */
2297 if (!MEM_P (x))
2299 p = &(*p)->next;
2300 continue;
2302 if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
2303 && ! canon_anti_dependence (x, false, mem_rtx,
2304 GET_MODE (mem_rtx), mem_addr))
2306 has_mem = true;
2307 num_mems++;
2308 p = &(*p)->next;
2309 continue;
2312 /* This one overlaps. */
2313 /* We must have a mapping from this MEM's address to the
2314 value (E). Remove that, too. */
2315 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
2316 addr = canonical_cselib_val (addr);
2317 gcc_checking_assert (v == canonical_cselib_val (v));
2318 mem_chain = &addr->addr_list;
2319 for (;;)
2321 cselib_val *canon = canonical_cselib_val ((*mem_chain)->elt);
2323 if (canon == v)
2325 unchain_one_elt_list (mem_chain);
2326 break;
2329 /* Record canonicalized elt. */
2330 (*mem_chain)->elt = canon;
2332 mem_chain = &(*mem_chain)->next;
2335 unchain_one_elt_loc_list (p);
2338 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2340 if (setting_insn && DEBUG_INSN_P (setting_insn))
2341 n_useless_debug_values++;
2342 else
2343 n_useless_values++;
2346 next = v->next_containing_mem;
2347 if (has_mem)
2349 *vp = v;
2350 vp = &(*vp)->next_containing_mem;
2352 else
2353 v->next_containing_mem = NULL;
2355 *vp = &dummy_val;
2358 /* Invalidate DEST, which is being assigned to or clobbered. */
2360 void
2361 cselib_invalidate_rtx (rtx dest)
2363 while (GET_CODE (dest) == SUBREG
2364 || GET_CODE (dest) == ZERO_EXTRACT
2365 || GET_CODE (dest) == STRICT_LOW_PART)
2366 dest = XEXP (dest, 0);
2368 if (REG_P (dest))
2369 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
2370 else if (MEM_P (dest))
2371 cselib_invalidate_mem (dest);
2374 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2376 static void
2377 cselib_invalidate_rtx_note_stores (rtx dest, const_rtx ignore ATTRIBUTE_UNUSED,
2378 void *data ATTRIBUTE_UNUSED)
2380 cselib_invalidate_rtx (dest);
2383 /* Record the result of a SET instruction. DEST is being set; the source
2384 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2385 describes its address. */
2387 static void
2388 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
2390 if (src_elt == 0 || side_effects_p (dest))
2391 return;
2393 if (REG_P (dest))
2395 unsigned int dreg = REGNO (dest);
2396 if (dreg < FIRST_PSEUDO_REGISTER)
2398 unsigned int n = REG_NREGS (dest);
2400 if (n > max_value_regs)
2401 max_value_regs = n;
2404 if (REG_VALUES (dreg) == 0)
2406 used_regs[n_used_regs++] = dreg;
2407 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2409 else
2411 /* The register should have been invalidated. */
2412 gcc_assert (REG_VALUES (dreg)->elt == 0);
2413 REG_VALUES (dreg)->elt = src_elt;
2416 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2417 n_useless_values--;
2418 new_elt_loc_list (src_elt, dest);
2420 else if (MEM_P (dest) && dest_addr_elt != 0
2421 && cselib_record_memory)
2423 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2424 n_useless_values--;
2425 add_mem_for_addr (dest_addr_elt, src_elt, dest);
2429 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2431 void
2432 cselib_add_permanent_equiv (cselib_val *elt, rtx x, rtx_insn *insn)
2434 cselib_val *nelt;
2435 rtx_insn *save_cselib_current_insn = cselib_current_insn;
2437 gcc_checking_assert (elt);
2438 gcc_checking_assert (PRESERVED_VALUE_P (elt->val_rtx));
2439 gcc_checking_assert (!side_effects_p (x));
2441 cselib_current_insn = insn;
2443 nelt = cselib_lookup (x, GET_MODE (elt->val_rtx), 1, VOIDmode);
2445 if (nelt != elt)
2447 cselib_any_perm_equivs = true;
2449 if (!PRESERVED_VALUE_P (nelt->val_rtx))
2450 cselib_preserve_value (nelt);
2452 new_elt_loc_list (nelt, elt->val_rtx);
2455 cselib_current_insn = save_cselib_current_insn;
2458 /* Return TRUE if any permanent equivalences have been recorded since
2459 the table was last initialized. */
2460 bool
2461 cselib_have_permanent_equivalences (void)
2463 return cselib_any_perm_equivs;
2466 /* There is no good way to determine how many elements there can be
2467 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2468 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2470 struct cselib_record_autoinc_data
2472 struct cselib_set *sets;
2473 int n_sets;
2476 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2477 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2479 static int
2480 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
2481 rtx dest, rtx src, rtx srcoff, void *arg)
2483 struct cselib_record_autoinc_data *data;
2484 data = (struct cselib_record_autoinc_data *)arg;
2486 data->sets[data->n_sets].dest = dest;
2488 if (srcoff)
2489 data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
2490 else
2491 data->sets[data->n_sets].src = src;
2493 data->n_sets++;
2495 return 0;
2498 /* Record the effects of any sets and autoincs in INSN. */
2499 static void
2500 cselib_record_sets (rtx_insn *insn)
2502 int n_sets = 0;
2503 int i;
2504 struct cselib_set sets[MAX_SETS];
2505 rtx body = PATTERN (insn);
2506 rtx cond = 0;
2507 int n_sets_before_autoinc;
2508 struct cselib_record_autoinc_data data;
2510 body = PATTERN (insn);
2511 if (GET_CODE (body) == COND_EXEC)
2513 cond = COND_EXEC_TEST (body);
2514 body = COND_EXEC_CODE (body);
2517 /* Find all sets. */
2518 if (GET_CODE (body) == SET)
2520 sets[0].src = SET_SRC (body);
2521 sets[0].dest = SET_DEST (body);
2522 n_sets = 1;
2524 else if (GET_CODE (body) == PARALLEL)
2526 /* Look through the PARALLEL and record the values being
2527 set, if possible. Also handle any CLOBBERs. */
2528 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2530 rtx x = XVECEXP (body, 0, i);
2532 if (GET_CODE (x) == SET)
2534 sets[n_sets].src = SET_SRC (x);
2535 sets[n_sets].dest = SET_DEST (x);
2536 n_sets++;
2541 if (n_sets == 1
2542 && MEM_P (sets[0].src)
2543 && !cselib_record_memory
2544 && MEM_READONLY_P (sets[0].src))
2546 rtx note = find_reg_equal_equiv_note (insn);
2548 if (note && CONSTANT_P (XEXP (note, 0)))
2549 sets[0].src = XEXP (note, 0);
2552 data.sets = sets;
2553 data.n_sets = n_sets_before_autoinc = n_sets;
2554 for_each_inc_dec (PATTERN (insn), cselib_record_autoinc_cb, &data);
2555 n_sets = data.n_sets;
2557 /* Look up the values that are read. Do this before invalidating the
2558 locations that are written. */
2559 for (i = 0; i < n_sets; i++)
2561 rtx dest = sets[i].dest;
2563 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2564 the low part after invalidating any knowledge about larger modes. */
2565 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2566 sets[i].dest = dest = XEXP (dest, 0);
2568 /* We don't know how to record anything but REG or MEM. */
2569 if (REG_P (dest)
2570 || (MEM_P (dest) && cselib_record_memory))
2572 rtx src = sets[i].src;
2573 if (cond)
2574 src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
2575 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
2576 if (MEM_P (dest))
2578 machine_mode address_mode = get_address_mode (dest);
2580 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
2581 address_mode, 1,
2582 GET_MODE (dest));
2584 else
2585 sets[i].dest_addr_elt = 0;
2589 if (cselib_record_sets_hook)
2590 cselib_record_sets_hook (insn, sets, n_sets);
2592 /* Invalidate all locations written by this insn. Note that the elts we
2593 looked up in the previous loop aren't affected, just some of their
2594 locations may go away. */
2595 note_stores (body, cselib_invalidate_rtx_note_stores, NULL);
2597 for (i = n_sets_before_autoinc; i < n_sets; i++)
2598 cselib_invalidate_rtx (sets[i].dest);
2600 /* If this is an asm, look for duplicate sets. This can happen when the
2601 user uses the same value as an output multiple times. This is valid
2602 if the outputs are not actually used thereafter. Treat this case as
2603 if the value isn't actually set. We do this by smashing the destination
2604 to pc_rtx, so that we won't record the value later. */
2605 if (n_sets >= 2 && asm_noperands (body) >= 0)
2607 for (i = 0; i < n_sets; i++)
2609 rtx dest = sets[i].dest;
2610 if (REG_P (dest) || MEM_P (dest))
2612 int j;
2613 for (j = i + 1; j < n_sets; j++)
2614 if (rtx_equal_p (dest, sets[j].dest))
2616 sets[i].dest = pc_rtx;
2617 sets[j].dest = pc_rtx;
2623 /* Now enter the equivalences in our tables. */
2624 for (i = 0; i < n_sets; i++)
2626 rtx dest = sets[i].dest;
2627 if (REG_P (dest)
2628 || (MEM_P (dest) && cselib_record_memory))
2629 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2633 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
2635 bool
2636 fp_setter_insn (rtx_insn *insn)
2638 rtx expr, pat = NULL_RTX;
2640 if (!RTX_FRAME_RELATED_P (insn))
2641 return false;
2643 expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2644 if (expr)
2645 pat = XEXP (expr, 0);
2646 if (!modified_in_p (hard_frame_pointer_rtx, pat ? pat : insn))
2647 return false;
2649 /* Don't return true for frame pointer restores in the epilogue. */
2650 if (find_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx))
2651 return false;
2652 return true;
2655 /* Record the effects of INSN. */
2657 void
2658 cselib_process_insn (rtx_insn *insn)
2660 int i;
2661 rtx x;
2663 cselib_current_insn = insn;
2665 /* Forget everything at a CODE_LABEL or a setjmp. */
2666 if ((LABEL_P (insn)
2667 || (CALL_P (insn)
2668 && find_reg_note (insn, REG_SETJMP, NULL)))
2669 && !cselib_preserve_constants)
2671 cselib_reset_table (next_uid);
2672 cselib_current_insn = NULL;
2673 return;
2676 if (! INSN_P (insn))
2678 cselib_current_insn = NULL;
2679 return;
2682 /* If this is a call instruction, forget anything stored in a
2683 call clobbered register, or, if this is not a const call, in
2684 memory. */
2685 if (CALL_P (insn))
2687 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2688 if (call_used_regs[i]
2689 || (REG_VALUES (i) && REG_VALUES (i)->elt
2690 && HARD_REGNO_CALL_PART_CLOBBERED (i,
2691 GET_MODE (REG_VALUES (i)->elt->val_rtx))))
2692 cselib_invalidate_regno (i, reg_raw_mode[i]);
2694 /* Since it is not clear how cselib is going to be used, be
2695 conservative here and treat looping pure or const functions
2696 as if they were regular functions. */
2697 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
2698 || !(RTL_CONST_OR_PURE_CALL_P (insn)))
2699 cselib_invalidate_mem (callmem);
2702 cselib_record_sets (insn);
2704 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2705 after we have processed the insn. */
2706 if (CALL_P (insn))
2708 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
2709 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
2710 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
2711 /* Flush evertything on setjmp. */
2712 if (cselib_preserve_constants
2713 && find_reg_note (insn, REG_SETJMP, NULL))
2715 cselib_preserve_only_values ();
2716 cselib_reset_table (next_uid);
2720 /* On setter of the hard frame pointer if frame_pointer_needed,
2721 invalidate stack_pointer_rtx, so that sp and {,h}fp based
2722 VALUEs are distinct. */
2723 if (reload_completed
2724 && frame_pointer_needed
2725 && fp_setter_insn (insn))
2726 cselib_invalidate_rtx (stack_pointer_rtx);
2728 cselib_current_insn = NULL;
2730 if (n_useless_values > MAX_USELESS_VALUES
2731 /* remove_useless_values is linear in the hash table size. Avoid
2732 quadratic behavior for very large hashtables with very few
2733 useless elements. */
2734 && ((unsigned int)n_useless_values
2735 > (cselib_hash_table->elements () - n_debug_values) / 4))
2736 remove_useless_values ();
2739 /* Initialize cselib for one pass. The caller must also call
2740 init_alias_analysis. */
2742 void
2743 cselib_init (int record_what)
2745 cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
2746 cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
2747 cselib_any_perm_equivs = false;
2749 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2750 see canon_true_dependence. This is only created once. */
2751 if (! callmem)
2752 callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2754 cselib_nregs = max_reg_num ();
2756 /* We preserve reg_values to allow expensive clearing of the whole thing.
2757 Reallocate it however if it happens to be too large. */
2758 if (!reg_values || reg_values_size < cselib_nregs
2759 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
2761 free (reg_values);
2762 /* Some space for newly emit instructions so we don't end up
2763 reallocating in between passes. */
2764 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
2765 reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
2767 used_regs = XNEWVEC (unsigned int, cselib_nregs);
2768 n_used_regs = 0;
2769 cselib_hash_table = new hash_table<cselib_hasher> (31);
2770 if (cselib_preserve_constants)
2771 cselib_preserved_hash_table = new hash_table<cselib_hasher> (31);
2772 next_uid = 1;
2775 /* Called when the current user is done with cselib. */
2777 void
2778 cselib_finish (void)
2780 bool preserved = cselib_preserve_constants;
2781 cselib_discard_hook = NULL;
2782 cselib_preserve_constants = false;
2783 cselib_any_perm_equivs = false;
2784 cfa_base_preserved_val = NULL;
2785 cfa_base_preserved_regno = INVALID_REGNUM;
2786 elt_list::pool.release ();
2787 elt_loc_list::pool.release ();
2788 cselib_val::pool.release ();
2789 value_pool.release ();
2790 cselib_clear_table ();
2791 delete cselib_hash_table;
2792 cselib_hash_table = NULL;
2793 if (preserved)
2794 delete cselib_preserved_hash_table;
2795 cselib_preserved_hash_table = NULL;
2796 free (used_regs);
2797 used_regs = 0;
2798 n_useless_values = 0;
2799 n_useless_debug_values = 0;
2800 n_debug_values = 0;
2801 next_uid = 0;
2804 /* Dump the cselib_val *X to FILE *OUT. */
2807 dump_cselib_val (cselib_val **x, FILE *out)
2809 cselib_val *v = *x;
2810 bool need_lf = true;
2812 print_inline_rtx (out, v->val_rtx, 0);
2814 if (v->locs)
2816 struct elt_loc_list *l = v->locs;
2817 if (need_lf)
2819 fputc ('\n', out);
2820 need_lf = false;
2822 fputs (" locs:", out);
2825 if (l->setting_insn)
2826 fprintf (out, "\n from insn %i ",
2827 INSN_UID (l->setting_insn));
2828 else
2829 fprintf (out, "\n ");
2830 print_inline_rtx (out, l->loc, 4);
2832 while ((l = l->next));
2833 fputc ('\n', out);
2835 else
2837 fputs (" no locs", out);
2838 need_lf = true;
2841 if (v->addr_list)
2843 struct elt_list *e = v->addr_list;
2844 if (need_lf)
2846 fputc ('\n', out);
2847 need_lf = false;
2849 fputs (" addr list:", out);
2852 fputs ("\n ", out);
2853 print_inline_rtx (out, e->elt->val_rtx, 2);
2855 while ((e = e->next));
2856 fputc ('\n', out);
2858 else
2860 fputs (" no addrs", out);
2861 need_lf = true;
2864 if (v->next_containing_mem == &dummy_val)
2865 fputs (" last mem\n", out);
2866 else if (v->next_containing_mem)
2868 fputs (" next mem ", out);
2869 print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
2870 fputc ('\n', out);
2872 else if (need_lf)
2873 fputc ('\n', out);
2875 return 1;
2878 /* Dump to OUT everything in the CSELIB table. */
2880 void
2881 dump_cselib_table (FILE *out)
2883 fprintf (out, "cselib hash table:\n");
2884 cselib_hash_table->traverse <FILE *, dump_cselib_val> (out);
2885 fprintf (out, "cselib preserved hash table:\n");
2886 cselib_preserved_hash_table->traverse <FILE *, dump_cselib_val> (out);
2887 if (first_containing_mem != &dummy_val)
2889 fputs ("first mem ", out);
2890 print_inline_rtx (out, first_containing_mem->val_rtx, 2);
2891 fputc ('\n', out);
2893 fprintf (out, "next uid %i\n", next_uid);
2896 #include "gt-cselib.h"