2015-06-24 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / cselib.c
blob6336e6bdc3e18286e76fc9aff4bac93f620f21e4
1 /* Common subexpression elimination library for GNU compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "rtl.h"
25 #include "alias.h"
26 #include "symtab.h"
27 #include "tree.h"/* FIXME: For hashing DEBUG_EXPR & friends. */
28 #include "tm_p.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "flags.h"
32 #include "insn-config.h"
33 #include "recog.h"
34 #include "function.h"
35 #include "emit-rtl.h"
36 #include "diagnostic-core.h"
37 #include "dumpfile.h"
38 #include "alloc-pool.h"
39 #include "cselib.h"
40 #include "predict.h"
41 #include "basic-block.h"
42 #include "valtrack.h"
43 #include "params.h"
44 #include "alloc-pool.h"
45 #include "target.h"
46 #include "bitmap.h"
48 /* A list of cselib_val structures. */
49 struct elt_list
51 struct elt_list *next;
52 cselib_val *elt;
54 /* Pool allocation new operator. */
55 inline void *operator new (size_t)
57 return pool.allocate ();
60 /* Delete operator utilizing pool allocation. */
61 inline void operator delete (void *ptr)
63 pool.remove ((elt_list *) ptr);
66 /* Memory allocation pool. */
67 static pool_allocator<elt_list> pool;
70 static bool cselib_record_memory;
71 static bool cselib_preserve_constants;
72 static bool cselib_any_perm_equivs;
73 static inline void promote_debug_loc (struct elt_loc_list *l);
74 static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
75 static void new_elt_loc_list (cselib_val *, rtx);
76 static void unchain_one_value (cselib_val *);
77 static void unchain_one_elt_list (struct elt_list **);
78 static void unchain_one_elt_loc_list (struct elt_loc_list **);
79 static void remove_useless_values (void);
80 static int rtx_equal_for_cselib_1 (rtx, rtx, machine_mode);
81 static unsigned int cselib_hash_rtx (rtx, int, machine_mode);
82 static cselib_val *new_cselib_val (unsigned int, machine_mode, rtx);
83 static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
84 static cselib_val *cselib_lookup_mem (rtx, int);
85 static void cselib_invalidate_regno (unsigned int, machine_mode);
86 static void cselib_invalidate_mem (rtx);
87 static void cselib_record_set (rtx, cselib_val *, cselib_val *);
88 static void cselib_record_sets (rtx_insn *);
90 struct expand_value_data
92 bitmap regs_active;
93 cselib_expand_callback callback;
94 void *callback_arg;
95 bool dummy;
98 static rtx cselib_expand_value_rtx_1 (rtx, struct expand_value_data *, int);
100 /* There are three ways in which cselib can look up an rtx:
101 - for a REG, the reg_values table (which is indexed by regno) is used
102 - for a MEM, we recursively look up its address and then follow the
103 addr_list of that value
104 - for everything else, we compute a hash value and go through the hash
105 table. Since different rtx's can still have the same hash value,
106 this involves walking the table entries for a given value and comparing
107 the locations of the entries with the rtx we are looking up. */
109 struct cselib_hasher : typed_noop_remove <cselib_val>
111 typedef cselib_val *value_type;
112 struct key {
113 /* The rtx value and its mode (needed separately for constant
114 integers). */
115 machine_mode mode;
116 rtx x;
117 /* The mode of the contaning MEM, if any, otherwise VOIDmode. */
118 machine_mode memmode;
120 typedef key *compare_type;
121 static inline hashval_t hash (const cselib_val *);
122 static inline bool equal (const cselib_val *, const key *);
125 /* The hash function for our hash table. The value is always computed with
126 cselib_hash_rtx when adding an element; this function just extracts the
127 hash value from a cselib_val structure. */
129 inline hashval_t
130 cselib_hasher::hash (const cselib_val *v)
132 return v->hash;
135 /* The equality test for our hash table. The first argument V is a table
136 element (i.e. a cselib_val), while the second arg X is an rtx. We know
137 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
138 CONST of an appropriate mode. */
140 inline bool
141 cselib_hasher::equal (const cselib_val *v, const key *x_arg)
143 struct elt_loc_list *l;
144 rtx x = x_arg->x;
145 machine_mode mode = x_arg->mode;
146 machine_mode memmode = x_arg->memmode;
148 if (mode != GET_MODE (v->val_rtx))
149 return false;
151 if (GET_CODE (x) == VALUE)
152 return x == v->val_rtx;
154 /* We don't guarantee that distinct rtx's have different hash values,
155 so we need to do a comparison. */
156 for (l = v->locs; l; l = l->next)
157 if (rtx_equal_for_cselib_1 (l->loc, x, memmode))
159 promote_debug_loc (l);
160 return true;
163 return false;
166 /* A table that enables us to look up elts by their value. */
167 static hash_table<cselib_hasher> *cselib_hash_table;
169 /* A table to hold preserved values. */
170 static hash_table<cselib_hasher> *cselib_preserved_hash_table;
172 /* This is a global so we don't have to pass this through every function.
173 It is used in new_elt_loc_list to set SETTING_INSN. */
174 static rtx_insn *cselib_current_insn;
176 /* The unique id that the next create value will take. */
177 static unsigned int next_uid;
179 /* The number of registers we had when the varrays were last resized. */
180 static unsigned int cselib_nregs;
182 /* Count values without known locations, or with only locations that
183 wouldn't have been known except for debug insns. Whenever this
184 grows too big, we remove these useless values from the table.
186 Counting values with only debug values is a bit tricky. We don't
187 want to increment n_useless_values when we create a value for a
188 debug insn, for this would get n_useless_values out of sync, but we
189 want increment it if all locs in the list that were ever referenced
190 in nondebug insns are removed from the list.
192 In the general case, once we do that, we'd have to stop accepting
193 nondebug expressions in the loc list, to avoid having two values
194 equivalent that, without debug insns, would have been made into
195 separate values. However, because debug insns never introduce
196 equivalences themselves (no assignments), the only means for
197 growing loc lists is through nondebug assignments. If the locs
198 also happen to be referenced in debug insns, it will work just fine.
200 A consequence of this is that there's at most one debug-only loc in
201 each loc list. If we keep it in the first entry, testing whether
202 we have a debug-only loc list takes O(1).
204 Furthermore, since any additional entry in a loc list containing a
205 debug loc would have to come from an assignment (nondebug) that
206 references both the initial debug loc and the newly-equivalent loc,
207 the initial debug loc would be promoted to a nondebug loc, and the
208 loc list would not contain debug locs any more.
210 So the only case we have to be careful with in order to keep
211 n_useless_values in sync between debug and nondebug compilations is
212 to avoid incrementing n_useless_values when removing the single loc
213 from a value that turns out to not appear outside debug values. We
214 increment n_useless_debug_values instead, and leave such values
215 alone until, for other reasons, we garbage-collect useless
216 values. */
217 static int n_useless_values;
218 static int n_useless_debug_values;
220 /* Count values whose locs have been taken exclusively from debug
221 insns for the entire life of the value. */
222 static int n_debug_values;
224 /* Number of useless values before we remove them from the hash table. */
225 #define MAX_USELESS_VALUES 32
227 /* This table maps from register number to values. It does not
228 contain pointers to cselib_val structures, but rather elt_lists.
229 The purpose is to be able to refer to the same register in
230 different modes. The first element of the list defines the mode in
231 which the register was set; if the mode is unknown or the value is
232 no longer valid in that mode, ELT will be NULL for the first
233 element. */
234 static struct elt_list **reg_values;
235 static unsigned int reg_values_size;
236 #define REG_VALUES(i) reg_values[i]
238 /* The largest number of hard regs used by any entry added to the
239 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
240 static unsigned int max_value_regs;
242 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
243 in cselib_clear_table() for fast emptying. */
244 static unsigned int *used_regs;
245 static unsigned int n_used_regs;
247 /* We pass this to cselib_invalidate_mem to invalidate all of
248 memory for a non-const call instruction. */
249 static GTY(()) rtx callmem;
251 /* Set by discard_useless_locs if it deleted the last location of any
252 value. */
253 static int values_became_useless;
255 /* Used as stop element of the containing_mem list so we can check
256 presence in the list by checking the next pointer. */
257 static cselib_val dummy_val;
259 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
260 that is constant through the whole function and should never be
261 eliminated. */
262 static cselib_val *cfa_base_preserved_val;
263 static unsigned int cfa_base_preserved_regno = INVALID_REGNUM;
265 /* Used to list all values that contain memory reference.
266 May or may not contain the useless values - the list is compacted
267 each time memory is invalidated. */
268 static cselib_val *first_containing_mem = &dummy_val;
270 pool_allocator<elt_list> elt_list::pool ("elt_list", 10);
271 pool_allocator<elt_loc_list> elt_loc_list::pool ("elt_loc_list", 10);
272 pool_allocator<cselib_val> cselib_val::pool ("cselib_val_list", 10);
274 static pool_allocator<rtx_def> value_pool ("value", 100, RTX_CODE_SIZE (VALUE),
275 true);
277 /* If nonnull, cselib will call this function before freeing useless
278 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
279 void (*cselib_discard_hook) (cselib_val *);
281 /* If nonnull, cselib will call this function before recording sets or
282 even clobbering outputs of INSN. All the recorded sets will be
283 represented in the array sets[n_sets]. new_val_min can be used to
284 tell whether values present in sets are introduced by this
285 instruction. */
286 void (*cselib_record_sets_hook) (rtx_insn *insn, struct cselib_set *sets,
287 int n_sets);
289 #define PRESERVED_VALUE_P(RTX) \
290 (RTL_FLAG_CHECK1 ("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
292 #define SP_BASED_VALUE_P(RTX) \
293 (RTL_FLAG_CHECK1 ("SP_BASED_VALUE_P", (RTX), VALUE)->jump)
297 /* Allocate a struct elt_list and fill in its two elements with the
298 arguments. */
300 static inline struct elt_list *
301 new_elt_list (struct elt_list *next, cselib_val *elt)
303 elt_list *el = new elt_list ();
304 el->next = next;
305 el->elt = elt;
306 return el;
309 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
310 list. */
312 static inline void
313 new_elt_loc_list (cselib_val *val, rtx loc)
315 struct elt_loc_list *el, *next = val->locs;
317 gcc_checking_assert (!next || !next->setting_insn
318 || !DEBUG_INSN_P (next->setting_insn)
319 || cselib_current_insn == next->setting_insn);
321 /* If we're creating the first loc in a debug insn context, we've
322 just created a debug value. Count it. */
323 if (!next && cselib_current_insn && DEBUG_INSN_P (cselib_current_insn))
324 n_debug_values++;
326 val = canonical_cselib_val (val);
327 next = val->locs;
329 if (GET_CODE (loc) == VALUE)
331 loc = canonical_cselib_val (CSELIB_VAL_PTR (loc))->val_rtx;
333 gcc_checking_assert (PRESERVED_VALUE_P (loc)
334 == PRESERVED_VALUE_P (val->val_rtx));
336 if (val->val_rtx == loc)
337 return;
338 else if (val->uid > CSELIB_VAL_PTR (loc)->uid)
340 /* Reverse the insertion. */
341 new_elt_loc_list (CSELIB_VAL_PTR (loc), val->val_rtx);
342 return;
345 gcc_checking_assert (val->uid < CSELIB_VAL_PTR (loc)->uid);
347 if (CSELIB_VAL_PTR (loc)->locs)
349 /* Bring all locs from LOC to VAL. */
350 for (el = CSELIB_VAL_PTR (loc)->locs; el->next; el = el->next)
352 /* Adjust values that have LOC as canonical so that VAL
353 becomes their canonical. */
354 if (el->loc && GET_CODE (el->loc) == VALUE)
356 gcc_checking_assert (CSELIB_VAL_PTR (el->loc)->locs->loc
357 == loc);
358 CSELIB_VAL_PTR (el->loc)->locs->loc = val->val_rtx;
361 el->next = val->locs;
362 next = val->locs = CSELIB_VAL_PTR (loc)->locs;
365 if (CSELIB_VAL_PTR (loc)->addr_list)
367 /* Bring in addr_list into canonical node. */
368 struct elt_list *last = CSELIB_VAL_PTR (loc)->addr_list;
369 while (last->next)
370 last = last->next;
371 last->next = val->addr_list;
372 val->addr_list = CSELIB_VAL_PTR (loc)->addr_list;
373 CSELIB_VAL_PTR (loc)->addr_list = NULL;
376 if (CSELIB_VAL_PTR (loc)->next_containing_mem != NULL
377 && val->next_containing_mem == NULL)
379 /* Add VAL to the containing_mem list after LOC. LOC will
380 be removed when we notice it doesn't contain any
381 MEMs. */
382 val->next_containing_mem = CSELIB_VAL_PTR (loc)->next_containing_mem;
383 CSELIB_VAL_PTR (loc)->next_containing_mem = val;
386 /* Chain LOC back to VAL. */
387 el = new elt_loc_list;
388 el->loc = val->val_rtx;
389 el->setting_insn = cselib_current_insn;
390 el->next = NULL;
391 CSELIB_VAL_PTR (loc)->locs = el;
394 el = new elt_loc_list;
395 el->loc = loc;
396 el->setting_insn = cselib_current_insn;
397 el->next = next;
398 val->locs = el;
401 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
402 originating from a debug insn, maintaining the debug values
403 count. */
405 static inline void
406 promote_debug_loc (struct elt_loc_list *l)
408 if (l && l->setting_insn && DEBUG_INSN_P (l->setting_insn)
409 && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
411 n_debug_values--;
412 l->setting_insn = cselib_current_insn;
413 if (cselib_preserve_constants && l->next)
415 gcc_assert (l->next->setting_insn
416 && DEBUG_INSN_P (l->next->setting_insn)
417 && !l->next->next);
418 l->next->setting_insn = cselib_current_insn;
420 else
421 gcc_assert (!l->next);
425 /* The elt_list at *PL is no longer needed. Unchain it and free its
426 storage. */
428 static inline void
429 unchain_one_elt_list (struct elt_list **pl)
431 struct elt_list *l = *pl;
433 *pl = l->next;
434 delete l;
437 /* Likewise for elt_loc_lists. */
439 static void
440 unchain_one_elt_loc_list (struct elt_loc_list **pl)
442 struct elt_loc_list *l = *pl;
444 *pl = l->next;
445 delete l;
448 /* Likewise for cselib_vals. This also frees the addr_list associated with
449 V. */
451 static void
452 unchain_one_value (cselib_val *v)
454 while (v->addr_list)
455 unchain_one_elt_list (&v->addr_list);
457 delete v;
460 /* Remove all entries from the hash table. Also used during
461 initialization. */
463 void
464 cselib_clear_table (void)
466 cselib_reset_table (1);
469 /* Return TRUE if V is a constant, a function invariant or a VALUE
470 equivalence; FALSE otherwise. */
472 static bool
473 invariant_or_equiv_p (cselib_val *v)
475 struct elt_loc_list *l;
477 if (v == cfa_base_preserved_val)
478 return true;
480 /* Keep VALUE equivalences around. */
481 for (l = v->locs; l; l = l->next)
482 if (GET_CODE (l->loc) == VALUE)
483 return true;
485 if (v->locs != NULL
486 && v->locs->next == NULL)
488 if (CONSTANT_P (v->locs->loc)
489 && (GET_CODE (v->locs->loc) != CONST
490 || !references_value_p (v->locs->loc, 0)))
491 return true;
492 /* Although a debug expr may be bound to different expressions,
493 we can preserve it as if it was constant, to get unification
494 and proper merging within var-tracking. */
495 if (GET_CODE (v->locs->loc) == DEBUG_EXPR
496 || GET_CODE (v->locs->loc) == DEBUG_IMPLICIT_PTR
497 || GET_CODE (v->locs->loc) == ENTRY_VALUE
498 || GET_CODE (v->locs->loc) == DEBUG_PARAMETER_REF)
499 return true;
501 /* (plus (value V) (const_int C)) is invariant iff V is invariant. */
502 if (GET_CODE (v->locs->loc) == PLUS
503 && CONST_INT_P (XEXP (v->locs->loc, 1))
504 && GET_CODE (XEXP (v->locs->loc, 0)) == VALUE
505 && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v->locs->loc, 0))))
506 return true;
509 return false;
512 /* Remove from hash table all VALUEs except constants, function
513 invariants and VALUE equivalences. */
516 preserve_constants_and_equivs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
518 cselib_val *v = *x;
520 if (invariant_or_equiv_p (v))
522 cselib_hasher::key lookup = {
523 GET_MODE (v->val_rtx), v->val_rtx, VOIDmode
525 cselib_val **slot
526 = cselib_preserved_hash_table->find_slot_with_hash (&lookup,
527 v->hash, INSERT);
528 gcc_assert (!*slot);
529 *slot = v;
532 cselib_hash_table->clear_slot (x);
534 return 1;
537 /* Remove all entries from the hash table, arranging for the next
538 value to be numbered NUM. */
540 void
541 cselib_reset_table (unsigned int num)
543 unsigned int i;
545 max_value_regs = 0;
547 if (cfa_base_preserved_val)
549 unsigned int regno = cfa_base_preserved_regno;
550 unsigned int new_used_regs = 0;
551 for (i = 0; i < n_used_regs; i++)
552 if (used_regs[i] == regno)
554 new_used_regs = 1;
555 continue;
557 else
558 REG_VALUES (used_regs[i]) = 0;
559 gcc_assert (new_used_regs == 1);
560 n_used_regs = new_used_regs;
561 used_regs[0] = regno;
562 max_value_regs
563 = hard_regno_nregs[regno][GET_MODE (cfa_base_preserved_val->locs->loc)];
565 else
567 for (i = 0; i < n_used_regs; i++)
568 REG_VALUES (used_regs[i]) = 0;
569 n_used_regs = 0;
572 if (cselib_preserve_constants)
573 cselib_hash_table->traverse <void *, preserve_constants_and_equivs>
574 (NULL);
575 else
577 cselib_hash_table->empty ();
578 gcc_checking_assert (!cselib_any_perm_equivs);
581 n_useless_values = 0;
582 n_useless_debug_values = 0;
583 n_debug_values = 0;
585 next_uid = num;
587 first_containing_mem = &dummy_val;
590 /* Return the number of the next value that will be generated. */
592 unsigned int
593 cselib_get_next_uid (void)
595 return next_uid;
598 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
599 INSERTing if requested. When X is part of the address of a MEM,
600 MEMMODE should specify the mode of the MEM. */
602 static cselib_val **
603 cselib_find_slot (machine_mode mode, rtx x, hashval_t hash,
604 enum insert_option insert, machine_mode memmode)
606 cselib_val **slot = NULL;
607 cselib_hasher::key lookup = { mode, x, memmode };
608 if (cselib_preserve_constants)
609 slot = cselib_preserved_hash_table->find_slot_with_hash (&lookup, hash,
610 NO_INSERT);
611 if (!slot)
612 slot = cselib_hash_table->find_slot_with_hash (&lookup, hash, insert);
613 return slot;
616 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
617 only return true for values which point to a cselib_val whose value
618 element has been set to zero, which implies the cselib_val will be
619 removed. */
622 references_value_p (const_rtx x, int only_useless)
624 const enum rtx_code code = GET_CODE (x);
625 const char *fmt = GET_RTX_FORMAT (code);
626 int i, j;
628 if (GET_CODE (x) == VALUE
629 && (! only_useless ||
630 (CSELIB_VAL_PTR (x)->locs == 0 && !PRESERVED_VALUE_P (x))))
631 return 1;
633 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
635 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
636 return 1;
637 else if (fmt[i] == 'E')
638 for (j = 0; j < XVECLEN (x, i); j++)
639 if (references_value_p (XVECEXP (x, i, j), only_useless))
640 return 1;
643 return 0;
646 /* For all locations found in X, delete locations that reference useless
647 values (i.e. values without any location). Called through
648 htab_traverse. */
651 discard_useless_locs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
653 cselib_val *v = *x;
654 struct elt_loc_list **p = &v->locs;
655 bool had_locs = v->locs != NULL;
656 rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
658 while (*p)
660 if (references_value_p ((*p)->loc, 1))
661 unchain_one_elt_loc_list (p);
662 else
663 p = &(*p)->next;
666 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
668 if (setting_insn && DEBUG_INSN_P (setting_insn))
669 n_useless_debug_values++;
670 else
671 n_useless_values++;
672 values_became_useless = 1;
674 return 1;
677 /* If X is a value with no locations, remove it from the hashtable. */
680 discard_useless_values (cselib_val **x, void *info ATTRIBUTE_UNUSED)
682 cselib_val *v = *x;
684 if (v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
686 if (cselib_discard_hook)
687 cselib_discard_hook (v);
689 CSELIB_VAL_PTR (v->val_rtx) = NULL;
690 cselib_hash_table->clear_slot (x);
691 unchain_one_value (v);
692 n_useless_values--;
695 return 1;
698 /* Clean out useless values (i.e. those which no longer have locations
699 associated with them) from the hash table. */
701 static void
702 remove_useless_values (void)
704 cselib_val **p, *v;
706 /* First pass: eliminate locations that reference the value. That in
707 turn can make more values useless. */
710 values_became_useless = 0;
711 cselib_hash_table->traverse <void *, discard_useless_locs> (NULL);
713 while (values_became_useless);
715 /* Second pass: actually remove the values. */
717 p = &first_containing_mem;
718 for (v = *p; v != &dummy_val; v = v->next_containing_mem)
719 if (v->locs && v == canonical_cselib_val (v))
721 *p = v;
722 p = &(*p)->next_containing_mem;
724 *p = &dummy_val;
726 n_useless_values += n_useless_debug_values;
727 n_debug_values -= n_useless_debug_values;
728 n_useless_debug_values = 0;
730 cselib_hash_table->traverse <void *, discard_useless_values> (NULL);
732 gcc_assert (!n_useless_values);
735 /* Arrange for a value to not be removed from the hash table even if
736 it becomes useless. */
738 void
739 cselib_preserve_value (cselib_val *v)
741 PRESERVED_VALUE_P (v->val_rtx) = 1;
744 /* Test whether a value is preserved. */
746 bool
747 cselib_preserved_value_p (cselib_val *v)
749 return PRESERVED_VALUE_P (v->val_rtx);
752 /* Arrange for a REG value to be assumed constant through the whole function,
753 never invalidated and preserved across cselib_reset_table calls. */
755 void
756 cselib_preserve_cfa_base_value (cselib_val *v, unsigned int regno)
758 if (cselib_preserve_constants
759 && v->locs
760 && REG_P (v->locs->loc))
762 cfa_base_preserved_val = v;
763 cfa_base_preserved_regno = regno;
767 /* Clean all non-constant expressions in the hash table, but retain
768 their values. */
770 void
771 cselib_preserve_only_values (void)
773 int i;
775 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
776 cselib_invalidate_regno (i, reg_raw_mode[i]);
778 cselib_invalidate_mem (callmem);
780 remove_useless_values ();
782 gcc_assert (first_containing_mem == &dummy_val);
785 /* Arrange for a value to be marked as based on stack pointer
786 for find_base_term purposes. */
788 void
789 cselib_set_value_sp_based (cselib_val *v)
791 SP_BASED_VALUE_P (v->val_rtx) = 1;
794 /* Test whether a value is based on stack pointer for
795 find_base_term purposes. */
797 bool
798 cselib_sp_based_value_p (cselib_val *v)
800 return SP_BASED_VALUE_P (v->val_rtx);
803 /* Return the mode in which a register was last set. If X is not a
804 register, return its mode. If the mode in which the register was
805 set is not known, or the value was already clobbered, return
806 VOIDmode. */
808 machine_mode
809 cselib_reg_set_mode (const_rtx x)
811 if (!REG_P (x))
812 return GET_MODE (x);
814 if (REG_VALUES (REGNO (x)) == NULL
815 || REG_VALUES (REGNO (x))->elt == NULL)
816 return VOIDmode;
818 return GET_MODE (REG_VALUES (REGNO (x))->elt->val_rtx);
821 /* Return nonzero if we can prove that X and Y contain the same value, taking
822 our gathered information into account. */
825 rtx_equal_for_cselib_p (rtx x, rtx y)
827 return rtx_equal_for_cselib_1 (x, y, VOIDmode);
830 /* If x is a PLUS or an autoinc operation, expand the operation,
831 storing the offset, if any, in *OFF. */
833 static rtx
834 autoinc_split (rtx x, rtx *off, machine_mode memmode)
836 switch (GET_CODE (x))
838 case PLUS:
839 *off = XEXP (x, 1);
840 return XEXP (x, 0);
842 case PRE_DEC:
843 if (memmode == VOIDmode)
844 return x;
846 *off = GEN_INT (-GET_MODE_SIZE (memmode));
847 return XEXP (x, 0);
848 break;
850 case PRE_INC:
851 if (memmode == VOIDmode)
852 return x;
854 *off = GEN_INT (GET_MODE_SIZE (memmode));
855 return XEXP (x, 0);
857 case PRE_MODIFY:
858 return XEXP (x, 1);
860 case POST_DEC:
861 case POST_INC:
862 case POST_MODIFY:
863 return XEXP (x, 0);
865 default:
866 return x;
870 /* Return nonzero if we can prove that X and Y contain the same value,
871 taking our gathered information into account. MEMMODE holds the
872 mode of the enclosing MEM, if any, as required to deal with autoinc
873 addressing modes. If X and Y are not (known to be) part of
874 addresses, MEMMODE should be VOIDmode. */
876 static int
877 rtx_equal_for_cselib_1 (rtx x, rtx y, machine_mode memmode)
879 enum rtx_code code;
880 const char *fmt;
881 int i;
883 if (REG_P (x) || MEM_P (x))
885 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0, memmode);
887 if (e)
888 x = e->val_rtx;
891 if (REG_P (y) || MEM_P (y))
893 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0, memmode);
895 if (e)
896 y = e->val_rtx;
899 if (x == y)
900 return 1;
902 if (GET_CODE (x) == VALUE)
904 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (x));
905 struct elt_loc_list *l;
907 if (GET_CODE (y) == VALUE)
908 return e == canonical_cselib_val (CSELIB_VAL_PTR (y));
910 for (l = e->locs; l; l = l->next)
912 rtx t = l->loc;
914 /* Avoid infinite recursion. We know we have the canonical
915 value, so we can just skip any values in the equivalence
916 list. */
917 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
918 continue;
919 else if (rtx_equal_for_cselib_1 (t, y, memmode))
920 return 1;
923 return 0;
925 else if (GET_CODE (y) == VALUE)
927 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (y));
928 struct elt_loc_list *l;
930 for (l = e->locs; l; l = l->next)
932 rtx t = l->loc;
934 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
935 continue;
936 else if (rtx_equal_for_cselib_1 (x, t, memmode))
937 return 1;
940 return 0;
943 if (GET_MODE (x) != GET_MODE (y))
944 return 0;
946 if (GET_CODE (x) != GET_CODE (y))
948 rtx xorig = x, yorig = y;
949 rtx xoff = NULL, yoff = NULL;
951 x = autoinc_split (x, &xoff, memmode);
952 y = autoinc_split (y, &yoff, memmode);
954 if (!xoff != !yoff)
955 return 0;
957 if (xoff && !rtx_equal_for_cselib_1 (xoff, yoff, memmode))
958 return 0;
960 /* Don't recurse if nothing changed. */
961 if (x != xorig || y != yorig)
962 return rtx_equal_for_cselib_1 (x, y, memmode);
964 return 0;
967 /* These won't be handled correctly by the code below. */
968 switch (GET_CODE (x))
970 CASE_CONST_UNIQUE:
971 case DEBUG_EXPR:
972 return 0;
974 case DEBUG_IMPLICIT_PTR:
975 return DEBUG_IMPLICIT_PTR_DECL (x)
976 == DEBUG_IMPLICIT_PTR_DECL (y);
978 case DEBUG_PARAMETER_REF:
979 return DEBUG_PARAMETER_REF_DECL (x)
980 == DEBUG_PARAMETER_REF_DECL (y);
982 case ENTRY_VALUE:
983 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
984 use rtx_equal_for_cselib_1 to compare the operands. */
985 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
987 case LABEL_REF:
988 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
990 case REG:
991 return REGNO (x) == REGNO (y);
993 case MEM:
994 /* We have to compare any autoinc operations in the addresses
995 using this MEM's mode. */
996 return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x));
998 default:
999 break;
1002 code = GET_CODE (x);
1003 fmt = GET_RTX_FORMAT (code);
1005 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1007 int j;
1009 switch (fmt[i])
1011 case 'w':
1012 if (XWINT (x, i) != XWINT (y, i))
1013 return 0;
1014 break;
1016 case 'n':
1017 case 'i':
1018 if (XINT (x, i) != XINT (y, i))
1019 return 0;
1020 break;
1022 case 'V':
1023 case 'E':
1024 /* Two vectors must have the same length. */
1025 if (XVECLEN (x, i) != XVECLEN (y, i))
1026 return 0;
1028 /* And the corresponding elements must match. */
1029 for (j = 0; j < XVECLEN (x, i); j++)
1030 if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
1031 XVECEXP (y, i, j), memmode))
1032 return 0;
1033 break;
1035 case 'e':
1036 if (i == 1
1037 && targetm.commutative_p (x, UNKNOWN)
1038 && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode)
1039 && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode))
1040 return 1;
1041 if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode))
1042 return 0;
1043 break;
1045 case 'S':
1046 case 's':
1047 if (strcmp (XSTR (x, i), XSTR (y, i)))
1048 return 0;
1049 break;
1051 case 'u':
1052 /* These are just backpointers, so they don't matter. */
1053 break;
1055 case '0':
1056 case 't':
1057 break;
1059 /* It is believed that rtx's at this level will never
1060 contain anything but integers and other rtx's,
1061 except for within LABEL_REFs and SYMBOL_REFs. */
1062 default:
1063 gcc_unreachable ();
1066 return 1;
1069 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1070 For registers and memory locations, we look up their cselib_val structure
1071 and return its VALUE element.
1072 Possible reasons for return 0 are: the object is volatile, or we couldn't
1073 find a register or memory location in the table and CREATE is zero. If
1074 CREATE is nonzero, table elts are created for regs and mem.
1075 N.B. this hash function returns the same hash value for RTXes that
1076 differ only in the order of operands, thus it is suitable for comparisons
1077 that take commutativity into account.
1078 If we wanted to also support associative rules, we'd have to use a different
1079 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1080 MEMMODE indicates the mode of an enclosing MEM, and it's only
1081 used to compute autoinc values.
1082 We used to have a MODE argument for hashing for CONST_INTs, but that
1083 didn't make sense, since it caused spurious hash differences between
1084 (set (reg:SI 1) (const_int))
1085 (plus:SI (reg:SI 2) (reg:SI 1))
1087 (plus:SI (reg:SI 2) (const_int))
1088 If the mode is important in any context, it must be checked specifically
1089 in a comparison anyway, since relying on hash differences is unsafe. */
1091 static unsigned int
1092 cselib_hash_rtx (rtx x, int create, machine_mode memmode)
1094 cselib_val *e;
1095 int i, j;
1096 enum rtx_code code;
1097 const char *fmt;
1098 unsigned int hash = 0;
1100 code = GET_CODE (x);
1101 hash += (unsigned) code + (unsigned) GET_MODE (x);
1103 switch (code)
1105 case VALUE:
1106 e = CSELIB_VAL_PTR (x);
1107 return e->hash;
1109 case MEM:
1110 case REG:
1111 e = cselib_lookup (x, GET_MODE (x), create, memmode);
1112 if (! e)
1113 return 0;
1115 return e->hash;
1117 case DEBUG_EXPR:
1118 hash += ((unsigned) DEBUG_EXPR << 7)
1119 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
1120 return hash ? hash : (unsigned int) DEBUG_EXPR;
1122 case DEBUG_IMPLICIT_PTR:
1123 hash += ((unsigned) DEBUG_IMPLICIT_PTR << 7)
1124 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x));
1125 return hash ? hash : (unsigned int) DEBUG_IMPLICIT_PTR;
1127 case DEBUG_PARAMETER_REF:
1128 hash += ((unsigned) DEBUG_PARAMETER_REF << 7)
1129 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x));
1130 return hash ? hash : (unsigned int) DEBUG_PARAMETER_REF;
1132 case ENTRY_VALUE:
1133 /* ENTRY_VALUEs are function invariant, thus try to avoid
1134 recursing on argument if ENTRY_VALUE is one of the
1135 forms emitted by expand_debug_expr, otherwise
1136 ENTRY_VALUE hash would depend on the current value
1137 in some register or memory. */
1138 if (REG_P (ENTRY_VALUE_EXP (x)))
1139 hash += (unsigned int) REG
1140 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x))
1141 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x));
1142 else if (MEM_P (ENTRY_VALUE_EXP (x))
1143 && REG_P (XEXP (ENTRY_VALUE_EXP (x), 0)))
1144 hash += (unsigned int) MEM
1145 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x), 0))
1146 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x), 0));
1147 else
1148 hash += cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode);
1149 return hash ? hash : (unsigned int) ENTRY_VALUE;
1151 case CONST_INT:
1152 hash += ((unsigned) CONST_INT << 7) + UINTVAL (x);
1153 return hash ? hash : (unsigned int) CONST_INT;
1155 case CONST_WIDE_INT:
1156 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
1157 hash += CONST_WIDE_INT_ELT (x, i);
1158 return hash;
1160 case CONST_DOUBLE:
1161 /* This is like the general case, except that it only counts
1162 the integers representing the constant. */
1163 hash += (unsigned) code + (unsigned) GET_MODE (x);
1164 if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (x) == VOIDmode)
1165 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1166 + (unsigned) CONST_DOUBLE_HIGH (x));
1167 else
1168 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
1169 return hash ? hash : (unsigned int) CONST_DOUBLE;
1171 case CONST_FIXED:
1172 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1173 hash += fixed_hash (CONST_FIXED_VALUE (x));
1174 return hash ? hash : (unsigned int) CONST_FIXED;
1176 case CONST_VECTOR:
1178 int units;
1179 rtx elt;
1181 units = CONST_VECTOR_NUNITS (x);
1183 for (i = 0; i < units; ++i)
1185 elt = CONST_VECTOR_ELT (x, i);
1186 hash += cselib_hash_rtx (elt, 0, memmode);
1189 return hash;
1192 /* Assume there is only one rtx object for any given label. */
1193 case LABEL_REF:
1194 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1195 differences and differences between each stage's debugging dumps. */
1196 hash += (((unsigned int) LABEL_REF << 7)
1197 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x)));
1198 return hash ? hash : (unsigned int) LABEL_REF;
1200 case SYMBOL_REF:
1202 /* Don't hash on the symbol's address to avoid bootstrap differences.
1203 Different hash values may cause expressions to be recorded in
1204 different orders and thus different registers to be used in the
1205 final assembler. This also avoids differences in the dump files
1206 between various stages. */
1207 unsigned int h = 0;
1208 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1210 while (*p)
1211 h += (h << 7) + *p++; /* ??? revisit */
1213 hash += ((unsigned int) SYMBOL_REF << 7) + h;
1214 return hash ? hash : (unsigned int) SYMBOL_REF;
1217 case PRE_DEC:
1218 case PRE_INC:
1219 /* We can't compute these without knowing the MEM mode. */
1220 gcc_assert (memmode != VOIDmode);
1221 i = GET_MODE_SIZE (memmode);
1222 if (code == PRE_DEC)
1223 i = -i;
1224 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1225 like (mem:MEMMODE (plus (reg) (const_int I))). */
1226 hash += (unsigned) PLUS - (unsigned)code
1227 + cselib_hash_rtx (XEXP (x, 0), create, memmode)
1228 + cselib_hash_rtx (GEN_INT (i), create, memmode);
1229 return hash ? hash : 1 + (unsigned) PLUS;
1231 case PRE_MODIFY:
1232 gcc_assert (memmode != VOIDmode);
1233 return cselib_hash_rtx (XEXP (x, 1), create, memmode);
1235 case POST_DEC:
1236 case POST_INC:
1237 case POST_MODIFY:
1238 gcc_assert (memmode != VOIDmode);
1239 return cselib_hash_rtx (XEXP (x, 0), create, memmode);
1241 case PC:
1242 case CC0:
1243 case CALL:
1244 case UNSPEC_VOLATILE:
1245 return 0;
1247 case ASM_OPERANDS:
1248 if (MEM_VOLATILE_P (x))
1249 return 0;
1251 break;
1253 default:
1254 break;
1257 i = GET_RTX_LENGTH (code) - 1;
1258 fmt = GET_RTX_FORMAT (code);
1259 for (; i >= 0; i--)
1261 switch (fmt[i])
1263 case 'e':
1265 rtx tem = XEXP (x, i);
1266 unsigned int tem_hash = cselib_hash_rtx (tem, create, memmode);
1268 if (tem_hash == 0)
1269 return 0;
1271 hash += tem_hash;
1273 break;
1274 case 'E':
1275 for (j = 0; j < XVECLEN (x, i); j++)
1277 unsigned int tem_hash
1278 = cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
1280 if (tem_hash == 0)
1281 return 0;
1283 hash += tem_hash;
1285 break;
1287 case 's':
1289 const unsigned char *p = (const unsigned char *) XSTR (x, i);
1291 if (p)
1292 while (*p)
1293 hash += *p++;
1294 break;
1297 case 'i':
1298 hash += XINT (x, i);
1299 break;
1301 case '0':
1302 case 't':
1303 /* unused */
1304 break;
1306 default:
1307 gcc_unreachable ();
1311 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
1314 /* Create a new value structure for VALUE and initialize it. The mode of the
1315 value is MODE. */
1317 static inline cselib_val *
1318 new_cselib_val (unsigned int hash, machine_mode mode, rtx x)
1320 cselib_val *e = new cselib_val;
1322 gcc_assert (hash);
1323 gcc_assert (next_uid);
1325 e->hash = hash;
1326 e->uid = next_uid++;
1327 /* We use an alloc pool to allocate this RTL construct because it
1328 accounts for about 8% of the overall memory usage. We know
1329 precisely when we can have VALUE RTXen (when cselib is active)
1330 so we don't need to put them in garbage collected memory.
1331 ??? Why should a VALUE be an RTX in the first place? */
1332 e->val_rtx = value_pool.allocate ();
1333 memset (e->val_rtx, 0, RTX_HDR_SIZE);
1334 PUT_CODE (e->val_rtx, VALUE);
1335 PUT_MODE (e->val_rtx, mode);
1336 CSELIB_VAL_PTR (e->val_rtx) = e;
1337 e->addr_list = 0;
1338 e->locs = 0;
1339 e->next_containing_mem = 0;
1341 if (dump_file && (dump_flags & TDF_CSELIB))
1343 fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
1344 if (flag_dump_noaddr || flag_dump_unnumbered)
1345 fputs ("# ", dump_file);
1346 else
1347 fprintf (dump_file, "%p ", (void*)e);
1348 print_rtl_single (dump_file, x);
1349 fputc ('\n', dump_file);
1352 return e;
1355 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1356 contains the data at this address. X is a MEM that represents the
1357 value. Update the two value structures to represent this situation. */
1359 static void
1360 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
1362 struct elt_loc_list *l;
1364 addr_elt = canonical_cselib_val (addr_elt);
1365 mem_elt = canonical_cselib_val (mem_elt);
1367 /* Avoid duplicates. */
1368 for (l = mem_elt->locs; l; l = l->next)
1369 if (MEM_P (l->loc)
1370 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
1372 promote_debug_loc (l);
1373 return;
1376 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
1377 new_elt_loc_list (mem_elt,
1378 replace_equiv_address_nv (x, addr_elt->val_rtx));
1379 if (mem_elt->next_containing_mem == NULL)
1381 mem_elt->next_containing_mem = first_containing_mem;
1382 first_containing_mem = mem_elt;
1386 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1387 If CREATE, make a new one if we haven't seen it before. */
1389 static cselib_val *
1390 cselib_lookup_mem (rtx x, int create)
1392 machine_mode mode = GET_MODE (x);
1393 machine_mode addr_mode;
1394 cselib_val **slot;
1395 cselib_val *addr;
1396 cselib_val *mem_elt;
1397 struct elt_list *l;
1399 if (MEM_VOLATILE_P (x) || mode == BLKmode
1400 || !cselib_record_memory
1401 || (FLOAT_MODE_P (mode) && flag_float_store))
1402 return 0;
1404 addr_mode = GET_MODE (XEXP (x, 0));
1405 if (addr_mode == VOIDmode)
1406 addr_mode = Pmode;
1408 /* Look up the value for the address. */
1409 addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
1410 if (! addr)
1411 return 0;
1413 addr = canonical_cselib_val (addr);
1414 /* Find a value that describes a value of our mode at that address. */
1415 for (l = addr->addr_list; l; l = l->next)
1416 if (GET_MODE (l->elt->val_rtx) == mode)
1418 promote_debug_loc (l->elt->locs);
1419 return l->elt;
1422 if (! create)
1423 return 0;
1425 mem_elt = new_cselib_val (next_uid, mode, x);
1426 add_mem_for_addr (addr, mem_elt, x);
1427 slot = cselib_find_slot (mode, x, mem_elt->hash, INSERT, VOIDmode);
1428 *slot = mem_elt;
1429 return mem_elt;
1432 /* Search through the possible substitutions in P. We prefer a non reg
1433 substitution because this allows us to expand the tree further. If
1434 we find, just a reg, take the lowest regno. There may be several
1435 non-reg results, we just take the first one because they will all
1436 expand to the same place. */
1438 static rtx
1439 expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1440 int max_depth)
1442 rtx reg_result = NULL;
1443 unsigned int regno = UINT_MAX;
1444 struct elt_loc_list *p_in = p;
1446 for (; p; p = p->next)
1448 /* Return these right away to avoid returning stack pointer based
1449 expressions for frame pointer and vice versa, which is something
1450 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1451 for more details. */
1452 if (REG_P (p->loc)
1453 && (REGNO (p->loc) == STACK_POINTER_REGNUM
1454 || REGNO (p->loc) == FRAME_POINTER_REGNUM
1455 || REGNO (p->loc) == HARD_FRAME_POINTER_REGNUM
1456 || REGNO (p->loc) == cfa_base_preserved_regno))
1457 return p->loc;
1458 /* Avoid infinite recursion trying to expand a reg into a
1459 the same reg. */
1460 if ((REG_P (p->loc))
1461 && (REGNO (p->loc) < regno)
1462 && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
1464 reg_result = p->loc;
1465 regno = REGNO (p->loc);
1467 /* Avoid infinite recursion and do not try to expand the
1468 value. */
1469 else if (GET_CODE (p->loc) == VALUE
1470 && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1471 continue;
1472 else if (!REG_P (p->loc))
1474 rtx result, note;
1475 if (dump_file && (dump_flags & TDF_CSELIB))
1477 print_inline_rtx (dump_file, p->loc, 0);
1478 fprintf (dump_file, "\n");
1480 if (GET_CODE (p->loc) == LO_SUM
1481 && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1482 && p->setting_insn
1483 && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1484 && XEXP (note, 0) == XEXP (p->loc, 1))
1485 return XEXP (p->loc, 1);
1486 result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
1487 if (result)
1488 return result;
1493 if (regno != UINT_MAX)
1495 rtx result;
1496 if (dump_file && (dump_flags & TDF_CSELIB))
1497 fprintf (dump_file, "r%d\n", regno);
1499 result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
1500 if (result)
1501 return result;
1504 if (dump_file && (dump_flags & TDF_CSELIB))
1506 if (reg_result)
1508 print_inline_rtx (dump_file, reg_result, 0);
1509 fprintf (dump_file, "\n");
1511 else
1512 fprintf (dump_file, "NULL\n");
1514 return reg_result;
1518 /* Forward substitute and expand an expression out to its roots.
1519 This is the opposite of common subexpression. Because local value
1520 numbering is such a weak optimization, the expanded expression is
1521 pretty much unique (not from a pointer equals point of view but
1522 from a tree shape point of view.
1524 This function returns NULL if the expansion fails. The expansion
1525 will fail if there is no value number for one of the operands or if
1526 one of the operands has been overwritten between the current insn
1527 and the beginning of the basic block. For instance x has no
1528 expansion in:
1530 r1 <- r1 + 3
1531 x <- r1 + 8
1533 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1534 It is clear on return. */
1537 cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
1539 struct expand_value_data evd;
1541 evd.regs_active = regs_active;
1542 evd.callback = NULL;
1543 evd.callback_arg = NULL;
1544 evd.dummy = false;
1546 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1549 /* Same as cselib_expand_value_rtx, but using a callback to try to
1550 resolve some expressions. The CB function should return ORIG if it
1551 can't or does not want to deal with a certain RTX. Any other
1552 return value, including NULL, will be used as the expansion for
1553 VALUE, without any further changes. */
1556 cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1557 cselib_expand_callback cb, void *data)
1559 struct expand_value_data evd;
1561 evd.regs_active = regs_active;
1562 evd.callback = cb;
1563 evd.callback_arg = data;
1564 evd.dummy = false;
1566 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1569 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1570 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1571 would return NULL or non-NULL, without allocating new rtx. */
1573 bool
1574 cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1575 cselib_expand_callback cb, void *data)
1577 struct expand_value_data evd;
1579 evd.regs_active = regs_active;
1580 evd.callback = cb;
1581 evd.callback_arg = data;
1582 evd.dummy = true;
1584 return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1587 /* Internal implementation of cselib_expand_value_rtx and
1588 cselib_expand_value_rtx_cb. */
1590 static rtx
1591 cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1592 int max_depth)
1594 rtx copy, scopy;
1595 int i, j;
1596 RTX_CODE code;
1597 const char *format_ptr;
1598 machine_mode mode;
1600 code = GET_CODE (orig);
1602 /* For the context of dse, if we end up expand into a huge tree, we
1603 will not have a useful address, so we might as well just give up
1604 quickly. */
1605 if (max_depth <= 0)
1606 return NULL;
1608 switch (code)
1610 case REG:
1612 struct elt_list *l = REG_VALUES (REGNO (orig));
1614 if (l && l->elt == NULL)
1615 l = l->next;
1616 for (; l; l = l->next)
1617 if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1619 rtx result;
1620 unsigned regno = REGNO (orig);
1622 /* The only thing that we are not willing to do (this
1623 is requirement of dse and if others potential uses
1624 need this function we should add a parm to control
1625 it) is that we will not substitute the
1626 STACK_POINTER_REGNUM, FRAME_POINTER or the
1627 HARD_FRAME_POINTER.
1629 These expansions confuses the code that notices that
1630 stores into the frame go dead at the end of the
1631 function and that the frame is not effected by calls
1632 to subroutines. If you allow the
1633 STACK_POINTER_REGNUM substitution, then dse will
1634 think that parameter pushing also goes dead which is
1635 wrong. If you allow the FRAME_POINTER or the
1636 HARD_FRAME_POINTER then you lose the opportunity to
1637 make the frame assumptions. */
1638 if (regno == STACK_POINTER_REGNUM
1639 || regno == FRAME_POINTER_REGNUM
1640 || regno == HARD_FRAME_POINTER_REGNUM
1641 || regno == cfa_base_preserved_regno)
1642 return orig;
1644 bitmap_set_bit (evd->regs_active, regno);
1646 if (dump_file && (dump_flags & TDF_CSELIB))
1647 fprintf (dump_file, "expanding: r%d into: ", regno);
1649 result = expand_loc (l->elt->locs, evd, max_depth);
1650 bitmap_clear_bit (evd->regs_active, regno);
1652 if (result)
1653 return result;
1654 else
1655 return orig;
1659 CASE_CONST_ANY:
1660 case SYMBOL_REF:
1661 case CODE_LABEL:
1662 case PC:
1663 case CC0:
1664 case SCRATCH:
1665 /* SCRATCH must be shared because they represent distinct values. */
1666 return orig;
1667 case CLOBBER:
1668 if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1669 return orig;
1670 break;
1672 case CONST:
1673 if (shared_const_p (orig))
1674 return orig;
1675 break;
1677 case SUBREG:
1679 rtx subreg;
1681 if (evd->callback)
1683 subreg = evd->callback (orig, evd->regs_active, max_depth,
1684 evd->callback_arg);
1685 if (subreg != orig)
1686 return subreg;
1689 subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1690 max_depth - 1);
1691 if (!subreg)
1692 return NULL;
1693 scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1694 GET_MODE (SUBREG_REG (orig)),
1695 SUBREG_BYTE (orig));
1696 if (scopy == NULL
1697 || (GET_CODE (scopy) == SUBREG
1698 && !REG_P (SUBREG_REG (scopy))
1699 && !MEM_P (SUBREG_REG (scopy))))
1700 return NULL;
1702 return scopy;
1705 case VALUE:
1707 rtx result;
1709 if (dump_file && (dump_flags & TDF_CSELIB))
1711 fputs ("\nexpanding ", dump_file);
1712 print_rtl_single (dump_file, orig);
1713 fputs (" into...", dump_file);
1716 if (evd->callback)
1718 result = evd->callback (orig, evd->regs_active, max_depth,
1719 evd->callback_arg);
1721 if (result != orig)
1722 return result;
1725 result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
1726 return result;
1729 case DEBUG_EXPR:
1730 if (evd->callback)
1731 return evd->callback (orig, evd->regs_active, max_depth,
1732 evd->callback_arg);
1733 return orig;
1735 default:
1736 break;
1739 /* Copy the various flags, fields, and other information. We assume
1740 that all fields need copying, and then clear the fields that should
1741 not be copied. That is the sensible default behavior, and forces
1742 us to explicitly document why we are *not* copying a flag. */
1743 if (evd->dummy)
1744 copy = NULL;
1745 else
1746 copy = shallow_copy_rtx (orig);
1748 format_ptr = GET_RTX_FORMAT (code);
1750 for (i = 0; i < GET_RTX_LENGTH (code); i++)
1751 switch (*format_ptr++)
1753 case 'e':
1754 if (XEXP (orig, i) != NULL)
1756 rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1757 max_depth - 1);
1758 if (!result)
1759 return NULL;
1760 if (copy)
1761 XEXP (copy, i) = result;
1763 break;
1765 case 'E':
1766 case 'V':
1767 if (XVEC (orig, i) != NULL)
1769 if (copy)
1770 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1771 for (j = 0; j < XVECLEN (orig, i); j++)
1773 rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1774 evd, max_depth - 1);
1775 if (!result)
1776 return NULL;
1777 if (copy)
1778 XVECEXP (copy, i, j) = result;
1781 break;
1783 case 't':
1784 case 'w':
1785 case 'i':
1786 case 's':
1787 case 'S':
1788 case 'T':
1789 case 'u':
1790 case 'B':
1791 case '0':
1792 /* These are left unchanged. */
1793 break;
1795 default:
1796 gcc_unreachable ();
1799 if (evd->dummy)
1800 return orig;
1802 mode = GET_MODE (copy);
1803 /* If an operand has been simplified into CONST_INT, which doesn't
1804 have a mode and the mode isn't derivable from whole rtx's mode,
1805 try simplify_*_operation first with mode from original's operand
1806 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1807 scopy = copy;
1808 switch (GET_RTX_CLASS (code))
1810 case RTX_UNARY:
1811 if (CONST_INT_P (XEXP (copy, 0))
1812 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1814 scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1815 GET_MODE (XEXP (orig, 0)));
1816 if (scopy)
1817 return scopy;
1819 break;
1820 case RTX_COMM_ARITH:
1821 case RTX_BIN_ARITH:
1822 /* These expressions can derive operand modes from the whole rtx's mode. */
1823 break;
1824 case RTX_TERNARY:
1825 case RTX_BITFIELD_OPS:
1826 if (CONST_INT_P (XEXP (copy, 0))
1827 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1829 scopy = simplify_ternary_operation (code, mode,
1830 GET_MODE (XEXP (orig, 0)),
1831 XEXP (copy, 0), XEXP (copy, 1),
1832 XEXP (copy, 2));
1833 if (scopy)
1834 return scopy;
1836 break;
1837 case RTX_COMPARE:
1838 case RTX_COMM_COMPARE:
1839 if (CONST_INT_P (XEXP (copy, 0))
1840 && GET_MODE (XEXP (copy, 1)) == VOIDmode
1841 && (GET_MODE (XEXP (orig, 0)) != VOIDmode
1842 || GET_MODE (XEXP (orig, 1)) != VOIDmode))
1844 scopy = simplify_relational_operation (code, mode,
1845 (GET_MODE (XEXP (orig, 0))
1846 != VOIDmode)
1847 ? GET_MODE (XEXP (orig, 0))
1848 : GET_MODE (XEXP (orig, 1)),
1849 XEXP (copy, 0),
1850 XEXP (copy, 1));
1851 if (scopy)
1852 return scopy;
1854 break;
1855 default:
1856 break;
1858 scopy = simplify_rtx (copy);
1859 if (scopy)
1860 return scopy;
1861 return copy;
1864 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1865 with VALUE expressions. This way, it becomes independent of changes
1866 to registers and memory.
1867 X isn't actually modified; if modifications are needed, new rtl is
1868 allocated. However, the return value can share rtl with X.
1869 If X is within a MEM, MEMMODE must be the mode of the MEM. */
1872 cselib_subst_to_values (rtx x, machine_mode memmode)
1874 enum rtx_code code = GET_CODE (x);
1875 const char *fmt = GET_RTX_FORMAT (code);
1876 cselib_val *e;
1877 struct elt_list *l;
1878 rtx copy = x;
1879 int i;
1881 switch (code)
1883 case REG:
1884 l = REG_VALUES (REGNO (x));
1885 if (l && l->elt == NULL)
1886 l = l->next;
1887 for (; l; l = l->next)
1888 if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
1889 return l->elt->val_rtx;
1891 gcc_unreachable ();
1893 case MEM:
1894 e = cselib_lookup_mem (x, 0);
1895 /* This used to happen for autoincrements, but we deal with them
1896 properly now. Remove the if stmt for the next release. */
1897 if (! e)
1899 /* Assign a value that doesn't match any other. */
1900 e = new_cselib_val (next_uid, GET_MODE (x), x);
1902 return e->val_rtx;
1904 case ENTRY_VALUE:
1905 e = cselib_lookup (x, GET_MODE (x), 0, memmode);
1906 if (! e)
1907 break;
1908 return e->val_rtx;
1910 CASE_CONST_ANY:
1911 return x;
1913 case PRE_DEC:
1914 case PRE_INC:
1915 gcc_assert (memmode != VOIDmode);
1916 i = GET_MODE_SIZE (memmode);
1917 if (code == PRE_DEC)
1918 i = -i;
1919 return cselib_subst_to_values (plus_constant (GET_MODE (x),
1920 XEXP (x, 0), i),
1921 memmode);
1923 case PRE_MODIFY:
1924 gcc_assert (memmode != VOIDmode);
1925 return cselib_subst_to_values (XEXP (x, 1), memmode);
1927 case POST_DEC:
1928 case POST_INC:
1929 case POST_MODIFY:
1930 gcc_assert (memmode != VOIDmode);
1931 return cselib_subst_to_values (XEXP (x, 0), memmode);
1933 default:
1934 break;
1937 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1939 if (fmt[i] == 'e')
1941 rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
1943 if (t != XEXP (x, i))
1945 if (x == copy)
1946 copy = shallow_copy_rtx (x);
1947 XEXP (copy, i) = t;
1950 else if (fmt[i] == 'E')
1952 int j;
1954 for (j = 0; j < XVECLEN (x, i); j++)
1956 rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
1958 if (t != XVECEXP (x, i, j))
1960 if (XVEC (x, i) == XVEC (copy, i))
1962 if (x == copy)
1963 copy = shallow_copy_rtx (x);
1964 XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
1966 XVECEXP (copy, i, j) = t;
1972 return copy;
1975 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
1978 cselib_subst_to_values_from_insn (rtx x, machine_mode memmode, rtx_insn *insn)
1980 rtx ret;
1981 gcc_assert (!cselib_current_insn);
1982 cselib_current_insn = insn;
1983 ret = cselib_subst_to_values (x, memmode);
1984 cselib_current_insn = NULL;
1985 return ret;
1988 /* Look up the rtl expression X in our tables and return the value it
1989 has. If CREATE is zero, we return NULL if we don't know the value.
1990 Otherwise, we create a new one if possible, using mode MODE if X
1991 doesn't have a mode (i.e. because it's a constant). When X is part
1992 of an address, MEMMODE should be the mode of the enclosing MEM if
1993 we're tracking autoinc expressions. */
1995 static cselib_val *
1996 cselib_lookup_1 (rtx x, machine_mode mode,
1997 int create, machine_mode memmode)
1999 cselib_val **slot;
2000 cselib_val *e;
2001 unsigned int hashval;
2003 if (GET_MODE (x) != VOIDmode)
2004 mode = GET_MODE (x);
2006 if (GET_CODE (x) == VALUE)
2007 return CSELIB_VAL_PTR (x);
2009 if (REG_P (x))
2011 struct elt_list *l;
2012 unsigned int i = REGNO (x);
2014 l = REG_VALUES (i);
2015 if (l && l->elt == NULL)
2016 l = l->next;
2017 for (; l; l = l->next)
2018 if (mode == GET_MODE (l->elt->val_rtx))
2020 promote_debug_loc (l->elt->locs);
2021 return l->elt;
2024 if (! create)
2025 return 0;
2027 if (i < FIRST_PSEUDO_REGISTER)
2029 unsigned int n = hard_regno_nregs[i][mode];
2031 if (n > max_value_regs)
2032 max_value_regs = n;
2035 e = new_cselib_val (next_uid, GET_MODE (x), x);
2036 new_elt_loc_list (e, x);
2037 if (REG_VALUES (i) == 0)
2039 /* Maintain the invariant that the first entry of
2040 REG_VALUES, if present, must be the value used to set the
2041 register, or NULL. */
2042 used_regs[n_used_regs++] = i;
2043 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
2045 else if (cselib_preserve_constants
2046 && GET_MODE_CLASS (mode) == MODE_INT)
2048 /* During var-tracking, try harder to find equivalences
2049 for SUBREGs. If a setter sets say a DImode register
2050 and user uses that register only in SImode, add a lowpart
2051 subreg location. */
2052 struct elt_list *lwider = NULL;
2053 l = REG_VALUES (i);
2054 if (l && l->elt == NULL)
2055 l = l->next;
2056 for (; l; l = l->next)
2057 if (GET_MODE_CLASS (GET_MODE (l->elt->val_rtx)) == MODE_INT
2058 && GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2059 > GET_MODE_SIZE (mode)
2060 && (lwider == NULL
2061 || GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2062 < GET_MODE_SIZE (GET_MODE (lwider->elt->val_rtx))))
2064 struct elt_loc_list *el;
2065 if (i < FIRST_PSEUDO_REGISTER
2066 && hard_regno_nregs[i][GET_MODE (l->elt->val_rtx)] != 1)
2067 continue;
2068 for (el = l->elt->locs; el; el = el->next)
2069 if (!REG_P (el->loc))
2070 break;
2071 if (el)
2072 lwider = l;
2074 if (lwider)
2076 rtx sub = lowpart_subreg (mode, lwider->elt->val_rtx,
2077 GET_MODE (lwider->elt->val_rtx));
2078 if (sub)
2079 new_elt_loc_list (e, sub);
2082 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
2083 slot = cselib_find_slot (mode, x, e->hash, INSERT, memmode);
2084 *slot = e;
2085 return e;
2088 if (MEM_P (x))
2089 return cselib_lookup_mem (x, create);
2091 hashval = cselib_hash_rtx (x, create, memmode);
2092 /* Can't even create if hashing is not possible. */
2093 if (! hashval)
2094 return 0;
2096 slot = cselib_find_slot (mode, x, hashval,
2097 create ? INSERT : NO_INSERT, memmode);
2098 if (slot == 0)
2099 return 0;
2101 e = (cselib_val *) *slot;
2102 if (e)
2103 return e;
2105 e = new_cselib_val (hashval, mode, x);
2107 /* We have to fill the slot before calling cselib_subst_to_values:
2108 the hash table is inconsistent until we do so, and
2109 cselib_subst_to_values will need to do lookups. */
2110 *slot = e;
2111 new_elt_loc_list (e, cselib_subst_to_values (x, memmode));
2112 return e;
2115 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2117 cselib_val *
2118 cselib_lookup_from_insn (rtx x, machine_mode mode,
2119 int create, machine_mode memmode, rtx_insn *insn)
2121 cselib_val *ret;
2123 gcc_assert (!cselib_current_insn);
2124 cselib_current_insn = insn;
2126 ret = cselib_lookup (x, mode, create, memmode);
2128 cselib_current_insn = NULL;
2130 return ret;
2133 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2134 maintains invariants related with debug insns. */
2136 cselib_val *
2137 cselib_lookup (rtx x, machine_mode mode,
2138 int create, machine_mode memmode)
2140 cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
2142 /* ??? Should we return NULL if we're not to create an entry, the
2143 found loc is a debug loc and cselib_current_insn is not DEBUG?
2144 If so, we should also avoid converting val to non-DEBUG; probably
2145 easiest setting cselib_current_insn to NULL before the call
2146 above. */
2148 if (dump_file && (dump_flags & TDF_CSELIB))
2150 fputs ("cselib lookup ", dump_file);
2151 print_inline_rtx (dump_file, x, 2);
2152 fprintf (dump_file, " => %u:%u\n",
2153 ret ? ret->uid : 0,
2154 ret ? ret->hash : 0);
2157 return ret;
2160 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2161 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2162 is used to determine how many hard registers are being changed. If MODE
2163 is VOIDmode, then only REGNO is being changed; this is used when
2164 invalidating call clobbered registers across a call. */
2166 static void
2167 cselib_invalidate_regno (unsigned int regno, machine_mode mode)
2169 unsigned int endregno;
2170 unsigned int i;
2172 /* If we see pseudos after reload, something is _wrong_. */
2173 gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
2174 || reg_renumber[regno] < 0);
2176 /* Determine the range of registers that must be invalidated. For
2177 pseudos, only REGNO is affected. For hard regs, we must take MODE
2178 into account, and we must also invalidate lower register numbers
2179 if they contain values that overlap REGNO. */
2180 if (regno < FIRST_PSEUDO_REGISTER)
2182 gcc_assert (mode != VOIDmode);
2184 if (regno < max_value_regs)
2185 i = 0;
2186 else
2187 i = regno - max_value_regs;
2189 endregno = end_hard_regno (mode, regno);
2191 else
2193 i = regno;
2194 endregno = regno + 1;
2197 for (; i < endregno; i++)
2199 struct elt_list **l = &REG_VALUES (i);
2201 /* Go through all known values for this reg; if it overlaps the range
2202 we're invalidating, remove the value. */
2203 while (*l)
2205 cselib_val *v = (*l)->elt;
2206 bool had_locs;
2207 rtx_insn *setting_insn;
2208 struct elt_loc_list **p;
2209 unsigned int this_last = i;
2211 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
2212 this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
2214 if (this_last < regno || v == NULL
2215 || (v == cfa_base_preserved_val
2216 && i == cfa_base_preserved_regno))
2218 l = &(*l)->next;
2219 continue;
2222 /* We have an overlap. */
2223 if (*l == REG_VALUES (i))
2225 /* Maintain the invariant that the first entry of
2226 REG_VALUES, if present, must be the value used to set
2227 the register, or NULL. This is also nice because
2228 then we won't push the same regno onto user_regs
2229 multiple times. */
2230 (*l)->elt = NULL;
2231 l = &(*l)->next;
2233 else
2234 unchain_one_elt_list (l);
2236 v = canonical_cselib_val (v);
2238 had_locs = v->locs != NULL;
2239 setting_insn = v->locs ? v->locs->setting_insn : NULL;
2241 /* Now, we clear the mapping from value to reg. It must exist, so
2242 this code will crash intentionally if it doesn't. */
2243 for (p = &v->locs; ; p = &(*p)->next)
2245 rtx x = (*p)->loc;
2247 if (REG_P (x) && REGNO (x) == i)
2249 unchain_one_elt_loc_list (p);
2250 break;
2254 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2256 if (setting_insn && DEBUG_INSN_P (setting_insn))
2257 n_useless_debug_values++;
2258 else
2259 n_useless_values++;
2265 /* Invalidate any locations in the table which are changed because of a
2266 store to MEM_RTX. If this is called because of a non-const call
2267 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2269 static void
2270 cselib_invalidate_mem (rtx mem_rtx)
2272 cselib_val **vp, *v, *next;
2273 int num_mems = 0;
2274 rtx mem_addr;
2276 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2277 mem_rtx = canon_rtx (mem_rtx);
2279 vp = &first_containing_mem;
2280 for (v = *vp; v != &dummy_val; v = next)
2282 bool has_mem = false;
2283 struct elt_loc_list **p = &v->locs;
2284 bool had_locs = v->locs != NULL;
2285 rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
2287 while (*p)
2289 rtx x = (*p)->loc;
2290 cselib_val *addr;
2291 struct elt_list **mem_chain;
2293 /* MEMs may occur in locations only at the top level; below
2294 that every MEM or REG is substituted by its VALUE. */
2295 if (!MEM_P (x))
2297 p = &(*p)->next;
2298 continue;
2300 if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
2301 && ! canon_anti_dependence (x, false, mem_rtx,
2302 GET_MODE (mem_rtx), mem_addr))
2304 has_mem = true;
2305 num_mems++;
2306 p = &(*p)->next;
2307 continue;
2310 /* This one overlaps. */
2311 /* We must have a mapping from this MEM's address to the
2312 value (E). Remove that, too. */
2313 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
2314 addr = canonical_cselib_val (addr);
2315 gcc_checking_assert (v == canonical_cselib_val (v));
2316 mem_chain = &addr->addr_list;
2317 for (;;)
2319 cselib_val *canon = canonical_cselib_val ((*mem_chain)->elt);
2321 if (canon == v)
2323 unchain_one_elt_list (mem_chain);
2324 break;
2327 /* Record canonicalized elt. */
2328 (*mem_chain)->elt = canon;
2330 mem_chain = &(*mem_chain)->next;
2333 unchain_one_elt_loc_list (p);
2336 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2338 if (setting_insn && DEBUG_INSN_P (setting_insn))
2339 n_useless_debug_values++;
2340 else
2341 n_useless_values++;
2344 next = v->next_containing_mem;
2345 if (has_mem)
2347 *vp = v;
2348 vp = &(*vp)->next_containing_mem;
2350 else
2351 v->next_containing_mem = NULL;
2353 *vp = &dummy_val;
2356 /* Invalidate DEST, which is being assigned to or clobbered. */
2358 void
2359 cselib_invalidate_rtx (rtx dest)
2361 while (GET_CODE (dest) == SUBREG
2362 || GET_CODE (dest) == ZERO_EXTRACT
2363 || GET_CODE (dest) == STRICT_LOW_PART)
2364 dest = XEXP (dest, 0);
2366 if (REG_P (dest))
2367 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
2368 else if (MEM_P (dest))
2369 cselib_invalidate_mem (dest);
2372 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2374 static void
2375 cselib_invalidate_rtx_note_stores (rtx dest, const_rtx ignore ATTRIBUTE_UNUSED,
2376 void *data ATTRIBUTE_UNUSED)
2378 cselib_invalidate_rtx (dest);
2381 /* Record the result of a SET instruction. DEST is being set; the source
2382 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2383 describes its address. */
2385 static void
2386 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
2388 if (src_elt == 0 || side_effects_p (dest))
2389 return;
2391 if (REG_P (dest))
2393 unsigned int dreg = REGNO (dest);
2394 if (dreg < FIRST_PSEUDO_REGISTER)
2396 unsigned int n = REG_NREGS (dest);
2398 if (n > max_value_regs)
2399 max_value_regs = n;
2402 if (REG_VALUES (dreg) == 0)
2404 used_regs[n_used_regs++] = dreg;
2405 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2407 else
2409 /* The register should have been invalidated. */
2410 gcc_assert (REG_VALUES (dreg)->elt == 0);
2411 REG_VALUES (dreg)->elt = src_elt;
2414 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2415 n_useless_values--;
2416 new_elt_loc_list (src_elt, dest);
2418 else if (MEM_P (dest) && dest_addr_elt != 0
2419 && cselib_record_memory)
2421 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2422 n_useless_values--;
2423 add_mem_for_addr (dest_addr_elt, src_elt, dest);
2427 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2429 void
2430 cselib_add_permanent_equiv (cselib_val *elt, rtx x, rtx_insn *insn)
2432 cselib_val *nelt;
2433 rtx_insn *save_cselib_current_insn = cselib_current_insn;
2435 gcc_checking_assert (elt);
2436 gcc_checking_assert (PRESERVED_VALUE_P (elt->val_rtx));
2437 gcc_checking_assert (!side_effects_p (x));
2439 cselib_current_insn = insn;
2441 nelt = cselib_lookup (x, GET_MODE (elt->val_rtx), 1, VOIDmode);
2443 if (nelt != elt)
2445 cselib_any_perm_equivs = true;
2447 if (!PRESERVED_VALUE_P (nelt->val_rtx))
2448 cselib_preserve_value (nelt);
2450 new_elt_loc_list (nelt, elt->val_rtx);
2453 cselib_current_insn = save_cselib_current_insn;
2456 /* Return TRUE if any permanent equivalences have been recorded since
2457 the table was last initialized. */
2458 bool
2459 cselib_have_permanent_equivalences (void)
2461 return cselib_any_perm_equivs;
2464 /* There is no good way to determine how many elements there can be
2465 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2466 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2468 struct cselib_record_autoinc_data
2470 struct cselib_set *sets;
2471 int n_sets;
2474 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2475 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2477 static int
2478 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
2479 rtx dest, rtx src, rtx srcoff, void *arg)
2481 struct cselib_record_autoinc_data *data;
2482 data = (struct cselib_record_autoinc_data *)arg;
2484 data->sets[data->n_sets].dest = dest;
2486 if (srcoff)
2487 data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
2488 else
2489 data->sets[data->n_sets].src = src;
2491 data->n_sets++;
2493 return 0;
2496 /* Record the effects of any sets and autoincs in INSN. */
2497 static void
2498 cselib_record_sets (rtx_insn *insn)
2500 int n_sets = 0;
2501 int i;
2502 struct cselib_set sets[MAX_SETS];
2503 rtx body = PATTERN (insn);
2504 rtx cond = 0;
2505 int n_sets_before_autoinc;
2506 struct cselib_record_autoinc_data data;
2508 body = PATTERN (insn);
2509 if (GET_CODE (body) == COND_EXEC)
2511 cond = COND_EXEC_TEST (body);
2512 body = COND_EXEC_CODE (body);
2515 /* Find all sets. */
2516 if (GET_CODE (body) == SET)
2518 sets[0].src = SET_SRC (body);
2519 sets[0].dest = SET_DEST (body);
2520 n_sets = 1;
2522 else if (GET_CODE (body) == PARALLEL)
2524 /* Look through the PARALLEL and record the values being
2525 set, if possible. Also handle any CLOBBERs. */
2526 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2528 rtx x = XVECEXP (body, 0, i);
2530 if (GET_CODE (x) == SET)
2532 sets[n_sets].src = SET_SRC (x);
2533 sets[n_sets].dest = SET_DEST (x);
2534 n_sets++;
2539 if (n_sets == 1
2540 && MEM_P (sets[0].src)
2541 && !cselib_record_memory
2542 && MEM_READONLY_P (sets[0].src))
2544 rtx note = find_reg_equal_equiv_note (insn);
2546 if (note && CONSTANT_P (XEXP (note, 0)))
2547 sets[0].src = XEXP (note, 0);
2550 data.sets = sets;
2551 data.n_sets = n_sets_before_autoinc = n_sets;
2552 for_each_inc_dec (PATTERN (insn), cselib_record_autoinc_cb, &data);
2553 n_sets = data.n_sets;
2555 /* Look up the values that are read. Do this before invalidating the
2556 locations that are written. */
2557 for (i = 0; i < n_sets; i++)
2559 rtx dest = sets[i].dest;
2561 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2562 the low part after invalidating any knowledge about larger modes. */
2563 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2564 sets[i].dest = dest = XEXP (dest, 0);
2566 /* We don't know how to record anything but REG or MEM. */
2567 if (REG_P (dest)
2568 || (MEM_P (dest) && cselib_record_memory))
2570 rtx src = sets[i].src;
2571 if (cond)
2572 src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
2573 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
2574 if (MEM_P (dest))
2576 machine_mode address_mode = get_address_mode (dest);
2578 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
2579 address_mode, 1,
2580 GET_MODE (dest));
2582 else
2583 sets[i].dest_addr_elt = 0;
2587 if (cselib_record_sets_hook)
2588 cselib_record_sets_hook (insn, sets, n_sets);
2590 /* Invalidate all locations written by this insn. Note that the elts we
2591 looked up in the previous loop aren't affected, just some of their
2592 locations may go away. */
2593 note_stores (body, cselib_invalidate_rtx_note_stores, NULL);
2595 for (i = n_sets_before_autoinc; i < n_sets; i++)
2596 cselib_invalidate_rtx (sets[i].dest);
2598 /* If this is an asm, look for duplicate sets. This can happen when the
2599 user uses the same value as an output multiple times. This is valid
2600 if the outputs are not actually used thereafter. Treat this case as
2601 if the value isn't actually set. We do this by smashing the destination
2602 to pc_rtx, so that we won't record the value later. */
2603 if (n_sets >= 2 && asm_noperands (body) >= 0)
2605 for (i = 0; i < n_sets; i++)
2607 rtx dest = sets[i].dest;
2608 if (REG_P (dest) || MEM_P (dest))
2610 int j;
2611 for (j = i + 1; j < n_sets; j++)
2612 if (rtx_equal_p (dest, sets[j].dest))
2614 sets[i].dest = pc_rtx;
2615 sets[j].dest = pc_rtx;
2621 /* Now enter the equivalences in our tables. */
2622 for (i = 0; i < n_sets; i++)
2624 rtx dest = sets[i].dest;
2625 if (REG_P (dest)
2626 || (MEM_P (dest) && cselib_record_memory))
2627 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2631 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
2633 bool
2634 fp_setter_insn (rtx_insn *insn)
2636 rtx expr, pat = NULL_RTX;
2638 if (!RTX_FRAME_RELATED_P (insn))
2639 return false;
2641 expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2642 if (expr)
2643 pat = XEXP (expr, 0);
2644 if (!modified_in_p (hard_frame_pointer_rtx, pat ? pat : insn))
2645 return false;
2647 /* Don't return true for frame pointer restores in the epilogue. */
2648 if (find_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx))
2649 return false;
2650 return true;
2653 /* Record the effects of INSN. */
2655 void
2656 cselib_process_insn (rtx_insn *insn)
2658 int i;
2659 rtx x;
2661 cselib_current_insn = insn;
2663 /* Forget everything at a CODE_LABEL or a setjmp. */
2664 if ((LABEL_P (insn)
2665 || (CALL_P (insn)
2666 && find_reg_note (insn, REG_SETJMP, NULL)))
2667 && !cselib_preserve_constants)
2669 cselib_reset_table (next_uid);
2670 cselib_current_insn = NULL;
2671 return;
2674 if (! INSN_P (insn))
2676 cselib_current_insn = NULL;
2677 return;
2680 /* If this is a call instruction, forget anything stored in a
2681 call clobbered register, or, if this is not a const call, in
2682 memory. */
2683 if (CALL_P (insn))
2685 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2686 if (call_used_regs[i]
2687 || (REG_VALUES (i) && REG_VALUES (i)->elt
2688 && HARD_REGNO_CALL_PART_CLOBBERED (i,
2689 GET_MODE (REG_VALUES (i)->elt->val_rtx))))
2690 cselib_invalidate_regno (i, reg_raw_mode[i]);
2692 /* Since it is not clear how cselib is going to be used, be
2693 conservative here and treat looping pure or const functions
2694 as if they were regular functions. */
2695 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
2696 || !(RTL_CONST_OR_PURE_CALL_P (insn)))
2697 cselib_invalidate_mem (callmem);
2700 cselib_record_sets (insn);
2702 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2703 after we have processed the insn. */
2704 if (CALL_P (insn))
2706 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
2707 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
2708 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
2709 /* Flush evertything on setjmp. */
2710 if (cselib_preserve_constants
2711 && find_reg_note (insn, REG_SETJMP, NULL))
2713 cselib_preserve_only_values ();
2714 cselib_reset_table (next_uid);
2718 /* On setter of the hard frame pointer if frame_pointer_needed,
2719 invalidate stack_pointer_rtx, so that sp and {,h}fp based
2720 VALUEs are distinct. */
2721 if (reload_completed
2722 && frame_pointer_needed
2723 && fp_setter_insn (insn))
2724 cselib_invalidate_rtx (stack_pointer_rtx);
2726 cselib_current_insn = NULL;
2728 if (n_useless_values > MAX_USELESS_VALUES
2729 /* remove_useless_values is linear in the hash table size. Avoid
2730 quadratic behavior for very large hashtables with very few
2731 useless elements. */
2732 && ((unsigned int)n_useless_values
2733 > (cselib_hash_table->elements () - n_debug_values) / 4))
2734 remove_useless_values ();
2737 /* Initialize cselib for one pass. The caller must also call
2738 init_alias_analysis. */
2740 void
2741 cselib_init (int record_what)
2743 cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
2744 cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
2745 cselib_any_perm_equivs = false;
2747 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2748 see canon_true_dependence. This is only created once. */
2749 if (! callmem)
2750 callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2752 cselib_nregs = max_reg_num ();
2754 /* We preserve reg_values to allow expensive clearing of the whole thing.
2755 Reallocate it however if it happens to be too large. */
2756 if (!reg_values || reg_values_size < cselib_nregs
2757 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
2759 free (reg_values);
2760 /* Some space for newly emit instructions so we don't end up
2761 reallocating in between passes. */
2762 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
2763 reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
2765 used_regs = XNEWVEC (unsigned int, cselib_nregs);
2766 n_used_regs = 0;
2767 cselib_hash_table = new hash_table<cselib_hasher> (31);
2768 if (cselib_preserve_constants)
2769 cselib_preserved_hash_table = new hash_table<cselib_hasher> (31);
2770 next_uid = 1;
2773 /* Called when the current user is done with cselib. */
2775 void
2776 cselib_finish (void)
2778 bool preserved = cselib_preserve_constants;
2779 cselib_discard_hook = NULL;
2780 cselib_preserve_constants = false;
2781 cselib_any_perm_equivs = false;
2782 cfa_base_preserved_val = NULL;
2783 cfa_base_preserved_regno = INVALID_REGNUM;
2784 elt_list::pool.release ();
2785 elt_loc_list::pool.release ();
2786 cselib_val::pool.release ();
2787 value_pool.release ();
2788 cselib_clear_table ();
2789 delete cselib_hash_table;
2790 cselib_hash_table = NULL;
2791 if (preserved)
2792 delete cselib_preserved_hash_table;
2793 cselib_preserved_hash_table = NULL;
2794 free (used_regs);
2795 used_regs = 0;
2796 n_useless_values = 0;
2797 n_useless_debug_values = 0;
2798 n_debug_values = 0;
2799 next_uid = 0;
2802 /* Dump the cselib_val *X to FILE *OUT. */
2805 dump_cselib_val (cselib_val **x, FILE *out)
2807 cselib_val *v = *x;
2808 bool need_lf = true;
2810 print_inline_rtx (out, v->val_rtx, 0);
2812 if (v->locs)
2814 struct elt_loc_list *l = v->locs;
2815 if (need_lf)
2817 fputc ('\n', out);
2818 need_lf = false;
2820 fputs (" locs:", out);
2823 if (l->setting_insn)
2824 fprintf (out, "\n from insn %i ",
2825 INSN_UID (l->setting_insn));
2826 else
2827 fprintf (out, "\n ");
2828 print_inline_rtx (out, l->loc, 4);
2830 while ((l = l->next));
2831 fputc ('\n', out);
2833 else
2835 fputs (" no locs", out);
2836 need_lf = true;
2839 if (v->addr_list)
2841 struct elt_list *e = v->addr_list;
2842 if (need_lf)
2844 fputc ('\n', out);
2845 need_lf = false;
2847 fputs (" addr list:", out);
2850 fputs ("\n ", out);
2851 print_inline_rtx (out, e->elt->val_rtx, 2);
2853 while ((e = e->next));
2854 fputc ('\n', out);
2856 else
2858 fputs (" no addrs", out);
2859 need_lf = true;
2862 if (v->next_containing_mem == &dummy_val)
2863 fputs (" last mem\n", out);
2864 else if (v->next_containing_mem)
2866 fputs (" next mem ", out);
2867 print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
2868 fputc ('\n', out);
2870 else if (need_lf)
2871 fputc ('\n', out);
2873 return 1;
2876 /* Dump to OUT everything in the CSELIB table. */
2878 void
2879 dump_cselib_table (FILE *out)
2881 fprintf (out, "cselib hash table:\n");
2882 cselib_hash_table->traverse <FILE *, dump_cselib_val> (out);
2883 fprintf (out, "cselib preserved hash table:\n");
2884 cselib_preserved_hash_table->traverse <FILE *, dump_cselib_val> (out);
2885 if (first_containing_mem != &dummy_val)
2887 fputs ("first mem ", out);
2888 print_inline_rtx (out, first_containing_mem->val_rtx, 2);
2889 fputc ('\n', out);
2891 fprintf (out, "next uid %i\n", next_uid);
2894 #include "gt-cselib.h"