Merge from trunk: 215733-215743
[official-gcc.git] / gcc-4_9 / gcc / cselib.c
blob7918b2be869d081cfa7c8de9c480f5a406ce02e9
1 /* Common subexpression elimination library for GNU compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
25 #include "rtl.h"
26 #include "tree.h"/* FIXME: For hashing DEBUG_EXPR & friends. */
27 #include "tm_p.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "flags.h"
31 #include "insn-config.h"
32 #include "recog.h"
33 #include "function.h"
34 #include "emit-rtl.h"
35 #include "diagnostic-core.h"
36 #include "ggc.h"
37 #include "hash-table.h"
38 #include "dumpfile.h"
39 #include "cselib.h"
40 #include "valtrack.h"
41 #include "params.h"
42 #include "alloc-pool.h"
43 #include "target.h"
44 #include "bitmap.h"
46 /* A list of cselib_val structures. */
47 struct elt_list {
48 struct elt_list *next;
49 cselib_val *elt;
52 /* See the documentation of cselib_find_slot below. */
53 static enum machine_mode find_slot_memmode;
55 static bool cselib_record_memory;
56 static bool cselib_preserve_constants;
57 static bool cselib_any_perm_equivs;
58 static inline void promote_debug_loc (struct elt_loc_list *l);
59 static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
60 static void new_elt_loc_list (cselib_val *, rtx);
61 static void unchain_one_value (cselib_val *);
62 static void unchain_one_elt_list (struct elt_list **);
63 static void unchain_one_elt_loc_list (struct elt_loc_list **);
64 static void remove_useless_values (void);
65 static int rtx_equal_for_cselib_1 (rtx, rtx, enum machine_mode);
66 static unsigned int cselib_hash_rtx (rtx, int, enum machine_mode);
67 static cselib_val *new_cselib_val (unsigned int, enum machine_mode, rtx);
68 static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
69 static cselib_val *cselib_lookup_mem (rtx, int);
70 static void cselib_invalidate_regno (unsigned int, enum machine_mode);
71 static void cselib_invalidate_mem (rtx);
72 static void cselib_record_set (rtx, cselib_val *, cselib_val *);
73 static void cselib_record_sets (rtx);
75 struct expand_value_data
77 bitmap regs_active;
78 cselib_expand_callback callback;
79 void *callback_arg;
80 bool dummy;
83 static rtx cselib_expand_value_rtx_1 (rtx, struct expand_value_data *, int);
85 /* There are three ways in which cselib can look up an rtx:
86 - for a REG, the reg_values table (which is indexed by regno) is used
87 - for a MEM, we recursively look up its address and then follow the
88 addr_list of that value
89 - for everything else, we compute a hash value and go through the hash
90 table. Since different rtx's can still have the same hash value,
91 this involves walking the table entries for a given value and comparing
92 the locations of the entries with the rtx we are looking up. */
94 struct cselib_hasher : typed_noop_remove <cselib_val>
96 typedef cselib_val value_type;
97 typedef rtx_def compare_type;
98 static inline hashval_t hash (const value_type *);
99 static inline bool equal (const value_type *, const compare_type *);
102 /* The hash function for our hash table. The value is always computed with
103 cselib_hash_rtx when adding an element; this function just extracts the
104 hash value from a cselib_val structure. */
106 inline hashval_t
107 cselib_hasher::hash (const value_type *v)
109 return v->hash;
112 /* The equality test for our hash table. The first argument V is a table
113 element (i.e. a cselib_val), while the second arg X is an rtx. We know
114 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
115 CONST of an appropriate mode. */
117 inline bool
118 cselib_hasher::equal (const value_type *v, const compare_type *x_arg)
120 struct elt_loc_list *l;
121 rtx x = CONST_CAST_RTX (x_arg);
122 enum machine_mode mode = GET_MODE (x);
124 gcc_assert (!CONST_SCALAR_INT_P (x) && GET_CODE (x) != CONST_FIXED);
126 if (mode != GET_MODE (v->val_rtx))
127 return false;
129 /* Unwrap X if necessary. */
130 if (GET_CODE (x) == CONST
131 && (CONST_SCALAR_INT_P (XEXP (x, 0))
132 || GET_CODE (XEXP (x, 0)) == CONST_FIXED))
133 x = XEXP (x, 0);
135 if (GET_CODE (x) == VALUE)
136 return x == v->val_rtx;
138 /* We don't guarantee that distinct rtx's have different hash values,
139 so we need to do a comparison. */
140 for (l = v->locs; l; l = l->next)
141 if (rtx_equal_for_cselib_1 (l->loc, x, find_slot_memmode))
143 promote_debug_loc (l);
144 return true;
147 return false;
150 /* A table that enables us to look up elts by their value. */
151 static hash_table <cselib_hasher> cselib_hash_table;
153 /* A table to hold preserved values. */
154 static hash_table <cselib_hasher> cselib_preserved_hash_table;
156 /* This is a global so we don't have to pass this through every function.
157 It is used in new_elt_loc_list to set SETTING_INSN. */
158 static rtx cselib_current_insn;
160 /* The unique id that the next create value will take. */
161 static unsigned int next_uid;
163 /* The number of registers we had when the varrays were last resized. */
164 static unsigned int cselib_nregs;
166 /* Count values without known locations, or with only locations that
167 wouldn't have been known except for debug insns. Whenever this
168 grows too big, we remove these useless values from the table.
170 Counting values with only debug values is a bit tricky. We don't
171 want to increment n_useless_values when we create a value for a
172 debug insn, for this would get n_useless_values out of sync, but we
173 want increment it if all locs in the list that were ever referenced
174 in nondebug insns are removed from the list.
176 In the general case, once we do that, we'd have to stop accepting
177 nondebug expressions in the loc list, to avoid having two values
178 equivalent that, without debug insns, would have been made into
179 separate values. However, because debug insns never introduce
180 equivalences themselves (no assignments), the only means for
181 growing loc lists is through nondebug assignments. If the locs
182 also happen to be referenced in debug insns, it will work just fine.
184 A consequence of this is that there's at most one debug-only loc in
185 each loc list. If we keep it in the first entry, testing whether
186 we have a debug-only loc list takes O(1).
188 Furthermore, since any additional entry in a loc list containing a
189 debug loc would have to come from an assignment (nondebug) that
190 references both the initial debug loc and the newly-equivalent loc,
191 the initial debug loc would be promoted to a nondebug loc, and the
192 loc list would not contain debug locs any more.
194 So the only case we have to be careful with in order to keep
195 n_useless_values in sync between debug and nondebug compilations is
196 to avoid incrementing n_useless_values when removing the single loc
197 from a value that turns out to not appear outside debug values. We
198 increment n_useless_debug_values instead, and leave such values
199 alone until, for other reasons, we garbage-collect useless
200 values. */
201 static int n_useless_values;
202 static int n_useless_debug_values;
204 /* Count values whose locs have been taken exclusively from debug
205 insns for the entire life of the value. */
206 static int n_debug_values;
208 /* Number of useless values before we remove them from the hash table. */
209 #define MAX_USELESS_VALUES 32
211 /* This table maps from register number to values. It does not
212 contain pointers to cselib_val structures, but rather elt_lists.
213 The purpose is to be able to refer to the same register in
214 different modes. The first element of the list defines the mode in
215 which the register was set; if the mode is unknown or the value is
216 no longer valid in that mode, ELT will be NULL for the first
217 element. */
218 static struct elt_list **reg_values;
219 static unsigned int reg_values_size;
220 #define REG_VALUES(i) reg_values[i]
222 /* The largest number of hard regs used by any entry added to the
223 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
224 static unsigned int max_value_regs;
226 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
227 in cselib_clear_table() for fast emptying. */
228 static unsigned int *used_regs;
229 static unsigned int n_used_regs;
231 /* We pass this to cselib_invalidate_mem to invalidate all of
232 memory for a non-const call instruction. */
233 static GTY(()) rtx callmem;
235 /* Set by discard_useless_locs if it deleted the last location of any
236 value. */
237 static int values_became_useless;
239 /* Used as stop element of the containing_mem list so we can check
240 presence in the list by checking the next pointer. */
241 static cselib_val dummy_val;
243 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
244 that is constant through the whole function and should never be
245 eliminated. */
246 static cselib_val *cfa_base_preserved_val;
247 static unsigned int cfa_base_preserved_regno = INVALID_REGNUM;
249 /* Used to list all values that contain memory reference.
250 May or may not contain the useless values - the list is compacted
251 each time memory is invalidated. */
252 static cselib_val *first_containing_mem = &dummy_val;
253 static alloc_pool elt_loc_list_pool, elt_list_pool, cselib_val_pool, value_pool;
255 /* If nonnull, cselib will call this function before freeing useless
256 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
257 void (*cselib_discard_hook) (cselib_val *);
259 /* If nonnull, cselib will call this function before recording sets or
260 even clobbering outputs of INSN. All the recorded sets will be
261 represented in the array sets[n_sets]. new_val_min can be used to
262 tell whether values present in sets are introduced by this
263 instruction. */
264 void (*cselib_record_sets_hook) (rtx insn, struct cselib_set *sets,
265 int n_sets);
267 #define PRESERVED_VALUE_P(RTX) \
268 (RTL_FLAG_CHECK1 ("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
270 #define SP_BASED_VALUE_P(RTX) \
271 (RTL_FLAG_CHECK1 ("SP_BASED_VALUE_P", (RTX), VALUE)->jump)
275 /* Allocate a struct elt_list and fill in its two elements with the
276 arguments. */
278 static inline struct elt_list *
279 new_elt_list (struct elt_list *next, cselib_val *elt)
281 struct elt_list *el;
282 el = (struct elt_list *) pool_alloc (elt_list_pool);
283 el->next = next;
284 el->elt = elt;
285 return el;
288 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
289 list. */
291 static inline void
292 new_elt_loc_list (cselib_val *val, rtx loc)
294 struct elt_loc_list *el, *next = val->locs;
296 gcc_checking_assert (!next || !next->setting_insn
297 || !DEBUG_INSN_P (next->setting_insn)
298 || cselib_current_insn == next->setting_insn);
300 /* If we're creating the first loc in a debug insn context, we've
301 just created a debug value. Count it. */
302 if (!next && cselib_current_insn && DEBUG_INSN_P (cselib_current_insn))
303 n_debug_values++;
305 val = canonical_cselib_val (val);
306 next = val->locs;
308 if (GET_CODE (loc) == VALUE)
310 loc = canonical_cselib_val (CSELIB_VAL_PTR (loc))->val_rtx;
312 gcc_checking_assert (PRESERVED_VALUE_P (loc)
313 == PRESERVED_VALUE_P (val->val_rtx));
315 if (val->val_rtx == loc)
316 return;
317 else if (val->uid > CSELIB_VAL_PTR (loc)->uid)
319 /* Reverse the insertion. */
320 new_elt_loc_list (CSELIB_VAL_PTR (loc), val->val_rtx);
321 return;
324 gcc_checking_assert (val->uid < CSELIB_VAL_PTR (loc)->uid);
326 if (CSELIB_VAL_PTR (loc)->locs)
328 /* Bring all locs from LOC to VAL. */
329 for (el = CSELIB_VAL_PTR (loc)->locs; el->next; el = el->next)
331 /* Adjust values that have LOC as canonical so that VAL
332 becomes their canonical. */
333 if (el->loc && GET_CODE (el->loc) == VALUE)
335 gcc_checking_assert (CSELIB_VAL_PTR (el->loc)->locs->loc
336 == loc);
337 CSELIB_VAL_PTR (el->loc)->locs->loc = val->val_rtx;
340 el->next = val->locs;
341 next = val->locs = CSELIB_VAL_PTR (loc)->locs;
344 if (CSELIB_VAL_PTR (loc)->addr_list)
346 /* Bring in addr_list into canonical node. */
347 struct elt_list *last = CSELIB_VAL_PTR (loc)->addr_list;
348 while (last->next)
349 last = last->next;
350 last->next = val->addr_list;
351 val->addr_list = CSELIB_VAL_PTR (loc)->addr_list;
352 CSELIB_VAL_PTR (loc)->addr_list = NULL;
355 if (CSELIB_VAL_PTR (loc)->next_containing_mem != NULL
356 && val->next_containing_mem == NULL)
358 /* Add VAL to the containing_mem list after LOC. LOC will
359 be removed when we notice it doesn't contain any
360 MEMs. */
361 val->next_containing_mem = CSELIB_VAL_PTR (loc)->next_containing_mem;
362 CSELIB_VAL_PTR (loc)->next_containing_mem = val;
365 /* Chain LOC back to VAL. */
366 el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
367 el->loc = val->val_rtx;
368 el->setting_insn = cselib_current_insn;
369 el->next = NULL;
370 CSELIB_VAL_PTR (loc)->locs = el;
373 el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
374 el->loc = loc;
375 el->setting_insn = cselib_current_insn;
376 el->next = next;
377 val->locs = el;
380 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
381 originating from a debug insn, maintaining the debug values
382 count. */
384 static inline void
385 promote_debug_loc (struct elt_loc_list *l)
387 if (l && l->setting_insn && DEBUG_INSN_P (l->setting_insn)
388 && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
390 n_debug_values--;
391 l->setting_insn = cselib_current_insn;
392 if (cselib_preserve_constants && l->next)
394 gcc_assert (l->next->setting_insn
395 && DEBUG_INSN_P (l->next->setting_insn)
396 && !l->next->next);
397 l->next->setting_insn = cselib_current_insn;
399 else
400 gcc_assert (!l->next);
404 /* The elt_list at *PL is no longer needed. Unchain it and free its
405 storage. */
407 static inline void
408 unchain_one_elt_list (struct elt_list **pl)
410 struct elt_list *l = *pl;
412 *pl = l->next;
413 pool_free (elt_list_pool, l);
416 /* Likewise for elt_loc_lists. */
418 static void
419 unchain_one_elt_loc_list (struct elt_loc_list **pl)
421 struct elt_loc_list *l = *pl;
423 *pl = l->next;
424 pool_free (elt_loc_list_pool, l);
427 /* Likewise for cselib_vals. This also frees the addr_list associated with
428 V. */
430 static void
431 unchain_one_value (cselib_val *v)
433 while (v->addr_list)
434 unchain_one_elt_list (&v->addr_list);
436 pool_free (cselib_val_pool, v);
439 /* Remove all entries from the hash table. Also used during
440 initialization. */
442 void
443 cselib_clear_table (void)
445 cselib_reset_table (1);
448 /* Return TRUE if V is a constant, a function invariant or a VALUE
449 equivalence; FALSE otherwise. */
451 static bool
452 invariant_or_equiv_p (cselib_val *v)
454 struct elt_loc_list *l;
456 if (v == cfa_base_preserved_val)
457 return true;
459 /* Keep VALUE equivalences around. */
460 for (l = v->locs; l; l = l->next)
461 if (GET_CODE (l->loc) == VALUE)
462 return true;
464 if (v->locs != NULL
465 && v->locs->next == NULL)
467 if (CONSTANT_P (v->locs->loc)
468 && (GET_CODE (v->locs->loc) != CONST
469 || !references_value_p (v->locs->loc, 0)))
470 return true;
471 /* Although a debug expr may be bound to different expressions,
472 we can preserve it as if it was constant, to get unification
473 and proper merging within var-tracking. */
474 if (GET_CODE (v->locs->loc) == DEBUG_EXPR
475 || GET_CODE (v->locs->loc) == DEBUG_IMPLICIT_PTR
476 || GET_CODE (v->locs->loc) == ENTRY_VALUE
477 || GET_CODE (v->locs->loc) == DEBUG_PARAMETER_REF)
478 return true;
480 /* (plus (value V) (const_int C)) is invariant iff V is invariant. */
481 if (GET_CODE (v->locs->loc) == PLUS
482 && CONST_INT_P (XEXP (v->locs->loc, 1))
483 && GET_CODE (XEXP (v->locs->loc, 0)) == VALUE
484 && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v->locs->loc, 0))))
485 return true;
488 return false;
491 /* Remove from hash table all VALUEs except constants, function
492 invariants and VALUE equivalences. */
495 preserve_constants_and_equivs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
497 cselib_val *v = *x;
499 if (invariant_or_equiv_p (v))
501 cselib_val **slot
502 = cselib_preserved_hash_table.find_slot_with_hash (v->val_rtx,
503 v->hash, INSERT);
504 gcc_assert (!*slot);
505 *slot = v;
508 cselib_hash_table.clear_slot (x);
510 return 1;
513 /* Remove all entries from the hash table, arranging for the next
514 value to be numbered NUM. */
516 void
517 cselib_reset_table (unsigned int num)
519 unsigned int i;
521 max_value_regs = 0;
523 if (cfa_base_preserved_val)
525 unsigned int regno = cfa_base_preserved_regno;
526 unsigned int new_used_regs = 0;
527 for (i = 0; i < n_used_regs; i++)
528 if (used_regs[i] == regno)
530 new_used_regs = 1;
531 continue;
533 else
534 REG_VALUES (used_regs[i]) = 0;
535 gcc_assert (new_used_regs == 1);
536 n_used_regs = new_used_regs;
537 used_regs[0] = regno;
538 max_value_regs
539 = hard_regno_nregs[regno][GET_MODE (cfa_base_preserved_val->locs->loc)];
541 else
543 for (i = 0; i < n_used_regs; i++)
544 REG_VALUES (used_regs[i]) = 0;
545 n_used_regs = 0;
548 if (cselib_preserve_constants)
549 cselib_hash_table.traverse <void *, preserve_constants_and_equivs> (NULL);
550 else
552 cselib_hash_table.empty ();
553 gcc_checking_assert (!cselib_any_perm_equivs);
556 n_useless_values = 0;
557 n_useless_debug_values = 0;
558 n_debug_values = 0;
560 next_uid = num;
562 first_containing_mem = &dummy_val;
565 /* Return the number of the next value that will be generated. */
567 unsigned int
568 cselib_get_next_uid (void)
570 return next_uid;
573 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
574 INSERTing if requested. When X is part of the address of a MEM,
575 MEMMODE should specify the mode of the MEM. While searching the
576 table, MEMMODE is held in FIND_SLOT_MEMMODE, so that autoinc RTXs
577 in X can be resolved. */
579 static cselib_val **
580 cselib_find_slot (rtx x, hashval_t hash, enum insert_option insert,
581 enum machine_mode memmode)
583 cselib_val **slot = NULL;
584 find_slot_memmode = memmode;
585 if (cselib_preserve_constants)
586 slot = cselib_preserved_hash_table.find_slot_with_hash (x, hash,
587 NO_INSERT);
588 if (!slot)
589 slot = cselib_hash_table.find_slot_with_hash (x, hash, insert);
590 find_slot_memmode = VOIDmode;
591 return slot;
594 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
595 only return true for values which point to a cselib_val whose value
596 element has been set to zero, which implies the cselib_val will be
597 removed. */
600 references_value_p (const_rtx x, int only_useless)
602 const enum rtx_code code = GET_CODE (x);
603 const char *fmt = GET_RTX_FORMAT (code);
604 int i, j;
606 if (GET_CODE (x) == VALUE
607 && (! only_useless ||
608 (CSELIB_VAL_PTR (x)->locs == 0 && !PRESERVED_VALUE_P (x))))
609 return 1;
611 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
613 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
614 return 1;
615 else if (fmt[i] == 'E')
616 for (j = 0; j < XVECLEN (x, i); j++)
617 if (references_value_p (XVECEXP (x, i, j), only_useless))
618 return 1;
621 return 0;
624 /* For all locations found in X, delete locations that reference useless
625 values (i.e. values without any location). Called through
626 htab_traverse. */
629 discard_useless_locs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
631 cselib_val *v = *x;
632 struct elt_loc_list **p = &v->locs;
633 bool had_locs = v->locs != NULL;
634 rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
636 while (*p)
638 if (references_value_p ((*p)->loc, 1))
639 unchain_one_elt_loc_list (p);
640 else
641 p = &(*p)->next;
644 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
646 if (setting_insn && DEBUG_INSN_P (setting_insn))
647 n_useless_debug_values++;
648 else
649 n_useless_values++;
650 values_became_useless = 1;
652 return 1;
655 /* If X is a value with no locations, remove it from the hashtable. */
658 discard_useless_values (cselib_val **x, void *info ATTRIBUTE_UNUSED)
660 cselib_val *v = *x;
662 if (v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
664 if (cselib_discard_hook)
665 cselib_discard_hook (v);
667 CSELIB_VAL_PTR (v->val_rtx) = NULL;
668 cselib_hash_table.clear_slot (x);
669 unchain_one_value (v);
670 n_useless_values--;
673 return 1;
676 /* Clean out useless values (i.e. those which no longer have locations
677 associated with them) from the hash table. */
679 static void
680 remove_useless_values (void)
682 cselib_val **p, *v;
684 /* First pass: eliminate locations that reference the value. That in
685 turn can make more values useless. */
688 values_became_useless = 0;
689 cselib_hash_table.traverse <void *, discard_useless_locs> (NULL);
691 while (values_became_useless);
693 /* Second pass: actually remove the values. */
695 p = &first_containing_mem;
696 for (v = *p; v != &dummy_val; v = v->next_containing_mem)
697 if (v->locs && v == canonical_cselib_val (v))
699 *p = v;
700 p = &(*p)->next_containing_mem;
702 *p = &dummy_val;
704 n_useless_values += n_useless_debug_values;
705 n_debug_values -= n_useless_debug_values;
706 n_useless_debug_values = 0;
708 cselib_hash_table.traverse <void *, discard_useless_values> (NULL);
710 gcc_assert (!n_useless_values);
713 /* Arrange for a value to not be removed from the hash table even if
714 it becomes useless. */
716 void
717 cselib_preserve_value (cselib_val *v)
719 PRESERVED_VALUE_P (v->val_rtx) = 1;
722 /* Test whether a value is preserved. */
724 bool
725 cselib_preserved_value_p (cselib_val *v)
727 return PRESERVED_VALUE_P (v->val_rtx);
730 /* Arrange for a REG value to be assumed constant through the whole function,
731 never invalidated and preserved across cselib_reset_table calls. */
733 void
734 cselib_preserve_cfa_base_value (cselib_val *v, unsigned int regno)
736 if (cselib_preserve_constants
737 && v->locs
738 && REG_P (v->locs->loc))
740 cfa_base_preserved_val = v;
741 cfa_base_preserved_regno = regno;
745 /* Clean all non-constant expressions in the hash table, but retain
746 their values. */
748 void
749 cselib_preserve_only_values (void)
751 int i;
753 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
754 cselib_invalidate_regno (i, reg_raw_mode[i]);
756 cselib_invalidate_mem (callmem);
758 remove_useless_values ();
760 gcc_assert (first_containing_mem == &dummy_val);
763 /* Arrange for a value to be marked as based on stack pointer
764 for find_base_term purposes. */
766 void
767 cselib_set_value_sp_based (cselib_val *v)
769 SP_BASED_VALUE_P (v->val_rtx) = 1;
772 /* Test whether a value is based on stack pointer for
773 find_base_term purposes. */
775 bool
776 cselib_sp_based_value_p (cselib_val *v)
778 return SP_BASED_VALUE_P (v->val_rtx);
781 /* Return the mode in which a register was last set. If X is not a
782 register, return its mode. If the mode in which the register was
783 set is not known, or the value was already clobbered, return
784 VOIDmode. */
786 enum machine_mode
787 cselib_reg_set_mode (const_rtx x)
789 if (!REG_P (x))
790 return GET_MODE (x);
792 if (REG_VALUES (REGNO (x)) == NULL
793 || REG_VALUES (REGNO (x))->elt == NULL)
794 return VOIDmode;
796 return GET_MODE (REG_VALUES (REGNO (x))->elt->val_rtx);
799 /* Return nonzero if we can prove that X and Y contain the same value, taking
800 our gathered information into account. */
803 rtx_equal_for_cselib_p (rtx x, rtx y)
805 return rtx_equal_for_cselib_1 (x, y, VOIDmode);
808 /* If x is a PLUS or an autoinc operation, expand the operation,
809 storing the offset, if any, in *OFF. */
811 static rtx
812 autoinc_split (rtx x, rtx *off, enum machine_mode memmode)
814 switch (GET_CODE (x))
816 case PLUS:
817 *off = XEXP (x, 1);
818 return XEXP (x, 0);
820 case PRE_DEC:
821 if (memmode == VOIDmode)
822 return x;
824 *off = GEN_INT (-GET_MODE_SIZE (memmode));
825 return XEXP (x, 0);
826 break;
828 case PRE_INC:
829 if (memmode == VOIDmode)
830 return x;
832 *off = GEN_INT (GET_MODE_SIZE (memmode));
833 return XEXP (x, 0);
835 case PRE_MODIFY:
836 return XEXP (x, 1);
838 case POST_DEC:
839 case POST_INC:
840 case POST_MODIFY:
841 return XEXP (x, 0);
843 default:
844 return x;
848 /* Return nonzero if we can prove that X and Y contain the same value,
849 taking our gathered information into account. MEMMODE holds the
850 mode of the enclosing MEM, if any, as required to deal with autoinc
851 addressing modes. If X and Y are not (known to be) part of
852 addresses, MEMMODE should be VOIDmode. */
854 static int
855 rtx_equal_for_cselib_1 (rtx x, rtx y, enum machine_mode memmode)
857 enum rtx_code code;
858 const char *fmt;
859 int i;
861 if (REG_P (x) || MEM_P (x))
863 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0, memmode);
865 if (e)
866 x = e->val_rtx;
869 if (REG_P (y) || MEM_P (y))
871 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0, memmode);
873 if (e)
874 y = e->val_rtx;
877 if (x == y)
878 return 1;
880 if (GET_CODE (x) == VALUE)
882 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (x));
883 struct elt_loc_list *l;
885 if (GET_CODE (y) == VALUE)
886 return e == canonical_cselib_val (CSELIB_VAL_PTR (y));
888 for (l = e->locs; l; l = l->next)
890 rtx t = l->loc;
892 /* Avoid infinite recursion. We know we have the canonical
893 value, so we can just skip any values in the equivalence
894 list. */
895 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
896 continue;
897 else if (rtx_equal_for_cselib_1 (t, y, memmode))
898 return 1;
901 return 0;
903 else if (GET_CODE (y) == VALUE)
905 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (y));
906 struct elt_loc_list *l;
908 for (l = e->locs; l; l = l->next)
910 rtx t = l->loc;
912 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
913 continue;
914 else if (rtx_equal_for_cselib_1 (x, t, memmode))
915 return 1;
918 return 0;
921 if (GET_MODE (x) != GET_MODE (y))
922 return 0;
924 if (GET_CODE (x) != GET_CODE (y))
926 rtx xorig = x, yorig = y;
927 rtx xoff = NULL, yoff = NULL;
929 x = autoinc_split (x, &xoff, memmode);
930 y = autoinc_split (y, &yoff, memmode);
932 if (!xoff != !yoff)
933 return 0;
935 if (xoff && !rtx_equal_for_cselib_1 (xoff, yoff, memmode))
936 return 0;
938 /* Don't recurse if nothing changed. */
939 if (x != xorig || y != yorig)
940 return rtx_equal_for_cselib_1 (x, y, memmode);
942 return 0;
945 /* These won't be handled correctly by the code below. */
946 switch (GET_CODE (x))
948 case CONST_DOUBLE:
949 case CONST_FIXED:
950 case DEBUG_EXPR:
951 return 0;
953 case DEBUG_IMPLICIT_PTR:
954 return DEBUG_IMPLICIT_PTR_DECL (x)
955 == DEBUG_IMPLICIT_PTR_DECL (y);
957 case DEBUG_PARAMETER_REF:
958 return DEBUG_PARAMETER_REF_DECL (x)
959 == DEBUG_PARAMETER_REF_DECL (y);
961 case ENTRY_VALUE:
962 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
963 use rtx_equal_for_cselib_1 to compare the operands. */
964 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
966 case LABEL_REF:
967 return XEXP (x, 0) == XEXP (y, 0);
969 case MEM:
970 /* We have to compare any autoinc operations in the addresses
971 using this MEM's mode. */
972 return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x));
974 default:
975 break;
978 code = GET_CODE (x);
979 fmt = GET_RTX_FORMAT (code);
981 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
983 int j;
985 switch (fmt[i])
987 case 'w':
988 if (XWINT (x, i) != XWINT (y, i))
989 return 0;
990 break;
992 case 'n':
993 case 'i':
994 if (XINT (x, i) != XINT (y, i))
995 return 0;
996 break;
998 case 'V':
999 case 'E':
1000 /* Two vectors must have the same length. */
1001 if (XVECLEN (x, i) != XVECLEN (y, i))
1002 return 0;
1004 /* And the corresponding elements must match. */
1005 for (j = 0; j < XVECLEN (x, i); j++)
1006 if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
1007 XVECEXP (y, i, j), memmode))
1008 return 0;
1009 break;
1011 case 'e':
1012 if (i == 1
1013 && targetm.commutative_p (x, UNKNOWN)
1014 && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode)
1015 && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode))
1016 return 1;
1017 if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode))
1018 return 0;
1019 break;
1021 case 'S':
1022 case 's':
1023 if (strcmp (XSTR (x, i), XSTR (y, i)))
1024 return 0;
1025 break;
1027 case 'u':
1028 /* These are just backpointers, so they don't matter. */
1029 break;
1031 case '0':
1032 case 't':
1033 break;
1035 /* It is believed that rtx's at this level will never
1036 contain anything but integers and other rtx's,
1037 except for within LABEL_REFs and SYMBOL_REFs. */
1038 default:
1039 gcc_unreachable ();
1042 return 1;
1045 /* We need to pass down the mode of constants through the hash table
1046 functions. For that purpose, wrap them in a CONST of the appropriate
1047 mode. */
1048 static rtx
1049 wrap_constant (enum machine_mode mode, rtx x)
1051 if (!CONST_SCALAR_INT_P (x) && GET_CODE (x) != CONST_FIXED)
1052 return x;
1053 gcc_assert (mode != VOIDmode);
1054 return gen_rtx_CONST (mode, x);
1057 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1058 For registers and memory locations, we look up their cselib_val structure
1059 and return its VALUE element.
1060 Possible reasons for return 0 are: the object is volatile, or we couldn't
1061 find a register or memory location in the table and CREATE is zero. If
1062 CREATE is nonzero, table elts are created for regs and mem.
1063 N.B. this hash function returns the same hash value for RTXes that
1064 differ only in the order of operands, thus it is suitable for comparisons
1065 that take commutativity into account.
1066 If we wanted to also support associative rules, we'd have to use a different
1067 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1068 MEMMODE indicates the mode of an enclosing MEM, and it's only
1069 used to compute autoinc values.
1070 We used to have a MODE argument for hashing for CONST_INTs, but that
1071 didn't make sense, since it caused spurious hash differences between
1072 (set (reg:SI 1) (const_int))
1073 (plus:SI (reg:SI 2) (reg:SI 1))
1075 (plus:SI (reg:SI 2) (const_int))
1076 If the mode is important in any context, it must be checked specifically
1077 in a comparison anyway, since relying on hash differences is unsafe. */
1079 static unsigned int
1080 cselib_hash_rtx (rtx x, int create, enum machine_mode memmode)
1082 cselib_val *e;
1083 int i, j;
1084 enum rtx_code code;
1085 const char *fmt;
1086 unsigned int hash = 0;
1088 code = GET_CODE (x);
1089 hash += (unsigned) code + (unsigned) GET_MODE (x);
1091 switch (code)
1093 case VALUE:
1094 e = CSELIB_VAL_PTR (x);
1095 return e->hash;
1097 case MEM:
1098 case REG:
1099 e = cselib_lookup (x, GET_MODE (x), create, memmode);
1100 if (! e)
1101 return 0;
1103 return e->hash;
1105 case DEBUG_EXPR:
1106 hash += ((unsigned) DEBUG_EXPR << 7)
1107 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
1108 return hash ? hash : (unsigned int) DEBUG_EXPR;
1110 case DEBUG_IMPLICIT_PTR:
1111 hash += ((unsigned) DEBUG_IMPLICIT_PTR << 7)
1112 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x));
1113 return hash ? hash : (unsigned int) DEBUG_IMPLICIT_PTR;
1115 case DEBUG_PARAMETER_REF:
1116 hash += ((unsigned) DEBUG_PARAMETER_REF << 7)
1117 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x));
1118 return hash ? hash : (unsigned int) DEBUG_PARAMETER_REF;
1120 case ENTRY_VALUE:
1121 /* ENTRY_VALUEs are function invariant, thus try to avoid
1122 recursing on argument if ENTRY_VALUE is one of the
1123 forms emitted by expand_debug_expr, otherwise
1124 ENTRY_VALUE hash would depend on the current value
1125 in some register or memory. */
1126 if (REG_P (ENTRY_VALUE_EXP (x)))
1127 hash += (unsigned int) REG
1128 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x))
1129 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x));
1130 else if (MEM_P (ENTRY_VALUE_EXP (x))
1131 && REG_P (XEXP (ENTRY_VALUE_EXP (x), 0)))
1132 hash += (unsigned int) MEM
1133 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x), 0))
1134 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x), 0));
1135 else
1136 hash += cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode);
1137 return hash ? hash : (unsigned int) ENTRY_VALUE;
1139 case CONST_INT:
1140 hash += ((unsigned) CONST_INT << 7) + UINTVAL (x);
1141 return hash ? hash : (unsigned int) CONST_INT;
1143 case CONST_DOUBLE:
1144 /* This is like the general case, except that it only counts
1145 the integers representing the constant. */
1146 hash += (unsigned) code + (unsigned) GET_MODE (x);
1147 if (GET_MODE (x) != VOIDmode)
1148 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
1149 else
1150 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1151 + (unsigned) CONST_DOUBLE_HIGH (x));
1152 return hash ? hash : (unsigned int) CONST_DOUBLE;
1154 case CONST_FIXED:
1155 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1156 hash += fixed_hash (CONST_FIXED_VALUE (x));
1157 return hash ? hash : (unsigned int) CONST_FIXED;
1159 case CONST_VECTOR:
1161 int units;
1162 rtx elt;
1164 units = CONST_VECTOR_NUNITS (x);
1166 for (i = 0; i < units; ++i)
1168 elt = CONST_VECTOR_ELT (x, i);
1169 hash += cselib_hash_rtx (elt, 0, memmode);
1172 return hash;
1175 /* Assume there is only one rtx object for any given label. */
1176 case LABEL_REF:
1177 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1178 differences and differences between each stage's debugging dumps. */
1179 hash += (((unsigned int) LABEL_REF << 7)
1180 + CODE_LABEL_NUMBER (XEXP (x, 0)));
1181 return hash ? hash : (unsigned int) LABEL_REF;
1183 case SYMBOL_REF:
1185 /* Don't hash on the symbol's address to avoid bootstrap differences.
1186 Different hash values may cause expressions to be recorded in
1187 different orders and thus different registers to be used in the
1188 final assembler. This also avoids differences in the dump files
1189 between various stages. */
1190 unsigned int h = 0;
1191 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1193 while (*p)
1194 h += (h << 7) + *p++; /* ??? revisit */
1196 hash += ((unsigned int) SYMBOL_REF << 7) + h;
1197 return hash ? hash : (unsigned int) SYMBOL_REF;
1200 case PRE_DEC:
1201 case PRE_INC:
1202 /* We can't compute these without knowing the MEM mode. */
1203 gcc_assert (memmode != VOIDmode);
1204 i = GET_MODE_SIZE (memmode);
1205 if (code == PRE_DEC)
1206 i = -i;
1207 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1208 like (mem:MEMMODE (plus (reg) (const_int I))). */
1209 hash += (unsigned) PLUS - (unsigned)code
1210 + cselib_hash_rtx (XEXP (x, 0), create, memmode)
1211 + cselib_hash_rtx (GEN_INT (i), create, memmode);
1212 return hash ? hash : 1 + (unsigned) PLUS;
1214 case PRE_MODIFY:
1215 gcc_assert (memmode != VOIDmode);
1216 return cselib_hash_rtx (XEXP (x, 1), create, memmode);
1218 case POST_DEC:
1219 case POST_INC:
1220 case POST_MODIFY:
1221 gcc_assert (memmode != VOIDmode);
1222 return cselib_hash_rtx (XEXP (x, 0), create, memmode);
1224 case PC:
1225 case CC0:
1226 case CALL:
1227 case UNSPEC_VOLATILE:
1228 return 0;
1230 case ASM_OPERANDS:
1231 if (MEM_VOLATILE_P (x))
1232 return 0;
1234 break;
1236 default:
1237 break;
1240 i = GET_RTX_LENGTH (code) - 1;
1241 fmt = GET_RTX_FORMAT (code);
1242 for (; i >= 0; i--)
1244 switch (fmt[i])
1246 case 'e':
1248 rtx tem = XEXP (x, i);
1249 unsigned int tem_hash = cselib_hash_rtx (tem, create, memmode);
1251 if (tem_hash == 0)
1252 return 0;
1254 hash += tem_hash;
1256 break;
1257 case 'E':
1258 for (j = 0; j < XVECLEN (x, i); j++)
1260 unsigned int tem_hash
1261 = cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
1263 if (tem_hash == 0)
1264 return 0;
1266 hash += tem_hash;
1268 break;
1270 case 's':
1272 const unsigned char *p = (const unsigned char *) XSTR (x, i);
1274 if (p)
1275 while (*p)
1276 hash += *p++;
1277 break;
1280 case 'i':
1281 hash += XINT (x, i);
1282 break;
1284 case '0':
1285 case 't':
1286 /* unused */
1287 break;
1289 default:
1290 gcc_unreachable ();
1294 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
1297 /* Create a new value structure for VALUE and initialize it. The mode of the
1298 value is MODE. */
1300 static inline cselib_val *
1301 new_cselib_val (unsigned int hash, enum machine_mode mode, rtx x)
1303 cselib_val *e = (cselib_val *) pool_alloc (cselib_val_pool);
1305 gcc_assert (hash);
1306 gcc_assert (next_uid);
1308 e->hash = hash;
1309 e->uid = next_uid++;
1310 /* We use an alloc pool to allocate this RTL construct because it
1311 accounts for about 8% of the overall memory usage. We know
1312 precisely when we can have VALUE RTXen (when cselib is active)
1313 so we don't need to put them in garbage collected memory.
1314 ??? Why should a VALUE be an RTX in the first place? */
1315 e->val_rtx = (rtx) pool_alloc (value_pool);
1316 memset (e->val_rtx, 0, RTX_HDR_SIZE);
1317 PUT_CODE (e->val_rtx, VALUE);
1318 PUT_MODE (e->val_rtx, mode);
1319 CSELIB_VAL_PTR (e->val_rtx) = e;
1320 e->addr_list = 0;
1321 e->locs = 0;
1322 e->next_containing_mem = 0;
1324 if (dump_file && (dump_flags & TDF_CSELIB))
1326 fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
1327 if (flag_dump_noaddr || flag_dump_unnumbered)
1328 fputs ("# ", dump_file);
1329 else
1330 fprintf (dump_file, "%p ", (void*)e);
1331 print_rtl_single (dump_file, x);
1332 fputc ('\n', dump_file);
1335 return e;
1338 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1339 contains the data at this address. X is a MEM that represents the
1340 value. Update the two value structures to represent this situation. */
1342 static void
1343 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
1345 struct elt_loc_list *l;
1347 addr_elt = canonical_cselib_val (addr_elt);
1348 mem_elt = canonical_cselib_val (mem_elt);
1350 /* Avoid duplicates. */
1351 for (l = mem_elt->locs; l; l = l->next)
1352 if (MEM_P (l->loc)
1353 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
1355 promote_debug_loc (l);
1356 return;
1359 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
1360 new_elt_loc_list (mem_elt,
1361 replace_equiv_address_nv (x, addr_elt->val_rtx));
1362 if (mem_elt->next_containing_mem == NULL)
1364 mem_elt->next_containing_mem = first_containing_mem;
1365 first_containing_mem = mem_elt;
1369 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1370 If CREATE, make a new one if we haven't seen it before. */
1372 static cselib_val *
1373 cselib_lookup_mem (rtx x, int create)
1375 enum machine_mode mode = GET_MODE (x);
1376 enum machine_mode addr_mode;
1377 cselib_val **slot;
1378 cselib_val *addr;
1379 cselib_val *mem_elt;
1380 struct elt_list *l;
1382 if (MEM_VOLATILE_P (x) || mode == BLKmode
1383 || !cselib_record_memory
1384 || (FLOAT_MODE_P (mode) && flag_float_store))
1385 return 0;
1387 addr_mode = GET_MODE (XEXP (x, 0));
1388 if (addr_mode == VOIDmode)
1389 addr_mode = Pmode;
1391 /* Look up the value for the address. */
1392 addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
1393 if (! addr)
1394 return 0;
1396 addr = canonical_cselib_val (addr);
1397 /* Find a value that describes a value of our mode at that address. */
1398 for (l = addr->addr_list; l; l = l->next)
1399 if (GET_MODE (l->elt->val_rtx) == mode)
1401 promote_debug_loc (l->elt->locs);
1402 return l->elt;
1405 if (! create)
1406 return 0;
1408 mem_elt = new_cselib_val (next_uid, mode, x);
1409 add_mem_for_addr (addr, mem_elt, x);
1410 slot = cselib_find_slot (wrap_constant (mode, x), mem_elt->hash,
1411 INSERT, mode);
1412 *slot = mem_elt;
1413 return mem_elt;
1416 /* Search through the possible substitutions in P. We prefer a non reg
1417 substitution because this allows us to expand the tree further. If
1418 we find, just a reg, take the lowest regno. There may be several
1419 non-reg results, we just take the first one because they will all
1420 expand to the same place. */
1422 static rtx
1423 expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1424 int max_depth)
1426 rtx reg_result = NULL;
1427 unsigned int regno = UINT_MAX;
1428 struct elt_loc_list *p_in = p;
1430 for (; p; p = p->next)
1432 /* Return these right away to avoid returning stack pointer based
1433 expressions for frame pointer and vice versa, which is something
1434 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1435 for more details. */
1436 if (REG_P (p->loc)
1437 && (REGNO (p->loc) == STACK_POINTER_REGNUM
1438 || REGNO (p->loc) == FRAME_POINTER_REGNUM
1439 || REGNO (p->loc) == HARD_FRAME_POINTER_REGNUM
1440 || REGNO (p->loc) == cfa_base_preserved_regno))
1441 return p->loc;
1442 /* Avoid infinite recursion trying to expand a reg into a
1443 the same reg. */
1444 if ((REG_P (p->loc))
1445 && (REGNO (p->loc) < regno)
1446 && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
1448 reg_result = p->loc;
1449 regno = REGNO (p->loc);
1451 /* Avoid infinite recursion and do not try to expand the
1452 value. */
1453 else if (GET_CODE (p->loc) == VALUE
1454 && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1455 continue;
1456 else if (!REG_P (p->loc))
1458 rtx result, note;
1459 if (dump_file && (dump_flags & TDF_CSELIB))
1461 print_inline_rtx (dump_file, p->loc, 0);
1462 fprintf (dump_file, "\n");
1464 if (GET_CODE (p->loc) == LO_SUM
1465 && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1466 && p->setting_insn
1467 && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1468 && XEXP (note, 0) == XEXP (p->loc, 1))
1469 return XEXP (p->loc, 1);
1470 result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
1471 if (result)
1472 return result;
1477 if (regno != UINT_MAX)
1479 rtx result;
1480 if (dump_file && (dump_flags & TDF_CSELIB))
1481 fprintf (dump_file, "r%d\n", regno);
1483 result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
1484 if (result)
1485 return result;
1488 if (dump_file && (dump_flags & TDF_CSELIB))
1490 if (reg_result)
1492 print_inline_rtx (dump_file, reg_result, 0);
1493 fprintf (dump_file, "\n");
1495 else
1496 fprintf (dump_file, "NULL\n");
1498 return reg_result;
1502 /* Forward substitute and expand an expression out to its roots.
1503 This is the opposite of common subexpression. Because local value
1504 numbering is such a weak optimization, the expanded expression is
1505 pretty much unique (not from a pointer equals point of view but
1506 from a tree shape point of view.
1508 This function returns NULL if the expansion fails. The expansion
1509 will fail if there is no value number for one of the operands or if
1510 one of the operands has been overwritten between the current insn
1511 and the beginning of the basic block. For instance x has no
1512 expansion in:
1514 r1 <- r1 + 3
1515 x <- r1 + 8
1517 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1518 It is clear on return. */
1521 cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
1523 struct expand_value_data evd;
1525 evd.regs_active = regs_active;
1526 evd.callback = NULL;
1527 evd.callback_arg = NULL;
1528 evd.dummy = false;
1530 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1533 /* Same as cselib_expand_value_rtx, but using a callback to try to
1534 resolve some expressions. The CB function should return ORIG if it
1535 can't or does not want to deal with a certain RTX. Any other
1536 return value, including NULL, will be used as the expansion for
1537 VALUE, without any further changes. */
1540 cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1541 cselib_expand_callback cb, void *data)
1543 struct expand_value_data evd;
1545 evd.regs_active = regs_active;
1546 evd.callback = cb;
1547 evd.callback_arg = data;
1548 evd.dummy = false;
1550 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1553 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1554 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1555 would return NULL or non-NULL, without allocating new rtx. */
1557 bool
1558 cselib_dummy_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 = true;
1568 return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1571 /* Internal implementation of cselib_expand_value_rtx and
1572 cselib_expand_value_rtx_cb. */
1574 static rtx
1575 cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1576 int max_depth)
1578 rtx copy, scopy;
1579 int i, j;
1580 RTX_CODE code;
1581 const char *format_ptr;
1582 enum machine_mode mode;
1584 code = GET_CODE (orig);
1586 /* For the context of dse, if we end up expand into a huge tree, we
1587 will not have a useful address, so we might as well just give up
1588 quickly. */
1589 if (max_depth <= 0)
1590 return NULL;
1592 switch (code)
1594 case REG:
1596 struct elt_list *l = REG_VALUES (REGNO (orig));
1598 if (l && l->elt == NULL)
1599 l = l->next;
1600 for (; l; l = l->next)
1601 if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1603 rtx result;
1604 unsigned regno = REGNO (orig);
1606 /* The only thing that we are not willing to do (this
1607 is requirement of dse and if others potential uses
1608 need this function we should add a parm to control
1609 it) is that we will not substitute the
1610 STACK_POINTER_REGNUM, FRAME_POINTER or the
1611 HARD_FRAME_POINTER.
1613 These expansions confuses the code that notices that
1614 stores into the frame go dead at the end of the
1615 function and that the frame is not effected by calls
1616 to subroutines. If you allow the
1617 STACK_POINTER_REGNUM substitution, then dse will
1618 think that parameter pushing also goes dead which is
1619 wrong. If you allow the FRAME_POINTER or the
1620 HARD_FRAME_POINTER then you lose the opportunity to
1621 make the frame assumptions. */
1622 if (regno == STACK_POINTER_REGNUM
1623 || regno == FRAME_POINTER_REGNUM
1624 || regno == HARD_FRAME_POINTER_REGNUM
1625 || regno == cfa_base_preserved_regno)
1626 return orig;
1628 bitmap_set_bit (evd->regs_active, regno);
1630 if (dump_file && (dump_flags & TDF_CSELIB))
1631 fprintf (dump_file, "expanding: r%d into: ", regno);
1633 result = expand_loc (l->elt->locs, evd, max_depth);
1634 bitmap_clear_bit (evd->regs_active, regno);
1636 if (result)
1637 return result;
1638 else
1639 return orig;
1643 CASE_CONST_ANY:
1644 case SYMBOL_REF:
1645 case CODE_LABEL:
1646 case PC:
1647 case CC0:
1648 case SCRATCH:
1649 /* SCRATCH must be shared because they represent distinct values. */
1650 return orig;
1651 case CLOBBER:
1652 if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1653 return orig;
1654 break;
1656 case CONST:
1657 if (shared_const_p (orig))
1658 return orig;
1659 break;
1661 case SUBREG:
1663 rtx subreg;
1665 if (evd->callback)
1667 subreg = evd->callback (orig, evd->regs_active, max_depth,
1668 evd->callback_arg);
1669 if (subreg != orig)
1670 return subreg;
1673 subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1674 max_depth - 1);
1675 if (!subreg)
1676 return NULL;
1677 scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1678 GET_MODE (SUBREG_REG (orig)),
1679 SUBREG_BYTE (orig));
1680 if (scopy == NULL
1681 || (GET_CODE (scopy) == SUBREG
1682 && !REG_P (SUBREG_REG (scopy))
1683 && !MEM_P (SUBREG_REG (scopy))))
1684 return NULL;
1686 return scopy;
1689 case VALUE:
1691 rtx result;
1693 if (dump_file && (dump_flags & TDF_CSELIB))
1695 fputs ("\nexpanding ", dump_file);
1696 print_rtl_single (dump_file, orig);
1697 fputs (" into...", dump_file);
1700 if (evd->callback)
1702 result = evd->callback (orig, evd->regs_active, max_depth,
1703 evd->callback_arg);
1705 if (result != orig)
1706 return result;
1709 result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
1710 return result;
1713 case DEBUG_EXPR:
1714 if (evd->callback)
1715 return evd->callback (orig, evd->regs_active, max_depth,
1716 evd->callback_arg);
1717 return orig;
1719 default:
1720 break;
1723 /* Copy the various flags, fields, and other information. We assume
1724 that all fields need copying, and then clear the fields that should
1725 not be copied. That is the sensible default behavior, and forces
1726 us to explicitly document why we are *not* copying a flag. */
1727 if (evd->dummy)
1728 copy = NULL;
1729 else
1730 copy = shallow_copy_rtx (orig);
1732 format_ptr = GET_RTX_FORMAT (code);
1734 for (i = 0; i < GET_RTX_LENGTH (code); i++)
1735 switch (*format_ptr++)
1737 case 'e':
1738 if (XEXP (orig, i) != NULL)
1740 rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1741 max_depth - 1);
1742 if (!result)
1743 return NULL;
1744 if (copy)
1745 XEXP (copy, i) = result;
1747 break;
1749 case 'E':
1750 case 'V':
1751 if (XVEC (orig, i) != NULL)
1753 if (copy)
1754 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1755 for (j = 0; j < XVECLEN (orig, i); j++)
1757 rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1758 evd, max_depth - 1);
1759 if (!result)
1760 return NULL;
1761 if (copy)
1762 XVECEXP (copy, i, j) = result;
1765 break;
1767 case 't':
1768 case 'w':
1769 case 'i':
1770 case 's':
1771 case 'S':
1772 case 'T':
1773 case 'u':
1774 case 'B':
1775 case '0':
1776 /* These are left unchanged. */
1777 break;
1779 default:
1780 gcc_unreachable ();
1783 if (evd->dummy)
1784 return orig;
1786 mode = GET_MODE (copy);
1787 /* If an operand has been simplified into CONST_INT, which doesn't
1788 have a mode and the mode isn't derivable from whole rtx's mode,
1789 try simplify_*_operation first with mode from original's operand
1790 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1791 scopy = copy;
1792 switch (GET_RTX_CLASS (code))
1794 case RTX_UNARY:
1795 if (CONST_INT_P (XEXP (copy, 0))
1796 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1798 scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1799 GET_MODE (XEXP (orig, 0)));
1800 if (scopy)
1801 return scopy;
1803 break;
1804 case RTX_COMM_ARITH:
1805 case RTX_BIN_ARITH:
1806 /* These expressions can derive operand modes from the whole rtx's mode. */
1807 break;
1808 case RTX_TERNARY:
1809 case RTX_BITFIELD_OPS:
1810 if (CONST_INT_P (XEXP (copy, 0))
1811 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1813 scopy = simplify_ternary_operation (code, mode,
1814 GET_MODE (XEXP (orig, 0)),
1815 XEXP (copy, 0), XEXP (copy, 1),
1816 XEXP (copy, 2));
1817 if (scopy)
1818 return scopy;
1820 break;
1821 case RTX_COMPARE:
1822 case RTX_COMM_COMPARE:
1823 if (CONST_INT_P (XEXP (copy, 0))
1824 && GET_MODE (XEXP (copy, 1)) == VOIDmode
1825 && (GET_MODE (XEXP (orig, 0)) != VOIDmode
1826 || GET_MODE (XEXP (orig, 1)) != VOIDmode))
1828 scopy = simplify_relational_operation (code, mode,
1829 (GET_MODE (XEXP (orig, 0))
1830 != VOIDmode)
1831 ? GET_MODE (XEXP (orig, 0))
1832 : GET_MODE (XEXP (orig, 1)),
1833 XEXP (copy, 0),
1834 XEXP (copy, 1));
1835 if (scopy)
1836 return scopy;
1838 break;
1839 default:
1840 break;
1842 scopy = simplify_rtx (copy);
1843 if (scopy)
1844 return scopy;
1845 return copy;
1848 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1849 with VALUE expressions. This way, it becomes independent of changes
1850 to registers and memory.
1851 X isn't actually modified; if modifications are needed, new rtl is
1852 allocated. However, the return value can share rtl with X.
1853 If X is within a MEM, MEMMODE must be the mode of the MEM. */
1856 cselib_subst_to_values (rtx x, enum machine_mode memmode)
1858 enum rtx_code code = GET_CODE (x);
1859 const char *fmt = GET_RTX_FORMAT (code);
1860 cselib_val *e;
1861 struct elt_list *l;
1862 rtx copy = x;
1863 int i;
1865 switch (code)
1867 case REG:
1868 l = REG_VALUES (REGNO (x));
1869 if (l && l->elt == NULL)
1870 l = l->next;
1871 for (; l; l = l->next)
1872 if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
1873 return l->elt->val_rtx;
1875 gcc_unreachable ();
1877 case MEM:
1878 e = cselib_lookup_mem (x, 0);
1879 /* This used to happen for autoincrements, but we deal with them
1880 properly now. Remove the if stmt for the next release. */
1881 if (! e)
1883 /* Assign a value that doesn't match any other. */
1884 e = new_cselib_val (next_uid, GET_MODE (x), x);
1886 return e->val_rtx;
1888 case ENTRY_VALUE:
1889 e = cselib_lookup (x, GET_MODE (x), 0, memmode);
1890 if (! e)
1891 break;
1892 return e->val_rtx;
1894 CASE_CONST_ANY:
1895 return x;
1897 case PRE_DEC:
1898 case PRE_INC:
1899 gcc_assert (memmode != VOIDmode);
1900 i = GET_MODE_SIZE (memmode);
1901 if (code == PRE_DEC)
1902 i = -i;
1903 return cselib_subst_to_values (plus_constant (GET_MODE (x),
1904 XEXP (x, 0), i),
1905 memmode);
1907 case PRE_MODIFY:
1908 gcc_assert (memmode != VOIDmode);
1909 return cselib_subst_to_values (XEXP (x, 1), memmode);
1911 case POST_DEC:
1912 case POST_INC:
1913 case POST_MODIFY:
1914 gcc_assert (memmode != VOIDmode);
1915 return cselib_subst_to_values (XEXP (x, 0), memmode);
1917 default:
1918 break;
1921 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1923 if (fmt[i] == 'e')
1925 rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
1927 if (t != XEXP (x, i))
1929 if (x == copy)
1930 copy = shallow_copy_rtx (x);
1931 XEXP (copy, i) = t;
1934 else if (fmt[i] == 'E')
1936 int j;
1938 for (j = 0; j < XVECLEN (x, i); j++)
1940 rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
1942 if (t != XVECEXP (x, i, j))
1944 if (XVEC (x, i) == XVEC (copy, i))
1946 if (x == copy)
1947 copy = shallow_copy_rtx (x);
1948 XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
1950 XVECEXP (copy, i, j) = t;
1956 return copy;
1959 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
1962 cselib_subst_to_values_from_insn (rtx x, enum machine_mode memmode, rtx insn)
1964 rtx ret;
1965 gcc_assert (!cselib_current_insn);
1966 cselib_current_insn = insn;
1967 ret = cselib_subst_to_values (x, memmode);
1968 cselib_current_insn = NULL;
1969 return ret;
1972 /* Look up the rtl expression X in our tables and return the value it
1973 has. If CREATE is zero, we return NULL if we don't know the value.
1974 Otherwise, we create a new one if possible, using mode MODE if X
1975 doesn't have a mode (i.e. because it's a constant). When X is part
1976 of an address, MEMMODE should be the mode of the enclosing MEM if
1977 we're tracking autoinc expressions. */
1979 static cselib_val *
1980 cselib_lookup_1 (rtx x, enum machine_mode mode,
1981 int create, enum machine_mode memmode)
1983 cselib_val **slot;
1984 cselib_val *e;
1985 unsigned int hashval;
1987 if (GET_MODE (x) != VOIDmode)
1988 mode = GET_MODE (x);
1990 if (GET_CODE (x) == VALUE)
1991 return CSELIB_VAL_PTR (x);
1993 if (REG_P (x))
1995 struct elt_list *l;
1996 unsigned int i = REGNO (x);
1998 l = REG_VALUES (i);
1999 if (l && l->elt == NULL)
2000 l = l->next;
2001 for (; l; l = l->next)
2002 if (mode == GET_MODE (l->elt->val_rtx))
2004 promote_debug_loc (l->elt->locs);
2005 return l->elt;
2008 if (! create)
2009 return 0;
2011 if (i < FIRST_PSEUDO_REGISTER)
2013 unsigned int n = hard_regno_nregs[i][mode];
2015 if (n > max_value_regs)
2016 max_value_regs = n;
2019 e = new_cselib_val (next_uid, GET_MODE (x), x);
2020 new_elt_loc_list (e, x);
2021 if (REG_VALUES (i) == 0)
2023 /* Maintain the invariant that the first entry of
2024 REG_VALUES, if present, must be the value used to set the
2025 register, or NULL. */
2026 used_regs[n_used_regs++] = i;
2027 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
2029 else if (cselib_preserve_constants
2030 && GET_MODE_CLASS (mode) == MODE_INT)
2032 /* During var-tracking, try harder to find equivalences
2033 for SUBREGs. If a setter sets say a DImode register
2034 and user uses that register only in SImode, add a lowpart
2035 subreg location. */
2036 struct elt_list *lwider = NULL;
2037 l = REG_VALUES (i);
2038 if (l && l->elt == NULL)
2039 l = l->next;
2040 for (; l; l = l->next)
2041 if (GET_MODE_CLASS (GET_MODE (l->elt->val_rtx)) == MODE_INT
2042 && GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2043 > GET_MODE_SIZE (mode)
2044 && (lwider == NULL
2045 || GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2046 < GET_MODE_SIZE (GET_MODE (lwider->elt->val_rtx))))
2048 struct elt_loc_list *el;
2049 if (i < FIRST_PSEUDO_REGISTER
2050 && hard_regno_nregs[i][GET_MODE (l->elt->val_rtx)] != 1)
2051 continue;
2052 for (el = l->elt->locs; el; el = el->next)
2053 if (!REG_P (el->loc))
2054 break;
2055 if (el)
2056 lwider = l;
2058 if (lwider)
2060 rtx sub = lowpart_subreg (mode, lwider->elt->val_rtx,
2061 GET_MODE (lwider->elt->val_rtx));
2062 if (sub)
2063 new_elt_loc_list (e, sub);
2066 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
2067 slot = cselib_find_slot (x, e->hash, INSERT, memmode);
2068 *slot = e;
2069 return e;
2072 if (MEM_P (x))
2073 return cselib_lookup_mem (x, create);
2075 hashval = cselib_hash_rtx (x, create, memmode);
2076 /* Can't even create if hashing is not possible. */
2077 if (! hashval)
2078 return 0;
2080 slot = cselib_find_slot (wrap_constant (mode, x), hashval,
2081 create ? INSERT : NO_INSERT, memmode);
2082 if (slot == 0)
2083 return 0;
2085 e = (cselib_val *) *slot;
2086 if (e)
2087 return e;
2089 e = new_cselib_val (hashval, mode, x);
2091 /* We have to fill the slot before calling cselib_subst_to_values:
2092 the hash table is inconsistent until we do so, and
2093 cselib_subst_to_values will need to do lookups. */
2094 *slot = e;
2095 new_elt_loc_list (e, cselib_subst_to_values (x, memmode));
2096 return e;
2099 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2101 cselib_val *
2102 cselib_lookup_from_insn (rtx x, enum machine_mode mode,
2103 int create, enum machine_mode memmode, rtx insn)
2105 cselib_val *ret;
2107 gcc_assert (!cselib_current_insn);
2108 cselib_current_insn = insn;
2110 ret = cselib_lookup (x, mode, create, memmode);
2112 cselib_current_insn = NULL;
2114 return ret;
2117 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2118 maintains invariants related with debug insns. */
2120 cselib_val *
2121 cselib_lookup (rtx x, enum machine_mode mode,
2122 int create, enum machine_mode memmode)
2124 cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
2126 /* ??? Should we return NULL if we're not to create an entry, the
2127 found loc is a debug loc and cselib_current_insn is not DEBUG?
2128 If so, we should also avoid converting val to non-DEBUG; probably
2129 easiest setting cselib_current_insn to NULL before the call
2130 above. */
2132 if (dump_file && (dump_flags & TDF_CSELIB))
2134 fputs ("cselib lookup ", dump_file);
2135 print_inline_rtx (dump_file, x, 2);
2136 fprintf (dump_file, " => %u:%u\n",
2137 ret ? ret->uid : 0,
2138 ret ? ret->hash : 0);
2141 return ret;
2144 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2145 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2146 is used to determine how many hard registers are being changed. If MODE
2147 is VOIDmode, then only REGNO is being changed; this is used when
2148 invalidating call clobbered registers across a call. */
2150 static void
2151 cselib_invalidate_regno (unsigned int regno, enum machine_mode mode)
2153 unsigned int endregno;
2154 unsigned int i;
2156 /* If we see pseudos after reload, something is _wrong_. */
2157 gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
2158 || reg_renumber[regno] < 0);
2160 /* Determine the range of registers that must be invalidated. For
2161 pseudos, only REGNO is affected. For hard regs, we must take MODE
2162 into account, and we must also invalidate lower register numbers
2163 if they contain values that overlap REGNO. */
2164 if (regno < FIRST_PSEUDO_REGISTER)
2166 gcc_assert (mode != VOIDmode);
2168 if (regno < max_value_regs)
2169 i = 0;
2170 else
2171 i = regno - max_value_regs;
2173 endregno = end_hard_regno (mode, regno);
2175 else
2177 i = regno;
2178 endregno = regno + 1;
2181 for (; i < endregno; i++)
2183 struct elt_list **l = &REG_VALUES (i);
2185 /* Go through all known values for this reg; if it overlaps the range
2186 we're invalidating, remove the value. */
2187 while (*l)
2189 cselib_val *v = (*l)->elt;
2190 bool had_locs;
2191 rtx setting_insn;
2192 struct elt_loc_list **p;
2193 unsigned int this_last = i;
2195 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
2196 this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
2198 if (this_last < regno || v == NULL
2199 || (v == cfa_base_preserved_val
2200 && i == cfa_base_preserved_regno))
2202 l = &(*l)->next;
2203 continue;
2206 /* We have an overlap. */
2207 if (*l == REG_VALUES (i))
2209 /* Maintain the invariant that the first entry of
2210 REG_VALUES, if present, must be the value used to set
2211 the register, or NULL. This is also nice because
2212 then we won't push the same regno onto user_regs
2213 multiple times. */
2214 (*l)->elt = NULL;
2215 l = &(*l)->next;
2217 else
2218 unchain_one_elt_list (l);
2220 v = canonical_cselib_val (v);
2222 had_locs = v->locs != NULL;
2223 setting_insn = v->locs ? v->locs->setting_insn : NULL;
2225 /* Now, we clear the mapping from value to reg. It must exist, so
2226 this code will crash intentionally if it doesn't. */
2227 for (p = &v->locs; ; p = &(*p)->next)
2229 rtx x = (*p)->loc;
2231 if (REG_P (x) && REGNO (x) == i)
2233 unchain_one_elt_loc_list (p);
2234 break;
2238 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2240 if (setting_insn && DEBUG_INSN_P (setting_insn))
2241 n_useless_debug_values++;
2242 else
2243 n_useless_values++;
2249 /* Invalidate any locations in the table which are changed because of a
2250 store to MEM_RTX. If this is called because of a non-const call
2251 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2253 static void
2254 cselib_invalidate_mem (rtx mem_rtx)
2256 cselib_val **vp, *v, *next;
2257 int num_mems = 0;
2258 rtx mem_addr;
2260 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2261 mem_rtx = canon_rtx (mem_rtx);
2263 vp = &first_containing_mem;
2264 for (v = *vp; v != &dummy_val; v = next)
2266 bool has_mem = false;
2267 struct elt_loc_list **p = &v->locs;
2268 bool had_locs = v->locs != NULL;
2269 rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
2271 while (*p)
2273 rtx x = (*p)->loc;
2274 cselib_val *addr;
2275 struct elt_list **mem_chain;
2277 /* MEMs may occur in locations only at the top level; below
2278 that every MEM or REG is substituted by its VALUE. */
2279 if (!MEM_P (x))
2281 p = &(*p)->next;
2282 continue;
2284 if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
2285 && ! canon_anti_dependence (x, false, mem_rtx,
2286 GET_MODE (mem_rtx), mem_addr))
2288 has_mem = true;
2289 num_mems++;
2290 p = &(*p)->next;
2291 continue;
2294 /* This one overlaps. */
2295 /* We must have a mapping from this MEM's address to the
2296 value (E). Remove that, too. */
2297 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
2298 addr = canonical_cselib_val (addr);
2299 gcc_checking_assert (v == canonical_cselib_val (v));
2300 mem_chain = &addr->addr_list;
2301 for (;;)
2303 cselib_val *canon = canonical_cselib_val ((*mem_chain)->elt);
2305 if (canon == v)
2307 unchain_one_elt_list (mem_chain);
2308 break;
2311 /* Record canonicalized elt. */
2312 (*mem_chain)->elt = canon;
2314 mem_chain = &(*mem_chain)->next;
2317 unchain_one_elt_loc_list (p);
2320 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2322 if (setting_insn && DEBUG_INSN_P (setting_insn))
2323 n_useless_debug_values++;
2324 else
2325 n_useless_values++;
2328 next = v->next_containing_mem;
2329 if (has_mem)
2331 *vp = v;
2332 vp = &(*vp)->next_containing_mem;
2334 else
2335 v->next_containing_mem = NULL;
2337 *vp = &dummy_val;
2340 /* Invalidate DEST, which is being assigned to or clobbered. */
2342 void
2343 cselib_invalidate_rtx (rtx dest)
2345 while (GET_CODE (dest) == SUBREG
2346 || GET_CODE (dest) == ZERO_EXTRACT
2347 || GET_CODE (dest) == STRICT_LOW_PART)
2348 dest = XEXP (dest, 0);
2350 if (REG_P (dest))
2351 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
2352 else if (MEM_P (dest))
2353 cselib_invalidate_mem (dest);
2356 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2358 static void
2359 cselib_invalidate_rtx_note_stores (rtx dest, const_rtx ignore ATTRIBUTE_UNUSED,
2360 void *data ATTRIBUTE_UNUSED)
2362 cselib_invalidate_rtx (dest);
2365 /* Record the result of a SET instruction. DEST is being set; the source
2366 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2367 describes its address. */
2369 static void
2370 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
2372 int dreg = REG_P (dest) ? (int) REGNO (dest) : -1;
2374 if (src_elt == 0 || side_effects_p (dest))
2375 return;
2377 if (dreg >= 0)
2379 if (dreg < FIRST_PSEUDO_REGISTER)
2381 unsigned int n = hard_regno_nregs[dreg][GET_MODE (dest)];
2383 if (n > max_value_regs)
2384 max_value_regs = n;
2387 if (REG_VALUES (dreg) == 0)
2389 used_regs[n_used_regs++] = dreg;
2390 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2392 else
2394 /* The register should have been invalidated. */
2395 gcc_assert (REG_VALUES (dreg)->elt == 0);
2396 REG_VALUES (dreg)->elt = src_elt;
2399 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2400 n_useless_values--;
2401 new_elt_loc_list (src_elt, dest);
2403 else if (MEM_P (dest) && dest_addr_elt != 0
2404 && cselib_record_memory)
2406 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2407 n_useless_values--;
2408 add_mem_for_addr (dest_addr_elt, src_elt, dest);
2412 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2414 void
2415 cselib_add_permanent_equiv (cselib_val *elt, rtx x, rtx insn)
2417 cselib_val *nelt;
2418 rtx save_cselib_current_insn = cselib_current_insn;
2420 gcc_checking_assert (elt);
2421 gcc_checking_assert (PRESERVED_VALUE_P (elt->val_rtx));
2422 gcc_checking_assert (!side_effects_p (x));
2424 cselib_current_insn = insn;
2426 nelt = cselib_lookup (x, GET_MODE (elt->val_rtx), 1, VOIDmode);
2428 if (nelt != elt)
2430 cselib_any_perm_equivs = true;
2432 if (!PRESERVED_VALUE_P (nelt->val_rtx))
2433 cselib_preserve_value (nelt);
2435 new_elt_loc_list (nelt, elt->val_rtx);
2438 cselib_current_insn = save_cselib_current_insn;
2441 /* Return TRUE if any permanent equivalences have been recorded since
2442 the table was last initialized. */
2443 bool
2444 cselib_have_permanent_equivalences (void)
2446 return cselib_any_perm_equivs;
2449 /* There is no good way to determine how many elements there can be
2450 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2451 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2453 struct cselib_record_autoinc_data
2455 struct cselib_set *sets;
2456 int n_sets;
2459 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2460 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2462 static int
2463 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
2464 rtx dest, rtx src, rtx srcoff, void *arg)
2466 struct cselib_record_autoinc_data *data;
2467 data = (struct cselib_record_autoinc_data *)arg;
2469 data->sets[data->n_sets].dest = dest;
2471 if (srcoff)
2472 data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
2473 else
2474 data->sets[data->n_sets].src = src;
2476 data->n_sets++;
2478 return -1;
2481 /* Record the effects of any sets and autoincs in INSN. */
2482 static void
2483 cselib_record_sets (rtx insn)
2485 int n_sets = 0;
2486 int i;
2487 struct cselib_set sets[MAX_SETS];
2488 rtx body = PATTERN (insn);
2489 rtx cond = 0;
2490 int n_sets_before_autoinc;
2491 struct cselib_record_autoinc_data data;
2493 body = PATTERN (insn);
2494 if (GET_CODE (body) == COND_EXEC)
2496 cond = COND_EXEC_TEST (body);
2497 body = COND_EXEC_CODE (body);
2500 /* Find all sets. */
2501 if (GET_CODE (body) == SET)
2503 sets[0].src = SET_SRC (body);
2504 sets[0].dest = SET_DEST (body);
2505 n_sets = 1;
2507 else if (GET_CODE (body) == PARALLEL)
2509 /* Look through the PARALLEL and record the values being
2510 set, if possible. Also handle any CLOBBERs. */
2511 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2513 rtx x = XVECEXP (body, 0, i);
2515 if (GET_CODE (x) == SET)
2517 sets[n_sets].src = SET_SRC (x);
2518 sets[n_sets].dest = SET_DEST (x);
2519 n_sets++;
2524 if (n_sets == 1
2525 && MEM_P (sets[0].src)
2526 && !cselib_record_memory
2527 && MEM_READONLY_P (sets[0].src))
2529 rtx note = find_reg_equal_equiv_note (insn);
2531 if (note && CONSTANT_P (XEXP (note, 0)))
2532 sets[0].src = XEXP (note, 0);
2535 data.sets = sets;
2536 data.n_sets = n_sets_before_autoinc = n_sets;
2537 for_each_inc_dec (&insn, cselib_record_autoinc_cb, &data);
2538 n_sets = data.n_sets;
2540 /* Look up the values that are read. Do this before invalidating the
2541 locations that are written. */
2542 for (i = 0; i < n_sets; i++)
2544 rtx dest = sets[i].dest;
2546 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2547 the low part after invalidating any knowledge about larger modes. */
2548 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2549 sets[i].dest = dest = XEXP (dest, 0);
2551 /* We don't know how to record anything but REG or MEM. */
2552 if (REG_P (dest)
2553 || (MEM_P (dest) && cselib_record_memory))
2555 rtx src = sets[i].src;
2556 if (cond)
2557 src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
2558 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
2559 if (MEM_P (dest))
2561 enum machine_mode address_mode = get_address_mode (dest);
2563 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
2564 address_mode, 1,
2565 GET_MODE (dest));
2567 else
2568 sets[i].dest_addr_elt = 0;
2572 if (cselib_record_sets_hook)
2573 cselib_record_sets_hook (insn, sets, n_sets);
2575 /* Invalidate all locations written by this insn. Note that the elts we
2576 looked up in the previous loop aren't affected, just some of their
2577 locations may go away. */
2578 note_stores (body, cselib_invalidate_rtx_note_stores, NULL);
2580 for (i = n_sets_before_autoinc; i < n_sets; i++)
2581 cselib_invalidate_rtx (sets[i].dest);
2583 /* If this is an asm, look for duplicate sets. This can happen when the
2584 user uses the same value as an output multiple times. This is valid
2585 if the outputs are not actually used thereafter. Treat this case as
2586 if the value isn't actually set. We do this by smashing the destination
2587 to pc_rtx, so that we won't record the value later. */
2588 if (n_sets >= 2 && asm_noperands (body) >= 0)
2590 for (i = 0; i < n_sets; i++)
2592 rtx dest = sets[i].dest;
2593 if (REG_P (dest) || MEM_P (dest))
2595 int j;
2596 for (j = i + 1; j < n_sets; j++)
2597 if (rtx_equal_p (dest, sets[j].dest))
2599 sets[i].dest = pc_rtx;
2600 sets[j].dest = pc_rtx;
2606 /* Now enter the equivalences in our tables. */
2607 for (i = 0; i < n_sets; i++)
2609 rtx dest = sets[i].dest;
2610 if (REG_P (dest)
2611 || (MEM_P (dest) && cselib_record_memory))
2612 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2616 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
2618 bool
2619 fp_setter_insn (rtx insn)
2621 rtx expr, pat = NULL_RTX;
2623 if (!RTX_FRAME_RELATED_P (insn))
2624 return false;
2626 expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2627 if (expr)
2628 pat = XEXP (expr, 0);
2629 if (!modified_in_p (hard_frame_pointer_rtx, pat ? pat : insn))
2630 return false;
2632 /* Don't return true for frame pointer restores in the epilogue. */
2633 if (find_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx))
2634 return false;
2635 return true;
2638 /* Record the effects of INSN. */
2640 void
2641 cselib_process_insn (rtx insn)
2643 int i;
2644 rtx x;
2646 cselib_current_insn = insn;
2648 /* Forget everything at a CODE_LABEL or a setjmp. */
2649 if ((LABEL_P (insn)
2650 || (CALL_P (insn)
2651 && find_reg_note (insn, REG_SETJMP, NULL)))
2652 && !cselib_preserve_constants)
2654 cselib_reset_table (next_uid);
2655 cselib_current_insn = NULL_RTX;
2656 return;
2659 if (! INSN_P (insn))
2661 cselib_current_insn = NULL_RTX;
2662 return;
2665 /* If this is a call instruction, forget anything stored in a
2666 call clobbered register, or, if this is not a const call, in
2667 memory. */
2668 if (CALL_P (insn))
2670 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2671 if (call_used_regs[i]
2672 || (REG_VALUES (i) && REG_VALUES (i)->elt
2673 && HARD_REGNO_CALL_PART_CLOBBERED (i,
2674 GET_MODE (REG_VALUES (i)->elt->val_rtx))))
2675 cselib_invalidate_regno (i, reg_raw_mode[i]);
2677 /* Since it is not clear how cselib is going to be used, be
2678 conservative here and treat looping pure or const functions
2679 as if they were regular functions. */
2680 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
2681 || !(RTL_CONST_OR_PURE_CALL_P (insn)))
2682 cselib_invalidate_mem (callmem);
2685 cselib_record_sets (insn);
2687 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2688 after we have processed the insn. */
2689 if (CALL_P (insn))
2691 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
2692 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
2693 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
2694 /* Flush evertything on setjmp. */
2695 if (cselib_preserve_constants
2696 && find_reg_note (insn, REG_SETJMP, NULL))
2698 cselib_preserve_only_values ();
2699 cselib_reset_table (next_uid);
2703 /* On setter of the hard frame pointer if frame_pointer_needed,
2704 invalidate stack_pointer_rtx, so that sp and {,h}fp based
2705 VALUEs are distinct. */
2706 if (reload_completed
2707 && frame_pointer_needed
2708 && fp_setter_insn (insn))
2709 cselib_invalidate_rtx (stack_pointer_rtx);
2711 cselib_current_insn = NULL_RTX;
2713 if (n_useless_values > MAX_USELESS_VALUES
2714 /* remove_useless_values is linear in the hash table size. Avoid
2715 quadratic behavior for very large hashtables with very few
2716 useless elements. */
2717 && ((unsigned int)n_useless_values
2718 > (cselib_hash_table.elements () - n_debug_values) / 4))
2719 remove_useless_values ();
2722 /* Initialize cselib for one pass. The caller must also call
2723 init_alias_analysis. */
2725 void
2726 cselib_init (int record_what)
2728 elt_list_pool = create_alloc_pool ("elt_list",
2729 sizeof (struct elt_list), 10);
2730 elt_loc_list_pool = create_alloc_pool ("elt_loc_list",
2731 sizeof (struct elt_loc_list), 10);
2732 cselib_val_pool = create_alloc_pool ("cselib_val_list",
2733 sizeof (cselib_val), 10);
2734 value_pool = create_alloc_pool ("value", RTX_CODE_SIZE (VALUE), 100);
2735 cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
2736 cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
2737 cselib_any_perm_equivs = false;
2739 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2740 see canon_true_dependence. This is only created once. */
2741 if (! callmem)
2742 callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2744 cselib_nregs = max_reg_num ();
2746 /* We preserve reg_values to allow expensive clearing of the whole thing.
2747 Reallocate it however if it happens to be too large. */
2748 if (!reg_values || reg_values_size < cselib_nregs
2749 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
2751 free (reg_values);
2752 /* Some space for newly emit instructions so we don't end up
2753 reallocating in between passes. */
2754 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
2755 reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
2757 used_regs = XNEWVEC (unsigned int, cselib_nregs);
2758 n_used_regs = 0;
2759 cselib_hash_table.create (31);
2760 if (cselib_preserve_constants)
2761 cselib_preserved_hash_table.create (31);
2762 next_uid = 1;
2765 /* Called when the current user is done with cselib. */
2767 void
2768 cselib_finish (void)
2770 bool preserved = cselib_preserve_constants;
2771 cselib_discard_hook = NULL;
2772 cselib_preserve_constants = false;
2773 cselib_any_perm_equivs = false;
2774 cfa_base_preserved_val = NULL;
2775 cfa_base_preserved_regno = INVALID_REGNUM;
2776 free_alloc_pool (elt_list_pool);
2777 free_alloc_pool (elt_loc_list_pool);
2778 free_alloc_pool (cselib_val_pool);
2779 free_alloc_pool (value_pool);
2780 cselib_clear_table ();
2781 cselib_hash_table.dispose ();
2782 if (preserved)
2783 cselib_preserved_hash_table.dispose ();
2784 free (used_regs);
2785 used_regs = 0;
2786 n_useless_values = 0;
2787 n_useless_debug_values = 0;
2788 n_debug_values = 0;
2789 next_uid = 0;
2792 /* Dump the cselib_val *X to FILE *OUT. */
2795 dump_cselib_val (cselib_val **x, FILE *out)
2797 cselib_val *v = *x;
2798 bool need_lf = true;
2800 print_inline_rtx (out, v->val_rtx, 0);
2802 if (v->locs)
2804 struct elt_loc_list *l = v->locs;
2805 if (need_lf)
2807 fputc ('\n', out);
2808 need_lf = false;
2810 fputs (" locs:", out);
2813 if (l->setting_insn)
2814 fprintf (out, "\n from insn %i ",
2815 INSN_UID (l->setting_insn));
2816 else
2817 fprintf (out, "\n ");
2818 print_inline_rtx (out, l->loc, 4);
2820 while ((l = l->next));
2821 fputc ('\n', out);
2823 else
2825 fputs (" no locs", out);
2826 need_lf = true;
2829 if (v->addr_list)
2831 struct elt_list *e = v->addr_list;
2832 if (need_lf)
2834 fputc ('\n', out);
2835 need_lf = false;
2837 fputs (" addr list:", out);
2840 fputs ("\n ", out);
2841 print_inline_rtx (out, e->elt->val_rtx, 2);
2843 while ((e = e->next));
2844 fputc ('\n', out);
2846 else
2848 fputs (" no addrs", out);
2849 need_lf = true;
2852 if (v->next_containing_mem == &dummy_val)
2853 fputs (" last mem\n", out);
2854 else if (v->next_containing_mem)
2856 fputs (" next mem ", out);
2857 print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
2858 fputc ('\n', out);
2860 else if (need_lf)
2861 fputc ('\n', out);
2863 return 1;
2866 /* Dump to OUT everything in the CSELIB table. */
2868 void
2869 dump_cselib_table (FILE *out)
2871 fprintf (out, "cselib hash table:\n");
2872 cselib_hash_table.traverse <FILE *, dump_cselib_val> (out);
2873 fprintf (out, "cselib preserved hash table:\n");
2874 cselib_preserved_hash_table.traverse <FILE *, dump_cselib_val> (out);
2875 if (first_containing_mem != &dummy_val)
2877 fputs ("first mem ", out);
2878 print_inline_rtx (out, first_containing_mem->val_rtx, 2);
2879 fputc ('\n', out);
2881 fprintf (out, "next uid %i\n", next_uid);
2884 #include "gt-cselib.h"