Handle constant fp classifications in fold-const-call.c
[official-gcc.git] / gcc / cselib.c
blob4fc7097188167bb5abe41c9928774943f87e87e7
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 "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "df.h"
28 #include "tm_p.h"
29 #include "regs.h"
30 #include "emit-rtl.h"
31 #include "dumpfile.h"
32 #include "cselib.h"
33 #include "params.h"
35 /* A list of cselib_val structures. */
36 struct elt_list
38 struct elt_list *next;
39 cselib_val *elt;
42 static bool cselib_record_memory;
43 static bool cselib_preserve_constants;
44 static bool cselib_any_perm_equivs;
45 static inline void promote_debug_loc (struct elt_loc_list *l);
46 static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
47 static void new_elt_loc_list (cselib_val *, rtx);
48 static void unchain_one_value (cselib_val *);
49 static void unchain_one_elt_list (struct elt_list **);
50 static void unchain_one_elt_loc_list (struct elt_loc_list **);
51 static void remove_useless_values (void);
52 static int rtx_equal_for_cselib_1 (rtx, rtx, machine_mode);
53 static unsigned int cselib_hash_rtx (rtx, int, machine_mode);
54 static cselib_val *new_cselib_val (unsigned int, machine_mode, rtx);
55 static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
56 static cselib_val *cselib_lookup_mem (rtx, int);
57 static void cselib_invalidate_regno (unsigned int, machine_mode);
58 static void cselib_invalidate_mem (rtx);
59 static void cselib_record_set (rtx, cselib_val *, cselib_val *);
60 static void cselib_record_sets (rtx_insn *);
62 struct expand_value_data
64 bitmap regs_active;
65 cselib_expand_callback callback;
66 void *callback_arg;
67 bool dummy;
70 static rtx cselib_expand_value_rtx_1 (rtx, struct expand_value_data *, int);
72 /* There are three ways in which cselib can look up an rtx:
73 - for a REG, the reg_values table (which is indexed by regno) is used
74 - for a MEM, we recursively look up its address and then follow the
75 addr_list of that value
76 - for everything else, we compute a hash value and go through the hash
77 table. Since different rtx's can still have the same hash value,
78 this involves walking the table entries for a given value and comparing
79 the locations of the entries with the rtx we are looking up. */
81 struct cselib_hasher : nofree_ptr_hash <cselib_val>
83 struct key {
84 /* The rtx value and its mode (needed separately for constant
85 integers). */
86 machine_mode mode;
87 rtx x;
88 /* The mode of the contaning MEM, if any, otherwise VOIDmode. */
89 machine_mode memmode;
91 typedef key *compare_type;
92 static inline hashval_t hash (const cselib_val *);
93 static inline bool equal (const cselib_val *, const key *);
96 /* The hash function for our hash table. The value is always computed with
97 cselib_hash_rtx when adding an element; this function just extracts the
98 hash value from a cselib_val structure. */
100 inline hashval_t
101 cselib_hasher::hash (const cselib_val *v)
103 return v->hash;
106 /* The equality test for our hash table. The first argument V is a table
107 element (i.e. a cselib_val), while the second arg X is an rtx. We know
108 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
109 CONST of an appropriate mode. */
111 inline bool
112 cselib_hasher::equal (const cselib_val *v, const key *x_arg)
114 struct elt_loc_list *l;
115 rtx x = x_arg->x;
116 machine_mode mode = x_arg->mode;
117 machine_mode memmode = x_arg->memmode;
119 if (mode != GET_MODE (v->val_rtx))
120 return false;
122 if (GET_CODE (x) == VALUE)
123 return x == v->val_rtx;
125 /* We don't guarantee that distinct rtx's have different hash values,
126 so we need to do a comparison. */
127 for (l = v->locs; l; l = l->next)
128 if (rtx_equal_for_cselib_1 (l->loc, x, memmode))
130 promote_debug_loc (l);
131 return true;
134 return false;
137 /* A table that enables us to look up elts by their value. */
138 static hash_table<cselib_hasher> *cselib_hash_table;
140 /* A table to hold preserved values. */
141 static hash_table<cselib_hasher> *cselib_preserved_hash_table;
143 /* This is a global so we don't have to pass this through every function.
144 It is used in new_elt_loc_list to set SETTING_INSN. */
145 static rtx_insn *cselib_current_insn;
147 /* The unique id that the next create value will take. */
148 static unsigned int next_uid;
150 /* The number of registers we had when the varrays were last resized. */
151 static unsigned int cselib_nregs;
153 /* Count values without known locations, or with only locations that
154 wouldn't have been known except for debug insns. Whenever this
155 grows too big, we remove these useless values from the table.
157 Counting values with only debug values is a bit tricky. We don't
158 want to increment n_useless_values when we create a value for a
159 debug insn, for this would get n_useless_values out of sync, but we
160 want increment it if all locs in the list that were ever referenced
161 in nondebug insns are removed from the list.
163 In the general case, once we do that, we'd have to stop accepting
164 nondebug expressions in the loc list, to avoid having two values
165 equivalent that, without debug insns, would have been made into
166 separate values. However, because debug insns never introduce
167 equivalences themselves (no assignments), the only means for
168 growing loc lists is through nondebug assignments. If the locs
169 also happen to be referenced in debug insns, it will work just fine.
171 A consequence of this is that there's at most one debug-only loc in
172 each loc list. If we keep it in the first entry, testing whether
173 we have a debug-only loc list takes O(1).
175 Furthermore, since any additional entry in a loc list containing a
176 debug loc would have to come from an assignment (nondebug) that
177 references both the initial debug loc and the newly-equivalent loc,
178 the initial debug loc would be promoted to a nondebug loc, and the
179 loc list would not contain debug locs any more.
181 So the only case we have to be careful with in order to keep
182 n_useless_values in sync between debug and nondebug compilations is
183 to avoid incrementing n_useless_values when removing the single loc
184 from a value that turns out to not appear outside debug values. We
185 increment n_useless_debug_values instead, and leave such values
186 alone until, for other reasons, we garbage-collect useless
187 values. */
188 static int n_useless_values;
189 static int n_useless_debug_values;
191 /* Count values whose locs have been taken exclusively from debug
192 insns for the entire life of the value. */
193 static int n_debug_values;
195 /* Number of useless values before we remove them from the hash table. */
196 #define MAX_USELESS_VALUES 32
198 /* This table maps from register number to values. It does not
199 contain pointers to cselib_val structures, but rather elt_lists.
200 The purpose is to be able to refer to the same register in
201 different modes. The first element of the list defines the mode in
202 which the register was set; if the mode is unknown or the value is
203 no longer valid in that mode, ELT will be NULL for the first
204 element. */
205 static struct elt_list **reg_values;
206 static unsigned int reg_values_size;
207 #define REG_VALUES(i) reg_values[i]
209 /* The largest number of hard regs used by any entry added to the
210 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
211 static unsigned int max_value_regs;
213 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
214 in cselib_clear_table() for fast emptying. */
215 static unsigned int *used_regs;
216 static unsigned int n_used_regs;
218 /* We pass this to cselib_invalidate_mem to invalidate all of
219 memory for a non-const call instruction. */
220 static GTY(()) rtx callmem;
222 /* Set by discard_useless_locs if it deleted the last location of any
223 value. */
224 static int values_became_useless;
226 /* Used as stop element of the containing_mem list so we can check
227 presence in the list by checking the next pointer. */
228 static cselib_val dummy_val;
230 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
231 that is constant through the whole function and should never be
232 eliminated. */
233 static cselib_val *cfa_base_preserved_val;
234 static unsigned int cfa_base_preserved_regno = INVALID_REGNUM;
236 /* Used to list all values that contain memory reference.
237 May or may not contain the useless values - the list is compacted
238 each time memory is invalidated. */
239 static cselib_val *first_containing_mem = &dummy_val;
241 static object_allocator<elt_list> elt_list_pool ("elt_list");
242 static object_allocator<elt_loc_list> elt_loc_list_pool ("elt_loc_list");
243 static object_allocator<cselib_val> cselib_val_pool ("cselib_val_list");
245 static pool_allocator value_pool ("value", RTX_CODE_SIZE (VALUE));
247 /* If nonnull, cselib will call this function before freeing useless
248 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
249 void (*cselib_discard_hook) (cselib_val *);
251 /* If nonnull, cselib will call this function before recording sets or
252 even clobbering outputs of INSN. All the recorded sets will be
253 represented in the array sets[n_sets]. new_val_min can be used to
254 tell whether values present in sets are introduced by this
255 instruction. */
256 void (*cselib_record_sets_hook) (rtx_insn *insn, struct cselib_set *sets,
257 int n_sets);
259 #define PRESERVED_VALUE_P(RTX) \
260 (RTL_FLAG_CHECK1 ("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
262 #define SP_BASED_VALUE_P(RTX) \
263 (RTL_FLAG_CHECK1 ("SP_BASED_VALUE_P", (RTX), VALUE)->jump)
267 /* Allocate a struct elt_list and fill in its two elements with the
268 arguments. */
270 static inline struct elt_list *
271 new_elt_list (struct elt_list *next, cselib_val *elt)
273 elt_list *el = elt_list_pool.allocate ();
274 el->next = next;
275 el->elt = elt;
276 return el;
279 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
280 list. */
282 static inline void
283 new_elt_loc_list (cselib_val *val, rtx loc)
285 struct elt_loc_list *el, *next = val->locs;
287 gcc_checking_assert (!next || !next->setting_insn
288 || !DEBUG_INSN_P (next->setting_insn)
289 || cselib_current_insn == next->setting_insn);
291 /* If we're creating the first loc in a debug insn context, we've
292 just created a debug value. Count it. */
293 if (!next && cselib_current_insn && DEBUG_INSN_P (cselib_current_insn))
294 n_debug_values++;
296 val = canonical_cselib_val (val);
297 next = val->locs;
299 if (GET_CODE (loc) == VALUE)
301 loc = canonical_cselib_val (CSELIB_VAL_PTR (loc))->val_rtx;
303 gcc_checking_assert (PRESERVED_VALUE_P (loc)
304 == PRESERVED_VALUE_P (val->val_rtx));
306 if (val->val_rtx == loc)
307 return;
308 else if (val->uid > CSELIB_VAL_PTR (loc)->uid)
310 /* Reverse the insertion. */
311 new_elt_loc_list (CSELIB_VAL_PTR (loc), val->val_rtx);
312 return;
315 gcc_checking_assert (val->uid < CSELIB_VAL_PTR (loc)->uid);
317 if (CSELIB_VAL_PTR (loc)->locs)
319 /* Bring all locs from LOC to VAL. */
320 for (el = CSELIB_VAL_PTR (loc)->locs; el->next; el = el->next)
322 /* Adjust values that have LOC as canonical so that VAL
323 becomes their canonical. */
324 if (el->loc && GET_CODE (el->loc) == VALUE)
326 gcc_checking_assert (CSELIB_VAL_PTR (el->loc)->locs->loc
327 == loc);
328 CSELIB_VAL_PTR (el->loc)->locs->loc = val->val_rtx;
331 el->next = val->locs;
332 next = val->locs = CSELIB_VAL_PTR (loc)->locs;
335 if (CSELIB_VAL_PTR (loc)->addr_list)
337 /* Bring in addr_list into canonical node. */
338 struct elt_list *last = CSELIB_VAL_PTR (loc)->addr_list;
339 while (last->next)
340 last = last->next;
341 last->next = val->addr_list;
342 val->addr_list = CSELIB_VAL_PTR (loc)->addr_list;
343 CSELIB_VAL_PTR (loc)->addr_list = NULL;
346 if (CSELIB_VAL_PTR (loc)->next_containing_mem != NULL
347 && val->next_containing_mem == NULL)
349 /* Add VAL to the containing_mem list after LOC. LOC will
350 be removed when we notice it doesn't contain any
351 MEMs. */
352 val->next_containing_mem = CSELIB_VAL_PTR (loc)->next_containing_mem;
353 CSELIB_VAL_PTR (loc)->next_containing_mem = val;
356 /* Chain LOC back to VAL. */
357 el = elt_loc_list_pool.allocate ();
358 el->loc = val->val_rtx;
359 el->setting_insn = cselib_current_insn;
360 el->next = NULL;
361 CSELIB_VAL_PTR (loc)->locs = el;
364 el = elt_loc_list_pool.allocate ();
365 el->loc = loc;
366 el->setting_insn = cselib_current_insn;
367 el->next = next;
368 val->locs = el;
371 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
372 originating from a debug insn, maintaining the debug values
373 count. */
375 static inline void
376 promote_debug_loc (struct elt_loc_list *l)
378 if (l && l->setting_insn && DEBUG_INSN_P (l->setting_insn)
379 && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
381 n_debug_values--;
382 l->setting_insn = cselib_current_insn;
383 if (cselib_preserve_constants && l->next)
385 gcc_assert (l->next->setting_insn
386 && DEBUG_INSN_P (l->next->setting_insn)
387 && !l->next->next);
388 l->next->setting_insn = cselib_current_insn;
390 else
391 gcc_assert (!l->next);
395 /* The elt_list at *PL is no longer needed. Unchain it and free its
396 storage. */
398 static inline void
399 unchain_one_elt_list (struct elt_list **pl)
401 struct elt_list *l = *pl;
403 *pl = l->next;
404 elt_list_pool.remove (l);
407 /* Likewise for elt_loc_lists. */
409 static void
410 unchain_one_elt_loc_list (struct elt_loc_list **pl)
412 struct elt_loc_list *l = *pl;
414 *pl = l->next;
415 elt_loc_list_pool.remove (l);
418 /* Likewise for cselib_vals. This also frees the addr_list associated with
419 V. */
421 static void
422 unchain_one_value (cselib_val *v)
424 while (v->addr_list)
425 unchain_one_elt_list (&v->addr_list);
427 cselib_val_pool.remove (v);
430 /* Remove all entries from the hash table. Also used during
431 initialization. */
433 void
434 cselib_clear_table (void)
436 cselib_reset_table (1);
439 /* Return TRUE if V is a constant, a function invariant or a VALUE
440 equivalence; FALSE otherwise. */
442 static bool
443 invariant_or_equiv_p (cselib_val *v)
445 struct elt_loc_list *l;
447 if (v == cfa_base_preserved_val)
448 return true;
450 /* Keep VALUE equivalences around. */
451 for (l = v->locs; l; l = l->next)
452 if (GET_CODE (l->loc) == VALUE)
453 return true;
455 if (v->locs != NULL
456 && v->locs->next == NULL)
458 if (CONSTANT_P (v->locs->loc)
459 && (GET_CODE (v->locs->loc) != CONST
460 || !references_value_p (v->locs->loc, 0)))
461 return true;
462 /* Although a debug expr may be bound to different expressions,
463 we can preserve it as if it was constant, to get unification
464 and proper merging within var-tracking. */
465 if (GET_CODE (v->locs->loc) == DEBUG_EXPR
466 || GET_CODE (v->locs->loc) == DEBUG_IMPLICIT_PTR
467 || GET_CODE (v->locs->loc) == ENTRY_VALUE
468 || GET_CODE (v->locs->loc) == DEBUG_PARAMETER_REF)
469 return true;
471 /* (plus (value V) (const_int C)) is invariant iff V is invariant. */
472 if (GET_CODE (v->locs->loc) == PLUS
473 && CONST_INT_P (XEXP (v->locs->loc, 1))
474 && GET_CODE (XEXP (v->locs->loc, 0)) == VALUE
475 && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v->locs->loc, 0))))
476 return true;
479 return false;
482 /* Remove from hash table all VALUEs except constants, function
483 invariants and VALUE equivalences. */
486 preserve_constants_and_equivs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
488 cselib_val *v = *x;
490 if (invariant_or_equiv_p (v))
492 cselib_hasher::key lookup = {
493 GET_MODE (v->val_rtx), v->val_rtx, VOIDmode
495 cselib_val **slot
496 = cselib_preserved_hash_table->find_slot_with_hash (&lookup,
497 v->hash, INSERT);
498 gcc_assert (!*slot);
499 *slot = v;
502 cselib_hash_table->clear_slot (x);
504 return 1;
507 /* Remove all entries from the hash table, arranging for the next
508 value to be numbered NUM. */
510 void
511 cselib_reset_table (unsigned int num)
513 unsigned int i;
515 max_value_regs = 0;
517 if (cfa_base_preserved_val)
519 unsigned int regno = cfa_base_preserved_regno;
520 unsigned int new_used_regs = 0;
521 for (i = 0; i < n_used_regs; i++)
522 if (used_regs[i] == regno)
524 new_used_regs = 1;
525 continue;
527 else
528 REG_VALUES (used_regs[i]) = 0;
529 gcc_assert (new_used_regs == 1);
530 n_used_regs = new_used_regs;
531 used_regs[0] = regno;
532 max_value_regs
533 = hard_regno_nregs[regno][GET_MODE (cfa_base_preserved_val->locs->loc)];
535 else
537 for (i = 0; i < n_used_regs; i++)
538 REG_VALUES (used_regs[i]) = 0;
539 n_used_regs = 0;
542 if (cselib_preserve_constants)
543 cselib_hash_table->traverse <void *, preserve_constants_and_equivs>
544 (NULL);
545 else
547 cselib_hash_table->empty ();
548 gcc_checking_assert (!cselib_any_perm_equivs);
551 n_useless_values = 0;
552 n_useless_debug_values = 0;
553 n_debug_values = 0;
555 next_uid = num;
557 first_containing_mem = &dummy_val;
560 /* Return the number of the next value that will be generated. */
562 unsigned int
563 cselib_get_next_uid (void)
565 return next_uid;
568 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
569 INSERTing if requested. When X is part of the address of a MEM,
570 MEMMODE should specify the mode of the MEM. */
572 static cselib_val **
573 cselib_find_slot (machine_mode mode, rtx x, hashval_t hash,
574 enum insert_option insert, machine_mode memmode)
576 cselib_val **slot = NULL;
577 cselib_hasher::key lookup = { mode, x, memmode };
578 if (cselib_preserve_constants)
579 slot = cselib_preserved_hash_table->find_slot_with_hash (&lookup, hash,
580 NO_INSERT);
581 if (!slot)
582 slot = cselib_hash_table->find_slot_with_hash (&lookup, hash, insert);
583 return slot;
586 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
587 only return true for values which point to a cselib_val whose value
588 element has been set to zero, which implies the cselib_val will be
589 removed. */
592 references_value_p (const_rtx x, int only_useless)
594 const enum rtx_code code = GET_CODE (x);
595 const char *fmt = GET_RTX_FORMAT (code);
596 int i, j;
598 if (GET_CODE (x) == VALUE
599 && (! only_useless ||
600 (CSELIB_VAL_PTR (x)->locs == 0 && !PRESERVED_VALUE_P (x))))
601 return 1;
603 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
605 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
606 return 1;
607 else if (fmt[i] == 'E')
608 for (j = 0; j < XVECLEN (x, i); j++)
609 if (references_value_p (XVECEXP (x, i, j), only_useless))
610 return 1;
613 return 0;
616 /* For all locations found in X, delete locations that reference useless
617 values (i.e. values without any location). Called through
618 htab_traverse. */
621 discard_useless_locs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
623 cselib_val *v = *x;
624 struct elt_loc_list **p = &v->locs;
625 bool had_locs = v->locs != NULL;
626 rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
628 while (*p)
630 if (references_value_p ((*p)->loc, 1))
631 unchain_one_elt_loc_list (p);
632 else
633 p = &(*p)->next;
636 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
638 if (setting_insn && DEBUG_INSN_P (setting_insn))
639 n_useless_debug_values++;
640 else
641 n_useless_values++;
642 values_became_useless = 1;
644 return 1;
647 /* If X is a value with no locations, remove it from the hashtable. */
650 discard_useless_values (cselib_val **x, void *info ATTRIBUTE_UNUSED)
652 cselib_val *v = *x;
654 if (v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
656 if (cselib_discard_hook)
657 cselib_discard_hook (v);
659 CSELIB_VAL_PTR (v->val_rtx) = NULL;
660 cselib_hash_table->clear_slot (x);
661 unchain_one_value (v);
662 n_useless_values--;
665 return 1;
668 /* Clean out useless values (i.e. those which no longer have locations
669 associated with them) from the hash table. */
671 static void
672 remove_useless_values (void)
674 cselib_val **p, *v;
676 /* First pass: eliminate locations that reference the value. That in
677 turn can make more values useless. */
680 values_became_useless = 0;
681 cselib_hash_table->traverse <void *, discard_useless_locs> (NULL);
683 while (values_became_useless);
685 /* Second pass: actually remove the values. */
687 p = &first_containing_mem;
688 for (v = *p; v != &dummy_val; v = v->next_containing_mem)
689 if (v->locs && v == canonical_cselib_val (v))
691 *p = v;
692 p = &(*p)->next_containing_mem;
694 *p = &dummy_val;
696 n_useless_values += n_useless_debug_values;
697 n_debug_values -= n_useless_debug_values;
698 n_useless_debug_values = 0;
700 cselib_hash_table->traverse <void *, discard_useless_values> (NULL);
702 gcc_assert (!n_useless_values);
705 /* Arrange for a value to not be removed from the hash table even if
706 it becomes useless. */
708 void
709 cselib_preserve_value (cselib_val *v)
711 PRESERVED_VALUE_P (v->val_rtx) = 1;
714 /* Test whether a value is preserved. */
716 bool
717 cselib_preserved_value_p (cselib_val *v)
719 return PRESERVED_VALUE_P (v->val_rtx);
722 /* Arrange for a REG value to be assumed constant through the whole function,
723 never invalidated and preserved across cselib_reset_table calls. */
725 void
726 cselib_preserve_cfa_base_value (cselib_val *v, unsigned int regno)
728 if (cselib_preserve_constants
729 && v->locs
730 && REG_P (v->locs->loc))
732 cfa_base_preserved_val = v;
733 cfa_base_preserved_regno = regno;
737 /* Clean all non-constant expressions in the hash table, but retain
738 their values. */
740 void
741 cselib_preserve_only_values (void)
743 int i;
745 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
746 cselib_invalidate_regno (i, reg_raw_mode[i]);
748 cselib_invalidate_mem (callmem);
750 remove_useless_values ();
752 gcc_assert (first_containing_mem == &dummy_val);
755 /* Arrange for a value to be marked as based on stack pointer
756 for find_base_term purposes. */
758 void
759 cselib_set_value_sp_based (cselib_val *v)
761 SP_BASED_VALUE_P (v->val_rtx) = 1;
764 /* Test whether a value is based on stack pointer for
765 find_base_term purposes. */
767 bool
768 cselib_sp_based_value_p (cselib_val *v)
770 return SP_BASED_VALUE_P (v->val_rtx);
773 /* Return the mode in which a register was last set. If X is not a
774 register, return its mode. If the mode in which the register was
775 set is not known, or the value was already clobbered, return
776 VOIDmode. */
778 machine_mode
779 cselib_reg_set_mode (const_rtx x)
781 if (!REG_P (x))
782 return GET_MODE (x);
784 if (REG_VALUES (REGNO (x)) == NULL
785 || REG_VALUES (REGNO (x))->elt == NULL)
786 return VOIDmode;
788 return GET_MODE (REG_VALUES (REGNO (x))->elt->val_rtx);
791 /* Return nonzero if we can prove that X and Y contain the same value, taking
792 our gathered information into account. */
795 rtx_equal_for_cselib_p (rtx x, rtx y)
797 return rtx_equal_for_cselib_1 (x, y, VOIDmode);
800 /* If x is a PLUS or an autoinc operation, expand the operation,
801 storing the offset, if any, in *OFF. */
803 static rtx
804 autoinc_split (rtx x, rtx *off, machine_mode memmode)
806 switch (GET_CODE (x))
808 case PLUS:
809 *off = XEXP (x, 1);
810 return XEXP (x, 0);
812 case PRE_DEC:
813 if (memmode == VOIDmode)
814 return x;
816 *off = GEN_INT (-GET_MODE_SIZE (memmode));
817 return XEXP (x, 0);
818 break;
820 case PRE_INC:
821 if (memmode == VOIDmode)
822 return x;
824 *off = GEN_INT (GET_MODE_SIZE (memmode));
825 return XEXP (x, 0);
827 case PRE_MODIFY:
828 return XEXP (x, 1);
830 case POST_DEC:
831 case POST_INC:
832 case POST_MODIFY:
833 return XEXP (x, 0);
835 default:
836 return x;
840 /* Return nonzero if we can prove that X and Y contain the same value,
841 taking our gathered information into account. MEMMODE holds the
842 mode of the enclosing MEM, if any, as required to deal with autoinc
843 addressing modes. If X and Y are not (known to be) part of
844 addresses, MEMMODE should be VOIDmode. */
846 static int
847 rtx_equal_for_cselib_1 (rtx x, rtx y, machine_mode memmode)
849 enum rtx_code code;
850 const char *fmt;
851 int i;
853 if (REG_P (x) || MEM_P (x))
855 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0, memmode);
857 if (e)
858 x = e->val_rtx;
861 if (REG_P (y) || MEM_P (y))
863 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0, memmode);
865 if (e)
866 y = e->val_rtx;
869 if (x == y)
870 return 1;
872 if (GET_CODE (x) == VALUE)
874 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (x));
875 struct elt_loc_list *l;
877 if (GET_CODE (y) == VALUE)
878 return e == canonical_cselib_val (CSELIB_VAL_PTR (y));
880 for (l = e->locs; l; l = l->next)
882 rtx t = l->loc;
884 /* Avoid infinite recursion. We know we have the canonical
885 value, so we can just skip any values in the equivalence
886 list. */
887 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
888 continue;
889 else if (rtx_equal_for_cselib_1 (t, y, memmode))
890 return 1;
893 return 0;
895 else if (GET_CODE (y) == VALUE)
897 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (y));
898 struct elt_loc_list *l;
900 for (l = e->locs; l; l = l->next)
902 rtx t = l->loc;
904 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
905 continue;
906 else if (rtx_equal_for_cselib_1 (x, t, memmode))
907 return 1;
910 return 0;
913 if (GET_MODE (x) != GET_MODE (y))
914 return 0;
916 if (GET_CODE (x) != GET_CODE (y))
918 rtx xorig = x, yorig = y;
919 rtx xoff = NULL, yoff = NULL;
921 x = autoinc_split (x, &xoff, memmode);
922 y = autoinc_split (y, &yoff, memmode);
924 if (!xoff != !yoff)
925 return 0;
927 if (xoff && !rtx_equal_for_cselib_1 (xoff, yoff, memmode))
928 return 0;
930 /* Don't recurse if nothing changed. */
931 if (x != xorig || y != yorig)
932 return rtx_equal_for_cselib_1 (x, y, memmode);
934 return 0;
937 /* These won't be handled correctly by the code below. */
938 switch (GET_CODE (x))
940 CASE_CONST_UNIQUE:
941 case DEBUG_EXPR:
942 return 0;
944 case DEBUG_IMPLICIT_PTR:
945 return DEBUG_IMPLICIT_PTR_DECL (x)
946 == DEBUG_IMPLICIT_PTR_DECL (y);
948 case DEBUG_PARAMETER_REF:
949 return DEBUG_PARAMETER_REF_DECL (x)
950 == DEBUG_PARAMETER_REF_DECL (y);
952 case ENTRY_VALUE:
953 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
954 use rtx_equal_for_cselib_1 to compare the operands. */
955 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
957 case LABEL_REF:
958 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
960 case REG:
961 return REGNO (x) == REGNO (y);
963 case MEM:
964 /* We have to compare any autoinc operations in the addresses
965 using this MEM's mode. */
966 return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x));
968 default:
969 break;
972 code = GET_CODE (x);
973 fmt = GET_RTX_FORMAT (code);
975 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
977 int j;
979 switch (fmt[i])
981 case 'w':
982 if (XWINT (x, i) != XWINT (y, i))
983 return 0;
984 break;
986 case 'n':
987 case 'i':
988 if (XINT (x, i) != XINT (y, i))
989 return 0;
990 break;
992 case 'V':
993 case 'E':
994 /* Two vectors must have the same length. */
995 if (XVECLEN (x, i) != XVECLEN (y, i))
996 return 0;
998 /* And the corresponding elements must match. */
999 for (j = 0; j < XVECLEN (x, i); j++)
1000 if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
1001 XVECEXP (y, i, j), memmode))
1002 return 0;
1003 break;
1005 case 'e':
1006 if (i == 1
1007 && targetm.commutative_p (x, UNKNOWN)
1008 && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode)
1009 && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode))
1010 return 1;
1011 if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode))
1012 return 0;
1013 break;
1015 case 'S':
1016 case 's':
1017 if (strcmp (XSTR (x, i), XSTR (y, i)))
1018 return 0;
1019 break;
1021 case 'u':
1022 /* These are just backpointers, so they don't matter. */
1023 break;
1025 case '0':
1026 case 't':
1027 break;
1029 /* It is believed that rtx's at this level will never
1030 contain anything but integers and other rtx's,
1031 except for within LABEL_REFs and SYMBOL_REFs. */
1032 default:
1033 gcc_unreachable ();
1036 return 1;
1039 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1040 For registers and memory locations, we look up their cselib_val structure
1041 and return its VALUE element.
1042 Possible reasons for return 0 are: the object is volatile, or we couldn't
1043 find a register or memory location in the table and CREATE is zero. If
1044 CREATE is nonzero, table elts are created for regs and mem.
1045 N.B. this hash function returns the same hash value for RTXes that
1046 differ only in the order of operands, thus it is suitable for comparisons
1047 that take commutativity into account.
1048 If we wanted to also support associative rules, we'd have to use a different
1049 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1050 MEMMODE indicates the mode of an enclosing MEM, and it's only
1051 used to compute autoinc values.
1052 We used to have a MODE argument for hashing for CONST_INTs, but that
1053 didn't make sense, since it caused spurious hash differences between
1054 (set (reg:SI 1) (const_int))
1055 (plus:SI (reg:SI 2) (reg:SI 1))
1057 (plus:SI (reg:SI 2) (const_int))
1058 If the mode is important in any context, it must be checked specifically
1059 in a comparison anyway, since relying on hash differences is unsafe. */
1061 static unsigned int
1062 cselib_hash_rtx (rtx x, int create, machine_mode memmode)
1064 cselib_val *e;
1065 int i, j;
1066 enum rtx_code code;
1067 const char *fmt;
1068 unsigned int hash = 0;
1070 code = GET_CODE (x);
1071 hash += (unsigned) code + (unsigned) GET_MODE (x);
1073 switch (code)
1075 case VALUE:
1076 e = CSELIB_VAL_PTR (x);
1077 return e->hash;
1079 case MEM:
1080 case REG:
1081 e = cselib_lookup (x, GET_MODE (x), create, memmode);
1082 if (! e)
1083 return 0;
1085 return e->hash;
1087 case DEBUG_EXPR:
1088 hash += ((unsigned) DEBUG_EXPR << 7)
1089 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
1090 return hash ? hash : (unsigned int) DEBUG_EXPR;
1092 case DEBUG_IMPLICIT_PTR:
1093 hash += ((unsigned) DEBUG_IMPLICIT_PTR << 7)
1094 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x));
1095 return hash ? hash : (unsigned int) DEBUG_IMPLICIT_PTR;
1097 case DEBUG_PARAMETER_REF:
1098 hash += ((unsigned) DEBUG_PARAMETER_REF << 7)
1099 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x));
1100 return hash ? hash : (unsigned int) DEBUG_PARAMETER_REF;
1102 case ENTRY_VALUE:
1103 /* ENTRY_VALUEs are function invariant, thus try to avoid
1104 recursing on argument if ENTRY_VALUE is one of the
1105 forms emitted by expand_debug_expr, otherwise
1106 ENTRY_VALUE hash would depend on the current value
1107 in some register or memory. */
1108 if (REG_P (ENTRY_VALUE_EXP (x)))
1109 hash += (unsigned int) REG
1110 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x))
1111 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x));
1112 else if (MEM_P (ENTRY_VALUE_EXP (x))
1113 && REG_P (XEXP (ENTRY_VALUE_EXP (x), 0)))
1114 hash += (unsigned int) MEM
1115 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x), 0))
1116 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x), 0));
1117 else
1118 hash += cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode);
1119 return hash ? hash : (unsigned int) ENTRY_VALUE;
1121 case CONST_INT:
1122 hash += ((unsigned) CONST_INT << 7) + UINTVAL (x);
1123 return hash ? hash : (unsigned int) CONST_INT;
1125 case CONST_WIDE_INT:
1126 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
1127 hash += CONST_WIDE_INT_ELT (x, i);
1128 return hash;
1130 case CONST_DOUBLE:
1131 /* This is like the general case, except that it only counts
1132 the integers representing the constant. */
1133 hash += (unsigned) code + (unsigned) GET_MODE (x);
1134 if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (x) == VOIDmode)
1135 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1136 + (unsigned) CONST_DOUBLE_HIGH (x));
1137 else
1138 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
1139 return hash ? hash : (unsigned int) CONST_DOUBLE;
1141 case CONST_FIXED:
1142 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1143 hash += fixed_hash (CONST_FIXED_VALUE (x));
1144 return hash ? hash : (unsigned int) CONST_FIXED;
1146 case CONST_VECTOR:
1148 int units;
1149 rtx elt;
1151 units = CONST_VECTOR_NUNITS (x);
1153 for (i = 0; i < units; ++i)
1155 elt = CONST_VECTOR_ELT (x, i);
1156 hash += cselib_hash_rtx (elt, 0, memmode);
1159 return hash;
1162 /* Assume there is only one rtx object for any given label. */
1163 case LABEL_REF:
1164 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1165 differences and differences between each stage's debugging dumps. */
1166 hash += (((unsigned int) LABEL_REF << 7)
1167 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x)));
1168 return hash ? hash : (unsigned int) LABEL_REF;
1170 case SYMBOL_REF:
1172 /* Don't hash on the symbol's address to avoid bootstrap differences.
1173 Different hash values may cause expressions to be recorded in
1174 different orders and thus different registers to be used in the
1175 final assembler. This also avoids differences in the dump files
1176 between various stages. */
1177 unsigned int h = 0;
1178 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1180 while (*p)
1181 h += (h << 7) + *p++; /* ??? revisit */
1183 hash += ((unsigned int) SYMBOL_REF << 7) + h;
1184 return hash ? hash : (unsigned int) SYMBOL_REF;
1187 case PRE_DEC:
1188 case PRE_INC:
1189 /* We can't compute these without knowing the MEM mode. */
1190 gcc_assert (memmode != VOIDmode);
1191 i = GET_MODE_SIZE (memmode);
1192 if (code == PRE_DEC)
1193 i = -i;
1194 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1195 like (mem:MEMMODE (plus (reg) (const_int I))). */
1196 hash += (unsigned) PLUS - (unsigned)code
1197 + cselib_hash_rtx (XEXP (x, 0), create, memmode)
1198 + cselib_hash_rtx (GEN_INT (i), create, memmode);
1199 return hash ? hash : 1 + (unsigned) PLUS;
1201 case PRE_MODIFY:
1202 gcc_assert (memmode != VOIDmode);
1203 return cselib_hash_rtx (XEXP (x, 1), create, memmode);
1205 case POST_DEC:
1206 case POST_INC:
1207 case POST_MODIFY:
1208 gcc_assert (memmode != VOIDmode);
1209 return cselib_hash_rtx (XEXP (x, 0), create, memmode);
1211 case PC:
1212 case CC0:
1213 case CALL:
1214 case UNSPEC_VOLATILE:
1215 return 0;
1217 case ASM_OPERANDS:
1218 if (MEM_VOLATILE_P (x))
1219 return 0;
1221 break;
1223 default:
1224 break;
1227 i = GET_RTX_LENGTH (code) - 1;
1228 fmt = GET_RTX_FORMAT (code);
1229 for (; i >= 0; i--)
1231 switch (fmt[i])
1233 case 'e':
1235 rtx tem = XEXP (x, i);
1236 unsigned int tem_hash = cselib_hash_rtx (tem, create, memmode);
1238 if (tem_hash == 0)
1239 return 0;
1241 hash += tem_hash;
1243 break;
1244 case 'E':
1245 for (j = 0; j < XVECLEN (x, i); j++)
1247 unsigned int tem_hash
1248 = cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
1250 if (tem_hash == 0)
1251 return 0;
1253 hash += tem_hash;
1255 break;
1257 case 's':
1259 const unsigned char *p = (const unsigned char *) XSTR (x, i);
1261 if (p)
1262 while (*p)
1263 hash += *p++;
1264 break;
1267 case 'i':
1268 hash += XINT (x, i);
1269 break;
1271 case '0':
1272 case 't':
1273 /* unused */
1274 break;
1276 default:
1277 gcc_unreachable ();
1281 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
1284 /* Create a new value structure for VALUE and initialize it. The mode of the
1285 value is MODE. */
1287 static inline cselib_val *
1288 new_cselib_val (unsigned int hash, machine_mode mode, rtx x)
1290 cselib_val *e = cselib_val_pool.allocate ();
1292 gcc_assert (hash);
1293 gcc_assert (next_uid);
1295 e->hash = hash;
1296 e->uid = next_uid++;
1297 /* We use an alloc pool to allocate this RTL construct because it
1298 accounts for about 8% of the overall memory usage. We know
1299 precisely when we can have VALUE RTXen (when cselib is active)
1300 so we don't need to put them in garbage collected memory.
1301 ??? Why should a VALUE be an RTX in the first place? */
1302 e->val_rtx = (rtx_def*) value_pool.allocate ();
1303 memset (e->val_rtx, 0, RTX_HDR_SIZE);
1304 PUT_CODE (e->val_rtx, VALUE);
1305 PUT_MODE (e->val_rtx, mode);
1306 CSELIB_VAL_PTR (e->val_rtx) = e;
1307 e->addr_list = 0;
1308 e->locs = 0;
1309 e->next_containing_mem = 0;
1311 if (dump_file && (dump_flags & TDF_CSELIB))
1313 fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
1314 if (flag_dump_noaddr || flag_dump_unnumbered)
1315 fputs ("# ", dump_file);
1316 else
1317 fprintf (dump_file, "%p ", (void*)e);
1318 print_rtl_single (dump_file, x);
1319 fputc ('\n', dump_file);
1322 return e;
1325 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1326 contains the data at this address. X is a MEM that represents the
1327 value. Update the two value structures to represent this situation. */
1329 static void
1330 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
1332 struct elt_loc_list *l;
1334 addr_elt = canonical_cselib_val (addr_elt);
1335 mem_elt = canonical_cselib_val (mem_elt);
1337 /* Avoid duplicates. */
1338 for (l = mem_elt->locs; l; l = l->next)
1339 if (MEM_P (l->loc)
1340 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
1342 promote_debug_loc (l);
1343 return;
1346 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
1347 new_elt_loc_list (mem_elt,
1348 replace_equiv_address_nv (x, addr_elt->val_rtx));
1349 if (mem_elt->next_containing_mem == NULL)
1351 mem_elt->next_containing_mem = first_containing_mem;
1352 first_containing_mem = mem_elt;
1356 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1357 If CREATE, make a new one if we haven't seen it before. */
1359 static cselib_val *
1360 cselib_lookup_mem (rtx x, int create)
1362 machine_mode mode = GET_MODE (x);
1363 machine_mode addr_mode;
1364 cselib_val **slot;
1365 cselib_val *addr;
1366 cselib_val *mem_elt;
1367 struct elt_list *l;
1369 if (MEM_VOLATILE_P (x) || mode == BLKmode
1370 || !cselib_record_memory
1371 || (FLOAT_MODE_P (mode) && flag_float_store))
1372 return 0;
1374 addr_mode = GET_MODE (XEXP (x, 0));
1375 if (addr_mode == VOIDmode)
1376 addr_mode = Pmode;
1378 /* Look up the value for the address. */
1379 addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
1380 if (! addr)
1381 return 0;
1383 addr = canonical_cselib_val (addr);
1384 /* Find a value that describes a value of our mode at that address. */
1385 for (l = addr->addr_list; l; l = l->next)
1386 if (GET_MODE (l->elt->val_rtx) == mode)
1388 promote_debug_loc (l->elt->locs);
1389 return l->elt;
1392 if (! create)
1393 return 0;
1395 mem_elt = new_cselib_val (next_uid, mode, x);
1396 add_mem_for_addr (addr, mem_elt, x);
1397 slot = cselib_find_slot (mode, x, mem_elt->hash, INSERT, VOIDmode);
1398 *slot = mem_elt;
1399 return mem_elt;
1402 /* Search through the possible substitutions in P. We prefer a non reg
1403 substitution because this allows us to expand the tree further. If
1404 we find, just a reg, take the lowest regno. There may be several
1405 non-reg results, we just take the first one because they will all
1406 expand to the same place. */
1408 static rtx
1409 expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1410 int max_depth)
1412 rtx reg_result = NULL;
1413 unsigned int regno = UINT_MAX;
1414 struct elt_loc_list *p_in = p;
1416 for (; p; p = p->next)
1418 /* Return these right away to avoid returning stack pointer based
1419 expressions for frame pointer and vice versa, which is something
1420 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1421 for more details. */
1422 if (REG_P (p->loc)
1423 && (REGNO (p->loc) == STACK_POINTER_REGNUM
1424 || REGNO (p->loc) == FRAME_POINTER_REGNUM
1425 || REGNO (p->loc) == HARD_FRAME_POINTER_REGNUM
1426 || REGNO (p->loc) == cfa_base_preserved_regno))
1427 return p->loc;
1428 /* Avoid infinite recursion trying to expand a reg into a
1429 the same reg. */
1430 if ((REG_P (p->loc))
1431 && (REGNO (p->loc) < regno)
1432 && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
1434 reg_result = p->loc;
1435 regno = REGNO (p->loc);
1437 /* Avoid infinite recursion and do not try to expand the
1438 value. */
1439 else if (GET_CODE (p->loc) == VALUE
1440 && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1441 continue;
1442 else if (!REG_P (p->loc))
1444 rtx result, note;
1445 if (dump_file && (dump_flags & TDF_CSELIB))
1447 print_inline_rtx (dump_file, p->loc, 0);
1448 fprintf (dump_file, "\n");
1450 if (GET_CODE (p->loc) == LO_SUM
1451 && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1452 && p->setting_insn
1453 && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1454 && XEXP (note, 0) == XEXP (p->loc, 1))
1455 return XEXP (p->loc, 1);
1456 result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
1457 if (result)
1458 return result;
1463 if (regno != UINT_MAX)
1465 rtx result;
1466 if (dump_file && (dump_flags & TDF_CSELIB))
1467 fprintf (dump_file, "r%d\n", regno);
1469 result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
1470 if (result)
1471 return result;
1474 if (dump_file && (dump_flags & TDF_CSELIB))
1476 if (reg_result)
1478 print_inline_rtx (dump_file, reg_result, 0);
1479 fprintf (dump_file, "\n");
1481 else
1482 fprintf (dump_file, "NULL\n");
1484 return reg_result;
1488 /* Forward substitute and expand an expression out to its roots.
1489 This is the opposite of common subexpression. Because local value
1490 numbering is such a weak optimization, the expanded expression is
1491 pretty much unique (not from a pointer equals point of view but
1492 from a tree shape point of view.
1494 This function returns NULL if the expansion fails. The expansion
1495 will fail if there is no value number for one of the operands or if
1496 one of the operands has been overwritten between the current insn
1497 and the beginning of the basic block. For instance x has no
1498 expansion in:
1500 r1 <- r1 + 3
1501 x <- r1 + 8
1503 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1504 It is clear on return. */
1507 cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
1509 struct expand_value_data evd;
1511 evd.regs_active = regs_active;
1512 evd.callback = NULL;
1513 evd.callback_arg = NULL;
1514 evd.dummy = false;
1516 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1519 /* Same as cselib_expand_value_rtx, but using a callback to try to
1520 resolve some expressions. The CB function should return ORIG if it
1521 can't or does not want to deal with a certain RTX. Any other
1522 return value, including NULL, will be used as the expansion for
1523 VALUE, without any further changes. */
1526 cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1527 cselib_expand_callback cb, void *data)
1529 struct expand_value_data evd;
1531 evd.regs_active = regs_active;
1532 evd.callback = cb;
1533 evd.callback_arg = data;
1534 evd.dummy = false;
1536 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1539 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1540 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1541 would return NULL or non-NULL, without allocating new rtx. */
1543 bool
1544 cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1545 cselib_expand_callback cb, void *data)
1547 struct expand_value_data evd;
1549 evd.regs_active = regs_active;
1550 evd.callback = cb;
1551 evd.callback_arg = data;
1552 evd.dummy = true;
1554 return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1557 /* Internal implementation of cselib_expand_value_rtx and
1558 cselib_expand_value_rtx_cb. */
1560 static rtx
1561 cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1562 int max_depth)
1564 rtx copy, scopy;
1565 int i, j;
1566 RTX_CODE code;
1567 const char *format_ptr;
1568 machine_mode mode;
1570 code = GET_CODE (orig);
1572 /* For the context of dse, if we end up expand into a huge tree, we
1573 will not have a useful address, so we might as well just give up
1574 quickly. */
1575 if (max_depth <= 0)
1576 return NULL;
1578 switch (code)
1580 case REG:
1582 struct elt_list *l = REG_VALUES (REGNO (orig));
1584 if (l && l->elt == NULL)
1585 l = l->next;
1586 for (; l; l = l->next)
1587 if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1589 rtx result;
1590 unsigned regno = REGNO (orig);
1592 /* The only thing that we are not willing to do (this
1593 is requirement of dse and if others potential uses
1594 need this function we should add a parm to control
1595 it) is that we will not substitute the
1596 STACK_POINTER_REGNUM, FRAME_POINTER or the
1597 HARD_FRAME_POINTER.
1599 These expansions confuses the code that notices that
1600 stores into the frame go dead at the end of the
1601 function and that the frame is not effected by calls
1602 to subroutines. If you allow the
1603 STACK_POINTER_REGNUM substitution, then dse will
1604 think that parameter pushing also goes dead which is
1605 wrong. If you allow the FRAME_POINTER or the
1606 HARD_FRAME_POINTER then you lose the opportunity to
1607 make the frame assumptions. */
1608 if (regno == STACK_POINTER_REGNUM
1609 || regno == FRAME_POINTER_REGNUM
1610 || regno == HARD_FRAME_POINTER_REGNUM
1611 || regno == cfa_base_preserved_regno)
1612 return orig;
1614 bitmap_set_bit (evd->regs_active, regno);
1616 if (dump_file && (dump_flags & TDF_CSELIB))
1617 fprintf (dump_file, "expanding: r%d into: ", regno);
1619 result = expand_loc (l->elt->locs, evd, max_depth);
1620 bitmap_clear_bit (evd->regs_active, regno);
1622 if (result)
1623 return result;
1624 else
1625 return orig;
1629 CASE_CONST_ANY:
1630 case SYMBOL_REF:
1631 case CODE_LABEL:
1632 case PC:
1633 case CC0:
1634 case SCRATCH:
1635 /* SCRATCH must be shared because they represent distinct values. */
1636 return orig;
1637 case CLOBBER:
1638 if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1639 return orig;
1640 break;
1642 case CONST:
1643 if (shared_const_p (orig))
1644 return orig;
1645 break;
1647 case SUBREG:
1649 rtx subreg;
1651 if (evd->callback)
1653 subreg = evd->callback (orig, evd->regs_active, max_depth,
1654 evd->callback_arg);
1655 if (subreg != orig)
1656 return subreg;
1659 subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1660 max_depth - 1);
1661 if (!subreg)
1662 return NULL;
1663 scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1664 GET_MODE (SUBREG_REG (orig)),
1665 SUBREG_BYTE (orig));
1666 if (scopy == NULL
1667 || (GET_CODE (scopy) == SUBREG
1668 && !REG_P (SUBREG_REG (scopy))
1669 && !MEM_P (SUBREG_REG (scopy))))
1670 return NULL;
1672 return scopy;
1675 case VALUE:
1677 rtx result;
1679 if (dump_file && (dump_flags & TDF_CSELIB))
1681 fputs ("\nexpanding ", dump_file);
1682 print_rtl_single (dump_file, orig);
1683 fputs (" into...", dump_file);
1686 if (evd->callback)
1688 result = evd->callback (orig, evd->regs_active, max_depth,
1689 evd->callback_arg);
1691 if (result != orig)
1692 return result;
1695 result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
1696 return result;
1699 case DEBUG_EXPR:
1700 if (evd->callback)
1701 return evd->callback (orig, evd->regs_active, max_depth,
1702 evd->callback_arg);
1703 return orig;
1705 default:
1706 break;
1709 /* Copy the various flags, fields, and other information. We assume
1710 that all fields need copying, and then clear the fields that should
1711 not be copied. That is the sensible default behavior, and forces
1712 us to explicitly document why we are *not* copying a flag. */
1713 if (evd->dummy)
1714 copy = NULL;
1715 else
1716 copy = shallow_copy_rtx (orig);
1718 format_ptr = GET_RTX_FORMAT (code);
1720 for (i = 0; i < GET_RTX_LENGTH (code); i++)
1721 switch (*format_ptr++)
1723 case 'e':
1724 if (XEXP (orig, i) != NULL)
1726 rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1727 max_depth - 1);
1728 if (!result)
1729 return NULL;
1730 if (copy)
1731 XEXP (copy, i) = result;
1733 break;
1735 case 'E':
1736 case 'V':
1737 if (XVEC (orig, i) != NULL)
1739 if (copy)
1740 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1741 for (j = 0; j < XVECLEN (orig, i); j++)
1743 rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1744 evd, max_depth - 1);
1745 if (!result)
1746 return NULL;
1747 if (copy)
1748 XVECEXP (copy, i, j) = result;
1751 break;
1753 case 't':
1754 case 'w':
1755 case 'i':
1756 case 's':
1757 case 'S':
1758 case 'T':
1759 case 'u':
1760 case 'B':
1761 case '0':
1762 /* These are left unchanged. */
1763 break;
1765 default:
1766 gcc_unreachable ();
1769 if (evd->dummy)
1770 return orig;
1772 mode = GET_MODE (copy);
1773 /* If an operand has been simplified into CONST_INT, which doesn't
1774 have a mode and the mode isn't derivable from whole rtx's mode,
1775 try simplify_*_operation first with mode from original's operand
1776 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1777 scopy = copy;
1778 switch (GET_RTX_CLASS (code))
1780 case RTX_UNARY:
1781 if (CONST_INT_P (XEXP (copy, 0))
1782 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1784 scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1785 GET_MODE (XEXP (orig, 0)));
1786 if (scopy)
1787 return scopy;
1789 break;
1790 case RTX_COMM_ARITH:
1791 case RTX_BIN_ARITH:
1792 /* These expressions can derive operand modes from the whole rtx's mode. */
1793 break;
1794 case RTX_TERNARY:
1795 case RTX_BITFIELD_OPS:
1796 if (CONST_INT_P (XEXP (copy, 0))
1797 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1799 scopy = simplify_ternary_operation (code, mode,
1800 GET_MODE (XEXP (orig, 0)),
1801 XEXP (copy, 0), XEXP (copy, 1),
1802 XEXP (copy, 2));
1803 if (scopy)
1804 return scopy;
1806 break;
1807 case RTX_COMPARE:
1808 case RTX_COMM_COMPARE:
1809 if (CONST_INT_P (XEXP (copy, 0))
1810 && GET_MODE (XEXP (copy, 1)) == VOIDmode
1811 && (GET_MODE (XEXP (orig, 0)) != VOIDmode
1812 || GET_MODE (XEXP (orig, 1)) != VOIDmode))
1814 scopy = simplify_relational_operation (code, mode,
1815 (GET_MODE (XEXP (orig, 0))
1816 != VOIDmode)
1817 ? GET_MODE (XEXP (orig, 0))
1818 : GET_MODE (XEXP (orig, 1)),
1819 XEXP (copy, 0),
1820 XEXP (copy, 1));
1821 if (scopy)
1822 return scopy;
1824 break;
1825 default:
1826 break;
1828 scopy = simplify_rtx (copy);
1829 if (scopy)
1830 return scopy;
1831 return copy;
1834 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1835 with VALUE expressions. This way, it becomes independent of changes
1836 to registers and memory.
1837 X isn't actually modified; if modifications are needed, new rtl is
1838 allocated. However, the return value can share rtl with X.
1839 If X is within a MEM, MEMMODE must be the mode of the MEM. */
1842 cselib_subst_to_values (rtx x, machine_mode memmode)
1844 enum rtx_code code = GET_CODE (x);
1845 const char *fmt = GET_RTX_FORMAT (code);
1846 cselib_val *e;
1847 struct elt_list *l;
1848 rtx copy = x;
1849 int i;
1851 switch (code)
1853 case REG:
1854 l = REG_VALUES (REGNO (x));
1855 if (l && l->elt == NULL)
1856 l = l->next;
1857 for (; l; l = l->next)
1858 if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
1859 return l->elt->val_rtx;
1861 gcc_unreachable ();
1863 case MEM:
1864 e = cselib_lookup_mem (x, 0);
1865 /* This used to happen for autoincrements, but we deal with them
1866 properly now. Remove the if stmt for the next release. */
1867 if (! e)
1869 /* Assign a value that doesn't match any other. */
1870 e = new_cselib_val (next_uid, GET_MODE (x), x);
1872 return e->val_rtx;
1874 case ENTRY_VALUE:
1875 e = cselib_lookup (x, GET_MODE (x), 0, memmode);
1876 if (! e)
1877 break;
1878 return e->val_rtx;
1880 CASE_CONST_ANY:
1881 return x;
1883 case PRE_DEC:
1884 case PRE_INC:
1885 gcc_assert (memmode != VOIDmode);
1886 i = GET_MODE_SIZE (memmode);
1887 if (code == PRE_DEC)
1888 i = -i;
1889 return cselib_subst_to_values (plus_constant (GET_MODE (x),
1890 XEXP (x, 0), i),
1891 memmode);
1893 case PRE_MODIFY:
1894 gcc_assert (memmode != VOIDmode);
1895 return cselib_subst_to_values (XEXP (x, 1), memmode);
1897 case POST_DEC:
1898 case POST_INC:
1899 case POST_MODIFY:
1900 gcc_assert (memmode != VOIDmode);
1901 return cselib_subst_to_values (XEXP (x, 0), memmode);
1903 default:
1904 break;
1907 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1909 if (fmt[i] == 'e')
1911 rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
1913 if (t != XEXP (x, i))
1915 if (x == copy)
1916 copy = shallow_copy_rtx (x);
1917 XEXP (copy, i) = t;
1920 else if (fmt[i] == 'E')
1922 int j;
1924 for (j = 0; j < XVECLEN (x, i); j++)
1926 rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
1928 if (t != XVECEXP (x, i, j))
1930 if (XVEC (x, i) == XVEC (copy, i))
1932 if (x == copy)
1933 copy = shallow_copy_rtx (x);
1934 XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
1936 XVECEXP (copy, i, j) = t;
1942 return copy;
1945 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
1948 cselib_subst_to_values_from_insn (rtx x, machine_mode memmode, rtx_insn *insn)
1950 rtx ret;
1951 gcc_assert (!cselib_current_insn);
1952 cselib_current_insn = insn;
1953 ret = cselib_subst_to_values (x, memmode);
1954 cselib_current_insn = NULL;
1955 return ret;
1958 /* Look up the rtl expression X in our tables and return the value it
1959 has. If CREATE is zero, we return NULL if we don't know the value.
1960 Otherwise, we create a new one if possible, using mode MODE if X
1961 doesn't have a mode (i.e. because it's a constant). When X is part
1962 of an address, MEMMODE should be the mode of the enclosing MEM if
1963 we're tracking autoinc expressions. */
1965 static cselib_val *
1966 cselib_lookup_1 (rtx x, machine_mode mode,
1967 int create, machine_mode memmode)
1969 cselib_val **slot;
1970 cselib_val *e;
1971 unsigned int hashval;
1973 if (GET_MODE (x) != VOIDmode)
1974 mode = GET_MODE (x);
1976 if (GET_CODE (x) == VALUE)
1977 return CSELIB_VAL_PTR (x);
1979 if (REG_P (x))
1981 struct elt_list *l;
1982 unsigned int i = REGNO (x);
1984 l = REG_VALUES (i);
1985 if (l && l->elt == NULL)
1986 l = l->next;
1987 for (; l; l = l->next)
1988 if (mode == GET_MODE (l->elt->val_rtx))
1990 promote_debug_loc (l->elt->locs);
1991 return l->elt;
1994 if (! create)
1995 return 0;
1997 if (i < FIRST_PSEUDO_REGISTER)
1999 unsigned int n = hard_regno_nregs[i][mode];
2001 if (n > max_value_regs)
2002 max_value_regs = n;
2005 e = new_cselib_val (next_uid, GET_MODE (x), x);
2006 new_elt_loc_list (e, x);
2007 if (REG_VALUES (i) == 0)
2009 /* Maintain the invariant that the first entry of
2010 REG_VALUES, if present, must be the value used to set the
2011 register, or NULL. */
2012 used_regs[n_used_regs++] = i;
2013 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
2015 else if (cselib_preserve_constants
2016 && GET_MODE_CLASS (mode) == MODE_INT)
2018 /* During var-tracking, try harder to find equivalences
2019 for SUBREGs. If a setter sets say a DImode register
2020 and user uses that register only in SImode, add a lowpart
2021 subreg location. */
2022 struct elt_list *lwider = NULL;
2023 l = REG_VALUES (i);
2024 if (l && l->elt == NULL)
2025 l = l->next;
2026 for (; l; l = l->next)
2027 if (GET_MODE_CLASS (GET_MODE (l->elt->val_rtx)) == MODE_INT
2028 && GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2029 > GET_MODE_SIZE (mode)
2030 && (lwider == NULL
2031 || GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2032 < GET_MODE_SIZE (GET_MODE (lwider->elt->val_rtx))))
2034 struct elt_loc_list *el;
2035 if (i < FIRST_PSEUDO_REGISTER
2036 && hard_regno_nregs[i][GET_MODE (l->elt->val_rtx)] != 1)
2037 continue;
2038 for (el = l->elt->locs; el; el = el->next)
2039 if (!REG_P (el->loc))
2040 break;
2041 if (el)
2042 lwider = l;
2044 if (lwider)
2046 rtx sub = lowpart_subreg (mode, lwider->elt->val_rtx,
2047 GET_MODE (lwider->elt->val_rtx));
2048 if (sub)
2049 new_elt_loc_list (e, sub);
2052 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
2053 slot = cselib_find_slot (mode, x, e->hash, INSERT, memmode);
2054 *slot = e;
2055 return e;
2058 if (MEM_P (x))
2059 return cselib_lookup_mem (x, create);
2061 hashval = cselib_hash_rtx (x, create, memmode);
2062 /* Can't even create if hashing is not possible. */
2063 if (! hashval)
2064 return 0;
2066 slot = cselib_find_slot (mode, x, hashval,
2067 create ? INSERT : NO_INSERT, memmode);
2068 if (slot == 0)
2069 return 0;
2071 e = (cselib_val *) *slot;
2072 if (e)
2073 return e;
2075 e = new_cselib_val (hashval, mode, x);
2077 /* We have to fill the slot before calling cselib_subst_to_values:
2078 the hash table is inconsistent until we do so, and
2079 cselib_subst_to_values will need to do lookups. */
2080 *slot = e;
2081 new_elt_loc_list (e, cselib_subst_to_values (x, memmode));
2082 return e;
2085 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2087 cselib_val *
2088 cselib_lookup_from_insn (rtx x, machine_mode mode,
2089 int create, machine_mode memmode, rtx_insn *insn)
2091 cselib_val *ret;
2093 gcc_assert (!cselib_current_insn);
2094 cselib_current_insn = insn;
2096 ret = cselib_lookup (x, mode, create, memmode);
2098 cselib_current_insn = NULL;
2100 return ret;
2103 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2104 maintains invariants related with debug insns. */
2106 cselib_val *
2107 cselib_lookup (rtx x, machine_mode mode,
2108 int create, machine_mode memmode)
2110 cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
2112 /* ??? Should we return NULL if we're not to create an entry, the
2113 found loc is a debug loc and cselib_current_insn is not DEBUG?
2114 If so, we should also avoid converting val to non-DEBUG; probably
2115 easiest setting cselib_current_insn to NULL before the call
2116 above. */
2118 if (dump_file && (dump_flags & TDF_CSELIB))
2120 fputs ("cselib lookup ", dump_file);
2121 print_inline_rtx (dump_file, x, 2);
2122 fprintf (dump_file, " => %u:%u\n",
2123 ret ? ret->uid : 0,
2124 ret ? ret->hash : 0);
2127 return ret;
2130 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2131 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2132 is used to determine how many hard registers are being changed. If MODE
2133 is VOIDmode, then only REGNO is being changed; this is used when
2134 invalidating call clobbered registers across a call. */
2136 static void
2137 cselib_invalidate_regno (unsigned int regno, machine_mode mode)
2139 unsigned int endregno;
2140 unsigned int i;
2142 /* If we see pseudos after reload, something is _wrong_. */
2143 gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
2144 || reg_renumber[regno] < 0);
2146 /* Determine the range of registers that must be invalidated. For
2147 pseudos, only REGNO is affected. For hard regs, we must take MODE
2148 into account, and we must also invalidate lower register numbers
2149 if they contain values that overlap REGNO. */
2150 if (regno < FIRST_PSEUDO_REGISTER)
2152 gcc_assert (mode != VOIDmode);
2154 if (regno < max_value_regs)
2155 i = 0;
2156 else
2157 i = regno - max_value_regs;
2159 endregno = end_hard_regno (mode, regno);
2161 else
2163 i = regno;
2164 endregno = regno + 1;
2167 for (; i < endregno; i++)
2169 struct elt_list **l = &REG_VALUES (i);
2171 /* Go through all known values for this reg; if it overlaps the range
2172 we're invalidating, remove the value. */
2173 while (*l)
2175 cselib_val *v = (*l)->elt;
2176 bool had_locs;
2177 rtx_insn *setting_insn;
2178 struct elt_loc_list **p;
2179 unsigned int this_last = i;
2181 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
2182 this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
2184 if (this_last < regno || v == NULL
2185 || (v == cfa_base_preserved_val
2186 && i == cfa_base_preserved_regno))
2188 l = &(*l)->next;
2189 continue;
2192 /* We have an overlap. */
2193 if (*l == REG_VALUES (i))
2195 /* Maintain the invariant that the first entry of
2196 REG_VALUES, if present, must be the value used to set
2197 the register, or NULL. This is also nice because
2198 then we won't push the same regno onto user_regs
2199 multiple times. */
2200 (*l)->elt = NULL;
2201 l = &(*l)->next;
2203 else
2204 unchain_one_elt_list (l);
2206 v = canonical_cselib_val (v);
2208 had_locs = v->locs != NULL;
2209 setting_insn = v->locs ? v->locs->setting_insn : NULL;
2211 /* Now, we clear the mapping from value to reg. It must exist, so
2212 this code will crash intentionally if it doesn't. */
2213 for (p = &v->locs; ; p = &(*p)->next)
2215 rtx x = (*p)->loc;
2217 if (REG_P (x) && REGNO (x) == i)
2219 unchain_one_elt_loc_list (p);
2220 break;
2224 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2226 if (setting_insn && DEBUG_INSN_P (setting_insn))
2227 n_useless_debug_values++;
2228 else
2229 n_useless_values++;
2235 /* Invalidate any locations in the table which are changed because of a
2236 store to MEM_RTX. If this is called because of a non-const call
2237 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2239 static void
2240 cselib_invalidate_mem (rtx mem_rtx)
2242 cselib_val **vp, *v, *next;
2243 int num_mems = 0;
2244 rtx mem_addr;
2246 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2247 mem_rtx = canon_rtx (mem_rtx);
2249 vp = &first_containing_mem;
2250 for (v = *vp; v != &dummy_val; v = next)
2252 bool has_mem = false;
2253 struct elt_loc_list **p = &v->locs;
2254 bool had_locs = v->locs != NULL;
2255 rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
2257 while (*p)
2259 rtx x = (*p)->loc;
2260 cselib_val *addr;
2261 struct elt_list **mem_chain;
2263 /* MEMs may occur in locations only at the top level; below
2264 that every MEM or REG is substituted by its VALUE. */
2265 if (!MEM_P (x))
2267 p = &(*p)->next;
2268 continue;
2270 if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
2271 && ! canon_anti_dependence (x, false, mem_rtx,
2272 GET_MODE (mem_rtx), mem_addr))
2274 has_mem = true;
2275 num_mems++;
2276 p = &(*p)->next;
2277 continue;
2280 /* This one overlaps. */
2281 /* We must have a mapping from this MEM's address to the
2282 value (E). Remove that, too. */
2283 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
2284 addr = canonical_cselib_val (addr);
2285 gcc_checking_assert (v == canonical_cselib_val (v));
2286 mem_chain = &addr->addr_list;
2287 for (;;)
2289 cselib_val *canon = canonical_cselib_val ((*mem_chain)->elt);
2291 if (canon == v)
2293 unchain_one_elt_list (mem_chain);
2294 break;
2297 /* Record canonicalized elt. */
2298 (*mem_chain)->elt = canon;
2300 mem_chain = &(*mem_chain)->next;
2303 unchain_one_elt_loc_list (p);
2306 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2308 if (setting_insn && DEBUG_INSN_P (setting_insn))
2309 n_useless_debug_values++;
2310 else
2311 n_useless_values++;
2314 next = v->next_containing_mem;
2315 if (has_mem)
2317 *vp = v;
2318 vp = &(*vp)->next_containing_mem;
2320 else
2321 v->next_containing_mem = NULL;
2323 *vp = &dummy_val;
2326 /* Invalidate DEST, which is being assigned to or clobbered. */
2328 void
2329 cselib_invalidate_rtx (rtx dest)
2331 while (GET_CODE (dest) == SUBREG
2332 || GET_CODE (dest) == ZERO_EXTRACT
2333 || GET_CODE (dest) == STRICT_LOW_PART)
2334 dest = XEXP (dest, 0);
2336 if (REG_P (dest))
2337 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
2338 else if (MEM_P (dest))
2339 cselib_invalidate_mem (dest);
2342 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2344 static void
2345 cselib_invalidate_rtx_note_stores (rtx dest, const_rtx ignore ATTRIBUTE_UNUSED,
2346 void *data ATTRIBUTE_UNUSED)
2348 cselib_invalidate_rtx (dest);
2351 /* Record the result of a SET instruction. DEST is being set; the source
2352 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2353 describes its address. */
2355 static void
2356 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
2358 if (src_elt == 0 || side_effects_p (dest))
2359 return;
2361 if (REG_P (dest))
2363 unsigned int dreg = REGNO (dest);
2364 if (dreg < FIRST_PSEUDO_REGISTER)
2366 unsigned int n = REG_NREGS (dest);
2368 if (n > max_value_regs)
2369 max_value_regs = n;
2372 if (REG_VALUES (dreg) == 0)
2374 used_regs[n_used_regs++] = dreg;
2375 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2377 else
2379 /* The register should have been invalidated. */
2380 gcc_assert (REG_VALUES (dreg)->elt == 0);
2381 REG_VALUES (dreg)->elt = src_elt;
2384 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2385 n_useless_values--;
2386 new_elt_loc_list (src_elt, dest);
2388 else if (MEM_P (dest) && dest_addr_elt != 0
2389 && cselib_record_memory)
2391 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2392 n_useless_values--;
2393 add_mem_for_addr (dest_addr_elt, src_elt, dest);
2397 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2399 void
2400 cselib_add_permanent_equiv (cselib_val *elt, rtx x, rtx_insn *insn)
2402 cselib_val *nelt;
2403 rtx_insn *save_cselib_current_insn = cselib_current_insn;
2405 gcc_checking_assert (elt);
2406 gcc_checking_assert (PRESERVED_VALUE_P (elt->val_rtx));
2407 gcc_checking_assert (!side_effects_p (x));
2409 cselib_current_insn = insn;
2411 nelt = cselib_lookup (x, GET_MODE (elt->val_rtx), 1, VOIDmode);
2413 if (nelt != elt)
2415 cselib_any_perm_equivs = true;
2417 if (!PRESERVED_VALUE_P (nelt->val_rtx))
2418 cselib_preserve_value (nelt);
2420 new_elt_loc_list (nelt, elt->val_rtx);
2423 cselib_current_insn = save_cselib_current_insn;
2426 /* Return TRUE if any permanent equivalences have been recorded since
2427 the table was last initialized. */
2428 bool
2429 cselib_have_permanent_equivalences (void)
2431 return cselib_any_perm_equivs;
2434 /* There is no good way to determine how many elements there can be
2435 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2436 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2438 struct cselib_record_autoinc_data
2440 struct cselib_set *sets;
2441 int n_sets;
2444 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2445 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2447 static int
2448 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
2449 rtx dest, rtx src, rtx srcoff, void *arg)
2451 struct cselib_record_autoinc_data *data;
2452 data = (struct cselib_record_autoinc_data *)arg;
2454 data->sets[data->n_sets].dest = dest;
2456 if (srcoff)
2457 data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
2458 else
2459 data->sets[data->n_sets].src = src;
2461 data->n_sets++;
2463 return 0;
2466 /* Record the effects of any sets and autoincs in INSN. */
2467 static void
2468 cselib_record_sets (rtx_insn *insn)
2470 int n_sets = 0;
2471 int i;
2472 struct cselib_set sets[MAX_SETS];
2473 rtx body = PATTERN (insn);
2474 rtx cond = 0;
2475 int n_sets_before_autoinc;
2476 struct cselib_record_autoinc_data data;
2478 body = PATTERN (insn);
2479 if (GET_CODE (body) == COND_EXEC)
2481 cond = COND_EXEC_TEST (body);
2482 body = COND_EXEC_CODE (body);
2485 /* Find all sets. */
2486 if (GET_CODE (body) == SET)
2488 sets[0].src = SET_SRC (body);
2489 sets[0].dest = SET_DEST (body);
2490 n_sets = 1;
2492 else if (GET_CODE (body) == PARALLEL)
2494 /* Look through the PARALLEL and record the values being
2495 set, if possible. Also handle any CLOBBERs. */
2496 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2498 rtx x = XVECEXP (body, 0, i);
2500 if (GET_CODE (x) == SET)
2502 sets[n_sets].src = SET_SRC (x);
2503 sets[n_sets].dest = SET_DEST (x);
2504 n_sets++;
2509 if (n_sets == 1
2510 && MEM_P (sets[0].src)
2511 && !cselib_record_memory
2512 && MEM_READONLY_P (sets[0].src))
2514 rtx note = find_reg_equal_equiv_note (insn);
2516 if (note && CONSTANT_P (XEXP (note, 0)))
2517 sets[0].src = XEXP (note, 0);
2520 data.sets = sets;
2521 data.n_sets = n_sets_before_autoinc = n_sets;
2522 for_each_inc_dec (PATTERN (insn), cselib_record_autoinc_cb, &data);
2523 n_sets = data.n_sets;
2525 /* Look up the values that are read. Do this before invalidating the
2526 locations that are written. */
2527 for (i = 0; i < n_sets; i++)
2529 rtx dest = sets[i].dest;
2531 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2532 the low part after invalidating any knowledge about larger modes. */
2533 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2534 sets[i].dest = dest = XEXP (dest, 0);
2536 /* We don't know how to record anything but REG or MEM. */
2537 if (REG_P (dest)
2538 || (MEM_P (dest) && cselib_record_memory))
2540 rtx src = sets[i].src;
2541 if (cond)
2542 src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
2543 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
2544 if (MEM_P (dest))
2546 machine_mode address_mode = get_address_mode (dest);
2548 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
2549 address_mode, 1,
2550 GET_MODE (dest));
2552 else
2553 sets[i].dest_addr_elt = 0;
2557 if (cselib_record_sets_hook)
2558 cselib_record_sets_hook (insn, sets, n_sets);
2560 /* Invalidate all locations written by this insn. Note that the elts we
2561 looked up in the previous loop aren't affected, just some of their
2562 locations may go away. */
2563 note_stores (body, cselib_invalidate_rtx_note_stores, NULL);
2565 for (i = n_sets_before_autoinc; i < n_sets; i++)
2566 cselib_invalidate_rtx (sets[i].dest);
2568 /* If this is an asm, look for duplicate sets. This can happen when the
2569 user uses the same value as an output multiple times. This is valid
2570 if the outputs are not actually used thereafter. Treat this case as
2571 if the value isn't actually set. We do this by smashing the destination
2572 to pc_rtx, so that we won't record the value later. */
2573 if (n_sets >= 2 && asm_noperands (body) >= 0)
2575 for (i = 0; i < n_sets; i++)
2577 rtx dest = sets[i].dest;
2578 if (REG_P (dest) || MEM_P (dest))
2580 int j;
2581 for (j = i + 1; j < n_sets; j++)
2582 if (rtx_equal_p (dest, sets[j].dest))
2584 sets[i].dest = pc_rtx;
2585 sets[j].dest = pc_rtx;
2591 /* Now enter the equivalences in our tables. */
2592 for (i = 0; i < n_sets; i++)
2594 rtx dest = sets[i].dest;
2595 if (REG_P (dest)
2596 || (MEM_P (dest) && cselib_record_memory))
2597 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2601 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
2603 bool
2604 fp_setter_insn (rtx_insn *insn)
2606 rtx expr, pat = NULL_RTX;
2608 if (!RTX_FRAME_RELATED_P (insn))
2609 return false;
2611 expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2612 if (expr)
2613 pat = XEXP (expr, 0);
2614 if (!modified_in_p (hard_frame_pointer_rtx, pat ? pat : insn))
2615 return false;
2617 /* Don't return true for frame pointer restores in the epilogue. */
2618 if (find_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx))
2619 return false;
2620 return true;
2623 /* Record the effects of INSN. */
2625 void
2626 cselib_process_insn (rtx_insn *insn)
2628 int i;
2629 rtx x;
2631 cselib_current_insn = insn;
2633 /* Forget everything at a CODE_LABEL or a setjmp. */
2634 if ((LABEL_P (insn)
2635 || (CALL_P (insn)
2636 && find_reg_note (insn, REG_SETJMP, NULL)))
2637 && !cselib_preserve_constants)
2639 cselib_reset_table (next_uid);
2640 cselib_current_insn = NULL;
2641 return;
2644 if (! INSN_P (insn))
2646 cselib_current_insn = NULL;
2647 return;
2650 /* If this is a call instruction, forget anything stored in a
2651 call clobbered register, or, if this is not a const call, in
2652 memory. */
2653 if (CALL_P (insn))
2655 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2656 if (call_used_regs[i]
2657 || (REG_VALUES (i) && REG_VALUES (i)->elt
2658 && HARD_REGNO_CALL_PART_CLOBBERED (i,
2659 GET_MODE (REG_VALUES (i)->elt->val_rtx))))
2660 cselib_invalidate_regno (i, reg_raw_mode[i]);
2662 /* Since it is not clear how cselib is going to be used, be
2663 conservative here and treat looping pure or const functions
2664 as if they were regular functions. */
2665 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
2666 || !(RTL_CONST_OR_PURE_CALL_P (insn)))
2667 cselib_invalidate_mem (callmem);
2670 cselib_record_sets (insn);
2672 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2673 after we have processed the insn. */
2674 if (CALL_P (insn))
2676 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
2677 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
2678 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
2679 /* Flush evertything on setjmp. */
2680 if (cselib_preserve_constants
2681 && find_reg_note (insn, REG_SETJMP, NULL))
2683 cselib_preserve_only_values ();
2684 cselib_reset_table (next_uid);
2688 /* On setter of the hard frame pointer if frame_pointer_needed,
2689 invalidate stack_pointer_rtx, so that sp and {,h}fp based
2690 VALUEs are distinct. */
2691 if (reload_completed
2692 && frame_pointer_needed
2693 && fp_setter_insn (insn))
2694 cselib_invalidate_rtx (stack_pointer_rtx);
2696 cselib_current_insn = NULL;
2698 if (n_useless_values > MAX_USELESS_VALUES
2699 /* remove_useless_values is linear in the hash table size. Avoid
2700 quadratic behavior for very large hashtables with very few
2701 useless elements. */
2702 && ((unsigned int)n_useless_values
2703 > (cselib_hash_table->elements () - n_debug_values) / 4))
2704 remove_useless_values ();
2707 /* Initialize cselib for one pass. The caller must also call
2708 init_alias_analysis. */
2710 void
2711 cselib_init (int record_what)
2713 cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
2714 cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
2715 cselib_any_perm_equivs = false;
2717 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2718 see canon_true_dependence. This is only created once. */
2719 if (! callmem)
2720 callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2722 cselib_nregs = max_reg_num ();
2724 /* We preserve reg_values to allow expensive clearing of the whole thing.
2725 Reallocate it however if it happens to be too large. */
2726 if (!reg_values || reg_values_size < cselib_nregs
2727 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
2729 free (reg_values);
2730 /* Some space for newly emit instructions so we don't end up
2731 reallocating in between passes. */
2732 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
2733 reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
2735 used_regs = XNEWVEC (unsigned int, cselib_nregs);
2736 n_used_regs = 0;
2737 cselib_hash_table = new hash_table<cselib_hasher> (31);
2738 if (cselib_preserve_constants)
2739 cselib_preserved_hash_table = new hash_table<cselib_hasher> (31);
2740 next_uid = 1;
2743 /* Called when the current user is done with cselib. */
2745 void
2746 cselib_finish (void)
2748 bool preserved = cselib_preserve_constants;
2749 cselib_discard_hook = NULL;
2750 cselib_preserve_constants = false;
2751 cselib_any_perm_equivs = false;
2752 cfa_base_preserved_val = NULL;
2753 cfa_base_preserved_regno = INVALID_REGNUM;
2754 elt_list_pool.release ();
2755 elt_loc_list_pool.release ();
2756 cselib_val_pool.release ();
2757 value_pool.release ();
2758 cselib_clear_table ();
2759 delete cselib_hash_table;
2760 cselib_hash_table = NULL;
2761 if (preserved)
2762 delete cselib_preserved_hash_table;
2763 cselib_preserved_hash_table = NULL;
2764 free (used_regs);
2765 used_regs = 0;
2766 n_useless_values = 0;
2767 n_useless_debug_values = 0;
2768 n_debug_values = 0;
2769 next_uid = 0;
2772 /* Dump the cselib_val *X to FILE *OUT. */
2775 dump_cselib_val (cselib_val **x, FILE *out)
2777 cselib_val *v = *x;
2778 bool need_lf = true;
2780 print_inline_rtx (out, v->val_rtx, 0);
2782 if (v->locs)
2784 struct elt_loc_list *l = v->locs;
2785 if (need_lf)
2787 fputc ('\n', out);
2788 need_lf = false;
2790 fputs (" locs:", out);
2793 if (l->setting_insn)
2794 fprintf (out, "\n from insn %i ",
2795 INSN_UID (l->setting_insn));
2796 else
2797 fprintf (out, "\n ");
2798 print_inline_rtx (out, l->loc, 4);
2800 while ((l = l->next));
2801 fputc ('\n', out);
2803 else
2805 fputs (" no locs", out);
2806 need_lf = true;
2809 if (v->addr_list)
2811 struct elt_list *e = v->addr_list;
2812 if (need_lf)
2814 fputc ('\n', out);
2815 need_lf = false;
2817 fputs (" addr list:", out);
2820 fputs ("\n ", out);
2821 print_inline_rtx (out, e->elt->val_rtx, 2);
2823 while ((e = e->next));
2824 fputc ('\n', out);
2826 else
2828 fputs (" no addrs", out);
2829 need_lf = true;
2832 if (v->next_containing_mem == &dummy_val)
2833 fputs (" last mem\n", out);
2834 else if (v->next_containing_mem)
2836 fputs (" next mem ", out);
2837 print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
2838 fputc ('\n', out);
2840 else if (need_lf)
2841 fputc ('\n', out);
2843 return 1;
2846 /* Dump to OUT everything in the CSELIB table. */
2848 void
2849 dump_cselib_table (FILE *out)
2851 fprintf (out, "cselib hash table:\n");
2852 cselib_hash_table->traverse <FILE *, dump_cselib_val> (out);
2853 fprintf (out, "cselib preserved hash table:\n");
2854 cselib_preserved_hash_table->traverse <FILE *, dump_cselib_val> (out);
2855 if (first_containing_mem != &dummy_val)
2857 fputs ("first mem ", out);
2858 print_inline_rtx (out, first_containing_mem->val_rtx, 2);
2859 fputc ('\n', out);
2861 fprintf (out, "next uid %i\n", next_uid);
2864 #include "gt-cselib.h"