Add C++11 header <cuchar>.
[official-gcc.git] / gcc / cselib.c
blob214995927f54fe3c931957e47b819f9e255c9d32
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 "tree.h"
25 #include "rtl.h"
26 #include "df.h"
27 #include "alias.h"
28 #include "tm_p.h"
29 #include "regs.h"
30 #include "flags.h"
31 #include "insn-config.h"
32 #include "recog.h"
33 #include "emit-rtl.h"
34 #include "diagnostic-core.h"
35 #include "dumpfile.h"
36 #include "alloc-pool.h"
37 #include "cselib.h"
38 #include "valtrack.h"
39 #include "params.h"
40 #include "alloc-pool.h"
41 #include "target.h"
43 /* A list of cselib_val structures. */
44 struct elt_list
46 struct elt_list *next;
47 cselib_val *elt;
50 static bool cselib_record_memory;
51 static bool cselib_preserve_constants;
52 static bool cselib_any_perm_equivs;
53 static inline void promote_debug_loc (struct elt_loc_list *l);
54 static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
55 static void new_elt_loc_list (cselib_val *, rtx);
56 static void unchain_one_value (cselib_val *);
57 static void unchain_one_elt_list (struct elt_list **);
58 static void unchain_one_elt_loc_list (struct elt_loc_list **);
59 static void remove_useless_values (void);
60 static int rtx_equal_for_cselib_1 (rtx, rtx, machine_mode);
61 static unsigned int cselib_hash_rtx (rtx, int, machine_mode);
62 static cselib_val *new_cselib_val (unsigned int, machine_mode, rtx);
63 static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
64 static cselib_val *cselib_lookup_mem (rtx, int);
65 static void cselib_invalidate_regno (unsigned int, machine_mode);
66 static void cselib_invalidate_mem (rtx);
67 static void cselib_record_set (rtx, cselib_val *, cselib_val *);
68 static void cselib_record_sets (rtx_insn *);
70 struct expand_value_data
72 bitmap regs_active;
73 cselib_expand_callback callback;
74 void *callback_arg;
75 bool dummy;
78 static rtx cselib_expand_value_rtx_1 (rtx, struct expand_value_data *, int);
80 /* There are three ways in which cselib can look up an rtx:
81 - for a REG, the reg_values table (which is indexed by regno) is used
82 - for a MEM, we recursively look up its address and then follow the
83 addr_list of that value
84 - for everything else, we compute a hash value and go through the hash
85 table. Since different rtx's can still have the same hash value,
86 this involves walking the table entries for a given value and comparing
87 the locations of the entries with the rtx we are looking up. */
89 struct cselib_hasher : nofree_ptr_hash <cselib_val>
91 struct key {
92 /* The rtx value and its mode (needed separately for constant
93 integers). */
94 machine_mode mode;
95 rtx x;
96 /* The mode of the contaning MEM, if any, otherwise VOIDmode. */
97 machine_mode memmode;
99 typedef key *compare_type;
100 static inline hashval_t hash (const cselib_val *);
101 static inline bool equal (const cselib_val *, const key *);
104 /* The hash function for our hash table. The value is always computed with
105 cselib_hash_rtx when adding an element; this function just extracts the
106 hash value from a cselib_val structure. */
108 inline hashval_t
109 cselib_hasher::hash (const cselib_val *v)
111 return v->hash;
114 /* The equality test for our hash table. The first argument V is a table
115 element (i.e. a cselib_val), while the second arg X is an rtx. We know
116 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
117 CONST of an appropriate mode. */
119 inline bool
120 cselib_hasher::equal (const cselib_val *v, const key *x_arg)
122 struct elt_loc_list *l;
123 rtx x = x_arg->x;
124 machine_mode mode = x_arg->mode;
125 machine_mode memmode = x_arg->memmode;
127 if (mode != GET_MODE (v->val_rtx))
128 return false;
130 if (GET_CODE (x) == VALUE)
131 return x == v->val_rtx;
133 /* We don't guarantee that distinct rtx's have different hash values,
134 so we need to do a comparison. */
135 for (l = v->locs; l; l = l->next)
136 if (rtx_equal_for_cselib_1 (l->loc, x, memmode))
138 promote_debug_loc (l);
139 return true;
142 return false;
145 /* A table that enables us to look up elts by their value. */
146 static hash_table<cselib_hasher> *cselib_hash_table;
148 /* A table to hold preserved values. */
149 static hash_table<cselib_hasher> *cselib_preserved_hash_table;
151 /* This is a global so we don't have to pass this through every function.
152 It is used in new_elt_loc_list to set SETTING_INSN. */
153 static rtx_insn *cselib_current_insn;
155 /* The unique id that the next create value will take. */
156 static unsigned int next_uid;
158 /* The number of registers we had when the varrays were last resized. */
159 static unsigned int cselib_nregs;
161 /* Count values without known locations, or with only locations that
162 wouldn't have been known except for debug insns. Whenever this
163 grows too big, we remove these useless values from the table.
165 Counting values with only debug values is a bit tricky. We don't
166 want to increment n_useless_values when we create a value for a
167 debug insn, for this would get n_useless_values out of sync, but we
168 want increment it if all locs in the list that were ever referenced
169 in nondebug insns are removed from the list.
171 In the general case, once we do that, we'd have to stop accepting
172 nondebug expressions in the loc list, to avoid having two values
173 equivalent that, without debug insns, would have been made into
174 separate values. However, because debug insns never introduce
175 equivalences themselves (no assignments), the only means for
176 growing loc lists is through nondebug assignments. If the locs
177 also happen to be referenced in debug insns, it will work just fine.
179 A consequence of this is that there's at most one debug-only loc in
180 each loc list. If we keep it in the first entry, testing whether
181 we have a debug-only loc list takes O(1).
183 Furthermore, since any additional entry in a loc list containing a
184 debug loc would have to come from an assignment (nondebug) that
185 references both the initial debug loc and the newly-equivalent loc,
186 the initial debug loc would be promoted to a nondebug loc, and the
187 loc list would not contain debug locs any more.
189 So the only case we have to be careful with in order to keep
190 n_useless_values in sync between debug and nondebug compilations is
191 to avoid incrementing n_useless_values when removing the single loc
192 from a value that turns out to not appear outside debug values. We
193 increment n_useless_debug_values instead, and leave such values
194 alone until, for other reasons, we garbage-collect useless
195 values. */
196 static int n_useless_values;
197 static int n_useless_debug_values;
199 /* Count values whose locs have been taken exclusively from debug
200 insns for the entire life of the value. */
201 static int n_debug_values;
203 /* Number of useless values before we remove them from the hash table. */
204 #define MAX_USELESS_VALUES 32
206 /* This table maps from register number to values. It does not
207 contain pointers to cselib_val structures, but rather elt_lists.
208 The purpose is to be able to refer to the same register in
209 different modes. The first element of the list defines the mode in
210 which the register was set; if the mode is unknown or the value is
211 no longer valid in that mode, ELT will be NULL for the first
212 element. */
213 static struct elt_list **reg_values;
214 static unsigned int reg_values_size;
215 #define REG_VALUES(i) reg_values[i]
217 /* The largest number of hard regs used by any entry added to the
218 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
219 static unsigned int max_value_regs;
221 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
222 in cselib_clear_table() for fast emptying. */
223 static unsigned int *used_regs;
224 static unsigned int n_used_regs;
226 /* We pass this to cselib_invalidate_mem to invalidate all of
227 memory for a non-const call instruction. */
228 static GTY(()) rtx callmem;
230 /* Set by discard_useless_locs if it deleted the last location of any
231 value. */
232 static int values_became_useless;
234 /* Used as stop element of the containing_mem list so we can check
235 presence in the list by checking the next pointer. */
236 static cselib_val dummy_val;
238 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
239 that is constant through the whole function and should never be
240 eliminated. */
241 static cselib_val *cfa_base_preserved_val;
242 static unsigned int cfa_base_preserved_regno = INVALID_REGNUM;
244 /* Used to list all values that contain memory reference.
245 May or may not contain the useless values - the list is compacted
246 each time memory is invalidated. */
247 static cselib_val *first_containing_mem = &dummy_val;
249 static object_allocator<elt_list> elt_list_pool ("elt_list", 10);
250 static object_allocator<elt_loc_list> elt_loc_list_pool ("elt_loc_list", 10);
251 static object_allocator<cselib_val> cselib_val_pool ("cselib_val_list", 10);
253 static pool_allocator value_pool ("value", 100, RTX_CODE_SIZE (VALUE));
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 *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 elt_list *el = elt_list_pool.allocate ();
282 el->next = next;
283 el->elt = elt;
284 return el;
287 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
288 list. */
290 static inline void
291 new_elt_loc_list (cselib_val *val, rtx loc)
293 struct elt_loc_list *el, *next = val->locs;
295 gcc_checking_assert (!next || !next->setting_insn
296 || !DEBUG_INSN_P (next->setting_insn)
297 || cselib_current_insn == next->setting_insn);
299 /* If we're creating the first loc in a debug insn context, we've
300 just created a debug value. Count it. */
301 if (!next && cselib_current_insn && DEBUG_INSN_P (cselib_current_insn))
302 n_debug_values++;
304 val = canonical_cselib_val (val);
305 next = val->locs;
307 if (GET_CODE (loc) == VALUE)
309 loc = canonical_cselib_val (CSELIB_VAL_PTR (loc))->val_rtx;
311 gcc_checking_assert (PRESERVED_VALUE_P (loc)
312 == PRESERVED_VALUE_P (val->val_rtx));
314 if (val->val_rtx == loc)
315 return;
316 else if (val->uid > CSELIB_VAL_PTR (loc)->uid)
318 /* Reverse the insertion. */
319 new_elt_loc_list (CSELIB_VAL_PTR (loc), val->val_rtx);
320 return;
323 gcc_checking_assert (val->uid < CSELIB_VAL_PTR (loc)->uid);
325 if (CSELIB_VAL_PTR (loc)->locs)
327 /* Bring all locs from LOC to VAL. */
328 for (el = CSELIB_VAL_PTR (loc)->locs; el->next; el = el->next)
330 /* Adjust values that have LOC as canonical so that VAL
331 becomes their canonical. */
332 if (el->loc && GET_CODE (el->loc) == VALUE)
334 gcc_checking_assert (CSELIB_VAL_PTR (el->loc)->locs->loc
335 == loc);
336 CSELIB_VAL_PTR (el->loc)->locs->loc = val->val_rtx;
339 el->next = val->locs;
340 next = val->locs = CSELIB_VAL_PTR (loc)->locs;
343 if (CSELIB_VAL_PTR (loc)->addr_list)
345 /* Bring in addr_list into canonical node. */
346 struct elt_list *last = CSELIB_VAL_PTR (loc)->addr_list;
347 while (last->next)
348 last = last->next;
349 last->next = val->addr_list;
350 val->addr_list = CSELIB_VAL_PTR (loc)->addr_list;
351 CSELIB_VAL_PTR (loc)->addr_list = NULL;
354 if (CSELIB_VAL_PTR (loc)->next_containing_mem != NULL
355 && val->next_containing_mem == NULL)
357 /* Add VAL to the containing_mem list after LOC. LOC will
358 be removed when we notice it doesn't contain any
359 MEMs. */
360 val->next_containing_mem = CSELIB_VAL_PTR (loc)->next_containing_mem;
361 CSELIB_VAL_PTR (loc)->next_containing_mem = val;
364 /* Chain LOC back to VAL. */
365 el = elt_loc_list_pool.allocate ();
366 el->loc = val->val_rtx;
367 el->setting_insn = cselib_current_insn;
368 el->next = NULL;
369 CSELIB_VAL_PTR (loc)->locs = el;
372 el = elt_loc_list_pool.allocate ();
373 el->loc = loc;
374 el->setting_insn = cselib_current_insn;
375 el->next = next;
376 val->locs = el;
379 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
380 originating from a debug insn, maintaining the debug values
381 count. */
383 static inline void
384 promote_debug_loc (struct elt_loc_list *l)
386 if (l && l->setting_insn && DEBUG_INSN_P (l->setting_insn)
387 && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
389 n_debug_values--;
390 l->setting_insn = cselib_current_insn;
391 if (cselib_preserve_constants && l->next)
393 gcc_assert (l->next->setting_insn
394 && DEBUG_INSN_P (l->next->setting_insn)
395 && !l->next->next);
396 l->next->setting_insn = cselib_current_insn;
398 else
399 gcc_assert (!l->next);
403 /* The elt_list at *PL is no longer needed. Unchain it and free its
404 storage. */
406 static inline void
407 unchain_one_elt_list (struct elt_list **pl)
409 struct elt_list *l = *pl;
411 *pl = l->next;
412 elt_list_pool.remove (l);
415 /* Likewise for elt_loc_lists. */
417 static void
418 unchain_one_elt_loc_list (struct elt_loc_list **pl)
420 struct elt_loc_list *l = *pl;
422 *pl = l->next;
423 elt_loc_list_pool.remove (l);
426 /* Likewise for cselib_vals. This also frees the addr_list associated with
427 V. */
429 static void
430 unchain_one_value (cselib_val *v)
432 while (v->addr_list)
433 unchain_one_elt_list (&v->addr_list);
435 cselib_val_pool.remove (v);
438 /* Remove all entries from the hash table. Also used during
439 initialization. */
441 void
442 cselib_clear_table (void)
444 cselib_reset_table (1);
447 /* Return TRUE if V is a constant, a function invariant or a VALUE
448 equivalence; FALSE otherwise. */
450 static bool
451 invariant_or_equiv_p (cselib_val *v)
453 struct elt_loc_list *l;
455 if (v == cfa_base_preserved_val)
456 return true;
458 /* Keep VALUE equivalences around. */
459 for (l = v->locs; l; l = l->next)
460 if (GET_CODE (l->loc) == VALUE)
461 return true;
463 if (v->locs != NULL
464 && v->locs->next == NULL)
466 if (CONSTANT_P (v->locs->loc)
467 && (GET_CODE (v->locs->loc) != CONST
468 || !references_value_p (v->locs->loc, 0)))
469 return true;
470 /* Although a debug expr may be bound to different expressions,
471 we can preserve it as if it was constant, to get unification
472 and proper merging within var-tracking. */
473 if (GET_CODE (v->locs->loc) == DEBUG_EXPR
474 || GET_CODE (v->locs->loc) == DEBUG_IMPLICIT_PTR
475 || GET_CODE (v->locs->loc) == ENTRY_VALUE
476 || GET_CODE (v->locs->loc) == DEBUG_PARAMETER_REF)
477 return true;
479 /* (plus (value V) (const_int C)) is invariant iff V is invariant. */
480 if (GET_CODE (v->locs->loc) == PLUS
481 && CONST_INT_P (XEXP (v->locs->loc, 1))
482 && GET_CODE (XEXP (v->locs->loc, 0)) == VALUE
483 && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v->locs->loc, 0))))
484 return true;
487 return false;
490 /* Remove from hash table all VALUEs except constants, function
491 invariants and VALUE equivalences. */
494 preserve_constants_and_equivs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
496 cselib_val *v = *x;
498 if (invariant_or_equiv_p (v))
500 cselib_hasher::key lookup = {
501 GET_MODE (v->val_rtx), v->val_rtx, VOIDmode
503 cselib_val **slot
504 = cselib_preserved_hash_table->find_slot_with_hash (&lookup,
505 v->hash, INSERT);
506 gcc_assert (!*slot);
507 *slot = v;
510 cselib_hash_table->clear_slot (x);
512 return 1;
515 /* Remove all entries from the hash table, arranging for the next
516 value to be numbered NUM. */
518 void
519 cselib_reset_table (unsigned int num)
521 unsigned int i;
523 max_value_regs = 0;
525 if (cfa_base_preserved_val)
527 unsigned int regno = cfa_base_preserved_regno;
528 unsigned int new_used_regs = 0;
529 for (i = 0; i < n_used_regs; i++)
530 if (used_regs[i] == regno)
532 new_used_regs = 1;
533 continue;
535 else
536 REG_VALUES (used_regs[i]) = 0;
537 gcc_assert (new_used_regs == 1);
538 n_used_regs = new_used_regs;
539 used_regs[0] = regno;
540 max_value_regs
541 = hard_regno_nregs[regno][GET_MODE (cfa_base_preserved_val->locs->loc)];
543 else
545 for (i = 0; i < n_used_regs; i++)
546 REG_VALUES (used_regs[i]) = 0;
547 n_used_regs = 0;
550 if (cselib_preserve_constants)
551 cselib_hash_table->traverse <void *, preserve_constants_and_equivs>
552 (NULL);
553 else
555 cselib_hash_table->empty ();
556 gcc_checking_assert (!cselib_any_perm_equivs);
559 n_useless_values = 0;
560 n_useless_debug_values = 0;
561 n_debug_values = 0;
563 next_uid = num;
565 first_containing_mem = &dummy_val;
568 /* Return the number of the next value that will be generated. */
570 unsigned int
571 cselib_get_next_uid (void)
573 return next_uid;
576 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
577 INSERTing if requested. When X is part of the address of a MEM,
578 MEMMODE should specify the mode of the MEM. */
580 static cselib_val **
581 cselib_find_slot (machine_mode mode, rtx x, hashval_t hash,
582 enum insert_option insert, machine_mode memmode)
584 cselib_val **slot = NULL;
585 cselib_hasher::key lookup = { mode, x, memmode };
586 if (cselib_preserve_constants)
587 slot = cselib_preserved_hash_table->find_slot_with_hash (&lookup, hash,
588 NO_INSERT);
589 if (!slot)
590 slot = cselib_hash_table->find_slot_with_hash (&lookup, hash, insert);
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_insn *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 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, 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, 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_UNIQUE:
949 case DEBUG_EXPR:
950 return 0;
952 case DEBUG_IMPLICIT_PTR:
953 return DEBUG_IMPLICIT_PTR_DECL (x)
954 == DEBUG_IMPLICIT_PTR_DECL (y);
956 case DEBUG_PARAMETER_REF:
957 return DEBUG_PARAMETER_REF_DECL (x)
958 == DEBUG_PARAMETER_REF_DECL (y);
960 case ENTRY_VALUE:
961 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
962 use rtx_equal_for_cselib_1 to compare the operands. */
963 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
965 case LABEL_REF:
966 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
968 case REG:
969 return REGNO (x) == REGNO (y);
971 case MEM:
972 /* We have to compare any autoinc operations in the addresses
973 using this MEM's mode. */
974 return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x));
976 default:
977 break;
980 code = GET_CODE (x);
981 fmt = GET_RTX_FORMAT (code);
983 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
985 int j;
987 switch (fmt[i])
989 case 'w':
990 if (XWINT (x, i) != XWINT (y, i))
991 return 0;
992 break;
994 case 'n':
995 case 'i':
996 if (XINT (x, i) != XINT (y, i))
997 return 0;
998 break;
1000 case 'V':
1001 case 'E':
1002 /* Two vectors must have the same length. */
1003 if (XVECLEN (x, i) != XVECLEN (y, i))
1004 return 0;
1006 /* And the corresponding elements must match. */
1007 for (j = 0; j < XVECLEN (x, i); j++)
1008 if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
1009 XVECEXP (y, i, j), memmode))
1010 return 0;
1011 break;
1013 case 'e':
1014 if (i == 1
1015 && targetm.commutative_p (x, UNKNOWN)
1016 && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode)
1017 && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode))
1018 return 1;
1019 if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode))
1020 return 0;
1021 break;
1023 case 'S':
1024 case 's':
1025 if (strcmp (XSTR (x, i), XSTR (y, i)))
1026 return 0;
1027 break;
1029 case 'u':
1030 /* These are just backpointers, so they don't matter. */
1031 break;
1033 case '0':
1034 case 't':
1035 break;
1037 /* It is believed that rtx's at this level will never
1038 contain anything but integers and other rtx's,
1039 except for within LABEL_REFs and SYMBOL_REFs. */
1040 default:
1041 gcc_unreachable ();
1044 return 1;
1047 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1048 For registers and memory locations, we look up their cselib_val structure
1049 and return its VALUE element.
1050 Possible reasons for return 0 are: the object is volatile, or we couldn't
1051 find a register or memory location in the table and CREATE is zero. If
1052 CREATE is nonzero, table elts are created for regs and mem.
1053 N.B. this hash function returns the same hash value for RTXes that
1054 differ only in the order of operands, thus it is suitable for comparisons
1055 that take commutativity into account.
1056 If we wanted to also support associative rules, we'd have to use a different
1057 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1058 MEMMODE indicates the mode of an enclosing MEM, and it's only
1059 used to compute autoinc values.
1060 We used to have a MODE argument for hashing for CONST_INTs, but that
1061 didn't make sense, since it caused spurious hash differences between
1062 (set (reg:SI 1) (const_int))
1063 (plus:SI (reg:SI 2) (reg:SI 1))
1065 (plus:SI (reg:SI 2) (const_int))
1066 If the mode is important in any context, it must be checked specifically
1067 in a comparison anyway, since relying on hash differences is unsafe. */
1069 static unsigned int
1070 cselib_hash_rtx (rtx x, int create, machine_mode memmode)
1072 cselib_val *e;
1073 int i, j;
1074 enum rtx_code code;
1075 const char *fmt;
1076 unsigned int hash = 0;
1078 code = GET_CODE (x);
1079 hash += (unsigned) code + (unsigned) GET_MODE (x);
1081 switch (code)
1083 case VALUE:
1084 e = CSELIB_VAL_PTR (x);
1085 return e->hash;
1087 case MEM:
1088 case REG:
1089 e = cselib_lookup (x, GET_MODE (x), create, memmode);
1090 if (! e)
1091 return 0;
1093 return e->hash;
1095 case DEBUG_EXPR:
1096 hash += ((unsigned) DEBUG_EXPR << 7)
1097 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
1098 return hash ? hash : (unsigned int) DEBUG_EXPR;
1100 case DEBUG_IMPLICIT_PTR:
1101 hash += ((unsigned) DEBUG_IMPLICIT_PTR << 7)
1102 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x));
1103 return hash ? hash : (unsigned int) DEBUG_IMPLICIT_PTR;
1105 case DEBUG_PARAMETER_REF:
1106 hash += ((unsigned) DEBUG_PARAMETER_REF << 7)
1107 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x));
1108 return hash ? hash : (unsigned int) DEBUG_PARAMETER_REF;
1110 case ENTRY_VALUE:
1111 /* ENTRY_VALUEs are function invariant, thus try to avoid
1112 recursing on argument if ENTRY_VALUE is one of the
1113 forms emitted by expand_debug_expr, otherwise
1114 ENTRY_VALUE hash would depend on the current value
1115 in some register or memory. */
1116 if (REG_P (ENTRY_VALUE_EXP (x)))
1117 hash += (unsigned int) REG
1118 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x))
1119 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x));
1120 else if (MEM_P (ENTRY_VALUE_EXP (x))
1121 && REG_P (XEXP (ENTRY_VALUE_EXP (x), 0)))
1122 hash += (unsigned int) MEM
1123 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x), 0))
1124 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x), 0));
1125 else
1126 hash += cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode);
1127 return hash ? hash : (unsigned int) ENTRY_VALUE;
1129 case CONST_INT:
1130 hash += ((unsigned) CONST_INT << 7) + UINTVAL (x);
1131 return hash ? hash : (unsigned int) CONST_INT;
1133 case CONST_WIDE_INT:
1134 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
1135 hash += CONST_WIDE_INT_ELT (x, i);
1136 return hash;
1138 case CONST_DOUBLE:
1139 /* This is like the general case, except that it only counts
1140 the integers representing the constant. */
1141 hash += (unsigned) code + (unsigned) GET_MODE (x);
1142 if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (x) == VOIDmode)
1143 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1144 + (unsigned) CONST_DOUBLE_HIGH (x));
1145 else
1146 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
1147 return hash ? hash : (unsigned int) CONST_DOUBLE;
1149 case CONST_FIXED:
1150 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1151 hash += fixed_hash (CONST_FIXED_VALUE (x));
1152 return hash ? hash : (unsigned int) CONST_FIXED;
1154 case CONST_VECTOR:
1156 int units;
1157 rtx elt;
1159 units = CONST_VECTOR_NUNITS (x);
1161 for (i = 0; i < units; ++i)
1163 elt = CONST_VECTOR_ELT (x, i);
1164 hash += cselib_hash_rtx (elt, 0, memmode);
1167 return hash;
1170 /* Assume there is only one rtx object for any given label. */
1171 case LABEL_REF:
1172 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1173 differences and differences between each stage's debugging dumps. */
1174 hash += (((unsigned int) LABEL_REF << 7)
1175 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x)));
1176 return hash ? hash : (unsigned int) LABEL_REF;
1178 case SYMBOL_REF:
1180 /* Don't hash on the symbol's address to avoid bootstrap differences.
1181 Different hash values may cause expressions to be recorded in
1182 different orders and thus different registers to be used in the
1183 final assembler. This also avoids differences in the dump files
1184 between various stages. */
1185 unsigned int h = 0;
1186 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1188 while (*p)
1189 h += (h << 7) + *p++; /* ??? revisit */
1191 hash += ((unsigned int) SYMBOL_REF << 7) + h;
1192 return hash ? hash : (unsigned int) SYMBOL_REF;
1195 case PRE_DEC:
1196 case PRE_INC:
1197 /* We can't compute these without knowing the MEM mode. */
1198 gcc_assert (memmode != VOIDmode);
1199 i = GET_MODE_SIZE (memmode);
1200 if (code == PRE_DEC)
1201 i = -i;
1202 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1203 like (mem:MEMMODE (plus (reg) (const_int I))). */
1204 hash += (unsigned) PLUS - (unsigned)code
1205 + cselib_hash_rtx (XEXP (x, 0), create, memmode)
1206 + cselib_hash_rtx (GEN_INT (i), create, memmode);
1207 return hash ? hash : 1 + (unsigned) PLUS;
1209 case PRE_MODIFY:
1210 gcc_assert (memmode != VOIDmode);
1211 return cselib_hash_rtx (XEXP (x, 1), create, memmode);
1213 case POST_DEC:
1214 case POST_INC:
1215 case POST_MODIFY:
1216 gcc_assert (memmode != VOIDmode);
1217 return cselib_hash_rtx (XEXP (x, 0), create, memmode);
1219 case PC:
1220 case CC0:
1221 case CALL:
1222 case UNSPEC_VOLATILE:
1223 return 0;
1225 case ASM_OPERANDS:
1226 if (MEM_VOLATILE_P (x))
1227 return 0;
1229 break;
1231 default:
1232 break;
1235 i = GET_RTX_LENGTH (code) - 1;
1236 fmt = GET_RTX_FORMAT (code);
1237 for (; i >= 0; i--)
1239 switch (fmt[i])
1241 case 'e':
1243 rtx tem = XEXP (x, i);
1244 unsigned int tem_hash = cselib_hash_rtx (tem, create, memmode);
1246 if (tem_hash == 0)
1247 return 0;
1249 hash += tem_hash;
1251 break;
1252 case 'E':
1253 for (j = 0; j < XVECLEN (x, i); j++)
1255 unsigned int tem_hash
1256 = cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
1258 if (tem_hash == 0)
1259 return 0;
1261 hash += tem_hash;
1263 break;
1265 case 's':
1267 const unsigned char *p = (const unsigned char *) XSTR (x, i);
1269 if (p)
1270 while (*p)
1271 hash += *p++;
1272 break;
1275 case 'i':
1276 hash += XINT (x, i);
1277 break;
1279 case '0':
1280 case 't':
1281 /* unused */
1282 break;
1284 default:
1285 gcc_unreachable ();
1289 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
1292 /* Create a new value structure for VALUE and initialize it. The mode of the
1293 value is MODE. */
1295 static inline cselib_val *
1296 new_cselib_val (unsigned int hash, machine_mode mode, rtx x)
1298 cselib_val *e = cselib_val_pool.allocate ();
1300 gcc_assert (hash);
1301 gcc_assert (next_uid);
1303 e->hash = hash;
1304 e->uid = next_uid++;
1305 /* We use an alloc pool to allocate this RTL construct because it
1306 accounts for about 8% of the overall memory usage. We know
1307 precisely when we can have VALUE RTXen (when cselib is active)
1308 so we don't need to put them in garbage collected memory.
1309 ??? Why should a VALUE be an RTX in the first place? */
1310 e->val_rtx = (rtx_def*) value_pool.allocate ();
1311 memset (e->val_rtx, 0, RTX_HDR_SIZE);
1312 PUT_CODE (e->val_rtx, VALUE);
1313 PUT_MODE (e->val_rtx, mode);
1314 CSELIB_VAL_PTR (e->val_rtx) = e;
1315 e->addr_list = 0;
1316 e->locs = 0;
1317 e->next_containing_mem = 0;
1319 if (dump_file && (dump_flags & TDF_CSELIB))
1321 fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
1322 if (flag_dump_noaddr || flag_dump_unnumbered)
1323 fputs ("# ", dump_file);
1324 else
1325 fprintf (dump_file, "%p ", (void*)e);
1326 print_rtl_single (dump_file, x);
1327 fputc ('\n', dump_file);
1330 return e;
1333 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1334 contains the data at this address. X is a MEM that represents the
1335 value. Update the two value structures to represent this situation. */
1337 static void
1338 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
1340 struct elt_loc_list *l;
1342 addr_elt = canonical_cselib_val (addr_elt);
1343 mem_elt = canonical_cselib_val (mem_elt);
1345 /* Avoid duplicates. */
1346 for (l = mem_elt->locs; l; l = l->next)
1347 if (MEM_P (l->loc)
1348 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
1350 promote_debug_loc (l);
1351 return;
1354 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
1355 new_elt_loc_list (mem_elt,
1356 replace_equiv_address_nv (x, addr_elt->val_rtx));
1357 if (mem_elt->next_containing_mem == NULL)
1359 mem_elt->next_containing_mem = first_containing_mem;
1360 first_containing_mem = mem_elt;
1364 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1365 If CREATE, make a new one if we haven't seen it before. */
1367 static cselib_val *
1368 cselib_lookup_mem (rtx x, int create)
1370 machine_mode mode = GET_MODE (x);
1371 machine_mode addr_mode;
1372 cselib_val **slot;
1373 cselib_val *addr;
1374 cselib_val *mem_elt;
1375 struct elt_list *l;
1377 if (MEM_VOLATILE_P (x) || mode == BLKmode
1378 || !cselib_record_memory
1379 || (FLOAT_MODE_P (mode) && flag_float_store))
1380 return 0;
1382 addr_mode = GET_MODE (XEXP (x, 0));
1383 if (addr_mode == VOIDmode)
1384 addr_mode = Pmode;
1386 /* Look up the value for the address. */
1387 addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
1388 if (! addr)
1389 return 0;
1391 addr = canonical_cselib_val (addr);
1392 /* Find a value that describes a value of our mode at that address. */
1393 for (l = addr->addr_list; l; l = l->next)
1394 if (GET_MODE (l->elt->val_rtx) == mode)
1396 promote_debug_loc (l->elt->locs);
1397 return l->elt;
1400 if (! create)
1401 return 0;
1403 mem_elt = new_cselib_val (next_uid, mode, x);
1404 add_mem_for_addr (addr, mem_elt, x);
1405 slot = cselib_find_slot (mode, x, mem_elt->hash, INSERT, VOIDmode);
1406 *slot = mem_elt;
1407 return mem_elt;
1410 /* Search through the possible substitutions in P. We prefer a non reg
1411 substitution because this allows us to expand the tree further. If
1412 we find, just a reg, take the lowest regno. There may be several
1413 non-reg results, we just take the first one because they will all
1414 expand to the same place. */
1416 static rtx
1417 expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1418 int max_depth)
1420 rtx reg_result = NULL;
1421 unsigned int regno = UINT_MAX;
1422 struct elt_loc_list *p_in = p;
1424 for (; p; p = p->next)
1426 /* Return these right away to avoid returning stack pointer based
1427 expressions for frame pointer and vice versa, which is something
1428 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1429 for more details. */
1430 if (REG_P (p->loc)
1431 && (REGNO (p->loc) == STACK_POINTER_REGNUM
1432 || REGNO (p->loc) == FRAME_POINTER_REGNUM
1433 || REGNO (p->loc) == HARD_FRAME_POINTER_REGNUM
1434 || REGNO (p->loc) == cfa_base_preserved_regno))
1435 return p->loc;
1436 /* Avoid infinite recursion trying to expand a reg into a
1437 the same reg. */
1438 if ((REG_P (p->loc))
1439 && (REGNO (p->loc) < regno)
1440 && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
1442 reg_result = p->loc;
1443 regno = REGNO (p->loc);
1445 /* Avoid infinite recursion and do not try to expand the
1446 value. */
1447 else if (GET_CODE (p->loc) == VALUE
1448 && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1449 continue;
1450 else if (!REG_P (p->loc))
1452 rtx result, note;
1453 if (dump_file && (dump_flags & TDF_CSELIB))
1455 print_inline_rtx (dump_file, p->loc, 0);
1456 fprintf (dump_file, "\n");
1458 if (GET_CODE (p->loc) == LO_SUM
1459 && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1460 && p->setting_insn
1461 && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1462 && XEXP (note, 0) == XEXP (p->loc, 1))
1463 return XEXP (p->loc, 1);
1464 result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
1465 if (result)
1466 return result;
1471 if (regno != UINT_MAX)
1473 rtx result;
1474 if (dump_file && (dump_flags & TDF_CSELIB))
1475 fprintf (dump_file, "r%d\n", regno);
1477 result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
1478 if (result)
1479 return result;
1482 if (dump_file && (dump_flags & TDF_CSELIB))
1484 if (reg_result)
1486 print_inline_rtx (dump_file, reg_result, 0);
1487 fprintf (dump_file, "\n");
1489 else
1490 fprintf (dump_file, "NULL\n");
1492 return reg_result;
1496 /* Forward substitute and expand an expression out to its roots.
1497 This is the opposite of common subexpression. Because local value
1498 numbering is such a weak optimization, the expanded expression is
1499 pretty much unique (not from a pointer equals point of view but
1500 from a tree shape point of view.
1502 This function returns NULL if the expansion fails. The expansion
1503 will fail if there is no value number for one of the operands or if
1504 one of the operands has been overwritten between the current insn
1505 and the beginning of the basic block. For instance x has no
1506 expansion in:
1508 r1 <- r1 + 3
1509 x <- r1 + 8
1511 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1512 It is clear on return. */
1515 cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
1517 struct expand_value_data evd;
1519 evd.regs_active = regs_active;
1520 evd.callback = NULL;
1521 evd.callback_arg = NULL;
1522 evd.dummy = false;
1524 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1527 /* Same as cselib_expand_value_rtx, but using a callback to try to
1528 resolve some expressions. The CB function should return ORIG if it
1529 can't or does not want to deal with a certain RTX. Any other
1530 return value, including NULL, will be used as the expansion for
1531 VALUE, without any further changes. */
1534 cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1535 cselib_expand_callback cb, void *data)
1537 struct expand_value_data evd;
1539 evd.regs_active = regs_active;
1540 evd.callback = cb;
1541 evd.callback_arg = data;
1542 evd.dummy = false;
1544 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1547 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1548 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1549 would return NULL or non-NULL, without allocating new rtx. */
1551 bool
1552 cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1553 cselib_expand_callback cb, void *data)
1555 struct expand_value_data evd;
1557 evd.regs_active = regs_active;
1558 evd.callback = cb;
1559 evd.callback_arg = data;
1560 evd.dummy = true;
1562 return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1565 /* Internal implementation of cselib_expand_value_rtx and
1566 cselib_expand_value_rtx_cb. */
1568 static rtx
1569 cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1570 int max_depth)
1572 rtx copy, scopy;
1573 int i, j;
1574 RTX_CODE code;
1575 const char *format_ptr;
1576 machine_mode mode;
1578 code = GET_CODE (orig);
1580 /* For the context of dse, if we end up expand into a huge tree, we
1581 will not have a useful address, so we might as well just give up
1582 quickly. */
1583 if (max_depth <= 0)
1584 return NULL;
1586 switch (code)
1588 case REG:
1590 struct elt_list *l = REG_VALUES (REGNO (orig));
1592 if (l && l->elt == NULL)
1593 l = l->next;
1594 for (; l; l = l->next)
1595 if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1597 rtx result;
1598 unsigned regno = REGNO (orig);
1600 /* The only thing that we are not willing to do (this
1601 is requirement of dse and if others potential uses
1602 need this function we should add a parm to control
1603 it) is that we will not substitute the
1604 STACK_POINTER_REGNUM, FRAME_POINTER or the
1605 HARD_FRAME_POINTER.
1607 These expansions confuses the code that notices that
1608 stores into the frame go dead at the end of the
1609 function and that the frame is not effected by calls
1610 to subroutines. If you allow the
1611 STACK_POINTER_REGNUM substitution, then dse will
1612 think that parameter pushing also goes dead which is
1613 wrong. If you allow the FRAME_POINTER or the
1614 HARD_FRAME_POINTER then you lose the opportunity to
1615 make the frame assumptions. */
1616 if (regno == STACK_POINTER_REGNUM
1617 || regno == FRAME_POINTER_REGNUM
1618 || regno == HARD_FRAME_POINTER_REGNUM
1619 || regno == cfa_base_preserved_regno)
1620 return orig;
1622 bitmap_set_bit (evd->regs_active, regno);
1624 if (dump_file && (dump_flags & TDF_CSELIB))
1625 fprintf (dump_file, "expanding: r%d into: ", regno);
1627 result = expand_loc (l->elt->locs, evd, max_depth);
1628 bitmap_clear_bit (evd->regs_active, regno);
1630 if (result)
1631 return result;
1632 else
1633 return orig;
1637 CASE_CONST_ANY:
1638 case SYMBOL_REF:
1639 case CODE_LABEL:
1640 case PC:
1641 case CC0:
1642 case SCRATCH:
1643 /* SCRATCH must be shared because they represent distinct values. */
1644 return orig;
1645 case CLOBBER:
1646 if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1647 return orig;
1648 break;
1650 case CONST:
1651 if (shared_const_p (orig))
1652 return orig;
1653 break;
1655 case SUBREG:
1657 rtx subreg;
1659 if (evd->callback)
1661 subreg = evd->callback (orig, evd->regs_active, max_depth,
1662 evd->callback_arg);
1663 if (subreg != orig)
1664 return subreg;
1667 subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1668 max_depth - 1);
1669 if (!subreg)
1670 return NULL;
1671 scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1672 GET_MODE (SUBREG_REG (orig)),
1673 SUBREG_BYTE (orig));
1674 if (scopy == NULL
1675 || (GET_CODE (scopy) == SUBREG
1676 && !REG_P (SUBREG_REG (scopy))
1677 && !MEM_P (SUBREG_REG (scopy))))
1678 return NULL;
1680 return scopy;
1683 case VALUE:
1685 rtx result;
1687 if (dump_file && (dump_flags & TDF_CSELIB))
1689 fputs ("\nexpanding ", dump_file);
1690 print_rtl_single (dump_file, orig);
1691 fputs (" into...", dump_file);
1694 if (evd->callback)
1696 result = evd->callback (orig, evd->regs_active, max_depth,
1697 evd->callback_arg);
1699 if (result != orig)
1700 return result;
1703 result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
1704 return result;
1707 case DEBUG_EXPR:
1708 if (evd->callback)
1709 return evd->callback (orig, evd->regs_active, max_depth,
1710 evd->callback_arg);
1711 return orig;
1713 default:
1714 break;
1717 /* Copy the various flags, fields, and other information. We assume
1718 that all fields need copying, and then clear the fields that should
1719 not be copied. That is the sensible default behavior, and forces
1720 us to explicitly document why we are *not* copying a flag. */
1721 if (evd->dummy)
1722 copy = NULL;
1723 else
1724 copy = shallow_copy_rtx (orig);
1726 format_ptr = GET_RTX_FORMAT (code);
1728 for (i = 0; i < GET_RTX_LENGTH (code); i++)
1729 switch (*format_ptr++)
1731 case 'e':
1732 if (XEXP (orig, i) != NULL)
1734 rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1735 max_depth - 1);
1736 if (!result)
1737 return NULL;
1738 if (copy)
1739 XEXP (copy, i) = result;
1741 break;
1743 case 'E':
1744 case 'V':
1745 if (XVEC (orig, i) != NULL)
1747 if (copy)
1748 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1749 for (j = 0; j < XVECLEN (orig, i); j++)
1751 rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1752 evd, max_depth - 1);
1753 if (!result)
1754 return NULL;
1755 if (copy)
1756 XVECEXP (copy, i, j) = result;
1759 break;
1761 case 't':
1762 case 'w':
1763 case 'i':
1764 case 's':
1765 case 'S':
1766 case 'T':
1767 case 'u':
1768 case 'B':
1769 case '0':
1770 /* These are left unchanged. */
1771 break;
1773 default:
1774 gcc_unreachable ();
1777 if (evd->dummy)
1778 return orig;
1780 mode = GET_MODE (copy);
1781 /* If an operand has been simplified into CONST_INT, which doesn't
1782 have a mode and the mode isn't derivable from whole rtx's mode,
1783 try simplify_*_operation first with mode from original's operand
1784 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1785 scopy = copy;
1786 switch (GET_RTX_CLASS (code))
1788 case RTX_UNARY:
1789 if (CONST_INT_P (XEXP (copy, 0))
1790 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1792 scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1793 GET_MODE (XEXP (orig, 0)));
1794 if (scopy)
1795 return scopy;
1797 break;
1798 case RTX_COMM_ARITH:
1799 case RTX_BIN_ARITH:
1800 /* These expressions can derive operand modes from the whole rtx's mode. */
1801 break;
1802 case RTX_TERNARY:
1803 case RTX_BITFIELD_OPS:
1804 if (CONST_INT_P (XEXP (copy, 0))
1805 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1807 scopy = simplify_ternary_operation (code, mode,
1808 GET_MODE (XEXP (orig, 0)),
1809 XEXP (copy, 0), XEXP (copy, 1),
1810 XEXP (copy, 2));
1811 if (scopy)
1812 return scopy;
1814 break;
1815 case RTX_COMPARE:
1816 case RTX_COMM_COMPARE:
1817 if (CONST_INT_P (XEXP (copy, 0))
1818 && GET_MODE (XEXP (copy, 1)) == VOIDmode
1819 && (GET_MODE (XEXP (orig, 0)) != VOIDmode
1820 || GET_MODE (XEXP (orig, 1)) != VOIDmode))
1822 scopy = simplify_relational_operation (code, mode,
1823 (GET_MODE (XEXP (orig, 0))
1824 != VOIDmode)
1825 ? GET_MODE (XEXP (orig, 0))
1826 : GET_MODE (XEXP (orig, 1)),
1827 XEXP (copy, 0),
1828 XEXP (copy, 1));
1829 if (scopy)
1830 return scopy;
1832 break;
1833 default:
1834 break;
1836 scopy = simplify_rtx (copy);
1837 if (scopy)
1838 return scopy;
1839 return copy;
1842 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1843 with VALUE expressions. This way, it becomes independent of changes
1844 to registers and memory.
1845 X isn't actually modified; if modifications are needed, new rtl is
1846 allocated. However, the return value can share rtl with X.
1847 If X is within a MEM, MEMMODE must be the mode of the MEM. */
1850 cselib_subst_to_values (rtx x, machine_mode memmode)
1852 enum rtx_code code = GET_CODE (x);
1853 const char *fmt = GET_RTX_FORMAT (code);
1854 cselib_val *e;
1855 struct elt_list *l;
1856 rtx copy = x;
1857 int i;
1859 switch (code)
1861 case REG:
1862 l = REG_VALUES (REGNO (x));
1863 if (l && l->elt == NULL)
1864 l = l->next;
1865 for (; l; l = l->next)
1866 if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
1867 return l->elt->val_rtx;
1869 gcc_unreachable ();
1871 case MEM:
1872 e = cselib_lookup_mem (x, 0);
1873 /* This used to happen for autoincrements, but we deal with them
1874 properly now. Remove the if stmt for the next release. */
1875 if (! e)
1877 /* Assign a value that doesn't match any other. */
1878 e = new_cselib_val (next_uid, GET_MODE (x), x);
1880 return e->val_rtx;
1882 case ENTRY_VALUE:
1883 e = cselib_lookup (x, GET_MODE (x), 0, memmode);
1884 if (! e)
1885 break;
1886 return e->val_rtx;
1888 CASE_CONST_ANY:
1889 return x;
1891 case PRE_DEC:
1892 case PRE_INC:
1893 gcc_assert (memmode != VOIDmode);
1894 i = GET_MODE_SIZE (memmode);
1895 if (code == PRE_DEC)
1896 i = -i;
1897 return cselib_subst_to_values (plus_constant (GET_MODE (x),
1898 XEXP (x, 0), i),
1899 memmode);
1901 case PRE_MODIFY:
1902 gcc_assert (memmode != VOIDmode);
1903 return cselib_subst_to_values (XEXP (x, 1), memmode);
1905 case POST_DEC:
1906 case POST_INC:
1907 case POST_MODIFY:
1908 gcc_assert (memmode != VOIDmode);
1909 return cselib_subst_to_values (XEXP (x, 0), memmode);
1911 default:
1912 break;
1915 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1917 if (fmt[i] == 'e')
1919 rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
1921 if (t != XEXP (x, i))
1923 if (x == copy)
1924 copy = shallow_copy_rtx (x);
1925 XEXP (copy, i) = t;
1928 else if (fmt[i] == 'E')
1930 int j;
1932 for (j = 0; j < XVECLEN (x, i); j++)
1934 rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
1936 if (t != XVECEXP (x, i, j))
1938 if (XVEC (x, i) == XVEC (copy, i))
1940 if (x == copy)
1941 copy = shallow_copy_rtx (x);
1942 XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
1944 XVECEXP (copy, i, j) = t;
1950 return copy;
1953 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
1956 cselib_subst_to_values_from_insn (rtx x, machine_mode memmode, rtx_insn *insn)
1958 rtx ret;
1959 gcc_assert (!cselib_current_insn);
1960 cselib_current_insn = insn;
1961 ret = cselib_subst_to_values (x, memmode);
1962 cselib_current_insn = NULL;
1963 return ret;
1966 /* Look up the rtl expression X in our tables and return the value it
1967 has. If CREATE is zero, we return NULL if we don't know the value.
1968 Otherwise, we create a new one if possible, using mode MODE if X
1969 doesn't have a mode (i.e. because it's a constant). When X is part
1970 of an address, MEMMODE should be the mode of the enclosing MEM if
1971 we're tracking autoinc expressions. */
1973 static cselib_val *
1974 cselib_lookup_1 (rtx x, machine_mode mode,
1975 int create, machine_mode memmode)
1977 cselib_val **slot;
1978 cselib_val *e;
1979 unsigned int hashval;
1981 if (GET_MODE (x) != VOIDmode)
1982 mode = GET_MODE (x);
1984 if (GET_CODE (x) == VALUE)
1985 return CSELIB_VAL_PTR (x);
1987 if (REG_P (x))
1989 struct elt_list *l;
1990 unsigned int i = REGNO (x);
1992 l = REG_VALUES (i);
1993 if (l && l->elt == NULL)
1994 l = l->next;
1995 for (; l; l = l->next)
1996 if (mode == GET_MODE (l->elt->val_rtx))
1998 promote_debug_loc (l->elt->locs);
1999 return l->elt;
2002 if (! create)
2003 return 0;
2005 if (i < FIRST_PSEUDO_REGISTER)
2007 unsigned int n = hard_regno_nregs[i][mode];
2009 if (n > max_value_regs)
2010 max_value_regs = n;
2013 e = new_cselib_val (next_uid, GET_MODE (x), x);
2014 new_elt_loc_list (e, x);
2015 if (REG_VALUES (i) == 0)
2017 /* Maintain the invariant that the first entry of
2018 REG_VALUES, if present, must be the value used to set the
2019 register, or NULL. */
2020 used_regs[n_used_regs++] = i;
2021 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
2023 else if (cselib_preserve_constants
2024 && GET_MODE_CLASS (mode) == MODE_INT)
2026 /* During var-tracking, try harder to find equivalences
2027 for SUBREGs. If a setter sets say a DImode register
2028 and user uses that register only in SImode, add a lowpart
2029 subreg location. */
2030 struct elt_list *lwider = NULL;
2031 l = REG_VALUES (i);
2032 if (l && l->elt == NULL)
2033 l = l->next;
2034 for (; l; l = l->next)
2035 if (GET_MODE_CLASS (GET_MODE (l->elt->val_rtx)) == MODE_INT
2036 && GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2037 > GET_MODE_SIZE (mode)
2038 && (lwider == NULL
2039 || GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2040 < GET_MODE_SIZE (GET_MODE (lwider->elt->val_rtx))))
2042 struct elt_loc_list *el;
2043 if (i < FIRST_PSEUDO_REGISTER
2044 && hard_regno_nregs[i][GET_MODE (l->elt->val_rtx)] != 1)
2045 continue;
2046 for (el = l->elt->locs; el; el = el->next)
2047 if (!REG_P (el->loc))
2048 break;
2049 if (el)
2050 lwider = l;
2052 if (lwider)
2054 rtx sub = lowpart_subreg (mode, lwider->elt->val_rtx,
2055 GET_MODE (lwider->elt->val_rtx));
2056 if (sub)
2057 new_elt_loc_list (e, sub);
2060 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
2061 slot = cselib_find_slot (mode, x, e->hash, INSERT, memmode);
2062 *slot = e;
2063 return e;
2066 if (MEM_P (x))
2067 return cselib_lookup_mem (x, create);
2069 hashval = cselib_hash_rtx (x, create, memmode);
2070 /* Can't even create if hashing is not possible. */
2071 if (! hashval)
2072 return 0;
2074 slot = cselib_find_slot (mode, x, hashval,
2075 create ? INSERT : NO_INSERT, memmode);
2076 if (slot == 0)
2077 return 0;
2079 e = (cselib_val *) *slot;
2080 if (e)
2081 return e;
2083 e = new_cselib_val (hashval, mode, x);
2085 /* We have to fill the slot before calling cselib_subst_to_values:
2086 the hash table is inconsistent until we do so, and
2087 cselib_subst_to_values will need to do lookups. */
2088 *slot = e;
2089 new_elt_loc_list (e, cselib_subst_to_values (x, memmode));
2090 return e;
2093 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2095 cselib_val *
2096 cselib_lookup_from_insn (rtx x, machine_mode mode,
2097 int create, machine_mode memmode, rtx_insn *insn)
2099 cselib_val *ret;
2101 gcc_assert (!cselib_current_insn);
2102 cselib_current_insn = insn;
2104 ret = cselib_lookup (x, mode, create, memmode);
2106 cselib_current_insn = NULL;
2108 return ret;
2111 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2112 maintains invariants related with debug insns. */
2114 cselib_val *
2115 cselib_lookup (rtx x, machine_mode mode,
2116 int create, machine_mode memmode)
2118 cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
2120 /* ??? Should we return NULL if we're not to create an entry, the
2121 found loc is a debug loc and cselib_current_insn is not DEBUG?
2122 If so, we should also avoid converting val to non-DEBUG; probably
2123 easiest setting cselib_current_insn to NULL before the call
2124 above. */
2126 if (dump_file && (dump_flags & TDF_CSELIB))
2128 fputs ("cselib lookup ", dump_file);
2129 print_inline_rtx (dump_file, x, 2);
2130 fprintf (dump_file, " => %u:%u\n",
2131 ret ? ret->uid : 0,
2132 ret ? ret->hash : 0);
2135 return ret;
2138 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2139 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2140 is used to determine how many hard registers are being changed. If MODE
2141 is VOIDmode, then only REGNO is being changed; this is used when
2142 invalidating call clobbered registers across a call. */
2144 static void
2145 cselib_invalidate_regno (unsigned int regno, machine_mode mode)
2147 unsigned int endregno;
2148 unsigned int i;
2150 /* If we see pseudos after reload, something is _wrong_. */
2151 gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
2152 || reg_renumber[regno] < 0);
2154 /* Determine the range of registers that must be invalidated. For
2155 pseudos, only REGNO is affected. For hard regs, we must take MODE
2156 into account, and we must also invalidate lower register numbers
2157 if they contain values that overlap REGNO. */
2158 if (regno < FIRST_PSEUDO_REGISTER)
2160 gcc_assert (mode != VOIDmode);
2162 if (regno < max_value_regs)
2163 i = 0;
2164 else
2165 i = regno - max_value_regs;
2167 endregno = end_hard_regno (mode, regno);
2169 else
2171 i = regno;
2172 endregno = regno + 1;
2175 for (; i < endregno; i++)
2177 struct elt_list **l = &REG_VALUES (i);
2179 /* Go through all known values for this reg; if it overlaps the range
2180 we're invalidating, remove the value. */
2181 while (*l)
2183 cselib_val *v = (*l)->elt;
2184 bool had_locs;
2185 rtx_insn *setting_insn;
2186 struct elt_loc_list **p;
2187 unsigned int this_last = i;
2189 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
2190 this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
2192 if (this_last < regno || v == NULL
2193 || (v == cfa_base_preserved_val
2194 && i == cfa_base_preserved_regno))
2196 l = &(*l)->next;
2197 continue;
2200 /* We have an overlap. */
2201 if (*l == REG_VALUES (i))
2203 /* Maintain the invariant that the first entry of
2204 REG_VALUES, if present, must be the value used to set
2205 the register, or NULL. This is also nice because
2206 then we won't push the same regno onto user_regs
2207 multiple times. */
2208 (*l)->elt = NULL;
2209 l = &(*l)->next;
2211 else
2212 unchain_one_elt_list (l);
2214 v = canonical_cselib_val (v);
2216 had_locs = v->locs != NULL;
2217 setting_insn = v->locs ? v->locs->setting_insn : NULL;
2219 /* Now, we clear the mapping from value to reg. It must exist, so
2220 this code will crash intentionally if it doesn't. */
2221 for (p = &v->locs; ; p = &(*p)->next)
2223 rtx x = (*p)->loc;
2225 if (REG_P (x) && REGNO (x) == i)
2227 unchain_one_elt_loc_list (p);
2228 break;
2232 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2234 if (setting_insn && DEBUG_INSN_P (setting_insn))
2235 n_useless_debug_values++;
2236 else
2237 n_useless_values++;
2243 /* Invalidate any locations in the table which are changed because of a
2244 store to MEM_RTX. If this is called because of a non-const call
2245 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2247 static void
2248 cselib_invalidate_mem (rtx mem_rtx)
2250 cselib_val **vp, *v, *next;
2251 int num_mems = 0;
2252 rtx mem_addr;
2254 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2255 mem_rtx = canon_rtx (mem_rtx);
2257 vp = &first_containing_mem;
2258 for (v = *vp; v != &dummy_val; v = next)
2260 bool has_mem = false;
2261 struct elt_loc_list **p = &v->locs;
2262 bool had_locs = v->locs != NULL;
2263 rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
2265 while (*p)
2267 rtx x = (*p)->loc;
2268 cselib_val *addr;
2269 struct elt_list **mem_chain;
2271 /* MEMs may occur in locations only at the top level; below
2272 that every MEM or REG is substituted by its VALUE. */
2273 if (!MEM_P (x))
2275 p = &(*p)->next;
2276 continue;
2278 if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
2279 && ! canon_anti_dependence (x, false, mem_rtx,
2280 GET_MODE (mem_rtx), mem_addr))
2282 has_mem = true;
2283 num_mems++;
2284 p = &(*p)->next;
2285 continue;
2288 /* This one overlaps. */
2289 /* We must have a mapping from this MEM's address to the
2290 value (E). Remove that, too. */
2291 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
2292 addr = canonical_cselib_val (addr);
2293 gcc_checking_assert (v == canonical_cselib_val (v));
2294 mem_chain = &addr->addr_list;
2295 for (;;)
2297 cselib_val *canon = canonical_cselib_val ((*mem_chain)->elt);
2299 if (canon == v)
2301 unchain_one_elt_list (mem_chain);
2302 break;
2305 /* Record canonicalized elt. */
2306 (*mem_chain)->elt = canon;
2308 mem_chain = &(*mem_chain)->next;
2311 unchain_one_elt_loc_list (p);
2314 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2316 if (setting_insn && DEBUG_INSN_P (setting_insn))
2317 n_useless_debug_values++;
2318 else
2319 n_useless_values++;
2322 next = v->next_containing_mem;
2323 if (has_mem)
2325 *vp = v;
2326 vp = &(*vp)->next_containing_mem;
2328 else
2329 v->next_containing_mem = NULL;
2331 *vp = &dummy_val;
2334 /* Invalidate DEST, which is being assigned to or clobbered. */
2336 void
2337 cselib_invalidate_rtx (rtx dest)
2339 while (GET_CODE (dest) == SUBREG
2340 || GET_CODE (dest) == ZERO_EXTRACT
2341 || GET_CODE (dest) == STRICT_LOW_PART)
2342 dest = XEXP (dest, 0);
2344 if (REG_P (dest))
2345 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
2346 else if (MEM_P (dest))
2347 cselib_invalidate_mem (dest);
2350 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2352 static void
2353 cselib_invalidate_rtx_note_stores (rtx dest, const_rtx ignore ATTRIBUTE_UNUSED,
2354 void *data ATTRIBUTE_UNUSED)
2356 cselib_invalidate_rtx (dest);
2359 /* Record the result of a SET instruction. DEST is being set; the source
2360 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2361 describes its address. */
2363 static void
2364 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
2366 if (src_elt == 0 || side_effects_p (dest))
2367 return;
2369 if (REG_P (dest))
2371 unsigned int dreg = REGNO (dest);
2372 if (dreg < FIRST_PSEUDO_REGISTER)
2374 unsigned int n = REG_NREGS (dest);
2376 if (n > max_value_regs)
2377 max_value_regs = n;
2380 if (REG_VALUES (dreg) == 0)
2382 used_regs[n_used_regs++] = dreg;
2383 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2385 else
2387 /* The register should have been invalidated. */
2388 gcc_assert (REG_VALUES (dreg)->elt == 0);
2389 REG_VALUES (dreg)->elt = src_elt;
2392 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2393 n_useless_values--;
2394 new_elt_loc_list (src_elt, dest);
2396 else if (MEM_P (dest) && dest_addr_elt != 0
2397 && cselib_record_memory)
2399 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2400 n_useless_values--;
2401 add_mem_for_addr (dest_addr_elt, src_elt, dest);
2405 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2407 void
2408 cselib_add_permanent_equiv (cselib_val *elt, rtx x, rtx_insn *insn)
2410 cselib_val *nelt;
2411 rtx_insn *save_cselib_current_insn = cselib_current_insn;
2413 gcc_checking_assert (elt);
2414 gcc_checking_assert (PRESERVED_VALUE_P (elt->val_rtx));
2415 gcc_checking_assert (!side_effects_p (x));
2417 cselib_current_insn = insn;
2419 nelt = cselib_lookup (x, GET_MODE (elt->val_rtx), 1, VOIDmode);
2421 if (nelt != elt)
2423 cselib_any_perm_equivs = true;
2425 if (!PRESERVED_VALUE_P (nelt->val_rtx))
2426 cselib_preserve_value (nelt);
2428 new_elt_loc_list (nelt, elt->val_rtx);
2431 cselib_current_insn = save_cselib_current_insn;
2434 /* Return TRUE if any permanent equivalences have been recorded since
2435 the table was last initialized. */
2436 bool
2437 cselib_have_permanent_equivalences (void)
2439 return cselib_any_perm_equivs;
2442 /* There is no good way to determine how many elements there can be
2443 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2444 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2446 struct cselib_record_autoinc_data
2448 struct cselib_set *sets;
2449 int n_sets;
2452 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2453 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2455 static int
2456 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
2457 rtx dest, rtx src, rtx srcoff, void *arg)
2459 struct cselib_record_autoinc_data *data;
2460 data = (struct cselib_record_autoinc_data *)arg;
2462 data->sets[data->n_sets].dest = dest;
2464 if (srcoff)
2465 data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
2466 else
2467 data->sets[data->n_sets].src = src;
2469 data->n_sets++;
2471 return 0;
2474 /* Record the effects of any sets and autoincs in INSN. */
2475 static void
2476 cselib_record_sets (rtx_insn *insn)
2478 int n_sets = 0;
2479 int i;
2480 struct cselib_set sets[MAX_SETS];
2481 rtx body = PATTERN (insn);
2482 rtx cond = 0;
2483 int n_sets_before_autoinc;
2484 struct cselib_record_autoinc_data data;
2486 body = PATTERN (insn);
2487 if (GET_CODE (body) == COND_EXEC)
2489 cond = COND_EXEC_TEST (body);
2490 body = COND_EXEC_CODE (body);
2493 /* Find all sets. */
2494 if (GET_CODE (body) == SET)
2496 sets[0].src = SET_SRC (body);
2497 sets[0].dest = SET_DEST (body);
2498 n_sets = 1;
2500 else if (GET_CODE (body) == PARALLEL)
2502 /* Look through the PARALLEL and record the values being
2503 set, if possible. Also handle any CLOBBERs. */
2504 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2506 rtx x = XVECEXP (body, 0, i);
2508 if (GET_CODE (x) == SET)
2510 sets[n_sets].src = SET_SRC (x);
2511 sets[n_sets].dest = SET_DEST (x);
2512 n_sets++;
2517 if (n_sets == 1
2518 && MEM_P (sets[0].src)
2519 && !cselib_record_memory
2520 && MEM_READONLY_P (sets[0].src))
2522 rtx note = find_reg_equal_equiv_note (insn);
2524 if (note && CONSTANT_P (XEXP (note, 0)))
2525 sets[0].src = XEXP (note, 0);
2528 data.sets = sets;
2529 data.n_sets = n_sets_before_autoinc = n_sets;
2530 for_each_inc_dec (PATTERN (insn), cselib_record_autoinc_cb, &data);
2531 n_sets = data.n_sets;
2533 /* Look up the values that are read. Do this before invalidating the
2534 locations that are written. */
2535 for (i = 0; i < n_sets; i++)
2537 rtx dest = sets[i].dest;
2539 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2540 the low part after invalidating any knowledge about larger modes. */
2541 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2542 sets[i].dest = dest = XEXP (dest, 0);
2544 /* We don't know how to record anything but REG or MEM. */
2545 if (REG_P (dest)
2546 || (MEM_P (dest) && cselib_record_memory))
2548 rtx src = sets[i].src;
2549 if (cond)
2550 src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
2551 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
2552 if (MEM_P (dest))
2554 machine_mode address_mode = get_address_mode (dest);
2556 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
2557 address_mode, 1,
2558 GET_MODE (dest));
2560 else
2561 sets[i].dest_addr_elt = 0;
2565 if (cselib_record_sets_hook)
2566 cselib_record_sets_hook (insn, sets, n_sets);
2568 /* Invalidate all locations written by this insn. Note that the elts we
2569 looked up in the previous loop aren't affected, just some of their
2570 locations may go away. */
2571 note_stores (body, cselib_invalidate_rtx_note_stores, NULL);
2573 for (i = n_sets_before_autoinc; i < n_sets; i++)
2574 cselib_invalidate_rtx (sets[i].dest);
2576 /* If this is an asm, look for duplicate sets. This can happen when the
2577 user uses the same value as an output multiple times. This is valid
2578 if the outputs are not actually used thereafter. Treat this case as
2579 if the value isn't actually set. We do this by smashing the destination
2580 to pc_rtx, so that we won't record the value later. */
2581 if (n_sets >= 2 && asm_noperands (body) >= 0)
2583 for (i = 0; i < n_sets; i++)
2585 rtx dest = sets[i].dest;
2586 if (REG_P (dest) || MEM_P (dest))
2588 int j;
2589 for (j = i + 1; j < n_sets; j++)
2590 if (rtx_equal_p (dest, sets[j].dest))
2592 sets[i].dest = pc_rtx;
2593 sets[j].dest = pc_rtx;
2599 /* Now enter the equivalences in our tables. */
2600 for (i = 0; i < n_sets; i++)
2602 rtx dest = sets[i].dest;
2603 if (REG_P (dest)
2604 || (MEM_P (dest) && cselib_record_memory))
2605 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2609 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
2611 bool
2612 fp_setter_insn (rtx_insn *insn)
2614 rtx expr, pat = NULL_RTX;
2616 if (!RTX_FRAME_RELATED_P (insn))
2617 return false;
2619 expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2620 if (expr)
2621 pat = XEXP (expr, 0);
2622 if (!modified_in_p (hard_frame_pointer_rtx, pat ? pat : insn))
2623 return false;
2625 /* Don't return true for frame pointer restores in the epilogue. */
2626 if (find_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx))
2627 return false;
2628 return true;
2631 /* Record the effects of INSN. */
2633 void
2634 cselib_process_insn (rtx_insn *insn)
2636 int i;
2637 rtx x;
2639 cselib_current_insn = insn;
2641 /* Forget everything at a CODE_LABEL or a setjmp. */
2642 if ((LABEL_P (insn)
2643 || (CALL_P (insn)
2644 && find_reg_note (insn, REG_SETJMP, NULL)))
2645 && !cselib_preserve_constants)
2647 cselib_reset_table (next_uid);
2648 cselib_current_insn = NULL;
2649 return;
2652 if (! INSN_P (insn))
2654 cselib_current_insn = NULL;
2655 return;
2658 /* If this is a call instruction, forget anything stored in a
2659 call clobbered register, or, if this is not a const call, in
2660 memory. */
2661 if (CALL_P (insn))
2663 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2664 if (call_used_regs[i]
2665 || (REG_VALUES (i) && REG_VALUES (i)->elt
2666 && HARD_REGNO_CALL_PART_CLOBBERED (i,
2667 GET_MODE (REG_VALUES (i)->elt->val_rtx))))
2668 cselib_invalidate_regno (i, reg_raw_mode[i]);
2670 /* Since it is not clear how cselib is going to be used, be
2671 conservative here and treat looping pure or const functions
2672 as if they were regular functions. */
2673 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
2674 || !(RTL_CONST_OR_PURE_CALL_P (insn)))
2675 cselib_invalidate_mem (callmem);
2678 cselib_record_sets (insn);
2680 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2681 after we have processed the insn. */
2682 if (CALL_P (insn))
2684 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
2685 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
2686 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
2687 /* Flush evertything on setjmp. */
2688 if (cselib_preserve_constants
2689 && find_reg_note (insn, REG_SETJMP, NULL))
2691 cselib_preserve_only_values ();
2692 cselib_reset_table (next_uid);
2696 /* On setter of the hard frame pointer if frame_pointer_needed,
2697 invalidate stack_pointer_rtx, so that sp and {,h}fp based
2698 VALUEs are distinct. */
2699 if (reload_completed
2700 && frame_pointer_needed
2701 && fp_setter_insn (insn))
2702 cselib_invalidate_rtx (stack_pointer_rtx);
2704 cselib_current_insn = NULL;
2706 if (n_useless_values > MAX_USELESS_VALUES
2707 /* remove_useless_values is linear in the hash table size. Avoid
2708 quadratic behavior for very large hashtables with very few
2709 useless elements. */
2710 && ((unsigned int)n_useless_values
2711 > (cselib_hash_table->elements () - n_debug_values) / 4))
2712 remove_useless_values ();
2715 /* Initialize cselib for one pass. The caller must also call
2716 init_alias_analysis. */
2718 void
2719 cselib_init (int record_what)
2721 cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
2722 cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
2723 cselib_any_perm_equivs = false;
2725 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2726 see canon_true_dependence. This is only created once. */
2727 if (! callmem)
2728 callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2730 cselib_nregs = max_reg_num ();
2732 /* We preserve reg_values to allow expensive clearing of the whole thing.
2733 Reallocate it however if it happens to be too large. */
2734 if (!reg_values || reg_values_size < cselib_nregs
2735 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
2737 free (reg_values);
2738 /* Some space for newly emit instructions so we don't end up
2739 reallocating in between passes. */
2740 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
2741 reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
2743 used_regs = XNEWVEC (unsigned int, cselib_nregs);
2744 n_used_regs = 0;
2745 cselib_hash_table = new hash_table<cselib_hasher> (31);
2746 if (cselib_preserve_constants)
2747 cselib_preserved_hash_table = new hash_table<cselib_hasher> (31);
2748 next_uid = 1;
2751 /* Called when the current user is done with cselib. */
2753 void
2754 cselib_finish (void)
2756 bool preserved = cselib_preserve_constants;
2757 cselib_discard_hook = NULL;
2758 cselib_preserve_constants = false;
2759 cselib_any_perm_equivs = false;
2760 cfa_base_preserved_val = NULL;
2761 cfa_base_preserved_regno = INVALID_REGNUM;
2762 elt_list_pool.release ();
2763 elt_loc_list_pool.release ();
2764 cselib_val_pool.release ();
2765 value_pool.release ();
2766 cselib_clear_table ();
2767 delete cselib_hash_table;
2768 cselib_hash_table = NULL;
2769 if (preserved)
2770 delete cselib_preserved_hash_table;
2771 cselib_preserved_hash_table = NULL;
2772 free (used_regs);
2773 used_regs = 0;
2774 n_useless_values = 0;
2775 n_useless_debug_values = 0;
2776 n_debug_values = 0;
2777 next_uid = 0;
2780 /* Dump the cselib_val *X to FILE *OUT. */
2783 dump_cselib_val (cselib_val **x, FILE *out)
2785 cselib_val *v = *x;
2786 bool need_lf = true;
2788 print_inline_rtx (out, v->val_rtx, 0);
2790 if (v->locs)
2792 struct elt_loc_list *l = v->locs;
2793 if (need_lf)
2795 fputc ('\n', out);
2796 need_lf = false;
2798 fputs (" locs:", out);
2801 if (l->setting_insn)
2802 fprintf (out, "\n from insn %i ",
2803 INSN_UID (l->setting_insn));
2804 else
2805 fprintf (out, "\n ");
2806 print_inline_rtx (out, l->loc, 4);
2808 while ((l = l->next));
2809 fputc ('\n', out);
2811 else
2813 fputs (" no locs", out);
2814 need_lf = true;
2817 if (v->addr_list)
2819 struct elt_list *e = v->addr_list;
2820 if (need_lf)
2822 fputc ('\n', out);
2823 need_lf = false;
2825 fputs (" addr list:", out);
2828 fputs ("\n ", out);
2829 print_inline_rtx (out, e->elt->val_rtx, 2);
2831 while ((e = e->next));
2832 fputc ('\n', out);
2834 else
2836 fputs (" no addrs", out);
2837 need_lf = true;
2840 if (v->next_containing_mem == &dummy_val)
2841 fputs (" last mem\n", out);
2842 else if (v->next_containing_mem)
2844 fputs (" next mem ", out);
2845 print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
2846 fputc ('\n', out);
2848 else if (need_lf)
2849 fputc ('\n', out);
2851 return 1;
2854 /* Dump to OUT everything in the CSELIB table. */
2856 void
2857 dump_cselib_table (FILE *out)
2859 fprintf (out, "cselib hash table:\n");
2860 cselib_hash_table->traverse <FILE *, dump_cselib_val> (out);
2861 fprintf (out, "cselib preserved hash table:\n");
2862 cselib_preserved_hash_table->traverse <FILE *, dump_cselib_val> (out);
2863 if (first_containing_mem != &dummy_val)
2865 fputs ("first mem ", out);
2866 print_inline_rtx (out, first_containing_mem->val_rtx, 2);
2867 fputc ('\n', out);
2869 fprintf (out, "next uid %i\n", next_uid);
2872 #include "gt-cselib.h"