testsuite: Update pr79251 ilp32 store counts.
[official-gcc.git] / gcc / cselib.c
blob2d34a914c6bc2ff677c08d9f66562a0dbf605ac8
1 /* Common subexpression elimination library for GNU compiler.
2 Copyright (C) 1987-2021 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "df.h"
28 #include "memmodel.h"
29 #include "tm_p.h"
30 #include "regs.h"
31 #include "emit-rtl.h"
32 #include "dumpfile.h"
33 #include "cselib.h"
34 #include "function-abi.h"
36 /* A list of cselib_val structures. */
37 struct elt_list
39 struct elt_list *next;
40 cselib_val *elt;
43 static bool cselib_record_memory;
44 static bool cselib_preserve_constants;
45 static bool cselib_any_perm_equivs;
46 static inline void promote_debug_loc (struct elt_loc_list *l);
47 static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
48 static void new_elt_loc_list (cselib_val *, rtx);
49 static void unchain_one_value (cselib_val *);
50 static void unchain_one_elt_list (struct elt_list **);
51 static void unchain_one_elt_loc_list (struct elt_loc_list **);
52 static void remove_useless_values (void);
53 static unsigned int cselib_hash_rtx (rtx, int, machine_mode);
54 static cselib_val *new_cselib_val (unsigned int, machine_mode, rtx);
55 static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
56 static cselib_val *cselib_lookup_mem (rtx, int);
57 static void cselib_invalidate_regno (unsigned int, machine_mode);
58 static void cselib_invalidate_mem (rtx);
59 static void cselib_record_set (rtx, cselib_val *, cselib_val *);
60 static void cselib_record_sets (rtx_insn *);
61 static rtx autoinc_split (rtx, rtx *, machine_mode);
63 #define PRESERVED_VALUE_P(RTX) \
64 (RTL_FLAG_CHECK1 ("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
66 #define SP_BASED_VALUE_P(RTX) \
67 (RTL_FLAG_CHECK1 ("SP_BASED_VALUE_P", (RTX), VALUE)->jump)
69 #define SP_DERIVED_VALUE_P(RTX) \
70 (RTL_FLAG_CHECK1 ("SP_DERIVED_VALUE_P", (RTX), VALUE)->call)
72 struct expand_value_data
74 bitmap regs_active;
75 cselib_expand_callback callback;
76 void *callback_arg;
77 bool dummy;
80 static rtx cselib_expand_value_rtx_1 (rtx, struct expand_value_data *, int);
82 /* There are three ways in which cselib can look up an rtx:
83 - for a REG, the reg_values table (which is indexed by regno) is used
84 - for a MEM, we recursively look up its address and then follow the
85 addr_list of that value
86 - for everything else, we compute a hash value and go through the hash
87 table. Since different rtx's can still have the same hash value,
88 this involves walking the table entries for a given value and comparing
89 the locations of the entries with the rtx we are looking up. */
91 struct cselib_hasher : nofree_ptr_hash <cselib_val>
93 struct key {
94 /* The rtx value and its mode (needed separately for constant
95 integers). */
96 machine_mode mode;
97 rtx x;
98 /* The mode of the contaning MEM, if any, otherwise VOIDmode. */
99 machine_mode memmode;
101 typedef key *compare_type;
102 static inline hashval_t hash (const cselib_val *);
103 static inline bool equal (const cselib_val *, const key *);
106 /* The hash function for our hash table. The value is always computed with
107 cselib_hash_rtx when adding an element; this function just extracts the
108 hash value from a cselib_val structure. */
110 inline hashval_t
111 cselib_hasher::hash (const cselib_val *v)
113 return v->hash;
116 /* The equality test for our hash table. The first argument V is a table
117 element (i.e. a cselib_val), while the second arg X is an rtx. We know
118 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
119 CONST of an appropriate mode. */
121 inline bool
122 cselib_hasher::equal (const cselib_val *v, const key *x_arg)
124 struct elt_loc_list *l;
125 rtx x = x_arg->x;
126 machine_mode mode = x_arg->mode;
127 machine_mode memmode = x_arg->memmode;
129 if (mode != GET_MODE (v->val_rtx))
130 return false;
132 if (GET_CODE (x) == VALUE)
133 return x == v->val_rtx;
135 if (SP_DERIVED_VALUE_P (v->val_rtx) && GET_MODE (x) == Pmode)
137 rtx xoff = NULL;
138 if (autoinc_split (x, &xoff, memmode) == v->val_rtx && xoff == NULL_RTX)
139 return true;
142 /* We don't guarantee that distinct rtx's have different hash values,
143 so we need to do a comparison. */
144 for (l = v->locs; l; l = l->next)
145 if (rtx_equal_for_cselib_1 (l->loc, x, memmode, 0))
147 promote_debug_loc (l);
148 return true;
151 return false;
154 /* A table that enables us to look up elts by their value. */
155 static hash_table<cselib_hasher> *cselib_hash_table;
157 /* A table to hold preserved values. */
158 static hash_table<cselib_hasher> *cselib_preserved_hash_table;
160 /* This is a global so we don't have to pass this through every function.
161 It is used in new_elt_loc_list to set SETTING_INSN. */
162 static rtx_insn *cselib_current_insn;
164 /* The unique id that the next create value will take. */
165 static unsigned int next_uid;
167 /* The number of registers we had when the varrays were last resized. */
168 static unsigned int cselib_nregs;
170 /* Count values without known locations, or with only locations that
171 wouldn't have been known except for debug insns. Whenever this
172 grows too big, we remove these useless values from the table.
174 Counting values with only debug values is a bit tricky. We don't
175 want to increment n_useless_values when we create a value for a
176 debug insn, for this would get n_useless_values out of sync, but we
177 want increment it if all locs in the list that were ever referenced
178 in nondebug insns are removed from the list.
180 In the general case, once we do that, we'd have to stop accepting
181 nondebug expressions in the loc list, to avoid having two values
182 equivalent that, without debug insns, would have been made into
183 separate values. However, because debug insns never introduce
184 equivalences themselves (no assignments), the only means for
185 growing loc lists is through nondebug assignments. If the locs
186 also happen to be referenced in debug insns, it will work just fine.
188 A consequence of this is that there's at most one debug-only loc in
189 each loc list. If we keep it in the first entry, testing whether
190 we have a debug-only loc list takes O(1).
192 Furthermore, since any additional entry in a loc list containing a
193 debug loc would have to come from an assignment (nondebug) that
194 references both the initial debug loc and the newly-equivalent loc,
195 the initial debug loc would be promoted to a nondebug loc, and the
196 loc list would not contain debug locs any more.
198 So the only case we have to be careful with in order to keep
199 n_useless_values in sync between debug and nondebug compilations is
200 to avoid incrementing n_useless_values when removing the single loc
201 from a value that turns out to not appear outside debug values. We
202 increment n_useless_debug_values instead, and leave such values
203 alone until, for other reasons, we garbage-collect useless
204 values. */
205 static int n_useless_values;
206 static int n_useless_debug_values;
208 /* Count values whose locs have been taken exclusively from debug
209 insns for the entire life of the value. */
210 static int n_debug_values;
212 /* Number of useless values before we remove them from the hash table. */
213 #define MAX_USELESS_VALUES 32
215 /* This table maps from register number to values. It does not
216 contain pointers to cselib_val structures, but rather elt_lists.
217 The purpose is to be able to refer to the same register in
218 different modes. The first element of the list defines the mode in
219 which the register was set; if the mode is unknown or the value is
220 no longer valid in that mode, ELT will be NULL for the first
221 element. */
222 static struct elt_list **reg_values;
223 static unsigned int reg_values_size;
224 #define REG_VALUES(i) reg_values[i]
226 /* The largest number of hard regs used by any entry added to the
227 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
228 static unsigned int max_value_regs;
230 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
231 in cselib_clear_table() for fast emptying. */
232 static unsigned int *used_regs;
233 static unsigned int n_used_regs;
235 /* We pass this to cselib_invalidate_mem to invalidate all of
236 memory for a non-const call instruction. */
237 static GTY(()) rtx callmem;
239 /* Set by discard_useless_locs if it deleted the last location of any
240 value. */
241 static int values_became_useless;
243 /* Used as stop element of the containing_mem list so we can check
244 presence in the list by checking the next pointer. */
245 static cselib_val dummy_val;
247 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
248 that is constant through the whole function and should never be
249 eliminated. */
250 static cselib_val *cfa_base_preserved_val;
251 static unsigned int cfa_base_preserved_regno = INVALID_REGNUM;
253 /* Used to list all values that contain memory reference.
254 May or may not contain the useless values - the list is compacted
255 each time memory is invalidated. */
256 static cselib_val *first_containing_mem = &dummy_val;
258 static object_allocator<elt_list> elt_list_pool ("elt_list");
259 static object_allocator<elt_loc_list> elt_loc_list_pool ("elt_loc_list");
260 static object_allocator<cselib_val> cselib_val_pool ("cselib_val_list");
262 static pool_allocator value_pool ("value", RTX_CODE_SIZE (VALUE));
264 /* If nonnull, cselib will call this function before freeing useless
265 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
266 void (*cselib_discard_hook) (cselib_val *);
268 /* If nonnull, cselib will call this function before recording sets or
269 even clobbering outputs of INSN. All the recorded sets will be
270 represented in the array sets[n_sets]. new_val_min can be used to
271 tell whether values present in sets are introduced by this
272 instruction. */
273 void (*cselib_record_sets_hook) (rtx_insn *insn, struct cselib_set *sets,
274 int n_sets);
278 /* Allocate a struct elt_list and fill in its two elements with the
279 arguments. */
281 static inline struct elt_list *
282 new_elt_list (struct elt_list *next, cselib_val *elt)
284 elt_list *el = elt_list_pool.allocate ();
285 el->next = next;
286 el->elt = elt;
287 return el;
290 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
291 list. */
293 static inline void
294 new_elt_loc_list (cselib_val *val, rtx loc)
296 struct elt_loc_list *el, *next = val->locs;
298 gcc_checking_assert (!next || !next->setting_insn
299 || !DEBUG_INSN_P (next->setting_insn)
300 || cselib_current_insn == next->setting_insn);
302 /* If we're creating the first loc in a debug insn context, we've
303 just created a debug value. Count it. */
304 if (!next && cselib_current_insn && DEBUG_INSN_P (cselib_current_insn))
305 n_debug_values++;
307 val = canonical_cselib_val (val);
308 next = val->locs;
310 if (GET_CODE (loc) == VALUE)
312 loc = canonical_cselib_val (CSELIB_VAL_PTR (loc))->val_rtx;
314 gcc_checking_assert (PRESERVED_VALUE_P (loc)
315 == PRESERVED_VALUE_P (val->val_rtx));
317 if (val->val_rtx == loc)
318 return;
319 else if (val->uid > CSELIB_VAL_PTR (loc)->uid)
321 /* Reverse the insertion. */
322 new_elt_loc_list (CSELIB_VAL_PTR (loc), val->val_rtx);
323 return;
326 gcc_checking_assert (val->uid < CSELIB_VAL_PTR (loc)->uid);
328 if (CSELIB_VAL_PTR (loc)->locs)
330 /* Bring all locs from LOC to VAL. */
331 for (el = CSELIB_VAL_PTR (loc)->locs; el->next; el = el->next)
333 /* Adjust values that have LOC as canonical so that VAL
334 becomes their canonical. */
335 if (el->loc && GET_CODE (el->loc) == VALUE)
337 gcc_checking_assert (CSELIB_VAL_PTR (el->loc)->locs->loc
338 == loc);
339 CSELIB_VAL_PTR (el->loc)->locs->loc = val->val_rtx;
342 el->next = val->locs;
343 next = val->locs = CSELIB_VAL_PTR (loc)->locs;
346 if (CSELIB_VAL_PTR (loc)->addr_list)
348 /* Bring in addr_list into canonical node. */
349 struct elt_list *last = CSELIB_VAL_PTR (loc)->addr_list;
350 while (last->next)
351 last = last->next;
352 last->next = val->addr_list;
353 val->addr_list = CSELIB_VAL_PTR (loc)->addr_list;
354 CSELIB_VAL_PTR (loc)->addr_list = NULL;
357 if (CSELIB_VAL_PTR (loc)->next_containing_mem != NULL
358 && val->next_containing_mem == NULL)
360 /* Add VAL to the containing_mem list after LOC. LOC will
361 be removed when we notice it doesn't contain any
362 MEMs. */
363 val->next_containing_mem = CSELIB_VAL_PTR (loc)->next_containing_mem;
364 CSELIB_VAL_PTR (loc)->next_containing_mem = val;
367 /* Chain LOC back to VAL. */
368 el = elt_loc_list_pool.allocate ();
369 el->loc = val->val_rtx;
370 el->setting_insn = cselib_current_insn;
371 el->next = NULL;
372 CSELIB_VAL_PTR (loc)->locs = el;
375 el = elt_loc_list_pool.allocate ();
376 el->loc = loc;
377 el->setting_insn = cselib_current_insn;
378 el->next = next;
379 val->locs = el;
382 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
383 originating from a debug insn, maintaining the debug values
384 count. */
386 static inline void
387 promote_debug_loc (struct elt_loc_list *l)
389 if (l && l->setting_insn && DEBUG_INSN_P (l->setting_insn)
390 && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
392 n_debug_values--;
393 l->setting_insn = cselib_current_insn;
394 if (cselib_preserve_constants && l->next)
396 gcc_assert (l->next->setting_insn
397 && DEBUG_INSN_P (l->next->setting_insn)
398 && !l->next->next);
399 l->next->setting_insn = cselib_current_insn;
401 else
402 gcc_assert (!l->next);
406 /* The elt_list at *PL is no longer needed. Unchain it and free its
407 storage. */
409 static inline void
410 unchain_one_elt_list (struct elt_list **pl)
412 struct elt_list *l = *pl;
414 *pl = l->next;
415 elt_list_pool.remove (l);
418 /* Likewise for elt_loc_lists. */
420 static void
421 unchain_one_elt_loc_list (struct elt_loc_list **pl)
423 struct elt_loc_list *l = *pl;
425 *pl = l->next;
426 elt_loc_list_pool.remove (l);
429 /* Likewise for cselib_vals. This also frees the addr_list associated with
430 V. */
432 static void
433 unchain_one_value (cselib_val *v)
435 while (v->addr_list)
436 unchain_one_elt_list (&v->addr_list);
438 cselib_val_pool.remove (v);
441 /* Remove all entries from the hash table. Also used during
442 initialization. */
444 void
445 cselib_clear_table (void)
447 cselib_reset_table (1);
450 /* Return TRUE if V is a constant, a function invariant or a VALUE
451 equivalence; FALSE otherwise. */
453 static bool
454 invariant_or_equiv_p (cselib_val *v)
456 struct elt_loc_list *l;
458 if (v == cfa_base_preserved_val)
459 return true;
461 /* Keep VALUE equivalences around. */
462 for (l = v->locs; l; l = l->next)
463 if (GET_CODE (l->loc) == VALUE)
464 return true;
466 if (v->locs != NULL
467 && v->locs->next == NULL)
469 if (CONSTANT_P (v->locs->loc)
470 && (GET_CODE (v->locs->loc) != CONST
471 || !references_value_p (v->locs->loc, 0)))
472 return true;
473 /* Although a debug expr may be bound to different expressions,
474 we can preserve it as if it was constant, to get unification
475 and proper merging within var-tracking. */
476 if (GET_CODE (v->locs->loc) == DEBUG_EXPR
477 || GET_CODE (v->locs->loc) == DEBUG_IMPLICIT_PTR
478 || GET_CODE (v->locs->loc) == ENTRY_VALUE
479 || GET_CODE (v->locs->loc) == DEBUG_PARAMETER_REF)
480 return true;
482 /* (plus (value V) (const_int C)) is invariant iff V is invariant. */
483 if (GET_CODE (v->locs->loc) == PLUS
484 && CONST_INT_P (XEXP (v->locs->loc, 1))
485 && GET_CODE (XEXP (v->locs->loc, 0)) == VALUE
486 && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v->locs->loc, 0))))
487 return true;
490 return false;
493 /* Remove from hash table all VALUEs except constants, function
494 invariants and VALUE equivalences. */
497 preserve_constants_and_equivs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
499 cselib_val *v = *x;
501 if (invariant_or_equiv_p (v))
503 cselib_hasher::key lookup = {
504 GET_MODE (v->val_rtx), v->val_rtx, VOIDmode
506 cselib_val **slot
507 = cselib_preserved_hash_table->find_slot_with_hash (&lookup,
508 v->hash, INSERT);
509 gcc_assert (!*slot);
510 *slot = v;
513 cselib_hash_table->clear_slot (x);
515 return 1;
518 /* Remove all entries from the hash table, arranging for the next
519 value to be numbered NUM. */
521 void
522 cselib_reset_table (unsigned int num)
524 unsigned int i;
526 max_value_regs = 0;
528 if (cfa_base_preserved_val)
530 unsigned int regno = cfa_base_preserved_regno;
531 unsigned int new_used_regs = 0;
532 for (i = 0; i < n_used_regs; i++)
533 if (used_regs[i] == regno)
535 new_used_regs = 1;
536 continue;
538 else
539 REG_VALUES (used_regs[i]) = 0;
540 gcc_assert (new_used_regs == 1);
541 n_used_regs = new_used_regs;
542 used_regs[0] = regno;
543 max_value_regs
544 = hard_regno_nregs (regno,
545 GET_MODE (cfa_base_preserved_val->locs->loc));
547 /* If cfa_base is sp + const_int, need to preserve also the
548 SP_DERIVED_VALUE_P value. */
549 for (struct elt_loc_list *l = cfa_base_preserved_val->locs;
550 l; l = l->next)
551 if (GET_CODE (l->loc) == PLUS
552 && GET_CODE (XEXP (l->loc, 0)) == VALUE
553 && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
554 && CONST_INT_P (XEXP (l->loc, 1)))
556 if (! invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (l->loc, 0))))
558 rtx val = cfa_base_preserved_val->val_rtx;
559 rtx_insn *save_cselib_current_insn = cselib_current_insn;
560 cselib_current_insn = l->setting_insn;
561 new_elt_loc_list (CSELIB_VAL_PTR (XEXP (l->loc, 0)),
562 plus_constant (Pmode, val,
563 -UINTVAL (XEXP (l->loc, 1))));
564 cselib_current_insn = save_cselib_current_insn;
566 break;
569 else
571 for (i = 0; i < n_used_regs; i++)
572 REG_VALUES (used_regs[i]) = 0;
573 n_used_regs = 0;
576 if (cselib_preserve_constants)
577 cselib_hash_table->traverse <void *, preserve_constants_and_equivs> (NULL);
578 else
580 cselib_hash_table->empty ();
581 gcc_checking_assert (!cselib_any_perm_equivs);
584 n_useless_values = 0;
585 n_useless_debug_values = 0;
586 n_debug_values = 0;
588 next_uid = num;
590 first_containing_mem = &dummy_val;
593 /* Return the number of the next value that will be generated. */
595 unsigned int
596 cselib_get_next_uid (void)
598 return next_uid;
601 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
602 INSERTing if requested. When X is part of the address of a MEM,
603 MEMMODE should specify the mode of the MEM. */
605 static cselib_val **
606 cselib_find_slot (machine_mode mode, rtx x, hashval_t hash,
607 enum insert_option insert, machine_mode memmode)
609 cselib_val **slot = NULL;
610 cselib_hasher::key lookup = { mode, x, memmode };
611 if (cselib_preserve_constants)
612 slot = cselib_preserved_hash_table->find_slot_with_hash (&lookup, hash,
613 NO_INSERT);
614 if (!slot)
615 slot = cselib_hash_table->find_slot_with_hash (&lookup, hash, insert);
616 return slot;
619 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
620 only return true for values which point to a cselib_val whose value
621 element has been set to zero, which implies the cselib_val will be
622 removed. */
625 references_value_p (const_rtx x, int only_useless)
627 const enum rtx_code code = GET_CODE (x);
628 const char *fmt = GET_RTX_FORMAT (code);
629 int i, j;
631 if (GET_CODE (x) == VALUE
632 && (! only_useless
633 || (CSELIB_VAL_PTR (x)->locs == 0 && !PRESERVED_VALUE_P (x))))
634 return 1;
636 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
638 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
639 return 1;
640 else if (fmt[i] == 'E')
641 for (j = 0; j < XVECLEN (x, i); j++)
642 if (references_value_p (XVECEXP (x, i, j), only_useless))
643 return 1;
646 return 0;
649 /* Return true if V is a useless VALUE and can be discarded as such. */
651 static bool
652 cselib_useless_value_p (cselib_val *v)
654 return (v->locs == 0
655 && !PRESERVED_VALUE_P (v->val_rtx)
656 && !SP_DERIVED_VALUE_P (v->val_rtx));
659 /* For all locations found in X, delete locations that reference useless
660 values (i.e. values without any location). Called through
661 htab_traverse. */
664 discard_useless_locs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
666 cselib_val *v = *x;
667 struct elt_loc_list **p = &v->locs;
668 bool had_locs = v->locs != NULL;
669 rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
671 while (*p)
673 if (references_value_p ((*p)->loc, 1))
674 unchain_one_elt_loc_list (p);
675 else
676 p = &(*p)->next;
679 if (had_locs && cselib_useless_value_p (v))
681 if (setting_insn && DEBUG_INSN_P (setting_insn))
682 n_useless_debug_values++;
683 else
684 n_useless_values++;
685 values_became_useless = 1;
687 return 1;
690 /* If X is a value with no locations, remove it from the hashtable. */
693 discard_useless_values (cselib_val **x, void *info ATTRIBUTE_UNUSED)
695 cselib_val *v = *x;
697 if (v->locs == 0 && cselib_useless_value_p (v))
699 if (cselib_discard_hook)
700 cselib_discard_hook (v);
702 CSELIB_VAL_PTR (v->val_rtx) = NULL;
703 cselib_hash_table->clear_slot (x);
704 unchain_one_value (v);
705 n_useless_values--;
708 return 1;
711 /* Clean out useless values (i.e. those which no longer have locations
712 associated with them) from the hash table. */
714 static void
715 remove_useless_values (void)
717 cselib_val **p, *v;
719 /* First pass: eliminate locations that reference the value. That in
720 turn can make more values useless. */
723 values_became_useless = 0;
724 cselib_hash_table->traverse <void *, discard_useless_locs> (NULL);
726 while (values_became_useless);
728 /* Second pass: actually remove the values. */
730 p = &first_containing_mem;
731 for (v = *p; v != &dummy_val; v = v->next_containing_mem)
732 if (v->locs && v == canonical_cselib_val (v))
734 *p = v;
735 p = &(*p)->next_containing_mem;
737 *p = &dummy_val;
739 n_useless_values += n_useless_debug_values;
740 n_debug_values -= n_useless_debug_values;
741 n_useless_debug_values = 0;
743 cselib_hash_table->traverse <void *, discard_useless_values> (NULL);
745 gcc_assert (!n_useless_values);
748 /* Arrange for a value to not be removed from the hash table even if
749 it becomes useless. */
751 void
752 cselib_preserve_value (cselib_val *v)
754 PRESERVED_VALUE_P (v->val_rtx) = 1;
757 /* Test whether a value is preserved. */
759 bool
760 cselib_preserved_value_p (cselib_val *v)
762 return PRESERVED_VALUE_P (v->val_rtx);
765 /* Arrange for a REG value to be assumed constant through the whole function,
766 never invalidated and preserved across cselib_reset_table calls. */
768 void
769 cselib_preserve_cfa_base_value (cselib_val *v, unsigned int regno)
771 if (cselib_preserve_constants
772 && v->locs
773 && REG_P (v->locs->loc))
775 cfa_base_preserved_val = v;
776 cfa_base_preserved_regno = regno;
780 /* Clean all non-constant expressions in the hash table, but retain
781 their values. */
783 void
784 cselib_preserve_only_values (void)
786 int i;
788 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
789 cselib_invalidate_regno (i, reg_raw_mode[i]);
791 cselib_invalidate_mem (callmem);
793 remove_useless_values ();
795 gcc_assert (first_containing_mem == &dummy_val);
798 /* Arrange for a value to be marked as based on stack pointer
799 for find_base_term purposes. */
801 void
802 cselib_set_value_sp_based (cselib_val *v)
804 SP_BASED_VALUE_P (v->val_rtx) = 1;
807 /* Test whether a value is based on stack pointer for
808 find_base_term purposes. */
810 bool
811 cselib_sp_based_value_p (cselib_val *v)
813 return SP_BASED_VALUE_P (v->val_rtx);
816 /* Return the mode in which a register was last set. If X is not a
817 register, return its mode. If the mode in which the register was
818 set is not known, or the value was already clobbered, return
819 VOIDmode. */
821 machine_mode
822 cselib_reg_set_mode (const_rtx x)
824 if (!REG_P (x))
825 return GET_MODE (x);
827 if (REG_VALUES (REGNO (x)) == NULL
828 || REG_VALUES (REGNO (x))->elt == NULL)
829 return VOIDmode;
831 return GET_MODE (REG_VALUES (REGNO (x))->elt->val_rtx);
834 /* If x is a PLUS or an autoinc operation, expand the operation,
835 storing the offset, if any, in *OFF. */
837 static rtx
838 autoinc_split (rtx x, rtx *off, machine_mode memmode)
840 switch (GET_CODE (x))
842 case PLUS:
843 *off = XEXP (x, 1);
844 x = XEXP (x, 0);
845 break;
847 case PRE_DEC:
848 if (memmode == VOIDmode)
849 return x;
851 *off = gen_int_mode (-GET_MODE_SIZE (memmode), GET_MODE (x));
852 x = XEXP (x, 0);
853 break;
855 case PRE_INC:
856 if (memmode == VOIDmode)
857 return x;
859 *off = gen_int_mode (GET_MODE_SIZE (memmode), GET_MODE (x));
860 x = XEXP (x, 0);
861 break;
863 case PRE_MODIFY:
864 x = XEXP (x, 1);
865 break;
867 case POST_DEC:
868 case POST_INC:
869 case POST_MODIFY:
870 x = XEXP (x, 0);
871 break;
873 default:
874 break;
877 if (GET_MODE (x) == Pmode
878 && (REG_P (x) || MEM_P (x) || GET_CODE (x) == VALUE)
879 && (*off == NULL_RTX || CONST_INT_P (*off)))
881 cselib_val *e;
882 if (GET_CODE (x) == VALUE)
883 e = CSELIB_VAL_PTR (x);
884 else
885 e = cselib_lookup (x, GET_MODE (x), 0, memmode);
886 if (e)
888 if (SP_DERIVED_VALUE_P (e->val_rtx)
889 && (*off == NULL_RTX || *off == const0_rtx))
891 *off = NULL_RTX;
892 return e->val_rtx;
894 for (struct elt_loc_list *l = e->locs; l; l = l->next)
895 if (GET_CODE (l->loc) == PLUS
896 && GET_CODE (XEXP (l->loc, 0)) == VALUE
897 && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
898 && CONST_INT_P (XEXP (l->loc, 1)))
900 if (*off == NULL_RTX)
901 *off = XEXP (l->loc, 1);
902 else
903 *off = plus_constant (Pmode, *off,
904 INTVAL (XEXP (l->loc, 1)));
905 if (*off == const0_rtx)
906 *off = NULL_RTX;
907 return XEXP (l->loc, 0);
911 return x;
914 /* Return nonzero if we can prove that X and Y contain the same value,
915 taking our gathered information into account. MEMMODE holds the
916 mode of the enclosing MEM, if any, as required to deal with autoinc
917 addressing modes. If X and Y are not (known to be) part of
918 addresses, MEMMODE should be VOIDmode. */
921 rtx_equal_for_cselib_1 (rtx x, rtx y, machine_mode memmode, int depth)
923 enum rtx_code code;
924 const char *fmt;
925 int i;
927 if (REG_P (x) || MEM_P (x))
929 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0, memmode);
931 if (e)
932 x = e->val_rtx;
935 if (REG_P (y) || MEM_P (y))
937 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0, memmode);
939 if (e)
940 y = e->val_rtx;
943 if (x == y)
944 return 1;
946 if (GET_CODE (x) == VALUE)
948 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (x));
949 struct elt_loc_list *l;
951 if (GET_CODE (y) == VALUE)
952 return e == canonical_cselib_val (CSELIB_VAL_PTR (y));
954 if ((SP_DERIVED_VALUE_P (x)
955 || SP_DERIVED_VALUE_P (e->val_rtx))
956 && GET_MODE (y) == Pmode)
958 rtx yoff = NULL;
959 rtx yr = autoinc_split (y, &yoff, memmode);
960 if ((yr == x || yr == e->val_rtx) && yoff == NULL_RTX)
961 return 1;
964 if (depth == 128)
965 return 0;
967 for (l = e->locs; l; l = l->next)
969 rtx t = l->loc;
971 /* Avoid infinite recursion. We know we have the canonical
972 value, so we can just skip any values in the equivalence
973 list. */
974 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
975 continue;
976 else if (rtx_equal_for_cselib_1 (t, y, memmode, depth + 1))
977 return 1;
980 return 0;
982 else if (GET_CODE (y) == VALUE)
984 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (y));
985 struct elt_loc_list *l;
987 if ((SP_DERIVED_VALUE_P (y)
988 || SP_DERIVED_VALUE_P (e->val_rtx))
989 && GET_MODE (x) == Pmode)
991 rtx xoff = NULL;
992 rtx xr = autoinc_split (x, &xoff, memmode);
993 if ((xr == y || xr == e->val_rtx) && xoff == NULL_RTX)
994 return 1;
997 if (depth == 128)
998 return 0;
1000 for (l = e->locs; l; l = l->next)
1002 rtx t = l->loc;
1004 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
1005 continue;
1006 else if (rtx_equal_for_cselib_1 (x, t, memmode, depth + 1))
1007 return 1;
1010 return 0;
1013 if (GET_MODE (x) != GET_MODE (y))
1014 return 0;
1016 if (GET_CODE (x) != GET_CODE (y)
1017 || (GET_CODE (x) == PLUS
1018 && GET_MODE (x) == Pmode
1019 && CONST_INT_P (XEXP (x, 1))
1020 && CONST_INT_P (XEXP (y, 1))))
1022 rtx xorig = x, yorig = y;
1023 rtx xoff = NULL, yoff = NULL;
1025 x = autoinc_split (x, &xoff, memmode);
1026 y = autoinc_split (y, &yoff, memmode);
1028 /* Don't recurse if nothing changed. */
1029 if (x != xorig || y != yorig)
1031 if (!xoff != !yoff)
1032 return 0;
1034 if (xoff && !rtx_equal_for_cselib_1 (xoff, yoff, memmode, depth))
1035 return 0;
1037 return rtx_equal_for_cselib_1 (x, y, memmode, depth);
1040 if (GET_CODE (xorig) != GET_CODE (yorig))
1041 return 0;
1044 /* These won't be handled correctly by the code below. */
1045 switch (GET_CODE (x))
1047 CASE_CONST_UNIQUE:
1048 case DEBUG_EXPR:
1049 return 0;
1051 case DEBUG_IMPLICIT_PTR:
1052 return DEBUG_IMPLICIT_PTR_DECL (x)
1053 == DEBUG_IMPLICIT_PTR_DECL (y);
1055 case DEBUG_PARAMETER_REF:
1056 return DEBUG_PARAMETER_REF_DECL (x)
1057 == DEBUG_PARAMETER_REF_DECL (y);
1059 case ENTRY_VALUE:
1060 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
1061 use rtx_equal_for_cselib_1 to compare the operands. */
1062 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
1064 case LABEL_REF:
1065 return label_ref_label (x) == label_ref_label (y);
1067 case REG:
1068 return REGNO (x) == REGNO (y);
1070 case MEM:
1071 /* We have to compare any autoinc operations in the addresses
1072 using this MEM's mode. */
1073 return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x),
1074 depth);
1076 default:
1077 break;
1080 code = GET_CODE (x);
1081 fmt = GET_RTX_FORMAT (code);
1083 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1085 int j;
1087 switch (fmt[i])
1089 case 'w':
1090 if (XWINT (x, i) != XWINT (y, i))
1091 return 0;
1092 break;
1094 case 'n':
1095 case 'i':
1096 if (XINT (x, i) != XINT (y, i))
1097 return 0;
1098 break;
1100 case 'p':
1101 if (maybe_ne (SUBREG_BYTE (x), SUBREG_BYTE (y)))
1102 return 0;
1103 break;
1105 case 'V':
1106 case 'E':
1107 /* Two vectors must have the same length. */
1108 if (XVECLEN (x, i) != XVECLEN (y, i))
1109 return 0;
1111 /* And the corresponding elements must match. */
1112 for (j = 0; j < XVECLEN (x, i); j++)
1113 if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
1114 XVECEXP (y, i, j), memmode, depth))
1115 return 0;
1116 break;
1118 case 'e':
1119 if (i == 1
1120 && targetm.commutative_p (x, UNKNOWN)
1121 && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode,
1122 depth)
1123 && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode,
1124 depth))
1125 return 1;
1126 if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode,
1127 depth))
1128 return 0;
1129 break;
1131 case 'S':
1132 case 's':
1133 if (strcmp (XSTR (x, i), XSTR (y, i)))
1134 return 0;
1135 break;
1137 case 'u':
1138 /* These are just backpointers, so they don't matter. */
1139 break;
1141 case '0':
1142 case 't':
1143 break;
1145 /* It is believed that rtx's at this level will never
1146 contain anything but integers and other rtx's,
1147 except for within LABEL_REFs and SYMBOL_REFs. */
1148 default:
1149 gcc_unreachable ();
1152 return 1;
1155 /* Helper function for cselib_hash_rtx. Arguments like for cselib_hash_rtx,
1156 except that it hashes (plus:P x c). */
1158 static unsigned int
1159 cselib_hash_plus_const_int (rtx x, HOST_WIDE_INT c, int create,
1160 machine_mode memmode)
1162 cselib_val *e = cselib_lookup (x, GET_MODE (x), create, memmode);
1163 if (! e)
1164 return 0;
1166 if (! SP_DERIVED_VALUE_P (e->val_rtx))
1167 for (struct elt_loc_list *l = e->locs; l; l = l->next)
1168 if (GET_CODE (l->loc) == PLUS
1169 && GET_CODE (XEXP (l->loc, 0)) == VALUE
1170 && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
1171 && CONST_INT_P (XEXP (l->loc, 1)))
1173 e = CSELIB_VAL_PTR (XEXP (l->loc, 0));
1174 c = trunc_int_for_mode (c + UINTVAL (XEXP (l->loc, 1)), Pmode);
1175 break;
1177 if (c == 0)
1178 return e->hash;
1180 unsigned hash = (unsigned) PLUS + (unsigned) GET_MODE (x);
1181 hash += e->hash;
1182 unsigned int tem_hash = (unsigned) CONST_INT + (unsigned) VOIDmode;
1183 tem_hash += ((unsigned) CONST_INT << 7) + (unsigned HOST_WIDE_INT) c;
1184 if (tem_hash == 0)
1185 tem_hash = (unsigned int) CONST_INT;
1186 hash += tem_hash;
1187 return hash ? hash : 1 + (unsigned int) PLUS;
1190 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1191 For registers and memory locations, we look up their cselib_val structure
1192 and return its VALUE element.
1193 Possible reasons for return 0 are: the object is volatile, or we couldn't
1194 find a register or memory location in the table and CREATE is zero. If
1195 CREATE is nonzero, table elts are created for regs and mem.
1196 N.B. this hash function returns the same hash value for RTXes that
1197 differ only in the order of operands, thus it is suitable for comparisons
1198 that take commutativity into account.
1199 If we wanted to also support associative rules, we'd have to use a different
1200 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1201 MEMMODE indicates the mode of an enclosing MEM, and it's only
1202 used to compute autoinc values.
1203 We used to have a MODE argument for hashing for CONST_INTs, but that
1204 didn't make sense, since it caused spurious hash differences between
1205 (set (reg:SI 1) (const_int))
1206 (plus:SI (reg:SI 2) (reg:SI 1))
1208 (plus:SI (reg:SI 2) (const_int))
1209 If the mode is important in any context, it must be checked specifically
1210 in a comparison anyway, since relying on hash differences is unsafe. */
1212 static unsigned int
1213 cselib_hash_rtx (rtx x, int create, machine_mode memmode)
1215 cselib_val *e;
1216 poly_int64 offset;
1217 int i, j;
1218 enum rtx_code code;
1219 const char *fmt;
1220 unsigned int hash = 0;
1222 code = GET_CODE (x);
1223 hash += (unsigned) code + (unsigned) GET_MODE (x);
1225 switch (code)
1227 case VALUE:
1228 e = CSELIB_VAL_PTR (x);
1229 return e->hash;
1231 case MEM:
1232 case REG:
1233 e = cselib_lookup (x, GET_MODE (x), create, memmode);
1234 if (! e)
1235 return 0;
1237 return e->hash;
1239 case DEBUG_EXPR:
1240 hash += ((unsigned) DEBUG_EXPR << 7)
1241 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
1242 return hash ? hash : (unsigned int) DEBUG_EXPR;
1244 case DEBUG_IMPLICIT_PTR:
1245 hash += ((unsigned) DEBUG_IMPLICIT_PTR << 7)
1246 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x));
1247 return hash ? hash : (unsigned int) DEBUG_IMPLICIT_PTR;
1249 case DEBUG_PARAMETER_REF:
1250 hash += ((unsigned) DEBUG_PARAMETER_REF << 7)
1251 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x));
1252 return hash ? hash : (unsigned int) DEBUG_PARAMETER_REF;
1254 case ENTRY_VALUE:
1255 /* ENTRY_VALUEs are function invariant, thus try to avoid
1256 recursing on argument if ENTRY_VALUE is one of the
1257 forms emitted by expand_debug_expr, otherwise
1258 ENTRY_VALUE hash would depend on the current value
1259 in some register or memory. */
1260 if (REG_P (ENTRY_VALUE_EXP (x)))
1261 hash += (unsigned int) REG
1262 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x))
1263 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x));
1264 else if (MEM_P (ENTRY_VALUE_EXP (x))
1265 && REG_P (XEXP (ENTRY_VALUE_EXP (x), 0)))
1266 hash += (unsigned int) MEM
1267 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x), 0))
1268 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x), 0));
1269 else
1270 hash += cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode);
1271 return hash ? hash : (unsigned int) ENTRY_VALUE;
1273 case CONST_INT:
1274 hash += ((unsigned) CONST_INT << 7) + UINTVAL (x);
1275 return hash ? hash : (unsigned int) CONST_INT;
1277 case CONST_WIDE_INT:
1278 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
1279 hash += CONST_WIDE_INT_ELT (x, i);
1280 return hash;
1282 case CONST_POLY_INT:
1284 inchash::hash h;
1285 h.add_int (hash);
1286 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1287 h.add_wide_int (CONST_POLY_INT_COEFFS (x)[i]);
1288 return h.end ();
1291 case CONST_DOUBLE:
1292 /* This is like the general case, except that it only counts
1293 the integers representing the constant. */
1294 hash += (unsigned) code + (unsigned) GET_MODE (x);
1295 if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (x) == VOIDmode)
1296 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1297 + (unsigned) CONST_DOUBLE_HIGH (x));
1298 else
1299 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
1300 return hash ? hash : (unsigned int) CONST_DOUBLE;
1302 case CONST_FIXED:
1303 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1304 hash += fixed_hash (CONST_FIXED_VALUE (x));
1305 return hash ? hash : (unsigned int) CONST_FIXED;
1307 case CONST_VECTOR:
1309 int units;
1310 rtx elt;
1312 units = const_vector_encoded_nelts (x);
1314 for (i = 0; i < units; ++i)
1316 elt = CONST_VECTOR_ENCODED_ELT (x, i);
1317 hash += cselib_hash_rtx (elt, 0, memmode);
1320 return hash;
1323 /* Assume there is only one rtx object for any given label. */
1324 case LABEL_REF:
1325 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1326 differences and differences between each stage's debugging dumps. */
1327 hash += (((unsigned int) LABEL_REF << 7)
1328 + CODE_LABEL_NUMBER (label_ref_label (x)));
1329 return hash ? hash : (unsigned int) LABEL_REF;
1331 case SYMBOL_REF:
1333 /* Don't hash on the symbol's address to avoid bootstrap differences.
1334 Different hash values may cause expressions to be recorded in
1335 different orders and thus different registers to be used in the
1336 final assembler. This also avoids differences in the dump files
1337 between various stages. */
1338 unsigned int h = 0;
1339 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1341 while (*p)
1342 h += (h << 7) + *p++; /* ??? revisit */
1344 hash += ((unsigned int) SYMBOL_REF << 7) + h;
1345 return hash ? hash : (unsigned int) SYMBOL_REF;
1348 case PRE_DEC:
1349 case PRE_INC:
1350 /* We can't compute these without knowing the MEM mode. */
1351 gcc_assert (memmode != VOIDmode);
1352 offset = GET_MODE_SIZE (memmode);
1353 if (code == PRE_DEC)
1354 offset = -offset;
1355 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1356 like (mem:MEMMODE (plus (reg) (const_int I))). */
1357 if (GET_MODE (x) == Pmode
1358 && (REG_P (XEXP (x, 0))
1359 || MEM_P (XEXP (x, 0))
1360 || GET_CODE (XEXP (x, 0)) == VALUE))
1362 HOST_WIDE_INT c;
1363 if (offset.is_constant (&c))
1364 return cselib_hash_plus_const_int (XEXP (x, 0),
1365 trunc_int_for_mode (c, Pmode),
1366 create, memmode);
1368 hash = ((unsigned) PLUS + (unsigned) GET_MODE (x)
1369 + cselib_hash_rtx (XEXP (x, 0), create, memmode)
1370 + cselib_hash_rtx (gen_int_mode (offset, GET_MODE (x)),
1371 create, memmode));
1372 return hash ? hash : 1 + (unsigned) PLUS;
1374 case PRE_MODIFY:
1375 gcc_assert (memmode != VOIDmode);
1376 return cselib_hash_rtx (XEXP (x, 1), create, memmode);
1378 case POST_DEC:
1379 case POST_INC:
1380 case POST_MODIFY:
1381 gcc_assert (memmode != VOIDmode);
1382 return cselib_hash_rtx (XEXP (x, 0), create, memmode);
1384 case PC:
1385 case CC0:
1386 case CALL:
1387 case UNSPEC_VOLATILE:
1388 return 0;
1390 case ASM_OPERANDS:
1391 if (MEM_VOLATILE_P (x))
1392 return 0;
1394 break;
1396 case PLUS:
1397 if (GET_MODE (x) == Pmode
1398 && (REG_P (XEXP (x, 0))
1399 || MEM_P (XEXP (x, 0))
1400 || GET_CODE (XEXP (x, 0)) == VALUE)
1401 && CONST_INT_P (XEXP (x, 1)))
1402 return cselib_hash_plus_const_int (XEXP (x, 0), INTVAL (XEXP (x, 1)),
1403 create, memmode);
1404 break;
1406 default:
1407 break;
1410 i = GET_RTX_LENGTH (code) - 1;
1411 fmt = GET_RTX_FORMAT (code);
1412 for (; i >= 0; i--)
1414 switch (fmt[i])
1416 case 'e':
1418 rtx tem = XEXP (x, i);
1419 unsigned int tem_hash = cselib_hash_rtx (tem, create, memmode);
1421 if (tem_hash == 0)
1422 return 0;
1424 hash += tem_hash;
1426 break;
1427 case 'E':
1428 for (j = 0; j < XVECLEN (x, i); j++)
1430 unsigned int tem_hash
1431 = cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
1433 if (tem_hash == 0)
1434 return 0;
1436 hash += tem_hash;
1438 break;
1440 case 's':
1442 const unsigned char *p = (const unsigned char *) XSTR (x, i);
1444 if (p)
1445 while (*p)
1446 hash += *p++;
1447 break;
1450 case 'i':
1451 hash += XINT (x, i);
1452 break;
1454 case 'p':
1455 hash += constant_lower_bound (SUBREG_BYTE (x));
1456 break;
1458 case '0':
1459 case 't':
1460 /* unused */
1461 break;
1463 default:
1464 gcc_unreachable ();
1468 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
1471 /* Create a new value structure for VALUE and initialize it. The mode of the
1472 value is MODE. */
1474 static inline cselib_val *
1475 new_cselib_val (unsigned int hash, machine_mode mode, rtx x)
1477 cselib_val *e = cselib_val_pool.allocate ();
1479 gcc_assert (hash);
1480 gcc_assert (next_uid);
1482 e->hash = hash;
1483 e->uid = next_uid++;
1484 /* We use an alloc pool to allocate this RTL construct because it
1485 accounts for about 8% of the overall memory usage. We know
1486 precisely when we can have VALUE RTXen (when cselib is active)
1487 so we don't need to put them in garbage collected memory.
1488 ??? Why should a VALUE be an RTX in the first place? */
1489 e->val_rtx = (rtx_def*) value_pool.allocate ();
1490 memset (e->val_rtx, 0, RTX_HDR_SIZE);
1491 PUT_CODE (e->val_rtx, VALUE);
1492 PUT_MODE (e->val_rtx, mode);
1493 CSELIB_VAL_PTR (e->val_rtx) = e;
1494 e->addr_list = 0;
1495 e->locs = 0;
1496 e->next_containing_mem = 0;
1498 if (dump_file && (dump_flags & TDF_CSELIB))
1500 fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
1501 if (flag_dump_noaddr || flag_dump_unnumbered)
1502 fputs ("# ", dump_file);
1503 else
1504 fprintf (dump_file, "%p ", (void*)e);
1505 print_rtl_single (dump_file, x);
1506 fputc ('\n', dump_file);
1509 return e;
1512 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1513 contains the data at this address. X is a MEM that represents the
1514 value. Update the two value structures to represent this situation. */
1516 static void
1517 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
1519 addr_elt = canonical_cselib_val (addr_elt);
1520 mem_elt = canonical_cselib_val (mem_elt);
1522 /* Avoid duplicates. */
1523 addr_space_t as = MEM_ADDR_SPACE (x);
1524 for (elt_loc_list *l = mem_elt->locs; l; l = l->next)
1525 if (MEM_P (l->loc)
1526 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt
1527 && MEM_ADDR_SPACE (l->loc) == as)
1529 promote_debug_loc (l);
1530 return;
1533 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
1534 new_elt_loc_list (mem_elt,
1535 replace_equiv_address_nv (x, addr_elt->val_rtx));
1536 if (mem_elt->next_containing_mem == NULL)
1538 mem_elt->next_containing_mem = first_containing_mem;
1539 first_containing_mem = mem_elt;
1543 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1544 If CREATE, make a new one if we haven't seen it before. */
1546 static cselib_val *
1547 cselib_lookup_mem (rtx x, int create)
1549 machine_mode mode = GET_MODE (x);
1550 machine_mode addr_mode;
1551 cselib_val **slot;
1552 cselib_val *addr;
1553 cselib_val *mem_elt;
1555 if (MEM_VOLATILE_P (x) || mode == BLKmode
1556 || !cselib_record_memory
1557 || (FLOAT_MODE_P (mode) && flag_float_store))
1558 return 0;
1560 addr_mode = GET_MODE (XEXP (x, 0));
1561 if (addr_mode == VOIDmode)
1562 addr_mode = Pmode;
1564 /* Look up the value for the address. */
1565 addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
1566 if (! addr)
1567 return 0;
1568 addr = canonical_cselib_val (addr);
1570 /* Find a value that describes a value of our mode at that address. */
1571 addr_space_t as = MEM_ADDR_SPACE (x);
1572 for (elt_list *l = addr->addr_list; l; l = l->next)
1573 if (GET_MODE (l->elt->val_rtx) == mode)
1575 for (elt_loc_list *l2 = l->elt->locs; l2; l2 = l2->next)
1576 if (MEM_P (l2->loc) && MEM_ADDR_SPACE (l2->loc) == as)
1578 promote_debug_loc (l->elt->locs);
1579 return l->elt;
1583 if (! create)
1584 return 0;
1586 mem_elt = new_cselib_val (next_uid, mode, x);
1587 add_mem_for_addr (addr, mem_elt, x);
1588 slot = cselib_find_slot (mode, x, mem_elt->hash, INSERT, VOIDmode);
1589 *slot = mem_elt;
1590 return mem_elt;
1593 /* Search through the possible substitutions in P. We prefer a non reg
1594 substitution because this allows us to expand the tree further. If
1595 we find, just a reg, take the lowest regno. There may be several
1596 non-reg results, we just take the first one because they will all
1597 expand to the same place. */
1599 static rtx
1600 expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1601 int max_depth)
1603 rtx reg_result = NULL;
1604 unsigned int regno = UINT_MAX;
1605 struct elt_loc_list *p_in = p;
1607 for (; p; p = p->next)
1609 /* Return these right away to avoid returning stack pointer based
1610 expressions for frame pointer and vice versa, which is something
1611 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1612 for more details. */
1613 if (REG_P (p->loc)
1614 && (REGNO (p->loc) == STACK_POINTER_REGNUM
1615 || REGNO (p->loc) == FRAME_POINTER_REGNUM
1616 || REGNO (p->loc) == HARD_FRAME_POINTER_REGNUM
1617 || REGNO (p->loc) == cfa_base_preserved_regno))
1618 return p->loc;
1619 /* Avoid infinite recursion trying to expand a reg into a
1620 the same reg. */
1621 if ((REG_P (p->loc))
1622 && (REGNO (p->loc) < regno)
1623 && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
1625 reg_result = p->loc;
1626 regno = REGNO (p->loc);
1628 /* Avoid infinite recursion and do not try to expand the
1629 value. */
1630 else if (GET_CODE (p->loc) == VALUE
1631 && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1632 continue;
1633 else if (!REG_P (p->loc))
1635 rtx result, note;
1636 if (dump_file && (dump_flags & TDF_CSELIB))
1638 print_inline_rtx (dump_file, p->loc, 0);
1639 fprintf (dump_file, "\n");
1641 if (GET_CODE (p->loc) == LO_SUM
1642 && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1643 && p->setting_insn
1644 && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1645 && XEXP (note, 0) == XEXP (p->loc, 1))
1646 return XEXP (p->loc, 1);
1647 result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
1648 if (result)
1649 return result;
1654 if (regno != UINT_MAX)
1656 rtx result;
1657 if (dump_file && (dump_flags & TDF_CSELIB))
1658 fprintf (dump_file, "r%d\n", regno);
1660 result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
1661 if (result)
1662 return result;
1665 if (dump_file && (dump_flags & TDF_CSELIB))
1667 if (reg_result)
1669 print_inline_rtx (dump_file, reg_result, 0);
1670 fprintf (dump_file, "\n");
1672 else
1673 fprintf (dump_file, "NULL\n");
1675 return reg_result;
1679 /* Forward substitute and expand an expression out to its roots.
1680 This is the opposite of common subexpression. Because local value
1681 numbering is such a weak optimization, the expanded expression is
1682 pretty much unique (not from a pointer equals point of view but
1683 from a tree shape point of view.
1685 This function returns NULL if the expansion fails. The expansion
1686 will fail if there is no value number for one of the operands or if
1687 one of the operands has been overwritten between the current insn
1688 and the beginning of the basic block. For instance x has no
1689 expansion in:
1691 r1 <- r1 + 3
1692 x <- r1 + 8
1694 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1695 It is clear on return. */
1698 cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
1700 struct expand_value_data evd;
1702 evd.regs_active = regs_active;
1703 evd.callback = NULL;
1704 evd.callback_arg = NULL;
1705 evd.dummy = false;
1707 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1710 /* Same as cselib_expand_value_rtx, but using a callback to try to
1711 resolve some expressions. The CB function should return ORIG if it
1712 can't or does not want to deal with a certain RTX. Any other
1713 return value, including NULL, will be used as the expansion for
1714 VALUE, without any further changes. */
1717 cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1718 cselib_expand_callback cb, void *data)
1720 struct expand_value_data evd;
1722 evd.regs_active = regs_active;
1723 evd.callback = cb;
1724 evd.callback_arg = data;
1725 evd.dummy = false;
1727 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1730 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1731 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1732 would return NULL or non-NULL, without allocating new rtx. */
1734 bool
1735 cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1736 cselib_expand_callback cb, void *data)
1738 struct expand_value_data evd;
1740 evd.regs_active = regs_active;
1741 evd.callback = cb;
1742 evd.callback_arg = data;
1743 evd.dummy = true;
1745 return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1748 /* Internal implementation of cselib_expand_value_rtx and
1749 cselib_expand_value_rtx_cb. */
1751 static rtx
1752 cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1753 int max_depth)
1755 rtx copy, scopy;
1756 int i, j;
1757 RTX_CODE code;
1758 const char *format_ptr;
1759 machine_mode mode;
1761 code = GET_CODE (orig);
1763 /* For the context of dse, if we end up expand into a huge tree, we
1764 will not have a useful address, so we might as well just give up
1765 quickly. */
1766 if (max_depth <= 0)
1767 return NULL;
1769 switch (code)
1771 case REG:
1773 struct elt_list *l = REG_VALUES (REGNO (orig));
1775 if (l && l->elt == NULL)
1776 l = l->next;
1777 for (; l; l = l->next)
1778 if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1780 rtx result;
1781 unsigned regno = REGNO (orig);
1783 /* The only thing that we are not willing to do (this
1784 is requirement of dse and if others potential uses
1785 need this function we should add a parm to control
1786 it) is that we will not substitute the
1787 STACK_POINTER_REGNUM, FRAME_POINTER or the
1788 HARD_FRAME_POINTER.
1790 These expansions confuses the code that notices that
1791 stores into the frame go dead at the end of the
1792 function and that the frame is not effected by calls
1793 to subroutines. If you allow the
1794 STACK_POINTER_REGNUM substitution, then dse will
1795 think that parameter pushing also goes dead which is
1796 wrong. If you allow the FRAME_POINTER or the
1797 HARD_FRAME_POINTER then you lose the opportunity to
1798 make the frame assumptions. */
1799 if (regno == STACK_POINTER_REGNUM
1800 || regno == FRAME_POINTER_REGNUM
1801 || regno == HARD_FRAME_POINTER_REGNUM
1802 || regno == cfa_base_preserved_regno)
1803 return orig;
1805 bitmap_set_bit (evd->regs_active, regno);
1807 if (dump_file && (dump_flags & TDF_CSELIB))
1808 fprintf (dump_file, "expanding: r%d into: ", regno);
1810 result = expand_loc (l->elt->locs, evd, max_depth);
1811 bitmap_clear_bit (evd->regs_active, regno);
1813 if (result)
1814 return result;
1815 else
1816 return orig;
1818 return orig;
1821 CASE_CONST_ANY:
1822 case SYMBOL_REF:
1823 case CODE_LABEL:
1824 case PC:
1825 case CC0:
1826 case SCRATCH:
1827 /* SCRATCH must be shared because they represent distinct values. */
1828 return orig;
1829 case CLOBBER:
1830 if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1831 return orig;
1832 break;
1834 case CONST:
1835 if (shared_const_p (orig))
1836 return orig;
1837 break;
1839 case SUBREG:
1841 rtx subreg;
1843 if (evd->callback)
1845 subreg = evd->callback (orig, evd->regs_active, max_depth,
1846 evd->callback_arg);
1847 if (subreg != orig)
1848 return subreg;
1851 subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1852 max_depth - 1);
1853 if (!subreg)
1854 return NULL;
1855 scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1856 GET_MODE (SUBREG_REG (orig)),
1857 SUBREG_BYTE (orig));
1858 if (scopy == NULL
1859 || (GET_CODE (scopy) == SUBREG
1860 && !REG_P (SUBREG_REG (scopy))
1861 && !MEM_P (SUBREG_REG (scopy))))
1862 return NULL;
1864 return scopy;
1867 case VALUE:
1869 rtx result;
1871 if (dump_file && (dump_flags & TDF_CSELIB))
1873 fputs ("\nexpanding ", dump_file);
1874 print_rtl_single (dump_file, orig);
1875 fputs (" into...", dump_file);
1878 if (evd->callback)
1880 result = evd->callback (orig, evd->regs_active, max_depth,
1881 evd->callback_arg);
1883 if (result != orig)
1884 return result;
1887 result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
1888 return result;
1891 case DEBUG_EXPR:
1892 if (evd->callback)
1893 return evd->callback (orig, evd->regs_active, max_depth,
1894 evd->callback_arg);
1895 return orig;
1897 default:
1898 break;
1901 /* Copy the various flags, fields, and other information. We assume
1902 that all fields need copying, and then clear the fields that should
1903 not be copied. That is the sensible default behavior, and forces
1904 us to explicitly document why we are *not* copying a flag. */
1905 if (evd->dummy)
1906 copy = NULL;
1907 else
1908 copy = shallow_copy_rtx (orig);
1910 format_ptr = GET_RTX_FORMAT (code);
1912 for (i = 0; i < GET_RTX_LENGTH (code); i++)
1913 switch (*format_ptr++)
1915 case 'e':
1916 if (XEXP (orig, i) != NULL)
1918 rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1919 max_depth - 1);
1920 if (!result)
1921 return NULL;
1922 if (copy)
1923 XEXP (copy, i) = result;
1925 break;
1927 case 'E':
1928 case 'V':
1929 if (XVEC (orig, i) != NULL)
1931 if (copy)
1932 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1933 for (j = 0; j < XVECLEN (orig, i); j++)
1935 rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1936 evd, max_depth - 1);
1937 if (!result)
1938 return NULL;
1939 if (copy)
1940 XVECEXP (copy, i, j) = result;
1943 break;
1945 case 't':
1946 case 'w':
1947 case 'i':
1948 case 's':
1949 case 'S':
1950 case 'T':
1951 case 'u':
1952 case 'B':
1953 case '0':
1954 /* These are left unchanged. */
1955 break;
1957 default:
1958 gcc_unreachable ();
1961 if (evd->dummy)
1962 return orig;
1964 mode = GET_MODE (copy);
1965 /* If an operand has been simplified into CONST_INT, which doesn't
1966 have a mode and the mode isn't derivable from whole rtx's mode,
1967 try simplify_*_operation first with mode from original's operand
1968 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1969 scopy = copy;
1970 switch (GET_RTX_CLASS (code))
1972 case RTX_UNARY:
1973 if (CONST_INT_P (XEXP (copy, 0))
1974 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1976 scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1977 GET_MODE (XEXP (orig, 0)));
1978 if (scopy)
1979 return scopy;
1981 break;
1982 case RTX_COMM_ARITH:
1983 case RTX_BIN_ARITH:
1984 /* These expressions can derive operand modes from the whole rtx's mode. */
1985 break;
1986 case RTX_TERNARY:
1987 case RTX_BITFIELD_OPS:
1988 if (CONST_INT_P (XEXP (copy, 0))
1989 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1991 scopy = simplify_ternary_operation (code, mode,
1992 GET_MODE (XEXP (orig, 0)),
1993 XEXP (copy, 0), XEXP (copy, 1),
1994 XEXP (copy, 2));
1995 if (scopy)
1996 return scopy;
1998 break;
1999 case RTX_COMPARE:
2000 case RTX_COMM_COMPARE:
2001 if (CONST_INT_P (XEXP (copy, 0))
2002 && GET_MODE (XEXP (copy, 1)) == VOIDmode
2003 && (GET_MODE (XEXP (orig, 0)) != VOIDmode
2004 || GET_MODE (XEXP (orig, 1)) != VOIDmode))
2006 scopy = simplify_relational_operation (code, mode,
2007 (GET_MODE (XEXP (orig, 0))
2008 != VOIDmode)
2009 ? GET_MODE (XEXP (orig, 0))
2010 : GET_MODE (XEXP (orig, 1)),
2011 XEXP (copy, 0),
2012 XEXP (copy, 1));
2013 if (scopy)
2014 return scopy;
2016 break;
2017 default:
2018 break;
2020 scopy = simplify_rtx (copy);
2021 if (scopy)
2022 return scopy;
2023 return copy;
2026 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
2027 with VALUE expressions. This way, it becomes independent of changes
2028 to registers and memory.
2029 X isn't actually modified; if modifications are needed, new rtl is
2030 allocated. However, the return value can share rtl with X.
2031 If X is within a MEM, MEMMODE must be the mode of the MEM. */
2034 cselib_subst_to_values (rtx x, machine_mode memmode)
2036 enum rtx_code code = GET_CODE (x);
2037 const char *fmt = GET_RTX_FORMAT (code);
2038 cselib_val *e;
2039 struct elt_list *l;
2040 rtx copy = x;
2041 int i;
2042 poly_int64 offset;
2044 switch (code)
2046 case REG:
2047 l = REG_VALUES (REGNO (x));
2048 if (l && l->elt == NULL)
2049 l = l->next;
2050 for (; l; l = l->next)
2051 if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
2052 return l->elt->val_rtx;
2054 gcc_unreachable ();
2056 case MEM:
2057 e = cselib_lookup_mem (x, 0);
2058 /* This used to happen for autoincrements, but we deal with them
2059 properly now. Remove the if stmt for the next release. */
2060 if (! e)
2062 /* Assign a value that doesn't match any other. */
2063 e = new_cselib_val (next_uid, GET_MODE (x), x);
2065 return e->val_rtx;
2067 case ENTRY_VALUE:
2068 e = cselib_lookup (x, GET_MODE (x), 0, memmode);
2069 if (! e)
2070 break;
2071 return e->val_rtx;
2073 CASE_CONST_ANY:
2074 return x;
2076 case PRE_DEC:
2077 case PRE_INC:
2078 gcc_assert (memmode != VOIDmode);
2079 offset = GET_MODE_SIZE (memmode);
2080 if (code == PRE_DEC)
2081 offset = -offset;
2082 return cselib_subst_to_values (plus_constant (GET_MODE (x),
2083 XEXP (x, 0), offset),
2084 memmode);
2086 case PRE_MODIFY:
2087 gcc_assert (memmode != VOIDmode);
2088 return cselib_subst_to_values (XEXP (x, 1), memmode);
2090 case POST_DEC:
2091 case POST_INC:
2092 case POST_MODIFY:
2093 gcc_assert (memmode != VOIDmode);
2094 return cselib_subst_to_values (XEXP (x, 0), memmode);
2096 case PLUS:
2097 if (GET_MODE (x) == Pmode && CONST_INT_P (XEXP (x, 1)))
2099 rtx t = cselib_subst_to_values (XEXP (x, 0), memmode);
2100 if (GET_CODE (t) == VALUE)
2102 if (SP_DERIVED_VALUE_P (t) && XEXP (x, 1) == const0_rtx)
2103 return t;
2104 for (struct elt_loc_list *l = CSELIB_VAL_PTR (t)->locs;
2105 l; l = l->next)
2106 if (GET_CODE (l->loc) == PLUS
2107 && GET_CODE (XEXP (l->loc, 0)) == VALUE
2108 && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
2109 && CONST_INT_P (XEXP (l->loc, 1)))
2110 return plus_constant (Pmode, l->loc, INTVAL (XEXP (x, 1)));
2112 if (t != XEXP (x, 0))
2114 copy = shallow_copy_rtx (x);
2115 XEXP (copy, 0) = t;
2117 return copy;
2120 default:
2121 break;
2124 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2126 if (fmt[i] == 'e')
2128 rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
2130 if (t != XEXP (x, i))
2132 if (x == copy)
2133 copy = shallow_copy_rtx (x);
2134 XEXP (copy, i) = t;
2137 else if (fmt[i] == 'E')
2139 int j;
2141 for (j = 0; j < XVECLEN (x, i); j++)
2143 rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
2145 if (t != XVECEXP (x, i, j))
2147 if (XVEC (x, i) == XVEC (copy, i))
2149 if (x == copy)
2150 copy = shallow_copy_rtx (x);
2151 XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
2153 XVECEXP (copy, i, j) = t;
2159 return copy;
2162 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
2165 cselib_subst_to_values_from_insn (rtx x, machine_mode memmode, rtx_insn *insn)
2167 rtx ret;
2168 gcc_assert (!cselib_current_insn);
2169 cselib_current_insn = insn;
2170 ret = cselib_subst_to_values (x, memmode);
2171 cselib_current_insn = NULL;
2172 return ret;
2175 /* Look up the rtl expression X in our tables and return the value it
2176 has. If CREATE is zero, we return NULL if we don't know the value.
2177 Otherwise, we create a new one if possible, using mode MODE if X
2178 doesn't have a mode (i.e. because it's a constant). When X is part
2179 of an address, MEMMODE should be the mode of the enclosing MEM if
2180 we're tracking autoinc expressions. */
2182 static cselib_val *
2183 cselib_lookup_1 (rtx x, machine_mode mode,
2184 int create, machine_mode memmode)
2186 cselib_val **slot;
2187 cselib_val *e;
2188 unsigned int hashval;
2190 if (GET_MODE (x) != VOIDmode)
2191 mode = GET_MODE (x);
2193 if (GET_CODE (x) == VALUE)
2194 return CSELIB_VAL_PTR (x);
2196 if (REG_P (x))
2198 struct elt_list *l;
2199 unsigned int i = REGNO (x);
2201 l = REG_VALUES (i);
2202 if (l && l->elt == NULL)
2203 l = l->next;
2204 for (; l; l = l->next)
2205 if (mode == GET_MODE (l->elt->val_rtx))
2207 promote_debug_loc (l->elt->locs);
2208 return l->elt;
2211 if (! create)
2212 return 0;
2214 if (i < FIRST_PSEUDO_REGISTER)
2216 unsigned int n = hard_regno_nregs (i, mode);
2218 if (n > max_value_regs)
2219 max_value_regs = n;
2222 e = new_cselib_val (next_uid, GET_MODE (x), x);
2223 if (GET_MODE (x) == Pmode && x == stack_pointer_rtx)
2224 SP_DERIVED_VALUE_P (e->val_rtx) = 1;
2225 new_elt_loc_list (e, x);
2227 scalar_int_mode int_mode;
2228 if (REG_VALUES (i) == 0)
2230 /* Maintain the invariant that the first entry of
2231 REG_VALUES, if present, must be the value used to set the
2232 register, or NULL. */
2233 used_regs[n_used_regs++] = i;
2234 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
2236 else if (cselib_preserve_constants
2237 && is_int_mode (mode, &int_mode))
2239 /* During var-tracking, try harder to find equivalences
2240 for SUBREGs. If a setter sets say a DImode register
2241 and user uses that register only in SImode, add a lowpart
2242 subreg location. */
2243 struct elt_list *lwider = NULL;
2244 scalar_int_mode lmode;
2245 l = REG_VALUES (i);
2246 if (l && l->elt == NULL)
2247 l = l->next;
2248 for (; l; l = l->next)
2249 if (is_int_mode (GET_MODE (l->elt->val_rtx), &lmode)
2250 && GET_MODE_SIZE (lmode) > GET_MODE_SIZE (int_mode)
2251 && (lwider == NULL
2252 || partial_subreg_p (lmode,
2253 GET_MODE (lwider->elt->val_rtx))))
2255 struct elt_loc_list *el;
2256 if (i < FIRST_PSEUDO_REGISTER
2257 && hard_regno_nregs (i, lmode) != 1)
2258 continue;
2259 for (el = l->elt->locs; el; el = el->next)
2260 if (!REG_P (el->loc))
2261 break;
2262 if (el)
2263 lwider = l;
2265 if (lwider)
2267 rtx sub = lowpart_subreg (int_mode, lwider->elt->val_rtx,
2268 GET_MODE (lwider->elt->val_rtx));
2269 if (sub)
2270 new_elt_loc_list (e, sub);
2273 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
2274 slot = cselib_find_slot (mode, x, e->hash, INSERT, memmode);
2275 *slot = e;
2276 return e;
2279 if (MEM_P (x))
2280 return cselib_lookup_mem (x, create);
2282 hashval = cselib_hash_rtx (x, create, memmode);
2283 /* Can't even create if hashing is not possible. */
2284 if (! hashval)
2285 return 0;
2287 slot = cselib_find_slot (mode, x, hashval,
2288 create ? INSERT : NO_INSERT, memmode);
2289 if (slot == 0)
2290 return 0;
2292 e = (cselib_val *) *slot;
2293 if (e)
2294 return e;
2296 e = new_cselib_val (hashval, mode, x);
2298 /* We have to fill the slot before calling cselib_subst_to_values:
2299 the hash table is inconsistent until we do so, and
2300 cselib_subst_to_values will need to do lookups. */
2301 *slot = e;
2302 rtx v = cselib_subst_to_values (x, memmode);
2304 /* If cselib_preserve_constants, we might get a SP_DERIVED_VALUE_P
2305 VALUE that isn't in the hash tables anymore. */
2306 if (GET_CODE (v) == VALUE && SP_DERIVED_VALUE_P (v) && PRESERVED_VALUE_P (v))
2307 PRESERVED_VALUE_P (e->val_rtx) = 1;
2309 new_elt_loc_list (e, v);
2310 return e;
2313 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2315 cselib_val *
2316 cselib_lookup_from_insn (rtx x, machine_mode mode,
2317 int create, machine_mode memmode, rtx_insn *insn)
2319 cselib_val *ret;
2321 gcc_assert (!cselib_current_insn);
2322 cselib_current_insn = insn;
2324 ret = cselib_lookup (x, mode, create, memmode);
2326 cselib_current_insn = NULL;
2328 return ret;
2331 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2332 maintains invariants related with debug insns. */
2334 cselib_val *
2335 cselib_lookup (rtx x, machine_mode mode,
2336 int create, machine_mode memmode)
2338 cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
2340 /* ??? Should we return NULL if we're not to create an entry, the
2341 found loc is a debug loc and cselib_current_insn is not DEBUG?
2342 If so, we should also avoid converting val to non-DEBUG; probably
2343 easiest setting cselib_current_insn to NULL before the call
2344 above. */
2346 if (dump_file && (dump_flags & TDF_CSELIB))
2348 fputs ("cselib lookup ", dump_file);
2349 print_inline_rtx (dump_file, x, 2);
2350 fprintf (dump_file, " => %u:%u\n",
2351 ret ? ret->uid : 0,
2352 ret ? ret->hash : 0);
2355 return ret;
2358 /* Invalidate the value at *L, which is part of REG_VALUES (REGNO). */
2360 static void
2361 cselib_invalidate_regno_val (unsigned int regno, struct elt_list **l)
2363 cselib_val *v = (*l)->elt;
2364 if (*l == REG_VALUES (regno))
2366 /* Maintain the invariant that the first entry of
2367 REG_VALUES, if present, must be the value used to set
2368 the register, or NULL. This is also nice because
2369 then we won't push the same regno onto user_regs
2370 multiple times. */
2371 (*l)->elt = NULL;
2372 l = &(*l)->next;
2374 else
2375 unchain_one_elt_list (l);
2377 v = canonical_cselib_val (v);
2379 bool had_locs = v->locs != NULL;
2380 rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
2382 /* Now, we clear the mapping from value to reg. It must exist, so
2383 this code will crash intentionally if it doesn't. */
2384 for (elt_loc_list **p = &v->locs; ; p = &(*p)->next)
2386 rtx x = (*p)->loc;
2388 if (REG_P (x) && REGNO (x) == regno)
2390 unchain_one_elt_loc_list (p);
2391 break;
2395 if (had_locs && cselib_useless_value_p (v))
2397 if (setting_insn && DEBUG_INSN_P (setting_insn))
2398 n_useless_debug_values++;
2399 else
2400 n_useless_values++;
2404 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2405 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2406 is used to determine how many hard registers are being changed. If MODE
2407 is VOIDmode, then only REGNO is being changed; this is used when
2408 invalidating call clobbered registers across a call. */
2410 static void
2411 cselib_invalidate_regno (unsigned int regno, machine_mode mode)
2413 unsigned int endregno;
2414 unsigned int i;
2416 /* If we see pseudos after reload, something is _wrong_. */
2417 gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
2418 || reg_renumber[regno] < 0);
2420 /* Determine the range of registers that must be invalidated. For
2421 pseudos, only REGNO is affected. For hard regs, we must take MODE
2422 into account, and we must also invalidate lower register numbers
2423 if they contain values that overlap REGNO. */
2424 if (regno < FIRST_PSEUDO_REGISTER)
2426 gcc_assert (mode != VOIDmode);
2428 if (regno < max_value_regs)
2429 i = 0;
2430 else
2431 i = regno - max_value_regs;
2433 endregno = end_hard_regno (mode, regno);
2435 else
2437 i = regno;
2438 endregno = regno + 1;
2441 for (; i < endregno; i++)
2443 struct elt_list **l = &REG_VALUES (i);
2445 /* Go through all known values for this reg; if it overlaps the range
2446 we're invalidating, remove the value. */
2447 while (*l)
2449 cselib_val *v = (*l)->elt;
2450 unsigned int this_last = i;
2452 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
2453 this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
2455 if (this_last < regno || v == NULL
2456 || (v == cfa_base_preserved_val
2457 && i == cfa_base_preserved_regno))
2459 l = &(*l)->next;
2460 continue;
2463 /* We have an overlap. */
2464 cselib_invalidate_regno_val (i, l);
2469 /* Invalidate any locations in the table which are changed because of a
2470 store to MEM_RTX. If this is called because of a non-const call
2471 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2473 static void
2474 cselib_invalidate_mem (rtx mem_rtx)
2476 cselib_val **vp, *v, *next;
2477 int num_mems = 0;
2478 rtx mem_addr;
2480 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2481 mem_rtx = canon_rtx (mem_rtx);
2483 vp = &first_containing_mem;
2484 for (v = *vp; v != &dummy_val; v = next)
2486 bool has_mem = false;
2487 struct elt_loc_list **p = &v->locs;
2488 bool had_locs = v->locs != NULL;
2489 rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
2491 while (*p)
2493 rtx x = (*p)->loc;
2494 cselib_val *addr;
2495 struct elt_list **mem_chain;
2497 /* MEMs may occur in locations only at the top level; below
2498 that every MEM or REG is substituted by its VALUE. */
2499 if (!MEM_P (x))
2501 p = &(*p)->next;
2502 continue;
2504 if (num_mems < param_max_cselib_memory_locations
2505 && ! canon_anti_dependence (x, false, mem_rtx,
2506 GET_MODE (mem_rtx), mem_addr))
2508 has_mem = true;
2509 num_mems++;
2510 p = &(*p)->next;
2511 continue;
2514 /* This one overlaps. */
2515 /* We must have a mapping from this MEM's address to the
2516 value (E). Remove that, too. */
2517 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
2518 addr = canonical_cselib_val (addr);
2519 gcc_checking_assert (v == canonical_cselib_val (v));
2520 mem_chain = &addr->addr_list;
2521 for (;;)
2523 cselib_val *canon = canonical_cselib_val ((*mem_chain)->elt);
2525 if (canon == v)
2527 unchain_one_elt_list (mem_chain);
2528 break;
2531 /* Record canonicalized elt. */
2532 (*mem_chain)->elt = canon;
2534 mem_chain = &(*mem_chain)->next;
2537 unchain_one_elt_loc_list (p);
2540 if (had_locs && cselib_useless_value_p (v))
2542 if (setting_insn && DEBUG_INSN_P (setting_insn))
2543 n_useless_debug_values++;
2544 else
2545 n_useless_values++;
2548 next = v->next_containing_mem;
2549 if (has_mem)
2551 *vp = v;
2552 vp = &(*vp)->next_containing_mem;
2554 else
2555 v->next_containing_mem = NULL;
2557 *vp = &dummy_val;
2560 /* Invalidate DEST. */
2562 void
2563 cselib_invalidate_rtx (rtx dest)
2565 while (GET_CODE (dest) == SUBREG
2566 || GET_CODE (dest) == ZERO_EXTRACT
2567 || GET_CODE (dest) == STRICT_LOW_PART)
2568 dest = XEXP (dest, 0);
2570 if (REG_P (dest))
2571 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
2572 else if (MEM_P (dest))
2573 cselib_invalidate_mem (dest);
2576 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2578 static void
2579 cselib_invalidate_rtx_note_stores (rtx dest, const_rtx,
2580 void *data ATTRIBUTE_UNUSED)
2582 cselib_invalidate_rtx (dest);
2585 /* Record the result of a SET instruction. DEST is being set; the source
2586 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2587 describes its address. */
2589 static void
2590 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
2592 if (src_elt == 0 || side_effects_p (dest))
2593 return;
2595 if (REG_P (dest))
2597 unsigned int dreg = REGNO (dest);
2598 if (dreg < FIRST_PSEUDO_REGISTER)
2600 unsigned int n = REG_NREGS (dest);
2602 if (n > max_value_regs)
2603 max_value_regs = n;
2606 if (REG_VALUES (dreg) == 0)
2608 used_regs[n_used_regs++] = dreg;
2609 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2611 else
2613 /* The register should have been invalidated. */
2614 gcc_assert (REG_VALUES (dreg)->elt == 0);
2615 REG_VALUES (dreg)->elt = src_elt;
2618 if (cselib_useless_value_p (src_elt))
2619 n_useless_values--;
2620 new_elt_loc_list (src_elt, dest);
2622 else if (MEM_P (dest) && dest_addr_elt != 0
2623 && cselib_record_memory)
2625 if (cselib_useless_value_p (src_elt))
2626 n_useless_values--;
2627 add_mem_for_addr (dest_addr_elt, src_elt, dest);
2631 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2633 void
2634 cselib_add_permanent_equiv (cselib_val *elt, rtx x, rtx_insn *insn)
2636 cselib_val *nelt;
2637 rtx_insn *save_cselib_current_insn = cselib_current_insn;
2639 gcc_checking_assert (elt);
2640 gcc_checking_assert (PRESERVED_VALUE_P (elt->val_rtx));
2641 gcc_checking_assert (!side_effects_p (x));
2643 cselib_current_insn = insn;
2645 nelt = cselib_lookup (x, GET_MODE (elt->val_rtx), 1, VOIDmode);
2647 if (nelt != elt)
2649 cselib_any_perm_equivs = true;
2651 if (!PRESERVED_VALUE_P (nelt->val_rtx))
2652 cselib_preserve_value (nelt);
2654 new_elt_loc_list (nelt, elt->val_rtx);
2657 cselib_current_insn = save_cselib_current_insn;
2660 /* Return TRUE if any permanent equivalences have been recorded since
2661 the table was last initialized. */
2662 bool
2663 cselib_have_permanent_equivalences (void)
2665 return cselib_any_perm_equivs;
2668 /* Record stack_pointer_rtx to be equal to
2669 (plus:P cfa_base_preserved_val offset). Used by var-tracking
2670 at the start of basic blocks for !frame_pointer_needed functions. */
2672 void
2673 cselib_record_sp_cfa_base_equiv (HOST_WIDE_INT offset, rtx_insn *insn)
2675 rtx sp_derived_value = NULL_RTX;
2676 for (struct elt_loc_list *l = cfa_base_preserved_val->locs; l; l = l->next)
2677 if (GET_CODE (l->loc) == VALUE
2678 && SP_DERIVED_VALUE_P (l->loc))
2680 sp_derived_value = l->loc;
2681 break;
2683 else if (GET_CODE (l->loc) == PLUS
2684 && GET_CODE (XEXP (l->loc, 0)) == VALUE
2685 && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
2686 && CONST_INT_P (XEXP (l->loc, 1)))
2688 sp_derived_value = XEXP (l->loc, 0);
2689 offset = offset + UINTVAL (XEXP (l->loc, 1));
2690 break;
2692 if (sp_derived_value == NULL_RTX)
2693 return;
2694 cselib_val *val
2695 = cselib_lookup_from_insn (plus_constant (Pmode, sp_derived_value, offset),
2696 Pmode, 1, VOIDmode, insn);
2697 if (val != NULL)
2699 PRESERVED_VALUE_P (val->val_rtx) = 1;
2700 cselib_record_set (stack_pointer_rtx, val, NULL);
2704 /* Return true if V is SP_DERIVED_VALUE_P (or SP_DERIVED_VALUE_P + CONST_INT)
2705 that can be expressed using cfa_base_preserved_val + CONST_INT. */
2707 bool
2708 cselib_sp_derived_value_p (cselib_val *v)
2710 if (!SP_DERIVED_VALUE_P (v->val_rtx))
2711 for (struct elt_loc_list *l = v->locs; l; l = l->next)
2712 if (GET_CODE (l->loc) == PLUS
2713 && GET_CODE (XEXP (l->loc, 0)) == VALUE
2714 && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
2715 && CONST_INT_P (XEXP (l->loc, 1)))
2716 v = CSELIB_VAL_PTR (XEXP (l->loc, 0));
2717 if (!SP_DERIVED_VALUE_P (v->val_rtx))
2718 return false;
2719 for (struct elt_loc_list *l = v->locs; l; l = l->next)
2720 if (l->loc == cfa_base_preserved_val->val_rtx)
2721 return true;
2722 else if (GET_CODE (l->loc) == PLUS
2723 && XEXP (l->loc, 0) == cfa_base_preserved_val->val_rtx
2724 && CONST_INT_P (XEXP (l->loc, 1)))
2725 return true;
2726 return false;
2729 /* There is no good way to determine how many elements there can be
2730 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2731 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2733 struct cselib_record_autoinc_data
2735 struct cselib_set *sets;
2736 int n_sets;
2739 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2740 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2742 static int
2743 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
2744 rtx dest, rtx src, rtx srcoff, void *arg)
2746 struct cselib_record_autoinc_data *data;
2747 data = (struct cselib_record_autoinc_data *)arg;
2749 data->sets[data->n_sets].dest = dest;
2751 if (srcoff)
2752 data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
2753 else
2754 data->sets[data->n_sets].src = src;
2756 data->n_sets++;
2758 return 0;
2761 /* Record the effects of any sets and autoincs in INSN. */
2762 static void
2763 cselib_record_sets (rtx_insn *insn)
2765 int n_sets = 0;
2766 int i;
2767 struct cselib_set sets[MAX_SETS];
2768 rtx cond = 0;
2769 int n_sets_before_autoinc;
2770 int n_strict_low_parts = 0;
2771 struct cselib_record_autoinc_data data;
2773 rtx body = PATTERN (insn);
2774 if (GET_CODE (body) == COND_EXEC)
2776 cond = COND_EXEC_TEST (body);
2777 body = COND_EXEC_CODE (body);
2780 /* Find all sets. */
2781 if (GET_CODE (body) == SET)
2783 sets[0].src = SET_SRC (body);
2784 sets[0].dest = SET_DEST (body);
2785 n_sets = 1;
2787 else if (GET_CODE (body) == PARALLEL)
2789 /* Look through the PARALLEL and record the values being
2790 set, if possible. Also handle any CLOBBERs. */
2791 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2793 rtx x = XVECEXP (body, 0, i);
2795 if (GET_CODE (x) == SET)
2797 sets[n_sets].src = SET_SRC (x);
2798 sets[n_sets].dest = SET_DEST (x);
2799 n_sets++;
2804 if (n_sets == 1
2805 && MEM_P (sets[0].src)
2806 && !cselib_record_memory
2807 && MEM_READONLY_P (sets[0].src))
2809 rtx note = find_reg_equal_equiv_note (insn);
2811 if (note && CONSTANT_P (XEXP (note, 0)))
2812 sets[0].src = XEXP (note, 0);
2815 data.sets = sets;
2816 data.n_sets = n_sets_before_autoinc = n_sets;
2817 for_each_inc_dec (PATTERN (insn), cselib_record_autoinc_cb, &data);
2818 n_sets = data.n_sets;
2820 /* Look up the values that are read. Do this before invalidating the
2821 locations that are written. */
2822 for (i = 0; i < n_sets; i++)
2824 rtx dest = sets[i].dest;
2825 rtx orig = dest;
2827 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2828 the low part after invalidating any knowledge about larger modes. */
2829 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2830 sets[i].dest = dest = XEXP (dest, 0);
2832 /* We don't know how to record anything but REG or MEM. */
2833 if (REG_P (dest)
2834 || (MEM_P (dest) && cselib_record_memory))
2836 rtx src = sets[i].src;
2837 if (cond)
2838 src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
2839 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
2840 if (MEM_P (dest))
2842 machine_mode address_mode = get_address_mode (dest);
2844 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
2845 address_mode, 1,
2846 GET_MODE (dest));
2848 else
2849 sets[i].dest_addr_elt = 0;
2852 /* Improve handling of STRICT_LOW_PART if the current value is known
2853 to be const0_rtx, then the low bits will be set to dest and higher
2854 bits will remain zero. Used in code like:
2856 {di:SI=0;clobber flags:CC;}
2857 flags:CCNO=cmp(bx:SI,0)
2858 strict_low_part(di:QI)=flags:CCNO<=0
2860 where we can note both that di:QI=flags:CCNO<=0 and
2861 also that because di:SI is known to be 0 and strict_low_part(di:QI)
2862 preserves the upper bits that di:SI=zero_extend(flags:CCNO<=0). */
2863 scalar_int_mode mode;
2864 if (dest != orig
2865 && cselib_record_sets_hook
2866 && REG_P (dest)
2867 && HARD_REGISTER_P (dest)
2868 && sets[i].src_elt
2869 && is_a <scalar_int_mode> (GET_MODE (dest), &mode)
2870 && n_sets + n_strict_low_parts < MAX_SETS)
2872 opt_scalar_int_mode wider_mode_iter;
2873 FOR_EACH_WIDER_MODE (wider_mode_iter, mode)
2875 scalar_int_mode wider_mode = wider_mode_iter.require ();
2876 if (GET_MODE_PRECISION (wider_mode) > BITS_PER_WORD)
2877 break;
2879 rtx reg = gen_lowpart (wider_mode, dest);
2880 if (!REG_P (reg))
2881 break;
2883 cselib_val *v = cselib_lookup (reg, wider_mode, 0, VOIDmode);
2884 if (!v)
2885 continue;
2887 struct elt_loc_list *l;
2888 for (l = v->locs; l; l = l->next)
2889 if (l->loc == const0_rtx)
2890 break;
2892 if (!l)
2893 continue;
2895 sets[n_sets + n_strict_low_parts].dest = reg;
2896 sets[n_sets + n_strict_low_parts].src = dest;
2897 sets[n_sets + n_strict_low_parts++].src_elt = sets[i].src_elt;
2898 break;
2903 if (cselib_record_sets_hook)
2904 cselib_record_sets_hook (insn, sets, n_sets);
2906 /* Invalidate all locations written by this insn. Note that the elts we
2907 looked up in the previous loop aren't affected, just some of their
2908 locations may go away. */
2909 note_pattern_stores (body, cselib_invalidate_rtx_note_stores, NULL);
2911 for (i = n_sets_before_autoinc; i < n_sets; i++)
2912 cselib_invalidate_rtx (sets[i].dest);
2914 /* If this is an asm, look for duplicate sets. This can happen when the
2915 user uses the same value as an output multiple times. This is valid
2916 if the outputs are not actually used thereafter. Treat this case as
2917 if the value isn't actually set. We do this by smashing the destination
2918 to pc_rtx, so that we won't record the value later. */
2919 if (n_sets >= 2 && asm_noperands (body) >= 0)
2921 for (i = 0; i < n_sets; i++)
2923 rtx dest = sets[i].dest;
2924 if (REG_P (dest) || MEM_P (dest))
2926 int j;
2927 for (j = i + 1; j < n_sets; j++)
2928 if (rtx_equal_p (dest, sets[j].dest))
2930 sets[i].dest = pc_rtx;
2931 sets[j].dest = pc_rtx;
2937 /* Now enter the equivalences in our tables. */
2938 for (i = 0; i < n_sets; i++)
2940 rtx dest = sets[i].dest;
2941 if (REG_P (dest)
2942 || (MEM_P (dest) && cselib_record_memory))
2943 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2946 /* And deal with STRICT_LOW_PART. */
2947 for (i = 0; i < n_strict_low_parts; i++)
2949 if (! PRESERVED_VALUE_P (sets[n_sets + i].src_elt->val_rtx))
2950 continue;
2951 machine_mode dest_mode = GET_MODE (sets[n_sets + i].dest);
2952 cselib_val *v
2953 = cselib_lookup (sets[n_sets + i].dest, dest_mode, 1, VOIDmode);
2954 cselib_preserve_value (v);
2955 rtx r = gen_rtx_ZERO_EXTEND (dest_mode,
2956 sets[n_sets + i].src_elt->val_rtx);
2957 cselib_add_permanent_equiv (v, r, insn);
2961 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
2963 bool
2964 fp_setter_insn (rtx_insn *insn)
2966 rtx expr, pat = NULL_RTX;
2968 if (!RTX_FRAME_RELATED_P (insn))
2969 return false;
2971 expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2972 if (expr)
2973 pat = XEXP (expr, 0);
2974 if (!modified_in_p (hard_frame_pointer_rtx, pat ? pat : insn))
2975 return false;
2977 /* Don't return true for frame pointer restores in the epilogue. */
2978 if (find_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx))
2979 return false;
2980 return true;
2983 /* V is one of the values in REG_VALUES (REGNO). Return true if it
2984 would be invalidated by CALLEE_ABI. */
2986 static bool
2987 cselib_invalidated_by_call_p (const function_abi &callee_abi,
2988 unsigned int regno, cselib_val *v)
2990 machine_mode mode = GET_MODE (v->val_rtx);
2991 if (mode == VOIDmode)
2993 v = REG_VALUES (regno)->elt;
2994 if (!v)
2995 /* If we don't know what the mode of the constant value is, and we
2996 don't know what mode the register was set in, conservatively
2997 assume that the register is clobbered. The value's going to be
2998 essentially useless in this case anyway. */
2999 return true;
3000 mode = GET_MODE (v->val_rtx);
3002 return callee_abi.clobbers_reg_p (mode, regno);
3005 /* Record the effects of INSN. */
3007 void
3008 cselib_process_insn (rtx_insn *insn)
3010 int i;
3011 rtx x;
3013 cselib_current_insn = insn;
3015 /* Forget everything at a CODE_LABEL or a setjmp. */
3016 if ((LABEL_P (insn)
3017 || (CALL_P (insn)
3018 && find_reg_note (insn, REG_SETJMP, NULL)))
3019 && !cselib_preserve_constants)
3021 cselib_reset_table (next_uid);
3022 cselib_current_insn = NULL;
3023 return;
3026 if (! INSN_P (insn))
3028 cselib_current_insn = NULL;
3029 return;
3032 /* If this is a call instruction, forget anything stored in a
3033 call clobbered register, or, if this is not a const call, in
3034 memory. */
3035 if (CALL_P (insn))
3037 function_abi callee_abi = insn_callee_abi (insn);
3038 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3040 elt_list **l = &REG_VALUES (i);
3041 while (*l)
3043 cselib_val *v = (*l)->elt;
3044 if (v && cselib_invalidated_by_call_p (callee_abi, i, v))
3045 cselib_invalidate_regno_val (i, l);
3046 else
3047 l = &(*l)->next;
3051 /* Since it is not clear how cselib is going to be used, be
3052 conservative here and treat looping pure or const functions
3053 as if they were regular functions. */
3054 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
3055 || !(RTL_CONST_OR_PURE_CALL_P (insn)))
3056 cselib_invalidate_mem (callmem);
3057 else
3058 /* For const/pure calls, invalidate any argument slots because
3059 they are owned by the callee. */
3060 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
3061 if (GET_CODE (XEXP (x, 0)) == USE
3062 && MEM_P (XEXP (XEXP (x, 0), 0)))
3063 cselib_invalidate_mem (XEXP (XEXP (x, 0), 0));
3066 cselib_record_sets (insn);
3068 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
3069 after we have processed the insn. */
3070 if (CALL_P (insn))
3072 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
3073 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
3074 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
3076 /* Flush everything on setjmp. */
3077 if (cselib_preserve_constants
3078 && find_reg_note (insn, REG_SETJMP, NULL))
3080 cselib_preserve_only_values ();
3081 cselib_reset_table (next_uid);
3085 /* On setter of the hard frame pointer if frame_pointer_needed,
3086 invalidate stack_pointer_rtx, so that sp and {,h}fp based
3087 VALUEs are distinct. */
3088 if (reload_completed
3089 && frame_pointer_needed
3090 && fp_setter_insn (insn))
3091 cselib_invalidate_rtx (stack_pointer_rtx);
3093 cselib_current_insn = NULL;
3095 if (n_useless_values > MAX_USELESS_VALUES
3096 /* remove_useless_values is linear in the hash table size. Avoid
3097 quadratic behavior for very large hashtables with very few
3098 useless elements. */
3099 && ((unsigned int)n_useless_values
3100 > (cselib_hash_table->elements () - n_debug_values) / 4))
3101 remove_useless_values ();
3104 /* Initialize cselib for one pass. The caller must also call
3105 init_alias_analysis. */
3107 void
3108 cselib_init (int record_what)
3110 cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
3111 cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
3112 cselib_any_perm_equivs = false;
3114 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
3115 see canon_true_dependence. This is only created once. */
3116 if (! callmem)
3117 callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
3119 cselib_nregs = max_reg_num ();
3121 /* We preserve reg_values to allow expensive clearing of the whole thing.
3122 Reallocate it however if it happens to be too large. */
3123 if (!reg_values || reg_values_size < cselib_nregs
3124 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
3126 free (reg_values);
3127 /* Some space for newly emit instructions so we don't end up
3128 reallocating in between passes. */
3129 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
3130 reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
3132 used_regs = XNEWVEC (unsigned int, cselib_nregs);
3133 n_used_regs = 0;
3134 /* FIXME: enable sanitization (PR87845) */
3135 cselib_hash_table
3136 = new hash_table<cselib_hasher> (31, /* ggc */ false,
3137 /* sanitize_eq_and_hash */ false);
3138 if (cselib_preserve_constants)
3139 cselib_preserved_hash_table
3140 = new hash_table<cselib_hasher> (31, /* ggc */ false,
3141 /* sanitize_eq_and_hash */ false);
3142 next_uid = 1;
3145 /* Called when the current user is done with cselib. */
3147 void
3148 cselib_finish (void)
3150 bool preserved = cselib_preserve_constants;
3151 cselib_discard_hook = NULL;
3152 cselib_preserve_constants = false;
3153 cselib_any_perm_equivs = false;
3154 cfa_base_preserved_val = NULL;
3155 cfa_base_preserved_regno = INVALID_REGNUM;
3156 elt_list_pool.release ();
3157 elt_loc_list_pool.release ();
3158 cselib_val_pool.release ();
3159 value_pool.release ();
3160 cselib_clear_table ();
3161 delete cselib_hash_table;
3162 cselib_hash_table = NULL;
3163 if (preserved)
3164 delete cselib_preserved_hash_table;
3165 cselib_preserved_hash_table = NULL;
3166 free (used_regs);
3167 used_regs = 0;
3168 n_useless_values = 0;
3169 n_useless_debug_values = 0;
3170 n_debug_values = 0;
3171 next_uid = 0;
3174 /* Dump the cselib_val *X to FILE *OUT. */
3177 dump_cselib_val (cselib_val **x, FILE *out)
3179 cselib_val *v = *x;
3180 bool need_lf = true;
3182 print_inline_rtx (out, v->val_rtx, 0);
3184 if (v->locs)
3186 struct elt_loc_list *l = v->locs;
3187 if (need_lf)
3189 fputc ('\n', out);
3190 need_lf = false;
3192 fputs (" locs:", out);
3195 if (l->setting_insn)
3196 fprintf (out, "\n from insn %i ",
3197 INSN_UID (l->setting_insn));
3198 else
3199 fprintf (out, "\n ");
3200 print_inline_rtx (out, l->loc, 4);
3202 while ((l = l->next));
3203 fputc ('\n', out);
3205 else
3207 fputs (" no locs", out);
3208 need_lf = true;
3211 if (v->addr_list)
3213 struct elt_list *e = v->addr_list;
3214 if (need_lf)
3216 fputc ('\n', out);
3217 need_lf = false;
3219 fputs (" addr list:", out);
3222 fputs ("\n ", out);
3223 print_inline_rtx (out, e->elt->val_rtx, 2);
3225 while ((e = e->next));
3226 fputc ('\n', out);
3228 else
3230 fputs (" no addrs", out);
3231 need_lf = true;
3234 if (v->next_containing_mem == &dummy_val)
3235 fputs (" last mem\n", out);
3236 else if (v->next_containing_mem)
3238 fputs (" next mem ", out);
3239 print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
3240 fputc ('\n', out);
3242 else if (need_lf)
3243 fputc ('\n', out);
3245 return 1;
3248 /* Dump to OUT everything in the CSELIB table. */
3250 void
3251 dump_cselib_table (FILE *out)
3253 fprintf (out, "cselib hash table:\n");
3254 cselib_hash_table->traverse <FILE *, dump_cselib_val> (out);
3255 fprintf (out, "cselib preserved hash table:\n");
3256 cselib_preserved_hash_table->traverse <FILE *, dump_cselib_val> (out);
3257 if (first_containing_mem != &dummy_val)
3259 fputs ("first mem ", out);
3260 print_inline_rtx (out, first_containing_mem->val_rtx, 2);
3261 fputc ('\n', out);
3263 fprintf (out, "next uid %i\n", next_uid);
3266 #include "gt-cselib.h"