re PR other/63387 (Optimize pairs of isnan() calls into a single isunordered())
[official-gcc.git] / gcc / cselib.c
blob7a50f5055bd5e3967cdd9881e59fe3c7861b4510
1 /* Common subexpression elimination library for GNU compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "rtl.h"
25 #include "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"/* FIXME: For hashing DEBUG_EXPR & friends. */
35 #include "tm_p.h"
36 #include "regs.h"
37 #include "hard-reg-set.h"
38 #include "flags.h"
39 #include "insn-config.h"
40 #include "recog.h"
41 #include "hashtab.h"
42 #include "input.h"
43 #include "function.h"
44 #include "emit-rtl.h"
45 #include "diagnostic-core.h"
46 #include "ggc.h"
47 #include "hash-table.h"
48 #include "dumpfile.h"
49 #include "cselib.h"
50 #include "predict.h"
51 #include "basic-block.h"
52 #include "valtrack.h"
53 #include "params.h"
54 #include "alloc-pool.h"
55 #include "target.h"
56 #include "bitmap.h"
58 /* A list of cselib_val structures. */
59 struct elt_list {
60 struct elt_list *next;
61 cselib_val *elt;
64 static bool cselib_record_memory;
65 static bool cselib_preserve_constants;
66 static bool cselib_any_perm_equivs;
67 static inline void promote_debug_loc (struct elt_loc_list *l);
68 static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
69 static void new_elt_loc_list (cselib_val *, rtx);
70 static void unchain_one_value (cselib_val *);
71 static void unchain_one_elt_list (struct elt_list **);
72 static void unchain_one_elt_loc_list (struct elt_loc_list **);
73 static void remove_useless_values (void);
74 static int rtx_equal_for_cselib_1 (rtx, rtx, machine_mode);
75 static unsigned int cselib_hash_rtx (rtx, int, machine_mode);
76 static cselib_val *new_cselib_val (unsigned int, machine_mode, rtx);
77 static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
78 static cselib_val *cselib_lookup_mem (rtx, int);
79 static void cselib_invalidate_regno (unsigned int, machine_mode);
80 static void cselib_invalidate_mem (rtx);
81 static void cselib_record_set (rtx, cselib_val *, cselib_val *);
82 static void cselib_record_sets (rtx_insn *);
84 struct expand_value_data
86 bitmap regs_active;
87 cselib_expand_callback callback;
88 void *callback_arg;
89 bool dummy;
92 static rtx cselib_expand_value_rtx_1 (rtx, struct expand_value_data *, int);
94 /* There are three ways in which cselib can look up an rtx:
95 - for a REG, the reg_values table (which is indexed by regno) is used
96 - for a MEM, we recursively look up its address and then follow the
97 addr_list of that value
98 - for everything else, we compute a hash value and go through the hash
99 table. Since different rtx's can still have the same hash value,
100 this involves walking the table entries for a given value and comparing
101 the locations of the entries with the rtx we are looking up. */
103 struct cselib_hasher : typed_noop_remove <cselib_val>
105 typedef cselib_val *value_type;
106 struct key {
107 /* The rtx value and its mode (needed separately for constant
108 integers). */
109 machine_mode mode;
110 rtx x;
111 /* The mode of the contaning MEM, if any, otherwise VOIDmode. */
112 machine_mode memmode;
114 typedef key *compare_type;
115 static inline hashval_t hash (const cselib_val *);
116 static inline bool equal (const cselib_val *, const key *);
119 /* The hash function for our hash table. The value is always computed with
120 cselib_hash_rtx when adding an element; this function just extracts the
121 hash value from a cselib_val structure. */
123 inline hashval_t
124 cselib_hasher::hash (const cselib_val *v)
126 return v->hash;
129 /* The equality test for our hash table. The first argument V is a table
130 element (i.e. a cselib_val), while the second arg X is an rtx. We know
131 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
132 CONST of an appropriate mode. */
134 inline bool
135 cselib_hasher::equal (const cselib_val *v, const key *x_arg)
137 struct elt_loc_list *l;
138 rtx x = x_arg->x;
139 machine_mode mode = x_arg->mode;
140 machine_mode memmode = x_arg->memmode;
142 if (mode != GET_MODE (v->val_rtx))
143 return false;
145 if (GET_CODE (x) == VALUE)
146 return x == v->val_rtx;
148 /* We don't guarantee that distinct rtx's have different hash values,
149 so we need to do a comparison. */
150 for (l = v->locs; l; l = l->next)
151 if (rtx_equal_for_cselib_1 (l->loc, x, memmode))
153 promote_debug_loc (l);
154 return true;
157 return false;
160 /* A table that enables us to look up elts by their value. */
161 static hash_table<cselib_hasher> *cselib_hash_table;
163 /* A table to hold preserved values. */
164 static hash_table<cselib_hasher> *cselib_preserved_hash_table;
166 /* This is a global so we don't have to pass this through every function.
167 It is used in new_elt_loc_list to set SETTING_INSN. */
168 static rtx_insn *cselib_current_insn;
170 /* The unique id that the next create value will take. */
171 static unsigned int next_uid;
173 /* The number of registers we had when the varrays were last resized. */
174 static unsigned int cselib_nregs;
176 /* Count values without known locations, or with only locations that
177 wouldn't have been known except for debug insns. Whenever this
178 grows too big, we remove these useless values from the table.
180 Counting values with only debug values is a bit tricky. We don't
181 want to increment n_useless_values when we create a value for a
182 debug insn, for this would get n_useless_values out of sync, but we
183 want increment it if all locs in the list that were ever referenced
184 in nondebug insns are removed from the list.
186 In the general case, once we do that, we'd have to stop accepting
187 nondebug expressions in the loc list, to avoid having two values
188 equivalent that, without debug insns, would have been made into
189 separate values. However, because debug insns never introduce
190 equivalences themselves (no assignments), the only means for
191 growing loc lists is through nondebug assignments. If the locs
192 also happen to be referenced in debug insns, it will work just fine.
194 A consequence of this is that there's at most one debug-only loc in
195 each loc list. If we keep it in the first entry, testing whether
196 we have a debug-only loc list takes O(1).
198 Furthermore, since any additional entry in a loc list containing a
199 debug loc would have to come from an assignment (nondebug) that
200 references both the initial debug loc and the newly-equivalent loc,
201 the initial debug loc would be promoted to a nondebug loc, and the
202 loc list would not contain debug locs any more.
204 So the only case we have to be careful with in order to keep
205 n_useless_values in sync between debug and nondebug compilations is
206 to avoid incrementing n_useless_values when removing the single loc
207 from a value that turns out to not appear outside debug values. We
208 increment n_useless_debug_values instead, and leave such values
209 alone until, for other reasons, we garbage-collect useless
210 values. */
211 static int n_useless_values;
212 static int n_useless_debug_values;
214 /* Count values whose locs have been taken exclusively from debug
215 insns for the entire life of the value. */
216 static int n_debug_values;
218 /* Number of useless values before we remove them from the hash table. */
219 #define MAX_USELESS_VALUES 32
221 /* This table maps from register number to values. It does not
222 contain pointers to cselib_val structures, but rather elt_lists.
223 The purpose is to be able to refer to the same register in
224 different modes. The first element of the list defines the mode in
225 which the register was set; if the mode is unknown or the value is
226 no longer valid in that mode, ELT will be NULL for the first
227 element. */
228 static struct elt_list **reg_values;
229 static unsigned int reg_values_size;
230 #define REG_VALUES(i) reg_values[i]
232 /* The largest number of hard regs used by any entry added to the
233 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
234 static unsigned int max_value_regs;
236 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
237 in cselib_clear_table() for fast emptying. */
238 static unsigned int *used_regs;
239 static unsigned int n_used_regs;
241 /* We pass this to cselib_invalidate_mem to invalidate all of
242 memory for a non-const call instruction. */
243 static GTY(()) rtx callmem;
245 /* Set by discard_useless_locs if it deleted the last location of any
246 value. */
247 static int values_became_useless;
249 /* Used as stop element of the containing_mem list so we can check
250 presence in the list by checking the next pointer. */
251 static cselib_val dummy_val;
253 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
254 that is constant through the whole function and should never be
255 eliminated. */
256 static cselib_val *cfa_base_preserved_val;
257 static unsigned int cfa_base_preserved_regno = INVALID_REGNUM;
259 /* Used to list all values that contain memory reference.
260 May or may not contain the useless values - the list is compacted
261 each time memory is invalidated. */
262 static cselib_val *first_containing_mem = &dummy_val;
263 static alloc_pool elt_loc_list_pool, elt_list_pool, cselib_val_pool, value_pool;
265 /* If nonnull, cselib will call this function before freeing useless
266 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
267 void (*cselib_discard_hook) (cselib_val *);
269 /* If nonnull, cselib will call this function before recording sets or
270 even clobbering outputs of INSN. All the recorded sets will be
271 represented in the array sets[n_sets]. new_val_min can be used to
272 tell whether values present in sets are introduced by this
273 instruction. */
274 void (*cselib_record_sets_hook) (rtx_insn *insn, struct cselib_set *sets,
275 int n_sets);
277 #define PRESERVED_VALUE_P(RTX) \
278 (RTL_FLAG_CHECK1 ("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
280 #define SP_BASED_VALUE_P(RTX) \
281 (RTL_FLAG_CHECK1 ("SP_BASED_VALUE_P", (RTX), VALUE)->jump)
285 /* Allocate a struct elt_list and fill in its two elements with the
286 arguments. */
288 static inline struct elt_list *
289 new_elt_list (struct elt_list *next, cselib_val *elt)
291 struct elt_list *el;
292 el = (struct elt_list *) pool_alloc (elt_list_pool);
293 el->next = next;
294 el->elt = elt;
295 return el;
298 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
299 list. */
301 static inline void
302 new_elt_loc_list (cselib_val *val, rtx loc)
304 struct elt_loc_list *el, *next = val->locs;
306 gcc_checking_assert (!next || !next->setting_insn
307 || !DEBUG_INSN_P (next->setting_insn)
308 || cselib_current_insn == next->setting_insn);
310 /* If we're creating the first loc in a debug insn context, we've
311 just created a debug value. Count it. */
312 if (!next && cselib_current_insn && DEBUG_INSN_P (cselib_current_insn))
313 n_debug_values++;
315 val = canonical_cselib_val (val);
316 next = val->locs;
318 if (GET_CODE (loc) == VALUE)
320 loc = canonical_cselib_val (CSELIB_VAL_PTR (loc))->val_rtx;
322 gcc_checking_assert (PRESERVED_VALUE_P (loc)
323 == PRESERVED_VALUE_P (val->val_rtx));
325 if (val->val_rtx == loc)
326 return;
327 else if (val->uid > CSELIB_VAL_PTR (loc)->uid)
329 /* Reverse the insertion. */
330 new_elt_loc_list (CSELIB_VAL_PTR (loc), val->val_rtx);
331 return;
334 gcc_checking_assert (val->uid < CSELIB_VAL_PTR (loc)->uid);
336 if (CSELIB_VAL_PTR (loc)->locs)
338 /* Bring all locs from LOC to VAL. */
339 for (el = CSELIB_VAL_PTR (loc)->locs; el->next; el = el->next)
341 /* Adjust values that have LOC as canonical so that VAL
342 becomes their canonical. */
343 if (el->loc && GET_CODE (el->loc) == VALUE)
345 gcc_checking_assert (CSELIB_VAL_PTR (el->loc)->locs->loc
346 == loc);
347 CSELIB_VAL_PTR (el->loc)->locs->loc = val->val_rtx;
350 el->next = val->locs;
351 next = val->locs = CSELIB_VAL_PTR (loc)->locs;
354 if (CSELIB_VAL_PTR (loc)->addr_list)
356 /* Bring in addr_list into canonical node. */
357 struct elt_list *last = CSELIB_VAL_PTR (loc)->addr_list;
358 while (last->next)
359 last = last->next;
360 last->next = val->addr_list;
361 val->addr_list = CSELIB_VAL_PTR (loc)->addr_list;
362 CSELIB_VAL_PTR (loc)->addr_list = NULL;
365 if (CSELIB_VAL_PTR (loc)->next_containing_mem != NULL
366 && val->next_containing_mem == NULL)
368 /* Add VAL to the containing_mem list after LOC. LOC will
369 be removed when we notice it doesn't contain any
370 MEMs. */
371 val->next_containing_mem = CSELIB_VAL_PTR (loc)->next_containing_mem;
372 CSELIB_VAL_PTR (loc)->next_containing_mem = val;
375 /* Chain LOC back to VAL. */
376 el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
377 el->loc = val->val_rtx;
378 el->setting_insn = cselib_current_insn;
379 el->next = NULL;
380 CSELIB_VAL_PTR (loc)->locs = el;
383 el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
384 el->loc = loc;
385 el->setting_insn = cselib_current_insn;
386 el->next = next;
387 val->locs = el;
390 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
391 originating from a debug insn, maintaining the debug values
392 count. */
394 static inline void
395 promote_debug_loc (struct elt_loc_list *l)
397 if (l && l->setting_insn && DEBUG_INSN_P (l->setting_insn)
398 && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
400 n_debug_values--;
401 l->setting_insn = cselib_current_insn;
402 if (cselib_preserve_constants && l->next)
404 gcc_assert (l->next->setting_insn
405 && DEBUG_INSN_P (l->next->setting_insn)
406 && !l->next->next);
407 l->next->setting_insn = cselib_current_insn;
409 else
410 gcc_assert (!l->next);
414 /* The elt_list at *PL is no longer needed. Unchain it and free its
415 storage. */
417 static inline void
418 unchain_one_elt_list (struct elt_list **pl)
420 struct elt_list *l = *pl;
422 *pl = l->next;
423 pool_free (elt_list_pool, l);
426 /* Likewise for elt_loc_lists. */
428 static void
429 unchain_one_elt_loc_list (struct elt_loc_list **pl)
431 struct elt_loc_list *l = *pl;
433 *pl = l->next;
434 pool_free (elt_loc_list_pool, l);
437 /* Likewise for cselib_vals. This also frees the addr_list associated with
438 V. */
440 static void
441 unchain_one_value (cselib_val *v)
443 while (v->addr_list)
444 unchain_one_elt_list (&v->addr_list);
446 pool_free (cselib_val_pool, v);
449 /* Remove all entries from the hash table. Also used during
450 initialization. */
452 void
453 cselib_clear_table (void)
455 cselib_reset_table (1);
458 /* Return TRUE if V is a constant, a function invariant or a VALUE
459 equivalence; FALSE otherwise. */
461 static bool
462 invariant_or_equiv_p (cselib_val *v)
464 struct elt_loc_list *l;
466 if (v == cfa_base_preserved_val)
467 return true;
469 /* Keep VALUE equivalences around. */
470 for (l = v->locs; l; l = l->next)
471 if (GET_CODE (l->loc) == VALUE)
472 return true;
474 if (v->locs != NULL
475 && v->locs->next == NULL)
477 if (CONSTANT_P (v->locs->loc)
478 && (GET_CODE (v->locs->loc) != CONST
479 || !references_value_p (v->locs->loc, 0)))
480 return true;
481 /* Although a debug expr may be bound to different expressions,
482 we can preserve it as if it was constant, to get unification
483 and proper merging within var-tracking. */
484 if (GET_CODE (v->locs->loc) == DEBUG_EXPR
485 || GET_CODE (v->locs->loc) == DEBUG_IMPLICIT_PTR
486 || GET_CODE (v->locs->loc) == ENTRY_VALUE
487 || GET_CODE (v->locs->loc) == DEBUG_PARAMETER_REF)
488 return true;
490 /* (plus (value V) (const_int C)) is invariant iff V is invariant. */
491 if (GET_CODE (v->locs->loc) == PLUS
492 && CONST_INT_P (XEXP (v->locs->loc, 1))
493 && GET_CODE (XEXP (v->locs->loc, 0)) == VALUE
494 && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v->locs->loc, 0))))
495 return true;
498 return false;
501 /* Remove from hash table all VALUEs except constants, function
502 invariants and VALUE equivalences. */
505 preserve_constants_and_equivs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
507 cselib_val *v = *x;
509 if (invariant_or_equiv_p (v))
511 cselib_hasher::key lookup = {
512 GET_MODE (v->val_rtx), v->val_rtx, VOIDmode
514 cselib_val **slot
515 = cselib_preserved_hash_table->find_slot_with_hash (&lookup,
516 v->hash, INSERT);
517 gcc_assert (!*slot);
518 *slot = v;
521 cselib_hash_table->clear_slot (x);
523 return 1;
526 /* Remove all entries from the hash table, arranging for the next
527 value to be numbered NUM. */
529 void
530 cselib_reset_table (unsigned int num)
532 unsigned int i;
534 max_value_regs = 0;
536 if (cfa_base_preserved_val)
538 unsigned int regno = cfa_base_preserved_regno;
539 unsigned int new_used_regs = 0;
540 for (i = 0; i < n_used_regs; i++)
541 if (used_regs[i] == regno)
543 new_used_regs = 1;
544 continue;
546 else
547 REG_VALUES (used_regs[i]) = 0;
548 gcc_assert (new_used_regs == 1);
549 n_used_regs = new_used_regs;
550 used_regs[0] = regno;
551 max_value_regs
552 = hard_regno_nregs[regno][GET_MODE (cfa_base_preserved_val->locs->loc)];
554 else
556 for (i = 0; i < n_used_regs; i++)
557 REG_VALUES (used_regs[i]) = 0;
558 n_used_regs = 0;
561 if (cselib_preserve_constants)
562 cselib_hash_table->traverse <void *, preserve_constants_and_equivs>
563 (NULL);
564 else
566 cselib_hash_table->empty ();
567 gcc_checking_assert (!cselib_any_perm_equivs);
570 n_useless_values = 0;
571 n_useless_debug_values = 0;
572 n_debug_values = 0;
574 next_uid = num;
576 first_containing_mem = &dummy_val;
579 /* Return the number of the next value that will be generated. */
581 unsigned int
582 cselib_get_next_uid (void)
584 return next_uid;
587 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
588 INSERTing if requested. When X is part of the address of a MEM,
589 MEMMODE should specify the mode of the MEM. */
591 static cselib_val **
592 cselib_find_slot (machine_mode mode, rtx x, hashval_t hash,
593 enum insert_option insert, machine_mode memmode)
595 cselib_val **slot = NULL;
596 cselib_hasher::key lookup = { mode, x, memmode };
597 if (cselib_preserve_constants)
598 slot = cselib_preserved_hash_table->find_slot_with_hash (&lookup, hash,
599 NO_INSERT);
600 if (!slot)
601 slot = cselib_hash_table->find_slot_with_hash (&lookup, hash, insert);
602 return slot;
605 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
606 only return true for values which point to a cselib_val whose value
607 element has been set to zero, which implies the cselib_val will be
608 removed. */
611 references_value_p (const_rtx x, int only_useless)
613 const enum rtx_code code = GET_CODE (x);
614 const char *fmt = GET_RTX_FORMAT (code);
615 int i, j;
617 if (GET_CODE (x) == VALUE
618 && (! only_useless ||
619 (CSELIB_VAL_PTR (x)->locs == 0 && !PRESERVED_VALUE_P (x))))
620 return 1;
622 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
624 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
625 return 1;
626 else if (fmt[i] == 'E')
627 for (j = 0; j < XVECLEN (x, i); j++)
628 if (references_value_p (XVECEXP (x, i, j), only_useless))
629 return 1;
632 return 0;
635 /* For all locations found in X, delete locations that reference useless
636 values (i.e. values without any location). Called through
637 htab_traverse. */
640 discard_useless_locs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
642 cselib_val *v = *x;
643 struct elt_loc_list **p = &v->locs;
644 bool had_locs = v->locs != NULL;
645 rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
647 while (*p)
649 if (references_value_p ((*p)->loc, 1))
650 unchain_one_elt_loc_list (p);
651 else
652 p = &(*p)->next;
655 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
657 if (setting_insn && DEBUG_INSN_P (setting_insn))
658 n_useless_debug_values++;
659 else
660 n_useless_values++;
661 values_became_useless = 1;
663 return 1;
666 /* If X is a value with no locations, remove it from the hashtable. */
669 discard_useless_values (cselib_val **x, void *info ATTRIBUTE_UNUSED)
671 cselib_val *v = *x;
673 if (v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
675 if (cselib_discard_hook)
676 cselib_discard_hook (v);
678 CSELIB_VAL_PTR (v->val_rtx) = NULL;
679 cselib_hash_table->clear_slot (x);
680 unchain_one_value (v);
681 n_useless_values--;
684 return 1;
687 /* Clean out useless values (i.e. those which no longer have locations
688 associated with them) from the hash table. */
690 static void
691 remove_useless_values (void)
693 cselib_val **p, *v;
695 /* First pass: eliminate locations that reference the value. That in
696 turn can make more values useless. */
699 values_became_useless = 0;
700 cselib_hash_table->traverse <void *, discard_useless_locs> (NULL);
702 while (values_became_useless);
704 /* Second pass: actually remove the values. */
706 p = &first_containing_mem;
707 for (v = *p; v != &dummy_val; v = v->next_containing_mem)
708 if (v->locs && v == canonical_cselib_val (v))
710 *p = v;
711 p = &(*p)->next_containing_mem;
713 *p = &dummy_val;
715 n_useless_values += n_useless_debug_values;
716 n_debug_values -= n_useless_debug_values;
717 n_useless_debug_values = 0;
719 cselib_hash_table->traverse <void *, discard_useless_values> (NULL);
721 gcc_assert (!n_useless_values);
724 /* Arrange for a value to not be removed from the hash table even if
725 it becomes useless. */
727 void
728 cselib_preserve_value (cselib_val *v)
730 PRESERVED_VALUE_P (v->val_rtx) = 1;
733 /* Test whether a value is preserved. */
735 bool
736 cselib_preserved_value_p (cselib_val *v)
738 return PRESERVED_VALUE_P (v->val_rtx);
741 /* Arrange for a REG value to be assumed constant through the whole function,
742 never invalidated and preserved across cselib_reset_table calls. */
744 void
745 cselib_preserve_cfa_base_value (cselib_val *v, unsigned int regno)
747 if (cselib_preserve_constants
748 && v->locs
749 && REG_P (v->locs->loc))
751 cfa_base_preserved_val = v;
752 cfa_base_preserved_regno = regno;
756 /* Clean all non-constant expressions in the hash table, but retain
757 their values. */
759 void
760 cselib_preserve_only_values (void)
762 int i;
764 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
765 cselib_invalidate_regno (i, reg_raw_mode[i]);
767 cselib_invalidate_mem (callmem);
769 remove_useless_values ();
771 gcc_assert (first_containing_mem == &dummy_val);
774 /* Arrange for a value to be marked as based on stack pointer
775 for find_base_term purposes. */
777 void
778 cselib_set_value_sp_based (cselib_val *v)
780 SP_BASED_VALUE_P (v->val_rtx) = 1;
783 /* Test whether a value is based on stack pointer for
784 find_base_term purposes. */
786 bool
787 cselib_sp_based_value_p (cselib_val *v)
789 return SP_BASED_VALUE_P (v->val_rtx);
792 /* Return the mode in which a register was last set. If X is not a
793 register, return its mode. If the mode in which the register was
794 set is not known, or the value was already clobbered, return
795 VOIDmode. */
797 machine_mode
798 cselib_reg_set_mode (const_rtx x)
800 if (!REG_P (x))
801 return GET_MODE (x);
803 if (REG_VALUES (REGNO (x)) == NULL
804 || REG_VALUES (REGNO (x))->elt == NULL)
805 return VOIDmode;
807 return GET_MODE (REG_VALUES (REGNO (x))->elt->val_rtx);
810 /* Return nonzero if we can prove that X and Y contain the same value, taking
811 our gathered information into account. */
814 rtx_equal_for_cselib_p (rtx x, rtx y)
816 return rtx_equal_for_cselib_1 (x, y, VOIDmode);
819 /* If x is a PLUS or an autoinc operation, expand the operation,
820 storing the offset, if any, in *OFF. */
822 static rtx
823 autoinc_split (rtx x, rtx *off, machine_mode memmode)
825 switch (GET_CODE (x))
827 case PLUS:
828 *off = XEXP (x, 1);
829 return XEXP (x, 0);
831 case PRE_DEC:
832 if (memmode == VOIDmode)
833 return x;
835 *off = GEN_INT (-GET_MODE_SIZE (memmode));
836 return XEXP (x, 0);
837 break;
839 case PRE_INC:
840 if (memmode == VOIDmode)
841 return x;
843 *off = GEN_INT (GET_MODE_SIZE (memmode));
844 return XEXP (x, 0);
846 case PRE_MODIFY:
847 return XEXP (x, 1);
849 case POST_DEC:
850 case POST_INC:
851 case POST_MODIFY:
852 return XEXP (x, 0);
854 default:
855 return x;
859 /* Return nonzero if we can prove that X and Y contain the same value,
860 taking our gathered information into account. MEMMODE holds the
861 mode of the enclosing MEM, if any, as required to deal with autoinc
862 addressing modes. If X and Y are not (known to be) part of
863 addresses, MEMMODE should be VOIDmode. */
865 static int
866 rtx_equal_for_cselib_1 (rtx x, rtx y, machine_mode memmode)
868 enum rtx_code code;
869 const char *fmt;
870 int i;
872 if (REG_P (x) || MEM_P (x))
874 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0, memmode);
876 if (e)
877 x = e->val_rtx;
880 if (REG_P (y) || MEM_P (y))
882 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0, memmode);
884 if (e)
885 y = e->val_rtx;
888 if (x == y)
889 return 1;
891 if (GET_CODE (x) == VALUE)
893 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (x));
894 struct elt_loc_list *l;
896 if (GET_CODE (y) == VALUE)
897 return e == canonical_cselib_val (CSELIB_VAL_PTR (y));
899 for (l = e->locs; l; l = l->next)
901 rtx t = l->loc;
903 /* Avoid infinite recursion. We know we have the canonical
904 value, so we can just skip any values in the equivalence
905 list. */
906 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
907 continue;
908 else if (rtx_equal_for_cselib_1 (t, y, memmode))
909 return 1;
912 return 0;
914 else if (GET_CODE (y) == VALUE)
916 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (y));
917 struct elt_loc_list *l;
919 for (l = e->locs; l; l = l->next)
921 rtx t = l->loc;
923 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
924 continue;
925 else if (rtx_equal_for_cselib_1 (x, t, memmode))
926 return 1;
929 return 0;
932 if (GET_MODE (x) != GET_MODE (y))
933 return 0;
935 if (GET_CODE (x) != GET_CODE (y))
937 rtx xorig = x, yorig = y;
938 rtx xoff = NULL, yoff = NULL;
940 x = autoinc_split (x, &xoff, memmode);
941 y = autoinc_split (y, &yoff, memmode);
943 if (!xoff != !yoff)
944 return 0;
946 if (xoff && !rtx_equal_for_cselib_1 (xoff, yoff, memmode))
947 return 0;
949 /* Don't recurse if nothing changed. */
950 if (x != xorig || y != yorig)
951 return rtx_equal_for_cselib_1 (x, y, memmode);
953 return 0;
956 /* These won't be handled correctly by the code below. */
957 switch (GET_CODE (x))
959 CASE_CONST_UNIQUE:
960 case DEBUG_EXPR:
961 return 0;
963 case DEBUG_IMPLICIT_PTR:
964 return DEBUG_IMPLICIT_PTR_DECL (x)
965 == DEBUG_IMPLICIT_PTR_DECL (y);
967 case DEBUG_PARAMETER_REF:
968 return DEBUG_PARAMETER_REF_DECL (x)
969 == DEBUG_PARAMETER_REF_DECL (y);
971 case ENTRY_VALUE:
972 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
973 use rtx_equal_for_cselib_1 to compare the operands. */
974 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
976 case LABEL_REF:
977 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
979 case REG:
980 return REGNO (x) == REGNO (y);
982 case MEM:
983 /* We have to compare any autoinc operations in the addresses
984 using this MEM's mode. */
985 return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x));
987 default:
988 break;
991 code = GET_CODE (x);
992 fmt = GET_RTX_FORMAT (code);
994 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
996 int j;
998 switch (fmt[i])
1000 case 'w':
1001 if (XWINT (x, i) != XWINT (y, i))
1002 return 0;
1003 break;
1005 case 'n':
1006 case 'i':
1007 if (XINT (x, i) != XINT (y, i))
1008 return 0;
1009 break;
1011 case 'V':
1012 case 'E':
1013 /* Two vectors must have the same length. */
1014 if (XVECLEN (x, i) != XVECLEN (y, i))
1015 return 0;
1017 /* And the corresponding elements must match. */
1018 for (j = 0; j < XVECLEN (x, i); j++)
1019 if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
1020 XVECEXP (y, i, j), memmode))
1021 return 0;
1022 break;
1024 case 'e':
1025 if (i == 1
1026 && targetm.commutative_p (x, UNKNOWN)
1027 && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode)
1028 && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode))
1029 return 1;
1030 if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode))
1031 return 0;
1032 break;
1034 case 'S':
1035 case 's':
1036 if (strcmp (XSTR (x, i), XSTR (y, i)))
1037 return 0;
1038 break;
1040 case 'u':
1041 /* These are just backpointers, so they don't matter. */
1042 break;
1044 case '0':
1045 case 't':
1046 break;
1048 /* It is believed that rtx's at this level will never
1049 contain anything but integers and other rtx's,
1050 except for within LABEL_REFs and SYMBOL_REFs. */
1051 default:
1052 gcc_unreachable ();
1055 return 1;
1058 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1059 For registers and memory locations, we look up their cselib_val structure
1060 and return its VALUE element.
1061 Possible reasons for return 0 are: the object is volatile, or we couldn't
1062 find a register or memory location in the table and CREATE is zero. If
1063 CREATE is nonzero, table elts are created for regs and mem.
1064 N.B. this hash function returns the same hash value for RTXes that
1065 differ only in the order of operands, thus it is suitable for comparisons
1066 that take commutativity into account.
1067 If we wanted to also support associative rules, we'd have to use a different
1068 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1069 MEMMODE indicates the mode of an enclosing MEM, and it's only
1070 used to compute autoinc values.
1071 We used to have a MODE argument for hashing for CONST_INTs, but that
1072 didn't make sense, since it caused spurious hash differences between
1073 (set (reg:SI 1) (const_int))
1074 (plus:SI (reg:SI 2) (reg:SI 1))
1076 (plus:SI (reg:SI 2) (const_int))
1077 If the mode is important in any context, it must be checked specifically
1078 in a comparison anyway, since relying on hash differences is unsafe. */
1080 static unsigned int
1081 cselib_hash_rtx (rtx x, int create, machine_mode memmode)
1083 cselib_val *e;
1084 int i, j;
1085 enum rtx_code code;
1086 const char *fmt;
1087 unsigned int hash = 0;
1089 code = GET_CODE (x);
1090 hash += (unsigned) code + (unsigned) GET_MODE (x);
1092 switch (code)
1094 case VALUE:
1095 e = CSELIB_VAL_PTR (x);
1096 return e->hash;
1098 case MEM:
1099 case REG:
1100 e = cselib_lookup (x, GET_MODE (x), create, memmode);
1101 if (! e)
1102 return 0;
1104 return e->hash;
1106 case DEBUG_EXPR:
1107 hash += ((unsigned) DEBUG_EXPR << 7)
1108 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
1109 return hash ? hash : (unsigned int) DEBUG_EXPR;
1111 case DEBUG_IMPLICIT_PTR:
1112 hash += ((unsigned) DEBUG_IMPLICIT_PTR << 7)
1113 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x));
1114 return hash ? hash : (unsigned int) DEBUG_IMPLICIT_PTR;
1116 case DEBUG_PARAMETER_REF:
1117 hash += ((unsigned) DEBUG_PARAMETER_REF << 7)
1118 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x));
1119 return hash ? hash : (unsigned int) DEBUG_PARAMETER_REF;
1121 case ENTRY_VALUE:
1122 /* ENTRY_VALUEs are function invariant, thus try to avoid
1123 recursing on argument if ENTRY_VALUE is one of the
1124 forms emitted by expand_debug_expr, otherwise
1125 ENTRY_VALUE hash would depend on the current value
1126 in some register or memory. */
1127 if (REG_P (ENTRY_VALUE_EXP (x)))
1128 hash += (unsigned int) REG
1129 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x))
1130 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x));
1131 else if (MEM_P (ENTRY_VALUE_EXP (x))
1132 && REG_P (XEXP (ENTRY_VALUE_EXP (x), 0)))
1133 hash += (unsigned int) MEM
1134 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x), 0))
1135 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x), 0));
1136 else
1137 hash += cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode);
1138 return hash ? hash : (unsigned int) ENTRY_VALUE;
1140 case CONST_INT:
1141 hash += ((unsigned) CONST_INT << 7) + UINTVAL (x);
1142 return hash ? hash : (unsigned int) CONST_INT;
1144 case CONST_WIDE_INT:
1145 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
1146 hash += CONST_WIDE_INT_ELT (x, i);
1147 return hash;
1149 case CONST_DOUBLE:
1150 /* This is like the general case, except that it only counts
1151 the integers representing the constant. */
1152 hash += (unsigned) code + (unsigned) GET_MODE (x);
1153 if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (x) == VOIDmode)
1154 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1155 + (unsigned) CONST_DOUBLE_HIGH (x));
1156 else
1157 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
1158 return hash ? hash : (unsigned int) CONST_DOUBLE;
1160 case CONST_FIXED:
1161 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1162 hash += fixed_hash (CONST_FIXED_VALUE (x));
1163 return hash ? hash : (unsigned int) CONST_FIXED;
1165 case CONST_VECTOR:
1167 int units;
1168 rtx elt;
1170 units = CONST_VECTOR_NUNITS (x);
1172 for (i = 0; i < units; ++i)
1174 elt = CONST_VECTOR_ELT (x, i);
1175 hash += cselib_hash_rtx (elt, 0, memmode);
1178 return hash;
1181 /* Assume there is only one rtx object for any given label. */
1182 case LABEL_REF:
1183 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1184 differences and differences between each stage's debugging dumps. */
1185 hash += (((unsigned int) LABEL_REF << 7)
1186 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x)));
1187 return hash ? hash : (unsigned int) LABEL_REF;
1189 case SYMBOL_REF:
1191 /* Don't hash on the symbol's address to avoid bootstrap differences.
1192 Different hash values may cause expressions to be recorded in
1193 different orders and thus different registers to be used in the
1194 final assembler. This also avoids differences in the dump files
1195 between various stages. */
1196 unsigned int h = 0;
1197 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1199 while (*p)
1200 h += (h << 7) + *p++; /* ??? revisit */
1202 hash += ((unsigned int) SYMBOL_REF << 7) + h;
1203 return hash ? hash : (unsigned int) SYMBOL_REF;
1206 case PRE_DEC:
1207 case PRE_INC:
1208 /* We can't compute these without knowing the MEM mode. */
1209 gcc_assert (memmode != VOIDmode);
1210 i = GET_MODE_SIZE (memmode);
1211 if (code == PRE_DEC)
1212 i = -i;
1213 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1214 like (mem:MEMMODE (plus (reg) (const_int I))). */
1215 hash += (unsigned) PLUS - (unsigned)code
1216 + cselib_hash_rtx (XEXP (x, 0), create, memmode)
1217 + cselib_hash_rtx (GEN_INT (i), create, memmode);
1218 return hash ? hash : 1 + (unsigned) PLUS;
1220 case PRE_MODIFY:
1221 gcc_assert (memmode != VOIDmode);
1222 return cselib_hash_rtx (XEXP (x, 1), create, memmode);
1224 case POST_DEC:
1225 case POST_INC:
1226 case POST_MODIFY:
1227 gcc_assert (memmode != VOIDmode);
1228 return cselib_hash_rtx (XEXP (x, 0), create, memmode);
1230 case PC:
1231 case CC0:
1232 case CALL:
1233 case UNSPEC_VOLATILE:
1234 return 0;
1236 case ASM_OPERANDS:
1237 if (MEM_VOLATILE_P (x))
1238 return 0;
1240 break;
1242 default:
1243 break;
1246 i = GET_RTX_LENGTH (code) - 1;
1247 fmt = GET_RTX_FORMAT (code);
1248 for (; i >= 0; i--)
1250 switch (fmt[i])
1252 case 'e':
1254 rtx tem = XEXP (x, i);
1255 unsigned int tem_hash = cselib_hash_rtx (tem, create, memmode);
1257 if (tem_hash == 0)
1258 return 0;
1260 hash += tem_hash;
1262 break;
1263 case 'E':
1264 for (j = 0; j < XVECLEN (x, i); j++)
1266 unsigned int tem_hash
1267 = cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
1269 if (tem_hash == 0)
1270 return 0;
1272 hash += tem_hash;
1274 break;
1276 case 's':
1278 const unsigned char *p = (const unsigned char *) XSTR (x, i);
1280 if (p)
1281 while (*p)
1282 hash += *p++;
1283 break;
1286 case 'i':
1287 hash += XINT (x, i);
1288 break;
1290 case '0':
1291 case 't':
1292 /* unused */
1293 break;
1295 default:
1296 gcc_unreachable ();
1300 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
1303 /* Create a new value structure for VALUE and initialize it. The mode of the
1304 value is MODE. */
1306 static inline cselib_val *
1307 new_cselib_val (unsigned int hash, machine_mode mode, rtx x)
1309 cselib_val *e = (cselib_val *) pool_alloc (cselib_val_pool);
1311 gcc_assert (hash);
1312 gcc_assert (next_uid);
1314 e->hash = hash;
1315 e->uid = next_uid++;
1316 /* We use an alloc pool to allocate this RTL construct because it
1317 accounts for about 8% of the overall memory usage. We know
1318 precisely when we can have VALUE RTXen (when cselib is active)
1319 so we don't need to put them in garbage collected memory.
1320 ??? Why should a VALUE be an RTX in the first place? */
1321 e->val_rtx = (rtx) pool_alloc (value_pool);
1322 memset (e->val_rtx, 0, RTX_HDR_SIZE);
1323 PUT_CODE (e->val_rtx, VALUE);
1324 PUT_MODE (e->val_rtx, mode);
1325 CSELIB_VAL_PTR (e->val_rtx) = e;
1326 e->addr_list = 0;
1327 e->locs = 0;
1328 e->next_containing_mem = 0;
1330 if (dump_file && (dump_flags & TDF_CSELIB))
1332 fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
1333 if (flag_dump_noaddr || flag_dump_unnumbered)
1334 fputs ("# ", dump_file);
1335 else
1336 fprintf (dump_file, "%p ", (void*)e);
1337 print_rtl_single (dump_file, x);
1338 fputc ('\n', dump_file);
1341 return e;
1344 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1345 contains the data at this address. X is a MEM that represents the
1346 value. Update the two value structures to represent this situation. */
1348 static void
1349 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
1351 struct elt_loc_list *l;
1353 addr_elt = canonical_cselib_val (addr_elt);
1354 mem_elt = canonical_cselib_val (mem_elt);
1356 /* Avoid duplicates. */
1357 for (l = mem_elt->locs; l; l = l->next)
1358 if (MEM_P (l->loc)
1359 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
1361 promote_debug_loc (l);
1362 return;
1365 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
1366 new_elt_loc_list (mem_elt,
1367 replace_equiv_address_nv (x, addr_elt->val_rtx));
1368 if (mem_elt->next_containing_mem == NULL)
1370 mem_elt->next_containing_mem = first_containing_mem;
1371 first_containing_mem = mem_elt;
1375 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1376 If CREATE, make a new one if we haven't seen it before. */
1378 static cselib_val *
1379 cselib_lookup_mem (rtx x, int create)
1381 machine_mode mode = GET_MODE (x);
1382 machine_mode addr_mode;
1383 cselib_val **slot;
1384 cselib_val *addr;
1385 cselib_val *mem_elt;
1386 struct elt_list *l;
1388 if (MEM_VOLATILE_P (x) || mode == BLKmode
1389 || !cselib_record_memory
1390 || (FLOAT_MODE_P (mode) && flag_float_store))
1391 return 0;
1393 addr_mode = GET_MODE (XEXP (x, 0));
1394 if (addr_mode == VOIDmode)
1395 addr_mode = Pmode;
1397 /* Look up the value for the address. */
1398 addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
1399 if (! addr)
1400 return 0;
1402 addr = canonical_cselib_val (addr);
1403 /* Find a value that describes a value of our mode at that address. */
1404 for (l = addr->addr_list; l; l = l->next)
1405 if (GET_MODE (l->elt->val_rtx) == mode)
1407 promote_debug_loc (l->elt->locs);
1408 return l->elt;
1411 if (! create)
1412 return 0;
1414 mem_elt = new_cselib_val (next_uid, mode, x);
1415 add_mem_for_addr (addr, mem_elt, x);
1416 slot = cselib_find_slot (mode, x, mem_elt->hash, INSERT, VOIDmode);
1417 *slot = mem_elt;
1418 return mem_elt;
1421 /* Search through the possible substitutions in P. We prefer a non reg
1422 substitution because this allows us to expand the tree further. If
1423 we find, just a reg, take the lowest regno. There may be several
1424 non-reg results, we just take the first one because they will all
1425 expand to the same place. */
1427 static rtx
1428 expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1429 int max_depth)
1431 rtx reg_result = NULL;
1432 unsigned int regno = UINT_MAX;
1433 struct elt_loc_list *p_in = p;
1435 for (; p; p = p->next)
1437 /* Return these right away to avoid returning stack pointer based
1438 expressions for frame pointer and vice versa, which is something
1439 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1440 for more details. */
1441 if (REG_P (p->loc)
1442 && (REGNO (p->loc) == STACK_POINTER_REGNUM
1443 || REGNO (p->loc) == FRAME_POINTER_REGNUM
1444 || REGNO (p->loc) == HARD_FRAME_POINTER_REGNUM
1445 || REGNO (p->loc) == cfa_base_preserved_regno))
1446 return p->loc;
1447 /* Avoid infinite recursion trying to expand a reg into a
1448 the same reg. */
1449 if ((REG_P (p->loc))
1450 && (REGNO (p->loc) < regno)
1451 && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
1453 reg_result = p->loc;
1454 regno = REGNO (p->loc);
1456 /* Avoid infinite recursion and do not try to expand the
1457 value. */
1458 else if (GET_CODE (p->loc) == VALUE
1459 && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1460 continue;
1461 else if (!REG_P (p->loc))
1463 rtx result, note;
1464 if (dump_file && (dump_flags & TDF_CSELIB))
1466 print_inline_rtx (dump_file, p->loc, 0);
1467 fprintf (dump_file, "\n");
1469 if (GET_CODE (p->loc) == LO_SUM
1470 && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1471 && p->setting_insn
1472 && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1473 && XEXP (note, 0) == XEXP (p->loc, 1))
1474 return XEXP (p->loc, 1);
1475 result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
1476 if (result)
1477 return result;
1482 if (regno != UINT_MAX)
1484 rtx result;
1485 if (dump_file && (dump_flags & TDF_CSELIB))
1486 fprintf (dump_file, "r%d\n", regno);
1488 result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
1489 if (result)
1490 return result;
1493 if (dump_file && (dump_flags & TDF_CSELIB))
1495 if (reg_result)
1497 print_inline_rtx (dump_file, reg_result, 0);
1498 fprintf (dump_file, "\n");
1500 else
1501 fprintf (dump_file, "NULL\n");
1503 return reg_result;
1507 /* Forward substitute and expand an expression out to its roots.
1508 This is the opposite of common subexpression. Because local value
1509 numbering is such a weak optimization, the expanded expression is
1510 pretty much unique (not from a pointer equals point of view but
1511 from a tree shape point of view.
1513 This function returns NULL if the expansion fails. The expansion
1514 will fail if there is no value number for one of the operands or if
1515 one of the operands has been overwritten between the current insn
1516 and the beginning of the basic block. For instance x has no
1517 expansion in:
1519 r1 <- r1 + 3
1520 x <- r1 + 8
1522 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1523 It is clear on return. */
1526 cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
1528 struct expand_value_data evd;
1530 evd.regs_active = regs_active;
1531 evd.callback = NULL;
1532 evd.callback_arg = NULL;
1533 evd.dummy = false;
1535 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1538 /* Same as cselib_expand_value_rtx, but using a callback to try to
1539 resolve some expressions. The CB function should return ORIG if it
1540 can't or does not want to deal with a certain RTX. Any other
1541 return value, including NULL, will be used as the expansion for
1542 VALUE, without any further changes. */
1545 cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1546 cselib_expand_callback cb, void *data)
1548 struct expand_value_data evd;
1550 evd.regs_active = regs_active;
1551 evd.callback = cb;
1552 evd.callback_arg = data;
1553 evd.dummy = false;
1555 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1558 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1559 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1560 would return NULL or non-NULL, without allocating new rtx. */
1562 bool
1563 cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1564 cselib_expand_callback cb, void *data)
1566 struct expand_value_data evd;
1568 evd.regs_active = regs_active;
1569 evd.callback = cb;
1570 evd.callback_arg = data;
1571 evd.dummy = true;
1573 return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1576 /* Internal implementation of cselib_expand_value_rtx and
1577 cselib_expand_value_rtx_cb. */
1579 static rtx
1580 cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1581 int max_depth)
1583 rtx copy, scopy;
1584 int i, j;
1585 RTX_CODE code;
1586 const char *format_ptr;
1587 machine_mode mode;
1589 code = GET_CODE (orig);
1591 /* For the context of dse, if we end up expand into a huge tree, we
1592 will not have a useful address, so we might as well just give up
1593 quickly. */
1594 if (max_depth <= 0)
1595 return NULL;
1597 switch (code)
1599 case REG:
1601 struct elt_list *l = REG_VALUES (REGNO (orig));
1603 if (l && l->elt == NULL)
1604 l = l->next;
1605 for (; l; l = l->next)
1606 if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1608 rtx result;
1609 unsigned regno = REGNO (orig);
1611 /* The only thing that we are not willing to do (this
1612 is requirement of dse and if others potential uses
1613 need this function we should add a parm to control
1614 it) is that we will not substitute the
1615 STACK_POINTER_REGNUM, FRAME_POINTER or the
1616 HARD_FRAME_POINTER.
1618 These expansions confuses the code that notices that
1619 stores into the frame go dead at the end of the
1620 function and that the frame is not effected by calls
1621 to subroutines. If you allow the
1622 STACK_POINTER_REGNUM substitution, then dse will
1623 think that parameter pushing also goes dead which is
1624 wrong. If you allow the FRAME_POINTER or the
1625 HARD_FRAME_POINTER then you lose the opportunity to
1626 make the frame assumptions. */
1627 if (regno == STACK_POINTER_REGNUM
1628 || regno == FRAME_POINTER_REGNUM
1629 || regno == HARD_FRAME_POINTER_REGNUM
1630 || regno == cfa_base_preserved_regno)
1631 return orig;
1633 bitmap_set_bit (evd->regs_active, regno);
1635 if (dump_file && (dump_flags & TDF_CSELIB))
1636 fprintf (dump_file, "expanding: r%d into: ", regno);
1638 result = expand_loc (l->elt->locs, evd, max_depth);
1639 bitmap_clear_bit (evd->regs_active, regno);
1641 if (result)
1642 return result;
1643 else
1644 return orig;
1648 CASE_CONST_ANY:
1649 case SYMBOL_REF:
1650 case CODE_LABEL:
1651 case PC:
1652 case CC0:
1653 case SCRATCH:
1654 /* SCRATCH must be shared because they represent distinct values. */
1655 return orig;
1656 case CLOBBER:
1657 if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1658 return orig;
1659 break;
1661 case CONST:
1662 if (shared_const_p (orig))
1663 return orig;
1664 break;
1666 case SUBREG:
1668 rtx subreg;
1670 if (evd->callback)
1672 subreg = evd->callback (orig, evd->regs_active, max_depth,
1673 evd->callback_arg);
1674 if (subreg != orig)
1675 return subreg;
1678 subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1679 max_depth - 1);
1680 if (!subreg)
1681 return NULL;
1682 scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1683 GET_MODE (SUBREG_REG (orig)),
1684 SUBREG_BYTE (orig));
1685 if (scopy == NULL
1686 || (GET_CODE (scopy) == SUBREG
1687 && !REG_P (SUBREG_REG (scopy))
1688 && !MEM_P (SUBREG_REG (scopy))))
1689 return NULL;
1691 return scopy;
1694 case VALUE:
1696 rtx result;
1698 if (dump_file && (dump_flags & TDF_CSELIB))
1700 fputs ("\nexpanding ", dump_file);
1701 print_rtl_single (dump_file, orig);
1702 fputs (" into...", dump_file);
1705 if (evd->callback)
1707 result = evd->callback (orig, evd->regs_active, max_depth,
1708 evd->callback_arg);
1710 if (result != orig)
1711 return result;
1714 result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
1715 return result;
1718 case DEBUG_EXPR:
1719 if (evd->callback)
1720 return evd->callback (orig, evd->regs_active, max_depth,
1721 evd->callback_arg);
1722 return orig;
1724 default:
1725 break;
1728 /* Copy the various flags, fields, and other information. We assume
1729 that all fields need copying, and then clear the fields that should
1730 not be copied. That is the sensible default behavior, and forces
1731 us to explicitly document why we are *not* copying a flag. */
1732 if (evd->dummy)
1733 copy = NULL;
1734 else
1735 copy = shallow_copy_rtx (orig);
1737 format_ptr = GET_RTX_FORMAT (code);
1739 for (i = 0; i < GET_RTX_LENGTH (code); i++)
1740 switch (*format_ptr++)
1742 case 'e':
1743 if (XEXP (orig, i) != NULL)
1745 rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1746 max_depth - 1);
1747 if (!result)
1748 return NULL;
1749 if (copy)
1750 XEXP (copy, i) = result;
1752 break;
1754 case 'E':
1755 case 'V':
1756 if (XVEC (orig, i) != NULL)
1758 if (copy)
1759 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1760 for (j = 0; j < XVECLEN (orig, i); j++)
1762 rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1763 evd, max_depth - 1);
1764 if (!result)
1765 return NULL;
1766 if (copy)
1767 XVECEXP (copy, i, j) = result;
1770 break;
1772 case 't':
1773 case 'w':
1774 case 'i':
1775 case 's':
1776 case 'S':
1777 case 'T':
1778 case 'u':
1779 case 'B':
1780 case '0':
1781 /* These are left unchanged. */
1782 break;
1784 default:
1785 gcc_unreachable ();
1788 if (evd->dummy)
1789 return orig;
1791 mode = GET_MODE (copy);
1792 /* If an operand has been simplified into CONST_INT, which doesn't
1793 have a mode and the mode isn't derivable from whole rtx's mode,
1794 try simplify_*_operation first with mode from original's operand
1795 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1796 scopy = copy;
1797 switch (GET_RTX_CLASS (code))
1799 case RTX_UNARY:
1800 if (CONST_INT_P (XEXP (copy, 0))
1801 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1803 scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1804 GET_MODE (XEXP (orig, 0)));
1805 if (scopy)
1806 return scopy;
1808 break;
1809 case RTX_COMM_ARITH:
1810 case RTX_BIN_ARITH:
1811 /* These expressions can derive operand modes from the whole rtx's mode. */
1812 break;
1813 case RTX_TERNARY:
1814 case RTX_BITFIELD_OPS:
1815 if (CONST_INT_P (XEXP (copy, 0))
1816 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1818 scopy = simplify_ternary_operation (code, mode,
1819 GET_MODE (XEXP (orig, 0)),
1820 XEXP (copy, 0), XEXP (copy, 1),
1821 XEXP (copy, 2));
1822 if (scopy)
1823 return scopy;
1825 break;
1826 case RTX_COMPARE:
1827 case RTX_COMM_COMPARE:
1828 if (CONST_INT_P (XEXP (copy, 0))
1829 && GET_MODE (XEXP (copy, 1)) == VOIDmode
1830 && (GET_MODE (XEXP (orig, 0)) != VOIDmode
1831 || GET_MODE (XEXP (orig, 1)) != VOIDmode))
1833 scopy = simplify_relational_operation (code, mode,
1834 (GET_MODE (XEXP (orig, 0))
1835 != VOIDmode)
1836 ? GET_MODE (XEXP (orig, 0))
1837 : GET_MODE (XEXP (orig, 1)),
1838 XEXP (copy, 0),
1839 XEXP (copy, 1));
1840 if (scopy)
1841 return scopy;
1843 break;
1844 default:
1845 break;
1847 scopy = simplify_rtx (copy);
1848 if (scopy)
1849 return scopy;
1850 return copy;
1853 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1854 with VALUE expressions. This way, it becomes independent of changes
1855 to registers and memory.
1856 X isn't actually modified; if modifications are needed, new rtl is
1857 allocated. However, the return value can share rtl with X.
1858 If X is within a MEM, MEMMODE must be the mode of the MEM. */
1861 cselib_subst_to_values (rtx x, machine_mode memmode)
1863 enum rtx_code code = GET_CODE (x);
1864 const char *fmt = GET_RTX_FORMAT (code);
1865 cselib_val *e;
1866 struct elt_list *l;
1867 rtx copy = x;
1868 int i;
1870 switch (code)
1872 case REG:
1873 l = REG_VALUES (REGNO (x));
1874 if (l && l->elt == NULL)
1875 l = l->next;
1876 for (; l; l = l->next)
1877 if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
1878 return l->elt->val_rtx;
1880 gcc_unreachable ();
1882 case MEM:
1883 e = cselib_lookup_mem (x, 0);
1884 /* This used to happen for autoincrements, but we deal with them
1885 properly now. Remove the if stmt for the next release. */
1886 if (! e)
1888 /* Assign a value that doesn't match any other. */
1889 e = new_cselib_val (next_uid, GET_MODE (x), x);
1891 return e->val_rtx;
1893 case ENTRY_VALUE:
1894 e = cselib_lookup (x, GET_MODE (x), 0, memmode);
1895 if (! e)
1896 break;
1897 return e->val_rtx;
1899 CASE_CONST_ANY:
1900 return x;
1902 case PRE_DEC:
1903 case PRE_INC:
1904 gcc_assert (memmode != VOIDmode);
1905 i = GET_MODE_SIZE (memmode);
1906 if (code == PRE_DEC)
1907 i = -i;
1908 return cselib_subst_to_values (plus_constant (GET_MODE (x),
1909 XEXP (x, 0), i),
1910 memmode);
1912 case PRE_MODIFY:
1913 gcc_assert (memmode != VOIDmode);
1914 return cselib_subst_to_values (XEXP (x, 1), memmode);
1916 case POST_DEC:
1917 case POST_INC:
1918 case POST_MODIFY:
1919 gcc_assert (memmode != VOIDmode);
1920 return cselib_subst_to_values (XEXP (x, 0), memmode);
1922 default:
1923 break;
1926 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1928 if (fmt[i] == 'e')
1930 rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
1932 if (t != XEXP (x, i))
1934 if (x == copy)
1935 copy = shallow_copy_rtx (x);
1936 XEXP (copy, i) = t;
1939 else if (fmt[i] == 'E')
1941 int j;
1943 for (j = 0; j < XVECLEN (x, i); j++)
1945 rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
1947 if (t != XVECEXP (x, i, j))
1949 if (XVEC (x, i) == XVEC (copy, i))
1951 if (x == copy)
1952 copy = shallow_copy_rtx (x);
1953 XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
1955 XVECEXP (copy, i, j) = t;
1961 return copy;
1964 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
1967 cselib_subst_to_values_from_insn (rtx x, machine_mode memmode, rtx_insn *insn)
1969 rtx ret;
1970 gcc_assert (!cselib_current_insn);
1971 cselib_current_insn = insn;
1972 ret = cselib_subst_to_values (x, memmode);
1973 cselib_current_insn = NULL;
1974 return ret;
1977 /* Look up the rtl expression X in our tables and return the value it
1978 has. If CREATE is zero, we return NULL if we don't know the value.
1979 Otherwise, we create a new one if possible, using mode MODE if X
1980 doesn't have a mode (i.e. because it's a constant). When X is part
1981 of an address, MEMMODE should be the mode of the enclosing MEM if
1982 we're tracking autoinc expressions. */
1984 static cselib_val *
1985 cselib_lookup_1 (rtx x, machine_mode mode,
1986 int create, machine_mode memmode)
1988 cselib_val **slot;
1989 cselib_val *e;
1990 unsigned int hashval;
1992 if (GET_MODE (x) != VOIDmode)
1993 mode = GET_MODE (x);
1995 if (GET_CODE (x) == VALUE)
1996 return CSELIB_VAL_PTR (x);
1998 if (REG_P (x))
2000 struct elt_list *l;
2001 unsigned int i = REGNO (x);
2003 l = REG_VALUES (i);
2004 if (l && l->elt == NULL)
2005 l = l->next;
2006 for (; l; l = l->next)
2007 if (mode == GET_MODE (l->elt->val_rtx))
2009 promote_debug_loc (l->elt->locs);
2010 return l->elt;
2013 if (! create)
2014 return 0;
2016 if (i < FIRST_PSEUDO_REGISTER)
2018 unsigned int n = hard_regno_nregs[i][mode];
2020 if (n > max_value_regs)
2021 max_value_regs = n;
2024 e = new_cselib_val (next_uid, GET_MODE (x), x);
2025 new_elt_loc_list (e, x);
2026 if (REG_VALUES (i) == 0)
2028 /* Maintain the invariant that the first entry of
2029 REG_VALUES, if present, must be the value used to set the
2030 register, or NULL. */
2031 used_regs[n_used_regs++] = i;
2032 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
2034 else if (cselib_preserve_constants
2035 && GET_MODE_CLASS (mode) == MODE_INT)
2037 /* During var-tracking, try harder to find equivalences
2038 for SUBREGs. If a setter sets say a DImode register
2039 and user uses that register only in SImode, add a lowpart
2040 subreg location. */
2041 struct elt_list *lwider = NULL;
2042 l = REG_VALUES (i);
2043 if (l && l->elt == NULL)
2044 l = l->next;
2045 for (; l; l = l->next)
2046 if (GET_MODE_CLASS (GET_MODE (l->elt->val_rtx)) == MODE_INT
2047 && GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2048 > GET_MODE_SIZE (mode)
2049 && (lwider == NULL
2050 || GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
2051 < GET_MODE_SIZE (GET_MODE (lwider->elt->val_rtx))))
2053 struct elt_loc_list *el;
2054 if (i < FIRST_PSEUDO_REGISTER
2055 && hard_regno_nregs[i][GET_MODE (l->elt->val_rtx)] != 1)
2056 continue;
2057 for (el = l->elt->locs; el; el = el->next)
2058 if (!REG_P (el->loc))
2059 break;
2060 if (el)
2061 lwider = l;
2063 if (lwider)
2065 rtx sub = lowpart_subreg (mode, lwider->elt->val_rtx,
2066 GET_MODE (lwider->elt->val_rtx));
2067 if (sub)
2068 new_elt_loc_list (e, sub);
2071 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
2072 slot = cselib_find_slot (mode, x, e->hash, INSERT, memmode);
2073 *slot = e;
2074 return e;
2077 if (MEM_P (x))
2078 return cselib_lookup_mem (x, create);
2080 hashval = cselib_hash_rtx (x, create, memmode);
2081 /* Can't even create if hashing is not possible. */
2082 if (! hashval)
2083 return 0;
2085 slot = cselib_find_slot (mode, x, hashval,
2086 create ? INSERT : NO_INSERT, memmode);
2087 if (slot == 0)
2088 return 0;
2090 e = (cselib_val *) *slot;
2091 if (e)
2092 return e;
2094 e = new_cselib_val (hashval, mode, x);
2096 /* We have to fill the slot before calling cselib_subst_to_values:
2097 the hash table is inconsistent until we do so, and
2098 cselib_subst_to_values will need to do lookups. */
2099 *slot = e;
2100 new_elt_loc_list (e, cselib_subst_to_values (x, memmode));
2101 return e;
2104 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2106 cselib_val *
2107 cselib_lookup_from_insn (rtx x, machine_mode mode,
2108 int create, machine_mode memmode, rtx_insn *insn)
2110 cselib_val *ret;
2112 gcc_assert (!cselib_current_insn);
2113 cselib_current_insn = insn;
2115 ret = cselib_lookup (x, mode, create, memmode);
2117 cselib_current_insn = NULL;
2119 return ret;
2122 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2123 maintains invariants related with debug insns. */
2125 cselib_val *
2126 cselib_lookup (rtx x, machine_mode mode,
2127 int create, machine_mode memmode)
2129 cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
2131 /* ??? Should we return NULL if we're not to create an entry, the
2132 found loc is a debug loc and cselib_current_insn is not DEBUG?
2133 If so, we should also avoid converting val to non-DEBUG; probably
2134 easiest setting cselib_current_insn to NULL before the call
2135 above. */
2137 if (dump_file && (dump_flags & TDF_CSELIB))
2139 fputs ("cselib lookup ", dump_file);
2140 print_inline_rtx (dump_file, x, 2);
2141 fprintf (dump_file, " => %u:%u\n",
2142 ret ? ret->uid : 0,
2143 ret ? ret->hash : 0);
2146 return ret;
2149 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2150 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2151 is used to determine how many hard registers are being changed. If MODE
2152 is VOIDmode, then only REGNO is being changed; this is used when
2153 invalidating call clobbered registers across a call. */
2155 static void
2156 cselib_invalidate_regno (unsigned int regno, machine_mode mode)
2158 unsigned int endregno;
2159 unsigned int i;
2161 /* If we see pseudos after reload, something is _wrong_. */
2162 gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
2163 || reg_renumber[regno] < 0);
2165 /* Determine the range of registers that must be invalidated. For
2166 pseudos, only REGNO is affected. For hard regs, we must take MODE
2167 into account, and we must also invalidate lower register numbers
2168 if they contain values that overlap REGNO. */
2169 if (regno < FIRST_PSEUDO_REGISTER)
2171 gcc_assert (mode != VOIDmode);
2173 if (regno < max_value_regs)
2174 i = 0;
2175 else
2176 i = regno - max_value_regs;
2178 endregno = end_hard_regno (mode, regno);
2180 else
2182 i = regno;
2183 endregno = regno + 1;
2186 for (; i < endregno; i++)
2188 struct elt_list **l = &REG_VALUES (i);
2190 /* Go through all known values for this reg; if it overlaps the range
2191 we're invalidating, remove the value. */
2192 while (*l)
2194 cselib_val *v = (*l)->elt;
2195 bool had_locs;
2196 rtx_insn *setting_insn;
2197 struct elt_loc_list **p;
2198 unsigned int this_last = i;
2200 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
2201 this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
2203 if (this_last < regno || v == NULL
2204 || (v == cfa_base_preserved_val
2205 && i == cfa_base_preserved_regno))
2207 l = &(*l)->next;
2208 continue;
2211 /* We have an overlap. */
2212 if (*l == REG_VALUES (i))
2214 /* Maintain the invariant that the first entry of
2215 REG_VALUES, if present, must be the value used to set
2216 the register, or NULL. This is also nice because
2217 then we won't push the same regno onto user_regs
2218 multiple times. */
2219 (*l)->elt = NULL;
2220 l = &(*l)->next;
2222 else
2223 unchain_one_elt_list (l);
2225 v = canonical_cselib_val (v);
2227 had_locs = v->locs != NULL;
2228 setting_insn = v->locs ? v->locs->setting_insn : NULL;
2230 /* Now, we clear the mapping from value to reg. It must exist, so
2231 this code will crash intentionally if it doesn't. */
2232 for (p = &v->locs; ; p = &(*p)->next)
2234 rtx x = (*p)->loc;
2236 if (REG_P (x) && REGNO (x) == i)
2238 unchain_one_elt_loc_list (p);
2239 break;
2243 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2245 if (setting_insn && DEBUG_INSN_P (setting_insn))
2246 n_useless_debug_values++;
2247 else
2248 n_useless_values++;
2254 /* Invalidate any locations in the table which are changed because of a
2255 store to MEM_RTX. If this is called because of a non-const call
2256 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2258 static void
2259 cselib_invalidate_mem (rtx mem_rtx)
2261 cselib_val **vp, *v, *next;
2262 int num_mems = 0;
2263 rtx mem_addr;
2265 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2266 mem_rtx = canon_rtx (mem_rtx);
2268 vp = &first_containing_mem;
2269 for (v = *vp; v != &dummy_val; v = next)
2271 bool has_mem = false;
2272 struct elt_loc_list **p = &v->locs;
2273 bool had_locs = v->locs != NULL;
2274 rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
2276 while (*p)
2278 rtx x = (*p)->loc;
2279 cselib_val *addr;
2280 struct elt_list **mem_chain;
2282 /* MEMs may occur in locations only at the top level; below
2283 that every MEM or REG is substituted by its VALUE. */
2284 if (!MEM_P (x))
2286 p = &(*p)->next;
2287 continue;
2289 if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
2290 && ! canon_anti_dependence (x, false, mem_rtx,
2291 GET_MODE (mem_rtx), mem_addr))
2293 has_mem = true;
2294 num_mems++;
2295 p = &(*p)->next;
2296 continue;
2299 /* This one overlaps. */
2300 /* We must have a mapping from this MEM's address to the
2301 value (E). Remove that, too. */
2302 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
2303 addr = canonical_cselib_val (addr);
2304 gcc_checking_assert (v == canonical_cselib_val (v));
2305 mem_chain = &addr->addr_list;
2306 for (;;)
2308 cselib_val *canon = canonical_cselib_val ((*mem_chain)->elt);
2310 if (canon == v)
2312 unchain_one_elt_list (mem_chain);
2313 break;
2316 /* Record canonicalized elt. */
2317 (*mem_chain)->elt = canon;
2319 mem_chain = &(*mem_chain)->next;
2322 unchain_one_elt_loc_list (p);
2325 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2327 if (setting_insn && DEBUG_INSN_P (setting_insn))
2328 n_useless_debug_values++;
2329 else
2330 n_useless_values++;
2333 next = v->next_containing_mem;
2334 if (has_mem)
2336 *vp = v;
2337 vp = &(*vp)->next_containing_mem;
2339 else
2340 v->next_containing_mem = NULL;
2342 *vp = &dummy_val;
2345 /* Invalidate DEST, which is being assigned to or clobbered. */
2347 void
2348 cselib_invalidate_rtx (rtx dest)
2350 while (GET_CODE (dest) == SUBREG
2351 || GET_CODE (dest) == ZERO_EXTRACT
2352 || GET_CODE (dest) == STRICT_LOW_PART)
2353 dest = XEXP (dest, 0);
2355 if (REG_P (dest))
2356 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
2357 else if (MEM_P (dest))
2358 cselib_invalidate_mem (dest);
2361 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2363 static void
2364 cselib_invalidate_rtx_note_stores (rtx dest, const_rtx ignore ATTRIBUTE_UNUSED,
2365 void *data ATTRIBUTE_UNUSED)
2367 cselib_invalidate_rtx (dest);
2370 /* Record the result of a SET instruction. DEST is being set; the source
2371 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2372 describes its address. */
2374 static void
2375 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
2377 if (src_elt == 0 || side_effects_p (dest))
2378 return;
2380 if (REG_P (dest))
2382 unsigned int dreg = REGNO (dest);
2383 if (dreg < FIRST_PSEUDO_REGISTER)
2385 unsigned int n = REG_NREGS (dest);
2387 if (n > max_value_regs)
2388 max_value_regs = n;
2391 if (REG_VALUES (dreg) == 0)
2393 used_regs[n_used_regs++] = dreg;
2394 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2396 else
2398 /* The register should have been invalidated. */
2399 gcc_assert (REG_VALUES (dreg)->elt == 0);
2400 REG_VALUES (dreg)->elt = src_elt;
2403 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2404 n_useless_values--;
2405 new_elt_loc_list (src_elt, dest);
2407 else if (MEM_P (dest) && dest_addr_elt != 0
2408 && cselib_record_memory)
2410 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2411 n_useless_values--;
2412 add_mem_for_addr (dest_addr_elt, src_elt, dest);
2416 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2418 void
2419 cselib_add_permanent_equiv (cselib_val *elt, rtx x, rtx_insn *insn)
2421 cselib_val *nelt;
2422 rtx_insn *save_cselib_current_insn = cselib_current_insn;
2424 gcc_checking_assert (elt);
2425 gcc_checking_assert (PRESERVED_VALUE_P (elt->val_rtx));
2426 gcc_checking_assert (!side_effects_p (x));
2428 cselib_current_insn = insn;
2430 nelt = cselib_lookup (x, GET_MODE (elt->val_rtx), 1, VOIDmode);
2432 if (nelt != elt)
2434 cselib_any_perm_equivs = true;
2436 if (!PRESERVED_VALUE_P (nelt->val_rtx))
2437 cselib_preserve_value (nelt);
2439 new_elt_loc_list (nelt, elt->val_rtx);
2442 cselib_current_insn = save_cselib_current_insn;
2445 /* Return TRUE if any permanent equivalences have been recorded since
2446 the table was last initialized. */
2447 bool
2448 cselib_have_permanent_equivalences (void)
2450 return cselib_any_perm_equivs;
2453 /* There is no good way to determine how many elements there can be
2454 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2455 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2457 struct cselib_record_autoinc_data
2459 struct cselib_set *sets;
2460 int n_sets;
2463 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2464 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2466 static int
2467 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
2468 rtx dest, rtx src, rtx srcoff, void *arg)
2470 struct cselib_record_autoinc_data *data;
2471 data = (struct cselib_record_autoinc_data *)arg;
2473 data->sets[data->n_sets].dest = dest;
2475 if (srcoff)
2476 data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
2477 else
2478 data->sets[data->n_sets].src = src;
2480 data->n_sets++;
2482 return 0;
2485 /* Record the effects of any sets and autoincs in INSN. */
2486 static void
2487 cselib_record_sets (rtx_insn *insn)
2489 int n_sets = 0;
2490 int i;
2491 struct cselib_set sets[MAX_SETS];
2492 rtx body = PATTERN (insn);
2493 rtx cond = 0;
2494 int n_sets_before_autoinc;
2495 struct cselib_record_autoinc_data data;
2497 body = PATTERN (insn);
2498 if (GET_CODE (body) == COND_EXEC)
2500 cond = COND_EXEC_TEST (body);
2501 body = COND_EXEC_CODE (body);
2504 /* Find all sets. */
2505 if (GET_CODE (body) == SET)
2507 sets[0].src = SET_SRC (body);
2508 sets[0].dest = SET_DEST (body);
2509 n_sets = 1;
2511 else if (GET_CODE (body) == PARALLEL)
2513 /* Look through the PARALLEL and record the values being
2514 set, if possible. Also handle any CLOBBERs. */
2515 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2517 rtx x = XVECEXP (body, 0, i);
2519 if (GET_CODE (x) == SET)
2521 sets[n_sets].src = SET_SRC (x);
2522 sets[n_sets].dest = SET_DEST (x);
2523 n_sets++;
2528 if (n_sets == 1
2529 && MEM_P (sets[0].src)
2530 && !cselib_record_memory
2531 && MEM_READONLY_P (sets[0].src))
2533 rtx note = find_reg_equal_equiv_note (insn);
2535 if (note && CONSTANT_P (XEXP (note, 0)))
2536 sets[0].src = XEXP (note, 0);
2539 data.sets = sets;
2540 data.n_sets = n_sets_before_autoinc = n_sets;
2541 for_each_inc_dec (PATTERN (insn), cselib_record_autoinc_cb, &data);
2542 n_sets = data.n_sets;
2544 /* Look up the values that are read. Do this before invalidating the
2545 locations that are written. */
2546 for (i = 0; i < n_sets; i++)
2548 rtx dest = sets[i].dest;
2550 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2551 the low part after invalidating any knowledge about larger modes. */
2552 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2553 sets[i].dest = dest = XEXP (dest, 0);
2555 /* We don't know how to record anything but REG or MEM. */
2556 if (REG_P (dest)
2557 || (MEM_P (dest) && cselib_record_memory))
2559 rtx src = sets[i].src;
2560 if (cond)
2561 src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
2562 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
2563 if (MEM_P (dest))
2565 machine_mode address_mode = get_address_mode (dest);
2567 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
2568 address_mode, 1,
2569 GET_MODE (dest));
2571 else
2572 sets[i].dest_addr_elt = 0;
2576 if (cselib_record_sets_hook)
2577 cselib_record_sets_hook (insn, sets, n_sets);
2579 /* Invalidate all locations written by this insn. Note that the elts we
2580 looked up in the previous loop aren't affected, just some of their
2581 locations may go away. */
2582 note_stores (body, cselib_invalidate_rtx_note_stores, NULL);
2584 for (i = n_sets_before_autoinc; i < n_sets; i++)
2585 cselib_invalidate_rtx (sets[i].dest);
2587 /* If this is an asm, look for duplicate sets. This can happen when the
2588 user uses the same value as an output multiple times. This is valid
2589 if the outputs are not actually used thereafter. Treat this case as
2590 if the value isn't actually set. We do this by smashing the destination
2591 to pc_rtx, so that we won't record the value later. */
2592 if (n_sets >= 2 && asm_noperands (body) >= 0)
2594 for (i = 0; i < n_sets; i++)
2596 rtx dest = sets[i].dest;
2597 if (REG_P (dest) || MEM_P (dest))
2599 int j;
2600 for (j = i + 1; j < n_sets; j++)
2601 if (rtx_equal_p (dest, sets[j].dest))
2603 sets[i].dest = pc_rtx;
2604 sets[j].dest = pc_rtx;
2610 /* Now enter the equivalences in our tables. */
2611 for (i = 0; i < n_sets; i++)
2613 rtx dest = sets[i].dest;
2614 if (REG_P (dest)
2615 || (MEM_P (dest) && cselib_record_memory))
2616 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2620 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
2622 bool
2623 fp_setter_insn (rtx_insn *insn)
2625 rtx expr, pat = NULL_RTX;
2627 if (!RTX_FRAME_RELATED_P (insn))
2628 return false;
2630 expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2631 if (expr)
2632 pat = XEXP (expr, 0);
2633 if (!modified_in_p (hard_frame_pointer_rtx, pat ? pat : insn))
2634 return false;
2636 /* Don't return true for frame pointer restores in the epilogue. */
2637 if (find_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx))
2638 return false;
2639 return true;
2642 /* Record the effects of INSN. */
2644 void
2645 cselib_process_insn (rtx_insn *insn)
2647 int i;
2648 rtx x;
2650 cselib_current_insn = insn;
2652 /* Forget everything at a CODE_LABEL or a setjmp. */
2653 if ((LABEL_P (insn)
2654 || (CALL_P (insn)
2655 && find_reg_note (insn, REG_SETJMP, NULL)))
2656 && !cselib_preserve_constants)
2658 cselib_reset_table (next_uid);
2659 cselib_current_insn = NULL;
2660 return;
2663 if (! INSN_P (insn))
2665 cselib_current_insn = NULL;
2666 return;
2669 /* If this is a call instruction, forget anything stored in a
2670 call clobbered register, or, if this is not a const call, in
2671 memory. */
2672 if (CALL_P (insn))
2674 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2675 if (call_used_regs[i]
2676 || (REG_VALUES (i) && REG_VALUES (i)->elt
2677 && HARD_REGNO_CALL_PART_CLOBBERED (i,
2678 GET_MODE (REG_VALUES (i)->elt->val_rtx))))
2679 cselib_invalidate_regno (i, reg_raw_mode[i]);
2681 /* Since it is not clear how cselib is going to be used, be
2682 conservative here and treat looping pure or const functions
2683 as if they were regular functions. */
2684 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
2685 || !(RTL_CONST_OR_PURE_CALL_P (insn)))
2686 cselib_invalidate_mem (callmem);
2689 cselib_record_sets (insn);
2691 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2692 after we have processed the insn. */
2693 if (CALL_P (insn))
2695 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
2696 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
2697 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
2698 /* Flush evertything on setjmp. */
2699 if (cselib_preserve_constants
2700 && find_reg_note (insn, REG_SETJMP, NULL))
2702 cselib_preserve_only_values ();
2703 cselib_reset_table (next_uid);
2707 /* On setter of the hard frame pointer if frame_pointer_needed,
2708 invalidate stack_pointer_rtx, so that sp and {,h}fp based
2709 VALUEs are distinct. */
2710 if (reload_completed
2711 && frame_pointer_needed
2712 && fp_setter_insn (insn))
2713 cselib_invalidate_rtx (stack_pointer_rtx);
2715 cselib_current_insn = NULL;
2717 if (n_useless_values > MAX_USELESS_VALUES
2718 /* remove_useless_values is linear in the hash table size. Avoid
2719 quadratic behavior for very large hashtables with very few
2720 useless elements. */
2721 && ((unsigned int)n_useless_values
2722 > (cselib_hash_table->elements () - n_debug_values) / 4))
2723 remove_useless_values ();
2726 /* Initialize cselib for one pass. The caller must also call
2727 init_alias_analysis. */
2729 void
2730 cselib_init (int record_what)
2732 elt_list_pool = create_alloc_pool ("elt_list",
2733 sizeof (struct elt_list), 10);
2734 elt_loc_list_pool = create_alloc_pool ("elt_loc_list",
2735 sizeof (struct elt_loc_list), 10);
2736 cselib_val_pool = create_alloc_pool ("cselib_val_list",
2737 sizeof (cselib_val), 10);
2738 value_pool = create_alloc_pool ("value", RTX_CODE_SIZE (VALUE), 100);
2739 cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
2740 cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
2741 cselib_any_perm_equivs = false;
2743 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2744 see canon_true_dependence. This is only created once. */
2745 if (! callmem)
2746 callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2748 cselib_nregs = max_reg_num ();
2750 /* We preserve reg_values to allow expensive clearing of the whole thing.
2751 Reallocate it however if it happens to be too large. */
2752 if (!reg_values || reg_values_size < cselib_nregs
2753 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
2755 free (reg_values);
2756 /* Some space for newly emit instructions so we don't end up
2757 reallocating in between passes. */
2758 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
2759 reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
2761 used_regs = XNEWVEC (unsigned int, cselib_nregs);
2762 n_used_regs = 0;
2763 cselib_hash_table = new hash_table<cselib_hasher> (31);
2764 if (cselib_preserve_constants)
2765 cselib_preserved_hash_table = new hash_table<cselib_hasher> (31);
2766 next_uid = 1;
2769 /* Called when the current user is done with cselib. */
2771 void
2772 cselib_finish (void)
2774 bool preserved = cselib_preserve_constants;
2775 cselib_discard_hook = NULL;
2776 cselib_preserve_constants = false;
2777 cselib_any_perm_equivs = false;
2778 cfa_base_preserved_val = NULL;
2779 cfa_base_preserved_regno = INVALID_REGNUM;
2780 free_alloc_pool (elt_list_pool);
2781 free_alloc_pool (elt_loc_list_pool);
2782 free_alloc_pool (cselib_val_pool);
2783 free_alloc_pool (value_pool);
2784 cselib_clear_table ();
2785 delete cselib_hash_table;
2786 cselib_hash_table = NULL;
2787 if (preserved)
2788 delete cselib_preserved_hash_table;
2789 cselib_preserved_hash_table = NULL;
2790 free (used_regs);
2791 used_regs = 0;
2792 n_useless_values = 0;
2793 n_useless_debug_values = 0;
2794 n_debug_values = 0;
2795 next_uid = 0;
2798 /* Dump the cselib_val *X to FILE *OUT. */
2801 dump_cselib_val (cselib_val **x, FILE *out)
2803 cselib_val *v = *x;
2804 bool need_lf = true;
2806 print_inline_rtx (out, v->val_rtx, 0);
2808 if (v->locs)
2810 struct elt_loc_list *l = v->locs;
2811 if (need_lf)
2813 fputc ('\n', out);
2814 need_lf = false;
2816 fputs (" locs:", out);
2819 if (l->setting_insn)
2820 fprintf (out, "\n from insn %i ",
2821 INSN_UID (l->setting_insn));
2822 else
2823 fprintf (out, "\n ");
2824 print_inline_rtx (out, l->loc, 4);
2826 while ((l = l->next));
2827 fputc ('\n', out);
2829 else
2831 fputs (" no locs", out);
2832 need_lf = true;
2835 if (v->addr_list)
2837 struct elt_list *e = v->addr_list;
2838 if (need_lf)
2840 fputc ('\n', out);
2841 need_lf = false;
2843 fputs (" addr list:", out);
2846 fputs ("\n ", out);
2847 print_inline_rtx (out, e->elt->val_rtx, 2);
2849 while ((e = e->next));
2850 fputc ('\n', out);
2852 else
2854 fputs (" no addrs", out);
2855 need_lf = true;
2858 if (v->next_containing_mem == &dummy_val)
2859 fputs (" last mem\n", out);
2860 else if (v->next_containing_mem)
2862 fputs (" next mem ", out);
2863 print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
2864 fputc ('\n', out);
2866 else if (need_lf)
2867 fputc ('\n', out);
2869 return 1;
2872 /* Dump to OUT everything in the CSELIB table. */
2874 void
2875 dump_cselib_table (FILE *out)
2877 fprintf (out, "cselib hash table:\n");
2878 cselib_hash_table->traverse <FILE *, dump_cselib_val> (out);
2879 fprintf (out, "cselib preserved hash table:\n");
2880 cselib_preserved_hash_table->traverse <FILE *, dump_cselib_val> (out);
2881 if (first_containing_mem != &dummy_val)
2883 fputs ("first mem ", out);
2884 print_inline_rtx (out, first_containing_mem->val_rtx, 2);
2885 fputc ('\n', out);
2887 fprintf (out, "next uid %i\n", next_uid);
2890 #include "gt-cselib.h"